graphics: write proven compute buffer fills to guest memory #1628

Manually merged
github merged 1 commit from gh-pr/1628/head into gh-pr/1628/base 2026-11-26 10:01:58 +00:00
Owner

@⁠ysnock404 on GitHub · 2026-10-18 01:44 UTC · original
ysnock404:fix/ppsa04159-cpu-buffer-fill → main · +67 −26 · 3 files · 1 commits · merged 2026-10-18 03:42 UTC

Goat Simulator 3 (PPSA04159) clears color metadata with compute shaders that fill a typed buffer with one constant. These ranges are not image backings, so ClearImageFromBuffer declined them and the dispatch marked the whole range GPU-modified. The next MaterializeColorClear then had to read the range back synchronously, about once per frame per render target.

When the fill is already proven by ResolveComputeBufferFill, the range is at most 1 MiB, holds no GPU-owned bytes and no registered image, write the value to the guest backing and invalidate the caches instead of dispatching. The metadata stays CPU-owned and needs no readback.

Measured in-game on an RTX 3070 at the same spot:
fps 6.2 -> 8.1
CPU waits on GPU 182/s (243 ms/s) -> 57/s (128 ms/s)
buffer downloads 11.2 MB/s -> 0.1 MB/s
buffer evictions 198/s -> 12/s
No visual difference observed.

**@⁠ysnock404** on GitHub · 2026-10-18 01:44 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/1628) `ysnock404:fix/ppsa04159-cpu-buffer-fill` → `main` · +67 −26 · 3 files · 1 commits · merged 2026-10-18 03:42 UTC Goat Simulator 3 (PPSA04159) clears color metadata with compute shaders that fill a typed buffer with one constant. These ranges are not image backings, so ClearImageFromBuffer declined them and the dispatch marked the whole range GPU-modified. The next MaterializeColorClear then had to read the range back synchronously, about once per frame per render target. When the fill is already proven by ResolveComputeBufferFill, the range is at most 1 MiB, holds no GPU-owned bytes and no registered image, write the value to the guest backing and invalidate the caches instead of dispatching. The metadata stays CPU-owned and needs no readback. Measured in-game on an RTX 3070 at the same spot: fps 6.2 -> 8.1 CPU waits on GPU 182/s (243 ms/s) -> 57/s (128 ms/s) buffer downloads 11.2 MB/s -> 0.1 MB/s buffer evictions 198/s -> 12/s No visual difference observed.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 10:01:57 +00:00
github manually merged commit 53c1f0e931 into gh-pr/1628/base 2026-11-26 10:01:58 +00:00
Author
Owner

@⁠coderabbitai[bot] on GitHub · 2026-10-18 01:49 UTC · original · edited 2026-10-18 03:43 UTC

Review in Change Stack →

📝 Walkthrough

When the GPU image-clear path cannot clear a buffer range, the code attempts a CPU fill. The helper accepts ranges up to 1 MiB that are divisible by four, contain no GPU-modified bytes, are not registered as textures, and can be written to guest backing. On success, it writes repeated 32-bit values, invalidates the range, and logs at most 32 fills.

Suggested reviewers: nmzik

Priority: ➖ Normal

Change: Refactor

Merge Risk: 🟡 Moderate · up to 799a9

When a buffer has another guest mapping, this optimization can leave GPU-visible data stale. Address alias handling before merging unless that risk is explicitly accepted.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 33.33% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 3 functions across 1 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
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.
Title check Passed The title clearly and concisely describes the main change: writing proven compute buffer fills to guest memory.
Description check Passed The description explains the affected scenario, implementation conditions, performance impact, and visual validation. It is directly related to the changeset.
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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

Comment @coderabbitai help to get the list of available commands.

