Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions crates/xcl/src/gpu/bvh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,10 +262,11 @@ pub fn triangle_hit(v: &Triangle, o: [f32; 3], d: [f32; 3]) -> Option<[f32; 4]>
let sv = sub(o, v[0]);
let q = cross(sv, e1);
let (i, j, t) = (dot(sv, p), dot(d, q), dot(e2, q));
// (and ahead of the origin: t over det above 0; a hit at t = 0 is a miss too)
let hit = if det > 0.0 {
i >= 0.0 && j >= 0.0 && i + j <= det
i >= 0.0 && j >= 0.0 && i + j <= det && t > 0.0
} else {
det < 0.0 && i <= 0.0 && j <= 0.0 && i + j >= det
det < 0.0 && i <= 0.0 && j <= 0.0 && i + j >= det && t < 0.0
};
hit.then_some([t, det, i, j])
}
Expand Down Expand Up @@ -1065,6 +1066,19 @@ pub(crate) mod tests {
assert_eq!(got, [c0, INVALID, INVALID, INVALID]);
}

#[test]
fn triangle_hit_misses_a_triangle_behind_the_origin() {
let tri = [[-1.0, -1.0, 5.0], [1.0, -1.0, 5.0], [0.0, 1.0, 5.0]];
let rev = [tri[0], tri[2], tri[1]];
for v in [tri, rev] {
let r = triangle_hit(&v, [0.0, 0.0, 0.0], [0.0, 0.0, 1.0]).expect("ahead of the origin: hit");
assert_eq!(r[0] / r[1], 5.0);
assert!(triangle_hit(&v, [0.0, 0.0, 0.0], [0.0, 0.0, -1.0]).is_none(), "pointing away");
assert!(triangle_hit(&v, [0.0, 0.0, 6.0], [0.0, 0.0, 1.0]).is_none(), "starting past it");
assert!(triangle_hit(&v, [0.0, 0.0, 5.0], [0.0, 0.0, 1.0]).is_none(), "starting on it (t = 0)");
}
}

