fix(shader): read a MIMG sampler only for ops that sample #22076
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/22076"
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 B-Howell on 2026-11-17T04:11:53Z.
What
The scalar call analysis in
GraphBuilder.cpp(readsLinkRegisterandreadsScalarDword) counted the SSAMP field of every MIMG instruction as a read of four scalar registers. Only sampling ops read a sampler: sample, gather andimage_get_lod, the same setDecodeRdnaMimgalready range-checks SSAMP for.image_load*,image_store*,image_get_resinfoand the image atomics ignore the field, and compilers leave it 0. So a shader that calls a subroutine withs_call_b64 s[0:1]and then runsimage_load_mipwith SSAMP 0 was refused withscalar call link register s[0] ... escapes the constant-offset call/return model, although nothing readss[0:1]after the return.Changes:
DecodeRdnaMimgrecords whether the op takes a sampler in the newRdnaInstruction::imageSampler. It computed this already assamplerOp.readsLinkRegisterandreadsScalarDwordcount SSAMP only whenimageSampleris set.pairOverlapsRegisterreports no overlap for an empty range. Before, a count of 0 still matched a register one above the link pair.imageMemoryInfoFromInstructionstill readssource2for every MIMG op, so translation is unchanged.ScalarCall.cppaddsCheckImageSamplerField. With link pairss[0:1]ands[2:3], a call returns to a 2D MIMG instruction whose SSAMP iss[0:3]:image_load,image_load_mip,image_storeandimage_get_resinfomust build;image_sampleandimage_get_lodmust still be refused as link escapes.Tested
Windows 11 Pro 10.0.26300, WinLibs GCC 15.2.0 r7, Release, NVIDIA GeForce RTX 4080 SUPER (driver 621.14). Run with
ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation VK_LOADER_DEBUG=layer, using the LunarG layer 1.4.363.1 throughVK_ADD_LAYER_PATH:ctest -R '^(agc_driver_gcn_.*|agc_driver_guarded_null_pointer|agc_driver_guarded_direct_root|agc_gcn_color_target|scalar_call|scalar_swappc|control_flow_graph|image_by|image_msaa_load|agc_driver_scalar_swappc|agc_driver_scalar_swappc_fetch|agc_driver_image_by)$' -E '_trigger$' -VWithout the fix (the new test only): 23/24 pass.
scalar_callfails withscalar call link register s[0] written at program counter 0x00000000 escapes the constant-offset call/return model (used at program counter 0x00000004), the title's message below.This change: 24/24 pass.
scalar_callpasses 3 of 3 runs.Validation layer: both logs have 18
Insert instance layer "VK_LAYER_KHRONOS_validation"lines, one per Vulkan test. Neither haswas not found but was requested by env var VK_INSTANCE_LAYERS,failed to loadorVUID-.Not run:
agc_shader_control_flowdoes not link on Windows here, becauseobjcopyfails on the test executable, as it does foragc_shader_memory.Build tree: the tests ran in my Bloodborne tree. Its
GraphBuilder.cpp,RdnaImageOpDecoder.cpp,RdnaInstruction.hppandScalarCall.cppmatchmain's.Title: Bloodborne CUSA00900 01.09 (my own copy), on
mainf2a25e5b42aplus #17105, #17711, #20156, #21944, #21969, #21977, #22033, #18177 and #22060.0x2d20eb300:scalar call link register s[0] written at program counter 0x00000008 escapes the constant-offset call/return model (used at program counter 0x00000018). At 0x8 the shader runss_call_b64 s[0:1]to a subroutine that returns withs_setpc_b64 s[0:1]. At 0x18 it runsimage_load_mipwith resources[8:15]and SSAMP 0.a GCN CMASK that fast clears only some tiles of its color target is not implemented, which is not related to this change.Tested by @sharveshprabhu on
eb71f241merged ontomaina72186d3cf: the samectestfilter plusagc_shader_control_flow.*,--repeat until-fail:3, 24/24 on bothmainand this change on a Radeon 780M (AMD 26.10.2) and an RTX 5060 Laptop (NVIDIA 617.42), Windows 11 26300, WinLibs GCC 15.2.0 r7, validation layer on, 0VUID-.agc_shader_control_flowand its three variants pass on both GPUs; theobjcopylink failure was Windows Defender scanning the new executables (comment).python3 tools/check_conventions.py --base origin/mainpasses.tools/pr_overlap.py --branch fix/shader-mimg-sampler-link-readsreports no overlap with open pull requests.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-11-17T06:09:36Z.
Review: APPROVED
Reviewed — looks good
The change stops treating the MIMG sampler field as a scalar read for ops that do not sample, the translator uses that sampler only on the sampling paths, and I found no blocking defect at head
eb71f24180239bda2451f1c73cf9ece652e7ef90.What I ran
eb71f24180239bda2451f1c73cf9ece652e7ef90. Base:main. No merged-preview check.The author's test runs (ctest on Windows, the LunarG validation layer, Bloodborne) are reported in the description. I did not run them.
Results
count != 0ucheck addedcheck_conventions.pyFindings
Blocking
None.
Non-blocking
core/libs/tests/ScalarCall.cpp:571checks onlyimage_sample(0x20) andimage_get_lod(0x60) as still refused. Gather ops also setimageSamplerthroughinfo.gather(core/shader/recompiler/RdnaDecoder/src/RdnaImageOpDecoder.cpp:340), and no new test covers them. A gather op is still refused by the new code path, but a test for it would lock that in.Sources
core/shader/recompiler/ControlFlow/src/GraphBuilder.cpp:385ateb71f24180239bda2451f1c73cf9ece652e7ef90.core/shader/recompiler/ControlFlow/src/GraphBuilder.cpp:407and:655at the same commit.samplerOp:core/shader/recompiler/RdnaDecoder/src/RdnaImageOpDecoder.cpp:340and:445at the same commit.core/shader/recompiler/Translation/src/MemoryInstructions/ImageInstructions.cpp:28,:134,:204,:224at the same commit.core/libs/tests/ScalarCall.cpp:560to:578at the same commit.Not verified
check_conventions.pywere not run here. The author's Windows results are not my run.scalar_callfails) is author-reported only and was not run.files.json(core/libs/tests/ScalarCall.cpp,core/shader/recompiler/ControlFlow/src/GraphBuilder.cpp,core/shader/recompiler/RdnaDecoder/include/RdnaDecoder/RdnaInstruction.hpp,core/shader/recompiler/RdnaDecoder/src/RdnaImageOpDecoder.cpp). I read their changed hunks with surrounding context, not every line of each file.ResourceMaterializer.cppand the SPIR-V image emitter also include the MIMG decoder; I did not check whether they read the sampler field for non-sampling ops.To get this to a good state
Nothing is required.
Optional: add a gather opcode (for example
image_gather4) to the refused list inCheckImageSamplerField.