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