gpu: cheaper CPU write tracking for uploaded pages #1284
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!1284
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-1284"
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/write-tracking→main· +386 −33 · 11 files · 5 commits · openProblem
The buffer cache keeps the pages it has uploaded write-protected, so that a CPU write marks them dirty through a write fault. In God of War Ragnarök (PPSA08332, 05.005) the guest CPU writes about 2,000 such pages per frame in the opening cutscene and 3,000 in the sled sequence that follows. The GPU command thread protects them again when it uploads them, mostly in the sweep of registered buffers before draws that address memory directly, and the game writes many of them again later in the same frame.
Per frame, before this PR:
mprotecttimeThree costs add up:
ProtectTransient), although it only reads the mapping list;Change
GuestAddressSpace::ProtectTransienttakes the address space mutex in shared mode. Mapping changes still take it exclusively.PageManagerchanges the protection of a page only under its own region lock, so concurrent callers never protect the same page.TextureCachecounts the registered images of each page of its page table in atomics written under its lock.InvalidateMemoryreturns without the lock when the written pages have none. Images count their pages when they register, before they are tracked, so a fault on a page an image protects always finds the count; a write racing with a registration behaves as one just before it, which the lock already allowed.RegionManagerpublishes its dirty bits in atomic words after each change.MemoryTrackerdirty queries, and upload and download checks, read them without the region lock when the range has nothing to do. These calls run on the GPU command thread, which is the only one that clears CPU-dirty bits and the only one that changes GPU-dirty bits; other threads only set CPU-dirty bits, under the lock and before their write. Debug builds check that uploads, downloads and GPU-dirty clears never overlap.Test
virtual_memory_allocation_tests:TrackerProtectionDuringBackingRead. A tracker protection change completes while a sparse backing read holds the address space lock in shared mode.shader_recompiler_compute_tests --imageless-invalidation-only(ctesttexture_cache_imageless_invalidation, also in the default run):ImagelessPageInvalidation. While the test holds the texture cache lock, a write to a page without images returns; a write to an image page still marks the image CPU-dirty; after the image is deleted, writes to its pages no longer wait for the lock.memory_tracker_tests:TestQueriesDoNotWaitForWriteFault: while a write fault holds a region (stopped inside its protection change), dirty queries and an upload check of a clean page of the same region complete;overlapping-gpu-dirty-owners).TestConcurrentColdUploadsnow runs its uploads one after the other and checks that a cold writable upload does not block region creation on another thread.Each new test fails without its change (checked by reverting the change alone).
Validation
Linux, Ryzen AI Max+ 395, Radeon 8060S, Mesa RADV 26.2.3.
God of War Ragnarök from a new game, alternating every 5 s between the previous code and this change within one session, so both see the same scenes, on a branch with other pending GPU thread changes: the sled frame time drops by 6.6 ± 2.8 ms (± one standard error). I did not measure the cutscene separately for this change; there the GPU command thread mostly waits for GPU work at its readbacks, so time it saves tends to turn into waiting.
Screenshots of both settings show no rendering difference.
memory_tracker_tests,page_manager_tests,virtual_memory_allocation_tests,resource_tracking_testsandresource_materialization_testspass.shader_recompiler_compute_tests, one case per process: same results asorigin/mainexcept the newImagelessPageInvalidationcase, which fails without the change (598 of 608 cases pass with it, 597 without; the other 10 fail on both on this RADV setup).memory_tracker_testspasses, and the wholeshader_recompiler_compute_testssuite, one case per process, gives the same results as the release build (the overlap check never fires).Not tested on Windows or macOS. The change adds no platform-specific code:
VirtualProtect, likemprotect, can run concurrently on different pages, and the new tests use only the existing portable helpers.AI use
I used Claude Code (AI agent) to profile the GPU command thread and the guest fault path, 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:29 UTC · original · edited 2026-10-16 01:07 UTC
No actionable comments were generated in the recent review. 🎉
ℹ️ Recent review info
⚙️ Run configuration
5c9efa30-c8ad-49ea-bade-5ccdc8129874📥 Commits
Reviewing files that changed from the base of the PR and between 3385d5fc608da8e1aab029e77e9d76034cd8f93b and
9a1e0cbfba.📒 Files selected for processing: 4
src/graphics/host_gpu/renderer/cache/textureCache.cppsrc/graphics/host_gpu/renderer/cache/textureCache.htests/ShaderRecompilerComputeTests.cpptests/VirtualMemoryAllocationTests.cppIncluded review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 4 remain after this review.
📝 Walkthrough
Memory tracking now publishes atomic dirty-page snapshots and uses unlocked checks for queries and transfers. GPU-dirty operations use owner scopes. Transient page protection now uses a shared lock. TextureCache tracks image registrations by page and skips invalidation when a valid range has no registered images. Tests cover concurrent memory operations, protection changes, sparse backing reads, and texture invalidation.
Suggested reviewers:
nmzikPriority: ➖ Normal
Change: Refactor
Merge Risk: ⚪ Minimal · up to
9a1e0No actionable merge-blocking issue remains in the reviewed changes; proceed with normal checks.
Pre-merge checks:
4 ·
1
Full details: Docstring Coverage
Explanation
Docstring coverage is 12.50% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 40 functions across 8 files. (1 skipped: 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.@coderabbitai[bot] on GitHub · 2026-10-09 07:29 UTC · original · reviewed
Actionable comments posted: 1
ℹ️ Review info
⚙️ Run configuration
9ffac5ec-b917-4ece-913a-66da77f57a6d📥 Commits
Reviewing files that changed from the base of the PR and between
7b9997baeaand 29d49db31ef2c42f2d8851c0e6bca22ec4581bd8.📒 Files selected for processing (11)
CMakeLists.txtsrc/common/bitArray.hsrc/graphics/host_gpu/memoryTracker.cppsrc/graphics/host_gpu/memoryTracker.hsrc/graphics/host_gpu/regionManager.hsrc/graphics/host_gpu/renderer/cache/textureCache.cppsrc/graphics/host_gpu/renderer/cache/textureCache.hsrc/kernel/memoryAddressSpace.inctests/MemoryTrackerTests.cpptests/ShaderRecompilerComputeTests.cpptests/VirtualMemoryAllocationTests.cppIncluded review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 3 remain after this review.
@coderabbitai[bot] on GitHub · 2026-10-09 07:29 UTC · original · edited 2026-10-09 07:46 UTC
On
tests/MemoryTrackerTests.cppline 1018 (outdated):🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
Check every page after the simulated writes.
IsRegionCpuModified(base, gpu_page * page_size)returns true if only one page is dirty. If an ahead page becomes writable without a CPU-dirty bit, the loop skips its fault and this assertion can still pass. Check both the dirty bit and write permission for each page before accepting the result.✅ Addressed in commit d23b24d
@NeySlim on GitHub · 2026-10-09 07:46 UTC · original
On
tests/MemoryTrackerTests.cppline 1018 (outdated):Fixed in d23b24d7: the test now checks every page before the GPU-written one for both the CPU-dirty bit and write permission, instead of one range query plus the last page. It fails when a dirty page is left protected.
@coderabbitai[bot] on GitHub · 2026-10-09 07:46 UTC · original
On
tests/MemoryTrackerTests.cppline 1018 (outdated):@NeySlim, thanks for the fix. The per-page checks inTestSequentialWritesFaultAheadaddress the finding. Each page must now have both its CPU-dirty bit set and write permission.If you found this review helpful, would you consider giving us a shout-out on X?
Thank you for using CodeRabbit!
✅ Review thread resolved.
You are interacting with an AI system.
@NeySlim on GitHub · 2026-10-09 09:43 UTC · original
Updated after an internal review:
overlapping-gpu-dirty-owners).TestConcurrentColdUploadsruns its uploads one after the other and still checks that a cold writable upload does not block region creation on another thread.ProtectTransientcomment: the guarantee comes from thePageManagerregion lock.View command line instructions
Manual merge helper
Use this merge commit message when completing the merge manually.
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.