1 // Copyright (c) 2018 Google LLC.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include <sstream>
16 #include <string>
17 #include <vector>
18 
19 #include "gmock/gmock.h"
20 #include "source/spirv_target_env.h"
21 #include "test/unit_spirv.h"
22 #include "test/val/val_fixtures.h"
23 
24 namespace spvtools {
25 namespace val {
26 namespace {
27 
28 using ::testing::Combine;
29 using ::testing::HasSubstr;
30 using ::testing::Values;
31 using ::testing::ValuesIn;
32 
33 using ValidateMode = spvtest::ValidateBase<bool>;
34 
35 const std::string kVoidFunction = R"(%void = OpTypeVoid
36 %void_fn = OpTypeFunction %void
37 %main = OpFunction %void None %void_fn
38 %entry = OpLabel
39 OpReturn
40 OpFunctionEnd
41 )";
42 
TEST_F(ValidateMode, GLComputeNoMode)43 TEST_F(ValidateMode, GLComputeNoMode) {
44   const std::string spirv = R"(
45 OpCapability Shader
46 OpMemoryModel Logical GLSL450
47 OpEntryPoint GLCompute %main "main"
48 )" + kVoidFunction;
49 
50   CompileSuccessfully(spirv);
51   EXPECT_THAT(SPV_SUCCESS, ValidateInstructions());
52 }
53 
TEST_F(ValidateMode, GLComputeNoModeVulkan)54 TEST_F(ValidateMode, GLComputeNoModeVulkan) {
55   const std::string spirv = R"(
56 OpCapability Shader
57 OpMemoryModel Logical GLSL450
58 OpEntryPoint GLCompute %main "main"
59 )" + kVoidFunction;
60 
61   spv_target_env env = SPV_ENV_VULKAN_1_0;
62   CompileSuccessfully(spirv, env);
63   EXPECT_THAT(SPV_ERROR_INVALID_DATA, ValidateInstructions(env));
64   EXPECT_THAT(getDiagnosticString(),
65               AnyVUID("VUID-StandaloneSpirv-LocalSize-06426"));
66   EXPECT_THAT(
67       getDiagnosticString(),
68       HasSubstr(
69           "In the Vulkan environment, GLCompute execution model entry "
70           "points require either the LocalSize or LocalSizeId execution mode "
71           "or an object decorated with WorkgroupSize must be specified."));
72 }
73 
TEST_F(ValidateMode, GLComputeNoModeVulkanWorkgroupSize)74 TEST_F(ValidateMode, GLComputeNoModeVulkanWorkgroupSize) {
75   const std::string spirv = R"(
76 OpCapability Shader
77 OpMemoryModel Logical GLSL450
78 OpEntryPoint GLCompute %main "main"
79 OpDecorate %int3_1 BuiltIn WorkgroupSize
80 %int = OpTypeInt 32 0
81 %int3 = OpTypeVector %int 3
82 %int_1 = OpConstant %int 1
83 %int3_1 = OpConstantComposite %int3 %int_1 %int_1 %int_1
84 )" + kVoidFunction;
85 
86   spv_target_env env = SPV_ENV_VULKAN_1_0;
87   CompileSuccessfully(spirv, env);
88   EXPECT_THAT(SPV_SUCCESS, ValidateInstructions(env));
89 }
90 
TEST_F(ValidateMode, GLComputeVulkanLocalSize)91 TEST_F(ValidateMode, GLComputeVulkanLocalSize) {
92   const std::string spirv = R"(
93 OpCapability Shader
94 OpMemoryModel Logical GLSL450
95 OpEntryPoint GLCompute %main "main"
96 OpExecutionMode %main LocalSize 1 1 1
97 )" + kVoidFunction;
98 
99   spv_target_env env = SPV_ENV_VULKAN_1_0;
100   CompileSuccessfully(spirv, env);
101   EXPECT_THAT(SPV_SUCCESS, ValidateInstructions(env));
102 }
103 
TEST_F(ValidateMode, GLComputeVulkanLocalSizeIdBad)104 TEST_F(ValidateMode, GLComputeVulkanLocalSizeIdBad) {
105   const std::string spirv = R"(
106 OpCapability Shader
107 OpMemoryModel Logical GLSL450
108 OpEntryPoint GLCompute %main "main"
109 OpExecutionModeId %main LocalSizeId %int_1 %int_1 %int_1
110 %int = OpTypeInt 32 0
111 %int_1 = OpConstant %int 1
112 )" + kVoidFunction;
113 
114   spv_target_env env = SPV_ENV_VULKAN_1_1;  // need SPIR-V 1.2
115   CompileSuccessfully(spirv, env);
116   EXPECT_THAT(SPV_ERROR_INVALID_DATA, ValidateInstructions(env));
117   EXPECT_THAT(
118       getDiagnosticString(),
119       HasSubstr("LocalSizeId mode is not allowed by the current environment."));
120 }
121 
TEST_F(ValidateMode, GLComputeVulkanLocalSizeIdGood)122 TEST_F(ValidateMode, GLComputeVulkanLocalSizeIdGood) {
123   const std::string spirv = R"(
124 OpCapability Shader
125 OpMemoryModel Logical GLSL450
126 OpEntryPoint GLCompute %main "main"
127 OpExecutionModeId %main LocalSizeId %int_1 %int_1 %int_1
128 %int = OpTypeInt 32 0
129 %int_1 = OpConstant %int 1
130 )" + kVoidFunction;
131 
132   spv_target_env env = SPV_ENV_VULKAN_1_1;  // need SPIR-V 1.2
133   CompileSuccessfully(spirv, env);
134   spvValidatorOptionsSetAllowLocalSizeId(getValidatorOptions(), true);
135   EXPECT_THAT(SPV_SUCCESS, ValidateInstructions(env));
136 }
137 
TEST_F(ValidateMode, FragmentOriginLowerLeftVulkan)138 TEST_F(ValidateMode, FragmentOriginLowerLeftVulkan) {
139   const std::string spirv = R"(
140 OpCapability Shader
141 OpMemoryModel Logical GLSL450
142 OpEntryPoint Fragment %main "main"
143 OpExecutionMode %main OriginLowerLeft
144 )" + kVoidFunction;
145 
146   spv_target_env env = SPV_ENV_VULKAN_1_0;
147   CompileSuccessfully(spirv, env);
148   EXPECT_THAT(SPV_ERROR_INVALID_DATA, ValidateInstructions(env));
149   EXPECT_THAT(getDiagnosticString(),
150               AnyVUID("VUID-StandaloneSpirv-OriginLowerLeft-04653"));
151   EXPECT_THAT(getDiagnosticString(),
152               HasSubstr("In the Vulkan environment, the OriginLowerLeft "
153                         "execution mode must not be used."));
154 }
155 
TEST_F(ValidateMode, FragmentPixelCenterIntegerVulkan)156 TEST_F(ValidateMode, FragmentPixelCenterIntegerVulkan) {
157   const std::string spirv = R"(
158 OpCapability Shader
159 OpMemoryModel Logical GLSL450
160 OpEntryPoint Fragment %main "main"
161 OpExecutionMode %main OriginUpperLeft
162 OpExecutionMode %main PixelCenterInteger
163 )" + kVoidFunction;
164 
165   spv_target_env env = SPV_ENV_VULKAN_1_0;
166   CompileSuccessfully(spirv, env);
167   EXPECT_THAT(SPV_ERROR_INVALID_DATA, ValidateInstructions(env));
168   EXPECT_THAT(getDiagnosticString(),
169               AnyVUID("VUID-StandaloneSpirv-PixelCenterInteger-04654"));
170   EXPECT_THAT(getDiagnosticString(),
171               HasSubstr("In the Vulkan environment, the PixelCenterInteger "
172                         "execution mode must not be used."));
173 }
174 
TEST_F(ValidateMode, GeometryNoOutputMode)175 TEST_F(ValidateMode, GeometryNoOutputMode) {
176   const std::string spirv = R"(
177 OpCapability Geometry
178 OpMemoryModel Logical GLSL450
179 OpEntryPoint Geometry %main "main"
180 OpExecutionMode %main InputPoints
181 )" + kVoidFunction;
182 
183   CompileSuccessfully(spirv);
184   EXPECT_THAT(SPV_ERROR_INVALID_DATA, ValidateInstructions());
185   EXPECT_THAT(getDiagnosticString(),
186               HasSubstr("Geometry execution model entry points must specify "
187                         "exactly one of OutputPoints, OutputLineStrip or "
188                         "OutputTriangleStrip execution modes."));
189 }
190 
TEST_F(ValidateMode, GeometryNoInputMode)191 TEST_F(ValidateMode, GeometryNoInputMode) {
192   const std::string spirv = R"(
193 OpCapability Geometry
194 OpMemoryModel Logical GLSL450
195 OpEntryPoint Geometry %main "main"
196 OpExecutionMode %main OutputPoints
197 )" + kVoidFunction;
198 
199   CompileSuccessfully(spirv);
200   EXPECT_THAT(SPV_ERROR_INVALID_DATA, ValidateInstructions());
201   EXPECT_THAT(
202       getDiagnosticString(),
203       HasSubstr("Geometry execution model entry points must specify exactly "
204                 "one of InputPoints, InputLines, InputLinesAdjacency, "
205                 "Triangles or InputTrianglesAdjacency execution modes."));
206 }
207 
TEST_F(ValidateMode, FragmentNoOrigin)208 TEST_F(ValidateMode, FragmentNoOrigin) {
209   const std::string spirv = R"(
210 OpCapability Shader
211 OpMemoryModel Logical GLSL450
212 OpEntryPoint Fragment %main "main"
213 )" + kVoidFunction;
214 
215   CompileSuccessfully(spirv);
216   EXPECT_THAT(SPV_ERROR_INVALID_DATA, ValidateInstructions());
217   EXPECT_THAT(
218       getDiagnosticString(),
219       HasSubstr("Fragment execution model entry points require either an "
220                 "OriginUpperLeft or OriginLowerLeft execution mode."));
221 }
222 
TEST_F(ValidateMode, FragmentBothOrigins)223 TEST_F(ValidateMode, FragmentBothOrigins) {
224   const std::string spirv = R"(
225 OpCapability Shader
226 OpMemoryModel Logical GLSL450
227 OpEntryPoint Fragment %main "main"
228 OpExecutionMode %main OriginUpperLeft
229 OpExecutionMode %main OriginLowerLeft
230 )" + kVoidFunction;
231 
232   CompileSuccessfully(spirv);
233   EXPECT_THAT(SPV_ERROR_INVALID_DATA, ValidateInstructions());
234   EXPECT_THAT(
235       getDiagnosticString(),
236       HasSubstr("Fragment execution model entry points can only specify one of "
237                 "OriginUpperLeft or OriginLowerLeft execution modes."));
238 }
239 
TEST_F(ValidateMode, FragmentDepthGreaterAndLess)240 TEST_F(ValidateMode, FragmentDepthGreaterAndLess) {
241   const std::string spirv = R"(
242 OpCapability Shader
243 OpMemoryModel Logical GLSL450
244 OpEntryPoint Fragment %main "main"
245 OpExecutionMode %main OriginUpperLeft
246 OpExecutionMode %main DepthGreater
247 OpExecutionMode %main DepthLess
248 )" + kVoidFunction;
249 
250   CompileSuccessfully(spirv);
251   EXPECT_THAT(SPV_ERROR_INVALID_DATA, ValidateInstructions());
252   EXPECT_THAT(getDiagnosticString(),
253               HasSubstr("Fragment execution model entry points can specify at "
254                         "most one of DepthGreater, DepthLess or DepthUnchanged "
255                         "execution modes."));
256 }
257 
TEST_F(ValidateMode, FragmentDepthGreaterAndUnchanged)258 TEST_F(ValidateMode, FragmentDepthGreaterAndUnchanged) {
259   const std::string spirv = R"(
260 OpCapability Shader
261 OpMemoryModel Logical GLSL450
262 OpEntryPoint Fragment %main "main"
263 OpExecutionMode %main OriginUpperLeft
264 OpExecutionMode %main DepthGreater
265 OpExecutionMode %main DepthUnchanged
266 )" + kVoidFunction;
267 
268   CompileSuccessfully(spirv);
269   EXPECT_THAT(SPV_ERROR_INVALID_DATA, ValidateInstructions());
270   EXPECT_THAT(getDiagnosticString(),
271               HasSubstr("Fragment execution model entry points can specify at "
272                         "most one of DepthGreater, DepthLess or DepthUnchanged "
273                         "execution modes."));
274 }
275 
TEST_F(ValidateMode, FragmentDepthLessAndUnchanged)276 TEST_F(ValidateMode, FragmentDepthLessAndUnchanged) {
277   const std::string spirv = R"(
278 OpCapability Shader
279 OpMemoryModel Logical GLSL450
280 OpEntryPoint Fragment %main "main"
281 OpExecutionMode %main OriginUpperLeft
282 OpExecutionMode %main DepthLess
283 OpExecutionMode %main DepthUnchanged
284 )" + kVoidFunction;
285 
286   CompileSuccessfully(spirv);
287   EXPECT_THAT(SPV_ERROR_INVALID_DATA, ValidateInstructions());
288   EXPECT_THAT(getDiagnosticString(),
289               HasSubstr("Fragment execution model entry points can specify at "
290                         "most one of DepthGreater, DepthLess or DepthUnchanged "
291                         "execution modes."));
292 }
293 
TEST_F(ValidateMode, FragmentAllDepths)294 TEST_F(ValidateMode, FragmentAllDepths) {
295   const std::string spirv = R"(
296 OpCapability Shader
297 OpMemoryModel Logical GLSL450
298 OpEntryPoint Fragment %main "main"
299 OpExecutionMode %main OriginUpperLeft
300 OpExecutionMode %main DepthGreater
301 OpExecutionMode %main DepthLess
302 OpExecutionMode %main DepthUnchanged
303 )" + kVoidFunction;
304 
305   CompileSuccessfully(spirv);
306   EXPECT_THAT(SPV_ERROR_INVALID_DATA, ValidateInstructions());
307   EXPECT_THAT(getDiagnosticString(),
308               HasSubstr("Fragment execution model entry points can specify at "
309                         "most one of DepthGreater, DepthLess or DepthUnchanged "
310                         "execution modes."));
311 }
312 
TEST_F(ValidateMode, TessellationControlSpacingEqualAndFractionalOdd)313 TEST_F(ValidateMode, TessellationControlSpacingEqualAndFractionalOdd) {
314   const std::string spirv = R"(
315 OpCapability Tessellation
316 OpMemoryModel Logical GLSL450
317 OpEntryPoint TessellationControl %main "main"
318 OpExecutionMode %main SpacingEqual
319 OpExecutionMode %main SpacingFractionalOdd
320 )" + kVoidFunction;
321 
322   CompileSuccessfully(spirv);
323   EXPECT_THAT(SPV_ERROR_INVALID_DATA, ValidateInstructions());
324   EXPECT_THAT(getDiagnosticString(),
325               HasSubstr("Tessellation execution model entry points can specify "
326                         "at most one of SpacingEqual, SpacingFractionalOdd or "
327                         "SpacingFractionalEven execution modes."));
328 }
329 
TEST_F(ValidateMode, TessellationControlSpacingEqualAndSpacingFractionalEven)330 TEST_F(ValidateMode, TessellationControlSpacingEqualAndSpacingFractionalEven) {
331   const std::string spirv = R"(
332 OpCapability Tessellation
333 OpMemoryModel Logical GLSL450
334 OpEntryPoint TessellationControl %main "main"
335 OpExecutionMode %main SpacingEqual
336 OpExecutionMode %main SpacingFractionalEven
337 )" + kVoidFunction;
338 
339   CompileSuccessfully(spirv);
340   EXPECT_THAT(SPV_ERROR_INVALID_DATA, ValidateInstructions());
341   EXPECT_THAT(getDiagnosticString(),
342               HasSubstr("Tessellation execution model entry points can specify "
343                         "at most one of SpacingEqual, SpacingFractionalOdd or "
344                         "SpacingFractionalEven execution modes."));
345 }
346 
TEST_F(ValidateMode, TessellationControlSpacingFractionalOddAndSpacingFractionalEven)347 TEST_F(ValidateMode,
348        TessellationControlSpacingFractionalOddAndSpacingFractionalEven) {
349   const std::string spirv = R"(
350 OpCapability Tessellation
351 OpMemoryModel Logical GLSL450
352 OpEntryPoint TessellationControl %main "main"
353 OpExecutionMode %main SpacingFractionalOdd
354 OpExecutionMode %main SpacingFractionalEven
355 )" + kVoidFunction;
356 
357   CompileSuccessfully(spirv);
358   EXPECT_THAT(SPV_ERROR_INVALID_DATA, ValidateInstructions());
359   EXPECT_THAT(getDiagnosticString(),
360               HasSubstr("Tessellation execution model entry points can specify "
361                         "at most one of SpacingEqual, SpacingFractionalOdd or "
362                         "SpacingFractionalEven execution modes."));
363 }
364 
TEST_F(ValidateMode, TessellationControlAllSpacing)365 TEST_F(ValidateMode, TessellationControlAllSpacing) {
366   const std::string spirv = R"(
367 OpCapability Tessellation
368 OpMemoryModel Logical GLSL450
369 OpEntryPoint TessellationControl %main "main"
370 OpExecutionMode %main SpacingEqual
371 OpExecutionMode %main SpacingFractionalOdd
372 OpExecutionMode %main SpacingFractionalEven
373 )" + kVoidFunction;
374 
375   CompileSuccessfully(spirv);
376   EXPECT_THAT(SPV_ERROR_INVALID_DATA, ValidateInstructions());
377   EXPECT_THAT(getDiagnosticString(),
378               HasSubstr("Tessellation execution model entry points can specify "
379                         "at most one of SpacingEqual, SpacingFractionalOdd or "
380                         "SpacingFractionalEven execution modes."));
381 }
382 
TEST_F(ValidateMode, TessellationEvaluationSpacingEqualAndSpacingFractionalOdd)383 TEST_F(ValidateMode,
384        TessellationEvaluationSpacingEqualAndSpacingFractionalOdd) {
385   const std::string spirv = R"(
386 OpCapability Tessellation
387 OpMemoryModel Logical GLSL450
388 OpEntryPoint TessellationEvaluation %main "main"
389 OpExecutionMode %main SpacingEqual
390 OpExecutionMode %main SpacingFractionalOdd
391 )" + kVoidFunction;
392 
393   CompileSuccessfully(spirv);
394   EXPECT_THAT(SPV_ERROR_INVALID_DATA, ValidateInstructions());
395   EXPECT_THAT(getDiagnosticString(),
396               HasSubstr("Tessellation execution model entry points can specify "
397                         "at most one of SpacingEqual, SpacingFractionalOdd or "
398                         "SpacingFractionalEven execution modes."));
399 }
400 
TEST_F(ValidateMode, TessellationEvaluationSpacingEqualAndSpacingFractionalEven)401 TEST_F(ValidateMode,
402        TessellationEvaluationSpacingEqualAndSpacingFractionalEven) {
403   const std::string spirv = R"(
404 OpCapability Tessellation
405 OpMemoryModel Logical GLSL450
406 OpEntryPoint TessellationEvaluation %main "main"
407 OpExecutionMode %main SpacingEqual
408 OpExecutionMode %main SpacingFractionalEven
409 )" + kVoidFunction;
410 
411   CompileSuccessfully(spirv);
412   EXPECT_THAT(SPV_ERROR_INVALID_DATA, ValidateInstructions());
413   EXPECT_THAT(getDiagnosticString(),
414               HasSubstr("Tessellation execution model entry points can specify "
415                         "at most one of SpacingEqual, SpacingFractionalOdd or "
416                         "SpacingFractionalEven execution modes."));
417 }
418 
TEST_F(ValidateMode, TessellationEvaluationSpacingFractionalOddAndSpacingFractionalEven)419 TEST_F(ValidateMode,
420        TessellationEvaluationSpacingFractionalOddAndSpacingFractionalEven) {
421   const std::string spirv = R"(
422 OpCapability Tessellation
423 OpMemoryModel Logical GLSL450
424 OpEntryPoint TessellationEvaluation %main "main"
425 OpExecutionMode %main SpacingFractionalOdd
426 OpExecutionMode %main SpacingFractionalEven
427 )" + kVoidFunction;
428 
429   CompileSuccessfully(spirv);
430   EXPECT_THAT(SPV_ERROR_INVALID_DATA, ValidateInstructions());
431   EXPECT_THAT(getDiagnosticString(),
432               HasSubstr("Tessellation execution model entry points can specify "
433                         "at most one of SpacingEqual, SpacingFractionalOdd or "
434                         "SpacingFractionalEven execution modes."));
435 }
436 
TEST_F(ValidateMode, TessellationEvaluationAllSpacing)437 TEST_F(ValidateMode, TessellationEvaluationAllSpacing) {
438   const std::string spirv = R"(
439 OpCapability Tessellation
440 OpMemoryModel Logical GLSL450
441 OpEntryPoint TessellationEvaluation %main "main"
442 OpExecutionMode %main SpacingEqual
443 OpExecutionMode %main SpacingFractionalOdd
444 OpExecutionMode %main SpacingFractionalEven
445 )" + kVoidFunction;
446 
447   CompileSuccessfully(spirv);
448   EXPECT_THAT(SPV_ERROR_INVALID_DATA, ValidateInstructions());
449   EXPECT_THAT(getDiagnosticString(),
450               HasSubstr("Tessellation execution model entry points can specify "
451                         "at most one of SpacingEqual, SpacingFractionalOdd or "
452                         "SpacingFractionalEven execution modes."));
453 }
454 
TEST_F(ValidateMode, TessellationControlBothVertex)455 TEST_F(ValidateMode, TessellationControlBothVertex) {
456   const std::string spirv = R"(
457 OpCapability Tessellation
458 OpMemoryModel Logical GLSL450
459 OpEntryPoint TessellationControl %main "main"
460 OpExecutionMode %main VertexOrderCw
461 OpExecutionMode %main VertexOrderCcw
462 )" + kVoidFunction;
463 
464   CompileSuccessfully(spirv);
465   EXPECT_THAT(SPV_ERROR_INVALID_DATA, ValidateInstructions());
466   EXPECT_THAT(
467       getDiagnosticString(),
468       HasSubstr("Tessellation execution model entry points can specify at most "
469                 "one of VertexOrderCw or VertexOrderCcw execution modes."));
470 }
471 
TEST_F(ValidateMode, TessellationEvaluationBothVertex)472 TEST_F(ValidateMode, TessellationEvaluationBothVertex) {
473   const std::string spirv = R"(
474 OpCapability Tessellation
475 OpMemoryModel Logical GLSL450
476 OpEntryPoint TessellationEvaluation %main "main"
477 OpExecutionMode %main VertexOrderCw
478 OpExecutionMode %main VertexOrderCcw
479 )" + kVoidFunction;
480 
481   CompileSuccessfully(spirv);
482   EXPECT_THAT(SPV_ERROR_INVALID_DATA, ValidateInstructions());
483   EXPECT_THAT(
484       getDiagnosticString(),
485       HasSubstr("Tessellation execution model entry points can specify at most "
486                 "one of VertexOrderCw or VertexOrderCcw execution modes."));
487 }
488 
489 using ValidateModeGeometry = spvtest::ValidateBase<std::tuple<
490     std::tuple<std::string, std::string, std::string, std::string, std::string>,
491     std::tuple<std::string, std::string, std::string>>>;
492 
TEST_P(ValidateModeGeometry, ExecutionMode)493 TEST_P(ValidateModeGeometry, ExecutionMode) {
494   std::vector<std::string> input_modes;
495   std::vector<std::string> output_modes;
496   input_modes.push_back(std::get<0>(std::get<0>(GetParam())));
497   input_modes.push_back(std::get<1>(std::get<0>(GetParam())));
498   input_modes.push_back(std::get<2>(std::get<0>(GetParam())));
499   input_modes.push_back(std::get<3>(std::get<0>(GetParam())));
500   input_modes.push_back(std::get<4>(std::get<0>(GetParam())));
501   output_modes.push_back(std::get<0>(std::get<1>(GetParam())));
502   output_modes.push_back(std::get<1>(std::get<1>(GetParam())));
503   output_modes.push_back(std::get<2>(std::get<1>(GetParam())));
504 
505   std::ostringstream sstr;
506   sstr << "OpCapability Geometry\n";
507   sstr << "OpMemoryModel Logical GLSL450\n";
508   sstr << "OpEntryPoint Geometry %main \"main\"\n";
509   size_t num_input_modes = 0;
510   for (auto input : input_modes) {
511     if (!input.empty()) {
512       num_input_modes++;
513       sstr << "OpExecutionMode %main " << input << "\n";
514     }
515   }
516   size_t num_output_modes = 0;
517   for (auto output : output_modes) {
518     if (!output.empty()) {
519       num_output_modes++;
520       sstr << "OpExecutionMode %main " << output << "\n";
521     }
522   }
523   sstr << "%void = OpTypeVoid\n";
524   sstr << "%void_fn = OpTypeFunction %void\n";
525   sstr << "%int = OpTypeInt 32 0\n";
526   sstr << "%int1 = OpConstant %int 1\n";
527   sstr << "%main = OpFunction %void None %void_fn\n";
528   sstr << "%entry = OpLabel\n";
529   sstr << "OpReturn\n";
530   sstr << "OpFunctionEnd\n";
531 
532   CompileSuccessfully(sstr.str());
533   if (num_input_modes == 1 && num_output_modes == 1) {
534     EXPECT_THAT(SPV_SUCCESS, ValidateInstructions());
535   } else {
536     EXPECT_THAT(SPV_ERROR_INVALID_DATA, ValidateInstructions());
537     if (num_input_modes != 1) {
538       EXPECT_THAT(getDiagnosticString(),
539                   HasSubstr("Geometry execution model entry points must "
540                             "specify exactly one of InputPoints, InputLines, "
541                             "InputLinesAdjacency, Triangles or "
542                             "InputTrianglesAdjacency execution modes."));
543     } else {
544       EXPECT_THAT(
545           getDiagnosticString(),
546           HasSubstr("Geometry execution model entry points must specify "
547                     "exactly one of OutputPoints, OutputLineStrip or "
548                     "OutputTriangleStrip execution modes."));
549     }
550   }
551 }
552 
553 INSTANTIATE_TEST_SUITE_P(
554     GeometryRequiredModes, ValidateModeGeometry,
555     Combine(Combine(Values("InputPoints", ""), Values("InputLines", ""),
556                     Values("InputLinesAdjacency", ""), Values("Triangles", ""),
557                     Values("InputTrianglesAdjacency", "")),
558             Combine(Values("OutputPoints", ""), Values("OutputLineStrip", ""),
559                     Values("OutputTriangleStrip", ""))));
560 
561 using ValidateModeExecution =
562     spvtest::ValidateBase<std::tuple<spv_result_t, std::string, std::string,
563                                      std::string, spv_target_env>>;
564 
TEST_P(ValidateModeExecution, ExecutionMode)565 TEST_P(ValidateModeExecution, ExecutionMode) {
566   const spv_result_t expectation = std::get<0>(GetParam());
567   const std::string error = std::get<1>(GetParam());
568   const std::string model = std::get<2>(GetParam());
569   const std::string mode = std::get<3>(GetParam());
570   const spv_target_env env = std::get<4>(GetParam());
571 
572   std::ostringstream sstr;
573   sstr << "OpCapability Shader\n";
574   sstr << "OpCapability Geometry\n";
575   sstr << "OpCapability Tessellation\n";
576   sstr << "OpCapability TransformFeedback\n";
577   if (!spvIsVulkanEnv(env)) {
578     sstr << "OpCapability Kernel\n";
579     if (env == SPV_ENV_UNIVERSAL_1_3) {
580       sstr << "OpCapability SubgroupDispatch\n";
581     } else if (env == SPV_ENV_UNIVERSAL_1_5) {
582       sstr << "OpCapability TileImageColorReadAccessEXT\n";
583       sstr << "OpCapability TileImageDepthReadAccessEXT\n";
584       sstr << "OpCapability TileImageStencilReadAccessEXT\n";
585       sstr << "OpExtension \"SPV_EXT_shader_tile_image\"\n";
586     }
587   }
588   sstr << "OpMemoryModel Logical GLSL450\n";
589   sstr << "OpEntryPoint " << model << " %main \"main\"\n";
590   if (mode.find("LocalSizeId") == 0 || mode.find("LocalSizeHintId") == 0 ||
591       mode.find("SubgroupsPerWorkgroupId") == 0) {
592     sstr << "OpExecutionModeId %main " << mode << "\n";
593   } else {
594     sstr << "OpExecutionMode %main " << mode << "\n";
595   }
596   if (model == "Geometry") {
597     if (!(mode.find("InputPoints") == 0 || mode.find("InputLines") == 0 ||
598           mode.find("InputLinesAdjacency") == 0 ||
599           mode.find("Triangles") == 0 ||
600           mode.find("InputTrianglesAdjacency") == 0)) {
601       // Exactly one of the above modes is required for Geometry shaders.
602       sstr << "OpExecutionMode %main InputPoints\n";
603     }
604     if (!(mode.find("OutputPoints") == 0 || mode.find("OutputLineStrip") == 0 ||
605           mode.find("OutputTriangleStrip") == 0)) {
606       // Exactly one of the above modes is required for Geometry shaders.
607       sstr << "OpExecutionMode %main OutputPoints\n";
608     }
609   } else if (model == "Fragment") {
610     if (!(mode.find("OriginUpperLeft") == 0 ||
611           mode.find("OriginLowerLeft") == 0)) {
612       // Exactly one of the above modes is required for Fragment shaders.
613       sstr << "OpExecutionMode %main OriginUpperLeft\n";
614     }
615   }
616   sstr << "%void = OpTypeVoid\n";
617   sstr << "%void_fn = OpTypeFunction %void\n";
618   sstr << "%int = OpTypeInt 32 0\n";
619   sstr << "%int1 = OpConstant %int 1\n";
620   sstr << "%main = OpFunction %void None %void_fn\n";
621   sstr << "%entry = OpLabel\n";
622   sstr << "OpReturn\n";
623   sstr << "OpFunctionEnd\n";
624 
625   CompileSuccessfully(sstr.str(), env);
626   EXPECT_THAT(expectation, ValidateInstructions(env));
627   if (expectation != SPV_SUCCESS) {
628     EXPECT_THAT(getDiagnosticString(), HasSubstr(error));
629   }
630 }
631 
632 INSTANTIATE_TEST_SUITE_P(
633     ValidateModeGeometryOnlyGoodSpv10, ValidateModeExecution,
634     Combine(Values(SPV_SUCCESS), Values(""), Values("Geometry"),
635             Values("Invocations 3", "InputPoints", "InputLines",
636                    "InputLinesAdjacency", "InputTrianglesAdjacency",
637                    "OutputPoints", "OutputLineStrip", "OutputTriangleStrip"),
638             Values(SPV_ENV_UNIVERSAL_1_0)));
639 
640 INSTANTIATE_TEST_SUITE_P(
641     ValidateModeGeometryOnlyBadSpv10, ValidateModeExecution,
642     Combine(Values(SPV_ERROR_INVALID_DATA),
643             Values("Execution mode can only be used with the Geometry "
644                    "execution model."),
645             Values("Fragment", "TessellationEvaluation", "TessellationControl",
646                    "GLCompute", "Vertex", "Kernel"),
647             Values("Invocations 3", "InputPoints", "InputLines",
648                    "InputLinesAdjacency", "InputTrianglesAdjacency",
649                    "OutputPoints", "OutputLineStrip", "OutputTriangleStrip"),
650             Values(SPV_ENV_UNIVERSAL_1_0)));
651 
652 INSTANTIATE_TEST_SUITE_P(
653     ValidateModeTessellationOnlyGoodSpv10, ValidateModeExecution,
654     Combine(Values(SPV_SUCCESS), Values(""),
655             Values("TessellationControl", "TessellationEvaluation"),
656             Values("SpacingEqual", "SpacingFractionalEven",
657                    "SpacingFractionalOdd", "VertexOrderCw", "VertexOrderCcw",
658                    "PointMode", "Quads", "Isolines"),
659             Values(SPV_ENV_UNIVERSAL_1_0)));
660 
661 INSTANTIATE_TEST_SUITE_P(
662     ValidateModeTessellationOnlyBadSpv10, ValidateModeExecution,
663     Combine(Values(SPV_ERROR_INVALID_DATA),
664             Values("Execution mode can only be used with a tessellation "
665                    "execution model."),
666             Values("Fragment", "Geometry", "GLCompute", "Vertex", "Kernel"),
667             Values("SpacingEqual", "SpacingFractionalEven",
668                    "SpacingFractionalOdd", "VertexOrderCw", "VertexOrderCcw",
669                    "PointMode", "Quads", "Isolines"),
670             Values(SPV_ENV_UNIVERSAL_1_0)));
671 
672 INSTANTIATE_TEST_SUITE_P(ValidateModeGeometryAndTessellationGoodSpv10,
673                          ValidateModeExecution,
674                          Combine(Values(SPV_SUCCESS), Values(""),
675                                  Values("TessellationControl",
676                                         "TessellationEvaluation", "Geometry"),
677                                  Values("Triangles", "OutputVertices 3"),
678                                  Values(SPV_ENV_UNIVERSAL_1_0)));
679 
680 INSTANTIATE_TEST_SUITE_P(
681     ValidateModeGeometryAndTessellationBadSpv10, ValidateModeExecution,
682     Combine(Values(SPV_ERROR_INVALID_DATA),
683             Values("Execution mode can only be used with a Geometry or "
684                    "tessellation execution model."),
685             Values("Fragment", "GLCompute", "Vertex", "Kernel"),
686             Values("Triangles", "OutputVertices 3"),
687             Values(SPV_ENV_UNIVERSAL_1_0)));
688 
689 INSTANTIATE_TEST_SUITE_P(
690     ValidateModeFragmentOnlyGoodSpv10, ValidateModeExecution,
691     Combine(Values(SPV_SUCCESS), Values(""), Values("Fragment"),
692             Values("PixelCenterInteger", "OriginUpperLeft", "OriginLowerLeft",
693                    "EarlyFragmentTests", "DepthReplacing", "DepthLess",
694                    "DepthUnchanged"),
695             Values(SPV_ENV_UNIVERSAL_1_0)));
696 
697 INSTANTIATE_TEST_SUITE_P(
698     ValidateModeFragmentOnlyBadSpv10, ValidateModeExecution,
699     Combine(Values(SPV_ERROR_INVALID_DATA),
700             Values("Execution mode can only be used with the Fragment "
701                    "execution model."),
702             Values("Geometry", "TessellationControl", "TessellationEvaluation",
703                    "GLCompute", "Vertex", "Kernel"),
704             Values("PixelCenterInteger", "OriginUpperLeft", "OriginLowerLeft",
705                    "EarlyFragmentTests", "DepthReplacing", "DepthGreater",
706                    "DepthLess", "DepthUnchanged"),
707             Values(SPV_ENV_UNIVERSAL_1_0)));
708 
709 INSTANTIATE_TEST_SUITE_P(ValidateModeFragmentOnlyGoodSpv15,
710                          ValidateModeExecution,
711                          Combine(Values(SPV_SUCCESS), Values(""),
712                                  Values("Fragment"),
713                                  Values("NonCoherentColorAttachmentReadEXT",
714                                         "NonCoherentDepthAttachmentReadEXT",
715                                         "NonCoherentStencilAttachmentReadEXT"),
716                                  Values(SPV_ENV_UNIVERSAL_1_5)));
717 
718 INSTANTIATE_TEST_SUITE_P(
719     ValidateModeFragmentOnlyBadSpv15, ValidateModeExecution,
720     Combine(Values(SPV_ERROR_INVALID_DATA),
721             Values("Execution mode can only be used with the Fragment "
722                    "execution model."),
723             Values("Geometry", "TessellationControl", "TessellationEvaluation",
724                    "GLCompute", "Vertex", "Kernel"),
725             Values("NonCoherentColorAttachmentReadEXT",
726                    "NonCoherentDepthAttachmentReadEXT",
727                    "NonCoherentStencilAttachmentReadEXT"),
728             Values(SPV_ENV_UNIVERSAL_1_5)));
729 
730 INSTANTIATE_TEST_SUITE_P(ValidateModeKernelOnlyGoodSpv13, ValidateModeExecution,
731                          Combine(Values(SPV_SUCCESS), Values(""),
732                                  Values("Kernel"),
733                                  Values("LocalSizeHint 1 1 1", "VecTypeHint 4",
734                                         "ContractionOff",
735                                         "LocalSizeHintId %int1 %int1 %int1"),
736                                  Values(SPV_ENV_UNIVERSAL_1_3)));
737 
738 INSTANTIATE_TEST_SUITE_P(
739     ValidateModeKernelOnlyBadSpv13, ValidateModeExecution,
740     Combine(
741         Values(SPV_ERROR_INVALID_DATA),
742         Values(
743             "Execution mode can only be used with the Kernel execution model."),
744         Values("Geometry", "TessellationControl", "TessellationEvaluation",
745                "GLCompute", "Vertex", "Fragment"),
746         Values("LocalSizeHint 1 1 1", "VecTypeHint 4", "ContractionOff",
747                "LocalSizeHintId %int1 %int1 %int1"),
748         Values(SPV_ENV_UNIVERSAL_1_3)));
749 
750 INSTANTIATE_TEST_SUITE_P(
751     ValidateModeGLComputeAndKernelGoodSpv13, ValidateModeExecution,
752     Combine(Values(SPV_SUCCESS), Values(""), Values("Kernel", "GLCompute"),
753             Values("LocalSize 1 1 1", "LocalSizeId %int1 %int1 %int1"),
754             Values(SPV_ENV_UNIVERSAL_1_3)));
755 
756 INSTANTIATE_TEST_SUITE_P(
757     ValidateModeGLComputeAndKernelBadSpv13, ValidateModeExecution,
758     Combine(Values(SPV_ERROR_INVALID_DATA),
759             Values("Execution mode can only be used with a Kernel or GLCompute "
760                    "execution model."),
761             Values("Geometry", "TessellationControl", "TessellationEvaluation",
762                    "Fragment", "Vertex"),
763             Values("LocalSize 1 1 1", "LocalSizeId %int1 %int1 %int1"),
764             Values(SPV_ENV_UNIVERSAL_1_3)));
765 
766 INSTANTIATE_TEST_SUITE_P(
767     ValidateModeAllGoodSpv13, ValidateModeExecution,
768     Combine(Values(SPV_SUCCESS), Values(""),
769             Values("Kernel", "GLCompute", "Geometry", "TessellationControl",
770                    "TessellationEvaluation", "Fragment", "Vertex"),
771             Values("Xfb", "Initializer", "Finalizer", "SubgroupSize 1",
772                    "SubgroupsPerWorkgroup 1", "SubgroupsPerWorkgroupId %int1"),
773             Values(SPV_ENV_UNIVERSAL_1_3)));
774 
TEST_F(ValidateModeExecution, MeshNVLocalSize)775 TEST_F(ValidateModeExecution, MeshNVLocalSize) {
776   const std::string spirv = R"(
777 OpCapability Shader
778 OpCapability MeshShadingNV
779 OpExtension "SPV_NV_mesh_shader"
780 OpMemoryModel Logical GLSL450
781 OpEntryPoint MeshNV %main "main"
782 OpExecutionMode %main LocalSize 1 1 1
783 )" + kVoidFunction;
784 
785   CompileSuccessfully(spirv);
786   EXPECT_THAT(SPV_SUCCESS, ValidateInstructions());
787 }
788 
TEST_F(ValidateModeExecution, TaskNVLocalSize)789 TEST_F(ValidateModeExecution, TaskNVLocalSize) {
790   const std::string spirv = R"(
791 OpCapability Shader
792 OpCapability MeshShadingNV
793 OpExtension "SPV_NV_mesh_shader"
794 OpMemoryModel Logical GLSL450
795 OpEntryPoint TaskNV %main "main"
796 OpExecutionMode %main LocalSize 1 1 1
797 )" + kVoidFunction;
798 
799   CompileSuccessfully(spirv);
800   EXPECT_THAT(SPV_SUCCESS, ValidateInstructions());
801 }
802 
TEST_F(ValidateModeExecution, MeshNVOutputPoints)803 TEST_F(ValidateModeExecution, MeshNVOutputPoints) {
804   const std::string spirv = R"(
805 OpCapability Shader
806 OpCapability MeshShadingNV
807 OpExtension "SPV_NV_mesh_shader"
808 OpMemoryModel Logical GLSL450
809 OpEntryPoint MeshNV %main "main"
810 OpExecutionMode %main OutputPoints
811 )" + kVoidFunction;
812 
813   CompileSuccessfully(spirv);
814   EXPECT_THAT(SPV_SUCCESS, ValidateInstructions());
815 }
816 
TEST_F(ValidateModeExecution, MeshNVOutputVertices)817 TEST_F(ValidateModeExecution, MeshNVOutputVertices) {
818   const std::string spirv = R"(
819 OpCapability Shader
820 OpCapability MeshShadingNV
821 OpExtension "SPV_NV_mesh_shader"
822 OpMemoryModel Logical GLSL450
823 OpEntryPoint MeshNV %main "main"
824 OpExecutionMode %main OutputVertices 42
825 )" + kVoidFunction;
826 
827   CompileSuccessfully(spirv);
828   EXPECT_THAT(SPV_SUCCESS, ValidateInstructions());
829 }
830 
TEST_F(ValidateModeExecution, MeshNVLocalSizeId)831 TEST_F(ValidateModeExecution, MeshNVLocalSizeId) {
832   const std::string spirv = R"(
833 OpCapability Shader
834 OpCapability MeshShadingNV
835 OpExtension "SPV_NV_mesh_shader"
836 OpMemoryModel Logical GLSL450
837 OpEntryPoint MeshNV %main "main"
838 OpExecutionModeId %main LocalSizeId %int_1 %int_1 %int_1
839 %int = OpTypeInt 32 0
840 %int_1 = OpConstant %int 1
841 )" + kVoidFunction;
842 
843   spv_target_env env = SPV_ENV_UNIVERSAL_1_3;
844   CompileSuccessfully(spirv, env);
845   EXPECT_THAT(SPV_SUCCESS, ValidateInstructions(env));
846 }
847 
TEST_F(ValidateModeExecution, TaskNVLocalSizeId)848 TEST_F(ValidateModeExecution, TaskNVLocalSizeId) {
849   const std::string spirv = R"(
850 OpCapability Shader
851 OpCapability MeshShadingNV
852 OpExtension "SPV_NV_mesh_shader"
853 OpMemoryModel Logical GLSL450
854 OpEntryPoint TaskNV %main "main"
855 OpExecutionModeId %main LocalSizeId %int_1 %int_1 %int_1
856 %int = OpTypeInt 32 0
857 %int_1 = OpConstant %int 1
858 )" + kVoidFunction;
859 
860   spv_target_env env = SPV_ENV_UNIVERSAL_1_3;
861   CompileSuccessfully(spirv, env);
862   EXPECT_THAT(SPV_SUCCESS, ValidateInstructions(env));
863 }
864 
TEST_F(ValidateModeExecution, ExecModeSubgroupsPerWorkgroupIdBad)865 TEST_F(ValidateModeExecution, ExecModeSubgroupsPerWorkgroupIdBad) {
866   const std::string spirv = R"(
867 OpCapability Shader
868 OpCapability SubgroupDispatch
869 OpMemoryModel Logical GLSL450
870 OpEntryPoint Vertex %main "main"
871 OpExecutionMode %main SubgroupsPerWorkgroupId %int_1
872 %int = OpTypeInt 32 0
873 %int_1 = OpConstant %int 1
874 )" + kVoidFunction;
875 
876   spv_target_env env = SPV_ENV_UNIVERSAL_1_3;
877   CompileSuccessfully(spirv, env);
878   EXPECT_THAT(SPV_ERROR_INVALID_DATA, ValidateInstructions(env));
879   EXPECT_THAT(getDiagnosticString(),
880               HasSubstr("OpExecutionMode is only valid when the Mode operand "
881                         "is an execution mode that takes no Extra Operands"));
882 }
883 
TEST_F(ValidateModeExecution, ExecModeIdSubgroupsPerWorkgroupIdGood)884 TEST_F(ValidateModeExecution, ExecModeIdSubgroupsPerWorkgroupIdGood) {
885   const std::string spirv = R"(
886 OpCapability Shader
887 OpCapability SubgroupDispatch
888 OpMemoryModel Logical GLSL450
889 OpEntryPoint Vertex %main "main"
890 OpExecutionModeId %main SubgroupsPerWorkgroupId %int_1
891 %int = OpTypeInt 32 0
892 %int_1 = OpConstant %int 1
893 )" + kVoidFunction;
894 
895   spv_target_env env = SPV_ENV_UNIVERSAL_1_3;
896   CompileSuccessfully(spirv, env);
897   EXPECT_THAT(SPV_SUCCESS, ValidateInstructions(env));
898 }
899 
TEST_F(ValidateModeExecution, ExecModeIdSubgroupsPerWorkgroupIdNonConstantBad)900 TEST_F(ValidateModeExecution, ExecModeIdSubgroupsPerWorkgroupIdNonConstantBad) {
901   const std::string spirv = R"(
902 OpCapability Shader
903 OpCapability SubgroupDispatch
904 OpMemoryModel Logical GLSL450
905 OpEntryPoint Vertex %main "main"
906 OpExecutionModeId %main SubgroupsPerWorkgroupId %int_1
907 %int = OpTypeInt 32 0
908 %int_ptr = OpTypePointer Private %int
909 %int_1 = OpVariable %int_ptr Private
910 )" + kVoidFunction;
911 
912   spv_target_env env = SPV_ENV_UNIVERSAL_1_3;
913   CompileSuccessfully(spirv, env);
914   EXPECT_THAT(SPV_ERROR_INVALID_ID, ValidateInstructions(env));
915   EXPECT_THAT(getDiagnosticString(),
916               HasSubstr("For OpExecutionModeId all Extra Operand ids must be "
917                         "constant instructions."));
918 }
919 
TEST_F(ValidateModeExecution, ExecModeLocalSizeHintIdBad)920 TEST_F(ValidateModeExecution, ExecModeLocalSizeHintIdBad) {
921   const std::string spirv = R"(
922 OpCapability Kernel
923 OpCapability Shader
924 OpMemoryModel Logical GLSL450
925 OpEntryPoint Kernel %main "main"
926 OpExecutionMode %main LocalSizeHintId %int_1 %int_1 %int_1
927 %int = OpTypeInt 32 0
928 %int_1 = OpConstant %int 1
929 )" + kVoidFunction;
930 
931   spv_target_env env = SPV_ENV_UNIVERSAL_1_3;
932   CompileSuccessfully(spirv, env);
933   EXPECT_THAT(SPV_ERROR_INVALID_DATA, ValidateInstructions(env));
934   EXPECT_THAT(getDiagnosticString(),
935               HasSubstr("OpExecutionMode is only valid when the Mode operand "
936                         "is an execution mode that takes no Extra Operands"));
937 }
938 
TEST_F(ValidateModeExecution, ExecModeIdLocalSizeHintIdGood)939 TEST_F(ValidateModeExecution, ExecModeIdLocalSizeHintIdGood) {
940   const std::string spirv = R"(
941 OpCapability Kernel
942 OpCapability Shader
943 OpMemoryModel Logical GLSL450
944 OpEntryPoint Kernel %main "main"
945 OpExecutionModeId %main LocalSizeHintId %int_1 %int_1 %int_1
946 %int = OpTypeInt 32 0
947 %int_1 = OpConstant %int 1
948 )" + kVoidFunction;
949 
950   spv_target_env env = SPV_ENV_UNIVERSAL_1_3;
951   CompileSuccessfully(spirv, env);
952   EXPECT_THAT(SPV_SUCCESS, ValidateInstructions(env));
953 }
954 
TEST_F(ValidateModeExecution, ExecModeIdLocalSizeHintIdNonConstantBad)955 TEST_F(ValidateModeExecution, ExecModeIdLocalSizeHintIdNonConstantBad) {
956   const std::string spirv = R"(
957 OpCapability Kernel
958 OpCapability Shader
959 OpMemoryModel Logical GLSL450
960 OpEntryPoint Vertex %main "main"
961 OpExecutionModeId %main LocalSizeHintId %int_1 %int_1 %int_1
962 %int = OpTypeInt 32 0
963 %int_ptr = OpTypePointer Private %int
964 %int_1 = OpVariable %int_ptr Private
965 )" + kVoidFunction;
966 
967   spv_target_env env = SPV_ENV_UNIVERSAL_1_3;
968   CompileSuccessfully(spirv, env);
969   EXPECT_THAT(SPV_ERROR_INVALID_ID, ValidateInstructions(env));
970   EXPECT_THAT(getDiagnosticString(),
971               HasSubstr("For OpExecutionModeId all Extra Operand ids must be "
972                         "constant instructions."));
973 }
974 
TEST_F(ValidateModeExecution, ExecModeLocalSizeIdBad)975 TEST_F(ValidateModeExecution, ExecModeLocalSizeIdBad) {
976   const std::string spirv = R"(
977 OpCapability Kernel
978 OpCapability Shader
979 OpMemoryModel Logical GLSL450
980 OpEntryPoint Kernel %main "main"
981 OpExecutionMode %main LocalSizeId %int_1 %int_1 %int_1
982 %int = OpTypeInt 32 0
983 %int_1 = OpConstant %int 1
984 )" + kVoidFunction;
985 
986   spv_target_env env = SPV_ENV_UNIVERSAL_1_3;
987   CompileSuccessfully(spirv, env);
988   EXPECT_THAT(SPV_ERROR_INVALID_DATA, ValidateInstructions(env));
989   EXPECT_THAT(getDiagnosticString(),
990               HasSubstr("OpExecutionMode is only valid when the Mode operand "
991                         "is an execution mode that takes no Extra Operands"));
992 }
993 
TEST_F(ValidateModeExecution, ExecModeIdLocalSizeIdGood)994 TEST_F(ValidateModeExecution, ExecModeIdLocalSizeIdGood) {
995   const std::string spirv = R"(
996 OpCapability Kernel
997 OpCapability Shader
998 OpMemoryModel Logical GLSL450
999 OpEntryPoint Kernel %main "main"
1000 OpExecutionModeId %main LocalSizeId %int_1 %int_1 %int_1
1001 %int = OpTypeInt 32 0
1002 %int_1 = OpConstant %int 1
1003 )" + kVoidFunction;
1004 
1005   spv_target_env env = SPV_ENV_UNIVERSAL_1_3;
1006   CompileSuccessfully(spirv, env);
1007   EXPECT_THAT(SPV_SUCCESS, ValidateInstructions(env));
1008 }
1009 
TEST_F(ValidateModeExecution, ExecModeIdLocalSizeIdNonConstantBad)1010 TEST_F(ValidateModeExecution, ExecModeIdLocalSizeIdNonConstantBad) {
1011   const std::string spirv = R"(
1012 OpCapability Kernel
1013 OpCapability Shader
1014 OpMemoryModel Logical GLSL450
1015 OpEntryPoint Vertex %main "main"
1016 OpExecutionModeId %main LocalSizeId %int_1 %int_1 %int_1
1017 %int = OpTypeInt 32 0
1018 %int_ptr = OpTypePointer Private %int
1019 %int_1 = OpVariable %int_ptr Private
1020 )" + kVoidFunction;
1021 
1022   spv_target_env env = SPV_ENV_UNIVERSAL_1_3;
1023   CompileSuccessfully(spirv, env);
1024   EXPECT_THAT(SPV_ERROR_INVALID_ID, ValidateInstructions(env));
1025   EXPECT_THAT(getDiagnosticString(),
1026               HasSubstr("For OpExecutionModeId all Extra Operand ids must be "
1027                         "constant instructions."));
1028 }
1029 
TEST_F(ValidateMode, FragmentShaderInterlockVertexBad)1030 TEST_F(ValidateMode, FragmentShaderInterlockVertexBad) {
1031   const std::string spirv = R"(
1032 OpCapability Shader
1033 OpCapability FragmentShaderPixelInterlockEXT
1034 OpExtension "SPV_EXT_fragment_shader_interlock"
1035 OpMemoryModel Logical GLSL450
1036 OpEntryPoint Vertex %main "main"
1037 OpExecutionMode %main PixelInterlockOrderedEXT
1038 )" + kVoidFunction;
1039 
1040   CompileSuccessfully(spirv);
1041   EXPECT_THAT(SPV_ERROR_INVALID_DATA, ValidateInstructions());
1042   EXPECT_THAT(
1043       getDiagnosticString(),
1044       HasSubstr(
1045           "Execution mode can only be used with the Fragment execution model"));
1046 }
1047 
TEST_F(ValidateMode, FragmentShaderInterlockTooManyModesBad)1048 TEST_F(ValidateMode, FragmentShaderInterlockTooManyModesBad) {
1049   const std::string spirv = R"(
1050 OpCapability Shader
1051 OpCapability FragmentShaderPixelInterlockEXT
1052 OpCapability FragmentShaderSampleInterlockEXT
1053 OpExtension "SPV_EXT_fragment_shader_interlock"
1054 OpMemoryModel Logical GLSL450
1055 OpEntryPoint Fragment %main "main"
1056 OpExecutionMode %main OriginUpperLeft
1057 OpExecutionMode %main PixelInterlockOrderedEXT
1058 OpExecutionMode %main SampleInterlockOrderedEXT
1059 )" + kVoidFunction;
1060 
1061   CompileSuccessfully(spirv);
1062   EXPECT_THAT(SPV_ERROR_INVALID_DATA, ValidateInstructions());
1063   EXPECT_THAT(
1064       getDiagnosticString(),
1065       HasSubstr("Fragment execution model entry points can specify at most "
1066                 "one fragment shader interlock execution mode"));
1067 }
1068 
TEST_F(ValidateMode, FragmentShaderInterlockNoModeBad)1069 TEST_F(ValidateMode, FragmentShaderInterlockNoModeBad) {
1070   const std::string spirv = R"(
1071 OpCapability Shader
1072 OpCapability FragmentShaderPixelInterlockEXT
1073 OpExtension "SPV_EXT_fragment_shader_interlock"
1074 OpMemoryModel Logical GLSL450
1075 OpEntryPoint Fragment %main "main"
1076 OpExecutionMode %main OriginUpperLeft
1077 %void = OpTypeVoid
1078 %void_fn = OpTypeFunction %void
1079 %func = OpFunction %void None %void_fn
1080 %entryf = OpLabel
1081 OpBeginInvocationInterlockEXT
1082 OpEndInvocationInterlockEXT
1083 OpReturn
1084 OpFunctionEnd
1085 %main = OpFunction %void None %void_fn
1086 %entry = OpLabel
1087 %1 = OpFunctionCall %void %func
1088 OpReturn
1089 OpFunctionEnd
1090 )";
1091 
1092   CompileSuccessfully(spirv);
1093   EXPECT_THAT(SPV_ERROR_INVALID_ID, ValidateInstructions());
1094   EXPECT_THAT(
1095       getDiagnosticString(),
1096       HasSubstr(
1097           "OpBeginInvocationInterlockEXT/OpEndInvocationInterlockEXT require a "
1098           "fragment shader interlock execution mode"));
1099 }
1100 
TEST_F(ValidateMode, FragmentShaderInterlockGood)1101 TEST_F(ValidateMode, FragmentShaderInterlockGood) {
1102   const std::string spirv = R"(
1103 OpCapability Shader
1104 OpCapability FragmentShaderPixelInterlockEXT
1105 OpExtension "SPV_EXT_fragment_shader_interlock"
1106 OpMemoryModel Logical GLSL450
1107 OpEntryPoint Fragment %main "main"
1108 OpExecutionMode %main OriginUpperLeft
1109 OpExecutionMode %main PixelInterlockOrderedEXT
1110 %void = OpTypeVoid
1111 %void_fn = OpTypeFunction %void
1112 %func = OpFunction %void None %void_fn
1113 %entryf = OpLabel
1114 OpBeginInvocationInterlockEXT
1115 OpEndInvocationInterlockEXT
1116 OpReturn
1117 OpFunctionEnd
1118 %main = OpFunction %void None %void_fn
1119 %entry = OpLabel
1120 %1 = OpFunctionCall %void %func
1121 OpReturn
1122 OpFunctionEnd
1123 )";
1124 
1125   CompileSuccessfully(spirv);
1126   EXPECT_THAT(SPV_SUCCESS, ValidateInstructions());
1127 }
1128 
1129 
TEST_F(ValidateMode, FragmentShaderStencilRefFrontTooManyModesBad)1130 TEST_F(ValidateMode, FragmentShaderStencilRefFrontTooManyModesBad) {
1131   const std::string spirv = R"(
1132 OpCapability Shader
1133 OpCapability StencilExportEXT
1134 OpExtension "SPV_AMD_shader_early_and_late_fragment_tests"
1135 OpExtension "SPV_EXT_shader_stencil_export"
1136 OpMemoryModel Logical GLSL450
1137 OpEntryPoint Fragment %main "main"
1138 OpExecutionMode %main OriginUpperLeft
1139 OpExecutionMode %main EarlyAndLateFragmentTestsAMD
1140 OpExecutionMode %main StencilRefLessFrontAMD
1141 OpExecutionMode %main StencilRefGreaterFrontAMD
1142 )" + kVoidFunction;
1143 
1144   CompileSuccessfully(spirv);
1145   EXPECT_THAT(SPV_ERROR_INVALID_DATA, ValidateInstructions());
1146   EXPECT_THAT(
1147       getDiagnosticString(),
1148       HasSubstr("Fragment execution model entry points can specify at most "
1149                 "one of StencilRefUnchangedFrontAMD, "
1150                 "StencilRefLessFrontAMD or StencilRefGreaterFrontAMD "
1151                 "execution modes."));
1152 }
1153 
TEST_F(ValidateMode, FragmentShaderStencilRefBackTooManyModesBad)1154 TEST_F(ValidateMode, FragmentShaderStencilRefBackTooManyModesBad) {
1155   const std::string spirv = R"(
1156 OpCapability Shader
1157 OpCapability StencilExportEXT
1158 OpExtension "SPV_AMD_shader_early_and_late_fragment_tests"
1159 OpExtension "SPV_EXT_shader_stencil_export"
1160 OpMemoryModel Logical GLSL450
1161 OpEntryPoint Fragment %main "main"
1162 OpExecutionMode %main OriginUpperLeft
1163 OpExecutionMode %main EarlyAndLateFragmentTestsAMD
1164 OpExecutionMode %main StencilRefLessBackAMD
1165 OpExecutionMode %main StencilRefGreaterBackAMD
1166 )" + kVoidFunction;
1167 
1168   CompileSuccessfully(spirv);
1169   EXPECT_THAT(SPV_ERROR_INVALID_DATA, ValidateInstructions());
1170   EXPECT_THAT(
1171       getDiagnosticString(),
1172       HasSubstr("Fragment execution model entry points can specify at most "
1173                 "one of StencilRefUnchangedBackAMD, "
1174                 "StencilRefLessBackAMD or StencilRefGreaterBackAMD "
1175                 "execution modes."));
1176 }
1177 
TEST_F(ValidateMode, FragmentShaderStencilRefFrontGood)1178 TEST_F(ValidateMode, FragmentShaderStencilRefFrontGood) {
1179   const std::string spirv = R"(
1180 OpCapability Shader
1181 OpCapability StencilExportEXT
1182 OpExtension "SPV_AMD_shader_early_and_late_fragment_tests"
1183 OpExtension "SPV_EXT_shader_stencil_export"
1184 OpMemoryModel Logical GLSL450
1185 OpEntryPoint Fragment %main "main"
1186 OpExecutionMode %main OriginUpperLeft
1187 OpExecutionMode %main EarlyAndLateFragmentTestsAMD
1188 OpExecutionMode %main StencilRefLessFrontAMD
1189 )" + kVoidFunction;
1190 
1191   CompileSuccessfully(spirv);
1192   EXPECT_THAT(SPV_SUCCESS, ValidateInstructions());
1193 }
1194 
TEST_F(ValidateMode, FragmentShaderStencilRefBackGood)1195 TEST_F(ValidateMode, FragmentShaderStencilRefBackGood) {
1196   const std::string spirv = R"(
1197 OpCapability Shader
1198 OpCapability StencilExportEXT
1199 OpExtension "SPV_AMD_shader_early_and_late_fragment_tests"
1200 OpExtension "SPV_EXT_shader_stencil_export"
1201 OpMemoryModel Logical GLSL450
1202 OpEntryPoint Fragment %main "main"
1203 OpExecutionMode %main OriginUpperLeft
1204 OpExecutionMode %main EarlyAndLateFragmentTestsAMD
1205 OpExecutionMode %main StencilRefLessBackAMD
1206 )" + kVoidFunction;
1207 
1208   CompileSuccessfully(spirv);
1209   EXPECT_THAT(SPV_SUCCESS, ValidateInstructions());
1210 }
1211 
TEST_F(ValidateMode, FragmentShaderDemoteVertexBad)1212 TEST_F(ValidateMode, FragmentShaderDemoteVertexBad) {
1213   const std::string spirv = R"(
1214 OpCapability Shader
1215 OpCapability DemoteToHelperInvocationEXT
1216 OpExtension "SPV_EXT_demote_to_helper_invocation"
1217 OpMemoryModel Logical GLSL450
1218 OpEntryPoint Vertex %main "main"
1219 %bool = OpTypeBool
1220 %void = OpTypeVoid
1221 %void_fn = OpTypeFunction %void
1222 %main = OpFunction %void None %void_fn
1223 %entry = OpLabel
1224 OpDemoteToHelperInvocationEXT
1225 %1 = OpIsHelperInvocationEXT %bool
1226 OpReturn
1227 OpFunctionEnd
1228 )";
1229 
1230   CompileSuccessfully(spirv);
1231   EXPECT_THAT(SPV_ERROR_INVALID_ID, ValidateInstructions());
1232   EXPECT_THAT(
1233       getDiagnosticString(),
1234       HasSubstr(
1235           "OpDemoteToHelperInvocationEXT requires Fragment execution model"));
1236   EXPECT_THAT(
1237       getDiagnosticString(),
1238       HasSubstr("OpIsHelperInvocationEXT requires Fragment execution model"));
1239 }
1240 
TEST_F(ValidateMode, FragmentShaderDemoteGood)1241 TEST_F(ValidateMode, FragmentShaderDemoteGood) {
1242   const std::string spirv = R"(
1243 OpCapability Shader
1244 OpCapability DemoteToHelperInvocationEXT
1245 OpExtension "SPV_EXT_demote_to_helper_invocation"
1246 OpMemoryModel Logical GLSL450
1247 OpEntryPoint Fragment %main "main"
1248 OpExecutionMode %main OriginUpperLeft
1249 %bool = OpTypeBool
1250 %void = OpTypeVoid
1251 %void_fn = OpTypeFunction %void
1252 %main = OpFunction %void None %void_fn
1253 %entry = OpLabel
1254 OpDemoteToHelperInvocationEXT
1255 %1 = OpIsHelperInvocationEXT %bool
1256 OpReturn
1257 OpFunctionEnd
1258 )";
1259 
1260   CompileSuccessfully(spirv);
1261   EXPECT_THAT(SPV_SUCCESS, ValidateInstructions());
1262 }
1263 
TEST_F(ValidateMode, FragmentShaderDemoteBadType)1264 TEST_F(ValidateMode, FragmentShaderDemoteBadType) {
1265   const std::string spirv = R"(
1266 OpCapability Shader
1267 OpCapability DemoteToHelperInvocationEXT
1268 OpExtension "SPV_EXT_demote_to_helper_invocation"
1269 OpMemoryModel Logical GLSL450
1270 OpEntryPoint Fragment %main "main"
1271 OpExecutionMode %main OriginUpperLeft
1272 %u32 = OpTypeInt 32 0
1273 %void = OpTypeVoid
1274 %void_fn = OpTypeFunction %void
1275 %main = OpFunction %void None %void_fn
1276 %entry = OpLabel
1277 OpDemoteToHelperInvocationEXT
1278 %1 = OpIsHelperInvocationEXT %u32
1279 OpReturn
1280 OpFunctionEnd
1281 )";
1282 
1283   CompileSuccessfully(spirv);
1284   EXPECT_THAT(SPV_ERROR_INVALID_DATA, ValidateInstructions());
1285   EXPECT_THAT(getDiagnosticString(),
1286               HasSubstr("Expected bool scalar type as Result Type"));
1287 }
1288 
TEST_F(ValidateMode, LocalSizeIdVulkan1p3DoesNotRequireOption)1289 TEST_F(ValidateMode, LocalSizeIdVulkan1p3DoesNotRequireOption) {
1290   const std::string spirv = R"(
1291 OpCapability Shader
1292 OpMemoryModel Logical GLSL450
1293 OpEntryPoint GLCompute %main "main"
1294 OpExecutionModeId %main LocalSizeId %int_1 %int_1 %int_1
1295 %void = OpTypeVoid
1296 %int = OpTypeInt 32 0
1297 %int_1 = OpConstant %int 1
1298 %void_fn = OpTypeFunction %void
1299 %main = OpFunction %void None %void_fn
1300 %entry = OpLabel
1301 OpReturn
1302 OpFunctionEnd
1303 )";
1304 
1305   CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_3);
1306   EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_VULKAN_1_3));
1307 }
1308 
1309 }  // namespace
1310 }  // namespace val
1311 }  // namespace spvtools
1312