From 3137c7b97799129bc5fa4dae6ff5ff211510e7a1 Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Thu, 15 Oct 2026 13:45:16 +0200 Subject: [PATCH] Relit chunks remesh only where their light changed (#270) A light job for a chunk that was already lit (a neighbour loaded, or a block changed in an unlit neighbour) dirtied every section of the 3x3 columns around it: 216 remeshes, whether or not any light changed. It now writes only the sections whose light changed and dirties the sections around the changed positions, as the incremental relight already does. A chunk lit for the first time still dirties its 3x3 neighbourhood. Co-Authored-By: Claude Opus 5.5 --- crates/horizon-client/src/light.rs | 68 ++++++++++++++++++++++++++ crates/horizon-client/src/streaming.rs | 28 ++++++++--- 2 files changed, 88 insertions(+), 8 deletions(-) diff --git a/crates/horizon-client/src/light.rs b/crates/horizon-client/src/light.rs index 6e1a1fece..936bc9655 100644 --- a/crates/horizon-client/src/light.rs +++ b/crates/horizon-client/src/light.rs @@ -363,6 +363,32 @@ pub fn apply_lighting(chunk: &mut Chunk, lights: Vec) { } } +/// `apply_lighting` for a chunk lit before: writes only the sections whose +/// light changed and returns the block positions that changed. +pub fn apply_relighting(chunk: &mut Chunk, lights: Vec) -> Vec<(i32, i32, i32)> { + let mut changed = Vec::new(); + let (bx, bz) = (chunk.x * 16, chunk.z * 16); + for (sy, (section, light)) in chunk.sections.iter_mut().zip(lights).enumerate() { + let old = §ion.light; + if let (Light::Uniform(a), Light::Uniform(b)) = (old, &light) + && a == b + { + continue; + } + let before = changed.len(); + for i in 0..4096 { + if old.get(i) != light.get(i) { + let y = MIN_Y + sy as i32 * 16 + (i >> 8) as i32; + changed.push((bx + (i & 15) as i32, y, bz + (i >> 4 & 15) as i32)); + } + } + if changed.len() > before { + Arc::make_mut(section).light = light; + } + } + changed +} + /// Resident chunks the incremental relighter reads and writes. pub trait LightWorld { fn state(&self, x: i32, y: i32, z: i32) -> Option; @@ -749,6 +775,48 @@ mod tests { } } + #[test] + fn relighting_reports_exactly_the_changed_positions() { + let mut chunk = Chunk::empty(1, -1); + chunk.set(3, MIN_Y + 20, 5, TORCH); + let lit = |c: &Chunk| { + let region = [[None; 3], [None, Some(c), None], [None; 3]]; + light_chunk(®ion, &tables(), true) + }; + let light = lit(&chunk); + apply_lighting(&mut chunk, light); + let sections: Vec<_> = chunk.sections.iter().map(Arc::as_ptr).collect(); + // The same light again: nothing changes and no section is copied. + let light = lit(&chunk); + assert!(apply_relighting(&mut chunk, light).is_empty()); + assert_eq!( + sections, + chunk.sections.iter().map(Arc::as_ptr).collect::>() + ); + let before = chunk.clone(); + chunk.set(3, MIN_Y + 20, 5, STONE); + let light = lit(&chunk); + let changed = apply_relighting(&mut chunk, light); + let mut expected = Vec::new(); + for (sy, (old, new)) in before.sections.iter().zip(&chunk.sections).enumerate() { + for i in 0..4096 { + if old.light(i) != new.light(i) { + let (x, y, z) = (i as i32 & 15, (i >> 8) as i32, (i >> 4) as i32 & 15); + expected.push((16 + x, MIN_Y + sy as i32 * 16 + y, -16 + z)); + } + } + } + assert!(!expected.is_empty()); + assert_eq!(changed, expected); + // Only the torch's section and the two it lights into are copied. + let copied = sections + .iter() + .zip(&chunk.sections) + .filter(|(old, new)| **old != Arc::as_ptr(new)) + .count(); + assert!(copied <= 3, "{copied} sections copied"); + } + #[test] fn empty_chunk_missing_neighbours_and_no_sky() { let mut chunk = Chunk::empty(0, 0); diff --git a/crates/horizon-client/src/streaming.rs b/crates/horizon-client/src/streaming.rs index 5b252e384..cc3b23aaa 100644 --- a/crates/horizon-client/src/streaming.rs +++ b/crates/horizon-client/src/streaming.rs @@ -5,7 +5,7 @@ use crate::fast_hash::{FxHashMap, FxHashSet}; use crate::{ chunk::{Chunk, SECTIONS}, - light::{LightTables, apply_lighting, light_chunk}, + light::{LightTables, apply_lighting, apply_relighting, light_chunk}, mesher::{MeshTables, Mesher, Region, SectionMesh, borrow}, world::World, }; @@ -636,17 +636,29 @@ impl Streaming { if let Some(chunk) = world.chunks.get_mut(&(x, z)) { let chunk = Arc::make_mut(chunk); // Keep blocks edited since the snapshot; take only light. - apply_lighting(chunk, sections); + // A chunk relit (a neighbour loaded or changed) remeshes + // only around the light that changed, as vanilla's light + // updates dirty only their sections (#270). + let first = self.lit.insert((x, z)); + let changed = if first { + apply_lighting(chunk, sections); + Vec::new() + } else { + apply_relighting(chunk, sections) + }; chunk.lit = true; - self.lit.insert((x, z)); - self.replay_light_edits(world, x, z, snapshot); - for dz in -1..=1 { - for dx in -1..=1 { - for sy in 0..SECTIONS as i32 { - world.dirty_sections.insert((x + dx, sy, z + dz)); + if first { + for dz in -1..=1 { + for dx in -1..=1 { + for sy in 0..SECTIONS as i32 { + world.dirty_sections.insert((x + dx, sy, z + dz)); + } } } + } else { + Self::mark_light_changes(world, &changed); } + self.replay_light_edits(world, x, z, snapshot); } } Done::Meshed(x, sy, z, mesh, seq) => {