fix(agc): release texture cache entries before a budget-full allocation goes to host memory #13064

Merged
milescoviello merged 1 commit from upstream-pr/13064 into main 2026-10-30 09:19:51 +00:00
milescoviello commented 2026-10-30 00:06:19 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-30T00:06:19Z.


What

Context::DeviceMemoryType picks the memory type for textures, storage textures, device buffers, resident heap mirror slabs and the presentation scaler's images. When VK_EXT_memory_budget reports the device-local heap without room for the allocation, it places the allocation in a host memory type up front. A driver refusal (VK_ERROR_OUT_OF_DEVICE_MEMORY) gets a recovery first: the buffer pool trims, the texture caches release what they can, and the allocation is retried. The budget path skipped that recovery, so a full heap sent every new storage texture to host memory while the sampled texture cache still held reclaimable entries.

Ghost of Tsushima (PPSA02225) on a 12 GB RTX 4070 Ti hits this in its opening. The device-local heap sits at its budget (10,511 of 10,511 MiB), and [gpu] storage texture: device-local heap 0 uses ... of its ... MiB budget, ... placed in host memory climbs to 512 allocations and 7,098 MiB, with no texture caches released line in the whole run. On Linux NVIDIA that host memory is pinned and outside the title's cgroup, so the run ran the host out of memory 423 s in.

How

When the budget has no room and a host type is allowed, DeviceMemoryType calls the context's cacheReclaim (ReleaseTextureCaches, as an image allocation) for the allocation's size plus what the heap is already over its budget:

  • If the caches release that much, the allocation keeps the device-local type. If the driver then refuses it, the existing refusal path still applies.
  • If they release less, the budget is read again, and the allocation keeps the device-local type when it now fits.
  • Otherwise it goes to host memory as before, with the same [gpu] ... placed in host memory line.

Without cacheReclaim (DeviceMemoryFallback's mock contexts, and any context that doesn't set it) nothing changes. docs/user/USAGE.md describes the step next to the refusal recovery.

Tested

Linux (Ubuntu 26.04.1, GCC 15.2), RTX 4070 Ti (NVIDIA 615.71.09). Tests ran at c7d9fc8fa5, this change on main dc38224141. The head here, e2c1c305ff, is the same commit rebased onto main 438cebf361; the PR files didn't change.

  • At this head e2c1c305ff:

    • The 9 tests below pass 3 of 3.
    • The validation-layer command below, main 438cebf361 vs e2c1c305ff: 8 of 8 pass on each side, with no VUID- messages and no missing-layer message. The sampled-cache fixture created 4 Vulkan devices on each side.
    • The merge preview with main 1a217758a5 is clean.
  • At c7d9fc8fa5:

    • New cases in agc_device_memory_fallback, with a mock cacheReclaim that records what it was asked for and can lower the reported usage:
      • With room in the budget, the caches aren't asked.
      • The caches release the whole allocation: device-local type, asked for the allocation's 64 MiB as a new image.
      • Usage over the budget: asked for the allocation plus the overage (745 MiB), device-local type.
      • A partial release that the budget read again shows: device-local type.
      • A partial release the budget doesn't show, and a release of nothing: host type, as before.
    • Control: the new test file alone on main dc38224141 (73e3f30de7) fails agc_device_memory_fallback: caches release the whole allocation: chose memory type 0, expected 1, and the caches weren't asked.
    • ^agc_device_memory_fallback$|^agc_buffer_pool|^agc_driver_sampled_cache|^agc_sampled_cache (9 tests) pass 3 of 3.
    • Validation layer (Khronos 1.4.363, VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 ctest -R '^agc_buffer_pool|^agc_driver_sampled_cache|^agc_sampled_cache' -V), main dc38224141 vs c7d9fc8fa5:
      • 8 of 8 pass on each side.
      • There are no VUID- messages on either side, and no missing-layer message.
      • The sampled-cache fixture created 4 Vulkan devices on each side.
  • Ghost of Tsushima (PPSA02225 02.024.000), 30-minute runs on my GoT test stack. These are supplementary: they aren't a matched pair, and run 48 also has GPU-heap and other local fixes.

    run this change budget-path host placements texture caches released lines result
    47 no 512 allocations, 7,098 MiB 0 host memory ran out at 423 s
    48 yes 4,096 allocations, 408 MiB 16,651 ran the full 1,807 s, lowest host available 4.0 GB

    The driver still refused device-local allocations in run 48, and the refusal path placed 20,770 MiB in host memory over the run (cumulative; those allocations are released and made again).

  • Windows 11 VM (GeForce GT 1030, WinLibs GCC 15.2, Release): the same 9 tests pass 3 of 3 at c7d9fc8fa5 and on main dc38224141.

