GPU/Bindless: O(1) texture forget reverse index, FxHash maps, and identical-rewrite fast-path #2940

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

@⁠RVZO6 on GitHub · 2026-11-26 00:57 UTC · original
RVZO6:rvzo/bindless-fast → main · +234 −30 · 1 files · 1 commits · open

Summary

Optimizes bindless heap slot/texture tracking in crates/victory-gpu/src/bindless.rs without changing observable semantics or lock-ordering safety:

  1. Reverse texture → slot index (FxMap<usize, TextureSlots>): replaces textures: BTreeMap<usize, u32> so bindless::forget() (called from every Resource::drop) removes only the slot(s) viewing the dropped texture in O(slots viewing texture) instead of scanning all ~130,000 slots with inner.slots.retain(...) under the viewed.inner mutex. Uses an inline TextureSlots::One(u32) variant for the common 1-slot case (zero extra heap allocation per texture).
  2. FxMap for slots, ranges, textures, hulls, ends: replaces ranges: BTreeMap (which is never iterated in sorted order; log() now counts distinct copies via sorted/deduped ends.keys()) with O(1) hashing instead of O(log N) tree traversals on every descriptor rewrite.
  3. Identical-target rewrite short-circuit in Viewed::set: returns early without mutating slots, ranges, ends, hulls, or textures when overwriting a slot with the identical Seen target (or clearing an already-empty slot).
  4. Single-heap fast-path in uploads(): skips allocating/hashing the cross-heap dedup HashSet when heaps.len() == 1. (viewed.inner remains held across made_copy/staging to preserve Resource liveness against concurrent Resource::drop -> bindless::forget().)

Zero overlap with #2487/#2489 (which only update test fixtures in rounds/heap_views_*), #2821, or #2848.

Measured (--release bench_bindless_forget_and_set, median of 3 runs, 130,000-slot heap on 63bcca4)

Operation Before (63bcca4) After Speedup
500 forget_hit (texture drops) 263.939 ms 0.092 ms 2,869× (-99.97%)
100k Viewed::set (50% identical, 50% changing) 26.155 ms 1.235 ms 21.2× (-95.3%)
10k forget_miss (non-bindless drops) 155.1 µs 89.0 µs 1.74× (-42.6%)

Verification

  • New unit test bindless::tests::texture_reverse_index_tracks_multi_slot_and_forget covers single-slot, multi-slot, identical overwrite, rebind, and forget_resource.
  • Verified green on windows-latest MSVC + WARP SM 6.6 CI (clippy default + --tests + --features diagnostics, xtask policy, full workspace tests including existing bindless WARP tests): https://github.com/RVZO6/VIctory/actions/runs/45730729035

Not run: the title; any hardware GPU.
AI-assisted: T3 Code (Antigravity, gemini-4-neon).

