Skip to content

Commit 0afcb47

Browse files
committed
Support rayQueryTerminate
1 parent 40b2d30 commit 0afcb47

File tree

6 files changed

+173
-4
lines changed

6 files changed

+173
-4
lines changed

naga/src/back/spv/instructions.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -842,6 +842,12 @@ impl super::Instruction {
842842
instruction
843843
}
844844

845+
pub(super) fn ray_query_terminate(query: Word) -> Self {
846+
let mut instruction = Self::new(Op::RayQueryTerminateKHR);
847+
instruction.add_operand(query);
848+
instruction
849+
}
850+
845851
//
846852
// Conversion Instructions
847853
//

naga/src/back/spv/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,7 @@ enum LookupRayQueryFunction {
459459
ConfirmIntersection,
460460
GetVertexPositions { committed: bool },
461461
GetIntersection { committed: bool },
462+
Terminate,
462463
}
463464

464465
#[derive(Debug)]

naga/src/back/spv/ray.rs

Lines changed: 103 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1781,6 +1781,98 @@ impl Writer {
17811781

17821782
func_id
17831783
}
1784+
1785+
fn write_ray_query_terminate(&mut self) -> spirv::Word {
1786+
if let Some(&word) = self
1787+
.ray_query_functions
1788+
.get(&LookupRayQueryFunction::Terminate)
1789+
{
1790+
return word;
1791+
}
1792+
1793+
let ray_query_type_id = self.get_ray_query_pointer_id();
1794+
1795+
let u32_ty = self.get_u32_type_id();
1796+
let u32_ptr_ty = self.get_pointer_type_id(u32_ty, spirv::StorageClass::Function);
1797+
1798+
let bool_type_id = self.get_bool_type_id();
1799+
1800+
let (func_id, mut function, arg_ids) =
1801+
self.write_function_signature(&[ray_query_type_id, u32_ptr_ty], self.void_type);
1802+
1803+
let query_id = arg_ids[0];
1804+
let init_tracker_id = arg_ids[1];
1805+
1806+
let block_id = self.id_gen.next();
1807+
let mut block = Block::new(block_id);
1808+
1809+
let initialized_tracker_id = self.id_gen.next();
1810+
block.body.push(Instruction::load(
1811+
u32_ty,
1812+
initialized_tracker_id,
1813+
init_tracker_id,
1814+
None,
1815+
));
1816+
1817+
let merge_id = self.id_gen.next();
1818+
let merge_block = Block::new(merge_id);
1819+
1820+
let valid_block_id = self.id_gen.next();
1821+
let mut valid_block = Block::new(valid_block_id);
1822+
1823+
let instruction = if self.ray_query_initialization_tracking {
1824+
let has_proceeded = write_ray_flags_contains_flags(
1825+
self,
1826+
&mut block,
1827+
initialized_tracker_id,
1828+
super::RayQueryPoint::PROCEED.bits(),
1829+
);
1830+
1831+
let finished_proceed_id = write_ray_flags_contains_flags(
1832+
self,
1833+
&mut block,
1834+
initialized_tracker_id,
1835+
super::RayQueryPoint::FINISHED_TRAVERSAL.bits(),
1836+
);
1837+
1838+
// Can't find anything to suggest double calling this function is invalid.
1839+
1840+
let not_finished_id = self.id_gen.next();
1841+
block.body.push(Instruction::unary(
1842+
spirv::Op::LogicalNot,
1843+
bool_type_id,
1844+
not_finished_id,
1845+
finished_proceed_id,
1846+
));
1847+
1848+
let valid_call = self.write_logical_and(&mut block, not_finished_id, has_proceeded);
1849+
1850+
block.body.push(Instruction::selection_merge(
1851+
merge_id,
1852+
spirv::SelectionControl::NONE,
1853+
));
1854+
1855+
Instruction::branch_conditional(valid_call, valid_block_id, merge_id)
1856+
} else {
1857+
Instruction::branch(valid_block_id)
1858+
};
1859+
1860+
function.consume(block, instruction);
1861+
1862+
valid_block
1863+
.body
1864+
.push(Instruction::ray_query_terminate(query_id));
1865+
1866+
function.consume(valid_block, Instruction::branch(merge_id));
1867+
1868+
function.consume(merge_block, Instruction::return_void());
1869+
1870+
function.to_words(&mut self.logical_layout.function_definitions);
1871+
1872+
self.ray_query_functions
1873+
.insert(LookupRayQueryFunction::Proceed, func_id);
1874+
func_id
1875+
}
17841876
}
17851877

17861878
impl BlockContext<'_> {
@@ -1863,7 +1955,17 @@ impl BlockContext<'_> {
18631955
&[query_id, tracker_ids.initialized_tracker],
18641956
));
18651957
}
1866-
crate::RayQueryFunction::Terminate => {}
1958+
crate::RayQueryFunction::Terminate => {
1959+
let id = self.gen_id();
1960+
1961+
let func_id = self.writer.write_ray_query_terminate();
1962+
block.body.push(Instruction::function_call(
1963+
self.writer.void_type,
1964+
id,
1965+
func_id,
1966+
&[query_id, tracker_ids.initialized_tracker],
1967+
));
1968+
}
18671969
}
18681970
}
18691971

