From 76b16050905754bb29d059bb0d6aeaa13c7fa77a Mon Sep 17 00:00:00 2001 From: Schell Carl Scivally Date: Sat, 12 Oct 2024 07:24:16 +1300 Subject: [PATCH] [spv-in] sampling from depth textures returns a scalar and is splatted (#6384) --- CHANGELOG.md | 1 + naga/src/front/spv/image.rs | 74 ++++-- naga/src/proc/mod.rs | 4 + naga/tests/in/spv/fetch_depth.spv | Bin 0 -> 772 bytes naga/tests/in/spv/fetch_depth.spvasm | 52 +++++ naga/tests/out/hlsl/fetch_depth.hlsl | 25 ++ naga/tests/out/hlsl/fetch_depth.ron | 12 + naga/tests/out/ir/fetch_depth.compact.ron | 195 ++++++++++++++++ naga/tests/out/ir/fetch_depth.ron | 265 ++++++++++++++++++++++ naga/tests/out/msl/fetch_depth.msl | 31 +++ naga/tests/out/spv/fetch_depth.spvasm | 74 ++++++ naga/tests/out/wgsl/fetch_depth.wgsl | 26 +++ naga/tests/snapshots.rs | 6 +- 13 files changed, 744 insertions(+), 21 deletions(-) create mode 100644 naga/tests/in/spv/fetch_depth.spv create mode 100644 naga/tests/in/spv/fetch_depth.spvasm create mode 100644 naga/tests/out/hlsl/fetch_depth.hlsl create mode 100644 naga/tests/out/hlsl/fetch_depth.ron create mode 100644 naga/tests/out/ir/fetch_depth.compact.ron create mode 100644 naga/tests/out/ir/fetch_depth.ron create mode 100644 naga/tests/out/msl/fetch_depth.msl create mode 100644 naga/tests/out/spv/fetch_depth.spvasm create mode 100644 naga/tests/out/wgsl/fetch_depth.wgsl diff --git a/CHANGELOG.md b/CHANGELOG.md index 28392f582..65ea26072 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -104,6 +104,7 @@ By @bradwerth [#6216](https://github.com/gfx-rs/wgpu/pull/6216). #### Naga +- SPIR-V frontend splats depth texture sample and load results. Fixes [issue #4551](https://github.com/gfx-rs/wgpu/issues/4551). By @schell in [#6384](https://github.com/gfx-rs/wgpu/pull/6384). - Accept only `vec3` (not `vecN`) for the `cross` built-in. By @ErichDonGubler in [#6171](https://github.com/gfx-rs/wgpu/pull/6171). - Configure `SourceLanguage` when enabling debug info in SPV-out. By @kvark in [#6256](https://github.com/gfx-rs/wgpu/pull/6256). - Per-polygon and flat inputs should not be considered subgroup uniform. By @magcius in [#6276](https://github.com/gfx-rs/wgpu/pull/6276). diff --git a/naga/src/front/spv/image.rs b/naga/src/front/spv/image.rs index 284c4cf7f..71ba79e1e 100644 --- a/naga/src/front/spv/image.rs +++ b/naga/src/front/spv/image.rs @@ -377,36 +377,61 @@ impl> super::Frontend { let coord_handle = self.get_expr_handle(coordinate_id, coord_lexp, ctx, emitter, block, body_idx); let coord_type_handle = self.lookup_type.lookup(coord_lexp.type_id)?.handle; - let (coordinate, array_index) = match ctx.type_arena[image_ty].inner { + let (coordinate, array_index, is_depth) = match ctx.type_arena[image_ty].inner { crate::TypeInner::Image { dim, arrayed, - class: _, - } => extract_image_coordinates( - dim, - if arrayed { - ExtraCoordinate::ArrayLayer - } else { - ExtraCoordinate::Garbage - }, - coord_handle, - coord_type_handle, - ctx, - ), + class, + } => { + let (coord, array_index) = extract_image_coordinates( + dim, + if arrayed { + ExtraCoordinate::ArrayLayer + } else { + ExtraCoordinate::Garbage + }, + coord_handle, + coord_type_handle, + ctx, + ); + (coord, array_index, class.is_depth()) + } _ => return Err(Error::InvalidImage(image_ty)), }; - let expr = crate::Expression::ImageLoad { + let image_load_expr = crate::Expression::ImageLoad { image: image_lexp.handle, coordinate, array_index, sample, level, }; + let image_load_handle = ctx + .expressions + .append(image_load_expr, self.span_from_with_op(start)); + + let handle = if is_depth { + let result_ty = self.lookup_type.lookup(result_type_id)?; + // The return type of `OpImageRead` can be a scalar or vector. + match ctx.type_arena[result_ty.handle].inner { + crate::TypeInner::Vector { size, .. } => { + let splat_expr = crate::Expression::Splat { + size, + value: image_load_handle, + }; + ctx.expressions + .append(splat_expr, self.span_from_with_op(start)) + } + _ => image_load_handle, + } + } else { + image_load_handle + }; + self.lookup_expression.insert( result_id, LookupExpression { - handle: ctx.expressions.append(expr, self.span_from_with_op(start)), + handle, type_id: result_type_id, block_id, }, @@ -593,11 +618,12 @@ impl> super::Frontend { ref other => return Err(Error::InvalidGlobalVar(other.clone())), } - let ((coordinate, array_index), depth_ref) = match ctx.type_arena[image_ty].inner { + let ((coordinate, array_index), depth_ref, is_depth) = match ctx.type_arena[image_ty].inner + { crate::TypeInner::Image { dim, arrayed, - class: _, + class, } => ( extract_image_coordinates( dim, @@ -642,6 +668,7 @@ impl> super::Frontend { None => None, } }, + class.is_depth(), ), _ => return Err(Error::InvalidImage(image_ty)), }; @@ -656,10 +683,21 @@ impl> super::Frontend { level, depth_ref, }; + let image_sample_handle = ctx.expressions.append(expr, self.span_from_with_op(start)); + let handle = if is_depth && depth_ref.is_none() { + let splat_expr = crate::Expression::Splat { + size: crate::VectorSize::Quad, + value: image_sample_handle, + }; + ctx.expressions + .append(splat_expr, self.span_from_with_op(start)) + } else { + image_sample_handle + }; self.lookup_expression.insert( result_id, LookupExpression { - handle: ctx.expressions.append(expr, self.span_from_with_op(start)), + handle, type_id: result_type_id, block_id, }, diff --git a/naga/src/proc/mod.rs b/naga/src/proc/mod.rs index abbe0c7e4..b7a08bc8a 100644 --- a/naga/src/proc/mod.rs +++ b/naga/src/proc/mod.rs @@ -622,6 +622,10 @@ impl super::ImageClass { crate::ImageClass::Storage { .. } => false, } } + + pub const fn is_depth(self) -> bool { + matches!(self, crate::ImageClass::Depth { .. }) + } } impl crate::Module { diff --git a/naga/tests/in/spv/fetch_depth.spv b/naga/tests/in/spv/fetch_depth.spv new file mode 100644 index 0000000000000000000000000000000000000000..4b9915618d3f4d629c6d66cba1427190a321f809 GIT binary patch literal 772 zcmYL`%T5A85JiibMj|K!L3wCmdL&`v9tlPlZe;5hus{e47B29E{R=lH-qR1Mq^2IX z`re-EvY584*)X%p7W!g+D~t~D`^v0qW$5#*tYb|@@XgoZuw3r8$IX8IvHd*m)hp!8 zIeE{{5|5cz%^Cbn6KzFiS7IZ_rZEJ(qdeo}*a>$5t7BX9vA+MW%W#LWYB27@Y}AhR zY1W0if}_VgchC5>steU#jt#|jsp#b+VO>RP&J4s*Db||lDUv^=^L^zQd;>) global_2 : register(t2); + +void function() +{ + uint2 _e6 = asuint(global_1.Load2(0)); + float _e7 = global_2.Load(int3(_e6, 0)).x; + global.Store(0, asuint((_e7).xxxx.x)); + return; +} + +[numthreads(32, 1, 1)] +void cullfetch_depth() +{ + function(); +} diff --git a/naga/tests/out/hlsl/fetch_depth.ron b/naga/tests/out/hlsl/fetch_depth.ron new file mode 100644 index 000000000..cb8a85c5f --- /dev/null +++ b/naga/tests/out/hlsl/fetch_depth.ron @@ -0,0 +1,12 @@ +( + vertex:[ + ], + fragment:[ + ], + compute:[ + ( + entry_point:"cullfetch_depth", + target_profile:"cs_5_1", + ), + ], +) diff --git a/naga/tests/out/ir/fetch_depth.compact.ron b/naga/tests/out/ir/fetch_depth.compact.ron new file mode 100644 index 000000000..dc5ecf08a --- /dev/null +++ b/naga/tests/out/ir/fetch_depth.compact.ron @@ -0,0 +1,195 @@ +( + types: [ + ( + name: None, + inner: Scalar(( + kind: Float, + width: 4, + )), + ), + ( + name: None, + inner: Scalar(( + kind: Sint, + width: 4, + )), + ), + ( + name: None, + inner: Struct( + members: [ + ( + name: None, + ty: 0, + binding: None, + offset: 0, + ), + ], + span: 4, + ), + ), + ( + name: None, + inner: Vector( + size: Bi, + scalar: ( + kind: Uint, + width: 4, + ), + ), + ), + ( + name: None, + inner: Struct( + members: [ + ( + name: None, + ty: 3, + binding: None, + offset: 0, + ), + ], + span: 8, + ), + ), + ( + name: None, + inner: Image( + dim: D2, + arrayed: false, + class: Depth( + multi: false, + ), + ), + ), + ], + special_types: ( + ray_desc: None, + ray_intersection: None, + predeclared_types: {}, + ), + constants: [ + ( + name: None, + ty: 1, + init: 0, + ), + ], + overrides: [], + global_variables: [ + ( + name: None, + space: Storage( + access: ("LOAD | STORE"), + ), + binding: Some(( + group: 0, + binding: 0, + )), + ty: 2, + init: None, + ), + ( + name: None, + space: Storage( + access: ("LOAD"), + ), + binding: Some(( + group: 0, + binding: 1, + )), + ty: 4, + init: None, + ), + ( + name: None, + space: Handle, + binding: Some(( + group: 0, + binding: 2, + )), + ty: 5, + init: None, + ), + ], + global_expressions: [ + Literal(I32(0)), + ], + functions: [ + ( + name: None, + arguments: [], + result: None, + local_variables: [], + expressions: [ + GlobalVariable(2), + GlobalVariable(0), + GlobalVariable(1), + Constant(0), + AccessIndex( + base: 1, + index: 0, + ), + AccessIndex( + base: 2, + index: 0, + ), + Load( + pointer: 5, + ), + ImageLoad( + image: 0, + coordinate: 6, + array_index: None, + sample: None, + level: Some(3), + ), + Splat( + size: Quad, + value: 7, + ), + AccessIndex( + base: 8, + index: 0, + ), + ], + named_expressions: {}, + body: [ + Emit(( + start: 4, + end: 10, + )), + Store( + pointer: 4, + value: 9, + ), + Return( + value: None, + ), + ], + ), + ], + entry_points: [ + ( + name: "cull::fetch_depth", + stage: Compute, + early_depth_test: None, + workgroup_size: (32, 1, 1), + function: ( + name: Some("cull::fetch_depth_wrap"), + arguments: [], + result: None, + local_variables: [], + expressions: [], + named_expressions: {}, + body: [ + Call( + function: 0, + arguments: [], + result: None, + ), + ], + ), + ), + ], +) \ No newline at end of file diff --git a/naga/tests/out/ir/fetch_depth.ron b/naga/tests/out/ir/fetch_depth.ron new file mode 100644 index 000000000..210a9ee26 --- /dev/null +++ b/naga/tests/out/ir/fetch_depth.ron @@ -0,0 +1,265 @@ +( + types: [ + ( + name: None, + inner: Scalar(( + kind: Uint, + width: 4, + )), + ), + ( + name: None, + inner: Scalar(( + kind: Float, + width: 4, + )), + ), + ( + name: None, + inner: Vector( + size: Quad, + scalar: ( + kind: Float, + width: 4, + ), + ), + ), + ( + name: None, + inner: Scalar(( + kind: Sint, + width: 4, + )), + ), + ( + name: None, + inner: Struct( + members: [ + ( + name: None, + ty: 1, + binding: None, + offset: 0, + ), + ], + span: 4, + ), + ), + ( + name: None, + inner: Pointer( + base: 4, + space: Storage( + access: ("LOAD | STORE"), + ), + ), + ), + ( + name: None, + inner: Vector( + size: Bi, + scalar: ( + kind: Uint, + width: 4, + ), + ), + ), + ( + name: None, + inner: Struct( + members: [ + ( + name: None, + ty: 6, + binding: None, + offset: 0, + ), + ], + span: 8, + ), + ), + ( + name: None, + inner: Pointer( + base: 7, + space: Storage( + access: ("LOAD | STORE"), + ), + ), + ), + ( + name: None, + inner: Vector( + size: Bi, + scalar: ( + kind: Float, + width: 4, + ), + ), + ), + ( + name: None, + inner: Image( + dim: D2, + arrayed: false, + class: Depth( + multi: false, + ), + ), + ), + ( + name: None, + inner: Pointer( + base: 1, + space: Storage( + access: ("LOAD | STORE"), + ), + ), + ), + ( + name: None, + inner: Pointer( + base: 6, + space: Storage( + access: ("LOAD | STORE"), + ), + ), + ), + ], + special_types: ( + ray_desc: None, + ray_intersection: None, + predeclared_types: {}, + ), + constants: [ + ( + name: None, + ty: 0, + init: 0, + ), + ( + name: None, + ty: 3, + init: 1, + ), + ], + overrides: [], + global_variables: [ + ( + name: None, + space: Storage( + access: ("LOAD | STORE"), + ), + binding: Some(( + group: 0, + binding: 0, + )), + ty: 4, + init: None, + ), + ( + name: None, + space: Storage( + access: ("LOAD"), + ), + binding: Some(( + group: 0, + binding: 1, + )), + ty: 7, + init: None, + ), + ( + name: None, + space: Handle, + binding: Some(( + group: 0, + binding: 2, + )), + ty: 10, + init: None, + ), + ], + global_expressions: [ + Literal(U32(0)), + Literal(I32(0)), + ], + functions: [ + ( + name: None, + arguments: [], + result: None, + local_variables: [], + expressions: [ + GlobalVariable(2), + GlobalVariable(0), + GlobalVariable(1), + Constant(1), + Constant(0), + AccessIndex( + base: 1, + index: 0, + ), + AccessIndex( + base: 2, + index: 0, + ), + Load( + pointer: 6, + ), + ImageLoad( + image: 0, + coordinate: 7, + array_index: None, + sample: None, + level: Some(3), + ), + Splat( + size: Quad, + value: 8, + ), + AccessIndex( + base: 9, + index: 0, + ), + ], + named_expressions: {}, + body: [ + Emit(( + start: 5, + end: 11, + )), + Store( + pointer: 5, + value: 10, + ), + Return( + value: None, + ), + ], + ), + ], + entry_points: [ + ( + name: "cull::fetch_depth", + stage: Compute, + early_depth_test: None, + workgroup_size: (32, 1, 1), + function: ( + name: Some("cull::fetch_depth_wrap"), + arguments: [], + result: None, + local_variables: [], + expressions: [], + named_expressions: {}, + body: [ + Call( + function: 0, + arguments: [], + result: None, + ), + ], + ), + ), + ], +) \ No newline at end of file diff --git a/naga/tests/out/msl/fetch_depth.msl b/naga/tests/out/msl/fetch_depth.msl new file mode 100644 index 000000000..867a28437 --- /dev/null +++ b/naga/tests/out/msl/fetch_depth.msl @@ -0,0 +1,31 @@ +// language: metal1.0 +#include +#include + +using metal::uint; + +struct type_2 { + float member; +}; +struct type_4 { + metal::uint2 member; +}; + +void function( + device type_2& global, + device type_4 const& global_1, + metal::depth2d global_2 +) { + metal::uint2 _e6 = global_1.member; + float _e7 = global_2.read(metal::uint2(_e6), 0); + global.member = metal::float4(_e7).x; + return; +} + +kernel void cullfetch_depth( + device type_2& global [[user(fake0)]] +, device type_4 const& global_1 [[user(fake0)]] +, metal::depth2d global_2 [[user(fake0)]] +) { + function(global, global_1, global_2); +} diff --git a/naga/tests/out/spv/fetch_depth.spvasm b/naga/tests/out/spv/fetch_depth.spvasm new file mode 100644 index 000000000..54aa5eb1c --- /dev/null +++ b/naga/tests/out/spv/fetch_depth.spvasm @@ -0,0 +1,74 @@ +; SPIR-V +; Version: 1.1 +; Generator: rspirv +; Bound: 46 +OpCapability Shader +OpExtension "SPV_KHR_storage_buffer_storage_class" +%1 = OpExtInstImport "GLSL.std.450" +OpMemoryModel Logical GLSL450 +OpEntryPoint GLCompute %40 "cull::fetch_depth" +OpExecutionMode %40 LocalSize 32 1 1 +OpMemberDecorate %5 0 Offset 0 +OpMemberDecorate %8 0 Offset 0 +OpDecorate %11 DescriptorSet 0 +OpDecorate %11 Binding 0 +OpDecorate %12 Block +OpMemberDecorate %12 0 Offset 0 +OpDecorate %14 NonWritable +OpDecorate %14 DescriptorSet 0 +OpDecorate %14 Binding 1 +OpDecorate %15 Block +OpMemberDecorate %15 0 Offset 0 +OpDecorate %17 DescriptorSet 0 +OpDecorate %17 Binding 2 +%2 = OpTypeVoid +%3 = OpTypeFloat 32 +%4 = OpTypeInt 32 1 +%5 = OpTypeStruct %3 +%7 = OpTypeInt 32 0 +%6 = OpTypeVector %7 2 +%8 = OpTypeStruct %6 +%9 = OpTypeImage %3 2D 1 0 0 1 Unknown +%10 = OpConstant %4 0 +%12 = OpTypeStruct %5 +%13 = OpTypePointer StorageBuffer %12 +%11 = OpVariable %13 StorageBuffer +%15 = OpTypeStruct %8 +%16 = OpTypePointer StorageBuffer %15 +%14 = OpVariable %16 StorageBuffer +%18 = OpTypePointer UniformConstant %9 +%17 = OpVariable %18 UniformConstant +%21 = OpTypeFunction %2 +%22 = OpTypePointer StorageBuffer %5 +%23 = OpConstant %7 0 +%25 = OpTypePointer StorageBuffer %8 +%29 = OpTypePointer StorageBuffer %3 +%30 = OpTypePointer StorageBuffer %6 +%33 = OpTypeVector %3 4 +%20 = OpFunction %2 None %21 +%19 = OpLabel +%24 = OpAccessChain %22 %11 %23 +%26 = OpAccessChain %25 %14 %23 +%27 = OpLoad %9 %17 +OpBranch %28 +%28 = OpLabel +%31 = OpAccessChain %30 %26 %23 +%32 = OpLoad %6 %31 +%34 = OpImageFetch %33 %27 %32 Lod %10 +%35 = OpCompositeExtract %3 %34 0 +%36 = OpCompositeConstruct %33 %35 %35 %35 %35 +%37 = OpCompositeExtract %3 %36 0 +%38 = OpAccessChain %29 %24 %23 +OpStore %38 %37 +OpReturn +OpFunctionEnd +%40 = OpFunction %2 None %21 +%39 = OpLabel +%41 = OpAccessChain %22 %11 %23 +%42 = OpAccessChain %25 %14 %23 +%43 = OpLoad %9 %17 +OpBranch %44 +%44 = OpLabel +%45 = OpFunctionCall %2 %20 +OpReturn +OpFunctionEnd \ No newline at end of file diff --git a/naga/tests/out/wgsl/fetch_depth.wgsl b/naga/tests/out/wgsl/fetch_depth.wgsl new file mode 100644 index 000000000..3e206bc26 --- /dev/null +++ b/naga/tests/out/wgsl/fetch_depth.wgsl @@ -0,0 +1,26 @@ +struct type_2 { + member: f32, +} + +struct type_4 { + member: vec2, +} + +@group(0) @binding(0) +var global: type_2; +@group(0) @binding(1) +var global_1: type_4; +@group(0) @binding(2) +var global_2: texture_depth_2d; + +fn function() { + let _e6 = global_1.member; + let _e7 = textureLoad(global_2, _e6, 0i); + global.member = vec4(_e7).x; + return; +} + +@compute @workgroup_size(32, 1, 1) +fn cullfetch_depth() { + function(); +} diff --git a/naga/tests/snapshots.rs b/naga/tests/snapshots.rs index 9b9ff1593..244c9f506 100644 --- a/naga/tests/snapshots.rs +++ b/naga/tests/snapshots.rs @@ -1063,11 +1063,11 @@ fn convert_spv_all() { false, Targets::METAL | Targets::GLSL | Targets::HLSL | Targets::WGSL, ); + convert_spv("atomic_i_increment", false, Targets::IR); convert_spv( - "atomic_i_increment", + "fetch_depth", false, - // TODO(@schell): remove Targets::NO_VALIDATION when OpAtomicIIncrement lands - Targets::IR | Targets::NO_VALIDATION, + Targets::IR | Targets::SPIRV | Targets::METAL | Targets::HLSL | Targets::WGSL, ); }