**@⁠coderabbitai[bot]** on GitHub · 2026-10-18 01:49 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/1628#issuecomment-6244421356) · edited 2026-10-18 03:43 UTC <!-- 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/1628?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 --> <!-- walkthrough_start --> <details> <summary><strong>📝 Walkthrough</strong></summary> <dl> <dd> When the GPU image-clear path cannot clear a buffer range, the code attempts a CPU fill. The helper accepts ranges up to 1 MiB that are divisible by four, contain no GPU-modified bytes, are not registered as textures, and can be written to guest backing. On success, it writes repeated 32-bit values, invalidates the range, and logs at most 32 fills. **Suggested reviewers:** `nmzik` </dd> </dl> </details> <!-- walkthrough_end --> <!-- change_assessment_start --> **Priority:** ➖ Normal <!-- change_assessment_commit:"799a98bdff3ede39d8cdff0318e47eaf5d336a5c" --> **Change:** Refactor <!-- change_assessment_end --> <details> <summary><strong>Merge Risk:</strong> <strong>🟡 Moderate</strong> · up to <code>799a9</code></summary> <dl> <dd> <!-- final_review_risk_start --> <!-- final_review_risk_label **Merge Risk:** **🟡 Moderate** · up to `799a9` --> <!-- final_review_risk_coverage:{"sourceCommitId":"799a98bdff3ede39d8cdff0318e47eaf5d336a5c","coveredCommitId":"799a98bdff3ede39d8cdff0318e47eaf5d336a5c","kind":"reviewed"} --> When a buffer has another guest mapping, this optimization can leave GPU-visible data stale. Address alias handling before merging unless that risk is explicitly accepted. <!-- 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-failed-14-v2.svg" width="14" height="14" align="middle" alt="Failed" title="Failed"></picture></sup> 1</strong></summary> <dl> <dd> <strong><sup><picture><img src="https://storage.googleapis.com/coderabbit_public_assets/pre-merge-checks-counter-failed-14-v2.svg" width="14" height="14" align="middle" alt="Failed" title="Failed"></picture></sup> Failed checks: 1 warning</strong> | Check name | Status | Explanation | Resolution | | :---------------- | :----------------------------------------------------------------------------------------------: | :----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | :--------------------------------------------------------------------------------- | | Docstring Coverage | ![Warning](https://storage.googleapis.com/coderabbit_public_assets/pre-merge-checks-warning.svg) | Docstring coverage is 33.33% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 3 functions across 1 files. | Write docstrings for the functions missing them to satisfy the coverage threshold. | <details> <summary><strong><sup><picture><img src="https://storage.googleapis.com/coderabbit_public_assets/pre-merge-checks-counter-passed-14-v2.svg" width="14" height="14" align="middle" alt="Passed" title="Passed"></picture></sup> Passed checks: 4 passed</strong></summary> <dl> <dd> | Check name | Status | Explanation | | :------------------------ | :--------------------------------------------------------------------------------------------: | :------------------------------------------------------------------------------------------------------------------------------------------------------------- | | 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. | | Title check | ![Passed](https://storage.googleapis.com/coderabbit_public_assets/pre-merge-checks-passed.svg) | The title clearly and concisely describes the main change: writing proven compute buffer fills to guest memory. | | Description check | ![Passed](https://storage.googleapis.com/coderabbit_public_assets/pre-merge-checks-passed.svg) | The description explains the affected scenario, implementation conditions, performance impact, and visual validation. It is directly related to the changeset. | </dd> </dl> </details> </dd> </dl> </details> <!-- pre_merge_checks_walkthrough_end --> <!-- autopilot:start --> - [ ] <!-- {"checkboxId":"2708ad07-9f24-4260-9c11-7dc76a49f2e3"} --> <strong title="Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts">Autofix</strong> · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts <!-- autopilot:end --> <!-- tips_start --> Thanks for using [CodeRabbit](https://coderabbit.ai?utm_source=oss&utm_medium=github&utm_campaign=KytyPS5/KytyPS5&utm_content=1628)! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. <details> <summary><strong>❤️ Share</strong></summary> <dl> <dd> - [X](https://twitter.com/intent/tweet?text=I%20just%20used%20%40coderabbitai%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20the%20proprietary%20code.%20Check%20it%20out%3A&url=https%3A//coderabbit.ai) - [Mastodon](https://mastodon.social/share?text=I%20just%20used%20%40coderabbitai%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20the%20proprietary%20code.%20Check%20it%20out%3A%20https%3A%2F%2Fcoderabbit.ai) - [Reddit](https://www.reddit.com/submit?title=Great%20tool%20for%20code%20review%20-%20CodeRabbit&text=I%20just%20used%20CodeRabbit%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20proprietary%20code.%20Check%20it%20out%3A%20https%3A//coderabbit.ai) - [LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fcoderabbit.ai&mini=true&title=Great%20tool%20for%20code%20review%20-%20CodeRabbit&summary=I%20just%20used%20CodeRabbit%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20proprietary%20code) </dd> </dl> </details> <sub>Comment `@coderabbitai help` to get the list of available commands.</sub> <!-- tips_end -->
Author
Owner

@⁠coderabbitai[bot] on GitHub · 2026-10-18 01:49 UTC · original
On src/graphics/host_gpu/renderer/renderCompute.cpp line 146 (outdated):

@@ -128,6 +128,31 @@ bool ResolveComputeBufferFill(const ShaderComputeInputInfo& input, uint32_t grou
 	return true;
 }
 
