Skip to content

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

Merged
oneandonlydean merged 6 commits into
boykopovar:mainfrom
atinseau:codex/perf-image-table-resolution
Nov 14, 2026
Merged

oneandonlydean merged 6 commits into
boykopovar:mainfrom
atinseau:codex/perf-image-table-resolution

Conversation

@atinseau

Copy link
Copy Markdown
Contributor

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. perf(agc): drop three per-draw allocations on the draw and materialize paths #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.

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

@github-actions github-actions Bot added perf Performance change perf numbers missing perf pull request without a before and after summary line labels Nov 12, 2026
@github-actions

Copy link
Copy Markdown

This pull request is labeled perf, so it needs the summary line of a capture on main and one with the change, from the same title, scene and machine, as the Performance report form asks. Please paste both aps5-summary.jsonl lines, attach the two traces and add the output of tools/trace_digest.py.

@JJenkx

JJenkx commented Nov 12, 2026

Copy link
Copy Markdown
Contributor

Windows check of head 5d42e94887. Setup: Windows 11 Enterprise 22621, AMD RX 6700 XT, AMD driver 26.10.1, MinGW-w64 GCC 15.2 UCRT, Release.

The 38 agc_shader_* tests that cover the changed files, including agc_shader_image_limits_mixed, agc_shader_image_limits_refusals, agc_shader_image_limits_table, agc_shader_empty_table_resolution and agc_shader_bindless:

  • GPU, through the shared Windows GPU FIFO with ANYPS5_REQUIRE_VULKAN=1, merged onto main 719fc1c05d (merge 4c3eefa079): all 38 passed in 3 of 3 runs, 114 of 114 outcomes. The binaries matched the hashes recorded at preparation, and the receipt shows exit 0 and app_count=0.
  • CPU, Vulkan hidden, merged onto main 44270d9910 (merge cfb2e6297a): all 38 pass in 3 of 3 runs.

This is a correctness check only; I did not measure the allocation or timing change.

AI-assisted: yes.

@github-actions

github-actions Bot commented Nov 12, 2026 •

Copy link
Copy Markdown

Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.

