0013. Render engine is a derived cache of the document
Status: Superseded by 0024 for phasing and memory (2026-11-27). Still valid: the engine is a derived cache of the doc; paint order = pre-order; remote drag previews via remote-xf slots; bench gate within 3%. Changed: there is no CPU primitive mirror in any phase; instances are derived per chunk from doc columns at first integration (E1).
Context
Baseline that must not regress (M4 Pro, Chrome): 4,000 website pages (682K layers, 11M primitives) and 5M shapes at ~120 fps; worst-zoom GPU <= 3.6 ms. Today the engine’s Scene is also the only data model.
Decision
- The engine keeps its proven structures (paint-ordered primitive buffer, 1,024-primitive chunks, LOD impostors, dirty-chunk uploads, GPU drag preview) and becomes a consumer of
DocCore: once per frame, before flushBounds, it drains the doc’s ChangeSet and rewrites only affected primitives (world transform = parent chain; text -> glyphs; path -> segments). Idle frames do one integer compare.
- Paint order = pre-order of the doc tree, so a frame’s subtree is a contiguous primitive range; reorders inside a frame move only that range; appends (the common case) stay O(changed).
- Remote drag previews: up to 4 remote affines as extra uniforms plus 2 flag bits per primitive (
F_REMOTE_XF); no buffer rewrites while peers drag.
- Phase E1: keep the CPU primitive mirror (no render-path change; memory +~80 B/node).
- Phase E2 (after E1 ships): drop the CPU mirror; regenerate a dirty chunk’s primitives from the doc into a scratch buffer, compute bounds/impostors, upload. Hit-testing and marquee use doc geometry plus cached world offsets. Cuts website memory ~70%.
- Gates for every engine change:
npm run bench (tools/bench, Playwright-driven Chrome on the reference machine) within 3% of the Phase 0 baseline fps and GPU ms; heap/typed-array bytes reported.
Alternatives
- Engine buffer as the doc store (zero duplication): world coordinates and glyph-only text leak into server/MCP; rejected (ADR 0003).
- Local-space primitives with a parent-offset texture on the GPU: smallest memory, but adds a texel fetch per vertex on the hot path; reconsider only if E2 is not enough.
Consequences
The doc never knows about chunks or GPU buffers; the engine can be replaced or moved to a worker/WebGPU later behind the same feed.