perf(agc): keep a full sampled texture cache from refusing textures over entries unused for seconds #28914

Open
milescoviello wants to merge 1 commit from upstream-pr/28914 into main
milescoviello commented 2026-12-04 16:23:48 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-12-04T16:23:48Z.


What

When the sampled texture cache is over its budget, admitTexture (ShaderResources.cpp) refuses a new texture while the least recently used entry was used in the current frame (for the texture's first use, in the current frame or the last one). The refused texture has already been built from guest memory; it serves that one lookup and is dropped, so its next lookup builds it again.

The rule counts frames by presents. When presents are minutes apart (a long load, or a scene that takes minutes per frame on the host), "used in this frame" covers minutes, and the cache keeps entries that have not been used for minutes while the textures in use are rebuilt at every lookup.

This adds a time limit to the refusal: the least recently used entry keeps its place only if it was also used within the last 10 s (APS5_TEXTURE_RECENT_USE_MS; 0 restores the old rule).

  • Each cache entry keeps the time of its last use (used), set where present is set: on a hit (touchTexture) and when the entry is inserted.
  • At frame times under 10 s nothing changes, since an entry used in the current or last frame was used within that time.
  • Nothing else changes: the over-budget ceiling, held entries passed over, eviction order and the second-use rule stay as they are.

Tested

  • Unit tests, Linux (Ubuntu 26.04.1, GCC 15.2, RTX 4070 Ti), at c10c3779bf: the 24 tests matching agc_driver_sampled_cache|agc_driver_resource_cache|agc_driver_recover_|agc_sampled_cache (including the new agc_driver_sampled_cache_stale_frame) pass in each of 3 runs, none skipped.
  • Control: with the time limit taken out of the condition and the new test kept, agc_driver_sampled_cache_stale_frame fails at "a texture first used in the same frame 400 ms later, when the least recently used entry had not been used for longer than APS5_TEXTURE_RECENT_USE_MS (200), was not cached".
  • New agc_driver_sampled_cache_stale_frame (SampledCacheBudget.cpp stale-frame, with APS5_TEXTURE_RECENT_USE_MS=200): a frame fills the cache; a texture first used right after is refused, as before; 400 ms later, still in the same frame, a new texture is cached in place of the entry unused for 400 ms, while the entry used again just before stays.

PPSA38500, Ubuntu 26.04.1, RTX 4070 Ti 12 GB (NVIDIA 615.71.09), on my local stack (main de4730493a plus about 40 open pull requests), with the cache forced to 6500 MiB (APS5_TEXTURE_CACHE_MIB=6500), APS5_PROFILE_DRAW=1, one boot of 3600 s per side. The rule was measured as a local switch with the same condition (refuse only if the entry was used within 10000 ms), not with this branch.

  • Why: without the limit, the cache refused 913,046 lookups in the hour. At each refusal, the least recently used entry had last been used:

    under 1 s 1-10 s 10-60 s 1-5 min over 5 min
    0 127 27,973 327,411 557,535

    Per frame, only 1,000 to 3,700 distinct textures were refused, each rebuilt about 250 times.

  • With the limit, per present after the first 1200 s (about 3 presents per boot, so these are rough):

    without with
    Resources.Complete 504 s 147 s
    Driver.Dispatch 1054 s 379 s
    frame time (p50) 1318 s 309 s
    textures used uncached 913,046 in 3600 s 539,294 in 2832 s
  • Not settled: the boot with the limit ended at 2832 s on a SIGSEGV inside the NVIDIA driver's vkAllocateMemory (called from BufferPool::allocateMemoryWithReuse). At that point device-local memory was full, so texture allocations were going to host memory, and the process was at its 32 GiB memory cap. The boot without the limit had neither. One boot per side cannot say whether the limit makes that more likely. More boots per side are running, and this stays a draft until they are in.

Related: #12575 keeps a refused texture for as long as frames keep using it; this pull request changes when a texture is refused. Both change code around admitTexture; neither changes the other's rule. #28620 changes only the notice sent for a refused texture.

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 and the licence notice of code adapted from upstream (see AGENTS.md)
  • 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: none
  • GPUs tested: RTX 4070 Ti, NVIDIA 615.71.09, Ubuntu 26.04.1
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5650176300 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/28914). Originally posted by [milescoviello](https://github.com/milescoviello) on 2026-12-04T16:23:48Z. --- ### What When the sampled texture cache is over its budget, `admitTexture` (`ShaderResources.cpp`) refuses a new texture while the least recently used entry was used in the current frame (for the texture's first use, in the current frame or the last one). The refused texture has already been built from guest memory; it serves that one lookup and is dropped, so its next lookup builds it again. The rule counts frames by presents. When presents are minutes apart (a long load, or a scene that takes minutes per frame on the host), "used in this frame" covers minutes, and the cache keeps entries that have not been used for minutes while the textures in use are rebuilt at every lookup. This adds a time limit to the refusal: the least recently used entry keeps its place only if it was also used within the last 10 s (`APS5_TEXTURE_RECENT_USE_MS`; `0` restores the old rule). - Each cache entry keeps the time of its last use (`used`), set where `present` is set: on a hit (`touchTexture`) and when the entry is inserted. - At frame times under 10 s nothing changes, since an entry used in the current or last frame was used within that time. - Nothing else changes: the over-budget ceiling, held entries passed over, eviction order and the second-use rule stay as they are. ### Tested - Unit tests, Linux (Ubuntu 26.04.1, GCC 15.2, RTX 4070 Ti), at `c10c3779bf`: the 24 tests matching `agc_driver_sampled_cache|agc_driver_resource_cache|agc_driver_recover_|agc_sampled_cache` (including the new `agc_driver_sampled_cache_stale_frame`) pass in each of 3 runs, none skipped. - Control: with the time limit taken out of the condition and the new test kept, `agc_driver_sampled_cache_stale_frame` fails at "a texture first used in the same frame 400 ms later, when the least recently used entry had not been used for longer than APS5_TEXTURE_RECENT_USE_MS (200), was not cached". - New `agc_driver_sampled_cache_stale_frame` (`SampledCacheBudget.cpp stale-frame`, with `APS5_TEXTURE_RECENT_USE_MS=200`): a frame fills the cache; a texture first used right after is refused, as before; 400 ms later, still in the same frame, a new texture is cached in place of the entry unused for 400 ms, while the entry used again just before stays. PPSA38500, Ubuntu 26.04.1, RTX 4070 Ti 12 GB (NVIDIA 615.71.09), on my local stack (`main` `de4730493a` plus about 40 open pull requests), with the cache forced to 6500 MiB (`APS5_TEXTURE_CACHE_MIB=6500`), `APS5_PROFILE_DRAW=1`, one boot of 3600 s per side. The rule was measured as a local switch with the same condition (refuse only if the entry was used within 10000 ms), not with this branch. - **Why:** without the limit, the cache refused 913,046 lookups in the hour. At each refusal, the least recently used entry had last been used: | under 1 s | 1-10 s | 10-60 s | 1-5 min | over 5 min | |---|---|---|---|---| | 0 | 127 | 27,973 | 327,411 | 557,535 | Per frame, only 1,000 to 3,700 distinct textures were refused, each rebuilt about 250 times. - **With the limit, per present after the first 1200 s** (about 3 presents per boot, so these are rough): | | without | with | |---|---|---| | `Resources.Complete` | 504 s | 147 s | | `Driver.Dispatch` | 1054 s | 379 s | | frame time (p50) | 1318 s | 309 s | | textures used uncached | 913,046 in 3600 s | 539,294 in 2832 s | - **Not settled:** the boot with the limit ended at 2832 s on a SIGSEGV inside the NVIDIA driver's `vkAllocateMemory` (called from `BufferPool::allocateMemoryWithReuse`). At that point device-local memory was full, so texture allocations were going to host memory, and the process was at its 32 GiB memory cap. The boot without the limit had neither. One boot per side cannot say whether the limit makes that more likely. More boots per side are running, and this stays a draft until they are in. Related: #12575 keeps a refused texture for as long as frames keep using it; this pull request changes when a texture is refused. Both change code around `admitTexture`; neither changes the other's rule. #28620 changes only the notice sent for a refused texture. ### 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) and the licence notice of code adapted from upstream (see AGENTS.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: none - [x] GPUs tested: RTX 4070 Ti, NVIDIA 615.71.09, Ubuntu 26.04.1 - [x] AI-assisted: yes
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/28914:upstream-pr/28914'
git switch 'upstream-pr/28914'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/28914'
git switch 'upstream-pr/28914'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/28914'
git switch 'upstream-pr/28914'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/28914'
git switch 'main'
git merge --squash 'upstream-pr/28914'
git switch 'main'
git merge --ff-only 'upstream-pr/28914'
git switch 'main'
git merge 'upstream-pr/28914'
git push origin 'main'
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!28914
No description provided.