diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index 2df6aa9b0..a115796e5 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -10247,6 +10247,10 @@ impl Game<'_> { if let (Some(lod), Some(player)) = (&mut self.lod, &self.player) { let lod_distance = self.settings.horizon.lod_distance_on(&renderer.info); lod.set_near_loading(self.streaming.busy()); + lod.set_reduced_detail( + self.settings.horizon.far_detail + == horizon_client::graphics::FarDetail::Reduced, + ); // The covered columns change only with the complete ones and // the render area: rebuilt then, not every frame. let at = ( diff --git a/crates/horizon-client/src/game/control.rs b/crates/horizon-client/src/game/control.rs index d79f69d3c..df641d5c2 100644 --- a/crates/horizon-client/src/game/control.rs +++ b/crates/horizon-client/src/game/control.rs @@ -838,6 +838,8 @@ impl Game<'_> { "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), + "far_detail": self.settings.horizon.far_detail.name(), + "resident_quads": self.renderer.as_ref().map_or(0, |r| r.lod_quads()), "pending_observations": self.lod_observe.len(), "refined_samples": self.lod.as_ref().map_or(0, |lod| lod.refined_samples()), }); diff --git a/crates/horizon-client/src/gfx.rs b/crates/horizon-client/src/gfx.rs index 127aaf737..9be2958fb 100644 --- a/crates/horizon-client/src/gfx.rs +++ b/crates/horizon-client/src/gfx.rs @@ -850,6 +850,13 @@ impl Renderer { pub fn resident_sections(&self) -> usize { self.sections.len() } + /// Quads of the far terrain tiles with GPU buffers. + pub fn lod_quads(&self) -> u64 { + self.lods + .values() + .map(|tile| u64::from(tile.mesh.solid + tile.mesh.translucent)) + .sum() + } /// Bytes of the section vertex arena's buffers, and of the vertices /// resident sections hold in them. pub fn section_vertex_bytes(&self) -> [u64; 2] { @@ -916,6 +923,7 @@ impl Renderer { None => open_device(&window, offscreen, api, layers).await?, }; let info = adapter.get_info(); + crate::graphics::suggest_reduced_far_detail(crate::graphics::lod_capped(&info)); let size = window.inner_size(); let (config, present_modes) = if let Some(surface) = &surface { let mut config = surface diff --git a/crates/horizon-client/src/graphics.rs b/crates/horizon-client/src/graphics.rs index bea0ffaa9..1974c4538 100644 --- a/crates/horizon-client/src/graphics.rs +++ b/crates/horizon-client/src/graphics.rs @@ -156,6 +156,48 @@ pub enum Overlay { Graph, } +/// How finely far terrain is drawn (`FarDetail` in the LOD options). +#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)] +pub enum FarDetail { + /// Each tile level starts at eight times its size from the camera. + #[default] + Full, + /// Coarser levels start at half that distance: about a quarter of the + /// far quads, for GPUs that far terrain slows down. Changes the picture. + Reduced, +} +impl FarDetail { + pub fn name(self) -> &'static str { + match self { + FarDetail::Full => "full", + FarDetail::Reduced => "reduced", + } + } + pub fn label(self) -> &'static str { + match self { + FarDetail::Full => "Full", + FarDetail::Reduced => "Reduced", + } + } + fn parse(s: &str) -> Self { + if s == "reduced" { + FarDetail::Reduced + } else { + FarDetail::Full + } + } +} + +static REDUCED_SUGGESTED: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false); +/// Whether the settings suggest Reduced far detail: set when the renderer +/// starts, on the GPUs `lod_capped` names (far terrain slows them most). +pub fn suggest_reduced_far_detail(on: bool) { + REDUCED_SUGGESTED.store(on, std::sync::atomic::Ordering::Relaxed); +} +pub fn reduced_far_detail_suggested() -> bool { + REDUCED_SUGGESTED.load(std::sync::atomic::Ordering::Relaxed) +} + /// The farthest LOD Distance, in chunks, on integrated GPUs other than /// Apple's: they share the CPU's memory bandwidth, and past it far terrain /// costs them more frame time than it is worth. @@ -181,6 +223,8 @@ pub struct Graphics { pub preset: Preset, /// Distant surface terrain range, in chunks; 0 disables LODs. pub lod_distance: u32, + /// How finely far terrain is drawn; Full unless the player picks Reduced. + pub far_detail: FarDetail, /// Skip sections hidden behind closed ones (vanilla's occlusion graph). pub cave_culling: bool, /// Hide shared leaf and mangrove-root faces, reducing foliage density. @@ -221,6 +265,7 @@ impl Default for Graphics { Self { preset: Preset::Custom, lod_distance: 0, + far_detail: FarDetail::Full, cave_culling: true, cull_leaves: false, entity_culling: false, @@ -268,6 +313,7 @@ impl Graphics { match key.trim() { "preset" => g.preset = Preset::parse(value), "lodDistance" => g.lod_distance = number(g.lod_distance).min(1024), + "farDetail" => g.far_detail = FarDetail::parse(value), "caveCulling" => g.cave_culling = flag(g.cave_culling), "cullLeaves" => g.cull_leaves = flag(g.cull_leaves), "entityCulling" => g.entity_culling = flag(g.entity_culling), @@ -306,9 +352,10 @@ impl Graphics { } pub fn serialize(&self) -> String { format!( - "preset:{}\nlodDistance:{}\ncaveCulling:{}\ncullLeaves:{}\nentityCulling:{}\nchunkUpdates:{}\nuploadBudgetKiB:{}\nsky:{}\nfog:{}\nweather:{}\nanimatedTextures:{}\npresentMode:{}\nframePacing:{}\nworkers:{}\noverlay:{}\nresolutionScale:{}\nfxaa:{}\nlevel:{}\nbatteryMode:{}\nshaderPack:{}\ngraphicsApi:{}\n", + "preset:{}\nlodDistance:{}\nfarDetail:{}\ncaveCulling:{}\ncullLeaves:{}\nentityCulling:{}\nchunkUpdates:{}\nuploadBudgetKiB:{}\nsky:{}\nfog:{}\nweather:{}\nanimatedTextures:{}\npresentMode:{}\nframePacing:{}\nworkers:{}\noverlay:{}\nresolutionScale:{}\nfxaa:{}\nlevel:{}\nbatteryMode:{}\nshaderPack:{}\ngraphicsApi:{}\n", self.preset.name(), self.lod_distance, + self.far_detail.name(), bool_text(self.cave_culling), bool_text(self.cull_leaves), bool_text(self.entity_culling), @@ -410,6 +457,7 @@ mod tests { let g = Graphics { preset: Preset::Balanced, lod_distance: 256, + far_detail: FarDetail::Reduced, cave_culling: false, cull_leaves: true, entity_culling: true, @@ -491,6 +539,24 @@ mod tests { assert_eq!(Graphics::parse("lodDistance:invalid\n").lod_distance, 0); } + #[test] + fn far_detail_is_full_unless_reduced_is_picked() { + assert_eq!(Graphics::default().far_detail, FarDetail::Full); + assert_eq!( + Graphics::parse("farDetail:reduced\n").far_detail, + FarDetail::Reduced + ); + for text in ["farDetail:full\n", "farDetail:other\n", ""] { + assert_eq!(Graphics::parse(text).far_detail, FarDetail::Full); + } + // No preset changes it. + for preset in Preset::ALL { + let mut s = crate::settings::Settings::default(); + apply_preset(preset, &mut s); + assert_eq!(s.horizon.far_detail, FarDetail::Full, "{preset:?}"); + } + } + #[test] fn automatic_workers_leave_cores_for_the_render_and_server_threads() { let g = Graphics::default(); diff --git a/crates/horizon-client/src/graphics_ui.rs b/crates/horizon-client/src/graphics_ui.rs index eea2d03cb..cc27b9488 100644 --- a/crates/horizon-client/src/graphics_ui.rs +++ b/crates/horizon-client/src/graphics_ui.rs @@ -2,7 +2,7 @@ //! they show, in what groups, with what description and cost, and their //! input. How they look comes from `graphics_style`. use crate::{ - graphics::{GraphicsApi, Overlay, Pacing, PresentMode, Preset}, + graphics::{FarDetail, GraphicsApi, Overlay, Pacing, PresentMode, Preset}, graphics_style::{Cost, STYLE}, hud::{HudAtlas, HudBatch}, options_ui::Opt, @@ -22,6 +22,7 @@ pub enum Level { pub enum H { Preset, LodDistance, + FarDetail, CaveCulling, CullLeaves, EntityCulling, @@ -85,6 +86,16 @@ pub fn rows(level: Level) -> Vec { Medium, "Coarse distant terrain in local worlds. Integrated GPUs stop at 128 chunks.", ); + // Suggested, never switched on, where far terrain costs the GPU most. + let far = h( + H::FarDetail, + Medium, + if crate::graphics::reduced_far_detail_suggested() { + "Reduced draws far terrain coarser: faster on this integrated GPU." + } else { + "Reduced draws far terrain coarser, for GPUs it slows down." + }, + ); let fov = v( Opt::Fov, Low, @@ -107,11 +118,12 @@ pub fn rows(level: Level) -> Vec { "Sets the options below together. Changing one makes it Custom.", ); let mut rows = match level { - Level::Simple => vec![preset, render, lod, fov, fps, vsync, gui], + Level::Simple => vec![preset, render, lod, far, fov, fps, vsync, gui], Level::Advanced => vec![ Row::Header("Rendering"), render, lod, + far, v( Opt::SimulationDistance, Medium, @@ -335,6 +347,7 @@ impl H { 0 => labelled("LOD Distance", "Off"), n => labelled("LOD Distance", format!("{n} chunks")), }, + H::FarDetail => labelled("Far Detail", g.far_detail.label()), 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)), @@ -411,6 +424,9 @@ impl H { H::LodDistance => { g.lod_distance = step(&[0, 64, 128, 256, 512, 1024], g.lod_distance, back) } + H::FarDetail => { + g.far_detail = step(&[FarDetail::Full, FarDetail::Reduced], g.far_detail, 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, diff --git a/crates/horizon-client/src/lod.rs b/crates/horizon-client/src/lod.rs index 0aa7b1f94..f8adb1f16 100644 --- a/crates/horizon-client/src/lod.rs +++ b/crates/horizon-client/src/lod.rs @@ -326,14 +326,17 @@ struct Plan { impl Plan { #[cfg(test)] fn new(camera: [i32; 2], detail: i32, distance: u32) -> Self { - Self::with_previous(camera, detail, distance, &Self::default(), 0) + Self::with_previous(camera, detail, distance, &Self::default(), 0, false) } + /// `reduced`: Reduced far detail (`graphics::FarDetail`), each level from + /// half the distance. fn with_previous( camera: [i32; 2], detail: i32, distance: u32, previous: &Self, elevation: i32, + reduced: bool, ) -> Self { let range = distance.min(1024) as i32 * 16; let mut level = 0; @@ -349,25 +352,35 @@ impl Plan { 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, previous); + result.visit(key, camera, detail, range, previous, reduced); } } result } - fn visit(&mut self, key: TileKey, camera: [i32; 2], detail: i32, range: i32, previous: &Self) { + fn visit( + &mut self, + key: TileKey, + camera: [i32; 2], + detail: i32, + range: i32, + previous: &Self, + reduced: bool, + ) { if key.distance(camera) > range { return; } self.nodes.insert(key); - // Keep distant cells small on screen; finest tiles retain individual blocks. - let threshold = key.size() * if previous.split.contains(&key) { 10 } else { 8 }; + // Keep distant cells small on screen; finest tiles retain individual + // blocks. Reduced keeps them twice as large (a quarter of the cells). + let split = if previous.split.contains(&key) { 10 } else { 8 }; + let threshold = key.size() * if reduced { split / 2 } else { split }; 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, previous); + self.visit(child, camera, detail, range, previous, reduced); } } } @@ -517,8 +530,10 @@ pub struct Lod { /// The plan or the covered columns changed: covered leaves need stand-ins. replanned: bool, invalidated: FxHashSet<(i32, i32)>, - area: Option<([i32; 2], i32, u32, i32)>, + area: Option<([i32; 2], i32, u32, i32, bool)>, plan: Plan, + /// Reduced far detail (`set_reduced_detail`). + reduced_detail: bool, pending: FxHashSet, sample_nearby: bool, detail_masks: FxHashMap, @@ -714,6 +729,7 @@ impl Lod { replanned: true, invalidated: FxHashSet::default(), area: None, + reduced_detail: false, plan: Plan::default(), pending: FxHashSet::default(), sample_nearby: true, @@ -930,6 +946,11 @@ impl Lod { pub fn set_near_loading(&mut self, loading: bool) { self.near_loading = loading; } + /// Reduced far detail (`graphics::FarDetail::Reduced`): the next update + /// replans when it changes. + pub fn set_reduced_detail(&mut self, reduced: bool) { + self.reduced_detail = reduced; + } fn held(&self) -> bool { self.small && self.near_loading } @@ -1021,11 +1042,19 @@ impl Lod { detail, distance, elevation, + self.reduced_detail, ); if self.area != Some(area) { crate::spans::time("lod_plan", || { self.area = Some(area); - self.plan = Plan::with_previous(camera, detail, distance, &self.plan, elevation); + self.plan = Plan::with_previous( + camera, + detail, + distance, + &self.plan, + elevation, + self.reduced_detail, + ); self.ready.retain(|key, _| self.plan.nodes.contains(key)); self.detail_masks .retain(|key, _| self.plan.nodes.contains(key)); @@ -1421,6 +1450,35 @@ mod tests { use super::*; use crate::mesher::TerrainVertex; #[test] + fn reduced_far_detail_only_merges_tiles_away_from_the_near_terrain() { + let leaves = |plan: &Plan| { + plan.nodes + .iter() + .filter(|k| !plan.split.contains(k)) + .count() + }; + for camera in [[0, 0], [100, -40]] { + for distance in [64, 128, 512, 1024] { + let full = Plan::new(camera, 12, distance); + let reduced = Plan::with_previous(camera, 12, distance, &Plan::default(), 0, true); + // Never finer than Full anywhere, and as fine next to the + // full-detail area, whose edge the finest tiles match. + assert!(reduced.split.is_subset(&full.split)); + assert_eq!(reduced.roots, full.roots); + for key in reduced.nodes.iter().filter(|k| !reduced.split.contains(k)) { + if key.distance(camera) < 12 * 16 + 16 { + assert_eq!(key.level, 0, "{key:?}"); + } + } + let (full, reduced) = (leaves(&full), leaves(&reduced)); + assert!( + reduced * 2 < full, + "{reduced} of {full} tiles at {distance}" + ); + } + } + } + #[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] { @@ -1453,7 +1511,7 @@ mod tests { #[test] 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); + let high = Plan::with_previous([0, 0], 12, 1024, &Plan::default(), 1024, false); assert!(high.nodes.len() < ground.nodes.len()); assert!( high.nodes.contains(&TileKey { @@ -1505,7 +1563,7 @@ mod tests { #[test] 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 after = Plan::with_previous([16, 0], 2, 256, &before, 0, false); let mut retained = 0; for key in &before.split { if key.distance([16, 0]) < key.size() * 10 { @@ -1804,7 +1862,7 @@ mod tests { // 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.area = Some(([-16, -16], 2, 64, 0, false)); lod.plan.roots = vec![key]; lod.plan.nodes.insert(key); // As a replan does (`update` only selects after a change).