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

1//! `requires …;` extensions in WGSL.
2//!
3//! The focal point of this module is the [`LanguageExtension`] API.
4
5use strum::VariantArray;
6
7/// A language extension recognized by Naga, but not guaranteed to be present in all environments.
8///
9/// WGSL spec.: <https://www.w3.org/TR/WGSL/#language-extensions-sec>
10#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
11pub enum LanguageExtension {
12    #[allow(unused)]
13    Implemented(ImplementedLanguageExtension),
14    Unimplemented(UnimplementedLanguageExtension),
15}
16
17impl LanguageExtension {
18    const READONLY_AND_READWRITE_STORAGE_TEXTURES: &'static str =
19        "readonly_and_readwrite_storage_textures";
20    const PACKED4X8_INTEGER_DOT_PRODUCT: &'static str = "packed_4x8_integer_dot_product";
21    const UNRESTRICTED_POINTER_PARAMETERS: &'static str = "unrestricted_pointer_parameters";
22    const POINTER_COMPOSITE_ACCESS: &'static str = "pointer_composite_access";
23
24    /// Convert from a sentinel word in WGSL into its associated [`LanguageExtension`], if possible.
25    pub fn from_ident(s: &str) -> Option<Self> {
26        Some(match s {
27            Self::READONLY_AND_READWRITE_STORAGE_TEXTURES => Self::Unimplemented(
28                UnimplementedLanguageExtension::ReadOnlyAndReadWriteStorageTextures,
29            ),
30            Self::PACKED4X8_INTEGER_DOT_PRODUCT => {
31                Self::Unimplemented(UnimplementedLanguageExtension::Packed4x8IntegerDotProduct)
32            }
33            Self::UNRESTRICTED_POINTER_PARAMETERS => {
34                Self::Unimplemented(UnimplementedLanguageExtension::UnrestrictedPointerParameters)
35            }
36            Self::POINTER_COMPOSITE_ACCESS => {
37                Self::Implemented(ImplementedLanguageExtension::PointerCompositeAccess)
38            }
39            _ => return None,
40        })
41    }
42
43    /// Maps this [`LanguageExtension`] into the sentinel word associated with it in WGSL.
44    pub const fn to_ident(self) -> &'static str {
45        match self {
46            Self::Implemented(kind) => kind.to_ident(),
47            Self::Unimplemented(kind) => match kind {
48                UnimplementedLanguageExtension::ReadOnlyAndReadWriteStorageTextures => {
49                    Self::READONLY_AND_READWRITE_STORAGE_TEXTURES
50                }
51                UnimplementedLanguageExtension::Packed4x8IntegerDotProduct => {
52                    Self::PACKED4X8_INTEGER_DOT_PRODUCT
53                }
54                UnimplementedLanguageExtension::UnrestrictedPointerParameters => {
55                    Self::UNRESTRICTED_POINTER_PARAMETERS
56                }
57            },
58        }
59    }
60}
61
62/// A variant of [`LanguageExtension::Implemented`].
63#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq, VariantArray)]
64pub enum ImplementedLanguageExtension {
65    PointerCompositeAccess,
66}
67
68impl ImplementedLanguageExtension {
69    /// Returns slice of all variants of [`ImplementedLanguageExtension`].
70    pub const fn all() -> &'static [Self] {
71        Self::VARIANTS
72    }
73
74    /// Maps this [`ImplementedLanguageExtension`] into the sentinel word associated with it in WGSL.
75    pub const fn to_ident(self) -> &'static str {
76        match self {
77            ImplementedLanguageExtension::PointerCompositeAccess => {
78                LanguageExtension::POINTER_COMPOSITE_ACCESS
79            }
80        }
81    }
82}
83
84/// A variant of [`LanguageExtension::Unimplemented`].
85#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
86pub enum UnimplementedLanguageExtension {
87    ReadOnlyAndReadWriteStorageTextures,
88    Packed4x8IntegerDotProduct,
89    UnrestrictedPointerParameters,
90}
91
92impl UnimplementedLanguageExtension {
93    pub(crate) const fn tracking_issue_num(self) -> u16 {
94        match self {
95            Self::ReadOnlyAndReadWriteStorageTextures => 6204,
96            Self::Packed4x8IntegerDotProduct => 6445,
97            Self::UnrestrictedPointerParameters => 5158,
98        }
99    }
100}