Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 61 additions & 26 deletions crates/map/src/camera.rs
Original file line number Diff line number Diff line change
Expand Up @@ -371,10 +371,16 @@ impl Camera {
let pad = 2.0 * k;
let (hw0, hw1) = (w / 2.0 * z0 / t.d + pad, w / 2.0 * z1 / t.d + pad);
let (v0, v1) = (v_of(z0) + pad, v_of(z1) - pad);
let ground = [(-hw1, v1), (hw1, v1), (hw0, v0), (-hw0, v0)].map(|(u, v)| self.ground_to_map(u, v));
// Its ground, from the centre: a pan moves the band by as much, keeping its scale and
// size to the bit, so its tiles can be taken again ([`Band`]).
let offsets = [(-hw1, v1), (hw1, v1), (hw0, v0), (-hw0, v0)].map(|(u, v)| {
let (ux, uy) = self.unrotate(u, v);
Coord::new(ux * self.res, -uy * self.res)
});
let ground = offsets.map(|o| Coord::new(self.center.x + o.x, self.center.y + o.y));
let mut e = Extent::EMPTY;
for c in ground {
e.expand(c);
for o in offsets {
e.expand(o);
}
let mut res = self.res * k;
// A band squeezed on screen (far ground at a steep pitch is foreshortened up and
Expand All @@ -391,23 +397,35 @@ impl Camera {
if side > MAX_BAND_SIDE {
res *= side / MAX_BAND_SIDE;
}
let camera = Camera { center: e.center(), res, bearing: 0.0, pitch: 0.0 };
let mid = e.center();
let camera = Camera { center: Coord::new(self.center.x + mid.x, self.center.y + mid.y), res, bearing: 0.0, pitch: 0.0 };
let (width, height) = ((e.width() / res).ceil().max(1.0), (e.height() / res).ceil().max(1.0));
let depth = (z_of(v0) / t.d, z_of(v1) / t.d);
bands.push(Band { camera, width, height, depth, ground });
let per_unit = t.s / (t.d * self.res);
let depth_per_unit = if per_unit.is_finite() && per_unit > 0.0 { per_unit } else { 0.0 };
bands.push(Band { camera, width, height, depth, depth_per_unit, ground });
z0 = z1;
}
// A view too small to have any depth to it (or whose near and far ends meet) is one band
// over its ground, as a flat view is.
// over its ground, as a flat view is: its extent ([`Camera::extent`]), worked out from the
// centre as the other bands are, so a pan keeps its scale and size to the bit.
if bands.is_empty() {
let e = self.extent(w, h);
let camera = Camera { center: e.center(), res: self.res, bearing: 0.0, pitch: 0.0 };
let mut e = Extent::EMPTY;
for (sx, sy) in [(0.0, 0.0), (w, 0.0), (0.0, h), (w, h)] {
let (u, v) = self.unproject(sx, sy, w, h).unwrap_or_else(|| self.far_point(sx, w, h));
let (ux, uy) = self.unrotate(u, v);
e.expand(Coord::new(ux * self.res, -uy * self.res));
}
let mid = e.center();
let camera =
Camera { center: Coord::new(self.center.x + mid.x, self.center.y + mid.y), res: self.res, bearing: 0.0, pitch: 0.0 };
bands.push(Band {
camera,
width: (e.width() / self.res).ceil().max(1.0),
height: (e.height() / self.res).ceil().max(1.0),
depth: (1.0, 1.0),
ground: corners(&e),
depth_per_unit: 0.0,
ground: corners(&e).map(|o| Coord::new(self.center.x + o.x, self.center.y + o.y)),
});
}
bands
Expand Down Expand Up @@ -507,6 +525,10 @@ pub struct Band {
/// The nearest and farthest ground it holds, as multiples of the tilted camera's distance
/// from the view's centre.
pub depth: (f64, f64),
/// How much farther (as `depth` counts) the ground is a map unit past the band's far edge,
/// away from the camera: the depth grows evenly with the distance there. 0 in a flat view's
/// band, and where the tilt gives no finite growth.
pub depth_per_unit: f64,
/// The ground it holds, map units: a convex quadrilateral (far edge first), within the
/// camera's extent, which is its bounding box.
pub ground: [Coord; 4],
Expand All @@ -515,7 +537,7 @@ pub struct Band {
impl Band {
/// A flat view's one band: `camera` itself at `width`×`height`, all its ground.
pub fn flat(camera: Camera, width: f64, height: f64) -> Band {
Band { camera, width, height, depth: (1.0, 1.0), ground: corners(&camera.extent(width, height)) }
Band { camera, width, height, depth: (1.0, 1.0), depth_per_unit: 0.0, ground: corners(&camera.extent(width, height)) }
}

/// Whether `e` touches the band's ground (its edges count), as two convex shapes meet: no
Expand Down Expand Up @@ -560,18 +582,6 @@ impl Band {
}
(lo <= hi).then_some((lo, hi))
}

/// How much farther (as [`Band::depth`] counts) the ground is a map unit past the band's far
/// edge, away from the camera: the depth grows evenly with the distance there. 0 in a flat
/// view's band.
pub fn depth_per_unit(&self) -> f64 {
let [f0, f1, n0, n1] = self.ground;
let mid = |a: Coord, b: Coord| Coord::new((a.x + b.x) / 2.0, (a.y + b.y) / 2.0);
let (far, near) = (mid(f0, f1), mid(n0, n1));
let apart = (far.x - near.x).hypot(far.y - near.y);
let per = (self.depth.1 - self.depth.0) / apart;
if per.is_finite() && per > 0.0 { per } else { 0.0 }
}
}

/// An extent's corners, in turn round it.
Expand Down Expand Up @@ -748,7 +758,14 @@ mod tests {
fn a_band_touches_what_meets_its_ground_not_only_its_box() {
// A diamond in the box 0..10.
let ground = [Coord::new(5.0, 10.0), Coord::new(10.0, 5.0), Coord::new(5.0, 0.0), Coord::new(0.0, 5.0)];
let b = Band { camera: Camera::new(Coord::new(5.0, 5.0), 1.0), width: 10.0, height: 10.0, depth: (1.0, 2.0), ground };
let b = Band {
camera: Camera::new(Coord::new(5.0, 5.0), 1.0),
width: 10.0,
height: 10.0,
depth: (1.0, 2.0),
depth_per_unit: 0.0,
ground,
};
assert!(b.ground_touches(&Extent::new(4.0, 4.0, 6.0, 6.0)), "inside");
assert!(b.ground_touches(&Extent::new(-5.0, -5.0, 15.0, 15.0)), "round it");
assert!(b.ground_touches(&Extent::new(7.0, 7.0, 9.0, 9.0)), "over its north-east edge");
Expand Down Expand Up @@ -791,7 +808,7 @@ mod tests {
let s = 0.05 * dx.hypot(dy);
let past = Coord::new(fx + dx * 0.05, fy + dy * 0.05);
let depth = 1.0 / c.depth_scale(past, h).unwrap();
let want = b.depth.1 + b.depth_per_unit() * s;
let want = b.depth.1 + b.depth_per_unit * s;
assert!((depth - want).abs() < 1e-9 * want, "{pitch}: {depth} vs {want}");
}
// The fast lookup agrees with the camera.
Expand All @@ -817,19 +834,37 @@ mod tests {
}
}
}
// A pan moves the bands by as much, keeping their scales and sizes to the bit.
c.set_pitch(60.0);
let mut panned = c;
panned.center = Coord::new(c.center.x + 12_345.678, c.center.y - 9_876.543);
for (a, b) in c.tilt_bands(w, h).iter().zip(panned.tilt_bands(w, h)) {
assert_eq!(a.camera.res.to_bits(), b.camera.res.to_bits());
assert_eq!((a.width, a.height, a.depth, a.depth_per_unit), (b.width, b.height, b.depth, b.depth_per_unit));
}
// Degenerate views still give a band (the last always reaching the far end).
c.set_pitch(70.0);
let mut one_band = 0;
for (w, h) in [(1.0, 1.0), (0.0, 0.0), (3.0, 1e-9), (800.0, 2.0)] {
let bands = c.tilt_bands(w, h);
assert!(!bands.is_empty() && bands.len() <= MAX_TILT_BANDS, "{w}x{h}");
// A pan keeps them to the bit too, the one band over a view with no depth among them.
let mut panned = c;
panned.center = Coord::new(c.center.x + 12_345.678, c.center.y - 9_876.543);
for (a, b) in bands.iter().zip(panned.tilt_bands(w, h)) {
assert_eq!(a.camera.res.to_bits(), b.camera.res.to_bits(), "{w}x{h}");
assert_eq!((a.width, a.height, a.depth, a.depth_per_unit), (b.width, b.height, b.depth, b.depth_per_unit), "{w}x{h}");
}
one_band += usize::from(bands.len() == 1 && bands[0].depth == (1.0, 1.0));
}
assert!(one_band > 0, "a view with no depth to it");
// A flat view is one band: itself.
c.set_pitch(0.0);
let e = c.extent(w, h);
let ground =
[Coord::new(e.min_x, e.max_y), Coord::new(e.max_x, e.max_y), Coord::new(e.max_x, e.min_y), Coord::new(e.min_x, e.min_y)];
assert_eq!(c.tilt_bands(w, h), vec![Band { camera: c, width: w, height: h, depth: (1.0, 1.0), ground }]);
assert_eq!(c.tilt_bands(w, h)[0].depth_per_unit(), 0.0);
assert_eq!(c.tilt_bands(w, h), vec![Band { camera: c, width: w, height: h, depth: (1.0, 1.0), depth_per_unit: 0.0, ground }]);
assert_eq!(c.tilt_bands(w, h)[0].depth_per_unit, 0.0);
}

#[test]
Expand Down
5 changes: 3 additions & 2 deletions crates/render/src/frame_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,9 @@ const KEPT: usize = 2;

/// The most bytes of meshes the kept GPU checkpoints hold together ([`GpuPart`]), as
/// [`crate::layer_meshes`] counts them: they share them with the scenes on screen and the meshes
/// that module keeps, so what they hold beside those is at most this, the same budget again.
const MAX_MESH_BYTES: usize = crate::layer_meshes::MAX_BYTES;
/// that module keeps, so what they hold beside those is at most this: 128 MB (32 MB in the
/// browser), that module's least budget ([`crate::layer_meshes::max_bytes`]).
const MAX_MESH_BYTES: usize = if cfg!(target_arch = "wasm32") { 32 << 20 } else { 128 << 20 };

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This copies the kept-mesh floor as a literal (128 MB, and 32 MB for wasm, which layer_meshes::max_bytes also hard-codes) rather than naming it. Before this PR the value was layer_meshes::MAX_BYTES, so the two couldn't drift. Now a change to one floor leaves the other stale, and the doc's "that module's least budget" link points at a function whose value runs from 128 MB to 640 MB. Suggest one pub(crate) const in layer_meshes holding the two floor values (desktop and wasm), read by both this line and max_bytes. Nothing is wrong at today's values, so this is a quality point.

Category: quality

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in #3441: layer_meshes::MIN_BYTES (128 MB, 32 MB in the browser) is the one floor, read by max_bytes, max_bytes_for and frame_cache::MAX_MESH_BYTES.


/// What a frame is drawn from besides its layers.
#[derive(Clone, Debug)]
Expand Down
7 changes: 6 additions & 1 deletion crates/render/src/gpu_scene.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,16 @@ pub struct CmdClip {
pub depth: [f32; 2],
/// Point markers standing upright, sized by their distance (see `render::upright_markers`).
pub upright: bool,
/// Its strokes' widths and its markers' sizes, in pixels, are drawn this many times as
/// large: a long band is drawn at a lower pixel density (`render::band_view`), and its
/// meshes, sized in its pixels, are brought to the view's while drawing, not written, so the
/// meshes it keeps (`layer_meshes`) stay shared.
pub px: f32,
}

impl CmdClip {
/// Every depth, markers as drawn.
pub const ALL: CmdClip = CmdClip { depth: [0.0, f32::MAX], upright: false };
pub const ALL: CmdClip = CmdClip { depth: [0.0, f32::MAX], upright: false, px: 1.0 };
}

/// One draw command. `Image` is a CPU-rasterized run (basemap, tiles, rasters, and layers
Expand Down
Loading
Loading