#12786 (mine, open) changes the refusal-path recovery to keep the current present's storage images when the allocation can go to host memory. This path calls the recovery the way main's refusal path does today. With #12786, this call should keep those images too; whichever lands second can add that.

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 under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: PPSA02225 #4461 (Ghost of Tsushima, host memory exhaustion in its opening)
  • GPUs tested: NVIDIA GeForce RTX 4070 Ti (Linux, 615.71.09)
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5077369861 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/13064). Originally posted by [milescoviello](https://github.com/milescoviello) on 2026-10-30T00:06:19Z. --- ### What `Context::DeviceMemoryType` picks the memory type for textures, storage textures, device buffers, resident heap mirror slabs and the presentation scaler's images. When `VK_EXT_memory_budget` reports the device-local heap without room for the allocation, it places the allocation in a host memory type up front. A driver refusal (`VK_ERROR_OUT_OF_DEVICE_MEMORY`) gets a recovery first: the buffer pool trims, the texture caches release what they can, and the allocation is retried. The budget path skipped that recovery, so a full heap sent every new storage texture to host memory while the sampled texture cache still held reclaimable entries. Ghost of Tsushima (PPSA02225) on a 12 GB RTX 4070 Ti hits this in its opening. The device-local heap sits at its budget (10,511 of 10,511 MiB), and `[gpu] storage texture: device-local heap 0 uses ... of its ... MiB budget, ... placed in host memory` climbs to 512 allocations and 7,098 MiB, with no `texture caches released` line in the whole run. On Linux NVIDIA that host memory is pinned and outside the title's cgroup, so the run ran the host out of memory 423 s in. ### How When the budget has no room and a host type is allowed, `DeviceMemoryType` calls the context's `cacheReclaim` (`ReleaseTextureCaches`, as an image allocation) for the allocation's size plus what the heap is already over its budget: - If the caches release that much, the allocation keeps the device-local type. If the driver then refuses it, the existing refusal path still applies. - If they release less, the budget is read again, and the allocation keeps the device-local type when it now fits. - Otherwise it goes to host memory as before, with the same `[gpu] ... placed in host memory` line. Without `cacheReclaim` (`DeviceMemoryFallback`'s mock contexts, and any context that doesn't set it) nothing changes. `docs/user/USAGE.md` describes the step next to the refusal recovery. ### Tested Linux (Ubuntu 26.04.1, GCC 15.2), RTX 4070 Ti (NVIDIA 615.71.09). Tests ran at `c7d9fc8fa5`, this change on `main` `dc38224141`. The head here, `e2c1c305ff`, is the same commit rebased onto `main` `438cebf361`; the PR files didn't change. - **At this head `e2c1c305ff`:** - The 9 tests below pass 3 of 3. - The validation-layer command below, `main` `438cebf361` vs `e2c1c305ff`: 8 of 8 pass on each side, with no `VUID-` messages and no missing-layer message. The sampled-cache fixture created 4 Vulkan devices on each side. - The merge preview with `main` `1a217758a5` is clean. - **At `c7d9fc8fa5`:** - **New cases in `agc_device_memory_fallback`**, with a mock `cacheReclaim` that records what it was asked for and can lower the reported usage: - With room in the budget, the caches aren't asked. - The caches release the whole allocation: device-local type, asked for the allocation's 64 MiB as a new image. - Usage over the budget: asked for the allocation plus the overage (745 MiB), device-local type. - A partial release that the budget read again shows: device-local type. - A partial release the budget doesn't show, and a release of nothing: host type, as before. - **Control:** the new test file alone on `main` `dc38224141` (`73e3f30de7`) fails `agc_device_memory_fallback`: `caches release the whole allocation: chose memory type 0, expected 1`, and the caches weren't asked. - `^agc_device_memory_fallback$|^agc_buffer_pool|^agc_driver_sampled_cache|^agc_sampled_cache` (9 tests) pass 3 of 3. - **Validation layer** (Khronos 1.4.363, `VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 ctest -R '^agc_buffer_pool|^agc_driver_sampled_cache|^agc_sampled_cache' -V`), `main` `dc38224141` vs `c7d9fc8fa5`: - 8 of 8 pass on each side. - There are no `VUID-` messages on either side, and no missing-layer message. - The sampled-cache fixture created 4 Vulkan devices on each side. - **Ghost of Tsushima (PPSA02225 02.024.000)**, 30-minute runs on my GoT test stack. These are supplementary: they aren't a matched pair, and run 48 also has GPU-heap and other local fixes. | run | this change | budget-path host placements | `texture caches released` lines | result | |---|---|---|---|---| | 47 | no | 512 allocations, 7,098 MiB | 0 | host memory ran out at 423 s | | 48 | yes | 4,096 allocations, 408 MiB | 16,651 | ran the full 1,807 s, lowest host available 4.0 GB | The driver still refused device-local allocations in run 48, and the refusal path placed 20,770 MiB in host memory over the run (cumulative; those allocations are released and made again). - Windows 11 VM (GeForce GT 1030, WinLibs GCC 15.2, Release): the same 9 tests pass 3 of 3 at `c7d9fc8fa5` and on `main` `dc38224141`. #12786 (mine, open) changes the refusal-path recovery to keep the current present's storage images when the allocation can go to host memory. This path calls the recovery the way `main`'s refusal path does today. With #12786, this call should keep those images too; whichever lands second can add that. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [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](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Fixes: none - [x] Unblocks: PPSA02225 #4461 (Ghost of Tsushima, host memory exhaustion in its opening) - [x] GPUs tested: NVIDIA GeForce RTX 4070 Ti (Linux, 615.71.09) - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
MarcZX10 commented 2026-10-30 00:12:28 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-30T00:12:28Z.

Review: COMMENTED


Re-checked — new head, GPU validation pending

The current head is source-reviewable and merges cleanly, but the available GPU tests were run on an earlier commit and the author says an exact-head rerun is pending.

What I ran

Reviewed exact head e2c1c305ff5b03074a56ee7def28a639af862a82 against current main 39fc186c978aa6332538fb1101d1722124979ab5; the merge preview was clean at b1b8d5409e947d7079514b84dc25c39b4b5682e2. Environment: Debian Linux x86-64, Python 3.11.2; no compiler or build was run.

git merge-tree --write-tree upstream/main refs/review/pr-13064-current
git diff --check upstream/main...refs/review/pr-13064-current
python3 tools/check_conventions.py --base upstream/main --head refs/review/pr-13064-current

Results

Check Result
Merge preview Clean
Diff check Passed; no output
Conventions Passed
Hosted CI Eligibility, conventions, intake, and report pass; Windows is queued; Linux and specialized build jobs are skipped
Source review The budget path attempts cache reclaim before choosing host memory, then refreshes budget data after partial release. I checked all DeviceMemoryType call sites and the cache reclaim callback's lock/reentrancy guards.

The PR description reports the focused fallback, buffer-pool, and cache tests plus validation-layer runs on c7d9fc8fa5, with a rerun on this rebased head still queued. I could not independently confirm that earlier commit's test source against the current head.

Not verified

I did not build or run the GPU tests or a game locally. Exact-head Linux/GPU test results, Windows validation, and physical-console behavior are not yet available to me. The reported Ghost of Tsushima comparison is explicitly not a matched pair and includes other fixes, so I treated it as supplementary rather than causal proof.

To get this to a good state

Please add the queued focused-test results for this exact head. I will re-check the same source and integration points when the results or head change.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5706586921 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/13064#pullrequestreview-5706586921). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-30T00:12:28Z. **Review:** COMMENTED --- # Re-checked — new head, GPU validation pending The current head is source-reviewable and merges cleanly, but the available GPU tests were run on an earlier commit and the author says an exact-head rerun is pending. ## What I ran Reviewed exact head `e2c1c305ff5b03074a56ee7def28a639af862a82` against current `main` `39fc186c978aa6332538fb1101d1722124979ab5`; the merge preview was clean at `b1b8d5409e947d7079514b84dc25c39b4b5682e2`. Environment: Debian Linux x86-64, Python 3.11.2; no compiler or build was run. ```sh git merge-tree --write-tree upstream/main refs/review/pr-13064-current git diff --check upstream/main...refs/review/pr-13064-current python3 tools/check_conventions.py --base upstream/main --head refs/review/pr-13064-current ``` ## Results | Check | Result | |---|---| | Merge preview | Clean | | Diff check | Passed; no output | | Conventions | Passed | | Hosted CI | Eligibility, conventions, intake, and report pass; Windows is queued; Linux and specialized build jobs are skipped | | Source review | The budget path attempts cache reclaim before choosing host memory, then refreshes budget data after partial release. I checked all `DeviceMemoryType` call sites and the cache reclaim callback's lock/reentrancy guards. | The PR description reports the focused fallback, buffer-pool, and cache tests plus validation-layer runs on `c7d9fc8fa5`, with a rerun on this rebased head still queued. I could not independently confirm that earlier commit's test source against the current head. ## Not verified I did not build or run the GPU tests or a game locally. Exact-head Linux/GPU test results, Windows validation, and physical-console behavior are not yet available to me. The reported Ghost of Tsushima comparison is explicitly not a matched pair and includes other fixes, so I treated it as supplementary rather than causal proof. ## To get this to a good state Please add the queued focused-test results for this exact head. I will re-check the same source and integration points when the results or head change.
MarcZX10 commented 2026-10-30 01:48:08 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-30T01:48:08Z.

Review: APPROVED


Reviewed — looks good

The exact-head source review and the author-reported Linux GPU results support this change; no blocking issue remains.

What I ran

Reviewed head e2c1c305ff5b03074a56ee7def28a639af862a82 against PR base 39fc186c978aa6332538fb1101d1722124979ab5; no local build or test was run. The hosted Windows workflow passed its job, but its product build and test-selection/test-run steps were skipped.

Results

Check Result
Source and test diff Reviewed at the exact head; no blocking finding
Author-reported Linux RTX 4070 Ti tests 9 focused tests passed 3/3; validation-layer comparison passed 8/8 on both main and PR
Hosted checks Conventions, eligibility, intake, labels and report passed
Hosted Windows Job passed; product build and tests skipped
Linux and specialized hosted jobs Skipped

Not verified

I did not independently run the GPU tests, build the project, or test console behavior. The focused GPU and validation results above are author-reported.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5707177157 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/13064#pullrequestreview-5707177157). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-30T01:48:08Z. **Review:** APPROVED --- # Reviewed — looks good The exact-head source review and the author-reported Linux GPU results support this change; no blocking issue remains. ## What I ran Reviewed head `e2c1c305ff5b03074a56ee7def28a639af862a82` against PR base `39fc186c978aa6332538fb1101d1722124979ab5`; no local build or test was run. The hosted Windows workflow passed its job, but its product build and test-selection/test-run steps were skipped. ## Results | Check | Result | |---|---| | Source and test diff | Reviewed at the exact head; no blocking finding | | Author-reported Linux RTX 4070 Ti tests | 9 focused tests passed 3/3; validation-layer comparison passed 8/8 on both main and PR | | Hosted checks | Conventions, eligibility, intake, labels and report passed | | Hosted Windows | Job passed; product build and tests skipped | | Linux and specialized hosted jobs | Skipped | ## Not verified I did not independently run the GPU tests, build the project, or test console behavior. The focused GPU and validation results above are author-reported.
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!13064
No description provided.