shader: lower d16 image_sample and image_sample_lz in gfx2hlsl #3908

Merged
Leyukaka merged 4 commits from upstream-pr/3908 into main 2026-12-03 02:17:26 +00:00
Leyukaka commented 2026-12-03 01:32:16 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by Leyukaka on 2026-12-03T01:32:16Z.


Refs #2023, #2463

Change

Pixel-stage image_sample and image_sample_lz with d16 were refused as image_sample[_lz] modifier d16.
This is one refusal reason that recurs across many pixel shaders; this PR removes it for the forms below and
leaves the others refused by name.

  • tools/startup/gfx2hlsl.py (Stage.image): for 2D, 2D-array and 3D images, d16 is accepted on
    image_sample (implicit LOD) and image_sample_lz. The channels enabled by dmask, in dmask order, are
    narrowed to binary16 with the existing pack16_rne helper and packed pairwise, the first channel of each pair
    in bits 15:0. This is the same rounding and packing as the native compute d16_sample_srv lowering
    (packed_half_rne).
  • The destination must be exactly ceil(popcount(dmask) / 2) VGPRs. With an odd channel count, bits 31:16 of
    the last VGPR are undefined: it is marked with the existing low16_only tracking, so a whole-word read of it
    stays refused.
  • Still refused: a16 together with d16, a destination width other than ceil(popcount(dmask) / 2), a
    repeated d16, cube and 1D d16 samples (both were already refused by the earlier cube and 1D modifier
    checks), d16 with tfe / lwe / r128 / unorm, compute stages, and image_sample_l / image_sample_b
    with d16.
  • docs/startup-runtime.md: new "D16 sample results" entry; d16 removed from the refused list of the packed
    2D sample entry.

Verification

Commit d6788f2aa on main b6171650f, Windows, MSVC, WARP and one hardware adapter:

ctest --preset release --no-tests=error -R "^startup_image_sample_(d16|a16)_|^startup_image_load_d16_" -V

Result: 9 of 9 passed: startup_image_sample_d16_controls, startup_image_sample_d16_warp,
startup_image_sample_d16_hardware, and the existing startup_image_sample_a16_* and
startup_image_load_d16_* tests.

New tests/startup_image_sample_d16_check.py:

  • controls (no arguments, pure Python): emitted HLSL for dmask 0xf (NSA image_sample_lz and implicit-LOD
    image_sample), odd dmask 0x7 and single channel 0x2, 3D and 2D-array with both ops, and the non-d16 form
    unchanged; refusals for the odd-tail whole-word read, a16+d16, cube and 1D d16, tfe / lwe / r128 /
    unorm with d16, wrong destination widths, a repeated d16 and a compute stage.
  • draw <dxc> <offscreen_check> <warp|hardware> [fxc]: four fixtures (_lz NSA dmask 0xf, implicit
    image_sample dmask 0xf, _lz dmask 0x7, _lz NSA dmask 0x2) draw against the 2x2 RGBA32F texture of
    tests/startup_image_3d.cpp (--startup-image-2d). The packed halves are widened back with v_cvt_f32_f16
    and must equal the sampled texel. Swapped-half, missing-shift, wrong-pair and wrong-channel mutants must fail
    readback.
  • binary <llvm-mc>: reassembles every fixture source and compares it with the recorded words. It was not run
    here: the Windows build has no llvm-mc. The diff adds it to the pixel-binary job (LLVM 15) in
    .github/workflows/windows.yml, so its first run is CI.

tests/startup_image_sample_a16_check.py and tests/startup_image_load_d16_check.py pinned the old refusal;
they now pin the a16+d16 refusal.

Evidence and follow-up

  • Fixtures are hand-encoded MIMG words written for this test; no external input.
  • Rounding is round-to-nearest-even, chosen to match the native compute path. The hardware rounding of a d16
    sample result was not measured independently, and filtering itself is not bit-exact across GPUs. The draw
    texels are exact in binary16, so the draws check packing and channel order, not rounding.
  • pack16_rne maps every NaN to the canonical sign | 0x7e00; the NaN payload is lost.
  • Unlike the d16 image_load lowering, no per-format exact UNORM16 mode is applied: a filtered result has no
    exact binary16 value to select per format.
  • 3D and 2D-array d16 samples have positive translation controls only; neither is drawn in this
    test.
  • Open for follow-up: a16 with d16, cube and 1D d16 samples, image_sample_l / image_sample_b with d16.
<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:5618001253 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/3908). Originally posted by [Leyukaka](https://github.com/Leyukaka) on 2026-12-03T01:32:16Z. --- Refs #2023, #2463 ## Change Pixel-stage `image_sample` and `image_sample_lz` with `d16` were refused as `image_sample[_lz] modifier d16`. This is one refusal reason that recurs across many pixel shaders; this PR removes it for the forms below and leaves the others refused by name. - `tools/startup/gfx2hlsl.py` (`Stage.image`): for 2D, 2D-array and 3D images, `d16` is accepted on `image_sample` (implicit LOD) and `image_sample_lz`. The channels enabled by `dmask`, in `dmask` order, are narrowed to binary16 with the existing `pack16_rne` helper and packed pairwise, the first channel of each pair in bits 15:0. This is the same rounding and packing as the native compute `d16_sample_srv` lowering (`packed_half_rne`). - The destination must be exactly `ceil(popcount(dmask) / 2)` VGPRs. With an odd channel count, bits 31:16 of the last VGPR are undefined: it is marked with the existing `low16_only` tracking, so a whole-word read of it stays refused. - Still refused: `a16` together with `d16`, a destination width other than `ceil(popcount(dmask) / 2)`, a repeated `d16`, cube and 1D `d16` samples (both were already refused by the earlier cube and 1D modifier checks), `d16` with `tfe` / `lwe` / `r128` / `unorm`, compute stages, and `image_sample_l` / `image_sample_b` with `d16`. - `docs/startup-runtime.md`: new "D16 sample results" entry; `d16` removed from the refused list of the packed 2D sample entry. ## Verification Commit d6788f2aa on main b6171650f, Windows, MSVC, WARP and one hardware adapter: ```text ctest --preset release --no-tests=error -R "^startup_image_sample_(d16|a16)_|^startup_image_load_d16_" -V ``` Result: 9 of 9 passed: `startup_image_sample_d16_controls`, `startup_image_sample_d16_warp`, `startup_image_sample_d16_hardware`, and the existing `startup_image_sample_a16_*` and `startup_image_load_d16_*` tests. New `tests/startup_image_sample_d16_check.py`: - controls (no arguments, pure Python): emitted HLSL for dmask 0xf (NSA `image_sample_lz` and implicit-LOD `image_sample`), odd dmask 0x7 and single channel 0x2, 3D and 2D-array with both ops, and the non-d16 form unchanged; refusals for the odd-tail whole-word read, a16+d16, cube and 1D d16, `tfe` / `lwe` / `r128` / `unorm` with d16, wrong destination widths, a repeated d16 and a compute stage. - `draw <dxc> <offscreen_check> <warp|hardware> [fxc]`: four fixtures (`_lz` NSA dmask 0xf, implicit `image_sample` dmask 0xf, `_lz` dmask 0x7, `_lz` NSA dmask 0x2) draw against the 2x2 RGBA32F texture of `tests/startup_image_3d.cpp` (`--startup-image-2d`). The packed halves are widened back with `v_cvt_f32_f16` and must equal the sampled texel. Swapped-half, missing-shift, wrong-pair and wrong-channel mutants must fail readback. - `binary <llvm-mc>`: reassembles every fixture source and compares it with the recorded words. It was not run here: the Windows build has no llvm-mc. The diff adds it to the `pixel-binary` job (LLVM 15) in `.github/workflows/windows.yml`, so its first run is CI. `tests/startup_image_sample_a16_check.py` and `tests/startup_image_load_d16_check.py` pinned the old refusal; they now pin the a16+d16 refusal. ## Evidence and follow-up - Fixtures are hand-encoded MIMG words written for this test; no external input. - Rounding is round-to-nearest-even, chosen to match the native compute path. The hardware rounding of a d16 sample result was not measured independently, and filtering itself is not bit-exact across GPUs. The draw texels are exact in binary16, so the draws check packing and channel order, not rounding. - `pack16_rne` maps every NaN to the canonical `sign | 0x7e00`; the NaN payload is lost. - Unlike the d16 `image_load` lowering, no per-format exact UNORM16 mode is applied: a filtered result has no exact binary16 value to select per format. - 3D and 2D-array d16 samples have positive translation controls only; neither is drawn in this test. - Open for follow-up: `a16` with `d16`, cube and 1D d16 samples, `image_sample_l` / `image_sample_b` with `d16`.
R0Y00 commented 2026-12-03 02:10:36 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-12-03T02:10:36Z.

Review: APPROVED


Independently reviewed b157bd11 after the baseline update. The selected channels pack in dmask order. The odd destination tail carries the existing low16-only guard, and unsupported sample forms stay refused. MSVC Debug, Windows 11 build 26300: ctest --preset dev --no-tests=error -j 1 -V -R '^(startup_image_sample_(d16|a16)_|startup_image_load_d16_)' passed 9/9 on WARP and RTX 4060 Laptop (616.56), zero skips. In the new D16 case, DXC and FXC each read back eight sampled texels per adapter and all four shader mutations per compiler and adapter were caught. 3D and array coverage here is translation-only. The GPU fixture's exact half inputs verify packing and channel order.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6162506373 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/3908#pullrequestreview-6162506373). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-12-03T02:10:36Z. **Review:** APPROVED --- Independently reviewed b157bd11 after the baseline update. The selected channels pack in dmask order. The odd destination tail carries the existing low16-only guard, and unsupported sample forms stay refused. MSVC Debug, Windows 11 build 26300: `ctest --preset dev --no-tests=error -j 1 -V -R '^(startup_image_sample_(d16|a16)_|startup_image_load_d16_)'` passed 9/9 on WARP and RTX 4060 Laptop (616.56), zero skips. In the new D16 case, DXC and FXC each read back eight sampled texels per adapter and all four shader mutations per compiler and adapter were caught. 3D and array coverage here is translation-only. The GPU fixture's exact half inputs verify packing and channel order.
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!3908
No description provided.