perf(shader): search a bindless image table once per access #4211

Merged
karanvora2599 merged 1 commit from upstream-pr/4211 into main 2026-10-13 13:39:51 +00:00
karanvora2599 commented 2026-10-12 01:25:12 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by karanvora2599 on 2026-10-12T01:25:12Z.


What

A bindless image access now searches its table once. It used to search it twice: once before branching over the image's runtime modes, and again inside the mode arm it took.

Background.

  • For a bindless image, EmitImage calls TableSlot, which emits EmitIndirectImageSelector: a binary search over the flattened_srt key mapping. Each step is two dependent storage-buffer loads, and the slot it finds picks the runtime metadata.
  • EmitImage then branches over the runtime modes. Each arm called EmitImageMode, which called TableSlot again and emitted a second, identical search.
  • So every bindless sample, gather, load or store ran two searches back to back. Nothing merges them: flattened_srt is a plain storage buffer, and the release build runs no SPIR-V optimizer.

How. EmitImageMode takes the TableSelection that EmitImage already made. The search is emitted once, in the block that dominates every mode arm. The search at the top of EmitImage is unchanged. It already ran for every arm, the FMASK arm and the depth-bits refusal included. Only the second copy inside EmitImageMode is gone.

Why results don't change. The runtime modes are copies of the base image (ResourceMaterializer::RuntimeImageModes starts each one from auto mode = image). They differ only in format, numeric class, dimension and cube fields. The fields the search reads (indirectRoot, indirectMappingOffset, indirectSearchIterations, source) are the base image's, so both searches produced the same slot and mapped flag.

Before / after on an RTX 4060 Laptop (Ada, driver 591.86). SASS is from the APS5_PIPELINE_STATS binary size, 16 bytes per instruction, for the two pipelines of agc_driver_bindless_images:

Pipeline SASS before SASS after Registers before Registers after
1 424 248 25 24
2 296 224 31 24

Across NVIDIA GPUs.

  • The removed code is a binary search: integer compares, selects and a chain of dependent storage-buffer loads.
  • None of it uses a generation-specific instruction, so these Ada counts stand for Turing through Blackwell.
  • Pascal compiles the same operations with its own encoding.
  • On every GPU the saving includes the latency of the dependent loads, which the counts above don't show.

Tested

Windows 11, MinGW-w64 GCC 15.2.0, Release. Devices:

  • RTX 4060 Laptop (Ada, driver 591.86)
  • Intel UHD Graphics (Raptor Lake, driver 101.6790)
  • lavapipe from Mesa 25.2.8 (llvmpipe, LLVM 21.1.7): the version CI installs; Windows build with GALLIUM_OVERRIDE_CPU_CAPS=avx.

