Skip to content

Prevent synchronous shader compilation during TOTK pipeline lookahead - #29

Merged
ly05010419 merged 1 commit into
ly05010419:mainfrom
Flowershangfromthebranches:fix/totk-bounded-pipeline-prediction
Oct 19, 2026
Merged

ly05010419 merged 1 commit into
ly05010419:mainfrom
Flowershangfromthebranches:fix/totk-bounded-pipeline-prediction

Conversation

@Flowershangfromthebranches

Copy link
Copy Markdown
Contributor

Scene changes can stall the submission thread while the pipeline predictor synchronously translates and compiles shaders for later draws. This PR makes lookahead use only already-resolved binding variants, bounds the scan, and reclaims completed predictions that were never consumed.

Changes

  • Skip unresolved future programs/variants during prediction; actual draws retain their normal translation, compilation and error handling.
  • Inspect the exact cached variant for the scanned binding state without changing the active program selection. Track temporary render-target state and restore the active state afterward.
  • Limit scanning to 8,192 commands and 32 primary candidates, checking a best-effort 2 ms budget between commands. Dispatch primary candidates immediately rather than after the full scan. The budget is not a hard bound on driver calls or the whole function.
  • Reclaim completed unused predictions after more than two submissions. Never reuse a slot until its worker has published completion under the existing mutex.
  • Extend the production CPU probe-gfx with unknown-program, alternate-binding and pending-slot lifetime regressions; preserve the existing scissor/conditional-counter checks.

Evidence and scope

An Apple M3 / 24 GiB / macOS 27.2, game 1.0.0 session recorded 32 relative long-frame entries above one second, all accompanied by shader-library or pipeline creation. The worst frame was 10,384.9 ms (submission/presentation 10,378.8 ms; GPU execution 17.5 ms). Nested compilation timers are not summed as independent wall time.

The player reports substantially fewer severe stalls with a broader local candidate that also contains opportunistic shader prefetching. That is not an isolated performance measurement of this PR. This narrower branch has build and CPU regression coverage, but has not been installed for a separate gameplay A/B. It does not claim to eliminate first-use compilation, fix global 1% lows, or deliver a particular FPS gain.

This PR contains only three source/documentation files. The prefetch queue, worker-process signal-mask changes, frame pacing, resolution, interpolation, keys, game files, saves, shader caches and generated apps are excluded. Further frame-rate optimization will be a separate PR.

Validation

  • Isolated branch native runtime build/link: passed. Reused unchanged graphics objects, rebuilt the renderer and the base versions of hle_nvn.c and totk_shader_rt.m so the test binary excludes the local prefetch feature. Renderer compilation emits 58 warnings; not described as warning-free.
  • Resulting runtime probe-gfx: passed, including all three added checks. No Metal device or GPU drawing; the normal probe initialization maps the existing game modules, without booting the game or changing its profile.
  • Repository source regressions: 38 passed, using the existing Python 3.14 build environment and pinned dependencies.
  • git diff --check: passed.
  • Installed player App and ongoing session: untouched.

Raw local evidence stays outside the repository; docs/pipeline-prediction-stalls.md records the method, evidence locations and limits.

Related work

tvc97 added a commit to tvc97/MacNativeGames that referenced this pull request Oct 19, 2026
…05010419#30)

Brings in upstream ly05010419#29 (bounded pipeline prediction without translation,
stale guesses reclaimed) and ly05010419#30 (in-game libraries and pipelines in the
background, plus the present residency and compile-options fixes).

Conflict resolution in nvn_mtl.m / hle_nvn.c: took the branch side,
re-applied what main has since ly05010419#29's base (ly05010419#13 warm_precompile, ly05010419#17
compact HUD), and kept main's prebuilt-library hit/miss diagnostic.
Our earlier prediction-table drain (pso_pend_drain into g_pso,
prog_get_cached, mtl_pp_stats and its log line) is replaced by ly05010419#29's
version. It no longer skips pso_user.tsv for drained pipelines.

Checked: make runtime-v143 (58 warnings, none new), 38 source tests,
probe-gfx pass, 3 empty-cache async boots without a GPU fault.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@ly05010419
ly05010419 merged commit 27f8185 into ly05010419:main Oct 19, 2026
1 check passed
ly05010419 added a commit that referenced this pull request Oct 19, 2026
…uesses for depth export; limits, statistics and negative controls

Follow-ups to PR #29:
- prog_prediction_stages: a program with a stage not translated yet (iOS, pr->missing) is not
  predicted; its draws are skipped and its fragment stage can be absent.
- pred_guess_wanted: a fragment function that exports depth gets no guesses without the depth
  buffer. ds_scan attaches depth for it, so those guesses were never used, and pso_desc compiled
  their depth-less library on the submission thread.
- Scan limits TOTK_PSO_PREDICT_SCAN / _KEYS (up to 160) / _US, defaults as in the PR
  (8192 commands, 32 keys, 2000 us), logged once.
- A statistics line every 100 scans: scans and the budget that ended them, pipelines started and
  taken, waits for the background, guesses refused (table full) and stale guesses freed.
- probe-gfx: a program with an untranslated stage is not predicted; no depth-less guess for depth
  export. verify_gfx.py runs negatives 24-27 (active variant taken, every slot freed, untranslated
  stages ignored, depth-less guesses made); each fails at its own check.
- The deferred-binds probe now reacts only to TOTK_PROBE_NEGATIVE=1, as its comment says: any
  value used to reach it first, so later probe-gfx negatives could not be reached.
- docs/pipeline-prediction-stalls.md: contributor-local paths removed; what still runs on the
  submission thread; the in-game comparison (one session per configuration, no speed-up claimed).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
tvc97 added a commit to tvc97/MacNativeGames that referenced this pull request Oct 19, 2026
Upstream merged ly05010419#29 with follow-ups (c5b3ae7): prediction limits and
statistics, and probe-gfx negative controls 24-27. Conflicts in nvn_mtl.m:
- prog_prediction_stages: upstream's missing-stage skip with negative 26;
  pr->missing also covers stages still loading in the background.
- pso_pend_reclaim: keeps demand slots (a draw waits for them), and takes
  upstream's negative 25 and reclaim count.
- pso_pend_kick: keeps the demand flag and upstream's table-full count.
- pso_predict_rest: upstream's max_keys bound, guesses kicked with demand 0.

pso_pend_poll, the async path's wait, now adds to upstream's waited-for
statistics as pso_pend_take does.

Checked: nvn_mtl.m and hle_nvn.c compile, nvn_mtl.m with 58 warnings, the
same as upstream main; 79 source tests pass.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
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