Skip to content

startup: reset the window presenter's allocator only after its fence is reached - #3196

Merged
R0Y00 merged 3 commits into
R0Y00:mainfrom
tedin7:up-presenter-fence
Nov 30, 2026
Merged

R0Y00 merged 3 commits into
R0Y00:mainfrom
tedin7:up-presenter-fence

Conversation

@tedin7

@tedin7 tedin7 commented Nov 27, 2026 •

Copy link
Copy Markdown
Collaborator

Part of #870

What this unblocks: a slow frame (a GPU frame longer than two seconds, common on WARP and on first-use shader compiles) no longer makes the window presenter reset a command allocator the GPU is still using.

present_to_window signalled its fence, waited at most 2000 ms, and on the next frame reset the command allocator whether or not the fence was reached. "ID3D12CommandAllocator::Reset" requires that no command list recorded with the allocator is still executing on the GPU; resetting earlier is a debug-layer error and can remove the device.

  • src/startup_stubs/presenter_fence.h: wait_for_submission over caller-supplied operations: returns reached only when the completed value reaches the submitted one; stops on device removal, a failed event arm or a failed wait; reports once when the wait passes two seconds.
  • src/startup_stubs/presenter_fence_win.h: the D3D12 binding (GetCompletedValue, SetEventOnCompletion, WaitForSingleObject in 1 s slices, GetDeviceRemovedReason).
  • d3d12_present.inl: the presenter waits before every allocator reset and after its Signal; a failed Signal, arm or wait, or a failed allocator/list reset, marks the presenter failed instead of reusing the allocator.

Tests:

  • startup_presenter_fence (pure decisions with a fake clock: ready, slow with one warning, removed before and during the wait, re-arm after a signalled slice, arm failure, wait failure).
  • startup_presenter_fence_warp / _hardware (debug layer: a real queue held behind a fence beyond two seconds; the allocator is reset only after the wait returns reached, with no debug-layer error).

Verification:

  • cmake -S . -B /tmp/up-presenter-fence-cfg -G Ninja -DDXC_EXECUTABLE=/bin/true -> configure OK
  • x86_64-w64-mingw32-g++-posix -std=c++20 -fsyntax-only -Isrc tests/startup_presenter_fence.cpp -> OK
  • Windows Server 2025 (Build 26100, MSVC 14.44.35207, D3D12 WARP) at 9a41fd99788ad778cc62797c756bf60308168687:
    • dev: cmake --build --preset dev --target d3d12_xs_device_obj d3d12_xs_shim startup_presenter_fence, then ctest --preset dev -R '^startup_presenter_fence' --no-tests=error -> 3/3 passed
    • release /O2: same targets, ctest --preset release -R '^startup_presenter_fence' --no-tests=error -> 3/3 passed
  • Mutant (the wait helper gives up after two seconds and reports the fence reached), dev: startup_presenter_fence, _warp and _hardware all fail (0/3 passed)
  • Coverage note: the tests guard the wait helper and its D3D12 binding; the presenter call site in d3d12_present.inl needs a real window and is covered by review only.

Draft until an independent peer review and the Win2025 WARP gate pass.

Thanks to @Makto94 for the testing that found this.

AI-assisted; commands listed were executed locally.

…eached

The presenter waited at most two seconds for the frame's fence and then reset the command allocator anyway, which is invalid while the GPU still runs that frame. It now waits in one-second slices until the fence is reached or the device is removed (a slow wait is logged once), and a failed Signal, event arm or wait stops the presenter instead of reusing the allocator.
@KelboLive

Copy link
Copy Markdown
Contributor

Review of 9a41fd99 (single commit):

@Leyukaka

Copy link
Copy Markdown
Collaborator

Review of 9a41fd99, diff only (no build or run on my side). Comment only.

The ordering looks right on the success path. present_to_window now waits for the presenter's own fence to reach submitted before Reset. The value it signals is submitted + 1, on the queue that just executed, and it is stored only after Signal succeeds. On the first frame the fence starts at 0, so there is no wait. Two points:

  1. A removed device's fence value counts as "reached". In wait_for_submission, the completion test is ops.completed() >= submitted, and completed() returns GetCompletedValue() as is. When the device is removed, that returns UINT64_MAX, which passes the test. src/startup_stubs/fence_completion.h already treats that value as removal ("D3D12 returns UINT64_MAX on device removal, not successful completion"). The removed() check before it only catches a removal that GetDeviceRemovedReason() already reports. Routing the comparison through x2w_startup::fence_completion(ops.completed(), submitted) would make the helper agree with the rest of the tree. A Fake case with done == UINT64_MAX and dead == false would pin it. The current fake sets both together. The window is small, a removal landing between the two calls, so this is a consistency point rather than a demonstrated failure.

  2. The test does not cover the change in present_to_window. tests/startup_presenter_fence.cpp includes presenter_fence_win.h and drives wait_for_submission / wait_for_fence, but it never calls present_to_window. If the wait before Reset were removed, it would still pass. The helper tests are useful as they are. This point is only about what they prove for the presenter.

AI-assisted.

Copy link
Copy Markdown

PortBridge found a related fence-lifetime fix in XCL #3062: XCL quarantines the old sparse allocator/list while this presenter waits for its own submitted fence. That makes the same lifetime condition useful without copying the different policy. On 9a41fd99, the test already covers a late fence and a bounded hardware gate. One independent check worth returning here is the existing startup_presenter_fence hardware test with the D3D12 debug layer on an idle Windows adapter. Report exit code, adapter/driver and debug error count. The two-second warning is informational; passing still requires completed fence plus a clean allocator reset.

@tedin7

tedin7 commented Nov 28, 2026

Copy link
Copy Markdown
Collaborator Author

Real-GPU verification (NVIDIA RTX 3070 Ti, driver 32.0.16.2101, Windows 11):

Tested at 9a41fd9 (current head). dev preset, ctest run one test at a time:

  • ctest -R '^startup_presenter_fence': 100% tests passed, 0 tests failed out of 3.
  • ctest -R '^startup_presenter_fence_hardware$': 100% tests passed, 0 tests failed out of 1.
  • Mutant patch (timeout mutant) applied: 0% passed, 3 of 3 failed: startup_presenter_fence, startup_presenter_fence_warp, startup_presenter_fence_hardware.

These complement the existing WARP runs.

AI-assisted; commands listed were executed locally.

@tedin7
tedin7 marked this pull request as ready for review November 28, 2026 10:49
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 28, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-28T10:53:51.285787Z 9a41fd9 Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 9a41fd9978

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread CMakeLists.txt Resolved
@tedin7

tedin7 commented Nov 28, 2026

Copy link
Copy Markdown
Collaborator Author

Merged current main into this branch (merge commit, no force push, no conflicts); head is now 5fb93b041. The registration finding was already fixed in 2313547fb (the tests live in tests/cmake/startup_presenter_fence.cmake).

Verification (Windows Server 2025, D3D12 WARP, head 5fb93b041): x2w_pipe.py win2025 3196 --regex '^startup_presenter_fence' -> 3/3 passed.

@MasterDomino ready for review and merge when you have time.

AI-assisted; commands listed were executed locally.

@pigalot pigalot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Independently reviewed 5fb93b04 integrated over main 212d4b5f (tree c70d77e0). The trial merge is clean and git diff --check is clean. The head's merge of main (5fb93b04) has an empty --remerge-diff.

What I traced in present_to_window:

  • submitted starts at 0 and the fence at 0, so the first frame does not wait.
  • submitted advances only after Signal succeeds, and the value signalled is on the queue that just executed the list.
  • A frame that records nothing (!drawn) closes the list without executing it and leaves submitted as it was, so the next frame's wait and reset are still correct.
  • Every path that leaves the allocator in an unknown state (failed Signal, arm, wait, Reset) sets failed, and failed is checked on entry, so the allocator is not touched again.
  • The event is auto-reset and can be left set when a wait returns through the completed-value check. The next wait then sees one early signaled slice, re-arms and carries on; the re-arm branch in wait_for_submission handles that, and startup_presenter_fence pins it.

Built d3d12_xs_shim and startup_presenter_fence in dev and release, then ran
ctest --test-dir out/build/<preset> --no-tests=error -j 1 -R '^startup_presenter_fence(_warp)?$' and, on its own, -R '^startup_presenter_fence_hardware$':
3 passed in Debug and 3 passed in Release /O2, no skips. On main 212d4b5f the tests are not registered.

Mutants (dev; each applied alone, rebuilt, then reverted):

Killed (10):

  • helper: completed > submitted; removal check dropped; arm failure ignored; failed wait treated as a timeout (test times out); no re-arm after a signalled slice; warning not latched; give up after 2 s as reached (your mutant).
  • binding: completed() reports the fence done after 1.5 s (fails _warp and _hardware); timeout slice mapped to failed (fails both).
  • binding, with the test's elapsed-time and completed-value checks also removed: _hardware still fails, so on the RTX 5090 the early Reset itself is caught. _warp passes with that pair of changes, so on WARP only the elapsed-time check tells the two apart.

Survive (7), none blocking:

  • warning threshold 1000 ms or 2500 ms instead of 2000 (the fake clock steps 1001 ms and completes on the third slice);
  • binding: a signalled slice mapped to timeout; the SetEventOnCompletion result ignored; the event never armed at all (the wait then finishes by polling at the next 1 s slice; _warp took 3.03 s instead of 2.64 s);
  • binding: removed() always false;
  • binding: slice of INFINITE instead of 1000 ms.

Equivalent in the tests (1): the event && guard dropped (the test always passes a valid event).

Notes, none blocking:

  1. Your mutant fails all three tests through pure_checks(), which runs first in every mode, so it does not show that the device tests tell the behaviours apart. The binding mutant above does.
  2. The event path of the binding is not pinned: nothing checks that the wait returns when the event fires rather than at the next slice. A device case that releases the gate mid-slice and bounds the time to return would cover the three event survivors.
  3. Leyukaka's point 1 still stands at this head: completed() >= submitted accepts UINT64_MAX. I traced what follows: the pre-reset wait returns reached, and nothing can still be executing on a removed device, so I do not see a wrong reset from it. Routing through fence_completion() would still be the consistent form.
  4. On removal during the post-signal wait the function now returns before check_device("window present", 0), so the "DEVICE REMOVED" line comes from a later check_device call instead of this one.
  5. The call site in d3d12_present.inl is not reached by any ctest, as the PR body says. A local title run of this head merged onto main 212d4b5f goes through it: [pass] judgement: Judged no worse than the baseline. Confidence high.

