gpu: stage buffer downloads in batches instead of aborting on a large one #842
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!842
Loading…
Reference in a new issue
No description provided.
Delete branch "gh-pr/842/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?
@theantipopau on GitHub · 2026-09-26 02:18 UTC · original
theantipopau:buffer-download-chunk→main· +328 −50 · 7 files · 2 commits · closed without mergingProblem
BufferCache::DownloadBufferMemoryreserved one staging region for a whole buffer read-back and aborted the process if it did not fit:total_sizeis the sum of every dirty range in the download, so the reservation grows with how much of the buffer the GPU dirtied. The limit is not a matter of timing.StreamBuffer::Mapcontains:That is a hard ceiling, checked before any waiting: a single reservation larger than the whole buffer can never succeed, even on an empty, fully drained staging buffer. So any game whose dirty ranges for one download total more than 64 MiB kills the emulator deterministically, and no amount of draining or retrying would have helped. This is what #829 reports for GTA V.
Fix
Stage the download in batches instead. Each batch reserves its own region, records its own barrier and copy, and registers its own deferred write-back. The stream keeps each region reserved until its write-back has consumed it, so consecutive batches cannot overlap.
A range larger than the whole staging buffer is now split across batches rather than dropped, which is the case that could never work before.
Two supporting details:
StreamBuffer::MaxReservation()reports the largest reservationMapwill accept. It rounds down to a whole device atom, becauseMaprounds a non-coherent reservation up tononCoherentAtomSizebefore comparing it with the buffer size. Sizing a batch toSize()would therefore still be rejected on a device whose atom does not divide the capacity evenly.AlignUp(size, 64)sum as before, so offsets and totals are byte-identical to the old single-reservation path.Tests
SplitBufferDownloadis deliberately pure, sotests/BufferDownloadBatchTests.cppcovers the packing with no device:The round-trip check flattens the batches and verifies each input range is tiled by consecutive copies that start where the previous ended and cover the whole range, so the split is proven lossless rather than merely summing correctly.
I confirmed the tests are load-bearing by disabling the batch budget guard: the suite then fails instead of passing.
Verification
buffer_download_batch_testspasses, also compiled clean under-Wall -Wextrawith g++ directly.kyty_emulatorbuilds and links.shader_recompiler_compute_tests --buffer-cache-range-onlystill passes its host-side cases, includingLeastRecentlyUsedCache.git diff --checkclean, no line over the 100-column limit.Not run: the GPU half of the compute tests, because this machine's AMD driver lacks
VK_EXT_attachment_feedback_loop_dynamic_state, soproduction rasterization features are not supportedlocally. The copy and barrier path is unchanged apart from being issued per batch, but it has not been exercised on a real device here.@coderabbitai[bot] on GitHub · 2026-09-26 02:25 UTC · original · edited 2026-09-26 03:13 UTC
Navigate logical layers of code changes, visualize relationships, and explore their blast radius.
No actionable comments were generated in the recent review. 🎉
ℹ️ Recent review info
⚙️ Run configuration
Configuration used: Repository: KytyPS5/KytyPS5/.coderabbit.yaml
Review profile: CHILL
Plan: Advanced
Run ID:
1a6fed33-6df6-443e-8cf5-9a231fde17f9📥 Commits
Reviewing files that changed from the base of the PR and between
858dda5963and338380c1c6.📒 Files selected for processing (1)
src/graphics/host_gpu/renderer/cache/bufferCache.cpp🚧 Files skipped from review as they are similar to previous changes (1)
Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 8 remain after this review.
📝 Walkthrough
Walkthrough
The change adds aligned batching for dirty GPU buffer downloads.
StreamBufferexposes the maximum reservation size, andBufferCache::DownloadBufferMemoryuses that limit to split transfers into batches. New tests cover batch sizing, alignment, range coverage, and unusable capacity. CMake builds and registers the test executable.Priority: ⬇️ Low
Change: Bug fix
Merge Risk: ⚪ Minimal · up to
33838The reviewed batching and write-back paths preserve the intended download behavior. No identified issue remains to block merge.
Security Architecture Review
Security architecture risk: 🟡 Moderate · up to
858ddA staging-buffer wrap can replace the active graphics command buffer while a download continues using its earlier handle. This may cause an emulator failure or incorrect backing-memory writes. The identified exposure is within the emulator’s graphics and memory path; broader access is not established.
Retained concerns
Security review details
Security Blast Radius
Security Findings and Attack Paths
Trust Boundaries and Controls
Hardening Proposals
🚥 Pre-merge checks | ✅ 4 | ❌ 1
❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
Comment
@coderabbitai helpto get the list of available commands.@coderabbitai[bot] on GitHub · 2026-09-26 02:25 UTC · original · edited 2026-09-26 03:14 UTC
On
src/graphics/host_gpu/renderer/cache/bufferCache.cppline 141 (outdated):🩺 Stability & Availability | 🟠 Major | ⚡ Quick win
🔎 Supported by static analysis
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 18197
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 32832
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 42288
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 42291
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 10778
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 3320
Refresh the command handle after a staging-buffer wrap.
Mapcan wait when a batch wraps. If the watch belongs to the current tick,CommandScheduler::Waitsubmits that command and starts a new one. The loop still uses thenativehandle captured beforeMap, so later batches can record into the submitted command buffer.🐛 Suggested fix
📝 Committable suggestion
✅ Addressed in commit
338380c@coderabbitai[bot] on GitHub · 2026-09-26 02:25 UTC · original · reviewed
Actionable comments posted: 1
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository: KytyPS5/KytyPS5/.coderabbit.yaml
Review profile: CHILL
Plan: Advanced
Run ID:
ea2744cf-9899-4c59-ad80-e8f1775a3856📥 Commits
Reviewing files that changed from the base of the PR and between
fd2e15ee73and858dda5963.📒 Files selected for processing (7)
CMakeLists.txtsrc/graphics/host_gpu/renderer/cache/bufferCache.cppsrc/graphics/host_gpu/renderer/cache/bufferDownloadBatch.cppsrc/graphics/host_gpu/renderer/cache/bufferDownloadBatch.hsrc/graphics/host_gpu/renderer/cache/streamBuffer.cppsrc/graphics/host_gpu/renderer/cache/streamBuffer.htests/BufferDownloadBatchTests.cppIncluded review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 6 remain after this review.
@theantipopau on GitHub · 2026-09-26 03:10 UTC · original
On
src/graphics/host_gpu/renderer/cache/bufferCache.cppline 141 (outdated):Fixed in
338380c1.Confirmed the hazard end-to-end before patching:
StreamBuffer::Map(streamBuffer.cpp:278) callsWaitPendingOperations, andCommandScheduler::Wait(commandScheduler.cpp:196-204) submits the current command buffer and callsBeginNext()when the waited tick is the current one, so the pre-loopnativecould indeed point at a submittedvk::CommandBufferfor all batches after the first wrap-wait. The original single-Map upstream code capturedcommand.Handle()afterMap, so only the batched rewrite introduced the reorder.The fix moves
EndRendering()+Handle()inside the loop afterMap/Commit:Maprecords nothing, so capturing after it (and afterCommit, which only flushes/advances offsets) keeps each batch's barriers andcopyBufferon the command buffer that is current at that point; a wait-cycled buffer yields the fresh handle.EndRendering()on a freshly re-begun buffer is a no-op (m_rendering == falseafterCommandBuffer::End()→EndRendering()), and when no wait happened it still ends the render pass exactly once as before.copieswere already loop-local, so nothing else captured the stale handle.Rebuilt
buffer_download_batch_tests+kyty_emulatorclean; the batch suite passes and host-side--buffer-cache-range-onlycases are unchanged.@coderabbitai[bot] on GitHub · 2026-09-26 03:15 UTC · original
On
src/graphics/host_gpu/renderer/cache/bufferCache.cppline 141 (outdated):@theantipopau, thanks for the detailed explanation. The currentDownloadBufferMemory()code callsEndRendering()and capturesHandle()after each batch’sMap()andCommit(). This addresses the stale command-buffer handle whenMap()waits and the scheduler begins a new command buffer.I have not independently run the tests you reported.
✏️ Learnings added
✅ Review thread resolved.
You are interacting with an AI system.
@Zezou-k on GitHub · 2026-09-26 13:58 UTC · original · edited 2026-09-26 14:04 UTC
This fixed the issue! Thank you.
@theantipopau on GitHub · 2026-09-29 21:17 UTC · original
Superseded — no longer needed.
Upstream
18a5f04d("graphics: retain oversized readbacks through GPU completion") landed the same fix forBufferCache::DownloadBufferMemory: instead of batching the dirty ranges across several staging reservations, it stages the whole range in a privateBufferthat the deferred write-back retires once the copy has been read.That is a cleaner shape than the batching I proposed here — one reservation, no split logic in
SplitBufferDownload, and the shared staging buffer is left untouched for the small downloads that still fit. Closing in favour of it.Thanks for reviewing.
Pull request closed