startup: lower combined idxen+offen and SGPR soffset buffer loads in gfx2hlsl #1806
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/1813"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
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:When both
idxenandoffenare enabled on the same instruction, the hardware packs both vector inputs into a consecutive two-VGPR pairv[n:n+1]:v[n]holds the element index (vindex, multiplied by the buffer stride), andv[n+1]holds an additional per-lane byte offset (voffset).What was broken in
Stage.buffer_address()Previously,
Stage.buffer_address()intools/startup/gfx2hlsl.pyhad four issues:voffsetdrop whenidxenandoffenwere used together:Because the code used
if 'idxen' in mods: ... elif 'offen' in mods:, enablingidxenskipped theoffenbranch completely.self.u('v[2:3]')resolved only the first register (v2) and silently discardedv3, reading from the wrong buffer address without raising an error.ValueErroron SGPR or non-literalsoffset:offset = int(ops[3], 0)assumedsoffsetwas always an integer literal. Whensoffsetnamed 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 rawValueErrorinstead of either translating the SGPR or raisingUnsupported(stopping batch translation, which only catchesUnsupported).IndexErroronldsbuffer loads:In
disasm_rdna2, MUBUF loads with theldsflag write directly to LDS and omit thevdatadestination operand (len(ops) == 3), causingops[3]to crash withIndexError.tfedrop:The
tfe(texel fail enable) modifier widensvdataby an extra residency-status VGPR thatgfx2hlsldoes not model, and was silently ignored.What this PR changes
buffer_load_dwordx4 v[4:7], v[2:3], s[16:19], 0 idxen offen offset:12a = v2 * ud4 + 12;(silently droppedv3)a = v2 * ud4 + v3 + 12;buffer_load_dwordx4 v[4:7], v[2:3], s[16:19], s8 idxen offen offset:12ValueErrorcrash on's8'a = v2 * ud4 + v3 + ud1 + 12;buffer_load_dword v4, v2, s[16:19], 0 offen offset:8 glc slc dlca = v2 + 8;a = v2 + 8;(cache flags still accepted)buffer_load_dword v2, s[16:19], 0 offen offset:8 ldsUnsupported/IndexErrorcrashUnsupported('buffer modifier lds')buffer_load_dword v4, v2, s[16:19], 0 offen offset:8 tfea = v2 + 8;(silently droppedtfe)Unsupported('buffer modifier tfe')buffer_load_dword v4, v[255:256], s[16:19], 0 idxen offena = v255 * ud4 + 0;(ignored out-of-rangev256)Unsupported('buffer vaddr v[255:256] for idxen offen')idxenalone,offenalone, or fixed offset with an unsigned literalsoffset) stays character-for-character identical.Verification
Tested at
1ce038f8(rebased ontomainatecc6e6ce) on Windows 11 26H2 (build26300.9974), MSVC19.44.35230.0, Windows SDK10.0.26100.0(dxc.exe+fxc.exe), Python3.12.10, D3D12 debug layer enabled (debug_errors=0 debug_warnings=0):1414:008c), driver10.0.26100.98181002:7550), driver32.0.32029.2010Commands 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=0across all three 16-vertex readback cases on both WARP and RX 9070 XT, plusfxcvs_5_1/ps_5_1compilation).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.pyconstructs all MUBUF, MTBUF, VOP1 and VOP2 instruction bytes synthetically from the RDNA 2 /tools/disasm_rdna2.pybit layouts under the MIT License (no external or title-derived bytes):startup_buffer_address_controls):idxen,offen, combinedidxen + offen(on both MUBUF and MTBUF), SGPRsoffset(root constants8, folded constants5, runtimes105), cache-policy flags (glc,slc,dlc), and thev[254:255]vs.v[255:256]VGPR boundary.Unsupportedrefusals forlds,tfe, unknown modifiers, modified descriptors onidxen + offen, mismatchedvaddrshapes, and non-SGPR/non-uintsoffsetencodings (vcc_lo,m0,null,-1,-16,0.5,0.15915494, reserved code209, and descriptor-holding SGPRs16).v3onidxen + offenand raisesValueErroron's8'.startup_buffer_address_{warp,hardware}):buffer_load_dwordx4withidxen + offen + s8 + offset:12, unalignedtbuffer_load_format_xyzw8_8_8_8_UNORMwithidxen + offen + s8 + offset:5throughldu(), andbuffer_load_dwordx4withoffen + s8 + offset:8), compiles them with bothfxc(vs_5_1/ps_5_1) anddxc(vs_6_0/cs_6_0), and compares all 320 readback dwords against an independent byte reference.voffsetorsoffset) fail at their exact first differing word (word 4andword 0), and 4 translation mutations (droppingvoffset—the pre-fix bug—swappingvindexandvoffset, dropping scalarsoffset, and zeroing immediateoffset) fail D3D12 readback.Documented in
docs/experiments.mdunder Startup vertex fetch from an unsigned byte → Combinedidxen+offen, scalarsoffsetand MUBUF/MTBUF modifier checks.Checklist
AI-assisted.
Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-24T19:18:11Z.
Review: COMMENTED
Review of
1ce038f8(head of this PR, baseecc6e6ce). 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 bothidxenandoffen,vaddris the pair (vindex, voffset) and the byte address is vindex * stride + voffset + soffset + inst_offset.What I checked in
tools/startup/gfx2hlsl.pyr[1]is the index andr[2]the byte offset, withr[2] - r[1] == 1andr[2] <= 255enforced. Correct.idxenalone andoffenalone take a single VGPR;offis only accepted when neither flag is set; the "indexed load through a modified descriptor" refusal is kept for bothidxenforms.soffset: a non-negative literal is added to the immediate offset,s0..s105goes throughself.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.glc/slc/dlcaccepted, any other modifier refused. Before this change an unknown modifier such astfewas silently ignored, so this is a deliberate behaviour change.tbuffer()stripsformat:before callingbuffer_address(), so MTBUF is not hit by it.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.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.gfx2hlsl.py, each rerun against the controls and then reverted (git statusclean afterwards). Every one makes the controls fail:a = v2 * ud2 + v3 + 12;instead of... + ud1 + 12;.a = v3 * ud2 + v2 + 12;.ldscase no longer reportsbuffer modifier lds.Not run by me: the WARP and hardware readback cases (
startup_buffer_address_{warp,hardware}) need Windows with DXC andoffscreen_check. I rely on the results in the PR text for those.Notes (P3, not blocking)
'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.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.