tombox-xshader: translate image_gather4_l with an explicit LOD #196
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!196
Loading…
Reference in a new issue
No description provided.
Delete branch "gh-pr/196/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?
@GDAgent on GitHub · 2026-11-23 00:42 UTC · original
GDAgent:fix/xshader-gather4-lod→main· +102 −2 · 2 files · 1 commits · closed without mergingLibrary / area:
tombox-xshader(hlsl.rsMIMG gather,tests/exec_mem/mimg.rs)Exports changed (exports.txt rows): none
Source of the behaviour: RDNA2 ISA:
image_gather4_l,image_gather4_l_o,image_gather4_c_l,image_gather4_c_l_ogather the 2x2 footprint of one channel from the mip chosen by an explicit LOD. HLSL has noGatherwith a mip level.These four opcodes on 2D and 2D-array images were refused (
gather4 with explicit LOD/bias unsupported). The existing implicit-LOD path gathers from a per-mip SRV view, whichd3d12_xsdoes not bind yet, so that route is not available.Changes: the explicit-LOD forms are lowered without per-mip views.
i = floor(u * w - 0.5),j = floor(v * h - 0.5)on that mip, plus the texel offset for the_oforms.SampleLevel(SampleCmpLevelfor_c, SM 6.7) at its texel centre on that mip, in gather order (i, j+1), (i+1, j+1), (i+1, j), (i, j), and dmask picks the channel.Sampling at a texel centre with an integer LOD returns that texel for point and linear filters, and the sampler still applies its address mode per texel, so wrap, mirror, clamp and border match a hardware gather.
Known differences, stated rather than hidden: the result differs from a hardware gather when the sampler has a non-zero
MipLODBiasor aMinLOD/MaxLODthat excludes the chosen mip, because the footprint is computed for the unbiased level. Cube, 1D and 3D images and unnormalised coordinates are still refused.Tests:
exec_mem::mimg::image_mimg_gather_explicit_lod, 23 cases on WARP: LOD rounding and clamping, all four channels, wrap addressing at both edges with point and linear filters, texel offsets, a 2D array, and depth compare. Swapping the tap order fails 38 checks.cargo test -p tombox-xshader --no-fail-faston currentmain: 292 passed, 0 failed (WARP).Not verified in a title: in The Matrix Awakens a compute shader with
image_gather4_lwas refused before (PSO creationE_FAIL); it now translates and compiles, and a later run logged no translation failures. The title's sampler settings for that shader (bias, LOD clamps) and the correctness of its output were not checked.cargo test -p tombox-xshaderpassesmissingwith a noteimage_gather4_l, image_gather4_l_o, image_gather4_c_l and image_gather4_c_l_o on 2D and 2D-array images were refused ("gather4 with explicit LOD/bias unsupported"). HLSL has no Gather with a mip level, so the existing implicit-LOD path gathers from a per-mip SRV view, which d3d12_xs does not bind yet. The explicit-LOD forms are lowered without per-mip views: - the mip is the nearest level of the given LOD (the same rule as the implicit-LOD gather: 0 for LOD <= 0.5, else ceil(LOD + 0.5) - 1), clamped to the view's mip count; - the 2x2 footprint is i = floor(u * w - 0.5), j = floor(v * h - 0.5) on that mip, plus the texel offset for the _o forms; - each of the four texels is read with SampleLevel (SampleCmpLevel for _c, SM 6.7) at its texel centre on that mip, in gather order (i, j+1), (i+1, j+1), (i+1, j), (i, j), and dmask picks the channel. Sampling at a texel centre with an integer LOD returns that texel for point and linear filters, and the sampler still applies its address mode per texel, so wrap, mirror, clamp and border match a hardware gather. The result differs from a hardware gather when the sampler has a non-zero MipLODBias or a MinLOD/MaxLOD that excludes the chosen mip, because the footprint is computed for the unbiased level. Cube, 1D and 3D images and unnormalised coordinates are still refused. Tests: tests/exec_mem/mimg.rs image_mimg_gather_explicit_lod, 23 cases on WARP: LOD rounding and clamping, all four channels, wrap addressing at both edges with point and linear filters, texel offsets, a 2D array, and depth compare. Swapping the tap order fails 38 checks.@omonkeyz on GitHub · 2026-11-23 00:55 UTC · original
Thanks, this looks right and the tests pass here: exec mimg 12/12 on WARP, and the new test passes with TOMBOX_EXEC_HW=1 too. Two things are needed before it can merge:
Push to this branch when it's ready and I'll apply it the same way (cherry-picked, with your authorship kept).
@omonkeyz on GitHub · 2026-11-23 00:56 UTC · original
Applied in
2e981c5(merged in2eda8b0) with your authorship kept, thanks. The sampler bias and LOD-clamp gap you described is now an OPEN entry in docs/BLOCKERS.md (9a77dd6). A follow-up PR would be welcome. It should apply the S# LOD_BIAS / MIN_LOD / MAX_LOD to the footprint mip where the descriptor is visible, and log the shader hash once where it isn't. Please include a WARP exec test with a non-zero clamp.Pull request closed