From e600cdd5c23c971763337e043dadda0b3347f9d7 Mon Sep 17 00:00:00 2001 From: tedin7 Date: Tue, 24 Nov 2026 23:11:26 +0100 Subject: [PATCH] fix: refuse buffer_load_format_* by name until descriptor DATA_FORMAT is known The untyped-format MUBUF loads take their element format from the resource descriptor, which the translator does not track. Refuse each width with that reason instead of the generic unknown-op message. Part of #1809, #870. --- CMakeLists.txt | 3 + docs/experiments.md | 8 ++ tests/startup_gfx2hlsl_buffer_format_check.py | 100 ++++++++++++++++++ tools/startup/gfx2hlsl.py | 21 ++++ 4 files changed, 132 insertions(+) create mode 100644 tests/startup_gfx2hlsl_buffer_format_check.py diff --git a/CMakeLists.txt b/CMakeLists.txt index bf81c0a05f..faab372da3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2100,6 +2100,9 @@ if(EXISTS "${INTAKE_PYTHON}") # A typed buffer load of BUF_FMT_16_16_16_16_FLOAT reads four halves from one 8-byte element (#1695). add_test(NAME startup_gfx2hlsl_tbuffer_16f COMMAND "${INTAKE_PYTHON}" -B "${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_gfx2hlsl_tbuffer_16f_check.py") + # Untyped-format buffer loads need the descriptor DATA_FORMAT, which is not tracked: refused by name (#1809). + add_test(NAME startup_gfx2hlsl_buffer_format COMMAND "${INTAKE_PYTHON}" -B + "${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_gfx2hlsl_buffer_format_check.py") # 32-bit scalar complement logic: s_nand_b32, s_nor_b32 and s_xnor_b32 against an integer reference (#1919). add_test(NAME startup_gfx2hlsl_sop2_complement COMMAND "${INTAKE_PYTHON}" -B "${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_gfx2hlsl_sop2_complement_check.py") diff --git a/docs/experiments.md b/docs/experiments.md index f3bb185135..8c0c6ba73a 100644 --- a/docs/experiments.md +++ b/docs/experiments.md @@ -6088,6 +6088,14 @@ alignment. An aligned-down second dword must change the fourth output. Unnamed f `d16` forms remain refused. Run `ctest --preset dev --no-tests=error -R '^startup_(gfx2hlsl_tbuffer_16f$|vertex_fetch_formats_)' -V`. +**Untyped-format buffer loads.** `buffer_load_format_{x,xy,xyz,xyzw}` (MUBUF) carry no format in the +instruction, unlike `tbuffer_load_format_*`: the element format is the `DATA_FORMAT` of the resource +descriptor, read at run time. The translator tracks only where descriptor words came from, never their +contents, so the format is unknown (census form `buffer_load_format_xyz v[6:8], off, s[32:35], 0`, issue #1809, +#870). Picking one from the destination width would load wrong data without any error, so each form is refused +by name with that reason. Lowering needs pipeline state that supplies the descriptor words. Run +`ctest --preset dev --no-tests=error -R '^startup_gfx2hlsl_buffer_format$' -V`. + **The 64-bit vector shifts.** `v_lshlrev_b64`, `v_lshrrev_b64` and `v_ashrrev_i64` had no lowering - the census form `v_lshrrev_b64 v[10:11], 0, 0` was one of the refused shaders in the loading-screen cache (#870, issue #1817). A 64-bit value here is two 32-bit words, so the count must never reach 32 in a 32-bit shift and diff --git a/tests/startup_gfx2hlsl_buffer_format_check.py b/tests/startup_gfx2hlsl_buffer_format_check.py new file mode 100644 index 0000000000..c22df9393c --- /dev/null +++ b/tests/startup_gfx2hlsl_buffer_format_check.py @@ -0,0 +1,100 @@ +"""Untyped-format buffer loads in tools/startup/gfx2hlsl.py (issue #1809). + +usage: startup_gfx2hlsl_buffer_format_check.py + +`buffer_load_format_*` (MUBUF) has no format field: the element format is the DATA_FORMAT of the resource +descriptor, which the translator does not track (`Stage.descriptor()` records only where the words came from). +Choosing a format from the destination width would load silently wrong data, so each form must be refused by +name, with the reason, while `tbuffer_load_format_*` (format in the instruction) keeps translating. + +The instruction bytes are constructed here from the field layout the checked-in decoder reads +(`tools/disasm_rdna2.py`, the MUBUF branch); no game-derived bytes are used. +""" +import os +import struct +import sys + +here = os.path.dirname(os.path.abspath(__file__)) +sys.path.insert(0, os.path.join(here, '..', 'tools')) +sys.path.insert(0, os.path.join(here, '..', 'tools', 'startup')) +import gfx2hlsl as G # noqa: E402 + +failures = [] + + +def check(label, condition, detail=''): + print(f"{label}: {'ok' if condition else 'MISMATCH'}") + if not condition: + failures.append(label) + if detail: + print(' ' + detail) + + +# MUBUF: w0 [31:26]=0b111000, [25:18] opcode, [15:0] flags and offset; w1 as in MTBUF (soffset 128 = 0). +MUBUF_LOAD_FORMAT = {'x': 0, 'xy': 1, 'xyz': 2, 'xyzw': 3} +MUBUF_LOAD_DWORD = 12 + + +def encode_mubuf(op, *, vdata=6, srsrc=32, soffset=128): + w0 = (0b111000 << 26) | (op << 18) + w1 = ((vdata & 0xff) << 8) | (((srsrc // 4) & 0x1f) << 16) | ((soffset & 0xff) << 24) + return struct.pack('