Skip to content

Shaders: LDS reads and writes of a byte or half - #632

Closed
itsRevela wants to merge 1 commit into
Marvisak:mainfrom
itsRevela:shaders/lds-bytes-halves
Closed

itsRevela wants to merge 1 commit into
Marvisak:mainfrom
itsRevela:shaders/lds-bytes-halves

Conversation

@itsRevela

Copy link
Copy Markdown
Contributor

What

LDS accesses of a byte or a half, which stopped translation with an LDS write of bytes or halves / an LDS read of bytes or halves:

  • ds_write_b8, ds_write_b16, ds_write_b8_d16_hi, ds_write_b16_d16_hi: write only their part of the dword. The _d16_hi forms write the data's high half. An atomic AND clears the part, then an atomic OR sets it, so lanes writing the other parts of the same dword keep theirs.
  • ds_read_u8, ds_read_i8, ds_read_u16, ds_read_i16: load the dword, shift the part down, and zero- or sign-extend it.
  • The d16 reads (ds_read_u8_d16, ..._d16_hi, ...) are still refused. They write half of the register and keep the other half, and the IR records a DS destination as written only, never read. They now fail with an LDS read into half a register.

As for whole dwords, addresses are taken as aligned to the access size.

Tests:

  • emit::valu_tests: build_valu_cs gains an LDS-size variant (build_cs) and a DS encoder, and run_cases a variant that takes the hosts to run on (run_cases_on).

Why

In the opening cutscene, 2 programs per run stopped at these instructions; one of them has one ds_write_b16 and nine ds_read_u16, so writes alone would only have moved the failure to the reads. Each dropped about 130 draws a frame. I found no issue or PR covering them.

One semantic difference: if two lanes write different values to the same byte in one instruction, the clear/set pair can leave the OR of the values, where the hardware keeps one lane's value. Lanes writing different bytes or halves of one dword (byte arrays, packed halves) give the hardware's result exactly.

Tested

Windows 11 Pro 10.0.26200, NVIDIA GeForce RTX 3070 Ti (driver 32.0.16.1088), Rust 1.98.1, on d2a4bae.

  • New emit::valu_tests::lds_bytes_and_halves_match_rdna2 (real GPU). A one-thread program writes a dword to LDS, then:

    • each byte or half write at several offsets, read back as a dword;
    • each byte or half read at several offsets.

    Results are checked against expected values on three input sets, with sign bits set and clear. It runs on hardware only: WARP refuses the compute pipeline of any program with LDS from this harness, a plain ds_write_b32/ds_read_b32 one too, so that's not new here.

  • New emit::lower::tests::lds_bytes_and_halves_lower and lds_reads_into_half_a_register_are_refused. They need no GPU, so CI runs them.

  • The fix matters. All three new tests fail with the two new branches replaced by the old refusals.

  • Crate checks:

    • cargo test -p victory-shader: 72 passed.
    • cargo clippy -p victory-shader --tests: nothing in the changed files.
    • cargo fmt -p victory-shader --check: clean.
    • cargo xtask policy: ok.
  • With the title (diag build, Windows Sandbox vGPU): both programs that failed on main translate. The run logs no LDS byte or half failures; untranslated programs went from 382 to 376 and translated ones from 1,634 to 1,647, with run-to-run variation.

AI-assisted (Claude Code). The title runs used my own copy; no game data is in this PR.

🤖 Generated with Claude Code

@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

Copy link
Copy Markdown
Contributor

Triage note: this overlaps two other open PRs for the same feature — sub-dword LDS accesses (ds_read_{u,i}{8,16}/_d16/_d16_hi, ds_write_b{8,16}/_d16_hi):

This PR (#632) also conflicts with main. Three competing implementations of the same lowering — whichever the maintainer picks, the other two will need rebases. No objection to the approach itself; flagging so the overlap is visible before review time is spent three times.

ds_write_b8, ds_write_b16 and their d16_hi forms replace only their part
of the dword: an atomic AND clears it and an atomic OR sets it, so lanes
writing the other parts of the same dword keep theirs. ds_read_u8,
ds_read_i8, ds_read_u16 and ds_read_i16 load the dword and zero- or
sign-extend their part. The d16 reads, which write half of the register
and keep the other, are still refused, now as "an LDS read into half a
register".

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@itsRevela
itsRevela force-pushed the shaders/lds-bytes-halves branch from 203b11b to 3bd8232 Compare November 24, 2026 12:25

@DenRakEiw DenRakEiw left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested head 3bd8232 (fast-forward on main ffe310e) on Windows 11 x64 (Rust 1.98.1 MSVC) with NVIDIA GeForce RTX 5090 (Blackwell):

  • cargo test -p victory-shader lds_bytes_and_halves_match_rdna2: passed on hardware (exercising all 6 sub-dword write variants ds_write_{b8,b16} / _d16_hi and all 6 sub-dword read variants ds_read_{u8,i8,u16,i16} across 3 bit-pattern cases)
  • cargo clippy -p victory-shader --all-targets: 0 warnings
  • cargo xtask policy: ok

Code review looks clean and well-structured: sub-dword LDS writes use paired Rmw::And + Rmw::Or atomics inside begin_guarded/end_guarded so concurrent lanes writing distinct bytes/halves of the same dword do not clobber each other, and reads properly zero-/sign-extend via (word >> shift) << (32 - bits) >> (32 - bits). Approving.

AI-assisted (Gemini 4 Neon).

@Marvisak

Copy link
Copy Markdown
Owner

Automated triage for the maintainers. Closing because this change is already in main, or main has since replaced the code it patches.

Byte and half LDS accesses are in main's ds(), including the d16 reads this PR still refuses.

If this is mistaken, reply here with what was missed (or push a change and say so) and it will be looked at again. Thank you for the contribution.

@Marvisak Marvisak closed this Nov 25, 2026
geiyiliyabumo pushed a commit to geiyiliyabumo/VIctory that referenced this pull request Nov 25, 2026
Shaders: Drop unread pixel inputs before linking mesh outputs
fornorharry added a commit to NitricAcidDev/VIctory-Omega-Fork that referenced this pull request Nov 25, 2026
…placed view stays

From open pull requests of fxceless/victory-gta6, each reviewed and applied
with its tests:

- Marvisak#644: an RGBA8 shader view that picks sRGB or linear keeps it on a typed
  texture. Extended here to every format with an sRGB twin (BC1, BC2, BC3,
  BC7, BGRA8, BGRX8): material maps are compressed, and a roughness map read
  through the wrong one of the two reads 0.21 for 0.5. Sampled on the GPU in
  all seven formats, both ways; the test fails without it. Views in other
  formats of a texture's family are logged (first 32).
- Marvisak#501: no blending into integer targets. The host refuses such a pipeline
  and its draws were missing.
- Marvisak#742, Marvisak#743: constants and stream-out aliases made afresh once are not kept
  for the next draw that reads something else there.
- Marvisak#632: a texture slot found streamed out is cleared only if the title has
  not written it again since.
- Marvisak#663: a window of bounded indirect arguments ends at copies and writes
  into its memory.

AI-assisted. GPU unit tests pass (RTX 5080). Not run with the title.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

5 participants