perf(agc): sub-allocate sampled texture memory from shared blocks #1484

Closed
Celegans12 wants to merge 1 commit from upstream-pr/1484 into main
Celegans12 commented 2026-10-08 08:57:02 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Celegans12 on 2026-10-08T08:57:02Z.


What

Every sampled texture got its own vkAllocateMemory. On an RTX 3080 Laptop under Windows an allocate plus free costs about 210 µs, and the PPSA26344 world creates about 14,700 textures per frame.

  • ImageMemory keeps per-device blocks of a 64th of the heap, 16 to 256 MiB. APS5_IMAGE_BLOCK_MIB sets the size; 0 gives every texture its own allocation as before.
  • A texture takes the best-fitting free range of any block at its alignment, and freed ranges merge. Blocks hold only optimal-tiling images, so bufferImageGranularity never applies.
  • A texture the driver wants dedicated (VkMemoryDedicatedRequirements), one over half a block, and one whose new block is refused for memory get their own allocation. After a refusal, no block of that type is tried again until a block's worth of its memory has been freed.
  • Empty blocks are freed, except one spare per memory type. APS5_PROFILE_DRAW adds an [imagemem] line.
  • #290 edits the same lines; whichever lands second routes ImageMemory's own allocations through #290's AllocateGpuMemory and frees its spare blocks in RelieveGpuMemory.
  • #1059 budgets the sampled cache by the device memory sampled textures hold; whichever lands second adds the blocks' free bytes to that memory, or the budget sinks as blocks empty.
Details

Microbenchmark on the RTX 3080 Laptop, 4,000 BC textures of 64 to 1024 texels with mips: bind plus release 215-223 µs per texture before, 9-12 µs after. 100,000 replacements among 8,000 live textures (2.2 GiB): 353 µs each before, 3.9 µs after, blocks 87% used. lavapipe: 6.4 µs before, 0.7 µs after.

Tested

  • Windows 11, MinGW-w64 GCC 15.2.0, based on main 4c380ae3: configure -DBUILD_TESTING=ON, full build, --target libs, ctest:
  • 407/407 tests pass (ctest --output-on-failure)
  • Same build on lavapipe (Mesa 26.2.4 llvmpipe, 8-wide subgroups, VK_DRIVER_FILES): 406/406 pass; agc_driver_flat_store is left out there because it takes several minutes on Windows llvmpipe
  • New agc_image_memory (mocked device): packing at each alignment with no overlap, reuse and merging of freed ranges, the spare block, dedicated and large textures, refused blocks and binds, the back-off after a refusal, concurrent binds. Does not build on main.
  • PPSA26344 v1.008 (3,600 s): in the world sampled textures came from 24-27 blocks of 252 MiB holding 4.9-5.1 GiB in 11,700-14,500 images (33 blocks allocated, 9 freed, 0 refused); allocate plus image creation took 266-279 µs per texture while the host paged about 3,200 pages/s (196 µs in an earlier run, 264 µs without blocks); no vkAllocateMemory failure.
  • Not tested: Linux (CI), 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:4784329483 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1484). Originally posted by [Celegans12](https://github.com/Celegans12) on 2026-10-08T08:57:02Z. --- ### What Every sampled texture got its own `vkAllocateMemory`. On an RTX 3080 Laptop under Windows an allocate plus free costs about 210 µs, and the PPSA26344 world creates about 14,700 textures per frame. - `ImageMemory` keeps per-device blocks of a 64th of the heap, 16 to 256 MiB. `APS5_IMAGE_BLOCK_MIB` sets the size; 0 gives every texture its own allocation as before. - A texture takes the best-fitting free range of any block at its alignment, and freed ranges merge. Blocks hold only optimal-tiling images, so `bufferImageGranularity` never applies. - A texture the driver wants dedicated (`VkMemoryDedicatedRequirements`), one over half a block, and one whose new block is refused for memory get their own allocation. After a refusal, no block of that type is tried again until a block's worth of its memory has been freed. - Empty blocks are freed, except one spare per memory type. `APS5_PROFILE_DRAW` adds an `[imagemem]` line. - #290 edits the same lines; whichever lands second routes `ImageMemory`'s own allocations through #290's `AllocateGpuMemory` and frees its spare blocks in `RelieveGpuMemory`. - #1059 budgets the sampled cache by the device memory sampled textures hold; whichever lands second adds the blocks' free bytes to that memory, or the budget sinks as blocks empty. <details><summary>Details</summary> Microbenchmark on the RTX 3080 Laptop, 4,000 BC textures of 64 to 1024 texels with mips: bind plus release 215-223 µs per texture before, 9-12 µs after. 100,000 replacements among 8,000 live textures (2.2 GiB): 353 µs each before, 3.9 µs after, blocks 87% used. lavapipe: 6.4 µs before, 0.7 µs after. </details> ### Tested - Windows 11, MinGW-w64 GCC 15.2.0, based on `main` 4c380ae3: configure `-DBUILD_TESTING=ON`, full build, `--target libs`, `ctest`: - 407/407 tests pass (`ctest --output-on-failure`) - Same build on lavapipe (Mesa 26.2.4 llvmpipe, 8-wide subgroups, `VK_DRIVER_FILES`): 406/406 pass; `agc_driver_flat_store` is left out there because it takes several minutes on Windows llvmpipe - New `agc_image_memory` (mocked device): packing at each alignment with no overlap, reuse and merging of freed ranges, the spare block, dedicated and large textures, refused blocks and binds, the back-off after a refusal, concurrent binds. Does not build on `main`. - PPSA26344 v1.008 (3,600 s): in the world sampled textures came from 24-27 blocks of 252 MiB holding 4.9-5.1 GiB in 11,700-14,500 images (33 blocks allocated, 9 freed, 0 refused); allocate plus image creation took 266-279 µs per texture while the host paged about 3,200 pages/s (196 µs in an earlier run, 264 µs without blocks); no `vkAllocateMemory` failure. - Not tested: Linux (CI), 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
EdouardSence commented 2026-10-08 13:18:11 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by EdouardSence on 2026-10-08T13:18:11Z.


Tested on AMD under Linux, since this PR wasn't: RX 9070 XT (RDNA4, GFX1201), RADV / Mesa 26.2.4, Linux 7.2.9 (CachyOS), GCC Release build.

  • ctest: this PR merged onto main 07c2405c, all 453 tests pass with ctest -j8 (main alone: 452/452 on the same machine).
  • Astro Bot (PPSA21567, retail): built as main + #759 (needed to boot this title on main) + this PR, then one scripted run headless from boot through the title screen. No error and no amdgpu fault came from this change.
    • Presents per 10 s on the title screen: 235, 227, 227; with main + #759 alone: 226, 229, 226.
    • The run then stops, with and without this PR, on storage image access to depth/stencil surface ... is not implemented (the gap #425 fills). So nothing past the title screen is covered.

AI-assisted: the runs and this write-up were done by Claude (Claude Code) on my machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6060746987 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1484#issuecomment-6060746987). Originally posted by [EdouardSence](https://github.com/EdouardSence) on 2026-10-08T13:18:11Z. --- Tested on AMD under Linux, since this PR wasn't: RX 9070 XT (RDNA4, GFX1201), RADV / Mesa 26.2.4, Linux 7.2.9 (CachyOS), GCC Release build. - **`ctest`**: this PR merged onto `main` 07c2405c, all 453 tests pass with `ctest -j8` (main alone: 452/452 on the same machine). - **Astro Bot (PPSA21567, retail)**: built as `main` + #759 (needed to boot this title on `main`) + this PR, then one scripted run headless from boot through the title screen. No error and no amdgpu fault came from this change. - Presents per 10 s on the title screen: 235, 227, 227; with `main` + #759 alone: 226, 229, 226. - The run then stops, with and without this PR, on `storage image access to depth/stencil surface ... is not implemented` (the gap #425 fills). So nothing past the title screen is covered. AI-assisted: the runs and this write-up were done by Claude (Claude Code) on my machine.
fisherjoey commented 2026-10-08 19:01:45 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by fisherjoey on 2026-10-08T19:01:45Z.


Linux NVIDIA run with a title that creates a lot of textures.

Demon's Souls (PPSA01341), tutorial level, Nobara 44 (Linux 7.2), RTX 5070 Ti (595.91.07), Ryzen 5 7600X. Our stack is main df16c4c2 plus open PRs we run (#1201, #1202, #1184) and local fixes, with and without this PR. 2 runs per arm, interleaved, same start point, level window t+150 to t+240 s:

presents / 10 s (per run) queue-0 Graphics::Draw ms in the window textures created in the window [imagemem] at the end
stack 21.0, 20.0 1904, 1769 0, 0 (838 cached, 5474 MiB) n/a
stack + this PR 20.5, 20.0 1788, 1821 0, 0 (838 cached, 5474 MiB) 7 blocks (1778 MiB), 1643 MiB in 496 images; 7 images in their own allocations (1063 MiB); blocks allocated 7, freed 0, refused 0 and 8 blocks (2032 MiB), 1922 MiB in 577 images; 7 images in their own allocations (1063 MiB); blocks allocated 8, freed 0, refused 0

Neutral here: about 2.0 fps either way, and no new errors or rejected allocations. In this part of the level our stack no longer creates textures in steady state (an earlier build churned through about 800 per 10 s), so the allocate and free cost this PR removes doesn't show up at the frame level. The blocks fill as expected: most textures land in shared blocks, 7 large ones get their own allocation, and nothing was refused or freed.

The cherry-pick applied cleanly. One local change of ours that allocates its own image memory needed the same ImageMemory::Bind change the PR makes in the snapshot constructor, so other open PRs that allocate texture memory directly may need it too.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6067000242 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1484#issuecomment-6067000242). Originally posted by [fisherjoey](https://github.com/fisherjoey) on 2026-10-08T19:01:45Z. --- Linux NVIDIA run with a title that creates a lot of textures. Demon's Souls (PPSA01341), tutorial level, Nobara 44 (Linux 7.2), RTX 5070 Ti (595.91.07), Ryzen 5 7600X. Our stack is `main` df16c4c2 plus open PRs we run (#1201, #1202, #1184) and local fixes, with and without this PR. 2 runs per arm, interleaved, same start point, level window t+150 to t+240 s: | | presents / 10 s (per run) | queue-0 `Graphics::Draw` ms in the window | textures created in the window | `[imagemem]` at the end | |---|---|---|---|---| | stack | 21.0, 20.0 | 1904, 1769 | 0, 0 (838 cached, 5474 MiB) | n/a | | stack + this PR | 20.5, 20.0 | 1788, 1821 | 0, 0 (838 cached, 5474 MiB) | `7 blocks (1778 MiB), 1643 MiB in 496 images; 7 images in their own allocations (1063 MiB); blocks allocated 7, freed 0, refused 0` and `8 blocks (2032 MiB), 1922 MiB in 577 images; 7 images in their own allocations (1063 MiB); blocks allocated 8, freed 0, refused 0` | Neutral here: about 2.0 fps either way, and no new errors or rejected allocations. In this part of the level our stack no longer creates textures in steady state (an earlier build churned through about 800 per 10 s), so the allocate and free cost this PR removes doesn't show up at the frame level. The blocks fill as expected: most textures land in shared blocks, 7 large ones get their own allocation, and nothing was refused or freed. The cherry-pick applied cleanly. One local change of ours that allocates its own image memory needed the same `ImageMemory::Bind` change the PR makes in the snapshot constructor, so other open PRs that allocate texture memory directly may need it too.
Celegans12 commented 2026-10-08 19:22:01 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Celegans12 on 2026-10-08T19:22:01Z.


Closing this. Thanks to fisherjoey for the Demon's Souls run: neither that nor Ghost of Yōtei shows a frame-level gain from it. Once the texture cache stops churning, the allocate and free cost it removes is too small to see, and our last Yōtei run measured no per-texture improvement either, so it isn't worth the review time.

If someone picks the idea up later: any other code that allocates image memory directly would need the same ImageMemory::Bind change as the snapshot constructor, as fisherjoey found with a local patch.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6067356860 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1484#issuecomment-6067356860). Originally posted by [Celegans12](https://github.com/Celegans12) on 2026-10-08T19:22:01Z. --- Closing this. Thanks to fisherjoey for the Demon's Souls run: neither that nor Ghost of Yōtei shows a frame-level gain from it. Once the texture cache stops churning, the allocate and free cost it removes is too small to see, and our last Yōtei run measured no per-texture improvement either, so it isn't worth the review time. If someone picks the idea up later: any other code that allocates image memory directly would need the same `ImageMemory::Bind` change as the snapshot constructor, as fisherjoey found with a local patch.

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!1484
No description provided.