Direct DXIL: clamp mip level and guard zero dims in image_sample_*_o offsets #3341
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/3341"
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-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 withumax(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:mimg'sovgpr(has_o) offset scaling path, when computing texel offsets for an explicit LOD (lod = Some(l)) or pixel-shader implicit LOD (calculate_lod), queryget_dimensions(tex, Some(0))fornum_mips, clamp the rounded LOD to[0, max(num_mips, 1) - 1]in both float (beforeftoui, handling+Inf) and integer domains, and then queryget_dimensions(tex, Some(clamped_level)).get_dimensions(tex, Some(0))query for_lz/gather/ default mip-0 cases, and clamp(w, h, d)withumax(dim, 1)across all branches so null views never divide by zero.offset_mip_clamp_tests::sample_offset_clamps_mip_and_guards_zero_dims_in_dxil(DXIL validation + IR check for both computeIMAGE_SAMPLE_L_Oand pixelIMAGE_SAMPLE_O) andoffset_mip_clamp_tests::gpu_sample_l_o_with_oob_lod_offsets_at_last_mip(guarded real-GPU dispatch testingIMAGE_SAMPLE_L_Oatlod = 5.0on a 2-mip8x8texture across offsets(0,0),(+1,0),(0,+1), and(-1,0)).Tested
cargo test -p xcl --lib -- gpu::dxil::lower_image::):sample_offset_clamps_mip_and_guards_zero_dims_in_dxil(only 1getDimensionscall) andgpu_sample_l_o_with_oob_lod_offsets_at_last_mipfailed (lane 0 offset=(0,0) at lod=5.0 on 2-mip texture: got 1.0, expected 6.0becauseGetDimensions(5)returned0->0.0/0.0 = NaNclamping to corner texel(0,0)).8 passed; 0 failed(0.23s), with all four offset lanes reading their exact4x4mip-1 texels (6.0,7.0,10.0,5.0).rustc 1.99.0, RTX 5070 Ti driver32.0.16.1074, DXC1.9.2609.5):cargo test --workspace -j 2 -- --test-threads=4 --skip test_title_runs_and_game_folder_is_untouched --skip uma_driver::tests::2611 passed; 0 failed; 49 ignored(149.5s).SKIPPED:1(1no heap windows).Regression run
Baseline (
mainancestorf0e8fdd5c3942ddd399d5ee11384c8595c5aacff) and this PR (bb5fddc7a6f08a15e387c5f58441c48a54f34b40), 150 s on RTX 5070 Ti:Not tested
recomp/emit.rsbackend (left untouched here).test_title_runs_and_game_folder_is_untouchedanduma_driver::tests::were skipped in the workspace test run.AI use
Imported from ivall/xcl. Originally posted by Foniass on 2026-12-01T05:25:54Z.
merge-check
base:
d0832e614fhead:
bb5fddc7a6merge: 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,
GetDimensionspast the last mip returns 0, and the offset's 0/0 gives NaN; the level is now clamped beforeftoui(so +Inf/NaN LODs are handled) andnum_mipsis guarded. It changes the default path: every_osample gets 3umax, and_l_oand implicit-LOD pixel-shader_osamples also get a secondgetDimensionsthat 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), andmimg_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 withmax(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 takingwave64from the runner's adapter, so they can differ on multi-GPU machines (userunner::device()); its doc says 4x4 / 2x2 but the texture is 8x8 / 4x4; theWaitForSingleObjectresult isn't checked. (4) The DXIL test (~2104, ~2118) only checks thatgetDimensionsappears at least twice, not that the clamp exists. (5) AI use names no tool or model, the commit areaDirect DXIL:should beRecompiler:, and the suite ran with--skipfilters. 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
d0832e61vs 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.
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_samplehas the same unclamped explicit-LOD code (ROUND_NE, fmax 0, ftoui, thenget_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.
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 1203The PS implicit-LOD offset branch also catches bias samples (IMAGE_SAMPLE_B_O), but
calculate_lodignores the bias thatsample_biasthen 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 samplessample_level(floor(b))while the offset falls into the_arm and uses mip 0, the same mismatch.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 1192The offset mip is picked with ROUND_NE, but RDNA2 point-mip selection rounds half up (
floor(lod + 0.501953125), asmimg_gather4_lodin 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.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 1186The clamp only bounds LOD to [0, num_mips-1] and ignores S# MIN_LOD/MAX_LOD/LOD_BIAS, the T# min_lod and the
cloperand, all of which the real sample applies (mimg_gather4_lodhandles 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.View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.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.