From 2ec71e53875b9db9e0ed5fa2932c9f23ef47e89c Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Sun, 18 Oct 2026 11:52:47 +0200 Subject: [PATCH] Mesh scheduling looks at dirty sections column by column Sections waiting to be remeshed were one hash set that the render thread walked section by section every frame, though every check (column loaded, in view, its 3x3 lit, its region) holds for a whole column. At RD 32 about 12,000 sections wait at the edge of the lit area at any time. They are now kept per column (a bit per section index), so each frame looks at each column once and meshes all of its ready sections together. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/streaming.rs | 107 ++++++++++++++----------- crates/horizon-client/src/world.rs | 42 +++++++++- 2 files changed, 99 insertions(+), 50 deletions(-) diff --git a/crates/horizon-client/src/streaming.rs b/crates/horizon-client/src/streaming.rs index 6acdcafd2..4d658fd56 100644 --- a/crates/horizon-client/src/streaming.rs +++ b/crates/horizon-client/src/streaming.rs @@ -544,7 +544,7 @@ impl Streaming { self.lighting.remove(&(x, z)); self.meshing.retain(|k, _| (k.0, k.2) != (x, z)); self.waiting.retain(|k| (k.0, k.2) != (x, z)); - world.dirty_sections.retain(|k| (k.0, k.2) != (x, z)); + world.dirty_sections.take_column(x, z); world.chunks.remove(&(x, z)) } fn submit(&mut self, priority: i64, job: Job) { @@ -809,7 +809,9 @@ impl Streaming { let far: HashSet<(i32, i32)> = far.iter().copied().collect(); self.meshing.retain(|k, _| !far.contains(&(k.0, k.2))); self.waiting.retain(|k| !far.contains(&(k.0, k.2))); - world.dirty_sections.retain(|k| !far.contains(&(k.0, k.2))); + world + .dirty_sections + .retain_columns(|x, z| !far.contains(&(x, z))); } // Drop far proto chunks that are not checked out. let (center, r4) = (self.center, i64::from(self.radius + 4)); @@ -1025,66 +1027,63 @@ impl Streaming { !back }); } - let dirty: Vec<_> = world.dirty_sections.iter().copied().collect(); - let mut columns: FxHashMap<(i32, i32), Option>> = FxHashMap::default(); - let mut ready_columns: FxHashMap<(i32, i32), bool> = FxHashMap::default(); - for (x, sy, z) in dirty { + // Column by column: the checks below hold for all of a column's + // sections, and columns waiting for light cost one look per frame. + let columns: Vec<_> = world.dirty_sections.columns().collect(); + for ((x, z), sections) in columns { if !world.chunks.contains_key(&(x, z)) { - world.dirty_sections.remove(&(x, sy, z)); + world.dirty_sections.take_column(x, z); continue; } if !self.ranges().in_view(x, z) { - world.dirty_sections.remove(&(x, sy, z)); - self.waiting.insert((x, sy, z)); + world.dirty_sections.take_column(x, z); + for sy in section_indexes(sections) { + self.waiting.insert((x, sy, z)); + } continue; } - // Once per column: the 3x3 check would otherwise repeat for each - // of its sections every frame. A neighbour being lit dirties this - // column again when its light job finishes (new chunks relight - // their eight neighbours), so wait for it rather than mesh once - // per neighbour. Columns still in `dirty_light` here can't be lit - // yet (a neighbour of theirs is missing): waiting for them could - // last until the player comes back. - let (lit, lighting) = (&self.lit, &self.lighting); - let ready = *ready_columns.entry((x, z)).or_insert_with(|| { - (-1..=1).all(|dz| { - (-1..=1).all(|dx| { - let n = (x + dx, z + dz); - lit.contains(&n) && !lighting.contains_key(&n) - }) + // A neighbour being lit dirties this column again when its light + // job finishes (new chunks relight their eight neighbours), so + // wait for it rather than mesh once per neighbour. Columns still + // in `dirty_light` here can't be lit yet (a neighbour of theirs is + // missing): waiting for them could last until the player comes + // back. + let ready = (-1..=1).all(|dz| { + (-1..=1).all(|dx| { + let n = (x + dx, z + dz); + self.lit.contains(&n) && !self.lighting.contains_key(&n) }) }); if !ready { continue; } - let region = columns - .entry((x, z)) - .or_insert_with(|| Self::region(world, x, z)); - let Some(region) = region.clone() else { + let Some(region) = Self::region(world, x, z) else { continue; }; - world.dirty_sections.remove(&(x, sy, z)); - let first = self.meshed.insert((x, z)); - self.sequence += 1; - self.meshing.insert((x, sy, z), self.sequence); - self.submit( - self.distance(x, z) * 4 + if first { 2 } else { 0 }, - Job::Mesh( - x, - sy, - z, - region, - self.sequence, - MeshSettings { - nether: self.nether_shading, - zoom: self.biome_zoom, - blend_radius: self.biome_blend, - smooth_lighting: self.smooth_lighting, - cutout_leaves: self.cutout_leaves, - cull_leaves: self.cull_leaves, - }, - ), - ); + world.dirty_sections.take_column(x, z); + for sy in section_indexes(sections) { + let first = self.meshed.insert((x, z)); + self.sequence += 1; + self.meshing.insert((x, sy, z), self.sequence); + self.submit( + self.distance(x, z) * 4 + if first { 2 } else { 0 }, + Job::Mesh( + x, + sy, + z, + region.clone(), + self.sequence, + MeshSettings { + nether: self.nether_shading, + zoom: self.biome_zoom, + blend_radius: self.biome_blend, + smooth_lighting: self.smooth_lighting, + cutout_leaves: self.cutout_leaves, + cull_leaves: self.cull_leaves, + }, + ), + ); + } } (uploads, removed) } @@ -1355,6 +1354,16 @@ impl Drop for Streaming { } } } +/// The section indexes set in a column's bits, lowest first. +fn section_indexes(mut bits: u32) -> impl Iterator { + std::iter::from_fn(move || { + (bits != 0).then(|| { + let sy = bits.trailing_zeros() as i32; + bits &= bits - 1; + sy + }) + }) +} #[cfg(test)] mod tests { use super::*; diff --git a/crates/horizon-client/src/world.rs b/crates/horizon-client/src/world.rs index efcbf9591..350201e5e 100644 --- a/crates/horizon-client/src/world.rs +++ b/crates/horizon-client/src/world.rs @@ -8,6 +8,46 @@ use std::{ sync::Arc, }; +/// Sections (x, section index, z) waiting to be remeshed, kept by column +/// (a bit per section index), so the mesh scheduler looks at each column once +/// rather than at every section of it. +#[derive(Clone, Debug, Default)] +pub struct DirtySections { + columns: crate::fast_hash::FxHashMap<(i32, i32), u32>, +} +const _: () = assert!(SECTIONS <= 32); +impl DirtySections { + pub fn insert(&mut self, (x, sy, z): (i32, i32, i32)) { + debug_assert!((0..SECTIONS as i32).contains(&sy)); + *self.columns.entry((x, z)).or_default() |= 1 << sy; + } + pub fn contains(&self, &(x, sy, z): &(i32, i32, i32)) -> bool { + (0..SECTIONS as i32).contains(&sy) + && self.columns.get(&(x, z)).is_some_and(|m| m & 1 << sy != 0) + } + /// The dirty section indexes of column (x, z), as bits, removed. + pub fn take_column(&mut self, x: i32, z: i32) -> u32 { + self.columns.remove(&(x, z)).unwrap_or(0) + } + /// Keep the columns `keep` accepts. + pub fn retain_columns(&mut self, mut keep: impl FnMut(i32, i32) -> bool) { + self.columns.retain(|&(x, z), _| keep(x, z)); + } + /// The columns holding dirty sections, with their section bits. + pub fn columns(&self) -> impl Iterator + '_ { + self.columns.iter().map(|(&c, &m)| (c, m)) + } + pub fn clear(&mut self) { + self.columns.clear(); + } + pub fn is_empty(&self) -> bool { + self.columns.is_empty() + } + /// How many sections are dirty. + pub fn len(&self) -> usize { + self.columns.values().map(|m| m.count_ones() as usize).sum() + } +} pub struct World { pub fast_lava: bool, pub chunks: crate::chunk_map::ChunkMap, @@ -16,7 +56,7 @@ pub struct World { pub health_rules: crate::health::HealthRules, pub air: BlockStateId, /// Sections whose geometry changed and need remeshing. - pub dirty_sections: crate::fast_hash::FxHashSet<(i32, i32, i32)>, + pub dirty_sections: DirtySections, /// Chunks whose light must be recomputed. pub dirty_light: crate::fast_hash::FxHashSet<(i32, i32)>, /// Edits on lit chunks (position, old state, new state), relit