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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
/*!
Module for code shared between ray queries and ray tracing pipeline code.
*/
pub mod pipeline;
pub mod query;
use alloc::{vec, vec::Vec};
use super::{Block, Function, FunctionArgument, Instruction, LookupFunctionType, Writer};
struct ExtractedRayDesc {
ray_flags_id: spirv::Word,
cull_mask_id: spirv::Word,
tmin_id: spirv::Word,
tmax_id: spirv::Word,
ray_origin_id: spirv::Word,
ray_dir_id: spirv::Word,
valid_id: Option<spirv::Word>,
}
/// helper function to check if a particular flag is set in a u32.
fn write_ray_flags_contains_flags(
writer: &mut Writer,
block: &mut Block,
id: spirv::Word,
flag: u32,
) -> spirv::Word {
let bit_id = writer.get_constant_scalar(crate::Literal::U32(flag));
let zero_id = writer.get_constant_scalar(crate::Literal::U32(0));
let u32_type_id = writer.get_u32_type_id();
let bool_ty = writer.get_bool_type_id();
let and_id = writer.id_gen.next();
block.body.push(Instruction::binary(
spirv::Op::BitwiseAnd,
u32_type_id,
and_id,
id,
bit_id,
));
let eq_id = writer.id_gen.next();
block.body.push(Instruction::binary(
spirv::Op::INotEqual,
bool_ty,
eq_id,
and_id,
zero_id,
));
eq_id
}
impl Writer {
fn write_extract_ray_desc(
&mut self,
block: &mut Block,
desc_id: spirv::Word,
validate: bool,
) -> ExtractedRayDesc {
let bool_type_id = self.get_bool_type_id();
let bool_vec3_type_id = self.get_vec3_bool_type_id();
let f32_type_id = self.get_f32_type_id();
let flag_type_id = self.get_numeric_type_id(super::NumericType::Scalar(crate::Scalar::U32));
//Note: composite extract indices and types must match `generate_ray_desc_type`
let ray_flags_id = self.id_gen.next();
block.body.push(Instruction::composite_extract(
flag_type_id,
ray_flags_id,
desc_id,
&[0],
));
let cull_mask_id = self.id_gen.next();
block.body.push(Instruction::composite_extract(
flag_type_id,
cull_mask_id,
desc_id,
&[1],
));
let tmin_id = self.id_gen.next();
block.body.push(Instruction::composite_extract(
f32_type_id,
tmin_id,
desc_id,
&[2],
));
let tmax_id = self.id_gen.next();
block.body.push(Instruction::composite_extract(
f32_type_id,
tmax_id,
desc_id,
&[3],
));
let vector_type_id = self.get_numeric_type_id(super::NumericType::Vector {
size: crate::VectorSize::Tri,
scalar: crate::Scalar::F32,
});
let ray_origin_id = self.id_gen.next();
block.body.push(Instruction::composite_extract(
vector_type_id,
ray_origin_id,
desc_id,
&[4],
));
let ray_dir_id = self.id_gen.next();
block.body.push(Instruction::composite_extract(
vector_type_id,
ray_dir_id,
desc_id,
&[5],
));
let valid_id = validate.then(||{
let tmin_le_tmax_id = self.id_gen.next();
// Check both that tmin is less than or equal to tmax (https://docs.vulkan.org/spec/latest/appendices/spirvenv.html#VUID-RuntimeSpirv-OpRayQueryInitializeKHR-06350)
// and implicitly that neither tmin or tmax are NaN (https://docs.vulkan.org/spec/latest/appendices/spirvenv.html#VUID-RuntimeSpirv-OpRayQueryInitializeKHR-06351)
// because this checks if tmin and tmax are ordered too (i.e: not NaN).
block.body.push(Instruction::binary(
spirv::Op::FOrdLessThanEqual,
bool_type_id,
tmin_le_tmax_id,
tmin_id,
tmax_id,
));
// Check that tmin is greater than or equal to 0 (and
// therefore also tmax is too because it is greater than
// or equal to tmin) (https://docs.vulkan.org/spec/latest/appendices/spirvenv.html#VUID-RuntimeSpirv-OpRayQueryInitializeKHR-06349).
let tmin_ge_zero_id = self.id_gen.next();
let zero_id = self.get_constant_scalar(crate::Literal::F32(0.0));
block.body.push(Instruction::binary(
spirv::Op::FOrdGreaterThanEqual,
bool_type_id,
tmin_ge_zero_id,
tmin_id,
zero_id,
));
// Check that ray origin is finite (https://docs.vulkan.org/spec/latest/appendices/spirvenv.html#VUID-RuntimeSpirv-OpRayQueryInitializeKHR-06348)
let ray_origin_infinite_id = self.id_gen.next();
block.body.push(Instruction::unary(
spirv::Op::IsInf,
bool_vec3_type_id,
ray_origin_infinite_id,
ray_origin_id,
));
let any_ray_origin_infinite_id = self.id_gen.next();
block.body.push(Instruction::unary(
spirv::Op::Any,
bool_type_id,
any_ray_origin_infinite_id,
ray_origin_infinite_id,
));
let ray_origin_nan_id = self.id_gen.next();
block.body.push(Instruction::unary(
spirv::Op::IsNan,
bool_vec3_type_id,
ray_origin_nan_id,
ray_origin_id,
));
let any_ray_origin_nan_id = self.id_gen.next();
block.body.push(Instruction::unary(
spirv::Op::Any,
bool_type_id,
any_ray_origin_nan_id,
ray_origin_nan_id,
));
let ray_origin_not_finite_id = self.id_gen.next();
block.body.push(Instruction::binary(
spirv::Op::LogicalOr,
bool_type_id,
ray_origin_not_finite_id,
any_ray_origin_nan_id,
any_ray_origin_infinite_id,
));
let all_ray_origin_finite_id = self.id_gen.next();
block.body.push(Instruction::unary(
spirv::Op::LogicalNot,
bool_type_id,
all_ray_origin_finite_id,
ray_origin_not_finite_id,
));
// Check that ray direction is finite (https://docs.vulkan.org/spec/latest/appendices/spirvenv.html#VUID-RuntimeSpirv-OpRayQueryInitializeKHR-06348)
let ray_dir_infinite_id = self.id_gen.next();
block.body.push(Instruction::unary(
spirv::Op::IsInf,
bool_vec3_type_id,
ray_dir_infinite_id,
ray_dir_id,
));
let any_ray_dir_infinite_id = self.id_gen.next();
block.body.push(Instruction::unary(
spirv::Op::Any,
bool_type_id,
any_ray_dir_infinite_id,
ray_dir_infinite_id,
));
let ray_dir_nan_id = self.id_gen.next();
block.body.push(Instruction::unary(
spirv::Op::IsNan,
bool_vec3_type_id,
ray_dir_nan_id,
ray_dir_id,
));
let any_ray_dir_nan_id = self.id_gen.next();
block.body.push(Instruction::unary(
spirv::Op::Any,
bool_type_id,
any_ray_dir_nan_id,
ray_dir_nan_id,
));
let ray_dir_not_finite_id = self.id_gen.next();
block.body.push(Instruction::binary(
spirv::Op::LogicalOr,
bool_type_id,
ray_dir_not_finite_id,
any_ray_dir_nan_id,
any_ray_dir_infinite_id,
));
let all_ray_dir_finite_id = self.id_gen.next();
block.body.push(Instruction::unary(
spirv::Op::LogicalNot,
bool_type_id,
all_ray_dir_finite_id,
ray_dir_not_finite_id,
));
/// Writes spirv to check that less than two booleans are true
///
/// For each boolean: removes it, `and`s it with all others (i.e for all possible combinations of two booleans in the list checks to see if both are true).
/// Then `or`s all of these checks together. This produces whether two or more booleans are true.
fn write_less_than_2_true(
writer: &mut Writer,
block: &mut Block,
mut bools: Vec<spirv::Word>,
) -> spirv::Word {
assert!(bools.len() > 1, "Must have multiple booleans!");
let bool_ty = writer.get_bool_type_id();
let mut each_two_true = Vec::new();
while let Some(last_bool) = bools.pop() {
for &bool in &bools {
let both_true_id = writer.write_logical_and(
block,
last_bool,
bool,
);
each_two_true.push(both_true_id);
}
}
let mut all_or_id = each_two_true.pop().expect("since this must have multiple booleans, there must be at least one thing in `each_two_true`");
for two_true in each_two_true {
let new_all_or_id = writer.id_gen.next();
block.body.push(Instruction::binary(
spirv::Op::LogicalOr,
bool_ty,
new_all_or_id,
all_or_id,
two_true,
));
all_or_id = new_all_or_id;
}
let less_than_two_id = writer.id_gen.next();
block.body.push(Instruction::unary(
spirv::Op::LogicalNot,
bool_ty,
less_than_two_id,
all_or_id,
));
less_than_two_id
}
// Check that at most one of skip triangles and skip AABBs is
// present (https://docs.vulkan.org/spec/latest/appendices/spirvenv.html#VUID-RuntimeSpirv-OpRayQueryInitializeKHR-06889)
let contains_skip_triangles = write_ray_flags_contains_flags(
self,
block,
ray_flags_id,
crate::RayFlag::SKIP_TRIANGLES.bits(),
);
let contains_skip_aabbs = write_ray_flags_contains_flags(
self,
block,
ray_flags_id,
crate::RayFlag::SKIP_AABBS.bits(),
);
let not_contain_skip_triangles_aabbs = write_less_than_2_true(
self,
block,
vec![contains_skip_triangles, contains_skip_aabbs],
);
// Check that at most one of skip triangles (taken from above check),
// cull back facing, and cull front face is present (https://docs.vulkan.org/spec/latest/appendices/spirvenv.html#VUID-RuntimeSpirv-OpRayQueryInitializeKHR-06890)
let contains_cull_back = write_ray_flags_contains_flags(
self,
block,
ray_flags_id,
crate::RayFlag::CULL_BACK_FACING.bits(),
);
let contains_cull_front = write_ray_flags_contains_flags(
self,
block,
ray_flags_id,
crate::RayFlag::CULL_FRONT_FACING.bits(),
);
let not_contain_skip_triangles_cull = write_less_than_2_true(
self,
block,
vec![
contains_skip_triangles,
contains_cull_back,
contains_cull_front,
],
);
// Check that at most one of force opaque, force not opaque, cull opaque,
// and cull not opaque are present (https://docs.vulkan.org/spec/latest/appendices/spirvenv.html#VUID-RuntimeSpirv-OpRayQueryInitializeKHR-06891)
let contains_opaque = write_ray_flags_contains_flags(
self,
block,
ray_flags_id,
crate::RayFlag::FORCE_OPAQUE.bits(),
);
let contains_no_opaque = write_ray_flags_contains_flags(
self,
block,
ray_flags_id,
crate::RayFlag::FORCE_NO_OPAQUE.bits(),
);
let contains_cull_opaque = write_ray_flags_contains_flags(
self,
block,
ray_flags_id,
crate::RayFlag::CULL_OPAQUE.bits(),
);
let contains_cull_no_opaque = write_ray_flags_contains_flags(
self,
block,
ray_flags_id,
crate::RayFlag::CULL_NO_OPAQUE.bits(),
);
let not_contain_multiple_opaque = write_less_than_2_true(
self,
block,
vec![
contains_opaque,
contains_no_opaque,
contains_cull_opaque,
contains_cull_no_opaque,
],
);
// Combine all checks into a single flag saying whether the call is valid or not.
self.write_reduce_and(
block,
vec![
tmin_le_tmax_id,
tmin_ge_zero_id,
all_ray_origin_finite_id,
all_ray_dir_finite_id,
not_contain_skip_triangles_aabbs,
not_contain_skip_triangles_cull,
not_contain_multiple_opaque,
],
)
});
ExtractedRayDesc {
ray_flags_id,
cull_mask_id,
tmin_id,
tmax_id,
ray_origin_id,
ray_dir_id,
valid_id,
}
}
/// writes a logical and of two scalar booleans
fn write_logical_and(
&mut self,
block: &mut Block,
one: spirv::Word,
two: spirv::Word,
) -> spirv::Word {
let id = self.id_gen.next();
let bool_id = self.get_bool_type_id();
block.body.push(Instruction::binary(
spirv::Op::LogicalAnd,
bool_id,
id,
one,
two,
));
id
}
fn write_reduce_and(&mut self, block: &mut Block, mut bools: Vec<spirv::Word>) -> spirv::Word {
// The combined `and`ed together of all of the bools up to this point.
let mut current_combined = bools.pop().unwrap();
for boolean in bools {
current_combined = self.write_logical_and(block, current_combined, boolean)
}
current_combined
}
// returns the id of the function, the function, and ids for its arguments.
fn write_function_signature(
&mut self,
arg_types: &[spirv::Word],
return_ty: spirv::Word,
) -> (spirv::Word, Function, Vec<spirv::Word>) {
let func_ty = self.get_function_type(LookupFunctionType {
parameter_type_ids: Vec::from(arg_types),
return_type_id: return_ty,
});
let mut function = Function::default();
let func_id = self.id_gen.next();
function.signature = Some(Instruction::function(
return_ty,
func_id,
spirv::FunctionControl::empty(),
func_ty,
));
let mut arg_ids = Vec::with_capacity(arg_types.len());
for (idx, &arg_ty) in arg_types.iter().enumerate() {
let id = self.id_gen.next();
let instruction = Instruction::function_parameter(arg_ty, id);
function.parameters.push(FunctionArgument {
instruction,
handle_id: idx as u32,
});
arg_ids.push(id);
}
(func_id, function, arg_ids)
}
}