Shaders/Lower: align and extract sub-dword buffer_load_{ubyte,sbyte,ushort,sshort} on CBVs #3008

Open
github-bot wants to merge 1 commit from github-bot/gh-3008 into main AGit
Owner

@⁠RVZO6 on GitHub · 2026-11-26 03:19 UTC · original
RVZO6:rvzo/cbv-subdword-loads → main · +379 −20 · 2 files · 1 commits · open

Lower::buffer's early-return path for ResourceType::Cbv (crates/victory-shader/src/emit/lower.rs:9481-9523) previously ignored sub = Some((8 | 16, signed)):

  • Table CBV (RootType::Table): passed unaligned byte offsets directly to rawBufferLoad.f32 with align = 4 and returned the full unshifted 32-bit dword.
  • Root CBV (RootType::Descriptor): called cbuffer_dword() and returned all 32 bits of the containing dword without shifting right by (byte & 3) * 8 or zero-/sign-extending the 8- or 16-bit lane.

Fix:

  • Factor Lower::extract_sub(w, byte, width, signed) shared with the SRV/UAV sub-dword path (keeping lower.rs at 521,524 B, well under the 512 KiB asset-policy limit).
  • Align table CBV sub-dword loads to 4 bytes (byte & !3), refresh let c = self.cur after raw_view, and apply extract_sub on both table and root CBVs.
  • Guard the CBV early return with && !store && atomic.is_none().

Verification

  • Negative control (before fix): cbv_subdword_dxil_distinguishes_width_and_signedness failed (table_cbv: ubyte and sbyte must differ) — ubyte, sbyte, ushort, sshort emitted identical DXIL on CBVs.
  • After fix: all 4 instructions (buffer_load_{ubyte,sbyte,ushort,sshort}) across both table CBV and root CBV pass dxil.dll validation and execute bit-exact zero-/sign-extended byte & short reads on WARP SM 6.6 in emit/lower/cbv_subdword_tests.rs.
  • Official Windows MSVC + WARP SM 6.6 CI (Rust 1.98.1, default + --tests + --features diagnostics clippy, xtask policy, full workspace tests): RVZO6/VIctory CI run #45747688145 (green, combined Wave 9 check on 63bcca4).
  • Not run: the title; any hardware GPU.

AI-assisted: T3 Code (Antigravity, gemini-4-neon).

