Skip to content
This repository has been archived by the owner on Jan 29, 2025. It is now read-only.

Ray Query support #2256

Merged
merged 12 commits into from
Mar 23, 2023
28 changes: 28 additions & 0 deletions src/back/dot/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,28 @@ impl StatementGraph {
}
"Atomic"
}
S::RayQuery { query, ref fun } => {
self.dependencies.push((id, query, "query"));
match *fun {
crate::RayQueryFunction::Initialize {
acceleration_structure,
descriptor,
} => {
self.dependencies.push((
id,
acceleration_structure,
"acceleration_structure",
));
self.dependencies.push((id, descriptor, "descriptor"));
"RayQueryInitialize"
}
crate::RayQueryFunction::Proceed { result } => {
self.emits.push((id, result));
"RayQueryProceed"
}
crate::RayQueryFunction::Terminate => "RayQueryTerminate",
}
}
};
// Set the last node to the merge node
last_node = merge_id;
Expand Down Expand Up @@ -550,6 +572,12 @@ fn write_function_expressions(
edges.insert("", expr);
("ArrayLength".into(), 7)
}
E::RayQueryProceedResult => ("rayQueryProceedResult".into(), 4),
E::RayQueryGetIntersection { query, committed } => {
edges.insert("", query);
let ty = if committed { "Committed" } else { "Candidate" };
(format!("rayQueryGet{}Intersection", ty).into(), 4)
}
};

// give uniform expressions an outline
Expand Down
9 changes: 8 additions & 1 deletion src/back/glsl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -879,6 +879,8 @@ impl<'a, W: Write> Writer<'a, W> {
| TypeInner::Struct { .. }
| TypeInner::Image { .. }
| TypeInner::Sampler { .. }
| TypeInner::AccelerationStructure
| TypeInner::RayQuery
| TypeInner::BindingArray { .. } => {
return Err(Error::Custom(format!("Unable to write type {inner:?}")))
}
Expand Down Expand Up @@ -2195,6 +2197,7 @@ impl<'a, W: Write> Writer<'a, W> {
self.write_expr(value, ctx)?;
writeln!(self.out, ");")?;
}
Statement::RayQuery { .. } => unreachable!(),
}

Ok(())
Expand Down Expand Up @@ -3277,13 +3280,17 @@ impl<'a, W: Write> Writer<'a, W> {
}
}
// These expressions never show up in `Emit`.
Expression::CallResult(_) | Expression::AtomicResult { .. } => unreachable!(),
Expression::CallResult(_)
| Expression::AtomicResult { .. }
| Expression::RayQueryProceedResult => unreachable!(),
// `ArrayLength` is written as `expr.length()` and we convert it to a uint
Expression::ArrayLength(expr) => {
write!(self.out, "uint(")?;
self.write_expr(expr, ctx)?;
write!(self.out, ".length())")?
}
// not supported yet
Expression::RayQueryGetIntersection { .. } => unreachable!(),
}

Ok(())
Expand Down
7 changes: 6 additions & 1 deletion src/back/hlsl/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1980,6 +1980,7 @@ impl<'a, W: fmt::Write> super::Writer<'a, W> {

writeln!(self.out, "{level}}}")?
}
Statement::RayQuery { .. } => unreachable!(),
}

Ok(())
Expand Down Expand Up @@ -2878,8 +2879,12 @@ impl<'a, W: fmt::Write> super::Writer<'a, W> {
self.write_expr(module, reject, func_ctx)?;
write!(self.out, ")")?
}
// Not supported yet
Expression::RayQueryGetIntersection { .. } => unreachable!(),
// Nothing to do here, since call expression already cached
Expression::CallResult(_) | Expression::AtomicResult { .. } => {}
Expression::CallResult(_)
| Expression::AtomicResult { .. }
| Expression::RayQueryProceedResult => {}
}

if !closing_bracket.is_empty() {
Expand Down
30 changes: 30 additions & 0 deletions src/back/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,3 +218,33 @@ impl crate::Statement {
}
}
}

bitflags::bitflags! {
/// Ray flags, for a [`RayDesc`]'s `flags` field.
///
/// Note that these exactly correspond to the SPIR-V "Ray Flags" mask, and
/// the SPIR-V backend passes them directly through to the
/// `OpRayQueryInitializeKHR` instruction. (We have to choose something, so
/// we might as well make one back end's life easier.)
///
/// [`RayDesc`]: crate::Module::generate_ray_desc_type
#[derive(Default)]
pub struct RayFlag: u32 {
const OPAQUE = 0x01;
const NO_OPAQUE = 0x02;
const TERMINATE_ON_FIRST_HIT = 0x04;
const SKIP_CLOSEST_HIT_SHADER = 0x08;
const CULL_BACK_FACING = 0x10;
const CULL_FRONT_FACING = 0x20;
const CULL_OPAQUE = 0x40;
const CULL_NO_OPAQUE = 0x80;
const SKIP_TRIANGLES = 0x100;
const SKIP_AABBS = 0x200;
}
}

#[repr(u32)]
enum RayIntersectionType {
Triangle = 1,
BoundingBox = 4,
}
10 changes: 2 additions & 8 deletions src/back/msl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,10 +314,7 @@ impl Options {
match slot {
Some(slot) => Ok(ResolvedBinding::Resource(BindTarget {
buffer: Some(slot),
texture: None,
sampler: None,
binding_array_size: None,
mutable: false,
..Default::default()
})),
None if self.fake_missing_bindings => Ok(ResolvedBinding::User {
prefix: "fake",
Expand All @@ -338,10 +335,7 @@ impl Options {
match slot {
Some(slot) => Ok(ResolvedBinding::Resource(BindTarget {
buffer: Some(slot),
texture: None,
sampler: None,
binding_array_size: None,
mutable: false,
..Default::default()
})),
None if self.fake_missing_bindings => Ok(ResolvedBinding::User {
prefix: "fake",
Expand Down
Loading