Debian Codegraph @

test/val/val_decoration_test.cpp

raw
1 // Copyright (c) 2017 Google Inc.
2 // Modifications Copyright (C) 2024 Advanced Micro Devices, Inc. All rights
3 // reserved.
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16
17 // Validation tests for decorations
18
19 #include <string>
20 #include <vector>
21
22 #include "gmock/gmock.h"
23 #include "source/val/decoration.h"
24 #include "spirv-tools/libspirv.h"
25 #include "test/unit_spirv.h"
26 #include "test/val/val_code_generator.h"
27 #include "test/val/val_fixtures.h"
28
29 namespace spvtools {
30 namespace val {
31 namespace {
32
33 using ::testing::Combine;
34 using ::testing::Eq;
35 using ::testing::HasSubstr;
36 using ::testing::Values;
37
38 struct TestResult {
39 TestResult(spv_result_t in_validation_result = SPV_SUCCESS,
40 const std::string& in_error_str = "")
41 : validation_result(in_validation_result), error_str(in_error_str) {}
42 spv_result_t validation_result;
43 const std::string error_str;
44 };
45
46 using ValidateDecorations = spvtest::ValidateBase<bool>;
47 using ValidateDecorationString = spvtest::ValidateBase<std::string>;
48 using ValidateVulkanCombineDecorationResult =
49 spvtest::ValidateBase<std::tuple<const char*, const char*, TestResult>>;
50
51 TEST_F(ValidateDecorations, ValidateOpDecorateRegistration) {
52 std::string spirv = R"(
53 OpCapability Shader
54 OpCapability Linkage
55 OpMemoryModel Logical GLSL450
56 OpDecorate %1 Location 4
57 OpDecorate %1 Centroid
58 %2 = OpTypeFloat 32
59 %3 = OpTypePointer Output %2
60 %1 = OpVariable %3 Output
61 ; Since %1 is used first in Decoration, it gets id 1.
62 )";
63 const uint32_t id = 1;
64 CompileSuccessfully(spirv);
65 EXPECT_EQ(SPV_SUCCESS, ValidateAndRetrieveValidationState());
66 // Must have 2 decorations.
67 EXPECT_THAT(
68 vstate_->id_decorations(id),
69 Eq(std::set<Decoration>{Decoration(spv::Decoration::Location, {4}),
70 Decoration(spv::Decoration::Centroid)}));
71 }
72
73 TEST_F(ValidateDecorations, ValidateOpMemberDecorateRegistration) {
74 std::string spirv = R"(
75 OpCapability Shader
76 OpCapability Linkage
77 OpMemoryModel Logical GLSL450
78 OpDecorate %_arr_double_uint_6 ArrayStride 4
79 OpMemberDecorate %_struct_115 2 NonReadable
80 OpMemberDecorate %_struct_115 2 Offset 2
81 OpDecorate %_struct_115 BufferBlock
82 %float = OpTypeFloat 32
83 %uint = OpTypeInt 32 0
84 %uint_6 = OpConstant %uint 6
85 %_arr_double_uint_6 = OpTypeArray %float %uint_6
86 %_struct_115 = OpTypeStruct %float %float %_arr_double_uint_6
87 )";
88 CompileSuccessfully(spirv);
89 EXPECT_EQ(SPV_SUCCESS, ValidateAndRetrieveValidationState());
90
91 // The array must have 1 decoration.
92 const uint32_t arr_id = 1;
93 EXPECT_THAT(
94 vstate_->id_decorations(arr_id),
95 Eq(std::set<Decoration>{Decoration(spv::Decoration::ArrayStride, {4})}));
96
97 // The struct must have 3 decorations.
98 const uint32_t struct_id = 2;
99 EXPECT_THAT(
100 vstate_->id_decorations(struct_id),
101 Eq(std::set<Decoration>{Decoration(spv::Decoration::NonReadable, {}, 2),
102 Decoration(spv::Decoration::Offset, {2}, 2),
103 Decoration(spv::Decoration::BufferBlock)}));
104 }
105
106 TEST_F(ValidateDecorations, ValidateOpMemberDecorateOutOfBound) {
107 std::string spirv = R"(
108 OpCapability Shader
109 OpMemoryModel Logical GLSL450
110 OpEntryPoint Fragment %1 "Main"
111 OpExecutionMode %1 OriginUpperLeft
112 OpMemberDecorate %_struct_2 1 RelaxedPrecision
113 %void = OpTypeVoid
114 %4 = OpTypeFunction %void
115 %float = OpTypeFloat 32
116 %_struct_2 = OpTypeStruct %float
117 %1 = OpFunction %void None %4
118 %6 = OpLabel
119 OpReturn
120 OpFunctionEnd
121 )";
122 CompileSuccessfully(spirv);
123 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateAndRetrieveValidationState());
124 EXPECT_THAT(getDiagnosticString(),
125 HasSubstr("Index 1 provided in OpMemberDecorate for struct <id> "
126 "'2[%_struct_2]' is out of bounds. The structure has 1 "
127 "members. Largest valid index is 0."));
128 }
129
130 TEST_F(ValidateDecorations, ValidateGroupDecorateRegistration) {
131 std::string spirv = R"(
132 OpCapability Shader
133 OpCapability Linkage
134 OpMemoryModel Logical GLSL450
135 OpDecorate %1 DescriptorSet 0
136 OpDecorate %1 RelaxedPrecision
137 OpDecorate %1 Restrict
138 %1 = OpDecorationGroup
139 OpGroupDecorate %1 %2 %3
140 OpGroupDecorate %1 %4
141 %float = OpTypeFloat 32
142 %_runtimearr_float = OpTypeRuntimeArray %float
143 %_struct_9 = OpTypeStruct %_runtimearr_float
144 %_ptr_Uniform__struct_9 = OpTypePointer Uniform %_struct_9
145 %2 = OpVariable %_ptr_Uniform__struct_9 Uniform
146 %_struct_10 = OpTypeStruct %_runtimearr_float
147 %_ptr_Uniform__struct_10 = OpTypePointer Uniform %_struct_10
148 %3 = OpVariable %_ptr_Uniform__struct_10 Uniform
149 %_struct_11 = OpTypeStruct %_runtimearr_float
150 %_ptr_Uniform__struct_11 = OpTypePointer Uniform %_struct_11
151 %4 = OpVariable %_ptr_Uniform__struct_11 Uniform
152 )";
153 CompileSuccessfully(spirv);
154 EXPECT_EQ(SPV_SUCCESS, ValidateAndRetrieveValidationState());
155
156 // Decoration group has 3 decorations.
157 auto expected_decorations =
158 std::set<Decoration>{Decoration(spv::Decoration::DescriptorSet, {0}),
159 Decoration(spv::Decoration::RelaxedPrecision),
160 Decoration(spv::Decoration::Restrict)};
161
162 // Decoration group is applied to id 1, 2, 3, and 4. Note that id 1 (which is
163 // the decoration group id) also has all the decorations.
164 EXPECT_THAT(vstate_->id_decorations(1), Eq(expected_decorations));
165 EXPECT_THAT(vstate_->id_decorations(2), Eq(expected_decorations));
166 EXPECT_THAT(vstate_->id_decorations(3), Eq(expected_decorations));
167 EXPECT_THAT(vstate_->id_decorations(4), Eq(expected_decorations));
168 }
169
170 TEST_F(ValidateDecorations, ValidateGroupMemberDecorateRegistration) {
171 std::string spirv = R"(
172 OpCapability Shader
173 OpCapability Linkage
174 OpMemoryModel Logical GLSL450
175 OpDecorate %1 Offset 3
176 %1 = OpDecorationGroup
177 OpGroupMemberDecorate %1 %_struct_1 3 %_struct_2 3 %_struct_3 3
178 %float = OpTypeFloat 32
179 %_runtimearr = OpTypeRuntimeArray %float
180 %_struct_1 = OpTypeStruct %float %float %float %_runtimearr
181 %_struct_2 = OpTypeStruct %float %float %float %_runtimearr
182 %_struct_3 = OpTypeStruct %float %float %float %_runtimearr
183 )";
184 CompileSuccessfully(spirv);
185 EXPECT_EQ(SPV_SUCCESS, ValidateAndRetrieveValidationState());
186 // Decoration group has 1 decoration.
187 auto expected_decorations =
188 std::set<Decoration>{Decoration(spv::Decoration::Offset, {3}, 3)};
189
190 // Decoration group is applied to id 2, 3, and 4.
191 EXPECT_THAT(vstate_->id_decorations(2), Eq(expected_decorations));
192 EXPECT_THAT(vstate_->id_decorations(3), Eq(expected_decorations));
193 EXPECT_THAT(vstate_->id_decorations(4), Eq(expected_decorations));
194 }
195
196 TEST_F(ValidateDecorations, LinkageImportUsedForInitializedVariableBad) {
197 std::string spirv = R"(
198 OpCapability Shader
199 OpCapability Linkage
200 OpMemoryModel Logical GLSL450
201 OpDecorate %target LinkageAttributes "link_ptr" Import
202 %float = OpTypeFloat 32
203 %_ptr_float = OpTypePointer Uniform %float
204 %zero = OpConstantNull %float
205 %target = OpVariable %_ptr_float Uniform %zero
206 )";
207 CompileSuccessfully(spirv);
208 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateAndRetrieveValidationState());
209 EXPECT_THAT(getDiagnosticString(),
210 HasSubstr("A module-scope OpVariable with initialization value "
211 "cannot be marked with the Import Linkage Type."));
212 }
213 TEST_F(ValidateDecorations, LinkageExportUsedForInitializedVariableGood) {
214 std::string spirv = R"(
215 OpCapability Shader
216 OpCapability Linkage
217 OpMemoryModel Logical GLSL450
218 OpDecorate %target LinkageAttributes "link_ptr" Export
219 %float = OpTypeFloat 32
220 %_ptr_float = OpTypePointer Uniform %float
221 %zero = OpConstantNull %float
222 %target = OpVariable %_ptr_float Uniform %zero
223 )";
224 CompileSuccessfully(spirv);
225 EXPECT_EQ(SPV_SUCCESS, ValidateAndRetrieveValidationState());
226 }
227
228 TEST_F(ValidateDecorations, StructAllMembersHaveBuiltInDecorationsGood) {
229 std::string spirv = R"(
230 OpCapability Shader
231 OpCapability Linkage
232 OpMemoryModel Logical GLSL450
233 OpDecorate %_struct_1 Block
234 OpMemberDecorate %_struct_1 0 BuiltIn Position
235 OpMemberDecorate %_struct_1 1 BuiltIn Position
236 OpMemberDecorate %_struct_1 2 BuiltIn Position
237 OpMemberDecorate %_struct_1 3 BuiltIn Position
238 %float = OpTypeFloat 32
239 %_runtimearr = OpTypeRuntimeArray %float
240 %_struct_1 = OpTypeStruct %float %float %float %_runtimearr
241 )";
242 CompileSuccessfully(spirv);
243 EXPECT_EQ(SPV_SUCCESS, ValidateAndRetrieveValidationState());
244 }
245
246 TEST_F(ValidateDecorations, MixedBuiltInDecorationsBad) {
247 std::string spirv = R"(
248 OpCapability Shader
249 OpCapability Linkage
250 OpMemoryModel Logical GLSL450
251 OpDecorate %_struct_1 Block
252 OpMemberDecorate %_struct_1 0 BuiltIn Position
253 OpMemberDecorate %_struct_1 1 BuiltIn Position
254 %float = OpTypeFloat 32
255 %_runtimearr = OpTypeRuntimeArray %float
256 %_struct_1 = OpTypeStruct %float %float %float %_runtimearr
257 )";
258 CompileSuccessfully(spirv);
259 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateAndRetrieveValidationState());
260 EXPECT_THAT(
261 getDiagnosticString(),
262 HasSubstr("When BuiltIn decoration is applied to a structure-type "
263 "member, all members of that structure type must also be "
264 "decorated with BuiltIn (No allowed mixing of built-in "
265 "variables and non-built-in variables within a single "
266 "structure). Structure id 1 does not meet this requirement."));
267 }
268
269 TEST_F(ValidateDecorations, StructContainsBuiltInStructBad) {
270 std::string spirv = R"(
271 OpCapability Shader
272 OpCapability Linkage
273 OpMemoryModel Logical GLSL450
274 OpDecorate %_struct_1 Block
275 OpMemberDecorate %_struct_1 0 BuiltIn Position
276 OpMemberDecorate %_struct_1 1 BuiltIn Position
277 OpMemberDecorate %_struct_1 2 BuiltIn Position
278 OpMemberDecorate %_struct_1 3 BuiltIn Position
279 %float = OpTypeFloat 32
280 %_runtimearr = OpTypeRuntimeArray %float
281 %_struct_1 = OpTypeStruct %float %float %float %_runtimearr
282 %_struct_2 = OpTypeStruct %_struct_1
283 )";
284 CompileSuccessfully(spirv);
285 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateAndRetrieveValidationState());
286 EXPECT_THAT(
287 getDiagnosticString(),
288 HasSubstr("Structure <id> '1[%_struct_1]' contains members with "
289 "BuiltIn decoration. Therefore this structure may not "
290 "be contained as a member of another structure type. "
291 "Structure <id> '4[%_struct_4]' contains structure <id> "
292 "'1[%_struct_1]'."));
293 }
294
295 TEST_F(ValidateDecorations, StructContainsNonBuiltInStructGood) {
296 std::string spirv = R"(
297 OpCapability Shader
298 OpCapability Linkage
299 OpMemoryModel Logical GLSL450
300 %float = OpTypeFloat 32
301 %_struct_1 = OpTypeStruct %float
302 %_struct_2 = OpTypeStruct %_struct_1
303 )";
304 CompileSuccessfully(spirv);
305 EXPECT_EQ(SPV_SUCCESS, ValidateAndRetrieveValidationState());
306 }
307
308 TEST_F(ValidateDecorations, MultipleBuiltInObjectsConsumedByOpEntryPointBad) {
309 std::string spirv = R"(
310 OpCapability Shader
311 OpCapability Geometry
312 OpMemoryModel Logical GLSL450
313 OpEntryPoint Geometry %main "main" %in_1 %in_2
314 OpExecutionMode %main InputPoints
315 OpExecutionMode %main OutputPoints
316 OpDecorate %struct_1 Block
317 OpDecorate %struct_2 Block
318 OpMemberDecorate %struct_1 0 BuiltIn InvocationId
319 OpMemberDecorate %struct_2 0 BuiltIn Position
320 %int = OpTypeInt 32 1
321 %void = OpTypeVoid
322 %func = OpTypeFunction %void
323 %float = OpTypeFloat 32
324 %struct_1 = OpTypeStruct %int
325 %struct_2 = OpTypeStruct %float
326 %ptr_builtin_1 = OpTypePointer Input %struct_1
327 %ptr_builtin_2 = OpTypePointer Input %struct_2
328 %in_1 = OpVariable %ptr_builtin_1 Input
329 %in_2 = OpVariable %ptr_builtin_2 Input
330 %main = OpFunction %void None %func
331 %5 = OpLabel
332 OpReturn
333 OpFunctionEnd
334 )";
335 CompileSuccessfully(spirv);
336 EXPECT_EQ(SPV_ERROR_INVALID_BINARY, ValidateAndRetrieveValidationState());
337 EXPECT_THAT(getDiagnosticString(),
338 HasSubstr("There must be at most one object per Storage Class "
339 "that can contain a structure type containing members "
340 "decorated with BuiltIn, consumed per entry-point."));
341 }
342
343 TEST_F(ValidateDecorations,
344 OneBuiltInObjectPerStorageClassConsumedByOpEntryPointGood) {
345 std::string spirv = R"(
346 OpCapability Shader
347 OpCapability Geometry
348 OpMemoryModel Logical GLSL450
349 OpEntryPoint Geometry %main "main" %in_1 %out_1
350 OpExecutionMode %main InputPoints
351 OpExecutionMode %main OutputPoints
352 OpDecorate %struct_1 Block
353 OpDecorate %struct_2 Block
354 OpMemberDecorate %struct_1 0 BuiltIn InvocationId
355 OpMemberDecorate %struct_2 0 BuiltIn Position
356 %int = OpTypeInt 32 1
357 %void = OpTypeVoid
358 %func = OpTypeFunction %void
359 %float = OpTypeFloat 32
360 %struct_1 = OpTypeStruct %int
361 %struct_2 = OpTypeStruct %float
362 %ptr_builtin_1 = OpTypePointer Input %struct_1
363 %ptr_builtin_2 = OpTypePointer Output %struct_2
364 %in_1 = OpVariable %ptr_builtin_1 Input
365 %out_1 = OpVariable %ptr_builtin_2 Output
366 %main = OpFunction %void None %func
367 %5 = OpLabel
368 OpReturn
369 OpFunctionEnd
370 )";
371 CompileSuccessfully(spirv);
372 EXPECT_EQ(SPV_SUCCESS, ValidateAndRetrieveValidationState());
373 }
374
375 TEST_F(ValidateDecorations, NoBuiltInObjectsConsumedByOpEntryPointGood) {
376 std::string spirv = R"(
377 OpCapability Shader
378 OpCapability Geometry
379 OpMemoryModel Logical GLSL450
380 OpEntryPoint Geometry %main "main" %in_1 %out_1
381 OpExecutionMode %main InputPoints
382 OpExecutionMode %main OutputPoints
383 %int = OpTypeInt 32 1
384 %void = OpTypeVoid
385 %func = OpTypeFunction %void
386 %float = OpTypeFloat 32
387 %struct_1 = OpTypeStruct %int
388 %struct_2 = OpTypeStruct %float
389 %ptr_builtin_1 = OpTypePointer Input %struct_1
390 %ptr_builtin_2 = OpTypePointer Output %struct_2
391 %in_1 = OpVariable %ptr_builtin_1 Input
392 %out_1 = OpVariable %ptr_builtin_2 Output
393 %main = OpFunction %void None %func
394 %5 = OpLabel
395 OpReturn
396 OpFunctionEnd
397 )";
398 CompileSuccessfully(spirv);
399 EXPECT_EQ(SPV_SUCCESS, ValidateAndRetrieveValidationState());
400 }
401
402 TEST_F(ValidateDecorations, EntryPointFunctionHasLinkageAttributeBad) {
403 std::string spirv = R"(
404 OpCapability Shader
405 OpCapability Linkage
406 OpMemoryModel Logical GLSL450
407 OpEntryPoint GLCompute %main "main"
408 OpDecorate %main LinkageAttributes "import_main" Import
409 %1 = OpTypeVoid
410 %2 = OpTypeFunction %1
411 %main = OpFunction %1 None %2
412 %4 = OpLabel
413 OpReturn
414 OpFunctionEnd
415 )";
416 CompileSuccessfully(spirv.c_str());
417 EXPECT_EQ(SPV_ERROR_INVALID_BINARY, ValidateInstructions());
418 EXPECT_THAT(
419 getDiagnosticString(),
420 HasSubstr("The LinkageAttributes Decoration (Linkage name: import_main) "
421 "cannot be applied to function id 1 because it is targeted by "
422 "an OpEntryPoint instruction."));
423 }
424
425 TEST_F(ValidateDecorations, FunctionDeclarationWithoutImportLinkageBad) {
426 std::string spirv = R"(
427 OpCapability Shader
428 OpCapability Linkage
429 OpMemoryModel Logical GLSL450
430 %void = OpTypeVoid
431 %func = OpTypeFunction %void
432 %main = OpFunction %void None %func
433 OpFunctionEnd
434 )";
435 CompileSuccessfully(spirv);
436 EXPECT_EQ(SPV_ERROR_INVALID_BINARY, ValidateAndRetrieveValidationState());
437 EXPECT_THAT(
438 getDiagnosticString(),
439 HasSubstr("Function declaration (id 3) must have a LinkageAttributes "
440 "decoration with the Import Linkage type."));
441 }
442
443 TEST_F(ValidateDecorations, FunctionDeclarationWithImportLinkageGood) {
444 std::string spirv = R"(
445 OpCapability Shader
446 OpCapability Linkage
447 OpMemoryModel Logical GLSL450
448 OpDecorate %main LinkageAttributes "link_fn" Import
449 %void = OpTypeVoid
450 %func = OpTypeFunction %void
451 %main = OpFunction %void None %func
452 OpFunctionEnd
453 )";
454 CompileSuccessfully(spirv);
455 EXPECT_EQ(SPV_SUCCESS, ValidateAndRetrieveValidationState());
456 }
457
458 TEST_F(ValidateDecorations, FunctionDeclarationWithExportLinkageBad) {
459 std::string spirv = R"(
460 OpCapability Shader
461 OpCapability Linkage
462 OpMemoryModel Logical GLSL450
463 OpDecorate %main LinkageAttributes "link_fn" Export
464 %void = OpTypeVoid
465 %func = OpTypeFunction %void
466 %main = OpFunction %void None %func
467 OpFunctionEnd
468 )";
469 CompileSuccessfully(spirv);
470 EXPECT_EQ(SPV_ERROR_INVALID_BINARY, ValidateAndRetrieveValidationState());
471 EXPECT_THAT(
472 getDiagnosticString(),
473 HasSubstr("Function declaration (id 1) must have a LinkageAttributes "
474 "decoration with the Import Linkage type."));
475 }
476
477 TEST_F(ValidateDecorations, FunctionDefinitionWithImportLinkageBad) {
478 std::string spirv = R"(
479 OpCapability Shader
480 OpCapability Linkage
481 OpMemoryModel Logical GLSL450
482 OpDecorate %main LinkageAttributes "link_fn" Import
483 %void = OpTypeVoid
484 %func = OpTypeFunction %void
485 %main = OpFunction %void None %func
486 %label = OpLabel
487 OpReturn
488 OpFunctionEnd
489 )";
490 CompileSuccessfully(spirv);
491 EXPECT_EQ(SPV_ERROR_INVALID_BINARY, ValidateAndRetrieveValidationState());
492 EXPECT_THAT(getDiagnosticString(),
493 HasSubstr("Function definition (id 1) may not be decorated with "
494 "Import Linkage type."));
495 }
496
497 TEST_F(ValidateDecorations, FunctionDefinitionWithoutImportLinkageGood) {
498 std::string spirv = R"(
499 OpCapability Shader
500 OpCapability Linkage
501 OpMemoryModel Logical GLSL450
502 %void = OpTypeVoid
503 %func = OpTypeFunction %void
504 %main = OpFunction %void None %func
505 %label = OpLabel
506 OpReturn
507 OpFunctionEnd
508 )";
509 CompileSuccessfully(spirv);
510 EXPECT_EQ(SPV_SUCCESS, ValidateAndRetrieveValidationState());
511 }
512
513 TEST_F(ValidateDecorations, BuiltinVariablesGoodVulkan) {
514 const spv_target_env env = SPV_ENV_VULKAN_1_0;
515 std::string spirv = R"(
516 OpCapability Shader
517 OpMemoryModel Logical GLSL450
518 OpEntryPoint Fragment %main "main" %gl_FragCoord %_entryPointOutput
519 OpExecutionMode %main OriginUpperLeft
520 OpSource HLSL 500
521 OpDecorate %gl_FragCoord BuiltIn FragCoord
522 OpDecorate %_entryPointOutput Location 0
523 %void = OpTypeVoid
524 %3 = OpTypeFunction %void
525 %float = OpTypeFloat 32
526 %v4float = OpTypeVector %float 4
527 %float_0 = OpConstant %float 0
528 %14 = OpConstantComposite %v4float %float_0 %float_0 %float_0 %float_0
529 %_ptr_Input_v4float = OpTypePointer Input %v4float
530 %gl_FragCoord = OpVariable %_ptr_Input_v4float Input
531 %_ptr_Output_v4float = OpTypePointer Output %v4float
532 %_entryPointOutput = OpVariable %_ptr_Output_v4float Output
533 %main = OpFunction %void None %3
534 %5 = OpLabel
535 OpStore %_entryPointOutput %14
536 OpReturn
537 OpFunctionEnd
538 )";
539
540 CompileSuccessfully(spirv, env);
541 EXPECT_EQ(SPV_SUCCESS, ValidateAndRetrieveValidationState(env));
542 }
543
544 TEST_F(ValidateDecorations, BuiltinVariablesWithLocationDecorationVulkan) {
545 const spv_target_env env = SPV_ENV_VULKAN_1_0;
546 std::string spirv = R"(
547 OpCapability Shader
548 OpMemoryModel Logical GLSL450
549 OpEntryPoint Fragment %main "main" %gl_FragCoord %_entryPointOutput
550 OpExecutionMode %main OriginUpperLeft
551 OpSource HLSL 500
552 OpDecorate %gl_FragCoord BuiltIn FragCoord
553 OpDecorate %gl_FragCoord Location 0
554 OpDecorate %_entryPointOutput Location 0
555 %void = OpTypeVoid
556 %3 = OpTypeFunction %void
557 %float = OpTypeFloat 32
558 %v4float = OpTypeVector %float 4
559 %float_0 = OpConstant %float 0
560 %14 = OpConstantComposite %v4float %float_0 %float_0 %float_0 %float_0
561 %_ptr_Input_v4float = OpTypePointer Input %v4float
562 %gl_FragCoord = OpVariable %_ptr_Input_v4float Input
563 %_ptr_Output_v4float = OpTypePointer Output %v4float
564 %_entryPointOutput = OpVariable %_ptr_Output_v4float Output
565 %main = OpFunction %void None %3
566 %5 = OpLabel
567 OpStore %_entryPointOutput %14
568 OpReturn
569 OpFunctionEnd
570 )";
571
572 CompileSuccessfully(spirv, env);
573 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateAndRetrieveValidationState(env));
574 EXPECT_THAT(getDiagnosticString(),
575 AnyVUID("VUID-StandaloneSpirv-Location-04915"));
576 EXPECT_THAT(getDiagnosticString(),
577 HasSubstr("A BuiltIn variable (id 2) cannot have any Location or "
578 "Component decorations"));
579 }
580 TEST_F(ValidateDecorations, BuiltinVariablesWithComponentDecorationVulkan) {
581 const spv_target_env env = SPV_ENV_VULKAN_1_0;
582 std::string spirv = R"(
583 OpCapability Shader
584 OpMemoryModel Logical GLSL450
585 OpEntryPoint Fragment %main "main" %gl_FragCoord %_entryPointOutput
586 OpExecutionMode %main OriginUpperLeft
587 OpSource HLSL 500
588 OpDecorate %gl_FragCoord BuiltIn FragCoord
589 OpDecorate %gl_FragCoord Component 0
590 OpDecorate %_entryPointOutput Location 0
591 %void = OpTypeVoid
592 %3 = OpTypeFunction %void
593 %float = OpTypeFloat 32
594 %v4float = OpTypeVector %float 4
595 %float_0 = OpConstant %float 0
596 %14 = OpConstantComposite %v4float %float_0 %float_0 %float_0 %float_0
597 %_ptr_Input_v4float = OpTypePointer Input %v4float
598 %gl_FragCoord = OpVariable %_ptr_Input_v4float Input
599 %_ptr_Output_v4float = OpTypePointer Output %v4float
600 %_entryPointOutput = OpVariable %_ptr_Output_v4float Output
601 %main = OpFunction %void None %3
602 %5 = OpLabel
603 OpStore %_entryPointOutput %14
604 OpReturn
605 OpFunctionEnd
606 )";
607
608 CompileSuccessfully(spirv, env);
609 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateAndRetrieveValidationState(env));
610 EXPECT_THAT(getDiagnosticString(),
611 AnyVUID("VUID-StandaloneSpirv-Location-04915"));
612 EXPECT_THAT(getDiagnosticString(),
613 HasSubstr("A BuiltIn variable (id 2) cannot have any Location or "
614 "Component decorations"));
615 }
616
617 TEST_F(ValidateDecorations, LocationDecorationOnNumericTypeBad) {
618 const spv_target_env env = SPV_ENV_VULKAN_1_0;
619 std::string spirv = R"(
620 OpCapability Shader
621 OpMemoryModel Logical GLSL450
622 OpEntryPoint Fragment %main "main" %fragCoord
623 OpExecutionMode %main OriginUpperLeft
624 OpDecorate %fragCoord Location 0
625 OpDecorate %v4float Location 1
626 %void = OpTypeVoid
627 %voidfn = OpTypeFunction %void
628 %float = OpTypeFloat 32
629 %v4float = OpTypeVector %float 4
630 %ptr_v4float = OpTypePointer Output %v4float
631 %fragCoord = OpVariable %ptr_v4float Output
632 %non_interface = OpVariable %ptr_v4float Output
633 %main = OpFunction %void None %voidfn
634 %label = OpLabel
635 OpReturn
636 OpFunctionEnd
637 )";
638
639 CompileSuccessfully(spirv, env);
640 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateAndRetrieveValidationState(env));
641 EXPECT_THAT(getDiagnosticString(),
642 HasSubstr("Location decoration on target <id> '3[%v4float]' must "
643 "be a variable"));
644 }
645
646 TEST_F(ValidateDecorations, LocationDecorationOnStructBad) {
647 const spv_target_env env = SPV_ENV_VULKAN_1_0;
648 std::string spirv = R"(
649 OpCapability Shader
650 OpMemoryModel Logical GLSL450
651 OpEntryPoint Fragment %main "main" %fragCoord
652 OpExecutionMode %main OriginUpperLeft
653 OpDecorate %fragCoord Location 0
654 OpDecorate %struct Location 1
655 %void = OpTypeVoid
656 %voidfn = OpTypeFunction %void
657 %float = OpTypeFloat 32
658 %struct = OpTypeStruct %float
659 %v4float = OpTypeVector %float 4
660 %ptr_v4float = OpTypePointer Output %v4float
661 %fragCoord = OpVariable %ptr_v4float Output
662 %non_interface = OpVariable %ptr_v4float Output
663 %main = OpFunction %void None %voidfn
664 %label = OpLabel
665 OpReturn
666 OpFunctionEnd
667 )";
668
669 CompileSuccessfully(spirv, env);
670 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateAndRetrieveValidationState(env));
671 EXPECT_THAT(getDiagnosticString(),
672 HasSubstr("Location decoration on target <id> '3[%_struct_3]' "
673 "must be a variable"));
674 }
675
676 TEST_F(ValidateDecorations,
677 LocationDecorationUnusedNonInterfaceVariableVulkan_Ignored) {
678 const spv_target_env env = SPV_ENV_VULKAN_1_0;
679 std::string spirv = R"(
680 OpCapability Shader
681 OpMemoryModel Logical GLSL450
682 OpEntryPoint Fragment %main "main" %fragCoord
683 OpExecutionMode %main OriginUpperLeft
684 OpSource GLSL 450
685 OpDecorate %fragCoord Location 0
686 OpDecorate %non_interface Location 1
687 %void = OpTypeVoid
688 %voidfn = OpTypeFunction %void
689 %float = OpTypeFloat 32
690 %v4float = OpTypeVector %float 4
691 %ptr_v4float = OpTypePointer Output %v4float
692 %fragCoord = OpVariable %ptr_v4float Output
693 %non_interface = OpVariable %ptr_v4float Output
694 %main = OpFunction %void None %voidfn
695 %label = OpLabel
696 OpReturn
697 OpFunctionEnd
698 )";
699
700 CompileSuccessfully(spirv, env);
701 EXPECT_EQ(SPV_SUCCESS, ValidateAndRetrieveValidationState(env));
702 EXPECT_EQ(getDiagnosticString(), "");
703 }
704
705 TEST_F(ValidateDecorations,
706 LocationDecorationNonInterfaceStructVulkan_Ignored) {
707 const spv_target_env env = SPV_ENV_VULKAN_1_0;
708 std::string spirv = R"(
709 OpCapability Shader
710 OpMemoryModel Logical GLSL450
711 OpEntryPoint Fragment %main "main" %fragCoord
712 OpExecutionMode %main OriginUpperLeft
713 OpDecorate %fragCoord Location 0
714 OpMemberDecorate %block 0 Location 2
715 OpMemberDecorate %block 0 Component 1
716 OpDecorate %block Block
717 %void = OpTypeVoid
718 %voidfn = OpTypeFunction %void
719 %float = OpTypeFloat 32
720 %vec3 = OpTypeVector %float 3
721 %outvar_ptr = OpTypePointer Output %vec3
722 %fragCoord = OpVariable %outvar_ptr Output
723 %block = OpTypeStruct %vec3
724 %invar_ptr = OpTypePointer Input %block
725 %non_interface = OpVariable %invar_ptr Input
726 %main = OpFunction %void None %voidfn
727 %label = OpLabel
728 OpReturn
729 OpFunctionEnd
730 )";
731
732 CompileSuccessfully(spirv, env);
733 EXPECT_EQ(SPV_SUCCESS, ValidateAndRetrieveValidationState(env));
734 EXPECT_EQ(getDiagnosticString(), "");
735 }
736
737 TEST_F(ValidateDecorations, LocationDecorationNonInterfaceStructVulkanGood) {
738 const spv_target_env env = SPV_ENV_VULKAN_1_0;
739 std::string spirv = R"(
740 OpCapability Shader
741 OpMemoryModel Logical GLSL450
742 OpEntryPoint Fragment %main "main" %fragCoord %interface
743 OpExecutionMode %main OriginUpperLeft
744 OpDecorate %fragCoord Location 0
745 OpMemberDecorate %block 0 Location 2
746 OpMemberDecorate %block 0 Component 1
747 OpDecorate %block Block
748 %void = OpTypeVoid
749 %voidfn = OpTypeFunction %void
750 %float = OpTypeFloat 32
751 %vec3 = OpTypeVector %float 3
752 %outvar_ptr = OpTypePointer Output %vec3
753 %fragCoord = OpVariable %outvar_ptr Output
754 %block = OpTypeStruct %vec3
755 %invar_ptr = OpTypePointer Input %block
756 %interface = OpVariable %invar_ptr Input ;; this variable is unused. Ignore it
757 %main = OpFunction %void None %voidfn
758 %label = OpLabel
759 OpReturn
760 OpFunctionEnd
761 )";
762
763 CompileSuccessfully(spirv, env);
764 EXPECT_EQ(SPV_SUCCESS, ValidateAndRetrieveValidationState(env));
765 }
766
767 TEST_F(ValidateDecorations, LocationDecorationVariableNonStructVulkanBad) {
768 const spv_target_env env = SPV_ENV_VULKAN_1_0;
769 std::string spirv = R"(
770 OpCapability Shader
771 OpMemoryModel Logical GLSL450
772 OpEntryPoint Fragment %main "main" %fragCoord %nonblock_var
773 OpExecutionMode %main OriginUpperLeft
774 OpSource GLSL 450
775 OpDecorate %fragCoord Location 0
776 %void = OpTypeVoid
777 %voidfn = OpTypeFunction %void
778 %float = OpTypeFloat 32
779 %v4float = OpTypeVector %float 4
780 %ptr_v4float = OpTypePointer Output %v4float
781 %fragCoord = OpVariable %ptr_v4float Output
782 %nonblock_var = OpVariable %ptr_v4float Output
783 %main = OpFunction %void None %voidfn
784 %label = OpLabel
785 OpReturn
786 OpFunctionEnd
787 )";
788
789 CompileSuccessfully(spirv, env);
790 EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateAndRetrieveValidationState(env));
791 EXPECT_THAT(getDiagnosticString(),
792 AnyVUID("VUID-StandaloneSpirv-Location-04916"));
793 EXPECT_THAT(getDiagnosticString(),
794 HasSubstr("Variable must be decorated with a location"));
795 }
796
797 TEST_F(ValidateDecorations, LocationDecorationVariableStructNoBlockVulkanBad) {
798 const spv_target_env env = SPV_ENV_VULKAN_1_0;
799 std::string spirv = R"(
800 OpCapability Shader
801 OpMemoryModel Logical GLSL450
802 OpEntryPoint Fragment %main "main" %fragCoord %block_var
803 OpExecutionMode %main OriginUpperLeft
804 OpSource GLSL 450
805 OpDecorate %fragCoord Location 0
806 %void = OpTypeVoid
807 %voidfn = OpTypeFunction %void
808 %float = OpTypeFloat 32
809 %v4float = OpTypeVector %float 4
810 %ptr_v4float = OpTypePointer Output %v4float
811 %fragCoord = OpVariable %ptr_v4float Output
812 %block = OpTypeStruct %v4float
813 %block_ptr = OpTypePointer Output %block
814 %block_var = OpVariable %block_ptr Output
815 %main = OpFunction %void None %voidfn
816 %label = OpLabel
817 OpReturn
818 OpFunctionEnd
819 )";
820
821 CompileSuccessfully(spirv, env);
822 EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateAndRetrieveValidationState(env));
823 EXPECT_THAT(getDiagnosticString(),
824 AnyVUID("VUID-StandaloneSpirv-Location-04917"));
825 EXPECT_THAT(getDiagnosticString(),
826 HasSubstr("Variable must be decorated with a location"));
827 }
828
829 TEST_F(ValidateDecorations, LocationDecorationVariableNoBlockVulkanGood) {
830 const spv_target_env env = SPV_ENV_VULKAN_1_0;
831 std::string spirv = R"(
832 OpCapability Shader
833 OpMemoryModel Logical GLSL450
834 OpEntryPoint Fragment %main "main" %fragCoord %block_var
835 OpExecutionMode %main OriginUpperLeft
836 OpSource GLSL 450
837 OpDecorate %fragCoord Location 0
838 OpDecorate %block_var Location 1
839 %void = OpTypeVoid
840 %voidfn = OpTypeFunction %void
841 %float = OpTypeFloat 32
842 %v4float = OpTypeVector %float 4
843 %ptr_v4float = OpTypePointer Output %v4float
844 %fragCoord = OpVariable %ptr_v4float Output
845 %block = OpTypeStruct %v4float
846 %block_ptr = OpTypePointer Output %block
847 %block_var = OpVariable %block_ptr Output
848 %main = OpFunction %void None %voidfn
849 %label = OpLabel
850 OpReturn
851 OpFunctionEnd
852 )";
853
854 CompileSuccessfully(spirv, env);
855 EXPECT_EQ(SPV_SUCCESS, ValidateAndRetrieveValidationState());
856 }
857
858 TEST_F(ValidateDecorations, LocationDecorationVariableExtraMemeberVulkan) {
859 const spv_target_env env = SPV_ENV_VULKAN_1_0;
860 std::string spirv = R"(
861 OpCapability Shader
862 OpMemoryModel Logical GLSL450
863 OpEntryPoint Fragment %main "main" %fragCoord %block_var
864 OpExecutionMode %main OriginUpperLeft
865 OpSource GLSL 450
866 OpDecorate %fragCoord Location 0
867 OpDecorate %block Block
868 OpDecorate %block_var Location 1
869 OpMemberDecorate %block 0 Location 1
870 %void = OpTypeVoid
871 %voidfn = OpTypeFunction %void
872 %float = OpTypeFloat 32
873 %v4float = OpTypeVector %float 4
874 %ptr_v4float = OpTypePointer Output %v4float
875 %fragCoord = OpVariable %ptr_v4float Output
876 %block = OpTypeStruct %v4float
877 %block_ptr = OpTypePointer Output %block
878 %block_var = OpVariable %block_ptr Output
879 %main = OpFunction %void None %voidfn
880 %label = OpLabel
881 OpReturn
882 OpFunctionEnd
883
884 )";
885
886 CompileSuccessfully(spirv, env);
887 EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateAndRetrieveValidationState(env));
888 EXPECT_THAT(getDiagnosticString(),
889 AnyVUID("VUID-StandaloneSpirv-Location-04918"));
890 EXPECT_THAT(getDiagnosticString(),
891 HasSubstr("Members cannot be assigned a location"));
892 }
893
894 TEST_F(ValidateDecorations, LocationDecorationVariableMissingMemeberVulkan) {
895 const spv_target_env env = SPV_ENV_VULKAN_1_0;
896 std::string spirv = R"(
897 OpCapability Shader
898 OpMemoryModel Logical GLSL450
899 OpEntryPoint Fragment %main "main" %fragCoord %block_var
900 OpExecutionMode %main OriginUpperLeft
901 OpSource GLSL 450
902 OpDecorate %fragCoord Location 0
903 OpDecorate %block Block
904 OpMemberDecorate %block 0 Location 1
905 %void = OpTypeVoid
906 %voidfn = OpTypeFunction %void
907 %float = OpTypeFloat 32
908 %v4float = OpTypeVector %float 4
909 %ptr_v4float = OpTypePointer Output %v4float
910 %fragCoord = OpVariable %ptr_v4float Output
911 %block = OpTypeStruct %v4float %v4float
912 %block_ptr = OpTypePointer Output %block
913 %block_var = OpVariable %block_ptr Output
914 %main = OpFunction %void None %voidfn
915 %label = OpLabel
916 OpReturn
917 OpFunctionEnd
918 )";
919
920 CompileSuccessfully(spirv, env);
921 EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateAndRetrieveValidationState(env));
922 EXPECT_THAT(getDiagnosticString(),
923 AnyVUID("VUID-StandaloneSpirv-Location-04919"));
924 EXPECT_THAT(getDiagnosticString(),
925 HasSubstr("Member index 1 is missing a location assignment"));
926 }
927
928 TEST_F(ValidateDecorations, LocationDecorationVariableOnlyMemeberVulkanGood) {
929 const spv_target_env env = SPV_ENV_VULKAN_1_0;
930 std::string spirv = R"(
931 OpCapability Shader
932 OpMemoryModel Logical GLSL450
933 OpEntryPoint Fragment %main "main" %fragCoord %block_var
934 OpExecutionMode %main OriginUpperLeft
935 OpSource GLSL 450
936 OpDecorate %fragCoord Location 0
937 OpDecorate %block Block
938 OpMemberDecorate %block 0 Location 1
939 OpMemberDecorate %block 1 Location 4
940 %void = OpTypeVoid
941 %voidfn = OpTypeFunction %void
942 %float = OpTypeFloat 32
943 %v4float = OpTypeVector %float 4
944 %ptr_v4float = OpTypePointer Output %v4float
945 %fragCoord = OpVariable %ptr_v4float Output
946 %block = OpTypeStruct %v4float %v4float
947 %block_ptr = OpTypePointer Output %block
948 %block_var = OpVariable %block_ptr Output
949 %main = OpFunction %void None %voidfn
950 %label = OpLabel
951 OpReturn
952 OpFunctionEnd
953 )";
954
955 CompileSuccessfully(spirv, env);
956 EXPECT_EQ(SPV_SUCCESS, ValidateAndRetrieveValidationState());
957 }
958
959 // #version 440
960 // #extension GL_EXT_nonuniform_qualifier : enable
961 // layout(binding = 1) uniform sampler2D s2d[];
962 // layout(location = 0) in nonuniformEXT int i;
963 // void main()
964 // {
965 // vec4 v = texture(s2d[i], vec2(0.3));
966 // }
967 TEST_F(ValidateDecorations, RuntimeArrayOfDescriptorSetsIsAllowed) {
968 const spv_target_env env = SPV_ENV_VULKAN_1_0;
969 std::string spirv = R"(
970 OpCapability Shader
971 OpCapability ShaderNonUniformEXT
972 OpCapability RuntimeDescriptorArrayEXT
973 OpCapability SampledImageArrayNonUniformIndexingEXT
974 OpExtension "SPV_EXT_descriptor_indexing"
975 %1 = OpExtInstImport "GLSL.std.450"
976 OpMemoryModel Logical GLSL450
977 OpEntryPoint Vertex %main "main" %i
978 OpSource GLSL 440
979 OpSourceExtension "GL_EXT_nonuniform_qualifier"
980 OpName %main "main"
981 OpName %v "v"
982 OpName %s2d "s2d"
983 OpName %i "i"
984 OpDecorate %s2d DescriptorSet 0
985 OpDecorate %s2d Binding 1
986 OpDecorate %i Location 0
987 OpDecorate %i NonUniformEXT
988 OpDecorate %18 NonUniformEXT
989 OpDecorate %21 NonUniformEXT
990 %void = OpTypeVoid
991 %3 = OpTypeFunction %void
992 %float = OpTypeFloat 32
993 %v4float = OpTypeVector %float 4
994 %_ptr_Function_v4float = OpTypePointer Function %v4float
995 %10 = OpTypeImage %float 2D 0 0 0 1 Unknown
996 %11 = OpTypeSampledImage %10
997 %_runtimearr_11 = OpTypeRuntimeArray %11
998 %_ptr_Uniform__runtimearr_11 = OpTypePointer Uniform %_runtimearr_11
999 %s2d = OpVariable %_ptr_Uniform__runtimearr_11 Uniform
1000 %int = OpTypeInt 32 1
1001 %_ptr_Input_int = OpTypePointer Input %int
1002 %i = OpVariable %_ptr_Input_int Input
1003 %_ptr_Uniform_11 = OpTypePointer Uniform %11
1004 %v2float = OpTypeVector %float 2
1005 %float_0_300000012 = OpConstant %float 0.300000012
1006 %24 = OpConstantComposite %v2float %float_0_300000012 %float_0_300000012
1007 %float_0 = OpConstant %float 0
1008 %main = OpFunction %void None %3
1009 %5 = OpLabel
1010 %v = OpVariable %_ptr_Function_v4float Function
1011 %18 = OpLoad %int %i
1012 %20 = OpAccessChain %_ptr_Uniform_11 %s2d %18
1013 %21 = OpLoad %11 %20
1014 %26 = OpImageSampleExplicitLod %v4float %21 %24 Lod %float_0
1015 OpStore %v %26
1016 OpReturn
1017 OpFunctionEnd
1018 )";
1019 CompileSuccessfully(spirv, env);
1020 EXPECT_EQ(SPV_SUCCESS, ValidateAndRetrieveValidationState());
1021 }
1022
1023 TEST_F(ValidateDecorations, BlockDecoratingArrayBad) {
1024 std::string spirv = R"(
1025 OpCapability Shader
1026 %1 = OpExtInstImport "GLSL.std.450"
1027 OpMemoryModel Logical GLSL450
1028 OpEntryPoint GLCompute %main "main"
1029 OpExecutionMode %main LocalSize 1 1 1
1030 OpSource GLSL 430
1031 OpDecorate %Output Block
1032 %void = OpTypeVoid
1033 %3 = OpTypeFunction %void
1034 %float = OpTypeFloat 32
1035 %int = OpTypeInt 32 1
1036 %int_3 = OpConstant %int 3
1037 %Output = OpTypeArray %float %int_3
1038 %_ptr_Uniform_Output = OpTypePointer Uniform %Output
1039 %dataOutput = OpVariable %_ptr_Uniform_Output Uniform
1040 %main = OpFunction %void None %3
1041 %5 = OpLabel
1042 OpReturn
1043 OpFunctionEnd
1044 )";
1045
1046 CompileSuccessfully(spirv);
1047 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateAndRetrieveValidationState());
1048 EXPECT_THAT(getDiagnosticString(), HasSubstr("must be a structure type"));
1049 }
1050
1051 TEST_F(ValidateDecorations, BlockDecoratingIntBad) {
1052 std::string spirv = R"(
1053 OpCapability Shader
1054 %1 = OpExtInstImport "GLSL.std.450"
1055 OpMemoryModel Logical GLSL450
1056 OpEntryPoint GLCompute %main "main"
1057 OpExecutionMode %main LocalSize 1 1 1
1058 OpSource GLSL 430
1059 OpDecorate %Output Block
1060 %void = OpTypeVoid
1061 %3 = OpTypeFunction %void
1062 %Output = OpTypeInt 32 1
1063 %_ptr_Uniform_Output = OpTypePointer Uniform %Output
1064 %dataOutput = OpVariable %_ptr_Uniform_Output Uniform
1065 %main = OpFunction %void None %3
1066 %5 = OpLabel
1067 OpReturn
1068 OpFunctionEnd
1069 )";
1070
1071 CompileSuccessfully(spirv);
1072 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateAndRetrieveValidationState());
1073 EXPECT_THAT(getDiagnosticString(), HasSubstr("must be a structure type"));
1074 }
1075
1076 TEST_F(ValidateDecorations, BlockMissingOffsetBad) {
1077 std::string spirv = R"(
1078 OpCapability Shader
1079 %1 = OpExtInstImport "GLSL.std.450"
1080 OpMemoryModel Logical GLSL450
1081 OpEntryPoint GLCompute %main "main"
1082 OpExecutionMode %main LocalSize 1 1 1
1083 OpSource GLSL 430
1084 OpDecorate %Output Block
1085 %void = OpTypeVoid
1086 %3 = OpTypeFunction %void
1087 %float = OpTypeFloat 32
1088 %Output = OpTypeStruct %float
1089 %_ptr_Uniform_Output = OpTypePointer Uniform %Output
1090 %dataOutput = OpVariable %_ptr_Uniform_Output Uniform
1091 %main = OpFunction %void None %3
1092 %5 = OpLabel
1093 OpReturn
1094 OpFunctionEnd
1095 )";
1096
1097 CompileSuccessfully(spirv);
1098 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateAndRetrieveValidationState());
1099 EXPECT_THAT(getDiagnosticString(),
1100 HasSubstr("must be explicitly laid out with Offset decorations"));
1101 }
1102
1103 TEST_F(ValidateDecorations, BufferBlockMissingOffsetBad) {
1104 std::string spirv = R"(
1105 OpCapability Shader
1106 %1 = OpExtInstImport "GLSL.std.450"
1107 OpMemoryModel Logical GLSL450
1108 OpEntryPoint GLCompute %main "main"
1109 OpExecutionMode %main LocalSize 1 1 1
1110 OpSource GLSL 430
1111 OpDecorate %Output BufferBlock
1112 %void = OpTypeVoid
1113 %3 = OpTypeFunction %void
1114 %float = OpTypeFloat 32
1115 %Output = OpTypeStruct %float
1116 %_ptr_Uniform_Output = OpTypePointer Uniform %Output
1117 %dataOutput = OpVariable %_ptr_Uniform_Output Uniform
1118 %main = OpFunction %void None %3
1119 %5 = OpLabel
1120 OpReturn
1121 OpFunctionEnd
1122 )";
1123
1124 CompileSuccessfully(spirv);
1125 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateAndRetrieveValidationState());
1126 EXPECT_THAT(getDiagnosticString(),
1127 HasSubstr("must be explicitly laid out with Offset decorations"));
1128 }
1129
1130 TEST_F(ValidateDecorations, BlockNestedStructMissingOffsetBad) {
1131 std::string spirv = R"(
1132 OpCapability Shader
1133 %1 = OpExtInstImport "GLSL.std.450"
1134 OpMemoryModel Logical GLSL450
1135 OpEntryPoint GLCompute %main "main"
1136 OpExecutionMode %main LocalSize 1 1 1
1137 OpSource GLSL 430
1138 OpMemberDecorate %S 0 Offset 0
1139 OpMemberDecorate %Output 0 Offset 0
1140 OpMemberDecorate %Output 1 Offset 16
1141 OpMemberDecorate %Output 2 Offset 32
1142 OpDecorate %Output Block
1143 %void = OpTypeVoid
1144 %3 = OpTypeFunction %void
1145 %float = OpTypeFloat 32
1146 %v4float = OpTypeVector %float 4
1147 %v3float = OpTypeVector %float 3
1148 %int = OpTypeInt 32 1
1149 %S = OpTypeStruct %v3float %int
1150 %Output = OpTypeStruct %float %v4float %S
1151 %_ptr_Uniform_Output = OpTypePointer Uniform %Output
1152 %dataOutput = OpVariable %_ptr_Uniform_Output Uniform
1153 %main = OpFunction %void None %3
1154 %5 = OpLabel
1155 OpReturn
1156 OpFunctionEnd
1157 )";
1158
1159 CompileSuccessfully(spirv);
1160 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateAndRetrieveValidationState());
1161 EXPECT_THAT(getDiagnosticString(),
1162 HasSubstr("must be explicitly laid out with Offset decorations"));
1163 }
1164
1165 TEST_F(ValidateDecorations, BufferBlockNestedStructMissingOffsetBad) {
1166 std::string spirv = R"(
1167 OpCapability Shader
1168 %1 = OpExtInstImport "GLSL.std.450"
1169 OpMemoryModel Logical GLSL450
1170 OpEntryPoint GLCompute %main "main"
1171 OpExecutionMode %main LocalSize 1 1 1
1172 OpSource GLSL 430
1173 OpMemberDecorate %S 0 Offset 0
1174 OpMemberDecorate %Output 0 Offset 0
1175 OpMemberDecorate %Output 1 Offset 16
1176 OpMemberDecorate %Output 2 Offset 32
1177 OpDecorate %Output BufferBlock
1178 %void = OpTypeVoid
1179 %3 = OpTypeFunction %void
1180 %float = OpTypeFloat 32
1181 %v4float = OpTypeVector %float 4
1182 %v3float = OpTypeVector %float 3
1183 %int = OpTypeInt 32 1
1184 %S = OpTypeStruct %v3float %int
1185 %Output = OpTypeStruct %float %v4float %S
1186 %_ptr_Uniform_Output = OpTypePointer Uniform %Output
1187 %dataOutput = OpVariable %_ptr_Uniform_Output Uniform
1188 %main = OpFunction %void None %3
1189 %5 = OpLabel
1190 OpReturn
1191 OpFunctionEnd
1192 )";
1193
1194 CompileSuccessfully(spirv);
1195 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateAndRetrieveValidationState());
1196 EXPECT_THAT(getDiagnosticString(),
1197 HasSubstr("must be explicitly laid out with Offset decorations"));
1198 }
1199
1200 TEST_F(ValidateDecorations, BlockGLSLSharedBad) {
1201 std::string spirv = R"(
1202 OpCapability Shader
1203 %1 = OpExtInstImport "GLSL.std.450"
1204 OpMemoryModel Logical GLSL450
1205 OpEntryPoint GLCompute %main "main"
1206 OpExecutionMode %main LocalSize 1 1 1
1207 OpSource GLSL 430
1208 OpDecorate %Output Block
1209 OpDecorate %Output GLSLShared
1210 OpMemberDecorate %Output 0 Offset 0
1211 %void = OpTypeVoid
1212 %3 = OpTypeFunction %void
1213 %float = OpTypeFloat 32
1214 %Output = OpTypeStruct %float
1215 %_ptr_Uniform_Output = OpTypePointer Uniform %Output
1216 %dataOutput = OpVariable %_ptr_Uniform_Output Uniform
1217 %main = OpFunction %void None %3
1218 %5 = OpLabel
1219 OpReturn
1220 OpFunctionEnd
1221 )";
1222
1223 CompileSuccessfully(spirv);
1224 EXPECT_EQ(SPV_ERROR_INVALID_ID,
1225 ValidateAndRetrieveValidationState(SPV_ENV_VULKAN_1_0));
1226 EXPECT_THAT(
1227 getDiagnosticString(),
1228 HasSubstr(
1229 "'GLSLShared' is not valid for the Vulkan execution environment"));
1230 EXPECT_THAT(getDiagnosticString(),
1231 HasSubstr("[VUID-StandaloneSpirv-GLSLShared-04669]"));
1232 }
1233
1234 TEST_F(ValidateDecorations, BufferBlockGLSLSharedBad) {
1235 std::string spirv = R"(
1236 OpCapability Shader
1237 %1 = OpExtInstImport "GLSL.std.450"
1238 OpMemoryModel Logical GLSL450
1239 OpEntryPoint GLCompute %main "main"
1240 OpExecutionMode %main LocalSize 1 1 1
1241 OpSource GLSL 430
1242 OpDecorate %Output BufferBlock
1243 OpDecorate %Output GLSLShared
1244 OpMemberDecorate %Output 0 Offset 0
1245 %void = OpTypeVoid
1246 %3 = OpTypeFunction %void
1247 %float = OpTypeFloat 32
1248 %Output = OpTypeStruct %float
1249 %_ptr_Uniform_Output = OpTypePointer Uniform %Output
1250 %dataOutput = OpVariable %_ptr_Uniform_Output Uniform
1251 %main = OpFunction %void None %3
1252 %5 = OpLabel
1253 OpReturn
1254 OpFunctionEnd
1255 )";
1256
1257 CompileSuccessfully(spirv);
1258 EXPECT_EQ(SPV_ERROR_INVALID_ID,
1259 ValidateAndRetrieveValidationState(SPV_ENV_VULKAN_1_0));
1260 EXPECT_THAT(
1261 getDiagnosticString(),
1262 HasSubstr(
1263 "'GLSLShared' is not valid for the Vulkan execution environment"));
1264 EXPECT_THAT(getDiagnosticString(),
1265 HasSubstr("[VUID-StandaloneSpirv-GLSLShared-04669]"));
1266 }
1267
1268 TEST_F(ValidateDecorations, BlockNestedStructGLSLSharedBad) {
1269 std::string spirv = R"(
1270 OpCapability Shader
1271 %1 = OpExtInstImport "GLSL.std.450"
1272 OpMemoryModel Logical GLSL450
1273 OpEntryPoint GLCompute %main "main"
1274 OpExecutionMode %main LocalSize 1 1 1
1275 OpSource GLSL 430
1276 OpMemberDecorate %S 0 Offset 0
1277 OpDecorate %S GLSLShared
1278 OpMemberDecorate %Output 0 Offset 0
1279 OpMemberDecorate %Output 1 Offset 16
1280 OpMemberDecorate %Output 2 Offset 32
1281 OpDecorate %Output Block
1282 %void = OpTypeVoid
1283 %3 = OpTypeFunction %void
1284 %float = OpTypeFloat 32
1285 %v4float = OpTypeVector %float 4
1286 %int = OpTypeInt 32 1
1287 %S = OpTypeStruct %int
1288 %Output = OpTypeStruct %float %v4float %S
1289 %_ptr_Uniform_Output = OpTypePointer Uniform %Output
1290 %dataOutput = OpVariable %_ptr_Uniform_Output Uniform
1291 %main = OpFunction %void None %3
1292 %5 = OpLabel
1293 OpReturn
1294 OpFunctionEnd
1295 )";
1296
1297 CompileSuccessfully(spirv);
1298 EXPECT_EQ(SPV_ERROR_INVALID_ID,
1299 ValidateAndRetrieveValidationState(SPV_ENV_VULKAN_1_0));
1300 EXPECT_THAT(
1301 getDiagnosticString(),
1302 HasSubstr(
1303 "'GLSLShared' is not valid for the Vulkan execution environment"));
1304 EXPECT_THAT(getDiagnosticString(),
1305 HasSubstr("[VUID-StandaloneSpirv-GLSLShared-04669]"));
1306 }
1307
1308 TEST_F(ValidateDecorations, BufferBlockNestedStructGLSLSharedBad) {
1309 std::string spirv = R"(
1310 OpCapability Shader
1311 %1 = OpExtInstImport "GLSL.std.450"
1312 OpMemoryModel Logical GLSL450
1313 OpEntryPoint GLCompute %main "main"
1314 OpExecutionMode %main LocalSize 1 1 1
1315 OpSource GLSL 430
1316 OpMemberDecorate %S 0 Offset 0
1317 OpDecorate %S GLSLShared
1318 OpMemberDecorate %Output 0 Offset 0
1319 OpMemberDecorate %Output 1 Offset 16
1320 OpMemberDecorate %Output 2 Offset 32
1321 OpDecorate %Output BufferBlock
1322 %void = OpTypeVoid
1323 %3 = OpTypeFunction %void
1324 %float = OpTypeFloat 32
1325 %v4float = OpTypeVector %float 4
1326 %int = OpTypeInt 32 1
1327 %S = OpTypeStruct %int
1328 %Output = OpTypeStruct %float %v4float %S
1329 %_ptr_Uniform_Output = OpTypePointer Uniform %Output
1330 %dataOutput = OpVariable %_ptr_Uniform_Output Uniform
1331 %main = OpFunction %void None %3
1332 %5 = OpLabel
1333 OpReturn
1334 OpFunctionEnd
1335 )";
1336
1337 CompileSuccessfully(spirv);
1338 EXPECT_EQ(SPV_ERROR_INVALID_ID,
1339 ValidateAndRetrieveValidationState(SPV_ENV_VULKAN_1_0));
1340 EXPECT_THAT(
1341 getDiagnosticString(),
1342 HasSubstr(
1343 "'GLSLShared' is not valid for the Vulkan execution environment"));
1344 EXPECT_THAT(getDiagnosticString(),
1345 HasSubstr("[VUID-StandaloneSpirv-GLSLShared-04669]"));
1346 }
1347
1348 TEST_F(ValidateDecorations, BlockGLSLPackedBad) {
1349 std::string spirv = R"(
1350 OpCapability Shader
1351 %1 = OpExtInstImport "GLSL.std.450"
1352 OpMemoryModel Logical GLSL450
1353 OpEntryPoint GLCompute %main "main"
1354 OpExecutionMode %main LocalSize 1 1 1
1355 OpSource GLSL 430
1356 OpDecorate %Output Block
1357 OpDecorate %Output GLSLPacked
1358 OpMemberDecorate %Output 0 Offset 0
1359 %void = OpTypeVoid
1360 %3 = OpTypeFunction %void
1361 %float = OpTypeFloat 32
1362 %Output = OpTypeStruct %float
1363 %_ptr_Uniform_Output = OpTypePointer Uniform %Output
1364 %dataOutput = OpVariable %_ptr_Uniform_Output Uniform
1365 %main = OpFunction %void None %3
1366 %5 = OpLabel
1367 OpReturn
1368 OpFunctionEnd
1369 )";
1370
1371 CompileSuccessfully(spirv);
1372 EXPECT_EQ(SPV_ERROR_INVALID_ID,
1373 ValidateAndRetrieveValidationState(SPV_ENV_VULKAN_1_0));
1374 EXPECT_THAT(
1375 getDiagnosticString(),
1376 HasSubstr(
1377 "'GLSLPacked' is not valid for the Vulkan execution environment"));
1378 EXPECT_THAT(getDiagnosticString(),
1379 HasSubstr("[VUID-StandaloneSpirv-GLSLShared-04669]"));
1380 }
1381
1382 TEST_F(ValidateDecorations, BufferBlockGLSLPackedBad) {
1383 std::string spirv = R"(
1384 OpCapability Shader
1385 %1 = OpExtInstImport "GLSL.std.450"
1386 OpMemoryModel Logical GLSL450
1387 OpEntryPoint GLCompute %main "main"
1388 OpExecutionMode %main LocalSize 1 1 1
1389 OpSource GLSL 430
1390 OpDecorate %Output BufferBlock
1391 OpDecorate %Output GLSLPacked
1392 OpMemberDecorate %Output 0 Offset 0
1393 %void = OpTypeVoid
1394 %3 = OpTypeFunction %void
1395 %float = OpTypeFloat 32
1396 %Output = OpTypeStruct %float
1397 %_ptr_Uniform_Output = OpTypePointer Uniform %Output
1398 %dataOutput = OpVariable %_ptr_Uniform_Output Uniform
1399 %main = OpFunction %void None %3
1400 %5 = OpLabel
1401 OpReturn
1402 OpFunctionEnd
1403 )";
1404
1405 CompileSuccessfully(spirv);
1406 EXPECT_EQ(SPV_ERROR_INVALID_ID,
1407 ValidateAndRetrieveValidationState(SPV_ENV_VULKAN_1_0));
1408 EXPECT_THAT(
1409 getDiagnosticString(),
1410 HasSubstr(
1411 "'GLSLPacked' is not valid for the Vulkan execution environment"));
1412 EXPECT_THAT(getDiagnosticString(),
1413 HasSubstr("[VUID-StandaloneSpirv-GLSLShared-04669]"));
1414 }
1415
1416 TEST_F(ValidateDecorations, BlockNestedStructGLSLPackedBad) {
1417 std::string spirv = R"(
1418 OpCapability Shader
1419 %1 = OpExtInstImport "GLSL.std.450"
1420 OpMemoryModel Logical GLSL450
1421 OpEntryPoint GLCompute %main "main"
1422 OpExecutionMode %main LocalSize 1 1 1
1423 OpSource GLSL 430
1424 OpMemberDecorate %S 0 Offset 0
1425 OpDecorate %S GLSLPacked
1426 OpMemberDecorate %Output 0 Offset 0
1427 OpMemberDecorate %Output 1 Offset 16
1428 OpMemberDecorate %Output 2 Offset 32
1429 OpDecorate %Output Block
1430 %void = OpTypeVoid
1431 %3 = OpTypeFunction %void
1432 %float = OpTypeFloat 32
1433 %v4float = OpTypeVector %float 4
1434 %int = OpTypeInt 32 1
1435 %S = OpTypeStruct %int
1436 %Output = OpTypeStruct %float %v4float %S
1437 %_ptr_Uniform_Output = OpTypePointer Uniform %Output
1438 %dataOutput = OpVariable %_ptr_Uniform_Output Uniform
1439 %main = OpFunction %void None %3
1440 %5 = OpLabel
1441 OpReturn
1442 OpFunctionEnd
1443 )";
1444
1445 CompileSuccessfully(spirv);
1446 EXPECT_EQ(SPV_ERROR_INVALID_ID,
1447 ValidateAndRetrieveValidationState(SPV_ENV_VULKAN_1_0));
1448 EXPECT_THAT(
1449 getDiagnosticString(),
1450 HasSubstr(
1451 "'GLSLPacked' is not valid for the Vulkan execution environment"));
1452 EXPECT_THAT(getDiagnosticString(),
1453 HasSubstr("[VUID-StandaloneSpirv-GLSLShared-04669]"));
1454 }
1455
1456 TEST_F(ValidateDecorations, BufferBlockNestedStructGLSLPackedBad) {
1457 std::string spirv = R"(
1458 OpCapability Shader
1459 %1 = OpExtInstImport "GLSL.std.450"
1460 OpMemoryModel Logical GLSL450
1461 OpEntryPoint GLCompute %main "main"
1462 OpExecutionMode %main LocalSize 1 1 1
1463 OpSource GLSL 430
1464 OpMemberDecorate %S 0 Offset 0
1465 OpDecorate %S GLSLPacked
1466 OpMemberDecorate %Output 0 Offset 0
1467 OpMemberDecorate %Output 1 Offset 16
1468 OpMemberDecorate %Output 2 Offset 32
1469 OpDecorate %Output BufferBlock
1470 %void = OpTypeVoid
1471 %3 = OpTypeFunction %void
1472 %float = OpTypeFloat 32
1473 %v4float = OpTypeVector %float 4
1474 %int = OpTypeInt 32 1
1475 %S = OpTypeStruct %int
1476 %Output = OpTypeStruct %float %v4float %S
1477 %_ptr_Uniform_Output = OpTypePointer Uniform %Output
1478 %dataOutput = OpVariable %_ptr_Uniform_Output Uniform
1479 %main = OpFunction %void None %3
1480 %5 = OpLabel
1481 OpReturn
1482 OpFunctionEnd
1483 )";
1484
1485 CompileSuccessfully(spirv);
1486 EXPECT_EQ(SPV_ERROR_INVALID_ID,
1487 ValidateAndRetrieveValidationState(SPV_ENV_VULKAN_1_0));
1488 EXPECT_THAT(
1489 getDiagnosticString(),
1490 HasSubstr(
1491 "'GLSLPacked' is not valid for the Vulkan execution environment"));
1492 EXPECT_THAT(getDiagnosticString(),
1493 HasSubstr("[VUID-StandaloneSpirv-GLSLShared-04669]"));
1494 }
1495
1496 TEST_F(ValidateDecorations, BlockMissingArrayStrideBad) {
1497 std::string spirv = R"(
1498 OpCapability Shader
1499 %1 = OpExtInstImport "GLSL.std.450"
1500 OpMemoryModel Logical GLSL450
1501 OpEntryPoint GLCompute %main "main"
1502 OpExecutionMode %main LocalSize 1 1 1
1503 OpSource GLSL 430
1504 OpDecorate %Output Block
1505 OpMemberDecorate %Output 0 Offset 0
1506 %void = OpTypeVoid
1507 %3 = OpTypeFunction %void
1508 %float = OpTypeFloat 32
1509 %int = OpTypeInt 32 1
1510 %int_3 = OpConstant %int 3
1511 %array = OpTypeArray %float %int_3
1512 %Output = OpTypeStruct %array
1513 %_ptr_Uniform_Output = OpTypePointer Uniform %Output
1514 %dataOutput = OpVariable %_ptr_Uniform_Output Uniform
1515 %main = OpFunction %void None %3
1516 %5 = OpLabel
1517 OpReturn
1518 OpFunctionEnd
1519 )";
1520
1521 CompileSuccessfully(spirv);
1522 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateAndRetrieveValidationState());
1523 EXPECT_THAT(getDiagnosticString(),
1524 HasSubstr("must be explicitly laid out with ArrayStride or "
1525 "ArrayStrideIdEXT decorations"));
1526 }
1527
1528 TEST_F(ValidateDecorations, BufferBlockMissingArrayStrideBad) {
1529 std::string spirv = R"(
1530 OpCapability Shader
1531 %1 = OpExtInstImport "GLSL.std.450"
1532 OpMemoryModel Logical GLSL450
1533 OpEntryPoint GLCompute %main "main"
1534 OpExecutionMode %main LocalSize 1 1 1
1535 OpSource GLSL 430
1536 OpDecorate %Output BufferBlock
1537 OpMemberDecorate %Output 0 Offset 0
1538 %void = OpTypeVoid
1539 %3 = OpTypeFunction %void
1540 %float = OpTypeFloat 32
1541 %int = OpTypeInt 32 1
1542 %int_3 = OpConstant %int 3
1543 %array = OpTypeArray %float %int_3
1544 %Output = OpTypeStruct %array
1545 %_ptr_Uniform_Output = OpTypePointer Uniform %Output
1546 %dataOutput = OpVariable %_ptr_Uniform_Output Uniform
1547 %main = OpFunction %void None %3
1548 %5 = OpLabel
1549 OpReturn
1550 OpFunctionEnd
1551 )";
1552
1553 CompileSuccessfully(spirv);
1554 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateAndRetrieveValidationState());
1555 EXPECT_THAT(getDiagnosticString(),
1556 HasSubstr("must be explicitly laid out with ArrayStride or "
1557 "ArrayStrideIdEXT decorations"));
1558 }
1559
1560 TEST_F(ValidateDecorations, BlockNestedStructMissingArrayStrideBad) {
1561 std::string spirv = R"(
1562 OpCapability Shader
1563 %1 = OpExtInstImport "GLSL.std.450"
1564 OpMemoryModel Logical GLSL450
1565 OpEntryPoint GLCompute %main "main"
1566 OpExecutionMode %main LocalSize 1 1 1
1567 OpSource GLSL 430
1568 OpMemberDecorate %S 0 Offset 0
1569 OpMemberDecorate %Output 0 Offset 0
1570 OpMemberDecorate %Output 1 Offset 16
1571 OpMemberDecorate %Output 2 Offset 32
1572 OpDecorate %Output Block
1573 %void = OpTypeVoid
1574 %3 = OpTypeFunction %void
1575 %float = OpTypeFloat 32
1576 %v4float = OpTypeVector %float 4
1577 %int = OpTypeInt 32 1
1578 %int_3 = OpConstant %int 3
1579 %array = OpTypeArray %float %int_3
1580 %S = OpTypeStruct %array
1581 %Output = OpTypeStruct %float %v4float %S
1582 %_ptr_Uniform_Output = OpTypePointer Uniform %Output
1583 %dataOutput = OpVariable %_ptr_Uniform_Output Uniform
1584 %main = OpFunction %void None %3
1585 %5 = OpLabel
1586 OpReturn
1587 OpFunctionEnd
1588 )";
1589
1590 CompileSuccessfully(spirv);
1591 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateAndRetrieveValidationState());
1592 EXPECT_THAT(getDiagnosticString(),
1593 HasSubstr("must be explicitly laid out with ArrayStride or "
1594 "ArrayStrideIdEXT decorations"));
1595 }
1596
1597 TEST_F(ValidateDecorations, BufferBlockNestedStructMissingArrayStrideBad) {
1598 std::string spirv = R"(
1599 OpCapability Shader
1600 %1 = OpExtInstImport "GLSL.std.450"
1601 OpMemoryModel Logical GLSL450
1602 OpEntryPoint GLCompute %main "main"
1603 OpExecutionMode %main LocalSize 1 1 1
1604 OpSource GLSL 430
1605 OpMemberDecorate %S 0 Offset 0
1606 OpMemberDecorate %Output 0 Offset 0
1607 OpMemberDecorate %Output 1 Offset 16
1608 OpMemberDecorate %Output 2 Offset 32
1609 OpDecorate %Output BufferBlock
1610 %void = OpTypeVoid
1611 %3 = OpTypeFunction %void
1612 %float = OpTypeFloat 32
1613 %v4float = OpTypeVector %float 4
1614 %int = OpTypeInt 32 1
1615 %int_3 = OpConstant %int 3
1616 %array = OpTypeArray %float %int_3
1617 %S = OpTypeStruct %array
1618 %Output = OpTypeStruct %float %v4float %S
1619 %_ptr_Uniform_Output = OpTypePointer Uniform %Output
1620 %dataOutput = OpVariable %_ptr_Uniform_Output Uniform
1621 %main = OpFunction %void None %3
1622 %5 = OpLabel
1623 OpReturn
1624 OpFunctionEnd
1625 )";
1626
1627 CompileSuccessfully(spirv);
1628 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateAndRetrieveValidationState());
1629 EXPECT_THAT(getDiagnosticString(),
1630 HasSubstr("must be explicitly laid out with ArrayStride or "
1631 "ArrayStrideIdEXT decorations"));
1632 }
1633
1634 TEST_F(ValidateDecorations, BlockMissingMatrixStrideBad) {
1635 std::string spirv = R"(
1636 OpCapability Shader
1637 %1 = OpExtInstImport "GLSL.std.450"
1638 OpMemoryModel Logical GLSL450
1639 OpEntryPoint GLCompute %main "main"
1640 OpExecutionMode %main LocalSize 1 1 1
1641 OpSource GLSL 430
1642 OpDecorate %Output Block
1643 OpMemberDecorate %Output 0 Offset 0
1644 %void = OpTypeVoid
1645 %3 = OpTypeFunction %void
1646 %float = OpTypeFloat 32
1647 %v3float = OpTypeVector %float 3
1648 %matrix = OpTypeMatrix %v3float 4
1649 %Output = OpTypeStruct %matrix
1650 %_ptr_Uniform_Output = OpTypePointer Uniform %Output
1651 %dataOutput = OpVariable %_ptr_Uniform_Output Uniform
1652 %main = OpFunction %void None %3
1653 %5 = OpLabel
1654 OpReturn
1655 OpFunctionEnd
1656 )";
1657
1658 CompileSuccessfully(spirv);
1659 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateAndRetrieveValidationState());
1660 EXPECT_THAT(
1661 getDiagnosticString(),
1662 HasSubstr("must be explicitly laid out with MatrixStride decorations"));
1663 }
1664
1665 TEST_F(ValidateDecorations, BufferBlockMissingMatrixStrideBad) {
1666 std::string spirv = R"(
1667 OpCapability Shader
1668 %1 = OpExtInstImport "GLSL.std.450"
1669 OpMemoryModel Logical GLSL450
1670 OpEntryPoint GLCompute %main "main"
1671 OpExecutionMode %main LocalSize 1 1 1
1672 OpSource GLSL 430
1673 OpDecorate %Output BufferBlock
1674 OpMemberDecorate %Output 0 Offset 0
1675 %void = OpTypeVoid
1676 %3 = OpTypeFunction %void
1677 %float = OpTypeFloat 32
1678 %v3float = OpTypeVector %float 3
1679 %matrix = OpTypeMatrix %v3float 4
1680 %Output = OpTypeStruct %matrix
1681 %_ptr_Uniform_Output = OpTypePointer Uniform %Output
1682 %dataOutput = OpVariable %_ptr_Uniform_Output Uniform
1683 %main = OpFunction %void None %3
1684 %5 = OpLabel
1685 OpReturn
1686 OpFunctionEnd
1687 )";
1688
1689 CompileSuccessfully(spirv);
1690 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateAndRetrieveValidationState());
1691 EXPECT_THAT(
1692 getDiagnosticString(),
1693 HasSubstr("must be explicitly laid out with MatrixStride decorations"));
1694 }
1695
1696 TEST_F(ValidateDecorations, BlockMissingMatrixStrideArrayBad) {
1697 std::string spirv = R"(
1698 OpCapability Shader
1699 %1 = OpExtInstImport "GLSL.std.450"
1700 OpMemoryModel Logical GLSL450
1701 OpEntryPoint GLCompute %main "main"
1702 OpExecutionMode %main LocalSize 1 1 1
1703 OpSource GLSL 430
1704 OpDecorate %Output Block
1705 OpMemberDecorate %Output 0 Offset 0
1706 %void = OpTypeVoid
1707 %3 = OpTypeFunction %void
1708 %float = OpTypeFloat 32
1709 %v3float = OpTypeVector %float 3
1710 %matrix = OpTypeMatrix %v3float 4
1711 %int = OpTypeInt 32 1
1712 %int_3 = OpConstant %int 3
1713 %array = OpTypeArray %matrix %int_3
1714 %Output = OpTypeStruct %matrix
1715 %_ptr_Uniform_Output = OpTypePointer Uniform %Output
1716 %dataOutput = OpVariable %_ptr_Uniform_Output Uniform
1717 %main = OpFunction %void None %3
1718 %5 = OpLabel
1719 OpReturn
1720 OpFunctionEnd
1721 )";
1722
1723 CompileSuccessfully(spirv);
1724 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateAndRetrieveValidationState());
1725 EXPECT_THAT(
1726 getDiagnosticString(),
1727 HasSubstr("must be explicitly laid out with MatrixStride decorations"));
1728 }
1729
1730 TEST_F(ValidateDecorations, BufferBlockMissingMatrixStrideArrayBad) {
1731 std::string spirv = R"(
1732 OpCapability Shader
1733 %1 = OpExtInstImport "GLSL.std.450"
1734 OpMemoryModel Logical GLSL450
1735 OpEntryPoint GLCompute %main "main"
1736 OpExecutionMode %main LocalSize 1 1 1
1737 OpSource GLSL 430
1738 OpDecorate %Output BufferBlock
1739 OpMemberDecorate %Output 0 Offset 0
1740 %void = OpTypeVoid
1741 %3 = OpTypeFunction %void
1742 %float = OpTypeFloat 32
1743 %v3float = OpTypeVector %float 3
1744 %matrix = OpTypeMatrix %v3float 4
1745 %int = OpTypeInt 32 1
1746 %int_3 = OpConstant %int 3
1747 %array = OpTypeArray %matrix %int_3
1748 %Output = OpTypeStruct %matrix
1749 %_ptr_Uniform_Output = OpTypePointer Uniform %Output
1750 %dataOutput = OpVariable %_ptr_Uniform_Output Uniform
1751 %main = OpFunction %void None %3
1752 %5 = OpLabel
1753 OpReturn
1754 OpFunctionEnd
1755 )";
1756
1757 CompileSuccessfully(spirv);
1758 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateAndRetrieveValidationState());
1759 EXPECT_THAT(
1760 getDiagnosticString(),
1761 HasSubstr("must be explicitly laid out with MatrixStride decorations"));
1762 }
1763
1764 TEST_F(ValidateDecorations, BlockNestedStructMissingMatrixStrideBad) {
1765 std::string spirv = R"(
1766 OpCapability Shader
1767 %1 = OpExtInstImport "GLSL.std.450"
1768 OpMemoryModel Logical GLSL450
1769 OpEntryPoint GLCompute %main "main"
1770 OpExecutionMode %main LocalSize 1 1 1
1771 OpSource GLSL 430
1772 OpMemberDecorate %S 0 Offset 0
1773 OpMemberDecorate %Output 0 Offset 0
1774 OpMemberDecorate %Output 1 Offset 16
1775 OpMemberDecorate %Output 2 Offset 32
1776 OpDecorate %Output Block
1777 %void = OpTypeVoid
1778 %3 = OpTypeFunction %void
1779 %float = OpTypeFloat 32
1780 %v3float = OpTypeVector %float 3
1781 %v4float = OpTypeVector %float 4
1782 %matrix = OpTypeMatrix %v3float 4
1783 %S = OpTypeStruct %matrix
1784 %Output = OpTypeStruct %float %v4float %S
1785 %_ptr_Uniform_Output = OpTypePointer Uniform %Output
1786 %dataOutput = OpVariable %_ptr_Uniform_Output Uniform
1787 %main = OpFunction %void None %3
1788 %5 = OpLabel
1789 OpReturn
1790 OpFunctionEnd
1791 )";
1792
1793 CompileSuccessfully(spirv);
1794 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateAndRetrieveValidationState());
1795 EXPECT_THAT(
1796 getDiagnosticString(),
1797 HasSubstr("must be explicitly laid out with MatrixStride decorations"));
1798 }
1799
1800 TEST_F(ValidateDecorations, BufferBlockNestedStructMissingMatrixStrideBad) {
1801 std::string spirv = R"(
1802 OpCapability Shader
1803 %1 = OpExtInstImport "GLSL.std.450"
1804 OpMemoryModel Logical GLSL450
1805 OpEntryPoint GLCompute %main "main"
1806 OpExecutionMode %main LocalSize 1 1 1
1807 OpSource GLSL 430
1808 OpMemberDecorate %S 0 Offset 0
1809 OpMemberDecorate %Output 0 Offset 0
1810 OpMemberDecorate %Output 1 Offset 16
1811 OpMemberDecorate %Output 2 Offset 32
1812 OpDecorate %Output BufferBlock
1813 %void = OpTypeVoid
1814 %3 = OpTypeFunction %void
1815 %float = OpTypeFloat 32
1816 %v3float = OpTypeVector %float 3
1817 %v4float = OpTypeVector %float 4
1818 %matrix = OpTypeMatrix %v3float 4
1819 %S = OpTypeStruct %matrix
1820 %Output = OpTypeStruct %float %v4float %S
1821 %_ptr_Uniform_Output = OpTypePointer Uniform %Output
1822 %dataOutput = OpVariable %_ptr_Uniform_Output Uniform
1823 %main = OpFunction %void None %3
1824 %5 = OpLabel
1825 OpReturn
1826 OpFunctionEnd
1827 )";
1828
1829 CompileSuccessfully(spirv);
1830 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateAndRetrieveValidationState());
1831 EXPECT_THAT(
1832 getDiagnosticString(),
1833 HasSubstr("must be explicitly laid out with MatrixStride decorations"));
1834 }
1835
1836 TEST_F(ValidateDecorations, BlockStandardUniformBufferLayout) {
1837 std::string spirv = R"(
1838 OpCapability Shader
1839 %1 = OpExtInstImport "GLSL.std.450"
1840 OpMemoryModel Logical GLSL450
1841 OpEntryPoint GLCompute %main "main"
1842 OpExecutionMode %main LocalSize 1 1 1
1843 OpSource GLSL 430
1844 OpMemberDecorate %F 0 Offset 0
1845 OpMemberDecorate %F 1 Offset 8
1846 OpDecorate %_arr_float_uint_2 ArrayStride 16
1847 OpDecorate %_arr_mat3v3float_uint_2 ArrayStride 48
1848 OpMemberDecorate %O 0 Offset 0
1849 OpMemberDecorate %O 1 Offset 16
1850 OpMemberDecorate %O 2 Offset 32
1851 OpMemberDecorate %O 3 Offset 64
1852 OpMemberDecorate %O 4 ColMajor
1853 OpMemberDecorate %O 4 Offset 80
1854 OpMemberDecorate %O 4 MatrixStride 16
1855 OpDecorate %_arr_O_uint_2 ArrayStride 176
1856 OpMemberDecorate %Output 0 Offset 0
1857 OpMemberDecorate %Output 1 Offset 8
1858 OpMemberDecorate %Output 2 Offset 16
1859 OpMemberDecorate %Output 3 Offset 32
1860 OpMemberDecorate %Output 4 Offset 48
1861 OpMemberDecorate %Output 5 Offset 64
1862 OpMemberDecorate %Output 6 ColMajor
1863 OpMemberDecorate %Output 6 Offset 96
1864 OpMemberDecorate %Output 6 MatrixStride 16
1865 OpMemberDecorate %Output 7 Offset 128
1866 OpDecorate %Output Block
1867 OpDecorate %dataOutput DescriptorSet 0
1868 OpDecorate %dataOutput Binding 0
1869 %void = OpTypeVoid
1870 %3 = OpTypeFunction %void
1871 %float = OpTypeFloat 32
1872 %v2float = OpTypeVector %float 2
1873 %v3float = OpTypeVector %float 3
1874 %int = OpTypeInt 32 1
1875 %uint = OpTypeInt 32 0
1876 %v2uint = OpTypeVector %uint 2
1877 %F = OpTypeStruct %int %v2uint
1878 %uint_2 = OpConstant %uint 2
1879 %_arr_float_uint_2 = OpTypeArray %float %uint_2
1880 %mat2v3float = OpTypeMatrix %v3float 2
1881 %v3uint = OpTypeVector %uint 3
1882 %mat3v3float = OpTypeMatrix %v3float 3
1883 %_arr_mat3v3float_uint_2 = OpTypeArray %mat3v3float %uint_2
1884 %O = OpTypeStruct %v3uint %v2float %_arr_float_uint_2 %v2float %_arr_mat3v3float_uint_2
1885 %_arr_O_uint_2 = OpTypeArray %O %uint_2
1886 %Output = OpTypeStruct %float %v2float %v3float %F %float %_arr_float_uint_2 %mat2v3float %_arr_O_uint_2
1887 %_ptr_Uniform_Output = OpTypePointer Uniform %Output
1888 %dataOutput = OpVariable %_ptr_Uniform_Output Uniform
1889 %main = OpFunction %void None %3
1890 %5 = OpLabel
1891 OpReturn
1892 OpFunctionEnd
1893 )";
1894
1895 CompileSuccessfully(spirv);
1896 EXPECT_EQ(SPV_SUCCESS,
1897 ValidateAndRetrieveValidationState(SPV_ENV_VULKAN_1_0));
1898 }
1899
1900 TEST_F(ValidateDecorations, BlockLayoutPermitsTightVec3ScalarPackingGood) {
1901 // See https://github.com/KhronosGroup/SPIRV-Tools/issues/1666
1902 std::string spirv = R"(
1903 OpCapability Shader
1904 OpMemoryModel Logical GLSL450
1905 OpEntryPoint Vertex %main "main"
1906 OpSource GLSL 450
1907 OpMemberDecorate %S 0 Offset 0
1908 OpMemberDecorate %S 1 Offset 12
1909 OpDecorate %S Block
1910 OpDecorate %B DescriptorSet 0
1911 OpDecorate %B Binding 0
1912 %void = OpTypeVoid
1913 %3 = OpTypeFunction %void
1914 %float = OpTypeFloat 32
1915 %v3float = OpTypeVector %float 3
1916 %S = OpTypeStruct %v3float %float
1917 %_ptr_Uniform_S = OpTypePointer Uniform %S
1918 %B = OpVariable %_ptr_Uniform_S Uniform
1919 %main = OpFunction %void None %3
1920 %5 = OpLabel
1921 OpReturn
1922 OpFunctionEnd
1923 )";
1924
1925 CompileSuccessfully(spirv);
1926 EXPECT_EQ(SPV_SUCCESS, ValidateAndRetrieveValidationState(SPV_ENV_VULKAN_1_0))
1927 << getDiagnosticString();
1928 }
1929
1930 TEST_F(ValidateDecorations, BlockCantAppearWithinABlockBad) {
1931 // See https://github.com/KhronosGroup/SPIRV-Tools/issues/1587
1932 std::string spirv = R"(
1933 OpCapability Shader
1934 OpMemoryModel Logical GLSL450
1935 OpEntryPoint Vertex %main "main"
1936 OpSource GLSL 450
1937 OpMemberDecorate %S 0 Offset 0
1938 OpMemberDecorate %S 1 Offset 16
1939 OpMemberDecorate %S2 0 Offset 0
1940 OpMemberDecorate %S2 1 Offset 12
1941 OpDecorate %S Block
1942 OpDecorate %S2 Block
1943 OpDecorate %B DescriptorSet 0
1944 OpDecorate %B Binding 0
1945 %void = OpTypeVoid
1946 %3 = OpTypeFunction %void
1947 %float = OpTypeFloat 32
1948 %S2 = OpTypeStruct %float %float
1949 %S = OpTypeStruct %float %S2
1950 %_ptr_Uniform_S = OpTypePointer Uniform %S
1951 %B = OpVariable %_ptr_Uniform_S Uniform
1952 %main = OpFunction %void None %3
1953 %5 = OpLabel
1954 OpReturn
1955 OpFunctionEnd
1956 )";
1957
1958 CompileSuccessfully(spirv);
1959 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateAndRetrieveValidationState());
1960 EXPECT_THAT(getDiagnosticString(),
1961 HasSubstr("rules: A Block or BufferBlock cannot be nested within "
1962 "another Block or BufferBlock."));
1963 }
1964
1965 TEST_F(ValidateDecorations, BufferblockCantAppearWithinABufferblockBad) {
1966 // See https://github.com/KhronosGroup/SPIRV-Tools/issues/1587
1967 std::string spirv = R"(
1968 OpCapability Shader
1969 OpMemoryModel Logical GLSL450
1970 OpEntryPoint Vertex %main "main"
1971 OpSource GLSL 450
1972 OpMemberDecorate %S 0 Offset 0
1973 OpMemberDecorate %S 1 Offset 16
1974 OpMemberDecorate %S2 0 Offset 0
1975 OpMemberDecorate %S2 1 Offset 16
1976 OpMemberDecorate %S3 0 Offset 0
1977 OpMemberDecorate %S3 1 Offset 12
1978 OpDecorate %S BufferBlock
1979 OpDecorate %S3 BufferBlock
1980 OpDecorate %B DescriptorSet 0
1981 OpDecorate %B Binding 0
1982 %void = OpTypeVoid
1983 %3 = OpTypeFunction %void
1984 %float = OpTypeFloat 32
1985 %S3 = OpTypeStruct %float %float
1986 %S2 = OpTypeStruct %float %S3
1987 %S = OpTypeStruct %float %S2
1988 %_ptr_Uniform_S = OpTypePointer Uniform %S
1989 %B = OpVariable %_ptr_Uniform_S Uniform
1990 %main = OpFunction %void None %3
1991 %5 = OpLabel
1992 OpReturn
1993 OpFunctionEnd
1994 )";
1995
1996 CompileSuccessfully(spirv);
1997 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateAndRetrieveValidationState());
1998 EXPECT_THAT(getDiagnosticString(),
1999 HasSubstr("rules: A Block or BufferBlock cannot be nested within "
2000 "another Block or BufferBlock."));
2001 }
2002
2003 TEST_F(ValidateDecorations, BufferblockCantAppearWithinABlockBad) {
2004 // See https://github.com/KhronosGroup/SPIRV-Tools/issues/1587
2005 std::string spirv = R"(
2006 OpCapability Shader
2007 OpMemoryModel Logical GLSL450
2008 OpEntryPoint Vertex %main "main"
2009 OpSource GLSL 450
2010 OpMemberDecorate %S 0 Offset 0
2011 OpMemberDecorate %S 1 Offset 16
2012 OpMemberDecorate %S2 0 Offset 0
2013 OpMemberDecorate %S2 1 Offset 16
2014 OpMemberDecorate %S3 0 Offset 0
2015 OpMemberDecorate %S3 1 Offset 12
2016 OpDecorate %S Block
2017 OpDecorate %S3 BufferBlock
2018 OpDecorate %B DescriptorSet 0
2019 OpDecorate %B Binding 0
2020 %void = OpTypeVoid
2021 %3 = OpTypeFunction %void
2022 %float = OpTypeFloat 32
2023 %S3 = OpTypeStruct %float %float
2024 %S2 = OpTypeStruct %float %S3
2025 %S = OpTypeStruct %float %S2
2026 %_ptr_Uniform_S = OpTypePointer Uniform %S
2027 %B = OpVariable %_ptr_Uniform_S Uniform
2028 %main = OpFunction %void None %3
2029 %5 = OpLabel
2030 OpReturn
2031 OpFunctionEnd
2032 )";
2033
2034 CompileSuccessfully(spirv);
2035 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateAndRetrieveValidationState());
2036 EXPECT_THAT(getDiagnosticString(),
2037 HasSubstr("rules: A Block or BufferBlock cannot be nested within "
2038 "another Block or BufferBlock."));
2039 }
2040
2041 TEST_F(ValidateDecorations, BlockCantAppearWithinABufferblockBad) {
2042 // See https://github.com/KhronosGroup/SPIRV-Tools/issues/1587
2043 std::string spirv = R"(
2044 OpCapability Shader
2045 OpMemoryModel Logical GLSL450
2046 OpEntryPoint Vertex %main "main"
2047 OpSource GLSL 450
2048 OpMemberDecorate %S 0 Offset 0
2049 OpMemberDecorate %S 1 Offset 16
2050 OpMemberDecorate %S2 0 Offset 0
2051 OpMemberDecorate %S2 1 Offset 16
2052 OpMemberDecorate %S3 0 Offset 0
2053 OpMemberDecorate %S3 1 Offset 16
2054 OpMemberDecorate %S4 0 Offset 0
2055 OpMemberDecorate %S4 1 Offset 12
2056 OpDecorate %S BufferBlock
2057 OpDecorate %S4 Block
2058 OpDecorate %B DescriptorSet 0
2059 OpDecorate %B Binding 0
2060 %void = OpTypeVoid
2061 %3 = OpTypeFunction %void
2062 %float = OpTypeFloat 32
2063 %S4 = OpTypeStruct %float %float
2064 %S3 = OpTypeStruct %float %S4
2065 %S2 = OpTypeStruct %float %S3
2066 %S = OpTypeStruct %float %S2
2067 %_ptr_Uniform_S = OpTypePointer Uniform %S
2068 %B = OpVariable %_ptr_Uniform_S Uniform
2069 %main = OpFunction %void None %3
2070 %5 = OpLabel
2071 OpReturn
2072 OpFunctionEnd
2073 )";
2074
2075 CompileSuccessfully(spirv);
2076 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateAndRetrieveValidationState());
2077 EXPECT_THAT(getDiagnosticString(),
2078 HasSubstr("rules: A Block or BufferBlock cannot be nested within "
2079 "another Block or BufferBlock."));
2080 }
2081
2082 TEST_F(ValidateDecorations, BlockCannotAppearWithinBlockArray) {
2083 const std::string spirv = R"(
2084 OpCapability Shader
2085 OpMemoryModel Logical GLSL450
2086 OpEntryPoint Vertex %main "main"
2087 OpMemberDecorate %outer 0 Offset 0
2088 OpMemberDecorate %outer 1 Offset 4
2089 OpMemberDecorate %outer 2 Offset 20
2090 OpDecorate %outer Block
2091 OpMemberDecorate %inner 0 Offset 0
2092 OpDecorate %inner Block
2093 %void = OpTypeVoid
2094 %void_fn = OpTypeFunction %void
2095 %int = OpTypeInt 32 0
2096 %int_4 = OpConstant %int 4
2097 %inner = OpTypeStruct %int
2098 %array = OpTypeArray %inner %int_4
2099 %outer = OpTypeStruct %int %array %int
2100 %main = OpFunction %void None %void_fn
2101 %entry = OpLabel
2102 OpReturn
2103 OpFunctionEnd
2104 )";
2105
2106 CompileSuccessfully(spirv);
2107 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
2108 EXPECT_THAT(getDiagnosticString(),
2109 HasSubstr("rules: A Block or BufferBlock cannot be nested within "
2110 "another Block or BufferBlock."));
2111 }
2112
2113 TEST_F(ValidateDecorations, BlockCannotAppearWithinBlockMultiArray) {
2114 const std::string spirv = R"(
2115 OpCapability Shader
2116 OpMemoryModel Logical GLSL450
2117 OpEntryPoint Vertex %main "main"
2118 OpMemberDecorate %outer 0 Offset 0
2119 OpMemberDecorate %outer 1 Offset 4
2120 OpDecorate %outer Block
2121 OpMemberDecorate %inner 0 Offset 0
2122 OpDecorate %inner Block
2123 %void = OpTypeVoid
2124 %void_fn = OpTypeFunction %void
2125 %int = OpTypeInt 32 0
2126 %int_4 = OpConstant %int 4
2127 %inner = OpTypeStruct %int
2128 %array1 = OpTypeArray %inner %int_4
2129 %array2 = OpTypeArray %array1 %int_4
2130 %outer = OpTypeStruct %int %array2
2131 %main = OpFunction %void None %void_fn
2132 %entry = OpLabel
2133 OpReturn
2134 OpFunctionEnd
2135 )";
2136
2137 CompileSuccessfully(spirv);
2138 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
2139 EXPECT_THAT(getDiagnosticString(),
2140 HasSubstr("rules: A Block or BufferBlock cannot be nested within "
2141 "another Block or BufferBlock."));
2142 }
2143
2144 TEST_F(ValidateDecorations, BlockLayoutForbidsTightScalarVec3PackingBad) {
2145 // See https://github.com/KhronosGroup/SPIRV-Tools/issues/1666
2146 std::string spirv = R"(
2147 OpCapability Shader
2148 OpMemoryModel Logical GLSL450
2149 OpEntryPoint Vertex %main "main"
2150 OpSource GLSL 450
2151 OpMemberDecorate %S 0 Offset 0
2152 OpMemberDecorate %S 1 Offset 4
2153 OpDecorate %S Block
2154 OpDecorate %B DescriptorSet 0
2155 OpDecorate %B Binding 0
2156 %void = OpTypeVoid
2157 %3 = OpTypeFunction %void
2158 %float = OpTypeFloat 32
2159 %v3float = OpTypeVector %float 3
2160 %S = OpTypeStruct %float %v3float
2161 %_ptr_Uniform_S = OpTypePointer Uniform %S
2162 %B = OpVariable %_ptr_Uniform_S Uniform
2163 %main = OpFunction %void None %3
2164 %5 = OpLabel
2165 OpReturn
2166 OpFunctionEnd
2167 )";
2168
2169 CompileSuccessfully(spirv);
2170 EXPECT_EQ(SPV_ERROR_INVALID_ID,
2171 ValidateAndRetrieveValidationState(SPV_ENV_VULKAN_1_0));
2172 EXPECT_THAT(
2173 getDiagnosticString(),
2174 HasSubstr("Structure id 2 decorated as Block for variable in Uniform "
2175 "storage class must follow standard uniform buffer layout "
2176 "rules: member 1 at offset 4 is not aligned to 16"));
2177 }
2178
2179 TEST_F(ValidateDecorations,
2180 BlockLayoutPermitsTightScalarVec3PackingWithRelaxedLayoutGood) {
2181 // Same as previous test, but with explicit option to relax block layout.
2182 std::string spirv = R"(
2183 OpCapability Shader
2184 OpMemoryModel Logical GLSL450
2185 OpEntryPoint Vertex %main "main"
2186 OpSource GLSL 450
2187 OpMemberDecorate %S 0 Offset 0
2188 OpMemberDecorate %S 1 Offset 4
2189 OpDecorate %S Block
2190 OpDecorate %B DescriptorSet 0
2191 OpDecorate %B Binding 0
2192 %void = OpTypeVoid
2193 %3 = OpTypeFunction %void
2194 %float = OpTypeFloat 32
2195 %v3float = OpTypeVector %float 3
2196 %S = OpTypeStruct %float %v3float
2197 %_ptr_Uniform_S = OpTypePointer Uniform %S
2198 %B = OpVariable %_ptr_Uniform_S Uniform
2199 %main = OpFunction %void None %3
2200 %5 = OpLabel
2201 OpReturn
2202 OpFunctionEnd
2203 )";
2204
2205 CompileSuccessfully(spirv);
2206 spvValidatorOptionsSetRelaxBlockLayout(getValidatorOptions(), true);
2207 EXPECT_EQ(SPV_SUCCESS,
2208 ValidateAndRetrieveValidationState(SPV_ENV_VULKAN_1_0));
2209 EXPECT_THAT(getDiagnosticString(), Eq(""));
2210 }
2211
2212 TEST_F(ValidateDecorations,
2213 BlockLayoutPermitsTightScalarVec3PackingBadOffsetWithRelaxedLayoutBad) {
2214 // Same as previous test, but with the vector not aligned to its scalar
2215 // element. Use offset 5 instead of a multiple of 4.
2216 std::string spirv = R"(
2217 OpCapability Shader
2218 OpMemoryModel Logical GLSL450
2219 OpEntryPoint Vertex %main "main"
2220 OpSource GLSL 450
2221 OpMemberDecorate %S 0 Offset 0
2222 OpMemberDecorate %S 1 Offset 5
2223 OpDecorate %S Block
2224 OpDecorate %B DescriptorSet 0
2225 OpDecorate %B Binding 0
2226 %void = OpTypeVoid
2227 %3 = OpTypeFunction %void
2228 %float = OpTypeFloat 32
2229 %v3float = OpTypeVector %float 3
2230 %S = OpTypeStruct %float %v3float
2231 %_ptr_Uniform_S = OpTypePointer Uniform %S
2232 %B = OpVariable %_ptr_Uniform_S Uniform
2233 %main = OpFunction %void None %3
2234 %5 = OpLabel
2235 OpReturn
2236 OpFunctionEnd
2237 )";
2238
2239 CompileSuccessfully(spirv);
2240 spvValidatorOptionsSetRelaxBlockLayout(getValidatorOptions(), true);
2241 EXPECT_EQ(SPV_ERROR_INVALID_ID,
2242 ValidateAndRetrieveValidationState(SPV_ENV_VULKAN_1_0));
2243 EXPECT_THAT(
2244 getDiagnosticString(),
2245 HasSubstr(
2246 "Structure id 2 decorated as Block for variable in Uniform storage "
2247 "class must follow relaxed uniform buffer layout rules: member 1 at "
2248 "offset 5 is not aligned to scalar element size 4"));
2249 }
2250
2251 TEST_F(ValidateDecorations,
2252 BlockLayoutPermitsTightScalarVec3PackingWithVulkan1_1Good) {
2253 // Same as previous test, but with Vulkan 1.1. Vulkan 1.1 included
2254 // VK_KHR_relaxed_block_layout in core.
2255 std::string spirv = R"(
2256 OpCapability Shader
2257 OpMemoryModel Logical GLSL450
2258 OpEntryPoint Vertex %main "main"
2259 OpSource GLSL 450
2260 OpMemberDecorate %S 0 Offset 0
2261 OpMemberDecorate %S 1 Offset 4
2262 OpDecorate %S Block
2263 OpDecorate %B DescriptorSet 0
2264 OpDecorate %B Binding 0
2265 %void = OpTypeVoid
2266 %3 = OpTypeFunction %void
2267 %float = OpTypeFloat 32
2268 %v3float = OpTypeVector %float 3
2269 %S = OpTypeStruct %float %v3float
2270 %_ptr_Uniform_S = OpTypePointer Uniform %S
2271 %B = OpVariable %_ptr_Uniform_S Uniform
2272 %main = OpFunction %void None %3
2273 %5 = OpLabel
2274 OpReturn
2275 OpFunctionEnd
2276 )";
2277
2278 CompileSuccessfully(spirv);
2279 EXPECT_EQ(SPV_SUCCESS,
2280 ValidateAndRetrieveValidationState(SPV_ENV_VULKAN_1_1));
2281 EXPECT_THAT(getDiagnosticString(), Eq(""));
2282 }
2283
2284 TEST_F(ValidateDecorations,
2285 BlockLayoutPermitsTightScalarVec3PackingWithScalarLayoutGood) {
2286 // Same as previous test, but with scalar block layout.
2287 std::string spirv = R"(
2288 OpCapability Shader
2289 OpMemoryModel Logical GLSL450
2290 OpEntryPoint Vertex %main "main"
2291 OpSource GLSL 450
2292 OpMemberDecorate %S 0 Offset 0
2293 OpMemberDecorate %S 1 Offset 4
2294 OpDecorate %S Block
2295 OpDecorate %B DescriptorSet 0
2296 OpDecorate %B Binding 0
2297 %void = OpTypeVoid
2298 %3 = OpTypeFunction %void
2299 %float = OpTypeFloat 32
2300 %v3float = OpTypeVector %float 3
2301 %S = OpTypeStruct %float %v3float
2302 %_ptr_Uniform_S = OpTypePointer Uniform %S
2303 %B = OpVariable %_ptr_Uniform_S Uniform
2304 %main = OpFunction %void None %3
2305 %5 = OpLabel
2306 OpReturn
2307 OpFunctionEnd
2308 )";
2309
2310 CompileSuccessfully(spirv);
2311 spvValidatorOptionsSetScalarBlockLayout(getValidatorOptions(), true);
2312 EXPECT_EQ(SPV_SUCCESS,
2313 ValidateAndRetrieveValidationState(SPV_ENV_VULKAN_1_0));
2314 EXPECT_THAT(getDiagnosticString(), Eq(""));
2315 }
2316
2317 TEST_F(ValidateDecorations,
2318 BlockLayoutPermitsScalarAlignedArrayWithScalarLayoutGood) {
2319 // The array at offset 4 is ok with scalar block layout.
2320 std::string spirv = R"(
2321 OpCapability Shader
2322 OpMemoryModel Logical GLSL450
2323 OpEntryPoint Vertex %main "main"
2324 OpSource GLSL 450
2325 OpMemberDecorate %S 0 Offset 0
2326 OpMemberDecorate %S 1 Offset 4
2327 OpDecorate %S Block
2328 OpDecorate %B DescriptorSet 0
2329 OpDecorate %B Binding 0
2330 OpDecorate %arr_float ArrayStride 4
2331 %void = OpTypeVoid
2332 %3 = OpTypeFunction %void
2333 %uint = OpTypeInt 32 0
2334 %uint_3 = OpConstant %uint 3
2335 %float = OpTypeFloat 32
2336 %arr_float = OpTypeArray %float %uint_3
2337 %S = OpTypeStruct %float %arr_float
2338 %_ptr_Uniform_S = OpTypePointer Uniform %S
2339 %B = OpVariable %_ptr_Uniform_S Uniform
2340 %main = OpFunction %void None %3
2341 %5 = OpLabel
2342 OpReturn
2343 OpFunctionEnd
2344 )";
2345
2346 CompileSuccessfully(spirv);
2347 spvValidatorOptionsSetScalarBlockLayout(getValidatorOptions(), true);
2348 EXPECT_EQ(SPV_SUCCESS,
2349 ValidateAndRetrieveValidationState(SPV_ENV_VULKAN_1_0));
2350 EXPECT_THAT(getDiagnosticString(), Eq(""));
2351 }
2352
2353 TEST_F(ValidateDecorations,
2354 BlockLayoutPermitsScalarAlignedArrayOfVec3WithScalarLayoutGood) {
2355 // The array at offset 4 is ok with scalar block layout, even though
2356 // its elements are vec3.
2357 // This is the same as the previous case, but the array elements are vec3
2358 // instead of float.
2359 std::string spirv = R"(
2360 OpCapability Shader
2361 OpMemoryModel Logical GLSL450
2362 OpEntryPoint Vertex %main "main"
2363 OpSource GLSL 450
2364 OpMemberDecorate %S 0 Offset 0
2365 OpMemberDecorate %S 1 Offset 4
2366 OpDecorate %S Block
2367 OpDecorate %B DescriptorSet 0
2368 OpDecorate %B Binding 0
2369 OpDecorate %arr_vec3 ArrayStride 12
2370 %void = OpTypeVoid
2371 %3 = OpTypeFunction %void
2372 %uint = OpTypeInt 32 0
2373 %uint_3 = OpConstant %uint 3
2374 %float = OpTypeFloat 32
2375 %vec3 = OpTypeVector %float 3
2376 %arr_vec3 = OpTypeArray %vec3 %uint_3
2377 %S = OpTypeStruct %float %arr_vec3
2378 %_ptr_Uniform_S = OpTypePointer Uniform %S
2379 %B = OpVariable %_ptr_Uniform_S Uniform
2380 %main = OpFunction %void None %3
2381 %5 = OpLabel
2382 OpReturn
2383 OpFunctionEnd
2384 )";
2385
2386 CompileSuccessfully(spirv);
2387 spvValidatorOptionsSetScalarBlockLayout(getValidatorOptions(), true);
2388 EXPECT_EQ(SPV_SUCCESS,
2389 ValidateAndRetrieveValidationState(SPV_ENV_VULKAN_1_0));
2390 EXPECT_THAT(getDiagnosticString(), Eq(""));
2391 }
2392
2393 TEST_F(ValidateDecorations,
2394 BlockLayoutPermitsScalarAlignedStructWithScalarLayoutGood) {
2395 // Scalar block layout permits the struct at offset 4, even though
2396 // it contains a vector with base alignment 8 and scalar alignment 4.
2397 std::string spirv = R"(
2398 OpCapability Shader
2399 OpMemoryModel Logical GLSL450
2400 OpEntryPoint Vertex %main "main"
2401 OpSource GLSL 450
2402 OpMemberDecorate %S 0 Offset 0
2403 OpMemberDecorate %S 1 Offset 4
2404 OpMemberDecorate %st 0 Offset 0
2405 OpMemberDecorate %st 1 Offset 8
2406 OpDecorate %S Block
2407 OpDecorate %B DescriptorSet 0
2408 OpDecorate %B Binding 0
2409 %void = OpTypeVoid
2410 %3 = OpTypeFunction %void
2411 %float = OpTypeFloat 32
2412 %vec2 = OpTypeVector %float 2
2413 %st = OpTypeStruct %vec2 %float
2414 %S = OpTypeStruct %float %st
2415 %_ptr_Uniform_S = OpTypePointer Uniform %S
2416 %B = OpVariable %_ptr_Uniform_S Uniform
2417 %main = OpFunction %void None %3
2418 %5 = OpLabel
2419 OpReturn
2420 OpFunctionEnd
2421 )";
2422
2423 CompileSuccessfully(spirv);
2424 spvValidatorOptionsSetScalarBlockLayout(getValidatorOptions(), true);
2425 EXPECT_EQ(SPV_SUCCESS,
2426 ValidateAndRetrieveValidationState(SPV_ENV_VULKAN_1_0));
2427 EXPECT_THAT(getDiagnosticString(), Eq(""));
2428 }
2429
2430 TEST_F(
2431 ValidateDecorations,
2432 BlockLayoutPermitsFieldsInBaseAlignmentPaddingAtEndOfStructWithScalarLayoutGood) {
2433 // Scalar block layout permits fields in what would normally be the padding at
2434 // the end of a struct.
2435 std::string spirv = R"(
2436 OpCapability Shader
2437 OpCapability Float64
2438 OpMemoryModel Logical GLSL450
2439 OpEntryPoint Vertex %main "main"
2440 OpSource GLSL 450
2441 OpMemberDecorate %st 0 Offset 0
2442 OpMemberDecorate %st 1 Offset 8
2443 OpMemberDecorate %S 0 Offset 0
2444 OpMemberDecorate %S 1 Offset 12
2445 OpDecorate %S Block
2446 OpDecorate %B DescriptorSet 0
2447 OpDecorate %B Binding 0
2448 %void = OpTypeVoid
2449 %3 = OpTypeFunction %void
2450 %float = OpTypeFloat 32
2451 %double = OpTypeFloat 64
2452 %st = OpTypeStruct %double %float
2453 %S = OpTypeStruct %st %float
2454 %_ptr_Uniform_S = OpTypePointer Uniform %S
2455 %B = OpVariable %_ptr_Uniform_S Uniform
2456 %main = OpFunction %void None %3
2457 %5 = OpLabel
2458 OpReturn
2459 OpFunctionEnd
2460 )";
2461
2462 CompileSuccessfully(spirv);
2463 spvValidatorOptionsSetScalarBlockLayout(getValidatorOptions(), true);
2464 EXPECT_EQ(SPV_SUCCESS,
2465 ValidateAndRetrieveValidationState(SPV_ENV_VULKAN_1_0));
2466 EXPECT_THAT(getDiagnosticString(), Eq(""));
2467 }
2468
2469 TEST_F(
2470 ValidateDecorations,
2471 BlockLayoutPermitsStraddlingVectorWithScalarLayoutOverrideRelaxBlockLayoutGood) {
2472 // Same as previous, but set relaxed block layout first. Scalar layout always
2473 // wins.
2474 std::string spirv = R"(
2475 OpCapability Shader
2476 OpMemoryModel Logical GLSL450
2477 OpEntryPoint Vertex %main "main"
2478 OpSource GLSL 450
2479 OpMemberDecorate %S 0 Offset 0
2480 OpMemberDecorate %S 1 Offset 4
2481 OpDecorate %S Block
2482 OpDecorate %B DescriptorSet 0
2483 OpDecorate %B Binding 0
2484 %void = OpTypeVoid
2485 %3 = OpTypeFunction %void
2486 %float = OpTypeFloat 32
2487 %vec4 = OpTypeVector %float 4
2488 %S = OpTypeStruct %float %vec4
2489 %_ptr_Uniform_S = OpTypePointer Uniform %S
2490 %B = OpVariable %_ptr_Uniform_S Uniform
2491 %main = OpFunction %void None %3
2492 %5 = OpLabel
2493 OpReturn
2494 OpFunctionEnd
2495 )";
2496
2497 CompileSuccessfully(spirv);
2498 spvValidatorOptionsSetRelaxBlockLayout(getValidatorOptions(), true);
2499 spvValidatorOptionsSetScalarBlockLayout(getValidatorOptions(), true);
2500 EXPECT_EQ(SPV_SUCCESS,
2501 ValidateAndRetrieveValidationState(SPV_ENV_VULKAN_1_0));
2502 EXPECT_THAT(getDiagnosticString(), Eq(""));
2503 }
2504
2505 TEST_F(
2506 ValidateDecorations,
2507 BlockLayoutPermitsStraddlingVectorWithRelaxedLayoutOverridenByScalarBlockLayoutGood) {
2508 // Same as previous, but set scalar block layout first. Scalar layout always
2509 // wins.
2510 std::string spirv = R"(
2511 OpCapability Shader
2512 OpMemoryModel Logical GLSL450
2513 OpEntryPoint Vertex %main "main"
2514 OpSource GLSL 450
2515 OpMemberDecorate %S 0 Offset 0
2516 OpMemberDecorate %S 1 Offset 4
2517 OpDecorate %S Block
2518 OpDecorate %B DescriptorSet 0
2519 OpDecorate %B Binding 0
2520 %void = OpTypeVoid
2521 %3 = OpTypeFunction %void
2522 %float = OpTypeFloat 32
2523 %vec4 = OpTypeVector %float 4
2524 %S = OpTypeStruct %float %vec4
2525 %_ptr_Uniform_S = OpTypePointer Uniform %S
2526 %B = OpVariable %_ptr_Uniform_S Uniform
2527 %main = OpFunction %void None %3
2528 %5 = OpLabel
2529 OpReturn
2530 OpFunctionEnd
2531 )";
2532
2533 CompileSuccessfully(spirv);
2534 spvValidatorOptionsSetScalarBlockLayout(getValidatorOptions(), true);
2535 spvValidatorOptionsSetRelaxBlockLayout(getValidatorOptions(), true);
2536 EXPECT_EQ(SPV_SUCCESS,
2537 ValidateAndRetrieveValidationState(SPV_ENV_VULKAN_1_0));
2538 EXPECT_THAT(getDiagnosticString(), Eq(""));
2539 }
2540
2541 TEST_F(ValidateDecorations, BufferBlock16bitStandardStorageBufferLayout) {
2542 std::string spirv = R"(
2543 OpCapability Shader
2544 OpCapability StorageUniform16
2545 OpExtension "SPV_KHR_16bit_storage"
2546 OpMemoryModel Logical GLSL450
2547 OpEntryPoint GLCompute %main "main"
2548 OpExecutionMode %main LocalSize 1 1 1
2549 OpDecorate %f32arr ArrayStride 4
2550 OpDecorate %f16arr ArrayStride 2
2551 OpMemberDecorate %SSBO32 0 Offset 0
2552 OpMemberDecorate %SSBO16 0 Offset 0
2553 OpDecorate %SSBO32 BufferBlock
2554 OpDecorate %SSBO16 BufferBlock
2555 OpDecorate %varSSBO32 DescriptorSet 0
2556 OpDecorate %varSSBO32 Binding 0
2557 OpDecorate %varSSBO16 DescriptorSet 0
2558 OpDecorate %varSSBO16 Binding 1
2559 %void = OpTypeVoid
2560 %voidf = OpTypeFunction %void
2561 %u32 = OpTypeInt 32 0
2562 %i32 = OpTypeInt 32 1
2563 %f32 = OpTypeFloat 32
2564 %uvec3 = OpTypeVector %u32 3
2565 %c_i32_32 = OpConstant %i32 32
2566 %c_i32_128 = OpConstant %i32 128
2567 %f32arr = OpTypeArray %f32 %c_i32_128
2568 %f16 = OpTypeFloat 16
2569 %f16arr = OpTypeArray %f16 %c_i32_128
2570 %SSBO32 = OpTypeStruct %f32arr
2571 %SSBO16 = OpTypeStruct %f16arr
2572 %_ptr_Uniform_SSBO32 = OpTypePointer Uniform %SSBO32
2573 %varSSBO32 = OpVariable %_ptr_Uniform_SSBO32 Uniform
2574 %_ptr_Uniform_SSBO16 = OpTypePointer Uniform %SSBO16
2575 %varSSBO16 = OpVariable %_ptr_Uniform_SSBO16 Uniform
2576 %main = OpFunction %void None %voidf
2577 %label = OpLabel
2578 OpReturn
2579 OpFunctionEnd
2580 )";
2581
2582 CompileSuccessfully(spirv);
2583 EXPECT_EQ(SPV_SUCCESS,
2584 ValidateAndRetrieveValidationState(SPV_ENV_VULKAN_1_0));
2585 }
2586
2587 TEST_F(ValidateDecorations, BlockArrayExtendedAlignmentGood) {
2588 // For uniform buffer, Array base alignment is 16, and ArrayStride
2589 // must be a multiple of 16.
2590 std::string spirv = R"(
2591 OpCapability Shader
2592 OpMemoryModel Logical GLSL450
2593 OpEntryPoint Vertex %main "main"
2594 OpSource GLSL 450
2595 OpDecorate %_arr_float_uint_2 ArrayStride 16
2596 OpMemberDecorate %S 0 Offset 0
2597 OpMemberDecorate %S 1 Offset 16
2598 OpDecorate %S Block
2599 %void = OpTypeVoid
2600 %3 = OpTypeFunction %void
2601 %float = OpTypeFloat 32
2602 %v2float = OpTypeVector %float 2
2603 %uint = OpTypeInt 32 0
2604 %uint_2 = OpConstant %uint 2
2605 %_arr_float_uint_2 = OpTypeArray %float %uint_2
2606 %S = OpTypeStruct %v2float %_arr_float_uint_2
2607 %_ptr_PushConstant_S = OpTypePointer PushConstant %S
2608 %u = OpVariable %_ptr_PushConstant_S PushConstant
2609 %main = OpFunction %void None %3
2610 %5 = OpLabel
2611 OpReturn
2612 OpFunctionEnd
2613 )";
2614
2615 CompileSuccessfully(spirv);
2616 EXPECT_EQ(SPV_SUCCESS, ValidateAndRetrieveValidationState())
2617 << getDiagnosticString();
2618 }
2619
2620 TEST_F(ValidateDecorations, BlockArrayBaseAlignmentBad) {
2621 // For uniform buffer, Array base alignment is 16.
2622 std::string spirv = R"(
2623 OpCapability Shader
2624 OpMemoryModel Logical GLSL450
2625 OpEntryPoint Vertex %main "main"
2626 OpSource GLSL 450
2627 OpDecorate %_arr_float_uint_2 ArrayStride 16
2628 OpMemberDecorate %S 0 Offset 0
2629 OpMemberDecorate %S 1 Offset 8
2630 OpDecorate %S Block
2631 %void = OpTypeVoid
2632 %3 = OpTypeFunction %void
2633 %float = OpTypeFloat 32
2634 %v2float = OpTypeVector %float 2
2635 %uint = OpTypeInt 32 0
2636 %uint_2 = OpConstant %uint 2
2637 %_arr_float_uint_2 = OpTypeArray %float %uint_2
2638 %S = OpTypeStruct %v2float %_arr_float_uint_2
2639 %_ptr_Uniform_S = OpTypePointer Uniform %S
2640 %u = OpVariable %_ptr_Uniform_S Uniform
2641 %main = OpFunction %void None %3
2642 %5 = OpLabel
2643 OpReturn
2644 OpFunctionEnd
2645 )";
2646
2647 CompileSuccessfully(spirv);
2648 EXPECT_EQ(SPV_ERROR_INVALID_ID,
2649 ValidateAndRetrieveValidationState(SPV_ENV_VULKAN_1_0));
2650 EXPECT_THAT(
2651 getDiagnosticString(),
2652 HasSubstr(
2653 "Structure id 3 decorated as Block for variable in Uniform "
2654 "storage class must follow standard uniform buffer layout rules: "
2655 "member 1 at offset 8 is not aligned to 16"));
2656 }
2657
2658 TEST_F(ValidateDecorations, BlockArrayBaseAlignmentWithRelaxedLayoutStillBad) {
2659 // For uniform buffer, Array base alignment is 16, and ArrayStride
2660 // must be a multiple of 16. This case uses relaxed block layout. Relaxed
2661 // layout only relaxes rules for vector alignment, not array alignment.
2662 std::string spirv = R"(
2663 OpCapability Shader
2664 OpMemoryModel Logical GLSL450
2665 OpEntryPoint Vertex %main "main"
2666 OpSource GLSL 450
2667 OpDecorate %_arr_float_uint_2 ArrayStride 16
2668 OpDecorate %u DescriptorSet 0
2669 OpDecorate %u Binding 0
2670 OpMemberDecorate %S 0 Offset 0
2671 OpMemberDecorate %S 1 Offset 8
2672 OpDecorate %S Block
2673 %void = OpTypeVoid
2674 %3 = OpTypeFunction %void
2675 %float = OpTypeFloat 32
2676 %v2float = OpTypeVector %float 2
2677 %uint = OpTypeInt 32 0
2678 %uint_2 = OpConstant %uint 2
2679 %_arr_float_uint_2 = OpTypeArray %float %uint_2
2680 %S = OpTypeStruct %v2float %_arr_float_uint_2
2681 %_ptr_Uniform_S = OpTypePointer Uniform %S
2682 %u = OpVariable %_ptr_Uniform_S Uniform
2683 %main = OpFunction %void None %3
2684 %5 = OpLabel
2685 OpReturn
2686 OpFunctionEnd
2687 )";
2688
2689 CompileSuccessfully(spirv);
2690 EXPECT_EQ(SPV_ERROR_INVALID_ID,
2691 ValidateAndRetrieveValidationState(SPV_ENV_VULKAN_1_0));
2692 spvValidatorOptionsSetRelaxBlockLayout(getValidatorOptions(), true);
2693 EXPECT_THAT(
2694 getDiagnosticString(),
2695 HasSubstr(
2696 "Structure id 4 decorated as Block for variable in Uniform "
2697 "storage class must follow standard uniform buffer layout rules: "
2698 "member 1 at offset 8 is not aligned to 16"));
2699 }
2700
2701 TEST_F(ValidateDecorations, BlockArrayBaseAlignmentWithVulkan1_1StillBad) {
2702 // Same as previous test, but with Vulkan 1.1, which includes
2703 // VK_KHR_relaxed_block_layout in core.
2704 std::string spirv = R"(
2705 OpCapability Shader
2706 OpMemoryModel Logical GLSL450
2707 OpEntryPoint Vertex %main "main"
2708 OpSource GLSL 450
2709 OpDecorate %_arr_float_uint_2 ArrayStride 16
2710 OpDecorate %u DescriptorSet 0
2711 OpDecorate %u Binding 0
2712 OpMemberDecorate %S 0 Offset 0
2713 OpMemberDecorate %S 1 Offset 8
2714 OpDecorate %S Block
2715 %void = OpTypeVoid
2716 %3 = OpTypeFunction %void
2717 %float = OpTypeFloat 32
2718 %v2float = OpTypeVector %float 2
2719 %uint = OpTypeInt 32 0
2720 %uint_2 = OpConstant %uint 2
2721 %_arr_float_uint_2 = OpTypeArray %float %uint_2
2722 %S = OpTypeStruct %v2float %_arr_float_uint_2
2723 %_ptr_Uniform_S = OpTypePointer Uniform %S
2724 %u = OpVariable %_ptr_Uniform_S Uniform
2725 %main = OpFunction %void None %3
2726 %5 = OpLabel
2727 OpReturn
2728 OpFunctionEnd
2729 )";
2730
2731 CompileSuccessfully(spirv);
2732 EXPECT_EQ(SPV_ERROR_INVALID_ID,
2733 ValidateAndRetrieveValidationState(SPV_ENV_VULKAN_1_1));
2734 EXPECT_THAT(
2735 getDiagnosticString(),
2736 HasSubstr(
2737 "Structure id 4 decorated as Block for variable in Uniform "
2738 "storage class must follow relaxed uniform buffer layout rules: "
2739 "member 1 at offset 8 is not aligned to 16"));
2740 }
2741
2742 TEST_F(ValidateDecorations,
2743 BlockArrayBaseAlignmentWithBlockStandardLayoutGood) {
2744 // Same as previous test, but with VK_KHR_uniform_buffer_standard_layout
2745 std::string spirv = R"(
2746 OpCapability Shader
2747 OpMemoryModel Logical GLSL450
2748 OpEntryPoint Vertex %main "main"
2749 OpSource GLSL 450
2750 OpDecorate %_arr_float_uint_2 ArrayStride 16
2751 OpDecorate %u DescriptorSet 0
2752 OpDecorate %u Binding 0
2753 OpMemberDecorate %S 0 Offset 0
2754 OpMemberDecorate %S 1 Offset 8
2755 OpDecorate %S Block
2756 %void = OpTypeVoid
2757 %3 = OpTypeFunction %void
2758 %float = OpTypeFloat 32
2759 %v2float = OpTypeVector %float 2
2760 %uint = OpTypeInt 32 0
2761 %uint_2 = OpConstant %uint 2
2762 %_arr_float_uint_2 = OpTypeArray %float %uint_2
2763 %S = OpTypeStruct %v2float %_arr_float_uint_2
2764 %_ptr_Uniform_S = OpTypePointer Uniform %S
2765 %u = OpVariable %_ptr_Uniform_S Uniform
2766 %main = OpFunction %void None %3
2767 %5 = OpLabel
2768 OpReturn
2769 OpFunctionEnd
2770 )";
2771
2772 CompileSuccessfully(spirv);
2773 spvValidatorOptionsSetUniformBufferStandardLayout(getValidatorOptions(),
2774 true);
2775 EXPECT_EQ(SPV_SUCCESS,
2776 ValidateAndRetrieveValidationState(SPV_ENV_VULKAN_1_0));
2777 EXPECT_THAT(getDiagnosticString(), Eq(""));
2778 }
2779
2780 TEST_F(ValidateDecorations, VulkanBufferBlockOnStorageBufferBad) {
2781 std::string spirv = R"(
2782 OpCapability Shader
2783 OpExtension "SPV_KHR_storage_buffer_storage_class"
2784 OpMemoryModel Logical GLSL450
2785 OpEntryPoint Fragment %1 "main"
2786 OpExecutionMode %1 OriginUpperLeft
2787
2788 OpDecorate %struct BufferBlock
2789
2790 %void = OpTypeVoid
2791 %voidfn = OpTypeFunction %void
2792 %float = OpTypeFloat 32
2793 %struct = OpTypeStruct %float
2794 %ptr = OpTypePointer StorageBuffer %struct
2795 %var = OpVariable %ptr StorageBuffer
2796
2797 %1 = OpFunction %void None %voidfn
2798 %label = OpLabel
2799 OpReturn
2800 OpFunctionEnd
2801 )";
2802
2803 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_1);
2804 EXPECT_EQ(SPV_ERROR_INVALID_ID,
2805 ValidateAndRetrieveValidationState(SPV_ENV_VULKAN_1_1));
2806 EXPECT_THAT(getDiagnosticString(),
2807 AnyVUID("VUID-StandaloneSpirv-PushConstant-06675"));
2808 EXPECT_THAT(getDiagnosticString(),
2809 HasSubstr("In Vulkan, BufferBlock is disallowed on variables in "
2810 "the StorageBuffer storage class"));
2811 }
2812
2813 TEST_F(ValidateDecorations, PushConstantArrayBaseAlignmentGood) {
2814 // Tests https://github.com/KhronosGroup/SPIRV-Tools/issues/1664
2815 // From GLSL vertex shader:
2816 // #version 450
2817 // layout(push_constant) uniform S { vec2 v; float arr[2]; } u;
2818 // void main() { }
2819
2820 std::string spirv = R"(
2821 OpCapability Shader
2822 OpMemoryModel Logical GLSL450
2823 OpEntryPoint Vertex %main "main"
2824 OpSource GLSL 450
2825 OpDecorate %_arr_float_uint_2 ArrayStride 4
2826 OpMemberDecorate %S 0 Offset 0
2827 OpMemberDecorate %S 1 Offset 8
2828 OpDecorate %S Block
2829 %void = OpTypeVoid
2830 %3 = OpTypeFunction %void
2831 %float = OpTypeFloat 32
2832 %v2float = OpTypeVector %float 2
2833 %uint = OpTypeInt 32 0
2834 %uint_2 = OpConstant %uint 2
2835 %_arr_float_uint_2 = OpTypeArray %float %uint_2
2836 %S = OpTypeStruct %v2float %_arr_float_uint_2
2837 %_ptr_PushConstant_S = OpTypePointer PushConstant %S
2838 %u = OpVariable %_ptr_PushConstant_S PushConstant
2839 %main = OpFunction %void None %3
2840 %5 = OpLabel
2841 OpReturn
2842 OpFunctionEnd
2843 )";
2844
2845 CompileSuccessfully(spirv);
2846 EXPECT_EQ(SPV_SUCCESS, ValidateAndRetrieveValidationState(SPV_ENV_VULKAN_1_0))
2847 << getDiagnosticString();
2848 }
2849
2850 TEST_F(ValidateDecorations, PushConstantArrayBadAlignmentBad) {
2851 // Like the previous test, but with offset 7 instead of 8.
2852 std::string spirv = R"(
2853 OpCapability Shader
2854 OpMemoryModel Logical GLSL450
2855 OpEntryPoint Vertex %main "main"
2856 OpSource GLSL 450
2857 OpDecorate %_arr_float_uint_2 ArrayStride 4
2858 OpMemberDecorate %S 0 Offset 0
2859 OpMemberDecorate %S 1 Offset 7
2860 OpDecorate %S Block
2861 %void = OpTypeVoid
2862 %3 = OpTypeFunction %void
2863 %float = OpTypeFloat 32
2864 %v2float = OpTypeVector %float 2
2865 %uint = OpTypeInt 32 0
2866 %uint_2 = OpConstant %uint 2
2867 %_arr_float_uint_2 = OpTypeArray %float %uint_2
2868 %S = OpTypeStruct %v2float %_arr_float_uint_2
2869 %_ptr_PushConstant_S = OpTypePointer PushConstant %S
2870 %u = OpVariable %_ptr_PushConstant_S PushConstant
2871 %main = OpFunction %void None %3
2872 %5 = OpLabel
2873 OpReturn
2874 OpFunctionEnd
2875 )";
2876
2877 CompileSuccessfully(spirv);
2878 EXPECT_EQ(SPV_ERROR_INVALID_ID,
2879 ValidateAndRetrieveValidationState(SPV_ENV_VULKAN_1_0));
2880 EXPECT_THAT(
2881 getDiagnosticString(),
2882 HasSubstr(
2883 "Structure id 3 decorated as Block for variable in PushConstant "
2884 "storage class must follow standard storage buffer layout rules: "
2885 "member 1 at offset 7 is not aligned to 4"));
2886 }
2887
2888 TEST_F(ValidateDecorations,
2889 PushConstantLayoutPermitsTightVec3ScalarPackingGood) {
2890 // See https://github.com/KhronosGroup/SPIRV-Tools/issues/1666
2891 std::string spirv = R"(
2892 OpCapability Shader
2893 OpMemoryModel Logical GLSL450
2894 OpEntryPoint Vertex %main "main"
2895 OpSource GLSL 450
2896 OpMemberDecorate %S 0 Offset 0
2897 OpMemberDecorate %S 1 Offset 12
2898 OpDecorate %S Block
2899 %void = OpTypeVoid
2900 %3 = OpTypeFunction %void
2901 %float = OpTypeFloat 32
2902 %v3float = OpTypeVector %float 3
2903 %S = OpTypeStruct %v3float %float
2904 %_ptr_PushConstant_S = OpTypePointer PushConstant %S
2905 %B = OpVariable %_ptr_PushConstant_S PushConstant
2906 %main = OpFunction %void None %3
2907 %5 = OpLabel
2908 OpReturn
2909 OpFunctionEnd
2910 )";
2911
2912 CompileSuccessfully(spirv);
2913 EXPECT_EQ(SPV_SUCCESS, ValidateAndRetrieveValidationState(SPV_ENV_VULKAN_1_0))
2914 << getDiagnosticString();
2915 }
2916
2917 TEST_F(ValidateDecorations,
2918 PushConstantLayoutForbidsTightScalarVec3PackingBad) {
2919 // See https://github.com/KhronosGroup/SPIRV-Tools/issues/1666
2920 std::string spirv = R"(
2921 OpCapability Shader
2922 OpMemoryModel Logical GLSL450
2923 OpEntryPoint Vertex %main "main"
2924 OpSource GLSL 450
2925 OpMemberDecorate %S 0 Offset 0
2926 OpMemberDecorate %S 1 Offset 4
2927 OpDecorate %S Block
2928 %void = OpTypeVoid
2929 %3 = OpTypeFunction %void
2930 %float = OpTypeFloat 32
2931 %v3float = OpTypeVector %float 3
2932 %S = OpTypeStruct %float %v3float
2933 %_ptr_Uniform_S = OpTypePointer PushConstant %S
2934 %B = OpVariable %_ptr_Uniform_S PushConstant
2935 %main = OpFunction %void None %3
2936 %5 = OpLabel
2937 OpReturn
2938 OpFunctionEnd
2939 )";
2940
2941 CompileSuccessfully(spirv);
2942 EXPECT_EQ(SPV_ERROR_INVALID_ID,
2943 ValidateAndRetrieveValidationState(SPV_ENV_VULKAN_1_0));
2944 EXPECT_THAT(
2945 getDiagnosticString(),
2946 HasSubstr(
2947 "Structure id 2 decorated as Block for variable in PushConstant "
2948 "storage class must follow standard storage buffer layout "
2949 "rules: member 1 at offset 4 is not aligned to 16"));
2950 }
2951
2952 TEST_F(ValidateDecorations, PushConstantMissingBlockGood) {
2953 std::string spirv = R"(
2954 OpCapability Shader
2955 OpMemoryModel Logical GLSL450
2956 OpEntryPoint Fragment %1 "main"
2957 OpExecutionMode %1 OriginUpperLeft
2958
2959 OpMemberDecorate %struct 0 Offset 0
2960
2961 %void = OpTypeVoid
2962 %voidfn = OpTypeFunction %void
2963 %float = OpTypeFloat 32
2964 %struct = OpTypeStruct %float
2965 %ptr = OpTypePointer PushConstant %struct
2966 %pc = OpVariable %ptr PushConstant
2967
2968 %1 = OpFunction %void None %voidfn
2969 %label = OpLabel
2970 OpReturn
2971 OpFunctionEnd
2972 )";
2973
2974 CompileSuccessfully(spirv);
2975 EXPECT_EQ(SPV_SUCCESS, ValidateAndRetrieveValidationState())
2976 << getDiagnosticString();
2977 }
2978
2979 TEST_F(ValidateDecorations, VulkanPushConstantMissingBlockBad) {
2980 std::string spirv = R"(
2981 OpCapability Shader
2982 OpMemoryModel Logical GLSL450
2983 OpEntryPoint Fragment %1 "main"
2984 OpExecutionMode %1 OriginUpperLeft
2985
2986 OpMemberDecorate %struct 0 Offset 0
2987
2988 %void = OpTypeVoid
2989 %voidfn = OpTypeFunction %void
2990 %float = OpTypeFloat 32
2991 %struct = OpTypeStruct %float
2992 %ptr = OpTypePointer PushConstant %struct
2993 %pc = OpVariable %ptr PushConstant
2994
2995 %1 = OpFunction %void None %voidfn
2996 %label = OpLabel
2997 OpReturn
2998 OpFunctionEnd
2999 )";
3000
3001 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_1);
3002 EXPECT_EQ(SPV_ERROR_INVALID_ID,
3003 ValidateAndRetrieveValidationState(SPV_ENV_VULKAN_1_1));
3004 EXPECT_THAT(getDiagnosticString(),
3005 AnyVUID("VUID-StandaloneSpirv-PushConstant-06675"));
3006 EXPECT_THAT(getDiagnosticString(),
3007 HasSubstr("PushConstant id '2' is missing Block decoration.\n"
3008 "From Vulkan spec:\n"
3009 "Such variables must be identified with a Block "
3010 "decoration"));
3011 }
3012
3013 TEST_F(ValidateDecorations, MultiplePushConstantsSingleEntryPointGood) {
3014 std::string spirv = R"(
3015 OpCapability Shader
3016 OpMemoryModel Logical GLSL450
3017 OpEntryPoint Fragment %1 "main"
3018 OpExecutionMode %1 OriginUpperLeft
3019
3020 OpDecorate %struct Block
3021 OpMemberDecorate %struct 0 Offset 0
3022
3023 %void = OpTypeVoid
3024 %voidfn = OpTypeFunction %void
3025 %float = OpTypeFloat 32
3026 %int = OpTypeInt 32 0
3027 %int_0 = OpConstant %int 0
3028 %struct = OpTypeStruct %float
3029 %ptr = OpTypePointer PushConstant %struct
3030 %ptr_float = OpTypePointer PushConstant %float
3031 %pc1 = OpVariable %ptr PushConstant
3032 %pc2 = OpVariable %ptr PushConstant
3033
3034 %1 = OpFunction %void None %voidfn
3035 %label = OpLabel
3036 %2 = OpAccessChain %ptr_float %pc1 %int_0
3037 %3 = OpLoad %float %2
3038 %4 = OpAccessChain %ptr_float %pc2 %int_0
3039 %5 = OpLoad %float %4
3040 OpReturn
3041 OpFunctionEnd
3042 )";
3043
3044 CompileSuccessfully(spirv);
3045 EXPECT_EQ(SPV_SUCCESS, ValidateAndRetrieveValidationState())
3046 << getDiagnosticString();
3047 }
3048
3049 TEST_F(ValidateDecorations,
3050 VulkanMultiplePushConstantsDifferentEntryPointGood) {
3051 std::string spirv = R"(
3052 OpCapability Shader
3053 OpMemoryModel Logical GLSL450
3054 OpEntryPoint Vertex %1 "func1"
3055 OpEntryPoint Fragment %2 "func2"
3056 OpExecutionMode %2 OriginUpperLeft
3057
3058 OpDecorate %struct Block
3059 OpMemberDecorate %struct 0 Offset 0
3060
3061 %void = OpTypeVoid
3062 %voidfn = OpTypeFunction %void
3063 %float = OpTypeFloat 32
3064 %int = OpTypeInt 32 0
3065 %int_0 = OpConstant %int 0
3066 %struct = OpTypeStruct %float
3067 %ptr = OpTypePointer PushConstant %struct
3068 %ptr_float = OpTypePointer PushConstant %float
3069 %pc1 = OpVariable %ptr PushConstant
3070 %pc2 = OpVariable %ptr PushConstant
3071
3072 %1 = OpFunction %void None %voidfn
3073 %label1 = OpLabel
3074 %3 = OpAccessChain %ptr_float %pc1 %int_0
3075 %4 = OpLoad %float %3
3076 OpReturn
3077 OpFunctionEnd
3078
3079 %2 = OpFunction %void None %voidfn
3080 %label2 = OpLabel
3081 %5 = OpAccessChain %ptr_float %pc2 %int_0
3082 %6 = OpLoad %float %5
3083 OpReturn
3084 OpFunctionEnd
3085 )";
3086
3087 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_1);
3088 EXPECT_EQ(SPV_SUCCESS, ValidateAndRetrieveValidationState(SPV_ENV_VULKAN_1_1))
3089 << getDiagnosticString();
3090 }
3091
3092 TEST_F(ValidateDecorations,
3093 VulkanMultiplePushConstantsUnusedSingleEntryPointGood) {
3094 std::string spirv = R"(
3095 OpCapability Shader
3096 OpMemoryModel Logical GLSL450
3097 OpEntryPoint Fragment %1 "main"
3098 OpExecutionMode %1 OriginUpperLeft
3099
3100 OpDecorate %struct Block
3101 OpMemberDecorate %struct 0 Offset 0
3102
3103 %void = OpTypeVoid
3104 %voidfn = OpTypeFunction %void
3105 %float = OpTypeFloat 32
3106 %int = OpTypeInt 32 0
3107 %int_0 = OpConstant %int 0
3108 %struct = OpTypeStruct %float
3109 %ptr = OpTypePointer PushConstant %struct
3110 %ptr_float = OpTypePointer PushConstant %float
3111 %pc1 = OpVariable %ptr PushConstant
3112 %pc2 = OpVariable %ptr PushConstant
3113
3114 %1 = OpFunction %void None %voidfn
3115 %label = OpLabel
3116 OpReturn
3117 OpFunctionEnd
3118 )";
3119
3120 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_1);
3121 EXPECT_EQ(SPV_SUCCESS, ValidateAndRetrieveValidationState(SPV_ENV_VULKAN_1_1))
3122 << getDiagnosticString();
3123 }
3124
3125 TEST_F(ValidateDecorations, VulkanMultiplePushConstantsSingleEntryPointBad) {
3126 std::string spirv = R"(
3127 OpCapability Shader
3128 OpMemoryModel Logical GLSL450
3129 OpEntryPoint Fragment %1 "main"
3130 OpExecutionMode %1 OriginUpperLeft
3131
3132 OpDecorate %struct Block
3133 OpMemberDecorate %struct 0 Offset 0
3134
3135 %void = OpTypeVoid
3136 %voidfn = OpTypeFunction %void
3137 %float = OpTypeFloat 32
3138 %int = OpTypeInt 32 0
3139 %int_0 = OpConstant %int 0
3140 %struct = OpTypeStruct %float
3141 %ptr = OpTypePointer PushConstant %struct
3142 %ptr_float = OpTypePointer PushConstant %float
3143 %pc1 = OpVariable %ptr PushConstant
3144 %pc2 = OpVariable %ptr PushConstant
3145
3146 %1 = OpFunction %void None %voidfn
3147 %label = OpLabel
3148 %2 = OpAccessChain %ptr_float %pc1 %int_0
3149 %3 = OpLoad %float %2
3150 %4 = OpAccessChain %ptr_float %pc2 %int_0
3151 %5 = OpLoad %float %4
3152 OpReturn
3153 OpFunctionEnd
3154 )";
3155
3156 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_1);
3157 EXPECT_EQ(SPV_ERROR_INVALID_ID,
3158 ValidateAndRetrieveValidationState(SPV_ENV_VULKAN_1_1));
3159 EXPECT_THAT(getDiagnosticString(),
3160 AnyVUID("VUID-StandaloneSpirv-OpEntryPoint-06674"));
3161 EXPECT_THAT(
3162 getDiagnosticString(),
3163 HasSubstr(
3164 "Entry point id '1' uses more than one PushConstant interface.\n"
3165 "From Vulkan spec:\n"
3166 "There must be no more than one push constant block "
3167 "statically used per shader entry point."));
3168 }
3169
3170 TEST_F(ValidateDecorations,
3171 VulkanMultiplePushConstantsDifferentEntryPointSubFunctionGood) {
3172 std::string spirv = R"(
3173 OpCapability Shader
3174 OpMemoryModel Logical GLSL450
3175 OpEntryPoint Vertex %1 "func1"
3176 OpEntryPoint Fragment %2 "func2"
3177 OpExecutionMode %2 OriginUpperLeft
3178
3179 OpDecorate %struct Block
3180 OpMemberDecorate %struct 0 Offset 0
3181
3182 %void = OpTypeVoid
3183 %voidfn = OpTypeFunction %void
3184 %float = OpTypeFloat 32
3185 %int = OpTypeInt 32 0
3186 %int_0 = OpConstant %int 0
3187 %struct = OpTypeStruct %float
3188 %ptr = OpTypePointer PushConstant %struct
3189 %ptr_float = OpTypePointer PushConstant %float
3190 %pc1 = OpVariable %ptr PushConstant
3191 %pc2 = OpVariable %ptr PushConstant
3192
3193 %sub1 = OpFunction %void None %voidfn
3194 %label_sub1 = OpLabel
3195 %3 = OpAccessChain %ptr_float %pc1 %int_0
3196 %4 = OpLoad %float %3
3197 OpReturn
3198 OpFunctionEnd
3199
3200 %sub2 = OpFunction %void None %voidfn
3201 %label_sub2 = OpLabel
3202 %5 = OpAccessChain %ptr_float %pc2 %int_0
3203 %6 = OpLoad %float %5
3204 OpReturn
3205 OpFunctionEnd
3206
3207 %1 = OpFunction %void None %voidfn
3208 %label1 = OpLabel
3209 %call1 = OpFunctionCall %void %sub1
3210 OpReturn
3211 OpFunctionEnd
3212
3213 %2 = OpFunction %void None %voidfn
3214 %label2 = OpLabel
3215 %call2 = OpFunctionCall %void %sub2
3216 OpReturn
3217 OpFunctionEnd
3218 )";
3219
3220 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_1);
3221 EXPECT_EQ(SPV_SUCCESS, ValidateAndRetrieveValidationState(SPV_ENV_VULKAN_1_1))
3222 << getDiagnosticString();
3223 }
3224
3225 TEST_F(ValidateDecorations,
3226 VulkanMultiplePushConstantsSingleEntryPointSubFunctionBad) {
3227 std::string spirv = R"(
3228 OpCapability Shader
3229 OpMemoryModel Logical GLSL450
3230 OpEntryPoint Fragment %1 "main"
3231 OpExecutionMode %1 OriginUpperLeft
3232
3233 OpDecorate %struct Block
3234 OpMemberDecorate %struct 0 Offset 0
3235
3236 %void = OpTypeVoid
3237 %voidfn = OpTypeFunction %void
3238 %float = OpTypeFloat 32
3239 %int = OpTypeInt 32 0
3240 %int_0 = OpConstant %int 0
3241 %struct = OpTypeStruct %float
3242 %ptr = OpTypePointer PushConstant %struct
3243 %ptr_float = OpTypePointer PushConstant %float
3244 %pc1 = OpVariable %ptr PushConstant
3245 %pc2 = OpVariable %ptr PushConstant
3246
3247 %sub1 = OpFunction %void None %voidfn
3248 %label_sub1 = OpLabel
3249 %3 = OpAccessChain %ptr_float %pc1 %int_0
3250 %4 = OpLoad %float %3
3251 OpReturn
3252 OpFunctionEnd
3253
3254 %sub2 = OpFunction %void None %voidfn
3255 %label_sub2 = OpLabel
3256 %5 = OpAccessChain %ptr_float %pc2 %int_0
3257 %6 = OpLoad %float %5
3258 OpReturn
3259 OpFunctionEnd
3260
3261 %1 = OpFunction %void None %voidfn
3262 %label1 = OpLabel
3263 %call1 = OpFunctionCall %void %sub1
3264 %call2 = OpFunctionCall %void %sub2
3265 OpReturn
3266 OpFunctionEnd
3267 )";
3268
3269 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_1);
3270 EXPECT_EQ(SPV_ERROR_INVALID_ID,
3271 ValidateAndRetrieveValidationState(SPV_ENV_VULKAN_1_1));
3272 EXPECT_THAT(getDiagnosticString(),
3273 AnyVUID("VUID-StandaloneSpirv-OpEntryPoint-06674"));
3274 EXPECT_THAT(
3275 getDiagnosticString(),
3276 HasSubstr(
3277 "Entry point id '1' uses more than one PushConstant interface.\n"
3278 "From Vulkan spec:\n"
3279 "There must be no more than one push constant block "
3280 "statically used per shader entry point."));
3281 }
3282
3283 TEST_F(ValidateDecorations,
3284 VulkanMultiplePushConstantsSingleEntryPointInterfaceBad) {
3285 std::string spirv = R"(
3286 OpCapability Shader
3287 OpMemoryModel Logical GLSL450
3288 OpEntryPoint Vertex %func1 "func1" %pc1 %pc2
3289 OpDecorate %struct Block
3290 OpMemberDecorate %struct 0 Offset 0
3291 %void = OpTypeVoid
3292 %voidfn = OpTypeFunction %void
3293 %float = OpTypeFloat 32
3294 %int = OpTypeInt 32 0
3295 %int_0 = OpConstant %int 0
3296 %struct = OpTypeStruct %float
3297 %ptr = OpTypePointer PushConstant %struct
3298 %ptr_float = OpTypePointer PushConstant %float
3299 %pc1 = OpVariable %ptr PushConstant
3300 %pc2 = OpVariable %ptr PushConstant
3301 %func1 = OpFunction %void None %voidfn
3302 %label1 = OpLabel
3303 %access1 = OpAccessChain %ptr_float %pc1 %int_0
3304 %load1 = OpLoad %float %access1
3305 OpReturn
3306 OpFunctionEnd
3307 %func2 = OpFunction %void None %voidfn
3308 %label2 = OpLabel
3309 %access2 = OpAccessChain %ptr_float %pc2 %int_0
3310 %load2 = OpLoad %float %access2
3311 OpReturn
3312 OpFunctionEnd
3313 )";
3314
3315 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_2);
3316 EXPECT_EQ(SPV_ERROR_INVALID_DATA,
3317 ValidateAndRetrieveValidationState(SPV_ENV_VULKAN_1_2));
3318 EXPECT_THAT(getDiagnosticString(),
3319 AnyVUID("VUID-StandaloneSpirv-OpVariable-06673"));
3320 EXPECT_THAT(getDiagnosticString(),
3321 HasSubstr("Entry-point has more than one variable with the "
3322 "PushConstant storage class in the interface"));
3323 }
3324
3325 TEST_F(ValidateDecorations, VulkanUniformMissingDescriptorSetBad) {
3326 std::string spirv = R"(
3327 OpCapability Shader
3328 OpMemoryModel Logical GLSL450
3329 OpEntryPoint Fragment %1 "main"
3330 OpExecutionMode %1 OriginUpperLeft
3331
3332 OpDecorate %struct Block
3333 OpMemberDecorate %struct 0 Offset 0
3334 OpDecorate %var Binding 0
3335
3336 %void = OpTypeVoid
3337 %voidfn = OpTypeFunction %void
3338 %float = OpTypeFloat 32
3339 %struct = OpTypeStruct %float
3340 %ptr = OpTypePointer Uniform %struct
3341 %ptr_float = OpTypePointer Uniform %float
3342 %var = OpVariable %ptr Uniform
3343 %int = OpTypeInt 32 0
3344 %int_0 = OpConstant %int 0
3345
3346 %1 = OpFunction %void None %voidfn
3347 %label = OpLabel
3348 %2 = OpAccessChain %ptr_float %var %int_0
3349 %3 = OpLoad %float %2
3350 OpReturn
3351 OpFunctionEnd
3352 )";
3353
3354 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_1);
3355 EXPECT_EQ(SPV_ERROR_INVALID_ID,
3356 ValidateAndRetrieveValidationState(SPV_ENV_VULKAN_1_1));
3357 EXPECT_THAT(getDiagnosticString(),
3358 AnyVUID("VUID-StandaloneSpirv-UniformConstant-06677"));
3359 EXPECT_THAT(getDiagnosticString(),
3360 HasSubstr("Uniform id '3' is missing DescriptorSet decoration.\n"
3361 "From Vulkan spec:\n"
3362 "These variables must have DescriptorSet and Binding "
3363 "decorations specified"));
3364 }
3365
3366 TEST_F(ValidateDecorations, VulkanUniformMissingBindingBad) {
3367 std::string spirv = R"(
3368 OpCapability Shader
3369 OpMemoryModel Logical GLSL450
3370 OpEntryPoint Fragment %1 "main"
3371 OpExecutionMode %1 OriginUpperLeft
3372
3373 OpDecorate %struct Block
3374 OpMemberDecorate %struct 0 Offset 0
3375 OpDecorate %var DescriptorSet 0
3376
3377 %void = OpTypeVoid
3378 %voidfn = OpTypeFunction %void
3379 %float = OpTypeFloat 32
3380 %struct = OpTypeStruct %float
3381 %ptr = OpTypePointer Uniform %struct
3382 %ptr_float = OpTypePointer Uniform %float
3383 %var = OpVariable %ptr Uniform
3384 %int = OpTypeInt 32 0
3385 %int_0 = OpConstant %int 0
3386
3387 %1 = OpFunction %void None %voidfn
3388 %label = OpLabel
3389 %2 = OpAccessChain %ptr_float %var %int_0
3390 %3 = OpLoad %float %2
3391 OpReturn
3392 OpFunctionEnd
3393 )";
3394
3395 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_1);
3396 EXPECT_EQ(SPV_ERROR_INVALID_ID,
3397 ValidateAndRetrieveValidationState(SPV_ENV_VULKAN_1_1));
3398 EXPECT_THAT(getDiagnosticString(),
3399 AnyVUID("VUID-StandaloneSpirv-UniformConstant-06677"));
3400 EXPECT_THAT(getDiagnosticString(),
3401 HasSubstr("Uniform id '3' is missing Binding decoration.\n"
3402 "From Vulkan spec:\n"
3403 "These variables must have DescriptorSet and Binding "
3404 "decorations specified"));
3405 }
3406
3407 TEST_F(ValidateDecorations, VulkanUniformConstantMissingDescriptorSetBad) {
3408 std::string spirv = R"(
3409 OpCapability Shader
3410 OpMemoryModel Logical GLSL450
3411 OpEntryPoint Fragment %1 "main"
3412 OpExecutionMode %1 OriginUpperLeft
3413
3414 OpDecorate %var Binding 0
3415
3416 %void = OpTypeVoid
3417 %voidfn = OpTypeFunction %void
3418 %sampler = OpTypeSampler
3419 %ptr = OpTypePointer UniformConstant %sampler
3420 %var = OpVariable %ptr UniformConstant
3421
3422 %1 = OpFunction %void None %voidfn
3423 %label = OpLabel
3424 %2 = OpLoad %sampler %var
3425 OpReturn
3426 OpFunctionEnd
3427 )";
3428
3429 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_1);
3430 EXPECT_EQ(SPV_ERROR_INVALID_ID,
3431 ValidateAndRetrieveValidationState(SPV_ENV_VULKAN_1_1));
3432 EXPECT_THAT(getDiagnosticString(),
3433 AnyVUID("VUID-StandaloneSpirv-UniformConstant-06677"));
3434 EXPECT_THAT(
3435 getDiagnosticString(),
3436 HasSubstr("UniformConstant id '2' is missing DescriptorSet decoration.\n"
3437 "From Vulkan spec:\n"
3438 "These variables must have DescriptorSet and Binding "
3439 "decorations specified"));
3440 }
3441
3442 TEST_F(ValidateDecorations, VulkanUniformConstantMissingBindingBad) {
3443 std::string spirv = R"(
3444 OpCapability Shader
3445 OpMemoryModel Logical GLSL450
3446 OpEntryPoint Fragment %1 "main"
3447 OpExecutionMode %1 OriginUpperLeft
3448
3449 OpDecorate %var DescriptorSet 0
3450
3451 %void = OpTypeVoid
3452 %voidfn = OpTypeFunction %void
3453 %sampler = OpTypeSampler
3454 %ptr = OpTypePointer UniformConstant %sampler
3455 %var = OpVariable %ptr UniformConstant
3456
3457 %1 = OpFunction %void None %voidfn
3458 %label = OpLabel
3459 %2 = OpLoad %sampler %var
3460 OpReturn
3461 OpFunctionEnd
3462 )";
3463
3464 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_1);
3465 EXPECT_EQ(SPV_ERROR_INVALID_ID,
3466 ValidateAndRetrieveValidationState(SPV_ENV_VULKAN_1_1));
3467 EXPECT_THAT(getDiagnosticString(),
3468 AnyVUID("VUID-StandaloneSpirv-UniformConstant-06677"));
3469 EXPECT_THAT(
3470 getDiagnosticString(),
3471 HasSubstr("UniformConstant id '2' is missing Binding decoration.\n"
3472 "From Vulkan spec:\n"
3473 "These variables must have DescriptorSet and Binding "
3474 "decorations specified"));
3475 }
3476
3477 TEST_F(ValidateDecorations, VulkanStorageBufferMissingDescriptorSetBad) {
3478 std::string spirv = R"(
3479 OpCapability Shader
3480 OpExtension "SPV_KHR_storage_buffer_storage_class"
3481 OpMemoryModel Logical GLSL450
3482 OpEntryPoint Fragment %1 "main"
3483 OpExecutionMode %1 OriginUpperLeft
3484
3485 OpDecorate %struct Block
3486 OpMemberDecorate %struct 0 Offset 0
3487 OpDecorate %var Binding 0
3488
3489 %void = OpTypeVoid
3490 %voidfn = OpTypeFunction %void
3491 %float = OpTypeFloat 32
3492 %struct = OpTypeStruct %float
3493 %ptr = OpTypePointer StorageBuffer %struct
3494 %var = OpVariable %ptr StorageBuffer
3495 %ptr_float = OpTypePointer StorageBuffer %float
3496 %int = OpTypeInt 32 0
3497 %int_0 = OpConstant %int 0
3498
3499 %1 = OpFunction %void None %voidfn
3500 %label = OpLabel
3501 %2 = OpAccessChain %ptr_float %var %int_0
3502 %3 = OpLoad %float %2
3503 OpReturn
3504 OpFunctionEnd
3505 )";
3506
3507 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_1);
3508 EXPECT_EQ(SPV_ERROR_INVALID_ID,
3509 ValidateAndRetrieveValidationState(SPV_ENV_VULKAN_1_1));
3510 EXPECT_THAT(getDiagnosticString(),
3511 AnyVUID("VUID-StandaloneSpirv-UniformConstant-06677"));
3512 EXPECT_THAT(
3513 getDiagnosticString(),
3514 HasSubstr("StorageBuffer id '3' is missing DescriptorSet decoration.\n"
3515 "From Vulkan spec:\n"
3516 "These variables must have DescriptorSet and Binding "
3517 "decorations specified"));
3518 }
3519
3520 TEST_F(ValidateDecorations, VulkanStorageBufferMissingBindingBad) {
3521 std::string spirv = R"(
3522 OpCapability Shader
3523 OpExtension "SPV_KHR_storage_buffer_storage_class"
3524 OpMemoryModel Logical GLSL450
3525 OpEntryPoint Fragment %1 "main"
3526 OpExecutionMode %1 OriginUpperLeft
3527
3528 OpDecorate %struct Block
3529 OpMemberDecorate %struct 0 Offset 0
3530 OpDecorate %var DescriptorSet 0
3531
3532 %void = OpTypeVoid
3533 %voidfn = OpTypeFunction %void
3534 %float = OpTypeFloat 32
3535 %struct = OpTypeStruct %float
3536 %ptr = OpTypePointer StorageBuffer %struct
3537 %var = OpVariable %ptr StorageBuffer
3538 %ptr_float = OpTypePointer StorageBuffer %float
3539 %int = OpTypeInt 32 0
3540 %int_0 = OpConstant %int 0
3541
3542 %1 = OpFunction %void None %voidfn
3543 %label = OpLabel
3544 %2 = OpAccessChain %ptr_float %var %int_0
3545 %3 = OpLoad %float %2
3546 OpReturn
3547 OpFunctionEnd
3548 )";
3549
3550 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_1);
3551 EXPECT_EQ(SPV_ERROR_INVALID_ID,
3552 ValidateAndRetrieveValidationState(SPV_ENV_VULKAN_1_1));
3553 EXPECT_THAT(getDiagnosticString(),
3554 AnyVUID("VUID-StandaloneSpirv-UniformConstant-06677"));
3555 EXPECT_THAT(getDiagnosticString(),
3556 HasSubstr("StorageBuffer id '3' is missing Binding decoration.\n"
3557 "From Vulkan spec:\n"
3558 "These variables must have DescriptorSet and Binding "
3559 "decorations specified"));
3560 }
3561
3562 TEST_F(ValidateDecorations,
3563 VulkanStorageBufferMissingDescriptorSetSubFunctionBad) {
3564 std::string spirv = R"(
3565 OpCapability Shader
3566 OpExtension "SPV_KHR_storage_buffer_storage_class"
3567 OpMemoryModel Logical GLSL450
3568 OpEntryPoint Fragment %1 "main"
3569 OpExecutionMode %1 OriginUpperLeft
3570
3571 OpDecorate %struct Block
3572 OpMemberDecorate %struct 0 Offset 0
3573 OpDecorate %var Binding 0
3574
3575 %void = OpTypeVoid
3576 %voidfn = OpTypeFunction %void
3577 %float = OpTypeFloat 32
3578 %struct = OpTypeStruct %float
3579 %ptr = OpTypePointer StorageBuffer %struct
3580 %var = OpVariable %ptr StorageBuffer
3581 %ptr_float = OpTypePointer StorageBuffer %float
3582 %int = OpTypeInt 32 0
3583 %int_0 = OpConstant %int 0
3584
3585 %1 = OpFunction %void None %voidfn
3586 %label = OpLabel
3587 %call = OpFunctionCall %void %2
3588 OpReturn
3589 OpFunctionEnd
3590 %2 = OpFunction %void None %voidfn
3591 %label2 = OpLabel
3592 %3 = OpAccessChain %ptr_float %var %int_0
3593 %4 = OpLoad %float %3
3594 OpReturn
3595 OpFunctionEnd
3596 )";
3597
3598 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_1);
3599 EXPECT_EQ(SPV_ERROR_INVALID_ID,
3600 ValidateAndRetrieveValidationState(SPV_ENV_VULKAN_1_1));
3601 EXPECT_THAT(getDiagnosticString(),
3602 AnyVUID("VUID-StandaloneSpirv-UniformConstant-06677"));
3603 EXPECT_THAT(
3604 getDiagnosticString(),
3605 HasSubstr("StorageBuffer id '3' is missing DescriptorSet decoration.\n"
3606 "From Vulkan spec:\n"
3607 "These variables must have DescriptorSet and Binding "
3608 "decorations specified"));
3609 }
3610
3611 TEST_F(ValidateDecorations, UniformMissingDescriptorSetGood) {
3612 std::string spirv = R"(
3613 OpCapability Shader
3614 OpMemoryModel Logical GLSL450
3615 OpEntryPoint Fragment %1 "main"
3616 OpExecutionMode %1 OriginUpperLeft
3617
3618 OpDecorate %struct Block
3619 OpMemberDecorate %struct 0 Offset 0
3620 OpDecorate %var Binding 0
3621
3622 %void = OpTypeVoid
3623 %voidfn = OpTypeFunction %void
3624 %float = OpTypeFloat 32
3625 %struct = OpTypeStruct %float
3626 %ptr = OpTypePointer Uniform %struct
3627 %var = OpVariable %ptr Uniform
3628
3629 %1 = OpFunction %void None %voidfn
3630 %label = OpLabel
3631 OpReturn
3632 OpFunctionEnd
3633 )";
3634
3635 CompileSuccessfully(spirv);
3636 EXPECT_EQ(SPV_SUCCESS, ValidateAndRetrieveValidationState())
3637 << getDiagnosticString();
3638 }
3639
3640 TEST_F(ValidateDecorations, UniformMissingBindingGood) {
3641 std::string spirv = R"(
3642 OpCapability Shader
3643 OpMemoryModel Logical GLSL450
3644 OpEntryPoint Fragment %1 "main"
3645 OpExecutionMode %1 OriginUpperLeft
3646
3647 OpDecorate %struct Block
3648 OpMemberDecorate %struct 0 Offset 0
3649 OpDecorate %var DescriptorSet 0
3650
3651 %void = OpTypeVoid
3652 %voidfn = OpTypeFunction %void
3653 %float = OpTypeFloat 32
3654 %struct = OpTypeStruct %float
3655 %ptr = OpTypePointer Uniform %struct
3656 %var = OpVariable %ptr Uniform
3657
3658 %1 = OpFunction %void None %voidfn
3659 %label = OpLabel
3660 OpReturn
3661 OpFunctionEnd
3662 )";
3663
3664 CompileSuccessfully(spirv);
3665 EXPECT_EQ(SPV_SUCCESS, ValidateAndRetrieveValidationState())
3666 << getDiagnosticString();
3667 }
3668
3669 TEST_F(ValidateDecorations, UniformConstantMissingDescriptorSetGood) {
3670 std::string spirv = R"(
3671 OpCapability Shader
3672 OpMemoryModel Logical GLSL450
3673 OpEntryPoint Fragment %1 "main"
3674 OpExecutionMode %1 OriginUpperLeft
3675
3676 OpDecorate %var Binding 0
3677
3678 %void = OpTypeVoid
3679 %voidfn = OpTypeFunction %void
3680 %sampler = OpTypeSampler
3681 %ptr = OpTypePointer UniformConstant %sampler
3682 %var = OpVariable %ptr UniformConstant
3683
3684 %1 = OpFunction %void None %voidfn
3685 %label = OpLabel
3686 OpReturn
3687 OpFunctionEnd
3688 )";
3689
3690 CompileSuccessfully(spirv);
3691 EXPECT_EQ(SPV_SUCCESS, ValidateAndRetrieveValidationState())
3692 << getDiagnosticString();
3693 }
3694
3695 TEST_F(ValidateDecorations, UniformConstantMissingBindingGood) {
3696 std::string spirv = R"(
3697 OpCapability Shader
3698 OpMemoryModel Logical GLSL450
3699 OpEntryPoint Fragment %1 "main"
3700 OpExecutionMode %1 OriginUpperLeft
3701
3702 OpDecorate %var DescriptorSet 0
3703
3704 %void = OpTypeVoid
3705 %voidfn = OpTypeFunction %void
3706 %sampler = OpTypeSampler
3707 %ptr = OpTypePointer UniformConstant %sampler
3708 %var = OpVariable %ptr UniformConstant
3709
3710 %1 = OpFunction %void None %voidfn
3711 %label = OpLabel
3712 OpReturn
3713 OpFunctionEnd
3714 )";
3715
3716 CompileSuccessfully(spirv);
3717 EXPECT_EQ(SPV_SUCCESS, ValidateAndRetrieveValidationState())
3718 << getDiagnosticString();
3719 }
3720
3721 TEST_F(ValidateDecorations, StorageBufferMissingDescriptorSetGood) {
3722 std::string spirv = R"(
3723 OpCapability Shader
3724 OpExtension "SPV_KHR_storage_buffer_storage_class"
3725 OpMemoryModel Logical GLSL450
3726 OpEntryPoint Fragment %1 "main"
3727 OpExecutionMode %1 OriginUpperLeft
3728
3729 OpDecorate %struct BufferBlock
3730 OpDecorate %var Binding 0
3731
3732 %void = OpTypeVoid
3733 %voidfn = OpTypeFunction %void
3734 %float = OpTypeFloat 32
3735 %struct = OpTypeStruct %float
3736 %ptr = OpTypePointer StorageBuffer %struct
3737 %var = OpVariable %ptr StorageBuffer
3738
3739 %1 = OpFunction %void None %voidfn
3740 %label = OpLabel
3741 OpReturn
3742 OpFunctionEnd
3743 )";
3744
3745 CompileSuccessfully(spirv);
3746 EXPECT_EQ(SPV_SUCCESS, ValidateAndRetrieveValidationState())
3747 << getDiagnosticString();
3748 }
3749
3750 TEST_F(ValidateDecorations, StorageBufferMissingBindingGood) {
3751 std::string spirv = R"(
3752 OpCapability Shader
3753 OpExtension "SPV_KHR_storage_buffer_storage_class"
3754 OpMemoryModel Logical GLSL450
3755 OpEntryPoint Fragment %1 "main"
3756 OpExecutionMode %1 OriginUpperLeft
3757
3758 OpDecorate %struct BufferBlock
3759 OpDecorate %var DescriptorSet 0
3760
3761 %void = OpTypeVoid
3762 %voidfn = OpTypeFunction %void
3763 %float = OpTypeFloat 32
3764 %struct = OpTypeStruct %float
3765 %ptr = OpTypePointer StorageBuffer %struct
3766 %var = OpVariable %ptr StorageBuffer
3767
3768 %1 = OpFunction %void None %voidfn
3769 %label = OpLabel
3770 OpReturn
3771 OpFunctionEnd
3772 )";
3773
3774 CompileSuccessfully(spirv);
3775 EXPECT_EQ(SPV_SUCCESS, ValidateAndRetrieveValidationState())
3776 << getDiagnosticString();
3777 }
3778
3779 TEST_F(ValidateDecorations, StorageBufferStorageClassArrayBaseAlignmentGood) {
3780 // Spot check buffer rules when using StorageBuffer storage class with Block
3781 // decoration.
3782 std::string spirv = R"(
3783 OpCapability Shader
3784 OpExtension "SPV_KHR_storage_buffer_storage_class"
3785 OpMemoryModel Logical GLSL450
3786 OpEntryPoint Vertex %main "main"
3787 OpSource GLSL 450
3788 OpDecorate %_arr_float_uint_2 ArrayStride 4
3789 OpMemberDecorate %S 0 Offset 0
3790 OpMemberDecorate %S 1 Offset 8
3791 OpDecorate %S Block
3792 OpDecorate %u DescriptorSet 0
3793 OpDecorate %u Binding 0
3794 %void = OpTypeVoid
3795 %3 = OpTypeFunction %void
3796 %float = OpTypeFloat 32
3797 %v2float = OpTypeVector %float 2
3798 %uint = OpTypeInt 32 0
3799 %uint_2 = OpConstant %uint 2
3800 %_arr_float_uint_2 = OpTypeArray %float %uint_2
3801 %S = OpTypeStruct %v2float %_arr_float_uint_2
3802 %_ptr_Uniform_S = OpTypePointer StorageBuffer %S
3803 %u = OpVariable %_ptr_Uniform_S StorageBuffer
3804 %main = OpFunction %void None %3
3805 %5 = OpLabel
3806 OpReturn
3807 OpFunctionEnd
3808 )";
3809
3810 CompileSuccessfully(spirv);
3811 EXPECT_EQ(SPV_SUCCESS, ValidateAndRetrieveValidationState(SPV_ENV_VULKAN_1_0))
3812 << getDiagnosticString();
3813 }
3814
3815 TEST_F(ValidateDecorations, StorageBufferStorageClassArrayBadAlignmentBad) {
3816 // Like the previous test, but with offset 7.
3817 std::string spirv = R"(
3818 OpCapability Shader
3819 OpExtension "SPV_KHR_storage_buffer_storage_class"
3820 OpMemoryModel Logical GLSL450
3821 OpEntryPoint Vertex %main "main"
3822 OpSource GLSL 450
3823 OpDecorate %_arr_float_uint_2 ArrayStride 4
3824 OpMemberDecorate %S 0 Offset 0
3825 OpMemberDecorate %S 1 Offset 7
3826 OpDecorate %S Block
3827 OpDecorate %u DescriptorSet 0
3828 OpDecorate %u Binding 0
3829 %void = OpTypeVoid
3830 %3 = OpTypeFunction %void
3831 %float = OpTypeFloat 32
3832 %v2float = OpTypeVector %float 2
3833 %uint = OpTypeInt 32 0
3834 %uint_2 = OpConstant %uint 2
3835 %_arr_float_uint_2 = OpTypeArray %float %uint_2
3836 %S = OpTypeStruct %v2float %_arr_float_uint_2
3837 %_ptr_Uniform_S = OpTypePointer StorageBuffer %S
3838 %u = OpVariable %_ptr_Uniform_S StorageBuffer
3839 %main = OpFunction %void None %3
3840 %5 = OpLabel
3841 OpReturn
3842 OpFunctionEnd
3843 )";
3844
3845 CompileSuccessfully(spirv);
3846 EXPECT_EQ(SPV_ERROR_INVALID_ID,
3847 ValidateAndRetrieveValidationState(SPV_ENV_VULKAN_1_0));
3848 EXPECT_THAT(
3849 getDiagnosticString(),
3850 HasSubstr(
3851 "Structure id 3 decorated as Block for variable in StorageBuffer "
3852 "storage class must follow standard storage buffer layout rules: "
3853 "member 1 at offset 7 is not aligned to 4"));
3854 }
3855
3856 TEST_F(ValidateDecorations, BufferBlockStandardStorageBufferLayout) {
3857 std::string spirv = R"(
3858 OpCapability Shader
3859 %1 = OpExtInstImport "GLSL.std.450"
3860 OpMemoryModel Logical GLSL450
3861 OpEntryPoint GLCompute %main "main"
3862 OpExecutionMode %main LocalSize 1 1 1
3863 OpSource GLSL 430
3864 OpMemberDecorate %F 0 Offset 0
3865 OpMemberDecorate %F 1 Offset 8
3866 OpDecorate %_arr_float_uint_2 ArrayStride 4
3867 OpDecorate %_arr_mat3v3float_uint_2 ArrayStride 48
3868 OpMemberDecorate %O 0 Offset 0
3869 OpMemberDecorate %O 1 Offset 16
3870 OpMemberDecorate %O 2 Offset 24
3871 OpMemberDecorate %O 3 Offset 32
3872 OpMemberDecorate %O 4 ColMajor
3873 OpMemberDecorate %O 4 Offset 48
3874 OpMemberDecorate %O 4 MatrixStride 16
3875 OpDecorate %_arr_O_uint_2 ArrayStride 144
3876 OpMemberDecorate %Output 0 Offset 0
3877 OpMemberDecorate %Output 1 Offset 8
3878 OpMemberDecorate %Output 2 Offset 16
3879 OpMemberDecorate %Output 3 Offset 32
3880 OpMemberDecorate %Output 4 Offset 48
3881 OpMemberDecorate %Output 5 Offset 52
3882 OpMemberDecorate %Output 6 ColMajor
3883 OpMemberDecorate %Output 6 Offset 64
3884 OpMemberDecorate %Output 6 MatrixStride 16
3885 OpMemberDecorate %Output 7 Offset 96
3886 OpDecorate %Output BufferBlock
3887 OpDecorate %dataOutput DescriptorSet 0
3888 OpDecorate %dataOutput Binding 0
3889 %void = OpTypeVoid
3890 %3 = OpTypeFunction %void
3891 %float = OpTypeFloat 32
3892 %v2float = OpTypeVector %float 2
3893 %v3float = OpTypeVector %float 3
3894 %int = OpTypeInt 32 1
3895 %uint = OpTypeInt 32 0
3896 %v2uint = OpTypeVector %uint 2
3897 %F = OpTypeStruct %int %v2uint
3898 %uint_2 = OpConstant %uint 2
3899 %_arr_float_uint_2 = OpTypeArray %float %uint_2
3900 %mat2v3float = OpTypeMatrix %v3float 2
3901 %v3uint = OpTypeVector %uint 3
3902 %mat3v3float = OpTypeMatrix %v3float 3
3903 %_arr_mat3v3float_uint_2 = OpTypeArray %mat3v3float %uint_2
3904 %O = OpTypeStruct %v3uint %v2float %_arr_float_uint_2 %v2float %_arr_mat3v3float_uint_2
3905 %_arr_O_uint_2 = OpTypeArray %O %uint_2
3906 %Output = OpTypeStruct %float %v2float %v3float %F %float %_arr_float_uint_2 %mat2v3float %_arr_O_uint_2
3907 %_ptr_Uniform_Output = OpTypePointer Uniform %Output
3908 %dataOutput = OpVariable %_ptr_Uniform_Output Uniform
3909 %main = OpFunction %void None %3
3910 %5 = OpLabel
3911 OpReturn
3912 OpFunctionEnd
3913 )";
3914
3915 CompileSuccessfully(spirv);
3916 EXPECT_EQ(SPV_SUCCESS,
3917 ValidateAndRetrieveValidationState(SPV_ENV_VULKAN_1_0));
3918 }
3919
3920 TEST_F(ValidateDecorations,
3921 StorageBufferLayoutPermitsTightVec3ScalarPackingGood) {
3922 // See https://github.com/KhronosGroup/SPIRV-Tools/issues/1666
3923 std::string spirv = R"(
3924 OpCapability Shader
3925 OpExtension "SPV_KHR_storage_buffer_storage_class"
3926 OpMemoryModel Logical GLSL450
3927 OpEntryPoint Vertex %main "main"
3928 OpSource GLSL 450
3929 OpMemberDecorate %S 0 Offset 0
3930 OpMemberDecorate %S 1 Offset 12
3931 OpDecorate %S Block
3932 OpDecorate %B DescriptorSet 0
3933 OpDecorate %B Binding 0
3934 %void = OpTypeVoid
3935 %3 = OpTypeFunction %void
3936 %float = OpTypeFloat 32
3937 %v3float = OpTypeVector %float 3
3938 %S = OpTypeStruct %v3float %float
3939 %_ptr_StorageBuffer_S = OpTypePointer StorageBuffer %S
3940 %B = OpVariable %_ptr_StorageBuffer_S StorageBuffer
3941 %main = OpFunction %void None %3
3942 %5 = OpLabel
3943 OpReturn
3944 OpFunctionEnd
3945 )";
3946
3947 CompileSuccessfully(spirv);
3948 EXPECT_EQ(SPV_SUCCESS, ValidateAndRetrieveValidationState(SPV_ENV_VULKAN_1_0))
3949 << getDiagnosticString();
3950 }
3951
3952 TEST_F(ValidateDecorations,
3953 StorageBufferLayoutForbidsTightScalarVec3PackingBad) {
3954 // See https://github.com/KhronosGroup/SPIRV-Tools/issues/1666
3955 std::string spirv = R"(
3956 OpCapability Shader
3957 OpExtension "SPV_KHR_storage_buffer_storage_class"
3958 OpMemoryModel Logical GLSL450
3959 OpEntryPoint Vertex %main "main"
3960 OpSource GLSL 450
3961 OpMemberDecorate %S 0 Offset 0
3962 OpMemberDecorate %S 1 Offset 4
3963 OpDecorate %S Block
3964 OpDecorate %B DescriptorSet 0
3965 OpDecorate %B Binding 0
3966 %void = OpTypeVoid
3967 %3 = OpTypeFunction %void
3968 %float = OpTypeFloat 32
3969 %v3float = OpTypeVector %float 3
3970 %S = OpTypeStruct %float %v3float
3971 %_ptr_StorageBuffer_S = OpTypePointer StorageBuffer %S
3972 %B = OpVariable %_ptr_StorageBuffer_S StorageBuffer
3973 %main = OpFunction %void None %3
3974 %5 = OpLabel
3975 OpReturn
3976 OpFunctionEnd
3977 )";
3978
3979 CompileSuccessfully(spirv);
3980 EXPECT_EQ(SPV_ERROR_INVALID_ID,
3981 ValidateAndRetrieveValidationState(SPV_ENV_VULKAN_1_0));
3982 EXPECT_THAT(
3983 getDiagnosticString(),
3984 HasSubstr(
3985 "Structure id 2 decorated as Block for variable in StorageBuffer "
3986 "storage class must follow standard storage buffer layout "
3987 "rules: member 1 at offset 4 is not aligned to 16"));
3988 }
3989
3990 TEST_F(ValidateDecorations,
3991 BlockStandardUniformBufferLayoutIncorrectOffset0Bad) {
3992 std::string spirv = R"(
3993 OpCapability Shader
3994 %1 = OpExtInstImport "GLSL.std.450"
3995 OpMemoryModel Logical GLSL450
3996 OpEntryPoint GLCompute %main "main"
3997 OpExecutionMode %main LocalSize 1 1 1
3998 OpSource GLSL 430
3999 OpMemberDecorate %F 0 Offset 0
4000 OpMemberDecorate %F 1 Offset 8
4001 OpDecorate %_arr_float_uint_2 ArrayStride 16
4002 OpDecorate %_arr_mat3v3float_uint_2 ArrayStride 48
4003 OpMemberDecorate %O 0 Offset 0
4004 OpMemberDecorate %O 1 Offset 16
4005 OpMemberDecorate %O 2 Offset 24
4006 OpMemberDecorate %O 3 Offset 33
4007 OpMemberDecorate %O 4 ColMajor
4008 OpMemberDecorate %O 4 Offset 80
4009 OpMemberDecorate %O 4 MatrixStride 16
4010 OpDecorate %_arr_O_uint_2 ArrayStride 176
4011 OpMemberDecorate %Output 0 Offset 0
4012 OpMemberDecorate %Output 1 Offset 8
4013 OpMemberDecorate %Output 2 Offset 16
4014 OpMemberDecorate %Output 3 Offset 32
4015 OpMemberDecorate %Output 4 Offset 48
4016 OpMemberDecorate %Output 5 Offset 64
4017 OpMemberDecorate %Output 6 ColMajor
4018 OpMemberDecorate %Output 6 Offset 96
4019 OpMemberDecorate %Output 6 MatrixStride 16
4020 OpMemberDecorate %Output 7 Offset 128
4021 OpDecorate %Output Block
4022 OpDecorate %dataOutput DescriptorSet 0
4023 OpDecorate %dataOutput Binding 0
4024 %void = OpTypeVoid
4025 %3 = OpTypeFunction %void
4026 %float = OpTypeFloat 32
4027 %v2float = OpTypeVector %float 2
4028 %v3float = OpTypeVector %float 3
4029 %int = OpTypeInt 32 1
4030 %uint = OpTypeInt 32 0
4031 %v2uint = OpTypeVector %uint 2
4032 %F = OpTypeStruct %int %v2uint
4033 %uint_2 = OpConstant %uint 2
4034 %_arr_float_uint_2 = OpTypeArray %float %uint_2
4035 %mat2v3float = OpTypeMatrix %v3float 2
4036 %v3uint = OpTypeVector %uint 3
4037 %mat3v3float = OpTypeMatrix %v3float 3
4038 %_arr_mat3v3float_uint_2 = OpTypeArray %mat3v3float %uint_2
4039 %O = OpTypeStruct %v3uint %v2float %_arr_float_uint_2 %v2float %_arr_mat3v3float_uint_2
4040 %_arr_O_uint_2 = OpTypeArray %O %uint_2
4041 %Output = OpTypeStruct %float %v2float %v3float %F %float %_arr_float_uint_2 %mat2v3float %_arr_O_uint_2
4042 %_ptr_Uniform_Output = OpTypePointer Uniform %Output
4043 %dataOutput = OpVariable %_ptr_Uniform_Output Uniform
4044 %main = OpFunction %void None %3
4045 %5 = OpLabel
4046 OpReturn
4047 OpFunctionEnd
4048 )";
4049
4050 CompileSuccessfully(spirv);
4051 EXPECT_EQ(SPV_ERROR_INVALID_ID,
4052 ValidateAndRetrieveValidationState(SPV_ENV_VULKAN_1_0));
4053 EXPECT_THAT(
4054 getDiagnosticString(),
4055 HasSubstr("Structure id 6 decorated as Block for variable in Uniform "
4056 "storage class must follow standard uniform buffer layout "
4057 "rules: member 2 at offset 152 is not aligned to 16"));
4058 }
4059
4060 TEST_F(ValidateDecorations,
4061 BlockStandardUniformBufferLayoutIncorrectOffset1Bad) {
4062 std::string spirv = R"(
4063 OpCapability Shader
4064 %1 = OpExtInstImport "GLSL.std.450"
4065 OpMemoryModel Logical GLSL450
4066 OpEntryPoint GLCompute %main "main"
4067 OpExecutionMode %main LocalSize 1 1 1
4068 OpSource GLSL 430
4069 OpMemberDecorate %F 0 Offset 0
4070 OpMemberDecorate %F 1 Offset 8
4071 OpDecorate %_arr_float_uint_2 ArrayStride 16
4072 OpDecorate %_arr_mat3v3float_uint_2 ArrayStride 48
4073 OpMemberDecorate %O 0 Offset 0
4074 OpMemberDecorate %O 1 Offset 16
4075 OpMemberDecorate %O 2 Offset 32
4076 OpMemberDecorate %O 3 Offset 64
4077 OpMemberDecorate %O 4 ColMajor
4078 OpMemberDecorate %O 4 Offset 80
4079 OpMemberDecorate %O 4 MatrixStride 16
4080 OpDecorate %_arr_O_uint_2 ArrayStride 176
4081 OpMemberDecorate %Output 0 Offset 0
4082 OpMemberDecorate %Output 1 Offset 8
4083 OpMemberDecorate %Output 2 Offset 16
4084 OpMemberDecorate %Output 3 Offset 32
4085 OpMemberDecorate %Output 4 Offset 48
4086 OpMemberDecorate %Output 5 Offset 71
4087 OpMemberDecorate %Output 6 ColMajor
4088 OpMemberDecorate %Output 6 Offset 96
4089 OpMemberDecorate %Output 6 MatrixStride 16
4090 OpMemberDecorate %Output 7 Offset 128
4091 OpDecorate %Output Block
4092 OpDecorate %dataOutput DescriptorSet 0
4093 OpDecorate %dataOutput Binding 0
4094 %void = OpTypeVoid
4095 %3 = OpTypeFunction %void
4096 %float = OpTypeFloat 32
4097 %v2float = OpTypeVector %float 2
4098 %v3float = OpTypeVector %float 3
4099 %int = OpTypeInt 32 1
4100 %uint = OpTypeInt 32 0
4101 %v2uint = OpTypeVector %uint 2
4102 %F = OpTypeStruct %int %v2uint
4103 %uint_2 = OpConstant %uint 2
4104 %_arr_float_uint_2 = OpTypeArray %float %uint_2
4105 %mat2v3float = OpTypeMatrix %v3float 2
4106 %v3uint = OpTypeVector %uint 3
4107 %mat3v3float = OpTypeMatrix %v3float 3
4108 %_arr_mat3v3float_uint_2 = OpTypeArray %mat3v3float %uint_2
4109 %O = OpTypeStruct %v3uint %v2float %_arr_float_uint_2 %v2float %_arr_mat3v3float_uint_2
4110 %_arr_O_uint_2 = OpTypeArray %O %uint_2
4111 %Output = OpTypeStruct %float %v2float %v3float %F %float %_arr_float_uint_2 %mat2v3float %_arr_O_uint_2
4112 %_ptr_Uniform_Output = OpTypePointer Uniform %Output
4113 %dataOutput = OpVariable %_ptr_Uniform_Output Uniform
4114 %main = OpFunction %void None %3
4115 %5 = OpLabel
4116 OpReturn
4117 OpFunctionEnd
4118 )";
4119
4120 CompileSuccessfully(spirv);
4121 EXPECT_EQ(SPV_ERROR_INVALID_ID,
4122 ValidateAndRetrieveValidationState(SPV_ENV_VULKAN_1_0));
4123 EXPECT_THAT(
4124 getDiagnosticString(),
4125 HasSubstr("Structure id 8 decorated as Block for variable in Uniform "
4126 "storage class must follow standard uniform buffer layout "
4127 "rules: member 5 at offset 71 is not aligned to 16"));
4128 }
4129
4130 TEST_F(ValidateDecorations, BlockUniformBufferLayoutIncorrectArrayStrideBad) {
4131 std::string spirv = R"(
4132 OpCapability Shader
4133 %1 = OpExtInstImport "GLSL.std.450"
4134 OpMemoryModel Logical GLSL450
4135 OpEntryPoint GLCompute %main "main"
4136 OpExecutionMode %main LocalSize 1 1 1
4137 OpSource GLSL 430
4138 OpMemberDecorate %F 0 Offset 0
4139 OpMemberDecorate %F 1 Offset 8
4140 OpDecorate %_arr_float_uint_2 ArrayStride 16
4141 OpDecorate %_arr_mat3v3float_uint_2 ArrayStride 49
4142 OpMemberDecorate %O 0 Offset 0
4143 OpMemberDecorate %O 1 Offset 16
4144 OpMemberDecorate %O 2 Offset 32
4145 OpMemberDecorate %O 3 Offset 64
4146 OpMemberDecorate %O 4 ColMajor
4147 OpMemberDecorate %O 4 Offset 80
4148 OpMemberDecorate %O 4 MatrixStride 16
4149 OpDecorate %_arr_O_uint_2 ArrayStride 176
4150 OpMemberDecorate %Output 0 Offset 0
4151 OpMemberDecorate %Output 1 Offset 8
4152 OpMemberDecorate %Output 2 Offset 16
4153 OpMemberDecorate %Output 3 Offset 32
4154 OpMemberDecorate %Output 4 Offset 48
4155 OpMemberDecorate %Output 5 Offset 64
4156 OpMemberDecorate %Output 6 ColMajor
4157 OpMemberDecorate %Output 6 Offset 96
4158 OpMemberDecorate %Output 6 MatrixStride 16
4159 OpMemberDecorate %Output 7 Offset 128
4160 OpDecorate %Output Block
4161 %void = OpTypeVoid
4162 %3 = OpTypeFunction %void
4163 %float = OpTypeFloat 32
4164 %v2float = OpTypeVector %float 2
4165 %v3float = OpTypeVector %float 3
4166 %int = OpTypeInt 32 1
4167 %uint = OpTypeInt 32 0
4168 %v2uint = OpTypeVector %uint 2
4169 %F = OpTypeStruct %int %v2uint
4170 %uint_2 = OpConstant %uint 2
4171 %_arr_float_uint_2 = OpTypeArray %float %uint_2
4172 %mat2v3float = OpTypeMatrix %v3float 2
4173 %v3uint = OpTypeVector %uint 3
4174 %mat3v3float = OpTypeMatrix %v3float 3
4175 %_arr_mat3v3float_uint_2 = OpTypeArray %mat3v3float %uint_2
4176 %O = OpTypeStruct %v3uint %v2float %_arr_float_uint_2 %v2float %_arr_mat3v3float_uint_2
4177 %_arr_O_uint_2 = OpTypeArray %O %uint_2
4178 %Output = OpTypeStruct %float %v2float %v3float %F %float %_arr_float_uint_2 %mat2v3float %_arr_O_uint_2
4179 %_ptr_Uniform_Output = OpTypePointer Uniform %Output
4180 %dataOutput = OpVariable %_ptr_Uniform_Output Uniform
4181 %main = OpFunction %void None %3
4182 %5 = OpLabel
4183 OpReturn
4184 OpFunctionEnd
4185 )";
4186
4187 CompileSuccessfully(spirv);
4188 EXPECT_EQ(SPV_ERROR_INVALID_ID,
4189 ValidateAndRetrieveValidationState(SPV_ENV_VULKAN_1_0));
4190 EXPECT_THAT(
4191 getDiagnosticString(),
4192 HasSubstr(
4193 "Structure id 6 decorated as Block for variable in Uniform storage "
4194 "class must follow standard uniform buffer layout rules: member 4 "
4195 "contains "
4196 "an array with stride 49 not satisfying alignment to 16"));
4197 }
4198
4199 TEST_F(ValidateDecorations,
4200 BufferBlockStandardStorageBufferLayoutImproperStraddleBad) {
4201 std::string spirv = R"(
4202 OpCapability Shader
4203 %1 = OpExtInstImport "GLSL.std.450"
4204 OpMemoryModel Logical GLSL450
4205 OpEntryPoint GLCompute %main "main"
4206 OpExecutionMode %main LocalSize 1 1 1
4207 OpSource GLSL 430
4208 OpMemberDecorate %Output 0 Offset 0
4209 OpMemberDecorate %Output 1 Offset 8
4210 OpDecorate %Output BufferBlock
4211 %void = OpTypeVoid
4212 %3 = OpTypeFunction %void
4213 %float = OpTypeFloat 32
4214 %v3float = OpTypeVector %float 3
4215 %Output = OpTypeStruct %float %v3float
4216 %_ptr_Uniform_Output = OpTypePointer Uniform %Output
4217 %dataOutput = OpVariable %_ptr_Uniform_Output Uniform
4218 %main = OpFunction %void None %3
4219 %5 = OpLabel
4220 OpReturn
4221 OpFunctionEnd
4222 )";
4223
4224 CompileSuccessfully(spirv);
4225 EXPECT_EQ(SPV_ERROR_INVALID_ID,
4226 ValidateAndRetrieveValidationState(SPV_ENV_VULKAN_1_0));
4227 EXPECT_THAT(
4228 getDiagnosticString(),
4229 HasSubstr("Structure id 3 decorated as BufferBlock for variable in "
4230 "Uniform storage class must follow standard storage buffer "
4231 "layout rules: member 1 at offset 8 is not aligned to 16"));
4232 }
4233
4234 TEST_F(ValidateDecorations,
4235 BlockUniformBufferLayoutOffsetInsideArrayPaddingBad) {
4236 // In this case the 2nd member fits entirely within the padding.
4237 std::string spirv = R"(
4238 OpCapability Shader
4239 %1 = OpExtInstImport "GLSL.std.450"
4240 OpMemoryModel Logical GLSL450
4241 OpEntryPoint GLCompute %main "main"
4242 OpExecutionMode %main LocalSize 1 1 1
4243 OpSource GLSL 430
4244 OpDecorate %_arr_float_uint_2 ArrayStride 16
4245 OpMemberDecorate %Output 0 Offset 0
4246 OpMemberDecorate %Output 1 Offset 20
4247 OpDecorate %Output Block
4248 %void = OpTypeVoid
4249 %3 = OpTypeFunction %void
4250 %float = OpTypeFloat 32
4251 %uint = OpTypeInt 32 0
4252 %v2uint = OpTypeVector %uint 2
4253 %uint_2 = OpConstant %uint 2
4254 %_arr_float_uint_2 = OpTypeArray %float %uint_2
4255 %Output = OpTypeStruct %_arr_float_uint_2 %float
4256 %_ptr_Uniform_Output = OpTypePointer Uniform %Output
4257 %dataOutput = OpVariable %_ptr_Uniform_Output Uniform
4258 %main = OpFunction %void None %3
4259 %5 = OpLabel
4260 OpReturn
4261 OpFunctionEnd
4262 )";
4263
4264 CompileSuccessfully(spirv);
4265 EXPECT_EQ(SPV_ERROR_INVALID_ID,
4266 ValidateAndRetrieveValidationState(SPV_ENV_VULKAN_1_0));
4267 EXPECT_THAT(
4268 getDiagnosticString(),
4269 HasSubstr(
4270 "Structure id 4 decorated as Block for variable in Uniform storage "
4271 "class must follow standard uniform buffer layout rules: member 1 at "
4272 "offset 20 overlaps previous member ending at offset 31"));
4273 }
4274
4275 TEST_F(ValidateDecorations,
4276 BlockUniformBufferLayoutOffsetInsideStructPaddingBad) {
4277 // In this case the 2nd member fits entirely within the padding.
4278 std::string spirv = R"(
4279 OpCapability Shader
4280 OpMemoryModel Logical GLSL450
4281 OpEntryPoint GLCompute %1 "main"
4282 OpExecutionMode %1 LocalSize 1 1 1
4283 OpMemberDecorate %_struct_6 0 Offset 0
4284 OpMemberDecorate %_struct_2 0 Offset 0
4285 OpMemberDecorate %_struct_2 1 Offset 4
4286 OpDecorate %_struct_2 Block
4287 %void = OpTypeVoid
4288 %4 = OpTypeFunction %void
4289 %float = OpTypeFloat 32
4290 %_struct_6 = OpTypeStruct %float
4291 %_struct_2 = OpTypeStruct %_struct_6 %float
4292 %_ptr_Uniform__struct_2 = OpTypePointer Uniform %_struct_2
4293 %8 = OpVariable %_ptr_Uniform__struct_2 Uniform
4294 %1 = OpFunction %void None %4
4295 %9 = OpLabel
4296 OpReturn
4297 OpFunctionEnd
4298 )";
4299
4300 CompileSuccessfully(spirv);
4301 EXPECT_EQ(SPV_ERROR_INVALID_ID,
4302 ValidateAndRetrieveValidationState(SPV_ENV_VULKAN_1_0));
4303 EXPECT_THAT(
4304 getDiagnosticString(),
4305 HasSubstr(
4306 "Structure id 3 decorated as Block for variable in Uniform storage "
4307 "class must follow standard uniform buffer layout rules: member 1 at "
4308 "offset 4 overlaps previous member ending at offset 15"));
4309 }
4310
4311 TEST_F(ValidateDecorations, BlockLayoutOffsetOutOfOrderGoodUniversal1_0) {
4312 std::string spirv = R"(
4313 OpCapability Shader
4314 %1 = OpExtInstImport "GLSL.std.450"
4315 OpMemoryModel Logical GLSL450
4316 OpEntryPoint GLCompute %main "main"
4317 OpExecutionMode %main LocalSize 1 1 1
4318 OpMemberDecorate %Outer 0 Offset 4
4319 OpMemberDecorate %Outer 1 Offset 0
4320 OpDecorate %Outer Block
4321 OpDecorate %O DescriptorSet 0
4322 OpDecorate %O Binding 0
4323 %void = OpTypeVoid
4324 %3 = OpTypeFunction %void
4325 %uint = OpTypeInt 32 0
4326 %Outer = OpTypeStruct %uint %uint
4327 %_ptr_Uniform_Outer = OpTypePointer Uniform %Outer
4328 %O = OpVariable %_ptr_Uniform_Outer Uniform
4329 %main = OpFunction %void None %3
4330 %5 = OpLabel
4331 OpReturn
4332 OpFunctionEnd
4333 )";
4334
4335 CompileSuccessfully(spirv);
4336 EXPECT_EQ(SPV_SUCCESS,
4337 ValidateAndRetrieveValidationState(SPV_ENV_UNIVERSAL_1_0));
4338 }
4339
4340 TEST_F(ValidateDecorations, BlockLayoutOffsetOutOfOrderGoodOpenGL4_5) {
4341 std::string spirv = R"(
4342 OpCapability Shader
4343 %1 = OpExtInstImport "GLSL.std.450"
4344 OpMemoryModel Logical GLSL450
4345 OpEntryPoint GLCompute %main "main"
4346 OpExecutionMode %main LocalSize 1 1 1
4347 OpMemberDecorate %Outer 0 Offset 4
4348 OpMemberDecorate %Outer 1 Offset 0
4349 OpDecorate %Outer Block
4350 OpDecorate %O DescriptorSet 0
4351 OpDecorate %O Binding 0
4352 %void = OpTypeVoid
4353 %3 = OpTypeFunction %void
4354 %uint = OpTypeInt 32 0
4355 %Outer = OpTypeStruct %uint %uint
4356 %_ptr_Uniform_Outer = OpTypePointer Uniform %Outer
4357 %O = OpVariable %_ptr_Uniform_Outer Uniform
4358 %main = OpFunction %void None %3
4359 %5 = OpLabel
4360 OpReturn
4361 OpFunctionEnd
4362 )";
4363
4364 CompileSuccessfully(spirv);
4365 EXPECT_EQ(SPV_SUCCESS,
4366 ValidateAndRetrieveValidationState(SPV_ENV_OPENGL_4_5));
4367 }
4368
4369 TEST_F(ValidateDecorations, BlockLayoutOffsetOutOfOrderGoodVulkan1_1) {
4370 std::string spirv = R"(
4371 OpCapability Shader
4372 %1 = OpExtInstImport "GLSL.std.450"
4373 OpMemoryModel Logical GLSL450
4374 OpEntryPoint GLCompute %main "main"
4375 OpExecutionMode %main LocalSize 1 1 1
4376 OpMemberDecorate %Outer 0 Offset 4
4377 OpMemberDecorate %Outer 1 Offset 0
4378 OpDecorate %Outer Block
4379 OpDecorate %O DescriptorSet 0
4380 OpDecorate %O Binding 0
4381 %void = OpTypeVoid
4382 %3 = OpTypeFunction %void
4383 %uint = OpTypeInt 32 0
4384 %Outer = OpTypeStruct %uint %uint
4385 %_ptr_Uniform_Outer = OpTypePointer Uniform %Outer
4386 %O = OpVariable %_ptr_Uniform_Outer Uniform
4387 %main = OpFunction %void None %3
4388 %5 = OpLabel
4389 OpReturn
4390 OpFunctionEnd
4391 )";
4392
4393 CompileSuccessfully(spirv);
4394 EXPECT_EQ(SPV_SUCCESS, ValidateAndRetrieveValidationState(SPV_ENV_VULKAN_1_1))
4395 << getDiagnosticString();
4396 EXPECT_THAT(getDiagnosticString(), Eq(""));
4397 }
4398
4399 TEST_F(ValidateDecorations, BlockLayoutOffsetOverlapBad) {
4400 std::string spirv = R"(
4401 OpCapability Shader
4402 %1 = OpExtInstImport "GLSL.std.450"
4403 OpMemoryModel Logical GLSL450
4404 OpEntryPoint GLCompute %main "main"
4405 OpExecutionMode %main LocalSize 1 1 1
4406 OpMemberDecorate %Outer 0 Offset 0
4407 OpMemberDecorate %Outer 1 Offset 16
4408 OpMemberDecorate %Inner 0 Offset 0
4409 OpMemberDecorate %Inner 1 Offset 16
4410 OpDecorate %Outer Block
4411 OpDecorate %O DescriptorSet 0
4412 OpDecorate %O Binding 0
4413 %void = OpTypeVoid
4414 %3 = OpTypeFunction %void
4415 %uint = OpTypeInt 32 0
4416 %Inner = OpTypeStruct %uint %uint
4417 %Outer = OpTypeStruct %Inner %uint
4418 %_ptr_Uniform_Outer = OpTypePointer Uniform %Outer
4419 %O = OpVariable %_ptr_Uniform_Outer Uniform
4420 %main = OpFunction %void None %3
4421 %5 = OpLabel
4422 OpReturn
4423 OpFunctionEnd
4424 )";
4425
4426 CompileSuccessfully(spirv);
4427 EXPECT_EQ(SPV_ERROR_INVALID_ID,
4428 ValidateAndRetrieveValidationState(SPV_ENV_VULKAN_1_0));
4429 EXPECT_THAT(
4430 getDiagnosticString(),
4431 HasSubstr(
4432 "Structure id 3 decorated as Block for variable in Uniform storage "
4433 "class must follow standard uniform buffer layout rules: member 1 at "
4434 "offset 16 overlaps previous member ending at offset 31"));
4435 }
4436
4437 TEST_F(ValidateDecorations, BufferBlockEmptyStruct) {
4438 std::string spirv = R"(
4439 OpCapability Shader
4440 %1 = OpExtInstImport "GLSL.std.450"
4441 OpMemoryModel Logical GLSL450
4442 OpEntryPoint GLCompute %main "main"
4443 OpExecutionMode %main LocalSize 1 1 1
4444 OpSource GLSL 430
4445 OpMemberDecorate %Output 0 Offset 0
4446 OpDecorate %Output BufferBlock
4447 %void = OpTypeVoid
4448 %3 = OpTypeFunction %void
4449 %S = OpTypeStruct
4450 %Output = OpTypeStruct %S
4451 %_ptr_Uniform_Output = OpTypePointer Uniform %Output
4452 %dataOutput = OpVariable %_ptr_Uniform_Output Uniform
4453 %main = OpFunction %void None %3
4454 %5 = OpLabel
4455 OpReturn
4456 OpFunctionEnd
4457 )";
4458
4459 CompileSuccessfully(spirv);
4460 EXPECT_EQ(SPV_SUCCESS, ValidateAndRetrieveValidationState());
4461 }
4462
4463 TEST_F(ValidateDecorations, RowMajorMatrixTightPackingGood) {
4464 // Row major matrix rule:
4465 // A row-major matrix of C columns has a base alignment equal to
4466 // the base alignment of a vector of C matrix components.
4467 // Note: The "matrix component" is the scalar element type.
4468
4469 // The matrix has 3 columns and 2 rows (C=3, R=2).
4470 // So the base alignment of b is the same as a vector of 3 floats, which is 16
4471 // bytes. The matrix consists of two of these, and therefore occupies 2 x 16
4472 // bytes, or 32 bytes.
4473 //
4474 // So the offsets can be:
4475 // a -> 0
4476 // b -> 16
4477 // c -> 48
4478 // d -> 60 ; d fits at bytes 12-15 after offset of c. Tight (vec3;float)
4479 // packing
4480
4481 std::string spirv = R"(
4482 OpCapability Shader
4483 OpMemoryModel Logical GLSL450
4484 OpEntryPoint Vertex %1 "main"
4485 OpSource GLSL 450
4486 OpMemberDecorate %_struct_2 0 Offset 0
4487 OpMemberDecorate %_struct_2 1 RowMajor
4488 OpMemberDecorate %_struct_2 1 Offset 16
4489 OpMemberDecorate %_struct_2 1 MatrixStride 16
4490 OpMemberDecorate %_struct_2 2 Offset 48
4491 OpMemberDecorate %_struct_2 3 Offset 60
4492 OpDecorate %_struct_2 Block
4493 OpDecorate %3 DescriptorSet 0
4494 OpDecorate %3 Binding 0
4495 %void = OpTypeVoid
4496 %5 = OpTypeFunction %void
4497 %float = OpTypeFloat 32
4498 %v4float = OpTypeVector %float 4
4499 %v2float = OpTypeVector %float 2
4500 %mat3v2float = OpTypeMatrix %v2float 3
4501 %v3float = OpTypeVector %float 3
4502 %_struct_2 = OpTypeStruct %v4float %mat3v2float %v3float %float
4503 %_ptr_Uniform__struct_2 = OpTypePointer Uniform %_struct_2
4504 %3 = OpVariable %_ptr_Uniform__struct_2 Uniform
4505 %1 = OpFunction %void None %5
4506 %12 = OpLabel
4507 OpReturn
4508 OpFunctionEnd
4509 )";
4510
4511 CompileSuccessfully(spirv);
4512 EXPECT_EQ(SPV_SUCCESS, ValidateAndRetrieveValidationState())
4513 << getDiagnosticString();
4514 }
4515
4516 TEST_F(ValidateDecorations, ArrayArrayRowMajorMatrixTightPackingGood) {
4517 // Like the previous case, but we have an array of arrays of matrices.
4518 // The RowMajor decoration goes on the struct member (surprisingly).
4519
4520 std::string spirv = R"(
4521 OpCapability Shader
4522 OpMemoryModel Logical GLSL450
4523 OpEntryPoint Vertex %1 "main"
4524 OpSource GLSL 450
4525 OpMemberDecorate %_struct_2 0 Offset 0
4526 OpMemberDecorate %_struct_2 1 RowMajor
4527 OpMemberDecorate %_struct_2 1 Offset 16
4528 OpMemberDecorate %_struct_2 1 MatrixStride 16
4529 OpMemberDecorate %_struct_2 2 Offset 80
4530 OpMemberDecorate %_struct_2 3 Offset 92
4531 OpDecorate %arr_mat ArrayStride 32
4532 OpDecorate %arr_arr_mat ArrayStride 32
4533 OpDecorate %_struct_2 Block
4534 OpDecorate %3 DescriptorSet 0
4535 OpDecorate %3 Binding 0
4536 %void = OpTypeVoid
4537 %5 = OpTypeFunction %void
4538 %float = OpTypeFloat 32
4539 %v4float = OpTypeVector %float 4
4540 %v2float = OpTypeVector %float 2
4541 %mat3v2float = OpTypeMatrix %v2float 3
4542 %uint = OpTypeInt 32 0
4543 %uint_1 = OpConstant %uint 1
4544 %uint_2 = OpConstant %uint 2
4545 %arr_mat = OpTypeArray %mat3v2float %uint_1
4546 %arr_arr_mat = OpTypeArray %arr_mat %uint_2
4547 %v3float = OpTypeVector %float 3
4548 %_struct_2 = OpTypeStruct %v4float %arr_arr_mat %v3float %float
4549 %_ptr_Uniform__struct_2 = OpTypePointer Uniform %_struct_2
4550 %3 = OpVariable %_ptr_Uniform__struct_2 Uniform
4551 %1 = OpFunction %void None %5
4552 %12 = OpLabel
4553 OpReturn
4554 OpFunctionEnd
4555 )";
4556
4557 CompileSuccessfully(spirv);
4558 EXPECT_EQ(SPV_SUCCESS, ValidateAndRetrieveValidationState(SPV_ENV_VULKAN_1_0))
4559 << getDiagnosticString();
4560 }
4561
4562 TEST_F(ValidateDecorations, ArrayArrayRowMajorMatrixNextMemberOverlapsBad) {
4563 // Like the previous case, but the offset of member 2 overlaps the matrix.
4564 std::string spirv = R"(
4565 OpCapability Shader
4566 OpMemoryModel Logical GLSL450
4567 OpEntryPoint Vertex %1 "main"
4568 OpSource GLSL 450
4569 OpMemberDecorate %_struct_2 0 Offset 0
4570 OpMemberDecorate %_struct_2 1 RowMajor
4571 OpMemberDecorate %_struct_2 1 Offset 16
4572 OpMemberDecorate %_struct_2 1 MatrixStride 16
4573 OpMemberDecorate %_struct_2 2 Offset 64
4574 OpMemberDecorate %_struct_2 3 Offset 92
4575 OpDecorate %arr_mat ArrayStride 32
4576 OpDecorate %arr_arr_mat ArrayStride 32
4577 OpDecorate %_struct_2 Block
4578 OpDecorate %3 DescriptorSet 0
4579 OpDecorate %3 Binding 0
4580 %void = OpTypeVoid
4581 %5 = OpTypeFunction %void
4582 %float = OpTypeFloat 32
4583 %v4float = OpTypeVector %float 4
4584 %v2float = OpTypeVector %float 2
4585 %mat3v2float = OpTypeMatrix %v2float 3
4586 %uint = OpTypeInt 32 0
4587 %uint_1 = OpConstant %uint 1
4588 %uint_2 = OpConstant %uint 2
4589 %arr_mat = OpTypeArray %mat3v2float %uint_1
4590 %arr_arr_mat = OpTypeArray %arr_mat %uint_2
4591 %v3float = OpTypeVector %float 3
4592 %_struct_2 = OpTypeStruct %v4float %arr_arr_mat %v3float %float
4593 %_ptr_Uniform__struct_2 = OpTypePointer Uniform %_struct_2
4594 %3 = OpVariable %_ptr_Uniform__struct_2 Uniform
4595 %1 = OpFunction %void None %5
4596 %12 = OpLabel
4597 OpReturn
4598 OpFunctionEnd
4599 )";
4600
4601 CompileSuccessfully(spirv);
4602 EXPECT_EQ(SPV_ERROR_INVALID_ID,
4603 ValidateAndRetrieveValidationState(SPV_ENV_VULKAN_1_0));
4604 EXPECT_THAT(
4605 getDiagnosticString(),
4606 HasSubstr(
4607 "Structure id 2 decorated as Block for variable in Uniform storage "
4608 "class must follow standard uniform buffer layout rules: member 2 at "
4609 "offset 64 overlaps previous member ending at offset 79"));
4610 }
4611
4612 TEST_F(ValidateDecorations, StorageBufferArraySizeCalculationPackGood) {
4613 // Original GLSL
4614
4615 // #version 450
4616 // layout (set=0,binding=0) buffer S {
4617 // uvec3 arr[2][2]; // first 3 elements are 16 bytes, last is 12
4618 // uint i; // Can't have offset 60 = 3x16 + 12
4619 // } B;
4620 // void main() {}
4621
4622 std::string spirv = R"(
4623 OpCapability Shader
4624 OpMemoryModel Logical GLSL450
4625 OpEntryPoint Vertex %1 "main"
4626 OpDecorate %_arr_v3uint_uint_2 ArrayStride 16
4627 OpDecorate %_arr__arr_v3uint_uint_2_uint_2 ArrayStride 32
4628 OpMemberDecorate %_struct_4 0 Offset 0
4629 OpMemberDecorate %_struct_4 1 Offset 64
4630 OpDecorate %_struct_4 BufferBlock
4631 OpDecorate %5 DescriptorSet 0
4632 OpDecorate %5 Binding 0
4633 %void = OpTypeVoid
4634 %7 = OpTypeFunction %void
4635 %uint = OpTypeInt 32 0
4636 %v3uint = OpTypeVector %uint 3
4637 %uint_2 = OpConstant %uint 2
4638 %_arr_v3uint_uint_2 = OpTypeArray %v3uint %uint_2
4639 %_arr__arr_v3uint_uint_2_uint_2 = OpTypeArray %_arr_v3uint_uint_2 %uint_2
4640 %_struct_4 = OpTypeStruct %_arr__arr_v3uint_uint_2_uint_2 %uint
4641 %_ptr_Uniform__struct_4 = OpTypePointer Uniform %_struct_4
4642 %5 = OpVariable %_ptr_Uniform__struct_4 Uniform
4643 %1 = OpFunction %void None %7
4644 %12 = OpLabel
4645 OpReturn
4646 OpFunctionEnd
4647 )";
4648
4649 CompileSuccessfully(spirv);
4650 EXPECT_EQ(SPV_SUCCESS,
4651 ValidateAndRetrieveValidationState(SPV_ENV_VULKAN_1_0));
4652 }
4653
4654 TEST_F(ValidateDecorations, StorageBufferArraySizeCalculationPackGoodScalar) {
4655 // Original GLSL
4656
4657 // #version 450
4658 // layout (set=0,binding=0) buffer S {
4659 // uvec3 arr[2][2]; // first 3 elements are 16 bytes, last is 12
4660 // uint i; // Can have offset 60 = 3x16 + 12
4661 // } B;
4662 // void main() {}
4663
4664 std::string spirv = R"(
4665 OpCapability Shader
4666 OpMemoryModel Logical GLSL450
4667 OpEntryPoint Vertex %1 "main"
4668 OpDecorate %_arr_v3uint_uint_2 ArrayStride 16
4669 OpDecorate %_arr__arr_v3uint_uint_2_uint_2 ArrayStride 32
4670 OpMemberDecorate %_struct_4 0 Offset 0
4671 OpMemberDecorate %_struct_4 1 Offset 60
4672 OpDecorate %_struct_4 BufferBlock
4673 OpDecorate %5 DescriptorSet 0
4674 OpDecorate %5 Binding 0
4675 %void = OpTypeVoid
4676 %7 = OpTypeFunction %void
4677 %uint = OpTypeInt 32 0
4678 %v3uint = OpTypeVector %uint 3
4679 %uint_2 = OpConstant %uint 2
4680 %_arr_v3uint_uint_2 = OpTypeArray %v3uint %uint_2
4681 %_arr__arr_v3uint_uint_2_uint_2 = OpTypeArray %_arr_v3uint_uint_2 %uint_2
4682 %_struct_4 = OpTypeStruct %_arr__arr_v3uint_uint_2_uint_2 %uint
4683 %_ptr_Uniform__struct_4 = OpTypePointer Uniform %_struct_4
4684 %5 = OpVariable %_ptr_Uniform__struct_4 Uniform
4685 %1 = OpFunction %void None %7
4686 %12 = OpLabel
4687 OpReturn
4688 OpFunctionEnd
4689 )";
4690
4691 options_->scalar_block_layout = true;
4692 CompileSuccessfully(spirv);
4693 EXPECT_EQ(SPV_SUCCESS,
4694 ValidateAndRetrieveValidationState(SPV_ENV_VULKAN_1_0));
4695 }
4696
4697 TEST_F(ValidateDecorations, StorageBufferArraySizeCalculationPackBad) {
4698 // Like previous but, the offset of the second member is too small.
4699
4700 std::string spirv = R"(
4701 OpCapability Shader
4702 OpMemoryModel Logical GLSL450
4703 OpEntryPoint Vertex %1 "main"
4704 OpDecorate %_arr_v3uint_uint_2 ArrayStride 16
4705 OpDecorate %_arr__arr_v3uint_uint_2_uint_2 ArrayStride 32
4706 OpMemberDecorate %_struct_4 0 Offset 0
4707 OpMemberDecorate %_struct_4 1 Offset 60
4708 OpDecorate %_struct_4 BufferBlock
4709 OpDecorate %5 DescriptorSet 0
4710 OpDecorate %5 Binding 0
4711 %void = OpTypeVoid
4712 %7 = OpTypeFunction %void
4713 %uint = OpTypeInt 32 0
4714 %v3uint = OpTypeVector %uint 3
4715 %uint_2 = OpConstant %uint 2
4716 %_arr_v3uint_uint_2 = OpTypeArray %v3uint %uint_2
4717 %_arr__arr_v3uint_uint_2_uint_2 = OpTypeArray %_arr_v3uint_uint_2 %uint_2
4718 %_struct_4 = OpTypeStruct %_arr__arr_v3uint_uint_2_uint_2 %uint
4719 %_ptr_Uniform__struct_4 = OpTypePointer Uniform %_struct_4
4720 %5 = OpVariable %_ptr_Uniform__struct_4 Uniform
4721 %1 = OpFunction %void None %7
4722 %12 = OpLabel
4723 OpReturn
4724 OpFunctionEnd
4725 )";
4726
4727 CompileSuccessfully(spirv);
4728 EXPECT_EQ(SPV_ERROR_INVALID_ID,
4729 ValidateAndRetrieveValidationState(SPV_ENV_VULKAN_1_0));
4730 EXPECT_THAT(getDiagnosticString(),
4731 HasSubstr("Structure id 4 decorated as BufferBlock for variable "
4732 "in Uniform storage class must follow standard storage "
4733 "buffer layout rules: member 1 at offset 60 overlaps "
4734 "previous member ending at offset 63"));
4735 }
4736
4737 TEST_F(ValidateDecorations, UniformBufferArraySizeCalculationPackGood) {
4738 // Like the corresponding buffer block case, but the array padding must
4739 // count for the last element as well, and so the offset of the second
4740 // member must be at least 64.
4741 std::string spirv = R"(
4742 OpCapability Shader
4743 OpMemoryModel Logical GLSL450
4744 OpEntryPoint Vertex %1 "main"
4745 OpDecorate %_arr_v3uint_uint_2 ArrayStride 16
4746 OpDecorate %_arr__arr_v3uint_uint_2_uint_2 ArrayStride 32
4747 OpMemberDecorate %_struct_4 0 Offset 0
4748 OpMemberDecorate %_struct_4 1 Offset 64
4749 OpDecorate %_struct_4 Block
4750 OpDecorate %5 DescriptorSet 0
4751 OpDecorate %5 Binding 0
4752 %void = OpTypeVoid
4753 %7 = OpTypeFunction %void
4754 %uint = OpTypeInt 32 0
4755 %v3uint = OpTypeVector %uint 3
4756 %uint_2 = OpConstant %uint 2
4757 %_arr_v3uint_uint_2 = OpTypeArray %v3uint %uint_2
4758 %_arr__arr_v3uint_uint_2_uint_2 = OpTypeArray %_arr_v3uint_uint_2 %uint_2
4759 %_struct_4 = OpTypeStruct %_arr__arr_v3uint_uint_2_uint_2 %uint
4760 %_ptr_Uniform__struct_4 = OpTypePointer Uniform %_struct_4
4761 %5 = OpVariable %_ptr_Uniform__struct_4 Uniform
4762 %1 = OpFunction %void None %7
4763 %12 = OpLabel
4764 OpReturn
4765 OpFunctionEnd
4766 )";
4767
4768 CompileSuccessfully(spirv);
4769 EXPECT_EQ(SPV_SUCCESS,
4770 ValidateAndRetrieveValidationState(SPV_ENV_VULKAN_1_0));
4771 }
4772
4773 TEST_F(ValidateDecorations, UniformBufferArraySizeCalculationPackBad) {
4774 // Like previous but, the offset of the second member is too small.
4775
4776 std::string spirv = R"(
4777 OpCapability Shader
4778 OpMemoryModel Logical GLSL450
4779 OpEntryPoint Vertex %1 "main"
4780 OpDecorate %_arr_v3uint_uint_2 ArrayStride 16
4781 OpDecorate %_arr__arr_v3uint_uint_2_uint_2 ArrayStride 32
4782 OpMemberDecorate %_struct_4 0 Offset 0
4783 OpMemberDecorate %_struct_4 1 Offset 60
4784 OpDecorate %_struct_4 Block
4785 OpDecorate %5 DescriptorSet 0
4786 OpDecorate %5 Binding 0
4787 %void = OpTypeVoid
4788 %7 = OpTypeFunction %void
4789 %uint = OpTypeInt 32 0
4790 %v3uint = OpTypeVector %uint 3
4791 %uint_2 = OpConstant %uint 2
4792 %_arr_v3uint_uint_2 = OpTypeArray %v3uint %uint_2
4793 %_arr__arr_v3uint_uint_2_uint_2 = OpTypeArray %_arr_v3uint_uint_2 %uint_2
4794 %_struct_4 = OpTypeStruct %_arr__arr_v3uint_uint_2_uint_2 %uint
4795 %_ptr_Uniform__struct_4 = OpTypePointer Uniform %_struct_4
4796 %5 = OpVariable %_ptr_Uniform__struct_4 Uniform
4797 %1 = OpFunction %void None %7
4798 %12 = OpLabel
4799 OpReturn
4800 OpFunctionEnd
4801 )";
4802
4803 CompileSuccessfully(spirv);
4804 EXPECT_EQ(SPV_ERROR_INVALID_ID,
4805 ValidateAndRetrieveValidationState(SPV_ENV_VULKAN_1_0));
4806 EXPECT_THAT(
4807 getDiagnosticString(),
4808 HasSubstr(
4809 "Structure id 4 decorated as Block for variable in Uniform storage "
4810 "class must follow standard uniform buffer layout rules: member 1 at "
4811 "offset 60 overlaps previous member ending at offset 63"));
4812 }
4813
4814 TEST_F(ValidateDecorations, LayoutNotCheckedWhenSkipBlockLayout) {
4815 // Checks that block layout is not verified in skipping block layout mode.
4816 // Even for obviously wrong layout.
4817 std::string spirv = R"(
4818 OpCapability Shader
4819 OpMemoryModel Logical GLSL450
4820 OpEntryPoint Vertex %main "main"
4821 OpSource GLSL 450
4822 OpMemberDecorate %S 0 Offset 3 ; wrong alignment
4823 OpMemberDecorate %S 1 Offset 3 ; same offset as before!
4824 OpDecorate %S Block
4825 OpDecorate %B DescriptorSet 0
4826 OpDecorate %B Binding 0
4827 %void = OpTypeVoid
4828 %3 = OpTypeFunction %void
4829 %float = OpTypeFloat 32
4830 %v3float = OpTypeVector %float 3
4831 %S = OpTypeStruct %float %v3float
4832 %_ptr_Uniform_S = OpTypePointer Uniform %S
4833 %B = OpVariable %_ptr_Uniform_S Uniform
4834 %main = OpFunction %void None %3
4835 %5 = OpLabel
4836 OpReturn
4837 OpFunctionEnd
4838 )";
4839
4840 CompileSuccessfully(spirv);
4841 spvValidatorOptionsSetSkipBlockLayout(getValidatorOptions(), true);
4842 EXPECT_EQ(SPV_SUCCESS,
4843 ValidateAndRetrieveValidationState(SPV_ENV_VULKAN_1_0));
4844 EXPECT_THAT(getDiagnosticString(), Eq(""));
4845 }
4846
4847 TEST_F(ValidateDecorations, EntryPointVariableWrongStorageClass) {
4848 const std::string spirv = R"(
4849 OpCapability Shader
4850 OpMemoryModel Logical GLSL450
4851 OpEntryPoint Fragment %1 "func" %var
4852 OpExecutionMode %1 OriginUpperLeft
4853 %void = OpTypeVoid
4854 %int = OpTypeInt 32 0
4855 %ptr_int_Workgroup = OpTypePointer Workgroup %int
4856 %var = OpVariable %ptr_int_Workgroup Workgroup
4857 %func_ty = OpTypeFunction %void
4858 %1 = OpFunction %void None %func_ty
4859 %2 = OpLabel
4860 OpReturn
4861 OpFunctionEnd
4862 )";
4863
4864 CompileSuccessfully(spirv);
4865 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
4866 EXPECT_THAT(getDiagnosticString(),
4867 HasSubstr("OpEntryPoint interfaces must be OpVariables with "
4868 "Storage Class of Input(1) or Output(3). Found Storage "
4869 "Class 4 for Entry Point id 1."));
4870 }
4871
4872 TEST_F(ValidateDecorations, VulkanMemoryModelNonCoherent) {
4873 const std::string spirv = R"(
4874 OpCapability Shader
4875 OpCapability VulkanMemoryModelKHR
4876 OpCapability Linkage
4877 OpExtension "SPV_KHR_vulkan_memory_model"
4878 OpExtension "SPV_KHR_storage_buffer_storage_class"
4879 OpMemoryModel Logical VulkanKHR
4880 OpDecorate %1 Coherent
4881 %2 = OpTypeInt 32 0
4882 %3 = OpTypePointer StorageBuffer %2
4883 %1 = OpVariable %3 StorageBuffer
4884 )";
4885
4886 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
4887 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
4888 EXPECT_THAT(getDiagnosticString(),
4889 HasSubstr("Coherent decoration targeting '1[%1]' is "
4890 "banned when using the Vulkan memory model."));
4891 }
4892
4893 TEST_F(ValidateDecorations, VulkanMemoryModelNoCoherentMember) {
4894 const std::string spirv = R"(
4895 OpCapability Shader
4896 OpCapability VulkanMemoryModelKHR
4897 OpCapability Linkage
4898 OpExtension "SPV_KHR_vulkan_memory_model"
4899 OpMemoryModel Logical VulkanKHR
4900 OpMemberDecorate %1 0 Coherent
4901 %2 = OpTypeInt 32 0
4902 %1 = OpTypeStruct %2 %2
4903 )";
4904
4905 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
4906 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
4907 EXPECT_THAT(
4908 getDiagnosticString(),
4909 HasSubstr(
4910 "Coherent decoration targeting '1[%_struct_1]' (member index 0) "
4911 "is banned when using the Vulkan memory model."));
4912 }
4913
4914 TEST_F(ValidateDecorations, VulkanMemoryModelNoVolatile) {
4915 const std::string spirv = R"(
4916 OpCapability Shader
4917 OpCapability VulkanMemoryModelKHR
4918 OpCapability Linkage
4919 OpExtension "SPV_KHR_vulkan_memory_model"
4920 OpExtension "SPV_KHR_storage_buffer_storage_class"
4921 OpMemoryModel Logical VulkanKHR
4922 OpDecorate %1 Volatile
4923 %2 = OpTypeInt 32 0
4924 %3 = OpTypePointer StorageBuffer %2
4925 %1 = OpVariable %3 StorageBuffer
4926 )";
4927
4928 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
4929 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
4930 EXPECT_THAT(getDiagnosticString(),
4931 HasSubstr("Volatile decoration targeting '1[%1]' is banned when "
4932 "using the Vulkan memory model."));
4933 }
4934
4935 TEST_F(ValidateDecorations, VulkanMemoryModelNoVolatileMember) {
4936 const std::string spirv = R"(
4937 OpCapability Shader
4938 OpCapability VulkanMemoryModelKHR
4939 OpCapability Linkage
4940 OpExtension "SPV_KHR_vulkan_memory_model"
4941 OpMemoryModel Logical VulkanKHR
4942 OpMemberDecorate %1 1 Volatile
4943 %2 = OpTypeInt 32 0
4944 %1 = OpTypeStruct %2 %2
4945 )";
4946
4947 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
4948 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
4949 EXPECT_THAT(getDiagnosticString(),
4950 HasSubstr("Volatile decoration targeting '1[%_struct_1]' (member "
4951 "index 1) is banned when using the Vulkan memory "
4952 "model."));
4953 }
4954
4955 TEST_F(ValidateDecorations, FPRoundingModeGood) {
4956 std::string spirv = R"(
4957 OpCapability Shader
4958 OpCapability Linkage
4959 OpCapability StorageBuffer16BitAccess
4960 OpExtension "SPV_KHR_storage_buffer_storage_class"
4961 OpExtension "SPV_KHR_variable_pointers"
4962 OpExtension "SPV_KHR_16bit_storage"
4963 OpMemoryModel Logical GLSL450
4964 OpEntryPoint GLCompute %main "main"
4965 OpDecorate %_ FPRoundingMode RTE
4966 %half = OpTypeFloat 16
4967 %float = OpTypeFloat 32
4968 %float_1_25 = OpConstant %float 1.25
4969 %half_ptr = OpTypePointer StorageBuffer %half
4970 %half_ptr_var = OpVariable %half_ptr StorageBuffer
4971 %void = OpTypeVoid
4972 %func = OpTypeFunction %void
4973 %main = OpFunction %void None %func
4974 %main_entry = OpLabel
4975 %_ = OpFConvert %half %float_1_25
4976 OpStore %half_ptr_var %_
4977 OpReturn
4978 OpFunctionEnd
4979 )";
4980
4981 CompileSuccessfully(spirv);
4982 EXPECT_EQ(SPV_SUCCESS, ValidateAndRetrieveValidationState());
4983 }
4984
4985 TEST_F(ValidateDecorations, FPRoundingModeVectorGood) {
4986 std::string spirv = R"(
4987 OpCapability Shader
4988 OpCapability Linkage
4989 OpCapability StorageBuffer16BitAccess
4990 OpExtension "SPV_KHR_storage_buffer_storage_class"
4991 OpExtension "SPV_KHR_variable_pointers"
4992 OpExtension "SPV_KHR_16bit_storage"
4993 OpMemoryModel Logical GLSL450
4994 OpEntryPoint GLCompute %main "main"
4995 OpDecorate %_ FPRoundingMode RTE
4996 %half = OpTypeFloat 16
4997 %float = OpTypeFloat 32
4998 %v2half = OpTypeVector %half 2
4999 %v2float = OpTypeVector %float 2
5000 %float_1_25 = OpConstant %float 1.25
5001 %floats = OpConstantComposite %v2float %float_1_25 %float_1_25
5002 %halfs_ptr = OpTypePointer StorageBuffer %v2half
5003 %halfs_ptr_var = OpVariable %halfs_ptr StorageBuffer
5004 %void = OpTypeVoid
5005 %func = OpTypeFunction %void
5006 %main = OpFunction %void None %func
5007 %main_entry = OpLabel
5008 %_ = OpFConvert %v2half %floats
5009 OpStore %halfs_ptr_var %_
5010 OpReturn
5011 OpFunctionEnd
5012 )";
5013
5014 CompileSuccessfully(spirv);
5015 EXPECT_EQ(SPV_SUCCESS, ValidateAndRetrieveValidationState());
5016 }
5017
5018 TEST_F(ValidateDecorations, FPRoundingModeNotOpFConvert) {
5019 std::string spirv = R"(
5020 OpCapability Shader
5021 OpCapability Linkage
5022 OpCapability StorageBuffer16BitAccess
5023 OpExtension "SPV_KHR_storage_buffer_storage_class"
5024 OpExtension "SPV_KHR_variable_pointers"
5025 OpExtension "SPV_KHR_16bit_storage"
5026 OpMemoryModel Logical GLSL450
5027 OpEntryPoint GLCompute %main "main"
5028 OpDecorate %_ FPRoundingMode RTE
5029 %short = OpTypeInt 16 1
5030 %int = OpTypeInt 32 1
5031 %int_17 = OpConstant %int 17
5032 %short_ptr = OpTypePointer StorageBuffer %short
5033 %short_ptr_var = OpVariable %short_ptr StorageBuffer
5034 %void = OpTypeVoid
5035 %func = OpTypeFunction %void
5036 %main = OpFunction %void None %func
5037 %main_entry = OpLabel
5038 %_ = OpSConvert %short %int_17
5039 OpStore %short_ptr_var %_
5040 OpReturn
5041 OpFunctionEnd
5042 )";
5043
5044 CompileSuccessfully(spirv);
5045 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateAndRetrieveValidationState());
5046 EXPECT_THAT(getDiagnosticString(),
5047 HasSubstr("FPRoundingMode decoration can be applied only to a "
5048 "width-only conversion instruction for floating-point "
5049 "object."));
5050 }
5051
5052 TEST_F(ValidateDecorations, FPRoundingModeNoOpStoreGood) {
5053 std::string spirv = R"(
5054 OpCapability Shader
5055 OpCapability Linkage
5056 OpCapability StorageBuffer16BitAccess
5057 OpExtension "SPV_KHR_storage_buffer_storage_class"
5058 OpExtension "SPV_KHR_variable_pointers"
5059 OpExtension "SPV_KHR_16bit_storage"
5060 OpMemoryModel Logical GLSL450
5061 OpEntryPoint GLCompute %main "main"
5062 OpDecorate %_ FPRoundingMode RTE
5063 %half = OpTypeFloat 16
5064 %float = OpTypeFloat 32
5065 %float_1_25 = OpConstant %float 1.25
5066 %half_ptr = OpTypePointer StorageBuffer %half
5067 %half_ptr_var = OpVariable %half_ptr StorageBuffer
5068 %void = OpTypeVoid
5069 %func = OpTypeFunction %void
5070 %main = OpFunction %void None %func
5071 %main_entry = OpLabel
5072 %_ = OpFConvert %half %float_1_25
5073 OpReturn
5074 OpFunctionEnd
5075 )";
5076
5077 CompileSuccessfully(spirv);
5078 EXPECT_EQ(SPV_SUCCESS, ValidateAndRetrieveValidationState());
5079 }
5080
5081 TEST_F(ValidateDecorations, FPRoundingModeFConvert64to16Good) {
5082 std::string spirv = R"(
5083 OpCapability Shader
5084 OpCapability Linkage
5085 OpCapability StorageBuffer16BitAccess
5086 OpCapability Float64
5087 OpExtension "SPV_KHR_storage_buffer_storage_class"
5088 OpExtension "SPV_KHR_variable_pointers"
5089 OpExtension "SPV_KHR_16bit_storage"
5090 OpMemoryModel Logical GLSL450
5091 OpEntryPoint GLCompute %main "main"
5092 OpDecorate %_ FPRoundingMode RTE
5093 %half = OpTypeFloat 16
5094 %double = OpTypeFloat 64
5095 %double_1_25 = OpConstant %double 1.25
5096 %half_ptr = OpTypePointer StorageBuffer %half
5097 %half_ptr_var = OpVariable %half_ptr StorageBuffer
5098 %void = OpTypeVoid
5099 %func = OpTypeFunction %void
5100 %main = OpFunction %void None %func
5101 %main_entry = OpLabel
5102 %_ = OpFConvert %half %double_1_25
5103 OpStore %half_ptr_var %_
5104 OpReturn
5105 OpFunctionEnd
5106 )";
5107
5108 CompileSuccessfully(spirv);
5109 EXPECT_EQ(SPV_SUCCESS, ValidateAndRetrieveValidationState());
5110 }
5111
5112 TEST_F(ValidateDecorations, FPRoundingModeNotStoreInFloat16) {
5113 std::string spirv = R"(
5114 OpCapability Shader
5115 OpCapability Linkage
5116 OpCapability StorageBuffer16BitAccess
5117 OpCapability Float64
5118 OpExtension "SPV_KHR_storage_buffer_storage_class"
5119 OpExtension "SPV_KHR_variable_pointers"
5120 OpExtension "SPV_KHR_16bit_storage"
5121 OpMemoryModel Logical GLSL450
5122 OpEntryPoint GLCompute %main "main"
5123 OpDecorate %_ FPRoundingMode RTE
5124 %float = OpTypeFloat 32
5125 %double = OpTypeFloat 64
5126 %double_1_25 = OpConstant %double 1.25
5127 %float_ptr = OpTypePointer StorageBuffer %float
5128 %float_ptr_var = OpVariable %float_ptr StorageBuffer
5129 %void = OpTypeVoid
5130 %func = OpTypeFunction %void
5131 %main = OpFunction %void None %func
5132 %main_entry = OpLabel
5133 %_ = OpFConvert %float %double_1_25
5134 OpStore %float_ptr_var %_
5135 OpReturn
5136 OpFunctionEnd
5137 )";
5138
5139 CompileSuccessfully(spirv);
5140 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateAndRetrieveValidationState());
5141 EXPECT_THAT(
5142 getDiagnosticString(),
5143 HasSubstr("FPRoundingMode decoration can be applied only to the "
5144 "Object operand of an OpStore storing through a "
5145 "pointer to a 16-bit floating-point scalar or vector object."));
5146 }
5147
5148 TEST_F(ValidateDecorations, FPRoundingModeMultipleOpStoreGood) {
5149 std::string spirv = R"(
5150 OpCapability Shader
5151 OpCapability Linkage
5152 OpCapability StorageBuffer16BitAccess
5153 OpExtension "SPV_KHR_storage_buffer_storage_class"
5154 OpExtension "SPV_KHR_variable_pointers"
5155 OpExtension "SPV_KHR_16bit_storage"
5156 OpMemoryModel Logical GLSL450
5157 OpEntryPoint GLCompute %main "main"
5158 OpDecorate %_ FPRoundingMode RTE
5159 %half = OpTypeFloat 16
5160 %float = OpTypeFloat 32
5161 %float_1_25 = OpConstant %float 1.25
5162 %half_ptr = OpTypePointer StorageBuffer %half
5163 %half_ptr_var_0 = OpVariable %half_ptr StorageBuffer
5164 %half_ptr_var_1 = OpVariable %half_ptr StorageBuffer
5165 %half_ptr_var_2 = OpVariable %half_ptr StorageBuffer
5166 %void = OpTypeVoid
5167 %func = OpTypeFunction %void
5168 %main = OpFunction %void None %func
5169 %main_entry = OpLabel
5170 %_ = OpFConvert %half %float_1_25
5171 OpStore %half_ptr_var_0 %_
5172 OpStore %half_ptr_var_1 %_
5173 OpStore %half_ptr_var_2 %_
5174 OpReturn
5175 OpFunctionEnd
5176 )";
5177
5178 CompileSuccessfully(spirv);
5179 EXPECT_EQ(SPV_SUCCESS, ValidateAndRetrieveValidationState());
5180 }
5181
5182 TEST_F(ValidateDecorations, FPRoundingModeMultipleUsesBad) {
5183 std::string spirv = R"(
5184 OpCapability Shader
5185 OpCapability Linkage
5186 OpCapability StorageBuffer16BitAccess
5187 OpExtension "SPV_KHR_storage_buffer_storage_class"
5188 OpExtension "SPV_KHR_variable_pointers"
5189 OpExtension "SPV_KHR_16bit_storage"
5190 OpMemoryModel Logical GLSL450
5191 OpEntryPoint GLCompute %main "main"
5192 OpDecorate %_ FPRoundingMode RTE
5193 %half = OpTypeFloat 16
5194 %float = OpTypeFloat 32
5195 %float_1_25 = OpConstant %float 1.25
5196 %half_ptr = OpTypePointer StorageBuffer %half
5197 %half_ptr_var_0 = OpVariable %half_ptr StorageBuffer
5198 %half_ptr_var_1 = OpVariable %half_ptr StorageBuffer
5199 %void = OpTypeVoid
5200 %func = OpTypeFunction %void
5201 %main = OpFunction %void None %func
5202 %main_entry = OpLabel
5203 %_ = OpFConvert %half %float_1_25
5204 OpStore %half_ptr_var_0 %_
5205 %result = OpFAdd %half %_ %_
5206 OpStore %half_ptr_var_1 %_
5207 OpReturn
5208 OpFunctionEnd
5209 )";
5210
5211 CompileSuccessfully(spirv);
5212 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateAndRetrieveValidationState());
5213 EXPECT_THAT(getDiagnosticString(),
5214 HasSubstr("FPRoundingMode decoration can be applied only to the "
5215 "Object operand of an OpStore."));
5216 }
5217
5218 TEST_F(ValidateDecorations, VulkanFPRoundingModeGood) {
5219 std::string spirv = R"(
5220 OpCapability Shader
5221 OpCapability StorageBuffer16BitAccess
5222 %1 = OpExtInstImport "GLSL.std.450"
5223 OpMemoryModel Logical GLSL450
5224 OpEntryPoint GLCompute %main "main" %_
5225 OpExecutionMode %main LocalSize 1 1 1
5226 OpMemberDecorate %ssbo 0 Offset 0
5227 OpDecorate %ssbo Block
5228 OpDecorate %_ DescriptorSet 0
5229 OpDecorate %_ Binding 0
5230 OpDecorate %17 FPRoundingMode RTE
5231 %void = OpTypeVoid
5232 %3 = OpTypeFunction %void
5233 %float = OpTypeFloat 32
5234 %_ptr_Function_float = OpTypePointer Function %float
5235 %float_1 = OpConstant %float 1
5236 %half = OpTypeFloat 16
5237 %ssbo = OpTypeStruct %half
5238 %_ptr_StorageBuffer_ssbo = OpTypePointer StorageBuffer %ssbo
5239 %_ = OpVariable %_ptr_StorageBuffer_ssbo StorageBuffer
5240 %int = OpTypeInt 32 1
5241 %int_0 = OpConstant %int 0
5242 %_ptr_StorageBuffer_half = OpTypePointer StorageBuffer %half
5243 %main = OpFunction %void None %3
5244 %5 = OpLabel
5245 %b = OpVariable %_ptr_Function_float Function
5246 OpStore %b %float_1
5247 %16 = OpLoad %float %b
5248 %17 = OpFConvert %half %16
5249 %19 = OpAccessChain %_ptr_StorageBuffer_half %_ %int_0
5250 OpStore %19 %17
5251 OpReturn
5252 OpFunctionEnd
5253 )";
5254
5255 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_2);
5256 EXPECT_EQ(SPV_SUCCESS,
5257 ValidateAndRetrieveValidationState(SPV_ENV_VULKAN_1_2));
5258 }
5259
5260 TEST_F(ValidateDecorations, VulkanFPRoundingModeBadMode) {
5261 std::string spirv = R"(
5262 OpCapability Shader
5263 OpCapability StorageBuffer16BitAccess
5264 %1 = OpExtInstImport "GLSL.std.450"
5265 OpMemoryModel Logical GLSL450
5266 OpEntryPoint GLCompute %main "main" %_
5267 OpExecutionMode %main LocalSize 1 1 1
5268 OpMemberDecorate %ssbo 0 Offset 0
5269 OpDecorate %ssbo Block
5270 OpDecorate %_ DescriptorSet 0
5271 OpDecorate %_ Binding 0
5272 OpDecorate %17 FPRoundingMode RTP
5273 %void = OpTypeVoid
5274 %3 = OpTypeFunction %void
5275 %float = OpTypeFloat 32
5276 %_ptr_Function_float = OpTypePointer Function %float
5277 %float_1 = OpConstant %float 1
5278 %half = OpTypeFloat 16
5279 %ssbo = OpTypeStruct %half
5280 %_ptr_StorageBuffer_ssbo = OpTypePointer StorageBuffer %ssbo
5281 %_ = OpVariable %_ptr_StorageBuffer_ssbo StorageBuffer
5282 %int = OpTypeInt 32 1
5283 %int_0 = OpConstant %int 0
5284 %_ptr_StorageBuffer_half = OpTypePointer StorageBuffer %half
5285 %main = OpFunction %void None %3
5286 %5 = OpLabel
5287 %b = OpVariable %_ptr_Function_float Function
5288 OpStore %b %float_1
5289 %16 = OpLoad %float %b
5290 %17 = OpFConvert %half %16
5291 %19 = OpAccessChain %_ptr_StorageBuffer_half %_ %int_0
5292 OpStore %19 %17
5293 OpReturn
5294 OpFunctionEnd
5295 )";
5296
5297 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_2);
5298 EXPECT_EQ(SPV_ERROR_INVALID_ID,
5299 ValidateAndRetrieveValidationState(SPV_ENV_VULKAN_1_2));
5300 EXPECT_THAT(getDiagnosticString(),
5301 AnyVUID("VUID-StandaloneSpirv-FPRoundingMode-04675"));
5302 EXPECT_THAT(
5303 getDiagnosticString(),
5304 HasSubstr("In Vulkan, the FPRoundingMode mode must only by RTE or RTZ."));
5305 }
5306
5307 TEST_F(ValidateDecorations, GroupDecorateTargetsDecorationGroup) {
5308 std::string spirv = R"(
5309 OpCapability Shader
5310 OpCapability Linkage
5311 OpMemoryModel Logical GLSL450
5312 %1 = OpDecorationGroup
5313 OpGroupDecorate %1 %1
5314 )";
5315
5316 CompileSuccessfully(spirv);
5317 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
5318 EXPECT_THAT(getDiagnosticString(),
5319 HasSubstr("OpGroupDecorate may not target OpDecorationGroup <id> "
5320 "'1[%1]'"));
5321 }
5322
5323 TEST_F(ValidateDecorations, GroupDecorateTargetsDecorationGroup2) {
5324 std::string spirv = R"(
5325 OpCapability Shader
5326 OpCapability Linkage
5327 OpMemoryModel Logical GLSL450
5328 %1 = OpDecorationGroup
5329 OpGroupDecorate %1 %2 %1
5330 %2 = OpTypeVoid
5331 )";
5332
5333 CompileSuccessfully(spirv);
5334 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
5335 EXPECT_THAT(getDiagnosticString(),
5336 HasSubstr("OpGroupDecorate may not target OpDecorationGroup <id> "
5337 "'1[%1]'"));
5338 }
5339
5340 TEST_F(ValidateDecorations, RecurseThroughRuntimeArray) {
5341 const std::string spirv = R"(
5342 OpCapability Shader
5343 OpExtension "SPV_KHR_storage_buffer_storage_class"
5344 OpMemoryModel Logical GLSL450
5345 OpEntryPoint GLCompute %main "main"
5346 OpExecutionMode %main LocalSize 1 1 1
5347 OpDecorate %outer Block
5348 OpMemberDecorate %inner 0 Offset 0
5349 OpMemberDecorate %inner 1 Offset 1
5350 OpDecorate %runtime ArrayStride 16
5351 OpMemberDecorate %outer 0 Offset 0
5352 %int = OpTypeInt 32 0
5353 %inner = OpTypeStruct %int %int
5354 %runtime = OpTypeRuntimeArray %inner
5355 %outer = OpTypeStruct %runtime
5356 %outer_ptr = OpTypePointer StorageBuffer %outer
5357 %var = OpVariable %outer_ptr StorageBuffer
5358 %void = OpTypeVoid
5359 %void_fn = OpTypeFunction %void
5360 %main = OpFunction %void None %void_fn
5361 %entry = OpLabel
5362 OpReturn
5363 OpFunctionEnd
5364 )";
5365
5366 CompileSuccessfully(spirv);
5367 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_0));
5368 EXPECT_THAT(
5369 getDiagnosticString(),
5370 HasSubstr(
5371 "Structure id 3 decorated as Block for variable in StorageBuffer "
5372 "storage class must follow standard storage buffer layout "
5373 "rules: member 1 at offset 1 is not aligned to 4"));
5374 }
5375
5376 TEST_F(ValidateDecorations, VulkanStructWithoutDecorationWithRuntimeArray) {
5377 std::string str = R"(
5378 OpCapability Shader
5379 OpMemoryModel Logical GLSL450
5380 OpEntryPoint Fragment %func "func"
5381 OpExecutionMode %func OriginUpperLeft
5382 OpDecorate %array_t ArrayStride 4
5383 OpMemberDecorate %struct_t 0 Offset 0
5384 OpMemberDecorate %struct_t 1 Offset 4
5385 %uint_t = OpTypeInt 32 0
5386 %array_t = OpTypeRuntimeArray %uint_t
5387 %struct_t = OpTypeStruct %uint_t %array_t
5388 %struct_ptr = OpTypePointer StorageBuffer %struct_t
5389 %2 = OpVariable %struct_ptr StorageBuffer
5390 %void = OpTypeVoid
5391 %func_t = OpTypeFunction %void
5392 %func = OpFunction %void None %func_t
5393 %1 = OpLabel
5394 OpReturn
5395 OpFunctionEnd
5396 )";
5397
5398 CompileSuccessfully(str.c_str(), SPV_ENV_VULKAN_1_1);
5399 ASSERT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_1));
5400 EXPECT_THAT(getDiagnosticString(),
5401 AnyVUID("VUID-StandaloneSpirv-OpTypeRuntimeArray-04680"));
5402 EXPECT_THAT(getDiagnosticString(),
5403 HasSubstr("Vulkan, OpTypeStruct containing an OpTypeRuntimeArray "
5404 "must be decorated with Block or BufferBlock."));
5405 }
5406
5407 TEST_F(ValidateDecorations, EmptyStructAtNonZeroOffsetGood) {
5408 const std::string spirv = R"(
5409 OpCapability Shader
5410 OpMemoryModel Logical GLSL450
5411 OpEntryPoint GLCompute %main "main"
5412 OpExecutionMode %main LocalSize 1 1 1
5413 OpDecorate %struct Block
5414 OpMemberDecorate %struct 0 Offset 0
5415 OpMemberDecorate %struct 1 Offset 16
5416 OpDecorate %var DescriptorSet 0
5417 OpDecorate %var Binding 0
5418 %void = OpTypeVoid
5419 %float = OpTypeFloat 32
5420 %empty = OpTypeStruct
5421 %struct = OpTypeStruct %float %empty
5422 %ptr_struct_ubo = OpTypePointer Uniform %struct
5423 %var = OpVariable %ptr_struct_ubo Uniform
5424 %voidfn = OpTypeFunction %void
5425 %main = OpFunction %void None %voidfn
5426 %entry = OpLabel
5427 OpReturn
5428 OpFunctionEnd
5429 )";
5430
5431 CompileSuccessfully(spirv);
5432 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
5433 }
5434
5435 // OpDecorateId
5436
5437 TEST_F(ValidateDecorations, DecorateIdGood) {
5438 const std::string spirv = R"(
5439 OpCapability Shader
5440 OpMemoryModel Logical Simple
5441 OpEntryPoint GLCompute %main "main"
5442 OpExecutionMode %main LocalSize 1 1 1
5443 OpName %subgroupscope "subgroupscope"
5444 OpName %int0 "int0"
5445 OpName %fn "fn"
5446 OpDecorateId %int0 UniformId %subgroupscope
5447 %void = OpTypeVoid
5448 %float = OpTypeFloat 32
5449 %int = OpTypeInt 32 1
5450 %subgroupscope = OpConstant %int 3
5451 %int0 = OpConstantNull %int
5452 %fn = OpTypeFunction %void
5453 %main = OpFunction %void None %fn
5454 %entry = OpLabel
5455 OpReturn
5456 OpFunctionEnd
5457 )";
5458
5459 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_4);
5460 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_UNIVERSAL_1_4));
5461 EXPECT_THAT(getDiagnosticString(), Eq(""));
5462 }
5463
5464 TEST_F(ValidateDecorations, DecorateIdGroupBad) {
5465 const std::string spirv = R"(
5466 OpCapability Shader
5467 OpMemoryModel Logical Simple
5468 OpEntryPoint GLCompute %main "main"
5469 OpExecutionMode %main LocalSize 1 1 1
5470 OpName %subgroupscope "subgroupscope"
5471 OpName %int0 "int0"
5472 OpName %fn "fn"
5473 OpName %group "group"
5474 OpDecorateId %group UniformId %subgroupscope
5475 %group = OpDecorationGroup
5476 OpGroupDecorate %group %int0
5477 %void = OpTypeVoid
5478 %float = OpTypeFloat 32
5479 %int = OpTypeInt 32 1
5480 %subgroupscope = OpConstant %int 3
5481 %int0 = OpConstantNull %int
5482 %fn = OpTypeFunction %void
5483 %main = OpFunction %void None %fn
5484 %entry = OpLabel
5485 OpReturn
5486 OpFunctionEnd
5487 )";
5488
5489 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_4);
5490 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_UNIVERSAL_1_4));
5491 EXPECT_THAT(getDiagnosticString(),
5492 HasSubstr("must not be an OpDecorationGroup instruction.\n"
5493 " OpDecorateId %group UniformId %subgroupscope"));
5494 }
5495
5496 TEST_F(ValidateDecorations, DecorateIdOutOfOrderBad) {
5497 const std::string spirv = R"(
5498 OpCapability Shader
5499 OpMemoryModel Logical Simple
5500 OpEntryPoint GLCompute %main "main"
5501 OpExecutionMode %main LocalSize 1 1 1
5502 OpName %subgroupscope "subgroupscope"
5503 OpName %int0 "int0"
5504 OpName %fn "fn"
5505 OpDecorateId %int0 UniformId %subgroupscope
5506 %void = OpTypeVoid
5507 %float = OpTypeFloat 32
5508 %int = OpTypeInt 32 1
5509 %int0 = OpConstantNull %int
5510 %subgroupscope = OpConstant %int 3
5511 %fn = OpTypeFunction %void
5512 %main = OpFunction %void None %fn
5513 %entry = OpLabel
5514 OpReturn
5515 OpFunctionEnd
5516 )";
5517
5518 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_4);
5519 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_UNIVERSAL_1_4));
5520 EXPECT_THAT(getDiagnosticString(),
5521 HasSubstr("[%subgroupscope]' must appear earlier in the"
5522 " binary than the target\n"
5523 " OpDecorateId %int0 UniformId %subgroupscope"));
5524 }
5525
5526 // Uniform and UniformId decorations
5527
5528 TEST_F(ValidateDecorations, UniformDecorationGood) {
5529 const std::string spirv = R"(
5530 OpCapability Shader
5531 OpMemoryModel Logical Simple
5532 OpEntryPoint GLCompute %main "main"
5533 OpExecutionMode %main LocalSize 1 1 1
5534 OpDecorate %int0 Uniform
5535 OpDecorate %var Uniform
5536 OpDecorate %val Uniform
5537 %void = OpTypeVoid
5538 %int = OpTypeInt 32 1
5539 %int0 = OpConstantNull %int
5540 %intptr = OpTypePointer Private %int
5541 %var = OpVariable %intptr Private
5542 %fn = OpTypeFunction %void
5543 %main = OpFunction %void None %fn
5544 %entry = OpLabel
5545 %val = OpLoad %int %var
5546 OpReturn
5547 OpFunctionEnd
5548 )";
5549
5550 CompileSuccessfully(spirv);
5551 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
5552 EXPECT_THAT(getDiagnosticString(), Eq(""));
5553 }
5554
5555 // Returns SPIR-V assembly for a shader that uses a given decoration
5556 // instruction.
5557 std::string ShaderWithUniformLikeDecoration(const std::string& inst) {
5558 return std::string(R"(
5559 OpCapability Shader
5560 OpMemoryModel Logical Simple
5561 OpEntryPoint GLCompute %main "main"
5562 OpExecutionMode %main LocalSize 1 1 1
5563 OpName %subgroupscope "subgroupscope"
5564 OpName %call "call"
5565 OpName %myfunc "myfunc"
5566 OpName %int0 "int0"
5567 OpName %int1 "int1"
5568 OpName %float0 "float0"
5569 OpName %fn "fn"
5570 )") + inst +
5571 R"(
5572 %void = OpTypeVoid
5573 %float = OpTypeFloat 32
5574 %int = OpTypeInt 32 1
5575 %int1 = OpConstant %int 1
5576 %int_99 = OpConstant %int 99
5577 %subgroupscope = OpConstant %int 3
5578 %float0 = OpConstantNull %float
5579 %int0 = OpConstantNull %int
5580 %fn = OpTypeFunction %void
5581 %myfunc = OpFunction %void None %fn
5582 %myfuncentry = OpLabel
5583 OpReturn
5584 OpFunctionEnd
5585 %main = OpFunction %void None %fn
5586 %entry = OpLabel
5587 %call = OpFunctionCall %void %myfunc
5588 OpReturn
5589 OpFunctionEnd
5590 )";
5591 }
5592
5593 TEST_F(ValidateDecorations, UniformIdDecorationWithScopeIdV13Bad) {
5594 const std::string spirv = ShaderWithUniformLikeDecoration(
5595 "OpDecorateId %int0 UniformId %subgroupscope");
5596 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
5597 EXPECT_EQ(SPV_ERROR_WRONG_VERSION,
5598 ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
5599 EXPECT_THAT(getDiagnosticString(),
5600 HasSubstr("requires SPIR-V version 1.4 or later\n"
5601 " OpDecorateId %int0 UniformId %subgroupscope"))
5602 << spirv;
5603 }
5604
5605 TEST_F(ValidateDecorations, UniformIdDecorationWithScopeIdV13BadTargetV14) {
5606 const std::string spirv = ShaderWithUniformLikeDecoration(
5607 "OpDecorateId %int0 UniformId %subgroupscope");
5608 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
5609 EXPECT_EQ(SPV_ERROR_WRONG_VERSION,
5610 ValidateInstructions(SPV_ENV_UNIVERSAL_1_4));
5611 EXPECT_THAT(getDiagnosticString(),
5612 HasSubstr("requires SPIR-V version 1.4 or later"));
5613 }
5614
5615 TEST_F(ValidateDecorations, UniformIdDecorationWithScopeIdV14Good) {
5616 const std::string spirv = ShaderWithUniformLikeDecoration(
5617 "OpDecorateId %int0 UniformId %subgroupscope");
5618 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_4);
5619 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_UNIVERSAL_1_4));
5620 EXPECT_THAT(getDiagnosticString(), Eq(""));
5621 }
5622
5623 TEST_F(ValidateDecorations, UniformDecorationTargetsTypeBad) {
5624 const std::string spirv =
5625 ShaderWithUniformLikeDecoration("OpDecorate %fn Uniform");
5626
5627 CompileSuccessfully(spirv);
5628 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
5629 EXPECT_THAT(getDiagnosticString(),
5630 HasSubstr("Uniform decoration applied to a non-object"));
5631 EXPECT_THAT(getDiagnosticString(), HasSubstr("%fn = OpTypeFunction %void"));
5632 }
5633
5634 TEST_F(ValidateDecorations, UniformIdDecorationTargetsTypeBad) {
5635 const std::string spirv = ShaderWithUniformLikeDecoration(
5636 "OpDecorateId %fn UniformId %subgroupscope");
5637
5638 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_4);
5639 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_UNIVERSAL_1_4));
5640 EXPECT_THAT(getDiagnosticString(),
5641 HasSubstr("UniformId decoration applied to a non-object"));
5642 EXPECT_THAT(getDiagnosticString(), HasSubstr("%fn = OpTypeFunction %void"));
5643 }
5644
5645 TEST_F(ValidateDecorations, UniformDecorationTargetsVoidValueBad) {
5646 const std::string spirv =
5647 ShaderWithUniformLikeDecoration("OpDecorate %call Uniform");
5648
5649 CompileSuccessfully(spirv);
5650 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
5651 EXPECT_THAT(getDiagnosticString(),
5652 HasSubstr("Uniform decoration applied to a value with void type\n"
5653 " %call = OpFunctionCall %void %myfunc"));
5654 }
5655
5656 TEST_F(ValidateDecorations, UniformIdDecorationTargetsVoidValueBad) {
5657 const std::string spirv = ShaderWithUniformLikeDecoration(
5658 "OpDecorateId %call UniformId %subgroupscope");
5659
5660 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_4);
5661 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_UNIVERSAL_1_4))
5662 << spirv;
5663 EXPECT_THAT(
5664 getDiagnosticString(),
5665 HasSubstr("UniformId decoration applied to a value with void type\n"
5666 " %call = OpFunctionCall %void %myfunc"));
5667 }
5668
5669 TEST_F(ValidateDecorations,
5670 UniformDecorationWithScopeIdV14IdIsFloatValueIsBad) {
5671 const std::string spirv =
5672 ShaderWithUniformLikeDecoration("OpDecorateId %int0 UniformId %float0");
5673
5674 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_4);
5675 EXPECT_EQ(SPV_ERROR_INVALID_DATA,
5676 ValidateInstructions(SPV_ENV_UNIVERSAL_1_4));
5677 EXPECT_THAT(getDiagnosticString(),
5678 HasSubstr("ConstantNull: expected scope to be a 32-bit int"));
5679 }
5680
5681 TEST_F(ValidateDecorations,
5682 UniformDecorationWithScopeIdV14IdIsInvalidIntValueBad) {
5683 const std::string spirv =
5684 ShaderWithUniformLikeDecoration("OpDecorateId %int0 UniformId %int_99");
5685
5686 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_4);
5687 EXPECT_EQ(SPV_ERROR_INVALID_DATA,
5688 ValidateInstructions(SPV_ENV_UNIVERSAL_1_4));
5689 EXPECT_THAT(
5690 getDiagnosticString(),
5691 HasSubstr("Invalid scope value:\n %int_99 = OpConstant %int 99\n"));
5692 }
5693
5694 TEST_F(ValidateDecorations, UniformDecorationWithScopeIdV14VulkanEnv) {
5695 const std::string spirv =
5696 ShaderWithUniformLikeDecoration("OpDecorateId %int0 UniformId %int1");
5697
5698 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_1_SPIRV_1_4);
5699 EXPECT_EQ(SPV_ERROR_INVALID_DATA,
5700 ValidateInstructions(SPV_ENV_VULKAN_1_1_SPIRV_1_4));
5701 EXPECT_THAT(getDiagnosticString(),
5702 AnyVUID("VUID-StandaloneSpirv-None-04636"));
5703 EXPECT_THAT(getDiagnosticString(),
5704 HasSubstr(": in Vulkan environment Execution Scope is limited to "
5705 "Workgroup and Subgroup"));
5706 }
5707
5708 TEST_F(ValidateDecorations, UniformDecorationWithWrongInstructionBad) {
5709 const std::string spirv =
5710 ShaderWithUniformLikeDecoration("OpDecorateId %int0 Uniform");
5711
5712 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_2);
5713 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_UNIVERSAL_1_2));
5714 EXPECT_THAT(getDiagnosticString(),
5715 HasSubstr("Decorations that don't take ID parameters may not be "
5716 "used with OpDecorateId\n"
5717 " OpDecorateId %int0 Uniform"));
5718 }
5719
5720 TEST_F(ValidateDecorations, UniformIdDecorationWithWrongInstructionBad) {
5721 const std::string spirv = ShaderWithUniformLikeDecoration(
5722 "OpDecorate %int0 UniformId %subgroupscope");
5723
5724 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_4);
5725 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_UNIVERSAL_1_4));
5726 EXPECT_THAT(
5727 getDiagnosticString(),
5728 HasSubstr(
5729 "Decorations taking ID parameters may not be used with OpDecorateId\n"
5730 " OpDecorate %int0 UniformId %subgroupscope"));
5731 }
5732
5733 TEST_F(ValidateDecorations, MultipleOffsetDecorationsOnSameID) {
5734 std::string spirv = R"(
5735 OpCapability Shader
5736 OpMemoryModel Logical GLSL450
5737 OpEntryPoint Fragment %1 "main"
5738 OpExecutionMode %1 OriginUpperLeft
5739
5740 OpMemberDecorate %struct 0 Offset 0
5741 OpMemberDecorate %struct 0 Offset 0
5742
5743 %void = OpTypeVoid
5744 %voidfn = OpTypeFunction %void
5745 %float = OpTypeFloat 32
5746 %struct = OpTypeStruct %float
5747
5748 %1 = OpFunction %void None %voidfn
5749 %label = OpLabel
5750 OpReturn
5751 OpFunctionEnd
5752 )";
5753
5754 CompileSuccessfully(spirv);
5755 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateAndRetrieveValidationState());
5756 EXPECT_THAT(getDiagnosticString(),
5757 HasSubstr("ID '2', member '0' decorated with Offset multiple "
5758 "times is not allowed."));
5759 }
5760
5761 TEST_F(ValidateDecorations, MultipleArrayStrideDecorationsOnSameID) {
5762 std::string spirv = R"(
5763 OpCapability Shader
5764 OpMemoryModel Logical GLSL450
5765 OpEntryPoint Fragment %1 "main"
5766 OpExecutionMode %1 OriginUpperLeft
5767
5768 OpDecorate %array ArrayStride 4
5769 OpDecorate %array ArrayStride 4
5770
5771 %void = OpTypeVoid
5772 %voidfn = OpTypeFunction %void
5773 %float = OpTypeFloat 32
5774 %uint = OpTypeInt 32 0
5775 %uint_4 = OpConstant %uint 4
5776 %array = OpTypeArray %float %uint_4
5777
5778 %1 = OpFunction %void None %voidfn
5779 %label = OpLabel
5780 OpReturn
5781 OpFunctionEnd
5782 )";
5783
5784 CompileSuccessfully(spirv);
5785 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateAndRetrieveValidationState());
5786 EXPECT_THAT(getDiagnosticString(),
5787 HasSubstr("ID '2' decorated with ArrayStride multiple "
5788 "times is not allowed."));
5789 }
5790
5791 TEST_F(ValidateDecorations, MultipleMatrixStrideDecorationsOnSameID) {
5792 std::string spirv = R"(
5793 OpCapability Shader
5794 OpMemoryModel Logical GLSL450
5795 OpEntryPoint Fragment %1 "main"
5796 OpExecutionMode %1 OriginUpperLeft
5797
5798 OpMemberDecorate %struct 0 Offset 0
5799 OpMemberDecorate %struct 0 ColMajor
5800 OpMemberDecorate %struct 0 MatrixStride 16
5801 OpMemberDecorate %struct 0 MatrixStride 16
5802
5803 %void = OpTypeVoid
5804 %voidfn = OpTypeFunction %void
5805 %float = OpTypeFloat 32
5806 %fvec4 = OpTypeVector %float 4
5807 %fmat4 = OpTypeMatrix %fvec4 4
5808 %struct = OpTypeStruct %fmat4
5809
5810 %1 = OpFunction %void None %voidfn
5811 %label = OpLabel
5812 OpReturn
5813 OpFunctionEnd
5814 )";
5815
5816 CompileSuccessfully(spirv);
5817 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateAndRetrieveValidationState());
5818 EXPECT_THAT(getDiagnosticString(),
5819 HasSubstr("ID '2', member '0' decorated with MatrixStride "
5820 "multiple times is not allowed."));
5821 }
5822
5823 TEST_F(ValidateDecorations, MultipleRowMajorDecorationsOnSameID) {
5824 std::string spirv = R"(
5825 OpCapability Shader
5826 OpMemoryModel Logical GLSL450
5827 OpEntryPoint Fragment %1 "main"
5828 OpExecutionMode %1 OriginUpperLeft
5829
5830 OpMemberDecorate %struct 0 Offset 0
5831 OpMemberDecorate %struct 0 MatrixStride 16
5832 OpMemberDecorate %struct 0 RowMajor
5833 OpMemberDecorate %struct 0 RowMajor
5834
5835 %void = OpTypeVoid
5836 %voidfn = OpTypeFunction %void
5837 %float = OpTypeFloat 32
5838 %fvec4 = OpTypeVector %float 4
5839 %fmat4 = OpTypeMatrix %fvec4 4
5840 %struct = OpTypeStruct %fmat4
5841
5842 %1 = OpFunction %void None %voidfn
5843 %label = OpLabel
5844 OpReturn
5845 OpFunctionEnd
5846 )";
5847
5848 CompileSuccessfully(spirv);
5849 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateAndRetrieveValidationState());
5850 EXPECT_THAT(getDiagnosticString(),
5851 HasSubstr("ID '2', member '0' decorated with RowMajor multiple "
5852 "times is not allowed."));
5853 }
5854
5855 TEST_F(ValidateDecorations, MultipleColMajorDecorationsOnSameID) {
5856 std::string spirv = R"(
5857 OpCapability Shader
5858 OpMemoryModel Logical GLSL450
5859 OpEntryPoint Fragment %1 "main"
5860 OpExecutionMode %1 OriginUpperLeft
5861
5862 OpMemberDecorate %struct 0 Offset 0
5863 OpMemberDecorate %struct 0 MatrixStride 16
5864 OpMemberDecorate %struct 0 ColMajor
5865 OpMemberDecorate %struct 0 ColMajor
5866
5867 %void = OpTypeVoid
5868 %voidfn = OpTypeFunction %void
5869 %float = OpTypeFloat 32
5870 %fvec4 = OpTypeVector %float 4
5871 %fmat4 = OpTypeMatrix %fvec4 4
5872 %struct = OpTypeStruct %fmat4
5873
5874 %1 = OpFunction %void None %voidfn
5875 %label = OpLabel
5876 OpReturn
5877 OpFunctionEnd
5878 )";
5879
5880 CompileSuccessfully(spirv);
5881 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateAndRetrieveValidationState());
5882 EXPECT_THAT(getDiagnosticString(),
5883 HasSubstr("ID '2', member '0' decorated with ColMajor multiple "
5884 "times is not allowed."));
5885 }
5886
5887 TEST_F(ValidateDecorations, RowMajorAndColMajorDecorationsOnSameID) {
5888 std::string spirv = R"(
5889 OpCapability Shader
5890 OpMemoryModel Logical GLSL450
5891 OpEntryPoint Fragment %1 "main"
5892 OpExecutionMode %1 OriginUpperLeft
5893
5894 OpMemberDecorate %struct 0 Offset 0
5895 OpMemberDecorate %struct 0 MatrixStride 16
5896 OpMemberDecorate %struct 0 ColMajor
5897 OpMemberDecorate %struct 0 RowMajor
5898
5899 %void = OpTypeVoid
5900 %voidfn = OpTypeFunction %void
5901 %float = OpTypeFloat 32
5902 %fvec4 = OpTypeVector %float 4
5903 %fmat4 = OpTypeMatrix %fvec4 4
5904 %struct = OpTypeStruct %fmat4
5905
5906 %1 = OpFunction %void None %voidfn
5907 %label = OpLabel
5908 OpReturn
5909 OpFunctionEnd
5910 )";
5911
5912 CompileSuccessfully(spirv);
5913 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateAndRetrieveValidationState());
5914 EXPECT_THAT(getDiagnosticString(),
5915 HasSubstr("ID '2', member '0' decorated with both RowMajor and "
5916 "ColMajor is not allowed."));
5917 }
5918
5919 TEST_F(ValidateDecorations, BlockAndBufferBlockDecorationsOnSameID) {
5920 std::string spirv = R"(
5921 OpCapability Shader
5922 OpMemoryModel Logical GLSL450
5923 OpEntryPoint Fragment %1 "main"
5924 OpExecutionMode %1 OriginUpperLeft
5925
5926 OpDecorate %struct Block
5927 OpDecorate %struct BufferBlock
5928 OpMemberDecorate %struct 0 Offset 0
5929 OpMemberDecorate %struct 0 MatrixStride 16
5930 OpMemberDecorate %struct 0 RowMajor
5931
5932 %void = OpTypeVoid
5933 %voidfn = OpTypeFunction %void
5934 %float = OpTypeFloat 32
5935 %fvec4 = OpTypeVector %float 4
5936 %fmat4 = OpTypeMatrix %fvec4 4
5937 %struct = OpTypeStruct %fmat4
5938
5939 %1 = OpFunction %void None %voidfn
5940 %label = OpLabel
5941 OpReturn
5942 OpFunctionEnd
5943 )";
5944
5945 CompileSuccessfully(spirv);
5946 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateAndRetrieveValidationState());
5947 EXPECT_THAT(
5948 getDiagnosticString(),
5949 HasSubstr(
5950 "ID '2' decorated with both BufferBlock and Block is not allowed."));
5951 }
5952
5953 std::string MakeIntegerShader(
5954 const std::string& decoration, const std::string& inst,
5955 const std::string& extension =
5956 "OpExtension \"SPV_KHR_no_integer_wrap_decoration\"") {
5957 return R"(
5958 OpCapability Shader
5959 OpCapability Linkage
5960 )" + extension +
5961 R"(
5962 %glsl = OpExtInstImport "GLSL.std.450"
5963 %opencl = OpExtInstImport "OpenCL.std"
5964 OpMemoryModel Logical GLSL450
5965 OpEntryPoint GLCompute %main "main"
5966 OpName %entry "entry"
5967 )" + decoration +
5968 R"(
5969 %void = OpTypeVoid
5970 %voidfn = OpTypeFunction %void
5971 %int = OpTypeInt 32 1
5972 %zero = OpConstantNull %int
5973 %float = OpTypeFloat 32
5974 %float0 = OpConstantNull %float
5975 %main = OpFunction %void None %voidfn
5976 %entry = OpLabel
5977 )" + inst +
5978 R"(
5979 OpReturn
5980 OpFunctionEnd)";
5981 }
5982
5983 // NoSignedWrap
5984
5985 TEST_F(ValidateDecorations, NoSignedWrapOnTypeBad) {
5986 std::string spirv = MakeIntegerShader("OpDecorate %void NoSignedWrap", "");
5987
5988 CompileSuccessfully(spirv);
5989 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
5990 EXPECT_THAT(
5991 getDiagnosticString(),
5992 HasSubstr("NoSignedWrap decoration may not be applied to TypeVoid"));
5993 }
5994
5995 TEST_F(ValidateDecorations, NoSignedWrapOnLabelBad) {
5996 std::string spirv = MakeIntegerShader("OpDecorate %entry NoSignedWrap", "");
5997
5998 CompileSuccessfully(spirv);
5999 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
6000 EXPECT_THAT(getDiagnosticString(),
6001 HasSubstr("NoSignedWrap decoration may not be applied to Label"));
6002 }
6003
6004 TEST_F(ValidateDecorations, NoSignedWrapRequiresExtensionBad) {
6005 std::string spirv = MakeIntegerShader("OpDecorate %val NoSignedWrap",
6006 "%val = OpIAdd %int %zero %zero", "");
6007
6008 CompileSuccessfully(spirv);
6009 EXPECT_NE(SPV_SUCCESS, ValidateInstructions());
6010 EXPECT_THAT(getDiagnosticString(),
6011 HasSubstr("requires one of these extensions: "
6012 "SPV_KHR_no_integer_wrap_decoration"));
6013 }
6014
6015 TEST_F(ValidateDecorations, NoSignedWrapRequiresExtensionV13Bad) {
6016 std::string spirv = MakeIntegerShader("OpDecorate %val NoSignedWrap",
6017 "%val = OpIAdd %int %zero %zero", "");
6018
6019 CompileSuccessfully(spirv);
6020 EXPECT_NE(SPV_SUCCESS, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
6021 EXPECT_THAT(getDiagnosticString(),
6022 HasSubstr("requires one of these extensions: "
6023 "SPV_KHR_no_integer_wrap_decoration"));
6024 }
6025
6026 TEST_F(ValidateDecorations, NoSignedWrapOkInSPV14Good) {
6027 std::string spirv = MakeIntegerShader("OpDecorate %val NoSignedWrap",
6028 "%val = OpIAdd %int %zero %zero", "");
6029
6030 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_4);
6031 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_UNIVERSAL_1_4));
6032 EXPECT_THAT(getDiagnosticString(), Eq(""));
6033 }
6034
6035 TEST_F(ValidateDecorations, NoSignedWrapIAddGood) {
6036 std::string spirv = MakeIntegerShader("OpDecorate %val NoSignedWrap",
6037 "%val = OpIAdd %int %zero %zero");
6038
6039 CompileSuccessfully(spirv);
6040 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
6041 EXPECT_THAT(getDiagnosticString(), Eq(""));
6042 }
6043
6044 TEST_F(ValidateDecorations, NoSignedWrapISubGood) {
6045 std::string spirv = MakeIntegerShader("OpDecorate %val NoSignedWrap",
6046 "%val = OpISub %int %zero %zero");
6047
6048 CompileSuccessfully(spirv);
6049 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
6050 EXPECT_THAT(getDiagnosticString(), Eq(""));
6051 }
6052
6053 TEST_F(ValidateDecorations, NoSignedWrapIMulGood) {
6054 std::string spirv = MakeIntegerShader("OpDecorate %val NoSignedWrap",
6055 "%val = OpIMul %int %zero %zero");
6056
6057 CompileSuccessfully(spirv);
6058 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
6059 EXPECT_THAT(getDiagnosticString(), Eq(""));
6060 }
6061
6062 TEST_F(ValidateDecorations, NoSignedWrapShiftLeftLogicalGood) {
6063 std::string spirv =
6064 MakeIntegerShader("OpDecorate %val NoSignedWrap",
6065 "%val = OpShiftLeftLogical %int %zero %zero");
6066
6067 CompileSuccessfully(spirv);
6068 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
6069 EXPECT_THAT(getDiagnosticString(), Eq(""));
6070 }
6071
6072 TEST_F(ValidateDecorations, NoSignedWrapSNegateGood) {
6073 std::string spirv = MakeIntegerShader("OpDecorate %val NoSignedWrap",
6074 "%val = OpSNegate %int %zero");
6075
6076 CompileSuccessfully(spirv);
6077 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
6078 EXPECT_THAT(getDiagnosticString(), Eq(""));
6079 }
6080
6081 TEST_F(ValidateDecorations, NoSignedWrapSRemBad) {
6082 std::string spirv = MakeIntegerShader("OpDecorate %val NoSignedWrap",
6083 "%val = OpSRem %int %zero %zero");
6084
6085 CompileSuccessfully(spirv);
6086 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
6087 EXPECT_THAT(getDiagnosticString(),
6088 HasSubstr("NoSignedWrap decoration may not be applied to SRem"));
6089 }
6090
6091 TEST_F(ValidateDecorations, NoSignedWrapFAddBad) {
6092 std::string spirv = MakeIntegerShader("OpDecorate %val NoSignedWrap",
6093 "%val = OpFAdd %float %float0 %float0");
6094
6095 CompileSuccessfully(spirv);
6096 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
6097 EXPECT_THAT(getDiagnosticString(),
6098 HasSubstr("NoSignedWrap decoration may not be applied to FAdd"));
6099 }
6100
6101 TEST_F(ValidateDecorations, NoSignedWrapExtInstOpenCLGood) {
6102 std::string spirv =
6103 MakeIntegerShader("OpDecorate %val NoSignedWrap",
6104 "%val = OpExtInst %int %opencl s_abs %zero");
6105
6106 CompileSuccessfully(spirv);
6107 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
6108 EXPECT_THAT(getDiagnosticString(), Eq(""));
6109 }
6110
6111 TEST_F(ValidateDecorations, NoSignedWrapExtInstGLSLGood) {
6112 std::string spirv = MakeIntegerShader(
6113 "OpDecorate %val NoSignedWrap", "%val = OpExtInst %int %glsl SAbs %zero");
6114
6115 CompileSuccessfully(spirv);
6116 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
6117 EXPECT_THAT(getDiagnosticString(), Eq(""));
6118 }
6119
6120 // TODO(dneto): For NoSignedWrap and NoUnsignedWrap, permit
6121 // "OpExtInst for instruction numbers specified in the extended
6122 // instruction-set specifications as accepting this decoration."
6123
6124 // NoUnignedWrap
6125
6126 TEST_F(ValidateDecorations, NoUnsignedWrapOnTypeBad) {
6127 std::string spirv = MakeIntegerShader("OpDecorate %void NoUnsignedWrap", "");
6128
6129 CompileSuccessfully(spirv);
6130 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
6131 EXPECT_THAT(
6132 getDiagnosticString(),
6133 HasSubstr("NoUnsignedWrap decoration may not be applied to TypeVoid"));
6134 }
6135
6136 TEST_F(ValidateDecorations, NoUnsignedWrapOnLabelBad) {
6137 std::string spirv = MakeIntegerShader("OpDecorate %entry NoUnsignedWrap", "");
6138
6139 CompileSuccessfully(spirv);
6140 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
6141 EXPECT_THAT(
6142 getDiagnosticString(),
6143 HasSubstr("NoUnsignedWrap decoration may not be applied to Label"));
6144 }
6145
6146 TEST_F(ValidateDecorations, NoUnsignedWrapRequiresExtensionBad) {
6147 std::string spirv = MakeIntegerShader("OpDecorate %val NoUnsignedWrap",
6148 "%val = OpIAdd %int %zero %zero", "");
6149
6150 CompileSuccessfully(spirv);
6151 EXPECT_NE(SPV_SUCCESS, ValidateInstructions());
6152 EXPECT_THAT(getDiagnosticString(),
6153 HasSubstr("requires one of these extensions: "
6154 "SPV_KHR_no_integer_wrap_decoration"));
6155 }
6156
6157 TEST_F(ValidateDecorations, NoUnsignedWrapRequiresExtensionV13Bad) {
6158 std::string spirv = MakeIntegerShader("OpDecorate %val NoUnsignedWrap",
6159 "%val = OpIAdd %int %zero %zero", "");
6160
6161 CompileSuccessfully(spirv);
6162 EXPECT_NE(SPV_SUCCESS, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
6163 EXPECT_THAT(getDiagnosticString(),
6164 HasSubstr("requires one of these extensions: "
6165 "SPV_KHR_no_integer_wrap_decoration"));
6166 }
6167
6168 TEST_F(ValidateDecorations, NoUnsignedWrapOkInSPV14Good) {
6169 std::string spirv = MakeIntegerShader("OpDecorate %val NoUnsignedWrap",
6170 "%val = OpIAdd %int %zero %zero", "");
6171
6172 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_4);
6173 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_UNIVERSAL_1_4));
6174 EXPECT_THAT(getDiagnosticString(), Eq(""));
6175 }
6176
6177 TEST_F(ValidateDecorations, NoUnsignedWrapIAddGood) {
6178 std::string spirv = MakeIntegerShader("OpDecorate %val NoUnsignedWrap",
6179 "%val = OpIAdd %int %zero %zero");
6180
6181 CompileSuccessfully(spirv);
6182 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
6183 EXPECT_THAT(getDiagnosticString(), Eq(""));
6184 }
6185
6186 TEST_F(ValidateDecorations, NoUnsignedWrapISubGood) {
6187 std::string spirv = MakeIntegerShader("OpDecorate %val NoUnsignedWrap",
6188 "%val = OpISub %int %zero %zero");
6189
6190 CompileSuccessfully(spirv);
6191 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
6192 EXPECT_THAT(getDiagnosticString(), Eq(""));
6193 }
6194
6195 TEST_F(ValidateDecorations, NoUnsignedWrapIMulGood) {
6196 std::string spirv = MakeIntegerShader("OpDecorate %val NoUnsignedWrap",
6197 "%val = OpIMul %int %zero %zero");
6198
6199 CompileSuccessfully(spirv);
6200 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
6201 EXPECT_THAT(getDiagnosticString(), Eq(""));
6202 }
6203
6204 TEST_F(ValidateDecorations, NoUnsignedWrapShiftLeftLogicalGood) {
6205 std::string spirv =
6206 MakeIntegerShader("OpDecorate %val NoUnsignedWrap",
6207 "%val = OpShiftLeftLogical %int %zero %zero");
6208
6209 CompileSuccessfully(spirv);
6210 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
6211 EXPECT_THAT(getDiagnosticString(), Eq(""));
6212 }
6213
6214 TEST_F(ValidateDecorations, NoUnsignedWrapSNegateGood) {
6215 std::string spirv = MakeIntegerShader("OpDecorate %val NoUnsignedWrap",
6216 "%val = OpSNegate %int %zero");
6217
6218 CompileSuccessfully(spirv);
6219 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
6220 EXPECT_THAT(getDiagnosticString(), Eq(""));
6221 }
6222
6223 TEST_F(ValidateDecorations, NoUnsignedWrapSRemBad) {
6224 std::string spirv = MakeIntegerShader("OpDecorate %val NoUnsignedWrap",
6225 "%val = OpSRem %int %zero %zero");
6226
6227 CompileSuccessfully(spirv);
6228 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
6229 EXPECT_THAT(
6230 getDiagnosticString(),
6231 HasSubstr("NoUnsignedWrap decoration may not be applied to SRem"));
6232 }
6233
6234 TEST_F(ValidateDecorations, NoUnsignedWrapFAddBad) {
6235 std::string spirv = MakeIntegerShader("OpDecorate %val NoUnsignedWrap",
6236 "%val = OpFAdd %float %float0 %float0");
6237
6238 CompileSuccessfully(spirv);
6239 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
6240 EXPECT_THAT(
6241 getDiagnosticString(),
6242 HasSubstr("NoUnsignedWrap decoration may not be applied to FAdd"));
6243 }
6244
6245 TEST_F(ValidateDecorations, NoUnsignedWrapExtInstOpenCLGood) {
6246 std::string spirv =
6247 MakeIntegerShader("OpDecorate %val NoUnsignedWrap",
6248 "%val = OpExtInst %int %opencl s_abs %zero");
6249
6250 CompileSuccessfully(spirv);
6251 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
6252 EXPECT_THAT(getDiagnosticString(), Eq(""));
6253 }
6254
6255 TEST_F(ValidateDecorations, NoUnsignedWrapExtInstGLSLGood) {
6256 std::string spirv =
6257 MakeIntegerShader("OpDecorate %val NoUnsignedWrap",
6258 "%val = OpExtInst %int %glsl SAbs %zero");
6259
6260 CompileSuccessfully(spirv);
6261 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
6262 EXPECT_THAT(getDiagnosticString(), Eq(""));
6263 }
6264
6265 TEST_F(ValidateDecorations, AliasedandRestrictBad) {
6266 const std::string body = R"(
6267 OpCapability Shader
6268 %1 = OpExtInstImport "GLSL.std.450"
6269 OpMemoryModel Logical GLSL450
6270 OpEntryPoint GLCompute %main "main"
6271 OpExecutionMode %main LocalSize 1 1 1
6272 OpSource GLSL 430
6273 OpMemberDecorate %Output 0 Offset 0
6274 OpDecorate %Output BufferBlock
6275 OpDecorate %dataOutput Restrict
6276 OpDecorate %dataOutput Aliased
6277 %void = OpTypeVoid
6278 %3 = OpTypeFunction %void
6279 %float = OpTypeFloat 32
6280 %Output = OpTypeStruct %float
6281 %_ptr_Uniform_Output = OpTypePointer Uniform %Output
6282 %dataOutput = OpVariable %_ptr_Uniform_Output Uniform
6283 %main = OpFunction %void None %3
6284 %5 = OpLabel
6285 OpReturn
6286 OpFunctionEnd
6287 )";
6288
6289 CompileSuccessfully(body.c_str());
6290 ASSERT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
6291 EXPECT_THAT(
6292 getDiagnosticString(),
6293 HasSubstr("decorated with both Aliased and Restrict is not allowed"));
6294 }
6295
6296 // TODO(dneto): For NoUnsignedWrap and NoUnsignedWrap, permit
6297 // "OpExtInst for instruction numbers specified in the extended
6298 // instruction-set specifications as accepting this decoration."
6299
6300 TEST_F(ValidateDecorations, PSBAliasedRestrictPointerSuccess) {
6301 const std::string body = R"(
6302 OpCapability PhysicalStorageBufferAddresses
6303 OpCapability Int64
6304 OpCapability Shader
6305 OpExtension "SPV_EXT_physical_storage_buffer"
6306 OpMemoryModel PhysicalStorageBuffer64 GLSL450
6307 OpEntryPoint Fragment %main "main"
6308 OpExecutionMode %main OriginUpperLeft
6309 OpDecorate %val1 RestrictPointer
6310 %uint64 = OpTypeInt 64 0
6311 %ptr = OpTypePointer PhysicalStorageBuffer %uint64
6312 %pptr_f = OpTypePointer Function %ptr
6313 %void = OpTypeVoid
6314 %voidfn = OpTypeFunction %void
6315 %main = OpFunction %void None %voidfn
6316 %entry = OpLabel
6317 %val1 = OpVariable %pptr_f Function
6318 OpReturn
6319 OpFunctionEnd
6320 )";
6321
6322 CompileSuccessfully(body.c_str());
6323 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
6324 }
6325
6326 TEST_F(ValidateDecorations, PSBAliasedRestrictPointerBoth) {
6327 const std::string body = R"(
6328 OpCapability PhysicalStorageBufferAddresses
6329 OpCapability Int64
6330 OpCapability Shader
6331 OpExtension "SPV_EXT_physical_storage_buffer"
6332 OpMemoryModel PhysicalStorageBuffer64 GLSL450
6333 OpEntryPoint Fragment %main "main"
6334 OpExecutionMode %main OriginUpperLeft
6335 OpDecorate %val1 RestrictPointer
6336 OpDecorate %val1 AliasedPointer
6337 %uint64 = OpTypeInt 64 0
6338 %ptr = OpTypePointer PhysicalStorageBuffer %uint64
6339 %pptr_f = OpTypePointer Function %ptr
6340 %void = OpTypeVoid
6341 %voidfn = OpTypeFunction %void
6342 %main = OpFunction %void None %voidfn
6343 %entry = OpLabel
6344 %val1 = OpVariable %pptr_f Function
6345 OpReturn
6346 OpFunctionEnd
6347 )";
6348
6349 CompileSuccessfully(body.c_str());
6350 ASSERT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
6351 EXPECT_THAT(getDiagnosticString(),
6352 HasSubstr("decorated with both AliasedPointer and "
6353 "RestrictPointer is not allowed"));
6354 }
6355
6356 TEST_F(ValidateDecorations, PSBAliasedRestrictFunctionParamSuccess) {
6357 const std::string body = R"(
6358 OpCapability PhysicalStorageBufferAddresses
6359 OpCapability Int64
6360 OpCapability Shader
6361 OpExtension "SPV_EXT_physical_storage_buffer"
6362 OpMemoryModel PhysicalStorageBuffer64 GLSL450
6363 OpEntryPoint Fragment %main "main"
6364 OpExecutionMode %main OriginUpperLeft
6365 OpDecorate %fparam Restrict
6366 %uint64 = OpTypeInt 64 0
6367 %ptr = OpTypePointer PhysicalStorageBuffer %uint64
6368 %void = OpTypeVoid
6369 %voidfn = OpTypeFunction %void
6370 %fnptr = OpTypeFunction %void %ptr
6371 %main = OpFunction %void None %voidfn
6372 %entry = OpLabel
6373 OpReturn
6374 OpFunctionEnd
6375 %fn = OpFunction %void None %fnptr
6376 %fparam = OpFunctionParameter %ptr
6377 %lab = OpLabel
6378 OpReturn
6379 OpFunctionEnd
6380 )";
6381
6382 CompileSuccessfully(body.c_str());
6383 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
6384 }
6385
6386 TEST_F(ValidateDecorations, PSBAliasedRestrictFunctionParamBoth) {
6387 const std::string body = R"(
6388 OpCapability PhysicalStorageBufferAddresses
6389 OpCapability Int64
6390 OpCapability Shader
6391 OpExtension "SPV_EXT_physical_storage_buffer"
6392 OpMemoryModel PhysicalStorageBuffer64 GLSL450
6393 OpEntryPoint Fragment %main "main"
6394 OpExecutionMode %main OriginUpperLeft
6395 OpDecorate %fparam Restrict
6396 OpDecorate %fparam Aliased
6397 %uint64 = OpTypeInt 64 0
6398 %ptr = OpTypePointer PhysicalStorageBuffer %uint64
6399 %void = OpTypeVoid
6400 %voidfn = OpTypeFunction %void
6401 %fnptr = OpTypeFunction %void %ptr
6402 %main = OpFunction %void None %voidfn
6403 %entry = OpLabel
6404 OpReturn
6405 OpFunctionEnd
6406 %fn = OpFunction %void None %fnptr
6407 %fparam = OpFunctionParameter %ptr
6408 %lab = OpLabel
6409 OpReturn
6410 OpFunctionEnd
6411 )";
6412
6413 CompileSuccessfully(body.c_str());
6414 ASSERT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
6415 EXPECT_THAT(
6416 getDiagnosticString(),
6417 HasSubstr("decorated with both Aliased and Restrict is not allowed"));
6418 }
6419
6420 TEST_F(ValidateDecorations, PSBFPRoundingModeSuccess) {
6421 std::string spirv = R"(
6422 OpCapability PhysicalStorageBufferAddresses
6423 OpCapability Shader
6424 OpCapability Linkage
6425 OpCapability StorageBuffer16BitAccess
6426 OpExtension "SPV_EXT_physical_storage_buffer"
6427 OpExtension "SPV_KHR_storage_buffer_storage_class"
6428 OpExtension "SPV_KHR_variable_pointers"
6429 OpExtension "SPV_KHR_16bit_storage"
6430 OpMemoryModel PhysicalStorageBuffer64 GLSL450
6431 OpEntryPoint GLCompute %main "main"
6432 OpDecorate %_ FPRoundingMode RTE
6433 OpDecorate %half_ptr_var AliasedPointer
6434 %half = OpTypeFloat 16
6435 %float = OpTypeFloat 32
6436 %float_1_25 = OpConstant %float 1.25
6437 %half_ptr = OpTypePointer PhysicalStorageBuffer %half
6438 %half_pptr_f = OpTypePointer Function %half_ptr
6439 %void = OpTypeVoid
6440 %func = OpTypeFunction %void
6441 %main = OpFunction %void None %func
6442 %main_entry = OpLabel
6443 %half_ptr_var = OpVariable %half_pptr_f Function
6444 %val1 = OpLoad %half_ptr %half_ptr_var
6445 %_ = OpFConvert %half %float_1_25
6446 OpStore %val1 %_ Aligned 2
6447 OpReturn
6448 OpFunctionEnd
6449 )";
6450
6451 CompileSuccessfully(spirv);
6452 EXPECT_EQ(SPV_SUCCESS, ValidateAndRetrieveValidationState());
6453 }
6454
6455 TEST_F(ValidateDecorations, InvalidStraddle) {
6456 const std::string spirv = R"(
6457 OpCapability Shader
6458 OpMemoryModel Logical GLSL450
6459 OpEntryPoint GLCompute %main "main"
6460 OpExecutionMode %main LocalSize 1 1 1
6461 OpMemberDecorate %inner_struct 0 Offset 0
6462 OpMemberDecorate %inner_struct 1 Offset 4
6463 OpDecorate %outer_struct Block
6464 OpMemberDecorate %outer_struct 0 Offset 0
6465 OpMemberDecorate %outer_struct 1 Offset 8
6466 OpDecorate %var DescriptorSet 0
6467 OpDecorate %var Binding 0
6468 %void = OpTypeVoid
6469 %float = OpTypeFloat 32
6470 %float2 = OpTypeVector %float 2
6471 %inner_struct = OpTypeStruct %float %float2
6472 %outer_struct = OpTypeStruct %float2 %inner_struct
6473 %ptr_ssbo_outer = OpTypePointer StorageBuffer %outer_struct
6474 %var = OpVariable %ptr_ssbo_outer StorageBuffer
6475 %void_fn = OpTypeFunction %void
6476 %main = OpFunction %void None %void_fn
6477 %entry = OpLabel
6478 OpReturn
6479 OpFunctionEnd
6480 )";
6481
6482 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_1);
6483 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_1));
6484 EXPECT_THAT(getDiagnosticString(),
6485 HasSubstr("Structure id 2 decorated as Block for variable in "
6486 "StorageBuffer storage class must follow relaxed "
6487 "storage buffer layout rules: member 1 is an "
6488 "improperly straddling vector at offset 12"));
6489 }
6490
6491 TEST_F(ValidateDecorations, DescriptorArray) {
6492 const std::string spirv = R"(
6493 OpCapability Shader
6494 OpExtension "SPV_KHR_storage_buffer_storage_class"
6495 OpMemoryModel Logical GLSL450
6496 OpEntryPoint GLCompute %main "main"
6497 OpExecutionMode %main LocalSize 1 1 1
6498 OpDecorate %struct Block
6499 OpMemberDecorate %struct 0 Offset 0
6500 OpMemberDecorate %struct 1 Offset 1
6501 OpDecorate %var DescriptorSet 0
6502 OpDecorate %var Binding 0
6503 %void = OpTypeVoid
6504 %float = OpTypeFloat 32
6505 %int = OpTypeInt 32 0
6506 %int_2 = OpConstant %int 2
6507 %float2 = OpTypeVector %float 2
6508 %struct = OpTypeStruct %float %float2
6509 %struct_array = OpTypeArray %struct %int_2
6510 %ptr_ssbo_array = OpTypePointer StorageBuffer %struct_array
6511 %var = OpVariable %ptr_ssbo_array StorageBuffer
6512 %void_fn = OpTypeFunction %void
6513 %main = OpFunction %void None %void_fn
6514 %entry = OpLabel
6515 OpReturn
6516 OpFunctionEnd
6517 )";
6518
6519 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_0);
6520 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_0));
6521 EXPECT_THAT(getDiagnosticString(),
6522 HasSubstr("Structure id 2 decorated as Block for variable in "
6523 "StorageBuffer storage class must follow standard "
6524 "storage buffer layout rules: member 1 at offset 1 is "
6525 "not aligned to 8"));
6526 }
6527
6528 TEST_F(ValidateDecorations, DescriptorRuntimeArray) {
6529 const std::string spirv = R"(
6530 OpCapability Shader
6531 OpCapability RuntimeDescriptorArrayEXT
6532 OpExtension "SPV_KHR_storage_buffer_storage_class"
6533 OpExtension "SPV_EXT_descriptor_indexing"
6534 OpMemoryModel Logical GLSL450
6535 OpEntryPoint GLCompute %main "main"
6536 OpExecutionMode %main LocalSize 1 1 1
6537 OpDecorate %struct Block
6538 OpMemberDecorate %struct 0 Offset 0
6539 OpMemberDecorate %struct 1 Offset 1
6540 OpDecorate %var DescriptorSet 0
6541 OpDecorate %var Binding 0
6542 %void = OpTypeVoid
6543 %float = OpTypeFloat 32
6544 %int = OpTypeInt 32 0
6545 %float2 = OpTypeVector %float 2
6546 %struct = OpTypeStruct %float %float2
6547 %struct_array = OpTypeRuntimeArray %struct
6548 %ptr_ssbo_array = OpTypePointer StorageBuffer %struct_array
6549 %var = OpVariable %ptr_ssbo_array StorageBuffer
6550 %void_fn = OpTypeFunction %void
6551 %main = OpFunction %void None %void_fn
6552 %entry = OpLabel
6553 OpReturn
6554 OpFunctionEnd
6555 )";
6556
6557 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_0);
6558 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_0));
6559 EXPECT_THAT(getDiagnosticString(),
6560 HasSubstr("Structure id 2 decorated as Block for variable in "
6561 "StorageBuffer storage class must follow standard "
6562 "storage buffer layout rules: member 1 at offset 1 is "
6563 "not aligned to 8"));
6564 }
6565
6566 TEST_F(ValidateDecorations, MultiDimensionalArray) {
6567 const std::string spirv = R"(
6568 OpCapability Shader
6569 OpMemoryModel Logical GLSL450
6570 OpEntryPoint GLCompute %main "main"
6571 OpExecutionMode %main LocalSize 1 1 1
6572 OpDecorate %struct Block
6573 OpMemberDecorate %struct 0 Offset 0
6574 OpDecorate %array_4 ArrayStride 4
6575 OpDecorate %array_3 ArrayStride 48
6576 OpDecorate %var DescriptorSet 0
6577 OpDecorate %var Binding 0
6578 %void = OpTypeVoid
6579 %int = OpTypeInt 32 0
6580 %int_3 = OpConstant %int 3
6581 %int_4 = OpConstant %int 4
6582 %array_4 = OpTypeArray %int %int_4
6583 %array_3 = OpTypeArray %array_4 %int_3
6584 %struct = OpTypeStruct %array_3
6585 %ptr_struct = OpTypePointer Uniform %struct
6586 %var = OpVariable %ptr_struct Uniform
6587 %void_fn = OpTypeFunction %void
6588 %main = OpFunction %void None %void_fn
6589 %entry = OpLabel
6590 OpReturn
6591 OpFunctionEnd
6592 )";
6593
6594 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_0);
6595 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_0));
6596 EXPECT_THAT(getDiagnosticString(),
6597 HasSubstr("Structure id 2 decorated as Block for variable in "
6598 "Uniform storage class must follow standard uniform "
6599 "buffer layout rules: member 0 contains an array with "
6600 "stride 4 not satisfying alignment to 16"));
6601 }
6602
6603 TEST_F(ValidateDecorations, ImproperStraddleInArray) {
6604 const std::string spirv = R"(
6605 OpCapability Shader
6606 OpMemoryModel Logical GLSL450
6607 OpEntryPoint GLCompute %main "main"
6608 OpExecutionMode %main LocalSize 1 1 1
6609 OpDecorate %struct Block
6610 OpMemberDecorate %struct 0 Offset 0
6611 OpDecorate %array ArrayStride 24
6612 OpMemberDecorate %inner 0 Offset 0
6613 OpMemberDecorate %inner 1 Offset 4
6614 OpMemberDecorate %inner 2 Offset 12
6615 OpMemberDecorate %inner 3 Offset 16
6616 OpDecorate %var DescriptorSet 0
6617 OpDecorate %var Binding 0
6618 %void = OpTypeVoid
6619 %int = OpTypeInt 32 0
6620 %int_2 = OpConstant %int 2
6621 %int2 = OpTypeVector %int 2
6622 %inner = OpTypeStruct %int %int2 %int %int
6623 %array = OpTypeArray %inner %int_2
6624 %struct = OpTypeStruct %array
6625 %ptr_struct = OpTypePointer StorageBuffer %struct
6626 %var = OpVariable %ptr_struct StorageBuffer
6627 %void_fn = OpTypeFunction %void
6628 %main = OpFunction %void None %void_fn
6629 %entry = OpLabel
6630 OpReturn
6631 OpFunctionEnd
6632 )";
6633
6634 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_1);
6635 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_1));
6636 EXPECT_THAT(getDiagnosticString(),
6637 HasSubstr("Structure id 4 decorated as Block for variable in "
6638 "StorageBuffer storage class must follow relaxed "
6639 "storage buffer layout rules: member 1 is an "
6640 "improperly straddling vector at offset 28"));
6641 }
6642
6643 TEST_F(ValidateDecorations, LargeArray) {
6644 const std::string spirv = R"(
6645 OpCapability Shader
6646 OpMemoryModel Logical GLSL450
6647 OpEntryPoint GLCompute %main "main"
6648 OpExecutionMode %main LocalSize 1 1 1
6649 OpDecorate %struct Block
6650 OpMemberDecorate %struct 0 Offset 0
6651 OpDecorate %array ArrayStride 24
6652 OpMemberDecorate %inner 0 Offset 0
6653 OpMemberDecorate %inner 1 Offset 8
6654 OpMemberDecorate %inner 2 Offset 16
6655 OpMemberDecorate %inner 3 Offset 20
6656 OpDecorate %var DescriptorSet 0
6657 OpDecorate %var Binding 0
6658 %void = OpTypeVoid
6659 %int = OpTypeInt 32 0
6660 %int_2000000 = OpConstant %int 2000000
6661 %int2 = OpTypeVector %int 2
6662 %inner = OpTypeStruct %int %int2 %int %int
6663 %array = OpTypeArray %inner %int_2000000
6664 %struct = OpTypeStruct %array
6665 %ptr_struct = OpTypePointer StorageBuffer %struct
6666 %var = OpVariable %ptr_struct StorageBuffer
6667 %void_fn = OpTypeFunction %void
6668 %main = OpFunction %void None %void_fn
6669 %entry = OpLabel
6670 OpReturn
6671 OpFunctionEnd
6672 )";
6673
6674 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_1);
6675 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_VULKAN_1_1));
6676 }
6677
6678 // NonReadable/NonWritable
6679
6680 // Returns a SPIR-V shader module with variables in various storage classes,
6681 // parameterizable by which ID should be decorated as NonReadable or
6682 // NonWritable.
6683 std::string ShaderWithNonReadableNonWritableTarget(
6684 const std::string& target, bool member_decorate = false,
6685 const std::string& decoration = "NonWritable") {
6686 const std::string decoration_inst =
6687 std::string(member_decorate ? "OpMemberDecorate " : "OpDecorate ") +
6688 target + (member_decorate ? " 0" : "");
6689
6690 return std::string(R"(
6691 OpCapability Shader
6692 OpCapability RuntimeDescriptorArrayEXT
6693 OpCapability TensorsARM
6694 OpExtension "SPV_EXT_descriptor_indexing"
6695 OpExtension "SPV_KHR_storage_buffer_storage_class"
6696 OpExtension "SPV_ARM_tensors"
6697 OpMemoryModel Logical GLSL450
6698 OpEntryPoint Vertex %main "main"
6699 OpName %label "label"
6700 OpName %param_f "param_f"
6701 OpName %param_p "param_p"
6702 OpName %_ptr_imstor "_ptr_imstor"
6703 OpName %_ptr_imsam "_ptr_imsam"
6704 OpName %var_wg "var_wg"
6705 OpName %var_imsam "var_imsam"
6706 OpName %var_priv "var_priv"
6707 OpName %var_func "var_func"
6708 OpName %simple_struct "simple_struct"
6709
6710 OpDecorate %struct_b Block
6711 OpDecorate %struct_b_rtarr Block
6712 OpMemberDecorate %struct_b 0 Offset 0
6713 OpMemberDecorate %struct_b_rtarr 0 Offset 0
6714 OpDecorate %rtarr ArrayStride 4
6715 )") + decoration_inst +
6716 " " + decoration +
6717
6718 R"(
6719 %void = OpTypeVoid
6720 %void_fn = OpTypeFunction %void
6721 %float = OpTypeFloat 32
6722 %float_0 = OpConstant %float 0
6723 %int = OpTypeInt 32 0
6724 %int_2 = OpConstant %int 2
6725 %struct_b = OpTypeStruct %float
6726 %rtarr = OpTypeRuntimeArray %float
6727 %struct_b_rtarr = OpTypeStruct %rtarr
6728 %simple_struct = OpTypeStruct %float
6729 ; storage image
6730 %imstor = OpTypeImage %float 2D 0 0 0 2 R32f
6731 ; sampled image
6732 %imsam = OpTypeImage %float 2D 0 0 0 1 R32f
6733 ; tensor
6734 %tensor = OpTypeTensorARM %float %int_2
6735 %array_imstor = OpTypeArray %imstor %int_2
6736 %rta_imstor = OpTypeRuntimeArray %imstor
6737
6738 %_ptr_Uniform_stb = OpTypePointer Uniform %struct_b
6739 %_ptr_StorageBuffer_stb = OpTypePointer StorageBuffer %struct_b
6740 %_ptr_StorageBuffer_stb_rtarr = OpTypePointer StorageBuffer %struct_b_rtarr
6741 %_ptr_Workgroup = OpTypePointer Workgroup %float
6742 %_ptr_Private = OpTypePointer Private %float
6743 %_ptr_Function = OpTypePointer Function %float
6744 %_ptr_imstor = OpTypePointer UniformConstant %imstor
6745 %_ptr_imsam = OpTypePointer UniformConstant %imsam
6746 %_ptr_array_imstor = OpTypePointer UniformConstant %array_imstor
6747 %_ptr_rta_imstor = OpTypePointer UniformConstant %rta_imstor
6748 %_ptr_tensor_UniformConstant = OpTypePointer UniformConstant %tensor
6749
6750 %extra_fn = OpTypeFunction %void %float %_ptr_Private %_ptr_imstor
6751
6752 %var_ubo = OpVariable %_ptr_Uniform_stb Uniform
6753 %var_ssbo_sb = OpVariable %_ptr_StorageBuffer_stb StorageBuffer
6754 %var_ssbo_sb_rtarr = OpVariable %_ptr_StorageBuffer_stb_rtarr StorageBuffer
6755 %var_wg = OpVariable %_ptr_Workgroup Workgroup
6756 %var_priv = OpVariable %_ptr_Private Private
6757 %var_imstor = OpVariable %_ptr_imstor UniformConstant
6758 %var_imsam = OpVariable %_ptr_imsam UniformConstant
6759 %var_array_imstor = OpVariable %_ptr_array_imstor UniformConstant
6760 %var_rta_imstor = OpVariable %_ptr_rta_imstor UniformConstant
6761 %var_tensor = OpVariable %_ptr_tensor_UniformConstant UniformConstant
6762
6763 %helper = OpFunction %void None %extra_fn
6764 %param_f = OpFunctionParameter %float
6765 %param_p = OpFunctionParameter %_ptr_Private
6766 %param_pimstor = OpFunctionParameter %_ptr_imstor
6767 %helper_label = OpLabel
6768 %helper_func_var = OpVariable %_ptr_Function Function
6769 OpReturn
6770 OpFunctionEnd
6771
6772 %main = OpFunction %void None %void_fn
6773 %label = OpLabel
6774 %var_func = OpVariable %_ptr_Function Function
6775 OpReturn
6776 OpFunctionEnd
6777 )";
6778 }
6779
6780 TEST_F(ValidateDecorations, NonWritableLabelTargetBad) {
6781 std::string spirv = ShaderWithNonReadableNonWritableTarget("%label");
6782
6783 CompileSuccessfully(spirv);
6784 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
6785 EXPECT_THAT(getDiagnosticString(),
6786 HasSubstr("must be a memory object declaration"));
6787 }
6788
6789 TEST_F(ValidateDecorations, NonWritableTypeTargetBad) {
6790 std::string spirv = ShaderWithNonReadableNonWritableTarget("%void");
6791
6792 CompileSuccessfully(spirv);
6793 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
6794 EXPECT_THAT(getDiagnosticString(),
6795 HasSubstr("must be a memory object declaration"));
6796 }
6797
6798 TEST_F(ValidateDecorations, NonWritableValueTargetBad) {
6799 std::string spirv = ShaderWithNonReadableNonWritableTarget("%float_0");
6800
6801 CompileSuccessfully(spirv);
6802 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
6803 EXPECT_THAT(getDiagnosticString(),
6804 HasSubstr("must be a memory object declaration"));
6805 }
6806
6807 TEST_F(ValidateDecorations, NonWritableValueParamBad) {
6808 std::string spirv = ShaderWithNonReadableNonWritableTarget("%param_f");
6809
6810 CompileSuccessfully(spirv);
6811 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
6812 EXPECT_THAT(getDiagnosticString(), HasSubstr("must be a pointer type"));
6813 }
6814
6815 TEST_F(ValidateDecorations, NonWritablePointerParamButWrongTypeBad) {
6816 std::string spirv = ShaderWithNonReadableNonWritableTarget("%param_p");
6817
6818 CompileSuccessfully(spirv);
6819 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
6820 EXPECT_THAT(
6821 getDiagnosticString(),
6822 HasSubstr(
6823 "Target of NonWritable decoration is invalid: must "
6824 "point to a storage image, tensor variable in UniformConstant "
6825 "storage class, uniform block, or storage "
6826 "buffer\n %param_p = OpFunctionParameter %_ptr_Private_float"));
6827 }
6828
6829 TEST_F(ValidateDecorations, NonWritablePointerParamStorageImageGood) {
6830 std::string spirv = ShaderWithNonReadableNonWritableTarget("%param_pimstor");
6831
6832 CompileSuccessfully(spirv);
6833 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
6834 EXPECT_THAT(getDiagnosticString(), Eq(""));
6835 }
6836
6837 TEST_F(ValidateDecorations, NonWritableVarStorageImageGood) {
6838 std::string spirv = ShaderWithNonReadableNonWritableTarget("%var_imstor");
6839
6840 CompileSuccessfully(spirv);
6841 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
6842 EXPECT_THAT(getDiagnosticString(), Eq(""));
6843 }
6844
6845 TEST_F(ValidateDecorations, NonWritableVarSampledImageBad) {
6846 std::string spirv = ShaderWithNonReadableNonWritableTarget("%var_imsam");
6847
6848 CompileSuccessfully(spirv);
6849 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
6850 EXPECT_THAT(
6851 getDiagnosticString(),
6852 HasSubstr("Target of NonWritable decoration is invalid: must "
6853 "point to a storage image, tensor variable in UniformConstant "
6854 "storage class, uniform block, or storage "
6855 "buffer\n %var_imsam"));
6856 }
6857
6858 TEST_F(ValidateDecorations, NonWritableVarUboGood) {
6859 std::string spirv = ShaderWithNonReadableNonWritableTarget("%var_ubo");
6860
6861 CompileSuccessfully(spirv);
6862 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
6863 EXPECT_THAT(getDiagnosticString(), Eq(""));
6864 }
6865
6866 TEST_F(ValidateDecorations, NonWritableVarSsboInUniformGood) {
6867 const std::string spirv = R"(
6868 OpCapability Shader
6869 OpMemoryModel Logical GLSL450
6870 OpEntryPoint Vertex %main "main"
6871 OpDecorate %struct_bb BufferBlock
6872 OpMemberDecorate %struct_bb 0 Offset 0
6873 OpDecorate %var_ssbo_u NonWritable
6874 %void = OpTypeVoid
6875 %void_fn = OpTypeFunction %void
6876 %float = OpTypeFloat 32
6877 %struct_bb = OpTypeStruct %float
6878 %_ptr_Uniform_stbb = OpTypePointer Uniform %struct_bb
6879 %var_ssbo_u = OpVariable %_ptr_Uniform_stbb Uniform
6880 %main = OpFunction %void None %void_fn
6881 %label = OpLabel
6882 OpReturn
6883 OpFunctionEnd
6884 )";
6885
6886 CompileSuccessfully(spirv);
6887 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
6888 EXPECT_THAT(getDiagnosticString(), Eq(""));
6889 }
6890
6891 TEST_F(ValidateDecorations, NonWritableVarSsboInStorageBufferGood) {
6892 std::string spirv = ShaderWithNonReadableNonWritableTarget("%var_ssbo_sb");
6893
6894 CompileSuccessfully(spirv);
6895 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
6896 EXPECT_THAT(getDiagnosticString(), Eq(""));
6897 }
6898
6899 TEST_F(ValidateDecorations, NonWritableMemberOfSsboInStorageBufferGood) {
6900 std::string spirv =
6901 ShaderWithNonReadableNonWritableTarget("%struct_b_rtarr", true);
6902
6903 CompileSuccessfully(spirv);
6904 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
6905 EXPECT_THAT(getDiagnosticString(), Eq(""));
6906 }
6907
6908 TEST_F(ValidateDecorations, NonWritableMemberOfStructGood) {
6909 std::string spirv =
6910 ShaderWithNonReadableNonWritableTarget("%simple_struct", true);
6911
6912 CompileSuccessfully(spirv);
6913 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
6914 }
6915
6916 TEST_F(ValidateDecorations, NonWritableTensorVarUniformConstantGood) {
6917 std::string spirv = ShaderWithNonReadableNonWritableTarget("%var_tensor");
6918
6919 CompileSuccessfully(spirv);
6920 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
6921 EXPECT_THAT(getDiagnosticString(), Eq(""));
6922 }
6923
6924 TEST_F(ValidateDecorations, NonReadableTensorVarUniformConstantGood) {
6925 std::string spirv = ShaderWithNonReadableNonWritableTarget(
6926 "%var_tensor", false, "NonReadable");
6927
6928 CompileSuccessfully(spirv);
6929 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
6930 EXPECT_THAT(getDiagnosticString(), Eq(""));
6931 }
6932
6933 TEST_F(ValidateDecorations, NonWritableVarWorkgroupBad) {
6934 std::string spirv = ShaderWithNonReadableNonWritableTarget("%var_wg");
6935
6936 CompileSuccessfully(spirv);
6937 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
6938 EXPECT_THAT(
6939 getDiagnosticString(),
6940 HasSubstr("Target of NonWritable decoration is invalid: must "
6941 "point to a storage image, tensor variable in UniformConstant "
6942 "storage class, uniform block, or storage "
6943 "buffer\n %var_wg"));
6944 }
6945
6946 TEST_F(ValidateDecorations, NonReadableVarWorkgroupBad) {
6947 std::string spirv =
6948 ShaderWithNonReadableNonWritableTarget("%var_wg", false, "NonReadable");
6949
6950 CompileSuccessfully(spirv);
6951 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
6952 EXPECT_THAT(
6953 getDiagnosticString(),
6954 HasSubstr("Target of NonReadable decoration is invalid: must "
6955 "point to a storage image, tensor variable in UniformConstant "
6956 "storage class, uniform block, or storage "
6957 "buffer\n %var_wg"));
6958 }
6959
6960 TEST_F(ValidateDecorations, NonWritableVarWorkgroupV14Bad) {
6961 std::string spirv = ShaderWithNonReadableNonWritableTarget("%var_wg");
6962
6963 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_4);
6964 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_UNIVERSAL_1_4));
6965 EXPECT_THAT(
6966 getDiagnosticString(),
6967 HasSubstr("Target of NonWritable decoration is invalid: must "
6968 "point to a storage image, tensor variable in UniformConstant "
6969 "storage class, uniform block, storage "
6970 "buffer, or variable in Private or Function storage class\n "
6971 "%var_wg"));
6972 }
6973
6974 TEST_F(ValidateDecorations, NonWritableVarPrivateBad) {
6975 std::string spirv = ShaderWithNonReadableNonWritableTarget("%var_priv");
6976
6977 CompileSuccessfully(spirv);
6978 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
6979 EXPECT_THAT(
6980 getDiagnosticString(),
6981 HasSubstr("Target of NonWritable decoration is invalid: must "
6982 "point to a storage image, tensor variable in UniformConstant "
6983 "storage class, uniform block, or storage "
6984 "buffer\n %var_priv"));
6985 }
6986
6987 TEST_F(ValidateDecorations, NonWritableVarPrivateV13Bad) {
6988 std::string spirv = ShaderWithNonReadableNonWritableTarget("%var_priv");
6989
6990 CompileSuccessfully(spirv);
6991 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
6992 EXPECT_THAT(
6993 getDiagnosticString(),
6994 HasSubstr("Target of NonWritable decoration is invalid: must "
6995 "point to a storage image, tensor variable in UniformConstant "
6996 "storage class, uniform block, or storage "
6997 "buffer\n %var_priv"));
6998 }
6999
7000 TEST_F(ValidateDecorations, NonWritableVarPrivateV14Good) {
7001 std::string spirv = ShaderWithNonReadableNonWritableTarget("%var_priv");
7002
7003 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_4);
7004 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_UNIVERSAL_1_4));
7005 EXPECT_THAT(getDiagnosticString(), Eq(""));
7006 }
7007
7008 TEST_F(ValidateDecorations, NonWritableVarPrivateV13TargetV14Bad) {
7009 std::string spirv = ShaderWithNonReadableNonWritableTarget("%var_priv");
7010
7011 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
7012 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_UNIVERSAL_1_4));
7013 EXPECT_THAT(
7014 getDiagnosticString(),
7015 HasSubstr("Target of NonWritable decoration is invalid: must "
7016 "point to a storage image, tensor variable in UniformConstant "
7017 "storage class, uniform block, or storage "
7018 "buffer\n %var_priv"));
7019 }
7020
7021 TEST_F(ValidateDecorations, NonWritableVarFunctionBad) {
7022 std::string spirv = ShaderWithNonReadableNonWritableTarget("%var_func");
7023
7024 CompileSuccessfully(spirv);
7025 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
7026 EXPECT_THAT(
7027 getDiagnosticString(),
7028 HasSubstr("Target of NonWritable decoration is invalid: must "
7029 "point to a storage image, tensor variable in UniformConstant "
7030 "storage class, uniform block, or storage "
7031 "buffer\n %var_func"));
7032 }
7033
7034 TEST_F(ValidateDecorations, NonWritableArrayGood) {
7035 std::string spirv =
7036 ShaderWithNonReadableNonWritableTarget("%var_array_imstor");
7037
7038 CompileSuccessfully(spirv);
7039 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
7040 }
7041
7042 TEST_F(ValidateDecorations, NonWritableRuntimeArrayGood) {
7043 std::string spirv = ShaderWithNonReadableNonWritableTarget("%var_rta_imstor");
7044
7045 CompileSuccessfully(spirv);
7046 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
7047 }
7048
7049 TEST_P(ValidateVulkanCombineDecorationResult, Decorate) {
7050 const char* const decoration = std::get<0>(GetParam());
7051 const char* const vuid = std::get<1>(GetParam());
7052 const TestResult& test_result = std::get<2>(GetParam());
7053
7054 CodeGenerator generator = CodeGenerator::GetDefaultShaderCodeGenerator();
7055 generator.before_types_ = "OpDecorate %u32 ";
7056 generator.before_types_ += decoration;
7057 generator.before_types_ += "\n";
7058
7059 EntryPoint entry_point;
7060 entry_point.name = "main";
7061 entry_point.execution_model = "Vertex";
7062 generator.entry_points_.push_back(std::move(entry_point));
7063
7064 CompileSuccessfully(generator.Build(), SPV_ENV_VULKAN_1_0);
7065 ASSERT_EQ(test_result.validation_result,
7066 ValidateInstructions(SPV_ENV_VULKAN_1_0));
7067 if (!test_result.error_str.empty()) {
7068 EXPECT_THAT(getDiagnosticString(), HasSubstr(test_result.error_str));
7069 }
7070 if (vuid) {
7071 EXPECT_THAT(getDiagnosticString(), AnyVUID(vuid));
7072 }
7073 }
7074
7075 INSTANTIATE_TEST_SUITE_P(
7076 DecorationAllowListFailure, ValidateVulkanCombineDecorationResult,
7077 Combine(Values("GLSLShared", "GLSLPacked"),
7078 Values("VUID-StandaloneSpirv-GLSLShared-04669"),
7079 Values(TestResult(
7080 SPV_ERROR_INVALID_ID,
7081 "is not valid for the Vulkan execution environment."))));
7082
7083 TEST_F(ValidateDecorations, NonWritableVarFunctionV13Bad) {
7084 std::string spirv = ShaderWithNonReadableNonWritableTarget("%var_func");
7085
7086 CompileSuccessfully(spirv);
7087 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
7088 EXPECT_THAT(
7089 getDiagnosticString(),
7090 HasSubstr("Target of NonWritable decoration is invalid: must "
7091 "point to a storage image, tensor variable in UniformConstant "
7092 "storage class, uniform block, or storage "
7093 "buffer\n %var_func"));
7094 }
7095
7096 TEST_F(ValidateDecorations, NonWritableVarFunctionV14Good) {
7097 std::string spirv = ShaderWithNonReadableNonWritableTarget("%var_func");
7098
7099 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_4);
7100 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_UNIVERSAL_1_4));
7101 EXPECT_THAT(getDiagnosticString(), Eq(""));
7102 }
7103
7104 TEST_F(ValidateDecorations, NonWritableVarFunctionV13TargetV14Bad) {
7105 std::string spirv = ShaderWithNonReadableNonWritableTarget("%var_func");
7106
7107 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
7108 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_UNIVERSAL_1_4));
7109 EXPECT_THAT(
7110 getDiagnosticString(),
7111 HasSubstr("Target of NonWritable decoration is invalid: must "
7112 "point to a storage image, tensor variable in UniformConstant "
7113 "storage class, uniform block, or storage "
7114 "buffer\n %var_func"));
7115 }
7116
7117 TEST_F(ValidateDecorations, BufferBlockV13ValV14Good) {
7118 std::string spirv = R"(
7119 OpCapability Shader
7120 OpCapability Linkage
7121 OpMemoryModel Logical GLSL450
7122 OpDecorate %1 BufferBlock
7123 %1 = OpTypeStruct
7124 )";
7125
7126 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
7127 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_UNIVERSAL_1_4));
7128 }
7129
7130 TEST_F(ValidateDecorations, BufferBlockV14Bad) {
7131 std::string spirv = R"(
7132 OpCapability Shader
7133 OpCapability Linkage
7134 OpMemoryModel Logical GLSL450
7135 OpDecorate %1 BufferBlock
7136 %1 = OpTypeStruct
7137 )";
7138
7139 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_4);
7140 EXPECT_EQ(SPV_ERROR_WRONG_VERSION,
7141 ValidateInstructions(SPV_ENV_UNIVERSAL_1_4));
7142 EXPECT_THAT(getDiagnosticString(),
7143 HasSubstr("2nd operand of Decorate: operand BufferBlock(3) "
7144 "requires SPIR-V version 1.3 or earlier"));
7145 }
7146
7147 // Component
7148
7149 TEST_F(ValidateDecorations, ComponentDecorationBadTarget) {
7150 std::string spirv = R"(
7151 OpCapability Shader
7152 OpMemoryModel Logical GLSL450
7153 OpEntryPoint Vertex %main "main"
7154 OpDecorate %t Component 0
7155 %void = OpTypeVoid
7156 %3 = OpTypeFunction %void
7157 %float = OpTypeFloat 32
7158 %t = OpTypeVector %float 2
7159 %main = OpFunction %void None %3
7160 %5 = OpLabel
7161 OpReturn
7162 OpFunctionEnd
7163 )";
7164
7165 CompileSuccessfully(spirv);
7166 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateAndRetrieveValidationState());
7167 EXPECT_THAT(getDiagnosticString(),
7168 HasSubstr("must be a memory object declaration"));
7169 }
7170
7171 TEST_F(ValidateDecorations, ComponentDecorationBadStorageClass) {
7172 std::string spirv = R"(
7173 OpCapability Shader
7174 OpMemoryModel Logical GLSL450
7175 OpEntryPoint Vertex %main "main"
7176 OpDecorate %v Component 0
7177 %void = OpTypeVoid
7178 %3 = OpTypeFunction %void
7179 %float = OpTypeFloat 32
7180 %t = OpTypeVector %float 2
7181 %ptr_private = OpTypePointer Private %t
7182 %v = OpVariable %ptr_private Private
7183 %main = OpFunction %void None %3
7184 %5 = OpLabel
7185 OpReturn
7186 OpFunctionEnd
7187 )";
7188
7189 CompileSuccessfully(spirv);
7190 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateAndRetrieveValidationState());
7191 EXPECT_THAT(getDiagnosticString(),
7192 HasSubstr("Target of Component decoration is invalid: must "
7193 "point to a Storage Class of Input(1) or Output(3)"));
7194 }
7195
7196 TEST_F(ValidateDecorations, ComponentDecorationBadTypeVulkan) {
7197 const spv_target_env env = SPV_ENV_VULKAN_1_0;
7198 std::string spirv = R"(
7199 OpCapability Shader
7200 OpCapability Matrix
7201 OpMemoryModel Logical GLSL450
7202 OpEntryPoint Vertex %main "main"
7203 OpDecorate %v Component 0
7204 %void = OpTypeVoid
7205 %3 = OpTypeFunction %void
7206 %float = OpTypeFloat 32
7207 %vtype = OpTypeVector %float 4
7208 %t = OpTypeMatrix %vtype 4
7209 %ptr_input = OpTypePointer Input %t
7210 %v = OpVariable %ptr_input Input
7211 %main = OpFunction %void None %3
7212 %5 = OpLabel
7213 OpReturn
7214 OpFunctionEnd
7215 )";
7216
7217 CompileSuccessfully(spirv, env);
7218 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateAndRetrieveValidationState(env));
7219 EXPECT_THAT(getDiagnosticString(),
7220 AnyVUID("VUID-StandaloneSpirv-Component-10583"));
7221 EXPECT_THAT(getDiagnosticString(),
7222 HasSubstr("Component decoration specified for type"));
7223 EXPECT_THAT(getDiagnosticString(), HasSubstr("is not a scalar or vector"));
7224 }
7225
7226 std::string ShaderWithComponentDecoration(const std::string& type,
7227 const std::string& decoration) {
7228 return R"(
7229 OpCapability Shader
7230 OpCapability Int64
7231 OpMemoryModel Logical GLSL450
7232 OpEntryPoint Fragment %main "main" %entryPointOutput
7233 OpExecutionMode %main OriginUpperLeft
7234 OpDecorate %entryPointOutput Location 0
7235 OpDecorate %entryPointOutput )" +
7236 decoration + R"(
7237 %void = OpTypeVoid
7238 %3 = OpTypeFunction %void
7239 %float = OpTypeFloat 32
7240 %v3float = OpTypeVector %float 3
7241 %v4float = OpTypeVector %float 4
7242 %uint = OpTypeInt 32 0
7243 %uint64 = OpTypeInt 64 0
7244 %v2uint64 = OpTypeVector %uint64 2
7245 %v3uint64 = OpTypeVector %uint64 3
7246 %uint_2 = OpConstant %uint 2
7247 %arr_v3float_uint_2 = OpTypeArray %v3float %uint_2
7248 %float_0 = OpConstant %float 0
7249 %_ptr_Output_type = OpTypePointer Output %)" + type + R"(
7250 %entryPointOutput = OpVariable %_ptr_Output_type Output
7251 %main = OpFunction %void None %3
7252 %5 = OpLabel
7253 OpReturn
7254 OpFunctionEnd
7255 )";
7256 }
7257
7258 TEST_F(ValidateDecorations, ComponentDecorationIntGood0Vulkan) {
7259 const spv_target_env env = SPV_ENV_VULKAN_1_0;
7260 std::string spirv = ShaderWithComponentDecoration("uint", "Component 0");
7261
7262 CompileSuccessfully(spirv, env);
7263 EXPECT_EQ(SPV_SUCCESS, ValidateAndRetrieveValidationState(env));
7264 EXPECT_THAT(getDiagnosticString(), Eq(""));
7265 }
7266
7267 TEST_F(ValidateDecorations, ComponentDecorationIntGood1Vulkan) {
7268 const spv_target_env env = SPV_ENV_VULKAN_1_0;
7269 std::string spirv = ShaderWithComponentDecoration("uint", "Component 1");
7270
7271 CompileSuccessfully(spirv, env);
7272 EXPECT_EQ(SPV_SUCCESS, ValidateAndRetrieveValidationState(env));
7273 EXPECT_THAT(getDiagnosticString(), Eq(""));
7274 }
7275
7276 TEST_F(ValidateDecorations, ComponentDecorationIntGood2Vulkan) {
7277 const spv_target_env env = SPV_ENV_VULKAN_1_0;
7278 std::string spirv = ShaderWithComponentDecoration("uint", "Component 2");
7279
7280 CompileSuccessfully(spirv, env);
7281 EXPECT_EQ(SPV_SUCCESS, ValidateAndRetrieveValidationState(env));
7282 EXPECT_THAT(getDiagnosticString(), Eq(""));
7283 }
7284
7285 TEST_F(ValidateDecorations, ComponentDecorationIntGood3Vulkan) {
7286 const spv_target_env env = SPV_ENV_VULKAN_1_0;
7287 std::string spirv = ShaderWithComponentDecoration("uint", "Component 3");
7288
7289 CompileSuccessfully(spirv, env);
7290 EXPECT_EQ(SPV_SUCCESS, ValidateAndRetrieveValidationState(env));
7291 EXPECT_THAT(getDiagnosticString(), Eq(""));
7292 }
7293
7294 TEST_F(ValidateDecorations, ComponentDecorationIntBad4Vulkan) {
7295 const spv_target_env env = SPV_ENV_VULKAN_1_0;
7296 std::string spirv = ShaderWithComponentDecoration("uint", "Component 4");
7297
7298 CompileSuccessfully(spirv, env);
7299 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateAndRetrieveValidationState(env));
7300 EXPECT_THAT(getDiagnosticString(),
7301 AnyVUID("VUID-StandaloneSpirv-Component-04920"));
7302 EXPECT_THAT(
7303 getDiagnosticString(),
7304 HasSubstr("Component decoration value must not be greater than 3"));
7305 }
7306
7307 TEST_F(ValidateDecorations, ComponentDecorationVector3GoodVulkan) {
7308 const spv_target_env env = SPV_ENV_VULKAN_1_0;
7309 std::string spirv = ShaderWithComponentDecoration("v3float", "Component 1");
7310
7311 CompileSuccessfully(spirv, env);
7312 EXPECT_EQ(SPV_SUCCESS, ValidateAndRetrieveValidationState(env));
7313 EXPECT_THAT(getDiagnosticString(), Eq(""));
7314 }
7315
7316 TEST_F(ValidateDecorations, ComponentDecorationVector4GoodVulkan) {
7317 const spv_target_env env = SPV_ENV_VULKAN_1_0;
7318 std::string spirv = ShaderWithComponentDecoration("v4float", "Component 0");
7319
7320 CompileSuccessfully(spirv, env);
7321 EXPECT_EQ(SPV_SUCCESS, ValidateAndRetrieveValidationState(env));
7322 EXPECT_THAT(getDiagnosticString(), Eq(""));
7323 }
7324
7325 TEST_F(ValidateDecorations, ComponentDecorationVector4Bad1Vulkan) {
7326 const spv_target_env env = SPV_ENV_VULKAN_1_0;
7327 std::string spirv = ShaderWithComponentDecoration("v4float", "Component 1");
7328
7329 CompileSuccessfully(spirv, env);
7330 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateAndRetrieveValidationState(env));
7331 EXPECT_THAT(getDiagnosticString(),
7332 AnyVUID("VUID-StandaloneSpirv-Component-04921"));
7333 EXPECT_THAT(getDiagnosticString(),
7334 HasSubstr("Sequence of components starting with 1 "
7335 "and ending with 4 gets larger than 3"));
7336 }
7337
7338 TEST_F(ValidateDecorations, ComponentDecorationVector4Bad3Vulkan) {
7339 const spv_target_env env = SPV_ENV_VULKAN_1_0;
7340 std::string spirv = ShaderWithComponentDecoration("v4float", "Component 3");
7341
7342 CompileSuccessfully(spirv, env);
7343 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateAndRetrieveValidationState(env));
7344 EXPECT_THAT(getDiagnosticString(),
7345 AnyVUID("VUID-StandaloneSpirv-Component-04921"));
7346 EXPECT_THAT(getDiagnosticString(),
7347 HasSubstr("Sequence of components starting with 3 "
7348 "and ending with 6 gets larger than 3"));
7349 }
7350
7351 TEST_F(ValidateDecorations, ComponentDecorationArrayGoodVulkan) {
7352 const spv_target_env env = SPV_ENV_VULKAN_1_0;
7353 std::string spirv =
7354 ShaderWithComponentDecoration("arr_v3float_uint_2", "Component 1");
7355
7356 CompileSuccessfully(spirv, env);
7357 EXPECT_EQ(SPV_SUCCESS, ValidateAndRetrieveValidationState(env));
7358 EXPECT_THAT(getDiagnosticString(), Eq(""));
7359 }
7360
7361 TEST_F(ValidateDecorations, ComponentDecorationArrayBadVulkan) {
7362 const spv_target_env env = SPV_ENV_VULKAN_1_0;
7363 std::string spirv =
7364 ShaderWithComponentDecoration("arr_v3float_uint_2", "Component 2");
7365
7366 CompileSuccessfully(spirv, env);
7367 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateAndRetrieveValidationState(env));
7368 EXPECT_THAT(getDiagnosticString(),
7369 AnyVUID("VUID-StandaloneSpirv-Component-04921"));
7370 EXPECT_THAT(getDiagnosticString(),
7371 HasSubstr("Sequence of components starting with 2 "
7372 "and ending with 4 gets larger than 3"));
7373 }
7374
7375 TEST_F(ValidateDecorations, ComponentDecoration64ScalarGoodVulkan) {
7376 const spv_target_env env = SPV_ENV_VULKAN_1_0;
7377 std::string spirv = ShaderWithComponentDecoration("uint64", "Component 0");
7378
7379 CompileSuccessfully(spirv, env);
7380 EXPECT_EQ(SPV_SUCCESS, ValidateAndRetrieveValidationState(env));
7381 }
7382
7383 TEST_F(ValidateDecorations, ComponentDecoration64Scalar1BadVulkan) {
7384 const spv_target_env env = SPV_ENV_VULKAN_1_0;
7385 std::string spirv = ShaderWithComponentDecoration("uint64", "Component 1");
7386
7387 CompileSuccessfully(spirv, env);
7388 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateAndRetrieveValidationState(env));
7389 EXPECT_THAT(getDiagnosticString(),
7390 AnyVUID("VUID-StandaloneSpirv-Component-04923"));
7391 EXPECT_THAT(getDiagnosticString(),
7392 HasSubstr("Component decoration value must not be 1 or 3 for "
7393 "64-bit data types"));
7394 }
7395
7396 TEST_F(ValidateDecorations, ComponentDecoration64Scalar2GoodVulkan) {
7397 const spv_target_env env = SPV_ENV_VULKAN_1_0;
7398 std::string spirv = ShaderWithComponentDecoration("uint64", "Component 2");
7399
7400 CompileSuccessfully(spirv, env);
7401 EXPECT_EQ(SPV_SUCCESS, ValidateAndRetrieveValidationState(env));
7402 }
7403
7404 TEST_F(ValidateDecorations, ComponentDecoration64Scalar3BadVulkan) {
7405 const spv_target_env env = SPV_ENV_VULKAN_1_0;
7406 std::string spirv = ShaderWithComponentDecoration("uint64", "Component 3");
7407
7408 CompileSuccessfully(spirv, env);
7409 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateAndRetrieveValidationState(env));
7410 EXPECT_THAT(getDiagnosticString(),
7411 AnyVUID("VUID-StandaloneSpirv-Component-04923"));
7412 EXPECT_THAT(getDiagnosticString(),
7413 HasSubstr("Component decoration value must not be 1 or 3 for "
7414 "64-bit data types"));
7415 }
7416
7417 TEST_F(ValidateDecorations, ComponentDecoration64Vec0GoodVulkan) {
7418 const spv_target_env env = SPV_ENV_VULKAN_1_0;
7419 std::string spirv = ShaderWithComponentDecoration("v2uint64", "Component 0");
7420
7421 CompileSuccessfully(spirv, env);
7422 EXPECT_EQ(SPV_SUCCESS, ValidateAndRetrieveValidationState(env));
7423 }
7424
7425 TEST_F(ValidateDecorations, ComponentDecoration64Vec1BadVulkan) {
7426 const spv_target_env env = SPV_ENV_VULKAN_1_0;
7427 std::string spirv = ShaderWithComponentDecoration("v2uint64", "Component 1");
7428
7429 CompileSuccessfully(spirv, env);
7430 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateAndRetrieveValidationState(env));
7431 EXPECT_THAT(getDiagnosticString(),
7432 AnyVUID("VUID-StandaloneSpirv-Component-04923"));
7433 EXPECT_THAT(getDiagnosticString(),
7434 HasSubstr("Component decoration value must not be 1 or 3 for "
7435 "64-bit data types"));
7436 }
7437
7438 TEST_F(ValidateDecorations, ComponentDecoration64Vec2BadVulkan) {
7439 const spv_target_env env = SPV_ENV_VULKAN_1_0;
7440 std::string spirv = ShaderWithComponentDecoration("v2uint64", "Component 2");
7441
7442 CompileSuccessfully(spirv, env);
7443 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateAndRetrieveValidationState(env));
7444 EXPECT_THAT(getDiagnosticString(),
7445 AnyVUID("VUID-StandaloneSpirv-Component-04922"));
7446 EXPECT_THAT(getDiagnosticString(),
7447 HasSubstr("Sequence of components starting with 2 "
7448 "and ending with 5 gets larger than 3"));
7449 }
7450
7451 TEST_F(ValidateDecorations, ComponentDecoration64VecWideBadVulkan) {
7452 const spv_target_env env = SPV_ENV_VULKAN_1_0;
7453 std::string spirv = ShaderWithComponentDecoration("v3uint64", "Component 0");
7454
7455 CompileSuccessfully(spirv, env);
7456 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateAndRetrieveValidationState(env));
7457 EXPECT_THAT(getDiagnosticString(),
7458 AnyVUID("VUID-StandaloneSpirv-Component-07703"));
7459 EXPECT_THAT(getDiagnosticString(),
7460 HasSubstr("Component decoration only allowed on 64-bit scalar "
7461 "and 2-component vector"));
7462 }
7463
7464 TEST_F(ValidateDecorations, ComponentDecorationBlockGood) {
7465 std::string spirv = R"(
7466 OpCapability Shader
7467 OpMemoryModel Logical GLSL450
7468 OpEntryPoint Fragment %4 "main" %9 %12
7469 OpExecutionMode %4 OriginUpperLeft
7470 OpDecorate %9 Location 0
7471 OpMemberDecorate %block 0 Location 2
7472 OpMemberDecorate %block 0 Component 1
7473 OpDecorate %block Block
7474 %2 = OpTypeVoid
7475 %3 = OpTypeFunction %2
7476 %float = OpTypeFloat 32
7477 %vec3 = OpTypeVector %float 3
7478 %8 = OpTypePointer Output %vec3
7479 %9 = OpVariable %8 Output
7480 %block = OpTypeStruct %vec3
7481 %11 = OpTypePointer Input %block
7482 %12 = OpVariable %11 Input
7483 %int = OpTypeInt 32 1
7484 %14 = OpConstant %int 0
7485 %15 = OpTypePointer Input %vec3
7486 %4 = OpFunction %2 None %3
7487 %5 = OpLabel
7488 %16 = OpAccessChain %15 %12 %14
7489 %17 = OpLoad %vec3 %16
7490 OpStore %9 %17
7491 OpReturn
7492 OpFunctionEnd
7493 )";
7494
7495 CompileSuccessfully(spirv);
7496 EXPECT_EQ(SPV_SUCCESS, ValidateAndRetrieveValidationState());
7497 EXPECT_THAT(getDiagnosticString(), Eq(""));
7498 }
7499
7500 TEST_F(ValidateDecorations, ComponentDecorationBlockBadVulkan) {
7501 const spv_target_env env = SPV_ENV_VULKAN_1_0;
7502 std::string spirv = R"(
7503 OpCapability Shader
7504 OpMemoryModel Logical GLSL450
7505 OpEntryPoint Fragment %4 "main" %9 %12
7506 OpExecutionMode %4 OriginUpperLeft
7507 OpDecorate %9 Location 0
7508 OpMemberDecorate %block 0 Location 2
7509 OpMemberDecorate %block 0 Component 2
7510 OpDecorate %block Block
7511 %2 = OpTypeVoid
7512 %3 = OpTypeFunction %2
7513 %float = OpTypeFloat 32
7514 %vec3 = OpTypeVector %float 3
7515 %8 = OpTypePointer Output %vec3
7516 %9 = OpVariable %8 Output
7517 %block = OpTypeStruct %vec3
7518 %11 = OpTypePointer Input %block
7519 %12 = OpVariable %11 Input
7520 %int = OpTypeInt 32 1
7521 %14 = OpConstant %int 0
7522 %15 = OpTypePointer Input %vec3
7523 %4 = OpFunction %2 None %3
7524 %5 = OpLabel
7525 %16 = OpAccessChain %15 %12 %14
7526 %17 = OpLoad %vec3 %16
7527 OpStore %9 %17
7528 OpReturn
7529 OpFunctionEnd
7530 )";
7531
7532 CompileSuccessfully(spirv, env);
7533 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateAndRetrieveValidationState(env));
7534 EXPECT_THAT(getDiagnosticString(),
7535 AnyVUID("VUID-StandaloneSpirv-Component-04921"));
7536 EXPECT_THAT(getDiagnosticString(),
7537 HasSubstr("Sequence of components starting with 2 "
7538 "and ending with 4 gets larger than 3"));
7539 }
7540
7541 TEST_F(ValidateDecorations, ComponentDecorationFunctionParameter) {
7542 std::string spirv = R"(
7543 OpCapability Shader
7544 OpMemoryModel Logical GLSL450
7545 OpEntryPoint Vertex %main "main"
7546
7547 OpDecorate %param_f Component 0
7548
7549 %void = OpTypeVoid
7550 %void_fn = OpTypeFunction %void
7551 %float = OpTypeFloat 32
7552 %float_0 = OpConstant %float 0
7553 %int = OpTypeInt 32 0
7554 %int_2 = OpConstant %int 2
7555 %struct_b = OpTypeStruct %float
7556
7557 %extra_fn = OpTypeFunction %void %float
7558
7559 %helper = OpFunction %void None %extra_fn
7560 %param_f = OpFunctionParameter %float
7561 %helper_label = OpLabel
7562 OpReturn
7563 OpFunctionEnd
7564
7565 %main = OpFunction %void None %void_fn
7566 %label = OpLabel
7567 OpReturn
7568 OpFunctionEnd
7569 )";
7570
7571 CompileSuccessfully(spirv);
7572 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateAndRetrieveValidationState());
7573 EXPECT_THAT(getDiagnosticString(), HasSubstr("must be a pointer type"));
7574 }
7575
7576 TEST_F(ValidateDecorations, VulkanStorageBufferBlock) {
7577 const std::string spirv = R"(
7578 OpCapability Shader
7579 OpExtension "SPV_KHR_storage_buffer_storage_class"
7580 OpMemoryModel Logical GLSL450
7581 OpEntryPoint GLCompute %main "main"
7582 OpExecutionMode %main LocalSize 1 1 1
7583 OpDecorate %struct Block
7584 OpMemberDecorate %struct 0 Offset 0
7585 OpDecorate %var DescriptorSet 0
7586 OpDecorate %var Binding 0
7587 %void = OpTypeVoid
7588 %uint = OpTypeInt 32 0
7589 %struct = OpTypeStruct %uint
7590 %ptr_ssbo = OpTypePointer StorageBuffer %struct
7591 %var = OpVariable %ptr_ssbo StorageBuffer
7592 %void_fn = OpTypeFunction %void
7593 %main = OpFunction %void None %void_fn
7594 %entry = OpLabel
7595 OpReturn
7596 OpFunctionEnd
7597 )";
7598
7599 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_0);
7600 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_VULKAN_1_0));
7601 }
7602
7603 TEST_F(ValidateDecorations, VulkanStorageBufferMissingBlock) {
7604 const std::string spirv = R"(
7605 OpCapability Shader
7606 OpExtension "SPV_KHR_storage_buffer_storage_class"
7607 OpMemoryModel Logical GLSL450
7608 OpEntryPoint GLCompute %main "main"
7609 OpExecutionMode %main LocalSize 1 1 1
7610 %void = OpTypeVoid
7611 %uint = OpTypeInt 32 0
7612 %struct = OpTypeStruct %uint
7613 %ptr_ssbo = OpTypePointer StorageBuffer %struct
7614 %var = OpVariable %ptr_ssbo StorageBuffer
7615 %void_fn = OpTypeFunction %void
7616 %main = OpFunction %void None %void_fn
7617 %entry = OpLabel
7618 OpReturn
7619 OpFunctionEnd
7620 )";
7621
7622 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_0);
7623 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_0));
7624 EXPECT_THAT(getDiagnosticString(),
7625 AnyVUID("VUID-StandaloneSpirv-PushConstant-06675"));
7626 EXPECT_THAT(getDiagnosticString(),
7627 HasSubstr("From Vulkan spec:\nSuch variables "
7628 "must be identified with a Block decoration"));
7629 }
7630
7631 TEST_F(ValidateDecorations, VulkanStorageBufferArrayMissingBlock) {
7632 const std::string spirv = R"(
7633 OpCapability Shader
7634 OpExtension "SPV_KHR_storage_buffer_storage_class"
7635 OpMemoryModel Logical GLSL450
7636 OpEntryPoint GLCompute %main "main"
7637 OpExecutionMode %main LocalSize 1 1 1
7638 %void = OpTypeVoid
7639 %uint = OpTypeInt 32 0
7640 %uint_4 = OpConstant %uint 4
7641 %struct = OpTypeStruct %uint
7642 %array = OpTypeArray %struct %uint_4
7643 %ptr_ssbo = OpTypePointer StorageBuffer %array
7644 %var = OpVariable %ptr_ssbo StorageBuffer
7645 %void_fn = OpTypeFunction %void
7646 %main = OpFunction %void None %void_fn
7647 %entry = OpLabel
7648 OpReturn
7649 OpFunctionEnd
7650 )";
7651
7652 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_0);
7653 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_0));
7654 EXPECT_THAT(getDiagnosticString(),
7655 AnyVUID("VUID-StandaloneSpirv-PushConstant-06675"));
7656 EXPECT_THAT(getDiagnosticString(),
7657 HasSubstr("From Vulkan spec:\nSuch variables "
7658 "must be identified with a Block decoration"));
7659 }
7660
7661 TEST_F(ValidateDecorations, VulkanStorageBufferRuntimeArrayMissingBlock) {
7662 const std::string spirv = R"(
7663 OpCapability Shader
7664 OpCapability RuntimeDescriptorArrayEXT
7665 OpExtension "SPV_EXT_descriptor_indexing"
7666 OpExtension "SPV_KHR_storage_buffer_storage_class"
7667 OpMemoryModel Logical GLSL450
7668 OpEntryPoint GLCompute %main "main"
7669 OpExecutionMode %main LocalSize 1 1 1
7670 %void = OpTypeVoid
7671 %uint = OpTypeInt 32 0
7672 %struct = OpTypeStruct %uint
7673 %array = OpTypeRuntimeArray %struct
7674 %ptr_ssbo = OpTypePointer StorageBuffer %array
7675 %var = OpVariable %ptr_ssbo StorageBuffer
7676 %void_fn = OpTypeFunction %void
7677 %main = OpFunction %void None %void_fn
7678 %entry = OpLabel
7679 OpReturn
7680 OpFunctionEnd
7681 )";
7682
7683 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_0);
7684 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_0));
7685 EXPECT_THAT(getDiagnosticString(),
7686 AnyVUID("VUID-StandaloneSpirv-PushConstant-06675"));
7687 EXPECT_THAT(getDiagnosticString(),
7688 HasSubstr("From Vulkan spec:\nSuch variables "
7689 "must be identified with a Block decoration"));
7690 }
7691
7692 TEST_F(ValidateDecorations, VulkanUniformBlock) {
7693 const std::string spirv = R"(
7694 OpCapability Shader
7695 OpMemoryModel Logical GLSL450
7696 OpEntryPoint GLCompute %main "main"
7697 OpExecutionMode %main LocalSize 1 1 1
7698 OpDecorate %struct Block
7699 OpMemberDecorate %struct 0 Offset 0
7700 OpDecorate %var DescriptorSet 0
7701 OpDecorate %var Binding 0
7702 %void = OpTypeVoid
7703 %uint = OpTypeInt 32 0
7704 %struct = OpTypeStruct %uint
7705 %ptr_ubo = OpTypePointer Uniform %struct
7706 %var = OpVariable %ptr_ubo Uniform
7707 %void_fn = OpTypeFunction %void
7708 %main = OpFunction %void None %void_fn
7709 %entry = OpLabel
7710 OpReturn
7711 OpFunctionEnd
7712 )";
7713
7714 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_0);
7715 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_VULKAN_1_0));
7716 }
7717
7718 TEST_F(ValidateDecorations, VulkanUniformBufferBlock) {
7719 const std::string spirv = R"(
7720 OpCapability Shader
7721 OpMemoryModel Logical GLSL450
7722 OpEntryPoint GLCompute %main "main"
7723 OpExecutionMode %main LocalSize 1 1 1
7724 OpDecorate %struct BufferBlock
7725 OpMemberDecorate %struct 0 Offset 0
7726 OpDecorate %var DescriptorSet 0
7727 OpDecorate %var Binding 0
7728 %void = OpTypeVoid
7729 %uint = OpTypeInt 32 0
7730 %struct = OpTypeStruct %uint
7731 %ptr_ubo = OpTypePointer Uniform %struct
7732 %var = OpVariable %ptr_ubo Uniform
7733 %void_fn = OpTypeFunction %void
7734 %main = OpFunction %void None %void_fn
7735 %entry = OpLabel
7736 OpReturn
7737 OpFunctionEnd
7738 )";
7739
7740 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_0);
7741 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_VULKAN_1_0));
7742 }
7743
7744 TEST_F(ValidateDecorations, VulkanUniformMissingBlock) {
7745 const std::string spirv = R"(
7746 OpCapability Shader
7747 OpMemoryModel Logical GLSL450
7748 OpEntryPoint GLCompute %main "main"
7749 OpExecutionMode %main LocalSize 1 1 1
7750 %void = OpTypeVoid
7751 %uint = OpTypeInt 32 0
7752 %struct = OpTypeStruct %uint
7753 %ptr_ubo = OpTypePointer Uniform %struct
7754 %var = OpVariable %ptr_ubo Uniform
7755 %void_fn = OpTypeFunction %void
7756 %main = OpFunction %void None %void_fn
7757 %entry = OpLabel
7758 OpReturn
7759 OpFunctionEnd
7760 )";
7761
7762 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_0);
7763 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_0));
7764 EXPECT_THAT(getDiagnosticString(),
7765 AnyVUID("VUID-StandaloneSpirv-Uniform-06676"));
7766 EXPECT_THAT(getDiagnosticString(),
7767 HasSubstr("From Vulkan spec:\nSuch variables must be "
7768 "identified with a Block or BufferBlock decoration"));
7769 }
7770
7771 TEST_F(ValidateDecorations, VulkanUniformArrayMissingBlock) {
7772 const std::string spirv = R"(
7773 OpCapability Shader
7774 OpMemoryModel Logical GLSL450
7775 OpEntryPoint GLCompute %main "main"
7776 OpExecutionMode %main LocalSize 1 1 1
7777 %void = OpTypeVoid
7778 %uint = OpTypeInt 32 0
7779 %uint_4 = OpConstant %uint 4
7780 %struct = OpTypeStruct %uint
7781 %array = OpTypeArray %struct %uint_4
7782 %ptr_ubo = OpTypePointer Uniform %array
7783 %var = OpVariable %ptr_ubo Uniform
7784 %void_fn = OpTypeFunction %void
7785 %main = OpFunction %void None %void_fn
7786 %entry = OpLabel
7787 OpReturn
7788 OpFunctionEnd
7789 )";
7790
7791 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_0);
7792 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_0));
7793 EXPECT_THAT(getDiagnosticString(),
7794 AnyVUID("VUID-StandaloneSpirv-Uniform-06676"));
7795 EXPECT_THAT(getDiagnosticString(),
7796 HasSubstr("From Vulkan spec:\nSuch variables must be "
7797 "identified with a Block or BufferBlock decoration"));
7798 }
7799
7800 TEST_F(ValidateDecorations, VulkanUniformRuntimeArrayMissingBlock) {
7801 const std::string spirv = R"(
7802 OpCapability Shader
7803 OpCapability RuntimeDescriptorArrayEXT
7804 OpExtension "SPV_EXT_descriptor_indexing"
7805 OpMemoryModel Logical GLSL450
7806 OpEntryPoint GLCompute %main "main"
7807 OpExecutionMode %main LocalSize 1 1 1
7808 %void = OpTypeVoid
7809 %uint = OpTypeInt 32 0
7810 %struct = OpTypeStruct %uint
7811 %array = OpTypeRuntimeArray %struct
7812 %ptr_ubo = OpTypePointer Uniform %array
7813 %var = OpVariable %ptr_ubo Uniform
7814 %void_fn = OpTypeFunction %void
7815 %main = OpFunction %void None %void_fn
7816 %entry = OpLabel
7817 OpReturn
7818 OpFunctionEnd
7819 )";
7820
7821 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_0);
7822 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_0));
7823 EXPECT_THAT(getDiagnosticString(),
7824 AnyVUID("VUID-StandaloneSpirv-Uniform-06676"));
7825 EXPECT_THAT(getDiagnosticString(),
7826 HasSubstr("From Vulkan spec:\nSuch variables must be "
7827 "identified with a Block or BufferBlock decoration"));
7828 }
7829
7830 TEST_F(ValidateDecorations, VulkanArrayStrideZero) {
7831 const std::string spirv = R"(
7832 OpCapability Shader
7833 OpMemoryModel Logical GLSL450
7834 OpEntryPoint GLCompute %main "main"
7835 OpExecutionMode %main LocalSize 1 1 1
7836 OpDecorate %var DescriptorSet 0
7837 OpDecorate %var Binding 0
7838 OpDecorate %struct Block
7839 OpMemberDecorate %struct 0 Offset 0
7840 OpDecorate %array ArrayStride 0
7841 %void = OpTypeVoid
7842 %int = OpTypeInt 32 0
7843 %int_4 = OpConstant %int 4
7844 %array = OpTypeArray %int %int_4
7845 %struct = OpTypeStruct %array
7846 %ptr_ssbo_struct = OpTypePointer StorageBuffer %struct
7847 %var = OpVariable %ptr_ssbo_struct StorageBuffer
7848 %void_fn = OpTypeFunction %void
7849 %main = OpFunction %void None %void_fn
7850 %entry = OpLabel
7851 OpReturn
7852 OpFunctionEnd
7853 )";
7854
7855 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_1);
7856 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_1));
7857 EXPECT_THAT(getDiagnosticString(),
7858 HasSubstr("contains an array with stride 0"));
7859 }
7860
7861 TEST_F(ValidateDecorations, VulkanArrayStrideTooSmall) {
7862 const std::string spirv = R"(
7863 OpCapability Shader
7864 OpMemoryModel Logical GLSL450
7865 OpEntryPoint GLCompute %main "main"
7866 OpExecutionMode %main LocalSize 1 1 1
7867 OpDecorate %var DescriptorSet 0
7868 OpDecorate %var Binding 0
7869 OpDecorate %struct Block
7870 OpMemberDecorate %struct 0 Offset 0
7871 OpDecorate %inner ArrayStride 4
7872 OpDecorate %outer ArrayStride 4
7873 %void = OpTypeVoid
7874 %int = OpTypeInt 32 0
7875 %int_4 = OpConstant %int 4
7876 %inner = OpTypeArray %int %int_4
7877 %outer = OpTypeArray %inner %int_4
7878 %struct = OpTypeStruct %outer
7879 %ptr_ssbo_struct = OpTypePointer StorageBuffer %struct
7880 %var = OpVariable %ptr_ssbo_struct StorageBuffer
7881 %void_fn = OpTypeFunction %void
7882 %main = OpFunction %void None %void_fn
7883 %entry = OpLabel
7884 OpReturn
7885 OpFunctionEnd
7886 )";
7887
7888 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_1);
7889 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_1));
7890 EXPECT_THAT(
7891 getDiagnosticString(),
7892 HasSubstr(
7893 "contains an array with stride 4, but with an element size of 16"));
7894 }
7895
7896 TEST_F(ValidateDecorations, FunctionsWithOpGroupDecorate) {
7897 std::string spirv = R"(
7898 OpCapability Addresses
7899 OpCapability Linkage
7900 OpCapability Kernel
7901 OpCapability Int8
7902 %1 = OpExtInstImport "OpenCL.std"
7903 OpMemoryModel Physical32 OpenCL
7904 OpName %foo "foo"
7905 OpName %entry "entry"
7906 OpName %bar "bar"
7907 OpName %entry_0 "entry"
7908 OpName %k "k"
7909 OpName %entry_1 "entry"
7910 OpName %b "b"
7911 OpDecorate %28 FuncParamAttr Zext
7912 %28 = OpDecorationGroup
7913 OpDecorate %k LinkageAttributes "k" Export
7914 OpDecorate %foo LinkageAttributes "foo" Export
7915 OpDecorate %bar LinkageAttributes "bar" Export
7916 OpDecorate %b Alignment 1
7917 OpGroupDecorate %28 %foo %bar
7918 %uchar = OpTypeInt 8 0
7919 %bool = OpTypeBool
7920 %3 = OpTypeFunction %bool
7921 %void = OpTypeVoid
7922 %10 = OpTypeFunction %void
7923 %_ptr_Function_uchar = OpTypePointer Function %uchar
7924 %true = OpConstantTrue %bool
7925 %foo = OpFunction %bool DontInline %3
7926 %entry = OpLabel
7927 OpReturnValue %true
7928 OpFunctionEnd
7929 %bar = OpFunction %bool DontInline %3
7930 %entry_0 = OpLabel
7931 OpReturnValue %true
7932 OpFunctionEnd
7933 %k = OpFunction %void DontInline %10
7934 %entry_1 = OpLabel
7935 %b = OpVariable %_ptr_Function_uchar Function
7936 OpReturn
7937 OpFunctionEnd
7938 )";
7939 CompileSuccessfully(spirv);
7940 EXPECT_EQ(SPV_SUCCESS, ValidateAndRetrieveValidationState());
7941 }
7942
7943 TEST_F(ValidateDecorations, LocationVariableGood) {
7944 const std::string spirv = R"(
7945 OpCapability Shader
7946 OpCapability Linkage
7947 OpMemoryModel Logical GLSL450
7948 OpDecorate %in_var Location 0
7949 %float = OpTypeFloat 32
7950 %ptr_input_float = OpTypePointer Input %float
7951 %in_var = OpVariable %ptr_input_float Input
7952 )";
7953
7954 CompileSuccessfully(spirv);
7955 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
7956 }
7957
7958 TEST_F(ValidateDecorations, LocationStructMemberGood) {
7959 const std::string spirv = R"(
7960 OpCapability Shader
7961 OpCapability Linkage
7962 OpMemoryModel Logical GLSL450
7963 OpMemberDecorate %struct 0 Location 0
7964 %float = OpTypeFloat 32
7965 %struct = OpTypeStruct %float
7966 )";
7967
7968 CompileSuccessfully(spirv);
7969 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
7970 }
7971
7972 TEST_F(ValidateDecorations, LocationStructBad) {
7973 const std::string spirv = R"(
7974 OpCapability Shader
7975 OpCapability Linkage
7976 OpMemoryModel Logical GLSL450
7977 OpDecorate %struct Location 0
7978 %float = OpTypeFloat 32
7979 %struct = OpTypeStruct %float
7980 )";
7981
7982 CompileSuccessfully(spirv);
7983 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
7984 EXPECT_THAT(getDiagnosticString(), HasSubstr("must be a variable"));
7985 }
7986
7987 TEST_F(ValidateDecorations, LocationFloatBad) {
7988 const std::string spirv = R"(
7989 OpCapability Shader
7990 OpCapability Linkage
7991 OpMemoryModel Logical GLSL450
7992 OpDecorate %float Location 0
7993 %float = OpTypeFloat 32
7994 )";
7995
7996 CompileSuccessfully(spirv);
7997 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
7998 EXPECT_THAT(getDiagnosticString(), HasSubstr("must be a variable"));
7999 }
8000
8001 TEST_F(ValidateDecorations, WorkgroupSingleBlockVariable) {
8002 std::string spirv = R"(
8003 OpCapability Shader
8004 OpCapability WorkgroupMemoryExplicitLayoutKHR
8005 OpExtension "SPV_KHR_workgroup_memory_explicit_layout"
8006 OpMemoryModel Logical GLSL450
8007 OpEntryPoint GLCompute %main "main" %_
8008 OpExecutionMode %main LocalSize 8 1 1
8009 OpMemberDecorate %first 0 Offset 0
8010 OpDecorate %first Block
8011 %void = OpTypeVoid
8012 %3 = OpTypeFunction %void
8013 %int = OpTypeInt 32 1
8014 %first = OpTypeStruct %int
8015 %_ptr_Workgroup_first = OpTypePointer Workgroup %first
8016 %_ = OpVariable %_ptr_Workgroup_first Workgroup
8017 %int_0 = OpConstant %int 0
8018 %int_2 = OpConstant %int 2
8019 %_ptr_Workgroup_int = OpTypePointer Workgroup %int
8020 %main = OpFunction %void None %3
8021 %5 = OpLabel
8022 %13 = OpAccessChain %_ptr_Workgroup_int %_ %int_0
8023 OpStore %13 %int_2
8024 OpReturn
8025 OpFunctionEnd
8026 )";
8027
8028 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_4);
8029 EXPECT_EQ(SPV_SUCCESS,
8030 ValidateAndRetrieveValidationState(SPV_ENV_UNIVERSAL_1_4));
8031 }
8032
8033 TEST_F(ValidateDecorations, WorkgroupBlockVariableRequiresV14) {
8034 std::string spirv = R"(
8035 OpCapability Shader
8036 OpCapability WorkgroupMemoryExplicitLayoutKHR
8037 OpExtension "SPV_KHR_workgroup_memory_explicit_layout"
8038 OpMemoryModel Logical GLSL450
8039 OpEntryPoint GLCompute %main "main" %_
8040 OpExecutionMode %main LocalSize 8 1 1
8041 OpMemberDecorate %first 0 Offset 0
8042 OpDecorate %first Block
8043 %void = OpTypeVoid
8044 %3 = OpTypeFunction %void
8045 %int = OpTypeInt 32 1
8046 %first = OpTypeStruct %int
8047 %_ptr_Workgroup_first = OpTypePointer Workgroup %first
8048 %_ = OpVariable %_ptr_Workgroup_first Workgroup
8049 %int_0 = OpConstant %int 0
8050 %int_2 = OpConstant %int 2
8051 %_ptr_Workgroup_int = OpTypePointer Workgroup %int
8052 %main = OpFunction %void None %3
8053 %5 = OpLabel
8054 %13 = OpAccessChain %_ptr_Workgroup_int %_ %int_0
8055 OpStore %13 %int_2
8056 OpReturn
8057 OpFunctionEnd
8058 )";
8059
8060 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
8061 EXPECT_EQ(SPV_ERROR_WRONG_VERSION,
8062 ValidateAndRetrieveValidationState(SPV_ENV_UNIVERSAL_1_4));
8063 EXPECT_THAT(getDiagnosticString(),
8064 HasSubstr("requires SPIR-V version 1.4 or later"));
8065 }
8066
8067 TEST_F(ValidateDecorations, WorkgroupSingleNonBlockVariable) {
8068 std::string spirv = R"(
8069 OpCapability Shader
8070 OpMemoryModel Logical GLSL450
8071 OpEntryPoint GLCompute %main "main" %a
8072 OpExecutionMode %main LocalSize 8 1 1
8073 %void = OpTypeVoid
8074 %3 = OpTypeFunction %void
8075 %int = OpTypeInt 32 1
8076 %_ptr_Workgroup_int = OpTypePointer Workgroup %int
8077 %a = OpVariable %_ptr_Workgroup_int Workgroup
8078 %int_2 = OpConstant %int 2
8079 %main = OpFunction %void None %3
8080 %5 = OpLabel
8081 OpStore %a %int_2
8082 OpReturn
8083 OpFunctionEnd
8084 )";
8085
8086 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_4);
8087 EXPECT_EQ(SPV_SUCCESS,
8088 ValidateAndRetrieveValidationState(SPV_ENV_UNIVERSAL_1_4));
8089 }
8090
8091 TEST_F(ValidateDecorations, WorkgroupMultiBlockVariable) {
8092 std::string spirv = R"(
8093 OpCapability Shader
8094 OpCapability WorkgroupMemoryExplicitLayoutKHR
8095 OpExtension "SPV_KHR_workgroup_memory_explicit_layout"
8096 OpMemoryModel Logical GLSL450
8097 OpEntryPoint GLCompute %main "main" %_ %__0
8098 OpExecutionMode %main LocalSize 8 1 1
8099 OpMemberDecorate %first 0 Offset 0
8100 OpDecorate %first Block
8101 OpMemberDecorate %second 0 Offset 0
8102 OpDecorate %second Block
8103 OpDecorate %_ Aliased
8104 OpDecorate %__0 Aliased
8105 %void = OpTypeVoid
8106 %3 = OpTypeFunction %void
8107 %int = OpTypeInt 32 1
8108 %first = OpTypeStruct %int
8109 %_ptr_Workgroup_first = OpTypePointer Workgroup %first
8110 %_ = OpVariable %_ptr_Workgroup_first Workgroup
8111 %int_0 = OpConstant %int 0
8112 %int_2 = OpConstant %int 2
8113 %_ptr_Workgroup_int = OpTypePointer Workgroup %int
8114 %second = OpTypeStruct %int
8115 %_ptr_Workgroup_second = OpTypePointer Workgroup %second
8116 %__0 = OpVariable %_ptr_Workgroup_second Workgroup
8117 %int_3 = OpConstant %int 3
8118 %main = OpFunction %void None %3
8119 %5 = OpLabel
8120 %13 = OpAccessChain %_ptr_Workgroup_int %_ %int_0
8121 OpStore %13 %int_2
8122 %18 = OpAccessChain %_ptr_Workgroup_int %__0 %int_0
8123 OpStore %18 %int_3
8124 OpReturn
8125 OpFunctionEnd
8126 )";
8127
8128 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_4);
8129 EXPECT_EQ(SPV_SUCCESS,
8130 ValidateAndRetrieveValidationState(SPV_ENV_UNIVERSAL_1_4));
8131 }
8132
8133 TEST_F(ValidateDecorations, WorkgroupBlockVariableWith8BitType) {
8134 std::string spirv = R"(
8135 OpCapability Shader
8136 OpCapability Int8
8137 OpCapability WorkgroupMemoryExplicitLayout8BitAccessKHR
8138 OpExtension "SPV_KHR_workgroup_memory_explicit_layout"
8139 OpMemoryModel Logical GLSL450
8140 OpEntryPoint GLCompute %main "main" %_
8141 OpExecutionMode %main LocalSize 2 1 1
8142 OpMemberDecorate %first 0 Offset 0
8143 OpDecorate %first Block
8144 %void = OpTypeVoid
8145 %3 = OpTypeFunction %void
8146 %char = OpTypeInt 8 1
8147 %first = OpTypeStruct %char
8148 %_ptr_Workgroup_first = OpTypePointer Workgroup %first
8149 %_ = OpVariable %_ptr_Workgroup_first Workgroup
8150 %int = OpTypeInt 32 1
8151 %int_0 = OpConstant %int 0
8152 %char_2 = OpConstant %char 2
8153 %_ptr_Workgroup_char = OpTypePointer Workgroup %char
8154 %main = OpFunction %void None %3
8155 %5 = OpLabel
8156 %14 = OpAccessChain %_ptr_Workgroup_char %_ %int_0
8157 OpStore %14 %char_2
8158 OpReturn
8159 OpFunctionEnd
8160 )";
8161
8162 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_4);
8163 EXPECT_EQ(SPV_SUCCESS,
8164 ValidateAndRetrieveValidationState(SPV_ENV_UNIVERSAL_1_4));
8165 }
8166
8167 TEST_F(ValidateDecorations, WorkgroupMultiNonBlockVariable) {
8168 std::string spirv = R"(
8169 OpCapability Shader
8170 OpMemoryModel Logical GLSL450
8171 OpEntryPoint GLCompute %main "main" %a %b
8172 OpExecutionMode %main LocalSize 8 1 1
8173 %void = OpTypeVoid
8174 %3 = OpTypeFunction %void
8175 %int = OpTypeInt 32 1
8176 %_ptr_Workgroup_int = OpTypePointer Workgroup %int
8177 %a = OpVariable %_ptr_Workgroup_int Workgroup
8178 %int_2 = OpConstant %int 2
8179 %b = OpVariable %_ptr_Workgroup_int Workgroup
8180 %int_3 = OpConstant %int 3
8181 %main = OpFunction %void None %3
8182 %5 = OpLabel
8183 OpStore %a %int_2
8184 OpStore %b %int_3
8185 OpReturn
8186 OpFunctionEnd
8187 )";
8188
8189 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_4);
8190 EXPECT_EQ(SPV_SUCCESS,
8191 ValidateAndRetrieveValidationState(SPV_ENV_UNIVERSAL_1_4));
8192 }
8193
8194 TEST_F(ValidateDecorations, WorkgroupBlockVariableWith16BitType) {
8195 std::string spirv = R"(
8196 OpCapability Shader
8197 OpCapability Float16
8198 OpCapability Int16
8199 OpCapability WorkgroupMemoryExplicitLayoutKHR
8200 OpCapability WorkgroupMemoryExplicitLayout16BitAccessKHR
8201 OpExtension "SPV_KHR_workgroup_memory_explicit_layout"
8202 OpMemoryModel Logical GLSL450
8203 OpEntryPoint GLCompute %main "main" %_
8204 OpExecutionMode %main LocalSize 2 1 1
8205 OpMemberDecorate %first 0 Offset 0
8206 OpMemberDecorate %first 1 Offset 2
8207 OpDecorate %first Block
8208 %void = OpTypeVoid
8209 %3 = OpTypeFunction %void
8210 %short = OpTypeInt 16 1
8211 %half = OpTypeFloat 16
8212 %first = OpTypeStruct %short %half
8213 %_ptr_Workgroup_first = OpTypePointer Workgroup %first
8214 %_ = OpVariable %_ptr_Workgroup_first Workgroup
8215 %int = OpTypeInt 32 1
8216 %int_0 = OpConstant %int 0
8217 %short_3 = OpConstant %short 3
8218 %_ptr_Workgroup_short = OpTypePointer Workgroup %short
8219 %int_1 = OpConstant %int 1
8220 %half_0x1_898p_3 = OpConstant %half 0x1.898p+3
8221 %_ptr_Workgroup_half = OpTypePointer Workgroup %half
8222 %main = OpFunction %void None %3
8223 %5 = OpLabel
8224 %15 = OpAccessChain %_ptr_Workgroup_short %_ %int_0
8225 OpStore %15 %short_3
8226 %19 = OpAccessChain %_ptr_Workgroup_half %_ %int_1
8227 OpStore %19 %half_0x1_898p_3
8228 OpReturn
8229 OpFunctionEnd
8230 )";
8231
8232 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_4);
8233 EXPECT_EQ(SPV_SUCCESS,
8234 ValidateAndRetrieveValidationState(SPV_ENV_UNIVERSAL_1_4));
8235 }
8236
8237 TEST_F(ValidateDecorations, WorkgroupBlockVariableScalarLayout) {
8238 std::string spirv = R"(
8239 OpCapability Shader
8240 OpCapability WorkgroupMemoryExplicitLayoutKHR
8241 OpExtension "SPV_KHR_workgroup_memory_explicit_layout"
8242 OpMemoryModel Logical GLSL450
8243 OpEntryPoint Vertex %main "main" %B
8244 OpSource GLSL 450
8245 OpMemberDecorate %S 0 Offset 0
8246 OpMemberDecorate %S 1 Offset 4
8247 OpMemberDecorate %S 2 Offset 16
8248 OpMemberDecorate %S 3 Offset 28
8249 OpDecorate %S Block
8250 OpDecorate %B Aliased
8251 %void = OpTypeVoid
8252 %3 = OpTypeFunction %void
8253 %float = OpTypeFloat 32
8254 %v3float = OpTypeVector %float 3
8255 %S = OpTypeStruct %float %v3float %v3float %v3float
8256 %_ptr_Workgroup_S = OpTypePointer Workgroup %S
8257 %B = OpVariable %_ptr_Workgroup_S Workgroup
8258 %main = OpFunction %void None %3
8259 %5 = OpLabel
8260 OpReturn
8261 OpFunctionEnd
8262 )";
8263
8264 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_4);
8265 spvValidatorOptionsSetWorkgroupScalarBlockLayout(getValidatorOptions(), true);
8266 EXPECT_EQ(SPV_SUCCESS, ValidateAndRetrieveValidationState(SPV_ENV_UNIVERSAL_1_4))
8267 << getDiagnosticString();
8268 }
8269
8270 TEST_F(ValidateDecorations, WorkgroupMixBlockAndNonBlockBad) {
8271 std::string spirv = R"(
8272 OpCapability Shader
8273 OpCapability WorkgroupMemoryExplicitLayoutKHR
8274 OpExtension "SPV_KHR_workgroup_memory_explicit_layout"
8275 OpMemoryModel Logical GLSL450
8276 OpEntryPoint GLCompute %main "main" %_ %b
8277 OpExecutionMode %main LocalSize 8 1 1
8278 OpMemberDecorate %first 0 Offset 0
8279 OpDecorate %first Block
8280 OpDecorate %_ Aliased
8281 OpDecorate %b Aliased
8282 %void = OpTypeVoid
8283 %3 = OpTypeFunction %void
8284 %int = OpTypeInt 32 1
8285 %first = OpTypeStruct %int
8286 %_ptr_Workgroup_first = OpTypePointer Workgroup %first
8287 %_ = OpVariable %_ptr_Workgroup_first Workgroup
8288 %int_0 = OpConstant %int 0
8289 %int_2 = OpConstant %int 2
8290 %_ptr_Workgroup_int = OpTypePointer Workgroup %int
8291 %b = OpVariable %_ptr_Workgroup_int Workgroup
8292 %int_3 = OpConstant %int 3
8293 %main = OpFunction %void None %3
8294 %5 = OpLabel
8295 %13 = OpAccessChain %_ptr_Workgroup_int %_ %int_0
8296 OpStore %13 %int_2
8297 OpStore %b %int_3
8298 OpReturn
8299 OpFunctionEnd
8300 )";
8301
8302 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_4);
8303 EXPECT_EQ(SPV_ERROR_INVALID_BINARY,
8304 ValidateAndRetrieveValidationState(SPV_ENV_UNIVERSAL_1_4));
8305 EXPECT_THAT(
8306 getDiagnosticString(),
8307 HasSubstr("either all or none of the Workgroup Storage Class variables "
8308 "in the entry point interface must point to struct types "
8309 "decorated with Block"));
8310 }
8311
8312 TEST_F(ValidateDecorations, WorkgroupMultiBlockVariableMissingAliased) {
8313 std::string spirv = R"(
8314 OpCapability Shader
8315 OpCapability WorkgroupMemoryExplicitLayoutKHR
8316 OpExtension "SPV_KHR_workgroup_memory_explicit_layout"
8317 OpMemoryModel Logical GLSL450
8318 OpEntryPoint GLCompute %main "main" %_ %__0
8319 OpExecutionMode %main LocalSize 8 1 1
8320 OpMemberDecorate %first 0 Offset 0
8321 OpDecorate %first Block
8322 OpMemberDecorate %second 0 Offset 0
8323 OpDecorate %second Block
8324 OpDecorate %_ Aliased
8325 %void = OpTypeVoid
8326 %3 = OpTypeFunction %void
8327 %int = OpTypeInt 32 1
8328 %first = OpTypeStruct %int
8329 %_ptr_Workgroup_first = OpTypePointer Workgroup %first
8330 %_ = OpVariable %_ptr_Workgroup_first Workgroup
8331 %int_0 = OpConstant %int 0
8332 %int_2 = OpConstant %int 2
8333 %_ptr_Workgroup_int = OpTypePointer Workgroup %int
8334 %second = OpTypeStruct %int
8335 %_ptr_Workgroup_second = OpTypePointer Workgroup %second
8336 %__0 = OpVariable %_ptr_Workgroup_second Workgroup
8337 %int_3 = OpConstant %int 3
8338 %main = OpFunction %void None %3
8339 %5 = OpLabel
8340 %13 = OpAccessChain %_ptr_Workgroup_int %_ %int_0
8341 OpStore %13 %int_2
8342 %18 = OpAccessChain %_ptr_Workgroup_int %__0 %int_0
8343 OpStore %18 %int_3
8344 OpReturn
8345 OpFunctionEnd
8346 )";
8347
8348 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_4);
8349 EXPECT_EQ(SPV_ERROR_INVALID_BINARY,
8350 ValidateAndRetrieveValidationState(SPV_ENV_UNIVERSAL_1_4));
8351 EXPECT_THAT(
8352 getDiagnosticString(),
8353 HasSubstr("more than one Workgroup Storage Class variable in the "
8354 "entry point interface point to a type decorated with Block, "
8355 "all of them must be decorated with Aliased"));
8356 }
8357
8358 TEST_F(ValidateDecorations, WorkgroupSingleBlockVariableNotAStruct) {
8359 std::string spirv = R"(
8360 OpCapability Shader
8361 OpCapability WorkgroupMemoryExplicitLayoutKHR
8362 OpExtension "SPV_KHR_workgroup_memory_explicit_layout"
8363 OpMemoryModel Logical GLSL450
8364 OpEntryPoint GLCompute %main "main" %_
8365 OpExecutionMode %main LocalSize 8 1 1
8366 OpDecorate %first Block
8367 %void = OpTypeVoid
8368 %3 = OpTypeFunction %void
8369 %int = OpTypeInt 32 1
8370 %int_3 = OpConstant %int 3
8371 %first = OpTypeArray %int %int_3
8372 %_ptr_Workgroup_first = OpTypePointer Workgroup %first
8373 %_ = OpVariable %_ptr_Workgroup_first Workgroup
8374 %int_0 = OpConstant %int 0
8375 %int_2 = OpConstant %int 2
8376 %_ptr_Workgroup_int = OpTypePointer Workgroup %int
8377 %main = OpFunction %void None %3
8378 %5 = OpLabel
8379 %13 = OpAccessChain %_ptr_Workgroup_int %_ %int_0
8380 OpStore %13 %int_2
8381 OpReturn
8382 OpFunctionEnd
8383 )";
8384
8385 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_4);
8386 EXPECT_EQ(SPV_ERROR_INVALID_ID,
8387 ValidateAndRetrieveValidationState(SPV_ENV_UNIVERSAL_1_4));
8388 EXPECT_THAT(getDiagnosticString(), HasSubstr("must be a structure type"));
8389 }
8390
8391 TEST_F(ValidateDecorations, WorkgroupSingleBlockVariableMissingLayout) {
8392 std::string spirv = R"(
8393 OpCapability Shader
8394 OpCapability WorkgroupMemoryExplicitLayoutKHR
8395 OpExtension "SPV_KHR_workgroup_memory_explicit_layout"
8396 OpMemoryModel Logical GLSL450
8397 OpEntryPoint GLCompute %main "main" %_
8398 OpExecutionMode %main LocalSize 8 1 1
8399 OpDecorate %first Block
8400 %void = OpTypeVoid
8401 %3 = OpTypeFunction %void
8402 %int = OpTypeInt 32 1
8403 %first = OpTypeStruct %int
8404 %_ptr_Workgroup_first = OpTypePointer Workgroup %first
8405 %_ = OpVariable %_ptr_Workgroup_first Workgroup
8406 %int_0 = OpConstant %int 0
8407 %int_2 = OpConstant %int 2
8408 %_ptr_Workgroup_int = OpTypePointer Workgroup %int
8409 %main = OpFunction %void None %3
8410 %5 = OpLabel
8411 %13 = OpAccessChain %_ptr_Workgroup_int %_ %int_0
8412 OpStore %13 %int_2
8413 OpReturn
8414 OpFunctionEnd
8415 )";
8416
8417 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_4);
8418 EXPECT_EQ(SPV_ERROR_INVALID_ID,
8419 ValidateAndRetrieveValidationState(SPV_ENV_VULKAN_1_1_SPIRV_1_4));
8420 EXPECT_THAT(
8421 getDiagnosticString(),
8422 HasSubstr("Block must be explicitly laid out with Offset decorations"));
8423 }
8424
8425 TEST_F(ValidateDecorations, WorkgroupSingleBlockVariableBadLayout) {
8426 std::string spirv = R"(
8427 OpCapability Shader
8428 OpCapability WorkgroupMemoryExplicitLayoutKHR
8429 OpExtension "SPV_KHR_workgroup_memory_explicit_layout"
8430 OpMemoryModel Logical GLSL450
8431 OpEntryPoint GLCompute %main "main" %_
8432 OpExecutionMode %main LocalSize 8 1 1
8433 OpMemberDecorate %first 0 Offset 1
8434 OpDecorate %first Block
8435 %void = OpTypeVoid
8436 %3 = OpTypeFunction %void
8437 %int = OpTypeInt 32 1
8438 %first = OpTypeStruct %int
8439 %_ptr_Workgroup_first = OpTypePointer Workgroup %first
8440 %_ = OpVariable %_ptr_Workgroup_first Workgroup
8441 %int_0 = OpConstant %int 0
8442 %int_2 = OpConstant %int 2
8443 %_ptr_Workgroup_int = OpTypePointer Workgroup %int
8444 %main = OpFunction %void None %3
8445 %5 = OpLabel
8446 %13 = OpAccessChain %_ptr_Workgroup_int %_ %int_0
8447 OpStore %13 %int_2
8448 OpReturn
8449 OpFunctionEnd
8450 )";
8451
8452 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_4);
8453 EXPECT_EQ(SPV_ERROR_INVALID_ID,
8454 ValidateAndRetrieveValidationState(SPV_ENV_VULKAN_1_1_SPIRV_1_4));
8455 EXPECT_THAT(
8456 getDiagnosticString(),
8457 HasSubstr("Block for variable in Workgroup storage class must follow "
8458 "relaxed storage buffer layout rules: "
8459 "member 0 at offset 1 is not aligned to 4"));
8460 }
8461
8462 TEST_F(ValidateDecorations, WorkgroupBlockNoCapability) {
8463 std::string spirv = R"(
8464 OpCapability Shader
8465 OpMemoryModel Logical GLSL450
8466 OpEntryPoint GLCompute %main "main" %_
8467 OpExecutionMode %main LocalSize 1 1 1
8468 OpMemberDecorate %struct 0 Offset 0
8469 OpMemberDecorate %struct 1 Offset 4
8470 OpDecorate %struct Block
8471 %void = OpTypeVoid
8472 %3 = OpTypeFunction %void
8473 %int = OpTypeInt 32 1
8474 %struct = OpTypeStruct %int %int
8475 %ptr_workgroup = OpTypePointer Workgroup %struct
8476 %_ = OpVariable %ptr_workgroup Workgroup
8477 %main = OpFunction %void None %3
8478 %5 = OpLabel
8479 OpReturn
8480 OpFunctionEnd
8481 )";
8482
8483 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_4);
8484 EXPECT_EQ(SPV_ERROR_INVALID_BINARY,
8485 ValidateAndRetrieveValidationState(SPV_ENV_VULKAN_1_1_SPIRV_1_4));
8486 EXPECT_THAT(
8487 getDiagnosticString(),
8488 HasSubstr(
8489 "Workgroup Storage Class variables can't be decorated with Block "
8490 "unless declaring the WorkgroupMemoryExplicitLayoutKHR capability"));
8491 }
8492
8493 TEST_F(ValidateDecorations, BadMatrixStrideUniform) {
8494 const std::string spirv = R"(
8495 OpCapability Shader
8496 OpMemoryModel Logical GLSL450
8497 OpEntryPoint GLCompute %main "main"
8498 OpExecutionMode %main LocalSize 1 1 1
8499 OpDecorate %block Block
8500 OpMemberDecorate %block 0 Offset 0
8501 OpMemberDecorate %block 0 MatrixStride 3
8502 OpMemberDecorate %block 0 ColMajor
8503 OpDecorate %var DescriptorSet 0
8504 OpDecorate %var Binding 0
8505 %void = OpTypeVoid
8506 %float = OpTypeFloat 32
8507 %float4 = OpTypeVector %float 4
8508 %matrix4x4 = OpTypeMatrix %float4 4
8509 %block = OpTypeStruct %matrix4x4
8510 %block_ptr = OpTypePointer Uniform %block
8511 %var = OpVariable %block_ptr Uniform
8512 %void_fn = OpTypeFunction %void
8513 %main = OpFunction %void None %void_fn
8514 %entry = OpLabel
8515 OpReturn
8516 OpFunctionEnd
8517 )";
8518
8519 CompileSuccessfully(spirv);
8520 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_0));
8521 EXPECT_THAT(
8522 getDiagnosticString(),
8523 HasSubstr(
8524 "Structure id 2 decorated as Block for variable in Uniform storage "
8525 "class must follow standard uniform buffer layout rules: member 0 is "
8526 "a matrix with stride 3 not satisfying alignment to 16"));
8527 }
8528
8529 TEST_F(ValidateDecorations, BadMatrixStrideStorageBuffer) {
8530 const std::string spirv = R"(
8531 OpCapability Shader
8532 OpExtension "SPV_KHR_storage_buffer_storage_class"
8533 OpMemoryModel Logical GLSL450
8534 OpEntryPoint GLCompute %main "main"
8535 OpExecutionMode %main LocalSize 1 1 1
8536 OpDecorate %block Block
8537 OpMemberDecorate %block 0 Offset 0
8538 OpMemberDecorate %block 0 MatrixStride 3
8539 OpMemberDecorate %block 0 ColMajor
8540 OpDecorate %var DescriptorSet 0
8541 OpDecorate %var Binding 0
8542 %void = OpTypeVoid
8543 %float = OpTypeFloat 32
8544 %float4 = OpTypeVector %float 4
8545 %matrix4x4 = OpTypeMatrix %float4 4
8546 %block = OpTypeStruct %matrix4x4
8547 %block_ptr = OpTypePointer StorageBuffer %block
8548 %var = OpVariable %block_ptr StorageBuffer
8549 %void_fn = OpTypeFunction %void
8550 %main = OpFunction %void None %void_fn
8551 %entry = OpLabel
8552 OpReturn
8553 OpFunctionEnd
8554 )";
8555
8556 CompileSuccessfully(spirv);
8557 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_0));
8558 EXPECT_THAT(
8559 getDiagnosticString(),
8560 HasSubstr(
8561 "Structure id 2 decorated as Block for variable in StorageBuffer "
8562 "storage class must follow standard storage buffer layout rules: "
8563 "member 0 is a matrix with stride 3 not satisfying alignment to 16"));
8564 }
8565
8566 TEST_F(ValidateDecorations, BadMatrixStridePushConstant) {
8567 const std::string spirv = R"(
8568 OpCapability Shader
8569 OpMemoryModel Logical GLSL450
8570 OpEntryPoint GLCompute %main "main"
8571 OpExecutionMode %main LocalSize 1 1 1
8572 OpDecorate %block Block
8573 OpMemberDecorate %block 0 Offset 0
8574 OpMemberDecorate %block 0 MatrixStride 3
8575 OpMemberDecorate %block 0 ColMajor
8576 %void = OpTypeVoid
8577 %float = OpTypeFloat 32
8578 %float4 = OpTypeVector %float 4
8579 %matrix4x4 = OpTypeMatrix %float4 4
8580 %block = OpTypeStruct %matrix4x4
8581 %block_ptr = OpTypePointer PushConstant %block
8582 %var = OpVariable %block_ptr PushConstant
8583 %void_fn = OpTypeFunction %void
8584 %main = OpFunction %void None %void_fn
8585 %entry = OpLabel
8586 OpReturn
8587 OpFunctionEnd
8588 )";
8589
8590 CompileSuccessfully(spirv);
8591 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_0));
8592 EXPECT_THAT(
8593 getDiagnosticString(),
8594 HasSubstr(
8595 "Structure id 2 decorated as Block for variable in PushConstant "
8596 "storage class must follow standard storage buffer layout rules: "
8597 "member 0 is a matrix with stride 3 not satisfying alignment to 16"));
8598 }
8599
8600 TEST_F(ValidateDecorations, BadMatrixStrideStorageBufferScalarLayout) {
8601 const std::string spirv = R"(
8602 OpCapability Shader
8603 OpExtension "SPV_KHR_storage_buffer_storage_class"
8604 OpMemoryModel Logical GLSL450
8605 OpEntryPoint GLCompute %main "main"
8606 OpExecutionMode %main LocalSize 1 1 1
8607 OpDecorate %block Block
8608 OpMemberDecorate %block 0 Offset 0
8609 OpMemberDecorate %block 0 MatrixStride 3
8610 OpMemberDecorate %block 0 RowMajor
8611 OpDecorate %var DescriptorSet 0
8612 OpDecorate %var Binding 0
8613 %void = OpTypeVoid
8614 %float = OpTypeFloat 32
8615 %float4 = OpTypeVector %float 4
8616 %matrix4x4 = OpTypeMatrix %float4 4
8617 %block = OpTypeStruct %matrix4x4
8618 %block_ptr = OpTypePointer StorageBuffer %block
8619 %var = OpVariable %block_ptr StorageBuffer
8620 %void_fn = OpTypeFunction %void
8621 %main = OpFunction %void None %void_fn
8622 %entry = OpLabel
8623 OpReturn
8624 OpFunctionEnd
8625 )";
8626
8627 options_->scalar_block_layout = true;
8628 CompileSuccessfully(spirv);
8629 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_0));
8630 EXPECT_THAT(
8631 getDiagnosticString(),
8632 HasSubstr(
8633 "Structure id 2 decorated as Block for variable in StorageBuffer "
8634 "storage class must follow scalar storage buffer layout rules: "
8635 "member 0 is a matrix with stride 3 not satisfying alignment to 4"));
8636 }
8637
8638 TEST_F(ValidateDecorations, MissingOffsetStructNestedInArray) {
8639 const std::string spirv = R"(
8640 OpCapability Shader
8641 OpExtension "SPV_KHR_storage_buffer_storage_class"
8642 OpMemoryModel Logical GLSL450
8643 OpEntryPoint GLCompute %main "main"
8644 OpExecutionMode %main LocalSize 1 1 1
8645 OpDecorate %array ArrayStride 4
8646 OpDecorate %outer Block
8647 OpMemberDecorate %outer 0 Offset 0
8648 OpDecorate %var DescriptorSet 0
8649 OpDecorate %var Binding 0
8650 %void = OpTypeVoid
8651 %int = OpTypeInt 32 0
8652 %int_4 = OpConstant %int 4
8653 %inner = OpTypeStruct %int
8654 %array = OpTypeArray %inner %int_4
8655 %outer = OpTypeStruct %array
8656 %ptr_ssbo_outer = OpTypePointer StorageBuffer %outer
8657 %var = OpVariable %ptr_ssbo_outer StorageBuffer
8658 %void_fn = OpTypeFunction %void
8659 %main = OpFunction %void None %void_fn
8660 %entry = OpLabel
8661 OpReturn
8662 OpFunctionEnd
8663 )";
8664
8665 CompileSuccessfully(spirv);
8666 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
8667 EXPECT_THAT(getDiagnosticString(),
8668 HasSubstr("Structure id 3 decorated as Block must be explicitly "
8669 "laid out with Offset decorations"));
8670 }
8671
8672 TEST_F(ValidateDecorations, AllOnesOffset) {
8673 const std::string spirv = R"(
8674 OpCapability Shader
8675 OpMemoryModel Logical GLSL450
8676 OpEntryPoint GLCompute %main "main"
8677 OpDecorate %var DescriptorSet 0
8678 OpDecorate %var Binding 0
8679 OpDecorate %outer Block
8680 OpMemberDecorate %outer 0 Offset 0
8681 OpMemberDecorate %struct 0 Offset 4294967295
8682 %void = OpTypeVoid
8683 %int = OpTypeInt 32 0
8684 %struct = OpTypeStruct %int
8685 %outer = OpTypeStruct %struct
8686 %ptr = OpTypePointer Uniform %outer
8687 %var = OpVariable %ptr Uniform
8688 %void_fn = OpTypeFunction %void
8689 %main = OpFunction %void None %void_fn
8690 %entry = OpLabel
8691 OpReturn
8692 OpFunctionEnd
8693 )";
8694
8695 CompileSuccessfully(spirv);
8696 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
8697 EXPECT_THAT(getDiagnosticString(),
8698 HasSubstr("decorated as Block must be explicitly laid out with "
8699 "Offset decorations"));
8700 }
8701
8702 TEST_F(ValidateDecorations, PerVertexVulkanGood) {
8703 const std::string spirv = R"(
8704 OpCapability Shader
8705 OpCapability FragmentBarycentricKHR
8706 OpExtension "SPV_KHR_fragment_shader_barycentric"
8707 %1 = OpExtInstImport "GLSL.std.450"
8708 OpMemoryModel Logical GLSL450
8709 OpEntryPoint Fragment %main "main" %vertexIDs
8710 OpExecutionMode %main OriginUpperLeft
8711 OpDecorate %vertexIDs Location 0
8712 OpDecorate %vertexIDs PerVertexKHR
8713 %void = OpTypeVoid
8714 %func = OpTypeFunction %void
8715 %float = OpTypeFloat 32
8716 %uint = OpTypeInt 32 0
8717 %ptrFloat = OpTypePointer Input %float
8718 %uint_3 = OpConstant %uint 3
8719 %floatArray = OpTypeArray %float %uint_3
8720 %ptrFloatArray = OpTypePointer Input %floatArray
8721 %vertexIDs = OpVariable %ptrFloatArray Input
8722 %int = OpTypeInt 32 1
8723 %int_0 = OpConstant %int 0
8724 %main = OpFunction %void None %func
8725 %label = OpLabel
8726 %access = OpAccessChain %ptrFloat %vertexIDs %int_0
8727 %load = OpLoad %float %access
8728 OpReturn
8729 OpFunctionEnd
8730 )";
8731
8732 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_0);
8733 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_VULKAN_1_0));
8734 }
8735
8736 TEST_F(ValidateDecorations, PerVertexVulkanOutput) {
8737 const std::string spirv = R"(
8738 OpCapability Shader
8739 OpCapability FragmentBarycentricKHR
8740 OpExtension "SPV_KHR_fragment_shader_barycentric"
8741 %1 = OpExtInstImport "GLSL.std.450"
8742 OpMemoryModel Logical GLSL450
8743 OpEntryPoint Fragment %main "main" %vertexIDs
8744 OpExecutionMode %main OriginUpperLeft
8745 OpDecorate %vertexIDs Location 0
8746 OpDecorate %vertexIDs PerVertexKHR
8747 %void = OpTypeVoid
8748 %func = OpTypeFunction %void
8749 %float = OpTypeFloat 32
8750 %uint = OpTypeInt 32 0
8751 %ptrFloat = OpTypePointer Output %float
8752 %uint_3 = OpConstant %uint 3
8753 %floatArray = OpTypeArray %float %uint_3
8754 %ptrFloatArray = OpTypePointer Output %floatArray
8755 %vertexIDs = OpVariable %ptrFloatArray Output
8756 %int = OpTypeInt 32 1
8757 %int_0 = OpConstant %int 0
8758 %main = OpFunction %void None %func
8759 %label = OpLabel
8760 %access = OpAccessChain %ptrFloat %vertexIDs %int_0
8761 %load = OpLoad %float %access
8762 OpReturn
8763 OpFunctionEnd
8764 )";
8765
8766 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_0);
8767 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_0));
8768 EXPECT_THAT(getDiagnosticString(),
8769 AnyVUID("VUID-StandaloneSpirv-PerVertexKHR-06777"));
8770 EXPECT_THAT(getDiagnosticString(), HasSubstr("storage class must be Input"));
8771 }
8772
8773 TEST_F(ValidateDecorations, PerVertexVulkanNonFragment) {
8774 const std::string spirv = R"(
8775 OpCapability Shader
8776 OpCapability FragmentBarycentricKHR
8777 OpExtension "SPV_KHR_fragment_shader_barycentric"
8778 %1 = OpExtInstImport "GLSL.std.450"
8779 OpMemoryModel Logical GLSL450
8780 OpEntryPoint Vertex %main "main" %vertexIDs
8781 OpDecorate %vertexIDs Location 0
8782 OpDecorate %vertexIDs PerVertexKHR
8783 %void = OpTypeVoid
8784 %func = OpTypeFunction %void
8785 %float = OpTypeFloat 32
8786 %uint = OpTypeInt 32 0
8787 %ptrFloat = OpTypePointer Input %float
8788 %uint_3 = OpConstant %uint 3
8789 %floatArray = OpTypeArray %float %uint_3
8790 %ptrFloatArray = OpTypePointer Input %floatArray
8791 %vertexIDs = OpVariable %ptrFloatArray Input
8792 %int = OpTypeInt 32 1
8793 %int_0 = OpConstant %int 0
8794 %main = OpFunction %void None %func
8795 %label = OpLabel
8796 %access = OpAccessChain %ptrFloat %vertexIDs %int_0
8797 %load = OpLoad %float %access
8798 OpReturn
8799 OpFunctionEnd
8800 )";
8801
8802 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_0);
8803 EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPV_ENV_VULKAN_1_0));
8804 EXPECT_THAT(getDiagnosticString(),
8805 AnyVUID("VUID-StandaloneSpirv-PerVertexKHR-06777"));
8806 EXPECT_THAT(
8807 getDiagnosticString(),
8808 HasSubstr(
8809 "PerVertexKHR can only be applied to Fragment Execution Models"));
8810 }
8811
8812 TEST_F(ValidateDecorations, PerVertexVulkanNonArray) {
8813 const std::string spirv = R"(
8814 OpCapability Shader
8815 OpCapability FragmentBarycentricKHR
8816 OpExtension "SPV_KHR_fragment_shader_barycentric"
8817 %1 = OpExtInstImport "GLSL.std.450"
8818 OpMemoryModel Logical GLSL450
8819 OpEntryPoint Fragment %main "main" %vertexIDs
8820 OpExecutionMode %main OriginUpperLeft
8821 OpDecorate %vertexIDs Location 0
8822 OpDecorate %vertexIDs PerVertexKHR
8823 %void = OpTypeVoid
8824 %func = OpTypeFunction %void
8825 %float = OpTypeFloat 32
8826 %ptrFloat = OpTypePointer Input %float
8827 %vertexIDs = OpVariable %ptrFloat Input
8828 %int = OpTypeInt 32 1
8829 %int_0 = OpConstant %int 0
8830 %main = OpFunction %void None %func
8831 %label = OpLabel
8832 %load = OpLoad %float %vertexIDs
8833 OpReturn
8834 OpFunctionEnd
8835 )";
8836
8837 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_0);
8838 EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPV_ENV_VULKAN_1_0));
8839 EXPECT_THAT(getDiagnosticString(),
8840 AnyVUID("VUID-StandaloneSpirv-Input-06778"));
8841 EXPECT_THAT(getDiagnosticString(),
8842 HasSubstr("PerVertexKHR must be declared as arrays"));
8843 }
8844
8845 TEST_F(ValidateDecorations, RelaxedPrecisionDecorationOnNumericTypeBad) {
8846 const spv_target_env env = SPV_ENV_VULKAN_1_0;
8847 std::string spirv = R"(
8848 OpCapability Shader
8849 OpMemoryModel Logical GLSL450
8850 OpEntryPoint Fragment %main "main"
8851 OpExecutionMode %main OriginUpperLeft
8852 OpDecorate %float RelaxedPrecision
8853 %void = OpTypeVoid
8854 %voidfn = OpTypeFunction %void
8855 %float = OpTypeFloat 32
8856 %main = OpFunction %void None %voidfn
8857 %label = OpLabel
8858 OpReturn
8859 OpFunctionEnd
8860 )";
8861
8862 CompileSuccessfully(spirv, env);
8863 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateAndRetrieveValidationState(env));
8864 EXPECT_THAT(
8865 getDiagnosticString(),
8866 HasSubstr("RelaxPrecision decoration cannot be applied to a type"));
8867 }
8868
8869 TEST_F(ValidateDecorations, RelaxedPrecisionDecorationOnStructMember) {
8870 const spv_target_env env = SPV_ENV_VULKAN_1_0;
8871 std::string spirv = R"(
8872 OpCapability Shader
8873 OpMemoryModel Logical GLSL450
8874 OpEntryPoint Fragment %main "main"
8875 OpExecutionMode %main OriginUpperLeft
8876 OpMemberDecorate %struct 0 RelaxedPrecision
8877 %void = OpTypeVoid
8878 %voidfn = OpTypeFunction %void
8879 %float = OpTypeFloat 32
8880 %struct = OpTypeStruct %float
8881 %main = OpFunction %void None %voidfn
8882 %label = OpLabel
8883 OpReturn
8884 OpFunctionEnd
8885 )";
8886
8887 CompileSuccessfully(spirv, env);
8888 EXPECT_EQ(SPV_SUCCESS, ValidateAndRetrieveValidationState(env));
8889 }
8890
8891 TEST_F(ValidateDecorations, VulkanFlatMultipleInterfaceGood) {
8892 std::string spirv = R"(
8893 OpCapability Shader
8894 OpCapability Geometry
8895 %1 = OpExtInstImport "GLSL.std.450"
8896 OpMemoryModel Logical GLSL450
8897 OpEntryPoint Fragment %main "main" %layer %gl_Layer
8898 OpExecutionMode %main OriginUpperLeft
8899 OpSource GLSL 450
8900 OpDecorate %layer Location 0
8901 OpDecorate %gl_Layer Flat
8902 OpDecorate %gl_Layer BuiltIn Layer
8903 %void = OpTypeVoid
8904 %3 = OpTypeFunction %void
8905 %int = OpTypeInt 32 1
8906 %_ptr_Output_int = OpTypePointer Output %int
8907 %layer = OpVariable %_ptr_Output_int Output
8908 %_ptr_Input_int = OpTypePointer Input %int
8909 %gl_Layer = OpVariable %_ptr_Input_int Input
8910 %main = OpFunction %void None %3
8911 %5 = OpLabel
8912 %11 = OpLoad %int %gl_Layer
8913 OpStore %layer %11
8914 OpReturn
8915 OpFunctionEnd
8916 )";
8917
8918 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_0);
8919 EXPECT_EQ(SPV_SUCCESS,
8920 ValidateAndRetrieveValidationState(SPV_ENV_VULKAN_1_0));
8921 }
8922
8923 TEST_F(ValidateDecorations, VulkanFlatMultipleInterfaceBad) {
8924 std::string spirv = R"(
8925 OpCapability Shader
8926 OpCapability Geometry
8927 %1 = OpExtInstImport "GLSL.std.450"
8928 OpMemoryModel Logical GLSL450
8929 OpEntryPoint Fragment %main "main" %layer %gl_Layer
8930 OpExecutionMode %main OriginUpperLeft
8931 OpSource GLSL 450
8932 OpDecorate %layer Location 0
8933 OpDecorate %gl_Layer BuiltIn Layer
8934 %void = OpTypeVoid
8935 %3 = OpTypeFunction %void
8936 %int = OpTypeInt 32 1
8937 %_ptr_Output_int = OpTypePointer Output %int
8938 %layer = OpVariable %_ptr_Output_int Output
8939 %_ptr_Input_int = OpTypePointer Input %int
8940 %gl_Layer = OpVariable %_ptr_Input_int Input
8941 %main = OpFunction %void None %3
8942 %5 = OpLabel
8943 %11 = OpLoad %int %gl_Layer
8944 OpStore %layer %11
8945 OpReturn
8946 OpFunctionEnd
8947 )";
8948
8949 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_0);
8950 EXPECT_EQ(SPV_ERROR_INVALID_ID,
8951 ValidateAndRetrieveValidationState(SPV_ENV_VULKAN_1_0));
8952 EXPECT_THAT(getDiagnosticString(),
8953 AnyVUID("VUID-StandaloneSpirv-Flat-04744"));
8954 EXPECT_THAT(
8955 getDiagnosticString(),
8956 HasSubstr(
8957 "Fragment OpEntryPoint operand 4 with Input interfaces with integer "
8958 "or float type must have a Flat decoration for Entry Point id 2."));
8959 }
8960
8961 TEST_F(ValidateDecorations, VulkanNoFlatFloat32) {
8962 std::string spirv = R"(
8963 OpCapability Shader
8964 %1 = OpExtInstImport "GLSL.std.450"
8965 OpMemoryModel Logical GLSL450
8966 OpEntryPoint Fragment %main "main" %in
8967 OpExecutionMode %main OriginUpperLeft
8968 OpSource GLSL 450
8969 OpDecorate %in Location 0
8970 %void = OpTypeVoid
8971 %3 = OpTypeFunction %void
8972 %float = OpTypeFloat 32
8973 %_ptr_Function_float = OpTypePointer Function %float
8974 %_ptr_Input_float = OpTypePointer Input %float
8975 %in = OpVariable %_ptr_Input_float Input
8976 %main = OpFunction %void None %3
8977 %5 = OpLabel
8978 %b = OpVariable %_ptr_Function_float Function
8979 %11 = OpLoad %float %in
8980 OpStore %b %11
8981 OpReturn
8982 OpFunctionEnd
8983 )";
8984
8985 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_0);
8986 EXPECT_EQ(SPV_SUCCESS,
8987 ValidateAndRetrieveValidationState(SPV_ENV_VULKAN_1_0));
8988 }
8989
8990 TEST_F(ValidateDecorations, VulkanNoFlatFloat64) {
8991 std::string spirv = R"(
8992 OpCapability Shader
8993 OpCapability Float64
8994 %1 = OpExtInstImport "GLSL.std.450"
8995 OpMemoryModel Logical GLSL450
8996 OpEntryPoint Fragment %main "main" %in
8997 OpExecutionMode %main OriginUpperLeft
8998 OpSource GLSL 450
8999 OpDecorate %in Location 0
9000 %void = OpTypeVoid
9001 %3 = OpTypeFunction %void
9002 %double = OpTypeFloat 64
9003 %_ptr_Function_double = OpTypePointer Function %double
9004 %_ptr_Input_double = OpTypePointer Input %double
9005 %in = OpVariable %_ptr_Input_double Input
9006 %main = OpFunction %void None %3
9007 %5 = OpLabel
9008 %b = OpVariable %_ptr_Function_double Function
9009 %11 = OpLoad %double %in
9010 OpStore %b %11
9011 OpReturn
9012 OpFunctionEnd
9013 )";
9014
9015 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_0);
9016 EXPECT_EQ(SPV_ERROR_INVALID_ID,
9017 ValidateAndRetrieveValidationState(SPV_ENV_VULKAN_1_0));
9018 EXPECT_THAT(getDiagnosticString(),
9019 AnyVUID("VUID-StandaloneSpirv-Flat-04744"));
9020 EXPECT_THAT(
9021 getDiagnosticString(),
9022 HasSubstr(
9023 "Fragment OpEntryPoint operand 3 with Input interfaces with integer "
9024 "or float type must have a Flat decoration for Entry Point id 2."));
9025 }
9026
9027 TEST_F(ValidateDecorations, VulkanNoFlatVectorFloat64) {
9028 std::string spirv = R"(
9029 OpCapability Shader
9030 OpCapability Float64
9031 %1 = OpExtInstImport "GLSL.std.450"
9032 OpMemoryModel Logical GLSL450
9033 OpEntryPoint Fragment %main "main" %in
9034 OpExecutionMode %main OriginUpperLeft
9035 OpSource GLSL 450
9036 OpDecorate %in Location 0
9037 %void = OpTypeVoid
9038 %3 = OpTypeFunction %void
9039 %double = OpTypeFloat 64
9040 %v2double = OpTypeVector %double 2
9041 %_ptr_Function_v2double = OpTypePointer Function %v2double
9042 %_ptr_Input_v2double = OpTypePointer Input %v2double
9043 %in = OpVariable %_ptr_Input_v2double Input
9044 %main = OpFunction %void None %3
9045 %5 = OpLabel
9046 %b = OpVariable %_ptr_Function_v2double Function
9047 %11 = OpLoad %v2double %in
9048 OpStore %b %11
9049 OpReturn
9050 OpFunctionEnd
9051 )";
9052
9053 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_0);
9054 EXPECT_EQ(SPV_SUCCESS,
9055 ValidateAndRetrieveValidationState(SPV_ENV_VULKAN_1_0));
9056 }
9057
9058 TEST_F(ValidateDecorations, VulkanNoFlatIntVector) {
9059 std::string spirv = R"(
9060 OpCapability Shader
9061 %1 = OpExtInstImport "GLSL.std.450"
9062 OpMemoryModel Logical GLSL450
9063 OpEntryPoint Fragment %main "main" %in
9064 OpExecutionMode %main OriginUpperLeft
9065 OpSource GLSL 450
9066 OpDecorate %in Location 0
9067 %void = OpTypeVoid
9068 %3 = OpTypeFunction %void
9069 %int = OpTypeInt 32 1
9070 %v2int = OpTypeVector %int 2
9071 %_ptr_Function_v2int = OpTypePointer Function %v2int
9072 %_ptr_Input_v2int = OpTypePointer Input %v2int
9073 %in = OpVariable %_ptr_Input_v2int Input
9074 %main = OpFunction %void None %3
9075 %5 = OpLabel
9076 %b = OpVariable %_ptr_Function_v2int Function
9077 %12 = OpLoad %v2int %in
9078 OpStore %b %12
9079 OpReturn
9080 OpFunctionEnd
9081 )";
9082
9083 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_0);
9084 EXPECT_EQ(SPV_ERROR_INVALID_ID,
9085 ValidateAndRetrieveValidationState(SPV_ENV_VULKAN_1_0));
9086 EXPECT_THAT(getDiagnosticString(),
9087 AnyVUID("VUID-StandaloneSpirv-Flat-04744"));
9088 EXPECT_THAT(
9089 getDiagnosticString(),
9090 HasSubstr(
9091 "Fragment OpEntryPoint operand 3 with Input interfaces with integer "
9092 "or float type must have a Flat decoration for Entry Point id 2."));
9093 }
9094
9095 TEST_P(ValidateDecorationString, VulkanOutputInvalidInterface) {
9096 const std::string decoration = GetParam();
9097 std::stringstream ss;
9098 ss << R"(
9099 OpCapability Shader
9100 OpCapability SampleRateShading
9101 %1 = OpExtInstImport "GLSL.std.450"
9102 OpMemoryModel Logical GLSL450
9103 OpEntryPoint Fragment %main "main" %out
9104 OpExecutionMode %main OriginUpperLeft
9105 OpSource GLSL 450
9106 OpDecorate %out )"
9107 << decoration << R"(
9108 OpDecorate %out Location 0
9109 %void = OpTypeVoid
9110 %3 = OpTypeFunction %void
9111 %int = OpTypeInt 32 1
9112 %_ptr_Output_int = OpTypePointer Output %int
9113 %out = OpVariable %_ptr_Output_int Output
9114 %int_1 = OpConstant %int 1
9115 %main = OpFunction %void None %3
9116 %5 = OpLabel
9117 OpStore %out %int_1
9118 OpReturn
9119 OpFunctionEnd
9120 )";
9121
9122 CompileSuccessfully(ss.str(), SPV_ENV_VULKAN_1_0);
9123 ASSERT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_0));
9124 EXPECT_THAT(getDiagnosticString(),
9125 AnyVUID("VUID-StandaloneSpirv-Flat-06201"));
9126 EXPECT_THAT(
9127 getDiagnosticString(),
9128 HasSubstr("decorated variable must not be used in fragment execution "
9129 "model as an Output storage class for Entry Point id 2."));
9130 }
9131
9132 TEST_P(ValidateDecorationString, VulkanVertexInputInvalidInterface) {
9133 const std::string decoration = GetParam();
9134 std::stringstream ss;
9135 ss << R"(
9136 OpCapability Shader
9137 OpCapability SampleRateShading
9138 %1 = OpExtInstImport "GLSL.std.450"
9139 OpMemoryModel Logical GLSL450
9140 OpEntryPoint Vertex %main "main" %out %in
9141 OpSource GLSL 450
9142 OpDecorate %in )"
9143 << decoration << R"(
9144 OpDecorate %out Location 0
9145 OpDecorate %in Location 0
9146 %void = OpTypeVoid
9147 %3 = OpTypeFunction %void
9148 %int = OpTypeInt 32 1
9149 %_ptr_Output_int = OpTypePointer Output %int
9150 %out = OpVariable %_ptr_Output_int Output
9151 %_ptr_Input_int = OpTypePointer Input %int
9152 %in = OpVariable %_ptr_Input_int Input
9153 %main = OpFunction %void None %3
9154 %5 = OpLabel
9155 %11 = OpLoad %int %in
9156 OpStore %out %11
9157 OpReturn
9158 OpFunctionEnd
9159 )";
9160
9161 CompileSuccessfully(ss.str(), SPV_ENV_VULKAN_1_0);
9162 ASSERT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_0));
9163 EXPECT_THAT(getDiagnosticString(),
9164 AnyVUID("VUID-StandaloneSpirv-Flat-06202"));
9165 EXPECT_THAT(
9166 getDiagnosticString(),
9167 HasSubstr("decorated variable must not be used in vertex execution model "
9168 "as an Input storage class for Entry Point id 2."));
9169 }
9170
9171 INSTANTIATE_TEST_SUITE_P(FragmentInputInterface, ValidateDecorationString,
9172 ::testing::Values("Flat", "NoPerspective", "Sample",
9173 "Centroid"));
9174
9175 TEST_F(ValidateDecorations, NVBindlessSamplerArrayInBlock) {
9176 const std::string spirv = R"(
9177 OpCapability Shader
9178 OpCapability BindlessTextureNV
9179 OpExtension "SPV_NV_bindless_texture"
9180 %1 = OpExtInstImport "GLSL.std.450"
9181 OpMemoryModel Logical GLSL450
9182 OpSamplerImageAddressingModeNV 64
9183 OpEntryPoint Fragment %main "main"
9184 OpExecutionMode %main OriginUpperLeft
9185 OpSource GLSL 450
9186 OpName %main "main"
9187 OpName %UBO "UBO"
9188 OpMemberName %UBO 0 "uboSampler"
9189 OpName %_ ""
9190 OpDecorate %array ArrayStride 16
9191 OpMemberDecorate %UBO 0 Offset 0
9192 OpDecorate %UBO Block
9193 OpDecorate %_ DescriptorSet 0
9194 OpDecorate %_ Binding 2
9195 %void = OpTypeVoid
9196 %3 = OpTypeFunction %void
9197 %float = OpTypeFloat 32
9198 %7 = OpTypeImage %float 2D 0 0 0 1 Unknown
9199 %8 = OpTypeSampledImage %7
9200 %uint = OpTypeInt 32 0
9201 %uint_3 = OpConstant %uint 3
9202 %array = OpTypeArray %8 %uint_3
9203 %UBO = OpTypeStruct %array
9204 %pointer = OpTypePointer Uniform %UBO
9205 %_ = OpVariable %pointer Uniform
9206 %main = OpFunction %void None %3
9207 %5 = OpLabel
9208 OpReturn
9209 OpFunctionEnd
9210 )";
9211
9212 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
9213 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
9214 }
9215
9216 TEST_F(ValidateDecorations, Std140ColMajorMat2x2) {
9217 const std::string spirv = R"(
9218 OpCapability Shader
9219 OpMemoryModel Logical GLSL450
9220 OpEntryPoint GLCompute %main "main"
9221 OpExecutionMode %main LocalSize 1 1 1
9222 OpDecorate %block Block
9223 OpMemberDecorate %block 0 Offset 0
9224 OpMemberDecorate %block 0 ColMajor
9225 OpMemberDecorate %block 0 MatrixStride 8
9226 OpDecorate %var DescriptorSet 0
9227 OpDecorate %var Binding 0
9228 %void = OpTypeVoid
9229 %void_fn = OpTypeFunction %void
9230 %float = OpTypeFloat 32
9231 %float2 = OpTypeVector %float 2
9232 %matrix = OpTypeMatrix %float2 2
9233 %block = OpTypeStruct %matrix
9234 %ptr_block = OpTypePointer Uniform %block
9235 %var = OpVariable %ptr_block Uniform
9236 %main = OpFunction %void None %void_fn
9237 %entry = OpLabel
9238 OpReturn
9239 OpFunctionEnd
9240 )";
9241
9242 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_0);
9243 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_0));
9244 EXPECT_THAT(
9245 getDiagnosticString(),
9246 HasSubstr(
9247 "member 0 is a matrix with stride 8 not satisfying alignment to 16"));
9248 }
9249
9250 TEST_F(ValidateDecorations, Std140RowMajorMat2x2) {
9251 const std::string spirv = R"(
9252 OpCapability Shader
9253 OpMemoryModel Logical GLSL450
9254 OpEntryPoint GLCompute %main "main"
9255 OpExecutionMode %main LocalSize 1 1 1
9256 OpDecorate %block Block
9257 OpMemberDecorate %block 0 Offset 0
9258 OpMemberDecorate %block 0 RowMajor
9259 OpMemberDecorate %block 0 MatrixStride 8
9260 OpDecorate %var DescriptorSet 0
9261 OpDecorate %var Binding 0
9262 %void = OpTypeVoid
9263 %void_fn = OpTypeFunction %void
9264 %float = OpTypeFloat 32
9265 %float2 = OpTypeVector %float 2
9266 %matrix = OpTypeMatrix %float2 2
9267 %block = OpTypeStruct %matrix
9268 %ptr_block = OpTypePointer Uniform %block
9269 %var = OpVariable %ptr_block Uniform
9270 %main = OpFunction %void None %void_fn
9271 %entry = OpLabel
9272 OpReturn
9273 OpFunctionEnd
9274 )";
9275
9276 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_0);
9277 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_0));
9278 EXPECT_THAT(
9279 getDiagnosticString(),
9280 HasSubstr(
9281 "member 0 is a matrix with stride 8 not satisfying alignment to 16"));
9282 }
9283
9284 TEST_F(ValidateDecorations, Std140ColMajorMat4x2) {
9285 const std::string spirv = R"(
9286 OpCapability Shader
9287 OpMemoryModel Logical GLSL450
9288 OpEntryPoint GLCompute %main "main"
9289 OpExecutionMode %main LocalSize 1 1 1
9290 OpDecorate %block Block
9291 OpMemberDecorate %block 0 Offset 0
9292 OpMemberDecorate %block 0 ColMajor
9293 OpMemberDecorate %block 0 MatrixStride 8
9294 OpDecorate %var DescriptorSet 0
9295 OpDecorate %var Binding 0
9296 %void = OpTypeVoid
9297 %void_fn = OpTypeFunction %void
9298 %float = OpTypeFloat 32
9299 %float2 = OpTypeVector %float 2
9300 %matrix = OpTypeMatrix %float2 4
9301 %block = OpTypeStruct %matrix
9302 %ptr_block = OpTypePointer Uniform %block
9303 %var = OpVariable %ptr_block Uniform
9304 %main = OpFunction %void None %void_fn
9305 %entry = OpLabel
9306 OpReturn
9307 OpFunctionEnd
9308 )";
9309
9310 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_0);
9311 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_0));
9312 EXPECT_THAT(
9313 getDiagnosticString(),
9314 HasSubstr(
9315 "member 0 is a matrix with stride 8 not satisfying alignment to 16"));
9316 }
9317
9318 TEST_F(ValidateDecorations, Std140ColMajorMat2x3) {
9319 const std::string spirv = R"(
9320 OpCapability Shader
9321 OpMemoryModel Logical GLSL450
9322 OpEntryPoint GLCompute %main "main"
9323 OpExecutionMode %main LocalSize 1 1 1
9324 OpDecorate %block Block
9325 OpMemberDecorate %block 0 Offset 0
9326 OpMemberDecorate %block 0 ColMajor
9327 OpMemberDecorate %block 0 MatrixStride 12
9328 OpDecorate %var DescriptorSet 0
9329 OpDecorate %var Binding 0
9330 %void = OpTypeVoid
9331 %void_fn = OpTypeFunction %void
9332 %float = OpTypeFloat 32
9333 %float3 = OpTypeVector %float 3
9334 %matrix = OpTypeMatrix %float3 2
9335 %block = OpTypeStruct %matrix
9336 %ptr_block = OpTypePointer Uniform %block
9337 %var = OpVariable %ptr_block Uniform
9338 %main = OpFunction %void None %void_fn
9339 %entry = OpLabel
9340 OpReturn
9341 OpFunctionEnd
9342 )";
9343
9344 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_0);
9345 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_0));
9346 EXPECT_THAT(getDiagnosticString(),
9347 HasSubstr("member 0 is a matrix with stride 12 not satisfying "
9348 "alignment to 16"));
9349 }
9350
9351 TEST_F(ValidateDecorations, MatrixMissingMajornessUniform) {
9352 const std::string spirv = R"(
9353 OpCapability Shader
9354 OpMemoryModel Logical GLSL450
9355 OpEntryPoint GLCompute %main "main"
9356 OpExecutionMode %main LocalSize 1 1 1
9357 OpDecorate %block Block
9358 OpMemberDecorate %block 0 Offset 0
9359 OpMemberDecorate %block 0 MatrixStride 16
9360 OpDecorate %var DescriptorSet 0
9361 OpDecorate %var Binding 0
9362 %void = OpTypeVoid
9363 %void_fn = OpTypeFunction %void
9364 %float = OpTypeFloat 32
9365 %float2 = OpTypeVector %float 2
9366 %matrix = OpTypeMatrix %float2 2
9367 %block = OpTypeStruct %matrix
9368 %ptr_block = OpTypePointer Uniform %block
9369 %var = OpVariable %ptr_block Uniform
9370 %main = OpFunction %void None %void_fn
9371 %entry = OpLabel
9372 OpReturn
9373 OpFunctionEnd
9374 )";
9375
9376 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_0);
9377 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_0));
9378 EXPECT_THAT(
9379 getDiagnosticString(),
9380 HasSubstr(
9381 "must be explicitly laid out with RowMajor or ColMajor decorations"));
9382 }
9383
9384 TEST_F(ValidateDecorations, MatrixMissingMajornessStorageBuffer) {
9385 const std::string spirv = R"(
9386 OpCapability Shader
9387 OpExtension "SPV_KHR_storage_buffer_storage_class"
9388 OpMemoryModel Logical GLSL450
9389 OpEntryPoint GLCompute %main "main"
9390 OpExecutionMode %main LocalSize 1 1 1
9391 OpDecorate %block Block
9392 OpMemberDecorate %block 0 Offset 0
9393 OpMemberDecorate %block 0 MatrixStride 16
9394 OpDecorate %var DescriptorSet 0
9395 OpDecorate %var Binding 0
9396 %void = OpTypeVoid
9397 %void_fn = OpTypeFunction %void
9398 %float = OpTypeFloat 32
9399 %float2 = OpTypeVector %float 2
9400 %matrix = OpTypeMatrix %float2 2
9401 %block = OpTypeStruct %matrix
9402 %ptr_block = OpTypePointer StorageBuffer %block
9403 %var = OpVariable %ptr_block StorageBuffer
9404 %main = OpFunction %void None %void_fn
9405 %entry = OpLabel
9406 OpReturn
9407 OpFunctionEnd
9408 )";
9409
9410 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_0);
9411 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_0));
9412 EXPECT_THAT(
9413 getDiagnosticString(),
9414 HasSubstr(
9415 "must be explicitly laid out with RowMajor or ColMajor decorations"));
9416 }
9417
9418 TEST_F(ValidateDecorations, MatrixMissingMajornessPushConstant) {
9419 const std::string spirv = R"(
9420 OpCapability Shader
9421 OpMemoryModel Logical GLSL450
9422 OpEntryPoint GLCompute %main "main"
9423 OpExecutionMode %main LocalSize 1 1 1
9424 OpDecorate %block Block
9425 OpMemberDecorate %block 0 Offset 0
9426 OpMemberDecorate %block 0 MatrixStride 16
9427 %void = OpTypeVoid
9428 %void_fn = OpTypeFunction %void
9429 %float = OpTypeFloat 32
9430 %float2 = OpTypeVector %float 2
9431 %matrix = OpTypeMatrix %float2 2
9432 %block = OpTypeStruct %matrix
9433 %ptr_block = OpTypePointer PushConstant %block
9434 %var = OpVariable %ptr_block PushConstant
9435 %main = OpFunction %void None %void_fn
9436 %entry = OpLabel
9437 OpReturn
9438 OpFunctionEnd
9439 )";
9440
9441 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_0);
9442 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_0));
9443 EXPECT_THAT(
9444 getDiagnosticString(),
9445 HasSubstr(
9446 "must be explicitly laid out with RowMajor or ColMajor decorations"));
9447 }
9448
9449 TEST_F(ValidateDecorations, StructWithRowAndColMajor) {
9450 const std::string spirv = R"(
9451 OpCapability Shader
9452 OpMemoryModel Logical GLSL450
9453 OpEntryPoint GLCompute %main "main"
9454 OpExecutionMode %main LocalSize 1 1 1
9455 OpDecorate %block Block
9456 OpMemberDecorate %block 0 Offset 0
9457 OpMemberDecorate %block 0 MatrixStride 16
9458 OpMemberDecorate %block 0 ColMajor
9459 OpMemberDecorate %block 1 Offset 32
9460 OpMemberDecorate %block 1 MatrixStride 16
9461 OpMemberDecorate %block 1 RowMajor
9462 %void = OpTypeVoid
9463 %void_fn = OpTypeFunction %void
9464 %float = OpTypeFloat 32
9465 %float2 = OpTypeVector %float 2
9466 %matrix = OpTypeMatrix %float2 2
9467 %block = OpTypeStruct %matrix %matrix
9468 %ptr_block = OpTypePointer PushConstant %block
9469 %var = OpVariable %ptr_block PushConstant
9470 %main = OpFunction %void None %void_fn
9471 %entry = OpLabel
9472 OpReturn
9473 OpFunctionEnd
9474 )";
9475
9476 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_0);
9477 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_VULKAN_1_0));
9478 }
9479
9480 TEST_F(ValidateDecorations, PhysicalStorageBufferWithOffset) {
9481 const std::string spirv = R"(
9482 OpCapability Shader
9483 OpCapability Int64
9484 OpCapability PhysicalStorageBufferAddresses
9485 OpMemoryModel PhysicalStorageBuffer64 GLSL450
9486 OpEntryPoint GLCompute %main "main" %pc
9487 OpExecutionMode %main LocalSize 1 1 1
9488 OpDecorate %pc_block Block
9489 OpMemberDecorate %pc_block 0 Offset 0
9490 OpMemberDecorate %pssbo_struct 0 Offset 0
9491 %void = OpTypeVoid
9492 %long = OpTypeInt 64 0
9493 %float = OpTypeFloat 32
9494 %int = OpTypeInt 32 0
9495 %int_0 = OpConstant %int 0
9496 %pc_block = OpTypeStruct %long
9497 %pc_block_ptr = OpTypePointer PushConstant %pc_block
9498 %pc_long_ptr = OpTypePointer PushConstant %long
9499 %pc = OpVariable %pc_block_ptr PushConstant
9500 %pssbo_struct = OpTypeStruct %float
9501 %pssbo_ptr = OpTypePointer PhysicalStorageBuffer %pssbo_struct
9502 %void_fn = OpTypeFunction %void
9503 %main = OpFunction %void None %void_fn
9504 %entry = OpLabel
9505 %pc_gep = OpAccessChain %pc_long_ptr %pc %int_0
9506 %addr = OpLoad %long %pc_gep
9507 %ptr = OpConvertUToPtr %pssbo_ptr %addr
9508 OpReturn
9509 OpFunctionEnd
9510 )";
9511
9512 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_3);
9513 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_VULKAN_1_3));
9514 }
9515
9516 TEST_F(ValidateDecorations, UntypedVariableDuplicateInterface) {
9517 const std::string spirv = R"(
9518 OpCapability Shader
9519 OpCapability UntypedPointersKHR
9520 OpCapability WorkgroupMemoryExplicitLayoutKHR
9521 OpExtension "SPV_KHR_workgroup_memory_explicit_layout"
9522 OpExtension "SPV_KHR_untyped_pointers"
9523 OpMemoryModel Logical GLSL450
9524 OpEntryPoint GLCompute %main "main" %var %var
9525 OpName %var "var"
9526 OpDecorate %struct Block
9527 OpMemberDecorate %struct 0 Offset 0
9528 %void = OpTypeVoid
9529 %int = OpTypeInt 32 0
9530 %struct = OpTypeStruct %int
9531 %ptr = OpTypeUntypedPointerKHR Workgroup
9532 %var = OpUntypedVariableKHR %ptr Workgroup %struct
9533 %void_fn = OpTypeFunction %void
9534 %main = OpFunction %void None %void_fn
9535 %entry = OpLabel
9536 OpReturn
9537 OpFunctionEnd
9538 )";
9539
9540 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_4);
9541 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_UNIVERSAL_1_4));
9542 EXPECT_THAT(getDiagnosticString(),
9543 HasSubstr("In SPIR-V 1.4 or later, non-unique OpEntryPoint "
9544 "interface '2[%var]' is disallowed"));
9545 }
9546
9547 TEST_F(ValidateDecorations, PhysicalStorageBufferMissingOffset) {
9548 const std::string spirv = R"(
9549 OpCapability Shader
9550 OpCapability Int64
9551 OpCapability PhysicalStorageBufferAddresses
9552 OpMemoryModel PhysicalStorageBuffer64 GLSL450
9553 OpEntryPoint GLCompute %main "main" %pc
9554 OpExecutionMode %main LocalSize 1 1 1
9555 OpDecorate %pc_block Block
9556 OpMemberDecorate %pc_block 0 Offset 0
9557 %void = OpTypeVoid
9558 %long = OpTypeInt 64 0
9559 %float = OpTypeFloat 32
9560 %int = OpTypeInt 32 0
9561 %int_0 = OpConstant %int 0
9562 %pc_block = OpTypeStruct %long
9563 %pc_block_ptr = OpTypePointer PushConstant %pc_block
9564 %pc_long_ptr = OpTypePointer PushConstant %long
9565 %pc = OpVariable %pc_block_ptr PushConstant
9566 %pssbo_struct = OpTypeStruct %float
9567 %pssbo_ptr = OpTypePointer PhysicalStorageBuffer %pssbo_struct
9568 %void_fn = OpTypeFunction %void
9569 %main = OpFunction %void None %void_fn
9570 %entry = OpLabel
9571 %pc_gep = OpAccessChain %pc_long_ptr %pc %int_0
9572 %addr = OpLoad %long %pc_gep
9573 %ptr = OpConvertUToPtr %pssbo_ptr %addr
9574 OpReturn
9575 OpFunctionEnd
9576 )";
9577
9578 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_3);
9579 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_3));
9580 EXPECT_THAT(
9581 getDiagnosticString(),
9582 HasSubstr("decorated as Block for variable in PhysicalStorageBuffer "
9583 "storage class must follow relaxed storage buffer layout "
9584 "rules: member 0 is missing an Offset decoration"));
9585 }
9586
9587 TEST_F(ValidateDecorations, PhysicalStorageBufferMissingArrayStride) {
9588 const std::string spirv = R"(
9589 OpCapability Shader
9590 OpCapability Int64
9591 OpCapability PhysicalStorageBufferAddresses
9592 OpMemoryModel PhysicalStorageBuffer64 GLSL450
9593 OpEntryPoint GLCompute %main "main" %pc
9594 OpExecutionMode %main LocalSize 1 1 1
9595 OpDecorate %pc_block Block
9596 OpMemberDecorate %pc_block 0 Offset 0
9597 %void = OpTypeVoid
9598 %long = OpTypeInt 64 0
9599 %float = OpTypeFloat 32
9600 %int = OpTypeInt 32 0
9601 %int_0 = OpConstant %int 0
9602 %int_4 = OpConstant %int 4
9603 %pc_block = OpTypeStruct %long
9604 %pc_block_ptr = OpTypePointer PushConstant %pc_block
9605 %pc_long_ptr = OpTypePointer PushConstant %long
9606 %pc = OpVariable %pc_block_ptr PushConstant
9607 %pssbo_array = OpTypeArray %float %int_4
9608 %pssbo_ptr = OpTypePointer PhysicalStorageBuffer %pssbo_array
9609 %void_fn = OpTypeFunction %void
9610 %main = OpFunction %void None %void_fn
9611 %entry = OpLabel
9612 %pc_gep = OpAccessChain %pc_long_ptr %pc %int_0
9613 %addr = OpLoad %long %pc_gep
9614 %ptr = OpConvertUToPtr %pssbo_ptr %addr
9615 OpReturn
9616 OpFunctionEnd
9617 )";
9618
9619 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_3);
9620 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_3));
9621 EXPECT_THAT(
9622 getDiagnosticString(),
9623 HasSubstr(
9624 "decorated as Block for variable in PhysicalStorageBuffer storage "
9625 "class must follow relaxed storage buffer layout rules: member 0 "
9626 "contains an array with stride 0, but with an element size of 4"));
9627 }
9628
9629 TEST_F(ValidateDecorations, MatrixArrayMissingMajorness) {
9630 const std::string spirv = R"(
9631 OpCapability Shader
9632 OpMemoryModel Logical GLSL450
9633 OpEntryPoint GLCompute %main "main"
9634 OpExecutionMode %main LocalSize 1 1 1
9635 OpDecorate %var DescriptorSet 0
9636 OpDecorate %var Binding 0
9637 OpDecorate %block Block
9638 OpMemberDecorate %block 0 Offset 0
9639 OpMemberDecorate %block 0 MatrixStride 16
9640 OpDecorate %array ArrayStride 32
9641 %void = OpTypeVoid
9642 %float = OpTypeFloat 32
9643 %int = OpTypeInt 32 0
9644 %int_2 = OpConstant %int 2
9645 %vec = OpTypeVector %float 2
9646 %mat = OpTypeMatrix %vec 2
9647 %array = OpTypeArray %mat %int_2
9648 %block = OpTypeStruct %array
9649 %ptr = OpTypePointer Uniform %block
9650 %var = OpVariable %ptr Uniform
9651 %void_fn = OpTypeFunction %void
9652 %main = OpFunction %void None %void_fn
9653 %entry = OpLabel
9654 OpReturn
9655 OpFunctionEnd
9656 )";
9657
9658 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_1);
9659 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_1));
9660 EXPECT_THAT(
9661 getDiagnosticString(),
9662 HasSubstr(
9663 "must be explicitly laid out with RowMajor or ColMajor decorations"));
9664 }
9665
9666 TEST_F(ValidateDecorations, MatrixArrayMissingStride) {
9667 const std::string spirv = R"(
9668 OpCapability Shader
9669 OpMemoryModel Logical GLSL450
9670 OpEntryPoint GLCompute %main "main"
9671 OpExecutionMode %main LocalSize 1 1 1
9672 OpDecorate %var DescriptorSet 0
9673 OpDecorate %var Binding 0
9674 OpDecorate %block Block
9675 OpMemberDecorate %block 0 Offset 0
9676 OpMemberDecorate %block 0 ColMajor
9677 OpDecorate %array ArrayStride 32
9678 %void = OpTypeVoid
9679 %float = OpTypeFloat 32
9680 %int = OpTypeInt 32 0
9681 %int_2 = OpConstant %int 2
9682 %vec = OpTypeVector %float 2
9683 %mat = OpTypeMatrix %vec 2
9684 %array = OpTypeArray %mat %int_2
9685 %block = OpTypeStruct %array
9686 %ptr = OpTypePointer Uniform %block
9687 %var = OpVariable %ptr Uniform
9688 %void_fn = OpTypeFunction %void
9689 %main = OpFunction %void None %void_fn
9690 %entry = OpLabel
9691 OpReturn
9692 OpFunctionEnd
9693 )";
9694
9695 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_1);
9696 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_1));
9697 EXPECT_THAT(
9698 getDiagnosticString(),
9699 HasSubstr("must be explicitly laid out with MatrixStride decorations"));
9700 }
9701
9702 TEST_F(ValidateDecorations, MatrixArrayBadStride) {
9703 const std::string spirv = R"(
9704 OpCapability Shader
9705 OpMemoryModel Logical GLSL450
9706 OpEntryPoint GLCompute %main "main"
9707 OpExecutionMode %main LocalSize 1 1 1
9708 OpDecorate %var DescriptorSet 0
9709 OpDecorate %var Binding 0
9710 OpDecorate %block Block
9711 OpMemberDecorate %block 0 Offset 0
9712 OpMemberDecorate %block 0 ColMajor
9713 OpMemberDecorate %block 0 MatrixStride 8
9714 OpDecorate %array ArrayStride 32
9715 %void = OpTypeVoid
9716 %float = OpTypeFloat 32
9717 %int = OpTypeInt 32 0
9718 %int_2 = OpConstant %int 2
9719 %vec = OpTypeVector %float 2
9720 %mat = OpTypeMatrix %vec 2
9721 %array = OpTypeArray %mat %int_2
9722 %block = OpTypeStruct %array
9723 %ptr = OpTypePointer Uniform %block
9724 %var = OpVariable %ptr Uniform
9725 %void_fn = OpTypeFunction %void
9726 %main = OpFunction %void None %void_fn
9727 %entry = OpLabel
9728 OpReturn
9729 OpFunctionEnd
9730 )";
9731
9732 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_1);
9733 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_1));
9734 EXPECT_THAT(
9735 getDiagnosticString(),
9736 HasSubstr("is a matrix with stride 8 not satisfying alignment to 16"));
9737 }
9738
9739 TEST_F(ValidateDecorations, MatrixArrayArrayMissingMajorness) {
9740 const std::string spirv = R"(
9741 OpCapability Shader
9742 OpMemoryModel Logical GLSL450
9743 OpEntryPoint GLCompute %main "main"
9744 OpExecutionMode %main LocalSize 1 1 1
9745 OpDecorate %var DescriptorSet 0
9746 OpDecorate %var Binding 0
9747 OpDecorate %block Block
9748 OpMemberDecorate %block 0 Offset 0
9749 OpMemberDecorate %block 0 MatrixStride 16
9750 OpDecorate %array ArrayStride 32
9751 OpDecorate %rta ArrayStride 64
9752 %void = OpTypeVoid
9753 %float = OpTypeFloat 32
9754 %int = OpTypeInt 32 0
9755 %int_2 = OpConstant %int 2
9756 %vec = OpTypeVector %float 2
9757 %mat = OpTypeMatrix %vec 2
9758 %array = OpTypeArray %mat %int_2
9759 %rta = OpTypeRuntimeArray %array
9760 %block = OpTypeStruct %rta
9761 %ptr = OpTypePointer StorageBuffer %block
9762 %var = OpVariable %ptr StorageBuffer
9763 %void_fn = OpTypeFunction %void
9764 %main = OpFunction %void None %void_fn
9765 %entry = OpLabel
9766 OpReturn
9767 OpFunctionEnd
9768 )";
9769
9770 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_1);
9771 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_1));
9772 EXPECT_THAT(
9773 getDiagnosticString(),
9774 HasSubstr(
9775 "must be explicitly laid out with RowMajor or ColMajor decorations"));
9776 }
9777
9778 TEST_F(ValidateDecorations, MatrixArrayArrayMissingStride) {
9779 const std::string spirv = R"(
9780 OpCapability Shader
9781 OpMemoryModel Logical GLSL450
9782 OpEntryPoint GLCompute %main "main"
9783 OpExecutionMode %main LocalSize 1 1 1
9784 OpDecorate %var DescriptorSet 0
9785 OpDecorate %var Binding 0
9786 OpDecorate %block Block
9787 OpMemberDecorate %block 0 Offset 0
9788 OpMemberDecorate %block 0 ColMajor
9789 OpDecorate %array ArrayStride 32
9790 OpDecorate %rta ArrayStride 64
9791 %void = OpTypeVoid
9792 %float = OpTypeFloat 32
9793 %int = OpTypeInt 32 0
9794 %int_2 = OpConstant %int 2
9795 %vec = OpTypeVector %float 2
9796 %mat = OpTypeMatrix %vec 2
9797 %array = OpTypeArray %mat %int_2
9798 %rta = OpTypeRuntimeArray %array
9799 %block = OpTypeStruct %rta
9800 %ptr = OpTypePointer StorageBuffer %block
9801 %var = OpVariable %ptr StorageBuffer
9802 %void_fn = OpTypeFunction %void
9803 %main = OpFunction %void None %void_fn
9804 %entry = OpLabel
9805 OpReturn
9806 OpFunctionEnd
9807 )";
9808
9809 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_1);
9810 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_1));
9811 EXPECT_THAT(
9812 getDiagnosticString(),
9813 HasSubstr("must be explicitly laid out with MatrixStride decorations"));
9814 }
9815
9816 TEST_F(ValidateDecorations, MatrixArrayArrayBadStride) {
9817 const std::string spirv = R"(
9818 OpCapability Shader
9819 OpMemoryModel Logical GLSL450
9820 OpEntryPoint GLCompute %main "main"
9821 OpExecutionMode %main LocalSize 1 1 1
9822 OpDecorate %var DescriptorSet 0
9823 OpDecorate %var Binding 0
9824 OpDecorate %block Block
9825 OpMemberDecorate %block 0 Offset 0
9826 OpMemberDecorate %block 0 ColMajor
9827 OpMemberDecorate %block 0 MatrixStride 8
9828 OpDecorate %array ArrayStride 32
9829 OpDecorate %a ArrayStride 64
9830 %void = OpTypeVoid
9831 %float = OpTypeFloat 32
9832 %int = OpTypeInt 32 0
9833 %int_2 = OpConstant %int 2
9834 %vec = OpTypeVector %float 2
9835 %mat = OpTypeMatrix %vec 2
9836 %array = OpTypeArray %mat %int_2
9837 %a = OpTypeArray %array %int_2
9838 %block = OpTypeStruct %a
9839 %ptr = OpTypePointer Uniform %block
9840 %var = OpVariable %ptr Uniform
9841 %void_fn = OpTypeFunction %void
9842 %main = OpFunction %void None %void_fn
9843 %entry = OpLabel
9844 OpReturn
9845 OpFunctionEnd
9846 )";
9847
9848 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_1);
9849 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_1));
9850 EXPECT_THAT(
9851 getDiagnosticString(),
9852 HasSubstr("is a matrix with stride 8 not satisfying alignment to 16"));
9853 }
9854
9855 TEST_F(ValidateDecorations, MultipleBuiltinsInputVertex) {
9856 const std::string body = R"(
9857 OpCapability Shader
9858 OpCapability DrawParameters
9859 OpMemoryModel Logical GLSL450
9860 OpEntryPoint Vertex %main "main" %_ %gl_BaseInstance1 %gl_BaseInstance2
9861 OpMemberDecorate %gl_PerVertex 0 BuiltIn Position
9862 OpMemberDecorate %gl_PerVertex 1 BuiltIn PointSize
9863 OpMemberDecorate %gl_PerVertex 2 BuiltIn ClipDistance
9864 OpMemberDecorate %gl_PerVertex 3 BuiltIn CullDistance
9865 OpDecorate %gl_PerVertex Block
9866 OpDecorate %gl_BaseInstance1 BuiltIn BaseInstance
9867 OpDecorate %gl_BaseInstance2 BuiltIn BaseInstance
9868 %void = OpTypeVoid
9869 %3 = OpTypeFunction %void
9870 %float = OpTypeFloat 32
9871 %v4float = OpTypeVector %float 4
9872 %uint = OpTypeInt 32 0
9873 %uint_1 = OpConstant %uint 1
9874 %_arr_float_uint_1 = OpTypeArray %float %uint_1
9875 %gl_PerVertex = OpTypeStruct %v4float %float %_arr_float_uint_1 %_arr_float_uint_1
9876 %_ptr_Output_gl_PerVertex = OpTypePointer Output %gl_PerVertex
9877 %_ = OpVariable %_ptr_Output_gl_PerVertex Output
9878 %int = OpTypeInt 32 1
9879 %int_0 = OpConstant %int 0
9880 %float_0 = OpConstant %float 0
9881 %17 = OpConstantComposite %v4float %float_0 %float_0 %float_0 %float_0
9882 %_ptr_Input_int = OpTypePointer Input %int
9883 %gl_BaseInstance1 = OpVariable %_ptr_Input_int Input
9884 %gl_BaseInstance2 = OpVariable %_ptr_Input_int Input
9885 %_ptr_Output_v4float = OpTypePointer Output %v4float
9886 %main = OpFunction %void None %3
9887 %5 = OpLabel
9888 %20 = OpLoad %int %gl_BaseInstance1
9889 %21 = OpConvertSToF %float %20
9890 %22 = OpVectorTimesScalar %v4float %17 %21
9891 %24 = OpAccessChain %_ptr_Output_v4float %_ %int_0
9892 OpStore %24 %22
9893 OpReturn
9894 OpFunctionEnd
9895 )";
9896
9897 CompileSuccessfully(body.c_str(), SPV_ENV_VULKAN_1_2);
9898 ASSERT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_2));
9899 EXPECT_THAT(getDiagnosticString(),
9900 HasSubstr("OpEntryPoint contains duplicate input variables with "
9901 "BaseInstance builtin"));
9902 EXPECT_THAT(getDiagnosticString(),
9903 AnyVUID("VUID-StandaloneSpirv-OpEntryPoint-09658"));
9904 }
9905
9906 TEST_F(ValidateDecorations, MultipleBuiltinsInputMesh) {
9907 const std::string body = R"(
9908 OpCapability DrawParameters
9909 OpCapability MeshShadingEXT
9910 OpExtension "SPV_EXT_mesh_shader"
9911 OpMemoryModel Logical GLSL450
9912 OpEntryPoint MeshEXT %main "main" %gl_DrawID_1 %gl_DrawID_2
9913 OpExecutionMode %main LocalSize 1 1 1
9914 OpExecutionMode %main OutputVertices 32
9915 OpExecutionMode %main OutputPrimitivesEXT 32
9916 OpExecutionMode %main OutputTrianglesEXT
9917 OpDecorate %gl_DrawID_1 BuiltIn DrawIndex
9918 OpDecorate %gl_DrawID_2 BuiltIn DrawIndex
9919 %void = OpTypeVoid
9920 %3 = OpTypeFunction %void
9921 %int = OpTypeInt 32 1
9922 %_ptr_Input_int = OpTypePointer Input %int
9923 %gl_DrawID_1 = OpVariable %_ptr_Input_int Input
9924 %gl_DrawID_2 = OpVariable %_ptr_Input_int Input
9925 %uint = OpTypeInt 32 0
9926 %main = OpFunction %void None %3
9927 %5 = OpLabel
9928 %9 = OpLoad %int %gl_DrawID_1
9929 %11 = OpBitcast %uint %9
9930 %12 = OpLoad %int %gl_DrawID_2
9931 %13 = OpBitcast %uint %12
9932 OpSetMeshOutputsEXT %11 %13
9933 OpReturn
9934 OpFunctionEnd
9935 )";
9936
9937 CompileSuccessfully(body.c_str(), SPV_ENV_VULKAN_1_2);
9938 ASSERT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_2));
9939 EXPECT_THAT(getDiagnosticString(),
9940 HasSubstr("OpEntryPoint contains duplicate input variables with "
9941 "DrawIndex builtin"));
9942 EXPECT_THAT(getDiagnosticString(),
9943 AnyVUID("VUID-StandaloneSpirv-OpEntryPoint-09658"));
9944 }
9945
9946 TEST_F(ValidateDecorations, MultipleBuiltinsInputCompute) {
9947 const std::string body = R"(
9948 OpCapability Shader
9949 OpMemoryModel Logical GLSL450
9950 OpEntryPoint GLCompute %main "main" %_ %gl_WorkGroupID_1 %gl_WorkGroupID_2
9951 OpExecutionMode %main LocalSize 1 1 1
9952 OpMemberDecorate %Buffers 0 Offset 0
9953 OpDecorate %Buffers Block
9954 OpDecorate %_ DescriptorSet 0
9955 OpDecorate %_ Binding 0
9956 OpDecorate %gl_WorkGroupID_1 BuiltIn WorkgroupId
9957 OpDecorate %gl_WorkGroupID_2 BuiltIn WorkgroupId
9958 %void = OpTypeVoid
9959 %3 = OpTypeFunction %void
9960 %uint = OpTypeInt 32 0
9961 %v3uint = OpTypeVector %uint 3
9962 %Buffers = OpTypeStruct %v3uint
9963 %_ptr_StorageBuffer_Buffers = OpTypePointer StorageBuffer %Buffers
9964 %_ = OpVariable %_ptr_StorageBuffer_Buffers StorageBuffer
9965 %int = OpTypeInt 32 1
9966 %int_0 = OpConstant %int 0
9967 %_ptr_Input_v3uint = OpTypePointer Input %v3uint
9968 %gl_WorkGroupID_1 = OpVariable %_ptr_Input_v3uint Input
9969 %gl_WorkGroupID_2 = OpVariable %_ptr_Input_v3uint Input
9970 %_ptr_StorageBuffer_v3uint = OpTypePointer StorageBuffer %v3uint
9971 %main = OpFunction %void None %3
9972 %5 = OpLabel
9973 %15 = OpLoad %v3uint %gl_WorkGroupID_1
9974 %16 = OpLoad %v3uint %gl_WorkGroupID_2
9975 %17 = OpIAdd %v3uint %15 %16
9976 %19 = OpAccessChain %_ptr_StorageBuffer_v3uint %_ %int_0
9977 OpStore %19 %17
9978 OpReturn
9979 OpFunctionEnd
9980 )";
9981
9982 CompileSuccessfully(body.c_str(), SPV_ENV_VULKAN_1_2);
9983 ASSERT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_2));
9984 EXPECT_THAT(getDiagnosticString(),
9985 HasSubstr("OpEntryPoint contains duplicate input variables with "
9986 "WorkgroupId builtin"));
9987 EXPECT_THAT(getDiagnosticString(),
9988 AnyVUID("VUID-StandaloneSpirv-OpEntryPoint-09658"));
9989 }
9990
9991 TEST_F(ValidateDecorations, MultipleBuiltinsOutputFragment) {
9992 const std::string body = R"(
9993 OpCapability Shader
9994 OpMemoryModel Logical GLSL450
9995 OpEntryPoint Fragment %main "main" %gl_FragDepth_1 %gl_FragDepth_2
9996 OpExecutionMode %main OriginUpperLeft
9997 OpExecutionMode %main DepthReplacing
9998 OpDecorate %gl_FragDepth_1 BuiltIn FragDepth
9999 OpDecorate %gl_FragDepth_2 BuiltIn FragDepth
10000 %void = OpTypeVoid
10001 %3 = OpTypeFunction %void
10002 %float = OpTypeFloat 32
10003 %_ptr_Output_float = OpTypePointer Output %float
10004 %gl_FragDepth_1 = OpVariable %_ptr_Output_float Output
10005 %gl_FragDepth_2 = OpVariable %_ptr_Output_float Output
10006 %float_1 = OpConstant %float 1
10007 %main = OpFunction %void None %3
10008 %5 = OpLabel
10009 OpStore %gl_FragDepth_1 %float_1
10010 %10 = OpLoad %float %gl_FragDepth_1
10011 %11 = OpFAdd %float %10 %float_1
10012 OpStore %gl_FragDepth_2 %11
10013 OpReturn
10014 OpFunctionEnd
10015 )";
10016
10017 CompileSuccessfully(body.c_str(), SPV_ENV_VULKAN_1_2);
10018 ASSERT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_2));
10019 EXPECT_THAT(getDiagnosticString(),
10020 HasSubstr("OpEntryPoint contains duplicate output variables with "
10021 "FragDepth builtin"));
10022 EXPECT_THAT(getDiagnosticString(),
10023 AnyVUID("VUID-StandaloneSpirv-OpEntryPoint-09659"));
10024 }
10025
10026 TEST_F(ValidateDecorations, MultipleBuiltinsRayTmaxKHR) {
10027 const std::string body = R"(
10028 OpCapability RayTracingKHR
10029 OpExtension "SPV_KHR_ray_tracing"
10030 OpMemoryModel Logical GLSL450
10031 OpEntryPoint AnyHitKHR %main "main" %gl_RayTmaxEXT %gl_HitTEXT %incomingPayload
10032 OpDecorate %gl_RayTmaxEXT BuiltIn RayTmaxKHR
10033 OpDecorate %gl_HitTEXT BuiltIn RayTmaxKHR
10034 %void = OpTypeVoid
10035 %3 = OpTypeFunction %void
10036 %float = OpTypeFloat 32
10037 %_ptr_Function_float = OpTypePointer Function %float
10038 %_ptr_Input_float = OpTypePointer Input %float
10039 %gl_RayTmaxEXT = OpVariable %_ptr_Input_float Input
10040 %gl_HitTEXT = OpVariable %_ptr_Input_float Input
10041 %v4float = OpTypeVector %float 4
10042 %_ptr_IncomingRayPayloadKHR_v4float = OpTypePointer IncomingRayPayloadKHR %v4float
10043 %incomingPayload = OpVariable %_ptr_IncomingRayPayloadKHR_v4float IncomingRayPayloadKHR
10044 %main = OpFunction %void None %3
10045 %5 = OpLabel
10046 %a = OpVariable %_ptr_Function_float Function
10047 %b = OpVariable %_ptr_Function_float Function
10048 %11 = OpLoad %float %gl_RayTmaxEXT
10049 OpStore %a %11
10050 %14 = OpLoad %float %gl_HitTEXT
10051 OpStore %b %14
10052 %18 = OpLoad %float %a
10053 %19 = OpLoad %float %b
10054 %22 = OpCompositeConstruct %v4float %18 %18 %19 %19
10055 OpStore %incomingPayload %22
10056 OpTerminateRayKHR
10057 OpFunctionEnd
10058 )";
10059
10060 CompileSuccessfully(body.c_str(), SPV_ENV_VULKAN_1_2);
10061 ASSERT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_2));
10062 EXPECT_THAT(
10063 getDiagnosticString(),
10064 HasSubstr(
10065 "OpEntryPoint contains duplicate input variables with RayTmax"));
10066 EXPECT_THAT(getDiagnosticString(),
10067 AnyVUID("VUID-StandaloneSpirv-OpEntryPoint-09658"));
10068 }
10069
10070 TEST_F(ValidateDecorations, MultipleBuiltinsBlock) {
10071 const std::string body = R"(
10072 OpCapability Shader
10073 OpMemoryModel Logical GLSL450
10074 OpEntryPoint Vertex %main "main" %var
10075 OpMemberDecorate %gl_PerVertex 0 BuiltIn Position
10076 OpMemberDecorate %gl_PerVertex 1 BuiltIn Position
10077 OpDecorate %gl_PerVertex Block
10078 %void = OpTypeVoid
10079 %3 = OpTypeFunction %void
10080 %float = OpTypeFloat 32
10081 %v4float = OpTypeVector %float 4
10082 %gl_PerVertex = OpTypeStruct %v4float %v4float
10083 %_ptr_gl_PerVertex = OpTypePointer Output %gl_PerVertex
10084 %var = OpVariable %_ptr_gl_PerVertex Output
10085 %int = OpTypeInt 32 1
10086 %int_0 = OpConstant %int 0
10087 %int_1 = OpConstant %int 1
10088 %float_0 = OpConstant %float 0
10089 %17 = OpConstantComposite %v4float %float_0 %float_0 %float_0 %float_0
10090 %ptr_vec4 = OpTypePointer Output %v4float
10091 %main = OpFunction %void None %3
10092 %5 = OpLabel
10093 %19 = OpAccessChain %ptr_vec4 %var %int_0
10094 OpStore %19 %17
10095 %22 = OpAccessChain %ptr_vec4 %var %int_1
10096 OpStore %22 %17
10097 OpReturn
10098 OpFunctionEnd
10099 )";
10100
10101 CompileSuccessfully(body.c_str(), SPV_ENV_VULKAN_1_0);
10102 ASSERT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_0));
10103 EXPECT_THAT(
10104 getDiagnosticString(),
10105 HasSubstr(
10106 "OpEntryPoint contains duplicate output variables with Position"));
10107 EXPECT_THAT(getDiagnosticString(),
10108 AnyVUID("VUID-StandaloneSpirv-OpEntryPoint-09659"));
10109 }
10110
10111 TEST_F(ValidateDecorations, MultipleBuiltinsBlockMixed) {
10112 const std::string body = R"(
10113 OpCapability Shader
10114 OpMemoryModel Logical GLSL450
10115 OpEntryPoint Vertex %main "main" %var %position
10116 OpMemberDecorate %gl_PerVertex 0 BuiltIn Position
10117 OpDecorate %gl_PerVertex Block
10118 OpDecorate %position BuiltIn Position
10119 %void = OpTypeVoid
10120 %3 = OpTypeFunction %void
10121 %float = OpTypeFloat 32
10122 %v4float = OpTypeVector %float 4
10123 %gl_PerVertex = OpTypeStruct %v4float
10124 %_ptr_gl_PerVertex = OpTypePointer Output %gl_PerVertex
10125 %var = OpVariable %_ptr_gl_PerVertex Output
10126 %int = OpTypeInt 32 1
10127 %int_0 = OpConstant %int 0
10128 %int_1 = OpConstant %int 1
10129 %float_0 = OpConstant %float 0
10130 %17 = OpConstantComposite %v4float %float_0 %float_0 %float_0 %float_0
10131 %ptr_vec4 = OpTypePointer Output %v4float
10132 %position = OpVariable %ptr_vec4 Output
10133 %main = OpFunction %void None %3
10134 %5 = OpLabel
10135 %19 = OpAccessChain %ptr_vec4 %var %int_0
10136 OpStore %19 %17
10137 OpStore %position %17
10138 OpReturn
10139 OpFunctionEnd
10140 )";
10141
10142 CompileSuccessfully(body.c_str(), SPV_ENV_VULKAN_1_0);
10143 ASSERT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_0));
10144 EXPECT_THAT(
10145 getDiagnosticString(),
10146 HasSubstr(
10147 "OpEntryPoint contains duplicate output variables with Position"));
10148 EXPECT_THAT(getDiagnosticString(),
10149 AnyVUID("VUID-StandaloneSpirv-OpEntryPoint-09659"));
10150 }
10151
10152 TEST_F(ValidateDecorations, UntypedVariableWorkgroupRequiresStruct) {
10153 const std::string spirv = R"(
10154 OpCapability Shader
10155 OpCapability UntypedPointersKHR
10156 OpCapability WorkgroupMemoryExplicitLayoutKHR
10157 OpExtension "SPV_KHR_untyped_pointers"
10158 OpExtension "SPV_KHR_workgroup_memory_explicit_layout"
10159 OpMemoryModel Logical GLSL450
10160 OpEntryPoint GLCompute %main "main" %var
10161 %void = OpTypeVoid
10162 %int = OpTypeInt 32 0
10163 %ptr = OpTypeUntypedPointerKHR Workgroup
10164 %var = OpUntypedVariableKHR %ptr Workgroup %int
10165 %void_fn = OpTypeFunction %void
10166 %main = OpFunction %void None %void_fn
10167 %entry = OpLabel
10168 OpReturn
10169 OpFunctionEnd
10170 )";
10171
10172 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_4);
10173 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_UNIVERSAL_1_4));
10174 EXPECT_THAT(getDiagnosticString(),
10175 HasSubstr("Untyped workgroup variables in shaders must be block "
10176 "decorated structs"));
10177 }
10178
10179 TEST_F(ValidateDecorations, UntypedVariableWorkgroupRequiresBlockStruct) {
10180 const std::string spirv = R"(
10181 OpCapability Shader
10182 OpCapability UntypedPointersKHR
10183 OpCapability WorkgroupMemoryExplicitLayoutKHR
10184 OpExtension "SPV_KHR_untyped_pointers"
10185 OpExtension "SPV_KHR_workgroup_memory_explicit_layout"
10186 OpMemoryModel Logical GLSL450
10187 OpEntryPoint GLCompute %main "main" %var
10188 %void = OpTypeVoid
10189 %int = OpTypeInt 32 0
10190 %struct = OpTypeStruct %int
10191 %ptr = OpTypeUntypedPointerKHR Workgroup
10192 %var = OpUntypedVariableKHR %ptr Workgroup %struct
10193 %void_fn = OpTypeFunction %void
10194 %main = OpFunction %void None %void_fn
10195 %entry = OpLabel
10196 OpReturn
10197 OpFunctionEnd
10198 )";
10199
10200 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_4);
10201 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_UNIVERSAL_1_4));
10202 EXPECT_THAT(getDiagnosticString(),
10203 HasSubstr("Untyped workgroup variables in shaders must be block "
10204 "decorated"));
10205 }
10206
10207 TEST_F(ValidateDecorations, UntypedVariableStorageBufferMissingBlock) {
10208 const std::string spirv = R"(
10209 OpCapability Shader
10210 OpCapability UntypedPointersKHR
10211 OpExtension "SPV_KHR_untyped_pointers"
10212 OpExtension "SPV_KHR_storage_buffer_storage_class"
10213 OpMemoryModel Logical GLSL450
10214 OpEntryPoint GLCompute %main "main"
10215 OpExecutionMode %main LocalSize 1 1 1
10216 OpName %struct "struct"
10217 %void = OpTypeVoid
10218 %int = OpTypeInt 32 0
10219 %struct = OpTypeStruct %int
10220 %ptr = OpTypeUntypedPointerKHR StorageBuffer
10221 %var = OpUntypedVariableKHR %ptr StorageBuffer %struct
10222 %void_fn = OpTypeFunction %void
10223 %main = OpFunction %void None %void_fn
10224 %entry = OpLabel
10225 OpReturn
10226 OpFunctionEnd
10227 )";
10228
10229 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_0);
10230 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_0));
10231 EXPECT_THAT(getDiagnosticString(),
10232 HasSubstr("StorageBuffer id '2' is missing Block decoration"));
10233 }
10234
10235 TEST_F(ValidateDecorations, UntypedVariableUniformMissingBlock) {
10236 const std::string spirv = R"(
10237 OpCapability Shader
10238 OpCapability UntypedPointersKHR
10239 OpExtension "SPV_KHR_untyped_pointers"
10240 OpMemoryModel Logical GLSL450
10241 OpEntryPoint GLCompute %main "main"
10242 OpExecutionMode %main LocalSize 1 1 1
10243 OpName %struct "struct"
10244 %void = OpTypeVoid
10245 %int = OpTypeInt 32 0
10246 %struct = OpTypeStruct %int
10247 %ptr = OpTypeUntypedPointerKHR Uniform
10248 %var = OpUntypedVariableKHR %ptr Uniform %struct
10249 %void_fn = OpTypeFunction %void
10250 %main = OpFunction %void None %void_fn
10251 %entry = OpLabel
10252 OpReturn
10253 OpFunctionEnd
10254 )";
10255
10256 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_0);
10257 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_0));
10258 EXPECT_THAT(
10259 getDiagnosticString(),
10260 HasSubstr("Uniform id '2' is missing Block or BufferBlock decoration"));
10261 }
10262
10263 TEST_F(ValidateDecorations, UntypedVariablePushConstantMissingBlock) {
10264 const std::string spirv = R"(
10265 OpCapability Shader
10266 OpCapability UntypedPointersKHR
10267 OpExtension "SPV_KHR_untyped_pointers"
10268 OpMemoryModel Logical GLSL450
10269 OpEntryPoint GLCompute %main "main"
10270 OpExecutionMode %main LocalSize 1 1 1
10271 OpName %struct "struct"
10272 %void = OpTypeVoid
10273 %int = OpTypeInt 32 0
10274 %struct = OpTypeStruct %int
10275 %ptr = OpTypeUntypedPointerKHR PushConstant
10276 %var = OpUntypedVariableKHR %ptr PushConstant %struct
10277 %void_fn = OpTypeFunction %void
10278 %main = OpFunction %void None %void_fn
10279 %entry = OpLabel
10280 OpReturn
10281 OpFunctionEnd
10282 )";
10283
10284 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_0);
10285 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_0));
10286 EXPECT_THAT(getDiagnosticString(),
10287 HasSubstr("PushConstant id '2' is missing Block decoration"));
10288 }
10289
10290 using UntypedVariableSetAndBinding = spvtest::ValidateBase<std::string>;
10291
10292 TEST_P(UntypedVariableSetAndBinding, MissingSet) {
10293 const auto sc = GetParam();
10294 const std::string spirv = R"(
10295 OpCapability Shader
10296 OpCapability UntypedPointersKHR
10297 OpExtension "SPV_KHR_untyped_pointers"
10298 OpExtension "SPV_KHR_storage_buffer_storage_class"
10299 OpMemoryModel Logical GLSL450
10300 OpEntryPoint GLCompute %main "main"
10301 OpExecutionMode %main LocalSize 1 1 1
10302 OpName %var "var"
10303 OpDecorate %struct Block
10304 OpMemberDecorate %struct 0 Offset 0
10305 OpDecorate %var Binding 0
10306 %void = OpTypeVoid
10307 %int = OpTypeInt 32 0
10308 %struct = OpTypeStruct %int
10309 %ptr = OpTypeUntypedPointerKHR )" +
10310 sc + R"(
10311 %var = OpUntypedVariableKHR %ptr )" + sc + R"( %struct
10312 %void_fn = OpTypeFunction %void
10313 %main = OpFunction %void None %void_fn
10314 %entry = OpLabel
10315 %load = OpLoad %struct %var
10316 OpReturn
10317 OpFunctionEnd
10318 )";
10319
10320 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_0);
10321 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_0));
10322 EXPECT_THAT(getDiagnosticString(),
10323 HasSubstr(sc + " id '2' is missing DescriptorSet decoration"));
10324 }
10325
10326 TEST_P(UntypedVariableSetAndBinding, MissingBinding) {
10327 const auto sc = GetParam();
10328 const std::string spirv = R"(
10329 OpCapability Shader
10330 OpCapability UntypedPointersKHR
10331 OpExtension "SPV_KHR_untyped_pointers"
10332 OpExtension "SPV_KHR_storage_buffer_storage_class"
10333 OpMemoryModel Logical GLSL450
10334 OpEntryPoint GLCompute %main "main"
10335 OpExecutionMode %main LocalSize 1 1 1
10336 OpName %var "var"
10337 OpDecorate %struct Block
10338 OpMemberDecorate %struct 0 Offset 0
10339 OpDecorate %var DescriptorSet 0
10340 %void = OpTypeVoid
10341 %int = OpTypeInt 32 0
10342 %struct = OpTypeStruct %int
10343 %ptr = OpTypeUntypedPointerKHR )" +
10344 sc + R"(
10345 %var = OpUntypedVariableKHR %ptr )" + sc + R"( %struct
10346 %void_fn = OpTypeFunction %void
10347 %main = OpFunction %void None %void_fn
10348 %entry = OpLabel
10349 %load = OpLoad %struct %var
10350 OpReturn
10351 OpFunctionEnd
10352 )";
10353
10354 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_0);
10355 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_0));
10356 EXPECT_THAT(getDiagnosticString(),
10357 HasSubstr(sc + " id '2' is missing Binding decoration"));
10358 }
10359
10360 INSTANTIATE_TEST_SUITE_P(ValidateUntypedVariableSetAndBinding,
10361 UntypedVariableSetAndBinding,
10362 Values("StorageBuffer", "Uniform"));
10363
10364 using UntypedPointerLayout =
10365 spvtest::ValidateBase<std::tuple<std::string, std::string>>;
10366
10367 TEST_P(UntypedPointerLayout, BadOffset) {
10368 const auto sc = std::get<0>(GetParam());
10369 const auto op = std::get<1>(GetParam());
10370 const std::string set = (sc == "StorageBuffer" || sc == "Uniform"
10371 ? R"(OpDecorate %var DescriptorSet 0
10372 OpDecorate %var Binding 0
10373 )"
10374 : R"()");
10375 const std::string spirv = R"(
10376 OpCapability Shader
10377 OpCapability VariablePointers
10378 OpCapability UntypedPointersKHR
10379 OpCapability WorkgroupMemoryExplicitLayoutKHR
10380 OpExtension "SPV_KHR_untyped_pointers"
10381 OpExtension "SPV_KHR_variable_pointers"
10382 OpExtension "SPV_KHR_workgroup_memory_explicit_layout"
10383 OpExtension "SPV_KHR_storage_buffer_storage_class"
10384 OpMemoryModel Logical GLSL450
10385 OpEntryPoint GLCompute %main "main" %var
10386 OpExecutionMode %main LocalSize 1 1 1
10387 OpName %var "var"
10388 OpDecorate %struct Block
10389 OpMemberDecorate %struct 0 Offset 0
10390 OpMemberDecorate %struct 1 Offset 4
10391 )" + set + R"(OpMemberDecorate %test_type 0 Offset 0
10392 OpMemberDecorate %test_type 1 Offset 1
10393 OpDecorate %ptr ArrayStride 16
10394 %void = OpTypeVoid
10395 %int = OpTypeInt 32 0
10396 %int_0 = OpConstant %int 0
10397 %struct = OpTypeStruct %int %int
10398 %test_type = OpTypeStruct %int %int
10399 %test_val = OpConstantNull %test_type
10400 %ptr = OpTypeUntypedPointerKHR )" +
10401 sc + R"(
10402 %var = OpUntypedVariableKHR %ptr )" +
10403 sc + R"( %struct
10404 %void_fn = OpTypeFunction %void
10405 %main = OpFunction %void None %void_fn
10406 %entry = OpLabel
10407 )" + op + R"(
10408 OpReturn
10409 OpFunctionEnd
10410 )";
10411
10412 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_2);
10413 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_2));
10414 const bool read_only = sc == "Uniform" || sc == "PushConstant";
10415 if (!read_only || op.find("OpStore") == std::string::npos) {
10416 EXPECT_THAT(getDiagnosticString(),
10417 HasSubstr("member 1 at offset 1 is not aligned to"));
10418 }
10419 }
10420
10421 TEST_P(UntypedPointerLayout, BadStride) {
10422 const auto sc = std::get<0>(GetParam());
10423 const auto op = std::get<1>(GetParam());
10424 const std::string set = (sc == "StorageBuffer" || sc == "Uniform"
10425 ? R"(OpDecorate %var DescriptorSet 0
10426 OpDecorate %var Binding 0
10427 )"
10428 : R"()");
10429 const std::string spirv = R"(
10430 OpCapability Shader
10431 OpCapability VariablePointers
10432 OpCapability UntypedPointersKHR
10433 OpCapability WorkgroupMemoryExplicitLayoutKHR
10434 OpExtension "SPV_KHR_untyped_pointers"
10435 OpExtension "SPV_KHR_variable_pointers"
10436 OpExtension "SPV_KHR_workgroup_memory_explicit_layout"
10437 OpExtension "SPV_KHR_storage_buffer_storage_class"
10438 OpMemoryModel Logical GLSL450
10439 OpEntryPoint GLCompute %main "main" %var
10440 OpExecutionMode %main LocalSize 1 1 1
10441 OpName %var "var"
10442 OpDecorate %struct Block
10443 OpMemberDecorate %struct 0 Offset 0
10444 OpMemberDecorate %struct 1 Offset 4
10445 )" + set + R"(OpDecorate %test_type ArrayStride 4
10446 OpDecorate %ptr ArrayStride 16
10447 %void = OpTypeVoid
10448 %int = OpTypeInt 32 0
10449 %int_0 = OpConstant %int 0
10450 %int_4 = OpConstant %int 4
10451 %int4 = OpTypeVector %int 4
10452 %test_type = OpTypeArray %int4 %int_4
10453 %test_val = OpConstantNull %test_type
10454 %struct = OpTypeStruct %int %int
10455 %ptr = OpTypeUntypedPointerKHR )" +
10456 sc + R"(
10457 %var = OpUntypedVariableKHR %ptr )" +
10458 sc + R"( %struct
10459 %void_fn = OpTypeFunction %void
10460 %main = OpFunction %void None %void_fn
10461 %entry = OpLabel
10462 )" + op + R"(
10463 OpReturn
10464 OpFunctionEnd
10465 )";
10466
10467 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_2);
10468 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_2));
10469 const bool read_only = sc == "Uniform" || sc == "PushConstant";
10470 if (!read_only || op.find("OpStore") == std::string::npos) {
10471 EXPECT_THAT(
10472 getDiagnosticString(),
10473 HasSubstr("array with stride 4 not satisfying alignment to 16"));
10474 }
10475 }
10476
10477 INSTANTIATE_TEST_SUITE_P(
10478 ValidateUntypedPointerLayout, UntypedPointerLayout,
10479 Combine(Values("StorageBuffer", "Uniform", "PushConstant", "Workgroup"),
10480 Values("%gep = OpUntypedAccessChainKHR %ptr %test_type %var %int_0",
10481 "%gep = OpUntypedInBoundsAccessChainKHR %ptr %test_type "
10482 "%var %int_0",
10483 "%gep = OpUntypedPtrAccessChainKHR %ptr %test_type %var "
10484 "%int_0 %int_0",
10485 "%ld = OpLoad %test_type %var", "OpStore %var %test_val")));
10486
10487 TEST_F(ValidateDecorations, UntypedArrayLengthMissingOffset) {
10488 const std::string spirv = R"(
10489 OpCapability Shader
10490 OpCapability UntypedPointersKHR
10491 OpExtension "SPV_KHR_untyped_pointers"
10492 OpMemoryModel Logical GLSL450
10493 OpEntryPoint GLCompute %main "main"
10494 OpExecutionMode %main LocalSize 1 1 1
10495 OpDecorate %struct Block
10496 OpDecorate %block Block
10497 OpMemberDecorate %block 0 Offset 0
10498 OpDecorate %array ArrayStride 4
10499 OpDecorate %var DescriptorSet 0
10500 OpDecorate %var Binding 0
10501 %void = OpTypeVoid
10502 %int = OpTypeInt 32 0
10503 %array = OpTypeRuntimeArray %int
10504 %struct = OpTypeStruct %array
10505 %block = OpTypeStruct %array
10506 %ptr = OpTypeUntypedPointerKHR StorageBuffer
10507 %var = OpUntypedVariableKHR %ptr StorageBuffer %block
10508 %void_fn = OpTypeFunction %void
10509 %main = OpFunction %void None %void_fn
10510 %entry = OpLabel
10511 %len = OpUntypedArrayLengthKHR %int %struct %var 0
10512 OpReturn
10513 OpFunctionEnd
10514 )";
10515
10516 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_2);
10517 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_2));
10518 EXPECT_THAT(getDiagnosticString(),
10519 HasSubstr("member 0 is missing an Offset decoration"));
10520 }
10521
10522 TEST_F(ValidateDecorations, ComponentMultipleArrays) {
10523 const std::string spirv = R"(
10524 OpCapability Tessellation
10525 %1 = OpExtInstImport "GLSL.std.450"
10526 OpMemoryModel Logical GLSL450
10527 OpEntryPoint TessellationEvaluation %main "main" %_ %FOO %FOO0
10528 OpExecutionMode %main Triangles
10529 OpExecutionMode %main SpacingEqual
10530 OpExecutionMode %main VertexOrderCcw
10531 OpSource GLSL 460
10532 OpSourceExtension "GL_EXT_nonuniform_qualifier"
10533 OpName %main "main"
10534 OpName %gl_PerVertex "gl_PerVertex"
10535 OpMemberName %gl_PerVertex 0 "gl_Position"
10536 OpMemberName %gl_PerVertex 1 "gl_PointSize"
10537 OpMemberName %gl_PerVertex 2 "gl_ClipDistance"
10538 OpMemberName %gl_PerVertex 3 "gl_CullDistance"
10539 OpName %_ ""
10540 OpName %FOO "FOO"
10541 OpMemberDecorate %gl_PerVertex 0 BuiltIn Position
10542 OpMemberDecorate %gl_PerVertex 1 BuiltIn PointSize
10543 OpMemberDecorate %gl_PerVertex 2 BuiltIn ClipDistance
10544 OpMemberDecorate %gl_PerVertex 3 BuiltIn CullDistance
10545 OpDecorate %gl_PerVertex Block
10546 OpDecorate %FOO Component 2
10547 OpDecorate %FOO Location 1
10548 OpDecorate %FOO0 Location 1
10549 OpDecorate %FOO0 Component 0
10550 %void = OpTypeVoid
10551 %3 = OpTypeFunction %void
10552 %float = OpTypeFloat 32
10553 %v4float = OpTypeVector %float 4
10554 %uint = OpTypeInt 32 0
10555 %uint_1 = OpConstant %uint 1
10556 %_arr_float_uint_1 = OpTypeArray %float %uint_1
10557 %gl_PerVertex = OpTypeStruct %v4float %float %_arr_float_uint_1 %_arr_float_uint_1
10558 %_ptr_Output_gl_PerVertex = OpTypePointer Output %gl_PerVertex
10559 %_ = OpVariable %_ptr_Output_gl_PerVertex Output
10560 %int = OpTypeInt 32 1
10561 %int_0 = OpConstant %int 0
10562 %v2float = OpTypeVector %float 2
10563 %uint_2 = OpConstant %uint 2
10564 %_arr_v2float_uint_2 = OpTypeArray %v2float %uint_2
10565 %uint_32 = OpConstant %uint 32
10566 %_arr__arr_v2float_uint_2_uint_32 = OpTypeArray %_arr_v2float_uint_2 %uint_32
10567 %_ptr_Input__arr__arr_v2float_uint_2_uint_32 = OpTypePointer Input %_arr__arr_v2float_uint_2_uint_32
10568 %FOO = OpVariable %_ptr_Input__arr__arr_v2float_uint_2_uint_32 Input
10569 %FOO0 = OpVariable %_ptr_Input__arr__arr_v2float_uint_2_uint_32 Input
10570 %_ptr_Input_v2float = OpTypePointer Input %v2float
10571 %int_1 = OpConstant %int 1
10572 %uint_0 = OpConstant %uint 0
10573 %_ptr_Output_float = OpTypePointer Output %float
10574 %main = OpFunction %void None %3
10575 %5 = OpLabel
10576 %24 = OpAccessChain %_ptr_Input_v2float %FOO %int_0 %int_0
10577 %25 = OpLoad %v2float %24
10578 %27 = OpAccessChain %_ptr_Input_v2float %FOO0 %int_1 %int_1
10579 %28 = OpLoad %v2float %27
10580 %29 = OpFAdd %v2float %25 %28
10581 %32 = OpAccessChain %_ptr_Output_float %_ %int_0 %uint_0
10582 %33 = OpCompositeExtract %float %29 0
10583 OpStore %32 %33
10584 %34 = OpAccessChain %_ptr_Output_float %_ %int_0 %uint_1
10585 %35 = OpCompositeExtract %float %29 1
10586 OpStore %34 %35
10587 OpReturn
10588 OpFunctionEnd
10589 )";
10590
10591 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_0);
10592 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_VULKAN_1_0));
10593 }
10594
10595 const std::string kNodeShaderPrelude = R"(
10596 OpCapability Shader
10597 OpCapability ShaderEnqueueAMDX
10598 OpExtension "SPV_AMDX_shader_enqueue"
10599 OpMemoryModel Logical GLSL450
10600 OpEntryPoint GLCompute %main "main"
10601 OpEntryPoint GLCompute %other "other"
10602 )";
10603
10604 const std::string kNodeShaderPostlude = R"(
10605 %uint = OpTypeInt 32 0
10606 %uint_0 = OpConstant %uint 0
10607 %uint_1 = OpConstant %uint 1
10608 %node0 = OpConstantStringAMDX "node0"
10609 %node1 = OpConstantStringAMDX "node1"
10610 %node2 = OpConstantStringAMDX "node2"
10611 %S = OpTypeStruct
10612 %_payloadarr_S_0 = OpTypeNodePayloadArrayAMDX %S
10613 %_payloadarr_S = OpTypeNodePayloadArrayAMDX %S
10614 %bool = OpTypeBool
10615 %true = OpConstantTrue %bool
10616 %void = OpTypeVoid
10617 %void_fn = OpTypeFunction %void
10618 %main = OpFunction %void None %void_fn
10619 %entry = OpLabel
10620 OpReturn
10621 OpFunctionEnd
10622 %other = OpFunction %void None %void_fn
10623 %entry0 = OpLabel
10624 OpReturn
10625 OpFunctionEnd
10626 )";
10627
10628 TEST_F(ValidateDecorations, NodeShader) {
10629 const std::string spirv = kNodeShaderPrelude + R"(
10630 OpExecutionModeId %main ShaderIndexAMDX %uint_0
10631 OpExecutionModeId %main IsApiEntryAMDX %true
10632 OpExecutionModeId %main MaxNodeRecursionAMDX %uint_1
10633 OpExecutionModeId %main MaxNumWorkgroupsAMDX %uint_1 %uint_1 %uint_1
10634 OpExecutionModeId %main SharesInputWithAMDX %node0 %uint_0
10635 OpExecutionModeId %other ShaderIndexAMDX %uint_0
10636 OpExecutionModeId %other StaticNumWorkgroupsAMDX %uint_1 %uint_1 %uint_1
10637 OpDecorateId %_payloadarr_S PayloadNodeNameAMDX %node1
10638 OpDecorateId %_payloadarr_S_0 PayloadNodeNameAMDX %node2
10639 OpDecorateId %_payloadarr_S PayloadNodeBaseIndexAMDX %uint_0
10640 OpDecorateId %_payloadarr_S PayloadNodeArraySizeAMDX %uint_1
10641 OpDecorateId %_payloadarr_S NodeSharesPayloadLimitsWithAMDX %_payloadarr_S_0
10642 )" + kNodeShaderPostlude;
10643
10644 spv_target_env env = SPV_ENV_UNIVERSAL_1_3;
10645 CompileSuccessfully(spirv, env);
10646 EXPECT_THAT(SPV_SUCCESS, ValidateInstructions(env));
10647 }
10648
10649 TEST_F(ValidateDecorations, NodeShaderDecoratePayloadNodeName) {
10650 const std::string spirv = kNodeShaderPrelude + R"(
10651 OpExecutionModeId %main ShaderIndexAMDX %uint_0
10652 OpExecutionModeId %main IsApiEntryAMDX %true
10653 OpExecutionModeId %main MaxNodeRecursionAMDX %uint_1
10654 OpExecutionModeId %main MaxNumWorkgroupsAMDX %uint_1 %uint_1 %uint_1
10655 OpExecutionModeId %main SharesInputWithAMDX %node0 %uint_0
10656 OpExecutionModeId %other ShaderIndexAMDX %uint_0
10657 OpExecutionModeId %other StaticNumWorkgroupsAMDX %uint_1 %uint_1 %uint_1
10658 OpDecorate %_payloadarr_S PayloadNodeNameAMDX %node1
10659 OpDecorate %_payloadarr_S_0 PayloadNodeNameAMDX %node2
10660 OpDecorateId %_payloadarr_S PayloadNodeBaseIndexAMDX %uint_0
10661 OpDecorateId %_payloadarr_S PayloadNodeArraySizeAMDX %uint_1
10662 OpDecorateId %_payloadarr_S NodeSharesPayloadLimitsWithAMDX %_payloadarr_S_0
10663 )" + kNodeShaderPostlude;
10664
10665 spv_target_env env = SPV_ENV_UNIVERSAL_1_3;
10666 CompileSuccessfully(spirv, env);
10667 EXPECT_THAT(SPV_ERROR_INVALID_ID, ValidateInstructions(env));
10668 EXPECT_THAT(
10669 getDiagnosticString(),
10670 HasSubstr(
10671 "Decorations taking ID parameters may not be used with OpDecorate"));
10672 }
10673
10674 TEST_F(ValidateDecorations, NodeShaderDecoratePayloadNodeBaseIndex) {
10675 const std::string spirv = kNodeShaderPrelude + R"(
10676 OpExecutionModeId %main ShaderIndexAMDX %uint_0
10677 OpExecutionModeId %main IsApiEntryAMDX %true
10678 OpExecutionModeId %main MaxNodeRecursionAMDX %uint_1
10679 OpExecutionModeId %main MaxNumWorkgroupsAMDX %uint_1 %uint_1 %uint_1
10680 OpExecutionModeId %main SharesInputWithAMDX %node0 %uint_0
10681 OpExecutionModeId %other ShaderIndexAMDX %uint_0
10682 OpExecutionModeId %other StaticNumWorkgroupsAMDX %uint_1 %uint_1 %uint_1
10683 OpDecorateId %_payloadarr_S PayloadNodeNameAMDX %node1
10684 OpDecorateId %_payloadarr_S_0 PayloadNodeNameAMDX %node2
10685 OpDecorate %_payloadarr_S PayloadNodeBaseIndexAMDX %uint_0
10686 OpDecorateId %_payloadarr_S PayloadNodeArraySizeAMDX %uint_1
10687 OpDecorateId %_payloadarr_S NodeSharesPayloadLimitsWithAMDX %_payloadarr_S_0
10688 )" + kNodeShaderPostlude;
10689
10690 spv_target_env env = SPV_ENV_UNIVERSAL_1_3;
10691 CompileSuccessfully(spirv, env);
10692 EXPECT_THAT(SPV_ERROR_INVALID_ID, ValidateInstructions(env));
10693 EXPECT_THAT(
10694 getDiagnosticString(),
10695 HasSubstr(
10696 "Decorations taking ID parameters may not be used with OpDecorate"));
10697 }
10698
10699 TEST_F(ValidateDecorations, NodeShaderDecoratePayloadNodeArraySize) {
10700 const std::string spirv = kNodeShaderPrelude + R"(
10701 OpExecutionModeId %main ShaderIndexAMDX %uint_0
10702 OpExecutionModeId %main IsApiEntryAMDX %true
10703 OpExecutionModeId %main MaxNodeRecursionAMDX %uint_1
10704 OpExecutionModeId %main MaxNumWorkgroupsAMDX %uint_1 %uint_1 %uint_1
10705 OpExecutionModeId %main SharesInputWithAMDX %node0 %uint_0
10706 OpExecutionModeId %other ShaderIndexAMDX %uint_0
10707 OpExecutionModeId %other StaticNumWorkgroupsAMDX %uint_1 %uint_1 %uint_1
10708 OpDecorateId %_payloadarr_S PayloadNodeNameAMDX %node1
10709 OpDecorateId %_payloadarr_S_0 PayloadNodeNameAMDX %node2
10710 OpDecorateId %_payloadarr_S PayloadNodeBaseIndexAMDX %uint_0
10711 OpDecorate %_payloadarr_S PayloadNodeArraySizeAMDX %uint_1
10712 OpDecorateId %_payloadarr_S NodeSharesPayloadLimitsWithAMDX %_payloadarr_S_0
10713 )" + kNodeShaderPostlude;
10714
10715 spv_target_env env = SPV_ENV_UNIVERSAL_1_3;
10716 CompileSuccessfully(spirv, env);
10717 EXPECT_THAT(SPV_ERROR_INVALID_ID, ValidateInstructions(env));
10718 EXPECT_THAT(
10719 getDiagnosticString(),
10720 HasSubstr(
10721 "Decorations taking ID parameters may not be used with OpDecorate"));
10722 }
10723
10724 TEST_F(ValidateDecorations, NodeShaderDecorateNodeSharesPayloadLimitsWith) {
10725 const std::string spirv = kNodeShaderPrelude + R"(
10726 OpExecutionModeId %main ShaderIndexAMDX %uint_0
10727 OpExecutionModeId %main IsApiEntryAMDX %true
10728 OpExecutionModeId %main MaxNodeRecursionAMDX %uint_1
10729 OpExecutionModeId %main MaxNumWorkgroupsAMDX %uint_1 %uint_1 %uint_1
10730 OpExecutionModeId %main SharesInputWithAMDX %node0 %uint_0
10731 OpExecutionModeId %other ShaderIndexAMDX %uint_0
10732 OpExecutionModeId %other StaticNumWorkgroupsAMDX %uint_1 %uint_1 %uint_1
10733 OpDecorateId %_payloadarr_S PayloadNodeNameAMDX %node1
10734 OpDecorateId %_payloadarr_S_0 PayloadNodeNameAMDX %node2
10735 OpDecorateId %_payloadarr_S PayloadNodeBaseIndexAMDX %uint_0
10736 OpDecorateId %_payloadarr_S PayloadNodeArraySizeAMDX %uint_1
10737 OpDecorate %_payloadarr_S NodeSharesPayloadLimitsWithAMDX %_payloadarr_S_0
10738 )" + kNodeShaderPostlude;
10739
10740 spv_target_env env = SPV_ENV_UNIVERSAL_1_3;
10741 CompileSuccessfully(spirv, env);
10742 EXPECT_THAT(SPV_ERROR_INVALID_ID, ValidateInstructions(env));
10743 EXPECT_THAT(
10744 getDiagnosticString(),
10745 HasSubstr(
10746 "Decorations taking ID parameters may not be used with OpDecorate"));
10747 }
10748
10749 TEST_F(ValidateDecorations, BlockArrayWithStride) {
10750 const std::string spirv = R"(
10751 OpCapability Shader
10752 OpCapability Linkage
10753 OpMemoryModel Logical GLSL450
10754 OpDecorate %struct Block
10755 OpMemberDecorate %struct 0 Offset 0
10756 OpDecorate %array ArrayStride 4
10757 %int = OpTypeInt 32 0
10758 %int_4 = OpConstant %int 4
10759 %struct = OpTypeStruct %int
10760 %array = OpTypeArray %struct %int_4
10761 )";
10762
10763 CompileSuccessfully(spirv);
10764 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
10765 EXPECT_THAT(getDiagnosticString(),
10766 HasSubstr("Array containing a Block or BufferBlock must not be "
10767 "decorated with ArrayStride"));
10768 }
10769
10770 TEST_F(ValidateDecorations, BufferBlockRuntimeArrayWithStride) {
10771 const std::string spirv = R"(
10772 OpCapability Shader
10773 OpCapability Linkage
10774 OpMemoryModel Logical GLSL450
10775 OpDecorate %struct BufferBlock
10776 OpMemberDecorate %struct 0 Offset 0
10777 OpDecorate %array ArrayStride 4
10778 %int = OpTypeInt 32 0
10779 %struct = OpTypeStruct %int
10780 %array = OpTypeRuntimeArray %struct
10781 )";
10782
10783 CompileSuccessfully(spirv);
10784 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
10785 EXPECT_THAT(getDiagnosticString(),
10786 HasSubstr("Array containing a Block or BufferBlock must not be "
10787 "decorated with ArrayStride"));
10788 }
10789
10790 TEST_F(ValidateDecorations, BlockArrayWithoutStride) {
10791 const std::string spirv = R"(
10792 OpCapability Shader
10793 OpExtension "SPV_KHR_storage_buffer_storage_class"
10794 OpMemoryModel Logical GLSL450
10795 OpEntryPoint GLCompute %main "main"
10796 OpExecutionMode %main LocalSize 1 1 1
10797 OpDecorate %struct Block
10798 OpMemberDecorate %struct 0 Offset 0
10799 OpDecorate %var DescriptorSet 0
10800 OpDecorate %var Binding 0
10801 %int = OpTypeInt 32 0
10802 %int_4 = OpConstant %int 4
10803 %struct = OpTypeStruct %int
10804 %array = OpTypeArray %struct %int_4
10805 %ptr = OpTypePointer StorageBuffer %array
10806 %var = OpVariable %ptr StorageBuffer
10807 %void = OpTypeVoid
10808 %void_fn = OpTypeFunction %void
10809 %main = OpFunction %void None %void_fn
10810 %entry = OpLabel
10811 OpReturn
10812 OpFunctionEnd
10813 )";
10814
10815 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_0);
10816 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_VULKAN_1_0));
10817 }
10818
10819 TEST_F(ValidateDecorations, BlockArrayWithoutStrideUntypedAccessChain) {
10820 const std::string spirv = R"(
10821 OpCapability Shader
10822 OpCapability UntypedPointersKHR
10823 OpExtension "SPV_KHR_storage_buffer_storage_class"
10824 OpExtension "SPV_KHR_untyped_pointers"
10825 OpMemoryModel Logical GLSL450
10826 OpEntryPoint GLCompute %main "main"
10827 OpExecutionMode %main LocalSize 1 1 1
10828 OpDecorate %struct Block
10829 OpMemberDecorate %struct 0 Offset 0
10830 OpDecorate %var DescriptorSet 0
10831 OpDecorate %var Binding 0
10832 %int = OpTypeInt 32 0
10833 %int_4 = OpConstant %int 4
10834 %struct = OpTypeStruct %int
10835 %array = OpTypeArray %struct %int_4
10836 %void = OpTypeVoid
10837 %ptr = OpTypeUntypedPointerKHR StorageBuffer
10838 %var = OpUntypedVariableKHR %ptr StorageBuffer %array
10839 %void_fn = OpTypeFunction %void
10840 %main = OpFunction %void None %void_fn
10841 %entry = OpLabel
10842 %gep = OpUntypedAccessChainKHR %ptr %array %var
10843 OpReturn
10844 OpFunctionEnd
10845 )";
10846
10847 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_0);
10848 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_VULKAN_1_0));
10849 }
10850
10851 TEST_F(ValidateDecorations, InvalidLayoutBlockFunctionPre1p4) {
10852 const std::string spirv = R"(
10853 OpCapability Shader
10854 OpMemoryModel Logical GLSL450
10855 OpEntryPoint GLCompute %main "main"
10856 OpExecutionMode %main LocalSize 1 1 1
10857 OpDecorate %block Block
10858 %void = OpTypeVoid
10859 %int = OpTypeInt 32 0
10860 %block = OpTypeStruct %int
10861 %ptr_function_block = OpTypePointer Function %block
10862 %void_fn = OpTypeFunction %void
10863 %main = OpFunction %void None %void_fn
10864 %entry = OpLabel
10865 %var = OpVariable %ptr_function_block Function
10866 OpReturn
10867 OpFunctionEnd
10868 )";
10869
10870 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_4);
10871 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_VULKAN_1_2));
10872 }
10873
10874 TEST_F(ValidateDecorations, InvalidLayoutBlockFunctionPost1p4) {
10875 const std::string spirv = R"(
10876 OpCapability Shader
10877 OpMemoryModel Logical GLSL450
10878 OpEntryPoint GLCompute %main "main"
10879 OpExecutionMode %main LocalSize 1 1 1
10880 OpDecorate %block Block
10881 %void = OpTypeVoid
10882 %int = OpTypeInt 32 0
10883 %block = OpTypeStruct %int
10884 %ptr_function_block = OpTypePointer Function %block
10885 %void_fn = OpTypeFunction %void
10886 %main = OpFunction %void None %void_fn
10887 %entry = OpLabel
10888 %var = OpVariable %ptr_function_block Function
10889 OpReturn
10890 OpFunctionEnd
10891 )";
10892
10893 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_5);
10894 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_3));
10895 EXPECT_THAT(getDiagnosticString(),
10896 AnyVUID("VUID-StandaloneSpirv-None-10684"));
10897 EXPECT_THAT(
10898 getDiagnosticString(),
10899 HasSubstr("Invalid explicit layout decorations on type for operand"));
10900 }
10901
10902 TEST_F(ValidateDecorations, InvalidLayoutOffsetPrivatePre1p4) {
10903 const std::string spirv = R"(
10904 OpCapability Shader
10905 OpMemoryModel Logical GLSL450
10906 OpEntryPoint GLCompute %main "main"
10907 OpExecutionMode %main LocalSize 1 1 1
10908 OpMemberDecorate %block 0 Offset 0
10909 %void = OpTypeVoid
10910 %int = OpTypeInt 32 0
10911 %block = OpTypeStruct %int
10912 %ptr_private_block = OpTypePointer Private %block
10913 %void_fn = OpTypeFunction %void
10914 %var = OpVariable %ptr_private_block Private
10915 %main = OpFunction %void None %void_fn
10916 %entry = OpLabel
10917 OpReturn
10918 OpFunctionEnd
10919 )";
10920
10921 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_0);
10922 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_VULKAN_1_0));
10923 }
10924
10925 TEST_F(ValidateDecorations, InvalidLayoutOffsetPrivatePost1p4) {
10926 const std::string spirv = R"(
10927 OpCapability Shader
10928 OpMemoryModel Logical GLSL450
10929 OpEntryPoint GLCompute %main "main"
10930 OpExecutionMode %main LocalSize 1 1 1
10931 OpMemberDecorate %block 0 Offset 0
10932 %void = OpTypeVoid
10933 %int = OpTypeInt 32 0
10934 %block = OpTypeStruct %int
10935 %ptr_private_block = OpTypePointer Private %block
10936 %void_fn = OpTypeFunction %void
10937 %var = OpVariable %ptr_private_block Private
10938 %main = OpFunction %void None %void_fn
10939 %entry = OpLabel
10940 OpReturn
10941 OpFunctionEnd
10942 )";
10943
10944 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_3);
10945 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_3));
10946 EXPECT_THAT(getDiagnosticString(),
10947 AnyVUID("VUID-StandaloneSpirv-None-10684"));
10948 EXPECT_THAT(
10949 getDiagnosticString(),
10950 HasSubstr("Invalid explicit layout decorations on type for operand"));
10951 }
10952
10953 TEST_F(ValidateDecorations, InvalidLayoutArrayStrideWorkgroupExplicitLayout) {
10954 const std::string spirv = R"(
10955 OpCapability Shader
10956 OpCapability WorkgroupMemoryExplicitLayoutKHR
10957 OpExtension "SPV_KHR_workgroup_memory_explicit_layout"
10958 OpMemoryModel Logical GLSL450
10959 OpEntryPoint GLCompute %main "main"
10960 OpExecutionMode %main LocalSize 1 1 1
10961 OpDecorate %array ArrayStride 4
10962 %void = OpTypeVoid
10963 %int = OpTypeInt 32 0
10964 %int_4 = OpConstant %int 4
10965 %array = OpTypeArray %int %int_4
10966 %ptr_wg_block = OpTypePointer Workgroup %array
10967 %void_fn = OpTypeFunction %void
10968 %var = OpVariable %ptr_wg_block Workgroup
10969 %main = OpFunction %void None %void_fn
10970 %entry = OpLabel
10971 OpReturn
10972 OpFunctionEnd
10973 )";
10974
10975 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_3);
10976 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_VULKAN_1_3));
10977 }
10978
10979 TEST_F(ValidateDecorations, InvalidLayoutArrayStrideWorkgroup) {
10980 const std::string spirv = R"(
10981 OpCapability Shader
10982 OpMemoryModel Logical GLSL450
10983 OpEntryPoint GLCompute %main "main"
10984 OpExecutionMode %main LocalSize 1 1 1
10985 OpDecorate %array ArrayStride 4
10986 %void = OpTypeVoid
10987 %int = OpTypeInt 32 0
10988 %int_4 = OpConstant %int 4
10989 %array = OpTypeArray %int %int_4
10990 %ptr_wg_block = OpTypePointer Workgroup %array
10991 %void_fn = OpTypeFunction %void
10992 %var = OpVariable %ptr_wg_block Workgroup
10993 %main = OpFunction %void None %void_fn
10994 %entry = OpLabel
10995 OpReturn
10996 OpFunctionEnd
10997 )";
10998
10999 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_0);
11000 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_0));
11001 EXPECT_THAT(getDiagnosticString(),
11002 AnyVUID("VUID-StandaloneSpirv-None-10684"));
11003 EXPECT_THAT(
11004 getDiagnosticString(),
11005 HasSubstr("Invalid explicit layout decorations on type for operand"));
11006 }
11007
11008 TEST_F(ValidateDecorations, InvalidLayoutArrayStrideUniformConstant) {
11009 const std::string spirv = R"(
11010 OpCapability Shader
11011 OpMemoryModel Logical GLSL450
11012 OpEntryPoint GLCompute %main "main"
11013 OpExecutionMode %main LocalSize 1 1 1
11014 OpDecorate %array ArrayStride 4
11015 OpDecorate %var DescriptorSet 0
11016 OpDecorate %var Binding 0
11017 %void = OpTypeVoid
11018 %int = OpTypeInt 32 0
11019 %int_4 = OpConstant %int 4
11020 %sampler = OpTypeSampler
11021 %array = OpTypeArray %sampler %int_4
11022 %ptr_uc_block = OpTypePointer UniformConstant %array
11023 %void_fn = OpTypeFunction %void
11024 %var = OpVariable %ptr_uc_block UniformConstant
11025 %main = OpFunction %void None %void_fn
11026 %entry = OpLabel
11027 OpReturn
11028 OpFunctionEnd
11029 )";
11030
11031 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_0);
11032 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_0));
11033 EXPECT_THAT(getDiagnosticString(),
11034 AnyVUID("VUID-StandaloneSpirv-None-10684"));
11035 EXPECT_THAT(
11036 getDiagnosticString(),
11037 HasSubstr("Invalid explicit layout decorations on type for operand"));
11038 }
11039
11040 TEST_F(ValidateDecorations, InvalidLayoutMatrixStrideFunctionPost1p4) {
11041 const std::string spirv = R"(
11042 OpCapability Shader
11043 OpMemoryModel Logical GLSL450
11044 OpEntryPoint GLCompute %main "main"
11045 OpExecutionMode %main LocalSize 1 1 1
11046 OpMemberDecorate %block 0 MatrixStride 16
11047 %void = OpTypeVoid
11048 %float = OpTypeFloat 32
11049 %v4float = OpTypeVector %float 4
11050 %mat4x4 = OpTypeMatrix %v4float 4
11051 %block = OpTypeStruct %mat4x4
11052 %ptr_function_block = OpTypePointer Function %block
11053 %void_fn = OpTypeFunction %void
11054 %main = OpFunction %void None %void_fn
11055 %entry = OpLabel
11056 %var = OpVariable %ptr_function_block Function
11057 OpReturn
11058 OpFunctionEnd
11059 )";
11060
11061 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_3);
11062 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_3));
11063 EXPECT_THAT(getDiagnosticString(),
11064 AnyVUID("VUID-StandaloneSpirv-None-10684"));
11065 EXPECT_THAT(
11066 getDiagnosticString(),
11067 HasSubstr("Invalid explicit layout decorations on type for operand"));
11068 }
11069
11070 TEST_F(ValidateDecorations, InvalidLayoutNestedMatrixStrideFunctionPost1p4) {
11071 const std::string spirv = R"(
11072 OpCapability Shader
11073 OpMemoryModel Logical GLSL450
11074 OpEntryPoint GLCompute %main "main"
11075 OpExecutionMode %main LocalSize 1 1 1
11076 OpMemberDecorate %block 0 MatrixStride 16
11077 %void = OpTypeVoid
11078 %float = OpTypeFloat 32
11079 %v4float = OpTypeVector %float 4
11080 %mat4x4 = OpTypeMatrix %v4float 4
11081 %block = OpTypeStruct %mat4x4
11082 %block2 = OpTypeStruct %block
11083 %int = OpTypeInt 32 0
11084 %int_2 = OpConstant %int 2
11085 %array = OpTypeArray %block2 %int_2
11086 %ptr_function_array = OpTypePointer Function %array
11087 %void_fn = OpTypeFunction %void
11088 %main = OpFunction %void None %void_fn
11089 %entry = OpLabel
11090 %var = OpVariable %ptr_function_array Function
11091 OpReturn
11092 OpFunctionEnd
11093 )";
11094
11095 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_3);
11096 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_3));
11097 EXPECT_THAT(getDiagnosticString(),
11098 AnyVUID("VUID-StandaloneSpirv-None-10684"));
11099 EXPECT_THAT(
11100 getDiagnosticString(),
11101 HasSubstr("Invalid explicit layout decorations on type for operand"));
11102 }
11103
11104 TEST_F(ValidateDecorations, InvalidLayoutBufferBlockWorkgroup) {
11105 const std::string spirv = R"(
11106 OpCapability Shader
11107 OpMemoryModel Logical GLSL450
11108 OpEntryPoint GLCompute %main "main"
11109 OpExecutionMode %main LocalSize 1 1 1
11110 OpDecorate %block BufferBlock
11111 %void = OpTypeVoid
11112 %int = OpTypeInt 32 0
11113 %block = OpTypeStruct %int
11114 %ptr_wg_block = OpTypePointer Workgroup %block
11115 %void_fn = OpTypeFunction %void
11116 %var = OpVariable %ptr_wg_block Workgroup
11117 %main = OpFunction %void None %void_fn
11118 %entry = OpLabel
11119 OpReturn
11120 OpFunctionEnd
11121 )";
11122
11123 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_0);
11124 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_0));
11125 EXPECT_THAT(getDiagnosticString(),
11126 AnyVUID("VUID-StandaloneSpirv-None-10684"));
11127 EXPECT_THAT(
11128 getDiagnosticString(),
11129 HasSubstr("Invalid explicit layout decorations on type for operand"));
11130 }
11131
11132 TEST_F(ValidateDecorations, InvalidLayoutUntypedStore) {
11133 const std::string spirv = R"(
11134 OpCapability Shader
11135 OpCapability UntypedPointersKHR
11136 OpExtension "SPV_KHR_untyped_pointers"
11137 OpExtension "SPV_KHR_storage_buffer_storage_class"
11138 OpMemoryModel Logical GLSL450
11139 OpEntryPoint GLCompute %main "main"
11140 OpExecutionMode %main LocalSize 1 1 1
11141 OpDecorate %block Block
11142 OpMemberDecorate %block 0 Offset 0
11143 OpDecorate %var DescriptorSet 0
11144 OpDecorate %var Binding 0
11145 %void = OpTypeVoid
11146 %int = OpTypeInt 32 0
11147 %block = OpTypeStruct %int
11148 %block_null = OpConstantNull %block
11149 %ptr = OpTypeUntypedPointerKHR StorageBuffer
11150 %var = OpUntypedVariableKHR %ptr StorageBuffer %block
11151 %void_fn = OpTypeFunction %void
11152 %main = OpFunction %void None %void_fn
11153 %entry = OpLabel
11154 OpStore %var %block_null
11155 OpReturn
11156 OpFunctionEnd
11157 )";
11158
11159 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_0);
11160 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_VULKAN_1_0));
11161 }
11162
11163 TEST_F(ValidateDecorations, ExplicitLayoutOnPtrPhysicalStorageBuffer) {
11164 const std::string spirv = R"(
11165 OpCapability PhysicalStorageBufferAddresses
11166 OpCapability Int64
11167 OpCapability Shader
11168 OpExtension "SPV_KHR_physical_storage_buffer"
11169 OpMemoryModel PhysicalStorageBuffer64 GLSL450
11170 OpEntryPoint GLCompute %main "main"
11171 OpExecutionMode %main LocalSize 1 1 1
11172 OpDecorate %_ptr_PhysicalStorageBuffer_int ArrayStride 4
11173 %void = OpTypeVoid
11174 %void_fn = OpTypeFunction %void
11175 %int = OpTypeInt 32 1
11176 %_ptr_PhysicalStorageBuffer_int = OpTypePointer PhysicalStorageBuffer %int ; ArrayStride 4
11177 %Foo = OpTypeStruct %_ptr_PhysicalStorageBuffer_int
11178 %_ptr_Function_Foo = OpTypePointer Function %Foo
11179 %int_0 = OpConstant %int 0
11180 %_ptr_Function__ptr_PhysicalStorageBuffer_int = OpTypePointer Function %_ptr_PhysicalStorageBuffer_int
11181 %ulong = OpTypeInt 64 0
11182 %ulong_0 = OpConstant %ulong 0
11183 %main = OpFunction %void None %void_fn
11184 %entry = OpLabel
11185 %obj = OpVariable %_ptr_Function_Foo Function
11186 %obj_member = OpAccessChain %_ptr_Function__ptr_PhysicalStorageBuffer_int %obj %int_0
11187 %nullptr = OpConvertUToPtr %_ptr_PhysicalStorageBuffer_int %ulong_0
11188 OpStore %obj_member %nullptr
11189 OpReturn
11190 OpFunctionEnd
11191 )";
11192
11193 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_5);
11194 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_VULKAN_1_2));
11195 }
11196
11197 TEST_F(ValidateDecorations, RuntimeArrayNotLargestOffsetInBlock) {
11198 const std::string spirv = R"(
11199 OpCapability Shader
11200 OpExtension "SPV_KHR_storage_buffer_storage_class"
11201 OpMemoryModel Logical GLSL450
11202 OpEntryPoint GLCompute %main "main"
11203 OpExecutionMode %main LocalSize 1 1 1
11204 OpDecorate %var DescriptorSet 0
11205 OpDecorate %var Binding 0
11206 OpDecorate %block Block
11207 OpMemberDecorate %block 0 Offset 16
11208 OpMemberDecorate %block 1 Offset 0
11209 %void = OpTypeVoid
11210 %void_fn = OpTypeFunction %void
11211 %int = OpTypeInt 32 0
11212 %array = OpTypeRuntimeArray %int
11213 %block = OpTypeStruct %int %array
11214 %ptr = OpTypePointer StorageBuffer %block
11215 %var = OpVariable %ptr StorageBuffer
11216 %main = OpFunction %void None %void_fn
11217 %entry = OpLabel
11218 OpReturn
11219 OpFunctionEnd
11220 )";
11221
11222 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_0);
11223 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_0));
11224 EXPECT_THAT(getDiagnosticString(),
11225 HasSubstr("has a runtime array at offset 0, but other members at "
11226 "larger offsets"));
11227 EXPECT_THAT(getDiagnosticString(),
11228 AnyVUID("VUID-StandaloneSpirv-OpTypeRuntimeArray-04680"));
11229 }
11230
11231 TEST_F(ValidateDecorations, RuntimeArrayNotLargestOffsetInBufferBlock) {
11232 const std::string spirv = R"(
11233 OpCapability Shader
11234 OpMemoryModel Logical GLSL450
11235 OpEntryPoint GLCompute %main "main"
11236 OpExecutionMode %main LocalSize 1 1 1
11237 OpDecorate %var DescriptorSet 0
11238 OpDecorate %var Binding 0
11239 OpDecorate %block BufferBlock
11240 OpMemberDecorate %block 0 Offset 16
11241 OpMemberDecorate %block 1 Offset 0
11242 %void = OpTypeVoid
11243 %void_fn = OpTypeFunction %void
11244 %int = OpTypeInt 32 0
11245 %array = OpTypeRuntimeArray %int
11246 %block = OpTypeStruct %int %array
11247 %ptr = OpTypePointer Uniform %block
11248 %var = OpVariable %ptr Uniform
11249 %main = OpFunction %void None %void_fn
11250 %entry = OpLabel
11251 OpReturn
11252 OpFunctionEnd
11253 )";
11254
11255 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_0);
11256 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_0));
11257 EXPECT_THAT(getDiagnosticString(),
11258 HasSubstr("has a runtime array at offset 0, but other members at "
11259 "larger offsets"));
11260 EXPECT_THAT(getDiagnosticString(),
11261 AnyVUID("VUID-StandaloneSpirv-OpTypeRuntimeArray-04680"));
11262 }
11263
11264 TEST_F(ValidateDecorations, LongVectorUniformPass) {
11265 const std::string spirv = R"(
11266 OpCapability Shader
11267 OpCapability Int8
11268 OpCapability UniformAndStorageBuffer8BitAccess
11269 OpCapability LongVectorEXT
11270
11271 OpExtension "SPV_KHR_8bit_storage"
11272 OpExtension "SPV_EXT_long_vector"
11273
11274 OpMemoryModel Logical GLSL450
11275 OpEntryPoint Vertex %BP_main "main"
11276
11277 OpDecorate %input0 DescriptorSet 0
11278 OpDecorate %input0 Binding 0
11279 OpDecorate %a10testtype ArrayStride 12
11280 OpDecorate %buf BufferBlock
11281 OpMemberDecorate %buf 0 Offset 0
11282
11283 %void = OpTypeVoid
11284 %bool = OpTypeBool
11285 %u32 = OpTypeInt 32 0
11286 %voidf = OpTypeFunction %void
11287 %c_u32_10 = OpConstant %u32 10
11288 %vectorSizeConst = OpConstant %u32 12
11289
11290 %scalartype = OpTypeInt 8 1
11291 %testtype = OpTypeVectorIdEXT %scalartype %vectorSizeConst
11292
11293 %a10testtype = OpTypeArray %testtype %c_u32_10
11294 %buf = OpTypeStruct %a10testtype
11295 %bufptr = OpTypePointer Uniform %buf
11296
11297 %input0 = OpVariable %bufptr Uniform
11298
11299
11300 %BP_main = OpFunction %void None %voidf
11301 %BP_label = OpLabel
11302 OpReturn
11303 OpFunctionEnd
11304
11305 )";
11306
11307 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_0);
11308 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_VULKAN_1_0));
11309 }
11310
11311 TEST_F(ValidateDecorations, LongVectorUniformSpecConstantFail) {
11312 const std::string spirv = R"(
11313 OpCapability Shader
11314 OpCapability LongVectorEXT
11315
11316 OpExtension "SPV_EXT_long_vector"
11317
11318 OpMemoryModel Logical GLSL450
11319 OpEntryPoint Vertex %BP_main "main"
11320
11321 OpDecorate %input0 DescriptorSet 0
11322 OpDecorate %input0 Binding 0
11323 OpDecorate %buf BufferBlock
11324 OpMemberDecorate %buf 0 Offset 0
11325 OpDecorate %spec_const SpecId 1
11326
11327 %void = OpTypeVoid
11328 %bool = OpTypeBool
11329 %u32 = OpTypeInt 32 0
11330 %voidf = OpTypeFunction %void
11331 %spec_const = OpSpecConstant %u32 12
11332
11333 %scalartype = OpTypeInt 32 1
11334 %testtype = OpTypeVectorIdEXT %scalartype %spec_const
11335
11336 %buf = OpTypeStruct %testtype
11337 %bufptr = OpTypePointer Uniform %buf
11338
11339 %input0 = OpVariable %bufptr Uniform
11340
11341
11342 %BP_main = OpFunction %void None %voidf
11343 %BP_label = OpLabel
11344 OpReturn
11345 OpFunctionEnd
11346
11347 )";
11348
11349 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_0);
11350 ASSERT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_0));
11351 EXPECT_THAT(getDiagnosticString(),
11352 AnyVUID("VUID-StandaloneSpirv-Function-12294"));
11353 }
11354
11355 TEST_F(ValidateDecorations, LongVectorWorkgroupSpecConstantFail) {
11356 const std::string spirv = R"(
11357 OpCapability Shader
11358 OpCapability LongVectorEXT
11359 OpCapability WorkgroupMemoryExplicitLayoutKHR
11360 OpExtension "SPV_KHR_workgroup_memory_explicit_layout"
11361
11362 OpExtension "SPV_EXT_long_vector"
11363
11364 OpMemoryModel Logical GLSL450
11365 OpEntryPoint Vertex %BP_main "main"
11366
11367 OpDecorate %spec_const SpecId 1
11368 OpMemberDecorate %str 0 Offset 0
11369 OpDecorate %str Block
11370
11371 %void = OpTypeVoid
11372 %bool = OpTypeBool
11373 %u32 = OpTypeInt 32 0
11374 %voidf = OpTypeFunction %void
11375 %spec_const = OpSpecConstant %u32 12
11376
11377 %scalartype = OpTypeInt 32 1
11378 %testtype = OpTypeVectorIdEXT %scalartype %spec_const
11379
11380 %str = OpTypeStruct %testtype
11381 %ptr_str_Workgroup = OpTypePointer Workgroup %str
11382 %var = OpVariable %ptr_str_Workgroup Workgroup
11383
11384 %BP_main = OpFunction %void None %voidf
11385 %BP_label = OpLabel
11386 OpReturn
11387 OpFunctionEnd
11388
11389 )";
11390
11391 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_3);
11392 ASSERT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_3));
11393 EXPECT_THAT(getDiagnosticString(),
11394 AnyVUID("VUID-StandaloneSpirv-Function-12294"));
11395 }
11396
11397 } // namespace
11398 } // namespace val
11399 } // namespace spvtools