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