Skip to main content

naga/front/wgsl/parse/directive/
enable_extension.rs

1//! `enable …;` extensions in WGSL.
2//!
3//! The focal point of this module is the [`EnableExtension`] API.
4
5use crate::front::wgsl::{Error, Result};
6use crate::Span;
7
8use alloc::boxed::Box;
9
10/// Tracks the status of every enable-extension known to Naga.
11#[derive(Clone, Copy, Debug, Eq, PartialEq)]
12pub(crate) struct EnableExtensions {
13    wgpu_mesh_shader: bool,
14    wgpu_ray_query: bool,
15    wgpu_ray_query_vertex_return: bool,
16    wgpu_ray_tracing_pipeline: bool,
17    wgpu_ray_tracing_invocation_reorder: bool,
18    dual_source_blending: bool,
19    /// Whether `enable f16;` was written earlier in the shader module.
20    f16: bool,
21    /// Whether `enable wgpu_int16;` was written earlier in the shader module.
22    wgpu_int16: bool,
23    clip_distances: bool,
24    wgpu_cooperative_matrix: bool,
25    draw_index: bool,
26    primitive_index: bool,
27    per_vertex: bool,
28    wgpu_binding_array: bool,
29    debug_printf: bool,
30}
31
32impl EnableExtensions {
33    pub(crate) const fn empty() -> Self {
34        Self {
35            wgpu_mesh_shader: false,
36            wgpu_ray_query: false,
37            wgpu_ray_query_vertex_return: false,
38            wgpu_ray_tracing_pipeline: false,
39            wgpu_ray_tracing_invocation_reorder: false,
40            f16: false,
41            wgpu_int16: false,
42            dual_source_blending: false,
43            clip_distances: false,
44            wgpu_cooperative_matrix: false,
45            draw_index: false,
46            primitive_index: false,
47            per_vertex: false,
48            wgpu_binding_array: false,
49            debug_printf: false,
50        }
51    }
52
53    /// Add an enable-extension to the set requested by a module.
54    pub(crate) const fn add(&mut self, ext: ImplementedEnableExtension) {
55        let field = match ext {
56            ImplementedEnableExtension::WgpuMeshShader => &mut self.wgpu_mesh_shader,
57            ImplementedEnableExtension::WgpuRayQuery => &mut self.wgpu_ray_query,
58            ImplementedEnableExtension::WgpuRayQueryVertexReturn => {
59                &mut self.wgpu_ray_query_vertex_return
60            }
61            ImplementedEnableExtension::WgpuRayTracingPipeline => {
62                &mut self.wgpu_ray_tracing_pipeline
63            }
64            ImplementedEnableExtension::WgpuRayTracingInvocationReorder => {
65                &mut self.wgpu_ray_tracing_invocation_reorder
66            }
67            ImplementedEnableExtension::DualSourceBlending => &mut self.dual_source_blending,
68            ImplementedEnableExtension::F16 => &mut self.f16,
69            ImplementedEnableExtension::WgpuInt16 => &mut self.wgpu_int16,
70            ImplementedEnableExtension::ClipDistances => &mut self.clip_distances,
71            ImplementedEnableExtension::WgpuCooperativeMatrix => &mut self.wgpu_cooperative_matrix,
72            ImplementedEnableExtension::DrawIndex => &mut self.draw_index,
73            ImplementedEnableExtension::PrimitiveIndex => &mut self.primitive_index,
74            ImplementedEnableExtension::WgpuPerVertex => &mut self.per_vertex,
75            ImplementedEnableExtension::WgpuBindingArray => &mut self.wgpu_binding_array,
76            ImplementedEnableExtension::WgpuDebugPrintf => &mut self.debug_printf,
77        };
78        *field = true;
79    }
80
81    /// Query whether an enable-extension tracked here has been requested.
82    pub(crate) const fn contains(&self, ext: ImplementedEnableExtension) -> bool {
83        match ext {
84            ImplementedEnableExtension::WgpuMeshShader => self.wgpu_mesh_shader,
85            ImplementedEnableExtension::WgpuRayQuery => self.wgpu_ray_query,
86            ImplementedEnableExtension::WgpuRayQueryVertexReturn => {
87                self.wgpu_ray_query_vertex_return
88            }
89            ImplementedEnableExtension::WgpuRayTracingPipeline => self.wgpu_ray_tracing_pipeline,
90            ImplementedEnableExtension::WgpuRayTracingInvocationReorder => {
91                self.wgpu_ray_tracing_invocation_reorder
92            }
93            ImplementedEnableExtension::DualSourceBlending => self.dual_source_blending,
94            ImplementedEnableExtension::F16 => self.f16,
95            ImplementedEnableExtension::WgpuInt16 => self.wgpu_int16,
96            ImplementedEnableExtension::ClipDistances => self.clip_distances,
97            ImplementedEnableExtension::WgpuCooperativeMatrix => self.wgpu_cooperative_matrix,
98            ImplementedEnableExtension::DrawIndex => self.draw_index,
99            ImplementedEnableExtension::PrimitiveIndex => self.primitive_index,
100            ImplementedEnableExtension::WgpuPerVertex => self.per_vertex,
101            ImplementedEnableExtension::WgpuBindingArray => self.wgpu_binding_array,
102            ImplementedEnableExtension::WgpuDebugPrintf => self.debug_printf,
103        }
104    }
105
106    pub(crate) fn require(
107        &self,
108        ext: ImplementedEnableExtension,
109        span: Span,
110    ) -> Result<'static, ()> {
111        if !self.contains(ext) {
112            Err(Box::new(Error::EnableExtensionNotEnabled {
113                span,
114                kind: ext.into(),
115            }))
116        } else {
117            Ok(())
118        }
119    }
120}
121
122impl Default for EnableExtensions {
123    fn default() -> Self {
124        Self::empty()
125    }
126}
127
128/// An enable-extension not guaranteed to be present in all environments.
129///
130/// WGSL spec.: <https://www.w3.org/TR/WGSL/#enable-extensions-sec>
131#[derive(Clone, Copy, Debug, Hash, Eq, PartialEq)]
132pub enum EnableExtension {
133    Implemented(ImplementedEnableExtension),
134    Unimplemented(UnimplementedEnableExtension),
135}
136
137impl From<ImplementedEnableExtension> for EnableExtension {
138    fn from(value: ImplementedEnableExtension) -> Self {
139        Self::Implemented(value)
140    }
141}
142
143impl EnableExtension {
144    const F16: &'static str = "f16";
145    const CLIP_DISTANCES: &'static str = "clip_distances";
146    const DUAL_SOURCE_BLENDING: &'static str = "dual_source_blending";
147    const MESH_SHADER: &'static str = "wgpu_mesh_shader";
148    const RAY_QUERY: &'static str = "wgpu_ray_query";
149    const RAY_QUERY_VERTEX_RETURN: &'static str = "wgpu_ray_query_vertex_return";
150    const RAY_TRACING_PIPELINE: &'static str = "wgpu_ray_tracing_pipeline";
151    const RAY_TRACING_INVOCATION_REORDER: &'static str = "wgpu_ray_tracing_invocation_reorder";
152    const COOPERATIVE_MATRIX: &'static str = "wgpu_cooperative_matrix";
153    const SUBGROUPS: &'static str = "subgroups";
154    const PRIMITIVE_INDEX: &'static str = "primitive_index";
155    const DRAW_INDEX: &'static str = "draw_index";
156    const PER_VERTEX: &'static str = "wgpu_per_vertex";
157    const BINDING_ARRAY: &'static str = "wgpu_binding_array";
158    const INT16: &'static str = "wgpu_int16";
159    const DEBUG_PRINTF: &'static str = "wgpu_debug_printf";
160
161    /// Convert from a sentinel word in WGSL into its associated [`EnableExtension`], if possible.
162    pub(crate) fn from_ident(word: &str, span: Span) -> Result<'_, Self> {
163        Ok(match word {
164            Self::F16 => Self::Implemented(ImplementedEnableExtension::F16),
165            Self::CLIP_DISTANCES => Self::Implemented(ImplementedEnableExtension::ClipDistances),
166            Self::DUAL_SOURCE_BLENDING => {
167                Self::Implemented(ImplementedEnableExtension::DualSourceBlending)
168            }
169            Self::MESH_SHADER => Self::Implemented(ImplementedEnableExtension::WgpuMeshShader),
170            Self::RAY_QUERY => Self::Implemented(ImplementedEnableExtension::WgpuRayQuery),
171            Self::RAY_QUERY_VERTEX_RETURN => {
172                Self::Implemented(ImplementedEnableExtension::WgpuRayQueryVertexReturn)
173            }
174            Self::RAY_TRACING_PIPELINE => {
175                Self::Implemented(ImplementedEnableExtension::WgpuRayTracingPipeline)
176            }
177            Self::RAY_TRACING_INVOCATION_REORDER => {
178                Self::Implemented(ImplementedEnableExtension::WgpuRayTracingInvocationReorder)
179            }
180            Self::COOPERATIVE_MATRIX => {
181                Self::Implemented(ImplementedEnableExtension::WgpuCooperativeMatrix)
182            }
183            Self::SUBGROUPS => Self::Unimplemented(UnimplementedEnableExtension::Subgroups),
184            Self::DRAW_INDEX => Self::Implemented(ImplementedEnableExtension::DrawIndex),
185            Self::PRIMITIVE_INDEX => Self::Implemented(ImplementedEnableExtension::PrimitiveIndex),
186            Self::PER_VERTEX => Self::Implemented(ImplementedEnableExtension::WgpuPerVertex),
187            Self::BINDING_ARRAY => Self::Implemented(ImplementedEnableExtension::WgpuBindingArray),
188            Self::INT16 => Self::Implemented(ImplementedEnableExtension::WgpuInt16),
189            Self::DEBUG_PRINTF => Self::Implemented(ImplementedEnableExtension::WgpuDebugPrintf),
190            _ => return Err(Box::new(Error::UnknownEnableExtension(span, word))),
191        })
192    }
193
194    /// Maps this [`EnableExtension`] into the sentinel word associated with it in WGSL.
195    pub const fn to_ident(self) -> &'static str {
196        match self {
197            Self::Implemented(kind) => match kind {
198                ImplementedEnableExtension::WgpuMeshShader => Self::MESH_SHADER,
199                ImplementedEnableExtension::WgpuRayQuery => Self::RAY_QUERY,
200                ImplementedEnableExtension::WgpuRayQueryVertexReturn => {
201                    Self::RAY_QUERY_VERTEX_RETURN
202                }
203                ImplementedEnableExtension::WgpuCooperativeMatrix => Self::COOPERATIVE_MATRIX,
204                ImplementedEnableExtension::DualSourceBlending => Self::DUAL_SOURCE_BLENDING,
205                ImplementedEnableExtension::F16 => Self::F16,
206                ImplementedEnableExtension::ClipDistances => Self::CLIP_DISTANCES,
207                ImplementedEnableExtension::DrawIndex => Self::DRAW_INDEX,
208                ImplementedEnableExtension::PrimitiveIndex => Self::PRIMITIVE_INDEX,
209                ImplementedEnableExtension::WgpuRayTracingPipeline => Self::RAY_TRACING_PIPELINE,
210                ImplementedEnableExtension::WgpuRayTracingInvocationReorder => {
211                    Self::RAY_TRACING_INVOCATION_REORDER
212                }
213                ImplementedEnableExtension::WgpuPerVertex => Self::PER_VERTEX,
214                ImplementedEnableExtension::WgpuBindingArray => Self::BINDING_ARRAY,
215                ImplementedEnableExtension::WgpuInt16 => Self::INT16,
216                ImplementedEnableExtension::WgpuDebugPrintf => Self::DEBUG_PRINTF,
217            },
218            Self::Unimplemented(kind) => match kind {
219                UnimplementedEnableExtension::Subgroups => Self::SUBGROUPS,
220            },
221        }
222    }
223}
224
225/// A variant of [`EnableExtension::Implemented`].
226#[derive(Clone, Copy, Debug, Hash, Eq, PartialEq)]
227#[cfg_attr(test, derive(strum::VariantArray))]
228pub enum ImplementedEnableExtension {
229    /// Enables `f16`/`half` primitive support in all shader languages.
230    ///
231    /// In the WGSL standard, this corresponds to [`enable f16;`].
232    ///
233    /// [`enable f16;`]: https://www.w3.org/TR/WGSL/#extension-f16
234    F16,
235    /// Enables the `blend_src` attribute in WGSL.
236    ///
237    /// In the WGSL standard, this corresponds to [`enable dual_source_blending;`].
238    ///
239    /// [`enable dual_source_blending;`]: https://www.w3.org/TR/WGSL/#extension-dual_source_blending
240    DualSourceBlending,
241    /// Enables the `clip_distances` variable in WGSL.
242    ///
243    /// In the WGSL standard, this corresponds to [`enable clip_distances;`].
244    ///
245    /// [`enable clip_distances;`]: https://www.w3.org/TR/WGSL/#extension-clip_distances
246    ClipDistances,
247    /// Enables the `wgpu_mesh_shader` extension, native only
248    WgpuMeshShader,
249    /// Enables the `wgpu_ray_query` extension, native only.
250    WgpuRayQuery,
251    /// Enables the `wgpu_ray_query_vertex_return` extension, native only.
252    WgpuRayQueryVertexReturn,
253    /// Enables the `wgpu_ray_tracing_pipeline` extension, native only.
254    WgpuRayTracingPipeline,
255    /// Enables the `wgpu_ray_tracing_invocation_reorder` extension, native only.
256    ///
257    /// This provides the `hit_object` type, the `hitObject*` built-in functions,
258    /// and `reorderThread`.
259    WgpuRayTracingInvocationReorder,
260    /// Enables the `wgpu_cooperative_matrix` extension, native only.
261    WgpuCooperativeMatrix,
262    /// Enables the `draw_index` builtin. Not currently part of the WGSL spec but probably will be at some point.
263    DrawIndex,
264    /// Enables the `@builtin(primitive_index)` attribute in WGSL.
265    ///
266    /// In the WGSL standard, this corresponds to [`enable primitive-index;`].
267    ///
268    /// [`enable primitive-index;`]: https://www.w3.org/TR/WGSL/#extension-primitive_index
269    PrimitiveIndex,
270    /// Enables the `wgpu_per_vertex` extension, allows using `@interpolate(per_vertex)` attribute in WGSL, native only.
271    WgpuPerVertex,
272    /// Enables the `wgpu_binding_array` extension, native only.
273    WgpuBindingArray,
274    /// Enables `i16`/`u16` 16-bit integer support in WGSL, native only.
275    WgpuInt16,
276    /// Enables the `wgpu_debug_printf` extension, allows using `debugPrintf`, native only.
277    WgpuDebugPrintf,
278}
279
280impl ImplementedEnableExtension {
281    /// A slice of all variants of [`ImplementedEnableExtension`].
282    pub const VARIANTS: &'static [Self] = &[
283        Self::F16,
284        Self::DualSourceBlending,
285        Self::ClipDistances,
286        Self::WgpuMeshShader,
287        Self::WgpuRayQuery,
288        Self::WgpuRayQueryVertexReturn,
289        Self::WgpuRayTracingPipeline,
290        Self::WgpuRayTracingInvocationReorder,
291        Self::WgpuCooperativeMatrix,
292        Self::DrawIndex,
293        Self::PrimitiveIndex,
294        Self::WgpuPerVertex,
295        Self::WgpuBindingArray,
296        Self::WgpuInt16,
297        Self::WgpuDebugPrintf,
298    ];
299
300    /// Returns slice of all variants of [`ImplementedEnableExtension`].
301    pub const fn all() -> &'static [Self] {
302        Self::VARIANTS
303    }
304
305    /// Returns the capability required for this enable extension.
306    pub const fn capability(self) -> crate::valid::Capabilities {
307        use crate::valid::Capabilities as C;
308        match self {
309            Self::F16 => C::SHADER_FLOAT16,
310            Self::DualSourceBlending => C::DUAL_SOURCE_BLENDING,
311            Self::ClipDistances => C::CLIP_DISTANCES,
312            Self::WgpuMeshShader => C::MESH_SHADER,
313            Self::WgpuRayQuery => C::RAY_QUERY,
314            Self::WgpuRayQueryVertexReturn => C::RAY_HIT_VERTEX_POSITION,
315            Self::WgpuCooperativeMatrix => C::COOPERATIVE_MATRIX,
316            Self::WgpuRayTracingPipeline => C::RAY_TRACING_PIPELINE,
317            Self::WgpuRayTracingInvocationReorder => C::RAY_TRACING_INVOCATION_REORDER,
318            Self::DrawIndex => C::DRAW_INDEX,
319            Self::PrimitiveIndex => C::PRIMITIVE_INDEX,
320            Self::WgpuPerVertex => C::PER_VERTEX,
321            Self::WgpuBindingArray => C::BUFFER_BINDING_ARRAY
322                .union(C::BUFFER_BINDING_ARRAY_NON_UNIFORM_INDEXING)
323                .union(C::STORAGE_BUFFER_BINDING_ARRAY)
324                .union(C::STORAGE_BUFFER_BINDING_ARRAY_NON_UNIFORM_INDEXING)
325                .union(C::STORAGE_TEXTURE_BINDING_ARRAY)
326                .union(C::STORAGE_TEXTURE_BINDING_ARRAY_NON_UNIFORM_INDEXING)
327                .union(C::TEXTURE_AND_SAMPLER_BINDING_ARRAY)
328                .union(C::TEXTURE_AND_SAMPLER_BINDING_ARRAY_NON_UNIFORM_INDEXING)
329                .union(C::ACCELERATION_STRUCTURE_BINDING_ARRAY),
330            Self::WgpuInt16 => C::SHADER_INT16,
331            Self::WgpuDebugPrintf => C::DEBUG_PRINTF,
332        }
333    }
334}
335
336#[test]
337/// Asserts that the manual implementation of VARIANTS is the same as the derived strum version would be
338/// while still allowing strum to be a dev-only dependency
339fn test_manual_variants_array_is_correct() {
340    assert_eq!(
341        <ImplementedEnableExtension as strum::VariantArray>::VARIANTS,
342        ImplementedEnableExtension::VARIANTS
343    );
344}
345
346/// A variant of [`EnableExtension::Unimplemented`].
347#[derive(Clone, Copy, Debug, Hash, Eq, PartialEq)]
348pub enum UnimplementedEnableExtension {
349    /// Enables subgroup built-ins in all languages.
350    ///
351    /// In the WGSL standard, this corresponds to [`enable subgroups;`].
352    ///
353    /// [`enable subgroups;`]: https://www.w3.org/TR/WGSL/#extension-subgroups
354    Subgroups,
355}
356
357impl UnimplementedEnableExtension {
358    pub(crate) const fn tracking_issue_num(self) -> u16 {
359        match self {
360            Self::Subgroups => 5555,
361        }
362    }
363}