Skip to content

Shaders: lower indexed tbuffer_load_format_* and indexed buffer_store_{byte,short} - #1871

Open
ESAT586 wants to merge 1 commit into
Marvisak:mainfrom
ESAT586:shader/indexed-tbuffer-and-subdword-stores
Open

ESAT586 wants to merge 1 commit into
Marvisak:mainfrom
ESAT586:shader/indexed-tbuffer-and-subdword-stores

Conversation

@ESAT586

@ESAT586 ESAT586 commented Nov 24, 2026

Copy link
Copy Markdown

Summary

Lowers indexed tbuffer_load_format_* (idxen) and indexed buffer_store_{byte,short} (idxen) in crates/victory-shader/src/emit/lower.rs, removing "a typed-format load by index" and "a byte or half buffer store by index":

  • Indexed tbuffer_load_format_* (format.is_some()):
    • Root views (Source::Built): MTBUF instructions supply their own format over raw/structured descriptors rather than requiring a typed buffer view, so root views without swizzling or a record limit (bits >> 30 == 0 && words[0] == u32::MAX) fold index * stride into the byte offset.
    • Bindless heap views (used.is_none()): folds index * view_stride(src) via VIEW_STRIDES into the byte offset.
    • Declared StructuredBuffer / RWStructuredBuffer (used.stride >= 4 && used.stride % 4 == 0): extends format_load to accept index: Option<V> and emit dx.op.rawBufferLoad.f32(handle, index, elementOffset, mask, 4) on (12 | u, stride) for both sub-dword formats (dword-aligned load + shift) and 32-128-bit formats, with idxen, offen, immediate offset, and soffset.
    • Uses exact format transfer width (total.div_ceil(32) * 4) for raw_view.
  • Indexed buffer_store_byte / buffer_store_short:
    • Declared RWStructuredBuffer UAVs (stride >= 4 && stride % 4 == 0) lower to structured dx.op.atomicBinOp.i32(handle, And/Or, index, at, undef, keep/val) on (12 | 0x1000, stride), preserving concurrent neighboring bytes/halves in the same dword and EXEC guards.
  • Refusals preserved: declared structured buffers with stride < 4 or stride % 4 != 0 still refuse with "a structured buffer of unknown stride"; root views with swizzling or a record limit still refuse with "a root view indexed with swizzling or a record limit".
  • Scope / non-overlap: intentionally does not touch tbuffer_load_format_d16_* packing (Shaders: tbuffer_load_format_d16 loads pack two 16-bit channels per VGPR #169), byte-offset-3 16-bit straddling (shader: handle dword-straddling 16-bit typed buffer loads at byte offset 3 #1669), buffer_load_{ubyte,sbyte,ushort,sshort} (Shaders: MUBUF sub-dword loads (buffer_load_ubyte, sbyte, ushort, sshort and the d16 forms) #1572), or buffer_store_{byte,short}_d16_hi (Shaders: integer memory atomics and sub-dword buffer accesses #164).

Test Plan

Added View::StructuredUav (#[cfg(test)]) in crates/victory-shader/src/emit/host.rs and 5 synthetic tests in crates/victory-shader/src/emit/stride_tests.rs, validated with dxil.dll and executed on NVIDIA GeForce RTX 3060 Ti and Microsoft D3D12 WARP:

  • indexed_tbuffer_load_format_reads_structured_srv_formats: StructuredBuffer SRV (stride 20) testing 8_8_8_8_UNORM, unaligned sub-dword 8_UNORM at offset 5, 8_8_SINT at offset 6, 64-bit 16_16_16_16_FLOAT at offset 8, channel-padded 8_UINT, and runtime idxen + offen + soffset 16_16_SNORM.
  • indexed_tbuffer_load_format_reads_structured_uav: RWStructuredBuffer UAV (stride 16) testing 8_8_8_8_UINT and 32_32_FLOAT with (0.0, 1.0) channel padding.
  • indexed_tbuffer_load_format_reads_root_view_and_heap_view: Source::Built root SRV (stride 16) and bindless ResourceDescriptorHeap via VIEW_STRIDES (stride 12).
  • indexed_byte_and_short_buffer_stores_preserve_neighbors_and_guard: pre-populated non-zero sentinel RWStructuredBuffer UAV (stride 16) testing concurrent 4-lane byte writes sharing a dword (including 0x00 clearing non-zero sentinel bits), divergent EXEC-guarded short writes sharing a dword, divergent idxen + soffset byte writes, and untouched sentinel dwords.
  • indexed_tbuffer_and_subdword_stores_refuse_unknown_stride_and_record_limit: verifies refusal on stride 0, stride 6, and record-limited root views.

Verification Results

  • cargo test -p victory-shader --lib emit::stride_tests::indexed_ -- --test-threads=1 --nocapture: 5 passed, 0 failed (RTX 3060 Ti + WARP + dxil.dll).
  • cargo test -p victory-shader --lib emit::memory_tests -- --test-threads=1: 2 passed, 0 failed.
  • cargo test -p victory-shader --lib emit::lower::tests -- --test-threads=1: 17 passed, 0 failed.
  • cargo clippy --workspace --all-targets -- -D warnings: clean (0 warnings).
  • cargo clippy --workspace --all-targets --all-features -- -D warnings: clean (0 warnings).
  • cargo xtask policy: asset policy: 174 files ok.

Disclosure

  • AI Assistance: Implemented and verified with Gemini 4 Neon (Extra High Mode).
  • Clean-Room: Based strictly on public AMD RDNA2 ISA documentation, DXIL specification, and synthetic unit tests; zero game binaries, dumps, or copyrighted assets were used.

Marvisak commented Nov 25, 2026 •

Copy link
Copy Markdown
Owner

Automated triage for the maintainers.

Needs work (small fix).

Thanks for rebasing. The rebased head fd38a61 now gets a full review. In Lower::buffer (emit/lower.rs) it lets tbuffer_load_format_* with idxen use the paths that untyped loads already use:

  • Root views (Source::Built): the index times the stride from bits 16–29 is folded into the byte offset.
  • Heap views: the index times view_stride is folded in the same way.
  • Declared structured views: rawBufferLoad gets (index, offset) on a (12 | u, stride) handle.

Indexed buffer_store_{byte,short} now uses the same clear/set atomics on a structured handle, and mark_view_write gets index * stride + at. This follows the indexed-atomic path just above it and the public RDNA2 addressing (base + index × stride + offset). The refusals for unknown stride and record-limited root views stay. The GPU readback tests in stride_tests.rs with sentinel neighbours are a good check. CI: passing (check).

What blocks it:

  • Encoding damage in stride_tests.rs: three existing comments change from 2 × lane / 3 × lane to 2 × lane / 3 × lane. The file was saved through the wrong encoding. Please restore those lines so the diff only adds code.

Optional: on the structured paths, an offset at or past stride reads 0 or is dropped on the host, while the hardware reaches into the next record. Please add a shortcut: comment, as nearby code does for its approximations.

With the encoding fixed, I expect this to be a merge candidate.


Generated by Claude Code

@catminou4 catminou4 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Merge conflict with current main (940d32f) — git merge origin/main fails on the PR head. The lowering work itself (indexed tbuffer_load_format_* / buffer_store_) looks like the right direction; a rebase is needed before it can land. Will re-test after the author rebases.

@ESAT586
ESAT586 force-pushed the shader/indexed-tbuffer-and-subdword-stores branch from 1076ad9 to fd38a61 Compare November 25, 2026 11:08
@ESAT586

ESAT586 commented Nov 25, 2026

Copy link
Copy Markdown
Author

Rebased onto \main\ (\940d32f) at \ d38a61\ (preserving \d16_channels\ on \ ormat_load\ and passing \index * stride + at\ / adjusted
aw_view\ byte offset to \mark_view_write\ from \cd94d35). Verified on RTX 3060 Ti + WARP: all 5 \indexed_*\ tests and \writes_through_views_mark_their_pages\ passed, strict Clippy (--all-targets\ and --all-features) clean, \cargo xtask policy\ ok.

…_{byte,short}

Removes the two indexed-buffer refusals in `emit::lower::Lower::buffer`
("a typed-format load by index" and "a byte or half buffer store by index"):

- `tbuffer_load_format_*` with `idxen` now handles:
  - Root views (`Source::Built` with `bits >> 30 == 0` and `words[0] == u32::MAX`):
    folds `index * stride` into the byte offset (since MTBUF carries its own format
    over raw/structured descriptors rather than requiring a typed buffer view).
  - Bindless heap descriptors (`ResourceDescriptorHeap`, `used.is_none()`):
    folds `index * view_stride(src)` into the byte offset via `VIEW_STRIDES`.
  - Declared `StructuredBuffer` SRVs and `RWStructuredBuffer` UAVs
    (`used.stride >= 4 && used.stride % 4 == 0`):
    routes `format_load` through `dx.op.rawBufferLoad.f32(handle, index, elementOffset, ...)`
    on `(12 | u, stride)`, supporting sub-dword (`total < 32`) aligned-dword extraction
    as well as 32/64/96/128-bit formats and `idxen + offen + offset + soffset`.
  - Tightens the raw-view byte count passed to `raw_view` to `total.div_ceil(32) * 4`
    (matching the exact dword count loaded by `format_load`).
- `buffer_store_byte` and `buffer_store_short` with `idxen` now route declared
  `RWStructuredBuffer` UAVs (`stride >= 4 && stride % 4 == 0`) through structured
  `dx.op.atomicBinOp.i32(handle, And/Or, index, at, undef, keep/val)` on
  `(12 | 0x1000, stride)`, preserving neighboring bytes/halves and `EXEC` guards.
- Unknown/non-dword-aligned strides and record-limited root views remain refused.
- Adds `View::StructuredUav` (test-only) in `emit::host` and 5 synthetic GPU/WARP/DXIL
  tests in `emit::stride_tests`.

Clean-room implementation from public RDNA2 ISA and DXIL specifications; no game
binaries, dumps, or copyrighted assets were used.
AI-assisted with Gemini 4 Neon (Extra High Mode).
@ESAT586
ESAT586 force-pushed the shader/indexed-tbuffer-and-subdword-stores branch from fd38a61 to 1b5a71a Compare November 25, 2026 11:31
@ESAT586

ESAT586 commented Nov 25, 2026

Copy link
Copy Markdown
Author

Fixed the three UTF-8 \×\ comments in \stride_tests.rs\ (diff is now purely additive) and added the \shortcut:\ comments on the structured view paths in \lower.rs\ at $sha. Re-verified: 5/5 \indexed_*\ tests on RTX 3060 Ti + WARP, strict Clippy clean, \cargo xtask policy\ ok.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants