Repository navigation
Description
With the GPU backend on (the status bar's CPU/GPU switch, or RUSTGIS_GPU), each full redraw tessellates every layer's geometry into meshes on one thread. Zoomed out, that makes a GPU redraw slower than drawing the frame on the CPU.
Measured
vector_layers_bench (#2525, with BENCH_BACKEND=gpu, #2539) on swisstopo's swiss-map-vector25, one map sheet (66 layers, 335,000 features, 3.0 million vertices). This is the CPU side of a GPU frame (the scene render_backend(.., Backend::Gpu) builds), not the device's upload and draw. Median frame, one build, CPU backend drawing in bands on 16 threads:
| Sequence | CPU 1× | GPU 1× | CPU 2× | GPU 2× |
|---|---|---|---|---|
| whole sheet | 336 ms | 322 ms | 828 ms | 613 ms |
| zoom | 166 ms | 250 ms | 293 ms | 278 ms |
| pan 10 m | 120 ms | 211 ms | 293 ms | 283 ms |
| pan 3 m | 54 ms | 38 ms | 158 ms | 49 ms |
The GPU wins zoomed in and at 2×: its cost doesn't grow with the pixels. Zoomed out at 1× it is up to 75% slower. A zoomed-out scene holds 3.2–4.9 million vertices (about 130–195 MB at 40 bytes a vertex).
While the map moves, the app moves the last scene's meshes without building anything (the camera fast path). These numbers are the redraw when it stops, or when the view leaves the meshes' reach.
Where
render_frame's GPU branch (crates/render/src/lib.rs, mesh::collect_layer for each layer) and crates/render/src/mesh.rs. Each layer's tessellation is independent of the others. Vector tiles already build their meshes on several threads (tile_mesh.rs).
Proposal
Tessellate the layers, or a big layer's features in chunks, on rustgis_data::threads::max() threads (AGENTS.md "Use the threads"):
- the same scene as one thread, command for command, in layer order;
- the layer's vertex room (
MAX_LAYER_VERTICES) decided as one thread decides it; - one thread below a size threshold and in the browser;
- scratch within
memory::thread_scratch; - a thread the system refuses costs speed only.
The CPU parts of the scene (layers drawn as pictures) stay where they are in the order.
Keeping a layer's meshes between redraws is #1057's step 2.4, and goes with it.
Tests
- A scene built on 1 and several threads is the same.
- A layer over its vertex room falls back as on one thread.
- The benchmark's GPU sequences before and after.
Related
- The benchmark's GPU option is merged (perf(render): the vector layers benchmark times GPU scenes and tilted views #2539):
BENCH_BACKEND=gpu cargo run --release -p rustgis-render --example vector_layers_bench -- map.gpkg. - perf(render): a tilted view on the GPU backend is drawn twice when a layer can't be a mesh #2540's second step decides before tessellating whether a layer is worth a mesh (features in view and their size on screen) instead of after
MAX_LAYER_VERTICESis reached. That takes the biggest zoomed-out layers out of tessellation altogether, so it comes before this and changes what is left to split; measure again after it. - Keeping a layer's meshes between redraws is Tracking: redraw only the layer that changed (per-layer rendering cache) #1057's step 2.4 (measurements posted there).
Activity
pick2510 commentedon Nov 22, 2026
I'm taking this. Below is a design for review before any code: meshing by tile rather than by layer, so the work splits across threads and the meshes can be reused while the map moves.
Why tiles, not layers
Since #2628, a GPU scene keeps each layer's mesh for the next scene of the same view. Its description says what that doesn't cover: "Pans and zooms still tessellate … Reusing meshes while navigating needs them made by tile or band." A mesh made for one view (culled to it plus
GPU_CULL_MARGIN, decimated at itsmap_per_px) doesn't serve the next. Splitting a layer's features across threads would speed each redraw up, but every pan would still tessellate everything.A tile's mesh depends only on:
It doesn't depend on the view, so one change gives three things:
Design
1. The grid. Square tiles in the flat projection's units, a power of two on a side, aligned with the level-of-detail levels (
lod::tolerance(level)= 2^level units). In Web Mercator that is the XYZ gridrustgis_tiles::template::TileIdalready uses. A frame takes the tile size that puts a tile at about 512 physical pixels, and the level chosen so the tile is decimated at no coarser than today's 0.35 px at the finest zoom it serves. Zooming within a level then reuses the tiles; crossing a level makes the next level's.2. Which features a tile holds. Today each frame culls every row against its cached extent, which is fine once per frame but not once per tile. Per layer and level, the rows are bucketed by the tiles their extent touches: a row index per tile, built once and kept while the geometry and level are the same. As a multiple of the layer that is 4 bytes a row per tile it touches, so a few bytes a row (AGENTS.md "Size for real data"); a feature spanning very many tiles is bucketed at a coarser level instead.
3. Borders: stencil, as vector tiles do. Tile squares don't overlap, so each tile writes its number into the stencil buffer once a scene. Every layer's mesh for that tile then draws only where the number is, as
TileMeshesalready draw (mapgpu.rs, the "tile's square writes its number into the stencil" pipeline). A feature crossing a border is clipped to the tile grown by its stroke's reach before tessellating, so:The 8-bit stencil limits a scene to 255 tiles: at 512 px with the scene's margin that is about 30 tiles at 1920×1080, and 120 at 4K. A finer tile size needs the count kept under that.
4. Precision and density per tile. Each tile's mesh has its own origin (the tile's corner), so
Imprecisegoes away for layers: zoomed in all the way, a tile is small.MAX_LAYER_VERTICESbecomes a limit per tile, decided by the estimatecollect_layeralready makes before tessellating. At first, a layer with any tile too dense is drawn on the CPU as now; later, only that tile's features.5. Threads. The (layer, tile) pairs missing from the cache are meshed on
rustgis_data::threads::for_itemsthreads, withtile_mesh::parallelmoved somewhere both can share it.1 + memory::thread_scratch() / (limit × 40).6. The cache. #2628's per-layer mesh cache, keyed by tile id instead of the view, under its byte cap (128 MiB on the desktop, 32 MiB in the browser), oldest first.
mapgpualready keeps a mesh's uploaded buffers by itsArc, so a tile kept is also not uploaded again. A scene covers the view plus its margin (scene_covers) with whole tiles.7. Drawing. A new
DrawCmd(or an extension ofMesh) carrying the tile number for the stencil test, in the scene's painter's order.What changes on screen
The GPU backend's meshes are decimated per level rather than per view, at 0.35 px or finer. Features are cut at tile borders under the stencil, which should be invisible. The CPU backend, exports, the command line and Python are unaffected: the GPU backend is opt-in and draws only on screen.
Steps, each a pull request
vector_layers_bench,BENCH_BACKEND=gpu.Open questions
TileIdscheme and the same stencil pipeline for layers read into memory, so the two meet rather than duplicate.Measured today, for comparison
From #2539 on swisstopo's swiss-map-vector25 (one map sheet, 66 layers, 3.0 million vertices). These are the full redraws that panning and zooming cause now. Median frame, 1920×1080:
The target: a pan that reuses most of its tiles takes a fraction of the GPU column, and a cold redraw scales with the threads.
fix(render): tiles kept off tilted bands, one limit on any thread cou…
perf(render): a GPU layer's meshes cut into tiles and made on several…
pick2510 commentedon Nov 28, 2026
Next: tiles in tilted views
Steps 1–4 are in (#3122, #3212, #3267). The slowest GPU sequence left is a tilted view, which is drawn wholly on the CPU.
Measured
vector_layers_bench, swisstopo sheet 216,BENCH_BACKEND=gpu, median frame:Why the CPU: a tilted view's bands each make one mesh a layer (no tiles), so the buildings layer in the near band (8.3 m a pixel) passes
MAX_LAYER_VERTICES. A tilted view takes no CPU pictures, so that one layer sends the whole view to the CPU (#2540).The prototype only turns tiles on in the bands. It shows the gain, but it isn't correct (reach below) and its scene holds 18M vertices: band 0 (2,952 × 1,440 px at 8.3 m) 6.7–8.9M, band 1 (2,087 × 945 px at 21.8 m, about the whole sheet) 8.9M.
Design
band_view) and keeps its depth slice in the shader (CmdClip), so it cuts its layers into tiles at its own scale, as a flat view does. The stencil needs nothing new: a layer tile's square sets and clears one bit around its own mesh (MESH_SQUARE_BIT), so the bands' tiles don't meet.vs_mesh:stroke_px · map_per_px / 2 · depth), so within a band a stroke reaches up tofar / neartimes as far as tessellated (2× for a middle band, up to 2.5× for the last, more when the last band takes what is left atMAX_TILT_BANDS). A long band drawn at a lower pixel density also scales its strokes up by the pixel ratio (gpu_turned). A feature's reach is grown by both, and a layer whose grown reach passes a tile side stays one mesh, as today. This is what keeps tilted views untiled today ("far strokes wider than a tile's reach").scene_verticesis shared:gpu_turnedcarries what the bands before took into the next (scene_meshed). A layer past it still sends the view to the CPU, as now.Steps, each a pull request
layer_mesheskeeps screen scenes only), so a tilted pan meshes only the tiles it brings in.Open questions
pick2510 commentedon Nov 29, 2026
Tilted views: steep pitches (step 3)
With #3281 and #3349 (in the merge queue), a tilted view at the benchmark's 60° stays on the GPU: tilt 10 m is 334 ms at 1× and 362 ms at 2×, from 2065 and 3319 ms on the CPU. At a steeper pitch it doesn't hold.
Measured
vector_layers_bench, swisstopo sheet 216,BENCH_BACKEND=gpu, tilt 10 m at other pitches (a local switch in the bench), median frame:Why
Timing each band of an 80° frame:
lod::LEVELS_KEPT). At 80° there are six bands, each at its own level, so each band thins every layer again on every frame: about 230 ms a band, also for a band that draws nothing.Prototype
All three, roughly (not a pull request yet):
Band::ground_acrossover the box's rows: exact for a convex ground), not only its tile.lod::CACHE_BYTES), and a far band's levels are small.Every frame stays on the GPU, peak memory 1.0–1.2 GB (1.3–2.3 GB before). Flat pan and zoom are unchanged (pan 10 m 171–179 ms, zoom 281–370 ms).
Next
One pull request with the three, each tested:
vector_layers_benchto run it.The coarser level of detail for far bands, the open question in the plan above, isn't needed for this sheet: the far bands were slow for what they meshed off their ground, not for their detail.
perf(render): a steep tilted view's far bands mesh only what reaches …
perf(render): a steep tilted view's far bands mesh only what reaches …
pick2510 commentedon Nov 30, 2026
Where this stands
With #3392 merged, every step planned here is in:
Measured
vector_layers_bench, swisstopo sheet 216 (66 layers, 335,173 features), 1920×1080, 16 threads, main at 4672e90. Median frame, the mean of two full runs (tilts at 80° and 85°: of the second and third of three). The GPU column is the CPU side of a GPU frame (the scenerender_backend(.., Backend::Gpu)builds), as in the measurements above.The GPU scene is now quicker than the CPU frame in every sequence, at 1× and 2×: 1.7–15 times. The CPU frames on this machine are slower than in the table at the top (whole sheet 1,420 ms against 336 ms); a build from 27 November gives the same, so the ratio above is the one to read, not the old absolute numbers.
Memory: peak resident memory over the full run is 2.40 GB with the GPU scene and 0.81 GB on the CPU. A layer keeping eight levels of detail instead of three (#3392) leaves the GPU peak unchanged (2.40 GB, from 2.41–2.44) and adds 45 MB (7%) on the CPU's zoom sequence, within each layer's 64 MB budget.
Left, not needed on this sheet
The slowest GPU sequences left are zoom (a new level each step) and the steepest tilts. Unless the maintainers want one of the points above here, this can be closed, and a new issue can take any of them.
refactor(render): one kept-mesh floor; draws_all_of's doc
refactor(render): one kept-mesh floor; draws_all_of's doc (opengeos#3441