gpu: invalidate whole ranges before CPU fills and copies #2063

Open
github-bot wants to merge 2 commits from github-bot/gh-2063 into main AGit
Owner

@⁠MrBigBritches on GitHub · 2026-11-02 02:22 UTC · original
MrBigBritches:buffer-fill-bulk-invalidate → main · +364 −6 · 7 files · 2 commits · open

When a DMA fill or a memory-to-memory copy targets memory that isn't GPU-modified, BufferCache writes 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.

FillBuffer and CopyBuffer now call RenderContext::InvalidateMemory on 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.

  • Range checked exactly: an image that starts partway into a page the write touches is marked CPU-dirty, where a fault on that page might only have marked it maybe-dirty.
  • Partly mapped ranges: a range that isn't fully mapped is written as before.

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:

  • A plain host write, which faults once per page.
  • FillBuffer and CopyBuffer, which must not fault, and must leave:
    • the same bytes
    • the range CPU-modified
    • the image CPU-dirty and untracked
    • an updated GPU copy

The test harness's fault handler now counts GPU faults the way the emulator does.

**@⁠MrBigBritches** on GitHub · 2026-11-02 02:22 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/2063) `MrBigBritches:buffer-fill-bulk-invalidate` → `main` · +364 −6 · 7 files · 2 commits · open When a DMA fill or a memory-to-memory copy targets memory that isn't GPU-modified, `BufferCache` writes 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. `FillBuffer` and `CopyBuffer` now call `RenderContext::InvalidateMemory` on 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. - **Range checked exactly:** an image that starts partway into a page the write touches is marked CPU-dirty, where a fault on that page might only have marked it maybe-dirty. - **Partly mapped ranges:** a range that isn't fully mapped is written as before. ## 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: - **A plain host write,** which faults once per page. - **`FillBuffer` and `CopyBuffer`,** which must not fault, and must leave: - the same bytes - the range CPU-modified - the image CPU-dirty and untracked - an updated GPU copy The test harness's fault handler now counts GPU faults the way the emulator does.
When a DMA fill or a memory-to-memory copy targets memory that is not
GPU modified, BufferCache writes it on the CPU and relied on the
resulting write faults to invalidate cached buffers, images and image
heaps one page at a time. In LEGO Batman: Legacy of the Dark Knight
(PPSA16833) the 160 KB and 288 KB DMA fills it issues before the title
screen caused about 7,500 such faults a second, all on the GPU command
thread.

FillBuffer and CopyBuffer now call RenderContext::InvalidateMemory for
the destination range first. That is the range form of the same three
invalidations a write fault runs, so the pages are already writable and
the CPU write no longer faults. The range is checked exactly, so an
image that starts partway into a page the write touches is marked CPU
dirty where a fault on that page might only have marked it maybe dirty.
A range that is not fully mapped is written as before.

The new test prepares identical clean cached ranges with an image over
each, and compares a plain host write, which faults once per page, with
FillBuffer and CopyBuffer, which must not fault and must leave the same
bytes, the range CPU modified, the image CPU dirty and untracked, and an
updated GPU copy. The test harness fault handler now counts GPU faults
the way the emulator does.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The bulk invalidation before a CPU fill or copy checked images against the exact written range.
A write that starts before a multi-page image and ends inside the image's first page therefore
marked the image CPU dirty, so its next use re-uploaded it from guest memory and lost anything
the GPU had written to it. The per-page faults it replaces only ever landed before such an image,
which untracks the image's head page and leaves the rest of the image alone.

The fill and copy now call RenderContext::InvalidateSequentialWrite, which invalidates buffers and
image heaps as before but marks an image CPU dirty only when a front-to-back write starts inside
it or crosses a page boundary inside it. Everything else takes the same head, tail and maybe-dirty
paths as a fault would. RenderContext::InvalidateMemory keeps its meaning for its other callers.
memcpy's write order is unspecified, so the copy is treated as front to back like the fill.

CheckBulkHeadPageFill puts a GPU-modified image partway into a page and fills up to a quarter
page into it, once with a plain host write and once with FillBuffer. The plain write must fault
and only untrack the head page. FillBuffer must not fault and must leave the image the same way.
Mutations caught: matching images by exact overlap again, and FillBuffer skipping the bulk
invalidation so it falls back to faulting.

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

@⁠coderabbitai[bot] on GitHub · 2026-11-02 02:29 UTC · original · edited 2026-11-04 05:23 UTC

Explain Change →

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: nmzik

Change: Refactor

Merge Risk: Moderate · up to 9fc3e

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

Failed Failed checks: 1 inconclusive

Check name Status Explanation Resolution
Docstring Coverage Inconclusive 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. Documenta… 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: 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, …
Description check Passed 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 …
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: 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](https://github.com/KytyPS5/KytyPS5/pull/2063#issuecomment-6567621682) · edited 2026-11-04 05:23 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/2063?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> 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:** `nmzik` </dd> </dl> </details> <!-- walkthrough_end --> <!-- change_assessment_start --> <!-- change_assessment_commit:"9fc3e6afa2e5ef2eaf101ff0233cbcb9cbb0e09d" --> <!-- change_assessment_priority:{"version":1,"urgency":"p2","manual":false} --> **Change:** Refactor <!-- 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>9fc3e</code></summary> <dl> <dd> <!-- final_review_risk_start --> <!-- final_review_risk_label **Merge Risk:** **🟡 Moderate** · up to `9fc3e` --> <!-- final_review_risk_coverage:{"sourceCommitId":"9fc3e6afa2e5ef2eaf101ff0233cbcb9cbb0e09d","coveredCommitId":"9fc3e6afa2e5ef2eaf101ff0233cbcb9cbb0e09d","kind":"reviewed"} --> Bulk 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. <!-- 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 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. Documenta… | 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: 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, … | | Description check | ![Passed](https://storage.googleapis.com/coderabbit_public_assets/pre-merge-checks-passed.svg) | 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 … | | 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: 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. </dd> </dl> </details> <details> <summary><strong>Full details: Description check</strong></summary> <dl> <dd> **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. </dd> </dl> </details> </dd> </dl> </details> <details> <summary><strong>Full details: Docstring Coverage</strong></summary> <dl> <dd> **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. </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=2063)! 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-02 02:29 UTC · original · edited 2026-11-04 05:18 UTC
On src/graphics/host_gpu/renderer/cache/bufferCache.cpp line 584 (outdated):

@@ -576,7 +576,12 @@ void BufferCache::FillBuffer(uint64_t vaddr, uint64_t size, uint32_t value, bool
 	}
 	(void)m_texture_cache.ClearMeta(vaddr);
 	if (!IsRegionGpuModified(vaddr, size)) {
-		// Access the guest mapping so write faults invalidate cached buffers and images.
+		// Invalidate cached buffers, images and image heaps for the whole range in one call.
+		// Letting the write fault page by page costs one fault per page on the GPU thread.
+		// The exact range is checked here, so an image that starts mid-page is marked CPU dirty
+		// where a fault on its first page might only have marked it maybe dirty.
+		// If the range is not fully mapped, fill anyway so the write faults as it did before.
+		(void)m_scheduler.Context().InvalidateMemory(vaddr, size);

Major · Data Integrity & Integration · Heavy lift

Supported by static analysis

Script executed:

rg -n 'InvalidateMemory|InvalidateCpuAliases|IsRegionGpuModified|OnCpuWrite|InitializeImage' src/graphics/host_gpu/renderer

Repository: KytyPS5/KytyPS5

Length of output: 5410


Script executed:

set -o pipefail
printf '%s\n' '--- bufferCache.cpp relevant ranges ---'
nl -ba src/graphics/host_gpu/renderer/cache/bufferCache.cpp | sed -n '270,335p;480,635p;650,745p'
printf '%s\n' '--- renderContext.cpp fault and range paths ---'
nl -ba src/graphics/host_gpu/renderer/renderContext.cpp | sed -n '35,130p'
printf '%s\n' '--- textureCache.cpp invalidation and image initialization ---'
nl -ba src/graphics/host_gpu/renderer/cache/textureCache.cpp | sed -n '990,1045p;1060,1140p;1170,1240p;1705,1760p;1885,1965p'
printf '%s\n' '--- declarations and tracker operations ---'
rg -n -F --glob '*.{h,cpp}' -e 'Untrack' -e 'Track' -e 'Flush' -e 'InvalidateCpuAliases' -e 'MarkCpu' -e 'CpuDirty' -e 'IsRegionGpuModified' src/graphics/host_gpu/renderer src/graphics/host_gpu/memory || true

Repository: KytyPS5/KytyPS5

Length of output: 42408


Script executed:

set -o pipefail
printf '%s\n' '--- bufferCache.cpp ---'
nl -ba src/graphics/host_gpu/renderer/cache/bufferCache.cpp | sed -n '270,335p;480,635p;650,745p'
printf '%s\n' '--- renderContext.cpp ---'
nl -ba src/graphics/host_gpu/renderer/renderContext.cpp | sed -n '35,130p'
printf '%s\n' '--- textureCache.cpp ---'
nl -ba src/graphics/host_gpu/renderer/cache/textureCache.cpp | sed -n '990,1045p;1060,1140p;1170,1240p;1705,1760p;1885,1965p'
printf '%s\n' '--- tracker-related definitions ---'
rg -n -F --glob '*.{h,cpp}' -e 'Untrack' -e 'Track' -e 'Flush' -e 'InvalidateCpuAliases' -e 'MarkCpu' -e 'CpuDirty' -e 'IsRegionGpuModified' src/graphics/host_gpu/renderer src/graphics/host_gpu/memory || true

Repository: KytyPS5/KytyPS5

Length of output: 42652


Script executed:

nl -ba src/graphics/host_gpu/renderer/cache/bufferCache.cpp | sed -n '500,630p'
nl -ba src/graphics/host_gpu/renderer/renderContext.cpp | sed -n '55,90p'
nl -ba src/graphics/host_gpu/renderer/cache/textureCache.cpp | sed -n '1000,1135p;1718,1750p;1915,1955p'

Repository: KytyPS5/KytyPS5

Length of output: 19120


Script executed:

nl -ba src/graphics/host_gpu/renderer/cache/textureCache.cpp | sed -n '280,420p'
nl -ba src/graphics/host_gpu/renderer/cache/textureCache.cpp | sed -n '1300,1370p'
nl -ba src/graphics/host_gpu/renderer/image/image.h | sed -n '60,135p'
rg -n -F --glob '*.{h,cpp}' -e 'FindImagesInRegion' -e 'UntrackImageHead' -e 'TrackImageHead' src/graphics/host_gpu/renderer/cache src/graphics/host_gpu/renderer/image

Repository: KytyPS5/KytyPS5

Length of output: 14563


Script executed:

nl -ba src/graphics/host_gpu/renderer/cache/textureCache.cpp | sed -n '300,410p'
nl -ba src/graphics/host_gpu/renderer/image/image.h | sed -n '68,130p'

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 UntrackImageHead without 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](https://github.com/KytyPS5/KytyPS5/pull/2063#discussion_r4438425144) · edited 2026-11-04 05:18 UTC On `src/graphics/host_gpu/renderer/cache/bufferCache.cpp` line 584 (outdated): ```diff @@ -576,7 +576,12 @@ void BufferCache::FillBuffer(uint64_t vaddr, uint64_t size, uint32_t value, bool } (void)m_texture_cache.ClearMeta(vaddr); if (!IsRegionGpuModified(vaddr, size)) { - // Access the guest mapping so write faults invalidate cached buffers and images. + // Invalidate cached buffers, images and image heaps for the whole range in one call. + // Letting the write fault page by page costs one fault per page on the GPU thread. + // The exact range is checked here, so an image that starts mid-page is marked CPU dirty + // where a fault on its first page might only have marked it maybe dirty. + // If the range is not fully mapped, fill anyway so the write faults as it did before. + (void)m_scheduler.Context().InvalidateMemory(vaddr, size); ``` <!-- 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** · **Data Integrity & Integration** · **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 rg -n 'InvalidateMemory|InvalidateCpuAliases|IsRegionGpuModified|OnCpuWrite|InitializeImage' src/graphics/host_gpu/renderer ``` Repository: KytyPS5/KytyPS5 Length of output: 5410 --- Script executed: ```bash set -o pipefail printf '%s\n' '--- bufferCache.cpp relevant ranges ---' nl -ba src/graphics/host_gpu/renderer/cache/bufferCache.cpp | sed -n '270,335p;480,635p;650,745p' printf '%s\n' '--- renderContext.cpp fault and range paths ---' nl -ba src/graphics/host_gpu/renderer/renderContext.cpp | sed -n '35,130p' printf '%s\n' '--- textureCache.cpp invalidation and image initialization ---' nl -ba src/graphics/host_gpu/renderer/cache/textureCache.cpp | sed -n '990,1045p;1060,1140p;1170,1240p;1705,1760p;1885,1965p' printf '%s\n' '--- declarations and tracker operations ---' rg -n -F --glob '*.{h,cpp}' -e 'Untrack' -e 'Track' -e 'Flush' -e 'InvalidateCpuAliases' -e 'MarkCpu' -e 'CpuDirty' -e 'IsRegionGpuModified' src/graphics/host_gpu/renderer src/graphics/host_gpu/memory || true ``` Repository: KytyPS5/KytyPS5 Length of output: 42408 --- Script executed: ```bash set -o pipefail printf '%s\n' '--- bufferCache.cpp ---' nl -ba src/graphics/host_gpu/renderer/cache/bufferCache.cpp | sed -n '270,335p;480,635p;650,745p' printf '%s\n' '--- renderContext.cpp ---' nl -ba src/graphics/host_gpu/renderer/renderContext.cpp | sed -n '35,130p' printf '%s\n' '--- textureCache.cpp ---' nl -ba src/graphics/host_gpu/renderer/cache/textureCache.cpp | sed -n '990,1045p;1060,1140p;1170,1240p;1705,1760p;1885,1965p' printf '%s\n' '--- tracker-related definitions ---' rg -n -F --glob '*.{h,cpp}' -e 'Untrack' -e 'Track' -e 'Flush' -e 'InvalidateCpuAliases' -e 'MarkCpu' -e 'CpuDirty' -e 'IsRegionGpuModified' src/graphics/host_gpu/renderer src/graphics/host_gpu/memory || true ``` Repository: KytyPS5/KytyPS5 Length of output: 42652 --- Script executed: ```bash nl -ba src/graphics/host_gpu/renderer/cache/bufferCache.cpp | sed -n '500,630p' nl -ba src/graphics/host_gpu/renderer/renderContext.cpp | sed -n '55,90p' nl -ba src/graphics/host_gpu/renderer/cache/textureCache.cpp | sed -n '1000,1135p;1718,1750p;1915,1955p' ``` Repository: KytyPS5/KytyPS5 Length of output: 19120 --- Script executed: ```bash nl -ba src/graphics/host_gpu/renderer/cache/textureCache.cpp | sed -n '280,420p' nl -ba src/graphics/host_gpu/renderer/cache/textureCache.cpp | sed -n '1300,1370p' nl -ba src/graphics/host_gpu/renderer/image/image.h | sed -n '60,135p' rg -n -F --glob '*.{h,cpp}' -e 'FindImagesInRegion' -e 'UntrackImageHead' -e 'TrackImageHead' src/graphics/host_gpu/renderer/cache src/graphics/host_gpu/renderer/image ``` Repository: KytyPS5/KytyPS5 Length of output: 14563 --- Script executed: ```bash nl -ba src/graphics/host_gpu/renderer/cache/textureCache.cpp | sed -n '300,410p' nl -ba src/graphics/host_gpu/renderer/image/image.h | sed -n '68,130p' ``` Repository: KytyPS5/KytyPS5 Length of output: 7094 --- </dd> </dl> </details> **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 `UntrackImageHead` without 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. <!-- fingerprinting:phantom:medusa:beignet55 --> <!-- cr-indicator-types:potential_issue --> <!-- cr-comment:v1:e415e8446d0e91bdbb44673e --> <!-- This is an auto-generated comment by CodeRabbit --> ✅ Addressed in commit 9fc3e6a
Author
Owner

@⁠coderabbitai[bot] on GitHub · 2026-11-02 02:29 UTC · original · reviewed

Actionable comments posted: 1


Review info
Run configuration
Commits

Reviewing files that changed from the base of the PR and between 893b035526 and 8fc3ff3c35.

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-02 02:29 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/2063#pullrequestreview-5731507382) · **reviewed** <!-- coderabbit-ai-generated:v1 --> **Actionable comments posted: 1** --- <details> <summary><picture><img src="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/info.svg" width="16" height="16" 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/893b03552699dc873474023bc1f4c69c57fb36b2/.coderabbit.yaml>) - **Commit**: [893b035](<https://github.com/KytyPS5/KytyPS5/commit/893b03552699dc873474023bc1f4c69c57fb36b2>) - **Layers**: Repository YAML - **Review profile**: CHILL - **Plan**: Advanced - **Run ID**: `599283d5-ed22-440f-9a84-bc814c580fd6` </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 893b03552699dc873474023bc1f4c69c57fb36b2 and 8fc3ff3c351e2e1b65280790b31240b9fe529319. </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> <!-- This is an auto-generated comment by CodeRabbit for review status --> <!-- coderabbit-review-publication v1 publication=702be057-3e24-4414-a781-b3b0e7edad50 attempt=c44aab84-37ce-4625-bd03-daa06408200d batch=1/1 -->
@ -119,0 +125,4 @@
const auto begin = image.info.data.address;
const auto end = image.info.data.End();
if (address >= begin) {
return address < end;
Author
Owner

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

Major · Data Integrity & Integration · Heavy lift

Supported by static analysis

Script executed:

sed -n 105,140p src/graphics/host_gpu/renderer/cache/textureCache.cpp
sed -n 1935,2010p src/graphics/host_gpu/renderer/cache/textureCache.cpp
rg -n 'UntrackImageHead|track_addr|IsDefinitelyCpuDirty|MarkCpuDirty' src/graphics/host_gpu/renderer | head -60

Repository: KytyPS5/KytyPS5

Length of output: 7787


Script executed:

set -e
f=src/graphics/host_gpu/renderer/cache/textureCache.cpp
echo "== UntrackImageHead / UntrackImageTail / UntrackImage =="
sed -n 376,420p "$f" | cat -n | sed 's/^/L+375 /'
echo "== InvalidateCpuAliases region & IsDefinitelyCpuDirty uses =="
sed -n 494,512p "$f" | cat -n | sed 's/^/L+493 /'
sed -n 722,736p "$f" | cat -n | sed 's/^/L+721 /'
sed -n 1220,1248p "$f" | cat -n | sed 's/^/L+1219 /'
echo "== image.h tracking and dirty accessors =="
sed -n 66,84p src/graphics/host_gpu/renderer/image/image.h | cat -n | sed 's/^/L+65 /'
sed -n 118,128p src/graphics/host_gpu/renderer/image/image.h | cat -n | sed 's/^/L+117 /'
echo "== GPU-modified state uses =="
rg -n -F -e 'IsGpuModified' -e 'ClearGpuModified' -e 'MarkGpuModified' -- src/graphics/host_gpu/renderer || test "$?" -eq 1

Repository: KytyPS5/KytyPS5

Length of output: 9471


Check the write start against the image's tracked range before marking it CPU-dirty.

SequentialWriteReachesImage returns address < end for any write that starts inside the image. It does not account for UntrackImageHead, 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 returns true. InvalidateCpuAliases then calls MarkCpuDirty() and UntrackImage(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_addr through track_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](https://github.com/KytyPS5/KytyPS5/pull/2063#discussion_r4463909610) <!-- 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** · **Data Integrity & Integration** · **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 sed -n 105,140p src/graphics/host_gpu/renderer/cache/textureCache.cpp sed -n 1935,2010p src/graphics/host_gpu/renderer/cache/textureCache.cpp rg -n 'UntrackImageHead|track_addr|IsDefinitelyCpuDirty|MarkCpuDirty' src/graphics/host_gpu/renderer | head -60 ``` Repository: KytyPS5/KytyPS5 Length of output: 7787 --- Script executed: ```bash set -e f=src/graphics/host_gpu/renderer/cache/textureCache.cpp echo "== UntrackImageHead / UntrackImageTail / UntrackImage ==" sed -n 376,420p "$f" | cat -n | sed 's/^/L+375 /' echo "== InvalidateCpuAliases region & IsDefinitelyCpuDirty uses ==" sed -n 494,512p "$f" | cat -n | sed 's/^/L+493 /' sed -n 722,736p "$f" | cat -n | sed 's/^/L+721 /' sed -n 1220,1248p "$f" | cat -n | sed 's/^/L+1219 /' echo "== image.h tracking and dirty accessors ==" sed -n 66,84p src/graphics/host_gpu/renderer/image/image.h | cat -n | sed 's/^/L+65 /' sed -n 118,128p src/graphics/host_gpu/renderer/image/image.h | cat -n | sed 's/^/L+117 /' echo "== GPU-modified state uses ==" rg -n -F -e 'IsGpuModified' -e 'ClearGpuModified' -e 'MarkGpuModified' -- src/graphics/host_gpu/renderer || test "$?" -eq 1 ``` Repository: KytyPS5/KytyPS5 Length of output: 9471 --- </dd> </dl> </details> **Check the write start against the image's tracked range before marking it CPU-dirty.** `SequentialWriteReachesImage` returns `address < end` for any write that starts inside the image. It does not account for `UntrackImageHead`, 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 returns `true`. `InvalidateCpuAliases` then calls `MarkCpuDirty()` and `UntrackImage(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_addr` through `track_addr_end`) instead of only the image bounds. The start-before-image branch and the tail handling must stay consistent with that check. <!-- fingerprinting:phantom:medusa:beignet55 --> <!-- cr-indicator-types:potential_issue --> <!-- cr-comment:v1:b0b7f948e4a582c3d89762c8 --> <!-- This is an auto-generated comment by CodeRabbit -->
Author
Owner

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

Actionable comments posted: 1

Nitpick comments: 1
tests/ShaderRecompilerComputeTests.cpp: 1

3371-3374: Trivial · Maintainability & Code Quality · Quick win

Assert that the head-page images remain GPU-modified.

If the fill drops GPU ownership but leaves IsDefinitelyCpuDirty() false and track_addr unchanged, this check passes. Line 3377 then clears GPU ownership without exposing the failure. Check IsGpuModified() on both images before cleanup so the test covers the GPU-content preservation it describes. (raw.githubusercontent.com)

Review info
Run configuration
Commits

Reviewing files that changed from the base of the PR and between 8fc3ff3c35 and 9fc3e6afa2.

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

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 7 remain after this review.

**@⁠coderabbitai[bot]** on GitHub · 2026-11-04 05:22 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/2063#pullrequestreview-5763619189) · **reviewed** <!-- coderabbit-ai-generated:v1 --> **Actionable comments posted: 1** <details> <summary><picture><source media="(prefers-color-scheme: dark)" srcset="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/brush-cleaning-dark.svg"><img src="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/brush-cleaning.svg" width="16" height="16" align="absmiddle" alt=""></picture> Nitpick comments: 1</summary><blockquote> <dl> <dd> <details> <summary>tests/ShaderRecompilerComputeTests.cpp: 1</summary><blockquote> <dl> <dd> `3371-3374`: **<picture><img src="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/neutral-16.svg" width="16" height="16" align="absmiddle" alt=""></picture> Trivial** · **Maintainability & Code Quality** · **Quick win** **Assert that the head-page images remain GPU-modified.** If the fill drops GPU ownership but leaves `IsDefinitelyCpuDirty()` false and `track_addr` unchanged, this check passes. Line 3377 then clears GPU ownership without exposing the failure. Check `IsGpuModified()` on both images before cleanup so the test covers the GPU-content preservation it describes. ([raw.githubusercontent.com](https://raw.githubusercontent.com/KytyPS5/KytyPS5/9fc3e6afa2e5ef2eaf101ff0233cbcb9cbb0e09d/tests/ShaderRecompilerComputeTests.cpp)) <!-- cr-comment:v1:baee619064436f0e2d708890 --> </dd> </dl> </blockquote></details> </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/893b03552699dc873474023bc1f4c69c57fb36b2/.coderabbit.yaml>) - **Commit**: [893b035](<https://github.com/KytyPS5/KytyPS5/commit/893b03552699dc873474023bc1f4c69c57fb36b2>) - **Layers**: Repository YAML - **Review profile**: CHILL - **Plan**: Advanced - **Run ID**: `97e77130-46c2-490c-9798-6b51a639c5e7` </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 8fc3ff3c351e2e1b65280790b31240b9fe529319 and 9fc3e6afa2e5ef2eaf101ff0233cbcb9cbb0e09d. </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: 6</summary> <dl> <dd> * `src/graphics/host_gpu/renderer/cache/bufferCache.cpp` * `src/graphics/host_gpu/renderer/cache/textureCache.cpp` * `src/graphics/host_gpu/renderer/cache/textureCache.h` * `src/graphics/host_gpu/renderer/renderContext.cpp` * `src/graphics/host_gpu/renderer/renderContext.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; 7 remain after this review. </dd> </dl> </details> </h6> <!-- This is an auto-generated comment by CodeRabbit for review status --> <!-- coderabbit-review-publication v1 publication=f2ae3da5-ee7a-4173-a3ce-d6cbd4cf501c attempt=774fd901-070a-4559-916e-1eb8509bcd85 batch=1/1 -->
This pull request has changes conflicting with the target branch.
  • src/graphics/host_gpu/renderer/cache/bufferCache.cpp
  • src/graphics/host_gpu/renderer/cache/textureCache.cpp
  • src/graphics/host_gpu/renderer/cache/textureCache.h
  • tests/ShaderRecompilerComputeTests.cpp
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.
git fetch -u origin '+refs/pull/2063/head:github-bot/gh-2063'
git switch 'github-bot/gh-2063'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-2063'
git switch 'github-bot/gh-2063'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-2063'
git switch 'github-bot/gh-2063'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-2063'
git switch 'main'
git merge --squash 'github-bot/gh-2063'
git switch 'main'
git merge --ff-only 'github-bot/gh-2063'
git switch 'main'
git merge 'github-bot/gh-2063'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.