Skip to content

nvn/pipeline: DM_PSO_LOOKUP_BG: look up a draw's pipeline base in the background, not on the replay thread - #3132

Closed
adurham wants to merge 1 commit into
seedds:mainfrom
adurham:fix/pso-lookup-off-replay-thread
Closed

adurham wants to merge 1 commit into
seedds:mainfrom
adurham:fix/pso-lookup-off-replay-thread

Conversation

@adurham

@adurham adurham commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

What and why

Single frames of 50-130 ms every few minutes while walking in a scene whose pipelines the archive holds but for one or two never drawn before (Ocarina of Time, Hall of Fallen Foes, M4 Max, 2560x1440: 6 in 40 minutes). The GPU is at 10 ms and the frame's own pipeline work is under 1 ms, but the submit thread's wall time is 50-117 ms against about 5 ms of CPU time.

DM_HITCH_DUMP (with the host threads, #3131; the same stacks can be had from sample) shows the guest's main thread waiting for g_sub_mu in nvn_queue_submit and the replay thread, which holds it, in draw → pso_get → pso_build_ex (or hybrid_get) → arch_lookup → newSpecializedFunction → MTLCompilerScheduler::buildRequest. A draw whose pipeline is not built asks for its base without compiling, then for its stand-in's, and both searched the archives on the replay thread. A search that finds nothing needs Metal's compiler scheduler to specialize the base's functions, behind the compile the background is running for the previous never-drawn pipeline: 17-40 ms idle, up to 150 ms behind a compile. The windows' found none time matched the stalls. g_pbase_miss already makes each missing base cost this once a process; this moves the one search off the replay thread, in the direction of PR #2677's background lookups for predictions.

With DM_PSO_LOOKUP_BG=1 (off by default), pso_build_ex takes compile 2, the base only if it is made (g_pbase): no archive search, no compile, nothing marked missing, no wait for another thread's lookup. A draw's first look for its pipeline's base and its stand-in's uses it. The search of the others and the compile of what the archives lack are the background's, through hy_base_kick and pso_pend_demand as before. A draw of a shader with a pulling entry that has neither pipeline yet is skipped (pso_get returns nil, as it does for a slice-merged pass's pipeline not built yet) and asks again at its next use, for at most 250 ms a key; then it builds its pipeline as before. The pipelines up to frame line counts the skipped draws and the keys built after the 250 ms. A draw of a shader with no pulling entry still compiles on the replay thread when its base is not made: unchanged.

Why it is opt-in: a draw skipped for up to 250 ms on the first sight of a pipeline is a missing element where before it was a stall of 17-150 ms. "Asked again at its next use" only helps a draw that is repeated every frame; one into a target that is drawn once stays missing until the target is drawn again. I did not compare skipped draws against screenshots, and I ran one game on one Mac: the first opening of Ocarina of Time's pause menu (23 pipelines never drawn before) skipped 123 draws and had one 215 ms frame from the 4 draws of shaders with no pulling entry. Flipping the default is the follow-up, once that and a second game are checked.

Reference entry: docs/reference/frame-rate.md, "A draw's first look for its pipeline's base searched the archives on the replay thread"; game note: Ocarina of Time, Frame rate.

Checked

  • Tests: new t_pso_first_sight (with DM_PSO_LOOKUP_BG=1, and --inline for the default) and cases 4 and 5 of t_pso_look_wait; t_pso_pending, t_pso_bg_look, t_pso_demand_record, t_pso_depth_stencil, t_pso_msaa, t_pso_warm, t_pso_race, t_pull, t_pull_inv, t_pend_index pass as on main; t_pso_stall fails on main too. test_docs.py passes; test_env_switches.py fails as on main. clang-tidy was not run (not installed on the machine).
  • Games run: Ocarina of Time v131072, a fixed walk and sword swings around the Hall of Fallen Foes for 40 minutes a run, the same archive state: 6 stalls of 50-127 ms in two runs of 22 and 18 minutes before; none over 30 ms in 40 minutes with DM_PSO_LOOKUP_BG=1, outside the harness's own screenshots and the area load.
  • Devices and OS: Mac Studio M4 Max, macOS 27.3. No iPhone (the iPhone side of the lookups is the open retest in the game note).
  • regress.py check --games: not run; only Ocarina of Time is installed on this machine. With the switch off, the one change to every game's path is that pso_build_ex reads a compile value of 2 as the known-only mode, which no existing caller passes.
  • Measurements: above.

Front ends

Nothing a front end has to act on.

Docs

  • The finding is in its game note and its docs/reference/frame-rate.md entry, and python3 dm/tools/docs_symptoms.py was run (the symptom index)
  • No chain tool changed
  • No game files, keys or data extracted from a game in the change or the attachments

Written with AI: Claude (Hermes Agent) · Responsibility: Adam Durham

@github-actions github-actions Bot added nvn NVN on Metal and its tests (dm/gfx/nvn, dm/gfx/tests) docs Documentation game: 0400486024DEC000 A game labels Nov 21, 2026
… background, not on the replay thread

