gpu: publish GPU writes to read-hot pages at completion #2037

Open
github-bot wants to merge 11 commits from github-bot/gh-2037 into main AGit
Owner

@⁠Nawid3333 on GitHub · 2026-10-31 21:48 UTC · original
Nawid3333:fix/publish-read-hot → main · +477 −25 · 8 files · 11 commits · open

Summary

A guest CPU read of a page the GPU wrote faults into a blocking download. Demon's Souls reads the same few pages again and again while it boots, so these downloads repeat thousands of times. This change remembers the pages the guest reads and writes later GPU writes to them into guest memory as soon as the GPU has finished, so the next read needs no download.

Problem

A guest read (or write) of a GPU-written page faults, and BufferCache::ReadMemory runs DownloadBufferMemory<false>: record the copy, submit, wait for the GPU, write guest memory. In a 20000-fault sample of a Demon's Souls (PPSA01341) boot without this change, the faults hit 139 distinct pages; the most frequent page faulted 2768 times.

Change

  • A read download notes its pages as read-hot (NoteReadHot): at most 16 pages per read and 1024 pages in total; a page is forgotten after 8 flips without a read (DecayReadHot at Flip and PrepareCpuFlip).
  • A GPU write to a read-hot range (ObtainBuffer with is_written) queues the range.
  • At ReleaseMem, BufferFlush, Flip and PrepareCpuFlip, PublishReadHot records the copies into the download buffer (shared helper RecordDownloadCopies, now also used by DownloadBufferMemory, 1 MiB per publish) and defers a priority operation that writes the bytes to guest memory once the GPU has finished, plus a deferred ConfirmPublish that clears the GPU-modified state of those ranges.
  • A newer GPU write to a queued range (ObtainBuffer, a download of the range, draws and dispatches with address writes through SupersedeAllPublishes) marks the entry superseded: the range stays GPU-modified and the next read downloads the newest data.
  • A guest store to such a page still faults, and its synchronous download first drains every queued publish (WaitPriorityOperations), so a publish never lands after a newer CPU write.

Effect

One PC: Ryzen 5 7600X, Radeon RX 9070 XT, Windows 11, AMD driver 26.10.1. Demon's Souls boot to character creation with a fixed input script and a warm shader cache, alternating runs without and with the change:

  • this branch: without the change, one run did not reach character creation; the other needed 2.5x and 3.0x as long as the two runs with the change;
  • an equivalent lab prototype on the same day: 3.9x and 4.1x.

In the prototype's fault-log runs, guest-read downloads went from 4498 and 5558 to 3710 and 3618, and their median cost from 10.04 and 13.57 ms to 0.31 ms. A verify run compared 33811 published ranges with a blocking download of the same bytes: 0 mismatches.

Tests

New case buffer_cache_hot_publish (shader_recompiler_compute_tests --buffer-cache-hot-publish-only, registered with ctest):

  • a read marks a page hot and downloads it;
  • a GPU fill of the hot page is published at PublishReadHot and the page is clean;
  • a page that was never read is not published and stays dirty;
  • a superseded publish leaves the page dirty, and the next read sees the newest write;
  • a page read 8 flips ago is still published, after 9 flips it is not;
  • only the read page of a two-page buffer is published;
  • a CPU write after a queued publish survives.

Four mutations (publish disabled, supersede ignored, reads never hot, decay one flip early) each make it fail. buffer_cache_dirty_gc still passes. Run on the RX 9070 XT. There the Vulkan harness only starts with two local changes that are not part of this PR (#2036): the robustStorageBufferAccessSizeAlignment == 1 check relaxed (the driver reports 4), and VK_EXT_pipeline_robustness not requested by name (core in Vulkan 1.4, not listed by this driver). The test file also needs #2010 to build on Windows.

#1254 reads back already-submitted data without splitting the current command buffer; this change publishes read-hot pages before the guest reads them, so fewer reads need a download and the remaining downloads are cheaper (see Effect). Both change DownloadBufferMemory and the write path of ObtainBuffer, so whichever lands second needs a rebase; it is mechanical, and I can rebase this one onto #1254.

AI use: drafted with AI assistance (Claude Code); verified and tested by me.

**@⁠Nawid3333** on GitHub · 2026-10-31 21:48 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/2037) `Nawid3333:fix/publish-read-hot` → `main` · +477 −25 · 8 files · 11 commits · open ## Summary A guest CPU read of a page the GPU wrote faults into a blocking download. Demon's Souls reads the same few pages again and again while it boots, so these downloads repeat thousands of times. This change remembers the pages the guest reads and writes later GPU writes to them into guest memory as soon as the GPU has finished, so the next read needs no download. ## Problem A guest read (or write) of a GPU-written page faults, and `BufferCache::ReadMemory` runs `DownloadBufferMemory<false>`: record the copy, submit, wait for the GPU, write guest memory. In a 20000-fault sample of a Demon's Souls (PPSA01341) boot without this change, the faults hit 139 distinct pages; the most frequent page faulted 2768 times. ## Change - A read download notes its pages as read-hot (`NoteReadHot`): at most 16 pages per read and 1024 pages in total; a page is forgotten after 8 flips without a read (`DecayReadHot` at `Flip` and `PrepareCpuFlip`). - A GPU write to a read-hot range (`ObtainBuffer` with `is_written`) queues the range. - At `ReleaseMem`, `BufferFlush`, `Flip` and `PrepareCpuFlip`, `PublishReadHot` records the copies into the download buffer (shared helper `RecordDownloadCopies`, now also used by `DownloadBufferMemory`, 1 MiB per publish) and defers a priority operation that writes the bytes to guest memory once the GPU has finished, plus a deferred `ConfirmPublish` that clears the GPU-modified state of those ranges. - A newer GPU write to a queued range (`ObtainBuffer`, a download of the range, draws and dispatches with address writes through `SupersedeAllPublishes`) marks the entry superseded: the range stays GPU-modified and the next read downloads the newest data. - A guest store to such a page still faults, and its synchronous download first drains every queued publish (`WaitPriorityOperations`), so a publish never lands after a newer CPU write. ## Effect One PC: Ryzen 5 7600X, Radeon RX 9070 XT, Windows 11, AMD driver 26.10.1. Demon's Souls boot to character creation with a fixed input script and a warm shader cache, alternating runs without and with the change: - this branch: without the change, one run did not reach character creation; the other needed 2.5x and 3.0x as long as the two runs with the change; - an equivalent lab prototype on the same day: 3.9x and 4.1x. In the prototype's fault-log runs, guest-read downloads went from 4498 and 5558 to 3710 and 3618, and their median cost from 10.04 and 13.57 ms to 0.31 ms. A verify run compared 33811 published ranges with a blocking download of the same bytes: 0 mismatches. ## Tests New case `buffer_cache_hot_publish` (`shader_recompiler_compute_tests --buffer-cache-hot-publish-only`, registered with ctest): - a read marks a page hot and downloads it; - a GPU fill of the hot page is published at `PublishReadHot` and the page is clean; - a page that was never read is not published and stays dirty; - a superseded publish leaves the page dirty, and the next read sees the newest write; - a page read 8 flips ago is still published, after 9 flips it is not; - only the read page of a two-page buffer is published; - a CPU write after a queued publish survives. Four mutations (publish disabled, supersede ignored, reads never hot, decay one flip early) each make it fail. `buffer_cache_dirty_gc` still passes. Run on the RX 9070 XT. There the Vulkan harness only starts with two local changes that are not part of this PR (#2036): the `robustStorageBufferAccessSizeAlignment == 1` check relaxed (the driver reports 4), and `VK_EXT_pipeline_robustness` not requested by name (core in Vulkan 1.4, not listed by this driver). The test file also needs #2010 to build on Windows. ## Related #1254 reads back already-submitted data without splitting the current command buffer; this change publishes read-hot pages before the guest reads them, so fewer reads need a download and the remaining downloads are cheaper (see Effect). Both change `DownloadBufferMemory` and the write path of `ObtainBuffer`, so whichever lands second needs a rebase; it is mechanical, and I can rebase this one onto #1254. AI use: drafted with AI assistance (Claude Code); verified and tested by me.
Guest reads of GPU-written pages fault into a blocking download. Pages the guest reads are remembered; later GPU writes to them are copied at ReleaseMem, BufferFlush and flips and written to guest memory once the GPU finished, so the next read needs no download. Newer GPU writes supersede a queued publish.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Image writes through the texture cache now supersede queued publishes of the same memory. Ranges over the publish budget stay queued for the next batch, and nothing is dropped when the download buffer cannot be mapped.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
# Conflicts:
#	src/graphics/guest_gpu/graphicsRun.cpp
#	src/graphics/host_gpu/renderer/renderCompute.cpp
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Resolve DownloadBufferMemory against 96b251749: take the ring or temporary
destination from MapDownload and keep recording the copies through
RecordDownloadCopies.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
An image GPU write over a read-hot page with a queued publish keeps the page
GPU-dirty after the publish completes, and SupersedeAllPublishes keeps every
queued publish of two batches from cleaning its range.

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

@⁠coderabbitai[bot] on GitHub · 2026-10-31 21:58 UTC · original · edited 2026-11-10 12:31 UTC

Review in Change Stack →

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • Resume reviews
  • Trigger review

No actionable comments were generated in the recent review.

Walkthrough

The buffer cache now tracks recently read pages and publishes eligible GPU-written ranges to backing memory. Graphics operations trigger publication and decay, while compute dispatches, prepared draws, and texture writes supersede pending publishes under specified conditions. The compute test suite adds coverage for publication, expiration, superseding writes, page boundaries, and later CPU writes. CTest can run this test alone.

Suggested reviewers: nmzik

Change: Feature

Merge Risk: Minimal · previous review f3469

No actionable merge-blocking risk is established by the supplied evidence.

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 clearly and concisely describes the main change: publishing GPU writes to read-hot pages when GPU work completes.
Description check Passed The description directly explains the repeated-download problem, the read-hot publication design, affected code paths, performance results, tests, and known integration notes.
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: 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 14 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

Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 77beba28b6 and f346989f15.

Files selected for processing: 1

  • 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-10-31 21:58 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/2037#issuecomment-6549675832) · edited 2026-11-10 12:31 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/2037?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-in-coderabbit-ui-dark.svg?v=3"><source media="(prefers-color-scheme: light)" srcset="https://storage.googleapis.com/coderabbit_public_assets/review-stack-in-coderabbit-ui.svg?v=3"><img src="https://storage.googleapis.com/coderabbit_public_assets/review-stack-in-coderabbit-ui.svg?v=3" alt="Review in Change Stack →" height="32"></picture></a> <!-- review_stack_entry_end --> <!-- This is an auto-generated comment: review paused by coderabbit.ai --> > [!NOTE] > ## Reviews paused > > It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the `reviews.auto_review.auto_pause_after_reviewed_commits` setting. > > - Last reviewed commit: f346989f15a19cc497af1713a8ebb84172b6dad4 > - Latest commit, not reviewed: 5abd6290fe82ffd5698eb17f46951b40b54764f6 > > Use the following commands to manage reviews: > - `@coderabbitai resume` to resume automatic reviews. > - `@coderabbitai review` to trigger a single review. > > Use the checkboxes below for quick actions: > - [ ] <!-- {"checkboxId":"7f6cc2e2-2e4e-497a-8c31-c9e4573e93d1"} --> Resume reviews > - [ ] <!-- {"checkboxId":"e9bb8d72-00e8-4f67-9cb2-caf3b22574fe"} --> Trigger review <!-- end of auto-generated comment: review paused by coderabbit.ai --> <!-- recent_review_status_start --> No actionable comments were generated in the recent review. <!-- recent_review_status_end --> <!-- walkthrough_start --> <details> <summary><strong>Walkthrough</strong></summary> <dl> <dd> <br> The buffer cache now tracks recently read pages and publishes eligible GPU-written ranges to backing memory. Graphics operations trigger publication and decay, while compute dispatches, prepared draws, and texture writes supersede pending publishes under specified conditions. The compute test suite adds coverage for publication, expiration, superseding writes, page boundaries, and later CPU writes. CTest can run this test alone. **Suggested reviewers:** `nmzik` </dd> </dl> </details> <!-- walkthrough_end --> <!-- change_assessment_start --> <!-- change_assessment_commit:"f346989f15a19cc497af1713a8ebb84172b6dad4" --> <!-- change_assessment_priority:{"version":1,"urgency":"p2","manual":false} --> **Change:** Feature <!-- 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-unassessed-16.svg" width="16" height="16" align="absmiddle" alt=""></picture> Minimal</strong> · previous review <code>f3469</code></summary> <dl> <dd> <br> <!-- final_review_risk_start --> <!-- final_review_risk_label **Merge Risk:** **⚪ Minimal** · up to `f3469` --> <!-- final_review_risk_coverage:{"sourceCommitId":"f346989f15a19cc497af1713a8ebb84172b6dad4","coveredCommitId":"f346989f15a19cc497af1713a8ebb84172b6dad4","kind":"reviewed"} --> No actionable merge-blocking risk is established by the supplied evidence. <!-- 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> <br> <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> <br> | Check name | Status | Explanation | | :------------------------ | :--------------------------------------------------------------------------------------------: | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | | Title check | ![Passed](https://storage.googleapis.com/coderabbit_public_assets/pre-merge-checks-passed.svg) | The title clearly and concisely describes the main change: publishing GPU writes to read-hot pages when GPU work completes. | | Description check | ![Passed](https://storage.googleapis.com/coderabbit_public_assets/pre-merge-checks-passed.svg) | The description directly explains the repeated-download problem, the read-hot publication design, affected code paths, performance results, tests, and known integration notes. | | 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. | </dd> </dl> </details> <details> <summary><strong>Full details: Docstring Coverage</strong></summary> <dl> <dd> <br> **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 14 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=2037)! 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> <br> - [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 --> <!-- recent_review_start --> <h6> <details> <summary><strong><picture><img src="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/info.svg" width="12" height="12" align="absmiddle" alt=""></picture> Recent review info</strong></summary> <dl> <dd> <br> <!-- review_info_content --> <details> <summary><strong><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</strong></summary> <dl> <dd> <br> - **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**: `a15dcea8-181a-4831-aeb6-b85ac632d455` </dd> </dl> </details> <details> <summary><strong><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</strong></summary> <dl> <dd> <br> Reviewing files that changed from the base of the PR and between 77beba28b6b20d6439adbdf619bbb2d6bb445161 and f346989f15a19cc497af1713a8ebb84172b6dad4. </dd> </dl> </details> <details> <summary><strong><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: 1</strong></summary> <dl> <dd> <br> * `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> <!-- recent_review_end -->
Author
Owner

@⁠coderabbitai[bot] on GitHub · 2026-10-31 21:58 UTC · original · edited 2026-10-31 22:29 UTC
On tests/ShaderRecompilerComputeTests.cpp line 4478 (outdated):

@@ -4422,6 +4422,162 @@ class VulkanHarness {
     std::printf("[host]    %-32s ok\n", name);
   }
 
