Merge worktree-agent-aa7f8206a004fc51a
This commit is contained in:
@@ -0,0 +1,212 @@
|
||||
//! Resident chunk storage benchmark: generates a block of vanilla terrain, then
|
||||
//! reports the resident block/biome memory, the cost of lookups, meshing and
|
||||
//! lighting on it.
|
||||
//!
|
||||
//! chunk_bench <client resource dir> [--seed S] [--size N]
|
||||
#![forbid(unsafe_code)]
|
||||
use horizon_client::{
|
||||
assets::Assets,
|
||||
biome::Biomes,
|
||||
blocks::BlockFacts,
|
||||
chunk::{Chunk, MIN_Y, SECTIONS},
|
||||
light::{LightTables, light_chunk},
|
||||
mesher::{MeshTables, Mesher},
|
||||
models,
|
||||
streaming::{ChunkGenerator, Proto},
|
||||
vanillagen::VanillaTerrain,
|
||||
};
|
||||
use horizon_core::biome::BiomeZoom;
|
||||
use std::{path::PathBuf, sync::Arc, time::Instant};
|
||||
type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;
|
||||
|
||||
fn main() -> Result<()> {
|
||||
let args: Vec<String> = std::env::args().skip(1).collect();
|
||||
let root = PathBuf::from(args.first().ok_or("usage: chunk_bench RESOURCES")?);
|
||||
let flag = |name: &str, default: i64| -> i64 {
|
||||
args.iter()
|
||||
.position(|a| a == name)
|
||||
.and_then(|i| args.get(i + 1))
|
||||
.and_then(|v| v.parse().ok())
|
||||
.unwrap_or(default)
|
||||
};
|
||||
let (seed, size) = (flag("--seed", 12345), flag("--size", 8) as i32);
|
||||
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 light = LightTables::load(&root, assets.registry, &facts)?;
|
||||
let terrain = VanillaTerrain::new(&root.join("worldgen"), assets.registry, &biomes, seed)?;
|
||||
let mut worker = terrain.worker();
|
||||
|
||||
// Protos for size+4 columns, decorated for the inner size+2, finished as size+2.
|
||||
let started = Instant::now();
|
||||
let outer = size + 4;
|
||||
let mut protos: Vec<Option<Proto>> = (0..outer * outer)
|
||||
.map(|i| Some(terrain.proto(&mut worker, i % outer, i / outer)))
|
||||
.collect();
|
||||
eprintln!("proto: {:.2}s", started.elapsed().as_secs_f64());
|
||||
let started = Instant::now();
|
||||
let inner = size + 2;
|
||||
for z in 1..=inner {
|
||||
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")
|
||||
});
|
||||
terrain.decorate(&mut worker, &mut region);
|
||||
for (k, p) in region.into_iter().enumerate() {
|
||||
let (dx, dz) = (k as i32 % 3 - 1, k as i32 / 3 - 1);
|
||||
protos[((z + dz) * outer + x + dx) as usize] = Some(p);
|
||||
}
|
||||
}
|
||||
}
|
||||
eprintln!("decorate: {:.2}s", started.elapsed().as_secs_f64());
|
||||
let started = Instant::now();
|
||||
let mut chunks: Vec<Chunk> = Vec::new();
|
||||
for z in 1..=inner {
|
||||
for x in 1..=inner {
|
||||
let proto = protos[(z * outer + x) as usize].take().expect("proto");
|
||||
let mut chunk = terrain.finish(proto);
|
||||
chunk.x = x;
|
||||
chunk.z = z;
|
||||
chunks.push(chunk);
|
||||
}
|
||||
}
|
||||
let finish = started.elapsed().as_secs_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();
|
||||
|
||||
// Light all inner-inner columns.
|
||||
let started = Instant::now();
|
||||
let mut lit = Vec::new();
|
||||
for z in 2..inner {
|
||||
for x in 2..inner {
|
||||
let region: [[Option<Arc<Chunk>>; 3]; 3] = std::array::from_fn(|dz| {
|
||||
std::array::from_fn(|dx| Some(at(x + dx as i32 - 1, z + dz as i32 - 1)))
|
||||
});
|
||||
let borrowed = horizon_client::mesher::borrow(®ion);
|
||||
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
|
||||
);
|
||||
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;
|
||||
}
|
||||
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();
|
||||
|
||||
// Memory.
|
||||
let (mut bytes, mut sections, mut uniform) = (0usize, 0usize, 0usize);
|
||||
for chunk in &chunks {
|
||||
bytes += chunk.heap_bytes();
|
||||
for s in &chunk.sections {
|
||||
sections += 1;
|
||||
uniform += usize::from(s.is_uniform(s.get(0)));
|
||||
}
|
||||
}
|
||||
println!(
|
||||
"resident: {} chunks, {} sections ({} uniform), {:.1} KiB per chunk ({:.2} MiB total)",
|
||||
chunks.len(),
|
||||
sections,
|
||||
uniform,
|
||||
bytes as f64 / 1024.0 / chunks.len() as f64,
|
||||
bytes as f64 / 1048576.0
|
||||
);
|
||||
|
||||
// Meshing.
|
||||
let mut mesher = Mesher {
|
||||
nether_shading: false,
|
||||
assets: &assets,
|
||||
tables: &tables,
|
||||
biomes: &biomes,
|
||||
visibility: tables.visibility.clone(),
|
||||
zoom: BiomeZoom::new(7),
|
||||
blend_radius: 2,
|
||||
smooth_lighting: true,
|
||||
cutout_leaves: true,
|
||||
};
|
||||
let mut best = f64::MAX;
|
||||
let mut quads = 0usize;
|
||||
for _ in 0..3 {
|
||||
let started = Instant::now();
|
||||
let mut jobs = 0;
|
||||
quads = 0;
|
||||
for z in 2..inner {
|
||||
for x in 2..inner {
|
||||
let region: [[Option<Arc<Chunk>>; 3]; 3] = std::array::from_fn(|dz| {
|
||||
std::array::from_fn(|dx| Some(at(x + dx as i32 - 1, z + dz as i32 - 1)))
|
||||
});
|
||||
let borrowed = horizon_client::mesher::borrow(®ion);
|
||||
for sy in 0..SECTIONS as i32 {
|
||||
let mesh = mesher.mesh(&borrowed, sy);
|
||||
quads += (mesh.solid.len() + mesh.translucent.len()) / 4;
|
||||
jobs += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
best = best.min(started.elapsed().as_secs_f64() / f64::from(jobs));
|
||||
}
|
||||
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;
|
||||
let started = Instant::now();
|
||||
let mut sum = 0u64;
|
||||
let lookups = 20_000_000u64;
|
||||
for _ in 0..lookups {
|
||||
state ^= state << 13;
|
||||
state ^= state >> 7;
|
||||
state ^= state << 17;
|
||||
let cx = 2 + (state % u64::from((inner - 2) as u32)) as i32;
|
||||
let cz = 2 + ((state >> 8) % u64::from((inner - 2) as u32)) as i32;
|
||||
let chunk = &chunks[((cz - 1) * inner + cx - 1) as usize];
|
||||
let (x, y, z) = (
|
||||
(state >> 16) as i32 & 15,
|
||||
MIN_Y + ((state >> 24) % 384) as i32,
|
||||
(state >> 40) as i32 & 15,
|
||||
);
|
||||
sum += u64::from(chunk.get(x, y, z));
|
||||
}
|
||||
println!(
|
||||
"random get: {:.2} ns per lookup (checksum {sum})",
|
||||
started.elapsed().as_secs_f64() * 1e9 / lookups as f64
|
||||
);
|
||||
// Coherent scan, as a column or area query does.
|
||||
let started = Instant::now();
|
||||
let mut sum = 0u64;
|
||||
for _ in 0..4 {
|
||||
for chunk in &chunks {
|
||||
for y in MIN_Y..MIN_Y + 384 {
|
||||
for z in 0..16 {
|
||||
for x in 0..16 {
|
||||
sum += u64::from(chunk.get(x, y, z));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
println!(
|
||||
"scan get: {:.2} ns per lookup (checksum {sum})",
|
||||
started.elapsed().as_secs_f64() * 1e9 / (4.0 * chunks.len() as f64 * 98304.0)
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
@@ -4,12 +4,13 @@
|
||||
#![forbid(unsafe_code)]
|
||||
use horizon_client::{
|
||||
assets::Assets,
|
||||
biome::{Biomes, GrassModifier},
|
||||
biome::{BiomeInfo, Biomes, GrassModifier},
|
||||
blocks::BlockFacts,
|
||||
chunk::{Chunk, Light, MIN_Y, index},
|
||||
mesher::{MeshTables, Mesher, Vertex},
|
||||
mesher::{MeshTables, Mesher, OPAQUE_LAYER, Vertex},
|
||||
models,
|
||||
};
|
||||
use horizon_core::biome::BiomeZoom;
|
||||
use horizon_world::registry::BlockStateId;
|
||||
use serde_json::{Value, json};
|
||||
use std::{
|
||||
@@ -48,17 +49,23 @@ fn scene(
|
||||
assets: &Assets,
|
||||
tables: &MeshTables,
|
||||
biomes: &mut Biomes,
|
||||
pristine: &[BiomeInfo],
|
||||
names: &BTreeMap<u32, &str>,
|
||||
elapsed: &mut Duration,
|
||||
) -> Result<Value> {
|
||||
let colors = &row["colors"];
|
||||
let color = |k: &str| -> Result<u32> { Ok(int(&colors[k])? as u32 & 0xFF_FFFF) };
|
||||
for biome in &mut biomes.list {
|
||||
biome.grass = color("grass")?;
|
||||
biome.modifier = GrassModifier::None;
|
||||
biome.foliage = color("foliage")?;
|
||||
biome.dry_foliage = color("dry_foliage")?;
|
||||
biome.water = color("water")?;
|
||||
// Scenes without a biome field give every biome the same constant colours;
|
||||
// the others use the real biome definitions.
|
||||
biomes.list = pristine.to_vec();
|
||||
if row.get("biomes").is_none() {
|
||||
for biome in &mut biomes.list {
|
||||
biome.grass = color("grass")?;
|
||||
biome.modifier = GrassModifier::None;
|
||||
biome.foliage = color("foliage")?;
|
||||
biome.dry_foliage = color("dry_foliage")?;
|
||||
biome.water = color("water")?;
|
||||
}
|
||||
}
|
||||
let default = row["light"].as_array().ok_or("missing default light")?;
|
||||
let packed = |sky: i32, block: i32| (sky << 4 | block) as u8;
|
||||
@@ -89,6 +96,31 @@ fn scene(
|
||||
values[index(x, y, z)] = packed(int(&cell[3])?, int(&cell[4])?);
|
||||
}
|
||||
}
|
||||
let (mut zoom, mut blend_radius) = (BiomeZoom::new(0), 2);
|
||||
let smooth_lighting = row["smooth"].as_bool().unwrap_or(true);
|
||||
let cutout_leaves = row["cutout_leaves"].as_bool().unwrap_or(true);
|
||||
if let Some(field) = row.get("biomes") {
|
||||
zoom = BiomeZoom::new(field["seed"].as_i64().ok_or("missing biome seed")?);
|
||||
blend_radius = int(&field["radius"])?;
|
||||
let default = biomes.id(field["default"].as_str().ok_or("missing default biome")?);
|
||||
for chunk in &mut chunks {
|
||||
for qy in 0..96 {
|
||||
for cell in 0..16 {
|
||||
chunk.set_biome_cell(cell & 3, qy, cell >> 2, default);
|
||||
}
|
||||
}
|
||||
}
|
||||
for cell in field["cells"].as_array().ok_or("missing biome cells")? {
|
||||
let (qx, qy, qz) = (int(&cell[0])?, int(&cell[1])?, int(&cell[2])?);
|
||||
let i = chunk_index(qx * 4, qz * 4)?;
|
||||
chunks[i].set_biome_cell(
|
||||
qx,
|
||||
qy - MIN_Y / 4,
|
||||
qz,
|
||||
biomes.id(cell[3].as_str().ok_or("biome name")?),
|
||||
);
|
||||
}
|
||||
}
|
||||
let region: [[Option<&Chunk>; 3]; 3] =
|
||||
std::array::from_fn(|z| std::array::from_fn(|x| Some(&chunks[z * 3 + x])));
|
||||
let mut mesher = Mesher {
|
||||
@@ -97,6 +129,10 @@ fn scene(
|
||||
tables,
|
||||
biomes: &*biomes,
|
||||
visibility: tables.visibility.clone(),
|
||||
zoom,
|
||||
blend_radius,
|
||||
smooth_lighting,
|
||||
cutout_leaves,
|
||||
};
|
||||
let section_y = int(&row["section_y"])?;
|
||||
let started = Instant::now();
|
||||
@@ -109,7 +145,9 @@ fn scene(
|
||||
return Err("partial quad".into());
|
||||
}
|
||||
for quad in vertices {
|
||||
let sprite = names.get(&quad[0].layer).ok_or("unknown sprite layer")?;
|
||||
let sprite = names
|
||||
.get(&(quad[0].layer & !OPAQUE_LAYER))
|
||||
.ok_or("unknown sprite layer")?;
|
||||
if quad.iter().any(|v: &Vertex| v.layer != quad[0].layer) {
|
||||
return Err("quad mixes sprite layers".into());
|
||||
}
|
||||
@@ -150,6 +188,7 @@ fn main() -> Result<()> {
|
||||
.iter()
|
||||
.map(|(name, layer)| (*layer, name.as_str()))
|
||||
.collect();
|
||||
let pristine = biomes.list.clone();
|
||||
let mut output = BufWriter::new(fs::File::create(&args[2])?);
|
||||
if args[1] == "facts" {
|
||||
// The per-state facts the mesher derives, in the oracle's order and positions.
|
||||
@@ -175,7 +214,15 @@ fn main() -> Result<()> {
|
||||
let (mut elapsed, mut sections) = (Duration::ZERO, 0u32);
|
||||
for line in BufReader::new(fs::File::open(&args[1])?).lines() {
|
||||
let row: Value = serde_json::from_str(&line?)?;
|
||||
let result = scene(&row, &assets, &tables, &mut biomes, &names, &mut elapsed)?;
|
||||
let result = scene(
|
||||
&row,
|
||||
&assets,
|
||||
&tables,
|
||||
&mut biomes,
|
||||
&pristine,
|
||||
&names,
|
||||
&mut elapsed,
|
||||
)?;
|
||||
serde_json::to_writer(&mut output, &result)?;
|
||||
output.write_all(b"\n")?;
|
||||
sections += 1;
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
//! 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_core::paletted::{PalettedContainer, Strategy};
|
||||
use horizon_world::registry::BlockStateId;
|
||||
use std::sync::Arc;
|
||||
|
||||
@@ -8,11 +9,11 @@ pub const MIN_Y: i32 = -64;
|
||||
pub const HEIGHT: i32 = 384;
|
||||
pub const SECTIONS: usize = (HEIGHT / 16) as usize;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub enum Blocks {
|
||||
Uniform(u16),
|
||||
Full(Box<[u16; 4096]>),
|
||||
}
|
||||
/// Block states are `PalettedContainer`s over the pinned registry (35,723
|
||||
/// states: a 16-bit global palette); biomes are 4x4x4 containers over the
|
||||
/// biome table (67 entries: a 7-bit global palette).
|
||||
pub const BLOCK_STRATEGY: Strategy = Strategy::blocks(16);
|
||||
pub const BIOME_STRATEGY: Strategy = Strategy::biomes(7);
|
||||
/// Packed light per block: sky light in the high nibble, block light in the low one.
|
||||
#[derive(Clone)]
|
||||
pub enum Light {
|
||||
@@ -25,13 +26,16 @@ pub fn index(x: i32, y: i32, z: i32) -> usize {
|
||||
}
|
||||
#[derive(Clone)]
|
||||
pub struct Section {
|
||||
pub blocks: Blocks,
|
||||
pub blocks: PalettedContainer,
|
||||
/// Biome table index per 4x4x4 cell, `(y << 2 | z) << 2 | x`.
|
||||
pub biomes: PalettedContainer,
|
||||
pub light: Light,
|
||||
}
|
||||
impl Default for Section {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
blocks: Blocks::Uniform(0),
|
||||
blocks: PalettedContainer::new(BLOCK_STRATEGY, 0),
|
||||
biomes: PalettedContainer::new(BIOME_STRATEGY, 0),
|
||||
light: Light::Uniform(0xF0),
|
||||
}
|
||||
}
|
||||
@@ -39,23 +43,19 @@ impl Default for Section {
|
||||
impl Section {
|
||||
#[inline]
|
||||
pub fn get(&self, i: usize) -> u16 {
|
||||
match &self.blocks {
|
||||
Blocks::Uniform(v) => *v,
|
||||
Blocks::Full(a) => a[i],
|
||||
}
|
||||
self.blocks.get(i) as u16
|
||||
}
|
||||
pub fn set(&mut self, i: usize, v: u16) -> u16 {
|
||||
match &mut self.blocks {
|
||||
Blocks::Uniform(u) if *u == v => v,
|
||||
Blocks::Uniform(u) => {
|
||||
let old = *u;
|
||||
let mut a = Box::new([old; 4096]);
|
||||
a[i] = v;
|
||||
self.blocks = Blocks::Full(a);
|
||||
old
|
||||
}
|
||||
Blocks::Full(a) => std::mem::replace(&mut a[i], v),
|
||||
}
|
||||
self.blocks.set(i, u32::from(v)) as u16
|
||||
}
|
||||
/// All 4,096 block states in index order.
|
||||
pub fn unpack(&self, out: &mut [u16; 4096]) {
|
||||
self.blocks.unpack_u16(out);
|
||||
}
|
||||
/// The block states of a whole section at once.
|
||||
pub fn from_blocks(values: &[u16; 4096]) -> PalettedContainer {
|
||||
let wide: Vec<u32> = values.iter().map(|&v| u32::from(v)).collect();
|
||||
PalettedContainer::from_values(BLOCK_STRATEGY, &wide)
|
||||
}
|
||||
#[inline]
|
||||
pub fn light(&self, i: usize) -> u8 {
|
||||
@@ -65,17 +65,26 @@ impl Section {
|
||||
}
|
||||
}
|
||||
pub fn is_uniform(&self, v: u16) -> bool {
|
||||
match &self.blocks {
|
||||
Blocks::Uniform(u) => *u == v,
|
||||
Blocks::Full(a) => a.iter().all(|&b| b == v),
|
||||
}
|
||||
self.blocks.is_uniform(u32::from(v))
|
||||
}
|
||||
/// Collapse storage that became uniform.
|
||||
/// Heap bytes held by the block, biome and light storage (benchmark accounting).
|
||||
pub fn heap_bytes(&self) -> usize {
|
||||
self.blocks.heap_bytes()
|
||||
+ self.biomes.heap_bytes()
|
||||
+ match &self.light {
|
||||
Light::Uniform(_) => 0,
|
||||
Light::Full(_) => 4096,
|
||||
}
|
||||
}
|
||||
/// Collapse storage that became uniform, and drop palette entries no
|
||||
/// block uses any more.
|
||||
pub fn compact(&mut self) {
|
||||
if let Blocks::Full(a) = &self.blocks
|
||||
&& a.iter().all(|&b| b == a[0])
|
||||
{
|
||||
self.blocks = Blocks::Uniform(a[0]);
|
||||
for container in [&mut self.blocks, &mut self.biomes] {
|
||||
if container.bits() != 0
|
||||
&& let Some(v) = container.uniform()
|
||||
{
|
||||
*container = PalettedContainer::new(container.strategy(), v);
|
||||
}
|
||||
}
|
||||
if let Light::Full(a) = &self.light
|
||||
&& a.iter().all(|&b| b == a[0])
|
||||
@@ -89,8 +98,6 @@ pub struct Chunk {
|
||||
pub x: i32,
|
||||
pub z: i32,
|
||||
pub sections: Vec<Arc<Section>>,
|
||||
/// Biome index per 4x4x4 cell (x,z in 0..4, y in 0..96) into `Biomes`.
|
||||
pub biomes: Arc<Vec<u16>>,
|
||||
/// Highest non-air, light-blocking-or-fluid Y per column (MOTION_BLOCKING-like), for rain/sky.
|
||||
pub heights: Arc<[i16; 256]>,
|
||||
pub lit: bool,
|
||||
@@ -119,7 +126,6 @@ impl Chunk {
|
||||
sections: (0..SECTIONS)
|
||||
.map(|_| Arc::new(Section::default()))
|
||||
.collect(),
|
||||
biomes: Arc::new(vec![0; 16 * (HEIGHT as usize / 4)]),
|
||||
heights: Arc::new([MIN_Y as i16; 256]),
|
||||
lit: false,
|
||||
light_loaded: false,
|
||||
@@ -161,9 +167,25 @@ impl Chunk {
|
||||
}
|
||||
self.sections[s as usize].light(index(x, y, z))
|
||||
}
|
||||
/// Heap bytes held by the sections (benchmark accounting).
|
||||
pub fn heap_bytes(&self) -> usize {
|
||||
self.sections.iter().map(|s| s.heap_bytes()).sum::<usize>()
|
||||
}
|
||||
/// Biome table index at a block position (`ChunkAccess.getNoiseBiome` with
|
||||
/// the quart y clamped to the column).
|
||||
#[inline]
|
||||
pub fn biome(&self, x: i32, y: i32, z: i32) -> u16 {
|
||||
let qy = ((y - MIN_Y) >> 2).clamp(0, HEIGHT / 4 - 1);
|
||||
self.biomes[(qy * 16 + ((z & 15) >> 2) * 4 + ((x & 15) >> 2)) as usize]
|
||||
self.sections[(qy >> 2) as usize]
|
||||
.biomes
|
||||
.get(((qy & 3) << 4 | ((z & 15) >> 2) << 2 | (x & 15) >> 2) as usize) as u16
|
||||
}
|
||||
/// 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),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -120,7 +120,11 @@ use horizon_client::{
|
||||
vanillagen::VanillaTerrain,
|
||||
world::World,
|
||||
};
|
||||
use horizon_core::{geometry::Direction, position::BlockPos};
|
||||
use horizon_core::{
|
||||
biome::{BiomeZoom, obfuscate_seed},
|
||||
geometry::Direction,
|
||||
position::BlockPos,
|
||||
};
|
||||
use horizon_core::{geometry::Vec3, random::RandomSource};
|
||||
use horizon_world::{
|
||||
block_collision::BlockCollisionRegistry,
|
||||
@@ -4935,6 +4939,10 @@ impl Game<'_> {
|
||||
)
|
||||
};
|
||||
streaming.radius = self.settings.render_distance;
|
||||
streaming.biome_blend = self.settings.biome_blend;
|
||||
streaming.smooth_lighting = self.settings.smooth_lighting;
|
||||
streaming.cutout_leaves = self.settings.cutout_leaves;
|
||||
streaming.biome_zoom = BiomeZoom::new(obfuscate_seed(seed));
|
||||
streaming.sky_light = dim != Dim::Nether;
|
||||
streaming.nether_shading = dim == Dim::Nether;
|
||||
Ok((streaming, save))
|
||||
@@ -6194,6 +6202,9 @@ impl Game<'_> {
|
||||
}
|
||||
fn apply_settings(&mut self) {
|
||||
self.streaming.radius = self.settings.render_distance;
|
||||
self.streaming.biome_blend = self.settings.biome_blend;
|
||||
self.streaming.smooth_lighting = self.settings.smooth_lighting;
|
||||
self.streaming.cutout_leaves = self.settings.cutout_leaves;
|
||||
self.audio.master = self.settings.master_volume;
|
||||
horizon_client::hud::GUI_SCALE.store(
|
||||
self.settings.gui_scale,
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -933,9 +933,14 @@ impl Game<'_> {
|
||||
};
|
||||
let mut found = Vec::new();
|
||||
for (s, section) in chunk.sections.iter().enumerate() {
|
||||
let horizon_client::chunk::Blocks::Full(blocks) = §ion.blocks else {
|
||||
if !section
|
||||
.blocks
|
||||
.maybe_has(|v| v == u32::from(portal) || v == u32::from(gateway))
|
||||
{
|
||||
continue;
|
||||
};
|
||||
}
|
||||
let mut blocks = Box::new([0u16; 4096]);
|
||||
section.unpack(&mut blocks);
|
||||
for (i, &b) in blocks.iter().enumerate() {
|
||||
if b == portal || b == gateway {
|
||||
let y = horizon_client::chunk::MIN_Y + s as i32 * 16 + (i >> 8) as i32;
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
use super::{Dim, Game, Mining, Screen};
|
||||
use horizon_client::{
|
||||
Result,
|
||||
chunk::{Blocks, Chunk, Light, MIN_Y, SECTIONS, Section},
|
||||
chunk::{BIOME_STRATEGY, BLOCK_STRATEGY, Chunk, Light, MIN_Y, SECTIONS, Section},
|
||||
containers::{BlockEntity, Furnace, FurnaceKind},
|
||||
entities::{Experience, ItemEntity},
|
||||
hud::{HudBatch, rgba},
|
||||
@@ -22,7 +22,7 @@ use horizon_client::{
|
||||
streaming::{ChunkGenerator, Proto, Streaming},
|
||||
world::World,
|
||||
};
|
||||
use horizon_core::{geometry::Vec3, random::RandomSource};
|
||||
use horizon_core::{geometry::Vec3, paletted::PalettedContainer, random::RandomSource};
|
||||
use horizon_protocol::{
|
||||
entity_data::{ITEM_ENTITY_ITEM, Value},
|
||||
item::{Hashed, Stack},
|
||||
@@ -420,6 +420,8 @@ impl Game<'_> {
|
||||
.is_none_or(|r| r.dimension.has_skylight);
|
||||
self.streaming.sky_light = has_sky;
|
||||
self.streaming.nether_shading = self.dim == Dim::Nether;
|
||||
// The seed in SpawnInfo is already the hashed one the client's BiomeManager holds.
|
||||
self.streaming.biome_zoom = horizon_core::biome::BiomeZoom::new(spawn.seed);
|
||||
self.player = None;
|
||||
self.apply_settings();
|
||||
}
|
||||
@@ -432,54 +434,55 @@ impl Game<'_> {
|
||||
.sections(r.dimension.sections(), 16, r.biome_bits)
|
||||
.map_err(|e| e.to_string())?;
|
||||
let mut chunk = Chunk::empty(p.x, p.z);
|
||||
let mut biomes = vec![0u16; chunk.biomes.len()];
|
||||
let offset = (r.dimension.min_y - MIN_Y).div_euclid(16);
|
||||
// Biome table index per quart cell of the dimension's own sections.
|
||||
let mut biome_cells: Vec<[u32; 64]> = Vec::with_capacity(sections.len());
|
||||
for (i, s) in sections.iter().enumerate() {
|
||||
let target = offset + i as i32;
|
||||
let mut cells = [0u32; 64];
|
||||
for (j, cell) in cells.iter_mut().enumerate() {
|
||||
*cell = u32::from(
|
||||
r.biome_map
|
||||
.get(s.biomes.get(j) as usize)
|
||||
.copied()
|
||||
.unwrap_or(0),
|
||||
);
|
||||
}
|
||||
biome_cells.push(cells);
|
||||
if !(0..SECTIONS as i32).contains(&target) {
|
||||
continue;
|
||||
}
|
||||
let blocks = if s.states.bits == 0 {
|
||||
Blocks::Uniform(s.states.get(0) as u16)
|
||||
} else {
|
||||
let mut a = Box::new([0u16; 4096]);
|
||||
for (j, v) in a.iter_mut().enumerate() {
|
||||
*v = s.states.get(j) as u16;
|
||||
}
|
||||
Blocks::Full(a)
|
||||
};
|
||||
let strategy = BLOCK_STRATEGY;
|
||||
let palette = s.states.palette.iter().map(|&p| p as u32).collect();
|
||||
let words = s.states.data.iter().map(|&w| w as u64).collect();
|
||||
let blocks = PalettedContainer::from_wire(strategy, s.states.bits, palette, words)
|
||||
.map_err(|e| e.to_string())?;
|
||||
let mut section = Section {
|
||||
blocks,
|
||||
biomes: PalettedContainer::from_values(BIOME_STRATEGY, &cells),
|
||||
light: Light::Uniform(0xF0),
|
||||
};
|
||||
section.compact();
|
||||
chunk.sections[target as usize] = Arc::new(section);
|
||||
for j in 0..64 {
|
||||
let biome = r
|
||||
.biome_map
|
||||
.get(s.biomes.get(j) as usize)
|
||||
.copied()
|
||||
.unwrap_or(0);
|
||||
if let Some(cell) = biomes.get_mut(target as usize * 64 + j) {
|
||||
*cell = biome;
|
||||
}
|
||||
// Sections outside the dimension's range take its nearest biomes
|
||||
// (the column's top or bottom cell layer repeated).
|
||||
if !biome_cells.is_empty() {
|
||||
let last = biome_cells.len() - 1;
|
||||
for si in 0..SECTIONS as i32 {
|
||||
let i = si - offset;
|
||||
if (0..sections.len() as i32).contains(&i) {
|
||||
continue;
|
||||
}
|
||||
let (edge, layer) = if i < 0 { (0, 0) } else { (last, 3) };
|
||||
let mut cells = [0u32; 64];
|
||||
for (j, cell) in cells.iter_mut().enumerate() {
|
||||
*cell = biome_cells[edge][layer * 16 + j % 16];
|
||||
}
|
||||
Arc::make_mut(&mut chunk.sections[si as usize]).biomes =
|
||||
PalettedContainer::from_values(BIOME_STRATEGY, &cells);
|
||||
}
|
||||
}
|
||||
// Columns outside the dimension's range take its nearest biome.
|
||||
let (lo, hi) = (
|
||||
offset.max(0) as usize * 64,
|
||||
((offset + sections.len() as i32).min(SECTIONS as i32)) as usize * 64,
|
||||
);
|
||||
if lo < hi {
|
||||
for j in 0..biomes.len() {
|
||||
if j < lo {
|
||||
biomes[j] = biomes[lo + j % 16];
|
||||
} else if j >= hi {
|
||||
biomes[j] = biomes[hi - 16 + j % 16];
|
||||
}
|
||||
}
|
||||
}
|
||||
chunk.biomes = Arc::new(biomes);
|
||||
Ok(chunk)
|
||||
}
|
||||
/// Keep the renderable block entities (chests, furnaces) in step with blocks.
|
||||
|
||||
@@ -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 §ion.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,
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
//! (sections with their paletted containers, block entities and the sky and
|
||||
//! block DataLayers) and the section block-change batches.
|
||||
use crate::{
|
||||
chunk::{Blocks, Chunk, Light, MIN_Y, SECTIONS, Section},
|
||||
chunk::{Chunk, Light, MIN_Y, SECTIONS, Section},
|
||||
containers::BlockEntity,
|
||||
};
|
||||
use horizon_protocol::{
|
||||
@@ -154,12 +154,7 @@ impl ChunkEncoder {
|
||||
.is_none_or(|f| f & NON_AIR == 0)
|
||||
}
|
||||
|
||||
fn section(
|
||||
&self,
|
||||
section: &Section,
|
||||
chunk: &Chunk,
|
||||
index: usize,
|
||||
) -> (horizon_protocol::chunk::Section, bool) {
|
||||
fn section(&self, section: &Section) -> (horizon_protocol::chunk::Section, bool) {
|
||||
let mut values = Vec::with_capacity(BLOCKS);
|
||||
let (mut non_empty, mut fluids, mut entities) = (0i16, 0i16, false);
|
||||
let mut count = |state: u16, n: i16| {
|
||||
@@ -172,21 +167,15 @@ impl ChunkEncoder {
|
||||
}
|
||||
f & ENTITY != 0
|
||||
};
|
||||
match §ion.blocks {
|
||||
Blocks::Uniform(v) => {
|
||||
entities |= count(*v, BLOCKS as i16);
|
||||
values.resize(BLOCKS, i32::from(*v));
|
||||
}
|
||||
Blocks::Full(a) => {
|
||||
for &v in a.iter() {
|
||||
entities |= count(v, 1);
|
||||
values.push(i32::from(v));
|
||||
}
|
||||
}
|
||||
let mut unpacked = [0u16; BLOCKS];
|
||||
section.unpack(&mut unpacked);
|
||||
for &v in &unpacked {
|
||||
entities |= count(v, 1);
|
||||
values.push(i32::from(v));
|
||||
}
|
||||
let biome_values: Vec<i32> = (0..BIOMES)
|
||||
.map(|j| {
|
||||
let internal = chunk.biomes.get(index * BIOMES + j).copied().unwrap_or(0);
|
||||
let internal = section.biomes.get(j) as u16;
|
||||
self.biome_ids
|
||||
.get(usize::from(internal))
|
||||
.copied()
|
||||
@@ -213,7 +202,7 @@ impl ChunkEncoder {
|
||||
let mut sections = Vec::with_capacity(SECTIONS);
|
||||
let mut entity_infos = Vec::new();
|
||||
for (i, s) in chunk.sections.iter().enumerate() {
|
||||
let (encoded, has_entities) = self.section(s, chunk, i);
|
||||
let (encoded, has_entities) = self.section(s);
|
||||
if has_entities {
|
||||
for y in 0..16 {
|
||||
for z in 0..16 {
|
||||
|
||||
@@ -216,6 +216,7 @@ pub fn light_chunk(
|
||||
let mut block = vec![0u8; W * W * h];
|
||||
let mut queue = VecDeque::new();
|
||||
let mut sky_queue = VecDeque::new();
|
||||
let mut ids = Box::new([0u16; 4096]);
|
||||
for (dz, row) in region.iter().enumerate() {
|
||||
for (dx, chunk) in row.iter().enumerate() {
|
||||
let Some(chunk) = chunk else { continue };
|
||||
@@ -223,8 +224,8 @@ pub fn light_chunk(
|
||||
if section.is_uniform(0) {
|
||||
continue;
|
||||
}
|
||||
for i in 0..4096 {
|
||||
let id = section.get(i);
|
||||
section.unpack(&mut ids);
|
||||
for (i, &id) in ids.iter().enumerate() {
|
||||
let (lx, lz, ly) = (i & 15, (i >> 4) & 15, i >> 8);
|
||||
let p = at(dx * 16 + lx, si * 16 + ly, dz * 16 + lz);
|
||||
states[p] = id;
|
||||
|
||||
@@ -12,7 +12,9 @@ use crate::{
|
||||
chunk::{Chunk, MIN_Y},
|
||||
models::catalog::BakedFace,
|
||||
};
|
||||
use horizon_core::{geometry::Direction, position::BlockPos, random::position_seed};
|
||||
use horizon_core::{
|
||||
biome::BiomeZoom, geometry::Direction, position::BlockPos, random::position_seed,
|
||||
};
|
||||
use horizon_world::{
|
||||
block_render::FaceVisibility,
|
||||
fluid::{FluidKind, FluidState},
|
||||
@@ -161,6 +163,8 @@ struct Info {
|
||||
legacy_solid: bool,
|
||||
/// `HalfTransparentBlock`/`LeavesBlock`: water sides show the overlay sprite.
|
||||
overlay: bool,
|
||||
/// `LeavesBlock`: with opaque leaves the faces between two leaf blocks are skipped.
|
||||
leaves: bool,
|
||||
blocks_flow: bool,
|
||||
ice: bool,
|
||||
sturdy: u8,
|
||||
@@ -313,6 +317,7 @@ impl MeshTables {
|
||||
offset,
|
||||
legacy_solid: s.legacy_solid,
|
||||
overlay: s.has_class("HalfTransparentBlock") || s.has_class("LeavesBlock"),
|
||||
leaves: s.has_class("LeavesBlock"),
|
||||
blocks_flow: assets.blocks_fluid_flow.contains(&*s.name),
|
||||
ice: s.has_class("IceBlock"),
|
||||
sturdy: s.sturdy_full,
|
||||
@@ -378,6 +383,13 @@ impl Padded {
|
||||
let mut blocks = vec![0u16; P * P * P];
|
||||
let mut light = vec![0u8; P * P * P];
|
||||
let base_y = MIN_Y + sy * 16;
|
||||
// The section itself is unpacked in one pass; the border cells of the
|
||||
// neighbouring sections are read one by one.
|
||||
let mut own = Box::new([0u16; 4096]);
|
||||
let center = region[1][1].map(|c| &c.sections[sy as usize]);
|
||||
if let Some(section) = center {
|
||||
section.unpack(&mut own);
|
||||
}
|
||||
for y in -B..16 + B {
|
||||
let wy = base_y + y;
|
||||
for z in -B..16 + B {
|
||||
@@ -385,7 +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] = 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;
|
||||
@@ -396,57 +414,113 @@ impl Padded {
|
||||
Self { blocks, light }
|
||||
}
|
||||
}
|
||||
/// Set on a vertex's sprite layer when its texture alpha must be ignored.
|
||||
pub const OPAQUE_LAYER: u32 = 1 << 31;
|
||||
const UNSET: u32 = u32::MAX;
|
||||
const NO_BIOME: u16 = u16::MAX;
|
||||
/// The `ColorResolver`s `ClientLevel` caches block tints for.
|
||||
#[derive(Clone, Copy)]
|
||||
struct Colors {
|
||||
grass: [u32; 256],
|
||||
tall_upper: [u32; 256],
|
||||
foliage: [u32; 256],
|
||||
dry: [u32; 256],
|
||||
water: [u32; 256],
|
||||
enum Resolver {
|
||||
Grass,
|
||||
Foliage,
|
||||
Dry,
|
||||
Water,
|
||||
}
|
||||
fn blend(region: &[[Option<&Chunk>; 3]; 3], biomes: &Biomes, cx: i32, cz: i32, y: i32) -> Colors {
|
||||
// Vanilla blends over a (2r+1)^2 area; r = 2 is the default option.
|
||||
const R: i32 = 2;
|
||||
let biome_at = |x: i32, z: i32| -> u16 {
|
||||
region[((z + 16) >> 4) as usize][((x + 16) >> 4) as usize]
|
||||
.map_or(0, |c| c.biome(x & 15, y, z & 15))
|
||||
};
|
||||
let mut colors = Colors {
|
||||
grass: [0; 256],
|
||||
tall_upper: [0; 256],
|
||||
foliage: [0; 256],
|
||||
dry: [0; 256],
|
||||
water: [0; 256],
|
||||
};
|
||||
let avg = |f: &dyn Fn(u16, i32, i32) -> u32, x: i32, z: i32| -> u32 {
|
||||
let (mut r, mut g, mut b) = (0, 0, 0);
|
||||
for dz in -R..=R {
|
||||
for dx in -R..=R {
|
||||
let c = f(biome_at(x + dx, z + dz), x + dx, z + dz);
|
||||
r += c >> 16 & 255;
|
||||
g += c >> 8 & 255;
|
||||
b += c & 255;
|
||||
}
|
||||
}
|
||||
let n = ((2 * R + 1) * (2 * R + 1)) as u32;
|
||||
(r / n) << 16 | (g / n) << 8 | (b / n)
|
||||
};
|
||||
for z in 0..16 {
|
||||
for x in 0..16 {
|
||||
let i = (z * 16 + x) as usize;
|
||||
let (wx, wz) = (cx * 16 + x, cz * 16 + z);
|
||||
colors.grass[i] = avg(
|
||||
&|b, dx, dz| biomes.grass(b, f64::from(wx + dx - x), f64::from(wz + dz - z)),
|
||||
x,
|
||||
z,
|
||||
);
|
||||
colors.tall_upper[i] = colors.grass[i];
|
||||
colors.foliage[i] = avg(&|b, _, _| biomes.get(b).foliage, x, z);
|
||||
colors.dry[i] = avg(&|b, _, _| biomes.get(b).dry_foliage, x, z);
|
||||
colors.water[i] = avg(&|b, _, _| biomes.get(b).water, x, z);
|
||||
/// Biome tints of one block layer: the zoomed biome of every block of the
|
||||
/// layer plus the blend radius around it, and the blended colours per column.
|
||||
struct Layer {
|
||||
zoomed: Vec<u16>,
|
||||
colors: [[u32; 256]; 4],
|
||||
}
|
||||
/// `ClientLevel.calculateBlockTint` behind a per-layer `BlockTintCache`: each
|
||||
/// block averages the colours of the biomes `BiomeManager.getBiome` picks (the
|
||||
/// eight-corner zoom) over a `(2r+1)^2` square at the block's own height.
|
||||
/// Layers and columns are only computed when a tinted block asks for them.
|
||||
struct Tints<'a> {
|
||||
region: &'a [[Option<&'a Chunk>; 3]; 3],
|
||||
biomes: &'a Biomes,
|
||||
zoom: BiomeZoom,
|
||||
radius: i32,
|
||||
plains: u16,
|
||||
/// Chunk x, chunk z and the section's lowest block y.
|
||||
origin: [i32; 3],
|
||||
layers: [Option<Box<Layer>>; 16],
|
||||
}
|
||||
impl<'a> Tints<'a> {
|
||||
fn new(
|
||||
region: &'a [[Option<&'a Chunk>; 3]; 3],
|
||||
biomes: &'a Biomes,
|
||||
zoom: BiomeZoom,
|
||||
radius: i32,
|
||||
origin: [i32; 3],
|
||||
) -> Self {
|
||||
Self {
|
||||
region,
|
||||
biomes,
|
||||
zoom,
|
||||
radius: radius.clamp(0, 7),
|
||||
plains: biomes.id("minecraft:plains"),
|
||||
origin,
|
||||
layers: Default::default(),
|
||||
}
|
||||
}
|
||||
colors
|
||||
/// The blended tint of block `(x, y, z)` (section-local x/z, y in 0..16).
|
||||
fn get(&mut self, resolver: Resolver, x: i32, y: i32, z: i32) -> u32 {
|
||||
let r = self.radius;
|
||||
let dim = (16 + 2 * r) as usize;
|
||||
let layer = self.layers[y as usize].get_or_insert_with(|| {
|
||||
Box::new(Layer {
|
||||
zoomed: vec![NO_BIOME; dim * dim],
|
||||
colors: [[UNSET; 256]; 4],
|
||||
})
|
||||
});
|
||||
let slot = (z * 16 + x) as usize;
|
||||
let cached = layer.colors[resolver as usize][slot];
|
||||
if cached != UNSET {
|
||||
return cached;
|
||||
}
|
||||
let [cx, cz, base_y] = self.origin;
|
||||
let (bx, by, bz) = (cx * 16, base_y + y, cz * 16);
|
||||
let (mut red, mut green, mut blue) = (0, 0, 0);
|
||||
for dz in -r..=r {
|
||||
for dx in -r..=r {
|
||||
let cell = ((z + dz + r) as usize) * dim + (x + dx + r) as usize;
|
||||
let mut biome = layer.zoomed[cell];
|
||||
if biome == NO_BIOME {
|
||||
// BiomeManager.getBiome over the loaded chunks (plains
|
||||
// where there is none).
|
||||
let q = self
|
||||
.zoom
|
||||
.quart_at_block(BlockPos::new(bx + x + dx, by, bz + z + dz));
|
||||
let (rx, rz) = ((q[0] >> 2) - cx + 1, (q[2] >> 2) - cz + 1);
|
||||
biome = if (0..3).contains(&rx) && (0..3).contains(&rz) {
|
||||
self.region[rz as usize][rx as usize].map_or(self.plains, |c| {
|
||||
c.biome((q[0] & 3) * 4, q[1] * 4, (q[2] & 3) * 4)
|
||||
})
|
||||
} else {
|
||||
self.plains
|
||||
};
|
||||
layer.zoomed[cell] = biome;
|
||||
}
|
||||
let c = match resolver {
|
||||
Resolver::Grass => {
|
||||
self.biomes
|
||||
.grass(biome, f64::from(bx + x + dx), f64::from(bz + z + dz))
|
||||
}
|
||||
Resolver::Foliage => self.biomes.get(biome).foliage,
|
||||
Resolver::Dry => self.biomes.get(biome).dry_foliage,
|
||||
Resolver::Water => self.biomes.get(biome).water,
|
||||
};
|
||||
red += c >> 16 & 255;
|
||||
green += c >> 8 & 255;
|
||||
blue += c & 255;
|
||||
}
|
||||
}
|
||||
let n = ((2 * r + 1) * (2 * r + 1)) as u32;
|
||||
let color = (red / n) << 16 | (green / n) << 8 | (blue / n);
|
||||
layer.colors[resolver as usize][slot] = color;
|
||||
color
|
||||
}
|
||||
}
|
||||
/// Packs 8-bit RGB into the vertex's RGBA8 (red in the low byte), opaque.
|
||||
fn pack_rgb([r, g, b]: [u32; 3]) -> u32 {
|
||||
@@ -464,6 +538,14 @@ pub struct Mesher<'a> {
|
||||
pub tables: &'a MeshTables,
|
||||
pub biomes: &'a Biomes,
|
||||
pub visibility: FaceVisibility,
|
||||
/// The client's biome zoom (the hashed world seed `BiomeManager` holds).
|
||||
pub zoom: BiomeZoom,
|
||||
/// The `biomeBlendRadius` option, 0 to 7.
|
||||
pub blend_radius: i32,
|
||||
/// The `ao` option: smooth lighting for models that allow it.
|
||||
pub smooth_lighting: bool,
|
||||
/// The `cutoutLeaves` option (fancy graphics); off makes leaves opaque.
|
||||
pub cutout_leaves: bool,
|
||||
}
|
||||
impl Mesher<'_> {
|
||||
/// `region[dz][dx]` are the chunks around the meshed column (centre 1,1).
|
||||
@@ -477,7 +559,13 @@ impl Mesher<'_> {
|
||||
let pad = Padded::new(region, sy);
|
||||
out.visibility = visibility(&pad, &self.tables.info);
|
||||
let base_y = MIN_Y + sy * 16;
|
||||
let colors = blend(region, self.biomes, center.x, center.z, base_y + 8);
|
||||
let mut tints = Tints::new(
|
||||
region,
|
||||
self.biomes,
|
||||
self.zoom,
|
||||
self.blend_radius,
|
||||
[center.x, center.z, base_y],
|
||||
);
|
||||
let info = &self.tables.info;
|
||||
for y in 0..16 {
|
||||
for z in 0..16 {
|
||||
@@ -502,7 +590,7 @@ impl Mesher<'_> {
|
||||
continue;
|
||||
}
|
||||
if state.fluid.kind() != FluidKind::Empty {
|
||||
let water = colors.water[(z * 16 + x) as usize];
|
||||
let water = tints.get(Resolver::Water, x, y, z);
|
||||
let nether = self.nether_shading;
|
||||
fluid::tesselate(self.tables, nether, &pad, water, &mut out, [x, y, z]);
|
||||
}
|
||||
@@ -522,7 +610,7 @@ impl Mesher<'_> {
|
||||
let Some((_, first)) = models.first() else {
|
||||
continue;
|
||||
};
|
||||
let ao = state.emission == 0 && first.ambient_occlusion;
|
||||
let ao = self.smooth_lighting && state.emission == 0 && first.ambient_occlusion;
|
||||
let origin = match offset(state, world.x, world.z) {
|
||||
Some(o) => [
|
||||
x as f32 + o[0] as f32,
|
||||
@@ -544,13 +632,26 @@ impl Mesher<'_> {
|
||||
true,
|
||||
)
|
||||
.unwrap_or(true);
|
||||
// LeavesBlock.skipRendering with opaque leaves.
|
||||
if !self.cutout_leaves && state.leaves && info[neighbor as usize].leaves {
|
||||
visible[d as usize] = false;
|
||||
}
|
||||
}
|
||||
for (_, model) in &models {
|
||||
for face in &model.faces {
|
||||
if face.quad.cull.is_some_and(|d| !visible[d as usize]) {
|
||||
continue;
|
||||
}
|
||||
self.face(&pad, &colors, &mut out, [x, y, z], origin, face, ao, state);
|
||||
self.face(
|
||||
&pad,
|
||||
&mut tints,
|
||||
&mut out,
|
||||
[x, y, z],
|
||||
origin,
|
||||
face,
|
||||
ao,
|
||||
state,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -559,18 +660,17 @@ impl Mesher<'_> {
|
||||
out
|
||||
}
|
||||
/// `BlockColors` tint source `index`, or `None` for an untinted (-1) layer.
|
||||
fn tint(state: &Info, index: i32, colors: &Colors, x: i32, z: i32) -> Option<u32> {
|
||||
fn tint(state: &Info, index: i32, tints: &mut Tints, [x, y, z]: [i32; 3]) -> Option<u32> {
|
||||
if index < 0 || index >= i32::from(state.tint_count) {
|
||||
return None;
|
||||
}
|
||||
let column = (z * 16 + x) as usize;
|
||||
Some(match state.tints[(index as usize).min(1)] {
|
||||
Tint::None => return None,
|
||||
Tint::Grass => colors.grass[column],
|
||||
Tint::TallGrassUpper => colors.tall_upper[column],
|
||||
Tint::Foliage => colors.foliage[column],
|
||||
Tint::DryFoliage => colors.dry[column],
|
||||
Tint::Water => colors.water[column],
|
||||
Tint::Grass => tints.get(Resolver::Grass, x, y, z),
|
||||
Tint::TallGrassUpper => tints.get(Resolver::Grass, x, y, z),
|
||||
Tint::Foliage => tints.get(Resolver::Foliage, x, y, z),
|
||||
Tint::DryFoliage => tints.get(Resolver::Dry, x, y, z),
|
||||
Tint::Water => tints.get(Resolver::Water, x, y, z),
|
||||
Tint::Constant(c) => c,
|
||||
Tint::Redstone(p) => redstone_color(p),
|
||||
Tint::Stem(age) => {
|
||||
@@ -583,7 +683,7 @@ impl Mesher<'_> {
|
||||
fn face(
|
||||
&self,
|
||||
pad: &Padded,
|
||||
colors: &Colors,
|
||||
tints: &mut Tints,
|
||||
out: &mut SectionMesh,
|
||||
pos: [i32; 3],
|
||||
origin: [f32; 3],
|
||||
@@ -608,7 +708,7 @@ impl Mesher<'_> {
|
||||
};
|
||||
([lighting::gray(directional); 4], [coords; 4])
|
||||
};
|
||||
let tint = Self::tint(state, q.tint_index, colors, pos[0], pos[2]);
|
||||
let tint = Self::tint(state, q.tint_index, tints, pos);
|
||||
let target = if face.translucent {
|
||||
&mut out.translucent
|
||||
} else {
|
||||
@@ -624,7 +724,12 @@ impl Mesher<'_> {
|
||||
target.push(Vertex {
|
||||
pos: [p[0] + origin[0], p[1] + origin[1], p[2] + origin[2]],
|
||||
uv: q.uv[i],
|
||||
layer: face.layer,
|
||||
// Opaque leaves (forceOpaque) draw in the solid layer: no alpha cutout.
|
||||
layer: if !self.cutout_leaves && state.leaves {
|
||||
face.layer | OPAQUE_LAYER
|
||||
} else {
|
||||
face.layer
|
||||
},
|
||||
color: pack_rgb(rgb),
|
||||
light: pack_coords(lighting::with_emission(coords[i], q.light_emission)),
|
||||
});
|
||||
|
||||
@@ -16,10 +16,11 @@
|
||||
use crate::{
|
||||
Result,
|
||||
biome::Biomes,
|
||||
chunk::{Blocks, Chunk, Light, MIN_Y, SECTIONS},
|
||||
chunk::{BIOME_STRATEGY, BLOCK_STRATEGY, Chunk, Light, MIN_Y, SECTIONS},
|
||||
streaming::ChunkGenerator,
|
||||
};
|
||||
use horizon_core::{
|
||||
paletted::PalettedContainer,
|
||||
position::{BlockPos, ChunkPos},
|
||||
ticks::{SavedTick, TickPriority, TickTypeId},
|
||||
};
|
||||
@@ -42,29 +43,6 @@ use std::{
|
||||
|
||||
pub const VERSION_NAME: &str = "26.3";
|
||||
|
||||
fn pack(indices: &[u16], bits: u32) -> Vec<i64> {
|
||||
let per_long = (64 / bits) as usize;
|
||||
let mut longs = vec![0i64; indices.len().div_ceil(per_long)];
|
||||
for (i, &v) in indices.iter().enumerate() {
|
||||
longs[i / per_long] |=
|
||||
(i64::from(v) & ((1i64 << bits) - 1)) << ((i % per_long) as u32 * bits);
|
||||
}
|
||||
longs
|
||||
}
|
||||
fn unpack(longs: &[i64], bits: u32, count: usize) -> Vec<u16> {
|
||||
let per_long = (64 / bits) as usize;
|
||||
let mask = (1u64 << bits) - 1;
|
||||
(0..count)
|
||||
.map(|i| {
|
||||
longs.get(i / per_long).map_or(0, |&l| {
|
||||
((l as u64 >> ((i % per_long) as u32 * bits)) & mask) as u16
|
||||
})
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
fn bits_for(len: usize, min: u32) -> u32 {
|
||||
(usize::BITS - (len.max(1) - 1).leading_zeros()).max(min)
|
||||
}
|
||||
fn string(s: &str) -> Tag {
|
||||
Tag::String(s.into())
|
||||
}
|
||||
@@ -670,33 +648,22 @@ pub fn encode_with(
|
||||
});
|
||||
for (si, section) in chunk.sections.iter().enumerate() {
|
||||
let y = (MIN_Y >> 4) + si as i32;
|
||||
let ids: Vec<u16> = match §ion.blocks {
|
||||
Blocks::Uniform(v) => vec![*v],
|
||||
Blocks::Full(a) => a.to_vec(),
|
||||
};
|
||||
let mut palette: Vec<u16> = Vec::new();
|
||||
let mut lookup: HashMap<u16, u16> = HashMap::new();
|
||||
let indices: Vec<u16> = ids
|
||||
.iter()
|
||||
.map(|id| {
|
||||
*lookup.entry(*id).or_insert_with(|| {
|
||||
palette.push(*id);
|
||||
(palette.len() - 1) as u16
|
||||
})
|
||||
})
|
||||
.collect();
|
||||
// PalettedContainer.pack: distinct states in index order, indices at
|
||||
// the saved width.
|
||||
let packed = section.blocks.pack();
|
||||
let mut states = vec![(
|
||||
"palette",
|
||||
Tag::List(
|
||||
palette
|
||||
packed
|
||||
.palette
|
||||
.iter()
|
||||
.map(|id| {
|
||||
let state = registry.state(BlockStateId(u32::from(*id)));
|
||||
.map(|&id| {
|
||||
let state = registry.state(BlockStateId(id));
|
||||
match state {
|
||||
// BlockState.CODEC: the block name for default states.
|
||||
Some(s)
|
||||
if tables.is_some_and(|t| {
|
||||
t.default_state.get(*id as usize) == Some(&true)
|
||||
t.default_state.get(id as usize) == Some(&true)
|
||||
}) =>
|
||||
{
|
||||
string(&s.name)
|
||||
@@ -708,37 +675,27 @@ pub fn encode_with(
|
||||
.collect(),
|
||||
),
|
||||
)];
|
||||
if palette.len() > 1 {
|
||||
if let Some(data) = &packed.data {
|
||||
states.push((
|
||||
"data",
|
||||
Tag::LongArray(pack(&indices, bits_for(palette.len(), 4))),
|
||||
Tag::LongArray(data.iter().map(|&w| w as i64).collect()),
|
||||
));
|
||||
}
|
||||
let quart: Vec<u16> = (0..64).map(|i| chunk.biomes[si * 64 + i]).collect();
|
||||
let mut biome_palette: Vec<u16> = Vec::new();
|
||||
let biome_indices: Vec<u16> = quart
|
||||
.iter()
|
||||
.map(|b| match biome_palette.iter().position(|p| p == b) {
|
||||
Some(i) => i as u16,
|
||||
None => {
|
||||
biome_palette.push(*b);
|
||||
(biome_palette.len() - 1) as u16
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
let biome_packed = section.biomes.pack();
|
||||
let mut biome_tag = vec![(
|
||||
"palette",
|
||||
Tag::List(
|
||||
biome_palette
|
||||
biome_packed
|
||||
.palette
|
||||
.iter()
|
||||
.map(|b| string(&biomes.get(*b).name))
|
||||
.map(|&b| string(&biomes.get(b as u16).name))
|
||||
.collect(),
|
||||
),
|
||||
)];
|
||||
if biome_palette.len() > 1 {
|
||||
if let Some(data) = &biome_packed.data {
|
||||
biome_tag.push((
|
||||
"data",
|
||||
Tag::LongArray(pack(&biome_indices, bits_for(biome_palette.len(), 0))),
|
||||
Tag::LongArray(data.iter().map(|&w| w as i64).collect()),
|
||||
));
|
||||
}
|
||||
let mut entries = vec![
|
||||
@@ -918,7 +875,6 @@ pub fn decode_with(
|
||||
}
|
||||
let air = registry.default_state("minecraft:air")?.0 as u16;
|
||||
let mut cache: HashMap<String, u16> = HashMap::new();
|
||||
let mut biome_data = (*chunk.biomes).clone();
|
||||
let Some(Tag::List(sections)) = tag.get("sections") else {
|
||||
return Some(chunk);
|
||||
};
|
||||
@@ -961,15 +917,17 @@ pub fn decode_with(
|
||||
})
|
||||
.collect();
|
||||
let target = Arc::make_mut(&mut chunk.sections[si as usize]);
|
||||
if ids.len() <= 1 {
|
||||
target.blocks = Blocks::Uniform(ids.first().copied().unwrap_or(air));
|
||||
} else if let Some(Tag::LongArray(data)) = states.get("data") {
|
||||
let indices = unpack(data, bits_for(ids.len(), 4), 4096);
|
||||
let mut full = Box::new([air; 4096]);
|
||||
for (i, idx) in indices.iter().enumerate() {
|
||||
full[i] = ids.get(*idx as usize).copied().unwrap_or(air);
|
||||
let data = match states.get("data") {
|
||||
Some(Tag::LongArray(data)) => {
|
||||
Some(data.iter().map(|&w| w as u64).collect::<Vec<_>>())
|
||||
}
|
||||
target.blocks = Blocks::Full(full);
|
||||
_ => 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())
|
||||
{
|
||||
target.blocks = blocks;
|
||||
}
|
||||
}
|
||||
if let Some(b) = section.get("biomes")
|
||||
@@ -982,18 +940,23 @@ pub fn decode_with(
|
||||
_ => 0,
|
||||
})
|
||||
.collect();
|
||||
let indices = match b.get("data") {
|
||||
Some(Tag::LongArray(data)) if ids.len() > 1 => {
|
||||
unpack(data, bits_for(ids.len(), 0), 64)
|
||||
let data = match b.get("data") {
|
||||
Some(Tag::LongArray(data)) => {
|
||||
Some(data.iter().map(|&w| w as u64).collect::<Vec<_>>())
|
||||
}
|
||||
_ => vec![0; 64],
|
||||
_ => None,
|
||||
};
|
||||
for (i, idx) in indices.iter().enumerate() {
|
||||
biome_data[si as usize * 64 + i] = ids.get(*idx as usize).copied().unwrap_or(0);
|
||||
let palette: Vec<u32> = ids.iter().map(|&id| u32::from(id)).collect();
|
||||
let target = Arc::make_mut(&mut chunk.sections[si as usize]);
|
||||
if let Ok(container) =
|
||||
PalettedContainer::from_packed(BIOME_STRATEGY, palette.clone(), data.as_deref())
|
||||
{
|
||||
target.biomes = container;
|
||||
} else if let Some(&first) = palette.first() {
|
||||
target.biomes = PalettedContainer::new(BIOME_STRATEGY, first);
|
||||
}
|
||||
}
|
||||
}
|
||||
chunk.biomes = Arc::new(biome_data);
|
||||
let mut extras = ChunkExtras {
|
||||
last_update: number(tag.get("LastUpdate")).unwrap_or(0),
|
||||
..ChunkExtras::default()
|
||||
|
||||
@@ -8,6 +8,7 @@ use crate::{
|
||||
mesher::{MeshTables, Mesher, Region, SectionMesh, borrow},
|
||||
world::World,
|
||||
};
|
||||
use horizon_core::biome::BiomeZoom;
|
||||
use std::{
|
||||
collections::{BinaryHeap, HashMap, HashSet},
|
||||
sync::{
|
||||
@@ -40,12 +41,21 @@ pub trait ChunkGenerator: Send + Sync + 'static {
|
||||
fn decorate(&self, _worker: &mut Self::Worker, _region: &mut [Proto; 9]) {}
|
||||
fn finish(&self, proto: Proto) -> Chunk;
|
||||
}
|
||||
/// Per-job mesher inputs that change with the dimension or the options.
|
||||
#[derive(Clone, Copy)]
|
||||
struct MeshSettings {
|
||||
nether: bool,
|
||||
zoom: BiomeZoom,
|
||||
blend_radius: i32,
|
||||
smooth_lighting: bool,
|
||||
cutout_leaves: bool,
|
||||
}
|
||||
enum Job {
|
||||
Generate(i32, i32),
|
||||
Decorate(i32, i32, Box<[Proto; 9]>),
|
||||
Finish(Proto),
|
||||
Light(i32, i32, Box<Region>, u64, bool),
|
||||
Mesh(i32, i32, i32, Box<Region>, u64, bool),
|
||||
Mesh(i32, i32, i32, Box<Region>, u64, MeshSettings),
|
||||
}
|
||||
pub enum Done {
|
||||
Generated(Chunk),
|
||||
@@ -111,6 +121,12 @@ pub struct Streaming {
|
||||
/// Dimension flags: sky light, Nether face shading.
|
||||
pub sky_light: bool,
|
||||
pub nether_shading: bool,
|
||||
/// `BiomeManager`'s zoom seed (the hashed world seed) and `biomeBlendRadius`.
|
||||
pub biome_zoom: BiomeZoom,
|
||||
pub biome_blend: i32,
|
||||
/// The `ao` and `cutoutLeaves` options.
|
||||
pub smooth_lighting: bool,
|
||||
pub cutout_leaves: bool,
|
||||
/// Incremental relighting of block edits on lit chunks (see `light::relight`).
|
||||
light_tables: Arc<LightTables>,
|
||||
/// Applied edits by sequence, replayed onto light jobs snapshotted before them.
|
||||
@@ -158,6 +174,10 @@ impl Streaming {
|
||||
let mut worker = generator.worker();
|
||||
let mut mesher = Mesher {
|
||||
nether_shading: false,
|
||||
zoom: BiomeZoom::new(0),
|
||||
blend_radius: 2,
|
||||
smooth_lighting: true,
|
||||
cutout_leaves: true,
|
||||
assets: &context.assets,
|
||||
tables: &context.tables,
|
||||
biomes: &context.biomes,
|
||||
@@ -192,8 +212,12 @@ impl Streaming {
|
||||
light_chunk(&borrow(®ion), &context.light, sky),
|
||||
seq,
|
||||
),
|
||||
Job::Mesh(x, sy, z, region, seq, nether) => {
|
||||
mesher.nether_shading = nether;
|
||||
Job::Mesh(x, sy, z, region, seq, settings) => {
|
||||
mesher.nether_shading = settings.nether;
|
||||
mesher.zoom = settings.zoom;
|
||||
mesher.blend_radius = settings.blend_radius;
|
||||
mesher.smooth_lighting = settings.smooth_lighting;
|
||||
mesher.cutout_leaves = settings.cutout_leaves;
|
||||
Done::Meshed(x, sy, z, mesher.mesh(&borrow(®ion), sy), seq)
|
||||
}
|
||||
};
|
||||
@@ -227,6 +251,10 @@ impl Streaming {
|
||||
paused: false,
|
||||
sky_light: true,
|
||||
nether_shading: false,
|
||||
biome_zoom: BiomeZoom::new(0),
|
||||
biome_blend: 2,
|
||||
smooth_lighting: true,
|
||||
cutout_leaves: true,
|
||||
light_tables,
|
||||
light_log: Vec::new(),
|
||||
light_edit_sequence: 0,
|
||||
@@ -599,7 +627,20 @@ impl Streaming {
|
||||
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, self.nether_shading),
|
||||
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,
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
(uploads, removed)
|
||||
|
||||
@@ -59,12 +59,13 @@ fn apply_fog(color: vec4<f32>, spherical: f32, cylindrical: f32) -> vec4<f32> {
|
||||
return vec4(mix(color.rgb, globals.fog_color.rgb, f * globals.fog_color.a), color.a);
|
||||
}
|
||||
@fragment fn fs_solid(in: TerrainOut) -> @location(0) vec4<f32> {
|
||||
let color = textureSample(sprites, sprite_sampler, in.uv, i32(in.layer)) * in.color;
|
||||
if color.a < 0.5 { discard; }
|
||||
// Bit 31 of the layer marks opaque leaves (forceOpaque): no alpha cutout.
|
||||
let color = textureSample(sprites, sprite_sampler, in.uv, i32(in.layer & 0x7fffffffu)) * in.color;
|
||||
if color.a < 0.5 && (in.layer & 0x80000000u) == 0u { discard; }
|
||||
return apply_fog(vec4(color.rgb, 1.0), in.spherical, in.cylindrical);
|
||||
}
|
||||
@fragment fn fs_translucent(in: TerrainOut) -> @location(0) vec4<f32> {
|
||||
let color = textureSample(sprites, sprite_sampler, in.uv, i32(in.layer)) * in.color;
|
||||
let color = textureSample(sprites, sprite_sampler, in.uv, i32(in.layer & 0x7fffffffu)) * in.color;
|
||||
if color.a < 0.01 { discard; }
|
||||
return apply_fog(color, in.spherical, in.cylindrical);
|
||||
}
|
||||
|
||||
@@ -76,7 +76,12 @@ impl ChunkGenerator for TestTerrain {
|
||||
impl TestTerrain {
|
||||
fn generate(&self, _: &mut (), cx: i32, cz: i32) -> Chunk {
|
||||
let mut chunk = Chunk::empty(cx, cz);
|
||||
chunk.biomes = std::sync::Arc::new(vec![self.plains; chunk.biomes.len()]);
|
||||
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),
|
||||
);
|
||||
}
|
||||
for z in 0..16 {
|
||||
for x in 0..16 {
|
||||
let (wx, wz) = (cx * 16 + x, cz * 16 + z);
|
||||
|
||||
@@ -2,10 +2,10 @@
|
||||
//! aquifers, surface rules, carvers and placed features) to chunk streaming.
|
||||
use crate::{
|
||||
biome::Biomes,
|
||||
chunk::{Chunk, HEIGHT, MIN_Y},
|
||||
chunk::{BIOME_STRATEGY, Chunk, HEIGHT, MIN_Y, SECTIONS, Section},
|
||||
streaming::{ChunkGenerator, Proto},
|
||||
};
|
||||
use horizon_core::position::ChunkPos;
|
||||
use horizon_core::{paletted::PalettedContainer, position::ChunkPos};
|
||||
use horizon_world::{
|
||||
registry::BlockRegistry,
|
||||
worldgen::generator::{GeneratorData, GeneratorWorker, OverworldGenerator, ProtoChunk},
|
||||
@@ -177,15 +177,27 @@ impl ChunkGenerator for VanillaTerrain {
|
||||
chunk.terrain.min_y().max(MIN_Y),
|
||||
(chunk.terrain.min_y() + chunk.terrain.height()).min(MIN_Y + HEIGHT),
|
||||
);
|
||||
for y in lo..hi {
|
||||
for z in 0..16 {
|
||||
for x in 0..16 {
|
||||
let id = chunk.block(bx + x, y, bz + z).0 as u16;
|
||||
if id != 0 {
|
||||
out.set(x, y, z, id);
|
||||
let mut values = Box::new([0u16; 4096]);
|
||||
for si in 0..SECTIONS as i32 {
|
||||
let base = MIN_Y + si * 16;
|
||||
if base + 16 <= lo || base >= hi {
|
||||
continue;
|
||||
}
|
||||
values.fill(0);
|
||||
let mut any = false;
|
||||
for y in base.max(lo)..(base + 16).min(hi) {
|
||||
for z in 0..16 {
|
||||
for x in 0..16 {
|
||||
let id = chunk.block(bx + x, y, bz + z).0 as u16;
|
||||
values[((y & 15) << 8 | z << 4 | x) as usize] = id;
|
||||
any |= id != 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
if any {
|
||||
Arc::make_mut(&mut out.sections[si as usize]).blocks =
|
||||
Section::from_blocks(&values);
|
||||
}
|
||||
}
|
||||
let base_section = chunk.terrain.min_y() >> 4;
|
||||
for (i, marks) in chunk.terrain.postprocessing().iter().enumerate() {
|
||||
@@ -199,19 +211,17 @@ impl ChunkGenerator for VanillaTerrain {
|
||||
}
|
||||
}
|
||||
}
|
||||
let mut biomes = vec![0u16; 16 * (HEIGHT as usize / 4)];
|
||||
for qy in 0..HEIGHT / 4 {
|
||||
for qz in 0..4 {
|
||||
for qx in 0..4 {
|
||||
let y = (MIN_Y + qy * 4).clamp(lo, hi - 1);
|
||||
let id = chunk.biome_id_at(bx + qx * 4, y, bz + qz * 4);
|
||||
let index = id.0 as usize;
|
||||
biomes[(qy * 16 + qz * 4 + qx) as usize] =
|
||||
self.biome_map.get(index).copied().unwrap_or(0);
|
||||
}
|
||||
for si in 0..SECTIONS as i32 {
|
||||
let mut cells = [0u32; 64];
|
||||
for (j, cell) in cells.iter_mut().enumerate() {
|
||||
let (qx, qz, qy) = (j as i32 & 3, (j as i32 >> 2) & 3, si * 4 + (j as i32 >> 4));
|
||||
let y = (MIN_Y + qy * 4).clamp(lo, hi - 1);
|
||||
let id = chunk.biome_id_at(bx + qx * 4, y, bz + qz * 4);
|
||||
*cell = u32::from(self.biome_map.get(id.0 as usize).copied().unwrap_or(0));
|
||||
}
|
||||
Arc::make_mut(&mut out.sections[si as usize]).biomes =
|
||||
PalettedContainer::from_values(BIOME_STRATEGY, &cells);
|
||||
}
|
||||
out.biomes = Arc::new(biomes);
|
||||
out.block_entities = crate::structure_blocks::block_entities(&chunk, self.loot.as_deref());
|
||||
out.structure_refs = chunk
|
||||
.structures()
|
||||
|
||||
@@ -0,0 +1,136 @@
|
||||
//! Text transport for the PalettedContainer comparison: replays scripted edits
|
||||
//! and prints the same hashes as `VanillaPalettedContainerOracle`.
|
||||
use horizon_core::paletted::{PalettedContainer, Strategy};
|
||||
use std::{
|
||||
fs,
|
||||
io::{self, Write},
|
||||
};
|
||||
const BASIS: u64 = 0xcbf2_9ce4_8422_2325;
|
||||
fn fnv(h: u64, b: u8) -> u64 {
|
||||
(h ^ u64::from(b)).wrapping_mul(0x100_0000_01b3)
|
||||
}
|
||||
fn hash_bytes(bytes: &[u8]) -> u64 {
|
||||
bytes.iter().fold(BASIS, |h, &b| fnv(h, b))
|
||||
}
|
||||
fn varint(out: &mut Vec<u8>, mut v: u32) {
|
||||
while v >= 0x80 {
|
||||
out.push((v & 0x7F) as u8 | 0x80);
|
||||
v >>= 7;
|
||||
}
|
||||
out.push(v as u8);
|
||||
}
|
||||
/// `PalettedContainer.write`.
|
||||
fn write(c: &PalettedContainer) -> Vec<u8> {
|
||||
let (bits, palette, words) = c.to_wire();
|
||||
let mut out = vec![bits];
|
||||
if bits == 0 {
|
||||
varint(&mut out, palette[0]);
|
||||
} else if !palette.is_empty() {
|
||||
varint(&mut out, palette.len() as u32);
|
||||
for p in palette {
|
||||
varint(&mut out, p);
|
||||
}
|
||||
}
|
||||
for w in words {
|
||||
out.extend(w.to_be_bytes());
|
||||
}
|
||||
out
|
||||
}
|
||||
/// `PalettedContainer.read` for the leading byte, palette and longs.
|
||||
fn read(strategy: Strategy, bytes: &[u8]) -> PalettedContainer {
|
||||
let mut at = 1;
|
||||
let mut varint = || {
|
||||
let (mut v, mut shift) = (0u32, 0);
|
||||
loop {
|
||||
let b = bytes[at];
|
||||
at += 1;
|
||||
v |= u32::from(b & 0x7F) << shift;
|
||||
if b & 0x80 == 0 {
|
||||
return v;
|
||||
}
|
||||
shift += 7;
|
||||
}
|
||||
};
|
||||
let bits = bytes[0];
|
||||
let palette = if bits == 0 {
|
||||
vec![varint()]
|
||||
} else if strategy.wire_has_palette(bits) {
|
||||
let n = varint();
|
||||
(0..n).map(|_| varint()).collect()
|
||||
} else {
|
||||
Vec::new()
|
||||
};
|
||||
let words = bytes[at..]
|
||||
.chunks_exact(8)
|
||||
.map(|c| u64::from_be_bytes(c.try_into().unwrap()))
|
||||
.collect();
|
||||
PalettedContainer::from_wire(strategy, bits, palette, words).unwrap()
|
||||
}
|
||||
fn wire(c: &PalettedContainer) -> String {
|
||||
let bytes = write(c);
|
||||
format!("{}:{}", bytes.len(), hash_bytes(&bytes) as i64)
|
||||
}
|
||||
fn values(c: &PalettedContainer) -> i64 {
|
||||
let mut h = BASIS;
|
||||
for i in 0..c.strategy().entries() {
|
||||
for b in c.get(i).to_le_bytes() {
|
||||
h = fnv(h, b);
|
||||
}
|
||||
}
|
||||
h as i64
|
||||
}
|
||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let args: Vec<String> = std::env::args().collect();
|
||||
let mut out = io::BufWriter::new(fs::File::create(&args[2])?);
|
||||
for line in fs::read_to_string(&args[1])?.lines() {
|
||||
let t: Vec<&str> = line.split_whitespace().collect();
|
||||
let strategy = if t[1] == "blocks" {
|
||||
Strategy::blocks(16)
|
||||
} else {
|
||||
Strategy::biomes(7)
|
||||
};
|
||||
let mut c = PalettedContainer::new(strategy, t[2].parse()?);
|
||||
let every: usize = t[3].parse()?;
|
||||
let (mut olds, mut n) = (BASIS, 0usize);
|
||||
let mut checkpoints = Vec::new();
|
||||
for pair in t[4..].chunks(2) {
|
||||
let old = c.set(pair[0].parse()?, pair[1].parse()?);
|
||||
for b in old.to_le_bytes() {
|
||||
olds = fnv(olds, b);
|
||||
}
|
||||
n += 1;
|
||||
if n % every == 0 {
|
||||
checkpoints.push(wire(&c));
|
||||
}
|
||||
}
|
||||
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, 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(),
|
||||
)?;
|
||||
writeln!(
|
||||
out,
|
||||
"{} {} cp={} bits={} final={} values={} read={}/{} packed={}/{}/{} loaded={}/{}",
|
||||
t[0],
|
||||
olds as i64,
|
||||
checkpoints.join(","),
|
||||
c.bits(),
|
||||
wire(&c),
|
||||
values(&c),
|
||||
values(&reread),
|
||||
wire(&reread),
|
||||
packed.bits,
|
||||
storage as i64,
|
||||
palette.join(","),
|
||||
values(&loaded),
|
||||
wire(&loaded),
|
||||
)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@@ -9,6 +9,7 @@ pub mod carving;
|
||||
pub mod density;
|
||||
pub mod geometry;
|
||||
pub mod noise;
|
||||
pub mod paletted;
|
||||
pub mod position;
|
||||
pub mod random;
|
||||
pub mod storage;
|
||||
|
||||
@@ -0,0 +1,744 @@
|
||||
//! Resident `PalettedContainer` of Java Edition 26.3: single-value, linear and
|
||||
//! hash-map palettes with vanilla's bit-width thresholds, and the global
|
||||
//! palette beyond them, over `SimpleBitStorage` words (values never span
|
||||
//! longs). One container type serves blocks (16^3 entries) and biomes (4^3).
|
||||
//!
|
||||
//! Growth follows `PalettedContainer.onResize`: a full palette asks for one
|
||||
//! more bit, the new configuration is built by re-adding the distinct values
|
||||
//! in index order, and the value that did not fit is added last. Palettes
|
||||
//! never shrink while resident; `pack` writes the canonical saved form.
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum Kind {
|
||||
/// `Strategy.createForBlockStates`: 4096 entries, linear palettes up to 4
|
||||
/// bits and hash-map palettes from 5 to 8.
|
||||
Blocks,
|
||||
/// `Strategy.createForBiomes`: 64 entries, linear palettes up to 3 bits.
|
||||
Biomes,
|
||||
}
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub struct Strategy {
|
||||
pub kind: Kind,
|
||||
/// `ceillog2` of the registry size: the width of the global palette.
|
||||
pub global_bits: u8,
|
||||
}
|
||||
/// `getConfigurationForBitCount`.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
enum Config {
|
||||
Single,
|
||||
/// Palette of up to `1 << bits` entries, stored in `bits` bits.
|
||||
List(u8),
|
||||
Global,
|
||||
}
|
||||
impl Strategy {
|
||||
pub const fn blocks(global_bits: u8) -> Self {
|
||||
Self {
|
||||
kind: Kind::Blocks,
|
||||
global_bits,
|
||||
}
|
||||
}
|
||||
pub const fn biomes(global_bits: u8) -> Self {
|
||||
Self {
|
||||
kind: Kind::Biomes,
|
||||
global_bits,
|
||||
}
|
||||
}
|
||||
pub const fn entries(self) -> usize {
|
||||
match self.kind {
|
||||
Kind::Blocks => 4096,
|
||||
Kind::Biomes => 64,
|
||||
}
|
||||
}
|
||||
fn config(self, bits: u8) -> Config {
|
||||
match (self.kind, bits) {
|
||||
(_, 0) => Config::Single,
|
||||
(Kind::Blocks, 1..=4) => Config::List(4),
|
||||
(Kind::Blocks, 5..=8) => Config::List(bits),
|
||||
(Kind::Biomes, 1..=3) => Config::List(bits),
|
||||
_ => Config::Global,
|
||||
}
|
||||
}
|
||||
/// Whether a container written with leading byte `bits` lists its palette
|
||||
/// (all but the global one).
|
||||
pub fn wire_has_palette(self, bits: u8) -> bool {
|
||||
self.config(bits) != Config::Global
|
||||
}
|
||||
/// Bits of the storage a configuration keeps in memory.
|
||||
fn storage_bits(self, config: Config) -> u8 {
|
||||
match config {
|
||||
Config::Single => 0,
|
||||
Config::List(bits) => bits,
|
||||
Config::Global => self.global_bits,
|
||||
}
|
||||
}
|
||||
}
|
||||
/// `Mth.ceillog2`.
|
||||
pub fn ceil_log2(n: usize) -> u8 {
|
||||
if n <= 1 {
|
||||
0
|
||||
} else {
|
||||
(usize::BITS - (n - 1).leading_zeros()) as u8
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub enum PaletteError {
|
||||
/// A palette bigger than its configuration allows, or empty.
|
||||
BadPalette { len: usize },
|
||||
/// Storage with the wrong number of longs.
|
||||
BadStorage { expected: usize, actual: usize },
|
||||
/// A stored index past the palette.
|
||||
BadIndex { index: u32, palette: usize },
|
||||
/// The declared bit count does not match the palette.
|
||||
BadBits { declared: u8, expected: u8 },
|
||||
}
|
||||
impl std::fmt::Display for PaletteError {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "{self:?}")
|
||||
}
|
||||
}
|
||||
impl std::error::Error for PaletteError {}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
enum Palette {
|
||||
Single(u32),
|
||||
/// Linear and hash-map palettes alike: ids in insertion order.
|
||||
List(Vec<u32>),
|
||||
Global,
|
||||
}
|
||||
/// `n / per_word` for `n < 4096` by multiplication: `MAGIC[bits] = ceil(2^20 /
|
||||
/// (64 / bits))` is exact while `n * (64 / bits) < 2^20`.
|
||||
const MAGIC: [u32; 33] = {
|
||||
let mut table = [0; 33];
|
||||
let mut bits = 1;
|
||||
while bits <= 32 {
|
||||
let per = 64 / bits as u32;
|
||||
table[bits] = (1u32 << 20).div_ceil(per);
|
||||
bits += 1;
|
||||
}
|
||||
table
|
||||
};
|
||||
/// Words of `bits`-wide values: `entries.div_ceil(64 / bits)`.
|
||||
pub fn storage_len(bits: u8, entries: usize) -> usize {
|
||||
if bits == 0 {
|
||||
0
|
||||
} else {
|
||||
entries.div_ceil(64 / usize::from(bits))
|
||||
}
|
||||
}
|
||||
#[inline]
|
||||
fn locate(bits: u8, i: usize) -> (usize, u32) {
|
||||
if bits.is_power_of_two() {
|
||||
// 64 / bits is a power of two: shifts instead of the multiplication.
|
||||
let log = bits.trailing_zeros();
|
||||
let word = i >> (6 - log);
|
||||
(word, ((i & ((64 >> log) - 1)) as u32) << log)
|
||||
} else {
|
||||
let per = 64 / u32::from(bits);
|
||||
let word = (i as u32 * MAGIC[usize::from(bits)]) >> 20;
|
||||
(word as usize, (i as u32 - word * per) * u32::from(bits))
|
||||
}
|
||||
}
|
||||
#[inline]
|
||||
fn mask(bits: u8) -> u64 {
|
||||
(1u64 << bits) - 1
|
||||
}
|
||||
fn read(words: &[u64], bits: u8, i: usize) -> u32 {
|
||||
let (word, shift) = locate(bits, i);
|
||||
((words[word] >> shift) & mask(bits)) as u32
|
||||
}
|
||||
fn write(words: &mut [u64], bits: u8, i: usize, value: u32) {
|
||||
let (word, shift) = locate(bits, i);
|
||||
words[word] = (words[word] & !(mask(bits) << shift)) | (u64::from(value) << shift);
|
||||
}
|
||||
/// Words holding `ids` in `bits`-wide slots.
|
||||
fn pack_words(bits: u8, entries: usize, ids: impl Iterator<Item = u32>) -> Vec<u64> {
|
||||
let mut words = vec![0u64; storage_len(bits, entries)];
|
||||
for (i, id) in ids.enumerate() {
|
||||
write(&mut words, bits, i, id);
|
||||
}
|
||||
words
|
||||
}
|
||||
/// The saved form: distinct values in index order, and the packed indices
|
||||
/// when there is more than one value.
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub struct Packed {
|
||||
pub palette: Vec<u32>,
|
||||
/// Bits per index in `data` (0 when `data` is absent).
|
||||
pub bits: u8,
|
||||
pub data: Option<Vec<u64>>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct PalettedContainer {
|
||||
strategy: Strategy,
|
||||
bits: u8,
|
||||
palette: Palette,
|
||||
words: Vec<u64>,
|
||||
}
|
||||
impl PalettedContainer {
|
||||
/// `new PalettedContainer(initialValue, strategy)`.
|
||||
pub fn new(strategy: Strategy, value: u32) -> Self {
|
||||
Self {
|
||||
strategy,
|
||||
bits: 0,
|
||||
palette: Palette::Single(value),
|
||||
words: Vec::new(),
|
||||
}
|
||||
}
|
||||
pub fn strategy(&self) -> Strategy {
|
||||
self.strategy
|
||||
}
|
||||
/// Bits per stored index (`BitStorage.getBits`).
|
||||
pub fn bits(&self) -> u8 {
|
||||
self.bits
|
||||
}
|
||||
/// Entries in the palette (the global palette reports its bit capacity).
|
||||
pub fn palette_len(&self) -> usize {
|
||||
match &self.palette {
|
||||
Palette::Single(_) => 1,
|
||||
Palette::List(list) => list.len(),
|
||||
Palette::Global => 1 << self.bits.min(31),
|
||||
}
|
||||
}
|
||||
pub fn is_global(&self) -> bool {
|
||||
matches!(self.palette, Palette::Global)
|
||||
}
|
||||
#[inline]
|
||||
fn raw(&self, i: usize) -> usize {
|
||||
let (word, shift) = locate(self.bits, i);
|
||||
((self.words[word] >> shift) & mask(self.bits)) as usize
|
||||
}
|
||||
#[inline]
|
||||
pub fn get(&self, i: usize) -> u32 {
|
||||
match &self.palette {
|
||||
Palette::Single(value) => *value,
|
||||
Palette::List(list) => list[self.raw(i)],
|
||||
Palette::Global => self.raw(i) as u32,
|
||||
}
|
||||
}
|
||||
/// The value when every entry is equal, without the palette having to be
|
||||
/// minimal.
|
||||
pub fn uniform(&self) -> Option<u32> {
|
||||
match &self.palette {
|
||||
Palette::Single(value) => Some(*value),
|
||||
Palette::List(list) if list.len() == 1 => Some(list[0]),
|
||||
Palette::Global => self.all_equal(self.get(0)).then(|| self.get(0)),
|
||||
Palette::List(list) => {
|
||||
let first = list[self.raw(0)];
|
||||
self.all_equal(first).then_some(first)
|
||||
}
|
||||
}
|
||||
}
|
||||
/// Whether all entries equal `value`.
|
||||
pub fn is_uniform(&self, value: u32) -> bool {
|
||||
match &self.palette {
|
||||
Palette::Single(v) => *v == value,
|
||||
Palette::List(list) => match list.iter().position(|&v| v == value) {
|
||||
None => false,
|
||||
Some(_) if list.len() == 1 => true,
|
||||
Some(_) => self.all_equal(value),
|
||||
},
|
||||
Palette::Global => self.all_equal(value),
|
||||
}
|
||||
}
|
||||
fn all_equal(&self, value: u32) -> bool {
|
||||
let id = match &self.palette {
|
||||
Palette::Single(v) => return *v == value,
|
||||
Palette::List(list) => match list.iter().position(|&v| v == value) {
|
||||
Some(id) => id as u64,
|
||||
None => return false,
|
||||
},
|
||||
Palette::Global => u64::from(value),
|
||||
};
|
||||
let bits = usize::from(self.bits);
|
||||
let per = 64 / bits;
|
||||
let pattern = (0..per).fold(0u64, |word, slot| word | id << (slot * bits));
|
||||
let entries = self.strategy.entries();
|
||||
let full = entries / per;
|
||||
if self.words[..full].iter().any(|&w| w != pattern) {
|
||||
return false;
|
||||
}
|
||||
let tail = entries - full * per;
|
||||
tail == 0 || {
|
||||
let used = (1u64 << (tail * bits)) - 1;
|
||||
self.words[full] & used == pattern & used
|
||||
}
|
||||
}
|
||||
/// `getAndSet`: stores `value` and returns the previous one.
|
||||
pub fn set(&mut self, i: usize, value: u32) -> u32 {
|
||||
let id = self.id_for(value);
|
||||
match &self.palette {
|
||||
Palette::Single(old) => *old,
|
||||
Palette::List(list) => {
|
||||
let old = read(&self.words, self.bits, i) as usize;
|
||||
write(&mut self.words, self.bits, i, id as u32);
|
||||
list[old]
|
||||
}
|
||||
Palette::Global => {
|
||||
let old = read(&self.words, self.bits, i);
|
||||
write(&mut self.words, self.bits, i, id as u32);
|
||||
old
|
||||
}
|
||||
}
|
||||
}
|
||||
/// `Palette.idFor`, growing the container when the palette is full.
|
||||
fn id_for(&mut self, value: u32) -> usize {
|
||||
match &mut self.palette {
|
||||
Palette::Single(v) if *v == value => 0,
|
||||
Palette::Single(_) => self.resize(1, value),
|
||||
Palette::List(list) => {
|
||||
if let Some(id) = list.iter().position(|&v| v == value) {
|
||||
id
|
||||
} else if list.len() < 1 << self.bits {
|
||||
list.push(value);
|
||||
list.len() - 1
|
||||
} else {
|
||||
self.resize(self.bits + 1, value)
|
||||
}
|
||||
}
|
||||
Palette::Global => value as usize,
|
||||
}
|
||||
}
|
||||
/// `PalettedContainer.onResize`: rebuild with the configuration for
|
||||
/// `bits`, then add `last` and return its id.
|
||||
fn resize(&mut self, bits: u8, last: u32) -> usize {
|
||||
let strategy = self.strategy;
|
||||
let config = strategy.config(bits);
|
||||
let storage_bits = strategy.storage_bits(config);
|
||||
let entries = strategy.entries();
|
||||
let mut palette = match config {
|
||||
Config::Single => Palette::Single(last),
|
||||
Config::List(_) => Palette::List(Vec::new()),
|
||||
Config::Global => Palette::Global,
|
||||
};
|
||||
let mut words = vec![0u64; storage_len(storage_bits, entries)];
|
||||
let id_in = |palette: &mut Palette, value: u32| -> u32 {
|
||||
match palette {
|
||||
Palette::Single(_) => 0,
|
||||
Palette::List(list) => match list.iter().position(|&v| v == value) {
|
||||
Some(id) => id as u32,
|
||||
None => {
|
||||
list.push(value);
|
||||
list.len() as u32 - 1
|
||||
}
|
||||
},
|
||||
Palette::Global => value,
|
||||
}
|
||||
};
|
||||
if let Palette::Single(only) = self.palette {
|
||||
let id = id_in(&mut palette, only);
|
||||
if storage_bits > 0 && id != 0 {
|
||||
for i in 0..entries {
|
||||
write(&mut words, storage_bits, i, id);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
let (mut last_old, mut new_id) = (u32::MAX, 0);
|
||||
for i in 0..entries {
|
||||
let old = read(&self.words, self.bits, i);
|
||||
if old != last_old {
|
||||
last_old = old;
|
||||
let value = match &self.palette {
|
||||
Palette::List(list) => list[old as usize],
|
||||
_ => old,
|
||||
};
|
||||
new_id = id_in(&mut palette, value);
|
||||
}
|
||||
if storage_bits > 0 {
|
||||
write(&mut words, storage_bits, i, new_id);
|
||||
}
|
||||
}
|
||||
}
|
||||
let id = id_in(&mut palette, last);
|
||||
self.palette = palette;
|
||||
self.bits = storage_bits;
|
||||
self.words = words;
|
||||
id as usize
|
||||
}
|
||||
/// All values in index order.
|
||||
pub fn unpack(&self, out: &mut [u32]) {
|
||||
assert_eq!(out.len(), self.strategy.entries());
|
||||
match &self.palette {
|
||||
Palette::Single(value) => out.fill(*value),
|
||||
palette => {
|
||||
let bits = usize::from(self.bits);
|
||||
let per = 64 / bits;
|
||||
let m = mask(self.bits);
|
||||
for (chunk, &word) in out.chunks_mut(per).zip(&self.words) {
|
||||
for (slot, cell) in chunk.iter_mut().enumerate() {
|
||||
let id = ((word >> (slot * bits)) & m) as u32;
|
||||
*cell = match palette {
|
||||
Palette::List(list) => list[id as usize],
|
||||
_ => id,
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
/// Block ids as 16-bit values (callers whose registry fits in 16 bits).
|
||||
pub fn unpack_u16(&self, out: &mut [u16; 4096]) {
|
||||
debug_assert_eq!(self.strategy.entries(), 4096);
|
||||
match &self.palette {
|
||||
Palette::Single(value) => out.fill(*value as u16),
|
||||
palette => {
|
||||
let bits = usize::from(self.bits);
|
||||
let per = 64 / bits;
|
||||
let m = mask(self.bits);
|
||||
for (chunk, &word) in out.chunks_mut(per).zip(&self.words) {
|
||||
for (slot, cell) in chunk.iter_mut().enumerate() {
|
||||
let id = ((word >> (slot * bits)) & m) as usize;
|
||||
*cell = match palette {
|
||||
Palette::List(list) => list[id] as u16,
|
||||
_ => id as u16,
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
/// A container holding `values` in the smallest configuration for their
|
||||
/// distinct count, with palette ids in first-occurrence order, like a
|
||||
/// container unpacked from disk.
|
||||
pub fn from_values(strategy: Strategy, values: &[u32]) -> Self {
|
||||
assert_eq!(values.len(), strategy.entries());
|
||||
let mut palette: Vec<u32> = Vec::new();
|
||||
let mut small = true;
|
||||
let mut lookup: std::collections::HashMap<u32, u32> = std::collections::HashMap::new();
|
||||
let mut ids = Vec::with_capacity(values.len());
|
||||
for &value in values {
|
||||
let id = if small {
|
||||
match palette.iter().position(|&v| v == value) {
|
||||
Some(id) => id as u32,
|
||||
None => {
|
||||
palette.push(value);
|
||||
if palette.len() > 16 {
|
||||
small = false;
|
||||
lookup.extend(palette.iter().enumerate().map(|(i, &v)| (v, i as u32)));
|
||||
}
|
||||
palette.len() as u32 - 1
|
||||
}
|
||||
}
|
||||
} else {
|
||||
*lookup.entry(value).or_insert_with(|| {
|
||||
palette.push(value);
|
||||
palette.len() as u32 - 1
|
||||
})
|
||||
};
|
||||
ids.push(id);
|
||||
}
|
||||
Self::build(strategy, palette, &ids)
|
||||
}
|
||||
/// A container over a distinct-value `palette` and an id per entry.
|
||||
fn build(strategy: Strategy, palette: Vec<u32>, ids: &[u32]) -> Self {
|
||||
let config = strategy.config(ceil_log2(palette.len()));
|
||||
match config {
|
||||
Config::Single => Self::new(strategy, palette[0]),
|
||||
Config::List(bits) => Self {
|
||||
strategy,
|
||||
bits,
|
||||
words: pack_words(bits, ids.len(), ids.iter().copied()),
|
||||
palette: Palette::List(palette),
|
||||
},
|
||||
Config::Global => {
|
||||
let bits = strategy.global_bits;
|
||||
Self {
|
||||
strategy,
|
||||
bits,
|
||||
words: pack_words(bits, ids.len(), ids.iter().map(|&id| palette[id as usize])),
|
||||
palette: Palette::Global,
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
/// `PalettedContainer.read`: the container as sent on the wire. `bits` is
|
||||
/// the leading byte, `palette` the VarInt entries (a single one for
|
||||
/// `bits == 0`, none for the global palette) and `words` the longs.
|
||||
pub fn from_wire(
|
||||
strategy: Strategy,
|
||||
bits: u8,
|
||||
palette: Vec<u32>,
|
||||
words: Vec<u64>,
|
||||
) -> Result<Self, PaletteError> {
|
||||
let config = strategy.config(bits);
|
||||
let storage_bits = strategy.storage_bits(config);
|
||||
let expected = storage_len(storage_bits, strategy.entries());
|
||||
if words.len() != expected {
|
||||
return Err(PaletteError::BadStorage {
|
||||
expected,
|
||||
actual: words.len(),
|
||||
});
|
||||
}
|
||||
match config {
|
||||
Config::Single => {
|
||||
if palette.len() != 1 {
|
||||
return Err(PaletteError::BadPalette { len: palette.len() });
|
||||
}
|
||||
Ok(Self::new(strategy, palette[0]))
|
||||
}
|
||||
Config::List(bits) => {
|
||||
if palette.is_empty() || palette.len() > 1 << bits {
|
||||
return Err(PaletteError::BadPalette { len: palette.len() });
|
||||
}
|
||||
let container = Self {
|
||||
strategy,
|
||||
bits,
|
||||
palette: Palette::List(palette),
|
||||
words,
|
||||
};
|
||||
container.check_indices()?;
|
||||
Ok(container)
|
||||
}
|
||||
Config::Global => Ok(Self {
|
||||
strategy,
|
||||
bits: storage_bits,
|
||||
palette: Palette::Global,
|
||||
words,
|
||||
}),
|
||||
}
|
||||
}
|
||||
fn check_indices(&self) -> Result<(), PaletteError> {
|
||||
let Palette::List(list) = &self.palette else {
|
||||
return Ok(());
|
||||
};
|
||||
if list.len() == 1 << self.bits {
|
||||
return Ok(());
|
||||
}
|
||||
for i in 0..self.strategy.entries() {
|
||||
let index = read(&self.words, self.bits, i);
|
||||
if index as usize >= list.len() {
|
||||
return Err(PaletteError::BadIndex {
|
||||
index,
|
||||
palette: list.len(),
|
||||
});
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
/// `PalettedContainer.write`: the leading bits byte, the palette entries
|
||||
/// (none for the global palette) and the raw longs, exactly as resident.
|
||||
pub fn to_wire(&self) -> (u8, Vec<u32>, &[u64]) {
|
||||
let palette = match &self.palette {
|
||||
Palette::Single(value) => vec![*value],
|
||||
Palette::List(list) => list.clone(),
|
||||
Palette::Global => Vec::new(),
|
||||
};
|
||||
(self.bits, palette, &self.words)
|
||||
}
|
||||
/// `PalettedContainer.pack`: distinct values in index order, indices
|
||||
/// re-encoded for the saved bit width.
|
||||
pub fn pack(&self) -> Packed {
|
||||
let entries = self.strategy.entries();
|
||||
let mut palette: Vec<u32> = Vec::new();
|
||||
let mut ids = Vec::with_capacity(entries);
|
||||
let mut values = vec![0u32; entries];
|
||||
self.unpack(&mut values);
|
||||
let mut lookup: std::collections::HashMap<u32, u32> = std::collections::HashMap::new();
|
||||
for &value in &values {
|
||||
let id = *lookup.entry(value).or_insert_with(|| {
|
||||
palette.push(value);
|
||||
palette.len() as u32 - 1
|
||||
});
|
||||
ids.push(id);
|
||||
}
|
||||
let bits = match self.strategy.config(ceil_log2(palette.len())) {
|
||||
Config::Single => 0,
|
||||
Config::List(bits) => bits,
|
||||
Config::Global => ceil_log2(palette.len()),
|
||||
};
|
||||
let data = (bits != 0).then(|| pack_words(bits, entries, ids.iter().copied()));
|
||||
Packed {
|
||||
palette,
|
||||
bits,
|
||||
data,
|
||||
}
|
||||
}
|
||||
/// `PalettedContainer.unpack`: a container from the saved form.
|
||||
pub fn from_packed(
|
||||
strategy: Strategy,
|
||||
palette: Vec<u32>,
|
||||
data: Option<&[u64]>,
|
||||
) -> Result<Self, PaletteError> {
|
||||
if palette.is_empty() {
|
||||
return Err(PaletteError::BadPalette { len: 0 });
|
||||
}
|
||||
let config = strategy.config(ceil_log2(palette.len()));
|
||||
let entries = strategy.entries();
|
||||
let disc_bits = match config {
|
||||
Config::Single => return Ok(Self::new(strategy, palette[0])),
|
||||
Config::List(bits) => bits,
|
||||
Config::Global => ceil_log2(palette.len()),
|
||||
};
|
||||
let data = data.ok_or(PaletteError::BadStorage {
|
||||
expected: storage_len(disc_bits, entries),
|
||||
actual: 0,
|
||||
})?;
|
||||
let expected = storage_len(disc_bits, entries);
|
||||
if data.len() != expected {
|
||||
return Err(PaletteError::BadStorage {
|
||||
expected,
|
||||
actual: data.len(),
|
||||
});
|
||||
}
|
||||
let mut ids = Vec::with_capacity(entries);
|
||||
for i in 0..entries {
|
||||
let index = read(data, disc_bits, i);
|
||||
if index as usize >= palette.len() {
|
||||
return Err(PaletteError::BadIndex {
|
||||
index,
|
||||
palette: palette.len(),
|
||||
});
|
||||
}
|
||||
ids.push(index);
|
||||
}
|
||||
Ok(match config {
|
||||
Config::List(bits) => Self {
|
||||
strategy,
|
||||
bits,
|
||||
words: data.to_vec(),
|
||||
palette: Palette::List(palette),
|
||||
},
|
||||
_ => Self::build(strategy, palette, &ids),
|
||||
})
|
||||
}
|
||||
/// Whether any palette entry satisfies `predicate` (`maybeHas`): entries
|
||||
/// no longer used by any position still count.
|
||||
pub fn maybe_has(&self, mut predicate: impl FnMut(u32) -> bool) -> bool {
|
||||
match &self.palette {
|
||||
Palette::Single(value) => predicate(*value),
|
||||
Palette::List(list) => list.iter().any(|&v| predicate(v)),
|
||||
Palette::Global => (0..1u32 << self.bits.min(31)).any(predicate),
|
||||
}
|
||||
}
|
||||
/// Heap bytes held by the palette and the storage.
|
||||
pub fn heap_bytes(&self) -> usize {
|
||||
self.words.capacity() * 8
|
||||
+ match &self.palette {
|
||||
Palette::List(list) => list.capacity() * 4,
|
||||
_ => 0,
|
||||
}
|
||||
}
|
||||
}
|
||||
impl PartialEq for PalettedContainer {
|
||||
/// Equal when every entry is.
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
let entries = self.strategy.entries();
|
||||
entries == other.strategy.entries() && (0..entries).all(|i| self.get(i) == other.get(i))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
const BLOCKS: Strategy = Strategy::blocks(16);
|
||||
const BIOMES: Strategy = Strategy::biomes(7);
|
||||
fn rng(state: &mut u64) -> u64 {
|
||||
*state ^= *state << 13;
|
||||
*state ^= *state >> 7;
|
||||
*state ^= *state << 17;
|
||||
*state
|
||||
}
|
||||
#[test]
|
||||
fn grows_through_vanilla_widths() {
|
||||
let mut c = PalettedContainer::new(BLOCKS, 0);
|
||||
assert_eq!((c.bits(), c.palette_len()), (0, 1));
|
||||
let mut expected = Vec::new();
|
||||
for n in 1..=300u32 {
|
||||
c.set(n as usize, n);
|
||||
expected.push(match n + 1 {
|
||||
2..=16 => 4,
|
||||
17..=32 => 5,
|
||||
33..=64 => 6,
|
||||
65..=128 => 7,
|
||||
129..=256 => 8,
|
||||
_ => 16,
|
||||
});
|
||||
assert_eq!(c.bits(), *expected.last().unwrap(), "after {n} values");
|
||||
}
|
||||
assert!(c.is_global());
|
||||
for n in 1..=300u32 {
|
||||
assert_eq!(c.get(n as usize), n);
|
||||
}
|
||||
let mut b = PalettedContainer::new(BIOMES, 3);
|
||||
for n in 1..=9u32 {
|
||||
b.set(n as usize, n + 3);
|
||||
let want = match n + 1 {
|
||||
2 => 1,
|
||||
3..=4 => 2,
|
||||
5..=8 => 3,
|
||||
_ => 7,
|
||||
};
|
||||
assert_eq!(b.bits(), want, "after {n} biomes");
|
||||
}
|
||||
}
|
||||
#[test]
|
||||
fn matches_a_plain_array_under_random_edits() {
|
||||
for (strategy, distinct) in [(BLOCKS, 700u64), (BLOCKS, 12), (BIOMES, 9), (BIOMES, 3)] {
|
||||
let entries = strategy.entries();
|
||||
let mut state = 0x1234_5678_9ABC_DEF1u64;
|
||||
let mut c = PalettedContainer::new(strategy, 5);
|
||||
let mut plain = vec![5u32; entries];
|
||||
for _ in 0..20_000 {
|
||||
let r = rng(&mut state);
|
||||
let i = (r % entries as u64) as usize;
|
||||
let v = ((r >> 20) % distinct) as u32 * 7;
|
||||
assert_eq!(c.set(i, v), plain[i]);
|
||||
plain[i] = v;
|
||||
}
|
||||
let mut out = vec![0; entries];
|
||||
c.unpack(&mut out);
|
||||
assert_eq!(out, plain);
|
||||
for (i, &v) in plain.iter().enumerate() {
|
||||
assert_eq!(c.get(i), v);
|
||||
}
|
||||
let packed = c.pack();
|
||||
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();
|
||||
assert_eq!(wire, c);
|
||||
}
|
||||
}
|
||||
#[test]
|
||||
fn uniform_detection_ignores_stale_palette_entries() {
|
||||
let mut c = PalettedContainer::new(BLOCKS, 0);
|
||||
c.set(10, 3);
|
||||
assert!(!c.is_uniform(0));
|
||||
c.set(10, 0);
|
||||
assert!(c.palette_len() == 2 && c.is_uniform(0) && c.uniform() == Some(0));
|
||||
assert!(!c.is_uniform(3));
|
||||
for i in 0..4096 {
|
||||
c.set(i, 3);
|
||||
}
|
||||
assert_eq!(c.uniform(), Some(3));
|
||||
}
|
||||
#[test]
|
||||
fn rejects_malformed_wire_data() {
|
||||
assert!(PalettedContainer::from_wire(BLOCKS, 4, vec![1, 2], vec![0; 255]).is_err());
|
||||
assert!(PalettedContainer::from_wire(BLOCKS, 4, vec![1, 2], vec![0x3333; 256]).is_err());
|
||||
assert!(PalettedContainer::from_wire(BLOCKS, 4, vec![], vec![0; 256]).is_err());
|
||||
assert!(PalettedContainer::from_wire(BLOCKS, 0, vec![1, 2], vec![]).is_err());
|
||||
assert!(PalettedContainer::from_wire(BIOMES, 2, vec![1, 2, 3, 4, 5], vec![0; 4]).is_err());
|
||||
assert!(PalettedContainer::from_wire(BLOCKS, 0, vec![9], vec![]).is_ok());
|
||||
}
|
||||
#[test]
|
||||
fn division_magic_is_exact() {
|
||||
for bits in 1..=32u8 {
|
||||
let per = 64 / usize::from(bits);
|
||||
for i in 0..4096usize {
|
||||
assert_eq!(locate(bits, i).0, i / per, "bits {bits} index {i}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+29
-8
@@ -24,7 +24,8 @@ emit for the same scenes.
|
||||
loads the prepared resources (`Assets`, `BlockFacts`, `Biomes`,
|
||||
`load_visibility`, `MeshTables`), builds the same blocks and light in real
|
||||
client `Chunk`s (a 3x3 chunk region), sets every biome's colours to the
|
||||
scene constants and runs `Mesher::mesh`.
|
||||
scene constants (or, for the biome-blend scenes, keeps the real biome
|
||||
definitions and fills the chunks' quart biome arrays) and runs `Mesher::mesh`.
|
||||
- **Comparison**: quads are matched as multisets per scene, keyed by sprite,
|
||||
render-layer class (translucent vs. solid/cutout) and geometry rounded to
|
||||
1/1000 after binary32 normalisation, with a winding-preserving rotation.
|
||||
@@ -37,7 +38,7 @@ emit for the same scenes.
|
||||
overlay classes (`HalfTransparentBlock`/`LeavesBlock`), tint source counts
|
||||
and `getOffset` at five columns.
|
||||
|
||||
The corpus (94 sections, ~842k quads, ~206k of them fluid quads) has full
|
||||
The corpus (112 sections, ~1.0M quads, ~206k of them fluid quads) has full
|
||||
cubes in open air, corners, cavities and overhangs; slabs, stairs, walls,
|
||||
fences, panes, bars, torches, flowers and grasses with XZ/XYZ offsets, tall
|
||||
plants, tinted blocks (grass, leaves, stems, redstone, lily pad, petals, leaf
|
||||
@@ -50,6 +51,27 @@ sculk sensor, glow lichen); Nether shading; every block's default state; every
|
||||
`RenderShape.MODEL` state of the registry on a sparse grid; and dense random
|
||||
scenes mixing all model states with fluids under propagated or random light.
|
||||
|
||||
Biome tint blending is compared against the client's real
|
||||
`ClientLevel.calculateBlockTint`: the oracle builds a `BiomeManager` (the
|
||||
eight-corner zoom with a random hashed seed) over a fixed noise-biome map of
|
||||
real `Biome` objects from the registry bootstrap (colormap sampling, colour
|
||||
overrides, the dark-forest and swamp grass modifiers), initialises
|
||||
`GrassColor`/`FoliageColor`/`DryFoliageColor` from the jar's colormaps, and
|
||||
averages the resolver over the blend radius at each block's own height. The
|
||||
`biome_blend_r*` scenes (radius 0, 1, 2, 3, 5, 7) put tinted blocks (grass,
|
||||
leaves, water, leaf litter, cane, ferns, vines) at four heights on every
|
||||
column of a Voronoi patchwork of 16 common biomes that shifts with height;
|
||||
the `biome_each_*` scenes cover all 67 biomes. Option scenes (`options_*`)
|
||||
cover flat lighting (`ao` off), opaque leaves (`cutoutLeaves` off, which
|
||||
`SectionCompiler` draws in the solid layer and `LeavesBlock.skipRendering`
|
||||
joins) and both together.
|
||||
|
||||
With `biomeBlendRadius` 0 the client takes the resolver's colour as is. Biome
|
||||
colours given as plain RGB (all water colours, most grass/foliage overrides)
|
||||
have alpha 0 there, so the client's tinted vertices come out fully
|
||||
transparent, which the terrain shader would discard. Horizon keeps them
|
||||
opaque and the gate compares RGB for radius-0 scenes.
|
||||
|
||||
## Running
|
||||
|
||||
```sh
|
||||
@@ -112,15 +134,14 @@ gate.
|
||||
|
||||
## What remains different or untested
|
||||
|
||||
- Biome tint blending: vanilla blends `BiomeManager`'s zoomed biome at each
|
||||
block's own height; Horizon blends its quart biome cells at the section's
|
||||
mid-height. The gate uses one constant colour per resolver, so this is not
|
||||
compared.
|
||||
- Changing `biomeBlendRadius`, `ao` or `cutoutLeaves` in the options does not
|
||||
remesh resident sections; new and edited sections use the new values.
|
||||
- Model variant seeds: `DoublePlantBlock`, `DoorBlock` and `AbstractBedBlock`
|
||||
override `getSeed`; Horizon seeds every block from its own position. No
|
||||
vanilla block among those has weighted variants, so output is unaffected.
|
||||
- Settings: only smooth lighting on and cutout ("fancy") leaves are covered;
|
||||
Horizon has no flat-lighting or opaque-leaves modes.
|
||||
- Opaque leaves: the vertex layer carries a flag (`OPAQUE_LAYER`) that makes
|
||||
the terrain shader ignore the texture's alpha; the shader side is not
|
||||
compared (GPU pixels).
|
||||
- Layers: vanilla's solid and cutout layers are compared as one class because
|
||||
Horizon draws both in its opaque pass.
|
||||
- Not compared: block entities and their renderers, animated sprite frames,
|
||||
|
||||
@@ -0,0 +1,103 @@
|
||||
package dev.horizonlab.parity;
|
||||
|
||||
import com.google.gson.*;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.buffer.Unpooled;
|
||||
import java.nio.file.*;
|
||||
import java.util.*;
|
||||
import java.util.stream.LongStream;
|
||||
import net.minecraft.core.IdMap;
|
||||
import net.minecraft.network.FriendlyByteBuf;
|
||||
import net.minecraft.world.level.chunk.PalettedContainer;
|
||||
import net.minecraft.world.level.chunk.PalettedContainerRO;
|
||||
import net.minecraft.world.level.chunk.Strategy;
|
||||
|
||||
/** Runs the real PalettedContainer (single-value, linear, hash-map and global palettes, resize,
|
||||
* network write/read, pack/unpack) over scripted edits and records hashes of everything
|
||||
* observable: set results, the serialized container after every few edits, the saved form and
|
||||
* the containers read back from both. */
|
||||
public final class VanillaPalettedContainerOracle {
|
||||
private static final Gson JSON = new GsonBuilder().serializeNulls().create();
|
||||
/** The palettes compare entries by identity, as registry objects allow: intern the boxes. */
|
||||
private static final Map<Integer, Integer> INTERNED = new HashMap<>();
|
||||
private static Integer intern(int value) { return INTERNED.computeIfAbsent(value, v -> new Integer(v)); }
|
||||
private static final class Ids implements IdMap<Integer> {
|
||||
final int size;
|
||||
Ids(int size) { this.size = size; }
|
||||
public int getId(Integer value) { return value < size ? value : -1; }
|
||||
public Integer byId(int id) { return id >= 0 && id < size ? intern(id) : null; }
|
||||
public int size() { return size; }
|
||||
public Iterator<Integer> iterator() { return java.util.stream.IntStream.range(0, size).boxed().iterator(); }
|
||||
}
|
||||
private static long fnv(long h, int b) { return (h ^ (b & 0xFF)) * 0x100000001b3L; }
|
||||
private static final long BASIS = 0xcbf29ce484222325L;
|
||||
private static long hashBytes(byte[] bytes) {
|
||||
long h = BASIS;
|
||||
for (byte b : bytes) h = fnv(h, b);
|
||||
return h;
|
||||
}
|
||||
private static long hashValues(PalettedContainer<Integer> c, int bits) {
|
||||
long h = BASIS;
|
||||
int n = 1 << (3 * bits);
|
||||
for (int i = 0; i < n; i++) {
|
||||
int v = c.get(i & (n - 1) & ((1 << bits) - 1), i >> (2 * bits), (i >> bits) & ((1 << bits) - 1));
|
||||
for (int k = 0; k < 4; k++) h = fnv(h, v >> (8 * k));
|
||||
}
|
||||
return h;
|
||||
}
|
||||
private static byte[] write(PalettedContainer<Integer> c) {
|
||||
var buffer = new FriendlyByteBuf(Unpooled.buffer());
|
||||
c.write(buffer);
|
||||
byte[] bytes = new byte[buffer.readableBytes()];
|
||||
buffer.readBytes(bytes);
|
||||
return bytes;
|
||||
}
|
||||
private static String wire(PalettedContainer<Integer> c) {
|
||||
byte[] bytes = write(c);
|
||||
return bytes.length + ":" + hashBytes(bytes);
|
||||
}
|
||||
/** Input line: name kind initial every i v i v ...; output: one line of fields. */
|
||||
private static String scenario(String line) {
|
||||
var t = line.trim().split(" ");
|
||||
boolean blocks = t[1].equals("blocks");
|
||||
int bits = blocks ? 4 : 2;
|
||||
Strategy<Integer> strategy = blocks ? Strategy.createForBlockStates(new Ids(35723)) : Strategy.createForBiomes(new Ids(67));
|
||||
var c = new PalettedContainer<>(intern(Integer.parseInt(t[2])), strategy);
|
||||
int every = Integer.parseInt(t[3]);
|
||||
var checkpoints = new StringJoiner(",");
|
||||
long oldsHash = BASIS;
|
||||
int n = 0, mask = (1 << bits) - 1;
|
||||
for (int k = 4; k + 1 < t.length; k += 2) {
|
||||
int i = Integer.parseInt(t[k]);
|
||||
int old = c.getAndSetUnchecked(i & mask, i >> (2 * bits), (i >> bits) & mask, intern(Integer.parseInt(t[k + 1])));
|
||||
for (int j = 0; j < 4; j++) oldsHash = fnv(oldsHash, old >> (8 * j));
|
||||
if (++n % every == 0) checkpoints.add(wire(c));
|
||||
}
|
||||
var out = new StringJoiner(" ");
|
||||
out.add(t[0]).add(Long.toString(oldsHash)).add("cp=" + checkpoints).add("bits=" + c.bitsPerEntry());
|
||||
out.add("final=" + wire(c)).add("values=" + hashValues(c, bits));
|
||||
var read = new PalettedContainer<>(intern(0), strategy);
|
||||
read.read(new FriendlyByteBuf(Unpooled.wrappedBuffer(write(c))));
|
||||
out.add("read=" + hashValues(read, bits) + "/" + wire(read));
|
||||
var packed = c.pack(strategy);
|
||||
long[] storage = packed.storage().map(LongStream::toArray).orElse(null);
|
||||
long storageHash = BASIS;
|
||||
if (storage != null) for (long word : storage) for (int j = 0; j < 8; j++) storageHash = fnv(storageHash, (int) (word >> (8 * j)));
|
||||
var palette = new StringJoiner(",");
|
||||
for (int v : packed.paletteEntries()) palette.add(Integer.toString(v));
|
||||
out.add("packed=" + packed.bitsPerEntry() + "/" + storageHash + "/" + palette);
|
||||
var loaded = PalettedContainer.unpack(strategy, new PalettedContainerRO.PackedData<>(packed.paletteEntries(),
|
||||
Optional.ofNullable(storage).map(LongStream::of), -1)).getOrThrow();
|
||||
out.add("loaded=" + hashValues(loaded, bits) + "/" + wire(loaded));
|
||||
return out.toString();
|
||||
}
|
||||
public static void main(String[] args) throws Exception {
|
||||
net.minecraft.SharedConstants.tryDetectVersion();
|
||||
try (var input = Files.newBufferedReader(Path.of(args[0])); var output = Files.newBufferedWriter(Path.of(args[1]))) {
|
||||
for (String line; (line = input.readLine()) != null; ) {
|
||||
output.write(scenario(line));
|
||||
output.newLine();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -10,6 +10,7 @@ import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.*;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.function.Function;
|
||||
import net.minecraft.client.color.block.BlockColors;
|
||||
import net.minecraft.client.renderer.BiomeColors;
|
||||
import net.minecraft.client.renderer.block.*;
|
||||
@@ -29,6 +30,14 @@ import net.minecraft.world.level.CardinalLighting;
|
||||
import net.minecraft.world.level.ColorResolver;
|
||||
import net.minecraft.world.level.EmptyBlockGetter;
|
||||
import net.minecraft.world.level.LightLayer;
|
||||
import net.minecraft.world.level.DryFoliageColor;
|
||||
import net.minecraft.world.level.FoliageColor;
|
||||
import net.minecraft.world.level.GrassColor;
|
||||
import net.minecraft.world.level.biome.Biome;
|
||||
import net.minecraft.world.level.biome.BiomeManager;
|
||||
import net.minecraft.core.registries.Registries;
|
||||
import net.minecraft.resources.ResourceKey;
|
||||
import net.minecraft.util.ARGB;
|
||||
import net.minecraft.world.level.block.*;
|
||||
import net.minecraft.world.level.block.entity.BlockEntity;
|
||||
import net.minecraft.world.level.block.state.BlockState;
|
||||
@@ -100,6 +109,12 @@ public final class VanillaSectionMeshOracle {
|
||||
return (T) value;
|
||||
}
|
||||
}
|
||||
private static int[] colormap(String name) throws IOException {
|
||||
try (var input = VanillaSectionMeshOracle.class.getResourceAsStream("/assets/minecraft/textures/colormap/" + name + ".png");
|
||||
var image = NativeImage.read(input)) {
|
||||
return image.makePixelArray();
|
||||
}
|
||||
}
|
||||
private static String resource(String path) throws IOException {
|
||||
try (var input = VanillaSectionMeshOracle.class.getResourceAsStream(path)) {
|
||||
return input == null ? null : new String(input.readAllBytes(), StandardCharsets.UTF_8);
|
||||
@@ -139,12 +154,47 @@ public final class VanillaSectionMeshOracle {
|
||||
return baked.computeIfAbsent(state, s -> Objects.requireNonNull(roots.get(s), () -> "no model for " + s).bake(s, baker));
|
||||
}
|
||||
}
|
||||
/** ClientLevel.calculateBlockTint over a BiomeManager with a fixed noise-biome map:
|
||||
* quart cells listed in the scene, a default elsewhere in the 3x3 chunk region and
|
||||
* plains outside it (chunks that are not loaded). */
|
||||
private static final class TintBiomes {
|
||||
final BiomeManager manager; final int radius;
|
||||
final Map<Long, Holder<Biome>> cells = new HashMap<>();
|
||||
final Holder<Biome> dflt, plains;
|
||||
TintBiomes(JsonObject o, HolderLookup.RegistryLookup<Biome> lookup) {
|
||||
radius = o.get("radius").getAsInt();
|
||||
Function<String, Holder<Biome>> named = name -> lookup.getOrThrow(ResourceKey.create(Registries.BIOME, Identifier.parse(name)));
|
||||
plains = named.apply("minecraft:plains");
|
||||
dflt = named.apply(o.get("default").getAsString());
|
||||
for (var item : o.getAsJsonArray("cells")) {
|
||||
var c = item.getAsJsonArray();
|
||||
cells.put(BlockPos.asLong(c.get(0).getAsInt(), c.get(1).getAsInt(), c.get(2).getAsInt()), named.apply(c.get(3).getAsString()));
|
||||
}
|
||||
manager = new BiomeManager((qx, qy, qz) -> {
|
||||
if (qx < -4 || qx >= 8 || qz < -4 || qz >= 8) return plains;
|
||||
return cells.getOrDefault(BlockPos.asLong(qx, Math.max(-16, Math.min(79, qy)), qz), dflt);
|
||||
}, o.get("seed").getAsLong());
|
||||
}
|
||||
int tint(BlockPos pos, ColorResolver resolver) {
|
||||
if (radius == 0) return resolver.getColor(manager.getBiome(pos).value(), pos.getX(), pos.getZ());
|
||||
int count = (radius * 2 + 1) * (radius * 2 + 1);
|
||||
int red = 0, green = 0, blue = 0;
|
||||
var cursor = new Cursor3D(pos.getX() - radius, pos.getY(), pos.getZ() - radius, pos.getX() + radius, pos.getY(), pos.getZ() + radius);
|
||||
var next = new BlockPos.MutableBlockPos();
|
||||
while (cursor.advance()) {
|
||||
next.set(cursor.nextX(), cursor.nextY(), cursor.nextZ());
|
||||
int color = resolver.getColor(manager.getBiome(next).value(), next.getX(), next.getZ());
|
||||
red += ARGB.red(color); green += ARGB.green(color); blue += ARGB.blue(color);
|
||||
}
|
||||
return ARGB.color(red / count, green / count, blue / count);
|
||||
}
|
||||
}
|
||||
private static long key(int x, int y, int z) { return BlockPos.asLong(x, y, z); }
|
||||
/** A fixed region: listed blocks over air, listed light over a default. */
|
||||
private static final class Scene implements BlockAndTintGetter {
|
||||
final Map<Long, BlockState> blocks = new HashMap<>();
|
||||
final Map<Long, int[]> light = new HashMap<>();
|
||||
int[] defaultLight; boolean nether; int grass, foliage, dry, water;
|
||||
int[] defaultLight; boolean nether; int grass, foliage, dry, water; TintBiomes biomes;
|
||||
public CardinalLighting cardinalLighting() { return nether ? CardinalLighting.NETHER : CardinalLighting.DEFAULT; }
|
||||
public LevelLightEngine getLightEngine() { return LevelLightEngine.EMPTY; }
|
||||
public int getBrightness(LightLayer layer, BlockPos pos) {
|
||||
@@ -152,6 +202,7 @@ public final class VanillaSectionMeshOracle {
|
||||
return layer == LightLayer.SKY ? value[0] : value[1];
|
||||
}
|
||||
public int getBlockTint(BlockPos pos, ColorResolver resolver) {
|
||||
if (biomes != null) return biomes.tint(pos, resolver);
|
||||
int rgb = resolver == BiomeColors.GRASS_COLOR_RESOLVER ? grass : resolver == BiomeColors.FOLIAGE_COLOR_RESOLVER ? foliage
|
||||
: resolver == BiomeColors.DRY_FOLIAGE_COLOR_RESOLVER ? dry : resolver == BiomeColors.WATER_COLOR_RESOLVER ? water : -1;
|
||||
if (rgb == -1) throw new AssertionError("unexpected colour resolver");
|
||||
@@ -165,7 +216,7 @@ public final class VanillaSectionMeshOracle {
|
||||
}
|
||||
private static final class Recorder {
|
||||
final List<Object> quads = new ArrayList<>();
|
||||
List<Object> pending; BlockPos block; String kind, layer, sprite;
|
||||
List<Object> pending; boolean forceSolid; BlockPos block; String kind, layer, sprite;
|
||||
void vertex(float x, float y, float z, int color, float u, float v, int light) {
|
||||
if (pending == null) pending = new ArrayList<>();
|
||||
pending.add(new Object[]{x, y, z, u, v, color, light});
|
||||
@@ -176,7 +227,7 @@ public final class VanillaSectionMeshOracle {
|
||||
}
|
||||
}
|
||||
private static String layer(ChunkSectionLayer layer) { return layer.name().toLowerCase(Locale.ROOT); }
|
||||
private static Object scene(JsonObject row, Models models, FluidStateModelSet fluids) {
|
||||
private static Object scene(JsonObject row, Models models, FluidStateModelSet fluids, HolderLookup.RegistryLookup<Biome> biomeLookup) {
|
||||
var scene = new Scene();
|
||||
var light = row.getAsJsonArray("light");
|
||||
scene.defaultLight = new int[]{light.get(0).getAsInt(), light.get(1).getAsInt()};
|
||||
@@ -184,6 +235,7 @@ public final class VanillaSectionMeshOracle {
|
||||
var colors = row.getAsJsonObject("colors");
|
||||
scene.grass = colors.get("grass").getAsInt(); scene.foliage = colors.get("foliage").getAsInt();
|
||||
scene.dry = colors.get("dry_foliage").getAsInt(); scene.water = colors.get("water").getAsInt();
|
||||
if (row.has("biomes")) scene.biomes = new TintBiomes(row.getAsJsonObject("biomes"), biomeLookup);
|
||||
for (var item : row.getAsJsonArray("blocks")) {
|
||||
var b = item.getAsJsonArray();
|
||||
scene.blocks.put(key(b.get(0).getAsInt(), b.get(1).getAsInt(), b.get(2).getAsInt()), Block.stateById(b.get(3).getAsInt()));
|
||||
@@ -194,10 +246,13 @@ public final class VanillaSectionMeshOracle {
|
||||
}
|
||||
var recorder = new Recorder();
|
||||
BlockModelLighter.enableCaching();
|
||||
var renderer = new ModelBlockRenderer(true, true, BlockColors.createDefault());
|
||||
boolean smooth = !row.has("smooth") || row.get("smooth").getAsBoolean();
|
||||
boolean cutoutLeaves = !row.has("cutout_leaves") || row.get("cutout_leaves").getAsBoolean();
|
||||
LeavesBlock.setCutoutLeaves(cutoutLeaves);
|
||||
var renderer = new ModelBlockRenderer(smooth, true, BlockColors.createDefault());
|
||||
var fluidRenderer = new FluidRenderer(fluids);
|
||||
BlockQuadOutput output = (x, y, z, quad, instance) -> {
|
||||
recorder.kind = "model"; recorder.layer = layer(quad.materialInfo().layer());
|
||||
recorder.kind = "model"; recorder.layer = recorder.forceSolid ? "solid" : layer(quad.materialInfo().layer());
|
||||
recorder.sprite = quad.materialInfo().sprite().contents().name().toString();
|
||||
int emission = quad.materialInfo().lightEmission();
|
||||
int slot = Arrays.asList(FLUID_SPRITES).indexOf(recorder.sprite);
|
||||
@@ -233,9 +288,8 @@ public final class VanillaSectionMeshOracle {
|
||||
}, state, fluid);
|
||||
}
|
||||
if (state.getRenderShape() == RenderShape.MODEL) {
|
||||
renderer.tesselateBlock(ModelBlockRenderer.forceOpaque(true, state) ? (x, y, z, quad, instance) -> {
|
||||
throw new AssertionError("cutout leaves are enabled");
|
||||
} : output, SectionPos.sectionRelative(pos.getX()), SectionPos.sectionRelative(pos.getY()), SectionPos.sectionRelative(pos.getZ()),
|
||||
recorder.forceSolid = ModelBlockRenderer.forceOpaque(cutoutLeaves, state);
|
||||
renderer.tesselateBlock(output, SectionPos.sectionRelative(pos.getX()), SectionPos.sectionRelative(pos.getY()), SectionPos.sectionRelative(pos.getZ()),
|
||||
scene, pos, state, models.get(state), state.getSeed(pos));
|
||||
}
|
||||
}
|
||||
@@ -244,6 +298,7 @@ public final class VanillaSectionMeshOracle {
|
||||
return map("name", row.get("name").getAsString(), "quads", recorder.quads);
|
||||
}
|
||||
private static BlockColors COLORS;
|
||||
private static HolderLookup.RegistryLookup<Biome> biomeLookup;
|
||||
/** Per-state facts the Rust mesher derives from prepared resources. */
|
||||
private static Object facts(BlockState state, int id) {
|
||||
var level = EmptyBlockGetter.INSTANCE;
|
||||
@@ -266,6 +321,7 @@ public final class VanillaSectionMeshOracle {
|
||||
net.minecraft.server.Bootstrap.bootStrap();
|
||||
VanillaHeightmapOracle.bindTags(JsonParser.parseString(Files.readString(Path.of(args[2]).resolve("block-tags.json"))).getAsJsonObject());
|
||||
LeavesBlock.setCutoutLeaves(true);
|
||||
GrassColor.init(colormap("grass")); FoliageColor.init(colormap("foliage")); DryFoliageColor.init(colormap("dry_foliage"));
|
||||
COLORS = BlockColors.createDefault();
|
||||
if (args[0].equals("facts")) {
|
||||
try (var output = Files.newBufferedWriter(Path.of(args[1]))) {
|
||||
@@ -288,10 +344,11 @@ public final class VanillaSectionMeshOracle {
|
||||
var atlas = new SpriteLoader.Preparations(16, 16, 0, missing, Map.of(), CompletableFuture.completedFuture(null));
|
||||
var materials = new Materials(atlas);
|
||||
var models = new Models(blocks, materials);
|
||||
biomeLookup = net.minecraft.data.registries.VanillaRegistries.createWorldLookup().lookupOrThrow(Registries.BIOME);
|
||||
var fluidModels = FluidStateModelSet.bake(materials);
|
||||
var fluids = new FluidStateModelSet(fluidModels, fluidModels.values().iterator().next());
|
||||
try (var output = Files.newBufferedWriter(Path.of(args[1]))) {
|
||||
for (var row : rows) { output.write(JSON.toJson(scene(row, models, fluids))); output.newLine(); }
|
||||
for (var row : rows) { output.write(JSON.toJson(scene(row, models, fluids, biomeLookup))); output.newLine(); }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,99 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Compare Horizon's resident PalettedContainer with the client's own.
|
||||
|
||||
Scripted edits run through vanilla's PalettedContainer (block-state and biome
|
||||
strategies: single-value, linear, hash-map and global palettes, resizing, network
|
||||
write and read, pack and unpack) and through horizon_core::paletted. Both sides
|
||||
print the set results, the serialized container after every few edits, the
|
||||
container read back from the wire, the saved form (palette, bit width, storage)
|
||||
and the container loaded from it, as hashes.
|
||||
"""
|
||||
import argparse
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import platform
|
||||
import random
|
||||
import subprocess
|
||||
try:
|
||||
from . import lab, test_rust_parity as base
|
||||
except ImportError:
|
||||
import lab
|
||||
import test_rust_parity as base
|
||||
|
||||
BUILD = lab.ROOT / 'target/paletted-container-parity'
|
||||
LIMITS = {'blocks': (4096, 35723), 'biomes': (64, 67)}
|
||||
|
||||
|
||||
def corpus():
|
||||
rng = random.Random(0x50414C45545445)
|
||||
counts = [1, 2, 3, 4, 5, 8, 9, 15, 16, 17, 31, 32, 33, 64, 100, 128, 129, 200, 256, 257, 300, 700, 2000]
|
||||
for kind, (entries, ids) in LIMITS.items():
|
||||
for count in counts:
|
||||
if kind == 'biomes' and count > 70:
|
||||
continue
|
||||
for trial in range(6):
|
||||
pool = rng.sample(range(ids), min(count, ids))
|
||||
initial = rng.choice(pool) if trial % 2 else rng.randrange(ids)
|
||||
ops = []
|
||||
for _ in range(rng.choice([entries // 4, entries, entries * 3])):
|
||||
ops.append((rng.randrange(entries), rng.choice(pool)))
|
||||
if trial == 5:
|
||||
# Every position written once, in order, then half of them reset.
|
||||
ops = [(i, pool[i % len(pool)]) for i in range(entries)] + [(i, initial) for i in range(0, entries, 2)]
|
||||
yield f'{kind}_{count}_{trial}', kind, initial, rng.choice([1, 7, 37, 211]), ops
|
||||
# A few edits at a time: the palette grows one value at a time from a single value.
|
||||
for trial in range(20):
|
||||
ops = [(rng.randrange(entries), rng.randrange(ids)) for _ in range(rng.randrange(1, 40))]
|
||||
yield f'{kind}_sparse_{trial}', kind, rng.randrange(ids), 1, ops
|
||||
# Overwriting with the value already there, and filling a section with one value.
|
||||
yield f'{kind}_fill', kind, 3, 500, [(i, 9) for i in range(entries)] + [(i, 3) for i in range(entries)]
|
||||
yield f'{kind}_same', kind, 3, 1, [(i, 3) for i in range(0, entries, 5)]
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description=__doc__.splitlines()[0])
|
||||
parser.parse_args()
|
||||
lock, jars, libraries = base.verify_artifacts()
|
||||
BUILD.mkdir(parents=True, exist_ok=True)
|
||||
oracle = lab.ROOT / 'mods/horizon/src/test/java/dev/horizonlab/parity/VanillaPalettedContainerOracle.java'
|
||||
sources = [Path(__file__), oracle, lab.ROOT / 'tools/lab.py', lab.ROOT / 'tools/test_rust_parity.py', lab.ROOT / 'Cargo.toml', lab.ROOT / 'Cargo.lock',
|
||||
lab.ROOT / 'crates/horizon-core/Cargo.toml', lab.ROOT / 'crates/horizon-core/examples/paletted_fixture.rs',
|
||||
*sorted((lab.ROOT / 'crates/horizon-core/src').rglob('*.rs'))]
|
||||
tested = base.source_hashes(sources)
|
||||
scenarios = list(corpus())
|
||||
inputs = BUILD / 'inputs.txt'
|
||||
inputs.write_text(''.join(f'{name} {kind} {initial} {every} ' + ' '.join(f'{i} {v}' for i, v in ops) + '\n'
|
||||
for name, kind, initial, every, ops in scenarios))
|
||||
|
||||
def run(command, name):
|
||||
with (BUILD / (name + '.log')).open('w') as log:
|
||||
base.run(list(map(str, command)), stdout=log, stderr=subprocess.STDOUT)
|
||||
classes = BUILD / 'classes'
|
||||
classes.mkdir(exist_ok=True)
|
||||
dependencies = os.pathsep.join(map(str, [jars['client'], *libraries]))
|
||||
run(['javac', '--release', lock['minecraft']['java_major'], '-proc:none', '-cp', dependencies, '-d', classes, oracle], 'compile-java')
|
||||
run(['cargo', 'build', '--locked', '--profile', 'fast', '-j', os.environ.get('CARGO_BUILD_JOBS', '3'), '-p', 'horizon-core', '--example', 'paletted_fixture'], 'compile-rust')
|
||||
metadata = json.loads(subprocess.check_output(['cargo', 'metadata', '--locked', '--no-deps', '--format-version', '1'], cwd=lab.ROOT))
|
||||
binary = Path(metadata['target_directory']) / 'fast/examples/paletted_fixture'
|
||||
run([binary, inputs, BUILD / 'rust.txt'], 'rust')
|
||||
run(['java', '-Xmx1G', '-cp', str(classes) + os.pathsep + dependencies, 'dev.horizonlab.parity.VanillaPalettedContainerOracle', inputs, BUILD / 'client.txt'], 'client')
|
||||
client = (BUILD / 'client.txt').read_text().splitlines()
|
||||
rust = (BUILD / 'rust.txt').read_text().splitlines()
|
||||
assert len(client) == len(rust) == len(scenarios), (len(client), len(rust), len(scenarios))
|
||||
bad = [(a, b) for a, b in zip(client, rust) if a != b]
|
||||
base.ensure_sources_unchanged(sources, tested)
|
||||
for a, b in bad[:5]:
|
||||
print('client', a[:600])
|
||||
print('rust ', b[:600])
|
||||
if bad:
|
||||
raise SystemExit(f'{len(bad)} of {len(scenarios)} scenarios differ')
|
||||
edits = sum(len(s[4]) for s in scenarios)
|
||||
report = {'schema': 1, 'minecraft': lock['minecraft']['version'], 'platform': platform.platform(), 'scenarios': len(scenarios), 'edits': edits,
|
||||
'sources_sha256': tested}
|
||||
lab.write_json(BUILD / 'report.json', report)
|
||||
print(f'PalettedContainer parity: {len(scenarios)} scenarios, {edits:,} edits, all hashes identical')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
@@ -29,6 +29,7 @@ except ImportError:
|
||||
import test_model_baking_parity as baking
|
||||
|
||||
BUILD = lab.ROOT / 'target/section-mesh-parity'
|
||||
RESOURCES = lab.LOCAL / 'horizon-client' / '26.3'
|
||||
SECTION_Y = 0
|
||||
COLORS = {'grass': 0x91BD59, 'foliage': 0x77AB2F, 'dry_foliage': 0xA88B5E, 'water': 0x3F76E4}
|
||||
POSITION_TOLERANCE = 1e-4
|
||||
@@ -71,6 +72,8 @@ class Scene:
|
||||
self.blocks = {}
|
||||
self.light_override = {}
|
||||
self.noise = None
|
||||
self.biomes = None
|
||||
self.options = {}
|
||||
|
||||
def put(self, x, y, z, name, **properties):
|
||||
self.blocks[(x, y, z)] = name if isinstance(name, int) else self.states(name, **properties)
|
||||
@@ -130,8 +133,38 @@ class Scene:
|
||||
return result
|
||||
|
||||
def row(self):
|
||||
return {'name': self.name, 'nether': self.nether, 'section_y': SECTION_Y, 'colors': COLORS, 'light': [15, 0],
|
||||
'blocks': [[*p, s] for p, s in sorted(self.blocks.items())], 'lights': self.lights()}
|
||||
row = {'name': self.name, 'nether': self.nether, 'section_y': SECTION_Y, 'colors': COLORS, 'light': [15, 0],
|
||||
'blocks': [[*p, s] for p, s in sorted(self.blocks.items())], 'lights': self.lights()}
|
||||
if self.biomes is not None:
|
||||
row['biomes'] = self.biomes
|
||||
row.update(self.options)
|
||||
return row
|
||||
|
||||
def tinted_columns(self):
|
||||
"""Every tint source at several heights over the section's 16x16 footprint."""
|
||||
names = [('grass_block', {}), ('short_grass', {}), ('oak_leaves', {}), ('water', {}), ('leaf_litter', {'segment_amount': 2}),
|
||||
('sugar_cane', {}), ('fern', {}), ('vine', {'north': True}), ('mangrove_leaves', {}), ('jungle_leaves', {})]
|
||||
for x in range(16):
|
||||
for z in range(16):
|
||||
for layer, y in enumerate((1, 5, 9, 13)):
|
||||
name, properties = names[(x * 5 + z * 3 + layer) % len(names)]
|
||||
self.put(x, y, z, 'stone')
|
||||
self.put(x, y + 1, z, name, **properties)
|
||||
if name == 'water':
|
||||
self.put(x, y + 2, z, 'water')
|
||||
|
||||
def biome_field(self, seed, radius, names, default='plains'):
|
||||
"""Noise-biome quart cells: a Voronoi patchwork that shifts with height."""
|
||||
rng = random.Random(seed)
|
||||
sites = [[(rng.uniform(-4, 8), rng.uniform(-4, 8), rng.choice(names)) for _ in range(9)] for _ in range(10)]
|
||||
cells = []
|
||||
for qy in range(-3, 8):
|
||||
layer = sites[(qy + 3) // 2 % len(sites)]
|
||||
for qx in range(-4, 8):
|
||||
for qz in range(-4, 8):
|
||||
best = min(layer, key=lambda site: (site[0] - qx) ** 2 + (site[1] - qz) ** 2)
|
||||
cells.append([qx, qy, qz, 'minecraft:' + best[2]])
|
||||
self.biomes = {'seed': rng.randrange(-2 ** 63, 2 ** 63), 'radius': radius, 'default': 'minecraft:' + default, 'cells': cells}
|
||||
|
||||
|
||||
def corpus(states, full=True):
|
||||
@@ -306,6 +339,38 @@ def corpus(states, full=True):
|
||||
s.put(rng.randrange(-1, 17), rng.randrange(-1, 17), rng.randrange(-1, 17), rng.choice(ids))
|
||||
if seed % 2:
|
||||
s.noise = seed
|
||||
# Biome tint blending: BiomeManager's zoomed biome at each block's own height, averaged
|
||||
# over the blend radius, for real biome colours (colormaps, overrides, modifiers).
|
||||
biome_names = sorted(path.stem for path in (RESOURCES / 'data/minecraft/worldgen/biome').glob('*.json'))
|
||||
common = ['plains', 'swamp', 'dark_forest', 'desert', 'jungle', 'badlands', 'mangrove_swamp', 'warm_ocean', 'snowy_plains',
|
||||
'birch_forest', 'cherry_grove', 'pale_garden', 'savanna', 'taiga', 'old_growth_pine_taiga', 'lush_caves']
|
||||
for i, radius in enumerate((2, 0, 1, 3, 5, 7)):
|
||||
s = scene(f'biome_blend_r{radius}')
|
||||
s.floor(0)
|
||||
s.tinted_columns()
|
||||
s.biome_field(0x42494F4D + i, radius, common)
|
||||
for i in range(0, len(biome_names), 8):
|
||||
s = scene(f'biome_each_{i // 8}')
|
||||
s.tinted_columns()
|
||||
s.biome_field(0x45414348 + i, 2, biome_names[i:i + 8], biome_names[i])
|
||||
# Options: flat lighting (ao off) and opaque leaves (cutoutLeaves off) alone and together.
|
||||
leaves = ['oak_leaves', 'spruce_leaves', 'birch_leaves', 'jungle_leaves', 'acacia_leaves', 'dark_oak_leaves',
|
||||
'azalea_leaves', 'flowering_azalea_leaves', 'mangrove_leaves', 'cherry_leaves', 'pale_oak_leaves']
|
||||
option_palette = ['stone', 'glass', 'grass_block', 'water', 'lava', 'oak_slab', 'oak_stairs', 'torch', 'poppy', 'short_grass',
|
||||
'glowstone', 'oak_fence', 'ice', 'oak_log', 'tinted_glass', 'slime_block', 'honey_block'] + leaves * 2
|
||||
for i, options in enumerate(({'smooth': False}, {'cutout_leaves': False}, {'smooth': False, 'cutout_leaves': False})):
|
||||
rng = random.Random(0x4F505453 + i)
|
||||
s = scene(f'options_{i}', nether=i == 2)
|
||||
s.options = options
|
||||
s.floor(0)
|
||||
for x in range(-1, 17):
|
||||
for y in range(1, 17):
|
||||
for z in range(-1, 17):
|
||||
if rng.random() < 0.5:
|
||||
name = rng.choice(option_palette)
|
||||
s.put(x, y, z, rng.choice(states.by_name['minecraft:' + name]))
|
||||
if i:
|
||||
s.noise = i
|
||||
s = scene('cubes_nether', nether=True)
|
||||
s.floor(3, 'netherrack')
|
||||
s.fill(4, 4, 4, 6, 6, 6, 'nether_bricks')
|
||||
@@ -337,6 +402,7 @@ def light(coords):
|
||||
|
||||
|
||||
def compare(scene, client, rust, states, report, limit):
|
||||
blend_zero = scene.get('biomes', {}).get('radius') == 0
|
||||
blocks = {tuple(b[:3]): b[3] for b in scene['blocks']}
|
||||
def describe(quad, rotation):
|
||||
v = quad['vertices']
|
||||
@@ -377,6 +443,11 @@ def compare(scene, client, rust, states, report, limit):
|
||||
if any(abs(p[i] - q[i]) > UV_TOLERANCE for i in (3, 4)):
|
||||
problems.add('uv')
|
||||
cp, cq = channels(p[5]), channels(q[5])
|
||||
if blend_zero and cp[3] == 0 and cq[3] == 255:
|
||||
# With biomeBlendRadius 0 the client uses the resolver's colour as is,
|
||||
# and biome colours given as plain RGB have alpha 0: tinted vertices come
|
||||
# out fully transparent. Horizon keeps them opaque; RGB is compared.
|
||||
cp[3] = 255
|
||||
if cp != cq:
|
||||
problems.add('color' if max(abs(x - y) for x, y in zip(cp, cq)) > 1 else 'color_rounding')
|
||||
if light(p[6]) != light(q[6]):
|
||||
|
||||
Reference in New Issue
Block a user