Symptom: single frames of 50-130 ms every few minutes while walking in a
scene whose pipelines the archive holds but for one or two never drawn
before (Ocarina of Time, Hall of Fallen Foes, M4 Max, 2560x1440: 6 in 40
minutes). The GPU is at 10 ms and the frame's own pipeline work is under
1 ms, but the submit thread's wall time is 50-117 ms against about 5 ms of
CPU time.

Cause: DM_HITCH_DUMP shows the guest's main thread waiting for g_sub_mu in
nvn_queue_submit and the replay thread, which holds it, in draw -> pso_get ->
pso_build_ex (or hybrid_get) -> arch_lookup -> newSpecializedFunction ->
MTLCompilerScheduler::buildRequest. A draw whose pipeline is not built first
asks for its base without compiling and then for its stand-in's, and both
searched the archives on the replay thread. A search that finds nothing needs
Metal's compiler scheduler to specialize the base's functions, behind the
compile the background is running for the previous never-drawn pipeline:
17-40 ms with an idle compiler, up to 150 ms behind a compile. The windows'
`found none` time matched the stalls.

Fix: pso_build_ex takes compile 2, the base only if it is made (g_pbase): no
archive search, no compile, nothing marked missing, no wait for another
thread's lookup. A draw's first look for its pipeline's base and for its
stand-in's uses it. The search of the others and the compile of what the
archives lack are the background's, through hy_base_kick and pso_pend_demand
as before. A draw of a shader with a pulling entry that has neither pipeline
yet is skipped (pso_get returns nil, as it does for a slice-merged pass's
pipeline not built yet) and asks again at its next use, for at most 250 ms a
key; then it builds its pipeline as before. The pipelines line counts the
skipped draws and the keys built after the 250 ms. All of this is off by
default and DM_PSO_LOOKUP_BG=1 turns it on: a skipped draw that is not repeated
(one into a target that is drawn once) stays missing until its target is drawn
again, and that has not been compared against screenshots or tried on another
game. A draw of a shader with no pulling entry still compiles on the replay
thread when its base is not made: unchanged.

Measured: the same walk and archive state, 6 stalls of 50-127 ms in two runs
of 22 and 18 minutes before; none over 30 ms in 40 minutes with
DM_PSO_LOOKUP_BG=1, outside the harness's own screenshots and the area load. The first opening of the pause
menu, 23 pipelines never drawn before: 123 draws skipped, one frame of 215 ms
from the 4 draws of shaders with no pulling entry. The skipped draws were not
compared against screenshots, so a missing element on a first sight is
possible, which is why it is off by default.

Checked: new t_pso_first_sight (with DM_PSO_LOOKUP_BG=1, and --inline for the
default) and cases 4 and 5 of
t_pso_look_wait; t_pso_pending, t_pso_bg_look, t_pso_demand_record,
t_pso_depth_stencil, t_pso_msaa, t_pso_warm, t_pso_race, t_pull, t_pull_inv
and t_pend_index pass as on main; t_pso_stall fails on main too. The docs
(test_docs.py) pass; test_env_switches.py fails as on main. M4 Max, macOS 27.3.
Only Ocarina of Time was run; the iPhone was not. clang-tidy was not run (not
installed here).
@adurham
adurham force-pushed the fix/pso-lookup-off-replay-thread branch from d26f23e to ede3477 Compare November 21, 2026 15:53
@seedds

seedds commented Nov 23, 2026

Copy link
Copy Markdown
Owner

Thanks @adurham for the thorough diagnosis, stack traces, and measurements here.

We're going to pass on this PR and close it rather than landing an opt-in draw-skipping path. Dropping draws for up to 250 ms risks visible pop-in and permanent missing content on targets that are only drawn once (such as cached shadows, lightmaps, UI atlases, or screen captures), and can also break depth-prepass pairing, occlusion queries, or shader writes. (Slice-merged passes are different because they split the pass and immediately draw in a normal pass rather than dropping the draw.) Because skipping draws isn't safe to make the default across games, we'd rather not carry it behind an environment switch.

While reviewing the diff against the pipeline lookup, hybrid stand-in, prediction, and draw paths in dm/gfx/nvn/nvn_mtl.m and offline archive generation in dm/gfx/tools/pso_offline.py, we also noticed a few extra drawbacks on the skip path: kicking the pulling base through the utility-priority background compile queue puts fast archive lookups behind slow compiles, and returning early skips both submission lookahead prediction and recording the pipeline for future prewarms.

What's still open: the underlying hitch you tracked down is real. A direction that avoids ever dropping draws would be to index the bundled pipeline archive (<arch>.mtl4a) with a .keys file like the prewarm and run archives already have, so missing bases skip Metal archive calls completely, and—if archive hits still stall behind background compiles—warm bases earlier on the higher-priority lookup queue before draw time.

@seedds seedds closed this Nov 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs Documentation game: 0400486024DEC000 A game nvn NVN on Metal and its tests (dm/gfx/nvn, dm/gfx/tests)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants