perf(gpu): shard HostHeap::mirror across 256 descriptor stripes #16

Open
github-bot wants to merge 2 commits from github-bot/gh-16 into beta AGit
Owner

@⁠Vladimir-Human on GitHub · 2026-11-25 16:32 UTC · original
Vladimir-Human:perf/lockfree-mirror-descriptors → beta · +939 −133 · 1 files · 2 commits · open

Replace HostHeap's single global made: Mutex<Vec<u64>> and kept: Mutex<...> with a lock-free AtomicU64 fast-path + 256 cache-line-aligned descriptor stripes and canonical-order stale != index invalidation (crates/victory-gpu/src/descriptor/host.rs), bringing KLBF Lab up to parity with victory-perf / flamingo on high-draw scenes.

What is in it (crates/victory-gpu/src/descriptor/host.rs)

  • Lock-free fast-path hits (made: Box<[AtomicU64]>): HostHeap::mirror checks tag == VIEW_TAG && key != 0 && self.made[index].load(Ordering::Acquire) == key && let Some(view) = view_at(i, generation), publishing key (Ordering::Release) only after make()/null_for() and replace_kept() complete. texture_views_remade clears made[slot] under its stripe guard while rewriting views so concurrent fast-path readers never observe a half-updated descriptor.
  • 256 striped slot locks & kept + lock-free copied: shards slow-path Create*View (make()) and kept resource slots across 256 cache-line-aligned stripes ((index & 255, index >> 8)), retiring displaced ID3D12Resource handles via crate::host::retire strictly outside kept and slot_locks. copied uses OnceLock<Box<[AtomicU64]>> CAS.
  • Deadlock-free cross-stripe stale != index invalidation: Spans::clashing_slots + HostHeap::note_buffer_checked(..., held_stripes) pre-checks missing stale stripes under placed before mutating state; HostHeap::mirror merges missing stripes into needed_stripes and acquires them strictly in ascending index order (0..255).
  • Tests & benchmarks: adds mirror_release_acquire_later_and_forget_semantics, concurrent_cache_hit_and_distinct_create_view_storm, concurrent_same_slot_and_stale_invalidations_do_not_deadlock, concurrent_stale_invalidation_and_cbv_overwrite_never_clobbers_live_slot, concurrent_texture_views_remade_and_fast_path_stay_consistent, and 8-thread --release benchmarks.

Verification (f5a960c, Windows 11 Build 26300, NVIDIA GeForce RTX 4070 SUPER, Rust 1.98.1)

  • VICTORY_D3D12_DEBUG=1 cargo test -p victory-gpu --features diagnostics --locked --lib descriptor::host::tests:: -- --test-threads=1: 24 passed, 0 failed, 2 ignored (#[ignore] benches) (0 D3D12 debug errors/warnings).
  • cargo clippy --workspace --all-targets --locked -- -D warnings (default & --all-features): 0 warnings in crates/victory-gpu/src/descriptor/host.rs (fails only on pre-existing origin/klbf-amd / beta lints in untouched sgd5.rs:557, resource.rs:2667, draw.rs:2441, indirect.rs:1574).
  • cargo test --workspace --locked: 585 passed, 1 pre-existing failed, 14 ignored (sole failure is pre-existing origin/klbf-amd / beta xtask policy::tests::repository_passes_policy on untouched crates/victory-shader/src/emit/lower.rs > 512 KiB and docs/klbf/logo.svg).
  • Independent BEFORE (0ad4037) vs AFTER (f5a960c) 8-thread --release benchmark on RTX 4070 SUPER:
    • bench_mirror_fast_path_8_threads[cbv_inline] (400k hits): median 50.639 -> 0.782 ms (64.8x faster, 7.90 -> 511.51 Mops/s)
    • bench_mirror_fast_path_8_threads[mixed_cbv_srv] (400k hits): median 52.075 -> 13.479 ms (3.86x faster, 7.68 -> 29.68 Mops/s)
    • bench_mirror_slow_path_create_views_8_threads (16,384 slots): median 12.002 -> 6.569 ms (1.83x faster, 1,365 -> 2,494 Kviews/s).

Not tested

  • Retail title E2E runtime run (clean-room development environment without game files).
  • AMD/Intel GPUs and Linux hosts.

AI use

  • AI-assisted implementation, concurrency proof/audit, and benchmarks (gemini-4-hydrogen-xhigh + gemini-4-neon-xhigh).
**@⁠Vladimir-Human** on GitHub · 2026-11-25 16:32 UTC · [original](https://github.com/PastiiJay/klbf-lab/pull/16) `Vladimir-Human:perf/lockfree-mirror-descriptors` → `beta` · +939 −133 · 1 files · 2 commits · open Replace `HostHeap`'s single global `made: Mutex<Vec<u64>>` and `kept: Mutex<...>` with a lock-free `AtomicU64` fast-path + 256 cache-line-aligned descriptor stripes and canonical-order `stale != index` invalidation (`crates/victory-gpu/src/descriptor/host.rs`), bringing KLBF Lab up to parity with `victory-perf` / `flamingo` on high-draw scenes. ### What is in it (`crates/victory-gpu/src/descriptor/host.rs`) - **Lock-free fast-path hits (`made: Box<[AtomicU64]>`)**: `HostHeap::mirror` checks `tag == VIEW_TAG && key != 0 && self.made[index].load(Ordering::Acquire) == key && let Some(view) = view_at(i, generation)`, publishing `key` (`Ordering::Release`) only after `make()`/`null_for()` and `replace_kept()` complete. `texture_views_remade` clears `made[slot]` under its stripe guard while rewriting views so concurrent fast-path readers never observe a half-updated descriptor. - **256 striped slot locks & `kept` + lock-free `copied`**: shards slow-path `Create*View` (`make()`) and kept resource slots across 256 cache-line-aligned stripes (`(index & 255, index >> 8)`), retiring displaced `ID3D12Resource` handles via `crate::host::retire` strictly outside `kept` and `slot_locks`. `copied` uses `OnceLock<Box<[AtomicU64]>>` CAS. - **Deadlock-free cross-stripe `stale != index` invalidation**: `Spans::clashing_slots` + `HostHeap::note_buffer_checked(..., held_stripes)` pre-checks missing stale stripes under `placed` **before** mutating state; `HostHeap::mirror` merges missing stripes into `needed_stripes` and acquires them strictly in ascending index order (`0..255`). - **Tests & benchmarks**: adds `mirror_release_acquire_later_and_forget_semantics`, `concurrent_cache_hit_and_distinct_create_view_storm`, `concurrent_same_slot_and_stale_invalidations_do_not_deadlock`, `concurrent_stale_invalidation_and_cbv_overwrite_never_clobbers_live_slot`, `concurrent_texture_views_remade_and_fast_path_stay_consistent`, and 8-thread `--release` benchmarks. ### Verification (`f5a960c`, Windows 11 Build 26300, NVIDIA GeForce RTX 4070 SUPER, Rust 1.98.1) - `VICTORY_D3D12_DEBUG=1 cargo test -p victory-gpu --features diagnostics --locked --lib descriptor::host::tests:: -- --test-threads=1`: **24 passed, 0 failed, 2 ignored (`#[ignore]` benches)** (`0` D3D12 debug errors/warnings). - `cargo clippy --workspace --all-targets --locked -- -D warnings` (default & `--all-features`): **0 warnings in `crates/victory-gpu/src/descriptor/host.rs`** (fails only on pre-existing `origin/klbf-amd` / `beta` lints in untouched `sgd5.rs:557`, `resource.rs:2667`, `draw.rs:2441`, `indirect.rs:1574`). - `cargo test --workspace --locked`: **585 passed, 1 pre-existing failed, 14 ignored** (sole failure is pre-existing `origin/klbf-amd` / `beta` `xtask` `policy::tests::repository_passes_policy` on untouched `crates/victory-shader/src/emit/lower.rs` > 512 KiB and `docs/klbf/logo.svg`). - **Independent BEFORE (`0ad4037`) vs AFTER (`f5a960c`) 8-thread `--release` benchmark on RTX 4070 SUPER**: - `bench_mirror_fast_path_8_threads[cbv_inline]` (400k hits): median **`50.639 -> 0.782 ms`** (**64.8x faster**, `7.90 -> 511.51 Mops/s`) - `bench_mirror_fast_path_8_threads[mixed_cbv_srv]` (400k hits): median **`52.075 -> 13.479 ms`** (**3.86x faster**, `7.68 -> 29.68 Mops/s`) - `bench_mirror_slow_path_create_views_8_threads` (16,384 slots): median **`12.002 -> 6.569 ms`** (**1.83x faster**, `1,365 -> 2,494 Kviews/s`). ### Not tested - Retail title E2E runtime run (clean-room development environment without game files). - AMD/Intel GPUs and Linux hosts. ### AI use - AI-assisted implementation, concurrency proof/audit, and benchmarks (`gemini-4-hydrogen-xhigh` + `gemini-4-neon-xhigh`).
Replace the global `HostHeap::made` mutex with `Box<[AtomicU64]>` using
Acquire/Release ordering so descriptor cache hits execute lock-free
(storing the view key with Release only after `make()` and `keep()`
finish). Guard slow-path slot updates and `kept` storage with 256
cache-aligned stripes, retire displaced COM resources outside all heap
locks, switch `copied` to lock-free `OnceLock<Box<[AtomicU64]>>` CAS,
and order/defer cross-stripe `stale != index` invalidations from
`note_buffer()` with a `Spans` sequence guard to eliminate AB-BA
deadlocks.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 05:52:03 +00:00
Author
Owner

@⁠HandshakeHackin on GitHub · 2026-11-25 16:52 UTC · original

I ran the four new concurrent descriptor tests on Windows with VICTORY_D3D12_WARP=1 and the diagnostics feature. All four passed. In the verification checkout I made the test setup fail if it could not create the host or descriptor heap, so a missing adapter could not silently skip the assertions. This covers the software backend. I did not measure performance.

Windows WARP test output

**@⁠HandshakeHackin** on GitHub · 2026-11-25 16:52 UTC · [original](https://github.com/PastiiJay/klbf-lab/pull/16#issuecomment-7168672015) I ran the four new concurrent descriptor tests on Windows with `VICTORY_D3D12_WARP=1` and the diagnostics feature. All four passed. In the verification checkout I made the test setup fail if it could not create the host or descriptor heap, so a missing adapter could not silently skip the assertions. This covers the software backend. I did not measure performance. [Windows WARP test output](https://github.com/HandshakeHackin/klbf-lab/actions/runs/45657892942/job/139359416688)
Author
Owner

@⁠PastiiJay on GitHub · 2026-11-25 17:02 UTC · original

Thanks a lot @⁠Vladimir-Human! Noted. I'll test it on the RX 9070 XT in the next test build and report back here.

**@⁠PastiiJay** on GitHub · 2026-11-25 17:02 UTC · [original](https://github.com/PastiiJay/klbf-lab/pull/16#issuecomment-7168914510) Thanks a lot @⁠Vladimir-Human! Noted. I'll test it on the RX 9070 XT in the next test build and report back here.
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/16/head:github-bot/gh-16'
git switch 'github-bot/gh-16'

Merge

Merge the changes and update on Forgejo.
git switch 'beta'
git merge --no-ff 'github-bot/gh-16'
git switch 'github-bot/gh-16'
git rebase 'beta'
git switch 'beta'
git merge --ff-only 'github-bot/gh-16'
git switch 'github-bot/gh-16'
git rebase 'beta'
git switch 'beta'
git merge --no-ff 'github-bot/gh-16'
git switch 'beta'
git merge --squash 'github-bot/gh-16'
git switch 'beta'
git merge --ff-only 'github-bot/gh-16'
git switch 'beta'
git merge 'github-bot/gh-16'
git push origin 'beta'
This discussion has been locked. Commenting is limited to contributors.
No description provided.