perf(shader): search a bindless image table once per access #4211
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/4211"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
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.
EmitImagecallsTableSlot, which emitsEmitIndirectImageSelector: a binary search over theflattened_srtkey mapping. Each step is two dependent storage-buffer loads, and the slot it finds picks the runtime metadata.EmitImagethen branches over the runtime modes. Each arm calledEmitImageMode, which calledTableSlotagain and emitted a second, identical search.flattened_srtis a plain storage buffer, and the release build runs no SPIR-V optimizer.How.
EmitImageModetakes theTableSelectionthatEmitImagealready made. The search is emitted once, in the block that dominates every mode arm. The search at the top ofEmitImageis unchanged. It already ran for every arm, the FMASK arm and the depth-bits refusal included. Only the second copy insideEmitImageModeis gone.Why results don't change. The runtime modes are copies of the base image (
ResourceMaterializer::RuntimeImageModesstarts each one fromauto 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_STATSbinary size, 16 bytes per instruction, for the two pipelines ofagc_driver_bindless_images:Across NVIDIA GPUs.
Tested
Windows 11, MinGW-w64 GCC 15.2.0, Release. Devices:
GALLIUM_OVERRIDE_CPU_CAPS=avx.This round's four changes were built and tested together on
mainatbca7a29d2(cube image gradients, early depth tests, bindless table search, LDS fence; one pull request each, in unrelated code). The branch is rebased onto34ba918bc. Its two new commits touch onlyGuestMemory.cppand thePreparedShaders.cppfixture.agc_driver_bindless_imagesandagc_shader_bindlesspass on all three devices, so the results are the same.agc_driver_*,agc_shader_*,agc_spirv_*,vector_*,agc_masked_select,agc_validate_program,agc_denormal*,agc_float16_*; 338 tests):agc_driver_prepared_shaders_registered_workgroup_sync, which fails the same way onmainatbca7a29d2(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.main:agc_driver_ds_swizzle,agc_driver_f32_dot2_roundingandagc_driver_prepared_shaders_registered_workgroup_sync. Also,agc_driver_compute_user_data_calls_registeredexited with 0xc0000374 once under the suite's load, then passed three runs on its own.agc_driver_prepared_shaders_registered_workgroup_sync, as onmain.Limits. The counts come from the test's two pipelines, not from titles.
Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt.mdfiles or images added to the repository (attach them to this PR)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, andSharedAtomicUpdateemitted 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 omitsEmitSharedAtomicCmpstF32, 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_SHADERbit but never actually read it, andZ_ORDER==3(EARLY_Z_THEN_RE_Z) was never treated as early depth at all — onlyZ_ORDER==1was. WithoutEarlyFragmentTestsdeclared, 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 letsDEPTH_BEFORE_SHADERset 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.
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.
EmitImagecallsTableSlotonce at the top, before it branches over the modes, for every arm (SpirvImageEmitter.cpp,const auto table = TableSlot(ctx, inst, memory, base);onmain). The only search this PR removes is the second oneEmitImageModeemitted 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.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,
mainagainstmainplus this PR alone.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.
main)core/shader/recompiler/SpirvBackend/src/SpirvImageEmitter.cppL1604core/shader/recompiler/SpirvBackend/src/SpirvImageEmitter.cppL1604Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-13T13:30:09Z.
Checked the equivalence:
RuntimeImageModescopies the base image per mode,TableSlotonly readsindirectRoot, the mapping offset, search iterations andsource, and the single search inEmitImagedominates every mode arm. Built and ran the libSceAgc/libSceAgcDriver tests:agc_driver_bindless_imagesandagc_shader_bindlesspass.agc_driver_tessellationsegfaults here, but it does so identically with this change reverted. Merging once the full Build is green.