This round's four changes were built and tested together on main at bca7a29d2 (cube image gradients, early depth tests, bindless table search, LDS fence; one pull request each, in unrelated code). The branch is rebased onto 34ba918bc. Its two new commits touch only GuestMemory.cpp and the PreparedShaders.cpp fixture.

  • agc_driver_bindless_images and agc_shader_bindless pass on all three devices, so the results are the same.
  • Shader and driver tests (agc_driver_*, agc_shader_*, agc_spirv_*, vector_*, agc_masked_select, agc_validate_program, agc_denormal*, agc_float16_*; 338 tests):
    • RTX 4060: everything passes except agc_driver_prepared_shaders_registered_workgroup_sync, which fails the same way on main at bca7a29d2 (fixed since by #4177). I left out the BDA tests that lose the device on driver 591.86 (#3291): agc_driver_flat_loads*, agc_driver_global_loads*, agc_driver_flat_apertures*, agc_driver_pixel_interlock.
    • Intel UHD: everything passes or is skipped for missing features, except three tests that fail the same way on main: agc_driver_ds_swizzle, agc_driver_f32_dot2_rounding and agc_driver_prepared_shaders_registered_workgroup_sync. Also, agc_driver_compute_user_data_calls_registered exited with 0xc0000374 once under the suite's load, then passed three runs on its own.
    • lavapipe 25.2.8: everything passes or is skipped for missing features, except agc_driver_prepared_shaders_registered_workgroup_sync, as on main.

Limits. The counts come from the test's two pipelines, not from titles.

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:4823839187 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4211). Originally posted by [karanvora2599](https://github.com/karanvora2599) on 2026-10-12T01:25:12Z. --- ### What A bindless image access now searches its table once. It used to search it twice: once before branching over the image's runtime modes, and again inside the mode arm it took. **Background.** - For a bindless image, `EmitImage` calls `TableSlot`, which emits `EmitIndirectImageSelector`: a binary search over the `flattened_srt` key mapping. Each step is two dependent storage-buffer loads, and the slot it finds picks the runtime metadata. - `EmitImage` then branches over the runtime modes. Each arm called `EmitImageMode`, which called `TableSlot` again and emitted a second, identical search. - So every bindless sample, gather, load or store ran two searches back to back. Nothing merges them: `flattened_srt` is a plain storage buffer, and the release build runs no SPIR-V optimizer. **How.** `EmitImageMode` takes the `TableSelection` that `EmitImage` already made. The search is emitted once, in the block that dominates every mode arm. The search at the top of `EmitImage` is unchanged. It already ran for every arm, the FMASK arm and the depth-bits refusal included. Only the second copy inside `EmitImageMode` is gone. **Why results don't change.** The runtime modes are copies of the base image (`ResourceMaterializer::RuntimeImageModes` starts each one from `auto mode = image`). They differ only in format, numeric class, dimension and cube fields. The fields the search reads (`indirectRoot`, `indirectMappingOffset`, `indirectSearchIterations`, `source`) are the base image's, so both searches produced the same slot and mapped flag. **Before / after** on an RTX 4060 Laptop (Ada, driver 591.86). SASS is from the `APS5_PIPELINE_STATS` binary size, 16 bytes per instruction, for the two pipelines of `agc_driver_bindless_images`: | Pipeline | SASS before | SASS after | Registers before | Registers after | |---|---|---|---|---| | 1 | 424 | 248 | 25 | 24 | | 2 | 296 | 224 | 31 | 24 | **Across NVIDIA GPUs.** - The removed code is a binary search: integer compares, selects and a chain of dependent storage-buffer loads. - None of it uses a generation-specific instruction, so these Ada counts stand for Turing through Blackwell. - Pascal compiles the same operations with its own encoding. - On every GPU the saving includes the latency of the dependent loads, which the counts above don't show. ### Tested Windows 11, MinGW-w64 GCC 15.2.0, Release. Devices: - RTX 4060 Laptop (Ada, driver 591.86) - Intel UHD Graphics (Raptor Lake, driver 101.6790) - lavapipe from Mesa 25.2.8 (llvmpipe, LLVM 21.1.7): the version CI installs; Windows build with `GALLIUM_OVERRIDE_CPU_CAPS=avx`. This round's four changes were built and tested together on `main` at bca7a29d2 (cube image gradients, early depth tests, bindless table search, LDS fence; one pull request each, in unrelated code). The branch is rebased onto 34ba918bc. Its two new commits touch only `GuestMemory.cpp` and the `PreparedShaders.cpp` fixture. - `agc_driver_bindless_images` and `agc_shader_bindless` pass on all three devices, so the results are the same. - Shader and driver tests (`agc_driver_*`, `agc_shader_*`, `agc_spirv_*`, `vector_*`, `agc_masked_select`, `agc_validate_program`, `agc_denormal*`, `agc_float16_*`; 338 tests): - RTX 4060: everything passes except `agc_driver_prepared_shaders_registered_workgroup_sync`, which fails the same way on `main` at bca7a29d2 (fixed since by #4177). I left out the BDA tests that lose the device on driver 591.86 (#3291): `agc_driver_flat_loads*`, `agc_driver_global_loads*`, `agc_driver_flat_apertures*`, `agc_driver_pixel_interlock`. - Intel UHD: everything passes or is skipped for missing features, except three tests that fail the same way on `main`: `agc_driver_ds_swizzle`, `agc_driver_f32_dot2_rounding` and `agc_driver_prepared_shaders_registered_workgroup_sync`. Also, `agc_driver_compute_user_data_calls_registered` exited with 0xc0000374 once under the suite's load, then passed three runs on its own. - lavapipe 25.2.8: everything passes or is skipped for missing features, except `agc_driver_prepared_shaders_registered_workgroup_sync`, as on `main`. **Limits.** The counts come from the test's two pipelines, not from titles. ### 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
Dextroid17 commented 2026-10-12 01:37:40 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Dextroid17 on 2026-10-12T01:37:40Z.


Three small, well-scoped perf/correctness fixes from the same author — reviewed individually since one of them is a real correctness bug, not just an optimization.

#4212 (LDS atomic double-fence): Confirmed exactly as described. AtomicUpdateTyped's compare-and-swap loop already ends its LDS path with a workgroup-scoped acquire-release barrier, and SharedAtomicUpdate emitted a byte-for-byte identical second one right after — same scope, same semantics, same state, truly redundant. The GDS path keeps its second barrier correctly, because GDS's loop barrier is device-scoped, not workgroup-scoped, so there it isn't a duplicate. The LDS/GDS split is driven by the same enum the loop itself already branches on, not a separate heuristic that could misclassify. One cosmetic nit: the body's instruction list omits EmitSharedAtomicCmpstF32, which also routes through this same function and gets the same fix.

#4211 (bindless table double-search): Also confirmed clean. The key question was whether the two searches are provably identical, not just usually identical — and they are: the runtime image-mode variants are copies of the base image that only ever mutate format/numeric-class/dimension/cube-type fields, never the four fields the search actually reads (indirectRoot, source, indirectSearchIterations, indirectMappingOffset). Since those are untouched across every mode, the second search could never have produced a different slot. Minor nuance worth noting, not a defect: the search is now also evaluated on two arms (FMASK, and the depth-bits-fail case) that previously skipped it entirely — practically inert for non-bindless images, but worth a mention in case a future bindless+FMASK combination surfaces a different failure-message path than before.

#4210 (early depth decode) — this one is a genuine correctness bug, not a perf nit, and the fix is right. The pixel-stage decoder's precheck already accepted the DEPTH_BEFORE_SHADER bit but never actually read it, and Z_ORDER==3 (EARLY_Z_THEN_RE_Z) was never treated as early depth at all — only Z_ORDER==1 was. Without EarlyFragmentTests declared, a Vulkan driver can't discard an occluded pixel before a shader that writes memory, so on every host GPU those shaders were running — and writing memory — for pixels the real hardware would never have reached. That's silent output corruption, not just wasted work. The fix is correct and the alpha-to-coverage exception is properly preserved. One gap worth flagging: the fix deliberately lets DEPTH_BEFORE_SHADER set early-Z even when combined with a Z-export or mask-export, which the accompanying TechnicalDebt entry only partially covers — it notes the bit-plus-Z-export combination as unmeasured but doesn't mention the mask-export case. Also, the new test can't actually exercise the alpha-to-coverage clause end-to-end, since that flag is only ever set by the driver, not by anything the decode test constructs — the "still tests late" claim rests on reading the code, not on a test proving it.

Written with AI assistance.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6116213828 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4211#issuecomment-6116213828). Originally posted by [Dextroid17](https://github.com/Dextroid17) on 2026-10-12T01:37:40Z. --- Three small, well-scoped perf/correctness fixes from the same author — reviewed individually since one of them is a real correctness bug, not just an optimization. **#4212 (LDS atomic double-fence):** Confirmed exactly as described. `AtomicUpdateTyped`'s compare-and-swap loop already ends its LDS path with a workgroup-scoped acquire-release barrier, and `SharedAtomicUpdate` emitted a byte-for-byte identical second one right after — same scope, same semantics, same state, truly redundant. The GDS path keeps its second barrier correctly, because GDS's loop barrier is device-scoped, not workgroup-scoped, so there it isn't a duplicate. The LDS/GDS split is driven by the same enum the loop itself already branches on, not a separate heuristic that could misclassify. One cosmetic nit: the body's instruction list omits `EmitSharedAtomicCmpstF32`, which also routes through this same function and gets the same fix. **#4211 (bindless table double-search):** Also confirmed clean. The key question was whether the two searches are *provably* identical, not just usually identical — and they are: the runtime image-mode variants are copies of the base image that only ever mutate format/numeric-class/dimension/cube-type fields, never the four fields the search actually reads (`indirectRoot`, `source`, `indirectSearchIterations`, `indirectMappingOffset`). Since those are untouched across every mode, the second search could never have produced a different slot. Minor nuance worth noting, not a defect: the search is now also evaluated on two arms (FMASK, and the depth-bits-fail case) that previously skipped it entirely — practically inert for non-bindless images, but worth a mention in case a future bindless+FMASK combination surfaces a different failure-message path than before. **#4210 (early depth decode) — this one is a genuine correctness bug, not a perf nit, and the fix is right.** The pixel-stage decoder's precheck already *accepted* the `DEPTH_BEFORE_SHADER` bit but never actually read it, and `Z_ORDER==3` (`EARLY_Z_THEN_RE_Z`) was never treated as early depth at all — only `Z_ORDER==1` was. Without `EarlyFragmentTests` declared, a Vulkan driver can't discard an occluded pixel before a shader that writes memory, so on every host GPU those shaders were running — and writing memory — for pixels the real hardware would never have reached. That's silent output corruption, not just wasted work. The fix is correct and the alpha-to-coverage exception is properly preserved. One gap worth flagging: the fix deliberately lets `DEPTH_BEFORE_SHADER` set early-Z even when combined with a Z-export or mask-export, which the accompanying TechnicalDebt entry only partially covers — it notes the bit-plus-Z-export combination as unmeasured but doesn't mention the mask-export case. Also, the new test can't actually exercise the alpha-to-coverage clause end-to-end, since that flag is only ever set by the driver, not by anything the decode test constructs — the "still tests late" claim rests on reading the code, not on a test proving it. Written with AI assistance.
karanvora2599 commented 2026-10-12 02:16:02 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by karanvora2599 on 2026-10-12T02:16:02Z.


Thanks @Dextroid17. One correction on the nuance: the FMASK and depth-bits arms already had the search before this change. EmitImage calls TableSlot once at the top, before it branches over the modes, for every arm (SpirvImageEmitter.cpp, const auto table = TableSlot(ctx, inst, memory, base); on main). The only search this PR removes is the second one EmitImageMode emitted inside each mode arm, so nothing is evaluated on an arm that didn't evaluate it before. I've added a line saying so to the description.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6116555512 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4211#issuecomment-6116555512). Originally posted by [karanvora2599](https://github.com/karanvora2599) on 2026-10-12T02:16:02Z. --- Thanks @Dextroid17. One correction on the nuance: the FMASK and depth-bits arms already had the search before this change. `EmitImage` calls `TableSlot` once at the top, before it branches over the modes, for every arm (`SpirvImageEmitter.cpp`, `const auto table = TableSlot(ctx, inst, memory, base);` on `main`). The only search this PR removes is the second one `EmitImageMode` emitted inside each mode arm, so nothing is evaluated on an arm that didn't evaluate it before. I've added a line saying so to the description.
oneandonlydean commented 2026-10-13 02:42:55 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-13T02:42:55Z.


Same as #4287: a frame-time comparison on a title with bindless image accesses in a hot shader, main against main plus this PR alone.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6137037410 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4211#issuecomment-6137037410). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-13T02:42:55Z. --- Same as #4287: a frame-time comparison on a title with bindless image accesses in a hot shader, `main` against `main` plus this PR alone.
github-actions[bot] commented 2026-10-13 13:10:31 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-13T13:10:31Z.


Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.

Pull request Conflicting files (lines on main) Same exports Same new files
#757 core/shader/recompiler/SpirvBackend/src/SpirvImageEmitter.cpp L1604
#758 core/shader/recompiler/SpirvBackend/src/SpirvImageEmitter.cpp L1604
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6146308233 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4211#issuecomment-6146308233). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-13T13:10:31Z. --- <!-- pr-overlap --> Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in **Depends on**. | Pull request | Conflicting files (lines on `main`) | Same exports | Same new files | |---|---|---|---| | #757 | `core/shader/recompiler/SpirvBackend/src/SpirvImageEmitter.cpp` [L1604](https://github.com/boykopovar/AnyPS5/blob/58f7242ea2a97897981623fcb56fd0a794ffb61c/core/shader/recompiler/SpirvBackend/src/SpirvImageEmitter.cpp#L1604) | | | | #758 | `core/shader/recompiler/SpirvBackend/src/SpirvImageEmitter.cpp` [L1604](https://github.com/boykopovar/AnyPS5/blob/58f7242ea2a97897981623fcb56fd0a794ffb61c/core/shader/recompiler/SpirvBackend/src/SpirvImageEmitter.cpp#L1604) | | |
adriadam10 commented 2026-10-13 13:30:09 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-13T13:30:09Z.


Checked the equivalence: RuntimeImageModes copies the base image per mode, TableSlot only reads indirectRoot, the mapping offset, search iterations and source, and the single search in EmitImage dominates every mode arm. Built and ran the libSceAgc/libSceAgcDriver tests: agc_driver_bindless_images and agc_shader_bindless pass. agc_driver_tessellation segfaults here, but it does so identically with this change reverted. Merging once the full Build is green.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6146683539 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4211#issuecomment-6146683539). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-13T13:30:09Z. --- Checked the equivalence: `RuntimeImageModes` copies the base image per mode, `TableSlot` only reads `indirectRoot`, the mapping offset, search iterations and `source`, and the single search in `EmitImage` dominates every mode arm. Built and ran the libSceAgc/libSceAgcDriver tests: `agc_driver_bindless_images` and `agc_shader_bindless` pass. `agc_driver_tessellation` segfaults here, but it does so identically with this change reverted. Merging once the full Build is green.
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!4211
No description provided.