Pull request Shared files not compared
PR 5315 core/libs/prx/libSceAgcDriver/tests/ShaderDiskCache.cpp
PR 5371 core/libs/prx/libSceAgcDriver/tests/ShaderMemory.cpp
core/shader/recompiler/Optimization/src/DescriptorBindingBuilder.cpp
core/shader/recompiler/Optimization/src/ResourceMaterializer.cpp
PR 5609 core/libs/prx/libSceAgcDriver/tests/ShaderDiskCache.cpp
core/shader/recompiler/Optimization/src/DescriptorBindingBuilder.cpp
core/shader/recompiler/Recompiler.hpp
PR 5925 core/shader/recompiler/Recompiler.hpp
PR 6188 core/shader/recompiler/Recompiler.hpp
PR 6235 core/libs/prx/libSceAgcDriver/tests/ShaderDiskCache.cpp
core/libs/prx/libSceAgcDriver/tests/ShaderMemory.cpp
core/shader/recompiler/Recompiler.hpp
PR 6280 core/libs/prx/libSceAgcDriver/tests/ShaderDiskCache.cpp
core/shader/recompiler/Optimization/src/DescriptorBindingBuilder.cpp
core/shader/recompiler/Optimization/src/ResourceMaterializer.cpp
PR 6412 core/libs/prx/libSceAgcDriver/tests/ShaderDiskCache.cpp
core/libs/prx/libSceAgcDriver/tests/ShaderMemory.cpp
core/shader/recompiler/Optimization/src/DescriptorBindingBuilder.cpp
core/shader/recompiler/Optimization/src/ResourceMaterializer.cpp
core/shader/recompiler/Recompiler.hpp
PR 6488 core/libs/prx/libSceAgcDriver/tests/ShaderDiskCache.cpp
core/libs/prx/libSceAgcDriver/tests/ShaderMemory.cpp
core/shader/recompiler/Optimization/src/ResourceMaterializer.cpp
PR 7085 core/libs/prx/libSceAgcDriver/tests/ShaderDiskCache.cpp
core/shader/recompiler/Optimization/src/DescriptorBindingBuilder.cpp
core/shader/recompiler/Optimization/src/ResourceMaterializer.cpp
PR 7642 core/libs/prx/libSceAgcDriver/tests/ShaderDiskCache.cpp
core/libs/prx/libSceAgcDriver/tests/ShaderMemory.cpp
core/libs/prx/libSceAgcDriver/tests/cmake/agc_shader_memory_tests.cmake
core/shader/recompiler/Optimization/src/DescriptorBindingBuilder.cpp
core/shader/recompiler/Optimization/src/ResourceMaterializer.cpp
core/shader/recompiler/Recompiler.hpp
PR 7684 core/libs/prx/libSceAgcDriver/tests/ShaderDiskCache.cpp
core/libs/prx/libSceAgcDriver/tests/ShaderMemory.cpp
core/shader/recompiler/Optimization/src/ResourceMaterializer.cpp
PR 7689 core/shader/recompiler/Optimization/src/DescriptorBindingBuilder.cpp
core/shader/recompiler/Optimization/src/ResourceMaterializer.cpp
core/shader/recompiler/Recompiler.hpp
PR 8004 core/libs/prx/libSceAgcDriver/tests/ShaderMemory.cpp
core/shader/recompiler/Recompiler.hpp
PR 9356 core/libs/prx/libSceAgcDriver/tests/ShaderMemory.cpp
core/shader/recompiler/Recompiler.hpp
PR 9387 core/libs/prx/libSceAgcDriver/tests/ShaderMemory.cpp
core/shader/recompiler/Recompiler.hpp
PR 9508 core/libs/prx/libSceAgcDriver/tests/ShaderDiskCache.cpp
core/libs/prx/libSceAgcDriver/tests/ShaderMemory.cpp
core/libs/prx/libSceAgcDriver/tests/cmake/agc_shader_memory_tests.cmake
core/shader/recompiler/Optimization/src/DescriptorBindingBuilder.cpp
core/shader/recompiler/Recompiler.hpp
PR 9852 core/libs/prx/libSceAgcDriver/tests/ShaderDiskCache.cpp
core/shader/recompiler/Optimization/src/DescriptorBindingBuilder.cpp
core/shader/recompiler/Recompiler.hpp
PR 9897 core/libs/prx/libSceAgcDriver/tests/ShaderDiskCache.cpp
core/libs/prx/libSceAgcDriver/tests/ShaderMemory.cpp
core/libs/prx/libSceAgcDriver/tests/cmake/agc_shader_memory_tests.cmake
core/shader/recompiler/Optimization/src/DescriptorBindingBuilder.cpp
core/shader/recompiler/Recompiler.hpp
PR 10049 core/libs/prx/libSceAgcDriver/tests/ShaderDiskCache.cpp
core/shader/recompiler/Optimization/src/DescriptorBindingBuilder.cpp
core/shader/recompiler/Recompiler.hpp
PR 10193 core/libs/prx/libSceAgcDriver/tests/ShaderDiskCache.cpp
core/libs/prx/libSceAgcDriver/tests/ShaderMemory.cpp
core/libs/prx/libSceAgcDriver/tests/cmake/agc_shader_memory_tests.cmake
core/shader/recompiler/Optimization/src/DescriptorBindingBuilder.cpp
core/shader/recompiler/Optimization/src/ResourceMaterializer.cpp
core/shader/recompiler/Recompiler.hpp
PR 10351 core/libs/prx/libSceAgcDriver/tests/ShaderDiskCache.cpp
core/shader/recompiler/Recompiler.hpp
PR 10359 core/libs/prx/libSceAgcDriver/tests/ShaderMemory.cpp
core/shader/recompiler/Recompiler.hpp
PR 10652 core/libs/prx/libSceAgcDriver/tests/ShaderDiskCache.cpp
core/shader/recompiler/Recompiler.hpp
PR 10757 core/libs/prx/libSceAgcDriver/tests/ShaderMemory.cpp
core/shader/recompiler/Recompiler.hpp
PR 11483 core/libs/prx/libSceAgcDriver/tests/ShaderMemory.cpp
core/shader/recompiler/Recompiler.hpp
PR 11551 core/libs/prx/libSceAgcDriver/tests/ShaderDiskCache.cpp
core/libs/prx/libSceAgcDriver/tests/ShaderMemory.cpp
core/shader/recompiler/Optimization/src/ResourceMaterializer.cpp
core/shader/recompiler/Recompiler.hpp
PR 12324 core/libs/prx/libSceAgcDriver/tests/ShaderMemory.cpp
PR 12790 core/shader/recompiler/Optimization/src/DescriptorBindingBuilder.cpp
PR 12828 core/libs/prx/libSceAgcDriver/tests/ShaderMemory.cpp
core/shader/recompiler/Recompiler.hpp
PR 12944 core/shader/recompiler/Recompiler.hpp
PR 13928 core/libs/prx/libSceAgcDriver/tests/ShaderDiskCache.cpp
core/libs/prx/libSceAgcDriver/tests/ShaderMemory.cpp
core/libs/prx/libSceAgcDriver/tests/cmake/agc_shader_memory_tests.cmake
core/shader/recompiler/Optimization/src/DescriptorBindingBuilder.cpp
core/shader/recompiler/Optimization/src/ResourceMaterializer.cpp
core/shader/recompiler/Recompiler.hpp
PR 13929 core/libs/prx/libSceAgcDriver/tests/ShaderDiskCache.cpp
core/libs/prx/libSceAgcDriver/tests/ShaderMemory.cpp
core/libs/prx/libSceAgcDriver/tests/cmake/agc_shader_memory_tests.cmake
core/shader/recompiler/Optimization/src/DescriptorBindingBuilder.cpp
core/shader/recompiler/Optimization/src/ResourceMaterializer.cpp
core/shader/recompiler/Recompiler.hpp
PR 14032 core/libs/prx/libSceAgcDriver/tests/ShaderDiskCache.cpp
core/libs/prx/libSceAgcDriver/tests/ShaderMemory.cpp
core/libs/prx/libSceAgcDriver/tests/cmake/agc_shader_memory_tests.cmake
core/shader/recompiler/Optimization/src/DescriptorBindingBuilder.cpp
core/shader/recompiler/Recompiler.hpp
PR 14217 core/libs/prx/libSceAgcDriver/tests/ShaderDiskCache.cpp
core/shader/recompiler/Recompiler.hpp
PR 14218 core/libs/prx/libSceAgcDriver/tests/ShaderDiskCache.cpp
core/shader/recompiler/Recompiler.hpp
PR 14944 core/libs/prx/libSceAgcDriver/tests/ShaderDiskCache.cpp
core/libs/prx/libSceAgcDriver/tests/ShaderMemory.cpp
core/libs/prx/libSceAgcDriver/tests/cmake/agc_shader_memory_tests.cmake
core/shader/recompiler/Optimization/src/DescriptorBindingBuilder.cpp
core/shader/recompiler/Recompiler.hpp
PR 15548 core/libs/prx/libSceAgcDriver/tests/ShaderMemory.cpp
core/shader/recompiler/Recompiler.hpp
PR 16372 core/libs/prx/libSceAgcDriver/tests/cmake/agc_shader_memory_tests.cmake
PR 17024 core/libs/prx/libSceAgcDriver/tests/ShaderMemory.cpp
core/libs/prx/libSceAgcDriver/tests/cmake/agc_shader_memory_tests.cmake
PR 17056 core/libs/prx/libSceAgcDriver/tests/ShaderDiskCache.cpp
core/libs/prx/libSceAgcDriver/tests/ShaderMemory.cpp
core/shader/recompiler/Recompiler.hpp
PR 17611 core/libs/prx/libSceAgcDriver/tests/ShaderMemory.cpp
core/shader/recompiler/Recompiler.hpp
PR 17701 core/shader/recompiler/Optimization/src/ResourceMaterializer.cpp
PR 18390 core/libs/prx/libSceAgcDriver/tests/ShaderMemory.cpp

