Direct DXIL: clamp mip level and guard zero dims in image_sample_*_o offsets #3341

Open
Kalmarv wants to merge 1 commit from upstream-pr/3341 into main
Kalmarv commented 2026-11-29 09:30:44 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by Kalmarv on 2026-11-29T09:30:44Z.


Clamp the mip level queried for texel-offset scaling in Direct DXIL image_sample_*_o (crates/xcl/src/gpu/dxil/lower_image.rs) to [0, num_mips - 1] and guard extracted dimensions with umax(size, 1) so out-of-range explicit/implicit LODs and null views never return zero dimensions and divide texel offsets by zero (NaN/+/-Inf).

What is in it

  • crates/xcl/src/gpu/dxil/lower_image.rs:
    • In mimg's ovgpr (has_o) offset scaling path, when computing texel offsets for an explicit LOD (lod = Some(l)) or pixel-shader implicit LOD (calculate_lod), query get_dimensions(tex, Some(0)) for num_mips, clamp the rounded LOD to [0, max(num_mips, 1) - 1] in both float (before ftoui, handling +Inf) and integer domains, and then query get_dimensions(tex, Some(clamped_level)).
    • Keep the single get_dimensions(tex, Some(0)) query for _lz / gather / default mip-0 cases, and clamp (w, h, d) with umax(dim, 1) across all branches so null views never divide by zero.
    • Add offset_mip_clamp_tests::sample_offset_clamps_mip_and_guards_zero_dims_in_dxil (DXIL validation + IR check for both compute IMAGE_SAMPLE_L_O and pixel IMAGE_SAMPLE_O) and offset_mip_clamp_tests::gpu_sample_l_o_with_oob_lod_offsets_at_last_mip (guarded real-GPU dispatch testing IMAGE_SAMPLE_L_O at lod = 5.0 on a 2-mip 8x8 texture across offsets (0,0), (+1,0), (0,+1), and (-1,0)).

Tested

  • Targeted Direct DXIL + RTX 5070 Ti GPU tests (cargo test -p xcl --lib -- gpu::dxil::lower_image::):
    • Before the fix: both sample_offset_clamps_mip_and_guards_zero_dims_in_dxil (only 1 getDimensions call) and gpu_sample_l_o_with_oob_lod_offsets_at_last_mip failed (lane 0 offset=(0,0) at lod=5.0 on 2-mip texture: got 1.0, expected 6.0 because GetDimensions(5) returned 0 -> 0.0/0.0 = NaN clamping to corner texel (0,0)).
    • After the fix: 8 passed; 0 failed (0.23s), with all four offset lanes reading their exact 4x4 mip-1 texels (6.0, 7.0, 10.0, 5.0).
  • Full workspace test suite on Windows 11 Pro (rustc 1.99.0, RTX 5070 Ti driver 32.0.16.1074, DXC 1.9.2609.5):
    • Command: cargo test --workspace -j 2 -- --test-threads=4 --skip test_title_runs_and_game_folder_is_untouched --skip uma_driver::tests::
    • Result: 2611 passed; 0 failed; 49 ignored (149.5s).
    • SKIPPED: 1 (1 no heap windows).

Regression run
Baseline (main ancestor f0e8fdd5c3942ddd399d5ee11384c8595c5aacff) and this PR (bb5fddc7a6f08a15e387c5f58441c48a54f34b40), 150 s on RTX 5070 Ti:

xcl-smoke v1
commit: f0e8fdd5c3942ddd399d5ee11384c8595c5aacff
gpu: NVIDIA GeForce RTX 5070 Ti
title: ea3b5e4d
seconds: 150
exit: 0x0
frames shown: 1695 (PresentX 1695)
presents per 2 s (last 3): 0 0 0
device removals: 0
crashes: 0
peak commit: 31546 MB
xcl-smoke v1
commit: bb5fddc7a6f08a15e387c5f58441c48a54f34b40
gpu: NVIDIA GeForce RTX 5070 Ti
title: ea3b5e4d
seconds: 150
exit: 0x0
frames shown: 3701 (PresentX 3701)
presents per 2 s (last 3): 60 60 17
device removals: 0
crashes: 0
peak commit: 15734 MB

