Skip to content

perf(render): tessellate a GPU scene's layers on several threads #2541

Open

Description

@pick2510

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

Activity

  1. added
    enhancement
    on Nov 20, 2026
  2. self-assigned this
    on Nov 22, 2026
  3. pick2510 commented on Nov 22, 2026

    @pick2510
    ContributorAuthor

    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 its map_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:

    • the layer's look and rows;
    • the geometry copy and level of detail;
    • the tile's area;
    • the pixel scale.

    It doesn't depend on the view, so one change gives three things:

    • Threads: the tiles of every layer in view are independent.
    • Reuse: a pan meshes only the tiles that come into view, and a zoom within a level reuses them all.
    • Bounds: the vertex limit and f32 precision become per tile, not per layer.

    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 grid rustgis_tiles::template::TileId already 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 TileMeshes already 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:

    • a big polygon isn't tessellated whole in every tile it touches;
    • a stroke's joins and caps at the cut lie outside the square, under the stencil: no seams.

    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 Imprecise goes away for layers: zoomed in all the way, a tile is small. MAX_LAYER_VERTICES becomes a limit per tile, decided by the estimate collect_layer already 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_items threads, with tile_mesh::parallel moved somewhere both can share it.

    • The scene's commands come out in layer order, then tile order (by tile id), so it is the same scene on any number of threads.
    • Scratch per thread is bounded by the per-tile vertex limit (40 bytes a vertex). The threads are capped at 1 + memory::thread_scratch() / (limit × 40).
    • One thread in the browser.
    • A thread the system refuses costs speed only.

    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. mapgpu already keeps a mesh's uploaded buffers by its Arc, 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 of Mesh) carrying the tile number for the stencil test, in the scene's painter's order.

    • Draw calls go from one per layer to one per layer and tile in view: about 30 × layers at 1080p.
    • If that shows in the GPU's time, a layer's tiles can share one buffer with a tile number per vertex.

    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

    1. The grid, the row buckets and tiled meshing on one thread, with the stencil commands. Scenes compared with today's (same features, same colours) on the sample world and the swisstopo sheet. Benchmark: vector_layers_bench, BENCH_BACKEND=gpu.
    2. Threads.
    3. The cache across pans and zooms (extending perf(render): keep each layer's GPU mesh between scenes of the same view (#1057) #2628). The benchmark measures a pan sequence with the cache warm, a pan and a zoom within a level.
    4. Density and precision per tile, so a dense layer zoomed in stays on the GPU. This also bears on perf(render): a tilted view on the GPU backend is drawn twice when a layer can't be a mesh #2540's second step.

    Open questions

    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:

    Sequence CPU backend GPU scene, CPU side
    whole sheet 336 ms 322 ms
    zoom 166 ms 250 ms
    pan 10 m 120 ms 211 ms
    pan 3 m 54 ms 38 ms

    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.

  4. pick2510 commented on Nov 28, 2026

    @pick2510
    ContributorAuthor

    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:

    sequence today prototype
    1× tilt 10 m 2065 ms, every frame on the CPU 900 ms on the GPU
    2× tilt 10 m 3319 ms, every frame on the CPU 940 ms on the GPU

    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

    1. Tiles in each band. Each band is drawn as a flat view of its own (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.
    2. A stroke's reach grown by the band's depth. In perspective the shader widens a stroke by its depth (vs_mesh: stroke_px · map_per_px / 2 · depth), so within a band a stroke reaches up to far / near times 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 at MAX_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").
    3. One scene budget across the bands. The bands are one GPU scene, so scene_vertices is shared: gpu_turned carries what the bands before took into the next (scene_meshed). A layer past it still sends the view to the CPU, as now.
    4. Fewer vertices: tiles within the band's ground. A band's cull is the bounding box of its trapezoid of ground. At a steep pitch the box is much larger than the trapezoid, and much of it is also in the next band's box. Only the tiles touching the trapezoid (grown by the reach) are made. Without this, 18M vertices pass the 16M ceiling on this sheet, and the view would go back to the CPU.

    Steps, each a pull request

    1. Tiles in bands with the grown reach (1, 2) and the shared budget (3), plus tiles within the band's ground (4). Tests: a tilted scene's pixels match the CPU's (the parity harness), a wide stroke at a band's far edge is not cut at a tile border, a layer whose grown reach passes a tile side stays one mesh, the budget spans bands. Benchmark: tilt 10 m at 1× and 2×, and its vertices.
    2. Keeping band tiles between frames (layer_meshes keeps screen scenes only), so a tilted pan meshes only the tiles it brings in.

    Open questions

    • The budget at a steep pitch. If tiles within the ground still leave a tilted scene past the budget on big layers, the far bands could take a coarser level of detail, or a far band's layer could be dropped to the CPU only for that band. That would need the CPU pictures a tilted GPU scene can't take today, so it's for later.
  5. pick2510 commented on Nov 29, 2026

    @pick2510
    ContributorAuthor

    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:

    pitch 1× 2×
    60° 334 ms, 7.4M vertices 362 ms, 6.8M
    70° 2.6 s, 16.8M 1.5 s, 6.7M
    75° 2.6 s, 17.5M 2.2 s, 13.5M
    80° 6.3 s, every frame on the CPU 2.9 s, 16.1M
    85° 6.5 s, every frame on the CPU 3.0 s, 17.8M

    Why

    Timing each band of an 80° frame:

    1. A far band meshes the whole layer. A band's tiles are 512 px at its own scale. At 485 m a pixel (the fifth band at 75°) a tile is about 250 km across, so any tile on the band's ground holds the whole 17 km sheet, and perf(render): a tilted view's bands cut their layers into tiles of their own ground #3281's ground filter, which picks tiles, lets every feature through. At 75° bands 2–4 meshed 4.7M, 3.0M and 2.4M vertices for a ground holding little or none of the sheet.
    2. The bands evict each other's thinned copies. A layer keeps three levels of detail (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.
    3. A band off the layer still walks it. With 2 fixed, a band whose ground holds none of a layer still costs about 80 ms.

    Prototype

    All three, roughly (not a pull request yet):

    1. A feature is bucketed only when its box, grown by its reach, meets the band's ground (Band::ground_across over the box's rows: exact for a convex ground), not only its tile.
    2. A layer keeps as many levels as a view has bands (8). The copies stay within the layer's display budget (lod::CACHE_BYTES), and a far band's levels are small.
    3. A band skips a layer whose extent, grown by a margin, misses its ground.
    pitch 1× 2×
    60° 215 ms, 3.6M vertices 258 ms, 4.1M
    70° 288 ms, 3.8M 355 ms, 4.5M
    75° 277 ms, 3.5M 363 ms, 4.4M
    80° 277 ms, 3.7M 339 ms, 4.4M
    85° 346 ms, 3.6M 388 ms, 4.3M

    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:

    • a far band's tiles hold only the features on its ground, and the scene is the one drawn without the filter where the ground is;
    • the bands of a steep view thin each layer once, not every frame;
    • a band whose ground misses a layer doesn't draw it, and the margin covers the widest symbol (the prototype's 64 px is a guess: it should come from the layer's reach, as the tiles' does);
    • the benchmark at 60–85°, plus a pitch setting in vector_layers_bench to 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.

  6. pick2510 commented on Nov 30, 2026

    @pick2510
    ContributorAuthor

    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 scene render_backend(.., Backend::Gpu) builds), as in the measurements above.

    sequence CPU 1× GPU 1× CPU 2× GPU 2×
    whole sheet 1,420 ms 192 ms 2,320 ms 152 ms
    zoom 610 ms 355 ms 857 ms 288 ms
    pan 10 m 644 ms 180 ms 993 ms 187 ms
    pan 3 m 378 ms 72 ms 642 ms 78 ms
    tilt 10 m, 60° 1,835 ms 389 ms 3,090 ms 348 ms
    tilt 10 m, 80° 2,070 ms 587 ms 4,094 ms 686 ms
    tilt 10 m, 85° 2,172 ms 796 ms 3,990 ms 829 ms

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

enhancement

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions