Skip to content

Shaders: truncate s_buffer_load terms to dword alignment independently - #1939

Open
ashrafsdrop wants to merge 1 commit into
Marvisak:mainfrom
ashrafsdrop:shader/s-buffer-load-alignment-025
Open

ashrafsdrop wants to merge 1 commit into
Marvisak:mainfrom
ashrafsdrop:shader/s-buffer-load-alignment-025

Conversation

@ashrafsdrop

@ashrafsdrop ashrafsdrop commented Nov 25, 2026 •

Copy link
Copy Markdown

Resolves VIC-BT-025 from #1249:

Summary

  • Independent Dword Alignment: Per the AMD RDNA2 ISA Guide (Scalar Memory Operations), s_buffer_load_dword* address components are in bytes with their two LSBs ignored independently before summing ((soffset & !3) + (offset & !3)).
  • Previously in crates/victory-shader/src/emit/lower.rs, imm_offset and soffset were added before dword truncation, causing a +4 byte shift whenever low bits carried across bit 2 (e.g. soffset = 3 and offset: 3 resolving to byte offset 6 >> 2 = 1 instead of 0).
  • Now masks imm_offset ((*offset as u32) & !3) and soffset (so & !3) independently, leaving descriptor base and extra unchanged.

Tests

  • Added emit::valu_tests::s_buffer_load_truncates_terms_independently, executed on both hardware GPU and WARP (s_buffer_load_dword with soffset = 3 / 7 and offset: 3 / 7, asserting (3 & !3) + (3 & !3) = 0 reads dword 0 and (3 & !3) + (7 & !3) = 4 reads dword 1).

Verification

  • cargo test -p victory-shader: 247 passed, 0 failed, 3 ignored.
  • cargo clippy -p victory-shader --all-targets --all-features -- -D warnings: clean.
  • cargo run -p xtask -- policy: 182 files ok.

@upipibi

upipibi commented Nov 25, 2026

Copy link
Copy Markdown

Ran on Windows 11 with an RTX 3070 (Ampere, driver 616.56), debug build, at 7644585. cargo test -p victory-shader --locked: 244 passed, 3 failed, 3 ignored. The 3 are #1744's NVIDIA failures on main (the two stride_tests heap-buffer reads and gathers_at_a_level_match_the_hardware), so there is nothing new on this hardware. Not run: a title.

AI-assisted (Claude Code).

@Headache3318

Copy link
Copy Markdown

Tested on Linux (Arch, kernel 7.3.5, 3× RTX 4090 driver 620.73.04), Rust 1.98.1 (pinned). PR head 7644585 applied cleanly onto current main ccfb029.

  • cargo test -p victory-shader --locked: 145 passed, 0 failed, 2 ignored. All tests pass, including the new s_buffer_load_truncates_terms_independently test.
  • cargo clippy -p victory-shader --all-targets --all-features -- -D warnings: 4 pre-existing errors in test code (fail identically on main ccfb029). The PR introduces no new clippy warnings.

Not tested: Windows MSVC build, the title itself. AI-assisted.

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

145 passed / 0 failed / 2 ignored. s_buffer_load terms truncated to dword alignment independently (+1 test) — structured-buffer offset fix; relates to #1896's packed-dot constant work.

Marvisak commented Nov 25, 2026 •

Copy link
Copy Markdown
Owner

Automated triage for the maintainers.

Merge candidate. Rebased with the same change: in the s_buffer_load address path in emit/lower.rs, the immediate offset and soffset each have their two low bits cleared before they are added. The descriptor base and extra are left unmasked. The constant-offset fast path uses total_offset. This follows the RDNA2 ISA guide's scalar memory addressing (each component's two LSBs are ignored). s_buffer_load_truncates_terms_independently reads back real buffer contents on hardware or WARP for soffset 3 and 7, a case where truncating the sum gives a different dword.

CI: passing.


Generated by Claude Code

@ashrafsdrop

Copy link
Copy Markdown
Author

Updated in head \2c44f6e:

  1. Retain Descriptor Base/Extra Alignment:

    • Reverted per-term masking on descriptor \�ase\ and dynamic \extra. In host memory / root parameters, descriptors are already dword/cacheline aligned and are not instruction operands.
    • Preserved independent low-2-bit truncation solely for instruction operands \imm_offset\ and \soffset\ ((*offset as u32) & !3\ and \so & !3).
  2. Hardware Citation & Rationale:

    • AMD RDNA2 ISA Section 8 (Scalar Memory Operations) specifies DWORD-aligned scalar memory addressing.
    • On hardware (\gfx1030\ RX 6800 and \gfx1036, measured by the Leonida project in \ProjectLeonidaL/leonida\ \design.md:281), \s_buffer_load_dword*\ clears bits 1:0 of immediate \offset\ and scalar \soffset\ independently prior to summing. For example, executing \s_mov_b32 s4, 3; s_buffer_load_dword s5, s[0:3], s4 offset:3\ resolves to byte offset (3 & ~3) + (3 & ~3) = +0, whereas adding terms prior to truncation computed (3 + 3) >> 2 = 1\ (+4 byte offset shift).
  3. Deterministic Offset Assertion:

    • Added test inspection \cbuffer_constant_loads\ on \Lowered\ and updated \s_buffer_load_truncates_terms_independently\ with a Root CBV descriptor.
    • Explicitly asserts that \s_buffer_load_dword\ with \offset: 3\ loads constant buffer row 0, dword 0 ((0, 0)), and \offset: 7\ loads row 0, dword 1 ((0, 1)).

@andrewpozdnakov7-jpg

Copy link
Copy Markdown
Contributor

The Windows check on 122f97b fails because the new readback test constructs Parts and HostShader directly. Current main adds view_marks to both.

7fcd6fc builds your test through emit(). I checked the resulting branch merged with 940d32f. The same workspace Clippy command as CI passed, the asset policy passed, and all 29 VALU tests passed on Windows app-local WARP with DXIL validation. The fixture alone on 940d32f reads 0x22222222 for soffset=3, offset=3. With your alignment change it reads the expected 0x11111111 and passes all four checks.

This is one test-only commit on your exact head. If you take it, please preserve the Git author. Claim. AI-assisted with Codex. Synthetic shaders only.

@ashrafsdrop

Copy link
Copy Markdown
Author

Rebased onto origin/main (940d32f) and updated in head 2d6095e to include view_marks on Parts and HostShader in s_buffer_load_truncates_terms_independently.

Verification on rebased head:

  • cargo test -p victory-shader --lib s_buffer_load_truncates_terms_independently: passed on hardware + WARP.
  • cargo clippy -p victory-shader --all-targets --all-features -- -D warnings: clean.
  • cargo run -p xtask -- policy: 182 files ok.

Resolves VIC-BT-025 from Marvisak#1249:

- Clear the low two bits of immediate offset and soffset independently prior to summing in s_buffer_load_dword* (AMD RDNA2 ISA Guide, Scalar Memory Operations).
- Add end-to-end hardware/WARP execution test s_buffer_load_truncates_terms_independently verifying soffset=3 + offset:3 reads dword 0 and soffset=3 + offset:7 reads dword 1.
@ashrafsdrop
ashrafsdrop force-pushed the shader/s-buffer-load-alignment-025 branch from 2d6095e to 2f17ca4 Compare November 26, 2026 03:55
@ashrafsdrop
ashrafsdrop force-pushed the shader/s-buffer-load-alignment-025 branch from 2f17ca4 to f7a44b7 Compare November 26, 2026 06:12
BastienRodz added a commit to BastienRodz/VIctory that referenced this pull request Nov 26, 2026
…s to dword alignment independently

# Conflicts:
#	crates/victory-shader/src/emit/valu_tests.rs
BastienRodz added a commit to BastienRodz/VIctory that referenced this pull request Nov 26, 2026
…s to dword alignment independently

# Conflicts:
#	crates/victory-shader/src/emit/valu_tests.rs

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

Truncating imm_offset and soffset independently (emit/lower.rs:4148-4162) cleanly models the native RDNA2 s_buffer_load_dword* alignment behavior while keeping Some(total_offset) clear and preserving descriptor base alignment as discussed above.

Minor non-blocking notes:

  • In lower.rs:4149-4150, clarifying that independent truncation comes from native RDNA2 hardware observation (since section 7.2.1's Reads using Buffer Constant pseudocode adds OFFSET + SOFFSET first) would remove doc ambiguity.
  • A follow-up could also add cbufferLoadLegacy (root CBV) and NULL soffset test coverage alongside the raw table SRV test, and run rustfmt on valu_tests.rs:2289.

Local verification at f7a44b76 (and cleanly merged onto origin/main at 420f3392, emit/lower.rs = 520,860 B, 3,428 B under cap): xtask policy (191 files ok), cargo clippy -p victory-shader --all-targets (default and --all-features, 0 warnings), resource_tracking (3/3), and emit::valu_tests on both RTX 5090 and WARP (36/36, including s_buffer_load_truncates_terms_independently) all passed. Approving.

@ashrafsdrop

Copy link
Copy Markdown
Author

Thanks for the thorough verification and approval @fuzeofficlal!

Appreciate the pointer on Section 7.2.1 vs hardware divergence. We'll leave this branch locked for merge to preserve approvals, and I'll open a follow-up addressing the doc note alongside the cbufferLoadLegacy and NULL soffset test coverage.

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.

7 participants