361c994d98
Vanilla draws a section when its column is within the render distance of the camera's column with a column of slack on each axis (SectionOcclusionGraph.isInViewDistance → ChunkTrackingView): 533 columns at render distance 12. Horizon meshed a circle of radius 12 (441 columns) and drew sections whose centre lay within 16 x distance + 24 blocks, so the edge of the world, mostly in fog, missed columns vanilla shows, and fell short of vanilla's corners. `streaming::in_view_distance` is vanilla's test. Streaming meshes the columns it accepts around the player (and forced ones), and the renderer's column walk draws the ones it accepts around the camera. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1735 lines
72 KiB
Rust
1735 lines
72 KiB
Rust
//! Chunk streaming: a worker pool generates, lights and meshes chunk columns
|
|
//! around the player, closest first. The game thread owns the world and only
|
|
//! hands immutable `Arc` snapshots to workers; results carry a sequence number
|
|
//! so stale work is dropped.
|
|
use crate::fast_hash::{FxHashMap, FxHashSet};
|
|
use crate::{
|
|
chunk::{Chunk, SECTIONS},
|
|
light::{LightTables, apply_lighting, apply_relighting, light_chunk},
|
|
mesher::{MeshTables, Mesher, Region, SectionMesh, borrow},
|
|
world::World,
|
|
};
|
|
use horizon_core::biome::BiomeZoom;
|
|
use horizon_world::block_render::FaceVisibility;
|
|
use horizon_world::worldgen::decoration_order::DecorationOrder;
|
|
use std::{
|
|
collections::{BinaryHeap, HashSet},
|
|
sync::{
|
|
Arc, Condvar, Mutex, RwLock,
|
|
atomic::{AtomicBool, Ordering},
|
|
mpsc,
|
|
},
|
|
thread,
|
|
};
|
|
|
|
/// 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;
|
|
/// 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;
|
|
/// Whether `proto` has been decorated (a saved one can come back decorated).
|
|
fn is_decorated(&self, _proto: &Proto) -> bool {
|
|
false
|
|
}
|
|
/// What to save of a proto chunk dropped before it became final.
|
|
fn saved_proto(&self, _proto: &Proto) -> Option<crate::save::SavedProto> {
|
|
None
|
|
}
|
|
/// Put a saved proto chunk's state back onto `proto`, generated again.
|
|
fn restore_proto(&self, _proto: &mut Proto, _saved: crate::save::SavedProto) {}
|
|
/// Save proto chunks decoration wrote to, so that they come back as they
|
|
/// were: generating them again would lose those writes.
|
|
fn save_protos(&self, _protos: &[&Proto]) {}
|
|
/// Save a column that finished after the player moved away: its
|
|
/// neighbours' decoration is done, so it can't be generated again.
|
|
fn save_finished(&self, _chunk: Chunk) {}
|
|
}
|
|
/// The generator's saving, for the streamer (which doesn't know its type).
|
|
trait Saves: Send + Sync {
|
|
fn save_protos(&self, protos: &[&Proto]);
|
|
fn save_finished(&self, chunk: Chunk);
|
|
}
|
|
impl<G: ChunkGenerator> Saves for G {
|
|
fn save_protos(&self, protos: &[&Proto]) {
|
|
ChunkGenerator::save_protos(self, protos);
|
|
}
|
|
fn save_finished(&self, chunk: Chunk) {
|
|
ChunkGenerator::save_finished(self, 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,
|
|
cull_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, MeshSettings),
|
|
/// Proto chunks checked out to be saved (`ChunkGenerator::save_protos`),
|
|
/// off the main thread like vanilla's IOWorker.
|
|
Save(Vec<((i32, i32), Proto)>),
|
|
}
|
|
pub enum Done {
|
|
Generated(Chunk),
|
|
/// A proto chunk, and whether it was decorated (loaded so from the save).
|
|
Proto(i32, i32, Proto, bool),
|
|
Decorated(i32, i32, Box<[Proto; 9]>),
|
|
Lit(i32, i32, Vec<crate::chunk::Light>, u64),
|
|
Meshed(i32, i32, i32, SectionMesh, u64),
|
|
Saved(Vec<((i32, i32), Proto)>),
|
|
}
|
|
struct Queued {
|
|
priority: i64,
|
|
order: u64,
|
|
job: Job,
|
|
}
|
|
impl PartialEq for Queued {
|
|
fn eq(&self, o: &Self) -> bool {
|
|
self.priority == o.priority && self.order == o.order
|
|
}
|
|
}
|
|
impl Eq for Queued {}
|
|
impl PartialOrd for Queued {
|
|
fn partial_cmp(&self, o: &Self) -> Option<std::cmp::Ordering> {
|
|
Some(self.cmp(o))
|
|
}
|
|
}
|
|
impl Ord for Queued {
|
|
fn cmp(&self, o: &Self) -> std::cmp::Ordering {
|
|
// BinaryHeap is a max-heap: lower priority value = more urgent.
|
|
o.priority
|
|
.cmp(&self.priority)
|
|
.then(o.order.cmp(&self.order))
|
|
}
|
|
}
|
|
struct Shared {
|
|
queue: Mutex<BinaryHeap<Queued>>,
|
|
ready: Condvar,
|
|
stop: AtomicBool,
|
|
/// What meshing reads (block models and sprites), swapped when resource
|
|
/// packs reload; each mesh job takes the current one.
|
|
context: RwLock<Arc<WorkerContext>>,
|
|
}
|
|
pub struct Streaming {
|
|
shared: Arc<Shared>,
|
|
results: mpsc::Receiver<Done>,
|
|
threads: Vec<thread::JoinHandle<()>>,
|
|
order: u64,
|
|
generating: FxHashSet<(i32, i32)>,
|
|
generation_scan: GenerationScan,
|
|
/// Proto chunks: `None` while checked out to a decoration or save job.
|
|
protos: FxHashMap<(i32, i32), Option<Proto>>,
|
|
decorated: FxHashSet<(i32, i32)>,
|
|
decorating: usize,
|
|
/// Save jobs in flight.
|
|
saving: usize,
|
|
/// Vanilla's decoration order around the player, and the centre and
|
|
/// radius it was last ranked for.
|
|
decoration_order: DecorationOrder,
|
|
order_area: Option<((i32, i32), i32)>,
|
|
finishing: FxHashSet<(i32, i32)>,
|
|
decorates: bool,
|
|
lighting: FxHashMap<(i32, i32), u64>,
|
|
meshing: FxHashMap<(i32, i32, i32), u64>,
|
|
/// Chunks whose light is current.
|
|
lit: FxHashSet<(i32, i32)>,
|
|
/// Columns whose sections have been meshed at least once.
|
|
meshed: FxHashSet<(i32, i32)>,
|
|
/// Dirty sections of loaded columns past the render distance, meshed once
|
|
/// the player comes closer, and the area they were set aside for.
|
|
waiting: FxHashSet<(i32, i32, i32)>,
|
|
waiting_area: ((i32, i32), i32, usize),
|
|
/// Proto chunks decoration wrote to since they were generated or saved.
|
|
written: FxHashSet<(i32, i32)>,
|
|
saves: Arc<dyn Saves>,
|
|
/// Results received while `save` waited, for the next `update`.
|
|
deferred: Vec<Done>,
|
|
sequence: u64,
|
|
pub center: (i32, i32),
|
|
pub radius: i32,
|
|
/// `/forceload`ed chunks: kept loaded whatever the distance.
|
|
pub forced: HashSet<(i32, i32)>,
|
|
/// No work is scheduled while paused (title screen).
|
|
pub paused: bool,
|
|
/// 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,
|
|
pub cull_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.
|
|
light_log: Vec<(u64, (i32, i32, i32))>,
|
|
light_edit_sequence: u64,
|
|
/// Edit sequence at each in-flight light job's snapshot.
|
|
light_snapshots: FxHashMap<(i32, i32), u64>,
|
|
pub generated_total: u64,
|
|
pub meshed_total: u64,
|
|
/// Chunks come from a server (`insert_remote`/`forget`): nothing is
|
|
/// generated and columns are only unloaded when the server says so.
|
|
pub remote: bool,
|
|
/// More areas to keep loaded (centre chunk, radius), e.g. around guests
|
|
/// of a world open to the network. They are generated and kept but not meshed.
|
|
pub extra: Vec<((i32, i32), i32)>,
|
|
/// Mesh the sections for drawing (false on a server thread).
|
|
pub mesh: bool,
|
|
}
|
|
/// Snapshot only after a scan found no missing candidates. Public range fields
|
|
/// and `World::chunks` can change without going through Streaming, so compare
|
|
/// their values and structural membership token rather than a frame timeout.
|
|
#[derive(Default)]
|
|
struct GenerationScan {
|
|
exhausted: bool,
|
|
center: (i32, i32),
|
|
radius: i32,
|
|
extra: Vec<((i32, i32), i32)>,
|
|
forced: HashSet<(i32, i32)>,
|
|
chunk_revision: u64,
|
|
}
|
|
impl GenerationScan {
|
|
fn unchanged(&self, ranges: &Ranges<'_>, world: &World) -> bool {
|
|
self.exhausted
|
|
&& self.center == ranges.center
|
|
&& self.radius == ranges.radius
|
|
&& self.extra == ranges.extra
|
|
&& self.forced == *ranges.forced
|
|
&& self.chunk_revision == world.chunks.membership_revision()
|
|
}
|
|
fn remember(&mut self, ranges: Ranges<'_>, world: &World) {
|
|
self.exhausted = true;
|
|
self.center = ranges.center;
|
|
self.radius = ranges.radius;
|
|
self.extra.clear();
|
|
self.extra.extend_from_slice(ranges.extra);
|
|
self.forced.clone_from(ranges.forced);
|
|
self.chunk_revision = world.chunks.membership_revision();
|
|
}
|
|
}
|
|
struct GenerationInputs<'a> {
|
|
ranges: Ranges<'a>,
|
|
world: &'a World,
|
|
generating: &'a FxHashSet<(i32, i32)>,
|
|
protos: &'a FxHashMap<(i32, i32), Option<Proto>>,
|
|
finishing: &'a FxHashSet<(i32, i32)>,
|
|
decorates: bool,
|
|
remote: bool,
|
|
}
|
|
impl GenerationScan {
|
|
/// None means the scan was skipped; Some preserves the original candidate
|
|
/// ordering and bounded batch, including an empty exhausted scan.
|
|
fn candidates(
|
|
&mut self,
|
|
input: GenerationInputs<'_>,
|
|
capacity: usize,
|
|
) -> Option<Vec<(i64, i32, i32)>> {
|
|
// Generation (load or proto), closest first, bounded in flight.
|
|
let (margin, reach) = Ranges::margins(input.decorates);
|
|
if input.remote
|
|
|| input.generating.len() >= capacity
|
|
|| self.unchanged(&input.ranges, input.world)
|
|
{
|
|
return None;
|
|
}
|
|
{
|
|
let mut wanted = Vec::new();
|
|
let areas: Vec<((i32, i32), i32)> =
|
|
std::iter::once((input.ranges.center, input.ranges.radius))
|
|
.chain(input.ranges.extra.iter().copied())
|
|
.collect();
|
|
for (center, radius) in areas {
|
|
let r = radius + margin.max(reach + 1);
|
|
for dz in -r..=r {
|
|
for dx in -r..=r {
|
|
let (x, z) = (center.0 + dx, center.1 + dz);
|
|
if !input.ranges.requested(x, z, input.decorates)
|
|
|| input.world.chunks.contains_key(&(x, z))
|
|
|| input.generating.contains(&(x, z))
|
|
|| input.protos.contains_key(&(x, z))
|
|
|| input.finishing.contains(&(x, z))
|
|
{
|
|
continue;
|
|
}
|
|
wanted.push((input.ranges.distance(x, z), x, z));
|
|
}
|
|
}
|
|
}
|
|
// Forced chunks and their generation neighbourhood.
|
|
let reach = margin - 1;
|
|
for &(fx, fz) in input.ranges.forced {
|
|
for dz in -reach..=reach {
|
|
for dx in -reach..=reach {
|
|
let (x, z) = (fx + dx, fz + dz);
|
|
if input.world.chunks.contains_key(&(x, z))
|
|
|| input.generating.contains(&(x, z))
|
|
|| input.protos.contains_key(&(x, z))
|
|
|| input.finishing.contains(&(x, z))
|
|
{
|
|
continue;
|
|
}
|
|
wanted.push((input.ranges.distance(x, z), x, z));
|
|
}
|
|
}
|
|
}
|
|
if wanted.is_empty() {
|
|
let ranges = Ranges {
|
|
center: input.ranges.center,
|
|
radius: input.ranges.radius,
|
|
extra: input.ranges.extra,
|
|
forced: input.ranges.forced,
|
|
};
|
|
self.remember(ranges, input.world);
|
|
} else {
|
|
// A bounded batch may leave candidates behind. Keep scanning
|
|
// as capacity opens, even if no range or membership changed.
|
|
self.exhausted = false;
|
|
}
|
|
wanted.sort_unstable();
|
|
wanted.dedup();
|
|
wanted.truncate(capacity - input.generating.len());
|
|
Some(wanted)
|
|
}
|
|
}
|
|
}
|
|
pub struct WorkerContext {
|
|
pub assets: Arc<crate::assets::Assets>,
|
|
pub tables: Arc<MeshTables>,
|
|
pub biomes: Arc<crate::biome::Biomes>,
|
|
pub light: Arc<LightTables>,
|
|
}
|
|
impl Streaming {
|
|
pub fn new<G: ChunkGenerator>(
|
|
generator: Arc<G>,
|
|
context: Arc<WorkerContext>,
|
|
threads: usize,
|
|
) -> Self {
|
|
let shared = Arc::new(Shared {
|
|
queue: Mutex::new(BinaryHeap::new()),
|
|
ready: Condvar::new(),
|
|
stop: AtomicBool::new(false),
|
|
context: RwLock::new(context.clone()),
|
|
});
|
|
let (send, results) = mpsc::channel();
|
|
let light_tables = context.light.clone();
|
|
let saves: Arc<dyn Saves> = generator.clone();
|
|
let threads = (0..threads.max(1))
|
|
.map(|i| {
|
|
let shared = shared.clone();
|
|
let send = send.clone();
|
|
let generator = generator.clone();
|
|
let light = context.light.clone();
|
|
thread::Builder::new()
|
|
.name(format!("horizon-worker-{i}"))
|
|
.spawn(move || {
|
|
let mut worker = generator.worker();
|
|
// The face visibility table and its occlusion cache stay with
|
|
// the worker across mesh jobs, like vanilla's per-thread cache:
|
|
// cloning it per job copied every state and face shape.
|
|
let mut faces: Option<(Arc<MeshTables>, FaceVisibility)> = None;
|
|
loop {
|
|
let job = {
|
|
let mut queue = shared.queue.lock().unwrap();
|
|
loop {
|
|
if shared.stop.load(Ordering::Relaxed) {
|
|
return;
|
|
}
|
|
if let Some(job) = queue.pop() {
|
|
break job.job;
|
|
}
|
|
queue = shared.ready.wait(queue).unwrap();
|
|
}
|
|
};
|
|
let done = match job {
|
|
Job::Generate(x, z) => match generator.load(x, z) {
|
|
Some(chunk) => Done::Generated(chunk),
|
|
None => {
|
|
let proto = generator.proto(&mut worker, x, z);
|
|
let decorated = generator.is_decorated(&proto);
|
|
Done::Proto(x, z, proto, decorated)
|
|
}
|
|
},
|
|
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::Save(protos) => {
|
|
let refs: Vec<&Proto> = protos.iter().map(|(_, p)| p).collect();
|
|
generator.save_protos(&refs);
|
|
Done::Saved(protos)
|
|
}
|
|
Job::Light(x, z, region, seq, sky) => {
|
|
Done::Lit(x, z, light_chunk(&borrow(®ion), &light, sky), seq)
|
|
}
|
|
Job::Mesh(x, sy, z, region, seq, settings) => {
|
|
let context = shared.context.read().unwrap().clone();
|
|
let visibility = match faces.take() {
|
|
Some((tables, visibility))
|
|
if Arc::ptr_eq(&tables, &context.tables) =>
|
|
{
|
|
visibility
|
|
}
|
|
_ => context.tables.visibility.clone(),
|
|
};
|
|
let mut mesher = Mesher {
|
|
nether_shading: settings.nether,
|
|
zoom: settings.zoom,
|
|
blend_radius: settings.blend_radius,
|
|
smooth_lighting: settings.smooth_lighting,
|
|
cutout_leaves: settings.cutout_leaves,
|
|
cull_leaves: settings.cull_leaves,
|
|
assets: &context.assets,
|
|
tables: &context.tables,
|
|
biomes: &context.biomes,
|
|
visibility,
|
|
};
|
|
let mesh = mesher.mesh(&borrow(®ion), sy);
|
|
faces = Some((context.tables.clone(), mesher.visibility));
|
|
Done::Meshed(x, sy, z, mesh, seq)
|
|
}
|
|
};
|
|
if send.send(done).is_err() {
|
|
return;
|
|
}
|
|
}
|
|
})
|
|
.expect("spawn worker")
|
|
})
|
|
.collect();
|
|
Self {
|
|
shared,
|
|
results,
|
|
threads,
|
|
order: 0,
|
|
generating: Default::default(),
|
|
generation_scan: GenerationScan::default(),
|
|
protos: Default::default(),
|
|
decorated: Default::default(),
|
|
decorating: 0,
|
|
saving: 0,
|
|
decoration_order: DecorationOrder::default(),
|
|
order_area: None,
|
|
finishing: Default::default(),
|
|
decorates: generator.decorates(),
|
|
lighting: Default::default(),
|
|
meshing: Default::default(),
|
|
lit: Default::default(),
|
|
meshed: Default::default(),
|
|
waiting: Default::default(),
|
|
waiting_area: ((0, 0), 0, 0),
|
|
written: Default::default(),
|
|
saves,
|
|
deferred: Vec::new(),
|
|
sequence: 0,
|
|
center: (0, 0),
|
|
radius: 8,
|
|
forced: HashSet::new(),
|
|
paused: false,
|
|
sky_light: true,
|
|
nether_shading: false,
|
|
biome_zoom: BiomeZoom::new(0),
|
|
biome_blend: 2,
|
|
smooth_lighting: true,
|
|
cutout_leaves: true,
|
|
cull_leaves: false,
|
|
light_tables,
|
|
light_log: Vec::new(),
|
|
light_edit_sequence: 0,
|
|
light_snapshots: Default::default(),
|
|
generated_total: 0,
|
|
meshed_total: 0,
|
|
remote: false,
|
|
extra: Vec::new(),
|
|
mesh: true,
|
|
}
|
|
}
|
|
/// Resource packs reloaded (`LevelRenderer.allChanged`): mesh with the new
|
|
/// models and sprites, and rebuild every loaded section. Meshes in flight
|
|
/// were made with the old resources; their results are dropped.
|
|
pub fn reload_resources(&mut self, world: &mut World, context: Arc<WorkerContext>) {
|
|
*self.shared.context.write().unwrap() = context;
|
|
self.meshing.clear();
|
|
for &(x, z) in world.chunks.keys() {
|
|
if self.lit.contains(&(x, z)) {
|
|
for sy in 0..SECTIONS as i32 {
|
|
world.dirty_sections.insert((x, sy, z));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
/// A column received from a server: store it and relight it and its neighbours.
|
|
pub fn insert_remote(&mut self, world: &mut World, chunk: Chunk) {
|
|
let key = (chunk.x, chunk.z);
|
|
self.lit.remove(&key);
|
|
self.lighting.remove(&key);
|
|
world.chunks.insert(key, Arc::new(chunk));
|
|
self.generated_total += 1;
|
|
for dz in -1..=1 {
|
|
for dx in -1..=1 {
|
|
world.dirty_light.insert((key.0 + dx, key.1 + dz));
|
|
}
|
|
}
|
|
}
|
|
/// A column the server unloaded (ClientboundForgetLevelChunkPacket).
|
|
pub fn forget(&mut self, world: &mut World, x: i32, z: i32) -> Option<Arc<Chunk>> {
|
|
self.lit.remove(&(x, z));
|
|
self.meshed.remove(&(x, z));
|
|
self.lighting.remove(&(x, z));
|
|
self.meshing.retain(|k, _| (k.0, k.2) != (x, z));
|
|
self.waiting.retain(|k| (k.0, k.2) != (x, z));
|
|
world.dirty_sections.retain(|k| (k.0, k.2) != (x, z));
|
|
world.chunks.remove(&(x, z))
|
|
}
|
|
fn submit(&mut self, priority: i64, job: Job) {
|
|
self.order += 1;
|
|
self.shared.queue.lock().unwrap().push(Queued {
|
|
priority,
|
|
order: self.order,
|
|
job,
|
|
});
|
|
self.shared.ready.notify_one();
|
|
}
|
|
fn receive_decorated(&mut self, x: i32, z: i32, region: Box<[Proto; 9]>) {
|
|
self.generation_scan.exhausted = false;
|
|
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));
|
|
self.written.insert(key);
|
|
}
|
|
}
|
|
/// Saved proto chunks come back; those now out of range are dropped by
|
|
/// the next `update`.
|
|
fn receive_saved(&mut self, protos: Vec<((i32, i32), Proto)>) {
|
|
self.generation_scan.exhausted = false;
|
|
self.saving -= 1;
|
|
for (key, proto) in protos {
|
|
self.protos.insert(key, Some(proto));
|
|
}
|
|
}
|
|
/// Check out the proto chunks at `keys` and save them on a worker.
|
|
fn save_checked_out(&mut self, keys: impl IntoIterator<Item = (i32, i32)>) {
|
|
let protos: Vec<((i32, i32), Proto)> = keys
|
|
.into_iter()
|
|
.filter_map(|k| Some((k, self.protos.get_mut(&k)?.take()?)))
|
|
.collect();
|
|
if !protos.is_empty() {
|
|
self.saving += 1;
|
|
// Ahead of every other job: a column generated again meanwhile
|
|
// would not find them saved.
|
|
self.submit(i64::MIN, Job::Save(protos));
|
|
}
|
|
}
|
|
fn receive_generated(&mut self, mut chunk: Chunk, world: &mut World) {
|
|
self.generation_scan.exhausted = false;
|
|
let key = (chunk.x, chunk.z);
|
|
self.generating.remove(&key);
|
|
self.finishing.remove(&key);
|
|
self.decorated.insert(key);
|
|
// A column that finished after the player moved away is dropped
|
|
// whole: its block entities and saved entities too, or they would
|
|
// stay in the world (and the entities spawn again, duplicated, when
|
|
// the column loads later). A generated one is saved first: its
|
|
// neighbours were decorated around it, so generating it again would
|
|
// not give it back.
|
|
if !self.ranges().chunk_kept(key.0, key.1, self.decorates) {
|
|
if chunk.fresh {
|
|
self.saves.save_finished(chunk);
|
|
}
|
|
return;
|
|
}
|
|
// Generated columns are saved like edited ones, as vanilla saves
|
|
// every chunk it generates.
|
|
if chunk.fresh {
|
|
world.modified.insert(key);
|
|
}
|
|
world
|
|
.block_entities
|
|
.extend(std::mem::take(&mut chunk.block_entities));
|
|
world
|
|
.block_names
|
|
.extend(std::mem::take(&mut chunk.block_names));
|
|
let entities = std::mem::take(&mut chunk.entities);
|
|
if !entities.is_empty() {
|
|
world.loaded_entities.push(((chunk.x, chunk.z), entities));
|
|
}
|
|
self.generated_total += 1;
|
|
// A chunk saved with its light needs no light job; its unlit
|
|
// neighbours still light themselves from it.
|
|
let loaded = chunk.light_loaded;
|
|
world.chunks.insert(key, Arc::new(chunk));
|
|
if loaded {
|
|
self.lit.insert(key);
|
|
for sy in 0..SECTIONS as i32 {
|
|
world.dirty_sections.insert((key.0, sy, key.1));
|
|
}
|
|
}
|
|
for dz in -1..=1 {
|
|
for dx in -1..=1 {
|
|
let n = (key.0 + dx, key.1 + dz);
|
|
if !loaded || world.chunks.get(&n).is_some_and(|c| !c.light_loaded) {
|
|
world.dirty_light.insert(n);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
fn distance(&self, x: i32, z: i32) -> i64 {
|
|
self.ranges().distance(x, z)
|
|
}
|
|
fn region(world: &World, x: i32, z: i32) -> Option<Box<Region>> {
|
|
let mut region: Region = Default::default();
|
|
for dz in 0..3 {
|
|
for dx in 0..3 {
|
|
region[dz][dx] = Some(
|
|
world
|
|
.chunks
|
|
.get(&(x + dx as i32 - 1, z + dz as i32 - 1))?
|
|
.clone(),
|
|
);
|
|
}
|
|
}
|
|
Some(Box::new(region))
|
|
}
|
|
pub fn is_lit(&self, x: i32, z: i32) -> bool {
|
|
self.lit.contains(&(x, z))
|
|
}
|
|
/// `ChunkMap.getLatestStatus` for the loading screen's chunk map: the
|
|
/// furthest step this column has reached, `None` before it is asked for.
|
|
pub fn latest_status(
|
|
&self,
|
|
world: &World,
|
|
x: i32,
|
|
z: i32,
|
|
) -> Option<crate::level_loading::Status> {
|
|
use crate::level_loading::Status;
|
|
let key = (x, z);
|
|
Some(if self.lit.contains(&key) {
|
|
Status::Full
|
|
} else if world.chunks.contains_key(&key) {
|
|
Status::InitializeLight
|
|
} else if self.decorated.contains(&key) || self.finishing.contains(&key) {
|
|
Status::Features
|
|
} else if self.protos.contains_key(&key) {
|
|
Status::Terrain
|
|
} else if self.generating.contains(&key) {
|
|
Status::Empty
|
|
} else {
|
|
return None;
|
|
})
|
|
}
|
|
pub fn is_meshing(&self, section: (i32, i32, i32)) -> bool {
|
|
self.meshing.contains_key(§ion)
|
|
}
|
|
pub fn pending(&self) -> usize {
|
|
self.shared.queue.lock().unwrap().len()
|
|
}
|
|
/// How much work each stage holds: queued jobs, then what is in flight or
|
|
/// waiting at each step from generation to meshing.
|
|
pub fn stages(&self) -> [(&'static str, usize); 7] {
|
|
[
|
|
("queued", self.pending()),
|
|
("generating", self.generating.len()),
|
|
(
|
|
"protos",
|
|
self.protos.values().filter(|p| p.is_some()).count(),
|
|
),
|
|
("decorating", self.decorating),
|
|
("finishing", self.finishing.len()),
|
|
("lighting", self.lighting.len()),
|
|
("meshing", self.meshing.len()),
|
|
]
|
|
}
|
|
/// Advance streaming: collect finished work, then schedule more.
|
|
/// Returns meshes ready for upload and columns that were unloaded.
|
|
pub fn update(
|
|
&mut self,
|
|
world: &mut World,
|
|
) -> (Vec<((i32, i32, i32), SectionMesh)>, Vec<Arc<Chunk>>) {
|
|
let mut uploads = Vec::new();
|
|
if self.paused {
|
|
return (uploads, Vec::new());
|
|
}
|
|
self.relight_edits(world);
|
|
let deferred = std::mem::take(&mut self.deferred);
|
|
let received = std::iter::from_fn(|| self.results.try_recv().ok());
|
|
for done in deferred.into_iter().chain(received.collect::<Vec<_>>()) {
|
|
match done {
|
|
Done::Proto(x, z, proto, decorated) => {
|
|
self.generation_scan.exhausted = false;
|
|
self.generating.remove(&(x, z));
|
|
if self.in_range(x, z, 4) || self.near_finished(x, z, 2) {
|
|
self.protos.insert((x, z), Some(proto));
|
|
if decorated || !self.decorates {
|
|
self.decorated.insert((x, z));
|
|
}
|
|
}
|
|
}
|
|
Done::Decorated(x, z, region) => self.receive_decorated(x, z, region),
|
|
Done::Generated(chunk) => self.receive_generated(chunk, world),
|
|
Done::Saved(protos) => self.receive_saved(protos),
|
|
Done::Lit(x, z, sections, seq) => {
|
|
if self.lighting.get(&(x, z)) != Some(&seq) {
|
|
continue;
|
|
}
|
|
self.lighting.remove(&(x, z));
|
|
let snapshot = self.light_snapshots.remove(&(x, z)).unwrap_or(u64::MAX);
|
|
if let Some(chunk) = world.chunks.get_mut(&(x, z)) {
|
|
let chunk = Arc::make_mut(chunk);
|
|
// Keep blocks edited since the snapshot; take only light.
|
|
// A chunk relit (a neighbour loaded or changed) remeshes
|
|
// only around the light that changed, as vanilla's light
|
|
// updates dirty only their sections (#270).
|
|
let first = self.lit.insert((x, z));
|
|
let changed = if first {
|
|
apply_lighting(chunk, sections);
|
|
Vec::new()
|
|
} else {
|
|
apply_relighting(chunk, sections)
|
|
};
|
|
chunk.lit = true;
|
|
if first {
|
|
for dz in -1..=1 {
|
|
for dx in -1..=1 {
|
|
for sy in 0..SECTIONS as i32 {
|
|
world.dirty_sections.insert((x + dx, sy, z + dz));
|
|
}
|
|
}
|
|
}
|
|
} else {
|
|
Self::mark_light_changes(world, &changed);
|
|
}
|
|
self.replay_light_edits(world, x, z, snapshot);
|
|
}
|
|
}
|
|
Done::Meshed(x, sy, z, mesh, seq) => {
|
|
if self.meshing.get(&(x, sy, z)) != Some(&seq) {
|
|
continue;
|
|
}
|
|
// Its column unloaded while it was meshed: nothing would
|
|
// ever remove the uploaded section.
|
|
if !world.chunks.contains_key(&(x, z)) {
|
|
self.meshing.remove(&(x, sy, z));
|
|
continue;
|
|
}
|
|
self.meshing.remove(&(x, sy, z));
|
|
self.meshed_total += 1;
|
|
uploads.push(((x, sy, z), mesh));
|
|
}
|
|
}
|
|
}
|
|
// Unload far columns.
|
|
let far: Vec<_> = world
|
|
.chunks
|
|
.keys()
|
|
.copied()
|
|
.filter(|&(x, z)| !self.remote && self.ranges().unloaded(x, z, self.decorates))
|
|
.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) {
|
|
removed.push(chunk);
|
|
}
|
|
self.lit.remove(key);
|
|
self.meshed.remove(key);
|
|
self.lighting.remove(key);
|
|
self.light_snapshots.remove(key);
|
|
}
|
|
if !far.is_empty() {
|
|
let far: HashSet<(i32, i32)> = far.iter().copied().collect();
|
|
self.meshing.retain(|k, _| !far.contains(&(k.0, k.2)));
|
|
self.waiting.retain(|k| !far.contains(&(k.0, k.2)));
|
|
world.dirty_sections.retain(|k| !far.contains(&(k.0, k.2)));
|
|
}
|
|
// Drop far proto chunks that are not checked out.
|
|
let areas: Vec<((i32, i32), i64)> =
|
|
std::iter::once((self.center, i64::from(self.radius + 4)))
|
|
.chain(self.extra.iter().map(|&(c, r)| (c, i64::from(r + 4))))
|
|
.collect();
|
|
let forced = &self.forced;
|
|
let far: Vec<(i32, i32)> = self
|
|
.protos
|
|
.iter()
|
|
.filter(|&(&(x, z), p)| {
|
|
p.is_some()
|
|
&& !areas.iter().any(|&((cx, cz), r4)| {
|
|
i64::from(x - cx).pow(2) + i64::from(z - cz).pow(2) <= r4 * r4
|
|
})
|
|
&& !forced
|
|
.iter()
|
|
.any(|f| (x - f.0).abs() <= 3 && (z - f.1).abs() <= 3)
|
|
})
|
|
.map(|(k, _)| *k)
|
|
.collect();
|
|
if !far.is_empty() {
|
|
self.generation_scan.exhausted = false;
|
|
}
|
|
// Those decoration wrote to are saved first (see
|
|
// `ChunkGenerator::save_protos`), and dropped once back.
|
|
let mut written = Vec::new();
|
|
for key in far {
|
|
if self.written.remove(&key) {
|
|
written.push(key);
|
|
} else {
|
|
self.protos.remove(&key);
|
|
}
|
|
}
|
|
self.save_checked_out(written);
|
|
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.written.remove(&(x, z));
|
|
self.submit(self.distance(x, z) * 4, Job::Finish(proto));
|
|
}
|
|
}
|
|
// Decorate chunks whose 3x3 proto neighbourhood is available, in
|
|
// vanilla's order: decoration is order dependent where write regions
|
|
// overlap (see `DecorationOrder`).
|
|
let workers = self.threads.len();
|
|
if self.decorates && self.order_area != Some((self.center, self.radius)) {
|
|
let (decorated, chunks) = (&self.decorated, &world.chunks);
|
|
// The render distance is the server view distance in singleplayer.
|
|
self.decoration_order
|
|
.reassign(self.center, self.radius, |x, z| {
|
|
decorated.contains(&(x, z)) || chunks.contains_key(&(x, z))
|
|
});
|
|
self.order_area = Some((self.center, self.radius));
|
|
let ranges = Ranges {
|
|
center: self.center,
|
|
radius: self.radius,
|
|
extra: &self.extra,
|
|
forced: &self.forced,
|
|
};
|
|
let (protos, generating) = (&self.protos, &self.generating);
|
|
self.decoration_order.retain(|x, z| {
|
|
ranges.in_range(x, z, 4)
|
|
|| ranges.near_finished(x, z, 2)
|
|
|| protos.contains_key(&(x, z))
|
|
|| generating.contains(&(x, z))
|
|
});
|
|
}
|
|
if self.decorates && self.decorating < workers * 2 {
|
|
let wants = |s: &Self, x: i32, z: i32| {
|
|
!s.decorated.contains(&(x, z))
|
|
&& !world.chunks.contains_key(&(x, z))
|
|
&& (s.in_range(x, z, 2) || s.near_finished(x, z, 1))
|
|
};
|
|
// A wanted chunk holds back higher-ranked ones only if it can be
|
|
// decorated: its 3x3 neighbourhood is (or will be) generated as
|
|
// protos, none of them already final.
|
|
let waits_for = |s: &Self, x: i32, z: i32| {
|
|
wants(s, x, z)
|
|
&& (-1..=1).all(|dz| {
|
|
(-1..=1).all(|dx| {
|
|
let n = (x + dx, z + dz);
|
|
!world.chunks.contains_key(&n)
|
|
&& (s.protos.contains_key(&n)
|
|
|| s.generating.contains(&n)
|
|
|| s.ranges().requested(n.0, n.1, true))
|
|
})
|
|
})
|
|
};
|
|
let keys: Vec<(i32, i32)> = self
|
|
.protos
|
|
.iter()
|
|
.filter(|(k, p)| p.is_some() && wants(self, k.0, k.1))
|
|
.map(|(k, _)| *k)
|
|
.collect();
|
|
let mut wanted: Vec<(u64, i64, i32, i32)> = keys
|
|
.into_iter()
|
|
.map(|(x, z)| (0, self.distance(x, z), x, z))
|
|
.collect();
|
|
// Chunks no ticket reached (forced and extra areas) go last, nearest first.
|
|
wanted.sort_unstable_by_key(|w| w.1);
|
|
for w in &mut wanted {
|
|
w.0 = self.decoration_order.rank_or_append(w.2, w.3);
|
|
}
|
|
wanted.sort_unstable();
|
|
for (_, d, x, z) in wanted {
|
|
if self.decorating >= workers * 2 {
|
|
break;
|
|
}
|
|
if self
|
|
.decoration_order
|
|
.blocked(x, z, |nx, nz| waits_for(self, nx, nz))
|
|
{
|
|
continue;
|
|
}
|
|
let keys: Vec<(i32, i32)> =
|
|
(0..9).map(|i| (x + (i % 3) - 1, z + (i / 3) - 1)).collect();
|
|
// A final neighbour cannot receive writes: skip decoration. Only
|
|
// worlds saved before proto chunks were saved get here (their
|
|
// final chunks' neighbours are generated again).
|
|
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));
|
|
}
|
|
}
|
|
self.schedule_generation(world, workers);
|
|
// Lighting once all neighbours exist.
|
|
let light: Vec<_> = world.dirty_light.iter().copied().collect();
|
|
for (x, z) in light {
|
|
if !world.chunks.contains_key(&(x, z)) {
|
|
world.dirty_light.remove(&(x, z));
|
|
continue;
|
|
}
|
|
let Some(region) = Self::region(world, x, z) else {
|
|
continue;
|
|
};
|
|
world.dirty_light.remove(&(x, z));
|
|
self.sequence += 1;
|
|
self.lighting.insert((x, z), self.sequence);
|
|
self.light_snapshots
|
|
.insert((x, z), self.light_edit_sequence);
|
|
self.submit(
|
|
self.distance(x, z) * 4 + 1,
|
|
Job::Light(x, z, region, self.sequence, self.sky_light),
|
|
);
|
|
}
|
|
// A server draws nothing: its sections are never meshed.
|
|
if !self.mesh {
|
|
world.dirty_sections.clear();
|
|
return (uploads, removed);
|
|
}
|
|
// Meshing once the column and its neighbours are lit, and the column
|
|
// is within the render distance (`in_view_distance`, the columns
|
|
// vanilla draws). Sections set aside past it come back
|
|
// when the player moves: a column that loads with its light gets no
|
|
// other request (#228).
|
|
let area = (self.center, self.radius, self.forced.len());
|
|
if self.waiting_area != area {
|
|
self.waiting_area = area;
|
|
let ranges = Ranges {
|
|
center: self.center,
|
|
radius: self.radius,
|
|
extra: &self.extra,
|
|
forced: &self.forced,
|
|
};
|
|
self.waiting.retain(|&(x, sy, z)| {
|
|
let back = ranges.in_view(x, z);
|
|
if back {
|
|
world.dirty_sections.insert((x, sy, z));
|
|
}
|
|
!back
|
|
});
|
|
}
|
|
let dirty: Vec<_> = world.dirty_sections.iter().copied().collect();
|
|
let mut columns: FxHashMap<(i32, i32), Option<Box<Region>>> = FxHashMap::default();
|
|
let mut ready_columns: FxHashMap<(i32, i32), bool> = FxHashMap::default();
|
|
for (x, sy, z) in dirty {
|
|
if !world.chunks.contains_key(&(x, z)) {
|
|
world.dirty_sections.remove(&(x, sy, z));
|
|
continue;
|
|
}
|
|
if !self.ranges().in_view(x, z) {
|
|
world.dirty_sections.remove(&(x, sy, z));
|
|
self.waiting.insert((x, sy, z));
|
|
continue;
|
|
}
|
|
// Once per column: the 3x3 check would otherwise repeat for each
|
|
// of its sections every frame. A neighbour being lit dirties this
|
|
// column again when its light job finishes (new chunks relight
|
|
// their eight neighbours), so wait for it rather than mesh once
|
|
// per neighbour. Columns still in `dirty_light` here can't be lit
|
|
// yet (a neighbour of theirs is missing): waiting for them could
|
|
// last until the player comes back.
|
|
let (lit, lighting) = (&self.lit, &self.lighting);
|
|
let ready = *ready_columns.entry((x, z)).or_insert_with(|| {
|
|
(-1..=1).all(|dz| {
|
|
(-1..=1).all(|dx| {
|
|
let n = (x + dx, z + dz);
|
|
lit.contains(&n) && !lighting.contains_key(&n)
|
|
})
|
|
})
|
|
});
|
|
if !ready {
|
|
continue;
|
|
}
|
|
let region = columns
|
|
.entry((x, z))
|
|
.or_insert_with(|| Self::region(world, x, z));
|
|
let Some(region) = region.clone() else {
|
|
continue;
|
|
};
|
|
world.dirty_sections.remove(&(x, sy, z));
|
|
let first = self.meshed.insert((x, z));
|
|
self.sequence += 1;
|
|
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,
|
|
MeshSettings {
|
|
nether: self.nether_shading,
|
|
zoom: self.biome_zoom,
|
|
blend_radius: self.biome_blend,
|
|
smooth_lighting: self.smooth_lighting,
|
|
cutout_leaves: self.cutout_leaves,
|
|
cull_leaves: self.cull_leaves,
|
|
},
|
|
),
|
|
);
|
|
}
|
|
(uploads, removed)
|
|
}
|
|
fn schedule_generation(&mut self, world: &World, workers: usize) {
|
|
let input = GenerationInputs {
|
|
ranges: Ranges {
|
|
center: self.center,
|
|
radius: self.radius,
|
|
extra: &self.extra,
|
|
forced: &self.forced,
|
|
},
|
|
world,
|
|
generating: &self.generating,
|
|
protos: &self.protos,
|
|
finishing: &self.finishing,
|
|
decorates: self.decorates,
|
|
remote: self.remote,
|
|
};
|
|
if let Some(wanted) = self.generation_scan.candidates(input, workers * 3) {
|
|
for (d, x, z) in wanted {
|
|
self.generating.insert((x, z));
|
|
self.submit(d * 4, Job::Generate(x, z));
|
|
}
|
|
}
|
|
}
|
|
/// Relight block edits on lit chunks in place (vanilla's incremental light
|
|
/// engine) instead of recomputing the 3x3 chunks around each edit.
|
|
fn relight_edits(&mut self, world: &mut World) {
|
|
if world.light_edits.is_empty() {
|
|
return;
|
|
}
|
|
let edits = std::mem::take(&mut world.light_edits);
|
|
let positions: Vec<(i32, i32, i32)> = edits
|
|
.iter()
|
|
.filter(|(_, old, new)| self.light_tables.differs(*old, *new))
|
|
.map(|(p, _, _)| *p)
|
|
.collect();
|
|
if positions.is_empty() {
|
|
return;
|
|
}
|
|
let changed = crate::light::relight(
|
|
&mut world.chunks,
|
|
&self.light_tables,
|
|
self.sky_light,
|
|
&positions,
|
|
false,
|
|
);
|
|
Self::mark_light_changes(world, &changed);
|
|
if !self.light_snapshots.is_empty() {
|
|
for p in positions {
|
|
self.light_edit_sequence += 1;
|
|
self.light_log.push((self.light_edit_sequence, p));
|
|
}
|
|
}
|
|
}
|
|
/// A light job computed from blocks older than some edits: redo those
|
|
/// edits on top of its result.
|
|
fn replay_light_edits(&mut self, world: &mut World, x: i32, z: i32, snapshot: u64) {
|
|
let positions: Vec<(i32, i32, i32)> = self
|
|
.light_log
|
|
.iter()
|
|
.filter(|(seq, p)| {
|
|
*seq > snapshot && ((p.0 >> 4) - x).abs() <= 1 && ((p.2 >> 4) - z).abs() <= 1
|
|
})
|
|
.map(|(_, p)| *p)
|
|
.collect();
|
|
if !positions.is_empty() {
|
|
let changed = crate::light::relight(
|
|
&mut world.chunks,
|
|
&self.light_tables,
|
|
self.sky_light,
|
|
&positions,
|
|
true,
|
|
);
|
|
Self::mark_light_changes(world, &changed);
|
|
}
|
|
let oldest = self.light_snapshots.values().copied().min();
|
|
match oldest {
|
|
Some(oldest) => self.light_log.retain(|(seq, _)| *seq > oldest),
|
|
None => self.light_log.clear(),
|
|
}
|
|
}
|
|
/// Sections whose meshes sample a changed light value (faces and corners
|
|
/// read light from neighbouring blocks).
|
|
fn mark_light_changes(world: &mut World, changed: &[(i32, i32, i32)]) {
|
|
let mut last = None;
|
|
for &(x, y, z) in changed {
|
|
for dz in -1..=1 {
|
|
for dy in -1..=1 {
|
|
for dx in -1..=1 {
|
|
let s = (
|
|
(y + dy - crate::chunk::MIN_Y) >> 4,
|
|
(x + dx) >> 4,
|
|
(z + dz) >> 4,
|
|
);
|
|
if last == Some(s) || !(0..SECTIONS as i32).contains(&s.0) {
|
|
continue;
|
|
}
|
|
last = Some(s);
|
|
world.dirty_sections.insert((s.1, s.0, s.2));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
fn ranges(&self) -> Ranges<'_> {
|
|
Ranges {
|
|
center: self.center,
|
|
radius: self.radius,
|
|
extra: &self.extra,
|
|
forced: &self.forced,
|
|
}
|
|
}
|
|
fn near_finished(&self, x: i32, z: i32, reach: i32) -> bool {
|
|
self.ranges().near_finished(x, z, reach)
|
|
}
|
|
/// Save what generation holds outside the world: the proto chunks
|
|
/// decoration wrote to. With `wait`, the decorations, columns and saves
|
|
/// in flight are waited for first (their proto chunks are out in the
|
|
/// jobs) and the protos are saved here, before the world's chunks;
|
|
/// otherwise a worker saves them (vanilla's autosave hands its chunks to
|
|
/// the IOWorker too), and those out in jobs are saved later, as they
|
|
/// unload or at the next save.
|
|
pub fn save(&mut self, world: &mut World, wait: bool) {
|
|
if !wait {
|
|
let written: Vec<(i32, i32)> = self.written.drain().collect();
|
|
self.save_checked_out(written);
|
|
return;
|
|
}
|
|
let deadline = std::time::Instant::now() + std::time::Duration::from_secs(30);
|
|
while self.decorating > 0 || !self.finishing.is_empty() || self.saving > 0 {
|
|
let left = deadline.saturating_duration_since(std::time::Instant::now());
|
|
match self.results.recv_timeout(left) {
|
|
Ok(Done::Decorated(x, z, region)) => self.receive_decorated(x, z, region),
|
|
Ok(Done::Generated(chunk)) => self.receive_generated(chunk, world),
|
|
Ok(Done::Saved(protos)) => self.receive_saved(protos),
|
|
Ok(other) => self.deferred.push(other),
|
|
Err(_) => {
|
|
log::warn!("Horizon: chunk generation did not finish before saving");
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
let protos: Vec<&Proto> = self
|
|
.written
|
|
.iter()
|
|
.filter_map(|k| self.protos.get(k)?.as_ref())
|
|
.collect();
|
|
if !protos.is_empty() {
|
|
self.saves.save_protos(&protos);
|
|
}
|
|
self.written.clear();
|
|
}
|
|
fn in_range(&self, x: i32, z: i32, margin: i32) -> bool {
|
|
self.ranges().in_range(x, z, margin)
|
|
}
|
|
}
|
|
/// `ChunkTrackingView.isInViewDistance`: whether the column (x, z) is drawn
|
|
/// at this render distance around `center`, the camera's column
|
|
/// (`SectionOcclusionGraph.isInViewDistance`). Each axis gets a column of
|
|
/// slack, so this reaches past the circle of `radius` columns.
|
|
pub fn in_view_distance(center: (i32, i32), radius: i32, x: i32, z: i32) -> bool {
|
|
let gap = |d: i32| i64::from((d.abs() - 1).max(0));
|
|
let (dx, dz) = (gap(x - center.0), gap(z - center.1));
|
|
dx * dx + dz * dz < i64::from(radius) * i64::from(radius)
|
|
}
|
|
/// The areas kept loaded: circles around the player and `extra`, and forced chunks.
|
|
struct Ranges<'a> {
|
|
center: (i32, i32),
|
|
radius: i32,
|
|
extra: &'a [((i32, i32), i32)],
|
|
forced: &'a HashSet<(i32, i32)>,
|
|
}
|
|
impl Ranges<'_> {
|
|
fn distance(&self, x: i32, z: i32) -> i64 {
|
|
let d = |c: (i32, i32)| {
|
|
let (dx, dz) = (i64::from(x - c.0), i64::from(z - c.1));
|
|
dx * dx + dz * dz
|
|
};
|
|
self.extra
|
|
.iter()
|
|
.map(|(c, _)| d(*c))
|
|
.fold(d(self.center), i64::min)
|
|
}
|
|
|
|
/// How far past the view distance chunks are generated, and how far around
|
|
/// a finished chunk (`near_finished`): decorated chunks need their
|
|
/// neighbours generated too.
|
|
fn margins(decorates: bool) -> (i32, i32) {
|
|
if decorates { (3, 2) } else { (1, 1) }
|
|
}
|
|
/// Chunks generation asks for: loaded from the save, or generated as protos.
|
|
fn requested(&self, x: i32, z: i32, decorates: bool) -> bool {
|
|
let (margin, reach) = Self::margins(decorates);
|
|
self.in_range(x, z, margin) || self.near_finished(x, z, reach)
|
|
}
|
|
/// Finished columns kept when they arrive: within two of the view distance
|
|
/// or next to a finished chunk, and every column generation asked for (a
|
|
/// saved one loads finished; dropping it had it asked for and loaded again
|
|
/// every frame).
|
|
fn chunk_kept(&self, x: i32, z: i32, decorates: bool) -> bool {
|
|
self.in_range(x, z, 2) || self.near_finished(x, z, 1) || self.requested(x, z, decorates)
|
|
}
|
|
/// Columns unloaded: past every column kept.
|
|
fn unloaded(&self, x: i32, z: i32, decorates: bool) -> bool {
|
|
!self.in_range(x, z, 3) && !self.near_finished(x, z, Self::margins(decorates).1)
|
|
}
|
|
/// Whether a chunk finished here (`in_range(_, _, 1)`) lies within `reach`
|
|
/// chunks of (x, z) on both axes: its decoration needs its 3x3 neighbours
|
|
/// decorated, which need their own 3x3 neighbours generated. The ranges are
|
|
/// circles, so a fixed margin alone misses the corners of those squares
|
|
/// (render distance 2 never finished the chunks next to the player).
|
|
fn near_finished(&self, x: i32, z: i32, reach: i32) -> bool {
|
|
let near = |c: (i32, i32), radius: i32| {
|
|
let gap = |d: i32| i64::from((d.abs() - reach).max(0));
|
|
let (dx, dz) = (gap(x - c.0), gap(z - c.1));
|
|
let r = i64::from(radius + 1);
|
|
dx * dx + dz * dz <= r * r
|
|
};
|
|
near(self.center, self.radius) || self.extra.iter().any(|&(c, r)| near(c, r))
|
|
}
|
|
fn in_range(&self, x: i32, z: i32, margin: i32) -> bool {
|
|
self.in_main_range(x, z, margin)
|
|
|| self.extra.iter().any(|&(c, radius)| {
|
|
let r = i64::from(radius + margin);
|
|
let (dx, dz) = (i64::from(x - c.0), i64::from(z - c.1));
|
|
dx * dx + dz * dz <= r * r
|
|
})
|
|
}
|
|
/// The columns meshed for drawing: those vanilla draws around the
|
|
/// player (`in_view_distance`), and forced ones.
|
|
fn in_view(&self, x: i32, z: i32) -> bool {
|
|
in_view_distance(self.center, self.radius, x, z) || self.forced.contains(&(x, z))
|
|
}
|
|
fn in_main_range(&self, x: i32, z: i32, margin: i32) -> bool {
|
|
let r = i64::from(self.radius + margin);
|
|
let (dx, dz) = (i64::from(x - self.center.0), i64::from(z - self.center.1));
|
|
if dx * dx + dz * dz <= r * r {
|
|
return true;
|
|
}
|
|
let reach = (margin - 1).max(0);
|
|
self.forced
|
|
.iter()
|
|
.any(|f| (x - f.0).abs() <= reach && (z - f.1).abs() <= reach)
|
|
}
|
|
}
|
|
impl Drop for Streaming {
|
|
fn drop(&mut self) {
|
|
// Saves in flight hold proto chunks nothing else would save.
|
|
let deadline = std::time::Instant::now() + std::time::Duration::from_secs(30);
|
|
while self.saving > 0 {
|
|
let left = deadline.saturating_duration_since(std::time::Instant::now());
|
|
match self.results.recv_timeout(left) {
|
|
Ok(Done::Saved(_)) => self.saving -= 1,
|
|
Ok(_) => {}
|
|
Err(_) => break,
|
|
}
|
|
}
|
|
self.shared.stop.store(true, Ordering::Relaxed);
|
|
self.shared.ready.notify_all();
|
|
for t in self.threads.drain(..) {
|
|
let _ = t.join();
|
|
}
|
|
}
|
|
}
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn view_distance_is_vanillas_shape() {
|
|
// ChunkTrackingView.isInViewDistance at render distance 12: a column
|
|
// of slack on each axis, so 533 columns rather than the 441 of a
|
|
// circle of radius 12.
|
|
let count = |r: i32| {
|
|
(-40..=40)
|
|
.flat_map(|x| (-40..=40).map(move |z| (x, z)))
|
|
.filter(|&(x, z)| in_view_distance((5, -7), r, x + 5, z - 7))
|
|
.count()
|
|
};
|
|
assert_eq!(count(12), 533);
|
|
assert_eq!(count(2), 25);
|
|
for (x, z, inside) in [(12, 0, true), (13, 0, false), (9, 9, true), (10, 9, false)] {
|
|
assert_eq!(in_view_distance((0, 0), 12, x, z), inside, "({x}, {z})");
|
|
assert_eq!(
|
|
in_view_distance((0, 0), 12, -z, -x),
|
|
inside,
|
|
"({}, {})",
|
|
-z,
|
|
-x
|
|
);
|
|
}
|
|
}
|
|
|
|
struct GenerationFixture {
|
|
world: World,
|
|
center: (i32, i32),
|
|
radius: i32,
|
|
extra: Vec<((i32, i32), i32)>,
|
|
forced: HashSet<(i32, i32)>,
|
|
generating: FxHashSet<(i32, i32)>,
|
|
protos: FxHashMap<(i32, i32), Option<Proto>>,
|
|
finishing: FxHashSet<(i32, i32)>,
|
|
decorates: bool,
|
|
remote: bool,
|
|
}
|
|
impl GenerationFixture {
|
|
fn new(radius: i32, decorates: bool) -> Self {
|
|
Self {
|
|
world: World::new(horizon_world::registry::BlockStateId(0), 0),
|
|
center: (0, 0),
|
|
radius,
|
|
extra: Vec::new(),
|
|
forced: HashSet::new(),
|
|
generating: Default::default(),
|
|
protos: Default::default(),
|
|
finishing: Default::default(),
|
|
decorates,
|
|
remote: false,
|
|
}
|
|
}
|
|
fn input(&self) -> GenerationInputs<'_> {
|
|
GenerationInputs {
|
|
ranges: Ranges {
|
|
center: self.center,
|
|
radius: self.radius,
|
|
extra: &self.extra,
|
|
forced: &self.forced,
|
|
},
|
|
world: &self.world,
|
|
generating: &self.generating,
|
|
protos: &self.protos,
|
|
finishing: &self.finishing,
|
|
decorates: self.decorates,
|
|
remote: self.remote,
|
|
}
|
|
}
|
|
fn fill(&mut self) {
|
|
// Scheduler only reads membership. Share one real empty column to
|
|
// keep a radius-32 fixture small; chunk content is never consulted.
|
|
let chunk = Arc::new(Chunk::empty(0, 0));
|
|
for (_, x, z) in baseline_candidates(self.input(), usize::MAX) {
|
|
self.world.chunks.insert((x, z), chunk.clone());
|
|
}
|
|
}
|
|
fn fill_steady(&mut self) {
|
|
let chunk = Arc::new(Chunk::empty(0, 0));
|
|
for (_, x, z) in baseline_candidates(self.input(), usize::MAX) {
|
|
if self.input().ranges.in_range(x, z, 1) {
|
|
self.world.chunks.insert((x, z), chunk.clone());
|
|
} else {
|
|
// Generation's outer neighbour margin remains proto;
|
|
// only columns in the finish range become resident.
|
|
self.protos.insert((x, z), Some(Box::new(())));
|
|
}
|
|
}
|
|
}
|
|
fn assert_same(&self, gate: &mut GenerationScan, capacity: usize) -> bool {
|
|
let expected = baseline_candidates(self.input(), capacity);
|
|
let result = gate.candidates(self.input(), capacity);
|
|
assert_eq!(result.as_deref().unwrap_or(&[]), expected);
|
|
result.is_some()
|
|
}
|
|
}
|
|
|
|
// Original scheduling scan, without snapshot bookkeeping, for differential
|
|
// correctness and timing. Keep this independent of the production gate.
|
|
fn baseline_candidates(input: GenerationInputs<'_>, capacity: usize) -> Vec<(i64, i32, i32)> {
|
|
if input.remote || input.generating.len() >= capacity {
|
|
return Vec::new();
|
|
}
|
|
let margin: i32 = if input.decorates { 3 } else { 1 };
|
|
let reach = if input.decorates { 2 } else { 1 };
|
|
let mut wanted = Vec::new();
|
|
let areas: Vec<_> = std::iter::once((input.ranges.center, input.ranges.radius))
|
|
.chain(input.ranges.extra.iter().copied())
|
|
.collect();
|
|
let missing = |x, z| {
|
|
!input.world.chunks.contains_key(&(x, z))
|
|
&& !input.generating.contains(&(x, z))
|
|
&& !input.protos.contains_key(&(x, z))
|
|
&& !input.finishing.contains(&(x, z))
|
|
};
|
|
for (center, radius) in areas {
|
|
let r = radius + margin.max(reach + 1);
|
|
for dz in -r..=r {
|
|
for dx in -r..=r {
|
|
let (x, z) = (center.0 + dx, center.1 + dz);
|
|
if (input.ranges.in_range(x, z, margin)
|
|
|| input.ranges.near_finished(x, z, reach))
|
|
&& missing(x, z)
|
|
{
|
|
wanted.push((input.ranges.distance(x, z), x, z));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
for &(fx, fz) in input.ranges.forced {
|
|
for dz in -(margin - 1)..=(margin - 1) {
|
|
for dx in -(margin - 1)..=(margin - 1) {
|
|
let (x, z) = (fx + dx, fz + dz);
|
|
if missing(x, z) {
|
|
wanted.push((input.ranges.distance(x, z), x, z));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
wanted.sort_unstable();
|
|
wanted.dedup();
|
|
wanted.truncate(capacity - input.generating.len());
|
|
wanted
|
|
}
|
|
|
|
#[test]
|
|
fn generation_gate_tracks_public_ranges_and_exact_membership() {
|
|
for decorates in [false, true] {
|
|
let mut f = GenerationFixture::new(2, decorates);
|
|
f.fill();
|
|
let mut gate = GenerationScan::default();
|
|
assert!(f.assert_same(&mut gate, 6));
|
|
assert!(!f.assert_same(&mut gate, 6));
|
|
// Light/mesh state and block content do not affect generation.
|
|
f.world.dirty_light.insert((0, 0));
|
|
f.world.dirty_sections.insert((0, 0, 0));
|
|
Arc::make_mut(f.world.chunks.get_mut(&(0, 0)).unwrap()).lit = true;
|
|
assert!(!f.assert_same(&mut gate, 6));
|
|
// Same-count removal/replacement must not leave a hole unnoticed.
|
|
let chunk = f.world.chunks.remove(&(0, 0)).unwrap();
|
|
f.world.chunks.insert((100, 100), chunk.clone());
|
|
assert!(f.assert_same(&mut gate, 6));
|
|
f.world.chunks.remove(&(100, 100));
|
|
f.world.chunks.insert((0, 0), chunk);
|
|
assert!(f.assert_same(&mut gate, 6));
|
|
assert!(!f.assert_same(&mut gate, 6));
|
|
f.center = (1, -1);
|
|
assert!(f.assert_same(&mut gate, 6));
|
|
f.fill();
|
|
f.assert_same(&mut gate, 6);
|
|
f.radius += 1;
|
|
assert!(f.assert_same(&mut gate, 6));
|
|
f.fill();
|
|
f.assert_same(&mut gate, 6);
|
|
f.extra.push(((20, -20), 2));
|
|
assert!(f.assert_same(&mut gate, 6));
|
|
f.fill();
|
|
f.assert_same(&mut gate, 6);
|
|
f.forced.insert((40, 40));
|
|
assert!(f.assert_same(&mut gate, 6));
|
|
f.fill();
|
|
f.assert_same(&mut gate, 6);
|
|
f.extra.clear();
|
|
f.forced.clear();
|
|
assert!(f.assert_same(&mut gate, 6));
|
|
f.remote = true;
|
|
assert!(!f.assert_same(&mut gate, 6));
|
|
f.world.chunks.clear();
|
|
f.remote = false;
|
|
assert!(f.assert_same(&mut gate, 6));
|
|
f.fill();
|
|
f.assert_same(&mut gate, 6);
|
|
f.world = World::new(horizon_world::registry::BlockStateId(0), 1);
|
|
assert!(f.assert_same(&mut gate, 6));
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn generation_gate_preserves_batches_stage_transitions_and_retries() {
|
|
for decorates in [false, true] {
|
|
let mut f = GenerationFixture::new(2, decorates);
|
|
let mut gate = GenerationScan::default();
|
|
let mut scheduled = FxHashSet::default();
|
|
loop {
|
|
let expected = baseline_candidates(f.input(), 3);
|
|
let batch = gate.candidates(f.input(), 3).unwrap_or_default();
|
|
assert_eq!(batch, expected);
|
|
if batch.is_empty() {
|
|
break;
|
|
}
|
|
for (_, x, z) in batch {
|
|
assert!(scheduled.insert((x, z)), "duplicate scheduling");
|
|
f.generating.insert((x, z));
|
|
}
|
|
if f.generating.len() == 3 {
|
|
assert!(!f.assert_same(&mut gate, 3)); // capacity pressure
|
|
} else {
|
|
f.assert_same(&mut gate, 3);
|
|
}
|
|
for key in std::mem::take(&mut f.generating) {
|
|
// load miss -> proto -> checked out decoration -> finish.
|
|
f.protos.insert(key, None);
|
|
}
|
|
gate.exhausted = false; // Done::Proto/Decorated invalidation
|
|
}
|
|
assert!(!scheduled.is_empty());
|
|
assert!(!f.assert_same(&mut gate, 3));
|
|
let key = (0, 0);
|
|
f.protos.remove(&key);
|
|
f.finishing.insert(key);
|
|
assert!(!f.assert_same(&mut gate, 3)); // eligibility unchanged
|
|
f.finishing.remove(&key);
|
|
f.world.chunks.insert(key, Arc::new(Chunk::empty(0, 0)));
|
|
gate.exhausted = false; // Done::Generated
|
|
assert!(f.assert_same(&mut gate, 3));
|
|
assert!(!f.assert_same(&mut gate, 3));
|
|
// A discarded/pruned proto must be requested again, even without
|
|
// any range or loaded-membership change.
|
|
let key = scheduled.iter().copied().find(|k| *k != (0, 0)).unwrap();
|
|
f.protos.remove(&key);
|
|
gate.exhausted = false;
|
|
assert!(f.assert_same(&mut gate, 3));
|
|
assert_eq!(baseline_candidates(f.input(), 3).len(), 1);
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
#[ignore = "reproducible actual generation scheduler benchmark; run with --ignored --nocapture"]
|
|
fn generation_scan_benchmark() {
|
|
use std::{hint::black_box, time::Instant};
|
|
let mut f = GenerationFixture::new(32, true);
|
|
f.fill_steady();
|
|
let mut gate = GenerationScan::default();
|
|
f.assert_same(&mut gate, 12);
|
|
assert!(!f.assert_same(&mut gate, 12));
|
|
const FRAMES: usize = 2000;
|
|
let measure = |optimized: bool, dynamic: bool| {
|
|
let mut samples = Vec::new();
|
|
let mut scans = 0;
|
|
let mut jobs = 0;
|
|
for repetition in 0..9 {
|
|
let mut fixture = GenerationFixture::new(32, true);
|
|
fixture.fill_steady();
|
|
let saved = fixture.world.chunks.get(&(0, 0)).unwrap().clone();
|
|
let mut gate = GenerationScan::default();
|
|
gate.candidates(fixture.input(), 12);
|
|
let start = Instant::now();
|
|
let mut count = 0;
|
|
let mut scheduled = 0;
|
|
for frame in 0..FRAMES {
|
|
// Remove a column, queue generation, then accept a saved
|
|
// load completion. Both schedulers see identical events.
|
|
if dynamic && frame % 32 == 0 {
|
|
fixture.world.chunks.remove(&(0, 0));
|
|
}
|
|
if dynamic && frame % 32 == 1 {
|
|
fixture.generating.remove(&(0, 0));
|
|
fixture.world.chunks.insert((0, 0), saved.clone());
|
|
gate.exhausted = false; // Done::Generated
|
|
}
|
|
let result = if optimized {
|
|
let result = gate.candidates(black_box(fixture.input()), 12);
|
|
count += usize::from(result.is_some());
|
|
result.unwrap_or_default()
|
|
} else {
|
|
count += 1;
|
|
baseline_candidates(black_box(fixture.input()), 12)
|
|
};
|
|
let expected_jobs = usize::from(dynamic && frame % 32 == 0);
|
|
assert_eq!(result.len(), expected_jobs);
|
|
for (_, x, z) in black_box(result) {
|
|
assert_eq!((x, z), (0, 0));
|
|
fixture.generating.insert((x, z));
|
|
scheduled += 1;
|
|
}
|
|
}
|
|
if repetition != 0 {
|
|
samples.push(start.elapsed().as_secs_f64() * 1e9 / FRAMES as f64);
|
|
}
|
|
assert!(fixture.generating.is_empty());
|
|
assert_eq!(fixture.world.chunks.len(), f.world.chunks.len());
|
|
scans = count;
|
|
jobs = scheduled;
|
|
}
|
|
samples.sort_by(f64::total_cmp);
|
|
(samples[samples.len() / 2], scans, jobs)
|
|
};
|
|
println!(
|
|
"radius=32 decorates=true positions/frame=5041 loaded_keys={} proto_keys={} frames/sample={FRAMES} warmup=1 measured=8",
|
|
f.world.chunks.len(),
|
|
f.protos.len()
|
|
);
|
|
for dynamic in [false, true] {
|
|
let (before, baseline_scans, before_jobs) = measure(false, dynamic);
|
|
let (after, optimized_scans, after_jobs) = measure(true, dynamic);
|
|
assert_eq!(before_jobs, after_jobs);
|
|
println!(
|
|
"dynamic={dynamic}: baseline={before:.1}ns/frame optimized={after:.1}ns/frame speedup={:.2}x scans={baseline_scans}/{optimized_scans} jobs={before_jobs}/{after_jobs}",
|
|
before / after
|
|
);
|
|
}
|
|
}
|
|
|
|
/// The ranges `Streaming::update` checks at each step, from generation to
|
|
/// light, must let every chunk the player spawns on finish and light: the
|
|
/// 3x3 columns around the player need their own 3x3 neighbours finished,
|
|
/// each of those its 3x3 neighbours decorated, and each of those its 3x3
|
|
/// neighbours generated and kept. Render distance 2 once stopped short.
|
|
#[test]
|
|
fn spawn_neighbourhood_completes_at_every_distance() {
|
|
let forced = HashSet::new();
|
|
for decorates in [true, false] {
|
|
let (margin, reach) = Ranges::margins(decorates);
|
|
for radius in 2..=32 {
|
|
let r = Ranges {
|
|
center: (5, -7),
|
|
radius,
|
|
extra: &[],
|
|
forced: &forced,
|
|
};
|
|
let square = |x: i32, z: i32, n: i32| {
|
|
(-n..=n).flat_map(move |dz| (-n..=n).map(move |dx| (x + dx, z + dz)))
|
|
};
|
|
let generated = |x, z| {
|
|
r.requested(x, z, decorates)
|
|
&& (x - r.center.0).abs() <= radius + margin.max(reach + 1)
|
|
&& (z - r.center.1).abs() <= radius + margin.max(reach + 1)
|
|
};
|
|
let proto_kept = |x, z| r.in_range(x, z, 4) || r.near_finished(x, z, 2);
|
|
let decorated =
|
|
|x, z| !decorates || r.in_range(x, z, 2) || r.near_finished(x, z, 1);
|
|
let chunk_kept = |x, z| r.chunk_kept(x, z, decorates);
|
|
let unloaded = |x, z| r.unloaded(x, z, decorates);
|
|
let finishes = |x: i32, z: i32| {
|
|
r.in_range(x, z, 1)
|
|
&& chunk_kept(x, z)
|
|
&& !unloaded(x, z)
|
|
&& square(x, z, 1).all(|(nx, nz)| {
|
|
decorated(nx, nz)
|
|
&& (!decorates
|
|
|| square(nx, nz, 1)
|
|
.all(|(gx, gz)| generated(gx, gz) && proto_kept(gx, gz)))
|
|
&& generated(nx, nz)
|
|
&& proto_kept(nx, nz)
|
|
})
|
|
};
|
|
let (cx, cz) = r.center;
|
|
for (x, z) in square(cx, cz, 1) {
|
|
for (nx, nz) in square(x, z, 1) {
|
|
assert!(
|
|
finishes(nx, nz),
|
|
"radius {radius} (decorates: {decorates}): ({nx}, {nz}) never finishes"
|
|
);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/// A column generation asks for is kept when it arrives finished (loaded
|
|
/// from the save) and is not unloaded right after: either way it would be
|
|
/// asked for and loaded again on the next frame, forever (8 such columns
|
|
/// after a teleport at render distance 16 held 8 of the in-flight slots).
|
|
#[test]
|
|
fn requested_columns_stay_loaded() {
|
|
let forced = HashSet::new();
|
|
for decorates in [true, false] {
|
|
for radius in 2..=32 {
|
|
let r = Ranges {
|
|
center: (5, -7),
|
|
radius,
|
|
extra: &[((radius * 2 + 9, 3), 4)],
|
|
forced: &forced,
|
|
};
|
|
let span = radius * 3 + 20;
|
|
for x in -span..=span {
|
|
for z in -span..=span {
|
|
if r.requested(x, z, decorates) {
|
|
assert!(
|
|
r.chunk_kept(x, z, decorates),
|
|
"radius {radius} (decorates: {decorates}): ({x}, {z}) is asked for but dropped"
|
|
);
|
|
}
|
|
if r.chunk_kept(x, z, decorates) {
|
|
assert!(
|
|
!r.unloaded(x, z, decorates),
|
|
"radius {radius} (decorates: {decorates}): ({x}, {z}) is kept then unloaded"
|
|
);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|