gpu: give overlapping DMA copies memmove's result #2102

Closed
github-bot wants to merge 2 commits from gh-pr/2102/head into gh-pr/2102/base
Owner

@⁠MrBigBritches on GitHub · 2026-11-04 06:33 UTC · original
MrBigBritches:dma-copy-overlap → main · +186 −2 · 3 files · 2 commits · closed without merging

A DMA_DATA copy between guest memory ranges can have an overlapping source and destination. BufferCache::CopyBuffer handled this badly on both paths:

  • CPU path (neither range GPU-modified, no GPU-written image based at the source): it used std::memcpy, which is undefined for overlapping ranges, so the bytes depended on the C library.
  • GPU path: cached buffers never overlap, so both ranges usually resolve to the same buffer. Buffer::CopyFrom then aborted the emulator with "Buffer: overlapping self-copy", because Vulkan forbids overlapping regions in one vkCmdCopyBuffer.

Both paths now give memmove's result, as if the bytes went through a temporary buffer:

  • The CPU path uses std::memmove.
  • The GPU path copies through a one-off device-local scratch buffer, in two copies. CopyFrom's own barriers order the two copies. The scratch buffer is freed through DeferOperation once the GPU is done, the same way UploadMemory already frees its temporary buffer.
  • A copy onto itself changes nothing, so it returns before any invalidation. An image based at that address keeps what the GPU wrote.

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 (in gpu_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.

CheckSelfCopyKeepsGpuImage requires 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:

  • the GPU path without the overlap routing, which aborts again
  • a forward word-by-word CPU copy
  • no early return for a self-copy
  • a self-copy that skips only the image invalidation
  • a self-copy that invalidates the image and then returns

Plain memcpy on the CPU path is not caught on Windows, because MSVC's memcpy already 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:

  • A GPU-path write into the middle of an image doesn't update that image, because InvalidateMemoryFromGPU only 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.
  • A copy that reads from inside a GPU-written image, not from its base, reads the buffer or guest memory, not the image, because FindImageFromRange only finds exact-base images.
**@⁠MrBigBritches** on GitHub · 2026-11-04 06:33 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/2102) `MrBigBritches:dma-copy-overlap` → `main` · +186 −2 · 3 files · 2 commits · closed without merging A DMA_DATA copy between guest memory ranges can have an overlapping source and destination. `BufferCache::CopyBuffer` handled this badly on both paths: - **CPU path** (neither range GPU-modified, no GPU-written image based at the source): it used `std::memcpy`, which is undefined for overlapping ranges, so the bytes depended on the C library. - **GPU path**: cached buffers never overlap, so both ranges usually resolve to the same buffer. `Buffer::CopyFrom` then aborted the emulator with "Buffer: overlapping self-copy", because Vulkan forbids overlapping regions in one `vkCmdCopyBuffer`. Both paths now give memmove's result, as if the bytes went through a temporary buffer: - The CPU path uses `std::memmove`. - The GPU path copies through a one-off device-local scratch buffer, in two copies. `CopyFrom`'s own barriers order the two copies. The scratch buffer is freed through `DeferOperation` once the GPU is done, the same way `UploadMemory` already frees its temporary buffer. - A copy onto itself changes nothing, so it returns before any invalidation. An image based at that address keeps what the GPU wrote. 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` (in `gpu_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. `CheckSelfCopyKeepsGpuImage` requires 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: - the GPU path without the overlap routing, which aborts again - a forward word-by-word CPU copy - no early return for a self-copy - a self-copy that skips only the image invalidation - a self-copy that invalidates the image and then returns Plain `memcpy` on the CPU path is not caught on Windows, because MSVC's `memcpy` already 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: - A GPU-path write into the middle of an image doesn't update that image, because `InvalidateMemoryFromGPU` only 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. - A copy that reads from inside a GPU-written image, not from its base, reads the buffer or guest memory, not the image, because `FindImageFromRange` only finds exact-base images.
A DMA_DATA copy between guest memory ranges can overlap itself. The CPU path copied with
std::memcpy, which is undefined for overlapping ranges, so the result depended on the C library.
The GPU path resolves both ranges to the same cached buffer, since cached buffers never overlap.
Buffer::CopyFrom then aborted with "overlapping self-copy". Vulkan forbids overlapping regions
in one copy.

Both paths now give memmove's result, as if the bytes went through a temporary buffer. The CPU
path uses std::memmove. On the GPU path, BufferCache::CopyOverlapping copies the source into a
one-off device-local scratch buffer and from there to the destination. Each CopyFrom call places
its own barriers, and those barriers order the two copies. The scratch buffer is freed through
DeferOperation once the GPU has finished, as UploadMemory does with its temporary buffer. A copy
onto itself changes nothing and returns early.

