Skip to content
Open
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
2 changes: 2 additions & 0 deletions dm/games/0400486024DEC000/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ Run headless, `DM_CTL` at a control file. The work folder has `boot.sh <secs> <l

From an empty save: at about 45 s `A` (title), `A` (New Game), then the movie. From `save_ref`: at about 58 s `A~0.2`, 5 s later `A~0.2` (Load), 30 s later three `A~0.1` 3 s apart, then 12 s to the bridge outside Link's house. There `LUp~3.5`, `A~0.2` (Climb Down) and `LDown~4` three or four times reach the village floor.

Diagnostics for this game's problems, all off by default (listed in [`docs/env_switches.tsv`](../../../docs/env_switches.tsv)): `DM_NVN_TEXDUMP=<first>-<last>:<path>` dumps every frame of a range (`_F<frame>` before the extension) and `DM_NVN_TEXDUMP_DEPTH=1` also saves the depth attachments, which reach the per-actor shadow maps (76x76 depth-only passes made anew each frame, beyond `DM_NVN_TEXRAW`); it was used on an M1 to follow the shadow chain over 9 frames, where every input came from the same frame. The dump synchronizes with the GPU every frame, so it changes timing.

## Open

- Root cause confirmed on M4 Pro (2026-11-08): [ignored event waits](../../../docs/reference/nvn-objects.md#gpu-event-waits-order-work-across-queues) let lighting consumers run before their producers. The game issues about 25 waits and 19 signals per frame. All 297 complete traced frames submit queue 5's ambient-cache consumer `9e6b00b050e30801` before queue 1's SH producer `21d9f83e6027c565`, despite the explicit dependency. Queue 1 also waits for the CPU frame-ready event after the CPU clears the lighting buffer. At frame 3872 the consumer ran at 0.686 ms, the clear at 0.966–0.968 ms, and the producer at 1.282 ms; its release followed at 4.467 ms. Exposure writer `1bba09ec4bc8ff67` has another event dependency. The native SDK's flush/invalidate functions are no-ops for these coherent pools.
Expand Down
69 changes: 65 additions & 4 deletions dm/gfx/nvn/nvn_mtl.m
Original file line number Diff line number Diff line change
Expand Up @@ -16796,12 +16796,34 @@ static void shot(id<MTLTexture> src, const char *path, int screen, const int cro
static void td_dump(void);
// DM_NVN_TEXDUMP=size=WxH:path instead picks the frame by content: the first frame that opens a render pass whose first color attachment level is WxH (for one-off work whose frame differs between runs)
static unsigned g_td_w, g_td_h;
// DM_NVN_TEXDUMP=first-last:path dumps every frame of that range (inclusive), each file name ending in _F<frame> before its extension (a serial that lives across frames would
// otherwise be overwritten). DM_NVN_TEXDUMP_DEPTH=1 also saves the depth attachments of the dumped frames (any size, or only WxH with DM_NVN_TEXDUMP_DEPTH=WxH): the raw
// depth (<name>_depth.raw, header line: width height Metal format bytes per row) and an 8-bit PGM stretched over the depths below 1 (<name>_depth.pgm).
// For the shadow investigation: a per-actor shadow map is a depth-only pass whose texture is made anew each frame, so neither DM_NVN_TEXRAW (by serial) nor the colour dump reaches it
static long g_td_last = -1; static int g_td_depth; static unsigned g_td_dw, g_td_dh; static NSMutableArray *g_td_dlist;
static void td_name(char *path, size_t n, long idx, const char *suffix) { // the dump path for idx, with _F<frame> (range mode) and then suffix, or else the extension, at the end
snprintf(path, n, g_td_path, (int)idx);
char *dot = strrchr(path, '.'), *slash = strrchr(path, '/'); char ext[16] = "";
if (dot && (!slash || dot > slash)) { strlcpy(ext, dot, sizeof ext); *dot = 0; }
if (g_td_last >= 0) { char fr[32]; snprintf(fr, sizeof fr, "_F%ld", g_td_frame); strlcat(path, fr, n); }
strlcat(path, suffix ? suffix : ext, n);
}
static void td_dump_depth(void);
void td_note(MTL4RenderPassDescriptor *pd, NSUInteger nc) {
if (g_td_frame == -2) { const char *e = getenv("DM_NVN_TEXDUMP"), *c = e ? strchr(e, ':') : NULL; g_td_frame = c ? atol(e) : -1; g_td_path = c ? c + 1 : NULL; g_td_list = [NSMutableArray array];
if (g_td_frame == -2) { const char *e = getenv("DM_NVN_TEXDUMP"), *c = e ? strchr(e, ':') : NULL; g_td_frame = c ? atol(e) : -1; g_td_path = c ? c + 1 : NULL; g_td_list = [NSMutableArray array]; g_td_dlist = [NSMutableArray array];
if (c && !strncmp(e, "size=", 5) && sscanf(e + 5, "%ux%u", &g_td_w, &g_td_h) == 2) g_td_frame = LONG_MAX;
if (c && !strncmp(e, "layers=", 7)) { g_td_layers = atoi(e + 7); g_td_frame = (long)atomic_load(&g_cur_frame); } }
if (c && !strncmp(e, "layers=", 7)) { g_td_layers = atoi(e + 7); g_td_frame = (long)atomic_load(&g_cur_frame); }
if (c && isdigit((unsigned char)*e)) { const char *m = strchr(e, '-'); if (m && m < c) { g_td_last = atol(m + 1); if (g_td_last < g_td_frame) g_td_last = -1; } }
const char *d = getenv("DM_NVN_TEXDUMP_DEPTH"); g_td_depth = d && *d && strcmp(d, "0");
if (g_td_depth && sscanf(d, "%ux%u", &g_td_dw, &g_td_dh) != 2) g_td_dw = g_td_dh = 0;
if (g_td_last >= 0 || g_td_depth) MLOG("[texdump] frames %ld-%ld%s", g_td_frame, g_td_last >= 0 ? g_td_last : g_td_frame, g_td_depth ? ", with depth attachments" : ""); }
if (g_td_frame < 0 || g_td_done) return;
long f = (long)atomic_load(&g_cur_frame);
if (g_td_last >= 0 && f > g_td_frame) { // range: dump the frame that ended, then go on with this one while it is in the range
td_dump();
if (f > g_td_last) return;
g_td_frame = f; g_td_done = 0;
}
if (g_td_layers) {
if (f != g_td_lframe) { if (g_td_lframe >= 0 && g_td_lcount >= g_td_layers) { MLOG("[texdump] frame %ld: %d draws into small array / cube layers", g_td_lframe, g_td_lcount); g_td_frame = g_td_lframe; td_dump(); return; }
g_td_lframe = f; g_td_frame = f; g_td_lcount = 0; [g_td_list removeAllObjects]; }
Expand All @@ -16817,6 +16839,44 @@ void td_note(MTL4RenderPassDescriptor *pd, NSUInteger nc) {
BOOL seen = NO; for (NSArray *e in g_td_list) if (e.count < 5 && [e[1] unsignedLongLongValue] == ser && [e[2] unsignedIntegerValue] == lv && [e[3] unsignedIntegerValue] == sl) seen = YES;
if (!seen) [g_td_list addObject:@[t, @(ser), @(lv), @(sl)]];
}
id<MTLTexture> dt = pd ? pd.depthAttachment.texture : nil;
if (g_td_depth && dt && dt.textureType == MTLTextureType2D && R.rt.depth) {
uint64_t ser = R.rt.depth->serial; NSUInteger lv = pd.depthAttachment.level;
if (!g_td_dw || (MAX(dt.width >> lv, 1ul) == g_td_dw && MAX(dt.height >> lv, 1ul) == g_td_dh)) {
BOOL seen = NO; for (NSArray *e in g_td_dlist) if ([e[1] unsignedLongLongValue] == ser && [e[2] unsignedIntegerValue] == lv) seen = YES;
if (!seen) [g_td_dlist addObject:@[dt, @(ser), @(lv)]];
}
}
}
static void td_dump_depth(void) { // after mtl_finish_gpu (td_dump): see DM_NVN_TEXDUMP_DEPTH
for (NSArray *e in g_td_dlist) {
id<MTLTexture> t = e[0]; uint64_t ser = [e[1] unsignedLongLongValue]; NSUInteger lv = [e[2] unsignedIntegerValue];
MTLPixelFormat pf = t.pixelFormat; size_t bpp = pf == MTLPixelFormatDepth16Unorm ? 2 : 4;
if (pf != MTLPixelFormatDepth16Unorm && pf != MTLPixelFormatDepth32Float && pf != MTLPixelFormatDepth32Float_Stencil8) { MLOG("[texdump] depth ser=%llu format %lu skipped", ser, (unsigned long)pf); continue; }
if (t.storageMode == MTLStorageModeMemoryless) continue;
NSUInteger w = MAX(t.width >> lv, 1ul), h = MAX(t.height >> lv, 1ul), row = w * bpp;
id<MTLBuffer> buf = [g_dev newBufferWithLength:row * h options:MTLResourceStorageModeShared];
if (!buf) { MLOG("[texdump] depth ser=%llu: no %lu-byte buffer, skipped", ser, (unsigned long)(row * h)); continue; }
id<MTLCommandBuffer> cb = q_cb(); id<MTLBlitCommandEncoder> b = [cb blitCommandEncoder];
[b copyFromTexture:t sourceSlice:0 sourceLevel:lv sourceOrigin:MTLOriginMake(0, 0, 0) sourceSize:MTLSizeMake(w, h, 1) toBuffer:buf destinationOffset:0 destinationBytesPerRow:row destinationBytesPerImage:row * h
options:pf == MTLPixelFormatDepth32Float_Stencil8 ? MTLBlitOptionDepthFromDepthStencil : MTLBlitOptionNone];
[b endEncoding]; [cb commit]; [cb waitUntilCompleted];
float *z = malloc(w * h * sizeof *z); uint8_t *g = malloc(w * h);
if (!z || !g) { free(z); free(g); continue; }
for (size_t i = 0; i < w * h; i++) z[i] = bpp == 2 ? ((const uint16_t *)buf.contents)[i] / 65535.0f : ((const float *)buf.contents)[i];
float lo = INFINITY, hi = -INFINITY; size_t n1 = 0;
for (size_t i = 0; i < w * h; i++) { if (z[i] >= 1.0f) { n1++; continue; } lo = MIN(lo, z[i]); hi = MAX(hi, z[i]); }
for (size_t i = 0; i < w * h; i++) g[i] = z[i] >= 1.0f ? 255 : hi > lo ? (uint8_t)lrintf(254.0f * (z[i] - lo) / (hi - lo)) : 0;
char path[1024]; FILE *f;
td_name(path, sizeof path, (long)(ser * 100 + lv), "_depth.raw");
if ((f = fopen(path, "wb"))) { fprintf(f, "%lu %lu %lu %lu\n", (unsigned long)w, (unsigned long)h, (unsigned long)pf, (unsigned long)row); fwrite(buf.contents, 1, row * h, f); fclose(f); }
td_name(path, sizeof path, (long)(ser * 100 + lv), "_depth.pgm");
if ((f = fopen(path, "wb"))) { fprintf(f, "P5\n%lu %lu\n255\n", (unsigned long)w, (unsigned long)h); fwrite(g, 1, w * h, f); fclose(f); }
MLOG("[texdump] frame %ld depth ser=%llu lv=%lu format %lu %lux%lu: %zu texels at 1 (cleared), the rest %g..%g → %s", g_td_frame, ser, (unsigned long)lv, (unsigned long)pf, (unsigned long)w, (unsigned long)h, n1,
lo == INFINITY ? 0.0 : (double)lo, hi == -INFINITY ? 0.0 : (double)hi, path);
free(z); free(g);
}
[g_td_dlist removeAllObjects];
}
void td_note_layer(id<MTLTexture> t, uint64_t ser, NSUInteger lv, NSUInteger sl) { // a slice-merged pass's other layers (see g_sm_plan)
if (g_td_frame < 0 || g_td_done || g_td_frame == LONG_MAX || (long)atomic_load(&g_cur_frame) != g_td_frame || !t) return;
Expand Down Expand Up @@ -16899,7 +16959,8 @@ static void td_dump(void) {
MTLSamplerDescriptor *sd = [MTLSamplerDescriptor new]; sp = [g_dev newSamplerStateWithDescriptor:sd];
if (!ps) { MLOG("[RED] TEXDUMP pipeline failed %s", e.localizedDescription.UTF8String); return; }
}
MLOG("[texdump] frame %ld: %lu color targets", g_td_frame, (unsigned long)g_td_list.count);
MLOG("[texdump] frame %ld: %lu color targets, %lu depth targets", g_td_frame, (unsigned long)g_td_list.count, (unsigned long)g_td_dlist.count);
td_dump_depth();
// DM_NVN_TEXRAW=index,...: save raw bytes by texture index (including 3D; file: %d in the path is replaced with the index, suffix .raw, first-line text header: width height depth format bytes-per-row)
for (const char *q = getenv("DM_NVN_TEXRAW"); q && *q; q = strchr(q, ',') ? strchr(q, ',') + 1 : q + strlen(q)) {
if (!strncmp(q, "info", 4)) { // DM_NVN_TEXRAW=info: the textures remembered by DM_NVN_TEXINFO
Expand Down Expand Up @@ -16948,7 +17009,7 @@ static void td_dump(void) {
id<MTLRenderCommandEncoder> en = [cb renderCommandEncoderWithDescriptor:rp];
[en setRenderPipelineState:ps]; [en setFragmentTexture:v atIndex:0]; [en setFragmentSamplerState:sp atIndex:0];
[en drawPrimitives:MTLPrimitiveTypeTriangle vertexStart:0 vertexCount:3]; [en endEncoding]; [cb commit]; [cb waitUntilCompleted];
char path[1024]; snprintf(path, sizeof path, g_td_path, (int)(ser * 100 + sl * 10 + lv));
char path[1024]; td_name(path, sizeof path, (long)(ser * 100 + sl * 10 + lv), NULL);
if (e.count > 4) { snprintf(path, sizeof path, g_td_path, 0); char *dot = strrchr(path, '.'); char ext[16] = ""; if (dot) { strlcpy(ext, dot, sizeof ext); *dot = 0; }
char tail[64]; snprintf(tail, sizeof tail, "_s%03d_%llu_%lu%s", [e[4] intValue], (unsigned long long)ser, (unsigned long)lv, ext); strlcat(path, tail, sizeof path); } // TEXSNAP copy
MLOG("[texdump] ser=%llu lv=%lu layer %lu format %lu %lux%lu", ser, (unsigned long)lv, (unsigned long)sl, (unsigned long)t.pixelFormat, (unsigned long)w, (unsigned long)h);
Expand Down
3 changes: 2 additions & 1 deletion docs/env_switches.tsv
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,8 @@ DM_NVN_TESS_MAX diagnostic `=<level>` the highest tessellation level (even, 2 to
DM_NVN_TESS_ONLY diagnostic `=<hull key>,...` replays only the tessellated draws of those hull shaders (the first 16 hex digits of a key) and skips the others with a [RED] line; to tell their effects apart
DM_NVN_TESS_STAGES diagnostic `=1` / `=2` stops a tessellated draw after the vertex capture / after the hull (and expansion) kernel, skipping the domain draw; to time the stages (default 3, all)
DM_NVN_TEXCPU diagnostic `=1` logs non-packaged (non-swizzled) textures in CPU-visible pools
DM_NVN_TEXDUMP diagnostic `=<frame>:<path>`, `size=<W>x<H>:<path>` or `layers=<n>:<path>`: saves every colour render target of that frame as PNG (`%d` = serial × 100 + layer × 10 + level)
DM_NVN_TEXDUMP diagnostic `=<frame>:<path>`, `<first>-<last>:<path>`, `size=<W>x<H>:<path>` or `layers=<n>:<path>`: saves every colour render target of that frame (or of each frame of the range, with `_F<frame>` before the extension) as PNG (`%d` = serial × 100 + layer × 10 + level)
DM_NVN_TEXDUMP_DEPTH diagnostic `=1` (or `=<W>x<H>` for one size) makes `DM_NVN_TEXDUMP` also save the depth attachments of the dumped frames: the raw depth (`_depth.raw`, a text header line) and an 8-bit PGM stretched over the depths below 1
DM_NVN_TEXINFO diagnostic `=<W>x<H>` logs storage and description of every texture of that size on first use, registration and initialization (pool, flags, packaged, non-zero bytes, swizzles)
DM_NVN_TEXRAW diagnostic `=<serial>,…` or `info` (the textures `DM_NVN_TEXINFO` picked out): saves those textures' raw bytes at the `DM_NVN_TEXDUMP` frame, with a text header line
DM_NVN_TEXSNAP diagnostic `=<in-frame pass index>,…`: with `DM_NVN_TEXDUMP`, copies those passes' colour attachments as they end and saves them with the end-of-frame dump
Expand Down
Loading