Skip to content

Shaders: LDS sub-dword writes, typed buffers at offsets and of some channels, typed loads by index - #1959

Open
RaghavVijayanand wants to merge 5 commits into
Marvisak:mainfrom
RaghavVijayanand:shader/buffer-lds-gaps
Open

RaghavVijayanand wants to merge 5 commits into
Marvisak:mainfrom
RaghavVijayanand:shader/buffer-lds-gaps

Conversation

@RaghavVijayanand

@RaghavVijayanand RaghavVijayanand commented Nov 25, 2026 •

Copy link
Copy Markdown

Buffer and LDS gaps in the translator, in four commits. Each builds and passes tests.

  • LDS byte and half writes keep one lane's value. Main (0b35b7f) already reads and writes LDS bytes and halves, but writes them with an atomic AND then OR, noted there as a shortcut. When several lanes write the same byte, that merges their bits. The write is now a compare-exchange loop on the dword, so neighbouring bytes are never clobbered and exactly one lane's value lands, as on the console. Main's read path is kept.
  • Typed buffer loads and stores at a byte offset: the access goes to element index + offset / element size.
    • The element size is taken from the used-resource entry's stride. This is a documented assumption: it is not verified that the console compiler records the format size there.
    • Still refused: a constant offset inside an element, and any offset when no stride is known.
  • Typed stores of fewer than four channels: dxil.dll requires full masks on typed UAV stores. So the store loads the element, keeps the channels it isn't given, and stores all four, inside the same EXEC guard.
    • Documented limits: it assumes the console leaves those channels untouched; an snorm -128 round-trips as -127; and the store is not atomic against other lanes writing the same element.
  • tbuffer_load_format_* by index: reads at byte index * stride + offset, through main's bindless-stride conversions (a root view's built descriptor takes its stride from dword 1, and a heap descriptor from the view-stride table).
    • Behaviour change to main's raw path: the lane-index-added refusal (dword 3 bit 23) now also applies to raw accesses through a root view, which main translated silently and incorrectly. This is easy to revert if you prefer main's behaviour.
    • Swizzled and add-tid descriptors are still refused.

Tests: the new emit/buffer_lds_tests.rs (8 tests) and a View::FormatUav in the test host. Every program is hand-assembled, validated with dxil.dll, run on the default hardware adapter and WARP, and checked against a CPU model.

  • LDS: 64 threads in two waves write neighbouring and identical bytes, with EXEC-only guards, and read back in every width and form.
  • Typed offsets: four ways of adding offsets, through RGBA32F and RGBA16F views, plus the refusals.
  • Partial stores: x, xy, xyz and d16_xy, with EXEC masking.
  • Loads by index: three formats with 20-byte records, plus a built descriptor whose own stride differs from the entry's.

A new test, typed_format_loads_by_index_from_the_heap_take_the_views_stride, covers the heap path. Each change was broken by hand to confirm the tests fail: AND-then-OR instead of compare-exchange, the offset ignored, kept channels zeroed, the index multiply dropped, and typed loads left out of the view-stride table.

Rebased onto current main (940d32f). On this machine, 5 tests fail on main itself, with or without this change: descriptor::host::tests::dynamic_slots_are_held_until_released, memory::tests::lists_share_a_table_and_copies_keep_their_views, the two stride_tests::a_heap_buffer_read_* tests, and synthetic_tests::gathers_at_a_level_match_the_hardware. Everything else passes, and clippy passes with -D warnings, with and without --all-features.

Copilot AI balanced review requested due to automatic review settings November 25, 2026 01:45

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

Marvisak commented Nov 25, 2026 •

Copy link
Copy Markdown
Owner

Automated triage for the maintainers.

This pull request no longer merges cleanly into main (conflicts in crates/victory-shader/src/emit/lower.rs). It will be reviewed once it is rebased onto the current main. The earlier verdict (needs work) was for an older state of main.


Generated by Claude Code

@RaghavVijayanand RaghavVijayanand changed the title Shaders: LDS bytes and halves, typed buffers at offsets and of some channels Shaders: LDS sub-dword writes, typed buffers at offsets and of some channels, typed loads by index Nov 25, 2026

@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.

Tested locally: macOS arm64, rustc 1.98.1, PR head merged into main 940d32f, cargo test -p victory-shader --locked.

144 passed / 0 failed / 2 ignored. LDS bytes and halves, typed buffers at offsets and of some channels — sub-dword LDS/buffer access completeness; relates to the LDS sub-dword cluster (#632/#116/#101/#571/#996) and #2003's MUBUF d16.

@sinan-cmyk

Copy link
Copy Markdown

Offline measurement on a retail dump

Result: no count change against main.

I merged head 40770f9 onto main 940d32f (clean merge) and ran victory translate --validate --list on shaders.rpf (1.0.2.1), with dxil.dll 1.8.2502.11. I compared the result with main program by program (25,889 programs). Validated: 23,918 on main, 23,918 with this PR.

No program changes its failure reason. The count shows that the change rejects nothing on this data. It does not show whether the output is right.

No shader names or data are included, per the asset policy. AI-assisted (Claude Code).

ds_write_b8/b16 and their d16_hi forms wrote their bits into the dword
holding them by an atomic AND, then an atomic OR: lanes writing the same
byte or half in one instruction left the OR of their values (the console
keeps one), and other lanes could see the part cleared between the two.
A write now replaces its bits with a compare-exchange loop on the dword:
lanes writing other bytes of the same dword keep theirs, and of lanes
writing the same byte one value wins, as on the console. The opcode
names of byte and half accesses are decoded in one place for the reads
and the writes.

Tested by a new module, emit/buffer_lds_tests.rs: a hand-assembled
64-thread (two-wave) program writes bytes and halves of the same dwords
from all lanes, every lane a bit into one shared byte, and bytes under an
EXEC mask, then reads them back in every width and form. The DXIL passes
dxil.dll, and the results match a model of the console's memory on the
default hardware adapter and WARP. The test fails with the atomic AND
then OR it replaces.
buffer_load_format_* stopped the translation at any offset but 0, and
buffer_store_format_xyzw at any instruction or lane offset. The console
adds index * stride + offset to the descriptor's base; the host's typed
view numbers elements of its format, and DXIL's typed loads and stores
take an element, not a byte offset. The access now goes to element
index + offset / size, the size the used-resource entry's stride (the
console's typed descriptors have their format's size as stride). What
stays refused, precisely: a constant offset inside an element, and any
offset into a view whose entry has no stride. A computed offset is
taken as whole elements (shortcut, in the code).

