Skip to content

dxil: emulate explicit-LOD gather4_l(_o) and support UNORM=1 non-cube sampling - #1951

Open
raylitebull wants to merge 1 commit into
xcl-dev:mainfrom
raylitebull:feat/dxil-gather4-lod-unorm
Open

raylitebull wants to merge 1 commit into
xcl-dev:mainfrom
raylitebull:feat/dxil-gather4-lod-unorm

Conversation

@raylitebull

@raylitebull raylitebull commented Nov 24, 2026 •

Copy link
Copy Markdown
Member

Emulates explicit-LOD image_gather4_l / image_gather4_l_o / image_gather4_c_l / image_gather4_c_l_o (plus _cl clamp, A16, D16, and UNORM=1 / FORCE_UNORMALIZED) in the direct RDNA2 -> DXIL translator across all 8 RDNA2 address modes, and adds non-cube UNORM=1 coordinate normalization for sample* and gather4*.

Part of #1269 and #1833.

What is in it

  • crates/xcl/src/gpu/dxil/lower_image.rs (mimg_gather4_lod, apply_rdna2_addr_mode, apply_depth_compare): emulate explicit-LOD gather4_l(_o) on Texture2D and Texture2DArray via 4 mip-level dx.op.textureLoad taps in hardware gather order (-,+), (+,+), (+,-), (-,-) honoring all 8 SQ_TEX_CLAMP address modes, S# signed S5.8 LOD_BIAS (s2[13:0] / 256.0), T# MIN_LOD (tw1[19:8], u4.8 / 256.0 — MIP_FILTER=none reads floor(T# MIN_LOD) at every LOD, and filtered samplers raise S#-clamped LOD to T# MIN_LOD before half-up rounding), S# [min_lod, max_lod] + mip_filter == 0 clamping, RDNA2 half-up mip rounding (floor(lod + 0.501953125) = 257/512, 0x3F00_8000) + GetDimensions mip count, signed 6-bit texel offsets (has_o), S# border color types (s3[31:30]), SQ_TEX_DEPTH_COMPARE (s0[14:12]), and #1969's MIMG heap-bounds guard (self.guarded(run, ...)).
  • crates/xcl/src/gpu/dxil/lower_image.rs (mimg_unorm_sample): normalize unnormalized texel coordinates (and optional signed 6-bit offsets) via mip GetDimensions for non-cube textures (D1, D2, D3, D1Array, D2Array) before DXIL sample/gather emission, with #1969 heap-bounds guard (safe_tw0 for implicit_lod).
  • gather4_lod_tests: gather4_lod_variants_translate_and_validate, gather4_lod_emits_bias_and_rdna2_half_up_rounding, gather4_lod_honors_tsharp_min_lod_and_mip_filter_none, and non_cube_unorm_sampling_translates_and_validate.

Tested

  • Toolchain: Windows 11 x64, rustc 1.99.1 (58cf8f9c3 2026-10-13), rebased cleanly onto ae0518c (ff184be).
  • ff184be integrates the T# MIN_LOD fix (@brothernog catch) and #1969 heap-bounds guard atop ae0518c via pure git during a local test hold; queued for GitHub Actions Build and test and fresh AMD / NVIDIA verification (gather4_lod_tests).
  • Earlier head e201efe passed cargo check --workspace --tests, gather4_lod_tests (3/3), and @upipibi's 5-case DXIL verification.

Regression run

  • Standalone 60 s scripts/smoke.ps1 pair and tests on ff184be vs ae0518c have not been run yet. AMD and NVIDIA reviewers are invited to run gather4_lod_tests, scripts/smoke.ps1, and pixel tests on ff184be vs ae0518c (including XCL_DIRECT_DXIL=1) and post results in the comments.

Not tested

  • cargo check / gather4_lod_tests and scripts/smoke.ps1 on updated head ff184be, runtime GPU pixel comparison against HLSL on AMD / NVIDIA / Intel hardware, wave32-only adapters, and Linux/Wine/vkd3d-proton.

AI use

  • Antigravity assisted with implementation and DXIL validation tests; the author reviewed the diff.

@upipibi

upipibi commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

Checked on an RTX 3070 (Ampere, driver 616.56, Windows 11, debug build) at 940c51b.

The batch run is clean: dxil:: and bvh:: units pass, and so do the gpu_ tests with XCL_DIRECT_DXIL=0 and 1 (110 passed each, same as main). None of the existing GPU tests reaches a gather4_l with a level above 0, though, so I ran a pixel-shader check. It has an 8×8 texture with 4 levels, where level k holds 100·k + x. The test reads the 2×2 footprint and goes through the direct translator (draw_console_views routed through direct_or).

case expected level this PR (direct) #1812 7f804be (direct / HLSL)
LOD 1.0, MIP_FILTER point 1 ok ok / ok
LOD 2.0, S# MAX_LOD 1.0 1 ok ok / ok
LOD 1.0, MIP_FILTER none 0 ok ok / ok
LOD 0.5, MIP_FILTER point 1 level 0 (all 256 pixels) ok / ok

The last row comes from l_rnd = ROUND_NE(l). RDNA2 picks the level as floor(lod + 0.501953125), measured on gfx1030/gfx1036 in boykopovar/AnyPS5's hardware-oracle runs. That is, it rounds half up, so LOD 0.5 reads level 1 and 2.5 reads level 3, where round-to-even gives 0 and 2. From reading only (not run): the S# LOD_BIAS isn't added before the clamp either.

Overlap: #1812 (brothernog, open and mergeable) already does explicit-LOD gather4_l/_l_o for 2D and 2D arrays on both backends. It applies the bias, uses the measured rounding, and has GPU tests. This PR's gather4_l half duplicates it for the direct path. The UNORM=1 non-cube sampling half is separate and could stand as its own PR.

AI-assisted (Claude Code). Synthetic tests only.

@raylitebull
raylitebull force-pushed the feat/dxil-gather4-lod-unorm branch from 940c51b to e201efe Compare November 25, 2026 00:16
@raylitebull

Copy link
Copy Markdown
Member Author

Thanks for the thorough pixel check and catch, @upipibi!

Updated #1951 cleanly on top of 80f4819 in commit e201efe to fix both points in crates/xcl/src/gpu/dxil/lower_image.rs (mimg_gather4_lod):

  1. S# LOD_BIAS (s2[13:0]): extracts the signed 14-bit S5.8 LOD_BIAS (((s2 << 18) as i32 >> 18) / 256.0) and adds it to lod before optional cl and [min_lod, max_lod] clamping.
  2. RDNA2 half-up mip rounding: switched from ROUND_NE to floor(lod + 0.501953125) (op::ROUND_NI, 257/512 / 0x3F00_8000) so LOD 0.5 selects level 1 and LOD 2.5 selects level 3 (with MIP_FILTER=none still collapsing max_lod = min_lod).

Verified locally with cargo check --workspace --tests and cargo test -p xcl --lib gather4_lod_tests (3 passed, 0 failed, dxil 1.8.2502.11 validated, including new gather4_lod_emits_bias_and_rdna2_half_up_rounding); GPU pixel checks have not been re-run on e201efe yet.

Re #1812: understood on the overlap for gather4_l/_l_o! If #1812 merges first (or if maintainers prefer keeping #1951 focused strictly on non-cube UNORM=1 normalization), I am happy to rebase #1951 down to just mimg_unorm_sample and its tests.

@Anon64

Anon64 commented Nov 25, 2026

Copy link
Copy Markdown

Regression run for this PR: head 940c51b, based on main 62265d3. RTX 3060 12 GB (581.80, wave32), Windows 11 26200, release, XCL_NETWORK=0, A pressed every 4 s from 20 s, scripts/smoke.ps1 on a retail title for 60 s, in one batch with the main run.

main 62265d3:

xcl-smoke v1
commit: 62265d3bbde2255f849fb650cc29872df91a4cf0
gpu: NVIDIA GeForce RTX 3060
title: 21222967
seconds: 60
exit: 0x0
frames shown: 1629 (PresentX 1629)
presents per 2 s (last 3): 60 60 47
device removals: 0
crashes: 0
peak commit: 23489 MB

this PR:

xcl-smoke v1
commit: 940c51b2d9c362802c924405abc606f2cd9ca82a
gpu: NVIDIA GeForce RTX 3060
title: 21222967
seconds: 60
exit: 0x0
frames shown: 1613 (PresentX 1614)
presents per 2 s (last 3): 60 49 50
device removals: 0
crashes: 0
peak commit: 24061 MB

No device removals or crashes; 99 % of main's frames, 102 % of its peak commit.

Not tested: the whole suite; the direct DXIL path this PR changes (these runs use the default HLSL path, XCL_DIRECT_DXIL unset).

Written with Claude Code (Claude Opus 5.5) from runs on this machine.

@raylitebull

Copy link
Copy Markdown
Member Author

Quick overlap note: full credit to #1812 (@brothernog), which already implements explicit-LOD gather4_l(_o) across both the HLSL and Direct DXIL backends.

#1951 adds Direct DXIL support for UNORM=1 non-cube sampling (mimg_unorm_sample) as well as compare/clamp explicit-LOD gather4 forms (gather4_c_l / gather4_cl). Once #1812 lands, we will rebase and trim #1951 down to only the non-overlapping UNORM=1 and _c_ / _cl paths.

@upipibi

upipibi commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

Re-checked e201efe on the RTX 3070 (driver 616.56, Windows 11, debug) with the same pixel test through the direct translator, plus one bias case:

case expected level 940c51b e201efe
LOD 1.0 1 ok ok
LOD 2.0, S# MAX_LOD 1.0 1 ok ok
LOD 1.0, MIP_FILTER none 0 ok ok
LOD 0.5 1 level 0 ok
LOD 2.0, S# LOD_BIAS −0.75 (0x3F40) → 1.25 1 level 2 ok

Both points are fixed on hardware. The overlap with #1812 for the gather4_l part still stands; that's for the maintainers to pick.

AI-assisted (Claude Code). Synthetic tests only.

@brothernog

Copy link
Copy Markdown

Heads-up on overlap, from reading the diff, not from a run: #1812, opened earlier, emulates the same image_gather4_l / _l_o forms with four mip-level Loads and the S# address modes. It does this in both the HLSL and the direct DXIL backend, with GPU pixel tests on both: 18 S# / T# cases, plus @upipibi's three review cases. The two PRs conflict in the same block of lower_image.rs, so whichever lands second needs to be rebuilt on the other.

Two differences:

One way to split it: #1812 for the uncompared gather4_l level and taps on both backends, and this PR rebased on top of it for the compare forms and UNORM. The maintainers can decide.

This comment was AI-assisted.

@raylitebull

Copy link
Copy Markdown
Member Author

Thanks for the detailed comparison, @brothernog! Agreed on the clean split: once #1812 lands for uncompared explicit-LOD \gather4_l\ / _l_o\ across HLSL and Direct DXIL (including \T# MIN_LOD\ floor handling), we will rebase and trim #1951 strictly down to the non-overlapping \gather4_c_l\ / _c_l_o\ compare forms and non-cube \UNORM=1\ sampling paths.

@raylitebull
raylitebull force-pushed the feat/dxil-gather4-lod-unorm branch from e201efe to ff184be Compare November 25, 2026 01:54
@raylitebull

Copy link
Copy Markdown
Member Author

Thanks for catching the missing T# MIN_LOD clamping, @brothernog!

Updated #1951 cleanly onto ae0518c (ff184be):

  • T# MIN_LOD (tw1[19:8], u4.8 / 256.0) in mimg_gather4_lod: MIP_FILTER=none now reads floor(T# MIN_LOD) at every LOD, and non-none MIP_FILTER raises the S#-clamped level to T# MIN_LOD before half-up + 0.501953125 rounding (added gather4_lod_honors_tsharp_min_lod_and_mip_filter_none test).
  • MIMG heap-bounds guard (#1969): wired self.guarded(run, ...) (plus safe_tw0 for implicit_lod) into both mimg_gather4_lod and mimg_unorm_sample (and we can still trim #1951 cleanly to gather4_c_l(_o) + UNORM=1 if #1812 lands first).

Pushed ff184be for GitHub Actions Build and test and fresh verification!

@upipibi

upipibi commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

Re-checked ff184be on the RTX 3070 (driver 616.56, Windows 11, debug) through the direct translator. The same six level cases as before all pass: LOD 1.0; LOD 2.0 under S# MAX_LOD 1.0; MIP_FILTER none; LOD 0.5; LOD 2.0 with S# LOD_BIAS −0.75; and LOD 0.499 (literal), which RDNA2 reads at level 1. For comparison, #1812 at b0f160f still reads level 0 for that last case.

AI-assisted (Claude Code). Synthetic tests only.

@DavidM1911

Copy link
Copy Markdown
Contributor

Verification on RTX 3060 Ti (Windows 11), branch merged with current main:

Thanks!

@JJAutoCompare

Copy link
Copy Markdown

Tested

Verified PR #1951 from a clean checkout at ff184bedd65af7d0e74a32a1515e0b7d591d8242. GitHub showed the PR open at that SHA before and after the run.

  • Windows 11 Enterprise Evaluation 26200.9974, AMD Ryzen 7 3800X, Intel Arc A770 driver 32.0.101.9175 (installed hardware). Rust and Cargo 1.100.0, x86_64-pc-windows-msvc, unoptimized test profile. The runtime log confirmed dxcompiler.dll and dxil.dll 1.9.2609.5 from %LOCALAPPDATA%\XclTooling\dxc-1.9.2609\bin\x64 via XCL_DXC_DIR.
  • Two Cargo jobs, incremental compilation off, dev and test debuginfo off, XCL_ADAPTER=A770, CARGO_TARGET_DIR=L:/XclTooling/pr2014-1c971de6/target. TEMP and TMP pointed to the run’s temp directory under L:/XclTooling/pr1951.
cargo test -p xcl --lib gpu::dxil::lower_image::gather4_lod_tests::non_cube_unorm_sampling_translates_and_validate -- --exact --nocapture

1 passed, 0 failed, 0 ignored, 0 skipped (1,066 filtered out, Cargo exit 0). The one fixture translated seven synthetic UNORM=1 RDNA2 shaders and DXC accepted all seven emitted DXIL containers. The cases cover samples on 1D, 2D, 3D, 1D-array, and 2D-array textures, plus 2D and 2D-array LOD-zero gathers, including a 3D sample offset and a 2D-array gather offset with D16 output.

Not tested

GPU execution, numerical sampling or pixel parity, bounds behavior, the explicit-mip gather4_l emulation covered separately by #1812, retail titles, smoke runs, and the broader suite.

AI use

T3 Code / Codex (gpt-6.2-sol) ran the checked-in fixture and wrote this report.

IvyNotFound added a commit to IvyNotFound/xcl that referenced this pull request Nov 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants