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