+// A proven constant fill of a small, image-free range without GPU-owned bytes is written to the
+// guest backing instead of dispatched. Color metadata cleared this way stays CPU-owned, so reading
+// it back later needs no synchronous GPU download.
+static bool TryFillBufferOnCpu(RenderContext& context, const ShaderComputeInputInfo& input,
+                               uint64_t address, uint64_t size, uint32_t value) {
+	constexpr uint64_t MaxCpuFillSize = 1024 * 1024;
+	if (size > MaxCpuFillSize || size % sizeof(uint32_t) != 0 ||
+	    context.GetBufferCache().IsRegionGpuModified(address, size) ||
+	    context.GetTextureCache().IsRegionRegistered(address, size)) {
+		return false;
+	}
+	const std::vector<uint32_t> words(size / sizeof(uint32_t), value);
+	if (!LibKernel::Memory::TryWriteBacking(address, words.data(), size)) {
+		return false;
+	}
+	(void)context.InvalidateMemory(address, size);

🗄️ Data Integrity & Integration | 🟠 Major | 🏗️ Heavy lift

Account for other aliases of the written backing.

If two guest virtual ranges map the same backing bytes, TryWriteBacking changes both ranges’ CPU-visible contents. InvalidateMemory(address, size) updates cache state only for the destination virtual range. A cached buffer or image at the other alias can then retain the old value when the GPU reads it. Reject aliased ranges from this fallback, or invalidate every affected alias before returning success. (raw.githubusercontent.com)

**@⁠coderabbitai[bot]** on GitHub · 2026-10-18 01:49 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/1628#discussion_r4305323440) On `src/graphics/host_gpu/renderer/renderCompute.cpp` line 146 (outdated): ```diff @@ -128,6 +128,31 @@ bool ResolveComputeBufferFill(const ShaderComputeInputInfo& input, uint32_t grou return true; } +// A proven constant fill of a small, image-free range without GPU-owned bytes is written to the +// guest backing instead of dispatched. Color metadata cleared this way stays CPU-owned, so reading +// it back later needs no synchronous GPU download. +static bool TryFillBufferOnCpu(RenderContext& context, const ShaderComputeInputInfo& input, + uint64_t address, uint64_t size, uint32_t value) { + constexpr uint64_t MaxCpuFillSize = 1024 * 1024; + if (size > MaxCpuFillSize || size % sizeof(uint32_t) != 0 || + context.GetBufferCache().IsRegionGpuModified(address, size) || + context.GetTextureCache().IsRegionRegistered(address, size)) { + return false; + } + const std::vector<uint32_t> words(size / sizeof(uint32_t), value); + if (!LibKernel::Memory::TryWriteBacking(address, words.data(), size)) { + return false; + } + (void)context.InvalidateMemory(address, size); ``` **🗄️ Data Integrity & Integration** | **🟠 Major** | **🏗️ Heavy lift** **Account for other aliases of the written backing.** If two guest virtual ranges map the same backing bytes, `TryWriteBacking` changes both ranges’ CPU-visible contents. `InvalidateMemory(address, size)` updates cache state only for the destination virtual range. A cached buffer or image at the other alias can then retain the old value when the GPU reads it. Reject aliased ranges from this fallback, or invalidate every affected alias before returning success. ([raw.githubusercontent.com](https://raw.githubusercontent.com/KytyPS5/KytyPS5/main/src/kernel/memoryAddressSpace.inc)) <!-- fingerprinting:phantom:poseidon:wombat --> <!-- cr-indicator-types:potential_issue --> <!-- cr-comment:v1:1f674a18991a232a25f45809 --> <!-- This is an auto-generated comment by CodeRabbit -->
Author
Owner

@⁠coderabbitai[bot] on GitHub · 2026-10-18 01:49 UTC · original · reviewed

Actionable comments posted: 1


ℹ️ Review info
⚙️ Run configuration
  • Configuration used: Repository: KytyPS5/KytyPS5/.coderabbit.yaml
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 310043d7-8ce8-4d2a-ae57-38795c305655
📥 Commits

Reviewing files that changed from the base of the PR and between fe3857a171 and 799a98bdff3ede39d8cdff0318e47eaf5d336a5c.

📒 Files selected for processing: 1
  • src/graphics/host_gpu/renderer/renderCompute.cpp

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

**@⁠coderabbitai[bot]** on GitHub · 2026-10-18 01:49 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/1628#pullrequestreview-5561006650) · **reviewed** **Actionable comments posted: 1** --- <details> <summary>ℹ️ Review info</summary> <details> <summary>⚙️ Run configuration</summary> - **Configuration used**: Repository: KytyPS5/KytyPS5/.coderabbit.yaml - **Review profile**: CHILL - **Plan**: Advanced - **Run ID**: `310043d7-8ce8-4d2a-ae57-38795c305655` </details> <details> <summary>📥 Commits</summary> Reviewing files that changed from the base of the PR and between fe3857a1715551e4f554b2ec29cc1ca3286287a1 and 799a98bdff3ede39d8cdff0318e47eaf5d336a5c. </details> <details> <summary>📒 Files selected for processing: 1</summary> * `src/graphics/host_gpu/renderer/renderCompute.cpp` </details> **Included review availability:** This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review. </details> <!-- This is an auto-generated comment by CodeRabbit for review status --> <!-- coderabbit-review-publication v1 publication=c403d095-6dc3-479b-a937-9f31f4044f26 attempt=7e17e62c-884b-49a7-a896-26947fc802db batch=1/1 -->
Author
Owner

@⁠Nmzik on GitHub · 2026-10-18 03:42 UTC · original

@⁠ysnock404 Thanks

**@⁠Nmzik** on GitHub · 2026-10-18 03:42 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/1628#issuecomment-6245296726) @⁠ysnock404 Thanks
This discussion has been locked. Commenting is limited to contributors.
No description provided.