#[test]
fn triangle_hit_small_and_large_det_avoids_underflow_and_overflow() {
let d = [0.0, 0.0, 1.0];
Expand Down
5 changes: 5 additions & 0 deletions crates/xcl/src/gpu/dxil/lower_image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,15 +309,20 @@ impl Lw {
let c1 = lw.fcmp(Pred::FOGe, i, z);
let c2 = lw.fcmp(Pred::FOGe, j, z);
let c3 = lw.fcmp(Pred::FOLe, ij, det);
// (and ahead of the origin: t over det above 0, as the HLSL form)
let c4 = lw.fcmp(Pred::FOGt, t, z);
let hp = lw.land(c1, c2);
let hp = lw.land(hp, c3);
let hp = lw.land(hp, c4);
let dneg = lw.fcmp(Pred::FOLt, det, z);
let n1 = lw.fcmp(Pred::FOLe, i, z);
let n2 = lw.fcmp(Pred::FOLe, j, z);
let n3 = lw.fcmp(Pred::FOGe, ij, det);
let hn = lw.land(dneg, n1);
let hn = lw.land(hn, n2);
let hn = lw.land(hn, n3);
let n4 = lw.fcmp(Pred::FOLt, t, z);
let hn = lw.land(hn, n4);
let hit = lw.sel(dpos, hp, hn);
let di = lw.fsub(det, i);
let b0 = lw.fsub(di, j);
Expand Down
3 changes: 2 additions & 1 deletion crates/xcl/src/gpu/recomp/emit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6465,7 +6465,8 @@ uint4 bvh_intersect(uint type, uint at, float tmax, float3 o, float3 d, float3 i
float3 sv = o - v0, q = cross(sv, e1);
float i = dot(sv, p), j = dot(d, q), t = dot(e2, q);
// inside when i, j, 1 - i - j >= 0 relative to det (either winding); avoid det * det underflow/overflow
bool hit = det > 0.0f ? (i >= 0.0f && j >= 0.0f && i + j <= det) : (det < 0.0f && i <= 0.0f && j <= 0.0f && i + j >= det);
// (and ahead of the origin: t over det above 0; no hit behind it or at t = 0)
bool hit = det > 0.0f ? (i >= 0.0f && j >= 0.0f && i + j <= det && t > 0.0f) : (det < 0.0f && i <= 0.0f && j <= 0.0f && i + j >= det && t < 0.0f);
// barycentrics back to the original vertex order: triangle id @60, per triangle I / J source
uint4 bc = asuint(float4(det - i - j, i, j, 0.0f));
uint tw = mem.Load(at + 60u);
Expand Down
56 changes: 56 additions & 0 deletions crates/xcl/src/gpu/recomp/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6392,6 +6392,62 @@ fn gpu_bvh_intersect_ray() {
}
}

/// image_bvh_intersect_ray reports a triangle hit only ahead of the ray's origin, as the hardware
/// does: a ray towards the triangle hits it at t = 5, one pointing away from it, starting past
/// it or starting on it (t = 0) misses; in either winding (the hit test branches on the denominator's sign). A shadow ray
/// starting on a surface must not find the triangles behind its start.
#[test]
fn gpu_bvh_triangle_behind_the_origin_is_missed() {
if !gpu_ready() {
return;
}
let base = 0x1000usize;
let mut mem = vec![0u32; 0x2000 / 4];
let w = |mem: &mut Vec<u32>, at: usize, vals: &[u32]| mem[(base + at) / 4..(base + at) / 4 + vals.len()].copy_from_slice(vals);
let ff = |v: &[f32]| v.iter().map(|x| x.to_bits()).collect::<Vec<u32>>();
// a triangle in z = 5 at node 0, and the same one with its vertices reversed at node 0x40
w(&mut mem, 0, &ff(&[-1.0, -1.0, 5.0, 1.0, -1.0, 5.0, -1.0, 1.0, 5.0]));
w(&mut mem, 0x40, &ff(&[-1.0, -1.0, 5.0, -1.0, 1.0, 5.0, 1.0, -1.0, 5.0]));
w(&mut mem, 60, &[9]);
w(&mut mem, 0x40 + 60, &[9]);
let mimg = |vdata: u64, vaddr: u64| encode("IMAGE_BVH_INTERSECT_RAY", "ENC_MIMG", &[("VDATA", vdata), ("VADDR", vaddr), ("SRSRC", 2), ("DMASK", 15)], None).unwrap();
let code = [mimg(100, 0), mimg(104, 20), encode("S_ENDPGM", "ENC_SOPP", &[], None).unwrap()].concat();
let mut st = KernelState::default();
st.sgpr[8..12].copy_from_slice(&[(base >> 8) as u32, 1 << 31, u32::MAX, 1 << 24]);
// lane % 4: 0 from the origin towards +z, 1 from the origin towards -z, 2 from z = 10 towards
// +z, 3 from z = 5 (on the triangle, t = 0) towards +z
let ray = |l: usize| -> ([f32; 3], f32) {
match l % 4 {
0 => ([0.0, 0.0, 0.0], 1.0),
1 => ([0.0, 0.0, 0.0], -1.0),
2 => ([0.0, 0.0, 10.0], 1.0),
_ => ([0.0, 0.0, 5.0], 1.0),
}
};
for (l, r) in st.vgpr.iter_mut().enumerate() {
let (o, dz) = ray(l);
for (at, node) in [(0usize, 0u32), (20, 0x40 >> 3)] {
r[at] = node;
let v = [100.0, o[0], o[1], o[2], 0.0, 0.0, dz, f32::INFINITY, f32::INFINITY, 1.0 / dz];
for (k, x) in v.iter().enumerate() {
r[at + 1 + k] = x.to_bits();
}
}
}
let res = runner::run_code(&code, &st, &mem).unwrap();
for l in 0..64usize {
let v = &res.vgpr[l];
for at in [100, 104] {
let q = [v[at], v[at + 1], v[at + 2], v[at + 3]].map(f32::from_bits);
if l % 4 == 0 {
assert_eq!(q[0] / q[1], 5.0, "lane {l}, result {at}: the triangle ahead is hit at t = 5 ({q:?})");
} else {
assert_eq!(q[0], f32::INFINITY, "lane {l}, result {at}: the triangle behind or at the origin is missed ({q:?})");
}
}
}
}

/// Checks the compute ABI read from the FSSM stage descriptor (+0x38 COMPUTE_PGM_RSRC2,
/// +0x3C.. numthreads) on every CS of the pack against PSV0 and the SGPRs/VGPRs the code reads.
#[test]
Expand Down
Loading