From fc830739053f950d956e0fb7633b8b4a603aa06c Mon Sep 17 00:00:00 2001 From: Jim Blandy Date: Tue, 4 Mar 2025 18:12:44 -0800 Subject: [PATCH] [naga] Move `back::wgsl::address_space_str` to `common::wgsl`. Move `back::wgsl::address_space_str` to `common::wgsl`, so that the front end can use it too. This commit is code motion and `use` adjustment only; there should be no change in behavior. --- naga/src/back/wgsl/writer.rs | 29 +---------------------- naga/src/common/wgsl.rs | 45 ++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 28 deletions(-) diff --git a/naga/src/back/wgsl/writer.rs b/naga/src/back/wgsl/writer.rs index ceff488968..7e049b0ba4 100644 --- a/naga/src/back/wgsl/writer.rs +++ b/naga/src/back/wgsl/writer.rs @@ -13,7 +13,7 @@ use crate::{ back::{self, Baked}, common::{ self, - wgsl::{ToWgsl, TryToWgsl}, + wgsl::{address_space_str, ToWgsl, TryToWgsl}, }, proc::{self, ExpressionKindTracker, NameKey}, valid, Handle, Module, ShaderStage, TypeInner, @@ -1877,33 +1877,6 @@ impl Writer { } } -const fn address_space_str( - space: crate::AddressSpace, -) -> (Option<&'static str>, Option<&'static str>) { - use crate::AddressSpace as As; - - ( - Some(match space { - As::Private => "private", - As::Uniform => "uniform", - As::Storage { access } => { - if access.contains(crate::StorageAccess::ATOMIC) { - return (Some("storage"), Some("atomic")); - } else if access.contains(crate::StorageAccess::STORE) { - return (Some("storage"), Some("read_write")); - } else { - "storage" - } - } - As::PushConstant => "push_constant", - As::WorkGroup => "workgroup", - As::Handle => return (None, None), - As::Function => "function", - }), - None, - ) -} - fn map_binding_to_attribute(binding: &crate::Binding) -> Vec { match *binding { crate::Binding::BuiltIn(built_in) => { diff --git a/naga/src/common/wgsl.rs b/naga/src/common/wgsl.rs index ab470d5f95..8a9c6b5b7a 100644 --- a/naga/src/common/wgsl.rs +++ b/naga/src/common/wgsl.rs @@ -337,3 +337,48 @@ impl ToWgsl for crate::ImageDimension { } } } + +/// Return the WGSL address space and access mode strings for `space`. +/// +/// Why don't we implement [`ToWgsl`] for [`AddressSpace`]? +/// +/// In WGSL, the full form of a pointer type is `ptr`, where: +/// - `AS` is the address space, +/// - `T` is the store type, and +/// - `AM` is the access mode. +/// +/// Since the type `T` intervenes between the address space and the +/// access mode, there isn't really any individual WGSL grammar +/// production that corresponds to an [`AddressSpace`], so [`ToWgsl`] +/// is too simple-minded for this case. +/// +/// Furthermore, we want to write `var` for most address +/// spaces, but we want to just write `var foo: T` for handle types. +/// +/// [`AddressSpace`]: crate::AddressSpace +pub const fn address_space_str( + space: crate::AddressSpace, +) -> (Option<&'static str>, Option<&'static str>) { + use crate::AddressSpace as As; + + ( + Some(match space { + As::Private => "private", + As::Uniform => "uniform", + As::Storage { access } => { + if access.contains(crate::StorageAccess::ATOMIC) { + return (Some("storage"), Some("atomic")); + } else if access.contains(crate::StorageAccess::STORE) { + return (Some("storage"), Some("read_write")); + } else { + "storage" + } + } + As::PushConstant => "push_constant", + As::WorkGroup => "workgroup", + As::Handle => return (None, None), + As::Function => "function", + }), + None, + ) +}