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

Closed
github-bot wants to merge 1 commit from gh-pr/196/head into gh-pr/196/base
Owner

@⁠GDAgent on GitHub · 2026-11-23 00:42 UTC · original
GDAgent:fix/xshader-gather4-lod → main · +102 −2 · 2 files · 1 commits · closed without merging

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
**@⁠GDAgent** on GitHub · 2026-11-23 00:42 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/196) `GDAgent:fix/xshader-gather4-lod` → `main` · +102 −2 · 2 files · 1 commits · closed without merging **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
image_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.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 06:13:26 +00:00
github-bot closed this pull request 2026-11-26 06:13:26 +00:00
Author
Owner

@⁠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:

  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** on GitHub · 2026-11-23 00:55 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/196#issuecomment-7088744118) 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).
Author
Owner

@⁠omonkeyz on GitHub · 2026-11-23 00:56 UTC · original

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.

**@⁠omonkeyz** on GitHub · 2026-11-23 00:56 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/196#issuecomment-7088752927) 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

This discussion has been locked. Commenting is limited to contributors.
No description provided.