From a072eea89da9f9380e85387c65a952e78fe2f973 Mon Sep 17 00:00:00 2001 From: sagudev <16504129+sagudev@users.noreply.github.com> Date: Thu, 17 Sep 2026 10:44:50 +0200 Subject: [PATCH] Move language_extension into naga-types Signed-off-by: sagudev <16504129+sagudev@users.noreply.github.com> --- Cargo.lock | 1 + naga-types/Cargo.toml | 3 + naga-types/src/lib.rs | 1 + naga-types/src/wgsl.rs | 124 ++++++++++++++++++ naga/src/front/wgsl/parse/directive.rs | 2 +- .../parse/directive/language_extension.rs | 122 ----------------- 6 files changed, 130 insertions(+), 123 deletions(-) create mode 100644 naga-types/src/wgsl.rs delete mode 100644 naga/src/front/wgsl/parse/directive/language_extension.rs diff --git a/Cargo.lock b/Cargo.lock index 243904b05..c9bd57cfd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2495,6 +2495,7 @@ dependencies = [ "indexmap", "rustc-hash 1.1.0", "serde", + "strum 0.28.0", "thiserror 2.0.20", ] diff --git a/naga-types/Cargo.toml b/naga-types/Cargo.toml index 0c2ff890f..abde06b0f 100644 --- a/naga-types/Cargo.toml +++ b/naga-types/Cargo.toml @@ -27,3 +27,6 @@ indexmap.workspace = true rustc-hash.workspace = true serde = { workspace = true, features = ["alloc", "derive"], optional = true } thiserror.workspace = true + +[dev-dependencies] +strum.workspace = true diff --git a/naga-types/src/lib.rs b/naga-types/src/lib.rs index 25ad2b4ba..ff0311a30 100644 --- a/naga-types/src/lib.rs +++ b/naga-types/src/lib.rs @@ -8,6 +8,7 @@ pub mod glsl; pub mod hlsl; pub mod msl; pub mod spv; +pub mod wgsl; /// Create a Markdown link definition referring to the `wgpu` crate. /// diff --git a/naga-types/src/wgsl.rs b/naga-types/src/wgsl.rs new file mode 100644 index 000000000..1f0450788 --- /dev/null +++ b/naga-types/src/wgsl.rs @@ -0,0 +1,124 @@ +/// `requires …;` extensions in WGSL. +/// +/// The focal point of this module is the [`LanguageExtension`](language_extension::LanguageExtension) API. +pub mod language_extension { + + /// A language extension recognized by Naga, but not guaranteed to be present in all environments. + /// + /// WGSL spec.: + #[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)] + pub enum LanguageExtension { + Implemented(ImplementedLanguageExtension), + Unimplemented(UnimplementedLanguageExtension), + } + + impl LanguageExtension { + const READONLY_AND_READWRITE_STORAGE_TEXTURES: &'static str = + "readonly_and_readwrite_storage_textures"; + const PACKED4X8_INTEGER_DOT_PRODUCT: &'static str = "packed_4x8_integer_dot_product"; + const UNRESTRICTED_POINTER_PARAMETERS: &'static str = "unrestricted_pointer_parameters"; + const POINTER_COMPOSITE_ACCESS: &'static str = "pointer_composite_access"; + const IMMEDIATE_ADDRESS_SPACE: &'static str = "immediate_address_space"; + + /// Convert from a sentinel word in WGSL into its associated [`LanguageExtension`], if possible. + pub fn from_ident(s: &str) -> Option { + Some(match s { + Self::READONLY_AND_READWRITE_STORAGE_TEXTURES => Self::Implemented( + ImplementedLanguageExtension::ReadOnlyAndReadWriteStorageTextures, + ), + Self::PACKED4X8_INTEGER_DOT_PRODUCT => { + Self::Implemented(ImplementedLanguageExtension::Packed4x8IntegerDotProduct) + } + Self::UNRESTRICTED_POINTER_PARAMETERS => Self::Unimplemented( + UnimplementedLanguageExtension::UnrestrictedPointerParameters, + ), + Self::POINTER_COMPOSITE_ACCESS => { + Self::Implemented(ImplementedLanguageExtension::PointerCompositeAccess) + } + Self::IMMEDIATE_ADDRESS_SPACE => { + Self::Implemented(ImplementedLanguageExtension::ImmediateAddressSpace) + } + _ => return None, + }) + } + + /// Maps this [`LanguageExtension`] into the sentinel word associated with it in WGSL. + pub const fn to_ident(self) -> &'static str { + match self { + Self::Implemented(kind) => kind.to_ident(), + Self::Unimplemented(kind) => match kind { + UnimplementedLanguageExtension::UnrestrictedPointerParameters => { + Self::UNRESTRICTED_POINTER_PARAMETERS + } + }, + } + } + } + + /// A variant of [`LanguageExtension::Implemented`]. + #[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)] + #[cfg_attr(test, derive(strum::VariantArray))] + pub enum ImplementedLanguageExtension { + ReadOnlyAndReadWriteStorageTextures, + Packed4x8IntegerDotProduct, + PointerCompositeAccess, + ImmediateAddressSpace, + } + + impl ImplementedLanguageExtension { + /// A slice of all variants of [`ImplementedLanguageExtension`]. + pub const VARIANTS: &'static [Self] = &[ + Self::ReadOnlyAndReadWriteStorageTextures, + Self::Packed4x8IntegerDotProduct, + Self::PointerCompositeAccess, + Self::ImmediateAddressSpace, + ]; + + /// Returns slice of all variants of [`ImplementedLanguageExtension`]. + pub const fn all() -> &'static [Self] { + Self::VARIANTS + } + + /// Maps this [`ImplementedLanguageExtension`] into the sentinel word associated with it in WGSL. + pub const fn to_ident(self) -> &'static str { + match self { + ImplementedLanguageExtension::ReadOnlyAndReadWriteStorageTextures => { + LanguageExtension::READONLY_AND_READWRITE_STORAGE_TEXTURES + } + ImplementedLanguageExtension::Packed4x8IntegerDotProduct => { + LanguageExtension::PACKED4X8_INTEGER_DOT_PRODUCT + } + ImplementedLanguageExtension::PointerCompositeAccess => { + LanguageExtension::POINTER_COMPOSITE_ACCESS + } + ImplementedLanguageExtension::ImmediateAddressSpace => { + LanguageExtension::IMMEDIATE_ADDRESS_SPACE + } + } + } + } + + #[test] + /// Asserts that the manual implementation of VARIANTS is the same as the derived strum version would be + /// while still allowing strum to be a dev-only dependency + fn test_manual_variants_array_is_correct() { + assert_eq!( + ::VARIANTS, + ImplementedLanguageExtension::VARIANTS + ); + } + + /// A variant of [`LanguageExtension::Unimplemented`]. + #[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)] + pub enum UnimplementedLanguageExtension { + UnrestrictedPointerParameters, + } + + impl UnimplementedLanguageExtension { + pub const fn tracking_issue_num(self) -> u16 { + match self { + Self::UnrestrictedPointerParameters => 5158, + } + } + } +} diff --git a/naga/src/front/wgsl/parse/directive.rs b/naga/src/front/wgsl/parse/directive.rs index 1fed03936..70c96df44 100644 --- a/naga/src/front/wgsl/parse/directive.rs +++ b/naga/src/front/wgsl/parse/directive.rs @@ -3,7 +3,7 @@ //! See also . pub mod enable_extension; -pub(crate) mod language_extension; +pub(crate) use naga_types::wgsl::language_extension; use alloc::boxed::Box; diff --git a/naga/src/front/wgsl/parse/directive/language_extension.rs b/naga/src/front/wgsl/parse/directive/language_extension.rs deleted file mode 100644 index 357c347c9..000000000 --- a/naga/src/front/wgsl/parse/directive/language_extension.rs +++ /dev/null @@ -1,122 +0,0 @@ -//! `requires …;` extensions in WGSL. -//! -//! The focal point of this module is the [`LanguageExtension`] API. - -/// A language extension recognized by Naga, but not guaranteed to be present in all environments. -/// -/// WGSL spec.: -#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)] -pub enum LanguageExtension { - Implemented(ImplementedLanguageExtension), - Unimplemented(UnimplementedLanguageExtension), -} - -impl LanguageExtension { - const READONLY_AND_READWRITE_STORAGE_TEXTURES: &'static str = - "readonly_and_readwrite_storage_textures"; - const PACKED4X8_INTEGER_DOT_PRODUCT: &'static str = "packed_4x8_integer_dot_product"; - const UNRESTRICTED_POINTER_PARAMETERS: &'static str = "unrestricted_pointer_parameters"; - const POINTER_COMPOSITE_ACCESS: &'static str = "pointer_composite_access"; - const IMMEDIATE_ADDRESS_SPACE: &'static str = "immediate_address_space"; - - /// Convert from a sentinel word in WGSL into its associated [`LanguageExtension`], if possible. - pub fn from_ident(s: &str) -> Option { - Some(match s { - Self::READONLY_AND_READWRITE_STORAGE_TEXTURES => { - Self::Implemented(ImplementedLanguageExtension::ReadOnlyAndReadWriteStorageTextures) - } - Self::PACKED4X8_INTEGER_DOT_PRODUCT => { - Self::Implemented(ImplementedLanguageExtension::Packed4x8IntegerDotProduct) - } - Self::UNRESTRICTED_POINTER_PARAMETERS => { - Self::Unimplemented(UnimplementedLanguageExtension::UnrestrictedPointerParameters) - } - Self::POINTER_COMPOSITE_ACCESS => { - Self::Implemented(ImplementedLanguageExtension::PointerCompositeAccess) - } - Self::IMMEDIATE_ADDRESS_SPACE => { - Self::Implemented(ImplementedLanguageExtension::ImmediateAddressSpace) - } - _ => return None, - }) - } - - /// Maps this [`LanguageExtension`] into the sentinel word associated with it in WGSL. - pub const fn to_ident(self) -> &'static str { - match self { - Self::Implemented(kind) => kind.to_ident(), - Self::Unimplemented(kind) => match kind { - UnimplementedLanguageExtension::UnrestrictedPointerParameters => { - Self::UNRESTRICTED_POINTER_PARAMETERS - } - }, - } - } -} - -/// A variant of [`LanguageExtension::Implemented`]. -#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)] -#[cfg_attr(test, derive(strum::VariantArray))] -pub enum ImplementedLanguageExtension { - ReadOnlyAndReadWriteStorageTextures, - Packed4x8IntegerDotProduct, - PointerCompositeAccess, - ImmediateAddressSpace, -} - -impl ImplementedLanguageExtension { - /// A slice of all variants of [`ImplementedLanguageExtension`]. - pub const VARIANTS: &'static [Self] = &[ - Self::ReadOnlyAndReadWriteStorageTextures, - Self::Packed4x8IntegerDotProduct, - Self::PointerCompositeAccess, - Self::ImmediateAddressSpace, - ]; - - /// Returns slice of all variants of [`ImplementedLanguageExtension`]. - pub const fn all() -> &'static [Self] { - Self::VARIANTS - } - - /// Maps this [`ImplementedLanguageExtension`] into the sentinel word associated with it in WGSL. - pub const fn to_ident(self) -> &'static str { - match self { - ImplementedLanguageExtension::ReadOnlyAndReadWriteStorageTextures => { - LanguageExtension::READONLY_AND_READWRITE_STORAGE_TEXTURES - } - ImplementedLanguageExtension::Packed4x8IntegerDotProduct => { - LanguageExtension::PACKED4X8_INTEGER_DOT_PRODUCT - } - ImplementedLanguageExtension::PointerCompositeAccess => { - LanguageExtension::POINTER_COMPOSITE_ACCESS - } - ImplementedLanguageExtension::ImmediateAddressSpace => { - LanguageExtension::IMMEDIATE_ADDRESS_SPACE - } - } - } -} - -#[test] -/// Asserts that the manual implementation of VARIANTS is the same as the derived strum version would be -/// while still allowing strum to be a dev-only dependency -fn test_manual_variants_array_is_correct() { - assert_eq!( - ::VARIANTS, - ImplementedLanguageExtension::VARIANTS - ); -} - -/// A variant of [`LanguageExtension::Unimplemented`]. -#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)] -pub enum UnimplementedLanguageExtension { - UnrestrictedPointerParameters, -} - -impl UnimplementedLanguageExtension { - pub(crate) const fn tracking_issue_num(self) -> u16 { - match self { - Self::UnrestrictedPointerParameters => 5158, - } - } -}