perf(agc): keep sampled textures that are bound or used this frame #1971

Merged
Celegans12 merged 1 commit from upstream-pr/1971 into main 2026-10-10 05:28:51 +00:00
Celegans12 commented 2026-10-09 03:26:49 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Celegans12 on 2026-10-09T03:26:49Z.


What

The sampled texture cache evicts its least recently used entry even when a build, a recorded batch or a cached resource set still holds that texture, which frees no device memory and makes the next lookup create a second copy. A frame that samples more textures than the cache holds also misses on all of them every frame: least recently used order evicts each texture just before the next frame asks for it again. In PPSA26344 every texture of a 16,386-entry image table is made again each frame.

  • Eviction passes over an entry whose texture is held elsewhere, or a view of a cached storage image (0 bytes), up to a quarter over the budget; the pass rescans about once a second for released textures.
  • While the least recently used entry was used in this frame or the previous one (Recorder::Presents), a texture's first use in the frame is returned uncached, and a second use in the same frame evicts as before. So a miss early in a frame does not evict what the rest of the frame needs, and a new set of textures replaces the old one on its second frame. Before the first present the order is plain LRU.
  • For a frame set of N textures over a cache of C, the next frame finds C of them instead of none.
  • APS5_PROFILE_DRAW adds a [texture-cache] line (cached, used uncached, evicted, passed over, found changed).
  • #1865 (sampled texture keys) conflicts with #1059 in ShaderResources, not with this commit.

Tested

  • Windows 11, MinGW-w64 GCC 15.2.0, based on main 6b56175a with #1059: configure -DBUILD_TESTING=ON, full build, --target libs, ctest:
  • 476/476 tests pass (ctest --output-on-failure)
  • Linux (WSL Ubuntu 24.04, GCC 13.3, Mesa llvmpipe, upstream's job configuration), merged with main f1451396: 500/500 pass
  • agc_driver_sampled_cache_budget extended: held textures are not made twice below the ceiling, released ones behind held ones leave a second later, a view survives snapshot pressure; a set two larger than the cache keeps the cache's share in the next frame, a second use is cached, a new set replaces the old on its second frame, and a new texture first used ahead of the cached ones leaves all of them cached. Fails on #1059.
  • PPSA26344 v1.008 (3,600 s, with #1059): in the world the sampled cache holds about 5 GiB in 15,500 textures and per frame passes over 363 held ones, uses 2,315 uncached on first use and evicts 277; a frame makes about 2,900 textures (16,600 before) and the 16,386-entry table resolves in 1.3-1.5 s instead of 5.0 s.
  • Not tested: console behaviour.

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 (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
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4795593977 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1971). Originally posted by [Celegans12](https://github.com/Celegans12) on 2026-10-09T03:26:49Z. --- ### What The sampled texture cache evicts its least recently used entry even when a build, a recorded batch or a cached resource set still holds that texture, which frees no device memory and makes the next lookup create a second copy. A frame that samples more textures than the cache holds also misses on all of them every frame: least recently used order evicts each texture just before the next frame asks for it again. In PPSA26344 every texture of a 16,386-entry image table is made again each frame. - Eviction passes over an entry whose texture is held elsewhere, or a view of a cached storage image (0 bytes), up to a quarter over the budget; the pass rescans about once a second for released textures. - While the least recently used entry was used in this frame or the previous one (`Recorder::Presents`), a texture's first use in the frame is returned uncached, and a second use in the same frame evicts as before. So a miss early in a frame does not evict what the rest of the frame needs, and a new set of textures replaces the old one on its second frame. Before the first present the order is plain LRU. - For a frame set of N textures over a cache of C, the next frame finds C of them instead of none. - `APS5_PROFILE_DRAW` adds a `[texture-cache]` line (cached, used uncached, evicted, passed over, found changed). - #1865 (sampled texture keys) conflicts with #1059 in `ShaderResources`, not with this commit. ### Tested - Windows 11, MinGW-w64 GCC 15.2.0, based on `main` 6b56175a with #1059: configure `-DBUILD_TESTING=ON`, full build, `--target libs`, `ctest`: - 476/476 tests pass (`ctest --output-on-failure`) - Linux (WSL Ubuntu 24.04, GCC 13.3, Mesa llvmpipe, upstream's job configuration), merged with `main` f1451396: 500/500 pass - `agc_driver_sampled_cache_budget` extended: held textures are not made twice below the ceiling, released ones behind held ones leave a second later, a view survives snapshot pressure; a set two larger than the cache keeps the cache's share in the next frame, a second use is cached, a new set replaces the old on its second frame, and a new texture first used ahead of the cached ones leaves all of them cached. Fails on #1059. - PPSA26344 v1.008 (3,600 s, with #1059): in the world the sampled cache holds about 5 GiB in 15,500 textures and per frame passes over 363 held ones, uses 2,315 uncached on first use and evicts 277; a frame makes about 2,900 textures (16,600 before) and the 16,386-entry table resolves in 1.3-1.5 s instead of 5.0 s. - Not tested: console behaviour. ### 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](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [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] AI-assisted: yes
MarcZX10 commented 2026-10-09 03:39:26 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-09T03:39:26Z.

Review: COMMENTED


Could firstUses store TextureKey values with TextureKeyHash instead of only size_t hashes? Two distinct descriptors can collide in TextureKeyHash; after one first-use miss, the collision makes the other descriptor look like it already missed this present, so it can bypass the recent-entry guard and evict a texture used this or last present on its first use. The cache index already has the full key equality available.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5465467614 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1971#pullrequestreview-5465467614). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-09T03:39:26Z. **Review:** COMMENTED --- Could `firstUses` store `TextureKey` values with `TextureKeyHash` instead of only `size_t` hashes? Two distinct descriptors can collide in `TextureKeyHash`; after one first-use miss, the collision makes the other descriptor look like it already missed this present, so it can bypass the recent-entry guard and evict a texture used this or last present on its first use. The cache index already has the full key equality available.
fisherjoey commented 2026-10-09 04:52:33 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by fisherjoey on 2026-10-09T04:52:33Z.


Linux results, since this was tested on Windows only. Everything passes here.

Nobara 44 (Linux 7.2), GCC 16.2.1, Release, BUILD_TESTING=ON. This PR merged onto main f1451396 cleanly.

main f1451396 main + this PR
RTX 5070 Ti, driver 595.91.07, ctest -R agc_driver -j1 207 / 207 210 / 210
RADV, Ryzen 5 7600X iGPU (gfx1036), Mesa 26.2.1, same 207 / 207 210 / 210
CPU tests (-E agc_driver) 286 / 287 286 / 287

The CPU failure is guest_dynamic_loader, which fails the same way on main.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6074515841 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1971#issuecomment-6074515841). Originally posted by [fisherjoey](https://github.com/fisherjoey) on 2026-10-09T04:52:33Z. --- Linux results, since this was tested on Windows only. Everything passes here. Nobara 44 (Linux 7.2), GCC 16.2.1, Release, `BUILD_TESTING=ON`. This PR merged onto `main` f1451396 cleanly. | | `main` f1451396 | `main` + this PR | |---|---|---| | RTX 5070 Ti, driver 595.91.07, `ctest -R agc_driver -j1` | 207 / 207 | 210 / 210 | | RADV, Ryzen 5 7600X iGPU (gfx1036), Mesa 26.2.1, same | 207 / 207 | 210 / 210 | | CPU tests (`-E agc_driver`) | 286 / 287 | 286 / 287 | The CPU failure is `guest_dynamic_loader`, which fails the same way on `main`.
Celegans12 commented 2026-10-09 05:18:32 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Celegans12 on 2026-10-09T05:18:32Z.


Thanks, you're right. A hash match was all it took to count as "already missed this present". So a second descriptor whose hash happened to equal the first one's was treated as a second use: it skipped the guard and evicted a texture the last frame had used.

firstUses is now a std::unordered_set<TextureKey, TextureKeyHash>, so it compares full keys the same way the cache index does. The per-present reset hasn't changed.

To test it I built a real collision instead of injecting a fake hash. agc_driver_sampled_cache_budget has a new case that makes two descriptors of the same surface. They differ only in word 6's statistics counter and the meta address bits, which the decoder ignores when META_COMPRESS is off, and word 7 is picked so that both keys get the same TextureKeyHash. The test fills the cache in one frame. In the next frame it uses both descriptors for the first time and checks that every cached texture is still there. On the old head (31c70367) it fails with 3 of the 4 textures left; with the change it passes. One caveat: to find the pair, the test repeats the FNV-1a steps of TextureKeyHash, so if the hash ever changes, the test needs the same change. If you'd rather have a test that injects a colliding hash, I can switch it.

The new head is e3180e13. Windows NVIDIA ctest is 476/476, and upstream's Linux job on the merge with main f1451396 is 500/500.

Details
  • Failure on 31c70367 (agc_driver_sampled_cache_budget): the first use of a descriptor whose cache key hash equals that of another descriptor first used in the frame left 3 of the 4 textures the last frame used cached
  • The pair: the base descriptor's words 0-5 are unchanged. The search tries word 6 = (top byte << 24) | bit 0 over 512 candidates and takes the first two that give the same upper 32 bits of the hash state after word 6. The second descriptor's word 7 is the XOR of the two states' lower halves, so the states after word 7 are equal. In a simulation over 20,000 random device handles, every run found at least 41 such pairs.
  • The merged tree (e3180e13 + main f1451396) builds libs and passes agc_driver_sampled_cache_budget, agc_driver_sampled_cache_budget_memory, agc_driver_storage_cache_budget and agc_driver_graphics.
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6074807625 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1971#issuecomment-6074807625). Originally posted by [Celegans12](https://github.com/Celegans12) on 2026-10-09T05:18:32Z. --- Thanks, you're right. A hash match was all it took to count as "already missed this present". So a second descriptor whose hash happened to equal the first one's was treated as a second use: it skipped the guard and evicted a texture the last frame had used. `firstUses` is now a `std::unordered_set<TextureKey, TextureKeyHash>`, so it compares full keys the same way the cache index does. The per-present reset hasn't changed. To test it I built a real collision instead of injecting a fake hash. `agc_driver_sampled_cache_budget` has a new case that makes two descriptors of the same surface. They differ only in word 6's statistics counter and the meta address bits, which the decoder ignores when META_COMPRESS is off, and word 7 is picked so that both keys get the same `TextureKeyHash`. The test fills the cache in one frame. In the next frame it uses both descriptors for the first time and checks that every cached texture is still there. On the old head (31c70367) it fails with 3 of the 4 textures left; with the change it passes. One caveat: to find the pair, the test repeats the FNV-1a steps of `TextureKeyHash`, so if the hash ever changes, the test needs the same change. If you'd rather have a test that injects a colliding hash, I can switch it. The new head is e3180e13. Windows NVIDIA ctest is 476/476, and upstream's Linux job on the merge with `main` f1451396 is 500/500. <details><summary>Details</summary> - Failure on 31c70367 (`agc_driver_sampled_cache_budget`): `the first use of a descriptor whose cache key hash equals that of another descriptor first used in the frame left 3 of the 4 textures the last frame used cached` - The pair: the base descriptor's words 0-5 are unchanged. The search tries word 6 = (top byte << 24) | bit 0 over 512 candidates and takes the first two that give the same upper 32 bits of the hash state after word 6. The second descriptor's word 7 is the XOR of the two states' lower halves, so the states after word 7 are equal. In a simulation over 20,000 random device handles, every run found at least 41 such pairs. - The merged tree (e3180e13 + `main` f1451396) builds `libs` and passes `agc_driver_sampled_cache_budget`, `agc_driver_sampled_cache_budget_memory`, `agc_driver_storage_cache_budget` and `agc_driver_graphics`. </details>
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!1971
No description provided.