Shaders: a typed fetch inside a dword reads the aligned pair #1288
No reviewers
Labels
No labels
accessibility
area: build
area: formats
area: gpu
area: loader
area: runtime
area: shader
bug
documentation
duplicate
enhancement
good first issue
gpu: amd
gpu: intel
gpu: nvidia
help wanted
host: wine
invalid
out of scope
question
request
run report
triage: confirmed
triage: has patch
triage: merge candidate
triage: needs info
triage: needs rebase
triage: needs work
triage: proposed close
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/Marvisak--VIctory!1288
Loading…
Reference in a new issue
No description provided.
Delete branch "gh-pr/1288/head"
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?
@pablogonz12 on GitHub · 2026-11-24 17:09 UTC · original
pablogonz12:up/vtx-fix→main· +214 −5 · 2 files · 5 commits · closed without mergingWhat
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-shaderbuilds on Linux as it is.ffe310e, 666 translated / 656 valid SPIR-V / 656 build a RADV pipeline, same failure table, no new failure.Not tested
Written with an AI assistant.
@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:One real bug caught in the unaligned load path (\crates/victory-shader/src/emit/lower.rs:3196):
== 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).
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!
== 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.
@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 offsetatwithat % 4 != 0used to passatstraight torawBufferLoadwithalign = 4— promising the host an alignment the console doesn't have, which pernotes/vtx.mdmakes some hosts hand back zeros (the collapsed-vertex symptom). The PR instead readsn+1dwords starting atat & !3and rebuilds each result word as(raw[k] >> s) | (raw[k+1] << (32-s))withs = (at%4)*8— I verified the byte mapping by hand (result bytejcomes fromraw[k]whenj < 4 - s/8, else fromraw[k+1]);sis 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_itlowerstbuffer_load_format_xyz(format 74) at byte offset 33 and assertsunaligned_typed_loads == 1, and at offset 32 asserts0(one plain read);a_typed_fetch_of_no_format_is_refusedpins the format-0 refusal;every_buffer_format_names_its_bits_and_numberschecks the wholebuffer_formattable 1..=77 is whole bytes and sane (74 = 12 bytes, 24 = 4, 56 = 4). Ran here: those 3 tests pass, fullcargo test -p victory-shader --lib123 passed / 0 failed / 1 ignored (21 s, GPU tests included).3. Checks:
cargo clippy -p victory-shader --all-targets --all-features --locked -- -D warningsclean;cargo build --release -p victorybuilds.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 unalignedload. 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.rsand theLoweredinit inlower.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 3lower.rshunks are pre-existing onmain(7099/7118/7137).shader.rsis otherwise fmt-clean.Nothing found that would block this.
@pablogonz12 on GitHub · 2026-11-24 17:36 UTC · original
Fixed in
112cbdfa(thanks for the report): the unaligned typed fetch no longer asks onerawBufferLoadfor five words; it loads at most four at a time, andloadasserts that. Added a format-77 offset-33 test.cargo test -p victory-shader --lib: 100 passed. Not tested: on hardware.@adamdotpy on GitHub · 2026-11-24 19:04 UTC · original
Verified the new head
112cbdfhere (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: aResRetcarries four data words plus status, so the originalload(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 newa_four_dword_typed_fetch_inside_a_dword_loads_at_most_four_words_at_a_timetest close it. Ran here: the 3 typed-fetch tests pass, fullcargo test -p victory-shader --lib124 passed / 0 failed / 1 ignored, clippy clean, release build ok, and the rustfmt commit cleared the two hunks I flagged —lower.rsnow 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.@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, and5441268removes 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 againstmain'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 ortranslateoutput 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
@pablogonz12 on GitHub · 2026-11-25 02:43 UTC · original
On the triage for
0d0f5f91. Fixed in54412685: the doubled#[cfg(test)]inlower.rsandshader.rs, and the shortcut comment onformat_loadnow 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 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
54412685I added temporary prints to the branches offormat_loadfor fetches of 32 bits or more, and translated our set of 751 programs captured in the world (740 translate, 11 fail):maindoes.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:35 UTC · original
Closing this one myself as discussed above: it doesn't earn its place. Thanks for the review.
Pull request closed