Skip to content

No pulling stand-ins for predicted keys with a made base - #2262

Closed
roandegraaf wants to merge 1 commit into
seedds:mainfrom
roandegraaf:predict-made-base
Closed

roandegraaf wants to merge 1 commit into
seedds:mainfrom
roandegraaf:predict-made-base

Conversation

@roandegraaf

Copy link
Copy Markdown
Contributor

What and why

Entering Hyrule Castle Town in Ocarina of Time hitched for about 150 ms, on every visit and in every run, even with all of the area's pipelines prewarmed and archived.

Cause. On a pipeline miss the replay predicts the pipelines the rest of the submission will need (pso_predict_rest_, dm/gfx/nvn/nvn_mtl.m). With hybrid pipelines it made a pulling stand-in for every key it proposed, to learn whether that key's draw would wait: an archive lookup of the pulling base on the submit thread each, outside its budget of 2 builds. In the town, all the proposed keys were "alternatives": each draw's key without its depth / stencil attachment, for a pass that attaches them on demand. Those keys are rarely drawn, so they never enter the pipeline cache, the user's prewarm list or the archive's use, and every prediction of every run proposed them again. A Metal 4 base does not depend on the depth and stencil formats, so each alternative shares the base of the key that is drawn; a draw of it would take that base for a specialization without compiling. The stand-in was never needed.

Change. General, in the host: the prediction first asks whether a proposed key's base is already made (pso_base_made). If it is, the key gets no stand-in and no background build, and is left out of later predictions' candidates (g_pp_easy), where it used to fill the 160 places for alternatives. Without Metal 4 bases nothing changes.

  • Reference entry: Pulling stand-ins made for predicted keys that are never drawn, with its line in the symptom index.
  • Game note: two lines in dm/games/0400486024DEC000/README.md (Frame rate, Found on this game).
  • Test: t_pso_depth_stencil gets the case prediction_made_base; its older prediction case now uses a second library, because its keys must have no base made.

Open:

  • A proposed key with no base made still gets its stand-in lookup on the submit thread. Moving it to the background needs a caller that asks without compiling to wait for a lookup in flight; otherwise the draw falls to a full compile.
  • 40 of the 49 archive files on the test Mac have no index, so a lookup asks them in turn: about 1.0–1.3 ms a lookup. How that time splits was not measured.

Checked

  • Tests, on this branch (ced36121 plus the change), GAME=0400486024DEC000, all pass: make -C dm/gfx/tests check_t_pso_depth_stencil 293 / 296 / 293 / 296 checks in its four configurations, check_t_pso_pending 52, check_t_pso_archive 338, check_t_pull 422, check_t_pso_warm 123 and 123, check_t_pso_stall 73, check_t_pso_bundle 71, check_t_pull_inv 6. With the pso_base_made test in the prediction turned off, prediction_made_base fails 3 checks in each Metal 4 configuration. make -C dm/host builds; python3 -m pytest -q dm/tools/tests/test_docs.py passes.
  • Games run, with title ID and version: The Legend of Zelda: Ocarina of Time, 0400486024DEC000, v131072, chain cb2e91c138e3, on a host built from 8db5dd45 with and without the change: a recorded walk into Hyrule Castle Town replayed from a fixed save, headless, 70 s, DM_FPS_HOLD=off, one run a side; the pipelines up to frame and long-frame log lines compared. Not repeated in the game on ced36121: only the tests and the build were run there.
  • Devices and OS: Apple M4 Pro (20-core GPU), macOS 27.2. No iPhone run.
  • scripts/regress.py check --games: not run. Ocarina of Time is the only game built on this Mac. The prediction runs for every NVN game that misses a pipeline, so the change reaches all of them; the tests above are the only check outside this game.
  • Measurements before and after (the route above):
Before After
The three predictions in town, on the submit thread 30.8 / 104.4 / 54.5 ms 0.5 / 2.0 / 1.4 ms
The 104.4 ms prediction 0 likely keys, 160 alternatives, 160 stand-ins, 100 found archive lookups no stand-in for a key whose base is made
pulling pipelines made 244, against 1 draw with a hybrid pipeline 1
The longest frame at the entry 149 ms (new pipelines 1 + shader libraries 0, 123.8 ms in all, which includes prediction time) no longer a long frame

The log's summary before: pipelines up to frame 1500: on the submit thread pipelines 123.8 ms, predictions 1 / 122.6 ms; all threads: archive lookups found 100 / 119.0 ms.

