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, Debug, Eq, PartialEq)]
12pub struct EnableExtensions {
13    dual_source_blending: bool,
14    /// Whether `enable f16;` was written earlier in the shader module.
15    f16: bool,
16    clip_distances: bool,
17}
18
19impl EnableExtensions {
20    pub(crate) const fn empty() -> Self {
21        Self {
22            f16: false,
23            dual_source_blending: false,
24            clip_distances: false,
25        }
26    }
27
28    /// Add an enable-extension to the set requested by a module.
29    pub(crate) fn add(&mut self, ext: ImplementedEnableExtension) {
30        let field = match ext {
31            ImplementedEnableExtension::DualSourceBlending => &mut self.dual_source_blending,
32            ImplementedEnableExtension::F16 => &mut self.f16,
33            ImplementedEnableExtension::ClipDistances => &mut self.clip_distances,
34        };
35        *field = true;
36    }
37
38    /// Query whether an enable-extension tracked here has been requested.
39    pub(crate) const fn contains(&self, ext: ImplementedEnableExtension) -> bool {
40        match ext {
41            ImplementedEnableExtension::DualSourceBlending => self.dual_source_blending,
42            ImplementedEnableExtension::F16 => self.f16,
43            ImplementedEnableExtension::ClipDistances => self.clip_distances,
44        }
45    }
46}
47
48impl Default for EnableExtensions {
49    fn default() -> Self {
50        Self::empty()
51    }
52}
53
54/// An enable-extension not guaranteed to be present in all environments.
55///
56/// WGSL spec.: <https://www.w3.org/TR/WGSL/#enable-extensions-sec>
57#[derive(Clone, Copy, Debug, Hash, Eq, PartialEq)]
58pub enum EnableExtension {
59    Implemented(ImplementedEnableExtension),
60    Unimplemented(UnimplementedEnableExtension),
61}
62
63impl From<ImplementedEnableExtension> for EnableExtension {
64    fn from(value: ImplementedEnableExtension) -> Self {
65        Self::Implemented(value)
66    }
67}
68
69impl EnableExtension {
70    const F16: &'static str = "f16";
71    const CLIP_DISTANCES: &'static str = "clip_distances";
72    const DUAL_SOURCE_BLENDING: &'static str = "dual_source_blending";
73    const SUBGROUPS: &'static str = "subgroups";
74
75    /// Convert from a sentinel word in WGSL into its associated [`EnableExtension`], if possible.
76    pub(crate) fn from_ident(word: &str, span: Span) -> Result<'_, Self> {
77        Ok(match word {
78            Self::F16 => Self::Implemented(ImplementedEnableExtension::F16),
79            Self::CLIP_DISTANCES => Self::Implemented(ImplementedEnableExtension::ClipDistances),
80            Self::DUAL_SOURCE_BLENDING => {
81                Self::Implemented(ImplementedEnableExtension::DualSourceBlending)
82            }
83            Self::SUBGROUPS => Self::Unimplemented(UnimplementedEnableExtension::Subgroups),
84            _ => return Err(Box::new(Error::UnknownEnableExtension(span, word))),
85        })
86    }
87
88    /// Maps this [`EnableExtension`] into the sentinel word associated with it in WGSL.
89    pub const fn to_ident(self) -> &'static str {
90        match self {
91            Self::Implemented(kind) => match kind {
92                ImplementedEnableExtension::DualSourceBlending => Self::DUAL_SOURCE_BLENDING,
93                ImplementedEnableExtension::F16 => Self::F16,
94                ImplementedEnableExtension::ClipDistances => Self::CLIP_DISTANCES,
95            },
96            Self::Unimplemented(kind) => match kind {
97                UnimplementedEnableExtension::Subgroups => Self::SUBGROUPS,
98            },
99        }
100    }
101}
102
103/// A variant of [`EnableExtension::Implemented`].
104#[derive(Clone, Copy, Debug, Hash, Eq, PartialEq)]
105pub enum ImplementedEnableExtension {
106    /// Enables `f16`/`half` primitive support in all shader languages.
107    ///
108    /// In the WGSL standard, this corresponds to [`enable f16;`].
109    ///
110    /// [`enable f16;`]: https://www.w3.org/TR/WGSL/#extension-f16
111    F16,
112    /// Enables the `blend_src` attribute in WGSL.
113    ///
114    /// In the WGSL standard, this corresponds to [`enable dual_source_blending;`].
115    ///
116    /// [`enable dual_source_blending;`]: https://www.w3.org/TR/WGSL/#extension-dual_source_blending
117    DualSourceBlending,
118    /// Enables the `clip_distances` variable in WGSL.
119    ///
120    /// In the WGSL standard, this corresponds to [`enable clip_distances;`].
121    ///
122    /// [`enable clip_distances;`]: https://www.w3.org/TR/WGSL/#extension-clip_distances
123    ClipDistances,
124}
125
126/// A variant of [`EnableExtension::Unimplemented`].
127#[derive(Clone, Copy, Debug, Hash, Eq, PartialEq)]
128pub enum UnimplementedEnableExtension {
129    /// Enables subgroup built-ins in all languages.
130    ///
131    /// In the WGSL standard, this corresponds to [`enable subgroups;`].
132    ///
133    /// [`enable subgroups;`]: https://www.w3.org/TR/WGSL/#extension-subgroups
134    Subgroups,
135}
136
137impl UnimplementedEnableExtension {
138    pub(crate) const fn tracking_issue_num(self) -> u16 {
139        match self {
140            Self::Subgroups => 5555,
141        }
142    }
143}