gpu: sync only logged ranges before BDA draws #1286
No reviewers
Labels
No labels
bug
documentation
duplicate
enhancement
GPU
GUI
help wanted
question
Shaders
Video
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/KytyPS5--KytyPS5!1286
Loading…
Reference in a new issue
No description provided.
Delete branch "gh-pr/1286/head"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
@NeySlim on GitHub · 2026-10-09 07:25 UTC · original
NeySlim:perf/bda-incremental-sync→main· +730 −116 · 13 files · 8 commits · closed without mergingStacked on #1211 (
perf/gpu-command-thread): it builds on the CPU-dirty generation added there. Only the last three commits are new.Problem
Before every draw or dispatch whose shaders address memory directly (about 700 per frame in God of War Ragnarök, PPSA08332, 05.005),
RenderContext::PrepareBdauploads the CPU-written pages of every registered buffer. #1211 skips it when the guest CPU wrote nothing since the previous call, but this game writes every frame (about 2000 write faults per frame), so the sweep still runs on most calls: it walks every mapped range and every registered buffer, takes each tracker region lock and scans its dirty bits, to upload a few pages.Profile of the GPU command thread in the opening cutscene:
PrepareBdais 10.4% of its samples, and draws and dispatches run 15.6SynchronizeBuffercalls each on average, most of them on clean buffers.Change
MemoryTrackerlogs the page ranges it marks CPU-dirty (InvalidateRegion,MarkRegionAsCpuModified,UntrackMemory, new regions), in a log reserved once (4096 ranges, an adjacent range extends the previous entry, so logging from the fault handler does not allocate).TakeCpuDirtyLoghands them over, or reports a gap on the first call or after an overflow. Until a sweep takes the log, writes skip it and its mutex (an atomic flag, with fences pairing the flag and the dirty bits so a write that skips the log is seen by the full sweep that follows).BufferCachekeeps the ranges of buffers registered since the previous sweep (up to 1024).PrepareBda, when the generation or the registration epoch changed, synchronizes only the logged and newly registered ranges that are mapped, with one upload per buffer and mapped span. A gap in either log falls back to the full sweep.RenderContext::MapMemorylogs the parts of a range that were not mapped, once they are. This replaces the mapping epoch of #1211, which would force a full sweep.Test
MemoryTrackerTests: newTestCpuDirtyLog(new regions, marks, invalidations, untracking and mapped ranges are logged as page ranges, adjacent ones merge, an overflow reports one gap) andTestUntakenLogSkipsItsMutex(before any take, CPU writes complete while the test holds the log mutex; once taken, the next write is logged).New
BdaIncrementalSweep(--bda-sweep-only, ctestbuffer_cache_bda_sweep, also in the default run). Three buffers in three tracker regions:Without the change the idle-buffer check fails (the full sweep uploads the page). The test runs on one thread, like the GPU command thread. Without the mapped-range logging, this test and
BdaSweepAfterRemapfrom #1211 fail at "mapped again".Validation
Linux, Ryzen AI Max+ 395, Radeon 8060S, Mesa RADV 26.2.3.
PrepareBdaper frame:What remains is the work on pages the CPU did write: re-protecting them after the upload and waiting for tracker region locks held by guest threads handling write faults. Draws and dispatches run 6.9
SynchronizeBuffercalls each on average instead of 15.6. Screenshots of the same scenes show no rendering difference.memory_tracker_tests,page_manager_tests,resource_tracking_tests,resource_materialization_testsand the newbuffer_cache_bda_sweeppass.shader_recompiler_compute_tests, one case per process: same results as #1211 without these commits, plus the new case (595 of 604 cases pass; the same 9 fail on both on this RADV setup).Not tested on Windows or macOS. The change adds no platform-specific code (a mutex, an atomic flag and fixed-capacity vectors next to the existing tracker state), and the new check uses the same direct-memory calls as the other buffer cache checks and runs on every platform.
AI use
I used Claude Code (AI agent) to profile the GPU command thread, write the change and the tests, and run the measurements and tests above on my machine. I reviewed the diff and the results before opening this PR.
@coderabbitai[bot] on GitHub · 2026-10-09 07:31 UTC · original · edited 2026-10-16 00:57 UTC
No actionable comments were generated in the recent review. 🎉
ℹ️ Recent review info
⚙️ Run configuration
52b03462-85ca-40b8-ac4c-472d33240b24📥 Commits
Reviewing files that changed from the base of the PR and between 61a3310b9166ae863add275c99845720749d925b and
aad472588f.📒 Files selected for processing: 5
CMakeLists.txtsrc/graphics/host_gpu/renderer/cache/bufferCache.cppsrc/graphics/host_gpu/renderer/pipeline/descriptors.cppsrc/graphics/shader/recompiler/ir/Value.htests/ShaderRecompilerComputeTests.cppIncluded review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 5 remain after this review.
📝 Walkthrough
The change adds CPU-dirty tracking and incremental synchronization for mapped addressable buffers. It adds checks for overlap between specialization reads and writable image ranges. Shader IR accessor definitions move into inline definitions. New tests cover dirty tracking, buffer sweeps, and remapping.
Suggested reviewers:
nmzikPriority: ➖ Normal
Change: Refactor
Merge Risk: ⚪ Minimal · up to
aad47No confirmed synchronization issue prevents merging, subject to normal checks.
Pre-merge checks:
4 ·
1
Full details: Docstring Coverage
Explanation
Docstring coverage is 6.12% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 49 functions across 10 files. (2 skipped: 1 unsupported, 1 too large.)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
❤️ Share
Comment
@coderabbitai helpto get the list of available commands.@NeySlim on GitHub · 2026-10-09 09:43 UTC · original
Updated after an internal review:
NoteCpuDirtyno longer takes the log mutex while no sweep has taken the log. An atomic flag guards it, and seq_cst fences pair the flag with the dirty bits, so a write that skips the log is seen by the full sweep that follows the take completing it. New testTestUntakenLogSkipsItsMutex.BdaIncrementalSweepno longer relies on a held region lock, which lock-free dirty queries would bypass, and runs on one thread like the GPU command thread: it takes the log entry of a write to the idle buffer and checks that the page is still CPU-dirty with its old GPU contents after the sweep. A full sweep fails it.@NeySlim on GitHub · 2026-10-14 05:59 UTC · original
#1391 goes after the same
PrepareBdasweep and is based directly on main. The approaches differ a bit:Both keep the existing page ownership checks for what actually gets uploaded. In God of War Ragnarök the sweep was about 10% of the GPU thread before this change. If #1391 is preferred I am fine with closing this one; otherwise I can make it standalone on main.
@NeySlim on GitHub · 2026-10-16 17:17 UTC · original
Closing this:
d58aefde4landed the same idea on main (QueueBdaSync/TakeBdaSync with SynchronizeBda), covering CPU writes, invalidations, untracking, new registrations and remapped memory, with tests for each. I went through the remaining differences and didn't find a case main misses, so there's nothing left here worth keeping.Pull request closed