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
5 changes: 3 additions & 2 deletions crates/render/examples/vector_layers_bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,11 @@ fn main() {
let mut m = Map::new("bench");
m.basemap = Basemap::Blank;
let mut layers = Vec::new();
// The data's extent, longitude/latitude: the sequences are fitted to it.
// The data's extent, longitude/latitude: the sequences are fitted to it. A layer is held in
// its file's own system (a sheet's LV95, say), so its box is converted.
let mut extent = rustgis_geom::Extent::EMPTY;
for d in datasets {
extent = extent.union(&d.extent());
extent = extent.union(&d.extent_to_lonlat(&d.extent()));
let vertices: usize = d.features.iter().filter_map(|f| f.geometry.as_ref()).map(rustgis_map::lod::vertices).sum();
let (name, rows) = (d.name.clone(), d.features.len());
layers.push((m.add_dataset(d), name, rows, vertices));
Expand Down
21 changes: 15 additions & 6 deletions crates/render/src/gpu_scene.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,19 @@ pub enum DrawCmd {
/// picture lives. A see-through layer drawn alone is one at the layer's opacity, so a change
/// of transparency alone draws nothing again.
Image { pixmap: Arc<tiny_skia::Pixmap>, opacity: f32 },
/// Tessellated polygon fills and line/ring strokes of one layer run. Shared: a layer that looks
/// as it did keeps its mesh from one scene to the next (`layer_meshes`), and the GPU keeps the
/// buffers of a mesh it has uploaded while the mesh lives.
Mesh { origin: [f64; 2], vertices: Arc<Vec<MeshVertex>>, indices: Arc<Vec<u32>> },
/// Tessellated polygon fills and line/ring strokes of one layer run, or of one tile of it
/// (`mesh_tiles`). Shared: a layer that looks as it did keeps its meshes from one scene to
/// the next (`layer_meshes`), and the GPU keeps the buffers of a mesh it has uploaded while
/// the mesh lives.
Mesh {
origin: [f64; 2],
vertices: Arc<Vec<MeshVertex>>,
indices: Arc<Vec<u32>>,
/// A tile's mesh is drawn only inside its square (least x and y, greatest x and y, map
/// units from `origin`): its features are cut a stroke's reach past it, and the next
/// tile draws the rest.
square: Option<[f32; 4]>,
},
/// Point markers: instances of a small set of unit-space shape templates, shared as
/// [`DrawCmd::Mesh`] is.
Points { origin: [f64; 2], instances: Arc<Vec<PointInstance>> },
Expand Down Expand Up @@ -141,8 +150,8 @@ impl DrawCmd {
pub fn share(&self) -> Option<DrawCmd> {
Some(match self {
DrawCmd::Image { pixmap, opacity } => DrawCmd::Image { pixmap: Arc::clone(pixmap), opacity: *opacity },
DrawCmd::Mesh { origin, vertices, indices } => {
DrawCmd::Mesh { origin: *origin, vertices: Arc::clone(vertices), indices: Arc::clone(indices) }
DrawCmd::Mesh { origin, vertices, indices, square } => {
DrawCmd::Mesh { origin: *origin, vertices: Arc::clone(vertices), indices: Arc::clone(indices), square: *square }
}
DrawCmd::Points { origin, instances } => DrawCmd::Points { origin: *origin, instances: Arc::clone(instances) },
DrawCmd::Extrusion { .. } => return None,
Expand Down
24 changes: 15 additions & 9 deletions crates/render/src/layer_meshes.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
//! Each layer's GPU mesh kept from one scene to the next (#1057): restyling, editing or
//! filtering one layer of a map drawn on the GPU tessellates that layer again, not every layer.
//!
//! A layer's mesh ([`crate::mesh::collect_layer`]) is made from the layer, the geometry copy it
//! draws, the rows it draws, and the view: which features reach it (the GPU's cull, wider than
//! the view so the camera can move the scene), the 0.35 px decimation, the stroke widths in
//! pixels and where f32 holds its vertices. A kept mesh is taken again when all of those are
//! A layer's mesh ([`crate::mesh::collect_layer`]: its tiles' meshes and its markers) is made
//! from the layer, the geometry copy it draws, the rows it draws, and the view: which features
//! reach it (the GPU's cull, wider than the view so the camera can move the scene), the 0.35 px
//! decimation, the tiles at that scale, the stroke widths in pixels and where f32 holds its
//! vertices. A kept mesh is taken again when all of those are
//! as they were: the layer by its look ([`LayerLook`]), the rest by [`Made`] and the rows drawn.
//! The mesh taken is then the one tessellating the layer again would make, vertex for vertex.
//!
Expand Down Expand Up @@ -125,8 +126,8 @@ impl Meshes {
/// A command sharing `cmd`'s data, when it is a mesh or points.
fn share(cmd: &DrawCmd) -> Option<DrawCmd> {
match cmd {
DrawCmd::Mesh { origin, vertices, indices } => {
Some(DrawCmd::Mesh { origin: *origin, vertices: Arc::clone(vertices), indices: Arc::clone(indices) })
DrawCmd::Mesh { origin, vertices, indices, square } => {
Some(DrawCmd::Mesh { origin: *origin, vertices: Arc::clone(vertices), indices: Arc::clone(indices), square: *square })
}
DrawCmd::Points { origin, instances } => Some(DrawCmd::Points { origin: *origin, instances: Arc::clone(instances) }),
_ => None,
Expand Down Expand Up @@ -349,8 +350,11 @@ mod tests {
use rustgis_geom::{Coord, Extent};
use rustgis_map::{Camera, Map, Renderer, Rgba, Symbol};

/// A square `s` degrees on a side, its south-west corner at `x` east and 0.2° north: clear of
/// the borders of the meshes' tiles at the views here, so a layer is one mesh.
fn square(x: f64, s: f64) -> Geometry {
Geometry::Polygon(vec![vec![vec![Coord::new(x, 0.0), Coord::new(x + s, 0.0), Coord::new(x + s, s), Coord::new(x, s)]]])
let y = 0.2;
Geometry::Polygon(vec![vec![vec![Coord::new(x, y), Coord::new(x + s, y), Coord::new(x + s, y + s), Coord::new(x, y + s)]]])
}

/// A map of two polygon layers drawn on the GPU, `a` under `b`, and a view of them.
Expand All @@ -364,8 +368,8 @@ mod tests {
d.push(Some(square(x, 1.0)), vec![]);
m.add_dataset(d)
};
let (a, b) = (layer("a", 0.0), layer("b", 2.0));
let camera = Camera { center: rustgis_geom::lonlat_to_merc(Coord::new(1.5, 0.5)), res: 2000.0, bearing: 0.0, pitch: 0.0 };
let (a, b) = (layer("a", 0.2), layer("b", 2.2));
let camera = Camera { center: rustgis_geom::lonlat_to_merc(Coord::new(1.7, 0.7)), res: 2000.0, bearing: 0.0, pitch: 0.0 };
(m, a, b, View { width: 400.0, height: 300.0, pixels_per_point: 1.0, camera, sky: false })
}

Expand Down Expand Up @@ -509,6 +513,7 @@ mod tests {
origin: [0.0; 2],
vertices: Arc::new(vec![bytemuck::Zeroable::zeroed(); n]),
indices: Arc::new(Vec::new()),
square: None,
};
let one = bytes_of(&[mesh(4096)]) + ENTRY_BYTES + 1;
// Room for two of the three.
Expand Down Expand Up @@ -579,6 +584,7 @@ mod tests {
origin: [0.0; 2],
vertices: Arc::new(vec![bytemuck::Zeroable::zeroed(); n]),
indices: Arc::new(Vec::new()),
square: None,
};
let size = |n: usize| bytes_of(&[mesh(n)]) + ENTRY_BYTES + 1;
let max = size(6000) + size(3000) + size(500);
Expand Down
96 changes: 59 additions & 37 deletions crates/render/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ mod line_markers;
pub mod logo;
mod mapgrid;
mod mesh;
mod mesh_tiles;
mod native_tiles;
mod pattern_fill;
pub mod pdf;
Expand Down Expand Up @@ -4732,6 +4733,12 @@ fn render_frame(
};
// GPU meshes reach further (`GPU_CULL_MARGIN`), so the camera fast path can move them.
let mesh_cull = cull_extent(view, GPU_CULL_MARGIN);
// A tilted view widens a far stroke past a tile's reach (the shader scales it by depth), so
// it keeps one mesh a layer; a flat one cuts its layers into tiles of the cull. A tilted
// view's bands are drawn as flat views (their cameras look straight down) for the shader to
// tilt, so they're told apart by what their labels are for.
let tilted = view.camera.pitch != 0.0 || matches!(labels_to, Labels::Tilted(..) | Labels::Shared);
let mesh_tiled = (!tilted).then_some(&mesh_cull);
let mesh_visible = |e: &Extent| match &globe {
Some(g) => g.may_see(e),
None => e.intersects(&mesh_cull),
Expand Down Expand Up @@ -5574,9 +5581,10 @@ fn render_frame(
&& layer.renderer.expression().is_none()
&& !upright.contains(&layer.id);
// Lyon refuses degenerate geometry tiny-skia draws fine, a layer too dense to mesh
// (`MAX_LAYER_VERTICES`) is quicker splatted, and zoomed in far, features reaching too
// far for f32 are drawn where they are by the CPU (#902): collect_layer reports all
// three, and the layer runs the CPU branch below like any ineligible one. It meshes the
// (`MAX_LAYER_VERTICES`) is quicker splatted, and zoomed in far, markers reaching too
// far for f32 are drawn where they are by the CPU (#902; lines and polygons are meshed
// in tiles, each from its own corner, `mesh_tiles`): collect_layer reports all three,
// and the layer runs the CPU branch below like any ineligible one. It meshes the
// thinned copy the CPU draws. A turned or tilted view takes no CPU pictures
// (`gpu_turned`), so there such a layer sends the whole view to the CPU renderer, not
// just itself.
Expand Down Expand Up @@ -5616,6 +5624,7 @@ fn render_frame(
extents,
&rows,
&mesh_visible,
mesh_tiled,
ctx.wireframe,
ctx.ppp,
map_per_px,
Expand Down Expand Up @@ -11499,30 +11508,32 @@ mod tests {
}
}

/// So does one with a layer drawn on the CPU for another reason: a polygon reaching too far
/// from its origin for f32, zoomed in all the way. The layer above it is never meshed.
/// So does a turned one with a layer drawn on the CPU for another reason: markers too far
/// apart for f32 from their origin, zoomed in all the way (a tilted view stands markers up,
/// drawn otherwise). The layer above it is never meshed.
#[test]
fn a_turned_or_tilted_view_stops_trying_the_gpu_after_a_layer_drawn_on_the_cpu() {
fn a_turned_view_stops_trying_the_gpu_after_a_layer_drawn_on_the_cpu() {
let mut m = Map::new("Map");
// The world's outline is too far for f32 this far in as well: none.
m.basemap = rustgis_map::Basemap::Blank;
// About 5 km across, over a view of a few centimetres.
square_layer(&mut m, "far", 1.0 - 0.025, 0.5 - 0.025, 0.05);
// One feature's markers about 5 km apart, one of them in a view of a few centimetres.
let mut far = Dataset::new("far", GeometryType::Point, vec![]);
far.push(Some(Geometry::MultiPoint(vec![Coord::new(1.0, 0.5), Coord::new(1.05, 0.5)])), vec![]);
m.add_dataset(far);
square_layer(&mut m, "near", 1.0, 0.5, 1e-7);
let at = lonlat_to_merc(Coord::new(1.0, 0.5));
for (bearing, pitch) in [(30.0, 0.0), (0.0, 50.0)] {
let camera = Camera { center: at, res: rustgis_map::camera::MIN_RES, bearing, pitch };
let view = View { width: 400.0, height: 600.0, pixels_per_point: 1.0, camera, sky: false };
MESHED.with(|m| m.borrow_mut().clear());
let Some(RenderOutput::Cpu(gpu)) = render_backend(&mut m, &view, Backend::Gpu) else {
panic!("turned {bearing}°, tilted {pitch}°: drawn on the CPU")
};
let meshed = MESHED.with(|m| m.borrow().clone());
assert!(meshed.contains(&("far".into(), false)), "turned {bearing}°, tilted {pitch}°: tried: {meshed:?}");
assert!(!meshed.iter().any(|(n, _)| n == "near"), "turned {bearing}°, tilted {pitch}°: went on: {meshed:?}");
let cpu = render(&mut m, &view).expect("cpu");
assert!(gpu.pixmap.data() == cpu.pixmap.data(), "turned {bearing}°, tilted {pitch}°: as the CPU draws it");
}
let (bearing, pitch) = (30.0, 0.0);
let camera = Camera { center: at, res: rustgis_map::camera::MIN_RES, bearing, pitch };
let view = View { width: 400.0, height: 600.0, pixels_per_point: 1.0, camera, sky: false };
MESHED.with(|m| m.borrow_mut().clear());
let Some(RenderOutput::Cpu(gpu)) = render_backend(&mut m, &view, Backend::Gpu) else {
panic!("turned {bearing}°, tilted {pitch}°: drawn on the CPU")
};
let meshed = MESHED.with(|m| m.borrow().clone());
assert!(meshed.contains(&("far".into(), false)), "turned {bearing}°, tilted {pitch}°: tried: {meshed:?}");
assert!(!meshed.iter().any(|(n, _)| n == "near"), "turned {bearing}°, tilted {pitch}°: went on: {meshed:?}");
let cpu = render(&mut m, &view).expect("cpu");
assert!(gpu.pixmap.data() == cpu.pixmap.data(), "turned {bearing}°, tilted {pitch}°: as the CPU draws it");
}

#[test]
Expand Down Expand Up @@ -11639,10 +11650,11 @@ mod tests {
}

#[test]
fn zoomed_in_past_f32_large_features_are_drawn_on_the_cpu_where_they_are() {
fn zoomed_in_past_f32_large_features_are_meshed_in_tiles_near_their_origins() {
// A 10° square's west edge at the deepest zoom (#902): its vertices lie hundreds of km
// from the mesh's origin, which f32 can't hold to a pixel at a tenth of a millimetre per
// pixel, so the layer is drawn on the CPU, with the edge through the middle of the view.
// from the view, which f32 can't hold to a pixel at a tenth of a millimetre per pixel
// from one origin. Cut into tiles (#2541), each mesh holds only its tile's part, from the
// tile's corner: the edge lies where it belongs, through the middle of the view.
let mut m = Map::new("Map");
m.basemap = rustgis_map::Basemap::Blank;
square_layer(&mut m, "s", 0.0, 40.0, 10.0);
Expand All @@ -11652,25 +11664,35 @@ mod tests {
let view = View { width: 400.0, height: 300.0, pixels_per_point: 1.0, camera, sky: false };
let Some(RenderOutput::Gpu(scene)) = render_backend(&mut m, &view, Backend::Gpu) else { panic!("gpu scene") };
let kinds = cmd_kinds(&scene);
assert!(!kinds.contains(&"mesh"), "no mesh f32 would misplace: {kinds:?}");
let Some(pixmap) = scene.cmds.iter().find_map(|c| match c {
gpu_scene::DrawCmd::Image { pixmap, .. } => Some(pixmap),
_ => None,
}) else {
panic!("the square is drawn on the CPU: {kinds:?}")
};
let alpha = |x: u32| pixmap.pixel(x, 150).map_or(0, |p| p.alpha());
assert_eq!(alpha(195), 0, "west of the edge is outside the square");
assert!(alpha(205) > 0, "east of the edge is inside it");
assert!(kinds.contains(&"mesh") && !kinds.contains(&"image"), "meshed: {kinds:?}");
let map_per_px = rustgis_map::camera::MIN_RES;
assert!(gpu_scene::f32_holds(gpu_scene::reach(&scene.cmds), map_per_px), "every tile within f32's reach");
let mut west = f64::INFINITY;
for c in &scene.cmds {
if let gpu_scene::DrawCmd::Mesh { origin, vertices, square, .. } = c {
assert!(square.is_some(), "a tile");
west = vertices.iter().map(|v| origin[0] + f64::from(v.position[0])).fold(west, f64::min);
}
}
assert!(west.abs() < map_per_px, "the edge where it belongs: {west}");

// Zoomed out, the same square is a mesh, and the scene says how far in it holds.
// Zoomed out, the same square is cut into tiles too, and the scene says how far in it
// holds: as far as a tile's side (and its outline's reach past it) from its corner.
let mut camera = view.camera;
camera.res = 1_000.0;
let view = View { camera, ..view };
let Some(RenderOutput::Gpu(scene)) = render_backend(&mut m, &view, Backend::Gpu) else { panic!("gpu scene") };
assert!(cmd_kinds(&scene).contains(&"mesh"));
let half = (rustgis_geom::lonlat_to_merc(Coord::new(10.0, 50.0)).y - rustgis_geom::lonlat_to_merc(Coord::new(0.0, 40.0)).y) / 2.0;
assert!((gpu_scene::reach(&scene.cmds) - half).abs() < 1.0, "reach {} vs {half}", gpu_scene::reach(&scene.cmds));
let side = crate::mesh_tiles::tile_side(1_000.0).expect("tiles");
let reach = gpu_scene::reach(&scene.cmds);
assert!(reach >= side && reach < side * 1.01, "reach {reach} vs a tile's side {side}");

// Tilted, its bands (each drawn as a flat view for the shader to tilt) keep one mesh a
// layer: a far stroke widens past a tile's reach.
let view = View { camera: Camera { pitch: 45.0, ..camera }, ..view };
let Some(RenderOutput::Gpu(scene)) = render_backend(&mut m, &view, Backend::Gpu) else { panic!("gpu scene") };
assert!(scene.tilt.is_some() && cmd_kinds(&scene).contains(&"mesh"), "a tilted scene: {:?}", cmd_kinds(&scene));
assert!(scene.cmds.iter().all(|c| !matches!(c, gpu_scene::DrawCmd::Mesh { square: Some(_), .. })), "no tiles in a tilted view");
}

#[test]
Expand Down
Loading
Loading