Debian Codegraph @

test/opt/desc_sroa_test.cpp

raw
1 // Copyright (c) 2019 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include <string>
16
17 #include "test/opt/pass_fixture.h"
18 #include "test/opt/pass_utils.h"
19
20 namespace spvtools {
21 namespace opt {
22 namespace {
23
24 using DescriptorScalarReplacementTest = PassTest<::testing::Test>;
25
26 std::string GetStructureArrayTestSpirv() {
27 // The SPIR-V for the following high-level shader:
28 // Flattening structures and arrays should result in the following binding
29 // numbers. Only the ones that are actually used in the shader should be in
30 // the final SPIR-V.
31 //
32 // globalS[0][0].t[0] 0 (used)
33 // globalS[0][0].t[1] 1
34 // globalS[0][0].s[0] 2 (used)
35 // globalS[0][0].s[1] 3
36 // globalS[0][1].t[0] 4
37 // globalS[0][1].t[1] 5
38 // globalS[0][1].s[0] 6
39 // globalS[0][1].s[1] 7
40 // globalS[1][0].t[0] 8
41 // globalS[1][0].t[1] 9
42 // globalS[1][0].s[0] 10
43 // globalS[1][0].s[1] 11
44 // globalS[1][1].t[0] 12
45 // globalS[1][1].t[1] 13 (used)
46 // globalS[1][1].s[0] 14
47 // globalS[1][1].s[1] 15 (used)
48
49 /*
50 struct S {
51 Texture2D t[2];
52 SamplerState s[2];
53 };
54
55 S globalS[2][2];
56
57 float4 main() : SV_Target {
58 return globalS[0][0].t[0].Sample(globalS[0][0].s[0], float2(0,0)) +
59 globalS[1][1].t[1].Sample(globalS[1][1].s[1], float2(0,0));
60 }
61 */
62
63 return R"(
64 OpCapability Shader
65 OpMemoryModel Logical GLSL450
66 OpEntryPoint Fragment %main "main" %out_var_SV_Target
67 OpExecutionMode %main OriginUpperLeft
68 OpName %S "S"
69 OpMemberName %S 0 "t"
70 OpMemberName %S 1 "s"
71 OpName %type_2d_image "type.2d.image"
72 OpName %type_sampler "type.sampler"
73 OpName %globalS "globalS"
74 OpName %out_var_SV_Target "out.var.SV_Target"
75 OpName %main "main"
76 OpName %src_main "src.main"
77 OpName %bb_entry "bb.entry"
78 OpName %type_sampled_image "type.sampled.image"
79 OpDecorate %out_var_SV_Target Location 0
80 OpDecorate %globalS DescriptorSet 0
81 OpDecorate %globalS Binding 0
82 %int = OpTypeInt 32 1
83 %int_0 = OpConstant %int 0
84 %int_1 = OpConstant %int 1
85 %float = OpTypeFloat 32
86 %float_0 = OpConstant %float 0
87 %v2float = OpTypeVector %float 2
88 %10 = OpConstantComposite %v2float %float_0 %float_0
89 %uint = OpTypeInt 32 0
90 %uint_2 = OpConstant %uint 2
91 %type_2d_image = OpTypeImage %float 2D 2 0 0 1 Unknown
92 %_arr_type_2d_image_uint_2 = OpTypeArray %type_2d_image %uint_2
93 %type_sampler = OpTypeSampler
94 %_arr_type_sampler_uint_2 = OpTypeArray %type_sampler %uint_2
95 %S = OpTypeStruct %_arr_type_2d_image_uint_2 %_arr_type_sampler_uint_2
96 %_arr_S_uint_2 = OpTypeArray %S %uint_2
97 %_arr__arr_S_uint_2_uint_2 = OpTypeArray %_arr_S_uint_2 %uint_2
98 %_ptr_UniformConstant__arr__arr_S_uint_2_uint_2 = OpTypePointer UniformConstant %_arr__arr_S_uint_2_uint_2
99 %v4float = OpTypeVector %float 4
100 %_ptr_Output_v4float = OpTypePointer Output %v4float
101 %void = OpTypeVoid
102 %24 = OpTypeFunction %void
103 %28 = OpTypeFunction %v4float
104 %_ptr_UniformConstant_type_2d_image = OpTypePointer UniformConstant %type_2d_image
105 %_ptr_UniformConstant_type_sampler = OpTypePointer UniformConstant %type_sampler
106 %type_sampled_image = OpTypeSampledImage %type_2d_image
107 %globalS = OpVariable %_ptr_UniformConstant__arr__arr_S_uint_2_uint_2 UniformConstant
108 %out_var_SV_Target = OpVariable %_ptr_Output_v4float Output
109 %main = OpFunction %void None %24
110 %25 = OpLabel
111 %26 = OpFunctionCall %v4float %src_main
112 OpStore %out_var_SV_Target %26
113 OpReturn
114 OpFunctionEnd
115 %src_main = OpFunction %v4float None %28
116 %bb_entry = OpLabel
117 %31 = OpAccessChain %_ptr_UniformConstant_type_2d_image %globalS %int_0 %int_0 %int_0 %int_0
118 %32 = OpLoad %type_2d_image %31
119 %34 = OpAccessChain %_ptr_UniformConstant_type_sampler %globalS %int_0 %int_0 %int_1 %int_0
120 %35 = OpLoad %type_sampler %34
121 %37 = OpSampledImage %type_sampled_image %32 %35
122 %38 = OpImageSampleImplicitLod %v4float %37 %10 None
123 %39 = OpAccessChain %_ptr_UniformConstant_type_2d_image %globalS %int_1 %int_1 %int_0 %int_1
124 %40 = OpLoad %type_2d_image %39
125 %41 = OpAccessChain %_ptr_UniformConstant_type_sampler %globalS %int_1 %int_1 %int_1 %int_1
126 %42 = OpLoad %type_sampler %41
127 %43 = OpSampledImage %type_sampled_image %40 %42
128 %44 = OpImageSampleImplicitLod %v4float %43 %10 None
129 %45 = OpFAdd %v4float %38 %44
130 OpReturnValue %45
131 OpFunctionEnd
132 )";
133 }
134
135 TEST_F(DescriptorScalarReplacementTest, ExpandArrayOfTextures) {
136 const std::string text = R"(
137 ; CHECK: OpDecorate [[var1:%\w+]] DescriptorSet 0
138 ; CHECK: OpDecorate [[var1]] Binding 0
139 ; CHECK: OpDecorate [[var2:%\w+]] DescriptorSet 0
140 ; CHECK: OpDecorate [[var2]] Binding 1
141 ; CHECK: OpDecorate [[var3:%\w+]] DescriptorSet 0
142 ; CHECK: OpDecorate [[var3]] Binding 2
143 ; CHECK: OpDecorate [[var4:%\w+]] DescriptorSet 0
144 ; CHECK: OpDecorate [[var4]] Binding 3
145 ; CHECK: OpDecorate [[var5:%\w+]] DescriptorSet 0
146 ; CHECK: OpDecorate [[var5]] Binding 4
147 ; CHECK: [[image_type:%\w+]] = OpTypeImage
148 ; CHECK: [[ptr_type:%\w+]] = OpTypePointer UniformConstant [[image_type]]
149 ; CHECK: [[var1]] = OpVariable [[ptr_type]] UniformConstant
150 ; CHECK: [[var2]] = OpVariable [[ptr_type]] UniformConstant
151 ; CHECK: [[var3]] = OpVariable [[ptr_type]] UniformConstant
152 ; CHECK: [[var4]] = OpVariable [[ptr_type]] UniformConstant
153 ; CHECK: [[var5]] = OpVariable [[ptr_type]] UniformConstant
154 ; CHECK: OpLoad [[image_type]] [[var1]]
155 ; CHECK: OpLoad [[image_type]] [[var2]]
156 ; CHECK: OpLoad [[image_type]] [[var3]]
157 ; CHECK: OpLoad [[image_type]] [[var4]]
158 ; CHECK: OpLoad [[image_type]] [[var5]]
159 OpCapability Shader
160 OpMemoryModel Logical GLSL450
161 OpEntryPoint Fragment %main "main"
162 OpExecutionMode %main OriginUpperLeft
163 OpSource HLSL 600
164 OpDecorate %MyTextures DescriptorSet 0
165 OpDecorate %MyTextures Binding 0
166 %int = OpTypeInt 32 1
167 %int_0 = OpConstant %int 0
168 %int_1 = OpConstant %int 1
169 %int_2 = OpConstant %int 2
170 %int_3 = OpConstant %int 3
171 %int_4 = OpConstant %int 4
172 %uint = OpTypeInt 32 0
173 %uint_5 = OpConstant %uint 5
174 %float = OpTypeFloat 32
175 %type_2d_image = OpTypeImage %float 2D 2 0 0 1 Unknown
176 %_arr_type_2d_image_uint_5 = OpTypeArray %type_2d_image %uint_5
177 %_ptr_UniformConstant__arr_type_2d_image_uint_5 = OpTypePointer UniformConstant %_arr_type_2d_image_uint_5
178 %v2float = OpTypeVector %float 2
179 %void = OpTypeVoid
180 %26 = OpTypeFunction %void
181 %_ptr_UniformConstant_type_2d_image = OpTypePointer UniformConstant %type_2d_image
182 %MyTextures = OpVariable %_ptr_UniformConstant__arr_type_2d_image_uint_5 UniformConstant
183 %main = OpFunction %void None %26
184 %28 = OpLabel
185 %29 = OpUndef %v2float
186 %30 = OpAccessChain %_ptr_UniformConstant_type_2d_image %MyTextures %int_0
187 %31 = OpLoad %type_2d_image %30
188 %35 = OpAccessChain %_ptr_UniformConstant_type_2d_image %MyTextures %int_1
189 %36 = OpLoad %type_2d_image %35
190 %40 = OpAccessChain %_ptr_UniformConstant_type_2d_image %MyTextures %int_2
191 %41 = OpLoad %type_2d_image %40
192 %45 = OpAccessChain %_ptr_UniformConstant_type_2d_image %MyTextures %int_3
193 %46 = OpLoad %type_2d_image %45
194 %50 = OpAccessChain %_ptr_UniformConstant_type_2d_image %MyTextures %int_4
195 %51 = OpLoad %type_2d_image %50
196 OpReturn
197 OpFunctionEnd
198
199 )";
200
201 SinglePassRunAndMatch<DescriptorScalarReplacement>(
202 text, true, /* flatten_composites=*/true, /* flatten_arrays=*/true);
203 }
204
205 TEST_F(DescriptorScalarReplacementTest, ExpandArrayOfSamplers) {
206 const std::string text = R"(
207 ; CHECK: OpDecorate [[var1:%\w+]] DescriptorSet 0
208 ; CHECK: OpDecorate [[var1]] Binding 1
209 ; CHECK: OpDecorate [[var2:%\w+]] DescriptorSet 0
210 ; CHECK: OpDecorate [[var2]] Binding 2
211 ; CHECK: OpDecorate [[var3:%\w+]] DescriptorSet 0
212 ; CHECK: OpDecorate [[var3]] Binding 3
213 ; CHECK: [[sampler_type:%\w+]] = OpTypeSampler
214 ; CHECK: [[ptr_type:%\w+]] = OpTypePointer UniformConstant [[sampler_type]]
215 ; CHECK: [[var1]] = OpVariable [[ptr_type]] UniformConstant
216 ; CHECK: [[var2]] = OpVariable [[ptr_type]] UniformConstant
217 ; CHECK: [[var3]] = OpVariable [[ptr_type]] UniformConstant
218 ; CHECK: OpLoad [[sampler_type]] [[var1]]
219 ; CHECK: OpLoad [[sampler_type]] [[var2]]
220 ; CHECK: OpLoad [[sampler_type]] [[var3]]
221 OpCapability Shader
222 OpMemoryModel Logical GLSL450
223 OpEntryPoint Fragment %main "main"
224 OpExecutionMode %main OriginUpperLeft
225 OpSource HLSL 600
226 OpDecorate %MySampler DescriptorSet 0
227 OpDecorate %MySampler Binding 1
228 %int = OpTypeInt 32 1
229 %int_0 = OpConstant %int 0
230 %int_1 = OpConstant %int 1
231 %int_2 = OpConstant %int 2
232 %uint = OpTypeInt 32 0
233 %uint_3 = OpConstant %uint 3
234 %type_sampler = OpTypeSampler
235 %_arr_type_sampler_uint_3 = OpTypeArray %type_sampler %uint_3
236 %_ptr_UniformConstant__arr_type_sampler_uint_3 = OpTypePointer UniformConstant %_arr_type_sampler_uint_3
237 %void = OpTypeVoid
238 %26 = OpTypeFunction %void
239 %_ptr_UniformConstant_type_sampler = OpTypePointer UniformConstant %type_sampler
240 %MySampler = OpVariable %_ptr_UniformConstant__arr_type_sampler_uint_3 UniformConstant
241 %main = OpFunction %void None %26
242 %28 = OpLabel
243 %31 = OpAccessChain %_ptr_UniformConstant_type_sampler %MySampler %int_0
244 %32 = OpLoad %type_sampler %31
245 %35 = OpAccessChain %_ptr_UniformConstant_type_sampler %MySampler %int_1
246 %36 = OpLoad %type_sampler %35
247 %40 = OpAccessChain %_ptr_UniformConstant_type_sampler %MySampler %int_2
248 %41 = OpLoad %type_sampler %40
249 OpReturn
250 OpFunctionEnd
251 )";
252
253 SinglePassRunAndMatch<DescriptorScalarReplacement>(
254 text, true, /* flatten_composites=*/true, /* flatten_arrays=*/true);
255 }
256
257 TEST_F(DescriptorScalarReplacementTest, ExpandArrayOfSSBOs) {
258 // Tests the expansion of an SSBO. Also check that an access chain with more
259 // than 1 index is correctly handled.
260 const std::string text = R"(
261 ; CHECK: OpDecorate [[var1:%\w+]] DescriptorSet 0
262 ; CHECK: OpDecorate [[var1]] Binding 0
263 ; CHECK: OpDecorate [[var2:%\w+]] DescriptorSet 0
264 ; CHECK: OpDecorate [[var2]] Binding 1
265 ; CHECK: OpTypeStruct
266 ; CHECK: [[struct_type:%\w+]] = OpTypeStruct
267 ; CHECK: [[ptr_type:%\w+]] = OpTypePointer Uniform [[struct_type]]
268 ; CHECK: [[var1]] = OpVariable [[ptr_type]] Uniform
269 ; CHECK: [[var2]] = OpVariable [[ptr_type]] Uniform
270 ; CHECK: [[ac1:%\w+]] = OpAccessChain %_ptr_Uniform_v4float [[var1]] %uint_0 %uint_0 %uint_0
271 ; CHECK: OpLoad %v4float [[ac1]]
272 ; CHECK: [[ac2:%\w+]] = OpAccessChain %_ptr_Uniform_v4float [[var2]] %uint_0 %uint_0 %uint_0
273 ; CHECK: OpLoad %v4float [[ac2]]
274 OpCapability Shader
275 OpMemoryModel Logical GLSL450
276 OpEntryPoint Fragment %main "main"
277 OpExecutionMode %main OriginUpperLeft
278 OpSource HLSL 600
279 OpDecorate %buffers DescriptorSet 0
280 OpDecorate %buffers Binding 0
281 OpMemberDecorate %S 0 Offset 0
282 OpDecorate %_runtimearr_S ArrayStride 16
283 OpMemberDecorate %type_StructuredBuffer_S 0 Offset 0
284 OpMemberDecorate %type_StructuredBuffer_S 0 NonWritable
285 OpDecorate %type_StructuredBuffer_S BufferBlock
286 %uint = OpTypeInt 32 0
287 %uint_0 = OpConstant %uint 0
288 %uint_1 = OpConstant %uint 1
289 %uint_2 = OpConstant %uint 2
290 %float = OpTypeFloat 32
291 %v4float = OpTypeVector %float 4
292 %S = OpTypeStruct %v4float
293 %_runtimearr_S = OpTypeRuntimeArray %S
294 %type_StructuredBuffer_S = OpTypeStruct %_runtimearr_S
295 %_arr_type_StructuredBuffer_S_uint_2 = OpTypeArray %type_StructuredBuffer_S %uint_2
296 %_ptr_Uniform__arr_type_StructuredBuffer_S_uint_2 = OpTypePointer Uniform %_arr_type_StructuredBuffer_S_uint_2
297 %_ptr_Uniform_type_StructuredBuffer_S = OpTypePointer Uniform %type_StructuredBuffer_S
298 %void = OpTypeVoid
299 %19 = OpTypeFunction %void
300 %_ptr_Uniform_v4float = OpTypePointer Uniform %v4float
301 %buffers = OpVariable %_ptr_Uniform__arr_type_StructuredBuffer_S_uint_2 Uniform
302 %main = OpFunction %void None %19
303 %21 = OpLabel
304 %22 = OpAccessChain %_ptr_Uniform_v4float %buffers %uint_0 %uint_0 %uint_0 %uint_0
305 %23 = OpLoad %v4float %22
306 %24 = OpAccessChain %_ptr_Uniform_type_StructuredBuffer_S %buffers %uint_1
307 %25 = OpAccessChain %_ptr_Uniform_v4float %24 %uint_0 %uint_0 %uint_0
308 %26 = OpLoad %v4float %25
309 OpReturn
310 OpFunctionEnd
311 )";
312
313 SinglePassRunAndMatch<DescriptorScalarReplacement>(
314 text, true, /* flatten_composites=*/true, /* flatten_arrays=*/true);
315 }
316
317 TEST_F(DescriptorScalarReplacementTest, NameNewVariables) {
318 // Checks that if the original variable has a name, then the new variables
319 // will have a name derived from that name.
320 const std::string text = R"(
321 ; CHECK: OpName [[var1:%\w+]] "SSBO[0]"
322 ; CHECK: OpName [[var2:%\w+]] "SSBO[1]"
323 ; CHECK: OpDecorate [[var1]] DescriptorSet 0
324 ; CHECK: OpDecorate [[var1]] Binding 0
325 ; CHECK: OpDecorate [[var2]] DescriptorSet 0
326 ; CHECK: OpDecorate [[var2]] Binding 1
327 ; CHECK: OpTypeStruct
328 ; CHECK: [[struct_type:%\w+]] = OpTypeStruct
329 ; CHECK: [[ptr_type:%\w+]] = OpTypePointer Uniform [[struct_type]]
330 ; CHECK: [[var1]] = OpVariable [[ptr_type]] Uniform
331 ; CHECK: [[var2]] = OpVariable [[ptr_type]] Uniform
332 ; CHECK: [[ac1:%\w+]] = OpAccessChain %_ptr_Uniform_v4float [[var1]] %uint_0 %uint_0 %uint_0
333 ; CHECK: OpLoad %v4float [[ac1]]
334 ; CHECK: [[ac2:%\w+]] = OpAccessChain %_ptr_Uniform_v4float [[var2]] %uint_0 %uint_0 %uint_0
335 ; CHECK: OpLoad %v4float [[ac2]]
336 OpCapability Shader
337 OpMemoryModel Logical GLSL450
338 OpEntryPoint Fragment %main "main"
339 OpExecutionMode %main OriginUpperLeft
340 OpSource HLSL 600
341 OpName %buffers "SSBO"
342 OpDecorate %buffers DescriptorSet 0
343 OpDecorate %buffers Binding 0
344 OpMemberDecorate %S 0 Offset 0
345 OpDecorate %_runtimearr_S ArrayStride 16
346 OpMemberDecorate %type_StructuredBuffer_S 0 Offset 0
347 OpMemberDecorate %type_StructuredBuffer_S 0 NonWritable
348 OpDecorate %type_StructuredBuffer_S BufferBlock
349 %uint = OpTypeInt 32 0
350 %uint_0 = OpConstant %uint 0
351 %uint_1 = OpConstant %uint 1
352 %uint_2 = OpConstant %uint 2
353 %float = OpTypeFloat 32
354 %v4float = OpTypeVector %float 4
355 %S = OpTypeStruct %v4float
356 %_runtimearr_S = OpTypeRuntimeArray %S
357 %type_StructuredBuffer_S = OpTypeStruct %_runtimearr_S
358 %_arr_type_StructuredBuffer_S_uint_2 = OpTypeArray %type_StructuredBuffer_S %uint_2
359 %_ptr_Uniform__arr_type_StructuredBuffer_S_uint_2 = OpTypePointer Uniform %_arr_type_StructuredBuffer_S_uint_2
360 %_ptr_Uniform_type_StructuredBuffer_S = OpTypePointer Uniform %type_StructuredBuffer_S
361 %void = OpTypeVoid
362 %19 = OpTypeFunction %void
363 %_ptr_Uniform_v4float = OpTypePointer Uniform %v4float
364 %buffers = OpVariable %_ptr_Uniform__arr_type_StructuredBuffer_S_uint_2 Uniform
365 %main = OpFunction %void None %19
366 %21 = OpLabel
367 %22 = OpAccessChain %_ptr_Uniform_v4float %buffers %uint_0 %uint_0 %uint_0 %uint_0
368 %23 = OpLoad %v4float %22
369 %24 = OpAccessChain %_ptr_Uniform_type_StructuredBuffer_S %buffers %uint_1
370 %25 = OpAccessChain %_ptr_Uniform_v4float %24 %uint_0 %uint_0 %uint_0
371 %26 = OpLoad %v4float %25
372 OpReturn
373 OpFunctionEnd
374 )";
375
376 SinglePassRunAndMatch<DescriptorScalarReplacement>(
377 text, true, /* flatten_composites=*/true, /* flatten_arrays=*/true);
378 }
379
380 TEST_F(DescriptorScalarReplacementTest, DontExpandCBuffers) {
381 // Checks that constant buffers are not expanded.
382 // Constant buffers are represented as global structures, but they should not
383 // be replaced with new variables for their elements.
384 /*
385 cbuffer MyCbuffer : register(b1) {
386 float2 a;
387 float2 b;
388 };
389 float main() : A {
390 return a.x + b.y;
391 }
392 */
393 const std::string text = R"(
394 ; CHECK: OpAccessChain %_ptr_Uniform_float %MyCbuffer %int_0 %int_0
395 ; CHECK: OpAccessChain %_ptr_Uniform_float %MyCbuffer %int_1 %int_1
396 OpCapability Shader
397 OpMemoryModel Logical GLSL450
398 OpEntryPoint Vertex %main "main" %out_var_A
399 OpSource HLSL 600
400 OpName %type_MyCbuffer "type.MyCbuffer"
401 OpMemberName %type_MyCbuffer 0 "a"
402 OpMemberName %type_MyCbuffer 1 "b"
403 OpName %MyCbuffer "MyCbuffer"
404 OpName %out_var_A "out.var.A"
405 OpName %main "main"
406 OpDecorate %out_var_A Location 0
407 OpDecorate %MyCbuffer DescriptorSet 0
408 OpDecorate %MyCbuffer Binding 1
409 OpMemberDecorate %type_MyCbuffer 0 Offset 0
410 OpMemberDecorate %type_MyCbuffer 1 Offset 8
411 OpDecorate %type_MyCbuffer Block
412 %int = OpTypeInt 32 1
413 %int_0 = OpConstant %int 0
414 %int_1 = OpConstant %int 1
415 %float = OpTypeFloat 32
416 %v2float = OpTypeVector %float 2
417 %type_MyCbuffer = OpTypeStruct %v2float %v2float
418 %_ptr_Uniform_type_MyCbuffer = OpTypePointer Uniform %type_MyCbuffer
419 %_ptr_Output_float = OpTypePointer Output %float
420 %void = OpTypeVoid
421 %13 = OpTypeFunction %void
422 %_ptr_Uniform_float = OpTypePointer Uniform %float
423 %MyCbuffer = OpVariable %_ptr_Uniform_type_MyCbuffer Uniform
424 %out_var_A = OpVariable %_ptr_Output_float Output
425 %main = OpFunction %void None %13
426 %15 = OpLabel
427 %16 = OpAccessChain %_ptr_Uniform_float %MyCbuffer %int_0 %int_0
428 %17 = OpLoad %float %16
429 %18 = OpAccessChain %_ptr_Uniform_float %MyCbuffer %int_1 %int_1
430 %19 = OpLoad %float %18
431 %20 = OpFAdd %float %17 %19
432 OpStore %out_var_A %20
433 OpReturn
434 OpFunctionEnd
435 )";
436
437 SinglePassRunAndMatch<DescriptorScalarReplacement>(
438 text, true, /* flatten_composites=*/true, /* flatten_arrays=*/true);
439 }
440
441 TEST_F(DescriptorScalarReplacementTest, DontExpandStructuredBuffers) {
442 // Checks that structured buffers are not expanded.
443 // Structured buffers are represented as global structures, that have one
444 // member which is a runtime array.
445 /*
446 struct S {
447 float2 a;
448 float2 b;
449 };
450 RWStructuredBuffer<S> sb;
451 float main() : A {
452 return sb[0].a.x + sb[0].b.x;
453 }
454 */
455 const std::string text = R"(
456 ; CHECK: OpAccessChain %_ptr_Uniform_float %sb %int_0 %uint_0 %int_0 %int_0
457 ; CHECK: OpAccessChain %_ptr_Uniform_float %sb %int_0 %uint_0 %int_1 %int_0
458 OpCapability Shader
459 OpMemoryModel Logical GLSL450
460 OpEntryPoint Vertex %main "main" %out_var_A
461 OpName %type_RWStructuredBuffer_S "type.RWStructuredBuffer.S"
462 OpName %S "S"
463 OpMemberName %S 0 "a"
464 OpMemberName %S 1 "b"
465 OpName %sb "sb"
466 OpName %out_var_A "out.var.A"
467 OpName %main "main"
468 OpDecorate %out_var_A Location 0
469 OpDecorate %sb DescriptorSet 0
470 OpDecorate %sb Binding 0
471 OpMemberDecorate %S 0 Offset 0
472 OpMemberDecorate %S 1 Offset 8
473 OpDecorate %_runtimearr_S ArrayStride 16
474 OpMemberDecorate %type_RWStructuredBuffer_S 0 Offset 0
475 OpDecorate %type_RWStructuredBuffer_S BufferBlock
476 %int = OpTypeInt 32 1
477 %int_0 = OpConstant %int 0
478 %uint = OpTypeInt 32 0
479 %uint_0 = OpConstant %uint 0
480 %int_1 = OpConstant %int 1
481 %float = OpTypeFloat 32
482 %v2float = OpTypeVector %float 2
483 %S = OpTypeStruct %v2float %v2float
484 %_runtimearr_S = OpTypeRuntimeArray %S
485 %type_RWStructuredBuffer_S = OpTypeStruct %_runtimearr_S
486 %_ptr_Uniform_type_RWStructuredBuffer_S = OpTypePointer Uniform %type_RWStructuredBuffer_S
487 %_ptr_Output_float = OpTypePointer Output %float
488 %void = OpTypeVoid
489 %17 = OpTypeFunction %void
490 %_ptr_Uniform_float = OpTypePointer Uniform %float
491 %sb = OpVariable %_ptr_Uniform_type_RWStructuredBuffer_S Uniform
492 %out_var_A = OpVariable %_ptr_Output_float Output
493 %main = OpFunction %void None %17
494 %19 = OpLabel
495 %20 = OpAccessChain %_ptr_Uniform_float %sb %int_0 %uint_0 %int_0 %int_0
496 %21 = OpLoad %float %20
497 %22 = OpAccessChain %_ptr_Uniform_float %sb %int_0 %uint_0 %int_1 %int_0
498 %23 = OpLoad %float %22
499 %24 = OpFAdd %float %21 %23
500 OpStore %out_var_A %24
501 OpReturn
502 OpFunctionEnd
503 )";
504
505 SinglePassRunAndMatch<DescriptorScalarReplacement>(
506 text, true, /* flatten_composites=*/true, /* flatten_arrays=*/true);
507 }
508
509 TEST_F(DescriptorScalarReplacementTest, StructureArrayNames) {
510 // Checks that names are properly generated for multi-dimension arrays and
511 // structure members.
512 const std::string checks = R"(
513 ; CHECK: OpName %globalS_0__0__t_0_ "globalS[0][0].t[0]"
514 ; CHECK: OpName %globalS_0__0__s_0_ "globalS[0][0].s[0]"
515 ; CHECK: OpName %globalS_1__1__t_1_ "globalS[1][1].t[1]"
516 ; CHECK: OpName %globalS_1__1__s_1_ "globalS[1][1].s[1]"
517 )";
518
519 const std::string text = checks + GetStructureArrayTestSpirv();
520 SinglePassRunAndMatch<DescriptorScalarReplacement>(
521 text, true, /* flatten_composites=*/true, /* flatten_arrays=*/true);
522 }
523
524 TEST_F(DescriptorScalarReplacementTest,
525 FlattensArraysOfStructsButNoResourceArrays) {
526 // Check that only the composite array is flattenned, but internal resource
527 // arrays are left as-is.
528 const std::string checks = R"(
529 ; CHECK: OpName %globalS_0__0__t "globalS[0][0].t"
530 ; CHECK: OpName %globalS_0__0__s "globalS[0][0].s"
531 ; CHECK: OpName %globalS_1__1__t "globalS[1][1].t"
532 ; CHECK: OpName %globalS_1__1__s "globalS[1][1].s"
533 ; CHECK-NOT: OpName %globalS_1__1__t_0_
534 ; CHECK-NOT: OpName %globalS_1__1__s_0_
535 )";
536
537 const std::string text = checks + GetStructureArrayTestSpirv();
538 SinglePassRunAndMatch<DescriptorScalarReplacement>(
539 text, true, /* flatten_composites=*/true, /* flatten_arrays=*/false);
540 }
541
542 TEST_F(DescriptorScalarReplacementTest, FlattenNothingIfAskedTo) {
543 // Not useful, but checks what happens if both are set to false.
544 // In such case, nothing happens.
545 const std::string checks = R"(
546 ; CHECK: OpName %globalS
547 ; CHECK-NOT: OpName %globalS_
548 )";
549
550 const std::string text = checks + GetStructureArrayTestSpirv();
551 SinglePassRunAndMatch<DescriptorScalarReplacement>(
552 text, true, /* flatten_composites=*/false, /* flatten_arrays=*/false);
553 }
554
555 TEST_F(DescriptorScalarReplacementTest, StructureArrayBindings) {
556 // Checks that flattening structures and arrays results in correct binding
557 // numbers.
558 const std::string checks = R"(
559 ; CHECK: OpDecorate %globalS_0__0__t_0_ Binding 0
560 ; CHECK: OpDecorate %globalS_0__0__s_0_ Binding 2
561 ; CHECK: OpDecorate %globalS_1__1__t_1_ Binding 13
562 ; CHECK: OpDecorate %globalS_1__1__s_1_ Binding 15
563 )";
564
565 const std::string text = checks + GetStructureArrayTestSpirv();
566 SinglePassRunAndMatch<DescriptorScalarReplacement>(
567 text, true, /* flatten_composites=*/true, /* flatten_arrays=*/true);
568 }
569
570 TEST_F(DescriptorScalarReplacementTest, StructureArrayReplacements) {
571 // Checks that all access chains indexing into structures and/or arrays are
572 // replaced with direct access to replacement variables.
573 const std::string checks = R"(
574 ; CHECK-NOT: OpAccessChain
575 ; CHECK: OpLoad %type_2d_image %globalS_0__0__t_0_
576 ; CHECK: OpLoad %type_sampler %globalS_0__0__s_0_
577 ; CHECK: OpLoad %type_2d_image %globalS_1__1__t_1_
578 ; CHECK: OpLoad %type_sampler %globalS_1__1__s_1_
579 )";
580
581 const std::string text = checks + GetStructureArrayTestSpirv();
582 SinglePassRunAndMatch<DescriptorScalarReplacement>(
583 text, true, /* flatten_composites=*/true, /* flatten_arrays=*/true);
584 }
585
586 TEST_F(DescriptorScalarReplacementTest, ResourceStructAsFunctionParam) {
587 // Checks that a mix of OpAccessChain, OpLoad, and OpCompositeExtract patterns
588 // can be properly replaced with replacement variables.
589 // This pattern can be seen when a global structure of resources is passed to
590 // a function.
591
592 /* High-level source:
593 // globalS[0].t[0] binding: 0 (used)
594 // globalS[0].t[1] binding: 1 (used)
595 // globalS[0].tt[0].s[0] binding: 2
596 // globalS[0].tt[0].s[1] binding: 3 (used)
597 // globalS[0].tt[0].s[2] binding: 4
598 // globalS[0].tt[1].s[0] binding: 5
599 // globalS[0].tt[1].s[1] binding: 6
600 // globalS[0].tt[1].s[2] binding: 7 (used)
601 // globalS[1].t[0] binding: 8 (used)
602 // globalS[1].t[1] binding: 9 (used)
603 // globalS[1].tt[0].s[0] binding: 10
604 // globalS[1].tt[0].s[1] binding: 11 (used)
605 // globalS[1].tt[0].s[2] binding: 12
606 // globalS[1].tt[1].s[0] binding: 13
607 // globalS[1].tt[1].s[1] binding: 14
608 // globalS[1].tt[1].s[2] binding: 15 (used)
609
610 struct T {
611 SamplerState s[3];
612 };
613
614 struct S {
615 Texture2D t[2];
616 T tt[2];
617 };
618
619 float4 tex2D(S x, float2 v) {
620 return x.t[0].Sample(x.tt[0].s[1], v) + x.t[1].Sample(x.tt[1].s[2], v);
621 }
622
623 S globalS[2];
624
625 float4 main() : SV_Target {
626 return tex2D(globalS[0], float2(0,0)) + tex2D(globalS[1], float2(0,0)) ;
627 }
628 */
629 const std::string shader = R"(
630 OpCapability Shader
631 OpMemoryModel Logical GLSL450
632 OpEntryPoint Fragment %main "main" %out_var_SV_Target
633 OpExecutionMode %main OriginUpperLeft
634 OpName %S "S"
635 OpMemberName %S 0 "t"
636 OpMemberName %S 1 "tt"
637 OpName %type_2d_image "type.2d.image"
638 OpName %T "T"
639 OpMemberName %T 0 "s"
640 OpName %type_sampler "type.sampler"
641 OpName %globalS "globalS"
642 OpName %out_var_SV_Target "out.var.SV_Target"
643 OpName %main "main"
644 OpName %type_sampled_image "type.sampled.image"
645 OpDecorate %out_var_SV_Target Location 0
646 OpDecorate %globalS DescriptorSet 0
647 OpDecorate %globalS Binding 0
648 %int = OpTypeInt 32 1
649 %int_0 = OpConstant %int 0
650 %float = OpTypeFloat 32
651 %float_0 = OpConstant %float 0
652 %v2float = OpTypeVector %float 2
653 %14 = OpConstantComposite %v2float %float_0 %float_0
654 %int_1 = OpConstant %int 1
655 %uint = OpTypeInt 32 0
656 %uint_2 = OpConstant %uint 2
657 %type_2d_image = OpTypeImage %float 2D 2 0 0 1 Unknown
658 %_arr_type_2d_image_uint_2 = OpTypeArray %type_2d_image %uint_2
659 %uint_3 = OpConstant %uint 3
660 %type_sampler = OpTypeSampler
661 %_arr_type_sampler_uint_3 = OpTypeArray %type_sampler %uint_3
662 %T = OpTypeStruct %_arr_type_sampler_uint_3
663 %_arr_T_uint_2 = OpTypeArray %T %uint_2
664 %S = OpTypeStruct %_arr_type_2d_image_uint_2 %_arr_T_uint_2
665 %_arr_S_uint_2 = OpTypeArray %S %uint_2
666 %_ptr_UniformConstant__arr_S_uint_2 = OpTypePointer UniformConstant %_arr_S_uint_2
667 %v4float = OpTypeVector %float 4
668 %_ptr_Output_v4float = OpTypePointer Output %v4float
669 %void = OpTypeVoid
670 %27 = OpTypeFunction %void
671 %_ptr_UniformConstant_S = OpTypePointer UniformConstant %S
672 %type_sampled_image = OpTypeSampledImage %type_2d_image
673 %globalS = OpVariable %_ptr_UniformConstant__arr_S_uint_2 UniformConstant
674 %out_var_SV_Target = OpVariable %_ptr_Output_v4float Output
675 %main = OpFunction %void None %27
676 %29 = OpLabel
677 %30 = OpAccessChain %_ptr_UniformConstant_S %globalS %int_0
678 %31 = OpLoad %S %30
679 %32 = OpCompositeExtract %_arr_type_2d_image_uint_2 %31 0
680 %33 = OpCompositeExtract %type_2d_image %32 0
681 %34 = OpCompositeExtract %type_2d_image %32 1
682 %35 = OpCompositeExtract %_arr_T_uint_2 %31 1
683 %36 = OpCompositeExtract %T %35 0
684 %37 = OpCompositeExtract %_arr_type_sampler_uint_3 %36 0
685 %38 = OpCompositeExtract %type_sampler %37 1
686 %39 = OpCompositeExtract %T %35 1
687 %40 = OpCompositeExtract %_arr_type_sampler_uint_3 %39 0
688 %41 = OpCompositeExtract %type_sampler %40 2
689 %42 = OpSampledImage %type_sampled_image %33 %38
690 %43 = OpImageSampleImplicitLod %v4float %42 %14 None
691 %44 = OpSampledImage %type_sampled_image %34 %41
692 %45 = OpImageSampleImplicitLod %v4float %44 %14 None
693 %46 = OpFAdd %v4float %43 %45
694 %47 = OpAccessChain %_ptr_UniformConstant_S %globalS %int_1
695 %48 = OpLoad %S %47
696 %49 = OpCompositeExtract %_arr_type_2d_image_uint_2 %48 0
697 %50 = OpCompositeExtract %type_2d_image %49 0
698 %51 = OpCompositeExtract %type_2d_image %49 1
699 %52 = OpCompositeExtract %_arr_T_uint_2 %48 1
700 %53 = OpCompositeExtract %T %52 0
701 %54 = OpCompositeExtract %_arr_type_sampler_uint_3 %53 0
702 %55 = OpCompositeExtract %type_sampler %54 1
703 %56 = OpCompositeExtract %T %52 1
704 %57 = OpCompositeExtract %_arr_type_sampler_uint_3 %56 0
705 %58 = OpCompositeExtract %type_sampler %57 2
706 %59 = OpSampledImage %type_sampled_image %50 %55
707 %60 = OpImageSampleImplicitLod %v4float %59 %14 None
708 %61 = OpSampledImage %type_sampled_image %51 %58
709 %62 = OpImageSampleImplicitLod %v4float %61 %14 None
710 %63 = OpFAdd %v4float %60 %62
711 %64 = OpFAdd %v4float %46 %63
712 OpStore %out_var_SV_Target %64
713 OpReturn
714 OpFunctionEnd
715 )";
716
717 const std::string checks = R"(
718 ; CHECK: OpName %globalS_0__t_0_ "globalS[0].t[0]"
719 ; CHECK: OpName %globalS_0__t_1_ "globalS[0].t[1]"
720 ; CHECK: OpName %globalS_1__t_0_ "globalS[1].t[0]"
721 ; CHECK: OpName %globalS_1__t_1_ "globalS[1].t[1]"
722 ; CHECK: OpName %globalS_0__tt_0__s_1_ "globalS[0].tt[0].s[1]"
723 ; CHECK: OpName %globalS_0__tt_1__s_2_ "globalS[0].tt[1].s[2]"
724 ; CHECK: OpName %globalS_1__tt_0__s_1_ "globalS[1].tt[0].s[1]"
725 ; CHECK: OpName %globalS_1__tt_1__s_2_ "globalS[1].tt[1].s[2]"
726 ; CHECK: OpDecorate %globalS_0__t_0_ Binding 0
727 ; CHECK: OpDecorate %globalS_0__t_1_ Binding 1
728 ; CHECK: OpDecorate %globalS_1__t_0_ Binding 8
729 ; CHECK: OpDecorate %globalS_1__t_1_ Binding 9
730 ; CHECK: OpDecorate %globalS_0__tt_0__s_1_ Binding 3
731 ; CHECK: OpDecorate %globalS_0__tt_1__s_2_ Binding 7
732 ; CHECK: OpDecorate %globalS_1__tt_0__s_1_ Binding 11
733 ; CHECK: OpDecorate %globalS_1__tt_1__s_2_ Binding 15
734
735 ; CHECK: %globalS_0__t_0_ = OpVariable %_ptr_UniformConstant_type_2d_image UniformConstant
736 ; CHECK: %globalS_0__t_1_ = OpVariable %_ptr_UniformConstant_type_2d_image UniformConstant
737 ; CHECK: %globalS_1__t_0_ = OpVariable %_ptr_UniformConstant_type_2d_image UniformConstant
738 ; CHECK: %globalS_1__t_1_ = OpVariable %_ptr_UniformConstant_type_2d_image UniformConstant
739 ; CHECK: %globalS_0__tt_0__s_1_ = OpVariable %_ptr_UniformConstant_type_sampler UniformConstant
740 ; CHECK: %globalS_0__tt_1__s_2_ = OpVariable %_ptr_UniformConstant_type_sampler UniformConstant
741 ; CHECK: %globalS_1__tt_0__s_1_ = OpVariable %_ptr_UniformConstant_type_sampler UniformConstant
742 ; CHECK: %globalS_1__tt_1__s_2_ = OpVariable %_ptr_UniformConstant_type_sampler UniformConstant
743
744 ; CHECK: [[img_1:%\w+]] = OpLoad %type_2d_image %globalS_0__t_0_
745 ; CHECK: [[img_2:%\w+]] = OpLoad %type_2d_image %globalS_0__t_1_
746 ; CHECK: [[sampler_1:%\w+]] = OpLoad %type_sampler %globalS_0__tt_0__s_1_
747 ; CHECK: [[sampler_2:%\w+]] = OpLoad %type_sampler %globalS_0__tt_1__s_2_
748
749 ; CHECK: [[sampled_img_1:%\w+]] = OpSampledImage %type_sampled_image [[img_1]] [[sampler_1]]
750 ; CHECK: [[sample_1:%\w+]] = OpImageSampleImplicitLod %v4float [[sampled_img_1]]
751 ; CHECK: [[sampled_img_2:%\w+]] = OpSampledImage %type_sampled_image [[img_2]] [[sampler_2]]
752 ; CHECK: [[sample_2:%\w+]] = OpImageSampleImplicitLod %v4float [[sampled_img_2]]
753 ; CHECK: OpFAdd %v4float [[sample_1]] [[sample_2]]
754
755 ; CHECK: [[img_3:%\w+]] = OpLoad %type_2d_image %globalS_1__t_0_
756 ; CHECK: [[img_4:%\w+]] = OpLoad %type_2d_image %globalS_1__t_1_
757 ; CHECK: [[sampler_3:%\w+]] = OpLoad %type_sampler %globalS_1__tt_0__s_1_
758 ; CHECK: [[sampler_4:%\w+]] = OpLoad %type_sampler %globalS_1__tt_1__s_2_
759
760 ; CHECK: [[sampled_img_3:%\w+]] = OpSampledImage %type_sampled_image [[img_3]] [[sampler_3]]
761 ; CHECK: [[sample_3:%\w+]] = OpImageSampleImplicitLod %v4float [[sampled_img_3]]
762 ; CHECK: [[sampled_img_4:%\w+]] = OpSampledImage %type_sampled_image [[img_4]] [[sampler_4]]
763 ; CHECK: [[sample_4:%\w+]] = OpImageSampleImplicitLod %v4float [[sampled_img_4]]
764 ; CHECK: OpFAdd %v4float [[sample_3]] [[sample_4]]
765 )";
766
767 SinglePassRunAndMatch<DescriptorScalarReplacement>(
768 checks + shader, true, /* flatten_composites=*/true,
769 /* flatten_arrays=*/true);
770 }
771
772 TEST_F(DescriptorScalarReplacementTest, BindingForResourceArrayOfStructs) {
773 // Check that correct binding numbers are given to an array of descriptors
774 // to structs.
775
776 const std::string shader = R"(
777 ; CHECK: OpDecorate {{%\w+}} Binding 0
778 ; CHECK: OpDecorate {{%\w+}} Binding 1
779 OpCapability Shader
780 %1 = OpExtInstImport "GLSL.std.450"
781 OpMemoryModel Logical GLSL450
782 OpEntryPoint Fragment %2 "psmain"
783 OpExecutionMode %2 OriginUpperLeft
784 OpDecorate %5 DescriptorSet 0
785 OpDecorate %5 Binding 0
786 OpMemberDecorate %_struct_4 0 Offset 0
787 OpMemberDecorate %_struct_4 1 Offset 4
788 OpDecorate %_struct_4 Block
789 %float = OpTypeFloat 32
790 %int = OpTypeInt 32 1
791 %int_0 = OpConstant %int 0
792 %int_1 = OpConstant %int 1
793 %uint = OpTypeInt 32 0
794 %uint_2 = OpConstant %uint 2
795 %_struct_4 = OpTypeStruct %float %int
796 %_arr__struct_4_uint_2 = OpTypeArray %_struct_4 %uint_2
797 %_ptr_Uniform__arr__struct_4_uint_2 = OpTypePointer Uniform %_arr__struct_4_uint_2
798 %void = OpTypeVoid
799 %25 = OpTypeFunction %void
800 %_ptr_Uniform_int = OpTypePointer Uniform %int
801 %5 = OpVariable %_ptr_Uniform__arr__struct_4_uint_2 Uniform
802 %2 = OpFunction %void None %25
803 %29 = OpLabel
804 %40 = OpAccessChain %_ptr_Uniform_int %5 %int_0 %int_1
805 %41 = OpAccessChain %_ptr_Uniform_int %5 %int_1 %int_1
806 OpReturn
807 OpFunctionEnd
808 )";
809
810 SinglePassRunAndMatch<DescriptorScalarReplacement>(
811 shader, true, /* flatten_composites=*/true, /* flatten_arrays=*/true);
812 }
813
814 TEST_F(DescriptorScalarReplacementTest, MemberDecorationForResourceStruct) {
815 // Check that an OpMemberDecorate instruction is correctly converted to a
816 // OpDecorate instruction.
817
818 const std::string shader = R"(
819 ; CHECK: OpDecorate [[t:%\w+]] DescriptorSet 0
820 ; CHECK: OpDecorate [[t]] Binding 0
821 ; CHECK: OpDecorate [[t]] RelaxedPrecision
822 ; CHECK: OpDecorate [[s:%\w+]] DescriptorSet 0
823 ; CHECK: OpDecorate [[s]] Binding 1
824 OpCapability Shader
825 OpMemoryModel Logical GLSL450
826 OpEntryPoint Fragment %PSMain "PSMain" %in_var_TEXCOORD %out_var_SV_Target
827 OpExecutionMode %PSMain OriginUpperLeft
828 OpSource HLSL 600
829 OpName %sampler2D_h "sampler2D_h"
830 OpMemberName %sampler2D_h 0 "t"
831 OpMemberName %sampler2D_h 1 "s"
832 OpName %type_2d_image "type.2d.image"
833 OpName %type_sampler "type.sampler"
834 OpName %_MainTex "_MainTex"
835 OpName %in_var_TEXCOORD "in.var.TEXCOORD"
836 OpName %out_var_SV_Target "out.var.SV_Target"
837 OpName %PSMain "PSMain"
838 OpName %type_sampled_image "type.sampled.image"
839 OpDecorate %in_var_TEXCOORD Location 0
840 OpDecorate %out_var_SV_Target Location 0
841 OpDecorate %_MainTex DescriptorSet 0
842 OpDecorate %_MainTex Binding 0
843 OpMemberDecorate %sampler2D_h 0 RelaxedPrecision
844 OpDecorate %out_var_SV_Target RelaxedPrecision
845 OpDecorate %69 RelaxedPrecision
846 %float = OpTypeFloat 32
847 %type_2d_image = OpTypeImage %float 2D 2 0 0 1 Unknown
848 %type_sampler = OpTypeSampler
849 %sampler2D_h = OpTypeStruct %type_2d_image %type_sampler
850 %_ptr_UniformConstant_sampler2D_h = OpTypePointer UniformConstant %sampler2D_h
851 %v2float = OpTypeVector %float 2
852 %_ptr_Input_v2float = OpTypePointer Input %v2float
853 %v4float = OpTypeVector %float 4
854 %_ptr_Output_v4float = OpTypePointer Output %v4float
855 %void = OpTypeVoid
856 %35 = OpTypeFunction %void
857 %type_sampled_image = OpTypeSampledImage %type_2d_image
858 %_MainTex = OpVariable %_ptr_UniformConstant_sampler2D_h UniformConstant
859 %in_var_TEXCOORD = OpVariable %_ptr_Input_v2float Input
860 %out_var_SV_Target = OpVariable %_ptr_Output_v4float Output
861 %PSMain = OpFunction %void None %35
862 %43 = OpLabel
863 %44 = OpLoad %v2float %in_var_TEXCOORD
864 %57 = OpLoad %sampler2D_h %_MainTex
865 %72 = OpCompositeExtract %type_2d_image %57 0
866 %73 = OpCompositeExtract %type_sampler %57 1
867 %68 = OpSampledImage %type_sampled_image %72 %73
868 %69 = OpImageSampleImplicitLod %v4float %68 %44 None
869 OpStore %out_var_SV_Target %69
870 OpReturn
871 OpFunctionEnd
872 )";
873
874 SinglePassRunAndMatch<DescriptorScalarReplacement>(
875 shader, true, /* flatten_composites=*/true, /* flatten_arrays=*/true);
876 }
877
878 TEST_F(DescriptorScalarReplacementTest, DecorateStringForReflect) {
879 // Check that an OpDecorateString instruction is correctly cloned to new
880 // variable.
881
882 const std::string shader = R"(
883 ; CHECK: OpName %g_testTextures_0_ "g_testTextures[0]"
884 ; CHECK: OpDecorate %g_testTextures_0_ DescriptorSet 0
885 ; CHECK: OpDecorate %g_testTextures_0_ Binding 0
886 ; CHECK: OpDecorateString %g_testTextures_0_ UserTypeGOOGLE "texture2d"
887 OpCapability Shader
888 OpExtension "SPV_GOOGLE_hlsl_functionality1"
889 OpExtension "SPV_GOOGLE_user_type"
890 OpMemoryModel Logical GLSL450
891 OpEntryPoint Fragment %main "main" %gl_FragCoord %out_var_SV_Target
892 OpExecutionMode %main OriginUpperLeft
893 OpSource HLSL 600
894 OpName %type_2d_image "type.2d.image"
895 OpName %g_testTextures "g_testTextures"
896 OpName %out_var_SV_Target "out.var.SV_Target"
897 OpName %main "main"
898 OpName %param_var_vPixelPos "param.var.vPixelPos"
899 OpName %src_main "src.main"
900 OpName %vPixelPos "vPixelPos"
901 OpName %bb_entry "bb.entry"
902 OpDecorate %gl_FragCoord BuiltIn FragCoord
903 OpDecorateString %gl_FragCoord UserSemantic "SV_Position"
904 OpDecorateString %out_var_SV_Target UserSemantic "SV_Target"
905 OpDecorate %out_var_SV_Target Location 0
906 OpDecorate %g_testTextures DescriptorSet 0
907 OpDecorate %g_testTextures Binding 0
908 OpDecorateString %g_testTextures UserTypeGOOGLE "texture2d"
909 %uint = OpTypeInt 32 0
910 %uint_0 = OpConstant %uint 0
911 %int = OpTypeInt 32 1
912 %int_0 = OpConstant %int 0
913 %uint_2 = OpConstant %uint 2
914 %float = OpTypeFloat 32
915 %type_2d_image = OpTypeImage %float 2D 2 0 0 1 Unknown
916 %_arr_type_2d_image_uint_2 = OpTypeArray %type_2d_image %uint_2
917 %_ptr_UniformConstant__arr_type_2d_image_uint_2 = OpTypePointer UniformConstant %_arr_type_2d_image_uint_2
918 %v4float = OpTypeVector %float 4
919 %_ptr_Input_v4float = OpTypePointer Input %v4float
920 %_ptr_Output_v4float = OpTypePointer Output %v4float
921 %void = OpTypeVoid
922 %18 = OpTypeFunction %void
923 %_ptr_Function_v4float = OpTypePointer Function %v4float
924 %25 = OpTypeFunction %v4float %_ptr_Function_v4float
925 %v2float = OpTypeVector %float 2
926 %v3uint = OpTypeVector %uint 3
927 %v3int = OpTypeVector %int 3
928 %v2int = OpTypeVector %int 2
929 %_ptr_UniformConstant_type_2d_image = OpTypePointer UniformConstant %type_2d_image
930 %g_testTextures = OpVariable %_ptr_UniformConstant__arr_type_2d_image_uint_2 UniformConstant
931 %gl_FragCoord = OpVariable %_ptr_Input_v4float Input
932 %out_var_SV_Target = OpVariable %_ptr_Output_v4float Output
933 %main = OpFunction %void None %18
934 %19 = OpLabel
935 %param_var_vPixelPos = OpVariable %_ptr_Function_v4float Function
936 %22 = OpLoad %v4float %gl_FragCoord
937 OpStore %param_var_vPixelPos %22
938 %23 = OpFunctionCall %v4float %src_main %param_var_vPixelPos
939 OpStore %out_var_SV_Target %23
940 OpReturn
941 OpFunctionEnd
942 %src_main = OpFunction %v4float None %25
943 %vPixelPos = OpFunctionParameter %_ptr_Function_v4float
944 %bb_entry = OpLabel
945 %28 = OpLoad %v4float %vPixelPos
946 %30 = OpVectorShuffle %v2float %28 %28 0 1
947 %31 = OpCompositeExtract %float %30 0
948 %32 = OpCompositeExtract %float %30 1
949 %33 = OpConvertFToU %uint %31
950 %34 = OpConvertFToU %uint %32
951 %36 = OpCompositeConstruct %v3uint %33 %34 %uint_0
952 %38 = OpBitcast %v3int %36
953 %40 = OpVectorShuffle %v2int %38 %38 0 1
954 %41 = OpCompositeExtract %int %38 2
955 %43 = OpAccessChain %_ptr_UniformConstant_type_2d_image %g_testTextures %int_0
956 %44 = OpLoad %type_2d_image %43
957 %45 = OpImageFetch %v4float %44 %40 Lod %41
958 OpReturnValue %45
959 OpFunctionEnd
960 )";
961
962 SinglePassRunAndMatch<DescriptorScalarReplacement>(
963 shader, true, /* flatten_composites=*/true, /* flatten_arrays=*/true);
964 }
965
966 TEST_F(DescriptorScalarReplacementTest, ExpandArrayInOpEntryPoint) {
967 const std::string text = R"(; SPIR-V
968 ; Version: 1.6
969 ; Bound: 31
970 ; Schema: 0
971 OpCapability Shader
972 OpMemoryModel Logical GLSL450
973
974 ; CHECK: OpEntryPoint GLCompute %main "main" %output_0_ %output_1_
975
976 OpEntryPoint GLCompute %main "main" %output
977 OpExecutionMode %main LocalSize 1 1 1
978 OpSource HLSL 670
979 OpName %type_RWByteAddressBuffer "type.RWByteAddressBuffer"
980 OpName %output "output"
981 OpName %main "main"
982 OpName %src_main "src.main"
983 OpName %bb_entry "bb.entry"
984
985 ; CHECK: OpDecorate %output_1_ DescriptorSet 0
986 ; CHECK: OpDecorate %output_1_ Binding 1
987 ; CHECK: OpDecorate %output_0_ DescriptorSet 0
988 ; CHECK: OpDecorate %output_0_ Binding 0
989
990 OpDecorate %output DescriptorSet 0
991 OpDecorate %output Binding 0
992
993 OpDecorate %_runtimearr_uint ArrayStride 4
994 OpMemberDecorate %type_RWByteAddressBuffer 0 Offset 0
995 OpDecorate %type_RWByteAddressBuffer Block
996 %int = OpTypeInt 32 1
997 %int_1 = OpConstant %int 1
998 %uint = OpTypeInt 32 0
999 %uint_0 = OpConstant %uint 0
1000 %uint_2 = OpConstant %uint 2
1001 %uint_32 = OpConstant %uint 32
1002 %_runtimearr_uint = OpTypeRuntimeArray %uint
1003 %type_RWByteAddressBuffer = OpTypeStruct %_runtimearr_uint
1004 %_arr_type_RWByteAddressBuffer_uint_2 = OpTypeArray %type_RWByteAddressBuffer %uint_2
1005 %_ptr_StorageBuffer__arr_type_RWByteAddressBuffer_uint_2 = OpTypePointer StorageBuffer %_arr_type_RWByteAddressBuffer_uint_2
1006 %void = OpTypeVoid
1007 %23 = OpTypeFunction %void
1008 %_ptr_StorageBuffer_type_RWByteAddressBuffer = OpTypePointer StorageBuffer %type_RWByteAddressBuffer
1009 %_ptr_StorageBuffer_uint = OpTypePointer StorageBuffer %uint
1010
1011 ; CHECK: %output_1_ = OpVariable %_ptr_StorageBuffer_type_RWByteAddressBuffer StorageBuffer
1012 ; CHECK: %output_0_ = OpVariable %_ptr_StorageBuffer_type_RWByteAddressBuffer StorageBuffer
1013
1014 %output = OpVariable %_ptr_StorageBuffer__arr_type_RWByteAddressBuffer_uint_2 StorageBuffer
1015
1016 %main = OpFunction %void None %23
1017 %26 = OpLabel
1018 %27 = OpFunctionCall %void %src_main
1019 OpReturn
1020 OpFunctionEnd
1021 %src_main = OpFunction %void None %23
1022 %bb_entry = OpLabel
1023 %28 = OpAccessChain %_ptr_StorageBuffer_type_RWByteAddressBuffer %output %int_1
1024 %29 = OpShiftRightLogical %uint %uint_0 %uint_2
1025 %30 = OpAccessChain %_ptr_StorageBuffer_uint %28 %uint_0 %29
1026 OpStore %30 %uint_32
1027 OpReturn
1028 OpFunctionEnd
1029 )";
1030
1031 SinglePassRunAndMatch<DescriptorScalarReplacement>(
1032 text, false, /* flatten_composites=*/true, /* flatten_arrays=*/true);
1033 }
1034
1035 TEST_F(DescriptorScalarReplacementTest,
1036 ExpandArrayWhenCompositeExpensionIsOff) {
1037 const std::string text = R"(; SPIR-V
1038 ; Version: 1.6
1039 ; Bound: 31
1040 ; Schema: 0
1041 OpCapability Shader
1042 OpMemoryModel Logical GLSL450
1043
1044 ; CHECK: OpEntryPoint GLCompute %main "main" %output_0_ %output_1_
1045
1046 OpEntryPoint GLCompute %main "main" %output
1047 OpExecutionMode %main LocalSize 1 1 1
1048 OpSource HLSL 670
1049 OpName %type_RWByteAddressBuffer "type.RWByteAddressBuffer"
1050 OpName %output "output"
1051 OpName %main "main"
1052 OpName %src_main "src.main"
1053 OpName %bb_entry "bb.entry"
1054
1055 ; CHECK: OpDecorate %output_1_ DescriptorSet 0
1056 ; CHECK: OpDecorate %output_1_ Binding 1
1057 ; CHECK: OpDecorate %output_0_ DescriptorSet 0
1058 ; CHECK: OpDecorate %output_0_ Binding 0
1059
1060 OpDecorate %output DescriptorSet 0
1061 OpDecorate %output Binding 0
1062
1063 OpDecorate %_runtimearr_uint ArrayStride 4
1064 OpMemberDecorate %type_RWByteAddressBuffer 0 Offset 0
1065 OpDecorate %type_RWByteAddressBuffer Block
1066 %int = OpTypeInt 32 1
1067 %int_1 = OpConstant %int 1
1068 %uint = OpTypeInt 32 0
1069 %uint_0 = OpConstant %uint 0
1070 %uint_2 = OpConstant %uint 2
1071 %uint_32 = OpConstant %uint 32
1072 %_runtimearr_uint = OpTypeRuntimeArray %uint
1073 %type_RWByteAddressBuffer = OpTypeStruct %_runtimearr_uint
1074 %_arr_type_RWByteAddressBuffer_uint_2 = OpTypeArray %type_RWByteAddressBuffer %uint_2
1075 %_ptr_StorageBuffer__arr_type_RWByteAddressBuffer_uint_2 = OpTypePointer StorageBuffer %_arr_type_RWByteAddressBuffer_uint_2
1076 %void = OpTypeVoid
1077 %23 = OpTypeFunction %void
1078 %_ptr_StorageBuffer_type_RWByteAddressBuffer = OpTypePointer StorageBuffer %type_RWByteAddressBuffer
1079 %_ptr_StorageBuffer_uint = OpTypePointer StorageBuffer %uint
1080
1081 ; CHECK: %output_1_ = OpVariable %_ptr_StorageBuffer_type_RWByteAddressBuffer StorageBuffer
1082 ; CHECK: %output_0_ = OpVariable %_ptr_StorageBuffer_type_RWByteAddressBuffer StorageBuffer
1083
1084 %output = OpVariable %_ptr_StorageBuffer__arr_type_RWByteAddressBuffer_uint_2 StorageBuffer
1085
1086 %main = OpFunction %void None %23
1087 %26 = OpLabel
1088 %27 = OpFunctionCall %void %src_main
1089 OpReturn
1090 OpFunctionEnd
1091 %src_main = OpFunction %void None %23
1092 %bb_entry = OpLabel
1093 %28 = OpAccessChain %_ptr_StorageBuffer_type_RWByteAddressBuffer %output %int_1
1094 %29 = OpShiftRightLogical %uint %uint_0 %uint_2
1095 %30 = OpAccessChain %_ptr_StorageBuffer_uint %28 %uint_0 %29
1096 OpStore %30 %uint_32
1097 OpReturn
1098 OpFunctionEnd
1099 )";
1100
1101 SinglePassRunAndMatch<DescriptorScalarReplacement>(
1102 text, false, /* flatten_composites=*/false, /* flatten_arrays=*/true);
1103 }
1104
1105 TEST_F(DescriptorScalarReplacementTest, ExpandStructButNotArray) {
1106 const std::string text = R"(; SPIR-V
1107 ; Version: 1.6
1108 ; Generator: Khronos SPIR-V Tools Assembler; 0
1109 ; Bound: 41
1110 ; Schema: 0
1111 OpCapability Shader
1112 OpMemoryModel Logical GLSL450
1113 OpEntryPoint Fragment %main "main" %out_var_SV_Target
1114 OpExecutionMode %main OriginUpperLeft
1115 OpSource HLSL 660
1116 OpName %type_2d_image "type.2d.image"
1117 OpName %Textures "Textures"
1118 OpName %type_sampler "type.sampler"
1119 OpName %out_var_SV_Target "out.var.SV_Target"
1120 OpName %main "main"
1121 OpName %type_sampled_image "type.sampled.image"
1122 OpName %TheStruct "TheStruct"
1123 OpMemberName %StructOfResources 0 "Texture"
1124 OpMemberName %StructOfResources 1 "Sampler"
1125 ; CHECK: OpName %TheStruct_Sampler "TheStruct.Sampler"
1126 ; CHECK: OpName %TheStruct_Texture "TheStruct.Texture"
1127 OpDecorate %out_var_SV_Target Location 0
1128 OpDecorate %Textures DescriptorSet 0
1129 OpDecorate %Textures Binding 0
1130 OpDecorate %TheStruct DescriptorSet 0
1131 OpDecorate %TheStruct Binding 10
1132 ; CHECK: OpDecorate %TheStruct_Sampler DescriptorSet 0
1133 ; CHECK: OpDecorate %TheStruct_Sampler Binding 11
1134 ; CHECK: OpDecorate %TheStruct_Texture DescriptorSet 0
1135 ; CHECK: OpDecorate %TheStruct_Texture Binding 10
1136 %int = OpTypeInt 32 1
1137 %int_0 = OpConstant %int 0
1138 %int_1 = OpConstant %int 1
1139 %float = OpTypeFloat 32
1140 %float_0 = OpConstant %float 0
1141 %v2float = OpTypeVector %float 2
1142 %13 = OpConstantComposite %v2float %float_0 %float_0
1143 %uint = OpTypeInt 32 0
1144 %uint_10 = OpConstant %uint 10
1145 %type_2d_image = OpTypeImage %float 2D 2 0 0 1 Unknown
1146 %_arr_type_2d_image_uint_10 = OpTypeArray %type_2d_image %uint_10
1147 %_ptr_UniformConstant__arr_type_2d_image_uint_10 = OpTypePointer UniformConstant %_arr_type_2d_image_uint_10
1148 %type_sampler = OpTypeSampler
1149 %StructOfResources = OpTypeStruct %type_2d_image %type_sampler
1150 %_ptr_UniformConstant__struct_18 = OpTypePointer UniformConstant %StructOfResources
1151 %v4float = OpTypeVector %float 4
1152 %_ptr_Output_v4float = OpTypePointer Output %v4float
1153 %void = OpTypeVoid
1154 %23 = OpTypeFunction %void
1155 %_ptr_UniformConstant_type_2d_image = OpTypePointer UniformConstant %type_2d_image
1156 %_ptr_UniformConstant_type_sampler = OpTypePointer UniformConstant %type_sampler
1157 %type_sampled_image = OpTypeSampledImage %type_2d_image
1158 %Textures = OpVariable %_ptr_UniformConstant__arr_type_2d_image_uint_10 UniformConstant
1159 %out_var_SV_Target = OpVariable %_ptr_Output_v4float Output
1160 %TheStruct = OpVariable %_ptr_UniformConstant__struct_18 UniformConstant
1161 %main = OpFunction %void None %23
1162 %26 = OpLabel
1163 %27 = OpAccessChain %_ptr_UniformConstant_type_2d_image %Textures %int_0
1164 %28 = OpLoad %type_2d_image %27
1165 %29 = OpAccessChain %_ptr_UniformConstant_type_sampler %TheStruct %int_1
1166 %31 = OpLoad %type_sampler %29
1167 ; CHECK: %31 = OpLoad %type_sampler %TheStruct_Sampler
1168 %32 = OpSampledImage %type_sampled_image %28 %31
1169 %33 = OpImageSampleImplicitLod %v4float %32 %13 None
1170 %34 = OpAccessChain %_ptr_UniformConstant_type_2d_image %TheStruct %int_0
1171 %35 = OpLoad %type_2d_image %34
1172 ; CHECK: %35 = OpLoad %type_2d_image %TheStruct_Texture
1173 %36 = OpAccessChain %_ptr_UniformConstant_type_sampler %TheStruct %int_1
1174 %37 = OpLoad %type_sampler %36
1175 ; CHECK: %37 = OpLoad %type_sampler %TheStruct_Sampler
1176 %38 = OpSampledImage %type_sampled_image %35 %37
1177 %39 = OpImageSampleImplicitLod %v4float %38 %13 None
1178 %40 = OpFAdd %v4float %33 %39
1179 OpStore %out_var_SV_Target %40
1180 OpReturn
1181 OpFunctionEnd
1182 )";
1183
1184 SinglePassRunAndMatch<DescriptorScalarReplacement>(
1185 text, false, /* flatten_composites=*/true, /* flatten_arrays=*/false);
1186 }
1187
1188 } // namespace
1189 } // namespace opt
1190 } // namespace spvtools