Skip to content

d3d12: keep pipeline wait budgets from moving backward - #2601

Merged
MoonEyes merged 1 commit into
ProjectLeonida:mainfrom
louisdibiasi:fix/d3d12-stale-frame-wait-budget
Nov 29, 2026
Merged

MoonEyes merged 1 commit into
ProjectLeonida:mainfrom
louisdibiasi:fix/d3d12-stale-frame-wait-budget

Conversation

@louisdibiasi

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: Keep an older frame request from resetting the Rust D3D12 pipeline wait budget

What and why

A recording thread can sample the frame number and reach the pipeline wait lock after a newer frame has already spent its budget. Resetting the budget on every frame-number change lets a delayed request for frame 41 reopen a spent budget for frame 42. Give stale requests zero wait time and keep the currently tracked frame's accounting intact.

Tested

  • A deterministic CPU regression covers frame 0, a delayed request, a repeat request for the current frame, and the next frame in both shared and non-shared wait modes. With the original logic the delayed request and the repeat request each get 500 ms. With the fix both get zero and the next frame gets its normal allowance.
  • All eight pipeline::make::tests and the frame counter test passed on Windows 11 with Rust 1.98.1. cargo check --locked --offline --profile diag -p leonida --features diagnostics and the changed-file game-data scan passed.
  • Gameplay impact is unmeasured. If a maintainer has a repeatable pipeline-compilation workload, a matched A/B with frame generation off would help check rendered frame times and draw completeness.

GPUs tested: n/a (CPU-only regression and application check).

AI-assisted: Yes, with OpenAI Codex (GPT-6).

Checklist

  • Based on current main and merges cleanly.
  • One topic.
  • No game data, keys, or decryption.

Skip waiting when a recording thread arrives with an older frame number. Otherwise the older request can reset the budget and let the newer frame spend it again.
@lixonik

lixonik commented Nov 29, 2026

Copy link
Copy Markdown
Contributor

Reviewed head 2336020, merged with main c8bf0e7. All 8 pipeline::make unit tests passed in leonida-gpu (Windows 10, Rust 1.100.1).

I also compared the reservation logic in isolation with a partly spent budget, in both shared and non-shared modes. With 60 ms already charged against a 100 ms limit for frame 42, the old main logic granted frame 41 another 100 ms. The updated logic granted frame 41 0 ms and left frame 42 with 40 ms. The next frame got the full 100 ms. CPU-only, no game files.

AI-assisted. Codex (GPT-6).

@zmasi

zmasi commented Nov 29, 2026

Copy link
Copy Markdown
Contributor

#2601 | head 2336020 (merged cleanly onto origin/main c8bf0e7) | Windows 11 (10.0.26200.9974), rustc 1.96.0 (ac68faa20 2026-05-25), MSVC 19.44.35228 (x64) | AI-assisted: yes

Summary — PASS

Verified 233602088041c9c2c75f724dbad187474e2cb286 merged cleanly onto origin/main (c8bf0e7b6568f471b42540010e335ca6413d4c58, git merge-tree --write-tree exit 0) on native Windows 11 (x64).

Verification Details

  1. Root cause (d3d12/crates/leonida-gpu/src/pipeline/make.rs):
    • On origin/main, SPENT initialized to (u64::MAX, Duration::ZERO) and reset whenever spent.0 != frame, so a recording thread that sampled frame = N - 1 before acquiring spent_of after another thread already advanced spent.0 to N reset *spent = (N - 1, Duration::ZERO) (and the next N waiter reset it back to (N, Duration::ZERO)), reopening the full per-frame pipeline wait budget.
  2. Build & Test (CARGO_BUILD_JOBS=4, --test-threads=4):
    • cargo test -p leonida-gpu --lib pipeline::make::tests -- --test-threads=4: test result: ok. 7 passed; 0 failed; 0 ignored; 0 measured; 564 filtered out; finished in 0.51s on origin/main -> test result: ok. 8 passed; 0 failed; 0 ignored; 0 measured; 564 filtered out; finished in 0.51s (13.75s) with #2601, including pipeline::make::tests::older_frames_cannot_reopen_a_pipeline_wait_budget ... ok across both shared = true and shared = false.
    • cargo check -p leonida-gpu --tests -> 0 errors, 0 warnings.

@timbot103 timbot103 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed head 233602088. The guard in d3d12/crates/leonida-gpu/src/pipeline/make.rs:443 gives older frame requests zero wait before changing the recorded frame or spend. The mutex serializes comparison, reset, and reservation for each budget. Saturating subtraction and the per-pipeline cap keep shared reservations within the remaining allowance, and completion accounts for the wait only if its frame still owns the budget.

The waiting path samples the process-wide unsigned frame counter in d3d12/crates/leonida-gpu/src/pipeline/make.rs:379. The counter starts at zero and advances independently of the pacing clock in d3d12/crates/leonida-gpu/src/frame.rs:76 and d3d12/crates/leonida-gpu/src/frame.rs:217. The relaxed atomic is sufficient for the frame number because the mutex protects the budget state. The patch reuses the existing Rust types and standard-library operations.

The regression at d3d12/crates/leonida-gpu/src/pipeline/make.rs:757 covers frame zero, a stale request after spending the current frame's budget, a repeat current-frame request, and the next frame in both wait modes.

Static audit only. No builds, tests, or GPU runs performed.

AI-assisted.

@OreoFR

OreoFR commented Nov 29, 2026

Copy link
Copy Markdown
Collaborator

Verified by OreoFR — READY

  • Base: main c8bf0e7 + this PR (head 2336020) merged with --no-ff
  • Red (main + only this PR's tests): main's inline budget logic, extracted unchanged into a reserve_frame_wait so the PR's test can call it: older_frames_cannot_reopen_a_pipeline_wait_budget fails (left: (500ms, 500ms), right: (0ns, 0ns)), so an older frame reopens the budget. The other 7 pipeline::make tests pass.
  • Green (merge): cargo test -p leonida-gpu --lib pipeline::make in d3d12/: 8 passed, 0 failed
  • Diff read: no issue. SPENT has to start at 0 now that frame < spent.0 returns zero, and the end-of-wait accounting is unchanged.
  • Other runs: Confirms lixonik's run (8/8 on the same head and main).
  • Machine: Windows 11, Intel Core Ultra 7 155H, RTX 4070 Laptop 8 GB, NVIDIA 621.40, Python 3.12.10, MSVC 19.44

AI-assisted (Claude Code, Claude Opus 5.5), run for OreoFR.

@merzleckeier

Copy link
Copy Markdown
Contributor

Linux game-run follow-up for 233602088. I compared 5844b6481 with the same base plus this PR on a desktop RTX 4070 SUPER 12 GiB (NVIDIA 620.73.04, CachyOS, Wine 11.22, VKD3D-Proton 71cae091, DXVK DXGI). Both runs were offline in Series S with frame generation off, the same input, copies of the same warm caches, and 500 ms for ordinary pipelines / 10 s for screen passes.

90–135 s after launch Main PR
Median rendered FPS 1.15 0.8
Median draws/frame 230 158
Highest logged interval mean of pipeline wait/frame 502.7 ms 175.4 ms

The opening-sequence title card was sampled on main only. The sampled images and draw counts differ, so the numbers do not establish a same-scene performance effect. The PR test harness ended early after 163 s and stopped the runtime. A previous cold A/B on 54b2a81e9 with the launcher's 10 s ordinary budget still showed waits around 10 s on both builds.

A separate test on newer main found a Series S presentation fallback in this Wine setup. I verified the compositor portion of #2577 separately and posted that result there. A visible open-world scene is still unverified. AI-assisted with OpenAI Codex (GPT-6).

@merzleckeier

Copy link
Copy Markdown
Contributor

Follow-up with a visually matched opening marker on Linux. I tested b589e0fe0 + the existing DXC compiler branch 9075a5e419 against the same build plus #2601 at 233602088. Both used app-local DXC v1.9.2609, the same launcher and input, copies of the same warm caches and the 500 ms ordinary pipeline wait budget. Series S, Wine 11.22, VKD3D-Proton 71cae091, DXVK DXGI, RTX 4070 SUPER, NVIDIA 620.73.04, CachyOS.

40 s from first sampled frame of the same opening title card Base + DXC Base + DXC + #2601
Log intervals 6 7
Median rendered FPS 0.9 1.3
Median draws/frame 1,165 960
Highest logged interval mean of pipeline wait/frame 503.5 ms 572.5 ms

Both runs reached the visible 3D opening sequence and exited normally. The title card was first captured at 125 s and 130 s with a five-second capture cadence. One run per arm and differing draw loads leave the effect on FPS unresolved. This run did not show a reduction in the largest logged pipeline wait interval mean.

The app-local compiler was needed to get comparable visible scenes. In a separate same-binary control on this base, adding only the official DXC DLL pair changed the max logged indirect clamp counter from 0 to 268 per frame and eliminated the two clamp creation failures. Compiler-side reproduction and GPU tests. AI-assisted with OpenAI Codex (GPT-6).

@enlight3d

Copy link
Copy Markdown
Contributor

RUN: PR #2601 (pipeline wait budgets cannot move backward)

Heads: PR 2336020, main eedd72e (current origin/main at the time; main had moved from 4364a4f during the job). Merged locally with git merge --no-ff (clean, make.rs auto-merged, nothing pushed). No conflict with #2566.

Command (d3d12/, MSVC target, vcvars64): cargo test -j 6 -p leonida-gpu --lib (and older_frames alone for the red), cargo check -j 6 -p leonida-gpu --tests.

  • RED (behavioural, the PR's test kept: reserve_frame_wait put back to the old rule, any frame different from the stored one resets the budget): older_frames_cannot_reopen_a_pipeline_wait_budget FAILED at make.rs:805 (left: (500ms, 500ms), right: (0ns, 0ns)): a delayed older frame got a fresh budget. Restored afterwards. (Reverting only the non-test code is a build failure: the test calls the new function.)
  • GREEN (merged tree): the new test passes; whole --lib suite 545 passed, 0 failed, 47 ignored. Plain main: 544 passed, 0 failed, 47 ignored (the difference is the new test).
  • cargo check -p leonida-gpu --tests on the merged tree: clean.

Machine: RTX 3090 24 GB, NVIDIA 621.40, Windows 11 (native D3D12 target).

@MoonEyes

Copy link
Copy Markdown
Collaborator

Merging. Head 233602088 has red/green verdicts from reviewers other than the author (above), merges cleanly with main 7c66bc52e, does not force a rebase of #2566, and carries a claim ID. Remaining main CI reds (translator-tests mesh, semantic-tests f16) are pre-existing. Merged by MoonEyes.

AI-assisted (Claude Code).

@MoonEyes
MoonEyes merged commit 38e5ee7 into ProjectLeonida:main Nov 29, 2026
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.

8 participants