**@⁠RVZO6** on GitHub · 2026-11-26 03:19 UTC · [original](https://github.com/Marvisak/VIctory/pull/3008) `RVZO6:rvzo/cbv-subdword-loads` → `main` · +379 −20 · 2 files · 1 commits · open `Lower::buffer`'s early-return path for `ResourceType::Cbv` (`crates/victory-shader/src/emit/lower.rs:9481-9523`) previously ignored `sub = Some((8 | 16, signed))`: - **Table CBV (`RootType::Table`):** passed unaligned byte offsets directly to `rawBufferLoad.f32` with `align = 4` and returned the full unshifted 32-bit dword. - **Root CBV (`RootType::Descriptor`):** called `cbuffer_dword()` and returned all 32 bits of the containing dword without shifting right by `(byte & 3) * 8` or zero-/sign-extending the 8- or 16-bit lane. Fix: - Factor `Lower::extract_sub(w, byte, width, signed)` shared with the SRV/UAV sub-dword path (keeping `lower.rs` at `521,524 B`, well under the 512 KiB asset-policy limit). - Align table CBV sub-dword loads to 4 bytes (`byte & !3`), refresh `let c = self.cur` after `raw_view`, and apply `extract_sub` on both table and root CBVs. - Guard the CBV early return with `&& !store && atomic.is_none()`. ### Verification - **Negative control (before fix):** `cbv_subdword_dxil_distinguishes_width_and_signedness` failed (`table_cbv: ubyte and sbyte must differ`) — `ubyte`, `sbyte`, `ushort`, `sshort` emitted identical DXIL on CBVs. - **After fix:** all 4 instructions (`buffer_load_{ubyte,sbyte,ushort,sshort}`) across both table CBV and root CBV pass `dxil.dll` validation and execute bit-exact zero-/sign-extended byte & short reads on **WARP SM 6.6** in `emit/lower/cbv_subdword_tests.rs`. - Official Windows MSVC + WARP SM 6.6 CI (Rust 1.98.1, default + `--tests` + `--features diagnostics` clippy, `xtask policy`, full workspace tests): [RVZO6/VIctory CI run #45747688145](https://github.com/RVZO6/VIctory/actions/runs/45747688145) (green, combined Wave 9 check on `63bcca4`). - **Not run:** the title; any hardware GPU. AI-assisted: T3 Code (Antigravity, gemini-4-neon).
Lower::buffer's early-return path for ResourceType::Cbv previously ignored
`sub = Some((8 | 16, signed))`:
- Table CBVs passed unaligned byte offsets directly to rawBufferLoad.f32
  with align = 4 and returned the full 32-bit dword.
- Root CBVs called cbuffer_dword() and returned all 32 bits of the
  containing dword without shifting by `(byte & 3) * 8` or zero-/sign-
  extending the 8- or 16-bit lane.

Factor out `Lower::extract_sub` shared with the SRV/UAV sub-dword path,
align table CBV sub-dword loads to 4 bytes (and refresh `self.cur` after
`raw_view`), and apply `extract_sub` on both table and root CBVs.
Tests live in `emit/lower/cbv_subdword_tests.rs` (keeping `lower.rs`
under the 512 KiB policy limit) covering DXIL validation and end-to-end
WARP SM 6.6 execution for all 4 ops across both table and root CBVs.

Assisted-by: T3 Code (Antigravity, gemini-4-neon)
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 05:19:59 +00:00
Author
Owner

@⁠Marvisak on GitHub · 2026-11-26 06:16 UTC · original

Automated triage for the maintainers.

The main fix looks right. In main, the CBV early return in Lower::buffer (emit/lower.rs) ignores sub, so buffer_load_{ubyte,sbyte,ushort,sshort} on a constant buffer returns the whole containing dword. The PR fixes both paths:

  • Table path: it aligns the address to 4 bytes and then shifts and extends.
  • Root path: cbuffer_dword already selects the dword from an unaligned address, and the PR adds the shift and extend.

The new shared extract_sub helper is the same code the SRV/UAV path used.

What it needs: the PR also changes the early-return guard to && !store && atomic.is_none(). That sends stores and atomics on a descriptor used as a CBV into the general UAV path (uav = store || …), which asks for a writable handle on a resource the root signature binds as a constant buffer. Nothing tests that path or explains what it lowers to. A store to a CBV is clearly wrong today, since it is lowered as a load. Either have it return an explicit Err("a store or atomic on a constant buffer"), or drop the guard from this PR. Then this would be a good candidate.


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-26 06:16 UTC · [original](https://github.com/Marvisak/VIctory/pull/3008#issuecomment-7184272178) <!-- victory-triage v1 head=0f35c0b9a60814f070fb837ae2d823864b2252f4 verdict=needs-work at=2026-11-26T06:19:00Z --> Automated triage for the maintainers. The main fix looks right. In `main`, the CBV early return in `Lower::buffer` (`emit/lower.rs`) ignores `sub`, so `buffer_load_{ubyte,sbyte,ushort,sshort}` on a constant buffer returns the whole containing dword. The PR fixes both paths: - **Table path:** it aligns the address to 4 bytes and then shifts and extends. - **Root path:** `cbuffer_dword` already selects the dword from an unaligned address, and the PR adds the shift and extend. The new shared `extract_sub` helper is the same code the SRV/UAV path used. What it needs: the PR also changes the early-return guard to `&& !store && atomic.is_none()`. That sends stores and atomics on a descriptor used as a CBV into the general UAV path (`uav = store || …`), which asks for a writable handle on a resource the root signature binds as a constant buffer. Nothing tests that path or explains what it lowers to. A store to a CBV is clearly wrong today, since it is lowered as a load. Either have it return an explicit `Err("a store or atomic on a constant buffer")`, or drop the guard from this PR. Then this would be a good candidate. --- _Generated by [Claude Code](https://claude.ai/code)_
github-bot force-pushed github-bot/gh-3008 from 0f35c0b9a6 to 2829f99a05 2026-11-26 08:34:27 +00:00 Compare
Author
Owner

@⁠Dukestar69 on GitHub · 2026-11-26 03:32 UTC · original · approved these changes

Tested merged onto origin/main (63bcca4) on Windows 11 x64 (NVIDIA GeForce RTX 5090 + WARP, dxil.dll validator active):

  • cargo test -p victory-shader --lib cbv_subdword_tests: 2 passed, 0 failed (cbv_subdword_dxil_distinguishes_width_and_signedness and cbv_subdword_loads_extract_and_extend_bytes_and_shorts pass on RTX 5090 hardware and WARP).
  • cargo clippy -p victory-shader --all-targets -- -D warnings: clean.
  • cargo xtask policy: 192 files ok.

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

**@⁠Dukestar69** on GitHub · 2026-11-26 03:32 UTC · [original](https://github.com/Marvisak/VIctory/pull/3008#pullrequestreview-6068189340) · **approved these changes** Tested merged onto `origin/main` (`63bcca4`) on Windows 11 x64 (NVIDIA GeForce RTX 5090 + WARP, `dxil.dll` validator active): - `cargo test -p victory-shader --lib cbv_subdword_tests`: **2 passed, 0 failed** (`cbv_subdword_dxil_distinguishes_width_and_signedness` and `cbv_subdword_loads_extract_and_extend_bytes_and_shorts` pass on RTX 5090 hardware and WARP). - `cargo clippy -p victory-shader --all-targets -- -D warnings`: clean. - `cargo xtask policy`: 192 files ok. Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
Author
Owner

@⁠fuzeofficlal on GitHub · 2026-11-26 03:38 UTC · original · requested changes

Code review on 0f35c0b9 (static source inspection; no runtime GPU/DXIL validation executed):

  • P2 — Stores/atomics on a tracked table CBV now fall through into the UAV path (crates/victory-shader/src/emit/lower.rs:9495, 9536-9588, crates/victory-gpu/src/descriptor/host.rs:1471):
    • Adding && !store && atomic.is_none() to the ResourceType::Cbv check at lower.rs:9495 lets any store or atomic referencing a tracked table CBV fall through to let uav = store || atomic.is_some() || ... (:9536), emitting a UAV write/atomic against a descriptor that the host binds as a raw SRV. Returning an explicit Err(...) for writes/atomics on ResourceType::Cbv before falling through avoids emitting an invalid D3D12 binding.
  • P2 — extract_sub silently truncates 16-bit loads straddling a 4-byte boundary (crates/victory-shader/src/emit/lower.rs:4907-4922, 9542, 9554):
    • Both CBV paths load a single 4-byte-aligned dword and extract (w >> ((byte & 3) * 8)) without checking alignment. For ushort/sshort (width == 16) at byte % 4 == 3, the high byte lies in the next dword and is silently dropped/zeroed. Suggest either checking/rejecting unaligned halfword offsets or combining the adjacent dword if RDNA2 allows unaligned buffer_load_ushort/sshort.
  • File size check: patched emit/lower.rs is 521,524 B (within the 512 KiB limit).
**@⁠fuzeofficlal** on GitHub · 2026-11-26 03:38 UTC · [original](https://github.com/Marvisak/VIctory/pull/3008#pullrequestreview-6068227511) · **requested changes** Code review on `0f35c0b9` (static source inspection; no runtime GPU/DXIL validation executed): - **P2 — Stores/atomics on a tracked table CBV now fall through into the UAV path (`crates/victory-shader/src/emit/lower.rs:9495, 9536-9588`, `crates/victory-gpu/src/descriptor/host.rs:1471`)**: - Adding `&& !store && atomic.is_none()` to the `ResourceType::Cbv` check at `lower.rs:9495` lets any store or atomic referencing a tracked table CBV fall through to `let uav = store || atomic.is_some() || ...` (`:9536`), emitting a UAV write/atomic against a descriptor that the host binds as a raw SRV. Returning an explicit `Err(...)` for writes/atomics on `ResourceType::Cbv` before falling through avoids emitting an invalid D3D12 binding. - **P2 — `extract_sub` silently truncates 16-bit loads straddling a 4-byte boundary (`crates/victory-shader/src/emit/lower.rs:4907-4922, 9542, 9554`)**: - Both CBV paths load a single 4-byte-aligned dword and extract `(w >> ((byte & 3) * 8))` without checking alignment. For `ushort`/`sshort` (`width == 16`) at `byte % 4 == 3`, the high byte lies in the next dword and is silently dropped/zeroed. Suggest either checking/rejecting unaligned halfword offsets or combining the adjacent dword if RDNA2 allows unaligned `buffer_load_ushort`/`sshort`. - File size check: patched `emit/lower.rs` is `521,524 B` (within the 512 KiB limit).
Author
Owner

@⁠kotekompotek on GitHub · 2026-11-26 04:41 UTC · original · approved these changes

LGTM — extract_sub does the textbook align-down + shift + sign/zero extension; the CBV gate correctly excludes stores and atomics now, and the align-down applies only when a sub-dword load exists. Test file covers the store/load matrix.

**@⁠kotekompotek** on GitHub · 2026-11-26 04:41 UTC · [original](https://github.com/Marvisak/VIctory/pull/3008#pullrequestreview-6068640864) · **approved these changes** LGTM — extract_sub does the textbook align-down + shift + sign/zero extension; the CBV gate correctly excludes stores and atomics now, and the align-down applies only when a sub-dword load exists. Test file covers the store/load matrix.
Author
Owner

@⁠RVZO6 on GitHub · 2026-11-26 08:26 UTC · original

Addressed the review comment in 2829f99:

  • Restored the if used.is_some_and(|u| u.kind == ResourceType::Cbv) && !typed { entry condition in crates/victory-shader/src/emit/lower.rs and explicitly refused store || atomic.is_some() on a CBV (return Err("a store or atomic to a constant buffer")) so buffer_store_* / buffer_atomic_* against a CBV descriptor can never fall through into the raw-buffer/UAV write path.
  • Added cbv_store_and_atomic_are_refused in crates/victory-shader/src/emit/lower/cbv_subdword_tests.rs (confirmed failing before the fix and passing after).
  • Verified clean on windows-latest MSVC + WARP SM 6.6 CI (policy, clippy default + --all-features, full workspace tests): https://github.com/RVZO6/VIctory/actions/runs/45782627843.

Not run: the title; any hardware GPU.
AI-assisted: T3 Code (Antigravity, gemini-4-neon).

**@⁠RVZO6** on GitHub · 2026-11-26 08:26 UTC · [original](https://github.com/Marvisak/VIctory/pull/3008#issuecomment-7186602471) Addressed the review comment in `2829f99`: - Restored the `if used.is_some_and(|u| u.kind == ResourceType::Cbv) && !typed {` entry condition in `crates/victory-shader/src/emit/lower.rs` and explicitly refused `store || atomic.is_some()` on a CBV (`return Err("a store or atomic to a constant buffer")`) so `buffer_store_*` / `buffer_atomic_*` against a CBV descriptor can never fall through into the raw-buffer/UAV write path. - Added `cbv_store_and_atomic_are_refused` in `crates/victory-shader/src/emit/lower/cbv_subdword_tests.rs` (confirmed failing before the fix and passing after). - Verified clean on `windows-latest` MSVC + WARP SM 6.6 CI (policy, clippy default + `--all-features`, full workspace tests): https://github.com/RVZO6/VIctory/actions/runs/45782627843. **Not run:** the title; any hardware GPU. AI-assisted: T3 Code (Antigravity, gemini-4-neon).
Author
Owner

@⁠passerbyyhx on GitHub · 2026-11-26 08:48 UTC · original

I checked the halfword-at-offset-3 question from the review on 2829f99a. The RTX 4090 Laptop and Microsoft WARP returned the same values through both a root CBV and a table CBV. All reads were inside a live 256-byte buffer.

With bytes 2–5 set to 34 81 92 5e, buffer_load_ushort at offsets 2, 3, 4 returned 00008134, 00000081, 00005e92. The signed variant returned ffff8134, 00000081, 00005e92. Changing only byte 4 from 92 to 16 changed offset 4 to 00005e16 in every variant. Offset 3 remained 00000081. The output guard held through all 16 dispatches. The byte in the next dword reaches the GPU and contributes to the aligned read at offset 4, but does not contribute to the halfword at offset 3 on this revision.

The RDNA 2 ISA, §8.1.7 specifies several alignment modes. On the first input, an unaligned unsigned halfword would be 9281, while automatic natural alignment would select 8134. The two strict modes require halfword alignment. I have no measurement of the console setting for this path. The opt-in readback asserts the aligned controls and guard and reports the crossing value for comparison. From the evidence revision, run cargo test --locked -p victory-shader --features host --lib emit::lower::cbv_subdword_tests::observe_interior_halfword_offsets -- --exact --ignored --nocapture --test-threads=1.

**@⁠passerbyyhx** on GitHub · 2026-11-26 08:48 UTC · [original](https://github.com/Marvisak/VIctory/pull/3008#issuecomment-7187051470) I checked the halfword-at-offset-3 question from [the review](https://git.gtahub.st/github/Marvisak--VIctory/pulls/3008) on `2829f99a`. The RTX 4090 Laptop and Microsoft WARP returned the same values through both a root CBV and a table CBV. All reads were inside a live 256-byte buffer. With bytes 2–5 set to `34 81 92 5e`, `buffer_load_ushort` at offsets 2, 3, 4 returned `00008134`, `00000081`, `00005e92`. The signed variant returned `ffff8134`, `00000081`, `00005e92`. Changing only byte 4 from `92` to `16` changed offset 4 to `00005e16` in every variant. Offset 3 remained `00000081`. The output guard held through all 16 dispatches. The byte in the next dword reaches the GPU and contributes to the aligned read at offset 4, but does not contribute to the halfword at offset 3 on this revision. [The RDNA 2 ISA, §8.1.7](https://docs.amd.com/api/khub/documents/Et~wpu9g~Ffl7d9q0QZ~Og/content#page=72) specifies several alignment modes. On the first input, an unaligned unsigned halfword would be `9281`, while automatic natural alignment would select `8134`. The two strict modes require halfword alignment. I have no measurement of the console setting for this path. The [opt-in readback](https://github.com/passerbyyhx/VIctory/blob/8da44d87e54e3b429e059d58fc68f97b2f59c6dc/crates/victory-shader/src/emit/lower/cbv_subdword_tests.rs#L344) asserts the aligned controls and guard and reports the crossing value for comparison. From the evidence revision, run `cargo test --locked -p victory-shader --features host --lib emit::lower::cbv_subdword_tests::observe_interior_halfword_offsets -- --exact --ignored --nocapture --test-threads=1`.
Author
Owner

@⁠fuzeofficlal on GitHub · 2026-11-26 09:17 UTC · original · reviewed

Verified 2829f99a locally on Windows 11 x64 (dxil.dll 10.0.26100.0):

  • cargo clippy -p victory-shader --all-targets --locked -- -D warnings (default and --all-features): clean (2.67s / 2.03s).
  • cargo test -p xtask --locked policy::tests::repository_passes_policy: 1 passed, 0 failed (0.35s).
  • cargo test -p victory-shader --lib --locked cbv_subdword_tests -- --test-threads=1: 3 passed, 0 failed on both RTX 5090 + WARP (0.73s) and VICTORY_WARP=1 (0.07s).
  • cargo test -p victory-shader --lib --locked emit::lower::tests: 33 passed, 0 failed (0.02s).
  • emit/lower.rs is 521,613 bytes (2,675 bytes under the 524,288-byte policy cap).

The explicit "a store or atomic on a constant buffer" rejection at lower.rs:9499-9501 resolves the raw CBV write fallthrough, and extract_sub / byte & !3 / raw_view cursor refresh are sound for in-dword reads. Three minor notes:

  1. Because line 9498 gates on && !typed, a formatted buffer_store_format_* on a tracked table CBV can still reach the typed UAV path; moving the store || atomic.is_some() guard ahead of !typed would close that too.
  2. At byte % 4 == 3, 16-bit loads still return only the top byte of that dword (matching the existing SRV/UAV path at :9785-9806 and the reported RTX/WARP readback); documenting that assumption and adding an odd-offset control in cbv_subdword_tests.rs would be helpful.
  3. rustfmt --edition 2024 --check flags two small formatting lines in cbv_subdword_tests.rs (:174 and :278).
**@⁠fuzeofficlal** on GitHub · 2026-11-26 09:17 UTC · [original](https://github.com/Marvisak/VIctory/pull/3008#pullrequestreview-6071382287) · **reviewed** Verified `2829f99a` locally on Windows 11 x64 (`dxil.dll` 10.0.26100.0): - `cargo clippy -p victory-shader --all-targets --locked -- -D warnings` (default and `--all-features`): clean (`2.67s` / `2.03s`). - `cargo test -p xtask --locked policy::tests::repository_passes_policy`: 1 passed, 0 failed (`0.35s`). - `cargo test -p victory-shader --lib --locked cbv_subdword_tests -- --test-threads=1`: 3 passed, 0 failed on both RTX 5090 + WARP (`0.73s`) and `VICTORY_WARP=1` (`0.07s`). - `cargo test -p victory-shader --lib --locked emit::lower::tests`: 33 passed, 0 failed (`0.02s`). - `emit/lower.rs` is `521,613` bytes (`2,675` bytes under the 524,288-byte policy cap). The explicit `"a store or atomic on a constant buffer"` rejection at `lower.rs:9499-9501` resolves the raw CBV write fallthrough, and `extract_sub` / `byte & !3` / `raw_view` cursor refresh are sound for in-dword reads. Three minor notes: 1. Because line 9498 gates on `&& !typed`, a formatted `buffer_store_format_*` on a tracked table CBV can still reach the typed UAV path; moving the `store || atomic.is_some()` guard ahead of `!typed` would close that too. 2. At `byte % 4 == 3`, 16-bit loads still return only the top byte of that dword (matching the existing SRV/UAV path at `:9785-9806` and the reported RTX/WARP readback); documenting that assumption and adding an odd-offset control in `cbv_subdword_tests.rs` would be helpful. 3. `rustfmt --edition 2024 --check` flags two small formatting lines in `cbv_subdword_tests.rs` (`:174` and `:278`).
This pull request can be merged automatically.
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/3008/head:github-bot/gh-3008'
git switch 'github-bot/gh-3008'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-3008'
git switch 'github-bot/gh-3008'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-3008'
git switch 'github-bot/gh-3008'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-3008'
git switch 'main'
git merge --squash 'github-bot/gh-3008'
git switch 'main'
git merge --ff-only 'github-bot/gh-3008'
git switch 'main'
git merge 'github-bot/gh-3008'
git push origin 'main'
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!3008
No description provided.