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

Closed
QuentinDanblon wants to merge 1 commit from upstream-pr/306 into main
QuentinDanblon commented 2026-11-23 17:13:38 +00:00 (Migrated from github.com)

Imported from omonkeyz/xbox-emulator. Originally posted by QuentinDanblon on 2026-11-23T17:13:38Z.


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

<!-- greyfinch-sync:omonkeyz/xbox-emulator:issue:6564703859 --> Imported from [omonkeyz/xbox-emulator](https://github.com/omonkeyz/xbox-emulator/pull/306). Originally posted by [QuentinDanblon](https://github.com/QuentinDanblon) on 2026-11-23T17:13:38Z. --- **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)
QuentinDanblon commented 2026-11-24 08:14:47 +00:00 (Migrated from github.com)

Imported from omonkeyz/xbox-emulator. Originally posted by QuentinDanblon on 2026-11-24T08:14:47Z.


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.

<!-- greyfinch-sync:omonkeyz/xbox-emulator:comment:7125900244 --> Imported from [omonkeyz/xbox-emulator](https://github.com/omonkeyz/xbox-emulator/pull/306#issuecomment-7125900244). Originally posted by [QuentinDanblon](https://github.com/QuentinDanblon) on 2026-11-24T08:14:47Z. --- 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.
SchmashinPrunes commented 2026-11-24 13:51:50 +00:00 (Migrated from github.com)

Imported from omonkeyz/xbox-emulator. Originally posted by SchmashinPrunes on 2026-11-24T13:51:50Z.


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.)

<!-- greyfinch-sync:omonkeyz/xbox-emulator:comment:7133449178 --> Imported from [omonkeyz/xbox-emulator](https://github.com/omonkeyz/xbox-emulator/pull/306#issuecomment-7133449178). Originally posted by [SchmashinPrunes](https://github.com/SchmashinPrunes) on 2026-11-24T13:51:50Z. --- 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.)
omonkeyz commented 2026-11-25 13:49:06 +00:00 (Migrated from github.com)

Imported from omonkeyz/xbox-emulator. Originally posted by omonkeyz on 2026-11-25T13:49:06Z.


Applied on main in 6f2b507 (cherry-picked onto the current history with your authorship kept; comment lines stripped per CONTRIBUTING). Thank you.

<!-- greyfinch-sync:omonkeyz/xbox-emulator:comment:7164333969 --> Imported from [omonkeyz/xbox-emulator](https://github.com/omonkeyz/xbox-emulator/pull/306#issuecomment-7164333969). Originally posted by [omonkeyz](https://github.com/omonkeyz) on 2026-11-25T13:49:06Z. --- 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

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!306
No description provided.