feat(gpu): adapt the frame budget for background texture streaming #313
No reviewers
Labels
No labels
accessibility
bug
documentation
duplicate
enhancement
good first issue
help wanted
invalid
question
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/lobomfz--VIctory-xmem!313
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-313"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
@tommiehansen on GitHub · 2026-12-03 12:21 UTC · original
tommiehansen:perf/phase3-frame-texture-budget→main· +1260 −16 · 7 files · 11 commits · openWhat this does
Adapt the frame budget for background texture streaming.
Checks
VICTORY_TEXTURE_STREAM_FRAME=1opts in); disabled path intended unchanged.rejected_recent_probes_do_not_exhaust_due_selection_budget, randomized oracle and 15 portable tests; final-head rerun and explicit before failure pending.cargo xtask policyon the final revision; no new game assets or build output intended.Verified
Current review head:
721061a15eca94973fc682b891b4125952705751. Existing measurements and commands are retained below at their original revisions. No fresh compile, policy or hardware test is claimed by this metadata review.Cost
Synthetic queue work −99.18%; live uploads, visual parity and frame-time benefit unverified.
Detailed implementation and historical validation
Frame-budgeted background texture streaming (
VICTORY_TEXTURE_STREAM_FRAME=1, opt-in; default behavior unchanged when unset).Summary
Replaces full-slot queue scans in
TextureQueuewith an indexed(due_frame, slot)traversal (DueCursor) paired with bounded frame upload counts/microsecond budgets and an overdue/recency index (ReadyIndex), eliminating O(total_slots) scans per frame under sparse or backlogged queues.Key Mechanism
TextureQueue::by_due,DueCursor): Resets at the start of each new frame (DueCursor::begin_frame) to start from the earliest due(due, slot)entries instead of carrying a wrapping cursor across frame boundaries, and advances within a frame past slots currently active on another worker or already selected (wanted). Walks at mostSTREAM_STEP_MAX(32) entries per short lock acquisition (DueCursor::next_due).ReadyIndex,sample_due): Walks at mostBACKLOG_THRESHOLD(1024) due entries instead of counting the entire queue, and tracks true overdue age (ReadyIndexkeyed by(due.max(written), written, slot)) so rewrites cannot mask the oldest overdue item.DueCursor::want_recent,ReadyIndex::next_recent):want_recent/note_recent_pick/note_due_pick, bounded byRECENT_PROBE_MAX = 8) instead of capping recent admission to one item per frame. This priority is bounded by the existingRECENT_WRITE_FRAMES = 4/RECENT_MAX_AGE_FRAMES = 16window (andprev_active_frameacross skipped passes, starting atframe.saturating_sub(RECENT_WRITE_FRAMES)before the first active frame), so bursts inside the window interleave while bursts exceeding the window age into standard(due, slot)order.recent_visitsinnext_recentcount against theSTREAM_STEP_MAX(32) per-lock inspection cap (reserving at least one visit fornext_due), while rejected recent probes (written < floor, deferredlaterentries, or already-looked slots withlooks != 0 || mapped != 0) no longer subtract fromremaining(due_selectionbudget). Accepted recent picks consume 1 budget unit.Benchmarks (CPU-Only Synthetic Suite, Apple M5 macOS)
Paired A/B/B/A (
--profile standard --blocks 4 --suite textures, 8 rounds/side) usingtools/benchmark-suite:vs
origin/main(45591b0unset vs500acacVICTORY_TEXTURE_STREAM_FRAME=1)origin/main(ms)500acac(ms)main->500acac)textures/stream_queue_lifecycleslots_inspected: 1,675,620 -> 5,940 (-99.65%)textures/stream_queue_policyslots_inspected: 495,906 -> 2,028;due_sampled: 16,384 -> 204Follow-Up Check vs Previous PR Head (
a68af21vs500acac, bothVICTORY_TEXTURE_STREAM_FRAME=1)a68af21(ms)500acac(ms)a68af21->500acac)textures/stream_queue_lifecycleidenticalskipped_pass_fresh_delay: 0 -> 0;fresh_burst_within_2_frames: 7 -> 7;slots_inspected: 5,940 -> 5,940textures/stream_queue_policyidenticalslots_inspected: 3,733 -> 2,028 (recent probes no longer debit due budget)Verification & Reproducibility
500acac): 15/15 tests pass in both debug (0.26s) and release (0.03s), including the 200-seed randomized queue oracle, 100-seed light-workload zero-left-behind test, rejected-recent-probe regression tests (laterdeferred +looks == 1+ mixed scenarios), 10-of-10 burst 1:1 interleaving, tiny-budget multi-frame burst anti-starvation, and multi-frame skipped-pass watermark tests.cargo clippy --offline -p victory-gpu --all-targets --target x86_64-pc-windows-msvc --features blake3/pure,diagnosticsclean instream_policy.rsandstream_next.Known Limitations
VICTORY_TEXTURE_STREAM_FRAME=1) so default runtime behavior onmainis preserved unless explicitly enabled.Thanks to @Kal0s-design for reviewing
17af52eanda68af21, surfacing the fresh-burst saturation, skipped-pass aging, rejected-recent-proberemainingcharge, Clippy nits, and PR description wording issues.@tommiehansen on GitHub · 2026-12-03 19:47 UTC · original
Adapted to
by_dueIndexed Queue & False-Backlog Fix (2da28da)Merged
origin/main(45591b0, including PR #295'sby_dueindexed due queue and PR #304's descriptor copy spans) and updatedVICTORY_TEXTURE_STREAM_FRAMEto consumeby_dueinstead of scanningTextureQueue::slots.Technical changes
by_duecursor (crates/victory-gpu/src/descriptor/stream_policy.rs,DueCursor): Replaces the previousslotsfull-queue cursor scan (which walked futurelaterandu64::MAXheldentries across 32-slot lock acquisitions) with a boundedby_duerange cursor (..=(frame, u32::MAX)). Resets cleanly at each new frame so oldest overdue slots are served first without starvation while advancing within a frame pastactive/wantedslots.sample_due,StreamQueueStats): Samplesby_dueup toBACKLOG_THRESHOLD(1024) instead of treatingslots.len() >= 1024(which includes future/held entries) as an active backlog, preventing false 3x frame-budget bursts and skippingTextureStreamPass::startcompletely whendue_sample == 0. Also populatesHEAP_TEXTURES_QUEUE[1..6]andHEAP_TEXTURES_OVERDUE_PEAKtelemetry in the adaptive path.DueCursor,sample_due,texture_stream_micros,texture_stream_spent,background_texture_pass, andpresent_clock_neededintocrates/victory-gpu/src/descriptor/stream_policy.rs.macOS ARM64 CPU Benchmark Results (
rustc -O, A/B/B/A 8-block median, direct productionstream_policy.rs)27,000-slot
TextureQueuemodeled after #295's measured profile:slotsscan)by_dueindex)sample_due+ overdue priority)Verification & Reproduction
rustc --edition 2024 --test crates/victory-gpu/src/descriptor/stream_policy.rs -o /tmp/test_stream_policy && /tmp/test_stream_policy(7 passed, 0 failed).cargo check -p victory-gpu --lib --target x86_64-pc-windows-msvc --features blake3/pureand--features blake3/pure,diagnostics(0 errors, 0 warnings, check-onlylibmimalloc-sysshim), pluscargo --offline xtask policy --staged(6 files ok) andxtask -- switches(0 new errors).VICTORY_TEXTURE_STREAM_FRAME=1interaction with real gameplay streaming, and frame-time impact require external AMD hardware testing.@Kal0s-design on GitHub · 2026-12-03 20:37 UTC · original
Tested
2da28daon Windows 11 with a native MSVC build (toolchain 1.98.1) and one NVIDIA desktop card for the D3D12 tests. I only ran-p victory-gpuand no game, so I can't say anything about frame times. No test goes throughheap_texturesorafter_present, so for the draw.rs loop and the present timing change I only read the code.With
VICTORY_TEXTURE_STREAM_FRAMEunset I don't see any change in behaviour.background_texture_passis false (draw.rs:3770), so the oldtexture_slots_wantedpath runs and the new branch of the upload loop (draw.rs:3886) is never reached.present_clock_needed(timing, adaptive, false)in host.rs:8757 comes out the same as the oldtiming || adaptive.The cursor looks right to me. I ran a throwaway randomized test (not pushed) that compares
next_dueagainst a simple reference model while slots get inserted, removed and re-dued between calls, and it matched over 200 runs. Every slot it picks also goes throughlease_textureagain under the lock before it's used. The twobreaks that leave a picked slot due (draw.rs:4168 and 4350) only happen in the 500 ms window after an out of memory error, and main stops there too.Two things worth knowing before anyone does the A/B.
The background order changes as well as the speed. The cursor goes back to the front of
by_dueevery frame (stream_policy.rs:49), so it always takes the oldest due slot first. Main'sin_order(descriptor/host.rs:1125) alternates the newest written slot with the oldest. In a local test with 2000 slots written at frame 1 and one more written at frame 49, main hands out the new one first in frame 50, and the cursor only reaches it on pick 2001. Material requests still go ahead of both, so this may be fine, but a slot written just now that no material asks for waits behind the whole backlog. Also the order is by due frame and then slot, not slot order like the description says, and "the cursor survives deferral" is only true within the same frame.With the switch on, the texture age numbers aren't comparable with main.
HEAP_TEXTURES_QUEUE[4]is never written on that path (draw.rs:3804-3812), and the overdue peak only looks atby_due.first()(descriptor/host.rs:2230) where main takes the max over every due slot.changed()(descriptor/host.rs:1286) keeps the lower due but moveswrittenup to now, so the first entry isn't always the oldest. For example slot A is due at 10 and gets changed at frame 40, and slot B is written and due at 20. At frame 50 main reports 30 and this branch reports 10.The 8 new tests passed 20 times in a row in both debug and release. None of them read the env switch. For the full suite I skipped
descriptor::tests::views_die_with_their_resourcebecause it's known to crash on main. The PR head got 337 passed and 5 failed (338 and 4 with the switch set), and main at0b652f8got 338 and 5. The same four failures inindirect::testsandpipeline::host::testsshow up on both. The last one is awatch::teststest that changes from run to run. Clippy shows no new warnings compared to main, and the branch still merges cleanly.AI-assisted: yes
@Kal0s-design on GitHub · 2026-12-03 22:02 UTC · original
Retested at
17af52e, same setup as before (native Windows build,-p victory-gpuonly, no game).The overdue numbers are fixed. I ran a throwaway randomized test (not pushed) through
written,changed,later,doneandstream_nextwith the switch on, 200 seeds of 500 frames.stream_readyalways held exactly the entries inslots, andages().1matched main's census overdue value on every frame. The sampled write age never went above main's, and in that load it never came out lower either. You can get it lower on purpose. With 40 slots written at frame 10 and one written at frame 1 but pushed to due 45 bylater, main says 49 at frame 50 and this branch says 40. That's what the new log field says, so I'm fine with it.The order is better for a single fresh slot. Same scenario as last time, 2000 slots written at frame 1 and one at frame 49, picked at frame 50. Main hands out 5000, 1, 2000, 2. This head gives 5000, 1, 2, 3 when the cursor has its turn on frame 50. When frame 50 is the off turn, with 8 picks per frame it takes 1 to 8 there and gives 5000 first on frame 51. The index is only kept with the switch on, so the same test run without it still gets 5000 at pick 2001, but with the switch off this path isn't used anyway.
Two cases still fall back to the old order.
More than one fresh slot. With 10 slots written at frame 49, main gives them picks 1, 3, 5 up to 19 of frame 50. Here only one of them gets the recent turn. The other nine come out last, behind all 2000 old slots, because by frame 52 they are outside the two frame window.
A frame with no background pass. If frame 50 is the off turn and the streamer doesn't run at frame 51, the slot written at 49 is already out of the window at frame 52 and comes out as pick 1993.
Neither one loses anything. The oldest lane still walks by due frame and slot and nothing starved in the randomized runs. The longest any due slot waited was 2 frames, at a load where picks kept up with writes. So I'd call this a bounded nudge for new writes rather than main's alternation, and that might be all you want. The new section of the description says due frame and slot, but the older paragraph still says "slot order" and that the cursor survives deferral, so the two now disagree.
The other points hold. The recent lane returns the same
(slot, write, age)tuple and goes through the samelease_texturecheck as before. The visit charge can't underflow because the probe leaves at least one visit for the cursor. With the switch off the only extra work is oneOnceLockread per queue edit, the index stays empty, and the old path runs. The meter log line does get one more field, which is just false then.Tests. The 10 policy and cursor tests passed 20 times in a row in debug and release, with the switch unset and with it set to 1. The host.rs cursor test now does depend on the switch, but it passes either way. Full suite, skipping
descriptor::tests::views_die_with_their_resourcelike before. Unset gave 340 passed and 4 failed. Set to 1 gave 339 and 5. The four inindirect::testsandpipeline::host::testsare the same ones that fail on main, and the fifth with the switch set is thewatch::teststest that changes from run to run. Last time it showed up on the unset run instead, so it doesn't follow the switch. Clippy shows no new warnings compared to main.AI-assisted: yes
@tommiehansen on GitHub · 2026-12-04 00:02 UTC · original
Thanks @Kal0s-design for catching both scheduling edge cases and the stale PR description text. Updated the branch to
a68af21(dd1d75f+a68af21) and refreshed the PR description to match the(due, slot)cursor and fairness rules.Changes in
dd1d75f..a68af21DueCursor::want_recent/ReadyIndex::next_recent): Replaced the single-shot per-framescout_recentflag with 1:1 per-pick alternation (want_recent/note_recent_pick/note_due_pick, bounded byRECENT_PROBE_MAX = 8, descending from(frame, u64::MAX, u32::MAX)viarecent_last+recent_exhausted) so fresh bursts cannot saturate after one admission and fall behind an overdue backlog.DueCursor::prev_active_frame,recent_floor): Tracks the previous active selection frame (prev_active_frame, bounded byRECENT_WRITE_FRAMES = 4andRECENT_MAX_AGE_FRAMES = 16, initialized toframe.saturating_sub(RECENT_WRITE_FRAMES)before the first active frame so initial/reset frames avoid an unnecessary 16-frame lookback). Fresh writes arriving right before deferred passes stay withinrecent_floorwhen background work resumes.Validation (
a68af21)stream_policy.rs): 13/13 passing in both debug (0.26s) and-Orelease (0.04s), includingburst_of_recent_writes_interleaves_one_to_one_with_backlog,multi_frame_burst_under_tiny_budget_and_continuous_stream_avoid_starvation, andskipped_background_passes_preserve_recent_write_priority_across_frames.cargo check --offline -p victory-gpu --all-targets --target x86_64-pc-windows-msvc --features blake3/pure,diagnosticsclean.--profile standard --blocks 4 --suite textures):origin/main(45591b0, unset vsVICTORY_TEXTURE_STREAM_FRAME=1):textures/stream_queue_lifecycle5.183 -> 0.863 ms(-83.35%,slots_inspected:1,675,620 -> 5,940);textures/stream_queue_policy1.803 -> 0.015 ms(-99.18%,slots_inspected:495,906 -> 3,733).17af52e(bothVICTORY_TEXTURE_STREAM_FRAME=1): intextures/stream_queue_lifecycle,skipped_pass_fresh_delaydropped32 -> 0,fresh_burst_within_2_framesimproved0 -> 7, andslots_inspecteddropped9,240 -> 5,940(-35.7%) at parity frame time (0.875 -> 0.870 ms,-0.41%). In statictextures/stream_queue_policy, 1:1 interleaving adds~2 us(0.013 -> 0.015 ms,+19.78%,slots_inspected:2,201 -> 3,733) vs the 1/frame scout while remaining-99.18%vsorigin/main.@Kal0s-design on GitHub · 2026-12-04 00:29 UTC · original
Rechecked at
a68af21with the same setup as before (native Windows build,-p victory-gpuonly, no game).Both order cases from last time are fixed. With 10 slots written at frame 49 behind 2000 old ones, frame 50 now hands them out at picks 1, 3, 5 up to 19, the same as main, or 2, 4 up to 20 when frame 50 starts on the old turn. At 8 picks per frame they go out 4 per frame over frames 50 to 52, frame for frame the same as main. The single slot written at 49 with one pick on the off turn at 50 and no pass at 51 is now pick 1 at frame 52 (it was 1993). The window edges behave the way the description says. After a pass at 50 the next pass keeps the alternation up to frame 62 and from 66 on the burst goes to the back, and a first ever pass still alternates at 53 and falls back at 54. Longer bursts only get part of the way. With 40 written at 49 and 8 picks per frame, 20 interleave over frames 50 to 54 and the other 20 end up behind the 2000 old slots, where main has all 40 out by frame 59. That's the bounded window doing what it says, but "cannot fall behind" is a bit strong for bursts longer than the window.
One new thing. The recent probe charges every entry it looks at against
remaining, including the ones it rejects, and outside a backlogremainingis exactly the due count fromsample_due. Entries in the probe range that failwritten >= floororlooks == 0 && mapped == 0use up visits, and the due cursor then runs out before it reaches them. Simple case, 10 slots written at frame 1 and put off withlaterto due 50, nothing else due. Main hands out all 10 at frame 50. This head gives 2 at 50, then 1 per frame, and the last 3 at 56. That's with one pass per frame. A second pass in the same frame does pick up the rest, sincerecent_lastis already past them. Recent writes that already had one look behave the same, 5 of them took until frame 55 next to 13 other due slots that main served all at once. In a randomized run with light load, frames that had picks to spare but still left a due slot behind went from 0 at2da28dato 1,866 at17af52eand 10,083 (out of about 97k) at this head. With 4 times the visit budget it drops to 0, so it's only the charge. Nothing gets lost, they're out of the window after a few frames. Not counting rejected recent probes againstremaining(they still count against the 32 per lock) would probably be enough.Fairness, same throwaway randomized harness, about 300 seeds of 600 frames with writes,
changed,later, removes, active slots and skipped passes. No slot was lost and picks from the oldest lane always came out in rising(due, slot)order within a frame. Longest wait of a due slot under light load was 7 frames (main 8,17af52e6). Under heavy load it was 28 (main 41,17af52e20), with p99 14 against 16 on main and 9 on17af52e. So the 1:1 alternation costs the backlog a bit compared with the last head but it's still ahead of main. A separate 500 seed run without removes checked that the ready index always held exactly the queued slots, and it did. These loads never got to 1024 due slots, so they say nothing about the backlog path.From reading the new state I didn't find a problem. The range can't invert because
prev <= lowerreturns first, held slots atu64::MAXsit above the upper bound, a removedrecent_lastkey is only used as a bound, and a frame going backwards resets everything (the host test's call at 40 after 50 covers that). A write that lands mid frame afterrecent_exhaustedwaits for the next frame, which seems fine.Tests. The 14 policy and cursor tests passed 20 times in a row in debug and release, with the switch unset and set to 1. Full suite, skipping
descriptor::tests::views_die_with_their_resourceas before, gave 343 passed and 5 failed both ways, the same fourindirect::testsandpipeline::host::testsfailures as main plus thewatch::testsone that changes from run to run. Clippy has three newmanual_is_multiple_ofwarnings, all in the new randomized test in stream_policy.rs (rng.next() % 5 == 0and the like), nothing in the library code. The branch still merges cleanly.The description matches the code now, the old "slot order" and "survives deferral" lines are gone. Small thing, the sampling bullet says O(1) but it walks up to 1024 entries.
AI-assisted: yes
@tommiehansen on GitHub · 2026-12-04 00:49 UTC · original
@Kal0s-design Thank you for re-checking
a68af21and pinpointing the rejected-proberemainingcharge — that was a clear accounting bug. Fixed in500acacwith a minimal change:remainingdue-selection budget:ReadyIndex::next_recent(crates/victory-gpu/src/descriptor/stream_policy.rs) now takesmax_probe(STREAM_STEP_MAX - 1, capped byRECENT_PROBE_MAX = 8) instead ofremaining, leaving at least one per-lock visit fornext_due.TextureQueue::stream_next(crates/victory-gpu/src/descriptor/host.rs) counts allrecent_visitsagainstSTREAM_STEP_MAX(STREAM_STEP_MAX - recent_visitspassed tonext_due), returns1againstremainingon an accepted recent pick, and passes fullremaining(notremaining - recent_visits) tonext_dueso rejected recent entries (written < floor, deferredlaterentries, orlooks != 0 || mapped != 0) do not exhaust the pass budget.rejected_recent_probes_do_not_exhaust_due_selection_budgetcovering both exact failure cases you described (10later-deferred entries due at frame 50, 5 already-looked recent writes next to 13 older due entries, plus a mixed fresh/looked/old 1:1 interleaving case — all complete in a single pass withrecent_visits + due_visits <= STREAM_STEP_MAX) andlight_workload_leaves_no_eligible_due_slots_behind_across_frames(100 seeds × 200 frames, 0 left-behind eligible slots).% 5 == 0,% 8 == 0, and% 4 == 0in the randomized test with.is_multiple_of(...)(0 warnings instream_policy.rsandstream_nextunder Windowscargo clippy --all-targets).<= 1024entries, not O(1)) and clarified that recent-write priority is bounded by theRECENT_WRITE_FRAMES = 4/RECENT_MAX_AGE_FRAMES = 16window.500acac):stream_policy.rsstandalone tests: 15/15 pass in debug (0.26s) and release (0.03s).victory-gpu --all-targetscargo clippy: clean on modified code.--profile standard --blocks 4 --suite textures):origin/main(45591b0unset vs500acacVICTORY_TEXTURE_STREAM_FRAME=1):textures/stream_queue_lifecycle5.141 -> 0.865 ms(-83.21%,slots_inspected1,675,620 -> 5,940);textures/stream_queue_policy1.805 -> 0.015 ms(-99.18%,slots_inspected495,906 -> 2,028,due_sampled16,384 -> 204).a68af21(a68af21vs500acac, both enabled):textures/stream_queue_lifecycle0.870 -> 0.859 ms(-0.77%,identical,skipped_pass_fresh_delay = 0,fresh_burst_within_2_frames = 7);textures/stream_queue_policy0.015 -> 0.015 ms(-0.53%,identical,slots_inspected3,733 -> 2,028).draw: adapt the frame budget for background texture streamingto feat(gpu): adapt the frame budget for background texture streamingView command line instructions
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.