CheckOverlappingDmaCopies runs forward and backward overlaps on each path, with every word unique,
and checks which path each case takes. Mutations caught: the GPU path without the overlap routing
(it aborts), and a forward word-by-word CPU copy. Plain memcpy is not caught on Windows, because
MSVC's memcpy already handles overlap. Other C libraries make no such promise.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The previous commit stopped a copy onto the same address from aborting by skipping the Vulkan copy,
but CopyBuffer had already run the rest of the GPU path. InvalidateMemoryFromGPU cleared the GPU
flag of an image based at that address and marked it buffer-modified. The source sync then refused
to download it, so the image later refreshed from a buffer that never held what the GPU wrote. A
compressed video-out image would abort on that refresh instead.

CopyBuffer now returns right after validation when both sides are guest memory at the same address,
before any invalidation or marking. No bytes change, so whichever copy was authoritative stays so.
CopyBuffer also gains its summary comment.

CheckSelfCopyKeepsGpuImage requires 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: no early return, an early return replaced
by skipping only the image invalidation, and an early return that invalidates the image first.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 10:14:17 +00:00
github-bot closed this pull request 2026-11-26 10:14:17 +00:00
Author
Owner

@⁠coderabbitai[bot] on GitHub · 2026-11-04 06:41 UTC · original · edited 2026-11-04 17:51 UTC

Explain Change →

Walkthrough

BufferCache now handles overlapping copies on direct guest-memory and GPU-buffer paths. Direct guest-memory copies use memmove and 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, shadowbeat070

Change: Bug fix

Merge Risk: Moderate · up to ff15c

Overlapping 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: Passed 4 · Inconclusive 1

Failed Failed checks: 1 inconclusive

