Skip to content

Draw circle, polygon and polyline tiles on the GPU - #41

Merged
wf9a5m75 merged 1 commit into
developfrom
feat/gpu-circle-tiles
Nov 13, 2026
Merged

wf9a5m75 merged 1 commit into
developfrom
feat/gpu-circle-tiles

Conversation

@wf9a5m75

@wf9a5m75 wf9a5m75 commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Shape tiles (circles, polygons, polylines) are drawn with Metal instead of CGContext. Without Metal, or with MAPCONDUCTOR_SHAPE_TILE_CPU=1 (for comparison), they are drawn on the CPU as before.

Circles: MetalShapeRasterizer

  • One instance per circle. The fill and the outline are computed from the distance to the centre, with 1 px antialiasing; the outline is centred on the edge. Circles are composited source-over in the given order.
  • Circles of a large set are read as plain values from CircleGeometryFile (ShapeTileStorage.circleValues). Before, each circle was rebuilt as a Shape, which took about 6 µs. Colours are converted to premultiplied RGBA once per palette entry.
  • A circle bent by the projection is drawn as its 128-gon, like a polygon, through the path rasterizer.

Polygons and polylines: MetalPathRasterizer

  • Fill: stencil-then-cover. Each ring becomes a fan of triangles from one point, +1 on front faces and −1 on back faces, which gives the nonzero winding. The shape's box is then painted where the stencil is non-zero, and the stencil is reset to 0.
  • Outline: a quad per segment plus a disc per vertex (round joins and caps) are written to the stencil, then the box is painted once. A translucent outline is not painted twice at its joins, as with strokePath.
  • Antialiasing: 4x MSAA.
  • Batching: shapes whose boxes do not overlap are drawn together. Order only matters where shapes overlap.
  • Polylines (roads overlap too much to batch) each take a stencil number from 1 to 255. Each line is painted where the stencil is not its own number, then sets it. Every line is painted once and lines stack in order. The stencil is cleared every 255 lines.
    • With stencil feedback (Apple GPU family 5 or later), the fragment shader writes the number, so a whole tile of lines is a single draw call.
    • Without it (the simulator), there is one draw per line, with the number as the reference value.

Both

  • A pool of render targets instead of one serial queue. Tiles are drawn in parallel, and they used to wait up to 340 ms for each other.
  • The shaders are compiled in the background when the first ShapeTileRenderer is made. This takes 0.4 s on an iPad Pro 11, and the first tiles used to wait for it.

Measurements

iPad Pro 11 (2018), sample app on MapLibre, built with -O. Times are per-tile draw time for the tiles of the first view, GPU vs CPU (MAPCONDUCTOR_SHAPE_TILE_CPU=1):

page GPU median / p90 / max / total CPU median / p90 / max / total
US Antenna Horizons (723,800 circles) 0 / 45 / 97 / 328 ms 0 / 376 / 1,832 / 4,083 ms
Boston Roads (54,645 polylines) 23 / 36 / 316 / 1,526 ms 30 / 48 / 681 / 2,530 ms
Boston parcels (98,815 polygons) 23 / 48 / 312 / 2,026 ms 32 / 62 / 552 / 3,025 ms
Boston buildings 20 / 36 / 232 / 1,685 ms 24 / 56 / 241 / 1,748 ms

On the largest tiles, building the vertices on the CPU (100 to 250 ms) now takes longer than the GPU draw (about 50 ms).

Checks

  • New MetalShapeRasterizerTests (circles) and MetalPathRasterizerTests (polygons and polylines) draw the same tile on the GPU and on the CPU and compare the pixels. They cover:
    • holes under the nonzero winding;
    • translucent shapes stacking in order;
    • a translucent line crossing itself, painted once, with round joins and caps;
    • Retina tiles;
    • zoomed-out dots.
  • The full suite passes on the iPhone 17 Pro simulator (551 tests). The simulator has no stencil feedback, so it covers the one-draw-per-line path.
  • The stencil-feedback path was checked on the iPad by writing the GPU and CPU tiles of Boston Roads and Boston parcels and comparing them. Pixels that differ by more than 48 levels are at most 1.5% of a tile for roads and 3.9% for parcels, all on shape edges.
  • PolygonTileRendererTests.testOutlineIsLeftOutUnderFourPixels now counts drawn pixels rather than pixels above 128 blue. A 1 px outline on a pixel boundary is drawn at half strength in two columns: 128/128 on the GPU, 127/129 on the CPU. The old threshold only passed on the CPU by that rounding.

A circle tile went through CGContext once per circle, and each circle of a large set was read back from the geometry file as a new Shape (about 6 µs each). On US Antenna Horizons (723,800 circles) a zoomed-out tile took up to 2 s.

MetalShapeRasterizer draws every circle of a tile as one instance (fill and outline by distance, 1 px antialiasing, source-over in the given order) and reads the pixels back for the Rust PNG encoder, like MetalMarkerRasterizer. The circles are read as plain values from the geometry file, and colours are converted once per palette entry. A tile with a circle bent by the projection (drawn as the 128-gon) stays on the CPU, as do tiles without Metal or with MAPCONDUCTOR_SHAPE_TILE_CPU=1.
@wf9a5m75
wf9a5m75 merged commit e588068 into develop Nov 13, 2026
2 checks passed
@wf9a5m75
wf9a5m75 deleted the feat/gpu-circle-tiles branch November 13, 2026 00:54
@wf9a5m75 wf9a5m75 changed the title Draw circle tiles on the GPU Draw circle, polygon and polyline tiles on the GPU Nov 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant