WIP: paletted containers and mesher changes

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Ludovic Marie
2026-10-10 06:01:10 +02:00
parent 6837f486a5
commit 2209950220
10 changed files with 82 additions and 58 deletions
+21 -5
View File
@@ -33,7 +33,11 @@ fn main() -> Result<()> {
let assets = Assets::load(&root)?;
let facts = BlockFacts::load(&root, assets.registry)?;
let biomes = Biomes::load(&root)?;
let tables = MeshTables::new(&assets, &facts, models::load_visibility(&root, assets.registry)?)?;
let tables = MeshTables::new(
&assets,
&facts,
models::load_visibility(&root, assets.registry)?,
)?;
let light = LightTables::load(&root, assets.registry, &facts)?;
let terrain = VanillaTerrain::new(&root.join("worldgen"), assets.registry, &biomes, seed)?;
let mut worker = terrain.worker();
@@ -51,7 +55,9 @@ fn main() -> Result<()> {
for x in 1..=inner {
let mut region: [Proto; 9] = std::array::from_fn(|k| {
let (dx, dz) = (k as i32 % 3 - 1, k as i32 / 3 - 1);
protos[((z + dz) * outer + x + dx) as usize].take().expect("proto")
protos[((z + dz) * outer + x + dx) as usize]
.take()
.expect("proto")
});
terrain.decorate(&mut worker, &mut region);
for (k, p) in region.into_iter().enumerate() {
@@ -73,7 +79,10 @@ fn main() -> Result<()> {
}
}
let finish = started.elapsed().as_secs_f64();
println!("finish (proto -> resident chunk): {:.1} ms per chunk", finish * 1e3 / chunks.len() as f64);
println!(
"finish (proto -> resident chunk): {:.1} ms per chunk",
finish * 1e3 / chunks.len() as f64
);
let chunks: Vec<Arc<Chunk>> = chunks.into_iter().map(Arc::new).collect();
let at = |x: i32, z: i32| chunks[((z - 1) * inner + x - 1) as usize].clone();
@@ -90,7 +99,10 @@ fn main() -> Result<()> {
lit.push(((x, z), light_chunk(&borrowed, &light, true)));
}
}
println!("light: {:.2} ms per column", started.elapsed().as_secs_f64() * 1e3 / lit.len() as f64);
println!(
"light: {:.2} ms per column",
started.elapsed().as_secs_f64() * 1e3 / lit.len() as f64
);
let mut chunks: Vec<Chunk> = chunks.iter().map(|c| (**c).clone()).collect();
for ((x, z), sections) in lit {
chunks[((z - 1) * inner + x - 1) as usize].sections = sections;
@@ -149,7 +161,11 @@ fn main() -> Result<()> {
}
best = best.min(started.elapsed().as_secs_f64() / f64::from(jobs));
}
println!("mesh: {:.1} us per section ({} quads total)", best * 1e6, quads);
println!(
"mesh: {:.1} us per section ({} quads total)",
best * 1e6,
quads
);
// Random lookups, as collision and entity queries do.
let mut state = 0x9E37_79B9_7F4A_7C15u64;
+9 -8
View File
@@ -1,8 +1,8 @@
//! Resident client chunk storage. Sections are reference-counted so worker
//! threads can mesh and light immutable snapshots while the game thread edits
//! (copy-on-write through `Arc::make_mut`).
use horizon_world::registry::BlockStateId;
use horizon_core::paletted::{PalettedContainer, Strategy};
use horizon_world::registry::BlockStateId;
use std::sync::Arc;
pub const MIN_Y: i32 = -64;
@@ -80,10 +80,10 @@ impl Section {
/// block uses any more.
pub fn compact(&mut self) {
for container in [&mut self.blocks, &mut self.biomes] {
if container.bits() != 0 {
if let Some(v) = container.uniform() {
*container = PalettedContainer::new(container.strategy(), v);
}
if container.bits() != 0
&& let Some(v) = container.uniform()
{
*container = PalettedContainer::new(container.strategy(), v);
}
}
if let Light::Full(a) = &self.light
@@ -183,8 +183,9 @@ impl Chunk {
/// Sets the biome of one quart cell (`qy` counts from the column's bottom).
pub fn set_biome_cell(&mut self, qx: i32, qy: i32, qz: i32, biome: u16) {
let section = Arc::make_mut(&mut self.sections[(qy >> 2) as usize]);
section
.biomes
.set(((qy & 3) << 4 | (qz & 3) << 2 | (qx & 3)) as usize, u32::from(biome));
section.biomes.set(
((qy & 3) << 4 | (qz & 3) << 2 | (qx & 3)) as usize,
u32::from(biome),
);
}
}
@@ -214,10 +214,9 @@ impl Game<'_> {
continue;
}
let qy = ((y - MIN_Y) >> 2).clamp(0, HEIGHT / 4 - 1);
let index = (qy * 16 + ((z & 15) >> 2) * 4 + ((x & 15) >> 2)) as usize;
if let Some(chunk) = self.world.chunks.get_mut(&key) {
let chunk = std::sync::Arc::make_mut(chunk);
std::sync::Arc::make_mut(&mut chunk.biomes)[index] = target as u16;
chunk.set_biome_cell((x & 15) >> 2, qy, (z & 15) >> 2, target as u16);
}
touched.insert(key);
changed += 1;
+16 -18
View File
@@ -336,30 +336,28 @@ impl Game<'_> {
}
}
fn sculk_scan_chunk(&mut self, key: (i32, i32)) {
use horizon_client::chunk::{Blocks, MIN_Y};
use horizon_client::chunk::MIN_Y;
let Some(chunk) = self.world.chunks.get(&key).cloned() else {
return;
};
let mut found = Vec::new();
for (s, section) in chunk.sections.iter().enumerate() {
let y0 = MIN_Y + s as i32 * 16;
match &section.blocks {
Blocks::Uniform(v) => {
// A uniform section of sculk blocks does not exist.
let _ = v;
}
Blocks::Full(a) => {
for (i, v) in a.iter().enumerate() {
if self.kind_of_state(*v) != 0 {
let (x, z, y) = (i & 15, (i >> 4) & 15, i >> 8);
found.push((
key.0 * 16 + x as i32,
y0 + y as i32,
key.1 * 16 + z as i32,
*v,
));
}
}
if section.blocks.uniform().is_some() {
// A uniform section of sculk blocks does not exist.
continue;
}
let mut a = [0u16; 4096];
section.unpack(&mut a);
for (i, v) in a.iter().enumerate() {
if self.kind_of_state(*v) != 0 {
let (x, z, y) = (i & 15, (i >> 4) & 15, i >> 8);
found.push((
key.0 * 16 + x as i32,
y0 + y as i32,
key.1 * 16 + z as i32,
*v,
));
}
}
}
+7 -8
View File
@@ -397,14 +397,13 @@ impl Padded {
let chunk = region[((z + 16) >> 4) as usize][((x + 16) >> 4) as usize];
let i = pi(x, y, z);
if let Some(c) = chunk {
blocks[i] = if (0..16).contains(&x)
&& (0..16).contains(&y)
&& (0..16).contains(&z)
{
own[(y << 8 | z << 4 | x) as usize]
} else {
c.get(x & 15, wy, z & 15)
};
blocks[i] =
if (0..16).contains(&x) && (0..16).contains(&y) && (0..16).contains(&z)
{
own[(y << 8 | z << 4 | x) as usize]
} else {
c.get(x & 15, wy, z & 15)
};
light[i] = c.light(x & 15, wy, z & 15);
} else {
light[i] = 0xF0;
+8 -3
View File
@@ -863,11 +863,14 @@ pub fn decode_with(
.collect();
let target = Arc::make_mut(&mut chunk.sections[si as usize]);
let data = match states.get("data") {
Some(Tag::LongArray(data)) => Some(data.iter().map(|&w| w as u64).collect::<Vec<_>>()),
Some(Tag::LongArray(data)) => {
Some(data.iter().map(|&w| w as u64).collect::<Vec<_>>())
}
_ => None,
};
let palette: Vec<u32> = ids.iter().map(|&id| u32::from(id)).collect();
if let Ok(blocks) = PalettedContainer::from_packed(BLOCK_STRATEGY, palette, data.as_deref())
if let Ok(blocks) =
PalettedContainer::from_packed(BLOCK_STRATEGY, palette, data.as_deref())
{
target.blocks = blocks;
}
@@ -883,7 +886,9 @@ pub fn decode_with(
})
.collect();
let data = match b.get("data") {
Some(Tag::LongArray(data)) => Some(data.iter().map(|&w| w as u64).collect::<Vec<_>>()),
Some(Tag::LongArray(data)) => {
Some(data.iter().map(|&w| w as u64).collect::<Vec<_>>())
}
_ => None,
};
let palette: Vec<u32> = ids.iter().map(|&id| u32::from(id)).collect();
+4 -5
View File
@@ -77,11 +77,10 @@ impl TestTerrain {
fn generate(&self, _: &mut (), cx: i32, cz: i32) -> Chunk {
let mut chunk = Chunk::empty(cx, cz);
for s in &mut chunk.sections {
std::sync::Arc::make_mut(s).biomes =
horizon_core::paletted::PalettedContainer::new(
crate::chunk::BIOME_STRATEGY,
u32::from(self.plains),
);
std::sync::Arc::make_mut(s).biomes = horizon_core::paletted::PalettedContainer::new(
crate::chunk::BIOME_STRATEGY,
u32::from(self.plains),
);
}
for z in 0..16 {
for x in 0..16 {
+2 -1
View File
@@ -195,7 +195,8 @@ impl ChunkGenerator for VanillaTerrain {
}
}
if any {
Arc::make_mut(&mut out.sections[si as usize]).blocks = Section::from_blocks(&values);
Arc::make_mut(&mut out.sections[si as usize]).blocks =
Section::from_blocks(&values);
}
}
let base_section = chunk.terrain.min_y() >> 4;
@@ -106,13 +106,14 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let reread = read(strategy, &write(&c));
let packed = c.pack();
let storage = packed.data.as_ref().map_or(BASIS, |data| {
data.iter()
.flat_map(|w| w.to_le_bytes())
.fold(BASIS, |h, b| fnv(h, b))
data.iter().flat_map(|w| w.to_le_bytes()).fold(BASIS, fnv)
});
let palette: Vec<String> = packed.palette.iter().map(u32::to_string).collect();
let loaded =
PalettedContainer::from_packed(strategy, packed.palette.clone(), packed.data.as_deref())?;
let loaded = PalettedContainer::from_packed(
strategy,
packed.palette.clone(),
packed.data.as_deref(),
)?;
writeln!(
out,
"{} {} cp={} bits={} final={} values={} read={}/{} packed={}/{}/{} loaded={}/{}",
+8 -3
View File
@@ -693,15 +693,20 @@ mod tests {
assert_eq!(c.get(i), v);
}
let packed = c.pack();
let loaded = PalettedContainer::from_packed(strategy, packed.palette.clone(), packed.data.as_deref())
.unwrap();
let loaded = PalettedContainer::from_packed(
strategy,
packed.palette.clone(),
packed.data.as_deref(),
)
.unwrap();
assert_eq!(loaded, c);
assert_eq!(loaded.pack(), packed);
let again = PalettedContainer::from_values(strategy, &plain);
assert_eq!(again, c);
assert_eq!(again.pack(), packed);
let (bits, palette, words) = c.to_wire();
let wire = PalettedContainer::from_wire(strategy, bits, palette, words.to_vec()).unwrap();
let wire =
PalettedContainer::from_wire(strategy, bits, palette, words.to_vec()).unwrap();
assert_eq!(wire, c);
}
}