Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions dm/games/0400486024DEC000/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ From an empty save: at about 45 s `A` (title), `A` (New Game), then the movie. F
- [Compute pipelines compiled at first use](../../../docs/reference/shaders.md#compute-pipelines-compiled-at-first-use): the offline archive holds a compute pipeline for each of the 4,125 compute shaders (480 MB, 204 s on an `applegpu_g18g` Mac). From `save_ref` to the village with an empty prewarm list, all 103 compute pipelines came from it and none were built. PR #1522 measured what that saves with the Metal cache cold: 195 s of pipelines on the submit thread before, 155 s of it in the title movie's twelve kernels.
- [Compute pipelines outside the bundled archive built at every start](../../../docs/reference/shaders.md#compute-pipelines-outside-the-bundled-archive-built-at-every-start): without the bundle, the first start's flushes wrote 88 compute pipelines into the device's archive, and the second start took 88 from it.
- [Metal's archiver store filled by one flush period](../../../docs/reference/rendering.md#metals-archiver-store-filled-by-one-flush-period): a compacting prewarm of 40,000 rows filled the archiver's store, Kakariko drawn without 1,502 pipelines (PR #1735).
- [A cold start leaves a file per timer flush, and the next start compacts them all](../../../docs/reference/rendering.md#a-cold-start-leaves-a-file-per-timer-flush-and-the-next-start-compacts-them-all): a cold start wrote 80 run files and the next start captured 8,859 pipelines again, 762 s before the first frame on an M4 with 16 GB; with the change 5 files, and 3.3 s (issue #1747).
- [Texture IDs of one storage share one packaged texture](../../../docs/reference/nvn-objects.md#texture-ids-of-one-storage-share-one-packaged-texture)
- [A texture shared by its packaged texture's serials left the residency set with one of them](../../../docs/reference/nvn-objects.md#a-texture-shared-by-its-packaged-textures-serials-left-the-residency-set-with-one-of-them): GPU page faults 15-45 minutes into play (issue #1326), on 260x260 terrain tiles made GPU textures at the GPU's first write.
- [Finalizing a copy of a texture object](../../../docs/reference/nvn-objects.md#finalizing-a-copy-of-a-texture-object)
Expand Down
21 changes: 14 additions & 7 deletions dm/gfx/nvn/nvn_mtl.m
Original file line number Diff line number Diff line change
Expand Up @@ -7702,6 +7702,7 @@ static int pso4_on(void) {
// left as they are
enum { PSO_ARCH_COMPACT = 8 };
static int g_compact_enabled; // immutable after pso4_init; unlike g_compact_files, never cleared by the archive queue
static _Atomic int g_compact_bases; static _Atomic uint64_t g_compact_ns; // render bases captured again and their time (the prepare line)
static NSArray<NSString *> *g_compact_files; static _Atomic int g_compacted;
// g_compact_periods: periods that may hold recaptured bases, handed over and not written yet; g_compact_closed: the last of them is handed over (the first flush after the prewarm);
// g_compact_bad: one was not written, or a base the prewarm took from an old file was not captured again: the old files stay (arch_compact_finish)
Expand Down Expand Up @@ -8055,9 +8056,10 @@ static void arch_write_file(ArchEpoch *ep) {
}
// A flush: the compiles from here on capture into a new period, and the current one is written once its compiles have returned. Returns at once, on any thread; a compile that
// does not return holds back only its own period's file. only: hand over that period alone, and only while it is the current one (a full period, a failed capture); those go
// ahead during a compacting prewarm, where the timer's flushes wait so that the recaptured bases make as few files as they can
// ahead during the prewarm, where the timer's flushes wait so that its bases, compiled or captured again, make as few files as they can: a cold start's timer flushes wrote a
// file each 10 s (Ocarina of Time: 67 files; GitHub issue #1747's, 43), more than PSO_ARCH_COMPACT, and the next start compacted them all
static void arch_flush_from(ArchEpoch *only) {
if (!g_arch_cap || (!only && g_compact_enabled && atomic_load(&g_warm_running))) return;
if (!g_arch_cap || (!only && atomic_load(&g_warm_running))) return;
os_unfair_lock_lock(&g_ep_lock); int any = (!only || g_ep == only) && (atomic_load(&g_ep->n) || atomic_load(&g_ep->active)); os_unfair_lock_unlock(&g_ep_lock);
if (!any) return; // nothing captured or under way: the period goes on
ArchEpoch *ne = ep_new();
Expand Down Expand Up @@ -8207,8 +8209,9 @@ static void pbase_key_of(MTLRenderPipelineDescriptor *d, pbase_key *k) {
// Metal can crash in materializeAirNTFromOnlineForSlice when serializing archive imports mixed with fresh bases (#342).
// Work around that framework bug by capturing the original shader functions, without lookupArchives. The normal compiler cache still applies.
int cap = 0; id<MTLRenderPipelineState> cb = arch_capturing() ? arch_capture(b, nil, vd, &cap) : nil;
g_compact_ns += bp_now() - t1;
if (cb) base = cb;
if (cap) g_compacted++; else g_compact_bad = 1; // not in the new files: the old ones stay
if (cap) { g_compacted++; g_compact_bases++; } else g_compact_bad = 1; // not in the new files: the old ones stay
}
if (!base) {
if (g_arch4.count + g_arch4p.count) { g_pt_miss_n++; g_pt_miss_ns += t1 - t0; }
Expand Down Expand Up @@ -8881,8 +8884,10 @@ static void warm_finish(void) {
(unsigned long long)g_warm_shaders, (unsigned long long)g_warm_rps, (unsigned long long)g_warm_skip_fz, (unsigned long long)g_warm_cps, (bp_now() - g_warm_t0) / 1e6, (unsigned long long)g_warm_skip_stale);
if (g_warm_mps) MLOG("pipeline prewarm: %llu native task / mesh pipelines; so far %llu taken from the pipeline archive, %llu built and captured for it, %llu built without it",
(unsigned long long)g_warm_mps, (unsigned long long)g_mtk_arch, (unsigned long long)g_mtk_built, (unsigned long long)g_mtk_dev);
if (g_comp4) MLOG("pipeline bases so far: %llu compiled, %llu from the archive, %llu reused (%.1f ms for the bases); %llu specializations (their descriptors %s), %llu pipelines without a base (MTLDevice)",
(unsigned long long)g_pb_built, (unsigned long long)g_pb_archived, (unsigned long long)g_pb_reused, g_pb_base_ns / 1e6, (unsigned long long)g_pb_spec,
char again[96] = ""; // only a compacting start captures bases again
if (g_compact_enabled) snprintf(again, sizeof again, " (%d of them captured again for the compaction, %.1f ms)", (int)g_compact_bases, g_compact_ns / 1e6);
if (g_comp4) MLOG("pipeline bases so far: %llu compiled, %llu from the archive%s, %llu reused (%.1f ms for the bases); %llu specializations (their descriptors %s), %llu pipelines without a base (MTLDevice)",
(unsigned long long)g_pb_built, (unsigned long long)g_pb_archived, again, (unsigned long long)g_pb_reused, g_pb_base_ns / 1e6, (unsigned long long)g_pb_spec,
(const char *[]){"not asked for yet", "returned retained", "returned autoreleased"}[atomic_load(&g_spec_own) % 3], (unsigned long long)g_pb_legacy);
if (g_arch_bundle || g_arch_cap) MLOG("compute pipelines so far: %llu from the archive bundled with the app, %llu from this device's archive, %llu built (%.1f ms looking up, %.1f ms building)",
(unsigned long long)g_cps_arch, (unsigned long long)g_cps_run, (unsigned long long)g_cps_built, g_cps_look_ns / 1e6, g_cps_build_ns / 1e6);
Expand Down Expand Up @@ -8938,10 +8943,12 @@ static void warm_prepare(NSArray<WarmTask *> *tasks, long lines) {
if (shown) front_progress("Preparing shaders", g_warm_done, lines);
}
if (shown) front_progress(NULL, lines, lines);
char again[64] = ""; // only a compacting start captures bases again
if (g_compact_enabled) snprintf(again, sizeof again, " (%d of them captured again for the compaction)", (int)g_compact_bases);
MLOG("pipeline prepare: %ld lines (%llu shaders, %llu render pipelines, %llu compute pipelines) in %.2f s on %d workers before the first frame, progress screen %s; "
"bases %llu compiled, %llu from the archive, %llu reused; %llu specializations",
"bases %llu compiled, %llu from the archive%s, %llu reused; %llu specializations",
lines, (unsigned long long)g_warm_shaders, (unsigned long long)g_warm_rps, (unsigned long long)g_warm_cps, (bp_now() - t0) / 1e9, workers, shown ? "shown" : "not shown",
(unsigned long long)(g_pb_built - built0), (unsigned long long)(g_pb_archived - arch0), (unsigned long long)(g_pb_reused - reused0), (unsigned long long)(g_pb_spec - spec0));
(unsigned long long)(g_pb_built - built0), (unsigned long long)(g_pb_archived - arch0), again, (unsigned long long)(g_pb_reused - reused0), (unsigned long long)(g_pb_spec - spec0));
}
// The prebuild in the prepare step: the bases of every shader pair are made before the game's first frame, behind the progress screen ("Preparing pipelines"), once per chain
// version, depth mode and pair list (prebuilt.tsv), so a running game never compiles them meanwhile. Built in the background while playing, they took an iPhone 17 Pro Max's GPU
Expand Down
53 changes: 50 additions & 3 deletions dm/gfx/tests/t_pso_archive.m
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Checks: pipelines that differ only in colour state share one base; a specialized pipeline draws the same pixels as one built through the MTLDevice; a flushed archive
// file serves the base again without compiling; a flush of bases without a vertex layout is a p<seq> file, where a base with a layout is not looked up; a function with no
// Metal 4 descriptor keeps the MTLDevice path; pso_key_norm drops what no pipeline depends on; compaction merges the files the prewarm took bases from into one per full period despite a
// mid-prewarm timer flush, removing them only after its last period, and a compaction that did not finish is not tried again on the same system build; depth-only draws write depth; concurrent colour variants compile
// mid-prewarm timer flush, removing them only after its last period, counting them for the prepare line, and a compaction that did not finish is not tried again on the same system build; a timer flush waits for any prewarm, so a cold start writes a file per full period; depth-only draws write depth; concurrent colour variants compile
// one base; archive flushes preserve unrelated compiler state; a function with function constants is specialized by its pipeline's descriptor, not through its library,
// and an MTLDevice pipeline gets it specialized; the descriptor for a specialization is the caller's whether the driver returns it retained or autoreleased; a compute pipeline is captured into a plain file and taken from it later, one without a Metal 4 descriptor never looked up; a native mesh pipeline is captured into a p file and taken from it later, with or without a task shader or a pixel shader, captured again by a compaction, and one without a Metal 4 descriptor built through the MTLDevice; a capture that fails builds the pipeline all the same and hands a full period over, or stops capturing after a failure in a period that captured nothing
#include <stdlib.h>
Expand Down Expand Up @@ -497,7 +497,7 @@ int main(int argc, char **argv) { @autoreleasepool {
if (getenv("DM_TEST_ARCHIVE_CONSTANTS")) src = [src stringByReplacingOccurrencesOfString:@"fragment O fs() { return" withString:@"constant bool enabled [[function_constant(0)]]; fragment O fs() { if (is_function_constant_defined(enabled) && enabled) discard_fragment(); return"];
if (argc == 3) {
NSString *metal = [@(argv[2]) stringByAppendingPathComponent:@"fixture.metal"], *library = [metal stringByAppendingString:@"lib"];
if (!strcmp(argv[1], "--compact-seed")) {
if (strstr(argv[1], "-seed")) { // a mode that writes the archive builds the shader library
[src writeToFile:metal atomically:YES encoding:NSUTF8StringEncoding error:nil];
NSTask *task = [NSTask new]; task.executableURL = [NSURL fileURLWithPath:@"/usr/bin/xcrun"];
task.arguments = @[@"-sdk", @"macosx", @"metal", metal, @"-o", library];
Expand All @@ -508,6 +508,31 @@ int main(int argc, char **argv) { @autoreleasepool {
} else g_lib = [g_dev newLibraryWithSource:src options:nil error:&err];
if (!g_lib) { fprintf(stderr, "%s\n", err.localizedDescription.UTF8String); return 1; }
g_lib2 = [g_dev newLibraryWithSource:[src stringByAppendingString:@"// another library: the same source would come back as the same library\n"] options:nil error:&err];
if (argc == 3 && !strncmp(argv[1], "--cold-", 7)) { // across fresh processes: a cold start's prewarm with a timer flush after each new base, then the next start
g_arch_period_max = 4; int seed = !strcmp(argv[1], "--cold-seed"), cut = !strcmp(argv[1], "--cold-cut-seed"), after_cut = !strcmp(argv[1], "--cold-after-cut");
pso4_init();
EXPECT("cold restart: no compaction", 0, g_compact_enabled);
uint64_t arch0 = g_pb_archived, built0 = g_pb_built;
atomic_store(&g_warm_running, 1);
for (int i = 0; i <= PSO_ARCH_COMPACT; i++) {
MTLRenderPipelineDescriptor *cd = desc(MTLPixelFormatRGBA8Unorm, 0, MTLPixelFormatRGBA8Unorm, MTLColorWriteMaskAll, 1);
cd.vertexDescriptor.layouts[16].stride = 16 * (i + 70);
EXPECT("cold restart: pipeline", 1, pso_build(cd, &err) != nil);
if (seed || cut) dispatch_sync(g_arch_q, ^{ arch_flush(); }); // the timer's
if (cut && i == 5) { // the cold start ends here, in its prewarm: the full period is written, the 2 captures after it are not
dispatch_group_wait(g_arch_pending, DISPATCH_TIME_FOREVER);
printf("t_pso_archive %s: %d checks, %d failed\n", argv[1], checks, failed);
_exit(failed != 0);
}
}
atomic_store(&g_warm_running, 0); arch_flush_wait();
int expect_archived = seed ? 0 : after_cut ? 4 : PSO_ARCH_COMPACT + 1;
EXPECT("cold restart: compiled in the cold start, from the archive in the next", expect_archived, (int)(g_pb_archived - arch0));
EXPECT("cold restart: compiled where not in the archive", PSO_ARCH_COMPACT + 1 - expect_archived, (int)(g_pb_built - built0));
[NSFileManager.defaultManager removeItemAtPath:@(dir) error:nil];
printf("t_pso_archive %s: %d checks, %d failed\n", argv[1], checks, failed);
return failed != 0;
}
// A fresh process is essential: a compiler that built the source archives still has their online binaries.
if (argc == 3) {
int seed = !strcmp(argv[1], "--compact-seed");
Expand Down Expand Up @@ -601,7 +626,7 @@ int main(int argc, char **argv) { @autoreleasepool {
NSMutableArray<id<MTL4Archive>> *open = [NSMutableArray array]; NSMutableArray<NSString *> *paths = [NSMutableArray array];
for (NSString *n in names.reverseObjectEnumerator) { NSString *f = [@(cdir) stringByAppendingPathComponent:n]; [paths addObject:f]; [open addObject:[g_dev newArchiveWithURL:[NSURL fileURLWithPath:f] error:&err]]; }
nvn_hm_clear(&g_pbase, hm_rel); g_arch4 = open; // as a new start with that many files
g_compact_enabled = 1; g_compact_files = paths; g_compacted = 0;
g_compact_enabled = 1; g_compact_files = paths; g_compacted = 0; g_compact_bases = 0; g_compact_ns = 0;
g_arch_period_max = 4; // the recaptured bases fill two periods and part of a third: each full one is handed over, the timer's flushes wait
uint64_t arch0 = g_pb_archived, built0 = g_pb_built;
ArchEpoch *ep = g_ep; ArchiveGateCompiler *gate = [ArchiveGateCompiler new]; gate->compiler = ep->comp; gate->calls = 1; ep->comp = (id<MTL4Compiler>)gate;
Expand All @@ -619,6 +644,8 @@ int main(int argc, char **argv) { @autoreleasepool {
EXPECT("compaction: every base from the archive", arch0 + descs.count, g_pb_archived);
EXPECT("compaction: none compiled", built0, g_pb_built);
EXPECT("compaction: every base captured again", descs.count, g_compacted);
EXPECT("compaction: the prepare line counts them", descs.count, g_compact_bases);
EXPECT("compaction: and their time", 1, g_compact_ns > 0);
MarkerSerializer *ms = [MarkerSerializer new]; ms->ser = g_ep->ser; g_ep->ser = (id<MTL4PipelineDataSetSerializer>)ms; // the period being written
arch_flush_wait();
EXPECT("compaction: the marker exists while the flush writes", 1, ms->calls == 1 && ms->marked == 1);
Expand Down Expand Up @@ -649,6 +676,26 @@ int main(int argc, char **argv) { @autoreleasepool {
[NSFileManager.defaultManager removeItemAtPath:@(cdir) error:nil]; g_arch_dir = saved; g_arch4 = @[];
}
compute_pipelines();
// a timer's flush waits for the prewarm's end, compacting or not, and so a cold start writes a file per full period, not one per 10 s: here 3 files for 9 new bases,
// where a file per timer flush made 9, more than PSO_ARCH_COMPACT, and the next start compacted them all
{
char cdir[] = "/tmp/t_pso_cold.XXXXXX"; if (!mkdtemp(cdir)) abort();
NSString *saved = g_arch_dir; g_arch_dir = @(cdir); nvn_hm_clear(&g_pbase, hm_rel); g_arch4 = @[]; g_arch4p = @[];
g_arch_period_max = 4; atomic_store(&g_warm_running, 1);
for (int i = 0; i <= PSO_ARCH_COMPACT; i++) {
MTLRenderPipelineDescriptor *cd = desc(MTLPixelFormatRGBA8Unorm, 0, MTLPixelFormatRGBA8Unorm, MTLColorWriteMaskAll, 1);
cd.vertexDescriptor.layouts[16].stride = 16 * (i + 70);
if (!pso_build(cd, &err)) { fprintf(stderr, "cold: no pipeline %d\n", i); failed++; }
ArchEpoch *ep = g_ep;
dispatch_sync(g_arch_q, ^{ arch_flush(); }); // the timer's
if (i % 4 != 3) EXPECT("cold: the timer's flush waits for the prewarm's end", 1, g_ep == ep);
}
atomic_store(&g_warm_running, 0); arch_flush_wait();
NSUInteger files = [NSFileManager.defaultManager contentsOfDirectoryAtPath:@(cdir) error:nil].count;
EXPECT("cold: a file per full period and one for the rest", (PSO_ARCH_COMPACT + 4) / 4, files);
nvn_hm_clear(&g_pbase, hm_rel); g_arch_period_max = ARCH_PERIOD_MAX;
[NSFileManager.defaultManager removeItemAtPath:@(cdir) error:nil]; g_arch_dir = saved; g_arch4 = @[];
}
mesh_pipelines();
// a function with no Metal 4 descriptor: the MTLDevice path
uint64_t legacy = g_pb_legacy;
Expand Down
Loading
Loading