mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-30 18:27:00 +01:00
Merge pull request #4372 from dotCooCoo/fix/raster3d-shadow-casters
craft-raster3d: shadow casters clipped at the near plane take one f64 fraction when a position difference overflows
This commit is contained in:
1 file changed
+186
-6
@@ -24,6 +24,51 @@ fn cross3(a: [f64; 3], b: [f64; 3]) -> [f64; 3] {
|
||||
[a[1] * b[2] - a[2] * b[1], a[2] * b[0] - a[0] * b[2], a[0] * b[1] - a[1] * b[0]]
|
||||
}
|
||||
|
||||
/// Whether the plane through the finite `f32` corners `p` passes exactly through the origin.
|
||||
/// Every product of two `f32` values is exact in `f64`, and `two_product` splits each
|
||||
/// three-factor term into an exact pair, so the expansion sum gives the determinant's exact sign.
|
||||
fn passes_through_origin(p: [[f32; 3]; 3]) -> bool {
|
||||
let [a, b, c] = p.map(|v| v.map(f64::from));
|
||||
let fast = dot3(a, cross3(b, c));
|
||||
let permanent = [(0, 1, 2), (1, 2, 0), (2, 0, 1)].iter().map(|&(i, j, k)| a[i].abs() * ((b[j] * c[k]).abs() + (b[k] * c[j]).abs())).sum::<f64>();
|
||||
if fast.abs() > 8.0 * f64::EPSILON * permanent {
|
||||
return false;
|
||||
}
|
||||
let two_product = |x: f64, y: f64| {
|
||||
let p = x * y;
|
||||
(p, x.mul_add(y, -p))
|
||||
};
|
||||
// Twelve exact parts; the expansion never holds more components than parts added.
|
||||
let mut expansion = [0.0_f64; 12];
|
||||
let mut len = 0;
|
||||
for (i, j, k) in [(0, 1, 2), (1, 2, 0), (2, 0, 1)] {
|
||||
for (s, bc) in [(1.0, b[j] * c[k]), (-1.0, b[k] * c[j])] {
|
||||
let (hi, lo) = two_product(s * a[i], bc);
|
||||
for x in [lo, hi] {
|
||||
let mut q = x;
|
||||
let mut kept = 0;
|
||||
for n in 0..len {
|
||||
let h = expansion[n];
|
||||
let sum = q + h;
|
||||
let bv = sum - q;
|
||||
let err = (q - (sum - bv)) + (h - bv);
|
||||
if err != 0.0 {
|
||||
expansion[kept] = err;
|
||||
kept += 1;
|
||||
}
|
||||
q = sum;
|
||||
}
|
||||
if q != 0.0 {
|
||||
expansion[kept] = q;
|
||||
kept += 1;
|
||||
}
|
||||
len = kept;
|
||||
}
|
||||
}
|
||||
}
|
||||
len == 0
|
||||
}
|
||||
|
||||
fn len3(a: [f64; 3]) -> f64 {
|
||||
dot3(a, a).sqrt()
|
||||
}
|
||||
@@ -93,6 +138,15 @@ fn light_poly(s: &Scene, m: &ShadowMap, ids: [usize; 3]) -> Vec<LightCorner> {
|
||||
.collect();
|
||||
let mut poly = q;
|
||||
if !m.ortho {
|
||||
// A caster edge-on to the light covers no texels. Its clipped corners can still come
|
||||
// apart when a position difference overflows, so it is dropped before clipping.
|
||||
let corners = [0, 1, 2].map(|i| poly[i].0);
|
||||
if corners.iter().all(|c| c[2] < NEAR) {
|
||||
return vec![];
|
||||
}
|
||||
if corners.iter().flatten().all(|c| c.is_finite()) && passes_through_origin(corners) {
|
||||
return vec![];
|
||||
}
|
||||
let mut out = vec![];
|
||||
for i in 0..poly.len() {
|
||||
let (a, b) = (poly[i], poly[(i + 1) % poly.len()]);
|
||||
@@ -103,17 +157,26 @@ fn light_poly(s: &Scene, m: &ShadowMap, ids: [usize; 3]) -> Vec<LightCorner> {
|
||||
if (da >= 0.0) != (db >= 0.0) {
|
||||
let (from_b, k) = f32_crossing(da, db);
|
||||
let (p, q) = if from_b { (b, a) } else { (a, b) };
|
||||
let lerp = |x: f32, y: f32| {
|
||||
let d = y - x;
|
||||
if d.is_finite() || !x.is_finite() || !y.is_finite() {
|
||||
x + d * k
|
||||
// `y - x` can overflow f32 between far-apart finite corners. An overflowing x or y
|
||||
// position difference selects the f64 fraction from the original depths for both
|
||||
// position and UVs. Otherwise each component whose own difference overflows is
|
||||
// taken in f64 with the f32 fraction.
|
||||
let overflows = |x: f32, y: f32| x.is_finite() && y.is_finite() && !(y - x).is_finite();
|
||||
let (pa, pb) = ((p.0[0], q.0[0]), (p.0[1], q.0[1]));
|
||||
let (ua, ub) = ((p.1[0], q.1[0]), (p.1[1], q.1[1]));
|
||||
let wide_pos = overflows(pa.0, pa.1) || overflows(pb.0, pb.1);
|
||||
let (dp, dq) = (f64::from(p.0[2]) - f64::from(NEAR), f64::from(q.0[2]) - f64::from(NEAR));
|
||||
let k64 = if wide_pos { dp / (dp - dq) } else { f64::from(k) };
|
||||
let lerp = |wide: bool, x: f32, y: f32| {
|
||||
if (wide || overflows(x, y)) && x.is_finite() && y.is_finite() {
|
||||
(f64::from(x) + (f64::from(y) - f64::from(x)) * k64) as f32
|
||||
} else {
|
||||
(f64::from(x) + (f64::from(y) - f64::from(x)) * f64::from(k)) as f32
|
||||
x + (y - x) * k
|
||||
}
|
||||
};
|
||||
// The crossing lies on the near plane; its depth is set to it, since the lerp of
|
||||
// far-apart depths can round to 0 or below.
|
||||
out.push(([lerp(p.0[0], q.0[0]), lerp(p.0[1], q.0[1]), NEAR], [lerp(p.1[0], q.1[0]), lerp(p.1[1], q.1[1])]));
|
||||
out.push(([lerp(wide_pos, pa.0, pa.1), lerp(wide_pos, pb.0, pb.1), NEAR], [lerp(wide_pos, ua.0, ua.1), lerp(wide_pos, ub.0, ub.1)]));
|
||||
}
|
||||
}
|
||||
poly = out;
|
||||
@@ -618,6 +681,42 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn edge_on_perspective_casters_with_overflowing_differences_write_no_texels() {
|
||||
let a = 2.0_f32.powi(127);
|
||||
for indices in [vec![0, 1, 2], vec![0, 2, 1]] {
|
||||
let s = Scene {
|
||||
vertices: [[-a, -0.75 * a, -32768.0], [a, 0.75 * a, 32768.0], [0.0, 0.0, NEAR]]
|
||||
.map(|pos| Vertex { pos, ..Vertex::default() })
|
||||
.to_vec(),
|
||||
indices,
|
||||
materials: vec![Material::default()],
|
||||
..Scene::default()
|
||||
};
|
||||
let depths = shadow_raster(&s, &perspective_map(), &[0]);
|
||||
assert!(depths.iter().all(|&depth| depth == f32::INFINITY));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_edge_on_test_keeps_the_rounding_residual_of_each_product() {
|
||||
// Each three-factor term needs more than 53 bits; the rounded terms sum to -2048.
|
||||
let (a, b) = ([1_000_008.0, 2_000_007.0, 3_000_002.0], [3_000_005.0, 2_000_007.0, 1_000_004.0]);
|
||||
let c = [0, 1, 2].map(|i| a[i] + b[i]);
|
||||
assert!(super::passes_through_origin([a, b, c]));
|
||||
assert!(!super::passes_through_origin([a, b, [c[0], c[1], c[2] + 1.0]]));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn casters_whose_rounded_determinant_is_zero_are_kept_in_every_corner_order() {
|
||||
let x = 2.0_f32.powi(48);
|
||||
let v = |pos: [f32; 3]| Vertex { pos, ..Vertex::default() };
|
||||
let s = Scene { vertices: vec![v([-x, -NEAR, -x]), v([x, -NEAR, x]), v([0.0, NEAR, NEAR])], ..Scene::default() };
|
||||
for ids in [[0, 1, 2], [1, 2, 0], [2, 0, 1], [0, 2, 1]] {
|
||||
assert!(!light_poly(&s, &identity_map(), ids).is_empty(), "{ids:?}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn perspective_casters_with_subnormal_reciprocals_keep_finite_depth() {
|
||||
for z in [1e38, f32::MAX] {
|
||||
@@ -659,4 +758,85 @@ mod tests {
|
||||
assert_eq!(covered(f32::MAX, [0.0, 1.0], indices), 0);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_near_plane_crossing_with_one_overflowing_uv_axis_preserves_the_other_axis() {
|
||||
let clipped = |u: f32, ids| {
|
||||
let s = Scene {
|
||||
vertices: [
|
||||
([-1.0, 0.0, 0.0], [-u, -16_777_216.0]),
|
||||
([1.0, 0.0, 3.0 * NEAR], [u, 33_554_432.0]),
|
||||
([0.0, 1.0, 3.0 * NEAR], [0.0, 0.0]),
|
||||
]
|
||||
.map(|(pos, uv)| Vertex { pos, uv, ..Vertex::default() })
|
||||
.to_vec(),
|
||||
..Scene::default()
|
||||
};
|
||||
light_poly(&s, &identity_map(), ids)
|
||||
};
|
||||
for ids in [[0, 1, 2], [1, 0, 2]] {
|
||||
let ordinary = clipped(1.0, ids);
|
||||
let overflowing = clipped(f32::MAX, ids);
|
||||
assert!(overflowing.iter().any(|(p, _)| p[2] == NEAR));
|
||||
assert!(overflowing.iter().all(|(_, uv)| uv.iter().all(|v| v.is_finite())));
|
||||
let unaffected = |poly: Vec<super::LightCorner>| poly.into_iter().map(|(p, uv)| (p, uv[1])).collect::<Vec<_>>();
|
||||
assert_eq!(unaffected(overflowing), unaffected(ordinary));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_near_plane_crossing_whose_difference_overflows_f32_takes_its_fraction_in_f64() {
|
||||
// 3e38 − (−3e38) overflows f32; the edge crosses the near plane near x = 3e35. An `f32`
|
||||
// fraction puts the crossing about 1.4e31 off the edge's own crossing.
|
||||
let v = |pos: [f32; 3]| Vertex { pos, ..Vertex::default() };
|
||||
let (a, b) = ([-3e38_f32, 0.0, -1.0], [3e38_f32, 1.0, 1.0]);
|
||||
let s = Scene { vertices: vec![v(a), v(b), v([0.0, 0.1, 1.0])], ..Scene::default() };
|
||||
let poly = light_poly(&s, &identity_map(), [0, 1, 2]);
|
||||
assert!(!poly.is_empty());
|
||||
for (p, uv) in &poly {
|
||||
assert!(p.iter().chain(uv.iter()).all(|c| c.is_finite()), "{poly:?}");
|
||||
}
|
||||
let t = (f64::from(NEAR) - f64::from(a[2])) / (f64::from(b[2]) - f64::from(a[2]));
|
||||
let x = f64::from(a[0]) + (f64::from(b[0]) - f64::from(a[0])) * t;
|
||||
assert!(poly.iter().any(|(p, _)| (f64::from(p[0]) - x).abs() <= x.abs() * 1e-6 && p[2] == NEAR), "{poly:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_wide_near_plane_crossing_stays_on_its_edge() {
|
||||
// The wide edge has y = x / 2; x's difference overflows f32 and y's does not.
|
||||
// Both components of its near-plane crossing take the same fraction.
|
||||
let v = |pos: [f32; 3]| Vertex { pos, ..Vertex::default() };
|
||||
let s = Scene { vertices: vec![v([3e38, 1.5e38, -1.0]), v([-3e38, -1.5e38, 1.0 + f32::EPSILON]), v([0.0, 1.0, NEAR])], ..Scene::default() };
|
||||
let poly = light_poly(&s, &identity_map(), [0, 1, 2]);
|
||||
assert!(poly.iter().any(|(p, _)| p[2] == NEAR && p[0] != 0.0), "{poly:?}");
|
||||
for (p, _) in &poly {
|
||||
let (x, y) = (f64::from(p[0]), f64::from(p[1]));
|
||||
if x == 0.0 {
|
||||
continue;
|
||||
}
|
||||
assert!(x.is_finite() && (y - x / 2.0).abs() <= x.abs() * 1e-6, "{poly:?}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_wide_near_plane_crossing_keeps_uvs_on_its_edge() {
|
||||
let v = |pos: [f32; 3], u: f32| Vertex { pos, uv: [u, 0.0], ..Vertex::default() };
|
||||
let s = Scene {
|
||||
vertices: vec![v([-3e38, 0.0, -32768.0], -32768000.0), v([3e38, 1.0, 32768.0], 32768000.0), v([0.0, 1.0, NEAR], 0.0)],
|
||||
..Scene::default()
|
||||
};
|
||||
for ids in [[0, 1, 2], [1, 0, 2]] {
|
||||
let poly = light_poly(&s, &identity_map(), ids);
|
||||
assert!(poly.iter().any(|(p, _)| p[2] == NEAR && p[0] != 0.0), "{poly:?}");
|
||||
for (p, uv) in &poly {
|
||||
let implied_u = f64::from(p[0]) / f64::from(s.vertices[0].pos[0]) * f64::from(s.vertices[0].uv[0]);
|
||||
assert!((f64::from(uv[0]) - implied_u).abs() <= implied_u.abs() * 1e-6, "{poly:?}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn identity_map() -> ShadowMap {
|
||||
let view = [[1.0, 0.0, 0.0, 0.0], [0.0, 1.0, 0.0, 0.0], [0.0, 0.0, 1.0, 0.0]];
|
||||
ShadowMap { view, focal: 1.0, ortho: false, size: 8, offset: 0, bias: 0.0, radius: 0.0 }
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user