GPU: index TextureQueue by due frame (VICTORY_DUE_INDEX) #1497

Manually merged
github merged 1 commit from github-bot/gh-1497 into main 2026-11-29 08:21:24 +00:00 AGit
Owner

@⁠Czer0xx on GitHub · 2026-11-28 20:49 UTC · original
Czer0xx:pr/due-index → main · +559 −38 · 2 files · 1 commits · merged 2026-11-29 08:16 UTC

Motivation

After #797 (28f87ef) packed TextureQueue into a dense 32-byte Vec<Entry>, collect_due_and_census, census, and all_due still linearly scan every queued entry under HostHeap::textures's mutex on every pass. During gameplay the queue holds ~25,000-33,000 entries (~800-1,060 KiB), even when only a fraction has due <= frame.

On RTX 3060 12 GB (1440p release, warm cache, post-#797 main), held taking the slots due still averaged 72.8 ms / 5 s (median 75 ms, max 100 ms), blocking other threads for 621.2 ms / 5 s (max 942 ms).

Changes

All new behavior is opt-in behind VICTORY_DUE_INDEX=1 (off by default, read once). With it unset, TextureQueue and heap_textures behave identically to main.

  • crates/victory-gpu/src/descriptor/host.rs:
    • Add pub(crate) fn due_index() (VICTORY_DUE_INDEX=1, off by default).
    • Add DueBuckets storing the same contiguous 32-byte Entry values grouped per due frame in BTreeMap<u64, Vec<Entry>>, with FastMap<u32, (u64, u32)> for O(1) slot lookup and in-bucket swap-remove, O(1)-maintained global census counters ([c[2], c[3], c[5]]), and a single recycled spare bucket buffer to avoid steady-state re-allocations as frame buckets drain.
    • collect_due_and_census, census, and all_due iterate only by_due.range(..=frame) (O(|due|) instead of O(|queue|)).
    • Reuse all existing #797 machinery (Due::with_capacity hints outside the lock, Due::ordered partial selection, QueueMarks lock-free shadows, QueueBatch, cold checked/mapped/held side maps).
    • Add unit tests:
      • due_buckets_matches_flat_texture_queue: 4,000-step randomized equivalence test comparing flat TextureQueue vs DueBuckets across written, remove, done, later, hold, room, partly, recheck (print == 0 and != 0), changed, and QueueBatch::apply, verifying identical due_and_census, ordered, census, all_due, checked, mapped, was_uploaded, QueueMarks, and internal invariants.
      • due_buckets_skips_future_and_recycles_spare: verifies zero-work future skips and empty-bucket capacity recycling.
  • crates/victory-gpu/src/draw.rs:
    • When VICTORY_DUE_INDEX=1 and VICTORY_TEXSPK_PASS_MS is configured, skip before(0) and texture_slots_due_known if the current frame's upper-bound texspk_cap_ms budget is already exhausted, while still flipping finished uploads via texture_views_remade() and texture_slots_flip().

Verification

Automated

  • cargo test -p victory-gpu --lib --locked descriptor::host::tests -- --test-threads=1: 61 passed, 0 failed (including all #797 tests and both new due_buckets_* tests).
  • cargo test -p victory-gpu --lib --locked texspk: 2 passed, 0 failed.
  • cargo clippy -p victory-gpu --lib --tests --locked: 0 warnings in modified files.
  • cargo build --release --locked: succeeded cleanly.

In-game A/B benchmark

Same release binary (pr/due-index on main 923e38c, which already includes #797), RTX 3060 12 GB, 1440p, DLSS Performance, warm pipeline cache (0 being made throughout 3D), identical flags (VICTORY_TEXTURE_STAGE_MS=16, VICTORY_POOL_SMALL=1), only toggling VICTORY_DUE_INDEX:

Metric (3D, per 5 s / per frame) Baseline (main + #797, DUE_INDEX=0) VICTORY_DUE_INDEX=1 Delta
held taking the slots due (med / mean / max / 5 s) 75.0 / 72.8 / 100 ms 41.0 / 38.2 / 60 ms -45% med / -48% mean / -40% max
held taking the slots due (steady state, last 2 windows) 48 / 27 ms 9 / 3 ms -81% to -89%
Total queue-lock wait (mean / max / 5 s) 721.9 / 1,120 ms 593.3 / 915 ms -18% mean / -18% max
Other threads waiting on queue lock (mean / max / 5 s) 621.2 / 942 ms 518.1 / 813 ms -17% mean / -14% max
Pass waiting on queue lock (med / max / 5 s) 99.0 / 177 ms 73.0 / 113 ms -26% med / -36% max
before the passes' loops (mean / max / 5 s) 319.5 / 473 ms 271.7 / 407 ms -15% mean / -14% max
3D FPS (mean / med / max) 10.43 / 10.20 / 14.20 11.20 / 10.75 / 16.40 +7.4% mean / +5.4% med
3D frame time (med / p95 / max) 82.9 / 125.3 / 640.7 ms 77.8 / 108.9 / 268.7 ms -6% med / -13% p95 / -58% max
1 s windows with longest >= 180 ms / >= 300 ms 22 (28.2%) / 2 13 (16.5%) / 0 -41% spikes / 0 over 300 ms
Submitting thread median / GPU busy median 74.0 ms / 74.0% 70.2 ms / 82.0% -3.8 ms / +8.0 pp

Both runs exited cleanly with zero D3D12 errors.

**@⁠Czer0xx** on GitHub · 2026-11-28 20:49 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/1497) `Czer0xx:pr/due-index` → `main` · +559 −38 · 2 files · 1 commits · merged 2026-11-29 08:16 UTC ## Motivation After #797 (`28f87ef`) packed `TextureQueue` into a dense 32-byte `Vec<Entry>`, `collect_due_and_census`, `census`, and `all_due` still linearly scan every queued entry under `HostHeap::textures`'s mutex on every pass. During gameplay the queue holds ~25,000-33,000 entries (~800-1,060 KiB), even when only a fraction has `due <= frame`. On RTX 3060 12 GB (1440p release, warm cache, post-#797 `main`), `held taking the slots due` still averaged 72.8 ms / 5 s (median 75 ms, max 100 ms), blocking other threads for 621.2 ms / 5 s (max 942 ms). ## Changes All new behavior is opt-in behind **`VICTORY_DUE_INDEX=1`** (off by default, read once). With it unset, `TextureQueue` and `heap_textures` behave identically to `main`. - **`crates/victory-gpu/src/descriptor/host.rs`**: - Add `pub(crate) fn due_index()` (`VICTORY_DUE_INDEX=1`, off by default). - Add `DueBuckets` storing the same contiguous 32-byte `Entry` values grouped per `due` frame in `BTreeMap<u64, Vec<Entry>>`, with `FastMap<u32, (u64, u32)>` for O(1) slot lookup and in-bucket swap-remove, O(1)-maintained global census counters (`[c[2], c[3], c[5]]`), and a single recycled `spare` bucket buffer to avoid steady-state re-allocations as frame buckets drain. - `collect_due_and_census`, `census`, and `all_due` iterate only `by_due.range(..=frame)` (`O(|due|)` instead of `O(|queue|)`). - Reuse all existing #797 machinery (`Due::with_capacity` hints outside the lock, `Due::ordered` partial selection, `QueueMarks` lock-free shadows, `QueueBatch`, cold `checked`/`mapped`/`held` side maps). - Add unit tests: - `due_buckets_matches_flat_texture_queue`: 4,000-step randomized equivalence test comparing flat `TextureQueue` vs `DueBuckets` across `written`, `remove`, `done`, `later`, `hold`, `room`, `partly`, `recheck` (`print == 0` and `!= 0`), `changed`, and `QueueBatch::apply`, verifying identical `due_and_census`, `ordered`, `census`, `all_due`, `checked`, `mapped`, `was_uploaded`, `QueueMarks`, and internal invariants. - `due_buckets_skips_future_and_recycles_spare`: verifies zero-work future skips and empty-bucket capacity recycling. - **`crates/victory-gpu/src/draw.rs`**: - When `VICTORY_DUE_INDEX=1` and `VICTORY_TEXSPK_PASS_MS` is configured, skip `before(0)` and `texture_slots_due_known` if the current frame's upper-bound `texspk_cap_ms` budget is already exhausted, while still flipping finished uploads via `texture_views_remade()` and `texture_slots_flip()`. ## Verification ### Automated - `cargo test -p victory-gpu --lib --locked descriptor::host::tests -- --test-threads=1`: **61 passed, 0 failed** (including all #797 tests and both new `due_buckets_*` tests). - `cargo test -p victory-gpu --lib --locked texspk`: **2 passed, 0 failed**. - `cargo clippy -p victory-gpu --lib --tests --locked`: **0 warnings** in modified files. - `cargo build --release --locked`: succeeded cleanly. ### In-game A/B benchmark Same release binary (`pr/due-index` on `main` `923e38c`, which already includes #797), RTX 3060 12 GB, 1440p, DLSS Performance, warm pipeline cache (`0 being made` throughout 3D), identical flags (`VICTORY_TEXTURE_STAGE_MS=16`, `VICTORY_POOL_SMALL=1`), only toggling `VICTORY_DUE_INDEX`: | Metric (3D, per 5 s / per frame) | Baseline (`main` + #797, `DUE_INDEX=0`) | `VICTORY_DUE_INDEX=1` | Delta | |---|---:|---:|---:| | `held taking the slots due` (med / mean / max / 5 s) | 75.0 / 72.8 / 100 ms | **41.0 / 38.2 / 60 ms** | **-45% med / -48% mean / -40% max** | | `held taking the slots due` (steady state, last 2 windows) | 48 / 27 ms | **9 / 3 ms** | **-81% to -89%** | | Total queue-lock wait (mean / max / 5 s) | 721.9 / 1,120 ms | **593.3 / 915 ms** | **-18% mean / -18% max** | | Other threads waiting on queue lock (mean / max / 5 s) | 621.2 / 942 ms | **518.1 / 813 ms** | **-17% mean / -14% max** | | Pass waiting on queue lock (med / max / 5 s) | 99.0 / 177 ms | **73.0 / 113 ms** | **-26% med / -36% max** | | `before the passes' loops` (mean / max / 5 s) | 319.5 / 473 ms | **271.7 / 407 ms** | **-15% mean / -14% max** | | 3D FPS (mean / med / max) | 10.43 / 10.20 / 14.20 | **11.20 / 10.75 / 16.40** | **+7.4% mean / +5.4% med** | | 3D frame time (med / p95 / max) | 82.9 / 125.3 / 640.7 ms | **77.8 / 108.9 / 268.7 ms** | **-6% med / -13% p95 / -58% max** | | 1 s windows with longest >= 180 ms / >= 300 ms | 22 (28.2%) / 2 | **13 (16.5%) / 0** | **-41% spikes / 0 over 300 ms** | | Submitting thread median / GPU busy median | 74.0 ms / 74.0% | **70.2 ms / 82.0%** | **-3.8 ms / +8.0 pp** | Both runs exited cleanly with zero D3D12 errors.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-28 20:51:40 +00:00
Author
Owner

@⁠copilot-pull-request-reviewer[bot] on GitHub · 2026-11-28 20:49 UTC · original · reviewed

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

**@⁠copilot-pull-request-reviewer[bot]** on GitHub · 2026-11-28 20:49 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/1497#pullrequestreview-6100030307) · **reviewed** Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.
Author
Owner

@⁠Slydock on GitHub · 2026-11-28 20:56 UTC · original · approved these changes

**@⁠Slydock** on GitHub · 2026-11-28 20:56 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/1497#pullrequestreview-6100055911) · **approved these changes**
Author
Owner

@⁠POMXARK on GitHub · 2026-11-29 02:27 UTC · original

@⁠Czer0xx — huge validation of the exact bottleneck this PR targets, plus a data point on taking the index one step further with an O(1) 256-slot timing wheel (ready: Vec + wheel: [Vec; 256] + overflow BTreeMap + O(1) census counters).

1. Synthetic 30,000-entry TextureQueue microbenchmark (300 passes = 5 s window, Ryzen 7 5700X)

Structure Time / 5 s Speedup Exact due set & census
Linear HashMap scan 14.09 ms 1.0x Reference
Dense Vec scan 4.67 ms 3.0x Exact match
BTreeMap<u64, Vec<Entry>> (PR #1497 design) 2.89 ms 4.9x Exact match
O(1) 256-slot Timing Wheel + ready + overflow 0.68 ms 20.7x (4.25x vs BTreeMap) Exact match

2. Full 20-minute 3D A/B on Windows 11 (RTX 5080 16 GB, Ryzen 7 5700X 8C/16T, 32 GB RAM, 1440p DLSS Quality + 4x FG, n=228 5-second windows, 0 errors)

Metric (per 5 s window) Baseline (BTreeMap<u32, Waiting> scan) O(1) 256-slot Timing Wheel Delta
held taking the slots due (med / mean) 129.0 / 142.8 ms 23.0 / 27.5 ms -82.2% / -80.7% (5.6x faster)
held taking the slots due (p90 / max) 271.0 / 539 ms 59.0 / 84 ms -78.2% / -84.4%
waited for texture queue's lock (med / mean) 860.5 / 1,103.9 ms 415.5 / 475.9 ms -51.7% / -56.9% (-628 ms / 5s)
waited for texture queue's lock (p90 / max) 2,410.0 / 3,668 ms 981.0 / 1,978 ms -59.3% / -46.1%
>=10k draws/f median 8.60 FPS 8.80 FPS +2.3%
Peak Commit 31,262 MB 30,625 MB -637 MB

Keeping already-due entries in a contiguous ready: Vec and near-term future frames [cursor+1 .. cursor+256) in a power-of-two ring (wheel[due & 255], overflowing only >= 256 frames into BTreeMap) avoids tree rebalancing on every frame advance and keeps steady-state due_and_census strictly O(|due|) with O(1) swap_remove. Clean-room reference implementation + tests: https://github.com/POMXARK/bcpack-gpu-sdk/blob/main/src/timing_wheel.rs — feel free to reuse or adapt directly in this PR if helpful!

**@⁠POMXARK** on GitHub · 2026-11-29 02:27 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/1497#issuecomment-7249439732) @⁠Czer0xx — huge validation of the exact bottleneck this PR targets, plus a data point on taking the index one step further with an **O(1) 256-slot timing wheel** (`ready: Vec` + `wheel: [Vec; 256]` + overflow `BTreeMap` + O(1) census counters). ### 1. Synthetic 30,000-entry `TextureQueue` microbenchmark (300 passes = 5 s window, Ryzen 7 5700X) | Structure | Time / 5 s | Speedup | Exact due set & census | |---|---:|---:|---| | Linear `HashMap` scan | `14.09 ms` | `1.0x` | Reference | | Dense `Vec` scan | `4.67 ms` | `3.0x` | Exact match | | `BTreeMap<u64, Vec<Entry>>` (PR #1497 design) | `2.89 ms` | `4.9x` | Exact match | | **O(1) 256-slot Timing Wheel + `ready` + overflow** | **`0.68 ms`** | **`20.7x` (`4.25x` vs `BTreeMap`)** | **Exact match** | ### 2. Full 20-minute 3D A/B on Windows 11 (RTX 5080 16 GB, Ryzen 7 5700X 8C/16T, 32 GB RAM, 1440p DLSS Quality + 4x FG, `n=228` 5-second windows, `0` errors) | Metric (per 5 s window) | Baseline (`BTreeMap<u32, Waiting>` scan) | **O(1) 256-slot Timing Wheel** | Delta | |---|---:|---:|---:| | **`held taking the slots due` (med / mean)** | `129.0` / `142.8 ms` | **`23.0` / `27.5 ms`** | **`-82.2%` / `-80.7%` (`5.6x` faster)** | | **`held taking the slots due` (p90 / max)** | `271.0` / `539 ms` | **`59.0` / `84 ms`** | **`-78.2%` / `-84.4%`** | | **`waited for texture queue's lock` (med / mean)** | `860.5` / `1,103.9 ms` | **`415.5` / `475.9 ms`** | **`-51.7%` / `-56.9%` (`-628 ms / 5s`)** | | **`waited for texture queue's lock` (p90 / max)** | `2,410.0` / `3,668 ms` | **`981.0` / `1,978 ms`** | **`-59.3%` / `-46.1%`** | | **`>=10k` draws/f median** | `8.60 FPS` | **`8.80 FPS`** | **`+2.3%`** | | **Peak Commit** | `31,262 MB` | **`30,625 MB`** | **`-637 MB`** | Keeping already-due entries in a contiguous `ready: Vec` and near-term future frames `[cursor+1 .. cursor+256)` in a power-of-two ring (`wheel[due & 255]`, overflowing only `>= 256` frames into `BTreeMap`) avoids tree rebalancing on every frame advance and keeps steady-state `due_and_census` strictly `O(|due|)` with O(1) `swap_remove`. Clean-room reference implementation + tests: https://github.com/POMXARK/bcpack-gpu-sdk/blob/main/src/timing_wheel.rs — feel free to reuse or adapt directly in this PR if helpful!
- Add verified runtime hooks for buffer and texture creation and GPU frees
- Track CPU access hints and streaming buddy extents to skip redundant GPU work
github-bot force-pushed github-bot/gh-1497 from 15b72ccd0a to f23ddf55ae 2026-11-29 03:39:09 +00:00 Compare
Author
Owner

@⁠Ty-lerCox on GitHub · 2026-11-29 04:08 UTC · original

this worked very well.

**@⁠Ty-lerCox** on GitHub · 2026-11-29 04:08 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/1497#issuecomment-7250474927) this worked very well.
github manually merged commit 561fc512c4 into main 2026-11-29 08:21:24 +00:00
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/trustno1-coder--VIctory-NVIDIA!1497
No description provided.