GPU: mirror 256-byte aligned CBV tables in place without dynamic slots (fix on fork) #840

Closed
opened 2026-11-26 04:14:02 +00:00 by github-bot · 7 comments
Owner

@⁠alessiodeia on GitHub · 2026-11-24 12:05 UTC · original

Fix ready on fork: alessiodeia/VIctory@1d21e06 (branch ix/reuse-aligned-cbv-tables, single commit on fe310e).

Problem

State::tables() (crates/victory-gpu/src/draw.rs) currently copies every descriptor table containing a ResourceType::Cbv into dynamic descriptor slots (and copies its constant data through list.scratch), even when every View::Constant in the table's span is already 256-byte aligned on the host and within host.count().

On capped shader-visible view heaps (1,000,000 views / MIN_DYNAMIC = 65,536 on NVIDIA), world frames record dozens of command lists in parallel before submission and exhaust all 65,536 dynamic slots - dropping 4,096+ draws/dispatches ( draw or dispatch dropped: no dynamic slots for its tables) even when finished lists' slots are reclaimed (#688).

Fix (1d21e06)

  • State::tables(): only routes a CBV-bearing table through dynamic slots when some View::Constant(address, size) in its span ..n has size == 0, fails s.gpu(address).is_multiple_of(256), or whose 256-byte-padded size leaves its shadow buffer (in addition to index >= host.count()).
  • **
    eadable():** refreshes the shadow range (shadow.refresh(list, address, size)) for View::Constant mirrored in place, keeping submission-time semantics while avoiding dynamic descriptor and scratch-buffer churn.
  • Test (draw::tests::aligned_cbv_tables_mirror_in_place_without_dynamic_slots): verifies on a real D3D12 device that a 256-byte-aligned CBV table allocates dynamic slots, while both a +16-byte misaligned CBV and a CBV whose 256-byte-padded size runs past the shadow buffer's end still fall back to dynamic slots.

Verification

