Skip to content

Commit 5e26ca1

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 d0db53d commit 5e26ca1

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
};
@@ -1876,33 +1879,6 @@ impl<W: Write> Writer<W> {
18761879
}
18771880
}
18781881

1879-
const fn address_space_str(
1880-
space: crate::AddressSpace,
1881-
) -> (Option<&'static str>, Option<&'static str>) {
1882-
use crate::AddressSpace as As;
1883-
1884-
(
1885-
Some(match space {
1886-
As::Private => "private",
1887-
As::Uniform => "uniform",
1888-
As::Storage { access } => {
1889-
if access.contains(crate::StorageAccess::ATOMIC) {
1890-
return (Some("storage"), Some("atomic"));
1891-
} else if access.contains(crate::StorageAccess::STORE) {
1892-
return (Some("storage"), Some("read_write"));
1893-
} else {
1894-
"storage"
1895-
}
1896-
}
1897-
As::PushConstant => "push_constant",
1898-
As::WorkGroup => "workgroup",
1899-
As::Handle => return (None, None),
1900-
As::Function => "function",
1901-
}),
1902-
None,
1903-
)
1904-
}
1905-
19061882
fn map_binding_to_attribute(binding: &crate::Binding) -> Vec<Attribute> {
19071883
match *binding {
19081884
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)