tombox-xshader: translate image_gather4_l with an explicit LOD #196

Closed
GDAgent wants to merge 1 commit from upstream-pr/196 into main
GDAgent commented 2026-11-23 00:42:05 +00:00 (Migrated from github.com)

Imported from omonkeyz/xbox-emulator. Originally posted by GDAgent on 2026-11-23T00:42:05Z.


Library / area: tombox-xshader (hlsl.rs MIMG 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_o gather the 2x2 footprint of one channel from the mip chosen by an explicit LOD. HLSL has no Gather with 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, which d3d12_xs does not bind yet, so that route is not available.

Changes: 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.

Known differences, stated rather than hidden: 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:

  • 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-fast on current main: 292 passed, 0 failed (WARP).
  • Native Windows 11; not run under Proton.

Not verified in a title: in The Matrix Awakens a compute shader with image_gather4_l was refused before (PSO creation E_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-xshader passes
  • No game or console files and no binaries included
  • No fake success; anything not possible is left missing with a note
<!-- greyfinch-sync:omonkeyz/xbox-emulator:issue:6549939408 --> Imported from [omonkeyz/xbox-emulator](https://github.com/omonkeyz/xbox-emulator/pull/196). Originally posted by [GDAgent](https://github.com/GDAgent) on 2026-11-23T00:42:05Z. --- **Library / area:** `tombox-xshader` (`hlsl.rs` MIMG 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_o` gather the 2x2 footprint of one channel from the mip chosen by an explicit LOD. HLSL has no `Gather` with 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, which `d3d12_xs` does not bind yet, so that route is not available. Changes: 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. Known differences, stated rather than hidden: 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: - `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-fast` on current `main`: 292 passed, 0 failed (WARP). - Native Windows 11; not run under Proton. **Not verified in a title:** in The Matrix Awakens a compute shader with `image_gather4_l` was refused before (PSO creation `E_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. - [x] `cargo test -p tombox-xshader` passes - [x] No game or console files and no binaries included - [x] No fake success; anything not possible is left `missing` with a note
omonkeyz commented 2026-11-23 00:55:51 +00:00 (Migrated from github.com)

Imported from omonkeyz/xbox-emulator. Originally posted by omonkeyz on 2026-11-23T00:55:51Z.


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:

  1. Where the sampler descriptor (S#) is visible to the shader, use its LOD_BIAS, MIN_LOD and MAX_LOD when choosing the footprint mip. The S# words are in SGPRs, and the mip pick already happens in the shader. If the S# can't be known, keep the current behaviour, but don't leave the gap silent: log the shader hash once, and add an OPEN entry to docs/BLOCKERS.md, "gather4_l footprint ignores sampler bias/clamp".
  2. Add a WARP exec test with a non-zero LOD clamp (MinLOD/MaxLOD) or bias that shows which mip gets chosen.

Push to this branch when it's ready and I'll apply it the same way (cherry-picked, with your authorship kept).

<!-- greyfinch-sync:omonkeyz/xbox-emulator:comment:7088744118 --> Imported from [omonkeyz/xbox-emulator](https://github.com/omonkeyz/xbox-emulator/pull/196#issuecomment-7088744118). Originally posted by [omonkeyz](https://github.com/omonkeyz) on 2026-11-23T00:55:51Z. --- 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: 1. Where the sampler descriptor (S#) is visible to the shader, use its LOD_BIAS, MIN_LOD and MAX_LOD when choosing the footprint mip. The S# words are in SGPRs, and the mip pick already happens in the shader. If the S# can't be known, keep the current behaviour, but don't leave the gap silent: log the shader hash once, and add an OPEN entry to docs/BLOCKERS.md, "gather4_l footprint ignores sampler bias/clamp". 2. Add a WARP exec test with a non-zero LOD clamp (MinLOD/MaxLOD) or bias that shows which mip gets chosen. Push to this branch when it's ready and I'll apply it the same way (cherry-picked, with your authorship kept).
omonkeyz commented 2026-11-23 00:56:33 +00:00 (Migrated from github.com)

Imported from omonkeyz/xbox-emulator. Originally posted by omonkeyz on 2026-11-23T00:56:33Z.


Applied in 2e981c5 (merged in 2eda8b0) 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.

<!-- greyfinch-sync:omonkeyz/xbox-emulator:comment:7088752927 --> Imported from [omonkeyz/xbox-emulator](https://github.com/omonkeyz/xbox-emulator/pull/196#issuecomment-7088752927). Originally posted by [omonkeyz](https://github.com/omonkeyz) on 2026-11-23T00:56:33Z. --- Applied in 2e981c5 (merged in 2eda8b0) 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

Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
omonkeyz/xbox-emulator!196
No description provided.