Shaders: 16-bit raw buffer loads join their bytes across dwords #3333

Open
Moon0755 wants to merge 1 commit from upstream-pr/3333 into main
Moon0755 commented 2026-11-26 18:50:04 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Moon0755 on 2026-11-26T18:50:04Z.


What

buffer_load_ushort and buffer_load_sshort through a raw view (no format, no index) read one aligned dword and took the half from it. At a byte offset of 3 mod 4 the half's second byte is in the next dword, so only the low byte came back: bits 15:8 were always 0. Such a load now also reads the next dword and takes its byte 0 as the high byte.

At the other three offsets the result is what it was. The second read then repeats the first dword, so no load reads memory it did not read before. Byte loads are unchanged; they cannot straddle.

The code had this marked as shortcut: the field is taken not to cross a dword. This removes that one shortcut and no other.

The open question, and what I can add to it

The triage notes on #1536, #1669 and #1795 leave the same point open: the ISA guide (§8.1.7) puts the alignment of non-formatted ops under SH_MEM_CONFIG.alignment_mode, and nothing says which mode the console's driver sets. I cannot settle that. Two things that bear on it:

  • A measurement of this instruction on RDNA2. AnyPS5 d333572 reports buffer_load_ushort and _sshort on gfx1036 under AMD's Windows driver 32.0.21043.5001: 512 rows with soffset, an immediate offset and a short range, wave32 and wave64, and both bytes come back whenever offset + 2 <= NUM_RECORDS. That is a PC card, not a console, and I take only the stated result from it.
  • main already reads unaligned. A half at byte 1 returns bytes 1 and 2; DWORD mode ("automatic alignment to multiple of the smaller of element size or a dword") would give bytes 0 and 1. At byte 3 main returns byte 3 and a zero, which is neither mode's answer: UNALIGNED gives bytes 3 and 4, DWORD bytes 2 and 3. So this change does not introduce the assumption. It makes byte 3 follow the one byte 1 already follows.

If the console turns out to align down, bytes 1 and 3 both need a different change (clear bit 0 of the address) and this one should be dropped.

How

The first read now goes through the existing raw_dword, the same call it used to spell out. For a 16-bit field the next dword's address is selected when the byte offset is 3 (otherwise the same address again), read with raw_dword, shifted left by 8 and OR-ed in when the byte offset is 3.

lower.rs grows by 373 bytes (520,563 to 520,936).

Evidence

The new test buffer_load_ushort_joins_bytes_across_dwords: 32 lanes read a half at byte 4 * lane + 3 of a raw view whose byte i holds i, and expect (4 * lane + 3) | (4 * lane + 4) << 8.

  • Red, then green in CI (WARP). Test only: run 45874569233, 302 passed, 1 failed, the new test. With the change: run 45876959446, 303 passed, repository_passes_policy ... ok.
  • On two hardware adapters here. Without the change the test fails on the AMD integrated Radeon and on the NVIDIA RTX 2060 the same way: lane values 3, 7, 11, ... instead of 1027, 2055, 3083, .... With it the test passes on both and on WARP.
  • Full victory-shader on the RTX 2060: 300 passed, 3 failed with the change. The three are the ones that fail there on unmodified 63bcca4 (299 and 3).

Other pull requests

  • #3008 conflicts: it moves these lines into a helper, extract_sub. Whichever goes in second needs a small rebase; I will do mine.
  • #1669 (typed formats), #1795 (LDS) and #2003 (_d16 loads) do not merge into main at the moment, so I could not try them together. #2037 and #2208 merge cleanly with this.
  • Not touched, same shortcut elsewhere: flat global_load_ushort/_sshort (#1536, closed by its author), byte and half stores (#2208, #1864).

Not checked

  • The console's alignment mode (above).
  • A half whose second byte is past the end of the buffer. The measurement above says the result is then 0; here the host view's bounds decide, and the low byte still comes back as it did before this change.
  • A half that straddles two host copies of title memory. The second dword is read at + 4 in the same view, as multi-dword loads do; #1536 did a second page lookup for the flat path.
  • buffer_load_sshort and wave64 have no case in the test. The signed form differs only in the final shift, which this does not touch.
  • Whether any shipped shader reads a half at an odd address. I have no corpus to count.

Synthetic tests only; no game files.

— Devin & Fable

<!-- greyfinch-sync:Marvisak/VIctory:issue:5515537781 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3333). Originally posted by [Moon0755](https://github.com/Moon0755) on 2026-11-26T18:50:04Z. --- ### What `buffer_load_ushort` and `buffer_load_sshort` through a raw view (no format, no index) read one aligned dword and took the half from it. At a byte offset of 3 mod 4 the half's second byte is in the next dword, so only the low byte came back: bits 15:8 were always 0. Such a load now also reads the next dword and takes its byte 0 as the high byte. At the other three offsets the result is what it was. The second read then repeats the first dword, so no load reads memory it did not read before. Byte loads are unchanged; they cannot straddle. The code had this marked as `shortcut: the field is taken not to cross a dword`. This removes that one shortcut and no other. ### The open question, and what I can add to it The triage notes on #1536, #1669 and #1795 leave the same point open: the ISA guide (§8.1.7) puts the alignment of non-formatted ops under `SH_MEM_CONFIG.alignment_mode`, and nothing says which mode the console's driver sets. I cannot settle that. Two things that bear on it: - **A measurement of this instruction on RDNA2.** [AnyPS5 `d333572`](https://github.com/boykopovar/AnyPS5/commit/d3335727b90a011044f3d79c88b7d94aabe5d3d4) reports `buffer_load_ushort` and `_sshort` on gfx1036 under AMD's Windows driver 32.0.21043.5001: 512 rows with `soffset`, an immediate offset and a short range, wave32 and wave64, and both bytes come back whenever `offset + 2 <= NUM_RECORDS`. That is a PC card, not a console, and I take only the stated result from it. - **`main` already reads unaligned.** A half at byte 1 returns bytes 1 and 2; `DWORD` mode ("automatic alignment to multiple of the smaller of element size or a dword") would give bytes 0 and 1. At byte 3 `main` returns byte 3 and a zero, which is neither mode's answer: `UNALIGNED` gives bytes 3 and 4, `DWORD` bytes 2 and 3. So this change does not introduce the assumption. It makes byte 3 follow the one byte 1 already follows. If the console turns out to align down, bytes 1 and 3 both need a different change (clear bit 0 of the address) and this one should be dropped. ### How The first read now goes through the existing `raw_dword`, the same call it used to spell out. For a 16-bit field the next dword's address is selected when the byte offset is 3 (otherwise the same address again), read with `raw_dword`, shifted left by 8 and OR-ed in when the byte offset is 3. `lower.rs` grows by 373 bytes (520,563 to 520,936). ### Evidence The new test `buffer_load_ushort_joins_bytes_across_dwords`: 32 lanes read a half at byte `4 * lane + 3` of a raw view whose byte `i` holds `i`, and expect `(4 * lane + 3) | (4 * lane + 4) << 8`. - **Red, then green in CI (WARP).** Test only: [run 45874569233](https://github.com/Moon0755/VIctory/actions/runs/45874569233), 302 passed, 1 failed, the new test. With the change: [run 45876959446](https://github.com/Moon0755/VIctory/actions/runs/45876959446), 303 passed, `repository_passes_policy ... ok`. - **On two hardware adapters here.** Without the change the test fails on the AMD integrated Radeon and on the NVIDIA RTX 2060 the same way: lane values `3, 7, 11, ...` instead of `1027, 2055, 3083, ...`. With it the test passes on both and on WARP. - **Full `victory-shader` on the RTX 2060:** 300 passed, 3 failed with the change. The three are the ones that fail there on unmodified `63bcca4` (299 and 3). ### Other pull requests - #3008 conflicts: it moves these lines into a helper, `extract_sub`. Whichever goes in second needs a small rebase; I will do mine. - #1669 (typed formats), #1795 (LDS) and #2003 (`_d16` loads) do not merge into `main` at the moment, so I could not try them together. #2037 and #2208 merge cleanly with this. - Not touched, same shortcut elsewhere: flat `global_load_ushort`/`_sshort` (#1536, closed by its author), byte and half stores (#2208, #1864). ### Not checked - The console's alignment mode (above). - A half whose second byte is past the end of the buffer. The measurement above says the result is then 0; here the host view's bounds decide, and the low byte still comes back as it did before this change. - A half that straddles two host copies of title memory. The second dword is read at `+ 4` in the same view, as multi-dword loads do; #1536 did a second page lookup for the flat path. - `buffer_load_sshort` and wave64 have no case in the test. The signed form differs only in the final shift, which this does not touch. - Whether any shipped shader reads a half at an odd address. I have no corpus to count. Synthetic tests only; no game files. — Devin & Fable
cloph-dsp commented 2026-11-26 19:03:50 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by cloph-dsp on 2026-11-26T19:03:50Z.


Independent local verification of PR #3333 (Shaders: 16-bit raw buffer loads join their bytes across dwords), head 80e8cede8a.

  • Windows, Rust 1.100.0/MSVC, cargo test --locked, CARGO_BUILD_JOBS=2, per-worktree CARGO_TARGET_DIR (D:\Victory-Contrib\target-3333).
  • cargo test --locked -p victory-shader --lib -- --exact emit::memory_tests::buffer_load_ushort_joins_bytes_across_dwords -> 1 passed, 0 failed, 305 filtered, exit 0.
  • git diff --check origin/main HEAD -> clean.
  • Negative control: the new test name is absent from the merge base 63bcca43..., so it is genuinely added by this PR.
  • CPU-side synthetic lowering test; no game data. Did not independently confirm the console alignment mode discussed in the PR.

AI-assisted (Hy4 flash). GPU/driver: N/A for this CPU test. No game data used.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7201155585 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3333#issuecomment-7201155585). Originally posted by [cloph-dsp](https://github.com/cloph-dsp) on 2026-11-26T19:03:50Z. --- Independent local verification of PR #3333 (`Shaders: 16-bit raw buffer loads join their bytes across dwords`), head 80e8cede8afc24173ab51b3ccbe5a4b703efd799. - Windows, Rust 1.100.0/MSVC, `cargo test --locked`, `CARGO_BUILD_JOBS=2`, per-worktree `CARGO_TARGET_DIR` (`D:\Victory-Contrib\target-3333`). - `cargo test --locked -p victory-shader --lib -- --exact emit::memory_tests::buffer_load_ushort_joins_bytes_across_dwords` -> 1 passed, 0 failed, 305 filtered, exit 0. - `git diff --check origin/main HEAD` -> clean. - Negative control: the new test name is absent from the merge base 63bcca43..., so it is genuinely added by this PR. - CPU-side synthetic lowering test; no game data. Did not independently confirm the console alignment mode discussed in the PR. AI-assisted (Hy4 flash). GPU/driver: N/A for this CPU test. No game data used.
Dukestar69 commented 2026-11-26 19:10:41 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Dukestar69 on 2026-11-26T19:10:41Z.

Review: APPROVED


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

  • Joining byte 3 of at with byte 0 of at + 4 for straddling 16-bit raw buffer loads (while reusing at when non-straddling to avoid reading past the buffer end) cleanly fixes unaligned 16-bit loads without introducing out-of-bounds reads.
  • cargo test -p victory-shader --lib emit::memory_tests:: -- --skip heap_direct_texture_reads_mark_their_heap_index --test-threads=1: 5 passed, 0 failed on RTX 5090 and 5 passed, 0 failed on VICTORY_WARP=1 (including buffer_load_ushort_joins_bytes_across_dwords).
  • 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:6079350560 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3333#pullrequestreview-6079350560). Originally posted by [Dukestar69](https://github.com/Dukestar69) on 2026-11-26T19:10:41Z. **Review:** APPROVED --- LGTM — verified on RTX 5090 and `VICTORY_WARP=1` (Windows / D3D12) against `origin/main` (`63bcca4`). - Joining byte 3 of `at` with byte 0 of `at + 4` for straddling 16-bit raw buffer loads (while reusing `at` when non-straddling to avoid reading past the buffer end) cleanly fixes unaligned 16-bit loads without introducing out-of-bounds reads. - `cargo test -p victory-shader --lib emit::memory_tests:: -- --skip heap_direct_texture_reads_mark_their_heap_index --test-threads=1`: 5 passed, 0 failed on RTX 5090 and 5 passed, 0 failed on `VICTORY_WARP=1` (including `buffer_load_ushort_joins_bytes_across_dwords`). - `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).
fuzeofficlal commented 2026-11-26 20:14:52 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-26T20:14:52Z.

Review: APPROVED


Looks good to me — joining byte 3 of the aligned dword (>> 24) with byte 0 of at + 4 (<< 8) for straddling 16-bit raw buffer loads while re-reading at when non-straddling cleanly fixes buffer_load_ushort/sshort across dword boundaries without reading extra memory on non-straddling lanes.

Verified locally on Windows 11 with head 80e8cede cleanly merged onto main (63bcca43):

  • Byte-size & audit: crates/victory-shader/src/emit/lower.rs is 520,936 bytes (3,352 bytes below 524,288); bit extraction (shl (32 - width) + lshr/ashr) and sign/zero extension in lower.rs:9758-9775 match in-bounds semantics for both ushort and sshort.
  • cargo run -p xtask -- policy (191 files ok) and cargo clippy --locked -p victory-shader --all-targets -- -D warnings (both default and --all-features) pass.
  • cargo test --locked -p victory-shader --lib emit::memory_tests (with Windows Kits\10\bin\10.0.26100.0\x64\dxil.dll present):
    • VICTORY_WARP=0 (--skip heap_direct_texture_reads_mark_their_heap_index): 5 passed; 0 failed (1.70s, including buffer_load_ushort_joins_bytes_across_dwords).
    • VICTORY_WARP=1 (full emit::memory_tests): 6 passed; 0 failed (0.32s).
  • cargo test --locked -p victory-shader --lib emit::lower: 33 passed; 0 failed (0.02s).

Minor non-blocking notes:

  • rustfmt --edition 2024 --check flags one long line on the validation call at crates/victory-shader/src/emit/memory_tests.rs:1049; the added lines in lower.rs are clean.
  • Consistent with your "Not checked" list, for title-memory (root_address) reads straddling a 64 KiB page boundary or 0xFFFF_FFFC, the second raw_dword reuses the first view handle h rather than re-resolving at + 4, and for bounded raw buffers at the exact last byte (size - 1), per-dword raw_dword zeroing can return 0x00AB rather than 0.
<!-- greyfinch-sync:Marvisak/VIctory:review:6080025895 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3333#pullrequestreview-6080025895). Originally posted by [fuzeofficlal](https://github.com/fuzeofficlal) on 2026-11-26T20:14:52Z. **Review:** APPROVED --- Looks good to me — joining byte 3 of the aligned dword (`>> 24`) with byte 0 of `at + 4` (`<< 8`) for straddling 16-bit raw buffer loads while re-reading `at` when non-straddling cleanly fixes `buffer_load_ushort`/`sshort` across dword boundaries without reading extra memory on non-straddling lanes. Verified locally on Windows 11 with head `80e8cede` cleanly merged onto `main` (`63bcca43`): - Byte-size & audit: `crates/victory-shader/src/emit/lower.rs` is `520,936` bytes (`3,352` bytes below `524,288`); bit extraction (`shl (32 - width)` + `lshr`/`ashr`) and sign/zero extension in `lower.rs:9758-9775` match in-bounds semantics for both `ushort` and `sshort`. - `cargo run -p xtask -- policy` (`191 files ok`) and `cargo clippy --locked -p victory-shader --all-targets -- -D warnings` (both default and `--all-features`) pass. - `cargo test --locked -p victory-shader --lib emit::memory_tests` (with `Windows Kits\10\bin\10.0.26100.0\x64\dxil.dll` present): - `VICTORY_WARP=0` (`--skip heap_direct_texture_reads_mark_their_heap_index`): `5 passed; 0 failed` (`1.70s`, including `buffer_load_ushort_joins_bytes_across_dwords`). - `VICTORY_WARP=1` (full `emit::memory_tests`): `6 passed; 0 failed` (`0.32s`). - `cargo test --locked -p victory-shader --lib emit::lower`: `33 passed; 0 failed` (`0.02s`). Minor non-blocking notes: - `rustfmt --edition 2024 --check` flags one long line on the validation call at `crates/victory-shader/src/emit/memory_tests.rs:1049`; the added lines in `lower.rs` are clean. - Consistent with your "Not checked" list, for title-memory (`root_address`) reads straddling a 64 KiB page boundary or `0xFFFF_FFFC`, the second `raw_dword` reuses the first view handle `h` rather than re-resolving `at + 4`, and for bounded raw buffers at the exact last byte (`size - 1`), per-dword `raw_dword` zeroing can return `0x00AB` rather than `0`.
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/3333:upstream-pr/3333'
git switch 'upstream-pr/3333'

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/3333'
git switch 'upstream-pr/3333'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/3333'
git switch 'upstream-pr/3333'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/3333'
git switch 'main'
git merge --squash 'upstream-pr/3333'
git switch 'main'
git merge --ff-only 'upstream-pr/3333'
git switch 'main'
git merge 'upstream-pr/3333'
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!3333
No description provided.