Not tested

  • AMD RDNA hardware, macOS/Linux hosts, and the HLSL recomp/emit.rs backend (left untouched here).
  • test_title_runs_and_game_folder_is_untouched and uma_driver::tests:: were skipped in the workspace test run.

AI use

  • Implemented, debugged, and verified with an AI coding assistant; all code, tests, workspace results, and smoke output were reviewed and verified locally.
<!-- greyfinch-sync:ivall/xcl:issue:5549428817 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3341). Originally posted by [Kalmarv](https://github.com/Kalmarv) on 2026-11-29T09:30:44Z. --- Clamp the mip level queried for texel-offset scaling in Direct DXIL `image_sample_*_o` (`crates/xcl/src/gpu/dxil/lower_image.rs`) to `[0, num_mips - 1]` and guard extracted dimensions with `umax(size, 1)` so out-of-range explicit/implicit LODs and null views never return zero dimensions and divide texel offsets by zero (`NaN`/`+/-Inf`). **What is in it** - `crates/xcl/src/gpu/dxil/lower_image.rs`: - In `mimg`'s `ovgpr` (`has_o`) offset scaling path, when computing texel offsets for an explicit LOD (`lod = Some(l)`) or pixel-shader implicit LOD (`calculate_lod`), query `get_dimensions(tex, Some(0))` for `num_mips`, clamp the rounded LOD to `[0, max(num_mips, 1) - 1]` in both float (before `ftoui`, handling `+Inf`) and integer domains, and then query `get_dimensions(tex, Some(clamped_level))`. - Keep the single `get_dimensions(tex, Some(0))` query for `_lz` / `gather` / default mip-0 cases, and clamp `(w, h, d)` with `umax(dim, 1)` across all branches so null views never divide by zero. - Add `offset_mip_clamp_tests::sample_offset_clamps_mip_and_guards_zero_dims_in_dxil` (DXIL validation + IR check for both compute `IMAGE_SAMPLE_L_O` and pixel `IMAGE_SAMPLE_O`) and `offset_mip_clamp_tests::gpu_sample_l_o_with_oob_lod_offsets_at_last_mip` (guarded real-GPU dispatch testing `IMAGE_SAMPLE_L_O` at `lod = 5.0` on a 2-mip `8x8` texture across offsets `(0,0)`, `(+1,0)`, `(0,+1)`, and `(-1,0)`). **Tested** - Targeted Direct DXIL + RTX 5070 Ti GPU tests (`cargo test -p xcl --lib -- gpu::dxil::lower_image::`): - Before the fix: both `sample_offset_clamps_mip_and_guards_zero_dims_in_dxil` (only 1 `getDimensions` call) and `gpu_sample_l_o_with_oob_lod_offsets_at_last_mip` failed (`lane 0 offset=(0,0) at lod=5.0 on 2-mip texture: got 1.0, expected 6.0` because `GetDimensions(5)` returned `0` -> `0.0/0.0 = NaN` clamping to corner texel `(0,0)`). - After the fix: `8 passed; 0 failed` (`0.23s`), with all four offset lanes reading their exact `4x4` mip-1 texels (`6.0`, `7.0`, `10.0`, `5.0`). - Full workspace test suite on Windows 11 Pro (`rustc 1.99.0`, RTX 5070 Ti driver `32.0.16.1074`, DXC `1.9.2609.5`): - Command: `cargo test --workspace -j 2 -- --test-threads=4 --skip test_title_runs_and_game_folder_is_untouched --skip uma_driver::tests::` - Result: `2611 passed; 0 failed; 49 ignored` (`149.5s`). - `SKIPPED`: `1` (`1` `no heap windows`). **Regression run** Baseline (`main` ancestor `f0e8fdd5c3942ddd399d5ee11384c8595c5aacff`) and this PR (`bb5fddc7a6f08a15e387c5f58441c48a54f34b40`), 150 s on RTX 5070 Ti: ``` xcl-smoke v1 commit: f0e8fdd5c3942ddd399d5ee11384c8595c5aacff gpu: NVIDIA GeForce RTX 5070 Ti title: ea3b5e4d seconds: 150 exit: 0x0 frames shown: 1695 (PresentX 1695) presents per 2 s (last 3): 0 0 0 device removals: 0 crashes: 0 peak commit: 31546 MB ``` ``` xcl-smoke v1 commit: bb5fddc7a6f08a15e387c5f58441c48a54f34b40 gpu: NVIDIA GeForce RTX 5070 Ti title: ea3b5e4d seconds: 150 exit: 0x0 frames shown: 3701 (PresentX 3701) presents per 2 s (last 3): 60 60 17 device removals: 0 crashes: 0 peak commit: 15734 MB ``` **Not tested** - AMD RDNA hardware, macOS/Linux hosts, and the HLSL `recomp/emit.rs` backend (left untouched here). - `test_title_runs_and_game_folder_is_untouched` and `uma_driver::tests::` were skipped in the workspace test run. **AI use** - Implemented, debugged, and verified with an AI coding assistant; all code, tests, workspace results, and smoke output were reviewed and verified locally.
Foniass commented 2026-12-01 05:25:54 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by Foniass on 2026-12-01T05:25:54Z.


merge-check
base: d0832e614f
head: bb5fddc7a6
merge: clean
build: cargo build --workspace -> ok
tests: cargo test --workspace --no-fail-fast -- --test-threads=4 -> 2892 passed, 0 failed, 51 ignored; SKIPPED: 28 (20 no wave64, 2 no d3d12SDKLayers.dll, 1 no D3D12 debug layer, 1 no heap windows, 1 cannot disable the driver cache, 3 others; the same set as on main); failing: none
review: read the whole diff (lower_image.rs, +373/-17, ~350 lines of tests). The fix is real: on main, GetDimensions past the last mip returns 0, and the offset's 0/0 gives NaN; the level is now clamped before ftoui (so +Inf/NaN LODs are handled) and num_mips is guarded. It changes the default path: every _o sample gets 3 umax, and _l_o and implicit-LOD pixel-shader _o samples also get a second getDimensions that depends on the first, plus about 5 ALU ops. Findings: (1) the HLSL path has the same bug and isn't fixed (emit.rs ~2827-2831, texel_offset ~2551-2560), and mimg_unorm_sample (~629-648) still uses an unclamped level (dims become 1x1 there, so wrong offsets rather than NaN). (2) The second dependent query (~1185-1197) could be avoided with max(dim0 >> level, 1) from the mip-0 query; its cost isn't measured. (3) The GPU test creates its own device on the default adapter (~2141) while taking wave64 from the runner's adapter, so they can differ on multi-GPU machines (use runner::device()); its doc says 4x4 / 2x2 but the texture is 8x8 / 4x4; the WaitForSingleObject result isn't checked. (4) The DXIL test (~2104, ~2118) only checks that getDimensions appears at least twice, not that the clamp exists. (5) AI use names no tool or model, the commit area Direct DXIL: should be Recompiler:, and the suite ran with --skip filters. Fine: lz and gather stay at mip 0, cubes are refused earlier, and array layers aren't offset.
performance: 60 s smoke of a retail title, main d0832e61 vs this merge (6495bfe7): 1516 -> 1354 frames shown (89 %; last presents per 2 s: 60 60 61), peak commit 18133 -> 16741 MB, 0 device removals, 0 crashes. That frame count is the lowest of about 25 smoke runs of this title on this machine today (others 1445-1690); the title presents at a steady 60 per 2 s, so the difference is in start-up time, not necessarily this change. One run, not a measured regression.
verdict: not yet: the HLSL path keeps the same NaN bug, and the extra dependent getDimensions per _o sample is unmeasured (this run's frame count is the lowest of today's ~25 smoke runs on this machine, so worth a repeat)
hardware / tool and model: RTX 5070 Ti (driver 621.14), Ryzen 7 7800X3D, Windows 11 26300 / Claude Code with Claude Opus 5.5

AI-written and posted by Foniass without a human check of each point.

<!-- greyfinch-sync:ivall/xcl:comment:7301762741 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3341#issuecomment-7301762741). Originally posted by [Foniass](https://github.com/Foniass) on 2026-12-01T05:25:54Z. --- merge-check base: d0832e614fa7d6ae027bb4feb27ccb9121f54a8f head: bb5fddc7a6f08a15e387c5f58441c48a54f34b40 merge: clean build: cargo build --workspace -> ok tests: cargo test --workspace --no-fail-fast -- --test-threads=4 -> 2892 passed, 0 failed, 51 ignored; SKIPPED: 28 (20 no wave64, 2 no d3d12SDKLayers.dll, 1 no D3D12 debug layer, 1 no heap windows, 1 cannot disable the driver cache, 3 others; the same set as on main); failing: none review: read the whole diff (lower_image.rs, +373/-17, ~350 lines of tests). The fix is real: on main, `GetDimensions` past the last mip returns 0, and the offset's 0/0 gives NaN; the level is now clamped before `ftoui` (so +Inf/NaN LODs are handled) and `num_mips` is guarded. It changes the default path: every `_o` sample gets 3 `umax`, and `_l_o` and implicit-LOD pixel-shader `_o` samples also get a second `getDimensions` that depends on the first, plus about 5 ALU ops. Findings: (1) the HLSL path has the same bug and isn't fixed (`emit.rs` ~2827-2831, `texel_offset` ~2551-2560), and `mimg_unorm_sample` (~629-648) still uses an unclamped level (dims become 1x1 there, so wrong offsets rather than NaN). (2) The second dependent query (~1185-1197) could be avoided with `max(dim0 >> level, 1)` from the mip-0 query; its cost isn't measured. (3) The GPU test creates its own device on the default adapter (~2141) while taking `wave64` from the runner's adapter, so they can differ on multi-GPU machines (use `runner::device()`); its doc says 4x4 / 2x2 but the texture is 8x8 / 4x4; the `WaitForSingleObject` result isn't checked. (4) The DXIL test (~2104, ~2118) only checks that `getDimensions` appears at least twice, not that the clamp exists. (5) AI use names no tool or model, the commit area `Direct DXIL:` should be `Recompiler:`, and the suite ran with `--skip` filters. Fine: lz and gather stay at mip 0, cubes are refused earlier, and array layers aren't offset. performance: 60 s smoke of a retail title, main d0832e61 vs this merge (6495bfe7): 1516 -> 1354 frames shown (89 %; last presents per 2 s: 60 60 61), peak commit 18133 -> 16741 MB, 0 device removals, 0 crashes. That frame count is the lowest of about 25 smoke runs of this title on this machine today (others 1445-1690); the title presents at a steady 60 per 2 s, so the difference is in start-up time, not necessarily this change. One run, not a measured regression. verdict: not yet: the HLSL path keeps the same NaN bug, and the extra dependent getDimensions per _o sample is unmeasured (this run's frame count is the lowest of today's ~25 smoke runs on this machine, so worth a repeat) hardware / tool and model: RTX 5070 Ti (driver 621.14), Ryzen 7 7800X3D, Windows 11 26300 / Claude Code with Claude Opus 5.5 AI-written and posted by Foniass without a human check of each point.
bmjask commented 2026-12-02 01:13:21 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by bmjask on 2026-12-02T01:13:21Z.

Review: COMMENTED


Review of bb5fddc7: 4 finding(s). 3 are inline. These are on lines outside the diff:

crates/xcl/src/gpu/dxil/lower_image.rs:630: mimg_unorm_sample has the same unclamped explicit-LOD code (ROUND_NE, fmax 0, ftoui, then get_dimensions(level) with no num_mips clamp and no +Inf handling), and this PR leaves it unfixed. IMAGE_SAMPLE_L_O with UNORM=1 and lod=5.0 on a 2-mip texture gets GetDimensions(5)=0, umax'd to 1, so coordinates and offset are divided by 1 and return clamped corner texels, the symptom this PR fixes. lod=+Inf also hits ftoui(+Inf), which is undefined.


These findings come from an AI-assisted review (Claude Code) and focus on correctness. Each is a candidate; please check it before acting on it.

<!-- greyfinch-sync:ivall/xcl:review:6143107611 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3341#pullrequestreview-6143107611). Originally posted by [bmjask](https://github.com/bmjask) on 2026-12-02T01:13:21Z. **Review:** COMMENTED --- Review of `bb5fddc7`: 4 finding(s). 3 are inline. These are on lines outside the diff: **`crates/xcl/src/gpu/dxil/lower_image.rs:630`**: `mimg_unorm_sample` has the same unclamped explicit-LOD code (ROUND_NE, fmax 0, ftoui, then `get_dimensions(level)` with no num_mips clamp and no +Inf handling), and this PR leaves it unfixed. IMAGE_SAMPLE_L_O with UNORM=1 and lod=5.0 on a 2-mip texture gets GetDimensions(5)=0, umax'd to 1, so coordinates and offset are divided by 1 and return clamped corner texels, the symptom this PR fixes. lod=+Inf also hits ftoui(+Inf), which is undefined. --- _These findings come from an AI-assisted review (Claude Code) and focus on correctness. Each is a candidate; please check it before acting on it._
bmjask commented 2026-12-02 01:13:22 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by bmjask on 2026-12-02T01:13:22Z.

Code review location: crates/xcl/src/gpu/dxil/lower_image.rs; line 1203

@@ -1177,34 +1177,43 @@ impl Lw {
                 let tex = lw.s.heap_texture(idx, kind, false, CompType::F32, 4, true);
                 let smp: Sampler = lw.mimg_sampler(d.pc, has_c, s3);
                 if let Some(o) = ovgpr {
-                    // texel offset / size of the sampled mip (0 for lz / gather; else the LOD given, else 0)
-                    let level = match (&lod, has_lz || gather) {
-                        (_, true) => lw.c(0),
-                        (Some(l), _) => {
-                            let r = lw.un(op::ROUND_NE, *l);
-                            let z = lw.fc(0.0);
-                            let r = lw.fmax(r, z);
-                            lw.ftoui(r)
-                        }
-                        // the implicit LOD of a pixel shader's sample, rounded
+                    // texel offset / size of the sampled mip (0 for lz / gather; else the LOD given
+                    // or PS implicit LOD clamped to [0, num_mips - 1] so out-of-range mips never
+                    // return 0 dimensions and divide offsets by 0 -> NaN/Inf)
+                    let one = lw.c(1);
+                    let z0 = lw.c(0);
+                    let clamped_dims = |lw: &mut Lw, l: Value| {
+                        let dims0 = lw.s.get_dimensions(tex.handle, Some(z0));
+                        let num_mips = lw.s.m.extract(dims0, 3);
+                        let num_mips = lw.umax(num_mips, one);
+                        let max_mip = lw.sub(num_mips, one);
+                        let max_mip_f = lw.uitof(max_mip);
+                        let r = lw.un(op::ROUND_NE, l);
+                        let z = lw.fc(0.0);
+                        let r = lw.fmax(r, z);
+                        let r = lw.fmin(r, max_mip_f);
+                        let level = lw.ftoui(r);
+                        let level = lw.umin(level, max_mip);
+                        lw.s.get_dimensions(tex.handle, Some(level))
+                    };
+                    let dims = match (&lod, has_lz || gather) {
+                        (_, true) => lw.s.get_dimensions(tex.handle, Some(z0)),
+                        (Some(l), _) => clamped_dims(lw, *l),
+                        // the implicit LOD of a pixel shader's sample, rounded and clamped
                         (None, false) if ps && grads.is_none() && !has_c => {

The PS implicit-LOD offset branch also catches bias samples (IMAGE_SAMPLE_B_O), but calculate_lod ignores the bias that sample_bias then applies, so the offset is scaled for the wrong mip. PS IMAGE_SAMPLE_B_O with bias=+1.0 on an 8x8 2-mip texture where the implicit LOD is 0 samples mip 1 (4x4), but the offset is divided by 8, so a +1 texel offset moves half a texel. On compute, the bias path samples sample_level(floor(b)) while the offset falls into the _ arm and uses mip 0, the same mismatch.

<!-- greyfinch-sync:ivall/xcl:review-comment:4763370850 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3341#discussion_r4763370850). Originally posted by [bmjask](https://github.com/bmjask) on 2026-12-02T01:13:22Z. **Code review location:** `crates/xcl/src/gpu/dxil/lower_image.rs`; line 1203 ~~~~diff @@ -1177,34 +1177,43 @@ impl Lw { let tex = lw.s.heap_texture(idx, kind, false, CompType::F32, 4, true); let smp: Sampler = lw.mimg_sampler(d.pc, has_c, s3); if let Some(o) = ovgpr { - // texel offset / size of the sampled mip (0 for lz / gather; else the LOD given, else 0) - let level = match (&lod, has_lz || gather) { - (_, true) => lw.c(0), - (Some(l), _) => { - let r = lw.un(op::ROUND_NE, *l); - let z = lw.fc(0.0); - let r = lw.fmax(r, z); - lw.ftoui(r) - } - // the implicit LOD of a pixel shader's sample, rounded + // texel offset / size of the sampled mip (0 for lz / gather; else the LOD given + // or PS implicit LOD clamped to [0, num_mips - 1] so out-of-range mips never + // return 0 dimensions and divide offsets by 0 -> NaN/Inf) + let one = lw.c(1); + let z0 = lw.c(0); + let clamped_dims = |lw: &mut Lw, l: Value| { + let dims0 = lw.s.get_dimensions(tex.handle, Some(z0)); + let num_mips = lw.s.m.extract(dims0, 3); + let num_mips = lw.umax(num_mips, one); + let max_mip = lw.sub(num_mips, one); + let max_mip_f = lw.uitof(max_mip); + let r = lw.un(op::ROUND_NE, l); + let z = lw.fc(0.0); + let r = lw.fmax(r, z); + let r = lw.fmin(r, max_mip_f); + let level = lw.ftoui(r); + let level = lw.umin(level, max_mip); + lw.s.get_dimensions(tex.handle, Some(level)) + }; + let dims = match (&lod, has_lz || gather) { + (_, true) => lw.s.get_dimensions(tex.handle, Some(z0)), + (Some(l), _) => clamped_dims(lw, *l), + // the implicit LOD of a pixel shader's sample, rounded and clamped (None, false) if ps && grads.is_none() && !has_c => { ~~~~ --- The PS implicit-LOD offset branch also catches bias samples (IMAGE_SAMPLE_B_O), but `calculate_lod` ignores the bias that `sample_bias` then applies, so the offset is scaled for the wrong mip. PS IMAGE_SAMPLE_B_O with bias=+1.0 on an 8x8 2-mip texture where the implicit LOD is 0 samples mip 1 (4x4), but the offset is divided by 8, so a +1 texel offset moves half a texel. On compute, the bias path samples `sample_level(floor(b))` while the offset falls into the `_` arm and uses mip 0, the same mismatch.
bmjask commented 2026-12-02 01:13:22 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by bmjask on 2026-12-02T01:13:22Z.

Code review location: crates/xcl/src/gpu/dxil/lower_image.rs; line 1192

@@ -1177,34 +1177,43 @@ impl Lw {
                 let tex = lw.s.heap_texture(idx, kind, false, CompType::F32, 4, true);
                 let smp: Sampler = lw.mimg_sampler(d.pc, has_c, s3);
                 if let Some(o) = ovgpr {
-                    // texel offset / size of the sampled mip (0 for lz / gather; else the LOD given, else 0)
-                    let level = match (&lod, has_lz || gather) {
-                        (_, true) => lw.c(0),
-                        (Some(l), _) => {
-                            let r = lw.un(op::ROUND_NE, *l);
-                            let z = lw.fc(0.0);
-                            let r = lw.fmax(r, z);
-                            lw.ftoui(r)
-                        }
-                        // the implicit LOD of a pixel shader's sample, rounded
+                    // texel offset / size of the sampled mip (0 for lz / gather; else the LOD given
+                    // or PS implicit LOD clamped to [0, num_mips - 1] so out-of-range mips never
+                    // return 0 dimensions and divide offsets by 0 -> NaN/Inf)
+                    let one = lw.c(1);
+                    let z0 = lw.c(0);
+                    let clamped_dims = |lw: &mut Lw, l: Value| {
+                        let dims0 = lw.s.get_dimensions(tex.handle, Some(z0));
+                        let num_mips = lw.s.m.extract(dims0, 3);
+                        let num_mips = lw.umax(num_mips, one);
+                        let max_mip = lw.sub(num_mips, one);
+                        let max_mip_f = lw.uitof(max_mip);
+                        let r = lw.un(op::ROUND_NE, l);
+                        let z = lw.fc(0.0);

The offset mip is picked with ROUND_NE, but RDNA2 point-mip selection rounds half up (floor(lod + 0.501953125), as mimg_gather4_lod in this file does and documents as "unlike ROUND_NE"). With IMAGE_SAMPLE_L_O at lod=0.5 (or 2.5) and a point mip filter, the sampler reads mip 1 (or 3) but the offset is scaled for mip 0 (or 2), so the wrong texel is fetched.

<!-- greyfinch-sync:ivall/xcl:review-comment:4763370856 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3341#discussion_r4763370856). Originally posted by [bmjask](https://github.com/bmjask) on 2026-12-02T01:13:22Z. **Code review location:** `crates/xcl/src/gpu/dxil/lower_image.rs`; line 1192 ~~~~diff @@ -1177,34 +1177,43 @@ impl Lw { let tex = lw.s.heap_texture(idx, kind, false, CompType::F32, 4, true); let smp: Sampler = lw.mimg_sampler(d.pc, has_c, s3); if let Some(o) = ovgpr { - // texel offset / size of the sampled mip (0 for lz / gather; else the LOD given, else 0) - let level = match (&lod, has_lz || gather) { - (_, true) => lw.c(0), - (Some(l), _) => { - let r = lw.un(op::ROUND_NE, *l); - let z = lw.fc(0.0); - let r = lw.fmax(r, z); - lw.ftoui(r) - } - // the implicit LOD of a pixel shader's sample, rounded + // texel offset / size of the sampled mip (0 for lz / gather; else the LOD given + // or PS implicit LOD clamped to [0, num_mips - 1] so out-of-range mips never + // return 0 dimensions and divide offsets by 0 -> NaN/Inf) + let one = lw.c(1); + let z0 = lw.c(0); + let clamped_dims = |lw: &mut Lw, l: Value| { + let dims0 = lw.s.get_dimensions(tex.handle, Some(z0)); + let num_mips = lw.s.m.extract(dims0, 3); + let num_mips = lw.umax(num_mips, one); + let max_mip = lw.sub(num_mips, one); + let max_mip_f = lw.uitof(max_mip); + let r = lw.un(op::ROUND_NE, l); + let z = lw.fc(0.0); ~~~~ --- The offset mip is picked with ROUND_NE, but RDNA2 point-mip selection rounds half up (`floor(lod + 0.501953125)`, as `mimg_gather4_lod` in this file does and documents as "unlike ROUND_NE"). With IMAGE_SAMPLE_L_O at lod=0.5 (or 2.5) and a point mip filter, the sampler reads mip 1 (or 3) but the offset is scaled for mip 0 (or 2), so the wrong texel is fetched.
bmjask commented 2026-12-02 01:13:22 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by bmjask on 2026-12-02T01:13:22Z.

Code review location: crates/xcl/src/gpu/dxil/lower_image.rs; line 1186

@@ -1177,34 +1177,43 @@ impl Lw {
                 let tex = lw.s.heap_texture(idx, kind, false, CompType::F32, 4, true);
                 let smp: Sampler = lw.mimg_sampler(d.pc, has_c, s3);
                 if let Some(o) = ovgpr {
-                    // texel offset / size of the sampled mip (0 for lz / gather; else the LOD given, else 0)
-                    let level = match (&lod, has_lz || gather) {
-                        (_, true) => lw.c(0),
-                        (Some(l), _) => {
-                            let r = lw.un(op::ROUND_NE, *l);
-                            let z = lw.fc(0.0);
-                            let r = lw.fmax(r, z);
-                            lw.ftoui(r)
-                        }
-                        // the implicit LOD of a pixel shader's sample, rounded
+                    // texel offset / size of the sampled mip (0 for lz / gather; else the LOD given
+                    // or PS implicit LOD clamped to [0, num_mips - 1] so out-of-range mips never
+                    // return 0 dimensions and divide offsets by 0 -> NaN/Inf)
+                    let one = lw.c(1);
+                    let z0 = lw.c(0);
+                    let clamped_dims = |lw: &mut Lw, l: Value| {
+                        let dims0 = lw.s.get_dimensions(tex.handle, Some(z0));

The clamp only bounds LOD to [0, num_mips-1] and ignores S# MIN_LOD/MAX_LOD/LOD_BIAS, the T# min_lod and the cl operand, all of which the real sample applies (mimg_gather4_lod handles them). With a sampler MAX_LOD=0 (a common way to force mip 0) and IMAGE_SAMPLE_L_O lod=1.0 on a 2-mip 8x8 texture, the sample reads mip 0 (8x8) but the offset is divided by mip 1's width of 4, so +1 lands two texels away.

<!-- greyfinch-sync:ivall/xcl:review-comment:4763370863 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3341#discussion_r4763370863). Originally posted by [bmjask](https://github.com/bmjask) on 2026-12-02T01:13:22Z. **Code review location:** `crates/xcl/src/gpu/dxil/lower_image.rs`; line 1186 ~~~~diff @@ -1177,34 +1177,43 @@ impl Lw { let tex = lw.s.heap_texture(idx, kind, false, CompType::F32, 4, true); let smp: Sampler = lw.mimg_sampler(d.pc, has_c, s3); if let Some(o) = ovgpr { - // texel offset / size of the sampled mip (0 for lz / gather; else the LOD given, else 0) - let level = match (&lod, has_lz || gather) { - (_, true) => lw.c(0), - (Some(l), _) => { - let r = lw.un(op::ROUND_NE, *l); - let z = lw.fc(0.0); - let r = lw.fmax(r, z); - lw.ftoui(r) - } - // the implicit LOD of a pixel shader's sample, rounded + // texel offset / size of the sampled mip (0 for lz / gather; else the LOD given + // or PS implicit LOD clamped to [0, num_mips - 1] so out-of-range mips never + // return 0 dimensions and divide offsets by 0 -> NaN/Inf) + let one = lw.c(1); + let z0 = lw.c(0); + let clamped_dims = |lw: &mut Lw, l: Value| { + let dims0 = lw.s.get_dimensions(tex.handle, Some(z0)); ~~~~ --- The clamp only bounds LOD to [0, num_mips-1] and ignores S# MIN_LOD/MAX_LOD/LOD_BIAS, the T# min_lod and the `cl` operand, all of which the real sample applies (`mimg_gather4_lod` handles them). With a sampler MAX_LOD=0 (a common way to force mip 0) and IMAGE_SAMPLE_L_O lod=1.0 on a 2-mip 8x8 texture, the sample reads mip 0 (8x8) but the offset is divided by mip 1's width of 4, so +1 lands two texels away.
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/3341:upstream-pr/3341'
git switch 'upstream-pr/3341'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/3341'
git switch 'upstream-pr/3341'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/3341'
git switch 'upstream-pr/3341'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/3341'
git switch 'main'
git merge --squash 'upstream-pr/3341'
git switch 'main'
git merge --ff-only 'upstream-pr/3341'
git switch 'main'
git merge 'upstream-pr/3341'
git push origin 'main'
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
xcl-dev/xcl!3341
No description provided.