In a later run with this and other pending changes together, the worst 300-frame window had 15.3 ms of pipeline work on the submit thread (6 predictions, 13.5 ms).

Two windowed play sessions on the same route before the change: 219.7 and 272.5 ms of pipeline work on the submit thread in the entry's 300-frame window, with frames of 150–172 ms. A second visit in a new run was no better, because the keys behind it are never drawn and so never recorded.

Docs

  • The finding is in its game note or its docs/reference/<subsystem>.md entry, and python3 dm/tools/docs_symptoms.py was run (the symptom index)
  • A chain tool changed: dmsh_chain.py --header was run and dm/gfx/nvn/dmsh_chain.h is part of the change
  • No game files, keys or data extracted from a game in the change or the attachments

…e is made: pso_predict_rest_ asks pso_base_made first, hands nothing to the background for such a key and leaves it out of later candidates (g_pp_easy), instead of making a stand-in (an archive lookup of the pulling base on the submit thread) for every key it proposed, outside its budget of 2. A Metal 4 base does not depend on the depth and stencil formats, so a draw's key without those attachments, which the prediction proposes for a pass that attaches them on demand, shares the drawn key's base; it is rarely drawn, so it never entered g_pso, a prewarm list or the archive's use, and every prediction of every run proposed it again. Ocarina of Time's Hyrule Castle Town (v131072, a host on 8db5dd4, Apple M4 Pro, headless, one run a side): the route's three predictions 30.8 / 104.4 / 54.5 ms on the submit thread, one of them 160 stand-ins with 100 found archive lookups and a frame of 149 ms, → 0.5 / 2.0 / 1.4 ms; pulling pipelines made 244 → 1. t_pso_depth_stencil's prediction_made_base pins it (its prediction case takes a second library, whose keys have no base made). Open: a proposed key with no base made still gets its stand-in lookup on the submit thread; no five-game regress run (one game built on this Mac), not repeated in the game on ced3612
@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 10, 2026
@roandegraaf
roandegraaf marked this pull request as ready for review November 10, 2026 23:10
seedds added a commit that referenced this pull request Nov 11, 2026
…whose base is made: after a submission's first pipeline miss, pso_predict_rest_ proposes each remaining draw's key and that key without its depth / stencil attachment, and with hybrid pipelines it made the pulling stand-in of every proposed key to learn whether its draw would wait, an archive lookup of the pulling base on the submit thread each, outside its budget of 2 builds. A Metal 4 base does not depend on the depth and stencil formats (pbase_key, pso_desc4), and a draw asks for its own pipeline without compiling before any stand-in (pso_build_ex(..., 0)), so a key whose base is made is one specialization away and its stand-in was never used; the attachment-free keys are rarely drawn, never enter the pipeline cache or a prewarm list, and were proposed again in every run. The prediction now asks first whether the key's base is made (pso_base_made): such a key gets no stand-in and no background build and is left out of later predictions' candidates (g_pp_easy). Without Metal 4 bases nothing changes; with DM_PSO_HYBRID=0 such a key is specialized by its draw instead of in the background. The author's measurement, Ocarina of Time v131072 entering Hyrule Castle Town on an M4 Pro, one run a side on a host from 8db5dd4: the three predictions 30.8 / 104.4 / 54.5 ms -> 0.5 / 2.0 / 1.4 ms on the submit thread, pulling pipelines made 244 -> 1. On main: t_pso_depth_stencil (new case prediction_made_base; 293 / 296 / 293 / 296 checks), t_pso_pending, t_pso_archive, t_pull, t_pso_warm, t_pso_stall, t_pso_bundle, t_pull_inv and test_docs pass and the host builds; no game run and no regress run on main. Open: a proposed key with no base made still gets its stand-in lookup on the submit thread (PR #2262)

Co-authored-by: Roan de Graaf <36153028+roandegraaf@users.noreply.github.com>
@seedds

seedds commented Nov 11, 2026

Copy link
Copy Markdown
Owner

Landed unchanged on main as 6a6a018, with you as co-author. Thank you: the diagnosis holds on current main (a base carries no depth or stencil format, and a draw asks for its pipeline without compiling before any stand-in), and the pipeline tests, the docs test and the host build pass there.

Still open, as your description says: a proposed key with no base made gets its stand-in lookup on the submit thread. No game was run on main with the change here, so the town-entry numbers remain yours from 8db5dd4; a run on current main would be welcome if you have the route at hand.

