-
Notifications
You must be signed in to change notification settings - Fork 669
Expand file tree
/
Copy pathvalidate_ray_tracing.cpp
More file actions
275 lines (244 loc) · 11 KB
/
validate_ray_tracing.cpp
File metadata and controls
275 lines (244 loc) · 11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
// Copyright (c) 2022 The Khronos Group Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Validates ray tracing instructions from SPV_KHR_ray_tracing
#include "source/opcode.h"
#include "source/val/instruction.h"
#include "source/val/validate.h"
#include "source/val/validation_state.h"
namespace spvtools {
namespace val {
spv_result_t RayTracingRayFlagsOperandValue(ValidationState_t& _,
const Instruction* inst,
uint32_t ray_flags_value) {
const auto HasMoreThenOneBitSet =
[ray_flags_value](const spv::RayFlagsMask ray_flag_mask) {
const uint32_t mask =
ray_flags_value & static_cast<uint32_t>(ray_flag_mask);
return mask != 0 && (mask & (mask - 1));
};
if (HasMoreThenOneBitSet(spv::RayFlagsMask::SkipAABBsKHR |
spv::RayFlagsMask::SkipTrianglesKHR)) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Ray Flags contains both SkipTrianglesKHR and SkipAABBsKHR";
}
if (HasMoreThenOneBitSet(spv::RayFlagsMask::SkipTrianglesKHR |
spv::RayFlagsMask::CullFrontFacingTrianglesKHR |
spv::RayFlagsMask::CullBackFacingTrianglesKHR)) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Ray Flags contains more than one of SkipTrianglesKHR or "
"CullFrontFacingTrianglesKHR or CullBackFacingTrianglesKHR";
}
if (HasMoreThenOneBitSet(spv::RayFlagsMask::OpaqueKHR |
spv::RayFlagsMask::NoOpaqueKHR |
spv::RayFlagsMask::CullOpaqueKHR |
spv::RayFlagsMask::CullNoOpaqueKHR)) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Ray Flags contains more than one of OpaqueKHR or NoOpaqueKHR or "
"CullOpaqueKHR or CullNoOpaqueKHR";
}
return SPV_SUCCESS;
}
spv_result_t RayTracingPass(ValidationState_t& _, const Instruction* inst) {
const spv::Op opcode = inst->opcode();
const uint32_t result_type = inst->type_id();
switch (opcode) {
case spv::Op::OpTraceRayKHR: {
_.function(inst->function()->id())
->RegisterExecutionModelLimitation(
[](spv::ExecutionModel model, std::string* message) {
if (model != spv::ExecutionModel::RayGenerationKHR &&
model != spv::ExecutionModel::ClosestHitKHR &&
model != spv::ExecutionModel::MissKHR) {
if (message) {
*message =
"OpTraceRayKHR requires RayGenerationKHR, "
"ClosestHitKHR and MissKHR execution models";
}
return false;
}
return true;
});
if (_.GetIdOpcode(_.GetOperandTypeId(inst, 0)) !=
spv::Op::OpTypeAccelerationStructureKHR) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Expected Acceleration Structure to be of type "
"OpTypeAccelerationStructureKHR";
}
const uint32_t ray_flags = inst->GetOperandAs<uint32_t>(1);
bool is_ray_flags_int32 = false;
bool is_ray_flags_const = false;
uint32_t ray_flags_value = 0;
std::tie(is_ray_flags_int32, is_ray_flags_const, ray_flags_value) =
_.EvalInt32IfConst(ray_flags);
if (!is_ray_flags_int32) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Ray Flags must be a 32-bit int scalar";
} else if (is_ray_flags_const) {
if (auto error =
RayTracingRayFlagsOperandValue(_, inst, ray_flags_value))
return error;
}
const uint32_t cull_mask = _.GetOperandTypeId(inst, 2);
if (!_.IsIntScalarType(cull_mask) || _.GetBitWidth(cull_mask) != 32) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Cull Mask must be a 32-bit int scalar";
}
const uint32_t sbt_offset = _.GetOperandTypeId(inst, 3);
if (!_.IsIntScalarType(sbt_offset) || _.GetBitWidth(sbt_offset) != 32) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "SBT Offset must be a 32-bit int scalar";
}
const uint32_t sbt_stride = _.GetOperandTypeId(inst, 4);
if (!_.IsIntScalarType(sbt_stride) || _.GetBitWidth(sbt_stride) != 32) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "SBT Stride must be a 32-bit int scalar";
}
const uint32_t miss_index = _.GetOperandTypeId(inst, 5);
if (!_.IsIntScalarType(miss_index) || _.GetBitWidth(miss_index) != 32) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Miss Index must be a 32-bit int scalar";
}
const uint32_t ray_origin = _.GetOperandTypeId(inst, 6);
if (!_.IsFloatVectorType(ray_origin) || _.GetDimension(ray_origin) != 3 ||
_.GetBitWidth(ray_origin) != 32) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Ray Origin must be a 32-bit float 3-component vector";
}
const uint32_t ray_tmin = inst->GetOperandAs<uint32_t>(7);
bool is_ray_tmin_float32 = false;
bool is_ray_tmin_const = false;
float ray_tmin_value = 0;
std::tie(is_ray_tmin_float32, is_ray_tmin_const, ray_tmin_value) =
_.EvalFloat32IfConst(ray_tmin);
if (!is_ray_tmin_float32) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Ray TMin must be a 32-bit float scalar";
} else if (is_ray_tmin_const && ray_tmin_value < 0.0f) {
// Don't need to check TMax for being negative because it can't without
// being less than TMin
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Ray Tmin is negative (" << ray_tmin_value << ")";
}
const uint32_t ray_direction = _.GetOperandTypeId(inst, 8);
if (!_.IsFloatVectorType(ray_direction) ||
_.GetDimension(ray_direction) != 3 ||
_.GetBitWidth(ray_direction) != 32) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Ray Direction must be a 32-bit float 3-component vector";
}
const uint32_t ray_tmax = inst->GetOperandAs<uint32_t>(9);
bool is_ray_tmax_float32 = false;
bool is_ray_tmax_const = false;
float ray_tmax_value = 0;
std::tie(is_ray_tmax_float32, is_ray_tmax_const, ray_tmax_value) =
_.EvalFloat32IfConst(ray_tmax);
if (!is_ray_tmax_float32) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Ray TMax must be a 32-bit float scalar";
}
if (is_ray_tmin_const && is_ray_tmax_const &&
ray_tmin_value > ray_tmax_value) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Ray Tmin (" << ray_tmin_value
<< ") is larger than Ray Tmax (" << ray_tmax_value << ")";
}
const Instruction* payload = _.FindDef(inst->GetOperandAs<uint32_t>(10));
if (payload->opcode() != spv::Op::OpVariable) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Payload must be the result of a OpVariable";
} else if (payload->GetOperandAs<spv::StorageClass>(2) !=
spv::StorageClass::RayPayloadKHR &&
payload->GetOperandAs<spv::StorageClass>(2) !=
spv::StorageClass::IncomingRayPayloadKHR) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Payload must have storage class RayPayloadKHR or "
"IncomingRayPayloadKHR";
}
break;
}
case spv::Op::OpReportIntersectionKHR: {
_.function(inst->function()->id())
->RegisterExecutionModelLimitation(
[](spv::ExecutionModel model, std::string* message) {
if (model != spv::ExecutionModel::IntersectionKHR) {
if (message) {
*message =
"OpReportIntersectionKHR requires IntersectionKHR "
"execution model";
}
return false;
}
return true;
});
if (!_.IsBoolScalarType(result_type)) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "expected Result Type to be bool scalar type";
}
const uint32_t hit = _.GetOperandTypeId(inst, 2);
if (!_.IsFloatScalarType(hit) || _.GetBitWidth(hit) != 32) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Hit must be a 32-bit int scalar";
}
const uint32_t hit_kind = _.GetOperandTypeId(inst, 3);
if (!_.IsUnsignedIntScalarType(hit_kind) ||
_.GetBitWidth(hit_kind) != 32) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Hit Kind must be a 32-bit unsigned int scalar";
}
break;
}
case spv::Op::OpExecuteCallableKHR: {
_.function(inst->function()->id())
->RegisterExecutionModelLimitation([](spv::ExecutionModel model,
std::string* message) {
if (model != spv::ExecutionModel::RayGenerationKHR &&
model != spv::ExecutionModel::ClosestHitKHR &&
model != spv::ExecutionModel::MissKHR &&
model != spv::ExecutionModel::CallableKHR) {
if (message) {
*message =
"OpExecuteCallableKHR requires RayGenerationKHR, "
"ClosestHitKHR, MissKHR and CallableKHR execution models";
}
return false;
}
return true;
});
const uint32_t sbt_index = _.GetOperandTypeId(inst, 0);
if (!_.IsUnsignedIntScalarType(sbt_index) ||
_.GetBitWidth(sbt_index) != 32) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "SBT Index must be a 32-bit unsigned int scalar";
}
const auto callable_data = _.FindDef(inst->GetOperandAs<uint32_t>(1));
if (callable_data->opcode() != spv::Op::OpVariable) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Callable Data must be the result of a OpVariable";
} else if (callable_data->GetOperandAs<spv::StorageClass>(2) !=
spv::StorageClass::CallableDataKHR &&
callable_data->GetOperandAs<spv::StorageClass>(2) !=
spv::StorageClass::IncomingCallableDataKHR) {
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Callable Data must have storage class CallableDataKHR or "
"IncomingCallableDataKHR";
}
break;
}
default:
break;
}
return SPV_SUCCESS;
}
} // namespace val
} // namespace spvtools