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 <noreply@anthropic.com>
This commit is contained in:
Ludovic Marie
2026-10-15 13:45:16 +02:00
parent 337ee470b5
commit 3137c7b977
2 changed files with 88 additions and 8 deletions
+68
View File
@@ -363,6 +363,32 @@ pub fn apply_lighting(chunk: &mut Chunk, lights: Vec<Light>) {
}
}
/// `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<Light>) -> 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 = &section.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<u16>;
@@ -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(&region, &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::<Vec<_>>()
);
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);
+20 -8
View File
@@ -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) => {