Check name Status Explanation Resolution
Docstring Coverage Inconclusive 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. Documentat… Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Title check Passed 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 BufferC…
Description check Passed 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 …
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
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](https://github.com/KytyPS5/KytyPS5/pull/2102#issuecomment-6624452443) · edited 2026-11-04 17:51 UTC <!-- coderabbit-ai-generated:v1 --> <!-- This is an auto-generated comment: summarize by coderabbit.ai --> <!-- review_stack_entry_start --> <a href="https://app.coderabbit.ai/change-stack/KytyPS5/KytyPS5/pull/2102?scope=ghh_OusAJGA4HG8ljoOV0wJvVx8NmnZIjc2DdjmxOu0xZWA&amp;cs_source=review_comment"><picture><source media="(prefers-color-scheme: dark)" srcset="https://storage.googleapis.com/coderabbit_public_assets/review-stack-buttons-v1/explain-change-dark.svg"><source media="(prefers-color-scheme: light)" srcset="https://storage.googleapis.com/coderabbit_public_assets/review-stack-buttons-v1/explain-change.svg"><img src="https://storage.googleapis.com/coderabbit_public_assets/review-stack-buttons-v1/explain-change.svg" alt="Explain Change →" height="32"></picture></a> <!-- review_stack_entry_end --> <!-- walkthrough_start --> <details> <summary><strong>Walkthrough</strong></summary> <dl> <dd> BufferCache now handles overlapping copies on direct guest-memory and GPU-buffer paths. Direct guest-memory copies use `memmove` and 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`, `shadowbeat070` </dd> </dl> </details> <!-- walkthrough_end --> <!-- change_assessment_start --> <!-- change_assessment_commit:"ff15c1d51ddc19502d3cea39a6f93a0a1e07ba77" --> <!-- change_assessment_priority:{"version":1,"urgency":"p2","manual":false} --> **Change:** Bug fix <!-- change_assessment_end --> <details> <summary><strong>Merge Risk:</strong> <strong><picture><img src="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/risk-normal-16.svg" width="16" height="16" align="absmiddle" alt=""></picture> Moderate</strong> · up to <code>ff15c</code></summary> <dl> <dd> <!-- final_review_risk_start --> <!-- final_review_risk_label **Merge Risk:** **🟡 Moderate** · up to `ff15c` --> <!-- final_review_risk_coverage:{"sourceCommitId":"ff15c1d51ddc19502d3cea39a6f93a0a1e07ba77","coveredCommitId":"ff15c1d51ddc19502d3cea39a6f93a0a1e07ba77","kind":"reviewed"} --> Overlapping 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. <!-- final_review_risk_end --> </dd> </dl> </details> <!-- pre_merge_checks_walkthrough_start --> <details> <summary><strong>Pre-merge checks: <sup><picture><img src="https://storage.googleapis.com/coderabbit_public_assets/pre-merge-checks-counter-passed-14-v2.svg" width="14" height="14" align="middle" alt="Passed" title="Passed"></picture></sup> 4 · <sup><picture><img src="https://storage.googleapis.com/coderabbit_public_assets/pre-merge-checks-counter-inconclusive-14-v2.svg" width="14" height="14" align="middle" alt="Inconclusive" title="Inconclusive"></picture></sup> 1</strong></summary> <dl> <dd> <strong><sup><picture><img src="https://storage.googleapis.com/coderabbit_public_assets/pre-merge-checks-counter-failed-14-v2.svg" width="14" height="14" align="middle" alt="Failed" title="Failed"></picture></sup> Failed checks: 1 inconclusive</strong> | Check name | Status | Explanation | Resolution | | :---------------- | :--------------------------------------------------------------------------------------------------------: | :-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | :--------------------------------------------------------------------------------- | | Docstring Coverage | ![Inconclusive](https://storage.googleapis.com/coderabbit_public_assets/pre-merge-checks-inconclusive.svg) | 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. Documentat… | Write docstrings for the functions missing them to satisfy the coverage threshold. | <details> <summary><strong><sup><picture><img src="https://storage.googleapis.com/coderabbit_public_assets/pre-merge-checks-counter-passed-14-v2.svg" width="14" height="14" align="middle" alt="Passed" title="Passed"></picture></sup> Passed checks: 4 passed</strong></summary> <dl> <dd> | Check name | Status | Explanation | | :------------------------ | :--------------------------------------------------------------------------------------------: | :-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | Title check | ![Passed](https://storage.googleapis.com/coderabbit_public_assets/pre-merge-checks-passed.svg) | 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 BufferC… | | Description check | ![Passed](https://storage.googleapis.com/coderabbit_public_assets/pre-merge-checks-passed.svg) | 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 … | | Linked Issues check | ![Passed](https://storage.googleapis.com/coderabbit_public_assets/pre-merge-checks-passed.svg) | Check skipped because no linked issues were found for this pull request. | | Out of Scope Changes check | ![Passed](https://storage.googleapis.com/coderabbit_public_assets/pre-merge-checks-passed.svg) | Check skipped because no linked issues were found for this pull request. | <details> <summary><strong>Full details: Title check</strong></summary> <dl> <dd> **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. </dd> </dl> </details> <details> <summary><strong>Full details: Description check</strong></summary> <dl> <dd> **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. </dd> </dl> </details> </dd> </dl> </details> <details> <summary><strong>Full details: Docstring Coverage</strong></summary> <dl> <dd> **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. </dd> </dl> </details> </dd> </dl> </details> <!-- pre_merge_checks_walkthrough_end --> <!-- tips_start --> Thanks for using [CodeRabbit](https://coderabbit.ai?utm_source=oss&utm_medium=github&utm_campaign=KytyPS5/KytyPS5&utm_content=2102)! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. <details> <summary><strong>Share</strong></summary> <dl> <dd> - [X](https://twitter.com/intent/tweet?text=I%20just%20used%20%40coderabbitai%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20the%20proprietary%20code.%20Check%20it%20out%3A&url=https%3A//coderabbit.ai) - [Mastodon](https://mastodon.social/share?text=I%20just%20used%20%40coderabbitai%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20the%20proprietary%20code.%20Check%20it%20out%3A%20https%3A%2F%2Fcoderabbit.ai) - [Reddit](https://www.reddit.com/submit?title=Great%20tool%20for%20code%20review%20-%20CodeRabbit&text=I%20just%20used%20CodeRabbit%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20proprietary%20code.%20Check%20it%20out%3A%20https%3A//coderabbit.ai) - [LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fcoderabbit.ai&mini=true&title=Great%20tool%20for%20code%20review%20-%20CodeRabbit&summary=I%20just%20used%20CodeRabbit%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20proprietary%20code) </dd> </dl> </details> <!-- tips_end -->
Author
Owner

@⁠coderabbitai[bot] on GitHub · 2026-11-04 06:41 UTC · original · reviewed

Caution

Some comments are outside the diff and can’t be posted inline due to GitHub limitations.

⚠️ Outside diff range comments (1)

Major · Materialize and invalidate every intersecting image before the overlapping… · bufferCache.cpp:593

src/graphics/host_gpu/renderer/cache/bufferCache.cpp:593
Major · Data Integrity & Integration · Heavy lift

Materialize and invalidate every intersecting image before the overlapping GPU copy.

InvalidateMemoryFromGPU finds intersecting images but updates only the image whose base equals dst_vaddr. When the new overlap branch writes to a shifted destination inside a GPU-modified image, the backing buffer can change while the cached image remains unchanged. Subsequent rendering can use stale image data.

Materialize the required image bytes before the copy, then mark every image intersecting [dst_vaddr, dst_vaddr + size) as buffer-modified. The exact-base limitation is pre-existing, but the new overlap branch makes this previously unsupported case execute.

Review info
Run configuration
Commits

Reviewing files that changed from the base of the PR and between eecd982193 and ed2ba2cb65.

Files selected for processing: 3
  • src/graphics/host_gpu/renderer/cache/bufferCache.cpp
  • src/graphics/host_gpu/renderer/cache/bufferCache.h
  • tests/ShaderRecompilerComputeTests.cpp

Included 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-11-04 06:41 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/2102#pullrequestreview-5764315499) · **reviewed** <!-- coderabbit-ai-generated:v1 --> > [!CAUTION] > Some comments are outside the diff and can’t be posted inline due to GitHub limitations. > > **⚠️ Outside diff range comments (1)** > > <details> > <summary><strong><picture><img src="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/major-16.svg" width="16" height="16" align="absmiddle" alt=""></picture> Major</strong> · Materialize and invalidate every intersecting image before the overlapping… · <code>bufferCache.cpp:593</code></summary><blockquote> > <dl> > <dd> > > `src/graphics/host_gpu/renderer/cache/bufferCache.cpp:593` > **<picture><img src="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/major-16.svg" width="16" height="16" align="absmiddle" alt=""></picture> Major** · **Data Integrity & Integration** · **Heavy lift** > > **Materialize and invalidate every intersecting image before the overlapping GPU copy.** > > `InvalidateMemoryFromGPU` finds intersecting images but updates only the image whose base equals `dst_vaddr`. When the new overlap branch writes to a shifted destination inside a GPU-modified image, the backing buffer can change while the cached image remains unchanged. Subsequent rendering can use stale image data. > > Materialize the required image bytes before the copy, then mark every image intersecting `[dst_vaddr, dst_vaddr + size)` as buffer-modified. The exact-base limitation is pre-existing, but the new overlap branch makes this previously unsupported case execute. > > <!-- cr-comment:v1:c29c3486ce685a2a85871d22 --> > > </dd> > </dl> > </blockquote></details> <h6> <details> <summary><picture><img src="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/info.svg" width="12" height="12" align="absmiddle" alt=""></picture> Review info</summary> <dl> <dd> <details> <summary><picture><source media="(prefers-color-scheme: dark)" srcset="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/cog-dark.svg"><img src="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/cog.svg" width="16" height="16" align="absmiddle" alt=""></picture> Run configuration</summary> <dl> <dd> - **Source mode**: Auto - **Repository**: [KytyPS5/KytyPS5](<https://github.com/KytyPS5/KytyPS5>) - **Branch**: [main](<https://github.com/KytyPS5/KytyPS5/tree/main>) - **File**: [.coderabbit.yaml](<https://github.com/KytyPS5/KytyPS5/blob/eecd982193ba5f3df3ddec500847ea3d99b87a0c/.coderabbit.yaml>) - **Commit**: [eecd982](<https://github.com/KytyPS5/KytyPS5/commit/eecd982193ba5f3df3ddec500847ea3d99b87a0c>) - **Layers**: Repository YAML - **Review profile**: CHILL - **Plan**: Advanced - **Run ID**: `a20b29fe-bb6a-44e3-80fb-9706ab8a8b68` </dd> </dl> </details> <details> <summary><picture><source media="(prefers-color-scheme: dark)" srcset="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/git-commit-horizontal-dark.svg"><img src="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/git-commit-horizontal.svg" width="16" height="16" align="absmiddle" alt=""></picture> Commits</summary> <dl> <dd> Reviewing files that changed from the base of the PR and between eecd982193ba5f3df3ddec500847ea3d99b87a0c and ed2ba2cb65d92e87c1cb1ef6f995ec7e36b8691d. </dd> </dl> </details> <details> <summary><picture><source media="(prefers-color-scheme: dark)" srcset="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/file-plus-corner-dark.svg"><img src="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/file-plus-corner.svg" width="16" height="16" align="absmiddle" alt=""></picture> Files selected for processing: 3</summary> <dl> <dd> * `src/graphics/host_gpu/renderer/cache/bufferCache.cpp` * `src/graphics/host_gpu/renderer/cache/bufferCache.h` * `tests/ShaderRecompilerComputeTests.cpp` </dd> </dl> </details> **Included review availability:** This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review. </dd> </dl> </details> </h6> <!-- This is an auto-generated comment by CodeRabbit for review status --> <!-- coderabbit-review-publication v1 publication=32fa44a9-0970-4705-9453-05d3370db29e attempt=783cfcb6-275c-4539-a6d3-e1027f6504a2 batch=1/1 -->
Author
Owner

@⁠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.

InvalidateMemoryFromGPU only 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:

  • Widening the invalidation by itself would bring back what ebe401d0 removed, where a write through a separate buffer replaced untouched texels with stale backing bytes.
  • Materializing first only works for images that can be downloaded, and CanDownload is false for compressed, MSAA, FMASK and some depth images. Marking those without materializing doesn't solve it. A compressed video-out image would hit the EXIT in RefreshImage, 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.
  • It needs a new lookup, because FindImageFromRange only 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. ff15c1d5 fixes that by returning before any invalidation, with a test.

**@⁠MrBigBritches** on GitHub · 2026-11-04 17:46 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/2102#issuecomment-6637777222) Thanks. The staleness is real, but I'm leaving it out of this PR, because it isn't specific to overlapping copies. `InvalidateMemoryFromGPU` only 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: - Widening the invalidation by itself would bring back what ebe401d0 removed, where a write through a separate buffer replaced untouched texels with stale backing bytes. - Materializing first only works for images that can be downloaded, and `CanDownload` is false for compressed, MSAA, FMASK and some depth images. Marking those without materializing doesn't solve it. A compressed video-out image would hit the `EXIT` in `RefreshImage`, 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. - It needs a new lookup, because `FindImageFromRange` only 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. ff15c1d5 fixes 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);
Author
Owner

@⁠coderabbitai[bot] on GitHub · 2026-11-04 17:51 UTC · original

Major · Stability & Availability · Heavy lift

Supported by static analysis

Script executed:

grep -n 'CopyOverlapping' -r src/graphics/host_gpu/renderer/cache/
sed -n '570,680p' src/graphics/host_gpu/renderer/cache/bufferCache.cpp
grep -rn 'DeferOperation\|DeferRelease\|DeferDestroy' src/graphics/host_gpu | head -30

Repository: KytyPS5/KytyPS5

Length of output: 7046


Script executed:

set -o pipefail
printf '%s\n' '--- scheduler declarations and implementation ---'
rg -n -F --glob '*.{h,cpp}' -e 'class CommandScheduler' -e 'Current()' -e 'Submit' -e 'Flush' -e 'DeferOperation' -e 'Process.*Defer' -e 'defer' src/graphics/host_gpu/renderer/commandScheduler.h src/graphics/host_gpu/renderer/commandScheduler.cpp
sed -n '1,280p' src/graphics/host_gpu/renderer/commandScheduler.h
sed -n '1,280p' src/graphics/host_gpu/renderer/commandScheduler.cpp
printf '%s\n' '--- Buffer allocation and failure handling ---'
rg -n -F --glob '*.{h,cpp}' -e 'Buffer::Buffer' -e 'class Buffer' -e 'MemoryUsage::DeviceLocal' -e 'eOutOfDeviceMemory' -e 'allocate' src/graphics/host_gpu/renderer/cache src/graphics/host_gpu/renderer | head -160
rg -n -F --glob '*.{h,cpp}' -e 'EXIT(' -e 'VK_CHECK' -e 'ResultException' src/graphics/host_gpu/renderer/cache/buffer.cpp src/graphics/host_gpu/renderer/cache/buffer.h src/graphics/host_gpu/renderer 2>/dev/null | head -160
printf '%s\n' '--- analogous temporary buffer paths ---'
sed -n '1,90p' src/graphics/host_gpu/renderer/cache/bufferCache.cpp
sed -n '1,180p' src/graphics/host_gpu/renderer/cache/streamBuffer.cpp
rg -n -F --glob '*.{h,cpp}' -e 'Temporary' -e 'temporary' -e 'make_unique<Buffer>' -e 'new Buffer' src/graphics/host_gpu/renderer | head -240
printf '%s\n' '--- repeated copy command path and scheduler boundaries ---'
rg -n -F --glob '*.{h,cpp}' -e 'CopyBuffer' -e 'CopyOverlapping' -e 'gpu_command_lane' -e 'CommandBuffer' src/graphics/host_gpu | head -260
rg -n -F --glob '*.{h,cpp}' -e 'Submit(' -e 'Flush(' -e 'Finish(' -e 'Wait' src/graphics/host_gpu | head -260

Repository: KytyPS5/KytyPS5

Length of output: 41892


Script executed:

set -o pipefail
printf '%s\n' '--- exact scheduler deferred lifetime and submission code ---'
sed -n '135,205p' src/graphics/host_gpu/renderer/commandScheduler.cpp
sed -n '292,350p' src/graphics/host_gpu/renderer/commandScheduler.cpp
printf '%s\n' '--- Buffer constructor and failure macro ---'
sed -n '1,125p' src/graphics/host_gpu/renderer/cache/streamBuffer.cpp
rg -n -F --glob '*.{h,cpp}' -e '#define EXIT_NOT_IMPLEMENTED' -e 'EXIT_NOT_IMPLEMENTED' src/common src | head -80
rg -n -F --glob '*.{h,cpp}' -e '#define EXIT_IF' -e 'void Exit' -e '[[noreturn]]' src/common src | head -100
printf '%s\n' '--- all CopyBuffer callers ---'
rg -n -F --glob '*.{h,cpp}' -- 'CopyBuffer(' src
printf '%s\n' '--- command-lane and render submission boundaries ---'
rg -n -i --glob '*.{h,cpp}' -e 'command.?lane' -e 'Finish()' -e 'Flush()' -e 'BeginCommand' src/graphics | head -220
sed -n '1,145p' src/graphics/host_gpu/renderer/renderContext.cpp
printf '%s\n' '--- analogous deferred temporary allocation ---'
sed -n '1,65p' src/graphics/host_gpu/renderer/cache/bufferCache.cpp
sed -n '380,425p' src/graphics/host_gpu/renderer/cache/bufferCache.cpp
sed -n '270,325p' src/graphics/host_gpu/renderer/cache/streamBuffer.cpp

Repository: KytyPS5/KytyPS5

Length of output: 46445


Script executed:

set -o pipefail
printf '%s\n' '--- fatal macro definition ---'
sed -n '1,80p' src/common/assert.h
printf '%s\n' '--- guest copy command and surrounding processing ---'
sed -n '120,230p' src/graphics/guest_gpu/graphicsRun.cpp
sed -n '315,365p' src/graphics/guest_gpu/graphicsRun.cpp
sed -n '800,925p' src/graphics/guest_gpu/graphicsRun.cpp
sed -n '985,1025p' src/graphics/guest_gpu/graphicsRun.cpp

Repository: KytyPS5/KytyPS5

Length of output: 15101


Script executed:

set -o pipefail
printf '%s\n' '--- GuestGpu processing and end-of-slice boundary ---'
rg -n -F --glob '*.cpp' -e 'GuestGpu::Process' -e 'void GuestGpu::Process' -e 'commands' src/graphics/guest_gpu/graphicsRun.cpp | head -100
sed -n '500,800p' src/graphics/guest_gpu/graphicsRun.cpp
sed -n '800,930p' src/graphics/guest_gpu/graphicsRun.cpp
printf '%s\n' '--- submission structure and command limits ---'
rg -n -F --glob '*.{h,cpp}' -e 'struct Submission' -e 'class Submission' -e 'm_submission_count' -e 'CommandBufferSize' -e 'command stream' src/graphics/guest_gpu

Repository: KytyPS5/KytyPS5

Length of output: 21401


Script executed:

set -o pipefail
sed -n '45,72p' src/graphics/guest_gpu/graphicsRun.h
sed -n '370,505p' src/graphics/guest_gpu/graphicsRun.cpp

Repository: KytyPS5/KytyPS5

Length of output: 5455


Bound deferred scratch memory for overlapping DMA copies.

BufferCache::CopyOverlapping allocates a device-local scratch buffer equal to the copy size for every overlapping DMA copy. GuestGpu::Process flushes 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 fatal EXIT_NOT_IMPLEMENTED path.

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](https://github.com/KytyPS5/KytyPS5/pull/2102#discussion_r4471184509) <!-- coderabbit-ai-generated:v1 --> **<picture><img src="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/major-16.svg" width="16" height="16" align="absmiddle" alt=""></picture> Major** · **Stability & Availability** · **Heavy lift** <details> <summary><picture><source media="(prefers-color-scheme: dark)" srcset="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/review-details-dark.svg"><img src="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/review-details.svg" width="16" height="16" align="absmiddle" alt=""></picture> Supported by static analysis</summary> <dl> <dd> Script executed: ```bash grep -n 'CopyOverlapping' -r src/graphics/host_gpu/renderer/cache/ sed -n '570,680p' src/graphics/host_gpu/renderer/cache/bufferCache.cpp grep -rn 'DeferOperation\|DeferRelease\|DeferDestroy' src/graphics/host_gpu | head -30 ``` Repository: KytyPS5/KytyPS5 Length of output: 7046 --- Script executed: ```bash set -o pipefail printf '%s\n' '--- scheduler declarations and implementation ---' rg -n -F --glob '*.{h,cpp}' -e 'class CommandScheduler' -e 'Current()' -e 'Submit' -e 'Flush' -e 'DeferOperation' -e 'Process.*Defer' -e 'defer' src/graphics/host_gpu/renderer/commandScheduler.h src/graphics/host_gpu/renderer/commandScheduler.cpp sed -n '1,280p' src/graphics/host_gpu/renderer/commandScheduler.h sed -n '1,280p' src/graphics/host_gpu/renderer/commandScheduler.cpp printf '%s\n' '--- Buffer allocation and failure handling ---' rg -n -F --glob '*.{h,cpp}' -e 'Buffer::Buffer' -e 'class Buffer' -e 'MemoryUsage::DeviceLocal' -e 'eOutOfDeviceMemory' -e 'allocate' src/graphics/host_gpu/renderer/cache src/graphics/host_gpu/renderer | head -160 rg -n -F --glob '*.{h,cpp}' -e 'EXIT(' -e 'VK_CHECK' -e 'ResultException' src/graphics/host_gpu/renderer/cache/buffer.cpp src/graphics/host_gpu/renderer/cache/buffer.h src/graphics/host_gpu/renderer 2>/dev/null | head -160 printf '%s\n' '--- analogous temporary buffer paths ---' sed -n '1,90p' src/graphics/host_gpu/renderer/cache/bufferCache.cpp sed -n '1,180p' src/graphics/host_gpu/renderer/cache/streamBuffer.cpp rg -n -F --glob '*.{h,cpp}' -e 'Temporary' -e 'temporary' -e 'make_unique<Buffer>' -e 'new Buffer' src/graphics/host_gpu/renderer | head -240 printf '%s\n' '--- repeated copy command path and scheduler boundaries ---' rg -n -F --glob '*.{h,cpp}' -e 'CopyBuffer' -e 'CopyOverlapping' -e 'gpu_command_lane' -e 'CommandBuffer' src/graphics/host_gpu | head -260 rg -n -F --glob '*.{h,cpp}' -e 'Submit(' -e 'Flush(' -e 'Finish(' -e 'Wait' src/graphics/host_gpu | head -260 ``` Repository: KytyPS5/KytyPS5 Length of output: 41892 --- Script executed: ```bash set -o pipefail printf '%s\n' '--- exact scheduler deferred lifetime and submission code ---' sed -n '135,205p' src/graphics/host_gpu/renderer/commandScheduler.cpp sed -n '292,350p' src/graphics/host_gpu/renderer/commandScheduler.cpp printf '%s\n' '--- Buffer constructor and failure macro ---' sed -n '1,125p' src/graphics/host_gpu/renderer/cache/streamBuffer.cpp rg -n -F --glob '*.{h,cpp}' -e '#define EXIT_NOT_IMPLEMENTED' -e 'EXIT_NOT_IMPLEMENTED' src/common src | head -80 rg -n -F --glob '*.{h,cpp}' -e '#define EXIT_IF' -e 'void Exit' -e '[[noreturn]]' src/common src | head -100 printf '%s\n' '--- all CopyBuffer callers ---' rg -n -F --glob '*.{h,cpp}' -- 'CopyBuffer(' src printf '%s\n' '--- command-lane and render submission boundaries ---' rg -n -i --glob '*.{h,cpp}' -e 'command.?lane' -e 'Finish()' -e 'Flush()' -e 'BeginCommand' src/graphics | head -220 sed -n '1,145p' src/graphics/host_gpu/renderer/renderContext.cpp printf '%s\n' '--- analogous deferred temporary allocation ---' sed -n '1,65p' src/graphics/host_gpu/renderer/cache/bufferCache.cpp sed -n '380,425p' src/graphics/host_gpu/renderer/cache/bufferCache.cpp sed -n '270,325p' src/graphics/host_gpu/renderer/cache/streamBuffer.cpp ``` Repository: KytyPS5/KytyPS5 Length of output: 46445 --- Script executed: ```bash set -o pipefail printf '%s\n' '--- fatal macro definition ---' sed -n '1,80p' src/common/assert.h printf '%s\n' '--- guest copy command and surrounding processing ---' sed -n '120,230p' src/graphics/guest_gpu/graphicsRun.cpp sed -n '315,365p' src/graphics/guest_gpu/graphicsRun.cpp sed -n '800,925p' src/graphics/guest_gpu/graphicsRun.cpp sed -n '985,1025p' src/graphics/guest_gpu/graphicsRun.cpp ``` Repository: KytyPS5/KytyPS5 Length of output: 15101 --- Script executed: ```bash set -o pipefail printf '%s\n' '--- GuestGpu processing and end-of-slice boundary ---' rg -n -F --glob '*.cpp' -e 'GuestGpu::Process' -e 'void GuestGpu::Process' -e 'commands' src/graphics/guest_gpu/graphicsRun.cpp | head -100 sed -n '500,800p' src/graphics/guest_gpu/graphicsRun.cpp sed -n '800,930p' src/graphics/guest_gpu/graphicsRun.cpp printf '%s\n' '--- submission structure and command limits ---' rg -n -F --glob '*.{h,cpp}' -e 'struct Submission' -e 'class Submission' -e 'm_submission_count' -e 'CommandBufferSize' -e 'command stream' src/graphics/guest_gpu ``` Repository: KytyPS5/KytyPS5 Length of output: 21401 --- Script executed: ```bash set -o pipefail sed -n '45,72p' src/graphics/guest_gpu/graphicsRun.h sed -n '370,505p' src/graphics/guest_gpu/graphicsRun.cpp ``` Repository: KytyPS5/KytyPS5 Length of output: 5455 --- </dd> </dl> </details> **Bound deferred scratch memory for overlapping DMA copies.** `BufferCache::CopyOverlapping` allocates a device-local scratch buffer equal to the copy size for every overlapping DMA copy. `GuestGpu::Process` flushes 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 fatal `EXIT_NOT_IMPLEMENTED` path. Reuse synchronized scratch storage or enforce a bound on the total deferred scratch allocation. <!-- fingerprinting:phantom:medusa:beignet55 --> <!-- cr-indicator-types:potential_issue --> <!-- cr-comment:v1:3d234d92c48c456bb1a3520b --> <!-- This is an auto-generated comment by CodeRabbit -->
Author
Owner

@⁠coderabbitai[bot] on GitHub · 2026-11-04 17:51 UTC · original · reviewed

Actionable comments posted: 1

Review info
Run configuration
Commits

Reviewing files that changed from the base of the PR and between ed2ba2cb65 and ff15c1d51d.

Files selected for processing: 3
  • src/graphics/host_gpu/renderer/cache/bufferCache.cpp
  • src/graphics/host_gpu/renderer/cache/bufferCache.h
  • tests/ShaderRecompilerComputeTests.cpp
Files skipped from review as they are similar to previous changes: 1
  • src/graphics/host_gpu/renderer/cache/bufferCache.h

Included 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-11-04 17:51 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/2102#pullrequestreview-5773097641) · **reviewed** <!-- coderabbit-ai-generated:v1 --> **Actionable comments posted: 1** <h6> <details> <summary><picture><img src="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/info.svg" width="12" height="12" align="absmiddle" alt=""></picture> Review info</summary> <dl> <dd> <details> <summary><picture><source media="(prefers-color-scheme: dark)" srcset="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/cog-dark.svg"><img src="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/cog.svg" width="16" height="16" align="absmiddle" alt=""></picture> Run configuration</summary> <dl> <dd> - **Source mode**: Auto - **Repository**: [KytyPS5/KytyPS5](<https://github.com/KytyPS5/KytyPS5>) - **Branch**: [main](<https://github.com/KytyPS5/KytyPS5/tree/main>) - **File**: [.coderabbit.yaml](<https://github.com/KytyPS5/KytyPS5/blob/eecd982193ba5f3df3ddec500847ea3d99b87a0c/.coderabbit.yaml>) - **Commit**: [eecd982](<https://github.com/KytyPS5/KytyPS5/commit/eecd982193ba5f3df3ddec500847ea3d99b87a0c>) - **Layers**: Repository YAML - **Review profile**: CHILL - **Plan**: Advanced - **Run ID**: `11922038-fe50-4fd5-a9a0-979273f767b4` </dd> </dl> </details> <details> <summary><picture><source media="(prefers-color-scheme: dark)" srcset="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/git-commit-horizontal-dark.svg"><img src="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/git-commit-horizontal.svg" width="16" height="16" align="absmiddle" alt=""></picture> Commits</summary> <dl> <dd> Reviewing files that changed from the base of the PR and between ed2ba2cb65d92e87c1cb1ef6f995ec7e36b8691d and ff15c1d51ddc19502d3cea39a6f93a0a1e07ba77. </dd> </dl> </details> <details> <summary><picture><source media="(prefers-color-scheme: dark)" srcset="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/file-plus-corner-dark.svg"><img src="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/file-plus-corner.svg" width="16" height="16" align="absmiddle" alt=""></picture> Files selected for processing: 3</summary> <dl> <dd> * `src/graphics/host_gpu/renderer/cache/bufferCache.cpp` * `src/graphics/host_gpu/renderer/cache/bufferCache.h` * `tests/ShaderRecompilerComputeTests.cpp` </dd> </dl> </details> <details> <summary><picture><source media="(prefers-color-scheme: dark)" srcset="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/file-similar-dark.svg"><img src="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/file-similar.svg" width="16" height="16" align="absmiddle" alt=""></picture> Files skipped from review as they are similar to previous changes: 1</summary> <dl> <dd> * src/graphics/host_gpu/renderer/cache/bufferCache.h </dd> </dl> </details> **Included review availability:** This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review. </dd> </dl> </details> </h6> <!-- This is an auto-generated comment by CodeRabbit for review status --> <!-- coderabbit-review-publication v1 publication=52216c79-dc15-4c78-af57-801575a380f4 attempt=77824819-8c34-41ca-b0e7-a5ee4a755384 batch=1/1 -->

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No description provided.