Skip to content

Recompiler: image_sample_*_g16 reads 16-bit gradients, packed in halves - #436

Open
sennecools wants to merge 1 commit into
xcl-dev:mainfrom
sennecools:recomp-mimg-g16
Open

sennecools wants to merge 1 commit into
xcl-dev:mainfrom
sennecools:recomp-mimg-g16

Conversation

@sennecools

@sennecools sennecools commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

The _g16 sampling forms carry their derivatives as 16-bit halves, and the dx and dy groups each start a dword (2D: dsdx|dtdx, dsdy|dtdy; 3D: dsdx|dtdx, drdx|pad, dsdy|dtdy, drdy|pad). XCL read them as 32-bit floats. That made the gradients wrong and moved the coordinates, and every address component after them, onto the wrong VGPRs. No issue covers this.

The retail title I ran uses none of these opcodes: an xcl shaders census of its pack found 0 instructions of all 16 IMAGE_SAMPLE_*_G16 variants. This is a correctness fix for titles that do use them.

What is in it

  • crates/xcl/src/gpu/recomp/emit.rs: _g16 gradients are laid out as 16-bit halves, each group starting a dword
  • crates/xcl/src/gpu/recomp/tests.rs: two new tests
    • sample_d_g16_reads_packed_half_gradients checks the HLSL addresses for 2D and 3D
    • gpu_sample_d_g16_coordinates_follow_packed_gradients runs a 2D image_sample_d_g16 on the GPU, with decoy coordinates where 32-bit gradients would put them

Tested

  • AMD Radeon RX 9070 XT, driver 32.0.32029.2010; Ryzen 7 9800X3D; Windows 11 build 26300; Rust 1.98.1
  • cargo test --workspace -- --test-threads=4 (debug): 668 passed, 0 failed, 41 ignored. No SKIPPED tests, and both new tests pass.
  • With only the emit.rs change reverted, cargo test -p xcl --lib g16: 0 passed, 2 failed (both new tests).
  • cargo build --release -p xcl for the regression run

Regression run
The retail title, 120 s, offline, release build. The gpu: line comes from smoke.ps1, which picked the virtual display adapter; the real GPU is the one above.

main:

xcl-smoke v1
commit: db1ddb25f3ff0c1686eed8200d238ad35f895926
gpu: Meta Virtual Monitor
title: 2c508222
seconds: 120
exit: 0x0
frames shown: 2748 (PresentX 2748)
presents per 2 s (last 3): 53 52 52
device removals: 0
crashes: 0
peak commit: 15544 MB

this PR:

xcl-smoke v1
commit: 1b250b6b031954a05ebff9ca74b60e7188678e49
gpu: Meta Virtual Monitor
title: 2c508222
seconds: 120
exit: 0x0
frames shown: 2770 (PresentX 2770)
presents per 2 s (last 3): 53 53 54
device removals: 0
crashes: 0
peak commit: 15545 MB

Not tested

  • The retail title never runs this code (0 _g16 instructions in its pack), so the regression run only shows that nothing else broke.
  • Tested on AMD RDNA4 only, not NVIDIA, Intel or wave32-only GPUs.
  • I did not check which adapter XCL used for the game run.
  • The GPU test covers 2D only; 1D, 3D and cube are checked in the HLSL only. The _cl, _o and c variants have no test of their own.

AI use

  • Claude Code, Claude Opus 5.5 (wrote the change and the test; a person reviewed and opens the PR)

The _g16 sampling forms carry their derivatives as 16-bit halves: the
dx and dy groups each start a dword (2D: dsdx|dtdx, dsdy|dtdy; 3D:
dsdx|dtdx, drdx|pad, dsdy|dtdy, drdy|pad). XCL read them as 32-bit
floats, so the gradients were garbage and the coordinates and every
later address component were read from the wrong VGPRs.

Tests: sample_d_g16_reads_packed_half_gradients (2D and 3D address
layout in the HLSL) and gpu_sample_d_g16_coordinates_follow_packed_gradients
(2D sample_d_g16 on the GPU, decoy coordinates where 32-bit gradients
would put them).

AI-assisted (Claude Code)
@alpz1000

Copy link
Copy Markdown

Independent RTX 3060 check of 1b250b6b031954a05ebff9ca74b60e7188678e49.

Tested on a GeForce RTX 3060 12 GB (610.88), Intel i5-8400, Windows 11 Home 26200.9974, Rust 1.100.0 x86_64-pc-windows-msvc, MSVC 14.44.35230, and DXC 1.9.2602.17 (SDK 10.0.26100), debug.

cargo test -p xcl --lib g16 -- --nocapture:

Source Passed Failed Ignored GPU skips
PR head 2 0 0 0
Same head with only emit.rs restored from its parent (PR tests kept) 0 2 0 0

The baseline failed both the generated HLSL check and GPU readback. For lane 0, the GPU returned [3.0, 3.0, 15.0, 1.0] instead of [0.0, 0.0, 0.0, 1.0]. Both tests passed with the fix.

Not tested: full workspace, retail titles, or other GPU adapters in this run.

AI use: OpenAI Codex (GPT-6) ran the tests locally and drafted this report from the actual results.

@AC-Storm-YT

Copy link
Copy Markdown

Test run on AMD RDNA4 – commit 1b250b6 (this PR's head, as is), clean tree

Tested

  • AMD Radeon RX 9070 XT (RDNA4, wave64), driver 32.0.32029.2010 – the tests ran on it (backend 'real' – AMD Radeon RX 9070 XT); AMD Ryzen 7 9800X3D; Windows 11 Pro build 26200; Rust 1.100.0 stable-x86_64-pc-windows-msvc, debug; DXC 1.8.2502.11
  • cargo build --workspace: ok
  • cargo test --workspace -- --test-threads=4: 668 passed, 0 failed, 41 ignored. No SKIPPED tests.
  • The new tests pass: gpu_sample_d_g16_coordinates_follow_packed_gradients (GPU), sample_d_g16_reads_packed_half_gradients

Not tested

  • No retail title or smoke.ps1 run
  • Release build; this commit merged with current main

Run and report: Claude Code (Claude Opus 5.5).

@TheHoracio

Copy link
Copy Markdown
Contributor

Test report on AMD RDNA3: RX 7800 XT (16 GB), driver 32.0.32015.7029, Ryzen 7 9800X3D, Windows 11 Pro 10.0.26300, Rust 1.100.0 MSVC, DXC 1.9.2602.17, debug build. No retail data was used.

Tested: the PR head 1b250b6 as it is (on its base db1ddb2, not merged onto current main).

  • cargo build --workspace, then cargo test --workspace --no-fail-fast -- --test-threads=4: 668 passed, 0 failed, 41 ignored, no SKIPPED tests.
  • Both new tests pass: gpu_sample_d_g16_coordinates_follow_packed_gradients (on the GPU, RDNA3) and sample_d_g16_reads_packed_half_gradients.

Not tested: a regression run on a title, a release build, and the head merged onto current main.

AI use: Claude Code ran the commands and drafted this comment; the runs are real runs on the hardware above.

@Luqueze

Luqueze commented Nov 24, 2026

Copy link
Copy Markdown

Independent RTX 4070 SUPER (Ada, wave32 only) check of 1b250b6b031954a05ebff9ca74b60e7188678e49, whole workspace.

Tested on a GeForce RTX 4070 SUPER 12 GB, driver 617.42 (32.0.16.1742), AMD Ryzen 7 5700X3D, 32 GB, Windows 11 Pro build 26300, Rust 1.100.0 stable-x86_64-pc-windows-msvc, VS 2022 Build Tools 17.14, DXC 1.9.2602.17 (SDK 10.0.26100.0), debug. No XCL_SAMPLE_* set.

cargo build --workspace + cargo test --workspace -- --test-threads=4 --nocapture:

Source Passed Failed Ignored SKIPPED
PR base db1ddb2 666 0 41 2 (no wave64)
PR head 1b250b6 668 0 41 2 (no wave64)
PR head merged onto main 2997c9a (no conflicts, not committed) 678 0 41 2 (no wave64)
  • The 2 extra tests are the PR's: sample_d_g16_reads_packed_half_gradients and gpu_sample_d_g16_coordinates_follow_packed_gradients; both pass, and the GPU one ran on the card (no SKIPPED line for it).
  • The SKIPPED tests are the same two in all three rows: gpu_geometry_wave32_prefix_count_over_a_mask, gpu_wave32_program_in_wave64_hardware.
  • cargo test -p xcl --lib g16 -- --nocapture on the PR head, 3 runs: 2 passed, 0 failed each time.
  • Same command with only emit.rs restored from its parent (PR tests kept): 0 passed, 2 failed. The GPU test returned [3.0, 3.0, 15.0, 1.0] for lane 0 instead of [0.0, 0.0, 0.0, 1.0], the same values as the RTX 3060 run above.
  • No Display 4101 (TDR) events during the runs.

Not tested: release build, retail titles / scripts/smoke.ps1, XCL_SAMPLE_* game-data tests, 1D / 3D / cube on the GPU (the PR's GPU test is 2D only), other adapters.

AI use: Claude Code (Claude Opus 5.5) ran the builds and tests on this machine and drafted this report from their output.

@QemmHD

QemmHD commented Nov 24, 2026

Copy link
Copy Markdown

Review: the layout matches LLVM's GFX10 codegen for image_sample_d_g16 with half gradients (llvm.amdgcn.image.sample.g16.ll): 1D v[0:2] = dsdh, dsdv, s; 2D packs dsdh|dtdh and dsdv|dtdv (v_perm_b32 … 0x5040100); 3D v[2:8] = dsdh|dtdh, drdh, dsdv|dtdv, drdv, s, t, r.

One gap: tests.rs:2362-2400 – the GPU test uses zero gradients on a one-mip texture with point filtering, so it can't observe the gradient values; the f16 decode and the 3D drdx|pad dword are only checked as HLSL text. A case with a 2-mip view (runner::ViewTexture via draw_console_views) and one non-zero gradient that should select mip 1 would check them on hardware; for 3D, a non-zero drdx alone also pins the padded dword.

AI-assisted review (Claude Code, Claude Opus 5.5).
Discord: bigpeppa.

@MiguelMachado-dev

Copy link
Copy Markdown
Contributor

The direct DXIL path has the same bug: dxil::lower_image still builds the address layout the way emit.rs did before this PR, one 32-bit dword per gradient (crates/xcl/src/gpu/dxil/lower_image.rs:387-388 on main 689f2db). With XCL_DIRECT_DXIL=1, a _g16 sample still reads its gradients as 32-bit floats and its coordinates from the wrong VGPRs. The same change (16-bit halves, with the dx and dy groups each starting a dword) applies there.

gpu_sample_d_g16_coordinates_follow_packed_gradients goes through runner::run_code_with, which takes the direct path when XCL_DIRECT_DXIL=1 is set, so I expect it to fail on that path until lower_image.rs gets the fix too. I have not run it.

Related: #1797 (rdna2::vgpr_uses counting the registers the translators read) has a G16 exception that counts the derivatives as 32-bit, to match the translators on main. Once this PR is merged, that exception is wrong, and I will take it out of #1797.

(Written with Claude Code, Claude Opus 5.5, from reading the code at main 689f2db and this PR's head 1b250b6. Nothing was run.)

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