startup: unpack 2D A16 coordinates for image_sample and image_sample_lz #1990

Merged
consolelogkyo merged 1 commit from upstream-pr/1997 into main 2026-11-24 23:26:03 +00:00
consolelogkyo commented 2026-11-24 22:51:47 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by consolelogkyo on 2026-11-24T22:51:47Z.


Summary

Fixes silent miscompilation of 2D image_sample and image_sample_lz instructions carrying the a16 packed 16-bit coordinate modifier in the startup shader translator (Refs #1723).

Background & What Changed

On RDNA 2 (gfx1013), 2D image_sample and image_sample_lz with a16 = 1 pack both IEEE binary16 texture coordinates into a single VGPR (u in bits 15:0, v in bits 31:16) without an NSA extension — the same single-register layout already handled for plain 2D image_load (#1854). Previously:

  1. tools/disasm_rdna2.py (decode_instruction): Only checked op_name == "image_load" when collapsing 2D a16 addr_count to 1, so 2D a16 image_sample and image_sample_lz disassembled with a two-register range v[N:N+1] instead of vN and did not flag a spurious NSA extension (invalid_nsa).
  2. tools/startup/gfx2hlsl.py (Stage.image): The sample / lz path checked neither modifiers nor dmask nor address-register counts on 2D, 3D, or 2D-array images. As a result, a 2D a16 sample translated without error into the shader cache while reading the raw packed word as binary32 u and the adjacent VGPR v(N+1) as binary32 v — sampling wrong texels at draw time. Unmodeled modifiers (unorm, r128, tfe, lwe, d16, reserved, invalid_nsa, and a16 on 3D / 2D-array dimensions) and empty dmask:0x0 were likewise silently dropped.
  3. Shader cache revision (tools/startup/gfx2hlsl.py & src/startup_stubs/d3d12_draw.inl): Both the offline pipeline_key seed and the desktop runtime revision[] advance in lockstep from x2w-startup-shader-5 to x2w-startup-shader-6. User-visible effect: any existing x2w-startup-shader-5 shader cache is regenerated on the next run rather than reused, because previously cached a16 sample pipelines hold the miscompiled two-VGPR translation.

With this change:

  • tools/disasm_rdna2.py decodes one address VGPR (vN) for 2D a16 image_load, image_sample, and image_sample_lz, surfacing invalid_nsa if nsize > 0 (leaving image_sample_l and 3D / 2D-array a16 forms unchanged).
  • Stage.image unpacks 2D a16 image_sample and image_sample_lz coordinates as float2(f16tof32(p), f16tof32(p >> 16)) (f16tof32 is the built-in Shader Model 5.0 HLSL intrinsic, so generated shaders compile under FXC ps_5_1 and DXC ps_6_0 directly with no helper definition in common()), preserves cache-policy flags (dlc, glc, slc) and valid 2D NSA pairs, and refuses unmodeled modifiers, empty dmask, wrong address-register counts, and 3D / 2D-array a16 with Unsupported.
  • tests/offscreen_check.cpp / tests/startup_image_3d.cpp add --startup-image-2d (single-slice 2x2 R32G32B32A32_FLOAT Texture2D, accepting exact 0x... 32-bit packed coordinate words in ud1) so translated 2D Texture2D shaders can be drawn unmodified under both DXC (ps_6_0) and FXC (ps_5_1).

Verification

  • Pure-Python & LLVM parity (tests/startup_image_sample_a16_check.py):
    • Verifies disasm_rdna2.decode_instruction single-VGPR a16 decoding and invalid_nsa flagging, lockstep offline/runtime x2w-startup-shader-6 cache keys, Sample vs SampleLevel(..., 0) lowering, non-v0 packed coordinate registers (v2), partial dmask, 2D NSA [v0, v3], cache-policy flags (glc, slc, dlc), 1D guard preservation, and refusal of unorm, tfe, lwe, d16, r128, invalid_nsa, all 6 reserved MIMG bits, dmask:0x0, 3D / 2D-array a16, and wrong address-register counts.
    • Wired into .github/workflows/windows.yml for both binary llvm-mc-15 assembly parity and decode libLLVM-15.so.1 disassembly parity.
  • Desktop GPU readback (startup_image_sample_a16_warp & startup_image_sample_a16_hardware):
    • Verified on commit base 82c07000, Windows 11 26H2 (build 26300.9974), D3D12 debug layer enabled (debug_errors=0 debug_warnings=0), with both DXC (ps_6_0) and FXC (ps_5_1) on:
      • Microsoft Basic Render Driver (1414:008c, driver 10.0.26100.9818)
      • AMD Radeon RX 9070 XT (1002:7550, driver 32.0.32029.2010)
    • All 3 pixel fixtures (sample_a16, lz_a16, and lz_v2_cache) match all four 2x2 quadrants plus negative low-half (u = -0.25) and negative high-half (v = -0.25, bit 31 set) wrap cases, while all 6 mutated HLSL negative controls (pre-fix float2(F(v0), F(v1)), swapped halves, dropped >> 16, >> 15, >> 17, and hardcoded v0 instead of v2) fail readback with the expected mismatched texel.
cmake --build --preset dev --target offscreen_check d3d12_xs_shim
ctest --preset dev --no-tests=error -R '^(startup_image_sample_a16_(controls|warp|hardware)|startup_image_load_mip_(controls|warp|hardware)|startup_gfx2hlsl_refusals|startup_tools_lint)$' -V
ctest --preset pr --no-tests=error

AI-assisted.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6593005590 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1997). Originally posted by [consolelogkyo](https://github.com/consolelogkyo) on 2026-11-24T22:51:47Z. --- ## Summary Fixes silent miscompilation of 2D `image_sample` and `image_sample_lz` instructions carrying the `a16` packed 16-bit coordinate modifier in the startup shader translator (`Refs #1723`). ### Background & What Changed On RDNA 2 (`gfx1013`), 2D `image_sample` and `image_sample_lz` with `a16 = 1` pack both IEEE binary16 texture coordinates into a single VGPR (`u` in bits `15:0`, `v` in bits `31:16`) without an NSA extension — the same single-register layout already handled for plain 2D `image_load` (`#1854`). Previously: 1. **`tools/disasm_rdna2.py` (`decode_instruction`):** Only checked `op_name == "image_load"` when collapsing 2D `a16` `addr_count` to `1`, so 2D `a16` `image_sample` and `image_sample_lz` disassembled with a two-register range `v[N:N+1]` instead of `vN` and did not flag a spurious NSA extension (`invalid_nsa`). 2. **`tools/startup/gfx2hlsl.py` (`Stage.image`):** The `sample` / `lz` path checked neither modifiers nor `dmask` nor address-register counts on 2D, 3D, or 2D-array images. As a result, a 2D `a16` sample translated without error into the shader cache while reading the raw packed word as binary32 `u` and the adjacent VGPR `v(N+1)` as binary32 `v` — sampling wrong texels at draw time. Unmodeled modifiers (`unorm`, `r128`, `tfe`, `lwe`, `d16`, `reserved`, `invalid_nsa`, and `a16` on 3D / 2D-array dimensions) and empty `dmask:0x0` were likewise silently dropped. 3. **Shader cache revision (`tools/startup/gfx2hlsl.py` & `src/startup_stubs/d3d12_draw.inl`):** Both the offline `pipeline_key` seed and the desktop runtime `revision[]` advance in lockstep from `x2w-startup-shader-5` to `x2w-startup-shader-6`. **User-visible effect:** any existing `x2w-startup-shader-5` shader cache is regenerated on the next run rather than reused, because previously cached `a16` sample pipelines hold the miscompiled two-VGPR translation. With this change: - `tools/disasm_rdna2.py` decodes one address VGPR (`vN`) for 2D `a16` `image_load`, `image_sample`, and `image_sample_lz`, surfacing `invalid_nsa` if `nsize > 0` (leaving `image_sample_l` and 3D / 2D-array `a16` forms unchanged). - `Stage.image` unpacks 2D `a16` `image_sample` and `image_sample_lz` coordinates as `float2(f16tof32(p), f16tof32(p >> 16))` (`f16tof32` is the built-in Shader Model 5.0 HLSL intrinsic, so generated shaders compile under FXC `ps_5_1` and DXC `ps_6_0` directly with no helper definition in `common()`), preserves cache-policy flags (`dlc`, `glc`, `slc`) and valid 2D NSA pairs, and refuses unmodeled modifiers, empty `dmask`, wrong address-register counts, and 3D / 2D-array `a16` with `Unsupported`. - `tests/offscreen_check.cpp` / `tests/startup_image_3d.cpp` add `--startup-image-2d` (single-slice `2x2` `R32G32B32A32_FLOAT` `Texture2D`, accepting exact `0x...` 32-bit packed coordinate words in `ud1`) so translated 2D `Texture2D` shaders can be drawn unmodified under both DXC (`ps_6_0`) and FXC (`ps_5_1`). ## Verification - **Pure-Python & LLVM parity (`tests/startup_image_sample_a16_check.py`):** - Verifies `disasm_rdna2.decode_instruction` single-VGPR `a16` decoding and `invalid_nsa` flagging, lockstep offline/runtime `x2w-startup-shader-6` cache keys, `Sample` vs `SampleLevel(..., 0)` lowering, non-`v0` packed coordinate registers (`v2`), partial `dmask`, 2D NSA `[v0, v3]`, cache-policy flags (`glc`, `slc`, `dlc`), 1D guard preservation, and refusal of `unorm`, `tfe`, `lwe`, `d16`, `r128`, `invalid_nsa`, all 6 reserved MIMG bits, `dmask:0x0`, 3D / 2D-array `a16`, and wrong address-register counts. - Wired into `.github/workflows/windows.yml` for both `binary llvm-mc-15` assembly parity and `decode libLLVM-15.so.1` disassembly parity. - **Desktop GPU readback (`startup_image_sample_a16_warp` & `startup_image_sample_a16_hardware`):** - Verified on commit base `82c07000`, Windows 11 26H2 (build `26300.9974`), D3D12 debug layer enabled (`debug_errors=0 debug_warnings=0`), with both DXC (`ps_6_0`) and FXC (`ps_5_1`) on: - **Microsoft Basic Render Driver** (`1414:008c`, driver `10.0.26100.9818`) - **AMD Radeon RX 9070 XT** (`1002:7550`, driver `32.0.32029.2010`) - All 3 pixel fixtures (`sample_a16`, `lz_a16`, and `lz_v2_cache`) match all four `2x2` quadrants plus negative low-half (`u = -0.25`) and negative high-half (`v = -0.25`, bit 31 set) wrap cases, while all 6 mutated HLSL negative controls (pre-fix `float2(F(v0), F(v1))`, swapped halves, dropped `>> 16`, `>> 15`, `>> 17`, and hardcoded `v0` instead of `v2`) fail readback with the expected mismatched texel. ```powershell cmake --build --preset dev --target offscreen_check d3d12_xs_shim ctest --preset dev --no-tests=error -R '^(startup_image_sample_a16_(controls|warp|hardware)|startup_image_load_mip_(controls|warp|hardware)|startup_gfx2hlsl_refusals|startup_tools_lint)$' -V ctest --preset pr --no-tests=error ``` AI-assisted.
ChazY11 commented 2026-11-24 23:03:41 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by ChazY11 on 2026-11-24T22:54:57Z.


Ran it, broke it five ways — all caught, decoder and translator independently

Windows 11 build 26300.9974, device-free:

out/pe-env/Scripts/python.exe -B tests/startup_image_sample_a16_check.py    # exit 0
out/pe-env/Scripts/python.exe -B tests/rdna2_tools_check.py                 # exit 0
out/pe-env/Scripts/python.exe -B tests/startup_image_sample_l_check.py      # exit 0
out/pe-env/Scripts/python.exe -B tests/startup_translator_check.py          # exit 0
mutation result
tools/disasm_rdna2.py: op_name in ("image_load", "image_sample", "image_sample_lz") → op_name == "image_load" (the pre-change line) caught
tools/disasm_rdna2.py: addr_count = 1 dropped caught
tools/startup/gfx2hlsl.py: f16tof32(p >> 16) → f16tof32(p) for v (both halves read the low half) caught
tools/startup/gfx2hlsl.py: the empty-dmask refusal disabled caught
tools/startup/gfx2hlsl.py: the address-register count check disabled caught

The first is the original defect, so the fixture genuinely fails without the fix. The third is the miscompilation this PR is about — reading the
raw packed word as binary32 u and the neighbour as v — and it is caught at the text level, which is the right level for it.

Two things I checked rather than assumed

  • f16tof32 needs no local definition. It appears at seven sites but common() defines only mul_legacy, ftz32, mad32, med3, class32
    and friends. It is the SM 5.0 HLSL intrinsic, so the generated shader compiles under FXC ps_5_1 without a preamble addition — worth saying in the
    description, because a reader who greps for a definition will not find one.
  • The halves are the right way round. u from bits 15:0, v from 31:16, matching the image_load A16 layout handled since #1854. The
    fixture pins both halves in order (float2(f16tof32(v0), f16tof32(v0 >> 16))), so a swap cannot pass.

On the refusals this PR adds

The sample/lz path previously checked neither modifiers nor dmask nor the address count on 2D, 3D and 2D-array images, and dropped unorm,
r128, tfe, lwe, d16, reserved, invalid_nsa and a16-on-3D/array silently. Turning nine silent drops into refusals is the larger half of
this change by risk, and three of my mutations land there — all caught, so the whitelist is exercised.

One thing worth stating in the description

The cache revision moves to x2w-startup-shader-6. That is a user-visible behaviour: an existing cache from -5 is regenerated rather than reused.
Saying so explicitly, with the reason (previously cached A16 samples hold the wrong translation), prevents someone from "restoring" the old revision
to avoid the rebuild.

Scope

No draw was run here; the changes to d3d12_draw.inl, offscreen_check.cpp and startup_image_3d.cpp are part of the WARP/hardware route that the
description does not claim to have verified for this PR. Everything above is the decoder and the emitted text.

Verdict

Reviewed. Five independent breakages, five failures, decoder and translator each pinned separately.

Notes

  • AI-assisted review.
<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7146791888 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1997#issuecomment-7146791888). Originally posted by [ChazY11](https://github.com/ChazY11) on 2026-11-24T22:54:57Z. --- ## Ran it, broke it five ways — all caught, decoder and translator independently Windows 11 build 26300.9974, device-free: ```powershell out/pe-env/Scripts/python.exe -B tests/startup_image_sample_a16_check.py # exit 0 out/pe-env/Scripts/python.exe -B tests/rdna2_tools_check.py # exit 0 out/pe-env/Scripts/python.exe -B tests/startup_image_sample_l_check.py # exit 0 out/pe-env/Scripts/python.exe -B tests/startup_translator_check.py # exit 0 ``` | mutation | result | |---|---| | `tools/disasm_rdna2.py`: `op_name in ("image_load", "image_sample", "image_sample_lz")` → `op_name == "image_load"` (the pre-change line) | **caught** | | `tools/disasm_rdna2.py`: `addr_count = 1` dropped | **caught** | | `tools/startup/gfx2hlsl.py`: `f16tof32(p >> 16)` → `f16tof32(p)` for `v` (both halves read the low half) | **caught** | | `tools/startup/gfx2hlsl.py`: the empty-`dmask` refusal disabled | **caught** | | `tools/startup/gfx2hlsl.py`: the address-register count check disabled | **caught** | The first is the original defect, so the fixture genuinely fails without the fix. The third is the miscompilation this PR is *about* — reading the raw packed word as binary32 `u` and the neighbour as `v` — and it is caught at the text level, which is the right level for it. ## Two things I checked rather than assumed - **`f16tof32` needs no local definition.** It appears at seven sites but `common()` defines only `mul_legacy`, `ftz32`, `mad32`, `med3`, `class32` and friends. It is the SM 5.0 HLSL intrinsic, so the generated shader compiles under FXC `ps_5_1` without a preamble addition — worth saying in the description, because a reader who greps for a definition will not find one. - **The halves are the right way round.** `u` from bits `15:0`, `v` from `31:16`, matching the `image_load` A16 layout handled since #1854. The fixture pins both halves in order (`float2(f16tof32(v0), f16tof32(v0 >> 16))`), so a swap cannot pass. ## On the refusals this PR adds The `sample`/`lz` path previously checked neither modifiers nor `dmask` nor the address count on 2D, 3D and 2D-array images, and dropped `unorm`, `r128`, `tfe`, `lwe`, `d16`, `reserved`, `invalid_nsa` and `a16`-on-3D/array silently. Turning nine silent drops into refusals is the larger half of this change by risk, and three of my mutations land there — all caught, so the whitelist is exercised. ## One thing worth stating in the description The cache revision moves to `x2w-startup-shader-6`. That is a user-visible behaviour: an existing cache from `-5` is regenerated rather than reused. Saying so explicitly, with the reason (previously cached A16 samples hold the wrong translation), prevents someone from "restoring" the old revision to avoid the rebuild. ## Scope No draw was run here; the changes to `d3d12_draw.inl`, `offscreen_check.cpp` and `startup_image_3d.cpp` are part of the WARP/hardware route that the description does not claim to have verified for this PR. Everything above is the decoder and the emitted text. ## Verdict Reviewed. Five independent breakages, five failures, decoder and translator each pinned separately. ## Notes - AI-assisted review.
consolelogkyo commented 2026-11-24 23:03:42 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by consolelogkyo on 2026-11-24T23:01:47Z.


Thank you @ChazY11 for the thorough review and the five-way mutation check!

I updated the PR description to call out both points explicitly:

  • f16tof32 is the built-in SM 5.0 HLSL intrinsic (so fxc ps_5_1 compiles it directly without a helper in common()).
  • Advancing the cache revision to x2w-startup-shader-6 intentionally regenerates existing -5 shader caches on the next run so pre-fix a16 sample translations are not reused.

AI-assisted.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7146928294 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1997#issuecomment-7146928294). Originally posted by [consolelogkyo](https://github.com/consolelogkyo) on 2026-11-24T23:01:47Z. --- Thank you @ChazY11 for the thorough review and the five-way mutation check! I updated the PR description to call out both points explicitly: - `f16tof32` is the built-in SM 5.0 HLSL intrinsic (so `fxc` `ps_5_1` compiles it directly without a helper in `common()`). - Advancing the cache revision to `x2w-startup-shader-6` intentionally regenerates existing `-5` shader caches on the next run so pre-fix `a16` sample translations are not reused. AI-assisted.
upstream-sync closed this pull request 2026-11-25 00:04:49 +00:00
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
R0Y00/Xbox2Windows!1990
No description provided.