Skip to content

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

Closed
ESAT586 wants to merge 1 commit into
Marvisak:mainfrom
ESAT586:gpu/cbv-null-and-zero-padding
Closed

ESAT586 wants to merge 1 commit into
Marvisak:mainfrom
ESAT586:gpu/cbv-null-and-zero-padding

Conversation

@ESAT586

@ESAT586 ESAT586 commented Nov 24, 2026

Copy link
Copy Markdown

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)

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.

…nvalid views

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 Marvisak#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 Marvisak#1581.

@catminou4 catminou4 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

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

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 Marvisak closed this Nov 25, 2026

Copy link
Copy Markdown
Owner

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

clquwu added a commit to clquwu/leonida-ultimate that referenced this pull request Nov 25, 2026
…, D16S8/R16 UAV views, audit defects & Stage 2 device fixes

- Placed texture Map()/Unmap() return va_ directly with xmem page disarm (LEONIDA_LINEAR_MAP_FIRST=1 opt-out) and pre-draw per-frame xs_refresh_placed_textures on first direct ExecuteCommandLists after PresentX (ubiquitarianism/leonida@545e8d51, five-star-recomp@38a8436, GPL-3.0).
- Preserve xbox_d16s8 (118) view formats 118/119/120 in proxy SRV/DSV/placed-view paths; create depth16 UAV plane 0 as R16_TYPELESS and allow R16_UNORM (56) on R16_TYPELESS/118 depth UAVs.
- Fix cc4553c review defects: lock-held arena_release_locked + had_retired retry in arena_allocate_reclaiming; slot 127 depth fallback unconditional clear/castable sanitization; view-flag-agnostic slot 62 copy lookup + richer skip diagnostics.
- Force integer RT blendEnable=VK_FALSE (Marvisak/VIctory#2042, GPL-3.0); off-element placed structured buffer V# + PC RAW fallback (five-star-recomp@955a679, GPL-3.0); console typeless-on-typeless view fallback (Marvisak/VIctory#2190, GPL-3.0) + CBV ring tail zeroing (Marvisak/VIctory#1629, GPL-3.0); refuse MEM_MAPPED in create_host_buffer with LEONIDA_IMPORT_REMAPPABLE=1 opt-out (Neonbridge@838c433, GPL-3.0); AS SRV Location resolution (Marvisak/VIctory#2098, GPL-3.0); 3D R9G9B9E5 RT+UAV RGBA16F view format fix; true placed-texture upload totals telemetry + boot_bench parser.
simnJS added a commit to MiamiGame/XTP that referenced this pull request Nov 25, 2026
…pty ones, at most 64 KiB

- Table CBVs filled into the list's scratch or own memory (upload fill or
  GPU copy) zero the bytes from the title's size to the 256-byte end the
  host view takes, as the console reads zeros past a view's size; before,
  they read what earlier recordings left there. A CBV copy is reused only
  where its bytes past the asked length are zeros.
- A constant view of no address or no bytes is a null CBV (dynamic slots:
  HostHeap::null_constant; title slots: `make` declines it, `null_for`
  makes a null CBV), not a 0-byte view, a null 2D SRV, or a fill from
  title address 0. create_cbv without a description records
  View::Constant(0, 0) instead of leaving the slot's old view.
- Host CBVs take at most 64 KiB (constant_bytes, capped_constant): larger
  ones remove the device on NVIDIA (0x887A0001). Title sizes over 64 KiB
  are logged (counted, with the largest seen), and only 64 KiB is copied.

Tests (WARP): draw::tests::table_cbvs_read_zeros_past_their_size,
descriptor::tests::a_constant_view_without_a_description_is_a_null_one,
descriptor::host::tests::constant_views_are_capped_at_64_kib_and_null_when_empty.

Ported from Marvisak/VIctory#1629 (head 111b773) by ESAT586
Ported from Marvisak/VIctory#1640 (head b916a9c) by NuraadGit
Ported from fxceless/victory-fork#911 (1087538) by douline
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants