Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
8 changes: 8 additions & 0 deletions docs/experiments.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
100 changes: 100 additions & 0 deletions tests/startup_gfx2hlsl_buffer_format_check.py
Original file line number Diff line number Diff line change
@@ -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('<II', w0, w1)


class Pipeline:
def __init__(self):
self.resources, self.notes, self.modified = {}, set(), set()

def resource(self, key, kind):
entry = self.resources.setdefault(key, dict(index=len(self.resources), kind=kind))
return 'r%d' % entry['index']

def ud(self, source):
return 0


class Stage(G.Stage):
def __init__(self):
self.lines, self.sym = [], {}
self.used_s, self.used_v, self.uniform_v = set(), set(), set()
self.exec_known = True
self.exports = {}
self.pipe = Pipeline()

def descriptor(self, tok):
return ('buffer', tok)


def translate(code):
stage = Stage()
decoded = G.decode(code)
for _o, _s, name, asm in decoded:
stage.one(name, asm)
return [a for _o, _s, _n, a in decoded], stage.lines


# 1. Every width is refused by its own name and says why; nothing is emitted.
for suffix, op in MUBUF_LOAD_FORMAT.items():
code = encode_mubuf(op)
try:
asm, lines = translate(code)
check('refuses buffer_load_format_' + suffix, False, 'translated: %r' % lines)
except G.Unsupported as e:
text = str(e)
check('refuses buffer_load_format_' + suffix, text.startswith('buffer_load_format_%s ' % suffix), text)
check('refusal names DATA_FORMAT for ' + suffix, 'DATA_FORMAT' in text, text)

# 2. The census form decodes as written in the issue.
asm, _ = (lambda d: ([a for _o, _s, _n, a in d], None))(G.decode(encode_mubuf(MUBUF_LOAD_FORMAT['xyz'])))
check('census form decodes', asm == ['buffer_load_format_xyz v[6:8], off, s[32:35], 0'], repr(asm))

# 3. Plain dword loads are unaffected: they read raw dwords and need no format.
asm, lines = translate(encode_mubuf(MUBUF_LOAD_DWORD))
check('buffer_load_dword still translates', any('r0.Load(a' in l for l in lines), repr(lines))

if failures:
print('FAIL')
for t in failures:
print(' ' + t)
sys.exit(1)
print('PASS')
21 changes: 21 additions & 0 deletions tools/startup/gfx2hlsl.py
Original file line number Diff line number Diff line change
Expand Up @@ -1044,6 +1044,27 @@ def op_s_swappc_b64(self, ops, mods):
self.load_format(FETCH_DXGI[e['format']][0], buf, e['vgpr'], e['count'])
self.fetched = True

def buffer_format_load(self, ops, mods, count):
# Untyped-format MUBUF loads carry no format in the instruction: the element format is the
# DATA_FORMAT field of the resource descriptor, read at run time. descriptor() only tracks where the
# descriptor words came from, not their contents, so the format is unknown here. Guessing one from the
# destination width would load silently wrong data, so the form is refused by name until the pipeline
# supplies descriptor words (issue #1809).
raise Unsupported('buffer_load_format_%s needs the descriptor DATA_FORMAT, which is not tracked'
% 'xyzw'[:count])

def op_buffer_load_format_x(self, ops, mods):
self.buffer_format_load(ops, mods, 1)

def op_buffer_load_format_xy(self, ops, mods):
self.buffer_format_load(ops, mods, 2)

def op_buffer_load_format_xyz(self, ops, mods):
self.buffer_format_load(ops, mods, 3)

def op_buffer_load_format_xyzw(self, ops, mods):
self.buffer_format_load(ops, mods, 4)

def op_tbuffer_load_format_x(self, ops, mods):
self.tbuffer(ops, mods, 1)

Expand Down
Loading