Skip to content

Commit aa9b462

Browse files
authored
[SPIRV] Fix layout rule for BufferPointer pointee type (#7956)
When picking the layout rule for the Pointee type of a BufferPointer, we are using the layout for the object containing the pointer, but that is incorrect. The layout for the PhysicalStorageBuffer should always follow the storage buffer layout rules. Fixes #7893
1 parent 917de5d commit aa9b462

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

tools/clang/lib/SPIRV/LowerTypeVisitor.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -836,12 +836,10 @@ const SpirvType *LowerTypeVisitor::lowerVkTypeInVkNamespace(
836836
}
837837

838838
QualType realType = hlsl::GetHLSLResourceTemplateParamType(type);
839-
if (rule == SpirvLayoutRule::Void) {
840-
rule = spvOptions.sBufferLayoutRule;
841-
}
842839
visitedTypeStack.push_back(type);
843840

844-
const SpirvType *spirvType = lowerType(realType, rule, llvm::None, srcLoc);
841+
const SpirvType *spirvType =
842+
lowerType(realType, spvOptions.sBufferLayoutRule, llvm::None, srcLoc);
845843
const auto *pointerType = spvContext.getPointerType(
846844
spirvType, spv::StorageClass::PhysicalStorageBuffer);
847845
spvContext.registerForwardReference(type, pointerType);
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// RUN: %dxc -spirv -E main -T cs_6_7 %s | FileCheck %s
2+
3+
4+
struct Foo {
5+
int val[4];
6+
};
7+
8+
// CHECK: OpDecorate [[ARRAY_INT_4:%[_0-9A-Za-z]*]] ArrayStride 4
9+
// CHECK: [[TYPE_FOO:%[_0-9A-Za-z]*]] = OpTypeStruct [[ARRAY_INT_4]]
10+
// CHECK: [[PTR_PHYS_BUF_FOO:%[_0-9A-Za-z]*]] = OpTypePointer PhysicalStorageBuffer [[TYPE_FOO]]
11+
// CHECK: [[TYPE_TEST_STRUCT:%[_0-9A-Za-z]*]] = OpTypeStruct [[PTR_PHYS_BUF_FOO]] %_ptr_PhysicalStorageBuffer_int
12+
// CHECK: [[PTR_UNIFORM_TYPE_TEST:%[_0-9A-Za-z]*]] = OpTypePointer Uniform [[TYPE_TEST_STRUCT]]
13+
// CHECK: [[TEST_VAR:%[_0-9A-Za-z]*]] = OpVariable [[PTR_UNIFORM_TYPE_TEST]] Uniform
14+
cbuffer Test {
15+
// The layout of `Foo` for the Buffer pointer should be the layout for
16+
// the storage buffer, even if the pointer is in a cbuffer.
17+
vk::BufferPointer<Foo> fooBuf;
18+
vk::BufferPointer<int> outBuf;
19+
};
20+
21+
22+
23+
[numthreads(256, 1, 1)]
24+
void main(in uint3 threadId : SV_DispatchThreadID) {
25+
// CHECK: [[FOO_BUF_ACCESS_CHAIN:%[_0-9A-Za-z]*]] = OpAccessChain %_ptr_Uniform__ptr_PhysicalStorageBuffer_Foo [[TEST_VAR]] %int_0
26+
// CHECK: [[FOO_BUF_LOAD:%[_0-9A-Za-z]*]] = OpLoad %_ptr_PhysicalStorageBuffer_Foo [[FOO_BUF_ACCESS_CHAIN]]
27+
int val = fooBuf.Get().val[0];
28+
outBuf.Get() = val;
29+
}

0 commit comments

Comments
 (0)