Debian Codegraph @

third_party/spirv-tools/test/opt/inst_bindless_check_test.cpp

raw
1 // Copyright (c) 2017 Valve Corporation
2 // Copyright (c) 2017 LunarG Inc.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15
16 // Bindless Check Instrumentation Tests.
17
18 #include <string>
19 #include <vector>
20
21 #include "test/opt/assembly_builder.h"
22 #include "test/opt/pass_fixture.h"
23 #include "test/opt/pass_utils.h"
24
25 namespace spvtools {
26 namespace opt {
27 namespace {
28
29 using InstBindlessTest = PassTest<::testing::Test>;
30
31 TEST_F(InstBindlessTest, NoInstrumentConstIndexInbounds) {
32 // Texture2D g_tColor[128];
33 //
34 // SamplerState g_sAniso;
35 //
36 // struct PS_INPUT
37 // {
38 // float2 vTextureCoords : TEXCOORD2;
39 // };
40 //
41 // struct PS_OUTPUT
42 // {
43 // float4 vColor : SV_Target0;
44 // };
45 //
46 // PS_OUTPUT MainPs(PS_INPUT i)
47 // {
48 // PS_OUTPUT ps_output;
49 //
50 // ps_output.vColor = g_tColor[ 37 ].Sample(g_sAniso, i.vTextureCoords.xy);
51 // return ps_output;
52 // }
53
54 const std::string before =
55 R"(OpCapability Shader
56 %1 = OpExtInstImport "GLSL.std.450"
57 OpMemoryModel Logical GLSL450
58 OpEntryPoint Fragment %MainPs "MainPs" %i_vTextureCoords %_entryPointOutput_vColor
59 OpExecutionMode %MainPs OriginUpperLeft
60 OpSource HLSL 500
61 OpName %MainPs "MainPs"
62 OpName %g_tColor "g_tColor"
63 OpName %g_sAniso "g_sAniso"
64 OpName %i_vTextureCoords "i.vTextureCoords"
65 OpName %_entryPointOutput_vColor "@entryPointOutput.vColor"
66 OpDecorate %g_tColor DescriptorSet 3
67 OpDecorate %g_tColor Binding 0
68 OpDecorate %g_sAniso DescriptorSet 0
69 OpDecorate %i_vTextureCoords Location 0
70 OpDecorate %_entryPointOutput_vColor Location 0
71 %void = OpTypeVoid
72 %8 = OpTypeFunction %void
73 %float = OpTypeFloat 32
74 %v2float = OpTypeVector %float 2
75 %v4float = OpTypeVector %float 4
76 %int = OpTypeInt 32 1
77 %int_0 = OpConstant %int 0
78 %int_37 = OpConstant %int 37
79 %15 = OpTypeImage %float 2D 0 0 0 1 Unknown
80 %uint = OpTypeInt 32 0
81 %uint_128 = OpConstant %uint 128
82 %_arr_15_uint_128 = OpTypeArray %15 %uint_128
83 %_ptr_UniformConstant__arr_15_uint_128 = OpTypePointer UniformConstant %_arr_15_uint_128
84 %g_tColor = OpVariable %_ptr_UniformConstant__arr_15_uint_128 UniformConstant
85 %_ptr_UniformConstant_15 = OpTypePointer UniformConstant %15
86 %21 = OpTypeSampler
87 %_ptr_UniformConstant_21 = OpTypePointer UniformConstant %21
88 %g_sAniso = OpVariable %_ptr_UniformConstant_21 UniformConstant
89 %23 = OpTypeSampledImage %15
90 %_ptr_Input_v2float = OpTypePointer Input %v2float
91 %i_vTextureCoords = OpVariable %_ptr_Input_v2float Input
92 %_ptr_Output_v4float = OpTypePointer Output %v4float
93 %_entryPointOutput_vColor = OpVariable %_ptr_Output_v4float Output
94 %MainPs = OpFunction %void None %8
95 %26 = OpLabel
96 %27 = OpLoad %v2float %i_vTextureCoords
97 %28 = OpAccessChain %_ptr_UniformConstant_15 %g_tColor %int_37
98 %29 = OpLoad %15 %28
99 %30 = OpLoad %21 %g_sAniso
100 %31 = OpSampledImage %23 %29 %30
101 %32 = OpImageSampleImplicitLod %v4float %31 %27
102 OpStore %_entryPointOutput_vColor %32
103 OpReturn
104 OpFunctionEnd
105 )";
106
107 SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
108 SinglePassRunAndCheck<InstBindlessCheckPass>(before, before, true, true, 7u,
109 23u, false, false);
110 }
111
112 TEST_F(InstBindlessTest, NoInstrumentNonBindless) {
113 // This test verifies that the pass will correctly not instrument vanilla
114 // texture sample.
115 //
116 // Texture2D g_tColor;
117 //
118 // SamplerState g_sAniso;
119 //
120 // struct PS_INPUT
121 // {
122 // float2 vTextureCoords : TEXCOORD2;
123 // };
124 //
125 // struct PS_OUTPUT
126 // {
127 // float4 vColor : SV_Target0;
128 // };
129 //
130 // PS_OUTPUT MainPs(PS_INPUT i)
131 // {
132 // PS_OUTPUT ps_output;
133 // ps_output.vColor =
134 // g_tColor.Sample(g_sAniso, i.vTextureCoords.xy);
135 // return ps_output;
136 // }
137
138 const std::string whole_file =
139 R"(OpCapability Shader
140 %1 = OpExtInstImport "GLSL.std.450"
141 OpMemoryModel Logical GLSL450
142 OpEntryPoint Fragment %MainPs "MainPs" %i_vTextureCoords %_entryPointOutput_vColor
143 OpExecutionMode %MainPs OriginUpperLeft
144 OpSource HLSL 500
145 OpName %MainPs "MainPs"
146 OpName %g_tColor "g_tColor"
147 OpName %g_sAniso "g_sAniso"
148 OpName %i_vTextureCoords "i.vTextureCoords"
149 OpName %_entryPointOutput_vColor "@entryPointOutput.vColor"
150 OpDecorate %g_tColor DescriptorSet 0
151 OpDecorate %g_tColor Binding 0
152 OpDecorate %g_sAniso DescriptorSet 0
153 OpDecorate %g_sAniso Binding 0
154 OpDecorate %i_vTextureCoords Location 0
155 OpDecorate %_entryPointOutput_vColor Location 0
156 %void = OpTypeVoid
157 %8 = OpTypeFunction %void
158 %float = OpTypeFloat 32
159 %v2float = OpTypeVector %float 2
160 %v4float = OpTypeVector %float 4
161 %12 = OpTypeImage %float 2D 0 0 0 1 Unknown
162 %_ptr_UniformConstant_12 = OpTypePointer UniformConstant %12
163 %g_tColor = OpVariable %_ptr_UniformConstant_12 UniformConstant
164 %14 = OpTypeSampler
165 %_ptr_UniformConstant_14 = OpTypePointer UniformConstant %14
166 %g_sAniso = OpVariable %_ptr_UniformConstant_14 UniformConstant
167 %16 = OpTypeSampledImage %12
168 %_ptr_Input_v2float = OpTypePointer Input %v2float
169 %i_vTextureCoords = OpVariable %_ptr_Input_v2float Input
170 %_ptr_Output_v4float = OpTypePointer Output %v4float
171 %_entryPointOutput_vColor = OpVariable %_ptr_Output_v4float Output
172 %MainPs = OpFunction %void None %8
173 %19 = OpLabel
174 %20 = OpLoad %v2float %i_vTextureCoords
175 %21 = OpLoad %12 %g_tColor
176 %22 = OpLoad %14 %g_sAniso
177 %23 = OpSampledImage %16 %21 %22
178 %24 = OpImageSampleImplicitLod %v4float %23 %20
179 OpStore %_entryPointOutput_vColor %24
180 OpReturn
181 OpFunctionEnd
182 )";
183
184 // SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
185 SinglePassRunAndCheck<InstBindlessCheckPass>(whole_file, whole_file, true,
186 true, 7u, 23u, false, false);
187 }
188
189 TEST_F(InstBindlessTest, Simple) {
190 // Texture2D g_tColor[128];
191 //
192 // layout(push_constant) cbuffer PerViewConstantBuffer_t
193 // {
194 // uint g_nDataIdx;
195 // };
196 //
197 // SamplerState g_sAniso;
198 //
199 // struct PS_INPUT
200 // {
201 // float2 vTextureCoords : TEXCOORD2;
202 // };
203 //
204 // struct PS_OUTPUT
205 // {
206 // float4 vColor : SV_Target0;
207 // };
208 //
209 // PS_OUTPUT MainPs(PS_INPUT i)
210 // {
211 // PS_OUTPUT ps_output;
212 // ps_output.vColor =
213 // g_tColor[ g_nDataIdx ].Sample(g_sAniso, i.vTextureCoords.xy);
214 // return ps_output;
215 // }
216
217 const std::string entry_before =
218 R"(OpCapability Shader
219 %1 = OpExtInstImport "GLSL.std.450"
220 OpMemoryModel Logical GLSL450
221 OpEntryPoint Fragment %MainPs "MainPs" %i_vTextureCoords %_entryPointOutput_vColor
222 OpExecutionMode %MainPs OriginUpperLeft
223 OpSource HLSL 500
224 )";
225
226 const std::string entry_after =
227 R"(OpCapability Shader
228 OpExtension "SPV_KHR_storage_buffer_storage_class"
229 %1 = OpExtInstImport "GLSL.std.450"
230 OpMemoryModel Logical GLSL450
231 OpEntryPoint Fragment %MainPs "MainPs" %i_vTextureCoords %_entryPointOutput_vColor %gl_FragCoord
232 OpExecutionMode %MainPs OriginUpperLeft
233 OpSource HLSL 500
234 )";
235
236 const std::string names_annots =
237 R"(OpName %MainPs "MainPs"
238 OpName %g_tColor "g_tColor"
239 OpName %PerViewConstantBuffer_t "PerViewConstantBuffer_t"
240 OpMemberName %PerViewConstantBuffer_t 0 "g_nDataIdx"
241 OpName %_ ""
242 OpName %g_sAniso "g_sAniso"
243 OpName %i_vTextureCoords "i.vTextureCoords"
244 OpName %_entryPointOutput_vColor "@entryPointOutput.vColor"
245 OpDecorate %g_tColor DescriptorSet 3
246 OpDecorate %g_tColor Binding 0
247 OpMemberDecorate %PerViewConstantBuffer_t 0 Offset 0
248 OpDecorate %PerViewConstantBuffer_t Block
249 OpDecorate %g_sAniso DescriptorSet 0
250 OpDecorate %i_vTextureCoords Location 0
251 OpDecorate %_entryPointOutput_vColor Location 0
252 )";
253
254 const std::string new_annots =
255 R"(OpDecorate %_runtimearr_uint ArrayStride 4
256 OpDecorate %_struct_55 Block
257 OpMemberDecorate %_struct_55 0 Offset 0
258 OpMemberDecorate %_struct_55 1 Offset 4
259 OpDecorate %57 DescriptorSet 7
260 OpDecorate %57 Binding 0
261 OpDecorate %gl_FragCoord BuiltIn FragCoord
262 )";
263
264 const std::string consts_types_vars =
265 R"(%void = OpTypeVoid
266 %10 = OpTypeFunction %void
267 %float = OpTypeFloat 32
268 %v2float = OpTypeVector %float 2
269 %v4float = OpTypeVector %float 4
270 %int = OpTypeInt 32 1
271 %int_0 = OpConstant %int 0
272 %16 = OpTypeImage %float 2D 0 0 0 1 Unknown
273 %uint = OpTypeInt 32 0
274 %uint_128 = OpConstant %uint 128
275 %_arr_16_uint_128 = OpTypeArray %16 %uint_128
276 %_ptr_UniformConstant__arr_16_uint_128 = OpTypePointer UniformConstant %_arr_16_uint_128
277 %g_tColor = OpVariable %_ptr_UniformConstant__arr_16_uint_128 UniformConstant
278 %PerViewConstantBuffer_t = OpTypeStruct %uint
279 %_ptr_PushConstant_PerViewConstantBuffer_t = OpTypePointer PushConstant %PerViewConstantBuffer_t
280 %_ = OpVariable %_ptr_PushConstant_PerViewConstantBuffer_t PushConstant
281 %_ptr_PushConstant_uint = OpTypePointer PushConstant %uint
282 %_ptr_UniformConstant_16 = OpTypePointer UniformConstant %16
283 %24 = OpTypeSampler
284 %_ptr_UniformConstant_24 = OpTypePointer UniformConstant %24
285 %g_sAniso = OpVariable %_ptr_UniformConstant_24 UniformConstant
286 %26 = OpTypeSampledImage %16
287 %_ptr_Input_v2float = OpTypePointer Input %v2float
288 %i_vTextureCoords = OpVariable %_ptr_Input_v2float Input
289 %_ptr_Output_v4float = OpTypePointer Output %v4float
290 %_entryPointOutput_vColor = OpVariable %_ptr_Output_v4float Output
291 )";
292
293 const std::string new_consts_types_vars =
294 R"(%uint_0 = OpConstant %uint 0
295 %bool = OpTypeBool
296 %48 = OpTypeFunction %void %uint %uint %uint %uint
297 %_runtimearr_uint = OpTypeRuntimeArray %uint
298 %_struct_55 = OpTypeStruct %uint %_runtimearr_uint
299 %_ptr_StorageBuffer__struct_55 = OpTypePointer StorageBuffer %_struct_55
300 %57 = OpVariable %_ptr_StorageBuffer__struct_55 StorageBuffer
301 %_ptr_StorageBuffer_uint = OpTypePointer StorageBuffer %uint
302 %uint_10 = OpConstant %uint 10
303 %uint_4 = OpConstant %uint 4
304 %uint_1 = OpConstant %uint 1
305 %uint_23 = OpConstant %uint 23
306 %uint_2 = OpConstant %uint 2
307 %uint_3 = OpConstant %uint 3
308 %_ptr_Input_v4float = OpTypePointer Input %v4float
309 %gl_FragCoord = OpVariable %_ptr_Input_v4float Input
310 %v4uint = OpTypeVector %uint 4
311 %uint_5 = OpConstant %uint 5
312 %uint_7 = OpConstant %uint 7
313 %uint_8 = OpConstant %uint 8
314 %uint_9 = OpConstant %uint 9
315 %uint_56 = OpConstant %uint 56
316 %103 = OpConstantNull %v4float
317 )";
318
319 const std::string func_pt1 =
320 R"(%MainPs = OpFunction %void None %10
321 %29 = OpLabel
322 %30 = OpLoad %v2float %i_vTextureCoords
323 %31 = OpAccessChain %_ptr_PushConstant_uint %_ %int_0
324 %32 = OpLoad %uint %31
325 %33 = OpAccessChain %_ptr_UniformConstant_16 %g_tColor %32
326 %34 = OpLoad %16 %33
327 %35 = OpLoad %24 %g_sAniso
328 %36 = OpSampledImage %26 %34 %35
329 )";
330
331 const std::string func_pt2_before =
332 R"(%37 = OpImageSampleImplicitLod %v4float %36 %30
333 OpStore %_entryPointOutput_vColor %37
334 OpReturn
335 OpFunctionEnd
336 )";
337
338 const std::string func_pt2_after =
339 R"(%40 = OpULessThan %bool %32 %uint_128
340 OpSelectionMerge %41 None
341 OpBranchConditional %40 %42 %43
342 %42 = OpLabel
343 %44 = OpLoad %16 %33
344 %45 = OpSampledImage %26 %44 %35
345 %46 = OpImageSampleImplicitLod %v4float %45 %30
346 OpBranch %41
347 %43 = OpLabel
348 %102 = OpFunctionCall %void %47 %uint_56 %uint_0 %32 %uint_128
349 OpBranch %41
350 %41 = OpLabel
351 %104 = OpPhi %v4float %46 %42 %103 %43
352 OpStore %_entryPointOutput_vColor %104
353 OpReturn
354 OpFunctionEnd
355 )";
356
357 const std::string output_func =
358 R"(%47 = OpFunction %void None %48
359 %49 = OpFunctionParameter %uint
360 %50 = OpFunctionParameter %uint
361 %51 = OpFunctionParameter %uint
362 %52 = OpFunctionParameter %uint
363 %53 = OpLabel
364 %59 = OpAccessChain %_ptr_StorageBuffer_uint %57 %uint_0
365 %62 = OpAtomicIAdd %uint %59 %uint_4 %uint_0 %uint_10
366 %63 = OpIAdd %uint %62 %uint_10
367 %64 = OpArrayLength %uint %57 1
368 %65 = OpULessThanEqual %bool %63 %64
369 OpSelectionMerge %66 None
370 OpBranchConditional %65 %67 %66
371 %67 = OpLabel
372 %68 = OpIAdd %uint %62 %uint_0
373 %70 = OpAccessChain %_ptr_StorageBuffer_uint %57 %uint_1 %68
374 OpStore %70 %uint_10
375 %72 = OpIAdd %uint %62 %uint_1
376 %73 = OpAccessChain %_ptr_StorageBuffer_uint %57 %uint_1 %72
377 OpStore %73 %uint_23
378 %75 = OpIAdd %uint %62 %uint_2
379 %76 = OpAccessChain %_ptr_StorageBuffer_uint %57 %uint_1 %75
380 OpStore %76 %49
381 %78 = OpIAdd %uint %62 %uint_3
382 %79 = OpAccessChain %_ptr_StorageBuffer_uint %57 %uint_1 %78
383 OpStore %79 %uint_4
384 %82 = OpLoad %v4float %gl_FragCoord
385 %84 = OpBitcast %v4uint %82
386 %85 = OpCompositeExtract %uint %84 0
387 %86 = OpIAdd %uint %62 %uint_4
388 %87 = OpAccessChain %_ptr_StorageBuffer_uint %57 %uint_1 %86
389 OpStore %87 %85
390 %88 = OpCompositeExtract %uint %84 1
391 %90 = OpIAdd %uint %62 %uint_5
392 %91 = OpAccessChain %_ptr_StorageBuffer_uint %57 %uint_1 %90
393 OpStore %91 %88
394 %93 = OpIAdd %uint %62 %uint_7
395 %94 = OpAccessChain %_ptr_StorageBuffer_uint %57 %uint_1 %93
396 OpStore %94 %50
397 %96 = OpIAdd %uint %62 %uint_8
398 %97 = OpAccessChain %_ptr_StorageBuffer_uint %57 %uint_1 %96
399 OpStore %97 %51
400 %99 = OpIAdd %uint %62 %uint_9
401 %100 = OpAccessChain %_ptr_StorageBuffer_uint %57 %uint_1 %99
402 OpStore %100 %52
403 OpBranch %66
404 %66 = OpLabel
405 OpReturn
406 OpFunctionEnd
407 )";
408
409 SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
410 SinglePassRunAndCheck<InstBindlessCheckPass, uint32_t, uint32_t, bool, bool>(
411 entry_before + names_annots + consts_types_vars + func_pt1 +
412 func_pt2_before,
413 entry_after + names_annots + new_annots + consts_types_vars +
414 new_consts_types_vars + func_pt1 + func_pt2_after + output_func,
415 true, true, 7u, 23u, false, false);
416 }
417
418 TEST_F(InstBindlessTest, InstrumentMultipleInstructions) {
419 // Texture2D g_tColor[128];
420 //
421 // layout(push_constant) cbuffer PerViewConstantBuffer_t
422 // {
423 // uint g_nDataIdx;
424 // uint g_nDataIdx2;
425 // };
426 //
427 // SamplerState g_sAniso;
428 //
429 // struct PS_INPUT
430 // {
431 // float2 vTextureCoords : TEXCOORD2;
432 // };
433 //
434 // struct PS_OUTPUT
435 // {
436 // float4 vColor : SV_Target0;
437 // };
438 //
439 // PS_OUTPUT MainPs(PS_INPUT i)
440 // {
441 // PS_OUTPUT ps_output;
442 //
443 // float t = g_tColor[g_nDataIdx ].Sample(g_sAniso, i.vTextureCoords.xy);
444 // float t2 = g_tColor[g_nDataIdx2].Sample(g_sAniso, i.vTextureCoords.xy);
445 // ps_output.vColor = t + t2;
446 // return ps_output;
447 // }
448
449 const std::string defs_before =
450 R"(OpCapability Shader
451 %1 = OpExtInstImport "GLSL.std.450"
452 OpMemoryModel Logical GLSL450
453 OpEntryPoint Fragment %MainPs "MainPs" %i_vTextureCoords %_entryPointOutput_vColor
454 OpExecutionMode %MainPs OriginUpperLeft
455 OpSource HLSL 500
456 OpName %MainPs "MainPs"
457 OpName %g_tColor "g_tColor"
458 OpName %PerViewConstantBuffer_t "PerViewConstantBuffer_t"
459 OpMemberName %PerViewConstantBuffer_t 0 "g_nDataIdx"
460 OpName %_ ""
461 OpName %g_sAniso "g_sAniso"
462 OpName %i_vTextureCoords "i.vTextureCoords"
463 OpName %_entryPointOutput_vColor "@entryPointOutput.vColor"
464 OpDecorate %g_tColor DescriptorSet 3
465 OpDecorate %g_tColor Binding 0
466 OpMemberDecorate %PerViewConstantBuffer_t 0 Offset 0
467 OpMemberDecorate %PerViewConstantBuffer_t 1 Offset 4
468 OpDecorate %PerViewConstantBuffer_t Block
469 OpDecorate %g_sAniso DescriptorSet 0
470 OpDecorate %i_vTextureCoords Location 0
471 OpDecorate %_entryPointOutput_vColor Location 0
472 %void = OpTypeVoid
473 %10 = OpTypeFunction %void
474 %float = OpTypeFloat 32
475 %v2float = OpTypeVector %float 2
476 %v4float = OpTypeVector %float 4
477 %int = OpTypeInt 32 1
478 %int_0 = OpConstant %int 0
479 %int_1 = OpConstant %int 1
480 %17 = OpTypeImage %float 2D 0 0 0 1 Unknown
481 %uint = OpTypeInt 32 0
482 %uint_128 = OpConstant %uint 128
483 %_arr_17_uint_128 = OpTypeArray %17 %uint_128
484 %_ptr_UniformConstant__arr_17_uint_128 = OpTypePointer UniformConstant %_arr_17_uint_128
485 %g_tColor = OpVariable %_ptr_UniformConstant__arr_17_uint_128 UniformConstant
486 %PerViewConstantBuffer_t = OpTypeStruct %uint %uint
487 %_ptr_PushConstant_PerViewConstantBuffer_t = OpTypePointer PushConstant %PerViewConstantBuffer_t
488 %_ = OpVariable %_ptr_PushConstant_PerViewConstantBuffer_t PushConstant
489 %_ptr_PushConstant_uint = OpTypePointer PushConstant %uint
490 %_ptr_UniformConstant_17 = OpTypePointer UniformConstant %17
491 %25 = OpTypeSampler
492 %_ptr_UniformConstant_25 = OpTypePointer UniformConstant %25
493 %g_sAniso = OpVariable %_ptr_UniformConstant_25 UniformConstant
494 %27 = OpTypeSampledImage %17
495 %_ptr_Input_v2float = OpTypePointer Input %v2float
496 %i_vTextureCoords = OpVariable %_ptr_Input_v2float Input
497 %_ptr_Output_v4float = OpTypePointer Output %v4float
498 %_entryPointOutput_vColor = OpVariable %_ptr_Output_v4float Output
499 )";
500
501 const std::string defs_after =
502 R"(OpCapability Shader
503 OpExtension "SPV_KHR_storage_buffer_storage_class"
504 %1 = OpExtInstImport "GLSL.std.450"
505 OpMemoryModel Logical GLSL450
506 OpEntryPoint Fragment %MainPs "MainPs" %i_vTextureCoords %_entryPointOutput_vColor %gl_FragCoord
507 OpExecutionMode %MainPs OriginUpperLeft
508 OpSource HLSL 500
509 OpName %MainPs "MainPs"
510 OpName %g_tColor "g_tColor"
511 OpName %PerViewConstantBuffer_t "PerViewConstantBuffer_t"
512 OpMemberName %PerViewConstantBuffer_t 0 "g_nDataIdx"
513 OpName %_ ""
514 OpName %g_sAniso "g_sAniso"
515 OpName %i_vTextureCoords "i.vTextureCoords"
516 OpName %_entryPointOutput_vColor "@entryPointOutput.vColor"
517 OpDecorate %g_tColor DescriptorSet 3
518 OpDecorate %g_tColor Binding 0
519 OpMemberDecorate %PerViewConstantBuffer_t 0 Offset 0
520 OpMemberDecorate %PerViewConstantBuffer_t 1 Offset 4
521 OpDecorate %PerViewConstantBuffer_t Block
522 OpDecorate %g_sAniso DescriptorSet 0
523 OpDecorate %i_vTextureCoords Location 0
524 OpDecorate %_entryPointOutput_vColor Location 0
525 OpDecorate %_runtimearr_uint ArrayStride 4
526 OpDecorate %_struct_63 Block
527 OpMemberDecorate %_struct_63 0 Offset 0
528 OpMemberDecorate %_struct_63 1 Offset 4
529 OpDecorate %65 DescriptorSet 7
530 OpDecorate %65 Binding 0
531 OpDecorate %gl_FragCoord BuiltIn FragCoord
532 %void = OpTypeVoid
533 %10 = OpTypeFunction %void
534 %float = OpTypeFloat 32
535 %v2float = OpTypeVector %float 2
536 %v4float = OpTypeVector %float 4
537 %int = OpTypeInt 32 1
538 %int_0 = OpConstant %int 0
539 %int_1 = OpConstant %int 1
540 %17 = OpTypeImage %float 2D 0 0 0 1 Unknown
541 %uint = OpTypeInt 32 0
542 %uint_128 = OpConstant %uint 128
543 %_arr_17_uint_128 = OpTypeArray %17 %uint_128
544 %_ptr_UniformConstant__arr_17_uint_128 = OpTypePointer UniformConstant %_arr_17_uint_128
545 %g_tColor = OpVariable %_ptr_UniformConstant__arr_17_uint_128 UniformConstant
546 %PerViewConstantBuffer_t = OpTypeStruct %uint %uint
547 %_ptr_PushConstant_PerViewConstantBuffer_t = OpTypePointer PushConstant %PerViewConstantBuffer_t
548 %_ = OpVariable %_ptr_PushConstant_PerViewConstantBuffer_t PushConstant
549 %_ptr_PushConstant_uint = OpTypePointer PushConstant %uint
550 %_ptr_UniformConstant_17 = OpTypePointer UniformConstant %17
551 %25 = OpTypeSampler
552 %_ptr_UniformConstant_25 = OpTypePointer UniformConstant %25
553 %g_sAniso = OpVariable %_ptr_UniformConstant_25 UniformConstant
554 %27 = OpTypeSampledImage %17
555 %_ptr_Input_v2float = OpTypePointer Input %v2float
556 %i_vTextureCoords = OpVariable %_ptr_Input_v2float Input
557 %_ptr_Output_v4float = OpTypePointer Output %v4float
558 %_entryPointOutput_vColor = OpVariable %_ptr_Output_v4float Output
559 %uint_0 = OpConstant %uint 0
560 %bool = OpTypeBool
561 %56 = OpTypeFunction %void %uint %uint %uint %uint
562 %_runtimearr_uint = OpTypeRuntimeArray %uint
563 %_struct_63 = OpTypeStruct %uint %_runtimearr_uint
564 %_ptr_StorageBuffer__struct_63 = OpTypePointer StorageBuffer %_struct_63
565 %65 = OpVariable %_ptr_StorageBuffer__struct_63 StorageBuffer
566 %_ptr_StorageBuffer_uint = OpTypePointer StorageBuffer %uint
567 %uint_10 = OpConstant %uint 10
568 %uint_4 = OpConstant %uint 4
569 %uint_1 = OpConstant %uint 1
570 %uint_23 = OpConstant %uint 23
571 %uint_2 = OpConstant %uint 2
572 %uint_3 = OpConstant %uint 3
573 %_ptr_Input_v4float = OpTypePointer Input %v4float
574 %gl_FragCoord = OpVariable %_ptr_Input_v4float Input
575 %v4uint = OpTypeVector %uint 4
576 %uint_5 = OpConstant %uint 5
577 %uint_7 = OpConstant %uint 7
578 %uint_8 = OpConstant %uint 8
579 %uint_9 = OpConstant %uint 9
580 %uint_58 = OpConstant %uint 58
581 %111 = OpConstantNull %v4float
582 %uint_64 = OpConstant %uint 64
583 )";
584
585 const std::string func_before =
586 R"(%MainPs = OpFunction %void None %10
587 %30 = OpLabel
588 %31 = OpLoad %v2float %i_vTextureCoords
589 %32 = OpAccessChain %_ptr_PushConstant_uint %_ %int_0
590 %33 = OpLoad %uint %32
591 %34 = OpAccessChain %_ptr_UniformConstant_17 %g_tColor %33
592 %35 = OpLoad %17 %34
593 %36 = OpLoad %25 %g_sAniso
594 %37 = OpSampledImage %27 %35 %36
595 %38 = OpImageSampleImplicitLod %v4float %37 %31
596 %39 = OpAccessChain %_ptr_PushConstant_uint %_ %int_1
597 %40 = OpLoad %uint %39
598 %41 = OpAccessChain %_ptr_UniformConstant_17 %g_tColor %40
599 %42 = OpLoad %17 %41
600 %43 = OpSampledImage %27 %42 %36
601 %44 = OpImageSampleImplicitLod %v4float %43 %31
602 %45 = OpFAdd %v4float %38 %44
603 OpStore %_entryPointOutput_vColor %45
604 OpReturn
605 OpFunctionEnd
606 )";
607
608 const std::string func_after =
609 R"(%MainPs = OpFunction %void None %10
610 %30 = OpLabel
611 %31 = OpLoad %v2float %i_vTextureCoords
612 %32 = OpAccessChain %_ptr_PushConstant_uint %_ %int_0
613 %33 = OpLoad %uint %32
614 %34 = OpAccessChain %_ptr_UniformConstant_17 %g_tColor %33
615 %35 = OpLoad %17 %34
616 %36 = OpLoad %25 %g_sAniso
617 %37 = OpSampledImage %27 %35 %36
618 %48 = OpULessThan %bool %33 %uint_128
619 OpSelectionMerge %49 None
620 OpBranchConditional %48 %50 %51
621 %50 = OpLabel
622 %52 = OpLoad %17 %34
623 %53 = OpSampledImage %27 %52 %36
624 %54 = OpImageSampleImplicitLod %v4float %53 %31
625 OpBranch %49
626 %51 = OpLabel
627 %110 = OpFunctionCall %void %55 %uint_58 %uint_0 %33 %uint_128
628 OpBranch %49
629 %49 = OpLabel
630 %112 = OpPhi %v4float %54 %50 %111 %51
631 %39 = OpAccessChain %_ptr_PushConstant_uint %_ %int_1
632 %40 = OpLoad %uint %39
633 %41 = OpAccessChain %_ptr_UniformConstant_17 %g_tColor %40
634 %42 = OpLoad %17 %41
635 %43 = OpSampledImage %27 %42 %36
636 %113 = OpULessThan %bool %40 %uint_128
637 OpSelectionMerge %114 None
638 OpBranchConditional %113 %115 %116
639 %115 = OpLabel
640 %117 = OpLoad %17 %41
641 %118 = OpSampledImage %27 %117 %36
642 %119 = OpImageSampleImplicitLod %v4float %118 %31
643 OpBranch %114
644 %116 = OpLabel
645 %121 = OpFunctionCall %void %55 %uint_64 %uint_0 %40 %uint_128
646 OpBranch %114
647 %114 = OpLabel
648 %122 = OpPhi %v4float %119 %115 %111 %116
649 %45 = OpFAdd %v4float %112 %122
650 OpStore %_entryPointOutput_vColor %45
651 OpReturn
652 OpFunctionEnd
653 )";
654
655 const std::string output_func =
656 R"(%55 = OpFunction %void None %56
657 %57 = OpFunctionParameter %uint
658 %58 = OpFunctionParameter %uint
659 %59 = OpFunctionParameter %uint
660 %60 = OpFunctionParameter %uint
661 %61 = OpLabel
662 %67 = OpAccessChain %_ptr_StorageBuffer_uint %65 %uint_0
663 %70 = OpAtomicIAdd %uint %67 %uint_4 %uint_0 %uint_10
664 %71 = OpIAdd %uint %70 %uint_10
665 %72 = OpArrayLength %uint %65 1
666 %73 = OpULessThanEqual %bool %71 %72
667 OpSelectionMerge %74 None
668 OpBranchConditional %73 %75 %74
669 %75 = OpLabel
670 %76 = OpIAdd %uint %70 %uint_0
671 %78 = OpAccessChain %_ptr_StorageBuffer_uint %65 %uint_1 %76
672 OpStore %78 %uint_10
673 %80 = OpIAdd %uint %70 %uint_1
674 %81 = OpAccessChain %_ptr_StorageBuffer_uint %65 %uint_1 %80
675 OpStore %81 %uint_23
676 %83 = OpIAdd %uint %70 %uint_2
677 %84 = OpAccessChain %_ptr_StorageBuffer_uint %65 %uint_1 %83
678 OpStore %84 %57
679 %86 = OpIAdd %uint %70 %uint_3
680 %87 = OpAccessChain %_ptr_StorageBuffer_uint %65 %uint_1 %86
681 OpStore %87 %uint_4
682 %90 = OpLoad %v4float %gl_FragCoord
683 %92 = OpBitcast %v4uint %90
684 %93 = OpCompositeExtract %uint %92 0
685 %94 = OpIAdd %uint %70 %uint_4
686 %95 = OpAccessChain %_ptr_StorageBuffer_uint %65 %uint_1 %94
687 OpStore %95 %93
688 %96 = OpCompositeExtract %uint %92 1
689 %98 = OpIAdd %uint %70 %uint_5
690 %99 = OpAccessChain %_ptr_StorageBuffer_uint %65 %uint_1 %98
691 OpStore %99 %96
692 %101 = OpIAdd %uint %70 %uint_7
693 %102 = OpAccessChain %_ptr_StorageBuffer_uint %65 %uint_1 %101
694 OpStore %102 %58
695 %104 = OpIAdd %uint %70 %uint_8
696 %105 = OpAccessChain %_ptr_StorageBuffer_uint %65 %uint_1 %104
697 OpStore %105 %59
698 %107 = OpIAdd %uint %70 %uint_9
699 %108 = OpAccessChain %_ptr_StorageBuffer_uint %65 %uint_1 %107
700 OpStore %108 %60
701 OpBranch %74
702 %74 = OpLabel
703 OpReturn
704 OpFunctionEnd
705 )";
706
707 SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
708 SinglePassRunAndCheck<InstBindlessCheckPass>(
709 defs_before + func_before, defs_after + func_after + output_func, true,
710 true, 7u, 23u, false, false);
711 }
712
713 TEST_F(InstBindlessTest, InstrumentOpImage) {
714 // This test verifies that the pass will correctly instrument shader
715 // using OpImage. This test was created by editing the SPIR-V
716 // from the Simple test.
717
718 const std::string defs_before =
719 R"(OpCapability Shader
720 OpCapability StorageImageReadWithoutFormat
721 %1 = OpExtInstImport "GLSL.std.450"
722 OpMemoryModel Logical GLSL450
723 OpEntryPoint Fragment %MainPs "MainPs" %i_vTextureCoords %_entryPointOutput_vColor
724 OpExecutionMode %MainPs OriginUpperLeft
725 OpSource HLSL 500
726 OpName %MainPs "MainPs"
727 OpName %g_tColor "g_tColor"
728 OpName %PerViewConstantBuffer_t "PerViewConstantBuffer_t"
729 OpMemberName %PerViewConstantBuffer_t 0 "g_nDataIdx"
730 OpName %_ ""
731 OpName %i_vTextureCoords "i.vTextureCoords"
732 OpName %_entryPointOutput_vColor "@entryPointOutput.vColor"
733 OpDecorate %g_tColor DescriptorSet 3
734 OpDecorate %g_tColor Binding 0
735 OpMemberDecorate %PerViewConstantBuffer_t 0 Offset 0
736 OpDecorate %PerViewConstantBuffer_t Block
737 OpDecorate %i_vTextureCoords Location 0
738 OpDecorate %_entryPointOutput_vColor Location 0
739 %void = OpTypeVoid
740 %3 = OpTypeFunction %void
741 %float = OpTypeFloat 32
742 %v4float = OpTypeVector %float 4
743 %int = OpTypeInt 32 1
744 %v2int = OpTypeVector %int 2
745 %int_0 = OpConstant %int 0
746 %20 = OpTypeImage %float 2D 0 0 0 0 Unknown
747 %uint = OpTypeInt 32 0
748 %uint_128 = OpConstant %uint 128
749 %39 = OpTypeSampledImage %20
750 %_arr_39_uint_128 = OpTypeArray %39 %uint_128
751 %_ptr_UniformConstant__arr_39_uint_128 = OpTypePointer UniformConstant %_arr_39_uint_128
752 %g_tColor = OpVariable %_ptr_UniformConstant__arr_39_uint_128 UniformConstant
753 %PerViewConstantBuffer_t = OpTypeStruct %uint
754 %_ptr_PushConstant_PerViewConstantBuffer_t = OpTypePointer PushConstant %PerViewConstantBuffer_t
755 %_ = OpVariable %_ptr_PushConstant_PerViewConstantBuffer_t PushConstant
756 %_ptr_PushConstant_uint = OpTypePointer PushConstant %uint
757 %_ptr_UniformConstant_39 = OpTypePointer UniformConstant %39
758 %_ptr_Input_v2int = OpTypePointer Input %v2int
759 %i_vTextureCoords = OpVariable %_ptr_Input_v2int Input
760 %_ptr_Output_v4float = OpTypePointer Output %v4float
761 %_entryPointOutput_vColor = OpVariable %_ptr_Output_v4float Output
762 )";
763
764 const std::string defs_after =
765 R"(OpCapability Shader
766 OpCapability StorageImageReadWithoutFormat
767 OpExtension "SPV_KHR_storage_buffer_storage_class"
768 %1 = OpExtInstImport "GLSL.std.450"
769 OpMemoryModel Logical GLSL450
770 OpEntryPoint Fragment %MainPs "MainPs" %i_vTextureCoords %_entryPointOutput_vColor %gl_FragCoord
771 OpExecutionMode %MainPs OriginUpperLeft
772 OpSource HLSL 500
773 OpName %MainPs "MainPs"
774 OpName %g_tColor "g_tColor"
775 OpName %PerViewConstantBuffer_t "PerViewConstantBuffer_t"
776 OpMemberName %PerViewConstantBuffer_t 0 "g_nDataIdx"
777 OpName %_ ""
778 OpName %i_vTextureCoords "i.vTextureCoords"
779 OpName %_entryPointOutput_vColor "@entryPointOutput.vColor"
780 OpDecorate %g_tColor DescriptorSet 3
781 OpDecorate %g_tColor Binding 0
782 OpMemberDecorate %PerViewConstantBuffer_t 0 Offset 0
783 OpDecorate %PerViewConstantBuffer_t Block
784 OpDecorate %i_vTextureCoords Location 0
785 OpDecorate %_entryPointOutput_vColor Location 0
786 OpDecorate %_runtimearr_uint ArrayStride 4
787 OpDecorate %_struct_51 Block
788 OpMemberDecorate %_struct_51 0 Offset 0
789 OpMemberDecorate %_struct_51 1 Offset 4
790 OpDecorate %53 DescriptorSet 7
791 OpDecorate %53 Binding 0
792 OpDecorate %gl_FragCoord BuiltIn FragCoord
793 %void = OpTypeVoid
794 %9 = OpTypeFunction %void
795 %float = OpTypeFloat 32
796 %v4float = OpTypeVector %float 4
797 %int = OpTypeInt 32 1
798 %v2int = OpTypeVector %int 2
799 %int_0 = OpConstant %int 0
800 %15 = OpTypeImage %float 2D 0 0 0 0 Unknown
801 %uint = OpTypeInt 32 0
802 %uint_128 = OpConstant %uint 128
803 %18 = OpTypeSampledImage %15
804 %_arr_18_uint_128 = OpTypeArray %18 %uint_128
805 %_ptr_UniformConstant__arr_18_uint_128 = OpTypePointer UniformConstant %_arr_18_uint_128
806 %g_tColor = OpVariable %_ptr_UniformConstant__arr_18_uint_128 UniformConstant
807 %PerViewConstantBuffer_t = OpTypeStruct %uint
808 %_ptr_PushConstant_PerViewConstantBuffer_t = OpTypePointer PushConstant %PerViewConstantBuffer_t
809 %_ = OpVariable %_ptr_PushConstant_PerViewConstantBuffer_t PushConstant
810 %_ptr_PushConstant_uint = OpTypePointer PushConstant %uint
811 %_ptr_UniformConstant_18 = OpTypePointer UniformConstant %18
812 %_ptr_Input_v2int = OpTypePointer Input %v2int
813 %i_vTextureCoords = OpVariable %_ptr_Input_v2int Input
814 %_ptr_Output_v4float = OpTypePointer Output %v4float
815 %_entryPointOutput_vColor = OpVariable %_ptr_Output_v4float Output
816 %uint_0 = OpConstant %uint 0
817 %bool = OpTypeBool
818 %44 = OpTypeFunction %void %uint %uint %uint %uint
819 %_runtimearr_uint = OpTypeRuntimeArray %uint
820 %_struct_51 = OpTypeStruct %uint %_runtimearr_uint
821 %_ptr_StorageBuffer__struct_51 = OpTypePointer StorageBuffer %_struct_51
822 %53 = OpVariable %_ptr_StorageBuffer__struct_51 StorageBuffer
823 %_ptr_StorageBuffer_uint = OpTypePointer StorageBuffer %uint
824 %uint_10 = OpConstant %uint 10
825 %uint_4 = OpConstant %uint 4
826 %uint_1 = OpConstant %uint 1
827 %uint_23 = OpConstant %uint 23
828 %uint_2 = OpConstant %uint 2
829 %uint_3 = OpConstant %uint 3
830 %_ptr_Input_v4float = OpTypePointer Input %v4float
831 %gl_FragCoord = OpVariable %_ptr_Input_v4float Input
832 %v4uint = OpTypeVector %uint 4
833 %uint_5 = OpConstant %uint 5
834 %uint_7 = OpConstant %uint 7
835 %uint_8 = OpConstant %uint 8
836 %uint_9 = OpConstant %uint 9
837 %uint_51 = OpConstant %uint 51
838 %99 = OpConstantNull %v4float
839 )";
840
841 const std::string func_before =
842 R"(%MainPs = OpFunction %void None %3
843 %5 = OpLabel
844 %53 = OpLoad %v2int %i_vTextureCoords
845 %63 = OpAccessChain %_ptr_PushConstant_uint %_ %int_0
846 %64 = OpLoad %uint %63
847 %65 = OpAccessChain %_ptr_UniformConstant_39 %g_tColor %64
848 %66 = OpLoad %39 %65
849 %75 = OpImage %20 %66
850 %71 = OpImageRead %v4float %75 %53
851 OpStore %_entryPointOutput_vColor %71
852 OpReturn
853 OpFunctionEnd
854 )";
855
856 const std::string func_after =
857 R"(%MainPs = OpFunction %void None %9
858 %26 = OpLabel
859 %27 = OpLoad %v2int %i_vTextureCoords
860 %28 = OpAccessChain %_ptr_PushConstant_uint %_ %int_0
861 %29 = OpLoad %uint %28
862 %30 = OpAccessChain %_ptr_UniformConstant_18 %g_tColor %29
863 %31 = OpLoad %18 %30
864 %32 = OpImage %15 %31
865 %36 = OpULessThan %bool %29 %uint_128
866 OpSelectionMerge %37 None
867 OpBranchConditional %36 %38 %39
868 %38 = OpLabel
869 %40 = OpLoad %18 %30
870 %41 = OpImage %15 %40
871 %42 = OpImageRead %v4float %41 %27
872 OpBranch %37
873 %39 = OpLabel
874 %98 = OpFunctionCall %void %43 %uint_51 %uint_0 %29 %uint_128
875 OpBranch %37
876 %37 = OpLabel
877 %100 = OpPhi %v4float %42 %38 %99 %39
878 OpStore %_entryPointOutput_vColor %100
879 OpReturn
880 OpFunctionEnd
881 )";
882
883 const std::string output_func =
884 R"(%43 = OpFunction %void None %44
885 %45 = OpFunctionParameter %uint
886 %46 = OpFunctionParameter %uint
887 %47 = OpFunctionParameter %uint
888 %48 = OpFunctionParameter %uint
889 %49 = OpLabel
890 %55 = OpAccessChain %_ptr_StorageBuffer_uint %53 %uint_0
891 %58 = OpAtomicIAdd %uint %55 %uint_4 %uint_0 %uint_10
892 %59 = OpIAdd %uint %58 %uint_10
893 %60 = OpArrayLength %uint %53 1
894 %61 = OpULessThanEqual %bool %59 %60
895 OpSelectionMerge %62 None
896 OpBranchConditional %61 %63 %62
897 %63 = OpLabel
898 %64 = OpIAdd %uint %58 %uint_0
899 %66 = OpAccessChain %_ptr_StorageBuffer_uint %53 %uint_1 %64
900 OpStore %66 %uint_10
901 %68 = OpIAdd %uint %58 %uint_1
902 %69 = OpAccessChain %_ptr_StorageBuffer_uint %53 %uint_1 %68
903 OpStore %69 %uint_23
904 %71 = OpIAdd %uint %58 %uint_2
905 %72 = OpAccessChain %_ptr_StorageBuffer_uint %53 %uint_1 %71
906 OpStore %72 %45
907 %74 = OpIAdd %uint %58 %uint_3
908 %75 = OpAccessChain %_ptr_StorageBuffer_uint %53 %uint_1 %74
909 OpStore %75 %uint_4
910 %78 = OpLoad %v4float %gl_FragCoord
911 %80 = OpBitcast %v4uint %78
912 %81 = OpCompositeExtract %uint %80 0
913 %82 = OpIAdd %uint %58 %uint_4
914 %83 = OpAccessChain %_ptr_StorageBuffer_uint %53 %uint_1 %82
915 OpStore %83 %81
916 %84 = OpCompositeExtract %uint %80 1
917 %86 = OpIAdd %uint %58 %uint_5
918 %87 = OpAccessChain %_ptr_StorageBuffer_uint %53 %uint_1 %86
919 OpStore %87 %84
920 %89 = OpIAdd %uint %58 %uint_7
921 %90 = OpAccessChain %_ptr_StorageBuffer_uint %53 %uint_1 %89
922 OpStore %90 %46
923 %92 = OpIAdd %uint %58 %uint_8
924 %93 = OpAccessChain %_ptr_StorageBuffer_uint %53 %uint_1 %92
925 OpStore %93 %47
926 %95 = OpIAdd %uint %58 %uint_9
927 %96 = OpAccessChain %_ptr_StorageBuffer_uint %53 %uint_1 %95
928 OpStore %96 %48
929 OpBranch %62
930 %62 = OpLabel
931 OpReturn
932 OpFunctionEnd
933 )";
934
935 // SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
936 SinglePassRunAndCheck<InstBindlessCheckPass>(
937 defs_before + func_before, defs_after + func_after + output_func, true,
938 true, 7u, 23u, false, false);
939 }
940
941 TEST_F(InstBindlessTest, InstrumentSampledImage) {
942 // This test verifies that the pass will correctly instrument shader
943 // using sampled image. This test was created by editing the SPIR-V
944 // from the Simple test.
945
946 const std::string defs_before =
947 R"(OpCapability Shader
948 %1 = OpExtInstImport "GLSL.std.450"
949 OpMemoryModel Logical GLSL450
950 OpEntryPoint Fragment %MainPs "MainPs" %i_vTextureCoords %_entryPointOutput_vColor
951 OpExecutionMode %MainPs OriginUpperLeft
952 OpSource HLSL 500
953 OpName %MainPs "MainPs"
954 OpName %g_tColor "g_tColor"
955 OpName %PerViewConstantBuffer_t "PerViewConstantBuffer_t"
956 OpMemberName %PerViewConstantBuffer_t 0 "g_nDataIdx"
957 OpName %_ ""
958 OpName %i_vTextureCoords "i.vTextureCoords"
959 OpName %_entryPointOutput_vColor "@entryPointOutput.vColor"
960 OpDecorate %g_tColor DescriptorSet 3
961 OpDecorate %g_tColor Binding 0
962 OpMemberDecorate %PerViewConstantBuffer_t 0 Offset 0
963 OpDecorate %PerViewConstantBuffer_t Block
964 OpDecorate %i_vTextureCoords Location 0
965 OpDecorate %_entryPointOutput_vColor Location 0
966 %void = OpTypeVoid
967 %3 = OpTypeFunction %void
968 %float = OpTypeFloat 32
969 %v2float = OpTypeVector %float 2
970 %v4float = OpTypeVector %float 4
971 %int = OpTypeInt 32 1
972 %int_0 = OpConstant %int 0
973 %20 = OpTypeImage %float 2D 0 0 0 1 Unknown
974 %uint = OpTypeInt 32 0
975 %uint_128 = OpConstant %uint 128
976 %39 = OpTypeSampledImage %20
977 %_arr_39_uint_128 = OpTypeArray %39 %uint_128
978 %_ptr_UniformConstant__arr_39_uint_128 = OpTypePointer UniformConstant %_arr_39_uint_128
979 %g_tColor = OpVariable %_ptr_UniformConstant__arr_39_uint_128 UniformConstant
980 %PerViewConstantBuffer_t = OpTypeStruct %uint
981 %_ptr_PushConstant_PerViewConstantBuffer_t = OpTypePointer PushConstant %PerViewConstantBuffer_t
982 %_ = OpVariable %_ptr_PushConstant_PerViewConstantBuffer_t PushConstant
983 %_ptr_PushConstant_uint = OpTypePointer PushConstant %uint
984 %_ptr_UniformConstant_39 = OpTypePointer UniformConstant %39
985 %_ptr_Input_v2float = OpTypePointer Input %v2float
986 %i_vTextureCoords = OpVariable %_ptr_Input_v2float Input
987 %_ptr_Output_v4float = OpTypePointer Output %v4float
988 %_entryPointOutput_vColor = OpVariable %_ptr_Output_v4float Output
989 )";
990
991 const std::string defs_after =
992 R"(OpCapability Shader
993 OpExtension "SPV_KHR_storage_buffer_storage_class"
994 %1 = OpExtInstImport "GLSL.std.450"
995 OpMemoryModel Logical GLSL450
996 OpEntryPoint Fragment %MainPs "MainPs" %i_vTextureCoords %_entryPointOutput_vColor %gl_FragCoord
997 OpExecutionMode %MainPs OriginUpperLeft
998 OpSource HLSL 500
999 OpName %MainPs "MainPs"
1000 OpName %g_tColor "g_tColor"
1001 OpName %PerViewConstantBuffer_t "PerViewConstantBuffer_t"
1002 OpMemberName %PerViewConstantBuffer_t 0 "g_nDataIdx"
1003 OpName %_ ""
1004 OpName %i_vTextureCoords "i.vTextureCoords"
1005 OpName %_entryPointOutput_vColor "@entryPointOutput.vColor"
1006 OpDecorate %g_tColor DescriptorSet 3
1007 OpDecorate %g_tColor Binding 0
1008 OpMemberDecorate %PerViewConstantBuffer_t 0 Offset 0
1009 OpDecorate %PerViewConstantBuffer_t Block
1010 OpDecorate %i_vTextureCoords Location 0
1011 OpDecorate %_entryPointOutput_vColor Location 0
1012 OpDecorate %_runtimearr_uint ArrayStride 4
1013 OpDecorate %_struct_49 Block
1014 OpMemberDecorate %_struct_49 0 Offset 0
1015 OpMemberDecorate %_struct_49 1 Offset 4
1016 OpDecorate %51 DescriptorSet 7
1017 OpDecorate %51 Binding 0
1018 OpDecorate %gl_FragCoord BuiltIn FragCoord
1019 %void = OpTypeVoid
1020 %9 = OpTypeFunction %void
1021 %float = OpTypeFloat 32
1022 %v2float = OpTypeVector %float 2
1023 %v4float = OpTypeVector %float 4
1024 %int = OpTypeInt 32 1
1025 %int_0 = OpConstant %int 0
1026 %15 = OpTypeImage %float 2D 0 0 0 1 Unknown
1027 %uint = OpTypeInt 32 0
1028 %uint_128 = OpConstant %uint 128
1029 %18 = OpTypeSampledImage %15
1030 %_arr_18_uint_128 = OpTypeArray %18 %uint_128
1031 %_ptr_UniformConstant__arr_18_uint_128 = OpTypePointer UniformConstant %_arr_18_uint_128
1032 %g_tColor = OpVariable %_ptr_UniformConstant__arr_18_uint_128 UniformConstant
1033 %PerViewConstantBuffer_t = OpTypeStruct %uint
1034 %_ptr_PushConstant_PerViewConstantBuffer_t = OpTypePointer PushConstant %PerViewConstantBuffer_t
1035 %_ = OpVariable %_ptr_PushConstant_PerViewConstantBuffer_t PushConstant
1036 %_ptr_PushConstant_uint = OpTypePointer PushConstant %uint
1037 %_ptr_UniformConstant_18 = OpTypePointer UniformConstant %18
1038 %_ptr_Input_v2float = OpTypePointer Input %v2float
1039 %i_vTextureCoords = OpVariable %_ptr_Input_v2float Input
1040 %_ptr_Output_v4float = OpTypePointer Output %v4float
1041 %_entryPointOutput_vColor = OpVariable %_ptr_Output_v4float Output
1042 %uint_0 = OpConstant %uint 0
1043 %bool = OpTypeBool
1044 %42 = OpTypeFunction %void %uint %uint %uint %uint
1045 %_runtimearr_uint = OpTypeRuntimeArray %uint
1046 %_struct_49 = OpTypeStruct %uint %_runtimearr_uint
1047 %_ptr_StorageBuffer__struct_49 = OpTypePointer StorageBuffer %_struct_49
1048 %51 = OpVariable %_ptr_StorageBuffer__struct_49 StorageBuffer
1049 %_ptr_StorageBuffer_uint = OpTypePointer StorageBuffer %uint
1050 %uint_10 = OpConstant %uint 10
1051 %uint_4 = OpConstant %uint 4
1052 %uint_1 = OpConstant %uint 1
1053 %uint_23 = OpConstant %uint 23
1054 %uint_2 = OpConstant %uint 2
1055 %uint_3 = OpConstant %uint 3
1056 %_ptr_Input_v4float = OpTypePointer Input %v4float
1057 %gl_FragCoord = OpVariable %_ptr_Input_v4float Input
1058 %v4uint = OpTypeVector %uint 4
1059 %uint_5 = OpConstant %uint 5
1060 %uint_7 = OpConstant %uint 7
1061 %uint_8 = OpConstant %uint 8
1062 %uint_9 = OpConstant %uint 9
1063 %uint_49 = OpConstant %uint 49
1064 %97 = OpConstantNull %v4float
1065 )";
1066
1067 const std::string func_before =
1068 R"(%MainPs = OpFunction %void None %3
1069 %5 = OpLabel
1070 %53 = OpLoad %v2float %i_vTextureCoords
1071 %63 = OpAccessChain %_ptr_PushConstant_uint %_ %int_0
1072 %64 = OpLoad %uint %63
1073 %65 = OpAccessChain %_ptr_UniformConstant_39 %g_tColor %64
1074 %66 = OpLoad %39 %65
1075 %71 = OpImageSampleImplicitLod %v4float %66 %53
1076 OpStore %_entryPointOutput_vColor %71
1077 OpReturn
1078 OpFunctionEnd
1079 )";
1080
1081 const std::string func_after =
1082 R"(%MainPs = OpFunction %void None %9
1083 %26 = OpLabel
1084 %27 = OpLoad %v2float %i_vTextureCoords
1085 %28 = OpAccessChain %_ptr_PushConstant_uint %_ %int_0
1086 %29 = OpLoad %uint %28
1087 %30 = OpAccessChain %_ptr_UniformConstant_18 %g_tColor %29
1088 %31 = OpLoad %18 %30
1089 %35 = OpULessThan %bool %29 %uint_128
1090 OpSelectionMerge %36 None
1091 OpBranchConditional %35 %37 %38
1092 %37 = OpLabel
1093 %39 = OpLoad %18 %30
1094 %40 = OpImageSampleImplicitLod %v4float %39 %27
1095 OpBranch %36
1096 %38 = OpLabel
1097 %96 = OpFunctionCall %void %41 %uint_49 %uint_0 %29 %uint_128
1098 OpBranch %36
1099 %36 = OpLabel
1100 %98 = OpPhi %v4float %40 %37 %97 %38
1101 OpStore %_entryPointOutput_vColor %98
1102 OpReturn
1103 OpFunctionEnd
1104 )";
1105
1106 const std::string output_func =
1107 R"(%41 = OpFunction %void None %42
1108 %43 = OpFunctionParameter %uint
1109 %44 = OpFunctionParameter %uint
1110 %45 = OpFunctionParameter %uint
1111 %46 = OpFunctionParameter %uint
1112 %47 = OpLabel
1113 %53 = OpAccessChain %_ptr_StorageBuffer_uint %51 %uint_0
1114 %56 = OpAtomicIAdd %uint %53 %uint_4 %uint_0 %uint_10
1115 %57 = OpIAdd %uint %56 %uint_10
1116 %58 = OpArrayLength %uint %51 1
1117 %59 = OpULessThanEqual %bool %57 %58
1118 OpSelectionMerge %60 None
1119 OpBranchConditional %59 %61 %60
1120 %61 = OpLabel
1121 %62 = OpIAdd %uint %56 %uint_0
1122 %64 = OpAccessChain %_ptr_StorageBuffer_uint %51 %uint_1 %62
1123 OpStore %64 %uint_10
1124 %66 = OpIAdd %uint %56 %uint_1
1125 %67 = OpAccessChain %_ptr_StorageBuffer_uint %51 %uint_1 %66
1126 OpStore %67 %uint_23
1127 %69 = OpIAdd %uint %56 %uint_2
1128 %70 = OpAccessChain %_ptr_StorageBuffer_uint %51 %uint_1 %69
1129 OpStore %70 %43
1130 %72 = OpIAdd %uint %56 %uint_3
1131 %73 = OpAccessChain %_ptr_StorageBuffer_uint %51 %uint_1 %72
1132 OpStore %73 %uint_4
1133 %76 = OpLoad %v4float %gl_FragCoord
1134 %78 = OpBitcast %v4uint %76
1135 %79 = OpCompositeExtract %uint %78 0
1136 %80 = OpIAdd %uint %56 %uint_4
1137 %81 = OpAccessChain %_ptr_StorageBuffer_uint %51 %uint_1 %80
1138 OpStore %81 %79
1139 %82 = OpCompositeExtract %uint %78 1
1140 %84 = OpIAdd %uint %56 %uint_5
1141 %85 = OpAccessChain %_ptr_StorageBuffer_uint %51 %uint_1 %84
1142 OpStore %85 %82
1143 %87 = OpIAdd %uint %56 %uint_7
1144 %88 = OpAccessChain %_ptr_StorageBuffer_uint %51 %uint_1 %87
1145 OpStore %88 %44
1146 %90 = OpIAdd %uint %56 %uint_8
1147 %91 = OpAccessChain %_ptr_StorageBuffer_uint %51 %uint_1 %90
1148 OpStore %91 %45
1149 %93 = OpIAdd %uint %56 %uint_9
1150 %94 = OpAccessChain %_ptr_StorageBuffer_uint %51 %uint_1 %93
1151 OpStore %94 %46
1152 OpBranch %60
1153 %60 = OpLabel
1154 OpReturn
1155 OpFunctionEnd
1156 )";
1157
1158 // SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
1159 SinglePassRunAndCheck<InstBindlessCheckPass>(
1160 defs_before + func_before, defs_after + func_after + output_func, true,
1161 true, 7u, 23u, false, false);
1162 }
1163
1164 TEST_F(InstBindlessTest, InstrumentImageWrite) {
1165 // This test verifies that the pass will correctly instrument shader
1166 // doing bindless image write. This test was created by editing the SPIR-V
1167 // from the Simple test.
1168
1169 const std::string defs_before =
1170 R"(OpCapability Shader
1171 OpCapability StorageImageWriteWithoutFormat
1172 %1 = OpExtInstImport "GLSL.std.450"
1173 OpMemoryModel Logical GLSL450
1174 OpEntryPoint Fragment %MainPs "MainPs" %i_vTextureCoords %_entryPointOutput_vColor
1175 OpExecutionMode %MainPs OriginUpperLeft
1176 OpSource HLSL 500
1177 OpName %MainPs "MainPs"
1178 OpName %g_tColor "g_tColor"
1179 OpName %PerViewConstantBuffer_t "PerViewConstantBuffer_t"
1180 OpMemberName %PerViewConstantBuffer_t 0 "g_nDataIdx"
1181 OpName %_ ""
1182 OpName %i_vTextureCoords "i.vTextureCoords"
1183 OpName %_entryPointOutput_vColor "@entryPointOutput.vColor"
1184 OpDecorate %g_tColor DescriptorSet 3
1185 OpDecorate %g_tColor Binding 0
1186 OpMemberDecorate %PerViewConstantBuffer_t 0 Offset 0
1187 OpDecorate %PerViewConstantBuffer_t Block
1188 OpDecorate %i_vTextureCoords Location 0
1189 OpDecorate %_entryPointOutput_vColor Location 0
1190 %void = OpTypeVoid
1191 %3 = OpTypeFunction %void
1192 %float = OpTypeFloat 32
1193 %v2float = OpTypeVector %float 2
1194 %v4float = OpTypeVector %float 4
1195 %int = OpTypeInt 32 1
1196 %v2int = OpTypeVector %int 2
1197 %int_0 = OpConstant %int 0
1198 %20 = OpTypeImage %float 2D 0 0 0 0 Unknown
1199 %uint = OpTypeInt 32 0
1200 %uint_128 = OpConstant %uint 128
1201 %80 = OpConstantNull %v4float
1202 %_arr_20_uint_128 = OpTypeArray %20 %uint_128
1203 %_ptr_UniformConstant__arr_20_uint_128 = OpTypePointer UniformConstant %_arr_20_uint_128
1204 %g_tColor = OpVariable %_ptr_UniformConstant__arr_20_uint_128 UniformConstant
1205 %PerViewConstantBuffer_t = OpTypeStruct %uint
1206 %_ptr_PushConstant_PerViewConstantBuffer_t = OpTypePointer PushConstant %PerViewConstantBuffer_t
1207 %_ = OpVariable %_ptr_PushConstant_PerViewConstantBuffer_t PushConstant
1208 %_ptr_PushConstant_uint = OpTypePointer PushConstant %uint
1209 %_ptr_UniformConstant_20 = OpTypePointer UniformConstant %20
1210 %_ptr_Input_v2int = OpTypePointer Input %v2int
1211 %i_vTextureCoords = OpVariable %_ptr_Input_v2int Input
1212 %_ptr_Output_v4float = OpTypePointer Output %v4float
1213 %_entryPointOutput_vColor = OpVariable %_ptr_Output_v4float Output
1214 )";
1215
1216 const std::string defs_after =
1217 R"(OpCapability Shader
1218 OpCapability StorageImageWriteWithoutFormat
1219 OpExtension "SPV_KHR_storage_buffer_storage_class"
1220 %1 = OpExtInstImport "GLSL.std.450"
1221 OpMemoryModel Logical GLSL450
1222 OpEntryPoint Fragment %MainPs "MainPs" %i_vTextureCoords %_entryPointOutput_vColor %gl_FragCoord
1223 OpExecutionMode %MainPs OriginUpperLeft
1224 OpSource HLSL 500
1225 OpName %MainPs "MainPs"
1226 OpName %g_tColor "g_tColor"
1227 OpName %PerViewConstantBuffer_t "PerViewConstantBuffer_t"
1228 OpMemberName %PerViewConstantBuffer_t 0 "g_nDataIdx"
1229 OpName %_ ""
1230 OpName %i_vTextureCoords "i.vTextureCoords"
1231 OpName %_entryPointOutput_vColor "@entryPointOutput.vColor"
1232 OpDecorate %g_tColor DescriptorSet 3
1233 OpDecorate %g_tColor Binding 0
1234 OpMemberDecorate %PerViewConstantBuffer_t 0 Offset 0
1235 OpDecorate %PerViewConstantBuffer_t Block
1236 OpDecorate %i_vTextureCoords Location 0
1237 OpDecorate %_entryPointOutput_vColor Location 0
1238 OpDecorate %_runtimearr_uint ArrayStride 4
1239 OpDecorate %_struct_48 Block
1240 OpMemberDecorate %_struct_48 0 Offset 0
1241 OpMemberDecorate %_struct_48 1 Offset 4
1242 OpDecorate %50 DescriptorSet 7
1243 OpDecorate %50 Binding 0
1244 OpDecorate %gl_FragCoord BuiltIn FragCoord
1245 %void = OpTypeVoid
1246 %9 = OpTypeFunction %void
1247 %float = OpTypeFloat 32
1248 %v2float = OpTypeVector %float 2
1249 %v4float = OpTypeVector %float 4
1250 %int = OpTypeInt 32 1
1251 %v2int = OpTypeVector %int 2
1252 %int_0 = OpConstant %int 0
1253 %16 = OpTypeImage %float 2D 0 0 0 0 Unknown
1254 %uint = OpTypeInt 32 0
1255 %uint_128 = OpConstant %uint 128
1256 %19 = OpConstantNull %v4float
1257 %_arr_16_uint_128 = OpTypeArray %16 %uint_128
1258 %_ptr_UniformConstant__arr_16_uint_128 = OpTypePointer UniformConstant %_arr_16_uint_128
1259 %g_tColor = OpVariable %_ptr_UniformConstant__arr_16_uint_128 UniformConstant
1260 %PerViewConstantBuffer_t = OpTypeStruct %uint
1261 %_ptr_PushConstant_PerViewConstantBuffer_t = OpTypePointer PushConstant %PerViewConstantBuffer_t
1262 %_ = OpVariable %_ptr_PushConstant_PerViewConstantBuffer_t PushConstant
1263 %_ptr_PushConstant_uint = OpTypePointer PushConstant %uint
1264 %_ptr_UniformConstant_16 = OpTypePointer UniformConstant %16
1265 %_ptr_Input_v2int = OpTypePointer Input %v2int
1266 %i_vTextureCoords = OpVariable %_ptr_Input_v2int Input
1267 %_ptr_Output_v4float = OpTypePointer Output %v4float
1268 %_entryPointOutput_vColor = OpVariable %_ptr_Output_v4float Output
1269 %uint_0 = OpConstant %uint 0
1270 %bool = OpTypeBool
1271 %41 = OpTypeFunction %void %uint %uint %uint %uint
1272 %_runtimearr_uint = OpTypeRuntimeArray %uint
1273 %_struct_48 = OpTypeStruct %uint %_runtimearr_uint
1274 %_ptr_StorageBuffer__struct_48 = OpTypePointer StorageBuffer %_struct_48
1275 %50 = OpVariable %_ptr_StorageBuffer__struct_48 StorageBuffer
1276 %_ptr_StorageBuffer_uint = OpTypePointer StorageBuffer %uint
1277 %uint_10 = OpConstant %uint 10
1278 %uint_4 = OpConstant %uint 4
1279 %uint_1 = OpConstant %uint 1
1280 %uint_23 = OpConstant %uint 23
1281 %uint_2 = OpConstant %uint 2
1282 %uint_3 = OpConstant %uint 3
1283 %_ptr_Input_v4float = OpTypePointer Input %v4float
1284 %gl_FragCoord = OpVariable %_ptr_Input_v4float Input
1285 %v4uint = OpTypeVector %uint 4
1286 %uint_5 = OpConstant %uint 5
1287 %uint_7 = OpConstant %uint 7
1288 %uint_8 = OpConstant %uint 8
1289 %uint_9 = OpConstant %uint 9
1290 %uint_51 = OpConstant %uint 51
1291 )";
1292
1293 const std::string func_before =
1294 R"(%MainPs = OpFunction %void None %3
1295 %5 = OpLabel
1296 %53 = OpLoad %v2int %i_vTextureCoords
1297 %63 = OpAccessChain %_ptr_PushConstant_uint %_ %int_0
1298 %64 = OpLoad %uint %63
1299 %65 = OpAccessChain %_ptr_UniformConstant_20 %g_tColor %64
1300 %66 = OpLoad %20 %65
1301 OpImageWrite %66 %53 %80
1302 OpStore %_entryPointOutput_vColor %80
1303 OpReturn
1304 OpFunctionEnd
1305 )";
1306
1307 const std::string func_after =
1308 R"(%MainPs = OpFunction %void None %9
1309 %27 = OpLabel
1310 %28 = OpLoad %v2int %i_vTextureCoords
1311 %29 = OpAccessChain %_ptr_PushConstant_uint %_ %int_0
1312 %30 = OpLoad %uint %29
1313 %31 = OpAccessChain %_ptr_UniformConstant_16 %g_tColor %30
1314 %32 = OpLoad %16 %31
1315 %35 = OpULessThan %bool %30 %uint_128
1316 OpSelectionMerge %36 None
1317 OpBranchConditional %35 %37 %38
1318 %37 = OpLabel
1319 %39 = OpLoad %16 %31
1320 OpImageWrite %39 %28 %19
1321 OpBranch %36
1322 %38 = OpLabel
1323 %95 = OpFunctionCall %void %40 %uint_51 %uint_0 %30 %uint_128
1324 OpBranch %36
1325 %36 = OpLabel
1326 OpStore %_entryPointOutput_vColor %19
1327 OpReturn
1328 OpFunctionEnd
1329 )";
1330
1331 const std::string output_func =
1332 R"(%40 = OpFunction %void None %41
1333 %42 = OpFunctionParameter %uint
1334 %43 = OpFunctionParameter %uint
1335 %44 = OpFunctionParameter %uint
1336 %45 = OpFunctionParameter %uint
1337 %46 = OpLabel
1338 %52 = OpAccessChain %_ptr_StorageBuffer_uint %50 %uint_0
1339 %55 = OpAtomicIAdd %uint %52 %uint_4 %uint_0 %uint_10
1340 %56 = OpIAdd %uint %55 %uint_10
1341 %57 = OpArrayLength %uint %50 1
1342 %58 = OpULessThanEqual %bool %56 %57
1343 OpSelectionMerge %59 None
1344 OpBranchConditional %58 %60 %59
1345 %60 = OpLabel
1346 %61 = OpIAdd %uint %55 %uint_0
1347 %63 = OpAccessChain %_ptr_StorageBuffer_uint %50 %uint_1 %61
1348 OpStore %63 %uint_10
1349 %65 = OpIAdd %uint %55 %uint_1
1350 %66 = OpAccessChain %_ptr_StorageBuffer_uint %50 %uint_1 %65
1351 OpStore %66 %uint_23
1352 %68 = OpIAdd %uint %55 %uint_2
1353 %69 = OpAccessChain %_ptr_StorageBuffer_uint %50 %uint_1 %68
1354 OpStore %69 %42
1355 %71 = OpIAdd %uint %55 %uint_3
1356 %72 = OpAccessChain %_ptr_StorageBuffer_uint %50 %uint_1 %71
1357 OpStore %72 %uint_4
1358 %75 = OpLoad %v4float %gl_FragCoord
1359 %77 = OpBitcast %v4uint %75
1360 %78 = OpCompositeExtract %uint %77 0
1361 %79 = OpIAdd %uint %55 %uint_4
1362 %80 = OpAccessChain %_ptr_StorageBuffer_uint %50 %uint_1 %79
1363 OpStore %80 %78
1364 %81 = OpCompositeExtract %uint %77 1
1365 %83 = OpIAdd %uint %55 %uint_5
1366 %84 = OpAccessChain %_ptr_StorageBuffer_uint %50 %uint_1 %83
1367 OpStore %84 %81
1368 %86 = OpIAdd %uint %55 %uint_7
1369 %87 = OpAccessChain %_ptr_StorageBuffer_uint %50 %uint_1 %86
1370 OpStore %87 %43
1371 %89 = OpIAdd %uint %55 %uint_8
1372 %90 = OpAccessChain %_ptr_StorageBuffer_uint %50 %uint_1 %89
1373 OpStore %90 %44
1374 %92 = OpIAdd %uint %55 %uint_9
1375 %93 = OpAccessChain %_ptr_StorageBuffer_uint %50 %uint_1 %92
1376 OpStore %93 %45
1377 OpBranch %59
1378 %59 = OpLabel
1379 OpReturn
1380 OpFunctionEnd
1381 )";
1382
1383 // SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
1384 SinglePassRunAndCheck<InstBindlessCheckPass>(
1385 defs_before + func_before, defs_after + func_after + output_func, true,
1386 true, 7u, 23u, false, false);
1387 }
1388
1389 TEST_F(InstBindlessTest, InstrumentVertexSimple) {
1390 // This test verifies that the pass will correctly instrument shader
1391 // doing bindless image write. This test was created by editing the SPIR-V
1392 // from the Simple test.
1393
1394 const std::string defs_before =
1395 R"(OpCapability Shader
1396 OpCapability Sampled1D
1397 %1 = OpExtInstImport "GLSL.std.450"
1398 OpMemoryModel Logical GLSL450
1399 OpEntryPoint Vertex %main "main" %_ %coords2D
1400 OpSource GLSL 450
1401 OpName %main "main"
1402 OpName %lod "lod"
1403 OpName %coords1D "coords1D"
1404 OpName %gl_PerVertex "gl_PerVertex"
1405 OpMemberName %gl_PerVertex 0 "gl_Position"
1406 OpMemberName %gl_PerVertex 1 "gl_PointSize"
1407 OpMemberName %gl_PerVertex 2 "gl_ClipDistance"
1408 OpMemberName %gl_PerVertex 3 "gl_CullDistance"
1409 OpName %_ ""
1410 OpName %texSampler1D "texSampler1D"
1411 OpName %foo "foo"
1412 OpMemberName %foo 0 "g_idx"
1413 OpName %__0 ""
1414 OpName %coords2D "coords2D"
1415 OpMemberDecorate %gl_PerVertex 0 BuiltIn Position
1416 OpMemberDecorate %gl_PerVertex 1 BuiltIn PointSize
1417 OpMemberDecorate %gl_PerVertex 2 BuiltIn ClipDistance
1418 OpMemberDecorate %gl_PerVertex 3 BuiltIn CullDistance
1419 OpDecorate %gl_PerVertex Block
1420 OpDecorate %texSampler1D DescriptorSet 0
1421 OpDecorate %texSampler1D Binding 3
1422 OpMemberDecorate %foo 0 Offset 0
1423 OpDecorate %foo Block
1424 OpDecorate %__0 DescriptorSet 0
1425 OpDecorate %__0 Binding 5
1426 OpDecorate %coords2D Location 0
1427 %void = OpTypeVoid
1428 %3 = OpTypeFunction %void
1429 %float = OpTypeFloat 32
1430 %_ptr_Function_float = OpTypePointer Function %float
1431 %float_3 = OpConstant %float 3
1432 %float_1_78900003 = OpConstant %float 1.78900003
1433 %v4float = OpTypeVector %float 4
1434 %uint = OpTypeInt 32 0
1435 %uint_1 = OpConstant %uint 1
1436 %_arr_float_uint_1 = OpTypeArray %float %uint_1
1437 %gl_PerVertex = OpTypeStruct %v4float %float %_arr_float_uint_1 %_arr_float_uint_1
1438 %_ptr_Output_gl_PerVertex = OpTypePointer Output %gl_PerVertex
1439 %_ = OpVariable %_ptr_Output_gl_PerVertex Output
1440 %int = OpTypeInt 32 1
1441 %int_0 = OpConstant %int 0
1442 %21 = OpTypeImage %float 1D 0 0 0 1 Unknown
1443 %22 = OpTypeSampledImage %21
1444 %uint_128 = OpConstant %uint 128
1445 %_arr_22_uint_128 = OpTypeArray %22 %uint_128
1446 %_ptr_UniformConstant__arr_22_uint_128 = OpTypePointer UniformConstant %_arr_22_uint_128
1447 %texSampler1D = OpVariable %_ptr_UniformConstant__arr_22_uint_128 UniformConstant
1448 %foo = OpTypeStruct %int
1449 %_ptr_Uniform_foo = OpTypePointer Uniform %foo
1450 %__0 = OpVariable %_ptr_Uniform_foo Uniform
1451 %_ptr_Uniform_int = OpTypePointer Uniform %int
1452 %_ptr_UniformConstant_22 = OpTypePointer UniformConstant %22
1453 %_ptr_Output_v4float = OpTypePointer Output %v4float
1454 %v2float = OpTypeVector %float 2
1455 %_ptr_Input_v2float = OpTypePointer Input %v2float
1456 %coords2D = OpVariable %_ptr_Input_v2float Input
1457 )";
1458
1459 const std::string defs_after =
1460 R"(OpCapability Shader
1461 OpCapability Sampled1D
1462 OpExtension "SPV_KHR_storage_buffer_storage_class"
1463 %1 = OpExtInstImport "GLSL.std.450"
1464 OpMemoryModel Logical GLSL450
1465 OpEntryPoint Vertex %main "main" %_ %coords2D %gl_VertexIndex %gl_InstanceIndex
1466 OpSource GLSL 450
1467 OpName %main "main"
1468 OpName %lod "lod"
1469 OpName %coords1D "coords1D"
1470 OpName %gl_PerVertex "gl_PerVertex"
1471 OpMemberName %gl_PerVertex 0 "gl_Position"
1472 OpMemberName %gl_PerVertex 1 "gl_PointSize"
1473 OpMemberName %gl_PerVertex 2 "gl_ClipDistance"
1474 OpMemberName %gl_PerVertex 3 "gl_CullDistance"
1475 OpName %_ ""
1476 OpName %texSampler1D "texSampler1D"
1477 OpName %foo "foo"
1478 OpMemberName %foo 0 "g_idx"
1479 OpName %__0 ""
1480 OpName %coords2D "coords2D"
1481 OpMemberDecorate %gl_PerVertex 0 BuiltIn Position
1482 OpMemberDecorate %gl_PerVertex 1 BuiltIn PointSize
1483 OpMemberDecorate %gl_PerVertex 2 BuiltIn ClipDistance
1484 OpMemberDecorate %gl_PerVertex 3 BuiltIn CullDistance
1485 OpDecorate %gl_PerVertex Block
1486 OpDecorate %texSampler1D DescriptorSet 0
1487 OpDecorate %texSampler1D Binding 3
1488 OpMemberDecorate %foo 0 Offset 0
1489 OpDecorate %foo Block
1490 OpDecorate %__0 DescriptorSet 0
1491 OpDecorate %__0 Binding 5
1492 OpDecorate %coords2D Location 0
1493 OpDecorate %_runtimearr_uint ArrayStride 4
1494 OpDecorate %_struct_61 Block
1495 OpMemberDecorate %_struct_61 0 Offset 0
1496 OpMemberDecorate %_struct_61 1 Offset 4
1497 OpDecorate %63 DescriptorSet 7
1498 OpDecorate %63 Binding 0
1499 OpDecorate %gl_VertexIndex BuiltIn VertexIndex
1500 OpDecorate %gl_InstanceIndex BuiltIn InstanceIndex
1501 %void = OpTypeVoid
1502 %12 = OpTypeFunction %void
1503 %float = OpTypeFloat 32
1504 %_ptr_Function_float = OpTypePointer Function %float
1505 %float_3 = OpConstant %float 3
1506 %float_1_78900003 = OpConstant %float 1.78900003
1507 %v4float = OpTypeVector %float 4
1508 %uint = OpTypeInt 32 0
1509 %uint_1 = OpConstant %uint 1
1510 %_arr_float_uint_1 = OpTypeArray %float %uint_1
1511 %gl_PerVertex = OpTypeStruct %v4float %float %_arr_float_uint_1 %_arr_float_uint_1
1512 %_ptr_Output_gl_PerVertex = OpTypePointer Output %gl_PerVertex
1513 %_ = OpVariable %_ptr_Output_gl_PerVertex Output
1514 %int = OpTypeInt 32 1
1515 %int_0 = OpConstant %int 0
1516 %24 = OpTypeImage %float 1D 0 0 0 1 Unknown
1517 %25 = OpTypeSampledImage %24
1518 %uint_128 = OpConstant %uint 128
1519 %_arr_25_uint_128 = OpTypeArray %25 %uint_128
1520 %_ptr_UniformConstant__arr_25_uint_128 = OpTypePointer UniformConstant %_arr_25_uint_128
1521 %texSampler1D = OpVariable %_ptr_UniformConstant__arr_25_uint_128 UniformConstant
1522 %foo = OpTypeStruct %int
1523 %_ptr_Uniform_foo = OpTypePointer Uniform %foo
1524 %__0 = OpVariable %_ptr_Uniform_foo Uniform
1525 %_ptr_Uniform_int = OpTypePointer Uniform %int
1526 %_ptr_UniformConstant_25 = OpTypePointer UniformConstant %25
1527 %_ptr_Output_v4float = OpTypePointer Output %v4float
1528 %v2float = OpTypeVector %float 2
1529 %_ptr_Input_v2float = OpTypePointer Input %v2float
1530 %coords2D = OpVariable %_ptr_Input_v2float Input
1531 %uint_0 = OpConstant %uint 0
1532 %bool = OpTypeBool
1533 %54 = OpTypeFunction %void %uint %uint %uint %uint
1534 %_runtimearr_uint = OpTypeRuntimeArray %uint
1535 %_struct_61 = OpTypeStruct %uint %_runtimearr_uint
1536 %_ptr_StorageBuffer__struct_61 = OpTypePointer StorageBuffer %_struct_61
1537 %63 = OpVariable %_ptr_StorageBuffer__struct_61 StorageBuffer
1538 %_ptr_StorageBuffer_uint = OpTypePointer StorageBuffer %uint
1539 %uint_10 = OpConstant %uint 10
1540 %uint_4 = OpConstant %uint 4
1541 %uint_23 = OpConstant %uint 23
1542 %uint_2 = OpConstant %uint 2
1543 %uint_3 = OpConstant %uint 3
1544 %_ptr_Input_uint = OpTypePointer Input %uint
1545 %gl_VertexIndex = OpVariable %_ptr_Input_uint Input
1546 %gl_InstanceIndex = OpVariable %_ptr_Input_uint Input
1547 %uint_5 = OpConstant %uint 5
1548 %uint_7 = OpConstant %uint 7
1549 %uint_8 = OpConstant %uint 8
1550 %uint_9 = OpConstant %uint 9
1551 %uint_74 = OpConstant %uint 74
1552 %106 = OpConstantNull %v4float
1553 )";
1554
1555 const std::string func_before =
1556 R"(%main = OpFunction %void None %3
1557 %5 = OpLabel
1558 %lod = OpVariable %_ptr_Function_float Function
1559 %coords1D = OpVariable %_ptr_Function_float Function
1560 OpStore %lod %float_3
1561 OpStore %coords1D %float_1_78900003
1562 %31 = OpAccessChain %_ptr_Uniform_int %__0 %int_0
1563 %32 = OpLoad %int %31
1564 %34 = OpAccessChain %_ptr_UniformConstant_22 %texSampler1D %32
1565 %35 = OpLoad %22 %34
1566 %36 = OpLoad %float %coords1D
1567 %37 = OpLoad %float %lod
1568 %38 = OpImageSampleExplicitLod %v4float %35 %36 Lod %37
1569 %40 = OpAccessChain %_ptr_Output_v4float %_ %int_0
1570 OpStore %40 %38
1571 OpReturn
1572 OpFunctionEnd
1573 )";
1574
1575 const std::string func_after =
1576 R"(%main = OpFunction %void None %12
1577 %35 = OpLabel
1578 %lod = OpVariable %_ptr_Function_float Function
1579 %coords1D = OpVariable %_ptr_Function_float Function
1580 OpStore %lod %float_3
1581 OpStore %coords1D %float_1_78900003
1582 %36 = OpAccessChain %_ptr_Uniform_int %__0 %int_0
1583 %37 = OpLoad %int %36
1584 %38 = OpAccessChain %_ptr_UniformConstant_25 %texSampler1D %37
1585 %39 = OpLoad %25 %38
1586 %40 = OpLoad %float %coords1D
1587 %41 = OpLoad %float %lod
1588 %46 = OpULessThan %bool %37 %uint_128
1589 OpSelectionMerge %47 None
1590 OpBranchConditional %46 %48 %49
1591 %48 = OpLabel
1592 %50 = OpLoad %25 %38
1593 %51 = OpImageSampleExplicitLod %v4float %50 %40 Lod %41
1594 OpBranch %47
1595 %49 = OpLabel
1596 %52 = OpBitcast %uint %37
1597 %105 = OpFunctionCall %void %53 %uint_74 %uint_0 %52 %uint_128
1598 OpBranch %47
1599 %47 = OpLabel
1600 %107 = OpPhi %v4float %51 %48 %106 %49
1601 %43 = OpAccessChain %_ptr_Output_v4float %_ %int_0
1602 OpStore %43 %107
1603 OpReturn
1604 OpFunctionEnd
1605 )";
1606
1607 const std::string output_func =
1608 R"(%53 = OpFunction %void None %54
1609 %55 = OpFunctionParameter %uint
1610 %56 = OpFunctionParameter %uint
1611 %57 = OpFunctionParameter %uint
1612 %58 = OpFunctionParameter %uint
1613 %59 = OpLabel
1614 %65 = OpAccessChain %_ptr_StorageBuffer_uint %63 %uint_0
1615 %68 = OpAtomicIAdd %uint %65 %uint_4 %uint_0 %uint_10
1616 %69 = OpIAdd %uint %68 %uint_10
1617 %70 = OpArrayLength %uint %63 1
1618 %71 = OpULessThanEqual %bool %69 %70
1619 OpSelectionMerge %72 None
1620 OpBranchConditional %71 %73 %72
1621 %73 = OpLabel
1622 %74 = OpIAdd %uint %68 %uint_0
1623 %75 = OpAccessChain %_ptr_StorageBuffer_uint %63 %uint_1 %74
1624 OpStore %75 %uint_10
1625 %77 = OpIAdd %uint %68 %uint_1
1626 %78 = OpAccessChain %_ptr_StorageBuffer_uint %63 %uint_1 %77
1627 OpStore %78 %uint_23
1628 %80 = OpIAdd %uint %68 %uint_2
1629 %81 = OpAccessChain %_ptr_StorageBuffer_uint %63 %uint_1 %80
1630 OpStore %81 %55
1631 %83 = OpIAdd %uint %68 %uint_3
1632 %84 = OpAccessChain %_ptr_StorageBuffer_uint %63 %uint_1 %83
1633 OpStore %84 %uint_0
1634 %87 = OpLoad %uint %gl_VertexIndex
1635 %88 = OpIAdd %uint %68 %uint_4
1636 %89 = OpAccessChain %_ptr_StorageBuffer_uint %63 %uint_1 %88
1637 OpStore %89 %87
1638 %91 = OpLoad %uint %gl_InstanceIndex
1639 %93 = OpIAdd %uint %68 %uint_5
1640 %94 = OpAccessChain %_ptr_StorageBuffer_uint %63 %uint_1 %93
1641 OpStore %94 %91
1642 %96 = OpIAdd %uint %68 %uint_7
1643 %97 = OpAccessChain %_ptr_StorageBuffer_uint %63 %uint_1 %96
1644 OpStore %97 %56
1645 %99 = OpIAdd %uint %68 %uint_8
1646 %100 = OpAccessChain %_ptr_StorageBuffer_uint %63 %uint_1 %99
1647 OpStore %100 %57
1648 %102 = OpIAdd %uint %68 %uint_9
1649 %103 = OpAccessChain %_ptr_StorageBuffer_uint %63 %uint_1 %102
1650 OpStore %103 %58
1651 OpBranch %72
1652 %72 = OpLabel
1653 OpReturn
1654 OpFunctionEnd
1655 )";
1656
1657 // SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
1658 SinglePassRunAndCheck<InstBindlessCheckPass>(
1659 defs_before + func_before, defs_after + func_after + output_func, true,
1660 true, 7u, 23u, false, false);
1661 }
1662
1663 TEST_F(InstBindlessTest, InstrumentTeseSimple) {
1664 // This test verifies that the pass will correctly instrument tessellation
1665 // evaluation shader doing bindless buffer load.
1666 //
1667 // clang-format off
1668 //
1669 // #version 450
1670 // #extension GL_EXT_nonuniform_qualifier : enable
1671 //
1672 // layout(std140, set = 0, binding = 0) uniform ufoo { uint index; } uniform_index_buffer;
1673 //
1674 // layout(set = 0, binding = 1) buffer bfoo { vec4 val; } adds[11];
1675 //
1676 // layout(triangles, equal_spacing, cw) in;
1677 //
1678 // void main() {
1679 // gl_Position = adds[uniform_index_buffer.index].val;
1680 // }
1681 //
1682 // clang-format on
1683
1684 const std::string defs_before =
1685 R"(OpCapability Tessellation
1686 %1 = OpExtInstImport "GLSL.std.450"
1687 OpMemoryModel Logical GLSL450
1688 OpEntryPoint TessellationEvaluation %main "main" %_
1689 OpExecutionMode %main Triangles
1690 OpExecutionMode %main SpacingEqual
1691 OpExecutionMode %main VertexOrderCw
1692 OpSource GLSL 450
1693 OpSourceExtension "GL_EXT_nonuniform_qualifier"
1694 OpName %main "main"
1695 OpName %gl_PerVertex "gl_PerVertex"
1696 OpMemberName %gl_PerVertex 0 "gl_Position"
1697 OpMemberName %gl_PerVertex 1 "gl_PointSize"
1698 OpMemberName %gl_PerVertex 2 "gl_ClipDistance"
1699 OpMemberName %gl_PerVertex 3 "gl_CullDistance"
1700 OpName %_ ""
1701 OpName %bfoo "bfoo"
1702 OpMemberName %bfoo 0 "val"
1703 OpName %adds "adds"
1704 OpName %ufoo "ufoo"
1705 OpMemberName %ufoo 0 "index"
1706 OpName %uniform_index_buffer "uniform_index_buffer"
1707 OpMemberDecorate %gl_PerVertex 0 BuiltIn Position
1708 OpMemberDecorate %gl_PerVertex 1 BuiltIn PointSize
1709 OpMemberDecorate %gl_PerVertex 2 BuiltIn ClipDistance
1710 OpMemberDecorate %gl_PerVertex 3 BuiltIn CullDistance
1711 OpDecorate %gl_PerVertex Block
1712 OpMemberDecorate %bfoo 0 Offset 0
1713 OpDecorate %bfoo Block
1714 OpDecorate %adds DescriptorSet 0
1715 OpDecorate %adds Binding 1
1716 OpMemberDecorate %ufoo 0 Offset 0
1717 OpDecorate %ufoo Block
1718 OpDecorate %uniform_index_buffer DescriptorSet 0
1719 OpDecorate %uniform_index_buffer Binding 0
1720 %void = OpTypeVoid
1721 %3 = OpTypeFunction %void
1722 %float = OpTypeFloat 32
1723 %v4float = OpTypeVector %float 4
1724 %uint = OpTypeInt 32 0
1725 %uint_1 = OpConstant %uint 1
1726 %_arr_float_uint_1 = OpTypeArray %float %uint_1
1727 %gl_PerVertex = OpTypeStruct %v4float %float %_arr_float_uint_1 %_arr_float_uint_1
1728 %_ptr_Output_gl_PerVertex = OpTypePointer Output %gl_PerVertex
1729 %_ = OpVariable %_ptr_Output_gl_PerVertex Output
1730 %int = OpTypeInt 32 1
1731 %int_0 = OpConstant %int 0
1732 %bfoo = OpTypeStruct %v4float
1733 %uint_11 = OpConstant %uint 11
1734 %_arr_bfoo_uint_11 = OpTypeArray %bfoo %uint_11
1735 %_ptr_StorageBuffer__arr_bfoo_uint_11 = OpTypePointer StorageBuffer %_arr_bfoo_uint_11
1736 %adds = OpVariable %_ptr_StorageBuffer__arr_bfoo_uint_11 StorageBuffer
1737 %ufoo = OpTypeStruct %uint
1738 %_ptr_Uniform_ufoo = OpTypePointer Uniform %ufoo
1739 %uniform_index_buffer = OpVariable %_ptr_Uniform_ufoo Uniform
1740 %_ptr_Uniform_uint = OpTypePointer Uniform %uint
1741 %_ptr_StorageBuffer_v4float = OpTypePointer StorageBuffer %v4float
1742 %_ptr_Output_v4float = OpTypePointer Output %v4float
1743 )";
1744
1745 const std::string defs_after =
1746 R"(OpCapability Tessellation
1747 OpExtension "SPV_KHR_storage_buffer_storage_class"
1748 %1 = OpExtInstImport "GLSL.std.450"
1749 OpMemoryModel Logical GLSL450
1750 OpEntryPoint TessellationEvaluation %main "main" %_ %gl_PrimitiveID %gl_TessCoord
1751 OpExecutionMode %main Triangles
1752 OpExecutionMode %main SpacingEqual
1753 OpExecutionMode %main VertexOrderCw
1754 OpSource GLSL 450
1755 OpSourceExtension "GL_EXT_nonuniform_qualifier"
1756 OpName %main "main"
1757 OpName %gl_PerVertex "gl_PerVertex"
1758 OpMemberName %gl_PerVertex 0 "gl_Position"
1759 OpMemberName %gl_PerVertex 1 "gl_PointSize"
1760 OpMemberName %gl_PerVertex 2 "gl_ClipDistance"
1761 OpMemberName %gl_PerVertex 3 "gl_CullDistance"
1762 OpName %_ ""
1763 OpName %bfoo "bfoo"
1764 OpMemberName %bfoo 0 "val"
1765 OpName %adds "adds"
1766 OpName %ufoo "ufoo"
1767 OpMemberName %ufoo 0 "index"
1768 OpName %uniform_index_buffer "uniform_index_buffer"
1769 OpMemberDecorate %gl_PerVertex 0 BuiltIn Position
1770 OpMemberDecorate %gl_PerVertex 1 BuiltIn PointSize
1771 OpMemberDecorate %gl_PerVertex 2 BuiltIn ClipDistance
1772 OpMemberDecorate %gl_PerVertex 3 BuiltIn CullDistance
1773 OpDecorate %gl_PerVertex Block
1774 OpMemberDecorate %bfoo 0 Offset 0
1775 OpDecorate %bfoo Block
1776 OpDecorate %adds DescriptorSet 0
1777 OpDecorate %adds Binding 1
1778 OpMemberDecorate %ufoo 0 Offset 0
1779 OpDecorate %ufoo Block
1780 OpDecorate %uniform_index_buffer DescriptorSet 0
1781 OpDecorate %uniform_index_buffer Binding 0
1782 OpDecorate %_runtimearr_uint ArrayStride 4
1783 OpDecorate %_struct_47 Block
1784 OpMemberDecorate %_struct_47 0 Offset 0
1785 OpMemberDecorate %_struct_47 1 Offset 4
1786 OpDecorate %49 DescriptorSet 7
1787 OpDecorate %49 Binding 0
1788 OpDecorate %gl_PrimitiveID BuiltIn PrimitiveId
1789 OpDecorate %gl_TessCoord BuiltIn TessCoord
1790 %void = OpTypeVoid
1791 %10 = OpTypeFunction %void
1792 %float = OpTypeFloat 32
1793 %v4float = OpTypeVector %float 4
1794 %uint = OpTypeInt 32 0
1795 %uint_1 = OpConstant %uint 1
1796 %_arr_float_uint_1 = OpTypeArray %float %uint_1
1797 %gl_PerVertex = OpTypeStruct %v4float %float %_arr_float_uint_1 %_arr_float_uint_1
1798 %_ptr_Output_gl_PerVertex = OpTypePointer Output %gl_PerVertex
1799 %_ = OpVariable %_ptr_Output_gl_PerVertex Output
1800 %int = OpTypeInt 32 1
1801 %int_0 = OpConstant %int 0
1802 %bfoo = OpTypeStruct %v4float
1803 %uint_11 = OpConstant %uint 11
1804 %_arr_bfoo_uint_11 = OpTypeArray %bfoo %uint_11
1805 %_ptr_StorageBuffer__arr_bfoo_uint_11 = OpTypePointer StorageBuffer %_arr_bfoo_uint_11
1806 %adds = OpVariable %_ptr_StorageBuffer__arr_bfoo_uint_11 StorageBuffer
1807 %ufoo = OpTypeStruct %uint
1808 %_ptr_Uniform_ufoo = OpTypePointer Uniform %ufoo
1809 %uniform_index_buffer = OpVariable %_ptr_Uniform_ufoo Uniform
1810 %_ptr_Uniform_uint = OpTypePointer Uniform %uint
1811 %_ptr_StorageBuffer_v4float = OpTypePointer StorageBuffer %v4float
1812 %_ptr_Output_v4float = OpTypePointer Output %v4float
1813 %uint_0 = OpConstant %uint 0
1814 %bool = OpTypeBool
1815 %40 = OpTypeFunction %void %uint %uint %uint %uint
1816 %_runtimearr_uint = OpTypeRuntimeArray %uint
1817 %_struct_47 = OpTypeStruct %uint %_runtimearr_uint
1818 %_ptr_StorageBuffer__struct_47 = OpTypePointer StorageBuffer %_struct_47
1819 %49 = OpVariable %_ptr_StorageBuffer__struct_47 StorageBuffer
1820 %_ptr_StorageBuffer_uint = OpTypePointer StorageBuffer %uint
1821 %uint_10 = OpConstant %uint 10
1822 %uint_4 = OpConstant %uint 4
1823 %uint_23 = OpConstant %uint 23
1824 %uint_2 = OpConstant %uint 2
1825 %uint_3 = OpConstant %uint 3
1826 %_ptr_Input_uint = OpTypePointer Input %uint
1827 %gl_PrimitiveID = OpVariable %_ptr_Input_uint Input
1828 %v3float = OpTypeVector %float 3
1829 %_ptr_Input_v3float = OpTypePointer Input %v3float
1830 %gl_TessCoord = OpVariable %_ptr_Input_v3float Input
1831 %v3uint = OpTypeVector %uint 3
1832 %uint_5 = OpConstant %uint 5
1833 %uint_6 = OpConstant %uint 6
1834 %uint_7 = OpConstant %uint 7
1835 %uint_8 = OpConstant %uint 8
1836 %uint_9 = OpConstant %uint 9
1837 %uint_63 = OpConstant %uint 63
1838 %101 = OpConstantNull %v4float
1839 )";
1840
1841 const std::string func_before =
1842 R"(%main = OpFunction %void None %3
1843 %5 = OpLabel
1844 %25 = OpAccessChain %_ptr_Uniform_uint %uniform_index_buffer %int_0
1845 %26 = OpLoad %uint %25
1846 %28 = OpAccessChain %_ptr_StorageBuffer_v4float %adds %26 %int_0
1847 %29 = OpLoad %v4float %28
1848 %31 = OpAccessChain %_ptr_Output_v4float %_ %int_0
1849 OpStore %31 %29
1850 OpReturn
1851 OpFunctionEnd
1852 )";
1853
1854 const std::string func_after =
1855 R"(%main = OpFunction %void None %10
1856 %26 = OpLabel
1857 %27 = OpAccessChain %_ptr_Uniform_uint %uniform_index_buffer %int_0
1858 %28 = OpLoad %uint %27
1859 %29 = OpAccessChain %_ptr_StorageBuffer_v4float %adds %28 %int_0
1860 %34 = OpULessThan %bool %28 %uint_11
1861 OpSelectionMerge %35 None
1862 OpBranchConditional %34 %36 %37
1863 %36 = OpLabel
1864 %38 = OpLoad %v4float %29
1865 OpBranch %35
1866 %37 = OpLabel
1867 %100 = OpFunctionCall %void %39 %uint_63 %uint_0 %28 %uint_11
1868 OpBranch %35
1869 %35 = OpLabel
1870 %102 = OpPhi %v4float %38 %36 %101 %37
1871 %31 = OpAccessChain %_ptr_Output_v4float %_ %int_0
1872 OpStore %31 %102
1873 OpReturn
1874 OpFunctionEnd
1875 )";
1876
1877 const std::string output_func =
1878 R"(%39 = OpFunction %void None %40
1879 %41 = OpFunctionParameter %uint
1880 %42 = OpFunctionParameter %uint
1881 %43 = OpFunctionParameter %uint
1882 %44 = OpFunctionParameter %uint
1883 %45 = OpLabel
1884 %51 = OpAccessChain %_ptr_StorageBuffer_uint %49 %uint_0
1885 %54 = OpAtomicIAdd %uint %51 %uint_4 %uint_0 %uint_10
1886 %55 = OpIAdd %uint %54 %uint_10
1887 %56 = OpArrayLength %uint %49 1
1888 %57 = OpULessThanEqual %bool %55 %56
1889 OpSelectionMerge %58 None
1890 OpBranchConditional %57 %59 %58
1891 %59 = OpLabel
1892 %60 = OpIAdd %uint %54 %uint_0
1893 %61 = OpAccessChain %_ptr_StorageBuffer_uint %49 %uint_1 %60
1894 OpStore %61 %uint_10
1895 %63 = OpIAdd %uint %54 %uint_1
1896 %64 = OpAccessChain %_ptr_StorageBuffer_uint %49 %uint_1 %63
1897 OpStore %64 %uint_23
1898 %66 = OpIAdd %uint %54 %uint_2
1899 %67 = OpAccessChain %_ptr_StorageBuffer_uint %49 %uint_1 %66
1900 OpStore %67 %41
1901 %69 = OpIAdd %uint %54 %uint_3
1902 %70 = OpAccessChain %_ptr_StorageBuffer_uint %49 %uint_1 %69
1903 OpStore %70 %uint_2
1904 %73 = OpLoad %uint %gl_PrimitiveID
1905 %74 = OpIAdd %uint %54 %uint_4
1906 %75 = OpAccessChain %_ptr_StorageBuffer_uint %49 %uint_1 %74
1907 OpStore %75 %73
1908 %79 = OpLoad %v3float %gl_TessCoord
1909 %81 = OpBitcast %v3uint %79
1910 %82 = OpCompositeExtract %uint %81 0
1911 %83 = OpCompositeExtract %uint %81 1
1912 %85 = OpIAdd %uint %54 %uint_5
1913 %86 = OpAccessChain %_ptr_StorageBuffer_uint %49 %uint_1 %85
1914 OpStore %86 %82
1915 %88 = OpIAdd %uint %54 %uint_6
1916 %89 = OpAccessChain %_ptr_StorageBuffer_uint %49 %uint_1 %88
1917 OpStore %89 %83
1918 %91 = OpIAdd %uint %54 %uint_7
1919 %92 = OpAccessChain %_ptr_StorageBuffer_uint %49 %uint_1 %91
1920 OpStore %92 %42
1921 %94 = OpIAdd %uint %54 %uint_8
1922 %95 = OpAccessChain %_ptr_StorageBuffer_uint %49 %uint_1 %94
1923 OpStore %95 %43
1924 %97 = OpIAdd %uint %54 %uint_9
1925 %98 = OpAccessChain %_ptr_StorageBuffer_uint %49 %uint_1 %97
1926 OpStore %98 %44
1927 OpBranch %58
1928 %58 = OpLabel
1929 OpReturn
1930 OpFunctionEnd
1931 )";
1932
1933 // SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
1934 SinglePassRunAndCheck<InstBindlessCheckPass>(
1935 defs_before + func_before, defs_after + func_after + output_func, true,
1936 true, 7u, 23u, false, false);
1937 }
1938
1939 TEST_F(InstBindlessTest, MultipleDebugFunctions) {
1940 // Same source as Simple, but compiled -g and not optimized, especially not
1941 // inlined. The OpSource has had the source extracted for the sake of brevity.
1942
1943 const std::string defs_before =
1944 R"(OpCapability Shader
1945 %2 = OpExtInstImport "GLSL.std.450"
1946 OpMemoryModel Logical GLSL450
1947 OpEntryPoint Fragment %MainPs "MainPs" %i_vTextureCoords %_entryPointOutput_vColor
1948 OpExecutionMode %MainPs OriginUpperLeft
1949 %1 = OpString "foo5.frag"
1950 OpSource HLSL 500 %1
1951 OpName %MainPs "MainPs"
1952 OpName %PS_INPUT "PS_INPUT"
1953 OpMemberName %PS_INPUT 0 "vTextureCoords"
1954 OpName %PS_OUTPUT "PS_OUTPUT"
1955 OpMemberName %PS_OUTPUT 0 "vColor"
1956 OpName %_MainPs_struct_PS_INPUT_vf21_ "@MainPs(struct-PS_INPUT-vf21;"
1957 OpName %i "i"
1958 OpName %ps_output "ps_output"
1959 OpName %g_tColor "g_tColor"
1960 OpName %PerViewConstantBuffer_t "PerViewConstantBuffer_t"
1961 OpMemberName %PerViewConstantBuffer_t 0 "g_nDataIdx"
1962 OpName %_ ""
1963 OpName %g_sAniso "g_sAniso"
1964 OpName %i_0 "i"
1965 OpName %i_vTextureCoords "i.vTextureCoords"
1966 OpName %_entryPointOutput_vColor "@entryPointOutput.vColor"
1967 OpName %param "param"
1968 OpDecorate %g_tColor DescriptorSet 0
1969 OpDecorate %g_tColor Binding 0
1970 OpMemberDecorate %PerViewConstantBuffer_t 0 Offset 0
1971 OpDecorate %PerViewConstantBuffer_t Block
1972 OpDecorate %g_sAniso DescriptorSet 0
1973 OpDecorate %g_sAniso Binding 1
1974 OpDecorate %i_vTextureCoords Location 0
1975 OpDecorate %_entryPointOutput_vColor Location 0
1976 %void = OpTypeVoid
1977 %4 = OpTypeFunction %void
1978 %float = OpTypeFloat 32
1979 %v2float = OpTypeVector %float 2
1980 %PS_INPUT = OpTypeStruct %v2float
1981 %_ptr_Function_PS_INPUT = OpTypePointer Function %PS_INPUT
1982 %v4float = OpTypeVector %float 4
1983 %PS_OUTPUT = OpTypeStruct %v4float
1984 %13 = OpTypeFunction %PS_OUTPUT %_ptr_Function_PS_INPUT
1985 %_ptr_Function_PS_OUTPUT = OpTypePointer Function %PS_OUTPUT
1986 %int = OpTypeInt 32 1
1987 %int_0 = OpConstant %int 0
1988 %21 = OpTypeImage %float 2D 0 0 0 1 Unknown
1989 %uint = OpTypeInt 32 0
1990 %uint_128 = OpConstant %uint 128
1991 %_arr_21_uint_128 = OpTypeArray %21 %uint_128
1992 %_ptr_UniformConstant__arr_21_uint_128 = OpTypePointer UniformConstant %_arr_21_uint_128
1993 %g_tColor = OpVariable %_ptr_UniformConstant__arr_21_uint_128 UniformConstant
1994 %PerViewConstantBuffer_t = OpTypeStruct %uint
1995 %_ptr_PushConstant_PerViewConstantBuffer_t = OpTypePointer PushConstant %PerViewConstantBuffer_t
1996 %_ = OpVariable %_ptr_PushConstant_PerViewConstantBuffer_t PushConstant
1997 %_ptr_PushConstant_uint = OpTypePointer PushConstant %uint
1998 %_ptr_UniformConstant_21 = OpTypePointer UniformConstant %21
1999 %36 = OpTypeSampler
2000 %_ptr_UniformConstant_36 = OpTypePointer UniformConstant %36
2001 %g_sAniso = OpVariable %_ptr_UniformConstant_36 UniformConstant
2002 %40 = OpTypeSampledImage %21
2003 %_ptr_Function_v2float = OpTypePointer Function %v2float
2004 %_ptr_Function_v4float = OpTypePointer Function %v4float
2005 %_ptr_Input_v2float = OpTypePointer Input %v2float
2006 %i_vTextureCoords = OpVariable %_ptr_Input_v2float Input
2007 %_ptr_Output_v4float = OpTypePointer Output %v4float
2008 %_entryPointOutput_vColor = OpVariable %_ptr_Output_v4float Output
2009 )";
2010
2011 const std::string defs_after =
2012 R"(OpCapability Shader
2013 OpExtension "SPV_KHR_storage_buffer_storage_class"
2014 %1 = OpExtInstImport "GLSL.std.450"
2015 OpMemoryModel Logical GLSL450
2016 OpEntryPoint Fragment %MainPs "MainPs" %i_vTextureCoords %_entryPointOutput_vColor %gl_FragCoord
2017 OpExecutionMode %MainPs OriginUpperLeft
2018 %5 = OpString "foo5.frag"
2019 OpSource HLSL 500 %5
2020 OpName %MainPs "MainPs"
2021 OpName %PS_INPUT "PS_INPUT"
2022 OpMemberName %PS_INPUT 0 "vTextureCoords"
2023 OpName %PS_OUTPUT "PS_OUTPUT"
2024 OpMemberName %PS_OUTPUT 0 "vColor"
2025 OpName %_MainPs_struct_PS_INPUT_vf21_ "@MainPs(struct-PS_INPUT-vf21;"
2026 OpName %i "i"
2027 OpName %ps_output "ps_output"
2028 OpName %g_tColor "g_tColor"
2029 OpName %PerViewConstantBuffer_t "PerViewConstantBuffer_t"
2030 OpMemberName %PerViewConstantBuffer_t 0 "g_nDataIdx"
2031 OpName %_ ""
2032 OpName %g_sAniso "g_sAniso"
2033 OpName %i_0 "i"
2034 OpName %i_vTextureCoords "i.vTextureCoords"
2035 OpName %_entryPointOutput_vColor "@entryPointOutput.vColor"
2036 OpName %param "param"
2037 OpDecorate %g_tColor DescriptorSet 0
2038 OpDecorate %g_tColor Binding 0
2039 OpMemberDecorate %PerViewConstantBuffer_t 0 Offset 0
2040 OpDecorate %PerViewConstantBuffer_t Block
2041 OpDecorate %g_sAniso DescriptorSet 0
2042 OpDecorate %g_sAniso Binding 1
2043 OpDecorate %i_vTextureCoords Location 0
2044 OpDecorate %_entryPointOutput_vColor Location 0
2045 OpDecorate %_runtimearr_uint ArrayStride 4
2046 OpDecorate %_struct_77 Block
2047 OpMemberDecorate %_struct_77 0 Offset 0
2048 OpMemberDecorate %_struct_77 1 Offset 4
2049 OpDecorate %79 DescriptorSet 7
2050 OpDecorate %79 Binding 0
2051 OpDecorate %gl_FragCoord BuiltIn FragCoord
2052 %void = OpTypeVoid
2053 %18 = OpTypeFunction %void
2054 %float = OpTypeFloat 32
2055 %v2float = OpTypeVector %float 2
2056 %PS_INPUT = OpTypeStruct %v2float
2057 %_ptr_Function_PS_INPUT = OpTypePointer Function %PS_INPUT
2058 %v4float = OpTypeVector %float 4
2059 %PS_OUTPUT = OpTypeStruct %v4float
2060 %23 = OpTypeFunction %PS_OUTPUT %_ptr_Function_PS_INPUT
2061 %_ptr_Function_PS_OUTPUT = OpTypePointer Function %PS_OUTPUT
2062 %int = OpTypeInt 32 1
2063 %int_0 = OpConstant %int 0
2064 %27 = OpTypeImage %float 2D 0 0 0 1 Unknown
2065 %uint = OpTypeInt 32 0
2066 %uint_128 = OpConstant %uint 128
2067 %_arr_27_uint_128 = OpTypeArray %27 %uint_128
2068 %_ptr_UniformConstant__arr_27_uint_128 = OpTypePointer UniformConstant %_arr_27_uint_128
2069 %g_tColor = OpVariable %_ptr_UniformConstant__arr_27_uint_128 UniformConstant
2070 %PerViewConstantBuffer_t = OpTypeStruct %uint
2071 %_ptr_PushConstant_PerViewConstantBuffer_t = OpTypePointer PushConstant %PerViewConstantBuffer_t
2072 %_ = OpVariable %_ptr_PushConstant_PerViewConstantBuffer_t PushConstant
2073 %_ptr_PushConstant_uint = OpTypePointer PushConstant %uint
2074 %_ptr_UniformConstant_27 = OpTypePointer UniformConstant %27
2075 %35 = OpTypeSampler
2076 %_ptr_UniformConstant_35 = OpTypePointer UniformConstant %35
2077 %g_sAniso = OpVariable %_ptr_UniformConstant_35 UniformConstant
2078 %37 = OpTypeSampledImage %27
2079 %_ptr_Function_v2float = OpTypePointer Function %v2float
2080 %_ptr_Function_v4float = OpTypePointer Function %v4float
2081 %_ptr_Input_v2float = OpTypePointer Input %v2float
2082 %i_vTextureCoords = OpVariable %_ptr_Input_v2float Input
2083 %_ptr_Output_v4float = OpTypePointer Output %v4float
2084 %_entryPointOutput_vColor = OpVariable %_ptr_Output_v4float Output
2085 %uint_0 = OpConstant %uint 0
2086 %bool = OpTypeBool
2087 %70 = OpTypeFunction %void %uint %uint %uint %uint
2088 %_runtimearr_uint = OpTypeRuntimeArray %uint
2089 %_struct_77 = OpTypeStruct %uint %_runtimearr_uint
2090 %_ptr_StorageBuffer__struct_77 = OpTypePointer StorageBuffer %_struct_77
2091 %79 = OpVariable %_ptr_StorageBuffer__struct_77 StorageBuffer
2092 %_ptr_StorageBuffer_uint = OpTypePointer StorageBuffer %uint
2093 %uint_10 = OpConstant %uint 10
2094 %uint_4 = OpConstant %uint 4
2095 %uint_1 = OpConstant %uint 1
2096 %uint_23 = OpConstant %uint 23
2097 %uint_2 = OpConstant %uint 2
2098 %uint_3 = OpConstant %uint 3
2099 %_ptr_Input_v4float = OpTypePointer Input %v4float
2100 %gl_FragCoord = OpVariable %_ptr_Input_v4float Input
2101 %v4uint = OpTypeVector %uint 4
2102 %uint_5 = OpConstant %uint 5
2103 %uint_7 = OpConstant %uint 7
2104 %uint_8 = OpConstant %uint 8
2105 %uint_9 = OpConstant %uint 9
2106 %uint_109 = OpConstant %uint 109
2107 %125 = OpConstantNull %v4float
2108 )";
2109
2110 const std::string func1_before =
2111 R"(%MainPs = OpFunction %void None %4
2112 %6 = OpLabel
2113 %i_0 = OpVariable %_ptr_Function_PS_INPUT Function
2114 %param = OpVariable %_ptr_Function_PS_INPUT Function
2115 OpLine %1 21 0
2116 %54 = OpLoad %v2float %i_vTextureCoords
2117 %55 = OpAccessChain %_ptr_Function_v2float %i_0 %int_0
2118 OpStore %55 %54
2119 %59 = OpLoad %PS_INPUT %i_0
2120 OpStore %param %59
2121 %60 = OpFunctionCall %PS_OUTPUT %_MainPs_struct_PS_INPUT_vf21_ %param
2122 %61 = OpCompositeExtract %v4float %60 0
2123 OpStore %_entryPointOutput_vColor %61
2124 OpReturn
2125 OpFunctionEnd
2126 )";
2127
2128 const std::string func1_after =
2129 R"(%MainPs = OpFunction %void None %18
2130 %42 = OpLabel
2131 %i_0 = OpVariable %_ptr_Function_PS_INPUT Function
2132 %param = OpVariable %_ptr_Function_PS_INPUT Function
2133 OpLine %5 21 0
2134 %43 = OpLoad %v2float %i_vTextureCoords
2135 %44 = OpAccessChain %_ptr_Function_v2float %i_0 %int_0
2136 OpStore %44 %43
2137 %45 = OpLoad %PS_INPUT %i_0
2138 OpStore %param %45
2139 %46 = OpFunctionCall %PS_OUTPUT %_MainPs_struct_PS_INPUT_vf21_ %param
2140 %47 = OpCompositeExtract %v4float %46 0
2141 OpStore %_entryPointOutput_vColor %47
2142 OpReturn
2143 OpFunctionEnd
2144 )";
2145
2146 const std::string func2_before =
2147 R"(%_MainPs_struct_PS_INPUT_vf21_ = OpFunction %PS_OUTPUT None %13
2148 %i = OpFunctionParameter %_ptr_Function_PS_INPUT
2149 %16 = OpLabel
2150 %ps_output = OpVariable %_ptr_Function_PS_OUTPUT Function
2151 OpLine %1 24 0
2152 %31 = OpAccessChain %_ptr_PushConstant_uint %_ %int_0
2153 %32 = OpLoad %uint %31
2154 %34 = OpAccessChain %_ptr_UniformConstant_21 %g_tColor %32
2155 %35 = OpLoad %21 %34
2156 %39 = OpLoad %36 %g_sAniso
2157 %41 = OpSampledImage %40 %35 %39
2158 %43 = OpAccessChain %_ptr_Function_v2float %i %int_0
2159 %44 = OpLoad %v2float %43
2160 %45 = OpImageSampleImplicitLod %v4float %41 %44
2161 %47 = OpAccessChain %_ptr_Function_v4float %ps_output %int_0
2162 OpStore %47 %45
2163 OpLine %1 25 0
2164 %48 = OpLoad %PS_OUTPUT %ps_output
2165 OpReturnValue %48
2166 OpFunctionEnd
2167 )";
2168
2169 const std::string func2_after =
2170 R"(%_MainPs_struct_PS_INPUT_vf21_ = OpFunction %PS_OUTPUT None %23
2171 %i = OpFunctionParameter %_ptr_Function_PS_INPUT
2172 %48 = OpLabel
2173 %ps_output = OpVariable %_ptr_Function_PS_OUTPUT Function
2174 OpLine %5 24 0
2175 %49 = OpAccessChain %_ptr_PushConstant_uint %_ %int_0
2176 %50 = OpLoad %uint %49
2177 %51 = OpAccessChain %_ptr_UniformConstant_27 %g_tColor %50
2178 %52 = OpLoad %27 %51
2179 %53 = OpLoad %35 %g_sAniso
2180 %54 = OpSampledImage %37 %52 %53
2181 %55 = OpAccessChain %_ptr_Function_v2float %i %int_0
2182 %56 = OpLoad %v2float %55
2183 OpNoLine
2184 %62 = OpULessThan %bool %50 %uint_128
2185 OpSelectionMerge %63 None
2186 OpBranchConditional %62 %64 %65
2187 %64 = OpLabel
2188 %66 = OpLoad %27 %51
2189 %67 = OpSampledImage %37 %66 %53
2190 OpLine %5 24 0
2191 %68 = OpImageSampleImplicitLod %v4float %67 %56
2192 OpNoLine
2193 OpBranch %63
2194 %65 = OpLabel
2195 %124 = OpFunctionCall %void %69 %uint_109 %uint_0 %50 %uint_128
2196 OpBranch %63
2197 %63 = OpLabel
2198 %126 = OpPhi %v4float %68 %64 %125 %65
2199 OpLine %5 24 0
2200 %58 = OpAccessChain %_ptr_Function_v4float %ps_output %int_0
2201 OpStore %58 %126
2202 OpLine %5 25 0
2203 %59 = OpLoad %PS_OUTPUT %ps_output
2204 OpReturnValue %59
2205 OpFunctionEnd
2206 )";
2207
2208 const std::string output_func =
2209 R"(%69 = OpFunction %void None %70
2210 %71 = OpFunctionParameter %uint
2211 %72 = OpFunctionParameter %uint
2212 %73 = OpFunctionParameter %uint
2213 %74 = OpFunctionParameter %uint
2214 %75 = OpLabel
2215 %81 = OpAccessChain %_ptr_StorageBuffer_uint %79 %uint_0
2216 %84 = OpAtomicIAdd %uint %81 %uint_4 %uint_0 %uint_10
2217 %85 = OpIAdd %uint %84 %uint_10
2218 %86 = OpArrayLength %uint %79 1
2219 %87 = OpULessThanEqual %bool %85 %86
2220 OpSelectionMerge %88 None
2221 OpBranchConditional %87 %89 %88
2222 %89 = OpLabel
2223 %90 = OpIAdd %uint %84 %uint_0
2224 %92 = OpAccessChain %_ptr_StorageBuffer_uint %79 %uint_1 %90
2225 OpStore %92 %uint_10
2226 %94 = OpIAdd %uint %84 %uint_1
2227 %95 = OpAccessChain %_ptr_StorageBuffer_uint %79 %uint_1 %94
2228 OpStore %95 %uint_23
2229 %97 = OpIAdd %uint %84 %uint_2
2230 %98 = OpAccessChain %_ptr_StorageBuffer_uint %79 %uint_1 %97
2231 OpStore %98 %71
2232 %100 = OpIAdd %uint %84 %uint_3
2233 %101 = OpAccessChain %_ptr_StorageBuffer_uint %79 %uint_1 %100
2234 OpStore %101 %uint_4
2235 %104 = OpLoad %v4float %gl_FragCoord
2236 %106 = OpBitcast %v4uint %104
2237 %107 = OpCompositeExtract %uint %106 0
2238 %108 = OpIAdd %uint %84 %uint_4
2239 %109 = OpAccessChain %_ptr_StorageBuffer_uint %79 %uint_1 %108
2240 OpStore %109 %107
2241 %110 = OpCompositeExtract %uint %106 1
2242 %112 = OpIAdd %uint %84 %uint_5
2243 %113 = OpAccessChain %_ptr_StorageBuffer_uint %79 %uint_1 %112
2244 OpStore %113 %110
2245 %115 = OpIAdd %uint %84 %uint_7
2246 %116 = OpAccessChain %_ptr_StorageBuffer_uint %79 %uint_1 %115
2247 OpStore %116 %72
2248 %118 = OpIAdd %uint %84 %uint_8
2249 %119 = OpAccessChain %_ptr_StorageBuffer_uint %79 %uint_1 %118
2250 OpStore %119 %73
2251 %121 = OpIAdd %uint %84 %uint_9
2252 %122 = OpAccessChain %_ptr_StorageBuffer_uint %79 %uint_1 %121
2253 OpStore %122 %74
2254 OpBranch %88
2255 %88 = OpLabel
2256 OpReturn
2257 OpFunctionEnd
2258 )";
2259
2260 // SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
2261 SinglePassRunAndCheck<InstBindlessCheckPass>(
2262 defs_before + func1_before + func2_before,
2263 defs_after + func1_after + func2_after + output_func, true, true, 7u, 23u,
2264 false, false);
2265 }
2266
2267 TEST_F(InstBindlessTest, RuntimeArray) {
2268 // This test verifies that the pass will correctly instrument shader
2269 // with runtime descriptor array. This test was created by editing the
2270 // SPIR-V from the Simple test.
2271
2272 const std::string defs_before =
2273 R"(OpCapability Shader
2274 OpCapability RuntimeDescriptorArray
2275 OpExtension "SPV_EXT_descriptor_indexing"
2276 %1 = OpExtInstImport "GLSL.std.450"
2277 OpMemoryModel Logical GLSL450
2278 OpEntryPoint Fragment %MainPs "MainPs" %i_vTextureCoords %_entryPointOutput_vColor
2279 OpExecutionMode %MainPs OriginUpperLeft
2280 OpSource HLSL 500
2281 OpName %MainPs "MainPs"
2282 OpName %g_tColor "g_tColor"
2283 OpName %PerViewConstantBuffer_t "PerViewConstantBuffer_t"
2284 OpMemberName %PerViewConstantBuffer_t 0 "g_nDataIdx"
2285 OpName %_ ""
2286 OpName %g_sAniso "g_sAniso"
2287 OpName %i_vTextureCoords "i.vTextureCoords"
2288 OpName %_entryPointOutput_vColor "@entryPointOutput.vColor"
2289 OpDecorate %g_tColor DescriptorSet 1
2290 OpDecorate %g_tColor Binding 2
2291 OpMemberDecorate %PerViewConstantBuffer_t 0 Offset 0
2292 OpDecorate %PerViewConstantBuffer_t Block
2293 OpDecorate %g_sAniso DescriptorSet 1
2294 OpDecorate %g_sAniso Binding 0
2295 OpDecorate %i_vTextureCoords Location 0
2296 OpDecorate %_entryPointOutput_vColor Location 0
2297 %void = OpTypeVoid
2298 %3 = OpTypeFunction %void
2299 %float = OpTypeFloat 32
2300 %v2float = OpTypeVector %float 2
2301 %v4float = OpTypeVector %float 4
2302 %int = OpTypeInt 32 1
2303 %int_0 = OpConstant %int 0
2304 %20 = OpTypeImage %float 2D 0 0 0 1 Unknown
2305 %uint = OpTypeInt 32 0
2306 %uint_1 = OpConstant %uint 1
2307 %_rarr_20 = OpTypeRuntimeArray %20
2308 %_ptr_UniformConstant__arr_20 = OpTypePointer UniformConstant %_rarr_20
2309 %g_tColor = OpVariable %_ptr_UniformConstant__arr_20 UniformConstant
2310 %PerViewConstantBuffer_t = OpTypeStruct %uint
2311 %_ptr_PushConstant_PerViewConstantBuffer_t = OpTypePointer PushConstant %PerViewConstantBuffer_t
2312 %_ = OpVariable %_ptr_PushConstant_PerViewConstantBuffer_t PushConstant
2313 %_ptr_PushConstant_uint = OpTypePointer PushConstant %uint
2314 %_ptr_UniformConstant_20 = OpTypePointer UniformConstant %20
2315 %35 = OpTypeSampler
2316 %_ptr_UniformConstant_35 = OpTypePointer UniformConstant %35
2317 %g_sAniso = OpVariable %_ptr_UniformConstant_35 UniformConstant
2318 %39 = OpTypeSampledImage %20
2319 %_ptr_Input_v2float = OpTypePointer Input %v2float
2320 %i_vTextureCoords = OpVariable %_ptr_Input_v2float Input
2321 %_ptr_Output_v4float = OpTypePointer Output %v4float
2322 %_entryPointOutput_vColor = OpVariable %_ptr_Output_v4float Output
2323 )";
2324
2325 const std::string defs_after =
2326 R"(OpCapability Shader
2327 OpCapability RuntimeDescriptorArray
2328 OpExtension "SPV_EXT_descriptor_indexing"
2329 OpExtension "SPV_KHR_storage_buffer_storage_class"
2330 %1 = OpExtInstImport "GLSL.std.450"
2331 OpMemoryModel Logical GLSL450
2332 OpEntryPoint Fragment %MainPs "MainPs" %i_vTextureCoords %_entryPointOutput_vColor %gl_FragCoord
2333 OpExecutionMode %MainPs OriginUpperLeft
2334 OpSource HLSL 500
2335 OpName %MainPs "MainPs"
2336 OpName %g_tColor "g_tColor"
2337 OpName %PerViewConstantBuffer_t "PerViewConstantBuffer_t"
2338 OpMemberName %PerViewConstantBuffer_t 0 "g_nDataIdx"
2339 OpName %_ ""
2340 OpName %g_sAniso "g_sAniso"
2341 OpName %i_vTextureCoords "i.vTextureCoords"
2342 OpName %_entryPointOutput_vColor "@entryPointOutput.vColor"
2343 OpDecorate %g_tColor DescriptorSet 1
2344 OpDecorate %g_tColor Binding 2
2345 OpMemberDecorate %PerViewConstantBuffer_t 0 Offset 0
2346 OpDecorate %PerViewConstantBuffer_t Block
2347 OpDecorate %g_sAniso DescriptorSet 1
2348 OpDecorate %g_sAniso Binding 0
2349 OpDecorate %i_vTextureCoords Location 0
2350 OpDecorate %_entryPointOutput_vColor Location 0
2351 OpDecorate %_runtimearr_uint ArrayStride 4
2352 OpDecorate %_struct_46 Block
2353 OpMemberDecorate %_struct_46 0 Offset 0
2354 OpDecorate %48 DescriptorSet 7
2355 OpDecorate %48 Binding 1
2356 OpDecorate %_struct_71 Block
2357 OpMemberDecorate %_struct_71 0 Offset 0
2358 OpMemberDecorate %_struct_71 1 Offset 4
2359 OpDecorate %73 DescriptorSet 7
2360 OpDecorate %73 Binding 0
2361 OpDecorate %gl_FragCoord BuiltIn FragCoord
2362 %void = OpTypeVoid
2363 %10 = OpTypeFunction %void
2364 %float = OpTypeFloat 32
2365 %v2float = OpTypeVector %float 2
2366 %v4float = OpTypeVector %float 4
2367 %int = OpTypeInt 32 1
2368 %int_0 = OpConstant %int 0
2369 %16 = OpTypeImage %float 2D 0 0 0 1 Unknown
2370 %uint = OpTypeInt 32 0
2371 %uint_1 = OpConstant %uint 1
2372 %_runtimearr_16 = OpTypeRuntimeArray %16
2373 %_ptr_UniformConstant__runtimearr_16 = OpTypePointer UniformConstant %_runtimearr_16
2374 %g_tColor = OpVariable %_ptr_UniformConstant__runtimearr_16 UniformConstant
2375 %PerViewConstantBuffer_t = OpTypeStruct %uint
2376 %_ptr_PushConstant_PerViewConstantBuffer_t = OpTypePointer PushConstant %PerViewConstantBuffer_t
2377 %_ = OpVariable %_ptr_PushConstant_PerViewConstantBuffer_t PushConstant
2378 %_ptr_PushConstant_uint = OpTypePointer PushConstant %uint
2379 %_ptr_UniformConstant_16 = OpTypePointer UniformConstant %16
2380 %24 = OpTypeSampler
2381 %_ptr_UniformConstant_24 = OpTypePointer UniformConstant %24
2382 %g_sAniso = OpVariable %_ptr_UniformConstant_24 UniformConstant
2383 %26 = OpTypeSampledImage %16
2384 %_ptr_Input_v2float = OpTypePointer Input %v2float
2385 %i_vTextureCoords = OpVariable %_ptr_Input_v2float Input
2386 %_ptr_Output_v4float = OpTypePointer Output %v4float
2387 %_entryPointOutput_vColor = OpVariable %_ptr_Output_v4float Output
2388 %uint_0 = OpConstant %uint 0
2389 %uint_2 = OpConstant %uint 2
2390 %41 = OpTypeFunction %uint %uint %uint
2391 %_runtimearr_uint = OpTypeRuntimeArray %uint
2392 %_struct_46 = OpTypeStruct %_runtimearr_uint
2393 %_ptr_StorageBuffer__struct_46 = OpTypePointer StorageBuffer %_struct_46
2394 %48 = OpVariable %_ptr_StorageBuffer__struct_46 StorageBuffer
2395 %_ptr_StorageBuffer_uint = OpTypePointer StorageBuffer %uint
2396 %bool = OpTypeBool
2397 %65 = OpTypeFunction %void %uint %uint %uint %uint
2398 %_struct_71 = OpTypeStruct %uint %_runtimearr_uint
2399 %_ptr_StorageBuffer__struct_71 = OpTypePointer StorageBuffer %_struct_71
2400 %73 = OpVariable %_ptr_StorageBuffer__struct_71 StorageBuffer
2401 %uint_10 = OpConstant %uint 10
2402 %uint_4 = OpConstant %uint 4
2403 %uint_23 = OpConstant %uint 23
2404 %uint_3 = OpConstant %uint 3
2405 %_ptr_Input_v4float = OpTypePointer Input %v4float
2406 %gl_FragCoord = OpVariable %_ptr_Input_v4float Input
2407 %v4uint = OpTypeVector %uint 4
2408 %uint_5 = OpConstant %uint 5
2409 %uint_7 = OpConstant %uint 7
2410 %uint_8 = OpConstant %uint 8
2411 %uint_9 = OpConstant %uint 9
2412 %uint_59 = OpConstant %uint 59
2413 %116 = OpConstantNull %v4float
2414 %119 = OpTypeFunction %uint %uint %uint %uint %uint
2415 )";
2416
2417 const std::string func_before =
2418 R"(%MainPs = OpFunction %void None %3
2419 %5 = OpLabel
2420 %53 = OpLoad %v2float %i_vTextureCoords
2421 %63 = OpAccessChain %_ptr_PushConstant_uint %_ %int_0
2422 %64 = OpLoad %uint %63
2423 %65 = OpAccessChain %_ptr_UniformConstant_20 %g_tColor %64
2424 %66 = OpLoad %20 %65
2425 %67 = OpLoad %35 %g_sAniso
2426 %68 = OpSampledImage %39 %66 %67
2427 %71 = OpImageSampleImplicitLod %v4float %68 %53
2428 OpStore %_entryPointOutput_vColor %71
2429 OpReturn
2430 OpFunctionEnd
2431 )";
2432
2433 const std::string func_after =
2434 R"(%MainPs = OpFunction %void None %10
2435 %29 = OpLabel
2436 %30 = OpLoad %v2float %i_vTextureCoords
2437 %31 = OpAccessChain %_ptr_PushConstant_uint %_ %int_0
2438 %32 = OpLoad %uint %31
2439 %33 = OpAccessChain %_ptr_UniformConstant_16 %g_tColor %32
2440 %34 = OpLoad %16 %33
2441 %35 = OpLoad %24 %g_sAniso
2442 %36 = OpSampledImage %26 %34 %35
2443 %55 = OpFunctionCall %uint %40 %uint_2 %uint_2
2444 %57 = OpULessThan %bool %32 %55
2445 OpSelectionMerge %58 None
2446 OpBranchConditional %57 %59 %60
2447 %59 = OpLabel
2448 %61 = OpLoad %16 %33
2449 %62 = OpSampledImage %26 %61 %35
2450 %136 = OpFunctionCall %uint %118 %uint_0 %uint_1 %uint_2 %32
2451 %137 = OpULessThan %bool %uint_0 %136
2452 OpSelectionMerge %138 None
2453 OpBranchConditional %137 %139 %140
2454 %139 = OpLabel
2455 %141 = OpLoad %16 %33
2456 %142 = OpSampledImage %26 %141 %35
2457 %143 = OpImageSampleImplicitLod %v4float %142 %30
2458 OpBranch %138
2459 %140 = OpLabel
2460 %144 = OpFunctionCall %void %64 %uint_59 %uint_1 %32 %uint_0
2461 OpBranch %138
2462 %138 = OpLabel
2463 %145 = OpPhi %v4float %143 %139 %116 %140
2464 OpBranch %58
2465 %60 = OpLabel
2466 %115 = OpFunctionCall %void %64 %uint_59 %uint_0 %32 %55
2467 OpBranch %58
2468 %58 = OpLabel
2469 %117 = OpPhi %v4float %145 %138 %116 %60
2470 OpStore %_entryPointOutput_vColor %117
2471 OpReturn
2472 OpFunctionEnd
2473 )";
2474
2475 const std::string new_funcs =
2476 R"(%40 = OpFunction %uint None %41
2477 %42 = OpFunctionParameter %uint
2478 %43 = OpFunctionParameter %uint
2479 %44 = OpLabel
2480 %50 = OpAccessChain %_ptr_StorageBuffer_uint %48 %uint_0 %42
2481 %51 = OpLoad %uint %50
2482 %52 = OpIAdd %uint %51 %43
2483 %53 = OpAccessChain %_ptr_StorageBuffer_uint %48 %uint_0 %52
2484 %54 = OpLoad %uint %53
2485 OpReturnValue %54
2486 OpFunctionEnd
2487 %64 = OpFunction %void None %65
2488 %66 = OpFunctionParameter %uint
2489 %67 = OpFunctionParameter %uint
2490 %68 = OpFunctionParameter %uint
2491 %69 = OpFunctionParameter %uint
2492 %70 = OpLabel
2493 %74 = OpAccessChain %_ptr_StorageBuffer_uint %73 %uint_0
2494 %77 = OpAtomicIAdd %uint %74 %uint_4 %uint_0 %uint_10
2495 %78 = OpIAdd %uint %77 %uint_10
2496 %79 = OpArrayLength %uint %73 1
2497 %80 = OpULessThanEqual %bool %78 %79
2498 OpSelectionMerge %81 None
2499 OpBranchConditional %80 %82 %81
2500 %82 = OpLabel
2501 %83 = OpIAdd %uint %77 %uint_0
2502 %84 = OpAccessChain %_ptr_StorageBuffer_uint %73 %uint_1 %83
2503 OpStore %84 %uint_10
2504 %86 = OpIAdd %uint %77 %uint_1
2505 %87 = OpAccessChain %_ptr_StorageBuffer_uint %73 %uint_1 %86
2506 OpStore %87 %uint_23
2507 %88 = OpIAdd %uint %77 %uint_2
2508 %89 = OpAccessChain %_ptr_StorageBuffer_uint %73 %uint_1 %88
2509 OpStore %89 %66
2510 %91 = OpIAdd %uint %77 %uint_3
2511 %92 = OpAccessChain %_ptr_StorageBuffer_uint %73 %uint_1 %91
2512 OpStore %92 %uint_4
2513 %95 = OpLoad %v4float %gl_FragCoord
2514 %97 = OpBitcast %v4uint %95
2515 %98 = OpCompositeExtract %uint %97 0
2516 %99 = OpIAdd %uint %77 %uint_4
2517 %100 = OpAccessChain %_ptr_StorageBuffer_uint %73 %uint_1 %99
2518 OpStore %100 %98
2519 %101 = OpCompositeExtract %uint %97 1
2520 %103 = OpIAdd %uint %77 %uint_5
2521 %104 = OpAccessChain %_ptr_StorageBuffer_uint %73 %uint_1 %103
2522 OpStore %104 %101
2523 %106 = OpIAdd %uint %77 %uint_7
2524 %107 = OpAccessChain %_ptr_StorageBuffer_uint %73 %uint_1 %106
2525 OpStore %107 %67
2526 %109 = OpIAdd %uint %77 %uint_8
2527 %110 = OpAccessChain %_ptr_StorageBuffer_uint %73 %uint_1 %109
2528 OpStore %110 %68
2529 %112 = OpIAdd %uint %77 %uint_9
2530 %113 = OpAccessChain %_ptr_StorageBuffer_uint %73 %uint_1 %112
2531 OpStore %113 %69
2532 OpBranch %81
2533 %81 = OpLabel
2534 OpReturn
2535 OpFunctionEnd
2536 %118 = OpFunction %uint None %119
2537 %120 = OpFunctionParameter %uint
2538 %121 = OpFunctionParameter %uint
2539 %122 = OpFunctionParameter %uint
2540 %123 = OpFunctionParameter %uint
2541 %124 = OpLabel
2542 %125 = OpAccessChain %_ptr_StorageBuffer_uint %48 %uint_0 %120
2543 %126 = OpLoad %uint %125
2544 %127 = OpIAdd %uint %126 %121
2545 %128 = OpAccessChain %_ptr_StorageBuffer_uint %48 %uint_0 %127
2546 %129 = OpLoad %uint %128
2547 %130 = OpIAdd %uint %129 %122
2548 %131 = OpAccessChain %_ptr_StorageBuffer_uint %48 %uint_0 %130
2549 %132 = OpLoad %uint %131
2550 %133 = OpIAdd %uint %132 %123
2551 %134 = OpAccessChain %_ptr_StorageBuffer_uint %48 %uint_0 %133
2552 %135 = OpLoad %uint %134
2553 OpReturnValue %135
2554 OpFunctionEnd
2555 )";
2556
2557 // SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
2558 SinglePassRunAndCheck<InstBindlessCheckPass>(
2559 defs_before + func_before, defs_after + func_after + new_funcs, true,
2560 true, 7u, 23u, true, true);
2561 }
2562
2563 TEST_F(InstBindlessTest, InstrumentInitCheckOnScalarDescriptor) {
2564 // This test verifies that the pass will correctly instrument vanilla
2565 // texture sample on a scalar descriptor with an initialization check if the
2566 // input_init_enable argument is set to true. This can happen when the
2567 // descriptor indexing extension is enabled in the API but the SPIR-V
2568 // does not have the extension enabled because it does not contain a
2569 // runtime array. This is the same shader as NoInstrumentNonBindless.
2570
2571 const std::string defs_before =
2572 R"(OpCapability Shader
2573 %1 = OpExtInstImport "GLSL.std.450"
2574 OpMemoryModel Logical GLSL450
2575 OpEntryPoint Fragment %MainPs "MainPs" %i_vTextureCoords %_entryPointOutput_vColor
2576 OpExecutionMode %MainPs OriginUpperLeft
2577 OpSource HLSL 500
2578 OpName %MainPs "MainPs"
2579 OpName %g_tColor "g_tColor"
2580 OpName %g_sAniso "g_sAniso"
2581 OpName %i_vTextureCoords "i.vTextureCoords"
2582 OpName %_entryPointOutput_vColor "@entryPointOutput.vColor"
2583 OpDecorate %g_tColor DescriptorSet 0
2584 OpDecorate %g_tColor Binding 0
2585 OpDecorate %g_sAniso DescriptorSet 0
2586 OpDecorate %g_sAniso Binding 0
2587 OpDecorate %i_vTextureCoords Location 0
2588 OpDecorate %_entryPointOutput_vColor Location 0
2589 %void = OpTypeVoid
2590 %8 = OpTypeFunction %void
2591 %float = OpTypeFloat 32
2592 %v2float = OpTypeVector %float 2
2593 %v4float = OpTypeVector %float 4
2594 %12 = OpTypeImage %float 2D 0 0 0 1 Unknown
2595 %_ptr_UniformConstant_12 = OpTypePointer UniformConstant %12
2596 %g_tColor = OpVariable %_ptr_UniformConstant_12 UniformConstant
2597 %14 = OpTypeSampler
2598 %_ptr_UniformConstant_14 = OpTypePointer UniformConstant %14
2599 %g_sAniso = OpVariable %_ptr_UniformConstant_14 UniformConstant
2600 %16 = OpTypeSampledImage %12
2601 %_ptr_Input_v2float = OpTypePointer Input %v2float
2602 %i_vTextureCoords = OpVariable %_ptr_Input_v2float Input
2603 %_ptr_Output_v4float = OpTypePointer Output %v4float
2604 %_entryPointOutput_vColor = OpVariable %_ptr_Output_v4float Output
2605 )";
2606
2607 const std::string defs_after =
2608 R"(OpCapability Shader
2609 OpExtension "SPV_KHR_storage_buffer_storage_class"
2610 %1 = OpExtInstImport "GLSL.std.450"
2611 OpMemoryModel Logical GLSL450
2612 OpEntryPoint Fragment %MainPs "MainPs" %i_vTextureCoords %_entryPointOutput_vColor %gl_FragCoord
2613 OpExecutionMode %MainPs OriginUpperLeft
2614 OpSource HLSL 500
2615 OpName %MainPs "MainPs"
2616 OpName %g_tColor "g_tColor"
2617 OpName %g_sAniso "g_sAniso"
2618 OpName %i_vTextureCoords "i.vTextureCoords"
2619 OpName %_entryPointOutput_vColor "@entryPointOutput.vColor"
2620 OpDecorate %g_tColor DescriptorSet 0
2621 OpDecorate %g_tColor Binding 0
2622 OpDecorate %g_sAniso DescriptorSet 0
2623 OpDecorate %g_sAniso Binding 0
2624 OpDecorate %i_vTextureCoords Location 0
2625 OpDecorate %_entryPointOutput_vColor Location 0
2626 OpDecorate %_runtimearr_uint ArrayStride 4
2627 OpDecorate %_struct_35 Block
2628 OpMemberDecorate %_struct_35 0 Offset 0
2629 OpDecorate %37 DescriptorSet 7
2630 OpDecorate %37 Binding 1
2631 OpDecorate %_struct_67 Block
2632 OpMemberDecorate %_struct_67 0 Offset 0
2633 OpMemberDecorate %_struct_67 1 Offset 4
2634 OpDecorate %69 DescriptorSet 7
2635 OpDecorate %69 Binding 0
2636 OpDecorate %gl_FragCoord BuiltIn FragCoord
2637 %void = OpTypeVoid
2638 %8 = OpTypeFunction %void
2639 %float = OpTypeFloat 32
2640 %v2float = OpTypeVector %float 2
2641 %v4float = OpTypeVector %float 4
2642 %12 = OpTypeImage %float 2D 0 0 0 1 Unknown
2643 %_ptr_UniformConstant_12 = OpTypePointer UniformConstant %12
2644 %g_tColor = OpVariable %_ptr_UniformConstant_12 UniformConstant
2645 %14 = OpTypeSampler
2646 %_ptr_UniformConstant_14 = OpTypePointer UniformConstant %14
2647 %g_sAniso = OpVariable %_ptr_UniformConstant_14 UniformConstant
2648 %16 = OpTypeSampledImage %12
2649 %_ptr_Input_v2float = OpTypePointer Input %v2float
2650 %i_vTextureCoords = OpVariable %_ptr_Input_v2float Input
2651 %_ptr_Output_v4float = OpTypePointer Output %v4float
2652 %_entryPointOutput_vColor = OpVariable %_ptr_Output_v4float Output
2653 %uint = OpTypeInt 32 0
2654 %uint_0 = OpConstant %uint 0
2655 %28 = OpTypeFunction %uint %uint %uint %uint %uint
2656 %_runtimearr_uint = OpTypeRuntimeArray %uint
2657 %_struct_35 = OpTypeStruct %_runtimearr_uint
2658 %_ptr_StorageBuffer__struct_35 = OpTypePointer StorageBuffer %_struct_35
2659 %37 = OpVariable %_ptr_StorageBuffer__struct_35 StorageBuffer
2660 %_ptr_StorageBuffer_uint = OpTypePointer StorageBuffer %uint
2661 %bool = OpTypeBool
2662 %uint_1 = OpConstant %uint 1
2663 %61 = OpTypeFunction %void %uint %uint %uint %uint
2664 %_struct_67 = OpTypeStruct %uint %_runtimearr_uint
2665 %_ptr_StorageBuffer__struct_67 = OpTypePointer StorageBuffer %_struct_67
2666 %69 = OpVariable %_ptr_StorageBuffer__struct_67 StorageBuffer
2667 %uint_10 = OpConstant %uint 10
2668 %uint_4 = OpConstant %uint 4
2669 %uint_23 = OpConstant %uint 23
2670 %uint_2 = OpConstant %uint 2
2671 %uint_3 = OpConstant %uint 3
2672 %_ptr_Input_v4float = OpTypePointer Input %v4float
2673 %gl_FragCoord = OpVariable %_ptr_Input_v4float Input
2674 %v4uint = OpTypeVector %uint 4
2675 %uint_5 = OpConstant %uint 5
2676 %uint_7 = OpConstant %uint 7
2677 %uint_8 = OpConstant %uint 8
2678 %uint_9 = OpConstant %uint 9
2679 %uint_39 = OpConstant %uint 39
2680 %113 = OpConstantNull %v4float
2681 )";
2682
2683 const std::string func_before =
2684 R"(%MainPs = OpFunction %void None %8
2685 %19 = OpLabel
2686 %20 = OpLoad %v2float %i_vTextureCoords
2687 %21 = OpLoad %12 %g_tColor
2688 %22 = OpLoad %14 %g_sAniso
2689 %23 = OpSampledImage %16 %21 %22
2690 %24 = OpImageSampleImplicitLod %v4float %23 %20
2691 OpStore %_entryPointOutput_vColor %24
2692 OpReturn
2693 OpFunctionEnd
2694 )";
2695
2696 const std::string func_after =
2697 R"(%MainPs = OpFunction %void None %8
2698 %19 = OpLabel
2699 %20 = OpLoad %v2float %i_vTextureCoords
2700 %21 = OpLoad %12 %g_tColor
2701 %22 = OpLoad %14 %g_sAniso
2702 %23 = OpSampledImage %16 %21 %22
2703 %50 = OpFunctionCall %uint %27 %uint_0 %uint_0 %uint_0 %uint_0
2704 %52 = OpULessThan %bool %uint_0 %50
2705 OpSelectionMerge %54 None
2706 OpBranchConditional %52 %55 %56
2707 %55 = OpLabel
2708 %57 = OpLoad %12 %g_tColor
2709 %58 = OpSampledImage %16 %57 %22
2710 %59 = OpImageSampleImplicitLod %v4float %58 %20
2711 OpBranch %54
2712 %56 = OpLabel
2713 %112 = OpFunctionCall %void %60 %uint_39 %uint_1 %uint_0 %uint_0
2714 OpBranch %54
2715 %54 = OpLabel
2716 %114 = OpPhi %v4float %59 %55 %113 %56
2717 OpStore %_entryPointOutput_vColor %114
2718 OpReturn
2719 OpFunctionEnd
2720 )";
2721
2722 const std::string new_funcs =
2723 R"(%27 = OpFunction %uint None %28
2724 %29 = OpFunctionParameter %uint
2725 %30 = OpFunctionParameter %uint
2726 %31 = OpFunctionParameter %uint
2727 %32 = OpFunctionParameter %uint
2728 %33 = OpLabel
2729 %39 = OpAccessChain %_ptr_StorageBuffer_uint %37 %uint_0 %29
2730 %40 = OpLoad %uint %39
2731 %41 = OpIAdd %uint %40 %30
2732 %42 = OpAccessChain %_ptr_StorageBuffer_uint %37 %uint_0 %41
2733 %43 = OpLoad %uint %42
2734 %44 = OpIAdd %uint %43 %31
2735 %45 = OpAccessChain %_ptr_StorageBuffer_uint %37 %uint_0 %44
2736 %46 = OpLoad %uint %45
2737 %47 = OpIAdd %uint %46 %32
2738 %48 = OpAccessChain %_ptr_StorageBuffer_uint %37 %uint_0 %47
2739 %49 = OpLoad %uint %48
2740 OpReturnValue %49
2741 OpFunctionEnd
2742 %60 = OpFunction %void None %61
2743 %62 = OpFunctionParameter %uint
2744 %63 = OpFunctionParameter %uint
2745 %64 = OpFunctionParameter %uint
2746 %65 = OpFunctionParameter %uint
2747 %66 = OpLabel
2748 %70 = OpAccessChain %_ptr_StorageBuffer_uint %69 %uint_0
2749 %73 = OpAtomicIAdd %uint %70 %uint_4 %uint_0 %uint_10
2750 %74 = OpIAdd %uint %73 %uint_10
2751 %75 = OpArrayLength %uint %69 1
2752 %76 = OpULessThanEqual %bool %74 %75
2753 OpSelectionMerge %77 None
2754 OpBranchConditional %76 %78 %77
2755 %78 = OpLabel
2756 %79 = OpIAdd %uint %73 %uint_0
2757 %80 = OpAccessChain %_ptr_StorageBuffer_uint %69 %uint_1 %79
2758 OpStore %80 %uint_10
2759 %82 = OpIAdd %uint %73 %uint_1
2760 %83 = OpAccessChain %_ptr_StorageBuffer_uint %69 %uint_1 %82
2761 OpStore %83 %uint_23
2762 %85 = OpIAdd %uint %73 %uint_2
2763 %86 = OpAccessChain %_ptr_StorageBuffer_uint %69 %uint_1 %85
2764 OpStore %86 %62
2765 %88 = OpIAdd %uint %73 %uint_3
2766 %89 = OpAccessChain %_ptr_StorageBuffer_uint %69 %uint_1 %88
2767 OpStore %89 %uint_4
2768 %92 = OpLoad %v4float %gl_FragCoord
2769 %94 = OpBitcast %v4uint %92
2770 %95 = OpCompositeExtract %uint %94 0
2771 %96 = OpIAdd %uint %73 %uint_4
2772 %97 = OpAccessChain %_ptr_StorageBuffer_uint %69 %uint_1 %96
2773 OpStore %97 %95
2774 %98 = OpCompositeExtract %uint %94 1
2775 %100 = OpIAdd %uint %73 %uint_5
2776 %101 = OpAccessChain %_ptr_StorageBuffer_uint %69 %uint_1 %100
2777 OpStore %101 %98
2778 %103 = OpIAdd %uint %73 %uint_7
2779 %104 = OpAccessChain %_ptr_StorageBuffer_uint %69 %uint_1 %103
2780 OpStore %104 %63
2781 %106 = OpIAdd %uint %73 %uint_8
2782 %107 = OpAccessChain %_ptr_StorageBuffer_uint %69 %uint_1 %106
2783 OpStore %107 %64
2784 %109 = OpIAdd %uint %73 %uint_9
2785 %110 = OpAccessChain %_ptr_StorageBuffer_uint %69 %uint_1 %109
2786 OpStore %110 %65
2787 OpBranch %77
2788 %77 = OpLabel
2789 OpReturn
2790 OpFunctionEnd
2791 )";
2792
2793 // SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
2794 SinglePassRunAndCheck<InstBindlessCheckPass>(
2795 defs_before + func_before, defs_after + func_after + new_funcs, true,
2796 true, 7u, 23u, true, true);
2797 }
2798
2799 TEST_F(InstBindlessTest, SPV14AddToEntryPoint) {
2800 const std::string text = R"(
2801 ; CHECK: OpEntryPoint Fragment {{%\w+}} "foo" {{%\w+}} {{%\w+}} {{%\w+}} [[v1:%\w+]] [[v2:%\w+]]
2802 ; CHECK: OpDecorate [[v1]] DescriptorSet 7
2803 ; CHECK: OpDecorate [[v2]] DescriptorSet 7
2804 ; CHECK: [[v1]] = OpVariable {{%\w+}} StorageBuffer
2805 ; CHECK: [[v2]] = OpVariable {{%\w+}} StorageBuffer
2806 OpCapability Shader
2807 OpExtension "SPV_EXT_descriptor_indexing"
2808 OpMemoryModel Logical GLSL450
2809 OpEntryPoint Fragment %foo "foo" %gid %image_var %sampler_var
2810 OpExecutionMode %foo OriginUpperLeft
2811 OpDecorate %image_var DescriptorSet 0
2812 OpDecorate %image_var Binding 0
2813 OpDecorate %sampler_var DescriptorSet 0
2814 OpDecorate %sampler_var Binding 1
2815 OpDecorate %gid DescriptorSet 0
2816 OpDecorate %gid Binding 2
2817 OpDecorate %struct Block
2818 OpMemberDecorate %struct 0 Offset 0
2819 %void = OpTypeVoid
2820 %int = OpTypeInt 32 0
2821 %int_0 = OpConstant %int 0
2822 %v3int = OpTypeVector %int 3
2823 %float = OpTypeFloat 32
2824 %v3float = OpTypeVector %float 3
2825 %v4float = OpTypeVector %float 4
2826 %struct = OpTypeStruct %v3int
2827 %ptr_ssbo_struct = OpTypePointer StorageBuffer %struct
2828 %ptr_ssbo_v3int = OpTypePointer StorageBuffer %v3int
2829 %gid = OpVariable %ptr_ssbo_struct StorageBuffer
2830 %image = OpTypeImage %float 3D 0 0 0 1 Unknown
2831 %ptr_uc_image = OpTypePointer UniformConstant %image
2832 %sampler = OpTypeSampler
2833 %ptr_uc_sampler = OpTypePointer UniformConstant %sampler
2834 %image_var = OpVariable %ptr_uc_image UniformConstant
2835 %sampler_var = OpVariable %ptr_uc_sampler UniformConstant
2836 %sampled = OpTypeSampledImage %image
2837 %void_fn = OpTypeFunction %void
2838 %foo = OpFunction %void None %void_fn
2839 %entry = OpLabel
2840 %ld_image = OpLoad %image %image_var
2841 %ld_sampler = OpLoad %sampler %sampler_var
2842 %gep = OpAccessChain %ptr_ssbo_v3int %gid %int_0
2843 %ld_gid = OpLoad %v3int %gep
2844 %convert = OpConvertUToF %v3float %ld_gid
2845 %sampled_image = OpSampledImage %sampled %ld_image %ld_sampler
2846 %sample = OpImageSampleImplicitLod %v4float %sampled_image %convert
2847 OpReturn
2848 OpFunctionEnd
2849 )";
2850
2851 SetTargetEnv(SPV_ENV_VULKAN_1_1_SPIRV_1_4);
2852 SinglePassRunAndMatch<InstBindlessCheckPass>(text, true, 7u, 23u, true, true);
2853 }
2854
2855 TEST_F(InstBindlessTest, SPV14AddToEntryPoints) {
2856 const std::string text = R"(
2857 ; CHECK: OpEntryPoint Fragment {{%\w+}} "foo" {{%\w+}} {{%\w+}} {{%\w+}} [[v1:%\w+]] [[v2:%\w+]]
2858 ; CHECK: OpEntryPoint Fragment {{%\w+}} "bar" {{%\w+}} {{%\w+}} {{%\w+}} [[v1:%\w+]] [[v2:%\w+]]
2859 ; CHECK: OpDecorate [[v1]] DescriptorSet 7
2860 ; CHECK: OpDecorate [[v2]] DescriptorSet 7
2861 ; CHECK: [[v1]] = OpVariable {{%\w+}} StorageBuffer
2862 ; CHECK: [[v2]] = OpVariable {{%\w+}} StorageBuffer
2863 OpCapability Shader
2864 OpExtension "SPV_EXT_descriptor_indexing"
2865 OpMemoryModel Logical GLSL450
2866 OpEntryPoint Fragment %foo "foo" %gid %image_var %sampler_var
2867 OpEntryPoint Fragment %foo "bar" %gid %image_var %sampler_var
2868 OpExecutionMode %foo OriginUpperLeft
2869 OpDecorate %image_var DescriptorSet 0
2870 OpDecorate %image_var Binding 0
2871 OpDecorate %sampler_var DescriptorSet 0
2872 OpDecorate %sampler_var Binding 1
2873 OpDecorate %gid DescriptorSet 0
2874 OpDecorate %gid Binding 2
2875 OpDecorate %struct Block
2876 OpMemberDecorate %struct 0 Offset 0
2877 %void = OpTypeVoid
2878 %int = OpTypeInt 32 0
2879 %int_0 = OpConstant %int 0
2880 %v3int = OpTypeVector %int 3
2881 %float = OpTypeFloat 32
2882 %v3float = OpTypeVector %float 3
2883 %v4float = OpTypeVector %float 4
2884 %struct = OpTypeStruct %v3int
2885 %ptr_ssbo_struct = OpTypePointer StorageBuffer %struct
2886 %ptr_ssbo_v3int = OpTypePointer StorageBuffer %v3int
2887 %gid = OpVariable %ptr_ssbo_struct StorageBuffer
2888 %image = OpTypeImage %float 3D 0 0 0 1 Unknown
2889 %ptr_uc_image = OpTypePointer UniformConstant %image
2890 %sampler = OpTypeSampler
2891 %ptr_uc_sampler = OpTypePointer UniformConstant %sampler
2892 %image_var = OpVariable %ptr_uc_image UniformConstant
2893 %sampler_var = OpVariable %ptr_uc_sampler UniformConstant
2894 %sampled = OpTypeSampledImage %image
2895 %void_fn = OpTypeFunction %void
2896 %foo = OpFunction %void None %void_fn
2897 %entry = OpLabel
2898 %ld_image = OpLoad %image %image_var
2899 %ld_sampler = OpLoad %sampler %sampler_var
2900 %gep = OpAccessChain %ptr_ssbo_v3int %gid %int_0
2901 %ld_gid = OpLoad %v3int %gep
2902 %convert = OpConvertUToF %v3float %ld_gid
2903 %sampled_image = OpSampledImage %sampled %ld_image %ld_sampler
2904 %sample = OpImageSampleImplicitLod %v4float %sampled_image %convert
2905 OpReturn
2906 OpFunctionEnd
2907 )";
2908
2909 SetTargetEnv(SPV_ENV_VULKAN_1_1_SPIRV_1_4);
2910 SinglePassRunAndMatch<InstBindlessCheckPass>(text, true, 7u, 23u, true, true);
2911 }
2912
2913 TEST_F(InstBindlessTest, InstBoundsAndInitLoadUnsizedUBOArray) {
2914 // #version 450
2915 // #extension GL_EXT_nonuniform_qualifier : enable
2916 //
2917 // layout(location=0) in nonuniformEXT flat int nu_ii;
2918 // layout(location=0) out float b;
2919 //
2920 // layout(binding=3) uniform uname { float a; } uniformBuffer[];
2921 //
2922 // void main()
2923 // {
2924 // b = uniformBuffer[nu_ii].a;
2925 // }
2926
2927 const std::string defs_before =
2928 R"(OpCapability Shader
2929 OpCapability ShaderNonUniform
2930 OpCapability RuntimeDescriptorArray
2931 OpCapability UniformBufferArrayNonUniformIndexing
2932 OpExtension "SPV_EXT_descriptor_indexing"
2933 %1 = OpExtInstImport "GLSL.std.450"
2934 OpMemoryModel Logical GLSL450
2935 OpEntryPoint Fragment %main "main" %b %nu_ii
2936 OpExecutionMode %main OriginUpperLeft
2937 OpSource GLSL 450
2938 OpSourceExtension "GL_EXT_nonuniform_qualifier"
2939 OpName %main "main"
2940 OpName %b "b"
2941 OpName %uname "uname"
2942 OpMemberName %uname 0 "a"
2943 OpName %uniformBuffer "uniformBuffer"
2944 OpName %nu_ii "nu_ii"
2945 OpDecorate %b Location 0
2946 OpMemberDecorate %uname 0 Offset 0
2947 OpDecorate %uname Block
2948 OpDecorate %uniformBuffer DescriptorSet 0
2949 OpDecorate %uniformBuffer Binding 3
2950 OpDecorate %nu_ii Flat
2951 OpDecorate %nu_ii Location 0
2952 OpDecorate %nu_ii NonUniform
2953 OpDecorate %16 NonUniform
2954 OpDecorate %20 NonUniform
2955 %void = OpTypeVoid
2956 %3 = OpTypeFunction %void
2957 %float = OpTypeFloat 32
2958 %_ptr_Output_float = OpTypePointer Output %float
2959 %b = OpVariable %_ptr_Output_float Output
2960 %uname = OpTypeStruct %float
2961 %_runtimearr_uname = OpTypeRuntimeArray %uname
2962 %_ptr_Uniform__runtimearr_uname = OpTypePointer Uniform %_runtimearr_uname
2963 %uniformBuffer = OpVariable %_ptr_Uniform__runtimearr_uname Uniform
2964 %int = OpTypeInt 32 1
2965 %_ptr_Input_int = OpTypePointer Input %int
2966 %nu_ii = OpVariable %_ptr_Input_int Input
2967 %int_0 = OpConstant %int 0
2968 %_ptr_Uniform_float = OpTypePointer Uniform %float
2969 )";
2970
2971 const std::string defs_after =
2972 R"(OpCapability Shader
2973 OpCapability ShaderNonUniform
2974 OpCapability RuntimeDescriptorArray
2975 OpCapability UniformBufferArrayNonUniformIndexing
2976 OpExtension "SPV_EXT_descriptor_indexing"
2977 OpExtension "SPV_KHR_storage_buffer_storage_class"
2978 %1 = OpExtInstImport "GLSL.std.450"
2979 OpMemoryModel Logical GLSL450
2980 OpEntryPoint Fragment %main "main" %b %nu_ii %gl_FragCoord
2981 OpExecutionMode %main OriginUpperLeft
2982 OpSource GLSL 450
2983 OpSourceExtension "GL_EXT_nonuniform_qualifier"
2984 OpName %main "main"
2985 OpName %b "b"
2986 OpName %uname "uname"
2987 OpMemberName %uname 0 "a"
2988 OpName %uniformBuffer "uniformBuffer"
2989 OpName %nu_ii "nu_ii"
2990 OpDecorate %b Location 0
2991 OpMemberDecorate %uname 0 Offset 0
2992 OpDecorate %uname Block
2993 OpDecorate %uniformBuffer DescriptorSet 0
2994 OpDecorate %uniformBuffer Binding 3
2995 OpDecorate %nu_ii Flat
2996 OpDecorate %nu_ii Location 0
2997 OpDecorate %nu_ii NonUniform
2998 OpDecorate %7 NonUniform
2999 OpDecorate %102 NonUniform
3000 OpDecorate %_runtimearr_uint ArrayStride 4
3001 OpDecorate %_struct_31 Block
3002 OpMemberDecorate %_struct_31 0 Offset 0
3003 OpDecorate %33 DescriptorSet 7
3004 OpDecorate %33 Binding 1
3005 OpDecorate %130 NonUniform
3006 OpDecorate %_struct_55 Block
3007 OpMemberDecorate %_struct_55 0 Offset 0
3008 OpMemberDecorate %_struct_55 1 Offset 4
3009 OpDecorate %57 DescriptorSet 7
3010 OpDecorate %57 Binding 0
3011 OpDecorate %gl_FragCoord BuiltIn FragCoord
3012 OpDecorate %127 NonUniform
3013 %void = OpTypeVoid
3014 %10 = OpTypeFunction %void
3015 %float = OpTypeFloat 32
3016 %_ptr_Output_float = OpTypePointer Output %float
3017 %b = OpVariable %_ptr_Output_float Output
3018 %uname = OpTypeStruct %float
3019 %_runtimearr_uname = OpTypeRuntimeArray %uname
3020 %_ptr_Uniform__runtimearr_uname = OpTypePointer Uniform %_runtimearr_uname
3021 %uniformBuffer = OpVariable %_ptr_Uniform__runtimearr_uname Uniform
3022 %int = OpTypeInt 32 1
3023 %_ptr_Input_int = OpTypePointer Input %int
3024 %nu_ii = OpVariable %_ptr_Input_int Input
3025 %int_0 = OpConstant %int 0
3026 %_ptr_Uniform_float = OpTypePointer Uniform %float
3027 %uint = OpTypeInt 32 0
3028 %uint_0 = OpConstant %uint 0
3029 %uint_1 = OpConstant %uint 1
3030 %uint_3 = OpConstant %uint 3
3031 %26 = OpTypeFunction %uint %uint %uint
3032 %_runtimearr_uint = OpTypeRuntimeArray %uint
3033 %_struct_31 = OpTypeStruct %_runtimearr_uint
3034 %_ptr_StorageBuffer__struct_31 = OpTypePointer StorageBuffer %_struct_31
3035 %33 = OpVariable %_ptr_StorageBuffer__struct_31 StorageBuffer
3036 %_ptr_StorageBuffer_uint = OpTypePointer StorageBuffer %uint
3037 %bool = OpTypeBool
3038 %49 = OpTypeFunction %void %uint %uint %uint %uint
3039 %_struct_55 = OpTypeStruct %uint %_runtimearr_uint
3040 %_ptr_StorageBuffer__struct_55 = OpTypePointer StorageBuffer %_struct_55
3041 %57 = OpVariable %_ptr_StorageBuffer__struct_55 StorageBuffer
3042 %uint_10 = OpConstant %uint 10
3043 %uint_4 = OpConstant %uint 4
3044 %uint_23 = OpConstant %uint 23
3045 %uint_2 = OpConstant %uint 2
3046 %v4float = OpTypeVector %float 4
3047 %_ptr_Input_v4float = OpTypePointer Input %v4float
3048 %gl_FragCoord = OpVariable %_ptr_Input_v4float Input
3049 %v4uint = OpTypeVector %uint 4
3050 %uint_5 = OpConstant %uint 5
3051 %uint_7 = OpConstant %uint 7
3052 %uint_8 = OpConstant %uint 8
3053 %uint_9 = OpConstant %uint 9
3054 %uint_45 = OpConstant %uint 45
3055 %101 = OpConstantNull %float
3056 %105 = OpTypeFunction %uint %uint %uint %uint %uint
3057 )";
3058
3059 const std::string func_before =
3060 R"(%main = OpFunction %void None %3
3061 %5 = OpLabel
3062 %16 = OpLoad %int %nu_ii
3063 %19 = OpAccessChain %_ptr_Uniform_float %uniformBuffer %16 %int_0
3064 %20 = OpLoad %float %19
3065 OpStore %b %20
3066 OpReturn
3067 OpFunctionEnd
3068 )";
3069
3070 const std::string func_after =
3071 R"(%main = OpFunction %void None %10
3072 %19 = OpLabel
3073 %7 = OpLoad %int %nu_ii
3074 %20 = OpAccessChain %_ptr_Uniform_float %uniformBuffer %7 %int_0
3075 %40 = OpFunctionCall %uint %25 %uint_1 %uint_3
3076 %42 = OpULessThan %bool %7 %40
3077 OpSelectionMerge %43 None
3078 OpBranchConditional %42 %44 %45
3079 %44 = OpLabel
3080 %103 = OpBitcast %uint %7
3081 %122 = OpFunctionCall %uint %104 %uint_0 %uint_0 %uint_3 %103
3082 %123 = OpULessThan %bool %uint_0 %122
3083 OpSelectionMerge %124 None
3084 OpBranchConditional %123 %125 %126
3085 %125 = OpLabel
3086 %127 = OpLoad %float %20
3087 OpBranch %124
3088 %126 = OpLabel
3089 %128 = OpBitcast %uint %7
3090 %129 = OpFunctionCall %void %48 %uint_45 %uint_1 %128 %uint_0
3091 OpBranch %124
3092 %124 = OpLabel
3093 %130 = OpPhi %float %127 %125 %101 %126
3094 OpBranch %43
3095 %45 = OpLabel
3096 %47 = OpBitcast %uint %7
3097 %100 = OpFunctionCall %void %48 %uint_45 %uint_0 %47 %40
3098 OpBranch %43
3099 %43 = OpLabel
3100 %102 = OpPhi %float %130 %124 %101 %45
3101 OpStore %b %102
3102 OpReturn
3103 OpFunctionEnd
3104 )";
3105
3106 const std::string new_funcs =
3107 R"(%25 = OpFunction %uint None %26
3108 %27 = OpFunctionParameter %uint
3109 %28 = OpFunctionParameter %uint
3110 %29 = OpLabel
3111 %35 = OpAccessChain %_ptr_StorageBuffer_uint %33 %uint_0 %27
3112 %36 = OpLoad %uint %35
3113 %37 = OpIAdd %uint %36 %28
3114 %38 = OpAccessChain %_ptr_StorageBuffer_uint %33 %uint_0 %37
3115 %39 = OpLoad %uint %38
3116 OpReturnValue %39
3117 OpFunctionEnd
3118 %48 = OpFunction %void None %49
3119 %50 = OpFunctionParameter %uint
3120 %51 = OpFunctionParameter %uint
3121 %52 = OpFunctionParameter %uint
3122 %53 = OpFunctionParameter %uint
3123 %54 = OpLabel
3124 %58 = OpAccessChain %_ptr_StorageBuffer_uint %57 %uint_0
3125 %61 = OpAtomicIAdd %uint %58 %uint_4 %uint_0 %uint_10
3126 %62 = OpIAdd %uint %61 %uint_10
3127 %63 = OpArrayLength %uint %57 1
3128 %64 = OpULessThanEqual %bool %62 %63
3129 OpSelectionMerge %65 None
3130 OpBranchConditional %64 %66 %65
3131 %66 = OpLabel
3132 %67 = OpIAdd %uint %61 %uint_0
3133 %68 = OpAccessChain %_ptr_StorageBuffer_uint %57 %uint_1 %67
3134 OpStore %68 %uint_10
3135 %70 = OpIAdd %uint %61 %uint_1
3136 %71 = OpAccessChain %_ptr_StorageBuffer_uint %57 %uint_1 %70
3137 OpStore %71 %uint_23
3138 %73 = OpIAdd %uint %61 %uint_2
3139 %74 = OpAccessChain %_ptr_StorageBuffer_uint %57 %uint_1 %73
3140 OpStore %74 %50
3141 %75 = OpIAdd %uint %61 %uint_3
3142 %76 = OpAccessChain %_ptr_StorageBuffer_uint %57 %uint_1 %75
3143 OpStore %76 %uint_4
3144 %80 = OpLoad %v4float %gl_FragCoord
3145 %82 = OpBitcast %v4uint %80
3146 %83 = OpCompositeExtract %uint %82 0
3147 %84 = OpIAdd %uint %61 %uint_4
3148 %85 = OpAccessChain %_ptr_StorageBuffer_uint %57 %uint_1 %84
3149 OpStore %85 %83
3150 %86 = OpCompositeExtract %uint %82 1
3151 %88 = OpIAdd %uint %61 %uint_5
3152 %89 = OpAccessChain %_ptr_StorageBuffer_uint %57 %uint_1 %88
3153 OpStore %89 %86
3154 %91 = OpIAdd %uint %61 %uint_7
3155 %92 = OpAccessChain %_ptr_StorageBuffer_uint %57 %uint_1 %91
3156 OpStore %92 %51
3157 %94 = OpIAdd %uint %61 %uint_8
3158 %95 = OpAccessChain %_ptr_StorageBuffer_uint %57 %uint_1 %94
3159 OpStore %95 %52
3160 %97 = OpIAdd %uint %61 %uint_9
3161 %98 = OpAccessChain %_ptr_StorageBuffer_uint %57 %uint_1 %97
3162 OpStore %98 %53
3163 OpBranch %65
3164 %65 = OpLabel
3165 OpReturn
3166 OpFunctionEnd
3167 %104 = OpFunction %uint None %105
3168 %106 = OpFunctionParameter %uint
3169 %107 = OpFunctionParameter %uint
3170 %108 = OpFunctionParameter %uint
3171 %109 = OpFunctionParameter %uint
3172 %110 = OpLabel
3173 %111 = OpAccessChain %_ptr_StorageBuffer_uint %33 %uint_0 %106
3174 %112 = OpLoad %uint %111
3175 %113 = OpIAdd %uint %112 %107
3176 %114 = OpAccessChain %_ptr_StorageBuffer_uint %33 %uint_0 %113
3177 %115 = OpLoad %uint %114
3178 %116 = OpIAdd %uint %115 %108
3179 %117 = OpAccessChain %_ptr_StorageBuffer_uint %33 %uint_0 %116
3180 %118 = OpLoad %uint %117
3181 %119 = OpIAdd %uint %118 %109
3182 %120 = OpAccessChain %_ptr_StorageBuffer_uint %33 %uint_0 %119
3183 %121 = OpLoad %uint %120
3184 OpReturnValue %121
3185 OpFunctionEnd
3186 )";
3187
3188 // SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
3189 SinglePassRunAndCheck<InstBindlessCheckPass>(
3190 defs_before + func_before, defs_after + func_after + new_funcs, true,
3191 true, 7u, 23u, true, true);
3192 }
3193
3194 TEST_F(InstBindlessTest, InstBoundsAndInitLoadUnsizedSSBOArrayDeprecated) {
3195 // #version 450
3196 // #extension GL_EXT_nonuniform_qualifier : enable
3197 //
3198 // layout(location=0) in nonuniformEXT flat int nu_ii;
3199 // layout(location=0) out float b;
3200 //
3201 // layout(binding=3) buffer bname { float b; } storageBuffer[];
3202 //
3203 // void main()
3204 // {
3205 // b = storageBuffer[nu_ii].b;
3206 // }
3207
3208 const std::string defs_before =
3209 R"(OpCapability Shader
3210 OpCapability ShaderNonUniform
3211 OpCapability RuntimeDescriptorArray
3212 OpCapability StorageBufferArrayNonUniformIndexing
3213 OpExtension "SPV_EXT_descriptor_indexing"
3214 %1 = OpExtInstImport "GLSL.std.450"
3215 OpMemoryModel Logical GLSL450
3216 OpEntryPoint Fragment %main "main" %b %nu_ii
3217 OpExecutionMode %main OriginUpperLeft
3218 OpSource GLSL 450
3219 OpSourceExtension "GL_EXT_nonuniform_qualifier"
3220 OpName %main "main"
3221 OpName %b "b"
3222 OpName %bname "bname"
3223 OpMemberName %bname 0 "a"
3224 OpName %storageBuffer "storageBuffer"
3225 OpName %nu_ii "nu_ii"
3226 OpDecorate %b Location 0
3227 OpMemberDecorate %bname 0 Offset 0
3228 OpDecorate %bname Block
3229 OpDecorate %storageBuffer DescriptorSet 0
3230 OpDecorate %storageBuffer Binding 3
3231 OpDecorate %nu_ii Flat
3232 OpDecorate %nu_ii Location 0
3233 OpDecorate %nu_ii NonUniform
3234 OpDecorate %16 NonUniform
3235 OpDecorate %20 NonUniform
3236 %void = OpTypeVoid
3237 %3 = OpTypeFunction %void
3238 %float = OpTypeFloat 32
3239 %_ptr_Output_float = OpTypePointer Output %float
3240 %b = OpVariable %_ptr_Output_float Output
3241 %bname = OpTypeStruct %float
3242 %_runtimearr_bname = OpTypeRuntimeArray %bname
3243 %_ptr_StorageBuffer__runtimearr_bname = OpTypePointer StorageBuffer %_runtimearr_bname
3244 %storageBuffer = OpVariable %_ptr_StorageBuffer__runtimearr_bname StorageBuffer
3245 %int = OpTypeInt 32 1
3246 %_ptr_Input_int = OpTypePointer Input %int
3247 %nu_ii = OpVariable %_ptr_Input_int Input
3248 %int_0 = OpConstant %int 0
3249 %_ptr_StorageBuffer_float = OpTypePointer StorageBuffer %float
3250 )";
3251
3252 const std::string defs_after =
3253 R"(OpCapability Shader
3254 OpCapability ShaderNonUniform
3255 OpCapability RuntimeDescriptorArray
3256 OpCapability StorageBufferArrayNonUniformIndexing
3257 OpExtension "SPV_EXT_descriptor_indexing"
3258 OpExtension "SPV_KHR_storage_buffer_storage_class"
3259 %1 = OpExtInstImport "GLSL.std.450"
3260 OpMemoryModel Logical GLSL450
3261 OpEntryPoint Fragment %main "main" %b %nu_ii %gl_FragCoord
3262 OpExecutionMode %main OriginUpperLeft
3263 OpSource GLSL 450
3264 OpSourceExtension "GL_EXT_nonuniform_qualifier"
3265 OpName %main "main"
3266 OpName %b "b"
3267 OpName %bname "bname"
3268 OpMemberName %bname 0 "a"
3269 OpName %storageBuffer "storageBuffer"
3270 OpName %nu_ii "nu_ii"
3271 OpDecorate %b Location 0
3272 OpMemberDecorate %bname 0 Offset 0
3273 OpDecorate %bname Block
3274 OpDecorate %storageBuffer DescriptorSet 0
3275 OpDecorate %storageBuffer Binding 3
3276 OpDecorate %nu_ii Flat
3277 OpDecorate %nu_ii Location 0
3278 OpDecorate %nu_ii NonUniform
3279 OpDecorate %7 NonUniform
3280 OpDecorate %102 NonUniform
3281 OpDecorate %_runtimearr_uint ArrayStride 4
3282 OpDecorate %_struct_31 Block
3283 OpMemberDecorate %_struct_31 0 Offset 0
3284 OpDecorate %33 DescriptorSet 7
3285 OpDecorate %33 Binding 1
3286 OpDecorate %130 NonUniform
3287 OpDecorate %_struct_55 Block
3288 OpMemberDecorate %_struct_55 0 Offset 0
3289 OpMemberDecorate %_struct_55 1 Offset 4
3290 OpDecorate %57 DescriptorSet 7
3291 OpDecorate %57 Binding 0
3292 OpDecorate %gl_FragCoord BuiltIn FragCoord
3293 OpDecorate %127 NonUniform
3294 %void = OpTypeVoid
3295 %10 = OpTypeFunction %void
3296 %float = OpTypeFloat 32
3297 %_ptr_Output_float = OpTypePointer Output %float
3298 %b = OpVariable %_ptr_Output_float Output
3299 %bname = OpTypeStruct %float
3300 %_runtimearr_bname = OpTypeRuntimeArray %bname
3301 %_ptr_StorageBuffer__runtimearr_bname = OpTypePointer StorageBuffer %_runtimearr_bname
3302 %storageBuffer = OpVariable %_ptr_StorageBuffer__runtimearr_bname StorageBuffer
3303 %int = OpTypeInt 32 1
3304 %_ptr_Input_int = OpTypePointer Input %int
3305 %nu_ii = OpVariable %_ptr_Input_int Input
3306 %int_0 = OpConstant %int 0
3307 %_ptr_StorageBuffer_float = OpTypePointer StorageBuffer %float
3308 %uint = OpTypeInt 32 0
3309 %uint_0 = OpConstant %uint 0
3310 %uint_1 = OpConstant %uint 1
3311 %uint_3 = OpConstant %uint 3
3312 %26 = OpTypeFunction %uint %uint %uint
3313 %_runtimearr_uint = OpTypeRuntimeArray %uint
3314 %_struct_31 = OpTypeStruct %_runtimearr_uint
3315 %_ptr_StorageBuffer__struct_31 = OpTypePointer StorageBuffer %_struct_31
3316 %33 = OpVariable %_ptr_StorageBuffer__struct_31 StorageBuffer
3317 %_ptr_StorageBuffer_uint = OpTypePointer StorageBuffer %uint
3318 %bool = OpTypeBool
3319 %49 = OpTypeFunction %void %uint %uint %uint %uint
3320 %_struct_55 = OpTypeStruct %uint %_runtimearr_uint
3321 %_ptr_StorageBuffer__struct_55 = OpTypePointer StorageBuffer %_struct_55
3322 %57 = OpVariable %_ptr_StorageBuffer__struct_55 StorageBuffer
3323 %uint_10 = OpConstant %uint 10
3324 %uint_4 = OpConstant %uint 4
3325 %uint_23 = OpConstant %uint 23
3326 %uint_2 = OpConstant %uint 2
3327 %v4float = OpTypeVector %float 4
3328 %_ptr_Input_v4float = OpTypePointer Input %v4float
3329 %gl_FragCoord = OpVariable %_ptr_Input_v4float Input
3330 %v4uint = OpTypeVector %uint 4
3331 %uint_5 = OpConstant %uint 5
3332 %uint_7 = OpConstant %uint 7
3333 %uint_8 = OpConstant %uint 8
3334 %uint_9 = OpConstant %uint 9
3335 %uint_45 = OpConstant %uint 45
3336 %101 = OpConstantNull %float
3337 %105 = OpTypeFunction %uint %uint %uint %uint %uint
3338 )";
3339
3340 const std::string func_before =
3341 R"(%main = OpFunction %void None %3
3342 %5 = OpLabel
3343 %16 = OpLoad %int %nu_ii
3344 %19 = OpAccessChain %_ptr_StorageBuffer_float %storageBuffer %16 %int_0
3345 %20 = OpLoad %float %19
3346 OpStore %b %20
3347 OpReturn
3348 OpFunctionEnd
3349 )";
3350
3351 const std::string func_after =
3352 R"(%main = OpFunction %void None %10
3353 %19 = OpLabel
3354 %7 = OpLoad %int %nu_ii
3355 %20 = OpAccessChain %_ptr_StorageBuffer_float %storageBuffer %7 %int_0
3356 %40 = OpFunctionCall %uint %25 %uint_1 %uint_3
3357 %42 = OpULessThan %bool %7 %40
3358 OpSelectionMerge %43 None
3359 OpBranchConditional %42 %44 %45
3360 %44 = OpLabel
3361 %103 = OpBitcast %uint %7
3362 %122 = OpFunctionCall %uint %104 %uint_0 %uint_0 %uint_3 %103
3363 %123 = OpULessThan %bool %uint_0 %122
3364 OpSelectionMerge %124 None
3365 OpBranchConditional %123 %125 %126
3366 %125 = OpLabel
3367 %127 = OpLoad %float %20
3368 OpBranch %124
3369 %126 = OpLabel
3370 %128 = OpBitcast %uint %7
3371 %129 = OpFunctionCall %void %48 %uint_45 %uint_1 %128 %uint_0
3372 OpBranch %124
3373 %124 = OpLabel
3374 %130 = OpPhi %float %127 %125 %101 %126
3375 OpBranch %43
3376 %45 = OpLabel
3377 %47 = OpBitcast %uint %7
3378 %100 = OpFunctionCall %void %48 %uint_45 %uint_0 %47 %40
3379 OpBranch %43
3380 %43 = OpLabel
3381 %102 = OpPhi %float %130 %124 %101 %45
3382 OpStore %b %102
3383 OpReturn
3384 OpFunctionEnd
3385 )";
3386
3387 const std::string new_funcs =
3388 R"(%25 = OpFunction %uint None %26
3389 %27 = OpFunctionParameter %uint
3390 %28 = OpFunctionParameter %uint
3391 %29 = OpLabel
3392 %35 = OpAccessChain %_ptr_StorageBuffer_uint %33 %uint_0 %27
3393 %36 = OpLoad %uint %35
3394 %37 = OpIAdd %uint %36 %28
3395 %38 = OpAccessChain %_ptr_StorageBuffer_uint %33 %uint_0 %37
3396 %39 = OpLoad %uint %38
3397 OpReturnValue %39
3398 OpFunctionEnd
3399 %48 = OpFunction %void None %49
3400 %50 = OpFunctionParameter %uint
3401 %51 = OpFunctionParameter %uint
3402 %52 = OpFunctionParameter %uint
3403 %53 = OpFunctionParameter %uint
3404 %54 = OpLabel
3405 %58 = OpAccessChain %_ptr_StorageBuffer_uint %57 %uint_0
3406 %61 = OpAtomicIAdd %uint %58 %uint_4 %uint_0 %uint_10
3407 %62 = OpIAdd %uint %61 %uint_10
3408 %63 = OpArrayLength %uint %57 1
3409 %64 = OpULessThanEqual %bool %62 %63
3410 OpSelectionMerge %65 None
3411 OpBranchConditional %64 %66 %65
3412 %66 = OpLabel
3413 %67 = OpIAdd %uint %61 %uint_0
3414 %68 = OpAccessChain %_ptr_StorageBuffer_uint %57 %uint_1 %67
3415 OpStore %68 %uint_10
3416 %70 = OpIAdd %uint %61 %uint_1
3417 %71 = OpAccessChain %_ptr_StorageBuffer_uint %57 %uint_1 %70
3418 OpStore %71 %uint_23
3419 %73 = OpIAdd %uint %61 %uint_2
3420 %74 = OpAccessChain %_ptr_StorageBuffer_uint %57 %uint_1 %73
3421 OpStore %74 %50
3422 %75 = OpIAdd %uint %61 %uint_3
3423 %76 = OpAccessChain %_ptr_StorageBuffer_uint %57 %uint_1 %75
3424 OpStore %76 %uint_4
3425 %80 = OpLoad %v4float %gl_FragCoord
3426 %82 = OpBitcast %v4uint %80
3427 %83 = OpCompositeExtract %uint %82 0
3428 %84 = OpIAdd %uint %61 %uint_4
3429 %85 = OpAccessChain %_ptr_StorageBuffer_uint %57 %uint_1 %84
3430 OpStore %85 %83
3431 %86 = OpCompositeExtract %uint %82 1
3432 %88 = OpIAdd %uint %61 %uint_5
3433 %89 = OpAccessChain %_ptr_StorageBuffer_uint %57 %uint_1 %88
3434 OpStore %89 %86
3435 %91 = OpIAdd %uint %61 %uint_7
3436 %92 = OpAccessChain %_ptr_StorageBuffer_uint %57 %uint_1 %91
3437 OpStore %92 %51
3438 %94 = OpIAdd %uint %61 %uint_8
3439 %95 = OpAccessChain %_ptr_StorageBuffer_uint %57 %uint_1 %94
3440 OpStore %95 %52
3441 %97 = OpIAdd %uint %61 %uint_9
3442 %98 = OpAccessChain %_ptr_StorageBuffer_uint %57 %uint_1 %97
3443 OpStore %98 %53
3444 OpBranch %65
3445 %65 = OpLabel
3446 OpReturn
3447 OpFunctionEnd
3448 %104 = OpFunction %uint None %105
3449 %106 = OpFunctionParameter %uint
3450 %107 = OpFunctionParameter %uint
3451 %108 = OpFunctionParameter %uint
3452 %109 = OpFunctionParameter %uint
3453 %110 = OpLabel
3454 %111 = OpAccessChain %_ptr_StorageBuffer_uint %33 %uint_0 %106
3455 %112 = OpLoad %uint %111
3456 %113 = OpIAdd %uint %112 %107
3457 %114 = OpAccessChain %_ptr_StorageBuffer_uint %33 %uint_0 %113
3458 %115 = OpLoad %uint %114
3459 %116 = OpIAdd %uint %115 %108
3460 %117 = OpAccessChain %_ptr_StorageBuffer_uint %33 %uint_0 %116
3461 %118 = OpLoad %uint %117
3462 %119 = OpIAdd %uint %118 %109
3463 %120 = OpAccessChain %_ptr_StorageBuffer_uint %33 %uint_0 %119
3464 %121 = OpLoad %uint %120
3465 OpReturnValue %121
3466 OpFunctionEnd
3467 )";
3468
3469 // SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
3470 SinglePassRunAndCheck<InstBindlessCheckPass>(
3471 defs_before + func_before, defs_after + func_after + new_funcs, true,
3472 true, 7u, 23u, true, true);
3473 }
3474
3475 TEST_F(InstBindlessTest, InstBoundsAndInitLoadUnsizedSSBOArray) {
3476 // Same as Deprecated but declaring as StorageBuffer Block
3477
3478 const std::string defs_before =
3479 R"(OpCapability Shader
3480 OpCapability ShaderNonUniform
3481 OpCapability RuntimeDescriptorArray
3482 OpCapability StorageBufferArrayNonUniformIndexing
3483 OpExtension "SPV_EXT_descriptor_indexing"
3484 %1 = OpExtInstImport "GLSL.std.450"
3485 OpMemoryModel Logical GLSL450
3486 OpEntryPoint Fragment %main "main" %b %nu_ii
3487 OpExecutionMode %main OriginUpperLeft
3488 OpSource GLSL 450
3489 OpSourceExtension "GL_EXT_nonuniform_qualifier"
3490 OpName %main "main"
3491 OpName %b "b"
3492 OpName %bname "bname"
3493 OpMemberName %bname 0 "a"
3494 OpName %storageBuffer "storageBuffer"
3495 OpName %nu_ii "nu_ii"
3496 OpDecorate %b Location 0
3497 OpMemberDecorate %bname 0 Offset 0
3498 OpDecorate %bname Block
3499 OpDecorate %storageBuffer DescriptorSet 0
3500 OpDecorate %storageBuffer Binding 3
3501 OpDecorate %nu_ii Flat
3502 OpDecorate %nu_ii Location 0
3503 OpDecorate %nu_ii NonUniform
3504 OpDecorate %16 NonUniform
3505 OpDecorate %20 NonUniform
3506 %void = OpTypeVoid
3507 %3 = OpTypeFunction %void
3508 %float = OpTypeFloat 32
3509 %_ptr_Output_float = OpTypePointer Output %float
3510 %b = OpVariable %_ptr_Output_float Output
3511 %bname = OpTypeStruct %float
3512 %_runtimearr_bname = OpTypeRuntimeArray %bname
3513 %_ptr_StorageBuffer__runtimearr_bname = OpTypePointer StorageBuffer %_runtimearr_bname
3514 %storageBuffer = OpVariable %_ptr_StorageBuffer__runtimearr_bname StorageBuffer
3515 %int = OpTypeInt 32 1
3516 %_ptr_Input_int = OpTypePointer Input %int
3517 %nu_ii = OpVariable %_ptr_Input_int Input
3518 %int_0 = OpConstant %int 0
3519 %_ptr_StorageBuffer_float = OpTypePointer StorageBuffer %float
3520 )";
3521
3522 const std::string defs_after =
3523 R"(OpCapability Shader
3524 OpCapability ShaderNonUniform
3525 OpCapability RuntimeDescriptorArray
3526 OpCapability StorageBufferArrayNonUniformIndexing
3527 OpExtension "SPV_EXT_descriptor_indexing"
3528 OpExtension "SPV_KHR_storage_buffer_storage_class"
3529 %1 = OpExtInstImport "GLSL.std.450"
3530 OpMemoryModel Logical GLSL450
3531 OpEntryPoint Fragment %main "main" %b %nu_ii %gl_FragCoord
3532 OpExecutionMode %main OriginUpperLeft
3533 OpSource GLSL 450
3534 OpSourceExtension "GL_EXT_nonuniform_qualifier"
3535 OpName %main "main"
3536 OpName %b "b"
3537 OpName %bname "bname"
3538 OpMemberName %bname 0 "a"
3539 OpName %storageBuffer "storageBuffer"
3540 OpName %nu_ii "nu_ii"
3541 OpDecorate %b Location 0
3542 OpMemberDecorate %bname 0 Offset 0
3543 OpDecorate %bname Block
3544 OpDecorate %storageBuffer DescriptorSet 0
3545 OpDecorate %storageBuffer Binding 3
3546 OpDecorate %nu_ii Flat
3547 OpDecorate %nu_ii Location 0
3548 OpDecorate %nu_ii NonUniform
3549 OpDecorate %7 NonUniform
3550 OpDecorate %102 NonUniform
3551 OpDecorate %_runtimearr_uint ArrayStride 4
3552 OpDecorate %_struct_31 Block
3553 OpMemberDecorate %_struct_31 0 Offset 0
3554 OpDecorate %33 DescriptorSet 7
3555 OpDecorate %33 Binding 1
3556 OpDecorate %130 NonUniform
3557 OpDecorate %_struct_55 Block
3558 OpMemberDecorate %_struct_55 0 Offset 0
3559 OpMemberDecorate %_struct_55 1 Offset 4
3560 OpDecorate %57 DescriptorSet 7
3561 OpDecorate %57 Binding 0
3562 OpDecorate %gl_FragCoord BuiltIn FragCoord
3563 OpDecorate %127 NonUniform
3564 %void = OpTypeVoid
3565 %10 = OpTypeFunction %void
3566 %float = OpTypeFloat 32
3567 %_ptr_Output_float = OpTypePointer Output %float
3568 %b = OpVariable %_ptr_Output_float Output
3569 %bname = OpTypeStruct %float
3570 %_runtimearr_bname = OpTypeRuntimeArray %bname
3571 %_ptr_StorageBuffer__runtimearr_bname = OpTypePointer StorageBuffer %_runtimearr_bname
3572 %storageBuffer = OpVariable %_ptr_StorageBuffer__runtimearr_bname StorageBuffer
3573 %int = OpTypeInt 32 1
3574 %_ptr_Input_int = OpTypePointer Input %int
3575 %nu_ii = OpVariable %_ptr_Input_int Input
3576 %int_0 = OpConstant %int 0
3577 %_ptr_StorageBuffer_float = OpTypePointer StorageBuffer %float
3578 %uint = OpTypeInt 32 0
3579 %uint_0 = OpConstant %uint 0
3580 %uint_1 = OpConstant %uint 1
3581 %uint_3 = OpConstant %uint 3
3582 %26 = OpTypeFunction %uint %uint %uint
3583 %_runtimearr_uint = OpTypeRuntimeArray %uint
3584 %_struct_31 = OpTypeStruct %_runtimearr_uint
3585 %_ptr_StorageBuffer__struct_31 = OpTypePointer StorageBuffer %_struct_31
3586 %33 = OpVariable %_ptr_StorageBuffer__struct_31 StorageBuffer
3587 %_ptr_StorageBuffer_uint = OpTypePointer StorageBuffer %uint
3588 %bool = OpTypeBool
3589 %49 = OpTypeFunction %void %uint %uint %uint %uint
3590 %_struct_55 = OpTypeStruct %uint %_runtimearr_uint
3591 %_ptr_StorageBuffer__struct_55 = OpTypePointer StorageBuffer %_struct_55
3592 %57 = OpVariable %_ptr_StorageBuffer__struct_55 StorageBuffer
3593 %uint_10 = OpConstant %uint 10
3594 %uint_4 = OpConstant %uint 4
3595 %uint_23 = OpConstant %uint 23
3596 %uint_2 = OpConstant %uint 2
3597 %v4float = OpTypeVector %float 4
3598 %_ptr_Input_v4float = OpTypePointer Input %v4float
3599 %gl_FragCoord = OpVariable %_ptr_Input_v4float Input
3600 %v4uint = OpTypeVector %uint 4
3601 %uint_5 = OpConstant %uint 5
3602 %uint_7 = OpConstant %uint 7
3603 %uint_8 = OpConstant %uint 8
3604 %uint_9 = OpConstant %uint 9
3605 %uint_45 = OpConstant %uint 45
3606 %101 = OpConstantNull %float
3607 %105 = OpTypeFunction %uint %uint %uint %uint %uint
3608 )";
3609
3610 const std::string func_before =
3611 R"(%main = OpFunction %void None %3
3612 %5 = OpLabel
3613 %16 = OpLoad %int %nu_ii
3614 %19 = OpAccessChain %_ptr_StorageBuffer_float %storageBuffer %16 %int_0
3615 %20 = OpLoad %float %19
3616 OpStore %b %20
3617 OpReturn
3618 OpFunctionEnd
3619 )";
3620
3621 const std::string func_after =
3622 R"(%main = OpFunction %void None %10
3623 %19 = OpLabel
3624 %7 = OpLoad %int %nu_ii
3625 %20 = OpAccessChain %_ptr_StorageBuffer_float %storageBuffer %7 %int_0
3626 %40 = OpFunctionCall %uint %25 %uint_1 %uint_3
3627 %42 = OpULessThan %bool %7 %40
3628 OpSelectionMerge %43 None
3629 OpBranchConditional %42 %44 %45
3630 %44 = OpLabel
3631 %103 = OpBitcast %uint %7
3632 %122 = OpFunctionCall %uint %104 %uint_0 %uint_0 %uint_3 %103
3633 %123 = OpULessThan %bool %uint_0 %122
3634 OpSelectionMerge %124 None
3635 OpBranchConditional %123 %125 %126
3636 %125 = OpLabel
3637 %127 = OpLoad %float %20
3638 OpBranch %124
3639 %126 = OpLabel
3640 %128 = OpBitcast %uint %7
3641 %129 = OpFunctionCall %void %48 %uint_45 %uint_1 %128 %uint_0
3642 OpBranch %124
3643 %124 = OpLabel
3644 %130 = OpPhi %float %127 %125 %101 %126
3645 OpBranch %43
3646 %45 = OpLabel
3647 %47 = OpBitcast %uint %7
3648 %100 = OpFunctionCall %void %48 %uint_45 %uint_0 %47 %40
3649 OpBranch %43
3650 %43 = OpLabel
3651 %102 = OpPhi %float %130 %124 %101 %45
3652 OpStore %b %102
3653 OpReturn
3654 OpFunctionEnd
3655 )";
3656
3657 const std::string new_funcs =
3658 R"(%25 = OpFunction %uint None %26
3659 %27 = OpFunctionParameter %uint
3660 %28 = OpFunctionParameter %uint
3661 %29 = OpLabel
3662 %35 = OpAccessChain %_ptr_StorageBuffer_uint %33 %uint_0 %27
3663 %36 = OpLoad %uint %35
3664 %37 = OpIAdd %uint %36 %28
3665 %38 = OpAccessChain %_ptr_StorageBuffer_uint %33 %uint_0 %37
3666 %39 = OpLoad %uint %38
3667 OpReturnValue %39
3668 OpFunctionEnd
3669 %48 = OpFunction %void None %49
3670 %50 = OpFunctionParameter %uint
3671 %51 = OpFunctionParameter %uint
3672 %52 = OpFunctionParameter %uint
3673 %53 = OpFunctionParameter %uint
3674 %54 = OpLabel
3675 %58 = OpAccessChain %_ptr_StorageBuffer_uint %57 %uint_0
3676 %61 = OpAtomicIAdd %uint %58 %uint_4 %uint_0 %uint_10
3677 %62 = OpIAdd %uint %61 %uint_10
3678 %63 = OpArrayLength %uint %57 1
3679 %64 = OpULessThanEqual %bool %62 %63
3680 OpSelectionMerge %65 None
3681 OpBranchConditional %64 %66 %65
3682 %66 = OpLabel
3683 %67 = OpIAdd %uint %61 %uint_0
3684 %68 = OpAccessChain %_ptr_StorageBuffer_uint %57 %uint_1 %67
3685 OpStore %68 %uint_10
3686 %70 = OpIAdd %uint %61 %uint_1
3687 %71 = OpAccessChain %_ptr_StorageBuffer_uint %57 %uint_1 %70
3688 OpStore %71 %uint_23
3689 %73 = OpIAdd %uint %61 %uint_2
3690 %74 = OpAccessChain %_ptr_StorageBuffer_uint %57 %uint_1 %73
3691 OpStore %74 %50
3692 %75 = OpIAdd %uint %61 %uint_3
3693 %76 = OpAccessChain %_ptr_StorageBuffer_uint %57 %uint_1 %75
3694 OpStore %76 %uint_4
3695 %80 = OpLoad %v4float %gl_FragCoord
3696 %82 = OpBitcast %v4uint %80
3697 %83 = OpCompositeExtract %uint %82 0
3698 %84 = OpIAdd %uint %61 %uint_4
3699 %85 = OpAccessChain %_ptr_StorageBuffer_uint %57 %uint_1 %84
3700 OpStore %85 %83
3701 %86 = OpCompositeExtract %uint %82 1
3702 %88 = OpIAdd %uint %61 %uint_5
3703 %89 = OpAccessChain %_ptr_StorageBuffer_uint %57 %uint_1 %88
3704 OpStore %89 %86
3705 %91 = OpIAdd %uint %61 %uint_7
3706 %92 = OpAccessChain %_ptr_StorageBuffer_uint %57 %uint_1 %91
3707 OpStore %92 %51
3708 %94 = OpIAdd %uint %61 %uint_8
3709 %95 = OpAccessChain %_ptr_StorageBuffer_uint %57 %uint_1 %94
3710 OpStore %95 %52
3711 %97 = OpIAdd %uint %61 %uint_9
3712 %98 = OpAccessChain %_ptr_StorageBuffer_uint %57 %uint_1 %97
3713 OpStore %98 %53
3714 OpBranch %65
3715 %65 = OpLabel
3716 OpReturn
3717 OpFunctionEnd
3718 %104 = OpFunction %uint None %105
3719 %106 = OpFunctionParameter %uint
3720 %107 = OpFunctionParameter %uint
3721 %108 = OpFunctionParameter %uint
3722 %109 = OpFunctionParameter %uint
3723 %110 = OpLabel
3724 %111 = OpAccessChain %_ptr_StorageBuffer_uint %33 %uint_0 %106
3725 %112 = OpLoad %uint %111
3726 %113 = OpIAdd %uint %112 %107
3727 %114 = OpAccessChain %_ptr_StorageBuffer_uint %33 %uint_0 %113
3728 %115 = OpLoad %uint %114
3729 %116 = OpIAdd %uint %115 %108
3730 %117 = OpAccessChain %_ptr_StorageBuffer_uint %33 %uint_0 %116
3731 %118 = OpLoad %uint %117
3732 %119 = OpIAdd %uint %118 %109
3733 %120 = OpAccessChain %_ptr_StorageBuffer_uint %33 %uint_0 %119
3734 %121 = OpLoad %uint %120
3735 OpReturnValue %121
3736 OpFunctionEnd
3737 )";
3738
3739 // SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
3740 SinglePassRunAndCheck<InstBindlessCheckPass>(
3741 defs_before + func_before, defs_after + func_after + new_funcs, true,
3742 true, 7u, 23u, true, true);
3743 }
3744
3745 TEST_F(InstBindlessTest, InstInitLoadUBOScalar) {
3746 // #version 450
3747 // #extension GL_EXT_nonuniform_qualifier : enable
3748 //
3749 // layout(location=0) out float b;
3750 // layout(binding=3) uniform uname { float a; } uniformBuffer;
3751 //
3752 // void main()
3753 // {
3754 // b = uniformBuffer.a;
3755 // }
3756
3757 const std::string defs_before =
3758 R"(OpCapability Shader
3759 OpExtension "SPV_EXT_descriptor_indexing"
3760 %1 = OpExtInstImport "GLSL.std.450"
3761 OpMemoryModel Logical GLSL450
3762 OpEntryPoint Fragment %main "main" %b
3763 OpExecutionMode %main OriginUpperLeft
3764 OpSource GLSL 450
3765 OpSourceExtension "GL_EXT_nonuniform_qualifier"
3766 OpName %main "main"
3767 OpName %b "b"
3768 OpName %uname "uname"
3769 OpMemberName %uname 0 "a"
3770 OpName %uniformBuffer "uniformBuffer"
3771 OpDecorate %b Location 0
3772 OpMemberDecorate %uname 0 Offset 0
3773 OpDecorate %uname Block
3774 OpDecorate %uniformBuffer DescriptorSet 0
3775 OpDecorate %uniformBuffer Binding 3
3776 %void = OpTypeVoid
3777 %3 = OpTypeFunction %void
3778 %float = OpTypeFloat 32
3779 %_ptr_Output_float = OpTypePointer Output %float
3780 %b = OpVariable %_ptr_Output_float Output
3781 %uname = OpTypeStruct %float
3782 %_ptr_Uniform_uname = OpTypePointer Uniform %uname
3783 %uniformBuffer = OpVariable %_ptr_Uniform_uname Uniform
3784 %int = OpTypeInt 32 1
3785 %int_0 = OpConstant %int 0
3786 %_ptr_Uniform_float = OpTypePointer Uniform %float
3787 )";
3788
3789 const std::string defs_after =
3790 R"(OpCapability Shader
3791 OpExtension "SPV_EXT_descriptor_indexing"
3792 OpExtension "SPV_KHR_storage_buffer_storage_class"
3793 %1 = OpExtInstImport "GLSL.std.450"
3794 OpMemoryModel Logical GLSL450
3795 OpEntryPoint Fragment %main "main" %b %gl_FragCoord
3796 OpExecutionMode %main OriginUpperLeft
3797 OpSource GLSL 450
3798 OpSourceExtension "GL_EXT_nonuniform_qualifier"
3799 OpName %main "main"
3800 OpName %b "b"
3801 OpName %uname "uname"
3802 OpMemberName %uname 0 "a"
3803 OpName %uniformBuffer "uniformBuffer"
3804 OpDecorate %b Location 0
3805 OpMemberDecorate %uname 0 Offset 0
3806 OpDecorate %uname Block
3807 OpDecorate %uniformBuffer DescriptorSet 0
3808 OpDecorate %uniformBuffer Binding 3
3809 OpDecorate %_runtimearr_uint ArrayStride 4
3810 OpDecorate %_struct_28 Block
3811 OpMemberDecorate %_struct_28 0 Offset 0
3812 OpDecorate %30 DescriptorSet 7
3813 OpDecorate %30 Binding 1
3814 OpDecorate %_struct_58 Block
3815 OpMemberDecorate %_struct_58 0 Offset 0
3816 OpMemberDecorate %_struct_58 1 Offset 4
3817 OpDecorate %60 DescriptorSet 7
3818 OpDecorate %60 Binding 0
3819 OpDecorate %gl_FragCoord BuiltIn FragCoord
3820 %void = OpTypeVoid
3821 %7 = OpTypeFunction %void
3822 %float = OpTypeFloat 32
3823 %_ptr_Output_float = OpTypePointer Output %float
3824 %b = OpVariable %_ptr_Output_float Output
3825 %uname = OpTypeStruct %float
3826 %_ptr_Uniform_uname = OpTypePointer Uniform %uname
3827 %uniformBuffer = OpVariable %_ptr_Uniform_uname Uniform
3828 %int = OpTypeInt 32 1
3829 %int_0 = OpConstant %int 0
3830 %_ptr_Uniform_float = OpTypePointer Uniform %float
3831 %uint = OpTypeInt 32 0
3832 %uint_0 = OpConstant %uint 0
3833 %uint_3 = OpConstant %uint 3
3834 %21 = OpTypeFunction %uint %uint %uint %uint %uint
3835 %_runtimearr_uint = OpTypeRuntimeArray %uint
3836 %_struct_28 = OpTypeStruct %_runtimearr_uint
3837 %_ptr_StorageBuffer__struct_28 = OpTypePointer StorageBuffer %_struct_28
3838 %30 = OpVariable %_ptr_StorageBuffer__struct_28 StorageBuffer
3839 %_ptr_StorageBuffer_uint = OpTypePointer StorageBuffer %uint
3840 %bool = OpTypeBool
3841 %uint_1 = OpConstant %uint 1
3842 %52 = OpTypeFunction %void %uint %uint %uint %uint
3843 %_struct_58 = OpTypeStruct %uint %_runtimearr_uint
3844 %_ptr_StorageBuffer__struct_58 = OpTypePointer StorageBuffer %_struct_58
3845 %60 = OpVariable %_ptr_StorageBuffer__struct_58 StorageBuffer
3846 %uint_10 = OpConstant %uint 10
3847 %uint_4 = OpConstant %uint 4
3848 %uint_23 = OpConstant %uint 23
3849 %uint_2 = OpConstant %uint 2
3850 %v4float = OpTypeVector %float 4
3851 %_ptr_Input_v4float = OpTypePointer Input %v4float
3852 %gl_FragCoord = OpVariable %_ptr_Input_v4float Input
3853 %v4uint = OpTypeVector %uint 4
3854 %uint_5 = OpConstant %uint 5
3855 %uint_7 = OpConstant %uint 7
3856 %uint_8 = OpConstant %uint 8
3857 %uint_9 = OpConstant %uint 9
3858 %uint_32 = OpConstant %uint 32
3859 %104 = OpConstantNull %float
3860 )";
3861
3862 const std::string func_before =
3863 R"(%main = OpFunction %void None %3
3864 %5 = OpLabel
3865 %15 = OpAccessChain %_ptr_Uniform_float %uniformBuffer %int_0
3866 %16 = OpLoad %float %15
3867 OpStore %b %16
3868 OpReturn
3869 OpFunctionEnd
3870 )";
3871
3872 const std::string func_after =
3873 R"(%main = OpFunction %void None %7
3874 %14 = OpLabel
3875 %15 = OpAccessChain %_ptr_Uniform_float %uniformBuffer %int_0
3876 %43 = OpFunctionCall %uint %20 %uint_0 %uint_0 %uint_3 %uint_0
3877 %45 = OpULessThan %bool %uint_0 %43
3878 OpSelectionMerge %47 None
3879 OpBranchConditional %45 %48 %49
3880 %48 = OpLabel
3881 %50 = OpLoad %float %15
3882 OpBranch %47
3883 %49 = OpLabel
3884 %103 = OpFunctionCall %void %51 %uint_32 %uint_1 %uint_0 %uint_0
3885 OpBranch %47
3886 %47 = OpLabel
3887 %105 = OpPhi %float %50 %48 %104 %49
3888 OpStore %b %105
3889 OpReturn
3890 OpFunctionEnd
3891 )";
3892
3893 const std::string new_funcs =
3894 R"(%20 = OpFunction %uint None %21
3895 %22 = OpFunctionParameter %uint
3896 %23 = OpFunctionParameter %uint
3897 %24 = OpFunctionParameter %uint
3898 %25 = OpFunctionParameter %uint
3899 %26 = OpLabel
3900 %32 = OpAccessChain %_ptr_StorageBuffer_uint %30 %uint_0 %22
3901 %33 = OpLoad %uint %32
3902 %34 = OpIAdd %uint %33 %23
3903 %35 = OpAccessChain %_ptr_StorageBuffer_uint %30 %uint_0 %34
3904 %36 = OpLoad %uint %35
3905 %37 = OpIAdd %uint %36 %24
3906 %38 = OpAccessChain %_ptr_StorageBuffer_uint %30 %uint_0 %37
3907 %39 = OpLoad %uint %38
3908 %40 = OpIAdd %uint %39 %25
3909 %41 = OpAccessChain %_ptr_StorageBuffer_uint %30 %uint_0 %40
3910 %42 = OpLoad %uint %41
3911 OpReturnValue %42
3912 OpFunctionEnd
3913 %51 = OpFunction %void None %52
3914 %53 = OpFunctionParameter %uint
3915 %54 = OpFunctionParameter %uint
3916 %55 = OpFunctionParameter %uint
3917 %56 = OpFunctionParameter %uint
3918 %57 = OpLabel
3919 %61 = OpAccessChain %_ptr_StorageBuffer_uint %60 %uint_0
3920 %64 = OpAtomicIAdd %uint %61 %uint_4 %uint_0 %uint_10
3921 %65 = OpIAdd %uint %64 %uint_10
3922 %66 = OpArrayLength %uint %60 1
3923 %67 = OpULessThanEqual %bool %65 %66
3924 OpSelectionMerge %68 None
3925 OpBranchConditional %67 %69 %68
3926 %69 = OpLabel
3927 %70 = OpIAdd %uint %64 %uint_0
3928 %71 = OpAccessChain %_ptr_StorageBuffer_uint %60 %uint_1 %70
3929 OpStore %71 %uint_10
3930 %73 = OpIAdd %uint %64 %uint_1
3931 %74 = OpAccessChain %_ptr_StorageBuffer_uint %60 %uint_1 %73
3932 OpStore %74 %uint_23
3933 %76 = OpIAdd %uint %64 %uint_2
3934 %77 = OpAccessChain %_ptr_StorageBuffer_uint %60 %uint_1 %76
3935 OpStore %77 %53
3936 %78 = OpIAdd %uint %64 %uint_3
3937 %79 = OpAccessChain %_ptr_StorageBuffer_uint %60 %uint_1 %78
3938 OpStore %79 %uint_4
3939 %83 = OpLoad %v4float %gl_FragCoord
3940 %85 = OpBitcast %v4uint %83
3941 %86 = OpCompositeExtract %uint %85 0
3942 %87 = OpIAdd %uint %64 %uint_4
3943 %88 = OpAccessChain %_ptr_StorageBuffer_uint %60 %uint_1 %87
3944 OpStore %88 %86
3945 %89 = OpCompositeExtract %uint %85 1
3946 %91 = OpIAdd %uint %64 %uint_5
3947 %92 = OpAccessChain %_ptr_StorageBuffer_uint %60 %uint_1 %91
3948 OpStore %92 %89
3949 %94 = OpIAdd %uint %64 %uint_7
3950 %95 = OpAccessChain %_ptr_StorageBuffer_uint %60 %uint_1 %94
3951 OpStore %95 %54
3952 %97 = OpIAdd %uint %64 %uint_8
3953 %98 = OpAccessChain %_ptr_StorageBuffer_uint %60 %uint_1 %97
3954 OpStore %98 %55
3955 %100 = OpIAdd %uint %64 %uint_9
3956 %101 = OpAccessChain %_ptr_StorageBuffer_uint %60 %uint_1 %100
3957 OpStore %101 %56
3958 OpBranch %68
3959 %68 = OpLabel
3960 OpReturn
3961 OpFunctionEnd
3962 )";
3963
3964 // SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
3965 SinglePassRunAndCheck<InstBindlessCheckPass>(
3966 defs_before + func_before, defs_after + func_after + new_funcs, true,
3967 true, 7u, 23u, true, true);
3968 }
3969
3970 TEST_F(InstBindlessTest, InstBoundsInitStoreUnsizedSSBOArray) {
3971 // #version 450
3972 // #extension GL_EXT_nonuniform_qualifier : enable
3973 //
3974 // layout(location=0) in nonuniformEXT flat int nu_ii;
3975 // layout(location=1) in float b;
3976 //
3977 // layout(binding=4) buffer bname { float b; } storageBuffer[];
3978 //
3979 // void main()
3980 // {
3981 // storageBuffer[nu_ii].b = b;
3982 // }
3983
3984 const std::string defs_before =
3985 R"(OpCapability Shader
3986 OpCapability ShaderNonUniform
3987 OpCapability RuntimeDescriptorArray
3988 OpCapability StorageBufferArrayNonUniformIndexing
3989 OpExtension "SPV_EXT_descriptor_indexing"
3990 %1 = OpExtInstImport "GLSL.std.450"
3991 OpMemoryModel Logical GLSL450
3992 OpEntryPoint Fragment %main "main" %nu_ii %b
3993 OpExecutionMode %main OriginUpperLeft
3994 OpSource GLSL 450
3995 OpSourceExtension "GL_EXT_nonuniform_qualifier"
3996 OpName %main "main"
3997 OpName %bname "bname"
3998 OpMemberName %bname 0 "b"
3999 OpName %storageBuffer "storageBuffer"
4000 OpName %nu_ii "nu_ii"
4001 OpName %b "b"
4002 OpMemberDecorate %bname 0 Offset 0
4003 OpDecorate %bname BufferBlock
4004 OpDecorate %storageBuffer DescriptorSet 0
4005 OpDecorate %storageBuffer Binding 4
4006 OpDecorate %nu_ii Flat
4007 OpDecorate %nu_ii Location 0
4008 OpDecorate %nu_ii NonUniform
4009 OpDecorate %14 NonUniform
4010 OpDecorate %b Location 1
4011 %void = OpTypeVoid
4012 %3 = OpTypeFunction %void
4013 %float = OpTypeFloat 32
4014 %bname = OpTypeStruct %float
4015 %_runtimearr_bname = OpTypeRuntimeArray %bname
4016 %_ptr_Uniform__runtimearr_bname = OpTypePointer Uniform %_runtimearr_bname
4017 %storageBuffer = OpVariable %_ptr_Uniform__runtimearr_bname Uniform
4018 %int = OpTypeInt 32 1
4019 %_ptr_Input_int = OpTypePointer Input %int
4020 %nu_ii = OpVariable %_ptr_Input_int Input
4021 %int_0 = OpConstant %int 0
4022 %_ptr_Input_float = OpTypePointer Input %float
4023 %b = OpVariable %_ptr_Input_float Input
4024 %_ptr_Uniform_float = OpTypePointer Uniform %float
4025 )";
4026
4027 const std::string defs_after =
4028 R"(OpCapability Shader
4029 OpCapability ShaderNonUniform
4030 OpCapability RuntimeDescriptorArray
4031 OpCapability StorageBufferArrayNonUniformIndexing
4032 OpExtension "SPV_EXT_descriptor_indexing"
4033 OpExtension "SPV_KHR_storage_buffer_storage_class"
4034 %1 = OpExtInstImport "GLSL.std.450"
4035 OpMemoryModel Logical GLSL450
4036 OpEntryPoint Fragment %main "main" %nu_ii %b %gl_FragCoord
4037 OpExecutionMode %main OriginUpperLeft
4038 OpSource GLSL 450
4039 OpSourceExtension "GL_EXT_nonuniform_qualifier"
4040 OpName %main "main"
4041 OpName %bname "bname"
4042 OpMemberName %bname 0 "b"
4043 OpName %storageBuffer "storageBuffer"
4044 OpName %nu_ii "nu_ii"
4045 OpName %b "b"
4046 OpMemberDecorate %bname 0 Offset 0
4047 OpDecorate %bname BufferBlock
4048 OpDecorate %storageBuffer DescriptorSet 0
4049 OpDecorate %storageBuffer Binding 4
4050 OpDecorate %nu_ii Flat
4051 OpDecorate %nu_ii Location 0
4052 OpDecorate %nu_ii NonUniform
4053 OpDecorate %7 NonUniform
4054 OpDecorate %b Location 1
4055 OpDecorate %_runtimearr_uint ArrayStride 4
4056 OpDecorate %_struct_31 Block
4057 OpMemberDecorate %_struct_31 0 Offset 0
4058 OpDecorate %33 DescriptorSet 7
4059 OpDecorate %33 Binding 1
4060 OpDecorate %_struct_54 Block
4061 OpMemberDecorate %_struct_54 0 Offset 0
4062 OpMemberDecorate %_struct_54 1 Offset 4
4063 OpDecorate %56 DescriptorSet 7
4064 OpDecorate %56 Binding 0
4065 OpDecorate %gl_FragCoord BuiltIn FragCoord
4066 %void = OpTypeVoid
4067 %9 = OpTypeFunction %void
4068 %float = OpTypeFloat 32
4069 %bname = OpTypeStruct %float
4070 %_runtimearr_bname = OpTypeRuntimeArray %bname
4071 %_ptr_Uniform__runtimearr_bname = OpTypePointer Uniform %_runtimearr_bname
4072 %storageBuffer = OpVariable %_ptr_Uniform__runtimearr_bname Uniform
4073 %int = OpTypeInt 32 1
4074 %_ptr_Input_int = OpTypePointer Input %int
4075 %nu_ii = OpVariable %_ptr_Input_int Input
4076 %int_0 = OpConstant %int 0
4077 %_ptr_Input_float = OpTypePointer Input %float
4078 %b = OpVariable %_ptr_Input_float Input
4079 %_ptr_Uniform_float = OpTypePointer Uniform %float
4080 %uint = OpTypeInt 32 0
4081 %uint_0 = OpConstant %uint 0
4082 %uint_1 = OpConstant %uint 1
4083 %uint_4 = OpConstant %uint 4
4084 %26 = OpTypeFunction %uint %uint %uint
4085 %_runtimearr_uint = OpTypeRuntimeArray %uint
4086 %_struct_31 = OpTypeStruct %_runtimearr_uint
4087 %_ptr_StorageBuffer__struct_31 = OpTypePointer StorageBuffer %_struct_31
4088 %33 = OpVariable %_ptr_StorageBuffer__struct_31 StorageBuffer
4089 %_ptr_StorageBuffer_uint = OpTypePointer StorageBuffer %uint
4090 %bool = OpTypeBool
4091 %48 = OpTypeFunction %void %uint %uint %uint %uint
4092 %_struct_54 = OpTypeStruct %uint %_runtimearr_uint
4093 %_ptr_StorageBuffer__struct_54 = OpTypePointer StorageBuffer %_struct_54
4094 %56 = OpVariable %_ptr_StorageBuffer__struct_54 StorageBuffer
4095 %uint_10 = OpConstant %uint 10
4096 %uint_23 = OpConstant %uint 23
4097 %uint_2 = OpConstant %uint 2
4098 %uint_3 = OpConstant %uint 3
4099 %v4float = OpTypeVector %float 4
4100 %_ptr_Input_v4float = OpTypePointer Input %v4float
4101 %gl_FragCoord = OpVariable %_ptr_Input_v4float Input
4102 %v4uint = OpTypeVector %uint 4
4103 %uint_5 = OpConstant %uint 5
4104 %uint_7 = OpConstant %uint 7
4105 %uint_8 = OpConstant %uint 8
4106 %uint_9 = OpConstant %uint 9
4107 %uint_45 = OpConstant %uint 45
4108 %102 = OpTypeFunction %uint %uint %uint %uint %uint
4109 )";
4110
4111 const std::string func_before =
4112 R"(%main = OpFunction %void None %3
4113 %5 = OpLabel
4114 %14 = OpLoad %int %nu_ii
4115 %18 = OpLoad %float %b
4116 %20 = OpAccessChain %_ptr_Uniform_float %storageBuffer %14 %int_0
4117 OpStore %20 %18
4118 OpReturn
4119 OpFunctionEnd
4120 )";
4121
4122 const std::string func_after =
4123 R"(%main = OpFunction %void None %9
4124 %18 = OpLabel
4125 %7 = OpLoad %int %nu_ii
4126 %19 = OpLoad %float %b
4127 %20 = OpAccessChain %_ptr_Uniform_float %storageBuffer %7 %int_0
4128 %40 = OpFunctionCall %uint %25 %uint_1 %uint_4
4129 %42 = OpULessThan %bool %7 %40
4130 OpSelectionMerge %43 None
4131 OpBranchConditional %42 %44 %45
4132 %44 = OpLabel
4133 %100 = OpBitcast %uint %7
4134 %119 = OpFunctionCall %uint %101 %uint_0 %uint_0 %uint_4 %100
4135 %120 = OpULessThan %bool %uint_0 %119
4136 OpSelectionMerge %121 None
4137 OpBranchConditional %120 %122 %123
4138 %122 = OpLabel
4139 OpStore %20 %19
4140 OpBranch %121
4141 %123 = OpLabel
4142 %124 = OpBitcast %uint %7
4143 %125 = OpFunctionCall %void %47 %uint_45 %uint_1 %124 %uint_0
4144 OpBranch %121
4145 %121 = OpLabel
4146 OpBranch %43
4147 %45 = OpLabel
4148 %46 = OpBitcast %uint %7
4149 %99 = OpFunctionCall %void %47 %uint_45 %uint_0 %46 %40
4150 OpBranch %43
4151 %43 = OpLabel
4152 OpReturn
4153 OpFunctionEnd
4154 )";
4155
4156 const std::string new_funcs =
4157 R"(%25 = OpFunction %uint None %26
4158 %27 = OpFunctionParameter %uint
4159 %28 = OpFunctionParameter %uint
4160 %29 = OpLabel
4161 %35 = OpAccessChain %_ptr_StorageBuffer_uint %33 %uint_0 %27
4162 %36 = OpLoad %uint %35
4163 %37 = OpIAdd %uint %36 %28
4164 %38 = OpAccessChain %_ptr_StorageBuffer_uint %33 %uint_0 %37
4165 %39 = OpLoad %uint %38
4166 OpReturnValue %39
4167 OpFunctionEnd
4168 %47 = OpFunction %void None %48
4169 %49 = OpFunctionParameter %uint
4170 %50 = OpFunctionParameter %uint
4171 %51 = OpFunctionParameter %uint
4172 %52 = OpFunctionParameter %uint
4173 %53 = OpLabel
4174 %57 = OpAccessChain %_ptr_StorageBuffer_uint %56 %uint_0
4175 %59 = OpAtomicIAdd %uint %57 %uint_4 %uint_0 %uint_10
4176 %60 = OpIAdd %uint %59 %uint_10
4177 %61 = OpArrayLength %uint %56 1
4178 %62 = OpULessThanEqual %bool %60 %61
4179 OpSelectionMerge %63 None
4180 OpBranchConditional %62 %64 %63
4181 %64 = OpLabel
4182 %65 = OpIAdd %uint %59 %uint_0
4183 %66 = OpAccessChain %_ptr_StorageBuffer_uint %56 %uint_1 %65
4184 OpStore %66 %uint_10
4185 %68 = OpIAdd %uint %59 %uint_1
4186 %69 = OpAccessChain %_ptr_StorageBuffer_uint %56 %uint_1 %68
4187 OpStore %69 %uint_23
4188 %71 = OpIAdd %uint %59 %uint_2
4189 %72 = OpAccessChain %_ptr_StorageBuffer_uint %56 %uint_1 %71
4190 OpStore %72 %49
4191 %74 = OpIAdd %uint %59 %uint_3
4192 %75 = OpAccessChain %_ptr_StorageBuffer_uint %56 %uint_1 %74
4193 OpStore %75 %uint_4
4194 %79 = OpLoad %v4float %gl_FragCoord
4195 %81 = OpBitcast %v4uint %79
4196 %82 = OpCompositeExtract %uint %81 0
4197 %83 = OpIAdd %uint %59 %uint_4
4198 %84 = OpAccessChain %_ptr_StorageBuffer_uint %56 %uint_1 %83
4199 OpStore %84 %82
4200 %85 = OpCompositeExtract %uint %81 1
4201 %87 = OpIAdd %uint %59 %uint_5
4202 %88 = OpAccessChain %_ptr_StorageBuffer_uint %56 %uint_1 %87
4203 OpStore %88 %85
4204 %90 = OpIAdd %uint %59 %uint_7
4205 %91 = OpAccessChain %_ptr_StorageBuffer_uint %56 %uint_1 %90
4206 OpStore %91 %50
4207 %93 = OpIAdd %uint %59 %uint_8
4208 %94 = OpAccessChain %_ptr_StorageBuffer_uint %56 %uint_1 %93
4209 OpStore %94 %51
4210 %96 = OpIAdd %uint %59 %uint_9
4211 %97 = OpAccessChain %_ptr_StorageBuffer_uint %56 %uint_1 %96
4212 OpStore %97 %52
4213 OpBranch %63
4214 %63 = OpLabel
4215 OpReturn
4216 OpFunctionEnd
4217 %101 = OpFunction %uint None %102
4218 %103 = OpFunctionParameter %uint
4219 %104 = OpFunctionParameter %uint
4220 %105 = OpFunctionParameter %uint
4221 %106 = OpFunctionParameter %uint
4222 %107 = OpLabel
4223 %108 = OpAccessChain %_ptr_StorageBuffer_uint %33 %uint_0 %103
4224 %109 = OpLoad %uint %108
4225 %110 = OpIAdd %uint %109 %104
4226 %111 = OpAccessChain %_ptr_StorageBuffer_uint %33 %uint_0 %110
4227 %112 = OpLoad %uint %111
4228 %113 = OpIAdd %uint %112 %105
4229 %114 = OpAccessChain %_ptr_StorageBuffer_uint %33 %uint_0 %113
4230 %115 = OpLoad %uint %114
4231 %116 = OpIAdd %uint %115 %106
4232 %117 = OpAccessChain %_ptr_StorageBuffer_uint %33 %uint_0 %116
4233 %118 = OpLoad %uint %117
4234 OpReturnValue %118
4235 OpFunctionEnd
4236 )";
4237
4238 // SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
4239 SinglePassRunAndCheck<InstBindlessCheckPass>(
4240 defs_before + func_before, defs_after + func_after + new_funcs, true,
4241 true, 7u, 23u, true, true);
4242 }
4243
4244 TEST_F(InstBindlessTest, InstBoundsInitLoadSizedUBOArray) {
4245 // #version 450
4246 // #extension GL_EXT_nonuniform_qualifier : enable
4247 //
4248 // layout(location=0) in nonuniformEXT flat int nu_ii;
4249 // layout(location=0) out float b;
4250 //
4251 // layout(binding=3) uniform uname { float a; } uniformBuffer[128];
4252 //
4253 // void main()
4254 // {
4255 // b = uniformBuffer[nu_ii].a;
4256 // }
4257
4258 const std::string defs_before =
4259 R"(OpCapability Shader
4260 OpCapability ShaderNonUniform
4261 OpCapability UniformBufferArrayNonUniformIndexing
4262 OpExtension "SPV_EXT_descriptor_indexing"
4263 %1 = OpExtInstImport "GLSL.std.450"
4264 OpMemoryModel Logical GLSL450
4265 OpEntryPoint Fragment %main "main" %b %nu_ii
4266 OpExecutionMode %main OriginUpperLeft
4267 OpSource GLSL 450
4268 OpSourceExtension "GL_EXT_nonuniform_qualifier"
4269 OpName %main "main"
4270 OpName %b "b"
4271 OpName %uname "uname"
4272 OpMemberName %uname 0 "a"
4273 OpName %uniformBuffer "uniformBuffer"
4274 OpName %nu_ii "nu_ii"
4275 OpDecorate %b Location 0
4276 OpMemberDecorate %uname 0 Offset 0
4277 OpDecorate %uname Block
4278 OpDecorate %uniformBuffer DescriptorSet 0
4279 OpDecorate %uniformBuffer Binding 3
4280 OpDecorate %nu_ii Flat
4281 OpDecorate %nu_ii Location 0
4282 OpDecorate %nu_ii NonUniform
4283 OpDecorate %18 NonUniform
4284 OpDecorate %22 NonUniform
4285 %void = OpTypeVoid
4286 %3 = OpTypeFunction %void
4287 %float = OpTypeFloat 32
4288 %_ptr_Output_float = OpTypePointer Output %float
4289 %b = OpVariable %_ptr_Output_float Output
4290 %uname = OpTypeStruct %float
4291 %uint = OpTypeInt 32 0
4292 %uint_128 = OpConstant %uint 128
4293 %_arr_uname_uint_128 = OpTypeArray %uname %uint_128
4294 %_ptr_Uniform__arr_uname_uint_128 = OpTypePointer Uniform %_arr_uname_uint_128
4295 %uniformBuffer = OpVariable %_ptr_Uniform__arr_uname_uint_128 Uniform
4296 %int = OpTypeInt 32 1
4297 %_ptr_Input_int = OpTypePointer Input %int
4298 %nu_ii = OpVariable %_ptr_Input_int Input
4299 %int_0 = OpConstant %int 0
4300 %_ptr_Uniform_float = OpTypePointer Uniform %float
4301 )";
4302
4303 const std::string defs_after =
4304 R"(OpCapability Shader
4305 OpCapability ShaderNonUniform
4306 OpCapability UniformBufferArrayNonUniformIndexing
4307 OpExtension "SPV_EXT_descriptor_indexing"
4308 OpExtension "SPV_KHR_storage_buffer_storage_class"
4309 %1 = OpExtInstImport "GLSL.std.450"
4310 OpMemoryModel Logical GLSL450
4311 OpEntryPoint Fragment %main "main" %b %nu_ii %gl_FragCoord
4312 OpExecutionMode %main OriginUpperLeft
4313 OpSource GLSL 450
4314 OpSourceExtension "GL_EXT_nonuniform_qualifier"
4315 OpName %main "main"
4316 OpName %b "b"
4317 OpName %uname "uname"
4318 OpMemberName %uname 0 "a"
4319 OpName %uniformBuffer "uniformBuffer"
4320 OpName %nu_ii "nu_ii"
4321 OpDecorate %b Location 0
4322 OpMemberDecorate %uname 0 Offset 0
4323 OpDecorate %uname Block
4324 OpDecorate %uniformBuffer DescriptorSet 0
4325 OpDecorate %uniformBuffer Binding 3
4326 OpDecorate %nu_ii Flat
4327 OpDecorate %nu_ii Location 0
4328 OpDecorate %nu_ii NonUniform
4329 OpDecorate %7 NonUniform
4330 OpDecorate %89 NonUniform
4331 OpDecorate %120 NonUniform
4332 OpDecorate %_runtimearr_uint ArrayStride 4
4333 OpDecorate %_struct_39 Block
4334 OpMemberDecorate %_struct_39 0 Offset 0
4335 OpMemberDecorate %_struct_39 1 Offset 4
4336 OpDecorate %41 DescriptorSet 7
4337 OpDecorate %41 Binding 0
4338 OpDecorate %gl_FragCoord BuiltIn FragCoord
4339 OpDecorate %_struct_98 Block
4340 OpMemberDecorate %_struct_98 0 Offset 0
4341 OpDecorate %100 DescriptorSet 7
4342 OpDecorate %100 Binding 1
4343 OpDecorate %117 NonUniform
4344 %void = OpTypeVoid
4345 %10 = OpTypeFunction %void
4346 %float = OpTypeFloat 32
4347 %_ptr_Output_float = OpTypePointer Output %float
4348 %b = OpVariable %_ptr_Output_float Output
4349 %uname = OpTypeStruct %float
4350 %uint = OpTypeInt 32 0
4351 %uint_128 = OpConstant %uint 128
4352 %_arr_uname_uint_128 = OpTypeArray %uname %uint_128
4353 %_ptr_Uniform__arr_uname_uint_128 = OpTypePointer Uniform %_arr_uname_uint_128
4354 %uniformBuffer = OpVariable %_ptr_Uniform__arr_uname_uint_128 Uniform
4355 %int = OpTypeInt 32 1
4356 %_ptr_Input_int = OpTypePointer Input %int
4357 %nu_ii = OpVariable %_ptr_Input_int Input
4358 %int_0 = OpConstant %int 0
4359 %_ptr_Uniform_float = OpTypePointer Uniform %float
4360 %uint_0 = OpConstant %uint 0
4361 %bool = OpTypeBool
4362 %32 = OpTypeFunction %void %uint %uint %uint %uint
4363 %_runtimearr_uint = OpTypeRuntimeArray %uint
4364 %_struct_39 = OpTypeStruct %uint %_runtimearr_uint
4365 %_ptr_StorageBuffer__struct_39 = OpTypePointer StorageBuffer %_struct_39
4366 %41 = OpVariable %_ptr_StorageBuffer__struct_39 StorageBuffer
4367 %_ptr_StorageBuffer_uint = OpTypePointer StorageBuffer %uint
4368 %uint_10 = OpConstant %uint 10
4369 %uint_4 = OpConstant %uint 4
4370 %uint_1 = OpConstant %uint 1
4371 %uint_23 = OpConstant %uint 23
4372 %uint_2 = OpConstant %uint 2
4373 %uint_3 = OpConstant %uint 3
4374 %v4float = OpTypeVector %float 4
4375 %_ptr_Input_v4float = OpTypePointer Input %v4float
4376 %gl_FragCoord = OpVariable %_ptr_Input_v4float Input
4377 %v4uint = OpTypeVector %uint 4
4378 %uint_5 = OpConstant %uint 5
4379 %uint_7 = OpConstant %uint 7
4380 %uint_8 = OpConstant %uint 8
4381 %uint_9 = OpConstant %uint 9
4382 %uint_46 = OpConstant %uint 46
4383 %88 = OpConstantNull %float
4384 %92 = OpTypeFunction %uint %uint %uint %uint %uint
4385 %_struct_98 = OpTypeStruct %_runtimearr_uint
4386 %_ptr_StorageBuffer__struct_98 = OpTypePointer StorageBuffer %_struct_98
4387 %100 = OpVariable %_ptr_StorageBuffer__struct_98 StorageBuffer
4388 )";
4389
4390 const std::string func_before =
4391 R"(%main = OpFunction %void None %3
4392 %5 = OpLabel
4393 %18 = OpLoad %int %nu_ii
4394 %21 = OpAccessChain %_ptr_Uniform_float %uniformBuffer %18 %int_0
4395 %22 = OpLoad %float %21
4396 OpStore %b %22
4397 OpReturn
4398 OpFunctionEnd
4399 )";
4400
4401 const std::string func_after =
4402 R"(%main = OpFunction %void None %10
4403 %21 = OpLabel
4404 %7 = OpLoad %int %nu_ii
4405 %22 = OpAccessChain %_ptr_Uniform_float %uniformBuffer %7 %int_0
4406 %25 = OpULessThan %bool %7 %uint_128
4407 OpSelectionMerge %26 None
4408 OpBranchConditional %25 %27 %28
4409 %27 = OpLabel
4410 %90 = OpBitcast %uint %7
4411 %112 = OpFunctionCall %uint %91 %uint_0 %uint_0 %uint_3 %90
4412 %113 = OpULessThan %bool %uint_0 %112
4413 OpSelectionMerge %114 None
4414 OpBranchConditional %113 %115 %116
4415 %115 = OpLabel
4416 %117 = OpLoad %float %22
4417 OpBranch %114
4418 %116 = OpLabel
4419 %118 = OpBitcast %uint %7
4420 %119 = OpFunctionCall %void %31 %uint_46 %uint_1 %118 %uint_0
4421 OpBranch %114
4422 %114 = OpLabel
4423 %120 = OpPhi %float %117 %115 %88 %116
4424 OpBranch %26
4425 %28 = OpLabel
4426 %30 = OpBitcast %uint %7
4427 %87 = OpFunctionCall %void %31 %uint_46 %uint_0 %30 %uint_128
4428 OpBranch %26
4429 %26 = OpLabel
4430 %89 = OpPhi %float %120 %114 %88 %28
4431 OpStore %b %89
4432 OpReturn
4433 OpFunctionEnd
4434 )";
4435
4436 const std::string new_funcs =
4437 R"(%31 = OpFunction %void None %32
4438 %33 = OpFunctionParameter %uint
4439 %34 = OpFunctionParameter %uint
4440 %35 = OpFunctionParameter %uint
4441 %36 = OpFunctionParameter %uint
4442 %37 = OpLabel
4443 %43 = OpAccessChain %_ptr_StorageBuffer_uint %41 %uint_0
4444 %46 = OpAtomicIAdd %uint %43 %uint_4 %uint_0 %uint_10
4445 %47 = OpIAdd %uint %46 %uint_10
4446 %48 = OpArrayLength %uint %41 1
4447 %49 = OpULessThanEqual %bool %47 %48
4448 OpSelectionMerge %50 None
4449 OpBranchConditional %49 %51 %50
4450 %51 = OpLabel
4451 %52 = OpIAdd %uint %46 %uint_0
4452 %54 = OpAccessChain %_ptr_StorageBuffer_uint %41 %uint_1 %52
4453 OpStore %54 %uint_10
4454 %56 = OpIAdd %uint %46 %uint_1
4455 %57 = OpAccessChain %_ptr_StorageBuffer_uint %41 %uint_1 %56
4456 OpStore %57 %uint_23
4457 %59 = OpIAdd %uint %46 %uint_2
4458 %60 = OpAccessChain %_ptr_StorageBuffer_uint %41 %uint_1 %59
4459 OpStore %60 %33
4460 %62 = OpIAdd %uint %46 %uint_3
4461 %63 = OpAccessChain %_ptr_StorageBuffer_uint %41 %uint_1 %62
4462 OpStore %63 %uint_4
4463 %67 = OpLoad %v4float %gl_FragCoord
4464 %69 = OpBitcast %v4uint %67
4465 %70 = OpCompositeExtract %uint %69 0
4466 %71 = OpIAdd %uint %46 %uint_4
4467 %72 = OpAccessChain %_ptr_StorageBuffer_uint %41 %uint_1 %71
4468 OpStore %72 %70
4469 %73 = OpCompositeExtract %uint %69 1
4470 %75 = OpIAdd %uint %46 %uint_5
4471 %76 = OpAccessChain %_ptr_StorageBuffer_uint %41 %uint_1 %75
4472 OpStore %76 %73
4473 %78 = OpIAdd %uint %46 %uint_7
4474 %79 = OpAccessChain %_ptr_StorageBuffer_uint %41 %uint_1 %78
4475 OpStore %79 %34
4476 %81 = OpIAdd %uint %46 %uint_8
4477 %82 = OpAccessChain %_ptr_StorageBuffer_uint %41 %uint_1 %81
4478 OpStore %82 %35
4479 %84 = OpIAdd %uint %46 %uint_9
4480 %85 = OpAccessChain %_ptr_StorageBuffer_uint %41 %uint_1 %84
4481 OpStore %85 %36
4482 OpBranch %50
4483 %50 = OpLabel
4484 OpReturn
4485 OpFunctionEnd
4486 %91 = OpFunction %uint None %92
4487 %93 = OpFunctionParameter %uint
4488 %94 = OpFunctionParameter %uint
4489 %95 = OpFunctionParameter %uint
4490 %96 = OpFunctionParameter %uint
4491 %97 = OpLabel
4492 %101 = OpAccessChain %_ptr_StorageBuffer_uint %100 %uint_0 %93
4493 %102 = OpLoad %uint %101
4494 %103 = OpIAdd %uint %102 %94
4495 %104 = OpAccessChain %_ptr_StorageBuffer_uint %100 %uint_0 %103
4496 %105 = OpLoad %uint %104
4497 %106 = OpIAdd %uint %105 %95
4498 %107 = OpAccessChain %_ptr_StorageBuffer_uint %100 %uint_0 %106
4499 %108 = OpLoad %uint %107
4500 %109 = OpIAdd %uint %108 %96
4501 %110 = OpAccessChain %_ptr_StorageBuffer_uint %100 %uint_0 %109
4502 %111 = OpLoad %uint %110
4503 OpReturnValue %111
4504 OpFunctionEnd
4505 )";
4506
4507 // SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
4508 SinglePassRunAndCheck<InstBindlessCheckPass>(
4509 defs_before + func_before, defs_after + func_after + new_funcs, true,
4510 true, 7u, 23u, true, true);
4511 }
4512
4513 TEST_F(InstBindlessTest,
4514 InstBoundsComputeShaderInitLoadVariableSizedSampledImagesArray) {
4515 // #version 450
4516 // #extension GL_EXT_nonuniform_qualifier : enable
4517 //
4518 // layout (local_size_x = 1, local_size_y = 1) in;
4519 //
4520 // layout(set = 0, binding = 0, std140) buffer Input {
4521 // uint index;
4522 // float red;
4523 // } sbo;
4524 //
4525 // layout(set = 0, binding = 1, rgba32f) readonly uniform image2D images[];
4526 //
4527 // void main()
4528 // {
4529 // sbo.red = imageLoad(images[sbo.index], ivec2(0, 0)).r;
4530 // }
4531
4532 const std::string defs_before =
4533 R"(OpCapability Shader
4534 OpCapability RuntimeDescriptorArray
4535 OpExtension "SPV_EXT_descriptor_indexing"
4536 %1 = OpExtInstImport "GLSL.std.450"
4537 OpMemoryModel Logical GLSL450
4538 OpEntryPoint GLCompute %main "main"
4539 OpExecutionMode %main LocalSize 1 1 1
4540 OpSource GLSL 450
4541 OpSourceExtension "GL_EXT_nonuniform_qualifier"
4542 OpName %main "main"
4543 OpName %Input "Input"
4544 OpMemberName %Input 0 "index"
4545 OpMemberName %Input 1 "red"
4546 OpName %sbo "sbo"
4547 OpName %images "images"
4548 OpMemberDecorate %Input 0 Offset 0
4549 OpMemberDecorate %Input 1 Offset 4
4550 OpDecorate %Input BufferBlock
4551 OpDecorate %sbo DescriptorSet 0
4552 OpDecorate %sbo Binding 0
4553 OpDecorate %images DescriptorSet 0
4554 OpDecorate %images Binding 1
4555 OpDecorate %images NonWritable
4556 %void = OpTypeVoid
4557 %3 = OpTypeFunction %void
4558 %uint = OpTypeInt 32 0
4559 %float = OpTypeFloat 32
4560 %Input = OpTypeStruct %uint %float
4561 %_ptr_Uniform_Input = OpTypePointer Uniform %Input
4562 %sbo = OpVariable %_ptr_Uniform_Input Uniform
4563 %int = OpTypeInt 32 1
4564 %int_1 = OpConstant %int 1
4565 %13 = OpTypeImage %float 2D 0 0 0 2 Rgba32f
4566 %_runtimearr_13 = OpTypeRuntimeArray %13
4567 %_ptr_UniformConstant__runtimearr_13 = OpTypePointer UniformConstant %_runtimearr_13
4568 %images = OpVariable %_ptr_UniformConstant__runtimearr_13 UniformConstant
4569 %int_0 = OpConstant %int 0
4570 %_ptr_Uniform_uint = OpTypePointer Uniform %uint
4571 %_ptr_UniformConstant_13 = OpTypePointer UniformConstant %13
4572 %v2int = OpTypeVector %int 2
4573 %25 = OpConstantComposite %v2int %int_0 %int_0
4574 %v4float = OpTypeVector %float 4
4575 %uint_0 = OpConstant %uint 0
4576 %_ptr_Uniform_float = OpTypePointer Uniform %float
4577 )";
4578
4579 const std::string defs_after =
4580 R"(OpCapability Shader
4581 OpCapability RuntimeDescriptorArray
4582 OpExtension "SPV_EXT_descriptor_indexing"
4583 OpExtension "SPV_KHR_storage_buffer_storage_class"
4584 %1 = OpExtInstImport "GLSL.std.450"
4585 OpMemoryModel Logical GLSL450
4586 OpEntryPoint GLCompute %main "main" %gl_GlobalInvocationID
4587 OpExecutionMode %main LocalSize 1 1 1
4588 OpSource GLSL 450
4589 OpSourceExtension "GL_EXT_nonuniform_qualifier"
4590 OpName %main "main"
4591 OpName %Input "Input"
4592 OpMemberName %Input 0 "index"
4593 OpMemberName %Input 1 "red"
4594 OpName %sbo "sbo"
4595 OpName %images "images"
4596 OpMemberDecorate %Input 0 Offset 0
4597 OpMemberDecorate %Input 1 Offset 4
4598 OpDecorate %Input BufferBlock
4599 OpDecorate %sbo DescriptorSet 0
4600 OpDecorate %sbo Binding 0
4601 OpDecorate %images DescriptorSet 0
4602 OpDecorate %images Binding 1
4603 OpDecorate %images NonWritable
4604 OpDecorate %_runtimearr_uint ArrayStride 4
4605 OpDecorate %_struct_39 Block
4606 OpMemberDecorate %_struct_39 0 Offset 0
4607 OpDecorate %41 DescriptorSet 7
4608 OpDecorate %41 Binding 1
4609 OpDecorate %_struct_63 Block
4610 OpMemberDecorate %_struct_63 0 Offset 0
4611 OpMemberDecorate %_struct_63 1 Offset 4
4612 OpDecorate %65 DescriptorSet 7
4613 OpDecorate %65 Binding 0
4614 OpDecorate %gl_GlobalInvocationID BuiltIn GlobalInvocationId
4615 %void = OpTypeVoid
4616 %7 = OpTypeFunction %void
4617 %uint = OpTypeInt 32 0
4618 %float = OpTypeFloat 32
4619 %Input = OpTypeStruct %uint %float
4620 %_ptr_Uniform_Input = OpTypePointer Uniform %Input
4621 %sbo = OpVariable %_ptr_Uniform_Input Uniform
4622 %int = OpTypeInt 32 1
4623 %int_1 = OpConstant %int 1
4624 %13 = OpTypeImage %float 2D 0 0 0 2 Rgba32f
4625 %_runtimearr_13 = OpTypeRuntimeArray %13
4626 %_ptr_UniformConstant__runtimearr_13 = OpTypePointer UniformConstant %_runtimearr_13
4627 %images = OpVariable %_ptr_UniformConstant__runtimearr_13 UniformConstant
4628 %int_0 = OpConstant %int 0
4629 %_ptr_Uniform_uint = OpTypePointer Uniform %uint
4630 %_ptr_UniformConstant_13 = OpTypePointer UniformConstant %13
4631 %v2int = OpTypeVector %int 2
4632 %20 = OpConstantComposite %v2int %int_0 %int_0
4633 %v4float = OpTypeVector %float 4
4634 %uint_0 = OpConstant %uint 0
4635 %_ptr_Uniform_float = OpTypePointer Uniform %float
4636 %uint_1 = OpConstant %uint 1
4637 %34 = OpTypeFunction %uint %uint %uint
4638 %_runtimearr_uint = OpTypeRuntimeArray %uint
4639 %_struct_39 = OpTypeStruct %_runtimearr_uint
4640 %_ptr_StorageBuffer__struct_39 = OpTypePointer StorageBuffer %_struct_39
4641 %41 = OpVariable %_ptr_StorageBuffer__struct_39 StorageBuffer
4642 %_ptr_StorageBuffer_uint = OpTypePointer StorageBuffer %uint
4643 %bool = OpTypeBool
4644 %57 = OpTypeFunction %void %uint %uint %uint %uint
4645 %_struct_63 = OpTypeStruct %uint %_runtimearr_uint
4646 %_ptr_StorageBuffer__struct_63 = OpTypePointer StorageBuffer %_struct_63
4647 %65 = OpVariable %_ptr_StorageBuffer__struct_63 StorageBuffer
4648 %uint_10 = OpConstant %uint 10
4649 %uint_4 = OpConstant %uint 4
4650 %uint_23 = OpConstant %uint 23
4651 %uint_2 = OpConstant %uint 2
4652 %uint_5 = OpConstant %uint 5
4653 %uint_3 = OpConstant %uint 3
4654 %v3uint = OpTypeVector %uint 3
4655 %_ptr_Input_v3uint = OpTypePointer Input %v3uint
4656 %gl_GlobalInvocationID = OpVariable %_ptr_Input_v3uint Input
4657 %uint_6 = OpConstant %uint 6
4658 %uint_7 = OpConstant %uint 7
4659 %uint_8 = OpConstant %uint 8
4660 %uint_9 = OpConstant %uint 9
4661 %uint_50 = OpConstant %uint 50
4662 %112 = OpConstantNull %v4float
4663 %115 = OpTypeFunction %uint %uint %uint %uint %uint
4664 %uint_47 = OpConstant %uint 47
4665 %140 = OpConstantNull %uint
4666 %uint_53 = OpConstant %uint 53
4667 )";
4668
4669 const std::string func_before =
4670 R"(%main = OpFunction %void None %3
4671 %5 = OpLabel
4672 %19 = OpAccessChain %_ptr_Uniform_uint %sbo %int_0
4673 %20 = OpLoad %uint %19
4674 %22 = OpAccessChain %_ptr_UniformConstant_13 %images %20
4675 %23 = OpLoad %13 %22
4676 %27 = OpImageRead %v4float %23 %25
4677 %29 = OpCompositeExtract %float %27 0
4678 %31 = OpAccessChain %_ptr_Uniform_float %sbo %int_1
4679 OpStore %31 %29
4680 OpReturn
4681 OpFunctionEnd
4682 )";
4683
4684 const std::string func_after =
4685 R"(%main = OpFunction %void None %7
4686 %24 = OpLabel
4687 %25 = OpAccessChain %_ptr_Uniform_uint %sbo %int_0
4688 %132 = OpFunctionCall %uint %114 %uint_0 %uint_0 %uint_0 %uint_0
4689 %133 = OpULessThan %bool %uint_0 %132
4690 OpSelectionMerge %134 None
4691 OpBranchConditional %133 %135 %136
4692 %135 = OpLabel
4693 %137 = OpLoad %uint %25
4694 OpBranch %134
4695 %136 = OpLabel
4696 %139 = OpFunctionCall %void %56 %uint_47 %uint_1 %uint_0 %uint_0
4697 OpBranch %134
4698 %134 = OpLabel
4699 %141 = OpPhi %uint %137 %135 %140 %136
4700 %27 = OpAccessChain %_ptr_UniformConstant_13 %images %141
4701 %28 = OpLoad %13 %27
4702 %48 = OpFunctionCall %uint %33 %uint_1 %uint_1
4703 %50 = OpULessThan %bool %141 %48
4704 OpSelectionMerge %51 None
4705 OpBranchConditional %50 %52 %53
4706 %52 = OpLabel
4707 %54 = OpLoad %13 %27
4708 %142 = OpFunctionCall %uint %114 %uint_0 %uint_0 %uint_1 %141
4709 %143 = OpULessThan %bool %uint_0 %142
4710 OpSelectionMerge %144 None
4711 OpBranchConditional %143 %145 %146
4712 %145 = OpLabel
4713 %147 = OpLoad %13 %27
4714 %148 = OpImageRead %v4float %147 %20
4715 OpBranch %144
4716 %146 = OpLabel
4717 %149 = OpFunctionCall %void %56 %uint_50 %uint_1 %141 %uint_0
4718 OpBranch %144
4719 %144 = OpLabel
4720 %150 = OpPhi %v4float %148 %145 %112 %146
4721 OpBranch %51
4722 %53 = OpLabel
4723 %111 = OpFunctionCall %void %56 %uint_50 %uint_0 %141 %48
4724 OpBranch %51
4725 %51 = OpLabel
4726 %113 = OpPhi %v4float %150 %144 %112 %53
4727 %30 = OpCompositeExtract %float %113 0
4728 %31 = OpAccessChain %_ptr_Uniform_float %sbo %int_1
4729 %151 = OpFunctionCall %uint %114 %uint_0 %uint_0 %uint_0 %uint_0
4730 %152 = OpULessThan %bool %uint_0 %151
4731 OpSelectionMerge %153 None
4732 OpBranchConditional %152 %154 %155
4733 %154 = OpLabel
4734 OpStore %31 %30
4735 OpBranch %153
4736 %155 = OpLabel
4737 %157 = OpFunctionCall %void %56 %uint_53 %uint_1 %uint_0 %uint_0
4738 OpBranch %153
4739 %153 = OpLabel
4740 OpReturn
4741 OpFunctionEnd
4742 )";
4743
4744 const std::string new_funcs =
4745 R"(%33 = OpFunction %uint None %34
4746 %35 = OpFunctionParameter %uint
4747 %36 = OpFunctionParameter %uint
4748 %37 = OpLabel
4749 %43 = OpAccessChain %_ptr_StorageBuffer_uint %41 %uint_0 %35
4750 %44 = OpLoad %uint %43
4751 %45 = OpIAdd %uint %44 %36
4752 %46 = OpAccessChain %_ptr_StorageBuffer_uint %41 %uint_0 %45
4753 %47 = OpLoad %uint %46
4754 OpReturnValue %47
4755 OpFunctionEnd
4756 %56 = OpFunction %void None %57
4757 %58 = OpFunctionParameter %uint
4758 %59 = OpFunctionParameter %uint
4759 %60 = OpFunctionParameter %uint
4760 %61 = OpFunctionParameter %uint
4761 %62 = OpLabel
4762 %66 = OpAccessChain %_ptr_StorageBuffer_uint %65 %uint_0
4763 %69 = OpAtomicIAdd %uint %66 %uint_4 %uint_0 %uint_10
4764 %70 = OpIAdd %uint %69 %uint_10
4765 %71 = OpArrayLength %uint %65 1
4766 %72 = OpULessThanEqual %bool %70 %71
4767 OpSelectionMerge %73 None
4768 OpBranchConditional %72 %74 %73
4769 %74 = OpLabel
4770 %75 = OpIAdd %uint %69 %uint_0
4771 %76 = OpAccessChain %_ptr_StorageBuffer_uint %65 %uint_1 %75
4772 OpStore %76 %uint_10
4773 %78 = OpIAdd %uint %69 %uint_1
4774 %79 = OpAccessChain %_ptr_StorageBuffer_uint %65 %uint_1 %78
4775 OpStore %79 %uint_23
4776 %81 = OpIAdd %uint %69 %uint_2
4777 %82 = OpAccessChain %_ptr_StorageBuffer_uint %65 %uint_1 %81
4778 OpStore %82 %58
4779 %85 = OpIAdd %uint %69 %uint_3
4780 %86 = OpAccessChain %_ptr_StorageBuffer_uint %65 %uint_1 %85
4781 OpStore %86 %uint_5
4782 %90 = OpLoad %v3uint %gl_GlobalInvocationID
4783 %91 = OpCompositeExtract %uint %90 0
4784 %92 = OpCompositeExtract %uint %90 1
4785 %93 = OpCompositeExtract %uint %90 2
4786 %94 = OpIAdd %uint %69 %uint_4
4787 %95 = OpAccessChain %_ptr_StorageBuffer_uint %65 %uint_1 %94
4788 OpStore %95 %91
4789 %96 = OpIAdd %uint %69 %uint_5
4790 %97 = OpAccessChain %_ptr_StorageBuffer_uint %65 %uint_1 %96
4791 OpStore %97 %92
4792 %99 = OpIAdd %uint %69 %uint_6
4793 %100 = OpAccessChain %_ptr_StorageBuffer_uint %65 %uint_1 %99
4794 OpStore %100 %93
4795 %102 = OpIAdd %uint %69 %uint_7
4796 %103 = OpAccessChain %_ptr_StorageBuffer_uint %65 %uint_1 %102
4797 OpStore %103 %59
4798 %105 = OpIAdd %uint %69 %uint_8
4799 %106 = OpAccessChain %_ptr_StorageBuffer_uint %65 %uint_1 %105
4800 OpStore %106 %60
4801 %108 = OpIAdd %uint %69 %uint_9
4802 %109 = OpAccessChain %_ptr_StorageBuffer_uint %65 %uint_1 %108
4803 OpStore %109 %61
4804 OpBranch %73
4805 %73 = OpLabel
4806 OpReturn
4807 OpFunctionEnd
4808 %114 = OpFunction %uint None %115
4809 %116 = OpFunctionParameter %uint
4810 %117 = OpFunctionParameter %uint
4811 %118 = OpFunctionParameter %uint
4812 %119 = OpFunctionParameter %uint
4813 %120 = OpLabel
4814 %121 = OpAccessChain %_ptr_StorageBuffer_uint %41 %uint_0 %116
4815 %122 = OpLoad %uint %121
4816 %123 = OpIAdd %uint %122 %117
4817 %124 = OpAccessChain %_ptr_StorageBuffer_uint %41 %uint_0 %123
4818 %125 = OpLoad %uint %124
4819 %126 = OpIAdd %uint %125 %118
4820 %127 = OpAccessChain %_ptr_StorageBuffer_uint %41 %uint_0 %126
4821 %128 = OpLoad %uint %127
4822 %129 = OpIAdd %uint %128 %119
4823 %130 = OpAccessChain %_ptr_StorageBuffer_uint %41 %uint_0 %129
4824 %131 = OpLoad %uint %130
4825 OpReturnValue %131
4826 OpFunctionEnd
4827 )";
4828
4829 // SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
4830 SinglePassRunAndCheck<InstBindlessCheckPass>(
4831 defs_before + func_before, defs_after + func_after + new_funcs, true,
4832 true, 7u, 23u, true, true);
4833 }
4834
4835 TEST_F(InstBindlessTest,
4836 InstBoundsRayGenerationInitLoadVariableSizedSampledImagesArray) {
4837 // #version 460
4838 // #extension GL_EXT_nonuniform_qualifier : require
4839 // #extension GL_NV_ray_tracing : require
4840 //
4841 // layout(set = 0, binding = 0, std140) buffer StorageBuffer {
4842 // uint index;
4843 // float red;
4844 // } sbo;
4845 //
4846 // layout(set = 0, binding = 1, rgba32f) readonly uniform image2D images[];
4847 //
4848 // void main()
4849 // {
4850 // sbo.red = imageLoad(images[sbo.index], ivec2(0, 0)).r;
4851 // }
4852
4853 const std::string defs_before =
4854 R"(OpCapability RuntimeDescriptorArray
4855 OpCapability RayTracingNV
4856 OpExtension "SPV_EXT_descriptor_indexing"
4857 OpExtension "SPV_NV_ray_tracing"
4858 %1 = OpExtInstImport "GLSL.std.450"
4859 OpMemoryModel Logical GLSL450
4860 OpEntryPoint RayGenerationNV %main "main"
4861 OpSource GLSL 460
4862 OpSourceExtension "GL_EXT_nonuniform_qualifier"
4863 OpSourceExtension "GL_NV_ray_tracing"
4864 OpName %main "main"
4865 OpName %StorageBuffer "StorageBuffer"
4866 OpMemberName %StorageBuffer 0 "index"
4867 OpMemberName %StorageBuffer 1 "red"
4868 OpName %sbo "sbo"
4869 OpName %images "images"
4870 OpMemberDecorate %StorageBuffer 0 Offset 0
4871 OpMemberDecorate %StorageBuffer 1 Offset 4
4872 OpDecorate %StorageBuffer BufferBlock
4873 OpDecorate %sbo DescriptorSet 0
4874 OpDecorate %sbo Binding 0
4875 OpDecorate %images DescriptorSet 0
4876 OpDecorate %images Binding 1
4877 OpDecorate %images NonWritable
4878 %void = OpTypeVoid
4879 )";
4880
4881 const std::string defs_after =
4882 R"(OpCapability RuntimeDescriptorArray
4883 OpCapability RayTracingNV
4884 OpExtension "SPV_EXT_descriptor_indexing"
4885 OpExtension "SPV_NV_ray_tracing"
4886 OpExtension "SPV_KHR_storage_buffer_storage_class"
4887 %1 = OpExtInstImport "GLSL.std.450"
4888 OpMemoryModel Logical GLSL450
4889 OpEntryPoint RayGenerationNV %main "main" %89
4890 OpSource GLSL 460
4891 OpSourceExtension "GL_EXT_nonuniform_qualifier"
4892 OpSourceExtension "GL_NV_ray_tracing"
4893 OpName %main "main"
4894 OpName %StorageBuffer "StorageBuffer"
4895 OpMemberName %StorageBuffer 0 "index"
4896 OpMemberName %StorageBuffer 1 "red"
4897 OpName %sbo "sbo"
4898 OpName %images "images"
4899 OpMemberDecorate %StorageBuffer 0 Offset 0
4900 OpMemberDecorate %StorageBuffer 1 Offset 4
4901 OpDecorate %StorageBuffer BufferBlock
4902 OpDecorate %sbo DescriptorSet 0
4903 OpDecorate %sbo Binding 0
4904 OpDecorate %images DescriptorSet 0
4905 OpDecorate %images Binding 1
4906 OpDecorate %images NonWritable
4907 OpDecorate %_runtimearr_uint ArrayStride 4
4908 OpDecorate %_struct_39 Block
4909 OpMemberDecorate %_struct_39 0 Offset 0
4910 OpDecorate %41 DescriptorSet 7
4911 OpDecorate %41 Binding 1
4912 OpDecorate %_struct_63 Block
4913 OpMemberDecorate %_struct_63 0 Offset 0
4914 OpMemberDecorate %_struct_63 1 Offset 4
4915 OpDecorate %65 DescriptorSet 7
4916 OpDecorate %65 Binding 0
4917 OpDecorate %89 BuiltIn LaunchIdNV
4918 %void = OpTypeVoid
4919 )";
4920
4921 const std::string func_before =
4922 R"(%3 = OpTypeFunction %void
4923 %uint = OpTypeInt 32 0
4924 %float = OpTypeFloat 32
4925 %StorageBuffer = OpTypeStruct %uint %float
4926 %_ptr_Uniform_StorageBuffer = OpTypePointer Uniform %StorageBuffer
4927 %sbo = OpVariable %_ptr_Uniform_StorageBuffer Uniform
4928 %int = OpTypeInt 32 1
4929 %int_1 = OpConstant %int 1
4930 %13 = OpTypeImage %float 2D 0 0 0 2 Rgba32f
4931 %_runtimearr_13 = OpTypeRuntimeArray %13
4932 %_ptr_UniformConstant__runtimearr_13 = OpTypePointer UniformConstant %_runtimearr_13
4933 %images = OpVariable %_ptr_UniformConstant__runtimearr_13 UniformConstant
4934 %int_0 = OpConstant %int 0
4935 %_ptr_Uniform_uint = OpTypePointer Uniform %uint
4936 %_ptr_UniformConstant_13 = OpTypePointer UniformConstant %13
4937 %v2int = OpTypeVector %int 2
4938 %25 = OpConstantComposite %v2int %int_0 %int_0
4939 %v4float = OpTypeVector %float 4
4940 %uint_0 = OpConstant %uint 0
4941 %_ptr_Uniform_float = OpTypePointer Uniform %float
4942 %main = OpFunction %void None %3
4943 %5 = OpLabel
4944 %19 = OpAccessChain %_ptr_Uniform_uint %sbo %int_0
4945 %20 = OpLoad %uint %19
4946 %22 = OpAccessChain %_ptr_UniformConstant_13 %images %20
4947 %23 = OpLoad %13 %22
4948 %27 = OpImageRead %v4float %23 %25
4949 %29 = OpCompositeExtract %float %27 0
4950 %31 = OpAccessChain %_ptr_Uniform_float %sbo %int_1
4951 OpStore %31 %29
4952 OpReturn
4953 OpFunctionEnd
4954 )";
4955
4956 const std::string func_after =
4957 R"(%7 = OpTypeFunction %void
4958 %uint = OpTypeInt 32 0
4959 %float = OpTypeFloat 32
4960 %StorageBuffer = OpTypeStruct %uint %float
4961 %_ptr_Uniform_StorageBuffer = OpTypePointer Uniform %StorageBuffer
4962 %sbo = OpVariable %_ptr_Uniform_StorageBuffer Uniform
4963 %int = OpTypeInt 32 1
4964 %int_1 = OpConstant %int 1
4965 %13 = OpTypeImage %float 2D 0 0 0 2 Rgba32f
4966 %_runtimearr_13 = OpTypeRuntimeArray %13
4967 %_ptr_UniformConstant__runtimearr_13 = OpTypePointer UniformConstant %_runtimearr_13
4968 %images = OpVariable %_ptr_UniformConstant__runtimearr_13 UniformConstant
4969 %int_0 = OpConstant %int 0
4970 %_ptr_Uniform_uint = OpTypePointer Uniform %uint
4971 %_ptr_UniformConstant_13 = OpTypePointer UniformConstant %13
4972 %v2int = OpTypeVector %int 2
4973 %20 = OpConstantComposite %v2int %int_0 %int_0
4974 %v4float = OpTypeVector %float 4
4975 %uint_0 = OpConstant %uint 0
4976 %_ptr_Uniform_float = OpTypePointer Uniform %float
4977 %uint_1 = OpConstant %uint 1
4978 %34 = OpTypeFunction %uint %uint %uint
4979 %_runtimearr_uint = OpTypeRuntimeArray %uint
4980 %_struct_39 = OpTypeStruct %_runtimearr_uint
4981 %_ptr_StorageBuffer__struct_39 = OpTypePointer StorageBuffer %_struct_39
4982 %41 = OpVariable %_ptr_StorageBuffer__struct_39 StorageBuffer
4983 %_ptr_StorageBuffer_uint = OpTypePointer StorageBuffer %uint
4984 %bool = OpTypeBool
4985 %57 = OpTypeFunction %void %uint %uint %uint %uint
4986 %_struct_63 = OpTypeStruct %uint %_runtimearr_uint
4987 %_ptr_StorageBuffer__struct_63 = OpTypePointer StorageBuffer %_struct_63
4988 %65 = OpVariable %_ptr_StorageBuffer__struct_63 StorageBuffer
4989 %uint_10 = OpConstant %uint 10
4990 %uint_4 = OpConstant %uint 4
4991 %uint_23 = OpConstant %uint 23
4992 %uint_2 = OpConstant %uint 2
4993 %uint_5313 = OpConstant %uint 5313
4994 %uint_3 = OpConstant %uint 3
4995 %v3uint = OpTypeVector %uint 3
4996 %_ptr_Input_v3uint = OpTypePointer Input %v3uint
4997 %89 = OpVariable %_ptr_Input_v3uint Input
4998 %uint_5 = OpConstant %uint 5
4999 %uint_6 = OpConstant %uint 6
5000 %uint_7 = OpConstant %uint 7
5001 %uint_8 = OpConstant %uint 8
5002 %uint_9 = OpConstant %uint 9
5003 %uint_51 = OpConstant %uint 51
5004 %113 = OpConstantNull %v4float
5005 %116 = OpTypeFunction %uint %uint %uint %uint %uint
5006 %uint_48 = OpConstant %uint 48
5007 %141 = OpConstantNull %uint
5008 %uint_54 = OpConstant %uint 54
5009 %main = OpFunction %void None %7
5010 %24 = OpLabel
5011 %25 = OpAccessChain %_ptr_Uniform_uint %sbo %int_0
5012 %133 = OpFunctionCall %uint %115 %uint_0 %uint_0 %uint_0 %uint_0
5013 %134 = OpULessThan %bool %uint_0 %133
5014 OpSelectionMerge %135 None
5015 OpBranchConditional %134 %136 %137
5016 %136 = OpLabel
5017 %138 = OpLoad %uint %25
5018 OpBranch %135
5019 %137 = OpLabel
5020 %140 = OpFunctionCall %void %56 %uint_48 %uint_1 %uint_0 %uint_0
5021 OpBranch %135
5022 %135 = OpLabel
5023 %142 = OpPhi %uint %138 %136 %141 %137
5024 %27 = OpAccessChain %_ptr_UniformConstant_13 %images %142
5025 %28 = OpLoad %13 %27
5026 %48 = OpFunctionCall %uint %33 %uint_1 %uint_1
5027 %50 = OpULessThan %bool %142 %48
5028 OpSelectionMerge %51 None
5029 OpBranchConditional %50 %52 %53
5030 %52 = OpLabel
5031 %54 = OpLoad %13 %27
5032 %143 = OpFunctionCall %uint %115 %uint_0 %uint_0 %uint_1 %142
5033 %144 = OpULessThan %bool %uint_0 %143
5034 OpSelectionMerge %145 None
5035 OpBranchConditional %144 %146 %147
5036 %146 = OpLabel
5037 %148 = OpLoad %13 %27
5038 %149 = OpImageRead %v4float %148 %20
5039 OpBranch %145
5040 %147 = OpLabel
5041 %150 = OpFunctionCall %void %56 %uint_51 %uint_1 %142 %uint_0
5042 OpBranch %145
5043 %145 = OpLabel
5044 %151 = OpPhi %v4float %149 %146 %113 %147
5045 OpBranch %51
5046 %53 = OpLabel
5047 %112 = OpFunctionCall %void %56 %uint_51 %uint_0 %142 %48
5048 OpBranch %51
5049 %51 = OpLabel
5050 %114 = OpPhi %v4float %151 %145 %113 %53
5051 %30 = OpCompositeExtract %float %114 0
5052 %31 = OpAccessChain %_ptr_Uniform_float %sbo %int_1
5053 %152 = OpFunctionCall %uint %115 %uint_0 %uint_0 %uint_0 %uint_0
5054 %153 = OpULessThan %bool %uint_0 %152
5055 OpSelectionMerge %154 None
5056 OpBranchConditional %153 %155 %156
5057 %155 = OpLabel
5058 OpStore %31 %30
5059 OpBranch %154
5060 %156 = OpLabel
5061 %158 = OpFunctionCall %void %56 %uint_54 %uint_1 %uint_0 %uint_0
5062 OpBranch %154
5063 %154 = OpLabel
5064 OpReturn
5065 OpFunctionEnd
5066 )";
5067
5068 const std::string new_funcs =
5069 R"(%33 = OpFunction %uint None %34
5070 %35 = OpFunctionParameter %uint
5071 %36 = OpFunctionParameter %uint
5072 %37 = OpLabel
5073 %43 = OpAccessChain %_ptr_StorageBuffer_uint %41 %uint_0 %35
5074 %44 = OpLoad %uint %43
5075 %45 = OpIAdd %uint %44 %36
5076 %46 = OpAccessChain %_ptr_StorageBuffer_uint %41 %uint_0 %45
5077 %47 = OpLoad %uint %46
5078 OpReturnValue %47
5079 OpFunctionEnd
5080 %56 = OpFunction %void None %57
5081 %58 = OpFunctionParameter %uint
5082 %59 = OpFunctionParameter %uint
5083 %60 = OpFunctionParameter %uint
5084 %61 = OpFunctionParameter %uint
5085 %62 = OpLabel
5086 %66 = OpAccessChain %_ptr_StorageBuffer_uint %65 %uint_0
5087 %69 = OpAtomicIAdd %uint %66 %uint_4 %uint_0 %uint_10
5088 %70 = OpIAdd %uint %69 %uint_10
5089 %71 = OpArrayLength %uint %65 1
5090 %72 = OpULessThanEqual %bool %70 %71
5091 OpSelectionMerge %73 None
5092 OpBranchConditional %72 %74 %73
5093 %74 = OpLabel
5094 %75 = OpIAdd %uint %69 %uint_0
5095 %76 = OpAccessChain %_ptr_StorageBuffer_uint %65 %uint_1 %75
5096 OpStore %76 %uint_10
5097 %78 = OpIAdd %uint %69 %uint_1
5098 %79 = OpAccessChain %_ptr_StorageBuffer_uint %65 %uint_1 %78
5099 OpStore %79 %uint_23
5100 %81 = OpIAdd %uint %69 %uint_2
5101 %82 = OpAccessChain %_ptr_StorageBuffer_uint %65 %uint_1 %81
5102 OpStore %82 %58
5103 %85 = OpIAdd %uint %69 %uint_3
5104 %86 = OpAccessChain %_ptr_StorageBuffer_uint %65 %uint_1 %85
5105 OpStore %86 %uint_5313
5106 %90 = OpLoad %v3uint %89
5107 %91 = OpCompositeExtract %uint %90 0
5108 %92 = OpCompositeExtract %uint %90 1
5109 %93 = OpCompositeExtract %uint %90 2
5110 %94 = OpIAdd %uint %69 %uint_4
5111 %95 = OpAccessChain %_ptr_StorageBuffer_uint %65 %uint_1 %94
5112 OpStore %95 %91
5113 %97 = OpIAdd %uint %69 %uint_5
5114 %98 = OpAccessChain %_ptr_StorageBuffer_uint %65 %uint_1 %97
5115 OpStore %98 %92
5116 %100 = OpIAdd %uint %69 %uint_6
5117 %101 = OpAccessChain %_ptr_StorageBuffer_uint %65 %uint_1 %100
5118 OpStore %101 %93
5119 %103 = OpIAdd %uint %69 %uint_7
5120 %104 = OpAccessChain %_ptr_StorageBuffer_uint %65 %uint_1 %103
5121 OpStore %104 %59
5122 %106 = OpIAdd %uint %69 %uint_8
5123 %107 = OpAccessChain %_ptr_StorageBuffer_uint %65 %uint_1 %106
5124 OpStore %107 %60
5125 %109 = OpIAdd %uint %69 %uint_9
5126 %110 = OpAccessChain %_ptr_StorageBuffer_uint %65 %uint_1 %109
5127 OpStore %110 %61
5128 OpBranch %73
5129 %73 = OpLabel
5130 OpReturn
5131 OpFunctionEnd
5132 %115 = OpFunction %uint None %116
5133 %117 = OpFunctionParameter %uint
5134 %118 = OpFunctionParameter %uint
5135 %119 = OpFunctionParameter %uint
5136 %120 = OpFunctionParameter %uint
5137 %121 = OpLabel
5138 %122 = OpAccessChain %_ptr_StorageBuffer_uint %41 %uint_0 %117
5139 %123 = OpLoad %uint %122
5140 %124 = OpIAdd %uint %123 %118
5141 %125 = OpAccessChain %_ptr_StorageBuffer_uint %41 %uint_0 %124
5142 %126 = OpLoad %uint %125
5143 %127 = OpIAdd %uint %126 %119
5144 %128 = OpAccessChain %_ptr_StorageBuffer_uint %41 %uint_0 %127
5145 %129 = OpLoad %uint %128
5146 %130 = OpIAdd %uint %129 %120
5147 %131 = OpAccessChain %_ptr_StorageBuffer_uint %41 %uint_0 %130
5148 %132 = OpLoad %uint %131
5149 OpReturnValue %132
5150 OpFunctionEnd
5151 )";
5152
5153 // SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
5154 SinglePassRunAndCheck<InstBindlessCheckPass>(
5155 defs_before + func_before, defs_after + func_after + new_funcs, true,
5156 true, 7u, 23u, true, true);
5157 }
5158
5159 TEST_F(InstBindlessTest,
5160 InstBoundsIntersectionInitLoadVariableSizedSampledImagesArray) {
5161 // #version 460
5162 // #extension GL_EXT_nonuniform_qualifier : require
5163 // #extension GL_NV_ray_tracing : require
5164 //
5165 // layout(set = 0, binding = 0, std140) buffer StorageBuffer {
5166 // uint index;
5167 // float red;
5168 // } sbo;
5169 //
5170 // layout(set = 0, binding = 1, rgba32f) readonly uniform image2D images[];
5171 //
5172 // void main()
5173 // {
5174 // sbo.red = imageLoad(images[sbo.index], ivec2(0, 0)).r;
5175 // }
5176
5177 const std::string defs_before =
5178 R"(OpCapability RuntimeDescriptorArray
5179 OpCapability RayTracingNV
5180 OpExtension "SPV_EXT_descriptor_indexing"
5181 OpExtension "SPV_NV_ray_tracing"
5182 %1 = OpExtInstImport "GLSL.std.450"
5183 OpMemoryModel Logical GLSL450
5184 OpEntryPoint IntersectionNV %main "main"
5185 OpSource GLSL 460
5186 OpSourceExtension "GL_EXT_nonuniform_qualifier"
5187 OpSourceExtension "GL_NV_ray_tracing"
5188 OpName %main "main"
5189 OpName %StorageBuffer "StorageBuffer"
5190 OpMemberName %StorageBuffer 0 "index"
5191 OpMemberName %StorageBuffer 1 "red"
5192 OpName %sbo "sbo"
5193 OpName %images "images"
5194 OpMemberDecorate %StorageBuffer 0 Offset 0
5195 OpMemberDecorate %StorageBuffer 1 Offset 4
5196 OpDecorate %StorageBuffer BufferBlock
5197 OpDecorate %sbo DescriptorSet 0
5198 OpDecorate %sbo Binding 0
5199 OpDecorate %images DescriptorSet 0
5200 OpDecorate %images Binding 1
5201 OpDecorate %images NonWritable
5202 %void = OpTypeVoid
5203 )";
5204
5205 const std::string defs_after =
5206 R"(OpCapability RuntimeDescriptorArray
5207 OpCapability RayTracingNV
5208 OpExtension "SPV_EXT_descriptor_indexing"
5209 OpExtension "SPV_NV_ray_tracing"
5210 OpExtension "SPV_KHR_storage_buffer_storage_class"
5211 %1 = OpExtInstImport "GLSL.std.450"
5212 OpMemoryModel Logical GLSL450
5213 OpEntryPoint IntersectionNV %main "main" %89
5214 OpSource GLSL 460
5215 OpSourceExtension "GL_EXT_nonuniform_qualifier"
5216 OpSourceExtension "GL_NV_ray_tracing"
5217 OpName %main "main"
5218 OpName %StorageBuffer "StorageBuffer"
5219 OpMemberName %StorageBuffer 0 "index"
5220 OpMemberName %StorageBuffer 1 "red"
5221 OpName %sbo "sbo"
5222 OpName %images "images"
5223 OpMemberDecorate %StorageBuffer 0 Offset 0
5224 OpMemberDecorate %StorageBuffer 1 Offset 4
5225 OpDecorate %StorageBuffer BufferBlock
5226 OpDecorate %sbo DescriptorSet 0
5227 OpDecorate %sbo Binding 0
5228 OpDecorate %images DescriptorSet 0
5229 OpDecorate %images Binding 1
5230 OpDecorate %images NonWritable
5231 OpDecorate %_runtimearr_uint ArrayStride 4
5232 OpDecorate %_struct_39 Block
5233 OpMemberDecorate %_struct_39 0 Offset 0
5234 OpDecorate %41 DescriptorSet 7
5235 OpDecorate %41 Binding 1
5236 OpDecorate %_struct_63 Block
5237 OpMemberDecorate %_struct_63 0 Offset 0
5238 OpMemberDecorate %_struct_63 1 Offset 4
5239 OpDecorate %65 DescriptorSet 7
5240 OpDecorate %65 Binding 0
5241 OpDecorate %89 BuiltIn LaunchIdNV
5242 %void = OpTypeVoid
5243 )";
5244
5245 const std::string func_before =
5246 R"(%3 = OpTypeFunction %void
5247 %uint = OpTypeInt 32 0
5248 %float = OpTypeFloat 32
5249 %StorageBuffer = OpTypeStruct %uint %float
5250 %_ptr_Uniform_StorageBuffer = OpTypePointer Uniform %StorageBuffer
5251 %sbo = OpVariable %_ptr_Uniform_StorageBuffer Uniform
5252 %int = OpTypeInt 32 1
5253 %int_1 = OpConstant %int 1
5254 %13 = OpTypeImage %float 2D 0 0 0 2 Rgba32f
5255 %_runtimearr_13 = OpTypeRuntimeArray %13
5256 %_ptr_UniformConstant__runtimearr_13 = OpTypePointer UniformConstant %_runtimearr_13
5257 %images = OpVariable %_ptr_UniformConstant__runtimearr_13 UniformConstant
5258 %int_0 = OpConstant %int 0
5259 %_ptr_Uniform_uint = OpTypePointer Uniform %uint
5260 %_ptr_UniformConstant_13 = OpTypePointer UniformConstant %13
5261 %v2int = OpTypeVector %int 2
5262 %25 = OpConstantComposite %v2int %int_0 %int_0
5263 %v4float = OpTypeVector %float 4
5264 %uint_0 = OpConstant %uint 0
5265 %_ptr_Uniform_float = OpTypePointer Uniform %float
5266 %main = OpFunction %void None %3
5267 %5 = OpLabel
5268 %19 = OpAccessChain %_ptr_Uniform_uint %sbo %int_0
5269 %20 = OpLoad %uint %19
5270 %22 = OpAccessChain %_ptr_UniformConstant_13 %images %20
5271 %23 = OpLoad %13 %22
5272 %27 = OpImageRead %v4float %23 %25
5273 %29 = OpCompositeExtract %float %27 0
5274 %31 = OpAccessChain %_ptr_Uniform_float %sbo %int_1
5275 OpStore %31 %29
5276 OpReturn
5277 OpFunctionEnd
5278 )";
5279
5280 const std::string func_after =
5281 R"(%7 = OpTypeFunction %void
5282 %uint = OpTypeInt 32 0
5283 %float = OpTypeFloat 32
5284 %StorageBuffer = OpTypeStruct %uint %float
5285 %_ptr_Uniform_StorageBuffer = OpTypePointer Uniform %StorageBuffer
5286 %sbo = OpVariable %_ptr_Uniform_StorageBuffer Uniform
5287 %int = OpTypeInt 32 1
5288 %int_1 = OpConstant %int 1
5289 %13 = OpTypeImage %float 2D 0 0 0 2 Rgba32f
5290 %_runtimearr_13 = OpTypeRuntimeArray %13
5291 %_ptr_UniformConstant__runtimearr_13 = OpTypePointer UniformConstant %_runtimearr_13
5292 %images = OpVariable %_ptr_UniformConstant__runtimearr_13 UniformConstant
5293 %int_0 = OpConstant %int 0
5294 %_ptr_Uniform_uint = OpTypePointer Uniform %uint
5295 %_ptr_UniformConstant_13 = OpTypePointer UniformConstant %13
5296 %v2int = OpTypeVector %int 2
5297 %20 = OpConstantComposite %v2int %int_0 %int_0
5298 %v4float = OpTypeVector %float 4
5299 %uint_0 = OpConstant %uint 0
5300 %_ptr_Uniform_float = OpTypePointer Uniform %float
5301 %uint_1 = OpConstant %uint 1
5302 %34 = OpTypeFunction %uint %uint %uint
5303 %_runtimearr_uint = OpTypeRuntimeArray %uint
5304 %_struct_39 = OpTypeStruct %_runtimearr_uint
5305 %_ptr_StorageBuffer__struct_39 = OpTypePointer StorageBuffer %_struct_39
5306 %41 = OpVariable %_ptr_StorageBuffer__struct_39 StorageBuffer
5307 %_ptr_StorageBuffer_uint = OpTypePointer StorageBuffer %uint
5308 %bool = OpTypeBool
5309 %57 = OpTypeFunction %void %uint %uint %uint %uint
5310 %_struct_63 = OpTypeStruct %uint %_runtimearr_uint
5311 %_ptr_StorageBuffer__struct_63 = OpTypePointer StorageBuffer %_struct_63
5312 %65 = OpVariable %_ptr_StorageBuffer__struct_63 StorageBuffer
5313 %uint_10 = OpConstant %uint 10
5314 %uint_4 = OpConstant %uint 4
5315 %uint_23 = OpConstant %uint 23
5316 %uint_2 = OpConstant %uint 2
5317 %uint_5314 = OpConstant %uint 5314
5318 %uint_3 = OpConstant %uint 3
5319 %v3uint = OpTypeVector %uint 3
5320 %_ptr_Input_v3uint = OpTypePointer Input %v3uint
5321 %89 = OpVariable %_ptr_Input_v3uint Input
5322 %uint_5 = OpConstant %uint 5
5323 %uint_6 = OpConstant %uint 6
5324 %uint_7 = OpConstant %uint 7
5325 %uint_8 = OpConstant %uint 8
5326 %uint_9 = OpConstant %uint 9
5327 %uint_51 = OpConstant %uint 51
5328 %113 = OpConstantNull %v4float
5329 %116 = OpTypeFunction %uint %uint %uint %uint %uint
5330 %uint_48 = OpConstant %uint 48
5331 %141 = OpConstantNull %uint
5332 %uint_54 = OpConstant %uint 54
5333 %main = OpFunction %void None %7
5334 %24 = OpLabel
5335 %25 = OpAccessChain %_ptr_Uniform_uint %sbo %int_0
5336 %133 = OpFunctionCall %uint %115 %uint_0 %uint_0 %uint_0 %uint_0
5337 %134 = OpULessThan %bool %uint_0 %133
5338 OpSelectionMerge %135 None
5339 OpBranchConditional %134 %136 %137
5340 %136 = OpLabel
5341 %138 = OpLoad %uint %25
5342 OpBranch %135
5343 %137 = OpLabel
5344 %140 = OpFunctionCall %void %56 %uint_48 %uint_1 %uint_0 %uint_0
5345 OpBranch %135
5346 %135 = OpLabel
5347 %142 = OpPhi %uint %138 %136 %141 %137
5348 %27 = OpAccessChain %_ptr_UniformConstant_13 %images %142
5349 %28 = OpLoad %13 %27
5350 %48 = OpFunctionCall %uint %33 %uint_1 %uint_1
5351 %50 = OpULessThan %bool %142 %48
5352 OpSelectionMerge %51 None
5353 OpBranchConditional %50 %52 %53
5354 %52 = OpLabel
5355 %54 = OpLoad %13 %27
5356 %143 = OpFunctionCall %uint %115 %uint_0 %uint_0 %uint_1 %142
5357 %144 = OpULessThan %bool %uint_0 %143
5358 OpSelectionMerge %145 None
5359 OpBranchConditional %144 %146 %147
5360 %146 = OpLabel
5361 %148 = OpLoad %13 %27
5362 %149 = OpImageRead %v4float %148 %20
5363 OpBranch %145
5364 %147 = OpLabel
5365 %150 = OpFunctionCall %void %56 %uint_51 %uint_1 %142 %uint_0
5366 OpBranch %145
5367 %145 = OpLabel
5368 %151 = OpPhi %v4float %149 %146 %113 %147
5369 OpBranch %51
5370 %53 = OpLabel
5371 %112 = OpFunctionCall %void %56 %uint_51 %uint_0 %142 %48
5372 OpBranch %51
5373 %51 = OpLabel
5374 %114 = OpPhi %v4float %151 %145 %113 %53
5375 %30 = OpCompositeExtract %float %114 0
5376 %31 = OpAccessChain %_ptr_Uniform_float %sbo %int_1
5377 %152 = OpFunctionCall %uint %115 %uint_0 %uint_0 %uint_0 %uint_0
5378 %153 = OpULessThan %bool %uint_0 %152
5379 OpSelectionMerge %154 None
5380 OpBranchConditional %153 %155 %156
5381 %155 = OpLabel
5382 OpStore %31 %30
5383 OpBranch %154
5384 %156 = OpLabel
5385 %158 = OpFunctionCall %void %56 %uint_54 %uint_1 %uint_0 %uint_0
5386 OpBranch %154
5387 %154 = OpLabel
5388 OpReturn
5389 OpFunctionEnd
5390 )";
5391
5392 const std::string new_funcs =
5393 R"(%33 = OpFunction %uint None %34
5394 %35 = OpFunctionParameter %uint
5395 %36 = OpFunctionParameter %uint
5396 %37 = OpLabel
5397 %43 = OpAccessChain %_ptr_StorageBuffer_uint %41 %uint_0 %35
5398 %44 = OpLoad %uint %43
5399 %45 = OpIAdd %uint %44 %36
5400 %46 = OpAccessChain %_ptr_StorageBuffer_uint %41 %uint_0 %45
5401 %47 = OpLoad %uint %46
5402 OpReturnValue %47
5403 OpFunctionEnd
5404 %56 = OpFunction %void None %57
5405 %58 = OpFunctionParameter %uint
5406 %59 = OpFunctionParameter %uint
5407 %60 = OpFunctionParameter %uint
5408 %61 = OpFunctionParameter %uint
5409 %62 = OpLabel
5410 %66 = OpAccessChain %_ptr_StorageBuffer_uint %65 %uint_0
5411 %69 = OpAtomicIAdd %uint %66 %uint_4 %uint_0 %uint_10
5412 %70 = OpIAdd %uint %69 %uint_10
5413 %71 = OpArrayLength %uint %65 1
5414 %72 = OpULessThanEqual %bool %70 %71
5415 OpSelectionMerge %73 None
5416 OpBranchConditional %72 %74 %73
5417 %74 = OpLabel
5418 %75 = OpIAdd %uint %69 %uint_0
5419 %76 = OpAccessChain %_ptr_StorageBuffer_uint %65 %uint_1 %75
5420 OpStore %76 %uint_10
5421 %78 = OpIAdd %uint %69 %uint_1
5422 %79 = OpAccessChain %_ptr_StorageBuffer_uint %65 %uint_1 %78
5423 OpStore %79 %uint_23
5424 %81 = OpIAdd %uint %69 %uint_2
5425 %82 = OpAccessChain %_ptr_StorageBuffer_uint %65 %uint_1 %81
5426 OpStore %82 %58
5427 %85 = OpIAdd %uint %69 %uint_3
5428 %86 = OpAccessChain %_ptr_StorageBuffer_uint %65 %uint_1 %85
5429 OpStore %86 %uint_5314
5430 %90 = OpLoad %v3uint %89
5431 %91 = OpCompositeExtract %uint %90 0
5432 %92 = OpCompositeExtract %uint %90 1
5433 %93 = OpCompositeExtract %uint %90 2
5434 %94 = OpIAdd %uint %69 %uint_4
5435 %95 = OpAccessChain %_ptr_StorageBuffer_uint %65 %uint_1 %94
5436 OpStore %95 %91
5437 %97 = OpIAdd %uint %69 %uint_5
5438 %98 = OpAccessChain %_ptr_StorageBuffer_uint %65 %uint_1 %97
5439 OpStore %98 %92
5440 %100 = OpIAdd %uint %69 %uint_6
5441 %101 = OpAccessChain %_ptr_StorageBuffer_uint %65 %uint_1 %100
5442 OpStore %101 %93
5443 %103 = OpIAdd %uint %69 %uint_7
5444 %104 = OpAccessChain %_ptr_StorageBuffer_uint %65 %uint_1 %103
5445 OpStore %104 %59
5446 %106 = OpIAdd %uint %69 %uint_8
5447 %107 = OpAccessChain %_ptr_StorageBuffer_uint %65 %uint_1 %106
5448 OpStore %107 %60
5449 %109 = OpIAdd %uint %69 %uint_9
5450 %110 = OpAccessChain %_ptr_StorageBuffer_uint %65 %uint_1 %109
5451 OpStore %110 %61
5452 OpBranch %73
5453 %73 = OpLabel
5454 OpReturn
5455 OpFunctionEnd
5456 %115 = OpFunction %uint None %116
5457 %117 = OpFunctionParameter %uint
5458 %118 = OpFunctionParameter %uint
5459 %119 = OpFunctionParameter %uint
5460 %120 = OpFunctionParameter %uint
5461 %121 = OpLabel
5462 %122 = OpAccessChain %_ptr_StorageBuffer_uint %41 %uint_0 %117
5463 %123 = OpLoad %uint %122
5464 %124 = OpIAdd %uint %123 %118
5465 %125 = OpAccessChain %_ptr_StorageBuffer_uint %41 %uint_0 %124
5466 %126 = OpLoad %uint %125
5467 %127 = OpIAdd %uint %126 %119
5468 %128 = OpAccessChain %_ptr_StorageBuffer_uint %41 %uint_0 %127
5469 %129 = OpLoad %uint %128
5470 %130 = OpIAdd %uint %129 %120
5471 %131 = OpAccessChain %_ptr_StorageBuffer_uint %41 %uint_0 %130
5472 %132 = OpLoad %uint %131
5473 OpReturnValue %132
5474 OpFunctionEnd
5475 )";
5476
5477 // SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
5478 SinglePassRunAndCheck<InstBindlessCheckPass>(
5479 defs_before + func_before, defs_after + func_after + new_funcs, true,
5480 true, 7u, 23u, true, true);
5481 }
5482
5483 TEST_F(InstBindlessTest,
5484 InstBoundsAnyHitInitLoadVariableSizedSampledImagesArray) {
5485 // #version 460
5486 // #extension GL_EXT_nonuniform_qualifier : require
5487 // #extension GL_NV_ray_tracing : require
5488 //
5489 // layout(set = 0, binding = 0, std140) buffer StorageBuffer {
5490 // uint index;
5491 // float red;
5492 // } sbo;
5493 //
5494 // layout(set = 0, binding = 1, rgba32f) readonly uniform image2D images[];
5495 //
5496 // void main()
5497 // {
5498 // sbo.red = imageLoad(images[sbo.index], ivec2(0, 0)).r;
5499 // }
5500
5501 const std::string defs_before =
5502 R"(OpCapability RuntimeDescriptorArray
5503 OpCapability RayTracingNV
5504 OpExtension "SPV_EXT_descriptor_indexing"
5505 OpExtension "SPV_NV_ray_tracing"
5506 %1 = OpExtInstImport "GLSL.std.450"
5507 OpMemoryModel Logical GLSL450
5508 OpEntryPoint AnyHitNV %main "main"
5509 OpSource GLSL 460
5510 OpSourceExtension "GL_EXT_nonuniform_qualifier"
5511 OpSourceExtension "GL_NV_ray_tracing"
5512 OpName %main "main"
5513 OpName %StorageBuffer "StorageBuffer"
5514 OpMemberName %StorageBuffer 0 "index"
5515 OpMemberName %StorageBuffer 1 "red"
5516 OpName %sbo "sbo"
5517 OpName %images "images"
5518 OpMemberDecorate %StorageBuffer 0 Offset 0
5519 OpMemberDecorate %StorageBuffer 1 Offset 4
5520 OpDecorate %StorageBuffer BufferBlock
5521 OpDecorate %sbo DescriptorSet 0
5522 OpDecorate %sbo Binding 0
5523 OpDecorate %images DescriptorSet 0
5524 OpDecorate %images Binding 1
5525 OpDecorate %images NonWritable
5526 %void = OpTypeVoid
5527 )";
5528
5529 const std::string defs_after =
5530 R"(OpCapability RuntimeDescriptorArray
5531 OpCapability RayTracingNV
5532 OpExtension "SPV_EXT_descriptor_indexing"
5533 OpExtension "SPV_NV_ray_tracing"
5534 OpExtension "SPV_KHR_storage_buffer_storage_class"
5535 %1 = OpExtInstImport "GLSL.std.450"
5536 OpMemoryModel Logical GLSL450
5537 OpEntryPoint AnyHitNV %main "main" %89
5538 OpSource GLSL 460
5539 OpSourceExtension "GL_EXT_nonuniform_qualifier"
5540 OpSourceExtension "GL_NV_ray_tracing"
5541 OpName %main "main"
5542 OpName %StorageBuffer "StorageBuffer"
5543 OpMemberName %StorageBuffer 0 "index"
5544 OpMemberName %StorageBuffer 1 "red"
5545 OpName %sbo "sbo"
5546 OpName %images "images"
5547 OpMemberDecorate %StorageBuffer 0 Offset 0
5548 OpMemberDecorate %StorageBuffer 1 Offset 4
5549 OpDecorate %StorageBuffer BufferBlock
5550 OpDecorate %sbo DescriptorSet 0
5551 OpDecorate %sbo Binding 0
5552 OpDecorate %images DescriptorSet 0
5553 OpDecorate %images Binding 1
5554 OpDecorate %images NonWritable
5555 OpDecorate %_runtimearr_uint ArrayStride 4
5556 OpDecorate %_struct_39 Block
5557 OpMemberDecorate %_struct_39 0 Offset 0
5558 OpDecorate %41 DescriptorSet 7
5559 OpDecorate %41 Binding 1
5560 OpDecorate %_struct_63 Block
5561 OpMemberDecorate %_struct_63 0 Offset 0
5562 OpMemberDecorate %_struct_63 1 Offset 4
5563 OpDecorate %65 DescriptorSet 7
5564 OpDecorate %65 Binding 0
5565 OpDecorate %89 BuiltIn LaunchIdNV
5566 %void = OpTypeVoid
5567 )";
5568
5569 const std::string func_before =
5570 R"(%3 = OpTypeFunction %void
5571 %uint = OpTypeInt 32 0
5572 %float = OpTypeFloat 32
5573 %StorageBuffer = OpTypeStruct %uint %float
5574 %_ptr_Uniform_StorageBuffer = OpTypePointer Uniform %StorageBuffer
5575 %sbo = OpVariable %_ptr_Uniform_StorageBuffer Uniform
5576 %int = OpTypeInt 32 1
5577 %int_1 = OpConstant %int 1
5578 %13 = OpTypeImage %float 2D 0 0 0 2 Rgba32f
5579 %_runtimearr_13 = OpTypeRuntimeArray %13
5580 %_ptr_UniformConstant__runtimearr_13 = OpTypePointer UniformConstant %_runtimearr_13
5581 %images = OpVariable %_ptr_UniformConstant__runtimearr_13 UniformConstant
5582 %int_0 = OpConstant %int 0
5583 %_ptr_Uniform_uint = OpTypePointer Uniform %uint
5584 %_ptr_UniformConstant_13 = OpTypePointer UniformConstant %13
5585 %v2int = OpTypeVector %int 2
5586 %25 = OpConstantComposite %v2int %int_0 %int_0
5587 %v4float = OpTypeVector %float 4
5588 %uint_0 = OpConstant %uint 0
5589 %_ptr_Uniform_float = OpTypePointer Uniform %float
5590 %main = OpFunction %void None %3
5591 %5 = OpLabel
5592 %19 = OpAccessChain %_ptr_Uniform_uint %sbo %int_0
5593 %20 = OpLoad %uint %19
5594 %22 = OpAccessChain %_ptr_UniformConstant_13 %images %20
5595 %23 = OpLoad %13 %22
5596 %27 = OpImageRead %v4float %23 %25
5597 %29 = OpCompositeExtract %float %27 0
5598 %31 = OpAccessChain %_ptr_Uniform_float %sbo %int_1
5599 OpStore %31 %29
5600 OpReturn
5601 OpFunctionEnd
5602 )";
5603
5604 const std::string func_after =
5605 R"(%7 = OpTypeFunction %void
5606 %uint = OpTypeInt 32 0
5607 %float = OpTypeFloat 32
5608 %StorageBuffer = OpTypeStruct %uint %float
5609 %_ptr_Uniform_StorageBuffer = OpTypePointer Uniform %StorageBuffer
5610 %sbo = OpVariable %_ptr_Uniform_StorageBuffer Uniform
5611 %int = OpTypeInt 32 1
5612 %int_1 = OpConstant %int 1
5613 %13 = OpTypeImage %float 2D 0 0 0 2 Rgba32f
5614 %_runtimearr_13 = OpTypeRuntimeArray %13
5615 %_ptr_UniformConstant__runtimearr_13 = OpTypePointer UniformConstant %_runtimearr_13
5616 %images = OpVariable %_ptr_UniformConstant__runtimearr_13 UniformConstant
5617 %int_0 = OpConstant %int 0
5618 %_ptr_Uniform_uint = OpTypePointer Uniform %uint
5619 %_ptr_UniformConstant_13 = OpTypePointer UniformConstant %13
5620 %v2int = OpTypeVector %int 2
5621 %20 = OpConstantComposite %v2int %int_0 %int_0
5622 %v4float = OpTypeVector %float 4
5623 %uint_0 = OpConstant %uint 0
5624 %_ptr_Uniform_float = OpTypePointer Uniform %float
5625 %uint_1 = OpConstant %uint 1
5626 %34 = OpTypeFunction %uint %uint %uint
5627 %_runtimearr_uint = OpTypeRuntimeArray %uint
5628 %_struct_39 = OpTypeStruct %_runtimearr_uint
5629 %_ptr_StorageBuffer__struct_39 = OpTypePointer StorageBuffer %_struct_39
5630 %41 = OpVariable %_ptr_StorageBuffer__struct_39 StorageBuffer
5631 %_ptr_StorageBuffer_uint = OpTypePointer StorageBuffer %uint
5632 %bool = OpTypeBool
5633 %57 = OpTypeFunction %void %uint %uint %uint %uint
5634 %_struct_63 = OpTypeStruct %uint %_runtimearr_uint
5635 %_ptr_StorageBuffer__struct_63 = OpTypePointer StorageBuffer %_struct_63
5636 %65 = OpVariable %_ptr_StorageBuffer__struct_63 StorageBuffer
5637 %uint_10 = OpConstant %uint 10
5638 %uint_4 = OpConstant %uint 4
5639 %uint_23 = OpConstant %uint 23
5640 %uint_2 = OpConstant %uint 2
5641 %uint_5315 = OpConstant %uint 5315
5642 %uint_3 = OpConstant %uint 3
5643 %v3uint = OpTypeVector %uint 3
5644 %_ptr_Input_v3uint = OpTypePointer Input %v3uint
5645 %89 = OpVariable %_ptr_Input_v3uint Input
5646 %uint_5 = OpConstant %uint 5
5647 %uint_6 = OpConstant %uint 6
5648 %uint_7 = OpConstant %uint 7
5649 %uint_8 = OpConstant %uint 8
5650 %uint_9 = OpConstant %uint 9
5651 %uint_51 = OpConstant %uint 51
5652 %113 = OpConstantNull %v4float
5653 %116 = OpTypeFunction %uint %uint %uint %uint %uint
5654 %uint_48 = OpConstant %uint 48
5655 %141 = OpConstantNull %uint
5656 %uint_54 = OpConstant %uint 54
5657 %main = OpFunction %void None %7
5658 %24 = OpLabel
5659 %25 = OpAccessChain %_ptr_Uniform_uint %sbo %int_0
5660 %133 = OpFunctionCall %uint %115 %uint_0 %uint_0 %uint_0 %uint_0
5661 %134 = OpULessThan %bool %uint_0 %133
5662 OpSelectionMerge %135 None
5663 OpBranchConditional %134 %136 %137
5664 %136 = OpLabel
5665 %138 = OpLoad %uint %25
5666 OpBranch %135
5667 %137 = OpLabel
5668 %140 = OpFunctionCall %void %56 %uint_48 %uint_1 %uint_0 %uint_0
5669 OpBranch %135
5670 %135 = OpLabel
5671 %142 = OpPhi %uint %138 %136 %141 %137
5672 %27 = OpAccessChain %_ptr_UniformConstant_13 %images %142
5673 %28 = OpLoad %13 %27
5674 %48 = OpFunctionCall %uint %33 %uint_1 %uint_1
5675 %50 = OpULessThan %bool %142 %48
5676 OpSelectionMerge %51 None
5677 OpBranchConditional %50 %52 %53
5678 %52 = OpLabel
5679 %54 = OpLoad %13 %27
5680 %143 = OpFunctionCall %uint %115 %uint_0 %uint_0 %uint_1 %142
5681 %144 = OpULessThan %bool %uint_0 %143
5682 OpSelectionMerge %145 None
5683 OpBranchConditional %144 %146 %147
5684 %146 = OpLabel
5685 %148 = OpLoad %13 %27
5686 %149 = OpImageRead %v4float %148 %20
5687 OpBranch %145
5688 %147 = OpLabel
5689 %150 = OpFunctionCall %void %56 %uint_51 %uint_1 %142 %uint_0
5690 OpBranch %145
5691 %145 = OpLabel
5692 %151 = OpPhi %v4float %149 %146 %113 %147
5693 OpBranch %51
5694 %53 = OpLabel
5695 %112 = OpFunctionCall %void %56 %uint_51 %uint_0 %142 %48
5696 OpBranch %51
5697 %51 = OpLabel
5698 %114 = OpPhi %v4float %151 %145 %113 %53
5699 %30 = OpCompositeExtract %float %114 0
5700 %31 = OpAccessChain %_ptr_Uniform_float %sbo %int_1
5701 %152 = OpFunctionCall %uint %115 %uint_0 %uint_0 %uint_0 %uint_0
5702 %153 = OpULessThan %bool %uint_0 %152
5703 OpSelectionMerge %154 None
5704 OpBranchConditional %153 %155 %156
5705 %155 = OpLabel
5706 OpStore %31 %30
5707 OpBranch %154
5708 %156 = OpLabel
5709 %158 = OpFunctionCall %void %56 %uint_54 %uint_1 %uint_0 %uint_0
5710 OpBranch %154
5711 %154 = OpLabel
5712 OpReturn
5713 OpFunctionEnd
5714 )";
5715
5716 const std::string new_funcs =
5717 R"(%33 = OpFunction %uint None %34
5718 %35 = OpFunctionParameter %uint
5719 %36 = OpFunctionParameter %uint
5720 %37 = OpLabel
5721 %43 = OpAccessChain %_ptr_StorageBuffer_uint %41 %uint_0 %35
5722 %44 = OpLoad %uint %43
5723 %45 = OpIAdd %uint %44 %36
5724 %46 = OpAccessChain %_ptr_StorageBuffer_uint %41 %uint_0 %45
5725 %47 = OpLoad %uint %46
5726 OpReturnValue %47
5727 OpFunctionEnd
5728 %56 = OpFunction %void None %57
5729 %58 = OpFunctionParameter %uint
5730 %59 = OpFunctionParameter %uint
5731 %60 = OpFunctionParameter %uint
5732 %61 = OpFunctionParameter %uint
5733 %62 = OpLabel
5734 %66 = OpAccessChain %_ptr_StorageBuffer_uint %65 %uint_0
5735 %69 = OpAtomicIAdd %uint %66 %uint_4 %uint_0 %uint_10
5736 %70 = OpIAdd %uint %69 %uint_10
5737 %71 = OpArrayLength %uint %65 1
5738 %72 = OpULessThanEqual %bool %70 %71
5739 OpSelectionMerge %73 None
5740 OpBranchConditional %72 %74 %73
5741 %74 = OpLabel
5742 %75 = OpIAdd %uint %69 %uint_0
5743 %76 = OpAccessChain %_ptr_StorageBuffer_uint %65 %uint_1 %75
5744 OpStore %76 %uint_10
5745 %78 = OpIAdd %uint %69 %uint_1
5746 %79 = OpAccessChain %_ptr_StorageBuffer_uint %65 %uint_1 %78
5747 OpStore %79 %uint_23
5748 %81 = OpIAdd %uint %69 %uint_2
5749 %82 = OpAccessChain %_ptr_StorageBuffer_uint %65 %uint_1 %81
5750 OpStore %82 %58
5751 %85 = OpIAdd %uint %69 %uint_3
5752 %86 = OpAccessChain %_ptr_StorageBuffer_uint %65 %uint_1 %85
5753 OpStore %86 %uint_5315
5754 %90 = OpLoad %v3uint %89
5755 %91 = OpCompositeExtract %uint %90 0
5756 %92 = OpCompositeExtract %uint %90 1
5757 %93 = OpCompositeExtract %uint %90 2
5758 %94 = OpIAdd %uint %69 %uint_4
5759 %95 = OpAccessChain %_ptr_StorageBuffer_uint %65 %uint_1 %94
5760 OpStore %95 %91
5761 %97 = OpIAdd %uint %69 %uint_5
5762 %98 = OpAccessChain %_ptr_StorageBuffer_uint %65 %uint_1 %97
5763 OpStore %98 %92
5764 %100 = OpIAdd %uint %69 %uint_6
5765 %101 = OpAccessChain %_ptr_StorageBuffer_uint %65 %uint_1 %100
5766 OpStore %101 %93
5767 %103 = OpIAdd %uint %69 %uint_7
5768 %104 = OpAccessChain %_ptr_StorageBuffer_uint %65 %uint_1 %103
5769 OpStore %104 %59
5770 %106 = OpIAdd %uint %69 %uint_8
5771 %107 = OpAccessChain %_ptr_StorageBuffer_uint %65 %uint_1 %106
5772 OpStore %107 %60
5773 %109 = OpIAdd %uint %69 %uint_9
5774 %110 = OpAccessChain %_ptr_StorageBuffer_uint %65 %uint_1 %109
5775 OpStore %110 %61
5776 OpBranch %73
5777 %73 = OpLabel
5778 OpReturn
5779 OpFunctionEnd
5780 %115 = OpFunction %uint None %116
5781 %117 = OpFunctionParameter %uint
5782 %118 = OpFunctionParameter %uint
5783 %119 = OpFunctionParameter %uint
5784 %120 = OpFunctionParameter %uint
5785 %121 = OpLabel
5786 %122 = OpAccessChain %_ptr_StorageBuffer_uint %41 %uint_0 %117
5787 %123 = OpLoad %uint %122
5788 %124 = OpIAdd %uint %123 %118
5789 %125 = OpAccessChain %_ptr_StorageBuffer_uint %41 %uint_0 %124
5790 %126 = OpLoad %uint %125
5791 %127 = OpIAdd %uint %126 %119
5792 %128 = OpAccessChain %_ptr_StorageBuffer_uint %41 %uint_0 %127
5793 %129 = OpLoad %uint %128
5794 %130 = OpIAdd %uint %129 %120
5795 %131 = OpAccessChain %_ptr_StorageBuffer_uint %41 %uint_0 %130
5796 %132 = OpLoad %uint %131
5797 OpReturnValue %132
5798 OpFunctionEnd
5799 )";
5800
5801 // SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
5802 SinglePassRunAndCheck<InstBindlessCheckPass>(
5803 defs_before + func_before, defs_after + func_after + new_funcs, true,
5804 true, 7u, 23u, true, true);
5805 }
5806
5807 TEST_F(InstBindlessTest,
5808 InstBoundsClosestHitInitLoadVariableSizedSampledImagesArray) {
5809 // #version 460
5810 // #extension GL_EXT_nonuniform_qualifier : require
5811 // #extension GL_NV_ray_tracing : require
5812 //
5813 // layout(set = 0, binding = 0, std140) buffer StorageBuffer {
5814 // uint index;
5815 // float red;
5816 // } sbo;
5817 //
5818 // layout(set = 0, binding = 1, rgba32f) readonly uniform image2D images[];
5819 //
5820 // void main()
5821 // {
5822 // sbo.red = imageLoad(images[sbo.index], ivec2(0, 0)).r;
5823 // }
5824
5825 const std::string defs_before =
5826 R"(OpCapability RuntimeDescriptorArray
5827 OpCapability RayTracingNV
5828 OpExtension "SPV_EXT_descriptor_indexing"
5829 OpExtension "SPV_NV_ray_tracing"
5830 %1 = OpExtInstImport "GLSL.std.450"
5831 OpMemoryModel Logical GLSL450
5832 OpEntryPoint ClosestHitNV %main "main"
5833 OpSource GLSL 460
5834 OpSourceExtension "GL_EXT_nonuniform_qualifier"
5835 OpSourceExtension "GL_NV_ray_tracing"
5836 OpName %main "main"
5837 OpName %StorageBuffer "StorageBuffer"
5838 OpMemberName %StorageBuffer 0 "index"
5839 OpMemberName %StorageBuffer 1 "red"
5840 OpName %sbo "sbo"
5841 OpName %images "images"
5842 OpMemberDecorate %StorageBuffer 0 Offset 0
5843 OpMemberDecorate %StorageBuffer 1 Offset 4
5844 OpDecorate %StorageBuffer BufferBlock
5845 OpDecorate %sbo DescriptorSet 0
5846 OpDecorate %sbo Binding 0
5847 OpDecorate %images DescriptorSet 0
5848 OpDecorate %images Binding 1
5849 OpDecorate %images NonWritable
5850 %void = OpTypeVoid
5851 )";
5852
5853 const std::string defs_after =
5854 R"(OpCapability RuntimeDescriptorArray
5855 OpCapability RayTracingNV
5856 OpExtension "SPV_EXT_descriptor_indexing"
5857 OpExtension "SPV_NV_ray_tracing"
5858 OpExtension "SPV_KHR_storage_buffer_storage_class"
5859 %1 = OpExtInstImport "GLSL.std.450"
5860 OpMemoryModel Logical GLSL450
5861 OpEntryPoint ClosestHitNV %main "main" %89
5862 OpSource GLSL 460
5863 OpSourceExtension "GL_EXT_nonuniform_qualifier"
5864 OpSourceExtension "GL_NV_ray_tracing"
5865 OpName %main "main"
5866 OpName %StorageBuffer "StorageBuffer"
5867 OpMemberName %StorageBuffer 0 "index"
5868 OpMemberName %StorageBuffer 1 "red"
5869 OpName %sbo "sbo"
5870 OpName %images "images"
5871 OpMemberDecorate %StorageBuffer 0 Offset 0
5872 OpMemberDecorate %StorageBuffer 1 Offset 4
5873 OpDecorate %StorageBuffer BufferBlock
5874 OpDecorate %sbo DescriptorSet 0
5875 OpDecorate %sbo Binding 0
5876 OpDecorate %images DescriptorSet 0
5877 OpDecorate %images Binding 1
5878 OpDecorate %images NonWritable
5879 OpDecorate %_runtimearr_uint ArrayStride 4
5880 OpDecorate %_struct_39 Block
5881 OpMemberDecorate %_struct_39 0 Offset 0
5882 OpDecorate %41 DescriptorSet 7
5883 OpDecorate %41 Binding 1
5884 OpDecorate %_struct_63 Block
5885 OpMemberDecorate %_struct_63 0 Offset 0
5886 OpMemberDecorate %_struct_63 1 Offset 4
5887 OpDecorate %65 DescriptorSet 7
5888 OpDecorate %65 Binding 0
5889 OpDecorate %89 BuiltIn LaunchIdNV
5890 %void = OpTypeVoid
5891 )";
5892
5893 const std::string func_before =
5894 R"(%3 = OpTypeFunction %void
5895 %uint = OpTypeInt 32 0
5896 %float = OpTypeFloat 32
5897 %StorageBuffer = OpTypeStruct %uint %float
5898 %_ptr_Uniform_StorageBuffer = OpTypePointer Uniform %StorageBuffer
5899 %sbo = OpVariable %_ptr_Uniform_StorageBuffer Uniform
5900 %int = OpTypeInt 32 1
5901 %int_1 = OpConstant %int 1
5902 %13 = OpTypeImage %float 2D 0 0 0 2 Rgba32f
5903 %_runtimearr_13 = OpTypeRuntimeArray %13
5904 %_ptr_UniformConstant__runtimearr_13 = OpTypePointer UniformConstant %_runtimearr_13
5905 %images = OpVariable %_ptr_UniformConstant__runtimearr_13 UniformConstant
5906 %int_0 = OpConstant %int 0
5907 %_ptr_Uniform_uint = OpTypePointer Uniform %uint
5908 %_ptr_UniformConstant_13 = OpTypePointer UniformConstant %13
5909 %v2int = OpTypeVector %int 2
5910 %25 = OpConstantComposite %v2int %int_0 %int_0
5911 %v4float = OpTypeVector %float 4
5912 %uint_0 = OpConstant %uint 0
5913 %_ptr_Uniform_float = OpTypePointer Uniform %float
5914 %main = OpFunction %void None %3
5915 %5 = OpLabel
5916 %19 = OpAccessChain %_ptr_Uniform_uint %sbo %int_0
5917 %20 = OpLoad %uint %19
5918 %22 = OpAccessChain %_ptr_UniformConstant_13 %images %20
5919 %23 = OpLoad %13 %22
5920 %27 = OpImageRead %v4float %23 %25
5921 %29 = OpCompositeExtract %float %27 0
5922 %31 = OpAccessChain %_ptr_Uniform_float %sbo %int_1
5923 OpStore %31 %29
5924 OpReturn
5925 OpFunctionEnd
5926 )";
5927
5928 const std::string func_after =
5929 R"(%7 = OpTypeFunction %void
5930 %uint = OpTypeInt 32 0
5931 %float = OpTypeFloat 32
5932 %StorageBuffer = OpTypeStruct %uint %float
5933 %_ptr_Uniform_StorageBuffer = OpTypePointer Uniform %StorageBuffer
5934 %sbo = OpVariable %_ptr_Uniform_StorageBuffer Uniform
5935 %int = OpTypeInt 32 1
5936 %int_1 = OpConstant %int 1
5937 %13 = OpTypeImage %float 2D 0 0 0 2 Rgba32f
5938 %_runtimearr_13 = OpTypeRuntimeArray %13
5939 %_ptr_UniformConstant__runtimearr_13 = OpTypePointer UniformConstant %_runtimearr_13
5940 %images = OpVariable %_ptr_UniformConstant__runtimearr_13 UniformConstant
5941 %int_0 = OpConstant %int 0
5942 %_ptr_Uniform_uint = OpTypePointer Uniform %uint
5943 %_ptr_UniformConstant_13 = OpTypePointer UniformConstant %13
5944 %v2int = OpTypeVector %int 2
5945 %20 = OpConstantComposite %v2int %int_0 %int_0
5946 %v4float = OpTypeVector %float 4
5947 %uint_0 = OpConstant %uint 0
5948 %_ptr_Uniform_float = OpTypePointer Uniform %float
5949 %uint_1 = OpConstant %uint 1
5950 %34 = OpTypeFunction %uint %uint %uint
5951 %_runtimearr_uint = OpTypeRuntimeArray %uint
5952 %_struct_39 = OpTypeStruct %_runtimearr_uint
5953 %_ptr_StorageBuffer__struct_39 = OpTypePointer StorageBuffer %_struct_39
5954 %41 = OpVariable %_ptr_StorageBuffer__struct_39 StorageBuffer
5955 %_ptr_StorageBuffer_uint = OpTypePointer StorageBuffer %uint
5956 %bool = OpTypeBool
5957 %57 = OpTypeFunction %void %uint %uint %uint %uint
5958 %_struct_63 = OpTypeStruct %uint %_runtimearr_uint
5959 %_ptr_StorageBuffer__struct_63 = OpTypePointer StorageBuffer %_struct_63
5960 %65 = OpVariable %_ptr_StorageBuffer__struct_63 StorageBuffer
5961 %uint_10 = OpConstant %uint 10
5962 %uint_4 = OpConstant %uint 4
5963 %uint_23 = OpConstant %uint 23
5964 %uint_2 = OpConstant %uint 2
5965 %uint_5316 = OpConstant %uint 5316
5966 %uint_3 = OpConstant %uint 3
5967 %v3uint = OpTypeVector %uint 3
5968 %_ptr_Input_v3uint = OpTypePointer Input %v3uint
5969 %89 = OpVariable %_ptr_Input_v3uint Input
5970 %uint_5 = OpConstant %uint 5
5971 %uint_6 = OpConstant %uint 6
5972 %uint_7 = OpConstant %uint 7
5973 %uint_8 = OpConstant %uint 8
5974 %uint_9 = OpConstant %uint 9
5975 %uint_51 = OpConstant %uint 51
5976 %113 = OpConstantNull %v4float
5977 %116 = OpTypeFunction %uint %uint %uint %uint %uint
5978 %uint_48 = OpConstant %uint 48
5979 %141 = OpConstantNull %uint
5980 %uint_54 = OpConstant %uint 54
5981 %main = OpFunction %void None %7
5982 %24 = OpLabel
5983 %25 = OpAccessChain %_ptr_Uniform_uint %sbo %int_0
5984 %133 = OpFunctionCall %uint %115 %uint_0 %uint_0 %uint_0 %uint_0
5985 %134 = OpULessThan %bool %uint_0 %133
5986 OpSelectionMerge %135 None
5987 OpBranchConditional %134 %136 %137
5988 %136 = OpLabel
5989 %138 = OpLoad %uint %25
5990 OpBranch %135
5991 %137 = OpLabel
5992 %140 = OpFunctionCall %void %56 %uint_48 %uint_1 %uint_0 %uint_0
5993 OpBranch %135
5994 %135 = OpLabel
5995 %142 = OpPhi %uint %138 %136 %141 %137
5996 %27 = OpAccessChain %_ptr_UniformConstant_13 %images %142
5997 %28 = OpLoad %13 %27
5998 %48 = OpFunctionCall %uint %33 %uint_1 %uint_1
5999 %50 = OpULessThan %bool %142 %48
6000 OpSelectionMerge %51 None
6001 OpBranchConditional %50 %52 %53
6002 %52 = OpLabel
6003 %54 = OpLoad %13 %27
6004 %143 = OpFunctionCall %uint %115 %uint_0 %uint_0 %uint_1 %142
6005 %144 = OpULessThan %bool %uint_0 %143
6006 OpSelectionMerge %145 None
6007 OpBranchConditional %144 %146 %147
6008 %146 = OpLabel
6009 %148 = OpLoad %13 %27
6010 %149 = OpImageRead %v4float %148 %20
6011 OpBranch %145
6012 %147 = OpLabel
6013 %150 = OpFunctionCall %void %56 %uint_51 %uint_1 %142 %uint_0
6014 OpBranch %145
6015 %145 = OpLabel
6016 %151 = OpPhi %v4float %149 %146 %113 %147
6017 OpBranch %51
6018 %53 = OpLabel
6019 %112 = OpFunctionCall %void %56 %uint_51 %uint_0 %142 %48
6020 OpBranch %51
6021 %51 = OpLabel
6022 %114 = OpPhi %v4float %151 %145 %113 %53
6023 %30 = OpCompositeExtract %float %114 0
6024 %31 = OpAccessChain %_ptr_Uniform_float %sbo %int_1
6025 %152 = OpFunctionCall %uint %115 %uint_0 %uint_0 %uint_0 %uint_0
6026 %153 = OpULessThan %bool %uint_0 %152
6027 OpSelectionMerge %154 None
6028 OpBranchConditional %153 %155 %156
6029 %155 = OpLabel
6030 OpStore %31 %30
6031 OpBranch %154
6032 %156 = OpLabel
6033 %158 = OpFunctionCall %void %56 %uint_54 %uint_1 %uint_0 %uint_0
6034 OpBranch %154
6035 %154 = OpLabel
6036 OpReturn
6037 OpFunctionEnd
6038 )";
6039
6040 const std::string new_funcs =
6041 R"(%33 = OpFunction %uint None %34
6042 %35 = OpFunctionParameter %uint
6043 %36 = OpFunctionParameter %uint
6044 %37 = OpLabel
6045 %43 = OpAccessChain %_ptr_StorageBuffer_uint %41 %uint_0 %35
6046 %44 = OpLoad %uint %43
6047 %45 = OpIAdd %uint %44 %36
6048 %46 = OpAccessChain %_ptr_StorageBuffer_uint %41 %uint_0 %45
6049 %47 = OpLoad %uint %46
6050 OpReturnValue %47
6051 OpFunctionEnd
6052 %56 = OpFunction %void None %57
6053 %58 = OpFunctionParameter %uint
6054 %59 = OpFunctionParameter %uint
6055 %60 = OpFunctionParameter %uint
6056 %61 = OpFunctionParameter %uint
6057 %62 = OpLabel
6058 %66 = OpAccessChain %_ptr_StorageBuffer_uint %65 %uint_0
6059 %69 = OpAtomicIAdd %uint %66 %uint_4 %uint_0 %uint_10
6060 %70 = OpIAdd %uint %69 %uint_10
6061 %71 = OpArrayLength %uint %65 1
6062 %72 = OpULessThanEqual %bool %70 %71
6063 OpSelectionMerge %73 None
6064 OpBranchConditional %72 %74 %73
6065 %74 = OpLabel
6066 %75 = OpIAdd %uint %69 %uint_0
6067 %76 = OpAccessChain %_ptr_StorageBuffer_uint %65 %uint_1 %75
6068 OpStore %76 %uint_10
6069 %78 = OpIAdd %uint %69 %uint_1
6070 %79 = OpAccessChain %_ptr_StorageBuffer_uint %65 %uint_1 %78
6071 OpStore %79 %uint_23
6072 %81 = OpIAdd %uint %69 %uint_2
6073 %82 = OpAccessChain %_ptr_StorageBuffer_uint %65 %uint_1 %81
6074 OpStore %82 %58
6075 %85 = OpIAdd %uint %69 %uint_3
6076 %86 = OpAccessChain %_ptr_StorageBuffer_uint %65 %uint_1 %85
6077 OpStore %86 %uint_5316
6078 %90 = OpLoad %v3uint %89
6079 %91 = OpCompositeExtract %uint %90 0
6080 %92 = OpCompositeExtract %uint %90 1
6081 %93 = OpCompositeExtract %uint %90 2
6082 %94 = OpIAdd %uint %69 %uint_4
6083 %95 = OpAccessChain %_ptr_StorageBuffer_uint %65 %uint_1 %94
6084 OpStore %95 %91
6085 %97 = OpIAdd %uint %69 %uint_5
6086 %98 = OpAccessChain %_ptr_StorageBuffer_uint %65 %uint_1 %97
6087 OpStore %98 %92
6088 %100 = OpIAdd %uint %69 %uint_6
6089 %101 = OpAccessChain %_ptr_StorageBuffer_uint %65 %uint_1 %100
6090 OpStore %101 %93
6091 %103 = OpIAdd %uint %69 %uint_7
6092 %104 = OpAccessChain %_ptr_StorageBuffer_uint %65 %uint_1 %103
6093 OpStore %104 %59
6094 %106 = OpIAdd %uint %69 %uint_8
6095 %107 = OpAccessChain %_ptr_StorageBuffer_uint %65 %uint_1 %106
6096 OpStore %107 %60
6097 %109 = OpIAdd %uint %69 %uint_9
6098 %110 = OpAccessChain %_ptr_StorageBuffer_uint %65 %uint_1 %109
6099 OpStore %110 %61
6100 OpBranch %73
6101 %73 = OpLabel
6102 OpReturn
6103 OpFunctionEnd
6104 %115 = OpFunction %uint None %116
6105 %117 = OpFunctionParameter %uint
6106 %118 = OpFunctionParameter %uint
6107 %119 = OpFunctionParameter %uint
6108 %120 = OpFunctionParameter %uint
6109 %121 = OpLabel
6110 %122 = OpAccessChain %_ptr_StorageBuffer_uint %41 %uint_0 %117
6111 %123 = OpLoad %uint %122
6112 %124 = OpIAdd %uint %123 %118
6113 %125 = OpAccessChain %_ptr_StorageBuffer_uint %41 %uint_0 %124
6114 %126 = OpLoad %uint %125
6115 %127 = OpIAdd %uint %126 %119
6116 %128 = OpAccessChain %_ptr_StorageBuffer_uint %41 %uint_0 %127
6117 %129 = OpLoad %uint %128
6118 %130 = OpIAdd %uint %129 %120
6119 %131 = OpAccessChain %_ptr_StorageBuffer_uint %41 %uint_0 %130
6120 %132 = OpLoad %uint %131
6121 OpReturnValue %132
6122 OpFunctionEnd
6123 )";
6124
6125 // SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
6126 SinglePassRunAndCheck<InstBindlessCheckPass>(
6127 defs_before + func_before, defs_after + func_after + new_funcs, true,
6128 true, 7u, 23u, true, true);
6129 }
6130
6131 TEST_F(InstBindlessTest,
6132 InstBoundsMissInitLoadVariableSizedSampledImagesArray) {
6133 // #version 460
6134 // #extension GL_EXT_nonuniform_qualifier : require
6135 // #extension GL_NV_ray_tracing : require
6136 //
6137 // layout(set = 0, binding = 0, std140) buffer StorageBuffer {
6138 // uint index;
6139 // float red;
6140 // } sbo;
6141 //
6142 // layout(set = 0, binding = 1, rgba32f) readonly uniform image2D images[];
6143 //
6144 // void main()
6145 // {
6146 // sbo.red = imageLoad(images[sbo.index], ivec2(0, 0)).r;
6147 // }
6148
6149 const std::string defs_before =
6150 R"(OpCapability RuntimeDescriptorArray
6151 OpCapability RayTracingNV
6152 OpExtension "SPV_EXT_descriptor_indexing"
6153 OpExtension "SPV_NV_ray_tracing"
6154 %1 = OpExtInstImport "GLSL.std.450"
6155 OpMemoryModel Logical GLSL450
6156 OpEntryPoint MissNV %main "main"
6157 OpSource GLSL 460
6158 OpSourceExtension "GL_EXT_nonuniform_qualifier"
6159 OpSourceExtension "GL_NV_ray_tracing"
6160 OpName %main "main"
6161 OpName %StorageBuffer "StorageBuffer"
6162 OpMemberName %StorageBuffer 0 "index"
6163 OpMemberName %StorageBuffer 1 "red"
6164 OpName %sbo "sbo"
6165 OpName %images "images"
6166 OpMemberDecorate %StorageBuffer 0 Offset 0
6167 OpMemberDecorate %StorageBuffer 1 Offset 4
6168 OpDecorate %StorageBuffer BufferBlock
6169 OpDecorate %sbo DescriptorSet 0
6170 OpDecorate %sbo Binding 0
6171 OpDecorate %images DescriptorSet 0
6172 OpDecorate %images Binding 1
6173 OpDecorate %images NonWritable
6174 %void = OpTypeVoid
6175 )";
6176
6177 const std::string defs_after =
6178 R"(OpCapability RuntimeDescriptorArray
6179 OpCapability RayTracingNV
6180 OpExtension "SPV_EXT_descriptor_indexing"
6181 OpExtension "SPV_NV_ray_tracing"
6182 OpExtension "SPV_KHR_storage_buffer_storage_class"
6183 %1 = OpExtInstImport "GLSL.std.450"
6184 OpMemoryModel Logical GLSL450
6185 OpEntryPoint MissNV %main "main" %89
6186 OpSource GLSL 460
6187 OpSourceExtension "GL_EXT_nonuniform_qualifier"
6188 OpSourceExtension "GL_NV_ray_tracing"
6189 OpName %main "main"
6190 OpName %StorageBuffer "StorageBuffer"
6191 OpMemberName %StorageBuffer 0 "index"
6192 OpMemberName %StorageBuffer 1 "red"
6193 OpName %sbo "sbo"
6194 OpName %images "images"
6195 OpMemberDecorate %StorageBuffer 0 Offset 0
6196 OpMemberDecorate %StorageBuffer 1 Offset 4
6197 OpDecorate %StorageBuffer BufferBlock
6198 OpDecorate %sbo DescriptorSet 0
6199 OpDecorate %sbo Binding 0
6200 OpDecorate %images DescriptorSet 0
6201 OpDecorate %images Binding 1
6202 OpDecorate %images NonWritable
6203 OpDecorate %_runtimearr_uint ArrayStride 4
6204 OpDecorate %_struct_39 Block
6205 OpMemberDecorate %_struct_39 0 Offset 0
6206 OpDecorate %41 DescriptorSet 7
6207 OpDecorate %41 Binding 1
6208 OpDecorate %_struct_63 Block
6209 OpMemberDecorate %_struct_63 0 Offset 0
6210 OpMemberDecorate %_struct_63 1 Offset 4
6211 OpDecorate %65 DescriptorSet 7
6212 OpDecorate %65 Binding 0
6213 OpDecorate %89 BuiltIn LaunchIdNV
6214 %void = OpTypeVoid
6215 )";
6216
6217 const std::string func_before =
6218 R"(%3 = OpTypeFunction %void
6219 %uint = OpTypeInt 32 0
6220 %float = OpTypeFloat 32
6221 %StorageBuffer = OpTypeStruct %uint %float
6222 %_ptr_Uniform_StorageBuffer = OpTypePointer Uniform %StorageBuffer
6223 %sbo = OpVariable %_ptr_Uniform_StorageBuffer Uniform
6224 %int = OpTypeInt 32 1
6225 %int_1 = OpConstant %int 1
6226 %13 = OpTypeImage %float 2D 0 0 0 2 Rgba32f
6227 %_runtimearr_13 = OpTypeRuntimeArray %13
6228 %_ptr_UniformConstant__runtimearr_13 = OpTypePointer UniformConstant %_runtimearr_13
6229 %images = OpVariable %_ptr_UniformConstant__runtimearr_13 UniformConstant
6230 %int_0 = OpConstant %int 0
6231 %_ptr_Uniform_uint = OpTypePointer Uniform %uint
6232 %_ptr_UniformConstant_13 = OpTypePointer UniformConstant %13
6233 %v2int = OpTypeVector %int 2
6234 %25 = OpConstantComposite %v2int %int_0 %int_0
6235 %v4float = OpTypeVector %float 4
6236 %uint_0 = OpConstant %uint 0
6237 %_ptr_Uniform_float = OpTypePointer Uniform %float
6238 %main = OpFunction %void None %3
6239 %5 = OpLabel
6240 %19 = OpAccessChain %_ptr_Uniform_uint %sbo %int_0
6241 %20 = OpLoad %uint %19
6242 %22 = OpAccessChain %_ptr_UniformConstant_13 %images %20
6243 %23 = OpLoad %13 %22
6244 %27 = OpImageRead %v4float %23 %25
6245 %29 = OpCompositeExtract %float %27 0
6246 %31 = OpAccessChain %_ptr_Uniform_float %sbo %int_1
6247 OpStore %31 %29
6248 OpReturn
6249 OpFunctionEnd
6250 )";
6251
6252 const std::string func_after =
6253 R"(%7 = OpTypeFunction %void
6254 %uint = OpTypeInt 32 0
6255 %float = OpTypeFloat 32
6256 %StorageBuffer = OpTypeStruct %uint %float
6257 %_ptr_Uniform_StorageBuffer = OpTypePointer Uniform %StorageBuffer
6258 %sbo = OpVariable %_ptr_Uniform_StorageBuffer Uniform
6259 %int = OpTypeInt 32 1
6260 %int_1 = OpConstant %int 1
6261 %13 = OpTypeImage %float 2D 0 0 0 2 Rgba32f
6262 %_runtimearr_13 = OpTypeRuntimeArray %13
6263 %_ptr_UniformConstant__runtimearr_13 = OpTypePointer UniformConstant %_runtimearr_13
6264 %images = OpVariable %_ptr_UniformConstant__runtimearr_13 UniformConstant
6265 %int_0 = OpConstant %int 0
6266 %_ptr_Uniform_uint = OpTypePointer Uniform %uint
6267 %_ptr_UniformConstant_13 = OpTypePointer UniformConstant %13
6268 %v2int = OpTypeVector %int 2
6269 %20 = OpConstantComposite %v2int %int_0 %int_0
6270 %v4float = OpTypeVector %float 4
6271 %uint_0 = OpConstant %uint 0
6272 %_ptr_Uniform_float = OpTypePointer Uniform %float
6273 %uint_1 = OpConstant %uint 1
6274 %34 = OpTypeFunction %uint %uint %uint
6275 %_runtimearr_uint = OpTypeRuntimeArray %uint
6276 %_struct_39 = OpTypeStruct %_runtimearr_uint
6277 %_ptr_StorageBuffer__struct_39 = OpTypePointer StorageBuffer %_struct_39
6278 %41 = OpVariable %_ptr_StorageBuffer__struct_39 StorageBuffer
6279 %_ptr_StorageBuffer_uint = OpTypePointer StorageBuffer %uint
6280 %bool = OpTypeBool
6281 %57 = OpTypeFunction %void %uint %uint %uint %uint
6282 %_struct_63 = OpTypeStruct %uint %_runtimearr_uint
6283 %_ptr_StorageBuffer__struct_63 = OpTypePointer StorageBuffer %_struct_63
6284 %65 = OpVariable %_ptr_StorageBuffer__struct_63 StorageBuffer
6285 %uint_10 = OpConstant %uint 10
6286 %uint_4 = OpConstant %uint 4
6287 %uint_23 = OpConstant %uint 23
6288 %uint_2 = OpConstant %uint 2
6289 %uint_5317 = OpConstant %uint 5317
6290 %uint_3 = OpConstant %uint 3
6291 %v3uint = OpTypeVector %uint 3
6292 %_ptr_Input_v3uint = OpTypePointer Input %v3uint
6293 %89 = OpVariable %_ptr_Input_v3uint Input
6294 %uint_5 = OpConstant %uint 5
6295 %uint_6 = OpConstant %uint 6
6296 %uint_7 = OpConstant %uint 7
6297 %uint_8 = OpConstant %uint 8
6298 %uint_9 = OpConstant %uint 9
6299 %uint_51 = OpConstant %uint 51
6300 %113 = OpConstantNull %v4float
6301 %116 = OpTypeFunction %uint %uint %uint %uint %uint
6302 %uint_48 = OpConstant %uint 48
6303 %141 = OpConstantNull %uint
6304 %uint_54 = OpConstant %uint 54
6305 %main = OpFunction %void None %7
6306 %24 = OpLabel
6307 %25 = OpAccessChain %_ptr_Uniform_uint %sbo %int_0
6308 %133 = OpFunctionCall %uint %115 %uint_0 %uint_0 %uint_0 %uint_0
6309 %134 = OpULessThan %bool %uint_0 %133
6310 OpSelectionMerge %135 None
6311 OpBranchConditional %134 %136 %137
6312 %136 = OpLabel
6313 %138 = OpLoad %uint %25
6314 OpBranch %135
6315 %137 = OpLabel
6316 %140 = OpFunctionCall %void %56 %uint_48 %uint_1 %uint_0 %uint_0
6317 OpBranch %135
6318 %135 = OpLabel
6319 %142 = OpPhi %uint %138 %136 %141 %137
6320 %27 = OpAccessChain %_ptr_UniformConstant_13 %images %142
6321 %28 = OpLoad %13 %27
6322 %48 = OpFunctionCall %uint %33 %uint_1 %uint_1
6323 %50 = OpULessThan %bool %142 %48
6324 OpSelectionMerge %51 None
6325 OpBranchConditional %50 %52 %53
6326 %52 = OpLabel
6327 %54 = OpLoad %13 %27
6328 %143 = OpFunctionCall %uint %115 %uint_0 %uint_0 %uint_1 %142
6329 %144 = OpULessThan %bool %uint_0 %143
6330 OpSelectionMerge %145 None
6331 OpBranchConditional %144 %146 %147
6332 %146 = OpLabel
6333 %148 = OpLoad %13 %27
6334 %149 = OpImageRead %v4float %148 %20
6335 OpBranch %145
6336 %147 = OpLabel
6337 %150 = OpFunctionCall %void %56 %uint_51 %uint_1 %142 %uint_0
6338 OpBranch %145
6339 %145 = OpLabel
6340 %151 = OpPhi %v4float %149 %146 %113 %147
6341 OpBranch %51
6342 %53 = OpLabel
6343 %112 = OpFunctionCall %void %56 %uint_51 %uint_0 %142 %48
6344 OpBranch %51
6345 %51 = OpLabel
6346 %114 = OpPhi %v4float %151 %145 %113 %53
6347 %30 = OpCompositeExtract %float %114 0
6348 %31 = OpAccessChain %_ptr_Uniform_float %sbo %int_1
6349 %152 = OpFunctionCall %uint %115 %uint_0 %uint_0 %uint_0 %uint_0
6350 %153 = OpULessThan %bool %uint_0 %152
6351 OpSelectionMerge %154 None
6352 OpBranchConditional %153 %155 %156
6353 %155 = OpLabel
6354 OpStore %31 %30
6355 OpBranch %154
6356 %156 = OpLabel
6357 %158 = OpFunctionCall %void %56 %uint_54 %uint_1 %uint_0 %uint_0
6358 OpBranch %154
6359 %154 = OpLabel
6360 OpReturn
6361 OpFunctionEnd
6362 )";
6363
6364 const std::string new_funcs =
6365 R"(%33 = OpFunction %uint None %34
6366 %35 = OpFunctionParameter %uint
6367 %36 = OpFunctionParameter %uint
6368 %37 = OpLabel
6369 %43 = OpAccessChain %_ptr_StorageBuffer_uint %41 %uint_0 %35
6370 %44 = OpLoad %uint %43
6371 %45 = OpIAdd %uint %44 %36
6372 %46 = OpAccessChain %_ptr_StorageBuffer_uint %41 %uint_0 %45
6373 %47 = OpLoad %uint %46
6374 OpReturnValue %47
6375 OpFunctionEnd
6376 %56 = OpFunction %void None %57
6377 %58 = OpFunctionParameter %uint
6378 %59 = OpFunctionParameter %uint
6379 %60 = OpFunctionParameter %uint
6380 %61 = OpFunctionParameter %uint
6381 %62 = OpLabel
6382 %66 = OpAccessChain %_ptr_StorageBuffer_uint %65 %uint_0
6383 %69 = OpAtomicIAdd %uint %66 %uint_4 %uint_0 %uint_10
6384 %70 = OpIAdd %uint %69 %uint_10
6385 %71 = OpArrayLength %uint %65 1
6386 %72 = OpULessThanEqual %bool %70 %71
6387 OpSelectionMerge %73 None
6388 OpBranchConditional %72 %74 %73
6389 %74 = OpLabel
6390 %75 = OpIAdd %uint %69 %uint_0
6391 %76 = OpAccessChain %_ptr_StorageBuffer_uint %65 %uint_1 %75
6392 OpStore %76 %uint_10
6393 %78 = OpIAdd %uint %69 %uint_1
6394 %79 = OpAccessChain %_ptr_StorageBuffer_uint %65 %uint_1 %78
6395 OpStore %79 %uint_23
6396 %81 = OpIAdd %uint %69 %uint_2
6397 %82 = OpAccessChain %_ptr_StorageBuffer_uint %65 %uint_1 %81
6398 OpStore %82 %58
6399 %85 = OpIAdd %uint %69 %uint_3
6400 %86 = OpAccessChain %_ptr_StorageBuffer_uint %65 %uint_1 %85
6401 OpStore %86 %uint_5317
6402 %90 = OpLoad %v3uint %89
6403 %91 = OpCompositeExtract %uint %90 0
6404 %92 = OpCompositeExtract %uint %90 1
6405 %93 = OpCompositeExtract %uint %90 2
6406 %94 = OpIAdd %uint %69 %uint_4
6407 %95 = OpAccessChain %_ptr_StorageBuffer_uint %65 %uint_1 %94
6408 OpStore %95 %91
6409 %97 = OpIAdd %uint %69 %uint_5
6410 %98 = OpAccessChain %_ptr_StorageBuffer_uint %65 %uint_1 %97
6411 OpStore %98 %92
6412 %100 = OpIAdd %uint %69 %uint_6
6413 %101 = OpAccessChain %_ptr_StorageBuffer_uint %65 %uint_1 %100
6414 OpStore %101 %93
6415 %103 = OpIAdd %uint %69 %uint_7
6416 %104 = OpAccessChain %_ptr_StorageBuffer_uint %65 %uint_1 %103
6417 OpStore %104 %59
6418 %106 = OpIAdd %uint %69 %uint_8
6419 %107 = OpAccessChain %_ptr_StorageBuffer_uint %65 %uint_1 %106
6420 OpStore %107 %60
6421 %109 = OpIAdd %uint %69 %uint_9
6422 %110 = OpAccessChain %_ptr_StorageBuffer_uint %65 %uint_1 %109
6423 OpStore %110 %61
6424 OpBranch %73
6425 %73 = OpLabel
6426 OpReturn
6427 OpFunctionEnd
6428 %115 = OpFunction %uint None %116
6429 %117 = OpFunctionParameter %uint
6430 %118 = OpFunctionParameter %uint
6431 %119 = OpFunctionParameter %uint
6432 %120 = OpFunctionParameter %uint
6433 %121 = OpLabel
6434 %122 = OpAccessChain %_ptr_StorageBuffer_uint %41 %uint_0 %117
6435 %123 = OpLoad %uint %122
6436 %124 = OpIAdd %uint %123 %118
6437 %125 = OpAccessChain %_ptr_StorageBuffer_uint %41 %uint_0 %124
6438 %126 = OpLoad %uint %125
6439 %127 = OpIAdd %uint %126 %119
6440 %128 = OpAccessChain %_ptr_StorageBuffer_uint %41 %uint_0 %127
6441 %129 = OpLoad %uint %128
6442 %130 = OpIAdd %uint %129 %120
6443 %131 = OpAccessChain %_ptr_StorageBuffer_uint %41 %uint_0 %130
6444 %132 = OpLoad %uint %131
6445 OpReturnValue %132
6446 OpFunctionEnd
6447 )";
6448
6449 // SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
6450 SinglePassRunAndCheck<InstBindlessCheckPass>(
6451 defs_before + func_before, defs_after + func_after + new_funcs, true,
6452 true, 7u, 23u, true, true);
6453 }
6454
6455 TEST_F(InstBindlessTest,
6456 InstBoundsCallableInitLoadVariableSizedSampledImagesArray) {
6457 // #version 460
6458 // #extension GL_EXT_nonuniform_qualifier : require
6459 // #extension GL_NV_ray_tracing : require
6460 //
6461 // layout(set = 0, binding = 0, std140) buffer StorageBuffer {
6462 // uint index;
6463 // float red;
6464 // } sbo;
6465 //
6466 // layout(set = 0, binding = 1, rgba32f) readonly uniform image2D images[];
6467 //
6468 // void main()
6469 // {
6470 // sbo.red = imageLoad(images[sbo.index], ivec2(0, 0)).r;
6471 // }
6472
6473 const std::string defs_before =
6474 R"(OpCapability RuntimeDescriptorArray
6475 OpCapability RayTracingNV
6476 OpExtension "SPV_EXT_descriptor_indexing"
6477 OpExtension "SPV_NV_ray_tracing"
6478 %1 = OpExtInstImport "GLSL.std.450"
6479 OpMemoryModel Logical GLSL450
6480 OpEntryPoint CallableNV %main "main"
6481 OpSource GLSL 460
6482 OpSourceExtension "GL_EXT_nonuniform_qualifier"
6483 OpSourceExtension "GL_NV_ray_tracing"
6484 OpName %main "main"
6485 OpName %StorageBuffer "StorageBuffer"
6486 OpMemberName %StorageBuffer 0 "index"
6487 OpMemberName %StorageBuffer 1 "red"
6488 OpName %sbo "sbo"
6489 OpName %images "images"
6490 OpMemberDecorate %StorageBuffer 0 Offset 0
6491 OpMemberDecorate %StorageBuffer 1 Offset 4
6492 OpDecorate %StorageBuffer BufferBlock
6493 OpDecorate %sbo DescriptorSet 0
6494 OpDecorate %sbo Binding 0
6495 OpDecorate %images DescriptorSet 0
6496 OpDecorate %images Binding 1
6497 OpDecorate %images NonWritable
6498 %void = OpTypeVoid
6499 )";
6500
6501 const std::string defs_after =
6502 R"(OpCapability RuntimeDescriptorArray
6503 OpCapability RayTracingNV
6504 OpExtension "SPV_EXT_descriptor_indexing"
6505 OpExtension "SPV_NV_ray_tracing"
6506 OpExtension "SPV_KHR_storage_buffer_storage_class"
6507 %1 = OpExtInstImport "GLSL.std.450"
6508 OpMemoryModel Logical GLSL450
6509 OpEntryPoint CallableNV %main "main" %89
6510 OpSource GLSL 460
6511 OpSourceExtension "GL_EXT_nonuniform_qualifier"
6512 OpSourceExtension "GL_NV_ray_tracing"
6513 OpName %main "main"
6514 OpName %StorageBuffer "StorageBuffer"
6515 OpMemberName %StorageBuffer 0 "index"
6516 OpMemberName %StorageBuffer 1 "red"
6517 OpName %sbo "sbo"
6518 OpName %images "images"
6519 OpMemberDecorate %StorageBuffer 0 Offset 0
6520 OpMemberDecorate %StorageBuffer 1 Offset 4
6521 OpDecorate %StorageBuffer BufferBlock
6522 OpDecorate %sbo DescriptorSet 0
6523 OpDecorate %sbo Binding 0
6524 OpDecorate %images DescriptorSet 0
6525 OpDecorate %images Binding 1
6526 OpDecorate %images NonWritable
6527 OpDecorate %_runtimearr_uint ArrayStride 4
6528 OpDecorate %_struct_39 Block
6529 OpMemberDecorate %_struct_39 0 Offset 0
6530 OpDecorate %41 DescriptorSet 7
6531 OpDecorate %41 Binding 1
6532 OpDecorate %_struct_63 Block
6533 OpMemberDecorate %_struct_63 0 Offset 0
6534 OpMemberDecorate %_struct_63 1 Offset 4
6535 OpDecorate %65 DescriptorSet 7
6536 OpDecorate %65 Binding 0
6537 OpDecorate %89 BuiltIn LaunchIdNV
6538 %void = OpTypeVoid
6539 )";
6540
6541 const std::string func_before =
6542 R"(%3 = OpTypeFunction %void
6543 %uint = OpTypeInt 32 0
6544 %float = OpTypeFloat 32
6545 %StorageBuffer = OpTypeStruct %uint %float
6546 %_ptr_Uniform_StorageBuffer = OpTypePointer Uniform %StorageBuffer
6547 %sbo = OpVariable %_ptr_Uniform_StorageBuffer Uniform
6548 %int = OpTypeInt 32 1
6549 %int_1 = OpConstant %int 1
6550 %13 = OpTypeImage %float 2D 0 0 0 2 Rgba32f
6551 %_runtimearr_13 = OpTypeRuntimeArray %13
6552 %_ptr_UniformConstant__runtimearr_13 = OpTypePointer UniformConstant %_runtimearr_13
6553 %images = OpVariable %_ptr_UniformConstant__runtimearr_13 UniformConstant
6554 %int_0 = OpConstant %int 0
6555 %_ptr_Uniform_uint = OpTypePointer Uniform %uint
6556 %_ptr_UniformConstant_13 = OpTypePointer UniformConstant %13
6557 %v2int = OpTypeVector %int 2
6558 %25 = OpConstantComposite %v2int %int_0 %int_0
6559 %v4float = OpTypeVector %float 4
6560 %uint_0 = OpConstant %uint 0
6561 %_ptr_Uniform_float = OpTypePointer Uniform %float
6562 %main = OpFunction %void None %3
6563 %5 = OpLabel
6564 %19 = OpAccessChain %_ptr_Uniform_uint %sbo %int_0
6565 %20 = OpLoad %uint %19
6566 %22 = OpAccessChain %_ptr_UniformConstant_13 %images %20
6567 %23 = OpLoad %13 %22
6568 %27 = OpImageRead %v4float %23 %25
6569 %29 = OpCompositeExtract %float %27 0
6570 %31 = OpAccessChain %_ptr_Uniform_float %sbo %int_1
6571 OpStore %31 %29
6572 OpReturn
6573 OpFunctionEnd
6574 )";
6575
6576 const std::string func_after =
6577 R"(%7 = OpTypeFunction %void
6578 %uint = OpTypeInt 32 0
6579 %float = OpTypeFloat 32
6580 %StorageBuffer = OpTypeStruct %uint %float
6581 %_ptr_Uniform_StorageBuffer = OpTypePointer Uniform %StorageBuffer
6582 %sbo = OpVariable %_ptr_Uniform_StorageBuffer Uniform
6583 %int = OpTypeInt 32 1
6584 %int_1 = OpConstant %int 1
6585 %13 = OpTypeImage %float 2D 0 0 0 2 Rgba32f
6586 %_runtimearr_13 = OpTypeRuntimeArray %13
6587 %_ptr_UniformConstant__runtimearr_13 = OpTypePointer UniformConstant %_runtimearr_13
6588 %images = OpVariable %_ptr_UniformConstant__runtimearr_13 UniformConstant
6589 %int_0 = OpConstant %int 0
6590 %_ptr_Uniform_uint = OpTypePointer Uniform %uint
6591 %_ptr_UniformConstant_13 = OpTypePointer UniformConstant %13
6592 %v2int = OpTypeVector %int 2
6593 %20 = OpConstantComposite %v2int %int_0 %int_0
6594 %v4float = OpTypeVector %float 4
6595 %uint_0 = OpConstant %uint 0
6596 %_ptr_Uniform_float = OpTypePointer Uniform %float
6597 %uint_1 = OpConstant %uint 1
6598 %34 = OpTypeFunction %uint %uint %uint
6599 %_runtimearr_uint = OpTypeRuntimeArray %uint
6600 %_struct_39 = OpTypeStruct %_runtimearr_uint
6601 %_ptr_StorageBuffer__struct_39 = OpTypePointer StorageBuffer %_struct_39
6602 %41 = OpVariable %_ptr_StorageBuffer__struct_39 StorageBuffer
6603 %_ptr_StorageBuffer_uint = OpTypePointer StorageBuffer %uint
6604 %bool = OpTypeBool
6605 %57 = OpTypeFunction %void %uint %uint %uint %uint
6606 %_struct_63 = OpTypeStruct %uint %_runtimearr_uint
6607 %_ptr_StorageBuffer__struct_63 = OpTypePointer StorageBuffer %_struct_63
6608 %65 = OpVariable %_ptr_StorageBuffer__struct_63 StorageBuffer
6609 %uint_10 = OpConstant %uint 10
6610 %uint_4 = OpConstant %uint 4
6611 %uint_23 = OpConstant %uint 23
6612 %uint_2 = OpConstant %uint 2
6613 %uint_5318 = OpConstant %uint 5318
6614 %uint_3 = OpConstant %uint 3
6615 %v3uint = OpTypeVector %uint 3
6616 %_ptr_Input_v3uint = OpTypePointer Input %v3uint
6617 %89 = OpVariable %_ptr_Input_v3uint Input
6618 %uint_5 = OpConstant %uint 5
6619 %uint_6 = OpConstant %uint 6
6620 %uint_7 = OpConstant %uint 7
6621 %uint_8 = OpConstant %uint 8
6622 %uint_9 = OpConstant %uint 9
6623 %uint_51 = OpConstant %uint 51
6624 %113 = OpConstantNull %v4float
6625 %116 = OpTypeFunction %uint %uint %uint %uint %uint
6626 %uint_48 = OpConstant %uint 48
6627 %141 = OpConstantNull %uint
6628 %uint_54 = OpConstant %uint 54
6629 %main = OpFunction %void None %7
6630 %24 = OpLabel
6631 %25 = OpAccessChain %_ptr_Uniform_uint %sbo %int_0
6632 %133 = OpFunctionCall %uint %115 %uint_0 %uint_0 %uint_0 %uint_0
6633 %134 = OpULessThan %bool %uint_0 %133
6634 OpSelectionMerge %135 None
6635 OpBranchConditional %134 %136 %137
6636 %136 = OpLabel
6637 %138 = OpLoad %uint %25
6638 OpBranch %135
6639 %137 = OpLabel
6640 %140 = OpFunctionCall %void %56 %uint_48 %uint_1 %uint_0 %uint_0
6641 OpBranch %135
6642 %135 = OpLabel
6643 %142 = OpPhi %uint %138 %136 %141 %137
6644 %27 = OpAccessChain %_ptr_UniformConstant_13 %images %142
6645 %28 = OpLoad %13 %27
6646 %48 = OpFunctionCall %uint %33 %uint_1 %uint_1
6647 %50 = OpULessThan %bool %142 %48
6648 OpSelectionMerge %51 None
6649 OpBranchConditional %50 %52 %53
6650 %52 = OpLabel
6651 %54 = OpLoad %13 %27
6652 %143 = OpFunctionCall %uint %115 %uint_0 %uint_0 %uint_1 %142
6653 %144 = OpULessThan %bool %uint_0 %143
6654 OpSelectionMerge %145 None
6655 OpBranchConditional %144 %146 %147
6656 %146 = OpLabel
6657 %148 = OpLoad %13 %27
6658 %149 = OpImageRead %v4float %148 %20
6659 OpBranch %145
6660 %147 = OpLabel
6661 %150 = OpFunctionCall %void %56 %uint_51 %uint_1 %142 %uint_0
6662 OpBranch %145
6663 %145 = OpLabel
6664 %151 = OpPhi %v4float %149 %146 %113 %147
6665 OpBranch %51
6666 %53 = OpLabel
6667 %112 = OpFunctionCall %void %56 %uint_51 %uint_0 %142 %48
6668 OpBranch %51
6669 %51 = OpLabel
6670 %114 = OpPhi %v4float %151 %145 %113 %53
6671 %30 = OpCompositeExtract %float %114 0
6672 %31 = OpAccessChain %_ptr_Uniform_float %sbo %int_1
6673 %152 = OpFunctionCall %uint %115 %uint_0 %uint_0 %uint_0 %uint_0
6674 %153 = OpULessThan %bool %uint_0 %152
6675 OpSelectionMerge %154 None
6676 OpBranchConditional %153 %155 %156
6677 %155 = OpLabel
6678 OpStore %31 %30
6679 OpBranch %154
6680 %156 = OpLabel
6681 %158 = OpFunctionCall %void %56 %uint_54 %uint_1 %uint_0 %uint_0
6682 OpBranch %154
6683 %154 = OpLabel
6684 OpReturn
6685 OpFunctionEnd
6686 )";
6687
6688 const std::string new_funcs =
6689 R"(%33 = OpFunction %uint None %34
6690 %35 = OpFunctionParameter %uint
6691 %36 = OpFunctionParameter %uint
6692 %37 = OpLabel
6693 %43 = OpAccessChain %_ptr_StorageBuffer_uint %41 %uint_0 %35
6694 %44 = OpLoad %uint %43
6695 %45 = OpIAdd %uint %44 %36
6696 %46 = OpAccessChain %_ptr_StorageBuffer_uint %41 %uint_0 %45
6697 %47 = OpLoad %uint %46
6698 OpReturnValue %47
6699 OpFunctionEnd
6700 %56 = OpFunction %void None %57
6701 %58 = OpFunctionParameter %uint
6702 %59 = OpFunctionParameter %uint
6703 %60 = OpFunctionParameter %uint
6704 %61 = OpFunctionParameter %uint
6705 %62 = OpLabel
6706 %66 = OpAccessChain %_ptr_StorageBuffer_uint %65 %uint_0
6707 %69 = OpAtomicIAdd %uint %66 %uint_4 %uint_0 %uint_10
6708 %70 = OpIAdd %uint %69 %uint_10
6709 %71 = OpArrayLength %uint %65 1
6710 %72 = OpULessThanEqual %bool %70 %71
6711 OpSelectionMerge %73 None
6712 OpBranchConditional %72 %74 %73
6713 %74 = OpLabel
6714 %75 = OpIAdd %uint %69 %uint_0
6715 %76 = OpAccessChain %_ptr_StorageBuffer_uint %65 %uint_1 %75
6716 OpStore %76 %uint_10
6717 %78 = OpIAdd %uint %69 %uint_1
6718 %79 = OpAccessChain %_ptr_StorageBuffer_uint %65 %uint_1 %78
6719 OpStore %79 %uint_23
6720 %81 = OpIAdd %uint %69 %uint_2
6721 %82 = OpAccessChain %_ptr_StorageBuffer_uint %65 %uint_1 %81
6722 OpStore %82 %58
6723 %85 = OpIAdd %uint %69 %uint_3
6724 %86 = OpAccessChain %_ptr_StorageBuffer_uint %65 %uint_1 %85
6725 OpStore %86 %uint_5318
6726 %90 = OpLoad %v3uint %89
6727 %91 = OpCompositeExtract %uint %90 0
6728 %92 = OpCompositeExtract %uint %90 1
6729 %93 = OpCompositeExtract %uint %90 2
6730 %94 = OpIAdd %uint %69 %uint_4
6731 %95 = OpAccessChain %_ptr_StorageBuffer_uint %65 %uint_1 %94
6732 OpStore %95 %91
6733 %97 = OpIAdd %uint %69 %uint_5
6734 %98 = OpAccessChain %_ptr_StorageBuffer_uint %65 %uint_1 %97
6735 OpStore %98 %92
6736 %100 = OpIAdd %uint %69 %uint_6
6737 %101 = OpAccessChain %_ptr_StorageBuffer_uint %65 %uint_1 %100
6738 OpStore %101 %93
6739 %103 = OpIAdd %uint %69 %uint_7
6740 %104 = OpAccessChain %_ptr_StorageBuffer_uint %65 %uint_1 %103
6741 OpStore %104 %59
6742 %106 = OpIAdd %uint %69 %uint_8
6743 %107 = OpAccessChain %_ptr_StorageBuffer_uint %65 %uint_1 %106
6744 OpStore %107 %60
6745 %109 = OpIAdd %uint %69 %uint_9
6746 %110 = OpAccessChain %_ptr_StorageBuffer_uint %65 %uint_1 %109
6747 OpStore %110 %61
6748 OpBranch %73
6749 %73 = OpLabel
6750 OpReturn
6751 OpFunctionEnd
6752 %115 = OpFunction %uint None %116
6753 %117 = OpFunctionParameter %uint
6754 %118 = OpFunctionParameter %uint
6755 %119 = OpFunctionParameter %uint
6756 %120 = OpFunctionParameter %uint
6757 %121 = OpLabel
6758 %122 = OpAccessChain %_ptr_StorageBuffer_uint %41 %uint_0 %117
6759 %123 = OpLoad %uint %122
6760 %124 = OpIAdd %uint %123 %118
6761 %125 = OpAccessChain %_ptr_StorageBuffer_uint %41 %uint_0 %124
6762 %126 = OpLoad %uint %125
6763 %127 = OpIAdd %uint %126 %119
6764 %128 = OpAccessChain %_ptr_StorageBuffer_uint %41 %uint_0 %127
6765 %129 = OpLoad %uint %128
6766 %130 = OpIAdd %uint %129 %120
6767 %131 = OpAccessChain %_ptr_StorageBuffer_uint %41 %uint_0 %130
6768 %132 = OpLoad %uint %131
6769 OpReturnValue %132
6770 OpFunctionEnd
6771 )";
6772
6773 // SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
6774 SinglePassRunAndCheck<InstBindlessCheckPass>(
6775 defs_before + func_before, defs_after + func_after + new_funcs, true,
6776 true, 7u, 23u, true, true);
6777 }
6778
6779 TEST_F(InstBindlessTest, InstBoundsInitSameBlockOpReplication) {
6780 // Test that same block ops like OpSampledImage are replicated properly
6781 // where needed.
6782 //
6783 // clang-format off
6784 //
6785 // #version 450 core
6786 // #extension GL_EXT_nonuniform_qualifier : enable
6787 //
6788 // layout(location = 0) in vec2 inTexcoord;
6789 // layout(location = 0) out vec4 outColor;
6790 //
6791 // layout(set = 0, binding = 0) uniform Uniforms {
6792 // vec2 var0;
6793 // } uniforms;
6794 //
6795 // layout(set = 0, binding = 1) uniform sampler uniformSampler;
6796 // layout(set = 0, binding = 2) uniform texture2D uniformTex;
6797 // layout(set = 0, binding = 3) uniform texture2D uniformTexArr[8];
6798 //
6799 // void main() {
6800 // int index = 0;
6801 // float x = texture(sampler2D(uniformTexArr[nonuniformEXT(index)], uniformSampler), inTexcoord.xy).x;
6802 // float y = texture(sampler2D(uniformTex, uniformSampler), inTexcoord.xy * uniforms.var0.xy).x;
6803 // outColor = vec4(x, y, 0.0, 0.0);
6804 // }
6805 //
6806 // clang-format on
6807
6808 const std::string defs_before =
6809 R"(OpCapability Shader
6810 OpCapability ShaderNonUniformEXT
6811 OpCapability SampledImageArrayNonUniformIndexingEXT
6812 OpExtension "SPV_EXT_descriptor_indexing"
6813 %1 = OpExtInstImport "GLSL.std.450"
6814 OpMemoryModel Logical GLSL450
6815 OpEntryPoint Fragment %main "main" %inTexcoord %outColor
6816 OpExecutionMode %main OriginUpperLeft
6817 OpSource GLSL 450
6818 OpSourceExtension "GL_EXT_nonuniform_qualifier"
6819 OpName %main "main"
6820 OpName %index "index"
6821 OpName %x "x"
6822 OpName %uniformTexArr "uniformTexArr"
6823 OpName %uniformSampler "uniformSampler"
6824 OpName %inTexcoord "inTexcoord"
6825 OpName %y "y"
6826 OpName %uniformTex "uniformTex"
6827 OpName %Uniforms "Uniforms"
6828 OpMemberName %Uniforms 0 "var0"
6829 OpName %uniforms "uniforms"
6830 OpName %outColor "outColor"
6831 OpDecorate %uniformTexArr DescriptorSet 0
6832 OpDecorate %uniformTexArr Binding 3
6833 OpDecorate %19 NonUniformEXT
6834 OpDecorate %22 NonUniformEXT
6835 OpDecorate %uniformSampler DescriptorSet 0
6836 OpDecorate %uniformSampler Binding 1
6837 OpDecorate %inTexcoord Location 0
6838 OpDecorate %uniformTex DescriptorSet 0
6839 OpDecorate %uniformTex Binding 2
6840 OpMemberDecorate %Uniforms 0 Offset 0
6841 OpDecorate %Uniforms Block
6842 OpDecorate %uniforms DescriptorSet 0
6843 OpDecorate %uniforms Binding 0
6844 OpDecorate %outColor Location 0
6845 %void = OpTypeVoid
6846 %3 = OpTypeFunction %void
6847 %int = OpTypeInt 32 1
6848 %_ptr_Function_int = OpTypePointer Function %int
6849 %int_0 = OpConstant %int 0
6850 %float = OpTypeFloat 32
6851 %_ptr_Function_float = OpTypePointer Function %float
6852 %13 = OpTypeImage %float 2D 0 0 0 1 Unknown
6853 %uint = OpTypeInt 32 0
6854 %uint_8 = OpConstant %uint 8
6855 %_arr_13_uint_8 = OpTypeArray %13 %uint_8
6856 %_ptr_UniformConstant__arr_13_uint_8 = OpTypePointer UniformConstant %_arr_13_uint_8
6857 %uniformTexArr = OpVariable %_ptr_UniformConstant__arr_13_uint_8 UniformConstant
6858 %_ptr_UniformConstant_13 = OpTypePointer UniformConstant %13
6859 %23 = OpTypeSampler
6860 %_ptr_UniformConstant_23 = OpTypePointer UniformConstant %23
6861 %uniformSampler = OpVariable %_ptr_UniformConstant_23 UniformConstant
6862 %27 = OpTypeSampledImage %13
6863 %v2float = OpTypeVector %float 2
6864 %_ptr_Input_v2float = OpTypePointer Input %v2float
6865 %inTexcoord = OpVariable %_ptr_Input_v2float Input
6866 %v4float = OpTypeVector %float 4
6867 %uint_0 = OpConstant %uint 0
6868 %uniformTex = OpVariable %_ptr_UniformConstant_13 UniformConstant
6869 %Uniforms = OpTypeStruct %v2float
6870 %_ptr_Uniform_Uniforms = OpTypePointer Uniform %Uniforms
6871 %uniforms = OpVariable %_ptr_Uniform_Uniforms Uniform
6872 %_ptr_Uniform_v2float = OpTypePointer Uniform %v2float
6873 %_ptr_Output_v4float = OpTypePointer Output %v4float
6874 %outColor = OpVariable %_ptr_Output_v4float Output
6875 %float_0 = OpConstant %float 0
6876 )";
6877
6878 const std::string defs_after =
6879 R"(OpCapability Shader
6880 OpCapability ShaderNonUniform
6881 OpCapability SampledImageArrayNonUniformIndexing
6882 OpExtension "SPV_EXT_descriptor_indexing"
6883 OpExtension "SPV_KHR_storage_buffer_storage_class"
6884 %1 = OpExtInstImport "GLSL.std.450"
6885 OpMemoryModel Logical GLSL450
6886 OpEntryPoint Fragment %main "main" %inTexcoord %outColor %gl_FragCoord
6887 OpExecutionMode %main OriginUpperLeft
6888 OpSource GLSL 450
6889 OpSourceExtension "GL_EXT_nonuniform_qualifier"
6890 OpName %main "main"
6891 OpName %index "index"
6892 OpName %x "x"
6893 OpName %uniformTexArr "uniformTexArr"
6894 OpName %uniformSampler "uniformSampler"
6895 OpName %inTexcoord "inTexcoord"
6896 OpName %y "y"
6897 OpName %uniformTex "uniformTex"
6898 OpName %Uniforms "Uniforms"
6899 OpMemberName %Uniforms 0 "var0"
6900 OpName %uniforms "uniforms"
6901 OpName %outColor "outColor"
6902 OpDecorate %uniformTexArr DescriptorSet 0
6903 OpDecorate %uniformTexArr Binding 3
6904 OpDecorate %19 NonUniform
6905 OpDecorate %22 NonUniform
6906 OpDecorate %uniformSampler DescriptorSet 0
6907 OpDecorate %uniformSampler Binding 1
6908 OpDecorate %inTexcoord Location 0
6909 OpDecorate %uniformTex DescriptorSet 0
6910 OpDecorate %uniformTex Binding 2
6911 OpMemberDecorate %Uniforms 0 Offset 0
6912 OpDecorate %Uniforms Block
6913 OpDecorate %uniforms DescriptorSet 0
6914 OpDecorate %uniforms Binding 0
6915 OpDecorate %outColor Location 0
6916 OpDecorate %63 NonUniform
6917 OpDecorate %_runtimearr_uint ArrayStride 4
6918 OpDecorate %_struct_75 Block
6919 OpMemberDecorate %_struct_75 0 Offset 0
6920 OpMemberDecorate %_struct_75 1 Offset 4
6921 OpDecorate %77 DescriptorSet 7
6922 OpDecorate %77 Binding 0
6923 OpDecorate %gl_FragCoord BuiltIn FragCoord
6924 OpDecorate %_struct_132 Block
6925 OpMemberDecorate %_struct_132 0 Offset 0
6926 OpDecorate %134 DescriptorSet 7
6927 OpDecorate %134 Binding 1
6928 OpDecorate %151 NonUniform
6929 %void = OpTypeVoid
6930 %3 = OpTypeFunction %void
6931 %int = OpTypeInt 32 1
6932 %_ptr_Function_int = OpTypePointer Function %int
6933 %int_0 = OpConstant %int 0
6934 %float = OpTypeFloat 32
6935 %_ptr_Function_float = OpTypePointer Function %float
6936 %13 = OpTypeImage %float 2D 0 0 0 1 Unknown
6937 %uint = OpTypeInt 32 0
6938 %uint_8 = OpConstant %uint 8
6939 %_arr_13_uint_8 = OpTypeArray %13 %uint_8
6940 %_ptr_UniformConstant__arr_13_uint_8 = OpTypePointer UniformConstant %_arr_13_uint_8
6941 %uniformTexArr = OpVariable %_ptr_UniformConstant__arr_13_uint_8 UniformConstant
6942 %_ptr_UniformConstant_13 = OpTypePointer UniformConstant %13
6943 %23 = OpTypeSampler
6944 %_ptr_UniformConstant_23 = OpTypePointer UniformConstant %23
6945 %uniformSampler = OpVariable %_ptr_UniformConstant_23 UniformConstant
6946 %27 = OpTypeSampledImage %13
6947 %v2float = OpTypeVector %float 2
6948 %_ptr_Input_v2float = OpTypePointer Input %v2float
6949 %inTexcoord = OpVariable %_ptr_Input_v2float Input
6950 %v4float = OpTypeVector %float 4
6951 %uint_0 = OpConstant %uint 0
6952 %uniformTex = OpVariable %_ptr_UniformConstant_13 UniformConstant
6953 %Uniforms = OpTypeStruct %v2float
6954 %_ptr_Uniform_Uniforms = OpTypePointer Uniform %Uniforms
6955 %uniforms = OpVariable %_ptr_Uniform_Uniforms Uniform
6956 %_ptr_Uniform_v2float = OpTypePointer Uniform %v2float
6957 %_ptr_Output_v4float = OpTypePointer Output %v4float
6958 %outColor = OpVariable %_ptr_Output_v4float Output
6959 %float_0 = OpConstant %float 0
6960 %bool = OpTypeBool
6961 %68 = OpTypeFunction %void %uint %uint %uint %uint
6962 %_runtimearr_uint = OpTypeRuntimeArray %uint
6963 %_struct_75 = OpTypeStruct %uint %_runtimearr_uint
6964 %_ptr_StorageBuffer__struct_75 = OpTypePointer StorageBuffer %_struct_75
6965 %77 = OpVariable %_ptr_StorageBuffer__struct_75 StorageBuffer
6966 %_ptr_StorageBuffer_uint = OpTypePointer StorageBuffer %uint
6967 %uint_10 = OpConstant %uint 10
6968 %uint_4 = OpConstant %uint 4
6969 %uint_1 = OpConstant %uint 1
6970 %uint_23 = OpConstant %uint 23
6971 %uint_2 = OpConstant %uint 2
6972 %uint_3 = OpConstant %uint 3
6973 %_ptr_Input_v4float = OpTypePointer Input %v4float
6974 %gl_FragCoord = OpVariable %_ptr_Input_v4float Input
6975 %v4uint = OpTypeVector %uint 4
6976 %uint_5 = OpConstant %uint 5
6977 %uint_7 = OpConstant %uint 7
6978 %uint_9 = OpConstant %uint 9
6979 %uint_79 = OpConstant %uint 79
6980 %122 = OpConstantNull %v4float
6981 %126 = OpTypeFunction %uint %uint %uint %uint %uint
6982 %_struct_132 = OpTypeStruct %_runtimearr_uint
6983 %_ptr_StorageBuffer__struct_132 = OpTypePointer StorageBuffer %_struct_132
6984 %134 = OpVariable %_ptr_StorageBuffer__struct_132 StorageBuffer
6985 %uint_87 = OpConstant %uint 87
6986 %165 = OpConstantNull %v2float
6987 %uint_89 = OpConstant %uint 89
6988 )";
6989
6990 const std::string func_before =
6991 R"(%main = OpFunction %void None %3
6992 %5 = OpLabel
6993 %index = OpVariable %_ptr_Function_int Function
6994 %x = OpVariable %_ptr_Function_float Function
6995 %y = OpVariable %_ptr_Function_float Function
6996 OpStore %index %int_0
6997 %19 = OpLoad %int %index
6998 %21 = OpAccessChain %_ptr_UniformConstant_13 %uniformTexArr %19
6999 %22 = OpLoad %13 %21
7000 %26 = OpLoad %23 %uniformSampler
7001 %28 = OpSampledImage %27 %22 %26
7002 %32 = OpLoad %v2float %inTexcoord
7003 %34 = OpImageSampleImplicitLod %v4float %28 %32
7004 %36 = OpCompositeExtract %float %34 0
7005 OpStore %x %36
7006 %39 = OpLoad %13 %uniformTex
7007 %40 = OpLoad %23 %uniformSampler
7008 %41 = OpSampledImage %27 %39 %40
7009 %42 = OpLoad %v2float %inTexcoord
7010 %47 = OpAccessChain %_ptr_Uniform_v2float %uniforms %int_0
7011 %48 = OpLoad %v2float %47
7012 %49 = OpFMul %v2float %42 %48
7013 %50 = OpImageSampleImplicitLod %v4float %41 %49
7014 %51 = OpCompositeExtract %float %50 0
7015 OpStore %y %51
7016 %54 = OpLoad %float %x
7017 %55 = OpLoad %float %y
7018 %57 = OpCompositeConstruct %v4float %54 %55 %float_0 %float_0
7019 OpStore %outColor %57
7020 OpReturn
7021 OpFunctionEnd
7022 )";
7023
7024 const std::string func_after =
7025 R"(%main = OpFunction %void None %3
7026 %5 = OpLabel
7027 %index = OpVariable %_ptr_Function_int Function
7028 %x = OpVariable %_ptr_Function_float Function
7029 %y = OpVariable %_ptr_Function_float Function
7030 OpStore %index %int_0
7031 %19 = OpLoad %int %index
7032 %21 = OpAccessChain %_ptr_UniformConstant_13 %uniformTexArr %19
7033 %22 = OpLoad %13 %21
7034 %26 = OpLoad %23 %uniformSampler
7035 %28 = OpSampledImage %27 %22 %26
7036 %32 = OpLoad %v2float %inTexcoord
7037 %59 = OpULessThan %bool %19 %uint_8
7038 OpSelectionMerge %60 None
7039 OpBranchConditional %59 %61 %62
7040 %61 = OpLabel
7041 %63 = OpLoad %13 %21
7042 %64 = OpSampledImage %27 %63 %26
7043 %124 = OpBitcast %uint %19
7044 %146 = OpFunctionCall %uint %125 %uint_0 %uint_0 %uint_3 %124
7045 %147 = OpULessThan %bool %uint_0 %146
7046 OpSelectionMerge %148 None
7047 OpBranchConditional %147 %149 %150
7048 %149 = OpLabel
7049 %151 = OpLoad %13 %21
7050 %152 = OpSampledImage %27 %151 %26
7051 %153 = OpImageSampleImplicitLod %v4float %152 %32
7052 OpBranch %148
7053 %150 = OpLabel
7054 %154 = OpBitcast %uint %19
7055 %155 = OpFunctionCall %void %67 %uint_79 %uint_1 %154 %uint_0
7056 OpBranch %148
7057 %148 = OpLabel
7058 %156 = OpPhi %v4float %153 %149 %122 %150
7059 OpBranch %60
7060 %62 = OpLabel
7061 %66 = OpBitcast %uint %19
7062 %121 = OpFunctionCall %void %67 %uint_79 %uint_0 %66 %uint_8
7063 OpBranch %60
7064 %60 = OpLabel
7065 %123 = OpPhi %v4float %156 %148 %122 %62
7066 %36 = OpCompositeExtract %float %123 0
7067 OpStore %x %36
7068 %39 = OpLoad %13 %uniformTex
7069 %40 = OpLoad %23 %uniformSampler
7070 %41 = OpSampledImage %27 %39 %40
7071 %42 = OpLoad %v2float %inTexcoord
7072 %47 = OpAccessChain %_ptr_Uniform_v2float %uniforms %int_0
7073 %157 = OpFunctionCall %uint %125 %uint_0 %uint_0 %uint_0 %uint_0
7074 %158 = OpULessThan %bool %uint_0 %157
7075 OpSelectionMerge %159 None
7076 OpBranchConditional %158 %160 %161
7077 %160 = OpLabel
7078 %162 = OpLoad %v2float %47
7079 OpBranch %159
7080 %161 = OpLabel
7081 %164 = OpFunctionCall %void %67 %uint_87 %uint_1 %uint_0 %uint_0
7082 OpBranch %159
7083 %159 = OpLabel
7084 %166 = OpPhi %v2float %162 %160 %165 %161
7085 %49 = OpFMul %v2float %42 %166
7086 %167 = OpSampledImage %27 %39 %40
7087 %168 = OpFunctionCall %uint %125 %uint_0 %uint_0 %uint_2 %uint_0
7088 %169 = OpULessThan %bool %uint_0 %168
7089 OpSelectionMerge %170 None
7090 OpBranchConditional %169 %171 %172
7091 %171 = OpLabel
7092 %173 = OpLoad %13 %uniformTex
7093 %174 = OpSampledImage %27 %173 %40
7094 %175 = OpImageSampleImplicitLod %v4float %174 %49
7095 OpBranch %170
7096 %172 = OpLabel
7097 %177 = OpFunctionCall %void %67 %uint_89 %uint_1 %uint_0 %uint_0
7098 OpBranch %170
7099 %170 = OpLabel
7100 %178 = OpPhi %v4float %175 %171 %122 %172
7101 %51 = OpCompositeExtract %float %178 0
7102 OpStore %y %51
7103 %54 = OpLoad %float %x
7104 %55 = OpLoad %float %y
7105 %57 = OpCompositeConstruct %v4float %54 %55 %float_0 %float_0
7106 OpStore %outColor %57
7107 OpReturn
7108 OpFunctionEnd
7109 )";
7110
7111 const std::string new_funcs =
7112 R"(%67 = OpFunction %void None %68
7113 %69 = OpFunctionParameter %uint
7114 %70 = OpFunctionParameter %uint
7115 %71 = OpFunctionParameter %uint
7116 %72 = OpFunctionParameter %uint
7117 %73 = OpLabel
7118 %79 = OpAccessChain %_ptr_StorageBuffer_uint %77 %uint_0
7119 %82 = OpAtomicIAdd %uint %79 %uint_4 %uint_0 %uint_10
7120 %83 = OpIAdd %uint %82 %uint_10
7121 %84 = OpArrayLength %uint %77 1
7122 %85 = OpULessThanEqual %bool %83 %84
7123 OpSelectionMerge %86 None
7124 OpBranchConditional %85 %87 %86
7125 %87 = OpLabel
7126 %88 = OpIAdd %uint %82 %uint_0
7127 %90 = OpAccessChain %_ptr_StorageBuffer_uint %77 %uint_1 %88
7128 OpStore %90 %uint_10
7129 %92 = OpIAdd %uint %82 %uint_1
7130 %93 = OpAccessChain %_ptr_StorageBuffer_uint %77 %uint_1 %92
7131 OpStore %93 %uint_23
7132 %95 = OpIAdd %uint %82 %uint_2
7133 %96 = OpAccessChain %_ptr_StorageBuffer_uint %77 %uint_1 %95
7134 OpStore %96 %69
7135 %98 = OpIAdd %uint %82 %uint_3
7136 %99 = OpAccessChain %_ptr_StorageBuffer_uint %77 %uint_1 %98
7137 OpStore %99 %uint_4
7138 %102 = OpLoad %v4float %gl_FragCoord
7139 %104 = OpBitcast %v4uint %102
7140 %105 = OpCompositeExtract %uint %104 0
7141 %106 = OpIAdd %uint %82 %uint_4
7142 %107 = OpAccessChain %_ptr_StorageBuffer_uint %77 %uint_1 %106
7143 OpStore %107 %105
7144 %108 = OpCompositeExtract %uint %104 1
7145 %110 = OpIAdd %uint %82 %uint_5
7146 %111 = OpAccessChain %_ptr_StorageBuffer_uint %77 %uint_1 %110
7147 OpStore %111 %108
7148 %113 = OpIAdd %uint %82 %uint_7
7149 %114 = OpAccessChain %_ptr_StorageBuffer_uint %77 %uint_1 %113
7150 OpStore %114 %70
7151 %115 = OpIAdd %uint %82 %uint_8
7152 %116 = OpAccessChain %_ptr_StorageBuffer_uint %77 %uint_1 %115
7153 OpStore %116 %71
7154 %118 = OpIAdd %uint %82 %uint_9
7155 %119 = OpAccessChain %_ptr_StorageBuffer_uint %77 %uint_1 %118
7156 OpStore %119 %72
7157 OpBranch %86
7158 %86 = OpLabel
7159 OpReturn
7160 OpFunctionEnd
7161 %125 = OpFunction %uint None %126
7162 %127 = OpFunctionParameter %uint
7163 %128 = OpFunctionParameter %uint
7164 %129 = OpFunctionParameter %uint
7165 %130 = OpFunctionParameter %uint
7166 %131 = OpLabel
7167 %135 = OpAccessChain %_ptr_StorageBuffer_uint %134 %uint_0 %127
7168 %136 = OpLoad %uint %135
7169 %137 = OpIAdd %uint %136 %128
7170 %138 = OpAccessChain %_ptr_StorageBuffer_uint %134 %uint_0 %137
7171 %139 = OpLoad %uint %138
7172 %140 = OpIAdd %uint %139 %129
7173 %141 = OpAccessChain %_ptr_StorageBuffer_uint %134 %uint_0 %140
7174 %142 = OpLoad %uint %141
7175 %143 = OpIAdd %uint %142 %130
7176 %144 = OpAccessChain %_ptr_StorageBuffer_uint %134 %uint_0 %143
7177 %145 = OpLoad %uint %144
7178 OpReturnValue %145
7179 OpFunctionEnd
7180 )";
7181
7182 SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
7183 SinglePassRunAndCheck<InstBindlessCheckPass>(
7184 defs_before + func_before, defs_after + func_after + new_funcs, true,
7185 true, 7u, 23u, true, true);
7186 }
7187
7188 TEST_F(InstBindlessTest, MultipleUniformNonAggregateRefsNoDescInit) {
7189 // Check that uniform refs do not go out-of-bounds. All checks use same input
7190 // buffer read function call result at top of function for uniform buffer
7191 // length. Because descriptor indexing is not being checked, we can avoid one
7192 // buffer load.
7193 //
7194 // Texture2D g_tColor;
7195 // SamplerState g_sAniso;
7196 //
7197 // layout(push_constant) cbuffer PerViewPushConst_t { bool g_B; };
7198 //
7199 // cbuffer PerViewConstantBuffer_t {
7200 // float2 g_TexOff0;
7201 // float2 g_TexOff1;
7202 // };
7203 //
7204 // struct PS_INPUT {
7205 // float2 vTextureCoords : TEXCOORD2;
7206 // };
7207 //
7208 // struct PS_OUTPUT {
7209 // float4 vColor : SV_Target0;
7210 // };
7211 //
7212 // PS_OUTPUT MainPs(PS_INPUT i) {
7213 // PS_OUTPUT ps_output;
7214 // float2 off;
7215 // float2 vtc;
7216 // if (g_B)
7217 // off = g_TexOff0;
7218 // else
7219 // off = g_TexOff1;
7220 // vtc = i.vTextureCoords.xy + off;
7221 // ps_output.vColor = g_tColor.Sample(g_sAniso, vtc);
7222 // return ps_output;
7223 // }
7224
7225 const std::string text = R"(
7226 OpCapability Shader
7227 ;CHECK: OpExtension "SPV_KHR_storage_buffer_storage_class"
7228 %1 = OpExtInstImport "GLSL.std.450"
7229 OpMemoryModel Logical GLSL450
7230 OpEntryPoint Fragment %MainPs "MainPs" %_ %__0 %g_tColor %g_sAniso %i_vTextureCoords %_entryPointOutput_vColor
7231 ;CHECK: OpEntryPoint Fragment %MainPs "MainPs" %_ %__0 %g_tColor %g_sAniso %i_vTextureCoords %_entryPointOutput_vColor %130 %157 %gl_FragCoord
7232 OpExecutionMode %MainPs OriginUpperLeft
7233 OpSource HLSL 500
7234 OpName %MainPs "MainPs"
7235 OpName %PerViewPushConst_t "PerViewPushConst_t"
7236 OpMemberName %PerViewPushConst_t 0 "g_B"
7237 OpName %_ ""
7238 OpName %PerViewConstantBuffer_t "PerViewConstantBuffer_t"
7239 OpMemberName %PerViewConstantBuffer_t 0 "g_TexOff0"
7240 OpMemberName %PerViewConstantBuffer_t 1 "g_TexOff1"
7241 OpName %__0 ""
7242 OpName %g_tColor "g_tColor"
7243 OpName %g_sAniso "g_sAniso"
7244 OpName %i_vTextureCoords "i.vTextureCoords"
7245 OpName %_entryPointOutput_vColor "@entryPointOutput.vColor"
7246 OpMemberDecorate %PerViewPushConst_t 0 Offset 0
7247 OpDecorate %PerViewPushConst_t Block
7248 OpMemberDecorate %PerViewConstantBuffer_t 0 Offset 0
7249 OpMemberDecorate %PerViewConstantBuffer_t 1 Offset 8
7250 OpDecorate %PerViewConstantBuffer_t Block
7251 OpDecorate %__0 DescriptorSet 0
7252 OpDecorate %__0 Binding 1
7253 OpDecorate %g_tColor DescriptorSet 0
7254 OpDecorate %g_tColor Binding 0
7255 OpDecorate %g_sAniso DescriptorSet 0
7256 OpDecorate %g_sAniso Binding 2
7257 OpDecorate %i_vTextureCoords Location 0
7258 OpDecorate %_entryPointOutput_vColor Location 0
7259 ;CHECK: OpDecorate %_runtimearr_uint ArrayStride 4
7260 ;CHECK: OpDecorate %_struct_128 Block
7261 ;CHECK: OpMemberDecorate %_struct_128 0 Offset 0
7262 ;CHECK: OpDecorate %130 DescriptorSet 7
7263 ;CHECK: OpDecorate %130 Binding 1
7264 ;CHECK: OpDecorate %_struct_155 Block
7265 ;CHECK: OpMemberDecorate %_struct_155 0 Offset 0
7266 ;CHECK: OpMemberDecorate %_struct_155 1 Offset 4
7267 ;CHECK: OpDecorate %157 DescriptorSet 7
7268 ;CHECK: OpDecorate %157 Binding 0
7269 ;CHECK: OpDecorate %gl_FragCoord BuiltIn FragCoord
7270 %void = OpTypeVoid
7271 %3 = OpTypeFunction %void
7272 %float = OpTypeFloat 32
7273 %v2float = OpTypeVector %float 2
7274 %v4float = OpTypeVector %float 4
7275 %uint = OpTypeInt 32 0
7276 %PerViewPushConst_t = OpTypeStruct %uint
7277 %_ptr_PushConstant_PerViewPushConst_t = OpTypePointer PushConstant %PerViewPushConst_t
7278 %_ = OpVariable %_ptr_PushConstant_PerViewPushConst_t PushConstant
7279 %int = OpTypeInt 32 1
7280 %int_0 = OpConstant %int 0
7281 %_ptr_PushConstant_uint = OpTypePointer PushConstant %uint
7282 %bool = OpTypeBool
7283 %uint_0 = OpConstant %uint 0
7284 %PerViewConstantBuffer_t = OpTypeStruct %v2float %v2float
7285 %_ptr_Uniform_PerViewConstantBuffer_t = OpTypePointer Uniform %PerViewConstantBuffer_t
7286 %__0 = OpVariable %_ptr_Uniform_PerViewConstantBuffer_t Uniform
7287 %_ptr_Uniform_v2float = OpTypePointer Uniform %v2float
7288 %int_1 = OpConstant %int 1
7289 %49 = OpTypeImage %float 2D 0 0 0 1 Unknown
7290 %_ptr_UniformConstant_49 = OpTypePointer UniformConstant %49
7291 %g_tColor = OpVariable %_ptr_UniformConstant_49 UniformConstant
7292 %53 = OpTypeSampler
7293 %_ptr_UniformConstant_53 = OpTypePointer UniformConstant %53
7294 %g_sAniso = OpVariable %_ptr_UniformConstant_53 UniformConstant
7295 %57 = OpTypeSampledImage %49
7296 %_ptr_Input_v2float = OpTypePointer Input %v2float
7297 %i_vTextureCoords = OpVariable %_ptr_Input_v2float Input
7298 %_ptr_Output_v4float = OpTypePointer Output %v4float
7299 %_entryPointOutput_vColor = OpVariable %_ptr_Output_v4float Output
7300 ;CHECK: %uint_7 = OpConstant %uint 7
7301 ;CHECK: %uint_1 = OpConstant %uint 1
7302 ;CHECK: %122 = OpTypeFunction %uint %uint %uint %uint
7303 ;CHECK: %_runtimearr_uint = OpTypeRuntimeArray %uint
7304 ;CHECK: %_struct_128 = OpTypeStruct %_runtimearr_uint
7305 ;CHECK: %_ptr_StorageBuffer__struct_128 = OpTypePointer StorageBuffer %_struct_128
7306 ;CHECK: %130 = OpVariable %_ptr_StorageBuffer__struct_128 StorageBuffer
7307 ;CHECK: %_ptr_StorageBuffer_uint = OpTypePointer StorageBuffer %uint
7308 ;CHECK: %uint_3 = OpConstant %uint 3
7309 ;CHECK: %148 = OpTypeFunction %void %uint %uint %uint %uint %uint
7310 ;CHECK: %_struct_155 = OpTypeStruct %uint %_runtimearr_uint
7311 ;CHECK: %_ptr_StorageBuffer__struct_155 = OpTypePointer StorageBuffer %_struct_155
7312 ;CHECK: %157 = OpVariable %_ptr_StorageBuffer__struct_155 StorageBuffer
7313 ;CHECK: %uint_11 = OpConstant %uint 11
7314 ;CHECK: %uint_4 = OpConstant %uint 4
7315 ;CHECK: %uint_23 = OpConstant %uint 23
7316 ;CHECK: %uint_2 = OpConstant %uint 2
7317 ;CHECK:%_ptr_Input_v4float = OpTypePointer Input %v4float
7318 ;CHECK:%gl_FragCoord = OpVariable %_ptr_Input_v4float Input
7319 ;CHECK: %v4uint = OpTypeVector %uint 4
7320 ;CHECK: %uint_5 = OpConstant %uint 5
7321 ;CHECK: %uint_8 = OpConstant %uint 8
7322 ;CHECK: %uint_9 = OpConstant %uint 9
7323 ;CHECK: %uint_10 = OpConstant %uint 10
7324 ;CHECK: %uint_71 = OpConstant %uint 71
7325 ;CHECK: %202 = OpConstantNull %v2float
7326 ;CHECK: %uint_75 = OpConstant %uint 75
7327 %MainPs = OpFunction %void None %3
7328 %5 = OpLabel
7329 ;CHECK: %140 = OpFunctionCall %uint %121 %uint_1 %uint_1 %uint_0
7330 ;CHECK: OpBranch %117
7331 ;CHECK: %117 = OpLabel
7332 ;CHECK: OpBranch %116
7333 ;CHECK: %116 = OpLabel
7334 %69 = OpLoad %v2float %i_vTextureCoords
7335 %82 = OpAccessChain %_ptr_PushConstant_uint %_ %int_0
7336 %83 = OpLoad %uint %82
7337 %84 = OpINotEqual %bool %83 %uint_0
7338 OpSelectionMerge %91 None
7339 OpBranchConditional %84 %85 %88
7340 %85 = OpLabel
7341 %86 = OpAccessChain %_ptr_Uniform_v2float %__0 %int_0
7342 %87 = OpLoad %v2float %86
7343 ;CHECK-NOT: %87 = OpLoad %v2float %86
7344 ;CHECK: %119 = OpIAdd %uint %uint_0 %uint_7
7345 ;CHECK: %141 = OpULessThan %bool %119 %140
7346 ;CHECK: OpSelectionMerge %143 None
7347 ;CHECK: OpBranchConditional %141 %144 %145
7348 ;CHECK: %144 = OpLabel
7349 ;CHECK: %146 = OpLoad %v2float %86
7350 ;CHECK: OpBranch %143
7351 ;CHECK: %145 = OpLabel
7352 ;CHECK: %201 = OpFunctionCall %void %147 %uint_71 %uint_3 %uint_0 %119 %140
7353 ;CHECK: OpBranch %143
7354 ;CHECK: %143 = OpLabel
7355 ;CHECK: %203 = OpPhi %v2float %146 %144 %202 %145
7356 OpBranch %91
7357 %88 = OpLabel
7358 %89 = OpAccessChain %_ptr_Uniform_v2float %__0 %int_1
7359 %90 = OpLoad %v2float %89
7360 ;CHECK-NOT: %90 = OpLoad %v2float %89
7361 ;CHECK: %204 = OpIAdd %uint %uint_8 %uint_7
7362 ;CHECK: %205 = OpULessThan %bool %204 %140
7363 ;CHECK: OpSelectionMerge %206 None
7364 ;CHECK: OpBranchConditional %205 %207 %208
7365 ;CHECK: %207 = OpLabel
7366 ;CHECK: %209 = OpLoad %v2float %89
7367 ;CHECK: OpBranch %206
7368 ;CHECK: %208 = OpLabel
7369 ;CHECK: %211 = OpFunctionCall %void %147 %uint_75 %uint_3 %uint_0 %204 %140
7370 ;CHECK: OpBranch %206
7371 ;CHECK: %206 = OpLabel
7372 ;CHECK: %212 = OpPhi %v2float %209 %207 %202 %208
7373 OpBranch %91
7374 %91 = OpLabel
7375 %115 = OpPhi %v2float %87 %85 %90 %88
7376 ;CHECK-NOT: %115 = OpPhi %v2float %87 %85 %90 %88
7377 ;CHECK: %115 = OpPhi %v2float %203 %143 %212 %206
7378 %95 = OpFAdd %v2float %69 %115
7379 %96 = OpLoad %49 %g_tColor
7380 %97 = OpLoad %53 %g_sAniso
7381 %98 = OpSampledImage %57 %96 %97
7382 %100 = OpImageSampleImplicitLod %v4float %98 %95
7383 OpStore %_entryPointOutput_vColor %100
7384 OpReturn
7385 OpFunctionEnd
7386 ;CHECK: %121 = OpFunction %uint None %122
7387 ;CHECK: %123 = OpFunctionParameter %uint
7388 ;CHECK: %124 = OpFunctionParameter %uint
7389 ;CHECK: %125 = OpFunctionParameter %uint
7390 ;CHECK: %126 = OpLabel
7391 ;CHECK: %132 = OpAccessChain %_ptr_StorageBuffer_uint %130 %uint_0 %123
7392 ;CHECK: %133 = OpLoad %uint %132
7393 ;CHECK: %134 = OpIAdd %uint %133 %124
7394 ;CHECK: %135 = OpAccessChain %_ptr_StorageBuffer_uint %130 %uint_0 %134
7395 ;CHECK: %136 = OpLoad %uint %135
7396 ;CHECK: %137 = OpIAdd %uint %136 %125
7397 ;CHECK: %138 = OpAccessChain %_ptr_StorageBuffer_uint %130 %uint_0 %137
7398 ;CHECK: %139 = OpLoad %uint %138
7399 ;CHECK: OpReturnValue %139
7400 ;CHECK: OpFunctionEnd
7401 ;CHECK: %147 = OpFunction %void None %148
7402 ;CHECK: %149 = OpFunctionParameter %uint
7403 ;CHECK: %150 = OpFunctionParameter %uint
7404 ;CHECK: %151 = OpFunctionParameter %uint
7405 ;CHECK: %152 = OpFunctionParameter %uint
7406 ;CHECK: %153 = OpFunctionParameter %uint
7407 ;CHECK: %154 = OpLabel
7408 ;CHECK: %158 = OpAccessChain %_ptr_StorageBuffer_uint %157 %uint_0
7409 ;CHECK: %161 = OpAtomicIAdd %uint %158 %uint_4 %uint_0 %uint_11
7410 ;CHECK: %162 = OpIAdd %uint %161 %uint_11
7411 ;CHECK: %163 = OpArrayLength %uint %157 1
7412 ;CHECK: %164 = OpULessThanEqual %bool %162 %163
7413 ;CHECK: OpSelectionMerge %165 None
7414 ;CHECK: OpBranchConditional %164 %166 %165
7415 ;CHECK: %166 = OpLabel
7416 ;CHECK: %167 = OpIAdd %uint %161 %uint_0
7417 ;CHECK: %168 = OpAccessChain %_ptr_StorageBuffer_uint %157 %uint_1 %167
7418 ;CHECK: OpStore %168 %uint_11
7419 ;CHECK: %170 = OpIAdd %uint %161 %uint_1
7420 ;CHECK: %171 = OpAccessChain %_ptr_StorageBuffer_uint %157 %uint_1 %170
7421 ;CHECK: OpStore %171 %uint_23
7422 ;CHECK: %173 = OpIAdd %uint %161 %uint_2
7423 ;CHECK: %174 = OpAccessChain %_ptr_StorageBuffer_uint %157 %uint_1 %173
7424 ;CHECK: OpStore %174 %149
7425 ;CHECK: %175 = OpIAdd %uint %161 %uint_3
7426 ;CHECK: %176 = OpAccessChain %_ptr_StorageBuffer_uint %157 %uint_1 %175
7427 ;CHECK: OpStore %176 %uint_4
7428 ;CHECK: %179 = OpLoad %v4float %gl_FragCoord
7429 ;CHECK: %181 = OpBitcast %v4uint %179
7430 ;CHECK: %182 = OpCompositeExtract %uint %181 0
7431 ;CHECK: %183 = OpIAdd %uint %161 %uint_4
7432 ;CHECK: %184 = OpAccessChain %_ptr_StorageBuffer_uint %157 %uint_1 %183
7433 ;CHECK: OpStore %184 %182
7434 ;CHECK: %185 = OpCompositeExtract %uint %181 1
7435 ;CHECK: %187 = OpIAdd %uint %161 %uint_5
7436 ;CHECK: %188 = OpAccessChain %_ptr_StorageBuffer_uint %157 %uint_1 %187
7437 ;CHECK: OpStore %188 %185
7438 ;CHECK: %189 = OpIAdd %uint %161 %uint_7
7439 ;CHECK: %190 = OpAccessChain %_ptr_StorageBuffer_uint %157 %uint_1 %189
7440 ;CHECK: OpStore %190 %150
7441 ;CHECK: %192 = OpIAdd %uint %161 %uint_8
7442 ;CHECK: %193 = OpAccessChain %_ptr_StorageBuffer_uint %157 %uint_1 %192
7443 ;CHECK: OpStore %193 %151
7444 ;CHECK: %195 = OpIAdd %uint %161 %uint_9
7445 ;CHECK: %196 = OpAccessChain %_ptr_StorageBuffer_uint %157 %uint_1 %195
7446 ;CHECK: OpStore %196 %152
7447 ;CHECK: %198 = OpIAdd %uint %161 %uint_10
7448 ;CHECK: %199 = OpAccessChain %_ptr_StorageBuffer_uint %157 %uint_1 %198
7449 ;CHECK: OpStore %199 %153
7450 ;CHECK: OpBranch %165
7451 ;CHECK: %165 = OpLabel
7452 ;CHECK: OpReturn
7453 ;CHECK: OpFunctionEnd
7454 )";
7455
7456 SetTargetEnv(SPV_ENV_VULKAN_1_2);
7457 SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
7458 SinglePassRunAndMatch<InstBindlessCheckPass>(text, true, 7u, 23u, false,
7459 false, true);
7460 }
7461
7462 TEST_F(InstBindlessTest, UniformArrayRefNoDescInit) {
7463 // Check that uniform array ref does not go out-of-bounds.
7464 //
7465 // Texture2D g_tColor;
7466 // SamplerState g_sAniso;
7467 //
7468 // layout(push_constant) cbuffer PerViewPushConst_t { uint g_c; };
7469 //
7470 // struct PerBatchEnvMapConstantBuffer_t {
7471 // float4x3 g_matEnvMapWorldToLocal;
7472 // float4 g_vEnvironmentMapBoxMins;
7473 // float2 g_TexOff;
7474 // };
7475 //
7476 // cbuffer _BindlessFastEnvMapCB_PS_t {
7477 // PerBatchEnvMapConstantBuffer_t g_envMapConstants[128];
7478 // };
7479 //
7480 // struct PS_INPUT {
7481 // float2 vTextureCoords : TEXCOORD2;
7482 // };
7483 //
7484 // struct PS_OUTPUT {
7485 // float4 vColor : SV_Target0;
7486 // };
7487 //
7488 // PS_OUTPUT MainPs(PS_INPUT i) {
7489 // PS_OUTPUT ps_output;
7490 // float2 off;
7491 // float2 vtc;
7492 // off = g_envMapConstants[g_c].g_TexOff;
7493 // vtc = i.vTextureCoords.xy + off;
7494 // ps_output.vColor = g_tColor.Sample(g_sAniso, vtc);
7495 // return ps_output;
7496 // }
7497
7498 const std::string text = R"(
7499 OpCapability Shader
7500 ;CHECK: OpExtension "SPV_KHR_storage_buffer_storage_class"
7501 %1 = OpExtInstImport "GLSL.std.450"
7502 OpMemoryModel Logical GLSL450
7503 OpEntryPoint Fragment %MainPs "MainPs" %_ %__0 %g_tColor %g_sAniso %i_vTextureCoords %_entryPointOutput_vColor
7504 OpExecutionMode %MainPs OriginUpperLeft
7505 OpSource HLSL 500
7506 OpName %MainPs "MainPs"
7507 OpName %PerBatchEnvMapConstantBuffer_t "PerBatchEnvMapConstantBuffer_t"
7508 OpMemberName %PerBatchEnvMapConstantBuffer_t 0 "g_matEnvMapWorldToLocal"
7509 OpMemberName %PerBatchEnvMapConstantBuffer_t 1 "g_vEnvironmentMapBoxMins"
7510 OpMemberName %PerBatchEnvMapConstantBuffer_t 2 "g_TexOff"
7511 OpName %_BindlessFastEnvMapCB_PS_t "_BindlessFastEnvMapCB_PS_t"
7512 OpMemberName %_BindlessFastEnvMapCB_PS_t 0 "g_envMapConstants"
7513 OpName %_ ""
7514 OpName %PerViewPushConst_t "PerViewPushConst_t"
7515 OpMemberName %PerViewPushConst_t 0 "g_c"
7516 OpName %__0 ""
7517 OpName %g_tColor "g_tColor"
7518 OpName %g_sAniso "g_sAniso"
7519 OpName %i_vTextureCoords "i.vTextureCoords"
7520 OpName %_entryPointOutput_vColor "@entryPointOutput.vColor"
7521 OpMemberDecorate %PerBatchEnvMapConstantBuffer_t 0 RowMajor
7522 OpMemberDecorate %PerBatchEnvMapConstantBuffer_t 0 Offset 0
7523 OpMemberDecorate %PerBatchEnvMapConstantBuffer_t 0 MatrixStride 16
7524 OpMemberDecorate %PerBatchEnvMapConstantBuffer_t 1 Offset 48
7525 OpMemberDecorate %PerBatchEnvMapConstantBuffer_t 2 Offset 64
7526 OpDecorate %_arr_PerBatchEnvMapConstantBuffer_t_uint_128 ArrayStride 80
7527 OpMemberDecorate %_BindlessFastEnvMapCB_PS_t 0 Offset 0
7528 OpDecorate %_BindlessFastEnvMapCB_PS_t Block
7529 OpDecorate %_ DescriptorSet 0
7530 OpDecorate %_ Binding 2
7531 OpMemberDecorate %PerViewPushConst_t 0 Offset 0
7532 OpDecorate %PerViewPushConst_t Block
7533 OpDecorate %g_tColor DescriptorSet 0
7534 OpDecorate %g_tColor Binding 0
7535 OpDecorate %g_sAniso DescriptorSet 0
7536 OpDecorate %g_sAniso Binding 1
7537 OpDecorate %i_vTextureCoords Location 0
7538 OpDecorate %_entryPointOutput_vColor Location 0
7539 ;CHECK: OpDecorate %_runtimearr_uint ArrayStride 4
7540 ;CHECK: OpDecorate %_struct_111 Block
7541 ;CHECK: OpMemberDecorate %_struct_111 0 Offset 0
7542 ;CHECK: OpDecorate %113 DescriptorSet 7
7543 ;CHECK: OpDecorate %113 Binding 1
7544 ;CHECK: OpDecorate %_struct_139 Block
7545 ;CHECK: OpMemberDecorate %_struct_139 0 Offset 0
7546 ;CHECK: OpMemberDecorate %_struct_139 1 Offset 4
7547 ;CHECK: OpDecorate %141 DescriptorSet 7
7548 ;CHECK: OpDecorate %141 Binding 0
7549 ;CHECK: OpDecorate %gl_FragCoord BuiltIn FragCoord
7550 %void = OpTypeVoid
7551 %3 = OpTypeFunction %void
7552 %float = OpTypeFloat 32
7553 %v2float = OpTypeVector %float 2
7554 %v4float = OpTypeVector %float 4
7555 %v3float = OpTypeVector %float 3
7556 %mat4v3float = OpTypeMatrix %v3float 4
7557 %PerBatchEnvMapConstantBuffer_t = OpTypeStruct %mat4v3float %v4float %v2float
7558 %uint = OpTypeInt 32 0
7559 %uint_128 = OpConstant %uint 128
7560 %_arr_PerBatchEnvMapConstantBuffer_t_uint_128 = OpTypeArray %PerBatchEnvMapConstantBuffer_t %uint_128
7561 %_BindlessFastEnvMapCB_PS_t = OpTypeStruct %_arr_PerBatchEnvMapConstantBuffer_t_uint_128
7562 %_ptr_Uniform__BindlessFastEnvMapCB_PS_t = OpTypePointer Uniform %_BindlessFastEnvMapCB_PS_t
7563 %_ = OpVariable %_ptr_Uniform__BindlessFastEnvMapCB_PS_t Uniform
7564 %int = OpTypeInt 32 1
7565 %int_0 = OpConstant %int 0
7566 %PerViewPushConst_t = OpTypeStruct %uint
7567 %_ptr_PushConstant_PerViewPushConst_t = OpTypePointer PushConstant %PerViewPushConst_t
7568 %__0 = OpVariable %_ptr_PushConstant_PerViewPushConst_t PushConstant
7569 %_ptr_PushConstant_uint = OpTypePointer PushConstant %uint
7570 %int_2 = OpConstant %int 2
7571 %_ptr_Uniform_v2float = OpTypePointer Uniform %v2float
7572 %46 = OpTypeImage %float 2D 0 0 0 1 Unknown
7573 %_ptr_UniformConstant_46 = OpTypePointer UniformConstant %46
7574 %g_tColor = OpVariable %_ptr_UniformConstant_46 UniformConstant
7575 %50 = OpTypeSampler
7576 %_ptr_UniformConstant_50 = OpTypePointer UniformConstant %50
7577 %g_sAniso = OpVariable %_ptr_UniformConstant_50 UniformConstant
7578 %54 = OpTypeSampledImage %46
7579 %_ptr_Input_v2float = OpTypePointer Input %v2float
7580 %i_vTextureCoords = OpVariable %_ptr_Input_v2float Input
7581 %_ptr_Output_v4float = OpTypePointer Output %v4float
7582 %_entryPointOutput_vColor = OpVariable %_ptr_Output_v4float Output
7583 ;CHECK: %uint_0 = OpConstant %uint 0
7584 ;CHECK: %uint_80 = OpConstant %uint 80
7585 ;CHECK: %uint_64 = OpConstant %uint 64
7586 ;CHECK: %uint_7 = OpConstant %uint 7
7587 ;CHECK: %uint_2 = OpConstant %uint 2
7588 ;CHECK: %uint_1 = OpConstant %uint 1
7589 ;CHECK: %105 = OpTypeFunction %uint %uint %uint %uint
7590 ;CHECK:%_runtimearr_uint = OpTypeRuntimeArray %uint
7591 ;CHECK:%_struct_111 = OpTypeStruct %_runtimearr_uint
7592 ;CHECK:%_ptr_StorageBuffer__struct_111 = OpTypePointer StorageBuffer %_struct_111
7593 ;CHECK: %113 = OpVariable %_ptr_StorageBuffer__struct_111 StorageBuffer
7594 ;CHECK:%_ptr_StorageBuffer_uint = OpTypePointer StorageBuffer %uint
7595 ;CHECK: %bool = OpTypeBool
7596 ;CHECK: %uint_3 = OpConstant %uint 3
7597 ;CHECK: %132 = OpTypeFunction %void %uint %uint %uint %uint %uint
7598 ;CHECK:%_struct_139 = OpTypeStruct %uint %_runtimearr_uint
7599 ;CHECK:%_ptr_StorageBuffer__struct_139 = OpTypePointer StorageBuffer %_struct_139
7600 ;CHECK: %141 = OpVariable %_ptr_StorageBuffer__struct_139 StorageBuffer
7601 ;CHECK: %uint_11 = OpConstant %uint 11
7602 ;CHECK: %uint_4 = OpConstant %uint 4
7603 ;CHECK: %uint_23 = OpConstant %uint 23
7604 ;CHECK:%_ptr_Input_v4float = OpTypePointer Input %v4float
7605 ;CHECK:%gl_FragCoord = OpVariable %_ptr_Input_v4float Input
7606 ;CHECK: %v4uint = OpTypeVector %uint 4
7607 ;CHECK: %uint_5 = OpConstant %uint 5
7608 ;CHECK: %uint_8 = OpConstant %uint 8
7609 ;CHECK: %uint_9 = OpConstant %uint 9
7610 ;CHECK: %uint_10 = OpConstant %uint 10
7611 ;CHECK: %uint_78 = OpConstant %uint 78
7612 ;CHECK: %185 = OpConstantNull %v2float
7613 %MainPs = OpFunction %void None %3
7614 %5 = OpLabel
7615 ;CHECK: %123 = OpFunctionCall %uint %104 %uint_1 %uint_2 %uint_0
7616 ;CHECK: OpBranch %93
7617 ;CHECK: %93 = OpLabel
7618 ;CHECK: OpBranch %92
7619 ;CHECK: %92 = OpLabel
7620 %66 = OpLoad %v2float %i_vTextureCoords
7621 %79 = OpAccessChain %_ptr_PushConstant_uint %__0 %int_0
7622 %80 = OpLoad %uint %79
7623 %81 = OpAccessChain %_ptr_Uniform_v2float %_ %int_0 %80 %int_2
7624 %82 = OpLoad %v2float %81
7625 ;CHECK-NOT: %82 = OpLoad %v2float %81
7626 ;CHECK: %96 = OpIMul %uint %uint_80 %80
7627 ;CHECK: %97 = OpIAdd %uint %uint_0 %96
7628 ;CHECK: %99 = OpIAdd %uint %97 %uint_64
7629 ;CHECK: %101 = OpIAdd %uint %99 %uint_7
7630 ;CHECK: %125 = OpULessThan %bool %101 %123
7631 ;CHECK: OpSelectionMerge %127 None
7632 ;CHECK: OpBranchConditional %125 %128 %129
7633 ;CHECK: %128 = OpLabel
7634 ;CHECK: %130 = OpLoad %v2float %81
7635 ;CHECK: OpBranch %127
7636 ;CHECK: %129 = OpLabel
7637 ;CHECK: %184 = OpFunctionCall %void %131 %uint_78 %uint_3 %uint_0 %101 %123
7638 ;CHECK: OpBranch %127
7639 ;CHECK: %127 = OpLabel
7640 ;CHECK: %186 = OpPhi %v2float %130 %128 %185 %129
7641 %86 = OpFAdd %v2float %66 %82
7642 ;CHECK-NOT: %86 = OpFAdd %v2float %66 %82
7643 ;CHECK: %86 = OpFAdd %v2float %66 %186
7644 %87 = OpLoad %46 %g_tColor
7645 %88 = OpLoad %50 %g_sAniso
7646 %89 = OpSampledImage %54 %87 %88
7647 %91 = OpImageSampleImplicitLod %v4float %89 %86
7648 OpStore %_entryPointOutput_vColor %91
7649 OpReturn
7650 OpFunctionEnd
7651 ;CHECK: %104 = OpFunction %uint None %105
7652 ;CHECK: %106 = OpFunctionParameter %uint
7653 ;CHECK: %107 = OpFunctionParameter %uint
7654 ;CHECK: %108 = OpFunctionParameter %uint
7655 ;CHECK: %109 = OpLabel
7656 ;CHECK: %115 = OpAccessChain %_ptr_StorageBuffer_uint %113 %uint_0 %106
7657 ;CHECK: %116 = OpLoad %uint %115
7658 ;CHECK: %117 = OpIAdd %uint %116 %107
7659 ;CHECK: %118 = OpAccessChain %_ptr_StorageBuffer_uint %113 %uint_0 %117
7660 ;CHECK: %119 = OpLoad %uint %118
7661 ;CHECK: %120 = OpIAdd %uint %119 %108
7662 ;CHECK: %121 = OpAccessChain %_ptr_StorageBuffer_uint %113 %uint_0 %120
7663 ;CHECK: %122 = OpLoad %uint %121
7664 ;CHECK: OpReturnValue %122
7665 ;CHECK: OpFunctionEnd
7666 ;CHECK: %131 = OpFunction %void None %132
7667 ;CHECK: %133 = OpFunctionParameter %uint
7668 ;CHECK: %134 = OpFunctionParameter %uint
7669 ;CHECK: %135 = OpFunctionParameter %uint
7670 ;CHECK: %136 = OpFunctionParameter %uint
7671 ;CHECK: %137 = OpFunctionParameter %uint
7672 ;CHECK: %138 = OpLabel
7673 ;CHECK: %142 = OpAccessChain %_ptr_StorageBuffer_uint %141 %uint_0
7674 ;CHECK: %145 = OpAtomicIAdd %uint %142 %uint_4 %uint_0 %uint_11
7675 ;CHECK: %146 = OpIAdd %uint %145 %uint_11
7676 ;CHECK: %147 = OpArrayLength %uint %141 1
7677 ;CHECK: %148 = OpULessThanEqual %bool %146 %147
7678 ;CHECK: OpSelectionMerge %149 None
7679 ;CHECK: OpBranchConditional %148 %150 %149
7680 ;CHECK: %150 = OpLabel
7681 ;CHECK: %151 = OpIAdd %uint %145 %uint_0
7682 ;CHECK: %152 = OpAccessChain %_ptr_StorageBuffer_uint %141 %uint_1 %151
7683 ;CHECK: OpStore %152 %uint_11
7684 ;CHECK: %154 = OpIAdd %uint %145 %uint_1
7685 ;CHECK: %155 = OpAccessChain %_ptr_StorageBuffer_uint %141 %uint_1 %154
7686 ;CHECK: OpStore %155 %uint_23
7687 ;CHECK: %156 = OpIAdd %uint %145 %uint_2
7688 ;CHECK: %157 = OpAccessChain %_ptr_StorageBuffer_uint %141 %uint_1 %156
7689 ;CHECK: OpStore %157 %133
7690 ;CHECK: %158 = OpIAdd %uint %145 %uint_3
7691 ;CHECK: %159 = OpAccessChain %_ptr_StorageBuffer_uint %141 %uint_1 %158
7692 ;CHECK: OpStore %159 %uint_4
7693 ;CHECK: %162 = OpLoad %v4float %gl_FragCoord
7694 ;CHECK: %164 = OpBitcast %v4uint %162
7695 ;CHECK: %165 = OpCompositeExtract %uint %164 0
7696 ;CHECK: %166 = OpIAdd %uint %145 %uint_4
7697 ;CHECK: %167 = OpAccessChain %_ptr_StorageBuffer_uint %141 %uint_1 %166
7698 ;CHECK: OpStore %167 %165
7699 ;CHECK: %168 = OpCompositeExtract %uint %164 1
7700 ;CHECK: %170 = OpIAdd %uint %145 %uint_5
7701 ;CHECK: %171 = OpAccessChain %_ptr_StorageBuffer_uint %141 %uint_1 %170
7702 ;CHECK: OpStore %171 %168
7703 ;CHECK: %172 = OpIAdd %uint %145 %uint_7
7704 ;CHECK: %173 = OpAccessChain %_ptr_StorageBuffer_uint %141 %uint_1 %172
7705 ;CHECK: OpStore %173 %134
7706 ;CHECK: %175 = OpIAdd %uint %145 %uint_8
7707 ;CHECK: %176 = OpAccessChain %_ptr_StorageBuffer_uint %141 %uint_1 %175
7708 ;CHECK: OpStore %176 %135
7709 ;CHECK: %178 = OpIAdd %uint %145 %uint_9
7710 ;CHECK: %179 = OpAccessChain %_ptr_StorageBuffer_uint %141 %uint_1 %178
7711 ;CHECK: OpStore %179 %136
7712 ;CHECK: %181 = OpIAdd %uint %145 %uint_10
7713 ;CHECK: %182 = OpAccessChain %_ptr_StorageBuffer_uint %141 %uint_1 %181
7714 ;CHECK: OpStore %182 %137
7715 ;CHECK: OpBranch %149
7716 ;CHECK: %149 = OpLabel
7717 ;CHECK: OpReturn
7718 ;CHECK: OpFunctionEnd
7719 )";
7720
7721 SetTargetEnv(SPV_ENV_VULKAN_1_2);
7722 SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
7723 SinglePassRunAndMatch<InstBindlessCheckPass>(text, true, 7u, 23u, false,
7724 false, true);
7725 }
7726
7727 TEST_F(InstBindlessTest, UniformArrayRefWithDescInit) {
7728 // The buffer-oob and desc-init checks should use the same debug
7729 // output buffer write function.
7730 //
7731 // Same source as UniformArrayRefNoDescInit
7732
7733 const std::string text = R"(
7734 OpCapability Shader
7735 ;CHECK: OpExtension "SPV_KHR_storage_buffer_storage_class"
7736 %1 = OpExtInstImport "GLSL.std.450"
7737 OpMemoryModel Logical GLSL450
7738 OpEntryPoint Fragment %MainPs "MainPs" %_ %__0 %g_tColor %g_sAniso %i_vTextureCoords %_entryPointOutput_vColor
7739 ;CHECK: OpEntryPoint Fragment %MainPs "MainPs" %_ %__0 %g_tColor %g_sAniso %i_vTextureCoords %_entryPointOutput_vColor %113 %144 %gl_FragCoord
7740 OpExecutionMode %MainPs OriginUpperLeft
7741 OpSource HLSL 500
7742 OpName %MainPs "MainPs"
7743 OpName %PerBatchEnvMapConstantBuffer_t
7744 "PerBatchEnvMapConstantBuffer_t" OpMemberName %PerBatchEnvMapConstantBuffer_t 0
7745 "g_matEnvMapWorldToLocal" OpMemberName %PerBatchEnvMapConstantBuffer_t 1
7746 "g_vEnvironmentMapBoxMins" OpMemberName %PerBatchEnvMapConstantBuffer_t 2
7747 "g_TexOff" OpName %_BindlessFastEnvMapCB_PS_t "_BindlessFastEnvMapCB_PS_t"
7748 OpMemberName %_BindlessFastEnvMapCB_PS_t 0 "g_envMapConstants"
7749 OpName %_ ""
7750 OpName %PerViewPushConst_t "PerViewPushConst_t"
7751 OpMemberName %PerViewPushConst_t 0 "g_c"
7752 OpName %__0 ""
7753 OpName %g_tColor "g_tColor"
7754 OpName %g_sAniso "g_sAniso"
7755 OpName %i_vTextureCoords "i.vTextureCoords"
7756 OpName %_entryPointOutput_vColor "@entryPointOutput.vColor"
7757 OpMemberDecorate %PerBatchEnvMapConstantBuffer_t 0 RowMajor
7758 OpMemberDecorate %PerBatchEnvMapConstantBuffer_t 0 Offset 0
7759 OpMemberDecorate %PerBatchEnvMapConstantBuffer_t 0 MatrixStride 16
7760 OpMemberDecorate %PerBatchEnvMapConstantBuffer_t 1 Offset 48
7761 OpMemberDecorate %PerBatchEnvMapConstantBuffer_t 2 Offset 64
7762 OpDecorate %_arr_PerBatchEnvMapConstantBuffer_t_uint_128 ArrayStride 80
7763 OpMemberDecorate %_BindlessFastEnvMapCB_PS_t 0 Offset 0
7764 OpDecorate %_BindlessFastEnvMapCB_PS_t Block
7765 OpDecorate %_ DescriptorSet 0
7766 OpDecorate %_ Binding 2
7767 OpMemberDecorate %PerViewPushConst_t 0 Offset 0
7768 OpDecorate %PerViewPushConst_t Block
7769 OpDecorate %g_tColor DescriptorSet 0
7770 OpDecorate %g_tColor Binding 0
7771 OpDecorate %g_sAniso DescriptorSet 0
7772 OpDecorate %g_sAniso Binding 1
7773 OpDecorate %i_vTextureCoords Location 0
7774 OpDecorate %_entryPointOutput_vColor Location 0
7775 ;CHECK: OpDecorate %_runtimearr_uint ArrayStride 4
7776 ;CHECK: OpDecorate %_struct_111 Block
7777 ;CHECK: OpMemberDecorate %_struct_111 0 Offset 0
7778 ;CHECK: OpDecorate %113 DescriptorSet 7
7779 ;CHECK: OpDecorate %113 Binding 1
7780 ;CHECK: OpDecorate %_struct_142 Block
7781 ;CHECK: OpMemberDecorate %_struct_142 0 Offset 0
7782 ;CHECK: OpMemberDecorate %_struct_142 1 Offset 4
7783 ;CHECK: OpDecorate %144 DescriptorSet 7
7784 ;CHECK: OpDecorate %144 Binding 0
7785 ;CHECK: OpDecorate %gl_FragCoord BuiltIn FragCoord
7786 %void = OpTypeVoid
7787 %3 = OpTypeFunction %void
7788 %float = OpTypeFloat 32
7789 %v2float = OpTypeVector %float 2
7790 %v4float = OpTypeVector %float 4
7791 %v3float = OpTypeVector %float 3
7792 %mat4v3float = OpTypeMatrix %v3float 4
7793 %PerBatchEnvMapConstantBuffer_t = OpTypeStruct %mat4v3float %v4float %v2float
7794 %uint = OpTypeInt 32 0
7795 %uint_128 = OpConstant %uint 128
7796 %_arr_PerBatchEnvMapConstantBuffer_t_uint_128 = OpTypeArray %PerBatchEnvMapConstantBuffer_t %uint_128
7797 %_BindlessFastEnvMapCB_PS_t = OpTypeStruct %_arr_PerBatchEnvMapConstantBuffer_t_uint_128
7798 %_ptr_Uniform__BindlessFastEnvMapCB_PS_t = OpTypePointer Uniform %_BindlessFastEnvMapCB_PS_t
7799 %_ = OpVariable %_ptr_Uniform__BindlessFastEnvMapCB_PS_t Uniform
7800 %int = OpTypeInt 32 1
7801 %int_0 = OpConstant %int 0
7802 %PerViewPushConst_t = OpTypeStruct %uint
7803 %_ptr_PushConstant_PerViewPushConst_t = OpTypePointer PushConstant %PerViewPushConst_t
7804 %__0 = OpVariable %_ptr_PushConstant_PerViewPushConst_t PushConstant
7805 %_ptr_PushConstant_uint = OpTypePointer PushConstant %uint
7806 %int_2 = OpConstant %int 2
7807 %_ptr_Uniform_v2float = OpTypePointer Uniform %v2float
7808 %46 = OpTypeImage %float 2D 0 0 0 1 Unknown
7809 %_ptr_UniformConstant_46 = OpTypePointer UniformConstant %46
7810 %g_tColor = OpVariable %_ptr_UniformConstant_46 UniformConstant
7811 %50 = OpTypeSampler
7812 %_ptr_UniformConstant_50 = OpTypePointer UniformConstant %50
7813 %g_sAniso = OpVariable %_ptr_UniformConstant_50 UniformConstant
7814 %54 = OpTypeSampledImage %46
7815 %_ptr_Input_v2float = OpTypePointer Input %v2float
7816 %i_vTextureCoords = OpVariable %_ptr_Input_v2float Input
7817 %_ptr_Output_v4float = OpTypePointer Output %v4float
7818 %_entryPointOutput_vColor = OpVariable %_ptr_Output_v4float Output
7819 ;CHECK: %uint_0 = OpConstant %uint 0
7820 ;CHECK: %uint_80 = OpConstant %uint 80
7821 ;CHECK: %uint_64 = OpConstant %uint 64
7822 ;CHECK: %uint_7 = OpConstant %uint 7
7823 ;CHECK: %uint_2 = OpConstant %uint 2
7824 ;CHECK: %104 = OpTypeFunction %uint %uint %uint %uint %uint
7825 ;CHECK:%_runtimearr_uint = OpTypeRuntimeArray %uint
7826 ;CHECK:%_struct_111 = OpTypeStruct %_runtimearr_uint
7827 ;CHECK:%_ptr_StorageBuffer__struct_111 = OpTypePointer StorageBuffer %_struct_111
7828 ;CHECK: %113 = OpVariable %_ptr_StorageBuffer__struct_111 StorageBuffer
7829 ;CHECK:%_ptr_StorageBuffer_uint = OpTypePointer StorageBuffer %uint
7830 ;CHECK: %bool = OpTypeBool
7831 ;CHECK: %uint_3 = OpConstant %uint 3
7832 ;CHECK: %135 = OpTypeFunction %void %uint %uint %uint %uint %uint
7833 ;CHECK:%_struct_142 = OpTypeStruct %uint %_runtimearr_uint
7834 ;CHECK:%_ptr_StorageBuffer__struct_142 = OpTypePointer StorageBuffer %_struct_142
7835 ;CHECK: %144 = OpVariable %_ptr_StorageBuffer__struct_142 StorageBuffer
7836 ;CHECK: %uint_11 = OpConstant %uint 11
7837 ;CHECK: %uint_4 = OpConstant %uint 4
7838 ;CHECK: %uint_1 = OpConstant %uint 1
7839 ;CHECK: %uint_23 = OpConstant %uint 23
7840 ;CHECK:%_ptr_Input_v4float = OpTypePointer Input %v4float
7841 ;CHECK:%gl_FragCoord = OpVariable %_ptr_Input_v4float Input
7842 ;CHECK: %v4uint = OpTypeVector %uint 4
7843 ;CHECK: %uint_5 = OpConstant %uint 5
7844 ;CHECK: %uint_8 = OpConstant %uint 8
7845 ;CHECK: %uint_9 = OpConstant %uint 9
7846 ;CHECK: %uint_10 = OpConstant %uint 10
7847 ;CHECK: %uint_78 = OpConstant %uint 78
7848 ;CHECK: %189 = OpConstantNull %v2float
7849 ;CHECK: %uint_83 = OpConstant %uint 83
7850 ;CHECK: %201 = OpConstantNull %v4float
7851 %MainPs = OpFunction %void None %3
7852 %5 = OpLabel
7853 ;CHECK: %126 = OpFunctionCall %uint %103 %uint_0 %uint_0 %uint_2 %uint_0
7854 ;CHECK: %191 = OpFunctionCall %uint %103 %uint_0 %uint_0 %uint_0 %uint_0
7855 ;CHECK: OpBranch %93
7856 ;CHECK: %93 = OpLabel
7857 ;CHECK: OpBranch %92
7858 ;CHECK: %92 = OpLabel
7859 %66 = OpLoad %v2float %i_vTextureCoords
7860 %79 = OpAccessChain %_ptr_PushConstant_uint %__0 %int_0
7861 %80 = OpLoad %uint %79
7862 %81 = OpAccessChain %_ptr_Uniform_v2float %_ %int_0 %80 %int_2
7863 %82 = OpLoad %v2float %81
7864 %86 = OpFAdd %v2float %66 %82
7865 ;CHECK-NOT: %82 = OpLoad %v2float %81
7866 ;CHECK-NOT: %86 = OpFAdd %v2float %66 %82
7867 ;CHECK: %96 = OpIMul %uint %uint_80 %80
7868 ;CHECK: %97 = OpIAdd %uint %uint_0 %96
7869 ;CHECK: %99 = OpIAdd %uint %97 %uint_64
7870 ;CHECK: %101 = OpIAdd %uint %99 %uint_7
7871 ;CHECK: %128 = OpULessThan %bool %101 %126
7872 ;CHECK: OpSelectionMerge %130 None
7873 ;CHECK: OpBranchConditional %128 %131 %132
7874 ;CHECK: %131 = OpLabel
7875 ;CHECK: %133 = OpLoad %v2float %81
7876 ;CHECK: OpBranch %130
7877 ;CHECK: %132 = OpLabel
7878 ;CHECK: %188 = OpFunctionCall %void %134 %uint_78 %uint_3 %uint_0 %101 %126
7879 ;CHECK: OpBranch %130
7880 ;CHECK: %130 = OpLabel
7881 ;CHECK: %190 = OpPhi %v2float %133 %131 %189 %132
7882 ;CHECK: %86 = OpFAdd %v2float %66 %190
7883 %87 = OpLoad %46 %g_tColor %88 = OpLoad %50 %g_sAniso %89 =
7884 OpSampledImage %54 %87 %88 %91 = OpImageSampleImplicitLod %v4float %89 %86
7885 OpStore %_entryPointOutput_vColor %91
7886 ;CHECK-NOT: %91 = OpImageSampleImplicitLod %v4float %89 %86
7887 ;CHECK-NOT: OpStore %_entryPointOutput_vColor %91
7888 ;CHECK: %192 = OpULessThan %bool %uint_0 %191
7889 ;CHECK: OpSelectionMerge %193 None
7890 ;CHECK: OpBranchConditional %192 %194 %195
7891 ;CHECK: %194 = OpLabel
7892 ;CHECK: %196 = OpLoad %46 %g_tColor
7893 ;CHECK: %197 = OpSampledImage %54 %196 %88
7894 ;CHECK: %198 = OpImageSampleImplicitLod %v4float %197 %86
7895 ;CHECK: OpBranch %193
7896 ;CHECK: %195 = OpLabel
7897 ;CHECK: %200 = OpFunctionCall %void %134 %uint_83 %uint_1 %uint_0 %uint_0 %uint_0
7898 ;CHECK: OpBranch %193
7899 ;CHECK: %193 = OpLabel
7900 ;CHECK: %202 = OpPhi %v4float %198 %194 %201 %195
7901 ;CHECK: OpStore %_entryPointOutput_vColor %202
7902 OpReturn
7903 OpFunctionEnd
7904 ;CHECK: %103 = OpFunction %uint None %104
7905 ;CHECK: %105 = OpFunctionParameter %uint
7906 ;CHECK: %106 = OpFunctionParameter %uint
7907 ;CHECK: %107 = OpFunctionParameter %uint
7908 ;CHECK: %108 = OpFunctionParameter %uint
7909 ;CHECK: %109 = OpLabel
7910 ;CHECK: %115 = OpAccessChain %_ptr_StorageBuffer_uint %113 %uint_0 %105
7911 ;CHECK: %116 = OpLoad %uint %115
7912 ;CHECK: %117 = OpIAdd %uint %116 %106
7913 ;CHECK: %118 = OpAccessChain %_ptr_StorageBuffer_uint %113 %uint_0 %117
7914 ;CHECK: %119 = OpLoad %uint %118
7915 ;CHECK: %120 = OpIAdd %uint %119 %107
7916 ;CHECK: %121 = OpAccessChain %_ptr_StorageBuffer_uint %113 %uint_0 %120
7917 ;CHECK: %122 = OpLoad %uint %121
7918 ;CHECK: %123 = OpIAdd %uint %122 %108
7919 ;CHECK: %124 = OpAccessChain %_ptr_StorageBuffer_uint %113 %uint_0 %123
7920 ;CHECK: %125 = OpLoad %uint %124
7921 ;CHECK: OpReturnValue %125
7922 ;CHECK: OpFunctionEnd
7923 ;CHECK: %134 = OpFunction %void None %135
7924 ;CHECK: %136 = OpFunctionParameter %uint
7925 ;CHECK: %137 = OpFunctionParameter %uint
7926 ;CHECK: %138 = OpFunctionParameter %uint
7927 ;CHECK: %139 = OpFunctionParameter %uint
7928 ;CHECK: %140 = OpFunctionParameter %uint
7929 ;CHECK: %141 = OpLabel
7930 ;CHECK: %145 = OpAccessChain %_ptr_StorageBuffer_uint %144 %uint_0
7931 ;CHECK: %148 = OpAtomicIAdd %uint %145 %uint_4 %uint_0 %uint_11
7932 ;CHECK: %149 = OpIAdd %uint %148 %uint_11
7933 ;CHECK: %150 = OpArrayLength %uint %144 1
7934 ;CHECK: %151 = OpULessThanEqual %bool %149 %150
7935 ;CHECK: OpSelectionMerge %152 None
7936 ;CHECK: OpBranchConditional %151 %153 %152
7937 ;CHECK: %153 = OpLabel
7938 ;CHECK: %154 = OpIAdd %uint %148 %uint_0
7939 ;CHECK: %156 = OpAccessChain %_ptr_StorageBuffer_uint %144 %uint_1 %154
7940 ;CHECK: OpStore %156 %uint_11
7941 ;CHECK: %158 = OpIAdd %uint %148 %uint_1
7942 ;CHECK: %159 = OpAccessChain %_ptr_StorageBuffer_uint %144 %uint_1 %158
7943 ;CHECK: OpStore %159 %uint_23
7944 ;CHECK: %160 = OpIAdd %uint %148 %uint_2
7945 ;CHECK: %161 = OpAccessChain %_ptr_StorageBuffer_uint %144 %uint_1 %160
7946 ;CHECK: OpStore %161 %136
7947 ;CHECK: %162 = OpIAdd %uint %148 %uint_3
7948 ;CHECK: %163 = OpAccessChain %_ptr_StorageBuffer_uint %144 %uint_1 %162
7949 ;CHECK: OpStore %163 %uint_4
7950 ;CHECK: %166 = OpLoad %v4float %gl_FragCoord
7951 ;CHECK: %168 = OpBitcast %v4uint %166
7952 ;CHECK: %169 = OpCompositeExtract %uint %168 0
7953 ;CHECK: %170 = OpIAdd %uint %148 %uint_4
7954 ;CHECK: %171 = OpAccessChain %_ptr_StorageBuffer_uint %144 %uint_1 %170
7955 ;CHECK: OpStore %171 %169
7956 ;CHECK: %172 = OpCompositeExtract %uint %168 1
7957 ;CHECK: %174 = OpIAdd %uint %148 %uint_5
7958 ;CHECK: %175 = OpAccessChain %_ptr_StorageBuffer_uint %144 %uint_1 %174
7959 ;CHECK: OpStore %175 %172
7960 ;CHECK: %176 = OpIAdd %uint %148 %uint_7
7961 ;CHECK: %177 = OpAccessChain %_ptr_StorageBuffer_uint %144 %uint_1 %176
7962 ;CHECK: OpStore %177 %137
7963 ;CHECK: %179 = OpIAdd %uint %148 %uint_8
7964 ;CHECK: %180 = OpAccessChain %_ptr_StorageBuffer_uint %144 %uint_1 %179
7965 ;CHECK: OpStore %180 %138
7966 ;CHECK: %182 = OpIAdd %uint %148 %uint_9
7967 ;CHECK: %183 = OpAccessChain %_ptr_StorageBuffer_uint %144 %uint_1 %182
7968 ;CHECK: OpStore %183 %139
7969 ;CHECK: %185 = OpIAdd %uint %148 %uint_10
7970 ;CHECK: %186 = OpAccessChain %_ptr_StorageBuffer_uint %144 %uint_1 %185
7971 ;CHECK: OpStore %186 %140
7972 ;CHECK: OpBranch %152
7973 ;CHECK: %152 = OpLabel
7974 ;CHECK: OpReturn
7975 ;CHECK: OpFunctionEnd
7976 )";
7977
7978 SetTargetEnv(SPV_ENV_VULKAN_1_2);
7979 SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
7980 SinglePassRunAndMatch<InstBindlessCheckPass>(text, true, 7u, 23u, true, true,
7981 true);
7982 }
7983
7984 TEST_F(InstBindlessTest, Descriptor16BitIdxRef) {
7985 // Check that descriptor indexed with 16bit index is inbounds and
7986 // initialized
7987 //
7988 // Use Simple source with min16uint g_nDataIdx
7989
7990 const std::string text = R"(
7991 OpCapability Shader
7992 OpCapability Int16
7993 OpCapability StoragePushConstant16
7994 ;CHECK: OpExtension "SPV_KHR_storage_buffer_storage_class"
7995 %1 = OpExtInstImport "GLSL.std.450"
7996 OpMemoryModel Logical GLSL450
7997 OpEntryPoint Fragment %MainPs "MainPs" %g_tColor %_ %g_sAniso %i_vTextureCoords %_entryPointOutput_vColor
7998 ;CHECK: OpEntryPoint Fragment %MainPs "MainPs" %g_tColor %_ %g_sAniso %i_vTextureCoords %_entryPointOutput_vColor %60 %gl_FragCoord %119
7999 OpExecutionMode %MainPs OriginUpperLeft
8000 OpSource HLSL 500
8001 OpName %MainPs "MainPs"
8002 OpName %g_tColor "g_tColor"
8003 OpName %PerViewConstantBuffer_t "PerViewConstantBuffer_t"
8004 OpMemberName %PerViewConstantBuffer_t 0 "g_nDataIdx"
8005 OpName %_ ""
8006 OpName %g_sAniso "g_sAniso"
8007 OpName %i_vTextureCoords "i.vTextureCoords"
8008 OpName %_entryPointOutput_vColor "@entryPointOutput.vColor"
8009 OpDecorate %g_tColor DescriptorSet 0
8010 OpDecorate %g_tColor Binding 0
8011 OpMemberDecorate %PerViewConstantBuffer_t 0 Offset 0
8012 OpDecorate %PerViewConstantBuffer_t Block
8013 OpDecorate %g_sAniso DescriptorSet 0
8014 OpDecorate %g_sAniso Binding 0
8015 OpDecorate %i_vTextureCoords Location 0
8016 OpDecorate %_entryPointOutput_vColor Location 0
8017 ;CHECK: OpDecorate %_runtimearr_uint ArrayStride 4
8018 ;CHECK: OpDecorate %_struct_58 Block
8019 ;CHECK: OpMemberDecorate %_struct_58 0 Offset 0
8020 ;CHECK: OpMemberDecorate %_struct_58 1 Offset 4
8021 ;CHECK: OpDecorate %60 DescriptorSet 7
8022 ;CHECK: OpDecorate %60 Binding 0
8023 ;CHECK: OpDecorate %gl_FragCoord BuiltIn FragCoord
8024 ;CHECK: OpDecorate %_struct_117 Block
8025 ;CHECK: OpMemberDecorate %_struct_117 0 Offset 0
8026 ;CHECK: OpDecorate %119 DescriptorSet 7
8027 ;CHECK: OpDecorate %119 Binding 1
8028 %void = OpTypeVoid
8029 %10 = OpTypeFunction %void
8030 %float = OpTypeFloat 32
8031 %v2float = OpTypeVector %float 2
8032 %v4float = OpTypeVector %float 4
8033 %int = OpTypeInt 32 1
8034 %int_0 = OpConstant %int 0
8035 %16 = OpTypeImage %float 2D 0 0 0 1 Unknown
8036 %uint = OpTypeInt 32 0
8037 %uint_128 = OpConstant %uint 128
8038 %_arr_16_uint_128 = OpTypeArray %16 %uint_128
8039 %_ptr_UniformConstant__arr_16_uint_128 = OpTypePointer UniformConstant %_arr_16_uint_128
8040 %g_tColor = OpVariable %_ptr_UniformConstant__arr_16_uint_128 UniformConstant
8041 %ushort = OpTypeInt 16 0
8042 %PerViewConstantBuffer_t = OpTypeStruct %ushort
8043 %_ptr_PushConstant_PerViewConstantBuffer_t = OpTypePointer PushConstant %PerViewConstantBuffer_t
8044 %_ = OpVariable %_ptr_PushConstant_PerViewConstantBuffer_t PushConstant
8045 %_ptr_PushConstant_ushort = OpTypePointer PushConstant %ushort
8046 %_ptr_UniformConstant_16 = OpTypePointer UniformConstant %16
8047 %25 = OpTypeSampler
8048 %_ptr_UniformConstant_25 = OpTypePointer UniformConstant %25
8049 %g_sAniso = OpVariable %_ptr_UniformConstant_25 UniformConstant
8050 %27 = OpTypeSampledImage %16
8051 %_ptr_Input_v2float = OpTypePointer Input %v2float
8052 %i_vTextureCoords = OpVariable %_ptr_Input_v2float Input
8053 %_ptr_Output_v4float = OpTypePointer Output %v4float
8054 %_entryPointOutput_vColor = OpVariable %_ptr_Output_v4float Output
8055 ;CHECK: %uint_0 = OpConstant %uint 0
8056 ;CHECK: %bool = OpTypeBool
8057 ;CHECK: %51 = OpTypeFunction %void %uint %uint %uint %uint
8058 ;CHECK:%_runtimearr_uint = OpTypeRuntimeArray %uint
8059 ;CHECK: %_struct_58 = OpTypeStruct %uint %_runtimearr_uint
8060 ;CHECK:%_ptr_StorageBuffer__struct_58 = OpTypePointer StorageBuffer %_struct_58
8061 ;CHECK: %60 = OpVariable %_ptr_StorageBuffer__struct_58 StorageBuffer
8062 ;CHECK:%_ptr_StorageBuffer_uint = OpTypePointer StorageBuffer %uint
8063 ;CHECK: %uint_10 = OpConstant %uint 10
8064 ;CHECK: %uint_4 = OpConstant %uint 4
8065 ;CHECK: %uint_1 = OpConstant %uint 1
8066 ;CHECK: %uint_23 = OpConstant %uint 23
8067 ;CHECK: %uint_2 = OpConstant %uint 2
8068 ;CHECK: %uint_3 = OpConstant %uint 3
8069 ;CHECK:%_ptr_Input_v4float = OpTypePointer Input %v4float
8070 ;CHECK:%gl_FragCoord = OpVariable %_ptr_Input_v4float Input
8071 ;CHECK: %v4uint = OpTypeVector %uint 4
8072 ;CHECK: %uint_5 = OpConstant %uint 5
8073 ;CHECK: %uint_7 = OpConstant %uint 7
8074 ;CHECK: %uint_8 = OpConstant %uint 8
8075 ;CHECK: %uint_9 = OpConstant %uint 9
8076 ;CHECK: %uint_60 = OpConstant %uint 60
8077 ;CHECK: %106 = OpConstantNull %v4float
8078 ;CHECK: %111 = OpTypeFunction %uint %uint %uint %uint %uint
8079 ;CHECK:%_struct_117 = OpTypeStruct %_runtimearr_uint
8080 ;CHECK:%_ptr_StorageBuffer__struct_117 = OpTypePointer StorageBuffer %_struct_117
8081 ;CHECK: %119 = OpVariable %_ptr_StorageBuffer__struct_117 StorageBuffer
8082 %MainPs = OpFunction %void None %10
8083 %30 = OpLabel
8084 ;CHECK: OpBranch %108
8085 ;CHECK: %108 = OpLabel
8086 ;CHECK: OpBranch %39
8087 ;CHECK: %39 = OpLabel
8088 %31 = OpLoad %v2float %i_vTextureCoords
8089 %32 = OpAccessChain %_ptr_PushConstant_ushort %_ %int_0
8090 %33 = OpLoad %ushort %32
8091 %34 = OpAccessChain %_ptr_UniformConstant_16 %g_tColor %33
8092 %35 = OpLoad %16 %34
8093 %36 = OpLoad %25 %g_sAniso
8094 %37 = OpSampledImage %27 %35 %36
8095 %38 = OpImageSampleImplicitLod %v4float %37 %31
8096 OpStore %_entryPointOutput_vColor %38
8097 ;CHECK-NOT: %38 = OpImageSampleImplicitLod %v4float %37 %31
8098 ;CHECK-NOT: OpStore %_entryPointOutput_vColor %38
8099 ;CHECK: %41 = OpUConvert %uint %33
8100 ;CHECK: %43 = OpULessThan %bool %41 %uint_128
8101 ;CHECK: OpSelectionMerge %44 None
8102 ;CHECK: OpBranchConditional %43 %45 %46
8103 ;CHECK: %45 = OpLabel
8104 ;CHECK: %47 = OpLoad %16 %34
8105 ;CHECK: %48 = OpSampledImage %27 %47 %36
8106 ;CHECK: %109 = OpUConvert %uint %33
8107 ;CHECK: %131 = OpFunctionCall %uint %110 %uint_0 %uint_0 %uint_0 %109
8108 ;CHECK: %132 = OpULessThan %bool %uint_0 %131
8109 ;CHECK: OpSelectionMerge %133 None
8110 ;CHECK: OpBranchConditional %132 %134 %135
8111 ;CHECK: %134 = OpLabel
8112 ;CHECK: %136 = OpLoad %16 %34
8113 ;CHECK: %137 = OpSampledImage %27 %136 %36
8114 ;CHECK: %138 = OpImageSampleImplicitLod %v4float %137 %31
8115 ;CHECK: OpBranch %133
8116 ;CHECK: %135 = OpLabel
8117 ;CHECK: %139 = OpUConvert %uint %33
8118 ;CHECK: %140 = OpFunctionCall %void %50 %uint_60 %uint_1 %139 %uint_0
8119 ;CHECK: OpBranch %133
8120 ;CHECK: %133 = OpLabel
8121 ;CHECK: %141 = OpPhi %v4float %138 %134 %106 %135
8122 ;CHECK: OpBranch %44
8123 ;CHECK: %46 = OpLabel
8124 ;CHECK: %105 = OpFunctionCall %void %50 %uint_60 %uint_0 %41 %uint_128
8125 ;CHECK: OpBranch %44
8126 ;CHECK: %44 = OpLabel
8127 ;CHECK: %107 = OpPhi %v4float %141 %133 %106 %46
8128 ;CHECK: OpStore %_entryPointOutput_vColor %107
8129 OpReturn
8130 OpFunctionEnd
8131 ;CHECK: %50 = OpFunction %void None %51
8132 ;CHECK: %52 = OpFunctionParameter %uint
8133 ;CHECK: %53 = OpFunctionParameter %uint
8134 ;CHECK: %54 = OpFunctionParameter %uint
8135 ;CHECK: %55 = OpFunctionParameter %uint
8136 ;CHECK: %56 = OpLabel
8137 ;CHECK: %62 = OpAccessChain %_ptr_StorageBuffer_uint %60 %uint_0
8138 ;CHECK: %65 = OpAtomicIAdd %uint %62 %uint_4 %uint_0 %uint_10
8139 ;CHECK: %66 = OpIAdd %uint %65 %uint_10
8140 ;CHECK: %67 = OpArrayLength %uint %60 1
8141 ;CHECK: %68 = OpULessThanEqual %bool %66 %67
8142 ;CHECK: OpSelectionMerge %69 None
8143 ;CHECK: OpBranchConditional %68 %70 %69
8144 ;CHECK: %70 = OpLabel
8145 ;CHECK: %71 = OpIAdd %uint %65 %uint_0
8146 ;CHECK: %73 = OpAccessChain %_ptr_StorageBuffer_uint %60 %uint_1 %71
8147 ;CHECK: OpStore %73 %uint_10
8148 ;CHECK: %75 = OpIAdd %uint %65 %uint_1
8149 ;CHECK: %76 = OpAccessChain %_ptr_StorageBuffer_uint %60 %uint_1 %75
8150 ;CHECK: OpStore %76 %uint_23
8151 ;CHECK: %78 = OpIAdd %uint %65 %uint_2
8152 ;CHECK: %79 = OpAccessChain %_ptr_StorageBuffer_uint %60 %uint_1 %78
8153 ;CHECK: OpStore %79 %52
8154 ;CHECK: %81 = OpIAdd %uint %65 %uint_3
8155 ;CHECK: %82 = OpAccessChain %_ptr_StorageBuffer_uint %60 %uint_1 %81
8156 ;CHECK: OpStore %82 %uint_4
8157 ;CHECK: %85 = OpLoad %v4float %gl_FragCoord
8158 ;CHECK: %87 = OpBitcast %v4uint %85
8159 ;CHECK: %88 = OpCompositeExtract %uint %87 0
8160 ;CHECK: %89 = OpIAdd %uint %65 %uint_4
8161 ;CHECK: %90 = OpAccessChain %_ptr_StorageBuffer_uint %60 %uint_1 %89
8162 ;CHECK: OpStore %90 %88
8163 ;CHECK: %91 = OpCompositeExtract %uint %87 1
8164 ;CHECK: %93 = OpIAdd %uint %65 %uint_5
8165 ;CHECK: %94 = OpAccessChain %_ptr_StorageBuffer_uint %60 %uint_1 %93
8166 ;CHECK: OpStore %94 %91
8167 ;CHECK: %96 = OpIAdd %uint %65 %uint_7
8168 ;CHECK: %97 = OpAccessChain %_ptr_StorageBuffer_uint %60 %uint_1 %96
8169 ;CHECK: OpStore %97 %53
8170 ;CHECK: %99 = OpIAdd %uint %65 %uint_8
8171 ;CHECK: %100 = OpAccessChain %_ptr_StorageBuffer_uint %60 %uint_1 %99
8172 ;CHECK: OpStore %100 %54
8173 ;CHECK: %102 = OpIAdd %uint %65 %uint_9
8174 ;CHECK: %103 = OpAccessChain %_ptr_StorageBuffer_uint %60 %uint_1 %102
8175 ;CHECK: OpStore %103 %55
8176 ;CHECK: OpBranch %69
8177 ;CHECK: %69 = OpLabel
8178 ;CHECK: OpReturn
8179 ;CHECK: OpFunctionEnd
8180 ;CHECK: %110 = OpFunction %uint None %111
8181 ;CHECK: %112 = OpFunctionParameter %uint
8182 ;CHECK: %113 = OpFunctionParameter %uint
8183 ;CHECK: %114 = OpFunctionParameter %uint
8184 ;CHECK: %115 = OpFunctionParameter %uint
8185 ;CHECK: %116 = OpLabel
8186 ;CHECK: %120 = OpAccessChain %_ptr_StorageBuffer_uint %119 %uint_0 %112
8187 ;CHECK: %121 = OpLoad %uint %120
8188 ;CHECK: %122 = OpIAdd %uint %121 %113
8189 ;CHECK: %123 = OpAccessChain %_ptr_StorageBuffer_uint %119 %uint_0 %122
8190 ;CHECK: %124 = OpLoad %uint %123
8191 ;CHECK: %125 = OpIAdd %uint %124 %114
8192 ;CHECK: %126 = OpAccessChain %_ptr_StorageBuffer_uint %119 %uint_0 %125
8193 ;CHECK: %127 = OpLoad %uint %126
8194 ;CHECK: %128 = OpIAdd %uint %127 %115
8195 ;CHECK: %129 = OpAccessChain %_ptr_StorageBuffer_uint %119 %uint_0 %128
8196 ;CHECK: %130 = OpLoad %uint %129
8197 ;CHECK: OpReturnValue %130
8198 ;CHECK: OpFunctionEnd
8199 )";
8200
8201 SetTargetEnv(SPV_ENV_VULKAN_1_2);
8202 SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
8203 SinglePassRunAndMatch<InstBindlessCheckPass>(text, true, 7u, 23u, true, true,
8204 false);
8205 }
8206
8207 TEST_F(InstBindlessTest, UniformArray16bitIdxRef) {
8208 // Check that uniform array ref with 16bit index does not go out-of-bounds.
8209 //
8210 // Texture2D g_tColor;
8211 // SamplerState g_sAniso;
8212 //
8213 // layout(push_constant) cbuffer PerViewPushConst_t { min16uint g_c; };
8214 //
8215 // struct PerBatchEnvMapConstantBuffer_t {
8216 // float4x3 g_matEnvMapWorldToLocal;
8217 // float4 g_vEnvironmentMapBoxMins;
8218 // float2 g_TexOff;
8219 // };
8220 //
8221 // cbuffer _BindlessFastEnvMapCB_PS_t {
8222 // PerBatchEnvMapConstantBuffer_t g_envMapConstants[128];
8223 // };
8224 //
8225 // struct PS_INPUT {
8226 // float2 vTextureCoords : TEXCOORD2;
8227 // };
8228 //
8229 // struct PS_OUTPUT {
8230 // float4 vColor : SV_Target0;
8231 // };
8232 //
8233 // PS_OUTPUT MainPs(PS_INPUT i) {
8234 // PS_OUTPUT ps_output;
8235 // float2 off;
8236 // float2 vtc;
8237 // off = g_envMapConstants[g_c].g_TexOff;
8238 // vtc = i.vTextureCoords.xy + off;
8239 // ps_output.vColor = g_tColor.Sample(g_sAniso, vtc);
8240 // return ps_output;
8241 // }
8242
8243 const std::string text = R"(
8244 OpCapability Shader
8245 OpCapability Int16
8246 OpCapability StoragePushConstant16
8247 ;CHECK: OpExtension "SPV_KHR_storage_buffer_storage_class"
8248 %1 = OpExtInstImport "GLSL.std.450"
8249 OpMemoryModel Logical GLSL450
8250 OpEntryPoint Fragment %MainPs "MainPs" %_ %__0 %g_tColor %g_sAniso %i_vTextureCoords %_entryPointOutput_vColor
8251 ;CHECK: OpEntryPoint Fragment %MainPs "MainPs" %_ %__0 %g_tColor %g_sAniso %i_vTextureCoords %_entryPointOutput_vColor %69 %97 %gl_FragCoord
8252 OpExecutionMode %MainPs OriginUpperLeft
8253 OpSource HLSL 500
8254 OpName %MainPs "MainPs"
8255 OpName %PerBatchEnvMapConstantBuffer_t "PerBatchEnvMapConstantBuffer_t"
8256 OpMemberName %PerBatchEnvMapConstantBuffer_t 0 "g_matEnvMapWorldToLocal"
8257 OpMemberName %PerBatchEnvMapConstantBuffer_t 1 "g_vEnvironmentMapBoxMins"
8258 OpMemberName %PerBatchEnvMapConstantBuffer_t 2 "g_TexOff"
8259 OpName %_BindlessFastEnvMapCB_PS_t "_BindlessFastEnvMapCB_PS_t"
8260 OpMemberName %_BindlessFastEnvMapCB_PS_t 0 "g_envMapConstants"
8261 OpName %_ ""
8262 OpName %PerViewPushConst_t "PerViewPushConst_t"
8263 OpMemberName %PerViewPushConst_t 0 "g_c"
8264 OpName %__0 ""
8265 OpName %g_tColor "g_tColor"
8266 OpName %g_sAniso "g_sAniso"
8267 OpName %i_vTextureCoords "i.vTextureCoords"
8268 OpName %_entryPointOutput_vColor "@entryPointOutput.vColor"
8269 OpMemberDecorate %PerBatchEnvMapConstantBuffer_t 0 RowMajor
8270 OpMemberDecorate %PerBatchEnvMapConstantBuffer_t 0 Offset 0
8271 OpMemberDecorate %PerBatchEnvMapConstantBuffer_t 0 MatrixStride 16
8272 OpMemberDecorate %PerBatchEnvMapConstantBuffer_t 1 Offset 48
8273 OpMemberDecorate %PerBatchEnvMapConstantBuffer_t 2 Offset 64
8274 OpDecorate %_arr_PerBatchEnvMapConstantBuffer_t_uint_128 ArrayStride 80
8275 OpMemberDecorate %_BindlessFastEnvMapCB_PS_t 0 Offset 0
8276 OpDecorate %_BindlessFastEnvMapCB_PS_t Block
8277 OpDecorate %_ DescriptorSet 0
8278 OpDecorate %_ Binding 0
8279 OpMemberDecorate %PerViewPushConst_t 0 Offset 0
8280 OpDecorate %PerViewPushConst_t Block
8281 OpDecorate %g_tColor DescriptorSet 0
8282 OpDecorate %g_tColor Binding 0
8283 OpDecorate %g_sAniso DescriptorSet 0
8284 OpDecorate %g_sAniso Binding 0
8285 OpDecorate %i_vTextureCoords Location 0
8286 OpDecorate %_entryPointOutput_vColor Location 0
8287 ;CHECK: OpDecorate %_runtimearr_uint ArrayStride 4
8288 ;CHECK: OpDecorate %_struct_67 Block
8289 ;CHECK: OpMemberDecorate %_struct_67 0 Offset 0
8290 ;CHECK: OpDecorate %69 DescriptorSet 7
8291 ;CHECK: OpDecorate %69 Binding 1
8292 ;CHECK: OpDecorate %_struct_95 Block
8293 ;CHECK: OpMemberDecorate %_struct_95 0 Offset 0
8294 ;CHECK: OpMemberDecorate %_struct_95 1 Offset 4
8295 ;CHECK: OpDecorate %97 DescriptorSet 7
8296 ;CHECK: OpDecorate %97 Binding 0
8297 ;CHECK: OpDecorate %gl_FragCoord BuiltIn FragCoord
8298 %void = OpTypeVoid
8299 %14 = OpTypeFunction %void
8300 %float = OpTypeFloat 32
8301 %v2float = OpTypeVector %float 2
8302 %v4float = OpTypeVector %float 4
8303 %v3float = OpTypeVector %float 3
8304 %mat4v3float = OpTypeMatrix %v3float 4
8305 %PerBatchEnvMapConstantBuffer_t = OpTypeStruct %mat4v3float %v4float %v2float
8306 %uint = OpTypeInt 32 0
8307 %uint_128 = OpConstant %uint 128
8308 %_arr_PerBatchEnvMapConstantBuffer_t_uint_128 = OpTypeArray %PerBatchEnvMapConstantBuffer_t %uint_128
8309 %_BindlessFastEnvMapCB_PS_t = OpTypeStruct %_arr_PerBatchEnvMapConstantBuffer_t_uint_128
8310 %_ptr_Uniform__BindlessFastEnvMapCB_PS_t = OpTypePointer Uniform %_BindlessFastEnvMapCB_PS_t
8311 %_ = OpVariable %_ptr_Uniform__BindlessFastEnvMapCB_PS_t Uniform
8312 %int = OpTypeInt 32 1
8313 %int_0 = OpConstant %int 0
8314 %ushort = OpTypeInt 16 0
8315 %PerViewPushConst_t = OpTypeStruct %ushort
8316 %_ptr_PushConstant_PerViewPushConst_t = OpTypePointer PushConstant %PerViewPushConst_t
8317 %__0 = OpVariable %_ptr_PushConstant_PerViewPushConst_t PushConstant
8318 %_ptr_PushConstant_ushort = OpTypePointer PushConstant %ushort
8319 %int_2 = OpConstant %int 2
8320 %_ptr_Uniform_v2float = OpTypePointer Uniform %v2float
8321 %30 = OpTypeImage %float 2D 0 0 0 1 Unknown
8322 %_ptr_UniformConstant_30 = OpTypePointer UniformConstant %30
8323 %g_tColor = OpVariable %_ptr_UniformConstant_30 UniformConstant
8324 %32 = OpTypeSampler
8325 %_ptr_UniformConstant_32 = OpTypePointer UniformConstant %32
8326 %g_sAniso = OpVariable %_ptr_UniformConstant_32 UniformConstant
8327 %34 = OpTypeSampledImage %30
8328 %_ptr_Input_v2float = OpTypePointer Input %v2float
8329 %i_vTextureCoords = OpVariable %_ptr_Input_v2float Input
8330 %_ptr_Output_v4float = OpTypePointer Output %v4float
8331 %_entryPointOutput_vColor = OpVariable %_ptr_Output_v4float Output
8332 ;CHECK: %uint_0 = OpConstant %uint 0
8333 ;CHECK: %uint_80 = OpConstant %uint 80
8334 ;CHECK: %uint_64 = OpConstant %uint 64
8335 ;CHECK: %uint_7 = OpConstant %uint 7
8336 ;CHECK: %uint_1 = OpConstant %uint 1
8337 ;CHECK: %61 = OpTypeFunction %uint %uint %uint %uint
8338 ;CHECK:%_runtimearr_uint = OpTypeRuntimeArray %uint
8339 ;CHECK: %_struct_67 = OpTypeStruct %_runtimearr_uint
8340 ;CHECK:%_ptr_StorageBuffer__struct_67 = OpTypePointer StorageBuffer %_struct_67
8341 ;CHECK: %69 = OpVariable %_ptr_StorageBuffer__struct_67 StorageBuffer
8342 ;CHECK:%_ptr_StorageBuffer_uint = OpTypePointer StorageBuffer %uint
8343 ;CHECK: %bool = OpTypeBool
8344 ;CHECK: %uint_3 = OpConstant %uint 3
8345 ;CHECK: %88 = OpTypeFunction %void %uint %uint %uint %uint %uint
8346 ;CHECK: %_struct_95 = OpTypeStruct %uint %_runtimearr_uint
8347 ;CHECK:%_ptr_StorageBuffer__struct_95 = OpTypePointer StorageBuffer %_struct_95
8348 ;CHECK: %97 = OpVariable %_ptr_StorageBuffer__struct_95 StorageBuffer
8349 ;CHECK: %uint_11 = OpConstant %uint 11
8350 ;CHECK: %uint_4 = OpConstant %uint 4
8351 ;CHECK: %uint_23 = OpConstant %uint 23
8352 ;CHECK: %uint_2 = OpConstant %uint 2
8353 ;CHECK:%_ptr_Input_v4float = OpTypePointer Input %v4float
8354 ;CHECK:%gl_FragCoord = OpVariable %_ptr_Input_v4float Input
8355 ;CHECK: %v4uint = OpTypeVector %uint 4
8356 ;CHECK: %uint_5 = OpConstant %uint 5
8357 ;CHECK: %uint_8 = OpConstant %uint 8
8358 ;CHECK: %uint_9 = OpConstant %uint 9
8359 ;CHECK: %uint_10 = OpConstant %uint 10
8360 ;CHECK: %uint_81 = OpConstant %uint 81
8361 ;CHECK: %142 = OpConstantNull %v2float
8362 %MainPs = OpFunction %void None %14
8363 %37 = OpLabel
8364 ;CHECK: %79 = OpFunctionCall %uint %60 %uint_1 %uint_0 %uint_0
8365 ;CHECK: OpBranch %49
8366 ;CHECK: %49 = OpLabel
8367 ;CHECK: OpBranch %48
8368 ;CHECK: %48 = OpLabel
8369 %38 = OpLoad %v2float %i_vTextureCoords
8370 %39 = OpAccessChain %_ptr_PushConstant_ushort %__0 %int_0
8371 %40 = OpLoad %ushort %39
8372 %41 = OpAccessChain %_ptr_Uniform_v2float %_ %int_0 %40 %int_2
8373 %42 = OpLoad %v2float %41
8374 %43 = OpFAdd %v2float %38 %42
8375 ;CHECK-NOT: %42 = OpLoad %v2float %41
8376 ;CHECK-NOT: %43 = OpFAdd %v2float %38 %42
8377 ;CHECK: %52 = OpUConvert %uint %40
8378 ;CHECK: %53 = OpIMul %uint %uint_80 %52
8379 ;CHECK: %54 = OpIAdd %uint %uint_0 %53
8380 ;CHECK: %56 = OpIAdd %uint %54 %uint_64
8381 ;CHECK: %58 = OpIAdd %uint %56 %uint_7
8382 ;CHECK: %81 = OpULessThan %bool %58 %79
8383 ;CHECK: OpSelectionMerge %83 None
8384 ;CHECK: OpBranchConditional %81 %84 %85
8385 ;CHECK: %84 = OpLabel
8386 ;CHECK: %86 = OpLoad %v2float %41
8387 ;CHECK: OpBranch %83
8388 ;CHECK: %85 = OpLabel
8389 ;CHECK: %141 = OpFunctionCall %void %87 %uint_81 %uint_3 %uint_0 %58 %79
8390 ;CHECK: OpBranch %83
8391 ;CHECK: %83 = OpLabel
8392 ;CHECK: %143 = OpPhi %v2float %86 %84 %142 %85
8393 ;CHECK: %43 = OpFAdd %v2float %38 %143
8394 %44 = OpLoad %30 %g_tColor
8395 %45 = OpLoad %32 %g_sAniso
8396 %46 = OpSampledImage %34 %44 %45
8397 %47 = OpImageSampleImplicitLod %v4float %46 %43
8398 OpStore %_entryPointOutput_vColor %47
8399 OpReturn
8400 OpFunctionEnd
8401 ;CHECK: %60 = OpFunction %uint None %61
8402 ;CHECK: %62 = OpFunctionParameter %uint
8403 ;CHECK: %63 = OpFunctionParameter %uint
8404 ;CHECK: %64 = OpFunctionParameter %uint
8405 ;CHECK: %65 = OpLabel
8406 ;CHECK: %71 = OpAccessChain %_ptr_StorageBuffer_uint %69 %uint_0 %62
8407 ;CHECK: %72 = OpLoad %uint %71
8408 ;CHECK: %73 = OpIAdd %uint %72 %63
8409 ;CHECK: %74 = OpAccessChain %_ptr_StorageBuffer_uint %69 %uint_0 %73
8410 ;CHECK: %75 = OpLoad %uint %74
8411 ;CHECK: %76 = OpIAdd %uint %75 %64
8412 ;CHECK: %77 = OpAccessChain %_ptr_StorageBuffer_uint %69 %uint_0 %76
8413 ;CHECK: %78 = OpLoad %uint %77
8414 ;CHECK: OpReturnValue %78
8415 ;CHECK: OpFunctionEnd
8416 ;CHECK: %87 = OpFunction %void None %88
8417 ;CHECK: %89 = OpFunctionParameter %uint
8418 ;CHECK: %90 = OpFunctionParameter %uint
8419 ;CHECK: %91 = OpFunctionParameter %uint
8420 ;CHECK: %92 = OpFunctionParameter %uint
8421 ;CHECK: %93 = OpFunctionParameter %uint
8422 ;CHECK: %94 = OpLabel
8423 ;CHECK: %98 = OpAccessChain %_ptr_StorageBuffer_uint %97 %uint_0
8424 ;CHECK: %101 = OpAtomicIAdd %uint %98 %uint_4 %uint_0 %uint_11
8425 ;CHECK: %102 = OpIAdd %uint %101 %uint_11
8426 ;CHECK: %103 = OpArrayLength %uint %97 1
8427 ;CHECK: %104 = OpULessThanEqual %bool %102 %103
8428 ;CHECK: OpSelectionMerge %105 None
8429 ;CHECK: OpBranchConditional %104 %106 %105
8430 ;CHECK: %106 = OpLabel
8431 ;CHECK: %107 = OpIAdd %uint %101 %uint_0
8432 ;CHECK: %108 = OpAccessChain %_ptr_StorageBuffer_uint %97 %uint_1 %107
8433 ;CHECK: OpStore %108 %uint_11
8434 ;CHECK: %110 = OpIAdd %uint %101 %uint_1
8435 ;CHECK: %111 = OpAccessChain %_ptr_StorageBuffer_uint %97 %uint_1 %110
8436 ;CHECK: OpStore %111 %uint_23
8437 ;CHECK: %113 = OpIAdd %uint %101 %uint_2
8438 ;CHECK: %114 = OpAccessChain %_ptr_StorageBuffer_uint %97 %uint_1 %113
8439 ;CHECK: OpStore %114 %89
8440 ;CHECK: %115 = OpIAdd %uint %101 %uint_3
8441 ;CHECK: %116 = OpAccessChain %_ptr_StorageBuffer_uint %97 %uint_1 %115
8442 ;CHECK: OpStore %116 %uint_4
8443 ;CHECK: %119 = OpLoad %v4float %gl_FragCoord
8444 ;CHECK: %121 = OpBitcast %v4uint %119
8445 ;CHECK: %122 = OpCompositeExtract %uint %121 0
8446 ;CHECK: %123 = OpIAdd %uint %101 %uint_4
8447 ;CHECK: %124 = OpAccessChain %_ptr_StorageBuffer_uint %97 %uint_1 %123
8448 ;CHECK: OpStore %124 %122
8449 ;CHECK: %125 = OpCompositeExtract %uint %121 1
8450 ;CHECK: %127 = OpIAdd %uint %101 %uint_5
8451 ;CHECK: %128 = OpAccessChain %_ptr_StorageBuffer_uint %97 %uint_1 %127
8452 ;CHECK: OpStore %128 %125
8453 ;CHECK: %129 = OpIAdd %uint %101 %uint_7
8454 ;CHECK: %130 = OpAccessChain %_ptr_StorageBuffer_uint %97 %uint_1 %129
8455 ;CHECK: OpStore %130 %90
8456 ;CHECK: %132 = OpIAdd %uint %101 %uint_8
8457 ;CHECK: %133 = OpAccessChain %_ptr_StorageBuffer_uint %97 %uint_1 %132
8458 ;CHECK: OpStore %133 %91
8459 ;CHECK: %135 = OpIAdd %uint %101 %uint_9
8460 ;CHECK: %136 = OpAccessChain %_ptr_StorageBuffer_uint %97 %uint_1 %135
8461 ;CHECK: OpStore %136 %92
8462 ;CHECK: %138 = OpIAdd %uint %101 %uint_10
8463 ;CHECK: %139 = OpAccessChain %_ptr_StorageBuffer_uint %97 %uint_1 %138
8464 ;CHECK: OpStore %139 %93
8465 ;CHECK: OpBranch %105
8466 ;CHECK: %105 = OpLabel
8467 ;CHECK: OpReturn
8468 ;CHECK: OpFunctionEnd
8469 )";
8470
8471 SetTargetEnv(SPV_ENV_VULKAN_1_2);
8472 SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
8473 SinglePassRunAndMatch<InstBindlessCheckPass>(text, true, 7u, 23u, false,
8474 false, true);
8475 }
8476
8477 // TODO(greg-lunarg): Add tests to verify handling of these cases:
8478 //
8479 // Compute shader
8480 // Geometry shader
8481 // Tesselation control shader
8482 // Tesselation eval shader
8483 // OpImage
8484 // SampledImage variable
8485
8486 } // namespace
8487 } // namespace opt
8488 } // namespace spvtools