diff --git a/crates/horizon-world/examples/chunkgen_bench.rs b/crates/horizon-world/examples/chunkgen_bench.rs index 4992515c5..23360b637 100644 --- a/crates/horizon-world/examples/chunkgen_bench.rs +++ b/crates/horizon-world/examples/chunkgen_bench.rs @@ -3,6 +3,7 @@ //! chunkgen_bench [--seed S] [--size N] [--threads T] //! [--center CX CZ] [--verify ] [--no-decorate] //! [--dimension overworld|the_nether|the_end] +//! [--warmup] [--json] [--no-map] use horizon_core::position::ChunkPos; use horizon_world::{ DATA_VERSION, @@ -275,6 +276,7 @@ fn main() -> Result<(), Error> { let (mut seed, mut size, mut threads, mut center) = (0i64, 16i32, 2usize, (0, 0)); let mut parity = None; let mut decorate = true; + let (mut warmup, mut json, mut map) = (false, false, true); let mut dimension = String::from("minecraft:overworld"); let mut starts: Option<(i32, i32, i32, i32, PathBuf)> = None; while let Some(arg) = args.next() { @@ -286,6 +288,9 @@ fn main() -> Result<(), Error> { "--center" => center = (next().parse()?, next().parse()?), "--verify" => parity = Some(PathBuf::from(next())), "--no-decorate" => decorate = false, + "--warmup" => warmup = true, + "--json" => json = true, + "--no-map" => map = false, "--dimension" => { let d = next(); dimension = if d.contains(':') { @@ -326,6 +331,16 @@ fn main() -> Result<(), Error> { dump_starts(&generator, seed, [x0, z0, x1, z1], &out)?; return Ok(()); } + if warmup { + let mut worker = generator.worker(); + for z in -4..=4 { + for x in -4..=4 { + std::hint::black_box( + worker.generate_proto(ChunkPos::new(center.0 + 64 + x, center.1 + 64 + z))?, + ); + } + } + } let (x0, z0) = (center.0 - size / 2, center.1 - size / 2); let count = (size * size) as usize; let next = AtomicUsize::new(0); @@ -353,6 +368,7 @@ fn main() -> Result<(), Error> { handles.into_iter().try_for_each(|h| h.join().unwrap()) })?; let elapsed = start.elapsed(); + let cpu_elapsed = cpu_seconds() - cpu; println!( "generated {count} proto chunks with {threads} threads in {elapsed:.2?}: {:.1} chunks/s ({:.1} ms/chunk/thread)", count as f64 / elapsed.as_secs_f64(), @@ -360,7 +376,7 @@ fn main() -> Result<(), Error> { ); println!( " cpu time {:.1} ms/chunk", - (cpu_seconds() - cpu) * 1000. / count as f64 + cpu_elapsed * 1000. / count as f64 ); let timings = timings.into_inner().unwrap(); let per_chunk = |f: fn(&StageTimings) -> std::time::Duration| { @@ -379,6 +395,16 @@ fn main() -> Result<(), Error> { per_chunk(|t| t.surface), per_chunk(|t| t.carvers) ); + if json { + println!( + "BENCH {}", + serde_json::json!({"phase": "proto", "chunks": count, + "threads": threads, "wall_s": elapsed.as_secs_f64(), "cpu_s": cpu_elapsed, + "biomes_ms_chunk": per_chunk(|t| t.biomes), "noise_ms_chunk": per_chunk(|t| t.noise), + "structures_ms_chunk": per_chunk(|t| t.structures), + "surface_ms_chunk": per_chunk(|t| t.surface), "carvers_ms_chunk": per_chunk(|t| t.carvers)}) + ); + } let mut chunks: Vec> = slots .into_iter() .map(|slot| slot.into_inner().unwrap()) @@ -441,6 +467,13 @@ fn main() -> Result<(), Error> { } } let elapsed = start.elapsed(); + if json { + println!( + "BENCH {}", + serde_json::json!({"phase": "decorate", "chunks": decorated, + "wall_s": elapsed.as_secs_f64()}) + ); + } let totals = totals.into_inner().unwrap(); println!( "decorated {decorated} chunks in {elapsed:.2?}: {:.1} chunks/s; feature roots placed {}, unsupported {}, failed {}", @@ -492,6 +525,9 @@ fn main() -> Result<(), Error> { } } } + if !map { + return Ok(()); + } let mut legend = BTreeMap::>::new(); let step = (size * 16 / 96).max(1); for bz in (0..size * 16).step_by(step as usize) { diff --git a/crates/horizon-world/src/worldgen/aquifer/noise_based.rs b/crates/horizon-world/src/worldgen/aquifer/noise_based.rs index 30181686b..3f3c7dc2c 100644 --- a/crates/horizon-world/src/worldgen/aquifer/noise_based.rs +++ b/crates/horizon-world/src/worldgen/aquifer/noise_based.rs @@ -120,6 +120,7 @@ pub struct NoiseBasedAquifer { statuses: Vec>, locations: Vec, surfaces: HashMap, + neighborhood: Option<([i32; 3], [(usize, BlockPos); 12])>, skip_above_y: i32, schedule: bool, } @@ -180,6 +181,7 @@ impl NoiseBasedAquif statuses, locations, surfaces: HashMap::new(), + neighborhood: None, skip_above_y: 0, schedule: false, }; @@ -479,48 +481,76 @@ impl NoiseBasedAquif y.wrapping_add(1).div_euclid(12), z.wrapping_sub(5) >> 4, ]; - let mut distances = [i32::MAX; 4]; - let mut indices = [0usize; 4]; - for dx in 0..=1i32 { - for dy in -1..=1i32 { - for dz in 0..=1i32 { - let cell = [ - grid[0].wrapping_add(dx), - grid[1].wrapping_add(dy), - grid[2].wrapping_add(dz), - ]; - let index = self.grid_index(cell)?; - if self.locations[index] == i64::MAX { - let mut random = self.random.at(BlockPos::new(cell[0], cell[1], cell[2])); - let px = cell[0] - .wrapping_shl(4) - .wrapping_add(random.next_int_bound(10).expect("positive bound")); - let py = cell[1] - .wrapping_mul(12) - .wrapping_add(random.next_int_bound(9).expect("positive bound")); - let pz = cell[2] - .wrapping_shl(4) - .wrapping_add(random.next_int_bound(10).expect("positive bound")); - self.locations[index] = BlockPos::new(px, py, pz).pack(); - } - let point = BlockPos::unpack(self.locations[index]); - let ax = point.x.wrapping_sub(x); - let ay = point.y.wrapping_sub(y); - let az = point.z.wrapping_sub(z); - let distance = ax - .wrapping_mul(ax) - .wrapping_add(ay.wrapping_mul(ay)) - .wrapping_add(az.wrapping_mul(az)); - if let Some(slot) = distances.iter().position(|old| distance <= *old) { - for i in (slot + 1..4).rev() { - distances[i] = distances[i - 1]; - indices[i] = indices[i - 1]; + // Fill in the original cell order, publishing only after checked traversal + // succeeds. Failed traversal keeps the last complete neighborhood. + if self.neighborhood.as_ref().map(|(key, _)| *key) != Some(grid) { + let mut entries = [(0, BlockPos::default()); 12]; + let mut next = 0; + for dx in 0..=1i32 { + for dy in -1..=1i32 { + for dz in 0..=1i32 { + let cell = [ + grid[0].wrapping_add(dx), + grid[1].wrapping_add(dy), + grid[2].wrapping_add(dz), + ]; + let index = self.grid_index(cell)?; + if self.locations[index] == i64::MAX { + let mut random = + self.random.at(BlockPos::new(cell[0], cell[1], cell[2])); + let px = cell[0] + .wrapping_shl(4) + .wrapping_add(random.next_int_bound(10).expect("positive bound")); + let py = cell[1] + .wrapping_mul(12) + .wrapping_add(random.next_int_bound(9).expect("positive bound")); + let pz = cell[2] + .wrapping_shl(4) + .wrapping_add(random.next_int_bound(10).expect("positive bound")); + self.locations[index] = BlockPos::new(px, py, pz).pack(); } - distances[slot] = distance; - indices[slot] = index; + entries[next] = (index, BlockPos::unpack(self.locations[index])); + next += 1; } } } + self.neighborhood = Some((grid, entries)); + } + let mut distances = [i32::MAX; 4]; + let mut indices = [0usize; 4]; + for &(index, point) in &self + .neighborhood + .as_ref() + .expect("populated neighborhood") + .1 + { + let ax = point.x.wrapping_sub(x); + let ay = point.y.wrapping_sub(y); + let az = point.z.wrapping_sub(z); + let distance = ax + .wrapping_mul(ax) + .wrapping_add(ay.wrapping_mul(ay)) + .wrapping_add(az.wrapping_mul(az)); + // Equal distances insert before existing entries, as in vanilla. + if distance <= distances[0] { + distances = [distance, distances[0], distances[1], distances[2]]; + indices = [index, indices[0], indices[1], indices[2]]; + } else if distance <= distances[1] { + distances[3] = distances[2]; + distances[2] = distances[1]; + distances[1] = distance; + indices[3] = indices[2]; + indices[2] = indices[1]; + indices[1] = index; + } else if distance <= distances[2] { + distances[3] = distances[2]; + distances[2] = distance; + indices[3] = indices[2]; + indices[2] = index; + } else if distance <= distances[3] { + distances[3] = distance; + indices[3] = index; + } } let a = self.status(context, indices[0])?; let s01 = similarity(distances[0], distances[1]);