Skip to content

d3d12: a pipeline a frame waits for goes before those asked for earlier (D3D12-PIPELINE-WAIT-QUEUE) - #2622

Merged
MoonEyes merged 3 commits into
ProjectLeonida:mainfrom
TheGeogeo:d3d12-pipeline-wait-queue
Nov 30, 2026
Merged

MoonEyes merged 3 commits into
ProjectLeonida:mainfrom
TheGeogeo:d3d12-pipeline-wait-queue

Conversation

@TheGeogeo

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: D3D12-PIPELINE-WAIT-QUEUE (#2570 (comment))

Depends on: #2601 (it touches the same function; this change avoids its lines, and git merge-tree merges the two cleanly)

What and why

When a draw's host pipeline is not made yet, ready() queues it at the back of the requested list, and the frame then waits for it up to the frame's budget (LEONIDA_PIPELINE_WAIT_MS; the launcher sets 10,000 ms).

I measured this on an RTX 3080 (main 7daa4a4a5, first visit to the world, empty pipeline cache, 8 workers):

  • about 1,700 pipelines were queued at once, and 562 of them took over 1 s each;
  • every frame waited its whole 10 s for a pipeline that a thousand others stood before, then dropped its draws anyway;
  • 34 waits expired, and the game ran at 0.35 fps for 3.5 minutes.

This change adds a queue state, AWAITED, between REQUESTED and MAKING:

  • When a frame starts to wait for a pipeline still in the queue, Queue::promote moves that entry to the front of its list, after those awaited before it. That is the requested list, or the screen list for passes over the screen.
  • An entry moves at most once and is never claimed twice. WAITING does not count it again, and a make a worker has already started is not interrupted.
  • A frame that waits again for a pipeline already moved, or being made, does not take the queue's lock.
  • LEONIDA_PIPELINE_AWAIT_FIRST=0 keeps the order asked, as before.

What the claim also proposed and this change does not do: skipping a wait that "cannot succeed". The only build time the runtime records per pipeline is the cold compile, in the slow map. leonida warm --used puts those pipelines in the driver's cache before the game starts, so a rule based on that time would skip waits that would have succeeded.

Frames on a cold cache can still wait up to the frame's budget. The waits now succeed, so a frame may spend its budget on several pipelines it then draws, rather than on one it drops. The budget setting bounds that, as before. The measurements below show both sides of this.

Tested

On Windows 11, from d3d12\:

  • Three new tests in pipeline::make::tests. They fail on main, where promote/AWAITED do not exist and the order is first in, first out.
    • an_awaited_pipeline_is_made_before_those_asked_earlier
    • an_awaited_early_pipeline_is_claimed_once
    • an_awaited_pass_over_the_screen_goes_first_among_them
  • cargo test -p leonida-gpu --locked: 540 passed. The pipeline::make tests also pass with --features diagnostics.
  • cargo clippy -p leonida-gpu --all-targets --locked: nothing in make.rs. rustfmt with d3d12/rustfmt.toml: nothing in the changed lines.
  • git diff --check is clean, and so is check_no_game_data.py.
  • An independent review found no race. Every move from REQUESTED to AWAITED to MAKING happens under QUEUE, and the lock order and wakeups are unchanged.
  • In game: I could not clear the driver's shader cache on this machine, so I simulated a cold cache. A local patch, not part of this pull request, made every pipeline take 300 ms longer, identically on both sides. Each run started from a fresh pipeline cache with the shader cache kept, went from the menus to the world, then ran 180 s of a scripted cutscene. Frame times come from PresentMon 2.6 and dropped draws from the frame lines. Each side was run once per setting.
setting side waits expired draws dropped (sum over the frame lines) worst frame's drops frames over 1 s longest frame min fps
Series X 1440p, WAIT_MS=10000 (launcher default) main 2 21,267 3,162 6 8.6 s 0.2
this change 0 21,152 3,402 18 8.7 s 0.3
Series S, WAIT_MS=1000 main 40 39,720 11,299 0 0.96 s 0.2
this change 0 29,833 3,055 4 1.25 s 1.5

Reading: no wait expires any more. With a 1 s budget, 25 % fewer draws are dropped, the worst frame drops 73 % fewer, and the slowest second is 7 times faster. With the launcher's 10 s budget, drops stay about the same, but more frames wait several pipelines up to the budget. That is the budget's doing: this change makes no wait longer, it only lets more of them succeed. Both sides drained the queue (about 1,600 pipelines) in the same time, about 80 s.

Not yet measured: a real cold driver cache. Two in-game A/B runs on other machines (RTX 4070 and RTX 3090, the latter with the driver's cache cleared) are planned with this branch; their numbers will follow in a comment.

GPUs tested: NVIDIA GeForce RTX 3080 10 GB, driver 32.0.16.2140, Windows 11 Pro 10.0.26300.

AI-assisted: yes

Checklist

…er (D3D12-PIPELINE-WAIT-QUEUE)

A draw whose host pipeline is not made queues it at the back of the
requested list, and its frame then waits for it up to the frame's budget.
On the first visit to the world about 1,700 pipelines were queued at once,
562 of them took over a second each, and with the launcher's 10 s budget
every frame waited the whole budget for a pipeline a thousand others stood
before, then dropped its draws anyway: 0.35 fps for three and a half
minutes (RTX 3080, 8 workers).

A new queue state, AWAITED, sits between REQUESTED and MAKING. When a frame
starts waiting for a pipeline that is still in the queue, the entry moves
to the front of its list (passes over the screen in theirs), after those
awaited before it, so the next free worker makes it. It moves at most once,
is never claimed twice, and WAITING does not count it again; makes already
running are not interrupted. LEONIDA_PIPELINE_AWAIT_FIRST=0 keeps the order
asked.

Frames on a cold cache still wait up to the frame's budget: the waits now
succeed, so a frame may spend its budget on several pipelines it then draws
instead of one it drops. The budget setting bounds that, as before.
@bobcatchris

Copy link
Copy Markdown
Contributor

Independent native Windows 11 + AMD Radeon RX 7900 XTX (gfx1100, driver 32.0.32029.2010, Windows 11 10.0.26200) check of ffd3681b2 merged cleanly onto main 4755667f5 (no game files):

  • Baseline (main 4755667f5): cargo test -p leonida-gpu --lib -- --test-threads=4 -> 544 passed; 0 failed; 47 ignored (12.27 s).
  • PR #2622 (ffd3681b2) + main 4755667f5: cargo test -p leonida-gpu --lib -- --test-threads=4 -> 547 passed; 0 failed; 47 ignored (13.86 s, +3 new pipeline::make tests, 0 regressions, 0 new compiler warnings).
  • python -I scripts/check_no_game_data.py d3d12/crates/leonida-gpu/src/pipeline/make.rs: clean.

AI-assisted: Antigravity (Gemini 4 Neon).

@merzleckeier

Copy link
Copy Markdown
Contributor

Independent check of ffd3681b with #2601 23360208 on main 7c330c76. The three new priority tests passed in a Windows x64 diag cross-build under Wine 11.22.

I found a wait-budget gap under queue contention. A test-only hook after the actual wait() reserves its frame budget let the fixture hold QUEUE for 240 ms with an 80 ms frame budget. Three runs per arm, one pending pipeline, no worker or GPU work.

Arm Total wait, ms Wait after releasing QUEUE, ms
Promotion off 80.073 / 80.061 / 80.069 returned while lock held
PR default 320.171 / 320.172 / 320.191 80.072 / 80.060 / 80.080
Timer started before promote(p) 240.141 / 240.124 / 240.139 0.011 / 0.009 / 0.024

wait() currently starts its timer after promote(p) acquires QUEUE. Moving let started = Instant::now() just above the if await_first() block kept priority promotion and all three priority tests intact. It also charged the lock delay against the condition-variable wait. The frame still waited for the lock holder in the forced-contention fixture.

This is a synthetic lock-contention test. AI-assisted with OpenAI Codex.

@OreoFR

OreoFR commented Nov 29, 2026

Copy link
Copy Markdown
Collaborator

RUN - D3D12-PIPELINE-WAIT-QUEUE A/B (PROVISIONAL) - OreoFR, RTX 4070 Laptop 8 GB, NVIDIA 621.40, Windows 11.

Build ffd3681b2 on main bd643e46e, diag profile, one leonida.exe for every run. A = AWAIT_FIRST=0, B = PR default. Cold pipeline cache per run (fresh data folder); launcher defaults in both arms (frame generation 2x, LEONIDA_WINDOWS_BAR_MB=3072). About 4 min in the world per run, measured from the first frame line with 100+ draws.

Run wait budget expired waits dropped draws frames > 1 s max being made (end) fps median end
A1 10 s 5 ~21.4k 3 842 (838) 0.1 title crash ~85 s into the world (no device loss)
B1 10 s 1 ~174k 26 1,104 (150) 0.3 time limit
A2 10 s 44 ~125k 23 1,106 (852) 0.2 time limit
B2 10 s 0 ~144k 26 1,251 (846) 0.2 time limit
A3w 1 s 455 ~661k 1 1,422 (911) 1.8 time limit
B3w 1 s 323 ~462k 4 1,155 (651) 1.8 time limit
  • Expired waits: B cuts them as intended (10 s: 5/44 → 1/0; 1 s: 455 → 323).
  • Dropped draws and frames over 1 s at the 10 s budget: no gain from B on this 8 GB laptop. At 1 s, B drops about 30 % fewer draws.
  • The pipeline queue never fully drained in the world in either arm. No device removal or panic in any of the 6 runs. VRAM ended at or over budget (6.7-7.1 GB) in every run that lasted.
  • Confounder: a parallel C++ build ran during A2, B2, A3w and B3w (CPU mean 89-91 % against 77-80 % for A1/B1). B1's queue drained to 150 with no build running, which points to CPU load more than to the arm.
  • n: 2 per arm at 10 s (A1 short because of the crash), 1 per arm at 1 s. PROVISIONAL.

Verdict: the change does what it says on expired waits, with no regression seen (no device loss, no new crash in B). It brings no fps or stutter gain on an 8 GB 4070 Laptop at the default budget.

This run was AI-assisted (Claude Code, oreofr-agent).

@hardly-relevant

Copy link
Copy Markdown
Contributor

RUN · VERIFY-2622-RTX4080-LAPTOP · PROVISIONAL (A/B/A′)

Windows 11 build 26300, RTX 4080 Laptop GPU (12 GB, profile M), NVIDIA 621.40, 32 GB RAM, Balanced power plan. One diagnostics build from main dd87aa405 (includes #2601), with #2622 at ffd3681b2, #2623 for stall captures, and #2613 for frame-time statistics. Series X 2560×1440, 2× frame generation capped at 28 shown FPS, LEONIDA_PIPELINE_WAIT_MS=1000, LEONIDA_LOG=info. Each run started from a copy of the original save and a precompiled application cache. The NVIDIA driver cache was kept across runs.

First ~151 s after the first logged 3D rendering marker A · promotion off B · promotion on A′ · promotion off, run last
Frame intervals over 1 s 5 1 0
Longest frame interval 1.20 s 1.07 s 0.38 s
Expired ordinary 1,000 ms pipeline waits 33 2 0
Mean dropped work per frame due to pending pipelines, sampled 3.76 0.05 0.01
Peak pipelines queued or building, sampled 325 1 2

Dropped work includes draws, dispatches and indirect executions. The tester reported the same save and a roughly matched driving route for A and B. They waited near spawn for the first backlog to clear in A, could drive immediately in B, and reported that A′ felt much closer to B. All three runs exited normally. A′ logged one pipeline-library save failure due to disk space near exit. Its telemetry and stall capture were retained.

Verdict. B improved on the initial A, and A′ reproduced B’s low backlog with promotion off. The driver cache could warm across runs, time before world entry varied substantially, and the driving route was approximate. The size of the promotion benefit on this machine remains unresolved.

Queue-lock timing. The tested code starts its timer after promotion can acquire the queue lock. Our rolling stall capture did not identify waits on that lock, so this gameplay test cannot establish whether that extra delay occurred. The synthetic contention test isolates that case.

AI-assisted with OpenAI Codex.

…its for (review)

wait() started its timer after promote() took the queue's lock, so time
spent waiting for that lock came on top of the frame's budget: in a
synthetic contention test (queue held 240 ms, 80 ms budget) a frame waited
320 ms. The timer now starts before the promotion, so the lock delay is part
of the budget (240 ms in the same test, the lock holder's time itself).
Reported with the test by merzleckeier.
@TheGeogeo

Copy link
Copy Markdown
Contributor Author

Thanks for the checks and the runs.

  • Lock timing (merzleckeier): you are right. wait() started its timer after promote() had taken the queue's lock, so a lock delay came on top of the frame's budget. Fixed in 61c5b18d8: the timer now starts before the promotion. The three priority tests still pass (cargo test -p leonida-gpu --locked pipeline::make: 10 passed). The branch merges cleanly with main 1d22590b9 and with runtime, d3d12: memory leak fixes from one sweep (RT-LEAK-SWEEP) #2569 and GPU: pipeline workers get a 64 MB stack for the driver's compiler #2588.
  • What the A/B runs say:
    • OreoFR's 4070 Laptop and the RTX 4080 Laptop runs agree with my simulation on the part the change controls: fewer expired waits. That is 5/44 → 1/0 at the launcher's 10 s, 455 → 323 and 33 → 2 at 1 s.
    • Dropped draws and long frames only improve with the shorter budget: about 30 % fewer drops at 1 s, none at 10 s.
    • The RTX 4080 run's A′ reproduces B once the driver cache has warmed, so the size of the benefit on a warm driver cache is still open.
    • No device loss and no new crash with promotion on.
  • Reading: with promotion, waits succeed, so the launcher's 10 s budget is what lets frames chain long waits. The budget setting decides between stutter and missing draws, and promotion makes a smaller budget pay off. I am not changing the launcher's default in this pull request.

@MoonEyes

MoonEyes commented Nov 30, 2026 •

Copy link
Copy Markdown
Collaborator

RUN · VERIFY-2622-RTX4060 · by MoonEyes (another person than the author) on head 46688881e merged with main be454caa4. Windows 11 Pro 26300, RTX 4060 8 GB, -SeriesS -FrameGen 2, diag build, warm pipeline cache.

  • cargo test -p leonida-gpu --lib --locked: 574 passed, 0 failed.
  • Game: one run of 480 s, the person walking in the world from 345 s (about 24 m; then stopped against something), no device loss, no unhandled exception, the process alive at the end. PASS for the gate of 300 s.
  • Not shown here: the effect. It comes from a cold pipeline cache (OreoFR has announced an A/B for that), and other work ran on the PC, so no frame rate is claimed.

AI-assisted (Claude Code).

Correction (same run): the pipeline cache was not warm. The build was the first with #2710, so the shaders were translated again (780 s before the title) and frames stayed under 5 fps until about 350 s after the start; from then on about 250 s of steady frames (median 16.7 fps shown, other work on the PC). The title was in the world from about 160 s, and nothing failed over the whole 616 s.

@OreoFR

OreoFR commented Nov 30, 2026

Copy link
Copy Markdown
Collaborator

RUN - cold-cache A/B (Verified by OreoFR), MEASURED on expired waits, PROVISIONAL on the rest - RTX 4070 Laptop 8 GB, NVIDIA 621.40, Windows 11.

  • Base: main 08e42a516; B = main + this PR at 46688881e (git merge --no-ff, clean), diag build of leonida.exe.
  • Recipe: play-d3d12.ps1 -SeriesS -NoDlss -NoFrameGen, BAR 0, texture budget 1024, the launcher's 10 s wait budget. Each run stands, then walks with W held from lift 3 (game actions) for 150 s, about 6.5 min in the world. Order A B B A, then one more pair B A: 3 runs per arm.
  • Cold, every run:
    • A new -Data folder with no cache-2\pipelines, so no pipeline library or book.
    • leonida.exe started from a new folder. A probe without the game showed that the NVIDIA driver's cache keys its entries on the executable's path: the same file copied to a new path compiled cold (34 ms against 0.4 ms warm).
    • DXCache was not cleared.
    • Shader translations, the image cache and the start save were the same warm copies in both arms.
Arm runs expired waits time waited for pipelines dropped draws frames >= 1 s peak being made first "all made" fps, first 120 s in world fps walking: median / p5
main 3 37, 37, 36 493 s (490-495) ~606k 24 854 +211 s 0.2 24.0 / 3.0 (20.6-28.9)
main + #2622 3 1, 1, 0 371 s (349-391) ~678k 23 1,109 +246 s 0.4 19.6 / 2.6 (16.1-22.3)
  • Expired waits: the change does what it says. On a real cold cache at the default budget there were 36-37 per run on main and 0-1 with the PR, with no overlap.
  • Time frames spent waiting for pipelines: -25 % (copystat "ms waiting for pipelines", summed over the world).
  • No gain in what the player sees in the first minutes:
    • dropped draws, frames over 1 s and fps in the first 120 s in the world are the same within noise;
    • both arms run at 0.2-0.5 fps there, with 20-25 frame lines of 1 s or more per run.
  • The backlog drains a little later with the PR (PROVISIONAL):
    • the queue peaks higher (1,054-1,188 against 801-896) and is first all made about 35 s later;
    • the walking window right after has a lower fps median (19.6 against 24.0), but the ranges overlap and the routes differ from run to run.
  • Stability: 0 device loss, 0 panics, every run alive at the end.
  • Not covered: the 1 s budget where the PR reports fewer drops, Series X, DLSS and frame generation, and other cards. A driver cache level that does not depend on the path is not ruled out.

This run was AI-assisted (Claude Code, oreofr-agent).

@MoonEyes

Copy link
Copy Markdown
Collaborator

Merging. Head 46688881e merges cleanly with main afb511541 and does not force a rebase of #2566. Run in the game of 480 s without device loss on RTX 4060 and six cold-cache runs on RTX 4070 Laptop (expired waits 36-37 to 0-1, no regression seen, no visible gain either) on this head; cargo test 574 passed. Merged by MoonEyes.

AI-assisted (Claude Code).

@MoonEyes
MoonEyes merged commit ad36782 into ProjectLeonida:main Nov 30, 2026
5 checks passed
djrolfes added a commit to djrolfes/leonida that referenced this pull request Nov 30, 2026
… 1D wave-base (ProjectLeonida#2685), NaN-exact, by_due texture queue, unlocked pool growth (ProjectLeonida#2693), pipeline wait queue (ProjectLeonida#2622)
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.

6 participants