gpu: replace O(N) bindless_plan scans with O(dirty) incremental tracking #199

Open
github-bot wants to merge 4 commits from github-bot/gh-199 into main AGit
Owner

@⁠Vladimir-Human on GitHub · 2026-11-25 21:26 UTC · original
Vladimir-Human:perf/incremental-bindless-plan → main · +2698 −290 · 4 files · 4 commits · open

Summary

AI-assisted performance PR eliminating the four full-table O(85,000) scans in Bindless::plan() (crates/victory-gpu/src/descriptor/host.rs) by introducing incremental per-slot change tracking (DirtyTracker / plan_dirty()), while preserving all recent origin/main (4f62c2a) behavior verbatim.

What changed

  • Incremental O(dirty) Bindless::plan(): Tracks dirty bindless slots across Shadow changes, Placed view bumps/evictions, Resource releases/destroys, and slot writes so unchanged frames and partial updates only inspect affected slots rather than walking all ~85k entries on the submit thread.
  • 4f62c2a preservation & integration:
    • Preserves VICTORY_LOD_CLAMP in largest_mip, placed_census in resource.rs, and the production write-only plan skip outside tests (cfg!(test) && self.writes_dirty).
    • Keeps write_slots in strict lockstep and preserves bit-exact output parity against full-table planning.
  • Kill-switch & tests:
    • VICTORY_PLAN_REUSE=0 reverts to full-scan planning for A/B validation and troubleshooting.
    • Added differential parity and eviction/ring-overflow tests (bindless_plan_differential_parity, bindless_plan_ring_overflow_fallbacks, pinning pre-eviction ID3D12Resource COM references across eviction).

Verification (RTX 4070 SUPER / i5-13400F, 3469dd5cf8)

  • cargo clippy -p victory-gpu --no-deps --all-targets --all-features --locked: 0 warnings in added/modified code.
  • VICTORY_D3D12_DEBUG=1 cargo test -p victory-gpu --locked --all-features -- --test-threads=1: 118 passed, 0 D3D12 debug errors/warnings (2 pre-existing unrelated failures on NVIDIA PORTABLE_VIEWS cap / watch::tests parallel run; watch::tests passes 1/1 standalone).
  • 10x consecutive VICTORY_D3D12_DEBUG=1 stress on bindless_plan_ring_overflow_fallbacks + bindless_plan_differential_parity: 10/10 passed.
  • VICTORY_D3D12_DEBUG=1 VICTORY_PLAN_REUSE=0 descriptor::host::tests::bindless: 6/6 passed, 0 D3D12 errors.
  • Release bench_bindless_plan_85k (85,000 slots, median of 12 iters, bit-exact parity verified):
    • No-op frame: 0.1 us -> 0.0 us
    • 4 changed slots: 2,731.4 us -> 79.9 us (34.2x)
    • 1 dead shadow (4 slots): 12,402.2 us -> 47.1 us (263.3x)
    • 1 bumped view (4 slots): 12,656.0 us -> 33.0 us (383.5x)
    • 1 evicted placed (4 slots): 14,533.2 us -> 28.5 us (509.9x)

Co-Authored-By: AI Assistant

**@⁠Vladimir-Human** on GitHub · 2026-11-25 21:26 UTC · [original](https://github.com/Belobug/VIctory-Belobug-AMD/pull/199) `Vladimir-Human:perf/incremental-bindless-plan` → `main` · +2698 −290 · 4 files · 4 commits · open ## Summary AI-assisted performance PR eliminating the four full-table `O(85,000)` scans in `Bindless::plan()` (`crates/victory-gpu/src/descriptor/host.rs`) by introducing incremental per-slot change tracking (`DirtyTracker` / `plan_dirty()`), while preserving all recent `origin/main` (`4f62c2a`) behavior verbatim. ### What changed - **Incremental `O(dirty)` `Bindless::plan()`**: Tracks dirty bindless slots across `Shadow` changes, `Placed` view bumps/evictions, `Resource` releases/destroys, and slot writes so unchanged frames and partial updates only inspect affected slots rather than walking all ~85k entries on the submit thread. - **`4f62c2a` preservation & integration**: - Preserves `VICTORY_LOD_CLAMP` in `largest_mip`, `placed_census` in `resource.rs`, and the production write-only plan skip outside tests (`cfg!(test) && self.writes_dirty`). - Keeps `write_slots` in strict lockstep and preserves bit-exact output parity against full-table planning. - **Kill-switch & tests**: - `VICTORY_PLAN_REUSE=0` reverts to full-scan planning for A/B validation and troubleshooting. - Added differential parity and eviction/ring-overflow tests (`bindless_plan_differential_parity`, `bindless_plan_ring_overflow_fallbacks`, pinning pre-eviction `ID3D12Resource` COM references across eviction). ### Verification (RTX 4070 SUPER / i5-13400F, `3469dd5cf8`) - `cargo clippy -p victory-gpu --no-deps --all-targets --all-features --locked`: 0 warnings in added/modified code. - `VICTORY_D3D12_DEBUG=1 cargo test -p victory-gpu --locked --all-features -- --test-threads=1`: **118 passed**, 0 D3D12 debug errors/warnings (2 pre-existing unrelated failures on NVIDIA `PORTABLE_VIEWS` cap / `watch::tests` parallel run; `watch::tests` passes 1/1 standalone). - 10x consecutive `VICTORY_D3D12_DEBUG=1` stress on `bindless_plan_ring_overflow_fallbacks` + `bindless_plan_differential_parity`: **10/10 passed**. - `VICTORY_D3D12_DEBUG=1 VICTORY_PLAN_REUSE=0` `descriptor::host::tests::bindless`: **6/6 passed**, 0 D3D12 errors. - Release `bench_bindless_plan_85k` (85,000 slots, median of 12 iters, bit-exact parity verified): - No-op frame: `0.1 us -> 0.0 us` - 4 changed slots: `2,731.4 us -> 79.9 us` (**34.2x**) - 1 dead shadow (4 slots): `12,402.2 us -> 47.1 us` (**263.3x**) - 1 bumped view (4 slots): `12,656.0 us -> 33.0 us` (**383.5x**) - 1 evicted placed (4 slots): `14,533.2 us -> 28.5 us` (**509.9x**) Co-Authored-By: AI Assistant
Replace all four O(N) full-heap scans in HostHeap::bindless_plan() with bounded event rings, reverse indices, and incremental BindlessPlan updates while preserving exact parity with VICTORY_PLAN_REUSE=0:

- Dead direct shadows (shadow.rs -> descriptor/host.rs): add bounded DEAD_RING (seq, shadow_ptr) and ReverseIndex<usize> by_shadow so remapped direct windows inspect only slots bound to dead shadows, falling back to full direct_slots scan on ring wrap or VICTORY_PLAN_REUSE=0.
- Released/bumped views (descriptor.rs -> descriptor/host.rs): record bumped (view_index, old_generation) keys in bounded BUMPED_VIEWS after Views::forget and index tracked slots in by_view so stale-view checks touch only slots naming bumped views, falling back to full slots scan on ring wrap or VICTORY_PLAN_REUSE=0.
- Evicted placed textures (resource.rs -> descriptor/host.rs): add bounded EVICTED_PLACED leaf-lock ring (updated on evict_placed_textures and TARGETS drops) and by_placed reverse index so evictions check only affected placed-texture slots without taking PLACED_TEXTURES/TARGETS on the fast path, preserving lock ordering and falling back to placed_live() on ring wrap or VICTORY_PLAN_REUSE=0.
- Incremental BindlessPlan rebuild (descriptor/host.rs): maintain write_slots, bounce_slots, AVL-backed ReadTracker (exact READ_GAP interval merging), and refcounted RetainedSet in lockstep with uses/slots so bindless_plan() rebuilds only dirty sub-lists in O(changed + active_writers).
- Add randomized ReadTracker parity tests, reverse-index sync tests, cross-heap differential parity tests, ring-overflow fallback tests, and an ignored release 85,000-slot benchmark (bench_bindless_plan_85k).
Resolve descriptor/host.rs cleanly preserving both 5d7bd16 (VICTORY_LOD_CLAMP in largest_mip) and 705a2d0 (skip write-only plan rebuilds outside tests via cfg!(test) gating on writes_dirty and lacks), while maintaining bit-exact parity with 4f62c2a full-scan reference.
Pin pre-eviction host ID3D12Resource COM handles in bindless_plan_differential_parity and bindless_plan_ring_overflow_fallbacks and assert on ID3D12Resource::as_raw() instead of freed-and-reallocated Box<Resource> pointers.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 03:37:48 +00:00
Author
Owner

@⁠Vladimir-Human on GitHub · 2026-11-25 23:43 UTC · original

Follow-up uncontested --release benchmark on idle i5-13400F / RTX 4070 SUPER (3469dd5cf, median of 12 iters):

  • 4 changed slots: 2,479.9 us -> 54.4 us (45.6x)
  • 1 dead shadow (4 slots): 9,500.5 us -> 42.2 us (225.1x)
  • 1 bumped view (4 slots): 11,369.5 us -> 30.1 us (377.7x)
  • 1 evicted placed (4 slots): 10,626.6 us -> 19.1 us (556.4x)
**@⁠Vladimir-Human** on GitHub · 2026-11-25 23:43 UTC · [original](https://github.com/Belobug/VIctory-Belobug-AMD/pull/199#issuecomment-7177962393) Follow-up uncontested `--release` benchmark on idle `i5-13400F` / `RTX 4070 SUPER` (`3469dd5cf`, median of 12 iters): - `4 changed slots`: `2,479.9 us -> 54.4 us` (**45.6x**) - `1 dead shadow (4 slots)`: `9,500.5 us -> 42.2 us` (**225.1x**) - `1 bumped view (4 slots)`: `11,369.5 us -> 30.1 us` (**377.7x**) - `1 evicted placed (4 slots)`: `10,626.6 us -> 19.1 us` (**556.4x**)
Author
Owner

@⁠adrianmjim on GitHub · 2026-11-26 19:47 UTC · original

Tested on native Windows 11 with an RX 7900 XT (hardware adapter, driver 32.0.32029.2010). Head 3469dd5, base 4f62c2a. Other jobs were running on the machine (CPU at 100%), so the timings are noisy.

Lib tests (cargo test -p victory-gpu --lib, debug, 15 interleaved runs each of base and PR):

  • No new failures. frame::tests::waits_end_on_ticks_and_count_frames fails on both (base 15/15, PR 13/15). The watch::tests flakes you mentioned happen on base too, in 4 of 15 runs on each side.
  • Each side also stopped once mid-run without a summary: base with exit 127, PR with an access violation. In the PR one, bindless_plan_differential_parity was among the tests that had not finished, along with the shadow/texture/watch tests. I did not root-cause either one.
  • The 4 new tests passed in every run that reached them. descriptor::host passes 16/16 with VICTORY_PLAN_REUSE=0.
  • Serial (--test-threads=1): base and PR both fail only watch::tests::written_pages_are_reported_to_every_asker.

Same-path comparison against base:
bench_bindless_plan_85k compares two code paths inside the PR. So I ported its scenario to a single heap through the default bindless_plan(), which builds on both base and PR. On base it also needs the PR's test-only test_register_direct and the direct() test lookup. Release, 5 interleaved runs, median of the per-run medians:

  • 4 changed slots: 4436 us -> 39 us
  • 1 dead shadow: 20077 us -> 22 us
  • 1 bumped view: 24673 us -> 15 us
  • 1 evicted placed: 31459 us -> 6207 us (the PR runs ranged from 1.2 to 43 ms)

Base and PR produce the same plans in all 10 runs: same read ranges, write ranges and bounces for all 108 plans. I left the retained-resource count out of that check because it varies from run to run on either build.

The evicted-placed number in the PR's own bench looks like an ordering effect:

  • Your bench gives here: 43109 -> 42.5 us (median of 5). That is about 1000x, against the 510x in the description.
  • In that loop the full-scan heap is planned first. From the code, the placed-texture map is global, so that plan re-creates the evicted texture and the incremental heap then finds it already made.
  • With only the order swapped in that loop (incremental heap first), I get 2.7 to 6.6 ms incremental vs 16.8 to 22.8 ms full over 3 runs. That is 3.4x to 6.2x.
  • I did not swap the other four cases. Their single-heap results above are in line with your bench.

Also note that both benches are test builds, and there base takes cfg!(test) branches. It always walks the direct slots (host.rs:1791 on base) and always remakes the plan (:1989). So the base cost in a release game build may be lower than these numbers.

Not tested: the D3D12 debug layer (no diagnostics build), clippy, WARP, and any in-game timing.

**@⁠adrianmjim** on GitHub · 2026-11-26 19:47 UTC · [original](https://github.com/Belobug/VIctory-Belobug-AMD/pull/199#issuecomment-7201959913) Tested on native Windows 11 with an RX 7900 XT (hardware adapter, driver 32.0.32029.2010). Head 3469dd5, base 4f62c2a. Other jobs were running on the machine (CPU at 100%), so the timings are noisy. Lib tests (`cargo test -p victory-gpu --lib`, debug, 15 interleaved runs each of base and PR): - No new failures. `frame::tests::waits_end_on_ticks_and_count_frames` fails on both (base 15/15, PR 13/15). The `watch::tests` flakes you mentioned happen on base too, in 4 of 15 runs on each side. - Each side also stopped once mid-run without a summary: base with exit 127, PR with an access violation. In the PR one, `bindless_plan_differential_parity` was among the tests that had not finished, along with the shadow/texture/watch tests. I did not root-cause either one. - The 4 new tests passed in every run that reached them. `descriptor::host` passes 16/16 with `VICTORY_PLAN_REUSE=0`. - Serial (`--test-threads=1`): base and PR both fail only `watch::tests::written_pages_are_reported_to_every_asker`. Same-path comparison against base: `bench_bindless_plan_85k` compares two code paths inside the PR. So I ported its scenario to a single heap through the default `bindless_plan()`, which builds on both base and PR. On base it also needs the PR's test-only `test_register_direct` and the `direct()` test lookup. Release, 5 interleaved runs, median of the per-run medians: - 4 changed slots: 4436 us -> 39 us - 1 dead shadow: 20077 us -> 22 us - 1 bumped view: 24673 us -> 15 us - 1 evicted placed: 31459 us -> 6207 us (the PR runs ranged from 1.2 to 43 ms) Base and PR produce the same plans in all 10 runs: same read ranges, write ranges and bounces for all 108 plans. I left the retained-resource count out of that check because it varies from run to run on either build. The evicted-placed number in the PR's own bench looks like an ordering effect: - Your bench gives here: 43109 -> 42.5 us (median of 5). That is about 1000x, against the 510x in the description. - In that loop the full-scan heap is planned first. From the code, the placed-texture map is global, so that plan re-creates the evicted texture and the incremental heap then finds it already made. - With only the order swapped in that loop (incremental heap first), I get 2.7 to 6.6 ms incremental vs 16.8 to 22.8 ms full over 3 runs. That is 3.4x to 6.2x. - I did not swap the other four cases. Their single-heap results above are in line with your bench. Also note that both benches are test builds, and there base takes `cfg!(test)` branches. It always walks the direct slots (host.rs:1791 on base) and always remakes the plan (:1989). So the base cost in a release game build may be lower than these numbers. Not tested: the D3D12 debug layer (no diagnostics build), clippy, WARP, and any in-game timing.
Author
Owner

@⁠Vladimir-Human on GitHub · 2026-11-27 07:04 UTC · original

Thanks @⁠adrianmjim for testing on RX 7900 XT (108/108 identical plans against base, single-heap comparison, and catching the ordering bug in bench_bindless_plan_85k's evicted-placed sub-case).

  • Fixed bench_bindless_plan_85k evicted-placed ordering (f470ac0): Case 5 now runs pass_frame() + title_memory_remapped() fresh before each side, alternates full-first vs incremental-first on k % 2, and asserts pairwise-distinct live COM resource pointers (host_before, host_full, host_inc) so neither path can reuse the other's re-created texture in the global placed-texture map.
  • Updated clean --release bench_bindless_plan_85k numbers (f470ac0, i5-13400F / RTX 4070 SUPER, 3 clean runs, 85,000 slots, median of 12 iters per run):
    • 4 changed slots: 2,897–3,250 us -> 79–104 us (31–41x)
    • 1 dead shadow (4 slots): 11,747–15,346 us -> 49–64 us (239–265x)
    • 1 bumped view (4 slots): 12,572–17,289 us -> 31–47 us (371–419x)
    • 1 evicted placed (4 slots): 12,684–18,126 us -> 702–1,400 us (13.0x–18.1x, median 15.6x — replacing the earlier ordering-affected 556x / 19.1 us figure now that both sides independently pay cold D3D12 placed-texture re-creation).
  • Test-build cfg!(test) caveat: Agreed with your note — both bench_bindless_plan_85k and the single-heap port run under cfg!(test), where the full-scan baseline always remakes the plan and takes test-only branches, so the real release game build baseline cost on unchanged/partially-changed frames can be lower than these test-build numbers.
  • Re-verified on f470ac0: cargo clippy -p victory-gpu --all-targets (0 new warnings in touched code), bench_bindless_plan_85k --release (3/3 clean runs passing), and descriptor::host tests in both default incremental and VICTORY_PLAN_REUSE=0 modes (all PR + bindless/eviction tests pass; only untouched upstream dynamic_slots_are_held_until_released hits NVIDIA's 1M shader-visible descriptor cap on RTX 4070 SUPER).
**@⁠Vladimir-Human** on GitHub · 2026-11-27 07:04 UTC · [original](https://github.com/Belobug/VIctory-Belobug-AMD/pull/199#issuecomment-7212120084) Thanks @⁠adrianmjim for testing on RX 7900 XT (108/108 identical plans against base, single-heap comparison, and catching the ordering bug in `bench_bindless_plan_85k`'s evicted-placed sub-case). - **Fixed `bench_bindless_plan_85k` evicted-placed ordering (`f470ac0`):** Case 5 now runs `pass_frame()` + `title_memory_remapped()` fresh before **each** side, alternates full-first vs incremental-first on `k % 2`, and asserts pairwise-distinct live COM resource pointers (`host_before`, `host_full`, `host_inc`) so neither path can reuse the other's re-created texture in the global placed-texture map. - **Updated clean `--release` `bench_bindless_plan_85k` numbers (`f470ac0`, `i5-13400F` / `RTX 4070 SUPER`, 3 clean runs, 85,000 slots, median of 12 iters per run):** - `4 changed slots`: `2,897–3,250 us -> 79–104 us` (**31–41x**) - `1 dead shadow (4 slots)`: `11,747–15,346 us -> 49–64 us` (**239–265x**) - `1 bumped view (4 slots)`: `12,572–17,289 us -> 31–47 us` (**371–419x**) - `1 evicted placed (4 slots)`: `12,684–18,126 us -> 702–1,400 us` (**13.0x–18.1x**, median **15.6x** — replacing the earlier ordering-affected `556x` / `19.1 us` figure now that both sides independently pay cold D3D12 placed-texture re-creation). - **Test-build `cfg!(test)` caveat:** Agreed with your note — both `bench_bindless_plan_85k` and the single-heap port run under `cfg!(test)`, where the full-scan baseline always remakes the plan and takes test-only branches, so the real release game build baseline cost on unchanged/partially-changed frames can be lower than these test-build numbers. - **Re-verified on `f470ac0`:** `cargo clippy -p victory-gpu --all-targets` (**0 new warnings** in touched code), `bench_bindless_plan_85k --release` (**3/3 clean runs passing**), and `descriptor::host` tests in both default incremental and `VICTORY_PLAN_REUSE=0` modes (all PR + bindless/eviction tests pass; only untouched upstream `dynamic_slots_are_held_until_released` hits NVIDIA's 1M shader-visible descriptor cap on RTX 4070 SUPER).
Author
Owner

@⁠adrianmjim on GitHub · 2026-11-27 07:06 UTC · original

Thanks @⁠Vladimir-Human. Re-creating before each side, alternating the order, and asserting distinct resource pointers is what that sub-case needed. The 13–18x figure is now a like-for-like cold re-creation on both sides, rather than one side reusing the other's texture. Nothing further from me.

**@⁠adrianmjim** on GitHub · 2026-11-27 07:06 UTC · [original](https://github.com/Belobug/VIctory-Belobug-AMD/pull/199#issuecomment-7212147765) Thanks @⁠Vladimir-Human. Re-creating before each side, alternating the order, and asserting distinct resource pointers is what that sub-case needed. The 13–18x figure is now a like-for-like cold re-creation on both sides, rather than one side reusing the other's texture. Nothing further from me.
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/199/head:github-bot/gh-199'
git switch 'github-bot/gh-199'

Merge

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