• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2022 The Khronos Group 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 // Tests ray query instructions from SPV_KHR_ray_query.
16 
17 #include <sstream>
18 #include <string>
19 
20 #include "gmock/gmock.h"
21 #include "spirv-tools/libspirv.h"
22 #include "test/val/val_fixtures.h"
23 
24 namespace spvtools {
25 namespace val {
26 namespace {
27 
28 using ::testing::HasSubstr;
29 using ::testing::Values;
30 
31 using ValidateRayQuery = spvtest::ValidateBase<bool>;
32 
GenerateShaderCode(const std::string & body,const std::string & capabilities_and_extensions="",const std::string & declarations="")33 std::string GenerateShaderCode(
34     const std::string& body,
35     const std::string& capabilities_and_extensions = "",
36     const std::string& declarations = "") {
37   std::ostringstream ss;
38   ss << R"(
39 OpCapability Shader
40 OpCapability Int64
41 OpCapability Float64
42 OpCapability RayQueryKHR
43 OpExtension "SPV_KHR_ray_query"
44 )";
45 
46   ss << capabilities_and_extensions;
47 
48   ss << R"(
49 OpMemoryModel Logical GLSL450
50 OpEntryPoint GLCompute %main "main"
51 OpExecutionMode %main LocalSize 1 1 1
52 
53 OpDecorate %top_level_as DescriptorSet 0
54 OpDecorate %top_level_as Binding 0
55 
56 %void = OpTypeVoid
57 %func = OpTypeFunction %void
58 %bool = OpTypeBool
59 %f32 = OpTypeFloat 32
60 %f64 = OpTypeFloat 64
61 %u32 = OpTypeInt 32 0
62 %s32 = OpTypeInt 32 1
63 %u64 = OpTypeInt 64 0
64 %s64 = OpTypeInt 64 1
65 %type_rq = OpTypeRayQueryKHR
66 %type_as = OpTypeAccelerationStructureKHR
67 
68 %s32vec2 = OpTypeVector %s32 2
69 %u32vec2 = OpTypeVector %u32 2
70 %f32vec2 = OpTypeVector %f32 2
71 %u32vec3 = OpTypeVector %u32 3
72 %s32vec3 = OpTypeVector %s32 3
73 %f32vec3 = OpTypeVector %f32 3
74 %u32vec4 = OpTypeVector %u32 4
75 %s32vec4 = OpTypeVector %s32 4
76 %f32vec4 = OpTypeVector %f32 4
77 
78 %mat4x3 = OpTypeMatrix %f32vec3 4
79 
80 %f32_0 = OpConstant %f32 0
81 %f64_0 = OpConstant %f64 0
82 %s32_0 = OpConstant %s32 0
83 %u32_0 = OpConstant %u32 0
84 %u64_0 = OpConstant %u64 0
85 
86 %u32vec3_0 = OpConstantComposite %u32vec3 %u32_0 %u32_0 %u32_0
87 %f32vec3_0 = OpConstantComposite %f32vec3 %f32_0 %f32_0 %f32_0
88 %f32vec4_0 = OpConstantComposite %f32vec4 %f32_0 %f32_0 %f32_0 %f32_0
89 
90 %ptr_rq = OpTypePointer Private %type_rq
91 %ray_query = OpVariable %ptr_rq Private
92 
93 %ptr_as = OpTypePointer UniformConstant %type_as
94 %top_level_as = OpVariable %ptr_as UniformConstant
95 
96 %ptr_function_u32 = OpTypePointer Function %u32
97 %ptr_function_f32 = OpTypePointer Function %f32
98 %ptr_function_f32vec3 = OpTypePointer Function %f32vec3
99 )";
100 
101   ss << declarations;
102 
103   ss << R"(
104 %main = OpFunction %void None %func
105 %main_entry = OpLabel
106 )";
107 
108   ss << body;
109 
110   ss << R"(
111 OpReturn
112 OpFunctionEnd)";
113   return ss.str();
114 }
115 
RayQueryResult(std::string opcode)116 std::string RayQueryResult(std::string opcode) {
117   if (opcode.compare("OpRayQueryProceedKHR") == 0 ||
118       opcode.compare("OpRayQueryGetIntersectionTypeKHR") == 0 ||
119       opcode.compare("OpRayQueryGetRayTMinKHR") == 0 ||
120       opcode.compare("OpRayQueryGetRayFlagsKHR") == 0 ||
121       opcode.compare("OpRayQueryGetIntersectionTKHR") == 0 ||
122       opcode.compare("OpRayQueryGetIntersectionInstanceCustomIndexKHR") == 0 ||
123       opcode.compare("OpRayQueryGetIntersectionInstanceIdKHR") == 0 ||
124       opcode.compare("OpRayQueryGetIntersectionInstanceShaderBindingTableRecord"
125                      "OffsetKHR") == 0 ||
126       opcode.compare("OpRayQueryGetIntersectionGeometryIndexKHR") == 0 ||
127       opcode.compare("OpRayQueryGetIntersectionPrimitiveIndexKHR") == 0 ||
128       opcode.compare("OpRayQueryGetIntersectionBarycentricsKHR") == 0 ||
129       opcode.compare("OpRayQueryGetIntersectionFrontFaceKHR") == 0 ||
130       opcode.compare("OpRayQueryGetIntersectionCandidateAABBOpaqueKHR") == 0 ||
131       opcode.compare("OpRayQueryGetIntersectionObjectRayDirectionKHR") == 0 ||
132       opcode.compare("OpRayQueryGetIntersectionObjectRayOriginKHR") == 0 ||
133       opcode.compare("OpRayQueryGetWorldRayDirectionKHR") == 0 ||
134       opcode.compare("OpRayQueryGetWorldRayOriginKHR") == 0 ||
135       opcode.compare("OpRayQueryGetIntersectionObjectToWorldKHR") == 0 ||
136       opcode.compare("OpRayQueryGetIntersectionWorldToObjectKHR") == 0) {
137     return "%result =";
138   }
139   return "";
140 }
141 
RayQueryResultType(std::string opcode,bool valid)142 std::string RayQueryResultType(std::string opcode, bool valid) {
143   if (opcode.compare("OpRayQueryGetIntersectionTypeKHR") == 0 ||
144       opcode.compare("OpRayQueryGetRayFlagsKHR") == 0 ||
145       opcode.compare("OpRayQueryGetIntersectionInstanceCustomIndexKHR") == 0 ||
146       opcode.compare("OpRayQueryGetIntersectionInstanceIdKHR") == 0 ||
147       opcode.compare("OpRayQueryGetIntersectionInstanceShaderBindingTableRecord"
148                      "OffsetKHR") == 0 ||
149       opcode.compare("OpRayQueryGetIntersectionGeometryIndexKHR") == 0 ||
150       opcode.compare("OpRayQueryGetIntersectionPrimitiveIndexKHR") == 0) {
151     return valid ? "%u32" : "%f64";
152   }
153 
154   if (opcode.compare("OpRayQueryGetRayTMinKHR") == 0 ||
155       opcode.compare("OpRayQueryGetIntersectionTKHR") == 0) {
156     return valid ? "%f32" : "%f64";
157   }
158 
159   if (opcode.compare("OpRayQueryGetIntersectionBarycentricsKHR") == 0) {
160     return valid ? "%f32vec2" : "%f64";
161   }
162 
163   if (opcode.compare("OpRayQueryGetIntersectionObjectRayDirectionKHR") == 0 ||
164       opcode.compare("OpRayQueryGetIntersectionObjectRayOriginKHR") == 0 ||
165       opcode.compare("OpRayQueryGetWorldRayDirectionKHR") == 0 ||
166       opcode.compare("OpRayQueryGetWorldRayOriginKHR") == 0) {
167     return valid ? "%f32vec3" : "%f64";
168   }
169 
170   if (opcode.compare("OpRayQueryProceedKHR") == 0 ||
171       opcode.compare("OpRayQueryGetIntersectionFrontFaceKHR") == 0 ||
172       opcode.compare("OpRayQueryGetIntersectionCandidateAABBOpaqueKHR") == 0) {
173     return valid ? "%bool" : "%f64";
174   }
175 
176   if (opcode.compare("OpRayQueryGetIntersectionObjectToWorldKHR") == 0 ||
177       opcode.compare("OpRayQueryGetIntersectionWorldToObjectKHR") == 0) {
178     return valid ? "%mat4x3" : "%f64";
179   }
180   return "";
181 }
182 
RayQueryIntersection(std::string opcode,bool valid)183 std::string RayQueryIntersection(std::string opcode, bool valid) {
184   if (opcode.compare("OpRayQueryGetIntersectionTypeKHR") == 0 ||
185       opcode.compare("OpRayQueryGetIntersectionTKHR") == 0 ||
186       opcode.compare("OpRayQueryGetIntersectionInstanceCustomIndexKHR") == 0 ||
187       opcode.compare("OpRayQueryGetIntersectionInstanceIdKHR") == 0 ||
188       opcode.compare("OpRayQueryGetIntersectionInstanceShaderBindingTableRecord"
189                      "OffsetKHR") == 0 ||
190       opcode.compare("OpRayQueryGetIntersectionGeometryIndexKHR") == 0 ||
191       opcode.compare("OpRayQueryGetIntersectionPrimitiveIndexKHR") == 0 ||
192       opcode.compare("OpRayQueryGetIntersectionBarycentricsKHR") == 0 ||
193       opcode.compare("OpRayQueryGetIntersectionFrontFaceKHR") == 0 ||
194       opcode.compare("OpRayQueryGetIntersectionObjectRayDirectionKHR") == 0 ||
195       opcode.compare("OpRayQueryGetIntersectionObjectRayOriginKHR") == 0 ||
196       opcode.compare("OpRayQueryGetIntersectionObjectToWorldKHR") == 0 ||
197       opcode.compare("OpRayQueryGetIntersectionWorldToObjectKHR") == 0) {
198     return valid ? "%s32_0" : "%f32_0";
199   }
200   return "";
201 }
202 
203 using RayQueryCommon = spvtest::ValidateBase<std::string>;
204 
TEST_P(RayQueryCommon,Success)205 TEST_P(RayQueryCommon, Success) {
206   std::string opcode = GetParam();
207   std::ostringstream ss;
208   ss << RayQueryResult(opcode);
209   ss << " " << opcode << " ";
210   ss << RayQueryResultType(opcode, true);
211   ss << " %ray_query ";
212   ss << RayQueryIntersection(opcode, true);
213   CompileSuccessfully(GenerateShaderCode(ss.str()).c_str());
214   EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
215 }
216 
TEST_P(RayQueryCommon,BadQuery)217 TEST_P(RayQueryCommon, BadQuery) {
218   std::string opcode = GetParam();
219   std::ostringstream ss;
220   ss << RayQueryResult(opcode);
221   ss << " " << opcode << " ";
222   ss << RayQueryResultType(opcode, true);
223   ss << " %top_level_as ";
224   ss << RayQueryIntersection(opcode, true);
225   CompileSuccessfully(GenerateShaderCode(ss.str()).c_str());
226   EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
227   EXPECT_THAT(getDiagnosticString(),
228               HasSubstr("Ray Query must be a pointer to OpTypeRayQueryKHR"));
229 }
230 
TEST_P(RayQueryCommon,BadResult)231 TEST_P(RayQueryCommon, BadResult) {
232   std::string opcode = GetParam();
233   std::string result_type = RayQueryResultType(opcode, false);
234   if (!result_type.empty()) {
235     std::ostringstream ss;
236     ss << RayQueryResult(opcode);
237     ss << " " << opcode << " ";
238     ss << result_type;
239     ss << " %ray_query ";
240     ss << RayQueryIntersection(opcode, true);
241     CompileSuccessfully(GenerateShaderCode(ss.str()).c_str());
242     EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
243 
244     std::string correct_result_type = RayQueryResultType(opcode, true);
245     if (correct_result_type.compare("%u32") == 0) {
246       EXPECT_THAT(
247           getDiagnosticString(),
248           HasSubstr("expected Result Type to be 32-bit int scalar type"));
249     } else if (correct_result_type.compare("%f32") == 0) {
250       EXPECT_THAT(
251           getDiagnosticString(),
252           HasSubstr("expected Result Type to be 32-bit float scalar type"));
253     } else if (correct_result_type.compare("%f32vec2") == 0) {
254       EXPECT_THAT(getDiagnosticString(),
255                   HasSubstr("expected Result Type to be 32-bit float "
256                             "2-component vector type"));
257     } else if (correct_result_type.compare("%f32vec3") == 0) {
258       EXPECT_THAT(getDiagnosticString(),
259                   HasSubstr("expected Result Type to be 32-bit float "
260                             "3-component vector type"));
261     } else if (correct_result_type.compare("%bool") == 0) {
262       EXPECT_THAT(getDiagnosticString(),
263                   HasSubstr("expected Result Type to be bool scalar type"));
264     } else if (correct_result_type.compare("%mat4x3") == 0) {
265       EXPECT_THAT(getDiagnosticString(),
266                   HasSubstr("expected matrix type as Result Type"));
267     }
268   }
269 }
270 
TEST_P(RayQueryCommon,BadIntersection)271 TEST_P(RayQueryCommon, BadIntersection) {
272   std::string opcode = GetParam();
273   std::string intersection = RayQueryIntersection(opcode, false);
274   if (!intersection.empty()) {
275     std::ostringstream ss;
276     ss << RayQueryResult(opcode);
277     ss << " " << opcode << " ";
278     ss << RayQueryResultType(opcode, true);
279     ss << " %ray_query ";
280     ss << intersection;
281     CompileSuccessfully(GenerateShaderCode(ss.str()).c_str());
282     EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
283     EXPECT_THAT(
284         getDiagnosticString(),
285         HasSubstr(
286             "expected Intersection ID to be a constant 32-bit int scalar"));
287   }
288 }
289 
290 INSTANTIATE_TEST_SUITE_P(
291     ValidateRayQueryCommon, RayQueryCommon,
292     Values("OpRayQueryTerminateKHR", "OpRayQueryConfirmIntersectionKHR",
293            "OpRayQueryProceedKHR", "OpRayQueryGetIntersectionTypeKHR",
294            "OpRayQueryGetRayTMinKHR", "OpRayQueryGetRayFlagsKHR",
295            "OpRayQueryGetWorldRayDirectionKHR",
296            "OpRayQueryGetWorldRayOriginKHR", "OpRayQueryGetIntersectionTKHR",
297            "OpRayQueryGetIntersectionInstanceCustomIndexKHR",
298            "OpRayQueryGetIntersectionInstanceIdKHR",
299            "OpRayQueryGetIntersectionInstanceShaderBindingTableRecordOffsetKHR",
300            "OpRayQueryGetIntersectionGeometryIndexKHR",
301            "OpRayQueryGetIntersectionPrimitiveIndexKHR",
302            "OpRayQueryGetIntersectionBarycentricsKHR",
303            "OpRayQueryGetIntersectionFrontFaceKHR",
304            "OpRayQueryGetIntersectionCandidateAABBOpaqueKHR",
305            "OpRayQueryGetIntersectionObjectRayDirectionKHR",
306            "OpRayQueryGetIntersectionObjectRayOriginKHR",
307            "OpRayQueryGetIntersectionObjectToWorldKHR",
308            "OpRayQueryGetIntersectionWorldToObjectKHR"));
309 
310 // tests various Intersection operand types
TEST_F(ValidateRayQuery,IntersectionSuccess)311 TEST_F(ValidateRayQuery, IntersectionSuccess) {
312   const std::string body = R"(
313 %result_1 = OpRayQueryGetIntersectionFrontFaceKHR %bool %ray_query %s32_0
314 %result_2 = OpRayQueryGetIntersectionFrontFaceKHR %bool %ray_query %u32_0
315 )";
316 
317   CompileSuccessfully(GenerateShaderCode(body).c_str());
318   EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
319 }
320 
TEST_F(ValidateRayQuery,IntersectionVector)321 TEST_F(ValidateRayQuery, IntersectionVector) {
322   const std::string body = R"(
323 %result = OpRayQueryGetIntersectionFrontFaceKHR %bool %ray_query %u32vec3_0
324 )";
325 
326   CompileSuccessfully(GenerateShaderCode(body).c_str());
327   EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
328   EXPECT_THAT(
329       getDiagnosticString(),
330       HasSubstr("expected Intersection ID to be a constant 32-bit int scalar"));
331 }
332 
TEST_F(ValidateRayQuery,IntersectionNonConstantVariable)333 TEST_F(ValidateRayQuery, IntersectionNonConstantVariable) {
334   const std::string body = R"(
335 %var = OpVariable %ptr_function_u32 Function
336 %result = OpRayQueryGetIntersectionFrontFaceKHR %bool %ray_query %var
337 )";
338 
339   CompileSuccessfully(GenerateShaderCode(body).c_str());
340   EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
341   EXPECT_THAT(
342       getDiagnosticString(),
343       HasSubstr("expected Intersection ID to be a constant 32-bit int scalar"));
344 }
345 
TEST_F(ValidateRayQuery,IntersectionNonConstantLoad)346 TEST_F(ValidateRayQuery, IntersectionNonConstantLoad) {
347   const std::string body = R"(
348 %var = OpVariable %ptr_function_u32 Function
349 %load = OpLoad %u32 %var
350 %result = OpRayQueryGetIntersectionFrontFaceKHR %bool %ray_query %load
351 )";
352 
353   CompileSuccessfully(GenerateShaderCode(body).c_str());
354   EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
355   EXPECT_THAT(
356       getDiagnosticString(),
357       HasSubstr("expected Intersection ID to be a constant 32-bit int scalar"));
358 }
359 
TEST_F(ValidateRayQuery,InitializeSuccess)360 TEST_F(ValidateRayQuery, InitializeSuccess) {
361   const std::string body = R"(
362 %var_u32 = OpVariable %ptr_function_u32 Function
363 %var_f32 = OpVariable %ptr_function_f32 Function
364 %var_f32vec3 = OpVariable %ptr_function_f32vec3 Function
365 
366 %as = OpLoad %type_as %top_level_as
367 OpRayQueryInitializeKHR %ray_query %as %u32_0 %u32_0 %f32vec3_0 %f32_0 %f32vec3_0 %f32_0
368 
369 %_u32 = OpLoad %u32 %var_u32
370 %_f32 = OpLoad %f32 %var_f32
371 %_f32vec3 = OpLoad %f32vec3 %var_f32vec3
372 OpRayQueryInitializeKHR %ray_query %as %_u32 %_u32 %_f32vec3 %_f32 %_f32vec3 %_f32
373 )";
374 
375   CompileSuccessfully(GenerateShaderCode(body).c_str());
376   EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
377 }
378 
TEST_F(ValidateRayQuery,InitializeFunctionSuccess)379 TEST_F(ValidateRayQuery, InitializeFunctionSuccess) {
380   const std::string declaration = R"(
381 %rq_ptr = OpTypePointer Private %type_rq
382 %rq_func_type = OpTypeFunction %void %rq_ptr
383 %rq_var_1 = OpVariable %rq_ptr Private
384 %rq_var_2 = OpVariable %rq_ptr Private
385 )";
386 
387   const std::string body = R"(
388 %fcall_1 = OpFunctionCall %void %rq_func %rq_var_1
389 %as_1 = OpLoad %type_as %top_level_as
390 OpRayQueryInitializeKHR %rq_var_1 %as_1 %u32_0 %u32_0 %f32vec3_0 %f32_0 %f32vec3_0 %f32_0
391 %fcall_2 = OpFunctionCall %void %rq_func %rq_var_2
392 OpReturn
393 OpFunctionEnd
394 %rq_func = OpFunction %void None %rq_func_type
395 %rq_param = OpFunctionParameter %rq_ptr
396 %label = OpLabel
397 %as_2 = OpLoad %type_as %top_level_as
398 OpRayQueryInitializeKHR %rq_param %as_2 %u32_0 %u32_0 %f32vec3_0 %f32_0 %f32vec3_0 %f32_0
399 )";
400 
401   CompileSuccessfully(GenerateShaderCode(body, "", declaration).c_str());
402   EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
403 }
404 
TEST_F(ValidateRayQuery,InitializeBadRayQuery)405 TEST_F(ValidateRayQuery, InitializeBadRayQuery) {
406   const std::string body = R"(
407 %load = OpLoad %type_as %top_level_as
408 OpRayQueryInitializeKHR %top_level_as %load %u32_0 %u32_0 %f32vec3_0 %f32_0 %f32vec3_0 %f32_0
409 )";
410 
411   CompileSuccessfully(GenerateShaderCode(body).c_str());
412   EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
413   EXPECT_THAT(getDiagnosticString(),
414               HasSubstr("Ray Query must be a pointer to OpTypeRayQueryKHR"));
415 }
416 
TEST_F(ValidateRayQuery,InitializeBadAS)417 TEST_F(ValidateRayQuery, InitializeBadAS) {
418   const std::string body = R"(
419 OpRayQueryInitializeKHR %ray_query %ray_query %u32_0 %u32_0 %f32vec3_0 %f32_0 %f32vec3_0 %f32_0
420 )";
421 
422   CompileSuccessfully(GenerateShaderCode(body).c_str());
423   EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
424   EXPECT_THAT(getDiagnosticString(),
425               HasSubstr("Expected Acceleration Structure to be of type "
426                         "OpTypeAccelerationStructureKHR"));
427 }
428 
TEST_F(ValidateRayQuery,InitializeBadRayFlags64)429 TEST_F(ValidateRayQuery, InitializeBadRayFlags64) {
430   const std::string body = R"(
431 %load = OpLoad %type_as %top_level_as
432 OpRayQueryInitializeKHR %ray_query %load %u64_0 %u32_0 %f32vec3_0 %f32_0 %f32vec3_0 %f32_0
433 )";
434 
435   CompileSuccessfully(GenerateShaderCode(body).c_str());
436   EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
437   EXPECT_THAT(getDiagnosticString(),
438               HasSubstr("Ray Flags must be a 32-bit int scalar"));
439 }
440 
TEST_F(ValidateRayQuery,InitializeBadRayFlagsVector)441 TEST_F(ValidateRayQuery, InitializeBadRayFlagsVector) {
442   const std::string body = R"(
443 %load = OpLoad %type_as %top_level_as
444 OpRayQueryInitializeKHR %ray_query %load %u32vec2 %u32_0 %f32vec3_0 %f32_0 %f32vec3_0 %f32_0
445 )";
446 
447   CompileSuccessfully(GenerateShaderCode(body).c_str());
448   EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
449   EXPECT_THAT(getDiagnosticString(),
450               HasSubstr("Operand '15[%v2uint]' cannot be a type"));
451 }
452 
TEST_F(ValidateRayQuery,InitializeBadCullMask)453 TEST_F(ValidateRayQuery, InitializeBadCullMask) {
454   const std::string body = R"(
455 %load = OpLoad %type_as %top_level_as
456 OpRayQueryInitializeKHR %ray_query %load %u32_0 %f32_0 %f32vec3_0 %f32_0 %f32vec3_0 %f32_0
457 )";
458 
459   CompileSuccessfully(GenerateShaderCode(body).c_str());
460   EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
461   EXPECT_THAT(getDiagnosticString(),
462               HasSubstr("Cull Mask must be a 32-bit int scalar"));
463 }
464 
TEST_F(ValidateRayQuery,InitializeBadRayOriginVec4)465 TEST_F(ValidateRayQuery, InitializeBadRayOriginVec4) {
466   const std::string body = R"(
467 %load = OpLoad %type_as %top_level_as
468 OpRayQueryInitializeKHR %ray_query %load %u32_0 %u32_0 %f32vec4_0 %f32_0 %f32vec3_0 %f32_0
469 )";
470 
471   CompileSuccessfully(GenerateShaderCode(body).c_str());
472   EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
473   EXPECT_THAT(
474       getDiagnosticString(),
475       HasSubstr("Ray Origin must be a 32-bit float 3-component vector"));
476 }
477 
TEST_F(ValidateRayQuery,InitializeBadRayOriginFloat)478 TEST_F(ValidateRayQuery, InitializeBadRayOriginFloat) {
479   const std::string body = R"(
480 %var_f32 = OpVariable %ptr_function_f32 Function
481 %_f32 = OpLoad %f32 %var_f32
482 %load = OpLoad %type_as %top_level_as
483 OpRayQueryInitializeKHR %ray_query %load %u32_0 %u32_0 %_f32 %f32_0 %f32vec3_0 %f32_0
484 )";
485 
486   CompileSuccessfully(GenerateShaderCode(body).c_str());
487   EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
488   EXPECT_THAT(
489       getDiagnosticString(),
490       HasSubstr("Ray Origin must be a 32-bit float 3-component vector"));
491 }
492 
TEST_F(ValidateRayQuery,InitializeBadRayOriginInt)493 TEST_F(ValidateRayQuery, InitializeBadRayOriginInt) {
494   const std::string body = R"(
495 %load = OpLoad %type_as %top_level_as
496 OpRayQueryInitializeKHR %ray_query %load %u32_0 %u32_0 %u32vec3_0 %f32_0 %f32vec3_0 %f32_0
497 )";
498 
499   CompileSuccessfully(GenerateShaderCode(body).c_str());
500   EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
501   EXPECT_THAT(
502       getDiagnosticString(),
503       HasSubstr("Ray Origin must be a 32-bit float 3-component vector"));
504 }
505 
TEST_F(ValidateRayQuery,InitializeBadRayTMin)506 TEST_F(ValidateRayQuery, InitializeBadRayTMin) {
507   const std::string body = R"(
508 %load = OpLoad %type_as %top_level_as
509 OpRayQueryInitializeKHR %ray_query %load %u32_0 %u32_0 %f32vec3_0 %u32_0 %f32vec3_0 %f32_0
510 )";
511 
512   CompileSuccessfully(GenerateShaderCode(body).c_str());
513   EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
514   EXPECT_THAT(getDiagnosticString(),
515               HasSubstr("Ray TMin must be a 32-bit float scalar"));
516 }
517 
TEST_F(ValidateRayQuery,InitializeBadRayDirection)518 TEST_F(ValidateRayQuery, InitializeBadRayDirection) {
519   const std::string body = R"(
520 %load = OpLoad %type_as %top_level_as
521 OpRayQueryInitializeKHR %ray_query %load %u32_0 %u32_0 %f32vec3_0 %f32_0 %f32vec4_0 %f32_0
522 )";
523 
524   CompileSuccessfully(GenerateShaderCode(body).c_str());
525   EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
526   EXPECT_THAT(
527       getDiagnosticString(),
528       HasSubstr("Ray Direction must be a 32-bit float 3-component vector"));
529 }
530 
TEST_F(ValidateRayQuery,InitializeBadRayTMax)531 TEST_F(ValidateRayQuery, InitializeBadRayTMax) {
532   const std::string body = R"(
533 %load = OpLoad %type_as %top_level_as
534 OpRayQueryInitializeKHR %ray_query %load %u32_0 %u32_0 %f32vec3_0 %f32_0 %f32vec3_0 %f64_0
535 )";
536 
537   CompileSuccessfully(GenerateShaderCode(body).c_str());
538   EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
539   EXPECT_THAT(getDiagnosticString(),
540               HasSubstr("Ray TMax must be a 32-bit float scalar"));
541 }
542 
TEST_F(ValidateRayQuery,GenerateIntersectionSuccess)543 TEST_F(ValidateRayQuery, GenerateIntersectionSuccess) {
544   const std::string body = R"(
545 %var = OpVariable %ptr_function_f32 Function
546 %load = OpLoad %f32 %var
547 OpRayQueryGenerateIntersectionKHR %ray_query %f32_0
548 OpRayQueryGenerateIntersectionKHR %ray_query %load
549 )";
550 
551   CompileSuccessfully(GenerateShaderCode(body).c_str());
552   EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
553 }
554 
TEST_F(ValidateRayQuery,GenerateIntersectionBadRayQuery)555 TEST_F(ValidateRayQuery, GenerateIntersectionBadRayQuery) {
556   const std::string body = R"(
557 OpRayQueryGenerateIntersectionKHR %top_level_as %f32_0
558 )";
559 
560   CompileSuccessfully(GenerateShaderCode(body).c_str());
561   EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
562   EXPECT_THAT(getDiagnosticString(),
563               HasSubstr("Ray Query must be a pointer to OpTypeRayQueryKHR"));
564 }
565 
TEST_F(ValidateRayQuery,GenerateIntersectionBadHitT)566 TEST_F(ValidateRayQuery, GenerateIntersectionBadHitT) {
567   const std::string body = R"(
568 OpRayQueryGenerateIntersectionKHR %ray_query %u32_0
569 )";
570 
571   CompileSuccessfully(GenerateShaderCode(body).c_str());
572   EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
573   EXPECT_THAT(getDiagnosticString(),
574               HasSubstr("Hit T must be a 32-bit float scalar"));
575 }
576 
TEST_F(ValidateRayQuery,RayQueryArraySuccess)577 TEST_F(ValidateRayQuery, RayQueryArraySuccess) {
578   // This shader is slightly different to the ones above, so it doesn't reuse
579   // the shader code generator.
580   const std::string shader = R"(
581                        OpCapability Shader
582                        OpCapability RayQueryKHR
583                        OpExtension "SPV_KHR_ray_query"
584                        OpMemoryModel Logical GLSL450
585                        OpEntryPoint GLCompute %main "main"
586                        OpExecutionMode %main LocalSize 1 1 1
587                        OpSource GLSL 460
588                        OpDecorate %topLevelAS DescriptorSet 0
589                        OpDecorate %topLevelAS Binding 0
590                        OpDecorate %gl_WorkGroupSize BuiltIn WorkgroupSize
591                %void = OpTypeVoid
592                %func = OpTypeFunction %void
593           %ray_query = OpTypeRayQueryKHR
594                %uint = OpTypeInt 32 0
595              %uint_2 = OpConstant %uint 2
596     %ray_query_array = OpTypeArray %ray_query %uint_2
597 %ptr_ray_query_array = OpTypePointer Private %ray_query_array
598          %rayQueries = OpVariable %ptr_ray_query_array Private
599                 %int = OpTypeInt 32 1
600               %int_0 = OpConstant %int 0
601       %ptr_ray_query = OpTypePointer Private %ray_query
602        %accel_struct = OpTypeAccelerationStructureKHR
603    %ptr_accel_struct = OpTypePointer UniformConstant %accel_struct
604          %topLevelAS = OpVariable %ptr_accel_struct UniformConstant
605              %uint_0 = OpConstant %uint 0
606            %uint_255 = OpConstant %uint 255
607               %float = OpTypeFloat 32
608             %v3float = OpTypeVector %float 3
609             %float_0 = OpConstant %float 0
610           %vec3_zero = OpConstantComposite %v3float %float_0 %float_0 %float_0
611             %float_1 = OpConstant %float 1
612       %vec3_xy_0_z_1 = OpConstantComposite %v3float %float_0 %float_0 %float_1
613            %float_10 = OpConstant %float 10
614              %v3uint = OpTypeVector %uint 3
615              %uint_1 = OpConstant %uint 1
616    %gl_WorkGroupSize = OpConstantComposite %v3uint %uint_1 %uint_1 %uint_1
617                %main = OpFunction %void None %func
618          %main_label = OpLabel
619     %first_ray_query = OpAccessChain %ptr_ray_query %rayQueries %int_0
620      %topLevelAS_val = OpLoad %accel_struct %topLevelAS
621                        OpRayQueryInitializeKHR %first_ray_query %topLevelAS_val %uint_0 %uint_255 %vec3_zero %float_0 %vec3_xy_0_z_1 %float_10
622                        OpReturn
623                        OpFunctionEnd
624 )";
625   CompileSuccessfully(shader);
626   EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
627 }
628 
629 }  // namespace
630 }  // namespace val
631 }  // namespace spvtools
632