Skip to content

d3d12: the threads of a frame share its pipeline wait budget and none is left with nothing (D3D12-COLD-START-SKIPS) - #3000

Merged
OreoFR merged 1 commit into
ProjectLeonida:mainfrom
enlight3d:cold-start-skips
Dec 2, 2026
Merged

OreoFR merged 1 commit into
ProjectLeonida:mainfrom
enlight3d:cold-start-skips

Conversation

@enlight3d

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: D3D12-COLD-START-SKIPS (#2570 (comment))

What and why

On a first start (a data folder with no pipeline book, so no pipeline is started early) the D3D12 runtime makes each pipeline at its first draw, and a draw waits for it within a budget per frame. That budget, one for all the title's recording threads, was a reservation: the first thread to wait in a frame took all that was left of it, and every other thread recording in that frame, as well as a thread still on an older frame, was left nothing. Their draws went out without a wait and were dropped as not made. At the arrival in the world that was 400 to 3,600 draws, dispatches and indirect executions a frame, and a pass the title runs only once, the one that draws the character's head, was lost for the whole run. A budget of ten minutes a frame (LEONIDA_PIPELINE_WAIT_MS=600000) still left hundreds a frame dropped, because the other threads still got nothing.

This change (d3d12/crates/leonida-gpu/src/pipeline/make.rs):

  • counts a frame's waits once over the threads that wait at the same time: what passed while at least one thread waited. A thread that begins to wait while others do is left what is left at that count, so waits that overlap still cost the frame its budget once (why the budget is shared: five threads each waiting the whole half second cost 2.5 s a frame). The time a thread records between two waits is not counted. A thread still on an older frame joins the current frame's waits and opens no budget of its own (d3d12: keep pipeline wait budgets from moving backward #2601 still holds). On by default; LEONIDA_PIPELINE_WAIT_DEADLINE=0 reserves the frame's budget as before (no effect with LEONIDA_PIPELINE_WAIT_SHARED=0), and LEONIDA_AB can turn it while the title runs;
  • charges a pipeline's own budget (LEONIDA_PIPELINE_WAIT_ONCE) the same way, the real time once, in every mode: LEONIDA_PIPELINE_WAIT_DEADLINE=0 and LEONIDA_PIPELINE_WAIT_SHARED=0 bring back only the old sharing of the frame's budget, and LEONIDA_PIPELINE_WAIT_ONCE=0 turns a pipeline's own budget off as before. Added up per thread, a pass run once that several threads waited for together ran out of its budget early (13 such pipelines on one first start, some reported two or three times at the same millisecond);
  • passes the queue in which a wait moves its pipeline forward, and the budget a pipeline's waits count against, into the wait, so that tests cover both; one log line gives the rules in use at the first wait.

d3d12/docs/switches.md is regenerated (tools/d3d12_switches.py --out, then --check; tests/test_d3d12_switches.py passes). The rows of this change: the new switch, LEONIDA_PIPELINE_WAIT_SHARED (its sentence now says it brings back only the frame's budget), and the count of files that name LEONIDA_AB. One row is unrelated: LEONIDA_RENDER_SCALE_MIN moves from the loader's section to the GPU's because the page on main had not been regenerated since host/menu.rs began to read it; --out writes it whatever the change.

Not in this pull request: waiting past the frame's budget for a pipeline never made. In one run it removed the last dropped draws, but at the cost of a 39 s frame, and its cost on a cold start is not measured.

Tested

Image runs (RTX 3090, NVIDIA 621.40, Windows 11; Series S settings, no DLSS, no frame generation, the title's hang watchdog quieted; 420 s each from the same save, each on a fresh data folder; captures kept locally, not published; no frame-time measurement). Two series. The first ran build b02c80796, the first form of this change, which ended a frame's waits at one deadline counted from its first wait. The second ran build c38efe212, the logic of this commit (its later edits are comments, a debug check and where a log line is written), four runs alternating the switch, on and off, on and off; the head was judged on the captures shuffled, before matching them to the switch. NVIDIA's own shader cache, per executable: cold in the first run, filled by the earlier runs for the later ones (same executable, run in the order below).

Run Switch Driver cache Frame lines with drops / most drops in a frame / draws dropped (about) 10 s windows with a frame of 5 s or more / longest frame Character's head
1 default cold (1st run of the executable) 17 / 1,684 / 10,100 18 / 11.2 s drawn
2 default warm (3rd run) 1 / 27.5 / 165 3 / 10.6 s drawn, face whole in the cut scene
3 LEONIDA_PIPELINE_WAIT_DEADLINE=0 (the reservation) warm (4th run) 13 / 1,707 / 43,600 5 / 11.7 s missing
main 0ff8a53, earlier — — 17 / 3,651 / 92,000 — missing

Second series, build c38efe212 (the final logic), same setup, one executable path for the four runs:

Run Switch Driver cache (queue peak) Frame lines with drops / most drops in a frame / draws dropped (about) 10 s windows with a frame of 5 s or more / longest frame Character's head
F1 default cold (1,076) 17 / 2,600 / 10,300 19 / 11.9 s drawn
F0 LEONIDA_PIPELINE_WAIT_DEADLINE=0 warmer (119) 8 / 396 / 7,100 2 / 10.1 s missing (also in the cut scene)
F1b default warmer (367) 7 / 403 / 1,600 14 / 11.8 s drawn
F0b LEONIDA_PIPELINE_WAIT_DEADLINE=0 warmer (935) 16 / 3,075 / 157,000 9 / 10.7 s missing

With the reservation the head was missing on every first start: 10 runs of 10 (8 before this change, and the two witnesses of the second series). With this change at its defaults it was drawn in 4 runs of 4 (two of each form). In run F0 the head was missing with few drops, so how many draws are dropped matters less than which: a pass run once. The defect also reproduces on an RTX 4070 Laptop (OreoFR); the fix was not run there.

Limits: four runs with the fix and three witnesses in this executable, on one GPU. A cold start still drops draws (runs 1 and F1: about 10,100 and 10,300), fewer than without (43,600 to 157,000) but not none. Frames of 5 s and more remain on a cold start, and run 1 had more windows with them than the witness: threads now wait where they used to drop. The column "10 s windows" is how the frame lines were read, not a measured frame-time distribution.

Rust, on 7c69bc8b1 (this branch, on main c4ca57615), from d3d12/:

this branch
cargo test -p leonida-gpu --lib --locked -- pipeline::make::tests 21 passed, 3 runs in a row
cargo test -p leonida-gpu --locked 653 passed, 0 failed, 59 ignored
cargo test -p leonida-gpu --features diagnostics --locked 735 passed, 0 failed, 60 ignored
cargo test --workspace --exclude xtask --locked -- --skip depth_bounds_pipeline_loads_and_filters_draws 1,646 passed, 2 failed, 74 ignored
cargo clippy --workspace --all-targets --locked 7 warnings, none in make.rs
cargo clippy -p leonida-gpu --all-targets --features diagnostics --locked 10 warnings, none in make.rs

The two failing workspace tests are both in leonida-loader, a crate that does not depend on leonida-gpu.

  • loads_binds_and_calls_probe failed in the same way on main here (the probe DLL it builds is not found where it looks; this machine builds with CARGO_BUILD_TARGET set).
  • console_insn::tests::a_clzero_loop_becomes_plain_stores passed alone, and in two further runs of the crate's tests (84 of 84).

Compared with main at the same base one commit earlier (1e3e316c5): 9 more tests in each leonida-gpu suite and in the workspace, and the same clippy warnings.

Red and green. The new API makes the tests fail to compile on main itself, so they were run on a seam with no change of behaviour, whose stubs behave as main does; each failed for the reason it states.

  • On main's reservation: the second thread to wait in a frame got 0 instead of what was left (400 ms of 500), and so did a thread on an older frame; of two overlapping waits, the second waited 6 µs.
  • On the first form of this change (one deadline from the frame's first wait): the time a thread recorded between two waits used up the frame's budget (0 instead of 400 ms), and a pipeline that two threads waited for together was charged 600 ms for 300 ms of waiting. That form made such overlapping waits the rule, which is why a pipeline's own budget is counted the same way here.

They pass with the change. Nine mutations, each built and run on c38efe212 (the same logic as this commit, whose later edits are comments, a debug check and where a log line is written), are each caught by at least one test:

  • the reservation brought back, as the default or in the count;
  • the count ignoring a wait under way, or restarting at each wait;
  • a thread's time charged to the pipeline;
  • the promotion removed;
  • every pipeline counted against the world's budget;
  • an older frame given nothing;
  • a wait's end counted in a frame it did not join.

python -I scripts/check_no_game_data.py passes on both changed files.

GPUs tested: NVIDIA GeForce RTX 3090, driver 621.40, Windows 11 Pro (build 26300): image runs and the Rust suites.

AI-assisted: yes (Claude Code, Claude Opus 5.5; reviews by two independent Claude Opus 5.5 agents)

Checklist

  • Based on current main and merges cleanly (a conflicting pull request is labeled needs rebase).
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed. (No translator change.)

🤖 Generated with Claude Code

… is left with nothing (D3D12-COLD-START-SKIPS)

The shared frame budget was a reservation: the first recording thread to
wait in a frame took all that was left of it, and every other thread of
that frame (and any thread still on an older frame) got nothing, so its
draw went out without a wait and was dropped as not made. On first starts
(no pipeline book) 400 to 3,600 draws, dispatches and indirect executions
a frame were dropped at the arrival in the world, and the character's
head, drawn by a pass the title runs once, stayed missing for the run.

- The frame's waits are counted once over the threads that wait at the
  same time: what passed while at least one thread waited. A thread that
  begins to wait while others do is left what is left at that count, so
  waits that overlap still cost the frame its budget once; the time a
  thread records between two waits is not counted; a thread on an older
  frame joins the current frame's waits and opens no budget of its own
  (ProjectLeonida#2601). On by default; LEONIDA_PIPELINE_WAIT_DEADLINE=0 reserves the
  frame's budget as before (no effect with LEONIDA_PIPELINE_WAIT_SHARED=0);
  LEONIDA_AB can turn it while the title runs.
- A pipeline's own budget (LEONIDA_PIPELINE_WAIT_ONCE) is charged the same
  way, the real time once, not the threads' times added up, whatever the
  sharing of the frame's budget (LEONIDA_PIPELINE_WAIT_ONCE=0 turns that
  budget off).
- The queue a wait moves its pipeline forward in and the budget a
  pipeline's waits count against are passed in, so that tests cover them;
  one line tells the rules in use at the first wait.

AI-assisted: Claude Code (Claude Opus 5.5)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@andrew-ai

Copy link
Copy Markdown

The shared deadline can refund time that was actually spent waiting when two waiters report completion in reverse timestamp order. At 7c69bc8 I compiled the PR's FrameWait and Waited unchanged and exercised take/give_back on one frame with a 100 ms budget:

Wait intervals Order of give_back Next take at 86 ms
A 0–20 ms, B 10–85 ms A then B 15 ms
Same B then A 80 ms

The union is 85 ms in both cases. wait_in samples now before locking the accounting mutex, so A can capture 20 ms, be descheduled, and report after B. Waited::end uses only the timestamp of the caller that decrements waiting to zero. This affects both frame and per-pipeline accounting. For this case, retaining the latest end timestamp across the active group and testing reversed completion order would prevent the 65 ms undercharge. This is a deterministic CPU accounting reproduction; I did not measure its frequency during gameplay. Codex-assisted.

@merzleckeier

Copy link
Copy Markdown
Contributor

A delayed end report can also count the gap between two disjoint waits.

I compiled the exact Waited and FrameWait implementations from 7c69bc8b1 merged onto 913b88b90. With a 100 ms deadline budget on one frame, schedule A from 0 to 10 ms and B from 40 to 50 ms. Call take(A, 0), take(B, 40), give_back(A, captured 10), give_back(B, 50), then take at 51 ms. This matches the ordering where A captures its completion timestamp before the mutex, B enters while A is descheduled, and A reports before B finishes.

Result Elapsed wait union Current code
Grant to B at 40 ms 90 ms 60 ms
Budget left after both completions 80 ms 50 ms
Time charged to one pipeline, if both wait for it 20 ms 50 ms

Reporting A before B starts gives the expected 90/80/20 ms. The two completion reports in the failing case are already in timestamp order. Testing overlapping waits in reversed completion order therefore covers a different case. The gap is charged because Waited still treats A as active when B starts and combines both into a single group. This was a deterministic timestamp test of the accounting code. AI-assisted with OpenAI Codex and independent raw-result verification.

@OreoFR

OreoFR commented Dec 1, 2026

Copy link
Copy Markdown
Collaborator

First-start RUN of head 7c69bc8 merged onto main 913b88b (tree ceaf7f729). RTX 4070 Laptop 8 GB, NVIDIA 621.40, Windows 11, -SeriesS -NoDlss -NoFrameGen, BAR 0, texture budget 1024, fresh data folder with no pipelines (every pipeline compiles on arrival), default switches.

  • In the world at 74 s; pictures looked at (not posted) at +60, +150 and +280 s.
  • Head present: at +280 s the character's head, hair and neck skin are drawn. My earlier cold first start on main 0ff8a53 (same scene, same settings, no pipelines) had only floating strands of hair and no face or skin. n=1 on each side; I did not run the =0 arm on this exe.
  • Cost to know about: while the pipelines compile, frames are longer than on main. +60 s: 5.18 s/frame here vs 3.73 s on main 0ff8a53; +150 s: 5.21 s vs 2.01 s. The window shows "not responding" during that phase. From +280 s it is normal (37.6 ms, 27 fps), with 0 device lost.
  • No overlap with d3d12: a pipeline a frame waits for goes before those asked for earlier (D3D12-PIPELINE-WAIT-QUEUE) #2622 (PIPELINE-WAIT-QUEUE, also in make.rs): it is already merged, and this pull request merges cleanly on top of it.
    AI-assisted: Claude Code (oreofr-agent).

@OreoFR

OreoFR commented Dec 2, 2026

Copy link
Copy Markdown
Collaborator

Red/green RUN on one exe: head 7c69bc8 merged onto main 913b88b (tree ceaf7f729). RTX 4070 Laptop 8 GB, NVIDIA 621.40, Windows 11, -SeriesS -NoDlss -NoFrameGen, BAR 0, texture budget 1024, each run on a fresh data folder with no pipelines, 330 s in the world, pictures looked at (not posted) at +60, +150, +280 and +320 s.

  • Red, LEONIDA_PIPELINE_WAIT_DEADLINE=0: the head is broken for the whole run: floating hair strands and no face or skin at +150, +280 and +320 s, although the frame was back to 36-42 ms from +150 s.
  • Green, default (no variable): head, hair and neck skin drawn at +280 and +320 s.
  • Both: 0 device lost, no panic. The default arm has the longer first-start frames I reported above (5.2-5.7 s at +60 and +150 s, window "not responding"), then 39-41 ms.
  • Together with my first default run: head present 2/2 with the fix, missing 2/2 without it (=0 here, and main 0ff8a53 earlier).

Safe to merge into main? Yes: merge-tree clean on main 7adb42f (main gained #2857, #2852 and #2991 since my test, none touches pipeline/), CI green on the head, on by default with =0 as the way back, claim D3D12-COLD-START-SKIPS in #2570, scope pipeline/make.rs and switches.md.

AI-assisted: Claude Code (oreofr-agent).

@OreoFR

OreoFR commented Dec 2, 2026

Copy link
Copy Markdown
Collaborator

Merging (added to the merge queue). Head 7c69bc8 merges cleanly with main 7adb42f. Claim: D3D12-COLD-START-SKIPS (#2570 (comment)). Red/green: oreofr-agent on an RTX 4070 Laptop, same exe, =0 head missing vs default head present (RUN above); the author's own ABAB on an RTX 3090. I ran: three first starts on fresh data folders with no pipelines, 330 s each, 0 device lost. CI: green on head and main; the queue runs the full CI before merging. AI-assisted: Claude Code (oreofr-agent), merge approved in writing by OreoFR.

@OreoFR
OreoFR added this pull request to the merge queue Dec 2, 2026
@enlight3d

Copy link
Copy Markdown
Contributor Author

Thanks @andrew-ai and @merzleckeier: both cases are real. wait_in reads the clock for the end of a wait before it takes the accounting locks (let now = Instant::now();, then p.waits and waits). A thread held between the two reports an end older than what the count has seen since. In the first case that refunds time that was spent; in the second it counts the gap between two waits that did not overlap. It touches both the frame's shared count and a pipeline's own count. It only happens when a thread is held between reading the clock and taking the lock, and it changes how much of the budget is left, not whether a draw may wait.

A follow-up pull request reads the clock under the accounting lock and keeps a late end from moving the count back, with both of your schedules as tests (red on this head, green with the change), for the frame's count and for a pipeline's own. Since this one is already in the merge queue, the follow-up will go on top of main rather than hold it up.

Separately, the cost OreoFR measured on the 4070 Laptop belongs in the record: while the pipelines compile on a first start, frames are longer than on main (5.2 s against 3.7 s and 2.0 s) and the window reports "not responding" until about +280 s. On a first start most of that compile time goes to the 1,034 programs of d3d12/scripts/rt_off_hashes.txt that the launcher runs with every ray a miss (71 to 74 % of the time spent on pipelines of a second or more, two runs on the RTX 3090); that is being looked at separately.

Merged via the queue into ProjectLeonida:main with commit ce08fea Dec 2, 2026
6 checks passed
@enlight3d

Copy link
Copy Markdown
Contributor Author

Follow-up for the two counting cases above: #3032. It reads the time of each begin and end of a wait under the lock of the count it goes into, and Waited no longer lets an earlier end give back time. Both of your schedules are tests there, red on this head and green with the change, for the frame's count and for a pipeline's own. One limit stays, by design: a wait counts until its end is timed, so a thread stopped after its wait and before reading the clock still counts as waiting meanwhile.

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