GPU: track dynamic descriptor slots in a 64-bit word bitmap #3218

Open
github-bot wants to merge 1 commit from github-bot/gh-3218 into main AGit
Owner

@⁠danielibabet on GitHub · 2026-11-26 13:33 UTC · original
danielibabet:pr/dynamic-slot-bitmap → main · +425 −34 · 1 files · 1 commits · open

Summary

Replace the Vec<bool> dynamic slot allocator in HostHeap with a dense 64-bit word bitmap (DynamicSlots). This is a performance fix for NVIDIA GPUs where the host heap is capped at 1,000,000 shader-visible descriptors, forcing MIN_DYNAMIC = 65,536 slots to absorb all descriptor tables above slot 934,464 and all root-view runs.

Problem

The previous allocator stored one bool per slot (up to 262,144 bytes / 256 KiB for a 256K heap) and scanned byte-by-byte under the heap Mutex. When the dynamic range fragmented or saturated — which happens every frame on NVIDIA with ~4,700 shadow copies requesting pair() → dynamic(2) — a single frame could scan hundreds of thousands of bytes while holding the lock, stalling all render threads.

Solution

DynamicSlots packs slot state into Vec<u64> (8× less memory: 8 KiB for the NVIDIA 64K range, fits in L1D):

  • SWAR scan without branches: skips full words (u64::MAX) or empty words (0) in 1–2 instructions; resolves cross-word spans with trailing_zeros / leading_zeros; locates internal gaps of 1–63 slots in ≤6 bit-shift operations.
  • O(1) early-rejection: min_failed tracks the smallest n that did not fit after a full scan. Requests of size ≥ min_failed are rejected in nanoseconds until a release clears at least one bit (zero false negatives, no heuristics, no frame-counter coupling).

Benchmark results (cargo test --release, i9-14900KF)

Scenario Before (Vec<bool>) After (DynamicSlots) Speedup
NVIDIA 64K — rotation, 3 batches in flight, fragmented pairs 0.275 ms/frame 0.006 ms/frame 43.7×
NVIDIA 64K — saturated frame (2,500 table/root-view requests) 100.390 ms 0.0011 ms 91,264×
256K heap — rotation, 3 batches in flight, fragmented pairs 3.801 ms/frame 0.024 ms/frame 158.7×
256K heap — saturated frame (2,500 requests) 404.578 ms 0.0011 ms 367,798×

Tests

  • Differential equivalence against the reference allocator across 30,000 random operations and word-boundary edge cases: OK
  • descriptor::host::tests on WARP: 17/17 passed
  • cargo clippy --workspace --all-targets --all-features --locked -- -D warnings: 0 warnings
  • cargo run -p xtask -- policy: 191 files OK
**@⁠danielibabet** on GitHub · 2026-11-26 13:33 UTC · [original](https://github.com/Marvisak/VIctory/pull/3218) `danielibabet:pr/dynamic-slot-bitmap` → `main` · +425 −34 · 1 files · 1 commits · open ## Summary Replace the `Vec<bool>` dynamic slot allocator in `HostHeap` with a dense 64-bit word bitmap (`DynamicSlots`). This is a performance fix for NVIDIA GPUs where the host heap is capped at 1,000,000 shader-visible descriptors, forcing `MIN_DYNAMIC` = 65,536 slots to absorb all descriptor tables above slot 934,464 and all root-view runs. ## Problem The previous allocator stored one `bool` per slot (up to 262,144 bytes / 256 KiB for a 256K heap) and scanned byte-by-byte under the heap `Mutex`. When the dynamic range fragmented or saturated — which happens every frame on NVIDIA with ~4,700 shadow copies requesting `pair() → dynamic(2)` — a single frame could scan hundreds of thousands of bytes while holding the lock, stalling all render threads. ## Solution `DynamicSlots` packs slot state into `Vec<u64>` (8× less memory: 8 KiB for the NVIDIA 64K range, fits in L1D): - **SWAR scan without branches**: skips full words (`u64::MAX`) or empty words (`0`) in 1–2 instructions; resolves cross-word spans with `trailing_zeros` / `leading_zeros`; locates internal gaps of 1–63 slots in ≤6 bit-shift operations. - **O(1) early-rejection**: `min_failed` tracks the smallest `n` that did not fit after a full scan. Requests of size ≥ `min_failed` are rejected in nanoseconds until a `release` clears at least one bit (zero false negatives, no heuristics, no frame-counter coupling). ## Benchmark results (cargo test --release, i9-14900KF) | Scenario | Before (Vec\<bool\>) | After (DynamicSlots) | Speedup | |---|---|---|---| | NVIDIA 64K — rotation, 3 batches in flight, fragmented pairs | 0.275 ms/frame | 0.006 ms/frame | **43.7×** | | NVIDIA 64K — saturated frame (2,500 table/root-view requests) | 100.390 ms | 0.0011 ms | **91,264×** | | 256K heap — rotation, 3 batches in flight, fragmented pairs | 3.801 ms/frame | 0.024 ms/frame | **158.7×** | | 256K heap — saturated frame (2,500 requests) | 404.578 ms | 0.0011 ms | **367,798×** | ## Tests - Differential equivalence against the reference allocator across 30,000 random operations and word-boundary edge cases: OK - `descriptor::host::tests` on WARP: 17/17 passed - `cargo clippy --workspace --all-targets --all-features --locked -- -D warnings`: 0 warnings - `cargo run -p xtask -- policy`: 191 files OK
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 13:35:53 +00:00
Author
Owner

@⁠Dukestar69 on GitHub · 2026-11-26 13:52 UTC · original · approved these changes

Verified on RTX 5090 against origin/main (63bcca4):

  • cargo test -p victory-gpu --lib descriptor::host:: -- --test-threads=1 --skip dynamic_slots_are_held_until_released: 16/16 passed (1 ignored bench; including dynamic_slot_bitmap_handles_word_boundaries_and_failed_watermark and dynamic_slot_bitmap_matches_reference_allocator).
  • cargo clippy -p victory-gpu --all-targets -- -D warnings: clean.
  • cargo xtask policy: 191 files ok.

Cleanly accelerates dynamic descriptor slot allocation using a 64-bit word bitmap.

Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).

**@⁠Dukestar69** on GitHub · 2026-11-26 13:52 UTC · [original](https://github.com/Marvisak/VIctory/pull/3218#pullrequestreview-6075285845) · **approved these changes** Verified on RTX 5090 against `origin/main` (`63bcca4`): - `cargo test -p victory-gpu --lib descriptor::host:: -- --test-threads=1 --skip dynamic_slots_are_held_until_released`: 16/16 passed (1 ignored bench; including `dynamic_slot_bitmap_handles_word_boundaries_and_failed_watermark` and `dynamic_slot_bitmap_matches_reference_allocator`). - `cargo clippy -p victory-gpu --all-targets -- -D warnings`: clean. - `cargo xtask policy`: 191 files ok. Cleanly accelerates dynamic descriptor slot allocation using a 64-bit word bitmap. Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
Author
Owner

@⁠rezioliver-collab on GitHub · 2026-11-26 18:01 UTC · original

I measured how much of the saturated-heap speedup survives when releases repeatedly invalidate the cached failed request size. With 65,535 slots on Apple M1, even reopening two slots on every iteration reduced the time for the 256-iteration trace from 42.61 ms to 0.324 ms.

Reopen two slots Base, ms/trace PR, ms/trace
Every iteration 42.61 0.324
Every 8th iteration 20.25 0.0426
Once per trace 17.13 0.00313

The heap started full. Each reopening tried a three-slot allocation, then reclaimed the two-slot hole. Holes alternated between slots 63–64 and the heap tail. Each iteration also requested 3–6 slots from the full heap. Every returned index and failed request matched between 63bcca43 and 249ba2a6, including the 65- and 4,095-slot controls.

I compiled the allocator and release methods with their mutex into a single-threaded native CPU harness (Rust 1.98.1, opt-level=3, macOS 14.7). Times are medians from 12 alternating pairs and measure allocator work only.

**@⁠rezioliver-collab** on GitHub · 2026-11-26 18:01 UTC · [original](https://github.com/Marvisak/VIctory/pull/3218#issuecomment-7199975647) I measured how much of the saturated-heap speedup survives when releases repeatedly invalidate the cached failed request size. With 65,535 slots on Apple M1, even reopening two slots on every iteration reduced the time for the 256-iteration trace from **42.61 ms to 0.324 ms**. | Reopen two slots | Base, ms/trace | PR, ms/trace | |---|---:|---:| | Every iteration | 42.61 | 0.324 | | Every 8th iteration | 20.25 | 0.0426 | | Once per trace | 17.13 | 0.00313 | The heap started full. Each reopening tried a three-slot allocation, then reclaimed the two-slot hole. Holes alternated between slots 63–64 and the heap tail. Each iteration also requested 3–6 slots from the full heap. Every returned index and failed request matched between `63bcca43` and `249ba2a6`, including the 65- and 4,095-slot controls. I compiled the allocator and release methods with their mutex into a single-threaded native CPU harness (Rust 1.98.1, `opt-level=3`, macOS 14.7). Times are medians from 12 alternating pairs and measure allocator work only.
Author
Owner

@⁠kalberts0089-hash on GitHub · 2026-11-27 08:15 UTC · original

NVIDIA test report: RTX 4070 Ti (desktop, driver 621.01), Windows 11 Pro, rustc 1.98.1. Head 249ba2a (base main 63bcca4).

  • cargo test -p victory-gpu --lib -- --test-threads=1: 231 passed, 2 failed, 1 ignored (main: 229 / 2 / 1). Both new tests pass (dynamic_slot_bitmap_handles_word_boundaries_and_failed_watermark, dynamic_slot_bitmap_matches_reference_allocator), and the bench is ignored. The 2 failures are the known capped-heap ones (dynamic_slots_are_held_until_released, lists_share_a_table_and_copies_keep_their_views) and fail the same way on main.
  • dynamic_slots_are_held_until_released is the existing test that takes and releases dynamic slots on a real heap, but on a capped heap it fails at its first assertion (>= HEAP_VIEWS) before reaching the allocator. With #3191 merged on top locally (clean, not committed), it passes, and all 17 descriptor::host tests pass (1 ignored). So the bitmap allocator holds up on a capped 1M heap.
  • cargo test -p victory-shader --lib --no-fail-fast, skipping heap_direct_texture_reads_mark_their_heap_index (#2571): 298 passed, 3 failed, 3 ignored, same as main. The 3 are the known NVIDIA failures (a_heap_buffer_read_by_element_takes_the_views_stride, a_heap_buffer_read_where_no_buffer_view_is_reads_zeros, gathers_at_a_level_match_the_hardware), unrelated.
  • Not tested: allocation timing on a saturated heap in a title (needs the game).

Synthetic tests only, no game files. AI-assisted (Claude Code).

**@⁠kalberts0089-hash** on GitHub · 2026-11-27 08:15 UTC · [original](https://github.com/Marvisak/VIctory/pull/3218#issuecomment-7213324100) NVIDIA test report: RTX 4070 Ti (desktop, driver 621.01), Windows 11 Pro, rustc 1.98.1. Head `249ba2a` (base main `63bcca4`). - `cargo test -p victory-gpu --lib -- --test-threads=1`: 231 passed, 2 failed, 1 ignored (main: 229 / 2 / 1). Both new tests pass (`dynamic_slot_bitmap_handles_word_boundaries_and_failed_watermark`, `dynamic_slot_bitmap_matches_reference_allocator`), and the bench is ignored. The 2 failures are the known capped-heap ones (`dynamic_slots_are_held_until_released`, `lists_share_a_table_and_copies_keep_their_views`) and fail the same way on main. - `dynamic_slots_are_held_until_released` is the existing test that takes and releases dynamic slots on a real heap, but on a capped heap it fails at its first assertion (`>= HEAP_VIEWS`) before reaching the allocator. With #3191 merged on top locally (clean, not committed), it passes, and all 17 `descriptor::host` tests pass (1 ignored). So the bitmap allocator holds up on a capped 1M heap. - `cargo test -p victory-shader --lib --no-fail-fast`, skipping `heap_direct_texture_reads_mark_their_heap_index` (#2571): 298 passed, 3 failed, 3 ignored, same as main. The 3 are the known NVIDIA failures (`a_heap_buffer_read_by_element_takes_the_views_stride`, `a_heap_buffer_read_where_no_buffer_view_is_reads_zeros`, `gathers_at_a_level_match_the_hardware`), unrelated. - Not tested: allocation timing on a saturated heap in a title (needs the game). Synthetic tests only, no game files. AI-assisted (Claude Code).
Author
Owner

@⁠KainanP on GitHub · 2026-11-27 12:02 UTC · original

List-call numbers for this PR (head 249ba2a), ns per draw:

main main + #3218 stack stack + #3218
1 thread 2506 / 2557 2494 / 2534 1110 / 1114 1110 / 1130
5 threads 8959 / 9047 8374 / 8938 1488 / 1460 1455 / 1499

Method. pipeline::list_bench from #2586 (fork branch perf/list-call-bench) with VICTORY_LIST_BENCH_SCALING=1: the draw sequence (root CBV, 4 root constants, index buffer, indexed draw that reads 6 textures through a table and 3 root CBVs). Each thread records its own list, best of 5 × 20,000 draws. Each row is the median of 7 interleaved runs, and there were two separate batches of 7 in opposite config order (shown as batch 1 / batch 2). Release --lib test build, RTX 4060 Laptop, Windows 11. Nothing else heavy was running (an editor and a browser, CPU 7–41 % between rounds, no game). The 5-thread row on main is noisy (single runs from 7.4 to 12.8 µs), so deltas under ~5 % there are not resolvable.

Bases. main = 63bcca4 + bench commits. Stack = main + #1978 + #2821 + #3258 + #3274 + #3275 + rebased #1356 + #3405 (the stack in #2586).

Reading it. Neutral on the steady-state draw path. Batch 1's −6.5 % at 5 threads on main did not repeat in batch 2 (−1.2 %). In this bench the dynamic range stays mostly empty, and draws take root-view runs only every few hundred draws, so the scan this PR replaces is short here. These numbers say nothing about the fragmented or saturated heap your microbench targets, which this bench doesn't create.

Correctness. cargo test -p victory-gpu --lib (release) on the RTX: 231 passed, 2 failed, 3 ignored. The failures are the #1744 heap-cap pair, also failing on main here. Both new tests pass, including the differential one against the Vec<bool> allocator. On the stack: 239 passed, 3 failed, the same 3 as the stack without this PR (the stack itself fails 3: the #1744 pair, plus a_list_lends_a_read_copy_only_while_it_is_the_textures in the stack's port of #3405. That test passes on #3405's own head, 232/2 here). I read min_failed: it rejects only sizes at or above one that failed a full scan (cursor..end plus 0..cursor+n-1, so the whole range, since runs can't wrap). Only a release that clears a set bit resets it, and take only sets bits, so the early rejection can't refuse a run that exists.

Not measured. A heap with ~4,700 shadow pairs taken (the NVIDIA case the PR is about), and any game frame.

AI use: Claude Code (Claude Opus 5.5).

**@⁠KainanP** on GitHub · 2026-11-27 12:02 UTC · [original](https://github.com/Marvisak/VIctory/pull/3218#issuecomment-7217763965) List-call numbers for this PR (head `249ba2a`), ns per draw: | | main | main + #3218 | stack | stack + #3218 | |---|---|---|---|---| | 1 thread | 2506 / 2557 | 2494 / 2534 | 1110 / 1114 | 1110 / 1130 | | 5 threads | 8959 / 9047 | 8374 / 8938 | 1488 / 1460 | 1455 / 1499 | **Method.** `pipeline::list_bench` from #2586 (fork branch `perf/list-call-bench`) with `VICTORY_LIST_BENCH_SCALING=1`: the draw sequence (root CBV, 4 root constants, index buffer, indexed draw that reads 6 textures through a table and 3 root CBVs). Each thread records its own list, best of 5 × 20,000 draws. Each row is the median of 7 interleaved runs, and there were two separate batches of 7 in opposite config order (shown as batch 1 / batch 2). Release `--lib` test build, RTX 4060 Laptop, Windows 11. Nothing else heavy was running (an editor and a browser, CPU 7–41 % between rounds, no game). The 5-thread row on `main` is noisy (single runs from 7.4 to 12.8 µs), so deltas under ~5 % there are not resolvable. **Bases.** `main` = `63bcca4` + bench commits. Stack = `main` + #1978 + #2821 + #3258 + #3274 + #3275 + rebased #1356 + #3405 (the stack in #2586). **Reading it.** Neutral on the steady-state draw path. Batch 1's −6.5 % at 5 threads on `main` did not repeat in batch 2 (−1.2 %). In this bench the dynamic range stays mostly empty, and draws take root-view runs only every few hundred draws, so the scan this PR replaces is short here. These numbers say nothing about the fragmented or saturated heap your microbench targets, which this bench doesn't create. **Correctness.** `cargo test -p victory-gpu --lib` (release) on the RTX: 231 passed, 2 failed, 3 ignored. The failures are the #1744 heap-cap pair, also failing on `main` here. Both new tests pass, including the differential one against the `Vec<bool>` allocator. On the stack: 239 passed, 3 failed, the same 3 as the stack without this PR (the stack itself fails 3: the #1744 pair, plus `a_list_lends_a_read_copy_only_while_it_is_the_textures` in the stack's port of #3405. That test passes on #3405's own head, 232/2 here). I read `min_failed`: it rejects only sizes at or above one that failed a full scan (`cursor..end` plus `0..cursor+n-1`, so the whole range, since runs can't wrap). Only a `release` that clears a set bit resets it, and `take` only sets bits, so the early rejection can't refuse a run that exists. **Not measured.** A heap with ~4,700 shadow pairs taken (the NVIDIA case the PR is about), and any game frame. _AI use: Claude Code (Claude Opus 5.5)._
This pull request can be merged automatically.
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/3218/head:github-bot/gh-3218'
git switch 'github-bot/gh-3218'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-3218'
git switch 'github-bot/gh-3218'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-3218'
git switch 'github-bot/gh-3218'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-3218'
git switch 'main'
git merge --squash 'github-bot/gh-3218'
git switch 'main'
git merge --ff-only 'github-bot/gh-3218'
git switch 'main'
git merge 'github-bot/gh-3218'
git push origin 'main'
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!3218
No description provided.