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

Open
RVZO6 wants to merge 1 commit from upstream-pr/3008 into main
RVZO6 commented 2026-11-26 03:19:27 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by RVZO6 on 2026-11-26T03:19:27Z.


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

<!-- greyfinch-sync:Marvisak/VIctory:issue:6618763744 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3008). Originally posted by [RVZO6](https://github.com/RVZO6) on 2026-11-26T03:19:27Z. --- `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).
Dukestar69 commented 2026-11-26 04:06:01 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Dukestar69 on 2026-11-26T03:32:29Z.

Review: APPROVED


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

<!-- greyfinch-sync:Marvisak/VIctory:review:6068189340 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3008#pullrequestreview-6068189340). Originally posted by [Dukestar69](https://github.com/Dukestar69) on 2026-11-26T03:32:29Z. **Review:** APPROVED --- 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).
fuzeofficlal commented 2026-11-26 04:06:01 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-26T03:38:41Z.

Review: CHANGES_REQUESTED


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).
<!-- greyfinch-sync:Marvisak/VIctory:review:6068227511 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3008#pullrequestreview-6068227511). Originally posted by [fuzeofficlal](https://github.com/fuzeofficlal) on 2026-11-26T03:38:41Z. **Review:** CHANGES_REQUESTED --- 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).
kotekompotek commented 2026-11-26 05:03:48 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by kotekompotek on 2026-11-26T04:41:31Z.

Review: APPROVED


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.

<!-- greyfinch-sync:Marvisak/VIctory:review:6068640864 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3008#pullrequestreview-6068640864). Originally posted by [kotekompotek](https://github.com/kotekompotek) on 2026-11-26T04:41:31Z. **Review:** APPROVED --- 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.
Marvisak commented 2026-11-26 07:01:45 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-26T06:16:48Z.


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

<!-- greyfinch-sync:Marvisak/VIctory:comment:7184272178 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3008#issuecomment-7184272178). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-26T06:16:48Z. --- <!-- 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)_
RVZO6 commented 2026-11-26 09:06:08 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by RVZO6 on 2026-11-26T08:26:47Z.


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

<!-- greyfinch-sync:Marvisak/VIctory:comment:7186602471 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3008#issuecomment-7186602471). Originally posted by [RVZO6](https://github.com/RVZO6) on 2026-11-26T08:26:47Z. --- 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).
passerbyyhx commented 2026-11-26 09:06:14 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by passerbyyhx on 2026-11-26T08:48:49Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7187051470 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3008#issuecomment-7187051470). Originally posted by [passerbyyhx](https://github.com/passerbyyhx) on 2026-11-26T08:48:49Z. --- I checked the halfword-at-offset-3 question from [the review](https://github.com/Marvisak/VIctory/pull/3008#pullrequestreview-6068227511) 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`.
fuzeofficlal commented 2026-11-26 10:04:39 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-26T09:17:43Z.

Review: COMMENTED


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).
<!-- greyfinch-sync:Marvisak/VIctory:review:6071382287 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3008#pullrequestreview-6071382287). Originally posted by [fuzeofficlal](https://github.com/fuzeofficlal) on 2026-11-26T09:17:43Z. **Review:** COMMENTED --- 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 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/3008:upstream-pr/3008'
git switch 'upstream-pr/3008'

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