MTBUF d16: RDNA2-measured conversions replace the ISA-only lowering (resubmit of #41) #133

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

@⁠SchmashinPrunes on GitHub · 2026-11-22 14:30 UTC · original
SchmashinPrunes:mtbuf-d16-v2 → main · +1596 −157 · 16 files · 1 commits · closed without merging

Resubmission of #41 (closed by the history rewrite), rebuilt on c8a8a8f as asked: cherry-picked onto main's current MTBUF code, comment lines stripped, and the probe notes moved from README.md to tools/rdna2-hwprobe/README.txt. #72 (@⁠QuentinDanblon's VALU probe) stacks on this.

Library / area: tombox-xshader (RDNA2 shader translator), MTBUF tbuffer_{load,store}_format_d16_*, plus tools/rdna2-hwprobe.

Exports changed (exports.txt rows): none

Source of the behaviour: measurements on real RDNA2 hardware, the gfx1036 iGPU of a Ryzen 5 9600X under ROCm, using tools/rdna2-hwprobe (hand-written kernels, synthetic inputs; no game or console files). gfx1036 is RDNA2 but not the console's gfx1013. The public ISA text only says "convert to packed 16-bit"; it doesn't specify the rounding, the int narrowing or the unused register half, so those were measured. Packed layout per LLVM's AMDGPU backend (HasPackedD16VMem on GFX9+).

What changes compared with main

  • Lowers d16 UNORM/SNORM, 16-bit USCALED/SSCALED and 32-bit-component loads and stores, which main refuses.
  • Replaces main's d16 conversions with the measured ones (below).
  • Partial stores: main's 32-bit fix from #42 (b72625c, whole element, missing components 0) is kept as is; d16 stores follow the same rule, which is what the hardware does for them too.

Measured semantics (77 formats × 65536 elements, every 8/16-bit value)

RDNA2 behaviour
layout Packed: c0/c1 in vdata lo/hi, c2/c3 in vdata+1. _x / _xyz keep the upper half of their last VGPR
unorm / snorm / u- / sscaled → fp16 Exact value rounded once to nearest-even, saturating at ±65504. Not via fp32: unorm16 65519 gives 0x3bff, fp32-then-fp16 would give 0x3c00
32-bit float → fp16 Round toward zero. Finite overflow → ±65504, inf stays inf
NaN → fp16 sign|0x7c00|((mant>>13) or 1). Payload truncated, not quieted
32-bit int → 16 Clamped, never truncated
store: fp16 → unorm / snorm unorm rounds to nearest-even; snorm rounds halves away from zero (2-bit field: ±0.5 → ±1). NaN → 0
store: int 16-bit input zero-extended (uint) / sign-extended (sint), then clamped
store: fewer components than the format Writes the whole element, missing components 0

A missing w on d16 loads reads 1 (0x3c00 / 1), matching the 32-bit loads; on hardware that 1 comes from the descriptor's dst_sel (the probe used an identity swizzle, so it saw 0). Scaled and 11/10-bit-float stores follow no simple rule and stay unlowered, like the 32-bit forms.

Translator

src/hlsl/mtbuf.rs: loads reuse the existing fetch and field extraction, then narrow each component with D16_HELPERS; stores unpack the halves and pack them per format. All conversions are integer-only (exact rounding in uint64_t, fp16 → unorm/snorm as (M·max) >> sh), because a float/double version was wrong on WARP twice (DXC folded x != x, and a double product was off by one). hlsl.rs gains a uses_d16 flag that emits the helpers.

Tests (tests/exec_mem/mtbuf.rs)

  • Reference model, written independently of the HLSL (fp16 rounding by search over exact fp16 values). It matched all recorded hardware results (5.0M load + 14.4M store elements). mtbuf_d16_model_matches_rdna2 pins it to 571 recorded vectors in mtbuf_hw.rs (regenerated by tools/rdna2-hwprobe/vectors.py), weighted to the cases where naive rules differ.
  • mtbuf_d16_load_formats (539 cases, each rerun with runtime inputs; 64 threads, all four ops over all 77 formats, VGPRs preset to sentinels) and mtbuf_d16_store_formats (330 cases, output pre-filled with 0xAA) compare the translator with the model on WARP.
  • mtbuf_d16_load_addressing (308) / mtbuf_d16_store_addressing (660): immediate, SGPR and unaligned addressing, adapted from main's earlier d16 tests (thanks @⁠QuentinDanblon).
  • mtbuf_refuses_unlowered: main's list minus the now-lowered d16 rows (8, 75) and (12, 9), plus d16 scaled / 11-bit float stores and an invalid d16 format.
  • Mutation check (from the first submission): ties up instead of even, f32 rounded to nearest, u32 truncated, _x zeroing the upper half, and snorm ties to even each make the tests fail.

Results (Windows 11 Pro, WARP, DXC 1.9.2609)

Full cargo test -p tombox-xshader --release on aa14bfa, then --test exec again on d585b94; rebased onto c8a8a8f without conflicts.

cargo check --locked -p tombox-xshader --all-targets      ok
mtbuf_d16_load: 4 mnemonics verified, 0 failing, 539 cases, 539 rerun with runtime inputs
mtbuf_d16_load_addr: 4 mnemonics verified, 0 failing, 308 cases, 308 rerun with runtime inputs
mtbuf_d16_store: 4 mnemonics verified, 0 failing, 330 cases
mtbuf_d16_store_addr: 4 mnemonics verified, 0 failing, 660 cases, 660 rerun with runtime inputs
mtbuf_load / mtbuf_store / mtbuf_partial_rdna2 / mtbuf_snorm2_ties: unchanged, all pass
isa_sweep on WARP: 541 mnemonics, 530 pass, 0 fail, 11 known WARP deviations, 28 skipped

Every target passes apart from main's own leonida_wave64_scalar_masks, which failed the same way on plain aa14bfa / d585b94 (the exec lane-bit change that bd880fc has since reverted).

Probe (tools/rdna2-hwprobe, Linux + ROCm)

run.sh <outdir> builds runner.cpp and the kernels from gen.py, runs them on the GPU and checks both models (model.py, analyze_stores.py); analyze_loads.py, nan_rule.py and vectors.py analyse a run and regenerate mtbuf_hw.rs. README.txt has the requirements, the input design and the full results. The scripts lost their comments for the gate; their outputs are unchanged (gen.py and partial_store32.py produce byte-identical kernels and inputs, and every script parses to the same AST as before apart from vectors.py, which no longer prints comment lines into mtbuf_hw.rs).

  • cargo test -p tombox-xshader passes
  • No game or console files and no binaries included (the probe generates its kernels and synthetic inputs)
  • No fake success; scaled / 11-10-bit float d16 stores stay unlowered with an explicit error

🤖 Generated with Claude Code

**@⁠SchmashinPrunes** on GitHub · 2026-11-22 14:30 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/133) `SchmashinPrunes:mtbuf-d16-v2` → `main` · +1596 −157 · 16 files · 1 commits · closed without merging Resubmission of #41 (closed by the history rewrite), rebuilt on c8a8a8f as asked: cherry-picked onto main's current MTBUF code, comment lines stripped, and the probe notes moved from `README.md` to `tools/rdna2-hwprobe/README.txt`. #72 (@⁠QuentinDanblon's VALU probe) stacks on this. **Library / area:** tombox-xshader (RDNA2 shader translator), MTBUF `tbuffer_{load,store}_format_d16_*`, plus `tools/rdna2-hwprobe`. **Exports changed (exports.txt rows):** none **Source of the behaviour:** measurements on real RDNA2 hardware, the gfx1036 iGPU of a Ryzen 5 9600X under ROCm, using `tools/rdna2-hwprobe` (hand-written kernels, synthetic inputs; no game or console files). gfx1036 is RDNA2 but not the console's gfx1013. The public ISA text only says "convert to packed 16-bit"; it doesn't specify the rounding, the int narrowing or the unused register half, so those were measured. Packed layout per LLVM's AMDGPU backend (`HasPackedD16VMem` on GFX9+). ### What changes compared with main - Lowers d16 UNORM/SNORM, 16-bit USCALED/SSCALED and 32-bit-component loads and stores, which main refuses. - Replaces main's d16 conversions with the measured ones (below). - Partial stores: main's 32-bit fix from #42 (b72625c, whole element, missing components 0) is kept as is; d16 stores follow the same rule, which is what the hardware does for them too. ### Measured semantics (77 formats × 65536 elements, every 8/16-bit value) | | RDNA2 behaviour | |---|---| | layout | Packed: c0/c1 in `vdata` lo/hi, c2/c3 in `vdata+1`. `_x` / `_xyz` **keep the upper half** of their last VGPR | | unorm / snorm / u- / sscaled → fp16 | Exact value rounded **once** to nearest-even, saturating at ±65504. Not via fp32: unorm16 65519 gives `0x3bff`, fp32-then-fp16 would give `0x3c00` | | 32-bit float → fp16 | **Round toward zero**. Finite overflow → ±65504, inf stays inf | | NaN → fp16 | `sign\|0x7c00\|((mant>>13) or 1)`. Payload truncated, **not quieted** | | 32-bit int → 16 | **Clamped**, never truncated | | store: fp16 → unorm / snorm | unorm rounds to nearest-even; snorm rounds **halves away from zero** (2-bit field: ±0.5 → ±1). NaN → 0 | | store: int | 16-bit input zero-extended (uint) / sign-extended (sint), then clamped | | store: fewer components than the format | Writes the **whole element**, missing components 0 | A missing w on d16 loads reads 1 (0x3c00 / 1), matching the 32-bit loads; on hardware that 1 comes from the descriptor's dst_sel (the probe used an identity swizzle, so it saw 0). Scaled and 11/10-bit-float **stores** follow no simple rule and stay unlowered, like the 32-bit forms. ### Translator `src/hlsl/mtbuf.rs`: loads reuse the existing fetch and field extraction, then narrow each component with `D16_HELPERS`; stores unpack the halves and pack them per format. All conversions are integer-only (exact rounding in `uint64_t`, fp16 → unorm/snorm as `(M·max) >> sh`), because a float/double version was wrong on WARP twice (DXC folded `x != x`, and a double product was off by one). `hlsl.rs` gains a `uses_d16` flag that emits the helpers. ### Tests (`tests/exec_mem/mtbuf.rs`) - **Reference model**, written independently of the HLSL (fp16 rounding by search over exact fp16 values). It matched **all** recorded hardware results (5.0M load + 14.4M store elements). `mtbuf_d16_model_matches_rdna2` pins it to 571 recorded vectors in `mtbuf_hw.rs` (regenerated by `tools/rdna2-hwprobe/vectors.py`), weighted to the cases where naive rules differ. - **`mtbuf_d16_load_formats`** (539 cases, each rerun with runtime inputs; 64 threads, all four ops over all 77 formats, VGPRs preset to sentinels) and **`mtbuf_d16_store_formats`** (330 cases, output pre-filled with 0xAA) compare the translator with the model on WARP. - **`mtbuf_d16_load_addressing`** (308) / **`mtbuf_d16_store_addressing`** (660): immediate, SGPR and unaligned addressing, adapted from main's earlier d16 tests (thanks @⁠QuentinDanblon). - **`mtbuf_refuses_unlowered`**: main's list minus the now-lowered d16 rows `(8, 75)` and `(12, 9)`, plus d16 scaled / 11-bit float stores and an invalid d16 format. - Mutation check (from the first submission): ties up instead of even, f32 rounded to nearest, u32 truncated, `_x` zeroing the upper half, and snorm ties to even each make the tests fail. ### Results (Windows 11 Pro, WARP, DXC 1.9.2609) Full `cargo test -p tombox-xshader --release` on aa14bfa, then `--test exec` again on d585b94; rebased onto c8a8a8f without conflicts. ``` cargo check --locked -p tombox-xshader --all-targets ok mtbuf_d16_load: 4 mnemonics verified, 0 failing, 539 cases, 539 rerun with runtime inputs mtbuf_d16_load_addr: 4 mnemonics verified, 0 failing, 308 cases, 308 rerun with runtime inputs mtbuf_d16_store: 4 mnemonics verified, 0 failing, 330 cases mtbuf_d16_store_addr: 4 mnemonics verified, 0 failing, 660 cases, 660 rerun with runtime inputs mtbuf_load / mtbuf_store / mtbuf_partial_rdna2 / mtbuf_snorm2_ties: unchanged, all pass isa_sweep on WARP: 541 mnemonics, 530 pass, 0 fail, 11 known WARP deviations, 28 skipped ``` Every target passes apart from main's own `leonida_wave64_scalar_masks`, which failed the same way on plain aa14bfa / d585b94 (the exec lane-bit change that bd880fc has since reverted). ### Probe (`tools/rdna2-hwprobe`, Linux + ROCm) `run.sh <outdir>` builds `runner.cpp` and the kernels from `gen.py`, runs them on the GPU and checks both models (`model.py`, `analyze_stores.py`); `analyze_loads.py`, `nan_rule.py` and `vectors.py` analyse a run and regenerate `mtbuf_hw.rs`. `README.txt` has the requirements, the input design and the full results. The scripts lost their comments for the gate; their outputs are unchanged (`gen.py` and `partial_store32.py` produce byte-identical kernels and inputs, and every script parses to the same AST as before apart from `vectors.py`, which no longer prints comment lines into `mtbuf_hw.rs`). - [x] `cargo test -p tombox-xshader` passes - [x] No game or console files and no binaries included (the probe generates its kernels and synthetic inputs) - [x] No fake success; scaled / 11-10-bit float d16 stores stay unlowered with an explicit error 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Replaces the d16 tbuffer lowering with conversion rules measured on RDNA2
hardware (gfx1036 via ROCm, tools/rdna2-hwprobe):

