wgpu/
macros.rs

1//! Convenience macros
2
3/// Macro to produce an array of [`VertexAttribute`](crate::VertexAttribute).
4///
5/// Output has type: `[VertexAttribute; _]`. Usage is as follows:
6/// ```
7/// # use wgpu::vertex_attr_array;
8/// let attrs = vertex_attr_array![0 => Float32x2, 1 => Float32, 2 => Uint16x4];
9/// ```
10/// This example specifies a list of three [`VertexAttribute`](crate::VertexAttribute),
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::VertexAttribute {
23                format: $crate::VertexFormat :: $item,
24                offset: $off,
25                shader_location: $loc,
26            },];
27            $off + $crate::VertexFormat :: $item.size();
28            $($ll => $ii ,)*
29        )
30    };
31}
32
33#[test]
34fn test_vertex_attr_array() {
35    let attrs = vertex_attr_array![0 => Float32x2, 3 => Uint16x4];
36    // VertexAttribute does not support PartialEq, so we cannot test directly
37    assert_eq!(attrs.len(), 2);
38    assert_eq!(attrs[0].offset, 0);
39    assert_eq!(attrs[0].shader_location, 0);
40    assert_eq!(attrs[1].offset, size_of::<(f32, f32)>() as u64);
41    assert_eq!(attrs[1].shader_location, 3);
42}
43
44/// Macro to load a SPIR-V module statically.
45///
46/// It ensures the word alignment as well as the magic number.
47///
48/// Return type: [`crate::ShaderModuleDescriptor`]
49#[macro_export]
50#[cfg(feature = "spirv")]
51macro_rules! include_spirv {
52    ($($token:tt)*) => {
53        {
54            //log::info!("including '{}'", $($token)*);
55            $crate::ShaderModuleDescriptor {
56                label: Some($($token)*),
57                source: $crate::util::make_spirv(include_bytes!($($token)*)),
58            }
59        }
60    };
61}
62
63/// Macro to load raw SPIR-V data statically, for use with [`Features::SPIRV_SHADER_PASSTHROUGH`].
64///
65/// It ensures the word alignment as well as the magic number.
66///
67/// [`Features::SPIRV_SHADER_PASSTHROUGH`]: crate::Features::SPIRV_SHADER_PASSTHROUGH
68#[macro_export]
69macro_rules! include_spirv_raw {
70    ($($token:tt)*) => {
71        {
72            //log::info!("including '{}'", $($token)*);
73            $crate::ShaderModuleDescriptorSpirV {
74                label: Some($($token)*),
75                source: $crate::util::make_spirv_raw(include_bytes!($($token)*)),
76            }
77        }
78    };
79}
80
81/// Load WGSL source code from a file at compile time.
82///
83/// The loaded path is relative to the path of the file containing the macro call, in the same way
84/// as [`include_str!`] operates.
85///
86/// ```ignore
87/// fn main() {
88///     let module: ShaderModuleDescriptor = include_wgsl!("shader.wgsl");
89/// }
90/// ```
91#[macro_export]
92macro_rules! include_wgsl {
93    ($($token:tt)*) => {
94        {
95            //log::info!("including '{}'", $($token)*);
96            $crate::ShaderModuleDescriptor {
97                label: Some($($token)*),
98                source: $crate::ShaderSource::Wgsl(std::borrow::Cow::Borrowed(include_str!($($token)*))),
99            }
100        }
101    };
102}