wgpu/macros/
mod.rs

1//! Convenience macros
2
3#[cfg(doc)]
4use crate::{VertexAttribute, VertexBufferLayout, VertexFormat};
5
6/// Macro to produce an array of [`VertexAttribute`].
7///
8/// The input is a sequence of pairs of shader locations (expression of type [`u32`]) and
9/// variant names of [`VertexFormat`].
10///
11/// The return value has type `[VertexAttribute; N]`, where `N` is the number of inputs.
12///
13/// Offsets are calculated automatically,
14/// using the assumption that there is no padding or other data between attributes.
15///
16/// # Example
17///
18/// ```
19/// // Suppose that this is our vertex format:
20/// #[repr(C, packed)]
21/// struct Vertex {
22///     foo: [f32; 2],
23///     bar: f32,
24///     baz: [u16; 4],
25/// }
26///
27/// // Then these attributes match it:
28/// let attrs = wgpu::vertex_attr_array![
29///     0 => Float32x2,
30///     1 => Float32,
31///     2 => Uint16x4,
32/// ];
33///
34/// // Here's the full data structure the macro produced:
35/// use wgpu::{VertexAttribute as A, VertexFormat as F};
36/// assert_eq!(attrs, [
37///     A { format: F::Float32x2, offset:  0, shader_location: 0, },
38///     A { format: F::Float32,   offset:  8, shader_location: 1, },
39///     A { format: F::Uint16x4,  offset: 12, shader_location: 2, },
40/// ]);
41/// ```
42///
43/// See [`VertexBufferLayout`] for an example building on this one.
44#[macro_export]
45macro_rules! vertex_attr_array {
46    ($($location:expr => $format:ident),* $(,)?) => {
47        $crate::_vertex_attr_array_helper!([] ; 0; $($location => $format ,)*)
48    };
49}
50
51#[doc(hidden)]
52#[macro_export]
53macro_rules! _vertex_attr_array_helper {
54    ([$($t:expr,)*] ; $off:expr ;) => { [$($t,)*] };
55    ([$($t:expr,)*] ; $off:expr ; $location:expr => $format:ident, $($ll:expr => $ii:ident ,)*) => {
56        $crate::_vertex_attr_array_helper!(
57            [$($t,)*
58            $crate::VertexAttribute {
59                format: $crate::VertexFormat :: $format,
60                offset: $off,
61                shader_location: $location,
62            },];
63            $off + $crate::VertexFormat :: $format.size();
64            $($ll => $ii ,)*
65        )
66    };
67}
68
69#[test]
70fn test_vertex_attr_array() {
71    let attrs = vertex_attr_array![0 => Float32x2, 3 => Uint16x4];
72    // VertexAttribute does not support PartialEq, so we cannot test directly
73    assert_eq!(attrs.len(), 2);
74    assert_eq!(attrs[0].offset, 0);
75    assert_eq!(attrs[0].shader_location, 0);
76    assert_eq!(attrs[1].offset, size_of::<(f32, f32)>() as u64);
77    assert_eq!(attrs[1].shader_location, 3);
78}
79
80#[macro_export]
81#[doc(hidden)]
82macro_rules! include_spirv_source {
83    ($($token:tt)*) => {
84        {
85            const SPIRV_SOURCE: [
86                u8;
87                $crate::__macro_helpers::include_bytes!($($token)*).len()
88            ] = *$crate::__macro_helpers::include_bytes!($($token)*);
89            const SPIRV_LEN: usize = SPIRV_SOURCE.len() / 4;
90            const SPIRV_WORDS: [u32; SPIRV_LEN] = $crate::util::make_spirv_const(SPIRV_SOURCE);
91            &SPIRV_WORDS
92        }
93    }
94}
95
96#[test]
97fn make_spirv_le_pass() {
98    static SPIRV: &[u32] = include_spirv_source!("le-aligned.spv");
99    assert_eq!(SPIRV, &[0x07230203, 0x11223344]);
100}
101
102#[test]
103fn make_spirv_be_pass() {
104    static SPIRV: &[u32] = include_spirv_source!("be-aligned.spv");
105    assert_eq!(SPIRV, &[0x07230203, 0x11223344]);
106}
107
108/// Macro to load a SPIR-V module statically.
109///
110/// It ensures the word alignment as well as the magic number.
111///
112/// Return type: [`crate::ShaderModuleDescriptor`]
113#[macro_export]
114#[cfg(feature = "spirv")]
115macro_rules! include_spirv {
116    ($($token:tt)*) => {
117        {
118            $crate::ShaderModuleDescriptor {
119                label: Some($($token)*),
120                source: $crate::ShaderSource::SpirV(
121                    $crate::__macro_helpers::Cow::Borrowed($crate::include_spirv_source!($($token)*))
122                ),
123            }
124        }
125    };
126}
127
128#[cfg(all(feature = "spirv", test))]
129#[expect(dead_code)]
130static SPIRV: crate::ShaderModuleDescriptor<'_> = include_spirv!("le-aligned.spv");
131
132/// Macro to load raw SPIR-V data statically, for use with [`Features::PASSTHROUGH_SHADERS`].
133///
134/// It ensures the word alignment as well as the magic number.
135///
136/// [`Features::PASSTHROUGH_SHADERS`]: crate::Features::PASSTHROUGH_SHADERS
137#[macro_export]
138macro_rules! include_spirv_raw {
139    ($($token:tt)*) => {
140        {
141            $crate::ShaderModuleDescriptorPassthrough {
142                label: $crate::__macro_helpers::Some($($token)*),
143                spirv: Some($crate::__macro_helpers::Cow::Borrowed($crate::include_spirv_source!($($token)*))),
144                entry_points: $crate::__macro_helpers::Cow::Borrowed(&[$crate::PassthroughShaderEntryPoint {
145                    name: $crate::__macro_helpers::Cow::Borrowed("main"),
146                    // This is unused for SPIR-V
147                    workgroup_size: (0, 0, 0),
148                }]),
149                dxil: None,
150                metallib: None,
151                msl: None,
152                hlsl: None,
153                glsl: None,
154                wgsl: None,
155            }
156        }
157    };
158}
159
160#[cfg(test)]
161#[expect(dead_code)]
162static SPIRV_RAW: crate::ShaderModuleDescriptorPassthrough<'_> =
163    include_spirv_raw!("le-aligned.spv");
164
165/// Load WGSL source code from a file at compile time.
166///
167/// The loaded path is relative to the path of the file containing the macro call, in the same way
168/// as [`include_str!`] operates.
169///
170/// ```ignore
171/// fn main() {
172///     let module: ShaderModuleDescriptor = include_wgsl!("shader.wgsl");
173/// }
174/// ```
175#[macro_export]
176macro_rules! include_wgsl {
177    ($($token:tt)*) => {
178        {
179            $crate::ShaderModuleDescriptor {
180                label: $crate::__macro_helpers::Some($($token)*),
181                source: $crate::ShaderSource::Wgsl($crate::__macro_helpers::Cow::Borrowed($crate::__macro_helpers::include_str!($($token)*))),
182            }
183        }
184    };
185}
186
187// Macros which help us generate the documentation of which hal types correspond to which backend.
188//
189// Because all backends are not compiled into the program, we cannot link to them in all situations,
190// we need to only link to the types if the backend is compiled in. These are used in #[doc] attributes
191// so cannot have more than one line, so cannot use internal cfgs.
192
193/// Helper macro to generate the documentation for dx12 hal methods, referencing the hal type.
194#[cfg(dx12)]
195macro_rules! hal_type_dx12 {
196    ($ty: literal) => {
197        concat!("- [`hal::api::Dx12`] uses [`hal::dx12::", $ty, "`]")
198    };
199}
200/// Helper macro to generate the documentation for dx12 hal methods, referencing the hal type.
201#[cfg(not(dx12))]
202macro_rules! hal_type_dx12 {
203    ($ty: literal) => {
204        concat!("- `hal::api::Dx12` uses `hal::dx12::", $ty, "`")
205    };
206}
207pub(crate) use hal_type_dx12;
208
209/// Helper macro to generate the documentation for metal hal methods, referencing the hal type.
210#[cfg(metal)]
211macro_rules! hal_type_metal {
212    ($ty: literal) => {
213        concat!("- [`hal::api::Metal`] uses [`hal::metal::", $ty, "`]")
214    };
215}
216/// Helper macro to generate the documentation for metal hal methods, referencing the hal type.
217#[cfg(not(metal))]
218macro_rules! hal_type_metal {
219    ($ty: literal) => {
220        concat!("- `hal::api::Metal` uses `hal::metal::", $ty, "`")
221    };
222}
223pub(crate) use hal_type_metal;
224
225/// Helper macro to generate the documentation for vulkan hal methods, referencing the hal type.
226#[cfg(vulkan)]
227macro_rules! hal_type_vulkan {
228    ($ty: literal) => {
229        concat!("- [`hal::api::Vulkan`] uses [`hal::vulkan::", $ty, "`]")
230    };
231}
232/// Helper macro to generate the documentation for vulkan hal methods, referencing the hal type.
233#[cfg(not(vulkan))]
234macro_rules! hal_type_vulkan {
235    ($ty: literal) => {
236        concat!("- `hal::api::Vulkan` uses `hal::vulkan::", $ty, "`")
237    };
238}
239pub(crate) use hal_type_vulkan;
240
241/// Helper macro to generate the documentation for gles hal methods, referencing the hal type.
242#[cfg(gles)]
243macro_rules! hal_type_gles {
244    ($ty: literal) => {
245        concat!("- [`hal::api::Gles`] uses [`hal::gles::", $ty, "`]")
246    };
247}
248/// Helper macro to generate the documentation for gles hal methods, referencing the hal type.
249#[cfg(not(gles))]
250macro_rules! hal_type_gles {
251    ($ty: literal) => {
252        concat!("- `hal::api::Gles` uses `hal::gles::", $ty, "`")
253    };
254}
255pub(crate) use hal_type_gles;
256
257#[doc(hidden)]
258pub mod helpers {
259    pub use alloc::{borrow::Cow, string::String};
260    pub use core::{include_bytes, include_str};
261    pub use Some;
262}