- loads: UNORM/SNORM/USCALED/SSCALED round once to nearest-even fp16,
  saturating at +-65504; 32-bit float rounds toward zero (overflow ->
  +-65504, inf kept, NaN payload kept); 32-bit ints clamp. These were
  refused before.
- stores: fp16 widened exactly (NaN payload kept), unorm rounds to
  nearest-even, snorm rounds ties away from zero, ints extend then clamp;
  the whole element is written with missing components 0, as main's
  32-bit stores now do.

mtbuf_d16_model_matches_rdna2 checks the Rust reference model against
the recorded hardware vectors (mtbuf_hw.rs); the WARP tests compare the
translator with that model. mtbuf_d16_{load,store}_addressing cover
immediate, SGPR and unaligned addressing. Probe notes are in
tools/rdna2-hwprobe/README.txt.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Xsrj8HzdWhzzYYavNNpHjk
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 06:11:15 +00:00
github-bot closed this pull request 2026-11-26 06:11:16 +00:00
Author
Owner

@⁠omonkeyz on GitHub · 2026-11-22 14:43 UTC · original

Applied in a415541 (merged in 9ab6f59) with your authorship kept, thanks! Translator lib 69/0 and the WARP mtbuf exec cases 10/0 pass here. #72 can now be resubmitted on top of this.

**@⁠omonkeyz** on GitHub · 2026-11-22 14:43 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/133#issuecomment-7080757748) Applied in a415541 (merged in 9ab6f59) with your authorship kept, thanks! Translator lib 69/0 and the WARP mtbuf exec cases 10/0 pass here. #72 can now be resubmitted on top of this.

Pull request closed

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