Skip to content

perf(gpu): async submission (VICTORY_ASYNC_SUBMIT=1): measure it, queue page mappings, present without a second drain, and prepare the next submission during a quiet flight (VICTORY_PREP_AHEAD=1) - #584

Merged
pragyaangaur merged 20 commits into
pragyaangaur:mainfrom
Nick233x:claude/perf-async-submit-17244c
Nov 28, 2026

Conversation

@Nick233x

@Nick233x Nick233x commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Why

In the open world (Series 1.0.2.1, M5 Pro, macOS 26.6.2, D3DMetal from GPTK 4.0 beta 3, with #570 applied), VIctory's frame report gives 431 ms/frame: 167 preparing + 218 on the GPU + ~20 copying back. The same report says threads held by host GPU work … title submissions 0.0 + 358.6, so the title's own threads spend ~359 ms of each frame inside our host submission work. A Metal System Trace over the same stretch shows the GPU busy only 41% of the time.

The title doesn't have to wait there. #447 already added VICTORY_ASYNC_SUBMIT=1, opt-in. With it, ExecuteCommandLists, Signal and Wait only queue their work. The victory-submit thread then prepares the work and submits it, joining up to 64 ready lists into one host submission, and victory-flight lands each submission. The title's fences move only when the work has landed. But under that switch the frame report had no numbers: the unlocked pump and the drain at PresentX weren't timed, so the "threads held" line read ~0 whatever happened. That meant the switch couldn't be compared with the default. It has never been tried in these runs.

What

  • Title threads, measured under VICTORY_ASYNC_SUBMIT=1 / VICTORY_FLIGHT1=1 (queue.rs). A title call now counts the time it spends queueing. A thread that waits for the worker to drain counts the drain lock it waits for and the run after it: PresentX as presents, and our own read-backs, captures and page mappings (before_sync, CopyPageMappings) under a new bucket, our syncs. The Flight-1-only pump that runs on title threads is counted too. The line is the same as before, so it compares directly with a default run:
    threads held by host GPU work per frame, ms waiting for the queues' lock + running it: title submissions a + b, …, presents c + d, our syncs e + f, our timer …
  • Submit worker and host GPU idle time (host/flight.rs). Each flight now records three times: waiting for the last flight to land (its remaining GPU time plus its copy-back), preparing, and launching (the host ExecuteCommandLists + Signal). It also records how long the host GPU sat idle before the flight, split into "nothing to run" (the worker came to a free GPU) and "preparing" (the work was there, but Wait-Before-Prep held it). Synchronous submissions mark the GPU free too. A new frame-report line shows this:
    host submissions in flight per frame: n; ms per frame: submit worker busy w (waiting for the last to land l, preparing p, launching x); host GPU idle before them i (nothing to run u, preparing q)
  • Docs (docs/macos.md): the two switches, what they overlap and what they don't, and how to read both lines.

The default doesn't change: async stays opt-in until a run shows it holds up on this title.

Expected effect (with VICTORY_ASYNC_SUBMIT=1)

  • title submissions should drop from ~359 ms to around a millisecond (queueing only). The title then waits at PresentX instead (presents), but only for whatever host work its own CPU time didn't overlap.
  • The floor stays the worker's serial chain: land N-1 → prepare N → launch N, now ~405 ms (167 + 218 + 20). One host submission is in flight at a time, and the next is prepared only after the last has landed. Host copies of title memory are single system-memory buffers that the GPU reads in place, and shadow.rs assumes one submission at a time (snapshots, pending ranges, copy-back merge), so preparing N+1 during N would overwrite what N reads. From overlap alone, expect up to about the title's own CPU time per frame, ~431 → ~405 ms (≈ +6%, 2.3 → ~2.45 fps).
  • On top of that, the worker joins more lists per host submission, because work piles up while a flight runs. That cuts per-submission costs. The held-work line's pieces= gives the count: 13 per frame today. perf(gpu): opt-in Wait-Before-Prep Flight-1 GPU overlap and async submit worker #447 measured 2.7× on an M5 Max on an older tree, but I wouldn't count on that here.
  • The new line's "host GPU idle … preparing" figure is how much a prepare-during-flight design (versioned shadow ranges) could still win. "Nothing to run" is how much an async PresentX could win, because the title would start frame N+1 before N lands.

Ordering and fences

These are unchanged from #447 and now covered by tests. The title's call returns once its work is queued. The fence moves only after the work's bottom-of-pipe writes. A Signal behind running work stays behind it: holds_work counts running queues, and the flight is marked active before finish_running. Host copies of title memory are refreshed when the worker prepares the submission, not when the title calls, which is how the console's GPU reads unified memory too (at execution, not at submission).

Tests

  • host::flight::tests::async_submit_runs_host_work_off_the_title_thread_and_signals_after_it (GPU). A list waits for a memory value. ExecuteCommandLists + Signal return with the fence still at 0 and nothing run, and the list can be reset at once. Once the value is written, the fence reaches 1, the BoP write is there, and the work's after ran on a thread other than the title's. The flight is counted.
  • host::flight::tests::async_submit_queue_wait_completes_after_the_signalling_queue_work (GPU). Queue B Waits for queue A's fence, and A's work is held behind a memory value. Neither fence moves before the value is written. Whenever B's fence reads as reached, A's fence and both queues' writes are already visible. Note: the worker may join both queues into one host submission. Inside a landing, all after hooks run before all BoP writes, so an after hook of B can't see A's BoP write. Nothing title-visible depends on that, and the sync pump coalesces the same way.
  • host::flight::tests::idle_time_is_split_at_when_the_work_was_there.
  • tools/macos/test-wine.sh -p victory-gpu (Wine + D3DMetal, M5 Pro), with all three commits and the game closed: 582 passed, 0 failed (19 ignored). (With the game running, one process for the whole suite ran out of Metal memory and failed in a cascade; the base commit failed the same way.)
  • cargo clippy --workspace --all-targets --target x86_64-pc-windows-gnullvm -- -D warnings, with and without --all-features: clean. cargo xtask policy: ok.

It merges cleanly with #556 and #570 and doesn't depend on them.

I haven't run the game with this change.

Re-test

Same place in the open world, two runs: the default, then VICTORY_ASYNC_SUBMIT=1. Compare ms/frame, the "threads held" line (title submissions → ~0, presents takes what's left), pieces= in the held-work line, and the new "host submissions in flight" line. If the async run hangs or shows corruption, VICTORY_FLIGHT1=0 keeps the worker but waits for each submission on it, which separates the two halves.

Follow-up: the first A/B, and the drains it showed

Same build plus this PR (63f2c10), same spot in the open world. Medians over frames with more than 2,500 draws:

fps ms/frame preparing (copies, textures) on the GPU title threads held
default 2.9 344 101 (47, 8) 217 submissions 296–336
VICTORY_ASYNC_SUBMIT=1 2.3 428 119 (44, 28) 216 submissions 0.1; presents 24–155 + 25–134; our syncs 2–124 + 0–110

Flight line, async: submit worker busy 292 (waiting for the last to land 145, preparing 103, launching 1); host GPU idle before them 261 (nothing to run 145, preparing 116).

So the title's threads were freed from submitting, but two drains took the time back. While the title's thread waited in them, it submitted nothing, and the GPU sat idle with nothing to run.

The only production callers of a synchronous Host::execute are PresentX's copy and the diagnostics captures, so the drains are these:

  • CopyPageMappingsBatchX drained all queued work and the flight in the air, on the title's thread. On the console the batch is a queue operation, and now it's one here too. Under VICTORY_ASYNC_SUBMIT=1 / VICTORY_FLIGHT1=1, while its queue holds work or a host submission is in flight, the batch becomes a new hold::Item::Action and the title's thread goes on. An action ends its queue's part of a coalesced piece; other queues still join. The submit worker lands the flight (the next preparation has to wait for that anyway), maps the pages, and only then prepares that queue's later work. With nothing held or in flight, it maps at once, as before. The mapper (xmem::map_alias) uses global locks only and no thread-locals, so running it on victory-submit is fine. It also no longer forgets host copies while a flight reads them.
  • PresentX drained twice. It drained once in queue::present, then again for its own swap-chain copy (Host::execute → before_sync). The second drain waited for whatever other title threads had queued since: the next frame's work. In the queued modes the copy now goes through execute_sync, so it waits only for the flight in the air and runs ahead of the work queued after PresentX, as on the console. In sync mode it's still Host::execute, and nothing changes.
  • New frame-report line to check what's left: queued host work drained per frame: by presents p; page mappings done at once a, queued behind their queue q; by our own host work s (file:line n× t ms, …). Host::execute and execute_with_barriers are #[track_caller], so a remaining drain names the line that called it.

The default (synchronous) path doesn't change: mappings still happen at once, PresentX still calls Host::execute, and the sync pumps run an action in place (none is queued in sync mode).

Expected effect (async)

  • our syncs should drop to about 0, leaving what PresentX must wait for. Waiting for the flight in the air is inherent to the copy, because its preparation can only run once that flight has landed.
  • nothing to run should drop by roughly the time the title spent blocked in mappings. The title keeps feeding the worker through its streaming.
  • The floor is still the worker's chain: preparing + GPU + landing, about 320–335 ms here. So expect async to land somewhere between the 428 ms above and roughly that floor, perhaps slightly ahead of the default's 344, but not far ahead.
  • Not explained yet: async prepares more textures (28 vs 8 ms). The new line will show whether the remaining drains line up with it.

Preparing N+1 while N is in flight: assessed, not done

prepare_submission writes state that a flying submission reads or will hand back:

  1. Shadow refresh (run_deferred → Shadow::update*): CPU writes into the same system-memory host copies the GPU reads in place. It takes the one snapshot per copy that copy-back diffs against, and clears pending, assuming "one submission runs at a time".
  2. Data N hands to N+1: N+1 must read title memory after N's copy-back for the ranges N's shaders write. Before landing, those bytes are stale.
  3. descriptor::sync_heaps: rewrites slots of the shader-visible host heaps that N's shaders read. D3D12 doesn't allow changing descriptors that in-flight work uses. Double-buffering them means a second set of million-slot heaps, whose creation needs quiet() (D3DMetal issue GPU tests that read back shader writes fail in about a third of runs on an M5 Max, never with one test thread #31).
  4. Per-submission singletons: the Mark list, upload staging released at landing, and texture recycling.

Items 1–3 together mean versioned shadow ranges (copy-on-write of the ranges in flight, with N's written ranges excluded from N+1's refresh until landing) plus double-buffered host view heaps. That's a redesign of shadow.rs and descriptor/host, and it needs runs of the game to check. It isn't in this PR. The flight line's host GPU idle … preparing (~116 ms/frame in the A/B) is the most it could win.

Tests added

  • hold::tests::an_action_runs_between_its_queues_work: both sync pumps run the work before the action, then the action, then the work after it. A signal before the action is delivered before it.
  • hold::tests::take_ready_hands_out_an_action_after_its_queues_work: per queue and coalesced. The action comes out only after its queue's piece was handed over, and never while that queue is running. Another queue's work still joins (coalesced) or comes out on its own (per queue). The work after the action comes next.
  • host::flight::tests::async_submit_queues_page_mappings_in_their_queues_order (GPU): under async, a batch behind held work returns at once with nothing mapped. It's mapped once the earlier work's fence reached 1, and before the later work's fence reaches 2.

Re-test

Same spot and command, VICTORY_ASYNC_SUBMIT=1, against the default. Look at:

  • ms/frame;
  • our syncs and presents in the "threads held" line;
  • the new "queued host work drained" line: mappings should show up as queued, and any our own host work entries name their lines;
  • nothing to run vs preparing in the flight line.

If the async run shows streaming corruption (missing or wrong texture pages), the queued mappings are the suspect. VICTORY_FLIGHT1=1 alone (without the worker) runs the same queue order on the title's thread, which narrows it down.

Follow-up 2: prepare N+1 while N is in flight (VICTORY_PREP_AHEAD=1, opt-in)

Second A/B (main d1609d9 + this PR at 92f47ab): async 2.8 fps / 358 ms against the default's 2.3 fps / 432 ms. our syncs went from 124–182 ms to 0. What's left in the flight line is host GPU idle … preparing ≈ 150 ms/frame: the next submission's work is queued, but Wait-Before-Prep lands the flight in the air before preparing it.

What it does

With VICTORY_PREP_AHEAD=1 (on top of Flight-1), the submit worker prepares the next host submission while the one before is still on the GPU, and launches it behind that one. At most two submissions are in flight. This happens only when the flight in the air is quiet at landing (HostList::quiet_at_landing): none of it goes back to title memory (no ranges shaders write, no write marks, no texture read-backs, no after work, no bottom-of-pipe writes), and it had no uploads. Its landing then changes nothing the preparation reads.

During that preparation, host copies are refreshed in place while the flight's shaders read them. This is safe for the same reason it's safe on the console: the title doesn't change memory that submitted work reads until that work's fence moves, which here is at its landing. So the refresh rewrites what the flight reads with the same bytes.

Some steps of the preparation need the GPU done with the flight first. Those land it first (flight::land_ahead, on the preparing thread, under Host::submitted), and each one is counted:

Lands the flight first Why
the frame's eviction pass it gives up host objects
a view remake (take_remake) it rewrites views
the shared UAV barrier lists one submission runs them at a time
a heap fill (Host::quiet) D3DMetal issue #31
title copies (Step::Copy/Move) they're done on the CPU while preparing; the console does them on the GPU after the flight
the new submission's write marks (before launch) every landing takes the marks bitmaps whole, so the older flight's landing would take the newer one's marks

To make the last row reliable, lists now record when they bind the write bitmaps (HostList::note_marks in draw.rs), instead of relying on note_unknown_gpu_write alone.

Plumbing

  • Flights: FlightState holds a queue of flights (at most two), with the title's signals attached per flight and delivered in order.
  • Landing: a landing records its own flight's fence value (SUBMISSIONS_DONE, retirement), not the newest submission's.
  • Marks: an even fence value ends with a second Mark, so arming one flight's mark never changes the other's. Native splits (shared split marks) stay together while behind a flight.
  • Device loss: if a landing finds its flight lost, it waits for the newer flight first, since the probe submits behind it.
  • Unchanged paths: the default path, and VICTORY_ASYNC_SUBMIT=1 without the switch, keep Wait-Before-Prep unchanged: one mark, the same splits, and prepare_submission only changes the type of its counter argument.

Frame line

host submissions prepared while the last was in flight per frame: p, launched while it still was l; landed it first for: not quiet at landing a, eviction b, view remake c, barrier lists d, heap fill e, title copies f, marks of the new one g, no second mark h

Expected effect

host GPU idle … preparing (~150 ms/frame) should go down by the preparation time of the submissions that are launched behind a flight (l above). How large l is depends on how many of this title's submissions are quiet at landing. Compute passes that write through views (culling, the BVH builder) aren't; render passes that only draw into render targets are. I can't predict the split without a run. If l turns out small, the line says which reason dominates. That's the next thing to unlock:

  • "not quiet at landing": needs per-flight write-mark bitmaps (shaders choosing a bitmap per flight), plus refreshes that skip ranges a flight in the air may write.
  • "marks of the new one": needs the same per-flight bitmaps.

Not done, and why

  • Versioned or double-buffered host copies in general. The marks bitmaps are global and bound in fixed heap slots, so preparing behind a flight that writes through views can't be made safe without changing the shader translation. A flight with copy-back also has to land before the next refresh, which must read the bytes the GPU wrote. The quiet-at-landing gate needs no extra copies and no extra memory.
  • VICTORY_HEAP_WORKER=1 already stages heap-indexed textures on its own thread, so textures are prepared ahead without this switch. It's worth trying together with this one.

Tests

  • prep_ahead_launches_behind_a_quiet_flight_and_lands_in_order (GPU). Landing is paused in the test, so the second submission is launched behind the first: two in flight. Both results are right, the newer flight's refresh sees the title's write, and its fence never shows before the older one's.
  • prep_ahead_lands_a_flight_with_copy_back_before_preparing (GPU), the overwrite hazard. The flight in the air writes a host copy that goes back at landing, and the next submission refreshes that copy. It's prepared only after the flight landed, so the GPU's write reaches title memory and the refresh reads it. Mutation check: with the gate forced open, the GPU's write is lost (title memory 0 instead of 0xCAFEBABE).
  • prep_ahead_lands_before_launching_a_flight_that_marks_writes, prep_ahead_lands_before_a_title_copy: the new submission is prepared behind the flight, which then lands before the marked submission launches or before the title copy runs.
  • tools/macos/test-wine.sh -p victory-gpu, after merging main (d1609d9): 601 passed, 0 failed.
  • Whole suite under VICTORY_ASYNC_SUBMIT=1 VICTORY_PREP_AHEAD=1: the same 3 failures as under VICTORY_ASYNC_SUBMIT=1 alone. They're tests that read GPU results synchronously right after submitting (list::…allocate_upload_survives_early_reset…, resource::…a_list_waiting_for_another_queues_write…, resource::…a_texture_read_back_reaches_the_title_buffer). Prep-ahead adds no failure.
  • Clippy (--workspace --all-targets, with and without --all-features, Windows target): clean. cargo xtask policy: ok.

Re-test

Same spot, three runs:

  1. VICTORY_ASYNC_SUBMIT=1 (the B2 baseline);
  2. VICTORY_ASYNC_SUBMIT=1 VICTORY_PREP_AHEAD=1;
  3. optionally run 2 plus VICTORY_HEAP_WORKER=1.

Compare ms/frame, the flight line's host GPU idle … preparing, and the new "prepared while the last was in flight" line (l, and the reasons). If anything looks wrong on screen with prep-ahead but not without (flicker, stale geometry, wrong streamed textures), that points at a title write into memory a submitted pass reads, and the first suspect is the in-place refresh.

Follow-up 3: a probe for a range-level rule (measurement only)

Run C (VICTORY_ASYNC_SUBMIT=1 VICTORY_PREP_AHEAD=1) showed 2.8 fps / 360 ms, unchanged from async alone. 6–8 of ~10 submissions per frame were "not quiet at landing", so almost nothing was prepared ahead. Before replacing the whole-flight rule with a range-level one, commit 6086751 measures the ceiling. It changes nothing that runs.

With VICTORY_PREP_AHEAD=1, every landing records two things:

  • Why its flight was not quiet: declared ranges, write marks, work after it, bottom-of-pipe writes, read-backs, table textures, uploads.
  • What it wrote back: its declared and bottom-of-pipe ranges, and the pages its shaders marked.

The preparation right after that landing records the host-copy ranges its refreshes actually rewrite (Batch::add, written copies only) and compares them against the flight's writes. New frame line:

prep-ahead probe per frame: flights not quiet at landing, for declared ranges a, write marks b, work after c, bottom-of-pipe writes d, read-backs e, table textures f, uploads g; preparations right behind one n: behind one not quiet only for ranges/marks/BoP r; refreshing nothing it declared k, nothing it wrote w, nothing a view write reaches v; could go ahead by ranges C (if marks were known ahead O)
  • C is the ceiling for a range rule that can be built now. It counts preparations behind a flight that was not quiet only because of declared ranges, marks or bottom-of-pipe writes, and that refresh none of the flight's declared ranges. If the flight marks its writes, they must also refresh no host copy that any view write can reach (the copies owning mark bits). That's the only bound on marked writes available before the flight lands.
  • O is the optimistic version: it assumes the pages a flight marks were known before it lands.
  • If C stays near 0 and the dominant reason is write marks, a range rule isn't worth building. The next step would then be per-flight mark bitmaps chosen by the shaders, which needs shader-translation changes. If C is a large share of ~10 per frame, the range rule is next, behind the same switch.

Test changes

  • prep_ahead_probe_counts_overlapping_and_disjoint_refreshes (GPU): behind a flight with a declared shader-written range, a refresh of another host copy is counted as able to go ahead, and a refresh of that range is not. overlaps_finds_shared_bytes_only checks the range sweep.
  • The prep-ahead tests now retry their scenario, up to 20 times, until it takes the path under test. In the full suite, another test's Host::quiet (a heap made on its thread) lands every flight in the air, which is correct. Each attempt also keeps its title memory allocated: freed test memory left its host copy registered, so the next attempt found that copy at the same address, and the copy-back saw no change.
  • Full Wine suite: 603 passed, 0 failed, three runs in a row. Clippy clean with and without --all-features; cargo xtask policy ok.

Re-test

One run as in C (VICTORY_ASYNC_SUBMIT=1 VICTORY_PREP_AHEAD=1), then read the new "prep-ahead probe" line. Behaviour is unchanged from C, so ms/frame should match.

Follow-up 4: host preparing time, and the range rule ready for A/B

None of this changes the default path or VICTORY_PREP_AHEAD=1.

4837ee3: preparing split by phase. A new frame-report line: host preparing per frame by phase, ms: evicting, heap views synced, uploads, list steps, preludes, refreshes, landing early, other.

  • Of the ~100 ms preparing a frame, the existing line names only copies (~47) and textures (8–28). The new line names the rest, so the next run shows which part to make faster.
  • The cost is one clock read per phase and list.
  • "preparing" now also includes the hang note made after it. That time was on the submit worker's chain but was not counted anywhere.

b817920: the hang note no longer copies pipeline names.

  • Every submission used to copy and format the names of all pipelines its lists bind, for a log line written only after an 8 s hang. Under Wine that cost 0.2 ms per submission at 100 pipelines a list, and 0.6 ms at 300. Benchmark: host::waiting_on_tests::waiting_on_takes_the_sets, ignored by default, run with --release --include-ignored.
  • Now the lists' sets are shared (Arc), and names are built only for the hang line.
  • Expected gain: ~1–6 ms/frame of submit-worker time at ~10 submissions a frame.

e0e0242: VICTORY_PREP_AHEAD=ranges, the range rule, off by default. It also prepares behind a flight that is not quiet, when what its landing writes is known before it runs:

  • declared shader ranges, taken as whole 64 KiB pages;
  • write marks, bounded by every copy that owns mark bits;
  • bottom-of-pipe writes outside host copies.

The preparation lands that flight first only when it would do one of these (Early::Overlap / Early::CpuRead):

  • refresh those bytes;
  • declare shader writes in a copy the flight writes;
  • fill from those bytes;
  • run host work.

A flight with work after it, read-backs, table textures or uploads lands first, as with =1. It matches the probe's "could go ahead by ranges" (C) except that fills and host work are also checked, which the probe doesn't count.

Shortcut: a texture over memory the flight's shaders write can be brought up one submission late.

Tests:

  • writes_keep_declared_pages_bottom_of_pipe_bytes_and_whole_copies;
  • prep_ranges_launches_behind_a_flight_writing_other_memory (two in flight; the write goes back; the refresh reads the title's bytes);
  • prep_ranges_lands_before_refreshing_what_the_flight_writes. This is the hazard test, and it fails with the before_refresh check removed.

A/B after the probe run:

  • Same spot, VICTORY_ASYNC_SUBMIT=1 VICTORY_PREP_AHEAD=ranges against =1.
  • Read "behind one not quiet by ranges" (how often the rule applied) and "landed it first for what it writes / CPU reads".
  • Expected: the flight line's host GPU idle … preparing shrinks by up to C/(submissions a frame) of itself, plus the hang-note saving. If C comes out near 0, =ranges will only show "landed it first" counts, and per-flight mark bitmaps are the next step.

Suite: 604 passed, 0 failed (two runs) after the first two commits; 607 passed, 0 failed (two runs) after the range rule. One of five runs hit a race in host::marks::work_goes_on_on_a_new_queue: another test's flight moves submitted between execute and the check. It passes alone and in the reruns. Clippy is clean with and without --all-features.

Follow-up 5: the probe result, and the ~180 MB of preparing copies

Run D (6086751): "could go ahead by ranges 0.0 (0.1 if marks were known ahead)" out of 10.3 preparations. Almost every flight is held by write marks, BoP writes, table textures and work after it, all at once. So VICTORY_PREP_AHEAD=ranges (e0e0242) stays off and won't be developed further. For the same reason, removing the table-textures blocker on its own would unblock nothing.

d16fabf: what the copies are, and VICTORY_SAME_COPIES.

  • New line host preparing copies per frame. It splits the copy MB/ms into three kinds: title copies between buffers, fills of upload memory, and refreshes of host copies (only changed 64 KiB pages, with fault tracking on). Up to now the ~180 MB had no breakdown.
  • VICTORY_SAME_COPIES=probe compares each title copy before running it: source against destination, and against every host copy of the destination. It counts the copies whose bytes are already in place. The copies still run.
  • =skip leaves those copies out. That saves the write and the refreshes it would make due for the destination's host copies (whole pages).
  • If a host copy holds other bytes (shader writes kept only there), the copy runs, as on the console.
  • Cost under Wine, warm memory (host::batch::same_copy_costs): comparing takes 4–5× a copy (8.4 µs vs 1.9 µs per 64 KiB). So skip only pays off if most copies are already in place and the saved refreshes outweigh the extra cost. Run probe first.
  • Tests:
    • copy in place is left out, and with probe it still runs;
    • a copy that changes something runs with skip too;
    • a copy whose host copy holds different bytes runs. Removing the host-copy check makes this test fail.

234f010: host::marks::work_goes_on_on_a_new_queue now runs under the mode lock and synchronously. It failed in about one full-suite run in four, because another test had switched Flight-1 on.

Next run: VICTORY_ASYNC_SUBMIT=1 VICTORY_SAME_COPIES=probe. Read the new line, plus host preparing per frame by phase:

  • If title copies are most of the MB and "already in place" is high, try =skip.
  • If refreshes dominate, the lever is the refresh path: page granularity, and what the title writes each frame. Title copies are not.
  • If fills dominate, the next step is a GPU blit from host copies instead of CPU fills.

Suite: 610 passed, 0 failed, three runs. Clippy clean, policy ok.

Follow-up 6: heap sync and refreshes, measured before changing them

Run E (VICTORY_SAME_COPIES=skip): title copies are 29 a frame (0.1 MB), so the ~174 MB is refreshes. Preparing by phase: heap views synced 41–56 ms, uploads 14–40, list steps 20–25, refreshes 18–20.

c4b3402: offline benchmarks, and three new lines for what they couldn't settle. It changes no behaviour, so it needs no switch.

  • Heap sync:
    • An unchanged 64 KiB heap page (2,048 slots) is checked in 1.8 µs. Comparing it with its last bytes takes 2.2 µs (bench_unchanged_page_scan_vs_compare).
    • The sync already reads only pages the title wrote, and unchanged records cost nothing further. So skipping by page bytes, or by generation, can't win.
    • Where its time goes in the world is unknown. The new line heap views synced per frame splits it into syncs, pages, slots checked and slots made again, and ms for taking the written ranges (pages re-guarded), reading, checking and making.
  • Refreshes:
    • A written page costs 3.0 µs to refresh (5.9 µs when scattered), the same when its bytes are unchanged (refresh_cost_per_written_page). That is roughly the copy itself, so a hash or compare skip saves nothing.
    • The GPU can't blit from title memory that isn't aliased, because D3DMetal refuses OpenExistingHeapFromAddress.
    • What removes a refresh entirely is zero-copy aliasing (mach_vm_remap, alias.rs). It is on here (24 GiB), but only for XMemVirtualAlloc commits that are read-write and whole pages.
    • New lines:
      • host copies refreshed per frame: copies, tracked/untracked MB, and the top 5 with address and size;
      • title memory committed without zero-copy aliasing: by reason, which is aliasing off, protection, placeholder, not whole pages, or remap failed.

Next run: VICTORY_ASYNC_SUBMIT=1, optionally with VICTORY_HEAP_WORKER=1. That existing switch, off by default, makes heap-indexed textures on its own thread, which should shrink the "uploads" phase. Read the three new lines:

  • If "making them" dominates the heap sync, the lever is view creation (D3DMetal), not the scan.
  • If the top refreshed copies sit in memory that is placeholder or not read-write, the lever is aliasing that memory too. Physical-page views would need their own remap.

Suite: 611/0 twice, victory-hle 211/0. Clippy is clean with and without --all-features, policy ok.

Follow-up 7: views made again, and the region refreshed whole

Run F: the heap sync takes 48.1 ms a frame, and 46.7 ms of that is making 40,989 slots again. Reading and checking pages takes under 2 ms.

9a036af: VICTORY_HEAP_READ_ONCE=1 (off by default). Each slot made again used to re-read its 12-byte record through the kernel under the heap lock. That costs 0.46 µs under Wine (bench_record_reads), about 19 ms a frame at 41k slots. With the switch, the stale slots' records are read once, under the same lock.

  • bench_made_again_read_once (ring-like, every slot takes another view each pass): 0.77 µs → 0.31 µs per slot made again, with the same made and kept state.
  • Expected in the world: roughly 46.7 → ~28 ms "making them" (about −19 ms per frame of preparing). Test: read_once_makes_what_reading_each_record_makes.
  • Caching by descriptor content already happens within a sync: one view per key, copied with CopyDescriptors. A cache that lasts across syncs would have to keep resources alive past eviction, so it isn't built. The benchmark's remaining 0.31 µs per slot is the next ceiling.

The 25.2 MB region refreshed whole (0x73c8a80000). Zero-copy aliasing (mach_vm_remap) only covers XMemVirtualAlloc commits that are read-write and whole pages. A region whose every page the title rewrites each frame, and that doesn't show in the "committed without zero-copy aliasing" line, most likely lives in physical pages (XMemAllocatePhysicalPages + XMemMapPhysicalPages, file sections). The GPU can only read those directly if the pages are backed by GPU-visible memory. The diagnostics-only VICTORY_PHYSICAL_TILES (alias::PhysicalHeap) is that trial. The refreshed-copies line now marks copies of memory the title shows at several addresses.

Next run: VICTORY_ASYNC_SUBMIT=1 VICTORY_HEAP_READ_ONCE=1. Read "making them", and whether the top refreshed copies are marked "(shown at several addresses)". Also send the "committed without zero-copy aliasing" line, if it printed.

Suite: 612/0 in three runs. Clippy clean, policy ok.

Follow-up 8: read-once by default, and what making slots costs

Run G (9a036af, VICTORY_HEAP_READ_ONCE=1): the heap sync fell from 50.1 to 33.5 ms, which is 0.70 µs per slot made again instead of 1.14. The game ran at 2.9 fps / 342.8 ms against 2.6 fps / 386 ms in run F, and with more draws. No glitches were reported.

dd434a4:

  • VICTORY_HEAP_READ_ONCE is on by default (=0 turns it off). It reads the same bytes under the same lock, and the run showed no difference except the time. Test: reading_records_once_is_on_unless_zero.
  • New line heap slots made again per frame:
    • outcomes: already holding that view, views created (with ms), copied from one created in the same sync, left null for draws, made null;
    • ms: looking up and preparing views, the slot loop, CopyDescriptors, keeping, noting (bindless), rest.
  • VICTORY_VIEW_PROBE=1 (off) also counts created views whose descriptor an earlier sync already created. That is the ceiling of a cache across syncs.
  • Offline, in bench_made_again_read_once (256 views rotating through 16k slots), creating views is only about 2% of making slots again. Most of the cost is the slot loop and the bindless notes. So a content cache would save little unless the world creates far more views. Use the probe to decide before building one.
  • The refreshed-copies line now names each top copy's memory (VirtualQuery: private or mapped, protection, allocation, region):
    • "private, protect 0x4" and not aliased would mean the alias path missed it.
    • "mapped" means a section view, that is, physical pages. mach_vm_remap aliasing doesn't cover those. No-copy access needs the pages backed by GPU memory (the VICTORY_PHYSICAL_TILES trial).
    • D3DMetal offers no newBufferWithBytesNoCopy path through D3D12. OpenExistingHeapFromAddress is refused.

Next run: VICTORY_ASYNC_SUBMIT=1 VICTORY_VIEW_PROBE=1. Send the two heap lines and the refreshed-copies line.

Suite: 615/0, two runs. Clippy clean, policy ok.

Follow-up 9: test flakes, and the physical pages behind the refreshes

778bfd7: four flaky tests fixed. I ran the suite 20 times under Wine: no access violation, but one failure. Three tests assume submission is synchronous and now run under the mode lock:

  • list::…allocate_upload_survives…;
  • resource::…a_texture_read_back…;
  • resource::…a_list_waiting_for_another_queues_write….

shadow::loose_memory_must_be_committed now takes a reservation that has no stale copy over it. Afterwards 20 of 20 runs were green (10 synchronous, 10 with VICTORY_ASYNC_SUBMIT=1), and CI is green.

Run H (778bfd7): 3.0–3.1 fps, heap views synced 23.9 ms. The four largest refreshed copies, about 75 MB of the 173 MB a frame, are all mapped, protect 0x2:

  • mapped means section views, i.e. the title's physical pages (XMemMapPhysicalPages). Zero-copy aliasing only remaps private XMemVirtualAlloc memory, so it doesn't reach these.
  • protect 0x2 is the change tracking's own guard on pages it has taken, not the title's protection. These refreshes therefore already copy only pages the title wrote since. The 25.2 MB region is the title writing every page of it each frame. Refreshing on some other trigger, such as after a DirectStorage batch, would copy the same bytes.

5955ee4: how those copies lie on physical pages. The GPU could only read them in place if the physical pages were GPU memory and a copy's pages fell in one GPU range: one run per section, or a tiled resource per page. The trial behind VICTORY_PHYSICAL_TILES (diagnostics builds) backs sections with a placed buffer. The refreshed-copies line now shows, for the five largest copies, physical pages P in R runs over S sections, from victory-hle's page table via victory_gpu::on_physical_runs. Test: physical_runs_count_pages_one_after_another_in_a_section.

  • If R ≈ 1 per section, a buffer placed over the section's backing is enough.
  • If R ≈ P, the pages are scattered and would need tiled resources (UpdateTileMappings) under D3DMetal.

The views made again: the line heap slots made again per frame is always printed (on Windows builds), directly after heap views synced per frame. Its part "created by an earlier sync" (the ceiling for a view cache) appears only with VICTORY_VIEW_PROBE=1.

Run I: VICTORY_ASYNC_SUBMIT=1 VICTORY_VIEW_PROBE=1. Send the two heap lines and the refreshed-copies line.

Suite: victory-gpu 615/0 (twice), victory-hle 212/0. Clippy clean, policy ok.

Follow-up 10: merge with main, the view cache, the physical pages, and list steps

5b24362: merged origin/main. main's #640 now reads a heap sync batch's stale records once per page, always, which is what VICTORY_HEAP_READ_ONCE did here. I kept main's version and removed the switch, its test and its docs row. Suite after the merge: 620/0 (twice), victory-hle 216/0.

(1) View cache: not built, because the measurement says it can't help. With main merged, bench_made_again (a ring of 256 views over 16k slots) takes 0.36 µs per slot made again. Of that:

part share
creating views 0.3%
the slots' loop 45%
viewed.set_batch (bindless range bookkeeping) 30%
copying descriptors 3%

A content cache would only remove the 0.3%. The real lever is the bookkeeping. In a ring of tables, each view leaves one slot and enters another, so a batch's net change to the bindless ranges is close to zero. Netting the +1/−1 per range before applying it would skip most of set_batch, up to ~30% of the ~23 ms. In the world, the line heap slots made again per frame gives the exact split. Its "created by an earlier sync" part needs VICTORY_VIEW_PROBE=1.

(2) A no-copy GPU view for the physical pages is possible. The existing hardware probe inplace_probe_6_remap_cpu_visible_tiles passes under D3DMetal. It shows that a reserved buffer whose tiles UpdateTileMappings maps onto a CPU-visible L0 write-back heap works:

  • in any order, including remapping;
  • CPU writes are seen by the GPU and GPU writes by the CPU, with no copy.

Run I's layout (576 pages in 4 runs, 384 in 7, one section each) fits it well. One mapping call per run covers a whole title buffer. The plan, behind a switch:

  1. Back each physical-page section with such a heap instead of a file mapping, and map the title's views onto it with mach_vm_remap. This is the existing VICTORY_PHYSICAL_TILES trial, today diagnostics-only.
  2. For a title buffer on those pages, use a reserved buffer tiled onto the heap in title order. Its host copy becomes zero-copy: no refresh, no copy-back.
  3. Update tile mappings in queue order when the title remaps pages (XMemMapPhysicalPages, CopyPageMappingsBatchX, already queued in order by this PR).

Expected gain: most of the ~175 MB / 15 ms of refreshes, plus the title threads' write faults on those pages (~35 µs a page in the tracking benchmark). Risks:

  • DirectStorage writes through our own section views;
  • section memory becomes wired GPU memory;
  • the cost of tile updates under D3DMetal.

This is a multi-step change to victory-hle and victory-gpu, and it needs game runs at each step.

(3) 33475e6: list steps split. New line host list steps per frame: each step kind counted, the heavier ones timed. The kinds are:

  • title copies and fills;
  • host work;
  • shader writes declared (their snapshots and the refreshes they flush);
  • table textures;
  • heap scans (bindless);
  • page tables (title memory);
  • refreshes and table slots, counted only; their time is the rest of the phase.

Run J: VICTORY_ASYNC_SUBMIT=1 VICTORY_VIEW_PROBE=1. Send the lines host preparing per frame by phase, host list steps per frame, heap views synced per frame, heap slots made again per frame and host copies refreshed per frame.

62476de: merged origin/main again (up to 7128c74). Two adjustments:

Suite after the merge: victory-gpu 639/0 (three runs), victory-hle 216/0. Clippy clean, policy ok. CI green (run 46087857132), and the PR is mergeable again.

Follow-up 11: the title's physical pages read in place (VICTORY_PHYSICAL_TILES=1)

5416c68, off by default, Rosetta and D3DMetal only. The largest refreshed copies in run I sit on the title's physical pages: 576 pages in 4 runs and 384 in 7, one section each. With the switch on, those pages are GPU memory, and such buffers need no copy:

  • Sections as GPU heaps. Each section of physical pages is a CPU-visible L0 heap. This is alias::PhysicalHeap, until now a diagnostics-only trial. The title's views are remapped onto the heap (mach_vm_remap), so the title's CPU writes and DirectStorage's writes land in GPU memory.
  • Zero-copy host copies. A title buffer whose pages all lie on such heaps when it is first used gets a host copy that is a reserved buffer. Its 64 KiB tiles are those pages in the title's order (UpdateTileMappings, one call per run of consecutive tiles). Nothing is refreshed into it or copied back, and what shaders write the title reads at once.
  • Remaps. When the title maps other pages there or unmaps them, victory-hle passes the new tiles (victory_gpu::physical_retiled) and the copy's tiles follow at once on the host queue.
  • D3DMetal ignores null tile mappings: the tile keeps its old page. The inplace probe only passed because its fresh readback buffer held zeros anyway. Unmapped pages therefore show a tile of zeros of our own.
  • Lock order. The tiles are asked for without the buffers' lock held. victory-hle holds its own lock while it tells the trackers about a mapping, and those trackers may read the buffers.

Tests:

  • victory-gpu:
    • Title pages remapped onto heap tiles 2, 0, 3, 1: the GPU reads the title's bytes with no refresh, and the title reads the GPU's write at once.
    • A retiled page shows its new tile; an unmapped page reads zeros while the others stay.
    • A buffer with a page off GPU memory is copied as before.
  • victory-hle, with the switch: mapped physical pages name their tiles in the title's order, title writes land in the heap, and unmapped pages name none.

Also fixed:

  • own_section_views_start_above_two_tib no longer checks the address floor when the whole view is the GPU heap.
  • Two prep-ahead tests now retry when another test's synchronous submission lands the flight they hold in the air, as until_taken was meant to do. The new test's tile updates made that more frequent.

Suites under Wine before the pause in Wine tests:

  • victory-gpu: 641/0 in four of five runs. The fifth failed in shadow::marked_pages_go_back_by_the_bytes_the_gpu_changed, which also fails without this change while other sessions' GPU tests run.
  • victory-hle: the new tests pass with the switch on. The last victory-hle suite run with the switch on had two failures: the floor test, fixed since but not re-run, and lifecycle::…a_title_window…, which looks unrelated. Both still need a Wine run after run K1.

Run J (or K): VICTORY_ASYNC_SUBMIT=1 VICTORY_PHYSICAL_TILES=1 VICTORY_VIEW_PROBE=1 against the same without VICTORY_PHYSICAL_TILES. Read host copies refreshed per frame (the physical-page copies should be gone), the refreshes phase, the startup log line first title buffer read on its physical pages in place, and watch for corrupt geometry or textures. Memory: the physical pages become wired GPU heaps.

🤖 Generated with Claude Code

…fences

VICTORY_ASYNC_SUBMIT=1 (pragyaangaur#447) already lets the title's ExecuteCommandLists,
Signal and Wait only queue their work for victory-submit, but its time was
invisible: the frame report's "threads held by host GPU work" line counted
nothing under it, so it could not be weighed against the default.

- queue.rs: the title's calls count what they spend queuing; threads that
  wait for the worker to drain (PresentX as "presents", our read-backs,
  captures and page mappings as the new "our syncs") count the drain's lock
  and run; the Flight-1-only pump on title threads is counted too.
- host/flight.rs: per flight, the worker's time waiting for the last flight
  to land, preparing and launching, and the host GPU's idle time before it,
  split into nothing to run and preparing; logged as a frame-report line.
- Tests: the title's thread is back while its work is held, may reset the
  list at once, never runs host work, and its fence moves only after the
  work's bottom-of-pipe write; a queue waiting on another queue's fence
  completes only after that queue's fence and write show.
- docs/macos.md: the switches and the lines.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
hackgods pushed a commit to hackgods/VIctory-macOS that referenced this pull request Nov 27, 2026
…fences

VICTORY_ASYNC_SUBMIT=1 (pragyaangaur#447) already lets the title's ExecuteCommandLists,
Signal and Wait only queue their work for victory-submit, but its time was
invisible: the frame report's "threads held by host GPU work" line counted
nothing under it, so it could not be weighed against the default.

- queue.rs: the title's calls count what they spend queuing; threads that
  wait for the worker to drain (PresentX as "presents", our read-backs,
  captures and page mappings as the new "our syncs") count the drain's lock
  and run; the Flight-1-only pump on title threads is counted too.
- host/flight.rs: per flight, the worker's time waiting for the last flight
  to land, preparing and launching, and the host GPU's idle time before it,
  split into nothing to run and preparing; logged as a frame-report line.
- Tests: the title's thread is back while its work is held, may reset the
  list at once, never runs host work, and its fence moves only after the
  work's bottom-of-pipe write; a queue waiting on another queue's fence
  completes only after that queue's fence and write show.
- docs/macos.md: the switches and the lines.

(Upstream pragyaangaur#584 by Nick233x, applied onto two flights: its step enum is
Phase here, as flight.rs already uses host's Step; the GPU counts as free
after a landing only once no flight is left in the air; the docs row
only.)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Nikola Milenkovic and others added 2 commits November 27, 2026 16:39
…mission

With VICTORY_ASYNC_SUBMIT=1, CopyPageMappingsBatchX drained all queued work
and the flight in the air on the title's thread: in the open world that was
the "our syncs" 124-182 ms of a 428 ms frame, and the GPU then sat idle with
nothing to run while the title waited (async 2.3 fps against 2.9 by default).

On the console the batch is a queue operation. It now is here too, with
VICTORY_ASYNC_SUBMIT=1 or VICTORY_FLIGHT1=1: while its queue holds work or a
host submission is in flight, the batch becomes a hold::Item::Action and the
title's thread goes on. The action ends its queue's part of a coalesced
piece (other queues still join); the submit worker lands the flight, as the
next preparation would anyway, maps, and only then prepares the queue's
later work. With nothing held or in flight it maps at once, as before. The
default (synchronous) path is unchanged.

The frame report gets a line of what still drains queued work: presents,
page mappings done at once or queued, and our own synchronous host work by
the line that called it (#[track_caller] through Host::execute).

Tests: an action runs between its queue's work in both pumps and keeps a
signal before it first; take_ready_with hands it out only after its queue's
work, with another queue's work still joining; under async a mapping
behind held work returns at once and maps after the earlier work's fence
and before the later work's.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
queue::present drains the queued work, then Host::present submitted its
swap-chain copy through Host::execute, whose before_sync drained a second
time: behind everything other title threads queued since (the next frame's
work), counted as "our syncs". With VICTORY_ASYNC_SUBMIT=1 or
VICTORY_FLIGHT1=1 the copy now goes through execute_sync: behind the host
submission in flight only, ahead of work queued after PresentX, as on the
console. The synchronous default still calls Host::execute (before_sync is
a no-op there).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Nick233x Nick233x changed the title perf(gpu): measure async submission (VICTORY_ASYNC_SUBMIT=1) in the frame report, and test its fence ordering perf(gpu): async submission (VICTORY_ASYNC_SUBMIT=1): measure it, queue page mappings in order, present without a second drain Nov 27, 2026
Nikola Milenkovic and others added 2 commits November 27, 2026 17:19
…t (VICTORY_PREP_AHEAD=1)

With VICTORY_ASYNC_SUBMIT=1 the host GPU still sat idle ~150 ms a frame
while the next submission was prepared: Wait-Before-Prep landed the flight
in the air first, as host copies of title memory are refreshed in place.

Opt-in VICTORY_PREP_AHEAD=1 (with Flight-1) prepares the next submission
behind the one in flight and launches it behind it, at most two in flight,
when the flight in the air is quiet at landing: no ranges shaders write, no
write marks, no read-backs, no work after it or bottom-of-pipe writes, no
uploads. Then its landing changes nothing the preparation reads, and the
in-place refreshes rewrite what it reads with the same bytes: the title
does not change memory submitted work reads until that work's fence moves,
at its landing. The flight in the air lands first (land_ahead, on the
preparing thread under Host::submitted) for what needs the GPU done with
it: the frame's eviction, a view remake, the shared barrier lists, a heap
fill (Host::quiet), title copies (done on the CPU while preparing), and,
before launching, the new submission's write marks, as every landing takes
the marks bitmaps whole. Lists now record binding those bitmaps.

FlightState holds up to two flights, with title signals per flight;
landing records the landed flight's own fence value (SUBMISSIONS_DONE,
retirement), and an even fence value ends with a second mark so arming one
never changes the other's. Native splits stay together behind a flight.
The default and VICTORY_ASYNC_SUBMIT=1 without the switch keep
Wait-Before-Prep; a frame-report line counts what was prepared and
launched behind a flight and why the others landed it first.

Tests: launched behind a quiet flight, two in flight, results and fences in
order; a flight whose shaders write a host copy lands before the next
refreshes it (a mutation that treats it as quiet loses the GPU's write);
write marks and title copies land the flight first.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…mit-17244c

# Conflicts:
#	crates/victory-gpu/src/host.rs
@Nick233x Nick233x changed the title perf(gpu): async submission (VICTORY_ASYNC_SUBMIT=1): measure it, queue page mappings in order, present without a second drain perf(gpu): async submission (VICTORY_ASYNC_SUBMIT=1): measure it, queue page mappings, present without a second drain, and prepare the next submission during a quiet flight (VICTORY_PREP_AHEAD=1) Nov 27, 2026
@IlyassRS3

Copy link
Copy Markdown
Contributor

This breaks the native macOS build. CONTRIBUTING asks for cargo test --workspace on a Mac, and with this PR merged into main (d1609d9), cargo test -p victory-gpu no longer compiles on an M5 (macOS 27.2.1):

error[E0433]: cannot find `host` in `crate`
    --> crates/victory-gpu/src/resource.rs:1918:23
error[E0433]: cannot find `placement` in `crate`
error[E0433]: cannot find `shadow` in `crate`

EVICTED_IN and evict_due went in between evict_unused's doc comment and #[cfg(windows)] on one side, and the function on the other. So the attribute and the doc now apply to the static, and evict_unused is compiled off Windows, where those modules don't exist. Windows builds don't see it, which is why the gnullvm clippy is clean.

Moving the gate back fixes it. With this on top of the merge, native cargo test -p victory-gpu gives 193 passed, 0 failed (main gives 191), and cargo clippy -p victory-gpu --all-targets --target x86_64-pc-windows-gnullvm -- -D warnings is clean with and without --all-features:

diff --git a/crates/victory-gpu/src/resource.rs b/crates/victory-gpu/src/resource.rs
index eb9f18d..871bb69 100644
--- a/crates/victory-gpu/src/resource.rs
+++ b/crates/victory-gpu/src/resource.rs
@@ -1894,21 +1894,23 @@ pub(crate) fn evict_heap_uav() -> bool {
     *ON.get_or_init(|| std::env::var_os("VICTORY_EVICT_HEAP_UAV").is_some_and(|v| v != "0"))
 }
 
-/// Once per presented frame, before a submission (`Host::execute`, one at a
-/// time, the last one has run): gives up host textures fed from title memory
-/// that the title has not used for [`IDLE_MS`], and past [`BUDGET_HIGH`] of
-/// the card's budget the least recently used ones until [`BUDGET_LOW`].
-/// Host copies of title memory nothing asked for in as long go too
-/// ([`crate::shadow::release_stale`]).
-#[cfg(windows)]
 /// The frame [`evict_unused`] last ran in.
+#[cfg(windows)]
 static EVICTED_IN: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(u64::MAX);
 
 /// Whether [`evict_unused`] would run now (once per presented frame).
+#[cfg(windows)]
 pub(crate) fn evict_due() -> bool {
     EVICTED_IN.load(std::sync::atomic::Ordering::Relaxed) != crate::queue::presented()
 }
 
+/// Once per presented frame, before a submission (`Host::execute`, one at a
+/// time, the last one has run): gives up host textures fed from title memory
+/// that the title has not used for [`IDLE_MS`], and past [`BUDGET_HIGH`] of
+/// the card's budget the least recently used ones until [`BUDGET_LOW`].
+/// Host copies of title memory nothing asked for in as long go too
+/// ([`crate::shadow::release_stale`]).
+#[cfg(windows)]
 pub(crate) fn evict_unused() {
     use std::sync::atomic::Ordering::Relaxed;
     let frame = crate::queue::presented();

I gated evict_due too, on the assumption that only the Windows path calls it. Drop that line if a native caller needs it.

AI-assisted, prepared with Claude Code.

@4TheDoctor

Copy link
Copy Markdown
Contributor

Tried this on an M3 Max (48 GB, macOS 27.2.1, CrossOver 26.1, D3DMetal 4.0 beta 3, Series X): PR head 08a01a6a with #589 merged locally so the scene is lit, VICTORY_GPU_TIMING=0, 720 s runs, one of each.

A heads-up first. In the VICTORY_ASYNC_SUBMIT=1 VICTORY_PREP_AHEAD=1 run the GPU stalled for 150 s, about 6 minutes after world entry, and it took the desktop with it: macOS's watchdog killed WindowServer twice (40 s without a check-in each time). In both stackshots NotificationCenter's com.Metal.CommandQueueDispatch thread is stuck uninterruptible inside IOGPUFamily/AGXG15C, and in the second one the kernel's IOServiceTerminateThread waits on a mutex that thread holds. victory.exe's submit and flight threads had not run for 36 s and 86 s at the two snapshots, but none of them was stuck in the kernel. After the 150 s the game carried on (about 360 ms/frame) until the run ended.

I can't pin it on prep-ahead: it never kicked in on this title here (launched while it still was 0.0, all of it "not quiet at landing 5.0"), so that run was async only in practice. One stall in two async runs isn't a rate either. It looks like the same family as #442 (submissions stuck in IOGPU), except this time it froze the whole Mac's graphics instead of only the game.

Median frame over the same stretch of the world (184 to 356 s after world entry). The default run had a cold shader cache, so its number is somewhat high:

median best
default 745 ms 400 ms
VICTORY_ASYNC_SUBMIT=1 536 ms 378 ms
+ VICTORY_PREP_AHEAD=1 (inert, see above) 407 ms 376 ms

Report lines from the async-only run: title submissions 710 → 0.2 ms/frame; presents 23 + 294; our syncs 0. Flight line: worker busy 344 (waiting for the last to land 60, preparing 165), host GPU idle before them 200 (nothing to run 21, preparing 179). Drains: presents 1.0, page mappings 0.1 at once and 0.1 queued, our own host work 0. pieces= went from 20 to 10-12.

I can run a longer async series on Apple Silicon if it helps, but given the desktop freeze I'd rather check with you first.

Nikola Milenkovic and others added 9 commits November 27, 2026 20:33
…P_AHEAD=1 probe)

Run C: with VICTORY_PREP_AHEAD=1, 6-8 of ~10 host submissions a frame were
not quiet at landing, so almost nothing was prepared ahead. Before
replacing the whole-flight rule with a range one, this measures its
ceiling without changing what runs.

With VICTORY_PREP_AHEAD=1, each landing records why its flight was not
quiet (declared ranges, write marks, work after it, bottom-of-pipe writes,
read-backs, table textures, uploads) and what it wrote back: declared and
bottom-of-pipe ranges, and the pages its shaders marked. The preparation
right after it records the host-copy ranges its refreshes rewrite
(Batch::add, written copies only) and is counted against them: refreshing
nothing the flight declared (known before it lands), nothing it wrote
(known after), nothing any view write can reach (the copies owning mark
bits, the bound a range rule can use for a flight that marks); a frame
line gives the ceiling by ranges, and the same if marks were known ahead.

The prep-ahead tests now retry their scenario: another test's Host::quiet
lands every flight in the air, and freed test memory left its host copy
registered for the next allocation at the same address.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A new line per report, "host preparing per frame by phase, ms": evicting,
heap views synced, uploads, list steps, preludes, refreshes, landing early
(VICTORY_PREP_AHEAD=1) and other. The preparing line only told copies and
textures apart, which left 40-50 ms of the ~100 ms a frame unnamed; this
names it, so the next run says which part to make faster. A lap is one
clock read per phase and list (~10 lists a submission).

"preparing" (SUBMISSION_US) now ends after the hang note, which it used to
follow: the note's time was on the submit worker's chain but counted
nowhere.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Every submission copied the names of all pipelines its lists bind into a
new set and formatted them, for a log line written only if the GPU hangs
for 8 s. Under Wine that cost 0.2 ms a submission at 100 pipelines a list
and 0.6 ms at 300 (benchmark waiting_on_takes_the_sets), on the submit
worker's chain about 10 times a frame. A list's set is now shared (Arc):
the note keeps the sets, and the hang line names them. A reset gives a
list a new set if the note still holds the old one, so nothing is copied
while recording either.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…et (VICTORY_PREP_AHEAD=ranges)

Off by default; =1 and the default path are unchanged. With =ranges a
submission is also prepared behind a flight that is not quiet at landing,
when what its landing writes is known before it runs (HostList::
ahead_writes): ranges its shaders declare (whole 64 KiB pages, as the
trackers take pages), write marks (bound by every copy owning mark bits)
and bottom-of-pipe writes outside host copies. A flight with work after
it, read-backs, table textures or uploads lands first, as before.

The preparation lands it first only where it would refresh those bytes
(Batch::add), declare shader writes in a copy it writes (one snapshot and
pending set a copy), fill from them, or run host work (Early::Overlap,
Early::CpuRead). Its landing then writes title memory the preparation
did not read and nothing into host copies: what its shaders wrote is in
them already, and a refresh of other bytes of a copy leaves its snapshot
and pending ranges alone.

shortcut: a texture over memory the flight's shaders write can be brought
up one submission late, as one written in the same submission already is.

The prep-ahead line counts preparations behind such a flight and why they
landed it. Tests: the spans (pages, bytes, whole copies), a launch behind
a flight writing other memory (two in flight, write goes back, refresh
reads the title's bytes), and the hazard: a refresh of what the flight
writes lands it first (fails with the check removed).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ode lock

In the full suite another test could switch Flight-1 on while it ran: its
submission then returned before it ran, and the mark check after it failed
in about one run in four.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…es first (VICTORY_SAME_COPIES)

A new frame-report line, "host preparing copies per frame", splits the
preparing copies (the ~180 MB a frame in the world) into title copies
between buffers, fills of upload memory from title memory, and refreshes
of host copies, with counts, MB and ms each.

VICTORY_SAME_COPIES (off by default): each title copy is compared first,
its source's bytes against its destination's and every host copy of the
destination (host_copy::same_copy). All the same, the copy changes nothing
anywhere: "probe" counts those, "skip" leaves them out, so the copy does
not write title memory and its destination's host copies are not made due
for a refresh again. A host copy holding other bytes (what shaders wrote
and kept there) makes the copy run, as on the console it overwrites them.
Comparing reads through the kernel as the copy does, so unmapped pages
fail it instead of faulting; aliased, overlapping and copies over 16 MiB
are not compared.

Comparing costs 4-5 times a warm copy under Wine (same_copy_costs: 8.4 us
against 1.9 us for 64 KiB, 1.2 ms against 0.23 ms for 8 MiB), so skip pays
only where most copies are in place, plus the refreshes it saves: probe
first.

Tests: the switch's values; a copy in place is left out and its copy stays
current, with probe it still runs; a copy changing the destination, or
whose host copy holds other bytes, runs with skip (fails with the host-copy
check removed).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Run E put "heap views synced" (41-56 ms a frame) and the refreshes of host
copies (~174 MB, 37-45 ms) first. Measured offline before changing them:

- An unchanged 64 KiB heap page (2,048 slots, all synced) is checked in
  1.8 us, less than comparing it with its last bytes (2.2 us;
  bench_unchanged_page_scan_vs_compare). The sync already checks only
  pages the title wrote, and slots whose record is unchanged cost nothing
  more: skipping by page contents would not help. Where its time goes in
  the world is not known, so a new line says it: "heap views synced per
  frame": syncs, pages, slots checked, made again; ms taking the written
  ranges (guarding pages again), reading pages, checking slots, making them.

- A refresh of a written page costs 3.0 us on tracked memory (5.9 us
  scattered), the same with unchanged bytes (refresh_cost_per_written_page):
  about the copy itself, so comparing first saves nothing, and the GPU
  cannot read title memory that is not aliased (D3DMetal refuses
  OpenExistingHeapFromAddress). What removes a refresh is zero-copy
  aliasing, so two new lines say which memory is refreshed and why it is
  not aliased: "host copies refreshed per frame" (copies, tracked and
  untracked MB, the five that took most, with address and size), and
  "title memory committed without zero-copy aliasing" (XMemVirtualAlloc
  commits by reason: aliasing off, protection, placeholder, not whole
  pages, remap failed).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…AP_READ_ONCE=1)

Run F: the heap sync is 48 ms a frame, 46.7 of it making ~41,000 slots
again. Each slot made again re-read its 12-byte record through the kernel
under the heap's lock (a title write since the page was read defers it to
`mirror`): 0.46 us a read under Wine (bench_record_reads), ~19 ms a frame.
With VICTORY_HEAP_READ_ONCE=1 (off by default) the records from the first
slot to the last are read once, under the same lock (at most 1 MiB; a
sync's slots lie in one 64 KiB page); a failed read falls back to one read
a slot. A title write after the read is seen by the next sync, as one
after a slot's own read was.

bench_made_again_read_once (a ring of tables: every slot takes another
view each pass, 256 views): 0.77 us a slot made again, 0.31 us with the
switch, the same views made and kept. Test: reading once makes and keeps
the same slots as reading each record, over three syncs with changes.

The refreshed-copies line marks copies of memory shown at several
addresses (physical pages the title maps more than once), which zero-copy
aliasing does not reach. a_title_copy_of_bytes_in_place_is_left_out takes
memory with no other copy or alias registered (memory another test gave
back may have them, and then nothing is compared): it failed once in the
full suite.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…lots costs

Run G (VICTORY_HEAP_READ_ONCE=1, ~2,700 frames in the world, no glitch
seen): heap sync 50.1 -> 33.5 ms a frame, 0.70 us a slot made again
against 1.14. It reads the same bytes under the same lock, so it is on by
default now; VICTORY_HEAP_READ_ONCE=0 turns it off.

A new line, "heap slots made again per frame", says what the remaining
~30 ms are: slots holding that view already, views created (and their
ms), copied from one created in the same sync, left null for draws, made
null; then ms looking the views up and preparing them, the slots' loop,
copying descriptors, keeping what they view, noting it (bindless), and
the rest. VICTORY_VIEW_PROBE=1 also counts the created views whose
descriptor an earlier sync created already: what a cache of views across
syncs could save at most.

Offline (bench_made_again_read_once, 256 views rotating through 16,384
slots), creating views is 2% of making slots again: most of it is the
slots' loop and noting what they view. A content cache would only take the
2%; the probe says how much more it is in the world before one is built.

The refreshed-copies line now names each top copy's memory (VirtualQuery:
private or mapped, protection, allocation, region), to tell whether
zero-copy aliasing (private XMemVirtualAlloc memory) could reach it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@4TheDoctor

Copy link
Copy Markdown
Contributor

Ran the new head on the M3 Max: 9a036afb with #589 merged so the cell is lit, VICTORY_ASYNC_SUBMIT=1, VICTORY_GPU_TIMING=0, no VICTORY_PREP_AHEAD, 720 s, cold cache.

  • No GPU stall this time: no ended submissions, no unwaited submissions, no WindowServer report, desktop fine the whole run. The run reached its 720 s.
  • World frames from 450 s: median 386 ms, steady at 366 to 395 ms from about 470 s on. The cell is lit (median luma 29 to 30).

One caveat on the number. On d1609d9 I see two steady frame levels with the same draws, about 400 ms and about 720 ms (details on #608), with or without async, and this run happened to stay on the fast one. Comparing like with like, from the host's 5-second report lines:

Run Fast level frame Prep GPU line
this head, async 386 ms 179 ms 170 ms
previous head 08a01a6a, async 387 ms 173 ms 183 ms
d1609d9 + #589 + #605 + #606 + #608, sync 401 ms 184 ms 177 ms

So async is a few percent on the fast level here. I left VICTORY_PREP_AHEAD off because the stall last time came in the run with it on. A longer run (30 min) is easy if that's what you need; say whether you want prep-ahead in it.

Nikola Milenkovic and others added 6 commits November 28, 2026 00:24
…th copies

A loop of the victory-gpu lib suite under Wine (20 runs at dd434a4) found
no access violation, but one failure, in a test checking right after a
submission what assumes it ran: another test had switched Flight-1 on.
That test and the two others known to assume synchronous submission
(they failed under VICTORY_ASYNC_SUBMIT=1) now hold the mode lock,
synchronous:

- list: allocate_upload_survives_early_reset_and_release_while_held_on_gpu
- resource: a_texture_read_back_reaches_the_title_buffer
- resource: a_list_waiting_for_another_queues_write_reads_what_it_wrote

shadow::loose_memory_must_be_committed failed once with
VICTORY_ASYNC_SUBMIT=1: a reservation where another test's memory had
been may still have a copy registered over it, which is found. It now
takes a reservation with no copy over it.

After: 20 runs, 10 synchronous and 10 with VICTORY_ASYNC_SUBMIT=1, all
615 passed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Run H: the four largest refreshed copies (37.7, 25.2, 12.6 and 182.5 MB
regions, ~75 MB of the 173 MB a frame) are all "mapped, protect 0x2".
Mapped: section views, the title's physical pages (XMemMapPhysicalPages),
which zero-copy aliasing (private XMemVirtualAlloc memory) does not reach.
Protect 0x2 (read-only) is the change tracking's guard on pages a copy has
taken, not the title's: their refreshes are already only of pages the
title wrote since, so the 25.2 MB region refreshed whole is the title
writing every page of it each frame. A refresh on some other event would
not take fewer bytes.

For the GPU to read them in place, those physical pages would have to be
GPU memory (the VICTORY_PHYSICAL_TILES trial backs sections with a placed
buffer) and a copy's pages lie in one GPU range: one run of pages in one
section, or a tiled resource mapping each. So the refreshed-copies line now
says, for each of the five largest, how its memory lies on physical pages
(victory-hle's page table, through victory_gpu::on_physical_runs): pages,
runs of pages one after another in a section, and sections.

Test (victory-hle): pages mapped 0, 1, 3, 2 make three runs of one section.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…lock

CI (run 46083986817) failed it: with another test's VICTORY_PREP_AHEAD on,
even submissions are marked by the second mark list, not host.mark(). It
now runs synchronous and without prep-ahead, as work_goes_on_on_a_new_queue
does.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
main's pragyaangaur#640 reads a heap sync batch's stale records once a page, always,
as VICTORY_HEAP_READ_ONCE did here (on by default since dd434a4): that one
is kept, and the switch, its test and its docs row go. bench_made_again
times the merged path and prints the split of making slots again. In
flight.rs, main's SUBMISSIONS_BEGUN store goes after the fence check here.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Run I: "list steps" is 16.6 ms a frame. A new line, "host list steps per
frame", counts each kind of step the lists run while preparing and times
the heavier ones (one clock read a step): title copies and fills, host
work, shader writes declared (their snapshots and the refreshes they
flush), table textures, heap scans (bindless), page tables (title
memory). The many refreshes asked and table slots are counted only; their
time is the rest of the phase.

Test: host work is counted and timed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
main's pragyaangaur#592 keeps a flight's undeclared shader writes armed until it
lands (InFlight.armed, disarmed at landing): kept, beside the prep-ahead
probe's landing record. flight.rs imports host's list Step as ListStep,
apart from its own Step of flight times. host re-exports flight's Gate
(main) and probe_marked (here).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants