gpu: hold game-thread BDA events until the command processor resumes #1933
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!1933
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-1933"
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-10-27 04:47 UTC · original
MrBigBritches:bda-sync-defer→main· +230 −6 · 6 files · 2 commits · openBefore every BDA draw, BDA synchronization uploads the pages written since the previous one. A write fault on a game thread queued its page at once. So when a frame had many BDA draws, a page the game kept writing for later frames was uploaded, write-protected, and faulted on again at nearly every one.
Events from threads other than the GPU thread are now held back. They are promoted when the command processor starts or resumes a submission, or reads a guest value that a game thread may have written after the data it guards.
This is the second part of #1809, split out on its own. The first part, uploading BDA synchronization through one staging copy, is #1932. This PR is independent of it.
Changes
BufferCache::QueueBdaSynctakes adeferredflag. Invalidations on any thread but the GPU thread (game threads, file I/O and so on) go to a separate set. GPU-thread writes still queue immediately, since later draws of the same submission can read them.BufferCache::PromoteBdaSyncmoves the held-back set into the oneTakeBdaSyncreturns. A flag set with each deferred event lets it return without taking the lock when nothing is held back. When the immediate set is empty, the held-back set is swapped in whole.GuestGpu::Process, at every submission slice.CommandProcessor::PromoteGuestWrites(), after reading a guest value:WAIT_REG_MEM,COND_EXEC,BRANCH,REWINDthat is already valid,SET_PREDICATIONon a memory value (op 3),MEM_SEMAPHOREwait that consumes a count.WAIT_REG_MEM. Without the others, a game that writes data and then sets a flag one of them reads could leave the BDA draws after it reading stale data.Measurements
Octopath Traveler II, the same route on three builds, all based on
main986721f5:main.The figures are per-second averages over matching stretches of each run. They come from a temporary log of the emulator's debug counters, which is not part of this PR.
mainmainmainmainmain. That suggests the difference is run-to-run noise. Each build ran once, so variance hasn't been measured directly.SET_PREDICATIONop-3 packets per second. Each one now promotes, which costs one atomic load when nothing is held back.Testing
mainin the measured burst, and both logs are free of errors.shader_recompiler_compute_tests:BdaSyncDeferredEventsTakeBdaSyncuntil promoted, and are promoted exactly once into both an empty and a non-empty set.Pm4ControlReadsPromoteBdaSyncWAIT_REG_MEM,COND_EXEC,BRANCH,REWIND,SET_PREDICATIONandMEM_SEMAPHOREeach promote a held-back event.BdaSyncSubmissionPromotesCOND_EXEC,REWINDorMEM_SEMAPHORE, or from submission start, each fails the matching test.BdaSyncUploadswrites from the test thread, so its write is now held back. It now checks that the write is held back, then promotes it before its existing checks, which are unchanged.main:vdecswdoesn't build, becausestd::aligned_allocisn't available in MSVC's C runtime.kernel_file_systemcrashes with0xc0000028.main, and #1922 fixes them.Conflicts
This PR and #1928 both change
CommandProcessor::WaitRegMem. Whichever merges second needs a small rebase.@coderabbitai[bot] on GitHub · 2026-10-27 04:56 UTC · original · edited 2026-10-27 05:20 UTC
No actionable comments were generated in the recent review.
bdd5948a-b00c-46ce-9e86-a55fab9396aaReviewing files that changed from the base of the PR and between
f84350b413andd6d22a51a0.tests/ShaderRecompilerComputeTests.cppIncluded review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 6 remain after this review.
Walkthrough
BufferCache stores off-GPU-thread BDA invalidation ranges separately until promotion. CommandProcessor and GuestGpu promote these ranges at submission and guest control-read points. Tests cover deferred ranges, promotion behavior, and the relevant PM4 packets.
Suggested reviewers:
nmzikPriority:
Normal
Change: Bug fix
Merge Risk:
Low · up to
d6d22The submission-promotion test has undefined cross-thread behavior, but production promotion ordering appears correct.
Pre-merge checks:
4 ·
1
Full details: Docstring Coverage
Explanation
Docstring coverage is 15.38% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 13 functions across 5 files. (1 skipped: 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
Comment
@coderabbitai helpto get the list of available commands.@ -5766,0 +5819,4 @@"a held-back BDA event was visible before the submission");// The submission only writes a marker, so the test can tell it has started.volatile uint32_t done = 0;@coderabbitai[bot] on GitHub · 2026-10-27 04:56 UTC · original · edited 2026-10-27 05:12 UTC
Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 7954
Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 17074
Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 11591
Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 11381
Use a bounded idle wait before reading the marker.
volatiledoes not synchronize theWriteDatastore with the test-thread read. Add a timeout-capableGuestGpu::WaitForIdle()overload and call it before readingdone. If the deadline expires, fail without readingdone; otherwise read the marker after the GPU thread has become idle.Do not add an unbounded
WaitForIdle()after the existing poll.TakeBdaSync()already synchronizes the BDA queue withPromoteBdaSync().@coderabbitai[bot] on GitHub · 2026-10-27 04:56 UTC · original · reviewed
Actionable comments posted: 1
af32b6cb-b779-42db-82de-311444584b6dReviewing files that changed from the base of the PR and between
db12f0c5fbandf84350b413.src/graphics/guest_gpu/command_processor/commandProcessor.hsrc/graphics/guest_gpu/command_processor/pm4Handlers.cppsrc/graphics/guest_gpu/graphicsRun.cppsrc/graphics/host_gpu/renderer/cache/bufferCache.cppsrc/graphics/host_gpu/renderer/cache/bufferCache.htests/ShaderRecompilerComputeTests.cppIncluded review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.
@ -5766,0 +5819,4 @@"a held-back BDA event was visible before the submission");// The submission only writes a marker, so the test can tell it has started.volatile uint32_t done = 0;@MrBigBritches on GitHub · 2026-10-27 05:05 UTC · original
The race is formally real but harmless on x86-64. Fixed by calling the existing WaitForIdle() after the bounded poll. It's only reached once the marker is seen, and it synchronizes through the GPU queue mutex, so no timeout API is added. Note the TakeBdaSync/PromoteBdaSync mutex gives exclusion, not ordering.
@ -5766,0 +5819,4 @@"a held-back BDA event was visible before the submission");// The submission only writes a marker, so the test can tell it has started.volatile uint32_t done = 0;@coderabbitai[bot] on GitHub · 2026-10-27 05:13 UTC · original
🧩 Analysis chain
🏁 Script executed:
Length of output: 9544
@MrBigBritchesI verified thatWaitForIdle()now runs after the marker check and beforeTakeBdaSync(). The GPU queue mutex orders completed submission processing before that assertion.You are right about my earlier BDA mutex claim: mutual exclusion alone does not ensure that promotion runs before
TakeBdaSync().The added wait does not remove the formal race in the earlier
volatilepoll. It fixes the ordering of the promotion assertion. I understand that you accept the remaining polling race for x86-64, and I will not press for a timeout API. The original race finding remains technically unresolved.If you found this review helpful, would you consider giving us a shout-out on X?
Thank you for using CodeRabbit!
You are interacting with an AI system.
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.