gpu: invalidate whole ranges before CPU fills and copies #2063
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!2063
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-2063"
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-02 02:22 UTC · original
MrBigBritches:buffer-fill-bulk-invalidate→main· +364 −6 · 7 files · 2 commits · openWhen a DMA fill or a memory-to-memory copy targets memory that isn't GPU-modified,
BufferCachewrites it on the CPU. It then relies on the resulting write faults to invalidate cached buffers, images and image heaps, one page at a time. LEGO Batman: Legacy of the Dark Knight (PPSA16833) issues 160 KB and 288 KB DMA fills before its title screen. They caused about 7,500 such faults a second, all on the GPU command thread.FillBufferandCopyBuffernow callRenderContext::InvalidateMemoryon the destination range first. It is the range form of the same three invalidations a write fault runs. The pages are then already writable, so the CPU write no longer faults.Measured
In LEGO Batman before the title screen, faults from these fills dropped from about 15,000 per 2 s to 0. The frame rate didn't change.
Tests
A new test prepares identical clean cached ranges with an image over each, and compares two paths:
FillBufferandCopyBuffer, which must not fault, and must leave:The test harness's fault handler now counts GPU faults the way the emulator does.
@coderabbitai[bot] on GitHub · 2026-11-02 02:29 UTC · original · edited 2026-11-04 05:23 UTC
Walkthrough
RenderContext and TextureCache add sequential-write invalidation for mapped guest-memory ranges. BufferCache uses this invalidation before eligible CPU fills and copies. Compute tests compare these operations with host writes and check fault counts, memory contents, image and buffer state, and GPU-copy readback. The test fault handler now counts handled GPU faults.
Suggested reviewers:
nmzikChange: Refactor
Merge Risk:
Moderate · up to
9fc3eBulk fills and copies that start in an image's already-untracked head page can mark a GPU-written image as CPU-dirty. A later refresh may then overwrite GPU-written image contents with stale guest memory. Fix the reachability check before merging.
Pre-merge checks:
4 ·
1
Full details: Title check
Explanation
The title names the main change: the PR makes FillBuffer and CopyBuffer invalidate whole destination ranges before CPU writes, instead of relying on per-page write faults. The changeset matches this, including the RenderContext, TextureCache and BufferCache edits. The title is short and specific, so a teammate scanning history can identify the primary change.
Full details: Description check
Explanation
The description states the problem (DMA fills and memory-to-memory copies on CPU-written memory raise per-page write faults), the fix (range invalidation before the CPU write), a measured result from LEGO Batman, and the test plan. It relates directly to the changeset, so it passes the lenient check.
Full details: Docstring Coverage
Explanation
Docstring coverage is 20.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 10 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
@coderabbitai[bot] on GitHub · 2026-11-02 02:29 UTC · original · edited 2026-11-04 05:18 UTC
On
src/graphics/host_gpu/renderer/cache/bufferCache.cppline 584 (outdated):Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 5410
Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 42408
Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 42652
Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 19120
Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 14563
Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 7094
Preserve GPU-owned image data when a write enters an image from its first page.
When a fill or destination copy starts before a GPU-modified image but overlaps it, the new range invalidation marks and untracks the whole image. The old first-page fault could call
UntrackImageHeadwithout marking a multi-page image CPU-dirty. The new path can therefore trigger a full upload from stale guest backing and overwrite unwritten image data.Resolve and merge the GPU-owned image contents before applying this partial-overlap invalidation. Writes whose first fault is already inside the image have the same behavior in both paths.
✅ Addressed in commit
9fc3e6a@coderabbitai[bot] on GitHub · 2026-11-02 02:29 UTC · original · reviewed
Actionable comments posted: 1
599283d5-ed22-440f-9a84-bc814c580fd6Reviewing files that changed from the base of the PR and between
893b035526and8fc3ff3c35.src/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 10 included reviews per hour; 9 remain after this review.
@ -119,0 +125,4 @@const auto begin = image.info.data.address;const auto end = image.info.data.End();if (address >= begin) {return address < end;@coderabbitai[bot] on GitHub · 2026-11-04 05:22 UTC · original
Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 7787
Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 9471
Check the write start against the image's tracked range before marking it CPU-dirty.
SequentialWriteReachesImagereturnsaddress < endfor any write that starts inside the image. It does not account forUntrackImageHead, which removes the first page from the image's watch. A write that starts in that untracked head page does not fault on the image, but this check still returnstrue.InvalidateCpuAliasesthen callsMarkCpuDirty()andUntrackImage(id).For a GPU-modified image, this can lead to a guest re-upload. A definitely CPU-dirty image is not downloaded first (
SafeToDownload()excludes CPU-dirty images), so GPU-written bytes outside the fill can be overwritten by stale guest data. This regression comes from the new sequential-write branch added in this PR.The start-inside case needs to test the address against the image's tracked range (
track_addrthroughtrack_addr_end) instead of only the image bounds. The start-before-image branch and the tail handling must stay consistent with that check.@coderabbitai[bot] on GitHub · 2026-11-04 05:22 UTC · original · reviewed
Actionable comments posted: 1
97e77130-46c2-490c-9798-6b51a639c5e7Reviewing files that changed from the base of the PR and between
8fc3ff3c35and9fc3e6afa2.src/graphics/host_gpu/renderer/cache/bufferCache.cppsrc/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.htests/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.
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.