cart_tmp_wgpu/
macros.rs

1//! Convenience macros
2
3/// Macro to produce an array of [`VertexAttributeDescriptor`].
4///
5/// Output has type: `[VertexAttributeDescriptor; _]`. Usage is as follows:
6/// ```
7/// # use wgpu::vertex_attr_array;
8/// let attrs = vertex_attr_array![0 => Float2, 1 => Float, 2 => Ushort4];
9/// ```
10/// This example specifies a list of three [`VertexAttributeDescriptor`],
11/// each with the given `shader_location` and `format`.
12/// Offsets are calculated automatically.
13#[macro_export]
14macro_rules! vertex_attr_array {
15    ($($loc:expr => $fmt:ident),* $(,)?) => {
16        $crate::vertex_attr_array!([] ; 0; $($loc => $fmt ,)*)
17    };
18    ([$($t:expr,)*] ; $off:expr ;) => { [$($t,)*] };
19    ([$($t:expr,)*] ; $off:expr ; $loc:expr => $item:ident, $($ll:expr => $ii:ident ,)*) => {
20        $crate::vertex_attr_array!(
21            [$($t,)*
22            $crate::VertexAttributeDescriptor {
23                format: $crate::VertexFormat :: $item,
24                offset: $off,
25                shader_location: $loc,
26            },];
27            $off + $crate::vertex_format_size!($item);
28            $($ll => $ii ,)*
29        )
30    };
31}
32
33/// Helper macro which turns a vertex attribute type into a size.
34///
35/// Mainly used as a helper for [`vertex_attr_array`] but might be externally useful.
36#[macro_export]
37macro_rules! vertex_format_size {
38    (Uchar2) => {
39        2
40    };
41    (Uchar4) => {
42        4
43    };
44    (Char2) => {
45        2
46    };
47    (Char4) => {
48        4
49    };
50    (Uchar2Norm) => {
51        2
52    };
53    (Uchar4Norm) => {
54        4
55    };
56    (Char2Norm) => {
57        2
58    };
59    (Char4Norm) => {
60        4
61    };
62    (Ushort2) => {
63        4
64    };
65    (Ushort4) => {
66        8
67    };
68    (Short2) => {
69        4
70    };
71    (Short4) => {
72        8
73    };
74    (Ushort2Norm) => {
75        4
76    };
77    (Ushort4Norm) => {
78        8
79    };
80    (Short2Norm) => {
81        4
82    };
83    (Short4Norm) => {
84        8
85    };
86    (Half2) => {
87        4
88    };
89    (Half4) => {
90        8
91    };
92    (Float) => {
93        4
94    };
95    (Float2) => {
96        8
97    };
98    (Float3) => {
99        12
100    };
101    (Float4) => {
102        16
103    };
104    (Uint) => {
105        4
106    };
107    (Uint2) => {
108        8
109    };
110    (Uint3) => {
111        12
112    };
113    (Uint4) => {
114        16
115    };
116    (Int) => {
117        4
118    };
119    (Int2) => {
120        8
121    };
122    (Int3) => {
123        12
124    };
125    (Int4) => {
126        16
127    };
128}
129
130#[test]
131fn test_vertex_attr_array() {
132    let attrs = vertex_attr_array![0 => Float2, 3 => Ushort4];
133    // VertexAttributeDescriptor does not support PartialEq, so we cannot test directly
134    assert_eq!(attrs.len(), 2);
135    assert_eq!(attrs[0].offset, 0);
136    assert_eq!(attrs[0].shader_location, 0);
137    assert_eq!(attrs[1].offset, std::mem::size_of::<(f32, f32)>() as u64);
138    assert_eq!(attrs[1].shader_location, 3);
139}
140
141/// Macro to load a SPIR-V module statically.
142///
143/// It ensure the word alignment as well as the magic number.
144#[macro_export]
145macro_rules! include_spirv {
146    ($($token:tt)*) => {
147        $crate::util::make_spirv(&$crate::util::WordAligned(*include_bytes!($($token)*)).0)
148    };
149}