Shaders: truncate s_buffer_load terms to dword alignment independently #1937

Open
ashrafsdrop wants to merge 1 commit from upstream-pr/1939 into main
ashrafsdrop commented 2026-11-25 01:22:56 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by ashrafsdrop on 2026-11-25T01:22:56Z.


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.
<!-- greyfinch-sync:Marvisak/VIctory:issue:5483278423 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1939). Originally posted by [ashrafsdrop](https://github.com/ashrafsdrop) on 2026-11-25T01:22:56Z. --- 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 commented 2026-11-25 02:06:17 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by upipibi on 2026-11-25T01:44:47Z.


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

<!-- greyfinch-sync:Marvisak/VIctory:comment:7149906599 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1939#issuecomment-7149906599). Originally posted by [upipibi](https://github.com/upipibi) on 2026-11-25T01:44:47Z. --- 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 commented 2026-11-25 02:06:25 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Headache3318 on 2026-11-25T01:49:01Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7149989806 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1939#issuecomment-7149989806). Originally posted by [Headache3318](https://github.com/Headache3318) on 2026-11-25T01:49:01Z. --- 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 commented 2026-11-25 03:05:21 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by catminou4 on 2026-11-25T03:05:21Z.

Review: APPROVED


Tested locally: macOS arm64, rustc 1.98.1, PR head merged into main 940d32f69b, 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.

<!-- greyfinch-sync:Marvisak/VIctory:review:6051251526 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1939#pullrequestreview-6051251526). Originally posted by [catminou4](https://github.com/catminou4) on 2026-11-25T03:05:21Z. **Review:** APPROVED --- Tested locally: macOS arm64, rustc 1.98.1, PR head merged into main 940d32f69b, `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 2026-11-25 09:06:09 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T08:16:48Z.


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

<!-- greyfinch-sync:Marvisak/VIctory:comment:7156830902 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1939#issuecomment-7156830902). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T08:16:48Z. --- <!-- victory-triage v1 head=f7a44b768bce0a5dadc1c494d6b0359202d903e1 verdict=merge-candidate at=2026-11-26T07:20:53Z --> 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](https://claude.ai/code)_
ashrafsdrop commented 2026-11-25 11:05:41 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by ashrafsdrop on 2026-11-25T10:54:11Z.


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).
<!-- greyfinch-sync:Marvisak/VIctory:comment:7160325371 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1939#issuecomment-7160325371). Originally posted by [ashrafsdrop](https://github.com/ashrafsdrop) on 2026-11-25T10:54:11Z. --- 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 commented 2026-11-25 14:07:19 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by andrewpozdnakov7-jpg on 2026-11-25T13:28:33Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7163831324 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1939#issuecomment-7163831324). Originally posted by [andrewpozdnakov7-jpg](https://github.com/andrewpozdnakov7-jpg) on 2026-11-25T13:28:33Z. --- The [Windows check](https://github.com/Marvisak/VIctory/actions/runs/45622230295/job/139233105924) on `122f97b` fails because the new readback test constructs `Parts` and `HostShader` directly. Current `main` adds `view_marks` to both. [7fcd6fc](https://github.com/andrewpozdnakov7-jpg/VIctory/commit/7fcd6fc64504bd7d443d96d2bb7e9e51c273db3d) 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](https://github.com/Marvisak/VIctory/issues/1633#issuecomment-7163713854). AI-assisted with Codex. Synthetic shaders only.
ashrafsdrop commented 2026-11-25 16:09:09 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by ashrafsdrop on 2026-11-25T15:21:06Z.


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.
<!-- greyfinch-sync:Marvisak/VIctory:comment:7166705108 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1939#issuecomment-7166705108). Originally posted by [ashrafsdrop](https://github.com/ashrafsdrop) on 2026-11-25T15:21:06Z. --- 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.
fuzeofficlal commented 2026-11-26 15:26:24 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-26T15:26:24Z.

Review: APPROVED


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.

<!-- greyfinch-sync:Marvisak/VIctory:review:6076734169 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1939#pullrequestreview-6076734169). Originally posted by [fuzeofficlal](https://github.com/fuzeofficlal) on 2026-11-26T15:26:24Z. **Review:** APPROVED --- 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 commented 2026-11-26 16:05:07 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by ashrafsdrop on 2026-11-26T15:37:28Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7196525571 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1939#issuecomment-7196525571). Originally posted by [ashrafsdrop](https://github.com/ashrafsdrop) on 2026-11-26T15:37:28Z. --- Thanks for the thorough verification and approval [@fuzeofficlal](https://github.com/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.
Dukestar69 commented 2026-11-26 23:29:15 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Dukestar69 on 2026-11-26T23:29:15Z.

Review: APPROVED


LGTM — verified on RTX 5090 and VICTORY_WARP=1 (Windows / D3D12) against origin/main (63bcca4).

  • Truncating s_buffer_load offset terms independently to dword alignment matches RDNA2 hardware behavior cleanly on both RTX 5090 and WARP.
  • cargo test -p victory-shader --lib emit::valu_tests::s_buffer_load -- --test-threads=1: passed on RTX 5090 and VICTORY_WARP=1.
  • cargo clippy -p victory-shader --all-targets -- -D warnings: clean.
  • cargo xtask policy: 191 files ok.

Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).

<!-- greyfinch-sync:Marvisak/VIctory:review:6081463566 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1939#pullrequestreview-6081463566). Originally posted by [Dukestar69](https://github.com/Dukestar69) on 2026-11-26T23:29:15Z. **Review:** APPROVED --- LGTM — verified on RTX 5090 and `VICTORY_WARP=1` (Windows / D3D12) against `origin/main` (`63bcca4`). - Truncating `s_buffer_load` offset terms independently to dword alignment matches RDNA2 hardware behavior cleanly on both RTX 5090 and WARP. - `cargo test -p victory-shader --lib emit::valu_tests::s_buffer_load -- --test-threads=1`: passed on RTX 5090 and `VICTORY_WARP=1`. - `cargo clippy -p victory-shader --all-targets -- -D warnings`: clean. - `cargo xtask policy`: 191 files ok. Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/1939:upstream-pr/1939'
git switch 'upstream-pr/1939'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/1939'
git switch 'upstream-pr/1939'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/1939'
git switch 'upstream-pr/1939'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/1939'
git switch 'main'
git merge --squash 'upstream-pr/1939'
git switch 'main'
git merge --ff-only 'upstream-pr/1939'
git switch 'main'
git merge 'upstream-pr/1939'
git push origin 'main'
Sign in to join this conversation.
No reviewers
No labels
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.

Reference
Marvisak/VIctory!1937
No description provided.