perf(shader): resolve image tables with fewer allocations #18827

Merged
atinseau merged 6 commits from upstream-pr/18827 into main 2026-11-14 04:46:55 +00:00
atinseau commented 2026-11-12 11:45:56 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by atinseau on 2026-11-12T11:45:56Z.


What

Resolving an image or sampler table (ResolveImageTable, the shared and empty-result paths of the resource materializer) allocates more than its result needs. This PR removes four sources of allocations and of contention, each with a test on the production seam.

  • The binding metadata arrays (image and sampler tables, with the spilled element flags) are reserved before known-size tables are appended. agc_shader_table_binding_growth fails without it (17 allocations at 64 elements) and checks changed descriptors and metadata at 0, 1, 64, 65, 256 and 3000 elements. A 256-image fixture goes from 29 to 7 allocations and from 3652 to 2639 ns per resolution.
  • The map, element and poison arrays of a table of distinct descriptors are reserved too, instead of growing one doubling at a time. agc_shader_table_resolution_growth resolves 1 to 1024 distinct elements and expects a fixed number of output arrays; without the change it counts 18 allocations for 64 elements.
  • Each resolving thread keeps its own immutable empty-result owner. #9177's global owner made every thread bump one reference count: with eight threads on one empty table, 99.10 ns per call against 1.97 ns with a per-thread owner (7 alternating runs, amortized throughput); with one thread, 6.38 against 10.45 ns. agc_shader_empty_table_resolution runs four threads and checks allocation reuse, the refusal of malformed indices and that a result outlives the thread that produced it.
  • Sampler reduction flags stay inline (in the object) for tables of up to 64 images and are built by a sized constructor, so a cache hit of a shared table resolution allocates nothing: one-image hits go from 50.3 to 16.5 ns and 32-image hits from 71.9 to 36.9 ns, one allocation to none. Tables of 65 images and more keep the heap fallback and their cost (95.1 against 95.5 ns at 65, 157.8 against 154.4 ns at 129). agc_shader_shared_table_resolution failed first with 100 allocations on 100 hits, and checks unchanged-result reuse, changed sampler and descriptor invalidation, held-result lifetime and malformed index refusal at 1, 32, 64, 65 and 129 images.

Field layouts and serialized formats are unchanged; the sized constructor is additive. The timings are fixture measurements (single process, alternating runs of the parent and the change), not a title trace.

Tested