naga/tests/out/spv/wgsl-aliased-ray-query.spvasm

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
; SPIR-V
22
; Version: 1.4
33
; Generator: rspirv
4-
; Bound: 244
4+
; Bound: 258
55
OpCapability Shader
66
OpCapability RayQueryKHR
77
OpExtension "SPV_KHR_ray_query"
@@ -308,6 +308,25 @@ OpBranch %231
308308
%231 = OpLabel
309309
OpReturn
310310
OpFunctionEnd
311+
%245 = OpFunction %2 None %225
312+
%246 = OpFunctionParameter %32
313+
%247 = OpFunctionParameter %33
314+
%248 = OpLabel
315+
%249 = OpLoad %7 %247
316+
%252 = OpBitwiseAnd %7 %249 %90
317+
%253 = OpINotEqual %10 %252 %35
318+
%254 = OpBitwiseAnd %7 %249 %23
319+
%255 = OpINotEqual %10 %254 %35
320+
%256 = OpLogicalNot %10 %255
321+
%257 = OpLogicalAnd %10 %256 %253
322+
OpSelectionMerge %250 None
323+
OpBranchConditional %257 %251 %250
324+
%251 = OpLabel
325+
OpRayQueryTerminateKHR %246
326+
OpBranch %250
327+
%250 = OpLabel
328+
OpReturn
329+
OpFunctionEnd
311330
%16 = OpFunction %2 None %17
312331
%15 = OpLabel
313332
%31 = OpVariable %32 Function
@@ -334,6 +353,7 @@ OpBranchConditional %221 %223 %224
334353
%243 = OpFunctionCall %2 %226 %31 %34
335354
OpReturn
336355
%224 = OpLabel
356+
%244 = OpFunctionCall %2 %245 %31 %34
337357
OpReturn
338358
%222 = OpLabel
339359
OpBranch %181

naga/tests/out/spv/wgsl-ray-query-no-init-tracking.spvasm

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
; SPIR-V
22
; Version: 1.4
33
; Generator: rspirv
4-
; Bound: 382
4+
; Bound: 396
55
OpCapability Shader
66
OpCapability RayQueryKHR
77
OpExtension "SPV_KHR_ray_query"
@@ -508,6 +508,25 @@ OpBranch %369
508508
%369 = OpLabel
509509
OpReturn
510510
OpFunctionEnd
511+
%383 = OpFunction %2 None %363
512+
%384 = OpFunctionParameter %32
513+
%385 = OpFunctionParameter %33
514+
%386 = OpLabel
515+
%387 = OpLoad %6 %385
516+
%390 = OpBitwiseAnd %6 %387 %93
517+
%391 = OpINotEqual %8 %390 %35
518+
%392 = OpBitwiseAnd %6 %387 %27
519+
%393 = OpINotEqual %8 %392 %35
520+
%394 = OpLogicalNot %8 %393
521+
%395 = OpLogicalAnd %8 %394 %391
522+
OpSelectionMerge %388 None
523+
OpBranchConditional %395 %389 %388
524+
%389 = OpLabel
525+
OpRayQueryTerminateKHR %384
526+
OpBranch %388
527+
%388 = OpLabel
528+
OpReturn
529+
OpFunctionEnd
511530
%262 = OpFunction %2 None %243
512531
%261 = OpLabel
513532
%266 = OpVariable %32 Function
@@ -534,6 +553,7 @@ OpBranchConditional %359 %361 %362
534553
%381 = OpFunctionCall %2 %364 %266 %267
535554
OpReturn
536555
%362 = OpLabel
556+
%382 = OpFunctionCall %2 %383 %266 %267
537557
OpReturn
538558
%360 = OpLabel
539559
OpBranch %319

naga/tests/out/spv/wgsl-ray-query.spvasm

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
; SPIR-V
22
; Version: 1.4
33
; Generator: rspirv
4-
; Bound: 382
4+
; Bound: 396
55
OpCapability Shader
66
OpCapability RayQueryKHR
77
OpExtension "SPV_KHR_ray_query"
@@ -508,6 +508,25 @@ OpBranch %369
508508
%369 = OpLabel
509509
OpReturn
510510
OpFunctionEnd
511+
%383 = OpFunction %2 None %363
512+
%384 = OpFunctionParameter %32
513+
%385 = OpFunctionParameter %33
514+
%386 = OpLabel
515+
%387 = OpLoad %6 %385
516+
%390 = OpBitwiseAnd %6 %387 %93
517+
%391 = OpINotEqual %8 %390 %35
518+
%392 = OpBitwiseAnd %6 %387 %27
519+
%393 = OpINotEqual %8 %392 %35
520+
%394 = OpLogicalNot %8 %393
521+
%395 = OpLogicalAnd %8 %394 %391
522+
OpSelectionMerge %388 None
523+
OpBranchConditional %395 %389 %388
524+
%389 = OpLabel
525+
OpRayQueryTerminateKHR %384
526+
OpBranch %388
527+
%388 = OpLabel
528+
OpReturn
529+
OpFunctionEnd
511530
%262 = OpFunction %2 None %243
512531
%261 = OpLabel
513532
%266 = OpVariable %32 Function
@@ -534,6 +553,7 @@ OpBranchConditional %359 %361 %362
534553
%381 = OpFunctionCall %2 %364 %266 %267
535554
OpReturn
536555
%362 = OpLabel
556+
%382 = OpFunctionCall %2 %383 %266 %267
537557
OpReturn
538558
%360 = OpLabel
539559
OpBranch %319

0 commit comments

Comments
 (0)