Skip to content

Recompiler: gather4 with an explicit LOD reads the mip it names - #1812

Open
brothernog wants to merge 1 commit into
xcl-dev:mainfrom
brothernog:gather4-explicit-lod
Open

brothernog wants to merge 1 commit into
xcl-dev:mainfrom
brothernog:gather4-explicit-lod

Conversation

@brothernog

@brothernog brothernog commented Nov 24, 2026 •

Copy link
Copy Markdown

image_gather4_l and image_gather4_l_o read mip 0 in the HLSL backend, whatever LOD the program passes: Gather{ch}() has no level argument, and the texel offset was scaled with mip 0's size (emit.rs, the has_lz || gather4 arm). #1951 (merged) fixed the same forms in the direct DXIL backend; this PR does the HLSL side with the same level rule and adds GPU tests that run both backends. In the retail title's shader pack, image_gather4_l appears in 16 programs and image_gather4_l_o in 16.

What changes

  • The level follows the console, as in dxil: emulate explicit-LOD gather4_l(_o) and support UNORM=1 non-cube sampling #1951: LOD plus the S# LOD_BIAS, clamped to the S# MIN_LOD..MAX_LOD (u4.8), raised to the T# MIN_LOD (word1[19:8], u4.8), then floor(l + 257/512), then clamped to the view's last mip.
    • RDNA2 first rounds the LOD to 1/256 with halves up, then takes the nearest level with ties up (hardware measurements on gfx1036 that @upipibi pointed to in review: 0.5 → 1, 2.5 → 3, and 0.499 → 1). floor(l + 257/512) does both steps; round() in DXIL goes to even.
    • With MIP_FILTER none it reads floor(T# MIN_LOD) at every LOD (1.75 → 1, 2.5 → 2). The T# MIN_LOD comes after the S# clamp.
    • The T# MIN_LOD is taken relative to the view's base level, like the LOD. A shortcut: comment marks it: the tests only use BASE_LEVEL 0.
    • A linear mip filter gathers like point.
  • The four texels are Loads at that level, so the host sampler's MinLOD/MaxLOD, bias and filter, and the view's ResourceMinLODClamp, can't move them to another level. The S# addressing is applied per axis in the shader (gat_ax in the HLSL prelude):
  • The footprint is floor(coord * size(level) - 0.5) plus the 6-bit signed offset, counted in texels of that level. The output is in Gather's component order, for the dmask channel.
  • 2D and 2D arrays are handled. Cube and 3D with an explicit LOD fail with the translator error gather4 explicit LOD (not 2D) instead of silently reading mip 0.
  • Unchanged in the HLSL backend, still reading mip 0 as before (pointed out by @futurepro62): the compare forms (gather4_c_l, _c_l_o), the bias forms (gather4_b, _b_cl, _c_b and their _o forms), and plain gather4 / gather4_c in a pixel shader.

Files

  • gpu/recomp/emit.rs: the explicit-LOD gather path and the gat_ax helper.
  • gpu/recomp/runner.rs: kernel test textures can carry mips. TestTexture::mips() works the count out from the data's length (SRV-only 2D and 2D arrays whose data holds each slice's full chain to some level; 1 otherwise). It is a method, not a field, so no other test's literal changes.
  • gpu/recomp/tests.rs:
    • gpu_gather4_explicit_lod_hlsl and gpu_gather4_explicit_lod_direct run the same cases through each backend (one test per backend, so a driver crash names it). The direct one runs dxil: emulate explicit-LOD gather4_l(_o) and support UNORM=1 non-cube sampling #1951's code on a GPU. They use an 8×8 texture and a 2-layer array, 4 mips each, with distinct values per mip and per texel, and 18 S# / T# cases: point, linear and no mip filter; MAX_LOD 1, MIN_LOD 2, bias ±1; wrap, mirror, mirror once and mixed axes; the border and half-border variants; T# MIN_LOD 1.75 and 2.5 with no mip filter, 1.75 with a point mip filter, and 1.75 over an S# MAX_LOD of 1. LODs include the ties 0.5 and 2.5, 0.499, one past the last mip and one below 0, plus negative and far offsets.
    • gpu_ps_image_gather4_l_s_sharp_level: @upipibi's three review cases, in a pixel shader through the runtime's own sampler_d3d12 (HLSL only: the draw helper is HLSL-only).

Tested

  • RTX 3050 8 GB (desktop, Ryzen 7 5800XT), NVIDIA 610.88, Windows 11 build 26300, Rust 1.100.0 MSVC, DXC 1.9.2602.17.
  • On main 68a284a + this PR (a56538a):
    • cargo test -p xcl --lib -- gather4 --test-threads=1 (debug): 8 passed.
    • cargo test --release -p xcl --lib -- --test-threads=4: 1222 passed, 1 failed, 26 ignored. The failure, gpu::recomp::lower_dxil::tests::a_bridged_mask_write_where_one_half_is_elsewhere_meets_no_barrier, fails the same way on main 68a284a alone.
    • With emit.rs back to main (tests kept): gpu_gather4_explicit_lod_hlsl fails (gather4_l hlsl [clamp, point mip] lane 8 (uv 0, 1; lod 0.5; offset (-1, 0))) and so does gpu_ps_image_gather4_l_s_sharp_level ((a) LOD 1, full range, point mip); gpu_gather4_explicit_lod_direct passes.
    • cargo clippy --release -p xcl --all-targets: no new warnings (818 on 68a284a and on this branch, same count).
  • Earlier heads were checked by @futurepro62 (RTX 5070 Ti, and an RDNA2 iGPU where the test process crashed at 8eb5a5c; see the thread), @upipibi (RTX 3070) and @JJAutoCompare (Arc A770).

Regression run
With XCL_NETWORK=0 XCL_PAGE_BUDGET_MB=4096, back to back, main first (one run each; frame counts vary from run to run, so the difference is noise-level):

main 68a284a:

xcl-smoke v1
commit: 68a284a596bd558b6bd9db6eedda6d6aa178c360
gpu: NVIDIA GeForce RTX 3050
title: retail-title
seconds: 60
exit: 0x0
frames shown: 1086 (PresentX 1086)
presents per 2 s (last 3): 51 51 50
device removals: 0
crashes: 0
peak commit: 14657 MB

This PR (a56538a):

xcl-smoke v1
commit: a56538a588f72f053be108b6efedfa044d248839
gpu: NVIDIA GeForce RTX 3050
title: retail-title
seconds: 60
exit: 0x0
frames shown: 1193 (PresentX 1193)
presents per 2 s (last 3): 52 54 54
device removals: 0
crashes: 0
peak commit: 15260 MB

Overlap

Not tested

  • AMD at this head (the RDNA2 crash at 8eb5a5c is not reproduced or explained here) and Intel at this head; wave64-native hardware.
  • A static (constant) S#, a T# MIN_LOD with BASE_LEVEL > 0, the half-border modes on hardware, integer-format textures.
  • Any visible change in a title.

AI use

  • Claude Code (Claude Opus 5.5): rebase, implementation, tests and measurements, and this description. I checked the diff and the revert checks.

AI-assisted.

@upipibi

upipibi commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Checked on Ampere: RTX 3070 8 GB, driver 616.56, Windows 11, debug, 5f3e9ec merged onto main 177cd40.

Merge with main. There is one conflict, in lower_image.rs: main moved the sampler choice into self.mimg_sampler(d.pc, has_c, s3). Keeping that line and your glod lines below it builds cleanly.

Results. gpu_gather4_explicit_lod passes on both backends. My own check also passes: a pixel-shader image_gather4_l at LOD 1 on a 4-level 8×8 RGBA32F texture, with exact footprints for all 256 pixels.

A gap: the sampler's LOD clamp. The mip is chosen from the raw LOD, but the four SampleLevel taps go through the host sampler. That sampler has the S#'s MinLOD / MaxLOD (desc.rs:323-324). When the sampler clamps the LOD to another level, the taps land on that level at the chosen mip's texel centres, which fall between its texels. Same texture, clamp to edge, point x/y:

Case Console level Wrong pixels (HLSL / direct) Pixel (0,0): got vs want
LOD 1, full range, point mip 1 0 / 0
LOD 2, S# MAX_LOD 1.0 1 192 / 192 [100,101,101,100] vs [100,100,100,100]
LOD 1, MIP_FILTER none (host MaxLOD = MinLOD) 0 224 / 224 [0,1,1,0] vs [0,0,0,0]

"Console level" follows a hardware measurement on RDNA2 (gfx1036) in boykopovar/AnyPS5#23856. For image_gather4_l it found: LOD_BIAS is added first, then the S# MIN_LOD / MAX_LOD clamp applies; MIP_FILTER none reads the T# MIN_LOD rounded down; a linear MIP_FILTER gathers like point. Clamping the LOD the same way before choosing the mip would make the host's clamp a no-op. A sampler with the full LOD range, no bias and a point mip filter is not affected.

On rounding versus truncation: the same measurement found the level changes at exactly n + 0.5, and ties go up (0.5 → 1, 1.5 → 2, 2.5 → 3). round() in HLSL is round-half-to-even in DXIL, so this PR picks one level lower than the hardware at 0.5, 2.5, 4.5, and so on. floor(lod + 0.5) matches at the ties. Values up to 1/512 below a tie also went up there, consistent with the LOD being rounded to 1/256 first (their fit, not a measured mechanism). (Edited: an earlier version of this comment said I had found no measurement.)

Test (pixel shader, 3 cases)
fn review_gather4_l_case(lod: u64, w1: u32, w2: u32, level: u32) -> (usize, Option<(usize, usize, [f32; 4], [f32; 4])>) {
    use windows::Win32::Graphics::Direct3D12::*;
    use windows::Win32::Graphics::Dxgi::Common::{DXGI_FORMAT_R32G32B32A32_FLOAT, DXGI_SAMPLE_DESC};
    let inv = (1.0f32 / 16.0).to_bits();
    let ps = prog(&[
        ("V_MUL_F32", "ENC_VOP2", &[("VDST", 4), ("SRC0", 255), ("VSRC1", 2)], Some(inv)),
        ("V_MUL_F32", "ENC_VOP2", &[("VDST", 5), ("SRC0", 255), ("VSRC1", 3)], Some(inv)),
        ("V_MOV_B32", "ENC_VOP1", &[("VDST", 6), ("SRC0", lod)], None),
        ("IMAGE_GATHER4_L", "ENC_MIMG", &[("VDATA", 8), ("VADDR", 4), ("SRSRC", 4), ("SSAMP", 6), ("DMASK", 0x1), ("DIM", 1)], None),
        ("EXP", "ENC_EXP", &[("TGT", 0), ("EN", 0xF), ("VSRC0", 8), ("VSRC1", 9), ("VSRC2", 10), ("VSRC3", 11), ("DONE", 1), ("VM", 1)], None),
    ]);
    let mut data = Vec::new();
    for k in 0..4u32 {
        let n = 8 >> k;
        for _y in 0..n {
            for x in 0..n {
                data.extend_from_slice(&[(100 * k + x) as f32; 4].map(f32::to_le_bytes).concat());
            }
        }
    }
    let res = D3D12_RESOURCE_DESC {
        Dimension: D3D12_RESOURCE_DIMENSION_TEXTURE2D,
        Width: 8,
        Height: 8,
        DepthOrArraySize: 1,
        MipLevels: 4,
        Format: DXGI_FORMAT_R32G32B32A32_FLOAT,
        SampleDesc: DXGI_SAMPLE_DESC { Count: 1, Quality: 0 },
        ..Default::default()
    };
    let mut srv = D3D12_SHADER_RESOURCE_VIEW_DESC {
        Format: DXGI_FORMAT_R32G32B32A32_FLOAT,
        ViewDimension: D3D12_SRV_DIMENSION_TEXTURE2D,
        Shader4ComponentMapping: D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING,
        ..Default::default()
    };
    srv.Anonymous.Texture2D = D3D12_TEX2D_SRV { MipLevels: 4, ..Default::default() };
    let tex = runner::ViewTexture { res, srv, data, row_bytes: 8 * 16 };
    let s_sharp = [2 | 2 << 3 | 2 << 6, w1, w2, 0];
    let mut sgprs = vec![0u32; 32];
    sgprs[16..24].copy_from_slice(&tsharp(0, 77, 9));
    sgprs[24..28].copy_from_slice(&s_sharp);
    let px = draw_console_views(None, Some(&ps), &[[0.0, 0.0], [0.0, 32.0], [32.0, 0.0]], &sgprs, &[tex], &[crate::gpu::desc::sampler_d3d12(&s_sharp, false)]);
    let n = (8 >> level) as i32;
    let cl = |c: i32| c.clamp(0, n - 1);
    let mut wrong = Vec::new();
    for (i, p) in px.iter().enumerate() {
        if p[0] < 0.0 {
            continue; // not covered
        }
        let (x, y) = (i % 16, i / 16);
        let u = (x as f32 + 0.5) / 16.0;
        let left = (u * n as f32 - 0.5).floor() as i32;
        let _ = y;
        let t = |c: i32| (100 * level as i32 + cl(c)) as f32;
        let want = [t(left), t(left + 1), t(left + 1), t(left)];
        if *p != want {
            wrong.push((x, y, *p, want));
        }
    }
    (wrong.len(), wrong.first().copied())
}

#[test]
fn review_gather4_l_reads_the_level_it_names() {
    if !gpu_ready() {
        return;
    }
    let a = review_gather4_l_case(242, 0xFFF << 12, 1 << 26, 1); // lod 1.0, full range, point mip
    let b = review_gather4_l_case(244, 0x100 << 12, 1 << 26, 1); // lod 2.0, MAX_LOD 1.0
    let c = review_gather4_l_case(242, 0xFFF << 12, 0, 0); // lod 1.0, MIP_FILTER none
    assert_eq!((a.0, b.0, c.0), (0, 0, 0), "{a:?} {b:?} {c:?}");
}

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

@futurepro62

futurepro62 commented Nov 24, 2026 •

Copy link
Copy Markdown

Full-suite run, since this PR has no test report yet. Tested exact head 5f3e9ec (on its base b9b6ea6).

Tested

  • NVIDIA GeForce RTX 5070 Ti 16 GB (Blackwell, 32-lane waves only, mesh shaders), driver 616.64, Ryzen 7 9800X3D, Windows 11 Home 26300, Rust 1.100.0 MSVC, DXC + dxil.dll 1.9.2602.17 (Windows SDK 10.0.26100), debug, D3D12 debug layer installed.
  • cargo build --workspace, then XCL_REQUIRE_GPU=mesh cargo test --workspace -- --test-threads=4 --skip heap_window_out_of_memory_falls_back_to_the_switch_form --skip pso_measurement_compiles_an_exact_synthetic_record: 888 passed, 0 failed, 42 ignored. SKIPPED (no wave64): gpu_geometry_wave32_prefix_count_over_a_mask, gpu_wave32_program_in_wave64_hardware.
  • The new tests ran and passed: gpu_gather4_explicit_lod.
  • Same head on the AMD Radeon integrated GPU (iGPU) of the Ryzen 7 9800X3D (RDNA2, XCL_ADAPTER=radeon), cargo test --workspace -- --test-threads=4: 890 passed, 0 failed, 42 ignored. SKIPPED: heap_window_out_of_memory_falls_back_to_the_switch_form (no heap windows).
  • Two tests were skipped with --skip on NVIDIA. Both fail on main 7f7e768 too, not because of this PR: under XCL_REQUIRE_GPU=mesh, runner::skip_test turns their skips into failures. heap_window_out_of_memory_falls_back_to_the_switch_form skips when XCL_HEAP_WINDOWS is unset (Tests: heap_window_out_of_memory_falls_back_to_the_switch_form fails under XCL_REQUIRE_GPU=mesh (opt-in skip counted as missing coverage) #1651), and shaders::tests::pso_measurement_compiles_an_exact_synthetic_record skipped because Developer Mode was off. Developer Mode was turned on before the AMD run, and there pso_measurement_compiles_an_exact_synthetic_record ran and passed.

Not tested

  • a release build, a game (test run only).

AI use

  • Claude Code (Claude Opus 5.5) ran the tests and wrote this report. I checked it before posting.

@brothernog
brothernog force-pushed the gather4-explicit-lod branch 2 times, most recently from f0d69fe to 7f804be Compare November 24, 2026 22:27
@brothernog

Copy link
Copy Markdown
Author

@upipibi thanks, both points were real, and the measurement pointer settled the rounding question I had left open. I rebased onto main 689f2db (the mimg_sampler conflict, resolved as you did, plus mips: 1 on the two new MSAA test textures from #1679) and pushed 7f804be:

  • Level. It now follows the S# as the measurement you cite describes: LOD plus LOD_BIAS, clamped to MIN_LOD..MAX_LOD, then floor(l + 0.5), so ties go up. With MIP_FILTER none it reads level 0 of the view. That is a stated shortcut: I don't know how the T# MIN_LOD relates to BASE_LEVEL here. A linear mip filter gathers like point.
  • Taps. Instead of clamping first and hoping the host sampler agrees, the four taps are now Loads at the chosen level. The S# addressing (wrap, mirror, clamp, mirror once, the border modes with the S# border colour type) is applied per axis in the shader, so neither the host sampler's MinLOD/MaxLOD, bias and filter nor the view's ResourceMinLODClamp can move a tap to another level.
  • Tests.
    • Your three cases are in as gpu_ps_image_gather4_l_s_sharp_level: a pixel shader through sampler_d3d12, on the HLSL path, since the draw helper is HLSL only.
    • gpu_gather4_explicit_lod now runs 14 S# cases on both backends: LOD ranges, bias ±1, point / linear / none mip filters, wrap, mirror, mirror once, mixed axes, three border variants, the ties 0.5 and 2.5, and negative offsets.
    • Revert checks: with the emitter changes reverted, both tests fail. With only lower_image.rs reverted, gather4_l direct dxil [clamp, point mip] … lod 0.5 fails.
  • cargo test --release -p xcl --lib: 928 passed, 0 failed, with and without XCL_HEAP_WINDOWS=1 (RTX 3050, 610.88). The smoke pair on the new head is in the description.

@futurepro62 thanks for the full-suite run on the 5070 Ti. It was on 5f3e9ec, the head before this change, so 7f804be hasn't run on Blackwell yet.

Not covered by a test: a static (constant) S#. The words come from the code there, through the same expressions.

This comment was AI-assisted.

@upipibi

upipibi commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

Re-tested 7f804be (on main 689f2db), RTX 3070, driver 616.56: all three of my cases now pass on both backends. That's LOD 1 full range, LOD 2 with S# MAX_LOD 1.0, and LOD 1 with MIP_FILTER none: 0 of 256 pixels wrong each, with and without XCL_DIRECT_DXIL=1. gpu_gather4_explicit_lod passes on both too. Thanks for the quick change.

On the remaining shortcut: ("how the T# MIN_LOD / BASE_LEVEL relate is unknown"): the same gfx1036 measurement (boykopovar/AnyPS5#23856) covers it. MIP_FILTER none reads the T# MIN_LOD rounded down (1.75 → 1, 2.5 → 2), at every LOD. Otherwise the T# MIN_LOD raises the level after the S# MAX_LOD clamp (bias, S# clamp, then T# MIN_LOD; the other order was 128 lanes off there). With a T# MIN_LOD of 0, as in these tests, both reduce to what the PR does now.

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

@futurepro62

Copy link
Copy Markdown

Follow-up from reading 7f804be, not from a GPU run: the new path is gated on glod = gather && lod.is_some() && !has_c (lower_image.rs, and the same in emit.rs), so a few sibling gather forms still go through Gather{ch}() / GatherCmp{ch}() and read mip 0 of the view:

  • image_gather4_c_l, image_gather4_c_l_o: an explicit LOD, but has_c excludes them. They take the plain GatherCmp path.
  • image_gather4_b, image_gather4_b_cl, image_gather4_c_b (and their _o forms): the bias is decoded (i_bias), but lod is None for these, so glod is false and the bias is dropped.
  • Plain image_gather4 / image_gather4_c in a pixel shader: whole_quad() already treats these as implicit-LOD instructions (they need whole quads for derivatives), but the lowering still gathers at level 0. On RDNA2 only the _lz forms are fixed at level 0, as far as I know.

I can't say how many programs in the retail pack use these, and I haven't measured any of them on a GPU, so I'm not suggesting they block this PR. They might be worth a line in the description, or a return err(d, …) the way the non-2D case is refused now, so they show up in coverage counts instead of quietly reading the wrong mip.

@DavidM1911

Copy link
Copy Markdown
Contributor

Verification on RTX 3060 Ti (Windows 11): merge conflict with current main — needs a rebase before it can be tested.

@raylitebull

Copy link
Copy Markdown
Member

To move this forward before merge:

  1. Rebase onto main to resolve the current merge conflict.
  2. Address @futurepro62's follow-up note on _c_l / _b forms.
  3. Confirm Build and test (Windows, no GPU) passes and get a fresh AMD retest on the rebased head.

@brothernog

Copy link
Copy Markdown
Author

@upipibi thanks for the retest, and for the T# rule. It's in as 51361a6, on both backends:

  • MIP_FILTER none reads floor(T# MIN_LOD), at every LOD.
  • Otherwise the level is LOD plus the S# bias, clamped to the S# MIN_LOD..MAX_LOD, then raised to the T# MIN_LOD, then floor(l + 0.5). The T# MIN_LOD is word1[19:8], u4.8, as gpu/desc.rs already reads it.
  • gpu_gather4_explicit_lod gained four cases with a T# MIN_LOD (18 now): 1.75 and 2.5 with MIP_FILTER none (levels 1 and 2), 1.75 with a point mip filter, and 1.75 over an S# MAX_LOD of 1 (level 2; the other order would give 1). With either backend's change reverted, that backend fails on the first new case and reads mip 0.

Two things your summary doesn't settle, so I wrote them down as assumptions:

  • With a point mip filter, the T# MIN_LOD is applied before the + 0.5 rounding, so 1.75 gives level 2. If the measurement saw 1 there, it's a one-line change.
  • The T# MIN_LOD is taken relative to the view's base level, like the LOD. The tests only use BASE_LEVEL 0, so a shortcut: comment stays for BASE_LEVEL > 0.

@DavidM1911 thanks, the conflict was real: #1969 moved the direct DXIL sample path into a guarded closure. The branch is rebased onto main de36462. The explicit-LOD gather now sits inside that closure, so it inherits the heap index guard. The branch is two commits now: the first is the old two folded together. cargo test --release -p xcl --lib: 957 passed, 0 failed, with the default settings, with XCL_HEAP_WINDOWS=1 and with XCL_DIRECT_DXIL=1 (RTX 3050 8 GB desktop, 610.88). A new smoke pair is in the description.

@futurepro62 agreed on all three. They are outside this PR, and I've listed them in the description as known gaps:

  • gather4_c_l / _c_l_o keep the mip-0 GatherCmp. Doing them with Loads means doing the depth compare in the shader.
  • The bias forms drop the bias.
  • Plain gather4 / gather4_c in a pixel shader read level 0 instead of the implicit LOD.

I'd rather not refuse them here. They translate today and read mip 0, and a refusal would turn every program that uses them into a translation failure. That trade belongs in its own PR, with pack counts. #1951 covers the compare forms in the direct DXIL backend.

This comment was AI-assisted.

@raylitebull

Copy link
Copy Markdown
Member

CI Build and test (Windows, no GPU) failed on 51361a6 at crates/xcl/src/gpu/recomp/tests.rs:2548 with error[E0063]: missing field 'mips' in initializer of 'TestTexture' (a newly merged test on main uses TestTexture without mips: 1).

Next step before merge: please rebase onto main, add mips: 1 at tests.rs:2548 (and any other new TestTexture initializers), and run CI + one independent AMD GPU pass on the rebased head.

@JJAutoCompare

JJAutoCompare commented Nov 25, 2026 •

Copy link
Copy Markdown

Tested

Clean PR head 51361a6a02ebe251b6c91bee7310ae05f6510fcd on Intel Arc A770 (driver 32.0.101.9175), AMD Ryzen 7 3800X, Windows 11 Enterprise Evaluation 26200.9974, Rust 1.100.0 x86_64-pc-windows-msvc, and DXC/DXIL 1.9.2609.5. I verified the live PR head before and after the run.

I prebuilt with cargo test --locked -p xcl --lib -j 2 --no-run in the unoptimized test profile with CARGO_INCREMENTAL=0, CARGO_PROFILE_DEV_DEBUG=0 and CARGO_PROFILE_TEST_DEBUG=0. With XCL_ADAPTER=A770, XCL_REQUIRE_GPU=wave64, XCL_DXC_DIR pointing to the verified 1.9.2609.5 x64 DLLs, and XCL_DIRECT_DXIL unset, I ran the resulting libtest executable under the shared Arc lock.

& $testExe gpu::recomp::tests::gpu_gather4_explicit_lod --exact --nocapture --test-threads=1
& $testExe gpu::recomp::tests::gpu_ps_image_gather4_l_s_sharp_level --exact --nocapture --test-threads=1

2 passed, 0 failed, 0 ignored, 0 skipped. Each invocation filtered out 981 tests.

  • gpu_gather4_explicit_lod explicitly dispatched HLSL compiled with DXC and direct DXIL within the same test. All 18 descriptor cases passed on each backend, with 64 lanes and three four-tap footprints per lane (2D, 2D with offsets, and 2D array). That is 3,456 footprint comparisons per backend. Cases cover LOD bias and clamps, point/linear/no mip filter, 0.5 and 2.5 ties, out-of-range LODs, address and border modes, negative offsets, and the four texture minimum-LOD cases, including a T# minimum above the S# maximum.
  • gpu_ps_image_gather4_l_s_sharp_level used HLSL/DXC through the draw helper and the S# host sampler. Zero mismatches across all 256 pixels in each of its three cases. LOD 1 with point mip selected level 1, LOD 2 with S# MAX_LOD=1 selected level 1, and LOD 1 with no mip filter selected level 0.

Not tested

Direct DXIL pixel shaders, static S#, nonzero texture base levels, comparison or implicit-LOD gathers, other GPUs, the current main merge result, the full workspace suite, and retail smoke runs.

AI use

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

Arc A770 rerun at 8eb5a5c

Tested

Both stock fixtures passed on the Intel Arc A770 at 8eb5a5c352084eda4fcce6b9332e2fa3aff193f2. PR #1812 was open at that exact head before and after the run, and the detached checkout stayed clean.

STOR-WINLAB ran Windows 11 Enterprise Evaluation build 26200.9974, Ryzen 7 3800X, Arc A770 driver 32.0.101.9175, Rust 1.100.0 (x86_64-pc-windows-msvc), and DXC/DXIL 1.9.2609.5. The unoptimized prebuild ran outside the Arc lock with two jobs, incremental compilation disabled, and dev/test debuginfo disabled. Both fixtures ran under the shared Arc lock with XCL_ADAPTER=A770, XCL_REQUIRE_GPU=wave64, and XCL_DXC_DIR pointed at the installed DXC 1.9.2609 directory.

cargo test --locked -p xcl --lib -j 2 --no-run
$exe = 'L:\XclTooling\pr2011-990b9d4-t3-76a0502d\target\debug\build\xcl\97fdf2b9ff8e1482\out\xcl-97fdf2b9ff8e1482.exe'
& $exe gpu::recomp::tests::gpu_gather4_explicit_lod --exact --nocapture --test-threads=1
& $exe gpu::recomp::tests::gpu_ps_image_gather4_l_s_sharp_level --exact --nocapture --test-threads=1

Each fixture reported 1 passed, 0 failed, 0 ignored, 0 skipped, with 1,125 filtered out. Cargo, both test processes, the Arc wrapper, and the outer process each exited 0. The binary SHA-256 matched before the wait and after lock acquisition: a4d17e2dda2f14f366b654769fdc67f87cf498c4efa38698fec29f0fb82bf870.

  • gpu_gather4_explicit_lod verified that 0.499 selects mip 1 in the full-range point-mip case on both HLSL and direct DXIL. Across 18 descriptor cases, it compared 3,456 footprints per backend for 2D gather, offset 2D gather, and 2D array gather.
  • gpu_ps_image_gather4_l_s_sharp_level checked all 256 pixels in each of three HLSL cases covering full-range point mip filtering, sampler MAX_LOD, and disabled mip filtering.

Combined: 2 passed, 0 failed, 0 ignored, 0 skipped. Compute ran in 1.37 s and pixel in 0.56 s. The Arc mutex was released.

Not tested

Full workspace suite, retail smoke, AMD hardware, direct DXIL pixel draws, or 0.499 in the pixel fixture.

AI use

T3 Code / Codex (gpt-6.2-sol) ran the verification, inspected the stock assertions, and wrote this report.

@upipibi

upipibi commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

Re-checked 51361a6 on the RTX 3070 (driver 616.56, Windows 11, debug) with the pixel test from before, on both backends. LOD 1.0, LOD 2.0 under S# MAX_LOD 1.0, MIP_FILTER none, LOD 0.5 and LOD 2.0 with an S# LOD_BIAS of −0.75 all read the expected level.

One case doesn't: LOD 0.499 (as a literal) reads level 0 on both backends, where RDNA2 reads level 1. boykopovar/AnyPS5#23666 measured the level change at LOD·256 = 127.5: 127.495 reads level 0, and 127.5 already reads level 1. That's because the LOD is first rounded to 1/256 with halves up, and then the nearest level is taken with ties up. floor(l + 0.5) on the unrounded LOD misses the window [0.498046875, 0.5) below every n + 0.5. floor(l + 0.501953125) gives both steps in one (0.501953125 is exact in f32).

On your two assumptions:

  • Point mip filter: yes. #23856 measured the T# MIN_LOD applied after the S# MAX_LOD, and before the rounding to a level. So 1.75 with a point filter gives level 2. MIP_FILTER none reads the T# MIN_LOD rounded down (1.75 → 1, 2.5 → 2), as you have it.
  • BASE_LEVEL: #23666 ran T# MIN_LOD 1.5 with BASE_LEVEL 1 (and BASE_LEVEL 1 with LAST_LEVEL 3) against its rule, all matching. Its summary doesn't spell out the relation to the base, though, so the fixture there is the place to confirm it; I haven't measured that myself.

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

@raylitebull

Copy link
Copy Markdown
Member

Reviewed b0f160f: switching to TestTexture::mips(&self) in crates/xcl/src/gpu/recomp/tests/runner.rs cleanly fixes the rebase without changing existing struct literals, and both HLSL (emit.rs) and direct DXIL (lower_image.rs) paths plus pixel-shader tests are covered (action_required CI run approved).

Two items remain before merge:

  1. @upipibi's review note on the u4.8 bias (+ 0.5 vs + 0.501953125 so LOD 0.499 samples mip 1 rather than mip 0) is still + 0.5 in b0f160f.
  2. An independent AMD GPU verification pass on the final head.

@brothernog

Copy link
Copy Markdown
Author

@raylitebull Thanks for catching the rounding edge case. It’s fixed in 8eb5a5c in both backends, and gpu_gather4_explicit_lod now checks that LOD 0.499 selects mip 1. An independent AMD run on that head is still pending. AI-assisted draft.

@upipibi

upipibi commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

Re-checked 8eb5a5c on the RTX 3070 (driver 616.56, Windows 11, debug) with the same pixel-shader gather test as before, on both backends. All six cases read the expected level with XCL_DIRECT_DXIL=0 and 1: LOD 1.0; LOD 2.0 under S# MAX_LOD 1.0; MIP_FILTER none; LOD 0.5; LOD 2.0 with LOD_BIAS −0.75; and LOD 0.499, which now reads level 1 (floor(lod + 0.501953125)), as on the hardware. The gather tests in the tree pass too (4 of 4, XCL_DIRECT_DXIL=1).

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

@brothernog

Copy link
Copy Markdown
Author

@upipibi Thanks for retesting both backends on 8eb5a5c and confirming the 0.499 case on the RTX 3070. An independent AMD run on this head is still pending. AI-assisted reply.

IvyNotFound added a commit to IvyNotFound/xcl that referenced this pull request Nov 25, 2026
@brothernog

Copy link
Copy Markdown
Author

Thanks @JJAutoCompare for rerunning both fixtures on the Arc A770! Could someone with an AMD GPU independently verify that explicit LOD 0.499 selects mip 1 on both the HLSL and direct DXIL paths at 8eb5a5c? Please include the GPU and driver version. AI-assisted.

@futurepro62

Copy link
Copy Markdown

AMD run of head 8eb5a5c (on cbc296a), for the AMD verification asked for above. Follows up on my earlier report.

Result: the new test gpu::recomp::tests::gpu_gather4_explicit_lod crashes the test process on an RDNA2 iGPU.

Tested

  • AMD Radeon integrated GPU (iGPU) of the Ryzen 7 9800X3D (RDNA2, 32-lane waves), XCL_ADAPTER=radeon, Windows 11 Home 26300, Rust 1.100.0 MSVC, DXC + dxil.dll 1.9.2602.17, debug.
  • cargo test -p xcl --lib -- --test-threads=1 --nocapture --skip gpu::dxil::fuzz_tests::fuzz_validate_and_gpu_1: 408 tests passed, then the process died while running gpu::recomp::tests::gpu_gather4_explicit_lod, with exit code 0xc0000005 (STATUS_ACCESS_VIOLATION). No panic message or test output was printed before the crash.
  • The full cargo test --workspace -- --test-threads=4 run on the Radeon crashed the same way at this head, in two separate runs, so no pass/fail counts were produced. Because the process dies there, the PR's other new test, gpu_ps_image_gather4_l_s_sharp_level, did not get to run on AMD.
  • Main 06c9f55 (which doesn't have this test) runs the whole suite on the same iGPU without a crash: 2 known failures, fuzz_validate_and_gpu_1 and fine_timer_makes_short_sleeps_short.
  • On NVIDIA (RTX 5070 Ti, driver 621.40, XCL_REQUIRE_GPU=mesh, same main-failing --skips as the earlier report), the same head is clean: 1144 passed, 0 failed, 44 ignored, and gpu_gather4_explicit_lod passes.

Not tested

  • Wave64 / discrete AMD (RDNA3/4), a release build, a game (test run only). I did not try to debug the crash further.

AI use

  • Claude Code (Claude Opus 5.5) ran the tests and wrote this report. I checked it before posting.

image_gather4_l / _l_o took mip 0 in the HLSL backend. The level now follows the S# as in the
direct backend (xcl-dev#1951): LOD plus LOD_BIAS, clamped to MIN_LOD..MAX_LOD, raised to the T# MIN_LOD,
then floor(l + 257/512) (the LOD rounded to 1/256, ties up), clamped to the view's mips;
MIP_FILTER none reads floor(T# MIN_LOD). The four taps are Loads at that level with the S#
addressing applied per axis, so the host sampler and the view's clamp cannot move them. The
half-border modes (4, 5) give no border colour, as in desc::sampler_d3d12 and the direct
backend. 2D and 2D arrays; cube and 3D are refused.

The kernel test runner gets mip chains (TestTexture::mips from the data length).
gpu_gather4_explicit_lod_hlsl / _direct run the same cases through each backend (one test per
backend, so a driver crash names it); the direct one runs xcl-dev#1951's code on a GPU.
@brothernog

Copy link
Copy Markdown
Author

Rebased onto main 68a284a as one commit, a56538a. #1951 now covers the direct DXIL side, so this PR drops its lower_image.rs changes and keeps the HLSL path, the runner's mip chains and the GPU tests.

@futurepro62 thanks for the AMD run. I have no AMD GPU here, so I couldn't reproduce the crash. Two changes should make a rerun more telling:

I read the host side again (runner.rs: the upload of every slice's mip chain, the footprints and the SRV mip counts) and found nothing wrong there, so my guess is the driver, compiling or running one of the two programs. That is only a guess.

Running the direct test against #1951 turned up one disagreement: the half-border address modes 4 (CLAMP_HALF_BORDER) and 5 (MIRROR_ONCE_HALF_BORDER). #1951 and desc::sampler_d3d12 give no border colour for them (clamp / mirror once); this PR's HLSL path and test gave the border colour. Neither side was measured on RDNA2. I aligned the HLSL path and the test with main, so both backends now agree. If anyone has an RDNA2 measurement, the case to check is clamp half border, black.

Tested a56538a on Windows 11 26300, RTX 3050 8 GB (desktop), NVIDIA 610.88, Rust 1.100.0 MSVC, DXC 1.9.2602.17 (a 60 s smoke run of the title against main is in the updated description):

  • cargo test -p xcl --lib -- gather4 --test-threads=1 (debug): 8 passed, including both new tests and gpu_ps_image_gather4_l_s_sharp_level.
  • With emit.rs back to main: gpu_gather4_explicit_lod_hlsl fails ([clamp, point mip] lane 8 … lod 0.5), gpu_ps_image_gather4_l_s_sharp_level fails ((a) LOD 1), and gpu_gather4_explicit_lod_direct passes (it runs main's code).
  • cargo test --release -p xcl --lib -- --test-threads=4: 1222 passed, 1 failed, 26 ignored. The failure, lower_dxil::tests::a_bridged_mask_write_where_one_half_is_elsewhere_meets_no_barrier, fails the same way on main 68a284a alone.
  • cargo clippy --release -p xcl --all-targets: 818 warnings on 68a284a and on this branch, no new ones.

This comment was AI-assisted.

@thiagocalile

Copy link
Copy Markdown

Independent test and smoke report for head a56538a588f72f053be108b6efedfa044d248839 on an AMD Radeon RX 6600 (8 GB VRAM, RDNA2, native wave64).

Tested

  • Hardware: AMD Radeon RX 6600 (8146 MiB VRAM, RDNA2, native wave64 + mesh shaders, driver 32.0.21045.29003, XCL_ADAPTER=radeon), AMD Ryzen 7 5700X3D, 47.9 GB RAM, Windows 11 (10.0.26200.0), Rust 1.100.0 (stable-x86_64-pc-windows-msvc), DXC 1.8.2502.11.
  • XCL_ADAPTER=radeon XCL_DIRECT_DXIL=0 cargo test --no-fail-fast -p xcl --lib -- gather4 --test-threads=1: 0 passed, 0 failed, 0 ignored (0 skipped).
  • XCL_ADAPTER=radeon XCL_DIRECT_DXIL=1 cargo test --no-fail-fast -p xcl --lib -- gather4 --test-threads=1: 0 passed, 0 failed, 0 ignored (0 skipped).

Regression run (scripts/smoke.ps1, release builds, XCL_ADAPTER=radeon)

main (2898511):

xcl-smoke v1
commit: 2898511af1c038a2ef809719e9b6f89e5675c422
gpu: AMD Radeon RX 6600
title: 3c290c16
seconds: 60
exit: 0x0
frames shown: 1488 (PresentX 1489)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 11934 MB

PR #1812 (a56538a):

xcl-smoke v1
commit: a56538a588f72f053be108b6efedfa044d248839
gpu: AMD Radeon RX 6600
title: 3c290c16
seconds: 60
exit: 0x0
frames shown: 1516 (PresentX 1516)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 12386 MB

Not tested: targeted gather unit tests (filter gather4 matched 0), full workspace suite, NVIDIA/Intel GPUs, Linux/Wine, or world load past the 60 s smoke window.

AI use: Antigravity (Gemini 4 Neon) ran the builds, tests, and smoke runs and drafted this report; the account owner reviewed and approved posting.

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.

7 participants