@atinseau

Copy link
Copy Markdown
Contributor Author

Thanks for the Windows run, JJenkx: it covers the platform I could not test.

On the perf numbers missing label: the numbers in the description come from fixtures that call the table resolution directly (allocation counts and single-process timings), and I have not captured a title trace for this change. The label will stay until a capture on main and one with the change exist; I am not going to claim a frame-time gain without them.

@MarcZX10 MarcZX10 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@atinseau

atinseau commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor Author

Thanks for the review. On the cross-thread question: the only caller outside the tests is Recompiler.cpp:1331, which dereferences the result and passes the tables to DescriptorBindingBuilder::Populate. Nothing compares the pointer there, and the tests compare it only within one thread.

On the allocation bound at 65 and 129 images: those sizes use the heap fallback the description names, one allocation per call, so allocations <= 100 over the 100 cache hits pins that single allocation and a second one per hit would fail it. Up to 64 images the bound is 0.

@Doorriiaann

Copy link
Copy Markdown
Contributor

A Linux run, since the description lists Linux as not tested: RTX 3070 (8 GB), NVIDIA 610.57.04, Ryzen 5 5600X, Debian 13, kernel 6.12.115, GCC 13.3.0 Release, validation layer 1.4.335, ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation, ctest -R '^agc_shader_' -V, 3 runs each.

