Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Diagnostics for Ocarina of Time's task hang: DM_NVN_A5288_C3 and DM_N…
…VN_MESH_GRID_LOG

On Apple silicon (M1, 16 GB here; #1326, #1605) Ocarina of Time's GPU hangs fall in passes where its
Hi-Z task a5288d0a6c4971fd launches mesh 3439aaa071d868ae. Reading the hung pass offline (#1605)
found no raw global loads in its task, mesh or vertex shaders. The task's only loop scans a Hi-Z
rectangle per object, clamped to c3[193].xy - 1: the occlusion tile grid's width and height, ints at
byte 3088 of Maxwell constant buffer 3 (NVN uniform binding 0), with the tiles as floats of the same
block from byte 3120 (#1326). Garbage there (float bits read as ints) would make the scan run for
seconds, or read past the block. Telling the suspects apart needs those values per draw: the game
rewriting the block while the GPU reads it, a stale binding, or a write the host loses.

DM_NVN_A5288_C3=1 keeps, for every a5288 draw, c3[193].xy, the block's address and size, whether an
inline update of the block is deferred in the pass, and the fence of its command buffer. It logs a
change of value, address or size (and once every 900 frames), and logs in red a W x H whose tiles
do not fit the block, with the values read as floats too. At each submission's completion it reads
the values again and counts the draws where they changed since encode, that is, a block rewritten
while the GPU could read it. On kIOGPUCommandBufferCallbackErrorHang it lists the hung command
buffer's draws against the values of the 64 command buffers before it.

DM_NVN_MESH_GRID_LOG=1 logs direct task / mesh draws of more than 1,024 workgroups. #1708 confirms
1,024 as the mesh grid limit on M1 / M2. Indirect counts are written by the GPU and are not seen here.

Both are off by default and cost one getenv when off. docs/env_switches.tsv lists them, with
DM_NVN_TEXDUMP_DEPTH from the previous commit.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
  • Loading branch information
ycarogabriel-lgtm and claude committed Nov 8, 2026
commit 9cd89ea4daf552f9730ab9f10239b3db0664ea87
4 changes: 4 additions & 0 deletions dm/gfx/nvn/nvn_geometry.inc
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,10 @@ static id<MTLRenderPipelineState> mtk_pso(NvProg *raster) {
static void mtk_encode(id<MTLRenderCommandEncoder> en_) {
id<MTL4RenderCommandEncoder> en = (id<MTL4RenderCommandEncoder>)en_; NvShader *ts = g_mtk.ts, *ms = g_mtk.ms;
[en setArgumentTable:g_vt atStages:MTLRenderStageMesh];
{ static int gl = -1; if (gl < 0) { const char *e = getenv("DM_NVN_MESH_GRID_LOG"); gl = e && *e == '1'; } // diagnostic, off by default (#1708: 1,024 threadgroups is the mesh grid limit on M1 / M2)
if (gl && !g_mtk.ind && g_mtk.count > 1024) { static uint64_t n; static uint32_t mx; n++; if (g_mtk.count > mx) mx = g_mtk.count;
if (n <= 16 || !(n & 255)) MLOG("mesh grid: a direct %s draw of %u workgroups (task %s, mesh %s): %s; %llu such draws, the largest %u", ts ? "task" : "mesh", g_mtk.count, ts ? ts->key : "-", ms->key,
ts ? "the object grid; its mesh grid comes from the task's payload" : "a mesh grid past 1,024 (#1708)", (unsigned long long)n, mx); } }
uint64_t first_gpu = g_mtk.ind_first;
if (!g_mtk.ind) { id<MTLBuffer> b; size_t o = arena_alloc(16, &b); memcpy((uint8_t *)b.contents + o, &g_mtk.first, 4); first_gpu = b.gpuAddress + o; }
if (ts) { [en setArgumentTable:g_ot atStages:MTLRenderStageObject]; g_args_dirty[3] = 1; encode_args(en, ts, 3, 0);
Expand Down
48 changes: 48 additions & 0 deletions dm/gfx/nvn/nvn_mtl.m
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,51 @@
gpu_lost_release();
front_gpu_lost();
}
// DM_NVN_A5288_C3=1 (diagnostic, off by default; #1605): Ocarina of Time's Hi-Z task a5288d0a6c4971fd scans a rectangle of its Hi-Z texture per object, clamped to
// c3[193].xy - 1 (the Hi-Z width and height, ints at byte 3088 of Maxwell constant buffer 3, which is NVN uniform binding 0: c<n> is binding n - 3); its only loop. Each a5288 draw's c3[193].xy is kept with the fence of its command
// buffer: a change of value is logged, and a GPU hang prints the values of the hung command buffer against those of the last command buffers that completed
static _Atomic uint64_t g_cur_frame; static uint64_t g_stat_pass; // tentative, defined below
enum { A5C_N = 4096 };
static struct { uint64_t fence, frame, pass, addr, size; int32_t x, y; uint8_t deferred; } g_a5c[A5C_N]; static _Atomic uint32_t g_a5c_i;
static int a5c_fits(int32_t x, int32_t y, uint64_t size);
static int a5c_on(void) { static int v = -1; if (v < 0) { const char *e = getenv("DM_NVN_A5288_C3"); v = e && *e == '1'; } return v; }
static void a5c_report(NSError *e, uint64_t fence) {
if (!a5c_on() || !e || ![e.description containsString:@"kIOGPUCommandBufferCallbackErrorHang"]) return;
uint32_t end = atomic_load(&g_a5c_i); int nh = 0, np = 0; char hv[512] = "", pv[512] = ""; int32_t px[16], py[16]; int pn[16], npv = 0;
for (uint32_t k = 0; k < A5C_N && k < end; k++) { uint32_t i = (end - 1 - k) & (A5C_N - 1);
if (g_a5c[i].fence == fence) { if (nh++ < 12) { size_t o = strlen(hv); snprintf(hv + o, sizeof hv - o, " F%llu p%llu %d,%d@%#llx/%llu%s%s", (unsigned long long)g_a5c[i].frame, (unsigned long long)g_a5c[i].pass, g_a5c[i].x, g_a5c[i].y, (unsigned long long)g_a5c[i].addr, (unsigned long long)g_a5c[i].size, a5c_fits(g_a5c[i].x, g_a5c[i].y, g_a5c[i].size) ? "" : "(PAST the block)", g_a5c[i].deferred ? "(deferred update)" : ""); } }
else if (g_a5c[i].fence < fence && g_a5c[i].fence + 64 >= fence) { np++; int j = 0; while (j < npv && (px[j] != g_a5c[i].x || py[j] != g_a5c[i].y)) j++; if (j == npv && npv < 16) { px[npv] = g_a5c[i].x; py[npv] = g_a5c[i].y; pn[npv++] = 0; } if (j < 16) pn[j]++; } }
for (int j = 0; j < npv; j++) { size_t o = strlen(pv); snprintf(pv + o, sizeof pv - o, " %d,%d x%d", px[j], py[j], pn[j]); }
MLOG("[RED] a5288 c3[193].xy (Hi-Z size; the scan rectangle is clamped to it - 1): hung command buffer (fence %llu) %d draws:%s; the 64 command buffers before it %d draws, values:%s", (unsigned long long)fence, nh, nh ? hv : " none", np, npv ? pv : " none");
}
// The occlusion tiles are floats of the same block from byte 3120 (#1326, the maintainer): W x H tiles must fit in it, or the scan reads past the block (a page fault)
static int a5c_fits(int32_t x, int32_t y, uint64_t size) { return x > 0 && y > 0 && x <= 65536 && y <= 65536 && 3120 + (uint64_t)x * (uint64_t)y * 4 <= size; }
static void a5c_note(const char *key, uint64_t addr, uint64_t size, int deferred) { // at a task-stage binding of c3 (uniform binding 0)
if (!a5c_on() || !key || strncmp(key, "a5288d0a6c4971fd", 16)) return;
int32_t x = 0, y = 0; if (size >= 194 * 16) { memcpy(&x, (const void *)(addr + 193 * 16), 4); memcpy(&y, (const void *)(addr + 193 * 16 + 4), 4); }
uint32_t i = atomic_fetch_add(&g_a5c_i, 1) & (A5C_N - 1);
g_a5c[i].fence = atomic_load(&g_fence) + 1; g_a5c[i].frame = atomic_load(&g_cur_frame); g_a5c[i].pass = g_stat_pass; g_a5c[i].x = x; g_a5c[i].y = y; g_a5c[i].deferred = (uint8_t)deferred;
g_a5c[i].addr = addr; g_a5c[i].size = size;
static int32_t lx = INT32_MIN, ly = INT32_MIN; static uint64_t lf, la, ls; static uint32_t nbad;
int fits = a5c_fits(x, y, size); float fx, fy; memcpy(&fx, &x, 4); memcpy(&fy, &y, 4);
if (!fits && nbad++ < 64) MLOG("[RED] a5288 c3[193].xy = %d,%d (as floats %g,%g) does not fit its block at %#llx (%llu bytes; %lld tiles from byte 3120) at frame %llu pass %llu%s", x, y, fx, fy, (unsigned long long)addr, (unsigned long long)size, (long long)x * y, (unsigned long long)g_a5c[i].frame, (unsigned long long)g_stat_pass, deferred ? "; an inline update of the block is deferred in this pass" : "");
if (x != lx || y != ly || addr != la || size != ls || g_a5c[i].frame >= lf + 900) {
MLOG("a5288 c3[193].xy = %d,%d at frame %llu pass %llu, block %#llx %llu bytes (Hi-Z size; worst-case scan rectangle %lld tiles, %s the block)%s", x, y, (unsigned long long)g_a5c[i].frame, (unsigned long long)g_stat_pass, (unsigned long long)addr, (unsigned long long)size, (long long)x * y, fits ? "within" : "PAST",
deferred ? ", an inline update of the block is deferred in this pass: memory may not hold the value the GPU reads" : "");
lx = x; ly = y; la = addr; ls = size; lf = g_a5c[i].frame; }
}
// At each submission's completion (DM_NVN_A5288_C3): its a5288 draws' c3[193].xy read again from memory. A different value means the block was rewritten while the GPU could
// still read it (the CPU of the next frame, or an update the host applied late): the first hypothesis of #1326 for a garbage size
static void a5c_complete(uint64_t fence) {
if (!a5c_on()) return;
static uint64_t ndraw, nchanged, nlog; uint32_t end = atomic_load(&g_a5c_i);
for (uint32_t k = 0; k < A5C_N && k < end; k++) { uint32_t i = (end - 1 - k) & (A5C_N - 1); if (g_a5c[i].fence < fence) break; if (g_a5c[i].fence != fence || g_a5c[i].size < 194 * 16) continue;
int32_t x, y; memcpy(&x, (const void *)(g_a5c[i].addr + 193 * 16), 4); memcpy(&y, (const void *)(g_a5c[i].addr + 193 * 16 + 4), 4); ndraw++;
if (x != g_a5c[i].x || y != g_a5c[i].y) { nchanged++;
if (nlog++ < 32 || !(nlog & 255)) MLOG("[RED] a5288 c3[193].xy of a draw at frame %llu pass %llu was %d,%d at encode and is %d,%d when its submission (fence %llu) completed: the block was rewritten while the GPU could read it (%llu of %llu draws so far)",
(unsigned long long)g_a5c[i].frame, (unsigned long long)g_a5c[i].pass, g_a5c[i].x, g_a5c[i].y, x, y, (unsigned long long)fence, (unsigned long long)nchanged, (unsigned long long)ndraw); } }
static uint64_t lastrep; if (ndraw >= lastrep + 20000) { lastrep = ndraw; MLOG("a5288 c3[193].xy at completion: %llu of %llu draws differ from their encode-time value", (unsigned long long)nchanged, (unsigned long long)ndraw); }
}
// DM_TEST_GPU_LOST=n (for testing): from the n-th submission on, imitate the revoked access without faulting the GPU: each submission's feedback reports it, in the shape the #1326
// logs show, and the GPU's signal of its fence is left out (its work still runs)
static int gpu_lost_test(void) {
Expand Down Expand Up @@ -2261,6 +2306,7 @@
uint64_t c0 = 0, c1 = 0; if (seg_ts()) { c0 = gpu_s_guest_ns(cb.GPUStartTime); c1 = gpu_s_guest_ns(cb.GPUEndTime); }
if (cb.error) { atomic_store(&g_last_commit_error, 1); res_top_once(cb.error); static int n, nv; int victim = [cb.error.description containsString:@"InnocentVictim"]; if (victim ? nv++ < 1 : n++ < 8) MLOG("[RED] command buffer error details fence=%llu: %s | GPU %.3f→%.3f", (unsigned long long)fence, cb.error.description.UTF8String, cb.GPUStartTime, cb.GPUEndTime); if (!victim) MLOG("[RED] state of the failed submission fence=%llu: %s", (unsigned long long)fence, cctx.UTF8String); MLOG("[RED] command buffer execution error: %s", cb.error.localizedDescription.UTF8String); }
char where[64]; snprintf(where, sizeof where, "fence %llu, its last command buffer", (unsigned long long)fence); cbs_feedback(cbs, cb.error, where);
a5c_report(cb.error, fence); a5c_complete(fence);
gpu_lost_check(lost_test ? gpu_lost_test_error() : cb.error, "submission");
post_complete(fence, c0, c1);
if (atomic_load(&g_gpu_lost)) gpu_lost_release(); // the GPU signals none of these fences
Expand Down Expand Up @@ -4044,7 +4090,7 @@
cpu_unc_take(t);
}
static uint64_t n[2]; if (n[unc]++ < 4) MLOG("texture written by the CPU before its first use ser=%llu %ux%ux%u format %#x%s pool flags %#x: uploaded from its storage, re-uploaded when it changes (%llu so far)",
(unsigned long long)t->serial, t->d.width, t->d.height, t->d.depth, t->d.format, unc ? " (uncompressed, in storage the GPU has not written)" : "", t->pool->flags, (unsigned long long)n[unc]);

Check failure on line 4093 in dm/gfx/nvn/nvn_mtl.m

View workflow job for this annotation

GitHub Actions / clang-tidy (ratchet)

Access to field 'flags' results in a dereference of a null pointer (loaded from field 'pool') [clang-analyzer-core.NullDereference] (10 in this file, the baseline has 9)
return 1;
}
// nvn_ft_poll (hle_nvn.c, tex_dirty_upload): the CPU wrote the pages of a registered texture after its first use. It is taken from its storage by the rule tex_cpu_content uses at first
Expand Down Expand Up @@ -9825,6 +9871,7 @@
if (!u || !u->addr || !mtl_gpu_addr(u->addr, u->size ? u->size : 1, &mb, &mo, &gpu)) { // reads 0 (the all-zero bank), as an unbound texture slot does, instead of through a null pointer
RED_ONCE(16, "stage %d cb%d has no uniform buffer bound", nvn_stage, reg); slots[aid] = zero_bank_gpu(); USE(zero_bank(), MTLResourceUsageRead); continue; }
uint64_t ul = cb_len(sh, reg, u->size); // what the shader can read of it
if (nvn_stage == 3 && reg == 0) a5c_note(sh->key, u->addr, u->size, g_nubd && ubd_hits(u->addr, u->addr + ul));
if (g_nubd && ubd_hits(u->addr, u->addr + ul)) { // inline updates deferred in this pass (see mtl_sub_update); ubd_conflict made sure they cover the binding or the rest can be read from memory
uint64_t g; const uint8_t *c; if (ubd_covers(u->addr, u->addr + ul)) gpu = ubd_shadow(u->addr, u->addr + ul);
else if (ubp_get(u->addr, u->addr + ul, &g, &c)) gpu = g;
Expand Down Expand Up @@ -13800,6 +13847,7 @@
if (sc) { if (k < SEG_CLK_N) { atomic_store(&sc->t[k][0], gpu_s_guest_ns(cb.GPUStartTime)); atomic_store(&sc->t[k][1], gpu_s_guest_ns(cb.GPUEndTime)); } seg_clk_put(sc); }
if (cb.error) { atomic_store(&g_last_commit_error, 1); static int k; if (k++ < 8) MLOG("[RED] segment command buffer execution error: %s", cb.error.description.UTF8String); res_top_once(cb.error); }
char where[64]; snprintf(where, sizeof where, "fence %llu, segment %u", (unsigned long long)cbs_fence, k); cbs_feedback(cbs, cb.error, where);
a5c_report(cb.error, cbs_fence);
gpu_lost_check(cb.error, "segment");
}];
gmem_commit_patch();
Expand Down
5 changes: 4 additions & 1 deletion docs/env_switches.tsv
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ DM_NICK switch The account nickname the game sees (default `Player`)
DM_NRO switch `0` turns off loading of the NRO modules the game's NRR files register (on by default): every `nn::ro::LoadModule` then fails
DM_NRO_CAPTURE diagnostic folder: a `nn::ro::LoadModule` with no rewritten copy writes the image to `<folder>/<SHA-256>.nro` and ends the run (the NSP script's capture of an NRO the game decodes at run time)
DM_NRO_DIR path folder of NROs the shader split reads like `main`, and in which it finds a CRA archive's file table (the NSP script sets it for a captured NRO)
DM_NVN_A5288_C3 diagnostic `=1`: for Ocarina of Time's Hi-Z task a5288d0a6c4971fd, logs c3[193].xy (the Hi-Z width and height that bound its scan), its uniform block's address and size and whether the tiles fit it, counts draws whose value changed by their submission's completion, and lists them for a command buffer that hung
DM_NVN_ALLOCPROF diagnostic `=1` charges Metal allocation growth to each replayed op type and function, and logs command allocator sizes and each new texture (serial, size, format, storage); `=2` also gives each main submission a fresh command allocator and reports command memory by operation, by encoder, by submission size and the indirect-count multi-draws encoded
DM_NVN_API switch `=<major>.<minor>` overrides the NVN API version `nvnDeviceGetInteger` reports (by default decided by the SDK version or the profile's `device_api`: 55.15 for SDK 11 and later, 53.313 before)
DM_NVN_BARRIER switch How draw passes are ordered: `full` (default) waits for all earlier GPU work, `game` only for the previous pass's fragment stage unless the game set a barrier, `ab` alternates with the `DM_AB` phases
Expand Down Expand Up @@ -164,6 +165,7 @@ DM_NVN_LAZY_CLEAR switch `0` makes clears with no draw after them open a pass of
DM_NVN_LOADPROBE diagnostic `=1`, `2` or `3`: attachments whose load is skipped are cleared to a sentinel colour (`3` NaN; `1` NaN for float formats) and the sentinel pixels left at pass end are counted (should be 0)
DM_NVN_LOG diagnostic `=1` logs the creation of NVN objects
DM_NVN_MAGSCAN diagnostic `=<fs key>[:every[:phase]]`: device self-check that reads each pass's colour attachments every `every` frames (default 30) and logs `[mag]` lines for solid magenta 16×16 blocks or non-finite pixels
DM_NVN_MESH_GRID_LOG diagnostic `=1`: logs direct task / mesh draws of more than 1,024 workgroups (the mesh grid limit on M1 / M2, #1708)
DM_NVN_PASSDUMP diagnostic `=<f>`, `<f1>-<f2>` or `every:<n>`: logs every render pass of those frames (attachments, clears, shader keys, bindings, draw state, presents, and the host function and line that ended each pass) for the `tbdr_*.py` tools
DM_NVN_PASSDUMP_ALL diagnostic `=1` makes `DM_NVN_PASSDUMP` record every draw and binding change, not only the first per pass and stage, including copies and inline uniform updates
DM_NVN_PASSGPU diagnostic `=<file>` writes the GPU time of each render and compute pass from stage-boundary timestamp counters (off under Metal 4); also gives each dispatch its own encoder
Expand Down Expand Up @@ -194,7 +196,8 @@ DM_NVN_T2T_FROM diagnostic `=<frame>`: `DM_NVN_T2T_TRACE` records copies only fr
DM_NVN_T2T_TRACE diagnostic `=<W>x<H>:<file>`: after each copy into an RGBA16F texture of that size, copies the result into a trace buffer on the GPU timeline; 240 entries are written to the file
DM_NVN_TBDR switch `=1` turns on the replay's TBDR pass optimizations (store / load elision, pass merging, same-target continuation); off by default since bindless textures defeat their read inference
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