WIP: staged proto/decorate/finish streaming and vanilla terrain adapter
Streaming now supports vanilla's generation stages (proto chunk, 3x3 decoration, finalization). vanillagen.rs binds the overworld generator from the generator branch; it is not yet registered in lib.rs until that branch is merged. Adds dropped items, particles, crack overlay, sounds, Anvil saves and the entity model exporter. Build not yet verified. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -50,7 +50,8 @@ impl TerrainBlockStates for Properties {
|
||||
}
|
||||
}
|
||||
pub struct Assets {
|
||||
pub registry: BlockRegistry,
|
||||
/// Leaked once per process: shared by world generation threads.
|
||||
pub registry: &'static BlockRegistry,
|
||||
pub properties: Properties,
|
||||
pub material_states: [BlockStateId; 6],
|
||||
pub sprite_size: u32,
|
||||
@@ -178,7 +179,8 @@ impl Assets {
|
||||
material_states[layer] = state;
|
||||
sprites.insert(root, &format!("minecraft:block/{name}"))?;
|
||||
}
|
||||
let models = crate::models::prepare(root, ®istry, &mut sprites)?;
|
||||
let registry: &'static BlockRegistry = Box::leak(Box::new(registry));
|
||||
let models = crate::models::prepare(root, registry, &mut sprites)?;
|
||||
// Fluids are drawn by the fluid renderer, not block models; breaking
|
||||
// progress uses the destroy-stage overlays.
|
||||
for name in ["water_still", "water_flow", "water_overlay", "lava_still", "lava_flow"] {
|
||||
|
||||
@@ -409,13 +409,23 @@ impl<G: ChunkGenerator> ChunkGenerator for Persistent<G> {
|
||||
fn worker(&self) -> G::Worker {
|
||||
self.inner.worker()
|
||||
}
|
||||
fn generate(&self, worker: &mut G::Worker, x: i32, z: i32) -> Chunk {
|
||||
if let Some(mut chunk) = self.save.load(x, z, &self.registry.registry, &self.biomes) {
|
||||
for s in &mut chunk.sections {
|
||||
Arc::make_mut(s).compact();
|
||||
}
|
||||
return chunk;
|
||||
fn load(&self, x: i32, z: i32) -> Option<Chunk> {
|
||||
let mut chunk = self.save.load(x, z, &self.registry.registry, &self.biomes)?;
|
||||
for s in &mut chunk.sections {
|
||||
Arc::make_mut(s).compact();
|
||||
}
|
||||
self.inner.generate(worker, x, z)
|
||||
Some(chunk)
|
||||
}
|
||||
fn proto(&self, worker: &mut G::Worker, x: i32, z: i32) -> crate::streaming::Proto {
|
||||
self.inner.proto(worker, x, z)
|
||||
}
|
||||
fn decorates(&self) -> bool {
|
||||
self.inner.decorates()
|
||||
}
|
||||
fn decorate(&self, worker: &mut G::Worker, region: &mut [crate::streaming::Proto; 9]) {
|
||||
self.inner.decorate(worker, region);
|
||||
}
|
||||
fn finish(&self, proto: crate::streaming::Proto) -> Chunk {
|
||||
self.inner.finish(proto)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,20 +18,39 @@ use std::{
|
||||
thread,
|
||||
};
|
||||
|
||||
/// Produces fully generated chunk columns. Implementations must be thread-safe;
|
||||
/// each worker thread owns one `Worker` for per-thread caches.
|
||||
/// A chunk under generation, opaque to the streamer.
|
||||
pub type Proto = Box<dyn std::any::Any + Send>;
|
||||
/// Chunk generation in vanilla's stages: a neighbour-independent proto chunk
|
||||
/// (biomes, noise, surface, carvers), decoration of a 3x3 neighbourhood
|
||||
/// (FEATURES, which may write into neighbours), then conversion of a chunk
|
||||
/// whose eight neighbours are decorated into a final column. Implementations
|
||||
/// must be thread-safe; each worker thread owns one `Worker`.
|
||||
pub trait ChunkGenerator: Send + Sync + 'static {
|
||||
type Worker: Send + 'static;
|
||||
fn worker(&self) -> Self::Worker;
|
||||
fn generate(&self, worker: &mut Self::Worker, x: i32, z: i32) -> Chunk;
|
||||
/// A previously saved final chunk, bypassing generation.
|
||||
fn load(&self, _x: i32, _z: i32) -> Option<Chunk> {
|
||||
None
|
||||
}
|
||||
fn proto(&self, worker: &mut Self::Worker, x: i32, z: i32) -> Proto;
|
||||
fn decorates(&self) -> bool {
|
||||
false
|
||||
}
|
||||
/// Decorate `region[4]`; the region is row-major (dz, dx) around it.
|
||||
fn decorate(&self, _worker: &mut Self::Worker, _region: &mut [Proto; 9]) {}
|
||||
fn finish(&self, proto: Proto) -> Chunk;
|
||||
}
|
||||
enum Job {
|
||||
Generate(i32, i32),
|
||||
Decorate(i32, i32, Box<[Proto; 9]>),
|
||||
Finish(i32, i32, Proto),
|
||||
Light(i32, i32, Box<Region>, u64),
|
||||
Mesh(i32, i32, i32, Box<Region>, u64),
|
||||
}
|
||||
pub enum Done {
|
||||
Generated(Chunk),
|
||||
Proto(i32, i32, Proto),
|
||||
Decorated(i32, i32, Box<[Proto; 9]>),
|
||||
Lit(i32, i32, Vec<Arc<crate::chunk::Section>>, u64),
|
||||
Meshed(i32, i32, i32, SectionMesh, u64),
|
||||
}
|
||||
@@ -70,6 +89,12 @@ pub struct Streaming {
|
||||
threads: Vec<thread::JoinHandle<()>>,
|
||||
order: u64,
|
||||
generating: HashSet<(i32, i32)>,
|
||||
/// Proto chunks: `None` while checked out to a decoration job.
|
||||
protos: HashMap<(i32, i32), Option<Proto>>,
|
||||
decorated: HashSet<(i32, i32)>,
|
||||
decorating: usize,
|
||||
finishing: HashSet<(i32, i32)>,
|
||||
decorates: bool,
|
||||
lighting: HashMap<(i32, i32), u64>,
|
||||
meshing: HashMap<(i32, i32, i32), u64>,
|
||||
/// Chunks whose light is current.
|
||||
@@ -126,9 +151,15 @@ impl Streaming {
|
||||
}
|
||||
};
|
||||
let done = match job {
|
||||
Job::Generate(x, z) => {
|
||||
Done::Generated(generator.generate(&mut worker, x, z))
|
||||
Job::Generate(x, z) => match generator.load(x, z) {
|
||||
Some(chunk) => Done::Generated(chunk),
|
||||
None => Done::Proto(x, z, generator.proto(&mut worker, x, z)),
|
||||
},
|
||||
Job::Decorate(x, z, mut region) => {
|
||||
generator.decorate(&mut worker, &mut region);
|
||||
Done::Decorated(x, z, region)
|
||||
}
|
||||
Job::Finish(_, _, proto) => Done::Generated(generator.finish(proto)),
|
||||
Job::Light(x, z, region, seq) => {
|
||||
Done::Lit(x, z, light_chunk(&borrow(®ion), &context.light), seq)
|
||||
}
|
||||
@@ -150,6 +181,11 @@ impl Streaming {
|
||||
threads,
|
||||
order: 0,
|
||||
generating: HashSet::new(),
|
||||
protos: HashMap::new(),
|
||||
decorated: HashSet::new(),
|
||||
decorating: 0,
|
||||
finishing: HashSet::new(),
|
||||
decorates: generator.decorates(),
|
||||
lighting: HashMap::new(),
|
||||
meshing: HashMap::new(),
|
||||
lit: HashSet::new(),
|
||||
@@ -195,9 +231,28 @@ impl Streaming {
|
||||
let mut uploads = Vec::new();
|
||||
while let Ok(done) = self.results.try_recv() {
|
||||
match done {
|
||||
Done::Proto(x, z, proto) => {
|
||||
self.generating.remove(&(x, z));
|
||||
if self.in_range(x, z, 4) {
|
||||
self.protos.insert((x, z), Some(proto));
|
||||
if !self.decorates {
|
||||
self.decorated.insert((x, z));
|
||||
}
|
||||
}
|
||||
}
|
||||
Done::Decorated(x, z, region) => {
|
||||
self.decorating -= 1;
|
||||
self.decorated.insert((x, z));
|
||||
for (i, proto) in region.into_iter().enumerate() {
|
||||
let key = (x + (i % 3) as i32 - 1, z + (i / 3) as i32 - 1);
|
||||
self.protos.insert(key, Some(proto));
|
||||
}
|
||||
}
|
||||
Done::Generated(chunk) => {
|
||||
let key = (chunk.x, chunk.z);
|
||||
self.generating.remove(&key);
|
||||
self.finishing.remove(&key);
|
||||
self.decorated.insert(key);
|
||||
if self.in_range(key.0, key.1, 2) {
|
||||
self.generated_total += 1;
|
||||
world.chunks.insert(key, Arc::new(chunk));
|
||||
@@ -249,6 +304,9 @@ impl Streaming {
|
||||
.copied()
|
||||
.filter(|&(x, z)| !self.in_range(x, z, 3))
|
||||
.collect();
|
||||
for key in &far {
|
||||
self.decorated.remove(key);
|
||||
}
|
||||
let mut removed = Vec::with_capacity(far.len());
|
||||
for key in &far {
|
||||
if let Some(chunk) = world.chunks.remove(key) {
|
||||
@@ -257,17 +315,78 @@ impl Streaming {
|
||||
self.lit.remove(key);
|
||||
self.meshed.remove(key);
|
||||
}
|
||||
// Generation, closest first, bounded in flight.
|
||||
// Drop far proto chunks that are not checked out.
|
||||
let (cx, cz, r4) = (self.center.0, self.center.1, i64::from(self.radius + 4));
|
||||
self.protos.retain(|&(x, z), p| {
|
||||
let d = i64::from(x - cx).pow(2) + i64::from(z - cz).pow(2);
|
||||
p.is_none() || d <= r4 * r4
|
||||
});
|
||||
let (protos, finishing) = (&self.protos, &self.finishing);
|
||||
self.decorated
|
||||
.retain(|k| protos.contains_key(k) || finishing.contains(k) || world.chunks.contains_key(k));
|
||||
// Finalize chunks whose neighbourhood is decorated.
|
||||
let candidates: Vec<(i32, i32)> = self.protos.keys().copied().collect();
|
||||
for (x, z) in candidates {
|
||||
if world.chunks.contains_key(&(x, z)) || self.finishing.contains(&(x, z)) || !self.in_range(x, z, 1) {
|
||||
continue;
|
||||
}
|
||||
let ready = (-1..=1).all(|dz| {
|
||||
(-1..=1).all(|dx| self.decorated.contains(&(x + dx, z + dz)) || world.chunks.contains_key(&(x + dx, z + dz)))
|
||||
});
|
||||
if ready && let Some(Some(_)) = self.protos.get(&(x, z)) {
|
||||
let proto = self.protos.remove(&(x, z)).flatten().expect("present proto");
|
||||
self.finishing.insert((x, z));
|
||||
self.submit(self.distance(x, z) * 4, Job::Finish(x, z, proto));
|
||||
}
|
||||
}
|
||||
// Decorate chunks whose 3x3 proto neighbourhood is available.
|
||||
let workers = self.threads.len();
|
||||
if self.decorates && self.decorating < workers * 2 {
|
||||
let mut wanted: Vec<(i64, i32, i32)> = self
|
||||
.protos
|
||||
.iter()
|
||||
.filter(|(k, p)| p.is_some() && !self.decorated.contains(k) && self.in_range(k.0, k.1, 2))
|
||||
.map(|(k, _)| (self.distance(k.0, k.1), k.0, k.1))
|
||||
.collect();
|
||||
wanted.sort_unstable();
|
||||
for (d, x, z) in wanted {
|
||||
if self.decorating >= workers * 2 {
|
||||
break;
|
||||
}
|
||||
let keys: Vec<(i32, i32)> = (0..9).map(|i| (x + (i % 3) - 1, z + (i / 3) - 1)).collect();
|
||||
// A final (saved) neighbour cannot receive writes: skip decoration.
|
||||
if keys.iter().any(|k| world.chunks.contains_key(k)) {
|
||||
self.decorated.insert((x, z));
|
||||
continue;
|
||||
}
|
||||
if !keys.iter().all(|k| matches!(self.protos.get(k), Some(Some(_)))) {
|
||||
continue;
|
||||
}
|
||||
let region: Vec<Proto> = keys
|
||||
.iter()
|
||||
.map(|k| self.protos.insert(*k, None).flatten().expect("present proto"))
|
||||
.collect();
|
||||
let region: Box<[Proto; 9]> = match region.try_into() {
|
||||
Ok(r) => Box::new(r),
|
||||
Err(_) => continue,
|
||||
};
|
||||
self.decorating += 1;
|
||||
self.submit(d * 4 + 1, Job::Decorate(x, z, region));
|
||||
}
|
||||
}
|
||||
// Generation (load or proto), closest first, bounded in flight.
|
||||
let margin = if self.decorates { 3 } else { 1 };
|
||||
if self.generating.len() < workers * 3 {
|
||||
let r = self.radius + 1;
|
||||
let r = self.radius + margin;
|
||||
let mut wanted = Vec::new();
|
||||
for dz in -r..=r {
|
||||
for dx in -r..=r {
|
||||
let (x, z) = (self.center.0 + dx, self.center.1 + dz);
|
||||
if !self.in_range(x, z, 1)
|
||||
if !self.in_range(x, z, margin)
|
||||
|| world.chunks.contains_key(&(x, z))
|
||||
|| self.generating.contains(&(x, z))
|
||||
|| self.protos.contains_key(&(x, z))
|
||||
|| self.finishing.contains(&(x, z))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -64,6 +64,14 @@ fn hash(x: i32, z: i32) -> u32 {
|
||||
impl ChunkGenerator for TestTerrain {
|
||||
type Worker = ();
|
||||
fn worker(&self) {}
|
||||
fn proto(&self, w: &mut (), x: i32, z: i32) -> crate::streaming::Proto {
|
||||
Box::new(self.generate(w, x, z))
|
||||
}
|
||||
fn finish(&self, proto: crate::streaming::Proto) -> Chunk {
|
||||
*proto.downcast::<Chunk>().expect("test terrain proto")
|
||||
}
|
||||
}
|
||||
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()]);
|
||||
|
||||
@@ -0,0 +1,126 @@
|
||||
//! Binds `horizon_world`'s vanilla overworld generator (noise terrain,
|
||||
//! aquifers, surface rules, carvers and placed features) to chunk streaming.
|
||||
use crate::{
|
||||
biome::Biomes,
|
||||
chunk::{Chunk, HEIGHT, MIN_Y},
|
||||
streaming::{ChunkGenerator, Proto},
|
||||
};
|
||||
use horizon_core::position::ChunkPos;
|
||||
use horizon_world::{
|
||||
registry::BlockRegistry,
|
||||
worldgen::generator::{GeneratorData, GeneratorWorker, OverworldGenerator, ProtoChunk},
|
||||
};
|
||||
use std::{path::Path, sync::Arc};
|
||||
|
||||
pub struct VanillaTerrain {
|
||||
generator: OverworldGenerator,
|
||||
/// Generator biome index -> client biome table index.
|
||||
biome_map: Vec<u16>,
|
||||
}
|
||||
/// A proto chunk, or the position of one whose generation failed.
|
||||
enum VanillaProto {
|
||||
Ready(Box<ProtoChunk>),
|
||||
Failed(i32, i32),
|
||||
}
|
||||
impl VanillaTerrain {
|
||||
pub fn new(dir: &Path, registry: &'static BlockRegistry, biomes: &Biomes, seed: i64) -> crate::Result<Self> {
|
||||
let data: Arc<GeneratorData> = GeneratorData::load(dir, registry)?;
|
||||
let biome_map = data.biome_names().iter().map(|name| biomes.id(name)).collect();
|
||||
let generator = OverworldGenerator::new(data, seed)?;
|
||||
let unsupported = generator.unsupported_features();
|
||||
if !unsupported.is_empty() {
|
||||
eprintln!(
|
||||
"Horizon: {} placed features are not implemented yet and will be skipped",
|
||||
unsupported.len()
|
||||
);
|
||||
}
|
||||
Ok(Self { generator, biome_map })
|
||||
}
|
||||
}
|
||||
impl ChunkGenerator for VanillaTerrain {
|
||||
type Worker = GeneratorWorker;
|
||||
fn worker(&self) -> GeneratorWorker {
|
||||
self.generator.worker()
|
||||
}
|
||||
fn proto(&self, worker: &mut GeneratorWorker, x: i32, z: i32) -> Proto {
|
||||
match worker.generate_proto(ChunkPos::new(x, z)) {
|
||||
Ok(chunk) => Box::new(VanillaProto::Ready(Box::new(chunk))),
|
||||
Err(e) => {
|
||||
eprintln!("Horizon: generating chunk {x} {z} failed: {e}");
|
||||
Box::new(VanillaProto::Failed(x, z))
|
||||
}
|
||||
}
|
||||
}
|
||||
fn decorates(&self) -> bool {
|
||||
true
|
||||
}
|
||||
fn decorate(&self, worker: &mut GeneratorWorker, region: &mut [Proto; 9]) {
|
||||
let taken: Vec<Box<VanillaProto>> = region
|
||||
.iter_mut()
|
||||
.map(|p| {
|
||||
std::mem::replace(p, Box::new(()))
|
||||
.downcast::<VanillaProto>()
|
||||
.expect("vanilla proto")
|
||||
})
|
||||
.collect();
|
||||
let all_ready = taken.iter().all(|p| matches!(**p, VanillaProto::Ready(_)));
|
||||
if all_ready {
|
||||
let chunks: Vec<ProtoChunk> = taken
|
||||
.into_iter()
|
||||
.map(|p| match *p {
|
||||
VanillaProto::Ready(c) => *c,
|
||||
VanillaProto::Failed(..) => unreachable!(),
|
||||
})
|
||||
.collect();
|
||||
let mut array: [ProtoChunk; 9] = match chunks.try_into() {
|
||||
Ok(a) => a,
|
||||
Err(_) => unreachable!(),
|
||||
};
|
||||
if let Err(e) = worker.decorate(&mut array) {
|
||||
eprintln!("Horizon: decorating chunk failed: {e}");
|
||||
}
|
||||
for (slot, chunk) in region.iter_mut().zip(array) {
|
||||
*slot = Box::new(VanillaProto::Ready(Box::new(chunk)));
|
||||
}
|
||||
} else {
|
||||
for (slot, p) in region.iter_mut().zip(taken) {
|
||||
*slot = p;
|
||||
}
|
||||
}
|
||||
}
|
||||
fn finish(&self, proto: Proto) -> Chunk {
|
||||
let proto = proto.downcast::<VanillaProto>().expect("vanilla proto");
|
||||
let chunk = match *proto {
|
||||
VanillaProto::Ready(c) => c,
|
||||
VanillaProto::Failed(x, z) => return Chunk::empty(x, z),
|
||||
};
|
||||
let pos = chunk.position();
|
||||
let mut out = Chunk::empty(pos.x, pos.z);
|
||||
let (bx, bz) = (pos.x * 16, pos.z * 16);
|
||||
for y in MIN_Y..MIN_Y + HEIGHT {
|
||||
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 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 id = chunk.biome_id_at(bx + qx * 4, MIN_Y + qy * 4, 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
out.biomes = Arc::new(biomes);
|
||||
for s in &mut out.sections {
|
||||
Arc::make_mut(s).compact();
|
||||
}
|
||||
out
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user