Skip to content

Commit

Permalink
spirv-val: Add OpConvertUToAccelerationStructureKHR (#4838)
Browse files Browse the repository at this point in the history
  • Loading branch information
sjfricke authored Jul 15, 2022
1 parent e2cf769 commit 93ebf69
Show file tree
Hide file tree
Showing 5 changed files with 158 additions and 4 deletions.
18 changes: 18 additions & 0 deletions source/val/validate_conversion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,24 @@ spv_result_t ConversionPass(ValidationState_t& _, const Instruction* inst) {
break;
}

case SpvOpConvertUToAccelerationStructureKHR: {
if (!_.IsAccelerationStructureType(result_type)) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Expected Result Type to be a Acceleration Structure: "
<< spvOpcodeString(opcode);
}

const uint32_t input_type = _.GetOperandTypeId(inst, 2);
if (!input_type || !_.IsUnsigned64BitHandle(input_type)) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Expected 64-bit uint scalar or 2-component 32-bit uint "
"vector as input: "
<< spvOpcodeString(opcode);
}

break;
}

default:
break;
}
Expand Down
5 changes: 1 addition & 4 deletions source/val/validate_misc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,7 @@ spv_result_t ValidateShaderClock(ValidationState_t& _,
// a vector of two - components of 32 -
// bit unsigned integer type
const uint32_t result_type = inst->type_id();
if (!(_.IsUnsignedIntScalarType(result_type) &&
_.GetBitWidth(result_type) == 64) &&
!(_.IsUnsignedIntVectorType(result_type) &&
_.GetDimension(result_type) == 2 && _.GetBitWidth(result_type) == 32)) {
if (!_.IsUnsigned64BitHandle(result_type)) {
return _.diag(SPV_ERROR_INVALID_DATA, inst) << "Expected Value to be a "
"vector of two components"
" of unsigned integer"
Expand Down
12 changes: 12 additions & 0 deletions source/val/validation_state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -965,6 +965,11 @@ bool ValidationState_t::GetPointerTypeInfo(uint32_t id, uint32_t* data_type,
return true;
}

bool ValidationState_t::IsAccelerationStructureType(uint32_t id) const {
const Instruction* inst = FindDef(id);
return inst && inst->opcode() == SpvOpTypeAccelerationStructureKHR;
}

bool ValidationState_t::IsCooperativeMatrixType(uint32_t id) const {
const Instruction* inst = FindDef(id);
return inst && inst->opcode() == SpvOpTypeCooperativeMatrixNV;
Expand All @@ -985,6 +990,13 @@ bool ValidationState_t::IsUnsignedIntCooperativeMatrixType(uint32_t id) const {
return IsUnsignedIntScalarType(FindDef(id)->word(2));
}

// Either a 32 bit 2-component uint vector or a 64 bit uint scalar
bool ValidationState_t::IsUnsigned64BitHandle(uint32_t id) const {
return ((IsUnsignedIntScalarType(id) && GetBitWidth(id) == 64) ||
(IsUnsignedIntVectorType(id) && GetDimension(id) == 2 &&
GetBitWidth(id) == 32));
}

spv_result_t ValidationState_t::CooperativeMatrixShapesMatch(
const Instruction* inst, uint32_t m1, uint32_t m2) {
const auto m1_type = FindDef(m1);
Expand Down
2 changes: 2 additions & 0 deletions source/val/validation_state.h
Original file line number Diff line number Diff line change
Expand Up @@ -592,10 +592,12 @@ class ValidationState_t {
bool IsBoolVectorType(uint32_t id) const;
bool IsBoolScalarOrVectorType(uint32_t id) const;
bool IsPointerType(uint32_t id) const;
bool IsAccelerationStructureType(uint32_t id) const;
bool IsCooperativeMatrixType(uint32_t id) const;
bool IsFloatCooperativeMatrixType(uint32_t id) const;
bool IsIntCooperativeMatrixType(uint32_t id) const;
bool IsUnsignedIntCooperativeMatrixType(uint32_t id) const;
bool IsUnsigned64BitHandle(uint32_t id) const;

// Returns true if |id| is a type id that contains |type| (or integer or
// floating point type) of |width| bits.
Expand Down
125 changes: 125 additions & 0 deletions test/val/val_conversion_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1641,6 +1641,131 @@ OpFunctionEnd
"integer type to have a 64-bit width for Vulkan environment."));
}

TEST_F(ValidateConversion, ConvertUToAccelerationStructureU32Vec2) {
const std::string extensions = R"(
OpCapability RayQueryKHR
OpExtension "SPV_KHR_ray_query"
)";
const std::string types = R"(
%u32vec2ptr_func = OpTypePointer Function %u32vec2
%typeAS = OpTypeAccelerationStructureKHR
)";
const std::string body = R"(
%asHandle = OpVariable %u32vec2ptr_func Function
%load = OpLoad %u32vec2 %asHandle
%val = OpConvertUToAccelerationStructureKHR %typeAS %load
)";

CompileSuccessfully(GenerateShaderCode(body, extensions, "", types).c_str());
ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
}

