SPB Git

Repository Public BSD-3-Clause

README.md 13.2 KB 179 lines markdown

1# ORIXAtlas23Prototype of the ORIX Atlas Engine, the future proprietary Metal map renderer of ORIX Maps. **It is not a complete4engine and is not linked into the application.** It draws the fills and lines of real vector tiles (no text, no icons,5no 3D, no interaction) into an offscreen texture, from pieces that are each unit-tested. The specification, the status6of every subsystem and the gap list against MapLibre are in [`docs/ATLAS_ENGINE.md`](../../docs/ATLAS_ENGINE.md).78Depends on `ORIXGeoCore`, `ORIXTileStore` (PMTiles reader, vector tile decoder, LRU cache) and `ORIXMapStyle` (themes).9System frameworks: Metal, CoreGraphics, simd. No third-party code.1011## Public API1213| Type | Role |14|---|---|15| `AtlasCamera` | Centre, fractional zoom (512-point tiles), bearing, pitch: the semantics of `MapCamera`. `normalized`, `projection(viewport:)`. |16| `AtlasProjection` | A camera bound to a viewport: `viewProjection` (doubles), `point(for:)`, `coordinate(at:)`, `worldPoint(at:)`, `groundQuad`, `visibleBounds`, `tileMatrix(for:wrap:)`, `tileUnitsPerPoint(tileZoom:)`. |17| `TileCover` | `tileZoom(forZoom:minZoom:maxZoom:)`, `tiles(for:tileZoom:limit:)` → `[CoveredTile]` (tile + world copy), centre-out, capped. |18| `TileFallback`, `TileClip`, `TileDrawItem` | Replacement of tiles that are not ready by ready children and clipped ancestors. |19| `Triangulator` | Ear clipping with holes on exact integer arithmetic; `Diagnostics` for invalid input. |20| `LineTessellator`, `LineMesh`, `LineVertex`, `LineJoin`, `LineCap` | Polylines → triangles; width-independent geometry (position + extrusion). |21| `AtlasLayer`, `AtlasStyle`, `AtlasStyleTable` | Layer model mirrored from `StyleBuilder`; per-frame table of colour / opacity / width from a `MapTheme` and a zoom. |22| `TileMeshBuilder`, `TileMesh`, `AtlasVertex` | Decoded `VectorTile` → one vertex buffer + one index buffer + ordered draw segments. |23| `AtlasRenderer` (actor) | `render(camera:size:scale:) -> CGImage`, `renderBitmap(...) -> AtlasBitmap`, `setTheme`, `lastFrame`, `cachedMeshBytes`, `corruptTileCount`, `purge()`. |2425```swift26let store = TileStore(archive: try PMTilesArchive(url: montrealURL))27let renderer = try await AtlasRenderer(store: store, theme: BuiltInThemes.day)28let image = try await renderer.render(camera: AtlasCamera(center: downtown, zoom: 14, bearing: 30, pitch: 45),29                                      size: CGSize(width: 393, height: 852), scale: 3)30```3132## Design choices worth knowing3334- **Per-draw style, not per-vertex colour.** A vertex carries a 16-bit layer index; colour, opacity, visibility and line35  width are read by the vertex shader from a 128-row table rebuilt each frame from the theme and the fractional zoom.36  A mesh therefore depends neither on the theme nor on the zoom: switching theme or zooming rebuilds nothing (tested:37  eight themes rendered from the same cached meshes, `tilesBuilt == 0`). `TileMeshBuilder.build` consequently takes no38  `MapTheme`, which deviates from the letter of the brief.39- **Line width in the shader.** Line vertices are `position + extrusion × halfWidth`; the half width in tile units is40  `points × tileUnitsPerPoint / 2`, so a width in points stays exact at every fractional zoom and over the change of41  tile zoom (tested to the pixel on a synthetic tile, zoom 12 to 17.2, flat and pitched at the view centre).42- **One vertex format (12 bytes), one pipeline.** Fills are lines with a zero extrusion. A tile is one vertex buffer,43  one index buffer, and at most 7 indexed draws (fills, then casing and base pass for tunnels, roads, bridges).44- **Clipping with `clip_distance`.** Each draw is clipped to the square of the tile it stands for (or to the part of45  an ancestor), in the vertex stage, without stencil and without `discard`. Neighbours never overlap, so translucent46  layers are never blended twice at tile borders.47- **Antialiasing: 4× MSAA** on the offscreen target, resolved into a shared-storage BGRA8 texture. Lines thinner than48  one pixel are drawn one pixel wide with proportionally reduced alpha. No analytic (distance-based) edge49  antialiasing: at 1× scale line edges show the five coverage levels of 4× MSAA.50- **Colours are blended in sRGB-encoded space** (`bgra8Unorm`, no linearization), as MapLibre does, so opaque pixels51  have exactly the theme's bytes.5253### Shader5455`Sources/ORIXAtlas/Shaders/Atlas.metal` is shipped as a **text resource** (`.copy`) and compiled **at run time** with56`MTLDevice.makeLibrary(source:)`. Reason: `swift build` / `swift test` do not run the Metal compiler on `.metal`57sources (only Xcode's build system produces `default.metallib`), and the prototype must be verified from the command58line. Measured cost: 4.8 ms for the first renderer of a process, 0.2–0.3 ms afterwards (the system shader cache may59already have been warm; a truly cold cache was not measured). A product build should precompile a `.metallib`.6061## Tests6263```64cd Packages/ORIXAtlas65swift test                                              # 72 tests run, 4 benchmark tests skipped66ORIX_ATLAS_BENCH=1 swift test -c release --filter Benchmarks67```6869Result on 2026-11-25 (Apple M5 Max, macOS 27.0, Swift 6.4.2): **76 tests in 10 suites, 72 passed, 4 skipped70(benchmarks, opt-in), 0 failed**, 7.8 s in debug. Tests that need `data/build/montreal.pmtiles` are disabled by71`.enabled(if: TestData.hasMontreal)` (9 of the 72, plus the 4 benchmarks); tests needing Metal by `.enabled(if: AtlasRenderer.isMetalAvailable)`.72The skip path with the Montréal file absent is by construction and was **not** exercised (the file was present).7374What the tests establish:7576- Camera: centre ↔ view centre, `coordinate(at:)` / `point(for:)` round trip under 1e-5 point for zoom 6.5–20,77  bearing 0–359, pitch 0–60; bearing and pitch directions; world wrap; nil beyond the poles; float tile matrix within78  0.05 point of the double projection for a z15 tile at zoom 20, pitch 55.79- Tile cover: zoom 0, world copies, antimeridian, pole clamp, every visible ground point covered (flat, rotated,80  pitched), centre-out order, cap; fallback to clipped ancestors and ready children.81- Triangulation: exact area and no triangle outside on hand-made and 400 random simple polygons (with holes);82  degenerate, self-touching, self-intersecting, extreme-coordinate and 1 500 random garbage inputs never trap and stay83  in range; a 20 000-point random ring finishes (7.5 s in debug). On real Montréal data: **128 170 polygons of the84  buildings, landuse, water and earth layers of 146 tiles, 128 170 triangulated with exactly the polygon's area**, no85  diagnostic.86- Lines: quad, miter, miter limit → bevel, round joins and caps, closed rings, degenerate input, area and distance87  properties.88- Mesh: layer order, segments, vertex layout (12 bytes), extent normalization, coordinate clamping, 600 byte-flipped89  copies of a real tile never trap.90- Renderer (pixels): water, land and motorway colours at known Montréal coordinates at zoom 10, 12, 13.5 and 15;91  the same places found at `point(for:)` in rotated and pitched frames (GPU matrix = CPU conversions); land / water of92  the world map, antimeridian, three world copies at zoom 0; no seam on uniform land across a tile border (flat,93  rotated, pitched); line and casing width to the pixel; hairline coverage; clipping of buffer geometry; corrupt tile94  counted once and replaced by its parent; cache hit renders an identical frame; 2 MB cache budget holds.9596PNG files are written to `Artifacts/` (git-ignored) by the renderer tests: `montreal-z10-day`, `-z12-day`, `-z14-day`,97`-z16-day`, `-z17.5-overzoom-day`, `-z15.5-bearing30-pitch60-day`, `-z12.5-bearing300-pitch45-day`, `montreal-z12.3-`98`{day,midnight,terrain,minimal,transit,topographic,explorer,highcontrast}`, `montreal-archive-toronto-z11-fallback`,99`world-z1.6-day`, `world-z1.6-bare`, `world-antimeridian-z3.2`, `world-z0-copies` (20 files).100101## Measurements102103**Mac numbers only: Apple M5 Max (18 cores, 48 GB), macOS 27.0, release build, 2026-11-25. Nothing was measured on an104iPhone**, where CPU, GPU, memory bandwidth and thermal limits are different; do not extrapolate.105106Command: `ORIX_ATLAS_BENCH=1 swift test -c release --filter Benchmarks` (one run; timings vary by a few percent107between runs). Tile set: the tiles of zoom 12–15 covering 45.47–45.56° N, 73.66–73.50° W (downtown Montréal, Mount108Royal, the river), at most 64 per zoom evenly spaced: 146 tiles (`TestData.benchmarkTiles()`).109110### Mesh build per tile (single thread, best of 3 per tile)111112| Zoom | Tiles | Decode p50 / p95 (ms) | Mesh build p50 / p95 (ms) | Vertices p50 / max | Indices p50 / max | Buffer bytes p50 / max |113|---|---|---|---|---|---|---|114| 12 | 6 | 0.65 / 0.75 | 1.74 / 2.54 | 45 823 / 62 590 | 116 751 / 162 984 | 1 016 880 / 1 403 016 |115| 13 | 20 | 0.42 / 0.64 | 1.33 / 2.25 | 42 737 / 78 782 | 107 556 / 203 679 | 943 068 / 1 760 100 |116| 14 | 56 | 0.45 / 0.64 | 1.60 / 3.37 | 44 592 / 80 289 | 118 737 / 225 024 | 1 006 512 / 1 863 564 |117| 15 | 64 | 0.95 / 2.00 | 0.78 / 1.38 | 22 811 / 44 491 | 58 233 / 117 699 | 507 000 / 1 004 688 |118| all | 146 | 0.51 / 1.79 | **1.11 / 2.74** | | | |119120"Decode" is `VectorTile(data:)` on already inflated bytes (`ORIXTileStore`); "mesh build" is `TileMeshBuilder.build`121(triangulation + line tessellation + buffer assembly, building outlines included).122123### Triangulation alone (best of 5)124125| Rings | Polygons | Ring points | Largest polygon | Triangles | Time | Throughput |126|---|---|---|---|---|---|---|127| buildings | 104 180 | 1 225 486 | 272 points | 910 351 | 71.7 ms | 12.7 M triangles/s |128| landuse | 22 975 | 288 527 | 911 points | 219 836 | 19.5 ms | 11.3 M triangles/s |129130These rings are small (the tile builder already cuts and simplifies them); the largest in the set has 911 points, so131the numbers say nothing about the behaviour on rings of tens of thousands of points.132133### Offscreen frame, 1179 × 2556 px (393 × 852 pt at 3×), 4× MSAA, ORIX Day134135Cold = new archive mapping, empty decoded-tile cache, empty mesh cache, first frame (tile read + inflate + decode +136mesh build on the task pool + upload + texture allocation + draw + read-back; shader compilation excluded, see above).137Warm = 60 further frames of the same camera from cached meshes.138139| Camera | Tiles | Draw calls | Triangles | Cold (ms) | of which tiles (ms) | Warm p50 / p95 (ms) | Encode p50 | GPU p50 | Read-back p50 | Mesh cache |140|---|---|---|---|---|---|---|---|---|---|---|141| z10 | 6 | 18 | 209 471 | 22.1 | 8.1 | 4.30 / 5.53 | 0.07 | 0.60 | 3.03 | 4.28 MB |142| z12 | 6 | 40 | 290 401 | 27.1 | 11.6 | 4.90 / 6.37 | 0.06 | 0.61 | 3.60 | 5.53 MB |143| z12.9 | 4 | 28 | 226 526 | 15.8 | 5.2 | 3.59 / 3.86 | 0.03 | 0.47 | 2.55 | 4.27 MB |144| z14 | 6 | 42 | 318 316 | 18.4 | 6.7 | 4.12 / 4.30 | 0.03 | 0.58 | 2.98 | 6.66 MB |145| z16 (z15 tiles) | 2 | 14 | 55 960 | 13.1 | 3.6 | 3.01 / 3.15 | 0.02 | 0.47 | 2.00 | 1.12 MB |146| z15.5, bearing 30, pitch 60 | 9 | 59 | 202 375 | 16.7 | 6.1 | 3.60 / 3.86 | 0.04 | 0.53 | 2.51 | 4.29 MB |147148The warm frame is dominated by the read-back (allocating and copying 12 MB), which an on-screen frame does not pay:149CPU encoding is under 0.1 ms and the GPU time reported by Metal about 0.5–0.6 ms on this Mac. Triangle counts include150the casing pass and layers hidden at that zoom (submitted, collapsed in the vertex shader).151152### Memory of cached meshes153154Cached mesh = vertex buffer (12 bytes per vertex) + index buffer (4 bytes per index); nothing else is retained per tile.155The 146 benchmark tiles together: 5 041 394 vertices, 13 226 406 indices, **113.4 MB, 0.78 MB per tile on average**156(0.5 MB at zoom 15, about 1 MB at zoom 12–14). One phone frame holds 1.1–6.7 MB (table above). Default cache budget:157256 MB (`AtlasRenderer.Configuration.meshCacheBytes`), LRU. The decoded-tile cache of `TileStore` (48 MB by default)158is separate and estimated by that module, not measured here.159160## Worked around locally (missing in other modules)161162- `ORIXMapStyle` exposes the layer model only as a MapLibre style document: the layer order, the `kind` filters and163  the width ramps of `StyleBuilder.Builder` are internal, so `AtlasStyle` **duplicates** them (road stops, land use164  groups, opacity ramps). They can drift; a renderer-neutral layer description in `ORIXMapStyle` would remove this.165- `ORIXTileStore`: `VectorTile`, `Layer` and `Feature` have no public memberwise initializer and there is no PMTiles166  writer, so the tests carry a small MVT encoder and a PMTiles v3 writer (`Tests/…/Support.swift`). `LRUCache`167  requires `Sendable` values, hence `GPUTileMesh: @unchecked Sendable` (immutable after creation). `LRUCache.insert`168  scans all entries to find the victim (fine at the prototype's sizes).169- `ORIXMapRenderer` is not a dependency (it links MapLibre), so `AtlasCamera` mirrors `MapCamera` instead of using it;170  the adapter belongs in the future `MapBackend` implementation.171172## Known limits173174Everything listed "not started" or "partial" in the specification. In particular: no labels, icons, dashes, building175extrusion, terrain, overlays, picking, on-screen view or gestures; single archive (no base + region composition);176tiles of one zoom per frame (no level of detail under pitch); tiles are loaded before the frame is drawn (the offscreen177path waits, so the fallback logic is only exercised for absent and corrupt tiles); vertex positions are 16-bit, a178coordinate beyond ±32 767 tile units is clamped and counted.179
Files main
Menu

Theme