[draft] gpu: make CPU access wait for pending image readbacks #2120
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!2120
Loading…
Reference in a new issue
No description provided.
Delete branch "gh-pr/2120/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?
@MrBigBritches on GitHub · 2026-11-05 04:44 UTC · original
MrBigBritches:readback-fault-wait→main· +2855 −49 · 21 files · 9 commits · closed without mergingSome games read a GPU-written image on the CPU right after its end-of-pipe label.
RELEASE_MEMwrites that label when the command processor reaches it, before the host GPU has finished, so the game reads old data.Kena: Bridge of Spirits shows this as objects flickering and vanishing while the camera moves. It looks like UE4's HZB occlusion test: verdicts are drawn to a 256x256 target and copied by a compute dispatch to a linear image, and the CPU then reads that image. With old verdicts, the game skips draws for visible objects.
With
--readback-fault-wait, a pending readback's guest pages are blocked until its data lands, so a guest access waits for the data to land. Labels are still written on time. Holding labels instead fixes Kena but lowers Astro Bot's render resolution, which is why #1928 removed them.The flag is off by default and requires
--readback-linear-images. There is no launcher field yet, so it is command-line only.Stacked on #1928. Its two commits come first here, and this stays a draft until that PR lands.
Commits
These follow #1928's two commits.
page_manager: add a readback hold count. A hold blocks all guest access to a page and is counted separately from watchers.texture_cache: add ReadbackGuard. Tracks held ranges and waits. A wait gives up after 2 s without GPU progress or 30 s in total, and the hold is then lifted so the guest reads old data rather than hanging. Three timeouts in a row stop holds for the session.texture_cache: hold qualifying readbacks until they land. Only linear images with one level, one layer and one depth slice qualify, up to 1 MiB, and only their whole pages are held. Private mappings are excluded.gpu: make guest faults wait for held readbacks. Each kind of thread takes the route it can safely take:Waitrather thanFinish, since pending operations could free a buffer the faulting code is still using.gpu: wait for held readbacks before host file calls. A host call fails on a held page instead of faulting, so file reads and writes wait first.config: add --readback-fault-wait. With no image readback on, the flag does nothing and logs one line saying so.gpu: reword and wrap readback fault comments. No code changes.Behaviour change with the flag on
Write faults on any mapped page change slightly, not only faults on held pages. If no watcher still explains a fault, it's treated as stale and retried, without entering the normal path. Without this, a fault taken under the texture cache's lock could try to take that lock again.
In the longest Kena run this fired 449,861 times, almost all on writes. Only 5,946 repeated the same address as that thread's last stale fault, and the longest streak was 99. It also fires when no pages are held at all, so holds don't cause it. Those numbers point to threads racing on one page rather than a thread stuck retrying, though I haven't confirmed that cause.
Tested
All runs are on Windows, with
--readback-linear-imagesand--readback-tiled-imageson. Flag off and on refer to--readback-fault-wait. Fps is the overlay's 30 s average from single runs.a7e14aa7, plus an unstaged measurement patch and an environment switch in place of the flag.07e87b95: one Kena pair with the real flag, started from the command line, gave 24.8 fps with flicker with the flag off, and 23.0 fps without flicker with it on. That launch path wrote no log, so this pair has no counts, and its fps can't be compared with the launcher runs above.Only Kena exercises the wait path. Astro Bot and Octopath show holds are harmless when nothing reads them, and LEGO and Yumia hold nothing.
Tests
page_manager: hold counts and how they combine with watchers.readback_guard(new): the qualifying gate, waits, timeouts, progress, the stop after repeated timeouts, and lifts.shader_recompiler_compute:ReadbackHoldAtLabel: pages are blocked while pending and open afterwards, the label is still written at once, images that don't qualify are never held, and unmapping is safe with a hold pending.ReadbackFaultWait: each thread route, including a guest write that a baseline with the flag off loses to the landing and that now survives.ReadbackFaultWaitFlag: the flag's default, and that it needs a readback option.The full suite passes apart from three existing problems:
common,kernel_file_system, andvdecsw_tests, which doesn't build on Windows (#1966). On my Windows build the compute test also needed#undef nearand#undef farto compile. I added those locally and left them out of this PR.Mutations. Each change was checked by breaking it and confirming that a test fails. About 90 mutations were killed. These survive:
page_manager: the tracked-space guard being off by one, which needs a sanitizer.ReadbackGuard: a timeout lift not notifying other waiters, which only delays them by one wait slice, andEXIT_IFon an empty progress source.These have no automated coverage:
KernelReadsocket branch,KernelPwriteandWritev, which were only checked by reading.main.cpptable, checked with--helpand the command-line Kena run.Known issues
page_managerandreadback_guardrun there.recvandsend,getsockopt,setsockopt, Linuxselectand futex, andlibSaveData'sWriteFilestill pass guest pointers to the OS without waiting. None looks like a readback target.ReadFilereceives the full length.mprotectopens a held page, a release can restore read-write over a guest read-only setting, and direct-memory aliases at another address aren't held.CompleteFliporTriggerInterrupt.@coderabbitai[bot] on GitHub · 2026-11-05 04:51 UTC · original
No actionable comments were generated in the recent review.
Walkthrough
The change adds optional page holds for eligible GPU image readbacks. Guest faults and host calls can wait for pending readbacks, while GPU-related execution paths can lift holds when they cannot wait. Readback submission is coordinated with labels, and tests and build workflows now cover the guard, page tracking, and access paths.
Suggested reviewers:
nmzikChange: Bug fix
Merge Risk:
Minimal · up to
4d226The change adds an opt-in, default-off flag that lets CPU access wait for pending GPU image readbacks. No concrete merge-blocking defect was established, so behavior with the flag off should be unchanged. Testing was reported on Windows only, so Linux and macOS behavior with the flag on is unverified.
Pre-merge checks:
4 ·
1
Full details: Docstring Coverage
Explanation
Docstring coverage is 44.59% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 74 functions across 18 files. (3 skipped: 2 unsupported, 1 too large.)
Resolution
Write docstrings for the functions missing them to satisfy the coverage threshold.
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
203da161-1f11-41ef-bc41-33f7bad9c5a0Reviewing files that changed from the base of the PR and between
07e87b950aand4d226996f5..github/workflows/build.ymlCMakeLists.txtsrc/common/emulatorConfig.cppsrc/common/emulatorConfig.hsrc/graphics/guest_gpu/command_processor/commandProcessor.hsrc/graphics/guest_gpu/graphicsRun.cppsrc/graphics/host_gpu/pageManager.cppsrc/graphics/host_gpu/pageManager.hsrc/graphics/host_gpu/renderer/cache/readbackGuard.cppsrc/graphics/host_gpu/renderer/cache/readbackGuard.hsrc/graphics/host_gpu/renderer/cache/textureCache.cppsrc/graphics/host_gpu/renderer/cache/textureCache.hsrc/graphics/host_gpu/renderer/renderContext.cppsrc/graphics/host_gpu/renderer/renderContext.hsrc/kernel/fileSystem.cppsrc/kernel/memory.cppsrc/kernel/memory.hsrc/main.cpptests/PageManagerTests.cpptests/ReadbackGuardTests.cpptests/ShaderRecompilerComputeTests.cppIncluded review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.
@Nmzik on GitHub · 2026-11-07 16:55 UTC · original
@MrBigBritches merged & credited
Pull request closed