Skip to content

Cut the 17 s world frames with asynchronous submission and a copy queue - #2960

Open
ynnmuraii wants to merge 10 commits into
Marvisak:mainfrom
ynnmuraii:perf-async-submit
Open

ynnmuraii wants to merge 10 commits into
Marvisak:mainfrom
ynnmuraii:perf-async-submit

Conversation

@ynnmuraii

@ynnmuraii ynnmuraii commented Nov 26, 2026 •

Copy link
Copy Markdown

The world stalls for 17 s a frame here (RTX 4060 8 GB): the render thread waits inside submit and inside the fetch of what shaders wrote, so the intro cutscene never draws. With the three things below the same frames take 0.3-2 s and the prologue card renders.

Ported from fxceless/victory-fork, where they sit behind switches:

  • VICTORY_ASYNC_SUBMIT=1: submissions are prepared and sent by a thread of the layer. A profile in that fork had the render thread inside submit for 257 of 292 ms a frame.
  • VICTORY_QUEUE2=1: fetches of what shaders wrote go to a copy queue; the copy-back thread's wait fell from about 125 to 7 ms a frame there. Its queue2.rs comes over with the fix for a piece fetched beside a later flight: textures count their state moves so a fetch recorded before a later use is told from one recorded after it.
  • VICTORY_PIPELINE_WAIT_MS and VICTORY_PIPELINE_WORKERS already exist upstream; the numbers below use 24 and 4.

Measured on one machine (Ryzen 7 5700X, RTX 4060 8 GB, Windows 11 26200, Series X, 1080p, warm shader cache), same build, from the first-run screens into the intro cutscene:

switches frame at about 1 400 draws frame at about 2 500 draws prologue card
none 16.9 s 17 s not reached
async + queue2, no workers 17 s 17 s not reached
workers only 17 s 17 s not reached
async + queue2 + workers 0.5-1.0 s 0.3-2.0 s drawn

No device removal in any of the runs with the device layer's debug output on; the runs above are 10-15 minutes each. Above about 2 900 draws the frame still goes back to 8-17 s, so this does not make the title playable, it makes the cutscene visible. Halving neither the worker count nor doubling it (4 against 8 workers) moves those heavy frames; they are not worker-bound.

The three are opt-in; the last row was run as VICTORY_ASYNC_SUBMIT=1 VICTORY_QUEUE2=1 VICTORY_PIPELINE_WAIT_MS=24 VICTORY_PIPELINE_WORKERS=4.

Two caveats from the fork's own notes: the async path assumes the title leaves its memory alone between submit and preparation (it does there, but nothing enforces it), and the copy queue brings reads back with an up-lane bit this port does not add.

