Recompiler: keep A16 MIMG bias in its own VGPR before packed coordinates #3351

Closed
Kalmarv wants to merge 1 commit from upstream-pr/3351 into main
Kalmarv commented 2026-11-29 10:01:42 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by Kalmarv on 2026-11-29T10:01:42Z.


What changed

  • crates/xcl/src/gpu/dxil/lower_image.rs (Lw::mimg): set starts = true on the has_b bias entry and on the first body coordinate (k == 0) when building the MIMG VADDR shape table, so under A16=1 the f16 bias sits in the low 16 bits of its own full 32-bit VGPR and body coordinates always start at the low 16 bits of the next VGPR.
  • crates/xcl/src/gpu/recomp/emit.rs (Em::mimg): applied the identical shape fix and updated the A16 address-layout doc comment to match.
  • crates/xcl/src/gpu/dxil/lower_image.rs (heap_bounds_tests::a16_biased_sample_keeps_bias_in_its_own_vgpr): added CPU/CI translation, DXIL validation, and HLSL layout assertions across 12 IMAGE_SAMPLE_B* (_b, _b_cl, _b_o, _b_cl_o, _c_b, _c_b_o, 1D/2D/3D/Cube/2DArray, and NSA=1) forms with A16=1, plus GPU execution tests in both Direct DXIL and HLSL with non-zero poison in the upper 16 bits of every bias VGPR.
  • crates/xcl/src/gpu/recomp/tests.rs (gpu_ps_image_sample_b_a16_multi_mip): added a multi-mip 5-level pixel-shader GPU draw test exercising IMAGE_SAMPLE_B with A16=1 (bias = +1.0 in v4[15:0], poison in v4[31:16], packed (u, v) in v5) in both HLSL and Direct DXIL to verify both mip-level selection and coordinate decoding.

Why

On RDNA2 (A16=1), a biased image sample's f16 LOD bias occupies a full 32-bit VGPR in its low 16 bits ([15:0], with [31:16] unused padding), and the packed 16-bit texture coordinates start at [15:0] of the following VGPR — matching LLVM AMDGPU (packImage16bitOpsToDwords / getAddrSizeMIMGOp) and XCL's own disassembler (rdna2::mimg_address_dwords).

Previously, both lower_image.rs and emit.rs pushed (16, false) for bias and (16, false) for body[0] whenever grad_n == 0. For image_sample_b, image_sample_b_cl, image_sample_b_o, and image_sample_b_cl_o with A16=1 (no z-compare dword between bias and coordinates), body[0] (s) was packed into the unused upper 16 bits of the bias VGPR (v_bias[31:16]), shifting every subsequent coordinate (t, r/slice/face) and trailing cl by 16 bits and dropping the final half-register.

Tested

Hardware: NVIDIA GeForce RTX 5070 Ti (driver 32.0.16.1074), AMD Ryzen 9 9950X, Windows 11 Pro (Build 26200), rustc 1.99.0, DXC 1.9.2609.5 (dxil 1.9.2609.5).

  • Rebased cleanly onto origin/main (3b6a91ed); cargo check --workspace --all-targets: 0 errors, 0 new warnings.
  • cargo build --workspace and cargo build --release -p xcl: succeeded.
  • Targeted CPU, DXIL validation, and RTX 5070 Ti GPU tests (cargo test -p xcl --lib a16_ -- --nocapture): passed (0 failed).
  • 150 s real-title smoke on RTX 5070 Ti (XCL_DIRECT_DXIL=all, XCL_HEAP_WINDOWS=1, XCL_DRED=1, XCL_DIRECT_SPLIT_INSTRUCTIONS=0):

PR (b68b6e2b):

xcl-smoke v1
commit: b68b6e2b1ea3f18570f96a43a2e722a2bffc943c
gpu: NVIDIA GeForce RTX 5070 Ti
title: ea3b5e4d
seconds: 150
exit: 0x0
frames shown: 1550 (PresentX 1550)
presents per 2 s (last 3): 0 0 0
device removals: 0
crashes: 0
peak commit: 33309 MB