Environment: NVIDIA GeForce RTX 5090 driver 32.0.16.1088 and Microsoft WARP; Windows 11 25H2 build 26200.9974; MSVC 19.52.37029; Python 3.12.10; D3D12 debug layer on.
CI: Build and test on Windows passed on 5fb93b04.

Approving 5fb93b04.

This reply was AI-assisted.

@pigalot

pigalot commented Nov 29, 2026

Copy link
Copy Markdown
Collaborator

I have not merged this: the Codex thread on CMakeLists.txt (test registrations) is still open, and CONTRIBUTING asks for all review conversations to be resolved first. I checked that its request is met at 5fb93b04 (the registrations are in tests/cmake/startup_presenter_fence.cmake), so it only needs resolving.

This reply was AI-assisted.

@tedin7

tedin7 commented Nov 29, 2026

Copy link
Copy Markdown
Collaborator Author

@pigalot Resolved that review thread — ready to merge when you have a moment.

AI-assisted.

@Leyukaka Leyukaka left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Review of 5fb93b04 against main eab7180c, by reading only (no build or run). Comment only.

  1. The wait before the allocator reset has no upper bound. At this head, src/startup_stubs/d3d12_present.inl:177-187:

    auto wait = [&] {
        return x2w_startup::wait_for_fence(p.fence, p.submitted, p.reached, device, [] {
    ...
    if (!wait()) {
        p.failed = true;
        return false;
    }
    if (FAILED(p.allocator->Reset()) || ...

    and the loop in presenter_fence.h:15-29 is for (;;) with four exits: removed, reached, arm_failed, wait_failed. A 1 s slice that times out (presenter_fence_win.h:19, WaitForSingleObject(event, 1000)) only goes round again; the two-second mark calls ops.slow() and continues. On main, d3d12_present.inl:195 is WaitForSingleObject(p.reached, 2000); and the function returned after that. So a frame whose fence never completes, on a device that never reports removal, now keeps the present thread in this loop for good, with one log line. Is that intended? An alternative is a bounded wait that logs, sets p.failed and returns without resetting the allocator, which keeps the guarantee this PR adds.

  2. The green Build and test on Windows check does not build or run the new tests. On pull requests the job runs cmake --build --preset pr and ctest --preset pr (.github/workflows/windows.yml:490,493). In CMakePresets.json at eab7180c, the pr build preset (:26-38) has a fixed "targets": [ "d3d12_sdk_check", "era_heap_check", "xmem_shim", ... ] list and the pr test preset (:46-53) has a "filter": { "include": { "name": "^(bcpack_all_formats(_golden_control)?|...|startup_present_readback|...)$" } }. Neither contains startup_presenter_fence, and this PR does not change CMakePresets.json. The tests first run in CI on the push to main (ctest --preset dev ... -E '_hardware$', :498), which leaves out _hardware.

@JohnConnorNPC

Copy link
Copy Markdown
Collaborator

Re-reviewed at 5fb93b0 against main 77d1ba4 (by reading the code; no Windows build here). git merge-tree --write-tree origin/main <head> exits 0. CI: 3 success, 2 skipped. The WARP and RTX reports above cover ^startup_presenter_fence.

MUST

  1. The present call can now block for good on the default render path. On main, present_to_window gives up after WaitForSingleObject(p.reached, 2000) and returns. At this head, wait_for_submission in presenter_fence.h is a for (;;) loop that exits only on removed, reached, arm_failed or wait_failed. A timed-out slice just loops again, and after 2 s it calls ops.slow() once. The window presenter signals on the title's own queue (present_to_window(static_cast<ID3D12CommandQueue*>(self->own().real), ...)). So if that queue is stuck behind a GPU-side Wait that nothing signals, and the device is never removed, the title's present call (CommandQueue::slot32) never returns. On main the same case freezes the picture, but the title thread keeps running. Every --render run goes through this code, and no switch turns it off. This is the open question above about the unbounded wait. I'm grading it MUST because a hang is not a fail-closed result. The bounded wait proposed above (time out, set p.failed, skip the reset) keeps this PR's guarantee that no allocator is reset before its fence. Please also add a fixture case where the fence never completes, so that branch is tested.

NIT
2. ops.completed() >= submitted also accepts UINT64_MAX from a removed device. That's harmless here, because removed() is checked first, but using fence_completion() would match the rest of the file.
3. If the device is removed during the post-signal wait, the function returns before check_device("window present", 0). The DEVICE REMOVED line then comes from a later call.

Holding, not merging, until 1 is resolved.

AI-assisted.

@R0Y00
R0Y00 merged commit cb9451e into R0Y00:main Nov 30, 2026
5 checks passed
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.

7 participants