@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 tested against main (63bcca43) on Windows 11 with both hardware D3D12 (RTX 5090) and WARP (VICTORY_D3D12_DEBUG=1). While the Arc::try_unwrap / HostList::deferred() lifecycle in list::restart -> leave -> recycle is well-guarded, there are four blocking defects that currently break both VICTORY_QUEUE2=1 and VICTORY_ASYNC_SUBMIT=1 as well as CI lint:

  1. clippy -D warnings failure (crates/victory-gpu/src/list.rs:911-924):

    • The // SAFETY: comment sits outside if !queued {, so the unsafe block at list.rs:914 fails cargo clippy --locked -p victory-gpu --tests -- -D warnings with clippy::undocumented-unsafe-blocks.
  2. VICTORY_QUEUE2=1 silently fails on its first readback with zero bytes copied (reproduced on both RTX 5090 and WARP):

    • Lists::new (crates/victory-gpu/src/queue2.rs:98-105) calls CreateCommandList (which returns an open command list) without calling Close().
    • On the very first fetch_on_copy (queue2.rs:279), lists.reset() tries to reset the still-open list/allocator, fails immediately, calls broke("a list did not reset", e), and permanently sets BROKEN = true.
    • queue2::run (queue2.rs:394) then falls back to second.fetch_on_direct(&now), whose direct_lists were also created by Lists::new without Close(), so queue2.rs:313 fails identically and returns false.
    • Because list.rs:913 already skipped recording CopyTextureRegion on the original list, neither queue copies anything. With only VICTORY_QUEUE2=1 set, both resource::tests::a_texture_read_back_reaches_the_title_buffer and resource::tests::a_list_waiting_for_another_queues_write_reads_what_it_wrote fail 100% on RTX 5090 and WARP. Closing each freshly created command list inside Lists::new is needed before reset() can ever succeed.
  3. Deferred readback ordering hazard when the source texture is overwritten later in the same list/batch:

    • list.rs:900-924 omits the inline CopyTextureRegion and defers it to crate::queue2::run(&fetches) after the entire Host::execute batch (host.rs:1351).
    • If a later command in the same list or a later list in the same runs batch overwrites the source texture after the readback command, even the fetch_on_direct fallback (queue2.rs:307-347, 387-398) runs after the batch completes and therefore reads the post-overwrite texture contents rather than the state at the readback point.
  4. VICTORY_ASYNC_SUBMIT=1 synchronization contract breaks & queue::present state race:

    • With VICTORY_ASYNC_SUBMIT=1, queue::execute / queue::signal enqueue Work::Run / Work::Signal and return before ExecuteCommandLists / Signal have reached the host queue, while fence::completed, fence::set_event_on_completion, and subsequent readbacks do not call before_sync(). Under VICTORY_ASYNC_SUBMIT=1, queue::tests::queue_signals_complete_at_once (expects 7, observes 0), a_texture_read_back_reaches_the_title_buffer, and a_list_waiting_for_another_queues_write_reads_what_it_wrote all fail on both RTX 5090 and WARP.
    • In queue::present (queue.rs:733, 747), host_transition reads/mutates copy.state on the calling thread before host.present eventually calls before_sync() inside self.execute(&[&p.copy]) (host.rs:3506), racing with the background victory-submit thread touching copy.state inside HostList::prelude() (host.rs:1958, 1967). Calling crate::queue::before_sync() at the start of queue::present (before touching plane states) would close that race.

