GPU: reuse idle 16 MiB title-memory page tables across view heaps #3662

Open
Luislariosss wants to merge 1 commit from upstream-pr/3662 into main
Luislariosss commented 2026-11-28 03:57:32 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Luislariosss on 2026-11-28T03:57:32Z.


Each view heap that touches title memory allocates a ~16.05 MiB (TABLE_DWORDS) page-table buffer in memory::Shared::new, which was previously destroyed whenever the heap was dropped and reallocated from scratch for the next heap.

Changes

  • Add a bounded idle pool (TABLE_POOL_CAP = 1) for page-table buffers in crates/victory-gpu/src/memory.rs.
  • In Drop for Shared, only return the table to the pool when sole_holder confirms no command list still holds a COM reference (list.keep(&s.table)); otherwise drop it as before so no in-flight GPU work can observe reuse.
  • In Shared::new, reuse and zero a pooled table when available before allocating a new one.
  • Add unit test memory::tests::idle_tables_are_capped.

Verification

  • cargo xtask policy (191 files ok)
  • cargo clippy -p victory-gpu --all-targets -- -D warnings (default and --features diagnostics)
  • cargo test -p victory-gpu --lib memory:: (9/9) and descriptor:: (22/22)
<!-- greyfinch-sync:Marvisak/VIctory:issue:5536442407 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3662). Originally posted by [Luislariosss](https://github.com/Luislariosss) on 2026-11-28T03:57:32Z. --- Each view heap that touches title memory allocates a ~16.05 MiB (`TABLE_DWORDS`) page-table buffer in `memory::Shared::new`, which was previously destroyed whenever the heap was dropped and reallocated from scratch for the next heap. ### Changes - Add a bounded idle pool (`TABLE_POOL_CAP = 1`) for page-table buffers in `crates/victory-gpu/src/memory.rs`. - In `Drop for Shared`, only return the table to the pool when `sole_holder` confirms no command list still holds a COM reference (`list.keep(&s.table)`); otherwise drop it as before so no in-flight GPU work can observe reuse. - In `Shared::new`, reuse and zero a pooled table when available before allocating a new one. - Add unit test `memory::tests::idle_tables_are_capped`. ### Verification - `cargo xtask policy` (191 files ok) - `cargo clippy -p victory-gpu --all-targets -- -D warnings` (default and `--features diagnostics`) - `cargo test -p victory-gpu --lib memory::` (9/9) and `descriptor::` (22/22)
oni-swr commented 2026-11-28 07:32:53 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by oni-swr on 2026-11-28T07:32:53Z.


Independent verification (software WARP only) — AI disclosure: Claude Sonnet 5.5 (Claude Code).

Environment: Windows 11 26200, VICTORY_WARP=1, VICTORY_D3D12_WARP=1, --features diagnostics (host.rs warp_adapter() path, which refuses a non-software adapter). Physical GPU not used. d3d10warp.dll 10.0.26100.9818, dxil.dll 1.8.2502.11 (Windows Kits 10.0.26100.0 x64).

SHAs: main 63bcca43c6a00a758510398fa43096ce7e4f7df0; PR head 3907257c72a7a23e533a6e5be784e2b39e21117b; merges cleanly, merge tree 2b048cbee67ccf5069ca74f261e2d7579b6eb971. Diff: memory.rs only (+85/-6).

Results (cargo test --locked -p victory-gpu --features diagnostics -j 4 --no-fail-fast):

  • base: 246 passed, 0 failed, 1 ignored; idle_tables_are_capped absent.
  • merged: 247 passed, 0 failed, 1 ignored; memory::tests::idle_tables_are_capped ok; 31 memory::/descriptor:: tests ok.
  • cargo clippy --locked -p victory-gpu --all-targets -j 4 -- -D warnings: clean, default and --features diagnostics.
  • cargo xtask policy: 191 files ok.

Review notes (no blockers):

  • sole_holder: AddRef/Release returning 2 means the dropped Shared is the only owner. No TOCTOU is possible, since a new reference can only be taken from an existing one and no other holder remains. A list that still holds it (via keep) makes the buffer be released rather than pooled, which is the conservative choice. Lock order is SHARED then TABLE_POOL on both paths, so there is no inversion.
  • ManuallyDrop::take in Drop for Shared is called once, and table is not used afterwards. bind derefs through &*s.table. Sound.
  • TABLE_POOL is a process-wide static, and host() is a single OnceLock device, so a pooled buffer can't outlive its device. Cap of 1 means at most 16 MiB idle.
  • write_bytes zeroing matches a fresh upload_buffer (all level-1 entries are 0, so everything routes to block 0 slot 0). It does not apply the null-pair init from #3119 (open, same Shared::new region). If #3119 lands first, the pooled path must apply that same null init after the zeroing, or reused tables would route to slot 0 until rewrite. Expect a textual/semantic overlap there. #3106 and #3487 also touch memory.rs. I did not test those merges.
  • Test gap: only the cap helper is unit-tested. The reuse path (sole_holder, zeroing) is not covered, though the existing GPU tests exercise Shared::new under WARP.
<!-- greyfinch-sync:Marvisak/VIctory:comment:7236009776 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3662#issuecomment-7236009776). Originally posted by [oni-swr](https://github.com/oni-swr) on 2026-11-28T07:32:53Z. --- **Independent verification (software WARP only)** — AI disclosure: Claude Sonnet 5.5 (Claude Code). **Environment:** Windows 11 26200, `VICTORY_WARP=1`, `VICTORY_D3D12_WARP=1`, `--features diagnostics` (host.rs `warp_adapter()` path, which refuses a non-software adapter). Physical GPU not used. `d3d10warp.dll` 10.0.26100.9818, `dxil.dll` 1.8.2502.11 (Windows Kits 10.0.26100.0 x64). **SHAs:** main `63bcca43c6a00a758510398fa43096ce7e4f7df0`; PR head `3907257c72a7a23e533a6e5be784e2b39e21117b`; merges cleanly, merge tree `2b048cbee67ccf5069ca74f261e2d7579b6eb971`. Diff: `memory.rs` only (+85/-6). **Results** (`cargo test --locked -p victory-gpu --features diagnostics -j 4 --no-fail-fast`): - base: 246 passed, 0 failed, 1 ignored; `idle_tables_are_capped` absent. - merged: 247 passed, 0 failed, 1 ignored; `memory::tests::idle_tables_are_capped` ok; 31 `memory::`/`descriptor::` tests ok. - `cargo clippy --locked -p victory-gpu --all-targets -j 4 -- -D warnings`: clean, default and `--features diagnostics`. - `cargo xtask policy`: 191 files ok. **Review notes (no blockers):** - `sole_holder`: AddRef/Release returning 2 means the dropped `Shared` is the only owner. No TOCTOU is possible, since a new reference can only be taken from an existing one and no other holder remains. A list that still holds it (via `keep`) makes the buffer be released rather than pooled, which is the conservative choice. Lock order is SHARED then TABLE_POOL on both paths, so there is no inversion. - `ManuallyDrop::take` in `Drop for Shared` is called once, and `table` is not used afterwards. `bind` derefs through `&*s.table`. Sound. - `TABLE_POOL` is a process-wide static, and `host()` is a single `OnceLock` device, so a pooled buffer can't outlive its device. Cap of 1 means at most 16 MiB idle. - `write_bytes` zeroing matches a fresh `upload_buffer` (all level-1 entries are 0, so everything routes to block 0 slot 0). It does **not** apply the null-pair init from #3119 (open, same `Shared::new` region). If #3119 lands first, the pooled path must apply that same null init after the zeroing, or reused tables would route to slot 0 until `rewrite`. Expect a textual/semantic overlap there. #3106 and #3487 also touch `memory.rs`. I did not test those merges. - Test gap: only the cap helper is unit-tested. The reuse path (`sole_holder`, zeroing) is not covered, though the existing GPU tests exercise `Shared::new` under WARP.
reallyvibe commented 2026-11-29 01:10:46 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by reallyvibe on 2026-11-29T01:10:46Z.


Merge checks for the two open points in oni-swr's report: the #3119 overlap and the untested reuse path

oni-swr's report left two questions open: whether pooled tables get #3119's null-pair init, and whether the reuse path is tested. I merged this head 3907257c with #3119 fe530463 on main 63bcca43. The merge is clean, and #3119's block lands after this PR's match idle, so the null pair is written into pooled tables as well as fresh ones. cargo build -p victory-gpu --lib and cargo clippy -p victory-gpu --all-targets -- -D warnings (default and --all-features) are clean.

For the reuse path I wrote the regression test below, against the merged tree. You're welcome to take it into this PR, or into #3119, whichever lands second. Its null-pair checks need #3119. Without #3119, keep only the pooling, zeroing and held-table checks.

    /// A page table taken from the pool routes every lookup to its heap's
    /// `null` view pair before `rewrite`, as a fresh one does, whatever the
    /// table held before; a table a list still holds is not pooled.
    #[test]
    fn pooled_table_routes_all_lookups_to_null_before_first_rewrite() {
        use windows::core::Interface;
        let _guard = gpu_test_lock();
        let (Some(_), Some(views)) = (crate::host::host(), HostHeap::new(false, 16)) else {
            return; // no host GPU here
        };
        TABLE_POOL.lock().unwrap().clear();
        let s = Shared::new(&views).expect("a page table");
        let raw = s.table.as_raw();
        // SAFETY: `s.data` is the persistent mapping of `s.table`, with room
        // for `TABLE_DWORDS` dwords, and no submission reads it.
        unsafe { std::slice::from_raw_parts_mut(s.data as *mut u32, TABLE_DWORDS) }.fill(0x5a5a_5a5a);
        drop(s);
        assert_eq!(TABLE_POOL.lock().unwrap().len(), 1, "the sole holder's table is pooled");
        let s = Shared::new(&views).expect("a page table");
        assert_eq!(s.table.as_raw(), raw, "the pooled table is reused");
        assert_ne!(s.null, 0);
        // SAFETY: as above.
        let dwords = unsafe { std::slice::from_raw_parts(s.data as *const u32, TABLE_DWORDS) };
        assert!(dwords[..LEVEL1].iter().all(|&b| b == 0), "every level-1 entry points at block 0");
        let (block0, _) = dwords[LEVEL1..LEVEL1 + BLOCK * 2].as_chunks::<2>();
        assert!(block0.iter().all(|&slot| slot == [s.null, 0]), "block 0 names the null pair");
        assert!(dwords[LEVEL1 + BLOCK * 2..].iter().all(|&d| d == 0), "the rest is cleared");
        for address in [0u64, 0x10, 0x40_0010, 0xd_1234_0010, 0xff_ffff_fff0, 1u64 << 40] {
            assert_eq!(look_up(dwords, address), (s.null, address as u32));
        }
        // A table a list still holds is not pooled.
        let list = crate::host::host().unwrap().new_list().unwrap();
        list.keep(&*s.table);
        drop(s);
        assert_eq!(TABLE_POOL.lock().unwrap().len(), 0, "a table a list holds is released, not pooled");
        drop(list);
    }

On the merged tree with this test, WARP memory:: passes 11/11. I mutated the tree one change at a time and ran each test in its own process; the test fails each time:

change fails
#3119's init only for freshly made tables this test (block 0 not the null pair); #3119's own test still passes
no write_bytes on a pooled table this test (old contents remain)
pooled whatever holds it (no sole_holder check) this test (a held table pooled)

So the null-pair concern doesn't apply to the merge as it stands. It would come back if a rebase moved #3119's init into the fresh-buffer branch only. #3119's test alone would not catch that unless a pooled table happened to be available when it runs. #3487 612207a8 also merges cleanly with this head: the lib and the test binary build, and WARP memory:: passes 13/13. #3106 a81d5dd4 conflicts textually in memory.rs, so that one is visible.

On lifetime: HostList::reset runs only after the list's last submission finished, and keep holds the table until then. So if sole_holder correctly reports no other COM holder, no list is still using the table. That depends on the reference count, though. sole_holder reads the value IUnknown::AddRef returns, and Microsoft documents that value as intended only for testing purposes (https://learn.microsoft.com/windows/win32/api/unknwn/nf-unknwn-iunknown-addref). This is an API-contract concern, not a reproduced failure: the WARP tests above passed. Whether the count is reliable on other drivers and runtimes isn't established.

Not measured: the time pooling saves, which this PR doesn't claim.

Windows 11, Ryzen 7 7445HS, WARP, synthetic data only.

AI-assisted (Claude Code).

<!-- greyfinch-sync:Marvisak/VIctory:comment:7248613119 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3662#issuecomment-7248613119). Originally posted by [reallyvibe](https://github.com/reallyvibe) on 2026-11-29T01:10:46Z. --- **Merge checks for the two open points in oni-swr's report: the #3119 overlap and the untested reuse path** oni-swr's report left two questions open: whether pooled tables get #3119's null-pair init, and whether the reuse path is tested. I merged this head `3907257c` with #3119 `fe530463` on main `63bcca43`. The merge is clean, and #3119's block lands after this PR's `match idle`, so the null pair is written into pooled tables as well as fresh ones. `cargo build -p victory-gpu --lib` and `cargo clippy -p victory-gpu --all-targets -- -D warnings` (default and `--all-features`) are clean. For the reuse path I wrote the regression test below, against the merged tree. You're welcome to take it into this PR, or into #3119, whichever lands second. Its null-pair checks need #3119. Without #3119, keep only the pooling, zeroing and held-table checks. ```rust /// A page table taken from the pool routes every lookup to its heap's /// `null` view pair before `rewrite`, as a fresh one does, whatever the /// table held before; a table a list still holds is not pooled. #[test] fn pooled_table_routes_all_lookups_to_null_before_first_rewrite() { use windows::core::Interface; let _guard = gpu_test_lock(); let (Some(_), Some(views)) = (crate::host::host(), HostHeap::new(false, 16)) else { return; // no host GPU here }; TABLE_POOL.lock().unwrap().clear(); let s = Shared::new(&views).expect("a page table"); let raw = s.table.as_raw(); // SAFETY: `s.data` is the persistent mapping of `s.table`, with room // for `TABLE_DWORDS` dwords, and no submission reads it. unsafe { std::slice::from_raw_parts_mut(s.data as *mut u32, TABLE_DWORDS) }.fill(0x5a5a_5a5a); drop(s); assert_eq!(TABLE_POOL.lock().unwrap().len(), 1, "the sole holder's table is pooled"); let s = Shared::new(&views).expect("a page table"); assert_eq!(s.table.as_raw(), raw, "the pooled table is reused"); assert_ne!(s.null, 0); // SAFETY: as above. let dwords = unsafe { std::slice::from_raw_parts(s.data as *const u32, TABLE_DWORDS) }; assert!(dwords[..LEVEL1].iter().all(|&b| b == 0), "every level-1 entry points at block 0"); let (block0, _) = dwords[LEVEL1..LEVEL1 + BLOCK * 2].as_chunks::<2>(); assert!(block0.iter().all(|&slot| slot == [s.null, 0]), "block 0 names the null pair"); assert!(dwords[LEVEL1 + BLOCK * 2..].iter().all(|&d| d == 0), "the rest is cleared"); for address in [0u64, 0x10, 0x40_0010, 0xd_1234_0010, 0xff_ffff_fff0, 1u64 << 40] { assert_eq!(look_up(dwords, address), (s.null, address as u32)); } // A table a list still holds is not pooled. let list = crate::host::host().unwrap().new_list().unwrap(); list.keep(&*s.table); drop(s); assert_eq!(TABLE_POOL.lock().unwrap().len(), 0, "a table a list holds is released, not pooled"); drop(list); } ``` On the merged tree with this test, WARP `memory::` passes 11/11. I mutated the tree one change at a time and ran each test in its own process; the test fails each time: | change | fails | |---|---| | #3119's init only for freshly made tables | this test (block 0 not the null pair); #3119's own test still passes | | no `write_bytes` on a pooled table | this test (old contents remain) | | pooled whatever holds it (no `sole_holder` check) | this test (a held table pooled) | So the null-pair concern doesn't apply to the merge as it stands. It would come back if a rebase moved #3119's init into the fresh-buffer branch only. #3119's test alone would not catch that unless a pooled table happened to be available when it runs. #3487 `612207a8` also merges cleanly with this head: the lib and the test binary build, and WARP `memory::` passes 13/13. #3106 `a81d5dd4` conflicts textually in `memory.rs`, so that one is visible. On lifetime: `HostList::reset` runs only after the list's last submission finished, and `keep` holds the table until then. So if `sole_holder` correctly reports no other COM holder, no list is still using the table. That depends on the reference count, though. `sole_holder` reads the value `IUnknown::AddRef` returns, and Microsoft documents that value as intended only for testing purposes (https://learn.microsoft.com/windows/win32/api/unknwn/nf-unknwn-iunknown-addref). This is an API-contract concern, not a reproduced failure: the WARP tests above passed. Whether the count is reliable on other drivers and runtimes isn't established. Not measured: the time pooling saves, which this PR doesn't claim. Windows 11, Ryzen 7 7445HS, WARP, synthetic data only. AI-assisted (Claude Code).
futurepro62 commented 2026-11-29 23:18:47 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by futurepro62 on 2026-11-29T23:18:47Z.


NVIDIA full-suite run, to go with the reports above by oni-swr, since none of them names a GPU run. Tested exact head 3907257 (on its base 63bcca4).

Tested

  • NVIDIA GeForce RTX 5070 Ti 16 GB (Blackwell, 32-lane waves only, mesh shaders), driver 621.40, Ryzen 7 9800X3D, Windows 11 Home 26300, Rust 1.98.1 MSVC (pinned), dxil.dll from Windows SDK 10.0.26100 (the loader's default), debug.
  • cargo build --workspace --locked, then cargo test --workspace --locked --no-fail-fast -- --test-threads=4 --skip emit::memory_tests::heap_direct_texture_reads_mark_their_heap_index --skip descriptor::host::tests::dynamic_slots_are_held_until_released --skip memory::tests::lists_share_a_table_and_copies_keep_their_views --skip emit::stride_tests::a_heap_buffer_read_by_element_takes_the_views_stride --skip emit::stride_tests::a_heap_buffer_read_where_no_buffer_view_is_reads_zeros --skip emit::synthetic_tests::gathers_at_a_level_match_the_hardware: 867 passed, 0 failed, 10 ignored.
  • The new tests ran and passed: idle_tables_are_capped.
  • --skip leaves out tests that fail on main 63bcca4 on this card too, not because of this PR: heap_direct_texture_reads_mark_their_heap_index ends the victory-shader test process with 0xc0000005 (#2571), so without the skip the rest of that crate never reports; dynamic_slots_are_held_until_released, lists_share_a_table_and_copies_keep_their_views and the two stride_tests hit the 1,000,000-view heap cap of NVIDIA drivers (#1835, #2317); gathers_at_a_level_match_the_hardware fails on lane 0 (#1744).

Not tested

  • AMD (the tests take the default adapter), a release build, the title (test run only).

AI use

  • Claude Code (Claude Opus 5.5) ran the tests and wrote this report. I checked it before posting.
<!-- greyfinch-sync:Marvisak/VIctory:comment:7264364139 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3662#issuecomment-7264364139). Originally posted by [futurepro62](https://github.com/futurepro62) on 2026-11-29T23:18:47Z. --- NVIDIA full-suite run, to go with the reports above by oni-swr, since none of them names a GPU run. Tested exact head `3907257` (on its base `63bcca4`). **Tested** - NVIDIA GeForce RTX 5070 Ti 16 GB (Blackwell, 32-lane waves only, mesh shaders), driver 621.40, Ryzen 7 9800X3D, Windows 11 Home 26300, Rust 1.98.1 MSVC (pinned), `dxil.dll` from Windows SDK 10.0.26100 (the loader's default), debug. - `cargo build --workspace --locked`, then `cargo test --workspace --locked --no-fail-fast -- --test-threads=4 --skip emit::memory_tests::heap_direct_texture_reads_mark_their_heap_index --skip descriptor::host::tests::dynamic_slots_are_held_until_released --skip memory::tests::lists_share_a_table_and_copies_keep_their_views --skip emit::stride_tests::a_heap_buffer_read_by_element_takes_the_views_stride --skip emit::stride_tests::a_heap_buffer_read_where_no_buffer_view_is_reads_zeros --skip emit::synthetic_tests::gathers_at_a_level_match_the_hardware`: **867 passed, 0 failed, 10 ignored**. - The new tests ran and passed: `idle_tables_are_capped`. - `--skip` leaves out tests that fail on main `63bcca4` on this card too, not because of this PR: `heap_direct_texture_reads_mark_their_heap_index` ends the `victory-shader` test process with `0xc0000005` (#2571), so without the skip the rest of that crate never reports; `dynamic_slots_are_held_until_released`, `lists_share_a_table_and_copies_keep_their_views` and the two `stride_tests` hit the 1,000,000-view heap cap of NVIDIA drivers (#1835, #2317); `gathers_at_a_level_match_the_hardware` fails on lane 0 (#1744). **Not tested** - AMD (the tests take the default adapter), a release build, the title (test run only). **AI use** - Claude Code (Claude Opus 5.5) ran the tests and wrote this report. I checked it before posting.
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/3662:upstream-pr/3662'
git switch 'upstream-pr/3662'

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