Load saved light and test it against vanilla

Chunks saved with isLightOn now read their SkyLight and BlockLight arrays back like vanilla instead of being relit: a missing sky layer repeats the bottom layer of the section above it, and a missing block layer is dark. Such chunks skip the light job, mesh as soon as their neighbours are lit, and only relight when a freshly generated neighbour arrives. tools/test_light_parity.py builds a scene of slabs, stairs, snow, trapdoors, lanterns, leaves and lava in a vanilla world, edits it, and requires every saved light value and every incremental relight to match Horizon's engine.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Ludovic Marie
2026-10-09 12:29:51 +02:00
parent e988862e92
commit fb954f7d7f
4 changed files with 220 additions and 2 deletions
+3
View File
@@ -94,6 +94,8 @@ pub struct Chunk {
/// Highest non-air, light-blocking-or-fluid Y per column (MOTION_BLOCKING-like), for rain/sky.
pub heights: Arc<[i16; 256]>,
pub lit: bool,
/// Light read from a save with `isLightOn`: no light job is needed.
pub light_loaded: bool,
/// Newly generated (not loaded from a save): eligible for generation-time spawns.
pub fresh: bool,
/// Block entities read from a save, moved into the world on arrival.
@@ -120,6 +122,7 @@ impl Chunk {
biomes: Arc::new(vec![0; 16 * (HEIGHT as usize / 4)]),
heights: Arc::new([MIN_Y as i16; 256]),
lit: false,
light_loaded: false,
fresh: false,
block_entities: Vec::new(),
structure_refs: Vec::new(),
+103 -1
View File
@@ -901,6 +901,60 @@ pub fn decode_with(
Some(chunk)
}
/// Read the stored `SkyLight`/`BlockLight` layers of a chunk saved with
/// `isLightOn` (`SerializableChunkData.read` + `ThreadedLevelLightEngine`),
/// so it needs no relighting. A missing sky layer repeats the bottom layer of
/// the section above it (15 above the highest stored one); a missing block
/// layer is dark.
pub fn load_light(tag: &Tag, chunk: &mut Chunk, sky_light: bool) {
if tag.get("isLightOn") != Some(&Tag::Byte(1)) {
return;
}
let Some(Tag::List(sections)) = tag.get("sections") else {
return;
};
let mut layers: Vec<(Option<&[u8]>, Option<&[u8]>)> = vec![(None, None); SECTIONS];
for section in sections {
let Some(y) = number(section.get("Y")) else {
continue;
};
let si = y as i32 - (MIN_Y >> 4);
if !(0..SECTIONS as i32).contains(&si) {
continue;
}
let layer = |name: &str| match section.get(name) {
Some(Tag::ByteArray(a)) if a.len() == 2048 => Some(&a[..]),
_ => None,
};
layers[si as usize] = (layer("SkyLight"), layer("BlockLight"));
}
let sky_default = if sky_light { 15u8 } else { 0 };
// Sky nibbles of the section above's bottom layer, going down.
let mut above: [u8; 256] = [sky_default; 256];
for si in (0..SECTIONS).rev() {
let (sky, block) = layers[si];
let mut data = Box::new([0u8; 4096]);
let nibble = |a: &[u8], i: usize| (a[i >> 1] >> ((i & 1) * 4)) & 15;
for (i, packed) in data.iter_mut().enumerate() {
let s = match sky {
Some(a) if sky_light => nibble(a, i),
_ => above[i & 255],
};
let b = block.map_or(0, |a| nibble(a, i));
*packed = s << 4 | b;
}
for (i, a) in above.iter_mut().enumerate() {
*a = data[i] >> 4;
}
let mut section = (*chunk.sections[si]).clone();
section.light = Light::Full(data);
section.compact();
chunk.sections[si] = Arc::new(section);
}
chunk.lit = true;
chunk.light_loaded = true;
}
type RegionCache = Mutex<HashMap<(i32, i32), Option<Arc<Mutex<RegionFile>>>>>;
/// One dimension folder (`dimensions/minecraft/<name>`). Region files are
/// read lazily by worker threads.
@@ -951,7 +1005,9 @@ impl SaveDir {
pub fn load(&self, x: i32, z: i32, registry: &BlockRegistry, biomes: &Biomes) -> Option<Chunk> {
let root = self.read_record("region", x, z)?;
let tables = self.tables.get().map(|t| &**t);
decode_with(&root, registry, biomes, tables)
let mut chunk = decode_with(&root, registry, biomes, tables)?;
load_light(&root.tag, &mut chunk, self.sky_light);
Some(chunk)
}
/// Entities saved for a chunk (`entities/`; older chunks may still keep
/// them in the chunk's own `entities` list).
@@ -2016,6 +2072,52 @@ mod tests {
assert_eq!(nibbles(&light, true)[0], 0x5F);
}
#[test]
fn stored_light_loads_with_vanilla_defaults() {
let mut sky = vec![0u8; 2048];
sky[0] = 0x5A; // x=0 -> 10, x=1 -> 5
let section = |y: i8, entries: Vec<(&str, Tag)>| {
let mut all = vec![("Y", Tag::Byte(y))];
all.extend(entries);
Tag::compound(all)
};
let tag = Tag::compound([
("isLightOn", Tag::Byte(1)),
(
"sections",
Tag::List(vec![
section(
-4,
vec![
("SkyLight", Tag::ByteArray(sky)),
("BlockLight", Tag::ByteArray(vec![0x21; 2048])),
],
),
// No sky layer: repeats the bottom layer of the section above.
section(-3, vec![]),
]),
),
]);
let mut chunk = Chunk::empty(0, 0);
load_light(&tag, &mut chunk, true);
assert!(chunk.lit && chunk.light_loaded);
let at = |c: &Chunk, si: usize, i: usize| c.sections[si].light(i);
assert_eq!(at(&chunk, 0, 0), 0xA1, "stored sky 10, block 1");
assert_eq!(at(&chunk, 0, 1), 0x52, "stored sky 5, block 2");
// Section -3 has no layers; above it (-2..) sky defaults to 15.
assert_eq!(at(&chunk, 1, 0), 0xF0);
assert_eq!(at(&chunk, SECTIONS - 1, 5), 0xF0);
let mut dark = Chunk::empty(0, 0);
load_light(&tag, &mut dark, false);
assert_eq!(at(&dark, 1, 0), 0x00, "no skylight dimension");
let mut unlit = Chunk::empty(0, 0);
load_light(
&Tag::compound([("isLightOn", Tag::Byte(0))]),
&mut unlit,
true,
);
assert!(!unlit.lit && !unlit.light_loaded);
}
#[test]
fn level_round_trips_through_the_vanilla_files() {
let dir = std::env::temp_dir().join(format!("horizon-level-{}", std::process::id()));
let _ = fs::remove_dir_all(&dir);
+14 -1
View File
@@ -329,10 +329,23 @@ impl Streaming {
self.decorated.insert(key);
if self.in_range(key.0, key.1, 2) {
self.generated_total += 1;
// A chunk saved with its light needs no light job; its
// unlit neighbours still light themselves from it.
let loaded = chunk.light_loaded;
world.chunks.insert(key, Arc::new(chunk));
if loaded {
self.lit.insert(key);
for sy in 0..SECTIONS as i32 {
world.dirty_sections.insert((key.0, sy, key.1));
}
}
for dz in -1..=1 {
for dx in -1..=1 {
world.dirty_light.insert((key.0 + dx, key.1 + dz));
let n = (key.0 + dx, key.1 + dz);
if !loaded || world.chunks.get(&n).is_some_and(|c| !c.light_loaded)
{
world.dirty_light.insert(n);
}
}
}
}
+100
View File
@@ -0,0 +1,100 @@
#!/usr/bin/env python3
"""Compare Horizon's light engine with the vanilla 26.3 server.
Builds a scene of slabs, stairs, snow layers, trapdoors, lanterns, glass,
leaves, water and glowstone in a fresh vanilla world, lets the vanilla light
engine settle, stops the server so the SkyLight/BlockLight arrays are saved,
then relights every saved chunk with `examples/light_parity` and requires every
value to agree. The example also replays block edits through the incremental
`light::relight` and checks them against a full relight.
test_light_parity.py [--seed N] [--keep]
"""
import argparse
import shutil
import subprocess
import sys
from pathlib import Path
ROOT = Path(__file__).resolve().parent.parent
LOCAL = ROOT / '.local'
RESOURCES = LOCAL / 'horizon-client' / '26.3'
SERVER = LOCAL / 'minecraft' / '26.3' / 'jars' / 'server.jar'
# Absolute coordinates well above the terrain, inside force-loaded chunks.
SCENE = [
'forceload add 0 0 63 63',
'sleep 6',
# Closed room with a bottom-slab roof and a glowstone lamp.
'fill 2 170 2 20 175 20 stone hollow',
'fill 2 176 2 20 176 20 stone_slab[type=bottom]',
'setblock 10 171 10 glowstone',
# Stairs, top and bottom; snow layers; trapdoors; lanterns; water.
'fill 4 171 4 4 171 18 oak_stairs[facing=east,half=top]',
'fill 6 171 4 6 171 18 stone_brick_stairs[facing=north,half=bottom,shape=inner_left]',
'fill 12 171 4 18 171 6 snow[layers=3]',
'fill 12 172 8 18 172 8 oak_trapdoor[half=bottom,open=false]',
'fill 12 173 12 18 173 12 oak_trapdoor[half=top,open=true,facing=north]',
'setblock 14 171 14 lantern',
'setblock 15 175 15 soul_lantern[hanging=true]',
'fill 8 171 16 9 172 17 water',
# Open-air structures: sky light through slabs, stairs and leaves.
'fill 24 170 24 46 170 46 stone',
'fill 26 171 26 44 171 26 stone_slab[type=top]',
'fill 26 171 28 44 171 28 stone_slab[type=bottom]',
'fill 26 172 30 44 172 30 oak_leaves[persistent=true]',
'fill 26 171 32 30 171 40 glass',
'fill 32 171 32 36 171 40 oak_stairs[facing=south,half=bottom]',
'fill 38 171 32 38 175 40 stone_brick_wall',
'fill 40 171 32 44 171 40 dirt_path',
'fill 40 172 34 44 172 34 daylight_detector',
'fill 24 180 50 46 180 50 stone_slab[type=double]',
'fill 24 178 52 46 178 52 oak_stairs[facing=west,half=top]',
'setblock 30 171 44 torch',
'setblock 34 171 44 campfire',
'setblock 38 171 44 sea_lantern',
'setblock 42 171 44 lava',
'sleep 10',
# Edits after the first lighting pass, so vanilla updates incrementally.
'setblock 10 171 10 air',
'setblock 10 176 10 air',
'setblock 30 171 26 air',
'fill 26 172 30 30 172 30 air',
# Vanilla saves a chunk only when its blocks changed, so light that
# reached an untouched chunk later is stale on disk: mark every compared
# chunk (0..2, 0..3) as edited so it is written at shutdown.
*[f'setblock {x * 16 + 8} 169 {z * 16 + 8} stone' for x in range(3) for z in range(4)],
'sleep 8',
]
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('--seed', default='4242')
parser.add_argument('--keep', action='store_true')
args = parser.parse_args()
run = LOCAL / 'rust-light-parity'
shutil.rmtree(run, ignore_errors=True)
run.mkdir(parents=True)
command = [sys.executable, str(ROOT / 'tools' / 'vanilla_world_check.py'), '--jar', str(SERVER),
'--dir', str(run), '--world', 'world', '--seed', args.seed, '--wait-after', '8']
for step in SCENE:
command += ['--command', step]
if subprocess.run(command).returncode:
sys.exit('vanilla server reported problems')
dimension = run / 'world' / 'dimensions' / 'minecraft' / 'overworld'
if not (dimension / 'region').is_dir():
dimension = run / 'world'
for extra in (['--chunks', '0', '0', '2', '3'], ['--incremental', '100']):
status = subprocess.run(['cargo', 'run', '--quiet', '--profile', 'fast', '-p', 'horizon-client',
'--example', 'light_parity', '--', str(RESOURCES), str(dimension), *extra],
cwd=ROOT).returncode
if status:
sys.exit('light mismatch against vanilla')
print('light parity ok')
if not args.keep:
shutil.rmtree(run, ignore_errors=True)
if __name__ == '__main__':
main()