From 425ad34f6672701a5a62ed08b715b9d7c9b2a020 Mon Sep 17 00:00:00 2001 From: seedds Date: Mon, 9 Nov 2026 14:47:39 -0500 Subject: [PATCH] A vertex library with layer entry points of its own gets bases for its main0 pairs: pso_offline.py and the host prebuild looked for render_target_array_index anywhere in the library, not in the entry the pair uses, as the shader loader does, so Ocarina of Time's effect vertex shaders (no stage inputs, no stand-in) were compiled at their first draw: a 230 ms frame at a camera turn in Hyrule Field, 39 ms after (M6, 2,675 more bases) Co-Authored-By: Claude Opus 5.5 --- dm/games/0400486024DEC000/README.md | 1 + dm/games/0400486024DEC000/bench.tsv | 2 ++ dm/gfx/nvn/nvn_mtl.m | 2 +- dm/gfx/tests/t_pso_bundle.m | 11 +++++++++++ dm/gfx/tools/pso_offline.py | 4 ++-- dm/tools/tests/test_pso_offline.py | 7 +++++-- docs/agents/symptoms.md | 1 + docs/reference/shaders.md | 9 +++++++++ 8 files changed, 32 insertions(+), 5 deletions(-) diff --git a/dm/games/0400486024DEC000/README.md b/dm/games/0400486024DEC000/README.md index f3a91ddbab..0c0a9d9df4 100644 --- a/dm/games/0400486024DEC000/README.md +++ b/dm/games/0400486024DEC000/README.md @@ -119,6 +119,7 @@ From an empty save: at about 45 s `A` (title), `A` (New Game), then the movie. F - [Frame-step stubs placed by their function](../../../docs/reference/rewriter.md#frame-step-stubs-placed-by-their-function) - [Repository patches missing under bracketed paths](../../../docs/reference/rewriter.md#repository-patches-missing-under-bracketed-paths): resolved, a build in a folder named with brackets lost this game's frame-step stubs and cuts without an error. - [Retired pool buffers pile up while a game submits nothing](../../../docs/reference/memory.md#retired-pool-buffers-pile-up-while-a-game-submits-nothing): out of focus GPU allocated went from 11 to 20 GB in 12 s with swap following (PR #1392); the likely cause of a session that stayed frozen after a window switch. Not reproduced on the village-deck route (2026-11-09, M6): 40 s out of focus, during the load or standing on the deck, made no pool deletions at all, so the scene that remakes a virtual pool's runs out of focus is still to be found. +- [Vertex shaders with layer entry points left out of the prebuild](../../../docs/reference/shaders.md#vertex-shaders-with-layer-entry-points-left-out-of-the-prebuild): resolved, a 230 ms frame at a camera turn in Hyrule Field. The effect vertex shaders (no stage inputs, layer entry points of their own) had no base in the bundled archive and were compiled at their first draw; with the fix the archive holds 33,365 bases instead of 30,690, and the worst frame on the `field-run` route is 39 ms (M6). - [Position invariance across shaders](../../../docs/reference/shaders.md#position-invariance-across-shaders): white shapes on plants and bushes for single frames in Kokiri Forest, from the foliage's mesh pre-pass and mesh colour pass rounding the position apart. - [gl_FragCoord.z against the depth stored for its own surface](../../../docs/reference/shaders.md#gl_fragcoordz-against-the-depth-stored-for-its-own-surface): the dark specks flickering in the ground mist (long-standing). The mist's own depth test (`5ad65c0eefd09c7f`, against its depth-only pass `4757b5b373a5406e`) rejected its own surface where Apple's GPU stored the depth one ulp under the fragment's z. Bisected by dropping fragment shaders: after mesh draws took the drop too (`skip_fs_draw`), the specks went with the mist or its depth-only pass, and when that pass wrote `[[position]].z` as depth. Resolved by the rewrite: no specks in play with its 39 rewritten shaders as an overlay (the player's check, 2026-11-09); a full library build on the new chain has not been run. diff --git a/dm/games/0400486024DEC000/bench.tsv b/dm/games/0400486024DEC000/bench.tsv index 4c2a0fc728..4842bf51ee 100644 --- a/dm/games/0400486024DEC000/bench.tsv +++ b/dm/games/0400486024DEC000/bench.tsv @@ -2,3 +2,5 @@ kokiri-deck 35:A~0.2,40:A~0.2,43:A~0.2,105:RRight~4,111:LUp~3,116:RLeft~4,122:LUp~3 85 45 the Kokiri Forest deck save: standing 20 s, then the camera turns right, Link walks, the camera turns left, he walks again title-movie 0:L0/0 20 30 the title movie from an empty save, the NDVE networks at the game's own quality (PR #1741) kokiri-still 35:A~0.2,40:A~0.2,43:A~0.2,50:A~0.2,57:A~0.2,64:A~0.2 85 180 the Kokiri Forest deck save, standing still for 3 minutes: for same-process alternating measurements (DM_NVN_UB), whose phases need a scene that does not change +field-run 35:A~0.2,40:A~0.2,43:A~0.2,48:A~0.2,53:A~0.2,105:RRight~4,111:LUp~3,116:RLeft~4,122:LUp~3 85 45 any save that loads with two more A presses than kokiri-deck (a slower title): standing 20 s, then the camera turns right, Link walks, the camera turns left, he walks again +field-still 35:A~0.2,40:A~0.2,43:A~0.2,48:A~0.2,53:A~0.2 85 180 any save that loads with two more A presses than kokiri-deck, standing still for 3 minutes: for same-process alternating measurements (DM_AB) diff --git a/dm/gfx/nvn/nvn_mtl.m b/dm/gfx/nvn/nvn_mtl.m index d0573c60de..95777aec53 100644 --- a/dm/gfx/nvn/nvn_mtl.m +++ b/dm/gfx/nvn/nvn_mtl.m @@ -8798,7 +8798,7 @@ static int pre_lib_absent(NSString *key, NSString *suffix) { NSError *e = nil; if (pre_lib_absent(vk, pull ? @"_bp.metallib" : @"_b.metallib") || pre_lib_absent(fk, @"_b.metallib")) return nil; id vl = lib_from_file([NSString stringWithFormat:@"%@/%@%@.metallib", g_msl_dir, vk, pull ? @"_bp" : @"_b"], &e); - if (!vl || t_lib_layer) return nil; // writes the layer: its pipelines take an input primitive class (pso_desc), no pulling pipeline + if (!vl || (t_lib_layer && !vj[@"entry_layer"])) return nil; // the entry writes the layer: its pipelines take an input primitive class (pso_desc), no pulling pipeline. A library with layer entry points of its own (entry_layer) writes it in those alone, as shader_load takes it (layer_out): Ocarina of Time's effect vertex shaders (no stage inputs, so no stand-in) were compiled at their first draw, 34-74 ms each id fl = lib_from_file([NSString stringWithFormat:@"%@/%@_b.metallib", g_msl_dir, fk], &e); MTL4FunctionDescriptor *vf = pre_fn(vl, ventry), *ff = fl ? pre_fn(fl, fentry) : nil; if (!vf || !ff) return nil; diff --git a/dm/gfx/tests/t_pso_bundle.m b/dm/gfx/tests/t_pso_bundle.m index 0e5dc0c15b..e31a5ac7df 100644 --- a/dm/gfx/tests/t_pso_bundle.m +++ b/dm/gfx/tests/t_pso_bundle.m @@ -137,6 +137,17 @@ int main(void) { @autoreleasepool { EXPECT("batched archive: first group's base", 1, [batchArchive newRenderPipelineStateWithDescriptor:pre_desc(@"v1", @"f1", 0) error:nil] != nil); EXPECT("batched archive: second group's base", 1, [batchArchive newRenderPipelineStateWithDescriptor:pre_desc(@"v1", @"f2", 0) error:nil] != nil); EXPECT("batched archive: absent depth entry stays absent", 0, [batchArchive newRenderPipelineStateWithDescriptor:pre_desc(@"v1", @"f1", 1) error:nil] != nil); + // A vertex library with layer entry points of its own (entry_layer, dmsh_build.py vs_layer_entry) writes the layer in those alone: its main0 pairs get a base, as shader_load + // takes them (layer_out 0). One whose main0 writes it (no entry_layer) gets none: its pipelines take an input primitive class + NSString *vl = @"#include \nusing namespace metal;\nstruct V { float4 p [[position]]; };\nstruct L { float4 p [[position]]; uint l [[render_target_array_index]]; };\n" + "vertex V main0(uint id [[vertex_id]]) { V o; o.p = float4(float(id & 1), float(id >> 1), 0, 1); return o; }\n" + "vertex L main0_l(uint id [[vertex_id]]) { L o; o.p = float4(float(id & 1), float(id >> 1), 0, 1); o.l = 1; return o; }\n"; + if (!lib(msl, @"vl_b", vl) && !lib(msl, @"vw_b", vl)) { + put([msl stringByAppendingPathComponent:@"vl_b.json"], [NSString stringWithFormat:@"{\"chain\": \"%s\", \"entry\": \"main0\", \"entry_layer\": \"main0_l\", \"vertex_inputs\": []}", DMSH_CHAIN]); + put([msl stringByAppendingPathComponent:@"vw_b.json"], [NSString stringWithFormat:@"{\"chain\": \"%s\", \"entry\": \"main0\", \"vertex_inputs\": []}", DMSH_CHAIN]); + EXPECT("prebuild: a library with layer entry points of its own gets main0's base", 1, pre_desc(@"vl", @"f1", 0) != nil); + EXPECT("prebuild: a library whose main0 writes the layer gets none", 0, pre_desc(@"vw", @"f1", 0) != nil); + } EXPECT("batched archive: full coverage stamp preserved", 1, [[NSString stringWithContentsOfFile:[batched stringByAppendingPathComponent:@"prebuilt.tsv"] encoding:NSUTF8StringEncoding error:nil] isEqualToString:[NSString stringWithContentsOfFile:[bundle stringByAppendingPathComponent:@"prebuilt.tsv"] encoding:NSUTF8StringEncoding error:nil]]); // its stamp matches: only the pair with function constants is built here diff --git a/dm/gfx/tools/pso_offline.py b/dm/gfx/tools/pso_offline.py index 9d20f63042..27c0b64288 100644 --- a/dm/gfx/tools/pso_offline.py +++ b/dm/gfx/tools/pso_offline.py @@ -6,7 +6,7 @@ # · the vertex shader's pulling library _bp (a vertex shader without stage inputs: its own _b), entry `entry`, or `entry_z` for the near = 0 depth mode, from its reflection # _b.json; the pixel shader's _b, entry `entry` (main0); no vertex descriptor, one sample, every colour attachment unspecialized # · left out as the host leaves them out (to the draws): a reflection of another chain version or none, a vertex shader with stage inputs and no pulling library, a vertex library that -# writes the layer (render_target_array_index), a pixel shader that reads the primitive ID (its reflection's raster.primitive_id: the host builds its pipelines whole, since +# writes the layer (render_target_array_index) from the entry the pair uses (not one whose library has layer entry points of its own, entry_layer: its main0 writes none), a pixel shader that reads the primitive ID (its reflection's raster.primitive_id: the host builds its pipelines whole, since # through a base every primitive reads 0), a missing library or entry point # · a pair whose functions declare function constants is left to the device (rest_pairs.tsv): the host specializes such functions with an empty set of values, and metal-tt makes # no specialized function from a library (applegpu-nt: "cannot find private metadata"), nor the function unspecialized ("cannot lower module with unresolved function constants") @@ -140,7 +140,7 @@ def writes_layer(lib): vl, fl = vk + ('_bp' if pull else '_b'), fk + '_b' vf, ff = fn(vl), fn(fl) if vf is None or ff is None: left['library'] += 1; continue - if writes_layer(vl): left['layer'] += 1; continue + if writes_layer(vl) and not vj.get('entry_layer'): left['layer'] += 1; continue # the entry the pair uses writes the layer; a library with layer entry points of its own (entry_layer) writes it in those alone, as the host takes it (shader_load layer_out) if ventry not in vf or fentry not in ff: left['entry'] += 1; continue if vf[ventry] or ff[fentry]: rest.append((vk, fk)); continue b = (vl, ventry, fl, fentry) diff --git a/dm/tools/tests/test_pso_offline.py b/dm/tools/tests/test_pso_offline.py index 9a0fa3b73c..a9848109a4 100644 --- a/dm/tools/tests/test_pso_offline.py +++ b/dm/tools/tests/test_pso_offline.py @@ -109,10 +109,13 @@ def test_bases_rest_and_left_out(self): self.shader('fsx', {'entry': 'main1'}, {'_b': [('main0', False)]}) self.shader('fsp', {'raster': {'primitive_id': 1}}, {'_b': [('main0', False)]}) # reads the primitive ID: no base self.shader('mesh', {'entry_z': 'main0_z', 'mesh_stage': {'kind': 'ms'}}, {'_b': vz}) # a mesh shader's pair (for its padding): built at the draw, no base + # Layer entry points of its own (dmsh_build.py vs_layer_entry): main0 writes no layer, as the host takes it (shader_load layer_out). Ocarina of Time's effect vertex shaders, + # with no stage inputs and so no stand-in, were left out and compiled at their first draw + self.shader('layered', {'entry_z': 'main0_z', 'entry_layer': 'main0_l', 'vertex_inputs': []}, {'_b': (vz + [('main0_l', False)], True)}) pairs = [('pull', 'fs'), ('plain', 'fs'), ('inputs', 'fs'), ('layer', 'fs'), ('other', 'fs'), ('nolib', 'fs'), ('pull', 'fsc'), ('pull', 'fsx'), ('pull', 'gone'), ('pull', 'fs'), - ('plain', 'fsp'), ('mesh', 'fs')] + ('plain', 'fsp'), ('mesh', 'fs'), ('layered', 'fs')] bases, rest, left = po.plan(self.d, CHAIN, pairs, False) - self.assertEqual(bases, [('pull_bp', 'main0', 'fs_b', 'main0'), ('plain_b', 'main0', 'fs_b', 'main0')]) + self.assertEqual(bases, [('pull_bp', 'main0', 'fs_b', 'main0'), ('plain_b', 'main0', 'fs_b', 'main0'), ('layered_b', 'main0', 'fs_b', 'main0')]) self.assertEqual(rest, [('pull', 'fsc')]) self.assertEqual(left, {'reflection': 2, 'mesh': 1, 'stage inputs': 1, 'library': 1, 'layer': 1, 'primitive ID': 1, 'entry': 1}) bases, _, _ = po.plan(self.d, CHAIN, pairs[:2], True) # near = 0: the entry_z entries diff --git a/docs/agents/symptoms.md b/docs/agents/symptoms.md index bc709640d5..2e165eb93d 100644 --- a/docs/agents/symptoms.md +++ b/docs/agents/symptoms.md @@ -1040,6 +1040,7 @@ One line per symptom in the [subsystem references](../reference/): what you see - a game without a recorded prewarm list (a provisional profile) stalls at its first dispatches while Metal compiles their compute pipelines. The Mac app's pipeline archive held render bases only (`archive.tsv`: `bases`), whatever the shader map listed. → [Compute pipelines compiled at first use](../reference/shaders.md#compute-pipelines-compiled-at-first-use) - a compute pipeline the archive bundled with the app does not hold (no bundle, as in a headless boot or a build without `pso_offline.py`; a shader outside the shader map; an OS update the bundle's binaries do not survive) was compiled again at its first dispatch of every start the system's shader cache had dropped it, and the device's own pipeline archive (`pso_archive`) never held one: `compute pipelines so far: 0 from the archive bundled with the app, N built` start after start. → [Compute pipelines outside the bundled archive built at every start](../reference/shaders.md#compute-pipelines-outside-the-bundled-archive-built-at-every-start) - the first sight of a scene drawn with task and mesh shaders freezes for seconds on a cold Metal cache, on every start once the system's shader cache has dropped the pipelines, although the prewarm finished: `task / mesh pipeline: task … mesh … fragment …, N ms required at frame F`. Before ea0c98df the draw was left out instead, and the foliage stayed missing or white until the pipeline was ready. → [Native mesh pipelines compiled at their first draw](../reference/shaders.md#native-mesh-pipelines-compiled-at-their-first-draw) +- a frame of 200 ms or more the first time an effect appears, although the bundled pipeline archive was trusted and the prewarm finished. With `DM_PSO_WAITLOG=1` the frame shows waits on render pipelines with no pulling entry, 34-74 ms each, for vertex shaders that have no stage inputs. → [Vertex shaders with layer entry points left out of the prebuild](../reference/shaders.md#vertex-shaders-with-layer-entry-points-left-out-of-the-prebuild) - `[RED] pipeline prewarm: seed list does not exist: …/pso_manifest.tsv` at every start, no `pipeline prewarm list …/pso_user.tsv` line, and the same compile stalls every session in places already visited, while `pso_user.tsv` keeps growing; the pipeline archive gains files every start and is never compacted (Donkey Kong Bananza: 88 files, 7 s to open them). → [The device's prewarm list unread without a seed list](../reference/shaders.md#the-devices-prewarm-list-unread-without-a-seed-list) - the first frame that draws with new programs loads their shader libraries on the submit thread (`[longframe] ... shader libraries N, X ms`): tens of milliseconds with the files cached, seconds when the drive is slow. → [Shader libraries loaded when the program is made](../reference/shaders.md#shader-libraries-loaded-when-the-program-is-made) - at boot, `CRASH sig=5 … pc=_dispatch_once_wait.cold.1` right after the `pipeline bases so far` line, with `bt: pso_prebuild __shmap_load_block_invoke … mtl_prog_prefetch h_nvnProgramSetShaders`. → [The prepare step inside the shader map's once](../reference/shaders.md#the-prepare-step-inside-the-shader-maps-once) diff --git a/docs/reference/shaders.md b/docs/reference/shaders.md index f562418563..ef9e96fbec 100644 --- a/docs/reference/shaders.md +++ b/docs/reference/shaders.md @@ -1990,6 +1990,15 @@ Finding, keying and translating shaders (`dm/gfx/tools/shader_split.py`, `dmsh_b - `t_mesh_first_use`: no first draw is left out. - **Open:** the archive path has run only in `t_pso_archive`, not on a game route. On iOS the Metal 4 path, and with it the archive, is off for a profile without `pso_mtl4 on` (Ocarina of Time has none), so there the rows and the system's shader cache are what keep a mesh pipeline. `pso_offline.py` makes no native mesh pipelines for the archive bundled with the app. +### Vertex shaders with layer entry points left out of the prebuild + +- **Symptom:** a frame of 200 ms or more the first time an effect appears, although the bundled pipeline archive was trusted and the prewarm finished. With `DM_PSO_WAITLOG=1` the frame shows waits on render pipelines with no pulling entry, 34-74 ms each, for vertex shaders that have no stage inputs. +- **Cause:** a vertex library can carry layer entry points of its own (`entry_layer` in its reflection, `main0_l`), which alone write `render_target_array_index`; its `main0` writes none. The shader loader already takes the layer from the entry it uses (`layer_out` in `shader_load`). The offline archive (`pso_offline.py`, `writes_layer`) and the device's prebuild (`pre_desc`) instead looked for the layer anywhere in the library and left every pair of such a shader out. A shader with no stage inputs has no stand-in pipeline, so each of those pairs was compiled at its first draw while the frame waited. +- **Fix:** both take the rule the loader takes: a library with `entry_layer` writes the layer only in those entries, so its `main0` (and `main0_z`) pairs get bases. A library without layer entry points whose code writes the layer is still left out. Shaders are not rebuilt; the archive is. A host with the fix and an archive built before it finds bases the archive lacks, so it trusts it no more and compiles the prebuild on the device in the background. +- **Found on:** [The Legend of Zelda: Ocarina of Time](../../dm/games/0400486024DEC000/README.md#found-on-this-game), v131072, M6 (macOS 27.2): 2,675 pairs had been left out, so the archive grows from 30,690 bases to 33,365. On the `field-run` bench route from a Hyrule Field save (a camera turn at about 105 s), two runs each: the worst frame was 230-234 ms with 4 waits of over 20 ms per run before, and 39 ms with none after. +- **Check:** `dm/tools/tests/test_pso_offline.py` (a library with `entry_layer` gets its `main0` base); `t_pso_bundle` (the prebuild gives such a library's `main0` a base, and a library whose `main0` writes the layer none). +- **Open:** a 39 ms frame at about 117 s on the same route, one pipeline taking 15-20 ms, under the wait log's threshold; not traced. + ### The device's prewarm list unread without a seed list - **Symptom:** `[RED] pipeline prewarm: seed list does not exist: …/pso_manifest.tsv` at every start, no `pipeline prewarm list …/pso_user.tsv` line, and the same compile stalls every session in places already visited, while `pso_user.tsv` keeps growing; the pipeline archive gains files every start and is never compacted (Donkey Kong Bananza: 88 files, 7 s to open them).