merge-base 0c558e2 this PR 5d42e94
agc_shader_* tests 48, all passed 3/3 52, all passed 3/3: the same 48 and agc_shader_table_binding_growth, agc_shader_table_resolution_growth, agc_shader_empty_table_resolution, agc_shader_shared_table_resolution
VUID- lines / layer not found 0 / 0 0 / 0

This is a correctness check only; I did not measure timings.

AI-assisted: yes

@relativespace2K

Copy link
Copy Markdown
Contributor

Windows/AMD validation on RDNA 2: the four new table cases pass.

  • AMD Radeon RX 6800 XT (gfx1030, subgroup 64), AMD proprietary driver 26.10.1, Windows 11 IoT Enterprise LTSC 26100, head 5d42e94887, Release, WinLibs GCC 15.2.
  • ANYPS5_REQUIRE_VULKAN=1, ANYPS5_NO_SHADER_CACHE=1, VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation.
  • agc_shader_table_binding_growth, agc_shader_table_resolution_growth, agc_shader_empty_table_resolution and agc_shader_shared_table_resolution all pass (0.01-0.02 s each).
  • The neighbouring agc_shader_bindless (0.33 s), agc_shader_memory (0.47 s) and agc_driver_image_table (0.92 s) also pass, so the resolution path is intact on the driver side too; no VUID- line and the layer loaded.

No AMD timing number: the PR's measurement is macOS/MoltenVK; this covers the functional side on a different vendor.

@github-actions github-actions Bot added build shader core/shader agc libSceAgc* labels Nov 12, 2026
@oneandonlydean
oneandonlydean merged commit 216a7e6 into boykopovar:main Nov 14, 2026
16 checks passed
oneandonlydean added a commit that referenced this pull request Nov 14, 2026
#18827 made the empty table result a function-local thread_local. On
Windows its destructor runs after emulated TLS freed the object, which
prx_thread_local_destructors and test_thread_local_destructors reject.
Pipodi pushed a commit to Pipodi/AnyPS5 that referenced this pull request Nov 14, 2026
boykopovar#18827 made the empty table result a function-local thread_local. On
Windows its destructor runs after emulated TLS freed the object, which
prx_thread_local_destructors and test_thread_local_destructors reject.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agc libSceAgc* build perf numbers missing perf pull request without a before and after summary line perf Performance change shader core/shader

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants