tombox-xshader: apply the S# LOD bias and clamp to the gather4_l footprint mip (follow-up of #196) #306
No reviewers
Labels
No labels
accessibility
bug
documentation
duplicate
enhancement
good first issue
help wanted
invalid
library
question
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/omonkeyz--xbox-emulator!306
Loading…
Reference in a new issue
No description provided.
Delete branch "gh-pr/306/head"
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?
@QuentinDanblon on GitHub · 2026-11-23 17:13 UTC · original
QuentinDanblon:pr/gather4-sampler-lod→main· +217 −3 · 3 files · 1 commits · closed without mergingLibrary / area:
tombox-xshader(src/hlsl.rs,tests/exec_mem/mimg.rs) andd3d12_xs(src/shader_xlat.rs, log only). The follow-up you asked for on #196 ("gather4_l footprint ignores sampler bias/clamp" indocs/BLOCKERS.md).Exports changed (exports.txt rows): none
Source of the behaviour: AMD "RDNA 2" ISA guide, section 8.2, S# table: MIN_LOD bits 43:32 (u4.8), MAX_LOD 55:44 (u4.8), LOD_BIAS 75:64 + 77:76 (s6.8), LOD_BIAS_SEC 83:78, MIP_FILTER 91:90. The same positions
d3d12_xs::title_heap::sampler_descalready uses. The guide does not say whether the S# LOD_BIAS applies to_L, nor the order of bias, clamp and base level; I took the D3D order (bias, then MinLOD/MaxLOD, then the view's mip range) and say so here. No third-party code.What it does
image_gather4_l/_l_o/_c_l/_c_l_opicked the footprint mip from the unbiased explicit LOD.S# visible (dwords 1 and 2 of the sampler register are constants,
s_mov_b32/s_movk_i32, in the same basic block as the gather): the mip comes fromclamp(lod + LOD_BIAS, MIN_LOD, MAX_LOD)with the existing nearest-level rule and view clamp, and each tap readsSampleLevel(m - LOD_BIAS). d3d12_xs builds the PC sampler from the same S# words (exact bias/256, MinLOD, MaxLOD), so D3D's own bias and clamp land exactly on mip m.Refused with a translation error, where the PC sampler cannot be steered onto one mip or the field is not modelled: fractional MIN_LOD or MAX_LOD (except MAX_LOD 0xfff), MIN_LOD > MAX_LOD, MIP_FILTER other than point or linear, non-zero LOD_BIAS_SEC.
S# not visible (loaded from a descriptor table, or computed at run time): behaviour unchanged.
Translated.gather_lod_blindlists the ISA offsets, and d3d12_xs logs once per shader hash:GATHER4_L: hash=... image_gather4_l at ISA [...] picks its footprint mip without the sampler LOD_BIAS/MIN_LOD/MAX_LOD: the S# is not visible to the translator. Shaders served from the prelaunch pack are not translated at run time, so they are not logged.Tests
image_mimg_gather_explicit_lod_sampler_bias_and_clamp, 22 cases. 15 with a visible S#: bias +1, +1.5, -0.75, +3, +0.5, -1; MAX_LOD 1 and 0; MIN_LOD 2 and 5 (past the last mip); MIN 1 / MAX 2 with bias; point and linear mip filter;_l_o, 2D array,_c_l. 7 with the S# made run-time or loaded from a table: they check the old behaviour and thatgather_lod_blindis set, and they show WARP does apply the sampler's MipLODBias and MinLOD/MaxLOD toSampleLevel.image_mimg_gather_explicit_lod_sampler_refusals: the five refusals.shader_xlat::tests::gather_lod_blind_note_is_once_per_shader_hash.On
73a988d(basef89e410, r32l): the gather tests pass on WARP and withTOMBOX_EXEC_HW=1on an RTX 5090 (the existingimage_mimg_gather_explicit_lodincluded, 23 cases, all rerun with runtime inputs). Fulltombox-xshadersuite on WARP: exec 130 passed 2 ignored, lib 116 passed 3 ignored, all other targets pass. Mutation:clamp(l + b, lo, hi)replaced byl + bfails 4 cases (MAX_LOD 1, MIN_LOD 2, MIN 1 / MAX 2,_l_owith MAX_LOD 1), restored.pr_gate.py --base origin/main: 0 problems.Overlap: #248 (pins the existing gather4 refusals) and #286 (exact pixel cases for sample and gather4_lz) do not do this; the only shared file is
tests/exec_mem/mimg.rs.Questions for you
_Land the bias/clamp order are not documented for RDNA2; D3D order used. Worth a check on an RDNA2 card if anyone has one.cargo test -p tombox-xshaderpasses (see above)🤖 Generated with Claude Code
@QuentinDanblon on GitHub · 2026-11-24 08:14 UTC · original
A design question before I write more code. Two open gaps need a descriptor field at run time, and the translator has no way to get one today:
image_gather4_lstill picks its footprint mip without LOD_BIAS / MIN_LOD / MAX_LOD.gather_lod_blindonly logs it.image_get_resinfoon 2D arrays (BLOCKERS, "array layers are not minified"). The layer word should bemax(1, layers >> (BASE_LEVEL + level)), and BASE_LEVEL is T# dword 3, bits 15:12. The entry says it "cannot be computed without a guess", so I have left it alone.Why neither can be fixed in the translator alone:
s_load_dwordxNfrom a root table is resolved statically (// (descriptor fetch from root table — resolved statically)), so the T#/S# words never exist in the HLSL.title_heap::image_viewalready usesimg.base_levelas the view's most-detailed mip, but nothing passes that value to the shader.TB_MIP_SPACEandTB_GMEM_SPACE, are not bound by d3d12_xs yet.mip_bind::Needs::refusalrefuses shaders that need them.A proposal, if you want one:
Translatedlists the slots whose descriptor words a shader needs, in the same way asmip_srvsandneeds_gmem.Needs::refusalalready does. Nothing is reported as success when it is not.Three questions:
device_wrap.rsowner, in which case I keep out of it?If the answer is "not now", I will leave both gaps documented as they are.
@SchmashinPrunes on GitHub · 2026-11-24 13:51 UTC · original
On question 3: I measured this on RDNA2 silicon. The probe and the BLOCKERS update are in #361. The full rule is there. In short, on gfx1036:
_l. The level comes fromclamp(lod + LOD_BIAS, S# MIN_LOD, S# MAX_LOD). The S# clamp follows the bias and counts from BASE_LEVEL, then the result is clamped to [BASE_LEVEL, LAST_LEVEL]. That is the D3D order this PR uses. It matched all 72,900 recorded gather4_l cases.floor(rel + 0.5)gives LOD 0.5 → level 1 and 1.5 → level 2.l > 0.5 ? ceil(l + 0.5) − 1 : 0picks the lower level at every exact .5. Since this PR already rewrites that line, it may be the natural place to change it.max(rel, MIN_LOD − BASE_LEVEL)after the S# clamp.image_gather4_lzreturns all zeros when floor(T# MIN_LOD) > BASE_LEVEL.On the design question in your last comment: the resinfo half may not need the new channel. On gfx1036 a 2D array's layer word is not minified. DEPTH + 1 comes back at every level and BASE_LEVEL (100 layers give 100 at level 3), so main's unminified host array size is already the RDNA2 value. The same PR closes that BLOCKERS line with the measurement.
(Measured with an AI assistant, Claude.)
@omonkeyz on GitHub · 2026-11-25 13:49 UTC · original
Applied on main in
6f2b507(cherry-picked onto the current history with your authorship kept; comment lines stripped per CONTRIBUTING). Thank you.Pull request closed