shader: bound waterfall image keys by their clamp on the selected lanes #2278
No reviewers
Labels
No labels
bug
documentation
duplicate
enhancement
GPU
GUI
help wanted
question
Shaders
Video
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/KytyPS5--KytyPS5!2278
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-2278"
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?
@Nawid3333 on GitHub · 2026-11-14 17:27 UTC · original
Nawid3333:shader/query-only-waterfall-image→main· +110 −22 · 6 files · 4 commits · openFor #774 (Warhammer Age of Sigmar Realms of Ruin, PPSA06927).
Cause
The reporter's last two runs stop in resource tracking with
GetImageResource dword 0 is not a valid runtime value, in pixel shaders0x8b06bb2656189c82(pc 0x1b4) and0x4b7ac860a15d013a(pc 0x72c). Both read 8-dword image descriptors from an address table in user data (s[0:1] + key * 32) inside waterfall loops:The table has no record count, so the tracker needs a bound for
key.SelectedSelectorDomaintakes the domain of aReadFirstLanekey from a finite set of literals, or else fromPossibleU32Bits(local). The second ignores the lane mask, so it sees the unclamped armxand finds no bound, although the selected lane satisfiesmaskandmaskimpliesc.The first shader's failing loop only queries the image size (
IMAGE_GET_RESINFO).ImageQueryDimensionsandImageQueryLodhad no EXEC operand, whileAccessPredicate,SetExactTableMaximumand the pointer-table path read the last operand of every image access as its EXEC. For the queries, that operand was the image address.Change
SelectedSelectorDomain: when no literal domain exists, the maximum ismin(PossibleU32Bits(local), SelectorMaximum(local, {}, depth + 1, mask)). This is theReadFirstLanecounterpart of theReadLanecase, which already bounds the local value under the guard that proves it equal to the key. The existing check before it (Implies(guard, mask)orHasActiveLane(mask)) is what makes the selected lane one wheremaskholds.SelectorMaximumpasses its depth through.ImageQueryDimensionsandImageQueryLodtake a trailing U1 EXEC operand, andIMAGE_GET_RESINFO/IMAGE_GET_LODpassir.GetExec()likeIMAGE_LOADdoes. The SPIR-V emitter does not read it.Tests
resource_tracking_tests,TestQueryOnlyImageWaterfall: the shape above in a pixel shader, as straight-line code and as a loop with the Phi mask; query only, and query followed by a sampling loop on the same key. Each table getskey_count16384 and no selector. A query whose EXEC istruestill stops. With main'ssrc/(3e226bff9), the query-only case stops with the reporter's message (stage=pixel pc=0x000001b4 GetImageResource dword 0 is not a valid runtime value).TestUniformizedMaterialImageKeys: the seven packed variants whose material-selector proof fails used to stop. Their key is the sameUMin32(16383, ...)clamp, so they now keep the clamp's 16384-row table domain. They still get no selector, and the test now checks exactly that (!selector,key_count == 16384).ResourceTrackingTests,shaderCfgTestsandShaderRecompilerComputeTeststake the new operand. The compute test's query keeps the predicate of the access it replaces.resource_tracking_tests,resource_materialization_tests,scalar_provenance_tests,shader_vertex_metadata_testsandshader_cfg_testspass. The fullshader_recompiler_compute_testsrun on an RX 9070 XT (Windows 11, clang-cl, with the local harness changes of #2038) has the same result with and without the change: 192 ok, the same two failed checks, and the same stop atimage.cpp:714(a multisampled format this GPU does not support for that use). Fork CI on this commit passed on Windows, Linux and macOS (run 43616718620).Offline, the 172 Ghost of Yōtei programs I have captures of were recompiled with main and with the change: the SPIR-V is byte-identical for all 172.
Costs
A shader of this shape reads up to 16384 table rows (32 bytes each) when its descriptors are specialized, as an
SMin32-clamped key already does throughClampedTableMaximum. Shaders that translate today are unchanged in the corpus above.In a game
Not yet tested in the game. The reporter has a test build of this commit (#774); I will add the result here. Every waterfall loop in the two failing shaders has the shape above (dumps of both in the reporter's logs).
Written with Claude Code (AI): Claude Code traced both shaders, wrote the change and the tests and ran them; Claude (Opus) reviewed the diff.
@coderabbitai[bot] on GitHub · 2026-11-14 17:36 UTC · original · edited 2026-11-26 23:35 UTC
Walkthrough
Image-query IR operations now receive the active execution mask. Resource tracking uses guard-aware selector bounds when finite-domain analysis has no known maximum. Tests update image-query operands and cover packed selectors and query-only or query-then-sample waterfalls.
Suggested reviewers:
nmzik,claxten10,senaxxChange: Bug fix
Merge Risk:
Low · up to
80e34This change fixes image-query resource tracking for clamped waterfall selectors. The remaining concerns are an older gap in sampler planning for LOD queries and a possible traversal-cost issue on unusual shader shapes. Neither is likely to block typical shaders, but both are still open and the change has not yet been tried in a game.
Pre-merge checks:
4
Full details: Title check
Explanation
The title names the main change: bounding waterfall image keys by their clamp on the selected lanes. The raw summary shows SelectedSelectorDomain now limits the selected lane maximum using the guarded SelectorMaximum, which matches the title. The title is specific and uses the same terms as the code changes and the objectives.
Full details: Description check
Explanation
The description covers the cause, the change, tests, costs, and in-game status. It describes the SelectedSelectorDomain fix, the new EXEC operand on ImageQueryDimensions and ImageQueryLod, and the test updates, all of which match the changeset.
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
Share
@coderabbitai[bot] on GitHub · 2026-11-14 17:36 UTC · original · reviewed
c977c7ef-2d50-429f-a154-42a0cda20638Reviewing files that changed from the base of the PR and between
3e226bff9dand050f7f48e5.src/graphics/shader/recompiler/frontend/translate/Memory.cppsrc/graphics/shader/recompiler/ir/opcodes/ValueOpcodes.incsrc/graphics/shader/recompiler/ir/passes/ResourceTracking.cpptests/ResourceTrackingTests.cpptests/ShaderRecompilerComputeTests.cpptests/shaderCfgTests.cppIncluded review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.
@Nawid3333 on GitHub · 2026-11-15 15:07 UTC · original
@coderabbitai The gap is real, but it is older than this PR: on main the sampler planner only runs for
ImageSampleRaw, soImageGatherRawandImageQueryLodwith a table- or waterfall-selected sampler stop the same way with or without this change. Neither #774 shader has anIMAGE_GET_LOD. I will look at gather and LOD-query samplers together, separately and with a test, rather than widen this one.@coderabbitai[bot] on GitHub · 2026-11-15 15:08 UTC · original
Analysis chain
Script executed:
Length of output: 15182
@Nawid3333I checked the planner dispatch onmainand this PR. Both invoke sampler planning only forImageSampleRawwithoutImageSampleFlagAdjust.Handling
ImageGatherRawandImageQueryLodsamplers separately makes sense. Would you like me to open a GitHub issue for that follow-up?You are interacting with an AI system.
@ -3710,1 +3710,3 @@if (result.maximum == UINT32_MAX) result.maximum = PossibleU32Bits(lane.Arg(0));if (result.maximum == UINT32_MAX)result.maximum = std::min(PossibleU32Bits(lane.Arg(0)),SelectorMaximum(lane.Arg(0), {}, depth + 1u, lane.Arg(1)));@coderabbitai[bot] on GitHub · 2026-11-24 12:10 UTC · original · edited 2026-11-24 12:33 UTC
Bound the recursive
SelectorMaximumtraversal on sharedUMin32graphs.The new
SelectedSelectorDomainfallback callsSelectorMaximumon the lane value.SelectorMaximumevaluates both operands ofUMin32andBitwiseAnd32, and it does not memoize results. A shared node used as both operands is visited once per path to it. Within the depth limit of 32, the traversal cost can grow exponentially with the number of shared levels.The inspected passes before resource tracking do not remove this shape. Constant propagation folds
UMin32only for constant operands. Dead code elimination removes unused values but does not rewrite shared expressions. MemoizeSelectorMaximumresults for each value, guard, and bounds combination, or reject the fallback when the operand graph is large.@coderabbitai[bot] on GitHub · 2026-11-24 12:10 UTC · original · reviewed
Actionable comments posted: 1
e3c52ff8-4cf4-4303-8b37-246f898ffba3Reviewing files that changed from the base of the PR and between
050f7f48e5andaf8ef59478.src/graphics/shader/recompiler/ir/opcodes/ValueOpcodes.incsrc/graphics/shader/recompiler/ir/passes/ResourceTracking.cpptests/ResourceTrackingTests.cpptests/ShaderRecompilerComputeTests.cpptests/shaderCfgTests.cppIncluded review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 8 remain after this review.
@Nawid3333 on GitHub · 2026-11-24 12:29 UTC · original
Merged main (
4e90654) into this branch. Since911f0052fa bounded handle whose every use is a sample keeps its key range astable_maximumwith an emptykey_count, so two checks intests/ResourceTrackingTests.cppstill expected the old form: the unproved packed variants ofTestUniformizedMaterialImageKeysand the sampled waterfall ofTestQueryOnlyImageWaterfall(QueryThenSample).af8ef59updates them totable_maximum16383; the query keepskey_count16384. resource_tracking_tests: the merge alone fails, withaf8ef59it passes.af8ef59changes no src file; the merge takes main's src as it is.@ -3710,1 +3710,3 @@if (result.maximum == UINT32_MAX) result.maximum = PossibleU32Bits(lane.Arg(0));if (result.maximum == UINT32_MAX)result.maximum = std::min(PossibleU32Bits(lane.Arg(0)),SelectorMaximum(lane.Arg(0), {}, depth + 1u, lane.Arg(1)));@Nawid3333 on GitHub · 2026-11-24 12:33 UTC · original
The walk over both
UMin32/BitwiseAnd32operands without memoization is inSelectorMaximumon main already (4e90654), and main calls it directly on keys (SelectorMaximum(key)at lines 3100, 3356 and 3570). This PR adds one more entry into that walk: the lane value of aReadFirstLane, which main bounds byFiniteSelectorDomainand, when that finds no bound, only byPossibleU32Bits. It passes the caller'sdepthinto it (depth + 1uhere, andSelectedSelectorDomain(*inst, guard, depth)from theReadFirstLanecase), so it stays within the same 32-level limit, and the cost per value is that of main's walk. MemoizingSelectorMaximumwould change it for all of its callers, so I'd keep that out of this PR.@ -3710,1 +3710,3 @@if (result.maximum == UINT32_MAX) result.maximum = PossibleU32Bits(lane.Arg(0));if (result.maximum == UINT32_MAX)result.maximum = std::min(PossibleU32Bits(lane.Arg(0)),SelectorMaximum(lane.Arg(0), {}, depth + 1u, lane.Arg(1)));@coderabbitai[bot] on GitHub · 2026-11-24 12:35 UTC · original
Analysis chain
Script executed:
Length of output: 9021
@Nawid3333You’re right:SelectorMaximumalready traverses both operands without memoization onmain. This PR adds a path into that traversal forReadFirstLanevalues when finite-domain analysis finds no maximum.The shared depth limit prevents deeper recursion, but it does not prevent repeated visits to shared nodes. I accept your decision to defer memoization. I’m not marking the finding as addressed.
You are interacting with an AI system.
@ -8165,0 +8241,4 @@? table->table_maximum == 0x3fffu && table->table_scalar && table->key_count.IsEmpty(): table->key_count == Value(0x4000u));}) && std::ranges::count_if(plan.info.images, [](const auto &image) { return image.sample_access_only; }) ==(variant == Variant::QueryThenSample ? 1 : 0),@coderabbitai[bot] on GitHub · 2026-11-26 23:35 UTC · original
Require the query resource in
QueryThenSample.If tracking drops the query resource and retains only the sample-only resource, this assertion still passes.
!images.empty()andall_ofalso accept that incorrect plan.Assert that a non-sample-only query resource remains, in addition to the existing sample-only count. This makes the test detect a lost query without rejecting the intended query-plus-sample plan.
@coderabbitai[bot] on GitHub · 2026-11-26 23:35 UTC · original · reviewed
Actionable comments posted: 1
a3416e97-89f2-40e2-b428-4791b968552fReviewing files that changed from the base of the PR and between
af8ef59478and80e34cab11.src/graphics/shader/recompiler/ir/passes/ResourceTracking.cpptests/ResourceTrackingTests.cpptests/ShaderRecompilerComputeTests.cpptests/shaderCfgTests.cppIncluded review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.