macOS 27.0.1, Apple M3 Max (36 GB), MoltenVK 1.4.2 from Vulkan SDK 1.4.363.0, Release build with -DBUILD_TESTING=ON, x86_64 under Rosetta 2. main does not run its libraries on macOS, so both sides are main 3b7f31fa7 with the macOS port (the squash of #8658, which its author upstreams in parts) and local macOS build fixes for tests of main that Apple clang refuses; the change side adds this PR's commits.

  • The four new tests pass with the change.
  • Focused AGC, shader recompiler and VideoOut set, 811 tests, on both sides with VK_LAYER_KHRONOS_validation, VK_LOADER_DEBUG=layer, ANYPS5_REQUIRE_VULKAN=1 and ANYPS5_NO_SHADER_CACHE=1 (ctest --test-dir build-macos -R '<811 test names>' -V): on the change 708 pass (the four new tests added), 71 skip, 35 fail and 1 times out; all 811 tests have the same status as on main. The failures are the ones main has on macOS (MoltenVK limits and macOS-only gaps of the tests); none is new. Both runs report the same 77 VUID- errors test by test, and was not found but was requested by env var VK_INSTANCE_LAYERS appears only in the 12 lines that print the failure regex of the agc_driver_image_table_packed* tests. The change run's logs show the loader inserting the layer (Insert instance layer) in 490 tests.
  • python3 tools/check_conventions.py --base origin/main passes. tools/pr_overlap.py could not compare open pull requests that conflict with main and share DescriptorBindingBuilder.cpp, ResourceMaterializer.cpp, Recompiler.hpp or the shader memory tests; they are image table features (for example #7642, #10193, #13928, #13929), and none reserves, isolates or inlines what this PR does.

Not tested: Linux and Windows.

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: none
  • GPUs tested: Apple M3 Max, MoltenVK 1.4.2 (Vulkan SDK 1.4.363.0), macOS 27.0.1
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5276787315 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/18827). Originally posted by [atinseau](https://github.com/atinseau) on 2026-11-12T11:45:56Z. --- ### What Resolving an image or sampler table (`ResolveImageTable`, the shared and empty-result paths of the resource materializer) allocates more than its result needs. This PR removes four sources of allocations and of contention, each with a test on the production seam. - The binding metadata arrays (image and sampler tables, with the spilled element flags) are reserved before known-size tables are appended. `agc_shader_table_binding_growth` fails without it (17 allocations at 64 elements) and checks changed descriptors and metadata at 0, 1, 64, 65, 256 and 3000 elements. A 256-image fixture goes from 29 to 7 allocations and from 3652 to 2639 ns per resolution. - The map, element and poison arrays of a table of distinct descriptors are reserved too, instead of growing one doubling at a time. `agc_shader_table_resolution_growth` resolves 1 to 1024 distinct elements and expects a fixed number of output arrays; without the change it counts 18 allocations for 64 elements. - Each resolving thread keeps its own immutable empty-result owner. #9177's global owner made every thread bump one reference count: with eight threads on one empty table, 99.10 ns per call against 1.97 ns with a per-thread owner (7 alternating runs, amortized throughput); with one thread, 6.38 against 10.45 ns. `agc_shader_empty_table_resolution` runs four threads and checks allocation reuse, the refusal of malformed indices and that a result outlives the thread that produced it. - Sampler reduction flags stay inline (in the object) for tables of up to 64 images and are built by a sized constructor, so a cache hit of a shared table resolution allocates nothing: one-image hits go from 50.3 to 16.5 ns and 32-image hits from 71.9 to 36.9 ns, one allocation to none. Tables of 65 images and more keep the heap fallback and their cost (95.1 against 95.5 ns at 65, 157.8 against 154.4 ns at 129). `agc_shader_shared_table_resolution` failed first with 100 allocations on 100 hits, and checks unchanged-result reuse, changed sampler and descriptor invalidation, held-result lifetime and malformed index refusal at 1, 32, 64, 65 and 129 images. Field layouts and serialized formats are unchanged; the sized constructor is additive. The timings are fixture measurements (single process, alternating runs of the parent and the change), not a title trace. ### Tested macOS 27.0.1, Apple M3 Max (36 GB), MoltenVK 1.4.2 from Vulkan SDK 1.4.363.0, Release build with `-DBUILD_TESTING=ON`, x86_64 under Rosetta 2. `main` does not run its libraries on macOS, so both sides are `main` `3b7f31fa7` with the macOS port (the squash of #8658, which its author upstreams in parts) and local macOS build fixes for tests of `main` that Apple clang refuses; the change side adds this PR's commits. - The four new tests pass with the change. - Focused AGC, shader recompiler and VideoOut set, 811 tests, on both sides with `VK_LAYER_KHRONOS_validation`, `VK_LOADER_DEBUG=layer`, `ANYPS5_REQUIRE_VULKAN=1` and `ANYPS5_NO_SHADER_CACHE=1` (`ctest --test-dir build-macos -R '<811 test names>' -V`): on the change 708 pass (the four new tests added), 71 skip, 35 fail and 1 times out; all 811 tests have the same status as on `main`. The failures are the ones `main` has on macOS (MoltenVK limits and macOS-only gaps of the tests); none is new. Both runs report the same 77 `VUID-` errors test by test, and `was not found but was requested by env var VK_INSTANCE_LAYERS` appears only in the 12 lines that print the failure regex of the `agc_driver_image_table_packed*` tests. The change run's logs show the loader inserting the layer (`Insert instance layer`) in 490 tests. - `python3 tools/check_conventions.py --base origin/main` passes. `tools/pr_overlap.py` could not compare open pull requests that conflict with `main` and share `DescriptorBindingBuilder.cpp`, `ResourceMaterializer.cpp`, `Recompiler.hpp` or the shader memory tests; they are image table features (for example #7642, #10193, #13928, #13929), and none reserves, isolates or inlines what this PR does. Not tested: Linux and Windows. ### 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: none - [x] GPUs tested: Apple M3 Max, MoltenVK 1.4.2 (Vulkan SDK 1.4.363.0), macOS 27.0.1 - [x] AI-assisted: yes
MarcZX10 commented 2026-11-12 14:29:29 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-12T14:29:29Z.

Review: COMMENTED


Reviewed — no blocking findings

The diff reserves allocations in image and sampler table resolution, gives each thread its own empty-result owner, and adds four tests. I found no defect in the code I read, and nothing was built or run.

What I ran

  • Head: 5d42e94887ba54b64b144ec5d6571e3c1f4ca4af, base main.
  • Read the review folder (description.md, commits.md, manifest.json, diff.patch) and the house-rule sections of the ruling file.
  • Reader calls at the head:
repo_read.py cat core/shader/recompiler/Recompiler.hpp 590 680
repo_read.py cat core/shader/recompiler/Optimization/src/ResourceMaterializer.cpp 1950 1985
repo_read.py cat core/shader/recompiler/Optimization/src/ResourceMaterializer.cpp 2212 2240
repo_read.py search ElementFlags core/shader
repo_read.py search allocationCount core/libs/prx/libSceAgcDriver/tests/ShaderMemory.cpp
repo_read.py search ImageElementBudget core/shader
  • No build, no test run, no conventions script run.

Results

Check Result Note
Diff has no code comments passed Read from diff.patch
ElementFlags equality and at bounds passed Read at head; inline bits stay zero past count
Four new tests registered in CMake passed Read from the diff
Build and unit tests not run No build in this review
Author-reported test run (811 tests, macOS) not run Not confirmed here
tools/check_conventions.py not run Author-reported

Findings

Blocking

None.

Non-blocking

  • core/shader/recompiler/Optimization/src/ResourceMaterializer.cpp:1887: static_cast<void>(reducing.at(pair.image)); followed by reducing[pair.image] = true; is a bounds check whose purpose is not stated. It keeps the std::out_of_range behaviour of the old std::vector<bool>::at. A short comment-free rewrite is not obvious, so the intent is easy to miss. Leave as is unless the house style has a pattern for it.
  • core/libs/prx/libSceAgcDriver/tests/ShaderMemory.cpp:4817: allocations <= (count <= 64u ? 0u : 100u) allows one allocation per cache hit at 65 and 129 images. This matches the heap fallback the description names, but the bound would not catch a regression to per-hit allocation for those sizes.

Not verified

  • Benchmark figures in the description (99.10 vs 1.97 ns, 6.38 vs 10.45 ns, 50.3 to 16.5 ns, 29 to 7 allocations, 18 allocations without the change). The tests check bounds, not these numbers.
  • Whether any caller compares result pointers across threads. ResourceMaterializer.cpp:2215 now returns a per-thread object, so the same empty result has a different address on each thread. Callers were not searched.
  • Compilation of the new test code (verifyImageTableBindingGrowth, verifyEmptyImageTableResolution, and the other helpers) against the headers.
  • The 811-test macOS run in the description.

Files

  • Read: ResourceMaterializer.cpp (diff hunks and ranges 1950–1985 and 2212–2240), Recompiler.hpp (diff hunks and 590–680), DescriptorBindingBuilder.cpp (diff hunks), agc_shader_memory_tests.cmake (diff), ShaderDiskCache.cpp (diff), ShaderMemory.cpp (diff hunks only; the file is excluded as too large).
  • Not read in full: all of the above except the ranges named.

To get this to a good state

  • Nothing blocks this PR on the code read. Before merge, the author should run the four new tests on the change and confirm the numbers in the description.
<!-- greyfinch-sync:boykopovar/AnyPS5:review:5876152944 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/18827#pullrequestreview-5876152944). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-12T14:29:29Z. **Review:** COMMENTED --- # Reviewed — no blocking findings The diff reserves allocations in image and sampler table resolution, gives each thread its own empty-result owner, and adds four tests. I found no defect in the code I read, and nothing was built or run. ## What I ran - Head: `5d42e94887ba54b64b144ec5d6571e3c1f4ca4af`, base `main`. - Read the review folder (`description.md`, `commits.md`, `manifest.json`, `diff.patch`) and the house-rule sections of the ruling file. - Reader calls at the head: ``` repo_read.py cat core/shader/recompiler/Recompiler.hpp 590 680 repo_read.py cat core/shader/recompiler/Optimization/src/ResourceMaterializer.cpp 1950 1985 repo_read.py cat core/shader/recompiler/Optimization/src/ResourceMaterializer.cpp 2212 2240 repo_read.py search ElementFlags core/shader repo_read.py search allocationCount core/libs/prx/libSceAgcDriver/tests/ShaderMemory.cpp repo_read.py search ImageElementBudget core/shader ``` - No build, no test run, no conventions script run. ## Results | Check | Result | Note | |---|---|---| | Diff has no code comments | passed | Read from `diff.patch` | | `ElementFlags` equality and `at` bounds | passed | Read at head; inline bits stay zero past `count` | | Four new tests registered in CMake | passed | Read from the diff | | Build and unit tests | not run | No build in this review | | Author-reported test run (811 tests, macOS) | not run | Not confirmed here | | `tools/check_conventions.py` | not run | Author-reported | ## Findings **Blocking** None. **Non-blocking** - `core/shader/recompiler/Optimization/src/ResourceMaterializer.cpp:1887`: `static_cast<void>(reducing.at(pair.image));` followed by `reducing[pair.image] = true;` is a bounds check whose purpose is not stated. It keeps the `std::out_of_range` behaviour of the old `std::vector<bool>::at`. A short comment-free rewrite is not obvious, so the intent is easy to miss. Leave as is unless the house style has a pattern for it. - `core/libs/prx/libSceAgcDriver/tests/ShaderMemory.cpp:4817`: `allocations <= (count <= 64u ? 0u : 100u)` allows one allocation per cache hit at 65 and 129 images. This matches the heap fallback the description names, but the bound would not catch a regression to per-hit allocation for those sizes. ## Not verified - Benchmark figures in the description (99.10 vs 1.97 ns, 6.38 vs 10.45 ns, 50.3 to 16.5 ns, 29 to 7 allocations, 18 allocations without the change). The tests check bounds, not these numbers. - Whether any caller compares result pointers across threads. `ResourceMaterializer.cpp:2215` now returns a per-thread object, so the same empty result has a different address on each thread. Callers were not searched. - Compilation of the new test code (`verifyImageTableBindingGrowth`, `verifyEmptyImageTableResolution`, and the other helpers) against the headers. - The 811-test macOS run in the description. ## Files - Read: `ResourceMaterializer.cpp` (diff hunks and ranges 1950–1985 and 2212–2240), `Recompiler.hpp` (diff hunks and 590–680), `DescriptorBindingBuilder.cpp` (diff hunks), `agc_shader_memory_tests.cmake` (diff), `ShaderDiskCache.cpp` (diff), `ShaderMemory.cpp` (diff hunks only; the file is excluded as too large). - Not read in full: all of the above except the ranges named. ## To get this to a good state - Nothing blocks this PR on the code read. Before merge, the author should run the four new tests on the change and confirm the numbers in the description.
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!18827
No description provided.