@seedds seedds closed this Nov 11, 2026
@roandegraaf

Copy link
Copy Markdown
Contributor Author

Re-measured on ced3612 with its own shader libraries (chain bc8830d4ba5a), as asked above; this is the PR's commit 038492e on ced3612, not main at 6a6a018.

The direction of the body's result holds, the sizes are smaller: from a warmed archive the baseline spent 0–71.5 ms a run in predictions on the submit thread and made 0–157 pulling pipelines for 0–60 draws that used one; the branch spent 0–4.3 ms and made 0–8 for 0–1. The branch does not get to "1 pulling pipeline for 1 draw" as the body has it: in the 2 of 6 runs where a prediction still cost time it made 5 and 8 for one draw, which is the open case of the description (a proposed key with no base made).

Machine and settings: Apple M4 Pro (20-core GPU, 24 GB), macOS 27.2, profile 0400486024DEC000 v131072, headless, DM_FPS_HOLD=off, audio off, hosts built from ced3612 and 038492e, the same rewritten modules. The route is the recorded walk from the drawbridge into the town, 70 s, about 3,970 frames. Every run starts from a copy of the same pipeline archive, warmed by the prebuild tool (34,649 shader pairs, 32,988 bases built, 0 failed) and then by 21 runs of the three routes I have with the baseline host and with a local merge of my five open branches. Six runs a side in two interleaved sets of three; two more baseline runs come from another comparison of the same night.

Per run, from the pipelines up to frame lines ced3612 (8 runs) branch (6 runs)
Runs with prediction time on the submit thread 5 of 8 2 of 6
Predictions, time on the submit thread 0.0, 0.0, 0.0, 6.3, 8.3, 29.3, 41.7, 71.5 ms 0.0, 0.0, 0.0, 0.0, 3.8, 4.3 ms
Pipelines, time on the submit thread 0.9–73.2 ms 1.6–6.9 ms
Worst 300-frame window: predictions 13 / 32.4 ms and 1 / 35.0 ms 1 / 4.3 ms
Worst 300-frame window: pipelines on the submit thread 36.5 ms 4.3 ms
Pulling pipelines made 0, 0, 0, 10, 10, 45, 75, 157 0, 0, 0, 0, 5, 8
Draws with a hybrid pipeline 0, 0, 0, 1, 1, 1, 2, 60 0, 0, 0, 0, 1, 1
Longest frame with pipeline time in it 55.0 ms (36.5 ms of pipelines), then 54.1 ms (29.3 ms) and 40.3 ms (20.9 ms) 33.3 ms (4.3 ms of pipelines), then 30.6 ms (4.3 ms)
Frame rate, last 1,200 frames 57.96–60.12 fps 57.43–59.17 fps
Game's GPU time 15.14–15.86 ms a frame 15.62–16.02 ms a frame
Other processes' GPU time 5.8–8.1 ms/s 6.4–8.5 ms/s
[RED] lines 0–1 a run 0–1 a run

Notes on the table:

  • The rows listing every run are sorted by value, not paired by run. The baseline run with 157 pulling pipelines had them in three windows (48, 50 and 58 made; 1, 1 and 58 draws with a hybrid pipeline).
  • Compared with the body (8db5dd4): predictions 30.8 / 104.4 / 54.5 ms → 0.5 / 2.0 / 1.4 ms there, 0–71.5 → 0–4.3 ms a run here; pulling pipelines 244 against 1 draw → 1 there, up to 157 against 60 → 5 and 8 against 1 here; longest frame 149 ms → none there, 55.0 ms → 33.3 ms here.
  • Which keys a run misses varies from run to run on both sides (three of eight baseline runs missed none), so with six to eight runs these are ranges, not a paired comparison.
  • The red lines are texture type mismatch … slot 7, at most one a run, on both sides (3 of 8 baseline runs, 3 of 6 branch runs).
  • The archive never reached a run that compiled no pipeline base at all: after the warming, runs of either host still compiled 0–24 bases, nearly all in the first 600 frames (the load).

Pictures: one frame 44 s into each run (DM_CTL shot), mean absolute difference of 255: branch against baseline 19.8–20.4, two baseline runs against each other 19.9–20.4. The shot lands in the middle of the walk through a crowd, so this only says the branch differs from the baseline no more than a run does from itself; the pictures look right by eye on both sides.

Tests on the branch at ced3612, all 0 failed: t_pso_depth_stencil 293 / 296 / 293 / 296 checks, t_pso_pending 52, t_pso_archive 338, t_pull 422, t_pso_warm 123 (twice), t_pso_stall 73, t_pso_bundle 71, t_pull_inv 6; test_docs.py 6 passed.

Not checked: main at 6a6a018 itself (the host and the shader libraries here are ced3612's), a cold archive, a real window, iOS, other games. GPU time by process was sampled from outside the host over 19 s inside the measured window.

@dean0396

Copy link
Copy Markdown
Contributor

On the open case in the description (a proposed key with no base made still gets its stand-in lookup on the submit thread): in my fork, the prediction makes no stand-ins at all.

Instead of calling hybrid_get for each queued key whose base isn't made (main nvn_mtl.m:11627), it tells from the shaders whether the draw would get a stand-in. That's the case when:

  • the vertex shader has a pulling entry that isn't marked unusable, and writes no layer;
  • the program has a fragment shader that doesn't read the primitive ID. Without a fragment shader there's no Metal 4 base, and with the primitive ID the pipeline is built whole.

Those keys get no stand-in and no speculative build in the prediction. If one is drawn, pso_get_'s hybrid branch makes its stand-in at the draw, as it does now. So the lookup happens once per key that is actually drawn, not once per proposed key. Keys that fail the test always wait, so they keep their speculative build.

Numbers, all from my fork (M3 Max, Ocarina of Time v131072). It doesn't have main's pso_base_made, so they are for this rule alone:

  • Before the change, entering the Water Temple made 684 stand-ins in one 300-frame window, none of them drawn, and spent 128 ms in predictions.
  • With it, in two scripted soak runs (29,400 and 27,600 frames):
    • child Castle Town made 9 stand-ins for 97 draws that used one;
    • adult Kakariko made 25 for 308;
    • no 300-frame window spent more than 12.4 ms in predictions.

These are other routes than roandegraaf's, so they don't compare one to one with the table above.

The cost: a pair whose pulling base is neither made nor archived no longer gets a head start from the prediction (hybrid_get used to start building that base). On the Mac that's rare, because the app bundles the bases: none of my logged windows had a miss with no pulling base ready. It would matter where the bases aren't ready yet, such as a device with no bundled archive before its own prebuild has made them, so there the old path may be better. I kept the old behaviour behind a switch for A/B.

If it's wanted on main, I can open a PR for pso_predict_rest_ with a case next to prediction_made_base in t_pso_depth_stencil.

neko973 pushed a commit to neko973/nxnative that referenced this pull request Nov 11, 2026
…whose base is made: after a submission's first pipeline miss, pso_predict_rest_ proposes each remaining draw's key and that key without its depth / stencil attachment, and with hybrid pipelines it made the pulling stand-in of every proposed key to learn whether its draw would wait, an archive lookup of the pulling base on the submit thread each, outside its budget of 2 builds. A Metal 4 base does not depend on the depth and stencil formats (pbase_key, pso_desc4), and a draw asks for its own pipeline without compiling before any stand-in (pso_build_ex(..., 0)), so a key whose base is made is one specialization away and its stand-in was never used; the attachment-free keys are rarely drawn, never enter the pipeline cache or a prewarm list, and were proposed again in every run. The prediction now asks first whether the key's base is made (pso_base_made): such a key gets no stand-in and no background build and is left out of later predictions' candidates (g_pp_easy). Without Metal 4 bases nothing changes; with DM_PSO_HYBRID=0 such a key is specialized by its draw instead of in the background. The author's measurement, Ocarina of Time v131072 entering Hyrule Castle Town on an M4 Pro, one run a side on a host from 8db5dd4: the three predictions 30.8 / 104.4 / 54.5 ms -> 0.5 / 2.0 / 1.4 ms on the submit thread, pulling pipelines made 244 -> 1. On main: t_pso_depth_stencil (new case prediction_made_base; 293 / 296 / 293 / 296 checks), t_pso_pending, t_pso_archive, t_pull, t_pso_warm, t_pso_stall, t_pso_bundle, t_pull_inv and test_docs pass and the host builds; no game run and no regress run on main. Open: a proposed key with no base made still gets its stand-in lookup on the submit thread (PR seedds#2262)

Co-authored-by: Roan de Graaf <36153028+roandegraaf@users.noreply.github.com>
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.

3 participants