diff --git a/crates/horizon-client/src/gfx.rs b/crates/horizon-client/src/gfx.rs index 073643809..dbb991027 100644 --- a/crates/horizon-client/src/gfx.rs +++ b/crates/horizon-client/src/gfx.rs @@ -62,6 +62,7 @@ mod java_stages; mod occlusion; mod pack_stages; pub mod panorama; +mod resident; mod timing; pub(crate) mod upload; mod world_post; @@ -98,8 +99,6 @@ struct SectionGpu { /// The solid quads by facing: back-facing groups are skipped. faces: crate::mesher::FaceGroups, } -/// A visible section this frame: key, squared distance, camera-relative origin. -type Visible = (SectionKey, f64, [f32; 3]); /// Line segments the hit outline can hold. const MAX_OUTLINE_EDGES: u64 = 256; pub struct Frame<'a> { @@ -310,7 +309,7 @@ pub struct Renderer { instance_capacity: u64, outline: wgpu::Buffer, depth: wgpu::TextureView, - sections: HashMap, + sections: resident::Resident, arena: arena::Arena, /// `DrawIndexedIndirectArgs` for this frame's solid then translucent draws. indirect: wgpu::Buffer, @@ -318,7 +317,7 @@ pub struct Renderer { /// Indirect draws may set `first_instance` (the section origin). pub multi_draw: bool, /// Per-frame scratch, kept to avoid allocating every frame. - visible: Vec, + visible: Vec, origins: Vec<[f32; 4]>, /// `DrawIndexedIndirectArgs` words: index count, instance count, first /// index, base vertex, first instance. @@ -1576,7 +1575,7 @@ impl Renderer { instance_capacity, outline, depth, - sections: HashMap::new(), + sections: resident::Resident::default(), arena, indirect, indirect_capacity: 1024, @@ -1723,7 +1722,7 @@ impl Renderer { self.sections.len() } pub fn has_section(&self, key: SectionKey) -> bool { - self.sections.contains_key(&key) + self.sections.contains(key) } pub fn upload(&mut self, key: SectionKey, mesh: &SectionMesh) { self.occlusion.set(key, mesh.visibility); @@ -1792,7 +1791,7 @@ impl Renderer { } } fn release_section(&mut self, key: SectionKey) { - if let Some(s) = self.sections.remove(&key) { + if let Some(s) = self.sections.remove(key) { self.arena .release(s.page, s.start, u64::from(s.solid + s.translucent) * 4); } @@ -2199,34 +2198,13 @@ impl Renderer { let reachable = &self.occlusion; let mut visible = std::mem::take(&mut self.visible); visible.clear(); - visible.extend( - self.sections - .keys() - .filter(|&&k| !culling || reachable.contains(k)) - .filter_map(|&(sx, sy, sz)| { - let origin = DVec3::new( - f64::from(sx * 16), - f64::from(crate::chunk::MIN_Y + sy * 16), - f64::from(sz * 16), - ) - camera; - let center = origin + DVec3::splat(8.0); - let horizontal = (center.x * center.x + center.z * center.z).sqrt(); - if horizontal > f64::from(frame.distance) + 24.0 { - return None; - } - let min = origin.as_vec3(); - let max = min + Vec3::splat(16.0); - if !view.intersects(min, max) { - return None; - } - Some(( - (sx, sy, sz), - center.length_squared(), - origin.as_vec3().to_array(), - )) - }), + self.sections.visible( + camera, + frame.distance, + &view, + |k| !culling || reachable.contains(k), + &mut visible, ); - visible.sort_unstable_by(|a, b| a.1.total_cmp(&b.1)); self.drawn_sections = visible.len(); if visible.len() as u64 > self.instance_capacity { self.instance_capacity = (visible.len() as u64).next_power_of_two(); @@ -2238,8 +2216,11 @@ impl Renderer { }); } self.origins.clear(); - self.origins - .extend(visible.iter().map(|(_, _, o)| [o[0], o[1], o[2], 0.0])); + self.origins.extend( + visible + .iter() + .map(|v| [v.origin[0], v.origin[1], v.origin[2], 0.0]), + ); if !self.origins.is_empty() { self.uploads.buffer( &mut encoder, @@ -2259,18 +2240,18 @@ impl Renderer { let eye = (frame.bob * crate::camera::view_matrix(frame.yaw, frame.pitch)) .inverse() .transform_point3(Vec3::ZERO); - for (i, (key, _, origin)) in visible.iter().enumerate() { - let s = &self.sections[key]; + for (i, v) in visible.iter().enumerate() { + let s = self.sections.get(v.slot); // Only the solid groups that can face the eye. - s.faces.ranges(eye - Vec3::from(*origin), |first, quads| { + s.faces.ranges(eye - Vec3::from(v.origin), |first, quads| { let first = s.start + u64::from(first) * 4; self.draws.push([quads * 6, 1, 0, first as u32, i as u32]); self.draw_pages.push(s.page); }); } let solid_draws = self.draws.len() as u32; - for (i, (key, _, _)) in visible.iter().enumerate().rev() { - let s = &self.sections[key]; + for (i, v) in visible.iter().enumerate().rev() { + let s = self.sections.get(v.slot); if s.translucent > 0 { let first = s.start + u64::from(s.solid) * 4; self.draws diff --git a/crates/horizon-client/src/gfx/resident.rs b/crates/horizon-client/src/gfx/resident.rs new file mode 100644 index 000000000..58e187090 --- /dev/null +++ b/crates/horizon-client/src/gfx/resident.rs @@ -0,0 +1,381 @@ +//! Resident sections, stored densely and grouped by chunk column for the +//! per-frame culling walk: a column whose box misses the frustum or lies past +//! the render distance rejects all of its sections with one test, and the +//! sections that remain are reached by index rather than by hashing keys. +use super::{Frustum, SectionKey}; +use crate::fast_hash::FxHashMap; +use glam::{DVec3, Vec3}; + +/// One chunk column's resident sections. +struct Column { + x: i32, + z: i32, + /// Section y (from the world's bottom) and slot, in no particular order. + sections: Vec<(i32, u32)>, + /// The lowest and highest section y of `sections`. + low: i32, + high: i32, +} + +impl Column { + fn bounds(&mut self) { + self.low = self.sections.iter().map(|s| s.0).min().unwrap_or(0); + self.high = self.sections.iter().map(|s| s.0).max().unwrap_or(0); + } +} + +/// Values of type `T` per section key, in a dense list with stable keys and +/// slots that move only when a section is removed. +pub struct Resident { + items: Vec<(SectionKey, T)>, + slots: FxHashMap, + columns: Vec, + column_index: FxHashMap<(i32, i32), u32>, +} + +impl Default for Resident { + fn default() -> Self { + Self { + items: Vec::new(), + slots: FxHashMap::default(), + columns: Vec::new(), + column_index: FxHashMap::default(), + } + } +} + +impl Resident { + pub fn len(&self) -> usize { + self.items.len() + } + pub fn contains(&self, key: SectionKey) -> bool { + self.slots.contains_key(&key) + } + pub fn values(&self) -> impl Iterator { + self.items.iter().map(|(_, v)| v) + } + /// The section in `slot` (`Visible::slot`). + pub fn get(&self, slot: u32) -> &T { + &self.items[slot as usize].1 + } + pub fn clear(&mut self) { + self.items.clear(); + self.slots.clear(); + self.columns.clear(); + self.column_index.clear(); + } + /// Adds or replaces `key`'s value. + pub fn insert(&mut self, key: SectionKey, value: T) { + if let Some(&slot) = self.slots.get(&key) { + self.items[slot as usize].1 = value; + return; + } + let slot = self.items.len() as u32; + self.items.push((key, value)); + self.slots.insert(key, slot); + let column = match self.column_index.get(&(key.0, key.2)) { + Some(&c) => &mut self.columns[c as usize], + None => { + self.column_index + .insert((key.0, key.2), self.columns.len() as u32); + self.columns.push(Column { + x: key.0, + z: key.2, + sections: Vec::new(), + low: key.1, + high: key.1, + }); + self.columns.last_mut().expect("just pushed") + } + }; + column.sections.push((key.1, slot)); + column.low = column.low.min(key.1); + column.high = column.high.max(key.1); + } + pub fn remove(&mut self, key: SectionKey) -> Option { + let slot = self.slots.remove(&key)?; + let c = self.column_index[&(key.0, key.2)] as usize; + let column = &mut self.columns[c]; + let at = column + .sections + .iter() + .position(|s| s.1 == slot) + .expect("section in its column"); + column.sections.swap_remove(at); + if column.sections.is_empty() { + self.column_index.remove(&(key.0, key.2)); + self.columns.swap_remove(c); + if let Some(moved) = self.columns.get(c) { + self.column_index.insert((moved.x, moved.z), c as u32); + } + } else { + column.bounds(); + } + let (_, value) = self.items.swap_remove(slot as usize); + // The last item now sits in `slot`: point its key and column at it. + if let Some(&(moved, _)) = self.items.get(slot as usize) { + let last = self.items.len() as u32; + self.slots.insert(moved, slot); + let column = &mut self.columns[self.column_index[&(moved.0, moved.2)] as usize]; + let entry = column + .sections + .iter_mut() + .find(|s| s.1 == last) + .expect("moved section in its column"); + entry.1 = slot; + } + Some(value) + } +} + +/// A section in view. +#[derive(Clone, Copy, Debug, PartialEq)] +pub struct Visible { + /// Its centre's squared distance from the camera, as bits: non-negative + /// floats order like their bits, so sorting compares integers. + pub distance: u64, + pub key: SectionKey, + pub slot: u32, + /// Its camera-relative origin. + pub origin: [f32; 3], +} + +/// The camera-relative origin of the section at `key`. +fn origin(key: SectionKey, camera: DVec3) -> DVec3 { + DVec3::new( + f64::from(key.0 * 16), + f64::from(crate::chunk::MIN_Y + key.1 * 16), + f64::from(key.2 * 16), + ) - camera +} + +/// Whether the section at `key` is drawn: within `distance` blocks of the +/// camera horizontally (plus a section and a half), and partly inside the +/// frustum. `None` when it is not, else its squared centre distance and origin. +fn in_view( + key: SectionKey, + camera: DVec3, + distance: f32, + frustum: &Frustum, +) -> Option<(f64, Vec3)> { + let origin = origin(key, camera); + let center = origin + DVec3::splat(8.0); + let horizontal = (center.x * center.x + center.z * center.z).sqrt(); + if horizontal > f64::from(distance) + 24.0 { + return None; + } + let min = origin.as_vec3(); + if !frustum.intersects(min, min + Vec3::splat(16.0)) { + return None; + } + Some((center.length_squared(), min)) +} + +impl Resident { + /// Appends the sections `in_view` and `reachable` to `out`, nearest + /// first (ties by key, so the order never depends on storage). + /// + /// 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. + pub fn visible( + &self, + camera: DVec3, + distance: f32, + frustum: &Frustum, + reachable: impl Fn(SectionKey) -> bool, + out: &mut Vec, + ) { + for column in &self.columns { + let low = origin((column.x, column.low, column.z), camera); + let center = low + DVec3::splat(8.0); + if (center.x * center.x + center.z * center.z).sqrt() > f64::from(distance) + 24.0 { + continue; + } + let min = low.as_vec3(); + let top = origin((column.x, column.high, column.z), camera).as_vec3(); + if !frustum.intersects(min, top + Vec3::splat(16.0)) { + continue; + } + for &(y, slot) in &column.sections { + let key = (column.x, y, column.z); + if !reachable(key) { + continue; + } + if let Some((d, o)) = in_view(key, camera, distance, frustum) { + out.push(Visible { + distance: d.to_bits(), + key, + slot, + origin: o.to_array(), + }); + } + } + } + out.sort_unstable_by_key(|v| (v.distance, v.key)); + } +} + +#[cfg(test)] +mod tests { + use super::*; + use std::collections::HashMap; + + #[test] + fn column_walk_finds_exactly_the_sections_in_view() { + let mut r = Resident::default(); + for x in -12i32..=12 { + for z in -12..=12 { + for y in 0..24 { + // Leave holes so columns have uneven bounds. + if (x * 7 + y * 3 + z * 5).rem_euclid(4) != 0 { + r.insert((x, y, z), ()); + } + } + } + } + let reachable = |k: SectionKey| (k.0 + k.1 + k.2).rem_euclid(9) != 0; + let cameras = [ + (DVec3::new(0.5, 70.0, 0.5), 0.0, 0.0), + (DVec3::new(-37.25, 130.5, 21.75), 135.0, 35.0), + (DVec3::new(100.0, -60.0, -3.0), 270.0, -80.0), + (DVec3::new(8.0, 64.0, 8.0), 45.0, 89.0), + ]; + for (camera, yaw, pitch) in cameras { + for distance in [32.0, 96.0, 160.0] { + let projection = super::super::world_projection(70.0, 16.0 / 9.0, 512.0); + let view = crate::camera::view_matrix(yaw, pitch); + let frustum = Frustum { + planes: super::super::frustum(projection * view), + }; + let mut fast = Vec::new(); + r.visible(camera, distance, &frustum, reachable, &mut fast); + let mut slow: Vec<(SectionKey, f64, [f32; 3])> = r + .items + .iter() + .filter(|(k, _)| reachable(*k)) + .filter_map(|(k, _)| { + in_view(*k, camera, distance, &frustum).map(|(d, o)| (*k, d, o.to_array())) + }) + .collect(); + slow.sort_unstable_by(|a, b| a.1.total_cmp(&b.1).then(a.0.cmp(&b.0))); + let fast: Vec<_> = fast + .iter() + .map(|v| { + assert_eq!(r.items[v.slot as usize].0, v.key); + (v.key, f64::from_bits(v.distance), v.origin) + }) + .collect(); + assert!(!slow.is_empty()); + assert_eq!(fast, slow, "{camera} {yaw} {pitch} {distance}"); + } + } + } + + /// 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) { + assert_eq!(r.len(), model.len()); + let mut seen = 0; + for column in &r.columns { + assert!(!column.sections.is_empty()); + let ys: Vec = column.sections.iter().map(|s| s.0).collect(); + assert_eq!(column.low, *ys.iter().min().unwrap()); + assert_eq!(column.high, *ys.iter().max().unwrap()); + for &(y, slot) in &column.sections { + let (key, value) = &r.items[slot as usize]; + assert_eq!(*key, (column.x, y, column.z)); + assert_eq!(model[key], *value); + seen += 1; + } + } + assert_eq!(seen, model.len()); + for key in model.keys() { + assert!(r.contains(*key)); + } + } + + #[test] + fn slots_and_columns_follow_inserts_and_removals() { + let mut r = Resident::default(); + let mut model = HashMap::new(); + // A deterministic mix of inserts, replacements and removals. + let mut seed = 0x2545_f491_u32; + for step in 0..4000u32 { + seed ^= seed << 13; + seed ^= seed >> 17; + seed ^= seed << 5; + let key = ( + (seed % 5) as i32 - 2, + ((seed >> 4) % 6) as i32 - 1, + ((seed >> 8) % 5) as i32 - 2, + ); + if (seed >> 12).is_multiple_of(3) { + assert_eq!(r.remove(key), model.remove(&key)); + } else { + r.insert(key, step); + model.insert(key, step); + } + check(&r, &model); + } + r.clear(); + assert_eq!(r.len(), 0); + assert!(r.columns.is_empty()); + } + + /// The old walk (every key of a hash map, sorted by distance, then two + /// lookups per visible section) against `visible` on a render distance + /// 32 world. Run with `cargo test --profile fast -p horizon-client --lib + /// -- --ignored column_walk_speed --nocapture`. + #[test] + #[ignore = "timing, not a check"] + fn column_walk_speed() { + use std::time::{Duration, Instant}; + let mut r = Resident::default(); + let mut map = HashMap::new(); + for x in -33i32..=33 { + for z in -33i32..=33 { + for y in 0..24 { + if (x * 7 + y * 3 + z * 5).rem_euclid(4) != 0 { + r.insert((x, y, z), [x, y, z]); + map.insert((x, y, z), [x, y, z]); + } + } + } + } + let camera = DVec3::new(8.5, 100.0, 8.5); + let projection = super::super::world_projection(70.0, 16.0 / 9.0, 1024.0); + let frustum = Frustum { + planes: super::super::frustum(projection * crate::camera::view_matrix(30.0, 35.0)), + }; + let (mut old, mut new) = (Duration::MAX, Duration::MAX); + let (mut list, mut keys) = (Vec::new(), Vec::new()); + let mut sum = 0i64; + for _ in 0..200 { + let t = Instant::now(); + keys.clear(); + keys.extend(map.keys().filter_map(|&k| { + in_view(k, camera, 512.0, &frustum).map(|(d, o)| (k, d, o.to_array())) + })); + keys.sort_unstable_by(|a, b| a.1.total_cmp(&b.1)); + for (k, _, _) in &keys { + sum += i64::from(map[k][1] + map[k][0]); + } + old = old.min(t.elapsed()); + let t = Instant::now(); + list.clear(); + r.visible(camera, 512.0, &frustum, |_| true, &mut list); + for v in &list { + sum += i64::from(r.get(v.slot)[1] + r.get(v.slot)[0]); + } + new = new.min(t.elapsed()); + } + assert_eq!(keys.len(), list.len()); + println!( + "{} resident, {} visible: old walk {old:?}, column walk {new:?} (checksum {sum})", + r.len(), + list.len() + ); + } +} diff --git a/crates/horizon-client/src/mesher/facing.rs b/crates/horizon-client/src/mesher/facing.rs index 2440bf926..2a7f0ba61 100644 --- a/crates/horizon-client/src/mesher/facing.rs +++ b/crates/horizon-client/src/mesher/facing.rs @@ -80,26 +80,22 @@ impl FaceGroups { } } +/// The axis and sign of each axis group (`UNALIGNED`'s entry is unused). +const AXES: [(usize, bool); GROUPS] = { + let mut axes = [(0, false); GROUPS]; + let mut axis = 0; + while axis < 3 { + axes[NEGATIVE[axis]] = (axis, false); + axes[GROUPS - 1 - NEGATIVE[axis]] = (axis, true); + axis += 1; + } + axes +}; + /// The axis and sign of axis group `g`. fn axis(g: usize) -> (usize, bool) { - if g < UNALIGNED { - ( - NEGATIVE - .iter() - .position(|&n| n == g) - .expect("negative group"), - false, - ) - } else { - let n = GROUPS - 1 - g; - ( - NEGATIVE - .iter() - .position(|&m| m == n) - .expect("positive group"), - true, - ) - } + debug_assert_ne!(g, UNALIGNED); + AXES[g] } /// The group of one quad, and its plane: an axis group only when all four