From e1e2298bed87d7a524ddc37601cdf87ae3a9eca1 Mon Sep 17 00:00:00 2001 From: seedds Date: Sun, 8 Nov 2026 12:16:29 -0400 Subject: [PATCH] Cache absent pipeline bases during non-compiling archive probes When hybrid demand fills the bounded build queue, draws can repeatedly scan every archive for the same missing base. Remember absent bases under the existing base lock, without suppressing demand compilation or overriding a completed base. Add a regression for repeated misses, independent layout keys and later successful compilation. Record the earlier OoT city measurements with their uncontrolled gameplay limits. --- dm/games/0400486024DEC000/README.md | 9 +++++++++ dm/gfx/nvn/nvn_mtl.m | 8 ++++++++ dm/gfx/tests/t_pso_archive.m | 31 +++++++++++++++++++++++++++++ docs/agents/symptoms.md | 1 + docs/reference/frame-rate.md | 8 ++++++++ 5 files changed, 57 insertions(+) diff --git a/dm/games/0400486024DEC000/README.md b/dm/games/0400486024DEC000/README.md index 2e2c6a829b..2d655aa8c3 100644 --- a/dm/games/0400486024DEC000/README.md +++ b/dm/games/0400486024DEC000/README.md @@ -98,6 +98,7 @@ From an empty save: at about 45 s `A` (title), `A` (New Game), then the movie. F ## Found on this game +- [Repeated archive misses while hybrid pipeline demand is saturated](../../../docs/reference/frame-rate.md#repeated-archive-misses-while-hybrid-pipeline-demand-is-saturated): repeated failed base-pipeline lookups dominated the presentation thread in the city. - [One Pro Controller read as two players](../../../docs/reference/services.md#one-pro-controller-read-as-two-players): resolved, no music in Kokiri Forest gameplay and its cutscenes; the controller applet opened over and over and the game kept its music suspended while it had focus. - [A fixed frame step plays in slow motion on a slow Mac](../../../docs/reference/frame-rate.md#a-fixed-frame-step-plays-in-slow-motion-on-a-slow-mac): the frame-ratio division. - [Frame-step stubs placed by their function](../../../docs/reference/rewriter.md#frame-step-stubs-placed-by-their-function) @@ -161,3 +162,11 @@ From an empty save: at about 45 s `A` (title), `A` (New Game), then the movie. F - [Live resolution variants share physical image storage](../../../docs/reference/rendering.md#live-resolution-variants-share-physical-image-storage): the dynamic-resolution depth buffer (`COMPRESSIBLE` alone) kept 15-16 live sizes at one offset, 14.5 MB each, as separate Metal textures (GPU allocation 7,710 → 7,520 MB in Kokiri Forest before main's Ocarina fixes; the 17 sizes still form on main at 2eb71820, where all Depth32Float textures take 225 MB with them shared). - [Pixel shader inputs the mesh shader does not write](../../../docs/reference/shaders.md#pixel-shader-inputs-the-mesh-shader-does-not-write): the foliage point meshes' pipelines (pixel shaders reading generic attribute 5 that no mesh output writes). - [Mesh outputs as wide as their pixel shaders read](../../../docs/reference/shaders.md#mesh-outputs-as-wide-as-their-pixel-shaders-read): the foliage mesh `626fbf48041cb82b`, padded to 35,840 bytes of mesh output, past Metal's 32 KB. + +## Local city pipeline lookup validation + +[Repeated archive misses while hybrid pipeline demand is saturated](../../../docs/reference/frame-rate.md#repeated-archive-misses-while-hybrid-pipeline-demand-is-saturated) was found on Apple M5 / macOS 27.0.1, game v131072, SDK 22.2.8. These gameplay measurements used baseline `bd979ced3ae6` and a host carrying local frame-step and packaging changes; the candidate added only the miss cache. They are historical evidence for the mechanism, not a fresh performance comparison on current main. In the city, frame 4500's 300-frame window had frame-time p50/p95/max 107.06/169.34/268.74 ms, GPU p50/p95 24.27/31.89 ms, and CPU translation 113.837 ms per frame. Its 9,208 failed archive searches cost 30,532.8 ms; the demand queue held 384 entries. A live sample spent 287/456 presentation-thread samples under Metal archive lookup. + +After the process-local miss cache was installed, the player returned to the same area and reported roughly stable 30 fps. The new frame 3600 window measured frame-time p50/p95/max 28.49/33.78/37.54 ms, GPU p50/p95 27.85/33.33 ms, and CPU translation 6.439 ms per frame. Five failed archive searches cost 14.6 ms over those 300 frames. Earlier new-run windows had similar CPU costs (6.317–6.358 ms). GPU time now dominates those windows. + +This is a same-area gameplay comparison, not a deterministic replay: camera, route and cache warmth were not held identical. Only this game was available locally; the regression harness could not locate its manually assembled build layout. The focused archive regression is rerun on the PR base; no new gameplay result is implied. diff --git a/dm/gfx/nvn/nvn_mtl.m b/dm/gfx/nvn/nvn_mtl.m index 38b66722b7..6b78369433 100644 --- a/dm/gfx/nvn/nvn_mtl.m +++ b/dm/gfx/nvn/nvn_mtl.m @@ -7654,6 +7654,12 @@ @implementation ArchEpoch @end static _Atomic uint64_t g_pt_hit_n, g_pt_hit_ns, g_pt_miss_n, g_pt_miss_ns, g_pt_spec_ns, g_pt_comp_ns, g_pt_pred_n, g_pt_pred_ns, g_pp_done_n; // bases compiled / taken from the archive / reused, specializations, MTLDevice builds typedef struct { const void *vf, *ff; uint32_t samples, a2c, rast, topo, amp; uint32_t va[31][3], vl[31][3]; } pbase_key; // what a base depends on: pso_desc's descriptor less the colour attachments static nvn_hmap g_pbase = {.klen = sizeof(pbase_key)}; +// The archive arrays are opened once by pso4_init and never extended. Remember +// absent bases too: a full pending-build queue can otherwise make every draw +// scan every archive again while its pulling pipeline serves as a stand-in. +// This only skips non-compiling probes; a compiler owner can still build/retry, +// and a completed base in g_pbase always takes precedence. +static nvn_hmap g_pbase_miss = {.klen = sizeof(pbase_key)}; // Only callers for the same base wait on this group; unrelated bases compile in parallel. // compiled: its owner asked the compiler (a compile 0 owner only looks in the archive, and a waiter that needs the base builds it itself when that found none). @interface PbaseBuild : NSObject { @public dispatch_group_t done; id ps; NSError *error; int compiled; } @end @@ -8088,6 +8094,7 @@ static void pbase_key_of(MTLRenderPipelineDescriptor *d, pbase_key *k) { os_unfair_lock_lock(&g_pbase_lock); base = hm_get_id(&g_pbase, &bk); job = nil; owner = 0; if (!base) { + if (!compile && hm_get_id(&g_pbase_miss, &bk)) { os_unfair_lock_unlock(&g_pbase_lock); return nil; } job = hm_get_id(&g_pbase_pending, &bk); if (!job) { job = [PbaseBuild new]; job->done = dispatch_group_create(); dispatch_group_enter(job->done); hm_put_id(&g_pbase_pending, &bk, job); owner = 1; } } @@ -8127,6 +8134,7 @@ static void pbase_key_of(MTLRenderPipelineDescriptor *d, pbase_key *k) { os_unfair_lock_lock(&g_pbase_lock); job->ps = base; if (base) hm_put_id(&g_pbase, &bk, base); + else if (!compile) hm_put_id(&g_pbase_miss, &bk, NSNull.null); hm_rel(nvn_hm_del(&g_pbase_pending, &bk)); dispatch_group_leave(job->done); os_unfair_lock_unlock(&g_pbase_lock); if (!base) { if (compile && e) *e = job->error; return nil; } // NOLINT(clang-analyzer-osx.cocoa.RetainCount): job stays in g_pbase_pending (hm_put_id's CFBridgingRetain) until hm_rel releases it, not a leak diff --git a/dm/gfx/tests/t_pso_archive.m b/dm/gfx/tests/t_pso_archive.m index 6bdbc1de16..ef4aaf64f4 100644 --- a/dm/gfx/tests/t_pso_archive.m +++ b/dm/gfx/tests/t_pso_archive.m @@ -383,6 +383,36 @@ static void spec_desc_ownership(void) { } } } +// Deterministic misses while the real Metal compiler remains available: a miss +// must suppress repeated probes, not suppress demand compilation or another key. +@interface MissingBaseArchive : NSObject { @public unsigned calls; } @end +@implementation MissingBaseArchive +- (id)newRenderPipelineStateWithDescriptor:(MTL4RenderPipelineDescriptor *)d error:(NSError **)e { + (void)d; (void)e; calls++; return nil; +} +@end +static void repeated_archive_miss(void) { + NSArray *saved = g_arch4, *savedp = g_arch4p; + MissingBaseArchive *archive = [MissingBaseArchive new]; + g_arch4 = @[(id)archive]; g_arch4p = @[]; + MTLRenderPipelineDescriptor *d = desc(MTLPixelFormatRGBA8Unorm, 0, MTLPixelFormatRGBA8Unorm, MTLColorWriteMaskAll, 1); + d.vertexDescriptor.layouts[16].stride = 1536; + NSError *e = nil; + for (int i = 0; i < 100; i++) EXPECT("negative probe: no archived pipeline", 1, pso_build_ex(d, &e, 0) == nil); + EXPECT("negative probe: one lookup for 100 misses", 1, archive->calls); + EXPECT("negative probe: no unfinished owner", 0, g_pbase_pending.n); + d.vertexDescriptor.layouts[16].stride = 1552; + EXPECT("negative probe: another layout is independent", 1, pso_build_ex(d, &e, 0) == nil); + EXPECT("negative probe: another key looked up", 2, archive->calls); + d.vertexDescriptor.layouts[16].stride = 1536; + EXPECT("negative probe: demand can still compile", 1, pso_build_ex(d, &e, 1) != nil); + unsigned after = archive->calls; + EXPECT("negative probe: compiled base overrides miss", 1, pso_build_ex(d, &e, 0) != nil); + EXPECT("negative probe: completed base needs no archive scan", after, archive->calls); + g_arch4 = saved; g_arch4p = savedp; + nvn_hm_clear(&g_pbase_miss, hm_rel); +} + int main(int argc, char **argv) { @autoreleasepool { if (!mkdtemp(archive_cache_root)) abort(); atexit(archive_cache_cleanup); @@ -590,6 +620,7 @@ int main(int argc, char **argv) { @autoreleasepool { EXPECT("constants: the vertex function given to it is specialized once", 1, fn_dev(cv) != cv && fn_dev(cv) == fn_dev(cv)); EXPECT("constants: a function without constants is its own", 1, fn_dev(cd.fragmentFunction) == cd.fragmentFunction); } + repeated_archive_miss(); spec_desc_ownership(); capture_preserves_foreign_folder(); [NSFileManager.defaultManager removeItemAtPath:@(dir) error:nil]; diff --git a/docs/agents/symptoms.md b/docs/agents/symptoms.md index 7b6233d6b3..2231834093 100644 --- a/docs/agents/symptoms.md +++ b/docs/agents/symptoms.md @@ -500,6 +500,7 @@ One line per symptom in the [subsystem references](../reference/): what you see - after a loading screen the game's resolution stays at its lowest (Ocarina of Time: 683x384) for minutes while it holds 60 fps with GPU time well under the frame, and climbs back only slowly or not at all. → [Dynamic resolution at its floor while the GPU has time to spare](../reference/frame-rate.md#dynamic-resolution-at-its-floor-while-the-gpu-has-time-to-spare) - many passes a frame on the same attachments, with the residency set line showing `passes ended to land them for a read` in the thousands and `refused …: earlier submission running` matching it. Ocarina of Time: 10,463 per 300 frames, each ending the pass over a uniform block no GPU write touched. → [A full table of running GPU writes ends passes for nothing](../reference/frame-rate.md#a-full-table-of-running-gpu-writes-ends-passes-for-nothing) - a game that reads textures through a pool (Shaders report which textures they read through a pool) renders far below its maximum dynamic resolution: Ocarina of Time's main pass at 763x429 with the GPU at 17 ms a frame. → [A host's own store in a fragment shader moves its depth test](../reference/frame-rate.md#a-hosts-own-store-in-a-fragment-shader-moves-its-depth-test) +- a busy scene runs far below its GPU throughput: CPU submission dominates frame time, the bounded demand-build table is full, and repeated failed archive searches dominate the presentation thread. → [Repeated archive misses while hybrid pipeline demand is saturated](../reference/frame-rate.md#repeated-archive-misses-while-hybrid-pipeline-demand-is-saturated) ## NVN objects diff --git a/docs/reference/frame-rate.md b/docs/reference/frame-rate.md index 74a2c6af69..e2e6cc5a24 100644 --- a/docs/reference/frame-rate.md +++ b/docs/reference/frame-rate.md @@ -532,3 +532,11 @@ Frame pacing: the host's frame step, engine frame limiters, present intervals an - **Fix:** `dmsh_build.py` `pool_arrays`: a fragment shader whose only store is the mark gets `[[early_fragment_tests]]`. One that discards, writes depth, stencil or a sample mask, or stores anything itself keeps its test where it was; there the mark costs the shading. A chain change. - **Check:** `python3 dm/tools/tests/test_dmsh_pool_arrays.py`. On the same route, the game's own controller then held its main pass at 1536x864, four times the pixels, at the same GPU time. - **Found on:** [The Legend of Zelda: Ocarina of Time](../../dm/games/0400486024DEC000/README.md): 56 of the 258 fragment programs of a frame in Kokiri Forest carry the mark, 3 of them with a discard. + +### Repeated archive misses while hybrid pipeline demand is saturated + +- **Symptom:** a busy scene runs far below its GPU throughput: CPU submission dominates frame time, the bounded demand-build table is full, and repeated failed archive searches dominate the presentation thread. +- **Cause:** when `pso_pend_try` finds no slot, `pso_get_` probes the archive before using its pulling stand-in. A full demand table cannot admit that key, so its later draws repeat the same failed archive scan. Successful bases were cached, absent ones were not. +- **Fix:** `g_pbase_miss`, protected by the existing base lock and keyed like the positive cache, suppresses repeated non-compiling probes. The archive arrays are opened once and remain unchanged for the process lifetime. Demand compilation still queries/builds normally, and a completed positive base takes precedence over an earlier miss. No result is persisted across launches. +- **Check:** `t_pso_archive` probes one absent layout 100 times, checks that only one archive lookup occurs, probes a distinct layout independently, then compiles the previously absent base and verifies that non-compiling probes can use it. The archive suite and fresh-process compaction test pass with Metal validation. The game's note records the live before/after measurements and their limits. +- **Found on:** [The Legend of Zelda: Ocarina of Time](../../dm/games/0400486024DEC000/README.md#local-city-pipeline-lookup-validation), Apple M5 / macOS 27.0.1.