gpu: give overlapping DMA copies memmove's result #2102
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!2102
Loading…
Reference in a new issue
No description provided.
Delete branch "gh-pr/2102/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?
@MrBigBritches on GitHub · 2026-11-04 06:33 UTC · original
MrBigBritches:dma-copy-overlap→main· +186 −2 · 3 files · 2 commits · closed without mergingA DMA_DATA copy between guest memory ranges can have an overlapping source and destination.
BufferCache::CopyBufferhandled this badly on both paths:std::memcpy, which is undefined for overlapping ranges, so the bytes depended on the C library.Buffer::CopyFromthen aborted the emulator with "Buffer: overlapping self-copy", because Vulkan forbids overlapping regions in onevkCmdCopyBuffer.Both paths now give memmove's result, as if the bytes went through a temporary buffer:
std::memmove.CopyFrom's own barriers order the two copies. The scratch buffer is freed throughDeferOperationonce the GPU is done, the same wayUploadMemoryalready frees its temporary buffer.I couldn't find documentation for how the PS5's own DMA engine handles overlap. memmove's result is well defined, and both paths now agree on it. The result no longer depends on which path a copy takes.
Tests
CheckOverlappingDmaCopies(ingpu_command_lane) runs a 16 KiB copy onto a range shifted by 16 bytes, forward and backward, on each path. Every word is unique, so a copy that reads bytes it has already written fails. Each case also checks that it takes the path it targets. The GPU cases first write the source on the GPU through a GDS round trip, then read the result back through a fault.CheckSelfCopyKeepsGpuImagerequires a GPU-written image to keep its flags after a self-copy and the range to stay unmarked. Its baseline copies onto an identical image's base from a GPU-written source, which does refresh that image.Mutations caught:
Plain
memcpyon the CPU path is not caught on Windows, because MSVC'smemcpyalready handles overlap. glibc and Apple's libc make no such promise.ctest passes on Windows except
kernel_file_system, which also fails on main.Known limitations
These apply to DMA copies in general, not only overlapping ones, and are left for a separate change:
InvalidateMemoryFromGPUonly updates an image based exactly at the written address. A later read at the image's base can then download the image over the written bytes.FindImageFromRangeonly finds exact-base images.@coderabbitai[bot] on GitHub · 2026-11-04 06:41 UTC · original · edited 2026-11-04 17:51 UTC
Walkthrough
BufferCache now handles overlapping copies on direct guest-memory and GPU-buffer paths. Direct guest-memory copies use
memmoveand skip copies between identical addresses. Overlapping GPU-buffer copies use a temporary device-local buffer whose release is deferred through the scheduler. Compute tests cover both copy directions on CPU and GPU paths, and check GPU image state for self-copies.Suggested reviewers:
nmzik,shadowbeat070Change: Bug fix
Merge Risk:
Moderate · up to
ff15cOverlapping DMA copies now behave like memmove. However, a shifted copy into a GPU-modified image can leave later rendering with stale image contents. A stream of many large overlapping copies can also exhaust GPU memory and terminate the process. Both should be resolved or explicitly accepted before merging.
Pre-merge checks:
4 ·
1
Full details: Title check
Explanation
The title names the main change: overlapping DMA copies in the GPU buffer cache now produce memmove's result on both the CPU and GPU paths. The raw summary and objectives confirm the change in BufferCache::CopyBuffer, including the memmove CPU path and the scratch-buffer GPU path, so the title is related to the changeset and specific enough for a teammate scanning history.
Full details: Description check
Explanation
The description states the bug (overlapping DMA_DATA copies handled badly by BufferCache::CopyBuffer on the CPU and GPU paths), the fix (std::memmove on the CPU path, a temporary device-local scratch buffer on the GPU path, and an early return for self-copies), and the tests added (CheckOverlappingDmaCopies and CheckSelfCopyKeepsGpuImage). It also lists known limitations. This matches the raw summary and objectives closely.
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 1 functions across 2 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-04 06:41 UTC · original · reviewed
a20b29fe-bb6a-44e3-80fb-9706ab8a8b68Reviewing files that changed from the base of the PR and between
eecd982193anded2ba2cb65.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.
@MrBigBritches on GitHub · 2026-11-04 17:46 UTC · original
Thanks. The staleness is real, but I'm leaving it out of this PR, because it isn't specific to overlapping copies.
InvalidateMemoryFromGPUonly updates an image based exactly at the written address. That applies to every GPU-path DMA copy, and to fills and storage writes, so a non-overlapping copy into the middle of an image behaves the same way. Overlapping copies usually aborted before, so this PR does make that shape of copy reachable. It reaches the same existing limitation, and it no longer stops the emulator.I don't think the proposed fix is safe as a small change:
ebe401d0removed, where a write through a separate buffer replaced untouched texels with stale backing bytes.CanDownloadis false for compressed, MSAA, FMASK and some depth images. Marking those without materializing doesn't solve it. A compressed video-out image would hit theEXITinRefreshImage, so a write inside one would abort where it doesn't today. An MSAA image is never re-uploaded, so it would stay flagged and still miss the write.FindImageFromRangeonly finds exact-base images, and it costs a full image download and upload for each inner write.This needs its own change across all of those paths, so I'd rather do it separately if a game shows it.
Checking this did turn up a related bug on a path this PR opened. An exact copy onto itself no longer aborted, but it still ran the invalidation, so an image based at that address could lose what the GPU wrote.
ff15c1d5fixes that by returning before any invalidation, with a test.@ -598,1 +605,4 @@: std::pair {&m_gds_buffer, dst_vaddr};// Cached buffers never overlap, so overlapping guest ranges always share one buffer.if (src == dst && src_offset < dst_offset + size && dst_offset < src_offset + size) {CopyOverlapping(command, *dst, src_offset, dst_offset, size);@coderabbitai[bot] on GitHub · 2026-11-04 17:51 UTC · original
Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 7046
Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 41892
Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 46445
Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 15101
Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 21401
Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 5455
Bound deferred scratch memory for overlapping DMA copies.
BufferCache::CopyOverlappingallocates a device-local scratch buffer equal to the copy size for every overlapping DMA copy.GuestGpu::Processflushes only after processing the submission, so repeated DMA packets can retain their scratch buffers at the same time. For example, 128 overlapping 16 MiB copies would request 2 GiB of scratch memory. If allocation fails, the code enters the fatalEXIT_NOT_IMPLEMENTEDpath.Reuse synchronized scratch storage or enforce a bound on the total deferred scratch allocation.
@coderabbitai[bot] on GitHub · 2026-11-04 17:51 UTC · original · reviewed
Actionable comments posted: 1
11922038-fe50-4fd5-a9a0-979273f767b4Reviewing files that changed from the base of the PR and between
ed2ba2cb65andff15c1d51d.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.
Pull request closed