GPU: list calls can translate on worker threads (VICTORY_DEFER_LISTS, opt-in) #3182
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/3184"
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?
Imported from Marvisak/VIctory. Originally posted by AurumCarlos on 2026-11-26T11:27:31Z.
First step for #2586: record list calls on the title's threads and translate them on worker threads. Off by default;
VICTORY_DEFER_LISTS=<workers>turns it on.What it does
A list made while the variable is set gets method tables whose entries log the call instead of translating it (
crates/victory-gpu/src/list/later.rs). The title's thread copies the arguments, including what they point to (barriers, viewports, scissors, constants, copy locations, rectangles, clear values, heaps, vertex views, render target handles, barrier group headers), and goes on. Workers take the log in batches of 32 and run the existing methods, unchanged, in the order the title made the calls. Each list is translated by one worker at a time, and different lists in parallel.Lists made without the variable keep today's tables, so the default path does not change. Every entry still calls the same functions.
What stays on the title's thread:
QueryInterface, upload memory (0x4a0), driver hints;Reset,ResetXand the lastRelease, which first wait for the list's logged calls. They free what those calls read: upload memory, the draw state and the host lists;ExecuteCommandListswaits for its lists before it takes the held-work lock, so other queues are not held up meanwhile.crate::external::{host_texture, record}(host libraries recording into a title list) wait too.Translation now reads title memory (descriptors, buffers, textures) after the call instead of during it, but still before the submission. On the console the GPU reads it even later, when the list runs, so a title cannot depend on the earlier read.
The 5-second report gets one line when the variable is set: calls translated by workers per frame, and how long title threads waited for them.
Tests
list::later::tests::each_log_runs_its_calls_in_order_on_workers: four logs filled at once. Each runs its calls in order, none on the logging thread, all of them by the time it settles.resource::tests::deferred_lists_waiting_for_another_queues_write_read_what_it_wrote: the existing two-queue test (wait, write, copies) with deferred lists. It calls through the tables as a title does. After the copy call it clears the copy location it passed, so the test fails if an argument is read later instead of copied (checked: it fails with the copy removed).every_inline_command_falls_back_to_the_tablenow resets through the table, as a title does. It calledresetdirectly whileClosewent through the table, so with deferred lists the reset ran before the logged Close.Run on an RX 9070 XT, Wine 11.22 (staging) with vkd3d-proton 3.1, cross-built for
x86_64-pc-windows-gnu:cargo test -p victory-gpu --lib: 201 passed. The same 30 tests fail onmain(63bcca4) in this environment and pass or fail identically on this branch.VICTORY_DEFER_LISTS=4, so every list in every test is deferred: 200 passed, same 30 failures. Three tests are left out:pipeline::depth_bounds_test: records on the host list directly between table calls and submits it itself, which a deferred list cannot support. The title cannot do this.frame::waits_end_on_ticks_and_count_frames: timing-sensitive. Passes alone with and without deferral.queue::aliased_present_planes_preserve_the_host_texture_state: hangs under Wine onmaintoo.cargo clippy -p victory-gpu --lib --tests -D warnings(Windows target, with and withoutdiagnostics),cargo xtask policy.Numbers
The benchmark from #2820 (
list_calls: 5 threads, about 8,300 calls each per frame, release), on this branch. I added a timer around its submit-and-wait locally (not part of this PR). Same machine as above. Median of 5 runs, ms per frame:What it shows:
DrawIndexedInstancedon the title's thread goes from about 3.8 µs to about 0.1 µs. Title-thread CPU time drops by 50-65%.main, the wall time does not follow. The descriptor writes that stay on the title's thread (CreateConstantBufferView, a device method) now wait for the workers on the locks #2820 found: 1.0 µs to 4.3 µs per call. With #2821, which removes those waits, title-thread time drops 45% at every worker count.During measurements, one benchmark run through
cargo testhung until its timeout, before the submission fix above. It did not happen again in 50 later runs (2, 4, 6 and 8 workers), and I have no explanation for it.Not done here
No game data. AI-assisted (Claude Code).
🤖 Generated with Claude Code
Imported from Marvisak/VIctory. Originally posted by Dukestar69 on 2026-11-26T12:09:28Z.
Review: APPROVED
Verified on RTX 5090 against
origin/main(63bcca4):cargo test -p victory-gpu --lib list:: -- --test-threads=1: 5/5 passed (includinglist::later::tests::each_log_runs_its_calls_in_order_on_workers).cargo test -p victory-gpu --lib draw:: -- --test-threads=1: 20/20 passed.cargo clippy -p victory-gpu --all-targets -- -D warnings: clean.cargo xtask policy: 192 files ok.Clean opt-in
VICTORY_DEFER_LISTSworker-thread translation for command list calls.Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
Imported from Marvisak/VIctory. Originally posted by steven-seagull27 on 2026-11-26T12:14:44Z.
I ran the two-queue fixture on native Windows at
768dcfa0, using an RTX 5080 and Windows' built-in WARP with the D3D12 debug layer. I expanded the pre-wait copy into 261 overlapping 8-byte writes through the title's command-list table. This crosses eight 32-call batch boundaries and leaves five writes in the final partial batch.VICTORY_DEFER_LISTS014Each setting gave the same result in two fresh processes per adapter. The pre-wait check observes VIctory's submission-time host-buffer copies. The separate texture check observes the graphics queue's write after the wait. Before the graphics submission, all 1,024 destination bytes still held the sentinel and the flag was 0. After submission, the flag was 1 and all four 16-byte rows matched. Omitting only the last five copies in a local control produced exactly 40 wrong pre-wait bytes on each adapter.
The existing
aliased_present_planes_preserve_the_host_texture_statetest also completed on both native Windows adapters with four workers selected. I haven't measured game frame times.Test-only commit on this PR head. To rerun it in PowerShell, choose
0,1, or4for the worker count and0(hardware) or1(WARP) for the adapter, then run:Imported from Marvisak/VIctory. Originally posted by AurumCarlos on 2026-11-26T12:30:47Z.
The check failure on
3b148e1is not from this change. The one failing test isframe::tests::waits_end_on_ticks_and_count_framesin victory-gpu (frame.rs:231): with 2 ms ticks, a zero-timeout poll right after a wait gotS_OKinstead ofTIMEOUTbecause the next tick had already passed on the runner. This PR doesn't touchframe.rs, the new commit only adds tests inresource.rs, and the previous head (768dcfa) passed. A re-run should clear it.Imported from Marvisak/VIctory. Originally posted by naorleizer on 2026-11-26T20:46:38Z.
Tried to get the NVIDIA numbers this PR is missing, on an 8 GB card. I couldn't — but the reason is worth recording, because it says where this switch can be measured at all.
Machine: RTX 3070 Ti 8 GB, i9-10850K, 32 GB RAM, driver 621.24, Windows 11, Series X dump 1.0.2.1, clean build of
bca3908(cargo build --release -p victory, rust 1.98.1),VICTORY_RELAY=2 VICTORY_CARD_BUFFERS_MIN_MB=1,VICTORY_DEFER_LISTSunset.Result: the tree never finishes loading, so there is no deferral number to report. Two attempts, both ended the same way:
DXGI_ERROR_DEVICE_REMOVEDon the following submission — exit0x887A0005.VICTORY_SHADER_LOOP_LIMIT=256added:host GPU device removed after a submission of 30 lists, same exit code, ~6 minutes in.Neither run reached gameplay: draws/frame stayed in single digits (4–13) the whole time, so my 4K-target card never got far enough to exercise list translation on a real frame.
Why this isn't a verdict on the PR: upstream's default path can't survive the first load on 8 GB here — that 15-second frame is the TDR watchdog's cue, and nothing in this tree caps the copy-back the 8 GB forks are built around. The forks (fxceless and friends) carry
COPY_PLAIN/BACK_OWN_VIEW/LATE_WAITetc.; upstream has none of those (VICTORY_*switch audit onbca3908: onlySHADER_LOOP_LIMITand yourDEFER_LISTSexist), so "upstream on 8 GB" is an untested configuration in general, not just for this PR.So for what it's worth from this rig: upstream
bca3908+ no deferral = device removed during load on an 8 GB NVIDIA card. Correctness is already covered by steven-seagull27's 5080/WARP fixture runs; what's still missing is a frame-time delta on a tree that can reach the world on this class of card.Happy to run a paired baseline/
DEFER_LISTS=4comparison — same binary, same cache, six minutes of world each, and I'll quote the PR's own "list calls translated by workers / title threads waited" line — if you point me at a branch where 8 GB survives the load (a fork branch carrying this change would do). I also have the fuller logs if any of the numbers above need drilling into.AI-assisted (GLM 5.3 via Hermes Agent) on my own machine, private research use; no game files, dumps or title memory shared.
Imported from Marvisak/VIctory. Originally posted by latentdev on 2026-11-26T22:26:29Z.
In-game numbers on an RTX 4090 (Windows): slower with deferral on this stack
Setup:
main63bcca4+ this PR atbca3908. It applied cleanly onto our NVIDIA stack: #439, #2121, #2312, #3102, #2821, #2178 and others, plus the views' and title-memory bitmaps in video memory.VICTORY_DEFER_LISTS.Medians of the frame-rate lines from 300 s into the world (180 s gives the same picture):
VICTORY_DEFER_LISTSWith 4 workers, the report line reads
list calls translated by workers: ~44,000/frame; title threads waited ~55 ms/frame for them. GPU and copy-back are unchanged. The extra time is all in the submissions' preparing, which roughly triples.I haven't profiled where it goes. My guess is workers contending with the submitting thread on the shared views and copy locks, or
ExecuteCommandListswaiting out the workers. Both runs kept written copies in system memory (VICTORY_GPU_UPLOAD_WRITES=0, which this PC needs to stay within memory), so the absolute GPU time is higher than usual. The switch was the only difference between them.The PR's tests pass here (
list::, includinglist::later::tests).Imported from Marvisak/VIctory. Originally posted by AurumCarlos on 2026-11-26T22:34:23Z.
Thanks both, these are the first in-game numbers for this switch.
@latentdev that's a clear result: with 4 workers, preparing goes from 130 to 392 ms while GPU and copy-back stay flat, so on your stack the deferral costs more than it saves. ~44,000 calls/frame with ~55 ms of waiting suggests the handoff per batch (queueing, waking a worker, the submitting thread waiting at execute) outweighs the translation it moves off the title threads, possibly made worse by the shared view/copy locks. I'll profile where the extra preparing time goes before asking for anything here. Until then the switch stays off by default, so nothing changes unless
VICTORY_DEFER_LISTSis set.@naorleizer thanks for trying and for writing down why it couldn't be measured on 8 GB; I don't have a fork branch that survives the load on that class of card, so I'd rather not send you on a detour. latentdev's run already answers the main question for now.
Imported from Marvisak/VIctory. Originally posted by AurumCarlos on 2026-11-26T23:14:55Z.
@latentdev following up on your 4090 numbers. Reading the code, the likeliest cause is lock contention during preparation:
descriptor::sync_heapsholdsSHADER_HEAPSfor the whole sync and takesVIEWSplus the heap'skept/madeper slot, which are the same locks the workers take for every draw-table entry they translate. Four workers doing nothing else make the single preparing thread queue behind them.eaf2a20tests that: workers now pause between batches whileHost::executeprepares a submission (aRwLockthey read per batch and the preparation writes).VICTORY_DEFER_PAUSE=0turns the pause off, so one build gives both sides. The frame line also reports how long submissions waited for the workers to pause.If you have time, the same prologue runs as before would answer it:
VICTORY_DEFER_LISTSunsetVICTORY_DEFER_LISTS=4VICTORY_DEFER_LISTS=4 VICTORY_DEFER_PAUSE=0Ideally with the diagnostics breakdown of preparing (
SyncHeapsUs,UploadsUs,PreludeUs), which would show where the extra time goes even if the pause doesn't fix it. The PR's tests pass with 0/1/4 workers and the pause on or off (Wine + vkd3d-proton on an RX 9070 XT here; no in-game run on my side).Imported from Marvisak/VIctory. Originally posted by latentdev on 2026-11-26T23:24:04Z.
@AurumCarlos thanks, the pause is a good test of that reading. I'll run
eaf2a20with the same three settings and the preparing breakdown (SyncHeapsUs,UploadsUs,PreludeUs).It won't be right away, though. The title runs close to this PC's memory limit and a first attempt was stopped when memory ran low, so I'll run them when the machine is free for it.
Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-27T00:28:28Z.
Review: CHANGES_REQUESTED
Tested
eaf2a208efa90f3bda519f5e88bd69ee3298c535across RTX 5090 and WARP (VICTORY_D3D12_DEBUG=1) with bothVICTORY_DEFER_LISTS=0andVICTORY_DEFER_LISTS=4(cargo xtask policy,cargo clippy --testsdefault &--features diagnostics,list::,waiting_for_another_queues_write, and--ignored overlapping_copies_across_worker_batches_survive_a_queue_waitall pass with 0 Debug Layer errors).The batching and pause design is clean, but three memory-safety issues in
crates/victory-gpu/src/list/later.rsneed to be fixed before merging:Cross-thread use-after-free when title COM objects are released before worker translation (
crates/victory-gpu/src/list/later.rs:235-253, 320-349, 452-455)Thispointers (pipeline,signature,heaps,dst,src,resource,heap,buffer) without retaining a COM reference, relying on the assumption that "D3D12 has them live until the list has run" (later.rs:236-237).ID3D12PipelineState/ID3D12CommandSignature/ID3D12DescriptorHeapimmediately after recording (beforeClose()orExecuteCommandLists— seedraw.rs:654andhost.rs:1008-1010), and transient resources can also be released before submission.log.open/ready, sopipeline::release/signature::release/descriptor::heap_release/resource::releaseimmediately freesBox<T>viaobject::release_boxed, and the worker then dereferences freed memory when draining the batch.AddRefon enqueue,Releaseafter the worker runs the call — e.g. via an RAII guard for non-nullThisarguments) for title objects captured by deferred closures.TOCTOU use-after-free on concurrent
Releaseof a deferring list (crates/victory-gpu/src/list/later.rs:367-372)refs == 2) and callReleaseconcurrently, both can observerefs(this) == 2, skipdrain(this), and let the secondsuper::list_release(this)drop the count1 -> 0and freeBox<List>while a worker thread is still dereferencingthis.drain(this)needs to run only after the atomic refcount decrement reaches0(beforeBox::from_rawfreesList), or unconditionally before decrementing.Undefined behavior in
copied()whencount == 0with a non-null misaligned/dangling pointer (crates/victory-gpu/src/list/later.rs:287-293)copied()checksitems.is_null()but passes(items, 0)straight tostd::slice::from_raw_parts. In D3D12, callers regularly passcount = 0(e.g.,NumRects = 0onClearRenderTargetView/ClearDepthStencilView/ClearUnorderedAccessView*orcount = 0onRSSetScissorRects/RSSetViewports) alongside a non-null unaligned or uninitialized pointer.copied::<u32>(1usize as *const u32, 0)aborts immediately in debug builds (0xc0000409) atcrates/victory-gpu/src/list/later.rs:292:19:unsafe precondition(s) violated: slice::from_raw_parts requires the pointer to be aligned and non-null...Some(Vec::new())whencount == 0(after checkingitems.is_null()) avoids callingfrom_raw_partson a zero-length pointer.Imported from Marvisak/VIctory. Originally posted by latentdev on 2026-11-27T01:47:55Z.
@AurumCarlos here are
eaf2a20's three settings. Setup: RTX 4090, our stack, warm shader cache,VICTORY_GPU_UPLOAD_WRITES=0(our low-memory setting), the BVH builder held, one ~7.5 min prologue run each. Values are medians of the world's frame lines from 20 s in (20–26 lines per run).VICTORY_DEFER_LISTS=4VICTORY_DEFER_LISTS=4 VICTORY_DEFER_PAUSE=0All in ms/frame. Workers translated about 38,000–44,000 calls/frame.
One caveat. Two of the runs (unset, and no pause) had stretches of 10 s frames. In those, the queue's safety valve released a
GREATER_EQUALvalue wait that hadn't held for 10 s (18 and 12 of them); those frames are left out above. The 4-worker run with the pause had none. A control run on our base without the PR lost the device about 95 s into the world, close to where the unset run's stalls began (88 s). So I don't think the stalls come from the PR, but with one run each I can't rule that out.Imported from Marvisak/VIctory. Originally posted by AurumCarlos on 2026-11-27T01:50:27Z.
@latentdev thanks for the careful runs, that settles it.
VICTORY_DEFER_LISTS=4is 1357 ms/frame against 788 unset, so translating off the title's threads costs more than it saves on this title.Unless I find why the workers are that much slower, I'm inclined to withdraw the PR rather than keep an opt-in that only slows things down. I'll confirm tomorrow; the option stays off by default meanwhile.
Imported from Marvisak/VIctory. Originally posted by AurumCarlos on 2026-11-27T07:09:09Z.
Closing this: it doesn't pay off. Thanks @latentdev, @naorleizer, @fuzeofficlal and @Dukestar69 for measuring, testing and reviewing it.
What the measurements showed (RTX 4090, prologue):
VICTORY_DEFER_LISTS=4is slower in every run, 1357 ms/frame against 788 with it off, and 1865 with the pause. The heap sync isn't the cost (59–71 ms in all three runs); the title threads waiting for the workers is, about 670–690 ms/frame. My best guess is that translation takes shared locks (heaps, views, tables) on nearly every call, so the workers mostly queue behind each other. Making this pay would first need translation with much less shared locking, which is a different change.99190adfixes the three memory-safety issues from the last review, so the branch is a sound starting point if anyone tries this again.Pull request closed