Skip to content

D3D12.X: clamp PcCalls across frame boundaries and drop orphaned PSO jobs immediately - #2296

Open
Decodere wants to merge 1 commit into
xcl-dev:mainfrom
Decodere:fix-1782-2178-pso-async-frame-orphan
Open

Decodere wants to merge 1 commit into
xcl-dev:mainfrom
Decodere:fix-1782-2178-pso-async-frame-orphan

Conversation

@Decodere

@Decodere Decodere commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

What is in it

Follow-up to #1782 and #2178 fixing two lifetime/timing bugs in crates/xcl/src/hle/d3d12x/pso_async.rs:

  1. Clamp PcCalls across monotonic frame boundaries: PcCalls now tracks frame: Option<u64> and frame_since: Option<Instant>, recording frame_since = Some(now) on monotonic frame advances (frame > prev in begin(), end(), and holds(), with pc_call() reading present::presents() under PC_CALLS.lock() like held_by_pc_call() and PcCall::drop()) and clamping call start times via since.max(frame_since). A long PC call that began on frame N and finishes after frame N+1 is presented no longer charges its pre-frame-N+1 duration into frame N+1's PC_CALL_HOLDS_AFTER (250 ms) budget, stale lower frame numbers never rewind frame or clear spent, and a call genuinely stuck >= 250 ms after a frame still holds the title.
  2. Immediate outside-lock drop of unstarted orphaned Job closures: Added drop_unstarted_job(&Pending) and call it from released() (after releasing PENDING and p.done locks) as well as the preload() and enqueue() worker p.orphan checks. When released() claims p.job before a worker takes it, the unstarted Job closure (with its captured COM objects and shader bytecode buffers) is cancelled and dropped immediately outside all locks rather than staying pinned until background workers drain deep queues, and *p.done = true + p.cv.notify_all() wakes any concurrent waiter (a worker that already passed the p.orphan check and claims p.job first still runs and discards its result once in complete()).

(Rebased onto origin/main 06c9f550 to adapt cleanly to #2080's Work queue / enqueue() extraction in pso_async.rs, calling drop_unstarted_job(&p) in enqueue()'s orphan branch.)

Tested

  • Pre-fix reproduction (cargo test --release -p xcl --lib hle::d3d12x::pso_async::used_tests:: -- --test-threads=1 --nocapture): 10 passed, 2 failed — both new tests failed (pre-frame duration leaked into frame 8's budget and normal queued job was not dropped immediately on released()).
  • Post-fix cargo test --release -p xcl --lib hle::d3d12x::pso_async::used_tests:: -- --test-threads=4 --nocapture: 12 passed, 0 failed, 0 ignored (0.61 s), including:
    • pc_call_crossing_frame_boundary_does_not_charge_prior_frame_time: verifies pre-frame duration does not leak on either holds() or end() after a frame advance, calls genuinely stuck >= PC_CALL_HOLDS_AFTER after the frame advance still hold the title, and stale lower frame numbers never rewind frame or wipe the newer frame's spent budget.
    • released_queued_pipeline_drops_job_immediately_before_workers_drain: saturates both the normal worker pool and the preload worker pool behind gates, calls released() on queued pipelines carrying a DropFlag, and asserts both jobs drop immediately while workers are still blocked and never execute.
    • Serializes worker-saturation tests in used_tests with a test-only WORKER_SATURATION_LOCK so a_released_queued_pipeline_is_not_built_by_normal_workers and the new saturation test never race under --test-threads=4.
  • Full release workspace (cargo test --release --workspace --no-fail-fast -- --test-threads=4 --nocapture on NVIDIA GeForce RTX 4060): 23 test binaries, 1267 passed, 0 failed, 46 ignored (28 pre-existing hardware/debug-layer SKIPPED: 21 no wave64 on 32-lane GPU, 3 wave32 per-lane refused, 2 missing D3D12 debug layer, 1 driver cache toggle, 1 no heap windows; 0 skipped in pso_async).

Regression run

Paired 60 s scripts/smoke.ps1 runs on the same RTX 4060 host against origin/main (06c9f5505f63f10c5ec49ef932d75b2c28177324) and PR head (5b97e22b2e77b2ba3e65d8a3396c0f7a9eaf04af):

Baseline (origin/main):

xcl-smoke v1
commit: 06c9f5505f63f10c5ec49ef932d75b2c28177324
gpu: NVIDIA GeForce RTX 4060
title: 29640639
seconds: 60
exit: 0x0
frames shown: 1566 (PresentX 1566)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14462 MB

PR head:

xcl-smoke v1
commit: 5b97e22b2e77b2ba3e65d8a3396c0f7a9eaf04af
gpu: NVIDIA GeForce RTX 4060
title: 29640639
seconds: 60
exit: 0x0
frames shown: 1559 (PresentX 1559)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14420 MB

Not tested

  • AMD/Intel GPUs and native wave64 hardware (this host has an NVIDIA RTX 4060).
  • D3D12 debug-layer validation (d3d12SDKLayers.dll is not installed on this host).

AI use

Implemented and verified with Google Antigravity (Gemini 4 Neon).

…jobs immediately

Track `frame: Option<u64>` and `frame_since: Option<Instant>` on `PcCalls` so
monotonic frame advances (`frame > prev`) record `frame_since = Some(now)` and
clamp call start timestamps in `end()` and `holds()` (while advancing frame in
`begin()` too, reading `present::presents()` under `PC_CALLS.lock()`). A long
PC call that started on frame N and ends after frame N+1 is presented no longer
charges pre-frame-N+1 duration into frame N+1's `PC_CALL_HOLDS_AFTER` budget,
stale lower frame numbers never rewind `frame` or clear `spent`, and calls
stuck >= 250 ms after a frame still hold the title.

Also drop unstarted orphaned `Job` closures immediately in `released()` (outside
`PENDING`, `done`, and `job` locks via `drop_unstarted_job`), waking any
concurrent waiter when taking an unstarted job, and mirror that cleanup in the
`preload()` and normal worker orphan checks so captured COM objects and shader
bytecode are not pinned until workers drain deep queues.

Assisted-by: Google Antigravity (Gemini 4 Neon)
@Gevroska

Gevroska commented Nov 25, 2026 •

Copy link
Copy Markdown

I tested the cancellation boundary at 99bb2826 with a standalone Rust CPU fixture. I paused the worker after it read orphan == false, then let released() mark the entry orphaned. When I resumed the worker before released() took the job mutex, the worker claimed and ran the job. On completion, it discarded the result exactly once. When released() took the job first, the waiter woke and the job never ran.

I’d qualify “any unstarted Job closure is dropped immediately” in the description. Release cancels a queued job if it takes the job slot first. A worker that already passed the orphan check can still win that race.

All four fixture cases passed. I used inert job results and did not run a driver compile. I used Codex GPT-6.2 Sol for the fixture and a separate GPT-6.2 Sol agent at high reasoning to verify the source and results.

I reviewed and approved this comment before posting.

@fromthemoonx

fromthemoonx commented Nov 25, 2026 •

Copy link
Copy Markdown

Rerun on the new head 5b97e22 (rebased onto main 06c9f55): passes on NVIDIA with the D3D12 debug layer, and the debug layer reports the same messages as on main. The conflict with #2080 noted below is resolved.

result
cargo test --workspace --no-fail-fast -- --test-threads=4 1267 passed, 0 failed, 46 ignored; 25 SKIPPED lines (no wave64 and the refused per-lane cases); test_title_draws_with_a_root_signature_under_the_debug_layer ran and passed
cargo test -p xcl --lib hle::d3d12x::pso_async::used_tests:: -- --test-threads=4, 30 runs 12 passed each time, 30 of 30
debug-layer messages in the e2e title's xcl.log, PR vs 06c9f55 identical: 3× error id 93, 1× error id 1046, 1× warning id 820, 8× id 0 (live-object report at exit)

First run, on 99bb282: Passed on NVIDIA with the D3D12 debug layer installed, but conflicted with main. #2080 (9a257e1) had changed the worker loop in queue() (Box<dyn FnOnce()> work items, the orphan check moved into enqueue()), so I tested the head on its base 1a0bbe3: 1252 passed, 0 failed, 46 ignored; used_tests 30 of 30; debug-layer messages identical to 1a0bbe3.

The e2e title runs with XCL_ASYNC_PSO unset (async on). It does not release a pipeline that is still queued, and it does not keep a PC call open across a present. So the debug-layer run checks that nothing changed for that title, not the two new paths themselves. The new unit tests cover those.

Tested: RTX 3060 12 GB (driver 610.88), Ryzen 7 5700X, Windows 11 Pro 26300, Rust 1.100.0 MSVC, debug, Graphics Tools (D3D12 debug layer) installed.

Not tested: a release build, a title, AMD / Intel.

AI use: Claude Code (Claude Opus 5.5) ran the tests and wrote this report.

@Decodere

Copy link
Copy Markdown
Contributor Author

Thanks @fromthemoonx (for the RTX 3060 + D3D12 debug-layer verification) and @Gevroska (for the cancellation-boundary testing and wording precision)!

Rebased onto origin/main 06c9f550 -> 5b97e22b:

  • Adapted cleanly to Recompiler: split large direct compute pipelines at CFG edges #2080's Work queue / enqueue() extraction in crates/xcl/src/hle/d3d12x/pso_async.rs (wiring drop_unstarted_job(&p) into enqueue()'s p.orphan check).
  • Updated the PR description wording per @Gevroska to clarify that released() cancels/drops immediately when it claims p.job before a worker takes it, whereas a worker that already passed the p.orphan check and claims p.job first still runs and discards its result once in complete().
  • Re-verified pso_async::used_tests (12/0), full release workspace (1,267 passed, 0 failed, 46 ignored), and paired 60 s RTX 4060 smoke (1566 -> 1559 frames, 0 removals/crashes, 14462 -> 14420 MB).

AI use: rebased, tested, and drafted with Google Antigravity (Gemini 4 Neon).

@nnonno91

Copy link
Copy Markdown

Independent parallel test rerun on rebased head 5b97e22b2e77b2ba3e65d8a3396c0f7a9eaf04af.

Tested

  • Ryzen 7 9800X3D, Windows 11 25H2 build 26200.9974, Rust 1.100.0 MSVC, unoptimized test profile. These focused tests are CPU-side.
  • Ran cargo test --locked -p xcl --lib hle::d3d12x::pso_async::used_tests:: -- --nocapture --test-threads=4 three times in separate processes. All three runs gave 12 passed, 0 failed, 0 ignored and no skips.
  • The frame-boundary timing case and the test that drops queued jobs while the normal/preload workers are still gated passed on each run.

Not tested

The parent, a retail title, GPU behavior and the full workspace suite.

AI use

OpenAI Codex (GPT-6.2 Sol) ran the checks, verified the logs and wrote this report.

gustavsi added a commit to gustavsi/xcl that referenced this pull request Nov 25, 2026
…#2296, xcl-dev#2297, xcl-dev#2320, xcl-dev#2323, xcl-dev#2329, xcl-dev#2333

Integrates shader heap-base addresses (xcl-dev#1803), fenced superseded texture unmaps (xcl-dev#2270), cross-queue VRAM heap reclamation fencing (xcl-dev#2275, using create_raw_fence under Wine), PcCall frame clamping and orphan PSO drop (xcl-dev#2296), PSO pointer guards (xcl-dev#2297), KickoffX idle spare list switching (xcl-dev#2320, preserving UnadoptGuard), console-list descriptor heap binding (xcl-dev#2323), ClearUnorderedAccessView PC handles (xcl-dev#2329), and dispatch_chain PSO retention (xcl-dev#2333).
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