GPU: zero 256-byte CBV scratch padding and bind null CBVs for empty/invalid views #1628

Closed
ESAT586 wants to merge 1 commit from upstream-pr/1629 into main
ESAT586 commented 2026-11-24 20:01:39 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by ESAT586 on 2026-11-24T20:01:39Z.


AI Authorship & Clean-Room Disclosure: This PR was authored and hardware-verified by Gemini 4 Neon (Extra High Mode) as an AI coding agent, developed strictly clean-room with zero game files or external dumps.

Summary

Fixes two constant-buffer upload/binding bugs in victory-gpu (reported alongside world-frame testing in #1581, where zeroing CBV scratch padding produced a visible FPS increase on an RTX 4070):

  1. Zero [size..padded) scratch padding for 256-byte-rounded CBVs (host.rs, draw.rs)

    • HostList::scratch() rounds allocations up to 256 bytes and reuses upload chunks across HostList::reset().
    • Previously, State::tables() (table CBVs) and root_view() (unaligned root CBVs) only filled size / len bytes with defer_fill, leaving [size..padded) holding stale bytes from earlier draws or frames.
    • Because PC D3D12 rounds D3D12_CONSTANT_BUFFER_VIEW_DESC::SizeInBytes up to a 256-byte multiple, shader loads past size (within the rounded 256-byte block) read stale scratch data instead of 0 as on console RDNA2 hardware.
    • Added HostList::defer_fill_padded() (extending Step::Fill with padded) so [len..padded) is zeroed immediately after copy_mapped at submission time, with zero overhead when len is already a 256-byte multiple.
  2. Bind a proper D3D12 null CBV for zero-address, zero-sized, overflowing, or OOM constant views (draw.rs, descriptor/host.rs)

    • Previously, View::Constant(0, 0) (and View::Constant(addr, 0) / View::Constant(0, size)) in State::tables() allocated 256 bytes of scratch and called constant_at(index, gpu, 0), producing an invalid D3D12 CBV with a non-null BufferLocation and SizeInBytes = 0 (and failing ReadProcessMemory on NULL for (0, size)).
    • When list.scratch() failed, State::tables() also fell back to host.make_null(index) (a 2D texture SRV null view) instead of a null CBV, risking AMD wild-address device removal (#59) and GBV descriptor-type mismatches.
    • Added HostHeap::make_null_constant(index) (CreateConstantBufferView(None, at)), guarded State::tables() and HostHeap::constant_at() with address != 0 && size != 0 and size.checked_next_multiple_of(256) (avoiding u32::next_multiple_of overflow panics), and applied the same zero-size / overflow guard in descriptor::host::make().
    • Leaves descriptor.rs untouched (no conflict with #1298 / #1574) and leaves the empty-slot None => branch in draw.rs untouched (no conflict with #581).

Changes

  • crates/victory-gpu/src/host.rs: extend Step::Fill with padded and add HostList::defer_fill_padded to zero trailing padding bytes at submission time.
  • crates/victory-gpu/src/descriptor/host.rs: add HostHeap::make_null_constant, harden HostHeap::constant_at and make() against null-address / zero-sized / overflowing CBVs, and add zero_sized_and_overflowing_cbvs_become_null_views.
  • crates/victory-gpu/src/draw.rs: use defer_fill_padded + make_null_constant in State::tables and defer_fill_padded in unaligned root_view, and add end-to-end GPU compute readback tests table_cbv_zeroes_scratch_padding_and_binds_null_cbvs_on_gpu and unaligned_root_cbv_zeroes_scratch_padding_on_gpu.

Verification

All tests are synthetic clean-room tests (no game data):

  • NVIDIA GeForce RTX 3060 Ti (cargo test -p victory-gpu --locked): 53 passed, 0 failed, 1 ignored
  • NVIDIA GeForce RTX 3060 Ti (cargo test -p victory-gpu --all-features --locked): 56 passed, 0 failed, 1 ignored
  • NVIDIA GeForce RTX 3060 Ti + D3D12 Debug Layer + GPU-Based Validation (VICTORY_D3D12_DEBUG=1 VICTORY_D3D12_GBV=1 cargo test -p victory-gpu --all-features --locked cbv): 3 passed, 0 failed
  • Microsoft WARP (VICTORY_D3D12_WARP=1 cargo test -p victory-gpu --all-features --locked cbv): 3 passed, 0 failed
  • Workspace Clippy (cargo clippy --workspace --all-targets --locked -- -D warnings, default & --all-features): 0 warnings
  • Asset policy (cargo xtask policy): 154 files ok

Refs #1581.

<!-- greyfinch-sync:Marvisak/VIctory:issue:6590304403 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1629). Originally posted by [ESAT586](https://github.com/ESAT586) on 2026-11-24T20:01:39Z. --- > **AI Authorship & Clean-Room Disclosure:** This PR was authored and hardware-verified by **Gemini 4 Neon (Extra High Mode)** as an AI coding agent, developed strictly clean-room with **zero game files or external dumps**. ## Summary Fixes two constant-buffer upload/binding bugs in `victory-gpu` (reported alongside world-frame testing in #1581, where zeroing CBV scratch padding produced a visible FPS increase on an RTX 4070): 1. **Zero `[size..padded)` scratch padding for 256-byte-rounded CBVs (`host.rs`, `draw.rs`)** - `HostList::scratch()` rounds allocations up to 256 bytes and reuses upload chunks across `HostList::reset()`. - Previously, `State::tables()` (table CBVs) and `root_view()` (unaligned root CBVs) only filled `size` / `len` bytes with `defer_fill`, leaving `[size..padded)` holding stale bytes from earlier draws or frames. - Because PC D3D12 rounds `D3D12_CONSTANT_BUFFER_VIEW_DESC::SizeInBytes` up to a 256-byte multiple, shader loads past `size` (within the rounded 256-byte block) read stale scratch data instead of `0` as on console RDNA2 hardware. - Added `HostList::defer_fill_padded()` (extending `Step::Fill` with `padded`) so `[len..padded)` is zeroed immediately after `copy_mapped` at submission time, with zero overhead when `len` is already a 256-byte multiple. 2. **Bind a proper D3D12 null CBV for zero-address, zero-sized, overflowing, or OOM constant views (`draw.rs`, `descriptor/host.rs`)** - Previously, `View::Constant(0, 0)` (and `View::Constant(addr, 0)` / `View::Constant(0, size)`) in `State::tables()` allocated 256 bytes of scratch and called `constant_at(index, gpu, 0)`, producing an invalid D3D12 CBV with a non-null `BufferLocation` and `SizeInBytes = 0` (and failing `ReadProcessMemory` on `NULL` for `(0, size)`). - When `list.scratch()` failed, `State::tables()` also fell back to `host.make_null(index)` (a 2D texture SRV null view) instead of a null CBV, risking AMD wild-address device removal (#59) and GBV descriptor-type mismatches. - Added `HostHeap::make_null_constant(index)` (`CreateConstantBufferView(None, at)`), guarded `State::tables()` and `HostHeap::constant_at()` with `address != 0 && size != 0` and `size.checked_next_multiple_of(256)` (avoiding `u32::next_multiple_of` overflow panics), and applied the same zero-size / overflow guard in `descriptor::host::make()`. - Leaves `descriptor.rs` untouched (no conflict with #1298 / #1574) and leaves the empty-slot `None =>` branch in `draw.rs` untouched (no conflict with #581). ## Changes - `crates/victory-gpu/src/host.rs`: extend `Step::Fill` with `padded` and add `HostList::defer_fill_padded` to zero trailing padding bytes at submission time. - `crates/victory-gpu/src/descriptor/host.rs`: add `HostHeap::make_null_constant`, harden `HostHeap::constant_at` and `make()` against null-address / zero-sized / overflowing CBVs, and add `zero_sized_and_overflowing_cbvs_become_null_views`. - `crates/victory-gpu/src/draw.rs`: use `defer_fill_padded` + `make_null_constant` in `State::tables` and `defer_fill_padded` in unaligned `root_view`, and add end-to-end GPU compute readback tests `table_cbv_zeroes_scratch_padding_and_binds_null_cbvs_on_gpu` and `unaligned_root_cbv_zeroes_scratch_padding_on_gpu`. ## Verification All tests are synthetic clean-room tests (no game data): - **NVIDIA GeForce RTX 3060 Ti** (`cargo test -p victory-gpu --locked`): **53 passed, 0 failed, 1 ignored** - **NVIDIA GeForce RTX 3060 Ti** (`cargo test -p victory-gpu --all-features --locked`): **56 passed, 0 failed, 1 ignored** - **NVIDIA GeForce RTX 3060 Ti + D3D12 Debug Layer + GPU-Based Validation** (`VICTORY_D3D12_DEBUG=1 VICTORY_D3D12_GBV=1 cargo test -p victory-gpu --all-features --locked cbv`): **3 passed, 0 failed** - **Microsoft WARP** (`VICTORY_D3D12_WARP=1 cargo test -p victory-gpu --all-features --locked cbv`): **3 passed, 0 failed** - **Workspace Clippy** (`cargo clippy --workspace --all-targets --locked -- -D warnings`, default & `--all-features`): **0 warnings** - **Asset policy** (`cargo xtask policy`): **154 files ok** Refs #1581.
catminou4 commented 2026-11-24 21:02:52 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by catminou4 on 2026-11-24T20:06:17Z.

Review: COMMENTED


Static review only: victory-gpu/victory-hle/apps are Windows-only (D3D12/windows_sys), so nothing here compiles or runs on macOS — please rely on the Windows CI leg.

CBV creation is hardened: constant_at and the descriptor make path now use checked_next_multiple_of(256) (u32::MAX-sized CBVs no longer wrap to a small SizeInBytes), null/zero-sized buffers become real null CBVs (CreateConstantBufferView(None, ...)) that read zeros, and scratch padding is zeroed. The new test covers gpu+size=0, address=0 and the overflow case. This matches the D3D12 requirement that CBV SizeInBytes be a multiple of 256 and > 0 — the previous size.next_multiple_of(256) could panic in debug or wrap in release on overflow. Windows-only; looks correct statically.

<!-- greyfinch-sync:Marvisak/VIctory:review:6046506574 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1629#pullrequestreview-6046506574). Originally posted by [catminou4](https://github.com/catminou4) on 2026-11-24T20:06:17Z. **Review:** COMMENTED --- Static review only: victory-gpu/victory-hle/apps are Windows-only (D3D12/windows_sys), so nothing here compiles or runs on macOS — please rely on the Windows CI leg. CBV creation is hardened: `constant_at` and the descriptor `make` path now use `checked_next_multiple_of(256)` (u32::MAX-sized CBVs no longer wrap to a small SizeInBytes), null/zero-sized buffers become real null CBVs (`CreateConstantBufferView(None, ...)`) that read zeros, and scratch padding is zeroed. The new test covers gpu+size=0, address=0 and the overflow case. This matches the D3D12 requirement that CBV SizeInBytes be a multiple of 256 and > 0 — the previous `size.next_multiple_of(256)` could panic in debug or wrap in release on overflow. Windows-only; looks correct statically.
upstream-sync closed this pull request 2026-11-25 02:04:40 +00:00
Marvisak commented 2026-11-25 02:05:55 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T01:22:01Z.


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

Built on HostHeap::constant_at and per-draw CBV scratch copies in tables, both gone from main (constant buffers are raw views of the title's slots; empty views are nulled by in_buffer). The FPS claim for zeroed padding is not credible.

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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7149472552 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1629#issuecomment-7149472552). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T01:22:01Z. --- Automated triage for the maintainers. Closing because this change is already in `main`, or `main` has since replaced the code it patches. Built on HostHeap::constant_at and per-draw CBV scratch copies in tables, both gone from main (constant buffers are raw views of the title's slots; empty views are nulled by in_buffer). The FPS claim for zeroed padding is not credible. 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 commented 2026-11-25 02:06:10 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T01:32:41Z.


Automated triage for the maintainers.

Needs rebase. This pull request conflicts with main (conflicting: crates/victory-gpu/src/descriptor/host.rs, crates/victory-gpu/src/draw.rs). It will be reviewed once it is rebased onto the current main.


Generated by Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:comment:7149668440 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1629#issuecomment-7149668440). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T01:32:41Z. --- <!-- victory-triage v1 head=111b77360b822ac36031185ac3363575df666355 verdict=needs-rebase at=2026-11-25T01:32:41Z --> Automated triage for the maintainers. **Needs rebase.** This pull request conflicts with `main` (conflicting: `crates/victory-gpu/src/descriptor/host.rs`, `crates/victory-gpu/src/draw.rs`). It will be reviewed once it is rebased onto the current `main`. --- _Generated by [Claude Code](https://claude.ai/code)_

Pull request closed

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