(Note: cargo xtask policy passed (192 files ok); default-off tests matched main's baseline on RTX 5090 and WARP.)

@ynnmuraii

Copy link
Copy Markdown
Author

Fixed 1 and 2 in 2e72cc9: the SAFETY comment moved inside the if, and Lists::new closes the list CreateCommandList hands back open, so the first reset can run; cargo clippy --locked -p victory-gpu --tests -- -D warnings is clean here now.

On 3 and 4: both are real and I have no fix yet. For 3 the simplest reading is that the deferred copy is only safe while nothing later in the same batch writes the source; a version that records the copy inline when the texture is written again later in the batch would need the list to know what it has already written. For 4 the reports are consistent with what the reviewer says — the ordering contract is not enforced, so the numbers in the body come partly from a path that can race.

Measured after 1 and 2 on the RTX 4060: the reset path no longer breaks, no device removal in 12 minutes, but a frame at 3 135 draws still takes 16.7 s. The 0.5-2 s numbers are at 1 100-2 500 draws only.

@ynnmuraii

Copy link
Copy Markdown
Author

3 addressed in the latest push: Host::defer_fetch now refuses a second read back of the same texture and subresource, so the duplicate is recorded in the list and only the first stays deferred. That covers the repeated fetch; the harder half of 3 remains — a later write to the source in the same batch still lands in the deferred copy, and the list would have to know what it has already written to catch it.

4 is still open. Current state: clippy clean, reset path fixed, no device removal over 15 minutes, frame time unchanged at 16.7 s on a 2 941-draw frame.

@ynnmuraii

Copy link
Copy Markdown
Author

4 addressed too: present now calls queue::before_sync() before it reads or moves any plane's state, so the submit thread has finished the lists that touch those textures before this thread mutates copy.state. Nothing is drained from the submit thread itself, so no deadlock.

That leaves the intentional part of 4 — execute/signal returning before the card has run — which is what the switch is for; the drain in present and the existing one in Host::execute are the fences around it.

Frame time again unchanged at 16.7 s on 3 289 draws; no device removal in any of the runs after the four fixes.

@kotekompotek kotekompotek 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.

Verified the ordering invariants: the worker drains the FIFO strictly (signals queued behind submissions, pump_held_if_free(Worker) runs before pending-- so released held work joins the line before a later submission can pass it), before_sync drains the submitter before synchronous submissions and before present's state moves, and SUBMITTING_HERE guards the worker's own host.execute. Fetch dedup by (copy, subresource) is right — a second read-back would snapshot the end-of-batch state. The documented assumption (title memory not rewritten between submit and prepare) is sound vs console timing. Both switches default off, so the hot path is unchanged.

omp and others added 3 commits November 26, 2026 07:12
After its watchdog fires the title writes a marker through a null pointer
(`mov dword ptr [0], 0x4d8bd421`): the console maps a null page, the PC does
not, and no allocator places memory at zero. A 64 KiB page is reserved at
0x10000 and every such instruction in the mapped image has its absolute
operand rewritten to it, keeping the original offset (some sites use [4]).
The page is made writable for the patch, flushed, and restored.

Measured: runs stop dying at the point every earlier one did (~150 s in).
A watchdog path in the title ends with an unconditional `int3` after
writing its breadcrumb; on a console its own handler catches it. With none
attached, the breakpoint killed the run. Only a real 0xCC at the reported
address is stepped, so debugger breakpoints are untouched.

Measured: a run that died on the trap at ~150 s passes it and continues.
@ynnmuraii

Copy link
Copy Markdown
Author

Added the fork's relay on top (the flights thread, 1 406 lines), with the two crash fixes it needs to survive here at all: redirect_null_write for the title's null breadcrumb and the int3 step-over. Without them the port dies in 2-3 minutes with 0xC0000005 on a write to 0, then 0x80000003.

With both: 10 minutes, no device removal, no exception. Frame time on the RTX 4060 is unchanged though — 16 s on 3 157 draws, one outlier at 33 s. So the relay is a working option here, not a gain; on this card the wall is the per-draw preparation, not the submit or the copy-back path.

@ynnmuraii

Copy link
Copy Markdown
Author

Ported the fork's framelog too (9cd2b104, diagnostics builds only, VICTORY_FRAME_LOG=0 turns the lines off) so the long frames can be attributed instead of guessed at. First run with it on the RTX 4060:

long frame 3370 at 156.5 s: 16948 ms | render thread: ran 31 ms; in the present call 16897
  (lines and settling 16842, copy and show 39) | submit thread: preparing 28.8, waiting for the card 16815.4
  | copies 21.6 ms 57.7 MB | pipelines: 0 made, 0 loaded | 2569 draws

So the heavy frames are the card's own time, not our preparation and not pipeline creation: 0 pipelines made, 16.8 s of waiting for the card, 57 MB of copies. Lighter frames of the same scene settle at 1.3 s. My earlier "CPU preparation" guess in the comments above was wrong — that comes from the fork's own overlay on its own build.

The counters fx has that our tree cannot source (card clock, per-call list timings, remaps) are noted in the commit; the copy-back counter covers the relay landing path here.

@ynnmuraii

Copy link
Copy Markdown
Author

Baseline with the same frame log and no switches, for comparison: 16904 ms | render thread: ran 297 ms; in submit calls 16871.5. With the switches the same 16 s sits in waiting for the card on our submit thread (16815) instead, so the title's thread is out of it. Total frame time is unchanged because the present then waits for the presented frame anyway — which is what both reviewers said in other words.

@walterwallaceson

Copy link
Copy Markdown

RTX 3070 8 GB, Windows 10: with the switches on, the world runs at about half the speed it does with them off. That held on two heads, a480378 and 9cd2b10, two runs each. This card never had the 17 s frames, so there was nothing for the switches to win here.

Setup: RTX 3070 8 GB, driver 621.14, Windows 10 22H2 (19045). main 63bcca4 + #463 (Agility runtime, needed on Windows 10) + #2626, then + this PR. Diag build, Enter scripted at 17, 30 and 60 s, nothing skipped, 15-minute runs. "On" means VICTORY_ASYNC_SUBMIT=1 VICTORY_QUEUE2=1 VICTORY_PIPELINE_WAIT_MS=24 VICTORY_PIPELINE_WORKERS=4, as in your table. "Off" is the same binary with none of them set.

a480378 on a480378 off 9cd2b10 on 9cd2b10 off
end 15 min, no removal 15 min, no removal 15 min, no removal 15 min, no removal
world speed, last ~4 min 0.7 fps 1.5 fps 0.8 fps 1.8 fps
copies/frame, end 132 ms 86 ms 171 ms 37 ms
indirect dispatches dropped/frame, end 80 315 68 226
slow submissions 3, slowest 893 ms 2, slowest 356 ms 2, slowest 313 ms 2, slowest 583 ms

Without this PR, the same stack ran at 1.8 fps, so with the switches off the PR costs nothing here. The switches also log as on (VICTORY_ASYNC_SUBMIT=1, VICTORY_QUEUE2=1), and no run crashed or removed the device.

Since all four settings were set together, I can't yet say which one costs the speed. A natural next split is the two switches without the workers and wait. Fewer indirect dispatches dropped with them on could be a side effect of fewer frames, not a fix.

One unrelated thing: the branch commits victory.toml.off, which looks like a local config. It fails xtask's policy::tests::repository_passes_policy (file type not on the allowlist). I dropped it locally to test.

Not tested: a second run of each cell; the frame log for the steady state (on this card it records almost no long frames after the first minutes).

No game data attached. I'm testing with Claude's help, and Claude drafted this comment.

Copy link
Copy Markdown
Owner

Automated triage for the maintainers.

Proposed close. The PR adds about 2,900 lines of opt-in submission/copy-queue/relay code ported from another fork. By its own measurements in the comments, it does not help. The heavy frames stay at about 16.7 s with the switches on, and the frame log puts that time in waiting for the GPU, not in our submit path. An independent RTX 3070 run found the world at roughly half speed with the switches on (0.8 vs 1.8 fps). The deferred copy also has a known unfixed race: a later write to the source in the same batch lands in the deferred copy (Host::defer_fetch in host.rs).

It also bundles unrelated, risky changes:

  • handler in victory-loader/src/crash.rs now steps over every int3 in title code. That hides the title's own watchdog stops instead of explaining them, and breakpoint_tests asserts nothing about it.
  • A null-write redirect, framelog hooks in victory-hle dstorage, and a committed local victory.toml.off holding personal paths. That file is not on the policy allowlist and fails repository_passes_policy.

CI: failing.

What would change this: a small PR with one change that a frame log shows lowering frame time on main, without the crash-handler, null-write and config changes. This PR will be closed in 48 hours unless the author pushes a change or a maintainer removes the triage: proposed close label.


Generated by Claude Code

@walterwallaceson

Copy link
Copy Markdown

Correction to my comment above: the "switches on halve the speed" result isn't solid. Since posting it, I found that frame rate on this RTX 3070 swings between roughly 0.7 and 1.8 fps from run to run of the same build. A control run of an earlier fast tree came out at 0.93 fps. My on/off runs alternated (on, off, on, off), and the "on" runs may simply have landed on slow turns. Please treat that table as not showing an effect either way until I can measure the run-to-run spread here. The stray victory.toml.off note still applies.

I'm testing with Claude's help, and Claude drafted this comment.

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