Shaders: lower sub-dword LDS reads and writes #101

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

@⁠Yaekai on GitHub · 2026-11-24 00:30 UTC · original
Yaekai:feat/shader-lds-subdword → main · +192 −33 · 1 files · 2 commits · closed without merging

Lower byte and 16-bit LDS reads and writes in crates/victory-shader/src/emit/lower.rs:

  • Sub-dword reads (ds_read_u8, ds_read_i8, ds_read_u16, ds_read_i16, and the _d16 / _d16_hi variants): load the enclosing 32-bit LDS dword (addr >> 2), shift by the byte/half offset within the dword, zero- or sign-extend to 32 bits, and for _d16 / _d16_hi preserve the untouched half of the destination VGPR (via low_half / new high_half).
  • Sub-dword writes (ds_write_b8, ds_write_b16, ds_write_b8_d16_hi, ds_write_b16_d16_hi): extract the source byte/halfword and update the enclosing 32-bit LDS dword with atomic And (clear mask) followed by atomic Or (shifted value) inside the execution guard so concurrent lanes writing adjacent bytes or halves within the same 32-bit dword do not race.

Verification

  • cargo test -p victory-shader — 57/57 passed.
  • rustfmt --edition 2024 --check crates/victory-shader/src/emit/lower.rs — clean.
  • victory translate --validate on shaders.rpf (26,084 programs): cleared all 9 an LDS write of bytes or halves and all 7 an LDS read of bytes or halves failures, increasing emitted & dxil.dll-validated compute shaders from 1,693 → 1,709 emitted and 1,670 → 1,686 validated (+16/16 validated, 0 new validator errors), including live world compute shader grass_batch_floating_veg_tess:MSTS_VS_Transform_AddWaterSurfaceDelta_e4f46870_Wrapped.
