gpu: stage oversized image readbacks in a private buffer instead of exiting #845

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

@⁠theantipopau on GitHub · 2026-09-26 05:24 UTC · original
theantipopau:image-readback-chunk → main · +25 −5 · 1 files · 3 commits · closed without merging

What

TextureCache::DownloadImageMemory stages whole-image readbacks through the shared 64 MiB download utility buffer and exits when StreamBuffer::Map cannot fit the image's full data range:

TextureCache: failed to map reusable download buffer

Any image whose data range is at or above the buffer capacity (a 64 MiB render target being read back for a save or a present-path copy) hits this. The fix mirrors the staging approach used for buffer downloads (#842) and oversized image uploads (#844): when the shared buffer cannot map the range, stage the readback in a private MemoryUsage::Download buffer of exactly the needed size.

Details

  • The private buffer is created with AllFlags, the same usage flags the shared download buffer carries, so every copy shape DownloadImage emits (linear regions, tiling passes, BGRA16 swap, depth readback) stays legal.
  • It is pre-filled from guest memory before the transfer, so padding bytes between regions — which the shared path preserves by copying over a guest-faithful staging copy — survive into the write-back. This matters because the readback publish writes the whole range.size back, not just the copied regions.
  • The deferred priority operation does the same Invalidate → WriteBacking publish as the shared path and then retires the buffer, so the private allocation lives exactly until the GPU copy has been read on the priority thread.
  • The shared-buffer path is byte-identical to before (Commit() still runs immediately on successful Map); only the nullptr branch changed behavior.

Testing

  • kyty_emulator builds clean with clang-cl/Ninja.
  • Not GPU-tested locally: the AMD driver here lacks VK_EXT_attachment_feedback_loop_dynamic_state, so production rasterization/readback paths cannot run. The oversized branch itself needs a title that reads back such an image; behavior is unchanged for every image that fits the 64 MiB buffer.

Related: #829 / #842 (buffer-download batching) and #844 (oversized image uploads), which together cover the other sides of the same staging ceilings.

**@⁠theantipopau** on GitHub · 2026-09-26 05:24 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/845) `theantipopau:image-readback-chunk` → `main` · +25 −5 · 1 files · 3 commits · closed without merging ## What `TextureCache::DownloadImageMemory` stages whole-image readbacks through the shared 64 MiB download utility buffer and exits when `StreamBuffer::Map` cannot fit the image's full data range: ``` TextureCache: failed to map reusable download buffer ``` Any image whose data range is at or above the buffer capacity (a 64 MiB render target being read back for a save or a present-path copy) hits this. The fix mirrors the staging approach used for buffer downloads (#842) and oversized image uploads (#844): when the shared buffer cannot map the range, stage the readback in a private `MemoryUsage::Download` buffer of exactly the needed size. ## Details - The private buffer is created with `AllFlags`, the same usage flags the shared download buffer carries, so every copy shape `DownloadImage` emits (linear regions, tiling passes, BGRA16 swap, depth readback) stays legal. - It is pre-filled from guest memory before the transfer, so padding bytes between regions — which the shared path preserves by copying over a guest-faithful staging copy — survive into the write-back. This matters because the readback publish writes the whole `range.size` back, not just the copied regions. - The deferred priority operation does the same `Invalidate` → `WriteBacking` publish as the shared path and then retires the buffer, so the private allocation lives exactly until the GPU copy has been read on the priority thread. - The shared-buffer path is byte-identical to before (`Commit()` still runs immediately on successful `Map`); only the `nullptr` branch changed behavior. ## Testing - `kyty_emulator` builds clean with clang-cl/Ninja. - Not GPU-tested locally: the AMD driver here lacks `VK_EXT_attachment_feedback_loop_dynamic_state`, so production rasterization/readback paths cannot run. The oversized branch itself needs a title that reads back such an image; behavior is unchanged for every image that fits the 64 MiB buffer. Related: #829 / #842 (buffer-download batching) and #844 (oversized image uploads), which together cover the other sides of the same staging ceilings.
DownloadImageMemory exits when the whole image data range cannot be
mapped out of the shared 64 MiB download buffer, so a game that reads
back a large image dies. Mirror the shared path with a private download
buffer of exactly the needed size: it is pre-filled from guest memory so
padding bytes the copy does not write survive, the deferred priority
write-back publishes it and then retires the buffer.

🤖 Generated with Codebuff
Co-Authored-By: Codebuff <[email protected]>
Co-Authored-By: Codebuff <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:34:48 +00:00
github-bot closed this pull request 2026-11-26 09:34:48 +00:00
Author
Owner

@⁠coderabbitai[bot] on GitHub · 2026-09-26 05:33 UTC · original · edited 2026-10-06 04:44 UTC

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Repository: KytyPS5/KytyPS5/.coderabbit.yaml
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 01235527-01e2-4190-a4e0-729b2a7d2e97
📥 Commits

Reviewing files that changed from the base of the PR and between b0cb9d6fdf and fcb234d831.

📒 Files selected for processing (1)
  • src/graphics/host_gpu/renderer/cache/textureCache.cpp

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


📝 Walkthrough

Walkthrough

DownloadImageMemory now allocates a private buffer sized to the image when the reusable download buffer cannot map the range. Both buffer paths perform image readback and issue a host-read barrier. The private-buffer path defers buffer invalidation and guest-memory write-back, then releases the buffer. A failed guest-backing read still returns false.

Suggested reviewers: defektu

Priority: ⬇️ Low

Change: Bug fix

Merge Risk: ⚪ Minimal · up to fcb23

No actionable merge-blocking issue was established. The oversized-readback path remains untested on a GPU, so validate it when suitable hardware is available.

Security Architecture Review

Security architecture risk: 🔵 Low · up to fcb23

The inspected paths preserve completion ordering, temporary-resource ownership, and guest-memory unmap synchronization. No introduced security vulnerability was established, but peak memory usage under overlapping large readbacks remains uncertain.

Retained concerns
No architecture-level concerns identified.

Security review details

Security Blast Radius

  • inferred — The evidenced exposure is additional temporary host/GPU memory consumption and publication into the existing image guest range within the emulator process. The fallback supplies no new destination address or independent privileged entrypoint; the maximum guest-driven allocation demand remains unresolved.

Trust Boundaries and Controls

  • observed — The inspected guest unmap route synchronously enters GPU-side unmapping and waits for pending priority publications before removing backing. Pending detection includes active callbacks, and tick-specific waiting covers both queued and active operations. This counters stale publication into recycled backing through KernelMunmap, including when the image registration has already been removed.

Resilience and Maintainability Implications

  • observed — Private allocations request VMA within-budget enforcement, and flush/invalidation operations check per-buffer bounds. Allocation failure remains terminal. That failure behavior does not establish a new denial-of-service attack because the corresponding base branch already terminated immediately.

Hardening Proposals

  • proposed — Consider aggregate staging-byte accounting and admission control for overlapping private readbacks to make resource containment explicit. This is a hardening proposal, not an observed regression or verified exhaustion attack.
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 1 functions across 1 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the main change: oversized image readbacks use a private buffer instead of exiting when the shared buffer cannot map the range.
Description check ✅ Passed The description explains the oversized image readback issue, the private-buffer fallback, its deferred publish behavior, and the reported testing.
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.

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-09-26 05:33 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/845#issuecomment-5843560524) · edited 2026-10-06 04:44 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/845?scope=ghh_OusAJGA4HG8ljoOV0wJvVx8NmnZIjc2DdjmxOu0xZWA&amp;cs_source=review_comment"><img src="https://storage.googleapis.com/coderabbit_public_assets/review-stack-in-coderabbit-ui-dark.svg?v=2" alt="Review in Change Stack →" width="220" height="32"></a> <!-- review_stack_entry_end --> <!-- recent_review_start --> No actionable comments were generated in the recent review. 🎉 <details> <summary>ℹ️ Recent review info</summary> <details> <summary>⚙️ Run configuration</summary> - **Configuration used**: Repository: KytyPS5/KytyPS5/.coderabbit.yaml - **Review profile**: CHILL - **Plan**: Advanced - **Run ID**: `01235527-01e2-4190-a4e0-729b2a7d2e97` </details> <details> <summary>📥 Commits</summary> Reviewing files that changed from the base of the PR and between b0cb9d6fdfe323fdbaa4f72b4906e04803a85c98 and fcb234d831656dbfbae5879a6f75406c060bf423. </details> <details> <summary>📒 Files selected for processing (1)</summary> * `src/graphics/host_gpu/renderer/cache/textureCache.cpp` </details> **Included review availability:** This review used your included allowance. Your plan provides up to 8 included reviews per hour; 2 remain after this review. </details> --- <!-- recent_review_end --> <!-- walkthrough_start --> <details> <summary>📝 Walkthrough</summary> ## Walkthrough `DownloadImageMemory` now allocates a private buffer sized to the image when the reusable download buffer cannot map the range. Both buffer paths perform image readback and issue a host-read barrier. The private-buffer path defers buffer invalidation and guest-memory write-back, then releases the buffer. A failed guest-backing read still returns `false`. **Suggested reviewers:** `defektu` </details> <!-- walkthrough_end --> <!-- change_assessment_start --> **Priority:** ⬇️ Low <!-- change_assessment_commit:"fcb234d831656dbfbae5879a6f75406c060bf423" --> **Change:** Bug fix <!-- change_assessment_end --> <!-- final_review_risk_start --> **Merge Risk:** _⚪ Minimal_ · up to `fcb23` <!-- final_review_risk_coverage:{"sourceCommitId":"fcb234d831656dbfbae5879a6f75406c060bf423","coveredCommitId":"fcb234d831656dbfbae5879a6f75406c060bf423","kind":"reviewed"} --> No actionable merge-blocking issue was established. The oversized-readback path remains untested on a GPU, so validate it when suitable hardware is available. <!-- final_review_risk_end --> <!-- architecture_review_start --> ### Security Architecture Review **Security architecture risk:** _🔵 Low_ · up to `fcb23` The inspected paths preserve completion ordering, temporary-resource ownership, and guest-memory unmap synchronization. No introduced security vulnerability was established, but peak memory usage under overlapping large readbacks remains uncertain. **Retained concerns** No architecture-level concerns identified. <details> <summary>Security review details</summary> **Security Blast Radius** - _inferred_ — The evidenced exposure is additional temporary host/GPU memory consumption and publication into the existing image guest range within the emulator process. The fallback supplies no new destination address or independent privileged entrypoint; the maximum guest-driven allocation demand remains unresolved. **Trust Boundaries and Controls** - _observed_ — The inspected guest unmap route synchronously enters GPU-side unmapping and waits for pending priority publications before removing backing. Pending detection includes active callbacks, and tick-specific waiting covers both queued and active operations. This counters stale publication into recycled backing through KernelMunmap, including when the image registration has already been removed. **Resilience and Maintainability Implications** - _observed_ — Private allocations request VMA within-budget enforcement, and flush/invalidation operations check per-buffer bounds. Allocation failure remains terminal. That failure behavior does not establish a new denial-of-service attack because the corresponding base branch already terminated immediately. **Hardening Proposals** - _proposed_ — Consider aggregate staging-byte accounting and admission control for overlapping private readbacks to make resource containment explicit. This is a hardening proposal, not an observed regression or verified exhaustion attack. </details> <!-- architecture_review_end --> <!-- pre_merge_checks_walkthrough_start --> <details> <summary>🚥 Pre-merge checks | ✅ 4 | ❌ 1</summary> ### ❌ Failed checks (1 warning) | Check name | Status | Explanation | Resolution | | :----------------: | :--------- | :---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | :--------------------------------------------------------------------------------- | | Docstring Coverage | ⚠️ Warning | Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 1 functions across 1 files. | Write docstrings for the functions missing them to satisfy the coverage threshold. | <details> <summary>✅ Passed checks (4 passed)</summary> | Check name | Status | Explanation | | :------------------------: | :------- | :----------------------------------------------------------------------------------------------------------------------------------------------------------- | | Title check | ✅ Passed | The title clearly summarizes the main change: oversized image readbacks use a private buffer instead of exiting when the shared buffer cannot map the range. | | Description check | ✅ Passed | The description explains the oversized image readback issue, the private-buffer fallback, its deferred publish behavior, and the reported testing. | | 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. | </details> </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=845)! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. <details> <summary>❤️ Share</summary> - [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) </details> <sub>Comment `@coderabbitai help` to get the list of available commands.</sub> <!-- tips_end -->
Author
Owner

@⁠theantipopau on GitHub · 2026-09-26 06:11 UTC · original

Game-tested this branch locally: Terminator 2D No Fate (PPSA25872), clang-cl/Ninja Release build of this exact commit.

  • 90 s run: clean boot through Graphics init, shader compile steady state VS 5 | PS 3 | CS 6, no errors, no fatals.
  • 5-minute soak: stable for the full run, same steady state, zero error lines.
  • Boot log matches an upstream/main (fd2e15ee) build, so the shared 64 MiB download-buffer fast path is behavior-neutral for images that fit it.

Same caveat as #844: this title never triggers the oversized readback branch (nothing near 64 MiB is read back whole), so this validates no-regression, not the new path itself.

**@⁠theantipopau** on GitHub · 2026-09-26 06:11 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/845#issuecomment-5843791521) Game-tested this branch locally: **Terminator 2D No Fate (PPSA25872)**, clang-cl/Ninja Release build of this exact commit. - 90 s run: clean boot through Graphics init, shader compile steady state `VS 5 | PS 3 | CS 6`, no errors, no fatals. - 5-minute soak: stable for the full run, same steady state, zero error lines. - Boot log matches an `upstream/main` (`fd2e15ee`) build, so the shared 64 MiB download-buffer fast path is behavior-neutral for images that fit it. Same caveat as #844: this title never triggers the oversized readback branch (nothing near 64 MiB is read back whole), so this validates no-regression, not the new path itself.

Pull request closed

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