Repository navigation
Faster render snapshot: cheaper blend, and moving meshes blended on the GPU - #257
Merged
Merged
Conversation
…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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
In the lighting test level, FPS dropped by almost half when the spheres started rotating.
--profileshowed why:Graphics.PublishSnapshotblends 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:
Interpolate::BlendedWorldwalks the parent chain once and keeps the two ends, where it walked it twice. A chain deeper than 8 uses the old code (BlendedWorldDeep).TransformUtil::LocalMatrixis written out: the same values as translate, rotate and scale from glm, without the matrix products.PreCullView: the renderer publishes the view it culled with, andPublishSnapshotdraws the publishedLocalToWorldof 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
Parentgoes into the snapshot as its twoTransformvalues (From,To,HasBlend) and the frame'sRenderSnapshot::BlendAlpha. Still meshes and children keep a matrix.5.
cull.compmakes 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,
PublishSnapshotper frame: 6.4 ms before, about 1.8 ms after part 1. Part 2 adds a smaller gain: with everything in viewResolveWorldsgoes from 0.43 to 0.01 ms, and with the default viewCullfrom 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.WaitForGpuis the same in both.Tests
BlendedWorldTests(new):BlendedWorldequalsBlendedWorldDeepfor chain depths 1 to 12, each with no level moved and with each level moved, at four blend factors;LocalMatrixequals the glm product.ResolveWorldchecks in the interpolation and proxy tests.ctest: 693/693 pass (RelWithDebInfo). Not run in a Debug build.Not done
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).Offscreen.Cull) could move to the GPU too.🤖 Generated with Claude Code
https://claude.ai/code/session_017NN37Q2jpV81xZaX3ocojK