From 9caca7a17ba4637c77831e147cdafec6d5e7a403 Mon Sep 17 00:00:00 2001 From: Arcady Goldmints-Orlov Date: Thu, 18 May 2023 16:18:42 -0600 Subject: [PATCH] Add decorations to structs with buffer references The containsPhysicalStorageBufferOrArray function now handles struct types correctly, checking their contents recursively for buffer reference types. As a result, OpVariables containing structs that have members that are buffer references now have the appropriate AliasedPointer or RestrictPointer decoration as per the spec. Fixes #3188 --- SPIRV/SpvBuilder.cpp | 8 +- Test/baseResults/spv.bufferhandle8.frag.out | 117 ++++++++++++-------- Test/spv.bufferhandle8.frag | 2 + 3 files changed, 77 insertions(+), 50 deletions(-) diff --git a/SPIRV/SpvBuilder.cpp b/SPIRV/SpvBuilder.cpp index 9d0733425..8d0b677ba 100644 --- a/SPIRV/SpvBuilder.cpp +++ b/SPIRV/SpvBuilder.cpp @@ -1373,7 +1373,7 @@ bool Builder::containsType(Id typeId, spv::Op typeOp, unsigned int width) const } // return true if the type is a pointer to PhysicalStorageBufferEXT or an -// array of such pointers. These require restrict/aliased decorations. +// contains such a pointer. These require restrict/aliased decorations. bool Builder::containsPhysicalStorageBufferOrArray(Id typeId) const { const Instruction& instr = *module.getInstruction(typeId); @@ -1385,6 +1385,12 @@ bool Builder::containsPhysicalStorageBufferOrArray(Id typeId) const return getTypeStorageClass(typeId) == StorageClassPhysicalStorageBufferEXT; case OpTypeArray: return containsPhysicalStorageBufferOrArray(getContainedTypeId(typeId)); + case OpTypeStruct: + for (int m = 0; m < instr.getNumOperands(); ++m) { + if (containsPhysicalStorageBufferOrArray(instr.getIdOperand(m))) + return true; + } + return false; default: return false; } diff --git a/Test/baseResults/spv.bufferhandle8.frag.out b/Test/baseResults/spv.bufferhandle8.frag.out index 496014493..52eec1101 100644 --- a/Test/baseResults/spv.bufferhandle8.frag.out +++ b/Test/baseResults/spv.bufferhandle8.frag.out @@ -1,7 +1,7 @@ spv.bufferhandle8.frag // Module Version 10000 // Generated by (magic number): 8000b -// Id's are bound by 27 +// Id's are bound by 39 Capability Shader Capability PhysicalStorageBufferAddressesEXT @@ -21,41 +21,46 @@ spv.bufferhandle8.frag MemberName 10(T1) 0 "x" Name 11 "T2" MemberName 11(T2) 0 "x" - Name 13 "T3" - MemberName 13(T3) 0 "Bindings" - Name 15 "t3" - Name 23 "t2" - MemberName 23(t2) 0 "f" - MemberName 23(t2) 1 "g" - Name 24 "blockType" - MemberName 24(blockType) 0 "a" - MemberName 24(blockType) 1 "b" - MemberName 24(blockType) 2 "c" - MemberName 24(blockType) 3 "d" - MemberName 24(blockType) 4 "e" - Name 26 "t" - MemberDecorate 8(Blah) 0 Offset 0 - MemberDecorate 8(Blah) 1 Offset 8 + Name 13 "x" + Name 14 "Blah" + MemberName 14(Blah) 0 "t1" + MemberName 14(Blah) 1 "t2" + Name 16 "T3" + MemberName 16(T3) 0 "Bindings" + Name 18 "t3" + Name 35 "t2" + MemberName 35(t2) 0 "f" + MemberName 35(t2) 1 "g" + Name 36 "blockType" + MemberName 36(blockType) 0 "a" + MemberName 36(blockType) 1 "b" + MemberName 36(blockType) 2 "c" + MemberName 36(blockType) 3 "d" + MemberName 36(blockType) 4 "e" + Name 38 "t" MemberDecorate 10(T1) 0 Offset 0 Decorate 10(T1) Block MemberDecorate 11(T2) 0 Offset 0 Decorate 11(T2) Block - Decorate 12 ArrayStride 16 - MemberDecorate 13(T3) 0 Offset 0 - Decorate 13(T3) Block - Decorate 15(t3) DescriptorSet 0 - Decorate 15(t3) Binding 0 - MemberDecorate 23(t2) 0 Offset 0 - MemberDecorate 23(t2) 1 Offset 8 - Decorate 23(t2) Block - MemberDecorate 24(blockType) 0 Offset 0 - MemberDecorate 24(blockType) 1 Offset 4 - MemberDecorate 24(blockType) 2 Offset 8 - MemberDecorate 24(blockType) 3 Offset 12 - MemberDecorate 24(blockType) 4 Offset 16 - Decorate 24(blockType) Block - Decorate 26(t) DescriptorSet 0 - Decorate 26(t) Binding 0 + MemberDecorate 14(Blah) 0 Offset 0 + MemberDecorate 14(Blah) 1 Offset 8 + Decorate 15 ArrayStride 16 + MemberDecorate 16(T3) 0 Offset 0 + Decorate 16(T3) Block + Decorate 18(t3) DescriptorSet 0 + Decorate 18(t3) Binding 0 + MemberDecorate 35(t2) 0 Offset 0 + MemberDecorate 35(t2) 1 Offset 8 + Decorate 35(t2) Block + MemberDecorate 36(blockType) 0 Offset 0 + MemberDecorate 36(blockType) 1 Offset 4 + MemberDecorate 36(blockType) 2 Offset 8 + MemberDecorate 36(blockType) 3 Offset 12 + MemberDecorate 36(blockType) 4 Offset 16 + Decorate 36(blockType) Block + Decorate 38(t) DescriptorSet 0 + Decorate 38(t) Binding 0 + Decorate 13(x) DecorationAliasedPointerEXT 2: TypeVoid 3: TypeFunction 2 TypeForwardPointer 6 PhysicalStorageBufferEXT @@ -66,24 +71,38 @@ spv.bufferhandle8.frag 6: TypePointer PhysicalStorageBufferEXT 10(T1) 11(T2): TypeStruct 9(int) 7: TypePointer PhysicalStorageBufferEXT 11(T2) - 12: TypeRuntimeArray 8(Blah) - 13(T3): TypeStruct 12 - 14: TypePointer StorageBuffer 13(T3) - 15(t3): 14(ptr) Variable StorageBuffer - 16: 9(int) Constant 0 - 17: 9(int) Constant 1 - 18: TypePointer StorageBuffer 8(Blah) - TypeForwardPointer 22 PhysicalStorageBufferEXT - 23(t2): TypeStruct 22 22 - 24(blockType): TypeStruct 9(int) 9(int) 9(int) 9(int) 9(int) - 22: TypePointer PhysicalStorageBufferEXT 24(blockType) - 25: TypePointer StorageBuffer 23(t2) - 26(t): 25(ptr) Variable StorageBuffer + 12: TypePointer Function 8(Blah) + 14(Blah): TypeStruct 6(ptr) 7(ptr) + 15: TypeRuntimeArray 14(Blah) + 16(T3): TypeStruct 15 + 17: TypePointer StorageBuffer 16(T3) + 18(t3): 17(ptr) Variable StorageBuffer + 19: 9(int) Constant 0 + 20: 9(int) Constant 2 + 21: TypePointer StorageBuffer 14(Blah) + 25: TypePointer Function 6(ptr) + 28: 9(int) Constant 1 + 29: TypePointer Function 7(ptr) + TypeForwardPointer 34 PhysicalStorageBufferEXT + 35(t2): TypeStruct 34 34 + 36(blockType): TypeStruct 9(int) 9(int) 9(int) 9(int) 9(int) + 34: TypePointer PhysicalStorageBufferEXT 36(blockType) + 37: TypePointer StorageBuffer 35(t2) + 38(t): 37(ptr) Variable StorageBuffer 4(main): 2 Function None 3 5: Label - 19: 18(ptr) AccessChain 15(t3) 16 17 - 20: 8(Blah) Load 19 - 21: 18(ptr) AccessChain 15(t3) 16 16 - Store 21 20 + 13(x): 12(ptr) Variable Function + 22: 21(ptr) AccessChain 18(t3) 19 20 + 23: 14(Blah) Load 22 + 24: 6(ptr) CompositeExtract 23 0 + 26: 25(ptr) AccessChain 13(x) 19 + Store 26 24 + 27: 7(ptr) CompositeExtract 23 1 + 30: 29(ptr) AccessChain 13(x) 28 + Store 30 27 + 31: 21(ptr) AccessChain 18(t3) 19 28 + 32: 14(Blah) Load 31 + 33: 21(ptr) AccessChain 18(t3) 19 19 + Store 33 32 Return FunctionEnd diff --git a/Test/spv.bufferhandle8.frag b/Test/spv.bufferhandle8.frag index 1bc13c3cf..92651355c 100644 --- a/Test/spv.bufferhandle8.frag +++ b/Test/spv.bufferhandle8.frag @@ -27,6 +27,8 @@ layout(set=0, binding=0) buffer T3 { Blah Bindings[]; } t3; + void main() { + Blah x = t3.Bindings[2]; t3.Bindings[0] = t3.Bindings[1]; }