Debian Codegraph @

third_party/spirv-tools/test/val/val_decoration_test.cpp

raw
1 // Copyright (c) 2017 Google Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 // Validation tests for decorations
16
17 #include <string>
18 #include <vector>
19
20 #include "gmock/gmock.h"
21 #include "source/val/decoration.h"
22 #include "test/test_fixture.h"
23 #include "test/unit_spirv.h"
24 #include "test/val/val_code_generator.h"
25 #include "test/val/val_fixtures.h"
26
27 namespace spvtools {
28 namespace val {
29 namespace {
30
31 using ::testing::Combine;
32 using ::testing::Eq;
33 using ::testing::HasSubstr;
34 using ::testing::Values;
35
36 struct TestResult {
37 TestResult(spv_result_t in_validation_result = SPV_SUCCESS,
38 const std::string& in_error_str = "")
39 : validation_result(in_validation_result), error_str(in_error_str) {}
40 spv_result_t validation_result;
41 const std::string error_str;
42 };
43
44 using ValidateDecorations = spvtest::ValidateBase<bool>;
45 using ValidateWebGPUCombineDecorationResult =
46 spvtest::ValidateBase<std::tuple<const char*, TestResult>>;
47
48 TEST_F(ValidateDecorations, ValidateOpDecorateRegistration) {
49 std::string spirv = R"(
50 OpCapability Shader
51 OpCapability Linkage
52 OpMemoryModel Logical GLSL450
53 OpDecorate %1 ArrayStride 4
54 OpDecorate %1 RelaxedPrecision
55 %2 = OpTypeFloat 32
56 %1 = OpTypeRuntimeArray %2
57 ; Since %1 is used first in Decoration, it gets id 1.
58 )";
59 const uint32_t id = 1;
60 CompileSuccessfully(spirv);
61 EXPECT_EQ(SPV_SUCCESS, ValidateAndRetrieveValidationState());
62 // Must have 2 decorations.
63 EXPECT_THAT(
64 vstate_->id_decorations(id),
65 Eq(std::vector<Decoration>{Decoration(SpvDecorationArrayStride, {4}),
66 Decoration(SpvDecorationRelaxedPrecision)}));
67 }
68
69 TEST_F(ValidateDecorations, ValidateOpMemberDecorateRegistration) {
70 std::string spirv = R"(
71 OpCapability Shader
72 OpCapability Linkage
73 OpMemoryModel Logical GLSL450
74 OpDecorate %_arr_double_uint_6 ArrayStride 4
75 OpMemberDecorate %_struct_115 2 NonReadable
76 OpMemberDecorate %_struct_115 2 Offset 2
77 OpDecorate %_struct_115 BufferBlock
78 %float = OpTypeFloat 32
79 %uint = OpTypeInt 32 0
80 %uint_6 = OpConstant %uint 6
81 %_arr_double_uint_6 = OpTypeArray %float %uint_6
82 %_struct_115 = OpTypeStruct %float %float %_arr_double_uint_6
83 )";
84 CompileSuccessfully(spirv);
85 EXPECT_EQ(SPV_SUCCESS, ValidateAndRetrieveValidationState());
86
87 // The array must have 1 decoration.
88 const uint32_t arr_id = 1;
89 EXPECT_THAT(
90 vstate_->id_decorations(arr_id),
91 Eq(std::vector<Decoration>{Decoration(SpvDecorationArrayStride, {4})}));
92
93 // The struct must have 3 decorations.
94 const uint32_t struct_id = 2;
95 EXPECT_THAT(
96 vstate_->id_decorations(struct_id),
97 Eq(std::vector<Decoration>{Decoration(SpvDecorationNonReadable, {}, 2),
98 Decoration(SpvDecorationOffset, {2}, 2),
99 Decoration(SpvDecorationBufferBlock)}));
100 }
101
102 TEST_F(ValidateDecorations, ValidateOpMemberDecorateOutOfBound) {
103 std::string spirv = R"(
104 OpCapability Shader
105 OpMemoryModel Logical GLSL450
106 OpEntryPoint Fragment %1 "Main"
107 OpExecutionMode %1 OriginUpperLeft
108 OpMemberDecorate %_struct_2 1 RelaxedPrecision
109 %void = OpTypeVoid
110 %4 = OpTypeFunction %void
111 %float = OpTypeFloat 32
112 %_struct_2 = OpTypeStruct %float
113 %1 = OpFunction %void None %4
114 %6 = OpLabel
115 OpReturn
116 OpFunctionEnd
117 )";
118 CompileSuccessfully(spirv);
119 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateAndRetrieveValidationState());
120 EXPECT_THAT(getDiagnosticString(),
121 HasSubstr("Index 1 provided in OpMemberDecorate for struct <id> "
122 "2[%_struct_2] is out of bounds. The structure has 1 "
123 "members. Largest valid index is 0."));
124 }
125
126 TEST_F(ValidateDecorations, ValidateGroupDecorateRegistration) {
127 std::string spirv = R"(
128 OpCapability Shader
129 OpCapability Linkage
130 OpMemoryModel Logical GLSL450
131 OpDecorate %1 DescriptorSet 0
132 OpDecorate %1 RelaxedPrecision
133 OpDecorate %1 Restrict
134 %1 = OpDecorationGroup
135 OpGroupDecorate %1 %2 %3
136 OpGroupDecorate %1 %4
137 %float = OpTypeFloat 32
138 %_runtimearr_float = OpTypeRuntimeArray %float
139 %_struct_9 = OpTypeStruct %_runtimearr_float
140 %_ptr_Uniform__struct_9 = OpTypePointer Uniform %_struct_9
141 %2 = OpVariable %_ptr_Uniform__struct_9 Uniform
142 %_struct_10 = OpTypeStruct %_runtimearr_float
143 %_ptr_Uniform__struct_10 = OpTypePointer Uniform %_struct_10
144 %3 = OpVariable %_ptr_Uniform__struct_10 Uniform
145 %_struct_11 = OpTypeStruct %_runtimearr_float
146 %_ptr_Uniform__struct_11 = OpTypePointer Uniform %_struct_11
147 %4 = OpVariable %_ptr_Uniform__struct_11 Uniform
148 )";
149 CompileSuccessfully(spirv);
150 EXPECT_EQ(SPV_SUCCESS, ValidateAndRetrieveValidationState());
151
152 // Decoration group has 3 decorations.
153 auto expected_decorations =
154 std::vector<Decoration>{Decoration(SpvDecorationDescriptorSet, {0}),
155 Decoration(SpvDecorationRelaxedPrecision),
156 Decoration(SpvDecorationRestrict)};
157
158 // Decoration group is applied to id 1, 2, 3, and 4. Note that id 1 (which is
159 // the decoration group id) also has all the decorations.
160 EXPECT_THAT(vstate_->id_decorations(1), Eq(expected_decorations));
161 EXPECT_THAT(vstate_->id_decorations(2), Eq(expected_decorations));
162 EXPECT_THAT(vstate_->id_decorations(3), Eq(expected_decorations));
163 EXPECT_THAT(vstate_->id_decorations(4), Eq(expected_decorations));
164 }
165
166 TEST_F(ValidateDecorations, WebGPUOpDecorationGroupBad) {
167 std::string spirv = R"(
168 OpCapability Shader
169 OpCapability VulkanMemoryModelKHR
170 OpExtension "SPV_KHR_vulkan_memory_model"
171 OpMemoryModel Logical VulkanKHR
172 OpDecorate %1 DescriptorSet 0
173 OpDecorate %1 NonWritable
174 OpDecorate %1 Restrict
175 %1 = OpDecorationGroup
176 OpGroupDecorate %1 %2 %3
177 OpGroupDecorate %1 %4
178 %float = OpTypeFloat 32
179 %_runtimearr_float = OpTypeRuntimeArray %float
180 %_struct_9 = OpTypeStruct %_runtimearr_float
181 %_ptr_Uniform__struct_9 = OpTypePointer Uniform %_struct_9
182 %2 = OpVariable %_ptr_Uniform__struct_9 Uniform
183 %_struct_10 = OpTypeStruct %_runtimearr_float
184 %_ptr_Uniform__struct_10 = OpTypePointer Uniform %_struct_10
185 %3 = OpVariable %_ptr_Uniform__struct_10 Uniform
186 %_struct_11 = OpTypeStruct %_runtimearr_float
187 %_ptr_Uniform__struct_11 = OpTypePointer Uniform %_struct_11
188 %4 = OpVariable %_ptr_Uniform__struct_11 Uniform
189 )";
190 CompileSuccessfully(spirv);
191 EXPECT_EQ(SPV_ERROR_INVALID_BINARY, ValidateInstructions(SPV_ENV_WEBGPU_0));
192 EXPECT_THAT(getDiagnosticString(),
193 HasSubstr("OpDecorationGroup is not allowed in the WebGPU "
194 "execution environment.\n %1 = OpDecorationGroup\n"));
195 }
196
197 // For WebGPU, OpGroupDecorate does not have a test case, because it requires
198 // being preceded by OpDecorationGroup, which will cause a validation error.
199
200 // For WebGPU, OpGroupMemberDecorate does not have a test case, because it
201 // requires being preceded by OpDecorationGroup, which will cause a validation
202 // error.
203
204 TEST_F(ValidateDecorations, ValidateGroupMemberDecorateRegistration) {
205 std::string spirv = R"(
206 OpCapability Shader
207 OpCapability Linkage
208 OpMemoryModel Logical GLSL450
209 OpDecorate %1 Offset 3
210 %1 = OpDecorationGroup
211 OpGroupMemberDecorate %1 %_struct_1 3 %_struct_2 3 %_struct_3 3
212 %float = OpTypeFloat 32
213 %_runtimearr = OpTypeRuntimeArray %float
214 %_struct_1 = OpTypeStruct %float %float %float %_runtimearr
215 %_struct_2 = OpTypeStruct %float %float %float %_runtimearr
216 %_struct_3 = OpTypeStruct %float %float %float %_runtimearr
217 )";
218 CompileSuccessfully(spirv);
219 EXPECT_EQ(SPV_SUCCESS, ValidateAndRetrieveValidationState());
220 // Decoration group has 1 decoration.
221 auto expected_decorations =
222 std::vector<Decoration>{Decoration(SpvDecorationOffset, {3}, 3)};
223
224 // Decoration group is applied to id 2, 3, and 4.
225 EXPECT_THAT(vstate_->id_decorations(2), Eq(expected_decorations));
226 EXPECT_THAT(vstate_->id_decorations(3), Eq(expected_decorations));
227 EXPECT_THAT(vstate_->id_decorations(4), Eq(expected_decorations));
228 }
229
230 TEST_F(ValidateDecorations, LinkageImportUsedForInitializedVariableBad) {
231 std::string spirv = R"(
232 OpCapability Shader
233 OpCapability Linkage
234 OpMemoryModel Logical GLSL450
235 OpDecorate %target LinkageAttributes "link_ptr" Import
236 %float = OpTypeFloat 32
237 %_ptr_float = OpTypePointer Uniform %float
238 %zero = OpConstantNull %float
239 %target = OpVariable %_ptr_float Uniform %zero
240 )";
241 CompileSuccessfully(spirv);
242 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateAndRetrieveValidationState());
243 EXPECT_THAT(getDiagnosticString(),
244 HasSubstr("A module-scope OpVariable with initialization value "
245 "cannot be marked with the Import Linkage Type."));
246 }
247 TEST_F(ValidateDecorations, LinkageExportUsedForInitializedVariableGood) {
248 std::string spirv = R"(
249 OpCapability Shader
250 OpCapability Linkage
251 OpMemoryModel Logical GLSL450
252 OpDecorate %target LinkageAttributes "link_ptr" Export
253 %float = OpTypeFloat 32
254 %_ptr_float = OpTypePointer Uniform %float
255 %zero = OpConstantNull %float
256 %target = OpVariable %_ptr_float Uniform %zero
257 )";
258 CompileSuccessfully(spirv);
259 EXPECT_EQ(SPV_SUCCESS, ValidateAndRetrieveValidationState());
260 }
261
262 TEST_F(ValidateDecorations, StructAllMembersHaveBuiltInDecorationsGood) {
263 std::string spirv = R"(
264 OpCapability Shader
265 OpCapability Linkage
266 OpMemoryModel Logical GLSL450
267 OpMemberDecorate %_struct_1 0 BuiltIn Position
268 OpMemberDecorate %_struct_1 1 BuiltIn Position
269 OpMemberDecorate %_struct_1 2 BuiltIn Position
270 OpMemberDecorate %_struct_1 3 BuiltIn Position
271 %float = OpTypeFloat 32
272 %_runtimearr = OpTypeRuntimeArray %float
273 %_struct_1 = OpTypeStruct %float %float %float %_runtimearr
274 )";
275 CompileSuccessfully(spirv);
276 EXPECT_EQ(SPV_SUCCESS, ValidateAndRetrieveValidationState());
277 }
278
279 TEST_F(ValidateDecorations, MixedBuiltInDecorationsBad) {
280 std::string spirv = R"(
281 OpCapability Shader
282 OpCapability Linkage
283 OpMemoryModel Logical GLSL450
284 OpMemberDecorate %_struct_1 0 BuiltIn Position
285 OpMemberDecorate %_struct_1 1 BuiltIn Position
286 %float = OpTypeFloat 32
287 %_runtimearr = OpTypeRuntimeArray %float
288 %_struct_1 = OpTypeStruct %float %float %float %_runtimearr
289 )";
290 CompileSuccessfully(spirv);
291 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateAndRetrieveValidationState());
292 EXPECT_THAT(
293 getDiagnosticString(),
294 HasSubstr("When BuiltIn decoration is applied to a structure-type "
295 "member, all members of that structure type must also be "
296 "decorated with BuiltIn (No allowed mixing of built-in "
297 "variables and non-built-in variables within a single "
298 "structure). Structure id 1 does not meet this requirement."));
299 }
300
301 TEST_F(ValidateDecorations, StructContainsBuiltInStructBad) {
302 std::string spirv = R"(
303 OpCapability Shader
304 OpCapability Linkage
305 OpMemoryModel Logical GLSL450
306 OpMemberDecorate %_struct_1 0 BuiltIn Position
307 OpMemberDecorate %_struct_1 1 BuiltIn Position
308 OpMemberDecorate %_struct_1 2 BuiltIn Position
309 OpMemberDecorate %_struct_1 3 BuiltIn Position
310 %float = OpTypeFloat 32
311 %_runtimearr = OpTypeRuntimeArray %float
312 %_struct_1 = OpTypeStruct %float %float %float %_runtimearr
313 %_struct_2 = OpTypeStruct %_struct_1
314 )";
315 CompileSuccessfully(spirv);
316 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateAndRetrieveValidationState());
317 EXPECT_THAT(getDiagnosticString(),
318 HasSubstr("Structure <id> 1[%_struct_1] contains members with "
319 "BuiltIn decoration. Therefore this structure may not "
320 "be contained as a member of another structure type. "
321 "Structure <id> 4[%_struct_4] contains structure <id> "
322 "1[%_struct_1]."));
323 }
324
325 TEST_F(ValidateDecorations, StructContainsNonBuiltInStructGood) {
326 std::string spirv = R"(
327 OpCapability Shader
328 OpCapability Linkage
329 OpMemoryModel Logical GLSL450
330 %float = OpTypeFloat 32
331 %_struct_1 = OpTypeStruct %float
332 %_struct_2 = OpTypeStruct %_struct_1
333 )";
334 CompileSuccessfully(spirv);
335 EXPECT_EQ(SPV_SUCCESS, ValidateAndRetrieveValidationState());
336 }
337
338 TEST_F(ValidateDecorations, MultipleBuiltInObjectsConsumedByOpEntryPointBad) {
339 std::string spirv = R"(
340 OpCapability Shader
341 OpCapability Geometry
342 OpMemoryModel Logical GLSL450
343 OpEntryPoint Geometry %main "main" %in_1 %in_2
344 OpExecutionMode %main InputPoints
345 OpExecutionMode %main OutputPoints
346 OpMemberDecorate %struct_1 0 BuiltIn InvocationId
347 OpMemberDecorate %struct_2 0 BuiltIn Position
348 %int = OpTypeInt 32 1
349 %void = OpTypeVoid
350 %func = OpTypeFunction %void
351 %float = OpTypeFloat 32
352 %struct_1 = OpTypeStruct %int
353 %struct_2 = OpTypeStruct %float
354 %ptr_builtin_1 = OpTypePointer Input %struct_1
355 %ptr_builtin_2 = OpTypePointer Input %struct_2
356 %in_1 = OpVariable %ptr_builtin_1 Input
357 %in_2 = OpVariable %ptr_builtin_2 Input
358 %main = OpFunction %void None %func
359 %5 = OpLabel
360 OpReturn
361 OpFunctionEnd
362 )";
363 CompileSuccessfully(spirv);
364 EXPECT_EQ(SPV_ERROR_INVALID_BINARY, ValidateAndRetrieveValidationState());
365 EXPECT_THAT(getDiagnosticString(),
366 HasSubstr("There must be at most one object per Storage Class "
367 "that can contain a structure type containing members "
368 "decorated with BuiltIn, consumed per entry-point."));
369 }
370
371 TEST_F(ValidateDecorations,
372 OneBuiltInObjectPerStorageClassConsumedByOpEntryPointGood) {
373 std::string spirv = R"(
374 OpCapability Shader
375 OpCapability Geometry
376 OpMemoryModel Logical GLSL450
377 OpEntryPoint Geometry %main "main" %in_1 %out_1
378 OpExecutionMode %main InputPoints
379 OpExecutionMode %main OutputPoints
380 OpMemberDecorate %struct_1 0 BuiltIn InvocationId
381 OpMemberDecorate %struct_2 0 BuiltIn Position
382 %int = OpTypeInt 32 1
383 %void = OpTypeVoid
384 %func = OpTypeFunction %void
385 %float = OpTypeFloat 32
386 %struct_1 = OpTypeStruct %int
387 %struct_2 = OpTypeStruct %float
388 %ptr_builtin_1 = OpTypePointer Input %struct_1
389 %ptr_builtin_2 = OpTypePointer Output %struct_2
390 %in_1 = OpVariable %ptr_builtin_1 Input
391 %out_1 = OpVariable %ptr_builtin_2 Output
392 %main = OpFunction %void None %func
393 %5 = OpLabel
394 OpReturn
395 OpFunctionEnd
396 )";
397 CompileSuccessfully(spirv);
398 EXPECT_EQ(SPV_SUCCESS, ValidateAndRetrieveValidationState());
399 }
400
401 TEST_F(ValidateDecorations, NoBuiltInObjectsConsumedByOpEntryPointGood) {
402 std::string spirv = R"(
403 OpCapability Shader
404 OpCapability Geometry
405 OpMemoryModel Logical GLSL450
406 OpEntryPoint Geometry %main "main" %in_1 %out_1
407 OpExecutionMode %main InputPoints
408 OpExecutionMode %main OutputPoints
409 %int = OpTypeInt 32 1
410 %void = OpTypeVoid
411 %func = OpTypeFunction %void
412 %float = OpTypeFloat 32
413 %struct_1 = OpTypeStruct %int
414 %struct_2 = OpTypeStruct %float
415 %ptr_builtin_1 = OpTypePointer Input %struct_1
416 %ptr_builtin_2 = OpTypePointer Output %struct_2
417 %in_1 = OpVariable %ptr_builtin_1 Input
418 %out_1 = OpVariable %ptr_builtin_2 Output
419 %main = OpFunction %void None %func
420 %5 = OpLabel
421 OpReturn
422 OpFunctionEnd
423 )";
424 CompileSuccessfully(spirv);
425 EXPECT_EQ(SPV_SUCCESS, ValidateAndRetrieveValidationState());
426 }
427
428 TEST_F(ValidateDecorations, EntryPointFunctionHasLinkageAttributeBad) {
429 std::string spirv = R"(
430 OpCapability Shader
431 OpCapability Linkage
432 OpMemoryModel Logical GLSL450
433 OpEntryPoint GLCompute %main "main"
434 OpDecorate %main LinkageAttributes "import_main" Import
435 %1 = OpTypeVoid
436 %2 = OpTypeFunction %1
437 %main = OpFunction %1 None %2
438 %4 = OpLabel
439 OpReturn
440 OpFunctionEnd
441 )";
442 CompileSuccessfully(spirv.c_str());
443 EXPECT_EQ(SPV_ERROR_INVALID_BINARY, ValidateInstructions());
444 EXPECT_THAT(
445 getDiagnosticString(),
446 HasSubstr("The LinkageAttributes Decoration (Linkage name: import_main) "
447 "cannot be applied to function id 1 because it is targeted by "
448 "an OpEntryPoint instruction."));
449 }
450
451 TEST_F(ValidateDecorations, FunctionDeclarationWithoutImportLinkageBad) {
452 std::string spirv = R"(
453 OpCapability Shader
454 OpCapability Linkage
455 OpMemoryModel Logical GLSL450
456 %void = OpTypeVoid
457 %func = OpTypeFunction %void
458 %main = OpFunction %void None %func
459 OpFunctionEnd
460 )";
461 CompileSuccessfully(spirv);
462 EXPECT_EQ(SPV_ERROR_INVALID_BINARY, ValidateAndRetrieveValidationState());
463 EXPECT_THAT(
464 getDiagnosticString(),
465 HasSubstr("Function declaration (id 3) must have a LinkageAttributes "
466 "decoration with the Import Linkage type."));
467 }
468
469 TEST_F(ValidateDecorations, FunctionDeclarationWithImportLinkageGood) {
470 std::string spirv = R"(
471 OpCapability Shader
472 OpCapability Linkage
473 OpMemoryModel Logical GLSL450
474 OpDecorate %main LinkageAttributes "link_fn" Import
475 %void = OpTypeVoid
476 %func = OpTypeFunction %void
477 %main = OpFunction %void None %func
478 OpFunctionEnd
479 )";
480 CompileSuccessfully(spirv);
481 EXPECT_EQ(SPV_SUCCESS, ValidateAndRetrieveValidationState());
482 }
483
484 TEST_F(ValidateDecorations, FunctionDeclarationWithExportLinkageBad) {
485 std::string spirv = R"(
486 OpCapability Shader
487 OpCapability Linkage
488 OpMemoryModel Logical GLSL450
489 OpDecorate %main LinkageAttributes "link_fn" Export
490 %void = OpTypeVoid
491 %func = OpTypeFunction %void
492 %main = OpFunction %void None %func
493 OpFunctionEnd
494 )";
495 CompileSuccessfully(spirv);
496 EXPECT_EQ(SPV_ERROR_INVALID_BINARY, ValidateAndRetrieveValidationState());
497 EXPECT_THAT(
498 getDiagnosticString(),
499 HasSubstr("Function declaration (id 1) must have a LinkageAttributes "
500 "decoration with the Import Linkage type."));
501 }
502
503 TEST_F(ValidateDecorations, FunctionDefinitionWithImportLinkageBad) {
504 std::string spirv = R"(
505 OpCapability Shader
506 OpCapability Linkage
507 OpMemoryModel Logical GLSL450
508 OpDecorate %main LinkageAttributes "link_fn" Import
509 %void = OpTypeVoid
510 %func = OpTypeFunction %void
511 %main = OpFunction %void None %func
512 %label = OpLabel
513 OpReturn
514 OpFunctionEnd
515 )";
516 CompileSuccessfully(spirv);
517 EXPECT_EQ(SPV_ERROR_INVALID_BINARY, ValidateAndRetrieveValidationState());
518 EXPECT_THAT(getDiagnosticString(),
519 HasSubstr("Function definition (id 1) may not be decorated with "
520 "Import Linkage type."));
521 }
522
523 TEST_F(ValidateDecorations, FunctionDefinitionWithoutImportLinkageGood) {
524 std::string spirv = R"(
525 OpCapability Shader
526 OpCapability Linkage
527 OpMemoryModel Logical GLSL450
528 %void = OpTypeVoid
529 %func = OpTypeFunction %void
530 %main = OpFunction %void None %func
531 %label = OpLabel
532 OpReturn
533 OpFunctionEnd
534 )";
535 CompileSuccessfully(spirv);
536 EXPECT_EQ(SPV_SUCCESS, ValidateAndRetrieveValidationState());
537 }
538
539 TEST_F(ValidateDecorations, BuiltinVariablesGoodVulkan) {
540 const spv_target_env env = SPV_ENV_VULKAN_1_0;
541 std::string spirv = R"(
542 OpCapability Shader
543 OpMemoryModel Logical GLSL450
544 OpEntryPoint Fragment %main "main" %gl_FragCoord %_entryPointOutput
545 OpExecutionMode %main OriginUpperLeft
546 OpSource HLSL 500
547 OpDecorate %gl_FragCoord BuiltIn FragCoord
548 OpDecorate %_entryPointOutput Location 0
549 %void = OpTypeVoid
550 %3 = OpTypeFunction %void
551 %float = OpTypeFloat 32
552 %v4float = OpTypeVector %float 4
553 %float_0 = OpConstant %float 0
554 %14 = OpConstantComposite %v4float %float_0 %float_0 %float_0 %float_0
555 %_ptr_Input_v4float = OpTypePointer Input %v4float
556 %gl_FragCoord = OpVariable %_ptr_Input_v4float Input
557 %_ptr_Output_v4float = OpTypePointer Output %v4float
558 %_entryPointOutput = OpVariable %_ptr_Output_v4float Output
559 %main = OpFunction %void None %3
560 %5 = OpLabel
561 OpStore %_entryPointOutput %14
562 OpReturn
563 OpFunctionEnd
564 )";
565
566 CompileSuccessfully(spirv, env);
567 EXPECT_EQ(SPV_SUCCESS, ValidateAndRetrieveValidationState(env));
568 }
569
570 TEST_F(ValidateDecorations, BuiltinVariablesWithLocationDecorationVulkan) {
571 const spv_target_env env = SPV_ENV_VULKAN_1_0;
572 std::string spirv = R"(
573 OpCapability Shader
574 OpMemoryModel Logical GLSL450
575 OpEntryPoint Fragment %main "main" %gl_FragCoord %_entryPointOutput
576 OpExecutionMode %main OriginUpperLeft
577 OpSource HLSL 500
578 OpDecorate %gl_FragCoord BuiltIn FragCoord
579 OpDecorate %gl_FragCoord Location 0
580 OpDecorate %_entryPointOutput Location 0
581 %void = OpTypeVoid
582 %3 = OpTypeFunction %void
583 %float = OpTypeFloat 32
584 %v4float = OpTypeVector %float 4
585 %float_0 = OpConstant %float 0
586 %14 = OpConstantComposite %v4float %float_0 %float_0 %float_0 %float_0
587 %_ptr_Input_v4float = OpTypePointer Input %v4float
588 %gl_FragCoord = OpVariable %_ptr_Input_v4float Input
589 %_ptr_Output_v4float = OpTypePointer Output %v4float
590 %_entryPointOutput = OpVariable %_ptr_Output_v4float Output
591 %main = OpFunction %void None %3
592 %5 = OpLabel
593 OpStore %_entryPointOutput %14
594 OpReturn
595 OpFunctionEnd
596 )";
597
598 CompileSuccessfully(spirv, env);
599 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateAndRetrieveValidationState(env));
600 EXPECT_THAT(getDiagnosticString(),
601 HasSubstr("A BuiltIn variable (id 2) cannot have any Location or "
602 "Component decorations"));
603 }
604 TEST_F(ValidateDecorations, BuiltinVariablesWithComponentDecorationVulkan) {
605 const spv_target_env env = SPV_ENV_VULKAN_1_0;
606 std::string spirv = R"(
607 OpCapability Shader
608 OpMemoryModel Logical GLSL450
609 OpEntryPoint Fragment %main "main" %gl_FragCoord %_entryPointOutput
610 OpExecutionMode %main OriginUpperLeft
611 OpSource HLSL 500
612 OpDecorate %gl_FragCoord BuiltIn FragCoord
613 OpDecorate %gl_FragCoord Component 0
614 OpDecorate %_entryPointOutput Location 0
615 %void = OpTypeVoid
616 %3 = OpTypeFunction %void
617 %float = OpTypeFloat 32
618 %v4float = OpTypeVector %float 4
619 %float_0 = OpConstant %float 0
620 %14 = OpConstantComposite %v4float %float_0 %float_0 %float_0 %float_0
621 %_ptr_Input_v4float = OpTypePointer Input %v4float
622 %gl_FragCoord = OpVariable %_ptr_Input_v4float Input
623 %_ptr_Output_v4float = OpTypePointer Output %v4float
624 %_entryPointOutput = OpVariable %_ptr_Output_v4float Output
625 %main = OpFunction %void None %3
626 %5 = OpLabel
627 OpStore %_entryPointOutput %14
628 OpReturn
629 OpFunctionEnd
630 )";
631
632 CompileSuccessfully(spirv, env);
633 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateAndRetrieveValidationState(env));
634 EXPECT_THAT(getDiagnosticString(),
635 HasSubstr("A BuiltIn variable (id 2) cannot have any Location or "
636 "Component decorations"));
637 }
638
639 // #version 440
640 // #extension GL_EXT_nonuniform_qualifier : enable
641 // layout(binding = 1) uniform sampler2D s2d[];
642 // layout(location = 0) in nonuniformEXT int i;
643 // void main()
644 // {
645 // vec4 v = texture(s2d[i], vec2(0.3));
646 // }
647 TEST_F(ValidateDecorations, RuntimeArrayOfDescriptorSetsIsAllowed) {
648 const spv_target_env env = SPV_ENV_VULKAN_1_0;
649 std::string spirv = R"(
650 OpCapability Shader
651 OpCapability ShaderNonUniformEXT
652 OpCapability RuntimeDescriptorArrayEXT
653 OpCapability SampledImageArrayNonUniformIndexingEXT
654 OpExtension "SPV_EXT_descriptor_indexing"
655 %1 = OpExtInstImport "GLSL.std.450"
656 OpMemoryModel Logical GLSL450
657 OpEntryPoint Vertex %main "main" %i
658 OpSource GLSL 440
659 OpSourceExtension "GL_EXT_nonuniform_qualifier"
660 OpName %main "main"
661 OpName %v "v"
662 OpName %s2d "s2d"
663 OpName %i "i"
664 OpDecorate %s2d DescriptorSet 0
665 OpDecorate %s2d Binding 1
666 OpDecorate %i Location 0
667 OpDecorate %i NonUniformEXT
668 OpDecorate %18 NonUniformEXT
669 OpDecorate %21 NonUniformEXT
670 %void = OpTypeVoid
671 %3 = OpTypeFunction %void
672 %float = OpTypeFloat 32
673 %v4float = OpTypeVector %float 4
674 %_ptr_Function_v4float = OpTypePointer Function %v4float
675 %10 = OpTypeImage %float 2D 0 0 0 1 Unknown
676 %11 = OpTypeSampledImage %10
677 %_runtimearr_11 = OpTypeRuntimeArray %11
678 %_ptr_Uniform__runtimearr_11 = OpTypePointer Uniform %_runtimearr_11
679 %s2d = OpVariable %_ptr_Uniform__runtimearr_11 Uniform
680 %int = OpTypeInt 32 1
681 %_ptr_Input_int = OpTypePointer Input %int
682 %i = OpVariable %_ptr_Input_int Input
683 %_ptr_Uniform_11 = OpTypePointer Uniform %11
684 %v2float = OpTypeVector %float 2
685 %float_0_300000012 = OpConstant %float 0.300000012
686 %24 = OpConstantComposite %v2float %float_0_300000012 %float_0_300000012
687 %float_0 = OpConstant %float 0
688 %main = OpFunction %void None %3
689 %5 = OpLabel
690 %v = OpVariable %_ptr_Function_v4float Function
691 %18 = OpLoad %int %i
692 %20 = OpAccessChain %_ptr_Uniform_11 %s2d %18
693 %21 = OpLoad %11 %20
694 %26 = OpImageSampleExplicitLod %v4float %21 %24 Lod %float_0
695 OpStore %v %26
696 OpReturn
697 OpFunctionEnd
698 )";
699 CompileSuccessfully(spirv, env);
700 EXPECT_EQ(SPV_SUCCESS, ValidateAndRetrieveValidationState());
701 }
702
703 TEST_F(ValidateDecorations, BlockDecoratingArrayBad) {
704 std::string spirv = R"(
705 OpCapability Shader
706 %1 = OpExtInstImport "GLSL.std.450"
707 OpMemoryModel Logical GLSL450
708 OpEntryPoint GLCompute %main "main"
709 OpExecutionMode %main LocalSize 1 1 1
710 OpSource GLSL 430
711 OpDecorate %Output Block
712 %void = OpTypeVoid
713 %3 = OpTypeFunction %void
714 %float = OpTypeFloat 32
715 %int = OpTypeInt 32 1
716 %int_3 = OpConstant %int 3
717 %Output = OpTypeArray %float %int_3
718 %_ptr_Uniform_Output = OpTypePointer Uniform %Output
719 %dataOutput = OpVariable %_ptr_Uniform_Output Uniform
720 %main = OpFunction %void None %3
721 %5 = OpLabel
722 OpReturn
723 OpFunctionEnd
724 )";
725
726 CompileSuccessfully(spirv);
727 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateAndRetrieveValidationState());
728 EXPECT_THAT(getDiagnosticString(),
729 HasSubstr("Block decoration on a non-struct type"));
730 }
731
732 TEST_F(ValidateDecorations, BlockDecoratingIntBad) {
733 std::string spirv = R"(
734 OpCapability Shader
735 %1 = OpExtInstImport "GLSL.std.450"
736 OpMemoryModel Logical GLSL450
737 OpEntryPoint GLCompute %main "main"
738 OpExecutionMode %main LocalSize 1 1 1
739 OpSource GLSL 430
740 OpDecorate %Output Block
741 %void = OpTypeVoid
742 %3 = OpTypeFunction %void
743 %Output = OpTypeInt 32 1
744 %_ptr_Uniform_Output = OpTypePointer Uniform %Output
745 %dataOutput = OpVariable %_ptr_Uniform_Output Uniform
746 %main = OpFunction %void None %3
747 %5 = OpLabel
748 OpReturn
749 OpFunctionEnd
750 )";
751
752 CompileSuccessfully(spirv);
753 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateAndRetrieveValidationState());
754 EXPECT_THAT(getDiagnosticString(),
755 HasSubstr("Block decoration on a non-struct type"));
756 }
757
758 TEST_F(ValidateDecorations, BlockMissingOffsetBad) {
759 std::string spirv = R"(
760 OpCapability Shader
761 %1 = OpExtInstImport "GLSL.std.450"
762 OpMemoryModel Logical GLSL450
763 OpEntryPoint GLCompute %main "main"
764 OpExecutionMode %main LocalSize 1 1 1
765 OpSource GLSL 430
766 OpDecorate %Output Block
767 %void = OpTypeVoid
768 %3 = OpTypeFunction %void
769 %float = OpTypeFloat 32
770 %Output = OpTypeStruct %float
771 %_ptr_Uniform_Output = OpTypePointer Uniform %Output
772 %dataOutput = OpVariable %_ptr_Uniform_Output Uniform
773 %main = OpFunction %void None %3
774 %5 = OpLabel
775 OpReturn
776 OpFunctionEnd
777 )";
778
779 CompileSuccessfully(spirv);
780 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateAndRetrieveValidationState());
781 EXPECT_THAT(getDiagnosticString(),
782 HasSubstr("must be explicitly laid out with Offset decorations"));
783 }
784
785 TEST_F(ValidateDecorations, BufferBlockMissingOffsetBad) {
786 std::string spirv = R"(
787 OpCapability Shader
788 %1 = OpExtInstImport "GLSL.std.450"
789 OpMemoryModel Logical GLSL450
790 OpEntryPoint GLCompute %main "main"
791 OpExecutionMode %main LocalSize 1 1 1
792 OpSource GLSL 430
793 OpDecorate %Output BufferBlock
794 %void = OpTypeVoid
795 %3 = OpTypeFunction %void
796 %float = OpTypeFloat 32
797 %Output = OpTypeStruct %float
798 %_ptr_Uniform_Output = OpTypePointer Uniform %Output
799 %dataOutput = OpVariable %_ptr_Uniform_Output Uniform
800 %main = OpFunction %void None %3
801 %5 = OpLabel
802 OpReturn
803 OpFunctionEnd
804 )";
805
806 CompileSuccessfully(spirv);
807 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateAndRetrieveValidationState());
808 EXPECT_THAT(getDiagnosticString(),
809 HasSubstr("must be explicitly laid out with Offset decorations"));
810 }
811
812 TEST_F(ValidateDecorations, BlockNestedStructMissingOffsetBad) {
813 std::string spirv = R"(
814 OpCapability Shader
815 %1 = OpExtInstImport "GLSL.std.450"
816 OpMemoryModel Logical GLSL450
817 OpEntryPoint GLCompute %main "main"
818 OpExecutionMode %main LocalSize 1 1 1
819 OpSource GLSL 430
820 OpMemberDecorate %S 0 Offset 0
821 OpMemberDecorate %Output 0 Offset 0
822 OpMemberDecorate %Output 1 Offset 16
823 OpMemberDecorate %Output 2 Offset 32
824 OpDecorate %Output Block
825 %void = OpTypeVoid
826 %3 = OpTypeFunction %void
827 %float = OpTypeFloat 32
828 %v4float = OpTypeVector %float 4
829 %v3float = OpTypeVector %float 3
830 %int = OpTypeInt 32 1
831 %S = OpTypeStruct %v3float %int
832 %Output = OpTypeStruct %float %v4float %S
833 %_ptr_Uniform_Output = OpTypePointer Uniform %Output
834 %dataOutput = OpVariable %_ptr_Uniform_Output Uniform
835 %main = OpFunction %void None %3
836 %5 = OpLabel
837 OpReturn
838 OpFunctionEnd
839 )";
840
841 CompileSuccessfully(spirv);
842 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateAndRetrieveValidationState());
843 EXPECT_THAT(getDiagnosticString(),
844 HasSubstr("must be explicitly laid out with Offset decorations"));
845 }
846
847 TEST_F(ValidateDecorations, BufferBlockNestedStructMissingOffsetBad) {
848 std::string spirv = R"(
849 OpCapability Shader
850 %1 = OpExtInstImport "GLSL.std.450"
851 OpMemoryModel Logical GLSL450
852 OpEntryPoint GLCompute %main "main"
853 OpExecutionMode %main LocalSize 1 1 1
854 OpSource GLSL 430
855 OpMemberDecorate %S 0 Offset 0
856 OpMemberDecorate %Output 0 Offset 0
857 OpMemberDecorate %Output 1 Offset 16
858 OpMemberDecorate %Output 2 Offset 32
859 OpDecorate %Output BufferBlock
860 %void = OpTypeVoid
861 %3 = OpTypeFunction %void
862 %float = OpTypeFloat 32
863 %v4float = OpTypeVector %float 4
864 %v3float = OpTypeVector %float 3
865 %int = OpTypeInt 32 1
866 %S = OpTypeStruct %v3float %int
867 %Output = OpTypeStruct %float %v4float %S
868 %_ptr_Uniform_Output = OpTypePointer Uniform %Output
869 %dataOutput = OpVariable %_ptr_Uniform_Output Uniform
870 %main = OpFunction %void None %3
871 %5 = OpLabel
872 OpReturn
873 OpFunctionEnd
874 )";
875
876 CompileSuccessfully(spirv);
877 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateAndRetrieveValidationState());
878 EXPECT_THAT(getDiagnosticString(),
879 HasSubstr("must be explicitly laid out with Offset decorations"));
880 }
881
882 TEST_F(ValidateDecorations, BlockGLSLSharedBad) {
883 std::string spirv = R"(
884 OpCapability Shader
885 %1 = OpExtInstImport "GLSL.std.450"
886 OpMemoryModel Logical GLSL450
887 OpEntryPoint GLCompute %main "main"
888 OpExecutionMode %main LocalSize 1 1 1
889 OpSource GLSL 430
890 OpDecorate %Output Block
891 OpDecorate %Output GLSLShared
892 OpMemberDecorate %Output 0 Offset 0
893 %void = OpTypeVoid
894 %3 = OpTypeFunction %void
895 %float = OpTypeFloat 32
896 %Output = OpTypeStruct %float
897 %_ptr_Uniform_Output = OpTypePointer Uniform %Output
898 %dataOutput = OpVariable %_ptr_Uniform_Output Uniform
899 %main = OpFunction %void None %3
900 %5 = OpLabel
901 OpReturn
902 OpFunctionEnd
903 )";
904
905 CompileSuccessfully(spirv);
906 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateAndRetrieveValidationState());
907 EXPECT_THAT(getDiagnosticString(),
908 HasSubstr("must not use GLSLShared decoration"));
909 }
910
911 TEST_F(ValidateDecorations, BufferBlockGLSLSharedBad) {
912 std::string spirv = R"(
913 OpCapability Shader
914 %1 = OpExtInstImport "GLSL.std.450"
915 OpMemoryModel Logical GLSL450
916 OpEntryPoint GLCompute %main "main"
917 OpExecutionMode %main LocalSize 1 1 1
918 OpSource GLSL 430
919 OpDecorate %Output BufferBlock
920 OpDecorate %Output GLSLShared
921 OpMemberDecorate %Output 0 Offset 0
922 %void = OpTypeVoid
923 %3 = OpTypeFunction %void
924 %float = OpTypeFloat 32
925 %Output = OpTypeStruct %float
926 %_ptr_Uniform_Output = OpTypePointer Uniform %Output
927 %dataOutput = OpVariable %_ptr_Uniform_Output Uniform
928 %main = OpFunction %void None %3
929 %5 = OpLabel
930 OpReturn
931 OpFunctionEnd
932 )";
933
934 CompileSuccessfully(spirv);
935 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateAndRetrieveValidationState());
936 EXPECT_THAT(getDiagnosticString(),
937 HasSubstr("must not use GLSLShared decoration"));
938 }
939
940 TEST_F(ValidateDecorations, BlockNestedStructGLSLSharedBad) {
941 std::string spirv = R"(
942 OpCapability Shader
943 %1 = OpExtInstImport "GLSL.std.450"
944 OpMemoryModel Logical GLSL450
945 OpEntryPoint GLCompute %main "main"
946 OpExecutionMode %main LocalSize 1 1 1
947 OpSource GLSL 430
948 OpMemberDecorate %S 0 Offset 0
949 OpDecorate %S GLSLShared
950 OpMemberDecorate %Output 0 Offset 0
951 OpMemberDecorate %Output 1 Offset 16
952 OpMemberDecorate %Output 2 Offset 32
953 OpDecorate %Output Block
954 %void = OpTypeVoid
955 %3 = OpTypeFunction %void
956 %float = OpTypeFloat 32
957 %v4float = OpTypeVector %float 4
958 %int = OpTypeInt 32 1
959 %S = OpTypeStruct %int
960 %Output = OpTypeStruct %float %v4float %S
961 %_ptr_Uniform_Output = OpTypePointer Uniform %Output
962 %dataOutput = OpVariable %_ptr_Uniform_Output Uniform
963 %main = OpFunction %void None %3
964 %5 = OpLabel
965 OpReturn
966 OpFunctionEnd
967 )";
968
969 CompileSuccessfully(spirv);
970 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateAndRetrieveValidationState());
971 EXPECT_THAT(getDiagnosticString(),
972 HasSubstr("must not use GLSLShared decoration"));
973 }
974
975 TEST_F(ValidateDecorations, BufferBlockNestedStructGLSLSharedBad) {
976 std::string spirv = R"(
977 OpCapability Shader
978 %1 = OpExtInstImport "GLSL.std.450"
979 OpMemoryModel Logical GLSL450
980 OpEntryPoint GLCompute %main "main"
981 OpExecutionMode %main LocalSize 1 1 1
982 OpSource GLSL 430
983 OpMemberDecorate %S 0 Offset 0
984 OpDecorate %S GLSLShared
985 OpMemberDecorate %Output 0 Offset 0
986 OpMemberDecorate %Output 1 Offset 16
987 OpMemberDecorate %Output 2 Offset 32
988 OpDecorate %Output BufferBlock
989 %void = OpTypeVoid
990 %3 = OpTypeFunction %void
991 %float = OpTypeFloat 32
992 %v4float = OpTypeVector %float 4
993 %int = OpTypeInt 32 1
994 %S = OpTypeStruct %int
995 %Output = OpTypeStruct %float %v4float %S
996 %_ptr_Uniform_Output = OpTypePointer Uniform %Output
997 %dataOutput = OpVariable %_ptr_Uniform_Output Uniform
998 %main = OpFunction %void None %3
999 %5 = OpLabel
1000 OpReturn
1001 OpFunctionEnd
1002 )";
1003
1004 CompileSuccessfully(spirv);
1005 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateAndRetrieveValidationState());
1006 EXPECT_THAT(getDiagnosticString(),
1007 HasSubstr("must not use GLSLShared decoration"));
1008 }
1009
1010 TEST_F(ValidateDecorations, BlockGLSLPackedBad) {
1011 std::string spirv = R"(
1012 OpCapability Shader
1013 %1 = OpExtInstImport "GLSL.std.450"
1014 OpMemoryModel Logical GLSL450
1015 OpEntryPoint GLCompute %main "main"
1016 OpExecutionMode %main LocalSize 1 1 1
1017 OpSource GLSL 430
1018 OpDecorate %Output Block
1019 OpDecorate %Output GLSLPacked
1020 OpMemberDecorate %Output 0 Offset 0
1021 %void = OpTypeVoid
1022 %3 = OpTypeFunction %void
1023 %float = OpTypeFloat 32
1024 %Output = OpTypeStruct %float
1025 %_ptr_Uniform_Output = OpTypePointer Uniform %Output
1026 %dataOutput = OpVariable %_ptr_Uniform_Output Uniform
1027 %main = OpFunction %void None %3
1028 %5 = OpLabel
1029 OpReturn
1030 OpFunctionEnd
1031 )";
1032
1033 CompileSuccessfully(spirv);
1034 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateAndRetrieveValidationState());
1035 EXPECT_THAT(getDiagnosticString(),
1036 HasSubstr("must not use GLSLPacked decoration"));
1037 }
1038
1039 TEST_F(ValidateDecorations, BufferBlockGLSLPackedBad) {
1040 std::string spirv = R"(
1041 OpCapability Shader
1042 %1 = OpExtInstImport "GLSL.std.450"
1043 OpMemoryModel Logical GLSL450
1044 OpEntryPoint GLCompute %main "main"
1045 OpExecutionMode %main LocalSize 1 1 1
1046 OpSource GLSL 430
1047 OpDecorate %Output BufferBlock
1048 OpDecorate %Output GLSLPacked
1049 OpMemberDecorate %Output 0 Offset 0
1050 %void = OpTypeVoid
1051 %3 = OpTypeFunction %void
1052 %float = OpTypeFloat 32
1053 %Output = OpTypeStruct %float
1054 %_ptr_Uniform_Output = OpTypePointer Uniform %Output
1055 %dataOutput = OpVariable %_ptr_Uniform_Output Uniform
1056 %main = OpFunction %void None %3
1057 %5 = OpLabel
1058 OpReturn
1059 OpFunctionEnd
1060 )";
1061
1062 CompileSuccessfully(spirv);
1063 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateAndRetrieveValidationState());
1064 EXPECT_THAT(getDiagnosticString(),
1065 HasSubstr("must not use GLSLPacked decoration"));
1066 }
1067
1068 TEST_F(ValidateDecorations, BlockNestedStructGLSLPackedBad) {
1069 std::string spirv = R"(
1070 OpCapability Shader
1071 %1 = OpExtInstImport "GLSL.std.450"
1072 OpMemoryModel Logical GLSL450
1073 OpEntryPoint GLCompute %main "main"
1074 OpExecutionMode %main LocalSize 1 1 1
1075 OpSource GLSL 430
1076 OpMemberDecorate %S 0 Offset 0
1077 OpDecorate %S GLSLPacked
1078 OpMemberDecorate %Output 0 Offset 0
1079 OpMemberDecorate %Output 1 Offset 16
1080 OpMemberDecorate %Output 2 Offset 32
1081 OpDecorate %Output Block
1082 %void = OpTypeVoid
1083 %3 = OpTypeFunction %void
1084 %float = OpTypeFloat 32
1085 %v4float = OpTypeVector %float 4
1086 %int = OpTypeInt 32 1
1087 %S = OpTypeStruct %int
1088 %Output = OpTypeStruct %float %v4float %S
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 not use GLSLPacked decoration"));
1101 }
1102
1103 TEST_F(ValidateDecorations, BufferBlockNestedStructGLSLPackedBad) {
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 OpMemberDecorate %S 0 Offset 0
1112 OpDecorate %S GLSLPacked
1113 OpMemberDecorate %Output 0 Offset 0
1114 OpMemberDecorate %Output 1 Offset 16
1115 OpMemberDecorate %Output 2 Offset 32
1116 OpDecorate %Output BufferBlock
1117 %void = OpTypeVoid
1118 %3 = OpTypeFunction %void
1119 %float = OpTypeFloat 32
1120 %v4float = OpTypeVector %float 4
1121 %int = OpTypeInt 32 1
1122 %S = OpTypeStruct %int
1123 %Output = OpTypeStruct %float %v4float %S
1124 %_ptr_Uniform_Output = OpTypePointer Uniform %Output
1125 %dataOutput = OpVariable %_ptr_Uniform_Output Uniform
1126 %main = OpFunction %void None %3
1127 %5 = OpLabel
1128 OpReturn
1129 OpFunctionEnd
1130 )";
1131
1132 CompileSuccessfully(spirv);
1133 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateAndRetrieveValidationState());
1134 EXPECT_THAT(getDiagnosticString(),
1135 HasSubstr("must not use GLSLPacked decoration"));
1136 }
1137
1138 TEST_F(ValidateDecorations, BlockMissingArrayStrideBad) {
1139 std::string spirv = R"(
1140 OpCapability Shader
1141 %1 = OpExtInstImport "GLSL.std.450"
1142 OpMemoryModel Logical GLSL450
1143 OpEntryPoint GLCompute %main "main"
1144 OpExecutionMode %main LocalSize 1 1 1
1145 OpSource GLSL 430
1146 OpDecorate %Output Block
1147 OpMemberDecorate %Output 0 Offset 0
1148 %void = OpTypeVoid
1149 %3 = OpTypeFunction %void
1150 %float = OpTypeFloat 32
1151 %int = OpTypeInt 32 1
1152 %int_3 = OpConstant %int 3
1153 %array = OpTypeArray %float %int_3
1154 %Output = OpTypeStruct %array
1155 %_ptr_Uniform_Output = OpTypePointer Uniform %Output
1156 %dataOutput = OpVariable %_ptr_Uniform_Output Uniform
1157 %main = OpFunction %void None %3
1158 %5 = OpLabel
1159 OpReturn
1160 OpFunctionEnd
1161 )";
1162
1163 CompileSuccessfully(spirv);
1164 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateAndRetrieveValidationState());
1165 EXPECT_THAT(
1166 getDiagnosticString(),
1167 HasSubstr("must be explicitly laid out with ArrayStride decorations"));
1168 }
1169
1170 TEST_F(ValidateDecorations, BufferBlockMissingArrayStrideBad) {
1171 std::string spirv = R"(
1172 OpCapability Shader
1173 %1 = OpExtInstImport "GLSL.std.450"
1174 OpMemoryModel Logical GLSL450
1175 OpEntryPoint GLCompute %main "main"
1176 OpExecutionMode %main LocalSize 1 1 1
1177 OpSource GLSL 430
1178 OpDecorate %Output BufferBlock
1179 OpMemberDecorate %Output 0 Offset 0
1180 %void = OpTypeVoid
1181 %3 = OpTypeFunction %void
1182 %float = OpTypeFloat 32
1183 %int = OpTypeInt 32 1
1184 %int_3 = OpConstant %int 3
1185 %array = OpTypeArray %float %int_3
1186 %Output = OpTypeStruct %array
1187 %_ptr_Uniform_Output = OpTypePointer Uniform %Output
1188 %dataOutput = OpVariable %_ptr_Uniform_Output Uniform
1189 %main = OpFunction %void None %3
1190 %5 = OpLabel
1191 OpReturn
1192 OpFunctionEnd
1193 )";
1194
1195 CompileSuccessfully(spirv);
1196 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateAndRetrieveValidationState());
1197 EXPECT_THAT(
1198 getDiagnosticString(),
1199 HasSubstr("must be explicitly laid out with ArrayStride decorations"));
1200 }
1201
1202 TEST_F(ValidateDecorations, BlockNestedStructMissingArrayStrideBad) {
1203 std::string spirv = R"(
1204 OpCapability Shader
1205 %1 = OpExtInstImport "GLSL.std.450"
1206 OpMemoryModel Logical GLSL450
1207 OpEntryPoint GLCompute %main "main"
1208 OpExecutionMode %main LocalSize 1 1 1
1209 OpSource GLSL 430
1210 OpMemberDecorate %S 0 Offset 0
1211 OpMemberDecorate %Output 0 Offset 0
1212 OpMemberDecorate %Output 1 Offset 16
1213 OpMemberDecorate %Output 2 Offset 32
1214 OpDecorate %Output Block
1215 %void = OpTypeVoid
1216 %3 = OpTypeFunction %void
1217 %float = OpTypeFloat 32
1218 %v4float = OpTypeVector %float 4
1219 %int = OpTypeInt 32 1
1220 %int_3 = OpConstant %int 3
1221 %array = OpTypeArray %float %int_3
1222 %S = OpTypeStruct %array
1223 %Output = OpTypeStruct %float %v4float %S
1224 %_ptr_Uniform_Output = OpTypePointer Uniform %Output
1225 %dataOutput = OpVariable %_ptr_Uniform_Output Uniform
1226 %main = OpFunction %void None %3
1227 %5 = OpLabel
1228 OpReturn
1229 OpFunctionEnd
1230 )";
1231
1232 CompileSuccessfully(spirv);
1233 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateAndRetrieveValidationState());
1234 EXPECT_THAT(
1235 getDiagnosticString(),
1236 HasSubstr("must be explicitly laid out with ArrayStride decorations"));
1237 }
1238
1239 TEST_F(ValidateDecorations, BufferBlockNestedStructMissingArrayStrideBad) {
1240 std::string spirv = R"(
1241 OpCapability Shader
1242 %1 = OpExtInstImport "GLSL.std.450"
1243 OpMemoryModel Logical GLSL450
1244 OpEntryPoint GLCompute %main "main"
1245 OpExecutionMode %main LocalSize 1 1 1
1246 OpSource GLSL 430
1247 OpMemberDecorate %S 0 Offset 0
1248 OpMemberDecorate %Output 0 Offset 0
1249 OpMemberDecorate %Output 1 Offset 16
1250 OpMemberDecorate %Output 2 Offset 32
1251 OpDecorate %Output BufferBlock
1252 %void = OpTypeVoid
1253 %3 = OpTypeFunction %void
1254 %float = OpTypeFloat 32
1255 %v4float = OpTypeVector %float 4
1256 %int = OpTypeInt 32 1
1257 %int_3 = OpConstant %int 3
1258 %array = OpTypeArray %float %int_3
1259 %S = OpTypeStruct %array
1260 %Output = OpTypeStruct %float %v4float %S
1261 %_ptr_Uniform_Output = OpTypePointer Uniform %Output
1262 %dataOutput = OpVariable %_ptr_Uniform_Output Uniform
1263 %main = OpFunction %void None %3
1264 %5 = OpLabel
1265 OpReturn
1266 OpFunctionEnd
1267 )";
1268
1269 CompileSuccessfully(spirv);
1270 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateAndRetrieveValidationState());
1271 EXPECT_THAT(
1272 getDiagnosticString(),
1273 HasSubstr("must be explicitly laid out with ArrayStride decorations"));
1274 }
1275
1276 TEST_F(ValidateDecorations, BlockMissingMatrixStrideBad) {
1277 std::string spirv = R"(
1278 OpCapability Shader
1279 %1 = OpExtInstImport "GLSL.std.450"
1280 OpMemoryModel Logical GLSL450
1281 OpEntryPoint GLCompute %main "main"
1282 OpExecutionMode %main LocalSize 1 1 1
1283 OpSource GLSL 430
1284 OpDecorate %Output Block
1285 OpMemberDecorate %Output 0 Offset 0
1286 %void = OpTypeVoid
1287 %3 = OpTypeFunction %void
1288 %float = OpTypeFloat 32
1289 %v3float = OpTypeVector %float 3
1290 %matrix = OpTypeMatrix %v3float 4
1291 %Output = OpTypeStruct %matrix
1292 %_ptr_Uniform_Output = OpTypePointer Uniform %Output
1293 %dataOutput = OpVariable %_ptr_Uniform_Output Uniform
1294 %main = OpFunction %void None %3
1295 %5 = OpLabel
1296 OpReturn
1297 OpFunctionEnd
1298 )";
1299
1300 CompileSuccessfully(spirv);
1301 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateAndRetrieveValidationState());
1302 EXPECT_THAT(
1303 getDiagnosticString(),
1304 HasSubstr("must be explicitly laid out with MatrixStride decorations"));
1305 }
1306
1307 TEST_F(ValidateDecorations, BufferBlockMissingMatrixStrideBad) {
1308 std::string spirv = R"(
1309 OpCapability Shader
1310 %1 = OpExtInstImport "GLSL.std.450"
1311 OpMemoryModel Logical GLSL450
1312 OpEntryPoint GLCompute %main "main"
1313 OpExecutionMode %main LocalSize 1 1 1
1314 OpSource GLSL 430
1315 OpDecorate %Output BufferBlock
1316 OpMemberDecorate %Output 0 Offset 0
1317 %void = OpTypeVoid
1318 %3 = OpTypeFunction %void
1319 %float = OpTypeFloat 32
1320 %v3float = OpTypeVector %float 3
1321 %matrix = OpTypeMatrix %v3float 4
1322 %Output = OpTypeStruct %matrix
1323 %_ptr_Uniform_Output = OpTypePointer Uniform %Output
1324 %dataOutput = OpVariable %_ptr_Uniform_Output Uniform
1325 %main = OpFunction %void None %3
1326 %5 = OpLabel
1327 OpReturn
1328 OpFunctionEnd
1329 )";
1330
1331 CompileSuccessfully(spirv);
1332 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateAndRetrieveValidationState());
1333 EXPECT_THAT(
1334 getDiagnosticString(),
1335 HasSubstr("must be explicitly laid out with MatrixStride decorations"));
1336 }
1337
1338 TEST_F(ValidateDecorations, BlockMissingMatrixStrideArrayBad) {
1339 std::string spirv = R"(
1340 OpCapability Shader
1341 %1 = OpExtInstImport "GLSL.std.450"
1342 OpMemoryModel Logical GLSL450
1343 OpEntryPoint GLCompute %main "main"
1344 OpExecutionMode %main LocalSize 1 1 1
1345 OpSource GLSL 430
1346 OpDecorate %Output Block
1347 OpMemberDecorate %Output 0 Offset 0
1348 %void = OpTypeVoid
1349 %3 = OpTypeFunction %void
1350 %float = OpTypeFloat 32
1351 %v3float = OpTypeVector %float 3
1352 %matrix = OpTypeMatrix %v3float 4
1353 %int = OpTypeInt 32 1
1354 %int_3 = OpConstant %int 3
1355 %array = OpTypeArray %matrix %int_3
1356 %Output = OpTypeStruct %matrix
1357 %_ptr_Uniform_Output = OpTypePointer Uniform %Output
1358 %dataOutput = OpVariable %_ptr_Uniform_Output Uniform
1359 %main = OpFunction %void None %3
1360 %5 = OpLabel
1361 OpReturn
1362 OpFunctionEnd
1363 )";
1364
1365 CompileSuccessfully(spirv);
1366 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateAndRetrieveValidationState());
1367 EXPECT_THAT(
1368 getDiagnosticString(),
1369 HasSubstr("must be explicitly laid out with MatrixStride decorations"));
1370 }
1371
1372 TEST_F(ValidateDecorations, BufferBlockMissingMatrixStrideArrayBad) {
1373 std::string spirv = R"(
1374 OpCapability Shader
1375 %1 = OpExtInstImport "GLSL.std.450"
1376 OpMemoryModel Logical GLSL450
1377 OpEntryPoint GLCompute %main "main"
1378 OpExecutionMode %main LocalSize 1 1 1
1379 OpSource GLSL 430
1380 OpDecorate %Output BufferBlock
1381 OpMemberDecorate %Output 0 Offset 0
1382 %void = OpTypeVoid
1383 %3 = OpTypeFunction %void
1384 %float = OpTypeFloat 32
1385 %v3float = OpTypeVector %float 3
1386 %matrix = OpTypeMatrix %v3float 4
1387 %int = OpTypeInt 32 1
1388 %int_3 = OpConstant %int 3
1389 %array = OpTypeArray %matrix %int_3
1390 %Output = OpTypeStruct %matrix
1391 %_ptr_Uniform_Output = OpTypePointer Uniform %Output
1392 %dataOutput = OpVariable %_ptr_Uniform_Output Uniform
1393 %main = OpFunction %void None %3
1394 %5 = OpLabel
1395 OpReturn
1396 OpFunctionEnd
1397 )";
1398
1399 CompileSuccessfully(spirv);
1400 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateAndRetrieveValidationState());
1401 EXPECT_THAT(
1402 getDiagnosticString(),
1403 HasSubstr("must be explicitly laid out with MatrixStride decorations"));
1404 }
1405
1406 TEST_F(ValidateDecorations, BlockNestedStructMissingMatrixStrideBad) {
1407 std::string spirv = R"(
1408 OpCapability Shader
1409 %1 = OpExtInstImport "GLSL.std.450"
1410 OpMemoryModel Logical GLSL450
1411 OpEntryPoint GLCompute %main "main"
1412 OpExecutionMode %main LocalSize 1 1 1
1413 OpSource GLSL 430
1414 OpMemberDecorate %S 0 Offset 0
1415 OpMemberDecorate %Output 0 Offset 0
1416 OpMemberDecorate %Output 1 Offset 16
1417 OpMemberDecorate %Output 2 Offset 32
1418 OpDecorate %Output Block
1419 %void = OpTypeVoid
1420 %3 = OpTypeFunction %void
1421 %float = OpTypeFloat 32
1422 %v3float = OpTypeVector %float 3
1423 %v4float = OpTypeVector %float 4
1424 %matrix = OpTypeMatrix %v3float 4
1425 %S = OpTypeStruct %matrix
1426 %Output = OpTypeStruct %float %v4float %S
1427 %_ptr_Uniform_Output = OpTypePointer Uniform %Output
1428 %dataOutput = OpVariable %_ptr_Uniform_Output Uniform
1429 %main = OpFunction %void None %3
1430 %5 = OpLabel
1431 OpReturn
1432 OpFunctionEnd
1433 )";
1434
1435 CompileSuccessfully(spirv);
1436 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateAndRetrieveValidationState());
1437 EXPECT_THAT(
1438 getDiagnosticString(),
1439 HasSubstr("must be explicitly laid out with MatrixStride decorations"));
1440 }
1441
1442 TEST_F(ValidateDecorations, BufferBlockNestedStructMissingMatrixStrideBad) {
1443 std::string spirv = R"(
1444 OpCapability Shader
1445 %1 = OpExtInstImport "GLSL.std.450"
1446 OpMemoryModel Logical GLSL450
1447 OpEntryPoint GLCompute %main "main"
1448 OpExecutionMode %main LocalSize 1 1 1
1449 OpSource GLSL 430
1450 OpMemberDecorate %S 0 Offset 0
1451 OpMemberDecorate %Output 0 Offset 0
1452 OpMemberDecorate %Output 1 Offset 16
1453 OpMemberDecorate %Output 2 Offset 32
1454 OpDecorate %Output BufferBlock
1455 %void = OpTypeVoid
1456 %3 = OpTypeFunction %void
1457 %float = OpTypeFloat 32
1458 %v3float = OpTypeVector %float 3
1459 %v4float = OpTypeVector %float 4
1460 %matrix = OpTypeMatrix %v3float 4
1461 %S = OpTypeStruct %matrix
1462 %Output = OpTypeStruct %float %v4float %S
1463 %_ptr_Uniform_Output = OpTypePointer Uniform %Output
1464 %dataOutput = OpVariable %_ptr_Uniform_Output Uniform
1465 %main = OpFunction %void None %3
1466 %5 = OpLabel
1467 OpReturn
1468 OpFunctionEnd
1469 )";
1470
1471 CompileSuccessfully(spirv);
1472 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateAndRetrieveValidationState());
1473 EXPECT_THAT(
1474 getDiagnosticString(),
1475 HasSubstr("must be explicitly laid out with MatrixStride decorations"));
1476 }
1477
1478 TEST_F(ValidateDecorations, BlockStandardUniformBufferLayout) {
1479 std::string spirv = R"(
1480 OpCapability Shader
1481 %1 = OpExtInstImport "GLSL.std.450"
1482 OpMemoryModel Logical GLSL450
1483 OpEntryPoint GLCompute %main "main"
1484 OpExecutionMode %main LocalSize 1 1 1
1485 OpSource GLSL 430
1486 OpMemberDecorate %F 0 Offset 0
1487 OpMemberDecorate %F 1 Offset 8
1488 OpDecorate %_arr_float_uint_2 ArrayStride 16
1489 OpDecorate %_arr_mat3v3float_uint_2 ArrayStride 48
1490 OpMemberDecorate %O 0 Offset 0
1491 OpMemberDecorate %O 1 Offset 16
1492 OpMemberDecorate %O 2 Offset 32
1493 OpMemberDecorate %O 3 Offset 64
1494 OpMemberDecorate %O 4 ColMajor
1495 OpMemberDecorate %O 4 Offset 80
1496 OpMemberDecorate %O 4 MatrixStride 16
1497 OpDecorate %_arr_O_uint_2 ArrayStride 176
1498 OpMemberDecorate %Output 0 Offset 0
1499 OpMemberDecorate %Output 1 Offset 8
1500 OpMemberDecorate %Output 2 Offset 16
1501 OpMemberDecorate %Output 3 Offset 32
1502 OpMemberDecorate %Output 4 Offset 48
1503 OpMemberDecorate %Output 5 Offset 64
1504 OpMemberDecorate %Output 6 ColMajor
1505 OpMemberDecorate %Output 6 Offset 96
1506 OpMemberDecorate %Output 6 MatrixStride 16
1507 OpMemberDecorate %Output 7 Offset 128
1508 OpDecorate %Output Block
1509 %void = OpTypeVoid
1510 %3 = OpTypeFunction %void
1511 %float = OpTypeFloat 32
1512 %v2float = OpTypeVector %float 2
1513 %v3float = OpTypeVector %float 3
1514 %int = OpTypeInt 32 1
1515 %uint = OpTypeInt 32 0
1516 %v2uint = OpTypeVector %uint 2
1517 %F = OpTypeStruct %int %v2uint
1518 %uint_2 = OpConstant %uint 2
1519 %_arr_float_uint_2 = OpTypeArray %float %uint_2
1520 %mat2v3float = OpTypeMatrix %v3float 2
1521 %v3uint = OpTypeVector %uint 3
1522 %mat3v3float = OpTypeMatrix %v3float 3
1523 %_arr_mat3v3float_uint_2 = OpTypeArray %mat3v3float %uint_2
1524 %O = OpTypeStruct %v3uint %v2float %_arr_float_uint_2 %v2float %_arr_mat3v3float_uint_2
1525 %_arr_O_uint_2 = OpTypeArray %O %uint_2
1526 %Output = OpTypeStruct %float %v2float %v3float %F %float %_arr_float_uint_2 %mat2v3float %_arr_O_uint_2
1527 %_ptr_Uniform_Output = OpTypePointer Uniform %Output
1528 %dataOutput = OpVariable %_ptr_Uniform_Output Uniform
1529 %main = OpFunction %void None %3
1530 %5 = OpLabel
1531 OpReturn
1532 OpFunctionEnd
1533 )";
1534
1535 CompileSuccessfully(spirv);
1536 EXPECT_EQ(SPV_SUCCESS, ValidateAndRetrieveValidationState());
1537 }
1538
1539 TEST_F(ValidateDecorations, BlockLayoutPermitsTightVec3ScalarPackingGood) {
1540 // See https://github.com/KhronosGroup/SPIRV-Tools/issues/1666
1541 std::string spirv = R"(
1542 OpCapability Shader
1543 OpMemoryModel Logical GLSL450
1544 OpEntryPoint Vertex %main "main"
1545 OpSource GLSL 450
1546 OpMemberDecorate %S 0 Offset 0
1547 OpMemberDecorate %S 1 Offset 12
1548 OpDecorate %S Block
1549 OpDecorate %B DescriptorSet 0
1550 OpDecorate %B Binding 0
1551 %void = OpTypeVoid
1552 %3 = OpTypeFunction %void
1553 %float = OpTypeFloat 32
1554 %v3float = OpTypeVector %float 3
1555 %S = OpTypeStruct %v3float %float
1556 %_ptr_Uniform_S = OpTypePointer Uniform %S
1557 %B = OpVariable %_ptr_Uniform_S Uniform
1558 %main = OpFunction %void None %3
1559 %5 = OpLabel
1560 OpReturn
1561 OpFunctionEnd
1562 )";
1563
1564 CompileSuccessfully(spirv);
1565 EXPECT_EQ(SPV_SUCCESS, ValidateAndRetrieveValidationState())
1566 << getDiagnosticString();
1567 }
1568
1569 TEST_F(ValidateDecorations, BlockCantAppearWithinABlockBad) {
1570 // See https://github.com/KhronosGroup/SPIRV-Tools/issues/1587
1571 std::string spirv = R"(
1572 OpCapability Shader
1573 OpMemoryModel Logical GLSL450
1574 OpEntryPoint Vertex %main "main"
1575 OpSource GLSL 450
1576 OpMemberDecorate %S 0 Offset 0
1577 OpMemberDecorate %S 1 Offset 16
1578 OpMemberDecorate %S2 0 Offset 0
1579 OpMemberDecorate %S2 1 Offset 12
1580 OpDecorate %S Block
1581 OpDecorate %S2 Block
1582 OpDecorate %B DescriptorSet 0
1583 OpDecorate %B Binding 0
1584 %void = OpTypeVoid
1585 %3 = OpTypeFunction %void
1586 %float = OpTypeFloat 32
1587 %S2 = OpTypeStruct %float %float
1588 %S = OpTypeStruct %float %S2
1589 %_ptr_Uniform_S = OpTypePointer Uniform %S
1590 %B = OpVariable %_ptr_Uniform_S Uniform
1591 %main = OpFunction %void None %3
1592 %5 = OpLabel
1593 OpReturn
1594 OpFunctionEnd
1595 )";
1596
1597 CompileSuccessfully(spirv);
1598 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateAndRetrieveValidationState());
1599 EXPECT_THAT(getDiagnosticString(),
1600 HasSubstr("rules: A Block or BufferBlock cannot be nested within "
1601 "another Block or BufferBlock."));
1602 }
1603
1604 TEST_F(ValidateDecorations, BufferblockCantAppearWithinABufferblockBad) {
1605 // See https://github.com/KhronosGroup/SPIRV-Tools/issues/1587
1606 std::string spirv = R"(
1607 OpCapability Shader
1608 OpMemoryModel Logical GLSL450
1609 OpEntryPoint Vertex %main "main"
1610 OpSource GLSL 450
1611 OpMemberDecorate %S 0 Offset 0
1612 OpMemberDecorate %S 1 Offset 16
1613 OpMemberDecorate %S2 0 Offset 0
1614 OpMemberDecorate %S2 1 Offset 16
1615 OpMemberDecorate %S3 0 Offset 0
1616 OpMemberDecorate %S3 1 Offset 12
1617 OpDecorate %S BufferBlock
1618 OpDecorate %S3 BufferBlock
1619 OpDecorate %B DescriptorSet 0
1620 OpDecorate %B Binding 0
1621 %void = OpTypeVoid
1622 %3 = OpTypeFunction %void
1623 %float = OpTypeFloat 32
1624 %S3 = OpTypeStruct %float %float
1625 %S2 = OpTypeStruct %float %S3
1626 %S = OpTypeStruct %float %S2
1627 %_ptr_Uniform_S = OpTypePointer Uniform %S
1628 %B = OpVariable %_ptr_Uniform_S Uniform
1629 %main = OpFunction %void None %3
1630 %5 = OpLabel
1631 OpReturn
1632 OpFunctionEnd
1633 )";
1634
1635 CompileSuccessfully(spirv);
1636 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateAndRetrieveValidationState());
1637 EXPECT_THAT(getDiagnosticString(),
1638 HasSubstr("rules: A Block or BufferBlock cannot be nested within "
1639 "another Block or BufferBlock."));
1640 }
1641
1642 TEST_F(ValidateDecorations, BufferblockCantAppearWithinABlockBad) {
1643 // See https://github.com/KhronosGroup/SPIRV-Tools/issues/1587
1644 std::string spirv = R"(
1645 OpCapability Shader
1646 OpMemoryModel Logical GLSL450
1647 OpEntryPoint Vertex %main "main"
1648 OpSource GLSL 450
1649 OpMemberDecorate %S 0 Offset 0
1650 OpMemberDecorate %S 1 Offset 16
1651 OpMemberDecorate %S2 0 Offset 0
1652 OpMemberDecorate %S2 1 Offset 16
1653 OpMemberDecorate %S3 0 Offset 0
1654 OpMemberDecorate %S3 1 Offset 12
1655 OpDecorate %S Block
1656 OpDecorate %S3 BufferBlock
1657 OpDecorate %B DescriptorSet 0
1658 OpDecorate %B Binding 0
1659 %void = OpTypeVoid
1660 %3 = OpTypeFunction %void
1661 %float = OpTypeFloat 32
1662 %S3 = OpTypeStruct %float %float
1663 %S2 = OpTypeStruct %float %S3
1664 %S = OpTypeStruct %float %S2
1665 %_ptr_Uniform_S = OpTypePointer Uniform %S
1666 %B = OpVariable %_ptr_Uniform_S Uniform
1667 %main = OpFunction %void None %3
1668 %5 = OpLabel
1669 OpReturn
1670 OpFunctionEnd
1671 )";
1672
1673 CompileSuccessfully(spirv);
1674 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateAndRetrieveValidationState());
1675 EXPECT_THAT(getDiagnosticString(),
1676 HasSubstr("rules: A Block or BufferBlock cannot be nested within "
1677 "another Block or BufferBlock."));
1678 }
1679
1680 TEST_F(ValidateDecorations, BlockCantAppearWithinABufferblockBad) {
1681 // See https://github.com/KhronosGroup/SPIRV-Tools/issues/1587
1682 std::string spirv = R"(
1683 OpCapability Shader
1684 OpMemoryModel Logical GLSL450
1685 OpEntryPoint Vertex %main "main"
1686 OpSource GLSL 450
1687 OpMemberDecorate %S 0 Offset 0
1688 OpMemberDecorate %S 1 Offset 16
1689 OpMemberDecorate %S2 0 Offset 0
1690 OpMemberDecorate %S2 1 Offset 16
1691 OpMemberDecorate %S3 0 Offset 0
1692 OpMemberDecorate %S3 1 Offset 16
1693 OpMemberDecorate %S4 0 Offset 0
1694 OpMemberDecorate %S4 1 Offset 12
1695 OpDecorate %S BufferBlock
1696 OpDecorate %S4 Block
1697 OpDecorate %B DescriptorSet 0
1698 OpDecorate %B Binding 0
1699 %void = OpTypeVoid
1700 %3 = OpTypeFunction %void
1701 %float = OpTypeFloat 32
1702 %S4 = OpTypeStruct %float %float
1703 %S3 = OpTypeStruct %float %S4
1704 %S2 = OpTypeStruct %float %S3
1705 %S = OpTypeStruct %float %S2
1706 %_ptr_Uniform_S = OpTypePointer Uniform %S
1707 %B = OpVariable %_ptr_Uniform_S Uniform
1708 %main = OpFunction %void None %3
1709 %5 = OpLabel
1710 OpReturn
1711 OpFunctionEnd
1712 )";
1713
1714 CompileSuccessfully(spirv);
1715 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateAndRetrieveValidationState());
1716 EXPECT_THAT(getDiagnosticString(),
1717 HasSubstr("rules: A Block or BufferBlock cannot be nested within "
1718 "another Block or BufferBlock."));
1719 }
1720
1721 TEST_F(ValidateDecorations, BlockLayoutForbidsTightScalarVec3PackingBad) {
1722 // See https://github.com/KhronosGroup/SPIRV-Tools/issues/1666
1723 std::string spirv = R"(
1724 OpCapability Shader
1725 OpMemoryModel Logical GLSL450
1726 OpEntryPoint Vertex %main "main"
1727 OpSource GLSL 450
1728 OpMemberDecorate %S 0 Offset 0
1729 OpMemberDecorate %S 1 Offset 4
1730 OpDecorate %S Block
1731 OpDecorate %B DescriptorSet 0
1732 OpDecorate %B Binding 0
1733 %void = OpTypeVoid
1734 %3 = OpTypeFunction %void
1735 %float = OpTypeFloat 32
1736 %v3float = OpTypeVector %float 3
1737 %S = OpTypeStruct %float %v3float
1738 %_ptr_Uniform_S = OpTypePointer Uniform %S
1739 %B = OpVariable %_ptr_Uniform_S Uniform
1740 %main = OpFunction %void None %3
1741 %5 = OpLabel
1742 OpReturn
1743 OpFunctionEnd
1744 )";
1745
1746 CompileSuccessfully(spirv);
1747 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateAndRetrieveValidationState());
1748 EXPECT_THAT(
1749 getDiagnosticString(),
1750 HasSubstr("Structure id 2 decorated as Block for variable in Uniform "
1751 "storage class must follow standard uniform buffer layout "
1752 "rules: member 1 at offset 4 is not aligned to 16"));
1753 }
1754
1755 TEST_F(ValidateDecorations,
1756 BlockLayoutPermitsTightScalarVec3PackingWithRelaxedLayoutGood) {
1757 // Same as previous test, but with explicit option to relax block layout.
1758 std::string spirv = R"(
1759 OpCapability Shader
1760 OpMemoryModel Logical GLSL450
1761 OpEntryPoint Vertex %main "main"
1762 OpSource GLSL 450
1763 OpMemberDecorate %S 0 Offset 0
1764 OpMemberDecorate %S 1 Offset 4
1765 OpDecorate %S Block
1766 OpDecorate %B DescriptorSet 0
1767 OpDecorate %B Binding 0
1768 %void = OpTypeVoid
1769 %3 = OpTypeFunction %void
1770 %float = OpTypeFloat 32
1771 %v3float = OpTypeVector %float 3
1772 %S = OpTypeStruct %float %v3float
1773 %_ptr_Uniform_S = OpTypePointer Uniform %S
1774 %B = OpVariable %_ptr_Uniform_S Uniform
1775 %main = OpFunction %void None %3
1776 %5 = OpLabel
1777 OpReturn
1778 OpFunctionEnd
1779 )";
1780
1781 CompileSuccessfully(spirv);
1782 spvValidatorOptionsSetRelaxBlockLayout(getValidatorOptions(), true);
1783 EXPECT_EQ(SPV_SUCCESS,
1784 ValidateAndRetrieveValidationState(SPV_ENV_VULKAN_1_0));
1785 EXPECT_THAT(getDiagnosticString(), Eq(""));
1786 }
1787
1788 TEST_F(ValidateDecorations,
1789 BlockLayoutPermitsTightScalarVec3PackingBadOffsetWithRelaxedLayoutBad) {
1790 // Same as previous test, but with the vector not aligned to its scalar
1791 // element. Use offset 5 instead of a multiple of 4.
1792 std::string spirv = R"(
1793 OpCapability Shader
1794 OpMemoryModel Logical GLSL450
1795 OpEntryPoint Vertex %main "main"
1796 OpSource GLSL 450
1797 OpMemberDecorate %S 0 Offset 0
1798 OpMemberDecorate %S 1 Offset 5
1799 OpDecorate %S Block
1800 OpDecorate %B DescriptorSet 0
1801 OpDecorate %B Binding 0
1802 %void = OpTypeVoid
1803 %3 = OpTypeFunction %void
1804 %float = OpTypeFloat 32
1805 %v3float = OpTypeVector %float 3
1806 %S = OpTypeStruct %float %v3float
1807 %_ptr_Uniform_S = OpTypePointer Uniform %S
1808 %B = OpVariable %_ptr_Uniform_S Uniform
1809 %main = OpFunction %void None %3
1810 %5 = OpLabel
1811 OpReturn
1812 OpFunctionEnd
1813 )";
1814
1815 CompileSuccessfully(spirv);
1816 spvValidatorOptionsSetRelaxBlockLayout(getValidatorOptions(), true);
1817 EXPECT_EQ(SPV_ERROR_INVALID_ID,
1818 ValidateAndRetrieveValidationState(SPV_ENV_VULKAN_1_0));
1819 EXPECT_THAT(
1820 getDiagnosticString(),
1821 HasSubstr(
1822 "Structure id 2 decorated as Block for variable in Uniform storage "
1823 "class must follow relaxed uniform buffer layout rules: member 1 at "
1824 "offset 5 is not aligned to scalar element size 4"));
1825 }
1826
1827 TEST_F(ValidateDecorations,
1828 BlockLayoutPermitsTightScalarVec3PackingWithVulkan1_1Good) {
1829 // Same as previous test, but with Vulkan 1.1. Vulkan 1.1 included
1830 // VK_KHR_relaxed_block_layout in core.
1831 std::string spirv = R"(
1832 OpCapability Shader
1833 OpMemoryModel Logical GLSL450
1834 OpEntryPoint Vertex %main "main"
1835 OpSource GLSL 450
1836 OpMemberDecorate %S 0 Offset 0
1837 OpMemberDecorate %S 1 Offset 4
1838 OpDecorate %S Block
1839 OpDecorate %B DescriptorSet 0
1840 OpDecorate %B Binding 0
1841 %void = OpTypeVoid
1842 %3 = OpTypeFunction %void
1843 %float = OpTypeFloat 32
1844 %v3float = OpTypeVector %float 3
1845 %S = OpTypeStruct %float %v3float
1846 %_ptr_Uniform_S = OpTypePointer Uniform %S
1847 %B = OpVariable %_ptr_Uniform_S Uniform
1848 %main = OpFunction %void None %3
1849 %5 = OpLabel
1850 OpReturn
1851 OpFunctionEnd
1852 )";
1853
1854 CompileSuccessfully(spirv);
1855 EXPECT_EQ(SPV_SUCCESS,
1856 ValidateAndRetrieveValidationState(SPV_ENV_VULKAN_1_1));
1857 EXPECT_THAT(getDiagnosticString(), Eq(""));
1858 }
1859
1860 TEST_F(ValidateDecorations,
1861 BlockLayoutPermitsTightScalarVec3PackingWithScalarLayoutGood) {
1862 // Same as previous test, but with scalar block layout.
1863 std::string spirv = R"(
1864 OpCapability Shader
1865 OpMemoryModel Logical GLSL450
1866 OpEntryPoint Vertex %main "main"
1867 OpSource GLSL 450
1868 OpMemberDecorate %S 0 Offset 0
1869 OpMemberDecorate %S 1 Offset 4
1870 OpDecorate %S Block
1871 OpDecorate %B DescriptorSet 0
1872 OpDecorate %B Binding 0
1873 %void = OpTypeVoid
1874 %3 = OpTypeFunction %void
1875 %float = OpTypeFloat 32
1876 %v3float = OpTypeVector %float 3
1877 %S = OpTypeStruct %float %v3float
1878 %_ptr_Uniform_S = OpTypePointer Uniform %S
1879 %B = OpVariable %_ptr_Uniform_S Uniform
1880 %main = OpFunction %void None %3
1881 %5 = OpLabel
1882 OpReturn
1883 OpFunctionEnd
1884 )";
1885
1886 CompileSuccessfully(spirv);
1887 spvValidatorOptionsSetScalarBlockLayout(getValidatorOptions(), true);
1888 EXPECT_EQ(SPV_SUCCESS,
1889 ValidateAndRetrieveValidationState(SPV_ENV_VULKAN_1_0));
1890 EXPECT_THAT(getDiagnosticString(), Eq(""));
1891 }
1892
1893 TEST_F(ValidateDecorations,
1894 BlockLayoutPermitsScalarAlignedArrayWithScalarLayoutGood) {
1895 // The array at offset 4 is ok with scalar block layout.
1896 std::string spirv = R"(
1897 OpCapability Shader
1898 OpMemoryModel Logical GLSL450
1899 OpEntryPoint Vertex %main "main"
1900 OpSource GLSL 450
1901 OpMemberDecorate %S 0 Offset 0
1902 OpMemberDecorate %S 1 Offset 4
1903 OpDecorate %S Block
1904 OpDecorate %B DescriptorSet 0
1905 OpDecorate %B Binding 0
1906 OpDecorate %arr_float ArrayStride 4
1907 %void = OpTypeVoid
1908 %3 = OpTypeFunction %void
1909 %uint = OpTypeInt 32 0
1910 %uint_3 = OpConstant %uint 3
1911 %float = OpTypeFloat 32
1912 %arr_float = OpTypeArray %float %uint_3
1913 %S = OpTypeStruct %float %arr_float
1914 %_ptr_Uniform_S = OpTypePointer Uniform %S
1915 %B = OpVariable %_ptr_Uniform_S Uniform
1916 %main = OpFunction %void None %3
1917 %5 = OpLabel
1918 OpReturn
1919 OpFunctionEnd
1920 )";
1921
1922 CompileSuccessfully(spirv);
1923 spvValidatorOptionsSetScalarBlockLayout(getValidatorOptions(), true);
1924 EXPECT_EQ(SPV_SUCCESS,
1925 ValidateAndRetrieveValidationState(SPV_ENV_VULKAN_1_0));
1926 EXPECT_THAT(getDiagnosticString(), Eq(""));
1927 }
1928
1929 TEST_F(ValidateDecorations,
1930 BlockLayoutPermitsScalarAlignedArrayOfVec3WithScalarLayoutGood) {
1931 // The array at offset 4 is ok with scalar block layout, even though
1932 // its elements are vec3.
1933 // This is the same as the previous case, but the array elements are vec3
1934 // instead of float.
1935 std::string spirv = R"(
1936 OpCapability Shader
1937 OpMemoryModel Logical GLSL450
1938 OpEntryPoint Vertex %main "main"
1939 OpSource GLSL 450
1940 OpMemberDecorate %S 0 Offset 0
1941 OpMemberDecorate %S 1 Offset 4
1942 OpDecorate %S Block
1943 OpDecorate %B DescriptorSet 0
1944 OpDecorate %B Binding 0
1945 OpDecorate %arr_vec3 ArrayStride 12
1946 %void = OpTypeVoid
1947 %3 = OpTypeFunction %void
1948 %uint = OpTypeInt 32 0
1949 %uint_3 = OpConstant %uint 3
1950 %float = OpTypeFloat 32
1951 %vec3 = OpTypeVector %float 3
1952 %arr_vec3 = OpTypeArray %vec3 %uint_3
1953 %S = OpTypeStruct %float %arr_vec3
1954 %_ptr_Uniform_S = OpTypePointer Uniform %S
1955 %B = OpVariable %_ptr_Uniform_S Uniform
1956 %main = OpFunction %void None %3
1957 %5 = OpLabel
1958 OpReturn
1959 OpFunctionEnd
1960 )";
1961
1962 CompileSuccessfully(spirv);
1963 spvValidatorOptionsSetScalarBlockLayout(getValidatorOptions(), true);
1964 EXPECT_EQ(SPV_SUCCESS,
1965 ValidateAndRetrieveValidationState(SPV_ENV_VULKAN_1_0));
1966 EXPECT_THAT(getDiagnosticString(), Eq(""));
1967 }
1968
1969 TEST_F(ValidateDecorations,
1970 BlockLayoutPermitsScalarAlignedStructWithScalarLayoutGood) {
1971 // Scalar block layout permits the struct at offset 4, even though
1972 // it contains a vector with base alignment 8 and scalar alignment 4.
1973 std::string spirv = R"(
1974 OpCapability Shader
1975 OpMemoryModel Logical GLSL450
1976 OpEntryPoint Vertex %main "main"
1977 OpSource GLSL 450
1978 OpMemberDecorate %S 0 Offset 0
1979 OpMemberDecorate %S 1 Offset 4
1980 OpMemberDecorate %st 0 Offset 0
1981 OpMemberDecorate %st 1 Offset 8
1982 OpDecorate %S Block
1983 OpDecorate %B DescriptorSet 0
1984 OpDecorate %B Binding 0
1985 %void = OpTypeVoid
1986 %3 = OpTypeFunction %void
1987 %float = OpTypeFloat 32
1988 %vec2 = OpTypeVector %float 2
1989 %st = OpTypeStruct %vec2 %float
1990 %S = OpTypeStruct %float %st
1991 %_ptr_Uniform_S = OpTypePointer Uniform %S
1992 %B = OpVariable %_ptr_Uniform_S Uniform
1993 %main = OpFunction %void None %3
1994 %5 = OpLabel
1995 OpReturn
1996 OpFunctionEnd
1997 )";
1998
1999 CompileSuccessfully(spirv);
2000 spvValidatorOptionsSetScalarBlockLayout(getValidatorOptions(), true);
2001 EXPECT_EQ(SPV_SUCCESS,
2002 ValidateAndRetrieveValidationState(SPV_ENV_VULKAN_1_0));
2003 EXPECT_THAT(getDiagnosticString(), Eq(""));
2004 }
2005
2006 TEST_F(
2007 ValidateDecorations,
2008 BlockLayoutPermitsFieldsInBaseAlignmentPaddingAtEndOfStructWithScalarLayoutGood) {
2009 // Scalar block layout permits fields in what would normally be the padding at
2010 // the end of a struct.
2011 std::string spirv = R"(
2012 OpCapability Shader
2013 OpCapability Float64
2014 OpMemoryModel Logical GLSL450
2015 OpEntryPoint Vertex %main "main"
2016 OpSource GLSL 450
2017 OpMemberDecorate %st 0 Offset 0
2018 OpMemberDecorate %st 1 Offset 8
2019 OpMemberDecorate %S 0 Offset 0
2020 OpMemberDecorate %S 1 Offset 12
2021 OpDecorate %S Block
2022 OpDecorate %B DescriptorSet 0
2023 OpDecorate %B Binding 0
2024 %void = OpTypeVoid
2025 %3 = OpTypeFunction %void
2026 %float = OpTypeFloat 32
2027 %double = OpTypeFloat 64
2028 %st = OpTypeStruct %double %float
2029 %S = OpTypeStruct %st %float
2030 %_ptr_Uniform_S = OpTypePointer Uniform %S
2031 %B = OpVariable %_ptr_Uniform_S Uniform
2032 %main = OpFunction %void None %3
2033 %5 = OpLabel
2034 OpReturn
2035 OpFunctionEnd
2036 )";
2037
2038 CompileSuccessfully(spirv);
2039 spvValidatorOptionsSetScalarBlockLayout(getValidatorOptions(), true);
2040 EXPECT_EQ(SPV_SUCCESS,
2041 ValidateAndRetrieveValidationState(SPV_ENV_VULKAN_1_0));
2042 EXPECT_THAT(getDiagnosticString(), Eq(""));
2043 }
2044
2045 TEST_F(
2046 ValidateDecorations,
2047 BlockLayoutPermitsStraddlingVectorWithScalarLayoutOverrideRelaxBlockLayoutGood) {
2048 // Same as previous, but set relaxed block layout first. Scalar layout always
2049 // wins.
2050 std::string spirv = R"(
2051 OpCapability Shader
2052 OpMemoryModel Logical GLSL450
2053 OpEntryPoint Vertex %main "main"
2054 OpSource GLSL 450
2055 OpMemberDecorate %S 0 Offset 0
2056 OpMemberDecorate %S 1 Offset 4
2057 OpDecorate %S Block
2058 OpDecorate %B DescriptorSet 0
2059 OpDecorate %B Binding 0
2060 %void = OpTypeVoid
2061 %3 = OpTypeFunction %void
2062 %float = OpTypeFloat 32
2063 %vec4 = OpTypeVector %float 4
2064 %S = OpTypeStruct %float %vec4
2065 %_ptr_Uniform_S = OpTypePointer Uniform %S
2066 %B = OpVariable %_ptr_Uniform_S Uniform
2067 %main = OpFunction %void None %3
2068 %5 = OpLabel
2069 OpReturn
2070 OpFunctionEnd
2071 )";
2072
2073 CompileSuccessfully(spirv);
2074 spvValidatorOptionsSetRelaxBlockLayout(getValidatorOptions(), true);
2075 spvValidatorOptionsSetScalarBlockLayout(getValidatorOptions(), true);
2076 EXPECT_EQ(SPV_SUCCESS,
2077 ValidateAndRetrieveValidationState(SPV_ENV_VULKAN_1_0));
2078 EXPECT_THAT(getDiagnosticString(), Eq(""));
2079 }
2080
2081 TEST_F(
2082 ValidateDecorations,
2083 BlockLayoutPermitsStraddlingVectorWithRelaxedLayoutOverridenByScalarBlockLayoutGood) {
2084 // Same as previous, but set scalar block layout first. Scalar layout always
2085 // wins.
2086 std::string spirv = R"(
2087 OpCapability Shader
2088 OpMemoryModel Logical GLSL450
2089 OpEntryPoint Vertex %main "main"
2090 OpSource GLSL 450
2091 OpMemberDecorate %S 0 Offset 0
2092 OpMemberDecorate %S 1 Offset 4
2093 OpDecorate %S Block
2094 OpDecorate %B DescriptorSet 0
2095 OpDecorate %B Binding 0
2096 %void = OpTypeVoid
2097 %3 = OpTypeFunction %void
2098 %float = OpTypeFloat 32
2099 %vec4 = OpTypeVector %float 4
2100 %S = OpTypeStruct %float %vec4
2101 %_ptr_Uniform_S = OpTypePointer Uniform %S
2102 %B = OpVariable %_ptr_Uniform_S Uniform
2103 %main = OpFunction %void None %3
2104 %5 = OpLabel
2105 OpReturn
2106 OpFunctionEnd
2107 )";
2108
2109 CompileSuccessfully(spirv);
2110 spvValidatorOptionsSetScalarBlockLayout(getValidatorOptions(), true);
2111 spvValidatorOptionsSetRelaxBlockLayout(getValidatorOptions(), true);
2112 EXPECT_EQ(SPV_SUCCESS,
2113 ValidateAndRetrieveValidationState(SPV_ENV_VULKAN_1_0));
2114 EXPECT_THAT(getDiagnosticString(), Eq(""));
2115 }
2116
2117 TEST_F(ValidateDecorations, BufferBlock16bitStandardStorageBufferLayout) {
2118 std::string spirv = R"(
2119 OpCapability Shader
2120 OpCapability StorageUniform16
2121 OpExtension "SPV_KHR_16bit_storage"
2122 OpMemoryModel Logical GLSL450
2123 OpEntryPoint GLCompute %main "main"
2124 OpExecutionMode %main LocalSize 1 1 1
2125 OpDecorate %f32arr ArrayStride 4
2126 OpDecorate %f16arr ArrayStride 2
2127 OpMemberDecorate %SSBO32 0 Offset 0
2128 OpMemberDecorate %SSBO16 0 Offset 0
2129 OpDecorate %SSBO32 BufferBlock
2130 OpDecorate %SSBO16 BufferBlock
2131 %void = OpTypeVoid
2132 %voidf = OpTypeFunction %void
2133 %u32 = OpTypeInt 32 0
2134 %i32 = OpTypeInt 32 1
2135 %f32 = OpTypeFloat 32
2136 %uvec3 = OpTypeVector %u32 3
2137 %c_i32_32 = OpConstant %i32 32
2138 %c_i32_128 = OpConstant %i32 128
2139 %f32arr = OpTypeArray %f32 %c_i32_128
2140 %f16 = OpTypeFloat 16
2141 %f16arr = OpTypeArray %f16 %c_i32_128
2142 %SSBO32 = OpTypeStruct %f32arr
2143 %SSBO16 = OpTypeStruct %f16arr
2144 %_ptr_Uniform_SSBO32 = OpTypePointer Uniform %SSBO32
2145 %varSSBO32 = OpVariable %_ptr_Uniform_SSBO32 Uniform
2146 %_ptr_Uniform_SSBO16 = OpTypePointer Uniform %SSBO16
2147 %varSSBO16 = OpVariable %_ptr_Uniform_SSBO16 Uniform
2148 %main = OpFunction %void None %voidf
2149 %label = OpLabel
2150 OpReturn
2151 OpFunctionEnd
2152 )";
2153
2154 CompileSuccessfully(spirv);
2155 EXPECT_EQ(SPV_SUCCESS, ValidateAndRetrieveValidationState());
2156 }
2157
2158 TEST_F(ValidateDecorations, BlockArrayExtendedAlignmentGood) {
2159 // For uniform buffer, Array base alignment is 16, and ArrayStride
2160 // must be a multiple of 16.
2161 std::string spirv = R"(
2162 OpCapability Shader
2163 OpMemoryModel Logical GLSL450
2164 OpEntryPoint Vertex %main "main"
2165 OpSource GLSL 450
2166 OpDecorate %_arr_float_uint_2 ArrayStride 16
2167 OpMemberDecorate %S 0 Offset 0
2168 OpMemberDecorate %S 1 Offset 16
2169 OpDecorate %S Block
2170 %void = OpTypeVoid
2171 %3 = OpTypeFunction %void
2172 %float = OpTypeFloat 32
2173 %v2float = OpTypeVector %float 2
2174 %uint = OpTypeInt 32 0
2175 %uint_2 = OpConstant %uint 2
2176 %_arr_float_uint_2 = OpTypeArray %float %uint_2
2177 %S = OpTypeStruct %v2float %_arr_float_uint_2
2178 %_ptr_PushConstant_S = OpTypePointer PushConstant %S
2179 %u = OpVariable %_ptr_PushConstant_S PushConstant
2180 %main = OpFunction %void None %3
2181 %5 = OpLabel
2182 OpReturn
2183 OpFunctionEnd
2184 )";
2185
2186 CompileSuccessfully(spirv);
2187 EXPECT_EQ(SPV_SUCCESS, ValidateAndRetrieveValidationState())
2188 << getDiagnosticString();
2189 }
2190
2191 TEST_F(ValidateDecorations, BlockArrayBaseAlignmentBad) {
2192 // For uniform buffer, Array base alignment is 16.
2193 std::string spirv = R"(
2194 OpCapability Shader
2195 OpMemoryModel Logical GLSL450
2196 OpEntryPoint Vertex %main "main"
2197 OpSource GLSL 450
2198 OpDecorate %_arr_float_uint_2 ArrayStride 16
2199 OpMemberDecorate %S 0 Offset 0
2200 OpMemberDecorate %S 1 Offset 8
2201 OpDecorate %S Block
2202 %void = OpTypeVoid
2203 %3 = OpTypeFunction %void
2204 %float = OpTypeFloat 32
2205 %v2float = OpTypeVector %float 2
2206 %uint = OpTypeInt 32 0
2207 %uint_2 = OpConstant %uint 2
2208 %_arr_float_uint_2 = OpTypeArray %float %uint_2
2209 %S = OpTypeStruct %v2float %_arr_float_uint_2
2210 %_ptr_Uniform_S = OpTypePointer Uniform %S
2211 %u = OpVariable %_ptr_Uniform_S Uniform
2212 %main = OpFunction %void None %3
2213 %5 = OpLabel
2214 OpReturn
2215 OpFunctionEnd
2216 )";
2217
2218 CompileSuccessfully(spirv);
2219 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateAndRetrieveValidationState());
2220 EXPECT_THAT(
2221 getDiagnosticString(),
2222 HasSubstr(
2223 "Structure id 3 decorated as Block for variable in Uniform "
2224 "storage class must follow standard uniform buffer layout rules: "
2225 "member 1 at offset 8 is not aligned to 16"));
2226 }
2227
2228 TEST_F(ValidateDecorations, BlockArrayBaseAlignmentWithRelaxedLayoutStillBad) {
2229 // For uniform buffer, Array base alignment is 16, and ArrayStride
2230 // must be a multiple of 16. This case uses relaxed block layout. Relaxed
2231 // layout only relaxes rules for vector alignment, not array alignment.
2232 std::string spirv = R"(
2233 OpCapability Shader
2234 OpMemoryModel Logical GLSL450
2235 OpEntryPoint Vertex %main "main"
2236 OpSource GLSL 450
2237 OpDecorate %_arr_float_uint_2 ArrayStride 16
2238 OpDecorate %u DescriptorSet 0
2239 OpDecorate %u Binding 0
2240 OpMemberDecorate %S 0 Offset 0
2241 OpMemberDecorate %S 1 Offset 8
2242 OpDecorate %S Block
2243 %void = OpTypeVoid
2244 %3 = OpTypeFunction %void
2245 %float = OpTypeFloat 32
2246 %v2float = OpTypeVector %float 2
2247 %uint = OpTypeInt 32 0
2248 %uint_2 = OpConstant %uint 2
2249 %_arr_float_uint_2 = OpTypeArray %float %uint_2
2250 %S = OpTypeStruct %v2float %_arr_float_uint_2
2251 %_ptr_Uniform_S = OpTypePointer Uniform %S
2252 %u = OpVariable %_ptr_Uniform_S Uniform
2253 %main = OpFunction %void None %3
2254 %5 = OpLabel
2255 OpReturn
2256 OpFunctionEnd
2257 )";
2258
2259 CompileSuccessfully(spirv);
2260 EXPECT_EQ(SPV_ERROR_INVALID_ID,
2261 ValidateAndRetrieveValidationState(SPV_ENV_VULKAN_1_0));
2262 spvValidatorOptionsSetRelaxBlockLayout(getValidatorOptions(), true);
2263 EXPECT_THAT(
2264 getDiagnosticString(),
2265 HasSubstr(
2266 "Structure id 4 decorated as Block for variable in Uniform "
2267 "storage class must follow standard uniform buffer layout rules: "
2268 "member 1 at offset 8 is not aligned to 16"));
2269 }
2270
2271 TEST_F(ValidateDecorations, BlockArrayBaseAlignmentWithVulkan1_1StillBad) {
2272 // Same as previous test, but with Vulkan 1.1, which includes
2273 // VK_KHR_relaxed_block_layout in core.
2274 std::string spirv = R"(
2275 OpCapability Shader
2276 OpMemoryModel Logical GLSL450
2277 OpEntryPoint Vertex %main "main"
2278 OpSource GLSL 450
2279 OpDecorate %_arr_float_uint_2 ArrayStride 16
2280 OpDecorate %u DescriptorSet 0
2281 OpDecorate %u Binding 0
2282 OpMemberDecorate %S 0 Offset 0
2283 OpMemberDecorate %S 1 Offset 8
2284 OpDecorate %S Block
2285 %void = OpTypeVoid
2286 %3 = OpTypeFunction %void
2287 %float = OpTypeFloat 32
2288 %v2float = OpTypeVector %float 2
2289 %uint = OpTypeInt 32 0
2290 %uint_2 = OpConstant %uint 2
2291 %_arr_float_uint_2 = OpTypeArray %float %uint_2
2292 %S = OpTypeStruct %v2float %_arr_float_uint_2
2293 %_ptr_Uniform_S = OpTypePointer Uniform %S
2294 %u = OpVariable %_ptr_Uniform_S Uniform
2295 %main = OpFunction %void None %3
2296 %5 = OpLabel
2297 OpReturn
2298 OpFunctionEnd
2299 )";
2300
2301 CompileSuccessfully(spirv);
2302 EXPECT_EQ(SPV_ERROR_INVALID_ID,
2303 ValidateAndRetrieveValidationState(SPV_ENV_VULKAN_1_1));
2304 EXPECT_THAT(
2305 getDiagnosticString(),
2306 HasSubstr(
2307 "Structure id 4 decorated as Block for variable in Uniform "
2308 "storage class must follow relaxed uniform buffer layout rules: "
2309 "member 1 at offset 8 is not aligned to 16"));
2310 }
2311
2312 TEST_F(ValidateDecorations,
2313 BlockArrayBaseAlignmentWithBlockStandardLayoutGood) {
2314 // Same as previous test, but with VK_KHR_uniform_buffer_standard_layout
2315 std::string spirv = R"(
2316 OpCapability Shader
2317 OpMemoryModel Logical GLSL450
2318 OpEntryPoint Vertex %main "main"
2319 OpSource GLSL 450
2320 OpDecorate %_arr_float_uint_2 ArrayStride 16
2321 OpDecorate %u DescriptorSet 0
2322 OpDecorate %u Binding 0
2323 OpMemberDecorate %S 0 Offset 0
2324 OpMemberDecorate %S 1 Offset 8
2325 OpDecorate %S Block
2326 %void = OpTypeVoid
2327 %3 = OpTypeFunction %void
2328 %float = OpTypeFloat 32
2329 %v2float = OpTypeVector %float 2
2330 %uint = OpTypeInt 32 0
2331 %uint_2 = OpConstant %uint 2
2332 %_arr_float_uint_2 = OpTypeArray %float %uint_2
2333 %S = OpTypeStruct %v2float %_arr_float_uint_2
2334 %_ptr_Uniform_S = OpTypePointer Uniform %S
2335 %u = OpVariable %_ptr_Uniform_S Uniform
2336 %main = OpFunction %void None %3
2337 %5 = OpLabel
2338 OpReturn
2339 OpFunctionEnd
2340 )";
2341
2342 CompileSuccessfully(spirv);
2343 spvValidatorOptionsSetUniformBufferStandardLayout(getValidatorOptions(),
2344 true);
2345 EXPECT_EQ(SPV_SUCCESS,
2346 ValidateAndRetrieveValidationState(SPV_ENV_VULKAN_1_0));
2347 EXPECT_THAT(getDiagnosticString(), Eq(""));
2348 }
2349
2350 TEST_F(ValidateDecorations, VulkanBufferBlockOnStorageBufferBad) {
2351 std::string spirv = R"(
2352 OpCapability Shader
2353 OpExtension "SPV_KHR_storage_buffer_storage_class"
2354 OpMemoryModel Logical GLSL450
2355 OpEntryPoint Fragment %1 "main"
2356 OpExecutionMode %1 OriginUpperLeft
2357
2358 OpDecorate %struct BufferBlock
2359
2360 %void = OpTypeVoid
2361 %voidfn = OpTypeFunction %void
2362 %float = OpTypeFloat 32
2363 %struct = OpTypeStruct %float
2364 %ptr = OpTypePointer StorageBuffer %struct
2365 %var = OpVariable %ptr StorageBuffer
2366
2367 %1 = OpFunction %void None %voidfn
2368 %label = OpLabel
2369 OpReturn
2370 OpFunctionEnd
2371 )";
2372
2373 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_1);
2374 EXPECT_EQ(SPV_ERROR_INVALID_ID,
2375 ValidateAndRetrieveValidationState(SPV_ENV_VULKAN_1_1));
2376 EXPECT_THAT(getDiagnosticString(),
2377 HasSubstr("In Vulkan, BufferBlock is disallowed on variables in "
2378 "the StorageBuffer storage class"));
2379 }
2380
2381 TEST_F(ValidateDecorations, PushConstantArrayBaseAlignmentGood) {
2382 // Tests https://github.com/KhronosGroup/SPIRV-Tools/issues/1664
2383 // From GLSL vertex shader:
2384 // #version 450
2385 // layout(push_constant) uniform S { vec2 v; float arr[2]; } u;
2386 // void main() { }
2387
2388 std::string spirv = R"(
2389 OpCapability Shader
2390 OpMemoryModel Logical GLSL450
2391 OpEntryPoint Vertex %main "main"
2392 OpSource GLSL 450
2393 OpDecorate %_arr_float_uint_2 ArrayStride 4
2394 OpMemberDecorate %S 0 Offset 0
2395 OpMemberDecorate %S 1 Offset 8
2396 OpDecorate %S Block
2397 %void = OpTypeVoid
2398 %3 = OpTypeFunction %void
2399 %float = OpTypeFloat 32
2400 %v2float = OpTypeVector %float 2
2401 %uint = OpTypeInt 32 0
2402 %uint_2 = OpConstant %uint 2
2403 %_arr_float_uint_2 = OpTypeArray %float %uint_2
2404 %S = OpTypeStruct %v2float %_arr_float_uint_2
2405 %_ptr_PushConstant_S = OpTypePointer PushConstant %S
2406 %u = OpVariable %_ptr_PushConstant_S PushConstant
2407 %main = OpFunction %void None %3
2408 %5 = OpLabel
2409 OpReturn
2410 OpFunctionEnd
2411 )";
2412
2413 CompileSuccessfully(spirv);
2414 EXPECT_EQ(SPV_SUCCESS, ValidateAndRetrieveValidationState())
2415 << getDiagnosticString();
2416 }
2417
2418 TEST_F(ValidateDecorations, PushConstantArrayBadAlignmentBad) {
2419 // Like the previous test, but with offset 7 instead of 8.
2420 std::string spirv = R"(
2421 OpCapability Shader
2422 OpMemoryModel Logical GLSL450
2423 OpEntryPoint Vertex %main "main"
2424 OpSource GLSL 450
2425 OpDecorate %_arr_float_uint_2 ArrayStride 4
2426 OpMemberDecorate %S 0 Offset 0
2427 OpMemberDecorate %S 1 Offset 7
2428 OpDecorate %S Block
2429 %void = OpTypeVoid
2430 %3 = OpTypeFunction %void
2431 %float = OpTypeFloat 32
2432 %v2float = OpTypeVector %float 2
2433 %uint = OpTypeInt 32 0
2434 %uint_2 = OpConstant %uint 2
2435 %_arr_float_uint_2 = OpTypeArray %float %uint_2
2436 %S = OpTypeStruct %v2float %_arr_float_uint_2
2437 %_ptr_PushConstant_S = OpTypePointer PushConstant %S
2438 %u = OpVariable %_ptr_PushConstant_S PushConstant
2439 %main = OpFunction %void None %3
2440 %5 = OpLabel
2441 OpReturn
2442 OpFunctionEnd
2443 )";
2444
2445 CompileSuccessfully(spirv);
2446 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateAndRetrieveValidationState());
2447 EXPECT_THAT(
2448 getDiagnosticString(),
2449 HasSubstr(
2450 "Structure id 3 decorated as Block for variable in PushConstant "
2451 "storage class must follow standard storage buffer layout rules: "
2452 "member 1 at offset 7 is not aligned to 4"));
2453 }
2454
2455 TEST_F(ValidateDecorations,
2456 PushConstantLayoutPermitsTightVec3ScalarPackingGood) {
2457 // See https://github.com/KhronosGroup/SPIRV-Tools/issues/1666
2458 std::string spirv = R"(
2459 OpCapability Shader
2460 OpMemoryModel Logical GLSL450
2461 OpEntryPoint Vertex %main "main"
2462 OpSource GLSL 450
2463 OpMemberDecorate %S 0 Offset 0
2464 OpMemberDecorate %S 1 Offset 12
2465 OpDecorate %S Block
2466 %void = OpTypeVoid
2467 %3 = OpTypeFunction %void
2468 %float = OpTypeFloat 32
2469 %v3float = OpTypeVector %float 3
2470 %S = OpTypeStruct %v3float %float
2471 %_ptr_PushConstant_S = OpTypePointer PushConstant %S
2472 %B = OpVariable %_ptr_PushConstant_S PushConstant
2473 %main = OpFunction %void None %3
2474 %5 = OpLabel
2475 OpReturn
2476 OpFunctionEnd
2477 )";
2478
2479 CompileSuccessfully(spirv);
2480 EXPECT_EQ(SPV_SUCCESS, ValidateAndRetrieveValidationState())
2481 << getDiagnosticString();
2482 }
2483
2484 TEST_F(ValidateDecorations,
2485 PushConstantLayoutForbidsTightScalarVec3PackingBad) {
2486 // See https://github.com/KhronosGroup/SPIRV-Tools/issues/1666
2487 std::string spirv = R"(
2488 OpCapability Shader
2489 OpMemoryModel Logical GLSL450
2490 OpEntryPoint Vertex %main "main"
2491 OpSource GLSL 450
2492 OpMemberDecorate %S 0 Offset 0
2493 OpMemberDecorate %S 1 Offset 4
2494 OpDecorate %S Block
2495 %void = OpTypeVoid
2496 %3 = OpTypeFunction %void
2497 %float = OpTypeFloat 32
2498 %v3float = OpTypeVector %float 3
2499 %S = OpTypeStruct %float %v3float
2500 %_ptr_Uniform_S = OpTypePointer PushConstant %S
2501 %B = OpVariable %_ptr_Uniform_S PushConstant
2502 %main = OpFunction %void None %3
2503 %5 = OpLabel
2504 OpReturn
2505 OpFunctionEnd
2506 )";
2507
2508 CompileSuccessfully(spirv);
2509 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateAndRetrieveValidationState());
2510 EXPECT_THAT(
2511 getDiagnosticString(),
2512 HasSubstr(
2513 "Structure id 2 decorated as Block for variable in PushConstant "
2514 "storage class must follow standard storage buffer layout "
2515 "rules: member 1 at offset 4 is not aligned to 16"));
2516 }
2517
2518 TEST_F(ValidateDecorations, PushConstantMissingBlockGood) {
2519 std::string spirv = R"(
2520 OpCapability Shader
2521 OpMemoryModel Logical GLSL450
2522 OpEntryPoint Fragment %1 "main"
2523 OpExecutionMode %1 OriginUpperLeft
2524
2525 OpMemberDecorate %struct 0 Offset 0
2526
2527 %void = OpTypeVoid
2528 %voidfn = OpTypeFunction %void
2529 %float = OpTypeFloat 32
2530 %struct = OpTypeStruct %float
2531 %ptr = OpTypePointer PushConstant %struct
2532 %pc = OpVariable %ptr PushConstant
2533
2534 %1 = OpFunction %void None %voidfn
2535 %label = OpLabel
2536 OpReturn
2537 OpFunctionEnd
2538 )";
2539
2540 CompileSuccessfully(spirv);
2541 EXPECT_EQ(SPV_SUCCESS, ValidateAndRetrieveValidationState())
2542 << getDiagnosticString();
2543 }
2544
2545 TEST_F(ValidateDecorations, VulkanPushConstantMissingBlockBad) {
2546 std::string spirv = R"(
2547 OpCapability Shader
2548 OpMemoryModel Logical GLSL450
2549 OpEntryPoint Fragment %1 "main"
2550 OpExecutionMode %1 OriginUpperLeft
2551
2552 OpMemberDecorate %struct 0 Offset 0
2553
2554 %void = OpTypeVoid
2555 %voidfn = OpTypeFunction %void
2556 %float = OpTypeFloat 32
2557 %struct = OpTypeStruct %float
2558 %ptr = OpTypePointer PushConstant %struct
2559 %pc = OpVariable %ptr PushConstant
2560
2561 %1 = OpFunction %void None %voidfn
2562 %label = OpLabel
2563 OpReturn
2564 OpFunctionEnd
2565 )";
2566
2567 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_1);
2568 EXPECT_EQ(SPV_ERROR_INVALID_ID,
2569 ValidateAndRetrieveValidationState(SPV_ENV_VULKAN_1_1));
2570 EXPECT_THAT(getDiagnosticString(),
2571 HasSubstr("PushConstant id '2' is missing Block decoration.\n"
2572 "From Vulkan spec, section 14.5.1:\n"
2573 "Such variables must be identified with a Block "
2574 "decoration"));
2575 }
2576
2577 TEST_F(ValidateDecorations, MultiplePushConstantsSingleEntryPointGood) {
2578 std::string spirv = R"(
2579 OpCapability Shader
2580 OpMemoryModel Logical GLSL450
2581 OpEntryPoint Fragment %1 "main"
2582 OpExecutionMode %1 OriginUpperLeft
2583
2584 OpDecorate %struct Block
2585 OpMemberDecorate %struct 0 Offset 0
2586
2587 %void = OpTypeVoid
2588 %voidfn = OpTypeFunction %void
2589 %float = OpTypeFloat 32
2590 %int = OpTypeInt 32 0
2591 %int_0 = OpConstant %int 0
2592 %struct = OpTypeStruct %float
2593 %ptr = OpTypePointer PushConstant %struct
2594 %ptr_float = OpTypePointer PushConstant %float
2595 %pc1 = OpVariable %ptr PushConstant
2596 %pc2 = OpVariable %ptr PushConstant
2597
2598 %1 = OpFunction %void None %voidfn
2599 %label = OpLabel
2600 %2 = OpAccessChain %ptr_float %pc1 %int_0
2601 %3 = OpLoad %float %2
2602 %4 = OpAccessChain %ptr_float %pc2 %int_0
2603 %5 = OpLoad %float %4
2604 OpReturn
2605 OpFunctionEnd
2606 )";
2607
2608 CompileSuccessfully(spirv);
2609 EXPECT_EQ(SPV_SUCCESS, ValidateAndRetrieveValidationState())
2610 << getDiagnosticString();
2611 }
2612
2613 TEST_F(ValidateDecorations,
2614 VulkanMultiplePushConstantsDifferentEntryPointGood) {
2615 std::string spirv = R"(
2616 OpCapability Shader
2617 OpMemoryModel Logical GLSL450
2618 OpEntryPoint Vertex %1 "func1"
2619 OpEntryPoint Fragment %2 "func2"
2620 OpExecutionMode %2 OriginUpperLeft
2621
2622 OpDecorate %struct Block
2623 OpMemberDecorate %struct 0 Offset 0
2624
2625 %void = OpTypeVoid
2626 %voidfn = OpTypeFunction %void
2627 %float = OpTypeFloat 32
2628 %int = OpTypeInt 32 0
2629 %int_0 = OpConstant %int 0
2630 %struct = OpTypeStruct %float
2631 %ptr = OpTypePointer PushConstant %struct
2632 %ptr_float = OpTypePointer PushConstant %float
2633 %pc1 = OpVariable %ptr PushConstant
2634 %pc2 = OpVariable %ptr PushConstant
2635
2636 %1 = OpFunction %void None %voidfn
2637 %label1 = OpLabel
2638 %3 = OpAccessChain %ptr_float %pc1 %int_0
2639 %4 = OpLoad %float %3
2640 OpReturn
2641 OpFunctionEnd
2642
2643 %2 = OpFunction %void None %voidfn
2644 %label2 = OpLabel
2645 %5 = OpAccessChain %ptr_float %pc2 %int_0
2646 %6 = OpLoad %float %5
2647 OpReturn
2648 OpFunctionEnd
2649 )";
2650
2651 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_1);
2652 EXPECT_EQ(SPV_SUCCESS, ValidateAndRetrieveValidationState(SPV_ENV_VULKAN_1_1))
2653 << getDiagnosticString();
2654 }
2655
2656 TEST_F(ValidateDecorations,
2657 VulkanMultiplePushConstantsUnusedSingleEntryPointGood) {
2658 std::string spirv = R"(
2659 OpCapability Shader
2660 OpMemoryModel Logical GLSL450
2661 OpEntryPoint Fragment %1 "main"
2662 OpExecutionMode %1 OriginUpperLeft
2663
2664 OpDecorate %struct Block
2665 OpMemberDecorate %struct 0 Offset 0
2666
2667 %void = OpTypeVoid
2668 %voidfn = OpTypeFunction %void
2669 %float = OpTypeFloat 32
2670 %int = OpTypeInt 32 0
2671 %int_0 = OpConstant %int 0
2672 %struct = OpTypeStruct %float
2673 %ptr = OpTypePointer PushConstant %struct
2674 %ptr_float = OpTypePointer PushConstant %float
2675 %pc1 = OpVariable %ptr PushConstant
2676 %pc2 = OpVariable %ptr PushConstant
2677
2678 %1 = OpFunction %void None %voidfn
2679 %label = OpLabel
2680 OpReturn
2681 OpFunctionEnd
2682 )";
2683
2684 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_1);
2685 EXPECT_EQ(SPV_SUCCESS, ValidateAndRetrieveValidationState(SPV_ENV_VULKAN_1_1))
2686 << getDiagnosticString();
2687 }
2688
2689 TEST_F(ValidateDecorations, VulkanMultiplePushConstantsSingleEntryPointBad) {
2690 std::string spirv = R"(
2691 OpCapability Shader
2692 OpMemoryModel Logical GLSL450
2693 OpEntryPoint Fragment %1 "main"
2694 OpExecutionMode %1 OriginUpperLeft
2695
2696 OpDecorate %struct Block
2697 OpMemberDecorate %struct 0 Offset 0
2698
2699 %void = OpTypeVoid
2700 %voidfn = OpTypeFunction %void
2701 %float = OpTypeFloat 32
2702 %int = OpTypeInt 32 0
2703 %int_0 = OpConstant %int 0
2704 %struct = OpTypeStruct %float
2705 %ptr = OpTypePointer PushConstant %struct
2706 %ptr_float = OpTypePointer PushConstant %float
2707 %pc1 = OpVariable %ptr PushConstant
2708 %pc2 = OpVariable %ptr PushConstant
2709
2710 %1 = OpFunction %void None %voidfn
2711 %label = OpLabel
2712 %2 = OpAccessChain %ptr_float %pc1 %int_0
2713 %3 = OpLoad %float %2
2714 %4 = OpAccessChain %ptr_float %pc2 %int_0
2715 %5 = OpLoad %float %4
2716 OpReturn
2717 OpFunctionEnd
2718 )";
2719
2720 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_1);
2721 EXPECT_EQ(SPV_ERROR_INVALID_ID,
2722 ValidateAndRetrieveValidationState(SPV_ENV_VULKAN_1_1));
2723 EXPECT_THAT(
2724 getDiagnosticString(),
2725 HasSubstr(
2726 "Entry point id '1' uses more than one PushConstant interface.\n"
2727 "From Vulkan spec, section 14.5.1:\n"
2728 "There must be no more than one push constant block "
2729 "statically used per shader entry point."));
2730 }
2731
2732 TEST_F(ValidateDecorations,
2733 VulkanMultiplePushConstantsDifferentEntryPointSubFunctionGood) {
2734 std::string spirv = R"(
2735 OpCapability Shader
2736 OpMemoryModel Logical GLSL450
2737 OpEntryPoint Vertex %1 "func1"
2738 OpEntryPoint Fragment %2 "func2"
2739 OpExecutionMode %2 OriginUpperLeft
2740
2741 OpDecorate %struct Block
2742 OpMemberDecorate %struct 0 Offset 0
2743
2744 %void = OpTypeVoid
2745 %voidfn = OpTypeFunction %void
2746 %float = OpTypeFloat 32
2747 %int = OpTypeInt 32 0
2748 %int_0 = OpConstant %int 0
2749 %struct = OpTypeStruct %float
2750 %ptr = OpTypePointer PushConstant %struct
2751 %ptr_float = OpTypePointer PushConstant %float
2752 %pc1 = OpVariable %ptr PushConstant
2753 %pc2 = OpVariable %ptr PushConstant
2754
2755 %sub1 = OpFunction %void None %voidfn
2756 %label_sub1 = OpLabel
2757 %3 = OpAccessChain %ptr_float %pc1 %int_0
2758 %4 = OpLoad %float %3
2759 OpReturn
2760 OpFunctionEnd
2761
2762 %sub2 = OpFunction %void None %voidfn
2763 %label_sub2 = OpLabel
2764 %5 = OpAccessChain %ptr_float %pc2 %int_0
2765 %6 = OpLoad %float %5
2766 OpReturn
2767 OpFunctionEnd
2768
2769 %1 = OpFunction %void None %voidfn
2770 %label1 = OpLabel
2771 %call1 = OpFunctionCall %void %sub1
2772 OpReturn
2773 OpFunctionEnd
2774
2775 %2 = OpFunction %void None %voidfn
2776 %label2 = OpLabel
2777 %call2 = OpFunctionCall %void %sub2
2778 OpReturn
2779 OpFunctionEnd
2780 )";
2781
2782 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_1);
2783 EXPECT_EQ(SPV_SUCCESS, ValidateAndRetrieveValidationState(SPV_ENV_VULKAN_1_1))
2784 << getDiagnosticString();
2785 }
2786
2787 TEST_F(ValidateDecorations,
2788 VulkanMultiplePushConstantsSingleEntryPointSubFunctionBad) {
2789 std::string spirv = R"(
2790 OpCapability Shader
2791 OpMemoryModel Logical GLSL450
2792 OpEntryPoint Fragment %1 "main"
2793 OpExecutionMode %1 OriginUpperLeft
2794
2795 OpDecorate %struct Block
2796 OpMemberDecorate %struct 0 Offset 0
2797
2798 %void = OpTypeVoid
2799 %voidfn = OpTypeFunction %void
2800 %float = OpTypeFloat 32
2801 %int = OpTypeInt 32 0
2802 %int_0 = OpConstant %int 0
2803 %struct = OpTypeStruct %float
2804 %ptr = OpTypePointer PushConstant %struct
2805 %ptr_float = OpTypePointer PushConstant %float
2806 %pc1 = OpVariable %ptr PushConstant
2807 %pc2 = OpVariable %ptr PushConstant
2808
2809 %sub1 = OpFunction %void None %voidfn
2810 %label_sub1 = OpLabel
2811 %3 = OpAccessChain %ptr_float %pc1 %int_0
2812 %4 = OpLoad %float %3
2813 OpReturn
2814 OpFunctionEnd
2815
2816 %sub2 = OpFunction %void None %voidfn
2817 %label_sub2 = OpLabel
2818 %5 = OpAccessChain %ptr_float %pc2 %int_0
2819 %6 = OpLoad %float %5
2820 OpReturn
2821 OpFunctionEnd
2822
2823 %1 = OpFunction %void None %voidfn
2824 %label1 = OpLabel
2825 %call1 = OpFunctionCall %void %sub1
2826 %call2 = OpFunctionCall %void %sub2
2827 OpReturn
2828 OpFunctionEnd
2829 )";
2830
2831 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_1);
2832 EXPECT_EQ(SPV_ERROR_INVALID_ID,
2833 ValidateAndRetrieveValidationState(SPV_ENV_VULKAN_1_1));
2834 EXPECT_THAT(
2835 getDiagnosticString(),
2836 HasSubstr(
2837 "Entry point id '1' uses more than one PushConstant interface.\n"
2838 "From Vulkan spec, section 14.5.1:\n"
2839 "There must be no more than one push constant block "
2840 "statically used per shader entry point."));
2841 }
2842
2843 TEST_F(ValidateDecorations, VulkanUniformMissingDescriptorSetBad) {
2844 std::string spirv = R"(
2845 OpCapability Shader
2846 OpMemoryModel Logical GLSL450
2847 OpEntryPoint Fragment %1 "main"
2848 OpExecutionMode %1 OriginUpperLeft
2849
2850 OpDecorate %struct Block
2851 OpMemberDecorate %struct 0 Offset 0
2852 OpDecorate %var Binding 0
2853
2854 %void = OpTypeVoid
2855 %voidfn = OpTypeFunction %void
2856 %float = OpTypeFloat 32
2857 %struct = OpTypeStruct %float
2858 %ptr = OpTypePointer Uniform %struct
2859 %ptr_float = OpTypePointer Uniform %float
2860 %var = OpVariable %ptr Uniform
2861 %int = OpTypeInt 32 0
2862 %int_0 = OpConstant %int 0
2863
2864 %1 = OpFunction %void None %voidfn
2865 %label = OpLabel
2866 %2 = OpAccessChain %ptr_float %var %int_0
2867 %3 = OpLoad %float %2
2868 OpReturn
2869 OpFunctionEnd
2870 )";
2871
2872 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_1);
2873 EXPECT_EQ(SPV_ERROR_INVALID_ID,
2874 ValidateAndRetrieveValidationState(SPV_ENV_VULKAN_1_1));
2875 EXPECT_THAT(getDiagnosticString(),
2876 HasSubstr("Uniform id '3' is missing DescriptorSet decoration.\n"
2877 "From Vulkan spec, section 14.5.2:\n"
2878 "These variables must have DescriptorSet and Binding "
2879 "decorations specified"));
2880 }
2881
2882 TEST_F(ValidateDecorations, VulkanUniformMissingBindingBad) {
2883 std::string spirv = R"(
2884 OpCapability Shader
2885 OpMemoryModel Logical GLSL450
2886 OpEntryPoint Fragment %1 "main"
2887 OpExecutionMode %1 OriginUpperLeft
2888
2889 OpDecorate %struct Block
2890 OpMemberDecorate %struct 0 Offset 0
2891 OpDecorate %var DescriptorSet 0
2892
2893 %void = OpTypeVoid
2894 %voidfn = OpTypeFunction %void
2895 %float = OpTypeFloat 32
2896 %struct = OpTypeStruct %float
2897 %ptr = OpTypePointer Uniform %struct
2898 %ptr_float = OpTypePointer Uniform %float
2899 %var = OpVariable %ptr Uniform
2900 %int = OpTypeInt 32 0
2901 %int_0 = OpConstant %int 0
2902
2903 %1 = OpFunction %void None %voidfn
2904 %label = OpLabel
2905 %2 = OpAccessChain %ptr_float %var %int_0
2906 %3 = OpLoad %float %2
2907 OpReturn
2908 OpFunctionEnd
2909 )";
2910
2911 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_1);
2912 EXPECT_EQ(SPV_ERROR_INVALID_ID,
2913 ValidateAndRetrieveValidationState(SPV_ENV_VULKAN_1_1));
2914 EXPECT_THAT(getDiagnosticString(),
2915 HasSubstr("Uniform id '3' is missing Binding decoration.\n"
2916 "From Vulkan spec, section 14.5.2:\n"
2917 "These variables must have DescriptorSet and Binding "
2918 "decorations specified"));
2919 }
2920
2921 TEST_F(ValidateDecorations, VulkanUniformConstantMissingDescriptorSetBad) {
2922 std::string spirv = R"(
2923 OpCapability Shader
2924 OpMemoryModel Logical GLSL450
2925 OpEntryPoint Fragment %1 "main"
2926 OpExecutionMode %1 OriginUpperLeft
2927
2928 OpDecorate %var Binding 0
2929
2930 %void = OpTypeVoid
2931 %voidfn = OpTypeFunction %void
2932 %sampler = OpTypeSampler
2933 %ptr = OpTypePointer UniformConstant %sampler
2934 %var = OpVariable %ptr UniformConstant
2935
2936 %1 = OpFunction %void None %voidfn
2937 %label = OpLabel
2938 %2 = OpLoad %sampler %var
2939 OpReturn
2940 OpFunctionEnd
2941 )";
2942
2943 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_1);
2944 EXPECT_EQ(SPV_ERROR_INVALID_ID,
2945 ValidateAndRetrieveValidationState(SPV_ENV_VULKAN_1_1));
2946 EXPECT_THAT(
2947 getDiagnosticString(),
2948 HasSubstr("UniformConstant id '2' is missing DescriptorSet decoration.\n"
2949 "From Vulkan spec, section 14.5.2:\n"
2950 "These variables must have DescriptorSet and Binding "
2951 "decorations specified"));
2952 }
2953
2954 TEST_F(ValidateDecorations, VulkanUniformConstantMissingBindingBad) {
2955 std::string spirv = R"(
2956 OpCapability Shader
2957 OpMemoryModel Logical GLSL450
2958 OpEntryPoint Fragment %1 "main"
2959 OpExecutionMode %1 OriginUpperLeft
2960
2961 OpDecorate %var DescriptorSet 0
2962
2963 %void = OpTypeVoid
2964 %voidfn = OpTypeFunction %void
2965 %sampler = OpTypeSampler
2966 %ptr = OpTypePointer UniformConstant %sampler
2967 %var = OpVariable %ptr UniformConstant
2968
2969 %1 = OpFunction %void None %voidfn
2970 %label = OpLabel
2971 %2 = OpLoad %sampler %var
2972 OpReturn
2973 OpFunctionEnd
2974 )";
2975
2976 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_1);
2977 EXPECT_EQ(SPV_ERROR_INVALID_ID,
2978 ValidateAndRetrieveValidationState(SPV_ENV_VULKAN_1_1));
2979 EXPECT_THAT(
2980 getDiagnosticString(),
2981 HasSubstr("UniformConstant id '2' is missing Binding decoration.\n"
2982 "From Vulkan spec, section 14.5.2:\n"
2983 "These variables must have DescriptorSet and Binding "
2984 "decorations specified"));
2985 }
2986
2987 TEST_F(ValidateDecorations, VulkanStorageBufferMissingDescriptorSetBad) {
2988 std::string spirv = R"(
2989 OpCapability Shader
2990 OpExtension "SPV_KHR_storage_buffer_storage_class"
2991 OpMemoryModel Logical GLSL450
2992 OpEntryPoint Fragment %1 "main"
2993 OpExecutionMode %1 OriginUpperLeft
2994
2995 OpDecorate %struct Block
2996 OpDecorate %var Binding 0
2997
2998 %void = OpTypeVoid
2999 %voidfn = OpTypeFunction %void
3000 %float = OpTypeFloat 32
3001 %struct = OpTypeStruct %float
3002 %ptr = OpTypePointer StorageBuffer %struct
3003 %var = OpVariable %ptr StorageBuffer
3004 %ptr_float = OpTypePointer StorageBuffer %float
3005 %int = OpTypeInt 32 0
3006 %int_0 = OpConstant %int 0
3007
3008 %1 = OpFunction %void None %voidfn
3009 %label = OpLabel
3010 %2 = OpAccessChain %ptr_float %var %int_0
3011 %3 = OpLoad %float %2
3012 OpReturn
3013 OpFunctionEnd
3014 )";
3015
3016 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_1);
3017 EXPECT_EQ(SPV_ERROR_INVALID_ID,
3018 ValidateAndRetrieveValidationState(SPV_ENV_VULKAN_1_1));
3019 EXPECT_THAT(
3020 getDiagnosticString(),
3021 HasSubstr("StorageBuffer id '3' is missing DescriptorSet decoration.\n"
3022 "From Vulkan spec, section 14.5.2:\n"
3023 "These variables must have DescriptorSet and Binding "
3024 "decorations specified"));
3025 }
3026
3027 TEST_F(ValidateDecorations, VulkanStorageBufferMissingBindingBad) {
3028 std::string spirv = R"(
3029 OpCapability Shader
3030 OpExtension "SPV_KHR_storage_buffer_storage_class"
3031 OpMemoryModel Logical GLSL450
3032 OpEntryPoint Fragment %1 "main"
3033 OpExecutionMode %1 OriginUpperLeft
3034
3035 OpDecorate %struct Block
3036 OpDecorate %var DescriptorSet 0
3037
3038 %void = OpTypeVoid
3039 %voidfn = OpTypeFunction %void
3040 %float = OpTypeFloat 32
3041 %struct = OpTypeStruct %float
3042 %ptr = OpTypePointer StorageBuffer %struct
3043 %var = OpVariable %ptr StorageBuffer
3044 %ptr_float = OpTypePointer StorageBuffer %float
3045 %int = OpTypeInt 32 0
3046 %int_0 = OpConstant %int 0
3047
3048 %1 = OpFunction %void None %voidfn
3049 %label = OpLabel
3050 %2 = OpAccessChain %ptr_float %var %int_0
3051 %3 = OpLoad %float %2
3052 OpReturn
3053 OpFunctionEnd
3054 )";
3055
3056 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_1);
3057 EXPECT_EQ(SPV_ERROR_INVALID_ID,
3058 ValidateAndRetrieveValidationState(SPV_ENV_VULKAN_1_1));
3059 EXPECT_THAT(getDiagnosticString(),
3060 HasSubstr("StorageBuffer id '3' is missing Binding decoration.\n"
3061 "From Vulkan spec, section 14.5.2:\n"
3062 "These variables must have DescriptorSet and Binding "
3063 "decorations specified"));
3064 }
3065
3066 TEST_F(ValidateDecorations,
3067 VulkanStorageBufferMissingDescriptorSetSubFunctionBad) {
3068 std::string spirv = R"(
3069 OpCapability Shader
3070 OpExtension "SPV_KHR_storage_buffer_storage_class"
3071 OpMemoryModel Logical GLSL450
3072 OpEntryPoint Fragment %1 "main"
3073 OpExecutionMode %1 OriginUpperLeft
3074
3075 OpDecorate %struct Block
3076 OpDecorate %var Binding 0
3077
3078 %void = OpTypeVoid
3079 %voidfn = OpTypeFunction %void
3080 %float = OpTypeFloat 32
3081 %struct = OpTypeStruct %float
3082 %ptr = OpTypePointer StorageBuffer %struct
3083 %var = OpVariable %ptr StorageBuffer
3084 %ptr_float = OpTypePointer StorageBuffer %float
3085 %int = OpTypeInt 32 0
3086 %int_0 = OpConstant %int 0
3087
3088 %1 = OpFunction %void None %voidfn
3089 %label = OpLabel
3090 %call = OpFunctionCall %void %2
3091 OpReturn
3092 OpFunctionEnd
3093 %2 = OpFunction %void None %voidfn
3094 %label2 = OpLabel
3095 %3 = OpAccessChain %ptr_float %var %int_0
3096 %4 = OpLoad %float %3
3097 OpReturn
3098 OpFunctionEnd
3099 )";
3100
3101 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_1);
3102 EXPECT_EQ(SPV_ERROR_INVALID_ID,
3103 ValidateAndRetrieveValidationState(SPV_ENV_VULKAN_1_1));
3104 EXPECT_THAT(
3105 getDiagnosticString(),
3106 HasSubstr("StorageBuffer id '3' is missing DescriptorSet decoration.\n"
3107 "From Vulkan spec, section 14.5.2:\n"
3108 "These variables must have DescriptorSet and Binding "
3109 "decorations specified"));
3110 }
3111
3112 TEST_F(ValidateDecorations,
3113 VulkanStorageBufferMissingDescriptorAndBindingUnusedGood) {
3114 std::string spirv = R"(
3115 OpCapability Shader
3116 OpExtension "SPV_KHR_storage_buffer_storage_class"
3117 OpMemoryModel Logical GLSL450
3118 OpEntryPoint Fragment %1 "main"
3119 OpExecutionMode %1 OriginUpperLeft
3120 OpDecorate %struct Block
3121 OpMemberDecorate %struct 0 Offset 0
3122
3123 %void = OpTypeVoid
3124 %voidfn = OpTypeFunction %void
3125 %float = OpTypeFloat 32
3126 %struct = OpTypeStruct %float
3127 %ptr = OpTypePointer StorageBuffer %struct
3128 %var = OpVariable %ptr StorageBuffer
3129
3130 %1 = OpFunction %void None %voidfn
3131 %label = OpLabel
3132 OpReturn
3133 OpFunctionEnd
3134 )";
3135
3136 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_1);
3137 EXPECT_EQ(SPV_SUCCESS,
3138 ValidateAndRetrieveValidationState(SPV_ENV_VULKAN_1_1));
3139 }
3140
3141 TEST_F(ValidateDecorations, UniformMissingDescriptorSetGood) {
3142 std::string spirv = R"(
3143 OpCapability Shader
3144 OpMemoryModel Logical GLSL450
3145 OpEntryPoint Fragment %1 "main"
3146 OpExecutionMode %1 OriginUpperLeft
3147
3148 OpDecorate %struct Block
3149 OpMemberDecorate %struct 0 Offset 0
3150 OpDecorate %var Binding 0
3151
3152 %void = OpTypeVoid
3153 %voidfn = OpTypeFunction %void
3154 %float = OpTypeFloat 32
3155 %struct = OpTypeStruct %float
3156 %ptr = OpTypePointer Uniform %struct
3157 %var = OpVariable %ptr Uniform
3158
3159 %1 = OpFunction %void None %voidfn
3160 %label = OpLabel
3161 OpReturn
3162 OpFunctionEnd
3163 )";
3164
3165 CompileSuccessfully(spirv);
3166 EXPECT_EQ(SPV_SUCCESS, ValidateAndRetrieveValidationState())
3167 << getDiagnosticString();
3168 }
3169
3170 TEST_F(ValidateDecorations, UniformMissingBindingGood) {
3171 std::string spirv = R"(
3172 OpCapability Shader
3173 OpMemoryModel Logical GLSL450
3174 OpEntryPoint Fragment %1 "main"
3175 OpExecutionMode %1 OriginUpperLeft
3176
3177 OpDecorate %struct Block
3178 OpMemberDecorate %struct 0 Offset 0
3179 OpDecorate %var DescriptorSet 0
3180
3181 %void = OpTypeVoid
3182 %voidfn = OpTypeFunction %void
3183 %float = OpTypeFloat 32
3184 %struct = OpTypeStruct %float
3185 %ptr = OpTypePointer Uniform %struct
3186 %var = OpVariable %ptr Uniform
3187
3188 %1 = OpFunction %void None %voidfn
3189 %label = OpLabel
3190 OpReturn
3191 OpFunctionEnd
3192 )";
3193
3194 CompileSuccessfully(spirv);
3195 EXPECT_EQ(SPV_SUCCESS, ValidateAndRetrieveValidationState())
3196 << getDiagnosticString();
3197 }
3198
3199 TEST_F(ValidateDecorations, UniformConstantMissingDescriptorSetGood) {
3200 std::string spirv = R"(
3201 OpCapability Shader
3202 OpMemoryModel Logical GLSL450
3203 OpEntryPoint Fragment %1 "main"
3204 OpExecutionMode %1 OriginUpperLeft
3205
3206 OpDecorate %var Binding 0
3207
3208 %void = OpTypeVoid
3209 %voidfn = OpTypeFunction %void
3210 %sampler = OpTypeSampler
3211 %ptr = OpTypePointer UniformConstant %sampler
3212 %var = OpVariable %ptr UniformConstant
3213
3214 %1 = OpFunction %void None %voidfn
3215 %label = OpLabel
3216 OpReturn
3217 OpFunctionEnd
3218 )";
3219
3220 CompileSuccessfully(spirv);
3221 EXPECT_EQ(SPV_SUCCESS, ValidateAndRetrieveValidationState())
3222 << getDiagnosticString();
3223 }
3224
3225 TEST_F(ValidateDecorations, UniformConstantMissingBindingGood) {
3226 std::string spirv = R"(
3227 OpCapability Shader
3228 OpMemoryModel Logical GLSL450
3229 OpEntryPoint Fragment %1 "main"
3230 OpExecutionMode %1 OriginUpperLeft
3231
3232 OpDecorate %var DescriptorSet 0
3233
3234 %void = OpTypeVoid
3235 %voidfn = OpTypeFunction %void
3236 %sampler = OpTypeSampler
3237 %ptr = OpTypePointer UniformConstant %sampler
3238 %var = OpVariable %ptr UniformConstant
3239
3240 %1 = OpFunction %void None %voidfn
3241 %label = OpLabel
3242 OpReturn
3243 OpFunctionEnd
3244 )";
3245
3246 CompileSuccessfully(spirv);
3247 EXPECT_EQ(SPV_SUCCESS, ValidateAndRetrieveValidationState())
3248 << getDiagnosticString();
3249 }
3250
3251 TEST_F(ValidateDecorations, StorageBufferMissingDescriptorSetGood) {
3252 std::string spirv = R"(
3253 OpCapability Shader
3254 OpExtension "SPV_KHR_storage_buffer_storage_class"
3255 OpMemoryModel Logical GLSL450
3256 OpEntryPoint Fragment %1 "main"
3257 OpExecutionMode %1 OriginUpperLeft
3258
3259 OpDecorate %struct BufferBlock
3260 OpDecorate %var Binding 0
3261
3262 %void = OpTypeVoid
3263 %voidfn = OpTypeFunction %void
3264 %float = OpTypeFloat 32
3265 %struct = OpTypeStruct %float
3266 %ptr = OpTypePointer StorageBuffer %struct
3267 %var = OpVariable %ptr StorageBuffer
3268
3269 %1 = OpFunction %void None %voidfn
3270 %label = OpLabel
3271 OpReturn
3272 OpFunctionEnd
3273 )";
3274
3275 CompileSuccessfully(spirv);
3276 EXPECT_EQ(SPV_SUCCESS, ValidateAndRetrieveValidationState())
3277 << getDiagnosticString();
3278 }
3279
3280 TEST_F(ValidateDecorations, StorageBufferMissingBindingGood) {
3281 std::string spirv = R"(
3282 OpCapability Shader
3283 OpExtension "SPV_KHR_storage_buffer_storage_class"
3284 OpMemoryModel Logical GLSL450
3285 OpEntryPoint Fragment %1 "main"
3286 OpExecutionMode %1 OriginUpperLeft
3287
3288 OpDecorate %struct BufferBlock
3289 OpDecorate %var DescriptorSet 0
3290
3291 %void = OpTypeVoid
3292 %voidfn = OpTypeFunction %void
3293 %float = OpTypeFloat 32
3294 %struct = OpTypeStruct %float
3295 %ptr = OpTypePointer StorageBuffer %struct
3296 %var = OpVariable %ptr StorageBuffer
3297
3298 %1 = OpFunction %void None %voidfn
3299 %label = OpLabel
3300 OpReturn
3301 OpFunctionEnd
3302 )";
3303
3304 CompileSuccessfully(spirv);
3305 EXPECT_EQ(SPV_SUCCESS, ValidateAndRetrieveValidationState())
3306 << getDiagnosticString();
3307 }
3308
3309 TEST_F(ValidateDecorations, StorageBufferStorageClassArrayBaseAlignmentGood) {
3310 // Spot check buffer rules when using StorageBuffer storage class with Block
3311 // decoration.
3312 std::string spirv = R"(
3313 OpCapability Shader
3314 OpExtension "SPV_KHR_storage_buffer_storage_class"
3315 OpMemoryModel Logical GLSL450
3316 OpEntryPoint Vertex %main "main"
3317 OpSource GLSL 450
3318 OpDecorate %_arr_float_uint_2 ArrayStride 4
3319 OpMemberDecorate %S 0 Offset 0
3320 OpMemberDecorate %S 1 Offset 8
3321 OpDecorate %S Block
3322 OpDecorate %u DescriptorSet 0
3323 OpDecorate %u Binding 0
3324 %void = OpTypeVoid
3325 %3 = OpTypeFunction %void
3326 %float = OpTypeFloat 32
3327 %v2float = OpTypeVector %float 2
3328 %uint = OpTypeInt 32 0
3329 %uint_2 = OpConstant %uint 2
3330 %_arr_float_uint_2 = OpTypeArray %float %uint_2
3331 %S = OpTypeStruct %v2float %_arr_float_uint_2
3332 %_ptr_Uniform_S = OpTypePointer StorageBuffer %S
3333 %u = OpVariable %_ptr_Uniform_S StorageBuffer
3334 %main = OpFunction %void None %3
3335 %5 = OpLabel
3336 OpReturn
3337 OpFunctionEnd
3338 )";
3339
3340 CompileSuccessfully(spirv);
3341 EXPECT_EQ(SPV_SUCCESS, ValidateAndRetrieveValidationState())
3342 << getDiagnosticString();
3343 }
3344
3345 TEST_F(ValidateDecorations, StorageBufferStorageClassArrayBadAlignmentBad) {
3346 // Like the previous test, but with offset 7.
3347 std::string spirv = R"(
3348 OpCapability Shader
3349 OpExtension "SPV_KHR_storage_buffer_storage_class"
3350 OpMemoryModel Logical GLSL450
3351 OpEntryPoint Vertex %main "main"
3352 OpSource GLSL 450
3353 OpDecorate %_arr_float_uint_2 ArrayStride 4
3354 OpMemberDecorate %S 0 Offset 0
3355 OpMemberDecorate %S 1 Offset 7
3356 OpDecorate %S Block
3357 OpDecorate %u DescriptorSet 0
3358 OpDecorate %u Binding 0
3359 %void = OpTypeVoid
3360 %3 = OpTypeFunction %void
3361 %float = OpTypeFloat 32
3362 %v2float = OpTypeVector %float 2
3363 %uint = OpTypeInt 32 0
3364 %uint_2 = OpConstant %uint 2
3365 %_arr_float_uint_2 = OpTypeArray %float %uint_2
3366 %S = OpTypeStruct %v2float %_arr_float_uint_2
3367 %_ptr_Uniform_S = OpTypePointer StorageBuffer %S
3368 %u = OpVariable %_ptr_Uniform_S StorageBuffer
3369 %main = OpFunction %void None %3
3370 %5 = OpLabel
3371 OpReturn
3372 OpFunctionEnd
3373 )";
3374
3375 CompileSuccessfully(spirv);
3376 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateAndRetrieveValidationState());
3377 EXPECT_THAT(
3378 getDiagnosticString(),
3379 HasSubstr(
3380 "Structure id 3 decorated as Block for variable in StorageBuffer "
3381 "storage class must follow standard storage buffer layout rules: "
3382 "member 1 at offset 7 is not aligned to 4"));
3383 }
3384
3385 TEST_F(ValidateDecorations, BufferBlockStandardStorageBufferLayout) {
3386 std::string spirv = R"(
3387 OpCapability Shader
3388 %1 = OpExtInstImport "GLSL.std.450"
3389 OpMemoryModel Logical GLSL450
3390 OpEntryPoint GLCompute %main "main"
3391 OpExecutionMode %main LocalSize 1 1 1
3392 OpSource GLSL 430
3393 OpMemberDecorate %F 0 Offset 0
3394 OpMemberDecorate %F 1 Offset 8
3395 OpDecorate %_arr_float_uint_2 ArrayStride 4
3396 OpDecorate %_arr_mat3v3float_uint_2 ArrayStride 48
3397 OpMemberDecorate %O 0 Offset 0
3398 OpMemberDecorate %O 1 Offset 16
3399 OpMemberDecorate %O 2 Offset 24
3400 OpMemberDecorate %O 3 Offset 32
3401 OpMemberDecorate %O 4 ColMajor
3402 OpMemberDecorate %O 4 Offset 48
3403 OpMemberDecorate %O 4 MatrixStride 16
3404 OpDecorate %_arr_O_uint_2 ArrayStride 144
3405 OpMemberDecorate %Output 0 Offset 0
3406 OpMemberDecorate %Output 1 Offset 8
3407 OpMemberDecorate %Output 2 Offset 16
3408 OpMemberDecorate %Output 3 Offset 32
3409 OpMemberDecorate %Output 4 Offset 48
3410 OpMemberDecorate %Output 5 Offset 52
3411 OpMemberDecorate %Output 6 ColMajor
3412 OpMemberDecorate %Output 6 Offset 64
3413 OpMemberDecorate %Output 6 MatrixStride 16
3414 OpMemberDecorate %Output 7 Offset 96
3415 OpDecorate %Output BufferBlock
3416 %void = OpTypeVoid
3417 %3 = OpTypeFunction %void
3418 %float = OpTypeFloat 32
3419 %v2float = OpTypeVector %float 2
3420 %v3float = OpTypeVector %float 3
3421 %int = OpTypeInt 32 1
3422 %uint = OpTypeInt 32 0
3423 %v2uint = OpTypeVector %uint 2
3424 %F = OpTypeStruct %int %v2uint
3425 %uint_2 = OpConstant %uint 2
3426 %_arr_float_uint_2 = OpTypeArray %float %uint_2
3427 %mat2v3float = OpTypeMatrix %v3float 2
3428 %v3uint = OpTypeVector %uint 3
3429 %mat3v3float = OpTypeMatrix %v3float 3
3430 %_arr_mat3v3float_uint_2 = OpTypeArray %mat3v3float %uint_2
3431 %O = OpTypeStruct %v3uint %v2float %_arr_float_uint_2 %v2float %_arr_mat3v3float_uint_2
3432 %_arr_O_uint_2 = OpTypeArray %O %uint_2
3433 %Output = OpTypeStruct %float %v2float %v3float %F %float %_arr_float_uint_2 %mat2v3float %_arr_O_uint_2
3434 %_ptr_Uniform_Output = OpTypePointer Uniform %Output
3435 %dataOutput = OpVariable %_ptr_Uniform_Output Uniform
3436 %main = OpFunction %void None %3
3437 %5 = OpLabel
3438 OpReturn
3439 OpFunctionEnd
3440 )";
3441
3442 CompileSuccessfully(spirv);
3443 EXPECT_EQ(SPV_SUCCESS, ValidateAndRetrieveValidationState());
3444 }
3445
3446 TEST_F(ValidateDecorations,
3447 StorageBufferLayoutPermitsTightVec3ScalarPackingGood) {
3448 // See https://github.com/KhronosGroup/SPIRV-Tools/issues/1666
3449 std::string spirv = R"(
3450 OpCapability Shader
3451 OpExtension "SPV_KHR_storage_buffer_storage_class"
3452 OpMemoryModel Logical GLSL450
3453 OpEntryPoint Vertex %main "main"
3454 OpSource GLSL 450
3455 OpMemberDecorate %S 0 Offset 0
3456 OpMemberDecorate %S 1 Offset 12
3457 OpDecorate %S Block
3458 OpDecorate %B DescriptorSet 0
3459 OpDecorate %B Binding 0
3460 %void = OpTypeVoid
3461 %3 = OpTypeFunction %void
3462 %float = OpTypeFloat 32
3463 %v3float = OpTypeVector %float 3
3464 %S = OpTypeStruct %v3float %float
3465 %_ptr_StorageBuffer_S = OpTypePointer StorageBuffer %S
3466 %B = OpVariable %_ptr_StorageBuffer_S StorageBuffer
3467 %main = OpFunction %void None %3
3468 %5 = OpLabel
3469 OpReturn
3470 OpFunctionEnd
3471 )";
3472
3473 CompileSuccessfully(spirv);
3474 EXPECT_EQ(SPV_SUCCESS, ValidateAndRetrieveValidationState())
3475 << getDiagnosticString();
3476 }
3477
3478 TEST_F(ValidateDecorations,
3479 StorageBufferLayoutForbidsTightScalarVec3PackingBad) {
3480 // See https://github.com/KhronosGroup/SPIRV-Tools/issues/1666
3481 std::string spirv = R"(
3482 OpCapability Shader
3483 OpExtension "SPV_KHR_storage_buffer_storage_class"
3484 OpMemoryModel Logical GLSL450
3485 OpEntryPoint Vertex %main "main"
3486 OpSource GLSL 450
3487 OpMemberDecorate %S 0 Offset 0
3488 OpMemberDecorate %S 1 Offset 4
3489 OpDecorate %S Block
3490 OpDecorate %B DescriptorSet 0
3491 OpDecorate %B Binding 0
3492 %void = OpTypeVoid
3493 %3 = OpTypeFunction %void
3494 %float = OpTypeFloat 32
3495 %v3float = OpTypeVector %float 3
3496 %S = OpTypeStruct %float %v3float
3497 %_ptr_StorageBuffer_S = OpTypePointer StorageBuffer %S
3498 %B = OpVariable %_ptr_StorageBuffer_S StorageBuffer
3499 %main = OpFunction %void None %3
3500 %5 = OpLabel
3501 OpReturn
3502 OpFunctionEnd
3503 )";
3504
3505 CompileSuccessfully(spirv);
3506 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateAndRetrieveValidationState());
3507 EXPECT_THAT(
3508 getDiagnosticString(),
3509 HasSubstr(
3510 "Structure id 2 decorated as Block for variable in StorageBuffer "
3511 "storage class must follow standard storage buffer layout "
3512 "rules: member 1 at offset 4 is not aligned to 16"));
3513 }
3514
3515 TEST_F(ValidateDecorations,
3516 BlockStandardUniformBufferLayoutIncorrectOffset0Bad) {
3517 std::string spirv = R"(
3518 OpCapability Shader
3519 %1 = OpExtInstImport "GLSL.std.450"
3520 OpMemoryModel Logical GLSL450
3521 OpEntryPoint GLCompute %main "main"
3522 OpExecutionMode %main LocalSize 1 1 1
3523 OpSource GLSL 430
3524 OpMemberDecorate %F 0 Offset 0
3525 OpMemberDecorate %F 1 Offset 8
3526 OpDecorate %_arr_float_uint_2 ArrayStride 16
3527 OpDecorate %_arr_mat3v3float_uint_2 ArrayStride 48
3528 OpMemberDecorate %O 0 Offset 0
3529 OpMemberDecorate %O 1 Offset 16
3530 OpMemberDecorate %O 2 Offset 24
3531 OpMemberDecorate %O 3 Offset 33
3532 OpMemberDecorate %O 4 ColMajor
3533 OpMemberDecorate %O 4 Offset 80
3534 OpMemberDecorate %O 4 MatrixStride 16
3535 OpDecorate %_arr_O_uint_2 ArrayStride 176
3536 OpMemberDecorate %Output 0 Offset 0
3537 OpMemberDecorate %Output 1 Offset 8
3538 OpMemberDecorate %Output 2 Offset 16
3539 OpMemberDecorate %Output 3 Offset 32
3540 OpMemberDecorate %Output 4 Offset 48
3541 OpMemberDecorate %Output 5 Offset 64
3542 OpMemberDecorate %Output 6 ColMajor
3543 OpMemberDecorate %Output 6 Offset 96
3544 OpMemberDecorate %Output 6 MatrixStride 16
3545 OpMemberDecorate %Output 7 Offset 128
3546 OpDecorate %Output Block
3547 %void = OpTypeVoid
3548 %3 = OpTypeFunction %void
3549 %float = OpTypeFloat 32
3550 %v2float = OpTypeVector %float 2
3551 %v3float = OpTypeVector %float 3
3552 %int = OpTypeInt 32 1
3553 %uint = OpTypeInt 32 0
3554 %v2uint = OpTypeVector %uint 2
3555 %F = OpTypeStruct %int %v2uint
3556 %uint_2 = OpConstant %uint 2
3557 %_arr_float_uint_2 = OpTypeArray %float %uint_2
3558 %mat2v3float = OpTypeMatrix %v3float 2
3559 %v3uint = OpTypeVector %uint 3
3560 %mat3v3float = OpTypeMatrix %v3float 3
3561 %_arr_mat3v3float_uint_2 = OpTypeArray %mat3v3float %uint_2
3562 %O = OpTypeStruct %v3uint %v2float %_arr_float_uint_2 %v2float %_arr_mat3v3float_uint_2
3563 %_arr_O_uint_2 = OpTypeArray %O %uint_2
3564 %Output = OpTypeStruct %float %v2float %v3float %F %float %_arr_float_uint_2 %mat2v3float %_arr_O_uint_2
3565 %_ptr_Uniform_Output = OpTypePointer Uniform %Output
3566 %dataOutput = OpVariable %_ptr_Uniform_Output Uniform
3567 %main = OpFunction %void None %3
3568 %5 = OpLabel
3569 OpReturn
3570 OpFunctionEnd
3571 )";
3572
3573 CompileSuccessfully(spirv);
3574 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateAndRetrieveValidationState());
3575 EXPECT_THAT(
3576 getDiagnosticString(),
3577 HasSubstr("Structure id 6 decorated as Block for variable in Uniform "
3578 "storage class must follow standard uniform buffer layout "
3579 "rules: member 2 at offset 152 is not aligned to 16"));
3580 }
3581
3582 TEST_F(ValidateDecorations,
3583 BlockStandardUniformBufferLayoutIncorrectOffset1Bad) {
3584 std::string spirv = R"(
3585 OpCapability Shader
3586 %1 = OpExtInstImport "GLSL.std.450"
3587 OpMemoryModel Logical GLSL450
3588 OpEntryPoint GLCompute %main "main"
3589 OpExecutionMode %main LocalSize 1 1 1
3590 OpSource GLSL 430
3591 OpMemberDecorate %F 0 Offset 0
3592 OpMemberDecorate %F 1 Offset 8
3593 OpDecorate %_arr_float_uint_2 ArrayStride 16
3594 OpDecorate %_arr_mat3v3float_uint_2 ArrayStride 48
3595 OpMemberDecorate %O 0 Offset 0
3596 OpMemberDecorate %O 1 Offset 16
3597 OpMemberDecorate %O 2 Offset 32
3598 OpMemberDecorate %O 3 Offset 64
3599 OpMemberDecorate %O 4 ColMajor
3600 OpMemberDecorate %O 4 Offset 80
3601 OpMemberDecorate %O 4 MatrixStride 16
3602 OpDecorate %_arr_O_uint_2 ArrayStride 176
3603 OpMemberDecorate %Output 0 Offset 0
3604 OpMemberDecorate %Output 1 Offset 8
3605 OpMemberDecorate %Output 2 Offset 16
3606 OpMemberDecorate %Output 3 Offset 32
3607 OpMemberDecorate %Output 4 Offset 48
3608 OpMemberDecorate %Output 5 Offset 71
3609 OpMemberDecorate %Output 6 ColMajor
3610 OpMemberDecorate %Output 6 Offset 96
3611 OpMemberDecorate %Output 6 MatrixStride 16
3612 OpMemberDecorate %Output 7 Offset 128
3613 OpDecorate %Output Block
3614 %void = OpTypeVoid
3615 %3 = OpTypeFunction %void
3616 %float = OpTypeFloat 32
3617 %v2float = OpTypeVector %float 2
3618 %v3float = OpTypeVector %float 3
3619 %int = OpTypeInt 32 1
3620 %uint = OpTypeInt 32 0
3621 %v2uint = OpTypeVector %uint 2
3622 %F = OpTypeStruct %int %v2uint
3623 %uint_2 = OpConstant %uint 2
3624 %_arr_float_uint_2 = OpTypeArray %float %uint_2
3625 %mat2v3float = OpTypeMatrix %v3float 2
3626 %v3uint = OpTypeVector %uint 3
3627 %mat3v3float = OpTypeMatrix %v3float 3
3628 %_arr_mat3v3float_uint_2 = OpTypeArray %mat3v3float %uint_2
3629 %O = OpTypeStruct %v3uint %v2float %_arr_float_uint_2 %v2float %_arr_mat3v3float_uint_2
3630 %_arr_O_uint_2 = OpTypeArray %O %uint_2
3631 %Output = OpTypeStruct %float %v2float %v3float %F %float %_arr_float_uint_2 %mat2v3float %_arr_O_uint_2
3632 %_ptr_Uniform_Output = OpTypePointer Uniform %Output
3633 %dataOutput = OpVariable %_ptr_Uniform_Output Uniform
3634 %main = OpFunction %void None %3
3635 %5 = OpLabel
3636 OpReturn
3637 OpFunctionEnd
3638 )";
3639
3640 CompileSuccessfully(spirv);
3641 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateAndRetrieveValidationState());
3642 EXPECT_THAT(
3643 getDiagnosticString(),
3644 HasSubstr("Structure id 8 decorated as Block for variable in Uniform "
3645 "storage class must follow standard uniform buffer layout "
3646 "rules: member 5 at offset 71 is not aligned to 16"));
3647 }
3648
3649 TEST_F(ValidateDecorations, BlockUniformBufferLayoutIncorrectArrayStrideBad) {
3650 std::string spirv = R"(
3651 OpCapability Shader
3652 %1 = OpExtInstImport "GLSL.std.450"
3653 OpMemoryModel Logical GLSL450
3654 OpEntryPoint GLCompute %main "main"
3655 OpExecutionMode %main LocalSize 1 1 1
3656 OpSource GLSL 430
3657 OpMemberDecorate %F 0 Offset 0
3658 OpMemberDecorate %F 1 Offset 8
3659 OpDecorate %_arr_float_uint_2 ArrayStride 16
3660 OpDecorate %_arr_mat3v3float_uint_2 ArrayStride 49
3661 OpMemberDecorate %O 0 Offset 0
3662 OpMemberDecorate %O 1 Offset 16
3663 OpMemberDecorate %O 2 Offset 32
3664 OpMemberDecorate %O 3 Offset 64
3665 OpMemberDecorate %O 4 ColMajor
3666 OpMemberDecorate %O 4 Offset 80
3667 OpMemberDecorate %O 4 MatrixStride 16
3668 OpDecorate %_arr_O_uint_2 ArrayStride 176
3669 OpMemberDecorate %Output 0 Offset 0
3670 OpMemberDecorate %Output 1 Offset 8
3671 OpMemberDecorate %Output 2 Offset 16
3672 OpMemberDecorate %Output 3 Offset 32
3673 OpMemberDecorate %Output 4 Offset 48
3674 OpMemberDecorate %Output 5 Offset 64
3675 OpMemberDecorate %Output 6 ColMajor
3676 OpMemberDecorate %Output 6 Offset 96
3677 OpMemberDecorate %Output 6 MatrixStride 16
3678 OpMemberDecorate %Output 7 Offset 128
3679 OpDecorate %Output Block
3680 %void = OpTypeVoid
3681 %3 = OpTypeFunction %void
3682 %float = OpTypeFloat 32
3683 %v2float = OpTypeVector %float 2
3684 %v3float = OpTypeVector %float 3
3685 %int = OpTypeInt 32 1
3686 %uint = OpTypeInt 32 0
3687 %v2uint = OpTypeVector %uint 2
3688 %F = OpTypeStruct %int %v2uint
3689 %uint_2 = OpConstant %uint 2
3690 %_arr_float_uint_2 = OpTypeArray %float %uint_2
3691 %mat2v3float = OpTypeMatrix %v3float 2
3692 %v3uint = OpTypeVector %uint 3
3693 %mat3v3float = OpTypeMatrix %v3float 3
3694 %_arr_mat3v3float_uint_2 = OpTypeArray %mat3v3float %uint_2
3695 %O = OpTypeStruct %v3uint %v2float %_arr_float_uint_2 %v2float %_arr_mat3v3float_uint_2
3696 %_arr_O_uint_2 = OpTypeArray %O %uint_2
3697 %Output = OpTypeStruct %float %v2float %v3float %F %float %_arr_float_uint_2 %mat2v3float %_arr_O_uint_2
3698 %_ptr_Uniform_Output = OpTypePointer Uniform %Output
3699 %dataOutput = OpVariable %_ptr_Uniform_Output Uniform
3700 %main = OpFunction %void None %3
3701 %5 = OpLabel
3702 OpReturn
3703 OpFunctionEnd
3704 )";
3705
3706 CompileSuccessfully(spirv);
3707 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateAndRetrieveValidationState());
3708 EXPECT_THAT(
3709 getDiagnosticString(),
3710 HasSubstr(
3711 "Structure id 6 decorated as Block for variable in Uniform storage "
3712 "class must follow standard uniform buffer layout rules: member 4 "
3713 "contains "
3714 "an array with stride 49 not satisfying alignment to 16"));
3715 }
3716
3717 TEST_F(ValidateDecorations,
3718 BufferBlockStandardStorageBufferLayoutImproperStraddleBad) {
3719 std::string spirv = R"(
3720 OpCapability Shader
3721 %1 = OpExtInstImport "GLSL.std.450"
3722 OpMemoryModel Logical GLSL450
3723 OpEntryPoint GLCompute %main "main"
3724 OpExecutionMode %main LocalSize 1 1 1
3725 OpSource GLSL 430
3726 OpMemberDecorate %Output 0 Offset 0
3727 OpMemberDecorate %Output 1 Offset 8
3728 OpDecorate %Output BufferBlock
3729 %void = OpTypeVoid
3730 %3 = OpTypeFunction %void
3731 %float = OpTypeFloat 32
3732 %v3float = OpTypeVector %float 3
3733 %Output = OpTypeStruct %float %v3float
3734 %_ptr_Uniform_Output = OpTypePointer Uniform %Output
3735 %dataOutput = OpVariable %_ptr_Uniform_Output Uniform
3736 %main = OpFunction %void None %3
3737 %5 = OpLabel
3738 OpReturn
3739 OpFunctionEnd
3740 )";
3741
3742 CompileSuccessfully(spirv);
3743 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateAndRetrieveValidationState());
3744 EXPECT_THAT(
3745 getDiagnosticString(),
3746 HasSubstr("Structure id 3 decorated as BufferBlock for variable in "
3747 "Uniform storage class must follow standard storage buffer "
3748 "layout rules: member 1 at offset 8 is not aligned to 16"));
3749 }
3750
3751 TEST_F(ValidateDecorations,
3752 BlockUniformBufferLayoutOffsetInsideArrayPaddingBad) {
3753 // In this case the 2nd member fits entirely within the padding.
3754 std::string spirv = R"(
3755 OpCapability Shader
3756 %1 = OpExtInstImport "GLSL.std.450"
3757 OpMemoryModel Logical GLSL450
3758 OpEntryPoint GLCompute %main "main"
3759 OpExecutionMode %main LocalSize 1 1 1
3760 OpSource GLSL 430
3761 OpDecorate %_arr_float_uint_2 ArrayStride 16
3762 OpMemberDecorate %Output 0 Offset 0
3763 OpMemberDecorate %Output 1 Offset 20
3764 OpDecorate %Output Block
3765 %void = OpTypeVoid
3766 %3 = OpTypeFunction %void
3767 %float = OpTypeFloat 32
3768 %uint = OpTypeInt 32 0
3769 %v2uint = OpTypeVector %uint 2
3770 %uint_2 = OpConstant %uint 2
3771 %_arr_float_uint_2 = OpTypeArray %float %uint_2
3772 %Output = OpTypeStruct %_arr_float_uint_2 %float
3773 %_ptr_Uniform_Output = OpTypePointer Uniform %Output
3774 %dataOutput = OpVariable %_ptr_Uniform_Output Uniform
3775 %main = OpFunction %void None %3
3776 %5 = OpLabel
3777 OpReturn
3778 OpFunctionEnd
3779 )";
3780
3781 CompileSuccessfully(spirv);
3782 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateAndRetrieveValidationState());
3783 EXPECT_THAT(
3784 getDiagnosticString(),
3785 HasSubstr(
3786 "Structure id 4 decorated as Block for variable in Uniform storage "
3787 "class must follow standard uniform buffer layout rules: member 1 at "
3788 "offset 20 overlaps previous member ending at offset 31"));
3789 }
3790
3791 TEST_F(ValidateDecorations,
3792 BlockUniformBufferLayoutOffsetInsideStructPaddingBad) {
3793 // In this case the 2nd member fits entirely within the padding.
3794 std::string spirv = R"(
3795 OpCapability Shader
3796 OpMemoryModel Logical GLSL450
3797 OpEntryPoint GLCompute %1 "main"
3798 OpMemberDecorate %_struct_6 0 Offset 0
3799 OpMemberDecorate %_struct_2 0 Offset 0
3800 OpMemberDecorate %_struct_2 1 Offset 4
3801 OpDecorate %_struct_2 Block
3802 %void = OpTypeVoid
3803 %4 = OpTypeFunction %void
3804 %float = OpTypeFloat 32
3805 %_struct_6 = OpTypeStruct %float
3806 %_struct_2 = OpTypeStruct %_struct_6 %float
3807 %_ptr_Uniform__struct_2 = OpTypePointer Uniform %_struct_2
3808 %8 = OpVariable %_ptr_Uniform__struct_2 Uniform
3809 %1 = OpFunction %void None %4
3810 %9 = OpLabel
3811 OpReturn
3812 OpFunctionEnd
3813 )";
3814
3815 CompileSuccessfully(spirv);
3816 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateAndRetrieveValidationState());
3817 EXPECT_THAT(
3818 getDiagnosticString(),
3819 HasSubstr(
3820 "Structure id 3 decorated as Block for variable in Uniform storage "
3821 "class must follow standard uniform buffer layout rules: member 1 at "
3822 "offset 4 overlaps previous member ending at offset 15"));
3823 }
3824
3825 TEST_F(ValidateDecorations, BlockLayoutOffsetOutOfOrderGoodUniversal1_0) {
3826 std::string spirv = R"(
3827 OpCapability Shader
3828 %1 = OpExtInstImport "GLSL.std.450"
3829 OpMemoryModel Logical GLSL450
3830 OpEntryPoint GLCompute %main "main"
3831 OpExecutionMode %main LocalSize 1 1 1
3832 OpMemberDecorate %Outer 0 Offset 4
3833 OpMemberDecorate %Outer 1 Offset 0
3834 OpDecorate %Outer Block
3835 OpDecorate %O DescriptorSet 0
3836 OpDecorate %O Binding 0
3837 %void = OpTypeVoid
3838 %3 = OpTypeFunction %void
3839 %uint = OpTypeInt 32 0
3840 %Outer = OpTypeStruct %uint %uint
3841 %_ptr_Uniform_Outer = OpTypePointer Uniform %Outer
3842 %O = OpVariable %_ptr_Uniform_Outer Uniform
3843 %main = OpFunction %void None %3
3844 %5 = OpLabel
3845 OpReturn
3846 OpFunctionEnd
3847 )";
3848
3849 CompileSuccessfully(spirv);
3850 EXPECT_EQ(SPV_SUCCESS,
3851 ValidateAndRetrieveValidationState(SPV_ENV_UNIVERSAL_1_0));
3852 }
3853
3854 TEST_F(ValidateDecorations, BlockLayoutOffsetOutOfOrderGoodOpenGL4_5) {
3855 std::string spirv = R"(
3856 OpCapability Shader
3857 %1 = OpExtInstImport "GLSL.std.450"
3858 OpMemoryModel Logical GLSL450
3859 OpEntryPoint GLCompute %main "main"
3860 OpExecutionMode %main LocalSize 1 1 1
3861 OpMemberDecorate %Outer 0 Offset 4
3862 OpMemberDecorate %Outer 1 Offset 0
3863 OpDecorate %Outer Block
3864 OpDecorate %O DescriptorSet 0
3865 OpDecorate %O Binding 0
3866 %void = OpTypeVoid
3867 %3 = OpTypeFunction %void
3868 %uint = OpTypeInt 32 0
3869 %Outer = OpTypeStruct %uint %uint
3870 %_ptr_Uniform_Outer = OpTypePointer Uniform %Outer
3871 %O = OpVariable %_ptr_Uniform_Outer Uniform
3872 %main = OpFunction %void None %3
3873 %5 = OpLabel
3874 OpReturn
3875 OpFunctionEnd
3876 )";
3877
3878 CompileSuccessfully(spirv);
3879 EXPECT_EQ(SPV_SUCCESS,
3880 ValidateAndRetrieveValidationState(SPV_ENV_OPENGL_4_5));
3881 }
3882
3883 TEST_F(ValidateDecorations, BlockLayoutOffsetOutOfOrderGoodVulkan1_1) {
3884 std::string spirv = R"(
3885 OpCapability Shader
3886 %1 = OpExtInstImport "GLSL.std.450"
3887 OpMemoryModel Logical GLSL450
3888 OpEntryPoint GLCompute %main "main"
3889 OpExecutionMode %main LocalSize 1 1 1
3890 OpMemberDecorate %Outer 0 Offset 4
3891 OpMemberDecorate %Outer 1 Offset 0
3892 OpDecorate %Outer Block
3893 OpDecorate %O DescriptorSet 0
3894 OpDecorate %O Binding 0
3895 %void = OpTypeVoid
3896 %3 = OpTypeFunction %void
3897 %uint = OpTypeInt 32 0
3898 %Outer = OpTypeStruct %uint %uint
3899 %_ptr_Uniform_Outer = OpTypePointer Uniform %Outer
3900 %O = OpVariable %_ptr_Uniform_Outer Uniform
3901 %main = OpFunction %void None %3
3902 %5 = OpLabel
3903 OpReturn
3904 OpFunctionEnd
3905 )";
3906
3907 CompileSuccessfully(spirv);
3908 EXPECT_EQ(SPV_SUCCESS, ValidateAndRetrieveValidationState(SPV_ENV_VULKAN_1_1))
3909 << getDiagnosticString();
3910 EXPECT_THAT(getDiagnosticString(), Eq(""));
3911 }
3912
3913 TEST_F(ValidateDecorations, BlockLayoutOffsetOverlapBad) {
3914 std::string spirv = R"(
3915 OpCapability Shader
3916 %1 = OpExtInstImport "GLSL.std.450"
3917 OpMemoryModel Logical GLSL450
3918 OpEntryPoint GLCompute %main "main"
3919 OpExecutionMode %main LocalSize 1 1 1
3920 OpMemberDecorate %Outer 0 Offset 0
3921 OpMemberDecorate %Outer 1 Offset 16
3922 OpMemberDecorate %Inner 0 Offset 0
3923 OpMemberDecorate %Inner 1 Offset 16
3924 OpDecorate %Outer Block
3925 OpDecorate %O DescriptorSet 0
3926 OpDecorate %O Binding 0
3927 %void = OpTypeVoid
3928 %3 = OpTypeFunction %void
3929 %uint = OpTypeInt 32 0
3930 %Inner = OpTypeStruct %uint %uint
3931 %Outer = OpTypeStruct %Inner %uint
3932 %_ptr_Uniform_Outer = OpTypePointer Uniform %Outer
3933 %O = OpVariable %_ptr_Uniform_Outer Uniform
3934 %main = OpFunction %void None %3
3935 %5 = OpLabel
3936 OpReturn
3937 OpFunctionEnd
3938 )";
3939
3940 CompileSuccessfully(spirv);
3941 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateAndRetrieveValidationState());
3942 EXPECT_THAT(
3943 getDiagnosticString(),
3944 HasSubstr(
3945 "Structure id 3 decorated as Block for variable in Uniform storage "
3946 "class must follow standard uniform buffer layout rules: member 1 at "
3947 "offset 16 overlaps previous member ending at offset 31"));
3948 }
3949
3950 TEST_F(ValidateDecorations, BufferBlockEmptyStruct) {
3951 std::string spirv = R"(
3952 OpCapability Shader
3953 %1 = OpExtInstImport "GLSL.std.450"
3954 OpMemoryModel Logical GLSL450
3955 OpEntryPoint GLCompute %main "main"
3956 OpExecutionMode %main LocalSize 1 1 1
3957 OpSource GLSL 430
3958 OpMemberDecorate %Output 0 Offset 0
3959 OpDecorate %Output BufferBlock
3960 %void = OpTypeVoid
3961 %3 = OpTypeFunction %void
3962 %S = OpTypeStruct
3963 %Output = OpTypeStruct %S
3964 %_ptr_Uniform_Output = OpTypePointer Uniform %Output
3965 %dataOutput = OpVariable %_ptr_Uniform_Output Uniform
3966 %main = OpFunction %void None %3
3967 %5 = OpLabel
3968 OpReturn
3969 OpFunctionEnd
3970 )";
3971
3972 CompileSuccessfully(spirv);
3973 EXPECT_EQ(SPV_SUCCESS, ValidateAndRetrieveValidationState());
3974 }
3975
3976 TEST_F(ValidateDecorations, RowMajorMatrixTightPackingGood) {
3977 // Row major matrix rule:
3978 // A row-major matrix of C columns has a base alignment equal to
3979 // the base alignment of a vector of C matrix components.
3980 // Note: The "matrix component" is the scalar element type.
3981
3982 // The matrix has 3 columns and 2 rows (C=3, R=2).
3983 // So the base alignment of b is the same as a vector of 3 floats, which is 16
3984 // bytes. The matrix consists of two of these, and therefore occupies 2 x 16
3985 // bytes, or 32 bytes.
3986 //
3987 // So the offsets can be:
3988 // a -> 0
3989 // b -> 16
3990 // c -> 48
3991 // d -> 60 ; d fits at bytes 12-15 after offset of c. Tight (vec3;float)
3992 // packing
3993
3994 std::string spirv = R"(
3995 OpCapability Shader
3996 OpMemoryModel Logical GLSL450
3997 OpEntryPoint Vertex %1 "main"
3998 OpSource GLSL 450
3999 OpMemberDecorate %_struct_2 0 Offset 0
4000 OpMemberDecorate %_struct_2 1 RowMajor
4001 OpMemberDecorate %_struct_2 1 Offset 16
4002 OpMemberDecorate %_struct_2 1 MatrixStride 16
4003 OpMemberDecorate %_struct_2 2 Offset 48
4004 OpMemberDecorate %_struct_2 3 Offset 60
4005 OpDecorate %_struct_2 Block
4006 OpDecorate %3 DescriptorSet 0
4007 OpDecorate %3 Binding 0
4008 %void = OpTypeVoid
4009 %5 = OpTypeFunction %void
4010 %float = OpTypeFloat 32
4011 %v4float = OpTypeVector %float 4
4012 %v2float = OpTypeVector %float 2
4013 %mat3v2float = OpTypeMatrix %v2float 3
4014 %v3float = OpTypeVector %float 3
4015 %_struct_2 = OpTypeStruct %v4float %mat3v2float %v3float %float
4016 %_ptr_Uniform__struct_2 = OpTypePointer Uniform %_struct_2
4017 %3 = OpVariable %_ptr_Uniform__struct_2 Uniform
4018 %1 = OpFunction %void None %5
4019 %12 = OpLabel
4020 OpReturn
4021 OpFunctionEnd
4022 )";
4023
4024 CompileSuccessfully(spirv);
4025 EXPECT_EQ(SPV_SUCCESS, ValidateAndRetrieveValidationState())
4026 << getDiagnosticString();
4027 }
4028
4029 TEST_F(ValidateDecorations, ArrayArrayRowMajorMatrixTightPackingGood) {
4030 // Like the previous case, but we have an array of arrays of matrices.
4031 // The RowMajor decoration goes on the struct member (surprisingly).
4032
4033 std::string spirv = R"(
4034 OpCapability Shader
4035 OpMemoryModel Logical GLSL450
4036 OpEntryPoint Vertex %1 "main"
4037 OpSource GLSL 450
4038 OpMemberDecorate %_struct_2 0 Offset 0
4039 OpMemberDecorate %_struct_2 1 RowMajor
4040 OpMemberDecorate %_struct_2 1 Offset 16
4041 OpMemberDecorate %_struct_2 1 MatrixStride 16
4042 OpMemberDecorate %_struct_2 2 Offset 80
4043 OpMemberDecorate %_struct_2 3 Offset 92
4044 OpDecorate %arr_mat ArrayStride 32
4045 OpDecorate %arr_arr_mat ArrayStride 32
4046 OpDecorate %_struct_2 Block
4047 OpDecorate %3 DescriptorSet 0
4048 OpDecorate %3 Binding 0
4049 %void = OpTypeVoid
4050 %5 = OpTypeFunction %void
4051 %float = OpTypeFloat 32
4052 %v4float = OpTypeVector %float 4
4053 %v2float = OpTypeVector %float 2
4054 %mat3v2float = OpTypeMatrix %v2float 3
4055 %uint = OpTypeInt 32 0
4056 %uint_1 = OpConstant %uint 1
4057 %uint_2 = OpConstant %uint 2
4058 %arr_mat = OpTypeArray %mat3v2float %uint_1
4059 %arr_arr_mat = OpTypeArray %arr_mat %uint_2
4060 %v3float = OpTypeVector %float 3
4061 %_struct_2 = OpTypeStruct %v4float %arr_arr_mat %v3float %float
4062 %_ptr_Uniform__struct_2 = OpTypePointer Uniform %_struct_2
4063 %3 = OpVariable %_ptr_Uniform__struct_2 Uniform
4064 %1 = OpFunction %void None %5
4065 %12 = OpLabel
4066 OpReturn
4067 OpFunctionEnd
4068 )";
4069
4070 CompileSuccessfully(spirv);
4071 EXPECT_EQ(SPV_SUCCESS, ValidateAndRetrieveValidationState())
4072 << getDiagnosticString();
4073 }
4074
4075 TEST_F(ValidateDecorations, ArrayArrayRowMajorMatrixNextMemberOverlapsBad) {
4076 // Like the previous case, but the offset of member 2 overlaps the matrix.
4077 std::string spirv = R"(
4078 OpCapability Shader
4079 OpMemoryModel Logical GLSL450
4080 OpEntryPoint Vertex %1 "main"
4081 OpSource GLSL 450
4082 OpMemberDecorate %_struct_2 0 Offset 0
4083 OpMemberDecorate %_struct_2 1 RowMajor
4084 OpMemberDecorate %_struct_2 1 Offset 16
4085 OpMemberDecorate %_struct_2 1 MatrixStride 16
4086 OpMemberDecorate %_struct_2 2 Offset 64
4087 OpMemberDecorate %_struct_2 3 Offset 92
4088 OpDecorate %arr_mat ArrayStride 32
4089 OpDecorate %arr_arr_mat ArrayStride 32
4090 OpDecorate %_struct_2 Block
4091 OpDecorate %3 DescriptorSet 0
4092 OpDecorate %3 Binding 0
4093 %void = OpTypeVoid
4094 %5 = OpTypeFunction %void
4095 %float = OpTypeFloat 32
4096 %v4float = OpTypeVector %float 4
4097 %v2float = OpTypeVector %float 2
4098 %mat3v2float = OpTypeMatrix %v2float 3
4099 %uint = OpTypeInt 32 0
4100 %uint_1 = OpConstant %uint 1
4101 %uint_2 = OpConstant %uint 2
4102 %arr_mat = OpTypeArray %mat3v2float %uint_1
4103 %arr_arr_mat = OpTypeArray %arr_mat %uint_2
4104 %v3float = OpTypeVector %float 3
4105 %_struct_2 = OpTypeStruct %v4float %arr_arr_mat %v3float %float
4106 %_ptr_Uniform__struct_2 = OpTypePointer Uniform %_struct_2
4107 %3 = OpVariable %_ptr_Uniform__struct_2 Uniform
4108 %1 = OpFunction %void None %5
4109 %12 = OpLabel
4110 OpReturn
4111 OpFunctionEnd
4112 )";
4113
4114 CompileSuccessfully(spirv);
4115 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateAndRetrieveValidationState());
4116 EXPECT_THAT(
4117 getDiagnosticString(),
4118 HasSubstr(
4119 "Structure id 2 decorated as Block for variable in Uniform storage "
4120 "class must follow standard uniform buffer layout rules: member 2 at "
4121 "offset 64 overlaps previous member ending at offset 79"));
4122 }
4123
4124 TEST_F(ValidateDecorations, StorageBufferArraySizeCalculationPackGood) {
4125 // Original GLSL
4126
4127 // #version 450
4128 // layout (set=0,binding=0) buffer S {
4129 // uvec3 arr[2][2]; // first 3 elements are 16 bytes, last is 12
4130 // uint i; // Can have offset 60 = 3x16 + 12
4131 // } B;
4132 // void main() {}
4133
4134 std::string spirv = R"(
4135 OpCapability Shader
4136 OpMemoryModel Logical GLSL450
4137 OpEntryPoint Vertex %1 "main"
4138 OpDecorate %_arr_v3uint_uint_2 ArrayStride 16
4139 OpDecorate %_arr__arr_v3uint_uint_2_uint_2 ArrayStride 32
4140 OpMemberDecorate %_struct_4 0 Offset 0
4141 OpMemberDecorate %_struct_4 1 Offset 60
4142 OpDecorate %_struct_4 BufferBlock
4143 OpDecorate %5 DescriptorSet 0
4144 OpDecorate %5 Binding 0
4145 %void = OpTypeVoid
4146 %7 = OpTypeFunction %void
4147 %uint = OpTypeInt 32 0
4148 %v3uint = OpTypeVector %uint 3
4149 %uint_2 = OpConstant %uint 2
4150 %_arr_v3uint_uint_2 = OpTypeArray %v3uint %uint_2
4151 %_arr__arr_v3uint_uint_2_uint_2 = OpTypeArray %_arr_v3uint_uint_2 %uint_2
4152 %_struct_4 = OpTypeStruct %_arr__arr_v3uint_uint_2_uint_2 %uint
4153 %_ptr_Uniform__struct_4 = OpTypePointer Uniform %_struct_4
4154 %5 = OpVariable %_ptr_Uniform__struct_4 Uniform
4155 %1 = OpFunction %void None %7
4156 %12 = OpLabel
4157 OpReturn
4158 OpFunctionEnd
4159 )";
4160
4161 CompileSuccessfully(spirv);
4162 EXPECT_EQ(SPV_SUCCESS, ValidateAndRetrieveValidationState());
4163 }
4164
4165 TEST_F(ValidateDecorations, StorageBufferArraySizeCalculationPackBad) {
4166 // Like previous but, the offset of the second member is too small.
4167
4168 std::string spirv = R"(
4169 OpCapability Shader
4170 OpMemoryModel Logical GLSL450
4171 OpEntryPoint Vertex %1 "main"
4172 OpDecorate %_arr_v3uint_uint_2 ArrayStride 16
4173 OpDecorate %_arr__arr_v3uint_uint_2_uint_2 ArrayStride 32
4174 OpMemberDecorate %_struct_4 0 Offset 0
4175 OpMemberDecorate %_struct_4 1 Offset 56
4176 OpDecorate %_struct_4 BufferBlock
4177 OpDecorate %5 DescriptorSet 0
4178 OpDecorate %5 Binding 0
4179 %void = OpTypeVoid
4180 %7 = OpTypeFunction %void
4181 %uint = OpTypeInt 32 0
4182 %v3uint = OpTypeVector %uint 3
4183 %uint_2 = OpConstant %uint 2
4184 %_arr_v3uint_uint_2 = OpTypeArray %v3uint %uint_2
4185 %_arr__arr_v3uint_uint_2_uint_2 = OpTypeArray %_arr_v3uint_uint_2 %uint_2
4186 %_struct_4 = OpTypeStruct %_arr__arr_v3uint_uint_2_uint_2 %uint
4187 %_ptr_Uniform__struct_4 = OpTypePointer Uniform %_struct_4
4188 %5 = OpVariable %_ptr_Uniform__struct_4 Uniform
4189 %1 = OpFunction %void None %7
4190 %12 = OpLabel
4191 OpReturn
4192 OpFunctionEnd
4193 )";
4194
4195 CompileSuccessfully(spirv);
4196 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateAndRetrieveValidationState());
4197 EXPECT_THAT(getDiagnosticString(),
4198 HasSubstr("Structure id 4 decorated as BufferBlock for variable "
4199 "in Uniform storage class must follow standard storage "
4200 "buffer layout rules: member 1 at offset 56 overlaps "
4201 "previous member ending at offset 59"));
4202 }
4203
4204 TEST_F(ValidateDecorations, UniformBufferArraySizeCalculationPackGood) {
4205 // Like the corresponding buffer block case, but the array padding must
4206 // count for the last element as well, and so the offset of the second
4207 // member must be at least 64.
4208 std::string spirv = R"(
4209 OpCapability Shader
4210 OpMemoryModel Logical GLSL450
4211 OpEntryPoint Vertex %1 "main"
4212 OpDecorate %_arr_v3uint_uint_2 ArrayStride 16
4213 OpDecorate %_arr__arr_v3uint_uint_2_uint_2 ArrayStride 32
4214 OpMemberDecorate %_struct_4 0 Offset 0
4215 OpMemberDecorate %_struct_4 1 Offset 64
4216 OpDecorate %_struct_4 Block
4217 OpDecorate %5 DescriptorSet 0
4218 OpDecorate %5 Binding 0
4219 %void = OpTypeVoid
4220 %7 = OpTypeFunction %void
4221 %uint = OpTypeInt 32 0
4222 %v3uint = OpTypeVector %uint 3
4223 %uint_2 = OpConstant %uint 2
4224 %_arr_v3uint_uint_2 = OpTypeArray %v3uint %uint_2
4225 %_arr__arr_v3uint_uint_2_uint_2 = OpTypeArray %_arr_v3uint_uint_2 %uint_2
4226 %_struct_4 = OpTypeStruct %_arr__arr_v3uint_uint_2_uint_2 %uint
4227 %_ptr_Uniform__struct_4 = OpTypePointer Uniform %_struct_4
4228 %5 = OpVariable %_ptr_Uniform__struct_4 Uniform
4229 %1 = OpFunction %void None %7
4230 %12 = OpLabel
4231 OpReturn
4232 OpFunctionEnd
4233 )";
4234
4235 CompileSuccessfully(spirv);
4236 EXPECT_EQ(SPV_SUCCESS, ValidateAndRetrieveValidationState());
4237 }
4238
4239 TEST_F(ValidateDecorations, UniformBufferArraySizeCalculationPackBad) {
4240 // Like previous but, the offset of the second member is too small.
4241
4242 std::string spirv = R"(
4243 OpCapability Shader
4244 OpMemoryModel Logical GLSL450
4245 OpEntryPoint Vertex %1 "main"
4246 OpDecorate %_arr_v3uint_uint_2 ArrayStride 16
4247 OpDecorate %_arr__arr_v3uint_uint_2_uint_2 ArrayStride 32
4248 OpMemberDecorate %_struct_4 0 Offset 0
4249 OpMemberDecorate %_struct_4 1 Offset 60
4250 OpDecorate %_struct_4 Block
4251 OpDecorate %5 DescriptorSet 0
4252 OpDecorate %5 Binding 0
4253 %void = OpTypeVoid
4254 %7 = OpTypeFunction %void
4255 %uint = OpTypeInt 32 0
4256 %v3uint = OpTypeVector %uint 3
4257 %uint_2 = OpConstant %uint 2
4258 %_arr_v3uint_uint_2 = OpTypeArray %v3uint %uint_2
4259 %_arr__arr_v3uint_uint_2_uint_2 = OpTypeArray %_arr_v3uint_uint_2 %uint_2
4260 %_struct_4 = OpTypeStruct %_arr__arr_v3uint_uint_2_uint_2 %uint
4261 %_ptr_Uniform__struct_4 = OpTypePointer Uniform %_struct_4
4262 %5 = OpVariable %_ptr_Uniform__struct_4 Uniform
4263 %1 = OpFunction %void None %7
4264 %12 = OpLabel
4265 OpReturn
4266 OpFunctionEnd
4267 )";
4268
4269 CompileSuccessfully(spirv);
4270 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateAndRetrieveValidationState());
4271 EXPECT_THAT(
4272 getDiagnosticString(),
4273 HasSubstr(
4274 "Structure id 4 decorated as Block for variable in Uniform storage "
4275 "class must follow standard uniform buffer layout rules: member 1 at "
4276 "offset 60 overlaps previous member ending at offset 63"));
4277 }
4278
4279 TEST_F(ValidateDecorations, LayoutNotCheckedWhenSkipBlockLayout) {
4280 // Checks that block layout is not verified in skipping block layout mode.
4281 // Even for obviously wrong layout.
4282 std::string spirv = R"(
4283 OpCapability Shader
4284 OpMemoryModel Logical GLSL450
4285 OpEntryPoint Vertex %main "main"
4286 OpSource GLSL 450
4287 OpMemberDecorate %S 0 Offset 3 ; wrong alignment
4288 OpMemberDecorate %S 1 Offset 3 ; same offset as before!
4289 OpDecorate %S Block
4290 OpDecorate %B DescriptorSet 0
4291 OpDecorate %B Binding 0
4292 %void = OpTypeVoid
4293 %3 = OpTypeFunction %void
4294 %float = OpTypeFloat 32
4295 %v3float = OpTypeVector %float 3
4296 %S = OpTypeStruct %float %v3float
4297 %_ptr_Uniform_S = OpTypePointer Uniform %S
4298 %B = OpVariable %_ptr_Uniform_S Uniform
4299 %main = OpFunction %void None %3
4300 %5 = OpLabel
4301 OpReturn
4302 OpFunctionEnd
4303 )";
4304
4305 CompileSuccessfully(spirv);
4306 spvValidatorOptionsSetSkipBlockLayout(getValidatorOptions(), true);
4307 EXPECT_EQ(SPV_SUCCESS,
4308 ValidateAndRetrieveValidationState(SPV_ENV_VULKAN_1_0));
4309 EXPECT_THAT(getDiagnosticString(), Eq(""));
4310 }
4311
4312 TEST_F(ValidateDecorations, EntryPointVariableWrongStorageClass) {
4313 const std::string spirv = R"(
4314 OpCapability Shader
4315 OpMemoryModel Logical GLSL450
4316 OpEntryPoint Fragment %1 "func" %var
4317 OpExecutionMode %1 OriginUpperLeft
4318 %void = OpTypeVoid
4319 %int = OpTypeInt 32 0
4320 %ptr_int_Workgroup = OpTypePointer Workgroup %int
4321 %var = OpVariable %ptr_int_Workgroup Workgroup
4322 %func_ty = OpTypeFunction %void
4323 %1 = OpFunction %void None %func_ty
4324 %2 = OpLabel
4325 OpReturn
4326 OpFunctionEnd
4327 )";
4328
4329 CompileSuccessfully(spirv);
4330 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
4331 EXPECT_THAT(getDiagnosticString(),
4332 HasSubstr("OpEntryPoint interfaces must be OpVariables with "
4333 "Storage Class of Input(1) or Output(3). Found Storage "
4334 "Class 4 for Entry Point id 1."));
4335 }
4336
4337 TEST_F(ValidateDecorations, VulkanMemoryModelNonCoherent) {
4338 const std::string spirv = R"(
4339 OpCapability Shader
4340 OpCapability VulkanMemoryModelKHR
4341 OpCapability Linkage
4342 OpExtension "SPV_KHR_vulkan_memory_model"
4343 OpExtension "SPV_KHR_storage_buffer_storage_class"
4344 OpMemoryModel Logical VulkanKHR
4345 OpDecorate %1 Coherent
4346 %2 = OpTypeInt 32 0
4347 %3 = OpTypePointer StorageBuffer %2
4348 %1 = OpVariable %3 StorageBuffer
4349 )";
4350
4351 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
4352 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
4353 EXPECT_THAT(getDiagnosticString(),
4354 HasSubstr("Coherent decoration targeting 1[%1] is "
4355 "banned when using the Vulkan memory model."));
4356 }
4357
4358 TEST_F(ValidateDecorations, VulkanMemoryModelNoCoherentMember) {
4359 const std::string spirv = R"(
4360 OpCapability Shader
4361 OpCapability VulkanMemoryModelKHR
4362 OpCapability Linkage
4363 OpExtension "SPV_KHR_vulkan_memory_model"
4364 OpMemoryModel Logical VulkanKHR
4365 OpMemberDecorate %1 0 Coherent
4366 %2 = OpTypeInt 32 0
4367 %1 = OpTypeStruct %2 %2
4368 )";
4369
4370 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
4371 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
4372 EXPECT_THAT(
4373 getDiagnosticString(),
4374 HasSubstr("Coherent decoration targeting 1[%_struct_1] (member index 0) "
4375 "is banned when using the Vulkan memory model."));
4376 }
4377
4378 TEST_F(ValidateDecorations, VulkanMemoryModelNoVolatile) {
4379 const std::string spirv = R"(
4380 OpCapability Shader
4381 OpCapability VulkanMemoryModelKHR
4382 OpCapability Linkage
4383 OpExtension "SPV_KHR_vulkan_memory_model"
4384 OpExtension "SPV_KHR_storage_buffer_storage_class"
4385 OpMemoryModel Logical VulkanKHR
4386 OpDecorate %1 Volatile
4387 %2 = OpTypeInt 32 0
4388 %3 = OpTypePointer StorageBuffer %2
4389 %1 = OpVariable %3 StorageBuffer
4390 )";
4391
4392 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
4393 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
4394 EXPECT_THAT(getDiagnosticString(),
4395 HasSubstr("Volatile decoration targeting 1[%1] is banned when "
4396 "using the Vulkan memory model."));
4397 }
4398
4399 TEST_F(ValidateDecorations, VulkanMemoryModelNoVolatileMember) {
4400 const std::string spirv = R"(
4401 OpCapability Shader
4402 OpCapability VulkanMemoryModelKHR
4403 OpCapability Linkage
4404 OpExtension "SPV_KHR_vulkan_memory_model"
4405 OpMemoryModel Logical VulkanKHR
4406 OpMemberDecorate %1 1 Volatile
4407 %2 = OpTypeInt 32 0
4408 %1 = OpTypeStruct %2 %2
4409 )";
4410
4411 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
4412 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
4413 EXPECT_THAT(getDiagnosticString(),
4414 HasSubstr("Volatile decoration targeting 1[%_struct_1] (member "
4415 "index 1) is banned when using the Vulkan memory "
4416 "model."));
4417 }
4418
4419 TEST_F(ValidateDecorations, FPRoundingModeGood) {
4420 std::string spirv = R"(
4421 OpCapability Shader
4422 OpCapability Linkage
4423 OpCapability StorageBuffer16BitAccess
4424 OpExtension "SPV_KHR_storage_buffer_storage_class"
4425 OpExtension "SPV_KHR_variable_pointers"
4426 OpExtension "SPV_KHR_16bit_storage"
4427 OpMemoryModel Logical GLSL450
4428 OpEntryPoint GLCompute %main "main"
4429 OpDecorate %_ FPRoundingMode RTE
4430 %half = OpTypeFloat 16
4431 %float = OpTypeFloat 32
4432 %float_1_25 = OpConstant %float 1.25
4433 %half_ptr = OpTypePointer StorageBuffer %half
4434 %half_ptr_var = OpVariable %half_ptr StorageBuffer
4435 %void = OpTypeVoid
4436 %func = OpTypeFunction %void
4437 %main = OpFunction %void None %func
4438 %main_entry = OpLabel
4439 %_ = OpFConvert %half %float_1_25
4440 OpStore %half_ptr_var %_
4441 OpReturn
4442 OpFunctionEnd
4443 )";
4444
4445 CompileSuccessfully(spirv);
4446 EXPECT_EQ(SPV_SUCCESS, ValidateAndRetrieveValidationState());
4447 }
4448
4449 TEST_F(ValidateDecorations, FPRoundingModeVectorGood) {
4450 std::string spirv = R"(
4451 OpCapability Shader
4452 OpCapability Linkage
4453 OpCapability StorageBuffer16BitAccess
4454 OpExtension "SPV_KHR_storage_buffer_storage_class"
4455 OpExtension "SPV_KHR_variable_pointers"
4456 OpExtension "SPV_KHR_16bit_storage"
4457 OpMemoryModel Logical GLSL450
4458 OpEntryPoint GLCompute %main "main"
4459 OpDecorate %_ FPRoundingMode RTE
4460 %half = OpTypeFloat 16
4461 %float = OpTypeFloat 32
4462 %v2half = OpTypeVector %half 2
4463 %v2float = OpTypeVector %float 2
4464 %float_1_25 = OpConstant %float 1.25
4465 %floats = OpConstantComposite %v2float %float_1_25 %float_1_25
4466 %halfs_ptr = OpTypePointer StorageBuffer %v2half
4467 %halfs_ptr_var = OpVariable %halfs_ptr StorageBuffer
4468 %void = OpTypeVoid
4469 %func = OpTypeFunction %void
4470 %main = OpFunction %void None %func
4471 %main_entry = OpLabel
4472 %_ = OpFConvert %v2half %floats
4473 OpStore %halfs_ptr_var %_
4474 OpReturn
4475 OpFunctionEnd
4476 )";
4477
4478 CompileSuccessfully(spirv);
4479 EXPECT_EQ(SPV_SUCCESS, ValidateAndRetrieveValidationState());
4480 }
4481
4482 TEST_F(ValidateDecorations, FPRoundingModeNotOpFConvert) {
4483 std::string spirv = R"(
4484 OpCapability Shader
4485 OpCapability Linkage
4486 OpCapability StorageBuffer16BitAccess
4487 OpExtension "SPV_KHR_storage_buffer_storage_class"
4488 OpExtension "SPV_KHR_variable_pointers"
4489 OpExtension "SPV_KHR_16bit_storage"
4490 OpMemoryModel Logical GLSL450
4491 OpEntryPoint GLCompute %main "main"
4492 OpDecorate %_ FPRoundingMode RTE
4493 %short = OpTypeInt 16 1
4494 %int = OpTypeInt 32 1
4495 %int_17 = OpConstant %int 17
4496 %short_ptr = OpTypePointer StorageBuffer %short
4497 %short_ptr_var = OpVariable %short_ptr StorageBuffer
4498 %void = OpTypeVoid
4499 %func = OpTypeFunction %void
4500 %main = OpFunction %void None %func
4501 %main_entry = OpLabel
4502 %_ = OpSConvert %short %int_17
4503 OpStore %short_ptr_var %_
4504 OpReturn
4505 OpFunctionEnd
4506 )";
4507
4508 CompileSuccessfully(spirv);
4509 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateAndRetrieveValidationState());
4510 EXPECT_THAT(getDiagnosticString(),
4511 HasSubstr("FPRoundingMode decoration can be applied only to a "
4512 "width-only conversion instruction for floating-point "
4513 "object."));
4514 }
4515
4516 TEST_F(ValidateDecorations, FPRoundingModeNoOpStoreGood) {
4517 std::string spirv = R"(
4518 OpCapability Shader
4519 OpCapability Linkage
4520 OpCapability StorageBuffer16BitAccess
4521 OpExtension "SPV_KHR_storage_buffer_storage_class"
4522 OpExtension "SPV_KHR_variable_pointers"
4523 OpExtension "SPV_KHR_16bit_storage"
4524 OpMemoryModel Logical GLSL450
4525 OpEntryPoint GLCompute %main "main"
4526 OpDecorate %_ FPRoundingMode RTE
4527 %half = OpTypeFloat 16
4528 %float = OpTypeFloat 32
4529 %float_1_25 = OpConstant %float 1.25
4530 %half_ptr = OpTypePointer StorageBuffer %half
4531 %half_ptr_var = OpVariable %half_ptr StorageBuffer
4532 %void = OpTypeVoid
4533 %func = OpTypeFunction %void
4534 %main = OpFunction %void None %func
4535 %main_entry = OpLabel
4536 %_ = OpFConvert %half %float_1_25
4537 OpReturn
4538 OpFunctionEnd
4539 )";
4540
4541 CompileSuccessfully(spirv);
4542 EXPECT_EQ(SPV_SUCCESS, ValidateAndRetrieveValidationState());
4543 }
4544
4545 TEST_F(ValidateDecorations, FPRoundingModeFConvert64to16Good) {
4546 std::string spirv = R"(
4547 OpCapability Shader
4548 OpCapability Linkage
4549 OpCapability StorageBuffer16BitAccess
4550 OpCapability Float64
4551 OpExtension "SPV_KHR_storage_buffer_storage_class"
4552 OpExtension "SPV_KHR_variable_pointers"
4553 OpExtension "SPV_KHR_16bit_storage"
4554 OpMemoryModel Logical GLSL450
4555 OpEntryPoint GLCompute %main "main"
4556 OpDecorate %_ FPRoundingMode RTE
4557 %half = OpTypeFloat 16
4558 %double = OpTypeFloat 64
4559 %double_1_25 = OpConstant %double 1.25
4560 %half_ptr = OpTypePointer StorageBuffer %half
4561 %half_ptr_var = OpVariable %half_ptr StorageBuffer
4562 %void = OpTypeVoid
4563 %func = OpTypeFunction %void
4564 %main = OpFunction %void None %func
4565 %main_entry = OpLabel
4566 %_ = OpFConvert %half %double_1_25
4567 OpStore %half_ptr_var %_
4568 OpReturn
4569 OpFunctionEnd
4570 )";
4571
4572 CompileSuccessfully(spirv);
4573 EXPECT_EQ(SPV_SUCCESS, ValidateAndRetrieveValidationState());
4574 }
4575
4576 TEST_F(ValidateDecorations, FPRoundingModeNotStoreInFloat16) {
4577 std::string spirv = R"(
4578 OpCapability Shader
4579 OpCapability Linkage
4580 OpCapability StorageBuffer16BitAccess
4581 OpCapability Float64
4582 OpExtension "SPV_KHR_storage_buffer_storage_class"
4583 OpExtension "SPV_KHR_variable_pointers"
4584 OpExtension "SPV_KHR_16bit_storage"
4585 OpMemoryModel Logical GLSL450
4586 OpEntryPoint GLCompute %main "main"
4587 OpDecorate %_ FPRoundingMode RTE
4588 %float = OpTypeFloat 32
4589 %double = OpTypeFloat 64
4590 %double_1_25 = OpConstant %double 1.25
4591 %float_ptr = OpTypePointer StorageBuffer %float
4592 %float_ptr_var = OpVariable %float_ptr StorageBuffer
4593 %void = OpTypeVoid
4594 %func = OpTypeFunction %void
4595 %main = OpFunction %void None %func
4596 %main_entry = OpLabel
4597 %_ = OpFConvert %float %double_1_25
4598 OpStore %float_ptr_var %_
4599 OpReturn
4600 OpFunctionEnd
4601 )";
4602
4603 CompileSuccessfully(spirv);
4604 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateAndRetrieveValidationState());
4605 EXPECT_THAT(
4606 getDiagnosticString(),
4607 HasSubstr("FPRoundingMode decoration can be applied only to the "
4608 "Object operand of an OpStore storing through a "
4609 "pointer to a 16-bit floating-point scalar or vector object."));
4610 }
4611
4612 TEST_F(ValidateDecorations, FPRoundingModeMultipleOpStoreGood) {
4613 std::string spirv = R"(
4614 OpCapability Shader
4615 OpCapability Linkage
4616 OpCapability StorageBuffer16BitAccess
4617 OpExtension "SPV_KHR_storage_buffer_storage_class"
4618 OpExtension "SPV_KHR_variable_pointers"
4619 OpExtension "SPV_KHR_16bit_storage"
4620 OpMemoryModel Logical GLSL450
4621 OpEntryPoint GLCompute %main "main"
4622 OpDecorate %_ FPRoundingMode RTE
4623 %half = OpTypeFloat 16
4624 %float = OpTypeFloat 32
4625 %float_1_25 = OpConstant %float 1.25
4626 %half_ptr = OpTypePointer StorageBuffer %half
4627 %half_ptr_var_0 = OpVariable %half_ptr StorageBuffer
4628 %half_ptr_var_1 = OpVariable %half_ptr StorageBuffer
4629 %half_ptr_var_2 = OpVariable %half_ptr StorageBuffer
4630 %void = OpTypeVoid
4631 %func = OpTypeFunction %void
4632 %main = OpFunction %void None %func
4633 %main_entry = OpLabel
4634 %_ = OpFConvert %half %float_1_25
4635 OpStore %half_ptr_var_0 %_
4636 OpStore %half_ptr_var_1 %_
4637 OpStore %half_ptr_var_2 %_
4638 OpReturn
4639 OpFunctionEnd
4640 )";
4641
4642 CompileSuccessfully(spirv);
4643 EXPECT_EQ(SPV_SUCCESS, ValidateAndRetrieveValidationState());
4644 }
4645
4646 TEST_F(ValidateDecorations, FPRoundingModeMultipleUsesBad) {
4647 std::string spirv = R"(
4648 OpCapability Shader
4649 OpCapability Linkage
4650 OpCapability StorageBuffer16BitAccess
4651 OpExtension "SPV_KHR_storage_buffer_storage_class"
4652 OpExtension "SPV_KHR_variable_pointers"
4653 OpExtension "SPV_KHR_16bit_storage"
4654 OpMemoryModel Logical GLSL450
4655 OpEntryPoint GLCompute %main "main"
4656 OpDecorate %_ FPRoundingMode RTE
4657 %half = OpTypeFloat 16
4658 %float = OpTypeFloat 32
4659 %float_1_25 = OpConstant %float 1.25
4660 %half_ptr = OpTypePointer StorageBuffer %half
4661 %half_ptr_var_0 = OpVariable %half_ptr StorageBuffer
4662 %half_ptr_var_1 = OpVariable %half_ptr StorageBuffer
4663 %void = OpTypeVoid
4664 %func = OpTypeFunction %void
4665 %main = OpFunction %void None %func
4666 %main_entry = OpLabel
4667 %_ = OpFConvert %half %float_1_25
4668 OpStore %half_ptr_var_0 %_
4669 %result = OpFAdd %half %_ %_
4670 OpStore %half_ptr_var_1 %_
4671 OpReturn
4672 OpFunctionEnd
4673 )";
4674
4675 CompileSuccessfully(spirv);
4676 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateAndRetrieveValidationState());
4677 EXPECT_THAT(getDiagnosticString(),
4678 HasSubstr("FPRoundingMode decoration can be applied only to the "
4679 "Object operand of an OpStore."));
4680 }
4681
4682 TEST_F(ValidateDecorations, GroupDecorateTargetsDecorationGroup) {
4683 std::string spirv = R"(
4684 OpCapability Shader
4685 OpCapability Linkage
4686 OpMemoryModel Logical GLSL450
4687 %1 = OpDecorationGroup
4688 OpGroupDecorate %1 %1
4689 )";
4690
4691 CompileSuccessfully(spirv);
4692 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
4693 EXPECT_THAT(getDiagnosticString(),
4694 HasSubstr("OpGroupDecorate may not target OpDecorationGroup <id> "
4695 "'1[%1]'"));
4696 }
4697
4698 TEST_F(ValidateDecorations, GroupDecorateTargetsDecorationGroup2) {
4699 std::string spirv = R"(
4700 OpCapability Shader
4701 OpCapability Linkage
4702 OpMemoryModel Logical GLSL450
4703 %1 = OpDecorationGroup
4704 OpGroupDecorate %1 %2 %1
4705 %2 = OpTypeVoid
4706 )";
4707
4708 CompileSuccessfully(spirv);
4709 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
4710 EXPECT_THAT(getDiagnosticString(),
4711 HasSubstr("OpGroupDecorate may not target OpDecorationGroup <id> "
4712 "'1[%1]'"));
4713 }
4714
4715 TEST_F(ValidateDecorations, RecurseThroughRuntimeArray) {
4716 const std::string spirv = R"(
4717 OpCapability Shader
4718 OpCapability Linkage
4719 OpMemoryModel Logical GLSL450
4720 OpDecorate %outer Block
4721 OpMemberDecorate %inner 0 Offset 0
4722 OpMemberDecorate %inner 1 Offset 1
4723 OpDecorate %runtime ArrayStride 16
4724 OpMemberDecorate %outer 0 Offset 0
4725 %int = OpTypeInt 32 0
4726 %inner = OpTypeStruct %int %int
4727 %runtime = OpTypeRuntimeArray %inner
4728 %outer = OpTypeStruct %runtime
4729 %outer_ptr = OpTypePointer Uniform %outer
4730 %var = OpVariable %outer_ptr Uniform
4731 )";
4732
4733 CompileSuccessfully(spirv);
4734 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
4735 EXPECT_THAT(
4736 getDiagnosticString(),
4737 HasSubstr("Structure id 2 decorated as Block for variable in Uniform "
4738 "storage class must follow standard uniform buffer layout "
4739 "rules: member 1 at offset 1 is not aligned to 4"));
4740 }
4741
4742 TEST_F(ValidateDecorations, EmptyStructAtNonZeroOffsetGood) {
4743 const std::string spirv = R"(
4744 OpCapability Shader
4745 OpMemoryModel Logical GLSL450
4746 OpEntryPoint GLCompute %main "main"
4747 OpExecutionMode %main LocalSize 1 1 1
4748 OpDecorate %struct Block
4749 OpMemberDecorate %struct 0 Offset 0
4750 OpMemberDecorate %struct 1 Offset 16
4751 OpDecorate %var DescriptorSet 0
4752 OpDecorate %var Binding 0
4753 %void = OpTypeVoid
4754 %float = OpTypeFloat 32
4755 %empty = OpTypeStruct
4756 %struct = OpTypeStruct %float %empty
4757 %ptr_struct_ubo = OpTypePointer Uniform %struct
4758 %var = OpVariable %ptr_struct_ubo Uniform
4759 %voidfn = OpTypeFunction %void
4760 %main = OpFunction %void None %voidfn
4761 %entry = OpLabel
4762 OpReturn
4763 OpFunctionEnd
4764 )";
4765
4766 CompileSuccessfully(spirv);
4767 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
4768 }
4769
4770 // Uniform and UniformId decorations
4771
4772 TEST_F(ValidateDecorations, UniformDecorationGood) {
4773 const std::string spirv = R"(
4774 OpCapability Shader
4775 OpMemoryModel Logical Simple
4776 OpEntryPoint GLCompute %main "main"
4777 OpExecutionMode %main LocalSize 1 1 1
4778 OpDecorate %int0 Uniform
4779 OpDecorate %var Uniform
4780 OpDecorate %val Uniform
4781 %void = OpTypeVoid
4782 %int = OpTypeInt 32 1
4783 %int0 = OpConstantNull %int
4784 %intptr = OpTypePointer Private %int
4785 %var = OpVariable %intptr Private
4786 %fn = OpTypeFunction %void
4787 %main = OpFunction %void None %fn
4788 %entry = OpLabel
4789 %val = OpLoad %int %var
4790 OpReturn
4791 OpFunctionEnd
4792 )";
4793
4794 CompileSuccessfully(spirv);
4795 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
4796 EXPECT_THAT(getDiagnosticString(), Eq(""));
4797 }
4798
4799 // Returns SPIR-V assembly for a shader that uses a given decoration
4800 // instruction.
4801 std::string ShaderWithUniformLikeDecoration(const std::string& inst) {
4802 return std::string(R"(
4803 OpCapability Shader
4804 OpMemoryModel Logical Simple
4805 OpEntryPoint GLCompute %main "main"
4806 OpExecutionMode %main LocalSize 1 1 1
4807 OpName %subgroupscope "subgroupscope"
4808 OpName %call "call"
4809 OpName %myfunc "myfunc"
4810 OpName %int0 "int0"
4811 OpName %float0 "float0"
4812 OpName %fn "fn"
4813 )") + inst +
4814 R"(
4815 %void = OpTypeVoid
4816 %float = OpTypeFloat 32
4817 %int = OpTypeInt 32 1
4818 %int0 = OpConstantNull %int
4819 %int_99 = OpConstant %int 99
4820 %subgroupscope = OpConstant %int 3
4821 %float0 = OpConstantNull %float
4822 %fn = OpTypeFunction %void
4823 %myfunc = OpFunction %void None %fn
4824 %myfuncentry = OpLabel
4825 OpReturn
4826 OpFunctionEnd
4827 %main = OpFunction %void None %fn
4828 %entry = OpLabel
4829 %call = OpFunctionCall %void %myfunc
4830 OpReturn
4831 OpFunctionEnd
4832 )";
4833 }
4834
4835 TEST_F(ValidateDecorations, UniformIdDecorationWithScopeIdV13Bad) {
4836 const std::string spirv = ShaderWithUniformLikeDecoration(
4837 "OpDecorateId %int0 UniformId %subgroupscope");
4838 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
4839 EXPECT_EQ(SPV_ERROR_WRONG_VERSION,
4840 ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
4841 EXPECT_THAT(getDiagnosticString(),
4842 HasSubstr("requires SPIR-V version 1.4 or later\n"
4843 " OpDecorateId %int0 UniformId %subgroupscope"))
4844 << spirv;
4845 }
4846
4847 TEST_F(ValidateDecorations, UniformIdDecorationWithScopeIdV13BadTargetV14) {
4848 const std::string spirv = ShaderWithUniformLikeDecoration(
4849 "OpDecorateId %int0 UniformId %subgroupscope");
4850 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
4851 EXPECT_EQ(SPV_ERROR_WRONG_VERSION,
4852 ValidateInstructions(SPV_ENV_UNIVERSAL_1_4));
4853 EXPECT_THAT(getDiagnosticString(),
4854 HasSubstr("requires SPIR-V version 1.4 or later"));
4855 }
4856
4857 TEST_F(ValidateDecorations, UniformIdDecorationWithScopeIdV14Good) {
4858 const std::string spirv = ShaderWithUniformLikeDecoration(
4859 "OpDecorateId %int0 UniformId %subgroupscope");
4860 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_4);
4861 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_UNIVERSAL_1_4));
4862 EXPECT_THAT(getDiagnosticString(), Eq(""));
4863 }
4864
4865 TEST_F(ValidateDecorations, UniformDecorationTargetsTypeBad) {
4866 const std::string spirv =
4867 ShaderWithUniformLikeDecoration("OpDecorate %fn Uniform");
4868
4869 CompileSuccessfully(spirv);
4870 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
4871 EXPECT_THAT(getDiagnosticString(),
4872 HasSubstr("Uniform decoration applied to a non-object"));
4873 EXPECT_THAT(getDiagnosticString(), HasSubstr("%fn = OpTypeFunction %void"));
4874 }
4875
4876 TEST_F(ValidateDecorations, UniformIdDecorationTargetsTypeBad) {
4877 const std::string spirv = ShaderWithUniformLikeDecoration(
4878 "OpDecorateId %fn UniformId %subgroupscope");
4879
4880 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_4);
4881 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_UNIVERSAL_1_4));
4882 EXPECT_THAT(getDiagnosticString(),
4883 HasSubstr("UniformId decoration applied to a non-object"));
4884 EXPECT_THAT(getDiagnosticString(), HasSubstr("%fn = OpTypeFunction %void"));
4885 }
4886
4887 TEST_F(ValidateDecorations, UniformDecorationTargetsVoidValueBad) {
4888 const std::string spirv =
4889 ShaderWithUniformLikeDecoration("OpDecorate %call Uniform");
4890
4891 CompileSuccessfully(spirv);
4892 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
4893 EXPECT_THAT(getDiagnosticString(),
4894 HasSubstr("Uniform decoration applied to a value with void type\n"
4895 " %call = OpFunctionCall %void %myfunc"));
4896 }
4897
4898 TEST_F(ValidateDecorations, UniformIdDecorationTargetsVoidValueBad) {
4899 const std::string spirv = ShaderWithUniformLikeDecoration(
4900 "OpDecorateId %call UniformId %subgroupscope");
4901
4902 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_4);
4903 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_UNIVERSAL_1_4))
4904 << spirv;
4905 EXPECT_THAT(
4906 getDiagnosticString(),
4907 HasSubstr("UniformId decoration applied to a value with void type\n"
4908 " %call = OpFunctionCall %void %myfunc"));
4909 }
4910
4911 TEST_F(ValidateDecorations,
4912 UniformDecorationWithScopeIdV14IdIsFloatValueIsBad) {
4913 const std::string spirv =
4914 ShaderWithUniformLikeDecoration("OpDecorateId %int0 UniformId %float0");
4915
4916 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_4);
4917 EXPECT_EQ(SPV_ERROR_INVALID_DATA,
4918 ValidateInstructions(SPV_ENV_UNIVERSAL_1_4));
4919 EXPECT_THAT(getDiagnosticString(),
4920 HasSubstr("ConstantNull: expected scope to be a 32-bit int"));
4921 }
4922
4923 TEST_F(ValidateDecorations,
4924 UniformDecorationWithScopeIdV14IdIsInvalidIntValueBad) {
4925 const std::string spirv =
4926 ShaderWithUniformLikeDecoration("OpDecorateId %int0 UniformId %int_99");
4927
4928 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_4);
4929 EXPECT_EQ(SPV_ERROR_INVALID_DATA,
4930 ValidateInstructions(SPV_ENV_UNIVERSAL_1_4));
4931 EXPECT_THAT(
4932 getDiagnosticString(),
4933 HasSubstr("Invalid scope value:\n %int_99 = OpConstant %int 99\n"));
4934 }
4935
4936 TEST_F(ValidateDecorations, UniformDecorationWithScopeIdV14VulkanEnv) {
4937 const std::string spirv =
4938 ShaderWithUniformLikeDecoration("OpDecorateId %int0 UniformId %int0");
4939
4940 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_1_SPIRV_1_4);
4941 EXPECT_EQ(SPV_ERROR_INVALID_DATA,
4942 ValidateInstructions(SPV_ENV_VULKAN_1_1_SPIRV_1_4));
4943 EXPECT_THAT(getDiagnosticString(),
4944 HasSubstr(": in Vulkan environment Execution Scope is limited to "
4945 "Workgroup and Subgroup"));
4946 }
4947
4948 TEST_F(ValidateDecorations, UniformDecorationWithWrongInstructionBad) {
4949 const std::string spirv =
4950 ShaderWithUniformLikeDecoration("OpDecorateId %int0 Uniform");
4951
4952 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_2);
4953 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_UNIVERSAL_1_2));
4954 EXPECT_THAT(getDiagnosticString(),
4955 HasSubstr("Decorations that don't take ID parameters may not be "
4956 "used with OpDecorateId\n"
4957 " OpDecorateId %int0 Uniform"));
4958 }
4959
4960 TEST_F(ValidateDecorations, UniformIdDecorationWithWrongInstructionBad) {
4961 const std::string spirv = ShaderWithUniformLikeDecoration(
4962 "OpDecorate %int0 UniformId %subgroupscope");
4963
4964 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_4);
4965 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_UNIVERSAL_1_4));
4966 EXPECT_THAT(
4967 getDiagnosticString(),
4968 HasSubstr(
4969 "Decorations taking ID parameters may not be used with OpDecorateId\n"
4970 " OpDecorate %int0 UniformId %subgroupscope"));
4971 }
4972
4973 TEST_F(ValidateDecorations, MultipleOffsetDecorationsOnSameID) {
4974 std::string spirv = R"(
4975 OpCapability Shader
4976 OpMemoryModel Logical GLSL450
4977 OpEntryPoint Fragment %1 "main"
4978 OpExecutionMode %1 OriginUpperLeft
4979
4980 OpMemberDecorate %struct 0 Offset 0
4981 OpMemberDecorate %struct 0 Offset 0
4982
4983 %void = OpTypeVoid
4984 %voidfn = OpTypeFunction %void
4985 %float = OpTypeFloat 32
4986 %struct = OpTypeStruct %float
4987
4988 %1 = OpFunction %void None %voidfn
4989 %label = OpLabel
4990 OpReturn
4991 OpFunctionEnd
4992 )";
4993
4994 CompileSuccessfully(spirv);
4995 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateAndRetrieveValidationState());
4996 EXPECT_THAT(getDiagnosticString(),
4997 HasSubstr("ID '2', member '0' decorated with Offset multiple "
4998 "times is not allowed."));
4999 }
5000
5001 TEST_F(ValidateDecorations, MultipleArrayStrideDecorationsOnSameID) {
5002 std::string spirv = R"(
5003 OpCapability Shader
5004 OpMemoryModel Logical GLSL450
5005 OpEntryPoint Fragment %1 "main"
5006 OpExecutionMode %1 OriginUpperLeft
5007
5008 OpDecorate %array ArrayStride 4
5009 OpDecorate %array ArrayStride 4
5010
5011 %void = OpTypeVoid
5012 %voidfn = OpTypeFunction %void
5013 %float = OpTypeFloat 32
5014 %uint = OpTypeInt 32 0
5015 %uint_4 = OpConstant %uint 4
5016 %array = OpTypeArray %float %uint_4
5017
5018 %1 = OpFunction %void None %voidfn
5019 %label = OpLabel
5020 OpReturn
5021 OpFunctionEnd
5022 )";
5023
5024 CompileSuccessfully(spirv);
5025 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateAndRetrieveValidationState());
5026 EXPECT_THAT(getDiagnosticString(),
5027 HasSubstr("ID '2' decorated with ArrayStride multiple "
5028 "times is not allowed."));
5029 }
5030
5031 TEST_F(ValidateDecorations, MultipleMatrixStrideDecorationsOnSameID) {
5032 std::string spirv = R"(
5033 OpCapability Shader
5034 OpMemoryModel Logical GLSL450
5035 OpEntryPoint Fragment %1 "main"
5036 OpExecutionMode %1 OriginUpperLeft
5037
5038 OpMemberDecorate %struct 0 Offset 0
5039 OpMemberDecorate %struct 0 ColMajor
5040 OpMemberDecorate %struct 0 MatrixStride 16
5041 OpMemberDecorate %struct 0 MatrixStride 16
5042
5043 %void = OpTypeVoid
5044 %voidfn = OpTypeFunction %void
5045 %float = OpTypeFloat 32
5046 %fvec4 = OpTypeVector %float 4
5047 %fmat4 = OpTypeMatrix %fvec4 4
5048 %struct = OpTypeStruct %fmat4
5049
5050 %1 = OpFunction %void None %voidfn
5051 %label = OpLabel
5052 OpReturn
5053 OpFunctionEnd
5054 )";
5055
5056 CompileSuccessfully(spirv);
5057 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateAndRetrieveValidationState());
5058 EXPECT_THAT(getDiagnosticString(),
5059 HasSubstr("ID '2', member '0' decorated with MatrixStride "
5060 "multiple times is not allowed."));
5061 }
5062
5063 TEST_F(ValidateDecorations, MultipleRowMajorDecorationsOnSameID) {
5064 std::string spirv = R"(
5065 OpCapability Shader
5066 OpMemoryModel Logical GLSL450
5067 OpEntryPoint Fragment %1 "main"
5068 OpExecutionMode %1 OriginUpperLeft
5069
5070 OpMemberDecorate %struct 0 Offset 0
5071 OpMemberDecorate %struct 0 MatrixStride 16
5072 OpMemberDecorate %struct 0 RowMajor
5073 OpMemberDecorate %struct 0 RowMajor
5074
5075 %void = OpTypeVoid
5076 %voidfn = OpTypeFunction %void
5077 %float = OpTypeFloat 32
5078 %fvec4 = OpTypeVector %float 4
5079 %fmat4 = OpTypeMatrix %fvec4 4
5080 %struct = OpTypeStruct %fmat4
5081
5082 %1 = OpFunction %void None %voidfn
5083 %label = OpLabel
5084 OpReturn
5085 OpFunctionEnd
5086 )";
5087
5088 CompileSuccessfully(spirv);
5089 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateAndRetrieveValidationState());
5090 EXPECT_THAT(getDiagnosticString(),
5091 HasSubstr("ID '2', member '0' decorated with RowMajor multiple "
5092 "times is not allowed."));
5093 }
5094
5095 TEST_F(ValidateDecorations, MultipleColMajorDecorationsOnSameID) {
5096 std::string spirv = R"(
5097 OpCapability Shader
5098 OpMemoryModel Logical GLSL450
5099 OpEntryPoint Fragment %1 "main"
5100 OpExecutionMode %1 OriginUpperLeft
5101
5102 OpMemberDecorate %struct 0 Offset 0
5103 OpMemberDecorate %struct 0 MatrixStride 16
5104 OpMemberDecorate %struct 0 ColMajor
5105 OpMemberDecorate %struct 0 ColMajor
5106
5107 %void = OpTypeVoid
5108 %voidfn = OpTypeFunction %void
5109 %float = OpTypeFloat 32
5110 %fvec4 = OpTypeVector %float 4
5111 %fmat4 = OpTypeMatrix %fvec4 4
5112 %struct = OpTypeStruct %fmat4
5113
5114 %1 = OpFunction %void None %voidfn
5115 %label = OpLabel
5116 OpReturn
5117 OpFunctionEnd
5118 )";
5119
5120 CompileSuccessfully(spirv);
5121 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateAndRetrieveValidationState());
5122 EXPECT_THAT(getDiagnosticString(),
5123 HasSubstr("ID '2', member '0' decorated with ColMajor multiple "
5124 "times is not allowed."));
5125 }
5126
5127 TEST_F(ValidateDecorations, RowMajorAndColMajorDecorationsOnSameID) {
5128 std::string spirv = R"(
5129 OpCapability Shader
5130 OpMemoryModel Logical GLSL450
5131 OpEntryPoint Fragment %1 "main"
5132 OpExecutionMode %1 OriginUpperLeft
5133
5134 OpMemberDecorate %struct 0 Offset 0
5135 OpMemberDecorate %struct 0 MatrixStride 16
5136 OpMemberDecorate %struct 0 ColMajor
5137 OpMemberDecorate %struct 0 RowMajor
5138
5139 %void = OpTypeVoid
5140 %voidfn = OpTypeFunction %void
5141 %float = OpTypeFloat 32
5142 %fvec4 = OpTypeVector %float 4
5143 %fmat4 = OpTypeMatrix %fvec4 4
5144 %struct = OpTypeStruct %fmat4
5145
5146 %1 = OpFunction %void None %voidfn
5147 %label = OpLabel
5148 OpReturn
5149 OpFunctionEnd
5150 )";
5151
5152 CompileSuccessfully(spirv);
5153 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateAndRetrieveValidationState());
5154 EXPECT_THAT(getDiagnosticString(),
5155 HasSubstr("ID '2', member '0' decorated with both RowMajor and "
5156 "ColMajor is not allowed."));
5157 }
5158
5159 TEST_F(ValidateDecorations, BlockAndBufferBlockDecorationsOnSameID) {
5160 std::string spirv = R"(
5161 OpCapability Shader
5162 OpMemoryModel Logical GLSL450
5163 OpEntryPoint Fragment %1 "main"
5164 OpExecutionMode %1 OriginUpperLeft
5165
5166 OpDecorate %struct Block
5167 OpDecorate %struct BufferBlock
5168 OpMemberDecorate %struct 0 Offset 0
5169 OpMemberDecorate %struct 0 MatrixStride 16
5170 OpMemberDecorate %struct 0 RowMajor
5171
5172 %void = OpTypeVoid
5173 %voidfn = OpTypeFunction %void
5174 %float = OpTypeFloat 32
5175 %fvec4 = OpTypeVector %float 4
5176 %fmat4 = OpTypeMatrix %fvec4 4
5177 %struct = OpTypeStruct %fmat4
5178
5179 %1 = OpFunction %void None %voidfn
5180 %label = OpLabel
5181 OpReturn
5182 OpFunctionEnd
5183 )";
5184
5185 CompileSuccessfully(spirv);
5186 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateAndRetrieveValidationState());
5187 EXPECT_THAT(
5188 getDiagnosticString(),
5189 HasSubstr(
5190 "ID '2' decorated with both BufferBlock and Block is not allowed."));
5191 }
5192
5193 std::string MakeIntegerShader(
5194 const std::string& decoration, const std::string& inst,
5195 const std::string& extension =
5196 "OpExtension \"SPV_KHR_no_integer_wrap_decoration\"") {
5197 return R"(
5198 OpCapability Shader
5199 OpCapability Linkage
5200 )" + extension +
5201 R"(
5202 %glsl = OpExtInstImport "GLSL.std.450"
5203 %opencl = OpExtInstImport "OpenCL.std"
5204 OpMemoryModel Logical GLSL450
5205 OpEntryPoint GLCompute %main "main"
5206 OpName %entry "entry"
5207 )" + decoration +
5208 R"(
5209 %void = OpTypeVoid
5210 %voidfn = OpTypeFunction %void
5211 %int = OpTypeInt 32 1
5212 %zero = OpConstantNull %int
5213 %float = OpTypeFloat 32
5214 %float0 = OpConstantNull %float
5215 %main = OpFunction %void None %voidfn
5216 %entry = OpLabel
5217 )" + inst +
5218 R"(
5219 OpReturn
5220 OpFunctionEnd)";
5221 }
5222
5223 // NoSignedWrap
5224
5225 TEST_F(ValidateDecorations, NoSignedWrapOnTypeBad) {
5226 std::string spirv = MakeIntegerShader("OpDecorate %void NoSignedWrap", "");
5227
5228 CompileSuccessfully(spirv);
5229 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
5230 EXPECT_THAT(
5231 getDiagnosticString(),
5232 HasSubstr("NoSignedWrap decoration may not be applied to TypeVoid"));
5233 }
5234
5235 TEST_F(ValidateDecorations, NoSignedWrapOnLabelBad) {
5236 std::string spirv = MakeIntegerShader("OpDecorate %entry NoSignedWrap", "");
5237
5238 CompileSuccessfully(spirv);
5239 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
5240 EXPECT_THAT(getDiagnosticString(),
5241 HasSubstr("NoSignedWrap decoration may not be applied to Label"));
5242 }
5243
5244 TEST_F(ValidateDecorations, NoSignedWrapRequiresExtensionBad) {
5245 std::string spirv = MakeIntegerShader("OpDecorate %val NoSignedWrap",
5246 "%val = OpIAdd %int %zero %zero", "");
5247
5248 CompileSuccessfully(spirv);
5249 EXPECT_NE(SPV_SUCCESS, ValidateInstructions());
5250 EXPECT_THAT(getDiagnosticString(),
5251 HasSubstr("requires one of these extensions: "
5252 "SPV_KHR_no_integer_wrap_decoration"));
5253 }
5254
5255 TEST_F(ValidateDecorations, NoSignedWrapRequiresExtensionV13Bad) {
5256 std::string spirv = MakeIntegerShader("OpDecorate %val NoSignedWrap",
5257 "%val = OpIAdd %int %zero %zero", "");
5258
5259 CompileSuccessfully(spirv);
5260 EXPECT_NE(SPV_SUCCESS, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
5261 EXPECT_THAT(getDiagnosticString(),
5262 HasSubstr("requires one of these extensions: "
5263 "SPV_KHR_no_integer_wrap_decoration"));
5264 }
5265
5266 TEST_F(ValidateDecorations, NoSignedWrapOkInSPV14Good) {
5267 std::string spirv = MakeIntegerShader("OpDecorate %val NoSignedWrap",
5268 "%val = OpIAdd %int %zero %zero", "");
5269
5270 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_4);
5271 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_UNIVERSAL_1_4));
5272 EXPECT_THAT(getDiagnosticString(), Eq(""));
5273 }
5274
5275 TEST_F(ValidateDecorations, NoSignedWrapIAddGood) {
5276 std::string spirv = MakeIntegerShader("OpDecorate %val NoSignedWrap",
5277 "%val = OpIAdd %int %zero %zero");
5278
5279 CompileSuccessfully(spirv);
5280 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
5281 EXPECT_THAT(getDiagnosticString(), Eq(""));
5282 }
5283
5284 TEST_F(ValidateDecorations, NoSignedWrapISubGood) {
5285 std::string spirv = MakeIntegerShader("OpDecorate %val NoSignedWrap",
5286 "%val = OpISub %int %zero %zero");
5287
5288 CompileSuccessfully(spirv);
5289 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
5290 EXPECT_THAT(getDiagnosticString(), Eq(""));
5291 }
5292
5293 TEST_F(ValidateDecorations, NoSignedWrapIMulGood) {
5294 std::string spirv = MakeIntegerShader("OpDecorate %val NoSignedWrap",
5295 "%val = OpIMul %int %zero %zero");
5296
5297 CompileSuccessfully(spirv);
5298 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
5299 EXPECT_THAT(getDiagnosticString(), Eq(""));
5300 }
5301
5302 TEST_F(ValidateDecorations, NoSignedWrapShiftLeftLogicalGood) {
5303 std::string spirv =
5304 MakeIntegerShader("OpDecorate %val NoSignedWrap",
5305 "%val = OpShiftLeftLogical %int %zero %zero");
5306
5307 CompileSuccessfully(spirv);
5308 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
5309 EXPECT_THAT(getDiagnosticString(), Eq(""));
5310 }
5311
5312 TEST_F(ValidateDecorations, NoSignedWrapSNegateGood) {
5313 std::string spirv = MakeIntegerShader("OpDecorate %val NoSignedWrap",
5314 "%val = OpSNegate %int %zero");
5315
5316 CompileSuccessfully(spirv);
5317 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
5318 EXPECT_THAT(getDiagnosticString(), Eq(""));
5319 }
5320
5321 TEST_F(ValidateDecorations, NoSignedWrapSRemBad) {
5322 std::string spirv = MakeIntegerShader("OpDecorate %val NoSignedWrap",
5323 "%val = OpSRem %int %zero %zero");
5324
5325 CompileSuccessfully(spirv);
5326 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
5327 EXPECT_THAT(getDiagnosticString(),
5328 HasSubstr("NoSignedWrap decoration may not be applied to SRem"));
5329 }
5330
5331 TEST_F(ValidateDecorations, NoSignedWrapFAddBad) {
5332 std::string spirv = MakeIntegerShader("OpDecorate %val NoSignedWrap",
5333 "%val = OpFAdd %float %float0 %float0");
5334
5335 CompileSuccessfully(spirv);
5336 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
5337 EXPECT_THAT(getDiagnosticString(),
5338 HasSubstr("NoSignedWrap decoration may not be applied to FAdd"));
5339 }
5340
5341 TEST_F(ValidateDecorations, NoSignedWrapExtInstOpenCLGood) {
5342 std::string spirv =
5343 MakeIntegerShader("OpDecorate %val NoSignedWrap",
5344 "%val = OpExtInst %int %opencl s_abs %zero");
5345
5346 CompileSuccessfully(spirv);
5347 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
5348 EXPECT_THAT(getDiagnosticString(), Eq(""));
5349 }
5350
5351 TEST_F(ValidateDecorations, NoSignedWrapExtInstGLSLGood) {
5352 std::string spirv = MakeIntegerShader(
5353 "OpDecorate %val NoSignedWrap", "%val = OpExtInst %int %glsl SAbs %zero");
5354
5355 CompileSuccessfully(spirv);
5356 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
5357 EXPECT_THAT(getDiagnosticString(), Eq(""));
5358 }
5359
5360 // TODO(dneto): For NoSignedWrap and NoUnsignedWrap, permit
5361 // "OpExtInst for instruction numbers specified in the extended
5362 // instruction-set specifications as accepting this decoration."
5363
5364 // NoUnignedWrap
5365
5366 TEST_F(ValidateDecorations, NoUnsignedWrapOnTypeBad) {
5367 std::string spirv = MakeIntegerShader("OpDecorate %void NoUnsignedWrap", "");
5368
5369 CompileSuccessfully(spirv);
5370 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
5371 EXPECT_THAT(
5372 getDiagnosticString(),
5373 HasSubstr("NoUnsignedWrap decoration may not be applied to TypeVoid"));
5374 }
5375
5376 TEST_F(ValidateDecorations, NoUnsignedWrapOnLabelBad) {
5377 std::string spirv = MakeIntegerShader("OpDecorate %entry NoUnsignedWrap", "");
5378
5379 CompileSuccessfully(spirv);
5380 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
5381 EXPECT_THAT(
5382 getDiagnosticString(),
5383 HasSubstr("NoUnsignedWrap decoration may not be applied to Label"));
5384 }
5385
5386 TEST_F(ValidateDecorations, NoUnsignedWrapRequiresExtensionBad) {
5387 std::string spirv = MakeIntegerShader("OpDecorate %val NoUnsignedWrap",
5388 "%val = OpIAdd %int %zero %zero", "");
5389
5390 CompileSuccessfully(spirv);
5391 EXPECT_NE(SPV_SUCCESS, ValidateInstructions());
5392 EXPECT_THAT(getDiagnosticString(),
5393 HasSubstr("requires one of these extensions: "
5394 "SPV_KHR_no_integer_wrap_decoration"));
5395 }
5396
5397 TEST_F(ValidateDecorations, NoUnsignedWrapRequiresExtensionV13Bad) {
5398 std::string spirv = MakeIntegerShader("OpDecorate %val NoUnsignedWrap",
5399 "%val = OpIAdd %int %zero %zero", "");
5400
5401 CompileSuccessfully(spirv);
5402 EXPECT_NE(SPV_SUCCESS, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
5403 EXPECT_THAT(getDiagnosticString(),
5404 HasSubstr("requires one of these extensions: "
5405 "SPV_KHR_no_integer_wrap_decoration"));
5406 }
5407
5408 TEST_F(ValidateDecorations, NoUnsignedWrapOkInSPV14Good) {
5409 std::string spirv = MakeIntegerShader("OpDecorate %val NoUnsignedWrap",
5410 "%val = OpIAdd %int %zero %zero", "");
5411
5412 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_4);
5413 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_UNIVERSAL_1_4));
5414 EXPECT_THAT(getDiagnosticString(), Eq(""));
5415 }
5416
5417 TEST_F(ValidateDecorations, NoUnsignedWrapIAddGood) {
5418 std::string spirv = MakeIntegerShader("OpDecorate %val NoUnsignedWrap",
5419 "%val = OpIAdd %int %zero %zero");
5420
5421 CompileSuccessfully(spirv);
5422 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
5423 EXPECT_THAT(getDiagnosticString(), Eq(""));
5424 }
5425
5426 TEST_F(ValidateDecorations, NoUnsignedWrapISubGood) {
5427 std::string spirv = MakeIntegerShader("OpDecorate %val NoUnsignedWrap",
5428 "%val = OpISub %int %zero %zero");
5429
5430 CompileSuccessfully(spirv);
5431 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
5432 EXPECT_THAT(getDiagnosticString(), Eq(""));
5433 }
5434
5435 TEST_F(ValidateDecorations, NoUnsignedWrapIMulGood) {
5436 std::string spirv = MakeIntegerShader("OpDecorate %val NoUnsignedWrap",
5437 "%val = OpIMul %int %zero %zero");
5438
5439 CompileSuccessfully(spirv);
5440 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
5441 EXPECT_THAT(getDiagnosticString(), Eq(""));
5442 }
5443
5444 TEST_F(ValidateDecorations, NoUnsignedWrapShiftLeftLogicalGood) {
5445 std::string spirv =
5446 MakeIntegerShader("OpDecorate %val NoUnsignedWrap",
5447 "%val = OpShiftLeftLogical %int %zero %zero");
5448
5449 CompileSuccessfully(spirv);
5450 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
5451 EXPECT_THAT(getDiagnosticString(), Eq(""));
5452 }
5453
5454 TEST_F(ValidateDecorations, NoUnsignedWrapSNegateGood) {
5455 std::string spirv = MakeIntegerShader("OpDecorate %val NoUnsignedWrap",
5456 "%val = OpSNegate %int %zero");
5457
5458 CompileSuccessfully(spirv);
5459 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
5460 EXPECT_THAT(getDiagnosticString(), Eq(""));
5461 }
5462
5463 TEST_F(ValidateDecorations, NoUnsignedWrapSRemBad) {
5464 std::string spirv = MakeIntegerShader("OpDecorate %val NoUnsignedWrap",
5465 "%val = OpSRem %int %zero %zero");
5466
5467 CompileSuccessfully(spirv);
5468 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
5469 EXPECT_THAT(
5470 getDiagnosticString(),
5471 HasSubstr("NoUnsignedWrap decoration may not be applied to SRem"));
5472 }
5473
5474 TEST_F(ValidateDecorations, NoUnsignedWrapFAddBad) {
5475 std::string spirv = MakeIntegerShader("OpDecorate %val NoUnsignedWrap",
5476 "%val = OpFAdd %float %float0 %float0");
5477
5478 CompileSuccessfully(spirv);
5479 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
5480 EXPECT_THAT(
5481 getDiagnosticString(),
5482 HasSubstr("NoUnsignedWrap decoration may not be applied to FAdd"));
5483 }
5484
5485 TEST_F(ValidateDecorations, NoUnsignedWrapExtInstOpenCLGood) {
5486 std::string spirv =
5487 MakeIntegerShader("OpDecorate %val NoUnsignedWrap",
5488 "%val = OpExtInst %int %opencl s_abs %zero");
5489
5490 CompileSuccessfully(spirv);
5491 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
5492 EXPECT_THAT(getDiagnosticString(), Eq(""));
5493 }
5494
5495 TEST_F(ValidateDecorations, NoUnsignedWrapExtInstGLSLGood) {
5496 std::string spirv =
5497 MakeIntegerShader("OpDecorate %val NoUnsignedWrap",
5498 "%val = OpExtInst %int %glsl SAbs %zero");
5499
5500 CompileSuccessfully(spirv);
5501 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
5502 EXPECT_THAT(getDiagnosticString(), Eq(""));
5503 }
5504
5505 TEST_F(ValidateDecorations, AliasedandRestrictBad) {
5506 const std::string body = R"(
5507 OpCapability Shader
5508 %1 = OpExtInstImport "GLSL.std.450"
5509 OpMemoryModel Logical GLSL450
5510 OpEntryPoint GLCompute %main "main"
5511 OpExecutionMode %main LocalSize 1 1 1
5512 OpSource GLSL 430
5513 OpMemberDecorate %Output 0 Offset 0
5514 OpDecorate %Output BufferBlock
5515 OpDecorate %dataOutput Restrict
5516 OpDecorate %dataOutput Aliased
5517 %void = OpTypeVoid
5518 %3 = OpTypeFunction %void
5519 %float = OpTypeFloat 32
5520 %Output = OpTypeStruct %float
5521 %_ptr_Uniform_Output = OpTypePointer Uniform %Output
5522 %dataOutput = OpVariable %_ptr_Uniform_Output Uniform
5523 %main = OpFunction %void None %3
5524 %5 = OpLabel
5525 OpReturn
5526 OpFunctionEnd
5527 )";
5528
5529 CompileSuccessfully(body.c_str());
5530 ASSERT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
5531 EXPECT_THAT(
5532 getDiagnosticString(),
5533 HasSubstr("decorated with both Aliased and Restrict is not allowed"));
5534 }
5535
5536 // TODO(dneto): For NoUnsignedWrap and NoUnsignedWrap, permit
5537 // "OpExtInst for instruction numbers specified in the extended
5538 // instruction-set specifications as accepting this decoration."
5539
5540 TEST_F(ValidateDecorations, PSBAliasedRestrictPointerSuccess) {
5541 const std::string body = R"(
5542 OpCapability PhysicalStorageBufferAddressesEXT
5543 OpCapability Int64
5544 OpCapability Shader
5545 OpExtension "SPV_EXT_physical_storage_buffer"
5546 OpMemoryModel PhysicalStorageBuffer64EXT GLSL450
5547 OpEntryPoint Fragment %main "main"
5548 OpExecutionMode %main OriginUpperLeft
5549 OpDecorate %val1 RestrictPointerEXT
5550 %uint64 = OpTypeInt 64 0
5551 %ptr = OpTypePointer PhysicalStorageBufferEXT %uint64
5552 %pptr_f = OpTypePointer Function %ptr
5553 %void = OpTypeVoid
5554 %voidfn = OpTypeFunction %void
5555 %main = OpFunction %void None %voidfn
5556 %entry = OpLabel
5557 %val1 = OpVariable %pptr_f Function
5558 OpReturn
5559 OpFunctionEnd
5560 )";
5561
5562 CompileSuccessfully(body.c_str());
5563 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
5564 }
5565
5566 TEST_F(ValidateDecorations, PSBAliasedRestrictPointerMissing) {
5567 const std::string body = R"(
5568 OpCapability PhysicalStorageBufferAddressesEXT
5569 OpCapability Int64
5570 OpCapability Shader
5571 OpExtension "SPV_EXT_physical_storage_buffer"
5572 OpMemoryModel PhysicalStorageBuffer64EXT GLSL450
5573 OpEntryPoint Fragment %main "main"
5574 OpExecutionMode %main OriginUpperLeft
5575 %uint64 = OpTypeInt 64 0
5576 %ptr = OpTypePointer PhysicalStorageBufferEXT %uint64
5577 %pptr_f = OpTypePointer Function %ptr
5578 %void = OpTypeVoid
5579 %voidfn = OpTypeFunction %void
5580 %main = OpFunction %void None %voidfn
5581 %entry = OpLabel
5582 %val1 = OpVariable %pptr_f Function
5583 OpReturn
5584 OpFunctionEnd
5585 )";
5586
5587 CompileSuccessfully(body.c_str());
5588 ASSERT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
5589 EXPECT_THAT(getDiagnosticString(),
5590 HasSubstr("expected AliasedPointerEXT or RestrictPointerEXT for "
5591 "PhysicalStorageBufferEXT pointer"));
5592 }
5593
5594 TEST_F(ValidateDecorations, PSBAliasedRestrictPointerBoth) {
5595 const std::string body = R"(
5596 OpCapability PhysicalStorageBufferAddressesEXT
5597 OpCapability Int64
5598 OpCapability Shader
5599 OpExtension "SPV_EXT_physical_storage_buffer"
5600 OpMemoryModel PhysicalStorageBuffer64EXT GLSL450
5601 OpEntryPoint Fragment %main "main"
5602 OpExecutionMode %main OriginUpperLeft
5603 OpDecorate %val1 RestrictPointerEXT
5604 OpDecorate %val1 AliasedPointerEXT
5605 %uint64 = OpTypeInt 64 0
5606 %ptr = OpTypePointer PhysicalStorageBufferEXT %uint64
5607 %pptr_f = OpTypePointer Function %ptr
5608 %void = OpTypeVoid
5609 %voidfn = OpTypeFunction %void
5610 %main = OpFunction %void None %voidfn
5611 %entry = OpLabel
5612 %val1 = OpVariable %pptr_f Function
5613 OpReturn
5614 OpFunctionEnd
5615 )";
5616
5617 CompileSuccessfully(body.c_str());
5618 ASSERT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
5619 EXPECT_THAT(
5620 getDiagnosticString(),
5621 HasSubstr("can't specify both AliasedPointerEXT and RestrictPointerEXT "
5622 "for PhysicalStorageBufferEXT pointer"));
5623 }
5624
5625 TEST_F(ValidateDecorations, PSBAliasedRestrictFunctionParamSuccess) {
5626 const std::string body = R"(
5627 OpCapability PhysicalStorageBufferAddressesEXT
5628 OpCapability Int64
5629 OpCapability Shader
5630 OpExtension "SPV_EXT_physical_storage_buffer"
5631 OpMemoryModel PhysicalStorageBuffer64EXT GLSL450
5632 OpEntryPoint Fragment %main "main"
5633 OpExecutionMode %main OriginUpperLeft
5634 OpDecorate %fparam Restrict
5635 %uint64 = OpTypeInt 64 0
5636 %ptr = OpTypePointer PhysicalStorageBufferEXT %uint64
5637 %void = OpTypeVoid
5638 %voidfn = OpTypeFunction %void
5639 %fnptr = OpTypeFunction %void %ptr
5640 %main = OpFunction %void None %voidfn
5641 %entry = OpLabel
5642 OpReturn
5643 OpFunctionEnd
5644 %fn = OpFunction %void None %fnptr
5645 %fparam = OpFunctionParameter %ptr
5646 %lab = OpLabel
5647 OpReturn
5648 OpFunctionEnd
5649 )";
5650
5651 CompileSuccessfully(body.c_str());
5652 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
5653 }
5654
5655 TEST_F(ValidateDecorations, PSBAliasedRestrictFunctionParamMissing) {
5656 const std::string body = R"(
5657 OpCapability PhysicalStorageBufferAddressesEXT
5658 OpCapability Int64
5659 OpCapability Shader
5660 OpExtension "SPV_EXT_physical_storage_buffer"
5661 OpMemoryModel PhysicalStorageBuffer64EXT GLSL450
5662 OpEntryPoint Fragment %main "main"
5663 OpExecutionMode %main OriginUpperLeft
5664 %uint64 = OpTypeInt 64 0
5665 %ptr = OpTypePointer PhysicalStorageBufferEXT %uint64
5666 %void = OpTypeVoid
5667 %voidfn = OpTypeFunction %void
5668 %fnptr = OpTypeFunction %void %ptr
5669 %main = OpFunction %void None %voidfn
5670 %entry = OpLabel
5671 OpReturn
5672 OpFunctionEnd
5673 %fn = OpFunction %void None %fnptr
5674 %fparam = OpFunctionParameter %ptr
5675 %lab = OpLabel
5676 OpReturn
5677 OpFunctionEnd
5678 )";
5679
5680 CompileSuccessfully(body.c_str());
5681 ASSERT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
5682 EXPECT_THAT(getDiagnosticString(),
5683 HasSubstr("expected Aliased or Restrict for "
5684 "PhysicalStorageBufferEXT pointer"));
5685 }
5686
5687 TEST_F(ValidateDecorations, PSBAliasedRestrictFunctionParamBoth) {
5688 const std::string body = R"(
5689 OpCapability PhysicalStorageBufferAddressesEXT
5690 OpCapability Int64
5691 OpCapability Shader
5692 OpExtension "SPV_EXT_physical_storage_buffer"
5693 OpMemoryModel PhysicalStorageBuffer64EXT GLSL450
5694 OpEntryPoint Fragment %main "main"
5695 OpExecutionMode %main OriginUpperLeft
5696 OpDecorate %fparam Restrict
5697 OpDecorate %fparam Aliased
5698 %uint64 = OpTypeInt 64 0
5699 %ptr = OpTypePointer PhysicalStorageBufferEXT %uint64
5700 %void = OpTypeVoid
5701 %voidfn = OpTypeFunction %void
5702 %fnptr = OpTypeFunction %void %ptr
5703 %main = OpFunction %void None %voidfn
5704 %entry = OpLabel
5705 OpReturn
5706 OpFunctionEnd
5707 %fn = OpFunction %void None %fnptr
5708 %fparam = OpFunctionParameter %ptr
5709 %lab = OpLabel
5710 OpReturn
5711 OpFunctionEnd
5712 )";
5713
5714 CompileSuccessfully(body.c_str());
5715 ASSERT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
5716 EXPECT_THAT(getDiagnosticString(),
5717 HasSubstr("can't specify both Aliased and Restrict for "
5718 "PhysicalStorageBufferEXT pointer"));
5719 }
5720
5721 TEST_F(ValidateDecorations, PSBFPRoundingModeSuccess) {
5722 std::string spirv = R"(
5723 OpCapability PhysicalStorageBufferAddressesEXT
5724 OpCapability Shader
5725 OpCapability Linkage
5726 OpCapability StorageBuffer16BitAccess
5727 OpExtension "SPV_EXT_physical_storage_buffer"
5728 OpExtension "SPV_KHR_storage_buffer_storage_class"
5729 OpExtension "SPV_KHR_variable_pointers"
5730 OpExtension "SPV_KHR_16bit_storage"
5731 OpMemoryModel PhysicalStorageBuffer64EXT GLSL450
5732 OpEntryPoint GLCompute %main "main"
5733 OpDecorate %_ FPRoundingMode RTE
5734 OpDecorate %half_ptr_var AliasedPointerEXT
5735 %half = OpTypeFloat 16
5736 %float = OpTypeFloat 32
5737 %float_1_25 = OpConstant %float 1.25
5738 %half_ptr = OpTypePointer PhysicalStorageBufferEXT %half
5739 %half_pptr_f = OpTypePointer Function %half_ptr
5740 %void = OpTypeVoid
5741 %func = OpTypeFunction %void
5742 %main = OpFunction %void None %func
5743 %main_entry = OpLabel
5744 %half_ptr_var = OpVariable %half_pptr_f Function
5745 %val1 = OpLoad %half_ptr %half_ptr_var
5746 %_ = OpFConvert %half %float_1_25
5747 OpStore %val1 %_ Aligned 2
5748 OpReturn
5749 OpFunctionEnd
5750 )";
5751
5752 CompileSuccessfully(spirv);
5753 EXPECT_EQ(SPV_SUCCESS, ValidateAndRetrieveValidationState());
5754 }
5755
5756 TEST_F(ValidateDecorations, InvalidStraddle) {
5757 const std::string spirv = R"(
5758 OpCapability Shader
5759 OpMemoryModel Logical GLSL450
5760 OpEntryPoint GLCompute %main "main"
5761 OpExecutionMode %main LocalSize 1 1 1
5762 OpMemberDecorate %inner_struct 0 Offset 0
5763 OpMemberDecorate %inner_struct 1 Offset 4
5764 OpDecorate %outer_struct Block
5765 OpMemberDecorate %outer_struct 0 Offset 0
5766 OpMemberDecorate %outer_struct 1 Offset 8
5767 OpDecorate %var DescriptorSet 0
5768 OpDecorate %var Binding 0
5769 %void = OpTypeVoid
5770 %float = OpTypeFloat 32
5771 %float2 = OpTypeVector %float 2
5772 %inner_struct = OpTypeStruct %float %float2
5773 %outer_struct = OpTypeStruct %float2 %inner_struct
5774 %ptr_ssbo_outer = OpTypePointer StorageBuffer %outer_struct
5775 %var = OpVariable %ptr_ssbo_outer StorageBuffer
5776 %void_fn = OpTypeFunction %void
5777 %main = OpFunction %void None %void_fn
5778 %entry = OpLabel
5779 OpReturn
5780 OpFunctionEnd
5781 )";
5782
5783 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_1);
5784 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_1));
5785 EXPECT_THAT(getDiagnosticString(),
5786 HasSubstr("Structure id 2 decorated as Block for variable in "
5787 "StorageBuffer storage class must follow relaxed "
5788 "storage buffer layout rules: member 1 is an "
5789 "improperly straddling vector at offset 12"));
5790 }
5791
5792 TEST_F(ValidateDecorations, DescriptorArray) {
5793 const std::string spirv = R"(
5794 OpCapability Shader
5795 OpExtension "SPV_KHR_storage_buffer_storage_class"
5796 OpMemoryModel Logical GLSL450
5797 OpEntryPoint GLCompute %main "main"
5798 OpExecutionMode %main LocalSize 1 1 1
5799 OpDecorate %struct Block
5800 OpMemberDecorate %struct 0 Offset 0
5801 OpMemberDecorate %struct 1 Offset 1
5802 OpDecorate %var DescriptorSet 0
5803 OpDecorate %var Binding 0
5804 %void = OpTypeVoid
5805 %float = OpTypeFloat 32
5806 %int = OpTypeInt 32 0
5807 %int_2 = OpConstant %int 2
5808 %float2 = OpTypeVector %float 2
5809 %struct = OpTypeStruct %float %float2
5810 %struct_array = OpTypeArray %struct %int_2
5811 %ptr_ssbo_array = OpTypePointer StorageBuffer %struct_array
5812 %var = OpVariable %ptr_ssbo_array StorageBuffer
5813 %void_fn = OpTypeFunction %void
5814 %main = OpFunction %void None %void_fn
5815 %entry = OpLabel
5816 OpReturn
5817 OpFunctionEnd
5818 )";
5819
5820 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_0);
5821 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_0));
5822 EXPECT_THAT(getDiagnosticString(),
5823 HasSubstr("Structure id 2 decorated as Block for variable in "
5824 "StorageBuffer storage class must follow standard "
5825 "storage buffer layout rules: member 1 at offset 1 is "
5826 "not aligned to 8"));
5827 }
5828
5829 TEST_F(ValidateDecorations, DescriptorRuntimeArray) {
5830 const std::string spirv = R"(
5831 OpCapability Shader
5832 OpCapability RuntimeDescriptorArrayEXT
5833 OpExtension "SPV_KHR_storage_buffer_storage_class"
5834 OpExtension "SPV_EXT_descriptor_indexing"
5835 OpMemoryModel Logical GLSL450
5836 OpEntryPoint GLCompute %main "main"
5837 OpExecutionMode %main LocalSize 1 1 1
5838 OpDecorate %struct Block
5839 OpMemberDecorate %struct 0 Offset 0
5840 OpMemberDecorate %struct 1 Offset 1
5841 OpDecorate %var DescriptorSet 0
5842 OpDecorate %var Binding 0
5843 %void = OpTypeVoid
5844 %float = OpTypeFloat 32
5845 %int = OpTypeInt 32 0
5846 %float2 = OpTypeVector %float 2
5847 %struct = OpTypeStruct %float %float2
5848 %struct_array = OpTypeRuntimeArray %struct
5849 %ptr_ssbo_array = OpTypePointer StorageBuffer %struct_array
5850 %var = OpVariable %ptr_ssbo_array StorageBuffer
5851 %void_fn = OpTypeFunction %void
5852 %main = OpFunction %void None %void_fn
5853 %entry = OpLabel
5854 OpReturn
5855 OpFunctionEnd
5856 )";
5857
5858 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_0);
5859 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_0));
5860 EXPECT_THAT(getDiagnosticString(),
5861 HasSubstr("Structure id 2 decorated as Block for variable in "
5862 "StorageBuffer storage class must follow standard "
5863 "storage buffer layout rules: member 1 at offset 1 is "
5864 "not aligned to 8"));
5865 }
5866
5867 TEST_F(ValidateDecorations, MultiDimensionalArray) {
5868 const std::string spirv = R"(
5869 OpCapability Shader
5870 OpMemoryModel Logical GLSL450
5871 OpEntryPoint GLCompute %main "main"
5872 OpExecutionMode %main LocalSize 1 1 1
5873 OpDecorate %struct Block
5874 OpMemberDecorate %struct 0 Offset 0
5875 OpDecorate %array_4 ArrayStride 4
5876 OpDecorate %array_3 ArrayStride 48
5877 OpDecorate %var DescriptorSet 0
5878 OpDecorate %var Binding 0
5879 %void = OpTypeVoid
5880 %int = OpTypeInt 32 0
5881 %int_3 = OpConstant %int 3
5882 %int_4 = OpConstant %int 4
5883 %array_4 = OpTypeArray %int %int_4
5884 %array_3 = OpTypeArray %array_4 %int_3
5885 %struct = OpTypeStruct %array_3
5886 %ptr_struct = OpTypePointer Uniform %struct
5887 %var = OpVariable %ptr_struct Uniform
5888 %void_fn = OpTypeFunction %void
5889 %main = OpFunction %void None %void_fn
5890 %entry = OpLabel
5891 OpReturn
5892 OpFunctionEnd
5893 )";
5894
5895 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_0);
5896 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_0));
5897 EXPECT_THAT(getDiagnosticString(),
5898 HasSubstr("Structure id 2 decorated as Block for variable in "
5899 "Uniform storage class must follow standard uniform "
5900 "buffer layout rules: member 0 contains an array with "
5901 "stride 4 not satisfying alignment to 16"));
5902 }
5903
5904 TEST_F(ValidateDecorations, ImproperStraddleInArray) {
5905 const std::string spirv = R"(
5906 OpCapability Shader
5907 OpMemoryModel Logical GLSL450
5908 OpEntryPoint GLCompute %main "main"
5909 OpExecutionMode %main LocalSize 1 1 1
5910 OpDecorate %struct Block
5911 OpMemberDecorate %struct 0 Offset 0
5912 OpDecorate %array ArrayStride 24
5913 OpMemberDecorate %inner 0 Offset 0
5914 OpMemberDecorate %inner 1 Offset 4
5915 OpMemberDecorate %inner 2 Offset 12
5916 OpMemberDecorate %inner 3 Offset 16
5917 OpDecorate %var DescriptorSet 0
5918 OpDecorate %var Binding 0
5919 %void = OpTypeVoid
5920 %int = OpTypeInt 32 0
5921 %int_2 = OpConstant %int 2
5922 %int2 = OpTypeVector %int 2
5923 %inner = OpTypeStruct %int %int2 %int %int
5924 %array = OpTypeArray %inner %int_2
5925 %struct = OpTypeStruct %array
5926 %ptr_struct = OpTypePointer StorageBuffer %struct
5927 %var = OpVariable %ptr_struct StorageBuffer
5928 %void_fn = OpTypeFunction %void
5929 %main = OpFunction %void None %void_fn
5930 %entry = OpLabel
5931 OpReturn
5932 OpFunctionEnd
5933 )";
5934
5935 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_1);
5936 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_1));
5937 EXPECT_THAT(getDiagnosticString(),
5938 HasSubstr("Structure id 4 decorated as Block for variable in "
5939 "StorageBuffer storage class must follow relaxed "
5940 "storage buffer layout rules: member 1 is an "
5941 "improperly straddling vector at offset 28"));
5942 }
5943
5944 TEST_F(ValidateDecorations, LargeArray) {
5945 const std::string spirv = R"(
5946 OpCapability Shader
5947 OpMemoryModel Logical GLSL450
5948 OpEntryPoint GLCompute %main "main"
5949 OpExecutionMode %main LocalSize 1 1 1
5950 OpDecorate %struct Block
5951 OpMemberDecorate %struct 0 Offset 0
5952 OpDecorate %array ArrayStride 24
5953 OpMemberDecorate %inner 0 Offset 0
5954 OpMemberDecorate %inner 1 Offset 8
5955 OpMemberDecorate %inner 2 Offset 16
5956 OpMemberDecorate %inner 3 Offset 20
5957 OpDecorate %var DescriptorSet 0
5958 OpDecorate %var Binding 0
5959 %void = OpTypeVoid
5960 %int = OpTypeInt 32 0
5961 %int_2000000 = OpConstant %int 2000000
5962 %int2 = OpTypeVector %int 2
5963 %inner = OpTypeStruct %int %int2 %int %int
5964 %array = OpTypeArray %inner %int_2000000
5965 %struct = OpTypeStruct %array
5966 %ptr_struct = OpTypePointer StorageBuffer %struct
5967 %var = OpVariable %ptr_struct StorageBuffer
5968 %void_fn = OpTypeFunction %void
5969 %main = OpFunction %void None %void_fn
5970 %entry = OpLabel
5971 OpReturn
5972 OpFunctionEnd
5973 )";
5974
5975 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_1);
5976 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_VULKAN_1_1));
5977 }
5978
5979 // NonWritable
5980
5981 // Returns a SPIR-V shader module with variables in various storage classes,
5982 // parameterizable by which ID should be decorated as NonWritable.
5983 std::string ShaderWithNonWritableTarget(const std::string& target,
5984 bool member_decorate = false) {
5985 const std::string decoration_inst =
5986 std::string(member_decorate ? "OpMemberDecorate " : "OpDecorate ") +
5987 target + (member_decorate ? " 0" : "");
5988
5989 return std::string(R"(
5990 OpCapability Shader
5991 OpCapability RuntimeDescriptorArrayEXT
5992 OpExtension "SPV_EXT_descriptor_indexing"
5993 OpExtension "SPV_KHR_storage_buffer_storage_class"
5994 OpMemoryModel Logical GLSL450
5995 OpEntryPoint Vertex %main "main"
5996 OpName %label "label"
5997 OpName %param_f "param_f"
5998 OpName %param_p "param_p"
5999 OpName %_ptr_imstor "_ptr_imstor"
6000 OpName %_ptr_imsam "_ptr_imsam"
6001 OpName %var_wg "var_wg"
6002 OpName %var_imsam "var_imsam"
6003 OpName %var_priv "var_priv"
6004 OpName %var_func "var_func"
6005 OpName %simple_struct "simple_struct"
6006
6007 OpDecorate %struct_b Block
6008 OpDecorate %struct_b_rtarr Block
6009 OpMemberDecorate %struct_b 0 Offset 0
6010 OpMemberDecorate %struct_b_rtarr 0 Offset 0
6011 OpDecorate %rtarr ArrayStride 4
6012 )") + decoration_inst +
6013
6014 R"( NonWritable
6015
6016 %void = OpTypeVoid
6017 %void_fn = OpTypeFunction %void
6018 %float = OpTypeFloat 32
6019 %float_0 = OpConstant %float 0
6020 %int = OpTypeInt 32 0
6021 %int_2 = OpConstant %int 2
6022 %struct_b = OpTypeStruct %float
6023 %rtarr = OpTypeRuntimeArray %float
6024 %struct_b_rtarr = OpTypeStruct %rtarr
6025 %simple_struct = OpTypeStruct %float
6026 ; storage image
6027 %imstor = OpTypeImage %float 2D 0 0 0 2 R32f
6028 ; sampled image
6029 %imsam = OpTypeImage %float 2D 0 0 0 1 R32f
6030 %array_imstor = OpTypeArray %imstor %int_2
6031 %rta_imstor = OpTypeRuntimeArray %imstor
6032
6033 %_ptr_Uniform_stb = OpTypePointer Uniform %struct_b
6034 %_ptr_StorageBuffer_stb = OpTypePointer StorageBuffer %struct_b
6035 %_ptr_StorageBuffer_stb_rtarr = OpTypePointer StorageBuffer %struct_b_rtarr
6036 %_ptr_Workgroup = OpTypePointer Workgroup %float
6037 %_ptr_Private = OpTypePointer Private %float
6038 %_ptr_Function = OpTypePointer Function %float
6039 %_ptr_imstor = OpTypePointer UniformConstant %imstor
6040 %_ptr_imsam = OpTypePointer UniformConstant %imsam
6041 %_ptr_array_imstor = OpTypePointer UniformConstant %array_imstor
6042 %_ptr_rta_imstor = OpTypePointer UniformConstant %rta_imstor
6043
6044 %extra_fn = OpTypeFunction %void %float %_ptr_Private %_ptr_imstor
6045
6046 %var_ubo = OpVariable %_ptr_Uniform_stb Uniform
6047 %var_ssbo_sb = OpVariable %_ptr_StorageBuffer_stb StorageBuffer
6048 %var_ssbo_sb_rtarr = OpVariable %_ptr_StorageBuffer_stb_rtarr StorageBuffer
6049 %var_wg = OpVariable %_ptr_Workgroup Workgroup
6050 %var_priv = OpVariable %_ptr_Private Private
6051 %var_imstor = OpVariable %_ptr_imstor UniformConstant
6052 %var_imsam = OpVariable %_ptr_imsam UniformConstant
6053 %var_array_imstor = OpVariable %_ptr_array_imstor UniformConstant
6054 %var_rta_imstor = OpVariable %_ptr_rta_imstor UniformConstant
6055
6056 %helper = OpFunction %void None %extra_fn
6057 %param_f = OpFunctionParameter %float
6058 %param_p = OpFunctionParameter %_ptr_Private
6059 %param_pimstor = OpFunctionParameter %_ptr_imstor
6060 %helper_label = OpLabel
6061 %helper_func_var = OpVariable %_ptr_Function Function
6062 OpReturn
6063 OpFunctionEnd
6064
6065 %main = OpFunction %void None %void_fn
6066 %label = OpLabel
6067 %var_func = OpVariable %_ptr_Function Function
6068 OpReturn
6069 OpFunctionEnd
6070 )";
6071 }
6072
6073 TEST_F(ValidateDecorations, NonWritableLabelTargetBad) {
6074 std::string spirv = ShaderWithNonWritableTarget("%label");
6075
6076 CompileSuccessfully(spirv);
6077 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
6078 EXPECT_THAT(getDiagnosticString(),
6079 HasSubstr("Target of NonWritable decoration must be a "
6080 "memory object declaration (a variable or a function "
6081 "parameter)\n %label = OpLabel"));
6082 }
6083
6084 TEST_F(ValidateDecorations, NonWritableTypeTargetBad) {
6085 std::string spirv = ShaderWithNonWritableTarget("%void");
6086
6087 CompileSuccessfully(spirv);
6088 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
6089 EXPECT_THAT(getDiagnosticString(),
6090 HasSubstr("Target of NonWritable decoration must be a "
6091 "memory object declaration (a variable or a function "
6092 "parameter)\n %void = OpTypeVoid"));
6093 }
6094
6095 TEST_F(ValidateDecorations, NonWritableValueTargetBad) {
6096 std::string spirv = ShaderWithNonWritableTarget("%float_0");
6097
6098 CompileSuccessfully(spirv);
6099 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
6100 EXPECT_THAT(getDiagnosticString(),
6101 HasSubstr("Target of NonWritable decoration must be a "
6102 "memory object declaration (a variable or a function "
6103 "parameter)\n %float_0 = OpConstant %float 0"));
6104 }
6105
6106 TEST_F(ValidateDecorations, NonWritableValueParamBad) {
6107 std::string spirv = ShaderWithNonWritableTarget("%param_f");
6108
6109 CompileSuccessfully(spirv);
6110 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
6111 EXPECT_THAT(getDiagnosticString(),
6112 HasSubstr("Target of NonWritable decoration is invalid: must "
6113 "point to a storage image, uniform block, or storage "
6114 "buffer\n %param_f = OpFunctionParameter %float"));
6115 }
6116
6117 TEST_F(ValidateDecorations, NonWritablePointerParamButWrongTypeBad) {
6118 std::string spirv = ShaderWithNonWritableTarget("%param_p");
6119
6120 CompileSuccessfully(spirv);
6121 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
6122 EXPECT_THAT(
6123 getDiagnosticString(),
6124 HasSubstr(
6125 "Target of NonWritable decoration is invalid: must "
6126 "point to a storage image, uniform block, or storage "
6127 "buffer\n %param_p = OpFunctionParameter %_ptr_Private_float"));
6128 }
6129
6130 TEST_F(ValidateDecorations, NonWritablePointerParamStorageImageGood) {
6131 std::string spirv = ShaderWithNonWritableTarget("%param_pimstor");
6132
6133 CompileSuccessfully(spirv);
6134 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
6135 EXPECT_THAT(getDiagnosticString(), Eq(""));
6136 }
6137
6138 TEST_F(ValidateDecorations, NonWritableVarStorageImageGood) {
6139 std::string spirv = ShaderWithNonWritableTarget("%var_imstor");
6140
6141 CompileSuccessfully(spirv);
6142 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
6143 EXPECT_THAT(getDiagnosticString(), Eq(""));
6144 }
6145
6146 TEST_F(ValidateDecorations, NonWritableVarSampledImageBad) {
6147 std::string spirv = ShaderWithNonWritableTarget("%var_imsam");
6148
6149 CompileSuccessfully(spirv);
6150 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
6151 EXPECT_THAT(getDiagnosticString(),
6152 HasSubstr("Target of NonWritable decoration is invalid: must "
6153 "point to a storage image, uniform block, or storage "
6154 "buffer\n %var_imsam"));
6155 }
6156
6157 TEST_F(ValidateDecorations, NonWritableVarUboGood) {
6158 std::string spirv = ShaderWithNonWritableTarget("%var_ubo");
6159
6160 CompileSuccessfully(spirv);
6161 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
6162 EXPECT_THAT(getDiagnosticString(), Eq(""));
6163 }
6164
6165 TEST_F(ValidateDecorations, NonWritableVarSsboInUniformGood) {
6166 const std::string spirv = R"(
6167 OpCapability Shader
6168 OpMemoryModel Logical GLSL450
6169 OpEntryPoint Vertex %main "main"
6170 OpDecorate %struct_bb BufferBlock
6171 OpMemberDecorate %struct_bb 0 Offset 0
6172 OpDecorate %var_ssbo_u NonWritable
6173 %void = OpTypeVoid
6174 %void_fn = OpTypeFunction %void
6175 %float = OpTypeFloat 32
6176 %struct_bb = OpTypeStruct %float
6177 %_ptr_Uniform_stbb = OpTypePointer Uniform %struct_bb
6178 %var_ssbo_u = OpVariable %_ptr_Uniform_stbb Uniform
6179 %main = OpFunction %void None %void_fn
6180 %label = OpLabel
6181 OpReturn
6182 OpFunctionEnd
6183 )";
6184
6185 CompileSuccessfully(spirv);
6186 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
6187 EXPECT_THAT(getDiagnosticString(), Eq(""));
6188 }
6189
6190 TEST_F(ValidateDecorations, NonWritableVarSsboInStorageBufferGood) {
6191 std::string spirv = ShaderWithNonWritableTarget("%var_ssbo_sb");
6192
6193 CompileSuccessfully(spirv);
6194 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
6195 EXPECT_THAT(getDiagnosticString(), Eq(""));
6196 }
6197
6198 TEST_F(ValidateDecorations, NonWritableMemberOfSsboInStorageBufferGood) {
6199 std::string spirv = ShaderWithNonWritableTarget("%struct_b_rtarr", true);
6200
6201 CompileSuccessfully(spirv);
6202 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
6203 EXPECT_THAT(getDiagnosticString(), Eq(""));
6204 }
6205
6206 TEST_F(ValidateDecorations, NonWritableMemberOfStructGood) {
6207 std::string spirv = ShaderWithNonWritableTarget("%simple_struct", true);
6208
6209 CompileSuccessfully(spirv);
6210 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
6211 }
6212
6213 TEST_F(ValidateDecorations, NonWritableVarWorkgroupBad) {
6214 std::string spirv = ShaderWithNonWritableTarget("%var_wg");
6215
6216 CompileSuccessfully(spirv);
6217 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
6218 EXPECT_THAT(getDiagnosticString(),
6219 HasSubstr("Target of NonWritable decoration is invalid: must "
6220 "point to a storage image, uniform block, or storage "
6221 "buffer\n %var_wg"));
6222 }
6223
6224 TEST_F(ValidateDecorations, NonWritableVarWorkgroupV14Bad) {
6225 std::string spirv = ShaderWithNonWritableTarget("%var_wg");
6226
6227 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_4);
6228 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_UNIVERSAL_1_4));
6229 EXPECT_THAT(getDiagnosticString(),
6230 HasSubstr("Target of NonWritable decoration is invalid: must "
6231 "point to a storage image, uniform block, storage "
6232 "buffer, or variable in Private or Function storage "
6233 "class\n %var_wg"));
6234 }
6235
6236 TEST_F(ValidateDecorations, NonWritableVarPrivateBad) {
6237 std::string spirv = ShaderWithNonWritableTarget("%var_priv");
6238
6239 CompileSuccessfully(spirv);
6240 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
6241 EXPECT_THAT(getDiagnosticString(),
6242 HasSubstr("Target of NonWritable decoration is invalid: must "
6243 "point to a storage image, uniform block, or storage "
6244 "buffer\n %var_priv"));
6245 }
6246
6247 TEST_F(ValidateDecorations, NonWritableVarPrivateV13Bad) {
6248 std::string spirv = ShaderWithNonWritableTarget("%var_priv");
6249
6250 CompileSuccessfully(spirv);
6251 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
6252 EXPECT_THAT(getDiagnosticString(),
6253 HasSubstr("Target of NonWritable decoration is invalid: must "
6254 "point to a storage image, uniform block, or storage "
6255 "buffer\n %var_priv"));
6256 }
6257
6258 TEST_F(ValidateDecorations, NonWritableVarPrivateV14Good) {
6259 std::string spirv = ShaderWithNonWritableTarget("%var_priv");
6260
6261 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_4);
6262 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_UNIVERSAL_1_4));
6263 EXPECT_THAT(getDiagnosticString(), Eq(""));
6264 }
6265
6266 TEST_F(ValidateDecorations, NonWritableVarPrivateV13TargetV14Bad) {
6267 std::string spirv = ShaderWithNonWritableTarget("%var_priv");
6268
6269 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
6270 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_UNIVERSAL_1_4));
6271 EXPECT_THAT(getDiagnosticString(),
6272 HasSubstr("Target of NonWritable decoration is invalid: must "
6273 "point to a storage image, uniform block, or storage "
6274 "buffer\n %var_priv"));
6275 }
6276
6277 TEST_F(ValidateDecorations, NonWritableVarFunctionBad) {
6278 std::string spirv = ShaderWithNonWritableTarget("%var_func");
6279
6280 CompileSuccessfully(spirv);
6281 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
6282 EXPECT_THAT(getDiagnosticString(),
6283 HasSubstr("Target of NonWritable decoration is invalid: must "
6284 "point to a storage image, uniform block, or storage "
6285 "buffer\n %var_func"));
6286 }
6287
6288 TEST_F(ValidateDecorations, NonWritableArrayGood) {
6289 std::string spirv = ShaderWithNonWritableTarget("%var_array_imstor");
6290
6291 CompileSuccessfully(spirv);
6292 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
6293 }
6294
6295 TEST_F(ValidateDecorations, NonWritableRuntimeArrayGood) {
6296 std::string spirv = ShaderWithNonWritableTarget("%var_rta_imstor");
6297
6298 CompileSuccessfully(spirv);
6299 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
6300 }
6301
6302 TEST_P(ValidateWebGPUCombineDecorationResult, Decorate) {
6303 const char* const decoration = std::get<0>(GetParam());
6304 const TestResult& test_result = std::get<1>(GetParam());
6305
6306 CodeGenerator generator = CodeGenerator::GetWebGPUShaderCodeGenerator();
6307 generator.before_types_ = "OpDecorate %u32 ";
6308 generator.before_types_ += decoration;
6309 generator.before_types_ += "\n";
6310
6311 EntryPoint entry_point;
6312 entry_point.name = "main";
6313 entry_point.execution_model = "Vertex";
6314 generator.entry_points_.push_back(std::move(entry_point));
6315
6316 CompileSuccessfully(generator.Build(), SPV_ENV_WEBGPU_0);
6317 ASSERT_EQ(test_result.validation_result,
6318 ValidateInstructions(SPV_ENV_WEBGPU_0));
6319 if (test_result.error_str != "") {
6320 EXPECT_THAT(getDiagnosticString(), HasSubstr(test_result.error_str));
6321 }
6322 }
6323
6324 TEST_P(ValidateWebGPUCombineDecorationResult, DecorateMember) {
6325 const char* const decoration = std::get<0>(GetParam());
6326 const TestResult& test_result = std::get<1>(GetParam());
6327
6328 CodeGenerator generator = CodeGenerator::GetWebGPUShaderCodeGenerator();
6329 generator.before_types_ = "OpMemberDecorate %struct_type 0 ";
6330 generator.before_types_ += decoration;
6331 generator.before_types_ += "\n";
6332
6333 generator.after_types_ = "%struct_type = OpTypeStruct %u32\n";
6334
6335 EntryPoint entry_point;
6336 entry_point.name = "main";
6337 entry_point.execution_model = "Vertex";
6338 generator.entry_points_.push_back(std::move(entry_point));
6339
6340 CompileSuccessfully(generator.Build(), SPV_ENV_WEBGPU_0);
6341 ASSERT_EQ(test_result.validation_result,
6342 ValidateInstructions(SPV_ENV_WEBGPU_0));
6343 if (!test_result.error_str.empty()) {
6344 EXPECT_THAT(getDiagnosticString(), HasSubstr(test_result.error_str));
6345 }
6346 }
6347
6348 INSTANTIATE_TEST_SUITE_P(
6349 DecorationCapabilityFailure, ValidateWebGPUCombineDecorationResult,
6350 Combine(Values("CPacked", "Patch", "Sample", "Constant",
6351 "SaturatedConversion", "NonUniformEXT"),
6352 Values(TestResult(SPV_ERROR_INVALID_CAPABILITY,
6353 "requires one of these capabilities"))));
6354
6355 INSTANTIATE_TEST_SUITE_P(
6356 DecorationAllowListFailure, ValidateWebGPUCombineDecorationResult,
6357 Combine(Values("RelaxedPrecision", "BufferBlock", "GLSLShared",
6358 "GLSLPacked", "Invariant", "Volatile", "Coherent"),
6359 Values(TestResult(
6360 SPV_ERROR_INVALID_ID,
6361 "is not valid for the WebGPU execution environment."))));
6362
6363 TEST_F(ValidateDecorations, NonWritableVarFunctionV13Bad) {
6364 std::string spirv = ShaderWithNonWritableTarget("%var_func");
6365
6366 CompileSuccessfully(spirv);
6367 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
6368 EXPECT_THAT(getDiagnosticString(),
6369 HasSubstr("Target of NonWritable decoration is invalid: must "
6370 "point to a storage image, uniform block, or storage "
6371 "buffer\n %var_func"));
6372 }
6373
6374 TEST_F(ValidateDecorations, NonWritableVarFunctionV14Good) {
6375 std::string spirv = ShaderWithNonWritableTarget("%var_func");
6376
6377 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_4);
6378 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_UNIVERSAL_1_4));
6379 EXPECT_THAT(getDiagnosticString(), Eq(""));
6380 }
6381
6382 TEST_F(ValidateDecorations, NonWritableVarFunctionV13TargetV14Bad) {
6383 std::string spirv = ShaderWithNonWritableTarget("%var_func");
6384
6385 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
6386 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_UNIVERSAL_1_4));
6387 EXPECT_THAT(getDiagnosticString(),
6388 HasSubstr("Target of NonWritable decoration is invalid: must "
6389 "point to a storage image, uniform block, or storage "
6390 "buffer\n %var_func"));
6391 }
6392
6393 TEST_F(ValidateDecorations, BufferBlockV13ValV14Good) {
6394 std::string spirv = R"(
6395 OpCapability Shader
6396 OpCapability Linkage
6397 OpMemoryModel Logical GLSL450
6398 OpDecorate %1 BufferBlock
6399 %1 = OpTypeStruct
6400 )";
6401
6402 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
6403 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_UNIVERSAL_1_4));
6404 }
6405
6406 TEST_F(ValidateDecorations, BufferBlockV14Bad) {
6407 std::string spirv = R"(
6408 OpCapability Shader
6409 OpCapability Linkage
6410 OpMemoryModel Logical GLSL450
6411 OpDecorate %1 BufferBlock
6412 %1 = OpTypeStruct
6413 )";
6414
6415 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_4);
6416 EXPECT_EQ(SPV_ERROR_WRONG_VERSION,
6417 ValidateInstructions(SPV_ENV_UNIVERSAL_1_4));
6418 EXPECT_THAT(getDiagnosticString(),
6419 HasSubstr("2nd operand of Decorate: operand BufferBlock(3) "
6420 "requires SPIR-V version 1.3 or earlier"));
6421 }
6422
6423 // Component
6424
6425 TEST_F(ValidateDecorations, ComponentDecorationBadTarget) {
6426 std::string spirv = R"(
6427 OpCapability Shader
6428 OpMemoryModel Logical GLSL450
6429 OpEntryPoint Vertex %main "main"
6430 OpDecorate %t Component 0
6431 %void = OpTypeVoid
6432 %3 = OpTypeFunction %void
6433 %float = OpTypeFloat 32
6434 %t = OpTypeVector %float 2
6435 %main = OpFunction %void None %3
6436 %5 = OpLabel
6437 OpReturn
6438 OpFunctionEnd
6439 )";
6440
6441 CompileSuccessfully(spirv);
6442 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateAndRetrieveValidationState());
6443 EXPECT_THAT(getDiagnosticString(),
6444 HasSubstr("Target of Component decoration must be "
6445 "a memory object declaration"));
6446 }
6447
6448 TEST_F(ValidateDecorations, ComponentDecorationBadStorageClass) {
6449 std::string spirv = R"(
6450 OpCapability Shader
6451 OpMemoryModel Logical GLSL450
6452 OpEntryPoint Vertex %main "main"
6453 OpDecorate %v Component 0
6454 %void = OpTypeVoid
6455 %3 = OpTypeFunction %void
6456 %float = OpTypeFloat 32
6457 %t = OpTypeVector %float 2
6458 %ptr_private = OpTypePointer Private %t
6459 %v = OpVariable %ptr_private Private
6460 %main = OpFunction %void None %3
6461 %5 = OpLabel
6462 OpReturn
6463 OpFunctionEnd
6464 )";
6465
6466 CompileSuccessfully(spirv);
6467 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateAndRetrieveValidationState());
6468 EXPECT_THAT(getDiagnosticString(),
6469 HasSubstr("Target of Component decoration is invalid: must "
6470 "point to a Storage Class of Input(1) or Output(3)"));
6471 }
6472
6473 TEST_F(ValidateDecorations, ComponentDecorationBadTypeVulkan) {
6474 const spv_target_env env = SPV_ENV_VULKAN_1_0;
6475 std::string spirv = R"(
6476 OpCapability Shader
6477 OpCapability Matrix
6478 OpMemoryModel Logical GLSL450
6479 OpEntryPoint Vertex %main "main"
6480 OpDecorate %v Component 0
6481 %void = OpTypeVoid
6482 %3 = OpTypeFunction %void
6483 %float = OpTypeFloat 32
6484 %vtype = OpTypeVector %float 4
6485 %t = OpTypeMatrix %vtype 4
6486 %ptr_input = OpTypePointer Input %t
6487 %v = OpVariable %ptr_input Input
6488 %main = OpFunction %void None %3
6489 %5 = OpLabel
6490 OpReturn
6491 OpFunctionEnd
6492 )";
6493
6494 CompileSuccessfully(spirv, env);
6495 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateAndRetrieveValidationState(env));
6496 EXPECT_THAT(getDiagnosticString(),
6497 HasSubstr("Component decoration specified for type"));
6498 EXPECT_THAT(getDiagnosticString(), HasSubstr("is not a scalar or vector"));
6499 }
6500
6501 std::string ShaderWithComponentDecoration(const std::string& type,
6502 const std::string& decoration) {
6503 return R"(
6504 OpCapability Shader
6505 OpMemoryModel Logical GLSL450
6506 OpEntryPoint Fragment %main "main" %entryPointOutput
6507 OpExecutionMode %main OriginUpperLeft
6508 OpDecorate %entryPointOutput Location 0
6509 OpDecorate %entryPointOutput )" +
6510 decoration + R"(
6511 %void = OpTypeVoid
6512 %3 = OpTypeFunction %void
6513 %float = OpTypeFloat 32
6514 %v3float = OpTypeVector %float 3
6515 %v4float = OpTypeVector %float 4
6516 %uint = OpTypeInt 32 0
6517 %uint_2 = OpConstant %uint 2
6518 %arr_v3float_uint_2 = OpTypeArray %v3float %uint_2
6519 %float_0 = OpConstant %float 0
6520 %_ptr_Output_type = OpTypePointer Output %)" + type + R"(
6521 %entryPointOutput = OpVariable %_ptr_Output_type Output
6522 %main = OpFunction %void None %3
6523 %5 = OpLabel
6524 OpReturn
6525 OpFunctionEnd
6526 )";
6527 }
6528
6529 TEST_F(ValidateDecorations, ComponentDecorationIntGood0Vulkan) {
6530 const spv_target_env env = SPV_ENV_VULKAN_1_0;
6531 std::string spirv = ShaderWithComponentDecoration("uint", "Component 0");
6532
6533 CompileSuccessfully(spirv, env);
6534 EXPECT_EQ(SPV_SUCCESS, ValidateAndRetrieveValidationState(env));
6535 EXPECT_THAT(getDiagnosticString(), Eq(""));
6536 }
6537
6538 TEST_F(ValidateDecorations, ComponentDecorationIntGood1Vulkan) {
6539 const spv_target_env env = SPV_ENV_VULKAN_1_0;
6540 std::string spirv = ShaderWithComponentDecoration("uint", "Component 1");
6541
6542 CompileSuccessfully(spirv, env);
6543 EXPECT_EQ(SPV_SUCCESS, ValidateAndRetrieveValidationState(env));
6544 EXPECT_THAT(getDiagnosticString(), Eq(""));
6545 }
6546
6547 TEST_F(ValidateDecorations, ComponentDecorationIntGood2Vulkan) {
6548 const spv_target_env env = SPV_ENV_VULKAN_1_0;
6549 std::string spirv = ShaderWithComponentDecoration("uint", "Component 2");
6550
6551 CompileSuccessfully(spirv, env);
6552 EXPECT_EQ(SPV_SUCCESS, ValidateAndRetrieveValidationState(env));
6553 EXPECT_THAT(getDiagnosticString(), Eq(""));
6554 }
6555
6556 TEST_F(ValidateDecorations, ComponentDecorationIntGood3Vulkan) {
6557 const spv_target_env env = SPV_ENV_VULKAN_1_0;
6558 std::string spirv = ShaderWithComponentDecoration("uint", "Component 3");
6559
6560 CompileSuccessfully(spirv, env);
6561 EXPECT_EQ(SPV_SUCCESS, ValidateAndRetrieveValidationState(env));
6562 EXPECT_THAT(getDiagnosticString(), Eq(""));
6563 }
6564
6565 TEST_F(ValidateDecorations, ComponentDecorationIntBad4Vulkan) {
6566 const spv_target_env env = SPV_ENV_VULKAN_1_0;
6567 std::string spirv = ShaderWithComponentDecoration("uint", "Component 4");
6568
6569 CompileSuccessfully(spirv, env);
6570 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateAndRetrieveValidationState(env));
6571 EXPECT_THAT(getDiagnosticString(),
6572 HasSubstr("Sequence of components starting with 4 "
6573 "and ending with 4 gets larger than 3"));
6574 }
6575
6576 TEST_F(ValidateDecorations, ComponentDecorationVector3GoodVulkan) {
6577 const spv_target_env env = SPV_ENV_VULKAN_1_0;
6578 std::string spirv = ShaderWithComponentDecoration("v3float", "Component 1");
6579
6580 CompileSuccessfully(spirv, env);
6581 EXPECT_EQ(SPV_SUCCESS, ValidateAndRetrieveValidationState(env));
6582 EXPECT_THAT(getDiagnosticString(), Eq(""));
6583 }
6584
6585 TEST_F(ValidateDecorations, ComponentDecorationVector4GoodVulkan) {
6586 const spv_target_env env = SPV_ENV_VULKAN_1_0;
6587 std::string spirv = ShaderWithComponentDecoration("v4float", "Component 0");
6588
6589 CompileSuccessfully(spirv, env);
6590 EXPECT_EQ(SPV_SUCCESS, ValidateAndRetrieveValidationState(env));
6591 EXPECT_THAT(getDiagnosticString(), Eq(""));
6592 }
6593
6594 TEST_F(ValidateDecorations, ComponentDecorationVector4Bad1Vulkan) {
6595 const spv_target_env env = SPV_ENV_VULKAN_1_0;
6596 std::string spirv = ShaderWithComponentDecoration("v4float", "Component 1");
6597
6598 CompileSuccessfully(spirv, env);
6599 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateAndRetrieveValidationState(env));
6600 EXPECT_THAT(getDiagnosticString(),
6601 HasSubstr("Sequence of components starting with 1 "
6602 "and ending with 4 gets larger than 3"));
6603 }
6604
6605 TEST_F(ValidateDecorations, ComponentDecorationVector4Bad3Vulkan) {
6606 const spv_target_env env = SPV_ENV_VULKAN_1_0;
6607 std::string spirv = ShaderWithComponentDecoration("v4float", "Component 3");
6608
6609 CompileSuccessfully(spirv, env);
6610 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateAndRetrieveValidationState(env));
6611 EXPECT_THAT(getDiagnosticString(),
6612 HasSubstr("Sequence of components starting with 3 "
6613 "and ending with 6 gets larger than 3"));
6614 }
6615
6616 TEST_F(ValidateDecorations, ComponentDecorationArrayGoodVulkan) {
6617 const spv_target_env env = SPV_ENV_VULKAN_1_0;
6618 std::string spirv =
6619 ShaderWithComponentDecoration("arr_v3float_uint_2", "Component 1");
6620
6621 CompileSuccessfully(spirv, env);
6622 EXPECT_EQ(SPV_SUCCESS, ValidateAndRetrieveValidationState(env));
6623 EXPECT_THAT(getDiagnosticString(), Eq(""));
6624 }
6625
6626 TEST_F(ValidateDecorations, ComponentDecorationArrayBadVulkan) {
6627 const spv_target_env env = SPV_ENV_VULKAN_1_0;
6628 std::string spirv =
6629 ShaderWithComponentDecoration("arr_v3float_uint_2", "Component 2");
6630
6631 CompileSuccessfully(spirv, env);
6632 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateAndRetrieveValidationState(env));
6633 EXPECT_THAT(getDiagnosticString(),
6634 HasSubstr("Sequence of components starting with 2 "
6635 "and ending with 4 gets larger than 3"));
6636 }
6637
6638 TEST_F(ValidateDecorations, ComponentDecorationBlockGood) {
6639 std::string spirv = R"(
6640 OpCapability Shader
6641 OpMemoryModel Logical GLSL450
6642 OpEntryPoint Fragment %4 "main" %9 %12
6643 OpExecutionMode %4 OriginUpperLeft
6644 OpDecorate %9 Location 0
6645 OpMemberDecorate %block 0 Location 2
6646 OpMemberDecorate %block 0 Component 1
6647 OpDecorate %block Block
6648 %2 = OpTypeVoid
6649 %3 = OpTypeFunction %2
6650 %float = OpTypeFloat 32
6651 %vec3 = OpTypeVector %float 3
6652 %8 = OpTypePointer Output %vec3
6653 %9 = OpVariable %8 Output
6654 %block = OpTypeStruct %vec3
6655 %11 = OpTypePointer Input %block
6656 %12 = OpVariable %11 Input
6657 %int = OpTypeInt 32 1
6658 %14 = OpConstant %int 0
6659 %15 = OpTypePointer Input %vec3
6660 %4 = OpFunction %2 None %3
6661 %5 = OpLabel
6662 %16 = OpAccessChain %15 %12 %14
6663 %17 = OpLoad %vec3 %16
6664 OpStore %9 %17
6665 OpReturn
6666 OpFunctionEnd
6667 )";
6668
6669 CompileSuccessfully(spirv);
6670 EXPECT_EQ(SPV_SUCCESS, ValidateAndRetrieveValidationState());
6671 EXPECT_THAT(getDiagnosticString(), Eq(""));
6672 }
6673
6674 TEST_F(ValidateDecorations, ComponentDecorationBlockBadVulkan) {
6675 const spv_target_env env = SPV_ENV_VULKAN_1_0;
6676 std::string spirv = R"(
6677 OpCapability Shader
6678 OpMemoryModel Logical GLSL450
6679 OpEntryPoint Fragment %4 "main" %9 %12
6680 OpExecutionMode %4 OriginUpperLeft
6681 OpDecorate %9 Location 0
6682 OpMemberDecorate %block 0 Location 2
6683 OpMemberDecorate %block 0 Component 2
6684 OpDecorate %block Block
6685 %2 = OpTypeVoid
6686 %3 = OpTypeFunction %2
6687 %float = OpTypeFloat 32
6688 %vec3 = OpTypeVector %float 3
6689 %8 = OpTypePointer Output %vec3
6690 %9 = OpVariable %8 Output
6691 %block = OpTypeStruct %vec3
6692 %11 = OpTypePointer Input %block
6693 %12 = OpVariable %11 Input
6694 %int = OpTypeInt 32 1
6695 %14 = OpConstant %int 0
6696 %15 = OpTypePointer Input %vec3
6697 %4 = OpFunction %2 None %3
6698 %5 = OpLabel
6699 %16 = OpAccessChain %15 %12 %14
6700 %17 = OpLoad %vec3 %16
6701 OpStore %9 %17
6702 OpReturn
6703 OpFunctionEnd
6704 )";
6705
6706 CompileSuccessfully(spirv, env);
6707 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateAndRetrieveValidationState(env));
6708 EXPECT_THAT(getDiagnosticString(),
6709 HasSubstr("Sequence of components starting with 2 "
6710 "and ending with 4 gets larger than 3"));
6711 }
6712
6713 TEST_F(ValidateDecorations, ComponentDecorationFunctionParameter) {
6714 std::string spirv = R"(
6715 OpCapability Shader
6716 OpMemoryModel Logical GLSL450
6717 OpEntryPoint Vertex %main "main"
6718
6719 OpDecorate %param_f Component 0
6720
6721 %void = OpTypeVoid
6722 %void_fn = OpTypeFunction %void
6723 %float = OpTypeFloat 32
6724 %float_0 = OpConstant %float 0
6725 %int = OpTypeInt 32 0
6726 %int_2 = OpConstant %int 2
6727 %struct_b = OpTypeStruct %float
6728
6729 %extra_fn = OpTypeFunction %void %float
6730
6731 %helper = OpFunction %void None %extra_fn
6732 %param_f = OpFunctionParameter %float
6733 %helper_label = OpLabel
6734 OpReturn
6735 OpFunctionEnd
6736
6737 %main = OpFunction %void None %void_fn
6738 %label = OpLabel
6739 OpReturn
6740 OpFunctionEnd
6741 )";
6742
6743 CompileSuccessfully(spirv);
6744 EXPECT_EQ(SPV_SUCCESS, ValidateAndRetrieveValidationState());
6745 EXPECT_THAT(getDiagnosticString(), Eq(""));
6746 }
6747
6748 TEST_F(ValidateDecorations, VulkanStorageBufferBlock) {
6749 const std::string spirv = R"(
6750 OpCapability Shader
6751 OpExtension "SPV_KHR_storage_buffer_storage_class"
6752 OpMemoryModel Logical GLSL450
6753 OpEntryPoint GLCompute %main "main"
6754 OpExecutionMode %main LocalSize 1 1 1
6755 OpDecorate %struct Block
6756 OpMemberDecorate %struct 0 Offset 0
6757 %void = OpTypeVoid
6758 %uint = OpTypeInt 32 0
6759 %struct = OpTypeStruct %uint
6760 %ptr_ssbo = OpTypePointer StorageBuffer %struct
6761 %var = OpVariable %ptr_ssbo StorageBuffer
6762 %void_fn = OpTypeFunction %void
6763 %main = OpFunction %void None %void_fn
6764 %entry = OpLabel
6765 OpReturn
6766 OpFunctionEnd
6767 )";
6768
6769 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_0);
6770 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_VULKAN_1_0));
6771 }
6772
6773 TEST_F(ValidateDecorations, VulkanStorageBufferMissingBlock) {
6774 const std::string spirv = R"(
6775 OpCapability Shader
6776 OpExtension "SPV_KHR_storage_buffer_storage_class"
6777 OpMemoryModel Logical GLSL450
6778 OpEntryPoint GLCompute %main "main"
6779 OpExecutionMode %main LocalSize 1 1 1
6780 %void = OpTypeVoid
6781 %uint = OpTypeInt 32 0
6782 %struct = OpTypeStruct %uint
6783 %ptr_ssbo = OpTypePointer StorageBuffer %struct
6784 %var = OpVariable %ptr_ssbo StorageBuffer
6785 %void_fn = OpTypeFunction %void
6786 %main = OpFunction %void None %void_fn
6787 %entry = OpLabel
6788 OpReturn
6789 OpFunctionEnd
6790 )";
6791
6792 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_0);
6793 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_0));
6794 EXPECT_THAT(getDiagnosticString(),
6795 HasSubstr("From Vulkan spec, section 14.5.2:\nSuch variables "
6796 "must be identified with a Block decoration"));
6797 }
6798
6799 TEST_F(ValidateDecorations, VulkanStorageBufferArrayMissingBlock) {
6800 const std::string spirv = R"(
6801 OpCapability Shader
6802 OpExtension "SPV_KHR_storage_buffer_storage_class"
6803 OpMemoryModel Logical GLSL450
6804 OpEntryPoint GLCompute %main "main"
6805 OpExecutionMode %main LocalSize 1 1 1
6806 %void = OpTypeVoid
6807 %uint = OpTypeInt 32 0
6808 %uint_4 = OpConstant %uint 4
6809 %struct = OpTypeStruct %uint
6810 %array = OpTypeArray %struct %uint_4
6811 %ptr_ssbo = OpTypePointer StorageBuffer %array
6812 %var = OpVariable %ptr_ssbo StorageBuffer
6813 %void_fn = OpTypeFunction %void
6814 %main = OpFunction %void None %void_fn
6815 %entry = OpLabel
6816 OpReturn
6817 OpFunctionEnd
6818 )";
6819
6820 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_0);
6821 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_0));
6822 EXPECT_THAT(getDiagnosticString(),
6823 HasSubstr("From Vulkan spec, section 14.5.2:\nSuch variables "
6824 "must be identified with a Block decoration"));
6825 }
6826
6827 TEST_F(ValidateDecorations, VulkanStorageBufferRuntimeArrayMissingBlock) {
6828 const std::string spirv = R"(
6829 OpCapability Shader
6830 OpCapability RuntimeDescriptorArrayEXT
6831 OpExtension "SPV_EXT_descriptor_indexing"
6832 OpExtension "SPV_KHR_storage_buffer_storage_class"
6833 OpMemoryModel Logical GLSL450
6834 OpEntryPoint GLCompute %main "main"
6835 OpExecutionMode %main LocalSize 1 1 1
6836 %void = OpTypeVoid
6837 %uint = OpTypeInt 32 0
6838 %struct = OpTypeStruct %uint
6839 %array = OpTypeRuntimeArray %struct
6840 %ptr_ssbo = OpTypePointer StorageBuffer %array
6841 %var = OpVariable %ptr_ssbo StorageBuffer
6842 %void_fn = OpTypeFunction %void
6843 %main = OpFunction %void None %void_fn
6844 %entry = OpLabel
6845 OpReturn
6846 OpFunctionEnd
6847 )";
6848
6849 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_0);
6850 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_0));
6851 EXPECT_THAT(getDiagnosticString(),
6852 HasSubstr("From Vulkan spec, section 14.5.2:\nSuch variables "
6853 "must be identified with a Block decoration"));
6854 }
6855
6856 TEST_F(ValidateDecorations, VulkanUniformBlock) {
6857 const std::string spirv = R"(
6858 OpCapability Shader
6859 OpMemoryModel Logical GLSL450
6860 OpEntryPoint GLCompute %main "main"
6861 OpExecutionMode %main LocalSize 1 1 1
6862 OpDecorate %struct Block
6863 OpMemberDecorate %struct 0 Offset 0
6864 %void = OpTypeVoid
6865 %uint = OpTypeInt 32 0
6866 %struct = OpTypeStruct %uint
6867 %ptr_ubo = OpTypePointer Uniform %struct
6868 %var = OpVariable %ptr_ubo Uniform
6869 %void_fn = OpTypeFunction %void
6870 %main = OpFunction %void None %void_fn
6871 %entry = OpLabel
6872 OpReturn
6873 OpFunctionEnd
6874 )";
6875
6876 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_0);
6877 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_VULKAN_1_0));
6878 }
6879
6880 TEST_F(ValidateDecorations, VulkanUniformBufferBlock) {
6881 const std::string spirv = R"(
6882 OpCapability Shader
6883 OpMemoryModel Logical GLSL450
6884 OpEntryPoint GLCompute %main "main"
6885 OpExecutionMode %main LocalSize 1 1 1
6886 OpDecorate %struct BufferBlock
6887 OpMemberDecorate %struct 0 Offset 0
6888 %void = OpTypeVoid
6889 %uint = OpTypeInt 32 0
6890 %struct = OpTypeStruct %uint
6891 %ptr_ubo = OpTypePointer Uniform %struct
6892 %var = OpVariable %ptr_ubo Uniform
6893 %void_fn = OpTypeFunction %void
6894 %main = OpFunction %void None %void_fn
6895 %entry = OpLabel
6896 OpReturn
6897 OpFunctionEnd
6898 )";
6899
6900 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_0);
6901 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_VULKAN_1_0));
6902 }
6903
6904 TEST_F(ValidateDecorations, VulkanUniformMissingBlock) {
6905 const std::string spirv = R"(
6906 OpCapability Shader
6907 OpMemoryModel Logical GLSL450
6908 OpEntryPoint GLCompute %main "main"
6909 OpExecutionMode %main LocalSize 1 1 1
6910 %void = OpTypeVoid
6911 %uint = OpTypeInt 32 0
6912 %struct = OpTypeStruct %uint
6913 %ptr_ubo = OpTypePointer Uniform %struct
6914 %var = OpVariable %ptr_ubo Uniform
6915 %void_fn = OpTypeFunction %void
6916 %main = OpFunction %void None %void_fn
6917 %entry = OpLabel
6918 OpReturn
6919 OpFunctionEnd
6920 )";
6921
6922 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_0);
6923 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_0));
6924 EXPECT_THAT(
6925 getDiagnosticString(),
6926 HasSubstr("From Vulkan spec, section 14.5.2:\nSuch variables must be "
6927 "identified with a Block or BufferBlock decoration"));
6928 }
6929
6930 TEST_F(ValidateDecorations, VulkanUniformArrayMissingBlock) {
6931 const std::string spirv = R"(
6932 OpCapability Shader
6933 OpMemoryModel Logical GLSL450
6934 OpEntryPoint GLCompute %main "main"
6935 OpExecutionMode %main LocalSize 1 1 1
6936 %void = OpTypeVoid
6937 %uint = OpTypeInt 32 0
6938 %uint_4 = OpConstant %uint 4
6939 %struct = OpTypeStruct %uint
6940 %array = OpTypeArray %struct %uint_4
6941 %ptr_ubo = OpTypePointer Uniform %array
6942 %var = OpVariable %ptr_ubo Uniform
6943 %void_fn = OpTypeFunction %void
6944 %main = OpFunction %void None %void_fn
6945 %entry = OpLabel
6946 OpReturn
6947 OpFunctionEnd
6948 )";
6949
6950 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_0);
6951 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_0));
6952 EXPECT_THAT(
6953 getDiagnosticString(),
6954 HasSubstr("From Vulkan spec, section 14.5.2:\nSuch variables must be "
6955 "identified with a Block or BufferBlock decoration"));
6956 }
6957
6958 TEST_F(ValidateDecorations, VulkanUniformRuntimeArrayMissingBlock) {
6959 const std::string spirv = R"(
6960 OpCapability Shader
6961 OpCapability RuntimeDescriptorArrayEXT
6962 OpExtension "SPV_EXT_descriptor_indexing"
6963 OpMemoryModel Logical GLSL450
6964 OpEntryPoint GLCompute %main "main"
6965 OpExecutionMode %main LocalSize 1 1 1
6966 %void = OpTypeVoid
6967 %uint = OpTypeInt 32 0
6968 %struct = OpTypeStruct %uint
6969 %array = OpTypeRuntimeArray %struct
6970 %ptr_ubo = OpTypePointer Uniform %array
6971 %var = OpVariable %ptr_ubo Uniform
6972 %void_fn = OpTypeFunction %void
6973 %main = OpFunction %void None %void_fn
6974 %entry = OpLabel
6975 OpReturn
6976 OpFunctionEnd
6977 )";
6978
6979 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_0);
6980 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_0));
6981 EXPECT_THAT(
6982 getDiagnosticString(),
6983 HasSubstr("From Vulkan spec, section 14.5.2:\nSuch variables must be "
6984 "identified with a Block or BufferBlock decoration"));
6985 }
6986
6987 TEST_F(ValidateDecorations, VulkanArrayStrideZero) {
6988 const std::string spirv = R"(
6989 OpCapability Shader
6990 OpMemoryModel Logical GLSL450
6991 OpEntryPoint GLCompute %main "main"
6992 OpExecutionMode %main LocalSize 1 1 1
6993 OpDecorate %var DescriptorSet 0
6994 OpDecorate %var Binding 0
6995 OpDecorate %struct Block
6996 OpMemberDecorate %struct 0 Offset 0
6997 OpDecorate %array ArrayStride 0
6998 %void = OpTypeVoid
6999 %int = OpTypeInt 32 0
7000 %int_4 = OpConstant %int 4
7001 %array = OpTypeArray %int %int_4
7002 %struct = OpTypeStruct %array
7003 %ptr_ssbo_struct = OpTypePointer StorageBuffer %struct
7004 %var = OpVariable %ptr_ssbo_struct StorageBuffer
7005 %void_fn = OpTypeFunction %void
7006 %main = OpFunction %void None %void_fn
7007 %entry = OpLabel
7008 OpReturn
7009 OpFunctionEnd
7010 )";
7011
7012 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_1);
7013 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_1));
7014 EXPECT_THAT(getDiagnosticString(),
7015 HasSubstr("contains an array with stride 0"));
7016 }
7017
7018 TEST_F(ValidateDecorations, VulkanArrayStrideTooSmall) {
7019 const std::string spirv = R"(
7020 OpCapability Shader
7021 OpMemoryModel Logical GLSL450
7022 OpEntryPoint GLCompute %main "main"
7023 OpExecutionMode %main LocalSize 1 1 1
7024 OpDecorate %var DescriptorSet 0
7025 OpDecorate %var Binding 0
7026 OpDecorate %struct Block
7027 OpMemberDecorate %struct 0 Offset 0
7028 OpDecorate %inner ArrayStride 4
7029 OpDecorate %outer ArrayStride 4
7030 %void = OpTypeVoid
7031 %int = OpTypeInt 32 0
7032 %int_4 = OpConstant %int 4
7033 %inner = OpTypeArray %int %int_4
7034 %outer = OpTypeArray %inner %int_4
7035 %struct = OpTypeStruct %outer
7036 %ptr_ssbo_struct = OpTypePointer StorageBuffer %struct
7037 %var = OpVariable %ptr_ssbo_struct StorageBuffer
7038 %void_fn = OpTypeFunction %void
7039 %main = OpFunction %void None %void_fn
7040 %entry = OpLabel
7041 OpReturn
7042 OpFunctionEnd
7043 )";
7044
7045 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_1);
7046 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_1));
7047 EXPECT_THAT(
7048 getDiagnosticString(),
7049 HasSubstr(
7050 "contains an array with stride 4, but with an element size of 16"));
7051 }
7052
7053 TEST_F(ValidateDecorations, FunctionsWithOpGroupDecorate) {
7054 std::string spirv = R"(
7055 OpCapability Addresses
7056 OpCapability Linkage
7057 OpCapability Kernel
7058 OpCapability Int8
7059 %1 = OpExtInstImport "OpenCL.std"
7060 OpMemoryModel Physical32 OpenCL
7061 OpName %foo "foo"
7062 OpName %entry "entry"
7063 OpName %bar "bar"
7064 OpName %entry_0 "entry"
7065 OpName %k "k"
7066 OpName %entry_1 "entry"
7067 OpName %b "b"
7068 OpDecorate %28 FuncParamAttr Zext
7069 %28 = OpDecorationGroup
7070 OpDecorate %k LinkageAttributes "k" Export
7071 OpDecorate %foo LinkageAttributes "foo" Export
7072 OpDecorate %bar LinkageAttributes "bar" Export
7073 OpDecorate %b Alignment 1
7074 OpGroupDecorate %28 %foo %bar
7075 %uchar = OpTypeInt 8 0
7076 %bool = OpTypeBool
7077 %3 = OpTypeFunction %bool
7078 %void = OpTypeVoid
7079 %10 = OpTypeFunction %void
7080 %_ptr_Function_uchar = OpTypePointer Function %uchar
7081 %true = OpConstantTrue %bool
7082 %foo = OpFunction %bool DontInline %3
7083 %entry = OpLabel
7084 OpReturnValue %true
7085 OpFunctionEnd
7086 %bar = OpFunction %bool DontInline %3
7087 %entry_0 = OpLabel
7088 OpReturnValue %true
7089 OpFunctionEnd
7090 %k = OpFunction %void DontInline %10
7091 %entry_1 = OpLabel
7092 %b = OpVariable %_ptr_Function_uchar Function
7093 OpReturn
7094 OpFunctionEnd
7095 )";
7096 CompileSuccessfully(spirv);
7097 EXPECT_EQ(SPV_SUCCESS, ValidateAndRetrieveValidationState());
7098 }
7099
7100 TEST_F(ValidateDecorations, LocationVariableGood) {
7101 const std::string spirv = R"(
7102 OpCapability Shader
7103 OpCapability Linkage
7104 OpMemoryModel Logical GLSL450
7105 OpDecorate %in_var Location 0
7106 %float = OpTypeFloat 32
7107 %ptr_input_float = OpTypePointer Input %float
7108 %in_var = OpVariable %ptr_input_float Input
7109 )";
7110
7111 CompileSuccessfully(spirv);
7112 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
7113 }
7114
7115 TEST_F(ValidateDecorations, LocationStructMemberGood) {
7116 const std::string spirv = R"(
7117 OpCapability Shader
7118 OpCapability Linkage
7119 OpMemoryModel Logical GLSL450
7120 OpMemberDecorate %struct 0 Location 0
7121 %float = OpTypeFloat 32
7122 %struct = OpTypeStruct %float
7123 )";
7124
7125 CompileSuccessfully(spirv);
7126 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
7127 }
7128
7129 TEST_F(ValidateDecorations, LocationStructBad) {
7130 const std::string spirv = R"(
7131 OpCapability Shader
7132 OpCapability Linkage
7133 OpMemoryModel Logical GLSL450
7134 OpDecorate %struct Location 0
7135 %float = OpTypeFloat 32
7136 %struct = OpTypeStruct %float
7137 )";
7138
7139 CompileSuccessfully(spirv);
7140 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
7141 EXPECT_THAT(getDiagnosticString(),
7142 HasSubstr("Location decoration can only be applied to a variable "
7143 "or member of a structure type"));
7144 }
7145
7146 TEST_F(ValidateDecorations, LocationFloatBad) {
7147 const std::string spirv = R"(
7148 OpCapability Shader
7149 OpCapability Linkage
7150 OpMemoryModel Logical GLSL450
7151 OpDecorate %float Location 0
7152 %float = OpTypeFloat 32
7153 )";
7154
7155 CompileSuccessfully(spirv);
7156 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
7157 EXPECT_THAT(getDiagnosticString(),
7158 HasSubstr("Location decoration can only be applied to a variable "
7159 "or member of a structure type"));
7160 }
7161
7162 } // namespace
7163 } // namespace val
7164 } // namespace spvtools