perf(shader): deduplicate runtime image switch modes and base-level sample branches #8716
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/8716"
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 hamb3r on 2026-10-20T22:01:16Z.
What
EmitImage(SameImageModeForInstruction) forImageAtomic,ImageQueryDimensions,ImageQueryLod,ImageRead, andImageWriteso modes that emit identical SPIR-V share a singleOpSwitchtarget basic block with multiple case literals.EmitImageSampleInstructionwhen the sample already uses LOD 0 (hasExplicitZeroLod:ImageSampleLZ,ImageSampleC_LZ, orImageSampleExplicitLodwith constant0.0fLOD).EmitFormatStoreComponentinEmitFormatConvertedImageStorefor 32-bitSINTcomponents, whereEmitFormatStoreComponentalready returns its input unchanged.Tested
x86_64), NVIDIA GeForce RTX 3080 (driver 617.14), AMD Ryzen 7 7700:main(7a5d9bf02af6) and on this branch (45998c33789b) withANYPS5_REQUIRE_VULKAN=1,ANYPS5_NO_SHADER_CACHE=1andVK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation(ctest --test-dir build-portable -R "^(agc_shader_memory|agc_redundant_value_elimination|agc_shader_srt_read_patching|agc_formatted_buffer_once|agc_formatted_buffer_byte_stores|agc_driver_gated_dispatch_capture|agc_driver_image_sample_unnormalized|agc_driver_image_store_sint|agc_driver_image_table|agc_driver_prepared_shaders_indirect)$" -V):main(5.10stotal) and this branch (5.78stotal).was not found but was requested by env var VK_INSTANCE_LAYERSand 0VUID-validation errors are logged.PPSA01341v01.007.000,APS5_TRACE_FRAMES=0:5): total cached SPIR-V size across the 278 shaders inframes 0:5decreased by 6,172,384 bytes (249.12 MB->243.23 MB).tools/trace_digest.py—main(ca74ef2177ee)frames 0:5 from present 0: 5 presents, frame p50 970.2 / p90 178158.2 / p99 178158.2 / max 178158.2 ms, GPU busy 51.6 ms per present
NVIDIA GeForce RTX 3080, NVIDIA 617.14, Windows 10.0.19045, 1920x1080, commit
ca74ef2177, dropped: noSlowest frames (largest slices inside each, inclusive)
Threads (ms per frame)
Largest slices (ms per frame, inclusive, all threads): Driver.Packet 290048.8, Driver.Packet.memory_wait 251696.5, Driver.Packet.dispatch 38274.4, Driver.Dispatch 38227.1, Vulkan.ComputePipeline 35186.4, Driver.Dispatch.prepared_invocation 1210.4, Driver.Dispatch.materialize 601.1, Vulkan.Dispatch 489.1, Vulkan.Dispatch.shader_resources 476.1, Resources.Prepare 331.4
Longest waits
Largest GPU batches (GPU ranges inside, and the CPU slices that recorded each, inclusive)
tools/trace_digest.py— PR (4bf7fec0488f)frames 0:5 from present 0: 5 presents, frame p50 834.3 / p90 17540.9 / p99 17540.9 / max 17540.9 ms, GPU busy 44.2 ms per present
NVIDIA GeForce RTX 3080, NVIDIA 617.14, Windows 10.0.19045, 1920x1080, commit 4bf7fec0488f, dropped: no
Slowest frames (largest slices inside each, inclusive)
Threads (ms per frame)
Largest slices (ms per frame, inclusive, all threads): Driver.Packet 33203.2, Driver.Packet.memory_wait 27768.9, Driver.Packet.dispatch 5389.9, Driver.Dispatch 5365.5, Vulkan.ComputePipeline 3652.8, Driver.Dispatch.materialize 580.1, Vulkan.Dispatch 408.7, Vulkan.Dispatch.shader_resources 394.3, Driver.Dispatch.prepared_invocation 225.6, Driver.Dispatch.lookup_cache 223.7
Longest waits
Largest GPU batches (GPU ranges inside, and the CPU slices that recorded each, inclusive)
Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-20T22:03:53Z.
Code review location:
core/shader/recompiler/SpirvBackend/src/SpirvImageEmitter.cpp; line 1897Correction: I withdraw this concern. Rechecking the condition shows
alreadyBaseLevelrequiresopcode == spv::OpImageSampleExplicitLod; it therefore cannot suppressSamplesBaseLevelIfUnnormalizedforOpImageSampleImplicitLod, and that fallback remains enabled for implicit samples. My earlier comment incorrectly described the opcode condition. I have not found a regression from this guard.Imported from boykopovar/AnyPS5. Originally posted by hamb3r on 2026-10-21T00:31:28Z.
Rebased/merged
origin/main(d8ea38616e) and audited line-by-linegcovdiff coverage oncore/shader/recompiler/SpirvBackend/src/SpirvImageEmitter.cpp.Uncovered diff lines before (
40 / 43 = 93.0%)Coverage added in
dd90db591d(43 / 43 = 100.0%)Added
verifyRuntimeImageSwitchDeduplication()incore/libs/prx/libSceAgcDriver/tests/ShaderMemory.cpp(agc_shader_memory) to exercise:IrOpcode::ImageReadandIrOpcode::ImageWriteruntime-imageOpSwitchdeduplication acrossResourceMaterializer::RuntimeImageModes(image)(verifying that packed cube and 2D-array modes with identicalpackedFormatshare anOpSwitchlabel while distinctpackedFormats stay separate — covering lines1910and1919).IrOpcode::ImageWriteruntime-image modes with distinctImageMipModevalues (verifying that they emit distinctOpSwitchlabels — covering line1916).Focused tests re-verified with
ANYPS5_REQUIRE_VULKAN=1,ANYPS5_NO_SHADER_CACHE=1, andVK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation(0VUID-errors).cc @MarcZX10
Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-21T05:54:43Z.
@hamb3r thanks, I checked the merged switch cases against every image field the read, write, atomic and query emitters use, and the grouping holds for each of them. One request: could you update the description to match the code? It mentions
EmitImageSwitch,EmitSampleImageSwitch,EmitWriteSwitch,EmitQuerySwitch, anis_zero_lodshort-circuit inEmitImageSampleWithLodand a hoistedresult_type, but none of these are in the diff, andSameImageModeForInstructionnever merges sampling modes. The code does three things: it merges equal runtime modes inEmitImagefor atomic, query, read and write; it skips the unnormalized base-level branch for samples that already use LOD 0; and it skipsEmitFormatStoreComponentfor 32-bit SINT components. That last one returns its input unchanged at 32 bits already, so the SPIR-V stays the same.Imported from boykopovar/AnyPS5. Originally posted by hamb3r on 2026-10-21T07:08:16Z.
@oneandonlydean Updated the PR description (What section) to accurately describe the three changes in SpirvImageEmitter.cpp. Thanks for the review!
Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-21T07:24:50Z.
@hamb3r thanks, the What section now matches the three changes in
SpirvImageEmitter.cpp: the merged image modes, the skipped base-level branch for LOD 0 samples, and the skipped conversion for 32-bit SINT components.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-23T05:16:35Z.
Review: CHANGES_REQUESTED
I reviewed the current head
8a747c6ef125e8a15fd213af0d6a08e0c58034df. The three changes remain distinct and the new emitter tests exercise packed-format deduplication and mip-mode separation. The PR reports the same focused validation-layer set on main and the change, with no missing-layer warning or VUIDs, plus Windows and Polaris results. Local diff whitespace, conventions, and registration checks pass; I could not run GPU tests on this host.One repository-readiness blocker remains:
tools/check_main_sync.py --base upstream/main --head <head>reports this branch 5 commits ahead / 256 behind currentupstream/main(merge basefcd455870), though it merges cleanly.core/libs/prx/libSceAgcDriver/tests/ShaderMemory.cppalso changed on main after that base (#6801), so the new test must be retained alongside current tests. CONTRIBUTING requires branches to be kept up to date. Please merge current main, preserve both sets of changes, and rerun the focused shader/AGC validation tests on the resulting head. This request concerns current-main readiness only; it is not based on intake volume.Imported from boykopovar/AnyPS5. Originally posted by imellag on 2026-10-28T18:45:08Z.
Review: COMMENTED
macOS / MoltenVK data point. Apple M4 Pro, macOS 27.2, MoltenVK 1.4.2, x86_64 Release under Rosetta.
maindoesn't run titles on macOS yet, so the base is a support branch, notmain:main750cf6cf48+ only the macOS boot PRs #7631 #10552 #11249 #11447 #11554 #10669 #7804, + #12529 (the revert of #10337, which roughly doubles the GPU time on this GPU and would hide smaller changes). Change: that base + eight open shader performance PRs merged together (#12287 #11952 #12299 #12280 #11368 #11265 #8716 #8958, all clean merges). I did not measure each PR alone.Two alternated runs per side, warm shader cache,
APS5_TRACE_SUMMARY_EVERY=10:Together they don't raise the GPU time on MoltenVK. I'm checking open shader performance PRs on this GPU because #10337 was measured on RADV and doubled it here.
Focused tests below ran with
ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validationon the same two trees; the layer was found and both logs have the sameVUID-lines (12VUID-VkPipelineColorBlendStateCreateInfo-pAttachments-00605, 20VUID-vkCreateDescriptorSetLayout-support-09582, from tests that fail the same way on the base).Interaction with #12299. The two merge without a textual conflict, but together they fail
agc_shader_memoryat this PR's new check:runtime image switch deduplication: OpSwitch did not cover every runtime image mode. Each one alone passes it (base + #8716: pass; base + #12299: pass; base + #8716 + #12299: fail; base + #8716 + #11952: pass). #12299 turns the direct-imageOpSwitchinto a balancediftree for value-returning images, so the switch this PR deduplicates is no longer there. Could you and @Filip722 agree which lands first and how the second adapts?This PR on MoltenVK otherwise:
agc_redundant_value_elimination,agc_shader_srt_read_patching,agc_formatted_buffer_once,agc_formatted_buffer_byte_stores,agc_driver_gated_dispatch_capture,agc_driver_image_sample_unnormalized,agc_driver_image_store_sintandagc_driver_prepared_shaders_indirectpass on the base and with the change;agc_driver_image_tablefails on both with the sameVUID-vkCreateDescriptorSetLayout-support-09582. No GPU regression in the group.AI-assisted: yes (Claude Code ran the builds, runs and tests and drafted this review)
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-29T06:15:35Z.
Review: CHANGES_REQUESTED
Re-checked — changes requested
The previous code-level concerns appear addressed: the author reports preserving #6801’s
ShaderMemory.cppadditions and the runtime image-switch deduplication regression, then rerunning focused Vulkan-validation tests and a Linux selected-test run. The PR includes the required same-title/machine performance summary lines and trace digests.One integration blocker remains. The current PR head
e6504377c6b565b8ab39609d5f83e7391a8df116is 1,644 commits behind currentmain2960e9a6e7e1fc99c4ed180c71495b5eb83464c7(merge base5bc84038c490a0427ff272e39e2a1cdf6fdc3c0e).ShaderMemory.cppandSpirvImageEmitter.cppchanged on both sides since that base. The merge preview is clean, but a clean merge alone does not validate the resulting code or establish that the recorded performance comparison still describes the current integration. Please update against currentmain, rerun the focused shader/image validation suite, and refresh the same-title performance pair if those current-main changes affect the measured emitter path.I did not run GPU tests locally. The hosted checks currently pass policy and Windows jobs plus relinker matrices; Linux product CI is skipped.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-29T10:56:18Z.
Review: CHANGES_REQUESTED
Re-check — changes requested for current-main performance validation\n\nThe author’s 10/10 focused Vulkan-validation run on
main1b580f13cf7eand head01514a6e7a49addresses the earlier test rerun request; the report includes validation-layer loading and zeroVUIDresults.\n\nCurrent upstreammainhas since advanced tod2b15d8802. It changedcore/shader/recompiler/SpirvBackend/src/SpirvImageEmitter.cpp,core/shader/recompiler/Optimization/src/ResourceMaterializer.cpp, anddocs/dev/debt/shader.md, all touched by this PR. The clean merge preview does not make the existing tests or performance comparison current for those emitter/materialization changes. Please update the branch and rerun the focused comparison on current main and the updated head with validation enabled. Since this is aperfPR and the changed main paths overlap the measured shader work, refresh the same-scene/same-machine summary lines, traces, and trace digests for the new pair.\n\nI checked the current-main merge preview and whitespace; both are clean. I did not run local GPU tests. Hosted Linux/Windows relinker and ASan pass; platform product jobs are skipped, and macOS relinker jobs remain queued.