Skip to content

Commit 5498b85

Browse files
committed
[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.
1 parent 7e2a477 commit 5498b85

File tree

2 files changed

+49
-28
lines changed

2 files changed

+49
-28
lines changed

naga/src/back/wgsl/writer.rs

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ use super::ToWgslIfImplemented as _;
1111
use crate::back::wgsl::polyfill::InversePolyfill;
1212
use crate::{
1313
back::{self, Baked},
14-
common::{self, wgsl::ToWgsl as _},
14+
common::{
15+
self,
16+
wgsl::{address_space_str, ToWgsl as _},
17+
},
1518
proc::{self, ExpressionKindTracker, NameKey},
1619
valid, Handle, Module, ShaderStage, TypeInner,
1720
};
@@ -1904,33 +1907,6 @@ impl<W: Write> Writer<W> {
19041907
}
19051908
}
19061909

1907-
const fn address_space_str(
1908-
space: crate::AddressSpace,
1909-
) -> (Option<&'static str>, Option<&'static str>) {
1910-
use crate::AddressSpace as As;
1911-
1912-
(
1913-
Some(match space {
1914-
As::Private => "private",
1915-
As::Uniform => "uniform",
1916-
As::Storage { access } => {
1917-
if access.contains(crate::StorageAccess::ATOMIC) {
1918-
return (Some("storage"), Some("atomic"));
1919-
} else if access.contains(crate::StorageAccess::STORE) {
1920-
return (Some("storage"), Some("read_write"));
1921-
} else {
1922-
"storage"
1923-
}
1924-
}
1925-
As::PushConstant => "push_constant",
1926-
As::WorkGroup => "workgroup",
1927-
As::Handle => return (None, None),
1928-
As::Function => "function",
1929-
}),
1930-
None,
1931-
)
1932-
}
1933-
19341910
fn map_binding_to_attribute(binding: &crate::Binding) -> Vec<Attribute> {
19351911
match *binding {
19361912
crate::Binding::BuiltIn(built_in) => {

naga/src/common/wgsl.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,3 +321,48 @@ impl ToWgsl for crate::ImageDimension {
321321
})
322322
}
323323
}
324+
325+
/// Return the WGSL address space and access mode strings for `space`.
326+
///
327+
/// Why don't we implement [`ToWgsl`] for [`AddressSpace`]?
328+
///
329+
/// In WGSL, the full form of a pointer type is `ptr<AS, T, AM>`, where:
330+
/// - `AS` is the address space,
331+
/// - `T` is the store type, and
332+
/// - `AM` is the access mode.
333+
///
334+
/// Since the type `T` intervenes between the address space and the
335+
/// access mode, there isn't really any individual WGSL grammar
336+
/// production that corresponds to an [`AddressSpace`], so [`ToWgsl`]
337+
/// is too simple-minded for this case.
338+
///
339+
/// Furthermore, we want to write `var<AS[, AM]>` for most address
340+
/// spaces, but we want to just write `var foo: T` for handle types.
341+
///
342+
/// [`AddressSpace`]: crate:AddressSpace
343+
pub const fn address_space_str(
344+
space: crate::AddressSpace,
345+
) -> (Option<&'static str>, Option<&'static str>) {
346+
use crate::AddressSpace as As;
347+
348+
(
349+
Some(match space {
350+
As::Private => "private",
351+
As::Uniform => "uniform",
352+
As::Storage { access } => {
353+
if access.contains(crate::StorageAccess::ATOMIC) {
354+
return (Some("storage"), Some("atomic"));
355+
} else if access.contains(crate::StorageAccess::STORE) {
356+
return (Some("storage"), Some("read_write"));
357+
} else {
358+
"storage"
359+
}
360+
}
361+
As::PushConstant => "push_constant",
362+
As::WorkGroup => "workgroup",
363+
As::Handle => return (None, None),
364+
As::Function => "function",
365+
}),
366+
None,
367+
)
368+
}

0 commit comments

Comments
 (0)