[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.
This commit is contained in:
Jim Blandy
2025-03-04 18:12:44 -08:00
parent 567a2c027f
commit fc83073905
2 changed files with 46 additions and 28 deletions

View File

@@ -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<W: Write> Writer<W> {
}
}
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<Attribute> {
match *binding {
crate::Binding::BuiltIn(built_in) => {

View File

@@ -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<AS, T, AM>`, 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<AS[, AM]>` 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,
)
}