yum-mirror/slang

Making it easier to work with shaders

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

Yong HeFix legalization crash when processing metal parameter blocks. (#8591)6a2cf239a

master
4.0 KiB143 linesraw
1//TEST:SIMPLE(filecheck=CHECK): -stage compute -entry computeMain -target metal
2//TEST:SIMPLE(filecheck=CHECK-ASM): -stage compute -entry computeMain -target metallib
3
4// This test verifies that resource types within structs and arrays
5// are preserved in Metal. This is done by lowering resource tyeps into
6// descriptor handle types during lower-buffer-element-type pass.
7
8struct Val
9{
10    RWStructuredBuffer<float> buffer;
11}
12
13struct Val_f
14{
15    float f;
16}
17
18struct ResourceStruct {
19    RWStructuredBuffer<float> buffer;
20    Texture2D<float4> texture;
21}
22
23struct NestedStruct {
24    Array<ResourceStruct, 2> resources;
25    ResourceStruct resource;
26    int padding;
27}
28
29struct ComplexStruct {
30    Array<NestedStruct, 3> nested;
31    RWStructuredBuffer<float> directBuffer;
32}
33
34struct ParentStruct {
35    struct ChildStruct {
36        Texture2D<float> texture;
37        int i;
38        float f;
39    } c;
40
41    Array<Texture2D<float>, 2> tex_array;
42}
43
44struct OtherParent {
45      ParentStruct::ChildStruct c2;
46}
47
48struct StructWithParameterBlock {
49    RWStructuredBuffer<float> buffer;
50    ParameterBlock<Array<Val, 2>> arr;
51}
52
53uniform StructWithParameterBlock struct_with_parameter_block;
54
55
56// Test various parameter block configurations
57ParameterBlock<ResourceStruct> simpleBlock;
58ParameterBlock<Array<ResourceStruct, 4>> arrayBlock; 
59ParameterBlock<NestedStruct> nestedBlock;
60ParameterBlock<ComplexStruct> complexBlock;
61ParameterBlock<Array<ComplexStruct, 2>> arrayComplexBlock;
62ParameterBlock<ParentStruct> parentBlock;
63
64ParameterBlock<RWStructuredBuffer<Val_f>> valBlock;
65
66
67RWStructuredBuffer<float> outputBuffer;
68
69void func(ParentStruct::ChildStruct c) {
70    outputBuffer[0] = c.texture.Load(int3(0));
71    Texture2D<float> tex = c.texture;
72    outputBuffer[0] = tex.Load(int3(0));
73
74}
75
76void func2(ParentStruct p) {
77    Array<Texture2D<float>, 2> tex_array_local = p.tex_array;
78    outputBuffer[1] = tex_array_local[0].Load(int3(0));
79    outputBuffer[2] = p.c.texture.Load(int3(3));
80}
81
82void func3(Array<Texture2D<float>, 2> arr) {
83    outputBuffer[3] = arr[0].Load(int3(0));
84}
85
86void func4(ParameterBlock<ParentStruct> block) {
87    ParentStruct p = block;
88    outputBuffer[8] = p.c.texture.Load(int3(0));
89}
90
91// CHECK-ASM: {{.*}} @computeMain
92[numthreads(1, 1, 1)]
93void computeMain(uint3 tid: SV_DispatchThreadID)
94{
95
96    // CHECK: {{.*}}->buffer{{.*}}
97    outputBuffer[0] = simpleBlock.buffer[0];
98    
99    // CHECK: arrayBlock{{.*}}->data{{.*}}[int(1)].buffer
100    ResourceStruct fromArray = arrayBlock[1];
101    outputBuffer[1] = fromArray.buffer[1];
102        
103    // CHECK: complexBlock{{.*}}->nested{{.*}}->data{{.*}}[int(1)])->resources{{.*}})->data{{.*}}[int(1)])->buffer{{.*}}
104    outputBuffer[3] = complexBlock.nested[1].resources[1].buffer[3];
105    
106    // CHECK: {{.*}}arrayComplexBlock{{.*}}->data{{.*}}[int(0)])->nested{{.*}})->data{{.*}}[int(2)])->resources{{.*}})->data{{.*}}[int(0)].buffer
107    ComplexStruct complexFromArray = arrayComplexBlock[0];
108    outputBuffer[4] = complexFromArray.nested[2].resources[0].buffer[4];
109    
110    // CHECK: {{.*}}->directBuffer{{.*}}
111    outputBuffer[5] = complexBlock.directBuffer[5];
112    
113    // Test parameter block accessed via a local variable
114    // CHECK: nestedBlock{{.*}}->resource{{.*}}.buffer_0
115    ResourceStruct resStruct = nestedBlock.resource;
116    outputBuffer[6] = resStruct.buffer[2];
117
118    // CHECK: {{.*}}valBlock{{.*}}
119    outputBuffer[7] = valBlock[0].f;
120
121    // Test parameter block inside a struct
122    // CHECK: {{.*}}struct_with_parameter_block_arr{{.*}}
123    StructWithParameterBlock local = struct_with_parameter_block;
124    outputBuffer[8] = local.arr[0].buffer[0];
125
126    // Test function with parameter block containing a struct with a resource type
127    func(parentBlock.c);
128
129    // Test function with a copy of the parameter block
130    OtherParent other_p;
131    other_p.c2 = parentBlock.c;
132    func(other_p.c2);
133
134    ParentStruct p;
135    p = parentBlock;
136    func2(p);
137
138    // Test function with a field from the parameter block
139    func3(p.tex_array);
140
141    // Test function with a parameter block in the function signature
142    func4(parentBlock);
143}