mirror of
https://github.com/storytold/wgpu.git
synced 2026-11-21 23:11:01 +01:00
Prevent ray queries hoisted out of loops from generating invalid code (#9945)
This commit is contained in:
1 parent
b82df96eb3
commit
ef0a0a230f
22 files changed
+707
-17
No files matched your search
@@ -161,6 +161,7 @@ By @sagudev in [#10115](https://github.com/gfx-rs/wgpu/pull/10115).
|
||||
- Reject return types on compute shader entrypoints. By @ErichDonGubler in [#10026](https://github.com/gfx-rs/wgpu/pull/10026).
|
||||
- Reject `@location(…)`s in compute shaders. By @ErichDonGubler in [#10026](https://github.com/gfx-rs/wgpu/pull/10026).
|
||||
- Correctly emit primitive_index for the SPIR-V backend, handling mesh and raytracing shaders. Before, you could not use primitive_index with these shader types. An enable primitive_index statement is still required in wgsl shaders, in addition to enable wgpu_mesh_shader/enable wgpu_ray_tracing_pipeline. By @JMS55 in [#10153](https://github.com/gfx-rs/wgpu/pull/10153).
|
||||
- Prevent invalid IR from being generated when using a ray query in a loop. By @Vecvec in [#9945](https://github.com/gfx-rs/wgpu/pull/9945)
|
||||
|
||||
#### DX12
|
||||
|
||||
|
||||
@@ -305,6 +305,7 @@ impl StatementGraph {
|
||||
"RayQueryConfirmIntersection"
|
||||
}
|
||||
crate::RayQueryFunction::Terminate => "RayQueryTerminate",
|
||||
crate::RayQueryFunction::Begin => "RayQueryVariableUsageBegins",
|
||||
}
|
||||
}
|
||||
S::SubgroupBallot { result, predicate } => {
|
||||
|
||||
@@ -2911,6 +2911,11 @@ impl<'a, W: fmt::Write> super::Writer<'a, W> {
|
||||
RayQueryFunction::Terminate => {
|
||||
self.write_terminate(module, level, query, &tracker_expr_name, func_ctx)?;
|
||||
}
|
||||
RayQueryFunction::Begin => {
|
||||
if self.options.ray_query_initialization_tracking {
|
||||
writeln!(self.out, "{level}{tracker_expr_name} = 0u;")?;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Statement::SubgroupBallot { result, predicate } => {
|
||||
|
||||
@@ -526,6 +526,14 @@ impl<W: Write> Writer<W> {
|
||||
writeln!(self.out, "{level}}}")?;
|
||||
}
|
||||
}
|
||||
// Reset the initialization tracker to have no flags set which is equivalent to having
|
||||
// a new variable as nothing else should be used if the ray query is marked as
|
||||
// uninitialized.
|
||||
crate::RayQueryFunction::Begin => {
|
||||
if context.expression.ray_query_initialization_tracking {
|
||||
writeln!(self.out, "{level}{tracker_expr_name} = 0u;")?;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
||||
@@ -905,6 +905,7 @@ fn adjust_stmt(new_pos: &HandleVec<Expression, Handle<Expression>>, stmt: &mut S
|
||||
}
|
||||
crate::RayQueryFunction::ConfirmIntersection => {}
|
||||
crate::RayQueryFunction::Terminate => {}
|
||||
crate::RayQueryFunction::Begin => {}
|
||||
}
|
||||
}
|
||||
Statement::CooperativeStore {
|
||||
|
||||
@@ -1584,6 +1584,21 @@ impl BlockContext<'_> {
|
||||
&[query_id, tracker_ids.initialized_tracker],
|
||||
));
|
||||
}
|
||||
crate::RayQueryFunction::Begin => {
|
||||
// Reset the ray query initialization tracker to zero as this is equivalent to it being a new variable.
|
||||
// If initialization tracking is disabled, then the previous state doesn't matter as it is UB.
|
||||
if self.writer.ray_query_initialization_tracking {
|
||||
let zero_value = self
|
||||
.writer
|
||||
.get_constant_scalar(crate::Literal::U32(RayQueryPoint::empty().bits()));
|
||||
|
||||
block.body.push(Instruction::store(
|
||||
tracker_ids.initialized_tracker,
|
||||
zero_value,
|
||||
None,
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -218,6 +218,7 @@ impl FunctionTracer<'_> {
|
||||
}
|
||||
Qf::ConfirmIntersection => {}
|
||||
Qf::Terminate => {}
|
||||
Qf::Begin => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -456,6 +457,7 @@ impl FunctionMap {
|
||||
}
|
||||
Qf::ConfirmIntersection => {}
|
||||
Qf::Terminate => {}
|
||||
Qf::Begin => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -603,6 +603,7 @@ pub(crate) enum Error<'a> {
|
||||
UnexpectedExprForTypeExpression(Span),
|
||||
MissingIncomingPayload(Span),
|
||||
UnterminatedBlockComment(Span),
|
||||
RayQueryWithInitializer(Span),
|
||||
}
|
||||
|
||||
impl From<ConflictingDiagnosticRuleError> for Error<'_> {
|
||||
@@ -1682,6 +1683,14 @@ impl<'a> Error<'a> {
|
||||
message: "unterminated block comment".into(),
|
||||
notes: vec![],
|
||||
},
|
||||
Error::RayQueryWithInitializer(span) => ParseError {
|
||||
message: "Ray query with initialize".into(),
|
||||
labels: vec![(
|
||||
*span,
|
||||
"variables with type `ray_query` are special and so cannot have initializers".into(),
|
||||
)],
|
||||
notes: vec![],
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1860,6 +1860,18 @@ impl<'source, 'temp> Lowerer<'source, 'temp> {
|
||||
ctx.named_expressions
|
||||
.insert(initializer, (l.name.name.to_string(), l.name.span));
|
||||
|
||||
if matches!(
|
||||
ctx.module.types[ty].inner,
|
||||
crate::TypeInner::RayQuery { .. }
|
||||
) {
|
||||
// If a `let` variable is a ray query, it must be invalid as a `let`
|
||||
// must have an initializer (it is also pretty useless as all other
|
||||
// operations are disallowed, or require write-able variables).
|
||||
return Err(Box::new(Error::RayQueryWithInitializer(
|
||||
ctx.function.expressions.get_span(initializer),
|
||||
)));
|
||||
}
|
||||
|
||||
return Ok(());
|
||||
}
|
||||
ast::LocalDecl::Var(ref v) => {
|
||||
@@ -1916,27 +1928,59 @@ impl<'source, 'temp> Lowerer<'source, 'temp> {
|
||||
let handle = ctx
|
||||
.as_expression(block, &mut emitter)
|
||||
.interrupt_emitter(ir::Expression::LocalVariable(var), Span::UNDEFINED)?;
|
||||
let initializer = if is_inside_loop {
|
||||
match initializer {
|
||||
Some(initializer) => Some(initializer),
|
||||
None => Some(
|
||||
ctx.as_expression(block, &mut emitter)
|
||||
.append_expression(ir::Expression::ZeroValue(ty), stmt.span)?,
|
||||
),
|
||||
}
|
||||
} else {
|
||||
initializer
|
||||
};
|
||||
|
||||
block.extend(emitter.finish(&ctx.function.expressions));
|
||||
ctx.local_table
|
||||
.insert(v.handle, Declared::Runtime(Typed::Reference(handle)));
|
||||
|
||||
match initializer {
|
||||
Some(initializer) => ir::Statement::Store {
|
||||
pointer: handle,
|
||||
value: initializer,
|
||||
},
|
||||
None => return Ok(()),
|
||||
match ctx.module.types[ty].inner {
|
||||
crate::TypeInner::RayQuery { .. } => {
|
||||
// Initializers are disallowed for ray queries as any store is disallowed.
|
||||
// However, in loops ray queries need to be reset using a special piece of
|
||||
// IR.
|
||||
|
||||
// Because we have a special case for ray queries, and initializers are always
|
||||
// disallowed for ray queries, we remove them here. This prevents having to
|
||||
// special-case them and then just emitting invalid IR anyway and gives a
|
||||
// clearer error message.
|
||||
if let Some(expr) = initializer {
|
||||
return Err(Box::new(Error::RayQueryWithInitializer(
|
||||
ctx.function.expressions.get_span(expr),
|
||||
)));
|
||||
}
|
||||
|
||||
if is_inside_loop {
|
||||
ir::Statement::RayQuery {
|
||||
query: handle,
|
||||
fun: ir::RayQueryFunction::Begin,
|
||||
}
|
||||
} else {
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
let initializer = if is_inside_loop {
|
||||
match initializer {
|
||||
Some(initializer) => Some(initializer),
|
||||
None => Some(
|
||||
ctx.as_expression(block, &mut emitter).append_expression(
|
||||
ir::Expression::ZeroValue(ty),
|
||||
stmt.span,
|
||||
)?,
|
||||
),
|
||||
}
|
||||
} else {
|
||||
initializer
|
||||
};
|
||||
|
||||
match initializer {
|
||||
Some(initializer) => ir::Statement::Store {
|
||||
pointer: handle,
|
||||
value: initializer,
|
||||
},
|
||||
None => return Ok(()),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
ast::LocalDecl::Const(ref c) => {
|
||||
|
||||
@@ -2005,6 +2005,14 @@ pub enum RayQueryFunction {
|
||||
ConfirmIntersection,
|
||||
|
||||
Terminate,
|
||||
|
||||
/// Resets a variable's to appear as though it was previously unused.
|
||||
///
|
||||
/// For most types in variables which don't have initialisers, a zero store
|
||||
/// is fine when resetting a variable in a loop (as functions always have
|
||||
/// their variables outside all blocks while wgsl and others can have blocks
|
||||
/// in loops) but ray queries cannot be stored to nor can be zero initialized.
|
||||
Begin,
|
||||
}
|
||||
|
||||
//TODO: consider removing `Clone`. It's not valid to clone `Statement::Emit` anyway.
|
||||
|
||||
@@ -1142,6 +1142,7 @@ impl FunctionInfo {
|
||||
}
|
||||
crate::RayQueryFunction::ConfirmIntersection => {}
|
||||
crate::RayQueryFunction::Terminate => {}
|
||||
crate::RayQueryFunction::Begin => {}
|
||||
}
|
||||
FunctionUniformity::new()
|
||||
}
|
||||
|
||||
@@ -525,6 +525,9 @@ impl super::Validator {
|
||||
if !mod_info[ty].contains(TypeFlags::CONSTRUCTIBLE) {
|
||||
return Err(ExpressionError::InvalidZeroValue(ty));
|
||||
}
|
||||
if matches!(module.types[ty].inner, crate::TypeInner::RayQuery { .. }) {
|
||||
return Err(ExpressionError::InvalidZeroValue(ty));
|
||||
}
|
||||
ShaderStages::all()
|
||||
}
|
||||
E::Compose { ref components, ty } => {
|
||||
|
||||
@@ -85,6 +85,8 @@ pub enum LocalVariableError {
|
||||
InitializerType,
|
||||
#[error("Initializer is not a const or override expression")]
|
||||
NonConstOrOverrideInitializer,
|
||||
#[error("Local variable has a type `ray_query` and so cannot be initialized.")]
|
||||
RayQueryWithInitializeExpression,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, thiserror::Error)]
|
||||
@@ -236,6 +238,8 @@ pub enum FunctionError {
|
||||
MismatchedPayloadType(Handle<crate::Type>, Handle<crate::Type>),
|
||||
#[error("The payload passed to `traceRay` must be a pointer directly to a global variable")]
|
||||
PayloadPointerNotGlobal,
|
||||
#[error("Tried to store to pointer {0:?} which is a ray query and so cannot be assigned to")]
|
||||
RayQueryStore(Handle<crate::Expression>),
|
||||
}
|
||||
|
||||
bitflags::bitflags! {
|
||||
@@ -1095,6 +1099,13 @@ impl super::Validator {
|
||||
let good = if let Some(&Ti::Atomic(ref scalar)) = pointer_base_ty {
|
||||
// The Naga IR allows storing a scalar to an atomic.
|
||||
*value_ty == Ti::Scalar(*scalar)
|
||||
} else if let Some(&Ti::RayQuery { .. }) = pointer_base_ty {
|
||||
return Err(FunctionError::RayQueryStore(pointer)
|
||||
.with_span_context((
|
||||
context.expressions.get_span(pointer),
|
||||
format!("this pointer has a base type of {pointer_base_ty:?} which cannot be stored to"),
|
||||
))
|
||||
.with_span(span, "store to a type which is not allowed to be stored to"));
|
||||
} else if let Some(tr) = pointer_base_tr {
|
||||
context.compare_types(value_tr, &tr)
|
||||
} else {
|
||||
@@ -1577,6 +1588,7 @@ impl super::Validator {
|
||||
}
|
||||
crate::RayQueryFunction::ConfirmIntersection => {}
|
||||
crate::RayQueryFunction::Terminate => {}
|
||||
crate::RayQueryFunction::Begin => {}
|
||||
}
|
||||
}
|
||||
S::SubgroupBallot { result, predicate } => {
|
||||
@@ -1813,6 +1825,10 @@ impl super::Validator {
|
||||
if !local_expr_kind.is_const_or_override(init) {
|
||||
return Err(LocalVariableError::NonConstOrOverrideInitializer);
|
||||
}
|
||||
|
||||
if matches!(gctx.types[var.ty].inner, crate::TypeInner::RayQuery { .. }) {
|
||||
return Err(LocalVariableError::RayQueryWithInitializeExpression);
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
||||
@@ -824,6 +824,7 @@ impl super::Validator {
|
||||
}
|
||||
crate::RayQueryFunction::ConfirmIntersection => {}
|
||||
crate::RayQueryFunction::Terminate => {}
|
||||
crate::RayQueryFunction::Begin => {}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
capabilities = "RAY_QUERY"
|
||||
targets = "METAL | SPIRV | HLSL"
|
||||
|
||||
[msl]
|
||||
lang_version = [2, 4]
|
||||
|
||||
[hlsl]
|
||||
shader_model = "V6_5"
|
||||
@@ -0,0 +1,12 @@
|
||||
enable wgpu_ray_query;
|
||||
|
||||
@group(0) @binding(0) var acc: acceleration_structure;
|
||||
|
||||
@compute @workgroup_size(1)
|
||||
fn main() {
|
||||
for (var i: u32 = 0u; i < 4u; i++) {
|
||||
var sq: ray_query;
|
||||
rayQueryInitialize(&sq, acc, RayDesc(0x01u, 0xFFu, 0.001, 1000.0, vec3(f32(i)), vec3(0.0, -1.0, 0.0)));
|
||||
rayQueryProceed(&sq);
|
||||
}
|
||||
}
|
||||
@@ -5748,3 +5748,70 @@ fn user_locations_not_accepted_in_compute_entry_point_arguments() {
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ray_query_store() {
|
||||
// ray queries cannot be stored to despite them being a `var`
|
||||
check_validation! {
|
||||
r#"
|
||||
enable wgpu_ray_query;
|
||||
|
||||
@compute @workgroup_size(1)
|
||||
fn main() {
|
||||
var rq: ray_query;
|
||||
var rq_2: ray_query;
|
||||
rq = rq_2;
|
||||
}
|
||||
"#:
|
||||
Err(naga::valid::ValidationError::EntryPoint {
|
||||
stage: naga::ShaderStage::Compute,
|
||||
source: naga::valid::EntryPointError::Function(
|
||||
naga::valid::FunctionError::RayQueryStore(_)
|
||||
),
|
||||
..
|
||||
}),
|
||||
Capabilities::RAY_QUERY
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ray_query_initializer() {
|
||||
// ray queries cannot have initializers
|
||||
check_validation! {
|
||||
r#"
|
||||
enable wgpu_ray_query;
|
||||
|
||||
@compute @workgroup_size(1)
|
||||
fn main() {
|
||||
var rq: ray_query = ray_query();
|
||||
}
|
||||
"#:
|
||||
Err(naga::valid::ValidationError::EntryPoint {
|
||||
stage: naga::ShaderStage::Compute,
|
||||
source: naga::valid::EntryPointError::Function(
|
||||
naga::valid::FunctionError::LocalVariable {
|
||||
source: naga::valid::LocalVariableError::RayQueryWithInitializeExpression,
|
||||
..
|
||||
},
|
||||
),
|
||||
..
|
||||
}),
|
||||
Capabilities::RAY_QUERY
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ray_query_let() {
|
||||
check_error_matches(
|
||||
"
|
||||
enable wgpu_ray_query;
|
||||
|
||||
@compute @workgroup_size(1)
|
||||
fn main() {
|
||||
var rq: ray_query;
|
||||
let _rq_1 = rq;
|
||||
}
|
||||
",
|
||||
"Ray query with initialize",
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
struct RayDesc_ {
|
||||
uint flags;
|
||||
uint cull_mask;
|
||||
float tmin;
|
||||
float tmax;
|
||||
float3 origin;
|
||||
int _pad5_0;
|
||||
float3 dir;
|
||||
int _end_pad_0;
|
||||
};
|
||||
|
||||
RayDesc RayDescFromRayDesc_(RayDesc_ arg0) {
|
||||
RayDesc ret = (RayDesc)0;
|
||||
ret.Origin = arg0.origin;
|
||||
ret.TMin = arg0.tmin;
|
||||
ret.Direction = arg0.dir;
|
||||
ret.TMax = arg0.tmax;
|
||||
return ret;
|
||||
}
|
||||
|
||||
RaytracingAccelerationStructure acc : register(t0);
|
||||
|
||||
RayDesc_ ConstructRayDesc_(uint arg0, uint arg1, float arg2, float arg3, float3 arg4, float3 arg5) {
|
||||
RayDesc_ ret = (RayDesc_)0;
|
||||
ret.flags = arg0;
|
||||
ret.cull_mask = arg1;
|
||||
ret.tmin = arg2;
|
||||
ret.tmax = arg3;
|
||||
ret.origin = arg4;
|
||||
ret.dir = arg5;
|
||||
return ret;
|
||||
}
|
||||
|
||||
[numthreads(1, 1, 1)]
|
||||
void main()
|
||||
{
|
||||
uint i = 0u;
|
||||
RayQuery<RAY_FLAG_NONE> sq;
|
||||
uint naga_query_init_tracker_for_sq = 0;
|
||||
|
||||
uint2 loop_bound = uint2(4294967295u, 4294967295u);
|
||||
bool loop_init = true;
|
||||
while(true) {
|
||||
if (all(loop_bound == uint2(0u, 0u))) { break; }
|
||||
loop_bound -= uint2(loop_bound.y == 0u, 1u);
|
||||
if (!loop_init) {
|
||||
uint _e21 = i;
|
||||
i = (_e21 + 1u);
|
||||
}
|
||||
loop_init = false;
|
||||
uint _e2 = i;
|
||||
if ((_e2 < 4u)) {
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
{
|
||||
naga_query_init_tracker_for_sq = 0u;
|
||||
uint _e9 = i;
|
||||
{
|
||||
RayDesc_ naga_desc = ConstructRayDesc_(1u, 255u, 0.001, 1000.0, (float(_e9)).xxx, float3(0.0, -1.0, 0.0));
|
||||
float naga_tmin = naga_desc.tmin;
|
||||
float naga_tmax = naga_desc.tmax;
|
||||
float3 naga_origin = naga_desc.origin;
|
||||
float3 naga_dir = naga_desc.dir;
|
||||
uint naga_flags = naga_desc.flags;
|
||||
bool naga_tmin_valid = (naga_tmin >= 0.0) && (naga_tmin <= naga_tmax) && !(((asuint(naga_tmin) & 2139095040) == 2139095040) && ((asuint(naga_tmin) & 0x7fffff) != 0));
|
||||
bool naga_tmax_valid = !(((asuint(naga_tmax) & 2139095040) == 2139095040) && ((asuint(naga_tmax) & 0x7fffff) != 0));
|
||||
bool naga_origin_valid = !any((((asuint(naga_origin) & 2139095040) == 2139095040) && ((asuint(naga_origin) & 0x7fffff) != 0)));
|
||||
bool naga_dir_valid = !any((((asuint(naga_dir) & 2139095040) == 2139095040) && ((asuint(naga_dir) & 0x7fffff) != 0)));
|
||||
bool naga_contains_opaque = ((naga_flags & 1) == 1);
|
||||
bool naga_contains_no_opaque = ((naga_flags & 2) == 2);
|
||||
bool naga_contains_cull_opaque = ((naga_flags & 64) == 64);
|
||||
bool naga_contains_cull_no_opaque = ((naga_flags & 128) == 128);
|
||||
bool naga_contains_cull_front = ((naga_flags & 32) == 32);
|
||||
bool naga_contains_cull_back = ((naga_flags & 16) == 16);
|
||||
bool naga_contains_skip_triangles = ((naga_flags & 256) == 256);
|
||||
bool naga_contains_skip_aabbs = ((naga_flags & 512) == 512);
|
||||
bool naga_contains_skip_triangles_aabbs = (naga_contains_skip_aabbs && naga_contains_skip_triangles) ;
|
||||
bool naga_contains_skip_triangles_cull = (naga_contains_cull_front && naga_contains_skip_triangles) || (naga_contains_cull_front && naga_contains_cull_back) || (naga_contains_cull_back && naga_contains_skip_triangles) ;
|
||||
bool naga_contains_multiple_opaque = (naga_contains_cull_no_opaque && naga_contains_opaque) || (naga_contains_cull_no_opaque && naga_contains_no_opaque) || (naga_contains_cull_no_opaque && naga_contains_cull_opaque) || (naga_contains_cull_opaque && naga_contains_opaque) || (naga_contains_cull_opaque && naga_contains_no_opaque) || (naga_contains_no_opaque && naga_contains_opaque) ;
|
||||
if (naga_tmin_valid && naga_tmax_valid && naga_origin_valid && naga_dir_valid && !(naga_contains_skip_triangles_aabbs || naga_contains_skip_triangles_cull || naga_contains_multiple_opaque)) {
|
||||
naga_query_init_tracker_for_sq = naga_query_init_tracker_for_sq | 1;
|
||||
sq.TraceRayInline(acc, naga_desc.flags, naga_desc.cull_mask, RayDescFromRayDesc_(naga_desc));
|
||||
}
|
||||
}
|
||||
bool _e19 = false;
|
||||
{
|
||||
bool naga_has_initialized = ((naga_query_init_tracker_for_sq & 1) == 1);
|
||||
bool naga_has_finished = ((naga_query_init_tracker_for_sq & 4) == 4);
|
||||
if (naga_has_initialized && !naga_has_finished) {
|
||||
_e19 = sq.Proceed();
|
||||
naga_query_init_tracker_for_sq = naga_query_init_tracker_for_sq | 2;
|
||||
if (!_e19) { naga_query_init_tracker_for_sq = naga_query_init_tracker_for_sq | 4; }
|
||||
}}
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
(
|
||||
vertex:[
|
||||
],
|
||||
fragment:[
|
||||
],
|
||||
compute:[
|
||||
(
|
||||
entry_point:"main",
|
||||
target_profile:"cs_6_5",
|
||||
),
|
||||
],
|
||||
task:[
|
||||
],
|
||||
mesh:[
|
||||
],
|
||||
)
|
||||
@@ -0,0 +1,77 @@
|
||||
// language: metal2.4
|
||||
#include <metal_stdlib>
|
||||
#include <simd/simd.h>
|
||||
|
||||
using metal::uint;
|
||||
|
||||
struct RayDesc {
|
||||
uint flags;
|
||||
uint cull_mask;
|
||||
float tmin;
|
||||
float tmax;
|
||||
metal::float3 origin;
|
||||
metal::float3 dir;
|
||||
};
|
||||
|
||||
[[max_total_threads_per_threadgroup(1)]] kernel void main_(
|
||||
metal::raytracing::instance_acceleration_structure acc [[user(fake0)]]
|
||||
) {
|
||||
uint i = 0u;
|
||||
metal::raytracing::intersection_query<metal::raytracing::instancing, metal::raytracing::triangle_data> sq = {};
|
||||
uint naga_query_init_tracker_for_sq = 0u;
|
||||
float naga_query_tmax_tracker_for_sq = 0.0;
|
||||
uint2 loop_bound = uint2(4294967295u);
|
||||
bool loop_init = true;
|
||||
while(true) {
|
||||
if (metal::all(loop_bound == uint2(0u))) { break; }
|
||||
loop_bound -= uint2(loop_bound.y == 0u, 1u);
|
||||
if (!loop_init) {
|
||||
uint _e21 = i;
|
||||
i = _e21 + 1u;
|
||||
}
|
||||
loop_init = false;
|
||||
uint _e2 = i;
|
||||
if (_e2 < 4u) {
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
{
|
||||
naga_query_init_tracker_for_sq = 0u;
|
||||
uint _e9 = i;
|
||||
RayDesc _e18 = RayDesc {1u, 255u, 0.001, 1000.0, metal::float3(static_cast<float>(_e9)), metal::float3(0.0, -1.0, 0.0)};
|
||||
{
|
||||
RayDesc desc = _e18;
|
||||
metal::raytracing::intersection_params params;
|
||||
metal::raytracing::opacity_cull_mode cull_mode =
|
||||
(desc.flags & 64) != 0 ? metal::raytracing::opacity_cull_mode::opaque : (
|
||||
(desc.flags & 128) != 0 ? metal::raytracing::opacity_cull_mode::non_opaque : metal::raytracing::opacity_cull_mode::none
|
||||
);
|
||||
params.set_opacity_cull_mode(cull_mode);
|
||||
bool force_opacity = cull_mode == metal::raytracing::opacity_cull_mode::none;
|
||||
if (force_opacity) {
|
||||
params.force_opacity(
|
||||
(desc.flags & 1) != 0 ? metal::raytracing::forced_opacity::opaque : (
|
||||
(desc.flags & 2) != 0 ? metal::raytracing::forced_opacity::non_opaque : metal::raytracing::forced_opacity::none
|
||||
)
|
||||
);
|
||||
}
|
||||
params.accept_any_intersection((desc.flags & 4) != 0);
|
||||
metal::raytracing::ray ray = metal::raytracing::ray(desc.origin, desc.dir, desc.tmin, desc.tmax);
|
||||
bool invalid_nan_infs = ((as_type<uint>(desc.origin.x) & 2139095040) == 2139095040) || ((as_type<uint>(desc.origin.y) & 2139095040) == 2139095040) || ((as_type<uint>(desc.origin.z) & 2139095040) == 2139095040) || ((as_type<uint>(desc.dir.x) & 2139095040) == 2139095040) || ((as_type<uint>(desc.dir.y) & 2139095040) == 2139095040) || ((as_type<uint>(desc.dir.z) & 2139095040) == 2139095040) || ((as_type<uint>(desc.tmin) & 2139095040) == 2139095040) || (((as_type<uint>(desc.tmax) & 2139095040) == 2139095040) && ((as_type<uint>(desc.tmax) & 0x7fffff) != 0));
|
||||
bool invalid_t = (desc.tmin > desc.tmax) || (desc.tmin < 0.0);
|
||||
bool invalid_dir = metal::all(metal::abs(desc.dir) == 0.0);
|
||||
if (!(invalid_dir || invalid_t || invalid_nan_infs)) {
|
||||
sq.reset(ray,acc, desc.cull_mask, params);
|
||||
naga_query_init_tracker_for_sq = 1;
|
||||
naga_query_tmax_tracker_for_sq = desc.tmax;
|
||||
}
|
||||
}
|
||||
bool _e19 = false;
|
||||
if (((naga_query_init_tracker_for_sq & 1) == 1) && !((naga_query_init_tracker_for_sq & 4) == 4)) {
|
||||
_e19 = sq.next();
|
||||
naga_query_init_tracker_for_sq = naga_query_init_tracker_for_sq | (_e19 ? 2: 6);
|
||||
}
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
@@ -0,0 +1,223 @@
|
||||
; SPIR-V
|
||||
; Version: 1.1
|
||||
; Generator: rspirv
|
||||
; Bound: 171
|
||||
OpCapability Shader
|
||||
OpCapability RayQueryKHR
|
||||
OpExtension "SPV_KHR_ray_query"
|
||||
%1 = OpExtInstImport "GLSL.std.450"
|
||||
OpMemoryModel Logical GLSL450
|
||||
OpEntryPoint GLCompute %12 "main"
|
||||
OpExecutionMode %12 LocalSize 1 1 1
|
||||
OpMemberDecorate %8 0 Offset 0
|
||||
OpMemberDecorate %8 1 Offset 4
|
||||
OpMemberDecorate %8 2 Offset 8
|
||||
OpMemberDecorate %8 3 Offset 12
|
||||
OpMemberDecorate %8 4 Offset 16
|
||||
OpMemberDecorate %8 5 Offset 32
|
||||
OpDecorate %9 DescriptorSet 0
|
||||
OpDecorate %9 Binding 0
|
||||
%2 = OpTypeVoid
|
||||
%3 = OpTypeAccelerationStructureKHR
|
||||
%4 = OpTypeInt 32 0
|
||||
%5 = OpTypeRayQueryKHR
|
||||
%6 = OpTypeFloat 32
|
||||
%7 = OpTypeVector %6 3
|
||||
%8 = OpTypeStruct %4 %4 %6 %6 %7 %7
|
||||
%10 = OpTypePointer UniformConstant %3
|
||||
%9 = OpVariable %10 UniformConstant
|
||||
%13 = OpTypeFunction %2
|
||||
%15 = OpConstant %4 0
|
||||
%16 = OpConstant %4 4
|
||||
%17 = OpConstant %4 1
|
||||
%18 = OpConstant %4 255
|
||||
%19 = OpConstant %6 0.001
|
||||
%20 = OpConstant %6 1000
|
||||
%21 = OpConstant %6 0
|
||||
%22 = OpConstant %6 -1
|
||||
%23 = OpConstantComposite %7 %21 %22 %21
|
||||
%25 = OpTypePointer Function %4
|
||||
%27 = OpTypePointer Function %5
|
||||
%29 = OpTypePointer Function %6
|
||||
%36 = OpTypeVector %4 2
|
||||
%37 = OpTypePointer Function %36
|
||||
%38 = OpTypeBool
|
||||
%39 = OpTypeVector %38 2
|
||||
%40 = OpConstantComposite %36 %15 %15
|
||||
%41 = OpConstant %4 4294967295
|
||||
%42 = OpConstantComposite %36 %41 %41
|
||||
%64 = OpTypeFunction %2 %27 %3 %8 %25 %29
|
||||
%72 = OpTypeVector %38 3
|
||||
%93 = OpConstant %4 256
|
||||
%96 = OpConstant %4 512
|
||||
%101 = OpConstant %4 16
|
||||
%104 = OpConstant %4 32
|
||||
%115 = OpConstant %4 2
|
||||
%118 = OpConstant %4 64
|
||||
%121 = OpConstant %4 128
|
||||
%147 = OpTypePointer Function %38
|
||||
%148 = OpTypeFunction %38 %27 %25
|
||||
%154 = OpConstantFalse %38
|
||||
%165 = OpConstant %4 6
|
||||
%65 = OpFunction %2 None %64
|
||||
%66 = OpFunctionParameter %27
|
||||
%67 = OpFunctionParameter %3
|
||||
%68 = OpFunctionParameter %8
|
||||
%69 = OpFunctionParameter %25
|
||||
%70 = OpFunctionParameter %29
|
||||
%71 = OpLabel
|
||||
%73 = OpCompositeExtract %4 %68 0
|
||||
%74 = OpCompositeExtract %4 %68 1
|
||||
%75 = OpCompositeExtract %6 %68 2
|
||||
%76 = OpCompositeExtract %6 %68 3
|
||||
%77 = OpCompositeExtract %7 %68 4
|
||||
%78 = OpCompositeExtract %7 %68 5
|
||||
%79 = OpFOrdLessThanEqual %38 %75 %76
|
||||
%80 = OpFOrdGreaterThanEqual %38 %75 %21
|
||||
%81 = OpIsInf %72 %77
|
||||
%82 = OpAny %38 %81
|
||||
%83 = OpIsNan %72 %77
|
||||
%84 = OpAny %38 %83
|
||||
%85 = OpLogicalOr %38 %84 %82
|
||||
%86 = OpLogicalNot %38 %85
|
||||
%87 = OpIsInf %72 %78
|
||||
%88 = OpAny %38 %87
|
||||
%89 = OpIsNan %72 %78
|
||||
%90 = OpAny %38 %89
|
||||
%91 = OpLogicalOr %38 %90 %88
|
||||
%92 = OpLogicalNot %38 %91
|
||||
%94 = OpBitwiseAnd %4 %73 %93
|
||||
%95 = OpINotEqual %38 %94 %15
|
||||
%97 = OpBitwiseAnd %4 %73 %96
|
||||
%98 = OpINotEqual %38 %97 %15
|
||||
%99 = OpLogicalAnd %38 %98 %95
|
||||
%100 = OpLogicalNot %38 %99
|
||||
%102 = OpBitwiseAnd %4 %73 %101
|
||||
%103 = OpINotEqual %38 %102 %15
|
||||
%105 = OpBitwiseAnd %4 %73 %104
|
||||
%106 = OpINotEqual %38 %105 %15
|
||||
%107 = OpLogicalAnd %38 %106 %95
|
||||
%108 = OpLogicalAnd %38 %106 %103
|
||||
%109 = OpLogicalAnd %38 %103 %95
|
||||
%110 = OpLogicalOr %38 %109 %107
|
||||
%111 = OpLogicalOr %38 %110 %108
|
||||
%112 = OpLogicalNot %38 %111
|
||||
%113 = OpBitwiseAnd %4 %73 %17
|
||||
%114 = OpINotEqual %38 %113 %15
|
||||
%116 = OpBitwiseAnd %4 %73 %115
|
||||
%117 = OpINotEqual %38 %116 %15
|
||||
%119 = OpBitwiseAnd %4 %73 %118
|
||||
%120 = OpINotEqual %38 %119 %15
|
||||
%122 = OpBitwiseAnd %4 %73 %121
|
||||
%123 = OpINotEqual %38 %122 %15
|
||||
%124 = OpLogicalAnd %38 %123 %114
|
||||
%125 = OpLogicalAnd %38 %123 %117
|
||||
%126 = OpLogicalAnd %38 %123 %120
|
||||
%127 = OpLogicalAnd %38 %120 %114
|
||||
%128 = OpLogicalAnd %38 %120 %117
|
||||
%129 = OpLogicalAnd %38 %117 %114
|
||||
%130 = OpLogicalOr %38 %129 %124
|
||||
%131 = OpLogicalOr %38 %130 %125
|
||||
%132 = OpLogicalOr %38 %131 %126
|
||||
%133 = OpLogicalOr %38 %132 %127
|
||||
%134 = OpLogicalOr %38 %133 %128
|
||||
%135 = OpLogicalNot %38 %134
|
||||
%136 = OpLogicalAnd %38 %135 %79
|
||||
%137 = OpLogicalAnd %38 %136 %80
|
||||
%138 = OpLogicalAnd %38 %137 %86
|
||||
%139 = OpLogicalAnd %38 %138 %92
|
||||
%140 = OpLogicalAnd %38 %139 %100
|
||||
%141 = OpLogicalAnd %38 %140 %112
|
||||
OpStore %70 %76
|
||||
OpSelectionMerge %142 None
|
||||
OpBranchConditional %141 %144 %143
|
||||
%144 = OpLabel
|
||||
OpRayQueryInitializeKHR %66 %67 %73 %74 %77 %75 %78 %76
|
||||
OpStore %69 %17
|
||||
OpBranch %142
|
||||
%143 = OpLabel
|
||||
OpBranch %142
|
||||
%142 = OpLabel
|
||||
OpReturn
|
||||
OpFunctionEnd
|
||||
%149 = OpFunction %38 None %148
|
||||
%150 = OpFunctionParameter %27
|
||||
%151 = OpFunctionParameter %25
|
||||
%152 = OpLabel
|
||||
%153 = OpVariable %147 Function %154
|
||||
%155 = OpLoad %4 %151
|
||||
%158 = OpBitwiseAnd %4 %155 %17
|
||||
%159 = OpINotEqual %38 %158 %15
|
||||
%160 = OpBitwiseAnd %4 %155 %16
|
||||
%161 = OpINotEqual %38 %160 %15
|
||||
%162 = OpLogicalNot %38 %161
|
||||
%163 = OpLogicalAnd %38 %162 %159
|
||||
OpSelectionMerge %156 None
|
||||
OpBranchConditional %163 %157 %156
|
||||
%157 = OpLabel
|
||||
%164 = OpRayQueryProceedKHR %38 %150
|
||||
OpStore %153 %164
|
||||
%166 = OpSelect %4 %164 %115 %165
|
||||
%167 = OpBitwiseOr %4 %155 %166
|
||||
OpStore %151 %167
|
||||
OpBranch %156
|
||||
%156 = OpLabel
|
||||
%168 = OpLoad %38 %153
|
||||
OpReturnValue %168
|
||||
OpFunctionEnd
|
||||
%12 = OpFunction %2 None %13
|
||||
%11 = OpLabel
|
||||
%24 = OpVariable %25 Function %15
|
||||
%26 = OpVariable %27 Function
|
||||
%28 = OpVariable %25 Function %15
|
||||
%30 = OpVariable %29 Function %21
|
||||
%43 = OpVariable %37 Function %42
|
||||
%14 = OpLoad %3 %9
|
||||
OpBranch %31
|
||||
%31 = OpLabel
|
||||
OpBranch %32
|
||||
%32 = OpLabel
|
||||
OpLoopMerge %33 %35 None
|
||||
OpBranch %44
|
||||
%44 = OpLabel
|
||||
%45 = OpLoad %36 %43
|
||||
%46 = OpIEqual %39 %40 %45
|
||||
%47 = OpAll %38 %46
|
||||
OpSelectionMerge %48 None
|
||||
OpBranchConditional %47 %33 %48
|
||||
%48 = OpLabel
|
||||
%49 = OpCompositeExtract %4 %45 1
|
||||
%50 = OpIEqual %38 %49 %15
|
||||
%51 = OpSelect %4 %50 %17 %15
|
||||
%52 = OpCompositeConstruct %36 %51 %17
|
||||
%53 = OpISub %36 %45 %52
|
||||
OpStore %43 %53
|
||||
OpBranch %34
|
||||
%34 = OpLabel
|
||||
%54 = OpLoad %4 %24
|
||||
%55 = OpULessThan %38 %54 %16
|
||||
OpSelectionMerge %56 None
|
||||
OpBranchConditional %55 %56 %57
|
||||
%57 = OpLabel
|
||||
OpBranch %33
|
||||
%56 = OpLabel
|
||||
OpBranch %58
|
||||
%58 = OpLabel
|
||||
OpStore %28 %15
|
||||
%60 = OpLoad %4 %24
|
||||
%61 = OpConvertUToF %6 %60
|
||||
%62 = OpCompositeConstruct %7 %61 %61 %61
|
||||
%63 = OpCompositeConstruct %8 %17 %18 %19 %20 %62 %23
|
||||
%145 = OpFunctionCall %2 %65 %26 %14 %63 %28 %30
|
||||
%146 = OpFunctionCall %38 %149 %26 %28
|
||||
OpBranch %59
|
||||
%59 = OpLabel
|
||||
OpBranch %35
|
||||
%35 = OpLabel
|
||||
%169 = OpLoad %4 %24
|
||||
%170 = OpIAdd %4 %169 %17
|
||||
OpStore %24 %170
|
||||
OpBranch %32
|
||||
%33 = OpLabel
|
||||
OpReturn
|
||||
OpFunctionEnd
|
||||
@@ -0,0 +1,74 @@
|
||||
#version 460
|
||||
#extension GL_EXT_ray_query : require
|
||||
layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
|
||||
|
||||
struct _8
|
||||
{
|
||||
uint _m0;
|
||||
uint _m1;
|
||||
float _m2;
|
||||
float _m3;
|
||||
vec3 _m4;
|
||||
vec3 _m5;
|
||||
};
|
||||
|
||||
layout(set = 0, binding = 0) uniform accelerationStructureEXT _9;
|
||||
|
||||
void _65(rayQueryEXT _66, accelerationStructureEXT _67, _8 _68, inout uint _69, out float _70)
|
||||
{
|
||||
bool _95 = (_68._m0 & 256u) != 0u;
|
||||
bool _103 = (_68._m0 & 16u) != 0u;
|
||||
bool _106 = (_68._m0 & 32u) != 0u;
|
||||
bool _114 = (_68._m0 & 1u) != 0u;
|
||||
bool _117 = (_68._m0 & 2u) != 0u;
|
||||
bool _120 = (_68._m0 & 64u) != 0u;
|
||||
bool _123 = (_68._m0 & 128u) != 0u;
|
||||
_70 = _68._m3;
|
||||
if (((((((!((((((_117 && _114) || (_123 && _114)) || (_123 && _117)) || (_123 && _120)) || (_120 && _114)) || (_120 && _117))) && (_68._m2 <= _68._m3)) && (_68._m2 >= 0.0)) && (!(any(isnan(_68._m4)) || any(isinf(_68._m4))))) && (!(any(isnan(_68._m5)) || any(isinf(_68._m5))))) && (!(((_68._m0 & 512u) != 0u) && _95))) && (!(((_103 && _95) || (_106 && _95)) || (_106 && _103))))
|
||||
{
|
||||
rayQueryInitializeEXT(_66, _67, _68._m0, _68._m1, _68._m4, _68._m2, _68._m5, _68._m3);
|
||||
_69 = 1u;
|
||||
}
|
||||
else
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
bool _149(rayQueryEXT _150, inout uint _151)
|
||||
{
|
||||
bool _153 = false;
|
||||
if ((!((_151 & 4u) != 0u)) && ((_151 & 1u) != 0u))
|
||||
{
|
||||
bool _164 = rayQueryProceedEXT(_150);
|
||||
_153 = _164;
|
||||
_151 |= (_164 ? 2u : 6u);
|
||||
}
|
||||
return _153;
|
||||
}
|
||||
|
||||
void main()
|
||||
{
|
||||
uint _24 = 0u;
|
||||
uint _28 = 0u;
|
||||
float _30 = 0.0;
|
||||
uvec2 _43 = uvec2(4294967295u);
|
||||
rayQueryEXT _26;
|
||||
for (;;)
|
||||
{
|
||||
if (all(equal(uvec2(0u), _43)))
|
||||
{
|
||||
break;
|
||||
}
|
||||
_43 -= uvec2(uint(_43.y == 0u), 1u);
|
||||
if (!(_24 < 4u))
|
||||
{
|
||||
break;
|
||||
}
|
||||
_28 = 0u;
|
||||
_65(_26, _9, _8(1u, 255u, 0.001000000047497451305389404296875, 1000.0, vec3(float(_24)), vec3(0.0, -1.0, 0.0)), _28, _30);
|
||||
bool _146 = _149(_26, _28);
|
||||
_24++;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user