Shaders: MUBUF sub-dword loads (buffer_load_ubyte, sbyte, ushort, sshort and the d16 forms) #1572

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

@⁠iurysf on GitHub · 2026-11-24 19:19 UTC · original
iurysf:mubuf-sub-dword-loads → main · +180 −2 · 2 files · 1 commits · closed without merging

What's wrong

Lower::buffer() refuses every MUBUF load narrower than a dword, so the program is dropped. One of the world's busiest computes, 28918b7150a7aad5 (575 to 848 dropped dispatches per frame in our RTX 4070 runs of the open world), reads packed 16-bit fields through a raw root UAV:

0x474 buffer_load_ushort v16, v14, s[0:3], 0 offen offset:24
0x47c buffer_load_ushort v17, v14, s[0:3], 0 offen offset:26
0x484 buffer_load_ushort v19, v15, s[0:3], 0 offen

With main + #489 + #1077 the program gets that far and stops there (#1077 lists buffer_load_ushort among what is left).

What the fix does

Source: AMD RDNA2 ISA, MUBUF loads
(https://www.amd.com/content/dam/amd/en/documents/radeon-tech-docs/instruction-set-architectures/rdna2-shader-instruction-set-architecture.pdf):
buffer_load_ubyte/sbyte/ushort/sshort zero- or sign-extend the field at the byte address to 32 bits; *_d16 / *_d16_hi write one half of the VGPR and keep the other.

  • Read the dword the address is in (off & ~3), shift it right by (off & 3) * 8.
  • A halfword at byte 3 of a dword takes its top byte from the next dword: that second read runs only for the lanes that cross, in a guarded block, so other lanes never touch memory past their field.
  • Extend with shl + lshr/ashr by 32 - width.
  • _d16: merge with the register's old value (old & 0xFFFF0000 | x & 0xFFFF, or old & 0xFFFF | x << 16 for _hi).
  • Raw and structured (idxen) buffers keep their bounds and alignment: both reads are 4-byte aligned dword rawBufferLoads. A constant buffer is still refused ("a sub-dword read of a constant buffer").

115 lines in lower.rs, 65 of test.

How it behaves after

Offline, translate --list --validate (dxil.dll 1.9) on 28918b7150a7aad5:

  • main + #489 + #1077 + this: 1 emitted, 1 validated.
  • main + this alone: stops earlier, at the spill-table read ("an address in an SGPR that is no root parameter"), which #489 fixes; this change has no effect there on its own.

Test: sub_dword_buffer_loads_read_the_field_at_the_byte_address runs all ten opcodes on every byte of two stored dwords (offsets 4..=11; halfwords 4..=10, including the one at byte 7 across both dwords), on hardware (RTX 4070) and WARP. The harness UAV grows from 1 to 4 dwords so a shader can write and read back.

cargo clippy --workspace --all-targets -- -D warnings is clean, cargo test --workspace passes (victory-shader: 121 passed, 1 ignored), cargo xtask policy is clean.

Overlap

#164 also lowers these loads, as part of a larger change (integer memory atomics and sub-dword buffer accesses, about 1.6k lines). This PR is only the MUBUF load part, so it can be reviewed on its own. If #164 lands first, this one can be closed.

AI-assisted.

**@⁠iurysf** on GitHub · 2026-11-24 19:19 UTC · [original](https://github.com/Marvisak/VIctory/pull/1572) `iurysf:mubuf-sub-dword-loads` → `main` · +180 −2 · 2 files · 1 commits · closed without merging ## What's wrong `Lower::buffer()` refuses every MUBUF load narrower than a dword, so the program is dropped. One of the world's busiest computes, `28918b7150a7aad5` (575 to 848 dropped dispatches per frame in our RTX 4070 runs of the open world), reads packed 16-bit fields through a raw root UAV: 0x474 buffer_load_ushort v16, v14, s[0:3], 0 offen offset:24 0x47c buffer_load_ushort v17, v14, s[0:3], 0 offen offset:26 0x484 buffer_load_ushort v19, v15, s[0:3], 0 offen With main + #489 + #1077 the program gets that far and stops there (#1077 lists `buffer_load_ushort` among what is left). ## What the fix does Source: AMD RDNA2 ISA, MUBUF loads (https://www.amd.com/content/dam/amd/en/documents/radeon-tech-docs/instruction-set-architectures/rdna2-shader-instruction-set-architecture.pdf): `buffer_load_ubyte/sbyte/ushort/sshort` zero- or sign-extend the field at the byte address to 32 bits; `*_d16` / `*_d16_hi` write one half of the VGPR and keep the other. - Read the dword the address is in (`off & ~3`), shift it right by `(off & 3) * 8`. - A halfword at byte 3 of a dword takes its top byte from the next dword: that second read runs only for the lanes that cross, in a guarded block, so other lanes never touch memory past their field. - Extend with `shl` + `lshr`/`ashr` by `32 - width`. - `_d16`: merge with the register's old value (`old & 0xFFFF0000 | x & 0xFFFF`, or `old & 0xFFFF | x << 16` for `_hi`). - Raw and structured (idxen) buffers keep their bounds and alignment: both reads are 4-byte aligned dword `rawBufferLoad`s. A constant buffer is still refused ("a sub-dword read of a constant buffer"). 115 lines in `lower.rs`, 65 of test. ## How it behaves after Offline, `translate --list --validate` (dxil.dll 1.9) on `28918b7150a7aad5`: - main + #489 + #1077 + this: 1 emitted, 1 validated. - main + this alone: stops earlier, at the spill-table read ("an address in an SGPR that is no root parameter"), which #489 fixes; this change has no effect there on its own. Test: `sub_dword_buffer_loads_read_the_field_at_the_byte_address` runs all ten opcodes on every byte of two stored dwords (offsets 4..=11; halfwords 4..=10, including the one at byte 7 across both dwords), on hardware (RTX 4070) and WARP. The harness UAV grows from 1 to 4 dwords so a shader can write and read back. `cargo clippy --workspace --all-targets -- -D warnings` is clean, `cargo test --workspace` passes (victory-shader: 121 passed, 1 ignored), `cargo xtask policy` is clean. ## Overlap #164 also lowers these loads, as part of a larger change (integer memory atomics and sub-dword buffer accesses, about 1.6k lines). This PR is only the MUBUF load part, so it can be reviewed on its own. If #164 lands first, this one can be closed. AI-assisted.
buffer() refused every MUBUF load narrower than a dword, so a program
that reads a halfword or a byte from a buffer (the console compiler
does it for packed 16-bit fields: `buffer_load_ushort v16, v14, s[0:3],
0 offen offset:24`) was dropped.

The load now reads the dword the byte address is in, shifts the field
down by the address's byte within it and zero- or sign-extends it (RDNA2
ISA, MUBUF: buffer_load_ubyte/sbyte/ushort/sshort). A halfword at byte 3
of a dword reads the next dword as well, only for the lanes that cross.
The _d16 and _d16_hi forms put the field in one half of the register and
keep the other. Raw and structured (idxen) buffers keep their bounds and
alignment: both reads are 4-byte aligned dword loads. A constant buffer
is still refused.

Test: all ten opcodes against every byte of two stored dwords, including
the halfword across them, on hardware and WARP. The test harness UAV is
four dwords now, so a shader can write and read back.

Co-authored-by: OpenBot <335604065+openbotgit[bot]@users.noreply.github.com>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:36:48 +00:00
github-bot closed this pull request 2026-11-26 04:36:48 +00:00
Author
Owner

@⁠geiyiliyabumo on GitHub · 2026-11-24 19:32 UTC · original

Verified commit 0c16661 on Windows 11 / AMD Radeon RX 9060 XT (RDNA4, gfx1200, SM 6.6) + WARP:

  • cargo test -p victory-shader --lib: 121 passed, 0 failed, 1 ignored (including the new MUBUF sub-dword and d16 load GPU tests).

AI-assisted; synthetic tests only, no game files.

**@⁠geiyiliyabumo** on GitHub · 2026-11-24 19:32 UTC · [original](https://github.com/Marvisak/VIctory/pull/1572#issuecomment-7142233322) Verified commit `0c16661` on **Windows 11 / AMD Radeon RX 9060 XT (RDNA4, `gfx1200`, SM 6.6)** + **WARP**: - `cargo test -p victory-shader --lib`: **121 passed, 0 failed, 1 ignored** (including the new MUBUF sub-dword and `d16` load GPU tests). *AI-assisted; synthetic tests only, no game files.*
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 00:42 UTC · original

Automated triage for the maintainers. Closing because this change is already in main, or main has since replaced the code it patches.

buffer_load_ubyte/sbyte/ushort/sshort (and byte/short stores) are lowered in main (lower.rs 8344-8357); only the MUBUF _d16 variants might be left. Same topic as #164 #1536.

If this is mistaken, reply here with what was missed (or push a change and say so) and it will be looked at again. Thank you for the contribution.

**@⁠Marvisak** on GitHub · 2026-11-25 00:42 UTC · [original](https://github.com/Marvisak/VIctory/pull/1572#issuecomment-7148795801) Automated triage for the maintainers. Closing because this change is already in `main`, or `main` has since replaced the code it patches. buffer_load_ubyte/sbyte/ushort/sshort (and byte/short stores) are lowered in main (lower.rs 8344-8357); only the MUBUF _d16 variants might be left. Same topic as #164 #1536. If this is mistaken, reply here with what was missed (or push a change and say so) and it will be looked at again. Thank you for the contribution.

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No reviewers
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.

Dependencies

No dependencies set

Reference
github/Marvisak--VIctory!1572
No description provided.