Your browser sucks, JavaScript disabled! Gunnar's console needs JavaScript and WebAssembly. Git over SSH on port 2222 does not.

nordisk/facett

facett-map3d-flight/.nornir/extrude-gpu-for-idiots.md

GPU-compute building extrusion — for idiots

What is this?

Cities have a lot of buildings. Each building starts life as a flat outline (a "footprint") on the map. To draw it in 3D we have to extrude it — push the outline up into a box with walls and a roof. Doing that for tens of thousands of buildings on the CPU is slow. This feature moves the heavy part onto the GPU, which does thousands of buildings at once.

The trick: don't ship geometry, ship instructions

The old way: the CPU builds every wall and roof triangle and hands the GPU a giant pile of vertices. The new way: the CPU just packs the raw outlines into a couple of tidy lists and a little recipe card per building (BuildingMeta: where its outline starts, how tall it is, where its roof triangles are). The GPU reads the recipe and builds the box itself. The CPU stops being a bricklayer and becomes a dispatcher.

The one hard bit it keeps on the CPU

Roofs can be awkward shapes (an L, an H, a courtyard). Chopping an awkward shape into triangles ("triangulation") is fiddly and the CPU is already good at it — so we keep that one step on the CPU (the shared cap_triangulation, reused from the lightweight map) and let the GPU do the easy, repetitive wall-building. Best of both.

How do we know the GPU didn't get it wrong?

We already have a trusted CPU building-extruder (extrude_prism_mesh) that has shipped for ages. So:

  1. We wrote the GPU recipe to build the box exactly the way the trusted CPU builder does — same corners, same order.
  2. A test builds the box both ways and checks they match corner-for-corner. This test needs no graphics card, so it runs on any laptop and goes red instantly if the recipe is wrong.
  3. A second test actually runs it on a real GPU and checks the answer matches. If the machine has no GPU, that test politely skips itself and leaves a note, instead of failing.

The safety net

The old CPU path is still there, unchanged. If a machine has no GPU (a web page, a laptop, a small panel), it just uses the CPU version. Nothing that worked before got slower or broke. We only added a fast lane.