Skip to content

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

Closed
Yaekai wants to merge 2 commits into
Marvisak:mainfrom
Yaekai:feat/shader-lds-subdword
Closed

Yaekai wants to merge 2 commits into
Marvisak:mainfrom
Yaekai:feat/shader-lds-subdword

Conversation

@Yaekai

@Yaekai Yaekai commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

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.

@Raxstreams

Copy link
Copy Markdown

Testing this right now, I ran into the same bug

@DaviTostes

Copy link
Copy Markdown

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 Build: make workspace tests run on Linux #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).

@Raxstreams

Copy link
Copy Markdown

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

@DaviTostes

Copy link
Copy Markdown

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

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

@Yaekai

Yaekai commented Nov 24, 2026

Copy link
Copy Markdown
Contributor Author

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

@ghost

ghost commented Nov 24, 2026

Copy link
Copy Markdown

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.

@slint6614-dev

Copy link
Copy Markdown

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

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

Merges cleanly with current main (8e79c80); cargo test -p victory-shader --locked on macOS arm64 / rustc 1.98.1 → 71 passed / 0 failed (1 ignored, Windows execution).

Sub-dword LDS lowering is careful: reads extract the byte/half by (addr & mask) * 8 shift with mask or sign extension, _d16/_d16_hi preserve the untouched half via low_half/high_half, and writes do an atomic clear+set (Rmw::And then Rmw::Or) so concurrent lanes writing adjacent bytes in one dword don't race — that's the right call for LDS. The // shortcut: comment honestly states the alignment/bounds assumption. The new lowering test builds a compute shader exercising every read/write form; the GPU execution check is Windows-gated for CI.

@alexdeliu

Copy link
Copy Markdown
Contributor

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 6638098 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

@fuzeofficlal

Copy link
Copy Markdown
Contributor

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

@Marvisak Marvisak closed this Nov 24, 2026
HoLa-Lee added a commit to HoLa-Lee/victory-wave-wide-barriers that referenced this pull request Nov 25, 2026
Records what an offline translation of the whole shader pack shows, and it
changes how the barrier change should be read: 26,084 of 26,084 programs
translate, and not one of them enters widen - no WAVE64-WIDE line with
VICTORY_WAVE_WHY=1, offline or in a menus-only run. The change is therefore
inert on this pack (it alters no translation) and unproven for Marvisak#101, whose
switch must reach programs this machine cannot.

Also records two negatives that came out of the traces: the first-run flow
touches four title paths and all of them are directories (it never asks to
write a file), and XPackage never has a missing slot called, plus that
movies.rpf is never handed to Bink.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants