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

Open
sennecools wants to merge 1 commit from upstream-pr/436 into main
sennecools commented 2026-11-24 00:16:12 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by sennecools on 2026-11-24T00:16:12Z.


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)
<!-- greyfinch-sync:ivall/xcl:issue:5463598273 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/436). Originally posted by [sennecools](https://github.com/sennecools) on 2026-11-24T00:16:12Z. --- 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)
alpz1000 commented 2026-11-24 07:04:57 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by alpz1000 on 2026-11-24T00:25:07Z.


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.

<!-- greyfinch-sync:ivall/xcl:comment:7118047185 --> Imported from [ivall/xcl](https://github.com/ivall/xcl/pull/436#issuecomment-7118047185). Originally posted by [alpz1000](https://github.com/alpz1000) on 2026-11-24T00:25:07Z. --- 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 commented 2026-11-24 07:05:00 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by AC-Storm-YT on 2026-11-24T00:35:31Z.


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).

<!-- greyfinch-sync:ivall/xcl:comment:7118228078 --> Imported from [ivall/xcl](https://github.com/ivall/xcl/pull/436#issuecomment-7118228078). Originally posted by [AC-Storm-YT](https://github.com/AC-Storm-YT) on 2026-11-24T00:35:31Z. --- 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 commented 2026-11-24 07:05:01 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by TheHoracio on 2026-11-24T00:36:44Z.


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.

<!-- greyfinch-sync:ivall/xcl:comment:7118249724 --> Imported from [ivall/xcl](https://github.com/ivall/xcl/pull/436#issuecomment-7118249724). Originally posted by [TheHoracio](https://github.com/TheHoracio) on 2026-11-24T00:36:44Z. --- 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 commented 2026-11-24 07:05:03 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by Luqueze on 2026-11-24T00:45:34Z.


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.

<!-- greyfinch-sync:ivall/xcl:comment:7118401905 --> Imported from [ivall/xcl](https://github.com/ivall/xcl/pull/436#issuecomment-7118401905). Originally posted by [Luqueze](https://github.com/Luqueze) on 2026-11-24T00:45:34Z. --- 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 commented 2026-11-24 07:05:13 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by QemmHD on 2026-11-24T01:18:17Z.


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.

<!-- greyfinch-sync:ivall/xcl:comment:7118967243 --> Imported from [ivall/xcl](https://github.com/ivall/xcl/pull/436#issuecomment-7118967243). Originally posted by [QemmHD](https://github.com/QemmHD) on 2026-11-24T01:18:17Z. --- Review: the layout matches LLVM's GFX10 codegen for `image_sample_d_g16` with half gradients ([llvm.amdgcn.image.sample.g16.ll](https://github.com/llvm/llvm-project/blob/main/llvm/test/CodeGen/AMDGPU/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 commented 2026-11-24 23:08:26 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by MiguelMachado-dev on 2026-11-24T22:38:14Z.


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.)

<!-- greyfinch-sync:ivall/xcl:comment:7146467020 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/436#issuecomment-7146467020). Originally posted by [MiguelMachado-dev](https://github.com/MiguelMachado-dev) on 2026-11-24T22:38:14Z. --- 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.)
DavidM1911 commented 2026-11-25 15:07:45 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by DavidM1911 on 2026-11-25T14:15:43Z.


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

  • cargo test -p xcl --lib: all passed, 0 failed, ~26 ignored. (Intermittent single-test flake / 0xc0000005 abort under parallel scheduling — tracked in #1978; clean on single-threaded rerun.)

Thanks!

<!-- greyfinch-sync:ivall/xcl:comment:7165004768 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/436#issuecomment-7165004768). Originally posted by [DavidM1911](https://github.com/DavidM1911) on 2026-11-25T14:15:43Z. --- Verification on RTX 3060 Ti (Windows 11), branch merged with current `main`: - `cargo test -p xcl --lib`: all passed, 0 failed, ~26 ignored. (Intermittent single-test flake / 0xc0000005 abort under parallel scheduling — tracked in #1978; clean on single-threaded rerun.) Thanks!
gut0to commented 2026-11-26 16:55:28 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by gut0to on 2026-11-26T16:55:28Z.

Code review location: crates/xcl/src/gpu/recomp/tests.rs; line 2375

@@ -2331,6 +2331,74 @@ fn gpu_images_sample_load_store_atomic() {
     assert_eq!(u32::from_le_bytes(r.textures[2][..4].try_into().unwrap()), 64, "image_atomic_inc (64 lanes, limit 100)");
 }
 
+/// image_sample_*_g16: the gradients are 16-bit halves, each of the dx and dy groups starting a
+/// dword (3D: dsdx|dtdx, drdx|pad, dsdy|dtdy, drdy|pad), so the coordinates follow sooner.
+#[test]
+fn sample_d_g16_reads_packed_half_gradients() {
+    let code = prog(&[
+        ("IMAGE_SAMPLE_D_G16", "ENC_MIMG", &[("VDATA", 20), ("VADDR", 0), ("SRSRC", 4), ("SSAMP", 6), ("DMASK", 0xF), ("DIM", 1)], None),
+        ("IMAGE_SAMPLE_D_G16", "ENC_MIMG", &[("VDATA", 24), ("VADDR", 10), ("SRSRC", 4), ("SSAMP", 6), ("DMASK", 0xF), ("DIM", 2)], None),
+    ]);
+    let t = translate(&code, &Options::default()).unwrap();
+    assert!(t.complete(), "{:?}\n{}", t.unsupported_why, t.hlsl);
+    let h = |v: u32, hi: u32| format!("f16tof32((v{v} >> {}u) & 0xFFFFu)", 16 * hi);
+    let d2 = format!("float2(asfloat(v2), asfloat(v3)), float2({}, {}), float2({}, {}))", h(0, 0), h(0, 1), h(1, 0), h(1, 1));
+    assert!(t.hlsl.contains(&d2), "2D: {d2}\n{}", t.hlsl);
+    let d3 = format!(
+        "float3(asfloat(v14), asfloat(v15), asfloat(v16)), float3({}, {}, {}), float3({}, {}, {}))",
+        h(10, 0),
+        h(10, 1),
+        h(11, 0),
+        h(12, 0),
+        h(12, 1),
+        h(13, 0)
+    );
+    assert!(t.hlsl.contains(&d3), "3D: {d3}\n{}", t.hlsl);
+}
+
+/// A 2D image_sample_d_g16 on the GPU: zero f16 gradients in v0 / v1, the coordinates in
+/// v2 / v3, decoy coordinates in v4 / v5 (where 32-bit gradients would put the coordinates).
+#[test]
+fn gpu_sample_d_g16_coordinates_follow_packed_gradients() {
+    if !gpu_ready() {
+        return;
+    }
+    use runner::{TestSampler, TestTexture, TexDim};
+    use windows::Win32::Graphics::Direct3D12::*;
+    use windows::Win32::Graphics::Dxgi::Common::DXGI_FORMAT_R32G32B32A32_FLOAT;
+    let code = prog(&[("IMAGE_SAMPLE_D_G16", "ENC_MIMG", &[("VDATA", 10), ("VADDR", 0), ("SRSRC", 4), ("SSAMP", 6), ("DMASK", 0xF), ("DIM", 1)], None)]);
+    let mut st = KernelState::default();
+    st.sgpr[16..24].copy_from_slice(&tsharp(0, 77, 9));
+    st.sgpr[24..28].copy_from_slice(&[0, 0, 0, 0]);
+    for (l, r) in st.vgpr.iter_mut().enumerate() {
+        let (x, y) = ((l % 4) as f32, (l / 4 % 4) as f32);
+        r[0] = 0; // dsdx | dtdx (f16 zeros)

AI-assisted review (OpenAI GPT-6); I inspected the changed code and tests, but did not run tests or a game.

Could the GPU regression also use distinct nonzero packed-half dsdx/dtdx and dsdy/dtdy values against a texture with distinguishable mip levels (or another gradient-sensitive fixture)? The current GPU case sets both gradient dwords to zero and uses a single-mip texture, so it verifies the coordinate slots and a mip-0 sample but cannot catch incorrect runtime gradient values or ordering. The HLSL text assertion checks codegen shape; a nonzero GPU case would exercise the actual SampleGrad inputs.

<!-- greyfinch-sync:ivall/xcl:review-comment:4711379147 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/436#discussion_r4711379147). Originally posted by [gut0to](https://github.com/gut0to) on 2026-11-26T16:55:28Z. **Code review location:** `crates/xcl/src/gpu/recomp/tests.rs`; line 2375 ~~~~diff @@ -2331,6 +2331,74 @@ fn gpu_images_sample_load_store_atomic() { assert_eq!(u32::from_le_bytes(r.textures[2][..4].try_into().unwrap()), 64, "image_atomic_inc (64 lanes, limit 100)"); } +/// image_sample_*_g16: the gradients are 16-bit halves, each of the dx and dy groups starting a +/// dword (3D: dsdx|dtdx, drdx|pad, dsdy|dtdy, drdy|pad), so the coordinates follow sooner. +#[test] +fn sample_d_g16_reads_packed_half_gradients() { + let code = prog(&[ + ("IMAGE_SAMPLE_D_G16", "ENC_MIMG", &[("VDATA", 20), ("VADDR", 0), ("SRSRC", 4), ("SSAMP", 6), ("DMASK", 0xF), ("DIM", 1)], None), + ("IMAGE_SAMPLE_D_G16", "ENC_MIMG", &[("VDATA", 24), ("VADDR", 10), ("SRSRC", 4), ("SSAMP", 6), ("DMASK", 0xF), ("DIM", 2)], None), + ]); + let t = translate(&code, &Options::default()).unwrap(); + assert!(t.complete(), "{:?}\n{}", t.unsupported_why, t.hlsl); + let h = |v: u32, hi: u32| format!("f16tof32((v{v} >> {}u) & 0xFFFFu)", 16 * hi); + let d2 = format!("float2(asfloat(v2), asfloat(v3)), float2({}, {}), float2({}, {}))", h(0, 0), h(0, 1), h(1, 0), h(1, 1)); + assert!(t.hlsl.contains(&d2), "2D: {d2}\n{}", t.hlsl); + let d3 = format!( + "float3(asfloat(v14), asfloat(v15), asfloat(v16)), float3({}, {}, {}), float3({}, {}, {}))", + h(10, 0), + h(10, 1), + h(11, 0), + h(12, 0), + h(12, 1), + h(13, 0) + ); + assert!(t.hlsl.contains(&d3), "3D: {d3}\n{}", t.hlsl); +} + +/// A 2D image_sample_d_g16 on the GPU: zero f16 gradients in v0 / v1, the coordinates in +/// v2 / v3, decoy coordinates in v4 / v5 (where 32-bit gradients would put the coordinates). +#[test] +fn gpu_sample_d_g16_coordinates_follow_packed_gradients() { + if !gpu_ready() { + return; + } + use runner::{TestSampler, TestTexture, TexDim}; + use windows::Win32::Graphics::Direct3D12::*; + use windows::Win32::Graphics::Dxgi::Common::DXGI_FORMAT_R32G32B32A32_FLOAT; + let code = prog(&[("IMAGE_SAMPLE_D_G16", "ENC_MIMG", &[("VDATA", 10), ("VADDR", 0), ("SRSRC", 4), ("SSAMP", 6), ("DMASK", 0xF), ("DIM", 1)], None)]); + let mut st = KernelState::default(); + st.sgpr[16..24].copy_from_slice(&tsharp(0, 77, 9)); + st.sgpr[24..28].copy_from_slice(&[0, 0, 0, 0]); + for (l, r) in st.vgpr.iter_mut().enumerate() { + let (x, y) = ((l % 4) as f32, (l / 4 % 4) as f32); + r[0] = 0; // dsdx | dtdx (f16 zeros) ~~~~ --- AI-assisted review (OpenAI GPT-6); I inspected the changed code and tests, but did not run tests or a game. Could the GPU regression also use distinct nonzero packed-half `dsdx/dtdx` and `dsdy/dtdy` values against a texture with distinguishable mip levels (or another gradient-sensitive fixture)? The current GPU case sets both gradient dwords to zero and uses a single-mip texture, so it verifies the coordinate slots and a mip-0 sample but cannot catch incorrect runtime gradient values or ordering. The HLSL text assertion checks codegen shape; a nonzero GPU case would exercise the actual `SampleGrad` inputs.
JJAutoCompare commented 2026-11-26 22:49:00 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by JJAutoCompare on 2026-11-26T22:49:00Z.


Tested

Independent run at clean PR head 1b250b6b031954a05ebff9ca74b60e7188678e49 on Intel Arc A770 (driver 32.0.101.9175), Ryzen 7 3800X, Windows 11 x64 26200.9974, Rust/Cargo 1.100.0 MSVC, DXC/DXIL 1.9.2609.5, debug test profile.

Prebuilt with cargo test --locked -p xcl --lib --no-run --message-format=json-render-diagnostics outside the GPU mutex. Ran <test-exe> gpu::recomp::tests::gpu_sample_d_g16_coordinates_follow_packed_gradients --exact --nocapture --test-threads=1 under the Arc mutex with XCL_ADAPTER=A770.

1 passed, 0 failed, 0 ignored and 0 GPU skips (0.93 s). Exact RGBA matched across all 64 logical lanes, confirming the coordinates came from VGPRs 2–3, after the two packed-gradient dwords.

Not tested

The fixture uses zero gradients and one mip, so nonzero half-gradient decoding and gradient-driven mip selection remain untested. Direct DXIL, the workspace suite and retail smoke were outside the run.

AI use

T3 Code / Codex (gpt-6.2-sol) ran the original verification and summarized the saved results.

<!-- greyfinch-sync:ivall/xcl:comment:7205009324 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/436#issuecomment-7205009324). Originally posted by [JJAutoCompare](https://github.com/JJAutoCompare) on 2026-11-26T22:49:00Z. --- **Tested** Independent run at clean PR head `1b250b6b031954a05ebff9ca74b60e7188678e49` on Intel Arc A770 (driver 32.0.101.9175), Ryzen 7 3800X, Windows 11 x64 26200.9974, Rust/Cargo 1.100.0 MSVC, DXC/DXIL 1.9.2609.5, debug test profile. Prebuilt with `cargo test --locked -p xcl --lib --no-run --message-format=json-render-diagnostics` outside the GPU mutex. Ran `<test-exe> gpu::recomp::tests::gpu_sample_d_g16_coordinates_follow_packed_gradients --exact --nocapture --test-threads=1` under the Arc mutex with `XCL_ADAPTER=A770`. 1 passed, 0 failed, 0 ignored and 0 GPU skips (0.93 s). Exact RGBA matched across all 64 logical lanes, confirming the coordinates came from VGPRs 2–3, after the two packed-gradient dwords. **Not tested** The fixture uses zero gradients and one mip, so nonzero half-gradient decoding and gradient-driven mip selection remain untested. Direct DXIL, the workspace suite and retail smoke were outside the run. **AI use** T3 Code / Codex (gpt-6.2-sol) ran the original verification and summarized the saved results.
Foniass commented 2026-12-01 03:08:23 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by Foniass on 2026-12-01T03:08:23Z.


Triage (#1607) – head 1b250b6

No open or merged PR changes the HLSL path for _g16: the gradient shape in emit.rs on main is still 2*grad_n 32-bit slots. #2838 is the direct-DXIL counterpart (lower_image.rs) and refers to this PR; the two complement each other. The layout (each of the dx and dy groups starts a dword, halves packed) matches what #2838 implements.
No problems found in the diff. The PR conflicts with main and needs a rebase.
Recommendation: keep open.

AI review: Claude Code (Claude Opus 5.5), diffs read only, not built or run; not checked by a human.

<!-- greyfinch-sync:ivall/xcl:comment:7299367590 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/436#issuecomment-7299367590). Originally posted by [Foniass](https://github.com/Foniass) on 2026-12-01T03:08:23Z. --- **Triage (#1607)** – head 1b250b6 No open or merged PR changes the HLSL path for `_g16`: the gradient shape in emit.rs on main is still 2*grad_n 32-bit slots. #2838 is the direct-DXIL counterpart (lower_image.rs) and refers to this PR; the two complement each other. The layout (each of the dx and dy groups starts a dword, halves packed) matches what #2838 implements. No problems found in the diff. The PR conflicts with main and needs a rebase. Recommendation: keep open. AI review: Claude Code (Claude Opus 5.5), diffs read only, not built or run; not checked by a human.
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/436:upstream-pr/436'
git switch 'upstream-pr/436'

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.

git switch 'main'
git merge --no-ff 'upstream-pr/436'
git switch 'upstream-pr/436'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/436'
git switch 'upstream-pr/436'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/436'
git switch 'main'
git merge --squash 'upstream-pr/436'
git switch 'main'
git merge --ff-only 'upstream-pr/436'
git switch 'main'
git merge 'upstream-pr/436'
git push origin 'main'
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
xcl-dev/xcl!431
No description provided.