Baseline (main ancestor f0e8fdd5, same machine and settings):

xcl-smoke v1
commit: f0e8fdd5c3942ddd399d5ee11384c8595c5aacff
gpu: NVIDIA GeForce RTX 5070 Ti
title: ea3b5e4d
seconds: 150
exit: 0x0
frames shown: 1695 (PresentX 1695)
presents per 2 s (last 3): 0 0 0
device removals: 0
crashes: 0
peak commit: 31546 MB

Not tested

  • AMD RDNA hardware (only NVIDIA RTX 5070 Ti is available locally).
  • _g16 16-bit gradient packing (already covered separately by open PR #436).
  • Full workspace suite on 3b6a91ed currently hits 4 pre-existing origin/main split-wave64 geometry test failures unrelated to this PR; all targeted a16_ GPU and validation tests pass on b68b6e2b.
  • Real-title runtime hits of image_sample_b* with A16=1 were not specifically instrumented; the 150 s smoke verifies zero regressions across the real title while the targeted GPU tests directly exercise the fixed compute and multi-mip pixel-shader paths.

AI use

Diagnosed, implemented, tested, and written with an AI coding assistant; all code, tests, DXIL validation, and real-hardware GPU/smoke runs were executed and verified directly.

<!-- greyfinch-sync:ivall/xcl:issue:5549644182 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3351). Originally posted by [Kalmarv](https://github.com/Kalmarv) on 2026-11-29T10:01:42Z. --- **What changed** - `crates/xcl/src/gpu/dxil/lower_image.rs` (`Lw::mimg`): set `starts = true` on the `has_b` bias entry and on the first body coordinate (`k == 0`) when building the MIMG `VADDR` `shape` table, so under `A16=1` the `f16` bias sits in the low 16 bits of its own full 32-bit VGPR and body coordinates always start at the low 16 bits of the next VGPR. - `crates/xcl/src/gpu/recomp/emit.rs` (`Em::mimg`): applied the identical `shape` fix and updated the `A16` address-layout doc comment to match. - `crates/xcl/src/gpu/dxil/lower_image.rs` (`heap_bounds_tests::a16_biased_sample_keeps_bias_in_its_own_vgpr`): added CPU/CI translation, DXIL validation, and HLSL layout assertions across 12 `IMAGE_SAMPLE_B*` (`_b`, `_b_cl`, `_b_o`, `_b_cl_o`, `_c_b`, `_c_b_o`, 1D/2D/3D/Cube/2DArray, and `NSA=1`) forms with `A16=1`, plus GPU execution tests in both Direct DXIL and HLSL with non-zero poison in the upper 16 bits of every bias VGPR. - `crates/xcl/src/gpu/recomp/tests.rs` (`gpu_ps_image_sample_b_a16_multi_mip`): added a multi-mip 5-level pixel-shader GPU draw test exercising `IMAGE_SAMPLE_B` with `A16=1` (`bias = +1.0` in `v4[15:0]`, poison in `v4[31:16]`, packed `(u, v)` in `v5`) in both HLSL and Direct DXIL to verify both mip-level selection and coordinate decoding. **Why** On RDNA2 (`A16=1`), a biased image sample's `f16` LOD bias occupies a full 32-bit VGPR in its low 16 bits (`[15:0]`, with `[31:16]` unused padding), and the packed 16-bit texture coordinates start at `[15:0]` of the following VGPR — matching LLVM AMDGPU (`packImage16bitOpsToDwords` / `getAddrSizeMIMGOp`) and XCL's own disassembler (`rdna2::mimg_address_dwords`). Previously, both `lower_image.rs` and `emit.rs` pushed `(16, false)` for `bias` and `(16, false)` for `body[0]` whenever `grad_n == 0`. For `image_sample_b`, `image_sample_b_cl`, `image_sample_b_o`, and `image_sample_b_cl_o` with `A16=1` (no `z-compare` dword between bias and coordinates), `body[0]` (`s`) was packed into the unused upper 16 bits of the bias VGPR (`v_bias[31:16]`), shifting every subsequent coordinate (`t`, `r`/slice/face) and trailing `cl` by 16 bits and dropping the final half-register. **Tested** Hardware: NVIDIA GeForce RTX 5070 Ti (driver `32.0.16.1074`), AMD Ryzen 9 9950X, Windows 11 Pro (Build 26200), `rustc 1.99.0`, DXC `1.9.2609.5` (`dxil 1.9.2609.5`). - Rebased cleanly onto `origin/main` (`3b6a91ed`); `cargo check --workspace --all-targets`: 0 errors, 0 new warnings. - `cargo build --workspace` and `cargo build --release -p xcl`: succeeded. - Targeted CPU, DXIL validation, and RTX 5070 Ti GPU tests (`cargo test -p xcl --lib a16_ -- --nocapture`): passed (0 failed). - 150 s real-title smoke on RTX 5070 Ti (`XCL_DIRECT_DXIL=all`, `XCL_HEAP_WINDOWS=1`, `XCL_DRED=1`, `XCL_DIRECT_SPLIT_INSTRUCTIONS=0`): PR (`b68b6e2b`): ``` xcl-smoke v1 commit: b68b6e2b1ea3f18570f96a43a2e722a2bffc943c gpu: NVIDIA GeForce RTX 5070 Ti title: ea3b5e4d seconds: 150 exit: 0x0 frames shown: 1550 (PresentX 1550) presents per 2 s (last 3): 0 0 0 device removals: 0 crashes: 0 peak commit: 33309 MB ``` Baseline (`main` ancestor `f0e8fdd5`, same machine and settings): ``` xcl-smoke v1 commit: f0e8fdd5c3942ddd399d5ee11384c8595c5aacff gpu: NVIDIA GeForce RTX 5070 Ti title: ea3b5e4d seconds: 150 exit: 0x0 frames shown: 1695 (PresentX 1695) presents per 2 s (last 3): 0 0 0 device removals: 0 crashes: 0 peak commit: 31546 MB ``` **Not tested** - AMD RDNA hardware (only NVIDIA RTX 5070 Ti is available locally). - `_g16` 16-bit gradient packing (already covered separately by open PR #436). - Full workspace suite on `3b6a91ed` currently hits 4 pre-existing `origin/main` split-wave64 geometry test failures unrelated to this PR; all targeted `a16_` GPU and validation tests pass on `b68b6e2b`. - Real-title runtime hits of `image_sample_b*` with `A16=1` were not specifically instrumented; the 150 s smoke verifies zero regressions across the real title while the targeted GPU tests directly exercise the fixed compute and multi-mip pixel-shader paths. **AI use** Diagnosed, implemented, tested, and written with an AI coding assistant; all code, tests, DXIL validation, and real-hardware GPU/smoke runs were executed and verified directly.
Foniass commented 2026-12-01 03:44:03 +00:00 (Migrated from github.com)

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


Review (head b68b6e2b, diff read, not a merge-check; it conflicts with main, so not built or run):

  • Needs a rebase, not superseded. The only conflict is the new test at the end of heap_bounds_tests in lower_image.rs, where #2060 (3db0ee4e) added direct_image_gather4h_and_8h_are_rejected. The fix lines merge cleanly, and main still has the old layout (lower_image.rs ~1094/1106, emit.rs ~2659/2671).
  • The fix looks right. With A16, main puts the first coordinate into the unused upper half of the bias register, shifting every later coordinate by 16 bits. Starting the first coordinate on a new register (k == 0) gives the hardware layout (bias alone in the low half of its own dword, then coordinates two per dword), matching rdna2::mimg_address_dwords. The (h, true) change on the bias entry has no effect, since bias always comes first or right after the 32-bit offset. Lod and clamp still pack after the coordinates, which is right.
  • Description: AI use names no tool or model; Tested has no full-suite counts; the smoke blocks belong under Regression run. The PR's CI is red on msg_gs_done_emits_in_dxil and geometry_export_rejected, which it calls pre-existing on main; worth confirming after the rebase.
  • Minor: the new test re-defines half_bits / tsharp (already in recomp/tests.rs) and sits in a module about heap bounds; the gather4_b forms take the same fix but have no test.

Fine: the same change in both backends. The CPU layout checks and both GPU tests (bias register with a poisoned upper half) would fail on main, and the GPU tests use skip_test. About 260 lines, one change. The PR's smoke run is within limits (91 % of frames, 106 % of peak commit).

AI review: Claude Code (Claude Opus 5.5), posted by Foniass without a human check of each point.

<!-- greyfinch-sync:ivall/xcl:comment:7300009099 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3351#issuecomment-7300009099). Originally posted by [Foniass](https://github.com/Foniass) on 2026-12-01T03:44:03Z. --- Review (head b68b6e2b, diff read, not a merge-check; it conflicts with main, so not built or run): - **Needs a rebase, not superseded.** The only conflict is the new test at the end of `heap_bounds_tests` in `lower_image.rs`, where #2060 (3db0ee4e) added `direct_image_gather4h_and_8h_are_rejected`. The fix lines merge cleanly, and main still has the old layout (`lower_image.rs` ~1094/1106, `emit.rs` ~2659/2671). - **The fix looks right.** With A16, main puts the first coordinate into the unused upper half of the bias register, shifting every later coordinate by 16 bits. Starting the first coordinate on a new register (`k == 0`) gives the hardware layout (bias alone in the low half of its own dword, then coordinates two per dword), matching `rdna2::mimg_address_dwords`. The `(h, true)` change on the bias entry has no effect, since bias always comes first or right after the 32-bit offset. Lod and clamp still pack after the coordinates, which is right. - **Description:** AI use names no tool or model; **Tested** has no full-suite counts; the smoke blocks belong under **Regression run**. The PR's CI is red on `msg_gs_done_emits_in_dxil` and `geometry_export_rejected`, which it calls pre-existing on main; worth confirming after the rebase. - Minor: the new test re-defines `half_bits` / `tsharp` (already in `recomp/tests.rs`) and sits in a module about heap bounds; the `gather4_b` forms take the same fix but have no test. Fine: the same change in both backends. The CPU layout checks and both GPU tests (bias register with a poisoned upper half) would fail on main, and the GPU tests use `skip_test`. About 260 lines, one change. The PR's smoke run is within limits (91 % of frames, 106 % of peak commit). AI review: Claude Code (Claude Opus 5.5), posted by Foniass without a human check of each point.
Kalmarv commented 2026-12-02 20:34:03 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by Kalmarv on 2026-12-02T20:34:03Z.


Consolidated into #3728 (Direct DXIL: MIMG operand layout fixes (A16 bias VGPR, S# FORCE_UNORMALIZED)), keeping the direct-DXIL lower_image.rs fix and its tests while omitting the legacy HLSL recomp/emit.rs changes. Closing this PR as superseded.

(Commented via Antigravity (Gemini 4 Neon).)

<!-- greyfinch-sync:ivall/xcl:comment:7357945539 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/3351#issuecomment-7357945539). Originally posted by [Kalmarv](https://github.com/Kalmarv) on 2026-12-02T20:34:03Z. --- Consolidated into #3728 (`Direct DXIL: MIMG operand layout fixes (A16 bias VGPR, S# FORCE_UNORMALIZED)`), keeping the direct-DXIL `lower_image.rs` fix and its tests while omitting the legacy HLSL `recomp/emit.rs` changes. Closing this PR as superseded. *(Commented via Antigravity (Gemini 4 Neon).)*

Pull request closed

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!3351
No description provided.