**@⁠RVZO6** on GitHub · 2026-11-26 00:57 UTC · [original](https://github.com/Marvisak/VIctory/pull/2940) `RVZO6:rvzo/bindless-fast` → `main` · +234 −30 · 1 files · 1 commits · open ### Summary Optimizes bindless heap slot/texture tracking in `crates/victory-gpu/src/bindless.rs` without changing observable semantics or lock-ordering safety: 1. **Reverse texture → slot index (`FxMap<usize, TextureSlots>`)**: replaces `textures: BTreeMap<usize, u32>` so `bindless::forget()` (called from every `Resource::drop`) removes only the slot(s) viewing the dropped texture in O(slots viewing texture) instead of scanning all ~130,000 slots with `inner.slots.retain(...)` under the `viewed.inner` mutex. Uses an inline `TextureSlots::One(u32)` variant for the common 1-slot case (zero extra heap allocation per texture). 2. **`FxMap` for `slots`, `ranges`, `textures`, `hulls`, `ends`**: replaces `ranges: BTreeMap` (which is never iterated in sorted order; `log()` now counts distinct copies via sorted/deduped `ends.keys()`) with O(1) hashing instead of O(log N) tree traversals on every descriptor rewrite. 3. **Identical-target rewrite short-circuit in `Viewed::set`**: returns early without mutating `slots`, `ranges`, `ends`, `hulls`, or `textures` when overwriting a slot with the identical `Seen` target (or clearing an already-empty slot). 4. **Single-heap fast-path in `uploads()`**: skips allocating/hashing the cross-heap dedup `HashSet` when `heaps.len() == 1`. (`viewed.inner` remains held across `made_copy`/`staging` to preserve `Resource` liveness against concurrent `Resource::drop -> bindless::forget()`.) Zero overlap with #2487/#2489 (which only update test fixtures in `rounds`/`heap_views_*`), #2821, or #2848. ### Measured (`--release bench_bindless_forget_and_set`, median of 3 runs, 130,000-slot heap on `63bcca4`) | Operation | Before (`63bcca4`) | After | Speedup | |---|---:|---:|---:| | 500 `forget_hit` (texture drops) | 263.939 ms | **0.092 ms** | **2,869× (-99.97%)** | | 100k `Viewed::set` (50% identical, 50% changing) | 26.155 ms | **1.235 ms** | **21.2× (-95.3%)** | | 10k `forget_miss` (non-bindless drops) | 155.1 µs | **89.0 µs** | **1.74× (-42.6%)** | ### Verification - New unit test `bindless::tests::texture_reverse_index_tracks_multi_slot_and_forget` covers single-slot, multi-slot, identical overwrite, rebind, and `forget_resource`. - Verified green on `windows-latest` MSVC + WARP SM 6.6 CI (clippy default + `--tests` + `--features diagnostics`, `xtask policy`, full workspace tests including existing bindless WARP tests): https://github.com/RVZO6/VIctory/actions/runs/45730729035 **Not run:** the title; any hardware GPU. AI-assisted: T3 Code (Antigravity, gemini-4-neon).
Optimize bindless heap tracking in crates/victory-gpu/src/bindless.rs:
- Replace BTreeMap<usize, u32> textures with FxMap<usize, TextureSlots>
  reverse index (inline single slot for the common 1-slot case) so
  bindless::forget() removes only the slots viewing that texture in
  O(slots viewing texture) instead of scanning all ~130,000 slots with
  slots.retain() under viewed.inner.
- Switch Inner::ranges from BTreeMap to FxMap (O(1) vs O(log N) per slot
  rewrite) and use FxMap on slots/textures/hulls/ends.
- Short-circuit Viewed::set when overwriting a slot with the identical Seen
  target (or None -> None) to avoid redundant map and hull mutations.
- Skip the cross-heap dedup HashSet in uploads() when there is only one heap.
- Keep viewed.inner held across made_copy/staging in uploads() to preserve
  liveness against concurrent Resource::drop -> bindless::forget().

Measured (--release bench_bindless_forget_and_set, median of 3 runs, 130,000 slots):
- 500 forget_hit:  263.939 ms -> 0.092 ms (2,869x, -99.97%)
- 100k set:         26.155 ms -> 1.235 ms (21.2x, -95.3%)
- 10k forget_miss: 155.1 us   -> 89.0 us  (1.74x, -42.6%)

Assisted-by: T3 Code (Antigravity, gemini-4-neon)
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 05:18:03 +00:00
Author
Owner

@⁠fuzeofficlal on GitHub · 2026-11-26 01:08 UTC · original

Code review and RTX 5090 + WARP + release benchmark verification at head 5bed3341d008 merged cleanly onto main (63bcca43c6a0):

  • Code review (crates/victory-gpu/src/bindless.rs):
    • TextureSlots (One(u32) / Many(Vec<u32>) with swap_remove back to One) gives forget_resource O(slots viewing texture) cleanup instead of scanning ~130k slots.
    • same_seen short-circuits identical rewrites in Inner::set_seen before mutating counts or pushing duplicate slots into TextureSlots::Many, while replacements strictly remove/decrement old state before inserting/incrementing new state.
    • Lock ordering (HEAPS.lock() clone -> drop -> per-heap inner lock) stays deadlock-free, and uploads skips HashSet allocation in the common single-heap case.
  • Verified (Windows 11, NVIDIA RTX 5090 + WARP, VICTORY_D3D12_DEBUG=1, Rust 1.98.1):
    • cargo xtask policy: 191 files ok; cargo clippy (default + --features diagnostics): 0 warnings
    • bindless:: draw::tests: 24/24 passed (1 ignored bench) on both RTX 5090 and WARP (including texture_reverse_index_tracks_multi_slot_and_forget)
    • --release bench_bindless_forget_and_set (130k slots): 500 forget_hit = 0.180 ms, 10k forget_miss = 62.6 us, 100k set = 1.332 ms.

Clean invariants and great speedup; LGTM.

**@⁠fuzeofficlal** on GitHub · 2026-11-26 01:08 UTC · [original](https://github.com/Marvisak/VIctory/pull/2940#issuecomment-7179470375) Code review and RTX 5090 + WARP + release benchmark verification at head `5bed3341d008` merged cleanly onto `main` (`63bcca43c6a0`): - **Code review (`crates/victory-gpu/src/bindless.rs`)**: - `TextureSlots` (`One(u32)` / `Many(Vec<u32>)` with `swap_remove` back to `One`) gives `forget_resource` O(slots viewing texture) cleanup instead of scanning ~130k slots. - `same_seen` short-circuits identical rewrites in `Inner::set_seen` before mutating counts or pushing duplicate slots into `TextureSlots::Many`, while replacements strictly remove/decrement old state before inserting/incrementing new state. - Lock ordering (`HEAPS.lock()` clone -> drop -> per-heap `inner` lock) stays deadlock-free, and `uploads` skips `HashSet` allocation in the common single-heap case. - **Verified (Windows 11, NVIDIA RTX 5090 + WARP, `VICTORY_D3D12_DEBUG=1`, Rust 1.98.1)**: - `cargo xtask policy`: `191 files ok`; `cargo clippy` (default + `--features diagnostics`): 0 warnings - `bindless:: draw::tests`: **24/24 passed** (1 ignored bench) on both RTX 5090 and WARP (including `texture_reverse_index_tracks_multi_slot_and_forget`) - `--release` `bench_bindless_forget_and_set` (130k slots): `500 forget_hit = 0.180 ms`, `10k forget_miss = 62.6 us`, `100k set = 1.332 ms`. Clean invariants and great speedup; LGTM.
Author
Owner

@⁠Marvisak on GitHub · 2026-11-26 04:27 UTC · original

Automated triage for the maintainers.

Merge candidate. A texture-to-slots reverse index (TextureSlots) replaces the counter in bindless::Inner, so forget() on every resource drop no longer runs slots.retain over the whole heap under the lock; the maps move to an Fx hasher, and unchanged rewrites are skipped (set_seen / same_seen). ranges is never iterated in order, TextureSlots::remove empties correctly, and forget_resource gives the same result as the old retain; the unit test checks real invariants. Small fixes: the Viewed doc comment is stale, and victory-gpu could share one Fx hasher rather than adding copies (see #2941, #2942).

CI: passing.


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-26 04:27 UTC · [original](https://github.com/Marvisak/VIctory/pull/2940#issuecomment-7182611540) <!-- victory-triage v1 head=5bed3341d008cc0871f6c20db8410cb8521cf9e7 verdict=merge-candidate at=2026-11-26T04:27:17Z --> Automated triage for the maintainers. **Merge candidate.** A texture-to-slots reverse index (`TextureSlots`) replaces the counter in `bindless::Inner`, so `forget()` on every resource drop no longer runs `slots.retain` over the whole heap under the lock; the maps move to an Fx hasher, and unchanged rewrites are skipped (`set_seen` / `same_seen`). `ranges` is never iterated in order, `TextureSlots::remove` empties correctly, and `forget_resource` gives the same result as the old `retain`; the unit test checks real invariants. Small fixes: the `Viewed` doc comment is stale, and `victory-gpu` could share one Fx hasher rather than adding copies (see #2941, #2942). CI: passing. --- _Generated by [Claude Code](https://claude.ai/code)_
Author
Owner

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

Tested merged onto origin/main (63bcca4) on Windows 11 x64 (NVIDIA GeForce RTX 5090):

  • cargo test -p victory-gpu --lib bindless::tests: 4 passed, 0 failed, 1 ignored (including new texture_reverse_index_tracks_multi_slot_and_forget).
  • cargo clippy -p victory-gpu --all-targets -- -D warnings: clean.
  • cargo xtask policy: 191 files ok.

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

**@⁠Dukestar69** on GitHub · 2026-11-26 04:48 UTC · [original](https://github.com/Marvisak/VIctory/pull/2940#pullrequestreview-6068694294) · **approved these changes** Tested merged onto `origin/main` (`63bcca4`) on Windows 11 x64 (NVIDIA GeForce RTX 5090): - `cargo test -p victory-gpu --lib bindless::tests`: **4 passed, 0 failed, 1 ignored** (including new `texture_reverse_index_tracks_multi_slot_and_forget`). - `cargo clippy -p victory-gpu --all-targets -- -D warnings`: clean. - `cargo xtask policy`: 191 files ok. Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
Author
Owner

@⁠MeMako on GitHub · 2026-11-26 07:50 UTC · original

Verified #2940 (5bed334) on native Windows 11 (10.0.26200) + AMD Radeon RX 9070 XT (RDNA 4, driver 32.0.32029.2010, Ryzen 9 7950X, 32 GB RAM).

  • cargo clippy -p victory-gpu -p victory --all-targets --locked -- -D warnings: clean; cargo test -p victory-gpu --lib --locked -- --test-threads=1: 274/274 passed (including texture_reverse_index_tracks_multi_slot_and_forget).
  • bench_bindless_forget_and_set (--release, 130k slots on Ryzen 9 7950X): 500 forget_hit = 0.123 ms, 10k forget_miss = 37.1 µs, 100k set = 0.595 ms.
  • Live title run (combined with #3052 + #3061 on local integration stack on main 63bcca4 + #2051 + #2472 + #2918 + #1010 + #2130 flags off + #3055 + #2914 + #2985 + #2849 + #3035 + #2958): 0 crashes, 0 visual regressions, noticeably faster world asset streaming (+14% draws resident in the first 26 heavy frames).

Hardware runs were performed by me on my 7950X / RX 9070 XT; comment drafted with Antigravity.

**@⁠MeMako** on GitHub · 2026-11-26 07:50 UTC · [original](https://github.com/Marvisak/VIctory/pull/2940#issuecomment-7185901604) Verified `#2940` (`5bed334`) on native Windows 11 (`10.0.26200`) + AMD Radeon RX 9070 XT (RDNA 4, driver `32.0.32029.2010`, Ryzen 9 7950X, 32 GB RAM). - `cargo clippy -p victory-gpu -p victory --all-targets --locked -- -D warnings`: clean; `cargo test -p victory-gpu --lib --locked -- --test-threads=1`: **274/274 passed** (including `texture_reverse_index_tracks_multi_slot_and_forget`). - `bench_bindless_forget_and_set` (`--release`, 130k slots on Ryzen 9 7950X): `500 forget_hit = 0.123 ms`, `10k forget_miss = 37.1 µs`, `100k set = 0.595 ms`. - Live title run (combined with `#3052` + `#3061` on local integration stack on `main` `63bcca4` + `#2051` + `#2472` + `#2918` + `#1010` + `#2130` flags off + `#3055` + `#2914` + `#2985` + `#2849` + `#3035` + `#2958`): 0 crashes, 0 visual regressions, noticeably faster world asset streaming (+14% draws resident in the first 26 heavy frames). *Hardware runs were performed by me on my 7950X / RX 9070 XT; comment drafted with Antigravity.*
Author
Owner

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

Reviewed and verified head 5bed3341d008cc0871f6c20db8410cb8521cf9e7 (clean merge with origin/main 63bcca43) on Windows 11 x64 (Ryzen 9 9950X, NVIDIA GeForce RTX 5090 + D3D12 WARP, VICTORY_D3D12_DEBUG=1, Rust 1.98.1).

Code review (crates/victory-gpu/src/bindless.rs)

  • Self-contained FxHasher / FxMap (L24-51) adds zero new dependencies and is appropriate for internal integer/pointer maps.
  • TextureSlots (L54-84) + Inner::set_seen (L211-227) + Inner::forget_resource (L229-242) preserve the exact bidirectional invariant between slots and textures (slot in textures[r] iff slots[slot] == Seen::Texture(r)): overwrites strictly remove/decrement the old entry before inserting the new one, TextureSlots::remove collapses Many back to One and deletes empty texture entries, and forget_resource removes the exact slot set that slots.retain previously scanned.
  • same_seen (L99-108) checks every field of every Seen variant (Arc::ptr_eq, address, len, uav for buffers; resource pointer for textures; full (kind, address, bytes) tuple for Left). Because HostHeap::mirror writes the updated D3D12 descriptor before calling Viewed::set, rewriting a slot with a different view descriptor (e.g., different swizzle/mip) of the same texture still updates the host descriptor while safely short-circuiting redundant Viewed churn and preventing duplicate slot insertion.
  • Single-heap uploads() fast path (L344-354) is sound because inner.textures keys are unique per heap, and log() (L510-513) derives the identical distinct-copy count from sorted/deduped inner.ends keys.
  • Non-blocking notes: the doc comment on Viewed (L114-115) still mentions slot counts rather than the reverse slot index; if #3213 lands alongside this, its is_live_texture(r) guard under Viewed::inner will need to sit inside Inner::set_seen.

Verification

  • cargo xtask policy: 191 files ok; cargo clippy --locked -p victory-gpu --all-targets -- -D warnings (default & --all-features): 0 warnings
  • bindless:: draw::tests: 24 passed, 0 failed, 1 ignored on RTX 5090 (default), RTX 5090 (--features diagnostics, VICTORY_D3D12_DEBUG=1), and WARP (VICTORY_D3D12_WARP=1, --features diagnostics, VICTORY_D3D12_DEBUG=1)
  • --release bench_bindless_forget_and_set (Ryzen 9 9950X, 130k slots, 3 runs, median): 500 forget_hit = 0.205 ms (0.068–0.219 ms), 10k forget_miss = 45.0 us (40.3–46.1 us), 100k set = 0.524 ms (0.517–0.598 ms)
  • Additional local invariant fuzz (1,500 random set_seen/forget_resource steps checking full map equivalence after every step, including buffer SRV↔UAV flips, Left address changes, and A→B rebinds followed by forget(A)) plus live heap SRV swizzle-overwrite/rebind/multi-heap uploads probe passed on both RTX 5090 and WARP with 0 D3D12 Debug Layer messages.

Solid optimization with clean invariants; approving.

**@⁠fuzeofficlal** on GitHub · 2026-11-26 13:51 UTC · [original](https://github.com/Marvisak/VIctory/pull/2940#pullrequestreview-6075271361) · **approved these changes** Reviewed and verified head `5bed3341d008cc0871f6c20db8410cb8521cf9e7` (clean merge with `origin/main` `63bcca43`) on Windows 11 x64 (Ryzen 9 9950X, **NVIDIA GeForce RTX 5090** + **D3D12 WARP**, `VICTORY_D3D12_DEBUG=1`, Rust 1.98.1). **Code review (`crates/victory-gpu/src/bindless.rs`)** - Self-contained `FxHasher` / `FxMap` (`L24-51`) adds zero new dependencies and is appropriate for internal integer/pointer maps. - `TextureSlots` (`L54-84`) + `Inner::set_seen` (`L211-227`) + `Inner::forget_resource` (`L229-242`) preserve the exact bidirectional invariant between `slots` and `textures` (`slot` in `textures[r]` iff `slots[slot] == Seen::Texture(r)`): overwrites strictly remove/decrement the old entry before inserting the new one, `TextureSlots::remove` collapses `Many` back to `One` and deletes empty texture entries, and `forget_resource` removes the exact slot set that `slots.retain` previously scanned. - `same_seen` (`L99-108`) checks every field of every `Seen` variant (`Arc::ptr_eq`, `address`, `len`, `uav` for buffers; resource pointer for textures; full `(kind, address, bytes)` tuple for `Left`). Because `HostHeap::mirror` writes the updated D3D12 descriptor before calling `Viewed::set`, rewriting a slot with a different view descriptor (e.g., different swizzle/mip) of the same texture still updates the host descriptor while safely short-circuiting redundant `Viewed` churn and preventing duplicate slot insertion. - Single-heap `uploads()` fast path (`L344-354`) is sound because `inner.textures` keys are unique per heap, and `log()` (`L510-513`) derives the identical distinct-copy count from sorted/deduped `inner.ends` keys. - Non-blocking notes: the doc comment on `Viewed` (`L114-115`) still mentions slot counts rather than the reverse slot index; if #3213 lands alongside this, its `is_live_texture(r)` guard under `Viewed::inner` will need to sit inside `Inner::set_seen`. **Verification** - `cargo xtask policy`: `191 files ok`; `cargo clippy --locked -p victory-gpu --all-targets -- -D warnings` (default & `--all-features`): 0 warnings - `bindless:: draw::tests`: **24 passed, 0 failed, 1 ignored** on RTX 5090 (default), RTX 5090 (`--features diagnostics`, `VICTORY_D3D12_DEBUG=1`), and WARP (`VICTORY_D3D12_WARP=1`, `--features diagnostics`, `VICTORY_D3D12_DEBUG=1`) - `--release` `bench_bindless_forget_and_set` (Ryzen 9 9950X, 130k slots, 3 runs, median): `500 forget_hit = 0.205 ms` (0.068–0.219 ms), `10k forget_miss = 45.0 us` (40.3–46.1 us), `100k set = 0.524 ms` (0.517–0.598 ms) - Additional local invariant fuzz (1,500 random `set_seen`/`forget_resource` steps checking full map equivalence after every step, including buffer SRV↔UAV flips, `Left` address changes, and A→B rebinds followed by `forget(A)`) plus live heap SRV swizzle-overwrite/rebind/multi-heap `uploads` probe passed on both RTX 5090 and WARP with 0 D3D12 Debug Layer messages. Solid optimization with clean invariants; approving.
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/2940/head:github-bot/gh-2940'
git switch 'github-bot/gh-2940'

Merge

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