From 8b16f76b3c1f17d12c1b2bd5e1ed85c44a10e361 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Thu, 22 Oct 2026 15:39:28 +0200 Subject: [PATCH] Section walk: no per-section frustum test in columns entirely inside A column whose box lies entirely inside the frustum holds no section the frustum would reject (plane distances only grow from its nearest corner to a section's farthest, and rounding keeps the order), so its sections skip the test. At render distance 32 most columns are past the distance where the vertical field of view takes in their whole height. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/gfx.rs | 14 +++++ crates/horizon-client/src/gfx/resident.rs | 70 ++++++++++++++++++++--- 2 files changed, 75 insertions(+), 9 deletions(-) diff --git a/crates/horizon-client/src/gfx.rs b/crates/horizon-client/src/gfx.rs index 14c040c5c..d75a1144f 100644 --- a/crates/horizon-client/src/gfx.rs +++ b/crates/horizon-client/src/gfx.rs @@ -4059,6 +4059,20 @@ impl Frustum { p.truncate().dot(v) + p.w < 0.0 }) } + /// Whether the camera-relative box `min..max` is entirely inside. Then + /// `intersects` accepts every box within it: each plane's distance only + /// grows from this box's nearest corner to the inner box's farthest, and + /// rounding keeps that order. + pub fn contains(&self, min: Vec3, max: Vec3) -> bool { + self.planes.iter().all(|p| { + let v = Vec3::new( + if p.x >= 0.0 { min.x } else { max.x }, + if p.y >= 0.0 { min.y } else { max.y }, + if p.z >= 0.0 { min.z } else { max.z }, + ); + p.truncate().dot(v) + p.w >= 0.0 + }) + } } /// Vanilla's `Projection`: reversed depth (near maps to 1, far to 0, tests /// are GREATER_THAN_OR_EQUAL), which keeps far faces a hair apart, like diff --git a/crates/horizon-client/src/gfx/resident.rs b/crates/horizon-client/src/gfx/resident.rs index 49407e77a..51e5d8176 100644 --- a/crates/horizon-client/src/gfx/resident.rs +++ b/crates/horizon-client/src/gfx/resident.rs @@ -169,14 +169,20 @@ fn in_view(key: SectionKey, camera: DVec3, radius: i32, frustum: &Frustum) -> Op if !in_view_distance(camera_column(camera), radius, key.0, key.2) { return None; } - in_frustum(key, camera, frustum) + in_frustum(key, camera, frustum, false) } -/// `in_view` without the distance test. -fn in_frustum(key: SectionKey, camera: DVec3, frustum: &Frustum) -> Option<(f64, Vec3)> { +/// `in_view` without the distance test; `inside`: the section's column is +/// entirely inside the frustum, so the section is too. +fn in_frustum( + key: SectionKey, + camera: DVec3, + frustum: &Frustum, + inside: bool, +) -> Option<(f64, Vec3)> { let origin = origin(key, camera); let min = origin.as_vec3(); - if !frustum.intersects(min, min + Vec3::splat(16.0)) { + if !inside && !frustum.intersects(min, min + Vec3::splat(16.0)) { return None; } Some(((origin + DVec3::splat(8.0)).length_squared(), min)) @@ -192,8 +198,9 @@ impl Resident { /// Columns are tested first, with the same distance test (it depends /// only on x and z) and a box spanning their sections: every section box /// lies inside it and the frustum test rounds monotonically, so a column - /// the frustum rejects holds no section it would accept. `radius` is the - /// render distance in chunks. + /// the frustum rejects holds no section it would accept, and one + /// entirely inside holds none it would reject (its sections skip the + /// test). `radius` is the render distance in chunks. pub fn visible( &self, camera: DVec3, @@ -211,7 +218,9 @@ impl Resident { } let min = origin((column.x, column.low, column.z), camera).as_vec3(); let top = origin((column.x, column.high, column.z), camera).as_vec3(); - if !frustum.intersects(min, top + Vec3::splat(16.0)) { + let max = top + Vec3::splat(16.0); + let inside = frustum.contains(min, max); + if !inside && !frustum.intersects(min, max) { continue; } for &(y, slot) in &column.sections { @@ -219,7 +228,7 @@ impl Resident { if near && !reachable(key) { continue; } - if let Some((d, o)) = in_frustum(key, camera, frustum) { + if let Some((d, o)) = in_frustum(key, camera, frustum, inside) { out.push(Visible { distance: d.to_bits(), key, @@ -277,7 +286,7 @@ mod tests { if near { in_view(k, camera, radius, &frustum).filter(|_| reachable(k)) } else if beyond(k.0, k.2) { - in_frustum(k, camera, &frustum) + in_frustum(k, camera, &frustum, false) } else { None } @@ -298,6 +307,49 @@ mod tests { } } + #[test] + fn columns_entirely_inside_skip_no_section() { + // Short columns ahead of the camera: most lie entirely inside. + let mut r = Resident::default(); + for x in -12i32..=12 { + for z in 1..=12 { + for y in 3..6 { + if (x + y + z).rem_euclid(3) != 0 { + r.insert((x, y, z), ()); + } + } + } + } + let camera = DVec3::new(0.5, -8.0, 0.5); + let projection = super::super::world_projection(70.0, 16.0 / 9.0, 512.0); + let frustum = Frustum { + planes: super::super::frustum(projection * crate::camera::view_matrix(0.0, 5.0)), + }; + let inside = r + .columns + .iter() + .filter(|c| { + let min = origin((c.x, c.low, c.z), camera).as_vec3(); + let top = origin((c.x, c.high, c.z), camera).as_vec3(); + frustum.contains(min, top + Vec3::splat(16.0)) + }) + .count(); + assert!(inside > 50 && inside < r.columns.len(), "{inside}"); + let mut fast = Vec::new(); + r.visible(camera, 12, &frustum, |_| true, |_, _| false, &mut fast); + let mut slow: Vec<_> = r + .items + .iter() + .filter_map(|&(k, _)| in_view(k, camera, 12, &frustum).map(|(d, _)| (d, k))) + .collect(); + slow.sort_unstable_by(|a, b| a.0.total_cmp(&b.0).then(a.1.cmp(&b.1))); + let fast: Vec<_> = fast + .iter() + .map(|v| (f64::from_bits(v.distance), v.key)) + .collect(); + assert_eq!(fast, slow); + } + /// Every key reaches its value through its slot and column, and every /// column's bounds and sections match the keys. fn check(r: &Resident, model: &HashMap) {