graphics: make CP copies and fills reach every image they overlap #2225
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!2225
Loading…
Reference in a new issue
No description provided.
Delete branch "gh-pr/2225/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?
@Nawid3333 on GitHub · 2026-11-12 01:28 UTC · original
Nawid3333:fix/partial-cp-copy-into-gpu-image-r3→main· +182 −2 · 5 files · 2 commits · closed without mergingCause
BufferCache::CopyBufferandBufferCache::FillBufferhave CPU paths and a GPU path. The CPU paths — astd::memcpycopy and a guest-memory fill — run while the involved ranges are not GPU-modified in the buffer tracker, and a copy whose source backs an image also stays off them (bufferCache.cpp:592-597 atbc934ef03). Otherwise the transfer runs on the GPU scheduler, and both GPU branches invalidate the destination first withTextureCache::InvalidateMemoryFromGPU(bufferCache.cpp:568, 600-602).InvalidateMemoryFromGPUwalks the images over the range but skips every image whose base address is not exactly the destination address (textureCache.cpp:1862-1871). A copy or fill that starts inside a GPU-written image therefore invalidates nothing: the image stays GpuModified and not BufferModified,RefreshImageuploads nothing (textureCache.cpp:1145-1167), and later samples keep the old texels. The written words never reach the image.Whole-level, single-layer copies are intercepted earlier by
TextureCache::CopyToImage(bufferCache.cpp:586-591), but that path rejects partial ranges and every linear image (!info.IsTiled()) (textureCache.cpp:1873-1915), so a partial copy into a linear image falls through to the branches above. A rejected copy may then take the memcpy branch, and there the copy itself survives: the memcpy write-faults on the tracked image pages,RenderContext::HandleFaultroutes toTextureCache::InvalidateMemory, which marks the image CpuDirty and untracks it, soSafeToDownload()is false (image.h:111-113) and the nextRefreshImagere-uploads the guest bytes (renderContext.cpp:57-71, textureCache.cpp:1942-1954). The loss is specific to the GPU branch: when the source or the destination is GPU-modified in the buffer tracker (or the source is a GPU image), CopyBuffer takes the GPU path andInvalidateMemoryFromGPUskips the image that starts before the destination.FillBuffer's GPU branch has the same hole.Side finding, not touched here: on the memcpy branch the copy survives, but the image's other GPU texels are lost when it is re-uploaded from stale guest memory (MarkCpuDirty without a prior readback), and CPU stores into a partly untracked image have the same effect (UntrackImageHead/Tail leave a page unprotected while the image stays GpuModified). Upstream treats that as deliberate; 7816ac790's test asserts "copies the image cannot take".
Change
CopyBuffer's andFillBuffer's GPU branches call the newTextureCache::PrepareBufferWrite(address, size)instead ofInvalidateMemoryFromGPU(bufferCache.cpp:568, 601 at665b874f9).PrepareBufferWrite(textureCache.cpp:1873-1891) walks every image the destination overlaps, under the texture-cache lock likeInvalidateMemoryFromGPU. An image the write covers whole — the destination starts at or before its base and its end is at or before the write's end — is cleared GpuModified and marked BufferModified, so it re-uploads from the buffer cache at its next use. A readable GPU-written image the write covers only in part first publishes its contents into the buffer cache withBcAlias::Publish(textureCache.cpp:695-706:ObtainBufferover the image marked written, thenDownloadImage), so its other texels survive; when the publish is not possible, the plain invalidation remains. Images that cannot be read back (stencil associations, MSAA, compressed video-out) keep the old base-address rule.The storage buffer path is unchanged:
NativeStorageBufferstill invalidates for written resources (descriptors.cpp:136), where the invalidated range is the descriptor's range, not the bytes the shader writes.Related work: gh search of PRs/issues for "CP copy image", "partial copy", "CopyToImage", "DMA image" finds no hits; items near these symbols are #2102 (memmove for overlapping DMA copies; touches CopyBuffer, different bug), #2067 (opt-in partial refresh for storage-buffer writes at an image's base), #2012, #1781, #931 (closed) and #937 (narrows the storage-buffer invalidation range). #2063 changes the CPU branches of the same two functions (it invalidates the whole range before a CPU fill or copy instead of taking one write fault per page); this change touches only their GPU branches. PR #1518 (FxDrops, draft) "preserve GPU contents across image aliases and buffer transfers" is prior work this builds on: it removes the base-address rule from
InvalidateMemoryFromGPUand adds a PrepareBufferWrite / PublishImageToBuffer pair before partial Fill/Copy — the same root, reached with a much larger series on an older base, and it conflicts with this change. Its head passes this test on its own base (experiments/2026-11-10-partial-cp-copy/pr1518-check.md).Tests
shader_recompiler_compute_testsaddsVulkanHarness::CheckPartialCopyIntoGpuImage(selector--partial-cp-copy-only, ctestgraphics_partial_cp_copy). It maps a 64 KiB direct allocation, fills the guest memory of three 64x64 R32Uint linear images in it with 0x5e5e5e5e, and GPU-clears two of them to 0x600d600d and marks them GPU-written. It then runs a copy and a fill into the middle of a GPU-written image: aCopyBufferof rows 16-19 of the first image from a source buffer it obtained as written (source words start at 0x10000000), and, after first marking a range of the second image GPU-modified in the buffer tracker (ObtainBufferwritten), aFillBufferof rows 16-19 with 0xf111f111. The third image is sampled once (uploaded from guest memory, never GPU-written) and then gets the sameCopyBufferof rows 16-19 from the GPU-modified source.It asserts its preconditions first: the images are GPU-written, based exactly at their addresses and linear (so
CopyToImagerejects them), the copy source is GPU-modified while the copy's destination range is not and the fill range is GPU-owned (both take the GPU path), and after each call the guest bytes at the destination are unchanged (the copy or fill did not run on the CPU). It then samples each image throughFindTexture(a sampling acquisition) and reads its texels: rows 16-19 must hold the written words, and every other texel 0x600d600d in the GPU-written images and 0x5e5e5e5e in the clean one.Main (
bc934ef03) with the test fails, with every precondition passing (keep-going run, exit 3, these three failures only):With the change it passes:
partial-cp-copy exit=0 | [gpu] PartialCopyIntoGpuImage ok, no "failed at" remains.This was run on an AMD RX 9070 XT with the lab overlays that GPU needs (
p2038-pin,feedback-optional,skip-bug13,d24->d32s8, keep-going). In the full suite (keep-going) main with the test has 25 failed checks: the three above and 22 pre-existing lab failures (RenderExecutorStencilBindingDiscovery x4, DepthAttachmentFeedback x3, ...). With the change it has those 22, with identical "failed at" lines including messages, and nothing else. clang-format-diff on the src diff produces no output.Fork CI on this head (run 43131147589) passed on Windows, Linux and macOS.
Costs
One whole-image transfer per partial write into a GPU-written image, once per GPU-write epoch (after the first publish the image is BufferModified, so further writes into it do not re-publish), plus one re-upload at the image's next use; before the change the same writes produced stale texels. Copies and fills that cover a readable non-base image whole now invalidate it — correct, its memory changed — which costs extra uploads for images that previously kept their GPU contents. The publish runs under the texture-cache lock, as it already does in
ResolveOverlap;ObtainBuffer(written)does not re-enter the texture cache (only the!is_writtentexel path does). No game evidence; proven by the test only.In a game
No game is known to show this yet.
Written with Claude Code (AI): a GLM review of
7816ac790pointed at this path and Codex costed the publish; Claude Code wrote the test and the change and ran them.@coderabbitai[bot] on GitHub · 2026-11-12 01:32 UTC · original
No actionable comments were generated in the recent review.
Walkthrough
Buffer writes now prepare overlapping texture-cache images before writing. The preparation method handles readable images with partial coverage and updates image ownership state for other selected images. Compute tests check partial copies and fills into GPU images, including cached texel values and guest backing memory. The tests run in the normal Vulkan sequence and through a dedicated CTest entry.
Suggested reviewers:
nmzik,thecruz,mrbigbritchesChange: Bug fix
Merge Risk:
Minimal · up to
665b8This change makes GPU buffer copies and fills update every overlapping cached image, and adds a test for partial writes. No concrete merge-blocking risk was established. The author reports that the new test passes with the change and that CI passed on all three platforms.
Pre-merge checks:
4
Full details: Title check
Explanation
The title names the main change: CP copies and fills (the GPU-path CopyBuffer and FillBuffer) now reach every image they overlap. The changeset adds PrepareBufferWrite to replace the base-address-only invalidation in those GPU branches, and adds a test for partial copies and fills into GPU-written images. The title is specific and matches the main change.
Full details: Description check
Explanation
The description explains the root cause (InvalidateMemoryFromGPU skips images whose base address differs from the destination), the change (PrepareBufferWrite replacing InvalidateMemoryFromGPU in the GPU branches of CopyBuffer and FillBuffer), the new test CheckPartialCopyIntoGpuImage and its results, and the costs. It relates directly to the changeset.
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
616cb43d-0a91-4053-86cd-cc3fa97b2662Reviewing files that changed from the base of the PR and between
bc934ef034and665b874f96.CMakeLists.txtsrc/graphics/host_gpu/renderer/cache/bufferCache.cppsrc/graphics/host_gpu/renderer/cache/textureCache.cppsrc/graphics/host_gpu/renderer/cache/textureCache.htests/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.
Pull request closed