Skip to content

Faster render snapshot: cheaper blend, and moving meshes blended on the GPU - #257

Merged
BenjaFriend merged 8 commits into
mainfrom
perf/snapshot-blend
Nov 17, 2026
Merged

BenjaFriend merged 8 commits into
mainfrom
perf/snapshot-blend

Conversation

@BenjaFriend

@BenjaFriend BenjaFriend commented Nov 17, 2026 •

Copy link
Copy Markdown
Member

Problem

In the lighting test level, FPS dropped by almost half when the spheres started rotating. --profile showed why: Graphics.PublishSnapshot blends the world matrix of every moving entity each frame, and that cost 6.4 ms a frame in a Debug build with the 3,136 rotators (1.6 ms with them still). The blend is done on the main thread, so it lands directly in the frame time. The simulation was not the cause (the rotator tick costs about 0.07 ms).

Changes

Part 1, cheaper CPU blend:

  1. Interpolate::BlendedWorld walks the parent chain once and keeps the two ends, where it walked it twice. A chain deeper than 8 uses the old code (BlendedWorldDeep).
  2. TransformUtil::LocalMatrix is written out: the same values as translate, rotate and scale from glm, without the matrix products.
  3. PreCullView: the renderer publishes the view it culled with, and PublishSnapshot draws the published LocalToWorld of a mesh far outside it instead of blending. The view is one frame old, so a mesh that comes into view that frame is drawn at the latest tick's place for that frame only.

Part 2, blend on the GPU (now that #255 has merged; design and numbers in Plans/gpu-blend.md):
4. A moving mesh with no Parent goes into the snapshot as its two Transform values (From, To, HasBlend) and the frame's RenderSnapshot::BlendAlpha. Still meshes and children keep a matrix.
5. cull.comp makes the blended matrix in phase 0 and tests the frustum for it. The G-buffer vertex shader (mrt.vert) reads matrices from one storage buffer by slot. Shader reflection now tells a storage buffer from a uniform buffer.
6. --gpu-blend on|off, [Engine] GpuBlend, a checkbox in GPU Options. On by default. Needs occlusion culling; otherwise the CPU blends (MeshDrawItem::ResolveWorld).
7. The only GPU to CPU readback is two stats counters (occluded, frustum-culled), read one frame late from mapped memory.

Measured

Debug, offscreen, 3,136 rotating spheres, PublishSnapshot per frame: 6.4 ms before, about 1.8 ms after part 1. Part 2 adds a smaller gain: with everything in view ResolveWorlds goes from 0.43 to 0.01 ms, and with the default view Cull from 2.34 to 1.57 ms.

Real window, real GPU (RelWithDebInfo, 1080p, rotating): 2.75 ms a frame with GPU blend on, 2.80 ms off. GPU-bound, so no fps change there; Vk.WaitForGpu is the same in both.

Tests

  • BlendedWorldTests (new): BlendedWorld equals BlendedWorldDeep for chain depths 1 to 12, each with no level moved and with each level moved, at four blend factors; LocalMatrix equals the glm product.
  • A snapshot test for the pair and for a child's matrix; ResolveWorld checks in the interpolation and proxy tests.
  • ctest: 693/693 pass (RelWithDebInfo). Not run in a Debug build.
  • GPU and CPU pictures at the same tick differ by at most 3/255 on about 1,700 of 2M pixels (float rounding). No validation layer messages, with and without occlusion culling.

Not done

  • Stage 3 in Plans/gpu-blend.md: build the snapshot only when a tick arrives, to remove the remaining per mesh loop (about 1.2 ms in Debug).
  • The CPU frustum test for still meshes (Offscreen.Cull) could move to the GPU too.

🤖 Generated with Claude Code

https://claude.ai/code/session_017NN37Q2jpV81xZaX3ocojK

BenjaFriend and others added 8 commits November 17, 2026 11:03
…meshes

Found with --profile: with the lighting test level's 3,136 rotating spheres,
Graphics.PublishSnapshot cost 6.4 ms a frame in a Debug build (1.6 ms still),
and FPS dropped by almost half.

- Interpolate::BlendedWorld walks the parent chain once and keeps the two ends,
  where it walked it twice. A chain deeper than 8 uses the old code
  (BlendedWorldDeep). A test holds the two to the same matrices at every depth
  around the limit.
- TransformUtil::LocalMatrix is written out: the same values as the three glm
  calls (a test checks it), without the matrix products.
- The renderer publishes the view it culled with (PreCullView), and
  PublishSnapshot draws the published LocalToWorld of a mesh far outside it
  instead of blending. The view is one frame old: a mesh that comes into view
  that frame is drawn at the latest tick's place for that frame.

Debug, rotating: PublishSnapshot 6.5 ms down to 1.8 ms. The simulation checksum
is the same.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017NN37Q2jpV81xZaX3ocojK
…e matrix

A mesh that moved between the two ticks and has no Parent goes into the
RenderSnapshot as its two Transform values (HasBlend, From, To) and the frame's
factor (BlendAlpha), instead of a matrix made on the CPU. The renderer resolves
it once a frame (OffscreenSubpass::ResolveWorlds, MeshDrawItem::ResolveWorld).
No picture change: this is the first step of blending on the GPU
(Plans/gpu-blend.md).

MeshCuller::Cull takes the resolved matrices.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017NN37Q2jpV81xZaX3ocojK
The cull compute shader makes the world matrix of each mesh that moves between
two ticks, from the two ends of its Transform and the frame's blend factor, and
tests it against the view volume (the CPU has no matrix for it). The G-buffer
vertex shader reads its matrix from one storage buffer, by slot; the per mesh
UBO holds only the slot. The CPU writes the matrix of a still mesh when its
LocalToWorld version changes.

- --gpu-blend on|off, [Engine] GpuBlend, a GPU Options checkbox. On by default;
  it needs occlusion culling, otherwise the CPU blends (as before).
- Shader reflection tells a storage buffer from a uniform buffer.
- OcclusionSlot has the two ends in place of the matrix.

GPU and CPU blend match to 3/255 on 1,697 of 2 million pixels at the same tick;
no validation layer messages. Plans/gpu-blend.md has the design and the numbers.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017NN37Q2jpV81xZaX3ocojK
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017NN37Q2jpV81xZaX3ocojK
@BenjaFriend
BenjaFriend changed the base branch from perf/profiler to main November 17, 2026 17:47
@BenjaFriend BenjaFriend changed the title Faster render snapshot: one-pass blend, direct LocalMatrix, skip far meshes Faster render snapshot: cheaper blend, and moving meshes blended on the GPU Nov 17, 2026
@BenjaFriend
BenjaFriend merged commit 4e3a18e into main Nov 17, 2026
28 checks passed
@BenjaFriend
BenjaFriend deleted the perf/snapshot-blend branch November 17, 2026 21:27
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