Skip to content

Commit 6c9592f

Browse files
authored
Enable the vk::counter_binding attribute for arrays (#5456)
The `vk::counter_binding` binding attribute is defined to only apply to scalars because until recently arrays of RWStructuredBuffers and the like could not be arrays. That has changed, so the attribute can apply to the arrays of RWStructuredBuffers.
1 parent 0033aa8 commit 6c9592f

3 files changed

Lines changed: 47 additions & 1 deletion

File tree

tools/clang/include/clang/Basic/Attr.td

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -943,6 +943,19 @@ def CounterStructuredBuffer : SubsetSubject<
943943
S->getType()->getAs<RecordType>()->getDecl()->getName() == "AppendStructuredBuffer" ||
944944
S->getType()->getAs<RecordType>()->getDecl()->getName() == "ConsumeStructuredBuffer")}]>;
945945

946+
// Array of StructuredBuffer types that can have associated counters
947+
def ArrayOfCounterStructuredBuffer
948+
: SubsetSubject<
949+
Var, [{S->hasGlobalStorage() && S->getType()->getAsArrayTypeUnsafe() &&
950+
S->getType()->getAsArrayTypeUnsafe()->getElementType()->getAs<RecordType>() &&
951+
S->getType()->getAsArrayTypeUnsafe()->getElementType()->getAs<RecordType>()->getDecl() &&
952+
(S->getType()->getAsArrayTypeUnsafe()->getElementType()->getAs<RecordType>()->getDecl()->getName() ==
953+
"RWStructuredBuffer" ||
954+
S->getType()->getAsArrayTypeUnsafe()->getElementType()->getAs<RecordType>()->getDecl()->getName() ==
955+
"AppendStructuredBuffer" ||
956+
S->getType()->getAsArrayTypeUnsafe()->getElementType()->getAs<RecordType>()->getDecl()->getName() ==
957+
"ConsumeStructuredBuffer")}]>;
958+
946959
// Global variable with "ConstantBuffer" type
947960
def ConstantBuffer
948961
: SubsetSubject<
@@ -1067,7 +1080,7 @@ def VKCapabilityExt : InheritableAttr {
10671080

10681081
def VKCounterBinding : InheritableAttr {
10691082
let Spellings = [CXX11<"vk", "counter_binding">];
1070-
let Subjects = SubjectList<[CounterStructuredBuffer], ErrorDiag, "ExpectedCounterStructuredBuffer">;
1083+
let Subjects = SubjectList<[ArrayOfCounterStructuredBuffer, CounterStructuredBuffer], ErrorDiag, "ExpectedCounterStructuredBuffer">;
10711084
let Args = [IntArgument<"Binding">];
10721085
let LangOpts = [SPIRV];
10731086
let Documentation = [Undocumented];
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// RUN: %dxc -T ps_6_6 -E main -fvk-allow-rwstructuredbuffer-arrays
2+
3+
struct PSInput
4+
{
5+
uint idx : COLOR;
6+
};
7+
8+
// CHECK: OpDecorate %g_rwbuffer DescriptorSet 4
9+
// CHECK: OpDecorate %g_rwbuffer Binding 3
10+
// CHECK: OpDecorate %counter_var_g_rwbuffer DescriptorSet 4
11+
// CHECK: OpDecorate %counter_var_g_rwbuffer Binding 4
12+
13+
// CHECK: %g_rwbuffer = OpVariable %_ptr_Uniform__arr_type_RWStructuredBuffer_uint_uint_5 Uniform
14+
// CHECK: %counter_var_g_rwbuffer = OpVariable %_ptr_Uniform__arr_type_ACSBuffer_counter_uint_5 Uniform
15+
[[vk::binding(3,4), vk::counter_binding(4)]] RWStructuredBuffer<uint> g_rwbuffer[5] : register(u0, space2);
16+
17+
float4 main(PSInput input) : SV_TARGET
18+
{
19+
// Correctly increment the counter.
20+
// CHECK: [[ac1:%\w+]] = OpAccessChain %_ptr_Uniform_type_ACSBuffer_counter %counter_var_g_rwbuffer {{%\d+}}
21+
// CHECK: [[ac2:%\w+]] = OpAccessChain %_ptr_Uniform_int [[ac1]] %uint_0
22+
// CHECK: OpAtomicIAdd %int [[ac2]] %uint_1 %uint_0 %int_1
23+
g_rwbuffer[input.idx].IncrementCounter();
24+
25+
// Correctly access the buffer.
26+
// CHECK: [[ac1:%\w+]] = OpAccessChain %_ptr_Uniform_type_RWStructuredBuffer_uint %g_rwbuffer {{%\d+}}
27+
// CHECK: [[ac2:%\w+]] = OpAccessChain %_ptr_Uniform_uint [[ac1]] %int_0 %uint_0
28+
// CHECK: OpLoad %uint [[ac2]]
29+
return g_rwbuffer[input.idx][0];
30+
}

tools/clang/unittests/SPIRV/CodeGenSpirvTest.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,9 @@ TEST_F(FileTest, RWStructuredBufferArrayCounterFlattened) {
164164
TEST_F(FileTest, RWStructuredBufferArrayCounterIndirect) {
165165
runFileTest("type.rwstructured-buffer.array.counter.indirect.hlsl");
166166
}
167+
TEST_F(FileTest, RWStructuredBufferArrayBindAttributes) {
168+
runFileTest("type.rwstructured-buffer.array.binding.attributes.hlsl");
169+
}
167170
TEST_F(FileTest, AppendStructuredBufferArrayError) {
168171
runFileTest("type.append-structured-buffer.array.hlsl");
169172
}

0 commit comments

Comments
 (0)