Windows 11 x64 (26300), Rust 1.98.1 (--locked), NVIDIA GeForce RTX 2070 SUPER (driver 617.42):

  • cargo test -p victory-gpu --locked --offline -- --test-threads=1: 51 passed, 0 failed, 1 ignored (benchmark).
  • cargo clippy -p victory-gpu --all-targets --locked --offline -- -D warnings: 0 warnings (default and --features diagnostics).
  • cargo --offline xtask policy: 154 files ok.
  • Live retail GTA VI 1.0.2.1 diagnostics run (RTX 2070 SUPER, stacked locally with #688 + #206 + #67 + mesh bounds clamping, up to frame 4100 in the prison intro):
    o dynamic slots for its tables drops went from 4,096+ to 0, with 0 constant buffer not 256-byte aligned warnings.

AI-assisted; no game files or assets attached.

**@⁠alessiodeia** on GitHub · 2026-11-24 12:05 UTC · [original](https://github.com/Marvisak/VIctory/issues/840) **Fix ready on fork:** [alessiodeia/VIctory@1d21e06](https://github.com/alessiodeia/VIctory/commit/1d21e0647f3907ccd3a2d668e832af7b75972c00) (branch [ ix/reuse-aligned-cbv-tables](https://github.com/alessiodeia/VIctory/tree/fix/reuse-aligned-cbv-tables), single commit on fe310e). ## Problem State::tables() (crates/victory-gpu/src/draw.rs) currently copies every descriptor table containing a ResourceType::Cbv into dynamic descriptor slots (and copies its constant data through list.scratch), even when every View::Constant in the table's span is already 256-byte aligned on the host and within host.count(). On capped shader-visible view heaps (1,000,000 views / MIN_DYNAMIC = 65,536 on NVIDIA), world frames record dozens of command lists in parallel before submission and exhaust all 65,536 dynamic slots - dropping **4,096+** draws/dispatches ( draw or dispatch dropped: no dynamic slots for its tables) even when finished lists' slots are reclaimed (#688). ## Fix (1d21e06) - **State::tables():** only routes a CBV-bearing table through dynamic slots when some View::Constant(address, size) in its span ..n has size == 0, fails s.gpu(address).is_multiple_of(256), or whose 256-byte-padded size leaves its shadow buffer (in addition to index >= host.count()). - ** eadable():** refreshes the shadow range (shadow.refresh(list, address, size)) for View::Constant mirrored in place, keeping submission-time semantics while avoiding dynamic descriptor and scratch-buffer churn. - **Test (draw::tests::aligned_cbv_tables_mirror_in_place_without_dynamic_slots):** verifies on a real D3D12 device that a 256-byte-aligned CBV table allocates dynamic slots, while both a +16-byte misaligned CBV and a CBV whose 256-byte-padded size runs past the shadow buffer's end still fall back to dynamic slots. ## Verification Windows 11 x64 (26300), Rust 1.98.1 (--locked), NVIDIA GeForce RTX 2070 SUPER (driver 617.42): - cargo test -p victory-gpu --locked --offline -- --test-threads=1: **51 passed, 0 failed, 1 ignored** (benchmark). - cargo clippy -p victory-gpu --all-targets --locked --offline -- -D warnings: **0 warnings** (default and --features diagnostics). - cargo --offline xtask policy: **154 files ok**. - Live retail GTA VI 1.0.2.1 diagnostics run (RTX 2070 SUPER, stacked locally with #688 + #206 + #67 + mesh bounds clamping, up to frame 4100 in the prison intro): o dynamic slots for its tables drops went from **4,096+** to **0**, with **0** constant buffer not 256-byte aligned warnings. AI-assisted; no game files or assets attached.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:14:02 +00:00
Author
Owner

@⁠andrewpozdnakov7-jpg on GitHub · 2026-11-24 12:55 UTC · original

I checked the padded-end case on 1d21e06. The original fixture uses offset 4096 - 128, so it exercises the misalignment fallback. I moved it to offset 4096 in a 4224-byte buffer and verified the host GPU address is aligned and exactly 128 bytes remain in its shadow.

With that fixture, the fork still mirrors the tail in place. The padded shadow::find can return a different loose shadow, while creating the CBV looks up the unpadded range. The incremental patch uses the same shadow as CBV creation and checks the padded size against the bytes left in it.

On Windows WARP, the aligned 256-byte control still used zero dynamic slots. The misaligned control and the aligned 128-byte tail each used the fallback. The diagnostics library suite passed 54 tests with one benchmark ignored.

AI-assisted. Verified with synthetic buffers.

Incremental patch on 1d21e06
diff --git a/crates/victory-gpu/src/draw.rs b/crates/victory-gpu/src/draw.rs
index f915dc2..a20c37c 100644
--- a/crates/victory-gpu/src/draw.rs
+++ b/crates/victory-gpu/src/draw.rs
@@ -672,12 +672,16 @@ impl State {
                 .max()
                 .unwrap_or(1);
             let needs_fresh = (0..n).any(|k| {
+                // Match the shadow used to create the CBV. A padded lookup
+                // can select a different shadow for loose guest memory.
                 matches!(
                     crate::descriptor::view(table + u64::from(k) * 32),
                     Some(View::Constant(address, size))
                         if size == 0
-                            || !crate::shadow::find(address, u64::from(size.next_multiple_of(256)))
-                                .is_some_and(|s| s.gpu(address).is_multiple_of(256))
+                            || !crate::shadow::find(address, u64::from(size)).is_some_and(|s| {
+                                s.gpu(address).is_multiple_of(256)
+                                    && s.left(address) >= u64::from(size).next_multiple_of(256)
+                            })
                 )
             });
             if needs_fresh {
@@ -1280,12 +1284,25 @@ mod tests {
         );
         let heap_start = slot::<extern "system" fn(This) -> u64>(heap_obj, 0x48)(heap_obj);
 
-        // Create a 4096-byte committed buffer so its shadow buffer is 256-byte aligned.
+        // Leave an aligned start for a tail shorter than the host's CBV alignment.
+        const TAIL_OFFSET: u32 = 4096;
+        const TAIL_SIZE: u32 = 128;
         let props = [2u32, 0, 0, 0, 0]; // UPLOAD heap
         let buf_desc = [
             1u32, // BUFFER
-            0, 0, 0, 4096, 0, // alignment=0, width=4096
-            1, 0x0001_0001, 0, 1, 0, 1, 0, 0,
+            0,
+            0,
+            0,
+            TAIL_OFFSET + TAIL_SIZE,
+            0,
+            1,
+            0x0001_0001,
+            0,
+            1,
+            0,
+            1,
+            0,
+            0,
         ];
         let mut buf: This = null;
         let res_iid = ID3D12Resource::IID;
@@ -1303,8 +1320,13 @@ mod tests {
             S_OK
         );
         let (data, len) = crate::resource::buffer_bytes(buf).expect("buffer bytes");
-        assert!(len >= 4096);
+        assert_eq!(len, u64::from(TAIL_OFFSET + TAIL_SIZE));
         let base = data as u64;
+        let tail = base + u64::from(TAIL_OFFSET);
+        let tail_shadow = crate::shadow::find(tail, u64::from(TAIL_SIZE))
+            .expect("the unpadded tail fits in the shadow buffer");
+        assert!(tail_shadow.gpu(tail).is_multiple_of(256));
+        assert_eq!(tail_shadow.left(tail), u64::from(TAIL_SIZE));
 
         let write_cbv = |slot_idx: u64, addr: u64, size: u32| {
             let cbv = [addr as u32, (addr >> 32) as u32, size, 0];
@@ -1314,8 +1336,8 @@ mod tests {
         write_cbv(0, base, 256);
         // Slot 1: 16-byte aligned (misaligned for PC) CBV -> needs dynamic slots.
         write_cbv(1, base + 16, 256);
-        // Slot 2: 256-byte aligned start, but padded 256-byte size runs past shadow end -> needs dynamic slots.
-        write_cbv(2, base + 4096 - 128, 128);
+        // Slot 2: only the padded size runs past the shadow end.
+        write_cbv(2, tail, TAIL_SIZE);
 
         let used = [UsedResource {
             kind: ResourceType::Cbv,
**@⁠andrewpozdnakov7-jpg** on GitHub · 2026-11-24 12:55 UTC · [original](https://github.com/Marvisak/VIctory/issues/840#issuecomment-7132113214) I checked the padded-end case on `1d21e06`. The original fixture uses offset `4096 - 128`, so it exercises the misalignment fallback. I moved it to offset `4096` in a `4224`-byte buffer and verified the host GPU address is aligned and exactly 128 bytes remain in its shadow. With that fixture, the fork still mirrors the tail in place. The padded `shadow::find` can return a different loose shadow, while creating the CBV looks up the unpadded range. The incremental patch uses the same shadow as CBV creation and checks the padded size against the bytes left in it. On Windows WARP, the aligned 256-byte control still used zero dynamic slots. The misaligned control and the aligned 128-byte tail each used the fallback. The diagnostics library suite passed 54 tests with one benchmark ignored. AI-assisted. Verified with synthetic buffers. <details> <summary>Incremental patch on 1d21e06</summary> ```diff diff --git a/crates/victory-gpu/src/draw.rs b/crates/victory-gpu/src/draw.rs index f915dc2..a20c37c 100644 --- a/crates/victory-gpu/src/draw.rs +++ b/crates/victory-gpu/src/draw.rs @@ -672,12 +672,16 @@ impl State { .max() .unwrap_or(1); let needs_fresh = (0..n).any(|k| { + // Match the shadow used to create the CBV. A padded lookup + // can select a different shadow for loose guest memory. matches!( crate::descriptor::view(table + u64::from(k) * 32), Some(View::Constant(address, size)) if size == 0 - || !crate::shadow::find(address, u64::from(size.next_multiple_of(256))) - .is_some_and(|s| s.gpu(address).is_multiple_of(256)) + || !crate::shadow::find(address, u64::from(size)).is_some_and(|s| { + s.gpu(address).is_multiple_of(256) + && s.left(address) >= u64::from(size).next_multiple_of(256) + }) ) }); if needs_fresh { @@ -1280,12 +1284,25 @@ mod tests { ); let heap_start = slot::<extern "system" fn(This) -> u64>(heap_obj, 0x48)(heap_obj); - // Create a 4096-byte committed buffer so its shadow buffer is 256-byte aligned. + // Leave an aligned start for a tail shorter than the host's CBV alignment. + const TAIL_OFFSET: u32 = 4096; + const TAIL_SIZE: u32 = 128; let props = [2u32, 0, 0, 0, 0]; // UPLOAD heap let buf_desc = [ 1u32, // BUFFER - 0, 0, 0, 4096, 0, // alignment=0, width=4096 - 1, 0x0001_0001, 0, 1, 0, 1, 0, 0, + 0, + 0, + 0, + TAIL_OFFSET + TAIL_SIZE, + 0, + 1, + 0x0001_0001, + 0, + 1, + 0, + 1, + 0, + 0, ]; let mut buf: This = null; let res_iid = ID3D12Resource::IID; @@ -1303,8 +1320,13 @@ mod tests { S_OK ); let (data, len) = crate::resource::buffer_bytes(buf).expect("buffer bytes"); - assert!(len >= 4096); + assert_eq!(len, u64::from(TAIL_OFFSET + TAIL_SIZE)); let base = data as u64; + let tail = base + u64::from(TAIL_OFFSET); + let tail_shadow = crate::shadow::find(tail, u64::from(TAIL_SIZE)) + .expect("the unpadded tail fits in the shadow buffer"); + assert!(tail_shadow.gpu(tail).is_multiple_of(256)); + assert_eq!(tail_shadow.left(tail), u64::from(TAIL_SIZE)); let write_cbv = |slot_idx: u64, addr: u64, size: u32| { let cbv = [addr as u32, (addr >> 32) as u32, size, 0]; @@ -1314,8 +1336,8 @@ mod tests { write_cbv(0, base, 256); // Slot 1: 16-byte aligned (misaligned for PC) CBV -> needs dynamic slots. write_cbv(1, base + 16, 256); - // Slot 2: 256-byte aligned start, but padded 256-byte size runs past shadow end -> needs dynamic slots. - write_cbv(2, base + 4096 - 128, 128); + // Slot 2: only the padded size runs past the shadow end. + write_cbv(2, tail, TAIL_SIZE); let used = [UsedResource { kind: ResourceType::Cbv, ``` </details>
Author
Owner

@⁠MichaelSmithAI on GitHub · 2026-11-24 12:56 UTC · original

Melissa verified the bytes seen by a compute shader after recording a dispatch and then changing the guest constant buffer before submission. On RTX 4090 under Wine/vkd3d-proton 3.2, successive dispatches through the aligned CBV read 17 and 29 with zero dynamic slots. The CBV at byte offset +16 read 43 using one dynamic slot.

One interaction with static table entries: when the shader metadata names slots 0 and 2 but slot 1 is also populated, the fresh-table path materializes slot 1. A preparation test on that layout selected zero fresh tables without the extra check and one after preserving the fresh path for the unnamed entry. The integration checks the entire span against its owning heap before scanning and incorporates the unpadded shadow lookup and aligned short-tail case already identified by andrewpozdnakov7-jpg.

On exact eb1339c, the Win64 GPU suite had 51 passes, one ignored test and the existing Wine rejected-pipeline-library test failure. These synthetic tests verify submission-time contents and descriptor selection. Your 4,096+ to zero drop count remains the measurement from the title stack. Integration and regression tests

**@⁠MichaelSmithAI** on GitHub · 2026-11-24 12:56 UTC · [original](https://github.com/Marvisak/VIctory/issues/840#issuecomment-7132131461) Melissa verified the bytes seen by a compute shader after recording a dispatch and then changing the guest constant buffer before submission. On RTX 4090 under Wine/vkd3d-proton 3.2, successive dispatches through the aligned CBV read 17 and 29 with zero dynamic slots. The CBV at byte offset +16 read 43 using one dynamic slot. One interaction with static table entries: when the shader metadata names slots 0 and 2 but slot 1 is also populated, the fresh-table path materializes slot 1. A preparation test on that layout selected zero fresh tables without the extra check and one after preserving the fresh path for the unnamed entry. The integration checks the entire span against its owning heap before scanning and incorporates the unpadded shadow lookup and aligned short-tail case already identified by andrewpozdnakov7-jpg. On exact `eb1339c`, the Win64 GPU suite had 51 passes, one ignored test and the existing Wine rejected-pipeline-library test failure. These synthetic tests verify submission-time contents and descriptor selection. Your 4,096+ to zero drop count remains the measurement from the title stack. [Integration and regression tests](https://github.com/MichaelSmithAI/VIctory/tree/test/840-owned-cbv-tables)
Author
Owner

@⁠alessiodeia on GitHub · 2026-11-24 13:31 UTC · original

Thanks @⁠andrewpozdnakov7-jpg and @⁠MichaelSmithAI for the thorough reviews and hardware checks! Both edge cases are now fixed in alessiodeia/VIctory@8fa463a on fix/reuse-aligned-cbv-tables:

  1. Exact shadow lookup & padded-end check (@⁠andrewpozdnakov7-jpg): State::tables() now calls shadow::find(address, u64::from(size)) (matching make()) and verifies both s.gpu(address).is_multiple_of(256) and s.left(address) >= u64::from(size).next_multiple_of(256). The tail-bounds unit test fixture now uses a 4224-byte buffer at aligned offset 4096 (size = 128) with explicit assertions on gpu(tail) % 256 == 0 and left(tail) == 128.
  2. Full 0..n host-heap bounds & unnamed populated slots (@⁠MichaelSmithAI): State::tables() now checks that every slot in 0..n fits inside the host heap (heap.index(handle).is_none_or(|i| i >= host.count())) and falls back to dynamic slots if any populated slot in 0..n is not named in used, preserving dynamically-indexed descriptor-array entries. Added a unit test covering a sparse table (slots 0 and 2 named, slot 1 populated and unnamed) confirming dynamic-slot fallback.

Verified locally on Windows:

  • cargo test -p victory-gpu --locked --offline -- --test-threads=1: 51 passed, 0 failed
  • cargo clippy -p victory-gpu --all-targets -- -D warnings (default & --features diagnostics): clean
  • cargo --offline xtask policy: 154 files ok
**@⁠alessiodeia** on GitHub · 2026-11-24 13:31 UTC · [original](https://github.com/Marvisak/VIctory/issues/840#issuecomment-7132961389) Thanks @⁠andrewpozdnakov7-jpg and @⁠MichaelSmithAI for the thorough reviews and hardware checks! Both edge cases are now fixed in `alessiodeia/VIctory@8fa463a` on `fix/reuse-aligned-cbv-tables`: 1. **Exact shadow lookup & padded-end check (@⁠andrewpozdnakov7-jpg):** `State::tables()` now calls `shadow::find(address, u64::from(size))` (matching `make()`) and verifies both `s.gpu(address).is_multiple_of(256)` and `s.left(address) >= u64::from(size).next_multiple_of(256)`. The tail-bounds unit test fixture now uses a 4224-byte buffer at aligned offset 4096 (`size = 128`) with explicit assertions on `gpu(tail) % 256 == 0` and `left(tail) == 128`. 2. **Full `0..n` host-heap bounds & unnamed populated slots (@⁠MichaelSmithAI):** `State::tables()` now checks that every slot in `0..n` fits inside the host heap (`heap.index(handle).is_none_or(|i| i >= host.count())`) and falls back to dynamic slots if any populated slot in `0..n` is not named in `used`, preserving dynamically-indexed descriptor-array entries. Added a unit test covering a sparse table (slots 0 and 2 named, slot 1 populated and unnamed) confirming dynamic-slot fallback. Verified locally on Windows: - `cargo test -p victory-gpu --locked --offline -- --test-threads=1`: 51 passed, 0 failed - `cargo clippy -p victory-gpu --all-targets -- -D warnings` (default & `--features diagnostics`): clean - `cargo --offline xtask policy`: 154 files ok
Author
Owner

@⁠andrewpozdnakov7-jpg on GitHub · 2026-11-24 13:45 UTC · original

Thanks @⁠MichaelSmithAI for including the aligned short-tail fix and regression in the integration. Commit eb1339c

Could you retain this Git trailer when carrying that contribution forward?

Co-authored-by: andrewpozdnakov7-jpg <[email protected]>

**@⁠andrewpozdnakov7-jpg** on GitHub · 2026-11-24 13:45 UTC · [original](https://github.com/Marvisak/VIctory/issues/840#issuecomment-7133299848) Thanks @⁠MichaelSmithAI for including the aligned short-tail fix and regression in the integration. [Commit `eb1339c`](https://github.com/MichaelSmithAI/VIctory/commit/eb1339c9bd60a2f4b63ae9fe6dafeb06c4adde71) Could you retain this Git trailer when carrying that contribution forward? `Co-authored-by: andrewpozdnakov7-jpg <[email protected]>`
Author
Owner

@⁠alessiodeia on GitHub · 2026-11-24 16:57 UTC · original

@⁠andrewpozdnakov7-jpg your shadow-lookup and padded-end fix is in our tip commit too, so I've added your trailer there: fix/reuse-aligned-cbv-tables is now e5e227d. It's the same tree as 8fa463a; only the commit message changed (Co-authored-by: andrewpozdnakov7-jpg <[email protected]>). Anyone who merged 8fa463a has identical code.

**@⁠alessiodeia** on GitHub · 2026-11-24 16:57 UTC · [original](https://github.com/Marvisak/VIctory/issues/840#issuecomment-7138456424) @⁠andrewpozdnakov7-jpg your shadow-lookup and padded-end fix is in our tip commit too, so I've added your trailer there: `fix/reuse-aligned-cbv-tables` is now `e5e227d`. It's the same tree as `8fa463a`; only the commit message changed (`Co-authored-by: andrewpozdnakov7-jpg <[email protected]>`). Anyone who merged `8fa463a` has identical code.
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 03:54 UTC · original

Automated triage for the maintainers.

The behaviour this patch changes is no longer on main. Since commit 683ad7a ("table constants read in place"), State::tables in crates/victory-gpu/src/draw.rs no longer sends every table that holds a constant buffer to dynamic slots. Table constant buffers are now raw views of their data, refreshed at submission, and a table gets dynamic slots only when it reaches past a capped host heap (index >= host.count()). The fork branch (e5e227d, based on ffe310e) targets the old code path.

If you still see no dynamic slots for its tables drops on current main, please post a new run report with the counts. The reclaiming of finished lists' slots is open as #688. Proposed for closing as fixed in main; this will be closed in 48 hours unless the reporter replies or a maintainer removes the label.


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-25 03:54 UTC · [original](https://github.com/Marvisak/VIctory/issues/840#issuecomment-7152358565) <!-- victory-issue-triage v1 verdict=proposed-close seen=2026-11-24T16:57:01Z at=2026-11-25T03:54:58Z --> Automated triage for the maintainers. The behaviour this patch changes is no longer on `main`. Since commit `683ad7a` ("table constants read in place"), `State::tables` in `crates/victory-gpu/src/draw.rs` no longer sends every table that holds a constant buffer to dynamic slots. Table constant buffers are now raw views of their data, refreshed at submission, and a table gets dynamic slots only when it reaches past a capped host heap (`index >= host.count()`). The fork branch (`e5e227d`, based on `ffe310e`) targets the old code path. If you still see `no dynamic slots for its tables` drops on current `main`, please post a new run report with the counts. The reclaiming of finished lists' slots is open as #688. Proposed for closing as fixed in `main`; this will be closed in 48 hours unless the reporter replies or a maintainer removes the label. --- _Generated by [Claude Code](https://claude.ai/code)_
Author
Owner

@⁠alessiodeia on GitHub · 2026-11-25 09:40 UTC · original

Agreed. On main the path this patch changed is gone: since 683ad7a, table constant buffers are raw views refreshed at submission, and only tables past a capped host heap get dynamic slots. The e5e227d branch targets the old code, so I'm closing this as fixed in main. If no dynamic slots for its tables drops come back on current main, I'll open a new report with counts.

**@⁠alessiodeia** on GitHub · 2026-11-25 09:40 UTC · [original](https://github.com/Marvisak/VIctory/issues/840#issuecomment-7158647245) Agreed. On `main` the path this patch changed is gone: since `683ad7a`, table constant buffers are raw views refreshed at submission, and only tables past a capped host heap get dynamic slots. The `e5e227d` branch targets the old code, so I'm closing this as fixed in `main`. If `no dynamic slots for its tables` drops come back on current `main`, I'll open a new report with counts.
This discussion has been locked. Commenting is limited to contributors.
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#840
No description provided.