Skip to content

startup: prepare graphics translations outside draw_lock - #3516

Draft
Leyukaka wants to merge 10 commits into
R0Y00:mainfrom
Leyukaka:startup-graphics-load-off-draw-lock
Draft

Leyukaka wants to merge 10 commits into
R0Y00:mainfrom
Leyukaka:startup-graphics-load-off-draw-lock

Conversation

@Leyukaka

@Leyukaka Leyukaka commented Nov 29, 2026 •

Copy link
Copy Markdown
Collaborator

Refs #3451 (graphics part, taken over in #3451 (comment) and #3451 (comment)).

Draft, kept as a draft under our open-PR cap in #3514.

Why

In a long local run of the application, presents stopped about 32-37 s before the application's watchdog fault, while cold graphics translations were being loaded and compiled under draw_lock. #3506 moves this work off the lock for compute only.

The approach follows @lukaslw-lab's compute design in #3467 (worker/unlocked translation); this PR is a separate implementation for graphics.

What changes

src/startup_stubs/d3d12_draw.inl: preparing the translation of a cold graphics key now runs on the initiating call with draw_lock released. Preparation covers the key hash and plain-key .txt probe, request writes, manifest and bytecode reads, eligible native compilation and translated root signature creation, for base keys and RTV variants.

  • One owner per key and phase. The owner reserves the entry under the lock before releasing it.
  • A caller that needs the same pending key and phase waits on a condition variable, which releases the lock, and rechecks the entry when woken. Waiters are woken on success, failure and exception.
  • Other keys and guest queue submissions can take the lock during preparation.
  • The result is prepared on a private candidate and installed under the lock. Guest stage bytes and the root blob are copied before the lock is released.
  • The device is pinned before the unlock and compared after the lock is reacquired. A result prepared for a superseded device is discarded. attach_device (d3d12_xs_device.cpp) now writes the device global under the lock.
  • The retry schedule for an absent manifest is unchanged (1, 2, 4, 8, 16, 32 s), and so is the order: a usable published translation first, then native, except in native-only mode.
  • An abandoned attempt (an exception escaping preparation, or a device change) backs off on the same 1-32 s schedule, including in native-only mode. Without this, a persistent failure would re-run preparation on every draw.
  • Request temp names are collision-safe: PID plus a process-wide serial, created with CREATE_NEW, renamed without replace.

docs/startup-runtime.md gets a section on the lock scope. The edit also removes the sentence saying native translation covers compute only.

What stays under the lock

Translated PSO creation and command recording. Moving PSO creation is a follow-up.

Tests

New, in tests/cmake/startup_graphics_published_load.cmake, each on WARP and hardware:

  • startup_graphics_published_load_unlock_*: with a cold load paused, a draw on another key and a guest queue submission complete before the load is released.
  • startup_graphics_published_load_concurrent_*: a same-key peer waits and the key is prepared once; an interrupted attempt is retried after its deadline and not before; a result prepared on a superseded device is not installed.
  • startup_graphics_published_load_native_backoff_*: in native-only mode, an abandoned preparation is not re-entered before each deadline and is re-entered after it, for 1, 2, 4, 8, 16, 32 and 32 s.
  • startup_graphics_published_load_precedence_*: published translation before native.

Results at f379379 on main 5b86df1:

  • ctest -j1 -R '^startup_graphics_published_load_': 8/8 passed, no skips. native_backoff takes about 96 s per adapter because it probes the real backoff intervals.
  • ctest -j4 -R '^(startup_indirect_|startup_stream_consumer|startup_native_graphics_draw_|native_rtv_variant_|startup_manifest_retry$)': 106/106 passed.
  • At the earlier head e8ad9ad, ^(startup_indirect_|startup_stream_consumer|startup_slot57_|native_fixed_mesh57_|startup_translated_|startup_draw_): 138 tests, 135 passed, 2 skipped (startup_draw_depth_target_debug_*), 1 failed (native_fixed_mesh57_waterfall_fine_hardware, the known tests: native_fixed_mesh57_waterfall_fine_hardware fails on main (mip 1 colour read where mip 0 is expected) #3489 failure).

Environment for these runs. They are host runs of our own code, not runs in an isolated environment.

Field Runs at f379379 Run at e8ad9ad
Commit f379379 on main 5b86df1 e8ad9ad on main 5b86df1
Adapter (_hardware) NVIDIA GeForce RTX 5080 not recorded
Driver 32.0.16.2101 not recorded
Windows build not recorded not recorded
Debug layer unavailable not recorded
WARP version (_warp) not recorded not recorded
Build dev preset, X2W_WITH_OPUS=OFF dev preset, X2W_WITH_OPUS=OFF

Mutations, each applied alone and rebuilt:

Mutation Caught by
Lock held during preparation unlock ("independent draw or guest submission stalled behind the graphics load"), concurrent
Waiter wake removed concurrent (timeout)
Superseded result installed concurrent
Abandoned-attempt backoff removed concurrent ("interrupted graphics key never retried")
Due abandoned attempt not advanced concurrent
Native tried before published precedence
Backoff skipped in native-only mode native_backoff, both adapters

The last one and the first were run at or after e8ad9ad. The other five were run on an earlier head of this branch (cd49b0c with the two fixes that followed), not repeated at f379379.

Not covered

  • No long-run re-measurement yet: the effect on the watchdog fault is not measured.
  • The concurrent test proves that the superseded result is not installed and that a retry runs. It does not prove that the retry draws: the test's list belongs to the old device, and the one-device recording guard refuses it.
  • The initiating call and same-key waiters still wait for the full load.
  • In legacy mode, if the first attempt on an RTV variant is abandoned, the due retry only looks for a published translation: the .rtv request is not published and native is not tried for that key.
  • native_backoff uses wall-clock margins (250 ms at the 1 s step). It passed with both adapters in parallel, but a heavily loaded host could miss the margin.
  • Hardware: one desktop GPU and WARP.

@lukaslw-lab lukaslw-lab 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 f3793797 by reading the diff, then ran it merged onto main 44a3ad5a with #3506, #3464, #3521 and #3097 (no tests run). Reads correct; one point for the rebase.

  • Long run for "Not covered" (360 s from a save, native only, RTX 3080 Ti): main alone stops at the #3451 fault after 125 s; with this PR and #3506, three runs reached 360 s. This does not separate the two PRs.
  • Concurrent preparation of different keys looks safe: a compiler per call (d3d12_draw.inl:774), DXIL and frontend cache writes go through unique temp files and a rename, and the catalog sits behind a mutex.
  • Rebase: main's NativeShaderTimer (d3d12_xs_device.cpp:433) assumes both native paths hold draw_lock and logs draw_lock_ms. After this PR the graphics time is unlocked: 14.5 s over 56 compiles in our run. Rename it or measure only the locked part. max_ticks load-then-store can also lose a maximum now.

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.

2 participants