From bae797575737c6568083faf6f929edc338892b53 Mon Sep 17 00:00:00 2001 From: Cosmic <84875247+Online5277@users.noreply.github.com> Date: Sat, 17 Oct 2026 18:05:39 -0400 Subject: [PATCH 1/6] Add bounded hierarchical surface terrain LODs for distant views (#718) --- crates/horizon-client/src/flatgen.rs | 37 +- crates/horizon-client/src/game.rs | 109 ++- crates/horizon-client/src/game/control.rs | 20 + crates/horizon-client/src/game/packs.rs | 1 + crates/horizon-client/src/game/remote.rs | 3 + crates/horizon-client/src/game/render_tick.rs | 2 +- crates/horizon-client/src/gfx.rs | 204 ++++- crates/horizon-client/src/gfx/lod.rs | 142 +++ crates/horizon-client/src/gfx/translucency.rs | 43 + crates/horizon-client/src/graphics.rs | 21 +- crates/horizon-client/src/graphics_ui.rs | 56 +- crates/horizon-client/src/lib.rs | 1 + crates/horizon-client/src/lod.rs | 862 ++++++++++++++++++ crates/horizon-client/src/mesher/facing.rs | 5 + crates/horizon-client/src/save.rs | 229 ++++- crates/horizon-client/src/streaming.rs | 46 +- crates/horizon-client/src/testgen.rs | 48 + crates/horizon-client/src/vanillagen.rs | 23 + .../horizon-world/src/worldgen/generator.rs | 119 ++- docs/graphics.md | 16 +- 20 files changed, 1923 insertions(+), 64 deletions(-) create mode 100644 crates/horizon-client/src/gfx/lod.rs create mode 100644 crates/horizon-client/src/lod.rs diff --git a/crates/horizon-client/src/flatgen.rs b/crates/horizon-client/src/flatgen.rs index 1cff68b11..339f1fca1 100644 --- a/crates/horizon-client/src/flatgen.rs +++ b/crates/horizon-client/src/flatgen.rs @@ -6,6 +6,7 @@ use horizon_world::{nbt::Tag, registry::BlockRegistry}; pub struct FlatTerrain { layers: Vec, biome: u16, + surface: Option, } impl FlatTerrain { /// The overworld generator of `world_gen_settings` (`data.dimensions`), if it @@ -34,9 +35,40 @@ impl FlatTerrain { layers.extend(std::iter::repeat_n(state, height as usize)); } } + let biome = biomes.id(&biome); + let mut water = None; + let mut surface = None; + for (i, &id) in layers + .iter() + .take(crate::chunk::HEIGHT as usize) + .enumerate() + .rev() + { + let state = horizon_world::registry::BlockStateId(id); + let name = registry.state(state)?.name.as_str(); + if matches!( + name, + "minecraft:air" | "minecraft:cave_air" | "minecraft:void_air" + ) { + continue; + } + let height = crate::chunk::MIN_Y + i as i32 + 1; + if matches!(name, "minecraft:water" | "minecraft:lava") { + water.get_or_insert((height, state)); + } else { + surface = Some(crate::lod::Surface { + height, + state, + water, + biome, + }); + break; + } + } Some(Self { layers, - biome: biomes.id(&biome), + biome, + surface, }) } /// FlatLevelSource.getSpawnHeight: the top of the layers. @@ -154,6 +186,9 @@ pub fn settings_from_json( impl ChunkGenerator for FlatTerrain { type Worker = (); fn worker(&self) {} + fn lod_surface(&self, _: &mut (), _: i32, _: i32) -> Option { + self.surface + } fn proto(&self, _: &mut (), cx: i32, cz: i32) -> crate::streaming::Proto { let mut chunk = Chunk::empty(cx, cz); for s in &mut chunk.sections { diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index f13be4c84..1fb3a7889 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -780,6 +780,9 @@ struct Game<'a> { screen_fx: horizon_client::screen_effects::ScreenEffects, world: World, streaming: Streaming, + lod: Option, + lod_source_attempted: bool, + lod_observe: horizon_client::fast_hash::FxHashSet<(i32, i32)>, options: Options, player: Option, spawn: Option, @@ -6426,6 +6429,7 @@ impl Game<'_> { self.using = None; self.screen = Screen::Playing; self.title = None; + self.clear_lod(); if let Some(r) = self.renderer.as_mut() { r.clear_sections(); } @@ -6575,6 +6579,7 @@ impl Game<'_> { self.pickups.clear(); self.orbs.clear(); self.mining = None; + self.clear_lod(); if let Some(r) = self.renderer.as_mut() { r.clear_sections(); } @@ -6753,6 +6758,7 @@ impl Game<'_> { self.sleep_timer = 0; self.clear_entities(); self.screen = Screen::Playing; + self.clear_lod(); if let Some(r) = self.renderer.as_mut() { r.clear_sections(); } @@ -8626,9 +8632,10 @@ impl Game<'_> { } else { self.bob_matrix(alpha as f32) }; - let distance = (self.streaming.radius * 16) as f32; + let terrain_distance = self.terrain_distance(); + let distance = (terrain_distance * 16) as f32; let depth_far = - horizon_client::camera::depth_far(self.streaming.radius, self.settings.cloud_range); + horizon_client::camera::depth_far(terrain_distance, self.settings.cloud_range); self.view_frustum = self .renderer .as_ref() @@ -8705,6 +8712,7 @@ impl Game<'_> { bob, sky, distance, + detail_distance: (self.streaming.radius * 16) as f32, depth_far, underwater, title: title && !live, @@ -8889,7 +8897,7 @@ impl Game<'_> { ); // Camera.depthFar, as the renderer's far plane. let depth_far = - horizon_client::camera::depth_far(self.streaming.radius, self.settings.cloud_range); + horizon_client::camera::depth_far(self.terrain_distance(), self.settings.cloud_range); if !self .border_renderer .prepare(&self.border_state, camera, distance, depth_far) @@ -9096,10 +9104,63 @@ impl Game<'_> { } Ok(()) } + /// Only surface LOD worlds extend the renderer's range, never simulation. + fn terrain_distance(&self) -> i32 { + if self.lod.is_some() && self.dim != Dim::Nether { + self.streaming + .radius + .max(self.settings.horizon.lod_distance.min(1024) as i32) + } else { + self.streaming.radius + } + } + fn clear_lod(&mut self) { + self.lod = None; + self.lod_source_attempted = false; + self.lod_observe.clear(); + if let Some(renderer) = &mut self.renderer { + renderer.clear_lods(); + } + } + 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() + && (self.remote.is_none() || self.integrated.is_some()); + if !enabled { + if self.lod.is_some() || self.lod_source_attempted { + self.clear_lod(); + } + return; + } + if self.lod_source_attempted { + return; + } + self.lod_source_attempted = true; + let source = match &self.integrated { + Some(server) => server.call(|game| game.streaming.lod_source()), + None => Some(self.streaming.lod_source()), + }; + if let Some(source) = source { + self.lod = Some(horizon_client::lod::Lod::new( + source, + self.pack.context.clone(), + )); + self.lod_observe.extend(self.world.chunks.keys().copied()); + } + } fn update_world(&mut self) { if let Err(e) = self.poll_remote() { log::warn!("Horizon: {e}"); } + self.prepare_lod(); + if self.lod.is_some() { + for &((x, _, z), _, _) in &self.world.light_edits { + self.lod_observe.insert((x >> 4, z >> 4)); + } + } if let Some(p) = &self.player { self.streaming.center = ( (p.position.x.floor() as i32) >> 4, @@ -9108,6 +9169,21 @@ impl Game<'_> { } let (uploads, unloaded) = horizon_client::spans::time("streaming", || self.streaming.update(&mut self.world)); + if let Some(lod) = &mut self.lod { + self.lod_observe + .extend(uploads.iter().map(|(key, _)| (key.0, key.2))); + for chunk in &unloaded { + 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); + } + } + } let dirty: Vec<_> = unloaded .iter() .filter(|c| self.world.modified.remove(&(c.x, c.z))) @@ -9183,6 +9259,30 @@ impl Game<'_> { renderer.remove((c.x, sy, c.z)); } } + if let (Some(lod), Some(player)) = (&mut self.lod, &self.player) { + let covered = renderer + .lod_detail_columns() + .filter(|&(x, z)| { + horizon_client::streaming::in_view_distance( + self.streaming.center, + self.streaming.radius, + x, + z, + ) + }) + .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 in lod.removed() { + renderer.remove_lod(key); + } + } } } } @@ -10040,6 +10140,9 @@ impl<'a> Game<'a> { screen_fx: Default::default(), world, streaming, + lod: None, + lod_source_attempted: false, + lod_observe: Default::default(), options, player: None, spawn: None, diff --git a/crates/horizon-client/src/game/control.rs b/crates/horizon-client/src/game/control.rs index cf7510065..9b5fc3bc6 100644 --- a/crates/horizon-client/src/game/control.rs +++ b/crates/horizon-client/src/game/control.rs @@ -20,6 +20,7 @@ const HELP: &[&str] = &[ "hotbar 1-9 select a hotbar slot", "give ITEM [COUNT] add items to the inventory", "render-distance N chunks, 2..64", + "lod-distance N far surface chunks, 0 disables, up to1024", "time TICKS set the time of day", "summon KIND [X Y Z] [baby] spawn a mob (in front of the player without a position)", "mobs [RADIUS] list mobs near the player with their AI state", @@ -368,6 +369,15 @@ impl Game<'_> { self.apply_settings(); ok() } + "lod-distance" => { + let n: u32 = arg(1)?.parse()?; + if n > 1024 { + return Err("LOD distance must be 0..1024".into()); + } + self.settings.horizon.lod_distance = n; + self.apply_settings(); + ok() + } "time" => { self.world.day_time = arg(1)?.parse()?; ok() @@ -699,6 +709,15 @@ impl Game<'_> { .map(|k| format!("{k:?}")) .collect(); physical_keys.sort(); + let lod = json!({ + "distance": self.settings.horizon.lod_distance, + "terrain_distance": self.terrain_distance(), + "source": if self.lod.is_none() { "none" } else if self.integrated.is_some() { "integrated" } else { "local" }, + "source_attempted": self.lod_source_attempted, + "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(), + }); let mut status = json!({ "screen": self.screen_name(), "debug": { @@ -757,6 +776,7 @@ impl Game<'_> { .eye_position() .map(|e| self.shown_name_tags(glam::DVec3::from_array(e))), }); + status["lod"] = lod; status["physical_keys"] = json!(physical_keys); status } diff --git a/crates/horizon-client/src/game/packs.rs b/crates/horizon-client/src/game/packs.rs index 0b95dd2ad..dad536282 100644 --- a/crates/horizon-client/src/game/packs.rs +++ b/crates/horizon-client/src/game/packs.rs @@ -170,6 +170,7 @@ impl Game<'_> { horizon_client::ui::load_metadata(root); lang::set(lang::Language::load(&[&root.to_path_buf()], &lang::code())); self.audio.reload(root); + self.clear_lod(); self.streaming.reload_resources(&mut self.world, context); self.pending_uploads.clear(); // New GPU textures: a renderer built from the reloaded resources; the diff --git a/crates/horizon-client/src/game/remote.rs b/crates/horizon-client/src/game/remote.rs index c2fab26db..7e8114444 100644 --- a/crates/horizon-client/src/game/remote.rs +++ b/crates/horizon-client/src/game/remote.rs @@ -258,6 +258,7 @@ impl Game<'_> { self.screen = Screen::Playing; self.title = None; self.dim = Dim::Overworld; + self.clear_lod(); if let Some(r) = self.renderer.as_mut() { r.clear_sections(); } @@ -362,6 +363,7 @@ impl Game<'_> { self.border = Default::default(); self.screen = Screen::Playing; self.container = None; + self.clear_lod(); if let Some(r) = self.renderer.as_mut() { r.clear_sections(); } @@ -595,6 +597,7 @@ impl Game<'_> { // A new ClientLevel: a new chunk storage, centred on 0, 0. r.chunk_center = (0, 0); } + self.clear_lod(); 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 4ee10e6d2..fad4f7352 100644 --- a/crates/horizon-client/src/game/render_tick.rs +++ b/crates/horizon-client/src/game/render_tick.rs @@ -85,7 +85,7 @@ impl Game<'_> { (water & 255) as f32 / 255.0, ], biome, - render_distance_chunks: self.streaming.radius, + render_distance_chunks: self.terrain_distance(), 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/gfx.rs b/crates/horizon-client/src/gfx.rs index 4280efd4a..ff874136f 100644 --- a/crates/horizon-client/src/gfx.rs +++ b/crates/horizon-client/src/gfx.rs @@ -59,6 +59,7 @@ pub fn outline_vertices(edges: &[[f64; 6]], camera: glam::DVec3) -> Vec<[f32; 6] mod arena; mod java_stages; +mod lod; mod occlusion; mod pack_stages; pub mod panorama; @@ -115,6 +116,8 @@ pub struct Frame<'a> { pub sky: Sky, /// Render distance in blocks (fog end). pub distance: f32, + /// Full-detail terrain distance in blocks; distant LODs bypass the cave graph. + pub detail_distance: f32, /// `Camera.depthFar`: the far plane (`crate::camera::depth_far`). pub depth_far: f32, pub underwater: bool, @@ -322,6 +325,8 @@ pub struct Renderer { outline: wgpu::Buffer, depth: wgpu::TextureView, sections: resident::Resident, + lods: HashMap, + detail_columns: HashMap<(i32, i32), u32>, arena: arena::Arena, sorted_indices: arena::Arena, translucent_sorter: translucency::Sorter, @@ -333,6 +338,8 @@ pub struct Renderer { pub multi_draw: bool, /// Per-frame scratch, kept to avoid allocating every frame. visible: Vec, + visible_lods: Vec, + translucent_draws: Vec<(u64, usize, [u32; 5], (u32, Option))>, origins: Vec<[f32; 4]>, /// `DrawIndexedIndirectArgs` words: index count, instance count, first /// index, base vertex, first instance. @@ -530,6 +537,7 @@ impl Renderer { let vertices: u64 = self .sections .values() + .chain(self.lods.values().map(|tile| &tile.mesh)) .map(|s| u64::from(s.solid + s.translucent) * 4) .sum(); [self.arena.bytes(), vertices * stride] @@ -1746,6 +1754,8 @@ impl Renderer { outline, depth, sections: resident::Resident::default(), + lods: HashMap::new(), + detail_columns: HashMap::new(), arena, sorted_indices, translucent_sorter: translucency::Sorter::default(), @@ -1754,6 +1764,8 @@ impl Renderer { indirect_capacity: 1024, multi_draw, visible: Vec::new(), + visible_lods: Vec::new(), + translucent_draws: Vec::new(), origins: Vec::new(), draws: Vec::new(), draw_pages: Vec::new(), @@ -1921,11 +1933,22 @@ impl Renderer { } else { self.block_entities.insert(key, mesh.block_entities.clone()); } + self.release_section(key); + let section = self.upload_mesh(mesh); + if section.is_some() || (mesh.solid.is_empty() && mesh.translucent.is_empty()) { + *self.detail_columns.entry((key.0, key.2)).or_default() |= 1 << key.1; + } else if let Some(mask) = self.detail_columns.get_mut(&(key.0, key.2)) { + *mask &= !(1 << key.1); + } + if let Some(section) = section { + self.sections.insert(key, section); + } + } + fn upload_mesh(&mut self, mesh: &SectionMesh) -> Option { let solid = (mesh.solid.len() / 4) as u32; let translucent = (mesh.translucent.len() / 4) as u32; - self.release_section(key); if solid + translucent == 0 { - return; + return None; } let max = solid.max(translucent); if max > self.index_quads { @@ -1934,16 +1957,14 @@ impl Renderer { } let vertices = (mesh.solid.len() + mesh.translucent.len()) as u64; // With video and system memory both full, the section is not drawn. - let Some((page, start)) = self.arena.alloc(&self.device, &self.queue, vertices) else { - return; - }; + let (page, start) = self.arena.alloc(&self.device, &self.queue, vertices)?; let sorted_indices = if translucent > 0 { let indices = self.sorted_indices .alloc(&self.device, &self.queue, u64::from(translucent) * 6); if indices.is_none() { self.arena.release(page, start, vertices); - return; + return None; } indices } else { @@ -1961,25 +1982,22 @@ impl Renderer { ); } } - self.sections.insert( - key, - SectionGpu { - page, - start, - solid, - translucent, - faces: mesh.faces, - sort: translucency::SortState::new( - mesh.translucent - .as_chunks::<4>() - .0 - .iter() - .map(|q| translucency::quad_center(q[0].pos, q[2].pos)) - .collect(), - ), - sorted_indices, - }, - ); + Some(SectionGpu { + page, + start, + solid, + translucent, + faces: mesh.faces, + sort: translucency::SortState::new( + mesh.translucent + .as_chunks::<4>() + .0 + .iter() + .map(|q| translucency::quad_center(q[0].pos, q[2].pos)) + .collect(), + ), + sorted_indices, + }) } /// Draws `count` section draws from `first` with the arena and section /// origins bound: one indirect multi-draw, or a loop without it. @@ -2008,14 +2026,17 @@ impl Renderer { } fn release_section(&mut self, key: SectionKey) { if let Some(s) = self.sections.remove(key) { - if let Some((page, start)) = s.sorted_indices { - self.sorted_indices - .release(page, start, u64::from(s.translucent) * 6); - } - self.arena - .release(s.page, s.start, u64::from(s.solid + s.translucent) * 4); + self.release_mesh(s); } } + fn release_mesh(&mut self, s: SectionGpu) { + if let Some((page, start)) = s.sorted_indices { + self.sorted_indices + .release(page, start, u64::from(s.translucent) * 6); + } + self.arena + .release(s.page, s.start, u64::from(s.solid + s.translucent) * 4); + } /// Block-entity blocks of every meshed section (see `SectionMesh::block_entities`). pub fn block_entities(&self) -> impl Iterator { self.block_entities.values().flatten() @@ -2023,12 +2044,26 @@ impl Renderer { pub fn clear_sections(&mut self) { self.block_entities.clear(); self.sections.clear(); + self.lods.clear(); + self.detail_columns.clear(); self.arena.clear(); self.sorted_indices.clear(); self.translucency_cursor = 0; self.occlusion.clear(); } + pub fn lod_detail_columns(&self) -> impl Iterator + '_ { + let complete = (1u32 << (crate::chunk::HEIGHT / 16)) - 1; + self.detail_columns + .iter() + .filter_map(move |(&column, &mask)| (mask == complete).then_some(column)) + } pub fn remove(&mut self, key: SectionKey) { + if let Some(mask) = self.detail_columns.get_mut(&(key.0, key.2)) { + *mask &= !(1 << key.1); + if *mask == 0 { + self.detail_columns.remove(&(key.0, key.2)); + } + } self.block_entities.remove(&key); self.occlusion.remove(key); self.release_section(key); @@ -2395,7 +2430,7 @@ impl Renderer { ); let culling = !frame.title && frame.cave_culling; if culling { - let radius = (frame.distance / 16.0) as i32 + 2; + let radius = (frame.detail_distance / 16.0) as i32 + 2; self.occlusion.update(camera_section, radius); } let reachable = &self.occlusion; @@ -2403,14 +2438,26 @@ impl Renderer { visible.clear(); self.sections.visible( camera, - (frame.distance / 16.0) as i32, + (frame.detail_distance / 16.0) as i32, &view, |k| !culling || reachable.contains(k), &mut visible, ); - self.drawn_sections = visible.len(); - if visible.len() as u64 > self.instance_capacity { - self.instance_capacity = (visible.len() as u64).next_power_of_two(); + let mut visible_lods = std::mem::take(&mut self.visible_lods); + visible_lods.clear(); + if frame.title { + visible.clear(); + } else { + visible_lods.extend( + self.lods + .iter() + .filter_map(|(&key, tile)| tile.visible(key, camera, frame.distance, &view)), + ); + visible_lods.sort_unstable_by_key(|v| (v.distance, v.key)); + } + self.drawn_sections = visible.len() + visible_lods.len(); + if self.drawn_sections as u64 > self.instance_capacity { + self.instance_capacity = (self.drawn_sections as u64).next_power_of_two(); self.instances = self.device.create_buffer(&wgpu::BufferDescriptor { label: Some("Section origins"), size: self.instance_capacity * 16, @@ -2422,7 +2469,12 @@ impl Renderer { self.origins.extend( visible .iter() - .map(|v| [v.origin[0], v.origin[1], v.origin[2], 0.0]), + .map(|v| [v.origin[0], v.origin[1], v.origin[2], 0.0]) + .chain( + visible_lods + .iter() + .map(|v| [v.origin[0], v.origin[1], v.origin[2], 0.0]), + ), ); if !self.origins.is_empty() { self.uploads.buffer( @@ -2432,9 +2484,6 @@ impl Renderer { bytemuck::cast_slice(&self.origins), ); } - if frame.title { - visible.clear(); - } // Solid sections front to back, then translucent ones back to front, // as one list of indirect draws over the section arena. self.draws.clear(); @@ -2452,10 +2501,20 @@ impl Renderer { self.draw_pages.push((s.page, None)); }); } + for (i, v) in visible_lods.iter().enumerate() { + let s = &self.lods[&v.key].mesh; + s.faces.ranges(eye - Vec3::from(v.origin), |first, quads| { + let first = s.start + u64::from(first) * 4; + self.draws + .push([quads * 6, 1, 0, first as u32, (visible.len() + i) as u32]); + self.draw_pages.push((s.page, None)); + }); + } let solid_draws = self.draws.len() as u32; // LevelRenderer: nearby sections each frame, plus a rotating eighth // (at least 15) of the visible list. Cached views avoid needless sorts. - let count = visible.len(); + self.translucent_draws.clear(); + let count = visible.len() + visible_lods.len(); let budget = (count / 8).max(15).min(count); self.translucency_cursor %= count.max(1); for (i, v) in visible.iter().enumerate().rev() { @@ -2483,16 +2542,62 @@ impl Renderer { ); } let first = s.start + u64::from(s.solid) * 4; - self.draws.push([ - s.translucent * 6, - 1, - index_start as u32, - first as u32, - i as u32, - ]); - self.draw_pages.push((s.page, Some(index_page))); + self.translucent_draws.push(( + v.distance, + i, + [ + s.translucent * 6, + 1, + index_start as u32, + first as u32, + i as u32, + ], + (s.page, Some(index_page)), + )); } } + for (i, v) in visible_lods.iter().enumerate() { + let instance = visible.len() + i; + let tile = self.lods.get_mut(&v.key).expect("visible resident tile"); + let s = &mut tile.mesh; + if let Some((index_page, index_start)) = s.sorted_indices { + let scheduled = (instance + count - self.translucency_cursor) % count < budget; + if (!s.sort.is_sorted() || scheduled) + && let Some(indices) = s.sort.update_lod( + camera.to_array(), + v.key.origin(), + &mut self.translucent_sorter, + ) + { + self.uploads.buffer( + &mut encoder, + self.sorted_indices.buffer(index_page), + index_start * 4, + bytemuck::cast_slice(indices), + ); + } + let first = s.start + u64::from(s.solid) * 4; + self.translucent_draws.push(( + v.distance, + instance, + [ + s.translucent * 6, + 1, + index_start as u32, + first as u32, + instance as u32, + ], + (s.page, Some(index_page)), + )); + } + } + // Near water and distant water share one back-to-front draw order. + self.translucent_draws + .sort_unstable_by(|a, b| b.0.cmp(&a.0).then(a.1.cmp(&b.1))); + for &(_, _, draw, pages) in &self.translucent_draws { + self.draws.push(draw); + self.draw_pages.push(pages); + } self.translucency_cursor += budget; let translucent_draws = self.draws.len() as u32 - solid_draws; if self.multi_draw && !self.draws.is_empty() { @@ -3052,6 +3157,7 @@ impl Renderer { self.last_encode = started.elapsed().saturating_sub(self.last_wait); let _ = self.started; self.visible = visible; + self.visible_lods = visible_lods; Ok(true) } } diff --git a/crates/horizon-client/src/gfx/lod.rs b/crates/horizon-client/src/gfx/lod.rs new file mode 100644 index 000000000..43c01f6ae --- /dev/null +++ b/crates/horizon-client/src/gfx/lod.rs @@ -0,0 +1,142 @@ +//! Far meshes share the terrain arena and passes, with their actual bounds +//! rather than the 16³ boxes used by the near-section residency walk. +use super::{Frustum, Renderer, SectionGpu}; +use crate::{lod::TileKey, mesher::SectionMesh}; +use glam::{DVec3, Vec3}; + +pub(super) struct Tile { + pub mesh: SectionGpu, + bounds: Bounds, +} + +#[derive(Clone, Copy)] +struct Bounds { + min: Vec3, + max: Vec3, +} + +impl Bounds { + fn mesh(mesh: &SectionMesh) -> Option { + let mut positions = mesh + .solid + .iter() + .chain(&mesh.translucent) + .map(|v| Vec3::from(v.pos)); + let first = positions.next()?; + let mut bounds = Self { + min: first, + max: first, + }; + for pos in positions { + bounds.min = bounds.min.min(pos); + bounds.max = bounds.max.max(pos); + } + Some(bounds) + } + + fn visible(self, origin: DVec3, distance: f32, frustum: &Frustum) -> Option { + let min = origin + self.min.as_dvec3(); + let max = origin + self.max.as_dvec3(); + let nearest = DVec3::ZERO.clamp(min, max); + if nearest.x * nearest.x + nearest.z * nearest.z > f64::from(distance).powi(2) + || !frustum.intersects(min.as_vec3(), max.as_vec3()) + { + return None; + } + Some(((min + max) * 0.5).length_squared().to_bits()) + } +} + +pub(super) struct Visible { + pub key: TileKey, + pub distance: u64, + pub origin: [f32; 3], +} + +impl Tile { + pub fn visible( + &self, + key: TileKey, + camera: DVec3, + distance: f32, + frustum: &Frustum, + ) -> Option { + let origin = DVec3::from(key.origin().map(f64::from)) - camera; + Some(Visible { + key, + distance: self.bounds.visible(origin, distance, frustum)?, + origin: origin.as_vec3().to_array(), + }) + } +} + +impl Renderer { + pub fn resident_lods(&self) -> usize { + self.lods.len() + } + + pub fn upload_lod(&mut self, key: TileKey, mesh: &SectionMesh) { + self.remove_lod(key); + if let Some(bounds) = Bounds::mesh(mesh) + && let Some(mesh) = self.upload_mesh(mesh) + { + self.lods.insert(key, Tile { mesh, bounds }); + } + } + + pub fn remove_lod(&mut self, key: TileKey) { + if let Some(tile) = self.lods.remove(&key) { + self.release_mesh(tile.mesh); + } + } + + pub fn clear_lods(&mut self) { + for (_, tile) in std::mem::take(&mut self.lods) { + self.release_mesh(tile.mesh); + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::mesher::TerrainVertex; + + #[test] + fn large_tile_bounds_include_water_and_do_not_cull_by_origin() { + let vertex = |pos| TerrainVertex { + pos, + uv: [0.0; 2], + layer: 0, + light: 0, + color: 0, + }; + let mesh = SectionMesh { + solid: vec![vertex([0.0, -64.0, 0.0]), vertex([1024.0, 100.0, 1024.0])], + translucent: vec![vertex([512.0, 120.0, 512.0])], + ..Default::default() + }; + let bounds = Bounds::mesh(&mesh).unwrap(); + assert_eq!(bounds.min, Vec3::new(0.0, -64.0, 0.0)); + assert_eq!(bounds.max, Vec3::new(1024.0, 120.0, 1024.0)); + let view = Frustum { + planes: super::super::frustum( + super::super::world_projection(70.0, 16.0 / 9.0, 8192.0) + * crate::camera::view_matrix(0.0, 0.0), + ), + }; + // The tile origin is farther than the range, but its far edge reaches + // the camera. Negative world origins therefore remain visible. + assert!( + bounds + .visible(DVec3::new(-1000.0, -80.0, -512.0), 256.0, &view) + .is_some() + ); + assert!( + bounds + .visible(DVec3::new(-2000.0, -80.0, -512.0), 256.0, &view) + .is_none() + ); + assert!(Bounds::mesh(&SectionMesh::default()).is_none()); + } +} diff --git a/crates/horizon-client/src/gfx/translucency.rs b/crates/horizon-client/src/gfx/translucency.rs index 927152a39..4c1ad6683 100644 --- a/crates/horizon-client/src/gfx/translucency.rs +++ b/crates/horizon-client/src/gfx/translucency.rs @@ -47,6 +47,25 @@ impl SortState { self.last.is_some() } + /// Large tiles cannot use the section viewpoint shortcut: the camera may + /// cross a quad's plane while remaining on the same side of the first 16³. + pub fn update_lod<'a>( + &mut self, + camera: [f64; 3], + origin: [i32; 3], + sorter: &'a mut Sorter, + ) -> Option<&'a [u32]> { + let block = camera.map(|v| v.floor() as i32); + if self.last.is_some_and(|(last, _)| last == block) { + return None; + } + self.last = Some((block, [0; 3])); + Some(sorter.sort( + &self.centers, + std::array::from_fn(|i| (camera[i] - f64::from(origin[i])) as f32), + )) + } + /// TranslucencyPointOfView and ResortTransparencyTask: diagonal views keep /// their ordering until the point of view changes; aligned views also /// resort on crossing a block boundary. Sub-block movement needs no upload. @@ -116,4 +135,28 @@ mod tests { .is_some() ); } + #[test] + fn large_tiles_resort_inside_the_same_section_viewpoint() { + let mut state = SortState::new(vec![[32.0, 0.0, 0.0], [96.0, 0.0, 0.0]]); + let mut sorter = Sorter::default(); + assert_eq!( + state + .update_lod([48.0, 20.0, 20.0], [0; 3], &mut sorter) + .unwrap()[0], + 4 + ); + assert!( + state + .update_lod([48.5, 20.0, 20.0], [0; 3], &mut sorter) + .is_none() + ); + // Both views have positive x/y/z signs relative to the first section, + // so the section-sized shortcut would incorrectly keep the old order. + assert_eq!( + state + .update_lod([80.0, 20.0, 20.0], [0; 3], &mut sorter) + .unwrap()[0], + 0 + ); + } } diff --git a/crates/horizon-client/src/graphics.rs b/crates/horizon-client/src/graphics.rs index f10c6b3b3..0009fd0fa 100644 --- a/crates/horizon-client/src/graphics.rs +++ b/crates/horizon-client/src/graphics.rs @@ -115,6 +115,8 @@ pub enum Overlay { #[derive(Clone, Debug, PartialEq)] pub struct Graphics { pub preset: Preset, + /// Distant surface terrain range, in chunks; 0 disables LODs. + pub lod_distance: u32, /// Skip sections hidden behind closed ones (vanilla's occlusion graph). pub cave_culling: bool, /// Hide shared leaf and mangrove-root faces, reducing foliage density. @@ -152,6 +154,7 @@ impl Default for Graphics { fn default() -> Self { Self { preset: Preset::Custom, + lod_distance: 0, cave_culling: true, cull_leaves: false, entity_culling: false, @@ -197,6 +200,7 @@ impl Graphics { let number = |d: u32| value.parse().unwrap_or(d); match key.trim() { "preset" => g.preset = Preset::parse(value), + "lodDistance" => g.lod_distance = number(g.lod_distance).min(1024), "caveCulling" => g.cave_culling = flag(g.cave_culling), "cullLeaves" => g.cull_leaves = flag(g.cull_leaves), "entityCulling" => g.entity_culling = flag(g.entity_culling), @@ -234,8 +238,9 @@ impl Graphics { } pub fn serialize(&self) -> String { format!( - "preset:{}\ncaveCulling:{}\ncullLeaves:{}\nentityCulling:{}\nchunkUpdates:{}\nuploadBudgetKiB:{}\nsky:{}\nfog:{}\nweather:{}\nanimatedTextures:{}\npresentMode:{}\nframePacing:{}\nworkers:{}\noverlay:{}\nresolutionScale:{}\nfxaa:{}\nlevel:{}\nbatteryMode:{}\nshaderPack:{}\n", + "preset:{}\nlodDistance:{}\ncaveCulling:{}\ncullLeaves:{}\nentityCulling:{}\nchunkUpdates:{}\nuploadBudgetKiB:{}\nsky:{}\nfog:{}\nweather:{}\nanimatedTextures:{}\npresentMode:{}\nframePacing:{}\nworkers:{}\noverlay:{}\nresolutionScale:{}\nfxaa:{}\nlevel:{}\nbatteryMode:{}\nshaderPack:{}\n", self.preset.name(), + self.lod_distance, bool_text(self.cave_culling), bool_text(self.cull_leaves), bool_text(self.entity_culling), @@ -330,6 +335,7 @@ mod tests { fn round_trips() { let g = Graphics { preset: Preset::Balanced, + lod_distance: 256, cave_culling: false, cull_leaves: true, entity_culling: true, @@ -357,6 +363,19 @@ mod tests { ); } + #[test] + fn lod_distance_is_opt_in_and_bounded() { + assert_eq!(Graphics::default().lod_distance, 0); + for distance in [0, 64, 128, 256, 512, 1024] { + let g = Graphics::parse(&format!("lodDistance:{distance}\n")); + assert_eq!(g.lod_distance, distance); + assert_eq!(Graphics::parse(&g.serialize()), g); + } + assert_eq!(Graphics::parse("lodDistance:10000\n").lod_distance, 1024); + assert_eq!(Graphics::parse("lodDistance:-1\n").lod_distance, 0); + assert_eq!(Graphics::parse("lodDistance:invalid\n").lod_distance, 0); + } + #[test] fn battery_mode_only_lowers_the_frame_cap() { let mut g = Graphics::default(); diff --git a/crates/horizon-client/src/graphics_ui.rs b/crates/horizon-client/src/graphics_ui.rs index 745cd5d80..236dc8333 100644 --- a/crates/horizon-client/src/graphics_ui.rs +++ b/crates/horizon-client/src/graphics_ui.rs @@ -21,6 +21,7 @@ pub enum Level { #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum H { Preset, + LodDistance, CaveCulling, CullLeaves, EntityCulling, @@ -76,7 +77,12 @@ pub fn rows(level: Level) -> Vec { let render = v( Opt::RenderDistance, High, - "How far terrain is drawn, in chunks. The biggest lever on frame time.", + "Full-detail terrain range. LOD Distance can extend local worlds farther.", + ); + let lod = h( + H::LodDistance, + Medium, + "Coarse distant surface in local worlds; no extra simulation.", ); let fov = v( Opt::Fov, @@ -100,10 +106,11 @@ pub fn rows(level: Level) -> Vec { "Sets the options below together. Changing one makes it Custom.", ); match level { - Level::Simple => vec![preset, render, fov, fps, vsync, gui], + Level::Simple => vec![preset, render, lod, fov, fps, vsync, gui], Level::Advanced => vec![ Row::Header("Rendering"), render, + lod, v( Opt::SimulationDistance, Medium, @@ -311,6 +318,10 @@ impl H { let g = &s.horizon; match self { H::Preset => labelled("Preset", g.preset.label()), + H::LodDistance => match g.lod_distance { + 0 => labelled("LOD Distance", "Off"), + n => labelled("LOD Distance", format!("{n} chunks")), + }, H::CaveCulling => labelled("Cave Culling", on_off(g.cave_culling)), H::CullLeaves => labelled("Cull Leaves", on_off(g.cull_leaves)), H::EntityCulling => labelled("Entity Culling", on_off(g.entity_culling)), @@ -383,6 +394,9 @@ impl H { } let g = &mut s.horizon; match self { + H::LodDistance => { + g.lod_distance = step(&[0, 64, 128, 256, 512, 1024], g.lod_distance, back) + } H::CaveCulling => g.cave_culling = !g.cave_culling, H::CullLeaves => g.cull_leaves = !g.cull_leaves, H::EntityCulling => g.entity_culling = !g.entity_culling, @@ -467,6 +481,10 @@ impl Item { } pub fn label(self, s: &Settings) -> Text { match self { + Item::Vanilla(Opt::RenderDistance) if s.horizon.lod_distance > 0 => labelled( + "Full Detail Distance", + format!("{} chunks", s.render_distance), + ), Item::Vanilla(o) => o.label(s), Item::Horizon(h) => h.label(s), } @@ -923,6 +941,40 @@ mod tests { } } + #[test] + fn lod_distance_cycles_in_both_levels_without_changing_near_distance() { + let mut s = Settings::default(); + let item = Item::Horizon(H::LodDistance); + let near = s.render_distance; + assert_eq!(item.label(&s).plain(), "LOD Distance: Off"); + for level in [Level::Simple, Level::Advanced] { + let items: Vec<_> = rows(level) + .into_iter() + .filter_map(|row| match row { + Row::Setting(setting) => Some(setting.item), + Row::Header(_) => None, + }) + .collect(); + let render = items + .iter() + .position(|i| *i == Item::Vanilla(Opt::RenderDistance)) + .unwrap(); + assert_eq!(items[render + 1], item); + } + for distance in [64, 128, 256, 512, 1024, 0] { + item.cycle(&mut s, false, [854, 480]); + assert_eq!(s.horizon.lod_distance, distance); + assert_eq!(s.render_distance, near); + } + item.cycle(&mut s, true, [854, 480]); + assert_eq!(s.horizon.lod_distance, 1024); + assert_eq!(item.label(&s).plain(), "LOD Distance: 1024 chunks"); + assert_eq!( + Item::Vanilla(Opt::RenderDistance).label(&s).plain(), + format!("Full Detail Distance: {near} chunks") + ); + } + #[test] fn the_shader_pack_row_is_off_by_default_and_outside_presets() { let mut s = Settings::default(); diff --git a/crates/horizon-client/src/lib.rs b/crates/horizon-client/src/lib.rs index 45e09a6a3..d1f5e64bd 100644 --- a/crates/horizon-client/src/lib.rs +++ b/crates/horizon-client/src/lib.rs @@ -100,6 +100,7 @@ pub mod landing; pub mod lang; pub mod level_loading; pub mod light; +pub mod lod; pub mod logging; pub mod menu; pub mod menu_blur; diff --git a/crates/horizon-client/src/lod.rs b/crates/horizon-client/src/lod.rs new file mode 100644 index 000000000..3d39c2639 --- /dev/null +++ b/crates/horizon-client/src/lod.rs @@ -0,0 +1,862 @@ +//! Bounded surface 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}, + fast_hash::{FxHashMap, FxHashSet}, + mesher::{FaceGroups, SectionMesh, TerrainVertex}, + streaming::{LodSource, WorkerContext}, +}; +use horizon_core::position::BlockPos; +use horizon_world::registry::BlockStateId; +use std::{ + collections::VecDeque, + sync::{ + Arc, Condvar, Mutex, + atomic::{AtomicBool, Ordering}, + mpsc, + }, + thread, +}; + +const CELLS: usize = 16; +const MAX_SAVED: usize = 8192; +/// Top solid face and optional fluid face, in absolute block coordinates. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct Surface { + pub height: i32, + pub state: BlockStateId, + pub water: Option<(i32, BlockStateId)>, + pub biome: u16, +} +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)] +pub struct TileKey { + pub x: i32, + pub z: i32, + pub level: u8, +} +impl TileKey { + pub fn size(self) -> i32 { + 64 << self.level + } + pub fn origin(self) -> [i32; 3] { + [self.x * self.size(), 0, self.z * self.size()] + } + fn children(self) -> [Self; 4] { + std::array::from_fn(|i| Self { + x: self.x * 2 + (i & 1) as i32, + z: self.z * 2 + (i >> 1) as i32, + level: self.level - 1, + }) + } + fn distance(self, camera: [i32; 2]) -> i32 { + let [x, _, z] = self.origin(); + (x - camera[0]) + .max(camera[0] - x - self.size()) + .max(z - camera[1]) + .max(camera[1] - z - self.size()) + .max(0) + } +} +#[derive(Default)] +struct Plan { + roots: Vec, + nodes: FxHashSet, + split: FxHashSet, +} +impl Plan { + fn new(camera: [i32; 2], detail: i32, distance: u32) -> Self { + let range = distance.min(1024) as i32 * 16; + let mut level = 0; + while (64 << level) < range { + level += 1; + } + let size = 64 << level; + let mut result = 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 + } + fn visit(&mut self, key: TileKey, camera: [i32; 2], detail: i32, range: i32) { + 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) { + self.split.insert(key); + for child in key.children() { + self.visit(child, camera, detail, range); + } + } + } + fn select( + &self, + key: TileKey, + ready: &FxHashMap>>, + out: &mut Vec, + ) -> bool { + if !self.nodes.contains(&key) { + return true; + } + if self.split.contains(&key) { + let start = out.len(); + if key + .children() + .into_iter() + .all(|child| self.select(child, ready, out)) + { + return true; + } + if ready.contains_key(&key) { + out.truncate(start); + 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); + true + } else { + false + } + } +} +struct Job { + key: TileKey, +} +struct Done { + key: TileKey, + samples: Vec>, +} +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, + context: Arc, + area: Option<([i32; 2], i32, u32)>, + plan: Plan, + pending: FxHashSet, + ready: FxHashMap>>, + active: FxHashSet, + dirty: FxHashSet, + removed: Vec, + covered: FxHashSet<(i32, i32)>, + covered_bounds: Option<[i32; 4]>, + saved: FxHashMap<(i32, i32), [Option; 16]>, + saved_order: VecDeque<(i32, i32)>, + styles: FxHashMap, +} +impl Lod { + pub fn new(source: Arc, context: Arc) -> Self { + let shared = Arc::new(Shared { + jobs: Mutex::new(VecDeque::new()), + wake: Condvar::new(), + stop: AtomicBool::new(false), + }); + let worker = shared.clone(); + let (tx, results) = mpsc::channel(); + let handle = thread::Builder::new() + .name("Far terrain".into()) + .spawn(move || { + let mut sampler = source.sampler(); + loop { + 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 worker.stop.load(Ordering::Relaxed) { + break; + } + jobs.pop_front().unwrap() + }; + 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; + } + for dx in 0..CELLS { + samples.push( + sampler.sample(x + dx as i32 * stride, z + dz as i32 * stride), + ); + } + } + if tx + .send(Done { + key: job.key, + samples, + }) + .is_err() + { + break; + } + } + }) + .expect("far terrain worker"); + Self { + shared, + thread: Some(handle), + results, + context, + area: None, + plan: Plan::default(), + pending: FxHashSet::default(), + ready: FxHashMap::default(), + active: FxHashSet::default(), + dirty: FxHashSet::default(), + removed: Vec::new(), + covered: FxHashSet::default(), + covered_bounds: None, + saved: FxHashMap::default(), + saved_order: VecDeque::new(), + styles: FxHashMap::default(), + } + } + pub fn stats(&self) -> (usize, usize) { + (self.active.len(), self.ready.len()) + } + pub fn removed(&mut self) -> Vec { + std::mem::take(&mut self.removed) + } + pub fn invalidate_chunk(&mut self, x: i32, z: i32) { + 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 + { + self.dirty.insert(key); + } + } + } + /// Sample only sixteen columns, from current block storage (heightmaps can + /// be stale after edits). Memory stays bounded while exploring indefinitely. + 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 key = (chunk.x, chunk.z); + if self.saved.get(&key) == Some(&values) { + return; + } + if !self.saved.contains_key(&key) { + while self.saved.len() >= MAX_SAVED { + if let Some(old) = self.saved_order.pop_front() { + self.saved.remove(&old); + } + } + self.saved_order.push_back(key); + } + self.saved.insert(key, values); + self.invalidate_chunk(chunk.x, chunk.z); + } + pub fn update( + &mut self, + camera: [f64; 3], + detail: i32, + distance: u32, + covered: FxHashSet<(i32, i32)>, + ) -> Vec<(TileKey, SectionMesh)> { + let camera = [camera[0].floor() as i32, camera[2].floor() 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, + ); + if self.area != Some(area) { + self.area = Some(area); + self.plan = Plan::new(camera, detail, distance); + self.ready.retain(|key, _| self.plan.nodes.contains(key)); + } + while let Ok(done) = self.results.try_recv() { + self.pending.remove(&done.key); + if self.plan.nodes.contains(&done.key) { + if done.samples.len() == CELLS * CELLS { + self.ready.insert(done.key, done.samples); + } + } + } + if self.covered != covered { + let changes: Vec<_> = self + .covered + .symmetric_difference(&covered) + .copied() + .collect(); + for (x, z) in changes { + self.invalidate_chunk(x, z); + } + self.covered_bounds = covered.iter().fold(None, |bounds, &(x, z)| { + Some(match bounds { + None => [x * 16, z * 16, (x + 1) * 16, (z + 1) * 16], + Some([x0, z0, x1, z1]) => [ + x0.min(x * 16), + z0.min(z * 16), + x1.max((x + 1) * 16), + z1.max((z + 1) * 16), + ], + }) + }); + 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 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: 240 + | (self + .context + .tables + .facts(state) + .map_or(0, |f| u16::from(f.emission)) + * 16 + << 8), + } + } 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: 240 + | (self + .context + .tables + .facts(state) + .map_or(0, |f| u16::from(f.emission)) + * 16 + << 8), + } + }; + 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]; + } + } + } + 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 + }); + 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, + ); + } + } + } + } + } + mesh.faces = FaceGroups::group(&mut mesh.solid); + mesh + } + 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 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, + }), + ); +} +impl Drop for Lod { + fn drop(&mut self) { + self.shared.stop.store(true, Ordering::Relaxed); + self.shared.wake.notify_one(); + if let Some(thread) = self.thread.take() { + let _ = thread.join(); + } + } +} +#[cfg(test)] +mod tests { + use super::*; + #[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()); + for key in &plan.nodes { + if !plan.split.contains(key) && key.distance(camera) < 12 * 16 { + assert_eq!(key.level, 0); + } + } + let cells = plan + .nodes + .iter() + .filter(|k| !plan.split.contains(k)) + .count(); + assert!(cells < 2000); + } + } + assert_eq!( + TileKey { + x: -1, + z: -2, + level: 1 + } + .origin(), + [-128, 0, -256] + ); + } + #[test] + fn parent_covers_missing_children_then_retires_without_overlap() { + let parent = TileKey { + x: -1, + z: 0, + level: 1, + }; + let mut plan = Plan::default(); + plan.roots.push(parent); + plan.nodes.insert(parent); + plan.split.insert(parent); + plan.nodes.extend(parent.children()); + let mut ready = FxHashMap::default(); + ready.insert(parent, vec![]); + ready.insert(parent.children()[0], vec![]); + let mut selected = Vec::new(); + assert!(plan.select(parent, &ready, &mut selected)); + assert_eq!(selected, [parent]); + for child in parent.children() { + ready.insert(child, vec![]); + } + selected.clear(); + assert!(plan.select(parent, &ready, &mut selected)); + 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, + light: 240, + }; + quad( + &mut mesh, + style, + 0x123456, + [ + [0., 70., 64.], + [64., 70., 64.], + [64., 70., 0.], + [0., 70., 0.], + ], + 1., + ); + 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. + ); + } + #[test] + #[ignore = "requires prepared models/textures in HORIZON_TEST_RESOURCES"] + fn real_mesh_clips_near_chunks_and_preserves_water_and_edited_surfaces() -> crate::Result<()> { + use crate::{ + assets::Assets, biome::Biomes, light::LightTables, mesher::MeshTables, + testgen::TestTerrain, + }; + let root = std::path::PathBuf::from( + std::env::var_os("HORIZON_TEST_RESOURCES").expect("HORIZON_TEST_RESOURCES"), + ); + let assets = Arc::new(Assets::load(&root)?); + let facts = BlockFacts::load(&root, assets.registry)?; + let biomes = Arc::new(Biomes::load(&root)?); + let tables = Arc::new(MeshTables::new( + &assets, + &facts, + crate::models::load_visibility(&root, assets.registry)?, + )?); + let context = Arc::new(WorkerContext { + light: Arc::new(LightTables::load(&root, assets.registry, &facts)?), + tables, + assets: assets.clone(), + biomes: biomes.clone(), + }); + let source = Arc::new(TestTerrain::new(assets.registry, &biomes)?); + let mut lod = Lod::new(source, context); + let stone = assets.registry.default_state("minecraft:stone").unwrap(); + let water = assets.registry.default_state("minecraft:water").unwrap(); + let key = TileKey { + x: -1, + z: -1, + level: 3, + }; + lod.ready.insert( + key, + vec![ + Some(Surface { + height: 70, + state: stone, + water: Some((80, water)), + biome: biomes.id("minecraft:plains"), + }); + CELLS * CELLS + ], + ); + // A chunk covers only one quarter of this parent's 32² cell. Both + // opaque terrain and water must leave precisely that 16² footprint. + lod.covered.insert((-1, -2)); + lod.covered_bounds = Some([-16, -32, 0, -16]); + let mesh = lod.mesh(key); + let top_area = |vertices: &[TerrainVertex], y: f32| { + vertices + .as_chunks::<4>() + .0 + .iter() + .filter(|quad| quad.iter().all(|v| (v.pos[1] - y).abs() < 0.001)) + .map(|quad| { + let a = glam::Vec3::from(quad[0].pos); + let b = glam::Vec3::from(quad[1].pos); + let c = glam::Vec3::from(quad[2].pos); + let normal = (b - a).cross(c - a); + assert!(normal.y > 0.0, "surface must face the camera from above"); + let center = (a + c) * 0.5; + let [ox, _, oz] = key.origin(); + let chunk = ( + ((center.x as i32) + ox).div_euclid(16), + ((center.z as i32) + oz).div_euclid(16), + ); + assert!( + !lod.covered.contains(&chunk), + "LOD overlaps a full-detail chunk" + ); + normal.y + }) + .sum::() + }; + let expected = (key.size() * key.size() - 16 * 16) as f32; + assert_eq!(top_area(&mesh.solid, 70.0), expected); + assert_eq!(top_area(&mesh.translucent, 80.0 - 1.0 / 9.0), expected); + assert_eq!( + mesh.faces.quads.iter().sum::() as usize, + mesh.solid.len() / 4 + ); + assert!(mesh.translucent.iter().all(|v| v.light == 240)); + let side = |axis: usize, plane: f32, low: f32, high: f32| { + let other = if axis == 0 { 2 } else { 0 }; + mesh.solid + .as_chunks::<4>() + .0 + .iter() + .filter(|quad| { + quad.iter().all(|v| v.pos[axis] == plane) + && quad.iter().map(|v| v.pos[1]).fold(f32::INFINITY, f32::min) + == MIN_Y as f32 + && quad + .iter() + .map(|v| v.pos[1]) + .fold(f32::NEG_INFINITY, f32::max) + == 70.0 + && quad + .iter() + .map(|v| v.pos[other]) + .fold(f32::INFINITY, f32::min) + == low + && quad + .iter() + .map(|v| v.pos[other]) + .fold(f32::NEG_INFINITY, f32::max) + == high + }) + .map(move |quad| { + let a = glam::Vec3::from(quad[0].pos); + (glam::Vec3::from(quad[1].pos) - a).cross(glam::Vec3::from(quad[2].pos) - a) + [axis] + }) + .collect::>() + }; + // The two exposed edges face the missing full-detail chunk and seal + // the height mismatch all the way down. Shared subpieces add no walls. + let east_cut = side(0, 496.0, 480.0, 496.0); + assert_eq!(east_cut.len(), 1); + assert!(east_cut[0] > 0.0); + let north_cut = side(2, 496.0, 496.0, 512.0); + assert_eq!(north_cut.len(), 1); + assert!(north_cut[0] < 0.0); + assert!(side(0, 496.0, 496.0, 512.0).is_empty()); + + // Block edits do not refresh Chunk.heights; observation must scan the + // actual blocks, including builds above the old heightmap. + let mut chunk = Chunk::empty(-1, -1); + chunk.set(0, 120, 0, stone.0); + chunk.set(4, 54, 4, stone.0); + for y in 55..64 { + chunk.set(4, y, 4, water.0); + } + 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))); + Ok(()) + } +} diff --git a/crates/horizon-client/src/mesher/facing.rs b/crates/horizon-client/src/mesher/facing.rs index 2a7f0ba61..b27e59e29 100644 --- a/crates/horizon-client/src/mesher/facing.rs +++ b/crates/horizon-client/src/mesher/facing.rs @@ -42,6 +42,11 @@ impl Default for FaceGroups { } impl FaceGroups { + /// Group arbitrary-sized terrain quads, including distant LOD tiles. + pub fn group(solid: &mut Vec) -> Self { + group(solid) + } + /// Whether group `g` can hold a front-facing quad for an eye at `eye`, /// relative to the section's minimum corner. pub fn visible(&self, g: usize, eye: Vec3) -> bool { diff --git a/crates/horizon-client/src/save.rs b/crates/horizon-client/src/save.rs index 7ef38788b..6adb69de6 100644 --- a/crates/horizon-client/src/save.rs +++ b/crates/horizon-client/src/save.rs @@ -29,7 +29,7 @@ use horizon_world::{ chunk::BlockState, chunk_status::ChunkStatus, compression::Compression, - heightmap::{Heightmap, HeightmapChunk, HeightmapType}, + heightmap::{Heightmap, HeightmapChunk, HeightmapState, HeightmapType}, nbt::{self, Compound, JavaString, Limits, NamedTag, Tag}, region::{ChunkRecord, RegionFile, RegionWriter}, registry::{BlockRegistry, BlockStateId}, @@ -2783,11 +2783,188 @@ pub struct Persistent { pub registry: Arc, pub biomes: Arc, } +type LodSurfaces = [Option; 16]; +struct SavedLodSampler { + generator: Arc>, + inner: Box, + cache: std::collections::VecDeque<((i32, i32), Option)>, +} +impl crate::streaming::LodSampler for SavedLodSampler { + fn sample(&mut self, x: i32, z: i32) -> Option { + let key = (x >> 4, z >> 4); + let cached = match self.cache.iter().position(|(at, _)| *at == key) { + Some(index) => self.cache.remove(index).unwrap(), + None => { + let surfaces = self.generator.save.lod_surfaces( + key.0, + key.1, + &self.generator.registry, + &self.generator.biomes, + ); + (key, surfaces) + } + }; + let surfaces = cached.1; + self.cache.push_front(cached); + self.cache.truncate(128); + match surfaces { + Some(surfaces) => surfaces[((z & 15) / 4 * 4 + (x & 15) / 4) as usize], + None => self.inner.sample(x, z), + } + } +} + +/// Read one palette entry without expanding the other entries in the section. +fn lod_palette_entry( + container: &Tag, + index: usize, + entries: usize, + minimum_bits: u32, +) -> Option<&Tag> { + let Tag::List(palette) = container.get("palette")? else { + return None; + }; + if palette.is_empty() || palette.len() > entries || index >= entries { + return None; + } + if palette.len() == 1 { + return palette.first(); + } + let bits = (usize::BITS - (palette.len() - 1).leading_zeros()).max(minimum_bits); + let per_word = 64 / bits as usize; + let Tag::LongArray(data) = container.get("data")? else { + return None; + }; + if data.len() != entries.div_ceil(per_word) { + return None; + } + let value = ((data[index / per_word] as u64 >> ((index % per_word) * bits as usize)) + & ((1 << bits) - 1)) as usize; + palette.get(value) +} +fn decode_lod_surfaces( + root: &NamedTag, + assets: &crate::assets::Assets, + biomes: &Biomes, +) -> Option { + let Tag::List(sections) = root.tag.get("sections")? else { + return None; + }; + let mut sections: Vec<_> = sections + .iter() + .filter_map(|section| { + let y = number(section.get("Y"))? as i32; + (MIN_Y >> 4..(MIN_Y >> 4) + SECTIONS as i32) + .contains(&y) + .then_some((y, section)) + }) + .collect(); + sections.sort_unstable_by_key(|(y, _)| std::cmp::Reverse(*y)); + let mut surfaces = [None; 16]; + let mut water = [None; 16]; + let mut states = BTreeMap::new(); + for (section_y, section) in sections { + let Some(blocks) = section.get("block_states") else { + continue; + }; + for z in (0..16).step_by(4) { + for x in (0..16).step_by(4) { + let cell = z / 4 * 4 + x / 4; + if surfaces[cell].is_some() { + continue; + } + for y in (0..16).rev() { + let entry = lod_palette_entry(blocks, y * 256 + z * 16 + x, 4096, 4)?; + let descriptor = BlockState::from_nbt(entry).ok()?; + let state = *states.entry(descriptor.clone()).or_insert_with(|| { + assets + .registry + .id(&descriptor) + .or_else(|| assets.registry.default_state(&descriptor.name)) + }); + let Some(state) = state else { + continue; + }; + let properties = assets.properties.heightmap_state(state); + if properties.is_air() { + continue; + } + let height = section_y * 16 + y as i32 + 1; + if matches!( + descriptor.name.as_str(), + "minecraft:water" | "minecraft:lava" + ) { + water[cell].get_or_insert((height, state)); + continue; + } + if !properties.blocks_motion_in_heightmap() { + continue; + } + let biome = section + .get("biomes") + .and_then(|container| { + lod_palette_entry(container, (y / 4) * 16 + (z / 4) * 4 + x / 4, 64, 1) + }) + .and_then(|tag| match tag { + Tag::String(name) => Some(biomes.id(&name.to_string_lossy())), + _ => None, + }) + .unwrap_or(0); + surfaces[cell] = Some(crate::lod::Surface { + height, + state, + water: water[cell], + biome, + }); + break; + } + } + } + } + Some(surfaces) +} +impl SaveDir { + fn lod_surfaces( + &self, + x: i32, + z: i32, + assets: &crate::assets::Assets, + biomes: &Biomes, + ) -> Option { + let pending = self + .pending + .lock() + .ok()? + .get(&(false, x, z)) + .map(|(_, record)| record.clone()); + let root = if let Some(record) = pending { + record.tag().map(|tag| (*tag).clone())? + } else { + // Far-only reads must not populate the gameplay region cache across the whole view. + let mut region = RegionFile::open(self.region_path("region", x >> 5, z >> 5)).ok()?; + region + .read(ChunkPos::new(x, z)) + .ok()?? + .nbt(Limits::default()) + .ok()? + }; + decode_lod_surfaces(&root, assets, biomes) + } +} + impl ChunkGenerator for Persistent { type Worker = G::Worker; fn worker(&self) -> G::Worker { self.inner.worker() } + fn lod_sampler(self: Arc) -> Box { + let inner = ChunkGenerator::lod_sampler(self.inner.clone()); + Box::new(SavedLodSampler { + generator: self, + inner, + cache: Default::default(), + }) + } fn load(&self, x: i32, z: i32) -> Option { self.save .tables @@ -2799,6 +2976,10 @@ impl ChunkGenerator for Persistent { chunk.entities = self.save.load_entities(x, z); Some(chunk) } + fn lod_surface(&self, worker: &mut G::Worker, x: i32, z: i32) -> Option { + // Resident/visited summaries override this procedural fallback in the LOD cache. + self.inner.lod_surface(worker, x, z) + } fn prepare_loaded(&self, worker: &mut G::Worker, chunk: &Chunk) { self.inner.prepare_loaded(worker, chunk); } @@ -2857,6 +3038,52 @@ impl ChunkGenerator for Persistent { #[cfg(test)] mod tests { + #[test] + fn saved_far_surface_reads_builds_and_water_without_gameplay_chunks() { + let Some(root) = std::env::var_os("HORIZON_TEST_RESOURCES") else { + return; + }; + let root = PathBuf::from(root); + let assets = crate::assets::Assets::load(&root).expect("assets"); + let biomes = Biomes::load(&root).expect("biomes"); + let stone = assets.registry.default_state("minecraft:stone").unwrap(); + let water = assets.registry.default_state("minecraft:water").unwrap(); + let sand = assets.registry.default_state("minecraft:sand").unwrap(); + let mut chunk = Chunk::empty(-2, 3); + chunk.set(0, 109, 0, stone.0); + chunk.set(4, 54, 4, sand.0); + for y in 55..64 { + chunk.set(4, y, 4, water.0); + } + let mut saved = encode(&chunk, assets.registry, &biomes); + put( + &mut saved.tag, + "block_entities", + Tag::String("not decoded by far sampling".into()), + ); + let dir = std::env::temp_dir().join(format!("horizon-lod-save-{}", std::process::id())); + fs::create_dir_all(&dir).unwrap(); + let save = SaveDir::new(dir.clone()); + save.write_records("region", vec![((-2, 3), Some(saved))], true) + .unwrap(); + let reopened = SaveDir::new(dir.clone()); + let surfaces = reopened + .lod_surfaces(-2, 3, &assets, &biomes) + .expect("saved summary"); + assert_eq!(surfaces[0].unwrap().height, 110); + assert_eq!(surfaces[0].unwrap().state, stone); + let underwater = surfaces[5].unwrap(); + assert_eq!((underwater.height, underwater.state), (55, sand)); + assert_eq!(underwater.water, Some((64, water))); + assert!(surfaces[15].is_none(), "saved void stays empty"); + assert!( + reopened.regions.lock().unwrap().is_empty(), + "far reads do not grow gameplay region cache" + ); + drop((save, reopened)); + fs::remove_dir_all(dir).unwrap(); + } + #[test] fn save_batch_preserves_order_coordinates_and_ownership() { use crate::containers::BlockEntity; diff --git a/crates/horizon-client/src/streaming.rs b/crates/horizon-client/src/streaming.rs index 38fa33c7d..6453908c7 100644 --- a/crates/horizon-client/src/streaming.rs +++ b/crates/horizon-client/src/streaming.rs @@ -32,6 +32,25 @@ pub type Proto = Box; pub trait ChunkGenerator: Send + Sync + 'static { type Worker: Send + 'static; fn worker(&self) -> Self::Worker; + fn lod_sampler(self: Arc) -> Box + where + Self: Sized, + { + let worker = self.worker(); + Box::new(GeneratorSampler { + generator: self, + worker, + }) + } + /// An undecorated far-terrain column, without allocating gameplay chunks. + fn lod_surface( + &self, + _worker: &mut Self::Worker, + _x: i32, + _z: i32, + ) -> Option { + None + } /// A previously saved final chunk, bypassing generation. fn load(&self, _x: i32, _z: i32) -> Option { None @@ -63,8 +82,29 @@ pub trait ChunkGenerator: Send + Sync + 'static { /// neighbours' decoration is done, so it can't be generated again. fn save_finished(&self, _chunk: Chunk) {} } +/// Independent, thread-owned far-terrain sampling state. +pub trait LodSampler: Send { + fn sample(&mut self, x: i32, z: i32) -> Option; +} +pub trait LodSource: Send + Sync { + fn sampler(self: Arc) -> Box; +} +struct GeneratorSampler { + generator: Arc, + worker: G::Worker, +} +impl LodSampler for GeneratorSampler { + fn sample(&mut self, x: i32, z: i32) -> Option { + self.generator.lod_surface(&mut self.worker, x, z) + } +} +impl LodSource for G { + fn sampler(self: Arc) -> Box { + ChunkGenerator::lod_sampler(self) + } +} /// The generator's saving, for the streamer (which doesn't know its type). -trait Saves: Send + Sync { +trait Saves: LodSource { fn save_protos(&self, protos: &[&Proto]); fn save_finished(&self, chunk: Chunk); } @@ -486,6 +526,10 @@ impl Streaming { mesh: true, } } + /// Shares generation resources, but gives distant work its own worker state. + pub fn lod_source(&self) -> Arc { + self.saves.clone() + } /// 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. diff --git a/crates/horizon-client/src/testgen.rs b/crates/horizon-client/src/testgen.rs index 6268a8b6c..cded7ae47 100644 --- a/crates/horizon-client/src/testgen.rs +++ b/crates/horizon-client/src/testgen.rs @@ -64,6 +64,19 @@ fn hash(x: i32, z: i32) -> u32 { impl ChunkGenerator for TestTerrain { type Worker = (); fn worker(&self) {} + fn lod_surface(&self, _: &mut (), x: i32, z: i32) -> Option { + let h = Self::height(x, z); + Some(crate::lod::Surface { + height: h + 1, + state: horizon_world::registry::BlockStateId(if h <= 63 { + self.sand + } else { + self.grass + }), + water: (h < 62).then_some((63, horizon_world::registry::BlockStateId(self.water))), + biome: self.plains, + }) + } fn proto(&self, w: &mut (), x: i32, z: i32) -> crate::streaming::Proto { Box::new(self.generate(w, x, z)) } @@ -136,3 +149,38 @@ impl TestTerrain { chunk } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn far_surface_agrees_with_generated_land_and_water() { + let terrain = TestTerrain { + stone: 1, + dirt: 2, + grass: 3, + water: 4, + sand: 5, + bedrock: 6, + log: 7, + leaves: 8, + short_grass: 9, + plains: 10, + }; + let mut wet = false; + for (cx, cz) in [(0, 0), (-7, 3), (15, -12), (-21, -8)] { + let chunk = terrain.generate(&mut (), cx, cz); + for x in 0..16 { + let surface = terrain.lod_surface(&mut (), cx * 16 + x, cz * 16).unwrap(); + assert_eq!(chunk.get(x, surface.height - 1, 0), surface.state.0); + if let Some((height, state)) = surface.water { + wet = true; + assert_eq!(chunk.get(x, height - 1, 0), state.0); + assert_eq!(chunk.get(x, height, 0), 0); + } + } + } + assert!(wet, "fixture includes underwater land"); + } +} diff --git a/crates/horizon-client/src/vanillagen.rs b/crates/horizon-client/src/vanillagen.rs index 657817bb9..6d8aeaa4b 100644 --- a/crates/horizon-client/src/vanillagen.rs +++ b/crates/horizon-client/src/vanillagen.rs @@ -35,6 +35,7 @@ pub struct VanillaTerrain { tick_blocks: Vec, /// Block index -> default state id: `tick_blocks` the other way. tick_states: Vec, + lod_supported: bool, } /// A proto chunk, or the position of one whose generation failed. enum VanillaProto { @@ -106,6 +107,7 @@ impl VanillaTerrain { query: std::sync::Mutex::new(None), tick_blocks, tick_states, + lod_supported: dimension != "minecraft:the_nether", }) } /// A proto chunk's blocks, biomes, scheduled ticks and post-processing @@ -265,6 +267,27 @@ impl ChunkGenerator for VanillaTerrain { fn worker(&self) -> GeneratorWorker { self.generator.worker() } + fn lod_surface( + &self, + worker: &mut GeneratorWorker, + x: i32, + z: i32, + ) -> Option { + if !self.lod_supported { + return None; + } + let surface = worker.surface_sample(x, z)?; + Some(crate::lod::Surface { + height: surface.height, + state: surface.state, + water: surface.water, + biome: self + .biome_map + .get(surface.biome.0 as usize) + .copied() + .unwrap_or(0), + }) + } /// The structure starts a saved chunk references, as vanilla reads them /// with the chunk: built here, on the loading worker, into the /// generator's cache, rather than on the game thread the first time diff --git a/crates/horizon-world/src/worldgen/generator.rs b/crates/horizon-world/src/worldgen/generator.rs index 12dfc10d9..41f5d424b 100644 --- a/crates/horizon-world/src/worldgen/generator.rs +++ b/crates/horizon-world/src/worldgen/generator.rs @@ -24,8 +24,8 @@ use super::{ decode_noise, density::DensityRegistry, material_condition::{ - MaterialConditionRegistry, MaterialRule, MaterialRuleError, MaterialRuleLibrary, - MaterialSurfaceQueries, + MaterialConditionRegistry, MaterialConditions, MaterialDensitySources, MaterialRule, + MaterialRuleError, MaterialRuleLibrary, MaterialSurfaceQueries, }, material_extensions::{ExtensionBlocks, ExtensionNoises, MaterialExtensions}, material_stage::{SurfaceInputs, TopMaterialInputs, build_surface}, @@ -1412,6 +1412,7 @@ trait SeededPipeline: Send + Sync { ) -> Result; fn data(&self) -> &Arc; fn sea_level(&self) -> i32; + fn surface_sample(&self, worker: &mut WorkerState, x: i32, z: i32) -> Option; fn structure_starts( &self, worker: &mut WorkerState, @@ -1594,6 +1595,69 @@ where } None } + fn surface_sample(&self, worker: &mut WorkerState, x: i32, z: i32) -> Option { + let mut top = None; + let mut water = None; + let mut below = 0; + self.noise_column(x, z, &mut |y, state| { + let properties = self.data.states.get(state); + if properties.is_air() || properties.has_fluid() { + if top.is_some() { + return true; + } + if properties.has_fluid() { + water.get_or_insert((y + 1, state)); + } else { + // Match the material traversal's water reset across air gaps. + water = None; + } + } else { + top.get_or_insert((y + 1, state)); + below += 1; + } + false + }); + let (height, state) = top?; + let y = height - 1; + let position = BlockPos::new(x, y, z); + let mut resolver = self.source.resolver( + &self.climate, + self.router.get(RouterField::Erosion).as_ref(), + ); + // Use a fresh bounded context: far samples must not grow the chunk/structure caches. + let mut biome_context = SamplerContext::default(); + let biome = resolver + .sample(&mut biome_context, self.zoom.quart_at_block(position)) + .ok()?; + let biome = self.data.biome_id(biome)?; + let volume = DensityVolume::new([1; 3], [x, y, z], [1; 3]).ok()?; + let queries = SurfaceQueries { + pipeline: self, + temperatures: &mut worker.temperatures, + biome: Biomes::Resolver(&mut resolver, &mut biome_context), + possible: None, + }; + let sources = MaterialDensitySources::new( + queries, + volume, + self.preliminary.clone(), + SamplerContext::default(), + ) + .ok()?; + let mut material = + MaterialConditions::::new(self.generation(), self.root.clone(), sources); + let rule = material.add_rule(self.rule.clone()).ok()?; + // shortcut: distant surfaces omit slope rules and column extensions; nearby chunks provide full terrain. + material.update_xz(x, z, 0, 0); + material.update_y(1, below, water.map_or(i32::MIN, |(height, _)| height), y); + let state = material.try_apply(rule, position).ok()?.unwrap_or(state); + Some(SurfaceSample { + height, + state, + water, + biome, + }) + } fn noise_biomes(&self, worker: &mut WorkerState, quarts: &[[i32; 3]]) -> Vec> { let mut resolver = self.source.resolver( &self.climate, @@ -1864,6 +1928,15 @@ impl OverworldGenerator { } } +/// A surface-only noise column for far terrain. Heights are exclusive top faces. +#[derive(Clone, Copy, Debug)] +pub struct SurfaceSample { + pub height: i32, + pub state: BlockStateId, + pub water: Option<(i32, BlockStateId)>, + pub biome: BiomeId, +} + /// Per-thread generation state. pub struct GeneratorWorker { pipeline: Arc, @@ -1877,6 +1950,10 @@ impl GeneratorWorker { self.state.timings.chunks += 1; result } + /// Noise/aquifer surface and its material rule, without chunks, structures or features. + pub fn surface_sample(&mut self, x: i32, z: i32) -> Option { + self.pipeline.surface_sample(&mut self.state, x, z) + } /// `/locate structure`: the nearest start of one of `names`. pub fn locate( &mut self, @@ -1935,6 +2012,44 @@ impl GeneratorWorker { mod tests { use super::*; + #[test] + fn far_surface_matches_undecorated_land_and_keeps_end_void() { + let root = std::env::var_os("HORIZON_TEST_RESOURCES") + .or_else(|| std::env::var_os("HORIZON_RESOURCES")) + .map(std::path::PathBuf::from) + .unwrap_or_else(|| { + Path::new(env!("CARGO_MANIFEST_DIR")).join("../../.local/horizon-client/26.3") + }); + if !root.join("worldgen/world_presets.json").exists() { + eprintln!("skipping: no worldgen bundle at {}", root.display()); + return; + } + let registry: &'static BlockRegistry = Box::leak(Box::new( + crate::level::data::load_registry(&root).expect("registry"), + )); + let data = GeneratorData::load(&root.join("worldgen"), registry).expect("worldgen data"); + let generator = + OverworldGenerator::without_structures(data.clone(), 1234).expect("generator"); + let mut worker = generator.worker(); + let sample = worker.surface_sample(0, 0).expect("land"); + let chunk = worker.generate_proto(ChunkPos::new(0, 0)).expect("terrain"); + assert_eq!( + sample.water.map_or(sample.height, |(height, _)| height), + chunk.surface_height(0, 0) + ); + assert_eq!(sample.state, chunk.block(0, sample.height - 1, 0)); + assert!( + sample.water.is_some(), + "fixture includes underwater terrain" + ); + let end = + OverworldGenerator::for_dimension(data, 1234, "minecraft:normal", "minecraft:the_end") + .expect("End generator"); + let mut worker = end.worker(); + assert!(worker.surface_sample(0, 0).is_some()); + assert!(worker.surface_sample(256, 0).is_none()); + } + /// Generators of one seed and dimension share their ring positions; /// another seed or generator gets its own, and old entries go. #[test] diff --git a/docs/graphics.md b/docs/graphics.md index a55313bea..727203646 100644 --- a/docs/graphics.md +++ b/docs/graphics.md @@ -4,7 +4,7 @@ In the Horizon menu style, the Options screen's **Video** section holds the graphics settings, with a Simple / Advanced switch at the top (remembered between sessions): -- **Simple**: a preset (Performance, Balanced, Quality, Ultra or Custom), render distance, FOV, max framerate, VSync and GUI scale. +- **Simple**: a preset (Performance, Balanced, Quality, Ultra or Custom), render distance, LOD distance, FOV, max framerate, VSync and GUI scale. - **Advanced**: every setting, grouped under Rendering, Quality, Display, Preferences, Performance and Overlay. In the Classic style, Video Settings starts with two buttons opening the same settings as vanilla-looking screens. @@ -17,7 +17,7 @@ Every row has a one-line description and its cost in frame time (none, low, medi | Group | Setting | Stored in | | --- | --- | --- | | Rendering | render distance, simulation distance, entity distance | `options.txt` (vanilla keys) | -| | cave culling, chunk updates per frame | `horizon-graphics.txt` | +| | LOD distance, cave culling, chunk updates per frame | `horizon-graphics.txt` | | Quality | smooth lighting, mipmap levels, biome blend, clouds, cloud distance, weather radius, particles, entity shadows, cutout leaves | `options.txt` | | | sky, fog, weather, animated textures | `horizon-graphics.txt` | | Display | FOV, max framerate, VSync, fullscreen, GUI scale | `options.txt` | @@ -31,7 +31,17 @@ Every row has a one-line description and its cost in frame time (none, low, medi Which settings each level shows, their descriptions and costs are in `crates/horizon-client/src/graphics_ui.rs` (`rows`); the Horizon shell draws them in `options_horizon.rs` with `ui_theme::described_row`, and the Classic screens use `graphics_style.rs`. -Not implemented: distant-terrain LOD, anisotropic filtering (wgpu needs linear magnification for it, which blurs close blocks), MSAA (FXAA instead) and exclusive fullscreen (fullscreen is borderless). +## Distant terrain + +**LOD Distance** is off by default. Enable it in Simple or Advanced graphics settings and choose 64, 128, 256, 512 or 1024 chunks. With LODs enabled, **Full Detail Distance** controls the nearby normal chunks; LOD Distance controls the outer view. Keep the nearby range modest, for example 12 chunks with a 256-chunk LOD distance. Increasing the distant range does not increase simulation distance. + +Distant terrain uses progressively coarser surface meshes, streamed in bounded background work instead of loading full chunks out to the distant range. It is available in local Overworld and End worlds; surface LODs are disabled in the Nether. Far samples approximate undecorated terrain: distant caves, decorations, structures and player builds are not guaranteed to match the nearby world. Procedural distant terrain requires a local world's generator; remote servers do not supply their world-generation seed. + +LOD terrain enters the existing world colour and depth passes, so supported shader-pack postprocessing can see it. This does not add Iris material passes or shadow maps: Java packs still have the [existing fullscreen-pass limitations](shader-packs.md). Shader appearance and transitions need testing with the selected pack. + +The setting is stored as `lodDistance` in `horizon-graphics.txt`; `0` disables it. Existing `renderDistance` in `options.txt` keeps its normal meaning for full chunks. + +Not implemented: anisotropic filtering (wgpu needs linear magnification for it, which blurs close blocks), MSAA (FXAA instead) and exclusive fullscreen (fullscreen is borderless). ## Benchmark From 594929b7956bf3ef5206ceff880c3e9049a88fc4 Mon Sep 17 00:00:00 2001 From: Cosmic <84875247+Online5277@users.noreply.github.com> Date: Sat, 17 Oct 2026 18:13:20 -0400 Subject: [PATCH 2/6] Validate LOD loading, shader depth and current terrain draw paths --- crates/horizon-client/src/game.rs | 1 + crates/horizon-client/src/lod.rs | 26 ++++++-------------------- docs/graphics.md | 2 +- 3 files changed, 8 insertions(+), 21 deletions(-) diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index 30d5c73c1..1151339a8 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -10694,6 +10694,7 @@ fn loading_frame(hud: &HudBatch) -> Frame<'_> { bob: glam::Mat4::IDENTITY, sky: horizon_client::environment::Sky::default(), distance: 12.0, + detail_distance: 12.0, depth_far: 1000.0, underwater: false, cave_culling: false, diff --git a/crates/horizon-client/src/lod.rs b/crates/horizon-client/src/lod.rs index 3d39c2639..59adc4e7e 100644 --- a/crates/horizon-client/src/lod.rs +++ b/crates/horizon-client/src/lod.rs @@ -326,10 +326,8 @@ impl Lod { } while let Ok(done) = self.results.try_recv() { self.pending.remove(&done.key); - if self.plan.nodes.contains(&done.key) { - if done.samples.len() == CELLS * CELLS { - self.ready.insert(done.key, done.samples); - } + if self.plan.nodes.contains(&done.key) && done.samples.len() == CELLS * CELLS { + self.ready.insert(done.key, done.samples); } } if self.covered != covered { @@ -397,6 +395,8 @@ impl Lod { "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 @@ -414,14 +414,7 @@ impl Lod { .unwrap_or(0) as u16, tint, translucent, - light: 240 - | (self - .context - .tables - .facts(state) - .map_or(0, |f| u16::from(f.emission)) - * 16 - << 8), + light, } } else { let models = self @@ -443,14 +436,7 @@ impl Lod { -1 }, translucent: face.is_some_and(|f| f.translucent), - light: 240 - | (self - .context - .tables - .facts(state) - .map_or(0, |f| u16::from(f.emission)) - * 16 - << 8), + light, } }; self.styles.insert(state, style); diff --git a/docs/graphics.md b/docs/graphics.md index efd1dd00a..1d125a851 100644 --- a/docs/graphics.md +++ b/docs/graphics.md @@ -35,7 +35,7 @@ Which settings each level shows, their descriptions and costs are in `crates/hor **LOD Distance** is off by default. Enable it in Simple or Advanced graphics settings and choose 64, 128, 256, 512 or 1024 chunks. With LODs enabled, **Full Detail Distance** controls the nearby normal chunks; LOD Distance controls the outer view. Keep the nearby range modest, for example 12 chunks with a 256-chunk LOD distance. Increasing the distant range does not increase simulation distance. -Distant terrain uses progressively coarser surface meshes, streamed in bounded background work instead of loading full chunks out to the distant range. It is available in local Overworld and End worlds; surface LODs are disabled in the Nether. Far samples approximate undecorated terrain: distant caves, decorations, structures and player builds are not guaranteed to match the nearby world. Procedural distant terrain requires a local world's generator; remote servers do not supply their world-generation seed. +Distant terrain uses progressively coarser surface meshes, streamed in bounded background work instead of loading full chunks out to the distant range. It is available in local Overworld and End worlds; surface LODs are disabled in the Nether. Unknown terrain samples use noise/aquifer heights and surface materials, omitting caves, decorations, structures and slope-dependent extensions. Loaded and saved terrain supplies bounded surface summaries, including player edits; small details can disappear between samples. This is a surface approximation, not full block geometry. Procedural distant terrain requires a local world's generator; remote servers do not supply their world-generation seed. LOD terrain enters the existing world colour and depth passes, so supported shader-pack postprocessing can see it. This does not add Iris material passes or shadow maps: Java packs still have the [existing fullscreen-pass limitations](shader-packs.md). Shader appearance and transitions need testing with the selected pack. From fde95ff20e31a6503b4885ba8503301dc9dc100b Mon Sep 17 00:00:00 2001 From: Cosmic <84875247+Online5277@users.noreply.github.com> Date: Sun, 18 Oct 2026 05:30:56 -0400 Subject: [PATCH 3/6] Build distant terrain from sparse seed samples and layered voxel columns --- crates/horizon-client/src/fog.rs | 47 + crates/horizon-client/src/game.rs | 83 +- crates/horizon-client/src/game/control.rs | 5 + crates/horizon-client/src/game/remote.rs | 3 + crates/horizon-client/src/game/render_tick.rs | 1 + crates/horizon-client/src/lod.rs | 1622 +++++++++++++---- crates/horizon-client/src/lod/mesh.rs | 1102 +++++++++++ crates/horizon-client/src/save.rs | 475 ++++- crates/horizon-client/src/streaming.rs | 29 + crates/horizon-client/src/vanillagen.rs | 44 +- crates/horizon-client/src/vanillagen/lod.rs | 700 +++++++ .../src/vanillagen/lod/vegetation.rs | 309 ++++ crates/horizon-core/src/density/cache.rs | 8 + .../horizon-core/src/density/interpolation.rs | 12 + crates/horizon-core/src/density/mod.rs | 1 + crates/horizon-core/src/density/tests.rs | 22 + .../horizon-world/src/worldgen/generator.rs | 370 +++- docs/graphics.md | 12 +- docs/shader-packs.md | 2 +- 19 files changed, 4326 insertions(+), 521 deletions(-) create mode 100644 crates/horizon-client/src/lod/mesh.rs create mode 100644 crates/horizon-client/src/vanillagen/lod.rs create mode 100644 crates/horizon-client/src/vanillagen/lod/vegetation.rs 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