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