Direct DXIL: emulate explicit-LOD comparison sampling for image_sample_c_l(_o) #3298
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/3298"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
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_LandIMAGE_SAMPLE_C_L_O) dropped the explicit LOD operand:!ps), they lowered todx.op.sampleCmpLevelZero, always reading mip 0 regardless of the requested LOD.ps), they lowered to implicit-LODdx.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 2DArrayimage_sample_c_l/image_sample_c_l_ocan be lowered cleanly in SM 6.6 via mip-leveltextureLoad+apply_depth_compare, matching the existingmimg_gather4_lodapproach.What is in it
crates/xcl/src/gpu/dxil/lower_image.rs:sample_cmp_mipandmimg_sample_cmp_lod, and route 2D / 2DArrayimage_sample_c_landimage_sample_c_l_o(base.starts_with("sample") && has_c && has_l && matches!(dim, 1 | 5)) through them inmimg.LOD_BIAS,[MIN_LOD, MAX_LOD], T#MIN_LOD,MIP_FILTER(none,pointwith RDNA2 half-up0.501953125,lineartwo-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.sample_cmp_lod_tests:sample_cmp_lod_variants_translate_validate_and_avoid_implicit_or_lz_cmp: verifies 2D/2DArrayIMAGE_SAMPLE_C_L/IMAGE_SAMPLE_C_L_O(compute and pixel,UNORM=1,A16/D16) validate withdxil.dlland lower viagetDimensions+textureLoadinstead ofsampleCmpLevelZero/sampleCmp.gpu_sample_cmp_lod_reads_explicit_mip_and_filters: real 2-mipTexture2D<float>GPU dispatch verifying explicitlod=1.0vslod=0.0,lod=0.5linear mip blending, 2x2 bilinear PCF on a checkerboard mip 1, S#LOD_BIAS=+1.0, andIMAGE_SAMPLE_C_L_Otexel offset (guarded viawave64_supported/wave32_supportedso no-GPU CI skips cleanly).Tested
32.0.16.1074),rustc 1.99.0 (b940084d7 2026-09-28), DXC 1.9 (1.9.2609).dx.op.sampleCmpLevelZero; GPU test returned0.0from mip 0 instead of1.0from mip 1 forlod=1.0).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.cargo test --workspace -j 2 -- --test-threads=4 --skip test_title_runs_and_game_folder_is_untouched --skip uma_driver::tests::withXCL_DXC_DIRset to DXC 1.9): 2601 passed, 0 failed, 49 ignored, 9 filtered (426.1s).SKIPPEDlines in workspace run (27 total): 21no wave64(RTX 5070 Ti is wave32), 3 wave32 per-lane#1426(V_MUL_F16,V_PK_MUL_F16,V_CVT_F16_F32), 2no heap windows(defaultXCL_HEAP_WINDOWSoff), 1no build ahead(XCL_PSO_PRECOMPILEoff).cargo build --release -p xcl -j 4) succeeded and passed the paired 150 s title smoke below.Regression run
main (
f0e8fdd5c3942ddd399d5ee11384c8595c5aacff):PR (
4270faa28b06ea7b6c9ad2d8e463051a16899173):Not tested
uma_driver::tests::andtest_title_runs_and_game_folder_is_untouched(filtered out of the workspace test run).IMAGE_SAMPLE_C_Lon 1D / 1DArray / Cube dimensions, and the HLSL fallback path (recomp/emit.rs, which is kept unchanged in this Direct DXIL PR).AI use
Imported from ivall/xcl. Originally posted by Foniass on 2026-11-29T14:59:04Z.
Review (head
4270faa2, diff read, not a merge-check):mimg_gather4_lodinstead of sharing them.D3D12CreateDevice) that ignoresrunner::device()andXCL_ADAPTER.dxil:with no body, and AI use names no model.Suggested verdict: not yet (share the code, use the test runner, split).
(Review written with Claude Code, Claude Opus 5.5.)
Imported from ivall/xcl. Originally posted by Foniass on 2026-12-01T06:13:56Z.
merge-check
base:
d0832e614fhead:
4270faa28bmerge: 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_ltosampleCmpLevelZerooutside pixel shaders and to implicit-LODsampleCmpin pixel shaders (the HLSL path does the same, emit.rs ~2850). Now every such instruction becomes manual comparison filtering, up to 2 mips x 2x2textureLoads plus up to 3getDimensions, with S# / T# bits decoded by hand and no switch; SM 6.7SampleCmpLevelisn't used. Findings: (1)mimg_sample_cmp_lodreads S# words 0-3 fromsreg(ssamp..ssamp+3)(~761-763). Since4250e761(after this PR's base),waterfall::collapseremoves thev_readfirstlanes that write those SGPRs in a collapsed sampler waterfall and routes the per-lane sampler throughmimg_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 whenwaterfall_ssamphas the pc, would fix it (mimg_gather4_lodhas 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_comparedoesn't. (4) AI use names no model; the subject areadxil:should beRecompiler:; ~700 lines; the GPU test's doc mentions MIP_FILTER=none but no case covers it. Fine: the S# bit fields matchsampler_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 usesskip_test.performance: 60 s smoke of a retail title, main
d0832e61vs 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.
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 667Not certain about this one: the emulated compare passes the raw
cmp_reftoapply_depth_comparewithout 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.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 835Not 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.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_lon 1D (0), 1DArray (4) and Cube (3) still reaches the(Some(z),..) if has_lz || !psarm 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.
Imported from ivall/xcl. Originally posted by Kalmarv on 2026-12-02T19:39:05Z.
Closing: oversized single-file patch duplicating
mimg_gather4_lodwith a failing GPU test onmainand missing 1D/Cube cases.Pull request closed