Merge pull request #602 from ludovic111/perf/vanilla-view-distance

Draw the columns vanilla draws (ChunkTrackingView.isInViewDistance)
This commit is contained in:
Ludovic Marie
2026-10-17 03:48:27 +02:00
committed by GitHub
3 changed files with 74 additions and 35 deletions
+1 -1
View File
@@ -2217,7 +2217,7 @@ impl Renderer {
visible.clear();
self.sections.visible(
camera,
frame.distance,
(frame.distance / 16.0) as i32,
&view,
|k| !culling || reachable.contains(k),
&mut visible,
+30 -28
View File
@@ -3,7 +3,7 @@
//! the render distance rejects all of its sections with one test, and the
//! sections that remain are reached by index rather than by hashing keys.
use super::{Frustum, SectionKey};
use crate::fast_hash::FxHashMap;
use crate::{fast_hash::FxHashMap, streaming::in_view_distance};
use glam::{DVec3, Vec3};
/// One chunk column's resident sections.
@@ -149,26 +149,28 @@ fn origin(key: SectionKey, camera: DVec3) -> DVec3 {
) - camera
}
/// Whether the section at `key` is drawn: within `distance` blocks of the
/// camera horizontally (plus a section and a half), and partly inside the
/// frustum. `None` when it is not, else its squared centre distance and origin.
fn in_view(
key: SectionKey,
camera: DVec3,
distance: f32,
frustum: &Frustum,
) -> Option<(f64, Vec3)> {
let origin = origin(key, camera);
let center = origin + DVec3::splat(8.0);
let horizontal = (center.x * center.x + center.z * center.z).sqrt();
if horizontal > f64::from(distance) + 24.0 {
/// The camera's chunk column.
fn camera_column(camera: DVec3) -> (i32, i32) {
(
(camera.x.floor() as i32) >> 4,
(camera.z.floor() as i32) >> 4,
)
}
/// Whether the section at `key` is drawn: its column within the render
/// distance of the camera's (`SectionOcclusionGraph.isInViewDistance`), and
/// partly inside the frustum. `None` when it is not, else its squared centre
/// distance and origin.
fn in_view(key: SectionKey, camera: DVec3, radius: i32, frustum: &Frustum) -> Option<(f64, Vec3)> {
if !in_view_distance(camera_column(camera), radius, key.0, key.2) {
return None;
}
let origin = origin(key, camera);
let min = origin.as_vec3();
if !frustum.intersects(min, min + Vec3::splat(16.0)) {
return None;
}
Some((center.length_squared(), min))
Some(((origin + DVec3::splat(8.0)).length_squared(), min))
}
impl<T> Resident<T> {
@@ -178,22 +180,22 @@ impl<T> Resident<T> {
/// Columns are tested first, with the same distance test (it depends
/// only on x and z) and a box spanning their sections: every section box
/// lies inside it and the frustum test rounds monotonically, so a column
/// the frustum rejects holds no section it would accept.
/// the frustum rejects holds no section it would accept. `radius` is the
/// render distance in chunks.
pub fn visible(
&self,
camera: DVec3,
distance: f32,
radius: i32,
frustum: &Frustum,
reachable: impl Fn(SectionKey) -> bool,
out: &mut Vec<Visible>,
) {
let from = camera_column(camera);
for column in &self.columns {
let low = origin((column.x, column.low, column.z), camera);
let center = low + DVec3::splat(8.0);
if (center.x * center.x + center.z * center.z).sqrt() > f64::from(distance) + 24.0 {
if !in_view_distance(from, radius, column.x, column.z) {
continue;
}
let min = low.as_vec3();
let min = origin((column.x, column.low, column.z), camera).as_vec3();
let top = origin((column.x, column.high, column.z), camera).as_vec3();
if !frustum.intersects(min, top + Vec3::splat(16.0)) {
continue;
@@ -203,7 +205,7 @@ impl<T> Resident<T> {
if !reachable(key) {
continue;
}
if let Some((d, o)) = in_view(key, camera, distance, frustum) {
if let Some((d, o)) = in_view(key, camera, radius, frustum) {
out.push(Visible {
distance: d.to_bits(),
key,
@@ -243,20 +245,20 @@ mod tests {
(DVec3::new(8.0, 64.0, 8.0), 45.0, 89.0),
];
for (camera, yaw, pitch) in cameras {
for distance in [32.0, 96.0, 160.0] {
for radius in [2, 6, 10] {
let projection = super::super::world_projection(70.0, 16.0 / 9.0, 512.0);
let view = crate::camera::view_matrix(yaw, pitch);
let frustum = Frustum {
planes: super::super::frustum(projection * view),
};
let mut fast = Vec::new();
r.visible(camera, distance, &frustum, reachable, &mut fast);
r.visible(camera, radius, &frustum, reachable, &mut fast);
let mut slow: Vec<(SectionKey, f64, [f32; 3])> = r
.items
.iter()
.filter(|(k, _)| reachable(*k))
.filter_map(|(k, _)| {
in_view(*k, camera, distance, &frustum).map(|(d, o)| (*k, d, o.to_array()))
in_view(*k, camera, radius, &frustum).map(|(d, o)| (*k, d, o.to_array()))
})
.collect();
slow.sort_unstable_by(|a, b| a.1.total_cmp(&b.1).then(a.0.cmp(&b.0)));
@@ -268,7 +270,7 @@ mod tests {
})
.collect();
assert!(!slow.is_empty());
assert_eq!(fast, slow, "{camera} {yaw} {pitch} {distance}");
assert_eq!(fast, slow, "{camera} {yaw} {pitch} {radius}");
}
}
}
@@ -356,7 +358,7 @@ mod tests {
let t = Instant::now();
keys.clear();
keys.extend(map.keys().filter_map(|&k| {
in_view(k, camera, 512.0, &frustum).map(|(d, o)| (k, d, o.to_array()))
in_view(k, camera, 32, &frustum).map(|(d, o)| (k, d, o.to_array()))
}));
keys.sort_unstable_by(|a, b| a.1.total_cmp(&b.1));
for (k, _, _) in &keys {
@@ -365,7 +367,7 @@ mod tests {
old = old.min(t.elapsed());
let t = Instant::now();
list.clear();
r.visible(camera, 512.0, &frustum, |_| true, &mut list);
r.visible(camera, 32, &frustum, |_| true, &mut list);
for v in &list {
sum += i64::from(r.get(v.slot)[1] + r.get(v.slot)[0]);
}
+43 -6
View File
@@ -968,7 +968,8 @@ impl Streaming {
return (uploads, removed);
}
// Meshing once the column and its neighbours are lit, and the column
// is within the render distance. Sections set aside past it come back
// is within the render distance (`in_view_distance`, the columns
// vanilla draws). Sections set aside past it come back
// when the player moves: a column that loads with its light gets no
// other request (#228).
let area = (self.center, self.radius, self.forced.len());
@@ -981,7 +982,7 @@ impl Streaming {
forced: &self.forced,
};
self.waiting.retain(|&(x, sy, z)| {
let back = ranges.in_main_range(x, z, 0);
let back = ranges.in_view(x, z);
if back {
world.dirty_sections.insert((x, sy, z));
}
@@ -996,7 +997,7 @@ impl Streaming {
world.dirty_sections.remove(&(x, sy, z));
continue;
}
if !self.in_main_range(x, z, 0) {
if !self.ranges().in_view(x, z) {
world.dirty_sections.remove(&(x, sy, z));
self.waiting.insert((x, sy, z));
continue;
@@ -1204,9 +1205,15 @@ impl Streaming {
fn in_range(&self, x: i32, z: i32, margin: i32) -> bool {
self.ranges().in_range(x, z, margin)
}
fn in_main_range(&self, x: i32, z: i32, margin: i32) -> bool {
self.ranges().in_main_range(x, z, margin)
}
}
/// `ChunkTrackingView.isInViewDistance`: whether the column (x, z) is drawn
/// at this render distance around `center`, the camera's column
/// (`SectionOcclusionGraph.isInViewDistance`). Each axis gets a column of
/// slack, so this reaches past the circle of `radius` columns.
pub fn in_view_distance(center: (i32, i32), radius: i32, x: i32, z: i32) -> bool {
let gap = |d: i32| i64::from((d.abs() - 1).max(0));
let (dx, dz) = (gap(x - center.0), gap(z - center.1));
dx * dx + dz * dz < i64::from(radius) * i64::from(radius)
}
/// The areas kept loaded: circles around the player and `extra`, and forced chunks.
struct Ranges<'a> {
@@ -1271,6 +1278,11 @@ impl Ranges<'_> {
dx * dx + dz * dz <= r * r
})
}
/// The columns meshed for drawing: those vanilla draws around the
/// player (`in_view_distance`), and forced ones.
fn in_view(&self, x: i32, z: i32) -> bool {
in_view_distance(self.center, self.radius, x, z) || self.forced.contains(&(x, z))
}
fn in_main_range(&self, x: i32, z: i32, margin: i32) -> bool {
let r = i64::from(self.radius + margin);
let (dx, dz) = (i64::from(x - self.center.0), i64::from(z - self.center.1));
@@ -1306,6 +1318,31 @@ impl Drop for Streaming {
mod tests {
use super::*;
#[test]
fn view_distance_is_vanillas_shape() {
// ChunkTrackingView.isInViewDistance at render distance 12: a column
// of slack on each axis, so 533 columns rather than the 441 of a
// circle of radius 12.
let count = |r: i32| {
(-40..=40)
.flat_map(|x| (-40..=40).map(move |z| (x, z)))
.filter(|&(x, z)| in_view_distance((5, -7), r, x + 5, z - 7))
.count()
};
assert_eq!(count(12), 533);
assert_eq!(count(2), 25);
for (x, z, inside) in [(12, 0, true), (13, 0, false), (9, 9, true), (10, 9, false)] {
assert_eq!(in_view_distance((0, 0), 12, x, z), inside, "({x}, {z})");
assert_eq!(
in_view_distance((0, 0), 12, -z, -x),
inside,
"({}, {})",
-z,
-x
);
}
}
struct GenerationFixture {
world: World,
center: (i32, i32),