Skip to content

Commit 4ecd313

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 8ff93cd commit 4ecd313

File tree

2 files changed

+46
-28
lines changed

2 files changed

+46
-28
lines changed

naga/src/back/wgsl/writer.rs

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use crate::{
1313
back::{self, Baked},
1414
common::{
1515
self,
16-
wgsl::{ToWgsl, TryToWgsl},
16+
wgsl::{address_space_str, ToWgsl, TryToWgsl},
1717
},
1818
proc::{self, ExpressionKindTracker, NameKey},
1919
valid, Handle, Module, ShaderStage, TypeInner,
@@ -1877,33 +1877,6 @@ impl<W: Write> Writer<W> {
18771877
}
18781878
}
18791879

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

naga/src/common/wgsl.rs

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

0 commit comments

Comments
 (0)