Skip to content

Commit b867034

Browse files
csyongheslangbot
andauthored
[WGSL] Enable arbitrary arrays in uniform buffers. (shader-slang#5497)
* [WGSL] Enable arbitrary arrays in uniform buffers. * format code * Undo irrelevant change and fixups. * Update expected failure list. * Fix. * Rename. --------- Co-authored-by: slangbot <186143334+slangbot@users.noreply.github.com>
1 parent f829420 commit b867034

10 files changed

+326
-103
lines changed

source/slang/slang-emit.cpp

+4-1
Original file line numberDiff line numberDiff line change
@@ -1455,7 +1455,10 @@ Result linkAndOptimizeIR(
14551455
if (requiredLoweringPassSet.meshOutput)
14561456
legalizeMeshOutputTypes(irModule);
14571457

1458-
lowerBufferElementTypeToStorageType(targetProgram, irModule);
1458+
BufferElementTypeLoweringOptions bufferElementTypeLoweringOptions;
1459+
bufferElementTypeLoweringOptions.use16ByteArrayElementForConstantBuffer =
1460+
isWGPUTarget(targetRequest);
1461+
lowerBufferElementTypeToStorageType(targetProgram, irModule, bufferElementTypeLoweringOptions);
14591462

14601463
// Rewrite functions that return arrays to return them via `out` parameter,
14611464
// since our target languages doesn't allow returning arrays.

source/slang/slang-ir-insts.h

+1
Original file line numberDiff line numberDiff line change
@@ -3566,6 +3566,7 @@ struct IRBuilder
35663566
IRInst* replaceOperand(IRUse* use, IRInst* newValue);
35673567

35683568
IRInst* getBoolValue(bool value);
3569+
IRInst* getIntValue(IRIntegerValue value);
35693570
IRInst* getIntValue(IRType* type, IRIntegerValue value);
35703571
IRInst* getFloatValue(IRType* type, IRFloatingPointValue value);
35713572
IRStringLit* getStringValue(const UnownedStringSlice& slice);

source/slang/slang-ir-lower-buffer-element-type.cpp

+256-97
Large diffs are not rendered by default.

source/slang/slang-ir-lower-buffer-element-type.h

+9-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@ class TargetProgram;
88
struct IRTypeLayoutRules;
99
struct IRType;
1010

11+
struct BufferElementTypeLoweringOptions
12+
{
13+
bool lowerBufferPointer = false;
14+
15+
// For WGSL, we can only create arrays that has a stride of 16 bytes for constant buffers.
16+
bool use16ByteArrayElementForConstantBuffer = false;
17+
};
18+
1119
// For each struct type S used as element type of a ConstantBuffer, ParameterBlock or
1220
// [RW]StructuredBuffer, we create a lowered type L, where matrix types are lowered to arrays of
1321
// vectors based on major-ness, and loads from the buffer are converted to L_to_S(load(buffer)), and
@@ -18,7 +26,7 @@ struct IRType;
1826
void lowerBufferElementTypeToStorageType(
1927
TargetProgram* target,
2028
IRModule* module,
21-
bool lowerBufferPointer = false);
29+
BufferElementTypeLoweringOptions options = BufferElementTypeLoweringOptions());
2230

2331

2432
// Returns the type layout rules should be used for a buffer resource type.

source/slang/slang-ir-spirv-legalize.cpp

+6-1
Original file line numberDiff line numberDiff line change
@@ -2205,7 +2205,12 @@ struct SPIRVLegalizationContext : public SourceEmitterBase
22052205
// pointers in this pass. In the future we should consider separate out IRAddress as
22062206
// the type for IRVar, and use IRPtrType to dedicate pointers in user code, so we can
22072207
// safely lower the pointer load stores early together with other buffer types.
2208-
lowerBufferElementTypeToStorageType(m_sharedContext->m_targetProgram, m_module, true);
2208+
BufferElementTypeLoweringOptions bufferElementTypeLoweringOptions;
2209+
bufferElementTypeLoweringOptions.lowerBufferPointer = true;
2210+
lowerBufferElementTypeToStorageType(
2211+
m_sharedContext->m_targetProgram,
2212+
m_module,
2213+
bufferElementTypeLoweringOptions);
22092214

22102215
// The above step may produce empty struct types, so we need to lower them out of existence.
22112216
legalizeEmptyTypes(m_sharedContext->m_targetProgram, m_module, m_sink);

source/slang/slang-ir.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -2264,6 +2264,11 @@ IRInst* IRBuilder::getBoolValue(bool inValue)
22642264
return _findOrEmitConstant(keyInst);
22652265
}
22662266

2267+
IRInst* IRBuilder::getIntValue(IRIntegerValue value)
2268+
{
2269+
return getIntValue(getIntType(), value);
2270+
}
2271+
22672272
IRInst* IRBuilder::getIntValue(IRType* type, IRIntegerValue inValue)
22682273
{
22692274
IRConstant keyInst;

tests/compute/non-square-row-major.slang

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,13 @@
33
// Note! This test doesn't work on CUDA or CPU targets, because both these targets
44
// assume matrices are tightly packed, whereas GPU targets align rows to 16 bytes.
55

6+
//TEST(compute):COMPARE_COMPUTE_EX(filecheck-buffer=ALIGNED):-slang -compute -wgpu -output-using-type -xslang -matrix-layout-row-major -shaderobj
67
//TEST(compute):COMPARE_COMPUTE_EX(filecheck-buffer=PACKED):-cpu -compute -output-using-type -compile-arg -O3 -xslang -matrix-layout-row-major -shaderobj
78
//TEST(compute):COMPARE_COMPUTE_EX(filecheck-buffer=ALIGNED):-slang -compute -output-using-type -xslang -matrix-layout-row-major -shaderobj
89
//TEST(compute):COMPARE_COMPUTE_EX(filecheck-buffer=ALIGNED):-slang -compute -dx12 -output-using-type -xslang -matrix-layout-row-major -shaderobj
910
//TEST(compute, vulkan):COMPARE_COMPUTE_EX(filecheck-buffer=ALIGNED):-vk -compute -output-using-type -xslang -matrix-layout-row-major -shaderobj
1011
//TEST(compute):COMPARE_COMPUTE_EX(filecheck-buffer=PACKED):-cuda -compute -output-using-type -xslang -matrix-layout-row-major -shaderobj
1112
//TEST(compute):COMPARE_COMPUTE(filecheck-buffer=PACKED): -slang -output-using-type -shaderobj -mtl
12-
//DISABLE_TEST(compute):COMPARE_COMPUTE:-wgpu
13-
1413

1514
// matrix<R, C>
1615
//TEST_INPUT:cbuffer(data=[1.0 2.0 3.0 4.0 5.0 6.0 0.0 0.0 10.0 20.0 0.0 0.0 ]):name matrixBuffer

tests/expected-failure-github.txt

-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ tests/compute/interface-shader-param-in-struct.slang.4 syn (wgpu)
5555
tests/compute/interface-shader-param.slang.5 syn (wgpu)
5656
tests/compute/kernel-context-threading.slang.6 syn (wgpu)
5757
tests/compute/loop-unroll.slang.7 syn (wgpu)
58-
tests/compute/non-square-row-major.slang.6 syn (wgpu)
5958
tests/compute/parameter-block (wgpu)
6059
tests/compute/texture-get-dimensions (wgpu)
6160
tests/compute/texture-sampling (wgpu)

tests/wgsl/uniform-array-2.slang

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//TEST(compute):COMPARE_COMPUTE_EX(filecheck-buffer=CHECK):-slang -compute -wgpu -output-using-type
2+
3+
struct Params{
4+
int2 data[2];
5+
int scalarData[2];
6+
}
7+
8+
//TEST_INPUT:set params = cbuffer(data=[0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15])
9+
ConstantBuffer<Params> params;
10+
11+
//TEST_INPUT:set outputBuffer = out ubuffer(data=[0 0 0 0], stride=4)
12+
RWStructuredBuffer<float> outputBuffer;
13+
14+
float helper(float2 v)
15+
{return v.y;}
16+
17+
[numthreads(1,1,1)]
18+
void computeMain()
19+
{
20+
// CHECK: 17
21+
outputBuffer[0] = helper(params.data[1]) + params.scalarData[1];
22+
}
23+

tests/wgsl/uniform-array.slang

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//TEST(compute):COMPARE_COMPUTE_EX(filecheck-buffer=CHECK):-slang -compute -wgpu -output-using-type
2+
3+
struct MyPoint { int x; int y; int type; }
4+
5+
struct Params{
6+
MyPoint data[2];
7+
}
8+
9+
//TEST_INPUT:set params = cbuffer(data=[0 1 2 3 4 5 6 7 8 9 10 11])
10+
ConstantBuffer<Params> params;
11+
12+
//TEST_INPUT:set outputBuffer = out ubuffer(data=[0 0 0 0], stride=4)
13+
RWStructuredBuffer<float> outputBuffer;
14+
15+
[numthreads(1,1,1)]
16+
void computeMain()
17+
{
18+
// CHECK: 5
19+
outputBuffer[0] = params.data[1].y;
20+
}
21+

0 commit comments

Comments
 (0)