1 // Copyright (c) 2022 The Khronos Group Inc.
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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.
15 // Validates ray tracing instructions from SPV_KHR_ray_tracing
17 #include "source/opcode.h"
18 #include "source/val/instruction.h"
19 #include "source/val/validate.h"
20 #include "source/val/validation_state.h"
25 spv_result_t
RayTracingPass(ValidationState_t
& _
, const Instruction
* inst
) {
26 const spv::Op opcode
= inst
->opcode();
27 const uint32_t result_type
= inst
->type_id();
30 case spv::Op::OpTraceRayKHR
: {
31 _
.function(inst
->function()->id())
32 ->RegisterExecutionModelLimitation(
33 [](spv::ExecutionModel model
, std::string
* message
) {
34 if (model
!= spv::ExecutionModel::RayGenerationKHR
&&
35 model
!= spv::ExecutionModel::ClosestHitKHR
&&
36 model
!= spv::ExecutionModel::MissKHR
) {
39 "OpTraceRayKHR requires RayGenerationKHR, "
40 "ClosestHitKHR and MissKHR execution models";
47 if (_
.GetIdOpcode(_
.GetOperandTypeId(inst
, 0)) !=
48 spv::Op::OpTypeAccelerationStructureKHR
) {
49 return _
.diag(SPV_ERROR_INVALID_DATA
, inst
)
50 << "Expected Acceleration Structure to be of type "
51 "OpTypeAccelerationStructureKHR";
54 const uint32_t ray_flags
= _
.GetOperandTypeId(inst
, 1);
55 if (!_
.IsIntScalarType(ray_flags
) || _
.GetBitWidth(ray_flags
) != 32) {
56 return _
.diag(SPV_ERROR_INVALID_DATA
, inst
)
57 << "Ray Flags must be a 32-bit int scalar";
60 const uint32_t cull_mask
= _
.GetOperandTypeId(inst
, 2);
61 if (!_
.IsIntScalarType(cull_mask
) || _
.GetBitWidth(cull_mask
) != 32) {
62 return _
.diag(SPV_ERROR_INVALID_DATA
, inst
)
63 << "Cull Mask must be a 32-bit int scalar";
66 const uint32_t sbt_offset
= _
.GetOperandTypeId(inst
, 3);
67 if (!_
.IsIntScalarType(sbt_offset
) || _
.GetBitWidth(sbt_offset
) != 32) {
68 return _
.diag(SPV_ERROR_INVALID_DATA
, inst
)
69 << "SBT Offset must be a 32-bit int scalar";
72 const uint32_t sbt_stride
= _
.GetOperandTypeId(inst
, 4);
73 if (!_
.IsIntScalarType(sbt_stride
) || _
.GetBitWidth(sbt_stride
) != 32) {
74 return _
.diag(SPV_ERROR_INVALID_DATA
, inst
)
75 << "SBT Stride must be a 32-bit int scalar";
78 const uint32_t miss_index
= _
.GetOperandTypeId(inst
, 5);
79 if (!_
.IsIntScalarType(miss_index
) || _
.GetBitWidth(miss_index
) != 32) {
80 return _
.diag(SPV_ERROR_INVALID_DATA
, inst
)
81 << "Miss Index must be a 32-bit int scalar";
84 const uint32_t ray_origin
= _
.GetOperandTypeId(inst
, 6);
85 if (!_
.IsFloatVectorType(ray_origin
) || _
.GetDimension(ray_origin
) != 3 ||
86 _
.GetBitWidth(ray_origin
) != 32) {
87 return _
.diag(SPV_ERROR_INVALID_DATA
, inst
)
88 << "Ray Origin must be a 32-bit float 3-component vector";
91 const uint32_t ray_tmin
= _
.GetOperandTypeId(inst
, 7);
92 if (!_
.IsFloatScalarType(ray_tmin
) || _
.GetBitWidth(ray_tmin
) != 32) {
93 return _
.diag(SPV_ERROR_INVALID_DATA
, inst
)
94 << "Ray TMin must be a 32-bit float scalar";
97 const uint32_t ray_direction
= _
.GetOperandTypeId(inst
, 8);
98 if (!_
.IsFloatVectorType(ray_direction
) ||
99 _
.GetDimension(ray_direction
) != 3 ||
100 _
.GetBitWidth(ray_direction
) != 32) {
101 return _
.diag(SPV_ERROR_INVALID_DATA
, inst
)
102 << "Ray Direction must be a 32-bit float 3-component vector";
105 const uint32_t ray_tmax
= _
.GetOperandTypeId(inst
, 9);
106 if (!_
.IsFloatScalarType(ray_tmax
) || _
.GetBitWidth(ray_tmax
) != 32) {
107 return _
.diag(SPV_ERROR_INVALID_DATA
, inst
)
108 << "Ray TMax must be a 32-bit float scalar";
111 const Instruction
* payload
= _
.FindDef(inst
->GetOperandAs
<uint32_t>(10));
112 if (payload
->opcode() != spv::Op::OpVariable
) {
113 return _
.diag(SPV_ERROR_INVALID_DATA
, inst
)
114 << "Payload must be the result of a OpVariable";
115 } else if (payload
->GetOperandAs
<spv::StorageClass
>(2) !=
116 spv::StorageClass::RayPayloadKHR
&&
117 payload
->GetOperandAs
<spv::StorageClass
>(2) !=
118 spv::StorageClass::IncomingRayPayloadKHR
) {
119 return _
.diag(SPV_ERROR_INVALID_DATA
, inst
)
120 << "Payload must have storage class RayPayloadKHR or "
121 "IncomingRayPayloadKHR";
126 case spv::Op::OpReportIntersectionKHR
: {
127 _
.function(inst
->function()->id())
128 ->RegisterExecutionModelLimitation(
129 [](spv::ExecutionModel model
, std::string
* message
) {
130 if (model
!= spv::ExecutionModel::IntersectionKHR
) {
133 "OpReportIntersectionKHR requires IntersectionKHR "
141 if (!_
.IsBoolScalarType(result_type
)) {
142 return _
.diag(SPV_ERROR_INVALID_DATA
, inst
)
143 << "expected Result Type to be bool scalar type";
146 const uint32_t hit
= _
.GetOperandTypeId(inst
, 2);
147 if (!_
.IsFloatScalarType(hit
) || _
.GetBitWidth(hit
) != 32) {
148 return _
.diag(SPV_ERROR_INVALID_DATA
, inst
)
149 << "Hit must be a 32-bit int scalar";
152 const uint32_t hit_kind
= _
.GetOperandTypeId(inst
, 3);
153 if (!_
.IsUnsignedIntScalarType(hit_kind
) ||
154 _
.GetBitWidth(hit_kind
) != 32) {
155 return _
.diag(SPV_ERROR_INVALID_DATA
, inst
)
156 << "Hit Kind must be a 32-bit unsigned int scalar";
161 case spv::Op::OpExecuteCallableKHR
: {
162 _
.function(inst
->function()->id())
163 ->RegisterExecutionModelLimitation([](spv::ExecutionModel model
,
164 std::string
* message
) {
165 if (model
!= spv::ExecutionModel::RayGenerationKHR
&&
166 model
!= spv::ExecutionModel::ClosestHitKHR
&&
167 model
!= spv::ExecutionModel::MissKHR
&&
168 model
!= spv::ExecutionModel::CallableKHR
) {
171 "OpExecuteCallableKHR requires RayGenerationKHR, "
172 "ClosestHitKHR, MissKHR and CallableKHR execution models";
179 const uint32_t sbt_index
= _
.GetOperandTypeId(inst
, 0);
180 if (!_
.IsUnsignedIntScalarType(sbt_index
) ||
181 _
.GetBitWidth(sbt_index
) != 32) {
182 return _
.diag(SPV_ERROR_INVALID_DATA
, inst
)
183 << "SBT Index must be a 32-bit unsigned int scalar";
186 const auto callable_data
= _
.FindDef(inst
->GetOperandAs
<uint32_t>(1));
187 if (callable_data
->opcode() != spv::Op::OpVariable
) {
188 return _
.diag(SPV_ERROR_INVALID_DATA
, inst
)
189 << "Callable Data must be the result of a OpVariable";
190 } else if (callable_data
->GetOperandAs
<spv::StorageClass
>(2) !=
191 spv::StorageClass::CallableDataKHR
&&
192 callable_data
->GetOperandAs
<spv::StorageClass
>(2) !=
193 spv::StorageClass::IncomingCallableDataKHR
) {
194 return _
.diag(SPV_ERROR_INVALID_DATA
, inst
)
195 << "Callable Data must have storage class CallableDataKHR or "
196 "IncomingCallableDataKHR";
209 } // namespace spvtools