Skip to content

Commit 1450ea8

Browse files
authored
[spirv] Fix issues of RWByteAddressBuffer in the InitList (#3145)
RWByteAddressBuffer object in the initList should be handled like OpaqueType RWBuffer or Texture2D.
1 parent b793c33 commit 1450ea8

4 files changed

Lines changed: 34 additions & 9 deletions

File tree

tools/clang/lib/SPIRV/InitListHandler.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,9 @@ bool InitListHandler::tryToSplitStruct() {
130130
const QualType initType = init->getAstResultType();
131131
if (!initType->isStructureType() ||
132132
// Sampler types will pass the above check but we cannot split it.
133-
isSampler(initType))
133+
isSampler(initType) ||
134+
// Can not split structuredOrByteBuffer
135+
isAKindOfStructuredOrByteBuffer(initType))
134136
return false;
135137

136138
// We are certain the current intializer will be replaced by now.
@@ -214,12 +216,11 @@ SpirvInstruction *InitListHandler::createInitForType(QualType type,
214216
// Samplers, (RW)Buffers, (RW)Textures
215217
// It is important that this happens before checking of structure types.
216218
if (isOpaqueType(type))
217-
return createInitForSamplerImageType(type, srcLoc);
219+
return createInitForBufferOrImageType(type, srcLoc);
218220

219221
// This should happen before the check for normal struct types
220222
if (isAKindOfStructuredOrByteBuffer(type)) {
221-
emitError("cannot handle structured/byte buffer as initializer", srcLoc);
222-
return nullptr;
223+
return createInitForBufferOrImageType(type, srcLoc);
223224
}
224225

225226
if (type->isStructureType())
@@ -427,9 +428,9 @@ InitListHandler::createInitForConstantArrayType(QualType type,
427428
}
428429

429430
SpirvInstruction *
430-
InitListHandler::createInitForSamplerImageType(QualType type,
431-
SourceLocation srcLoc) {
432-
assert(isOpaqueType(type));
431+
InitListHandler::createInitForBufferOrImageType(QualType type,
432+
SourceLocation srcLoc) {
433+
assert(isOpaqueType(type) || isAKindOfStructuredOrByteBuffer(type));
433434

434435
// Samplers, (RW)Buffers, and (RW)Textures are translated into OpTypeSampler
435436
// and OpTypeImage. They should be treated similar as builtin types.

tools/clang/lib/SPIRV/InitListHandler.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,8 @@ class InitListHandler {
133133
SpirvInstruction *createInitForStructType(QualType type, SourceLocation);
134134
SpirvInstruction *createInitForConstantArrayType(QualType type,
135135
SourceLocation);
136-
SpirvInstruction *createInitForSamplerImageType(QualType type,
137-
SourceLocation);
136+
SpirvInstruction *createInitForBufferOrImageType(QualType type,
137+
SourceLocation);
138138

139139
private:
140140
const ASTContext &astContext;
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Run: %dxc -T cs_6_0 -E main
2+
RWBuffer<int> buffer1 : register(u1);
3+
RWByteAddressBuffer buffer2 : register(u2);
4+
5+
// CHECK: [[Resource:%\w+]] = OpTypeStruct %type_RWByteAddressBuffer %uint
6+
struct Resource {
7+
RWByteAddressBuffer rwbuffer;
8+
uint offset;
9+
};
10+
11+
[numthreads(8, 1, 1)]
12+
void main(uint globalId : SV_DispatchThreadID,
13+
uint localId : SV_GroupThreadID,
14+
uint groupId : SV_GroupID) {
15+
// CHECK: [[buffer2:%\w+]] = OpVariable %_ptr_Uniform_type_RWByteAddressBuffer Uniform
16+
Resource resourceInfo2 = {buffer2, 2};
17+
// CHECK: OpCompositeConstruct [[Resource]] [[buffer2]] %uint_2
18+
buffer1[0] = resourceInfo2.rwbuffer.Load(0);
19+
}

tools/clang/unittests/SPIRV/CodeGenSpirvTest.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -983,6 +983,11 @@ TEST_F(FileTest, RWByteAddressBufferAtomicMethods) {
983983
runFileTest("method.rw-byte-address-buffer.atomic.hlsl");
984984
}
985985

986+
TEST_F(FileTest, InitializeListRWByteAddressBuffer) {
987+
runFileTest("initializelist.rwbyteaddressbuffer.hlsl", Expect::Success,
988+
/* runValidation */ false);
989+
}
990+
986991
// For Buffer/RWBuffer methods
987992
TEST_F(FileTest, BufferLoad) { runFileTest("method.buffer.load.hlsl"); }
988993
TEST_F(FileTest, BufferGetDimensions) {

0 commit comments

Comments
 (0)