+  void CheckBufferCacheReadHotPublish() {
+    constexpr const char *name = "BufferCacheReadHotPublish";
+    constexpr uintptr_t base = 0x000000020a000000ull;
+    constexpr uint64_t allocation_size = 0x800000;
+    constexpr uint64_t allocation_alignment = 0x10000;
+    constexpr uint64_t hot_offset = 0x10000;
+    constexpr uint64_t cold_offset = 0x80000;
+    constexpr uint32_t stale = 0x0badf00du;
+
+    EnsureRuntimeContext();
+    RenderContext context(m_runtime_context);
+    auto &scheduler = context.GetCommandScheduler();
+    HW::Context registers{};
+    HW::UserConfig user_config{};
+    HW::Shader shaders{};
+    scheduler.Begin(registers, user_config, shaders);
+    context.InitializeGpu(nullptr);
+
+    int64_t direct_offset = -1;
+    Require(name, "direct allocation",
+            Libs::LibKernel::Memory::KernelAllocateDirectMemory(
+                0, Libs::LibKernel::Memory::KernelGetDirectMemorySize(),
+                allocation_size, allocation_alignment, 0, &direct_offset) == 0,
+            "read-hot direct-memory allocation failed");
+    void *mapped = reinterpret_cast<void *>(base);
+    Require(name, "direct mapping",
+            Libs::LibKernel::Memory::KernelMapDirectMemory(
+                &mapped, allocation_size, 0x3, 0x10, direct_offset,
+                allocation_alignment) == 0 &&
+                mapped == reinterpret_cast<void *>(base),
+            "read-hot fixed direct-memory mapping failed");
+    auto *memory = static_cast<uint8_t *>(mapped);
+    std::memcpy(memory + hot_offset, &stale, sizeof(stale));
+    std::memcpy(memory + cold_offset, &stale, sizeof(stale));
+
+    {
+      auto &cache = context.GetBufferCache();
+      context.MapMemory(base, allocation_size);
+      const auto GpuWrite = [&](uint64_t offset, uint32_t value) {
+        (void)cache.ObtainBuffer(base + offset, sizeof(uint32_t), true, false);
+        cache.FillBuffer(base + offset, sizeof(uint32_t), value, false);
+      };
+      const auto Backing = [&](uint64_t offset) {
+        uint32_t value = 0;
+        Libs::LibKernel::Memory::TryReadBacking(base + offset, &value,
+                                                sizeof(value));
+        return value;
+      };
+      const auto Clean = [&](uint64_t offset) {
+        return !cache.HasGpuDirtyBytes(base + offset, sizeof(uint32_t)) &&
+               !cache.IsRegionGpuModified(base + offset, sizeof(uint32_t));
+      };
+      const auto Dirty = [&](uint64_t offset) {
+        return cache.HasGpuDirtyBytes(base + offset, sizeof(uint32_t));

Minor · Functional Correctness · Quick win

Check GPU tracking in Dirty.

Dirty checks only the dirty-byte range. If publication clears GPU tracking for an unread page but leaves that range dirty, the cold-page assertion passes even though a guest read can observe stale backing without a read fault. Require cache.IsRegionGpuModified(...) here too. GPU tracking controls the page’s read protection. (raw.githubusercontent.com)

✅ Addressed in commit 4fd72a8

**@⁠coderabbitai[bot]** on GitHub · 2026-10-31 21:58 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/2037#discussion_r4433446922) · edited 2026-10-31 22:29 UTC On `tests/ShaderRecompilerComputeTests.cpp` line 4478 (outdated): ```diff @@ -4422,6 +4422,162 @@ class VulkanHarness { std::printf("[host] %-32s ok\n", name); } + void CheckBufferCacheReadHotPublish() { + constexpr const char *name = "BufferCacheReadHotPublish"; + constexpr uintptr_t base = 0x000000020a000000ull; + constexpr uint64_t allocation_size = 0x800000; + constexpr uint64_t allocation_alignment = 0x10000; + constexpr uint64_t hot_offset = 0x10000; + constexpr uint64_t cold_offset = 0x80000; + constexpr uint32_t stale = 0x0badf00du; + + EnsureRuntimeContext(); + RenderContext context(m_runtime_context); + auto &scheduler = context.GetCommandScheduler(); + HW::Context registers{}; + HW::UserConfig user_config{}; + HW::Shader shaders{}; + scheduler.Begin(registers, user_config, shaders); + context.InitializeGpu(nullptr); + + int64_t direct_offset = -1; + Require(name, "direct allocation", + Libs::LibKernel::Memory::KernelAllocateDirectMemory( + 0, Libs::LibKernel::Memory::KernelGetDirectMemorySize(), + allocation_size, allocation_alignment, 0, &direct_offset) == 0, + "read-hot direct-memory allocation failed"); + void *mapped = reinterpret_cast<void *>(base); + Require(name, "direct mapping", + Libs::LibKernel::Memory::KernelMapDirectMemory( + &mapped, allocation_size, 0x3, 0x10, direct_offset, + allocation_alignment) == 0 && + mapped == reinterpret_cast<void *>(base), + "read-hot fixed direct-memory mapping failed"); + auto *memory = static_cast<uint8_t *>(mapped); + std::memcpy(memory + hot_offset, &stale, sizeof(stale)); + std::memcpy(memory + cold_offset, &stale, sizeof(stale)); + + { + auto &cache = context.GetBufferCache(); + context.MapMemory(base, allocation_size); + const auto GpuWrite = [&](uint64_t offset, uint32_t value) { + (void)cache.ObtainBuffer(base + offset, sizeof(uint32_t), true, false); + cache.FillBuffer(base + offset, sizeof(uint32_t), value, false); + }; + const auto Backing = [&](uint64_t offset) { + uint32_t value = 0; + Libs::LibKernel::Memory::TryReadBacking(base + offset, &value, + sizeof(value)); + return value; + }; + const auto Clean = [&](uint64_t offset) { + return !cache.HasGpuDirtyBytes(base + offset, sizeof(uint32_t)) && + !cache.IsRegionGpuModified(base + offset, sizeof(uint32_t)); + }; + const auto Dirty = [&](uint64_t offset) { + return cache.HasGpuDirtyBytes(base + offset, sizeof(uint32_t)); ``` <!-- coderabbit-ai-generated:v1 --> **<picture><img src="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/amber-16.svg" width="16" height="16" align="absmiddle" alt=""></picture> Minor** · **Functional Correctness** · **Quick win** **Check GPU tracking in `Dirty`.** `Dirty` checks only the dirty-byte range. If publication clears GPU tracking for an unread page but leaves that range dirty, the cold-page assertion passes even though a guest read can observe stale backing without a read fault. Require `cache.IsRegionGpuModified(...)` here too. GPU tracking controls the page’s read protection. ([raw.githubusercontent.com](https://raw.githubusercontent.com/KytyPS5/KytyPS5/29cd962462b06eb6ba1585014aef1d8ad2c6b683/src/graphics/host_gpu/regionManager.h)) <!-- fingerprinting:phantom:poseidon:wombat --> <!-- cr-indicator-types:potential_issue --> <!-- cr-comment:v1:3d7390b8b8328b8c67d5225c --> <!-- This is an auto-generated reply by CodeRabbit --> ✅ Addressed in commit 4fd72a8
Author
Owner

@⁠coderabbitai[bot] on GitHub · 2026-10-31 21:58 UTC · original · edited 2026-10-31 22:29 UTC
On tests/ShaderRecompilerComputeTests.cpp line 4548 (outdated):

@@ -4422,6 +4422,162 @@ class VulkanHarness {
     std::printf("[host]    %-32s ok\n", name);
   }
 
+  void CheckBufferCacheReadHotPublish() {
+    constexpr const char *name = "BufferCacheReadHotPublish";
+    constexpr uintptr_t base = 0x000000020a000000ull;
+    constexpr uint64_t allocation_size = 0x800000;
+    constexpr uint64_t allocation_alignment = 0x10000;
+    constexpr uint64_t hot_offset = 0x10000;
+    constexpr uint64_t cold_offset = 0x80000;
+    constexpr uint32_t stale = 0x0badf00du;
+
+    EnsureRuntimeContext();
+    RenderContext context(m_runtime_context);
+    auto &scheduler = context.GetCommandScheduler();
+    HW::Context registers{};
+    HW::UserConfig user_config{};
+    HW::Shader shaders{};
+    scheduler.Begin(registers, user_config, shaders);
+    context.InitializeGpu(nullptr);
+
+    int64_t direct_offset = -1;
+    Require(name, "direct allocation",
+            Libs::LibKernel::Memory::KernelAllocateDirectMemory(
+                0, Libs::LibKernel::Memory::KernelGetDirectMemorySize(),
+                allocation_size, allocation_alignment, 0, &direct_offset) == 0,
+            "read-hot direct-memory allocation failed");
+    void *mapped = reinterpret_cast<void *>(base);
+    Require(name, "direct mapping",
+            Libs::LibKernel::Memory::KernelMapDirectMemory(
+                &mapped, allocation_size, 0x3, 0x10, direct_offset,
+                allocation_alignment) == 0 &&
+                mapped == reinterpret_cast<void *>(base),
+            "read-hot fixed direct-memory mapping failed");
+    auto *memory = static_cast<uint8_t *>(mapped);
+    std::memcpy(memory + hot_offset, &stale, sizeof(stale));
+    std::memcpy(memory + cold_offset, &stale, sizeof(stale));
+
+    {
+      auto &cache = context.GetBufferCache();
+      context.MapMemory(base, allocation_size);
+      const auto GpuWrite = [&](uint64_t offset, uint32_t value) {
+        (void)cache.ObtainBuffer(base + offset, sizeof(uint32_t), true, false);
+        cache.FillBuffer(base + offset, sizeof(uint32_t), value, false);
+      };
+      const auto Backing = [&](uint64_t offset) {
+        uint32_t value = 0;
+        Libs::LibKernel::Memory::TryReadBacking(base + offset, &value,
+                                                sizeof(value));
+        return value;
+      };
+      const auto Clean = [&](uint64_t offset) {
+        return !cache.HasGpuDirtyBytes(base + offset, sizeof(uint32_t)) &&
+               !cache.IsRegionGpuModified(base + offset, sizeof(uint32_t));
+      };
+      const auto Dirty = [&](uint64_t offset) {
+        return cache.HasGpuDirtyBytes(base + offset, sizeof(uint32_t));
+      };
+
+      GpuWrite(hot_offset, 0x11111111u);
+      scheduler.Flush();
+      cache.ReadMemory(base + hot_offset, sizeof(uint32_t));
+      Require(name, "first download",
+              Backing(hot_offset) == 0x11111111u && Clean(hot_offset),
+              "the guest read did not publish the submitted GPU fill");
+
+      GpuWrite(hot_offset, 0x22222222u);
+      GpuWrite(cold_offset, 0x33333333u);
+      cache.PublishReadHot();
+      scheduler.Finish();
+      Require(name, "hot page publish",
+              Backing(hot_offset) == 0x22222222u && Clean(hot_offset),
+              "PublishReadHot did not write the hot page to backing memory");
+      Require(name, "cold page untouched",
+              Backing(cold_offset) == stale && Dirty(cold_offset),
+              "a page that was never read was published or cleaned");
+
+      GpuWrite(hot_offset, 0x44444444u);
+      cache.PublishReadHot();
+      GpuWrite(hot_offset, 0x55555555u);
+      scheduler.Finish();
+      Require(name, "superseded publish", Dirty(hot_offset),
+              "a publish superseded by a newer GPU write cleaned the page");
+      cache.ReadMemory(base + hot_offset, sizeof(uint32_t));
+      Require(name, "newest value after supersede",
+              Backing(hot_offset) == 0x55555555u && Clean(hot_offset),
+              "the guest read after a superseded publish missed the newest GPU write");
+
+      cache.ReadMemory(base + cold_offset, sizeof(uint32_t));
+
+      constexpr uint64_t decay_offset = 0x310000;
+      std::memcpy(memory + decay_offset, &stale, sizeof(stale));
+      GpuWrite(decay_offset, 0x10203040u);
+      scheduler.Flush();
+      cache.ReadMemory(base + decay_offset, sizeof(uint32_t));
+      for (int flip = 0; flip != 8; ++flip) {
+        cache.DecayReadHot();
+      }
+      GpuWrite(decay_offset, 0x50607080u);
+      cache.PublishReadHot();
+      scheduler.Finish();
+      Require(name, "hot for eight flips",
+              Backing(decay_offset) == 0x50607080u && Clean(decay_offset),
+              "a page read eight flips ago was not published");
+      cache.DecayReadHot();
+      GpuWrite(decay_offset, 0x91a2b3c4u);
+      cache.PublishReadHot();
+      scheduler.Finish();
+      Require(name, "cold after nine flips",
+              Backing(decay_offset) == 0x50607080u && Dirty(decay_offset),
+              "a page read nine flips ago was still published");
+      cache.ReadMemory(base + decay_offset, sizeof(uint32_t));
+
+      constexpr uint64_t owner_offset = 0x540000;
+      constexpr uint64_t neighbor_offset = owner_offset + 0x1000;
+      std::memcpy(memory + neighbor_offset, &stale, sizeof(stale));
+      (void)cache.ObtainBuffer(base + owner_offset, 0x2000, true, false);
+      cache.FillBuffer(base + owner_offset, 0x2000, 0x2468ace0u, false);
+      scheduler.Flush();
+      cache.ReadMemory(base + owner_offset, sizeof(uint32_t));
+      GpuWrite(owner_offset, 0x2468ace1u);
+      GpuWrite(neighbor_offset, 0x2468ace1u);
+      cache.PublishReadHot();
+      scheduler.Finish();
+      Require(name, "only the hot page of a buffer",
+              Backing(owner_offset) == 0x2468ace1u && Clean(owner_offset) &&
+                  Backing(neighbor_offset) != 0x2468ace1u &&

Minor · Functional Correctness · Quick win

Require the neighbor’s original backing value.

The earlier 0x2000 fill writes 0x2468ace0 across both pages. If the first read incorrectly publishes the neighbor, the later neighbor write restores its dirty state, and Backing(neighbor_offset) != 0x2468ace1u still passes. Compare the neighbor backing with stale to detect that cross-page publication. (raw.githubusercontent.com)

✅ Addressed in commit 4fd72a8

**@⁠coderabbitai[bot]** on GitHub · 2026-10-31 21:58 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/2037#discussion_r4433446923) · edited 2026-10-31 22:29 UTC On `tests/ShaderRecompilerComputeTests.cpp` line 4548 (outdated): ```diff @@ -4422,6 +4422,162 @@ class VulkanHarness { std::printf("[host] %-32s ok\n", name); } + void CheckBufferCacheReadHotPublish() { + constexpr const char *name = "BufferCacheReadHotPublish"; + constexpr uintptr_t base = 0x000000020a000000ull; + constexpr uint64_t allocation_size = 0x800000; + constexpr uint64_t allocation_alignment = 0x10000; + constexpr uint64_t hot_offset = 0x10000; + constexpr uint64_t cold_offset = 0x80000; + constexpr uint32_t stale = 0x0badf00du; + + EnsureRuntimeContext(); + RenderContext context(m_runtime_context); + auto &scheduler = context.GetCommandScheduler(); + HW::Context registers{}; + HW::UserConfig user_config{}; + HW::Shader shaders{}; + scheduler.Begin(registers, user_config, shaders); + context.InitializeGpu(nullptr); + + int64_t direct_offset = -1; + Require(name, "direct allocation", + Libs::LibKernel::Memory::KernelAllocateDirectMemory( + 0, Libs::LibKernel::Memory::KernelGetDirectMemorySize(), + allocation_size, allocation_alignment, 0, &direct_offset) == 0, + "read-hot direct-memory allocation failed"); + void *mapped = reinterpret_cast<void *>(base); + Require(name, "direct mapping", + Libs::LibKernel::Memory::KernelMapDirectMemory( + &mapped, allocation_size, 0x3, 0x10, direct_offset, + allocation_alignment) == 0 && + mapped == reinterpret_cast<void *>(base), + "read-hot fixed direct-memory mapping failed"); + auto *memory = static_cast<uint8_t *>(mapped); + std::memcpy(memory + hot_offset, &stale, sizeof(stale)); + std::memcpy(memory + cold_offset, &stale, sizeof(stale)); + + { + auto &cache = context.GetBufferCache(); + context.MapMemory(base, allocation_size); + const auto GpuWrite = [&](uint64_t offset, uint32_t value) { + (void)cache.ObtainBuffer(base + offset, sizeof(uint32_t), true, false); + cache.FillBuffer(base + offset, sizeof(uint32_t), value, false); + }; + const auto Backing = [&](uint64_t offset) { + uint32_t value = 0; + Libs::LibKernel::Memory::TryReadBacking(base + offset, &value, + sizeof(value)); + return value; + }; + const auto Clean = [&](uint64_t offset) { + return !cache.HasGpuDirtyBytes(base + offset, sizeof(uint32_t)) && + !cache.IsRegionGpuModified(base + offset, sizeof(uint32_t)); + }; + const auto Dirty = [&](uint64_t offset) { + return cache.HasGpuDirtyBytes(base + offset, sizeof(uint32_t)); + }; + + GpuWrite(hot_offset, 0x11111111u); + scheduler.Flush(); + cache.ReadMemory(base + hot_offset, sizeof(uint32_t)); + Require(name, "first download", + Backing(hot_offset) == 0x11111111u && Clean(hot_offset), + "the guest read did not publish the submitted GPU fill"); + + GpuWrite(hot_offset, 0x22222222u); + GpuWrite(cold_offset, 0x33333333u); + cache.PublishReadHot(); + scheduler.Finish(); + Require(name, "hot page publish", + Backing(hot_offset) == 0x22222222u && Clean(hot_offset), + "PublishReadHot did not write the hot page to backing memory"); + Require(name, "cold page untouched", + Backing(cold_offset) == stale && Dirty(cold_offset), + "a page that was never read was published or cleaned"); + + GpuWrite(hot_offset, 0x44444444u); + cache.PublishReadHot(); + GpuWrite(hot_offset, 0x55555555u); + scheduler.Finish(); + Require(name, "superseded publish", Dirty(hot_offset), + "a publish superseded by a newer GPU write cleaned the page"); + cache.ReadMemory(base + hot_offset, sizeof(uint32_t)); + Require(name, "newest value after supersede", + Backing(hot_offset) == 0x55555555u && Clean(hot_offset), + "the guest read after a superseded publish missed the newest GPU write"); + + cache.ReadMemory(base + cold_offset, sizeof(uint32_t)); + + constexpr uint64_t decay_offset = 0x310000; + std::memcpy(memory + decay_offset, &stale, sizeof(stale)); + GpuWrite(decay_offset, 0x10203040u); + scheduler.Flush(); + cache.ReadMemory(base + decay_offset, sizeof(uint32_t)); + for (int flip = 0; flip != 8; ++flip) { + cache.DecayReadHot(); + } + GpuWrite(decay_offset, 0x50607080u); + cache.PublishReadHot(); + scheduler.Finish(); + Require(name, "hot for eight flips", + Backing(decay_offset) == 0x50607080u && Clean(decay_offset), + "a page read eight flips ago was not published"); + cache.DecayReadHot(); + GpuWrite(decay_offset, 0x91a2b3c4u); + cache.PublishReadHot(); + scheduler.Finish(); + Require(name, "cold after nine flips", + Backing(decay_offset) == 0x50607080u && Dirty(decay_offset), + "a page read nine flips ago was still published"); + cache.ReadMemory(base + decay_offset, sizeof(uint32_t)); + + constexpr uint64_t owner_offset = 0x540000; + constexpr uint64_t neighbor_offset = owner_offset + 0x1000; + std::memcpy(memory + neighbor_offset, &stale, sizeof(stale)); + (void)cache.ObtainBuffer(base + owner_offset, 0x2000, true, false); + cache.FillBuffer(base + owner_offset, 0x2000, 0x2468ace0u, false); + scheduler.Flush(); + cache.ReadMemory(base + owner_offset, sizeof(uint32_t)); + GpuWrite(owner_offset, 0x2468ace1u); + GpuWrite(neighbor_offset, 0x2468ace1u); + cache.PublishReadHot(); + scheduler.Finish(); + Require(name, "only the hot page of a buffer", + Backing(owner_offset) == 0x2468ace1u && Clean(owner_offset) && + Backing(neighbor_offset) != 0x2468ace1u && ``` <!-- coderabbit-ai-generated:v1 --> **<picture><img src="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/amber-16.svg" width="16" height="16" align="absmiddle" alt=""></picture> Minor** · **Functional Correctness** · **Quick win** **Require the neighbor’s original backing value.** The earlier `0x2000` fill writes `0x2468ace0` across both pages. If the first read incorrectly publishes the neighbor, the later neighbor write restores its dirty state, and `Backing(neighbor_offset) != 0x2468ace1u` still passes. Compare the neighbor backing with `stale` to detect that cross-page publication. ([raw.githubusercontent.com](https://raw.githubusercontent.com/KytyPS5/KytyPS5/29cd962462b06eb6ba1585014aef1d8ad2c6b683/tests/ShaderRecompilerComputeTests.cpp)) <!-- fingerprinting:phantom:poseidon:wombat --> <!-- cr-indicator-types:potential_issue --> <!-- cr-comment:v1:9c40d278514d063edeb1ccb3 --> <!-- This is an auto-generated reply by CodeRabbit --> ✅ Addressed in commit 4fd72a8
Author
Owner

@⁠coderabbitai[bot] on GitHub · 2026-10-31 21:58 UTC · original · reviewed

Actionable comments posted: 2


Review info
Run configuration
Commits

Reviewing files that changed from the base of the PR and between bbf96a5e9f and 29cd962462.

Files selected for processing: 7
  • CMakeLists.txt
  • src/graphics/guest_gpu/graphicsRun.cpp
  • src/graphics/host_gpu/renderer/cache/bufferCache.cpp
  • src/graphics/host_gpu/renderer/cache/bufferCache.h
  • src/graphics/host_gpu/renderer/renderCompute.cpp
  • src/graphics/host_gpu/renderer/renderDraw.cpp
  • 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-10-31 21:58 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/2037#pullrequestreview-5725120794) · **reviewed** <!-- coderabbit-ai-generated:v1 --> **Actionable comments posted: 2** --- <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/bbf96a5e9f69cad84b99791d6418c81d01913e75/.coderabbit.yaml>) - **Commit**: [bbf96a5](<https://github.com/KytyPS5/KytyPS5/commit/bbf96a5e9f69cad84b99791d6418c81d01913e75>) - **Layers**: Repository YAML - **Review profile**: CHILL - **Plan**: Advanced - **Run ID**: `2880220e-30c6-4db4-b2a2-4c30ac80de2f` </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 bbf96a5e9f69cad84b99791d6418c81d01913e75 and 29cd962462b06eb6ba1585014aef1d8ad2c6b683. </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: 7</summary> <dl> <dd> * `CMakeLists.txt` * `src/graphics/guest_gpu/graphicsRun.cpp` * `src/graphics/host_gpu/renderer/cache/bufferCache.cpp` * `src/graphics/host_gpu/renderer/cache/bufferCache.h` * `src/graphics/host_gpu/renderer/renderCompute.cpp` * `src/graphics/host_gpu/renderer/renderDraw.cpp` * `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=d7c7f10d-96ed-46ac-97a1-9c723159e245 attempt=d5186016-f379-4eb3-8fde-760d1cf09141 batch=1/1 -->
Author
Owner

@⁠Nawid3333 on GitHub · 2026-10-31 22:09 UTC · original
On tests/ShaderRecompilerComputeTests.cpp line 4478 (outdated):

@@ -4422,6 +4422,162 @@ class VulkanHarness {
     std::printf("[host]    %-32s ok\n", name);
   }
 
+  void CheckBufferCacheReadHotPublish() {
+    constexpr const char *name = "BufferCacheReadHotPublish";
+    constexpr uintptr_t base = 0x000000020a000000ull;
+    constexpr uint64_t allocation_size = 0x800000;
+    constexpr uint64_t allocation_alignment = 0x10000;
+    constexpr uint64_t hot_offset = 0x10000;
+    constexpr uint64_t cold_offset = 0x80000;
+    constexpr uint32_t stale = 0x0badf00du;
+
+    EnsureRuntimeContext();
+    RenderContext context(m_runtime_context);
+    auto &scheduler = context.GetCommandScheduler();
+    HW::Context registers{};
+    HW::UserConfig user_config{};
+    HW::Shader shaders{};
+    scheduler.Begin(registers, user_config, shaders);
+    context.InitializeGpu(nullptr);
+
+    int64_t direct_offset = -1;
+    Require(name, "direct allocation",
+            Libs::LibKernel::Memory::KernelAllocateDirectMemory(
+                0, Libs::LibKernel::Memory::KernelGetDirectMemorySize(),
+                allocation_size, allocation_alignment, 0, &direct_offset) == 0,
+            "read-hot direct-memory allocation failed");
+    void *mapped = reinterpret_cast<void *>(base);
+    Require(name, "direct mapping",
+            Libs::LibKernel::Memory::KernelMapDirectMemory(
+                &mapped, allocation_size, 0x3, 0x10, direct_offset,
+                allocation_alignment) == 0 &&
+                mapped == reinterpret_cast<void *>(base),
+            "read-hot fixed direct-memory mapping failed");
+    auto *memory = static_cast<uint8_t *>(mapped);
+    std::memcpy(memory + hot_offset, &stale, sizeof(stale));
+    std::memcpy(memory + cold_offset, &stale, sizeof(stale));
+
+    {
+      auto &cache = context.GetBufferCache();
+      context.MapMemory(base, allocation_size);
+      const auto GpuWrite = [&](uint64_t offset, uint32_t value) {
+        (void)cache.ObtainBuffer(base + offset, sizeof(uint32_t), true, false);
+        cache.FillBuffer(base + offset, sizeof(uint32_t), value, false);
+      };
+      const auto Backing = [&](uint64_t offset) {
+        uint32_t value = 0;
+        Libs::LibKernel::Memory::TryReadBacking(base + offset, &value,
+                                                sizeof(value));
+        return value;
+      };
+      const auto Clean = [&](uint64_t offset) {
+        return !cache.HasGpuDirtyBytes(base + offset, sizeof(uint32_t)) &&
+               !cache.IsRegionGpuModified(base + offset, sizeof(uint32_t));
+      };
+      const auto Dirty = [&](uint64_t offset) {
+        return cache.HasGpuDirtyBytes(base + offset, sizeof(uint32_t));

Done in 4fd72a87: Dirty now also requires IsRegionGpuModified, so a page whose read protection was dropped fails the check even if its bytes stay dirty. The test passes on the RX 9070 XT.

**@⁠Nawid3333** on GitHub · 2026-10-31 22:09 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/2037#discussion_r4433474517) On `tests/ShaderRecompilerComputeTests.cpp` line 4478 (outdated): ```diff @@ -4422,6 +4422,162 @@ class VulkanHarness { std::printf("[host] %-32s ok\n", name); } + void CheckBufferCacheReadHotPublish() { + constexpr const char *name = "BufferCacheReadHotPublish"; + constexpr uintptr_t base = 0x000000020a000000ull; + constexpr uint64_t allocation_size = 0x800000; + constexpr uint64_t allocation_alignment = 0x10000; + constexpr uint64_t hot_offset = 0x10000; + constexpr uint64_t cold_offset = 0x80000; + constexpr uint32_t stale = 0x0badf00du; + + EnsureRuntimeContext(); + RenderContext context(m_runtime_context); + auto &scheduler = context.GetCommandScheduler(); + HW::Context registers{}; + HW::UserConfig user_config{}; + HW::Shader shaders{}; + scheduler.Begin(registers, user_config, shaders); + context.InitializeGpu(nullptr); + + int64_t direct_offset = -1; + Require(name, "direct allocation", + Libs::LibKernel::Memory::KernelAllocateDirectMemory( + 0, Libs::LibKernel::Memory::KernelGetDirectMemorySize(), + allocation_size, allocation_alignment, 0, &direct_offset) == 0, + "read-hot direct-memory allocation failed"); + void *mapped = reinterpret_cast<void *>(base); + Require(name, "direct mapping", + Libs::LibKernel::Memory::KernelMapDirectMemory( + &mapped, allocation_size, 0x3, 0x10, direct_offset, + allocation_alignment) == 0 && + mapped == reinterpret_cast<void *>(base), + "read-hot fixed direct-memory mapping failed"); + auto *memory = static_cast<uint8_t *>(mapped); + std::memcpy(memory + hot_offset, &stale, sizeof(stale)); + std::memcpy(memory + cold_offset, &stale, sizeof(stale)); + + { + auto &cache = context.GetBufferCache(); + context.MapMemory(base, allocation_size); + const auto GpuWrite = [&](uint64_t offset, uint32_t value) { + (void)cache.ObtainBuffer(base + offset, sizeof(uint32_t), true, false); + cache.FillBuffer(base + offset, sizeof(uint32_t), value, false); + }; + const auto Backing = [&](uint64_t offset) { + uint32_t value = 0; + Libs::LibKernel::Memory::TryReadBacking(base + offset, &value, + sizeof(value)); + return value; + }; + const auto Clean = [&](uint64_t offset) { + return !cache.HasGpuDirtyBytes(base + offset, sizeof(uint32_t)) && + !cache.IsRegionGpuModified(base + offset, sizeof(uint32_t)); + }; + const auto Dirty = [&](uint64_t offset) { + return cache.HasGpuDirtyBytes(base + offset, sizeof(uint32_t)); ``` Done in 4fd72a87: `Dirty` now also requires `IsRegionGpuModified`, so a page whose read protection was dropped fails the check even if its bytes stay dirty. The test passes on the RX 9070 XT.
Author
Owner

@⁠Nawid3333 on GitHub · 2026-10-31 22:09 UTC · original
On tests/ShaderRecompilerComputeTests.cpp line 4548 (outdated):

@@ -4422,6 +4422,162 @@ class VulkanHarness {
     std::printf("[host]    %-32s ok\n", name);
   }
 
+  void CheckBufferCacheReadHotPublish() {
+    constexpr const char *name = "BufferCacheReadHotPublish";
+    constexpr uintptr_t base = 0x000000020a000000ull;
+    constexpr uint64_t allocation_size = 0x800000;
+    constexpr uint64_t allocation_alignment = 0x10000;
+    constexpr uint64_t hot_offset = 0x10000;
+    constexpr uint64_t cold_offset = 0x80000;
+    constexpr uint32_t stale = 0x0badf00du;
+
+    EnsureRuntimeContext();
+    RenderContext context(m_runtime_context);
+    auto &scheduler = context.GetCommandScheduler();
+    HW::Context registers{};
+    HW::UserConfig user_config{};
+    HW::Shader shaders{};
+    scheduler.Begin(registers, user_config, shaders);
+    context.InitializeGpu(nullptr);
+
+    int64_t direct_offset = -1;
+    Require(name, "direct allocation",
+            Libs::LibKernel::Memory::KernelAllocateDirectMemory(
+                0, Libs::LibKernel::Memory::KernelGetDirectMemorySize(),
+                allocation_size, allocation_alignment, 0, &direct_offset) == 0,
+            "read-hot direct-memory allocation failed");
+    void *mapped = reinterpret_cast<void *>(base);
+    Require(name, "direct mapping",
+            Libs::LibKernel::Memory::KernelMapDirectMemory(
+                &mapped, allocation_size, 0x3, 0x10, direct_offset,
+                allocation_alignment) == 0 &&
+                mapped == reinterpret_cast<void *>(base),
+            "read-hot fixed direct-memory mapping failed");
+    auto *memory = static_cast<uint8_t *>(mapped);
+    std::memcpy(memory + hot_offset, &stale, sizeof(stale));
+    std::memcpy(memory + cold_offset, &stale, sizeof(stale));
+
+    {
+      auto &cache = context.GetBufferCache();
+      context.MapMemory(base, allocation_size);
+      const auto GpuWrite = [&](uint64_t offset, uint32_t value) {
+        (void)cache.ObtainBuffer(base + offset, sizeof(uint32_t), true, false);
+        cache.FillBuffer(base + offset, sizeof(uint32_t), value, false);
+      };
+      const auto Backing = [&](uint64_t offset) {
+        uint32_t value = 0;
+        Libs::LibKernel::Memory::TryReadBacking(base + offset, &value,
+                                                sizeof(value));
+        return value;
+      };
+      const auto Clean = [&](uint64_t offset) {
+        return !cache.HasGpuDirtyBytes(base + offset, sizeof(uint32_t)) &&
+               !cache.IsRegionGpuModified(base + offset, sizeof(uint32_t));
+      };
+      const auto Dirty = [&](uint64_t offset) {
+        return cache.HasGpuDirtyBytes(base + offset, sizeof(uint32_t));
+      };
+
+      GpuWrite(hot_offset, 0x11111111u);
+      scheduler.Flush();
+      cache.ReadMemory(base + hot_offset, sizeof(uint32_t));
+      Require(name, "first download",
+              Backing(hot_offset) == 0x11111111u && Clean(hot_offset),
+              "the guest read did not publish the submitted GPU fill");
+
+      GpuWrite(hot_offset, 0x22222222u);
+      GpuWrite(cold_offset, 0x33333333u);
+      cache.PublishReadHot();
+      scheduler.Finish();
+      Require(name, "hot page publish",
+              Backing(hot_offset) == 0x22222222u && Clean(hot_offset),
+              "PublishReadHot did not write the hot page to backing memory");
+      Require(name, "cold page untouched",
+              Backing(cold_offset) == stale && Dirty(cold_offset),
+              "a page that was never read was published or cleaned");
+
+      GpuWrite(hot_offset, 0x44444444u);
+      cache.PublishReadHot();
+      GpuWrite(hot_offset, 0x55555555u);
+      scheduler.Finish();
+      Require(name, "superseded publish", Dirty(hot_offset),
+              "a publish superseded by a newer GPU write cleaned the page");
+      cache.ReadMemory(base + hot_offset, sizeof(uint32_t));
+      Require(name, "newest value after supersede",
+              Backing(hot_offset) == 0x55555555u && Clean(hot_offset),
+              "the guest read after a superseded publish missed the newest GPU write");
+
+      cache.ReadMemory(base + cold_offset, sizeof(uint32_t));
+
+      constexpr uint64_t decay_offset = 0x310000;
+      std::memcpy(memory + decay_offset, &stale, sizeof(stale));
+      GpuWrite(decay_offset, 0x10203040u);
+      scheduler.Flush();
+      cache.ReadMemory(base + decay_offset, sizeof(uint32_t));
+      for (int flip = 0; flip != 8; ++flip) {
+        cache.DecayReadHot();
+      }
+      GpuWrite(decay_offset, 0x50607080u);
+      cache.PublishReadHot();
+      scheduler.Finish();
+      Require(name, "hot for eight flips",
+              Backing(decay_offset) == 0x50607080u && Clean(decay_offset),
+              "a page read eight flips ago was not published");
+      cache.DecayReadHot();
+      GpuWrite(decay_offset, 0x91a2b3c4u);
+      cache.PublishReadHot();
+      scheduler.Finish();
+      Require(name, "cold after nine flips",
+              Backing(decay_offset) == 0x50607080u && Dirty(decay_offset),
+              "a page read nine flips ago was still published");
+      cache.ReadMemory(base + decay_offset, sizeof(uint32_t));
+
+      constexpr uint64_t owner_offset = 0x540000;
+      constexpr uint64_t neighbor_offset = owner_offset + 0x1000;
+      std::memcpy(memory + neighbor_offset, &stale, sizeof(stale));
+      (void)cache.ObtainBuffer(base + owner_offset, 0x2000, true, false);
+      cache.FillBuffer(base + owner_offset, 0x2000, 0x2468ace0u, false);
+      scheduler.Flush();
+      cache.ReadMemory(base + owner_offset, sizeof(uint32_t));
+      GpuWrite(owner_offset, 0x2468ace1u);
+      GpuWrite(neighbor_offset, 0x2468ace1u);
+      cache.PublishReadHot();
+      scheduler.Finish();
+      Require(name, "only the hot page of a buffer",
+              Backing(owner_offset) == 0x2468ace1u && Clean(owner_offset) &&
+                  Backing(neighbor_offset) != 0x2468ace1u &&

Done in 4fd72a87: the neighbour page must keep its original backing (stale). The test passes, so the first read downloads only its own page and nothing crosses into the neighbour.

**@⁠Nawid3333** on GitHub · 2026-10-31 22:09 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/2037#discussion_r4433474700) On `tests/ShaderRecompilerComputeTests.cpp` line 4548 (outdated): ```diff @@ -4422,6 +4422,162 @@ class VulkanHarness { std::printf("[host] %-32s ok\n", name); } + void CheckBufferCacheReadHotPublish() { + constexpr const char *name = "BufferCacheReadHotPublish"; + constexpr uintptr_t base = 0x000000020a000000ull; + constexpr uint64_t allocation_size = 0x800000; + constexpr uint64_t allocation_alignment = 0x10000; + constexpr uint64_t hot_offset = 0x10000; + constexpr uint64_t cold_offset = 0x80000; + constexpr uint32_t stale = 0x0badf00du; + + EnsureRuntimeContext(); + RenderContext context(m_runtime_context); + auto &scheduler = context.GetCommandScheduler(); + HW::Context registers{}; + HW::UserConfig user_config{}; + HW::Shader shaders{}; + scheduler.Begin(registers, user_config, shaders); + context.InitializeGpu(nullptr); + + int64_t direct_offset = -1; + Require(name, "direct allocation", + Libs::LibKernel::Memory::KernelAllocateDirectMemory( + 0, Libs::LibKernel::Memory::KernelGetDirectMemorySize(), + allocation_size, allocation_alignment, 0, &direct_offset) == 0, + "read-hot direct-memory allocation failed"); + void *mapped = reinterpret_cast<void *>(base); + Require(name, "direct mapping", + Libs::LibKernel::Memory::KernelMapDirectMemory( + &mapped, allocation_size, 0x3, 0x10, direct_offset, + allocation_alignment) == 0 && + mapped == reinterpret_cast<void *>(base), + "read-hot fixed direct-memory mapping failed"); + auto *memory = static_cast<uint8_t *>(mapped); + std::memcpy(memory + hot_offset, &stale, sizeof(stale)); + std::memcpy(memory + cold_offset, &stale, sizeof(stale)); + + { + auto &cache = context.GetBufferCache(); + context.MapMemory(base, allocation_size); + const auto GpuWrite = [&](uint64_t offset, uint32_t value) { + (void)cache.ObtainBuffer(base + offset, sizeof(uint32_t), true, false); + cache.FillBuffer(base + offset, sizeof(uint32_t), value, false); + }; + const auto Backing = [&](uint64_t offset) { + uint32_t value = 0; + Libs::LibKernel::Memory::TryReadBacking(base + offset, &value, + sizeof(value)); + return value; + }; + const auto Clean = [&](uint64_t offset) { + return !cache.HasGpuDirtyBytes(base + offset, sizeof(uint32_t)) && + !cache.IsRegionGpuModified(base + offset, sizeof(uint32_t)); + }; + const auto Dirty = [&](uint64_t offset) { + return cache.HasGpuDirtyBytes(base + offset, sizeof(uint32_t)); + }; + + GpuWrite(hot_offset, 0x11111111u); + scheduler.Flush(); + cache.ReadMemory(base + hot_offset, sizeof(uint32_t)); + Require(name, "first download", + Backing(hot_offset) == 0x11111111u && Clean(hot_offset), + "the guest read did not publish the submitted GPU fill"); + + GpuWrite(hot_offset, 0x22222222u); + GpuWrite(cold_offset, 0x33333333u); + cache.PublishReadHot(); + scheduler.Finish(); + Require(name, "hot page publish", + Backing(hot_offset) == 0x22222222u && Clean(hot_offset), + "PublishReadHot did not write the hot page to backing memory"); + Require(name, "cold page untouched", + Backing(cold_offset) == stale && Dirty(cold_offset), + "a page that was never read was published or cleaned"); + + GpuWrite(hot_offset, 0x44444444u); + cache.PublishReadHot(); + GpuWrite(hot_offset, 0x55555555u); + scheduler.Finish(); + Require(name, "superseded publish", Dirty(hot_offset), + "a publish superseded by a newer GPU write cleaned the page"); + cache.ReadMemory(base + hot_offset, sizeof(uint32_t)); + Require(name, "newest value after supersede", + Backing(hot_offset) == 0x55555555u && Clean(hot_offset), + "the guest read after a superseded publish missed the newest GPU write"); + + cache.ReadMemory(base + cold_offset, sizeof(uint32_t)); + + constexpr uint64_t decay_offset = 0x310000; + std::memcpy(memory + decay_offset, &stale, sizeof(stale)); + GpuWrite(decay_offset, 0x10203040u); + scheduler.Flush(); + cache.ReadMemory(base + decay_offset, sizeof(uint32_t)); + for (int flip = 0; flip != 8; ++flip) { + cache.DecayReadHot(); + } + GpuWrite(decay_offset, 0x50607080u); + cache.PublishReadHot(); + scheduler.Finish(); + Require(name, "hot for eight flips", + Backing(decay_offset) == 0x50607080u && Clean(decay_offset), + "a page read eight flips ago was not published"); + cache.DecayReadHot(); + GpuWrite(decay_offset, 0x91a2b3c4u); + cache.PublishReadHot(); + scheduler.Finish(); + Require(name, "cold after nine flips", + Backing(decay_offset) == 0x50607080u && Dirty(decay_offset), + "a page read nine flips ago was still published"); + cache.ReadMemory(base + decay_offset, sizeof(uint32_t)); + + constexpr uint64_t owner_offset = 0x540000; + constexpr uint64_t neighbor_offset = owner_offset + 0x1000; + std::memcpy(memory + neighbor_offset, &stale, sizeof(stale)); + (void)cache.ObtainBuffer(base + owner_offset, 0x2000, true, false); + cache.FillBuffer(base + owner_offset, 0x2000, 0x2468ace0u, false); + scheduler.Flush(); + cache.ReadMemory(base + owner_offset, sizeof(uint32_t)); + GpuWrite(owner_offset, 0x2468ace1u); + GpuWrite(neighbor_offset, 0x2468ace1u); + cache.PublishReadHot(); + scheduler.Finish(); + Require(name, "only the hot page of a buffer", + Backing(owner_offset) == 0x2468ace1u && Clean(owner_offset) && + Backing(neighbor_offset) != 0x2468ace1u && ``` Done in 4fd72a87: the neighbour page must keep its original backing (`stale`). The test passes, so the first read downloads only its own page and nothing crosses into the neighbour.
Author
Owner

@⁠coderabbitai[bot] on GitHub · 2026-10-31 22:26 UTC · original · reviewed

Caution

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

⚠️ Outside diff range comments (2)

Major · Supersede overlapping publications in TextureCache::CommitGpuWrite. · renderCompute.cpp:369-374

src/graphics/host_gpu/renderer/renderCompute.cpp:369-374
Major · Data Integrity & Integration · Quick win

Supersede overlapping publications in TextureCache::CommitGpuWrite.

A read-hot publication can be in flight when a guest-backed fill or depth-to-color copy writes the same image and returns before the common SupersedeAllPublishes() call. CommitGpuWrite currently does not supersede that publication. The deferred publication can write its old snapshot, then ConfirmPublish can clear BufferCache GPU-dirty tracking. A later CPU read can therefore use stale backing bytes.

Use the existing m_buffer_cache reference at the shared image-write boundary:

Suggested fix
 void TextureCache::CommitGpuWrite(Image& image) {
 	if (!image.depth_id && image.backing.image == nullptr) {
 		EXIT("TextureCache: GPU writes require a native image or stencil association\n");
 	}
+	m_buffer_cache.SupersedePublishes(image.info.data.address, image.info.data.size);
 	image.ClearBufferModified();
 	if (image.IsCpuDirty()) {
 		image.RefreshComplete();
Minor · Retain ranges that were not successfully batched. · bufferCache.cpp:408-427

src/graphics/host_gpu/renderer/cache/bufferCache.cpp:408-427
Minor · Performance & Scalability · Quick win

Retain ranges that were not successfully batched.

m_publish_ranges.Clear() removes owner-rejected and over-budget ranges, even though they remain in m_gpu_modified_ranges. A failed Map removes all queued ranges before any publication is created. A later CPU read then uses the blocking download path instead of receiving the queued publication.

Remove only the targets after Map succeeds. Do not clear the entire set.

Suggested fix
-	m_publish_ranges.Clear();
 	if (batch->empty()) {
 		return;
 	}
 	auto [mapped, offset] = m_download_buffer.Map(total, 64);
 	if (mapped == nullptr) {
 		return;
 	}
+	for (const auto& target: targets) {
+		m_publish_ranges.Subtract(target.vaddr, target.size);
+	}
Nitpick comments: 1
tests/ShaderRecompilerComputeTests.cpp: 1

4554-4566: Trivial · Maintainability & Code Quality · Quick win

Route the CPU-write case through InvalidateMemory.

The CPU-write case calls cache.ReadMemory(base + race_offset, sizeof(uint32_t), true) directly, then writes the new value with std::memcpy. Guest CPU writes reach this drain path through InvalidateMemory, which passes ReadMemory(..., true) as the callback to m_memory_tracker.InvalidateRegion. This test never enters that path. If InvalidateMemory stopped calling the tracker, or the callback were dropped or changed, the assertions would still pass.

Before the std::memcpy, call InvalidateMemory for base + race_offset with the same size. Keep the existing backing and clean-state checks. Check the current InvalidateMemory signature in bufferCache.cpp before making the change.


Review info
Run configuration
Commits

Reviewing files that changed from the base of the PR and between 29cd962462 and 4fd72a87a9.

Files selected for processing: 1
  • tests/ShaderRecompilerComputeTests.cpp

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

**@⁠coderabbitai[bot]** on GitHub · 2026-10-31 22:26 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/2037#pullrequestreview-5725259212) · **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 (2)** > > <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> · Supersede overlapping publications in <code>TextureCache::CommitGpuWrite</code>. · <code>renderCompute.cpp:369-374</code></summary><blockquote> > <dl> > <dd> > > `src/graphics/host_gpu/renderer/renderCompute.cpp:369-374` > **<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** · **Quick win** > > **Supersede overlapping publications in `TextureCache::CommitGpuWrite`.** > > A read-hot publication can be in flight when a guest-backed fill or depth-to-color copy writes the same image and returns before the common `SupersedeAllPublishes()` call. `CommitGpuWrite` currently does not supersede that publication. The deferred publication can write its old snapshot, then `ConfirmPublish` can clear BufferCache GPU-dirty tracking. A later CPU read can therefore use stale backing bytes. > > Use the existing `m_buffer_cache` reference at the shared image-write boundary: > > <details> > <summary>Suggested fix</summary> > <dl> > <dd> > > ```diff > void TextureCache::CommitGpuWrite(Image& image) { > if (!image.depth_id && image.backing.image == nullptr) { > EXIT("TextureCache: GPU writes require a native image or stencil association\n"); > } > + m_buffer_cache.SupersedePublishes(image.info.data.address, image.info.data.size); > image.ClearBufferModified(); > if (image.IsCpuDirty()) { > image.RefreshComplete(); > ``` > > </dd> > </dl> > </details> > > <!-- cr-comment:v1:21ef85c666e5c97618fc3e20 --> > > </dd> > </dl> > </blockquote></details> > <details> > <summary><strong><picture><img src="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/amber-16.svg" width="16" height="16" align="absmiddle" alt=""></picture> Minor</strong> · Retain ranges that were not successfully batched. · <code>bufferCache.cpp:408-427</code></summary><blockquote> > <dl> > <dd> > > `src/graphics/host_gpu/renderer/cache/bufferCache.cpp:408-427` > **<picture><img src="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/amber-16.svg" width="16" height="16" align="absmiddle" alt=""></picture> Minor** · **Performance & Scalability** · **Quick win** > > **Retain ranges that were not successfully batched.** > > `m_publish_ranges.Clear()` removes owner-rejected and over-budget ranges, even though they remain in `m_gpu_modified_ranges`. A failed `Map` removes all queued ranges before any publication is created. A later CPU read then uses the blocking download path instead of receiving the queued publication. > > Remove only the targets after `Map` succeeds. Do not clear the entire set. > > <details> > <summary>Suggested fix</summary> > <dl> > <dd> > > ```diff > - m_publish_ranges.Clear(); > if (batch->empty()) { > return; > } > auto [mapped, offset] = m_download_buffer.Map(total, 64); > if (mapped == nullptr) { > return; > } > + for (const auto& target: targets) { > + m_publish_ranges.Subtract(target.vaddr, target.size); > + } > ``` > > </dd> > </dl> > </details> > > <!-- cr-comment:v1:d76eaabcc93ff49e0d749b02 --> > > </dd> > </dl> > </blockquote></details> <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> `4554-4566`: **<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** **Route the CPU-write case through `InvalidateMemory`.** The CPU-write case calls `cache.ReadMemory(base + race_offset, sizeof(uint32_t), true)` directly, then writes the new value with `std::memcpy`. Guest CPU writes reach this drain path through `InvalidateMemory`, which passes `ReadMemory(..., true)` as the callback to `m_memory_tracker.InvalidateRegion`. This test never enters that path. If `InvalidateMemory` stopped calling the tracker, or the callback were dropped or changed, the assertions would still pass. Before the `std::memcpy`, call `InvalidateMemory` for `base + race_offset` with the same size. Keep the existing backing and clean-state checks. Check the current `InvalidateMemory` signature in `bufferCache.cpp` before making the change. <!-- cr-comment:v1:7e625f574b3a7f521b26233b --> </dd> </dl> </blockquote></details> </dd> </dl> </blockquote></details> --- <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/bbf96a5e9f69cad84b99791d6418c81d01913e75/.coderabbit.yaml>) - **Commit**: [bbf96a5](<https://github.com/KytyPS5/KytyPS5/commit/bbf96a5e9f69cad84b99791d6418c81d01913e75>) - **Layers**: Repository YAML - **Review profile**: CHILL - **Plan**: Advanced - **Run ID**: `247291be-6d27-413f-ba8b-2481c0787494` </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 29cd962462b06eb6ba1585014aef1d8ad2c6b683 and 4fd72a87a971e158d903617f00fc4e6a95f9f205. </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: 1</summary> <dl> <dd> * `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; 8 remain after this review. </dd> </dl> </details> <!-- This is an auto-generated comment by CodeRabbit for review status --> <!-- coderabbit-review-publication v1 publication=52c6727b-e6ea-4845-849b-eb0fe423ec31 attempt=8ffabcb0-78e5-4618-8d9e-50b406d48953 batch=1/1 -->
Author
Owner

@⁠coderabbitai[bot] on GitHub · 2026-10-31 22:29 UTC · original
On tests/ShaderRecompilerComputeTests.cpp line 4548 (outdated):

@@ -4422,6 +4422,162 @@ class VulkanHarness {
     std::printf("[host]    %-32s ok\n", name);
   }
 
+  void CheckBufferCacheReadHotPublish() {
+    constexpr const char *name = "BufferCacheReadHotPublish";
+    constexpr uintptr_t base = 0x000000020a000000ull;
+    constexpr uint64_t allocation_size = 0x800000;
+    constexpr uint64_t allocation_alignment = 0x10000;
+    constexpr uint64_t hot_offset = 0x10000;
+    constexpr uint64_t cold_offset = 0x80000;
+    constexpr uint32_t stale = 0x0badf00du;
+
+    EnsureRuntimeContext();
+    RenderContext context(m_runtime_context);
+    auto &scheduler = context.GetCommandScheduler();
+    HW::Context registers{};
+    HW::UserConfig user_config{};
+    HW::Shader shaders{};
+    scheduler.Begin(registers, user_config, shaders);
+    context.InitializeGpu(nullptr);
+
+    int64_t direct_offset = -1;
+    Require(name, "direct allocation",
+            Libs::LibKernel::Memory::KernelAllocateDirectMemory(
+                0, Libs::LibKernel::Memory::KernelGetDirectMemorySize(),
+                allocation_size, allocation_alignment, 0, &direct_offset) == 0,
+            "read-hot direct-memory allocation failed");
+    void *mapped = reinterpret_cast<void *>(base);
+    Require(name, "direct mapping",
+            Libs::LibKernel::Memory::KernelMapDirectMemory(
+                &mapped, allocation_size, 0x3, 0x10, direct_offset,
+                allocation_alignment) == 0 &&
+                mapped == reinterpret_cast<void *>(base),
+            "read-hot fixed direct-memory mapping failed");
+    auto *memory = static_cast<uint8_t *>(mapped);
+    std::memcpy(memory + hot_offset, &stale, sizeof(stale));
+    std::memcpy(memory + cold_offset, &stale, sizeof(stale));
+
+    {
+      auto &cache = context.GetBufferCache();
+      context.MapMemory(base, allocation_size);
+      const auto GpuWrite = [&](uint64_t offset, uint32_t value) {
+        (void)cache.ObtainBuffer(base + offset, sizeof(uint32_t), true, false);
+        cache.FillBuffer(base + offset, sizeof(uint32_t), value, false);
+      };
+      const auto Backing = [&](uint64_t offset) {
+        uint32_t value = 0;
+        Libs::LibKernel::Memory::TryReadBacking(base + offset, &value,
+                                                sizeof(value));
+        return value;
+      };
+      const auto Clean = [&](uint64_t offset) {
+        return !cache.HasGpuDirtyBytes(base + offset, sizeof(uint32_t)) &&
+               !cache.IsRegionGpuModified(base + offset, sizeof(uint32_t));
+      };
+      const auto Dirty = [&](uint64_t offset) {
+        return cache.HasGpuDirtyBytes(base + offset, sizeof(uint32_t));
+      };
+
+      GpuWrite(hot_offset, 0x11111111u);
+      scheduler.Flush();
+      cache.ReadMemory(base + hot_offset, sizeof(uint32_t));
+      Require(name, "first download",
+              Backing(hot_offset) == 0x11111111u && Clean(hot_offset),
+              "the guest read did not publish the submitted GPU fill");
+
+      GpuWrite(hot_offset, 0x22222222u);
+      GpuWrite(cold_offset, 0x33333333u);
+      cache.PublishReadHot();
+      scheduler.Finish();
+      Require(name, "hot page publish",
+              Backing(hot_offset) == 0x22222222u && Clean(hot_offset),
+              "PublishReadHot did not write the hot page to backing memory");
+      Require(name, "cold page untouched",
+              Backing(cold_offset) == stale && Dirty(cold_offset),
+              "a page that was never read was published or cleaned");
+
+      GpuWrite(hot_offset, 0x44444444u);
+      cache.PublishReadHot();
+      GpuWrite(hot_offset, 0x55555555u);
+      scheduler.Finish();
+      Require(name, "superseded publish", Dirty(hot_offset),
+              "a publish superseded by a newer GPU write cleaned the page");
+      cache.ReadMemory(base + hot_offset, sizeof(uint32_t));
+      Require(name, "newest value after supersede",
+              Backing(hot_offset) == 0x55555555u && Clean(hot_offset),
+              "the guest read after a superseded publish missed the newest GPU write");
+
+      cache.ReadMemory(base + cold_offset, sizeof(uint32_t));
+
+      constexpr uint64_t decay_offset = 0x310000;
+      std::memcpy(memory + decay_offset, &stale, sizeof(stale));
+      GpuWrite(decay_offset, 0x10203040u);
+      scheduler.Flush();
+      cache.ReadMemory(base + decay_offset, sizeof(uint32_t));
+      for (int flip = 0; flip != 8; ++flip) {
+        cache.DecayReadHot();
+      }
+      GpuWrite(decay_offset, 0x50607080u);
+      cache.PublishReadHot();
+      scheduler.Finish();
+      Require(name, "hot for eight flips",
+              Backing(decay_offset) == 0x50607080u && Clean(decay_offset),
+              "a page read eight flips ago was not published");
+      cache.DecayReadHot();
+      GpuWrite(decay_offset, 0x91a2b3c4u);
+      cache.PublishReadHot();
+      scheduler.Finish();
+      Require(name, "cold after nine flips",
+              Backing(decay_offset) == 0x50607080u && Dirty(decay_offset),
+              "a page read nine flips ago was still published");
+      cache.ReadMemory(base + decay_offset, sizeof(uint32_t));
+
+      constexpr uint64_t owner_offset = 0x540000;
+      constexpr uint64_t neighbor_offset = owner_offset + 0x1000;
+      std::memcpy(memory + neighbor_offset, &stale, sizeof(stale));
+      (void)cache.ObtainBuffer(base + owner_offset, 0x2000, true, false);
+      cache.FillBuffer(base + owner_offset, 0x2000, 0x2468ace0u, false);
+      scheduler.Flush();
+      cache.ReadMemory(base + owner_offset, sizeof(uint32_t));
+      GpuWrite(owner_offset, 0x2468ace1u);
+      GpuWrite(neighbor_offset, 0x2468ace1u);
+      cache.PublishReadHot();
+      scheduler.Finish();
+      Require(name, "only the hot page of a buffer",
+              Backing(owner_offset) == 0x2468ace1u && Clean(owner_offset) &&
+                  Backing(neighbor_offset) != 0x2468ace1u &&

@⁠Nawid3333 Thanks for the fix in 4fd72a87. Requiring Backing(neighbor_offset) == stale closes the test gap: an unintended download of the neighbor’s earlier GPU value can no longer pass.


If you found this review helpful, would you consider giving us a shout-out on X?

Thank you for using CodeRabbit!

Review thread resolved.

You are interacting with an AI system.

**@⁠coderabbitai[bot]** on GitHub · 2026-10-31 22:29 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/2037#discussion_r4433537051) On `tests/ShaderRecompilerComputeTests.cpp` line 4548 (outdated): ```diff @@ -4422,6 +4422,162 @@ class VulkanHarness { std::printf("[host] %-32s ok\n", name); } + void CheckBufferCacheReadHotPublish() { + constexpr const char *name = "BufferCacheReadHotPublish"; + constexpr uintptr_t base = 0x000000020a000000ull; + constexpr uint64_t allocation_size = 0x800000; + constexpr uint64_t allocation_alignment = 0x10000; + constexpr uint64_t hot_offset = 0x10000; + constexpr uint64_t cold_offset = 0x80000; + constexpr uint32_t stale = 0x0badf00du; + + EnsureRuntimeContext(); + RenderContext context(m_runtime_context); + auto &scheduler = context.GetCommandScheduler(); + HW::Context registers{}; + HW::UserConfig user_config{}; + HW::Shader shaders{}; + scheduler.Begin(registers, user_config, shaders); + context.InitializeGpu(nullptr); + + int64_t direct_offset = -1; + Require(name, "direct allocation", + Libs::LibKernel::Memory::KernelAllocateDirectMemory( + 0, Libs::LibKernel::Memory::KernelGetDirectMemorySize(), + allocation_size, allocation_alignment, 0, &direct_offset) == 0, + "read-hot direct-memory allocation failed"); + void *mapped = reinterpret_cast<void *>(base); + Require(name, "direct mapping", + Libs::LibKernel::Memory::KernelMapDirectMemory( + &mapped, allocation_size, 0x3, 0x10, direct_offset, + allocation_alignment) == 0 && + mapped == reinterpret_cast<void *>(base), + "read-hot fixed direct-memory mapping failed"); + auto *memory = static_cast<uint8_t *>(mapped); + std::memcpy(memory + hot_offset, &stale, sizeof(stale)); + std::memcpy(memory + cold_offset, &stale, sizeof(stale)); + + { + auto &cache = context.GetBufferCache(); + context.MapMemory(base, allocation_size); + const auto GpuWrite = [&](uint64_t offset, uint32_t value) { + (void)cache.ObtainBuffer(base + offset, sizeof(uint32_t), true, false); + cache.FillBuffer(base + offset, sizeof(uint32_t), value, false); + }; + const auto Backing = [&](uint64_t offset) { + uint32_t value = 0; + Libs::LibKernel::Memory::TryReadBacking(base + offset, &value, + sizeof(value)); + return value; + }; + const auto Clean = [&](uint64_t offset) { + return !cache.HasGpuDirtyBytes(base + offset, sizeof(uint32_t)) && + !cache.IsRegionGpuModified(base + offset, sizeof(uint32_t)); + }; + const auto Dirty = [&](uint64_t offset) { + return cache.HasGpuDirtyBytes(base + offset, sizeof(uint32_t)); + }; + + GpuWrite(hot_offset, 0x11111111u); + scheduler.Flush(); + cache.ReadMemory(base + hot_offset, sizeof(uint32_t)); + Require(name, "first download", + Backing(hot_offset) == 0x11111111u && Clean(hot_offset), + "the guest read did not publish the submitted GPU fill"); + + GpuWrite(hot_offset, 0x22222222u); + GpuWrite(cold_offset, 0x33333333u); + cache.PublishReadHot(); + scheduler.Finish(); + Require(name, "hot page publish", + Backing(hot_offset) == 0x22222222u && Clean(hot_offset), + "PublishReadHot did not write the hot page to backing memory"); + Require(name, "cold page untouched", + Backing(cold_offset) == stale && Dirty(cold_offset), + "a page that was never read was published or cleaned"); + + GpuWrite(hot_offset, 0x44444444u); + cache.PublishReadHot(); + GpuWrite(hot_offset, 0x55555555u); + scheduler.Finish(); + Require(name, "superseded publish", Dirty(hot_offset), + "a publish superseded by a newer GPU write cleaned the page"); + cache.ReadMemory(base + hot_offset, sizeof(uint32_t)); + Require(name, "newest value after supersede", + Backing(hot_offset) == 0x55555555u && Clean(hot_offset), + "the guest read after a superseded publish missed the newest GPU write"); + + cache.ReadMemory(base + cold_offset, sizeof(uint32_t)); + + constexpr uint64_t decay_offset = 0x310000; + std::memcpy(memory + decay_offset, &stale, sizeof(stale)); + GpuWrite(decay_offset, 0x10203040u); + scheduler.Flush(); + cache.ReadMemory(base + decay_offset, sizeof(uint32_t)); + for (int flip = 0; flip != 8; ++flip) { + cache.DecayReadHot(); + } + GpuWrite(decay_offset, 0x50607080u); + cache.PublishReadHot(); + scheduler.Finish(); + Require(name, "hot for eight flips", + Backing(decay_offset) == 0x50607080u && Clean(decay_offset), + "a page read eight flips ago was not published"); + cache.DecayReadHot(); + GpuWrite(decay_offset, 0x91a2b3c4u); + cache.PublishReadHot(); + scheduler.Finish(); + Require(name, "cold after nine flips", + Backing(decay_offset) == 0x50607080u && Dirty(decay_offset), + "a page read nine flips ago was still published"); + cache.ReadMemory(base + decay_offset, sizeof(uint32_t)); + + constexpr uint64_t owner_offset = 0x540000; + constexpr uint64_t neighbor_offset = owner_offset + 0x1000; + std::memcpy(memory + neighbor_offset, &stale, sizeof(stale)); + (void)cache.ObtainBuffer(base + owner_offset, 0x2000, true, false); + cache.FillBuffer(base + owner_offset, 0x2000, 0x2468ace0u, false); + scheduler.Flush(); + cache.ReadMemory(base + owner_offset, sizeof(uint32_t)); + GpuWrite(owner_offset, 0x2468ace1u); + GpuWrite(neighbor_offset, 0x2468ace1u); + cache.PublishReadHot(); + scheduler.Finish(); + Require(name, "only the hot page of a buffer", + Backing(owner_offset) == 0x2468ace1u && Clean(owner_offset) && + Backing(neighbor_offset) != 0x2468ace1u && ``` <!-- coderabbit-ai-generated:v1 --> @⁠Nawid3333 Thanks for the fix in `4fd72a87`. Requiring `Backing(neighbor_offset) == stale` closes the test gap: an unintended download of the neighbor’s earlier GPU value can no longer pass. --- <!-- This is an auto-generated comment: tweet message by coderabbit.ai --> If you found this review helpful, would you consider giving us a shout-out on [X](https://twitter.com/intent/tweet?related=CodeRabbitAI&text=Catch%20cross-page%20publication%20bugs%20by%20checking%20the%20neighbor%E2%80%99s%20backing%20value%20stays%20%60stale%60.%20A%20precise%20test%20makes%20page%20isolation%20safer%E2%80%94try%20%40coderabbitai%20%F0%9F%94%8D%E2%9C%85&url=https%3A%2F%2Fgithub.com%2FKytyPS5%2FKytyPS5%2Fpull%2F2037%23discussion_r4433446923)? Thank you for using CodeRabbit! <!-- end of auto-generated comment: tweet message by coderabbit.ai --> Review thread resolved. <sub><i>You are interacting with an AI system.</i></sub> <!-- This is an auto-generated reply by CodeRabbit -->
Author
Owner

@⁠coderabbitai[bot] on GitHub · 2026-10-31 22:29 UTC · original
On tests/ShaderRecompilerComputeTests.cpp line 4478 (outdated):

@@ -4422,6 +4422,162 @@ class VulkanHarness {
     std::printf("[host]    %-32s ok\n", name);
   }
 
+  void CheckBufferCacheReadHotPublish() {
+    constexpr const char *name = "BufferCacheReadHotPublish";
+    constexpr uintptr_t base = 0x000000020a000000ull;
+    constexpr uint64_t allocation_size = 0x800000;
+    constexpr uint64_t allocation_alignment = 0x10000;
+    constexpr uint64_t hot_offset = 0x10000;
+    constexpr uint64_t cold_offset = 0x80000;
+    constexpr uint32_t stale = 0x0badf00du;
+
+    EnsureRuntimeContext();
+    RenderContext context(m_runtime_context);
+    auto &scheduler = context.GetCommandScheduler();
+    HW::Context registers{};
+    HW::UserConfig user_config{};
+    HW::Shader shaders{};
+    scheduler.Begin(registers, user_config, shaders);
+    context.InitializeGpu(nullptr);
+
+    int64_t direct_offset = -1;
+    Require(name, "direct allocation",
+            Libs::LibKernel::Memory::KernelAllocateDirectMemory(
+                0, Libs::LibKernel::Memory::KernelGetDirectMemorySize(),
+                allocation_size, allocation_alignment, 0, &direct_offset) == 0,
+            "read-hot direct-memory allocation failed");
+    void *mapped = reinterpret_cast<void *>(base);
+    Require(name, "direct mapping",
+            Libs::LibKernel::Memory::KernelMapDirectMemory(
+                &mapped, allocation_size, 0x3, 0x10, direct_offset,
+                allocation_alignment) == 0 &&
+                mapped == reinterpret_cast<void *>(base),
+            "read-hot fixed direct-memory mapping failed");
+    auto *memory = static_cast<uint8_t *>(mapped);
+    std::memcpy(memory + hot_offset, &stale, sizeof(stale));
+    std::memcpy(memory + cold_offset, &stale, sizeof(stale));
+
+    {
+      auto &cache = context.GetBufferCache();
+      context.MapMemory(base, allocation_size);
+      const auto GpuWrite = [&](uint64_t offset, uint32_t value) {
+        (void)cache.ObtainBuffer(base + offset, sizeof(uint32_t), true, false);
+        cache.FillBuffer(base + offset, sizeof(uint32_t), value, false);
+      };
+      const auto Backing = [&](uint64_t offset) {
+        uint32_t value = 0;
+        Libs::LibKernel::Memory::TryReadBacking(base + offset, &value,
+                                                sizeof(value));
+        return value;
+      };
+      const auto Clean = [&](uint64_t offset) {
+        return !cache.HasGpuDirtyBytes(base + offset, sizeof(uint32_t)) &&
+               !cache.IsRegionGpuModified(base + offset, sizeof(uint32_t));
+      };
+      const auto Dirty = [&](uint64_t offset) {
+        return cache.HasGpuDirtyBytes(base + offset, sizeof(uint32_t));

@⁠Nawid3333 Thanks for the fix in 4fd72a87. Requiring both checks addresses the gap in Dirty. Your reported RX 9070 XT test pass provides validation.


If you found this review helpful, would you consider giving us a shout-out on X?

Thank you for using CodeRabbit!

Review thread resolved.

You are interacting with an AI system.

**@⁠coderabbitai[bot]** on GitHub · 2026-10-31 22:29 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/2037#discussion_r4433537702) On `tests/ShaderRecompilerComputeTests.cpp` line 4478 (outdated): ```diff @@ -4422,6 +4422,162 @@ class VulkanHarness { std::printf("[host] %-32s ok\n", name); } + void CheckBufferCacheReadHotPublish() { + constexpr const char *name = "BufferCacheReadHotPublish"; + constexpr uintptr_t base = 0x000000020a000000ull; + constexpr uint64_t allocation_size = 0x800000; + constexpr uint64_t allocation_alignment = 0x10000; + constexpr uint64_t hot_offset = 0x10000; + constexpr uint64_t cold_offset = 0x80000; + constexpr uint32_t stale = 0x0badf00du; + + EnsureRuntimeContext(); + RenderContext context(m_runtime_context); + auto &scheduler = context.GetCommandScheduler(); + HW::Context registers{}; + HW::UserConfig user_config{}; + HW::Shader shaders{}; + scheduler.Begin(registers, user_config, shaders); + context.InitializeGpu(nullptr); + + int64_t direct_offset = -1; + Require(name, "direct allocation", + Libs::LibKernel::Memory::KernelAllocateDirectMemory( + 0, Libs::LibKernel::Memory::KernelGetDirectMemorySize(), + allocation_size, allocation_alignment, 0, &direct_offset) == 0, + "read-hot direct-memory allocation failed"); + void *mapped = reinterpret_cast<void *>(base); + Require(name, "direct mapping", + Libs::LibKernel::Memory::KernelMapDirectMemory( + &mapped, allocation_size, 0x3, 0x10, direct_offset, + allocation_alignment) == 0 && + mapped == reinterpret_cast<void *>(base), + "read-hot fixed direct-memory mapping failed"); + auto *memory = static_cast<uint8_t *>(mapped); + std::memcpy(memory + hot_offset, &stale, sizeof(stale)); + std::memcpy(memory + cold_offset, &stale, sizeof(stale)); + + { + auto &cache = context.GetBufferCache(); + context.MapMemory(base, allocation_size); + const auto GpuWrite = [&](uint64_t offset, uint32_t value) { + (void)cache.ObtainBuffer(base + offset, sizeof(uint32_t), true, false); + cache.FillBuffer(base + offset, sizeof(uint32_t), value, false); + }; + const auto Backing = [&](uint64_t offset) { + uint32_t value = 0; + Libs::LibKernel::Memory::TryReadBacking(base + offset, &value, + sizeof(value)); + return value; + }; + const auto Clean = [&](uint64_t offset) { + return !cache.HasGpuDirtyBytes(base + offset, sizeof(uint32_t)) && + !cache.IsRegionGpuModified(base + offset, sizeof(uint32_t)); + }; + const auto Dirty = [&](uint64_t offset) { + return cache.HasGpuDirtyBytes(base + offset, sizeof(uint32_t)); ``` <!-- coderabbit-ai-generated:v1 --> @⁠Nawid3333 Thanks for the fix in `4fd72a87`. Requiring both checks addresses the gap in `Dirty`. Your reported RX 9070 XT test pass provides validation. --- <!-- This is an auto-generated comment: tweet message by coderabbit.ai --> If you found this review helpful, would you consider giving us a shout-out on [X](https://twitter.com/intent/tweet?related=CodeRabbitAI&text=%40coderabbitai%20caught%20a%20subtle%20GPU%20tracking%20gap%3A%20%60Dirty%60%20now%20checks%20read%20protection%20as%20well%20as%20dirty%20bytes%2C%20preventing%20stale%20guest%20reads.%20Fix%20verified%20on%20RX%209070%20XT.%20Try%20%40coderabbitai%20%F0%9F%94%8D&url=https%3A%2F%2Fgithub.com%2FKytyPS5%2FKytyPS5%2Fpull%2F2037%23discussion_r4433446922)? Thank you for using CodeRabbit! <!-- end of auto-generated comment: tweet message by coderabbit.ai --> Review thread resolved. <sub><i>You are interacting with an AI system.</i></sub> <!-- This is an auto-generated reply by CodeRabbit -->
@ -4758,0 +4881,4 @@
Backing(owner_offset) == 0x2468ace1u && Clean(owner_offset) &&
Backing(neighbor_offset) == neighbor_before &&
Dirty(neighbor_offset),
"a publish crossed into a page that was never read");
Author
Owner

@⁠coderabbitai[bot] on GitHub · 2026-11-01 17:12 UTC · original · edited 2026-11-01 18:15 UTC

Minor · Functional Correctness · Quick win

Capture the neighbor baseline before the first read.

Line 4543 captures neighbor_before after the ReadMemory call on Line 4542. Assume that read incorrectly downloads the whole 0x2000 buffer. In that case, the neighbor backing becomes 0x2468ace0 before the test captures the baseline, so neighbor_before also holds 0x2468ace0. The later GpuWrite(neighbor_offset, ...) sets the neighbor dirty again, so Dirty(neighbor_offset) also passes. As a result, the "only the hot page of a buffer" check cannot detect a cross-page download on the first read.

A previous review comment raised this gap. The reply said the neighbor must keep stale. The current code does not seed stale into the neighbor and does not compare against stale.

🐛 Proposed fix
       constexpr uint64_t owner_offset = 0x540000;
       constexpr uint64_t neighbor_offset = owner_offset + 0x1000;
+      std::memcpy(memory + neighbor_offset, &stale, sizeof(stale));
       (void)cache.ObtainBuffer(base + owner_offset, 0x2000, true, false);
       cache.FillBuffer(base + owner_offset, 0x2000, 0x2468ace0u, false);
       scheduler.Flush();
       cache.ReadMemory(base + owner_offset, sizeof(uint32_t));
-      const auto neighbor_before = Backing(neighbor_offset);
       GpuWrite(owner_offset, 0x2468ace1u);
       GpuWrite(neighbor_offset, 0x2468ace1u);
       cache.PublishReadHot();
       scheduler.Finish();
       Require(name, "only the hot page of a buffer",
               Backing(owner_offset) == 0x2468ace1u && Clean(owner_offset) &&
-                  Backing(neighbor_offset) == neighbor_before &&
+                  Backing(neighbor_offset) == stale &&
                   Dirty(neighbor_offset),
               "a publish crossed into a page that was never read");
**@⁠coderabbitai[bot]** on GitHub · 2026-11-01 17:12 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/2037#discussion_r4436584658) · edited 2026-11-01 18:15 UTC <!-- coderabbit-ai-generated:v1 --> **<picture><img src="https://storage.googleapis.com/coderabbit_public_assets/review-comment-icons-v1/amber-16.svg" width="16" height="16" align="absmiddle" alt=""></picture> Minor** · **Functional Correctness** · **Quick win** **Capture the neighbor baseline before the first read.** Line 4543 captures `neighbor_before` after the `ReadMemory` call on Line 4542. Assume that read incorrectly downloads the whole 0x2000 buffer. In that case, the neighbor backing becomes `0x2468ace0` before the test captures the baseline, so `neighbor_before` also holds `0x2468ace0`. The later `GpuWrite(neighbor_offset, ...)` sets the neighbor dirty again, so `Dirty(neighbor_offset)` also passes. As a result, the "only the hot page of a buffer" check cannot detect a cross-page download on the first read. A previous review comment raised this gap. The reply said the neighbor must keep `stale`. The current code does not seed `stale` into the neighbor and does not compare against `stale`. <details> <summary>🐛 Proposed fix</summary> <dl> <dd> ```diff constexpr uint64_t owner_offset = 0x540000; constexpr uint64_t neighbor_offset = owner_offset + 0x1000; + std::memcpy(memory + neighbor_offset, &stale, sizeof(stale)); (void)cache.ObtainBuffer(base + owner_offset, 0x2000, true, false); cache.FillBuffer(base + owner_offset, 0x2000, 0x2468ace0u, false); scheduler.Flush(); cache.ReadMemory(base + owner_offset, sizeof(uint32_t)); - const auto neighbor_before = Backing(neighbor_offset); GpuWrite(owner_offset, 0x2468ace1u); GpuWrite(neighbor_offset, 0x2468ace1u); cache.PublishReadHot(); scheduler.Finish(); Require(name, "only the hot page of a buffer", Backing(owner_offset) == 0x2468ace1u && Clean(owner_offset) && - Backing(neighbor_offset) == neighbor_before && + Backing(neighbor_offset) == stale && Dirty(neighbor_offset), "a publish crossed into a page that was never read"); ``` </dd> </dl> </details> <!-- fingerprinting:phantom:medusa:pangolin --> <!-- cr-indicator-types:potential_issue --> <!-- cr-comment:v1:a95c0da6b1c6c4c464015650 --> <!-- This is an auto-generated reply by CodeRabbit -->
Author
Owner

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

Actionable comments posted: 1


Review info
Run configuration
Commits

Reviewing files that changed from the base of the PR and between 4fd72a87a9 and 346f4a8580.

Files selected for processing: 6
  • CMakeLists.txt
  • src/graphics/host_gpu/renderer/cache/bufferCache.cpp
  • src/graphics/host_gpu/renderer/cache/bufferCache.h
  • src/graphics/host_gpu/renderer/cache/textureCache.cpp
  • src/graphics/host_gpu/renderer/renderDraw.cpp
  • 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-01 17:12 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/2037#pullrequestreview-5729294288) · **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**: `bfb5b42e-6a82-408e-8bbf-953df459f695` </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 4fd72a87a971e158d903617f00fc4e6a95f9f205 and 346f4a858096f1fffec0495ee0e6e437dc21ef8b. </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> * `CMakeLists.txt` * `src/graphics/host_gpu/renderer/cache/bufferCache.cpp` * `src/graphics/host_gpu/renderer/cache/bufferCache.h` * `src/graphics/host_gpu/renderer/cache/textureCache.cpp` * `src/graphics/host_gpu/renderer/renderDraw.cpp` * `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> <!-- This is an auto-generated comment by CodeRabbit for review status --> <!-- coderabbit-review-publication v1 publication=3024c841-c4fd-4fc5-aa2e-3c9800b89c20 attempt=554d1053-ed0c-4ccb-873c-805fec85307e batch=1/1 -->
@ -4758,0 +4881,4 @@
Backing(owner_offset) == 0x2468ace1u && Clean(owner_offset) &&
Backing(neighbor_offset) == neighbor_before &&
Dirty(neighbor_offset),
"a publish crossed into a page that was never read");
Author
Owner

@⁠Nawid3333 on GitHub · 2026-11-01 18:15 UTC · original

Not applicable after the merge with main: since 1d5c8787f, the first ReadMemory downloads the 512 KiB window around the fault, clamped to the buffer, so the neighbor page is supposed to be refreshed by that read. With stale restored, this section fails on main's own behavior. This section checks PublishReadHot: the publish after the second GPU write must leave the never-read neighbor untouched and still GPU-dirty. Taking the baseline after the first read keeps that check independent of the window size. A publish that crosses into the neighbor writes 0x2468ace1 and fails it.

**@⁠Nawid3333** on GitHub · 2026-11-01 18:15 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/2037#discussion_r4436789646) Not applicable after the merge with main: since 1d5c8787f, the first `ReadMemory` downloads the 512 KiB window around the fault, clamped to the buffer, so the neighbor page is supposed to be refreshed by that read. With `stale` restored, this section fails on main's own behavior. This section checks `PublishReadHot`: the publish after the second GPU write must leave the never-read neighbor untouched and still GPU-dirty. Taking the baseline after the first read keeps that check independent of the window size. A publish that crosses into the neighbor writes `0x2468ace1` and fails it.
@ -4758,0 +4881,4 @@
Backing(owner_offset) == 0x2468ace1u && Clean(owner_offset) &&
Backing(neighbor_offset) == neighbor_before &&
Dirty(neighbor_offset),
"a publish crossed into a page that was never read");
Author
Owner

@⁠coderabbitai[bot] on GitHub · 2026-11-01 18:15 UTC · original

@⁠Nawid3333 You're right. I conflated the initial ReadMemory download with the later PublishReadHot operation.

The baseline belongs after ReadMemory because that download can legitimately refresh the neighbor. After the second GPU write, an incorrect publish into the neighbor would change its backing value to 0x2468ace1, so the comparison against neighbor_before would fail. Dirty(neighbor_offset) also checks that publication did not clean the neighbor.

The existing check tests the intended publication boundary. My proposed stale assertion was incorrect. I withdraw the finding.

Review thread resolved.

You are interacting with an AI system.

**@⁠coderabbitai[bot]** on GitHub · 2026-11-01 18:15 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/2037#discussion_r4436790728) <!-- coderabbit-ai-generated:v1 --> @⁠Nawid3333 You're right. I conflated the initial `ReadMemory` download with the later `PublishReadHot` operation. The baseline belongs after `ReadMemory` because that download can legitimately refresh the neighbor. After the second GPU write, an incorrect publish into the neighbor would change its backing value to `0x2468ace1`, so the comparison against `neighbor_before` would fail. `Dirty(neighbor_offset)` also checks that publication did not clean the neighbor. The existing check tests the intended publication boundary. My proposed `stale` assertion was incorrect. I withdraw the finding. <!-- <review_comment_withdrawn> --> Review thread resolved. <sub><i>You are interacting with an AI system.</i></sub> <!-- This is an auto-generated reply by CodeRabbit -->
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/2037/head:github-bot/gh-2037'
git switch 'github-bot/gh-2037'

Merge

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