startup: lower combined idxen+offen and SGPR soffset buffer loads in gfx2hlsl #1806

Merged
consolelogkyo merged 1 commit from upstream-pr/1813 into main 2026-11-24 19:27:30 +00:00
consolelogkyo commented 2026-11-24 19:04:45 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by consolelogkyo on 2026-11-24T19:04:45Z.


Change

Background: how RDNA 2 buffer addressing works

In AMD RDNA 2 shaders, raw buffer loads (MUBUF, e.g. buffer_load_dword*) and typed buffer / vertex-fetch loads (MTBUF, e.g. tbuffer_load_format_*) compute their byte address from up to four parts:

byte_address = (vindex * stride)   [when idxen is set]
             + voffset             [when offen is set]
             + soffset             [scalar byte offset: inline integer or SGPR]
             + inst_offset         [12-bit immediate offset:<n> in the instruction]

When both idxen and offen are enabled on the same instruction, the hardware packs both vector inputs into a consecutive two-VGPR pair v[n:n+1]:

  • v[n] holds the element index (vindex, multiplied by the buffer stride), and
  • v[n+1] holds an additional per-lane byte offset (voffset).

What was broken in Stage.buffer_address()

Previously, Stage.buffer_address() in tools/startup/gfx2hlsl.py had four issues:

  1. Silent voffset drop when idxen and offen were used together:
    Because the code used if 'idxen' in mods: ... elif 'offen' in mods:, enabling idxen skipped the offen branch completely. self.u('v[2:3]') resolved only the first register (v2) and silently discarded v3, reading from the wrong buffer address without raising an error.
  2. Uncaught ValueError on SGPR or non-literal soffset:
    offset = int(ops[3], 0) assumed soffset was always an integer literal. When soffset named an architectural SGPR (s0..s105, such as a root constant or computed byte offset) or a special operand (null, m0, vcc_lo, inline float), int(..., 0) raised a raw ValueError instead of either translating the SGPR or raising Unsupported (stopping batch translation, which only catches Unsupported).
  3. Uncaught IndexError on lds buffer loads:
    In disasm_rdna2, MUBUF loads with the lds flag write directly to LDS and omit the vdata destination operand (len(ops) == 3), causing ops[3] to crash with IndexError.
  4. Silent tfe drop:
    The tfe (texel fail enable) modifier widens vdata by an extra residency-status VGPR that gfx2hlsl does not model, and was silently ignored.

What this PR changes

Input assembly pattern Before After
buffer_load_dwordx4 v[4:7], v[2:3], s[16:19], 0 idxen offen offset:12 a = v2 * ud4 + 12; (silently dropped v3) a = v2 * ud4 + v3 + 12;
buffer_load_dwordx4 v[4:7], v[2:3], s[16:19], s8 idxen offen offset:12 ValueError crash on 's8' a = v2 * ud4 + v3 + ud1 + 12;
buffer_load_dword v4, v2, s[16:19], 0 offen offset:8 glc slc dlc a = v2 + 8; a = v2 + 8; (cache flags still accepted)
buffer_load_dword v2, s[16:19], 0 offen offset:8 lds Unsupported / IndexError crash Unsupported('buffer modifier lds')
buffer_load_dword v4, v2, s[16:19], 0 offen offset:8 tfe a = v2 + 8; (silently dropped tfe) Unsupported('buffer modifier tfe')
buffer_load_dword v4, v[255:256], s[16:19], 0 idxen offen a = v255 * ud4 + 0; (ignored out-of-range v256) Unsupported('buffer vaddr v[255:256] for idxen offen')
  • Single-mode HLSL output (idxen alone, offen alone, or fixed offset with an unsigned literal soffset) stays character-for-character identical.
  • Advances startup shader translation correctness (#870, #1149) and does not overlap any open PR.

Verification

Tested at 1ce038f8 (rebased onto main at ecc6e6ce) on Windows 11 26H2 (build 26300.9974), MSVC 19.44.35230.0, Windows SDK 10.0.26100.0 (dxc.exe + fxc.exe), Python 3.12.10, D3D12 debug layer enabled (debug_errors=0 debug_warnings=0):

  • WARP: Microsoft Basic Render Driver (1414:008c), driver 10.0.26100.9818
  • Hardware: AMD Radeon RX 9070 XT (1002:7550), driver 32.0.32029.2010

Commands and observed results:

  • ctest --preset dev --no-tests=error -j 1 -R '^startup_buffer_address_(controls|warp|hardware)$' -V -> 3/3 passed (0 skips, mismatches=0 across all three 16-vertex readback cases on both WARP and RX 9070 XT, plus fxc vs_5_1/ps_5_1 compilation).
  • ctest --preset dev --no-tests=error -j 1 -R '^(startup_r8_uint|startup_vertex_fetch_formats)_(warp|hardware)$' -> 4/4 passed.
  • ctest --preset pr --no-tests=error -> 26/26 passed (with pinned SPIR-V tools enabled).
  • ./out/pe-env/Scripts/python.exe -B tests/docs_links_check.py -> passed (17 Markdown files, 368 links).
  • ./out/pe-env/Scripts/python.exe -B tests/bidi_controls_check.py -> passed (1,312 tracked files).
  • ctest --preset dev --no-tests=error -R '^startup_tools_lint$' -> 1/1 passed.

Evidence and follow-up

tests/startup_buffer_address_check.py constructs all MUBUF, MTBUF, VOP1 and VOP2 instruction bytes synthetically from the RDNA 2 / tools/disasm_rdna2.py bit layouts under the MIT License (no external or title-derived bytes):

  1. Device-free controls (startup_buffer_address_controls):
    • Verifies exact HLSL output for fixed, idxen, offen, combined idxen + offen (on both MUBUF and MTBUF), SGPR soffset (root constant s8, folded constant s5, runtime s105), cache-policy flags (glc, slc, dlc), and the v[254:255] vs. v[255:256] VGPR boundary.
    • Verifies Unsupported refusals for lds, tfe, unknown modifiers, modified descriptors on idxen + offen, mismatched vaddr shapes, and non-SGPR/non-uint soffset encodings (vcc_lo, m0, null, -1, -16, 0.5, 0.15915494, reserved code 209, and descriptor-holding SGPR s16).
    • Includes a direct negative control confirming that the pre-fix implementation drops v3 on idxen + offen and raises ValueError on 's8'.
  2. D3D12 GPU readback (startup_buffer_address_{warp,hardware}):
    • Translates three 16-vertex shaders (buffer_load_dwordx4 with idxen + offen + s8 + offset:12, unaligned tbuffer_load_format_xyzw 8_8_8_8_UNORM with idxen + offen + s8 + offset:5 through ldu(), and buffer_load_dwordx4 with offen + s8 + offset:8), compiles them with both fxc (vs_5_1/ps_5_1) and dxc (vs_6_0/cs_6_0), and compares all 320 readback dwords against an independent byte reference.
    • Verifies that 4 wrong references (omitting voffset or soffset) fail at their exact first differing word (word 4 and word 0), and 4 translation mutations (dropping voffset—the pre-fix bug—swapping vindex and voffset, dropping scalar soffset, and zeroing immediate offset) fail D3D12 readback.

Documented in docs/experiments.md under Startup vertex fetch from an unsigned byte → Combined idxen + offen, scalar soffset and MUBUF/MTBUF modifier checks.

Checklist

  • This pull request covers one reviewable behavior.
  • I included the exact command and result for every check I ran.
  • I considered a negative control or failure case for a new fixture.
  • I updated README/docs when a capability or build requirement changed.
  • I did not include game files, dumps, distribution links or secrets.

AI-assisted.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6589286687 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1813). Originally posted by [consolelogkyo](https://github.com/consolelogkyo) on 2026-11-24T19:04:45Z. --- ## Change ### Background: how RDNA 2 buffer addressing works In AMD RDNA 2 shaders, raw buffer loads (`MUBUF`, e.g. `buffer_load_dword*`) and typed buffer / vertex-fetch loads (`MTBUF`, e.g. `tbuffer_load_format_*`) compute their byte address from up to four parts: ```text byte_address = (vindex * stride) [when idxen is set] + voffset [when offen is set] + soffset [scalar byte offset: inline integer or SGPR] + inst_offset [12-bit immediate offset:<n> in the instruction] ``` When **both** `idxen` and `offen` are enabled on the same instruction, the hardware packs both vector inputs into a consecutive two-VGPR pair `v[n:n+1]`: - `v[n]` holds the element index (`vindex`, multiplied by the buffer stride), and - `v[n+1]` holds an additional per-lane byte offset (`voffset`). ### What was broken in `Stage.buffer_address()` Previously, `Stage.buffer_address()` in `tools/startup/gfx2hlsl.py` had four issues: 1. **Silent `voffset` drop when `idxen` and `offen` were used together:** Because the code used `if 'idxen' in mods: ... elif 'offen' in mods:`, enabling `idxen` skipped the `offen` branch completely. `self.u('v[2:3]')` resolved only the first register (`v2`) and **silently discarded `v3`**, reading from the wrong buffer address without raising an error. 2. **Uncaught `ValueError` on SGPR or non-literal `soffset`:** `offset = int(ops[3], 0)` assumed `soffset` was always an integer literal. When `soffset` named an architectural SGPR (`s0`..`s105`, such as a root constant or computed byte offset) or a special operand (`null`, `m0`, `vcc_lo`, inline float), `int(..., 0)` raised a raw `ValueError` instead of either translating the SGPR or raising `Unsupported` (stopping batch translation, which only catches `Unsupported`). 3. **Uncaught `IndexError` on `lds` buffer loads:** In `disasm_rdna2`, MUBUF loads with the `lds` flag write directly to LDS and omit the `vdata` destination operand (`len(ops) == 3`), causing `ops[3]` to crash with `IndexError`. 4. **Silent `tfe` drop:** The `tfe` (texel fail enable) modifier widens `vdata` by an extra residency-status VGPR that `gfx2hlsl` does not model, and was silently ignored. ### What this PR changes | Input assembly pattern | Before | After | |---|---|---| | `buffer_load_dwordx4 v[4:7], v[2:3], s[16:19], 0 idxen offen offset:12` | `a = v2 * ud4 + 12;` *(silently dropped `v3`)* | `a = v2 * ud4 + v3 + 12;` | | `buffer_load_dwordx4 v[4:7], v[2:3], s[16:19], s8 idxen offen offset:12` | `ValueError` crash on `'s8'` | `a = v2 * ud4 + v3 + ud1 + 12;` | | `buffer_load_dword v4, v2, s[16:19], 0 offen offset:8 glc slc dlc` | `a = v2 + 8;` | `a = v2 + 8;` *(cache flags still accepted)* | | `buffer_load_dword v2, s[16:19], 0 offen offset:8 lds` | `Unsupported` / `IndexError` crash | `Unsupported('buffer modifier lds')` | | `buffer_load_dword v4, v2, s[16:19], 0 offen offset:8 tfe` | `a = v2 + 8;` *(silently dropped `tfe`)* | `Unsupported('buffer modifier tfe')` | | `buffer_load_dword v4, v[255:256], s[16:19], 0 idxen offen` | `a = v255 * ud4 + 0;` *(ignored out-of-range `v256`)* | `Unsupported('buffer vaddr v[255:256] for idxen offen')` | - Single-mode HLSL output (`idxen` alone, `offen` alone, or fixed offset with an unsigned literal `soffset`) stays character-for-character identical. - Advances startup shader translation correctness (#870, #1149) and does not overlap any open PR. ## Verification Tested at `1ce038f8` (rebased onto `main` at `ecc6e6ce`) on Windows 11 26H2 (build `26300.9974`), MSVC `19.44.35230.0`, Windows SDK `10.0.26100.0` (`dxc.exe` + `fxc.exe`), Python `3.12.10`, D3D12 debug layer enabled (`debug_errors=0 debug_warnings=0`): - **WARP:** Microsoft Basic Render Driver (`1414:008c`), driver `10.0.26100.9818` - **Hardware:** AMD Radeon RX 9070 XT (`1002:7550`), driver `32.0.32029.2010` Commands and observed results: - `ctest --preset dev --no-tests=error -j 1 -R '^startup_buffer_address_(controls|warp|hardware)$' -V` -> **3/3 passed** (0 skips, `mismatches=0` across all three 16-vertex readback cases on both WARP and RX 9070 XT, plus `fxc` `vs_5_1`/`ps_5_1` compilation). - `ctest --preset dev --no-tests=error -j 1 -R '^(startup_r8_uint|startup_vertex_fetch_formats)_(warp|hardware)$'` -> **4/4 passed**. - `ctest --preset pr --no-tests=error` -> **26/26 passed** (with pinned SPIR-V tools enabled). - `./out/pe-env/Scripts/python.exe -B tests/docs_links_check.py` -> **passed** (17 Markdown files, 368 links). - `./out/pe-env/Scripts/python.exe -B tests/bidi_controls_check.py` -> **passed** (1,312 tracked files). - `ctest --preset dev --no-tests=error -R '^startup_tools_lint$'` -> **1/1 passed**. ## Evidence and follow-up `tests/startup_buffer_address_check.py` constructs all MUBUF, MTBUF, VOP1 and VOP2 instruction bytes synthetically from the RDNA 2 / `tools/disasm_rdna2.py` bit layouts under the MIT License (no external or title-derived bytes): 1. **Device-free controls (`startup_buffer_address_controls`):** - Verifies exact HLSL output for fixed, `idxen`, `offen`, combined `idxen + offen` (on both MUBUF and MTBUF), SGPR `soffset` (root constant `s8`, folded constant `s5`, runtime `s105`), cache-policy flags (`glc`, `slc`, `dlc`), and the `v[254:255]` vs. `v[255:256]` VGPR boundary. - Verifies `Unsupported` refusals for `lds`, `tfe`, unknown modifiers, modified descriptors on `idxen + offen`, mismatched `vaddr` shapes, and non-SGPR/non-uint `soffset` encodings (`vcc_lo`, `m0`, `null`, `-1`, `-16`, `0.5`, `0.15915494`, reserved code `209`, and descriptor-holding SGPR `s16`). - Includes a direct negative control confirming that the pre-fix implementation drops `v3` on `idxen + offen` and raises `ValueError` on `'s8'`. 2. **D3D12 GPU readback (`startup_buffer_address_{warp,hardware}`):** - Translates three 16-vertex shaders (`buffer_load_dwordx4` with `idxen + offen + s8 + offset:12`, unaligned `tbuffer_load_format_xyzw` `8_8_8_8_UNORM` with `idxen + offen + s8 + offset:5` through `ldu()`, and `buffer_load_dwordx4` with `offen + s8 + offset:8`), compiles them with both `fxc` (`vs_5_1`/`ps_5_1`) and `dxc` (`vs_6_0`/`cs_6_0`), and compares all 320 readback dwords against an independent byte reference. - Verifies that **4 wrong references** (omitting `voffset` or `soffset`) fail at their exact first differing word (`word 4` and `word 0`), and **4 translation mutations** (dropping `voffset`—the pre-fix bug—swapping `vindex` and `voffset`, dropping scalar `soffset`, and zeroing immediate `offset`) fail D3D12 readback. Documented in `docs/experiments.md` under *Startup vertex fetch from an unsigned byte → Combined `idxen` + `offen`, scalar `soffset` and MUBUF/MTBUF modifier checks*. ## Checklist - [x] This pull request covers one reviewable behavior. - [x] I included the exact command and result for every check I ran. - [x] I considered a negative control or failure case for a new fixture. - [x] I updated README/docs when a capability or build requirement changed. - [x] I did not include game files, dumps, distribution links or secrets. AI-assisted.
upstream-sync closed this pull request 2026-11-24 20:00:44 +00:00
tedin7 commented 2026-11-24 20:00:44 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-24T19:18:11Z.

Review: COMMENTED


Review of 1ce038f8 (head of this PR, base ecc6e6ce). Comment only, no approval.

Verdict: no blocking findings. The change in Stage.buffer_address() matches the RDNA 2 MUBUF/MTBUF address rule as I read it: with both idxen and offen, vaddr is the pair (vindex, voffset) and the byte address is vindex * stride + voffset + soffset + inst_offset.

What I checked in tools/startup/gfx2hlsl.py

  • Pair order: r[1] is the index and r[2] the byte offset, with r[2] - r[1] == 1 and r[2] <= 255 enforced. Correct.
  • idxen alone and offen alone take a single VGPR; off is only accepted when neither flag is set; the "indexed load through a modified descriptor" refusal is kept for both idxen forms.
  • soffset: a non-negative literal is added to the immediate offset, s0..s105 goes through self.S() (so a descriptor-holding SGPR still raises), anything else (negative inline constants, m0, null, vcc, ranges) is refused. Conservative, and consistent with the docs.
  • Modifiers: glc/slc/dlc accepted, any other modifier refused. Before this change an unknown modifier such as tfe was silently ignored, so this is a deliberate behaviour change. tbuffer() strips format: before calling buffer_address(), so MTBUF is not hit by it.
  • All terms are uint, so the sum wraps like the 32-bit hardware address; out-of-range reads are left to ByteAddressBuffer.Load.

Commands run locally (worktree at the exact head SHA above, Linux, Python 3)

  • python3 -B tests/startup_buffer_address_check.py controls: passed.
  • Neighbours: startup_vertex_fetch_formats_check.py controls, startup_gfx2hlsl_smem_offset_check.py, startup_inline_sampler_check.py: passed.
  • git diff --check origin/main...HEAD, tests/docs_links_check.py (17 files, 368 links), tests/bidi_controls_check.py: clean.
  • Three real mutations of gfx2hlsl.py, each rerun against the controls and then reverted (git status clean afterwards). Every one makes the controls fail:
    • soffset term dropped: a = v2 * ud2 + v3 + 12; instead of ... + ud1 + 12;.
    • vindex and voffset swapped: a = v3 * ud2 + v2 + 12;.
    • unknown-modifier refusal disabled: the lds case no longer reports buffer modifier lds.

Not run by me: the WARP and hardware readback cases (startup_buffer_address_{warp,hardware}) need Windows with DXC and offscreen_check. I rely on the results in the PR text for those.

Notes (P3, not blocking)

  • The control cases compare generated HLSL strings exactly (for example 'a = v2 * ud2 + v3 + ud1 + 12;'), so a harmless re-ordering of terms would fail them. The GPU readback cases already guard the behaviour; consider keeping the string checks to the refusals.
  • Uppercase hex in offset: or in the literal soffset is refused. It is safe, and I believe the disassembler prints lowercase.

AI-assisted; commands listed were executed locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6045767776 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1813#pullrequestreview-6045767776). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-24T19:18:11Z. **Review:** COMMENTED --- Review of `1ce038f8` (head of this PR, base `ecc6e6ce`). Comment only, no approval. **Verdict: no blocking findings.** The change in `Stage.buffer_address()` matches the RDNA 2 MUBUF/MTBUF address rule as I read it: with both `idxen` and `offen`, `vaddr` is the pair (vindex, voffset) and the byte address is vindex * stride + voffset + soffset + inst_offset. **What I checked in `tools/startup/gfx2hlsl.py`** - Pair order: `r[1]` is the index and `r[2]` the byte offset, with `r[2] - r[1] == 1` and `r[2] <= 255` enforced. Correct. - `idxen` alone and `offen` alone take a single VGPR; `off` is only accepted when neither flag is set; the "indexed load through a modified descriptor" refusal is kept for both `idxen` forms. - `soffset`: a non-negative literal is added to the immediate offset, `s0`..`s105` goes through `self.S()` (so a descriptor-holding SGPR still raises), anything else (negative inline constants, `m0`, `null`, `vcc`, ranges) is refused. Conservative, and consistent with the docs. - Modifiers: `glc`/`slc`/`dlc` accepted, any other modifier refused. Before this change an unknown modifier such as `tfe` was silently ignored, so this is a deliberate behaviour change. `tbuffer()` strips `format:` before calling `buffer_address()`, so MTBUF is not hit by it. - All terms are uint, so the sum wraps like the 32-bit hardware address; out-of-range reads are left to `ByteAddressBuffer.Load`. **Commands run locally** (worktree at the exact head SHA above, Linux, Python 3) - `python3 -B tests/startup_buffer_address_check.py controls`: passed. - Neighbours: `startup_vertex_fetch_formats_check.py controls`, `startup_gfx2hlsl_smem_offset_check.py`, `startup_inline_sampler_check.py`: passed. - `git diff --check origin/main...HEAD`, `tests/docs_links_check.py` (17 files, 368 links), `tests/bidi_controls_check.py`: clean. - Three real mutations of `gfx2hlsl.py`, each rerun against the controls and then reverted (`git status` clean afterwards). Every one makes the controls fail: - soffset term dropped: `a = v2 * ud2 + v3 + 12;` instead of `... + ud1 + 12;`. - vindex and voffset swapped: `a = v3 * ud2 + v2 + 12;`. - unknown-modifier refusal disabled: the `lds` case no longer reports `buffer modifier lds`. **Not run by me:** the WARP and hardware readback cases (`startup_buffer_address_{warp,hardware}`) need Windows with DXC and `offscreen_check`. I rely on the results in the PR text for those. **Notes (P3, not blocking)** - The control cases compare generated HLSL strings exactly (for example `'a = v2 * ud2 + v3 + ud1 + 12;'`), so a harmless re-ordering of terms would fail them. The GPU readback cases already guard the behaviour; consider keeping the string checks to the refusals. - Uppercase hex in `offset:` or in the literal soffset is refused. It is safe, and I believe the disassembler prints lowercase. AI-assisted; commands listed were executed locally.
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!1806
No description provided.