**@⁠Yaekai** on GitHub · 2026-11-24 00:30 UTC · [original](https://github.com/Marvisak/VIctory/pull/101) `Yaekai:feat/shader-lds-subdword` → `main` · +192 −33 · 1 files · 2 commits · closed without merging Lower byte and 16-bit LDS reads and writes in `crates/victory-shader/src/emit/lower.rs`: - **Sub-dword reads** (`ds_read_u8`, `ds_read_i8`, `ds_read_u16`, `ds_read_i16`, and the `_d16` / `_d16_hi` variants): load the enclosing 32-bit LDS dword (`addr >> 2`), shift by the byte/half offset within the dword, zero- or sign-extend to 32 bits, and for `_d16` / `_d16_hi` preserve the untouched half of the destination VGPR (via `low_half` / new `high_half`). - **Sub-dword writes** (`ds_write_b8`, `ds_write_b16`, `ds_write_b8_d16_hi`, `ds_write_b16_d16_hi`): extract the source byte/halfword and update the enclosing 32-bit LDS dword with atomic `And` (clear mask) followed by atomic `Or` (shifted value) inside the execution guard so concurrent lanes writing adjacent bytes or halves within the same 32-bit dword do not race. ### Verification - `cargo test -p victory-shader` — 57/57 passed. - `rustfmt --edition 2024 --check crates/victory-shader/src/emit/lower.rs` — clean. - `victory translate --validate` on `shaders.rpf` (26,084 programs): cleared all 9 `an LDS write of bytes or halves` and all 7 `an LDS read of bytes or halves` failures, increasing emitted & `dxil.dll`-validated compute shaders from **1,693 → 1,709 emitted** and **1,670 → 1,686 validated** (+16/16 validated, 0 new validator errors), including live world compute shader `grass_batch_floating_veg_tess:MSTS_VS_Transform_AddWaterSurfaceDelta_e4f46870_Wrapped`.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 03:50:17 +00:00
github-bot closed this pull request 2026-11-26 03:50:17 +00:00
Author
Owner

@⁠Raxstreams on GitHub · 2026-11-24 00:34 UTC · original

Testing this right now, I ran into the same bug

**@⁠Raxstreams** on GitHub · 2026-11-24 00:34 UTC · [original](https://github.com/Marvisak/VIctory/pull/101#issuecomment-7118208270) Testing this right now, I ran into the same bug
Author
Owner

@⁠DaviTostes on GitHub · 2026-11-24 00:36 UTC · original

Tested c78ccf7 stacked on #39 on Arch Linux x86_64, rustc 1.98.1: pass (no regression only).

  • cargo test --workspace: 155 passed, 0 failed, 3 ignored; merge with #39 clean.
  • The diff adds no test for the sub-dword LDS paths, so this run only shows the existing shader tests stay green; a synthetic case with a byte/halfword read and write at an odd LDS offset would cover the change.
  • No game data used.

AI-assisted (DeepSeek V4.1 Flash coding agent).

**@⁠DaviTostes** on GitHub · 2026-11-24 00:36 UTC · [original](https://github.com/Marvisak/VIctory/pull/101#issuecomment-7118246572) Tested `c78ccf7` stacked on **#39** on **Arch Linux x86_64, rustc 1.98.1**: **pass (no regression only)**. - `cargo test --workspace`: **155 passed, 0 failed, 3 ignored**; merge with #39 clean. - The diff adds no test for the sub-dword LDS paths, so this run only shows the existing shader tests stay green; a synthetic case with a byte/halfword read and write at an odd LDS offset would cover the change. - No game data used. AI-assisted (DeepSeek V4.1 Flash coding agent).
Author
Owner

@⁠Raxstreams on GitHub · 2026-11-24 00:36 UTC · original

This made it further but I'm still not getting a world render but the PR is sound.

**@⁠Raxstreams** on GitHub · 2026-11-24 00:36 UTC · [original](https://github.com/Marvisak/VIctory/pull/101#issuecomment-7118247597) This made it further but I'm still not getting a world render but the PR is sound.
Author
Owner

@⁠DaviTostes on GitHub · 2026-11-24 00:50 UTC · original

Follow-up after looking at the rest of the queue:

  • #116 ("byte and halfword LDS reads and writes") implements the same lowering and brings a new crates/victory-shader/src/emit/lds_tests.rs (434 lines) with it; this branch adds no #[test]s. Whichever of the two goes in, the other should be closed so the author's work is not repeated.
  • This branch also predates #45: git diff main pr-101 removes 209 lines from crates/victory-loader/src/crash.rs (the CLWB handler). Merging keeps main's version, but the PR as displayed should be rebased so reviewers see only the LDS change.
  • The LDS lowering itself passes: cargo test -p victory-shader on this head is green on Linux if the crate builds alone; in the full workspace the victory-gpu E0425 still needs #112. Stacked on #39 earlier it was 155 passed / 0 failed (no new tests).

No game data used. AI-assisted (DeepSeek V4.1 Flash coding agent).

**@⁠DaviTostes** on GitHub · 2026-11-24 00:50 UTC · [original](https://github.com/Marvisak/VIctory/pull/101#issuecomment-7118490556) Follow-up after looking at the rest of the queue: - **#116 ("byte and halfword LDS reads and writes") implements the same lowering** and brings a new `crates/victory-shader/src/emit/lds_tests.rs` (434 lines) with it; this branch adds no `#[test]`s. Whichever of the two goes in, the other should be closed so the author's work is not repeated. - This branch also predates #45: `git diff main pr-101` removes 209 lines from `crates/victory-loader/src/crash.rs` (the CLWB handler). Merging keeps main's version, but the PR as displayed should be rebased so reviewers see only the LDS change. - The LDS lowering itself passes: `cargo test -p victory-shader` on this head is green on Linux **if** the crate builds alone; in the full workspace the `victory-gpu` E0425 still needs #112. Stacked on #39 earlier it was 155 passed / 0 failed (no new tests). No game data used. AI-assisted (DeepSeek V4.1 Flash coding agent).
Author
Owner

@⁠Yaekai on GitHub · 2026-11-24 02:31 UTC · original

Thanks @⁠DaviTostes! Rebased cleanly onto d2a4bae and added lds_subdword_reads_and_writes_lower in 4baaee7 covering odd-offset byte/halfword writes and all sub-dword read / _d16 / _d16_hi variants (cargo test -p victory-shader: 71/71 passed).

**@⁠Yaekai** on GitHub · 2026-11-24 02:31 UTC · [original](https://github.com/Marvisak/VIctory/pull/101#issuecomment-7120234254) Thanks @⁠DaviTostes! Rebased cleanly onto `d2a4bae` and added `lds_subdword_reads_and_writes_lower` in `4baaee7` covering odd-offset byte/halfword writes and all sub-dword read / `_d16` / `_d16_hi` variants (`cargo test -p victory-shader`: 71/71 passed).
Author
Owner

@⁠ghost on GitHub · 2026-11-24 05:50 UTC · original

Review note on head 4baaee7: for ds_read_*_d16 / _d16_hi, low_half (crates/victory-shader/src/emit/lower.rs:5290) keeps the destination's other half by reading its old value with inst.read(Reg::V(reg)), and falls back to 0 when that is None.

On main, ir.rs effects() (the Fields::Ds arm, around line 842) adds a ds_read destination only to the written registers, and Inst::read only searches the read list. So for these reads inst.read is always None and the kept half is always zeroed: a _d16 read clears the high 16 bits, a _d16_hi read clears the low 16.

#116 fixes this in effects():

if name.starts_with("ds_read") && name.contains("_d16") {
    regs(d, &mut r)?;
}

This PR would need the same, plus a test that writes a known value to the destination first and checks the other half survives.

Read-only review, nothing built. AI-assisted (Claude Code); the claim above was checked against this head and main at d2a4bae. Context: the triage in #366.

**@⁠ghost** on GitHub · 2026-11-24 05:50 UTC · [original](https://github.com/Marvisak/VIctory/pull/101#issuecomment-7123329686) Review note on head `4baaee7`: for `ds_read_*_d16` / `_d16_hi`, `low_half` (`crates/victory-shader/src/emit/lower.rs:5290`) keeps the destination's other half by reading its old value with `inst.read(Reg::V(reg))`, and falls back to 0 when that is `None`. On `main`, `ir.rs` `effects()` (the `Fields::Ds` arm, around line 842) adds a `ds_read` destination only to the written registers, and `Inst::read` only searches the read list. So for these reads `inst.read` is always `None` and the kept half is always zeroed: a `_d16` read clears the high 16 bits, a `_d16_hi` read clears the low 16. #116 fixes this in `effects()`: ```rust if name.starts_with("ds_read") && name.contains("_d16") { regs(d, &mut r)?; } ``` This PR would need the same, plus a test that writes a known value to the destination first and checks the other half survives. _Read-only review, nothing built. AI-assisted (Claude Code); the claim above was checked against this head and `main` at `d2a4bae`. Context: the triage in #366._
Author
Owner

@⁠slint6614-dev on GitHub · 2026-11-24 07:47 UTC · original

Measured on the full retail shader pack (26,503 containers) with victory translate --validate, against main d2a4bae (emitted / validated). Windows 11, dxil.dll from SDK 10.0.26100.

VS/PS CS MS sub-dword LDS refusals (7 reads, 9 writes on main) tests
main 12272 / 12172 1693 / 1670 8191 / 8129 16 ok
#101 same 1709 / 1686 same 0 ok
#632 same 1707 / 1684 same 0, but 2 new "an LDS read into half a register" ok
#383 12361 / 12252 1720 / 1697 8220 / 8158 0 see below
  • #101 turns all 16 into validated compute programs; #632 gets 14 of them.
  • #383 (wider PR) also clears image_sample_o, image_sample_d_cl, the 108 biased 16-bit samples and the 573 "scalar memory read as data", but those mostly move to other reasons ("a descriptor's address or format read as data" 71 → 595, "an image address" 2 → 27), and the signature mismatches go 21 → 29 (input) and 25 → 26 (output).
  • #383: cargo test -p victory-shader --release crashed with STATUS_ACCESS_VIOLATION in 2 of 6 parallel runs (0 of 6 on main; --test-threads=1 always passes, 76 tests). Possibly a race in one of the new tests.
**@⁠slint6614-dev** on GitHub · 2026-11-24 07:47 UTC · [original](https://github.com/Marvisak/VIctory/pull/101#issuecomment-7125371630) Measured on the full retail shader pack (26,503 containers) with `victory translate --validate`, against main d2a4bae (emitted / validated). Windows 11, dxil.dll from SDK 10.0.26100. | | VS/PS | CS | MS | sub-dword LDS refusals (7 reads, 9 writes on main) | tests | |---|---|---|---|---|---| | main | 12272 / 12172 | 1693 / 1670 | 8191 / 8129 | 16 | ok | | #101 | same | 1709 / 1686 | same | 0 | ok | | #632 | same | 1707 / 1684 | same | 0, but 2 new "an LDS read into half a register" | ok | | #383 | 12361 / 12252 | 1720 / 1697 | 8220 / 8158 | 0 | see below | - #101 turns all 16 into validated compute programs; #632 gets 14 of them. - #383 (wider PR) also clears `image_sample_o`, `image_sample_d_cl`, the 108 biased 16-bit samples and the 573 "scalar memory read as data", but those mostly move to other reasons ("a descriptor's address or format read as data" 71 → 595, "an image address" 2 → 27), and the signature mismatches go 21 → 29 (input) and 25 → 26 (output). - #383: `cargo test -p victory-shader --release` crashed with `STATUS_ACCESS_VIOLATION` in 2 of 6 parallel runs (0 of 6 on main; `--test-threads=1` always passes, 76 tests). Possibly a race in one of the new tests.
Author
Owner

@⁠alexdeliu on GitHub · 2026-11-24 17:22 UTC · original

Merged onto main @ ffe310eb, this PR (9fbd7e9f) no longer compiles its tests. crates/victory-shader/src/emit/lower.rs:7471 calls resource::track with 3 arguments. Since 6638098c it takes a 4th, lanes: Option<&Structured>; main's tests pass Some(&st) with let st = structure::structure(&p).unwrap();.

Checked with CI's steps (both clippy runs with -D warnings, cargo test --workspace) on Windows 11, AMD RX 9070. Every open PR: https://gist.github.com/alexdeliu/ef9ef0ebc1776ff104b5aa8eaea18e87

**@⁠alexdeliu** on GitHub · 2026-11-24 17:22 UTC · [original](https://github.com/Marvisak/VIctory/pull/101#issuecomment-7139101787) Merged onto `main` @ `ffe310eb`, this PR (`9fbd7e9f`) no longer compiles its tests. `crates/victory-shader/src/emit/lower.rs:7471` calls `resource::track` with 3 arguments. Since 6638098c it takes a 4th, `lanes: Option<&Structured>`; main's tests pass `Some(&st)` with `let st = structure::structure(&p).unwrap();`. <sub>Checked with CI's steps (both clippy runs with `-D warnings`, `cargo test --workspace`) on Windows 11, AMD RX 9070. Every open PR: https://gist.github.com/alexdeliu/ef9ef0ebc1776ff104b5aa8eaea18e87</sub>
Author
Owner

@⁠fuzeofficlal on GitHub · 2026-11-24 17:28 UTC · original

Reviewed 9fbd7e9 and ran sub-dword LDS roundtrip + cross-lane exchange tests on NVIDIA GeForce RTX 5090, AMD Radeon(TM) Graphics iGPU, Microsoft D3D12 WARP, and dxil.dll (VICTORY_D3D12_DEBUG=1).

Found three issues — including a silent 16-bit odd-byte / cross-dword truncation bug — and verified a clean 32-bit fix on top of 9fbd7e9 in fuzeofficlal/VIctory@5b98cf2 (branch fix/sub-dword-lds-d16-101):

  1. 16-bit odd-byte (at & 3 == 1) and cross-dword (at & 3 == 3) truncation (emit/lower.rs:2204, 2264):
    • let lane_mask = self.s.m.i32(if bytes == 1 { 3 } else { 2 }); masks bit 0 off 16-bit byte addresses:
      • At at % 4 == 1, ds_read_{u16,i16}* / ds_write_b16* read or overwrite bytes[0..2] instead of bytes[1..3].
      • At at % 4 == 3, RDNA2 hardware spans the high byte of lds[index] and the low byte of lds[index + 1] (reading 0 / dropping writes past the end of LDS), whereas & 2 rounds back to bytes[2..4].
    • Using off_in_word = at & 3 plus a pure 32-bit begin_guarded(Some(off_in_word == 3 && index + 1 < lds_dwords)) block for bytes == 2 handles both byte 1 and byte 3 accurately without enabling flags::INT64 or issuing extra atomics on aligned accesses.
  2. Explicit destination read dependency for ds_read_*_d16 / _d16_hi (ir.rs:843):
    • Recording regs(d, &mut r)? when name.starts_with("ds_read") && name.contains("_d16") documents the untouched-half read dependency directly on the instruction rather than relying only on the trailing implicit EXEC vector destination read.
  3. CI compile error after #566 (emit/lower.rs:7471):
    • lds_subdword_reads_and_writes_lower needs let st = structure::structure(&p).unwrap(); passed as the 4th argument to resource::track(&p, &native.layout, native.stage, Some(&st)).

Verification (5b98cf2)

  • dxil.dll validation + container hash check: passed
  • 64-thread cross-lane ds_write_b8 / ds_write_b16 + chained ds_read_u16_d16 / ds_read_u16_d16_hi + odd-offset (1, 3, 511 end-of-LDS) roundtrip tests with VICTORY_D3D12_DEBUG=1 (0 debug layer errors):
    • NVIDIA GeForce RTX 5090: passed
    • AMD Radeon(TM) Graphics: passed
    • D3D12 WARP: passed
**@⁠fuzeofficlal** on GitHub · 2026-11-24 17:28 UTC · [original](https://github.com/Marvisak/VIctory/pull/101#issuecomment-7139236711) Reviewed `9fbd7e9` and ran sub-dword LDS roundtrip + cross-lane exchange tests on **NVIDIA GeForce RTX 5090**, **AMD Radeon(TM) Graphics iGPU**, **Microsoft D3D12 WARP**, and **`dxil.dll`** (`VICTORY_D3D12_DEBUG=1`). Found three issues — including a silent 16-bit odd-byte / cross-dword truncation bug — and verified a clean 32-bit fix on top of `9fbd7e9` in [`fuzeofficlal/VIctory@5b98cf2`](https://github.com/fuzeofficlal/VIctory/commit/5b98cf29a723cca480daf4d9bb094e3394999c83) (branch [`fix/sub-dword-lds-d16-101`](https://github.com/fuzeofficlal/VIctory/tree/fix/sub-dword-lds-d16-101)): 1. **16-bit odd-byte (`at & 3 == 1`) and cross-dword (`at & 3 == 3`) truncation (`emit/lower.rs:2204, 2264`)**: - `let lane_mask = self.s.m.i32(if bytes == 1 { 3 } else { 2 });` masks bit 0 off 16-bit byte addresses: - At `at % 4 == 1`, `ds_read_{u16,i16}*` / `ds_write_b16*` read or overwrite `bytes[0..2]` instead of `bytes[1..3]`. - At `at % 4 == 3`, RDNA2 hardware spans the high byte of `lds[index]` and the low byte of `lds[index + 1]` (reading `0` / dropping writes past the end of LDS), whereas `& 2` rounds back to `bytes[2..4]`. - Using `off_in_word = at & 3` plus a pure 32-bit `begin_guarded(Some(off_in_word == 3 && index + 1 < lds_dwords))` block for `bytes == 2` handles both `byte 1` and `byte 3` accurately without enabling `flags::INT64` or issuing extra atomics on aligned accesses. 2. **Explicit destination read dependency for `ds_read_*_d16` / `_d16_hi` (`ir.rs:843`)**: - Recording `regs(d, &mut r)?` when `name.starts_with("ds_read") && name.contains("_d16")` documents the untouched-half read dependency directly on the instruction rather than relying only on the trailing implicit `EXEC` vector destination read. 3. **CI compile error after `#566` (`emit/lower.rs:7471`)**: - `lds_subdword_reads_and_writes_lower` needs `let st = structure::structure(&p).unwrap();` passed as the 4th argument to `resource::track(&p, &native.layout, native.stage, Some(&st))`. ### Verification (`5b98cf2`) - `dxil.dll` validation + container hash check: passed - 64-thread cross-lane `ds_write_b8` / `ds_write_b16` + chained `ds_read_u16_d16` / `ds_read_u16_d16_hi` + odd-offset (`1`, `3`, `511` end-of-LDS) roundtrip tests with `VICTORY_D3D12_DEBUG=1` (`0` debug layer errors): - **NVIDIA GeForce RTX 5090**: passed - **AMD Radeon(TM) Graphics**: passed - **D3D12 WARP**: passed

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