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

Closed
github-bot wants to merge 1 commit from gh-pr/1629/head into gh-pr/1629/base
Owner

@⁠ESAT586 on GitHub · 2026-11-24 20:01 UTC · original
ESAT586:gpu/cbv-null-and-zero-padding → main · +442 −14 · 3 files · 1 commits · closed without merging

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.

**@⁠ESAT586** on GitHub · 2026-11-24 20:01 UTC · [original](https://github.com/Marvisak/VIctory/pull/1629) `ESAT586:gpu/cbv-null-and-zero-padding` → `main` · +442 −14 · 3 files · 1 commits · closed without merging > **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.
Authored and verified by Gemini 4 Neon (Extra High Mode) as an AI coding
agent, strictly clean-room with zero game files or external dumps.

Two constant-buffer binding issues in victory-gpu (also observed on world
frames in #1581):

1. Stale scratch tail in 256-byte CBV padding:
   HostList::scratch rounds allocations to 256 bytes and reuses its upload
   chunks across HostList::reset(), while table CBVs (State::tables) and
   unaligned root CBVs (root_view) only filled `size` / `len` bytes. On PC
   D3D12, CBV SizeInBytes is rounded up to a 256-byte multiple, so shader
   reads in `[size..padded)` observed stale bytes from previous draws or
   frames instead of zeros (RDNA2 hardware bounds-checks CBVs at exact
   byte size and returns 0 past the end). Extend Step::Fill with
   HostList::defer_fill_padded to zero `[len..padded)` immediately after
   the submission-time copy.

2. Invalid non-null SizeInBytes=0 CBVs and wrong null descriptor type:
   In State::tables, View::Constant(address, size) with `address == 0 ||
   size == 0` (including the title's null CBV `View::Constant(0, 0)`) or
   an overflowing `size.checked_next_multiple_of(256)` previously allocated
   256 bytes of scratch and called `constant_at` with a non-null scratch
   GPU VA and `SizeInBytes = 0` (invalid in D3D12), or fell back to
   `host.make_null` (a 2D texture SRV, which triggers wild-address reads on
   AMD and GBV mismatches). Add `HostHeap::make_null_constant` (calling
   `CreateConstantBufferView(None, ...)`) and guard `State::tables`,
   `HostHeap::constant_at`, and `descriptor::host::make` consistently.

Verification (clean-room synthetic compute readback tests, zero game data):
- NVIDIA GeForce RTX 3060 Ti (`cargo test -p victory-gpu --locked` and
  `--all-features --locked`: 53/53 & 56/56 passed)
- NVIDIA GeForce RTX 3060 Ti with `VICTORY_D3D12_DEBUG=1` +
  `VICTORY_D3D12_GBV=1` (3/3 CBV tests passed)
- Microsoft WARP (`VICTORY_D3D12_WARP=1`, 3/3 CBV tests passed)
- `cargo clippy --workspace --all-targets --locked -- -D warnings` (default
  & `--all-features`) and `cargo xtask policy` passed.

Refs #1581.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:38:54 +00:00
github-bot closed this pull request 2026-11-26 04:38:54 +00:00
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 01:22 UTC · original

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** on GitHub · 2026-11-25 01:22 UTC · [original](https://github.com/Marvisak/VIctory/pull/1629#issuecomment-7149472552) 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.
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 01:32 UTC · original

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

**@⁠Marvisak** on GitHub · 2026-11-25 01:32 UTC · [original](https://github.com/Marvisak/VIctory/pull/1629#issuecomment-7149668440) <!-- 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

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