Direct DXIL: emulate explicit-LOD comparison sampling for image_sample_c_l(_o) #3298

Closed
Kalmarv wants to merge 1 commit from upstream-pr/3298 into main
Kalmarv commented 2026-11-29 06:45:45 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by Kalmarv on 2026-11-29T06:45:45Z.


In Direct DXIL (crates/xcl/src/gpu/dxil/lower_image.rs), explicit-LOD comparison samples (IMAGE_SAMPLE_C_L and IMAGE_SAMPLE_C_L_O) dropped the explicit LOD operand:

  • Outside pixel shaders (!ps), they lowered to dx.op.sampleCmpLevelZero, always reading mip 0 regardless of the requested LOD.
  • In pixel shaders (ps), they lowered to implicit-LOD dx.op.sampleCmp, computing screen-space derivatives instead of honoring the explicit LOD.

Because XCL emits SM 6.6 DXIL (before SM 6.7's SampleCmpLevel), 2D and 2DArray image_sample_c_l / image_sample_c_l_o can be lowered cleanly in SM 6.6 via mip-level textureLoad + apply_depth_compare, matching the existing mimg_gather4_lod approach.

What is in it

  • crates/xcl/src/gpu/dxil/lower_image.rs:
    • Add sample_cmp_mip and mimg_sample_cmp_lod, and route 2D / 2DArray image_sample_c_l and image_sample_c_l_o (base.starts_with("sample") && has_c && has_l && matches!(dim, 1 | 5)) through them in mimg.
    • Honor S# LOD_BIAS, [MIN_LOD, MAX_LOD], T# MIN_LOD, MIP_FILTER (none, point with RDNA2 half-up 0.501953125, linear two-mip trilinear blend), XY_MAG_FILTER / XY_MIN_FILTER (1-tap point vs 2x2 bilinear PCF), RDNA2 address modes, border color, UNORM=1 / FORCE_UNORMALIZED, A16 / D16, static samplers, and signed 6-bit texel offsets.
    • Add sample_cmp_lod_tests:
      • sample_cmp_lod_variants_translate_validate_and_avoid_implicit_or_lz_cmp: verifies 2D/2DArray IMAGE_SAMPLE_C_L / IMAGE_SAMPLE_C_L_O (compute and pixel, UNORM=1, A16/D16) validate with dxil.dll and lower via getDimensions + textureLoad instead of sampleCmpLevelZero / sampleCmp.
      • gpu_sample_cmp_lod_reads_explicit_mip_and_filters: real 2-mip Texture2D<float> GPU dispatch verifying explicit lod=1.0 vs lod=0.0, lod=0.5 linear mip blending, 2x2 bilinear PCF on a checkerboard mip 1, S# LOD_BIAS=+1.0, and IMAGE_SAMPLE_C_L_O texel offset (guarded via wave64_supported / wave32_supported so no-GPU CI skips cleanly).

Tested

  • Host: Windows 11 Pro (build 26200), AMD Ryzen 9 9950X, NVIDIA GeForce RTX 5070 Ti (driver 32.0.16.1074), rustc 1.99.0 (b940084d7 2026-09-28), DXC 1.9 (1.9.2609).
  • Pre-fix check: both new tests failed before the fix (disassembly showed dx.op.sampleCmpLevelZero; GPU test returned 0.0 from mip 0 instead of 1.0 from mip 1 for lod=1.0).
  • Post-fix targeted tests (cargo test -p xcl --lib -- gpu::dxil::lower_image::): 8 passed, 0 failed (0.61s), including all 2-mip GPU assertions on RTX 5070 Ti.
  • Full workspace (cargo test --workspace -j 2 -- --test-threads=4 --skip test_title_runs_and_game_folder_is_untouched --skip uma_driver::tests:: with XCL_DXC_DIR set to DXC 1.9): 2601 passed, 0 failed, 49 ignored, 9 filtered (426.1s).
  • Explicit GPU SKIPPED lines in workspace run (27 total): 21 no wave64 (RTX 5070 Ti is wave32), 3 wave32 per-lane #1426 (V_MUL_F16, V_PK_MUL_F16, V_CVT_F16_F32), 2 no heap windows (default XCL_HEAP_WINDOWS off), 1 no build ahead (XCL_PSO_PRECOMPILE off).
  • Release build (cargo build --release -p xcl -j 4) succeeded and passed the paired 150 s title smoke below.

Regression run

main (f0e8fdd5c3942ddd399d5ee11384c8595c5aacff):

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

PR (4270faa28b06ea7b6c9ad2d8e463051a16899173):

xcl-smoke v1
commit: 4270faa28b06ea7b6c9ad2d8e463051a16899173
gpu: NVIDIA GeForce RTX 5070 Ti
title: ea3b5e4d
seconds: 150
exit: 0x0
frames shown: 1744 (PresentX 1744)
presents per 2 s (last 3): 0 0 0
device removals: 0
crashes: 0
peak commit: 33297 MB

Not tested

  • AMD or Intel GPUs (only tested on NVIDIA GeForce RTX 5070 Ti).
  • Native wave64 GPU tests (RTX 5070 Ti is wave32; 21 wave64 tests skipped).
  • uma_driver::tests:: and test_title_runs_and_game_folder_is_untouched (filtered out of the workspace test run).
  • IMAGE_SAMPLE_C_L on 1D / 1DArray / Cube dimensions, and the HLSL fallback path (recomp/emit.rs, which is kept unchanged in this Direct DXIL PR).

AI use

  • Drafted, implemented, and tested with AI assistance (Antigravity); all code changes, pre/post DXIL validator and 2-mip RTX 5070 Ti GPU tests, workspace suite, and regression runs were verified locally.
<!-- greyfinch-sync:ivall/xcl:issue:5548371103 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3298). Originally posted by [Kalmarv](https://github.com/Kalmarv) on 2026-11-29T06:45:45Z. --- In Direct DXIL (`crates/xcl/src/gpu/dxil/lower_image.rs`), explicit-LOD comparison samples (`IMAGE_SAMPLE_C_L` and `IMAGE_SAMPLE_C_L_O`) dropped the explicit LOD operand: - Outside pixel shaders (`!ps`), they lowered to `dx.op.sampleCmpLevelZero`, always reading mip 0 regardless of the requested LOD. - In pixel shaders (`ps`), they lowered to implicit-LOD `dx.op.sampleCmp`, computing screen-space derivatives instead of honoring the explicit LOD. Because XCL emits SM 6.6 DXIL (before SM 6.7's `SampleCmpLevel`), 2D and 2DArray `image_sample_c_l` / `image_sample_c_l_o` can be lowered cleanly in SM 6.6 via mip-level `textureLoad` + `apply_depth_compare`, matching the existing `mimg_gather4_lod` approach. **What is in it** - `crates/xcl/src/gpu/dxil/lower_image.rs`: - Add `sample_cmp_mip` and `mimg_sample_cmp_lod`, and route 2D / 2DArray `image_sample_c_l` and `image_sample_c_l_o` (`base.starts_with("sample") && has_c && has_l && matches!(dim, 1 | 5)`) through them in `mimg`. - Honor S# `LOD_BIAS`, `[MIN_LOD, MAX_LOD]`, T# `MIN_LOD`, `MIP_FILTER` (`none`, `point` with RDNA2 half-up `0.501953125`, `linear` two-mip trilinear blend), `XY_MAG_FILTER` / `XY_MIN_FILTER` (1-tap point vs 2x2 bilinear PCF), RDNA2 address modes, border color, `UNORM=1` / `FORCE_UNORMALIZED`, `A16` / `D16`, static samplers, and signed 6-bit texel offsets. - Add `sample_cmp_lod_tests`: - `sample_cmp_lod_variants_translate_validate_and_avoid_implicit_or_lz_cmp`: verifies 2D/2DArray `IMAGE_SAMPLE_C_L` / `IMAGE_SAMPLE_C_L_O` (compute and pixel, `UNORM=1`, `A16`/`D16`) validate with `dxil.dll` and lower via `getDimensions` + `textureLoad` instead of `sampleCmpLevelZero` / `sampleCmp`. - `gpu_sample_cmp_lod_reads_explicit_mip_and_filters`: real 2-mip `Texture2D<float>` GPU dispatch verifying explicit `lod=1.0` vs `lod=0.0`, `lod=0.5` linear mip blending, 2x2 bilinear PCF on a checkerboard mip 1, S# `LOD_BIAS=+1.0`, and `IMAGE_SAMPLE_C_L_O` texel offset (guarded via `wave64_supported` / `wave32_supported` so no-GPU CI skips cleanly). **Tested** - Host: Windows 11 Pro (build 26200), AMD Ryzen 9 9950X, NVIDIA GeForce RTX 5070 Ti (driver `32.0.16.1074`), `rustc 1.99.0 (b940084d7 2026-09-28)`, DXC 1.9 (`1.9.2609`). - Pre-fix check: both new tests failed before the fix (disassembly showed `dx.op.sampleCmpLevelZero`; GPU test returned `0.0` from mip 0 instead of `1.0` from mip 1 for `lod=1.0`). - Post-fix targeted tests (`cargo test -p xcl --lib -- gpu::dxil::lower_image::`): 8 passed, 0 failed (`0.61s`), including all 2-mip GPU assertions on RTX 5070 Ti. - Full workspace (`cargo test --workspace -j 2 -- --test-threads=4 --skip test_title_runs_and_game_folder_is_untouched --skip uma_driver::tests::` with `XCL_DXC_DIR` set to DXC 1.9): 2601 passed, 0 failed, 49 ignored, 9 filtered (`426.1s`). - Explicit GPU `SKIPPED` lines in workspace run (27 total): 21 `no wave64` (RTX 5070 Ti is wave32), 3 wave32 per-lane `#1426` (`V_MUL_F16`, `V_PK_MUL_F16`, `V_CVT_F16_F32`), 2 `no heap windows` (default `XCL_HEAP_WINDOWS` off), 1 `no build ahead` (`XCL_PSO_PRECOMPILE` off). - Release build (`cargo build --release -p xcl -j 4`) succeeded and passed the paired 150 s title smoke below. **Regression run** main (`f0e8fdd5c3942ddd399d5ee11384c8595c5aacff`): ``` 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 ``` PR (`4270faa28b06ea7b6c9ad2d8e463051a16899173`): ``` xcl-smoke v1 commit: 4270faa28b06ea7b6c9ad2d8e463051a16899173 gpu: NVIDIA GeForce RTX 5070 Ti title: ea3b5e4d seconds: 150 exit: 0x0 frames shown: 1744 (PresentX 1744) presents per 2 s (last 3): 0 0 0 device removals: 0 crashes: 0 peak commit: 33297 MB ``` **Not tested** - AMD or Intel GPUs (only tested on NVIDIA GeForce RTX 5070 Ti). - Native wave64 GPU tests (RTX 5070 Ti is wave32; 21 wave64 tests skipped). - `uma_driver::tests::` and `test_title_runs_and_game_folder_is_untouched` (filtered out of the workspace test run). - `IMAGE_SAMPLE_C_L` on 1D / 1DArray / Cube dimensions, and the HLSL fallback path (`recomp/emit.rs`, which is kept unchanged in this Direct DXIL PR). **AI use** - Drafted, implemented, and tested with AI assistance (Antigravity); all code changes, pre/post DXIL validator and 2-mip RTX 5070 Ti GPU tests, workspace suite, and regression runs were verified locally.
Foniass commented 2026-11-29 14:59:04 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by Foniass on 2026-11-29T14:59:04Z.


Review (head 4270faa2, diff read, not a merge-check):

  • Probably right for normal shadow sampling, and it fixes a real gap (the LOD was ignored).
  • It copies ~150 lines of mimg_gather4_lod instead of sharing them.
  • The GPU test is ~300 lines of its own D3D12 setup (its own D3D12CreateDevice) that ignores runner::device() and XCL_ADAPTER.
  • The doc comment mentions a MIP_FILTER=none case that no test covers.
  • +697 lines in one file without a reason in the description; the commit subject is dxil: with no body, and AI use names no model.
  • CI's first red run was the pipeline-library flake (#3221), it passed on rerun.

Suggested verdict: not yet (share the code, use the test runner, split).

(Review written with Claude Code, Claude Opus 5.5.)

<!-- greyfinch-sync:ivall/xcl:comment:7257727740 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3298#issuecomment-7257727740). Originally posted by [Foniass](https://github.com/Foniass) on 2026-11-29T14:59:04Z. --- Review (head 4270faa2, diff read, not a merge-check): - Probably right for normal shadow sampling, and it fixes a real gap (the LOD was ignored). - It copies ~150 lines of `mimg_gather4_lod` instead of sharing them. - The GPU test is ~300 lines of its own D3D12 setup (its own `D3D12CreateDevice`) that ignores `runner::device()` and `XCL_ADAPTER`. - The doc comment mentions a MIP_FILTER=none case that no test covers. - +697 lines in one file without a reason in the description; the commit subject is `dxil:` with no body, and AI use names no model. - CI's first red run was the pipeline-library flake (#3221), it passed on rerun. Suggested verdict: not yet (share the code, use the test runner, split). (Review written with Claude Code, Claude Opus 5.5.)
Foniass commented 2026-12-01 06:13:56 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by Foniass on 2026-12-01T06:13:56Z.


merge-check
base: d0832e614f
head: 4270faa28b
merge: clean
build: cargo build --workspace -> ok
tests: cargo test --workspace --no-fail-fast -- --test-threads=4 -> 2891 passed, 1 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: gpu::dxil::lower_image::sample_cmp_lod_tests::gpu_sample_cmp_lod_reads_explicit_mip_and_filters
review: read the whole diff (lower_image.rs, +697/-1, ~295 runtime lines). It changes the default path: on main, direct DXIL lowers 2D / 2DArray image_sample_c_l to sampleCmpLevelZero outside pixel shaders and to implicit-LOD sampleCmp in pixel shaders (the HLSL path does the same, emit.rs ~2850). Now every such instruction becomes manual comparison filtering, up to 2 mips x 2x2 textureLoads plus up to 3 getDimensions, with S# / T# bits decoded by hand and no switch; SM 6.7 SampleCmpLevel isn't used. Findings: (1) mimg_sample_cmp_lod reads S# words 0-3 from sreg(ssamp..ssamp+3) (~761-763). Since 4250e761 (after this PR's base), waterfall::collapse removes the v_readfirstlanes that write those SGPRs in a collapsed sampler waterfall and routes the per-lane sampler through mimg_s3. So inside a collapsed loop, compare function, address mode, filter, LOD clamp and border come from stale registers, where main uses the per-lane heap sampler. Reading the words from the waterfall VGPRs, or keeping the old path when waterfall_ssamp has the pc, would fix it (mimg_gather4_lod has the same issue for words 0-2). (2) 1D / 1DArray / Cube keep LOD 0, and the HLSL path still ignores the LOD, so the backends now differ. (3) Question: hardware comparison may clamp the reference to [0,1] for UNORM depth formats; apply_depth_compare doesn't. (4) AI use names no model; the subject area dxil: should be Recompiler:; ~700 lines; the GPU test's doc mentions MIP_FILTER=none but no case covers it. Fine: the S# bit fields match sampler_d3d12 / mimg_gather4_lod, PCF compares before filtering, the point mip rounds half-up as RDNA2 does, the linear mip blend, offsets, array-slice clamp, static samplers and A16 / D16 handling are right, and the GPU test fails on main and uses skip_test.
performance: 60 s smoke of a retail title, main d0832e61 vs this merge (d38f63de): 1516 -> 1679 frames shown (110 %; last presents per 2 s: 60 60 60), peak commit 18133 -> 16992 MB, 0 device removals, 0 crashes.
verdict: not yet: the PR's own GPU test fails on this RTX 5070 Ti merged onto current main (lower_image.rs:1635, "lod=1.0 must read mip 1 (0.5 < 0.8 => 1.0)": got 0.0, expected 1.0), so the explicit-LOD path reads the wrong mip here; also, inside a collapsed sampler waterfall the S# words come from stale SGPRs (finding 1), and the HLSL path still ignores the LOD, so the backends now disagree
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:7302638798 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3298#issuecomment-7302638798). Originally posted by [Foniass](https://github.com/Foniass) on 2026-12-01T06:13:56Z. --- merge-check base: d0832e614fa7d6ae027bb4feb27ccb9121f54a8f head: 4270faa28b06ea7b6c9ad2d8e463051a16899173 merge: clean build: cargo build --workspace -> ok tests: cargo test --workspace --no-fail-fast -- --test-threads=4 -> 2891 passed, 1 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: gpu::dxil::lower_image::sample_cmp_lod_tests::gpu_sample_cmp_lod_reads_explicit_mip_and_filters review: read the whole diff (lower_image.rs, +697/-1, ~295 runtime lines). It changes the default path: on main, direct DXIL lowers 2D / 2DArray `image_sample_c_l` to `sampleCmpLevelZero` outside pixel shaders and to implicit-LOD `sampleCmp` in pixel shaders (the HLSL path does the same, emit.rs ~2850). Now every such instruction becomes manual comparison filtering, up to 2 mips x 2x2 `textureLoad`s plus up to 3 `getDimensions`, with S# / T# bits decoded by hand and no switch; SM 6.7 `SampleCmpLevel` isn't used. Findings: (1) `mimg_sample_cmp_lod` reads S# words 0-3 from `sreg(ssamp..ssamp+3)` (~761-763). Since 4250e761 (after this PR's base), `waterfall::collapse` removes the `v_readfirstlane`s that write those SGPRs in a collapsed sampler waterfall and routes the per-lane sampler through `mimg_s3`. So inside a collapsed loop, compare function, address mode, filter, LOD clamp and border come from stale registers, where main uses the per-lane heap sampler. Reading the words from the waterfall VGPRs, or keeping the old path when `waterfall_ssamp` has the pc, would fix it (`mimg_gather4_lod` has the same issue for words 0-2). (2) 1D / 1DArray / Cube keep LOD 0, and the HLSL path still ignores the LOD, so the backends now differ. (3) Question: hardware comparison may clamp the reference to [0,1] for UNORM depth formats; `apply_depth_compare` doesn't. (4) AI use names no model; the subject area `dxil:` should be `Recompiler:`; ~700 lines; the GPU test's doc mentions MIP_FILTER=none but no case covers it. Fine: the S# bit fields match `sampler_d3d12` / `mimg_gather4_lod`, PCF compares before filtering, the point mip rounds half-up as RDNA2 does, the linear mip blend, offsets, array-slice clamp, static samplers and A16 / D16 handling are right, and the GPU test fails on main and uses `skip_test`. performance: 60 s smoke of a retail title, main d0832e61 vs this merge (d38f63de): 1516 -> 1679 frames shown (110 %; last presents per 2 s: 60 60 60), peak commit 18133 -> 16992 MB, 0 device removals, 0 crashes. verdict: not yet: the PR's own GPU test fails on this RTX 5070 Ti merged onto current main (lower_image.rs:1635, "lod=1.0 must read mip 1 (0.5 < 0.8 => 1.0)": got 0.0, expected 1.0), so the explicit-LOD path reads the wrong mip here; also, inside a collapsed sampler waterfall the S# words come from stale SGPRs (finding 1), and the HLSL path still ignores the LOD, so the backends now disagree 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-01 21:42:15 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by bmjask on 2026-12-01T21:42:15Z.

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

@@ -590,6 +590,300 @@ impl Lw {
         self.image_result(vdata, &vals, d16, act)
     }
 
+    /// Evaluates one mip level `mip_i` of a 2D / 2DArray comparison sample via `textureLoad`,
+    /// applying RDNA2 address modes, border color, `apply_depth_compare`, and either point or
+    /// 2x2 bilinear PCF filtering.
+    #[allow(clippy::too_many_arguments)]
+    fn sample_cmp_mip(
+        &mut self,
+        tex: Texture,
+        mip_i: Value,
+        dim: u64,
+        coords: &[Value],
+        cmp_ref: Value,
+        ovgpr: Option<Value>,
+        unorm: bool,
+        s0: Value,
+        is_linear: Value,
+        border_val: Value,
+        cmp_func: Value,
+    ) -> LResult<Value> {
+        let z0 = self.c(0);
+        let one = self.c(1);
+        let dims = self.s.get_dimensions(tex.handle, Some(mip_i));
+        let w = self.s.m.extract(dims, 0);
+        let h = self.s.m.extract(dims, 1);
+        let w = self.umax(w, one);
+        let h = self.umax(h, one);
+
+        let wf = self.uitof(w);
+        let hf = self.uitof(h);
+        let (px, py) = if unorm {
+            (coords[0], coords[1])
+        } else {
+            let force_unorm = self.lshrc(s0, 15);
+            let force_unorm = self.andc(force_unorm, 1);
+            let is_unorm = self.nz(force_unorm);
+            let sx = self.fmul(coords[0], wf);
+            let sy = self.fmul(coords[1], hf);
+            (self.sel(is_unorm, coords[0], sx), self.sel(is_unorm, coords[1], sy))
+        };
+
+        let slice = if dim == 5 {
+            let arr = self.s.m.extract(dims, 2);
+            let arr = self.umax(arr, one);
+            let max_s = self.sub(arr, one);
+            let sr = self.un(op::ROUND_NE, coords[2]);
+            let si = self.ftosi(sr);
+            let si = self.smin_i32(si, max_s);
+            Some(self.smax_i32(z0, si))
+        } else {
+            None
+        };
+
+        let (ox, oy) = match ovgpr {
+            Some(o) => {
+                let x = self.shlc(o, 26);
+                let x = self.ashrc(x, 26);
+                let y = self.shlc(o, 18);
+                let y = self.ashrc(y, 26);
+                (Some(x), Some(y))
+            }
+            None => (None, None),
+        };
+
+        let mode_x = self.andc(s0, 7);
+        let mode_y = self.lshrc(s0, 3);
+        let mode_y = self.andc(mode_y, 7);
+
+        let eval_tap = |lw: &mut Lw, cx: Value, cy: Value, bx: Value, by: Value| -> Value {
+            let load_coords = match slice {
+                Some(sl) => vec![cx, cy, sl],
+                None => vec![cx, cy],
+            };
+            let loaded = lw.s.texture_load(tex, Some(mip_i), &load_coords, &[]);
+            let is_b = lw.lor(bx, by);
+            let val = lw.sel(is_b, border_val, loaded[0]);
+            lw.apply_depth_compare(cmp_ref, val, cmp_func)

Not certain about this one: the emulated compare passes the raw cmp_ref to apply_depth_compare without clamping it to [0,1] for UNORM depth views. The hardware SampleCmp path this replaces (and, as far as I know, D3D/RDNA2 comparison filtering) clamps the reference for UNORM formats. A D24/D16 shadow map with cmp_ref=1.02, texel=1.0 and LESS_EQUAL then gives 0 (shadowed) instead of 1 (lit) for receivers past the far plane.

<!-- greyfinch-sync:ivall/xcl:review-comment:4761413461 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3298#discussion_r4761413461). Originally posted by [bmjask](https://github.com/bmjask) on 2026-12-01T21:42:15Z. **Code review location:** `crates/xcl/src/gpu/dxil/lower_image.rs`; line 667 ~~~~diff @@ -590,6 +590,300 @@ impl Lw { self.image_result(vdata, &vals, d16, act) } + /// Evaluates one mip level `mip_i` of a 2D / 2DArray comparison sample via `textureLoad`, + /// applying RDNA2 address modes, border color, `apply_depth_compare`, and either point or + /// 2x2 bilinear PCF filtering. + #[allow(clippy::too_many_arguments)] + fn sample_cmp_mip( + &mut self, + tex: Texture, + mip_i: Value, + dim: u64, + coords: &[Value], + cmp_ref: Value, + ovgpr: Option<Value>, + unorm: bool, + s0: Value, + is_linear: Value, + border_val: Value, + cmp_func: Value, + ) -> LResult<Value> { + let z0 = self.c(0); + let one = self.c(1); + let dims = self.s.get_dimensions(tex.handle, Some(mip_i)); + let w = self.s.m.extract(dims, 0); + let h = self.s.m.extract(dims, 1); + let w = self.umax(w, one); + let h = self.umax(h, one); + + let wf = self.uitof(w); + let hf = self.uitof(h); + let (px, py) = if unorm { + (coords[0], coords[1]) + } else { + let force_unorm = self.lshrc(s0, 15); + let force_unorm = self.andc(force_unorm, 1); + let is_unorm = self.nz(force_unorm); + let sx = self.fmul(coords[0], wf); + let sy = self.fmul(coords[1], hf); + (self.sel(is_unorm, coords[0], sx), self.sel(is_unorm, coords[1], sy)) + }; + + let slice = if dim == 5 { + let arr = self.s.m.extract(dims, 2); + let arr = self.umax(arr, one); + let max_s = self.sub(arr, one); + let sr = self.un(op::ROUND_NE, coords[2]); + let si = self.ftosi(sr); + let si = self.smin_i32(si, max_s); + Some(self.smax_i32(z0, si)) + } else { + None + }; + + let (ox, oy) = match ovgpr { + Some(o) => { + let x = self.shlc(o, 26); + let x = self.ashrc(x, 26); + let y = self.shlc(o, 18); + let y = self.ashrc(y, 26); + (Some(x), Some(y)) + } + None => (None, None), + }; + + let mode_x = self.andc(s0, 7); + let mode_y = self.lshrc(s0, 3); + let mode_y = self.andc(mode_y, 7); + + let eval_tap = |lw: &mut Lw, cx: Value, cy: Value, bx: Value, by: Value| -> Value { + let load_coords = match slice { + Some(sl) => vec![cx, cy, sl], + None => vec![cx, cy], + }; + let loaded = lw.s.texture_load(tex, Some(mip_i), &load_coords, &[]); + let is_b = lw.lor(bx, by); + let val = lw.sel(is_b, border_val, loaded[0]); + lw.apply_depth_compare(cmp_ref, val, cmp_func) ~~~~ --- Not certain about this one: the emulated compare passes the raw `cmp_ref` to `apply_depth_compare` without clamping it to [0,1] for UNORM depth views. The hardware SampleCmp path this replaces (and, as far as I know, D3D/RDNA2 comparison filtering) clamps the reference for UNORM formats. A D24/D16 shadow map with cmp_ref=1.02, texel=1.0 and LESS_EQUAL then gives 0 (shadowed) instead of 1 (lit) for receivers past the far plane.
bmjask commented 2026-12-01 21:42:15 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by bmjask on 2026-12-01T21:42:15Z.

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

@@ -590,6 +590,300 @@ impl Lw {
         self.image_result(vdata, &vals, d16, act)
     }
 
+    /// Evaluates one mip level `mip_i` of a 2D / 2DArray comparison sample via `textureLoad`,
+    /// applying RDNA2 address modes, border color, `apply_depth_compare`, and either point or
+    /// 2x2 bilinear PCF filtering.
+    #[allow(clippy::too_many_arguments)]
+    fn sample_cmp_mip(
+        &mut self,
+        tex: Texture,
+        mip_i: Value,
+        dim: u64,
+        coords: &[Value],
+        cmp_ref: Value,
+        ovgpr: Option<Value>,
+        unorm: bool,
+        s0: Value,
+        is_linear: Value,
+        border_val: Value,
+        cmp_func: Value,
+    ) -> LResult<Value> {
+        let z0 = self.c(0);
+        let one = self.c(1);
+        let dims = self.s.get_dimensions(tex.handle, Some(mip_i));
+        let w = self.s.m.extract(dims, 0);
+        let h = self.s.m.extract(dims, 1);
+        let w = self.umax(w, one);
+        let h = self.umax(h, one);
+
+        let wf = self.uitof(w);
+        let hf = self.uitof(h);
+        let (px, py) = if unorm {
+            (coords[0], coords[1])
+        } else {
+            let force_unorm = self.lshrc(s0, 15);
+            let force_unorm = self.andc(force_unorm, 1);
+            let is_unorm = self.nz(force_unorm);
+            let sx = self.fmul(coords[0], wf);
+            let sy = self.fmul(coords[1], hf);
+            (self.sel(is_unorm, coords[0], sx), self.sel(is_unorm, coords[1], sy))
+        };
+
+        let slice = if dim == 5 {
+            let arr = self.s.m.extract(dims, 2);
+            let arr = self.umax(arr, one);
+            let max_s = self.sub(arr, one);
+            let sr = self.un(op::ROUND_NE, coords[2]);
+            let si = self.ftosi(sr);
+            let si = self.smin_i32(si, max_s);
+            Some(self.smax_i32(z0, si))
+        } else {
+            None
+        };
+
+        let (ox, oy) = match ovgpr {
+            Some(o) => {
+                let x = self.shlc(o, 26);
+                let x = self.ashrc(x, 26);
+                let y = self.shlc(o, 18);
+                let y = self.ashrc(y, 26);
+                (Some(x), Some(y))
+            }
+            None => (None, None),
+        };
+
+        let mode_x = self.andc(s0, 7);
+        let mode_y = self.lshrc(s0, 3);
+        let mode_y = self.andc(mode_y, 7);
+
+        let eval_tap = |lw: &mut Lw, cx: Value, cy: Value, bx: Value, by: Value| -> Value {
+            let load_coords = match slice {
+                Some(sl) => vec![cx, cy, sl],
+                None => vec![cx, cy],
+            };
+            let loaded = lw.s.texture_load(tex, Some(mip_i), &load_coords, &[]);
+            let is_b = lw.lor(bx, by);
+            let val = lw.sel(is_b, border_val, loaded[0]);
+            lw.apply_depth_compare(cmp_ref, val, cmp_func)
+        };
+
+        let res_var = self.tmp_var();
+        self.wr(res_var, z0);
+
+        self.guarded(is_linear, &mut |lw| {
+            let half = lw.fc(0.5);
+            let pxm = lw.fsub(px, half);
+            let pym = lw.fsub(py, half);
+            let fx_fl = lw.un(op::ROUND_NI, pxm);
+            let fy_fl = lw.un(op::ROUND_NI, pym);
+            let wx = lw.fsub(pxm, fx_fl);
+            let wy = lw.fsub(pym, fy_fl);
+            let mut ix0 = lw.ftosi(fx_fl);
+            let mut iy0 = lw.ftosi(fy_fl);
+            if let (Some(ox), Some(oy)) = (ox, oy) {
+                ix0 = lw.add(ix0, ox);
+                iy0 = lw.add(iy0, oy);
+            }
+            let ix1 = lw.add(ix0, one);
+            let iy1 = lw.add(iy0, one);
+
+            let (x0, bx0) = lw.apply_rdna2_addr_mode(ix0, w, mode_x);
+            let (x1, bx1) = lw.apply_rdna2_addr_mode(ix1, w, mode_x);
+            let (y0, by0) = lw.apply_rdna2_addr_mode(iy0, h, mode_y);
+            let (y1, by1) = lw.apply_rdna2_addr_mode(iy1, h, mode_y);
+
+            let c00 = eval_tap(lw, x0, y0, bx0, by0);
+            let c10 = eval_tap(lw, x1, y0, bx1, by0);
+            let c01 = eval_tap(lw, x0, y1, bx0, by1);
+            let c11 = eval_tap(lw, x1, y1, bx1, by1);
+
+            let d_top = lw.fsub(c10, c00);
+            let m_top = lw.fmul(d_top, wx);
+            let top = lw.fadd(c00, m_top);
+            let d_bot = lw.fsub(c11, c01);
+            let m_bot = lw.fmul(d_bot, wx);
+            let bot = lw.fadd(c01, m_bot);
+            let d_v = lw.fsub(bot, top);
+            let m_v = lw.fmul(d_v, wy);
+            let lin = lw.fadd(top, m_v);
+            let b = lw.bits(lin);
+            lw.wr(res_var, b);
+            Ok(())
+        })?;
+
+        let not_linear = self.lnot(is_linear);
+        self.guarded(not_linear, &mut |lw| {
+            let fx_p = lw.un(op::ROUND_NI, px);
+            let fy_p = lw.un(op::ROUND_NI, py);
+            let mut ix = lw.ftosi(fx_p);
+            let mut iy = lw.ftosi(fy_p);
+            if let (Some(ox), Some(oy)) = (ox, oy) {
+                ix = lw.add(ix, ox);
+                iy = lw.add(iy, oy);
+            }
+            let (xp, bxp) = lw.apply_rdna2_addr_mode(ix, w, mode_x);
+            let (yp, byp) = lw.apply_rdna2_addr_mode(iy, h, mode_y);
+            let pt = eval_tap(lw, xp, yp, bxp, byp);
+            let b = lw.bits(pt);
+            lw.wr(res_var, b);
+            Ok(())
+        })?;
+
+        let rb = self.rd(res_var);
+        Ok(self.f(rb))
+    }
+
+    /// Emulates explicit-LOD comparison sampling (`image_sample_c_l` / `image_sample_c_l_o`)
+    /// on 2D and 2DArray textures in SM 6.6 via mip-level `textureLoad` + `apply_depth_compare`,
+    /// honoring S# `LOD_BIAS`, `[MIN_LOD, MAX_LOD]`, T# `MIN_LOD`, point/linear/none mip filter,
+    /// point/bilinear XY filter (2x2 PCF), address modes, border color, `UNORM=1` /
+    /// `FORCE_UNORMALIZED`, `a16`/`d16`, static samplers, and texel offsets.
+    #[allow(clippy::too_many_arguments)]
+    fn mimg_sample_cmp_lod(
+        &mut self,
+        d: &Decoded,
+        dim: u64,
+        kind: TexKind,
+        tw0: Value,
+        tw1: Value,
+        ssamp: u64,
+        coords: &[Value],
+        lod: Value,
+        cmp_ref: Value,
+        ovgpr: Option<Value>,
+        unorm: bool,
+        comps: &[u64],
+        vdata: u64,
+        d16: bool,
+        act: Value,
+        run: Value,
+    ) -> LResult<()> {
+        let (s0, s1, s2, s3) = match self.static_samplers.get(&d.pc).copied() {
+            Some(w) => (self.c(w[0]), self.c(w[1]), self.c(w[2]), self.c(w[3])),
+            None => (self.sreg(ssamp), self.sreg(ssamp + 1), self.sreg(ssamp + 2), self.sreg(ssamp + 3)),
+        };
+
+        let res_tmp = self.tmp_var();
+        let z0 = self.c(0);
+        self.wr(res_tmp, z0);
+
+        self.guarded(run, &mut |lw| {
+            let tex = lw.s.heap_texture(tw0, kind, false, CompType::F32, 4, true);
+
+            let scale = lw.fc(1.0 / 256.0);
+            let lb_sh = lw.shlc(s2, 18);
+            let lb_se = lw.ashrc(lb_sh, 18);
+            let lb_f = lw.sitof(lb_se);
+            let lod_bias = lw.fmul(lb_f, scale);
+            let l_raw = lw.fadd(lod, lod_bias);
+
+            let min_u = lw.andc(s1, 0xFFF);
+            let min_f = lw.uitof(min_u);
+            let min_lod = lw.fmul(min_f, scale);
+            let max_s = lw.lshrc(s1, 12);
+            let max_u = lw.andc(max_s, 0xFFF);
+            let max_f = lw.uitof(max_u);
+            let max_lod_s = lw.fmul(max_f, scale);
+            let t_min_s = lw.lshrc(tw1, 8);
+            let t_min_u = lw.andc(t_min_s, 0xFFF);
+            let t_min_f = lw.uitof(t_min_u);
+            let t_min_lod = lw.fmul(t_min_f, scale);
+
+            let l_lo = lw.fmax(l_raw, min_lod);
+            let l_hi = lw.fmin(l_lo, max_lod_s);
+            let l_clamped = lw.fmax(l_hi, t_min_lod);
+
+            let one = lw.c(1);
+            let dims0 = lw.s.get_dimensions(tex.handle, Some(z0));
+            let dims_m = lw.s.m.extract(dims0, 3);
+            let num_mips = lw.umax(dims_m, one);
+            let max_mip = lw.sub(num_mips, one);
+            let max_mip_f = lw.uitof(max_mip);
+
+            let zf = lw.fc(0.0);
+            let l_pos = lw.fmax(l_clamped, zf);
+            let l_final = lw.fmin(l_pos, max_mip_f);
+
+            let mip_s = lw.lshrc(s2, 26);
+            let mip_flt = lw.andc(mip_s, 3);
+            let mip_none = lw.cmpc(Pred::Eq, mip_flt, 0);
+            let mip_linear = lw.cmpc(Pred::Eq, mip_flt, 2);
+
+            let t_ni = lw.un(op::ROUND_NI, t_min_lod);
+            let t_fl = lw.fmax(t_ni, zf);
+            let t_u = lw.ftoui(t_fl);
+            let m0_none = lw.umin(t_u, max_mip);
+
+            let half_up = lw.fc(0.501953125);
+            let pt_sum = lw.fadd(l_final, half_up);
+            let pt_ni = lw.un(op::ROUND_NI, pt_sum);
+            let pt_fl = lw.fmax(pt_ni, zf);
+            let pt_u = lw.ftoui(pt_fl);
+            let m0_pt = lw.umin(pt_u, max_mip);
+
+            let l_fl = lw.un(op::ROUND_NI, l_final);
+            let l_u = lw.ftoui(l_fl);
+            let m0_lin = lw.umin(l_u, max_mip);
+            let m0_p1 = lw.add(m0_lin, one);
+            let m1_lin = lw.umin(m0_p1, max_mip);
+            let w_mip = lw.fsub(l_final, l_fl);
+
+            let m0_sel = lw.sel(mip_linear, m0_lin, m0_pt);
+            let m0 = lw.sel(mip_none, m0_none, m0_sel);
+            let m1 = lw.sel(mip_linear, m1_lin, m0);
+
+            let is_mag = lw.fcmp(Pred::FOLe, l_raw, zf);

Not certain about this one: mag-vs-min XY filtering is decided from l_raw (lod + bias, before the S# MIN/MAX_LOD and T# MIN_LOD clamps), while the mip is picked from the clamped LOD. With XY_MAG=point, XY_MIN=bilinear, MIN_LOD=1.0 and lod=-1.0, mip 1 is read (minified) but the point filter is used, so PCF edges come out hard.

<!-- greyfinch-sync:ivall/xcl:review-comment:4761413469 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3298#discussion_r4761413469). Originally posted by [bmjask](https://github.com/bmjask) on 2026-12-01T21:42:15Z. **Code review location:** `crates/xcl/src/gpu/dxil/lower_image.rs`; line 835 ~~~~diff @@ -590,6 +590,300 @@ impl Lw { self.image_result(vdata, &vals, d16, act) } + /// Evaluates one mip level `mip_i` of a 2D / 2DArray comparison sample via `textureLoad`, + /// applying RDNA2 address modes, border color, `apply_depth_compare`, and either point or + /// 2x2 bilinear PCF filtering. + #[allow(clippy::too_many_arguments)] + fn sample_cmp_mip( + &mut self, + tex: Texture, + mip_i: Value, + dim: u64, + coords: &[Value], + cmp_ref: Value, + ovgpr: Option<Value>, + unorm: bool, + s0: Value, + is_linear: Value, + border_val: Value, + cmp_func: Value, + ) -> LResult<Value> { + let z0 = self.c(0); + let one = self.c(1); + let dims = self.s.get_dimensions(tex.handle, Some(mip_i)); + let w = self.s.m.extract(dims, 0); + let h = self.s.m.extract(dims, 1); + let w = self.umax(w, one); + let h = self.umax(h, one); + + let wf = self.uitof(w); + let hf = self.uitof(h); + let (px, py) = if unorm { + (coords[0], coords[1]) + } else { + let force_unorm = self.lshrc(s0, 15); + let force_unorm = self.andc(force_unorm, 1); + let is_unorm = self.nz(force_unorm); + let sx = self.fmul(coords[0], wf); + let sy = self.fmul(coords[1], hf); + (self.sel(is_unorm, coords[0], sx), self.sel(is_unorm, coords[1], sy)) + }; + + let slice = if dim == 5 { + let arr = self.s.m.extract(dims, 2); + let arr = self.umax(arr, one); + let max_s = self.sub(arr, one); + let sr = self.un(op::ROUND_NE, coords[2]); + let si = self.ftosi(sr); + let si = self.smin_i32(si, max_s); + Some(self.smax_i32(z0, si)) + } else { + None + }; + + let (ox, oy) = match ovgpr { + Some(o) => { + let x = self.shlc(o, 26); + let x = self.ashrc(x, 26); + let y = self.shlc(o, 18); + let y = self.ashrc(y, 26); + (Some(x), Some(y)) + } + None => (None, None), + }; + + let mode_x = self.andc(s0, 7); + let mode_y = self.lshrc(s0, 3); + let mode_y = self.andc(mode_y, 7); + + let eval_tap = |lw: &mut Lw, cx: Value, cy: Value, bx: Value, by: Value| -> Value { + let load_coords = match slice { + Some(sl) => vec![cx, cy, sl], + None => vec![cx, cy], + }; + let loaded = lw.s.texture_load(tex, Some(mip_i), &load_coords, &[]); + let is_b = lw.lor(bx, by); + let val = lw.sel(is_b, border_val, loaded[0]); + lw.apply_depth_compare(cmp_ref, val, cmp_func) + }; + + let res_var = self.tmp_var(); + self.wr(res_var, z0); + + self.guarded(is_linear, &mut |lw| { + let half = lw.fc(0.5); + let pxm = lw.fsub(px, half); + let pym = lw.fsub(py, half); + let fx_fl = lw.un(op::ROUND_NI, pxm); + let fy_fl = lw.un(op::ROUND_NI, pym); + let wx = lw.fsub(pxm, fx_fl); + let wy = lw.fsub(pym, fy_fl); + let mut ix0 = lw.ftosi(fx_fl); + let mut iy0 = lw.ftosi(fy_fl); + if let (Some(ox), Some(oy)) = (ox, oy) { + ix0 = lw.add(ix0, ox); + iy0 = lw.add(iy0, oy); + } + let ix1 = lw.add(ix0, one); + let iy1 = lw.add(iy0, one); + + let (x0, bx0) = lw.apply_rdna2_addr_mode(ix0, w, mode_x); + let (x1, bx1) = lw.apply_rdna2_addr_mode(ix1, w, mode_x); + let (y0, by0) = lw.apply_rdna2_addr_mode(iy0, h, mode_y); + let (y1, by1) = lw.apply_rdna2_addr_mode(iy1, h, mode_y); + + let c00 = eval_tap(lw, x0, y0, bx0, by0); + let c10 = eval_tap(lw, x1, y0, bx1, by0); + let c01 = eval_tap(lw, x0, y1, bx0, by1); + let c11 = eval_tap(lw, x1, y1, bx1, by1); + + let d_top = lw.fsub(c10, c00); + let m_top = lw.fmul(d_top, wx); + let top = lw.fadd(c00, m_top); + let d_bot = lw.fsub(c11, c01); + let m_bot = lw.fmul(d_bot, wx); + let bot = lw.fadd(c01, m_bot); + let d_v = lw.fsub(bot, top); + let m_v = lw.fmul(d_v, wy); + let lin = lw.fadd(top, m_v); + let b = lw.bits(lin); + lw.wr(res_var, b); + Ok(()) + })?; + + let not_linear = self.lnot(is_linear); + self.guarded(not_linear, &mut |lw| { + let fx_p = lw.un(op::ROUND_NI, px); + let fy_p = lw.un(op::ROUND_NI, py); + let mut ix = lw.ftosi(fx_p); + let mut iy = lw.ftosi(fy_p); + if let (Some(ox), Some(oy)) = (ox, oy) { + ix = lw.add(ix, ox); + iy = lw.add(iy, oy); + } + let (xp, bxp) = lw.apply_rdna2_addr_mode(ix, w, mode_x); + let (yp, byp) = lw.apply_rdna2_addr_mode(iy, h, mode_y); + let pt = eval_tap(lw, xp, yp, bxp, byp); + let b = lw.bits(pt); + lw.wr(res_var, b); + Ok(()) + })?; + + let rb = self.rd(res_var); + Ok(self.f(rb)) + } + + /// Emulates explicit-LOD comparison sampling (`image_sample_c_l` / `image_sample_c_l_o`) + /// on 2D and 2DArray textures in SM 6.6 via mip-level `textureLoad` + `apply_depth_compare`, + /// honoring S# `LOD_BIAS`, `[MIN_LOD, MAX_LOD]`, T# `MIN_LOD`, point/linear/none mip filter, + /// point/bilinear XY filter (2x2 PCF), address modes, border color, `UNORM=1` / + /// `FORCE_UNORMALIZED`, `a16`/`d16`, static samplers, and texel offsets. + #[allow(clippy::too_many_arguments)] + fn mimg_sample_cmp_lod( + &mut self, + d: &Decoded, + dim: u64, + kind: TexKind, + tw0: Value, + tw1: Value, + ssamp: u64, + coords: &[Value], + lod: Value, + cmp_ref: Value, + ovgpr: Option<Value>, + unorm: bool, + comps: &[u64], + vdata: u64, + d16: bool, + act: Value, + run: Value, + ) -> LResult<()> { + let (s0, s1, s2, s3) = match self.static_samplers.get(&d.pc).copied() { + Some(w) => (self.c(w[0]), self.c(w[1]), self.c(w[2]), self.c(w[3])), + None => (self.sreg(ssamp), self.sreg(ssamp + 1), self.sreg(ssamp + 2), self.sreg(ssamp + 3)), + }; + + let res_tmp = self.tmp_var(); + let z0 = self.c(0); + self.wr(res_tmp, z0); + + self.guarded(run, &mut |lw| { + let tex = lw.s.heap_texture(tw0, kind, false, CompType::F32, 4, true); + + let scale = lw.fc(1.0 / 256.0); + let lb_sh = lw.shlc(s2, 18); + let lb_se = lw.ashrc(lb_sh, 18); + let lb_f = lw.sitof(lb_se); + let lod_bias = lw.fmul(lb_f, scale); + let l_raw = lw.fadd(lod, lod_bias); + + let min_u = lw.andc(s1, 0xFFF); + let min_f = lw.uitof(min_u); + let min_lod = lw.fmul(min_f, scale); + let max_s = lw.lshrc(s1, 12); + let max_u = lw.andc(max_s, 0xFFF); + let max_f = lw.uitof(max_u); + let max_lod_s = lw.fmul(max_f, scale); + let t_min_s = lw.lshrc(tw1, 8); + let t_min_u = lw.andc(t_min_s, 0xFFF); + let t_min_f = lw.uitof(t_min_u); + let t_min_lod = lw.fmul(t_min_f, scale); + + let l_lo = lw.fmax(l_raw, min_lod); + let l_hi = lw.fmin(l_lo, max_lod_s); + let l_clamped = lw.fmax(l_hi, t_min_lod); + + let one = lw.c(1); + let dims0 = lw.s.get_dimensions(tex.handle, Some(z0)); + let dims_m = lw.s.m.extract(dims0, 3); + let num_mips = lw.umax(dims_m, one); + let max_mip = lw.sub(num_mips, one); + let max_mip_f = lw.uitof(max_mip); + + let zf = lw.fc(0.0); + let l_pos = lw.fmax(l_clamped, zf); + let l_final = lw.fmin(l_pos, max_mip_f); + + let mip_s = lw.lshrc(s2, 26); + let mip_flt = lw.andc(mip_s, 3); + let mip_none = lw.cmpc(Pred::Eq, mip_flt, 0); + let mip_linear = lw.cmpc(Pred::Eq, mip_flt, 2); + + let t_ni = lw.un(op::ROUND_NI, t_min_lod); + let t_fl = lw.fmax(t_ni, zf); + let t_u = lw.ftoui(t_fl); + let m0_none = lw.umin(t_u, max_mip); + + let half_up = lw.fc(0.501953125); + let pt_sum = lw.fadd(l_final, half_up); + let pt_ni = lw.un(op::ROUND_NI, pt_sum); + let pt_fl = lw.fmax(pt_ni, zf); + let pt_u = lw.ftoui(pt_fl); + let m0_pt = lw.umin(pt_u, max_mip); + + let l_fl = lw.un(op::ROUND_NI, l_final); + let l_u = lw.ftoui(l_fl); + let m0_lin = lw.umin(l_u, max_mip); + let m0_p1 = lw.add(m0_lin, one); + let m1_lin = lw.umin(m0_p1, max_mip); + let w_mip = lw.fsub(l_final, l_fl); + + let m0_sel = lw.sel(mip_linear, m0_lin, m0_pt); + let m0 = lw.sel(mip_none, m0_none, m0_sel); + let m1 = lw.sel(mip_linear, m1_lin, m0); + + let is_mag = lw.fcmp(Pred::FOLe, l_raw, zf); ~~~~ --- Not certain about this one: mag-vs-min XY filtering is decided from `l_raw` (lod + bias, before the S# MIN/MAX_LOD and T# MIN_LOD clamps), while the mip is picked from the clamped LOD. With XY_MAG=point, XY_MIN=bilinear, MIN_LOD=1.0 and lod=-1.0, mip 1 is read (minified) but the point filter is used, so PCF edges come out hard.
bmjask commented 2026-12-01 21:42:15 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by bmjask on 2026-12-01T21:42:15Z.

Review: COMMENTED


Review of 4270faa2: 3 finding(s). 2 are inline. These are on lines outside the diff:

crates/xcl/src/gpu/dxil/lower_image.rs:1944: Only DIM 1|5 are rerouted. image_sample_c_l on 1D (0), 1DArray (4) and Cube (3) still reaches the (Some(z),..) if has_lz || !ps arm and the implicit sampleCmp arm, so their explicit LOD is still dropped. A compute IMAGE_SAMPLE_C_L DIM=3 with lod=2 still lowers to sampleCmpLevelZero and reads mip 0, and in a PS it uses derivative LOD. That's the bug this PR fixes, left in place for the remaining dims.


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:6140670509 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3298#pullrequestreview-6140670509). Originally posted by [bmjask](https://github.com/bmjask) on 2026-12-01T21:42:15Z. **Review:** COMMENTED --- Review of `4270faa2`: 3 finding(s). 2 are inline. These are on lines outside the diff: **`crates/xcl/src/gpu/dxil/lower_image.rs:1944`**: Only DIM 1|5 are rerouted. `image_sample_c_l` on 1D (0), 1DArray (4) and Cube (3) still reaches the `(Some(z),..) if has_lz || !ps` arm and the implicit sampleCmp arm, so their explicit LOD is still dropped. A compute IMAGE_SAMPLE_C_L DIM=3 with lod=2 still lowers to sampleCmpLevelZero and reads mip 0, and in a PS it uses derivative LOD. That's the bug this PR fixes, left in place for the remaining dims. --- _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._
Kalmarv commented 2026-12-02 19:39:05 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by Kalmarv on 2026-12-02T19:39:05Z.


Closing: oversized single-file patch duplicating mimg_gather4_lod with a failing GPU test on main and missing 1D/Cube cases.

<!-- greyfinch-sync:ivall/xcl:comment:7356508701 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3298#issuecomment-7356508701). Originally posted by [Kalmarv](https://github.com/Kalmarv) on 2026-12-02T19:39:05Z. --- Closing: oversized single-file patch duplicating `mimg_gather4_lod` with a failing GPU test on `main` and missing 1D/Cube cases.

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
xcl-dev/xcl!3298
No description provided.