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
76 changes: 53 additions & 23 deletions crates/render/examples/vector_layers_bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,13 @@
//! quarter of the view a step, so every frame brings new features in;
//! - **tilt 10 m:** the same pan at 10 m, tilted 60°;
//! - **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 the same with every layer renamed in each frame
//! too, so that none looks as it did: what each frame drew before a frame kept what still
//! stands (the picture under the changed layer on the CPU, each layer's mesh on the GPU).
//! 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
//! before a frame kept what still stands (the picture under the changed layer, and the layer
//! alone while only its transparency changes; on the GPU each layer's mesh too).
//!
//! `BENCH_RASTER=dem.tif` reads a raster into memory and draws it under the layers, as a basemap
//! (the views are still fitted to the layers).
//!
//! `BENCH_BACKEND=gpu` times the GPU scene instead (`Backend::Gpu`): what the CPU does for a GPU
//! frame, tessellating the layers the GPU draws and drawing the rest into pictures, not the
Expand All @@ -24,7 +28,8 @@
//!
//! Made for swisstopo's swiss-map-vector25 (`SMV25_CHLV95LN02.gpkg`, 336,000 features in LV95,
//! from data.geo.admin.ch, one map sheet). For a profiler, `BENCH_ONLY=2,zoom` (a pixel scale and a sequence) runs
//! that sequence three times on the whole map and nothing else; `BENCH_THREADS=1` draws on one thread.
//! that sequence three times on the whole map and nothing else, `BENCH_ONLY=restyle` the restyle
//! sequences alone; `BENCH_THREADS=1` draws on one thread.
use rustgis_geom::{Coord, lonlat_to_merc};
use rustgis_map::{Basemap, Camera, Map};
use rustgis_render::gpu_scene::DrawCmd;
Expand All @@ -51,7 +56,7 @@ impl Drawn {
let mut d = Drawn::default();
for c in &scene.cmds {
match c {
DrawCmd::Image { pixmap } => {
DrawCmd::Image { pixmap, .. } => {
d.images += 1;
d.image_px += u64::from(pixmap.width()) * u64::from(pixmap.height());
}
Expand Down Expand Up @@ -110,6 +115,13 @@ fn main() {
}
}
println!("read {} layers in {:.1} s", read.len(), t.elapsed().as_secs_f64());
let raster = std::env::var_os("BENCH_RASTER").map(|path| {
let path = std::path::PathBuf::from(path);
match rustgis_data::raster::read_path(&path) {
Ok(r) => (r, path.display().to_string()),
Err(e) => fail(format!("{}: {e}", path.display())),
}
});
// The highest-numbered tables (land cover) at the bottom, as a topographic map stacks them.
let mut datasets: Vec<_> = read.into_iter().filter(|d| !d.features.is_empty()).collect();
datasets.sort_by(|a, b| b.name.cmp(&a.name));
Expand All @@ -124,6 +136,13 @@ fn main() {
let (name, rows) = (d.name.clone(), d.features.len());
layers.push((m.add_dataset(d), name, rows, vertices));
}
if let Some((r, path)) = raster {
println!("under them, the raster {path} ({} × {})", r.width, r.height);
m.add_raster("raster", rustgis_map::RasterLayer::from_path(r, path));
// Added on top; drawn at the bottom.
let r = m.layers.remove(0);
m.layers.push(r);
}
let total: (usize, usize) = layers.iter().fold((0, 0), |(r, v), l| (r + l.2, v + l.3));
println!("{} layers, {} features, {} vertices", layers.len(), total.0, total.1);
if !(extent.min_x.is_finite() && extent.max_x.is_finite() && extent.min_y.is_finite() && extent.max_y.is_finite())
Expand Down Expand Up @@ -171,11 +190,10 @@ fn main() {
("tilt 10 m", tilt),
];
let ids: Vec<u32> = layers.iter().map(|l| l.0).collect();
// One layer alone hides the raster too.
let show = |m: &mut Map, only: Option<u32>| {
for id in &ids {
if let Some(l) = m.layer_mut(*id) {
l.visible = only.is_none_or(|o| o == *id);
}
for l in &mut m.layers {
l.visible = only.is_none_or(|o| o == l.id);
}
};
let view = |camera: Camera, ppp: f64| View { width: 1920.0, height: 1080.0, pixels_per_point: ppp, camera, sky: false };
Expand Down Expand Up @@ -226,29 +244,37 @@ fn main() {
let max = s.last().copied().unwrap_or(0.0);
format!("{:>3} frames, median {median:6.0} ms, slowest {max:6.0} ms, total {:7.0} ms", s.len(), s.iter().sum::<f64>())
};
// The top layer's fill changed in each frame; with `all`, every layer renamed too.
let restyle = |m: &mut Map, cam: Camera, ppp: f64, all: bool| -> Vec<(f64, Drawn)> {
// The top layer's fill changed in each frame, or its transparency (`see_through`); with
// `all`, every layer renamed too.
let restyle = |m: &mut Map, cam: Camera, ppp: f64, see_through: bool, all: bool| -> Vec<(f64, Drawn)> {
let Some(top) = ids.last().copied() else { return Vec::new() };
(0..8u8)
.map(|i| {
if let Some(l) = m.layer_mut(top) {
let fill = rustgis_map::Rgba::rgb(40 + i * 20, 120, 200 - i * 20);
l.renderer = rustgis_map::Renderer::Single(rustgis_map::Symbol { fill, ..l.symbol(0) });
if see_through {
l.transparency = 20.0 + f32::from(i) * 5.0;
} else {
let fill = rustgis_map::Rgba::rgb(40 + i * 20, 120, 200 - i * 20);
l.renderer = rustgis_map::Renderer::Single(rustgis_map::Symbol { fill, ..l.symbol(0) });
}
}
// The raster too, when there is one.
if all {
for id in &ids {
if let Some(l) = m.layer_mut(*id) {
l.name = format!("{} {i}", l.name.trim_end_matches(|c: char| c.is_ascii_digit() || c == ' '));
}
for l in &mut m.layers {
l.name = format!("{} {i}", l.name.trim_end_matches(|c: char| c.is_ascii_digit() || c == ' '));
}
}
run_drawn(m, &[cam], ppp).remove(0)
})
.collect()
};
show(&mut m, None);
// `BENCH_ONLY=restyle` runs the restyle sequences alone.
let restyle_only = std::env::var("BENCH_ONLY").is_ok_and(|o| o == "restyle");
// `BENCH_ONLY` that names no pixel scale and sequence stops the run, not runs everything.
if let Ok(only) = std::env::var("BENCH_ONLY") {
if let Ok(only) = std::env::var("BENCH_ONLY")
&& !restyle_only
{
let Some((ppp, name, cams)) = only.split_once(',').and_then(|(p, n)| {
let cams = &sequences.iter().find(|s| s.0 == n)?.1;
Some((p.parse::<f64>().ok().filter(|p| p.is_finite() && *p > 0.0)?, n, cams))
Expand All @@ -266,7 +292,7 @@ fn main() {
// The first frame projects every layer and builds the whole map's level of detail.
let first = run(&mut m, &[camera(centre, whole)], 1.0);
println!("first frame (projects every layer): {:.0} ms", first.first().copied().unwrap_or(0.0));
for ppp in [1.0f64, 2.0] {
for ppp in [1.0f64, 2.0].into_iter().filter(|_| !restyle_only) {
for (name, cams) in &sequences {
show(&mut m, None);
let drawn = run_drawn(&mut m, cams, ppp);
Expand All @@ -293,18 +319,22 @@ fn main() {
show(&mut m, None);
for ppp in [1.0f64, 2.0] {
for res in [whole, 10.0] {
for all in [false, true] {
let drawn = restyle(&mut m, camera(centre, res), ppp, all);
for (see_through, all, what) in
[(false, false, "the top layer restyled"), (true, false, "its transparency"), (false, true, "every layer changed")]
{
let drawn = restyle(&mut m, camera(centre, res), ppp, see_through, all);
// The first frame of a view keeps; the next seven take what still stands.
let times: Vec<f64> = drawn.iter().skip(1).map(|d| d.0).collect();
let what = if all { "every layer changed" } else { "the top layer restyled" };
println!("{ppp}x restyle at {res:.1} m, {what}: {}{}", summary(&times), scenes(&drawn));
if let Some(l) = ids.last().and_then(|top| m.layer_mut(*top)) {
l.transparency = 0.0;
}
}
}
}
// The layers a GPU scene draws on the CPU: each alone, over the whole map and at 3 m, flat;
// after the sequences, so it warms no cache they time.
if backend.is_gpu() {
if backend.is_gpu() && !restyle_only {
let mut on_cpu = Vec::new();
for (id, name, rows, v) in &layers {
show(&mut m, Some(*id));
Expand Down
Loading
Loading