diff --git a/dm/games/0400C3F00006E000/README.md b/dm/games/0400C3F00006E000/README.md index fc7aaf5dbe..2123b15d29 100644 --- a/dm/games/0400C3F00006E000/README.md +++ b/dm/games/0400C3F00006E000/README.md @@ -98,7 +98,7 @@ Update 1.8.0 (v917504, SDK 21.4.0) is not on this Mac. Pull request #1116 ported - [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 interval chosen from CPU time. - [Frame-step stubs placed by their function](../../../docs/reference/rewriter.md#frame-step-stubs-placed-by-their-function) - +- [Indirect-count multi-draws split the pass an earlier draw opened](../../../docs/reference/rendering.md#indirect-count-multi-draws-split-the-pass-an-earlier-draw-opened): the scene passes' colour and depth loaded again. - [A NUP with complete standalone updates stops at "no base game"](../../../docs/reference/packing.md#a-nup-with-complete-standalone-updates-stops-at-no-base-game) (packing reproduced with the base dump; 1.8.0 gameplay remains untested) - [Large sparse textures as Metal placement sparse textures](../../../docs/reference/nvn-objects.md#large-sparse-textures-as-metal-placement-sparse-textures) - [Physical pages mapped at several places of a placement sparse texture](../../../docs/reference/nvn-objects.md#physical-pages-mapped-at-several-places-of-a-placement-sparse-texture) diff --git a/dm/gfx/nvn/nvn_mtl.m b/dm/gfx/nvn/nvn_mtl.m index 6b0ea824c7..84579fa47c 100644 --- a/dm/gfx/nvn/nvn_mtl.m +++ b/dm/gfx/nvn/nvn_mtl.m @@ -12095,7 +12095,7 @@ static uint32_t mdi_count(uint64_t cnt) { #define MDX_SLOTS 16384 // a power of two; a scan fills at most a quarter static const p_multi_draw *g_mdx_range; // only while replaying a compact range through the GPU mask typedef struct { uint64_t ind, cnt, out, tag; } mdx_ent; // out: GPU address of the copy, 0 when the record or count is in no Metal buffer (those draws wait, mdi_live) -static mdx_ent g_mdx[MDX_SLOTS]; static int g_mdx_n; static uint64_t g_mdx_tag = 1, g_mdx_ep = ~0ull, g_mdx_sub, g_mdx_runs, g_mdx_splits, g_mdx_draws, g_mdx_fast; static const uint8_t *g_mdx_stop; +static mdx_ent g_mdx[MDX_SLOTS]; static int g_mdx_n; static uint64_t g_mdx_tag = 1, g_mdx_ep = ~0ull, g_mdx_sub, g_mdx_runs, g_mdx_splits, g_mdx_draws, g_mdx_fast, g_mdx_pre; static const uint8_t *g_mdx_stop; // The kernel mdx_mask: dm/gfx/msl/mdx.metal static int mdx_wanted(const p_draw *dw, const p_draw_idx *di) { return dw ? dw->mdi_cnt && !ixc_wanted(dw, NULL) : di->mdi_cnt && !ixc_wanted(NULL, di); } static mdx_ent *mdx_slot(uint64_t ind, uint64_t cnt) { // its entry in this tag, or the free slot for it @@ -12119,8 +12119,8 @@ static void mdx_run(const p_draw *dw, const p_draw_idx *di) { uint64_t lo = ~0ull, hi = 0; // the hull of the records and counts taken, for the uniform update check #define MDX_TAKE(IND, CNT, I, W) do { mdx_ent *e_ = mdx_slot((IND), (CNT)); if (e_->tag != g_mdx_tag && nq < QMAX) { *e_ = (mdx_ent){(IND), (CNT), 0, g_mdx_tag}; g_mdx_n++; \ q[nq].e = e_; q[nq].i = (uint32_t)(I); q[nq++].words = (W); lo = MIN(lo, MIN((IND), (CNT))); hi = MAX(hi, MAX((IND) + 4 * (W), (CNT) + 4)); } } while (0) - if (dw && dw->prim == DM_PRIM_MESH_TASKS) MDX_TAKE(dw->ind, dw->ind, 0, 0); else if (dw) MDX_TAKE(dw->ind, dw->mdi_cnt, dw->mdi_i, 4); else MDX_TAKE(di->ind, di->mdi_cnt, di->mdi_i, 5); // words 0: an indirect mesh draw, keyed by its record alone - if (g_mdx_range) { + if (dw && dw->prim == DM_PRIM_MESH_TASKS) MDX_TAKE(dw->ind, dw->ind, 0, 0); else if (dw) MDX_TAKE(dw->ind, dw->mdi_cnt, dw->mdi_i, 4); else if (di) MDX_TAKE(di->ind, di->mdi_cnt, di->mdi_i, 5); // words 0: an indirect mesh draw, keyed by its record alone; neither: mdx_prescan, only the draws after this one + if (g_mdx_range && (dw || di)) { const p_multi_draw *m = g_mdx_range; for (int i = (dw ? dw->mdi_i : di->mdi_i) + 1; i < m->max && nq < QMAX; i++) { uint64_t ind = m->ind + (uint64_t)i * (uint64_t)m->stride; @@ -12158,8 +12158,17 @@ static void mdx_run(const p_draw *dw, const p_draw_idx *di) { args_dirty_all(); g_mdx_draws += (uint64_t)nq; if (!g_mdx_runs++) MLOG("indirect-count multi-draw masking: first time, %d draws (their records are copied on the GPU, zeros past the count, before the render pass)", nq); - else if (g_mdx_runs % 3600 == 0) MLOG("indirect-count multi-draw masking: %llu times, %llu draws (%llu encoded alone, mdx_next), render pass split %llu times; waits for the GPU-written count %llu", - (unsigned long long)g_mdx_runs, (unsigned long long)g_mdx_draws, (unsigned long long)g_mdx_fast, (unsigned long long)g_mdx_splits, (unsigned long long)g_mdi_waits); + else if (g_mdx_runs % 3600 == 0) MLOG("indirect-count multi-draw masking: %llu times, %llu draws (%llu encoded alone, mdx_next), render pass split %llu times (scans at a pass's opening %llu); waits for the GPU-written count %llu", + (unsigned long long)g_mdx_runs, (unsigned long long)g_mdx_draws, (unsigned long long)g_mdx_fast, (unsigned long long)g_mdx_splits, (unsigned long long)g_mdx_pre, (unsigned long long)g_mdi_waits); +} +// A render pass about to open for a draw that is not such a multi-draw: the multi-draws after it (up to the next write, as mdx_run scans) are copied now, before the pass, so that none of +// them opens the copy inside the pass and splits it. Mario Kart World's scene passes (2048x1152, colour and depth loaded again: 20 MB) were split this way at every frame. Once per epoch +// and submission: a scan taken up to its stop leaves nothing for a second one +static void mdx_prescan(void) { + static uint64_t ep = ~0ull, sub; + if (ep == g_mdx_epoch && sub == g_sub_serial) return; + ep = g_mdx_epoch; sub = g_sub_serial; g_mdx_pre++; + mdx_run(NULL, NULL); } // The draws of one multi-draw are recorded back to back with the same state, and only the first few are live: each one replayed right after the one before it (no op between them, // the same open pass) is encoded alone, without the rest of draw's work, which cost a whole draw for each masked one (about 1.5 µs on an M4 Pro, 1,100 a frame in Kamura Village) @@ -12500,6 +12509,7 @@ static void draw_(const p_draw *dw, const p_draw_idx *di) { if (mdx_on) { uint64_t ind = dw ? dw->ind : di->ind, cnt = dw ? dw->mdi_cnt : di->mdi_cnt; int i = dw ? dw->mdi_i : di->mdi_i; if (!mdx_find(ind, cnt, &mdx)) { mdx_run(dw, di); if (!mdx_find(ind, cnt, &mdx)) mdx = 0; } if (!mdx && !mdi_live(cnt, i)) return; } + else if (!g_enc && !g_defer_replaying && g_exec_next) mdx_prescan(); // the pass this draw opens: its multi-draws copied first (see mdx_prescan) if (di && wide_wanted(di) && !wide_find(di)) wide_run(di); // 8-bit indices written earlier in this submission: widened on the GPU before the render pass (see wide_run) if (!g_defer_replaying && !g_geo_program) { if (g_defer.active && g_defer.in_p && !g_defer.at_q) { if (defer_take(dw, di)) return; } diff --git a/docs/agents/symptoms.md b/docs/agents/symptoms.md index cb1115f44f..8e7b124ef5 100644 --- a/docs/agents/symptoms.md +++ b/docs/agents/symptoms.md @@ -706,6 +706,7 @@ One line per symptom in the [subsystem references](../reference/): what you see - a red line: the 8-bit index buffer had GPU-side writes in the submission (indices read stale). → [8-bit indices widened on the GPU after a write in the same submission](../reference/rendering.md#8-bit-indices-widened-on-the-gpu-after-a-write-in-the-same-submission) - indirect draws whose record the GPU writes earlier in the frame cannot be read on the CPU at replay; GPU-driven effects drawn as indirect quads, which Metal has no primitive for. → [Indirect draws and dispatches read by the GPU](../reference/rendering.md#indirect-draws-and-dispatches-read-by-the-gpu) - an empty G-buffer pass (the title screen showed only the UI); later, the submitting thread waiting for the GPU before each such draw (36–54 fps in Kamura Village on an iPhone). MIO also reported missing level geometry with maxima of 4,594 and 8,192 draws. → [Indirect-count multi-draws](../reference/rendering.md#indirect-count-multi-draws) +- in a pass dump (`DM_NVN_PASSDUMP`), `M … indirect-count multi-draw masking: render pass split here` inside scene passes, and the periodic `indirect-count multi-draw masking:` line counts `render pass split` times in the tens of thousands. Each split loads the pass's colour and depth again (Mario Kart World: 2048x1152, 20 MB). → [Indirect-count multi-draws split the pass an earlier draw opened](../reference/rendering.md#indirect-count-multi-draws-split-the-pass-an-earlier-draw-opened) - a hitch of a second or more as a level opens: a few frames of 330–540 ms whose submission takes 2–3 times its CPU time, and the wait ledger (`DM_NVN_WAITS=1`) shows thousands of indirect-count multi-draw waits for those frames only. → [CPU reads after a drain wait only for later writes](../reference/rendering.md#cpu-reads-after-a-drain-wait-only-for-later-writes) - a black screen while the frame loop runs at full rate, with `[RED] NVN unimplemented function called: nvnCommandBufferDrawTexture`; once drawn, the picture alternates frame by frame between the right image and another texture's (a solid colour, a still image). → [`nvnCommandBufferDrawTexture`](../reference/rendering.md#nvncommandbufferdrawtexture) - the HUD and sky render but the battle's characters and terrain are absent; the replay logs `draw skipped: rasterizer discard` between model draws. → [Rasterizer discard preserves vertex storage writes](../reference/rendering.md#rasterizer-discard-preserves-vertex-storage-writes) diff --git a/docs/reference/rendering.md b/docs/reference/rendering.md index 428e8e9195..efc8db189f 100644 --- a/docs/reference/rendering.md +++ b/docs/reference/rendering.md @@ -944,6 +944,14 @@ The replay on Metal (`dm/gfx/nvn/nvn_mtl.m`) and the Vulkan and OpenGL layers. E - **Found on:** [Monster Hunter Rise](../../dm/games/0100B04011742000/README.md) and [MIO: Memories in Orbit, issue #941](https://github.com/seedds/nxnative/issues/941). The visibility-buffer shading issue in MIO is still open. - **Note:** the replay used to commit its work and wait for the GPU before the first such draw after each GPU write, then read the count on the CPU (`mdi_wait`; still used when a CPU count is needed, including quads, fans and 8-bit indices). In Kamura Village that wait held the submitting thread 14 of its 20 ms a frame on an iPhone (36–54 fps). On the Mac the same scene's submit took 20 ms of wall time for 6.1 ms of work, and now 5.1 ms, all of it work. +### Indirect-count multi-draws split the pass an earlier draw opened + +- **Symptom:** in a pass dump (`DM_NVN_PASSDUMP`), `M … indirect-count multi-draw masking: render pass split here` inside scene passes, and the periodic `indirect-count multi-draw masking:` line counts `render pass split` times in the tens of thousands. Each split loads the pass's colour and depth again (Mario Kart World: 2048x1152, 20 MB). +- **Cause:** `mdx_run` copies a multi-draw's records at its first draw, and the compute pass that copies them cannot run inside a render pass. When an ordinary draw had already opened the pass, the copy ended it. +- **Fix:** general (`nvn_mtl.m` `mdx_prescan`): a draw that is not such a multi-draw and opens a pass first runs the same forward scan, once per `g_mdx_epoch` and submission, so the multi-draws after it are copied before the pass. Ranges replayed through `draw_multi` are not scanned ahead and behave as before. +- **Result on Mario Kart World,** the Grand Prix route: at a race frame, masking splits 2 → 0 and mergeable splits 5 → 2 (`tbdr_pass_merge.py`: 118 → 47 MB a frame); on an iPad Pro (M5) over 10 minutes, 72,000 splits → 0; GPU time on an M4 Pro p50 13.0 ms against 13.65–14.1 ms in five runs without it; on the iPad unchanged within noise (race p50 about 14.3 ms). +- **Found on:** [Mario Kart World](../../dm/games/0400C3F00006E000/README.md). + ### CPU reads after a drain wait only for later writes - **Symptom:** a hitch of a second or more as a level opens: a few frames of 330–540 ms whose submission takes 2–3 times its CPU time, and the wait ledger (`DM_NVN_WAITS=1`) shows thousands of indirect-count multi-draw waits for those frames only.