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

Open
RVZO6 wants to merge 1 commit from upstream-pr/2940 into main
RVZO6 commented 2026-11-26 00:57:01 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by RVZO6 on 2026-11-26T00:57:01Z.


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).

<!-- greyfinch-sync:Marvisak/VIctory:issue:6617025127 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2940). Originally posted by [RVZO6](https://github.com/RVZO6) on 2026-11-26T00:57:01Z. --- ### 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).
fuzeofficlal commented 2026-11-26 02:06:53 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-26T01:08:17Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7179470375 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2940#issuecomment-7179470375). Originally posted by [fuzeofficlal](https://github.com/fuzeofficlal) on 2026-11-26T01:08:17Z. --- 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.
Dukestar69 commented 2026-11-26 05:03:38 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Dukestar69 on 2026-11-26T04:48:49Z.

Review: APPROVED


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).

<!-- greyfinch-sync:Marvisak/VIctory:review:6068694294 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2940#pullrequestreview-6068694294). Originally posted by [Dukestar69](https://github.com/Dukestar69) on 2026-11-26T04:48:49Z. **Review:** APPROVED --- 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).
Marvisak commented 2026-11-26 05:04:10 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-26T04:27:26Z.


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

<!-- greyfinch-sync:Marvisak/VIctory:comment:7182611540 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2940#issuecomment-7182611540). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-26T04:27:26Z. --- <!-- 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)_
MeMako commented 2026-11-26 08:03:24 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by MeMako on 2026-11-26T07:50:56Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7185901604 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2940#issuecomment-7185901604). Originally posted by [MeMako](https://github.com/MeMako) on 2026-11-26T07:50:56Z. --- 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.*
fuzeofficlal commented 2026-11-26 14:01:27 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-26T13:51:05Z.

Review: APPROVED


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.

<!-- greyfinch-sync:Marvisak/VIctory:review:6075271361 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2940#pullrequestreview-6075271361). Originally posted by [fuzeofficlal](https://github.com/fuzeofficlal) on 2026-11-26T13:51:05Z. **Review:** APPROVED --- 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 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/2940:upstream-pr/2940'
git switch 'upstream-pr/2940'

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