TEST_F(ValidateConversion, ConvertUToAccelerationStructureSuccessU64) {
const std::string extensions = R"(
OpCapability RayQueryKHR
OpExtension "SPV_KHR_ray_query"
)";
const std::string types = R"(
%u64_func = OpTypePointer Function %u64
%typeAS = OpTypeAccelerationStructureKHR
)";
const std::string body = R"(
%asHandle = OpVariable %u64_func Function
%load = OpLoad %u64 %asHandle
%val = OpConvertUToAccelerationStructureKHR %typeAS %load
)";

CompileSuccessfully(GenerateShaderCode(body, extensions, "", types).c_str());
ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
}

TEST_F(ValidateConversion, ConvertUToAccelerationStructureResult) {
const std::string extensions = R"(
OpCapability RayQueryKHR
OpExtension "SPV_KHR_ray_query"
)";
const std::string types = R"(
%u32vec2ptr_func = OpTypePointer Function %u32vec2
%typeRQ = OpTypeRayQueryKHR
)";
const std::string body = R"(
%asHandle = OpVariable %u32vec2ptr_func Function
%load = OpLoad %u32vec2 %asHandle
%val = OpConvertUToAccelerationStructureKHR %typeRQ %load
)";

CompileSuccessfully(GenerateShaderCode(body, extensions, "", types).c_str());
ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
EXPECT_THAT(getDiagnosticString(),
HasSubstr("Expected Result Type to be a Acceleration Structure"));
}

TEST_F(ValidateConversion, ConvertUToAccelerationStructureU32) {
const std::string extensions = R"(
OpCapability RayQueryKHR
OpExtension "SPV_KHR_ray_query"
)";
const std::string types = R"(
%u32ptr_func = OpTypePointer Function %u32
%typeAS = OpTypeAccelerationStructureKHR
)";
const std::string body = R"(
%asHandle = OpVariable %u32ptr_func Function
%load = OpLoad %u32 %asHandle
%val = OpConvertUToAccelerationStructureKHR %typeAS %load
)";

CompileSuccessfully(GenerateShaderCode(body, extensions, "", types).c_str());
ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
EXPECT_THAT(getDiagnosticString(),
HasSubstr("Expected 64-bit uint scalar or 2-component 32-bit "
"uint vector as input"));
}

TEST_F(ValidateConversion, ConvertUToAccelerationStructureS64) {
const std::string extensions = R"(
OpCapability RayQueryKHR
OpExtension "SPV_KHR_ray_query"
)";
const std::string types = R"(
%s64ptr_func = OpTypePointer Function %s64
%typeAS = OpTypeAccelerationStructureKHR
)";
const std::string body = R"(
%asHandle = OpVariable %s64ptr_func Function
%load = OpLoad %s64 %asHandle
%val = OpConvertUToAccelerationStructureKHR %typeAS %load
)";

CompileSuccessfully(GenerateShaderCode(body, extensions, "", types).c_str());
ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
EXPECT_THAT(getDiagnosticString(),
HasSubstr("Expected 64-bit uint scalar or 2-component 32-bit "
"uint vector as input"));
}

TEST_F(ValidateConversion, ConvertUToAccelerationStructureS32Vec2) {
const std::string extensions = R"(
OpCapability RayQueryKHR
OpExtension "SPV_KHR_ray_query"
)";
const std::string types = R"(
%s32vec2ptr_func = OpTypePointer Function %s32vec2
%typeAS = OpTypeAccelerationStructureKHR
)";
const std::string body = R"(
%asHandle = OpVariable %s32vec2ptr_func Function
%load = OpLoad %s32vec2 %asHandle
%val = OpConvertUToAccelerationStructureKHR %typeAS %load
)";

CompileSuccessfully(GenerateShaderCode(body, extensions, "", types).c_str());
ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
EXPECT_THAT(getDiagnosticString(),
HasSubstr("Expected 64-bit uint scalar or 2-component 32-bit "
"uint vector as input"));
}

using ValidateSmallConversions = spvtest::ValidateBase<std::string>;

CodeGenerator GetSmallConversionsCodeGenerator() {
Expand Down

0 comments on commit 93ebf69

Please sign in to comment.