gpu: publish GPU writes to read-hot pages at completion #2037
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!2037
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-2037"
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?
@Nawid3333 on GitHub · 2026-10-31 21:48 UTC · original
Nawid3333:fix/publish-read-hot→main· +477 −25 · 8 files · 11 commits · openSummary
A guest CPU read of a page the GPU wrote faults into a blocking download. Demon's Souls reads the same few pages again and again while it boots, so these downloads repeat thousands of times. This change remembers the pages the guest reads and writes later GPU writes to them into guest memory as soon as the GPU has finished, so the next read needs no download.
Problem
A guest read (or write) of a GPU-written page faults, and
BufferCache::ReadMemoryrunsDownloadBufferMemory<false>: record the copy, submit, wait for the GPU, write guest memory. In a 20000-fault sample of a Demon's Souls (PPSA01341) boot without this change, the faults hit 139 distinct pages; the most frequent page faulted 2768 times.Change
NoteReadHot): at most 16 pages per read and 1024 pages in total; a page is forgotten after 8 flips without a read (DecayReadHotatFlipandPrepareCpuFlip).ObtainBufferwithis_written) queues the range.ReleaseMem,BufferFlush,FlipandPrepareCpuFlip,PublishReadHotrecords the copies into the download buffer (shared helperRecordDownloadCopies, now also used byDownloadBufferMemory, 1 MiB per publish) and defers a priority operation that writes the bytes to guest memory once the GPU has finished, plus a deferredConfirmPublishthat clears the GPU-modified state of those ranges.ObtainBuffer, a download of the range, draws and dispatches with address writes throughSupersedeAllPublishes) marks the entry superseded: the range stays GPU-modified and the next read downloads the newest data.WaitPriorityOperations), so a publish never lands after a newer CPU write.Effect
One PC: Ryzen 5 7600X, Radeon RX 9070 XT, Windows 11, AMD driver 26.10.1. Demon's Souls boot to character creation with a fixed input script and a warm shader cache, alternating runs without and with the change:
In the prototype's fault-log runs, guest-read downloads went from 4498 and 5558 to 3710 and 3618, and their median cost from 10.04 and 13.57 ms to 0.31 ms. A verify run compared 33811 published ranges with a blocking download of the same bytes: 0 mismatches.
Tests
New case
buffer_cache_hot_publish(shader_recompiler_compute_tests --buffer-cache-hot-publish-only, registered with ctest):PublishReadHotand the page is clean;Four mutations (publish disabled, supersede ignored, reads never hot, decay one flip early) each make it fail.
buffer_cache_dirty_gcstill passes. Run on the RX 9070 XT. There the Vulkan harness only starts with two local changes that are not part of this PR (#2036): therobustStorageBufferAccessSizeAlignment == 1check relaxed (the driver reports 4), andVK_EXT_pipeline_robustnessnot requested by name (core in Vulkan 1.4, not listed by this driver). The test file also needs #2010 to build on Windows.Related
#1254 reads back already-submitted data without splitting the current command buffer; this change publishes read-hot pages before the guest reads them, so fewer reads need a download and the remaining downloads are cheaper (see Effect). Both change
DownloadBufferMemoryand the write path ofObtainBuffer, so whichever lands second needs a rebase; it is mechanical, and I can rebase this one onto #1254.AI use: drafted with AI assistance (Claude Code); verified and tested by me.
@coderabbitai[bot] on GitHub · 2026-10-31 21:58 UTC · original · edited 2026-11-10 12:31 UTC
No actionable comments were generated in the recent review.
Walkthrough
The buffer cache now tracks recently read pages and publishes eligible GPU-written ranges to backing memory. Graphics operations trigger publication and decay, while compute dispatches, prepared draws, and texture writes supersede pending publishes under specified conditions. The compute test suite adds coverage for publication, expiration, superseding writes, page boundaries, and later CPU writes. CTest can run this test alone.
Suggested reviewers:
nmzikChange: Feature
Merge Risk:
Minimal · previous review
f3469No actionable merge-blocking risk is established by the supplied evidence.
Pre-merge checks:
4 ·
1
Full details: Docstring Coverage
Explanation
Docstring coverage is 0.00% 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 14 functions across 6 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
a15dcea8-181a-4831-aeb6-b85ac632d455Reviewing files that changed from the base of the PR and between
77beba28b6andf346989f15.tests/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.
@coderabbitai[bot] on GitHub · 2026-10-31 21:58 UTC · original · edited 2026-10-31 22:29 UTC
On
tests/ShaderRecompilerComputeTests.cppline 4478 (outdated):Check GPU tracking in
Dirty.Dirtychecks only the dirty-byte range. If publication clears GPU tracking for an unread page but leaves that range dirty, the cold-page assertion passes even though a guest read can observe stale backing without a read fault. Requirecache.IsRegionGpuModified(...)here too. GPU tracking controls the page’s read protection. (raw.githubusercontent.com)✅ Addressed in commit
4fd72a8@coderabbitai[bot] on GitHub · 2026-10-31 21:58 UTC · original · edited 2026-10-31 22:29 UTC
On
tests/ShaderRecompilerComputeTests.cppline 4548 (outdated):Require the neighbor’s original backing value.
The earlier
0x2000fill writes0x2468ace0across both pages. If the first read incorrectly publishes the neighbor, the later neighbor write restores its dirty state, andBacking(neighbor_offset) != 0x2468ace1ustill passes. Compare the neighbor backing withstaleto detect that cross-page publication. (raw.githubusercontent.com)✅ Addressed in commit
4fd72a8@coderabbitai[bot] on GitHub · 2026-10-31 21:58 UTC · original · reviewed
Actionable comments posted: 2
2880220e-30c6-4db4-b2a2-4c30ac80de2fReviewing files that changed from the base of the PR and between
bbf96a5e9fand29cd962462.CMakeLists.txtsrc/graphics/guest_gpu/graphicsRun.cppsrc/graphics/host_gpu/renderer/cache/bufferCache.cppsrc/graphics/host_gpu/renderer/cache/bufferCache.hsrc/graphics/host_gpu/renderer/renderCompute.cppsrc/graphics/host_gpu/renderer/renderDraw.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.
@Nawid3333 on GitHub · 2026-10-31 22:09 UTC · original
On
tests/ShaderRecompilerComputeTests.cppline 4478 (outdated):Done in
4fd72a87:Dirtynow also requiresIsRegionGpuModified, so a page whose read protection was dropped fails the check even if its bytes stay dirty. The test passes on the RX 9070 XT.@Nawid3333 on GitHub · 2026-10-31 22:09 UTC · original
On
tests/ShaderRecompilerComputeTests.cppline 4548 (outdated):Done in
4fd72a87: the neighbour page must keep its original backing (stale). The test passes, so the first read downloads only its own page and nothing crosses into the neighbour.@coderabbitai[bot] on GitHub · 2026-10-31 22:26 UTC · original · reviewed
247291be-6d27-413f-ba8b-2481c0787494Reviewing files that changed from the base of the PR and between
29cd962462and4fd72a87a9.tests/ShaderRecompilerComputeTests.cppIncluded review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 8 remain after this review.
@coderabbitai[bot] on GitHub · 2026-10-31 22:29 UTC · original
On
tests/ShaderRecompilerComputeTests.cppline 4548 (outdated):@Nawid3333 Thanks for the fix in
4fd72a87. RequiringBacking(neighbor_offset) == stalecloses the test gap: an unintended download of the neighbor’s earlier GPU value can no longer pass.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.
@coderabbitai[bot] on GitHub · 2026-10-31 22:29 UTC · original
On
tests/ShaderRecompilerComputeTests.cppline 4478 (outdated):@Nawid3333 Thanks for the fix in
4fd72a87. Requiring both checks addresses the gap inDirty. Your reported RX 9070 XT test pass provides validation.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.
@ -4758,0 +4881,4 @@Backing(owner_offset) == 0x2468ace1u && Clean(owner_offset) &&Backing(neighbor_offset) == neighbor_before &&Dirty(neighbor_offset),"a publish crossed into a page that was never read");@coderabbitai[bot] on GitHub · 2026-11-01 17:12 UTC · original · edited 2026-11-01 18:15 UTC
Capture the neighbor baseline before the first read.
Line 4543 captures
neighbor_beforeafter theReadMemorycall on Line 4542. Assume that read incorrectly downloads the whole 0x2000 buffer. In that case, the neighbor backing becomes0x2468ace0before the test captures the baseline, soneighbor_beforealso holds0x2468ace0. The laterGpuWrite(neighbor_offset, ...)sets the neighbor dirty again, soDirty(neighbor_offset)also passes. As a result, the "only the hot page of a buffer" check cannot detect a cross-page download on the first read.A previous review comment raised this gap. The reply said the neighbor must keep
stale. The current code does not seedstaleinto the neighbor and does not compare againststale.🐛 Proposed fix
@coderabbitai[bot] on GitHub · 2026-11-01 17:12 UTC · original · reviewed
Actionable comments posted: 1
bfb5b42e-6a82-408e-8bbf-953df459f695Reviewing files that changed from the base of the PR and between
4fd72a87a9and346f4a8580.CMakeLists.txtsrc/graphics/host_gpu/renderer/cache/bufferCache.cppsrc/graphics/host_gpu/renderer/cache/bufferCache.hsrc/graphics/host_gpu/renderer/cache/textureCache.cppsrc/graphics/host_gpu/renderer/renderDraw.cpptests/ShaderRecompilerComputeTests.cppIncluded review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 7 remain after this review.
@ -4758,0 +4881,4 @@Backing(owner_offset) == 0x2468ace1u && Clean(owner_offset) &&Backing(neighbor_offset) == neighbor_before &&Dirty(neighbor_offset),"a publish crossed into a page that was never read");@Nawid3333 on GitHub · 2026-11-01 18:15 UTC · original
Not applicable after the merge with main: since
1d5c8787f, the firstReadMemorydownloads the 512 KiB window around the fault, clamped to the buffer, so the neighbor page is supposed to be refreshed by that read. Withstalerestored, this section fails on main's own behavior. This section checksPublishReadHot: the publish after the second GPU write must leave the never-read neighbor untouched and still GPU-dirty. Taking the baseline after the first read keeps that check independent of the window size. A publish that crosses into the neighbor writes0x2468ace1and fails it.@ -4758,0 +4881,4 @@Backing(owner_offset) == 0x2468ace1u && Clean(owner_offset) &&Backing(neighbor_offset) == neighbor_before &&Dirty(neighbor_offset),"a publish crossed into a page that was never read");@coderabbitai[bot] on GitHub · 2026-11-01 18:15 UTC · original
@Nawid3333 You're right. I conflated the initial
ReadMemorydownload with the laterPublishReadHotoperation.The baseline belongs after
ReadMemorybecause that download can legitimately refresh the neighbor. After the second GPU write, an incorrect publish into the neighbor would change its backing value to0x2468ace1, so the comparison againstneighbor_beforewould fail.Dirty(neighbor_offset)also checks that publication did not clean the neighbor.The existing check tests the intended publication boundary. My proposed
staleassertion was incorrect. I withdraw the finding.Review thread resolved.
You are interacting with an AI system.
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.