xshader: saturate D16 integer image loads and look through half packing in type inference #159

Closed
github-bot wants to merge 1 commit from gh-pr/159/head into gh-pr/159/base
Owner

@⁠GDAgent on GitHub · 2026-11-22 16:49 UTC · original
GDAgent:port/xshader-d16-integer-images → main · +269 −13 · 6 files · 1 commits · closed without merging

Library / area: tombox-xshader (hlsl/mimg_d16.rs, hlsl.rs, tests/synthetic.rs, tests/exec_mem/mimg.rs, tests/exec_mem/mod.rs)

Exports changed (exports.txt rows): none

Source of the behaviour: RDNA2 D16 integer image load/store semantics as modelled by Mesa RADV (integer_dest_saturates in ac_nir_opt_16bit_tex_image and opt_image_store_data in nir_opt_16bit_tex_image). Ported from an earlier TomBox commit (86a9899, authored by this account; MIT OR Apache-2.0); the Mesa reference is credited in THIRD_PARTY/CREDITS.

Changes:

  • Update mimg_d16::lane_to_half so D16 integer image loads saturate each component to 16 bits (min(x, 0xffffu) for Lane::Uint, asuint(clamp(asint(x), -32768, 32767)) & 0xffffu for Lane::Sint) instead of truncating with & 0xffffu.
  • Update Emitter::infer_tbuf_types so D16 half-packing/unpacking operations (v_mov_b32, v_cndmask_b32, v_lshl_or_b32 with shift 16, register-to-register v_or_b32, v_and_b32 with 0xffff/0xffff0000, 16-bit v_lshrrev_b32/v_lshlrev_b32, v_perm_b32, etc.) are followed to the half producers/consumers rather than counted as integer evidence.
  • Refuse D16 image atomics and non-zero D16 access to UAVs whose uint type comes only from image_atomic_* (where texel signedness is unknown).
  • Add unit tests in mimg_d16, synthetic half-packing inference/refusal tests in tests/synthetic.rs, and 56 D3D12 execution cases in tests/exec_mem/mimg.rs covering D16 UINT4 and SINT4 loads (out-of-range saturation) and stores (zero- and sign-extension) across 7 DMASK patterns.

Tests (native Windows 11; not run under Proton):

  • cargo test --release -p tombox-xshader --lib --test opcodes --test synthetic: 117 passed, 0 failed, 2 ignored.

  • cargo test --release -p tombox-xshader --test exec -- image_mimg (D3D12 execution on hardware): 8 passed, including image_mimg_d16_packs_halves and image_mimg_d16_integer_images (56 D16 integer cases, each rerun with runtime inputs).

  • The full exec suite has 3 failures that also occur on plain main: scalar_smem_loads, exec_mem::mtbuf::mtbuf_d16_store_formats, vop::basic::vop2_basic. Nothing extra fails with this change.

  • Not verified in a title with this repo's build.

  • Related, not fixed here: #103 (MIMG load/store coverage).

  • cargo test -p tombox-xshader tests pass

  • No game or console files and no binaries included

  • No fake success; unsupported D16 atomic/unknown-signedness forms fail explicitly

**@⁠GDAgent** on GitHub · 2026-11-22 16:49 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/159) `GDAgent:port/xshader-d16-integer-images` → `main` · +269 −13 · 6 files · 1 commits · closed without merging **Library / area:** tombox-xshader (`hlsl/mimg_d16.rs`, `hlsl.rs`, `tests/synthetic.rs`, `tests/exec_mem/mimg.rs`, `tests/exec_mem/mod.rs`) **Exports changed (exports.txt rows):** none **Source of the behaviour:** RDNA2 D16 integer image load/store semantics as modelled by Mesa RADV (`integer_dest_saturates` in `ac_nir_opt_16bit_tex_image` and `opt_image_store_data` in `nir_opt_16bit_tex_image`). Ported from an earlier TomBox commit (`86a9899`, authored by this account; MIT OR Apache-2.0); the Mesa reference is credited in `THIRD_PARTY/CREDITS`. Changes: - Update `mimg_d16::lane_to_half` so D16 integer image loads saturate each component to 16 bits (`min(x, 0xffffu)` for `Lane::Uint`, `asuint(clamp(asint(x), -32768, 32767)) & 0xffffu` for `Lane::Sint`) instead of truncating with `& 0xffffu`. - Update `Emitter::infer_tbuf_types` so D16 half-packing/unpacking operations (`v_mov_b32`, `v_cndmask_b32`, `v_lshl_or_b32` with shift 16, register-to-register `v_or_b32`, `v_and_b32` with `0xffff`/`0xffff0000`, 16-bit `v_lshrrev_b32`/`v_lshlrev_b32`, `v_perm_b32`, etc.) are followed to the half producers/consumers rather than counted as integer evidence. - Refuse D16 image atomics and non-zero D16 access to UAVs whose `uint` type comes only from `image_atomic_*` (where texel signedness is unknown). - Add unit tests in `mimg_d16`, synthetic half-packing inference/refusal tests in `tests/synthetic.rs`, and 56 D3D12 execution cases in `tests/exec_mem/mimg.rs` covering D16 UINT4 and SINT4 loads (out-of-range saturation) and stores (zero- and sign-extension) across 7 DMASK patterns. Tests (native Windows 11; not run under Proton): - `cargo test --release -p tombox-xshader --lib --test opcodes --test synthetic`: 117 passed, 0 failed, 2 ignored. - `cargo test --release -p tombox-xshader --test exec -- image_mimg` (D3D12 execution on hardware): 8 passed, including `image_mimg_d16_packs_halves` and `image_mimg_d16_integer_images` (56 D16 integer cases, each rerun with runtime inputs). - The full exec suite has 3 failures that also occur on plain `main`: `scalar_smem_loads`, `exec_mem::mtbuf::mtbuf_d16_store_formats`, `vop::basic::vop2_basic`. Nothing extra fails with this change. - Not verified in a title with this repo's build. - Related, not fixed here: #103 (MIMG load/store coverage). - [x] `cargo test -p tombox-xshader` tests pass - [x] No game or console files and no binaries included - [x] No fake success; unsupported D16 atomic/unknown-signedness forms fail explicitly
- Saturate D16 integer image loads to 16 bits in mimg_d16::lane_to_half
  (uint clamped to 0..=0xffff, sint clamped to -32768..=32767) instead of
  masking with & 0xffffu, matching RDNA2 / Mesa RADV integer_dest_saturates.
- Update infer_tbuf_types so D16 half-packing/unpacking moves (v_lshl_or_b32
  by 16, v_and_b32 with 0xffff/0xffff0000, register-to-register v_or_b32,
  16-bit shifts) are followed to the half producers/consumers instead of
  being counted as integer evidence.
- Refuse D16 image atomics and non-zero D16 access to UAVs whose uint type
  comes only from image atomics (texel signedness unknown).
- Add D3D12 execution tests for D16 UINT4/SINT4 loads and stores across
  7 DMASKs plus synthetic half-packing inference and refusal tests.

Ported from local TomBox commit 86a9899 (GDAgent; MIT OR Apache-2.0).
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 06:12:10 +00:00
github-bot closed this pull request 2026-11-26 06:12:10 +00:00
Author
Owner

@⁠omonkeyz on GitHub · 2026-11-22 17:10 UTC · original

Applied in e239468 (merged in 43d17b0) with your authorship kept, thanks. I removed 17 trailing // comments because the repo allows no comment lines, and I resolved a conflict in mimg.rs against current main.

**@⁠omonkeyz** on GitHub · 2026-11-22 17:10 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/159#issuecomment-7082631109) Applied in e239468 (merged in 43d17b0) with your authorship kept, thanks. I removed 17 trailing // comments because the repo allows no comment lines, and I resolved a conflict in mimg.rs against current main.

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No description provided.