Skip to content

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

Open
Kalmarv wants to merge 1 commit into
xcl-dev:mainfrom
Kalmarv:samplers/sample-cmp-lod
Open

Kalmarv wants to merge 1 commit into
xcl-dev:mainfrom
Kalmarv:samplers/sample-cmp-lod

Conversation

@Kalmarv

@Kalmarv Kalmarv commented Nov 29, 2026

Copy link
Copy Markdown
Contributor

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.

@github-actions github-actions Bot added area:recompiler RDNA2 decoder, HLSL/direct-DXIL recompiler, wave64/wave32 gpu:amd Code is specific to AMD / RDNA / wave64 GPUs (scope tag; see needs:amd-test) labels Nov 29, 2026
@Kalmarv Kalmarv closed this Nov 29, 2026
@Kalmarv Kalmarv reopened this Nov 29, 2026
@Foniass

Foniass commented Nov 29, 2026

Copy link
Copy Markdown
Contributor

Review (head 4270faa, 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 (Tests: serialise the pipeline tests that store into the library builder #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

Foniass commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

merge-check
base: d0832e6
head: 4270faa
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 4250e76 (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 d0832e6 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 bmjask left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

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)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:recompiler RDNA2 decoder, HLSL/direct-DXIL recompiler, wave64/wave32 gpu:amd Code is specific to AMD / RDNA / wave64 GPUs (scope tag; see needs:amd-test)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants