Shaders: a typed fetch inside a dword reads the aligned pair #1288

Closed
github-bot wants to merge 5 commits from gh-pr/1288/head into gh-pr/1288/base
Owner

@⁠pablogonz12 on GitHub · 2026-11-24 17:09 UTC · original
pablogonz12:up/vtx-fix → main · +214 −5 · 2 files · 5 commits · closed without merging

What

lower.rs: a typed vertex fetch (tbuffer_load_format_* of several dwords) at a byte offset inside a dword was emitted at that address as a vector load, which DXIL takes to be 4-byte aligned — it is not, and the host may return anything, zeros included. Such a fetch is now assembled from the two dword-aligned words around it, as the byte and half fetches already were; a test-only counter (Lowered::unaligned_typed_loads) says how many loads took that path.

Tests: the buffer-format table's sizes (all 77 formats whole bytes, one dword or a byte/half inside one), the unaligned assembly (a_typed_fetch_inside_a_dword_reads_the_pair_around_it, asserting the pair path is taken with a 33-byte offset), and a fetch of no format still refused.

Why

A community developer's GPU capture on an RTX 3090 shows one draw's vertices all collapsing to (0,0,0). The failing fetch in that draw had a byte offset inside a dword, which is exactly the case above: the decode is sound, but the address handed to the host was not aligned while DXIL promised it was.

How tested

  • cargo test -p victory-shader --lib (Linux): 99 passed, 0 failed, 1 ignored, including the three tests above.
  • cargo xwin clippy --workspace --all-targets --locked --target x86_64-pc-windows-msvc -- -D warnings, with and without --all-features: clean. No need for the #1017 host-build fix here: victory-shader builds on Linux as it is.
  • Offline four-stage check over 768 programs (751-program world dump + 52-program BVH capture, 35 duplicate containers skipped): unchanged from main ffe310e, 666 translated / 656 valid SPIR-V / 656 build a RADV pipeline, same failure table, no new failure.

Not tested

  • In game: no run with this branch yet, so the capture's draw is not confirmed fixed on hardware.
  • Native Windows.

Written with an AI assistant.