Tested in emit/buffer_lds_tests.rs, with a typed UAV view the host test
harness can now start from given data (View::FormatUav): 32 lanes load
whole elements of R32G32B32A32_FLOAT and R16G16B16A16_FLOAT views at
offsets added the four ways the console adds them (the instruction's to
an index, a lane's to an index, a lane's plus a constant SGPR offset, a
lane's plus the instruction's), and store elements at an index plus the
instruction's offset and at a lane's offset. The DXIL passes dxil.dll and
the results match on the default hardware adapter and WARP. The refusals
are tested too. The tests fail with the offset ignored and with a
computed offset taken as elements.
buffer_store_format_x, _xy and _xyz (and their d16 forms) stopped the
translation: DXIL typed stores must write all four channels (dxil.dll
refuses a partial write mask on a typed UAV), while the console writes
the channels it is given. Such a store now loads the element (a typed
UAV load), keeps the channels it is not given, and stores all four, the
load and store under the same EXEC guard.

Shortcut, in the code: the channels not given are taken to keep what
memory holds, which this does not confirm on the console. They round-trip
through floats (exact for float and unorm formats), and the load and
store are not atomic with other lanes' stores to the same element.

Tested in emit/buffer_lds_tests.rs: 32 lanes store x, xy, xyz (even
lanes only, by EXEC alone) and two halves (d16_xy) into elements of
R32G32B32A32_FLOAT and R16G16B16A16_FLOAT views filled with known data.
The DXIL passes dxil.dll and every channel matches on the default
hardware adapter and WARP. The test fails with the kept channels zeroed
and with the store's EXEC guard dropped.
tbuffer_load_format_* with idxen stopped the translation. The console
adds index * stride to the byte offset, the stride the descriptor's;
the load now reads its raw view at that byte, as without an index.
Typed-format loads read raw views, so they now take the index the way
raw buffer accesses do: a descriptor built in the code around a root
view has its stride in the bits OR-ed into dword 1 (0 too: the console
then ignores the index), and a descriptor taken from the resource heap
(no used-resource entry) the view's stride from the host's view-stride
table. A built descriptor elsewhere takes its own stride too; one from a
table its used-resource entry's (shortcut, in the code: taken to be the
descriptor's, as for structured buffers). Refused: an entry without a
stride, and a built descriptor with swizzled addressing or the lane
index added (dword 1 bits 30-31, dword 3 bit 23; the lane-index refusal
now covers raw accesses through a root view too, which read at the wrong
bytes with it).

Tested in emit/buffer_lds_tests.rs: 32 lanes read 20-byte records of a
raw table view by index in 32_32_32_32, 8_8_8_8 (at a lane's offset) and
8 (at byte 3) uint formats, 12-byte records through a descriptor built
around a root SRV whose entry says stride 1, and 12-byte records of a
buffer through a descriptor taken from the resource heap, the stride the
view-stride table's. The DXIL passes dxil.dll and the results match on
the default hardware adapter and WARP; the refusals are tested too. The
table test fails with the index ignored, and the heap test with
typed-format loads left out of the view-stride table's addressing.
@RaghavVijayanand

Copy link
Copy Markdown
Author

Rebased onto main 63bcca4. Merged with what main gained since:

  • LDS sub-dword writes: the compare-exchange loop now sits inside main's bounds guard from Shaders: LDS accesses past the program's allocation read 0 and drop writes (#1870) #1949. A write past the allocation is dropped, and of lanes writing the same byte, one value wins.
  • Typed buffer stores: they use main's typed_h handle (made once per store, for the d16 integer-view path) together with this branch's byte-offset element and partial-channel merge.

Each commit builds. Locally (RTX 5060 Laptop): the buffer/LDS/typed tests pass (14 of 15), and clippy is clean. typed_stores_of_some_channels_keep_the_others fails on the RTX with "dispatch failed: The parameter is incorrect". That is the same NVIDIA 1M-view-heap-cap failure that main's own stride_tests::a_heap_buffer_* hit there. On WARP it passes along with those two (VICTORY_WARP=1). Before the rebase it passed on the RTX as well.

lower.rs grew past the repository's 512 KiB file limit with this branch on
top of main; lds_sub_dword and Lower::typed_element move to
lower/buffers.rs unchanged, and a comment is shortened.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

5 participants