fix(agc): count storage images in the recorder's kept bytes #6030

Closed
tv7 wants to merge 1 commit from upstream-pr/6030 into main
tv7 commented 2026-10-14 19:39:23 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by tv7 on 2026-10-14T19:39:23Z.


What

On an 8 GB card, Marvel's Wolverine (PPSA03671) runs out of device memory a few minutes after it starts presenting, and aborts in presentation with vkAllocateMemory storage texture: Vulkan result -2 (#5961). The storage-image cache stays at its 2 GiB budget, but the title cycles about 5.4 GiB of storage surfaces through it, so the cache evicts and makes images again many times per batch. Evicted images then stay alive, up to 1,873 images (8.3 GiB) at once in the measurements in #5961, and none of them were held by sampled views.

They are held by recorded work, and the recorder does not see their size:

  • cachedStorageTexture keeps a new storage image in the open batch with Recorder::Keep(entry.texture), so its size never reaches keptBytes.
  • An evicted image that recorded work still holds is not counted anywhere.

So Recorder::BoundKeptBytes (512 MiB per open batch, twice that in flight) never fires for them, and batches keep gigabytes of images alive until they complete.

This change adds each image's size to the open batch's kept bytes with KeepBytes, which counts each image once per batch:

  • the new image, when the cache makes it inside a batch;
  • an evicted image whose use_count() shows that something besides the eviction still holds it.

The existing bound then submits and retires batches before they hold that much. Nothing else changes: the cache budget, eviction order and the keep itself are the same.

Tested

Windows 11, RTX 3070 8 GB (driver 617.14), GCC 15.2.0 UCRT, Release.

  • agc_driver_storage_cache_budget has a new check: a dispatch that makes a 1 MiB storage image leaves at least that much in its open batch's kept bytes. On main it fails with a dispatch that made a 1 MiB storage image left 0 bytes in its batch's kept bytes, and with the change it passes.
  • ctest 1221/1221 on main 9f03c4fe0, and tools/check_conventions.py passes.
  • Wolverine, relinked with --to-intel, 900 s per run:
    • Main e34c47921: every run without the change aborted a few minutes in with the error above (plain, or with local logging only), with heap 0 usage up to 13 GiB against a budget of about 7 GiB.
    • Main 9811e9d4b plus this change: ran the full 900 s without aborting. 296 storage-texture allocations still failed with -2 and kept the snapshot path, which is not fatal, and heap 0 usage peaked at 8.7 GiB.
    • An earlier local variant that counted a new image a second time when it was evicted in the same batch had no failed allocations in its run. I kept the exact count here.
  • What this does not change: the cache still evicts and remakes the same surfaces many times when the working set is larger than its budget. That is a separate question from #5961.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt
  • No notes, investigation .md files or images added to the repository
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • AI-assisted: yes, Claude Code

Related: #5961 (this addresses the device-memory abort; the eviction churn remains).

🤖 Generated with Claude Code

https://claude.ai/code/session_01TGkh749FbEgqQsma2ix7V6

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4863474305 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6030). Originally posted by [tv7](https://github.com/tv7) on 2026-10-14T19:39:23Z. --- ### What On an 8 GB card, Marvel's Wolverine (PPSA03671) runs out of device memory a few minutes after it starts presenting, and aborts in presentation with `vkAllocateMemory storage texture: Vulkan result -2` (#5961). The storage-image cache stays at its 2 GiB budget, but the title cycles about 5.4 GiB of storage surfaces through it, so the cache evicts and makes images again many times per batch. Evicted images then stay alive, up to 1,873 images (8.3 GiB) at once in the measurements in #5961, and none of them were held by sampled views. They are held by recorded work, and the recorder does not see their size: - `cachedStorageTexture` keeps a new storage image in the open batch with `Recorder::Keep(entry.texture)`, so its size never reaches `keptBytes`. - An evicted image that recorded work still holds is not counted anywhere. So `Recorder::BoundKeptBytes` (512 MiB per open batch, twice that in flight) never fires for them, and batches keep gigabytes of images alive until they complete. This change adds each image's size to the open batch's kept bytes with `KeepBytes`, which counts each image once per batch: - the new image, when the cache makes it inside a batch; - an evicted image whose `use_count()` shows that something besides the eviction still holds it. The existing bound then submits and retires batches before they hold that much. Nothing else changes: the cache budget, eviction order and the keep itself are the same. ### Tested Windows 11, RTX 3070 8 GB (driver 617.14), GCC 15.2.0 UCRT, Release. - `agc_driver_storage_cache_budget` has a new check: a dispatch that makes a 1 MiB storage image leaves at least that much in its open batch's kept bytes. On main it fails with `a dispatch that made a 1 MiB storage image left 0 bytes in its batch's kept bytes`, and with the change it passes. - ctest 1221/1221 on main `9f03c4fe0`, and `tools/check_conventions.py` passes. - Wolverine, relinked with `--to-intel`, 900 s per run: - Main `e34c47921`: every run without the change aborted a few minutes in with the error above (plain, or with local logging only), with heap 0 usage up to 13 GiB against a budget of about 7 GiB. - Main `9811e9d4b` plus this change: ran the full 900 s without aborting. 296 storage-texture allocations still failed with -2 and kept the snapshot path, which is not fatal, and heap 0 usage peaked at 8.7 GiB. - An earlier local variant that counted a new image a second time when it was evicted in the same batch had no failed allocations in its run. I kept the exact count here. - What this does not change: the cache still evicts and remakes the same surfaces many times when the working set is larger than its budget. That is a separate question from #5961. ### Checklist - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except technical debt - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed in TechnicalDebt - [x] No notes, investigation `.md` files or images added to the repository - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] AI-assisted: yes, Claude Code Related: #5961 (this addresses the device-memory abort; the eviction churn remains). 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_01TGkh749FbEgqQsma2ix7V6
MarcZX10 commented 2026-10-14 19:40:50 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-14T19:40:50Z.

Review: COMMENTED


One behavior concern: KeepBytes runs when the new image is created, while the storage cache still owns it. If this reaches the kept-byte limit, submitting or retiring the batch cannot free that cached image, so cache churn can add backpressure for memory that is not yet releasable. That was the concern raised on #5998.

Could the accounting happen when the cache evicts an image that the recorded batch still holds? The image.use_count() > 1 check in the eviction loop appears to cover the case where no shared ShaderResources entry references it. A regression test that records an image, evicts it by exceeding the cache budget, and then checks OpenKeptBytes() would exercise the retained-evicted lifetime directly; the current test checks the image while it is still cached.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5523650507 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6030#pullrequestreview-5523650507). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-14T19:40:50Z. **Review:** COMMENTED --- One behavior concern: `KeepBytes` runs when the new image is created, while the storage cache still owns it. If this reaches the kept-byte limit, submitting or retiring the batch cannot free that cached image, so cache churn can add backpressure for memory that is not yet releasable. That was the concern raised on #5998. Could the accounting happen when the cache evicts an image that the recorded batch still holds? The `image.use_count() > 1` check in the eviction loop appears to cover the case where no shared `ShaderResources` entry references it. A regression test that records an image, evicts it by exceeding the cache budget, and then checks `OpenKeptBytes()` would exercise the retained-evicted lifetime directly; the current test checks the image while it is still cached.
github-actions[bot] commented 2026-10-14 19:43:22 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-14T19:43:22Z.


Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.

Pull request Conflicting files (lines on main) Same exports Same new files
#5998 core/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cpp L932
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6176850305 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6030#issuecomment-6176850305). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-14T19:43:22Z. --- <!-- pr-overlap --> Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in **Depends on**. | Pull request | Conflicting files (lines on `main`) | Same exports | Same new files | |---|---|---|---| | #5998 | `core/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cpp` [L932](https://github.com/boykopovar/AnyPS5/blob/c26982f631cd04f56f79dc18d58cce42eefaa43e/core/libs/prx/libSceAgcDriver/Graphics/src/ShaderResources.cpp#L932) | | |
tv7 commented 2026-10-14 19:45:53 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by tv7 on 2026-10-14T19:45:53Z.


Closing as a duplicate of #5998 by @MarcZX100, which was opened first and covers more holders. I'll run Wolverine on #5998 and post the results there. If the extra check in agc_driver_storage_cache_budget from this branch is useful (a dispatch that makes a storage image must leave its size in the open batch's kept bytes; on main it fails with left 0 bytes), feel free to take it.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6176899795 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6030#issuecomment-6176899795). Originally posted by [tv7](https://github.com/tv7) on 2026-10-14T19:45:53Z. --- Closing as a duplicate of #5998 by @MarcZX100, which was opened first and covers more holders. I'll run Wolverine on #5998 and post the results there. If the extra check in `agc_driver_storage_cache_budget` from this branch is useful (a dispatch that makes a storage image must leave its size in the open batch's kept bytes; on main it fails with `left 0 bytes`), feel free to take it.

Pull request closed

Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!6030
No description provided.