Merge pull request #771 from Online5277/feat/terrain-lod

🤖 Agent: merged, thanks Online5277! Terrain LOD lands off by default. With LODs off it matches main (0 differing pixels in 6 RD16 views, same FPS/GPU ms on the HD 620, same time to full render distance); CI verify and parity pass. We merged main in three times and adapted it to #905/#939 and the packed texture coordinates. The follow-ups from the review (edge band, integrated-GPU cost, lighter far meshes) we're taking on ourselves.
This commit is contained in:
Ludovic Marie
2026-10-19 07:49:12 +02:00
committed by GitHub
32 changed files with 5891 additions and 89 deletions
+36 -1
View File
@@ -6,6 +6,7 @@ use horizon_world::{nbt::Tag, registry::BlockRegistry};
pub struct FlatTerrain {
layers: Vec<u32>,
biome: u16,
surface: Option<crate::lod::Surface>,
}
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<crate::lod::Surface> {
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 {
+47
View File
@@ -181,6 +181,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,
@@ -337,6 +339,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`.
@@ -369,6 +383,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,
@@ -447,6 +462,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,
+133 -10
View File
@@ -1002,6 +1002,9 @@ struct Game<'a> {
screen_fx: horizon_client::screen_effects::ScreenEffects,
world: World,
streaming: Streaming,
lod: Option<horizon_client::lod::Lod>,
lod_source_attempted: bool,
lod_observe: horizon_client::fast_hash::FxHashSet<(i32, i32)>,
options: Options,
player: Option<PlayerMotion>,
spawn: Option<Vec3>,
@@ -6881,6 +6884,8 @@ impl Game<'_> {
self.using = None;
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();
}
@@ -7035,6 +7040,8 @@ impl Game<'_> {
self.pickups.clear();
self.orbs.clear();
self.mining = None;
self.clear_lod();
self.pending_uploads.clear();
if let Some(r) = self.renderer.as_mut() {
r.clear_sections();
}
@@ -7213,6 +7220,8 @@ impl Game<'_> {
self.sleep_timer = 0;
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();
}
@@ -9158,9 +9167,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()
@@ -9237,6 +9247,7 @@ impl Game<'_> {
bob,
sky,
distance,
detail_distance: (self.streaming.radius * 16) as f32,
depth_far,
underwater,
title: title && !live,
@@ -9248,7 +9259,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);
}
@@ -9416,7 +9433,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)
@@ -9623,10 +9640,62 @@ impl Game<'_> {
}
Ok(())
}
/// Only enabled LOD worlds extend the renderer's range, never simulation.
fn terrain_distance(&self) -> i32 {
if self.lod.is_some() {
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.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,
@@ -9635,6 +9704,23 @@ 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 {
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) {
horizon_client::spans::time("lod_observe", || {
lod.observe(chunk, self.res.facts())
});
}
}
}
let dirty: Vec<_> = unloaded
.iter()
.filter(|c| self.world.modified.remove(&(c.x, c.z)))
@@ -9672,14 +9758,18 @@ impl Game<'_> {
} else {
// 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.
// mesh of a waiting section replaces it, and sections of
// chunks no longer loaded are dropped.
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)));
@@ -9693,7 +9783,9 @@ impl Game<'_> {
};
let (mut count, mut bytes) = (0, 0u64);
while let Some((_, mesh)) = pending.last() {
let size = ((mesh.solid.len() + mesh.translucent.len()) * 32) as u64;
let size = ((mesh.solid.len() + mesh.translucent.len())
* std::mem::size_of::<horizon_client::mesher::TerrainVertex>())
as u64;
if (limit > 0 && count >= limit)
|| (budget > 0 && bytes > 0 && bytes + size > budget)
{
@@ -9710,6 +9802,33 @@ 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 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);
}
}
}
}
}
@@ -10607,6 +10726,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,
@@ -11426,6 +11548,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,
+25
View File
@@ -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",
@@ -410,6 +411,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" => {
let time: i64 = arg(1)?.parse()?;
self.world.day_time = time;
@@ -740,6 +750,16 @@ 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(),
"refined_samples": self.lod.as_ref().map_or(0, |lod| lod.refined_samples()),
});
let mut status = json!({
"screen": self.screen_name(),
"debug": {
@@ -798,6 +818,11 @@ impl Game<'_> {
.eye_position()
.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);
// Boats, minecarts, armor stands, item frames and paintings.
status["objects"] = json!(self.objects_status());
+1
View File
@@ -189,6 +189,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
+6
View File
@@ -288,6 +288,8 @@ impl Game<'_> {
self.screen = Screen::Playing;
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();
}
@@ -396,6 +398,8 @@ impl Game<'_> {
self.border = Default::default();
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();
}
@@ -706,6 +710,8 @@ impl Game<'_> {
// A new ClientLevel: a new chunk storage, centred on 0, 0.
r.chunk_center = (0, 0);
}
self.clear_lod();
self.pending_uploads.clear();
if let Some(renderer) = self.renderer.as_mut() {
renderer.clear_sections();
}
@@ -91,7 +91,8 @@ impl Game<'_> {
(water & 255) as f32 / 255.0,
],
biome,
render_distance_chunks: self.streaming.radius,
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.
+182 -54
View File
@@ -61,6 +61,7 @@ mod arena;
mod cull;
mod flat;
mod java_stages;
mod lod;
mod occlusion;
mod pack_stages;
pub mod panorama;
@@ -70,6 +71,16 @@ pub mod translucency;
pub(crate) mod upload;
mod world_post;
/// A section's instance: its camera-relative origin and, in `w`, the step of
/// its fixed-point UVs (`terrain.wgsl`'s `terrain_uv`).
fn section_instance(origin: [f32; 3]) -> [f32; 4] {
[
origin[0],
origin[1],
origin[2],
1.0 / crate::mesher::UV_STEPS,
]
}
/// A new value for `Renderer::block_entity_revision`.
fn next_block_entity_revision() -> u64 {
static NEXT: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
@@ -131,6 +142,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,
@@ -352,6 +365,8 @@ pub struct Renderer {
outline: wgpu::Buffer,
depth: wgpu::TextureView,
sections: resident::Resident<SectionGpu>,
lods: HashMap<crate::lod::TileKey, lod::Tile>,
detail_columns: HashMap<(i32, i32), u32>,
arena: arena::Arena,
sorted_indices: arena::Arena,
translucent_sorter: translucency::Sorter,
@@ -363,6 +378,8 @@ pub struct Renderer {
pub multi_draw: bool,
/// Per-frame scratch, kept to avoid allocating every frame.
visible: Vec<resident::Visible>,
visible_lods: Vec<lod::Visible>,
translucent_draws: Vec<(u64, usize, [u32; 5], (u32, Option<u32>))>,
origins: Vec<[f32; 4]>,
/// `DrawIndexedIndirectArgs` words: index count, instance count, first
/// index, base vertex, first instance.
@@ -570,6 +587,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]
@@ -1856,6 +1874,8 @@ impl Renderer {
outline,
depth,
sections: resident::Resident::default(),
lods: HashMap::new(),
detail_columns: HashMap::new(),
arena,
sorted_indices,
translucent_sorter: translucency::Sorter::default(),
@@ -1864,6 +1884,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(),
@@ -2067,11 +2089,22 @@ impl Renderer {
if changed {
self.block_entity_revision = next_block_entity_revision();
}
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<SectionGpu> {
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 {
@@ -2086,16 +2119,14 @@ impl Renderer {
})
});
// With video and system memory both full, the section is not drawn.
let Some((page, start)) = self.arena.alloc(&self.device, encoder, vertices) else {
return;
};
let (page, start) = self.arena.alloc(&self.device, encoder, vertices)?;
let sorted_indices = if translucent > 0 {
let indices =
self.sorted_indices
.alloc(&self.device, encoder, u64::from(translucent) * 6);
if indices.is_none() {
self.arena.release(page, start, vertices);
return;
return None;
}
indices
} else {
@@ -2130,26 +2161,23 @@ impl Renderer {
{
self.queue.submit([copies]);
}
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,
flat_id,
},
);
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,
flat_id,
})
}
/// The section copies recorded since the last submission, closed.
fn take_section_copies(&mut self) -> Option<wgpu::CommandBuffer> {
@@ -2194,17 +2222,20 @@ impl Renderer {
}
fn release_section(&mut self, key: SectionKey) {
if let Some(s) = self.sections.remove(key) {
if let (Some(flat), Some(id)) = (&mut self.flat, s.flat_id) {
flat.remove(id);
}
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(flat), Some(id)) = (&mut self.flat, s.flat_id) {
flat.remove(id);
}
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<Item = &([i32; 3], u32)> {
self.block_entities.values().flatten()
@@ -2217,6 +2248,8 @@ impl Renderer {
self.block_entities.clear();
self.block_entity_revision = next_block_entity_revision();
self.sections.clear();
self.lods.clear();
self.detail_columns.clear();
self.arena.clear();
if let Some(flat) = &mut self.flat {
flat.clear();
@@ -2225,7 +2258,19 @@ impl Renderer {
self.translucency_cursor = 0;
self.occlusion.clear();
}
pub fn lod_detail_columns(&self) -> impl Iterator<Item = (i32, i32)> + '_ {
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));
}
}
if self.block_entities.remove(&key).is_some() {
self.block_entity_revision = next_block_entity_revision();
}
@@ -2608,7 +2653,9 @@ impl Renderer {
let camera_section = camera_section(camera);
let culling = !frame.title && frame.cave_culling;
if culling {
crate::spans::time("occlusion", || self.start_occlusion(camera, frame.distance));
crate::spans::time("occlusion", || {
self.start_occlusion(camera, frame.detail_distance)
});
}
let reachable = &self.occlusion;
let mut visible = std::mem::take(&mut self.visible);
@@ -2616,15 +2663,27 @@ impl Renderer {
crate::spans::time("visibility", || {
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,
@@ -2638,7 +2697,8 @@ impl Renderer {
self.origins.extend(
visible
.iter()
.map(|v| [v.origin[0], v.origin[1], v.origin[2], 0.0]),
.map(|v| section_instance(v.origin))
.chain(visible_lods.iter().map(|v| lod::instance(v.origin))),
);
if !self.origins.is_empty() {
self.uploads.buffer(
@@ -2648,9 +2708,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();
@@ -2697,6 +2754,16 @@ impl Renderer {
None => {}
}
}
// Distant terrain tiles are never occlusion tested: phase 1.
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 phase1_draws = self.draws.len() as u32;
self.draws.extend_from_slice(&self.phase2_draws);
self.draw_pages.extend_from_slice(&self.phase2_pages);
@@ -2704,7 +2771,8 @@ impl Renderer {
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() {
@@ -2732,16 +2800,67 @@ 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)),
));
}
}
// Sections went back to front (`visible` is sorted by distance, then
// key): tiles' water only needs sorting in when there is some.
let section_translucent = self.translucent_draws.len();
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.
if self.translucent_draws.len() > section_translucent {
self.translucent_draws
.sort_unstable_by(|a, b| b.0.cmp(&a.0).then(b.1.cmp(&a.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;
self.section_draws = self.draws.len();
@@ -2928,6 +3047,7 @@ impl Renderer {
if self.flat_on && !self.draws.is_empty() && self.draws.len() >= flat.min_draws =>
{
let sections = &self.sections;
let lods = &self.lods;
let sorted = &self.sorted_indices;
crate::spans::time("flatten", || {
flat.prepare(
@@ -2939,7 +3059,14 @@ impl Renderer {
solid_draws as usize,
visible
.iter()
.filter_map(|v| sections.get(v.slot).flat_id.map(|id| (id, v.origin))),
.filter_map(|v| {
let id = sections.get(v.slot).flat_id?;
Some((id, section_instance(v.origin)))
})
.chain(visible_lods.iter().filter_map(|v| {
let id = lods[&v.key].mesh.flat_id?;
Some((id, lod::instance(v.origin)))
})),
|page| sorted.buffer(page).clone(),
)
})
@@ -3453,6 +3580,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)
}
}
+2 -2
View File
@@ -330,7 +330,7 @@ impl Flat {
draws: &[[u32; 5]],
pages: &[(u32, Option<u32>)],
solid_draws: usize,
origins: impl Iterator<Item = (u32, [f32; 3])>,
origins: impl Iterator<Item = (u32, [f32; 4])>,
sorted_page: impl Fn(u32) -> wgpu::Buffer,
) -> bool {
// Origins by section id.
@@ -340,7 +340,7 @@ impl Flat {
if self.origin_data.len() <= id {
self.origin_data.resize(id + 1, [0.0; 4]);
}
self.origin_data[id] = [o[0], o[1], o[2], 0.0];
self.origin_data[id] = o;
}
if self.origin_data.len() as u64 > self.origin_capacity {
self.origin_capacity = (self.origin_data.len() as u64).next_power_of_two();
+146
View File
@@ -0,0 +1,146 @@
//! 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<Self> {
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<u64> {
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())
}
}
/// A tile's instance: like a section's, with the UV step of merged faces.
pub(super) fn instance(origin: [f32; 3]) -> [f32; 4] {
[
origin[0],
origin[1],
origin[2],
1.0 / crate::mesher::TILED_UV_STEPS,
]
}
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<Visible> {
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::tiled(pos, [0.0; 2], 0, 0, 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());
}
}
@@ -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
);
}
}
+20 -1
View File
@@ -117,6 +117,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.
@@ -154,6 +156,7 @@ impl Default for Graphics {
fn default() -> Self {
Self {
preset: Preset::Custom,
lod_distance: 0,
cave_culling: true,
cull_leaves: false,
entity_culling: false,
@@ -199,6 +202,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),
@@ -236,8 +240,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),
@@ -337,6 +342,7 @@ mod tests {
fn round_trips() {
let g = Graphics {
preset: Preset::Balanced,
lod_distance: 256,
cave_culling: false,
cull_leaves: true,
entity_culling: true,
@@ -364,6 +370,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 automatic_workers_leave_cores_for_the_render_and_server_threads() {
let g = Graphics::default();
+54 -2
View File
@@ -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<Row> {
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<Row> {
"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();
+1
View File
@@ -102,6 +102,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;
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
+14
View File
@@ -64,6 +64,9 @@ pub struct TerrainVertex {
}
/// Fixed-point steps per unit of `TerrainVertex` UVs, which span `[0, 2)`.
pub const UV_STEPS: f32 = 32768.0;
/// Fixed-point steps per block of distant-terrain UVs (`TerrainVertex::tiled`),
/// which span `[0, 2048)`: merged faces repeat their sprite once per block.
pub const TILED_UV_STEPS: f32 = 32.0;
impl TerrainVertex {
pub fn new(pos: [f32; 3], uv: [f32; 2], layer: u16, color: u32, light: u16) -> Self {
Self {
@@ -74,6 +77,17 @@ impl TerrainVertex {
color,
}
}
/// A vertex of a merged distant-terrain face, its UV in blocks from the
/// face's corner. Drawn with an instance UV scale of `1 / TILED_UV_STEPS`.
pub fn tiled(pos: [f32; 3], uv: [f32; 2], layer: u16, color: u32, light: u16) -> Self {
Self {
pos,
uv: uv.map(|v| (v * TILED_UV_STEPS).round().clamp(0.0, f32::from(u16::MAX)) as u16),
layer,
light,
color,
}
}
/// The UV the GPU samples.
pub fn uv(&self) -> [f32; 2] {
self.uv.map(|v| f32::from(v) / UV_STEPS)
@@ -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<TerrainVertex>) -> 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 {
+559 -1
View File
@@ -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},
registry::{BlockRegistry, BlockStateId},
@@ -2833,11 +2833,362 @@ pub struct Persistent<G> {
pub registry: Arc<crate::assets::Assets>,
pub biomes: Arc<Biomes>,
}
type LodSurfaces = Arc<[crate::lod::Column; 256]>;
struct SavedLodSampler<G: ChunkGenerator> {
generator: Arc<Persistent<G>>,
inner: Box<dyn crate::streaming::LodSampler>,
cache: std::collections::VecDeque<((i32, i32), Option<LodSurfaces>)>,
details: Vec<crate::streaming::LodDetail>,
}
impl<G: ChunkGenerator> SavedLodSampler<G> {
fn saved_columns(&mut self, key: (i32, i32)) -> Option<LodSurfaces> {
let cached = match self.cache.iter().position(|(at, _)| *at == key) {
Some(index) => self.cache.remove(index).unwrap(),
None => (
key,
self.generator.save.lod_surfaces(
key.0,
key.1,
&self.generator.registry,
&self.generator.biomes,
),
),
};
let columns = cached.1.clone();
self.cache.push_front(cached);
self.cache.truncate(128);
columns
}
}
impl<G: ChunkGenerator> crate::streaming::LodSampler for SavedLodSampler<G> {
fn fork(&self) -> Option<Box<dyn crate::streaming::LodSampler>> {
Some(Box::new(Self {
generator: self.generator.clone(),
inner: self.inner.fork()?,
cache: Default::default(),
details: Vec::new(),
}))
}
fn request_detail(&mut self, chunks: &[(i32, i32)], center: [i32; 2]) {
let mut missing = Vec::new();
for &key in chunks {
if let Some(columns) = self.saved_columns(key) {
self.details.push((key, columns));
} else {
missing.push(key);
}
}
self.inner.request_detail(&missing, center);
}
fn drain_details(&mut self) -> Vec<crate::streaming::LodDetail> {
let mut details = std::mem::take(&mut self.details);
for (key, generated) in self.inner.drain_details() {
// A generation batch also returns neighbouring, unrequested chunks.
// Their saved builds must take precedence too.
details.push((key, self.saved_columns(key).unwrap_or(generated)));
}
details
}
fn grid(&mut self, x: i32, z: i32, stride: i32, side: usize) -> Vec<crate::lod::Column> {
let mut columns = self.inner.grid(x, z, stride, side);
if stride <= 16 {
for (index, column) in columns.iter_mut().enumerate() {
let (wx, wz) = (
x + (index % side) as i32 * stride,
z + (index / side) as i32 * stride,
);
if let Some(saved) = self.saved_columns((wx >> 4, wz >> 4)) {
*column = crate::lod::Column::reduced(
&saved,
(wx & 15) as usize,
(wz & 15) as usize,
stride as usize,
);
}
}
}
columns
}
fn sample(&mut self, x: i32, z: i32) -> Option<crate::lod::Surface> {
self.column(x, z).surface
}
fn column(&mut self, x: i32, z: i32) -> crate::lod::Column {
self.reduced_column(x, z, 1)
}
fn reduced_column(&mut self, x: i32, z: i32, width: i32) -> crate::lod::Column {
if width <= 16
&& let Some(columns) = self.saved_columns((x >> 4, z >> 4))
{
return crate::lod::Column::reduced(
&columns,
(x & 15) as usize,
(z & 15) as usize,
width as usize,
);
}
self.inner.reduced_column(x, z, width)
}
}
/// Read one palette index without expanding the other entries in the section.
fn lod_palette_index(
container: &Tag,
index: usize,
entries: usize,
minimum_bits: u32,
) -> Option<usize> {
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 Some(0);
}
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;
(value < palette.len()).then_some(value)
}
fn lod_light_layers(sections: &[(i32, &Tag)], sky_light: bool) -> Box<[u8]> {
let mut layers = [(None, None); SECTIONS];
for &(y, section) in sections {
let layer = |name: &str| match section.get(name) {
Some(Tag::ByteArray(data)) if data.len() == 2048 => Some(&data[..]),
_ => None,
};
layers[(y - (MIN_Y >> 4)) as usize] = (
if sky_light { layer("SkyLight") } else { None },
layer("BlockLight"),
);
}
let mut light = vec![0u8; SECTIONS * 4096].into_boxed_slice();
let mut above = [if sky_light { 15u8 } else { 0 }; 256];
let nibble = |data: &[u8], index: usize| (data[index / 2] >> ((index & 1) * 4)) & 15;
for si in (0..SECTIONS).rev() {
let (sky, block) = layers[si];
let section = &mut light[si * 4096..(si + 1) * 4096];
for (index, value) in section.iter_mut().enumerate() {
*value = sky.map_or(above[index & 255], |data| nibble(data, index)) << 4
| block.map_or(0, |data| nibble(data, index));
}
// Missing sky layers inherit the bottom layer above, as load_light does.
for (index, value) in above.iter_mut().enumerate() {
*value = section[index] >> 4;
}
}
light
}
fn lod_face_light(light: &[u8], x: usize, y: usize, z: usize) -> u16 {
let index = y * 256 + z * 16 + x;
let mut sky = light[index] >> 4;
let mut block = light[index] & 15;
for (valid, index) in [
(x > 0, index.wrapping_sub(1)),
(x < 15, index + 1),
(z > 0, index.wrapping_sub(16)),
(z < 15, index + 16),
(y > 0, index.wrapping_sub(256)),
(y + 1 < SECTIONS * 16, index + 256),
] {
if valid {
sky = sky.max(light[index] >> 4);
block = block.max(light[index] & 15);
}
}
u16::from(sky) << 4 | u16::from(block) << 12
}
fn decode_lod_surfaces(
root: &NamedTag,
assets: &crate::assets::Assets,
biomes: &Biomes,
sky_light: bool,
) -> Option<LodSurfaces> {
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 columns: Box<[crate::lod::Column; 256]> =
Box::new(std::array::from_fn(|_| crate::lod::Column {
surface: None,
spans: Vec::new(),
}));
let mut water = [None; 256];
let light = lod_light_layers(&sections, sky_light);
let mut states = BTreeMap::new();
for (section_y, section) in sections {
if let Some(blocks) = section.get("block_states") {
let Tag::List(palette) = blocks.get("palette")? else {
return None;
};
// Resolve each state once per palette, rather than parsing NBT for every block.
let palette: Vec<_> = palette
.iter()
.map(|entry| {
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))
});
Some(state.map(|state| {
let properties = assets.properties.heightmap_state(state);
let fluid = matches!(
descriptor.name.as_str(),
"minecraft:water" | "minecraft:lava"
);
(state, fluid, properties.blocks_motion_in_heightmap())
}))
})
.collect::<Option<_>>()?;
let biome_palette: Vec<_> = section
.get("biomes")
.and_then(|container| match container.get("palette")? {
Tag::List(palette) => Some(palette),
_ => None,
})
.into_iter()
.flatten()
.map(|entry| match entry {
Tag::String(name) => biomes.id(&name.to_string_lossy()),
_ => 0,
})
.collect();
for z in 0..16 {
for x in 0..16 {
let cell = z * 16 + x;
let column = &mut columns[cell];
for y in (0..16).rev() {
let index = y * 256 + cell;
let palette_index = lod_palette_index(blocks, index, 4096, 4)?;
let Some((state, fluid, solid)) = palette[palette_index] else {
continue;
};
if !solid && !fluid {
continue;
}
let biome = section
.get("biomes")
.and_then(|container| {
lod_palette_index(
container,
(y / 4) * 16 + (z / 4) * 4 + x / 4,
64,
1,
)
})
.and_then(|index| biome_palette.get(index))
.copied()
.unwrap_or(0);
let height = section_y * 16 + y as i32 + 1;
if column.surface.is_none() {
if fluid {
water[cell].get_or_insert((height, state));
} else {
column.surface = Some(crate::lod::Surface {
height,
state,
water: water[cell],
biome,
});
}
}
let light = lod_face_light(&light, x, (height - 1 - MIN_Y) as usize, z);
let min = (height - 1) as i16;
let max = height as i16;
if let Some(previous) = column.spans.last_mut()
&& previous.min == max
&& (previous.state, previous.biome, previous.light)
== (state, biome, light)
{
previous.min = min;
} else {
column.spans.push(crate::lod::Span {
min,
max,
state,
biome,
light,
});
}
}
}
}
}
}
for column in &mut *columns {
column.spans.reverse();
}
Some(Arc::from(columns))
}
impl SaveDir {
fn lod_surfaces(
&self,
x: i32,
z: i32,
assets: &crate::assets::Assets,
biomes: &Biomes,
) -> Option<LodSurfaces> {
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()?
};
// Proto saves may still be missing decoration from their neighbors.
if text(root.tag.get("Status"))
.is_some_and(|status| status != "minecraft:full" && status != "full")
{
return None;
}
decode_lod_surfaces(&root, assets, biomes, self.sky_light)
}
}
impl<G: ChunkGenerator> ChunkGenerator for Persistent<G> {
type Worker = G::Worker;
fn worker(&self) -> G::Worker {
self.inner.worker()
}
fn lod_sampler(self: Arc<Self>) -> Box<dyn crate::streaming::LodSampler> {
let inner = ChunkGenerator::lod_sampler(self.inner.clone());
Box::new(SavedLodSampler {
generator: self,
inner,
cache: Default::default(),
details: Vec::new(),
})
}
fn load(&self, x: i32, z: i32) -> Option<Chunk> {
self.save
.tables
@@ -2849,6 +3200,10 @@ impl<G: ChunkGenerator> ChunkGenerator for Persistent<G> {
chunk.entities = self.save.load_entities(x, z);
Some(chunk)
}
fn lod_surface(&self, worker: &mut G::Worker, x: i32, z: i32) -> Option<crate::lod::Surface> {
// 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);
}
@@ -2907,6 +3262,209 @@ impl<G: ChunkGenerator> ChunkGenerator for Persistent<G> {
#[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 dirt = assets.registry.default_state("minecraft:dirt").unwrap();
let leaves = assets
.registry
.default_state("minecraft:oak_leaves")
.unwrap();
let mut chunk = Chunk::empty(-2, 3);
chunk.set(0, 109, 0, stone.0);
chunk.set(3, 121, 5, stone.0);
chunk.set(3, 122, 5, stone.0);
chunk.set(3, 120, 5, dirt.0);
chunk.set(3, 119, 5, dirt.0);
chunk.set(3, 115, 5, stone.0);
chunk.set(11, 125, 11, stone.0);
chunk.set(14, 127, 14, stone.0);
chunk.set(14, 112, 14, stone.0);
for y in 95..98 {
chunk.set(2, y, 6, stone.0);
}
chunk.lit = true;
let mut cave_light = Box::new([0x03; 4096]);
cave_light[crate::chunk::index(12, 125, 11)] = 0xa7;
Arc::make_mut(&mut chunk.sections[((121 - MIN_Y) >> 4) as usize]).light =
Light::Full(cave_light);
chunk.set(7, 90, 9, sand.0);
chunk.set(7, 98, 9, leaves.0);
let forest = biomes.id("minecraft:forest");
chunk.set_biome_cell(7 / 4, (98 - MIN_Y) / 4, 9 / 4, forest);
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].surface.unwrap().height, 110);
assert_eq!(surfaces[0].surface.unwrap().state, stone);
let narrow_build = surfaces[5 * 16 + 3].surface.unwrap();
assert_eq!((narrow_build.height, narrow_build.state), (123, stone));
assert!(
surfaces[5 * 16 + 2].surface.is_none(),
"adjacent columns stay distinct"
);
let strata = &surfaces[5 * 16 + 3].spans;
assert_eq!(
strata
.iter()
.map(|span| (span.min, span.max, span.state))
.collect::<Vec<_>>(),
vec![(115, 116, stone), (119, 121, dirt), (121, 123, stone)]
);
assert!(
strata.iter().all(|span| span.light == 0x3000),
"saved cave darkness and block light survive"
);
assert_eq!(
surfaces[11 * 16 + 11].spans[0].light,
0x70a0,
"neighbor face light uses separate sky/block channels"
);
let boundaries = &surfaces[14 * 16 + 14].spans;
assert_eq!(boundaries.len(), 2);
assert!(
boundaries.iter().all(|span| span.light == 0x30f0),
"face light crosses section boundaries above and below"
);
let across_sections = &surfaces[6 * 16 + 2].spans;
assert_eq!(across_sections.len(), 1);
assert_eq!((across_sections[0].min, across_sections[0].max), (95, 98));
let canopy = surfaces[9 * 16 + 7].surface.unwrap();
assert_eq!(
(canopy.height, canopy.state, canopy.biome),
(99, leaves, forest)
);
let underwater = surfaces[4 * 16 + 4].surface.unwrap();
assert_eq!((underwater.height, underwater.state), (55, sand));
assert_eq!(underwater.water, Some((64, water)));
assert!(surfaces[15].surface.is_none(), "saved void stays empty");
assert!(
reopened.regions.lock().unwrap().is_empty(),
"far reads do not grow gameplay region cache"
);
let mut proto = encode(&chunk, assets.registry, &biomes);
put(
&mut proto.tag,
"Status",
string(ChunkStatus::Terrain.name()),
);
put(&mut proto.tag, "xPos", Tag::Int(-3));
save.write_records("region", vec![((-3, 3), Some(proto))], true)
.unwrap();
assert!(
reopened.lod_surfaces(-3, 3, &assets, &biomes).is_none(),
"unfinished proto saves must not suppress complete decoration"
);
let mut nether = SaveDir::new(dir.clone());
nether.sky_light = false;
let dark = nether.lod_surfaces(-2, 3, &assets, &biomes).unwrap();
assert!(
dark.iter()
.flat_map(|c| &c.spans)
.all(|s| s.light & 0xf0 == 0),
"no invented skylight in Nether saves"
);
struct SavedOnly(Vec<crate::streaming::LodDetail>);
impl crate::streaming::LodSampler for SavedOnly {
fn sample(&mut self, _: i32, _: i32) -> Option<crate::lod::Surface> {
panic!("saved data must win")
}
fn request_detail(&mut self, chunks: &[(i32, i32)], _: [i32; 2]) {
assert!(
chunks.is_empty(),
"saved builds must not request speculative generation"
);
}
fn drain_details(&mut self) -> Vec<crate::streaming::LodDetail> {
std::mem::take(&mut self.0)
}
}
let inner = Arc::new(crate::testgen::TestTerrain::new(assets.registry, &biomes).unwrap());
let generator = Arc::new(Persistent {
inner,
save: Arc::new(reopened),
registry: Arc::new(assets),
biomes: Arc::new(biomes),
});
let mut sampler = SavedLodSampler {
generator,
inner: Box::new(SavedOnly(Vec::new())),
cache: Default::default(),
details: Vec::new(),
};
use crate::streaming::LodSampler;
sampler.request_detail(&[(-2, 3)], [-32, 48]);
let details = sampler.drain_details();
assert_eq!(details.len(), 1);
assert_eq!(&details[0].1[..], &surfaces[..]);
sampler.cache.clear();
let generated: LodSurfaces =
Arc::new(std::array::from_fn(|_| crate::lod::Column::default()));
sampler.inner = Box::new(SavedOnly(vec![
((-2, 3), generated.clone()),
((-9, 9), generated.clone()),
]));
let details = sampler.drain_details();
assert_eq!(
&details[0].1[..],
&surfaces[..],
"unrequested batch neighbours must preserve saved builds"
);
assert!(Arc::ptr_eq(&details[1].1, &generated));
sampler.inner =
crate::streaming::ChunkGenerator::lod_sampler(sampler.generator.inner.clone());
let grid = sampler.grid(-32, 48, 2, 16);
for z in 0..8 {
for x in 0..8 {
assert_eq!(
grid[z * 16 + x],
crate::lod::Column::reduced(&surfaces, x * 2, z * 2, 2),
"saved builds must override a batched direct grid"
);
}
}
drop((save, sampler, nether));
fs::remove_dir_all(dir).unwrap();
}
#[test]
fn saved_far_surface_rejects_invalid_packed_palettes() {
let mut states = Tag::compound([
("palette", Tag::List(vec![Tag::Byte(0), Tag::Byte(1)])),
("data", Tag::LongArray(vec![0; 256])),
]);
assert_eq!(lod_palette_index(&states, 4095, 4096, 4), Some(0));
assert_eq!(lod_palette_index(&states, 4096, 4096, 4), None);
put(&mut states, "data", Tag::LongArray(vec![15; 256]));
assert_eq!(lod_palette_index(&states, 0, 4096, 4), None);
put(&mut states, "data", Tag::LongArray(vec![0; 255]));
assert_eq!(lod_palette_index(&states, 0, 4096, 4), None);
}
#[test]
fn save_batch_preserves_order_coordinates_and_ownership() {
use crate::containers::BlockEntity;
+74 -1
View File
@@ -32,6 +32,25 @@ pub type Proto = Box<dyn std::any::Any + Send>;
pub trait ChunkGenerator: Send + Sync + 'static {
type Worker: Send + 'static;
fn worker(&self) -> Self::Worker;
fn lod_sampler(self: Arc<Self>) -> Box<dyn LodSampler>
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<crate::lod::Surface> {
None
}
/// A previously saved final chunk, bypassing generation.
fn load(&self, _x: i32, _z: i32) -> Option<Chunk> {
None
@@ -63,8 +82,58 @@ pub trait ChunkGenerator: Send + Sync + 'static {
/// neighbours' decoration is done, so it can't be generated again.
fn save_finished(&self, _chunk: Chunk) {}
}
pub type LodDetail = ((i32, i32), Arc<[crate::lod::Column; 256]>);
/// Independent, thread-owned far-terrain sampling state.
pub trait LodSampler: Send {
/// Independent sampling state; native detail generation stays with the owner.
fn fork(&self) -> Option<Box<dyn LodSampler>> {
None
}
fn request_detail(&mut self, _chunks: &[(i32, i32)], _center: [i32; 2]) {}
fn drain_details(&mut self) -> Vec<LodDetail> {
Vec::new()
}
fn sample(&mut self, x: i32, z: i32) -> Option<crate::lod::Surface>;
fn column(&mut self, x: i32, z: i32) -> crate::lod::Column {
crate::lod::Column::from_surface(self.sample(x, z))
}
fn reduced_column(&mut self, x: i32, z: i32, _width: i32) -> crate::lod::Column {
self.column(x, z)
}
fn grid(&mut self, x: i32, z: i32, stride: i32, side: usize) -> Vec<crate::lod::Column> {
let mut columns = Vec::with_capacity(side * side);
for dz in 0..side {
for dx in 0..side {
columns.push(self.reduced_column(
x + dx as i32 * stride,
z + dz as i32 * stride,
stride,
));
}
}
columns
}
}
pub trait LodSource: Send + Sync {
fn sampler(self: Arc<Self>) -> Box<dyn LodSampler>;
}
struct GeneratorSampler<G: ChunkGenerator> {
generator: Arc<G>,
worker: G::Worker,
}
impl<G: ChunkGenerator> LodSampler for GeneratorSampler<G> {
fn sample(&mut self, x: i32, z: i32) -> Option<crate::lod::Surface> {
self.generator.lod_surface(&mut self.worker, x, z)
}
}
impl<G: ChunkGenerator> LodSource for G {
fn sampler(self: Arc<Self>) -> Box<dyn LodSampler> {
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);
}
@@ -490,6 +559,10 @@ impl Streaming {
mesh: true,
}
}
/// Shares generation resources, but gives distant work its own worker state.
pub fn lod_source(&self) -> Arc<dyn LodSource> {
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.
+8 -6
View File
@@ -72,16 +72,18 @@ fn sample_lightmap(sky: f32, block: f32) -> vec4<f32> {
return textureSampleLevel(lightmap, lightmap_sampler,
clamp(uv / 256.0 + 0.5 / 16.0, vec2(0.5 / 16.0), vec2(15.5 / 16.0)), 0.0);
}
// mesher.rs's TerrainVertex holds the UV in 32768 steps per unit: a power of
// two on an integer below 2^24, so it decodes exactly.
fn terrain_uv(packed: vec2<u32>) -> vec2<f32> {
return vec2<f32>(packed) * (1.0 / 32768.0);
// mesher.rs's TerrainVertex holds the UV in fixed point; the instance's
// origin.w is the step: 1/32768 per unit for sections (a power of two on an
// integer below 2^24, so it decodes exactly), 1/32 per block for distant
// terrain tiles (TerrainVertex::tiled).
fn terrain_uv(packed: vec2<u32>, step: f32) -> vec2<f32> {
return vec2<f32>(packed) * step;
}
@vertex fn vs_terrain(
@location(0) position: vec3<f32>, @location(1) uv: vec2<u32>,
@location(2) layer: u32, @location(3) color: vec4<f32>, @location(4) light: vec2<u32>,
@location(5) origin: vec4<f32>) -> TerrainOut {
return terrain_vertex(position, terrain_uv(uv), layer, color, light, origin.xyz);
return terrain_vertex(position, terrain_uv(uv, origin.w), layer, color, light, origin.xyz);
}
// Flat section draws (gfx/flat.rs): one draw per arena page over the frame's
// index list. A section's vertices fill whole 256-vertex blocks of its page:
@@ -94,7 +96,7 @@ fn terrain_uv(packed: vec2<u32>) -> vec2<f32> {
@location(2) layer: u32, @location(3) color: vec4<f32>, @location(4) light: vec2<u32>
) -> TerrainOut {
let origin = slot_origins[block_slots[vertex >> 8u]];
return terrain_vertex(position, terrain_uv(uv), layer, color, light, origin.xyz);
return terrain_vertex(position, terrain_uv(uv, origin.w), layer, color, light, origin.xyz);
}
fn terrain_vertex(position: vec3<f32>, uv: vec2<f32>, layer: u32, color: vec4<f32>,
light: vec2<u32>, origin: vec3<f32>) -> TerrainOut {
+48
View File
@@ -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<crate::lod::Surface> {
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");
}
}
+33 -2
View File
@@ -4,7 +4,7 @@ use crate::{
biome::Biomes,
chunk::{BIOME_STRATEGY, Chunk, HEIGHT, MIN_Y, SECTIONS, Section},
save::SavedProto,
streaming::{ChunkGenerator, Proto},
streaming::{ChunkGenerator, LodSampler, Proto},
};
use horizon_core::{
paletted::PalettedContainer,
@@ -16,11 +16,15 @@ use horizon_world::{
nbt::Tag,
registry::BlockRegistry,
worldgen::{
generator::{GeneratorData, GeneratorWorker, OverworldGenerator, ProtoChunk},
generator::{
GeneratorData, GeneratorWorker, OverworldGenerator, ProtoChunk, SurfaceSample,
},
terrain::PreLightChunkStage,
},
};
use std::{path::Path, sync::Arc};
mod lod;
use lod::VanillaLodSampler;
pub struct VanillaTerrain {
generator: OverworldGenerator,
@@ -35,6 +39,8 @@ pub struct VanillaTerrain {
tick_blocks: Vec<u32>,
/// Block index -> default state id: `tick_blocks` the other way.
tick_states: Vec<u32>,
seed: i64,
dimension: String,
}
/// A proto chunk, or the position of one whose generation failed.
enum VanillaProto {
@@ -42,6 +48,18 @@ enum VanillaProto {
Failed(i32, i32),
}
impl VanillaTerrain {
fn client_surface(&self, surface: SurfaceSample) -> crate::lod::Surface {
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),
}
}
pub fn new(
dir: &Path,
registry: &'static BlockRegistry,
@@ -119,6 +137,8 @@ impl VanillaTerrain {
query: std::sync::Mutex::new(None),
tick_blocks,
tick_states,
seed,
dimension: dimension.to_owned(),
})
}
/// A proto chunk's blocks, biomes, scheduled ticks and post-processing
@@ -278,6 +298,17 @@ impl ChunkGenerator for VanillaTerrain {
fn worker(&self) -> GeneratorWorker {
self.generator.worker()
}
fn lod_sampler(self: Arc<Self>) -> Box<dyn LodSampler> {
Box::new(VanillaLodSampler::new(self))
}
fn lod_surface(
&self,
worker: &mut GeneratorWorker,
x: i32,
z: i32,
) -> Option<crate::lod::Surface> {
Some(self.client_surface(worker.surface_sample(x, z)?))
}
/// 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
+700
View File
@@ -0,0 +1,700 @@
//! Speculative decoration is isolated from gameplay and never writes normal saves.
use super::*;
use crate::{
fast_hash::{FxHashMap, FxHashSet},
lod::{Column, Span, Surface},
streaming::LodDetail,
};
use horizon_world::{
heightmap::HeightmapState,
registry::BlockStateId,
worldgen::{decoration_order::vanilla_order, generator::GeneratorError},
};
use std::{
collections::VecDeque,
sync::{
Condvar, Mutex,
atomic::{AtomicBool, Ordering},
mpsc,
},
thread,
};
mod vegetation;
use vegetation::Vegetation;
const CACHE: usize = 1024;
const REQUESTS: usize = 64;
const BATCH: i32 = 4;
fn batch_origin((x, z): (i32, i32)) -> (i32, i32) {
(x.div_euclid(BATCH) * BATCH, z.div_euclid(BATCH) * BATCH)
}
#[derive(Default)]
struct Requests {
chunks: FxHashSet<(i32, i32)>,
center: [i32; 2],
current: Option<(i32, i32)>,
finished: FxHashSet<(i32, i32)>,
}
struct Shared {
requests: Mutex<Requests>,
wake: Condvar,
stop: AtomicBool,
}
fn distance((x, z): (i32, i32), center: [i32; 2]) -> i64 {
let dx = i64::from(x * 16 + 8 - center[0]);
let dz = i64::from(z * 16 + 8 - center[1]);
dx * dx + dz * dz
}
struct Builder {
shared: Arc<Shared>,
results: mpsc::Receiver<LodDetail>,
thread: Option<thread::JoinHandle<()>>,
}
impl Builder {
fn new(terrain: Arc<VanillaTerrain>) -> Self {
let shared = Arc::new(Shared {
requests: Mutex::new(Requests::default()),
wake: Condvar::new(),
stop: AtomicBool::new(false),
});
let worker = shared.clone();
let (tx, results) = mpsc::channel();
let thread = thread::Builder::new()
.name("Far decoration".into())
.spawn(move || {
// Structure pieces have mutable one-shot placement flags. Sharing
// gameplay's seeded pipeline would consume its chests/spawners.
let generator = match OverworldGenerator::for_dimension(
terrain.generator.data().clone(),
terrain.seed,
"minecraft:normal",
&terrain.dimension,
) {
Ok(generator) => generator,
Err(error) => {
log::warn!("Horizon: far decoration unavailable: {error}");
worker.stop.store(true, Ordering::Relaxed);
return;
}
};
let mut state = generator.worker();
loop {
let key = {
let mut requests = worker.requests.lock().unwrap();
while requests.chunks.is_empty() && !worker.stop.load(Ordering::Relaxed) {
requests = worker.wake.wait(requests).unwrap();
}
if worker.stop.load(Ordering::Relaxed) {
break;
}
let key = *requests
.chunks
.iter()
.min_by_key(|&&key| (distance(key, requests.center), key))
.unwrap();
let origin = batch_origin(key);
requests.chunks.retain(|&key| batch_origin(key) != origin);
requests.current = Some(origin);
origin
};
let result = decorated_batch(&mut state, key, BATCH, &worker.stop, |chunk| {
let pos = chunk.position();
let key = (pos.x, pos.z);
worker.requests.lock().unwrap().finished.insert(key);
let columns = columns(chunk, &terrain.biome_map, &terrain.dimension);
if tx.send((key, columns)).is_err() {
worker.stop.store(true, Ordering::Relaxed);
}
});
worker.requests.lock().unwrap().current = None;
match result {
Ok(_) => {}
Err(error) => {
log::warn!(
"Horizon: far decoration failed at {} {}: {error}",
key.0,
key.1
);
worker.stop.store(true, Ordering::Relaxed);
break;
}
}
}
})
.expect("far decoration worker");
Self {
shared,
results,
thread: Some(thread),
}
}
fn request(&self, chunks: &[(i32, i32)], center: [i32; 2]) {
if self.shared.stop.load(Ordering::Relaxed) {
return;
}
let mut requests = self.shared.requests.lock().unwrap();
requests.center = center;
for &key in chunks {
if requests.current != Some(batch_origin(key)) && !requests.finished.contains(&key) {
requests.chunks.insert(key);
}
}
if requests.chunks.len() > REQUESTS {
let mut sorted: Vec<_> = requests.chunks.iter().copied().collect();
sorted.sort_unstable_by_key(|&key| (distance(key, center), key));
requests.chunks = sorted.into_iter().take(REQUESTS).collect();
}
self.shared.wake.notify_one();
}
}
impl Drop for Builder {
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();
}
}
}
/// Share the two-chunk terrain halo and one-chunk feature halo across adjacent
/// targets. Emit each target only after all nine feature centers have run.
/// These proto chunks never enter World, lighting or normal save queues.
fn decorated_batch(
worker: &mut GeneratorWorker,
origin: (i32, i32),
size: i32,
stop: &AtomicBool,
mut emit: impl FnMut(&ProtoChunk),
) -> Result<FxHashMap<(i32, i32), ProtoChunk>, GeneratorError> {
// Each region starts afresh; flags from a previous speculative structure
// placement must not suppress blocks in this region.
worker.forget_structure_starts();
let mut chunks = FxHashMap::default();
for dx in -2..size + 2 {
for dz in -2..size + 2 {
if stop.load(Ordering::Relaxed) {
return Ok(FxHashMap::default());
}
let key = (origin.0 + dx, origin.1 + dz);
chunks.insert(key, worker.generate_proto(ChunkPos::new(key.0, key.1))?);
}
}
let mut remaining: FxHashMap<_, u8> = (0..size)
.flat_map(|x| (0..size).map(move |z| ((origin.0 + x, origin.1 + z), 9)))
.collect();
let center = (origin.0 + size / 2, origin.1 + size / 2);
for (x, z) in vanilla_order(center, size / 2)
.into_iter()
.filter(|&(x, z)| {
x >= origin.0 - 1 && x <= origin.0 + size && z >= origin.1 - 1 && z <= origin.1 + size
})
{
if stop.load(Ordering::Relaxed) {
return Ok(FxHashMap::default());
}
let mut region: [ProtoChunk; 9] = std::array::from_fn(|i| {
chunks
.remove(&(x + i as i32 % 3 - 1, z + i as i32 / 3 - 1))
.expect("generated neighborhood")
});
worker.decorate(&mut region)?;
for chunk in region {
let pos = chunk.position();
chunks.insert((pos.x, pos.z), chunk);
}
for dx in -1..=1 {
for dz in -1..=1 {
let key = (x + dx, z + dz);
if let Some(left) = remaining.get_mut(&key) {
*left -= 1;
if *left == 0 {
emit(&chunks[&key]);
}
}
}
}
}
assert!(
remaining.values().all(|&left| left == 0),
"incomplete far decoration halo"
);
Ok(chunks)
}
#[cfg(test)]
fn decorated_region(
worker: &mut GeneratorWorker,
center: (i32, i32),
stop: &AtomicBool,
) -> Result<Option<ProtoChunk>, GeneratorError> {
Ok(decorated_batch(worker, center, 1, stop, |_| {})?.remove(&center))
}
fn columns(chunk: &ProtoChunk, biome_map: &[u16], dimension: &str) -> Arc<[Column; 256]> {
let states = chunk.data().block_states();
let registry = chunk.data().registry();
let min = chunk.terrain.min_y().max(MIN_Y);
let max = (chunk.terrain.min_y() + chunk.terrain.height()).min(MIN_Y + HEIGHT);
Arc::new(std::array::from_fn(|index| {
let x = (index & 15) as i32;
let z = (index >> 4) as i32;
let mut column = Column::default();
for y in min..max {
let state = chunk.block(x, y, z);
let properties = states.get(state);
if properties.is_air()
|| (!properties.blocks_motion_in_heightmap() && !properties.has_fluid())
{
continue;
}
let name = registry.state(state).map_or("", |s| s.name.as_str());
let fluid = matches!(name, "minecraft:water" | "minecraft:lava");
let biome = biome_map
.get(chunk.biome_id_at(x, y, z).0 as usize)
.copied()
.unwrap_or(0);
// shortcut: no propagated far lighting, use the normal lighting
// engine if accurate distant block-light propagation is required.
let light = if dimension == "minecraft:the_nether" {
0
} else {
240
} | if name == "minecraft:lava" {
15 << 12
} else {
0
};
column.push(Span {
min: y as i16,
max: (y + 1) as i16,
state,
biome,
light,
});
if fluid {
let surface = column.surface.get_or_insert(Surface {
height: MIN_Y,
state: BlockStateId(0),
water: None,
biome,
});
surface.water = Some((y + 1, state));
} else {
column.surface = Some(Surface {
height: y + 1,
state,
water: None,
biome,
});
}
}
column
}))
}
pub(super) struct VanillaLodSampler {
generator: Arc<VanillaTerrain>,
worker: GeneratorWorker,
builder: Option<Builder>,
vegetation: Arc<Vegetation>,
cache: FxHashMap<(i32, i32), Arc<[Column; 256]>>,
order: VecDeque<(i32, i32)>,
details: Vec<LodDetail>,
}
impl VanillaLodSampler {
pub(super) fn new(generator: Arc<VanillaTerrain>) -> Self {
let worker = generator.generator.worker();
let builder = Some(Builder::new(generator.clone()));
let vegetation = Arc::new(Vegetation::new(
generator.generator.data(),
&generator.biome_map,
generator.seed,
));
Self {
generator,
worker,
builder,
vegetation,
cache: FxHashMap::default(),
order: VecDeque::new(),
details: Vec::new(),
}
}
}
impl LodSampler for VanillaLodSampler {
fn fork(&self) -> Option<Box<dyn LodSampler>> {
Some(Box::new(Self {
generator: self.generator.clone(),
worker: self.generator.generator.worker(),
builder: None,
vegetation: self.vegetation.clone(),
cache: FxHashMap::default(),
order: VecDeque::new(),
details: Vec::new(),
}))
}
fn request_detail(&mut self, chunks: &[(i32, i32)], center: [i32; 2]) {
let mut missing = Vec::new();
for &key in chunks {
if let Some(columns) = self.cache.get(&key) {
self.details.push((key, columns.clone()));
} else {
missing.push(key);
}
}
if let Some(builder) = &self.builder {
builder.request(&missing, center);
}
}
fn drain_details(&mut self) -> Vec<LodDetail> {
let Some(builder) = &self.builder else {
return std::mem::take(&mut self.details);
};
while let Ok((key, columns)) = builder.results.try_recv() {
if !self.cache.contains_key(&key) {
self.order.push_back(key);
}
self.cache.insert(key, columns.clone());
while self.cache.len() > CACHE {
if let Some(old) = self.order.pop_front() {
self.cache.remove(&old);
builder
.shared
.requests
.lock()
.unwrap()
.finished
.remove(&old);
}
}
self.details.push((key, columns));
}
std::mem::take(&mut self.details)
}
fn sample(&mut self, x: i32, z: i32) -> Option<Surface> {
self.column(x, z).surface
}
fn column(&mut self, x: i32, z: i32) -> Column {
self.reduced_column(x, z, 1)
}
fn reduced_column(&mut self, x: i32, z: i32, width: i32) -> Column {
if width <= 16
&& let Some(columns) = self.cache.get(&(x >> 4, z >> 4))
{
return Column::reduced(
columns,
(x & 15) as usize,
(z & 15) as usize,
width as usize,
);
}
self.grid(x, z, width, 1).pop().unwrap_or_default()
}
fn grid(&mut self, x: i32, z: i32, width: i32, side: usize) -> Vec<Column> {
let samples = match self
.worker
.lod_grid(x + width / 2, z + width / 2, width, side)
{
Ok(samples) => samples,
Err(error) => {
log::warn!("Horizon: direct far sampling failed: {error}");
return vec![Column::default(); side * side];
}
};
samples
.into_iter()
.enumerate()
.map(|(index, (surface, spans))| {
let (wx, wz) = (
x + (index % side) as i32 * width,
z + (index / side) as i32 * width,
);
if width <= 16
&& let Some(columns) = self.cache.get(&(wx >> 4, wz >> 4))
{
return Column::reduced(
columns,
(wx & 15) as usize,
(wz & 15) as usize,
width as usize,
);
}
self.preview_column(wx + width / 2, wz + width / 2, width, surface, spans)
})
.collect()
}
}
impl VanillaLodSampler {
fn preview_column(
&self,
x: i32,
z: i32,
width: i32,
surface: Option<SurfaceSample>,
spans: Vec<(i32, i32, BlockStateId)>,
) -> Column {
let surface = surface.map(|sample| self.generator.client_surface(sample));
let biome = surface.map_or(0, |surface| surface.biome);
let registry = self.generator.generator.data().registry();
let mut column = Column {
surface,
spans: Vec::new(),
};
for (min, max, state) in spans.into_iter().rev() {
column.push(Span {
min: min as i16,
max: max as i16,
state,
biome,
light: if self.generator.dimension == "minecraft:the_nether" {
0
} else {
240
} | if registry
.state(state)
.is_some_and(|s| s.name == "minecraft:lava")
{
15 << 12
} else {
0
},
});
}
let light = if self.generator.dimension == "minecraft:the_nether" {
0
} else {
240
};
self.vegetation.apply(&mut column, x, z, width, light);
column
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
#[ignore = "requires prepared worldgen data in HORIZON_TEST_RESOURCES"]
fn decorated_lod_refines_real_seed_canopies_and_all_dimensions() -> crate::Result<()> {
let root = std::path::PathBuf::from(
std::env::var_os("HORIZON_TEST_RESOURCES").expect("resources"),
);
let assets = crate::assets::Assets::load(&root)?;
let biomes = crate::biome::Biomes::load(&root)?;
let terrain = Arc::new(VanillaTerrain::new(
&root.join("worldgen"),
assets.registry,
&biomes,
1234,
)?);
let mut sampler = VanillaLodSampler::new(terrain.clone());
let mut fork = sampler.fork().expect("independent far sampler");
let direct = sampler.grid(-1024, 0, 32, 16);
assert_eq!(
direct,
fork.grid(-1024, 0, 32, 16),
"sampling workers must agree on seed/biome vegetation"
);
let grass = assets
.registry
.default_state("minecraft:grass_block")
.unwrap();
let planted = |biome, width, x, z| {
let mut column = Column::from_surface(Some(Surface {
height: 80,
state: grass,
water: None,
biome,
}));
sampler.vegetation.apply(&mut column, x, z, width, 240);
column
};
let forest = biomes.id("minecraft:forest");
let plains = biomes.id("minecraft:plains");
let leaves = |column: &Column| {
column.spans.iter().any(|span| {
assets
.registry
.state(span.state)
.unwrap()
.name
.ends_with("_leaves")
})
};
let mut forest_count = 0;
let mut plains_count = 0;
for z in 0..64 {
for x in 0..64 {
forest_count += usize::from(leaves(&planted(forest, 1, x, z)));
plains_count += usize::from(leaves(&planted(plains, 1, x, z)));
}
}
assert!(
forest_count > 1000 && forest_count > plains_count * 4,
"feature-derived vegetation density: forest={forest_count}, plains={plains_count}"
);
for width in [16, 32, 128, 512, 1024] {
assert!(
leaves(&planted(forest, width, 0, 0)),
"forest canopy missing at scale {width}"
);
}
let key = (-64, 0);
sampler.request_detail(&[key], [-1024, 0]);
let deadline = std::time::Instant::now() + std::time::Duration::from_secs(30);
let generated = loop {
if let Some((_, columns)) = sampler.drain_details().into_iter().find(|(k, _)| *k == key)
{
break columns;
}
assert!(
std::time::Instant::now() < deadline,
"decoration worker stalled"
);
std::thread::sleep(std::time::Duration::from_millis(5));
};
let count = |suffix: &str| {
generated
.iter()
.flat_map(|column| &column.spans)
.filter(|span| {
assets
.registry
.state(span.state)
.unwrap()
.name
.ends_with(suffix)
})
.map(|span| i32::from(span.max - span.min))
.sum::<i32>()
};
assert!(count("_leaves") > 0, "unexplored forest has a real canopy");
assert!(count("_log") > 0, "canopy retains its trunks");
assert!(generated.iter().all(|column| {
column
.spans
.windows(2)
.all(|pair| pair[0].max <= pair[1].min)
}));
for z in 0..16 {
for x in 0..16 {
assert_eq!(
sampler.column(key.0 * 16 + x, key.1 * 16 + z),
generated[(z * 16 + x) as usize]
);
}
}
sampler.request_detail(&[key], [-1024, 0]);
let cached = sampler.drain_details();
assert!(
Arc::ptr_eq(&cached[0].1, &generated),
"reuses completed compact data"
);
drop(sampler);
let mut worker = terrain.generator.worker();
let started = std::time::Instant::now();
for x in 0..BATCH {
for z in 0..BATCH {
decorated_region(&mut worker, (key.0 + x, key.1 + z), &AtomicBool::new(false))?
.unwrap();
}
}
let separate = started.elapsed();
let started = std::time::Instant::now();
let mut emitted = FxHashMap::default();
let region = decorated_batch(&mut worker, key, BATCH, &AtomicBool::new(false), |chunk| {
let pos = chunk.position();
assert!(
emitted
.insert(
(pos.x, pos.z),
columns(chunk, &terrain.biome_map, "minecraft:overworld")
)
.is_none()
);
})?;
eprintln!(
"far generation: {} separate targets={separate:?}, shared targets={:?}",
emitted.len(),
started.elapsed()
);
assert_eq!(emitted.len(), (BATCH * BATCH) as usize);
for (key, data) in emitted {
assert_eq!(
data,
columns(&region[&key], &terrain.biome_map, "minecraft:overworld"),
"emitted before all neighbouring features completed"
);
}
assert_eq!(batch_origin((-1, -5)), (-4, -8));
let stop = AtomicBool::new(true);
assert!(decorated_region(&mut terrain.generator.worker(), key, &stop)?.is_none());
for (dimension, center) in [
("minecraft:the_nether", (0, 0)),
("minecraft:the_end", (16, 0)),
] {
let generator = OverworldGenerator::for_dimension(
terrain.generator.data().clone(),
1234,
"minecraft:normal",
dimension,
)?;
let mut emitted = 0;
let mut region = decorated_batch(
&mut generator.worker(),
center,
BATCH,
&AtomicBool::new(false),
|_| emitted += 1,
)?;
assert_eq!(emitted, (BATCH * BATCH) as usize);
let chunk = region.remove(&center).unwrap();
let data = columns(&chunk, &terrain.biome_map, dimension);
if dimension == "minecraft:the_nether" {
assert!(
data.iter()
.flat_map(|c| &c.spans)
.all(|s| s.light & 0xf0 == 0)
);
assert!(data.iter().flat_map(|c| &c.spans).any(|s| {
assets.registry.state(s.state).unwrap().name == "minecraft:lava"
&& s.light >> 12 == 15
}));
} else {
assert!(
data.iter().all(|c| c.spans.is_empty()),
"End void stays empty"
);
}
}
// Reuse the saved-proto bastion fixture. Repeated speculative regions
// must reconstruct structures after their one-shot placement flags ran.
let generator = OverworldGenerator::for_dimension(
terrain.generator.data().clone(),
-1860902098141295193,
"minecraft:normal",
"minecraft:the_nether",
)?;
let mut worker = generator.worker();
let mut previous = None;
for _ in 0..2 {
let chunk = decorated_region(&mut worker, (3, 7), &AtomicBool::new(false))?.unwrap();
assert!(
chunk
.structures()
.iter()
.any(|(name, _, _, _)| name.contains("bastion"))
);
let data = columns(&chunk, &terrain.biome_map, "minecraft:the_nether");
let snapshot = (data, chunk.block_entities);
if let Some(previous) = &previous {
assert_eq!(
&snapshot, previous,
"speculative structure state leaked between regions"
);
}
previous = Some(snapshot);
}
Ok(())
}
}
@@ -0,0 +1,309 @@
//! Seed-stable visual vegetation from the world's own feature data.
//! shortcut: these positions approximate native placement; saved/visited and
//! completed native chunks replace them rather than changing gameplay worldgen.
use crate::{
fast_hash::FxHashMap,
lod::{Column, Span, Surface},
};
use horizon_world::{
registry::{BlockRegistry, BlockStateId},
worldgen::generator::GeneratorData,
};
use serde_json::Value;
#[derive(Clone, Copy)]
struct Tree {
leaves: BlockStateId,
log: BlockStateId,
height: i32,
variation: i32,
radius: i32,
count: f64,
}
pub(super) struct Vegetation {
trees: FxHashMap<u16, Vec<Tree>>,
seed: u64,
registry: &'static BlockRegistry,
}
fn mean(v: &Value) -> f64 {
if let Some(n) = v.as_f64() {
return n;
}
let distribution = v["distribution"].as_array();
if let Some(entries) = distribution {
let total: f64 = entries
.iter()
.map(|e| e["weight"].as_f64().unwrap_or(0.))
.sum();
if total > 0. {
return entries
.iter()
.map(|e| mean(&e["data"]) * e["weight"].as_f64().unwrap_or(0.))
.sum::<f64>()
/ total;
}
}
(v["min_inclusive"].as_f64().unwrap_or(0.) + v["max_inclusive"].as_f64().unwrap_or(0.)) * 0.5
}
fn block(v: &Value, registry: &BlockRegistry) -> Option<BlockStateId> {
let name = v
.as_str()
.or_else(|| v["id"].as_str())
.or_else(|| v["Name"].as_str())?;
registry.default_state(name)
}
fn gather(
data: &GeneratorData,
value: &Value,
placed: bool,
count: f64,
depth: usize,
out: &mut Vec<Tree>,
) {
if depth > 12 || count <= 0. {
return;
}
if let Some(name) = value.as_str() {
let json = if placed {
data.placed_feature_json(name)
} else {
data.configured_feature_json(name)
};
if let Some(json) = json.and_then(|s| serde_json::from_str::<Value>(s).ok()) {
gather(data, &json, placed, count, depth + 1, out);
}
return;
}
if placed {
let mut count = count;
if let Some(modifiers) = value["placement"].as_array() {
for modifier in modifiers {
match modifier["type"]
.as_str()
.unwrap_or("")
.strip_prefix("minecraft:")
.unwrap_or("")
{
"count" | "count_on_every_layer" => count *= mean(&modifier["count"]),
"rarity_filter" => count /= modifier["chance"].as_f64().unwrap_or(1.).max(1.),
"count_extra" => {
count *= modifier["count"].as_f64().unwrap_or(0.)
+ modifier["extra_count"].as_f64().unwrap_or(0.)
* modifier["extra_chance"].as_f64().unwrap_or(0.)
}
_ => {}
}
}
}
gather(data, &value["feature"], false, count, depth + 1, out);
return;
}
let config = if value["config"].is_object() {
&value["config"]
} else {
value
};
match value["type"].as_str().unwrap_or("") {
"minecraft:tree" => {
let (Some(leaves), Some(log)) = (
block(&config["foliage_provider"], data.registry()),
block(&config["trunk_provider"], data.registry()),
) else {
return;
};
let trunk = &config["trunk_placer"];
out.push(Tree {
leaves,
log,
height: trunk["base_height"].as_i64().unwrap_or(5) as i32,
variation: (trunk["height_rand_a"].as_i64().unwrap_or(0)
+ trunk["height_rand_b"].as_i64().unwrap_or(0))
as i32,
radius: mean(&config["foliage_placer"]["radius"])
.round()
.clamp(2., 6.) as i32,
count,
});
}
"minecraft:random_selector" => {
let mut remaining = count;
if let Some(features) = config["features"].as_array() {
for entry in features {
let chance = entry["chance"].as_f64().unwrap_or(0.).clamp(0., 1.);
gather(
data,
&entry["feature"],
true,
remaining * chance,
depth + 1,
out,
);
remaining *= 1. - chance;
}
}
gather(data, &config["default"], true, remaining, depth + 1, out);
}
"minecraft:random_boolean_selector" => {
for key in ["feature_true", "feature_false"] {
gather(data, &config[key], true, count * 0.5, depth + 1, out);
}
}
"minecraft:simple_random_selector" => {
if let Some(features) = config["features"].as_array() {
for feature in features {
gather(
data,
feature,
true,
count / features.len().max(1) as f64,
depth + 1,
out,
);
}
}
}
_ => {}
}
}
fn hash(seed: u64, x: i32, z: i32) -> u64 {
let mut n = seed
^ (x as u64).wrapping_mul(0x9e3779b97f4a7c15)
^ (z as u64).wrapping_mul(0xd1b54a32d192ed03);
n = (n ^ n >> 30).wrapping_mul(0xbf58476d1ce4e5b9);
n = (n ^ n >> 27).wrapping_mul(0x94d049bb133111eb);
n ^ n >> 31
}
impl Vegetation {
pub(super) fn new(data: &GeneratorData, map: &[u16], seed: i64) -> Self {
let mut trees = FxHashMap::default();
for (index, &biome) in map.iter().enumerate() {
let Ok(json) = serde_json::from_str::<Value>(
data.biome_json(horizon_world::worldgen::biome::BiomeId(index as u32)),
) else {
continue;
};
let mut list = Vec::new();
if let Some(steps) = json["features"].as_array() {
for step in steps {
if let Some(features) = step.as_array() {
for feature in features {
gather(data, feature, true, 1., 0, &mut list);
}
}
}
}
if !list.is_empty() {
trees.insert(biome, list);
}
}
Self {
trees,
seed: seed as u64,
registry: data.registry(),
}
}
pub(super) fn apply(&self, column: &mut Column, x: i32, z: i32, width: i32, light: u16) {
let Some(surface) = column.surface else {
return;
};
if surface.water.is_some_and(|(y, _)| y >= surface.height) {
return;
}
let ground = self
.registry
.state(surface.state)
.map_or("", |s| s.name.as_str());
if !matches!(
ground,
"minecraft:grass_block"
| "minecraft:dirt"
| "minecraft:coarse_dirt"
| "minecraft:podzol"
| "minecraft:mycelium"
| "minecraft:rooted_dirt"
| "minecraft:moss_block"
) {
return;
}
let Some(trees) = self.trees.get(&surface.biome) else {
return;
};
let total: f64 = trees.iter().map(|tree| tree.count).sum();
let choose = |n: u64| {
let mut value = (n as u32 as f64 / u32::MAX as f64) * total;
for tree in trees {
value -= tree.count;
if value <= 0. {
return tree;
}
}
&trees[trees.len() - 1]
};
let mut best = None;
if width >= 16 {
let n = hash(self.seed, x.div_euclid(4), z.div_euclid(4));
let tree = choose(n);
let coverage = 1.
- (-total * f64::from(tree.radius * tree.radius) * std::f64::consts::PI / 256.)
.exp();
if coverage >= 0.25 {
let height = tree.height + ((n >> 32) & 1) as i32;
best = Some((
surface.height + height.max(4),
surface.height + height.max(4) - tree.radius * 2,
*tree,
false,
));
}
} else {
let radius = trees.iter().map(|t| t.radius).max().unwrap_or(2);
for cz in (z - radius).div_euclid(4)..=(z + radius).div_euclid(4) {
for cx in (x - radius).div_euclid(4)..=(x + radius).div_euclid(4) {
let n = hash(self.seed, cx, cz);
if (n as u32 as f64 / u32::MAX as f64) >= (total / 16.).min(1.) {
continue;
}
let tree = *choose(n.rotate_left(21));
let tx = cx * 4 + ((n >> 32) & 3) as i32;
let tz = cz * 4 + ((n >> 34) & 3) as i32;
let d2 = (x - tx).pow(2) + (z - tz).pow(2);
if d2 > tree.radius.pow(2) {
continue;
}
let height =
tree.height + ((n >> 40) % (tree.variation.max(0) + 1) as u64) as i32;
let cap = ((tree.radius.pow(2) - d2) as f32).sqrt().ceil() as i32;
let top = surface.height + height + cap.min(2);
let bottom = surface.height + height - tree.radius;
if best.as_ref().is_none_or(|&(y, _, _, _)| top > y) {
best = Some((top, bottom, tree, x == tx && z == tz));
}
}
}
}
if let Some((top, bottom, tree, trunk)) = best {
let bottom = bottom.max(surface.height + 1);
if trunk {
column.push(Span {
min: surface.height as i16,
max: bottom as i16,
state: tree.log,
biome: surface.biome,
light,
});
}
column.push(Span {
min: bottom as i16,
max: top.max(bottom + 1) as i16,
state: tree.leaves,
biome: surface.biome,
light,
});
column.surface = Some(Surface {
height: top.max(bottom + 1),
state: tree.leaves,
..surface
});
}
}
}
+8
View File
@@ -28,6 +28,14 @@ impl SamplerContext {
..Self::default()
}
}
/// Far previews evaluate only the supplied lattice. Ordinary generation
/// retains vanilla's dense strided-volume evaluation and rounding.
pub fn sparse() -> Self {
Self {
sparse: true,
..Self::cached()
}
}
pub fn clear_caches(&mut self) {
if let Some(caches) = &mut self.caches {
caches.clear();
@@ -172,6 +172,18 @@ impl DensitySampler for InterpolatedSampler {
volume: DensityVolume,
) -> Result<(), SampleError> {
check_buffer(output, volume)?;
if context.sparse {
if (0..3).all(|i| {
volume.steps()[i] % self.cell_size[i] == 0
&& volume.minimum()[i].rem_euclid(self.cell_size[i]) == 0
}) {
return self.input.sample_volume(context, output, volume);
}
return visit(volume, |index, position| {
output[index] = self.sample_value(context, position)?;
Ok(())
});
}
if (0..3).all(|i| {
(volume.steps()[i] == self.cell_size[i] || volume.sizes()[i] == 1)
&& volume.minimum()[i].rem_euclid(self.cell_size[i]) == 0
+1
View File
@@ -90,6 +90,7 @@ pub struct SamplerContext {
scratch: Vec<Vec<f32>>,
caches: Option<Vec<cache::CacheCell>>,
fields: WorldFields,
sparse: bool,
}
impl SamplerContext {
fn with_buffer<T>(
+22
View File
@@ -501,6 +501,28 @@ fn grid_interpolation_uses_child_bulk_corners_and_preserves_aligned_delegation()
.unwrap();
assert!(output.iter().all(|v| v.to_bits() == (-0.0_f32).to_bits()));
assert_eq!(traced.volumes.lock().unwrap()[1], volume);
let distant = DensityVolume::new([16, 49, 16], [-8192, -64, -8192], [1024, 8, 1024]).unwrap();
let mut output = vec![19.; distant.size()];
sampler
.sample_volume(&mut SamplerContext::sparse(), &mut output, distant)
.unwrap();
assert_eq!(
traced.volumes.lock().unwrap()[2],
distant,
"far samples must not expand to a block-resolution bounding box"
);
let unaligned = DensityVolume::new([2; 3], [-5, -9, -7], [1024, 8, 1024]).unwrap();
let mut output = [19.; 8];
sampler
.sample_volume(&mut SamplerContext::sparse(), &mut output, unaligned)
.unwrap();
assert!(output.iter().all(|&v| v == 0.));
assert!(
traced.volumes.lock().unwrap()[3..]
.iter()
.all(|v| v.size() == 8),
"unaligned far points use bounded corner queries"
);
assert!(InterpolatedSampler::new(traced.clone(), 0, 8).is_err());
assert!(InterpolatedSampler::new(traced, 4, -1).is_err());
}
+473 -4
View File
@@ -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},
@@ -495,6 +495,20 @@ impl GeneratorData {
&self.states
}
/// Biome identifiers in registry (identifier-sorted) order.
pub fn configured_feature_json(&self, name: &str) -> Option<&str> {
self.feature_resources
.as_ref()?
.features
.get(name)
.map(String::as_str)
}
pub fn placed_feature_json(&self, name: &str) -> Option<&str> {
self.feature_resources
.as_ref()?
.placed
.get(name)
.map(String::as_str)
}
pub fn biome_names(&self) -> &[Arc<str>] {
&self.biome_names
}
@@ -1476,6 +1490,21 @@ trait SeededPipeline: Send + Sync {
) -> Result<DecorateStats, GeneratorError>;
fn data(&self) -> &Arc<GeneratorData>;
fn sea_level(&self) -> i32;
fn surface_sample(
&self,
worker: &mut WorkerState,
x: i32,
z: i32,
spans: Option<&mut Vec<(i32, i32, BlockStateId)>>,
) -> Option<SurfaceSample>;
fn lod_grid(
&self,
worker: &mut WorkerState,
x: i32,
z: i32,
width: i32,
side: usize,
) -> Result<Vec<LodColumn>, GeneratorError>;
fn structure_starts(
&self,
worker: &mut WorkerState,
@@ -1658,6 +1687,306 @@ where
}
None
}
fn surface_sample(
&self,
worker: &mut WorkerState,
x: i32,
z: i32,
mut spans: Option<&mut Vec<(i32, i32, BlockStateId)>>,
) -> Option<SurfaceSample> {
let mut top = None;
let mut water = None;
let mut below = 0;
let mut surface_complete = false;
let mut visit = |y, state| {
let properties = self.data.states.get(state);
if let Some(spans) = spans.as_mut()
&& !properties.is_air()
{
if let Some((min, _, previous)) = spans.last_mut()
&& *min == y + 1
&& *previous == state
{
*min = y;
} else {
spans.push((y, y + 1, state));
}
}
if surface_complete {
return false;
}
if properties.is_air() || properties.has_fluid() {
if top.is_some() {
surface_complete = true;
return spans.is_none();
}
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
};
self.noise_column(x, z, &mut visit);
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::<R, _>::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);
if let Some(spans) = spans
&& let Some(index) = spans.iter().position(|&(min, max, _)| min <= y && y < max)
&& spans[index].2 != state
{
let (min, max, _) = spans[index];
if min == y {
spans[index].2 = state;
} else {
spans[index].1 = y;
spans.insert(index, (y, max, state));
}
}
Some(SurfaceSample {
height,
state,
water,
biome,
})
}
fn lod_grid(
&self,
worker: &mut WorkerState,
x: i32,
z: i32,
width: i32,
side: usize,
) -> Result<Vec<LodColumn>, GeneratorError> {
if width <= 0 || side == 0 || side > 64 {
return Err(resource("far sample dimensions"));
}
let mut geometry = Vec::with_capacity(side * side);
let mut context = SamplerContext::sparse();
let (min, height) = (self.settings.noise_min_y, self.settings.noise_height);
let step_xz = width.max(4);
let ox = x.div_euclid(4) * 4;
let oz = z.div_euclid(4) * 4;
let nx = ((x - ox + (side as i32 - 1) * width + step_xz - 1) / step_xz) + 1;
let nz = ((z - oz + (side as i32 - 1) * width + step_xz - 1) / step_xz) + 1;
let ny = (height + 7) / 8 + 1;
let volume = DensityVolume::new([nx, ny, nz], [ox, min, oz], [step_xz, 8, step_xz])
.map_err(stage("far density"))?;
let mut densities = vec![0.; volume.size()];
self.router
.get(RouterField::FinalDensity)
.sample_volume(&mut context, &mut densities, volume)
.map_err(stage("far density"))?;
let aquifer = DisabledAquifer::new(
self.picker,
self.data.registry.default_state("minecraft:air").unwrap(),
);
// Interpolate the supplied lattice, never the physical bounding volume.
let value = |wx: i32, iy: usize, wz: i32| {
let rx = (wx - ox) as f32 / step_xz as f32;
let rz = (wz - oz) as f32 / step_xz as f32;
let ix = rx.floor() as usize;
let iz = rz.floor() as usize;
let at = |dx: usize, dz: usize| {
densities[volume
.index([
(ix + dx).min(nx as usize - 1),
iy,
(iz + dz).min(nz as usize - 1),
])
.unwrap()]
};
let mix = |a: f32, b: f32, t: f32| a + (b - a) * t;
mix(
mix(at(0, 0), at(1, 0), rx - ix as f32),
mix(at(0, 1), at(1, 1), rx - ix as f32),
rz - iz as f32,
)
};
let mut profile = vec![0.; ny as usize];
let mut resolver = self.source.resolver(
&self.climate,
self.router.get(RouterField::Erosion).as_ref(),
);
let mut biome_context = SamplerContext::cached();
for dz in 0..side {
for dx in 0..side {
let (wx, wz) = (x + dx as i32 * width, z + dz as i32 * width);
let mut top = None;
let mut water = None;
let mut below = 0;
let mut complete = false;
let mut spans: Vec<(i32, i32, BlockStateId)> = Vec::new();
for (iy, density) in profile.iter_mut().enumerate() {
*density = value(wx, iy, wz);
}
for y in (min..min + height).rev() {
let iy = (y - min) as usize / 8;
let alpha = ((y - min) % 8) as f32 / 8.;
let density = profile[iy] + (profile[iy + 1] - profile[iy]) * alpha;
let state = aquifer
.compute_substance([wx, y, wz], f64::from(density))
.unwrap_or(self.settings.default_block);
let properties = self.data.states.get(state);
if !properties.is_air() {
if let Some((min, _, old)) = spans.last_mut()
&& *min == y + 1
&& *old == state
{
*min = y;
} else {
spans.push((y, y + 1, state));
}
}
if !complete {
if properties.is_air() || properties.has_fluid() {
if top.is_some() {
complete = true;
} else if properties.has_fluid() {
water.get_or_insert((y + 1, state));
} else {
water = None;
}
} else {
top.get_or_insert((y + 1, state));
below += 1;
}
}
}
let biome = if let Some((height, _)) = top {
let name = resolver
.sample(
&mut biome_context,
self.zoom.quart_at_block(BlockPos::new(wx, height - 1, wz)),
)
.map_err(stage("far biome"))?;
self.data
.biome_id(name)
.ok_or_else(|| resource("far biome"))?
} else {
BiomeId(0)
};
geometry.push((top, water, below, spans, biome));
}
}
if geometry.iter().all(|column| column.0.is_none()) {
return Ok(geometry
.into_iter()
.map(|(_, _, _, spans, _)| (None, spans))
.collect());
}
let volume =
DensityVolume::new([1; 3], [x, 0, z], [1; 3]).map_err(stage("far material"))?;
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::cached(),
)
.map_err(stage("far material"))?;
let mut material =
MaterialConditions::<R, _>::new(self.generation(), self.root.clone(), sources);
let rule = material
.add_rule(self.rule.clone())
.map_err(stage("far material"))?;
let mut result = Vec::with_capacity(side * side);
for (index, (top, water, below, mut spans, biome)) in geometry.into_iter().enumerate() {
let (wx, wz) = (
x + (index % side) as i32 * width,
z + (index / side) as i32 * width,
);
let Some((height, mut state)) = top else {
result.push((None, spans));
continue;
};
material.update_xz(wx, wz, 0, 0);
for depth in 1..=below.min(8) {
let y = height - depth;
material.update_y(
depth,
below - depth + 1,
water.map_or(i32::MIN, |(height, _)| height),
y,
);
let Some(index) = spans.iter().position(|&(min, max, _)| min <= y && y < max)
else {
continue;
};
let (min, max, old) = spans[index];
let painted = material
.try_apply(rule, BlockPos::new(wx, y, wz))
.map_err(stage("far material"))?
.unwrap_or(old);
if depth == 1 {
state = painted;
}
if old == painted {
continue;
}
let mut replacement = Vec::with_capacity(3);
if y + 1 < max {
replacement.push((y + 1, max, old));
}
replacement.push((y, y + 1, painted));
if min < y {
replacement.push((min, y, old));
}
spans.splice(index..=index, replacement);
}
result.push((
Some(SurfaceSample {
height,
state,
water,
biome,
}),
spans,
));
}
Ok(result)
}
fn noise_biomes(&self, worker: &mut WorkerState, quarts: &[[i32; 3]]) -> Vec<Option<String>> {
let mut resolver = self.source.resolver(
&self.climate,
@@ -1942,6 +2271,17 @@ 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,
}
pub type LodColumn = (Option<SurfaceSample>, Vec<(i32, i32, BlockStateId)>);
/// Per-thread generation state.
pub struct GeneratorWorker {
pipeline: Arc<dyn SeededPipeline>,
@@ -1955,6 +2295,23 @@ 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<SurfaceSample> {
self.pipeline.surface_sample(&mut self.state, x, z, None)
}
/// Noise/aquifer material runs, excluding air; bounds are half-open.
/// Features and carvers still require generated chunks.
pub fn surface_column(
&mut self,
x: i32,
z: i32,
) -> (Option<SurfaceSample>, Vec<(i32, i32, BlockStateId)>) {
let mut spans = Vec::new();
let surface = self
.pipeline
.surface_sample(&mut self.state, x, z, Some(&mut spans));
(surface, spans)
}
/// `/locate structure`: the nearest start of one of `names`.
pub fn locate(
&mut self,
@@ -1964,11 +2321,22 @@ impl GeneratorWorker {
) -> Option<(String, [i32; 3])> {
self.pipeline.locate(&mut self.state, names, from, radius)
}
/// `BiomeSource.findClosestBiome3d`: the first sampled position whose
/// noise biome passes `allowed`, spiralling out from the origin.
/// Direct far sampling: work scales with rendered vertical resolution,
/// without allocating chunks, carving masks or propagated aquifers.
pub fn lod_grid(
&mut self,
x: i32,
z: i32,
width: i32,
side: usize,
) -> Result<Vec<LodColumn>, GeneratorError> {
self.pipeline.lod_grid(&mut self.state, x, z, width, side)
}
pub fn noise_biomes(&mut self, quarts: &[[i32; 3]]) -> Vec<Option<String>> {
self.pipeline.noise_biomes(&mut self.state, quarts)
}
/// `BiomeSource.findClosestBiome3d`: the first sampled position whose
/// noise biome passes `allowed`, spiralling out from the origin.
pub fn find_closest_biome(&mut self, search: &BiomeSearch<'_>) -> Option<(String, [i32; 3])> {
self.pipeline.find_closest_biome(&mut self.state, search)
}
@@ -2013,6 +2381,107 @@ 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 (column_surface, spans) = worker.surface_column(0, 0);
let column_surface = column_surface.expect("column land");
assert_eq!(column_surface.height, sample.height);
assert_eq!(column_surface.state, sample.state);
assert_eq!(column_surface.water, sample.water);
assert_eq!(column_surface.biome, sample.biome);
assert!(spans.iter().all(|&(min, max, state)| {
min < max && !generator.data().block_states().get(state).is_air()
}));
assert!(spans.windows(2).all(|pair| {
pair[1].1 <= pair[0].0 && (pair[1].1 != pair[0].0 || pair[1].2 != pair[0].2)
}));
assert!(spans.iter().any(|&(min, max, state)| {
min < sample.height && sample.height - 1 < max && state == sample.state
}));
assert!(sample.water.is_some_and(|(height, state)| {
spans
.iter()
.any(|&(min, max, block)| min < height && height - 1 < max && block == state)
}));
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 started = std::time::Instant::now();
let mut exact = Vec::new();
for z in 0..8 {
for x in 0..8 {
exact.push(worker.surface_column(-1024 + x * 32, z * 32));
}
}
let dense_time = started.elapsed();
let started = std::time::Instant::now();
let direct = worker.lod_grid(-1024, 0, 32, 8).unwrap();
for z in 0..8 {
for x in 0..8 {
let wx = -1024 + x * 32;
let wz = z * 32;
let (surface, spans) = &direct[(z * 8 + x) as usize];
let expected = exact[(z * 8 + x) as usize].0;
if let (Some(surface), Some(expected)) = (surface, expected) {
assert!(
(surface.height - expected.height).abs() <= 32,
"coarse height exceeds its vertical resolution at {wx} {wz}: {} vs {}",
surface.height,
expected.height
);
assert!(spans.iter().any(|&(min, max, state)| min < surface.height
&& surface.height - 1 < max
&& state == surface.state));
}
assert!(spans.iter().all(|&(min, max, _)| min < max));
assert!(spans.windows(2).all(|pair| pair[1].1 <= pair[0].0));
}
}
eprintln!(
"64 far columns: dense={dense_time:?}, direct32={:?}",
started.elapsed()
);
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());
let (surface, spans) = worker.surface_column(256, 0);
assert!(surface.is_none());
assert!(spans.is_empty());
let (surface, spans) = worker.lod_grid(256, 0, 32, 1).unwrap().pop().unwrap();
assert!(
surface.is_none() && spans.is_empty(),
"direct sampling retains End void"
);
}
#[test]
fn local_carver_cache_is_worker_owned_and_eviction_preserves_terrain() {
let root = std::env::var_os("HORIZON_RESOURCES")
+21 -3
View File
@@ -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,25 @@ 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 voxel columns. Columns retain separate material layers and air gaps; matching exposed faces are merged, with block-scale texture repeats. Complete chunk data is reduced once on the terrain worker and cached at each supported scale, across each cell’s area instead of enlarging its corner column. Material votes combine different light levels of the same block; reduced cells retain the brightest sampled sky and block light separately. Finer tiles replace only their own footprints in a loading fallback, and a movement margin limits rapid resolution changes at tile boundaries. Local Overworld, Nether and End worlds and a LAN world you host can supply their generator. Remote servers do not supply the client's world-generation seed or generator configuration.
Unknown areas are sampled directly from the dimension's seed-bound density and surface rules, with shared per-tile scratch and material evaluation. A small pool samples tiles independently of meshing. Refinement prioritizes visible screen error; high aerial views use the distance to the world height range to avoid unnecessary close detail below the camera. Initial vegetation uses the world's configured tree types, placement densities and biome assignments at every distance level. Tree positions, shapes and large-cell coverage are visual approximations, not a replay of native feature placement.
A separate bounded worker refines the nearest LOD levels with disposable ordinary chunk generation, including supported carvers, structures and decoration. Groups of 4×4 chunks share their generation halos, and each chunk is published once every neighbouring feature center has completed. Farther tiles use direct sampling rather than waiting for every intervening ordinary chunk. Saved and visited blocks take precedence over both paths. These temporary chunks never enter gameplay, simulation or normal save queues. Generated-column caches and request queues are bounded; far data is regenerated after reopening a world.
Coverage and detail improve progressively; a large selected radius does not mean all its target detail is already resident. The control status reports target and sampled target tile counts separately. Direct far sampling approximates vertical noise interpolation, fluid boundaries and lighting; surface slopes and column extensions, small features and partial block shapes can differ. Unexplored distant structures are not synthesized by the direct path, and native decoration order can differ from subsequent gameplay generation. Horizon-supported data-driven noise and tree configurations can be reused; this does not execute arbitrary Fabric/NeoForge worldgen or structure mods.
Ordinary Overworld atmospheric fog extends with the selected LOD distance. Fog Off disables distance haze at large ranges too; fluid, darkness and boss fog retain their limits.
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
+1 -1
View File
@@ -258,7 +258,7 @@ A `shaders/` folder of GLSL programs, named by where Iris (and OptiFine before i
| Custom textures, `noisetex` | easy | image loads |
| Custom uniforms (`uniform.*` expressions) | medium | a small expression evaluator per frame |
| Compute (`.csh`), `image2D` load/store | medium | wgpu has compute; storage formats are more limited than OpenGL's |
| Distant Horizons, Voxy hooks, `DH_*` | not planned | Horizon has no LOD terrain yet |
| Distant Horizons, Voxy hooks, `DH_*` | not planned | Horizon's native LOD terrain shares world colour/depth; it does not expose these mods' APIs |
Order: preprocessor and legaliser until the three test packs parse; then `composite`/`final`/`deferred` with the
uniforms; then `gbuffers_terrain`, `gbuffers_water` and `gbuffers_skybasic`/`skytextured` (the packs' look comes