Skip to content

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

Open
brothernog wants to merge 3 commits into
xcl-dev:mainfrom
brothernog:gather4-explicit-lod
Open

brothernog wants to merge 3 commits 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 both backends, whatever LOD the program passes. On the HLSL side, Gather{ch}() has no level argument, and the texel offset was scaled with mip 0's size (emit.rs, the has_lz || gather4 arm). The direct DXIL path had the same logic in lower_image.rs. 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: 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, as the same measurement found (1.75 → 1, 2.5 → 2). The T# MIN_LOD comes after the S# clamp; the other order was off there.
    • 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:
    • wrap, mirror, clamp and mirror once;
    • the border modes, where an out-of-range texel gives the S# border colour type (transparent black, opaque black, opaque white).
  • 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 now fail with the translator error gather4 explicit LOD (not 2D) instead of silently reading mip 0. No program in the retail pack hits it: xcl shaders --recomp still translates 26 698 of 26 698 with 0 errors.
  • Unchanged:
    • _lz gather.
    • Known gaps, still reading mip 0 as before (pointed out by @futurepro62):
      • The compare forms (gather4_c_l, _c_l_o) keep the mip-0 GatherCmp. SampleCmpLevel needs SM 6.7, and with Loads the depth compare would have to be done in the shader.
      • The bias forms (gather4_b, _b_cl, _c_b and their _o forms) drop the bias.
      • Plain gather4 / gather4_c in a pixel shader read level 0, not the implicit LOD.

Files

  • gpu/recomp/emit.rs (plus the gat_ax helper in the HLSL prelude) and gpu/dxil/lower_image.rs (Lw::gather_axis): the explicit-LOD gather path.
  • 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. An earlier field version broke twice on literals that other PRs added to main.
  • gpu/recomp/tests.rs:
    • gpu_gather4_explicit_lod runs through both the HLSL and the direct DXIL backend. It uses an 8×8 texture and a 2-layer array, 4 mips each, with distinct values per mip and per texel. It covers 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;
      • three border variants;
      • a T# MIN_LOD of 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.
    • The same test covers LODs including the ties 0.5 and 2.5, 0.499 (just below a tie, level 1 after the 1/256 rounding), one past the last mip and one below 0, plus negative 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. It runs on the HLSL backend only, because 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, release, DXC 1.9.2602.17.
  • On main cbc296a + this PR, cargo test --release -p xcl --lib -- --test-threads=4: 1100 passed, 0 failed, 26 ignored. Same with XCL_HEAP_WINDOWS=1. With XCL_DIRECT_DXIL=1, 1099 passed and 1 failed (gpu_image_get_resinfo_1d_array), which fails the same way on main cbc296a alone.
  • Revert checks:
    • With the emit.rs / lower_image.rs changes reverted (tests kept), both tests fail. First failure: gather4_l hlsl [clamp, point mip] lane 8 (uv 0, 1; lod 0.5; offset (-1, 0)), got [70, 70, 70, 70], expected [1030, …]. Also (b) LOD 2, MAX_LOD 1: pixels off the level-1 footprint, 192 instead of 0.
    • With only lower_image.rs reverted: gather4_l direct dxil [clamp, point mip] … lod 0.5.
  • cargo clippy --release -p xcl --all-targets: no new warnings (772 on cbc296a and on this branch).
  • @futurepro62 ran the full suite on an RTX 5070 Ti on the earlier head 5f3e9ec (888 passed); @upipibi checked that head on an RTX 3070.

Regression run
With XCL_NETWORK=0 XCL_PAGE_BUDGET_MB=4096, back to back:

main cbc296a:

xcl-smoke v1
commit: cbc296ab65a2661a61e3e2da109134bcd5319f60
gpu: NVIDIA GeForce RTX 3050
title: retail-title
seconds: 60
exit: 0x0
frames shown: 1429 (PresentX 1430)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 16741 MB

This PR (8eb5a5c): 110.4 % of main's frames, 89.7 % of its peak commit.

xcl-smoke v1
commit: 8eb5a5c352084eda4fcce6b9332e2fa3aff193f2
gpu: NVIDIA GeForce RTX 3050
title: retail-title
seconds: 60
exit: 0x0
frames shown: 1578 (PresentX 1578)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 15024 MB

Overlap

Not tested

  • AMD and Intel; wave64-native hardware.
  • A static (constant) S#: the words come from the code through the same expressions, but no test builds one.
  • A T# MIN_LOD with BASE_LEVEL > 0, and whether a fractional T# MIN_LOD with a point mip filter rounds half up (as here) or down.
  • Integer-format textures: the view stays float4 as before.
  • Any visible change in a title.

AI use

  • Claude Code (Claude Opus 5.5 orchestrating, Claude Sonnet 5.5 writing the change and test): implementation, tests and measurements, and this description. I checked the diff and reran the revert checks myself. The T# MIN_LOD follow-up was written by Claude Opus 5.5 directly.

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.

image_gather4_l / _l_o took mip 0 in both backends. The level now follows the S#: LOD plus LOD_BIAS, clamped to MIN_LOD..MAX_LOD, rounded half up, clamped to the view's mips; MIP_FILTER none reads level 0. 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. 2D and 2D arrays; cube and 3D are refused.
Per the gfx1036 measurement: with MIP_FILTER none the level is floor(T# MIN_LOD); otherwise the T# MIN_LOD (word1[19:8], u4.8) raises the LOD after the S# clamp, before the half-up rounding. Both backends; four T# MIN_LOD cases in gpu_gather4_explicit_lod.
RDNA2 rounds the LOD to u4.8 with halves up, then takes the nearest level with ties up (boykopovar/AnyPS5#23666): floor(l + 257/512) does both, so LOD 0.499 reads level 1. Both backends; gpu_gather4_explicit_lod tests 0.499 instead of 0.6.
@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
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