From 7cb114079e89d50d004554113e4140252107a965 Mon Sep 17 00:00:00 2001 From: Dominik Strebel Date: Sun, 29 Nov 2026 22:42:46 +0100 Subject: [PATCH 1/5] perf(render): a steep tilted view's far bands mesh only what reaches their ground Refs #2541 (tilted views, step 3) - A band's lines and polygons are left out, before styling, when their widest stroke can't reach its ground grown by a margin; its tiles list only the features reaching that ground. - A tile on a band's ground edge remembers the ground it was cut for and is taken again while the new ground in it lies within that, grown by the margin (an eighth of the view). - A layer keeps as many levels of detail as a view has bands. - vector_layers_bench: BENCH_PITCH. --- crates/map/src/camera.rs | 22 ++- crates/map/src/lib.rs | 3 +- crates/map/src/lod.rs | 7 +- crates/map/src/symbology.rs | 53 +++++++ crates/render/examples/vector_layers_bench.rs | 5 +- crates/render/src/layer_meshes.rs | 69 +++++++- crates/render/src/lib.rs | 105 +++++++++++- crates/render/src/mesh.rs | 31 +++- crates/render/src/mesh_tiles.rs | 150 ++++++++++++++++-- 9 files changed, 416 insertions(+), 29 deletions(-) diff --git a/crates/map/src/camera.rs b/crates/map/src/camera.rs index 6f371f0d63..27973e3a5d 100644 --- a/crates/map/src/camera.rs +++ b/crates/map/src/camera.rs @@ -558,6 +558,17 @@ impl Band { ![(1.0, 0.0), (0.0, 1.0)].into_iter().chain(edges).any(apart) } + /// Whether `e` lies wholly on the band's ground (its edges count): each of its corners on the + /// inner side of every edge of the convex ground, whichever way round the ground runs. + pub fn ground_holds(&self, e: &Extent) -> bool { + let side = |p: Coord, i: usize| { + let (a, b) = (self.ground[i], self.ground[(i + 1) % 4]); + (b.x - a.x) * (p.y - a.y) - (b.y - a.y) * (p.x - a.x) + }; + let all = |sign: f64| corners(e).iter().all(|&p| (0..4).all(|i| side(p, i) * sign >= 0.0)); + !e.is_empty() && (all(1.0) || all(-1.0)) + } + /// The least and greatest x of the band's ground between `y0` and `y1` (its edges count): /// the columns a row of tiles there touches it in, as [`Band::ground_touches`] finds tile /// by tile (the ground is convex, so they run unbroken). `None` when it doesn't reach there. @@ -755,7 +766,7 @@ mod tests { } #[test] - fn a_band_touches_what_meets_its_ground_not_only_its_box() { + fn a_band_touches_what_meets_its_ground_and_holds_what_lies_on_it() { // 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 { @@ -784,6 +795,15 @@ mod tests { assert_eq!(across, b.ground_touches(&Extent::new(x0, y0, x1, y1)), "{column}, {row}"); } } + // It holds a box wholly on the ground, its edges counting, whichever way round it runs. + let reversed = Band { ground: [ground[3], ground[2], ground[1], ground[0]], ..b }; + for b in [b, reversed] { + assert!(b.ground_holds(&Extent::new(4.0, 4.0, 6.0, 6.0)), "inside"); + assert!(b.ground_holds(&Extent::new(2.5, 2.5, 7.5, 7.5)), "corners on its edges"); + assert!(!b.ground_holds(&Extent::new(7.0, 7.0, 9.0, 9.0)), "over its north-east edge"); + assert!(!b.ground_holds(&Extent::new(-5.0, -5.0, 15.0, 15.0)), "round it"); + assert!(!b.ground_holds(&Extent::EMPTY)); + } } #[test] diff --git a/crates/map/src/lib.rs b/crates/map/src/lib.rs index 8c584b1038..cfdca251f9 100644 --- a/crates/map/src/lib.rs +++ b/crates/map/src/lib.rs @@ -5403,7 +5403,8 @@ mod tests { } } assert_eq!(lod::DISPLAY_BUILDS.get(), before + 3); - assert_eq!(l.display_rejected.len(), lod::LEVELS_KEPT); + // Each of the three kept (fewer than `LEVELS_KEPT`). + assert_eq!(l.display_rejected.len(), 3); l.touch_geometry(); assert!(!l.prepare_display(&flat, 0.001, false)); assert_eq!(lod::DISPLAY_BUILDS.get(), before + 4); diff --git a/crates/map/src/lod.rs b/crates/map/src/lod.rs index eed912d573..b5f93b167a 100644 --- a/crates/map/src/lod.rs +++ b/crates/map/src/lod.rs @@ -26,8 +26,11 @@ pub const THIN_ABOVE: usize = 200_000; /// it draws from the full geometry (zoomed in past the vertex spacing, or many tiny features). pub const MIN_SAVING: f64 = 0.25; -/// How many levels a layer keeps. -pub const LEVELS_KEPT: usize = 3; +/// How many levels a layer keeps: as many as a tilted view has bands +/// ([`crate::camera::MAX_TILT_BANDS`]), each drawn at the level of its own scale, so the bands of +/// one view don't push each other's copies out and thin the layer again every frame. The copies +/// stay within [`CACHE_BYTES`], and a far band's are small. +pub const LEVELS_KEPT: usize = crate::camera::MAX_TILT_BANDS; /// Additional geometry retained for display levels per layer, excluding original/projected data. pub const CACHE_BYTES: usize = 64 * 1024 * 1024; diff --git a/crates/map/src/symbology.rs b/crates/map/src/symbology.rs index 280d69ee45..9e38011ffb 100644 --- a/crates/map/src/symbology.rs +++ b/crates/map/src/symbology.rs @@ -1202,6 +1202,21 @@ impl Renderer { Classer { expr: rustgis_data::query::parse_fields(text, ds).ok(), env } } + /// The widest stroke any feature of a layer of `g` it styles is drawn with (points): every + /// symbol it holds, the one for no class among them. A layer's stacked strokes + /// ([`Renderer::strokes_for`]) and expressions ([`crate::Layer::symbol_by`]) aren't counted. + pub fn widest_stroke(&self, g: GeometryType) -> f32 { + let widest = |symbols: &mut dyn Iterator| symbols.map(|s| s.stroke_width).fold(0f32, f32::max); + match self { + Renderer::Single(s) | Renderer::Proportional { base: s, .. } => s.stroke_width, + Renderer::Unique { classes, other, .. } => widest(&mut classes.iter().map(|c| &c.1).chain(std::iter::once(other))), + // A feature in no class takes the first symbol's stroke, or the default's without one + // (`symbol_in`). + Renderer::Graduated { symbols, .. } if symbols.is_empty() => Symbol::default_for(g, 0).stroke_width, + Renderer::Graduated { symbols, .. } => widest(&mut symbols.iter()), + } + } + /// The first symbol (the single symbol, or a representative). pub fn base_symbol(&self) -> Symbol { match self { @@ -3979,6 +3994,44 @@ mod tests { use super::*; use rustgis_data::{Field, Value}; + /// The widest stroke a renderer draws is that of any of its symbols, the one for no class + /// among them. + #[test] + fn a_renderers_widest_stroke_is_any_of_its_symbols() { + let line = |w: f32| Symbol { stroke_width: w, ..Symbol::default_for(GeometryType::Polyline, 0) }; + assert_eq!(Renderer::Single(line(2.5)).widest_stroke(GeometryType::Polyline), 2.5); + let unique = |other: f32| Renderer::Unique { + field: "kind".into(), + classes: vec![("a".into(), line(1.0)), ("b".into(), line(3.0))], + other: line(other), + ramp: Ramp::default(), + class_labels: BTreeMap::new(), + other_label: None, + class_strokes: BTreeMap::new(), + other_strokes: Vec::new(), + class_fills: BTreeMap::new(), + other_fills: Vec::new(), + expression: None, + }; + assert_eq!(unique(0.5).widest_stroke(GeometryType::Polyline), 3.0); + assert_eq!(unique(6.0).widest_stroke(GeometryType::Polyline), 6.0, "the stroke of no class"); + let graduated = |symbols: Vec| Renderer::Graduated { + field: "n".into(), + breaks: vec![1.0, 2.0], + symbols, + ramp: Ramp::default(), + method: ClassMethod::default(), + min: 0.0, + classes: 2, + class_labels: BTreeMap::new(), + class_strokes: BTreeMap::new(), + class_fills: BTreeMap::new(), + }; + assert_eq!(graduated(vec![line(4.0), line(2.0)]).widest_stroke(GeometryType::Polyline), 4.0); + let default = Symbol::default_for(GeometryType::Polyline, 0).stroke_width; + assert_eq!(graduated(Vec::new()).widest_stroke(GeometryType::Polyline), default, "no symbols: the default's"); + } + /// Labels saved before label classes (#618) read as the Default class alone, and are written /// back the same, so older versions still read a project without classes. #[test] diff --git a/crates/render/examples/vector_layers_bench.rs b/crates/render/examples/vector_layers_bench.rs index 31f10f7dc5..c814286daf 100644 --- a/crates/render/examples/vector_layers_bench.rs +++ b/crates/render/examples/vector_layers_bench.rs @@ -9,7 +9,7 @@ //! out (the way out finds the levels of detail built on the way in, those still kept); //! - **pan 10 m / pan 3 m:** west to east across it at 10 and 3 Web Mercator metres a point, a //! quarter of the view a step, so every frame brings new features in; -//! - **tilt 10 m:** the same pan at 10 m, tilted 60°; +//! - **tilt 10 m:** the same pan at 10 m, tilted 60° (`BENCH_PITCH=80` another pitch); //! - **restyle:** the top layer's fill colour changed in each of 8 frames of one view, over the //! whole map and at 10 m a point (#1057), then its transparency instead, then the fill with //! every layer renamed in each frame too, so that none looks as it did: what each frame drew @@ -182,7 +182,8 @@ fn main() { } let back: Vec = zoom.iter().rev().skip(1).copied().collect(); zoom.extend(back); - let tilt: Vec = pan(10.0).into_iter().map(|c| Camera { pitch: 60.0, ..c }).collect(); + let pitch: f64 = std::env::var("BENCH_PITCH").ok().and_then(|p| p.parse().ok()).unwrap_or(60.0); + let tilt: Vec = pan(10.0).into_iter().map(|c| Camera { pitch, ..c }).collect(); let sequences: [(&str, Vec); 5] = [ ("whole", (0..4).map(|_| camera(centre, whole)).collect()), ("zoom", zoom), diff --git a/crates/render/src/layer_meshes.rs b/crates/render/src/layer_meshes.rs index 73d5811b52..f12ced1d0d 100644 --- a/crates/render/src/layer_meshes.rs +++ b/crates/render/src/layer_meshes.rs @@ -111,13 +111,18 @@ pub(crate) struct BandMade { /// How far its strokes reach ([`crate::mesh_tiles::Tiling::grow`] and `spread`). pub grow: f64, pub spread: f64, + /// How far past its ground its edge tiles hold features ([`crate::mesh_tiles::Tiling::margin`]). + pub margin: f64, /// Its ground: which of its tiles there are, as the cull says for a screen scene. pub ground: [rustgis_geom::Coord; 4], } impl BandMade { fn same_tiles(&self, other: &BandMade) -> bool { - self.slot == other.slot && self.grow.to_bits() == other.grow.to_bits() && self.spread.to_bits() == other.spread.to_bits() + self.slot == other.slot + && self.grow.to_bits() == other.grow.to_bits() + && self.spread.to_bits() == other.spread.to_bits() + && self.margin.to_bits() == other.margin.to_bits() } fn same(&self, other: &BandMade) -> bool { @@ -512,6 +517,31 @@ mod tests { } } + /// Whether `scene` draws all `afresh` draws, mesh by mesh in the same place: a tilted view's + /// band tile taken again may hold features past its ground too, which its depth clipping + /// leaves out, so each of `afresh`'s meshes need only be within `scene`'s there. + fn draws_all_of(scene: &GpuScene, afresh: &GpuScene) -> bool { + type Key = ([u64; 2], Option<[u32; 4]>); + let placed = |s: &GpuScene| -> Vec<(Key, std::collections::HashSet<[u32; 4]>)> { + s.cmds + .iter() + .filter_map(|c| match c { + DrawCmd::Mesh { origin, square, vertices, .. } => Some(( + (origin.map(f64::to_bits), square.map(|q| q.map(f32::to_bits))), + vertices + .iter() + .map(|v| [v.position[0].to_bits(), v.position[1].to_bits(), v.color[0].to_bits(), v.stroke_px.to_bits()]) + .collect(), + )), + _ => None, + }) + .collect() + }; + let (have, want) = (placed(scene), placed(afresh)); + let keys = |m: &[(Key, std::collections::HashSet<[u32; 4]>)]| m.iter().map(|(k, _)| *k).collect::>(); + keys(&have) == keys(&want) && want.iter().all(|(k, w)| have.iter().any(|(h, v)| h == k && w.is_subset(v))) + } + /// Each Mesh command's vertices and indices, by address and by value. fn meshes(s: &GpuScene) -> Vec<(*const Vec, Vec<[u32; 4]>, Vec)> { s.cmds @@ -639,8 +669,7 @@ mod tests { assert!(taken > 0 && taken < tiled(&panned), "{taken} of {} tiles taken", tiled(&panned)); super::clear(); let afresh = scene(&mut m, &at(1.5)); - let values = |s: &GpuScene| meshes(s).into_iter().map(|m| (m.1, m.2)).collect::>(); - assert!(values(&panned) == values(&afresh), "the panned scene is the one drawn afresh"); + assert!(draws_all_of(&panned, &afresh), "the panned scene draws all the one drawn afresh does"); // The same view again takes every band's mesh of the layer whole. let bands = at(1.5).camera.tilt_bands(1600.0, 1000.0).len(); let _ = super::taken(); @@ -650,6 +679,40 @@ mod tests { assert_eq!(meshes(&again).into_iter().map(|m| m.0).collect::>(), shared); } + /// A steep view's band tiles on its ground's edge hold only the features reaching the ground, + /// grown by the band's margin: a pan, which moves the ground over them, takes those whose + /// ground stays within that and makes the rest again, and the scene draws all the one drawn + /// afresh does. + #[test] + fn a_panned_steep_view_takes_its_bands_edge_tiles_while_they_hold_its_ground() { + super::clear(); + let mut m = Map::new("Map"); + m.basemap = rustgis_map::Basemap::Blank; + m.graticule = false; + let mut d = Dataset::new("lines", GeometryType::Polyline, vec![]); + // Short lines every 3 km over 200 km: edge tiles hold some, and leave out others. + for n in 0u32..4900 { + let (x, y) = (f64::from(n % 70) * 0.03, f64::from(n / 70) * 0.03); + d.push(Some(Geometry::Line(vec![vec![Coord::new(6.9 + x, 46.0 + y), Coord::new(6.905 + x, 46.005 + y)]])), vec![]); + } + m.add_dataset(d); + let at = |x: f64| View { + width: 3200.0, + height: 2400.0, + pixels_per_point: 1.0, + camera: Camera { center: rustgis_geom::lonlat_to_merc(Coord::new(7.9, x)), res: 4.0, bearing: 0.0, pitch: 75.0 }, + sky: false, + }; + // North by about 11 km: past the margin (an eighth of the view, 1.6 km) for some edge tiles. + let first = scene(&mut m, &at(46.9)); + let panned = scene(&mut m, &at(47.0)); + let before: Vec<_> = meshes(&first).into_iter().map(|m| m.0).collect(); + assert!(meshes(&panned).iter().any(|m| before.contains(&m.0)), "a whole tile taken"); + super::clear(); + let afresh = scene(&mut m, &at(47.0)); + assert!(draws_all_of(&panned, &afresh), "the panned scene draws all the one drawn afresh does"); + } + /// A tilted view's bands keep the layer's meshes one a band, sharing one copy of the rows /// they drew; each band keeps its last two views ([`VIEWS`]), not its first. #[test] diff --git a/crates/render/src/lib.rs b/crates/render/src/lib.rs index 8fd38fabc5..6f515f5969 100644 --- a/crates/render/src/lib.rs +++ b/crates/render/src/lib.rs @@ -439,6 +439,14 @@ const READ_ALL_OVER: f64 = 0.5; /// or zooming out to twice the extent reveals (as MapLibre keeps the tiles around the view). pub const GPU_CULL_MARGIN: f64 = 0.5; +/// How far past its ground (a share of the tilted view's longer side, at its centre's scale) a +/// band's tile on the ground's edge holds features ([`mesh_tiles::Tiling::margin`]): a pan of up +/// to about that takes the tile again rather than make it. Less than a flat view's +/// [`GPU_CULL_MARGIN`]: at a steep pitch much of a band's tiles lie off its ground, and what an +/// edge tile holds past it is meshed for nothing. An eighth was quickest at 60–85° on the +/// benchmark's sheet, its pans a quarter of the view a frame. +const BAND_EDGE_MARGIN: f64 = 0.125; + /// The map extent a frame drawn for `view` holds features for: the view grown by `margin` /// ([`CULL_MARGIN`], or [`GPU_CULL_MARGIN`] for GPU meshes) on every side. pub fn cull_extent(view: &View, margin: f64) -> Extent { @@ -3527,6 +3535,8 @@ fn gpu_turned( // ones keep theirs (`layer_meshes`). let _mesh_scene = layer_meshes::Scene::begin(); let mut meshed = 0usize; + // Not finite (a degenerate view), no feature is left out for its ground. + let margin = Some(BAND_EDGE_MARGIN * view.width.max(view.height) * view.camera.res).filter(|m| m.is_finite()).unwrap_or(f64::MAX); for (i, b) in bands.iter().enumerate() { // A stopped frame (see `cancel`) draws no further band, nor falls back to the CPU. if cancel::requested() { @@ -3538,7 +3548,7 @@ fn gpu_turned( let grow = (scale * b.depth.1).max(1.0); let spread = scale * b.depth_per_unit; let (grow, spread) = if grow.is_finite() && spread.is_finite() { (grow, spread) } else { (f64::MAX, f64::MAX) }; - GPU_BAND.with(|g| g.set(Some(GpuBand { band: *b, slot: i, grow, spread, meshed }))); + GPU_BAND.with(|g| g.set(Some(GpuBand { band: *b, slot: i, grow, spread, margin, meshed }))); let frame = render_frame(map, &v, full_res_rasters, Backend::Gpu, Labels::Tilted(view, &mut candidates), false, failed_source, env); meshed = GPU_BAND.with(|g| g.take()).map_or(meshed, |g| g.meshed); let Some(RenderOutput::Gpu(scene)) = frame else { @@ -4600,6 +4610,8 @@ struct GpuBand { /// And how much more past the far edge, per map unit there and per map unit a stroke reaches /// as tessellated ([`mesh_tiles::Tiling::spread`]). spread: f64, + /// How far past its ground a tile on its edge holds features ([`mesh_tiles::Tiling::margin`]). + margin: f64, /// The mesh vertices the bands before it took of the scene's budget ([`mesh::layer_room`]): /// the bands are one scene. The frame adds its own. meshed: usize, @@ -5766,7 +5778,13 @@ fn render_frame( map_per_px, ppp: ctx.ppp, wireframe: ctx.wireframe, - band: band.map(|b| layer_meshes::BandMade { slot: b.slot, grow: b.grow, spread: b.spread, ground: b.band.ground }), + band: band.map(|b| layer_meshes::BandMade { + slot: b.slot, + grow: b.grow, + spread: b.spread, + margin: b.margin, + ground: b.band.ground, + }), }; Some((rustgis_map::look::LayerLook::of(layer)?, made)) }); @@ -5790,6 +5808,7 @@ fn render_frame( band: band.as_ref().map(|b| &b.band), grow: band.map_or(1.0, |b| b.grow), spread: band.map_or(0.0, |b| b.spread), + margin: band.map_or(0.0, |b| b.margin), }); let collected = mesh::collect_layer( &mut cmds, @@ -11887,6 +11906,88 @@ mod tests { assert_eq!(TILTED_MESHED.get(), Some(per_band.iter().sum::())); } + /// At a steep pitch a far band's tiles are many times the layer's size, so a tile on its + /// ground holds features far off it: a band meshes only the lines and polygons that reach its + /// ground, and a band whose ground holds none of the layer meshes nothing. + #[test] + fn a_steep_views_far_bands_mesh_only_what_reaches_their_ground() { + let mut m = Map::new("Map"); + m.basemap = rustgis_map::Basemap::Blank; + m.graticule = false; + // Stroked lines over a few kilometres around the view's centre. + let mut d = Dataset::new("near", GeometryType::Polyline, vec![]); + for i in 0..40 { + let k = f64::from(i) * 0.001; + d.push( + Some(Geometry::Line(vec![vec![Coord::new(8.0 + k, 47.0), Coord::new(8.02 - k, 47.02), Coord::new(8.0, 47.01 + k * 0.2)]])), + vec![], + ); + } + m.add_dataset(d); + let camera = Camera { center: lonlat_to_merc(Coord::new(8.01, 47.01)), res: 5.0, bearing: 0.0, pitch: 80.0 }; + let view = View { width: 1600.0, height: 1000.0, pixels_per_point: 1.0, camera, sky: false }; + let bands = view.camera.tilt_bands(view.width, view.height); + assert!(bands.len() >= 4, "a steep view's bands: {}", bands.len()); + let Some(RenderOutput::Gpu(scene)) = render_backend(&mut m, &view, Backend::Gpu) else { panic!("a GPU scene") }; + let mut per_band = vec![0usize; bands.len()]; + for (c, clip) in scene.cmds.iter().zip(&scene.clips) { + let gpu_scene::DrawCmd::Mesh { vertices, .. } = c else { continue }; + let i = if clip.depth[1] == f32::MAX { + bands.len() - 1 + } else { + bands.iter().position(|b| b.depth.1 as f32 == clip.depth[1]).expect("a band's clip") + }; + per_band[i] += vertices.len(); + } + // The lines' box, grown by the bands' margin and a kilometre (far past any stroke's reach + // at these scales). + let margin = BAND_EDGE_MARGIN * view.width.max(view.height) * view.camera.res; + let lines = m.layers[0].extent_on(&m.flat(), rustgis_map::ExtentOf::All).padded(0.0, margin + 1_000.0); + let on = |b: &rustgis_map::camera::Band| { + b.ground_across(lines.min_y, lines.max_y).is_some_and(|(lo, hi)| lo <= lines.max_x && hi >= lines.min_x) + }; + for (i, b) in bands.iter().enumerate() { + let side = mesh_tiles::tile_side(b.camera.res).unwrap(); + assert!(on(b) || per_band[i] == 0, "band {i} ({side:.0} m tiles) meshes {} vertices off its ground", per_band[i]); + } + assert!( + bands.iter().any(|b| !on(b) && mesh_tiles::tile_side(b.camera.res).unwrap() > lines.width()), + "a far band whose tile holds the lines" + ); + assert!(per_band.iter().sum::() > 0, "the near bands mesh the lines: {per_band:?}"); + } + + /// Each band of a steep view thins a dense layer at the level of its own scale: the layer + /// keeps every band's level, so the next frame of the view thins nothing again. + #[test] + fn a_steep_views_bands_keep_each_level_they_thin() { + let mut m = Map::new("Map"); + m.basemap = rustgis_map::Basemap::Blank; + m.graticule = false; + // Past `THIN_ABOVE` vertices: lines of fine zigzags, thinned at any of these levels, held + // in a projected system (rows in longitude/latitude take display copies instead). + let mut d = Dataset::new("dense", GeometryType::Polyline, vec![]); + for i in 0..300 { + let y = 46.0 + f64::from(i) * 0.01; + let line = (0..1000).map(|j| Coord::new(6.0 + f64::from(j) * 0.004, y + if j % 2 == 0 { 0.0 } else { 1e-5 })).collect(); + d.push(Some(Geometry::Line(vec![line])), vec![]); + } + d.hold_in(rustgis_data::raster::Crs::Epsg(2056)).unwrap(); + m.add_dataset(d); + let camera = Camera { center: lonlat_to_merc(Coord::new(8.0, 47.0)), res: 20.0, bearing: 0.0, pitch: 80.0 }; + let view = View { width: 1600.0, height: 1000.0, pixels_per_point: 1.0, camera, sky: false }; + let bands = view.camera.tilt_bands(view.width, view.height); + let levels: std::collections::BTreeSet = + bands.iter().filter_map(|b| rustgis_map::lod::level(band_view(&view, b).lod_tolerance())).collect(); + assert!(levels.len() > 3, "more levels than a layer kept before: {levels:?}"); + let Some(RenderOutput::Gpu(_)) = render_backend(&mut m, &view, Backend::Gpu) else { panic!("a GPU scene") }; + let layer = &m.layers[0]; + for b in &bands { + let tol = band_view(&view, b).lod_tolerance(); + assert!(!layer.thinned_cache(tol).same(layer.projected_cache()), "the level of a band at {:.0} m kept", b.camera.res); + } + } + /// A stroke reaching less than a tile past its line is cut into tiles, but in a tilted view's /// band, where the shader widens it by its depth past a tile's side, its layer is one mesh: /// the band grows the stroke's reach by the depth of its far edge. diff --git a/crates/render/src/mesh.rs b/crates/render/src/mesh.rs index af5f94f1e1..f8b1869fed 100644 --- a/crates/render/src/mesh.rs +++ b/crates/render/src/mesh.rs @@ -495,6 +495,23 @@ pub(crate) fn stroke_reach(g: &Geometry, s: &Symbol, wireframe: bool, ppp: f32, f64::from(tessellation_width(width_px, map_per_px)) / 2.0 + widest } +/// How far any line or polygon of `layer` reaches on a tilted view's band (`tiling`), its widest +/// stroke ([`rustgis_map::Renderer::widest_stroke`]) widened by depth as [`Tiling::widened`] +/// widens each: a feature farther than that from the band's ground draws nothing on it. `None` +/// off a band, for a graphics layer (each graphic its own symbol), and where it reaches a tile +/// (`side`) or more: such a stroke makes the layer one mesh, which a feature left out here mustn't +/// change. +fn ground_reach(layer: &Layer, tiling: &Tiling<'_>, side: f64, wireframe: bool, ppp: f32, map_per_px: f64) -> Option { + if tiling.band.is_none() || layer.graphics { + return None; + } + // A line's stroke at that width reaches as far as any line's or polygon's (`stroke_reach`). + let width = layer.renderer.widest_stroke(layer.geometry_type()).max(if wireframe { 1.0 } else { 0.0 }); + let widest = Symbol { stroke_width: width, stroke: Rgba::rgb(0, 0, 0), ..Symbol::default_for(rustgis_data::GeometryType::Polyline, 0) }; + let reach = tiling.widened(stroke_reach(&Geometry::Line(Vec::new()), &widest, false, ppp, map_per_px)); + (reach.is_finite() && reach < side).then_some(reach) +} + /// Collects one GPU-eligible layer, or one streamed batch of it, into Mesh/Points commands. /// `geoms`, `extents`, `rows` and `visible` are the render loop's own (geometries as the CPU /// draws them, their cached extents, row filter, extent cull), so the GPU draws exactly the @@ -541,6 +558,11 @@ pub(crate) fn collect_layer( let side = crate::mesh_tiles::tile_side(map_per_px); let near = tiling.as_ref().and_then(|t| crate::mesh_tiles::feature_area(t.area, map_per_px)); let mut farthest = 0f64; + // A tilted view's band leaves out, before styling them, the lines and polygons whose widest + // stroke can't reach its ground grown by its margin, as its tiles do ([`ground_reach`]): a far + // band's tiles are many times the layer's size, so most of a layer on its tiles can be off + // its ground. + let off_ground = tiling.as_ref().and_then(|t| Some((t.band?, ground_reach(layer, t, side?, wireframe, ppp, map_per_px)? + t.margin))); for (row, g) in geoms.iter().enumerate() { let Some(g) = g else { continue }; // Culled on the extent the CPU path culls on (the full geometry's, cached), so both @@ -554,6 +576,12 @@ pub(crate) fn collect_layer( if rows.get(row) != Some(&true) || !seen { continue; } + if !marker + && let Some((band, r)) = off_ground + && !band.ground_across(e.min_y - r, e.max_y + r).is_some_and(|(lo, hi)| lo <= e.max_x + r && hi >= e.min_x - r) + { + continue; + } ext = ext.union(&e); let s = layer.symbol(row); if marker { @@ -595,7 +623,8 @@ pub(crate) fn collect_layer( // (past `near`): its tiles aren't taken again, nor kept. let keeps = side.is_some_and(|side| farthest < side); let kept = if keeps { t.kept } else { &[] }; - let made = crate::mesh_tiles::tile_meshes(&features, t.area, t.band, kept, wireframe, ppp, map_per_px, limit); + let made = + crate::mesh_tiles::tile_meshes(&features, t.area, t.band.map(|b| (b, t.margin)), kept, wireframe, ppp, map_per_px, limit); made.map(|made| { made.map(|(tiles, used)| { let cmds = tiles.iter().filter_map(|t| t.cmd.as_ref().and_then(DrawCmd::share)).collect(); diff --git a/crates/render/src/mesh_tiles.rs b/crates/render/src/mesh_tiles.rs index ce028d8868..15a30d2527 100644 --- a/crates/render/src/mesh_tiles.rs +++ b/crates/render/src/mesh_tiles.rs @@ -112,12 +112,17 @@ pub(crate) struct MadeTile { /// Its Mesh command, sharing its data with the scenes drawing it; `None` when nothing draws /// in it. pub(crate) cmd: Option, + /// The ground of the band it was made for when it lay on the ground's edge: it holds only the + /// features reaching that ground, grown by the band's margin ([`buckets`]), and is taken again + /// only while a band's ground in it lies within that ([`taken_on`]). `None`: it holds every + /// feature reaching it. + cut_for: Option<[Coord; 4]>, } impl MadeTile { /// The tile sharing this one's mesh. pub(crate) fn share(&self) -> MadeTile { - MadeTile { tile: self.tile, need: self.need, cmd: self.cmd.as_ref().and_then(DrawCmd::share) } + MadeTile { tile: self.tile, need: self.need, cmd: self.cmd.as_ref().and_then(DrawCmd::share), cut_for: self.cut_for } } /// Its mesh's vertices. @@ -148,6 +153,10 @@ pub(crate) struct Tiling<'a> { /// How much more a stroke is widened per map unit past the far edge, per map unit it reaches /// as tessellated: [`rustgis_map::camera::Band::depth_per_unit`] times `grow`'s scale. pub spread: f64, + /// How far past the band's ground (map units) a tile on its edge holds features: a pan moves + /// the ground, and the tile is taken again while the ground in it stays within that + /// ([`MadeTile::cut_for`]). 0 for a flat scene. + pub margin: f64, } impl Tiling<'_> { @@ -210,14 +219,14 @@ fn buckets( features: &[Feature<'_>], area: &Extent, side: f64, - band: Option<&rustgis_map::camera::Band>, + band: Option<(&rustgis_map::camera::Band, f64)>, ) -> Option)>> { let index = |v: f64| (v / side).floor() as i64; let (in_columns, in_rows) = grid(area, side)?; // A band draws only its own ground: a tile off it shows nothing there, so it is neither made // nor counted (its cull, the ground's box, is much larger at a steep pitch). Each row's // tiles on it run unbroken, the ground being convex: their columns, by row. - let on_ground: Option>> = band.map(|b| { + let on_ground: Option>> = band.map(|(b, _)| { in_rows .clone() .map(|row| { @@ -239,6 +248,16 @@ fn buckets( if !(grown.min_x <= grown.max_x && grown.min_y <= grown.max_y) { continue; } + // Nor a feature whose stroke doesn't reach the band's ground, grown by its margin: a far + // band's tiles are many times the layer's size, so a tile on its ground can hold the whole + // layer, little of it there (`ground_across` over the feature's rows is exact for the + // convex ground). A tile wholly on the ground loses none of its features to this. + if let Some((b, margin)) = band { + let (y0, y1) = (grown.min_y - margin, grown.max_y + margin); + if !b.ground_across(y0, y1).is_some_and(|(lo, hi)| lo <= grown.max_x + margin && hi >= grown.min_x - margin) { + continue; + } + } // Only the tiles of the scene's area: what lies past them isn't drawn. let columns = index(grown.min_x).max(*in_columns.start())..=index(grown.max_x).min(*in_columns.end()); let rows = index(grown.min_y).max(*in_rows.start())..=index(grown.max_y).min(*in_rows.end()); @@ -511,14 +530,14 @@ fn tile_mesh( // Each part is refused once it passes the room left: it fits a limit as low as the vertices. let need = need.max(v.len()); if i.is_empty() { - return Ok(MadeTile { tile, need, cmd: None }); + return Ok(MadeTile { tile, need, cmd: None, cut_for: None }); } if used.fetch_add(v.len(), Ordering::Relaxed).saturating_add(v.len()) > limit { return Err(TessError::TooDense); } let square = [0.0, 0.0, side as f32, side as f32]; let cmd = DrawCmd::Mesh { origin, vertices: Arc::new(v), indices: Arc::new(i), square: Some(square) }; - Ok(MadeTile { tile, need, cmd: Some(cmd) }) + Ok(MadeTile { tile, need, cmd: Some(cmd), cut_for: None }) } /// The layer's features (points left out: they aren't tiled) as tile meshes within `area`, @@ -528,7 +547,7 @@ fn tile_mesh( pub(crate) fn tile_meshes( features: &[Feature<'_>], area: &Extent, - band: Option<&rustgis_map::camera::Band>, + band: Option<(&rustgis_map::camera::Band, f64)>, kept: &[MadeTile], wireframe: bool, ppp: f32, @@ -538,12 +557,28 @@ pub(crate) fn tile_meshes( tile_meshes_on(features, area, band, kept, wireframe, ppp, map_per_px, limit, CAPS, None) } +/// Whether `kept` holds what a tile of `square` made now for `band` (with its `margin`) would +/// draw: every feature reaching it, or, made on a band's edge, those reaching that band's ground +/// grown by the margin, as long as the ground in the tile now lies within that. A pan moves the +/// ground over an edge tile; what it then holds past the ground the band's depth clipping leaves +/// out. +fn taken_on(kept: &MadeTile, band: Option<&rustgis_map::camera::Band>, square: &Extent, margin: f64) -> bool { + let (Some(made_for), Some(band)) = (kept.cut_for, band) else { return kept.cut_for.is_none() }; + let then = rustgis_map::camera::Band { ground: made_for, ..*band }; + // The ground in the tile, a convex polygon: within the ground then, grown by the margin (a + // convex shape too), when each of its corners is. A ground meeting the tile only along its + // edge or at a corner clips to nothing: the tile is made again, as what reaches that edge + // can't be told from the corners. + let ground = clip_ring(&band.ground, square); + !ground.is_empty() && ground.iter().all(|p| then.ground_touches(&Extent::new(p.x - margin, p.y - margin, p.x + margin, p.y + margin))) +} + /// [`tile_meshes`] within `caps`, on `threads` threads, or as [`threads_for`] decides. #[expect(clippy::too_many_arguments, reason = "the layer, the scene's area, band and scale, the tiles kept, the limits and the threads")] fn tile_meshes_on( features: &[Feature<'_>], area: &Extent, - band: Option<&rustgis_map::camera::Band>, + band: Option<(&rustgis_map::camera::Band, f64)>, kept: &[MadeTile], wireframe: bool, ppp: f32, @@ -554,9 +589,21 @@ fn tile_meshes_on( ) -> Option, usize), TessError>> { let side = tile_side(map_per_px)?; let tiles = buckets(features, area, side, band)?; - // The tiles kept from the last scene, as meshing them again would make them. - let taken: Vec> = - tiles.iter().map(|(tile, _)| kept.binary_search_by(|k| k.tile.cmp(tile)).ok().and_then(|i| kept.get(i))).collect(); + let (band, margin) = (band.map(|(b, _)| b), band.map_or(0.0, |(_, m)| m)); + let square = |t: &Tile| { + let [x, y] = t.corner(side); + Extent::new(x, y, x + side, y + side) + }; + // A band's tile wholly on its ground holds every feature reaching it; one on its edge only + // those reaching the ground, grown by the margin. + let cut_for = |t: &Tile| band.filter(|b| !b.ground_holds(&square(t))).map(|b| b.ground); + // The tiles kept from the last scene that hold what meshing them again would draw. + let taken: Vec> = tiles + .iter() + .map(|(tile, _)| { + kept.binary_search_by(|k| k.tile.cmp(tile)).ok().and_then(|i| kept.get(i)).filter(|k| taken_on(k, band, &square(tile), margin)) + }) + .collect(); let to_make: Vec<&(Tile, Vec)> = tiles.iter().zip(&taken).filter(|(_, k)| k.is_none()).map(|(t, _)| t).collect(); let threads = threads.unwrap_or_else(|| threads_for(features, &to_make)); // A tile's own room is the layer's limit, or one buffer's when that is less: not what the @@ -585,7 +632,7 @@ fn tile_meshes_on( Some(k) => k.share(), // A tile that panicked (hostile data) leaves the layer to the CPU, as lyon refusing it. None => match made.next().flatten().unwrap_or(Err(TessError::Refused)) { - Ok(t) => t, + Ok(t) => MadeTile { cut_for: cut_for(&t.tile), ..t }, Err(e) => return Some(Err(e)), }, }; @@ -889,6 +936,64 @@ mod tests { assert_eq!(tiles(2.0), [(-1, -1), (0, -1), (1, -1), (-1, 0), (0, 0), (1, 0)]); } + /// A band's tile on its ground's edge holds the features reaching that ground grown by the + /// band's margin: taken again while the ground in it stays within that, made again once it + /// doesn't, so what it draws holds all a tile made afresh draws. + #[test] + fn a_bands_edge_tile_is_taken_again_only_while_its_ground_lies_within_it() { + use rustgis_map::camera::{Band, Camera}; + let squares: Vec = (0..1600) + .map(|i| { + let (x, y) = (f64::from(i % 40) * 2.5 + 0.5, f64::from(i / 40) * 2.5 + 0.5); + Geometry::Polygon(vec![vec![vec![ + Coord::new(x, y), + Coord::new(x + 0.5, y), + Coord::new(x + 0.5, y + 0.5), + Coord::new(x, y), + ]]]) + }) + .collect(); + let s = symbol(rustgis_map::Rgba::rgb(200, 0, 0), 0.0); + let features: Vec> = squares.iter().map(|g| feature(g, s)).collect(); + let area = Extent::new(0.0, 0.0, 100.0, 100.0); + // A trapezoid, moved north by `dy`. + let band = |dy: f64| Band { + camera: Camera::new(Coord::new(50.0, 50.0 + dy), 1.0), + width: 100.0, + height: 100.0, + depth: (1.0, 2.0), + depth_per_unit: 0.0, + ground: [Coord::new(5.0, 60.0 + dy), Coord::new(95.0, 60.0 + dy), Coord::new(70.0, 20.0 + dy), Coord::new(30.0, 20.0 + dy)], + }; + let margin = 0.5; + let tiles = |b: &Band, kept: &[MadeTile]| { + // Tiles of 8 units: 512 pixels at a 64th of a unit a pixel. + tile_meshes_on(&features, &area, Some((b, margin)), kept, false, 1.0, 1.0 / 64.0, usize::MAX, CAPS, Some(1)).unwrap().unwrap().0 + }; + let vertices = |t: &MadeTile| -> std::collections::HashSet<[u32; 2]> { + match &t.cmd { + Some(DrawCmd::Mesh { vertices, .. }) => vertices.iter().map(|v| v.position.map(f32::to_bits)).collect(), + _ => Default::default(), + } + }; + let first = tiles(&band(0.0), &[]); + assert!(first.iter().any(|t| t.cut_for.is_some()) && first.iter().any(|t| t.cut_for.is_none()), "edge and whole tiles"); + let ptr = |t: &MadeTile| match &t.cmd { + Some(DrawCmd::Mesh { vertices, .. }) => Some(Arc::as_ptr(vertices)), + _ => None, + }; + for dy in [0.25, 12.0] { + let (taken, afresh) = (tiles(&band(dy), &first), tiles(&band(dy), &[])); + assert_eq!(taken.iter().map(|t| t.tile).collect::>(), afresh.iter().map(|t| t.tile).collect::>()); + for (t, a) in taken.iter().zip(&afresh) { + assert!(vertices(a).is_subset(&vertices(t)), "moved {dy}: tile {:?} draws all it would afresh", t.tile); + } + let edge_taken = + taken.iter().any(|t| first.iter().any(|f| f.tile == t.tile && f.cut_for.is_some() && ptr(f).is_some() && ptr(f) == ptr(t))); + assert!(dy > margin || edge_taken, "moved {dy}, within the margin: an edge tile taken again"); + } + } + #[test] fn a_bands_tiles_are_those_of_its_ground_and_only_they_count() { use rustgis_map::camera::{Band, Camera}; @@ -913,11 +1018,11 @@ mod tests { // strip across it, a row of them. let strip = band([Coord::new(0.0, 50.8), Coord::new(100.0, 50.8), Coord::new(100.0, 50.2), Coord::new(0.0, 50.2)]); assert!(buckets(std::slice::from_ref(&whole), &area, 1.0, None).is_none()); - let tiles = buckets(std::slice::from_ref(&whole), &area, 1.0, Some(&strip)).expect("within the caps"); + let tiles = buckets(std::slice::from_ref(&whole), &area, 1.0, Some((&strip, 0.0))).expect("within the caps"); assert_eq!(tiles.len(), 101); assert!(tiles.iter().all(|(t, members)| t.row == 50 && members == &[0])); - // A steep trapezoid's tiles are those touching it, edges and corners too, whatever the - // features reach. + // A steep trapezoid's tiles are those touching it, edges and corners too, each with the + // features whose reach touches it too. let ground = [Coord::new(-3.2, 90.0), Coord::new(97.5, 90.0), Coord::new(60.0, 10.0), Coord::new(40.0, 10.0)]; let trapezoid = band(ground); let small = @@ -925,21 +1030,32 @@ mod tests { let features: Vec> = (0..30).map(|i| small(f64::from(i * 7 % 97), f64::from(i * 13 % 89), f64::from(i % 3) * 2.5)).collect(); let side = 8.0; - let tiles = buckets(&features, &area, side, Some(&trapezoid)).expect("tiles"); + let tiles = buckets(&features, &area, side, Some((&trapezoid, 0.0))).expect("tiles"); let all = buckets(&features, &area, side, None).expect("tiles"); let on = |t: &Tile| { let [x, y] = t.corner(side); trapezoid.ground_touches(&Extent::new(x, y, x + side, y + side)) }; - let want: Vec<_> = all.into_iter().filter(|(t, _)| on(t)).collect(); + let reaches = |k: &usize| { + let f = &features[*k]; + let r = f.reach; + trapezoid.ground_touches(&Extent::new(f.extent.min_x - r, f.extent.min_y - r, f.extent.max_x + r, f.extent.max_y + r)) + }; + let want: Vec<_> = all + .into_iter() + .filter(|(t, _)| on(t)) + .map(|(t, members)| (t, members.into_iter().filter(reaches).collect::>())) + .filter(|(_, members)| !members.is_empty()) + .collect(); assert!(!want.is_empty() && tiles.len() < 100); + assert!(features.iter().enumerate().any(|(k, _)| !reaches(&k)), "a feature off the ground"); assert_eq!(tiles, want); } #[test] fn a_stroke_past_a_bands_far_edge_reaches_as_far_as_its_widening_there() { let (area, mut made) = (Extent::new(0.0, 0.0, 1.0, 1.0), Vec::new()); - let mut tiling = Tiling { area: &area, kept: &[], made: &mut made, band: None, grow: 1.0, spread: 0.0 }; + let mut tiling = Tiling { area: &area, kept: &[], made: &mut made, band: None, grow: 1.0, spread: 0.0, margin: 0.0 }; assert_eq!(tiling.widened(3.0), 3.0, "flat"); tiling.grow = 2.0; assert_eq!(tiling.widened(3.0), 6.0, "at the far edge"); From e4968870ddb47b4d5a0b559afb58d856ef614fa7 Mon Sep 17 00:00:00 2001 From: Dominik Strebel Date: Mon, 30 Nov 2026 04:33:59 +0100 Subject: [PATCH 2/5] fix(render): review round 1 for steep tilted views (pan within the band margin, infinite widths, tests) --- crates/map/src/lib.rs | 15 ++++++++++- crates/map/src/lod.rs | 4 ++- crates/map/src/symbology.rs | 1 + crates/render/examples/vector_layers_bench.rs | 2 +- crates/render/src/layer_meshes.rs | 25 +++++++++++-------- crates/render/src/lib.rs | 5 ++-- crates/render/src/mesh.rs | 5 ++++ crates/render/src/stream_tests.rs | 7 ++++-- crates/ui-egui/src/mapgpu.rs | 7 ++++-- 9 files changed, 52 insertions(+), 19 deletions(-) diff --git a/crates/map/src/lib.rs b/crates/map/src/lib.rs index cfdca251f9..ed6d067b8b 100644 --- a/crates/map/src/lib.rs +++ b/crates/map/src/lib.rs @@ -5403,11 +5403,24 @@ mod tests { } } assert_eq!(lod::DISPLAY_BUILDS.get(), before + 3); - // Each of the three kept (fewer than `LEVELS_KEPT`). assert_eq!(l.display_rejected.len(), 3); l.touch_geometry(); assert!(!l.prepare_display(&flat, 0.001, false)); assert_eq!(lod::DISPLAY_BUILDS.get(), before + 4); + // One more than `LEVELS_KEPT`: the least recently tried goes, the rest stay. + l.touch_geometry(); + let tols: Vec = (0..=lod::LEVELS_KEPT).map(|i| 0.001 * f64::powi(2.0, i as i32)).collect(); + let before = lod::DISPLAY_BUILDS.get(); + for &tol in &tols { + assert!(!l.prepare_display(&flat, tol, false)); + } + assert_eq!(l.display_rejected.len(), lod::LEVELS_KEPT); + for &tol in &tols[1..] { + assert!(!l.prepare_display(&flat, tol, false)); + } + assert_eq!(lod::DISPLAY_BUILDS.get(), before + tols.len()); + assert!(!l.prepare_display(&flat, tols[0], false)); + assert_eq!(lod::DISPLAY_BUILDS.get(), before + tols.len() + 1); } /// A layer held in another system keeps its globe copy in longitude/latitude beside a flat diff --git a/crates/map/src/lod.rs b/crates/map/src/lod.rs index b5f93b167a..d84a21e6fc 100644 --- a/crates/map/src/lod.rs +++ b/crates/map/src/lod.rs @@ -29,7 +29,9 @@ pub const MIN_SAVING: f64 = 0.25; /// How many levels a layer keeps: as many as a tilted view has bands /// ([`crate::camera::MAX_TILT_BANDS`]), each drawn at the level of its own scale, so the bands of /// one view don't push each other's copies out and thin the layer again every frame. The copies -/// stay within [`CACHE_BYTES`], and a far band's are small. +/// stay within [`CACHE_BYTES`], and a far band's are small. It also bounds, for every layer, a +/// streamed source's thinning decisions and the levels found not worth a copy: each as small as +/// a level's key. pub const LEVELS_KEPT: usize = crate::camera::MAX_TILT_BANDS; /// Additional geometry retained for display levels per layer, excluding original/projected data. diff --git a/crates/map/src/symbology.rs b/crates/map/src/symbology.rs index 9e38011ffb..cb34a4ee8d 100644 --- a/crates/map/src/symbology.rs +++ b/crates/map/src/symbology.rs @@ -4012,6 +4012,7 @@ mod tests { class_fills: BTreeMap::new(), other_fills: Vec::new(), expression: None, + keep_order: false, }; assert_eq!(unique(0.5).widest_stroke(GeometryType::Polyline), 3.0); assert_eq!(unique(6.0).widest_stroke(GeometryType::Polyline), 6.0, "the stroke of no class"); diff --git a/crates/render/examples/vector_layers_bench.rs b/crates/render/examples/vector_layers_bench.rs index c814286daf..a5286cdb29 100644 --- a/crates/render/examples/vector_layers_bench.rs +++ b/crates/render/examples/vector_layers_bench.rs @@ -182,7 +182,7 @@ fn main() { } let back: Vec = zoom.iter().rev().skip(1).copied().collect(); zoom.extend(back); - let pitch: f64 = std::env::var("BENCH_PITCH").ok().and_then(|p| p.parse().ok()).unwrap_or(60.0); + let pitch: f64 = std::env::var("BENCH_PITCH").ok().and_then(|p| p.parse().ok()).filter(|p: &f64| p.is_finite()).unwrap_or(60.0); let tilt: Vec = pan(10.0).into_iter().map(|c| Camera { pitch, ..c }).collect(); let sequences: [(&str, Vec); 5] = [ ("whole", (0..4).map(|_| camera(centre, whole)).collect()), diff --git a/crates/render/src/layer_meshes.rs b/crates/render/src/layer_meshes.rs index f12ced1d0d..8f92da0326 100644 --- a/crates/render/src/layer_meshes.rs +++ b/crates/render/src/layer_meshes.rs @@ -519,26 +519,31 @@ mod tests { /// Whether `scene` draws all `afresh` draws, mesh by mesh in the same place: a tilted view's /// band tile taken again may hold features past its ground too, which its depth clipping - /// leaves out, so each of `afresh`'s meshes need only be within `scene`'s there. + /// leaves out, so each of `afresh`'s meshes' triangles (by their vertices' values) need only + /// be among `scene`'s there. fn draws_all_of(scene: &GpuScene, afresh: &GpuScene) -> bool { type Key = ([u64; 2], Option<[u32; 4]>); - let placed = |s: &GpuScene| -> Vec<(Key, std::collections::HashSet<[u32; 4]>)> { + type Triangle = [[u32; 4]; 3]; + let placed = |s: &GpuScene| -> Vec<(Key, std::collections::HashSet)> { s.cmds .iter() .filter_map(|c| match c { - DrawCmd::Mesh { origin, square, vertices, .. } => Some(( - (origin.map(f64::to_bits), square.map(|q| q.map(f32::to_bits))), - vertices - .iter() - .map(|v| [v.position[0].to_bits(), v.position[1].to_bits(), v.color[0].to_bits(), v.stroke_px.to_bits()]) - .collect(), - )), + DrawCmd::Mesh { origin, square, vertices, indices, .. } => { + let at = |i: u32| { + let v = &vertices[i as usize]; + [v.position[0].to_bits(), v.position[1].to_bits(), v.color[0].to_bits(), v.stroke_px.to_bits()] + }; + Some(( + (origin.map(f64::to_bits), square.map(|q| q.map(f32::to_bits))), + indices.chunks_exact(3).map(|t| [at(t[0]), at(t[1]), at(t[2])]).collect(), + )) + } _ => None, }) .collect() }; let (have, want) = (placed(scene), placed(afresh)); - let keys = |m: &[(Key, std::collections::HashSet<[u32; 4]>)]| m.iter().map(|(k, _)| *k).collect::>(); + let keys = |m: &[(Key, std::collections::HashSet)]| m.iter().map(|(k, _)| *k).collect::>(); keys(&have) == keys(&want) && want.iter().all(|(k, w)| have.iter().any(|(h, v)| h == k && w.is_subset(v))) } diff --git a/crates/render/src/lib.rs b/crates/render/src/lib.rs index 6f515f5969..9d41e34d14 100644 --- a/crates/render/src/lib.rs +++ b/crates/render/src/lib.rs @@ -444,8 +444,9 @@ pub const GPU_CULL_MARGIN: f64 = 0.5; /// to about that takes the tile again rather than make it. Less than a flat view's /// [`GPU_CULL_MARGIN`]: at a steep pitch much of a band's tiles lie off its ground, and what an /// edge tile holds past it is meshed for nothing. An eighth was quickest at 60–85° on the -/// benchmark's sheet, its pans a quarter of the view a frame. -const BAND_EDGE_MARGIN: f64 = 0.125; +/// benchmark's sheet, its pans a quarter of the view a frame. A kept tilted scene is panned +/// only within it in the desktop app (`SceneBuffers::fits`), as what lies past it is left out. +pub const BAND_EDGE_MARGIN: f64 = 0.125; /// The map extent a frame drawn for `view` holds features for: the view grown by `margin` /// ([`CULL_MARGIN`], or [`GPU_CULL_MARGIN`] for GPU meshes) on every side. diff --git a/crates/render/src/mesh.rs b/crates/render/src/mesh.rs index f8b1869fed..b7ffb31362 100644 --- a/crates/render/src/mesh.rs +++ b/crates/render/src/mesh.rs @@ -507,6 +507,11 @@ fn ground_reach(layer: &Layer, tiling: &Tiling<'_>, side: f64, wireframe: bool, } // A line's stroke at that width reaches as far as any line's or polygon's (`stroke_reach`). let width = layer.renderer.widest_stroke(layer.geometry_type()).max(if wireframe { 1.0 } else { 0.0 }); + // An infinite width (an unused class, say) bounds nothing: `stroke_reach` gives it no reach, + // which would leave out what a finite stroke reaches. + if !(width * ppp).is_finite() { + return None; + } let widest = Symbol { stroke_width: width, stroke: Rgba::rgb(0, 0, 0), ..Symbol::default_for(rustgis_data::GeometryType::Polyline, 0) }; let reach = tiling.widened(stroke_reach(&Geometry::Line(Vec::new()), &widest, false, ppp, map_per_px)); (reach.is_finite() && reach < side).then_some(reach) diff --git a/crates/render/src/stream_tests.rs b/crates/render/src/stream_tests.rs index 9ec00dfcd3..1292608aeb 100644 --- a/crates/render/src/stream_tests.rs +++ b/crates/render/src/stream_tests.rs @@ -1652,10 +1652,13 @@ fn streamed_lod_retries_failed_decisions_and_bounds_retained_levels() { } // The oldest negative entry was evicted; recomputation may now return a different value. assert!(source.cached_thinning("mercator", 0, || Ok(true)).unwrap()); - // Keep recently reused levels, rather than evicting in insertion order. + // Keep recently reused levels, rather than evicting in insertion order: level 1 went for 0, + // so 2 is the oldest until it is reused, and a new level then pushes out 3 instead. assert!(source.cached_thinning("mercator", 2, || panic!("recent level should remain")).unwrap()); - assert!(source.cached_thinning("mercator", 4, || Ok(true)).unwrap()); + let new_level = rustgis_map::lod::LEVELS_KEPT as i32 + 1; + assert!(source.cached_thinning("mercator", new_level, || Ok(true)).unwrap()); assert!(source.cached_thinning("mercator", 2, || panic!("hit must refresh recency")).unwrap()); + assert!(!source.cached_thinning("mercator", 3, || Ok(false)).unwrap(), "the least recent level is evicted"); } /// A source panic must not disable caching for later successful frames in the same session. diff --git a/crates/ui-egui/src/mapgpu.rs b/crates/ui-egui/src/mapgpu.rs index a27ddfb210..9aba9ee744 100644 --- a/crates/ui-egui/src/mapgpu.rs +++ b/crates/ui-egui/src/mapgpu.rs @@ -1310,9 +1310,12 @@ impl SceneBuffers { turn < 0.5 && (pitch - t.camera.pitch).abs() < 0.5 && (0.7..1.4).contains(&zoom) - // Panned less than a quarter of the view: the ground it drew still covers it. + // Panned less than a quarter of the view, and within what its bands hold past + // their ground (`BAND_EDGE_MARGIN`): the ground it drew still covers it. && (cam.center.x - t.camera.center.x).hypot(cam.center.y - t.camera.center.y) - < 0.25 * self.size[0].min(self.size[1]) * t.camera.res + < (0.25 * self.size[0].min(self.size[1])) + .min(rustgis_engine::render::BAND_EDGE_MARGIN * self.size[0].max(self.size[1])) + * t.camera.res } } } From 6c0b4be9451585b201b4785e50eef1f1d557f3c0 Mon Sep 17 00:00:00 2001 From: Dominik Strebel Date: Mon, 30 Nov 2026 07:15:42 +0100 Subject: [PATCH 3/5] fix(render): review round 2 for steep tilted views (a quarter of the band margin for a pan, tiles within it, the GPU gate in ground_reach) --- crates/render/src/layer_meshes.rs | 2 +- crates/render/src/lib.rs | 22 +++-- crates/render/src/mesh.rs | 9 +- crates/render/src/mesh_tiles.rs | 122 ++++++++++++++++++--------- crates/ui-egui/src/mapgpu.rs | 9 +- crates/ui-egui/tests/gpu_fastpath.rs | 8 +- 6 files changed, 119 insertions(+), 53 deletions(-) diff --git a/crates/render/src/layer_meshes.rs b/crates/render/src/layer_meshes.rs index 8f92da0326..f6a5f9dddb 100644 --- a/crates/render/src/layer_meshes.rs +++ b/crates/render/src/layer_meshes.rs @@ -535,7 +535,7 @@ mod tests { }; Some(( (origin.map(f64::to_bits), square.map(|q| q.map(f32::to_bits))), - indices.chunks_exact(3).map(|t| [at(t[0]), at(t[1]), at(t[2])]).collect(), + indices.as_chunks::<3>().0.iter().map(|t| [at(t[0]), at(t[1]), at(t[2])]).collect(), )) } _ => None, diff --git a/crates/render/src/lib.rs b/crates/render/src/lib.rs index 9d41e34d14..7820304c32 100644 --- a/crates/render/src/lib.rs +++ b/crates/render/src/lib.rs @@ -440,14 +440,23 @@ const READ_ALL_OVER: f64 = 0.5; pub const GPU_CULL_MARGIN: f64 = 0.5; /// How far past its ground (a share of the tilted view's longer side, at its centre's scale) a -/// band's tile on the ground's edge holds features ([`mesh_tiles::Tiling::margin`]): a pan of up -/// to about that takes the tile again rather than make it. Less than a flat view's +/// band's tile on the ground's edge holds features ([`mesh_tiles::Tiling::margin`]). A quarter +/// of it is the room a kept tilted scene is panned in before it is made again +/// ([`band_pan_room`]), the rest how far the ground may move off an edge tile before it is made +/// again. Less than a flat view's /// [`GPU_CULL_MARGIN`]: at a steep pitch much of a band's tiles lie off its ground, and what an /// edge tile holds past it is meshed for nothing. An eighth was quickest at 60–85° on the -/// benchmark's sheet, its pans a quarter of the view a frame. A kept tilted scene is panned -/// only within it in the desktop app (`SceneBuffers::fits`), as what lies past it is left out. +/// benchmark's sheet, its pans a quarter of the view a frame. pub const BAND_EDGE_MARGIN: f64 = 0.125; +/// How far (map units) a kept tilted scene of a `width` × `height` view at `res` map units a +/// point is panned before it is made again: the share of [`BAND_EDGE_MARGIN`] its bands' tiles +/// keep for a pan (`SceneBuffers::fits`, in the desktop and browser apps), as what lies past it +/// is left out. +pub fn band_pan_room(width: f64, height: f64, res: f64) -> f64 { + mesh_tiles::pan_room(BAND_EDGE_MARGIN * width.max(height) * res) +} + /// The map extent a frame drawn for `view` holds features for: the view grown by `margin` /// ([`CULL_MARGIN`], or [`GPU_CULL_MARGIN`] for GPU meshes) on every side. pub fn cull_extent(view: &View, margin: f64) -> Extent { @@ -11899,7 +11908,10 @@ mod tests { tiles += 1; let e = Extent::new(origin[0] + f64::from(*x0), origin[1] + f64::from(*y0), origin[0] + f64::from(*x1), origin[1] + f64::from(*y1)); - assert!(bands[i].ground_touches(&e), "band {i}'s tile {e:?} on its ground"); + // On its ground, or within the room a pan of the kept scene takes past it. + let room = band_pan_room(view.width, view.height, view.camera.res); + let near = Extent::new(e.min_x - room, e.min_y - room, e.max_x + room, e.max_y + room); + assert!(bands[i].ground_touches(&near), "band {i}'s tile {e:?} on its ground"); } assert!(per_band.iter().any(|v| *v > mesh::MAX_LAYER_VERTICES), "a band past one mesh's limit: {per_band:?}"); assert!(tiles > bands.len(), "in tiles: {tiles}"); diff --git a/crates/render/src/mesh.rs b/crates/render/src/mesh.rs index b7ffb31362..a22aa8723a 100644 --- a/crates/render/src/mesh.rs +++ b/crates/render/src/mesh.rs @@ -502,7 +502,14 @@ pub(crate) fn stroke_reach(g: &Geometry, s: &Symbol, wireframe: bool, ppp: f32, /// (`side`) or more: such a stroke makes the layer one mesh, which a feature left out here mustn't /// change. fn ground_reach(layer: &Layer, tiling: &Tiling<'_>, side: f64, wireframe: bool, ppp: f32, map_per_px: f64) -> Option { - if tiling.band.is_none() || layer.graphics { + // Stacked strokes and symbols by feature (`symbol_by`, `size_by`, a class expression) aren't + // in `widest_stroke`: such a layer isn't meshed on the GPU (`render_frame`'s `gpu_layer`), + // and is left unfiltered here too, should it ever be. + let by_feature = layer.has_strokes() + || !layer.symbol_by.is_empty() + || (layer.size_by.is_some() && rustgis_map::symbol_by::SizeBy::applies_to(layer)) + || layer.renderer.expression().is_some(); + if tiling.band.is_none() || layer.graphics || by_feature { return None; } // A line's stroke at that width reaches as far as any line's or polygon's (`stroke_reach`). diff --git a/crates/render/src/mesh_tiles.rs b/crates/render/src/mesh_tiles.rs index 15a30d2527..fcef6fed5c 100644 --- a/crates/render/src/mesh_tiles.rs +++ b/crates/render/src/mesh_tiles.rs @@ -154,8 +154,9 @@ pub(crate) struct Tiling<'a> { /// as tessellated: [`rustgis_map::camera::Band::depth_per_unit`] times `grow`'s scale. pub spread: f64, /// How far past the band's ground (map units) a tile on its edge holds features: a pan moves - /// the ground, and the tile is taken again while the ground in it stays within that - /// ([`MadeTile::cut_for`]). 0 for a flat scene. + /// the ground, and the tile is taken again while the ground near it stays within most of that + /// ([`MadeTile::cut_for`], [`taken_on`]), the rest a kept scene's pan ([`pan_room`]). + /// 0 for a flat scene. pub margin: f64, } @@ -224,16 +225,19 @@ fn buckets( let index = |v: f64| (v / side).floor() as i64; let (in_columns, in_rows) = grid(area, side)?; // A band draws only its own ground: a tile off it shows nothing there, so it is neither made - // nor counted (its cull, the ground's box, is much larger at a steep pitch). Each row's - // tiles on it run unbroken, the ground being convex: their columns, by row. - let on_ground: Option>> = band.map(|(b, _)| { + // nor counted (its cull, the ground's box, is much larger at a steep pitch). The ground is + // grown by the share of the margin a kept scene is panned in ([`pan_room`]), so a pan shows + // no tile that wasn't made. Each row's tiles on it run unbroken, the ground being convex: + // their columns, by row. + let on_ground: Option>> = band.map(|(b, margin)| { + let h = pan_room(margin); in_rows .clone() .map(|row| { let y = row as f64 * side; - match b.ground_across(y, y + side) { + match b.ground_across(y - h, y + side + h) { // A tile whose edge meets the ground's touches it. - Some((lo, hi)) => ((lo / side).ceil() as i64).saturating_sub(1)..=index(hi), + Some((lo, hi)) => (((lo - h) / side).ceil() as i64).saturating_sub(1)..=index(hi + h), None => NO_COLUMNS, } }) @@ -557,20 +561,33 @@ pub(crate) fn tile_meshes( tile_meshes_on(features, area, band, kept, wireframe, ppp, map_per_px, limit, CAPS, None) } +/// How far a kept scene of a band is panned before it is made again: a quarter of its `margin`. +/// The rest is how far the ground may move off an edge tile before it is made again +/// ([`taken_on`]), so the two together stay within what the tile holds. A tile taken again +/// saves far more than a scene kept a little longer, a far band's edge tiles holding much of +/// a layer at a steep pitch. +pub(crate) fn pan_room(margin: f64) -> f64 { + margin / 4.0 +} + /// Whether `kept` holds what a tile of `square` made now for `band` (with its `margin`) would -/// draw: every feature reaching it, or, made on a band's edge, those reaching that band's ground -/// grown by the margin, as long as the ground in the tile now lies within that. A pan moves the -/// ground over an edge tile; what it then holds past the ground the band's depth clipping leaves -/// out. +/// draw, also once the scene is panned by up to [`pan_room`]: every feature reaching it, or, made +/// on a band's edge, those reaching that band's ground grown by the margin, as long as the ground +/// now near the tile lies within the rest of it. A pan moves the ground over an edge tile; +/// what it then holds past the ground the band's depth clipping leaves out. fn taken_on(kept: &MadeTile, band: Option<&rustgis_map::camera::Band>, square: &Extent, margin: f64) -> bool { let (Some(made_for), Some(band)) = (kept.cut_for, band) else { return kept.cut_for.is_none() }; let then = rustgis_map::camera::Band { ground: made_for, ..*band }; - // The ground in the tile, a convex polygon: within the ground then, grown by the margin (a - // convex shape too), when each of its corners is. A ground meeting the tile only along its - // edge or at a corner clips to nothing: the tile is made again, as what reaches that edge - // can't be told from the corners. - let ground = clip_ring(&band.ground, square); - !ground.is_empty() && ground.iter().all(|p| then.ground_touches(&Extent::new(p.x - margin, p.y - margin, p.x + margin, p.y + margin))) + // The ground a pan can bring into the tile is the ground now within `pan` of it, a convex + // polygon: within the ground then, grown by the rest of the margin (a convex shape too), when + // each of its corners is; a pan then keeps it within the whole margin. A ground meeting that + // box only along its edge or at a corner clips to nothing: the tile is made again, as what + // reaches that edge can't be told from the corners. + let pan = pan_room(margin); + let rest = margin - pan; + let near = Extent::new(square.min_x - pan, square.min_y - pan, square.max_x + pan, square.max_y + pan); + let ground = clip_ring(&band.ground, &near); + !ground.is_empty() && ground.iter().all(|p| then.ground_touches(&Extent::new(p.x - rest, p.y - rest, p.x + rest, p.y + rest))) } /// [`tile_meshes`] within `caps`, on `threads` threads, or as [`threads_for`] decides. @@ -942,55 +959,84 @@ mod tests { #[test] fn a_bands_edge_tile_is_taken_again_only_while_its_ground_lies_within_it() { use rustgis_map::camera::{Band, Camera}; + let square = |x: f64, y: f64, side: f64| { + Geometry::Polygon(vec![vec![vec![Coord::new(x, y), Coord::new(x + side, y), Coord::new(x + side, y + side), Coord::new(x, y)]]]) + }; + // Small squares all over, a row of them just north of the margin past the ground's north + // edge (64.4), and one over the whole area, in every tile. let squares: Vec = (0..1600) - .map(|i| { - let (x, y) = (f64::from(i % 40) * 2.5 + 0.5, f64::from(i / 40) * 2.5 + 0.5); - Geometry::Polygon(vec![vec![vec![ - Coord::new(x, y), - Coord::new(x + 0.5, y), - Coord::new(x + 0.5, y + 0.5), - Coord::new(x, y), - ]]]) - }) + .map(|i| square(f64::from(i % 40) * 2.5 + 0.5, f64::from(i / 40) * 2.5 + 0.5, 0.5)) + .chain((0..44).map(|i| square(6.0 + f64::from(i) * 2.0, 64.45, 0.05))) + .chain(std::iter::once(square(0.0, 0.0, 100.0))) .collect(); let s = symbol(rustgis_map::Rgba::rgb(200, 0, 0), 0.0); let features: Vec> = squares.iter().map(|g| feature(g, s)).collect(); let area = Extent::new(0.0, 0.0, 100.0, 100.0); - // A trapezoid, moved north by `dy`. - let band = |dy: f64| Band { - camera: Camera::new(Coord::new(50.0, 50.0 + dy), 1.0), + // A trapezoid, moved east by `dx` and north by `dy`; its north edge just south of a tile's. + let band = |dx: f64, dy: f64| Band { + camera: Camera::new(Coord::new(50.0 + dx, 50.0 + dy), 1.0), width: 100.0, height: 100.0, depth: (1.0, 2.0), depth_per_unit: 0.0, - ground: [Coord::new(5.0, 60.0 + dy), Coord::new(95.0, 60.0 + dy), Coord::new(70.0, 20.0 + dy), Coord::new(30.0, 20.0 + dy)], + ground: [ + Coord::new(5.0 + dx, 63.9 + dy), + Coord::new(95.0 + dx, 63.9 + dy), + Coord::new(70.0 + dx, 20.0 + dy), + Coord::new(30.0 + dx, 20.0 + dy), + ], }; let margin = 0.5; - let tiles = |b: &Band, kept: &[MadeTile]| { - // Tiles of 8 units: 512 pixels at a 64th of a unit a pixel. + // Tiles of 8 units: 512 pixels at a 64th of a unit a pixel. + let tiles_with = |b: &Band, kept: &[MadeTile], margin: f64| { tile_meshes_on(&features, &area, Some((b, margin)), kept, false, 1.0, 1.0 / 64.0, usize::MAX, CAPS, Some(1)).unwrap().unwrap().0 }; + let tiles = |b: &Band, kept: &[MadeTile]| tiles_with(b, kept, margin); let vertices = |t: &MadeTile| -> std::collections::HashSet<[u32; 2]> { match &t.cmd { Some(DrawCmd::Mesh { vertices, .. }) => vertices.iter().map(|v| v.position.map(f32::to_bits)).collect(), _ => Default::default(), } }; - let first = tiles(&band(0.0), &[]); + let first = tiles(&band(0.0, 0.0), &[]); assert!(first.iter().any(|t| t.cut_for.is_some()) && first.iter().any(|t| t.cut_for.is_none()), "edge and whole tiles"); let ptr = |t: &MadeTile| match &t.cmd { Some(DrawCmd::Mesh { vertices, .. }) => Some(Arc::as_ptr(vertices)), _ => None, }; - for dy in [0.25, 12.0] { - let (taken, afresh) = (tiles(&band(dy), &first), tiles(&band(dy), &[])); + for dy in [0.2, 12.0] { + let (taken, afresh) = (tiles(&band(0.0, dy), &first), tiles(&band(0.0, dy), &[])); assert_eq!(taken.iter().map(|t| t.tile).collect::>(), afresh.iter().map(|t| t.tile).collect::>()); - for (t, a) in taken.iter().zip(&afresh) { - assert!(vertices(a).is_subset(&vertices(t)), "moved {dy}: tile {:?} draws all it would afresh", t.tile); + // All the ground can show, panned by up to `pan_room`: its tiles made afresh with that + // as their margin. + for a in &tiles_with(&band(0.0, dy), &[], pan_room(margin)) { + let t = taken.iter().find(|t| t.tile == a.tile); + assert!(t.is_some_and(|t| vertices(a).is_subset(&vertices(t))), "moved {dy}: tile {:?} draws all it would afresh", a.tile); } let edge_taken = taken.iter().any(|t| first.iter().any(|f| f.tile == t.tile && f.cut_for.is_some() && ptr(f).is_some() && ptr(f) == ptr(t))); - assert!(dy > margin || edge_taken, "moved {dy}, within the margin: an edge tile taken again"); + assert!(dy > margin - pan_room(margin) || edge_taken, "moved {dy}, within the margin less the pan: an edge tile taken again"); + } + // A scene of tiles made for one ground, or taken for it from those made for another (as + // far off it as an edge tile is taken again with the whole margin, not the rest), is panned by + // up to `pan_room` before it is made again: whichever way, across the tiles' edges, what + // the ground then shows (its tiles made afresh with no margin) is among its tiles, each + // drawing no more than its own does. + let room = pan_room(margin); + for (made, kept) in [(band(0.0, 0.0), &[][..]), (band(0.0, 0.45), &first[..])] { + let scene = tiles(&made, kept); + let (x, y) = (made.camera.center.x, made.camera.center.y); + for (ex, ey) in [(1.0, 0.0), (-1.0, 0.0), (0.0, 1.0), (0.0, -1.0), (0.7, 0.7), (-0.7, 0.7), (0.7, -0.7), (-0.7, -0.7)] { + let (dx, dy) = (x - 50.0 + ex * room * 0.99, y - 50.0 + ey * room * 0.99); + for a in &tiles_with(&band(dx, dy), &[], 0.0) { + let t = scene.iter().find(|t| t.tile == a.tile); + assert!( + t.is_some_and(|t| vertices(a).is_subset(&vertices(t))), + "panned by ({dx}, {dy}): tile {:?} is in the scene and draws all it would afresh", + a.tile + ); + } + } } } diff --git a/crates/ui-egui/src/mapgpu.rs b/crates/ui-egui/src/mapgpu.rs index 9aba9ee744..07a6e43959 100644 --- a/crates/ui-egui/src/mapgpu.rs +++ b/crates/ui-egui/src/mapgpu.rs @@ -1310,12 +1310,11 @@ impl SceneBuffers { turn < 0.5 && (pitch - t.camera.pitch).abs() < 0.5 && (0.7..1.4).contains(&zoom) - // Panned less than a quarter of the view, and within what its bands hold past - // their ground (`BAND_EDGE_MARGIN`): the ground it drew still covers it. + // Panned less than a quarter of the view, and within the room its bands' tiles + // keep for a pan (`band_pan_room`): the ground it drew still covers it. && (cam.center.x - t.camera.center.x).hypot(cam.center.y - t.camera.center.y) - < (0.25 * self.size[0].min(self.size[1])) - .min(rustgis_engine::render::BAND_EDGE_MARGIN * self.size[0].max(self.size[1])) - * t.camera.res + < (0.25 * self.size[0].min(self.size[1]) * t.camera.res) + .min(rustgis_engine::render::band_pan_room(self.size[0], self.size[1], t.camera.res)) } } } diff --git a/crates/ui-egui/tests/gpu_fastpath.rs b/crates/ui-egui/tests/gpu_fastpath.rs index 2c058a1e8e..2aab5fa8c3 100644 --- a/crates/ui-egui/tests/gpu_fastpath.rs +++ b/crates/ui-egui/tests/gpu_fastpath.rs @@ -446,13 +446,15 @@ fn a_tilted_maps_new_scene_is_made_off_the_ui_thread() { source: egui::MouseWheelSource::Wheel, }); h.step(); - // Zoomed in past 0.7 of the scene's resolution, the scene no longer fits the view. - if h.state().session.map().camera.res < 0.7 * before.res { + // Zoomed in past 0.7 of the scene's resolution, or the centre moved by the zoom at the + // pointer past the room its bands keep for a pan, the scene no longer fits the view. + if h.state().map.drawing() { left = true; break; } } - assert!(left, "the wheel zoomed the tilted map in"); + assert!(left, "the wheel zoomed the tilted map past its scene"); + assert!(h.state().session.map().camera.res < before.res, "zoomed in"); let cam = h.state().session.map().camera; assert_eq!(h.state().map.full_frame_camera(), before, "the frame that outgrew the scene shows it"); assert_eq!(h.state().map.scene_camera(), Some(cam), "the scene on screen follows the camera"); From 49f62412158efbe56ca7a6cf221f13c58308c051 Mon Sep 17 00:00:00 2001 From: Dominik Strebel Date: Mon, 30 Nov 2026 09:09:38 +0100 Subject: [PATCH 4/5] fix(render): review round 3 for steep tilted views (turned views keep their pan, stacked fills unfiltered, zoom test at the centre) --- crates/render/src/mesh.rs | 8 +++++--- crates/ui-egui/src/mapgpu.rs | 10 ++++++---- crates/ui-egui/tests/gpu_fastpath.rs | 12 ++++++------ 3 files changed, 17 insertions(+), 13 deletions(-) diff --git a/crates/render/src/mesh.rs b/crates/render/src/mesh.rs index a22aa8723a..3443969418 100644 --- a/crates/render/src/mesh.rs +++ b/crates/render/src/mesh.rs @@ -502,10 +502,12 @@ pub(crate) fn stroke_reach(g: &Geometry, s: &Symbol, wireframe: bool, ppp: f32, /// (`side`) or more: such a stroke makes the layer one mesh, which a feature left out here mustn't /// change. fn ground_reach(layer: &Layer, tiling: &Tiling<'_>, side: f64, wireframe: bool, ppp: f32, map_per_px: f64) -> Option { - // Stacked strokes and symbols by feature (`symbol_by`, `size_by`, a class expression) aren't - // in `widest_stroke`: such a layer isn't meshed on the GPU (`render_frame`'s `gpu_layer`), - // and is left unfiltered here too, should it ever be. + // What can stroke a feature wider than `widest_stroke` says (stacked strokes and fills, symbols + // by feature: `symbol_by`, `size_by`, a class expression) keeps a layer off the GPU + // (`render_frame`'s `gpu_layer`, which also leaves out what draws no stroke here), and leaves + // it unfiltered here too, should it ever be meshed. let by_feature = layer.has_strokes() + || layer.has_fills() || !layer.symbol_by.is_empty() || (layer.size_by.is_some() && rustgis_map::symbol_by::SizeBy::applies_to(layer)) || layer.renderer.expression().is_some(); diff --git a/crates/ui-egui/src/mapgpu.rs b/crates/ui-egui/src/mapgpu.rs index 07a6e43959..8d70bd40b7 100644 --- a/crates/ui-egui/src/mapgpu.rs +++ b/crates/ui-egui/src/mapgpu.rs @@ -1307,14 +1307,16 @@ impl SceneBuffers { Some(t) => { let turn = rustgis_engine::map::camera::normalize_bearing(cam.bearing - t.camera.bearing).abs(); let zoom = cam.res / t.camera.res.max(1e-12); + let quarter = 0.25 * self.size[0].min(self.size[1]) * t.camera.res; + let band_room = rustgis_engine::render::band_pan_room(self.size[0], self.size[1], t.camera.res); turn < 0.5 && (pitch - t.camera.pitch).abs() < 0.5 && (0.7..1.4).contains(&zoom) - // Panned less than a quarter of the view, and within the room its bands' tiles - // keep for a pan (`band_pan_room`): the ground it drew still covers it. + // Panned less than a quarter of the view, and, tilted, within the room its bands' + // tiles keep for a pan (`band_pan_room`): the ground it drew still covers it. A + // view only turned has no bands. && (cam.center.x - t.camera.center.x).hypot(cam.center.y - t.camera.center.y) - < (0.25 * self.size[0].min(self.size[1]) * t.camera.res) - .min(rustgis_engine::render::band_pan_room(self.size[0], self.size[1], t.camera.res)) + < if t.camera.pitch == 0.0 { quarter } else { quarter.min(band_room) } } } } diff --git a/crates/ui-egui/tests/gpu_fastpath.rs b/crates/ui-egui/tests/gpu_fastpath.rs index 2aab5fa8c3..b520a2548c 100644 --- a/crates/ui-egui/tests/gpu_fastpath.rs +++ b/crates/ui-egui/tests/gpu_fastpath.rs @@ -436,8 +436,10 @@ fn a_tilted_maps_new_scene_is_made_off_the_ui_thread() { let before = h.state().map.full_frame_camera(); assert_eq!(before.pitch, h.state().session.map().camera.pitch, "the scene is the tilted view's"); let mut left = false; + // At the map's centre, the zoom keeps the camera's centre: the zoom alone outgrows the scene. + let center = h.state().map.view_rect.expect("map").0.center(); for _ in 0..400 { - h.input_mut().events.push(egui::Event::PointerMoved(egui::pos2(450.0, 300.0))); + h.input_mut().events.push(egui::Event::PointerMoved(center)); h.input_mut().events.push(egui::Event::MouseWheel { unit: egui::MouseWheelUnit::Point, delta: egui::vec2(0.0, 0.5), @@ -446,15 +448,13 @@ fn a_tilted_maps_new_scene_is_made_off_the_ui_thread() { source: egui::MouseWheelSource::Wheel, }); h.step(); - // Zoomed in past 0.7 of the scene's resolution, or the centre moved by the zoom at the - // pointer past the room its bands keep for a pan, the scene no longer fits the view. - if h.state().map.drawing() { + // Zoomed in past 0.7 of the scene's resolution, the scene no longer fits the view. + if h.state().session.map().camera.res < 0.7 * before.res { left = true; break; } } - assert!(left, "the wheel zoomed the tilted map past its scene"); - assert!(h.state().session.map().camera.res < before.res, "zoomed in"); + assert!(left, "the wheel zoomed the tilted map in"); let cam = h.state().session.map().camera; assert_eq!(h.state().map.full_frame_camera(), before, "the frame that outgrew the scene shows it"); assert_eq!(h.state().map.scene_camera(), Some(cam), "the scene on screen follows the camera"); From d6443e48cbeb8eaeeaa3c0d7f76d183f31a6382e Mon Sep 17 00:00:00 2001 From: Dominik Strebel Date: Mon, 30 Nov 2026 09:39:14 +0100 Subject: [PATCH 5/5] test(render): the band tile test compares triangles; collect_layer and ground_reach docs --- crates/render/src/mesh.rs | 12 +++++++----- crates/render/src/mesh_tiles.rs | 8 ++++++-- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/crates/render/src/mesh.rs b/crates/render/src/mesh.rs index 3443969418..18fe3ea692 100644 --- a/crates/render/src/mesh.rs +++ b/crates/render/src/mesh.rs @@ -502,10 +502,10 @@ pub(crate) fn stroke_reach(g: &Geometry, s: &Symbol, wireframe: bool, ppp: f32, /// (`side`) or more: such a stroke makes the layer one mesh, which a feature left out here mustn't /// change. fn ground_reach(layer: &Layer, tiling: &Tiling<'_>, side: f64, wireframe: bool, ppp: f32, map_per_px: f64) -> Option { - // What can stroke a feature wider than `widest_stroke` says (stacked strokes and fills, symbols - // by feature: `symbol_by`, `size_by`, a class expression) keeps a layer off the GPU - // (`render_frame`'s `gpu_layer`, which also leaves out what draws no stroke here), and leaves - // it unfiltered here too, should it ever be meshed. + // What draws a feature otherwise than `widest_stroke` says (stacked strokes, stacked fills + // with their outlines, symbols by feature: `symbol_by`, `size_by`, a class expression) keeps + // a layer off the GPU (`render_frame`'s `gpu_layer`, which also leaves out what draws no + // stroke here), and leaves it unfiltered here too, should it ever be meshed. let by_feature = layer.has_strokes() || layer.has_fills() || !layer.symbol_by.is_empty() @@ -534,7 +534,9 @@ fn ground_reach(layer: &Layer, tiling: &Tiling<'_>, side: f64, wireframe: bool, /// the rest meshed on several threads, or made one mesh when `tiling` is `None` (a view drawn /// tilted whole); its markers follow them as one command. A tilted view's band /// ([`Tiling::band`]) makes only the tiles on its ground, its strokes reaching as far as the -/// shader widens them, and is one mesh when one reaches a tile side or more. +/// shader widens them, and is one mesh when one reaches a tile side or more; a line or polygon +/// whose widest stroke can't reach that ground, grown by the band's margin, is left out before +/// it is styled ([`ground_reach`]). /// Tiled, its lines and polygons are those within a tile of the cull's tiles /// ([`crate::mesh_tiles::feature_area`]), which may be more than `visible` keeps, out of view. /// `limit` is what is left of the layer's room ([`layer_room`]; its earlier batches took the diff --git a/crates/render/src/mesh_tiles.rs b/crates/render/src/mesh_tiles.rs index fcef6fed5c..f1bba59041 100644 --- a/crates/render/src/mesh_tiles.rs +++ b/crates/render/src/mesh_tiles.rs @@ -992,9 +992,13 @@ mod tests { tile_meshes_on(&features, &area, Some((b, margin)), kept, false, 1.0, 1.0 / 64.0, usize::MAX, CAPS, Some(1)).unwrap().unwrap().0 }; let tiles = |b: &Band, kept: &[MadeTile]| tiles_with(b, kept, margin); - let vertices = |t: &MadeTile| -> std::collections::HashSet<[u32; 2]> { + // A tile's triangles, by their corners' positions: a tile drawing all another does has + // each of its triangles. + let vertices = |t: &MadeTile| -> std::collections::HashSet<[[u32; 2]; 3]> { match &t.cmd { - Some(DrawCmd::Mesh { vertices, .. }) => vertices.iter().map(|v| v.position.map(f32::to_bits)).collect(), + Some(DrawCmd::Mesh { vertices, indices, .. }) => { + indices.as_chunks::<3>().0.iter().map(|c| c.map(|i| vertices[i as usize].position.map(f32::to_bits))).collect() + } _ => Default::default(), } };