tombox-xshader: apply the S# LOD bias and clamp to the gather4_l footprint mip (follow-up of #196) #306

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

@⁠QuentinDanblon on GitHub · 2026-11-23 17:13 UTC · original
QuentinDanblon:pr/gather4-sampler-lod → main · +217 −3 · 3 files · 1 commits · closed without merging

Library / area: tombox-xshader (src/hlsl.rs, tests/exec_mem/mimg.rs) and d3d12_xs (src/shader_xlat.rs, log only). The follow-up you asked for on #196 ("gather4_l footprint ignores sampler bias/clamp" in docs/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_desc already 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_o picked 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 from clamp(lod + LOD_BIAS, MIN_LOD, MAX_LOD) with the existing nearest-level rule and view clamp, and each tap reads SampleLevel(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_blind lists 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 that gather_lod_blind is set, and they show WARP does apply the sampler's MipLODBias and MinLOD/MaxLOD to SampleLevel.
  • image_mimg_gather_explicit_lod_sampler_refusals: the five refusals.
  • d3d12_xs shader_xlat::tests::gather_lod_blind_note_is_once_per_shader_hash.

On 73a988d (base f89e410, r32l): the gather tests pass on WARP and with TOMBOX_EXEC_HW=1 on an RTX 5090 (the existing image_mimg_gather_explicit_lod included, 23 cases, all rerun with runtime inputs). Full tombox-xshader suite on WARP: exec 130 passed 2 ignored, lib 116 passed 3 ignored, all other targets pass. Mutation: clamp(l + b, lo, hi) replaced by l + b fails 4 cases (MAX_LOD 1, MIN_LOD 2, MIN 1 / MAX 2, _l_o with 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

  1. In the common case the S# is not visible: SGPRs loaded from a descriptor table are symbolic in the translated HLSL. So "S# in SGPRs" covers constant S# only (Durango static samplers, or rewritten S# words); table samplers stay "not visible" and are logged. Is that the split you meant?
  2. The PC sampler is assumed to carry the S# values. That holds for heap samplers (exact conversion in title_heap). For root-signature static samplers, the title's original float may differ from the S# u4.8/s6.8, and if a shader rewrites S# words read from the heap, the PC sampler keeps the heap values.
  3. LOD_BIAS on _L and the bias/clamp order are not documented for RDNA2; D3D order used. Worth a check on an RDNA2 card if anyone has one.
  4. The T# resource MIN_LOD (bits 51:40) is not handled here.
  5. The BLOCKERS entry can shrink to the "S# not visible" case; I did not edit it.
  • cargo test -p tombox-xshader passes (see above)
  • No game or console files and no binaries included
  • No fake success; unmodelled S# fields are refused, the blind case is logged

🤖 Generated with Claude Code

**@⁠QuentinDanblon** on GitHub · 2026-11-23 17:13 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/306) `QuentinDanblon:pr/gather4-sampler-lod` → `main` · +217 −3 · 3 files · 1 commits · closed without merging **Library / area:** `tombox-xshader` (`src/hlsl.rs`, `tests/exec_mem/mimg.rs`) and `d3d12_xs` (`src/shader_xlat.rs`, log only). The follow-up you asked for on #196 ("gather4_l footprint ignores sampler bias/clamp" in `docs/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_desc` already 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_o` picked 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 from `clamp(lod + LOD_BIAS, MIN_LOD, MAX_LOD)` with the existing nearest-level rule and view clamp, and each tap reads `SampleLevel(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_blind` lists 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 that `gather_lod_blind` is set, and they show WARP does apply the sampler's MipLODBias and MinLOD/MaxLOD to `SampleLevel`. - `image_mimg_gather_explicit_lod_sampler_refusals`: the five refusals. - d3d12_xs `shader_xlat::tests::gather_lod_blind_note_is_once_per_shader_hash`. On `73a988d` (base `f89e410`, r32l): the gather tests pass on WARP and with `TOMBOX_EXEC_HW=1` on an RTX 5090 (the existing `image_mimg_gather_explicit_lod` included, 23 cases, all rerun with runtime inputs). Full `tombox-xshader` suite on WARP: exec 130 passed 2 ignored, lib 116 passed 3 ignored, all other targets pass. Mutation: `clamp(l + b, lo, hi)` replaced by `l + b` fails 4 cases (MAX_LOD 1, MIN_LOD 2, MIN 1 / MAX 2, `_l_o` with 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 1. In the common case the S# is not visible: SGPRs loaded from a descriptor table are symbolic in the translated HLSL. So "S# in SGPRs" covers constant S# only (Durango static samplers, or rewritten S# words); table samplers stay "not visible" and are logged. Is that the split you meant? 2. The PC sampler is assumed to carry the S# values. That holds for heap samplers (exact conversion in title_heap). For root-signature static samplers, the title's original float may differ from the S# u4.8/s6.8, and if a shader rewrites S# words read from the heap, the PC sampler keeps the heap values. 3. LOD_BIAS on `_L` and the bias/clamp order are not documented for RDNA2; D3D order used. Worth a check on an RDNA2 card if anyone has one. 4. The T# resource MIN_LOD (bits 51:40) is not handled here. 5. The BLOCKERS entry can shrink to the "S# not visible" case; I did not edit it. - [x] `cargo test -p tombox-xshader` passes (see above) - [x] No game or console files and no binaries included - [x] No fake success; unmodelled S# fields are refused, the blind case is logged 🤖 Generated with [Claude Code](https://claude.com/claude-code)
image_gather4_l / _l_o / _c_l / _c_l_o chose the footprint mip from the
unbiased explicit LOD (docs/BLOCKERS.md, "gather4_l footprint ignores
sampler bias/clamp", follow-up of #196).

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 is then picked from
  L = clamp(lod + LOD_BIAS, MIN_LOD, MAX_LOD)
with the existing nearest-level rule and view clamp, and each tap is read
with SampleLevel(m - LOD_BIAS). d3d12_xs builds the PC sampler from the
same S# words (title_heap::sampler_desc: exact bias/256, MinLOD, MaxLOD),
so D3D's own bias and clamp on SampleLevel land exactly on mip m.
Refused explicitly (translation error) when the PC sampler cannot be
steered onto one mip or the field is not modelled:
- MIN_LOD or MAX_LOD (other than 0xfff) with a fractional part;
- MIN_LOD > MAX_LOD;
- MIP_FILTER other than point or linear (0 = base level only);
- non-zero LOD_BIAS_SEC (scaled by the T# perf_mod).

S# not visible (loaded from a descriptor table, or computed at run
time): the previous behaviour is kept. Translated.gather_lod_blind lists
the ISA offsets, and d3d12_xs logs "GATHER4_L: hash=..." once per shader
hash when it translates the shader at run time. Shaders served from the
prelaunch pack are not translated live and are not logged.

S# fields, from the AMD "RDNA 2" Instruction Set Architecture reference
guide, section 8.2, image sampler (S#) table: MIN_LOD bits 43:32 and
MAX_LOD 55:44, u4.8, in resource view space; LOD_BIAS bits 75:64 plus
77:76, s6.8 (14-bit signed, 8 fraction bits); LOD_BIAS_SEC 83:78;
MIP_FILTER 91:90 (0 none/base level, 1 point, 2 linear). The repo's
d3d12_xs sampler conversion uses the same positions. Mesa and LLVM
sources were not available offline and were not checked for this change.

Order: the RDNA2 guide says only that _L uses the given LOD instead of
the computed one; it does not say whether S# LOD_BIAS applies to _L, nor
the order of bias, clamp and base level. The D3D order is used: bias,
then MIN_LOD/MAX_LOD, then the view's mip range. WARP and the RTX 5090
apply the sampler MipLODBias and MinLOD/MaxLOD to SampleLevel this way
for the cases tested (the not-visible cases below).

Tests (tests/exec_mem/mimg.rs):
- image_mimg_gather_explicit_lod_sampler_bias_and_clamp, 22 cases:
  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
  filters; _l_o offsets; 2D array; _c_l depth compare. The 15 visible cases
  check the gathered texels of the expected mip. Four run-time S# cases
  and three table S# cases check the previous behaviour (footprint on the
  unbiased mip, taps read by the PC sampler at its biased/clamped mip)
  and that gather_lod_blind is set.
- image_mimg_gather_explicit_lod_sampler_refusals: the five refusals.
- d3d12_xs shader_xlat gather_lod_blind_note_is_once_per_shader_hash.
WARP: exec 130 passed, full tombox-xshader suite green. TOMBOX_EXEC_HW=1
(RTX 5090): the three gather explicit-LOD tests pass. Mutation: dropping
the clamp() makes 4 cases fail (MAX_LOD 1, MIN_LOD 2, MIN 1 / MAX 2, and
the offset case with MAX_LOD 1).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 06:18:01 +00:00
github-bot closed this pull request 2026-11-26 06:18:01 +00:00
Author
Owner

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

  • This PR, blind case. When the S# is loaded from a descriptor table, image_gather4_l still picks its footprint mip without LOD_BIAS / MIN_LOD / MAX_LOD. gather_lod_blind only logs it.
  • image_get_resinfo on 2D arrays (BLOCKERS, "array layers are not minified"). The layer word should be max(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_dwordxN from 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_view already uses img.base_level as the view's most-detailed mip, but nothing passes that value to the shader.
  • The two side channels that exist, TB_MIP_SPACE and TB_GMEM_SPACE, are not bound by d3d12_xs yet. mip_bind::Needs::refusal refuses shaders that need them.

A proposal, if you want one:

  1. Translated lists the slots whose descriptor words a shader needs, in the same way as mip_srvs and needs_gmem.
  2. The shader declares a small buffer in a new space and reads those words from it. For resinfo, that is only BASE_LEVEL.
  3. d3d12_xs fills that buffer when it builds the host views for a bound table, since it already decodes the T# there. Until it does, d3d12_xs refuses the shader with a reason, as Needs::refusal already does. Nothing is reported as success when it is not.

Three questions:

  • Would you take a PR like that?
  • If so, which channel do you prefer: a per-slot words buffer filled at bind time, or a read of the title's descriptor heap through the gmem buffer once that is bound?
  • Is this area already being worked on by the root-signature / device_wrap.rs owner, in which case I keep out of it?

If the answer is "not now", I will leave both gaps documented as they are.

**@⁠QuentinDanblon** on GitHub · 2026-11-24 08:14 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/306#issuecomment-7125900244) 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: - **This PR, blind case.** When the S# is loaded from a descriptor table, `image_gather4_l` still picks its footprint mip without LOD_BIAS / MIN_LOD / MAX_LOD. `gather_lod_blind` only logs it. - **`image_get_resinfo` on 2D arrays** (BLOCKERS, "array layers are not minified"). The layer word should be `max(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_dwordxN` from 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_view` already uses `img.base_level` as the view's most-detailed mip, but nothing passes that value to the shader. - The two side channels that exist, `TB_MIP_SPACE` and `TB_GMEM_SPACE`, are not bound by d3d12_xs yet. `mip_bind::Needs::refusal` refuses shaders that need them. A proposal, if you want one: 1. `Translated` lists the slots whose descriptor words a shader needs, in the same way as `mip_srvs` and `needs_gmem`. 2. The shader declares a small buffer in a new space and reads those words from it. For resinfo, that is only BASE_LEVEL. 3. d3d12_xs fills that buffer when it builds the host views for a bound table, since it already decodes the T# there. Until it does, d3d12_xs refuses the shader with a reason, as `Needs::refusal` already does. Nothing is reported as success when it is not. Three questions: - Would you take a PR like that? - If so, which channel do you prefer: a per-slot words buffer filled at bind time, or a read of the title's descriptor heap through the gmem buffer once that is bound? - Is this area already being worked on by the root-signature / `device_wrap.rs` owner, in which case I keep out of it? If the answer is "not now", I will leave both gaps documented as they are.
Author
Owner

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

  • LOD_BIAS applies to _l. The level comes from clamp(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.
  • One difference from the nearest-level rule here: an exact .5 rounds up on hardware. floor(rel + 0.5) gives LOD 0.5 → level 1 and 1.5 → level 2. l > 0.5 ? ceil(l + 0.5) − 1 : 0 picks the lower level at every exact .5. Since this PR already rewrites that line, it may be the natural place to change it.
  • MIP_FILTER none ignores the LOD and the S# fields and reads BASE_LEVEL, raised to floor(T# MIN_LOD) if that is higher.
  • Question 4, the T# MIN_LOD: it is an absolute level, applied as max(rel, MIN_LOD − BASE_LEVEL) after the S# clamp. image_gather4_lz returns 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.)

**@⁠SchmashinPrunes** on GitHub · 2026-11-24 13:51 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/306#issuecomment-7133449178) 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: - **LOD_BIAS applies to `_l`.** The level comes from `clamp(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. - **One difference from the nearest-level rule here:** an exact .5 rounds **up** on hardware. `floor(rel + 0.5)` gives LOD 0.5 → level 1 and 1.5 → level 2. `l > 0.5 ? ceil(l + 0.5) − 1 : 0` picks the lower level at every exact .5. Since this PR already rewrites that line, it may be the natural place to change it. - **MIP_FILTER none** ignores the LOD and the S# fields and reads BASE_LEVEL, raised to floor(T# MIN_LOD) if that is higher. - **Question 4, the T# MIN_LOD:** it is an absolute level, applied as `max(rel, MIN_LOD − BASE_LEVEL)` after the S# clamp. `image_gather4_lz` returns 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.)
Author
Owner

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

**@⁠omonkeyz** on GitHub · 2026-11-25 13:49 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/306#issuecomment-7164333969) 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

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