From 9ed77372b413cffa52104e424338d9e655e6208e Mon Sep 17 00:00:00 2001 From: atlv Date: Wed, 11 Mar 2026 16:53:52 -0700 Subject: [PATCH] Add coherent and volatile memory decorations for storage buffers. (#9168) --- CHANGELOG.md | 1 + naga/src/back/glsl/mod.rs | 2 + naga/src/back/glsl/writer.rs | 12 +++ naga/src/back/hlsl/mod.rs | 10 ++- naga/src/back/hlsl/writer.rs | 6 ++ naga/src/back/msl/mod.rs | 8 +- naga/src/back/msl/writer.rs | 17 +++- naga/src/back/spv/mod.rs | 2 + naga/src/back/spv/writer.rs | 13 ++++ naga/src/back/wgsl/writer.rs | 13 ++++ naga/src/front/glsl/variables.rs | 3 + naga/src/front/spv/mod.rs | 22 ++++++ naga/src/front/wgsl/lower/mod.rs | 1 + naga/src/front/wgsl/parse/ast.rs | 3 + naga/src/front/wgsl/parse/mod.rs | 9 +++ naga/src/ir/mod.rs | 23 ++++++ naga/src/valid/analyzer.rs | 2 + naga/src/valid/handles.rs | 1 + naga/src/valid/interface.rs | 30 +++++++ naga/src/valid/mod.rs | 4 + .../in/wgsl/memory-decorations-coherent.toml | 11 +++ .../in/wgsl/memory-decorations-coherent.wgsl | 14 ++++ naga/tests/in/wgsl/memory-decorations.toml | 8 ++ naga/tests/in/wgsl/memory-decorations.wgsl | 21 +++++ naga/tests/naga/validation.rs | 78 +++++++++++++++++++ ...ory-decorations-coherent.main.Compute.glsl | 20 +++++ .../wgsl-memory-decorations.main.Compute.glsl | 30 +++++++ .../wgsl-memory-decorations-coherent.hlsl | 10 +++ .../hlsl/wgsl-memory-decorations-coherent.ron | 12 +++ naga/tests/out/ir/spv-fetch_depth.compact.ron | 3 + naga/tests/out/ir/spv-fetch_depth.ron | 3 + naga/tests/out/ir/spv-shadow.compact.ron | 7 ++ naga/tests/out/ir/spv-shadow.ron | 7 ++ .../out/ir/spv-spec-constants.compact.ron | 8 ++ naga/tests/out/ir/spv-spec-constants.ron | 8 ++ naga/tests/out/ir/wgsl-access.compact.ron | 5 ++ naga/tests/out/ir/wgsl-access.ron | 5 ++ naga/tests/out/ir/wgsl-collatz.compact.ron | 1 + naga/tests/out/ir/wgsl-collatz.ron | 1 + ...ides-atomicCompareExchangeWeak.compact.ron | 1 + ...sl-overrides-atomicCompareExchangeWeak.ron | 1 + .../ir/wgsl-overrides-ray-query.compact.ron | 1 + .../tests/out/ir/wgsl-overrides-ray-query.ron | 1 + naga/tests/out/ir/wgsl-overrides.compact.ron | 2 + naga/tests/out/ir/wgsl-overrides.ron | 2 + .../out/ir/wgsl-storage-textures.compact.ron | 6 ++ naga/tests/out/ir/wgsl-storage-textures.ron | 6 ++ ...l-template-list-trailing-comma.compact.ron | 4 + .../ir/wgsl-template-list-trailing-comma.ron | 4 + .../out/ir/wgsl-texture-external.compact.ron | 2 + naga/tests/out/ir/wgsl-texture-external.ron | 2 + .../ir/wgsl-types_with_comments.compact.ron | 1 + .../tests/out/ir/wgsl-types_with_comments.ron | 3 + .../wgsl-memory-decorations-coherent.metal | 25 ++++++ .../wgsl-memory-decorations-coherent.spvasm | 39 ++++++++++ .../out/spv/wgsl-memory-decorations.spvasm | 52 +++++++++++++ .../wgsl-memory-decorations-coherent.wgsl | 15 ++++ .../out/wgsl/wgsl-memory-decorations.wgsl | 21 +++++ wgpu-core/src/device/mod.rs | 8 ++ wgpu-hal/src/dx12/adapter.rs | 3 +- wgpu-hal/src/gles/adapter.rs | 7 ++ wgpu-hal/src/metal/adapter.rs | 4 + wgpu-hal/src/metal/device.rs | 5 +- wgpu-hal/src/vulkan/adapter.rs | 4 +- wgpu-types/src/features.rs | 20 +++++ 65 files changed, 658 insertions(+), 15 deletions(-) create mode 100644 naga/tests/in/wgsl/memory-decorations-coherent.toml create mode 100644 naga/tests/in/wgsl/memory-decorations-coherent.wgsl create mode 100644 naga/tests/in/wgsl/memory-decorations.toml create mode 100644 naga/tests/in/wgsl/memory-decorations.wgsl create mode 100644 naga/tests/out/glsl/wgsl-memory-decorations-coherent.main.Compute.glsl create mode 100644 naga/tests/out/glsl/wgsl-memory-decorations.main.Compute.glsl create mode 100644 naga/tests/out/hlsl/wgsl-memory-decorations-coherent.hlsl create mode 100644 naga/tests/out/hlsl/wgsl-memory-decorations-coherent.ron create mode 100644 naga/tests/out/msl/wgsl-memory-decorations-coherent.metal create mode 100644 naga/tests/out/spv/wgsl-memory-decorations-coherent.spvasm create mode 100644 naga/tests/out/spv/wgsl-memory-decorations.spvasm create mode 100644 naga/tests/out/wgsl/wgsl-memory-decorations-coherent.wgsl create mode 100644 naga/tests/out/wgsl/wgsl-memory-decorations.wgsl diff --git a/CHANGELOG.md b/CHANGELOG.md index 441bf2e95..144573fc7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -154,6 +154,7 @@ depth_stencil: Some(wgpu::DepthStencilState::stencil( - Initial wgsl-in ray tracing pipelines. By @Vecvec in [#8570](https://github.com/gfx-rs/wgpu/pull/8570). - wgsl-out ray tracing pipelines. By @Vecvec in [#8970](https://github.com/gfx-rs/wgpu/pull/8970). - Allow parsing shaders which make use of `SPV_KHR_non_semantic_info` for debug info. Also removes `naga::front::spv::SUPPORTED_EXT_SETS`. By @inner-daemons in #8827. +- Added memory decorations for storage buffers: `coherent`, supported on all native backends, and `volatile`, only on Vulkan and GL. By @atlv24 in [#9168](https://github.com/gfx-rs/wgpu/pull/9168). #### GLES diff --git a/naga/src/back/glsl/mod.rs b/naga/src/back/glsl/mod.rs index 87f30d5d4..6c7576f6c 100644 --- a/naga/src/back/glsl/mod.rs +++ b/naga/src/back/glsl/mod.rs @@ -642,4 +642,6 @@ pub fn supported_capabilities() -> valid::Capabilities { | Caps::SHADER_FLOAT16_IN_FLOAT32 | Caps::SHADER_BARYCENTRICS | Caps::DRAW_INDEX + | Caps::MEMORY_DECORATION_COHERENT + | Caps::MEMORY_DECORATION_VOLATILE } diff --git a/naga/src/back/glsl/writer.rs b/naga/src/back/glsl/writer.rs index f28466338..7232605ff 100644 --- a/naga/src/back/glsl/writer.rs +++ b/naga/src/back/glsl/writer.rs @@ -699,6 +699,18 @@ impl<'a, W: Write> Writer<'a, W> { if let crate::AddressSpace::Storage { access } = global.space { self.write_storage_access(access)?; + if global + .memory_decorations + .contains(crate::MemoryDecorations::COHERENT) + { + write!(self.out, "coherent ")?; + } + if global + .memory_decorations + .contains(crate::MemoryDecorations::VOLATILE) + { + write!(self.out, "volatile ")?; + } } if let Some(storage_qualifier) = glsl_storage_qualifier(global.space) { diff --git a/naga/src/back/hlsl/mod.rs b/naga/src/back/hlsl/mod.rs index b2c99b701..07f2f7a47 100644 --- a/naga/src/back/hlsl/mod.rs +++ b/naga/src/back/hlsl/mod.rs @@ -800,8 +800,10 @@ pub fn supported_capabilities() -> crate::valid::Capabilities { // No BUFFER_BINDING_ARRAY_NON_UNIFORM_INDEXING | Caps::STORAGE_TEXTURE_BINDING_ARRAY_NON_UNIFORM_INDEXING | Caps::STORAGE_BUFFER_BINDING_ARRAY_NON_UNIFORM_INDEXING - // No COOPERATIVE_MATRIX - // No PER_VERTEX - // No RAY_TRACING_PIPELINE - // No DRAW_INDEX + // No COOPERATIVE_MATRIX + // No PER_VERTEX + // No RAY_TRACING_PIPELINE + // No DRAW_INDEX + // No MEMORY_DECORATION_VOLATILE + | Caps::MEMORY_DECORATION_COHERENT } diff --git a/naga/src/back/hlsl/writer.rs b/naga/src/back/hlsl/writer.rs index d11dc861a..4a08e9f18 100644 --- a/naga/src/back/hlsl/writer.rs +++ b/naga/src/back/hlsl/writer.rs @@ -990,6 +990,12 @@ impl<'a, W: fmt::Write> super::Writer<'a, W> { "b" } crate::AddressSpace::Storage { access } => { + if global + .memory_decorations + .contains(crate::MemoryDecorations::COHERENT) + { + write!(self.out, "globallycoherent ")?; + } let (prefix, register) = if access.contains(crate::StorageAccess::STORE) { ("RW", "u") } else { diff --git a/naga/src/back/msl/mod.rs b/naga/src/back/msl/mod.rs index 2bc2ce8e5..39b508df6 100644 --- a/naga/src/back/msl/mod.rs +++ b/naga/src/back/msl/mod.rs @@ -871,9 +871,11 @@ pub fn supported_capabilities() -> crate::valid::Capabilities { | Caps::STORAGE_TEXTURE_BINDING_ARRAY_NON_UNIFORM_INDEXING | Caps::STORAGE_BUFFER_BINDING_ARRAY_NON_UNIFORM_INDEXING | Caps::COOPERATIVE_MATRIX - // No PER_VERTEX - // No RAY_TRACING_PIPELINE - // No DRAW_INDEX + // No PER_VERTEX + // No RAY_TRACING_PIPELINE + // No DRAW_INDEX + // No MEMORY_DECORATION_VOLATILE + | Caps::MEMORY_DECORATION_COHERENT } #[test] diff --git a/naga/src/back/msl/writer.rs b/naga/src/back/msl/writer.rs index 62a24503a..23bd768ba 100644 --- a/naga/src/back/msl/writer.rs +++ b/naga/src/back/msl/writer.rs @@ -428,8 +428,16 @@ impl TypedGlobalVariable<'_> { first_time: false, }; - let (space, access, reference) = match var.space.to_msl_name() { + let (coherent, space, access, reference) = match var.space.to_msl_name() { Some(space) if self.reference => { + let coherent = if var + .memory_decorations + .contains(crate::MemoryDecorations::COHERENT) + { + "coherent " + } else { + "" + }; let access = if var.space.needs_access_qualifier() && !self.usage.intersects(valid::GlobalUse::WRITE) { @@ -437,14 +445,15 @@ impl TypedGlobalVariable<'_> { } else { "" }; - (space, access, "&") + (coherent, space, access, "&") } - _ => ("", "", ""), + _ => ("", "", "", ""), }; Ok(write!( out, - "{}{}{}{}{}{} {}", + "{}{}{}{}{}{}{} {}", + coherent, space, if space.is_empty() { "" } else { " " }, ty_name, diff --git a/naga/src/back/spv/mod.rs b/naga/src/back/spv/mod.rs index 93fef4458..df115a83f 100644 --- a/naga/src/back/spv/mod.rs +++ b/naga/src/back/spv/mod.rs @@ -1191,4 +1191,6 @@ pub fn supported_capabilities() -> crate::valid::Capabilities { | Caps::PER_VERTEX // No RAY_TRACING_PIPELINE | Caps::DRAW_INDEX + | Caps::MEMORY_DECORATION_COHERENT + | Caps::MEMORY_DECORATION_VOLATILE } diff --git a/naga/src/back/spv/writer.rs b/naga/src/back/spv/writer.rs index e41de1098..5fc74350c 100644 --- a/naga/src/back/spv/writer.rs +++ b/naga/src/back/spv/writer.rs @@ -3288,6 +3288,19 @@ impl Writer { //self.check(class.required_capabilities())?; + if global_variable + .memory_decorations + .contains(crate::MemoryDecorations::COHERENT) + { + self.decorate(id, Decoration::Coherent, &[]); + } + if global_variable + .memory_decorations + .contains(crate::MemoryDecorations::VOLATILE) + { + self.decorate(id, Decoration::Volatile, &[]); + } + if self.flags.contains(WriterFlags::DEBUG) { if let Some(ref name) = global_variable.name { self.debugs.push(Instruction::name(id, name)); diff --git a/naga/src/back/wgsl/writer.rs b/naga/src/back/wgsl/writer.rs index 47c0e7215..b328a91ec 100644 --- a/naga/src/back/wgsl/writer.rs +++ b/naga/src/back/wgsl/writer.rs @@ -1974,6 +1974,19 @@ impl Writer { writeln!(self.out)?; } + if global + .memory_decorations + .contains(crate::MemoryDecorations::COHERENT) + { + write!(self.out, "@coherent ")?; + } + if global + .memory_decorations + .contains(crate::MemoryDecorations::VOLATILE) + { + write!(self.out, "@volatile ")?; + } + // First write global name and address space if supported write!(self.out, "var")?; let (address, maybe_access) = address_space_str(global.space); diff --git a/naga/src/front/glsl/variables.rs b/naga/src/front/glsl/variables.rs index 166c8adf3..10b4ff429 100644 --- a/naga/src/front/glsl/variables.rs +++ b/naga/src/front/glsl/variables.rs @@ -62,6 +62,7 @@ impl Frontend { binding: None, ty, init: None, + memory_decorations: crate::MemoryDecorations::empty(), }, meta, ); @@ -448,6 +449,7 @@ impl Frontend { binding: None, ty, init, + memory_decorations: crate::MemoryDecorations::empty(), }, meta, ); @@ -636,6 +638,7 @@ impl Frontend { binding, ty, init, + memory_decorations: crate::MemoryDecorations::empty(), }, meta, ); diff --git a/naga/src/front/spv/mod.rs b/naga/src/front/spv/mod.rs index 644be8397..0ddb20b81 100644 --- a/naga/src/front/spv/mod.rs +++ b/naga/src/front/spv/mod.rs @@ -189,6 +189,8 @@ bitflags::bitflags! { struct DecorationFlags: u32 { const NON_READABLE = 0x1; const NON_WRITABLE = 0x2; + const COHERENT = 0x4; + const VOLATILE = 0x8; } } @@ -203,6 +205,17 @@ impl DecorationFlags { } access } + + fn to_memory_decorations(self) -> crate::MemoryDecorations { + let mut decorations = crate::MemoryDecorations::empty(); + if self.contains(DecorationFlags::COHERENT) { + decorations |= crate::MemoryDecorations::COHERENT; + } + if self.contains(DecorationFlags::VOLATILE) { + decorations |= crate::MemoryDecorations::VOLATILE; + } + decorations + } } #[derive(Debug, PartialEq)] @@ -813,6 +826,12 @@ impl> Frontend { spirv::Decoration::NonWritable => { dec.flags |= DecorationFlags::NON_WRITABLE; } + spirv::Decoration::Coherent => { + dec.flags |= DecorationFlags::COHERENT; + } + spirv::Decoration::Volatile => { + dec.flags |= DecorationFlags::VOLATILE; + } spirv::Decoration::ColMajor => { dec.matrix_major = Some(Majority::Column); } @@ -3007,6 +3026,7 @@ impl> Frontend { space, ty, init, + memory_decorations: dec.flags.to_memory_decorations(), }; (Variable::Global, var) } @@ -3054,6 +3074,7 @@ impl> Frontend { binding: None, ty, init: None, + memory_decorations: crate::MemoryDecorations::empty(), }; let inner = Variable::Input(crate::FunctionArgument { @@ -3114,6 +3135,7 @@ impl> Frontend { binding: None, ty, init, + memory_decorations: crate::MemoryDecorations::empty(), }; let inner = Variable::Output(crate::FunctionResult { ty, binding }); (inner, var) diff --git a/naga/src/front/wgsl/lower/mod.rs b/naga/src/front/wgsl/lower/mod.rs index 20a5a3205..e7fb73e87 100644 --- a/naga/src/front/wgsl/lower/mod.rs +++ b/naga/src/front/wgsl/lower/mod.rs @@ -1348,6 +1348,7 @@ impl<'source, 'temp> Lowerer<'source, 'temp> { binding, ty, init: initializer, + memory_decorations: v.memory_decorations, }, span, ); diff --git a/naga/src/front/wgsl/parse/ast.rs b/naga/src/front/wgsl/parse/ast.rs index f6a45d595..40230242a 100644 --- a/naga/src/front/wgsl/parse/ast.rs +++ b/naga/src/front/wgsl/parse/ast.rs @@ -269,6 +269,9 @@ pub struct GlobalVariable<'a> { pub ty: Option>, pub init: Option>>, pub doc_comments: Vec<&'a str>, + + /// Memory decorations for this variable (`@coherent`, `@volatile`). + pub memory_decorations: crate::MemoryDecorations, } #[derive(Debug)] diff --git a/naga/src/front/wgsl/parse/mod.rs b/naga/src/front/wgsl/parse/mod.rs index c1360b473..7da8f4db1 100644 --- a/naga/src/front/wgsl/parse/mod.rs +++ b/naga/src/front/wgsl/parse/mod.rs @@ -891,6 +891,7 @@ impl Parser { ty, init, doc_comments: Vec::new(), + memory_decorations: crate::MemoryDecorations::empty(), }) } @@ -1849,6 +1850,7 @@ impl Parser { let mut mesh_output = ParsedAttribute::default(); let mut must_use: ParsedAttribute = ParsedAttribute::default(); + let mut memory_decorations = crate::MemoryDecorations::empty(); let mut dependencies = FastIndexSet::default(); let mut ctx = ExpressionContext { @@ -2017,6 +2019,12 @@ impl Parser { "must_use" => { must_use.set(name_span, name_span)?; } + "coherent" => { + memory_decorations |= crate::MemoryDecorations::COHERENT; + } + "volatile" => { + memory_decorations |= crate::MemoryDecorations::VOLATILE; + } _ => return Err(Box::new(Error::UnknownAttribute(name_span))), } } @@ -2116,6 +2124,7 @@ impl Parser { let mut var = self.variable_decl(lexer, &mut ctx)?; var.binding = binding.take(); var.doc_comments = doc_comments; + var.memory_decorations = memory_decorations; Some(ast::GlobalDeclKind::Var(var)) } (Token::Word("fn"), _) => { diff --git a/naga/src/ir/mod.rs b/naga/src/ir/mod.rs index 53e3a5343..98b06dffc 100644 --- a/naga/src/ir/mod.rs +++ b/naga/src/ir/mod.rs @@ -727,6 +727,22 @@ bitflags::bitflags! { } } +bitflags::bitflags! { + /// Memory decorations for global variables. + #[cfg_attr(feature = "serialize", derive(Serialize))] + #[cfg_attr(feature = "deserialize", derive(Deserialize))] + #[cfg_attr(feature = "arbitrary", derive(Arbitrary))] + #[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)] + pub struct MemoryDecorations: u8 { + /// Reads and writes are automatically visible to other invocations + /// without explicit barriers. + const COHERENT = 0x1; + /// The variable may be modified by something external to the shader, + /// preventing certain compiler optimizations. + const VOLATILE = 0x2; + } +} + /// Image storage format. #[derive(Clone, Copy, Debug, Hash, Eq, Ord, PartialEq, PartialOrd)] #[cfg_attr(feature = "serialize", derive(Serialize))] @@ -1170,6 +1186,13 @@ pub struct GlobalVariable { /// /// This refers to an [`Expression`] in [`Module::global_expressions`]. pub init: Option>, + /// Memory decorations for this variable. + /// + /// These are meaningful for storage address space variables in SPIR-V, + /// where they map to SPIR-V memory decorations on the variable. + /// + /// In WGSL, these can be set with attributes like `@coherent` or `@volatile`. + pub memory_decorations: MemoryDecorations, } /// Variable defined at function level. diff --git a/naga/src/valid/analyzer.rs b/naga/src/valid/analyzer.rs index 06d116a0b..30ab11aef 100644 --- a/naga/src/valid/analyzer.rs +++ b/naga/src/valid/analyzer.rs @@ -1323,6 +1323,7 @@ fn uniform_control_flow() { ty, space: crate::AddressSpace::Handle, binding: None, + memory_decorations: crate::MemoryDecorations::empty(), }, Default::default(), ); @@ -1333,6 +1334,7 @@ fn uniform_control_flow() { ty, binding: None, space: crate::AddressSpace::Uniform, + memory_decorations: crate::MemoryDecorations::empty(), }, Default::default(), ); diff --git a/naga/src/valid/handles.rs b/naga/src/valid/handles.rs index fd0b4ed8d..93af8a69d 100644 --- a/naga/src/valid/handles.rs +++ b/naga/src/valid/handles.rs @@ -160,6 +160,7 @@ impl super::Validator { binding: _, ty, init, + memory_decorations: _, } = global_variable; validate_type(ty)?; if let Some(init_expr) = init { diff --git a/naga/src/valid/interface.rs b/naga/src/valid/interface.rs index 6466e2563..018fd9232 100644 --- a/naga/src/valid/interface.rs +++ b/naga/src/valid/interface.rs @@ -47,6 +47,12 @@ pub enum GlobalVariableError { InvalidImmediateType(#[source] ImmediateError), #[error("Task payload must not be zero-sized")] ZeroSizedTaskPayload, + #[error("Memory decorations (`@coherent`, `@volatile`) are only valid for variables in the `storage` address space")] + InvalidMemoryDecorationsAddressSpace, + #[error("`@coherent` requires the MEMORY_DECORATION_COHERENT capability")] + CoherentNotSupported, + #[error("`@volatile` requires the MEMORY_DECORATION_VOLATILE capability")] + VolatileNotSupported, } #[derive(Clone, Debug, thiserror::Error)] @@ -1127,6 +1133,30 @@ impl super::Validator { } } + if !var.memory_decorations.is_empty() + && !matches!(var.space, crate::AddressSpace::Storage { .. }) + { + return Err(GlobalVariableError::InvalidMemoryDecorationsAddressSpace); + } + if var + .memory_decorations + .contains(crate::MemoryDecorations::COHERENT) + && !self + .capabilities + .contains(Capabilities::MEMORY_DECORATION_COHERENT) + { + return Err(GlobalVariableError::CoherentNotSupported); + } + if var + .memory_decorations + .contains(crate::MemoryDecorations::VOLATILE) + && !self + .capabilities + .contains(Capabilities::MEMORY_DECORATION_VOLATILE) + { + return Err(GlobalVariableError::VolatileNotSupported); + } + if let Some(init) = var.init { match var.space { crate::AddressSpace::Private | crate::AddressSpace::Function => {} diff --git a/naga/src/valid/mod.rs b/naga/src/valid/mod.rs index 204453151..5b557e8b3 100644 --- a/naga/src/valid/mod.rs +++ b/naga/src/valid/mod.rs @@ -210,6 +210,10 @@ bitflags::bitflags! { const DRAW_INDEX = 1 << 39; /// Support for binding arrays of acceleration structures. const ACCELERATION_STRUCTURE_BINDING_ARRAY = 1 << 40; + /// Support for the `@coherent` memory decoration on storage buffers. + const MEMORY_DECORATION_COHERENT = 1 << 41; + /// Support for the `@volatile` memory decoration on storage buffers. + const MEMORY_DECORATION_VOLATILE = 1 << 42; } } diff --git a/naga/tests/in/wgsl/memory-decorations-coherent.toml b/naga/tests/in/wgsl/memory-decorations-coherent.toml new file mode 100644 index 000000000..ead31eded --- /dev/null +++ b/naga/tests/in/wgsl/memory-decorations-coherent.toml @@ -0,0 +1,11 @@ +capabilities = "MEMORY_DECORATION_COHERENT" +targets = "SPIRV | HLSL | GLSL | WGSL | METAL" + +[spv] +version = [1, 0] + +[glsl] +version = { Desktop = 430 } + +[msl] +lang_version = [3, 2] diff --git a/naga/tests/in/wgsl/memory-decorations-coherent.wgsl b/naga/tests/in/wgsl/memory-decorations-coherent.wgsl new file mode 100644 index 000000000..6d8265afd --- /dev/null +++ b/naga/tests/in/wgsl/memory-decorations-coherent.wgsl @@ -0,0 +1,14 @@ +struct Data { + values: array, +} + +@group(0) @binding(0) @coherent +var coherent_buf: Data; + +@group(0) @binding(1) +var plain_buf: Data; + +@compute @workgroup_size(1) +fn main() { + coherent_buf.values[0] = plain_buf.values[0]; +} diff --git a/naga/tests/in/wgsl/memory-decorations.toml b/naga/tests/in/wgsl/memory-decorations.toml new file mode 100644 index 000000000..2b365442e --- /dev/null +++ b/naga/tests/in/wgsl/memory-decorations.toml @@ -0,0 +1,8 @@ +capabilities = "MEMORY_DECORATION_COHERENT | MEMORY_DECORATION_VOLATILE" +targets = "SPIRV | GLSL | WGSL" + +[spv] +version = [1, 0] + +[glsl] +version = { Desktop = 430 } diff --git a/naga/tests/in/wgsl/memory-decorations.wgsl b/naga/tests/in/wgsl/memory-decorations.wgsl new file mode 100644 index 000000000..a0aab23f1 --- /dev/null +++ b/naga/tests/in/wgsl/memory-decorations.wgsl @@ -0,0 +1,21 @@ +struct Data { + values: array, +} + +@group(0) @binding(0) @coherent +var coherent_buf: Data; + +@group(0) @binding(1) @volatile +var volatile_buf: Data; + +@group(0) @binding(2) @coherent @volatile +var both_buf: Data; + +@group(0) @binding(3) +var plain_buf: Data; + +@compute @workgroup_size(1) +fn main() { + coherent_buf.values[0] = volatile_buf.values[0]; + both_buf.values[0] = plain_buf.values[0]; +} diff --git a/naga/tests/naga/validation.rs b/naga/tests/naga/validation.rs index c73ebeeb2..8b4852a08 100644 --- a/naga/tests/naga/validation.rs +++ b/naga/tests/naga/validation.rs @@ -111,6 +111,7 @@ fn populate_atomic_result() { binding: None, ty: ty_atomic_u32, init: None, + memory_decorations: naga::MemoryDecorations::empty(), }, span, ); @@ -281,6 +282,7 @@ fn emit_workgroup_uniform_load_result() { binding: None, ty: ty_u32, init: None, + memory_decorations: naga::MemoryDecorations::empty(), }, span, ); @@ -879,6 +881,7 @@ fn invalid_local_var_override_sized_array() { binding: None, ty: ty_array, init: None, + memory_decorations: naga::MemoryDecorations::empty(), }, span, ); @@ -1633,6 +1636,7 @@ fn unexpected_task_payload() { binding: None, ty: ty_payload, init: None, + memory_decorations: ir::MemoryDecorations::empty(), }, err_span, ); @@ -1665,3 +1669,77 @@ fn unexpected_task_payload() { } )); } + +#[test] +fn coherent_requires_capability() { + let module = naga::front::wgsl::parse_str( + "struct S { x: u32 } + @group(0) @binding(0) @coherent var buf: S;", + ) + .expect("module should parse"); + + let err = valid::Validator::new(ValidationFlags::default(), Capabilities::empty()) + .validate(&module) + .expect_err("should fail without capability"); + assert!(matches!( + err.into_inner(), + valid::ValidationError::GlobalVariable { + source: valid::GlobalVariableError::CoherentNotSupported, + .. + } + )); + + let result = valid::Validator::new( + ValidationFlags::default(), + Capabilities::MEMORY_DECORATION_COHERENT, + ) + .validate(&module); + assert!(result.is_ok(), "should succeed with capability: {result:?}"); +} + +#[test] +fn volatile_requires_capability() { + let module = naga::front::wgsl::parse_str( + "struct S { x: u32 } + @group(0) @binding(0) @volatile var buf: S;", + ) + .expect("module should parse"); + + let err = valid::Validator::new(ValidationFlags::default(), Capabilities::empty()) + .validate(&module) + .expect_err("should fail without capability"); + assert!(matches!( + err.into_inner(), + valid::ValidationError::GlobalVariable { + source: valid::GlobalVariableError::VolatileNotSupported, + .. + } + )); + + let result = valid::Validator::new( + ValidationFlags::default(), + Capabilities::MEMORY_DECORATION_VOLATILE, + ) + .validate(&module); + assert!(result.is_ok(), "should succeed with capability: {result:?}"); +} + +#[test] +fn memory_decorations_require_storage_address_space() { + let module = naga::front::wgsl::parse_str("@coherent var wg: array;") + .expect("module should parse"); + + let err = valid::Validator::new( + ValidationFlags::default(), + Capabilities::MEMORY_DECORATION_COHERENT | Capabilities::MEMORY_DECORATION_VOLATILE, + ) + .validate(&module) + .expect_err("should fail on non-storage address space"); + assert!(matches!( + err.into_inner(), + valid::ValidationError::GlobalVariable { + source: valid::GlobalVariableError::InvalidMemoryDecorationsAddressSpace, + .. + } + )); +} diff --git a/naga/tests/out/glsl/wgsl-memory-decorations-coherent.main.Compute.glsl b/naga/tests/out/glsl/wgsl-memory-decorations-coherent.main.Compute.glsl new file mode 100644 index 000000000..d83f84948 --- /dev/null +++ b/naga/tests/out/glsl/wgsl-memory-decorations-coherent.main.Compute.glsl @@ -0,0 +1,20 @@ +#version 430 core +#extension GL_ARB_compute_shader : require +#extension GL_ARB_shader_storage_buffer_object : require +layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in; + +layout(std430) coherent buffer Data_block_0Compute { + uint values[]; +} _group_0_binding_0_cs; + +layout(std430) buffer Data_block_1Compute { + uint values[]; +} _group_0_binding_1_cs; + + +void main() { + uint _e6 = _group_0_binding_1_cs.values[0]; + _group_0_binding_0_cs.values[0] = _e6; + return; +} + diff --git a/naga/tests/out/glsl/wgsl-memory-decorations.main.Compute.glsl b/naga/tests/out/glsl/wgsl-memory-decorations.main.Compute.glsl new file mode 100644 index 000000000..70650cde4 --- /dev/null +++ b/naga/tests/out/glsl/wgsl-memory-decorations.main.Compute.glsl @@ -0,0 +1,30 @@ +#version 430 core +#extension GL_ARB_compute_shader : require +#extension GL_ARB_shader_storage_buffer_object : require +layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in; + +layout(std430) coherent buffer Data_block_0Compute { + uint values[]; +} _group_0_binding_0_cs; + +layout(std430) volatile buffer Data_block_1Compute { + uint values[]; +} _group_0_binding_1_cs; + +layout(std430) coherent volatile buffer Data_block_2Compute { + uint values[]; +} _group_0_binding_2_cs; + +layout(std430) buffer Data_block_3Compute { + uint values[]; +} _group_0_binding_3_cs; + + +void main() { + uint _e6 = _group_0_binding_1_cs.values[0]; + _group_0_binding_0_cs.values[0] = _e6; + uint _e13 = _group_0_binding_3_cs.values[0]; + _group_0_binding_2_cs.values[0] = _e13; + return; +} + diff --git a/naga/tests/out/hlsl/wgsl-memory-decorations-coherent.hlsl b/naga/tests/out/hlsl/wgsl-memory-decorations-coherent.hlsl new file mode 100644 index 000000000..9563c1cc2 --- /dev/null +++ b/naga/tests/out/hlsl/wgsl-memory-decorations-coherent.hlsl @@ -0,0 +1,10 @@ +globallycoherent RWByteAddressBuffer coherent_buf : register(u0); +RWByteAddressBuffer plain_buf : register(u1); + +[numthreads(1, 1, 1)] +void main() +{ + uint _e6 = asuint(plain_buf.Load(0+0)); + coherent_buf.Store(0+0, asuint(_e6)); + return; +} diff --git a/naga/tests/out/hlsl/wgsl-memory-decorations-coherent.ron b/naga/tests/out/hlsl/wgsl-memory-decorations-coherent.ron new file mode 100644 index 000000000..a07b03300 --- /dev/null +++ b/naga/tests/out/hlsl/wgsl-memory-decorations-coherent.ron @@ -0,0 +1,12 @@ +( + vertex:[ + ], + fragment:[ + ], + compute:[ + ( + entry_point:"main", + target_profile:"cs_5_1", + ), + ], +) diff --git a/naga/tests/out/ir/spv-fetch_depth.compact.ron b/naga/tests/out/ir/spv-fetch_depth.compact.ron index 56efde17e..d07af0c72 100644 --- a/naga/tests/out/ir/spv-fetch_depth.compact.ron +++ b/naga/tests/out/ir/spv-fetch_depth.compact.ron @@ -91,6 +91,7 @@ )), ty: 2, init: None, + memory_decorations: (""), ), ( name: None, @@ -103,6 +104,7 @@ )), ty: 4, init: None, + memory_decorations: (""), ), ( name: None, @@ -113,6 +115,7 @@ )), ty: 5, init: None, + memory_decorations: (""), ), ], global_expressions: [ diff --git a/naga/tests/out/ir/spv-fetch_depth.ron b/naga/tests/out/ir/spv-fetch_depth.ron index 161f0fc8f..024dff874 100644 --- a/naga/tests/out/ir/spv-fetch_depth.ron +++ b/naga/tests/out/ir/spv-fetch_depth.ron @@ -159,6 +159,7 @@ )), ty: 4, init: None, + memory_decorations: (""), ), ( name: None, @@ -171,6 +172,7 @@ )), ty: 7, init: None, + memory_decorations: (""), ), ( name: None, @@ -181,6 +183,7 @@ )), ty: 10, init: None, + memory_decorations: (""), ), ], global_expressions: [ diff --git a/naga/tests/out/ir/spv-shadow.compact.ron b/naga/tests/out/ir/spv-shadow.compact.ron index 9a9851610..478005748 100644 --- a/naga/tests/out/ir/spv-shadow.compact.ron +++ b/naga/tests/out/ir/spv-shadow.compact.ron @@ -267,6 +267,7 @@ )), ty: 5, init: None, + memory_decorations: (""), ), ( name: Some("sampler_shadow"), @@ -277,6 +278,7 @@ )), ty: 13, init: None, + memory_decorations: (""), ), ( name: Some("u_globals"), @@ -287,6 +289,7 @@ )), ty: 8, init: None, + memory_decorations: (""), ), ( name: Some("s_lights"), @@ -299,6 +302,7 @@ )), ty: 12, init: None, + memory_decorations: (""), ), ( name: Some("in_position_fs"), @@ -306,6 +310,7 @@ binding: None, ty: 3, init: None, + memory_decorations: (""), ), ( name: Some("in_normal_fs"), @@ -313,6 +318,7 @@ binding: None, ty: 1, init: None, + memory_decorations: (""), ), ( name: Some("out_color_fs"), @@ -320,6 +326,7 @@ binding: None, ty: 3, init: None, + memory_decorations: (""), ), ], global_expressions: [ diff --git a/naga/tests/out/ir/spv-shadow.ron b/naga/tests/out/ir/spv-shadow.ron index 3ea0a97b3..95130a6c5 100644 --- a/naga/tests/out/ir/spv-shadow.ron +++ b/naga/tests/out/ir/spv-shadow.ron @@ -470,6 +470,7 @@ )), ty: 6, init: None, + memory_decorations: (""), ), ( name: Some("sampler_shadow"), @@ -480,6 +481,7 @@ )), ty: 29, init: None, + memory_decorations: (""), ), ( name: Some("u_globals"), @@ -490,6 +492,7 @@ )), ty: 12, init: None, + memory_decorations: (""), ), ( name: Some("s_lights"), @@ -502,6 +505,7 @@ )), ty: 19, init: None, + memory_decorations: (""), ), ( name: Some("in_position_fs"), @@ -509,6 +513,7 @@ binding: None, ty: 3, init: None, + memory_decorations: (""), ), ( name: Some("in_normal_fs"), @@ -516,6 +521,7 @@ binding: None, ty: 1, init: None, + memory_decorations: (""), ), ( name: Some("out_color_fs"), @@ -523,6 +529,7 @@ binding: None, ty: 3, init: None, + memory_decorations: (""), ), ], global_expressions: [ diff --git a/naga/tests/out/ir/spv-spec-constants.compact.ron b/naga/tests/out/ir/spv-spec-constants.compact.ron index 8bfa9f142..d56081dd3 100644 --- a/naga/tests/out/ir/spv-spec-constants.compact.ron +++ b/naga/tests/out/ir/spv-spec-constants.compact.ron @@ -215,6 +215,7 @@ binding: None, ty: 2, init: None, + memory_decorations: (""), ), ( name: Some("Vertex_Uv"), @@ -222,6 +223,7 @@ binding: None, ty: 2, init: None, + memory_decorations: (""), ), ( name: Some("Vertex_Position"), @@ -229,6 +231,7 @@ binding: None, ty: 3, init: None, + memory_decorations: (""), ), ( name: Some(""), @@ -239,6 +242,7 @@ )), ty: 4, init: None, + memory_decorations: (""), ), ( name: Some(""), @@ -246,6 +250,7 @@ binding: None, ty: 7, init: Some(11), + memory_decorations: (""), ), ( name: Some(""), @@ -256,6 +261,7 @@ )), ty: 9, init: None, + memory_decorations: (""), ), ( name: Some(""), @@ -266,6 +272,7 @@ )), ty: 10, init: None, + memory_decorations: (""), ), ( name: Some("Vertex_Normal"), @@ -273,6 +280,7 @@ binding: None, ty: 3, init: None, + memory_decorations: (""), ), ], global_expressions: [ diff --git a/naga/tests/out/ir/spv-spec-constants.ron b/naga/tests/out/ir/spv-spec-constants.ron index d835ace11..3adbe7f7b 100644 --- a/naga/tests/out/ir/spv-spec-constants.ron +++ b/naga/tests/out/ir/spv-spec-constants.ron @@ -316,6 +316,7 @@ binding: None, ty: 3, init: None, + memory_decorations: (""), ), ( name: Some("Vertex_Uv"), @@ -323,6 +324,7 @@ binding: None, ty: 3, init: None, + memory_decorations: (""), ), ( name: Some("Vertex_Position"), @@ -330,6 +332,7 @@ binding: None, ty: 5, init: None, + memory_decorations: (""), ), ( name: Some(""), @@ -340,6 +343,7 @@ )), ty: 8, init: None, + memory_decorations: (""), ), ( name: Some(""), @@ -347,6 +351,7 @@ binding: None, ty: 15, init: Some(13), + memory_decorations: (""), ), ( name: Some(""), @@ -357,6 +362,7 @@ )), ty: 18, init: None, + memory_decorations: (""), ), ( name: Some(""), @@ -367,6 +373,7 @@ )), ty: 21, init: None, + memory_decorations: (""), ), ( name: Some("Vertex_Normal"), @@ -374,6 +381,7 @@ binding: None, ty: 5, init: None, + memory_decorations: (""), ), ], global_expressions: [ diff --git a/naga/tests/out/ir/wgsl-access.compact.ron b/naga/tests/out/ir/wgsl-access.compact.ron index 3e2798767..1d60f9975 100644 --- a/naga/tests/out/ir/wgsl-access.compact.ron +++ b/naga/tests/out/ir/wgsl-access.compact.ron @@ -434,6 +434,7 @@ binding: None, ty: 3, init: Some(6), + memory_decorations: (""), ), ( name: Some("bar"), @@ -446,6 +447,7 @@ )), ty: 14, init: None, + memory_decorations: (""), ), ( name: Some("baz"), @@ -456,6 +458,7 @@ )), ty: 16, init: None, + memory_decorations: (""), ), ( name: Some("qux"), @@ -468,6 +471,7 @@ )), ty: 17, init: None, + memory_decorations: (""), ), ( name: Some("nested_mat_cx2"), @@ -478,6 +482,7 @@ )), ty: 20, init: None, + memory_decorations: (""), ), ], global_expressions: [ diff --git a/naga/tests/out/ir/wgsl-access.ron b/naga/tests/out/ir/wgsl-access.ron index 3e2798767..1d60f9975 100644 --- a/naga/tests/out/ir/wgsl-access.ron +++ b/naga/tests/out/ir/wgsl-access.ron @@ -434,6 +434,7 @@ binding: None, ty: 3, init: Some(6), + memory_decorations: (""), ), ( name: Some("bar"), @@ -446,6 +447,7 @@ )), ty: 14, init: None, + memory_decorations: (""), ), ( name: Some("baz"), @@ -456,6 +458,7 @@ )), ty: 16, init: None, + memory_decorations: (""), ), ( name: Some("qux"), @@ -468,6 +471,7 @@ )), ty: 17, init: None, + memory_decorations: (""), ), ( name: Some("nested_mat_cx2"), @@ -478,6 +482,7 @@ )), ty: 20, init: None, + memory_decorations: (""), ), ], global_expressions: [ diff --git a/naga/tests/out/ir/wgsl-collatz.compact.ron b/naga/tests/out/ir/wgsl-collatz.compact.ron index dc741ac0e..55392d7b9 100644 --- a/naga/tests/out/ir/wgsl-collatz.compact.ron +++ b/naga/tests/out/ir/wgsl-collatz.compact.ron @@ -62,6 +62,7 @@ )), ty: 2, init: None, + memory_decorations: (""), ), ], global_expressions: [], diff --git a/naga/tests/out/ir/wgsl-collatz.ron b/naga/tests/out/ir/wgsl-collatz.ron index dc741ac0e..55392d7b9 100644 --- a/naga/tests/out/ir/wgsl-collatz.ron +++ b/naga/tests/out/ir/wgsl-collatz.ron @@ -62,6 +62,7 @@ )), ty: 2, init: None, + memory_decorations: (""), ), ], global_expressions: [], diff --git a/naga/tests/out/ir/wgsl-overrides-atomicCompareExchangeWeak.compact.ron b/naga/tests/out/ir/wgsl-overrides-atomicCompareExchangeWeak.compact.ron index e76537924..57b9dd655 100644 --- a/naga/tests/out/ir/wgsl-overrides-atomicCompareExchangeWeak.compact.ron +++ b/naga/tests/out/ir/wgsl-overrides-atomicCompareExchangeWeak.compact.ron @@ -78,6 +78,7 @@ binding: None, ty: 2, init: None, + memory_decorations: (""), ), ], global_expressions: [], diff --git a/naga/tests/out/ir/wgsl-overrides-atomicCompareExchangeWeak.ron b/naga/tests/out/ir/wgsl-overrides-atomicCompareExchangeWeak.ron index e76537924..57b9dd655 100644 --- a/naga/tests/out/ir/wgsl-overrides-atomicCompareExchangeWeak.ron +++ b/naga/tests/out/ir/wgsl-overrides-atomicCompareExchangeWeak.ron @@ -78,6 +78,7 @@ binding: None, ty: 2, init: None, + memory_decorations: (""), ), ], global_expressions: [], diff --git a/naga/tests/out/ir/wgsl-overrides-ray-query.compact.ron b/naga/tests/out/ir/wgsl-overrides-ray-query.compact.ron index 5b681bef4..739bb00d0 100644 --- a/naga/tests/out/ir/wgsl-overrides-ray-query.compact.ron +++ b/naga/tests/out/ir/wgsl-overrides-ray-query.compact.ron @@ -108,6 +108,7 @@ )), ty: 1, init: None, + memory_decorations: (""), ), ], global_expressions: [], diff --git a/naga/tests/out/ir/wgsl-overrides-ray-query.ron b/naga/tests/out/ir/wgsl-overrides-ray-query.ron index 5b681bef4..739bb00d0 100644 --- a/naga/tests/out/ir/wgsl-overrides-ray-query.ron +++ b/naga/tests/out/ir/wgsl-overrides-ray-query.ron @@ -108,6 +108,7 @@ )), ty: 1, init: None, + memory_decorations: (""), ), ], global_expressions: [], diff --git a/naga/tests/out/ir/wgsl-overrides.compact.ron b/naga/tests/out/ir/wgsl-overrides.compact.ron index 0711878a9..1d58435cc 100644 --- a/naga/tests/out/ir/wgsl-overrides.compact.ron +++ b/naga/tests/out/ir/wgsl-overrides.compact.ron @@ -88,6 +88,7 @@ binding: None, ty: 1, init: Some(10), + memory_decorations: (""), ), ( name: Some("store_override"), @@ -95,6 +96,7 @@ binding: None, ty: 1, init: None, + memory_decorations: (""), ), ], global_expressions: [ diff --git a/naga/tests/out/ir/wgsl-overrides.ron b/naga/tests/out/ir/wgsl-overrides.ron index 0711878a9..1d58435cc 100644 --- a/naga/tests/out/ir/wgsl-overrides.ron +++ b/naga/tests/out/ir/wgsl-overrides.ron @@ -88,6 +88,7 @@ binding: None, ty: 1, init: Some(10), + memory_decorations: (""), ), ( name: Some("store_override"), @@ -95,6 +96,7 @@ binding: None, ty: 1, init: None, + memory_decorations: (""), ), ], global_expressions: [ diff --git a/naga/tests/out/ir/wgsl-storage-textures.compact.ron b/naga/tests/out/ir/wgsl-storage-textures.compact.ron index f2d3b49b7..62a34c79b 100644 --- a/naga/tests/out/ir/wgsl-storage-textures.compact.ron +++ b/naga/tests/out/ir/wgsl-storage-textures.compact.ron @@ -87,6 +87,7 @@ )), ty: 0, init: None, + memory_decorations: (""), ), ( name: Some("s_rg_r"), @@ -97,6 +98,7 @@ )), ty: 1, init: None, + memory_decorations: (""), ), ( name: Some("s_rgba_r"), @@ -107,6 +109,7 @@ )), ty: 2, init: None, + memory_decorations: (""), ), ( name: Some("s_r_w"), @@ -117,6 +120,7 @@ )), ty: 3, init: None, + memory_decorations: (""), ), ( name: Some("s_rg_w"), @@ -127,6 +131,7 @@ )), ty: 4, init: None, + memory_decorations: (""), ), ( name: Some("s_rgba_w"), @@ -137,6 +142,7 @@ )), ty: 5, init: None, + memory_decorations: (""), ), ], global_expressions: [], diff --git a/naga/tests/out/ir/wgsl-storage-textures.ron b/naga/tests/out/ir/wgsl-storage-textures.ron index f2d3b49b7..62a34c79b 100644 --- a/naga/tests/out/ir/wgsl-storage-textures.ron +++ b/naga/tests/out/ir/wgsl-storage-textures.ron @@ -87,6 +87,7 @@ )), ty: 0, init: None, + memory_decorations: (""), ), ( name: Some("s_rg_r"), @@ -97,6 +98,7 @@ )), ty: 1, init: None, + memory_decorations: (""), ), ( name: Some("s_rgba_r"), @@ -107,6 +109,7 @@ )), ty: 2, init: None, + memory_decorations: (""), ), ( name: Some("s_r_w"), @@ -117,6 +120,7 @@ )), ty: 3, init: None, + memory_decorations: (""), ), ( name: Some("s_rg_w"), @@ -127,6 +131,7 @@ )), ty: 4, init: None, + memory_decorations: (""), ), ( name: Some("s_rgba_w"), @@ -137,6 +142,7 @@ )), ty: 5, init: None, + memory_decorations: (""), ), ], global_expressions: [], diff --git a/naga/tests/out/ir/wgsl-template-list-trailing-comma.compact.ron b/naga/tests/out/ir/wgsl-template-list-trailing-comma.compact.ron index f4335f9b1..f57d94a05 100644 --- a/naga/tests/out/ir/wgsl-template-list-trailing-comma.compact.ron +++ b/naga/tests/out/ir/wgsl-template-list-trailing-comma.compact.ron @@ -41,6 +41,7 @@ binding: None, ty: 1, init: None, + memory_decorations: (""), ), ( name: Some("sized_no_comma"), @@ -48,6 +49,7 @@ binding: None, ty: 1, init: None, + memory_decorations: (""), ), ( name: Some("unsized_comma"), @@ -60,6 +62,7 @@ )), ty: 2, init: None, + memory_decorations: (""), ), ( name: Some("unsized_no_comma"), @@ -72,6 +75,7 @@ )), ty: 2, init: None, + memory_decorations: (""), ), ], global_expressions: [], diff --git a/naga/tests/out/ir/wgsl-template-list-trailing-comma.ron b/naga/tests/out/ir/wgsl-template-list-trailing-comma.ron index f4335f9b1..f57d94a05 100644 --- a/naga/tests/out/ir/wgsl-template-list-trailing-comma.ron +++ b/naga/tests/out/ir/wgsl-template-list-trailing-comma.ron @@ -41,6 +41,7 @@ binding: None, ty: 1, init: None, + memory_decorations: (""), ), ( name: Some("sized_no_comma"), @@ -48,6 +49,7 @@ binding: None, ty: 1, init: None, + memory_decorations: (""), ), ( name: Some("unsized_comma"), @@ -60,6 +62,7 @@ )), ty: 2, init: None, + memory_decorations: (""), ), ( name: Some("unsized_no_comma"), @@ -72,6 +75,7 @@ )), ty: 2, init: None, + memory_decorations: (""), ), ], global_expressions: [], diff --git a/naga/tests/out/ir/wgsl-texture-external.compact.ron b/naga/tests/out/ir/wgsl-texture-external.compact.ron index f22b5f8b8..4f0248296 100644 --- a/naga/tests/out/ir/wgsl-texture-external.compact.ron +++ b/naga/tests/out/ir/wgsl-texture-external.compact.ron @@ -190,6 +190,7 @@ )), ty: 8, init: None, + memory_decorations: (""), ), ( name: Some("samp"), @@ -200,6 +201,7 @@ )), ty: 9, init: None, + memory_decorations: (""), ), ], global_expressions: [], diff --git a/naga/tests/out/ir/wgsl-texture-external.ron b/naga/tests/out/ir/wgsl-texture-external.ron index f22b5f8b8..4f0248296 100644 --- a/naga/tests/out/ir/wgsl-texture-external.ron +++ b/naga/tests/out/ir/wgsl-texture-external.ron @@ -190,6 +190,7 @@ )), ty: 8, init: None, + memory_decorations: (""), ), ( name: Some("samp"), @@ -200,6 +201,7 @@ )), ty: 9, init: None, + memory_decorations: (""), ), ], global_expressions: [], diff --git a/naga/tests/out/ir/wgsl-types_with_comments.compact.ron b/naga/tests/out/ir/wgsl-types_with_comments.compact.ron index b11618912..53c55f49d 100644 --- a/naga/tests/out/ir/wgsl-types_with_comments.compact.ron +++ b/naga/tests/out/ir/wgsl-types_with_comments.compact.ron @@ -56,6 +56,7 @@ binding: None, ty: 0, init: None, + memory_decorations: (""), ), ], global_expressions: [ diff --git a/naga/tests/out/ir/wgsl-types_with_comments.ron b/naga/tests/out/ir/wgsl-types_with_comments.ron index 038535768..a7f952326 100644 --- a/naga/tests/out/ir/wgsl-types_with_comments.ron +++ b/naga/tests/out/ir/wgsl-types_with_comments.ron @@ -84,6 +84,7 @@ )), ty: 0, init: None, + memory_decorations: (""), ), ( name: Some("w_mem"), @@ -91,6 +92,7 @@ binding: None, ty: 1, init: None, + memory_decorations: (""), ), ( name: Some("w_mem2"), @@ -98,6 +100,7 @@ binding: None, ty: 1, init: None, + memory_decorations: (""), ), ], global_expressions: [ diff --git a/naga/tests/out/msl/wgsl-memory-decorations-coherent.metal b/naga/tests/out/msl/wgsl-memory-decorations-coherent.metal new file mode 100644 index 000000000..376631dec --- /dev/null +++ b/naga/tests/out/msl/wgsl-memory-decorations-coherent.metal @@ -0,0 +1,25 @@ +// language: metal3.2 +#include +#include + +using metal::uint; + +struct _mslBufferSizes { + uint size0; + uint size1; +}; + +typedef uint type_1[1]; +struct Data { + type_1 values; +}; + +kernel void main_( + coherent device Data& coherent_buf [[user(fake0)]] +, device Data const& plain_buf [[user(fake0)]] +, constant _mslBufferSizes& _buffer_sizes [[user(fake0)]] +) { + uint _e6 = plain_buf.values[0]; + coherent_buf.values[0] = _e6; + return; +} diff --git a/naga/tests/out/spv/wgsl-memory-decorations-coherent.spvasm b/naga/tests/out/spv/wgsl-memory-decorations-coherent.spvasm new file mode 100644 index 000000000..64ac53715 --- /dev/null +++ b/naga/tests/out/spv/wgsl-memory-decorations-coherent.spvasm @@ -0,0 +1,39 @@ +; SPIR-V +; Version: 1.0 +; Generator: rspirv +; Bound: 19 +OpCapability Shader +OpExtension "SPV_KHR_storage_buffer_storage_class" +%1 = OpExtInstImport "GLSL.std.450" +OpMemoryModel Logical GLSL450 +OpEntryPoint GLCompute %10 "main" +OpExecutionMode %10 LocalSize 1 1 1 +OpDecorate %4 ArrayStride 4 +OpMemberDecorate %5 0 Offset 0 +OpDecorate %5 Block +OpDecorate %6 Coherent +OpDecorate %6 DescriptorSet 0 +OpDecorate %6 Binding 0 +OpDecorate %8 DescriptorSet 0 +OpDecorate %8 Binding 1 +%2 = OpTypeVoid +%3 = OpTypeInt 32 0 +%4 = OpTypeRuntimeArray %3 +%5 = OpTypeStruct %4 +%7 = OpTypePointer StorageBuffer %5 +%6 = OpVariable %7 StorageBuffer +%8 = OpVariable %7 StorageBuffer +%11 = OpTypeFunction %2 +%13 = OpTypePointer StorageBuffer %4 +%14 = OpTypePointer StorageBuffer %3 +%15 = OpConstant %3 0 +%10 = OpFunction %2 None %11 +%9 = OpLabel +OpBranch %12 +%12 = OpLabel +%16 = OpAccessChain %14 %8 %15 %15 +%17 = OpLoad %3 %16 +%18 = OpAccessChain %14 %6 %15 %15 +OpStore %18 %17 +OpReturn +OpFunctionEnd \ No newline at end of file diff --git a/naga/tests/out/spv/wgsl-memory-decorations.spvasm b/naga/tests/out/spv/wgsl-memory-decorations.spvasm new file mode 100644 index 000000000..ccbaa35ba --- /dev/null +++ b/naga/tests/out/spv/wgsl-memory-decorations.spvasm @@ -0,0 +1,52 @@ +; SPIR-V +; Version: 1.0 +; Generator: rspirv +; Bound: 24 +OpCapability Shader +OpExtension "SPV_KHR_storage_buffer_storage_class" +%1 = OpExtInstImport "GLSL.std.450" +OpMemoryModel Logical GLSL450 +OpEntryPoint GLCompute %12 "main" +OpExecutionMode %12 LocalSize 1 1 1 +OpDecorate %4 ArrayStride 4 +OpMemberDecorate %5 0 Offset 0 +OpDecorate %5 Block +OpDecorate %6 Coherent +OpDecorate %6 DescriptorSet 0 +OpDecorate %6 Binding 0 +OpDecorate %8 Volatile +OpDecorate %8 DescriptorSet 0 +OpDecorate %8 Binding 1 +OpDecorate %9 Coherent +OpDecorate %9 Volatile +OpDecorate %9 DescriptorSet 0 +OpDecorate %9 Binding 2 +OpDecorate %10 DescriptorSet 0 +OpDecorate %10 Binding 3 +%2 = OpTypeVoid +%3 = OpTypeInt 32 0 +%4 = OpTypeRuntimeArray %3 +%5 = OpTypeStruct %4 +%7 = OpTypePointer StorageBuffer %5 +%6 = OpVariable %7 StorageBuffer +%8 = OpVariable %7 StorageBuffer +%9 = OpVariable %7 StorageBuffer +%10 = OpVariable %7 StorageBuffer +%13 = OpTypeFunction %2 +%15 = OpTypePointer StorageBuffer %4 +%16 = OpTypePointer StorageBuffer %3 +%17 = OpConstant %3 0 +%12 = OpFunction %2 None %13 +%11 = OpLabel +OpBranch %14 +%14 = OpLabel +%18 = OpAccessChain %16 %8 %17 %17 +%19 = OpLoad %3 %18 +%20 = OpAccessChain %16 %6 %17 %17 +OpStore %20 %19 +%21 = OpAccessChain %16 %10 %17 %17 +%22 = OpLoad %3 %21 +%23 = OpAccessChain %16 %9 %17 %17 +OpStore %23 %22 +OpReturn +OpFunctionEnd \ No newline at end of file diff --git a/naga/tests/out/wgsl/wgsl-memory-decorations-coherent.wgsl b/naga/tests/out/wgsl/wgsl-memory-decorations-coherent.wgsl new file mode 100644 index 000000000..699672e39 --- /dev/null +++ b/naga/tests/out/wgsl/wgsl-memory-decorations-coherent.wgsl @@ -0,0 +1,15 @@ +struct Data { + values: array, +} + +@group(0) @binding(0) +@coherent var coherent_buf: Data; +@group(0) @binding(1) +var plain_buf: Data; + +@compute @workgroup_size(1, 1, 1) +fn main() { + let _e6 = plain_buf.values[0]; + coherent_buf.values[0] = _e6; + return; +} diff --git a/naga/tests/out/wgsl/wgsl-memory-decorations.wgsl b/naga/tests/out/wgsl/wgsl-memory-decorations.wgsl new file mode 100644 index 000000000..5a062032b --- /dev/null +++ b/naga/tests/out/wgsl/wgsl-memory-decorations.wgsl @@ -0,0 +1,21 @@ +struct Data { + values: array, +} + +@group(0) @binding(0) +@coherent var coherent_buf: Data; +@group(0) @binding(1) +@volatile var volatile_buf: Data; +@group(0) @binding(2) +@coherent @volatile var both_buf: Data; +@group(0) @binding(3) +var plain_buf: Data; + +@compute @workgroup_size(1, 1, 1) +fn main() { + let _e6 = volatile_buf.values[0]; + coherent_buf.values[0] = _e6; + let _e13 = plain_buf.values[0]; + both_buf.values[0] = _e13; + return; +} diff --git a/wgpu-core/src/device/mod.rs b/wgpu-core/src/device/mod.rs index 841a91d70..10b352405 100644 --- a/wgpu-core/src/device/mod.rs +++ b/wgpu-core/src/device/mod.rs @@ -546,6 +546,14 @@ pub fn features_to_naga_capabilities( Caps::DRAW_INDEX, features.intersects(wgt::Features::SHADER_DRAW_INDEX), ); + caps.set( + Caps::MEMORY_DECORATION_COHERENT, + features.contains(wgt::Features::MEMORY_DECORATION_COHERENT), + ); + caps.set( + Caps::MEMORY_DECORATION_VOLATILE, + features.contains(wgt::Features::MEMORY_DECORATION_VOLATILE), + ); caps } diff --git a/wgpu-hal/src/dx12/adapter.rs b/wgpu-hal/src/dx12/adapter.rs index 38765f5e8..9eb96b8c6 100644 --- a/wgpu-hal/src/dx12/adapter.rs +++ b/wgpu-hal/src/dx12/adapter.rs @@ -490,7 +490,8 @@ impl super::Adapter { | wgt::Features::FLOAT32_FILTERABLE | wgt::Features::TEXTURE_ATOMIC | wgt::Features::PASSTHROUGH_SHADERS - | wgt::Features::EXTERNAL_TEXTURE; + | wgt::Features::EXTERNAL_TEXTURE + | wgt::Features::MEMORY_DECORATION_COHERENT; //TODO: in order to expose this, we need to run a compute shader // that extract the necessary statistics out of the D3D12 result. diff --git a/wgpu-hal/src/gles/adapter.rs b/wgpu-hal/src/gles/adapter.rs index 9f25e81c9..87aaa7f0e 100644 --- a/wgpu-hal/src/gles/adapter.rs +++ b/wgpu-hal/src/gles/adapter.rs @@ -664,6 +664,13 @@ impl super::Adapter { features.set(wgt::Features::INDIRECT_FIRST_INSTANCE, supported); } + // GLSL ES 3.10+ / GLSL 4.30+ natively support coherent/volatile qualifiers + // on storage buffers. These were introduced alongside storage buffer support. + if supports_storage { + features |= wgt::Features::MEMORY_DECORATION_COHERENT + | wgt::Features::MEMORY_DECORATION_VOLATILE; + } + let max_texture_size = unsafe { gl.get_parameter_i32(glow::MAX_TEXTURE_SIZE) } as u32; let max_texture_3d_size = unsafe { gl.get_parameter_i32(glow::MAX_3D_TEXTURE_SIZE) } as u32; diff --git a/wgpu-hal/src/metal/adapter.rs b/wgpu-hal/src/metal/adapter.rs index cae067521..7bb72e6be 100644 --- a/wgpu-hal/src/metal/adapter.rs +++ b/wgpu-hal/src/metal/adapter.rs @@ -1091,6 +1091,10 @@ impl super::PrivateCapabilities { F::SHADER_FLOAT32_ATOMIC, self.float_atomics && self.msl_version >= MTLLanguageVersion::Version3_0, ); + features.set( + F::MEMORY_DECORATION_COHERENT, + self.msl_version >= MTLLanguageVersion::Version3_2, + ); features.set( F::ADDRESS_MODE_CLAMP_TO_BORDER, diff --git a/wgpu-hal/src/metal/device.rs b/wgpu-hal/src/metal/device.rs index b3d70d2f0..870b050cc 100644 --- a/wgpu-hal/src/metal/device.rs +++ b/wgpu-hal/src/metal/device.rs @@ -177,8 +177,9 @@ impl super::Device { MTLLanguageVersion::Version2_4 => (2, 4), MTLLanguageVersion::Version3_0 => (3, 0), MTLLanguageVersion::Version3_1 => (3, 1), - // Newer version, fall back to 3.1 - _ => (3, 1), + MTLLanguageVersion::Version3_2 => (3, 2), + // Newer version, fall back to 3.2 + _ => (3, 2), }, inline_samplers: Default::default(), spirv_cross_compatibility: false, diff --git a/wgpu-hal/src/vulkan/adapter.rs b/wgpu-hal/src/vulkan/adapter.rs index 3e0eca274..d4b597899 100644 --- a/wgpu-hal/src/vulkan/adapter.rs +++ b/wgpu-hal/src/vulkan/adapter.rs @@ -652,7 +652,9 @@ impl PhysicalDeviceFeatures { | F::PIPELINE_CACHE | F::SHADER_EARLY_DEPTH_TEST | F::TEXTURE_ATOMIC - | F::PASSTHROUGH_SHADERS; + | F::PASSTHROUGH_SHADERS + | F::MEMORY_DECORATION_COHERENT + | F::MEMORY_DECORATION_VOLATILE; let mut dl_flags = Df::COMPUTE_SHADERS | Df::BASE_VERTEX diff --git a/wgpu-types/src/features.rs b/wgpu-types/src/features.rs index 6dac073b0..6db157de9 100644 --- a/wgpu-types/src/features.rs +++ b/wgpu-types/src/features.rs @@ -1319,6 +1319,26 @@ bitflags_array! { /// /// This is a native only feature. const ACCELERATION_STRUCTURE_BINDING_ARRAY = 1 << 60; + + /// Enables the `@coherent` memory decoration on storage buffer variables. + /// + /// Backend mapping: + /// - Vulkan + /// - DX12 + /// - Metal (3.2+) + /// - GLES (ES 3.1+ / GL 4.3+) + /// + /// This is a native only feature. + const MEMORY_DECORATION_COHERENT = 1 << 61; + + /// Enables the `@volatile` memory decoration on storage buffer variables. + /// + /// Backend mapping: + /// - Vulkan + /// - GLES (ES 3.1+ / GL 4.3+) + /// + /// This is a native only feature. + const MEMORY_DECORATION_VOLATILE = 1 << 62; } /// Features that are not guaranteed to be supported.