Skip to content

startup: create translated graphics PSOs outside draw_lock - #3528

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

Leyukaka wants to merge 16 commits into
R0Y00:mainfrom
Leyukaka:startup-graphics-pso-off-draw-lock

Conversation

@Leyukaka

Copy link
Copy Markdown
Collaborator

Stacked on #3516; review the last six commits (f379379..31be7fb) — this stays a draft until #3516 merges.

Refs #3451

What changes

#3516 moves shader translation off draw_lock. The first use of a translated PSO still created it with the lock held. This PR moves that creation out too (pipeline_for in src/startup_stubs/d3d12_draw.inl).

  • Reservation per key. The PSO cache is keyed by (list device, translation generation, ordered RTV/DSV signature), with one entry per state (plain / indexed-rect). The first draw that needs an entry reserves it. Draws needing the same entry wait on a condition variable under draw_lock and recheck the cache and their own recording after each wake. Recipe replay is reserved once per key, and rect geometry once per translation.
  • Captured inputs. Before unlocking, the recording thread takes references to the device, root signature and shader blobs, and copies the input layout (semantic names included), blend state and recipes, so nothing prepared outside the lock reads shared state.
  • Publication. After relocking, the PSO pointer and its complete flag are written together. A failed driver create is cached as a null result. If the device or the recording changed during the unlock, the result is dropped, the entry stays incomplete, and a waiter can retry.
  • Exceptions. The unlock guard relocks before the reservation is cleared, so waiters are always woken with draw_lock held, including on unwind. pipeline_for then returns null.
  • Recipe appends from threads of the same process take a separate mutex, never under draw_lock.
  • 824fb5c makes removal_device and info_queue atomics, since the device check can now run without draw_lock. 98ff231 fixes the one remaining plain read, in view_slot.
  • Fixtures: startup_graphics_pso_load_{unlock,concurrent}_{warp,hardware}, gated by X2W_TEST_GRAPHICS_PSO_* environment variables that are inert when unset. Docs: docs/experiments/startup-pso-recipe.md.

Unrelated recordings and queue submissions can now take draw_lock while a PSO is being created. The draw that started the creation, and draws waiting for the same state, still complete synchronously.

Separate fix in 31be7fb: texture uploads are published when their recording submits

This bug exists on main and is independent of the PSO change. A texture upload copied from the shared upload ring was marked done when it was recorded, not when its list was submitted. Only uploads with a dedicated buffer were tracked per list. A second list could therefore treat the texture as already uploaded, skip its own copy, and sample the texture before the first list's copy had executed if it was submitted first.

31be7fb records every texture copy per list (ring or dedicated). Other lists stage their own copy while one is pending, and the queue marks the upload done at submit, only if the recorded revision is still the current one. Records are dropped on Reset, failed Close and final list release.

The concurrent fixture depends on this fix (it submits a list that reuses a texture staged by another list). I can split it into its own PR on request.

Evidence

Local build at 31be7fb (fresh configure, full build, -DX2W_WITH_OPUS=OFF): exit 0.

ctest --preset dev --no-tests=error -R '^startup_graphics_(pso|published)_load_'

12/12 passed: startup_graphics_pso_load_{unlock,concurrent}_{warp,hardware} (this PR) and startup_graphics_published_load_{unlock,concurrent,native_backoff,precedence}_{warp,hardware} (#3516). The concurrent pair was rerun 5 more times: 5/5.

Mutations, each a full rebuild followed by the same filter:

  • M1, PSO creation kept under draw_lock (the unlock guard in pipeline_for replaced by a no-op): pso_load_unlock_{warp,hardware} and pso_load_concurrent_{warp,hardware} fail on gate timeouts.
  • M2, ring uploads marked reusable at record time again (the behaviour before 31be7fb): pso_load_concurrent_{warp,hardware} fail with publication pixel (0,0) format 28: 00000000 expected ff00ff00; pso_load_unlock_* still pass.

Restored source: 12/12.

Earlier failures on 98ff231

An offline build and test job run before 31be7fb failed four tests:

  • startup_graphics_pso_load_concurrent_{warp,hardware}: the 00000000 expected ff00ff00 readback above. Cause: the upload bug, fixed in 31be7fb and reproduced by M2.
  • startup_graphics_published_load_precedence_{warp,hardware}: FileNotFoundError when the check script started its child process. That job had not built startup_stream_consumer. With the executable built, both pass. Not a code failure.

Not covered

  • No full run of the application was measured with this change, so there are no lock-wait or frame-time numbers.
  • Minor: on the !current() paths of pipeline_for, draw_lock is retaken and then released and retaken again by the guard. Redundant, not incorrect.
  • A missing indexed-rect geometry is not cached in the PSO entry: each such draw yields the lock once and returns null (the geometry attempt itself is cached).

@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 31be7fb2 (draft, stacked on #3516) by reading f3793797..31be7fb2; not built or run.

Verdict: approve. The PSO handoff holds: reservations clear and wake under draw_lock on every path, including unwind, and the caller rechecks the recording after each wait or unlock. No MUST.

  • SHOULD Split 31be7fb into its own PR, as you offered. It fixes a bug on main and has its own mutation evidence (M2). It also changes upload cost on its own: while list A has a texture pending, any other list that binds it stages its own copy (reusable needs !pending_list || pending_list == list, src/startup_stubs/d3d12_draw.inl:2451). Titles that record several lists before the first submit now upload such textures once per list, and use more ring space. No counter tells these restages apart from content changes. One counter plus a title run would show whether ring stalls or dedicated pages go up.
  • NIT PsoTargetCache's comment says only rectangle geometry varies within a target signature. That holds because effective_blend_state and use_depth_stencil depend only on bound, tr and key.targets (:1990-2000). Say so, so a later change that adds pass state to the PSO also adds it to PsoCacheKey.
  • Checked, no finding:
    • Only install_graphics_translation makes a translation ready, and it sets ready_device, so no ready path returns a null PSO.
    • translated_rect_geometry never waits on a PSO reservation, so there is no wait cycle.
    • Upload revisions are global and only increase, so a released texture whose address is reused cannot match an old record. A failed copy leaves revision 0, which submit_texture_uploads treats as not done.
    • Map references across the unlock are safe (unordered_map nodes stay put).
    • The info_queue swap is under debug_lock.

Tests: pso_load_unlock_* proves creation runs with draw_lock released (M1). pso_load_concurrent_* proves waiters, recipe replay and a mismatched recipe, and the upload ordering (M2). The _FAIL and _ABANDON_ONCE variants reach the null result and the unwind path. CI ran none of them (startup_graphics_* is not in the Windows pr filter). The only results are the author's local 12/12 and mutation runs.

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