yum-mirror/slang

Making it easier to work with shaders

git clone https://git.yummers.dev/yum-mirror/slang

Yong HeEnhance buffer load specialization pass to specialize past field extracts. (#8547)e4611e2e3

master
1.8 KiB58 linesraw
1//TEST:SIMPLE(filecheck=CUDA): -target cuda -entry compute_main -stage compute
2//TEST:SIMPLE(filecheck=PTX): -target ptx -entry compute_main -stage compute
3
4//TEST:SIMPLE(filecheck=SPV): -target spirv
5
6// Check that we can specialize buffer loads through bindless handles, and
7// do not load big struct elements into registers unnecessarily.
8
9struct Bottom
10{
11    float bigArray[1024];
12    float bottomGetValue(int index) { return bigArray[index]; }
13}
14
15struct Middle
16{
17    Bottom bottom;
18    float middleGetValue(int index) { return bottom.bottomGetValue(index); }
19}
20
21struct Top
22{
23    StructuredBuffer<Middle>.Handle middle;
24
25    // Calling `middleGetValue` on `middle[0]` should not causing the entire `Middle`
26    // struct to be loaded into registers. Instead, we should be able to specialize
27    // `middleGetValue` to take a `StructuredBuffer<Middle>.Handle` and an `int`
28    // index, and recursively specialize `bottomGetValue` to only load the `Bottom.bigArray[index]` element.
29    float topGetValue(int index) { return middle[0].middleGetValue(index); }
30}
31
32struct Root
33{
34    Top top;
35}
36
37ConstantBuffer<Root> cb;
38
39RWStructuredBuffer<float> outputBuffer;
40
41// SPV: OpEntryPoint
42// SPV-NOT: OpLoad %Middle
43// SPV: %[[REG:[A-Za-z0-9_]+]] = OpLoad %float
44// SPV: OpStore {{.*}} %[[REG]]
45
46// Check that the generated CUDA code contains a specialized `bottomGetValue` function that has
47// the complete parameter list to access the `bigArray` element directly, without needing to load
48// the entire `Bottom` struct from the caller.
49//
50// CUDA-DAG: __device__ float Bottom_bottomGetValue{{.*}}(StructuredBuffer<Middle{{.*}}> {{.*}}, int {{.*}}, int {{.*}})
51// PTX: compute_main
52
53[shader("compute")]
54[numthreads(1, 1, 1)]
55void compute_main(uint3 tid: SV_DispatchThreadID)
56{
57    outputBuffer[0] = cb.top.topGetValue(0);
58}