Skip to content

feat(render): surface-cache primitives (baked fill + BakeSurface) - #20

Merged
tannevaled merged 1 commit into
mainfrom
surface-cache-primitives
Jul 6, 2026
Merged

feat(render): surface-cache primitives (baked fill + BakeSurface)#20
tannevaled merged 1 commit into
mainfrom
surface-cache-primitives

Conversation

@tannevaled

Copy link
Copy Markdown
Contributor

First step of the renderer perf work: Quake's surface cache. Collapses the world-surface span loop from a per-pixel lightmap bilinear + colormap lookup to a single fetch + store.

  • CachedSurface / CachedVertex — a per-face baked surface (texture already modulated by the lightmap through the colormap, as final palette indices).
  • FillPerspectiveCachedPolygon — perspective-correct textured copy from a baked surface (same 1/z subdivision; clamped; no per-pixel lightmap/colormap). Benchmarks 2.45× faster than the lightmapped fill on the same ~250k-px quad (412µs vs 1011µs, 0 allocs).
  • BakeSurface — builds a CachedSurface: per baked texel it samples the texture (tile-wrapped) + lightmap plane (bilinear) and combines them through the colormap, mirroring FillPerspectiveLightmappedPolygon exactly. A parity test confirms the cached path reproduces the per-pixel path bit-for-bit on a uniform-Z face.

Pure additions — nothing wires them yet, so no behaviour change. The runner integration (a persistent per-face cache for static faces) lands next. 100% line coverage on both new files; benchmarks + parity test included.

🤖 Generated with Claude Code

Adds the building blocks of Quake's surface cache, so the world-surface span
loop can collapse to a single fetch + store per pixel instead of a per-pixel
lightmap bilinear + colormap lookup:

- CachedSurface / CachedVertex: a per-face baked surface (texture already
  modulated by the lightmap through the colormap, as final palette indices).
- FillPerspectiveCachedPolygon: perspective-correct textured copy from a baked
  surface (same 1/z subdivision as the other fills; clamped, no per-pixel
  lightmap/colormap). Benchmarks 2.45x faster than the lightmapped fill on the
  same ~250k-px quad (412us vs 1011us, 0 allocs).
- BakeSurface: builds a CachedSurface for a face -- per baked texel it samples
  the texture (tile-wrapped) + the lightmap plane (bilinear) and combines them
  through the colormap, mirroring FillPerspectiveLightmappedPolygon exactly.
  A parity test confirms the cached path reproduces the per-pixel path
  bit-for-bit on a uniform-Z face.

Pure additions -- nothing wires them yet, so no behaviour change. The runner
integration (a persistent per-face cache for static faces) lands next.
100% line coverage on both new files; benchmarks + parity test included.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@tannevaled
tannevaled merged commit 230554f into main Jul 6, 2026
7 checks passed
@tannevaled
tannevaled deleted the surface-cache-primitives branch July 6, 2026 08:20
tannevaled added a commit that referenced this pull request Jul 6, 2026
Wires the surface-cache primitives (#20) into the world-face draw. A face is
CACHEABLE when its lightmap is constant over time -- fullbright (LightOfs<0)
or every active lightstyle is style 0 ('m', the only time-independent style)
-- AND its texture is not animated (no '+' chain). Such a face is baked ONCE
via render.BakeSurface into a per-face render.CachedSurface (keyed by face
index, persisted for the map's lifetime -- static faces never change, so no
invalidation) and painted with render.FillPerspectiveCachedPolygon: a single
fetch+store per pixel instead of a per-pixel lightmap bilinear + colormap
lookup. Animated faces (flickering lights, '+' textures) keep the per-pixel
lightmapped path; sky/turbulent are unchanged.

texMinS/texMinT for the bake come straight from lmInfo.MinS/MinT, since
TransformFaceLightmapped defines LmS=(U-MinS)/16 (so U=MinS+LmS*16, and the
cache is indexed by LmS*16 / LmT*16).

Verified in-browser (Firefox/Playwright, lq_e0m1): the world renders
pixel-identical to the per-pixel path (walls fully textured + lit, no
artifacts) and the same scene measured 1.32 -> 1.56 fps (+18%) under the
identical throttled harness. The win is scene-dependent -- larger in
wall-heavy views, bounded here by the ~half-screen of (uncached) water + sky;
the isolated cached fill benchmarks 2.45x faster than the lightmapped fill.

Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
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