diff --git a/crates/horizon-client/src/fog.rs b/crates/horizon-client/src/fog.rs index 83f1a8bf5..0716f3e77 100644 --- a/crates/horizon-client/src/fog.rs +++ b/crates/horizon-client/src/fog.rs @@ -180,6 +180,8 @@ pub struct FogInputs<'a> { pub water_fog_color: [f32; 3], pub biome: u16, pub render_distance_chunks: i32, + /// Extend ordinary Overworld atmosphere for terrain beyond vanilla's range. + pub distant_terrain: bool, /// The Cloud Distance option, in chunks. pub cloud_range_chunks: i32, pub camera_y: f64, @@ -336,6 +338,18 @@ pub fn setup_fog(tables: &FogTables, state: &FogState, input: &FogInputs) -> Fog let span = (rd / 10.0).clamp(4.0, 64.0); fog.render_distance_start = rd - span; fog.render_distance_end = rd; + if input.distant_terrain + && input.skybox + && input.fog_type == FogType::Atmospheric + && input.blindness.is_none() + && input.darkness.is_none() + && !input.boss_world_fog + && sky.fog_end >= 1024.0 + { + let scale = (rd / 1024.0).max(1.0); + fog.environmental_start *= scale; + fog.environmental_end *= scale; + } fog } /// `AtmosphericFogEnvironment.updateRainFogState`. @@ -368,6 +382,7 @@ mod tests { water_fog_color: [0.0, 0.0, 0.2], biome: 0, render_distance_chunks: 12, + distant_terrain: false, cloud_range_chunks: 128, camera_y: 70.0, min_y: -64, @@ -446,6 +461,38 @@ mod tests { assert_eq!(fog.sky_end, 192.0); } #[test] + fn distant_atmosphere_extends_without_extending_environmental_overrides() { + let sky = Sky { + fog_end: 1024., + ..Sky::default() + }; + let tables = FogTables { + water_fog_end: vec![1.], + }; + let mut input = inputs(&sky); + input.render_distance_chunks = 1024; + input.distant_terrain = true; + let fog = setup_fog(&tables, &FogState::default(), &input); + assert_eq!(fog.environmental_end, 16384.); + input.fog_type = FogType::Water; + assert_eq!( + setup_fog(&tables, &FogState::default(), &input).environmental_end, + 24. + ); + input.fog_type = FogType::Atmospheric; + input.darkness = Some(1.); + assert_eq!( + setup_fog(&tables, &FogState::default(), &input).environmental_end, + 15. + ); + input.darkness = None; + input.boss_world_fog = true; + assert_eq!( + setup_fog(&tables, &FogState::default(), &input).environmental_end, + 96. + ); + } + #[test] fn cloud_fog_ends_at_the_cloud_distance() { let sky = Sky { fog_end: 1024.0, diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index 1151339a8..4060a1691 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -6446,6 +6446,7 @@ impl Game<'_> { self.screen = Screen::Playing; self.title = None; self.clear_lod(); + self.pending_uploads.clear(); if let Some(r) = self.renderer.as_mut() { r.clear_sections(); } @@ -6596,6 +6597,7 @@ impl Game<'_> { self.orbs.clear(); self.mining = None; self.clear_lod(); + self.pending_uploads.clear(); if let Some(r) = self.renderer.as_mut() { r.clear_sections(); } @@ -6775,6 +6777,7 @@ impl Game<'_> { self.clear_entities(); self.screen = Screen::Playing; self.clear_lod(); + self.pending_uploads.clear(); if let Some(r) = self.renderer.as_mut() { r.clear_sections(); } @@ -8738,7 +8741,13 @@ impl Game<'_> { fog: Some({ let mut fog = self.frame_fog(camera, &sky, biome); // Horizon's fog setting: no distance fog (fluids keep theirs). - if !self.settings.horizon.fog && !underwater && fog.environmental_end >= distance { + if !self.settings.horizon.fog + && !underwater + && self.darkness_fog().is_none() + && !self.dragon_bar_shown() + && self.potions.effects.get("blindness").is_none() + && fog.environmental_end >= (self.streaming.radius * 16) as f32 + { (fog.render_distance_start, fog.render_distance_end) = (1e9, 2e9); (fog.environmental_start, fog.environmental_end) = (1e9, 2e9); } @@ -9112,9 +9121,9 @@ impl Game<'_> { } Ok(()) } - /// Only surface LOD worlds extend the renderer's range, never simulation. + /// Only enabled LOD worlds extend the renderer's range, never simulation. fn terrain_distance(&self) -> i32 { - if self.lod.is_some() && self.dim != Dim::Nether { + if self.lod.is_some() { self.streaming .radius .max(self.settings.horizon.lod_distance.min(1024) as i32) @@ -9132,7 +9141,6 @@ impl Game<'_> { } fn prepare_lod(&mut self) { let enabled = self.settings.horizon.lod_distance > 0 - && self.dim != Dim::Nether && self.renderer.is_some() && self.player.is_some() && self.title.is_none() @@ -9181,14 +9189,16 @@ impl Game<'_> { self.lod_observe .extend(uploads.iter().map(|(key, _)| (key.0, key.2))); for chunk in &unloaded { - lod.observe(chunk, &self.res.facts); + horizon_client::spans::time("lod_observe", || lod.observe(chunk, &self.res.facts)); self.lod_observe.remove(&(chunk.x, chunk.z)); } let observe: Vec<_> = self.lod_observe.iter().copied().take(2).collect(); for key in observe { self.lod_observe.remove(&key); if let Some(chunk) = self.world.chunks.get(&key) { - lod.observe(chunk, &self.res.facts); + horizon_client::spans::time("lod_observe", || { + lod.observe(chunk, &self.res.facts) + }); } } } @@ -9219,39 +9229,37 @@ impl Game<'_> { } if let Some(renderer) = self.renderer.as_mut() { let g = &self.settings.horizon; - let budgeted = g.chunk_updates > 0 || g.upload_budget_kib > 0; - if !budgeted && self.pending_uploads.is_empty() { - horizon_client::spans::time("upload", || { - for (key, mesh) in uploads { - renderer.upload(key, &mesh); - } - }); - } else { + // Even unlimited count/byte settings must yield during a join's + // upload burst; draining it all blocked input/rendering for seconds. + horizon_client::spans::time("upload", || { // Horizon's chunk-update and upload budgets: the nearest // sections go first, the rest wait for later frames; a newer // mesh of a waiting section replaces it. let pending = &mut self.pending_uploads; let fresh: horizon_client::fast_hash::FxHashSet<(i32, i32, i32)> = uploads.iter().map(|(k, _)| *k).collect(); - let gone: horizon_client::fast_hash::FxHashSet<(i32, i32)> = - unloaded.iter().map(|c| (c.x, c.z)).collect(); - pending.retain(|(k, _)| !fresh.contains(k) && !gone.contains(&(k.0, k.2))); - pending.extend(uploads); + let chunks = &self.world.chunks; + pending.retain(|(k, _)| !fresh.contains(k) && chunks.contains_key(&(k.0, k.2))); + pending.extend( + uploads + .into_iter() + .filter(|(k, _)| chunks.contains_key(&(k.0, k.2))), + ); let centre = self.streaming.center; let far = |k: &(i32, i32, i32)| (k.0 - centre.0).pow(2) + (k.2 - centre.1).pow(2); pending.sort_unstable_by_key(|(k, _)| std::cmp::Reverse(far(k))); - let (limit, budget) = if budgeted { - ( - g.chunk_updates as usize, - u64::from(g.upload_budget_kib) * 1024, - ) - } else { - (0, 0) - }; + let (limit, budget) = ( + g.chunk_updates as usize, + u64::from(g.upload_budget_kib) * 1024, + ); let (mut count, mut bytes) = (0, 0u64); + let upload_started = Instant::now(); while let Some((_, mesh)) = pending.last() { - let size = ((mesh.solid.len() + mesh.translucent.len()) * 32) as u64; - if (limit > 0 && count >= limit) + let size = ((mesh.solid.len() + mesh.translucent.len()) + * std::mem::size_of::()) + as u64; + if (count > 0 && upload_started.elapsed() >= Duration::from_millis(4)) + || (limit > 0 && count >= limit) || (budget > 0 && bytes > 0 && bytes + size > budget) { break; @@ -9261,7 +9269,7 @@ impl Game<'_> { count += 1; bytes += size; } - } + }); for c in unloaded { for sy in 0..(HEIGHT / 16) { renderer.remove((c.x, sy, c.z)); @@ -9279,13 +9287,16 @@ impl Game<'_> { ) }) .collect(); - for (key, mesh) in lod.update( - [player.position.x, player.position.y, player.position.z], - self.streaming.radius, - self.settings.horizon.lod_distance, - covered, - ) { - renderer.upload_lod(key, &mesh); + for (key, mesh) in horizon_client::spans::time("lod_update", || { + lod.update( + [player.position.x, player.position.y, player.position.z], + self.yaw, + self.streaming.radius, + self.settings.horizon.lod_distance, + covered, + ) + }) { + horizon_client::spans::time("lod_upload", || renderer.upload_lod(key, &mesh)); } for key in lod.removed() { renderer.remove_lod(key); diff --git a/crates/horizon-client/src/game/control.rs b/crates/horizon-client/src/game/control.rs index c94e31626..51abdebed 100644 --- a/crates/horizon-client/src/game/control.rs +++ b/crates/horizon-client/src/game/control.rs @@ -717,6 +717,7 @@ impl Game<'_> { "active_tiles": self.lod.as_ref().map_or(0, |lod| lod.stats().0), "cached_tiles": self.lod.as_ref().map_or(0, |lod| lod.stats().1), "pending_observations": self.lod_observe.len(), + "refined_samples": self.lod.as_ref().map_or(0, |lod| lod.refined_samples()), }); let mut status = json!({ "screen": self.screen_name(), @@ -777,6 +778,10 @@ impl Game<'_> { .map(|e| self.shown_name_tags(glam::DVec3::from_array(e))), }); status["lod"] = lod; + let (target, sampled) = self.lod.as_ref().map_or((0, 0), |lod| lod.progress()); + status["lod"]["target_tiles"] = target.into(); + status["lod"]["sampled_target_tiles"] = sampled.into(); + status["pending_uploads"] = self.pending_uploads.len().into(); status["physical_keys"] = json!(physical_keys); status } diff --git a/crates/horizon-client/src/game/remote.rs b/crates/horizon-client/src/game/remote.rs index 67322c90a..fd2517e7f 100644 --- a/crates/horizon-client/src/game/remote.rs +++ b/crates/horizon-client/src/game/remote.rs @@ -260,6 +260,7 @@ impl Game<'_> { self.title = None; self.dim = Dim::Overworld; self.clear_lod(); + self.pending_uploads.clear(); if let Some(r) = self.renderer.as_mut() { r.clear_sections(); } @@ -365,6 +366,7 @@ impl Game<'_> { self.screen = Screen::Playing; self.container = None; self.clear_lod(); + self.pending_uploads.clear(); if let Some(r) = self.renderer.as_mut() { r.clear_sections(); } @@ -613,6 +615,7 @@ impl Game<'_> { r.chunk_center = (0, 0); } self.clear_lod(); + self.pending_uploads.clear(); if let Some(renderer) = self.renderer.as_mut() { renderer.clear_sections(); } diff --git a/crates/horizon-client/src/game/render_tick.rs b/crates/horizon-client/src/game/render_tick.rs index fad4f7352..e0431dabe 100644 --- a/crates/horizon-client/src/game/render_tick.rs +++ b/crates/horizon-client/src/game/render_tick.rs @@ -86,6 +86,7 @@ impl Game<'_> { ], biome, render_distance_chunks: self.terrain_distance(), + distant_terrain: self.lod.is_some(), cloud_range_chunks: self.settings.cloud_range, camera_y: camera.y, // level.getMinY(): the Nether and the End start at y 0. diff --git a/crates/horizon-client/src/lod.rs b/crates/horizon-client/src/lod.rs index 59adc4e7e..3863b26cb 100644 --- a/crates/horizon-client/src/lod.rs +++ b/crates/horizon-client/src/lod.rs @@ -1,13 +1,13 @@ -//! Bounded surface terrain for distances beyond the resident chunk area. +//! Bounded voxel-column terrain for distances beyond the resident chunk area. //! Far tiles never enter World, lighting, simulation or the section cave graph. use crate::{ - blocks::{BlockFacts, Fluid}, - chunk::{Chunk, HEIGHT, MIN_Y}, + blocks::BlockFacts, + chunk::{Chunk, MIN_Y}, fast_hash::{FxHashMap, FxHashSet}, - mesher::{FaceGroups, SectionMesh, TerrainVertex}, - streaming::{LodSource, WorkerContext}, + mesher::SectionMesh, + streaming::{LodDetail, LodSource, WorkerContext}, }; -use horizon_core::position::BlockPos; +mod mesh; use horizon_world::registry::BlockStateId; use std::{ collections::VecDeque, @@ -20,7 +20,8 @@ use std::{ }; const CELLS: usize = 16; -const MAX_SAVED: usize = 8192; +const MAX_SAVED: usize = 1024; +const SAMPLE_JOBS: usize = 4; /// Top solid face and optional fluid face, in absolute block coordinates. #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub struct Surface { @@ -29,6 +30,203 @@ pub struct Surface { pub water: Option<(i32, BlockStateId)>, pub biome: u16, } +/// A half-open material run; gaps between runs remain air. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct Span { + pub min: i16, + pub max: i16, + pub state: BlockStateId, + pub biome: u16, + pub light: u16, +} +#[derive(Clone, Debug, Default, PartialEq, Eq)] +pub struct Column { + pub surface: Option, + pub spans: Vec, +} +impl Column { + /// Reduce an entire horizontal footprint, rather than inflating its corner + /// column (a single leaf used to become a whole distant tree-sized slab). + pub(crate) fn reduced(columns: &[Column; 256], x: usize, z: usize, width: usize) -> Self { + if width == 1 { + return columns[z * 16 + x].clone(); + } + let mut events = Vec::new(); + for dz in z..z + width { + for dx in x..x + width { + for span in &columns[dz * 16 + dx].spans { + let material = (span.state, span.biome, span.light); + events.push((span.min, material, 1)); + events.push((span.max, material, -1)); + } + } + } + events.sort_unstable_by_key(|event| event.0); + let mut result = Self::default(); + let mut votes = Vec::<((BlockStateId, u16), i32)>::new(); + let mut sky = [0i32; 16]; + let mut block = [0i32; 16]; + let mut occupied = 0; + let mut bottom = MIN_Y as i16; + for (y, material, change) in events { + if bottom < y { + if occupied * 2 >= (width * width) as i32 + && let Some(&((state, biome), _)) = + votes.iter().max_by_key(|&(material, count)| { + (*count, std::cmp::Reverse((material.0.0, material.1))) + }) + { + // Lighting must not split votes for the same material. + // Reduction can expose buried faces; retain the brightest + // actual light in this occupied footprint, per channel. + let light = (sky.iter().rposition(|&n| n > 0).unwrap_or(0) << 4) + | (block.iter().rposition(|&n| n > 0).unwrap_or(0) << 12); + result.push(Span { + min: bottom, + max: y, + state, + biome, + light: light as u16, + }); + } + bottom = y; + } + occupied += change; + sky[usize::from((material.2 >> 4) & 15)] += change; + block[usize::from((material.2 >> 12) & 15)] += change; + let material = (material.0, material.1); + if let Some(vote) = votes.iter_mut().find(|vote| vote.0 == material) { + vote.1 += change; + } else { + votes.push((material, change)); + } + } + result + } + pub fn from_surface(surface: Option) -> Self { + let mut column = Self { + surface, + spans: Vec::new(), + }; + if let Some(s) = surface { + if s.height > MIN_Y { + column.spans.push(Span { + min: MIN_Y as i16, + max: s.height as i16, + state: s.state, + biome: s.biome, + light: 240, + }); + } + if let Some((height, state)) = s.water { + column.spans.push(Span { + min: s.height as i16, + max: height as i16, + state, + biome: s.biome, + light: 240, + }); + } + } + column + } + pub(crate) fn push(&mut self, span: Span) { + if let Some(last) = self.spans.last_mut() + && last.max == span.min + && last.state == span.state + && last.biome == span.biome + && last.light == span.light + { + last.max = span.max; + } else { + self.spans.push(span); + } + } + fn from_chunk(chunk: &Chunk, facts: &BlockFacts, x: i32, z: i32) -> Self { + let face_light = |y: i32| { + let mut sky = 0; + let mut block = 0; + for (dx, dy, dz) in [ + (0, 0, 0), + (-1, 0, 0), + (1, 0, 0), + (0, -1, 0), + (0, 1, 0), + (0, 0, -1), + (0, 0, 1), + ] { + if !(0..16).contains(&(x + dx)) || !(0..16).contains(&(z + dz)) { + continue; + } + let value = chunk.light(x + dx, y + dy, z + dz); + sky = sky.max(value & 0xf0); + block = block.max(value & 15); + } + sky | block + }; + let mut column = Self::default(); + for (sy, section) in chunk.sections.iter().enumerate() { + let min = MIN_Y + sy as i32 * 16; + let uniform = section.blocks.bits() == 0 + && section.biomes.bits() == 0 + && matches!(section.light, crate::chunk::Light::Uniform(_)); + let uniform = uniform + && face_light(min) == chunk.light(x, min, z) + && face_light(min + 15) == chunk.light(x, min, z); + let step = if uniform { 16 } else { 1 }; + for offset in (0..16).step_by(step) { + let y = min + offset; + let state = BlockStateId(chunk.get(x, y, z)); + let f = facts.get(state); + if facts.is_air(state) + || !(f.solid_render + || f.full_collision + || f.name.ends_with("_leaves") + || matches!(&*f.name, "minecraft:water" | "minecraft:lava")) + { + continue; + } + let light = face_light(y); + column.push(Span { + min: y as i16, + max: (y + step as i32) as i16, + state, + biome: chunk.biome(x, y, z), + light: u16::from(light & 0xf0) + | (u16::from((light & 15).max(f.emission)) << 12), + }); + } + } + let mut water = None; + for span in column.spans.iter().rev() { + if matches!( + &*facts.get(span.state).name, + "minecraft:water" | "minecraft:lava" + ) { + water.get_or_insert((i32::from(span.max), span.state)); + } else { + column.surface = Some(Surface { + height: i32::from(span.max), + state: span.state, + water, + biome: span.biome, + }); + break; + } + } + if column.surface.is_none() + && let Some((height, state)) = water + { + column.surface = Some(Surface { + height: MIN_Y, + state: facts.air, + water: Some((height, state)), + biome: chunk.biome(x, height - 1, z), + }); + } + column + } +} #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)] pub struct TileKey { pub x: i32, @@ -37,7 +235,7 @@ pub struct TileKey { } impl TileKey { pub fn size(self) -> i32 { - 64 << self.level + 16 << self.level } pub fn origin(self) -> [i32; 3] { [self.x * self.size(), 0, self.z * self.size()] @@ -57,72 +255,107 @@ impl TileKey { .max(camera[1] - z - self.size()) .max(0) } + fn in_view(self, camera: [i32; 2], yaw: f32) -> bool { + let [x, _, z] = self.origin(); + let (sin, cos) = yaw.sin_cos(); + let mut forward = f32::NEG_INFINITY; + let mut left = f32::NEG_INFINITY; + let mut right = f32::NEG_INFINITY; + for (dx, dz) in [ + (0, 0), + (self.size(), 0), + (0, self.size()), + (self.size(), self.size()), + ] { + let (x, z) = ((x + dx - camera[0]) as f32, (z + dz - camera[1]) as f32); + let f = -x * sin + z * cos; + let side = x * cos + z * sin; + forward = forward.max(f); + left = left.max(f * 0.866 + side * 0.5); + right = right.max(f * 0.866 - side * 0.5); + } + forward > 0. && left > 0. && right > 0. + } } #[derive(Default)] struct Plan { roots: Vec, nodes: FxHashSet, split: FxHashSet, + elevation: i32, } impl Plan { + #[cfg(test)] fn new(camera: [i32; 2], detail: i32, distance: u32) -> Self { + Self::with_previous(camera, detail, distance, &Self::default(), 0) + } + fn with_previous( + camera: [i32; 2], + detail: i32, + distance: u32, + previous: &Self, + elevation: i32, + ) -> Self { let range = distance.min(1024) as i32 * 16; let mut level = 0; - while (64 << level) < range { + while (16 << level) < range { level += 1; } - let size = 64 << level; - let mut result = Self::default(); + let size = 16 << level; + let mut result = Self { + elevation, + ..Self::default() + }; for z in (camera[1] - range).div_euclid(size)..=(camera[1] + range).div_euclid(size) { for x in (camera[0] - range).div_euclid(size)..=(camera[0] + range).div_euclid(size) { let key = TileKey { x, z, level }; result.roots.push(key); - result.visit(key, camera, detail, range); + result.visit(key, camera, detail, range, previous); } } result } - fn visit(&mut self, key: TileKey, camera: [i32; 2], detail: i32, range: i32) { + fn visit(&mut self, key: TileKey, camera: [i32; 2], detail: i32, range: i32, previous: &Self) { if key.distance(camera) > range { return; } self.nodes.insert(key); - // Inside the near area, stride4 divides chunk boundaries exactly. - if key.level > 0 && key.distance(camera) < (key.size() * 2).max(detail * 16 + 64) { + // Keep distant cells small on screen; finest tiles retain individual blocks. + let threshold = key.size() * if previous.split.contains(&key) { 10 } else { 8 }; + if key.level > 0 + && (key.distance(camera) < detail * 16 + 16 + || key.distance(camera).max(self.elevation) < threshold) + { self.split.insert(key); for child in key.children() { - self.visit(child, camera, detail, range); + self.visit(child, camera, detail, range, previous); } } } fn select( &self, key: TileKey, - ready: &FxHashMap>>, + ready: &FxHashMap>, out: &mut Vec, ) -> bool { if !self.nodes.contains(&key) { return true; } if self.split.contains(&key) { - let start = out.len(); - if key + // Visit every child: a missing sibling must not hide ready detail. + let complete = key .children() .into_iter() - .all(|child| self.select(child, ready, out)) - { + .map(|child| self.select(child, ready, out)) + .fold(true, |complete, child| complete & child); + if complete { return true; } if ready.contains_key(&key) { - out.truncate(start); + // Its mesh is clipped around every selected finer tile. out.push(key); return true; } - // Keep any children already available while the parent is loading. - out.truncate(start); - for child in key.children() { - self.select(child, ready, out); - } false } else if ready.contains_key(&key) { out.push(key); @@ -132,44 +365,96 @@ impl Plan { } } } -struct Job { +struct MeshInput { key: TileKey, + columns: Vec, + neighbors: [Vec; 4], + finer: Vec, } -struct Done { - key: TileKey, - samples: Vec>, +struct ReducedColumns { + columns: Arc<[Column; 256]>, + levels: [Vec; 4], +} +impl ReducedColumns { + fn new(columns: Arc<[Column; 256]>) -> Self { + let levels = std::array::from_fn(|level| { + let width = 2 << level; + (0..16 / width) + .flat_map(|z| { + let columns = &columns; + (0..16 / width) + .map(move |x| Column::reduced(columns, x * width, z * width, width)) + }) + .collect() + }); + Self { columns, levels } + } + fn get(&self, x: usize, z: usize, width: usize) -> &Column { + if width == 1 { + &self.columns[z * 16 + x] + } else { + &self.levels[width.trailing_zeros() as usize - 1] + [(z / width) * (16 / width) + x / width] + } + } +} +enum Job { + Sample(TileKey), + Reduce(LodDetail), + RequestDetail { + chunks: Vec<(i32, i32)>, + center: [i32; 2], + }, + Mesh { + active: FxHashSet, + inputs: Vec, + covered: FxHashSet<(i32, i32)>, + bounds: Option<[i32; 4]>, + }, +} +enum Done { + Detail((i32, i32), ReducedColumns), + DetailRequested, + Sample { + key: TileKey, + samples: Vec, + }, + Mesh { + active: FxHashSet, + meshes: Vec<(TileKey, SectionMesh)>, + }, } struct Shared { jobs: Mutex>, wake: Condvar, stop: AtomicBool, } -#[derive(Clone, Copy)] -struct Style { - layer: u16, - tint: i32, - translucent: bool, - light: u16, -} /// One background worker and at most two outstanding sample jobs. Only the /// current view's quadtree and a bounded set of visited summaries are retained. pub struct Lod { shared: Arc, thread: Option>, results: mpsc::Receiver, + #[cfg(test)] context: Arc, - area: Option<([i32; 2], i32, u32)>, + meshing: bool, + invalidated: FxHashSet<(i32, i32)>, + area: Option<([i32; 2], i32, u32, i32)>, plan: Plan, pending: FxHashSet, - ready: FxHashMap>>, + sample_nearby: bool, + detail_masks: FxHashMap, + detail_request_pending: bool, + detail_request_at: std::time::Instant, + ready: FxHashMap>, active: FxHashSet, dirty: FxHashSet, removed: Vec, covered: FxHashSet<(i32, i32)>, covered_bounds: Option<[i32; 4]>, - saved: FxHashMap<(i32, i32), [Option; 16]>, + saved: FxHashMap<(i32, i32), Arc<[Column; 256]>>, + reduced: FxHashMap<(i32, i32), ReducedColumns>, saved_order: VecDeque<(i32, i32)>, - styles: FxHashMap, } impl Lod { pub fn new(source: Arc, context: Arc) -> Self { @@ -179,55 +464,159 @@ impl Lod { stop: AtomicBool::new(false), }); let worker = shared.clone(); + let mesh_context = context.clone(); let (tx, results) = mpsc::channel(); let handle = thread::Builder::new() .name("Far terrain".into()) .spawn(move || { let mut sampler = source.sampler(); - loop { + let count = thread::available_parallelism() + .map_or(1, |n| n.get() / 4) + .clamp(1, 3); + let mut sampling = Vec::new(); + for _ in 0..count { + let Some(mut sample) = sampler.fork() else { + break; + }; + let shared = worker.clone(); + let tx = tx.clone(); + sampling.push( + thread::Builder::new() + .name("Far sampling".into()) + .spawn(move || { + loop { + let key = { + let mut jobs = shared.jobs.lock().unwrap(); + loop { + if shared.stop.load(Ordering::Relaxed) { + return; + } + if let Some(index) = jobs + .iter() + .position(|job| matches!(job, Job::Sample(_))) + { + let Some(Job::Sample(key)) = jobs.remove(index) + else { + unreachable!() + }; + break key; + } + jobs = shared.wake.wait(jobs).unwrap(); + } + }; + let [x, _, z] = key.origin(); + let samples = + sample.grid(x, z, key.size() / CELLS as i32, CELLS); + if shared.stop.load(Ordering::Relaxed) { + return; + } + if tx.send(Done::Sample { key, samples }).is_err() { + return; + } + } + }) + .expect("far sampling worker"), + ); + } + let mut mesher = mesh::Mesher::default(); + 'work: loop { + for (key, columns) in sampler.drain_details() { + if tx + .send(Done::Detail(key, ReducedColumns::new(columns))) + .is_err() + { + break 'work; + } + } let job = { let mut jobs = worker.jobs.lock().unwrap(); - while jobs.is_empty() && !worker.stop.load(Ordering::Relaxed) { - jobs = worker.wake.wait(jobs).unwrap(); + if (jobs.is_empty() + || (!sampling.is_empty() + && jobs.iter().all(|job| matches!(job, Job::Sample(_))))) + && !worker.stop.load(Ordering::Relaxed) + { + jobs = worker + .wake + .wait_timeout(jobs, std::time::Duration::from_millis(100)) + .unwrap() + .0; } if worker.stop.load(Ordering::Relaxed) { break; } - jobs.pop_front().unwrap() + if sampling.is_empty() { + jobs.pop_front() + } else { + jobs.iter() + .position(|job| !matches!(job, Job::Sample(_))) + .and_then(|index| jobs.remove(index)) + } }; - let [x, _, z] = job.key.origin(); - let stride = job.key.size() / CELLS as i32; - let mut samples = Vec::with_capacity(CELLS * CELLS); - for dz in 0..CELLS { - if worker.stop.load(Ordering::Relaxed) { - break; + let Some(job) = job else { continue }; + let done = match job { + Job::Reduce((key, columns)) => { + Done::Detail(key, ReducedColumns::new(columns)) } - for dx in 0..CELLS { - samples.push( - sampler.sample(x + dx as i32 * stride, z + dz as i32 * stride), - ); + Job::RequestDetail { chunks, center } => { + sampler.request_detail(&chunks, center); + Done::DetailRequested } - } - if tx - .send(Done { - key: job.key, - samples, - }) - .is_err() - { + Job::Sample(key) => { + let [x, _, z] = key.origin(); + let samples = sampler.grid(x, z, key.size() / CELLS as i32, CELLS); + Done::Sample { key, samples } + } + Job::Mesh { + active, + inputs, + covered, + bounds, + } => { + let mut meshes = Vec::with_capacity(inputs.len()); + for input in inputs { + if worker.stop.load(Ordering::Relaxed) { + break 'work; + } + let mesh = mesher.mesh( + input.key, + &input.columns, + &covered, + bounds, + &mesh_context, + &input.neighbors, + &input.finer, + ); + meshes.push((input.key, mesh)); + } + Done::Mesh { active, meshes } + } + }; + if tx.send(done).is_err() { break; } } + worker.stop.store(true, Ordering::Relaxed); + worker.wake.notify_all(); + for handle in sampling { + let _ = handle.join(); + } }) .expect("far terrain worker"); Self { shared, thread: Some(handle), results, + #[cfg(test)] context, + meshing: false, + invalidated: FxHashSet::default(), area: None, plan: Plan::default(), pending: FxHashSet::default(), + sample_nearby: true, + detail_masks: FxHashMap::default(), + detail_request_pending: false, + detail_request_at: std::time::Instant::now(), ready: FxHashMap::default(), active: FxHashSet::default(), dirty: FxHashSet::default(), @@ -235,61 +624,55 @@ impl Lod { covered: FxHashSet::default(), covered_bounds: None, saved: FxHashMap::default(), + reduced: FxHashMap::default(), saved_order: VecDeque::new(), - styles: FxHashMap::default(), } } pub fn stats(&self) -> (usize, usize) { (self.active.len(), self.ready.len()) } + pub fn progress(&self) -> (usize, usize) { + let mut desired = 0; + let mut sampled = 0; + for key in &self.plan.nodes { + if !self.plan.split.contains(key) { + desired += 1; + sampled += usize::from(self.ready.contains_key(key)); + } + } + (desired, sampled) + } + pub fn refined_samples(&self) -> usize { + self.active + .iter() + .filter_map(|key| self.detail_masks.get(key)) + .flat_map(|mask| mask.iter()) + .map(|bits| bits.count_ones() as usize) + .sum() + } pub fn removed(&mut self) -> Vec { std::mem::take(&mut self.removed) } pub fn invalidate_chunk(&mut self, x: i32, z: i32) { + if self.meshing { + self.invalidated.insert((x, z)); + } for &key in &self.active { let [ox, _, oz] = key.origin(); - if x * 16 < ox + key.size() - && (x + 1) * 16 > ox - && z * 16 < oz + key.size() - && (z + 1) * 16 > oz + if x * 16 <= ox + key.size() + && (x + 1) * 16 >= ox + && z * 16 <= oz + key.size() + && (z + 1) * 16 >= oz { self.dirty.insert(key); } } } - /// Sample only sixteen columns, from current block storage (heightmaps can - /// be stale after edits). Memory stays bounded while exploring indefinitely. + /// Capture actual block columns, including canopy gaps and material layers. pub fn observe(&mut self, chunk: &Chunk, facts: &BlockFacts) { - let values = std::array::from_fn(|i| { - let x = (i & 3) as i32 * 4; - let z = (i >> 2) as i32 * 4; - let mut water = None; - for y in (MIN_Y..MIN_Y + HEIGHT).rev() { - let state = BlockStateId(chunk.get(x, y, z)); - if facts.is_air(state) { - continue; - } - let f = facts.get(state); - if !matches!(f.fluid, Fluid::None) { - if water.is_none() { - water = Some((y + 1, state)); - } - } else if f.solid_render || f.name.ends_with("_leaves") { - return Some(Surface { - height: y + 1, - state, - water, - biome: chunk.biome(x, y, z), - }); - } - } - water.map(|(y, state)| Surface { - height: MIN_Y, - state: facts.air, - water: Some((y, state)), - biome: chunk.biome(x, y - 1, z), - }) - }); + let values = Arc::new(std::array::from_fn(|i| { + Column::from_chunk(chunk, facts, (i & 15) as i32, (i >> 4) as i32) + })); let key = (chunk.x, chunk.z); if self.saved.get(&key) == Some(&values) { return; @@ -298,36 +681,210 @@ impl Lod { while self.saved.len() >= MAX_SAVED { if let Some(old) = self.saved_order.pop_front() { self.saved.remove(&old); + self.reduced.remove(&old); } } self.saved_order.push_back(key); } - self.saved.insert(key, values); + self.apply_fine(key, &values); + self.saved.insert(key, values.clone()); + self.reduced.remove(&key); + let mut jobs = self.shared.jobs.lock().unwrap(); + // Coalesce edits and drop jobs whose bounded visited cache was evicted. + jobs.retain(|job| !matches!(job, Job::Reduce((at, _)) if *at == key || !self.saved.contains_key(at))); + jobs.push_back(Job::Reduce((key, values))); + drop(jobs); + self.shared.wake.notify_all(); self.invalidate_chunk(chunk.x, chunk.z); } + fn apply_fine(&mut self, (cx, cz): (i32, i32), columns: &[Column; 256]) { + let fine = TileKey { + x: cx, + z: cz, + level: 0, + }; + if self.plan.nodes.contains(&fine) || self.ready.contains_key(&fine) { + self.ready.insert(fine, columns.to_vec()); + self.detail_masks.insert(fine, [u64::MAX; 4]); + } + } + fn apply_detail(&mut self, (cx, cz): (i32, i32), columns: &ReducedColumns) { + self.apply_fine((cx, cz), &columns.columns); + for (&key, samples) in &mut self.ready { + let [ox, _, oz] = key.origin(); + let stride = key.size() / 16; + // A single generated chunk cannot describe a larger cell's area. + if stride > 16 { + continue; + } + let x0 = ((cx * 16 - ox + stride - 1).div_euclid(stride)).clamp(0, 16); + let x1 = (((cx + 1) * 16 - ox + stride - 1).div_euclid(stride)).clamp(0, 16); + let z0 = ((cz * 16 - oz + stride - 1).div_euclid(stride)).clamp(0, 16); + let z1 = (((cz + 1) * 16 - oz + stride - 1).div_euclid(stride)).clamp(0, 16); + if x0 == x1 || z0 == z1 { + continue; + } + let mask = self.detail_masks.entry(key).or_default(); + for z in z0..z1 { + for x in x0..x1 { + let index = (z * 16 + x) as usize; + samples[index] = columns + .get( + (ox + x * stride).rem_euclid(16) as usize, + (oz + z * stride).rem_euclid(16) as usize, + stride as usize, + ) + .clone(); + mask[index / 64] |= 1 << (index % 64); + } + } + } + } + fn finish_sample(&mut self, key: TileKey, samples: Vec) { + self.pending.remove(&key); + if self.plan.nodes.contains(&key) && samples.len() == CELLS * CELLS { + let mut samples = samples; + if let (Some(old), Some(mask)) = (self.ready.get(&key), self.detail_masks.get(&key)) { + for i in 0..256 { + if mask[i / 64] & (1 << (i % 64)) != 0 { + samples[i] = old[i].clone(); + } + } + } + self.ready.insert(key, samples); + } + } + fn finish_detail(&mut self, key: (i32, i32), columns: ReducedColumns) { + if let Some(visited) = self.saved.get(&key).cloned() + && !Arc::ptr_eq(&visited, &columns.columns) + { + // Its latest reduction is queued; never restore old edits or seed data. + self.apply_fine(key, &visited); + return; + } + self.apply_detail(key, &columns); + if self.saved.contains_key(&key) { + self.reduced.insert(key, columns); + } + self.invalidate_chunk(key.0, key.1); + } + fn request_detail(&mut self, camera: [i32; 2]) { + if self.detail_request_pending + || self.detail_request_at.elapsed() < std::time::Duration::from_millis(250) + { + return; + } + self.detail_request_at = std::time::Instant::now(); + let mut keys: Vec<_> = self + .active + .iter() + .filter(|key| { + key.level <= 1 + && !self.plan.split.contains(key) + && !(key.level == 0 && self.covered.contains(&(key.x, key.z))) + && self.detail_masks.get(key) != Some(&[u64::MAX; 4]) + }) + .copied() + .collect(); + keys.sort_unstable_by_key(|key| (key.distance(camera), *key)); + let mut chunks = FxHashSet::default(); + for key in keys { + let [ox, _, oz] = key.origin(); + let stride = key.size() / 16; + let mask = self.detail_masks.get(&key).copied().unwrap_or_default(); + for index in 0..256 { + if mask[index / 64] & (1 << (index % 64)) == 0 { + let chunk = ( + (ox + (index as i32 % 16) * stride).div_euclid(16), + (oz + (index as i32 / 16) * stride).div_euclid(16), + ); + if self.covered.contains(&chunk) { + self.detail_masks.entry(key).or_default()[index / 64] |= 1 << (index % 64); + } else { + chunks.insert(chunk); + } + } + } + if chunks.len() >= 32 { + break; + } + } + let mut chunks: Vec<_> = chunks.into_iter().collect(); + chunks.sort_unstable_by_key(|&(x, z)| { + let dx = i64::from(x * 16 + 8 - camera[0]); + let dz = i64::from(z * 16 + 8 - camera[1]); + dx * dx + dz * dz + }); + chunks.truncate(32); + if !chunks.is_empty() { + self.detail_request_pending = true; + self.shared + .jobs + .lock() + .unwrap() + .push_back(Job::RequestDetail { + chunks, + center: camera, + }); + self.shared.wake.notify_all(); + } + } pub fn update( &mut self, camera: [f64; 3], + yaw: f32, detail: i32, distance: u32, covered: FxHashSet<(i32, i32)>, ) -> Vec<(TileKey, SectionMesh)> { + let elevation = + (((camera[1] - f64::from(MIN_Y + crate::chunk::HEIGHT)).clamp(0., 16384.)) as i32 / 32) + * 32; let camera = [camera[0].floor() as i32, camera[2].floor() as i32]; + // Fill the view ahead first; a nearest-only ring spends most work out + // of sight while visible forests remain undecorated noise terrain. + let ahead = (detail * 16 + 128).max(elevation * 2).min(4096) as f32; + let yaw = yaw.to_radians(); + let focus = [ + camera[0] - (yaw.sin() * ahead) as i32, + camera[1] + (yaw.cos() * ahead) as i32, + ]; // Replan at chunk boundaries, not on every camera movement. let area = ( [camera[0].div_euclid(16), camera[1].div_euclid(16)], detail, distance, + elevation, ); if self.area != Some(area) { self.area = Some(area); - self.plan = Plan::new(camera, detail, distance); + self.plan = Plan::with_previous(camera, detail, distance, &self.plan, elevation); self.ready.retain(|key, _| self.plan.nodes.contains(key)); + self.detail_masks + .retain(|key, _| self.plan.nodes.contains(key)); } + let mut meshes = Vec::new(); while let Ok(done) = self.results.try_recv() { - self.pending.remove(&done.key); - if self.plan.nodes.contains(&done.key) && done.samples.len() == CELLS * CELLS { - self.ready.insert(done.key, done.samples); + match done { + Done::Detail(key, columns) => { + crate::spans::time("lod_detail", || self.finish_detail(key, columns)) + } + Done::DetailRequested => self.detail_request_pending = false, + Done::Sample { key, samples } => self.finish_sample(key, samples), + Done::Mesh { + active, + meshes: completed, + } => { + // Retire parents only when the entire replacement is ready. + self.removed + .extend(self.active.difference(&active).copied()); + self.active = active; + self.meshing = false; + for (x, z) in std::mem::take(&mut self.invalidated) { + self.invalidate_chunk(x, z); + } + meshes.extend(completed); + } } } if self.covered != covered { @@ -352,262 +909,244 @@ impl Lod { }); self.covered = covered; } - let mut selected = Vec::new(); - for &key in &self.plan.roots { - self.plan.select(key, &self.ready, &mut selected); - } - let next: FxHashSet<_> = selected.iter().copied().collect(); - self.removed.extend(self.active.difference(&next).copied()); - self.dirty.extend(next.difference(&self.active).copied()); - self.active = next; - self.dirty.retain(|key| self.active.contains(key)); - let keys: Vec<_> = self.dirty.drain().collect(); - let meshes = keys.into_iter().map(|key| (key, self.mesh(key))).collect(); - let mut wanted: Vec<_> = self - .plan - .nodes - .iter() - .copied() - .filter(|key| !self.ready.contains_key(key) && !self.pending.contains(key)) - .collect(); - // Coarse ancestors first: cover the horizon quickly, then refine. - wanted - .sort_unstable_by_key(|key| (std::cmp::Reverse(key.level), key.distance(camera), *key)); - let mut jobs = self.shared.jobs.lock().unwrap(); - for key in wanted - .into_iter() - .take(2usize.saturating_sub(self.pending.len())) - { - self.pending.insert(key); - jobs.push_back(Job { key }); - } - self.shared.wake.notify_one(); - meshes - } - fn style(&mut self, state: BlockStateId) -> Style { - if let Some(style) = self.styles.get(&state) { - return *style; - } - let registry = self.context.assets.registry; - let name = registry.state(state).map(|s| s.name.as_str()).unwrap_or(""); - let fluid = match name { - "minecraft:water" | "minecraft:bubble_column" => Some(("block/water_still", true, -2)), - "minecraft:lava" => Some(("block/lava_still", false, -1)), - _ => None, - }; - let light = - 240 | (u16::from(self.context.tables.facts(state).map_or(0, |f| f.emission)) << 12); - let style = if let Some((sprite, translucent, tint)) = fluid { - Style { - layer: self - .context - .assets - .sprite_layers - .get(sprite) - .or_else(|| { - self.context - .assets - .sprite_layers - .get(&format!("minecraft:{sprite}")) - }) - .copied() - .unwrap_or(0) as u16, - tint, - translucent, - light, + // Complete resident columns need no duplicate noise work. Their real + // summaries are retained when the full-detail chunks leave the view. + for &key in &self.plan.nodes { + if key.level == 0 && self.covered.contains(&(key.x, key.z)) { + self.ready + .entry(key) + .or_insert_with(|| vec![Column::default(); CELLS * CELLS]); } - } else { - let models = self - .context - .assets - .model_catalog - .collect(state, BlockPos::new(0, 0, 0)) - .ok(); - let face = models.as_ref().and_then(|m| { - m.iter() - .flat_map(|(_, m)| &m.faces) - .find(|f| f.quad.facing == horizon_assets::model::Direction::Up) - }); - Style { - layer: face.map_or(0, |f| f.layer as u16), - tint: if face.is_some_and(|f| f.quad.tint_index >= 0) { - if name.contains("leaves") { -3 } else { 0 } - } else { - -1 - }, - translucent: face.is_some_and(|f| f.translucent), - light, + } + if !self.meshing { + let mut selected = Vec::new(); + for &key in &self.plan.roots { + self.plan.select(key, &self.ready, &mut selected); } - }; - self.styles.insert(state, style); - style - } - fn mesh(&mut self, key: TileKey) -> SectionMesh { - let mut mesh = SectionMesh::default(); - let stride = key.size() / CELLS as i32; - let [ox, _, oz] = key.origin(); - let mut samples = self.ready[&key].clone(); - for z in 0..CELLS { - for x in 0..CELLS { - let (wx, wz) = (ox + x as i32 * stride, oz + z as i32 * stride); - if let Some(saved) = self.saved.get(&(wx.div_euclid(16), wz.div_euclid(16))) { - samples[z * CELLS + x] = - saved[((wz.rem_euclid(16) / 4) * 4 + wx.rem_euclid(16) / 4) as usize]; + // Their normal meshes already provide coverage. Empty stand-ins + // must not become hundreds of redundant fallback clipping masks. + selected.retain(|key| key.level != 0 || !self.covered.contains(&(key.x, key.z))); + let next: FxHashSet<_> = selected.iter().copied().collect(); + let changed: Vec<_> = self.active.symmetric_difference(&next).copied().collect(); + for &tile in &next { + let [x, _, z] = tile.origin(); + let size = tile.size(); + if changed.iter().any(|other| { + let [ox, _, oz] = other.origin(); + let os = other.size(); + (x < ox + os && ox < x + size && z < oz + os && oz < z + size) + || (((x + size == ox) || (ox + os == x)) && z < oz + os && oz < z + size) + || (((z + size == oz) || (oz + os == z)) && x < ox + os && ox < x + size) + }) { + self.dirty.insert(tile); } } - } - for z in 0..CELLS { - for x in 0..CELLS { - let (wx, wz) = (ox + x as i32 * stride, oz + z as i32 * stride); - let Some(sample) = samples[z * CELLS + x] else { - continue; - }; - // Only fine cells overlap near chunks; coarse parent fallbacks - // also clip individual chunk-sized pieces while children load. - let overlaps = self.covered_bounds.is_some_and(|[x0, z0, x1, z1]| { - wx < x1 && wx + stride > x0 && wz < z1 && wz + stride > z0 + self.dirty.extend(next.difference(&self.active).copied()); + self.dirty.retain(|key| next.contains(key)); + if !self.dirty.is_empty() || next != self.active { + let inputs = self + .dirty + .iter() + .map(|&key| crate::spans::time("lod_input", || self.mesh_input(key, &next))) + .collect(); + self.dirty.clear(); + self.meshing = true; + self.shared.jobs.lock().unwrap().push_front(Job::Mesh { + active: next, + inputs, + covered: self.covered.clone(), + bounds: self.covered_bounds, }); - let step = if overlaps { stride.min(16) } else { stride }; - for dz in (0..stride).step_by(step as usize) { - for dx in (0..stride).step_by(step as usize) { - if self - .covered - .contains(&((wx + dx).div_euclid(16), (wz + dz).div_euclid(16))) - { - continue; - } - let (x0, z0, x1, z1) = ( - (x as i32 * stride + dx) as f32, - (z as i32 * stride + dz) as f32, - (x as i32 * stride + dx + step) as f32, - (z as i32 * stride + dz + step) as f32, - ); - let y = sample.height as f32; - if y > MIN_Y as f32 { - let style = self.style(sample.state); - let color = self.color(style, sample.biome); - quad( - &mut mesh, - style, - color, - [[x0, y, z1], [x1, y, z1], [x1, y, z0], [x0, y, z0]], - 1.0, - ); - for (nx, nz, cell_edge, neighbor, points, shade) in [ - ( - x as i32 - 1, - z as i32, - dx == 0, - [wx + dx - 1, wz + dz], - [[x0, y, z0], [x0, y, z1], [x0, 0.0, z1], [x0, 0.0, z0]], - 0.6, - ), - ( - x as i32 + 1, - z as i32, - dx + step == stride, - [wx + dx + step, wz + dz], - [[x1, y, z1], [x1, y, z0], [x1, 0.0, z0], [x1, 0.0, z1]], - 0.6, - ), - ( - x as i32, - z as i32 - 1, - dz == 0, - [wx + dx, wz + dz - 1], - [[x1, y, z0], [x0, y, z0], [x0, 0.0, z0], [x1, 0.0, z0]], - 0.8, - ), - ( - x as i32, - z as i32 + 1, - dz + step == stride, - [wx + dx, wz + dz + step], - [[x0, y, z1], [x1, y, z1], [x1, 0.0, z1], [x0, 0.0, z1]], - 0.8, - ), - ] { - let cut_edge = self.covered.contains(&( - neighbor[0].div_euclid(16), - neighbor[1].div_euclid(16), - )); - let floor = if cut_edge { - // Near terrain may be below the coarse top; - // close the cut instead of exposing its underside. - MIN_Y - } else if !cell_edge { - continue; - } else if (0..CELLS as i32).contains(&nx) - && (0..CELLS as i32).contains(&nz) - { - samples[(nz as usize) * CELLS + nx as usize] - .map_or(MIN_Y, |s| s.height) - } else { - MIN_Y - }; - if floor < sample.height { - let mut p = points; - p[2][1] = floor as f32; - p[3][1] = floor as f32; - p.reverse(); - quad(&mut mesh, style, color, p, shade); - } - } - } - if let Some((height, state)) = sample.water { - let style = self.style(state); - let color = self.color(style, sample.biome); - let y = height as f32 - 1.0 / 9.0; - quad( - &mut mesh, - style, - color, - [[x0, y, z1], [x1, y, z1], [x1, y, z0], [x0, y, z0]], - 1.0, - ); - } + } + } + // Load desired nearby leaves directly, alongside broad fallback coverage. + // Waiting for every ancestor/sibling made full detail take minutes to appear. + let mut wanted = Vec::new(); + for &root in &self.plan.roots { + if self.plan.nodes.contains(&root) && !self.ready.contains_key(&root) { + wanted.push((0, 0, root)); + } + } + for &key in &self.plan.nodes { + if !self.plan.split.contains(&key) && !self.ready.contains_key(&key) { + wanted.push(( + if key.in_view(camera, yaw) { 1 } else { 3 }, + i64::from(key.distance(focus)) * 4 + i64::from(key.distance(camera)), + key, + )); + } + } + let mut fallback = Vec::new(); + for &parent in &self.active { + if self.plan.split.contains(&parent) { + for child in parent.children() { + if self.plan.nodes.contains(&child) && !self.ready.contains_key(&child) { + // Covered near terrain bounds projected error. Using + // zero distance for a parent touching the camera kept + // refining it while huge visible walls waited elsewhere. + let priority = + i64::from(child.distance(camera).max((detail * 16).max(elevation))) + * 1024 + / i64::from(child.size()); + fallback.push(( + if child.in_view(camera, yaw) { 2 } else { 4 }, + priority, + child, + )); } } } } - mesh.faces = FaceGroups::group(&mut mesh.solid); - mesh + wanted.sort_unstable(); + fallback.sort_unstable(); + // Alternate one nearby final tile with one broad refinement tile. + if let Some(fallback) = fallback + .into_iter() + .find(|(_, _, k)| !self.pending.contains(k)) + { + let index = if self.sample_nearby { + wanted + .iter() + .position(|(_, _, k)| !self.pending.contains(k)) + .map_or(0, |i| i + 1) + } else { + 0 + }; + wanted.insert(index, fallback); + } + let mut unique = FxHashSet::default(); + let wanted: Vec<_> = wanted + .into_iter() + .map(|(_, _, key)| key) + .filter(|key| !self.pending.contains(key) && unique.insert(*key)) + .collect(); + self.request_detail(focus); + let mut jobs = self.shared.jobs.lock().unwrap(); + for key in wanted + .into_iter() + .take(SAMPLE_JOBS.saturating_sub(self.pending.len())) + { + self.pending.insert(key); + self.sample_nearby = !self.sample_nearby; + jobs.push_back(Job::Sample(key)); + } + self.shared.wake.notify_all(); + meshes } - fn color(&self, style: Style, biome: u16) -> u32 { - let b = self.context.biomes.get(biome); - match style.tint { - -2 => b.water, - -3 => b.foliage, - 0 => b.grass, - _ => 0xffffff, + fn visited_column(&self, wx: i32, wz: i32, width: i32) -> Option<&Column> { + let key = (wx.div_euclid(16), wz.div_euclid(16)); + if width == 1 { + self.saved + .get(&key) + .map(|columns| &columns[(wz.rem_euclid(16) * 16 + wx.rem_euclid(16)) as usize]) + } else if width <= 16 { + self.reduced.get(&key).map(|columns| { + columns.get( + wx.rem_euclid(16) as usize, + wz.rem_euclid(16) as usize, + width as usize, + ) + }) + } else { + None } } -} -fn quad(mesh: &mut SectionMesh, style: Style, color: u32, points: [[f32; 3]; 4], shade: f32) { - let channel = |shift: u32| (((color >> shift) & 255u32) as f32 * shade) as u32; - let color = channel(16) | (channel(8) << 8) | (channel(0) << 16) | 0xff000000; - let out = if style.translucent { - &mut mesh.translucent - } else { - &mut mesh.solid - }; - out.extend( - points - .into_iter() - .zip([[0., 1.], [1., 1.], [1., 0.], [0., 0.]]) - .map(|(pos, uv)| TerrainVertex { - pos, - uv, - layer: style.layer, - light: style.light, - color, - }), - ); + fn neighbor(&self, wx: i32, wz: i32, active: &FxHashSet) -> Option<(Column, i32)> { + for level in 0..=10 { + let size = 16 << level; + let key = TileKey { + x: wx.div_euclid(size), + z: wz.div_euclid(size), + level, + }; + if !active.contains(&key) { + continue; + } + let stride = 1 << level; + let [ox, _, oz] = key.origin(); + let x = (wx - ox) / stride; + let z = (wz - oz) / stride; + let (sx, sz) = (ox + x * stride, oz + z * stride); + if let Some(column) = self.visited_column(sx, sz, stride) { + return Some((column.clone(), stride)); + } + return Some((self.ready[&key][(z * 16 + x) as usize].clone(), stride)); + } + None + } + fn mesh_input(&self, key: TileKey, active: &FxHashSet) -> MeshInput { + let stride = key.size() / CELLS as i32; + let [ox, _, oz] = key.origin(); + let mut columns = self.ready[&key].clone(); + for z in 0..CELLS { + for x in 0..CELLS { + let (wx, wz) = (ox + x as i32 * stride, oz + z as i32 * stride); + if let Some(column) = self.visited_column(wx, wz, stride) { + columns[z * CELLS + x] = column.clone(); + } + } + } + let neighbors = std::array::from_fn(|direction| { + let mut edge = Vec::new(); + let mut offset = 0; + while offset < key.size() { + let (wx, wz) = match direction { + 0 => (ox - 1, oz + offset), + 1 => (ox + key.size(), oz + offset), + 2 => (ox + offset, oz - 1), + _ => (ox + offset, oz + key.size()), + }; + let (column, neighbor_stride) = self + .neighbor(wx, wz, active) + .unwrap_or_else(|| (Column::default(), stride)); + let coordinate = if direction < 2 { wz } else { wx }; + let width = (neighbor_stride - coordinate.rem_euclid(neighbor_stride)) + .min(key.size() - offset); + edge.push(mesh::EdgeColumn { + start: offset, + end: offset + width, + column, + }); + offset += width; + } + edge + }); + MeshInput { + key, + columns, + neighbors, + finer: active + .iter() + .copied() + .filter(|other| { + let [x, _, z] = other.origin(); + other.level < key.level + && x >= ox + && z >= oz + && x + other.size() <= ox + key.size() + && z + other.size() <= oz + key.size() + }) + .collect(), + } + } + #[cfg(test)] + fn mesh(&self, key: TileKey) -> SectionMesh { + let input = self.mesh_input(key, &self.active); + mesh::Mesher::default().mesh( + key, + &input.columns, + &self.covered, + self.covered_bounds, + &self.context, + &input.neighbors, + &input.finer, + ) + } } impl Drop for Lod { fn drop(&mut self) { self.shared.stop.store(true, Ordering::Relaxed); - self.shared.wake.notify_one(); + self.shared.wake.notify_all(); if let Some(thread) = self.thread.take() { let _ = thread.join(); } @@ -616,12 +1155,13 @@ impl Drop for Lod { #[cfg(test)] mod tests { use super::*; + use crate::mesher::TerrainVertex; #[test] fn far_plan_is_bounded_and_near_tiles_align_with_chunks() { for camera in [[0, 0], [-1, -17], [1023, -4097]] { for distance in [64, 256, 1024] { let plan = Plan::new(camera, 12, distance); - assert!(plan.nodes.len() < 2500, "{} nodes", plan.nodes.len()); + assert!(plan.nodes.len() < 12000, "{} nodes", plan.nodes.len()); for key in &plan.nodes { if !plan.split.contains(key) && key.distance(camera) < 12 * 16 { assert_eq!(key.level, 0); @@ -632,7 +1172,7 @@ mod tests { .iter() .filter(|k| !plan.split.contains(k)) .count(); - assert!(cells < 2000); + assert!(cells < 10000); } } assert_eq!( @@ -642,11 +1182,33 @@ mod tests { level: 1 } .origin(), - [-128, 0, -256] + [-32, 0, -64] ); } #[test] - fn parent_covers_missing_children_then_retires_without_overlap() { + fn aerial_distance_avoids_unseen_full_detail_outside_the_near_area() { + let ground = Plan::new([0, 0], 12, 1024); + let high = Plan::with_previous([0, 0], 12, 1024, &Plan::default(), 1024); + assert!(high.nodes.len() < ground.nodes.len()); + assert!( + high.nodes.contains(&TileKey { + x: 0, + z: 0, + level: 0 + }), + "near full-detail footprint stays exact" + ); + assert!( + !high.nodes.contains(&TileKey { + x: 20, + z: 0, + level: 0 + }), + "far terrain viewed from high above needs distance-appropriate detail" + ); + } + #[test] + fn late_parent_keeps_ready_detail_and_only_fills_missing_children() { let parent = TileKey { x: -1, z: 0, @@ -661,8 +1223,13 @@ mod tests { ready.insert(parent, vec![]); ready.insert(parent.children()[0], vec![]); let mut selected = Vec::new(); + ready.remove(&parent); + assert!(!plan.select(parent, &ready, &mut selected)); + assert_eq!(selected, [parent.children()[0]]); + ready.insert(parent, vec![]); + selected.clear(); assert!(plan.select(parent, &ready, &mut selected)); - assert_eq!(selected, [parent]); + assert_eq!(selected, [parent.children()[0], parent]); for child in parent.children() { ready.insert(child, vec![]); } @@ -671,36 +1238,89 @@ mod tests { assert_eq!(selected, parent.children()); } #[test] - fn coarse_quad_keeps_winding_light_and_transparency() { - let mut mesh = SectionMesh::default(); - let style = Style { - layer: 7, - tint: -1, - translucent: true, + fn detail_survives_movement_across_a_split_threshold() { + let before = Plan::new([0, 0], 2, 256); + let after = Plan::with_previous([16, 0], 2, 256, &before, 0); + let mut retained = 0; + for key in &before.split { + if key.distance([16, 0]) < key.size() * 10 { + assert!(after.split.contains(key)); + retained += 1; + } + } + assert!(retained > 100); + assert!(after.nodes.len() < 16000); + } + #[test] + fn reduced_material_votes_ignore_light_and_retain_exposure() { + let mut columns: [Column; 256] = std::array::from_fn(|_| Column::default()); + for i in 0..16 { + columns[(i / 4) * 16 + i % 4].spans.push(Span { + min: 60, + max: 64, + state: BlockStateId(if i < 10 { 2 } else { 1 }), + biome: 0, + light: if i < 10 { (i as u16 + 6) << 4 } else { 0 }, + }); + } + let coarse = Column::reduced(&columns, 0, 0, 4); + assert_eq!(coarse.spans.len(), 1); + assert_eq!( + coarse.spans[0].state, + BlockStateId(2), + "majority grass must beat dark dirt" + ); + assert_eq!(coarse.spans[0].light, 240, "retain sampled sky exposure"); + for column in &mut columns { + for span in &mut column.spans { + span.light = 0; + } + } + assert_eq!( + Column::reduced(&columns, 0, 0, 4).spans[0].light, + 0, + "unlit caves stay unlit" + ); + columns[0].spans[0].light = 7 << 12; + assert_eq!( + Column::reduced(&columns, 0, 0, 4).spans[0].light, + 7 << 12, + "block light does not invent skylight" + ); + } + #[test] + fn coarse_voxels_reduce_the_area_instead_of_inflating_one_leaf() { + let span = |min, max, state| Span { + min, + max, + state: BlockStateId(state), + biome: 0, light: 240, }; - quad( - &mut mesh, - style, - 0x123456, - [ - [0., 70., 64.], - [64., 70., 64.], - [64., 70., 0.], - [0., 70., 0.], - ], - 1., + let mut columns = std::array::from_fn(|_| Column { + surface: None, + spans: vec![span(0, 4, 1)], + }); + columns[0].spans.push(span(8, 10, 2)); + assert_eq!(Column::reduced(&columns, 0, 0, 4).spans, [span(0, 4, 1)]); + for z in 0..4 { + for x in 0..4 { + columns[z * 16 + x].spans.push(span(12, 14, 3)); + } + } + assert_eq!( + Column::reduced(&columns, 0, 0, 4).spans, + [span(0, 4, 1), span(12, 14, 3)] ); - assert!(mesh.solid.is_empty()); - assert_eq!(mesh.translucent.len(), 4); - assert_eq!(mesh.translucent[0].color, 0xff563412); - let q = &mesh.translucent; - assert!( - (glam::Vec3::from(q[1].pos) - glam::Vec3::from(q[0].pos)) - .cross(glam::Vec3::from(q[2].pos) - glam::Vec3::from(q[0].pos)) - .y - > 0. + assert_eq!(Column::reduced(&columns, 0, 0, 1), columns[0]); + let reduced = ReducedColumns::new(Arc::new(columns)); + assert_eq!( + reduced.get(0, 0, 1).spans, + [span(0, 4, 1), span(8, 10, 2), span(12, 14, 3)] ); + assert_eq!(reduced.get(0, 0, 4).spans, [span(0, 4, 1), span(12, 14, 3)]); + assert_eq!(reduced.get(4, 8, 4).spans, [span(0, 4, 1)]); + assert_eq!(reduced.get(0, 0, 16).spans, [span(0, 4, 1)]); } #[test] #[ignore = "requires prepared models/textures in HORIZON_TEST_RESOURCES"] @@ -733,17 +1353,17 @@ mod tests { let key = TileKey { x: -1, z: -1, - level: 3, + level: 5, }; lod.ready.insert( key, vec![ - Some(Surface { + Column::from_surface(Some(Surface { height: 70, state: stone, water: Some((80, water)), biome: biomes.id("minecraft:plains"), - }); + })); CELLS * CELLS ], ); @@ -839,10 +1459,244 @@ mod tests { } lod.observe(&chunk, &facts); let observed = &lod.saved[&(-1, -1)]; - assert_eq!(observed[0].unwrap().height, 121); - assert_eq!(observed[0].unwrap().state, stone); - assert_eq!(observed[5].unwrap().height, 55); - assert_eq!(observed[5].unwrap().water, Some((64, water))); + assert_eq!(observed[0].surface.unwrap().height, 121); + assert_eq!(observed[0].surface.unwrap().state, stone); + assert_eq!(observed[68].surface.unwrap().height, 55); + assert_eq!(observed[68].surface.unwrap().water, Some((64, water))); + + let fine = TileKey { + x: -1, + z: -1, + level: 0, + }; + lod.ready.insert(fine, vec![Column::default(); 256]); + lod.finish_detail( + (-1, -1), + ReducedColumns::new(Arc::new(std::array::from_fn(|_| Column::default()))), + ); + assert_eq!( + lod.ready[&fine][0].surface.unwrap().height, + 121, + "late seed generation cannot overwrite a visited build" + ); + assert_eq!(lod.detail_masks[&fine], [u64::MAX; 4]); + lod.plan.nodes.insert(fine); + lod.pending.insert(fine); + lod.finish_sample(fine, vec![Column::default(); 256]); + assert_eq!( + lod.ready[&fine][0].surface.unwrap().height, + 121, + "a late coarse sampling job cannot erase completed detail" + ); + assert_eq!(lod.detail_masks[&fine], [u64::MAX; 4]); + lod.plan.nodes.remove(&fine); + + // Real worker results populate the reusable pyramid; an older result + // must never put a previous edit back into that cache. + let receive_reduction = |lod: &mut Lod| { + let Done::Detail(key, columns) = lod + .results + .recv_timeout(std::time::Duration::from_secs(5)) + .unwrap() + else { + panic!("expected queued visited reduction"); + }; + lod.finish_detail(key, columns); + }; + receive_reduction(&mut lod); + assert!(Arc::ptr_eq( + &lod.reduced[&(-1, -1)].columns, + &lod.saved[&(-1, -1)] + )); + assert!(lod.visited_column(-16, -16, 2).unwrap().spans.is_empty()); + let old = lod.saved[&(-1, -1)].clone(); + chunk.set(0, 130, 0, stone.0); + lod.observe(&chunk, &facts); + assert!(!lod.reduced.contains_key(&(-1, -1))); + lod.finish_detail((-1, -1), ReducedColumns::new(old)); + assert!(!lod.reduced.contains_key(&(-1, -1))); + assert_eq!(lod.ready[&fine][0].surface.unwrap().height, 131); + receive_reduction(&mut lod); + assert!(Arc::ptr_eq( + &lod.reduced[&(-1, -1)].columns, + &lod.saved[&(-1, -1)] + )); + + // Exercise the actual worker/update seam: a parent stays displayed + // until all four child meshes can replace it in the same update. + let camera = [-256.0, 100.0, -256.0]; + lod.area = Some(([-16, -16], 2, 64, 0)); + lod.plan.roots = vec![key]; + lod.plan.nodes.insert(key); + let await_meshes = |lod: &mut Lod| { + let deadline = std::time::Instant::now() + std::time::Duration::from_secs(5); + loop { + let meshes = lod.update(camera, 0.0, 2, 64, lod.covered.clone()); + if !meshes.is_empty() { + return meshes; + } + assert!(std::time::Instant::now() < deadline, "mesh worker stalled"); + std::thread::sleep(std::time::Duration::from_millis(1)); + } + }; + let uploaded = await_meshes(&mut lod); + assert_eq!(uploaded.len(), 1); + assert_eq!(uploaded[0].0, key); + assert!(lod.active.contains(&key)); + lod.plan.split.insert(key); + for child in key.children() { + lod.plan.nodes.insert(child); + lod.ready.insert(child, lod.ready[&key].clone()); + } + assert!( + lod.update(camera, 0.0, 2, 64, lod.covered.clone()) + .is_empty() + ); + assert!( + lod.active.contains(&key), + "parent retired before child meshes" + ); + assert!(lod.removed().is_empty()); + let uploaded = await_meshes(&mut lod); + assert_eq!(uploaded.len(), 4); + assert!( + key.children() + .iter() + .all(|child| lod.active.contains(child)) + ); + assert_eq!(lod.removed(), [key]); + + let covered_fine = TileKey { + x: -1, + z: -2, + level: 0, + }; + lod.plan = Plan::default(); + lod.plan.roots.push(covered_fine); + lod.plan.nodes.insert(covered_fine); + lod.ready.insert(covered_fine, vec![Column::default(); 256]); + lod.covered.insert((-1, -2)); + let deadline = std::time::Instant::now() + std::time::Duration::from_secs(5); + loop { + lod.update(camera, 0., 2, 64, lod.covered.clone()); + if !lod.meshing && lod.active.is_empty() { + break; + } + assert!( + std::time::Instant::now() < deadline, + "covered normal chunk remained an active LOD mask" + ); + std::thread::sleep(std::time::Duration::from_millis(1)); + } + assert_eq!(lod.progress(), (1, 1)); + + // Keep one sample outstanding, as on ordinary frames. Completing just + // one job at a time must still let both loading queues make progress. + lod.shared.stop.store(true, Ordering::Relaxed); + lod.shared.wake.notify_all(); + lod.thread.take().unwrap().join().unwrap(); + while lod.results.try_recv().is_ok() {} + lod.shared.jobs.lock().unwrap().clear(); + lod.plan = Plan::new([-256, -256], 2, 64); + let parent = TileKey { + x: -2, + z: -2, + level: 3, + }; + lod.plan.roots = vec![parent]; + lod.ready.clear(); + lod.ready.insert(parent, vec![Column::default(); 256]); + lod.active = [parent].into_iter().collect(); + lod.pending = (0..SAMPLE_JOBS - 1) + .map(|i| TileKey { + x: 999 + i as i32, + z: 999, + level: 0, + }) + .collect(); + lod.sample_nearby = true; + lod.detail_request_pending = true; + lod.meshing = true; + lod.update(camera, 0.0, 2, 64, FxHashSet::default()); + let Some(Job::Sample(nearby)) = lod.shared.jobs.lock().unwrap().pop_front() else { + panic!("nearby detail was not scheduled"); + }; + assert_eq!(nearby.level, 0); + assert!( + nearby.origin()[2] > -256 + 64, + "detail should load ahead of the view" + ); + lod.finish_sample(nearby, vec![Column::default(); 256]); + lod.update(camera, 0.0, 2, 64, FxHashSet::default()); + let Some(Job::Sample(fallback)) = lod.shared.jobs.lock().unwrap().pop_front() else { + panic!("fallback refinement was starved"); + }; + assert!(fallback.level > 0); + lod.pending.remove(&fallback); + lod.sample_nearby = true; + lod.update(camera, 90.0, 2, 64, FxHashSet::default()); + let Some(Job::Sample(turned)) = lod.shared.jobs.lock().unwrap().pop_front() else { + panic!("turned view was not scheduled"); + }; + assert!( + turned.origin()[0] < -256 - 64, + "loading priority should follow a turn" + ); + lod.shared.jobs.lock().unwrap().clear(); + let near = TileKey { + x: -4, + z: -4, + level: 2, + }; + let far = TileKey { + x: 0, + z: 0, + level: 6, + }; + lod.plan = Plan::default(); + lod.plan.roots = vec![near, far]; + lod.ready.clear(); + for parent in [near, far] { + lod.plan.nodes.insert(parent); + lod.plan.split.insert(parent); + lod.plan.nodes.extend(parent.children()); + lod.ready.insert(parent, vec![Column::default(); 256]); + } + lod.active = [near, far].into_iter().collect(); + lod.shared.jobs.lock().unwrap().clear(); + lod.pending = (0..SAMPLE_JOBS - 1) + .map(|i| TileKey { + x: 999 + i as i32, + z: 999, + level: 0, + }) + .collect(); + lod.sample_nearby = false; + lod.update(camera, 0., 2, 64, FxHashSet::default()); + let Some(Job::Sample(broad)) = lod.shared.jobs.lock().unwrap().pop_front() else { + panic!("projected-error refinement missing") + }; + assert_eq!( + broad.level, 5, + "a near tile touching the camera must not starve a huge visible distant tile" + ); + lod.shared.jobs.lock().unwrap().clear(); + lod.active = (0..40).map(|x| TileKey { x, z: 0, level: 0 }).collect(); + lod.plan.split.clear(); + lod.detail_masks.clear(); + lod.covered = [(0, 0)].into_iter().collect(); + lod.detail_request_pending = false; + lod.detail_request_at = std::time::Instant::now() - std::time::Duration::from_secs(1); + lod.request_detail([0, 0]); + let Some(Job::RequestDetail { chunks, .. }) = lod.shared.jobs.lock().unwrap().pop_front() + else { + panic!("detail batch was not requested"); + }; + assert_eq!( + chunks, + (1..=32).map(|x| (x, 0)).collect::>(), + "one unfinished fine tile must not starve neighbouring requests" + ); Ok(()) } } diff --git a/crates/horizon-client/src/lod/mesh.rs b/crates/horizon-client/src/lod/mesh.rs new file mode 100644 index 000000000..82636e1d1 --- /dev/null +++ b/crates/horizon-client/src/lod/mesh.rs @@ -0,0 +1,1102 @@ +//! Exposed voxel-column faces, merged without changing their block-scale UVs. +use super::{Column, Span, TileKey}; +use crate::{ + fast_hash::{FxHashMap, FxHashSet}, + mesher::{FaceGroups, OPAQUE_LAYER, SectionMesh, TerrainVertex}, + streaming::WorkerContext, +}; +use horizon_assets::model::Direction; +use horizon_core::position::BlockPos; +use horizon_world::registry::BlockStateId; +use std::collections::BTreeMap; + +const CELLS: usize = 16; + +pub(super) struct EdgeColumn { + pub start: i32, + pub end: i32, + pub column: Column, +} + +#[derive(Clone, Copy)] +enum Tint { + None, + Grass, + Foliage, + Dry, + Water, + Constant(u32), +} +#[derive(Clone, Copy)] +struct Style { + layer: u16, + tint: Tint, + translucent: bool, +} +#[derive(Clone, Copy)] +struct Styles { + faces: [Option