# Anti-aliasing (2026-10-27) Status: **implemented, on by default** (`client/src/renderer/aa-pass.ts`). Settings → Quality → Anti-aliasing; bench `set_debug {antiAlias: true|false}` (session override, not persisted) and `set_debug {aaDebug: 1}` (the stage mask, below). Branch `aa-proto`; the diagnosis and survey it builds on are `docs/aa-survey.md` on branch `aa-research` (236398e1). Scratch, drivers, raw shots and sheets: `logs/aa-proto/` on nazrin. The owner's goal: "the per-voxel shadows aren't what I was talking about there. That's an intentional aesthetic. The issue is just the uneven stepping of voxel borders on transitions from one color to another … I'll be happy so long as we get the pixel stepping to look less harsh and obvious." So the per-cell shading and shadow quantisation stay exactly as lit; only the *pixel* stair-steps along each border between differently coloured cells — silhouettes, material borders, shadow-cell borders, entity edges — are smoothed. ## 1. What runs Two history-free stages between the lighting resolve and the blit (so the debug HUD, visual cards and brush overlays, all drawn after the blit, are never touched; the vt debug views turn AA off): 1. **Cell coverage** (`cell_main`). A pixel whose 3×3 sees more than one shading cell (VSC slot, sky or slot-less hit) and more than 2.5/255 of colour spread gathers the 3×3's visible cell faces: - the face is the one the neighbour's hit point lies on, of the cell its VSC key names (local coord + snap → the 4^ml cell), in its part's frame (`parts[partId]`: terrain regions and entities alike — one rule); - coplanar cells of one colour (≤ 4/255) merge into the bounding rectangle of their faces; - each face rectangle is intersected with the pixel's centre ray and its two ray differentials: the signed distances of the pixel centre to the rectangle's edges, in pixels, give the coverage (a 1-px linear ramp per edge, the product over the two in-plane axes); - faces are composited front to back by the depth where the pixel's ray meets their plane (the pixel's own face first); each takes what it covers of the footprint still free, the centre's colour keeps the rest. Colours are the neighbours' own resolved colours (fog, transparency and the P2 filter already applied), blended in display space as the 16-spp reference's box filter does. No extra rays, no colour threshold for detection: borders are found by cell identity. 2. **MLAA** (`edges_main`, `weights_main`, `blend_main`) on what stage 1 cannot place: a 3×3 that sees a cell smaller than a pixel (far LoD cells, foliage, faces at grazing angles), where the cells the centre rays hit no longer tile the footprint. SMAA 1x-style luma edges (threshold 0.05, local contrast adaptation ×2), orthogonal L/Z/U runs up to 16 px a side, analytic MLAA areas, no diagonals. Stage 1 passes its per-pixel state in its output's alpha, and MLAA never touches a pixel stage 1 blended. `aaDebug 1` shows the stages: white = cell coverage blended, cyan = MLAA blended, dark blue = handed to MLAA and left, grey = cells differ but colours do not, black = one cell. **Cost** (RX 5700, 800×600, settled fixed poses, the `antiAlias` pass, the production build; `logs/aa-proto/shots/v*`): | scene | pose | AA ms | frame ms (AA off) | px: cell-blended / MLAA-blended / handed to MLAA / cells differ, same colour | |---|---|---|---|---| | celestial-sweep | overview | 0.13 | 4.43 | 0.05 / 0.69 / 2.3 / 82 % | | | highedge | 0.18 | 2.30 | 1.6 / 0.18 / 0.8 / 60 % | | | pole | 0.13 | 2.48 | 0.3 / 0.26 / 0.7 / 68 % | | play-day | high | 0.17 | 3.81 | 1.0 / 0.41 / 1.4 / 58 % | | | sweep | 0.17 | 3.58 | 0.95 / 0.40 / 1.6 / 73 % | | | inside | 0.18 | 2.60 | 1.5 / 0.15 / 0.5 / 62 % | | | street | 0.15 | 2.37 | 0.3 / 0.07 / 0.3 / 44 % | | forest terrain (`aa-terrain`) | hiA | 0.21 | 11.6 | 0.4 / 3.1 / 36 / 19 % | | | hiB | 0.32 | 6.9 | 6.3 / 1.1 / 17 / 27 % | | celestial-sweep at 1280×960 | highedge | 0.38 | 4.08 | | The last column is what a cell-identity mask alone fires on: 44-82 % of the pixels (cells are a few pixels or less past mid range); the colour-spread skip (2.5/255) leaves 1-3 % for the coverage blend outside the forest. ## 2. Methods compared All history-free, all post-resolve; measured in the same sessions against the survey's 16-spp references (`logs/aa-research/ref*`, same poses, same LoD; metrics as the survey: edge px = reference Sobel > 6 levels, error = |ΔY| on 8-bit luminance, jaggy px = edge px > 24 levels off; pan = 16 frames at 0.26 px/frame, "pan bad px" = px/frame whose change differs from the reference's change by > 24 levels). 1. **Edge rays** ("B extended"): 4 rotated-grid primary rays (world + entity, the gbuffer and entity-overlay walkers) at flagged pixels; each sample took the resolved colour of the 3×3 pixel showing its cell (else one showing the same part + material, else the centre's). - With the brief's cell-id mask (any 3×3 cell difference) it fires on **60-62 %** of the pixels of celestial and play-day poses (cells are ≤ a few px past mid range): **1.5-2.2 ms**. - With a geometric mask (sky / part / material / normal / 10 % depth jump): 2-4 % of pixels, but still **0.6-1.6 ms** on celestial and play-day (incoherent rays, the entity BVH per sample) and **3.5-4.9 ms** on forest terrain. Out of budget everywhere. 2. **In-plane analytic cell coverage** — stage 1 above, alone. 3. **MLAA-class baseline** — stage 2 above, alone (SMAA 1x-style; a CMAA2/SMAA port with the precomputed area texture was not built: the analytic areas are the same function for orthogonal patterns). 4. Combinations: cell + edge rays (the rays at geometric discontinuities, cell elsewhere) and **cell + MLAA** (the winner). | pose | today | edge rays (geom.) | cell | MLAA | cell + rays | **cell + MLAA** | 16 spp | |---|---|---|---|---|---|---|---| | **edge error** (levels) | | | | | | | | | cel overview | 6.73 | 5.50 | 5.94 | 4.98 | 4.84 | **4.97** | 0 | | cel highedge (box + shadow) | 5.53 | 4.32 | 3.45 | 3.55 | 2.64 | **2.35** | 0 | | cel pole | 6.94 | 5.26 | 4.93 | 3.44 | 4.36 | **3.02** | 0 | | cel slats | 6.84 | 6.62 | 3.23 | 5.00 | 3.40 | **3.29** | 0 | | cel lowplate | 6.66 | 6.23 | 6.85 | 4.63 | 5.22 | **4.49** | 0 | | cel thin rail | 3.74 | 3.30 | 3.56 | 2.46 | 1.45 | **1.75** | 0 | | cel glass | 4.18 | 3.35 | 4.51 | 2.89 | 2.18 | **2.22** | 0 | | play inside | 6.44 | 3.64 | 4.73 | 3.29 | 2.75 | **2.90** | 0 | | play sweep (house) | 8.43 | 5.43 | 6.87 | 5.97 | 5.49 | **5.71** | 0 | | play high (house, antenna) | 9.38 | 5.67 | 7.80 | 7.34 | 6.51 | **5.66** | 0 | | **jaggy px** | | | | | | | | | cel highedge | 443 | 239 | 19 | 117 | 54 | **18** | | | cel pole | 862 | 478 | 323 | 145 | 387 | **193** | | | cel slats | 1 296 | 1 179 | 610 | 677 | 596 | **611** | | | play inside | 378 | 120 | 412 | 113 | 117 | **105** | | | play high | 1 336 | 537 | 1 021 | 880 | 683 | **586** | | | **pan bad px / frame** | | | | | | | | | cel overview | 417 | 241 | 174 | 162 | 166 | **160** | | | cel highedge | 151 | 145 | 17 | 95 | 48 | **24** | | | cel pole | 759 | 340 | 86 | 65 | 287 | **60** | | | cel slats | 968 | 918 | 132 | 345 | 178 | **134** | | | cel glass | 116 | 75 | 36 | 42 | 33 | **20** | | | play inside | 653 | 408 | 322 | 499 | 276 | **307** | | | **AA ms** | | | | | | | | | celestial (3 poses) | — | 0.60-1.23 | 0.09-0.13 | 0.05-0.07 | 0.62-1.24 | **0.14-0.18** | | | play-day (4 poses) | — | 0.79-1.58 | 0.09-0.13 | 0.07 | 0.97-1.44 | **0.14-0.19** | | | forest terrain (2 poses) | — | 3.50-4.94 | 0.38-0.43 | 0.06 | 3.50-4.98 | **0.43-0.50** (0.21-0.32 with the own-cell early-out) | | Edge-rays and cell + rays columns come from the first matrix (`shots/cel`, `shots/play`; the cell part then lacked the footprint fallback of §5), the rest from the second (`shots/cel2`, `shots/play2`). Play-day numbers carry the moving allays (`street` is all allay motion and is left out). Full rows: `logs/aa-proto/metrics-*.jsonl`. **Why cell + MLAA won.** - Cell coverage is exact where cells are ≥ 1 px: near and mid-range silhouettes, creases, material and shadow-cell borders, entity edges. It takes the jaggy count of the box/shadow pose from 443 to 19 (MLAA alone: 117) and the pan crawl there from 151 to 17 px/frame, and it smooths low-contrast steps (a shadow-cell border on a lit face) that a luma threshold never sees. It does not soften voxel corners: a corner stays where the geometry puts it. - It cannot place a border whose cells are thinner than a pixel — the cells the centre rays hit no longer tile the footprint (far shadow staircases on the floor, plate edges at grazing angles, the forest). Alone it is worse than MLAA there (pole, lowplate, overview) and on forest terrain it blends sub-pixel cells into more pan change (3 677 → 4 322 px/frame > 24 levels at hiA). Handing exactly those neighbourhoods to MLAA gives the best or near-best of both on every pose. - Edge rays (alone or with cell coverage) cost 4-30× more; cell + rays beats cell + MLAA on 5 of 10 poses by ≤ 0.3 levels of edge error and loses on the others. ## 3. Interactions checked - **No mode shifts.** Both stages read only the frame's own inputs. Highedge, 6 still frames: 0 px change > 10 levels (as without AA). A sun moving 0.02°/frame: 1 387 px/frame > 10 levels with AA, 1 397 without; the 6 frames after it stops: 0 / 0. Nothing differs between moving and still objects or sun. - **Terrain/entity parity:** entity cells use the same face/coverage code with their part's transform (the allays and the windmill on play-day). - **P2 sun-shadow filter, fog, transparency/glass:** AA reads the resolved colours, so all are included per neighbour. Glass pane edges are not cell borders (the cell behind is the same): stage 1 leaves them, MLAA takes them where they show a luma step. - **Debug HUD, visual cards, brush overlays:** drawn after the blit, never AA'd. The vt debug views turn AA off (raw per-pixel values). - **Resolution:** AA runs at render resolution before the 1:1 blit (there is no upscale); its targets follow resizes. ## 4. What it does not fix (owner decisions) 1. **Sub-pixel far texture shimmer (survey G4: forest, snow slopes) and thin features no centre ray hits.** MLAA cannot reconstruct what no sample saw; on the forest poses AA changes the pan crawl by +4 % (3 677 → 3 832 px/frame > 24 levels at hiA, 1 608 → 1 842 at hiB) — the moiré of the snow terraces is untouched (sheet `terrain-forest.png`, row 3). The survey's remaining options are unchanged: the LoD prefilter (`prunePixels` 2 → 4: no cell below a pixel; history-free, blockier far field) or light TAA (C; sharper still than moving — a mode-shift risk). No TAA was built. 2. **Budget:** 0.13-0.18 ms on celestial and play-day, 0.21-0.32 ms on the forest poses (of 7-12 ms frames), 0.38 ms at 1280×960. Within the ≤ 0.5 ms asked for; no trade-off to decide here. ## 5. Implementation notes - `aa-pass.ts`: `buildAaCellWgsl` (stage 1; camera, the composite's hitTau and slot, the parts table, the VSC at group 1), `buildAaMlaaWgsl` (stage 2); one compute pass, four dispatches; targets: the stage-1 colour (alpha = state), MLAA edges (r32uint) and run heights (rgba16float), the output the blit reads. Both modules are in `wgsl-module-catalog.ts`. - Stage 1 tests the pixel's own cell first: a pixel whose own cell is below a pixel goes straight to MLAA without the 3×3 face work (forest). - A face without a usable footprint (the pixel's ray parallel to its plane, the plane behind the camera, a differential ray past its horizon) keeps the pixel's own colour and covers nothing for a neighbour — without this the far ground at the horizon was replaced by sky. - Sheets (`logs/aa-proto/owner/`, column labels burned in): `silhouettes-and-material-borders.png`, `shadow-cell-borders.png`, `playday.png`, `far-subpixel.png`, `terrain-forest.png`, `pan-shimmer.png`.