Skip to content

Commit

Permalink
Simplified the derive macro quite a bit, need to investigate serde to…
Browse files Browse the repository at this point in the history
… evaluate how to do this sort of stuff.
  • Loading branch information
joaoccmartins committed Oct 10, 2024
1 parent 1e0711b commit f1c819c
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 75 deletions.
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,4 @@ members = [
"crates/rusteroids",
"crates/wgpu_utils",
"crates/wgpu_utils/vertex_attribute_derive",
"crates/wgpu_utils/wgpu_utils_base",
]
8 changes: 6 additions & 2 deletions crates/rusteroids/src/mesh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use std::ops::Range;

use glam::Mat4;
use wgpu::util::DeviceExt;
use wgpu::{Queue, RenderPass};
use wgpu_utils::{Attribute, VertexAttributeArray};
use wgpu::{Queue, RenderPass, VertexAttribute};
use wgpu_utils::VertexAttributeArray;

use crate::utils::{common_layout_descriptor, Bindable, UniformBuffer};
#[repr(C)]
Expand All @@ -13,6 +13,10 @@ pub struct Vertex {
pub color: [f32; 3],
}

impl Vertex {
const ATTR: [VertexAttribute; 2] = wgpu::vertex_attr_array![0 => Float32x2, 1 => Float32x3];
}

pub struct Geometry {
vertex_buffer: Option<wgpu::Buffer>,
model_uniform: Option<UniformBuffer>,
Expand Down
2 changes: 1 addition & 1 deletion crates/wgpu_utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ edition = "2021"

[dependencies]
vertex_attribute_derive = { path = "vertex_attribute_derive" }
wgpu_utils_base = { path = "wgpu_utils_base" }
wgpu = "22.0"
7 changes: 6 additions & 1 deletion crates/wgpu_utils/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
pub use vertex_attribute_derive::VertexAttributeArray;
pub use wgpu_utils_base::{Attribute, VertexAttributeArray};

pub trait VertexAttributeArray {
/// Generates a VertexBufferLayout to be used in a RenderPipeline
/// TODO: refactor static out of it.
fn desc() -> wgpu::VertexBufferLayout<'static>;
}
3 changes: 1 addition & 2 deletions crates/wgpu_utils/vertex_attribute_derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,4 @@ proc-macro = true
[dependencies]
quote = "1.0.37"
syn = "2.0.79"
wgpu = "22.0"
wgpu_utils_base = {path = "../wgpu_utils_base" }
wgpu = "22.0"
58 changes: 18 additions & 40 deletions crates/wgpu_utils/vertex_attribute_derive/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,23 @@
use proc_macro::TokenStream;
use quote::quote;
use syn::{Data, Fields};
use wgpu_utils_base::Attribute;

/// Ensures the struct is capable of generating a VertexBufferLayout
/// by calling desc. Requires the existing of a ```const ATTR: [wgpu::VertexAttribute; N]```
///
/// Example
/// ```
/// struct Vertex {
/// pos: [f32; 3]
/// uv: [f32; 2]
/// }
///
/// impl Vertex{
/// const ATTR: [wgpu::VertexAttribute; 2] = wpug::vertex_attr_array![0 => Float32x3; 1 Float32x2];
/// }
/// ```
///
/// TODO: make a bind_to_group(binding: u32, bind_group: &BindGroup, offsets: &[DynamicOffset])
/// TODO: refactor ATTR requirement out
#[proc_macro_derive(VertexAttributeArray)]
pub fn vertex_attribute_derive(input: TokenStream) -> TokenStream {
// Construct a representation of Rust code as a syntax tree
Expand All @@ -15,50 +30,13 @@ pub fn vertex_attribute_derive(input: TokenStream) -> TokenStream {

fn impl_vertex_attribute(ast: &syn::DeriveInput) -> TokenStream {
let name = &ast.ident;

let fields = match &ast.data {
Data::Struct(data) => {
if let Fields::Named(named_fields) = &data.fields {
named_fields.named.clone()
} else {
panic!("#[derive(VertexAttributeArray)] is only supported on structs with named fields");
}
}
// TODO: Add enums
_ => panic!("#[derive(VertexAttributeArray)] is only supported on structs"),
};

// Generate the vertex attributes
let field_types = fields.iter().enumerate().map(|(i, f)| {
let ty = &f.ty;
quote! {
wgpu::VertexAttribute {
format: <#ty as Attribute>::format(),
offset: <#ty as Attribute>::format().size(),
shader_location: #i as u32,
}
}
});

// The length of the array to be generated
let attrs_array_len = fields.len();

let gen = quote! {
impl #name {
pub const fn attrs() -> &'static [wgpu::VertexAttribute; #attrs_array_len] {
static ATTRS: [wgpu::VertexAttribute; #attrs_array_len] = [
#(#field_types),*
];
&ATTRS
}
}

impl VertexAttributeArray for #name {
fn desc() -> wgpu::VertexBufferLayout<'static> {
wgpu::VertexBufferLayout {
array_stride: std::mem::size_of::<Self>() as wgpu::BufferAddress,
step_mode: wgpu::VertexStepMode::Vertex,
attributes: Self::attrs(),
attributes: &Self::ATTR,
}
}
}
Expand Down
7 changes: 0 additions & 7 deletions crates/wgpu_utils/wgpu_utils_base/Cargo.toml

This file was deleted.

21 changes: 0 additions & 21 deletions crates/wgpu_utils/wgpu_utils_base/src/lib.rs

This file was deleted.

0 comments on commit f1c819c

Please sign in to comment.