wgpu_macros/
lib.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
extern crate proc_macro;
use proc_macro::TokenStream;
use proc_macro2::Ident;

use quote::{format_ident, quote};
use syn::spanned::Spanned;
use syn::{
  parse_macro_input, Data, DeriveInput, Expr, Fields, Lit, Meta, NestedMeta,
  Type,
};

enum Attr {
  Override(Ident),
  Norm(bool),
}

/// Allows for generation of a `wgpu::VertexBufferLayout`, which can be accessed
/// by the `LAYOUT` constant on the macro.
///
/// ```
/// # use wgpu;
/// # use wgpu_macros::VertexLayout;
/// # #[repr(C)]
/// #[derive(VertexLayout)]
/// struct Vertex {
///   position: [f32; 3],
///   tex_coords: [f32; 2],
/// }
///
/// fn main() {
///   Vertex::LAYOUT; // use in a RenderPipelineDescriptor
/// }
/// ```
///
/// # Changing `step_mode`
/// By default, the `step_mode` is set to `Vertex`.
/// To change the `step_mode` for the `VertexBufferLayout`, you can declare the
/// `layout` attribute macro for the struct, and passing one of the variants
/// of `wgpu::VertexStepMode`.
///
/// ```
/// # use wgpu_macros::VertexLayout;
/// # #[repr(C)]
/// #[derive(VertexLayout)]
/// #[layout(Instance)]
/// struct Vertex {}
/// ```
///
/// # Using `norm` Variants
///
/// By specifying `norm` the `layout` attribute macro for the field you want,
/// it will use the `norm` variant corresponding to the field value.
///
/// ```
/// # use wgpu_macros::VertexLayout;
/// # #[repr(C)]
/// #[derive(VertexLayout)]
/// struct Vertex {
///   # position: [f32; 3],
///   #[layout(norm)]
///   tex_coords: [u8; 2],
/// }
/// ```
///
/// So `Uint8x2` becomes `Unorm8x2`.
///
/// # Overriding Generated `VertexFormat`
///
/// By specifying the wanted `VertexFormat` in the `layout` attribute macro for
/// the field you want, you can override the generated `VertexFormat`.
///
/// ```
/// # use wgpu_macros::VertexLayout;
/// # #[repr(C)]
/// #[derive(VertexLayout)]
/// struct Vertex {
///   # position: [f32; 3],
///   #[layout(Uint16x4)]
///   tex_coords: [f32; 2],
/// }
/// ```
#[proc_macro_derive(VertexLayout, attributes(layout))]
pub fn vertex_layout(input: TokenStream) -> TokenStream {
  let input = parse_macro_input!(input as DeriveInput);

  let step_mode = input
    .attrs
    .into_iter()
    .find_map(|attr| {
      if *attr.path.get_ident().unwrap() == "layout" {
        match attr.parse_meta().unwrap() {
          Meta::List(list) => match list.nested.into_iter().next().unwrap() {
            NestedMeta::Meta(Meta::Path(path)) => {
              let ident = path.get_ident().unwrap();
              if matches!(ident.to_string().as_ref(), "Vertex" | "Instance") {
                Some(ident.clone())
              } else {
                panic!("Invalid value")
              }
            }
            _ => panic!("Invalid value"),
          },
          _ => panic!("Invalid value"),
        }
      } else {
        None
      }
    })
    .unwrap_or_else(|| format_ident!("Vertex"));

  let name = input.ident;

  let data = match input.data {
    Data::Struct(data) => data,
    _ => panic!("Only structs can derive VertexLayout"),
  };
  let fields = match data.fields {
    Fields::Named(fields) => fields.named,
    Fields::Unnamed(fields) => fields.unnamed,
    Fields::Unit => panic!("Unit structs arent allowed for VertexLayout"),
  };

  let vertices = fields.into_iter().enumerate().map(|(n, field)| {
    let span = field.span();
    let attr = field
      .attrs
      .into_iter()
      .find_map(|attr| {
        if *attr.path.get_ident().unwrap() == "layout" {
          match attr.parse_meta().unwrap() {
            Meta::List(list) => match list.nested.into_iter().next().unwrap() {
              NestedMeta::Meta(Meta::Path(path)) => {
                let ident = path.get_ident().unwrap();
                if *ident == "norm" {
                  Some(Attr::Norm(true))
                } else {
                  Some(Attr::Override(ident.clone()))
                }
              }
              _ => panic!("Invalid value"),
            },
            _ => panic!("Invalid value"),
          }
        } else {
          None
        }
      })
      .unwrap_or(Attr::Norm(false));

    let ident = match attr {
      Attr::Override(ident) => ident,
      Attr::Norm(norm) => {
        let (ty, len) = match field.ty {
          Type::Array(array) => {
            let len = match array.len {
              Expr::Lit(lit) => match lit.lit {
                Lit::Int(int) => int,
                _ => unreachable!(),
              },
              _ => unreachable!(),
            };
            let ty = match *array.elem {
              Type::Path(p) => p.path,
              _ => unreachable!(),
            };

            (ty, len.base10_parse::<usize>().unwrap())
          }
          Type::Path(path) => (path.path, 1),
          ty => panic!("Type '{:?}' isnt allowed for VertexLayout", ty),
        };
        let ty = ty.segments.into_iter().last().unwrap().ident.to_string();

        let full_type = match (ty.as_ref(), norm) {
          ("u8", false) => "Uint8",
          ("u8", true) => "Unorm8",
          ("i8", false) => "Sint8",
          ("i8", true) => "Snorm8",

          ("u16", false) => "Uint16",
          ("u16", true) => "Unorm16",
          ("i16", false) => "Sint16",
          ("i16", true) => "Snorm16",

          ("f32", false) => "Float32",
          ("u32", false) => "Uint32",
          ("i32", false) => "Sint32",

          ("f64", false) => "Float64",
          (ty, true) => panic!("Type '{ty}' cannot be normalized"),
          (ty, _) => panic!("Type '{ty}' is not allowed"),
        };

        match (full_type, len) {
          ("Uint8", 2 | 4) => {}
          ("Unorm8", 2 | 4) => {}
          ("Sint8", 2 | 4) => {}
          ("Snorm8", 2 | 4) => {}
          ("Uint16", 2 | 4) => {}
          ("Unorm16", 2 | 4) => {}
          ("Sint16", 2 | 4) => {}
          ("Snorm16", 2 | 4) => {}
          ("Float32", 1 | 2 | 3 | 4) => {}
          ("Uint32", 1 | 2 | 3 | 4) => {}
          ("Sint32", 1 | 2 | 3 | 4) => {}
          ("Float64", 1 | 2 | 3 | 4) => {}
          (_, len) => panic!("Type '{ty}' cannot be used {len} times"),
        }

        if len == 1 {
          quote::format_ident!("{full_type}", span = span)
        } else {
          quote::format_ident!("{full_type}x{}", len.to_string(), span = span)
        }
      }
    };

    let n = n as u32;
    quote!(#n => #ident)
  });

  let tokens = quote! {
    impl #name {
      pub const LAYOUT: wgpu::VertexBufferLayout<'static> = wgpu::VertexBufferLayout {
        array_stride: std::mem::size_of::<#name>() as wgpu::BufferAddress,
        step_mode: wgpu::VertexStepMode::#step_mode,
        attributes: &wgpu::vertex_attr_array![#(#vertices),*],
      };
    }
  };

  tokens.into()
}