**@⁠pablogonz12** on GitHub · 2026-11-24 17:09 UTC · [original](https://github.com/Marvisak/VIctory/pull/1288) `pablogonz12:up/vtx-fix` → `main` · +214 −5 · 2 files · 5 commits · closed without merging ## What `lower.rs`: a typed vertex fetch (`tbuffer_load_format_*` of several dwords) at a byte offset inside a dword was emitted at that address as a vector load, which DXIL takes to be 4-byte aligned — it is not, and the host may return anything, zeros included. Such a fetch is now assembled from the two dword-aligned words around it, as the byte and half fetches already were; a test-only counter (`Lowered::unaligned_typed_loads`) says how many loads took that path. Tests: the buffer-format table's sizes (all 77 formats whole bytes, one dword or a byte/half inside one), the unaligned assembly (`a_typed_fetch_inside_a_dword_reads_the_pair_around_it`, asserting the pair path is taken with a 33-byte offset), and a fetch of no format still refused. ## Why A community developer's GPU capture on an RTX 3090 shows one draw's vertices all collapsing to (0,0,0). The failing fetch in that draw had a byte offset inside a dword, which is exactly the case above: the decode is sound, but the address handed to the host was not aligned while DXIL promised it was. ## How tested - `cargo test -p victory-shader --lib` (Linux): 99 passed, 0 failed, 1 ignored, including the three tests above. - `cargo xwin clippy --workspace --all-targets --locked --target x86_64-pc-windows-msvc -- -D warnings`, with and without `--all-features`: clean. No need for the #1017 host-build fix here: `victory-shader` builds on Linux as it is. - Offline four-stage check over 768 programs (751-program world dump + 52-program BVH capture, 35 duplicate containers skipped): unchanged from main `ffe310e`, 666 translated / 656 valid SPIR-V / 656 build a RADV pipeline, same failure table, no new failure. ## Not tested - In game: no run with this branch yet, so the capture's draw is not confirmed fixed on hardware. - Native Windows. Written with an AI assistant.
A typed load of several dwords at a byte offset inside one (33 in the capture)
was emitted at that address with DXIL claiming 4-byte alignment it does not
have; it is now assembled from the two dword-aligned words around it, as the
byte and half fetches already are. Tests: the format table's sizes, an
unaligned fetch reading its pair (a test-only counter), and a fetch of no
format still refused.

Written with an AI assistant.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:26:28 +00:00
github-bot closed this pull request 2026-11-26 04:26:28 +00:00
Author
Owner

@⁠geiyiliyabumo on GitHub · 2026-11-24 17:30 UTC · original

Independent verification and code review of \63702b6\ on Windows 11 / AMD Radeon RX 9060 XT (RDNA4, \gfx1200) / Rust 1.98.1 MSVC:

  • Merge check vs \main\ (\ fe310e): clean
  • \cargo test -p victory-shader --lib: 123 passed, 0 failed, 1 ignored
  • \cargo clippy -p victory-shader --all-targets -- -D warnings\ (default and --all-features): clean
  • \cargo xtask policy: 154 files ok

One real bug caught in the unaligned load path (\crates/victory-shader/src/emit/lower.rs:3196):

  • When a 128-bit format (\ otal == 128,
    == 4, e.g. \32_32_32_32_FLOAT/UINT/SINT) is fetched at a non-dword-aligned constant byte offset (\t % 4 != 0), \load(self, aligned, n as u32 + 1)\ calls \load(self, aligned, 5).
  • Inside \load, \mask = (1u8 << 5) - 1 = 0x1F\ is invalid for DXIL
    awBufferLoad\ (which only accepts a 4-bit component mask \␀x0..=0xF), and \l.s.m.extract(c, r, 4)\ extracts field 4 of \dx.types.ResRet.f32\—which is the \i32\ residency status word rather than a 5th float channel!
  • Splitting
    == 4\ into a 4-dword \load(aligned, 4)\ plus a 1-dword \load(aligned + 16, 1)\ fixes the 128-bit case cleanly. (Also minor cleanup: duplicate #[cfg(test)]\ attributes on \lower.rs:909-910\ and \shader.rs:105-106.)

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

**@⁠geiyiliyabumo** on GitHub · 2026-11-24 17:30 UTC · [original](https://github.com/Marvisak/VIctory/pull/1288#issuecomment-7139293411) Independent verification and code review of \63702b6\ on **Windows 11 / AMD Radeon RX 9060 XT (RDNA4, \gfx1200\) / Rust 1.98.1 MSVC**: - Merge check vs \main\ (\ fe310e\): clean - \cargo test -p victory-shader --lib\: **123 passed, 0 failed, 1 ignored** - \cargo clippy -p victory-shader --all-targets -- -D warnings\ (default and \--all-features\): clean - \cargo xtask policy\: 154 files ok **One real bug caught in the unaligned load path (\crates/victory-shader/src/emit/lower.rs:3196\):** - When a 128-bit format (\ otal == 128\, \ == 4\, e.g. \32_32_32_32_FLOAT/UINT/SINT\) is fetched at a non-dword-aligned constant byte offset (\t % 4 != 0\), \load(self, aligned, n as u32 + 1)\ calls \load(self, aligned, 5)\. - Inside \load\, \mask = (1u8 << 5) - 1 = 0x1F\ is invalid for DXIL \ awBufferLoad\ (which only accepts a 4-bit component mask \␀x0..=0xF\), and \l.s.m.extract(c, r, 4)\ extracts field 4 of \dx.types.ResRet.f32\—which is the \i32\ residency status word rather than a 5th float channel! - Splitting \ == 4\ into a 4-dword \load(aligned, 4)\ plus a 1-dword \load(aligned + 16, 1)\ fixes the 128-bit case cleanly. (Also minor cleanup: duplicate \#[cfg(test)]\ attributes on \lower.rs:909-910\ and \shader.rs:105-106\.) *AI-assisted; synthetic tests only, no game files.*
Author
Owner

@⁠adamdotpy on GitHub · 2026-11-24 17:34 UTC · original

Reviewed at head 63702b6 (worktree on the PR head, isolated target dir, Windows 11 / Rust 1.98.1). Findings, ranked:

1. The fix is correct and the shift math checks out. In format_load (lower.rs:~3140), a multi-dword typed fetch at a constant byte offset at with at % 4 != 0 used to pass at straight to rawBufferLoad with align = 4 — promising the host an alignment the console doesn't have, which per notes/vtx.md makes some hosts hand back zeros (the collapsed-vertex symptom). The PR instead reads n+1 dwords starting at at & !3 and rebuilds each result word as (raw[k] >> s) | (raw[k+1] << (32-s)) with s = (at%4)*8 — I verified the byte mapping by hand (result byte j comes from raw[k] when j < 4 - s/8, else from raw[k+1]); s is 8/16/24 so both shifts are in range. Dword-aligned offsets keep the single plain read, and sub-dword formats (total < 32) were already align-down-and-shift. The extra dword read past the end is safe: out-of-bounds typed loads return 0 on D3D12.

2. Tests pin it. a_typed_fetch_inside_a_dword_reads_the_pair_around_it lowers tbuffer_load_format_xyz (format 74) at byte offset 33 and asserts unaligned_typed_loads == 1, and at offset 32 asserts 0 (one plain read); a_typed_fetch_of_no_format_is_refused pins the format-0 refusal; every_buffer_format_names_its_bits_and_numbers checks the whole buffer_format table 1..=77 is whole bytes and sane (74 = 12 bytes, 24 = 4, 56 = 4). Ran here: those 3 tests pass, full cargo test -p victory-shader --lib 123 passed / 0 failed / 1 ignored (21 s, GPU tests included).

3. Checks: cargo clippy -p victory-shader --all-targets --all-features --locked -- -D warnings clean; cargo build --release -p victory builds.

4. Scope limit, not a defect: only constant offsets get the pair-read (self.s.m.int_value(off)); a dynamically computed unaligned offset on a multi-dword format still goes to the single unaligned load. Fixing that needs a runtime alignment branch (two loads + shift), a bigger change — worth stating in the PR description so nobody assumes dynamic offsets are covered.

5. Two cosmetic nits, not blocking (CI has no fmt job): the new constructors carry a duplicated #[cfg(test)] attribute (shader.rs and the Lowered init in lower.rs — legal, both cfgs must hold, but one is redundant), and the new test module code adds 2 rustfmt hunks (lower.rs:7340, 7378, double blank lines); the other 3 lower.rs hunks are pre-existing on main (7099/7118/7137). shader.rs is otherwise fmt-clean.

Nothing found that would block this.

**@⁠adamdotpy** on GitHub · 2026-11-24 17:34 UTC · [original](https://github.com/Marvisak/VIctory/pull/1288#issuecomment-7139400049) Reviewed at head `63702b6` (worktree on the PR head, isolated target dir, Windows 11 / Rust 1.98.1). Findings, ranked: **1. The fix is correct and the shift math checks out.** In `format_load` (`lower.rs:~3140`), a multi-dword typed fetch at a constant byte offset `at` with `at % 4 != 0` used to pass `at` straight to `rawBufferLoad` with `align = 4` — promising the host an alignment the console doesn't have, which per `notes/vtx.md` makes some hosts hand back zeros (the collapsed-vertex symptom). The PR instead reads `n+1` dwords starting at `at & !3` and rebuilds each result word as `(raw[k] >> s) | (raw[k+1] << (32-s))` with `s = (at%4)*8` — I verified the byte mapping by hand (result byte `j` comes from `raw[k]` when `j < 4 - s/8`, else from `raw[k+1]`); `s` is 8/16/24 so both shifts are in range. Dword-aligned offsets keep the single plain read, and sub-dword formats (`total < 32`) were already align-down-and-shift. The extra dword read past the end is safe: out-of-bounds typed loads return 0 on D3D12. **2. Tests pin it.** `a_typed_fetch_inside_a_dword_reads_the_pair_around_it` lowers `tbuffer_load_format_xyz` (format 74) at byte offset 33 and asserts `unaligned_typed_loads == 1`, and at offset 32 asserts `0` (one plain read); `a_typed_fetch_of_no_format_is_refused` pins the format-0 refusal; `every_buffer_format_names_its_bits_and_numbers` checks the whole `buffer_format` table 1..=77 is whole bytes and sane (74 = 12 bytes, 24 = 4, 56 = 4). Ran here: those 3 tests pass, full `cargo test -p victory-shader --lib` 123 passed / 0 failed / 1 ignored (21 s, GPU tests included). **3. Checks:** `cargo clippy -p victory-shader --all-targets --all-features --locked -- -D warnings` clean; `cargo build --release -p victory` builds. **4. Scope limit, not a defect:** only *constant* offsets get the pair-read (`self.s.m.int_value(off)`); a dynamically computed unaligned offset on a multi-dword format still goes to the single unaligned `load`. Fixing that needs a runtime alignment branch (two loads + shift), a bigger change — worth stating in the PR description so nobody assumes dynamic offsets are covered. **5. Two cosmetic nits, not blocking (CI has no fmt job):** the new constructors carry a duplicated `#[cfg(test)]` attribute (`shader.rs` and the `Lowered` init in `lower.rs` — legal, both cfgs must hold, but one is redundant), and the new test module code adds 2 rustfmt hunks (`lower.rs:7340`, `7378`, double blank lines); the other 3 `lower.rs` hunks are pre-existing on `main` (`7099/7118/7137`). `shader.rs` is otherwise fmt-clean. Nothing found that would block this.
Author
Owner

@⁠pablogonz12 on GitHub · 2026-11-24 17:36 UTC · original

Fixed in 112cbdfa (thanks for the report): the unaligned typed fetch no longer asks one rawBufferLoad for five words; it loads at most four at a time, and load asserts that. Added a format-77 offset-33 test. cargo test -p victory-shader --lib: 100 passed. Not tested: on hardware.

**@⁠pablogonz12** on GitHub · 2026-11-24 17:36 UTC · [original](https://github.com/Marvisak/VIctory/pull/1288#issuecomment-7139450041) Fixed in 112cbdfa (thanks for the report): the unaligned typed fetch no longer asks one `rawBufferLoad` for five words; it loads at most four at a time, and `load` asserts that. Added a format-77 offset-33 test. `cargo test -p victory-shader --lib`: 100 passed. Not tested: on hardware.
Author
Owner

@⁠adamdotpy on GitHub · 2026-11-24 19:04 UTC · original

Verified the new head 112cbdf here (worktree on the PR head, isolated target dir, Windows 11 / Rust 1.98.1) — and this follow-up fixes a real gap my earlier review missed: a ResRet carries four data words plus status, so the original load(aligned, n + 1) asked for five words on a 4-dword format at an odd offset, and the fifth would have come back as the status word. The chunked loop (at most 4 words per load, debug_assert! pinning 1..=4) plus the new a_four_dword_typed_fetch_inside_a_dword_loads_at_most_four_words_at_a_time test close it. Ran here: the 3 typed-fetch tests pass, full cargo test -p victory-shader --lib 124 passed / 0 failed / 1 ignored, clippy clean, release build ok, and the rustfmt commit cleared the two hunks I flagged — lower.rs now shows only main's 3 pre-existing hunks. My earlier scope note (dynamic unaligned offsets still take a single unaligned load) still stands and is unchanged.

**@⁠adamdotpy** on GitHub · 2026-11-24 19:04 UTC · [original](https://github.com/Marvisak/VIctory/pull/1288#issuecomment-7141565801) Verified the new head `112cbdf` here (worktree on the PR head, isolated target dir, Windows 11 / Rust 1.98.1) — and this follow-up fixes a real gap my earlier review missed: a `ResRet` carries four data words plus status, so the original `load(aligned, n + 1)` asked for five words on a 4-dword format at an odd offset, and the fifth would have come back as the status word. The chunked loop (at most 4 words per load, `debug_assert!` pinning 1..=4) plus the new `a_four_dword_typed_fetch_inside_a_dword_loads_at_most_four_words_at_a_time` test close it. Ran here: the 3 typed-fetch tests pass, full `cargo test -p victory-shader --lib` 124 passed / 0 failed / 1 ignored, clippy clean, release build ok, and the rustfmt commit cleared the two hunks I flagged — `lower.rs` now shows only main's 3 pre-existing hunks. My earlier scope note (dynamic unaligned offsets still take a single unaligned load) still stands and is unchanged.
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 02:34 UTC · original · edited 2026-11-25 03:22 UTC

Automated triage for the maintainers.

Needs work. The byte assembly in format_load (crates/victory-shader/src/emit/lower.rs) is correct as code. Loads of at most four words fix the earlier five-word problem, and 5441268 removes the doubled #[cfg(test)] and updates the shortcut comment. CI passes.

The remaining gap is evidence. The new path only runs for compile-time-constant offsets (int_value(off)), but vertex fetches usually compute their offset at run time, and those still take the single unaligned load. Nothing yet ties this change to the captured draw. It also changes shader semantics against main's documented dword-alignment shortcut without a reference for how the hardware reads an unaligned multi-dword typed fetch. To change the verdict, add one of: a capture or translate output showing the failing fetch has a constant offset, coverage for run-time offsets, or an ISA-guide or hardware-readback reference.

On your reply: thank you for the fixes and for stating plainly what is still unverified. Those open points are what blocks merging.


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-25 02:34 UTC · [original](https://github.com/Marvisak/VIctory/pull/1288#issuecomment-7150887935) · edited 2026-11-25 03:22 UTC <!-- victory-triage v1 head=544126854b77dc06bcd2926bc79d7fd2f2b57fc3 verdict=needs-work at=2026-11-25T03:22:00Z --> Automated triage for the maintainers. **Needs work.** The byte assembly in `format_load` (`crates/victory-shader/src/emit/lower.rs`) is correct as code. Loads of at most four words fix the earlier five-word problem, and 5441268 removes the doubled `#[cfg(test)]` and updates the shortcut comment. CI passes. The remaining gap is evidence. The new path only runs for compile-time-constant offsets (`int_value(off)`), but vertex fetches usually compute their offset at run time, and those still take the single unaligned load. Nothing yet ties this change to the captured draw. It also changes shader semantics against `main`'s documented dword-alignment shortcut without a reference for how the hardware reads an unaligned multi-dword typed fetch. To change the verdict, add one of: a capture or `translate` output showing the failing fetch has a constant offset, coverage for run-time offsets, or an ISA-guide or hardware-readback reference. On your reply: thank you for the fixes and for stating plainly what is still unverified. Those open points are what blocks merging. --- _Generated by [Claude Code](https://claude.ai/code)_
Author
Owner

@⁠pablogonz12 on GitHub · 2026-11-25 02:43 UTC · original

On the triage for 0d0f5f91. Fixed in 54412685: the doubled #[cfg(test)] in lower.rs and shader.rs, and the shortcut comment on format_load now says that only constant offsets inside a dword get the pair path and run-time offsets are still taken as aligned (victory-shader lib 122 pass/1 ignored; Windows clippy -D warnings clean, default and all features). Still open: run-time offsets are not covered; I have not verified that the capture's failing fetch has a constant offset (the description does not establish it, and there is no game run on this branch); and I have no ISA or hardware readback reference for an unaligned multi-dword typed fetch. So treat the change as unconfirmed for the captured draw.

**@⁠pablogonz12** on GitHub · 2026-11-25 02:43 UTC · [original](https://github.com/Marvisak/VIctory/pull/1288#issuecomment-7151089114) On the triage for 0d0f5f91. Fixed in 54412685: the doubled `#[cfg(test)]` in `lower.rs` and `shader.rs`, and the shortcut comment on `format_load` now says that only constant offsets inside a dword get the pair path and run-time offsets are still taken as aligned (victory-shader lib 122 pass/1 ignored; Windows clippy -D warnings clean, default and all features). Still open: run-time offsets are not covered; I have not verified that the capture's failing fetch has a constant offset (the description does not establish it, and there is no game run on this branch); and I have no ISA or hardware readback reference for an unaligned multi-dword typed fetch. So treat the change as unconfirmed for the captured draw.
Author
Owner

@⁠pablogonz12 on GitHub · 2026-11-25 11:30 UTC · original

I can now answer the first of the three points, and the answer is against this PR for the programs I have. At head 54412685 I added temporary prints to the branches of format_load for fetches of 32 bits or more, and translated our set of 751 programs captured in the world (740 translate, 11 fail):

  • constant offset, aligned or not (the branch this PR adds the pair read to): reached 0 times;
  • run-time offset: 437 times (424 of 32 bits, 13 of 64 bits), which this PR leaves as main does.

So nothing in that set shows the change taking effect. It does not rule out a constant-offset fetch in a program outside the set, but I have none to show. I also still have no run-time coverage and no hardware or ISA reference. Unless a maintainer or someone with such a capture wants it kept, I think closing it is reasonable.

AI-assisted (Claude).

**@⁠pablogonz12** on GitHub · 2026-11-25 11:30 UTC · [original](https://github.com/Marvisak/VIctory/pull/1288#issuecomment-7161136460) I can now answer the first of the three points, and the answer is against this PR for the programs I have. At head 54412685 I added temporary prints to the branches of `format_load` for fetches of 32 bits or more, and translated our set of 751 programs captured in the world (740 translate, 11 fail): - constant offset, aligned or not (the branch this PR adds the pair read to): reached **0** times; - run-time offset: 437 times (424 of 32 bits, 13 of 64 bits), which this PR leaves as `main` does. So nothing in that set shows the change taking effect. It does not rule out a constant-offset fetch in a program outside the set, but I have none to show. I also still have no run-time coverage and no hardware or ISA reference. Unless a maintainer or someone with such a capture wants it kept, I think closing it is reasonable. AI-assisted (Claude).
Author
Owner

@⁠pablogonz12 on GitHub · 2026-11-25 11:35 UTC · original

Closing this one myself as discussed above: it doesn't earn its place. Thanks for the review.

**@⁠pablogonz12** on GitHub · 2026-11-25 11:35 UTC · [original](https://github.com/Marvisak/VIctory/pull/1288#issuecomment-7161242099) Closing this one myself as discussed above: it doesn't earn its place. Thanks for the review.

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!1288
No description provided.