Skip to content

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

Open
RVZO6 wants to merge 1 commit into
Marvisak:mainfrom
RVZO6:rvzo/bindless-fast
Open

RVZO6 wants to merge 1 commit into
Marvisak:mainfrom
RVZO6:rvzo/bindless-fast

Conversation

@RVZO6

@RVZO6 RVZO6 commented Nov 26, 2026

Copy link
Copy Markdown

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

…ntical-rewrite fast-path

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)
@fuzeofficlal

Copy link
Copy Markdown
Contributor

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.

Copy link
Copy Markdown
Owner

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

@Dukestar69 Dukestar69 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

clquwu added a commit to clquwu/leonida-ultimate that referenced this pull request Nov 26, 2026
… sparse shadow/delta & spare buffer reuse

Eliminate redundant bindless table generations, descriptor writes, and linear scans in runtime/src/vulkan_bindless.h:
- Short-circuit identical descriptor rewrites in BindlessSet::queue() and cancel pre-flush no-op round-trips in O(1), avoiding phantom vkUpdateDescriptorSets calls and delta_log_/write_seq_ churn (inspired by Marvisak/VIctory#2940, GPL-3.0, RVZO6).
- Add compact open-addressed FlatHandleIndex reverse map (VkImageView/VkSampler -> packed (binding, index) slots with inline single-slot fast path and backward-shift deletion) powering O(slots_referencing_handle) clear_image()/forget_image_view() and clear_sampler()/forget_sampler().
- Track sparse written_slots_ per binding so write_shadow() avoids scanning 153k+ slots on partially_bound sets (and emits a single O(1) write when fully populated), coalesce contiguous runs in write_delta() with reused scratch buffers, and add O(1) lowest-free allocation hints and free counters.
- Opportunistically recycle completed retired generations before allocating new pools/buffers, perform idle in-place table rewrites when completed >= retire_ticket, retain up to 3 freshest spare sets + same-size DEVICE_LOCAL TableBuffers when pool budget allows (>=13 sets) to skip redundant vkCreateBuffer/vkAllocateMemory/vkCreateDescriptorPool/write_table_binding overhead.
- Expand bindless_sizing_test and d3d12_bindless_churn_test with CPU fast-path and GPU LEONIDA_VK_VALIDATION=1 steady-state coverage.
@MeMako

MeMako commented Nov 26, 2026

Copy link
Copy Markdown

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.

BastienRodz added a commit to BastienRodz/VIctory that referenced this pull request Nov 26, 2026
…verse index, FxHash maps, and identical-rewrite fast-path

@fuzeofficlal fuzeofficlal left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants