diff --git a/docs/architecture.md b/docs/architecture.md index f0e09b1f..5ce3e21c 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -153,8 +153,10 @@ keeps per-core data without locks and relies on that, so the host models it (`ru a village frame's 45 mesh draws are 45 passes cut, 90 to 135 dispatches and about 270 barriers more than with mesh shaders. The buffers (`mesh_fallback_mb`, 128 MiB) are reused by every draw, so they hold the largest draw, not a frame; a draw that emits more loses what does not fit, and the log says how much it needed. - - A program's modules and compute pipelines are made on a thread of their own at its first draw, which is skipped - until they are ready; they are not recorded for the next start, where the pipeline cache makes them quick. + - A program's modules and compute pipelines are made by worker threads at its first draw, which is skipped until + they are ready (`mesh_fallback_workers`, auto: one for each 4 logical processors, 1 to 4, so a place's programs, + which all arrive in its first frames, are not made one after the other); they are not recorded for the next start, + where the pipeline cache makes them quick. Not carried: the far shadow cascades' coarser shading rate, a draw's first workgroup other than 0, and more workgroups in one draw than one compute dispatch may have. - Pipelines: `VK_EXT_graphics_pipeline_library` where the driver has it: stage libraries compiled ahead, a fast link diff --git a/nx2navi.ini.example b/nx2navi.ini.example index f2538825..3dfe8258 100644 --- a/nx2navi.ini.example +++ b/nx2navi.ini.example @@ -889,6 +889,9 @@ # Video memory in MB for what those compute passes hand to the draws. A draw that needs more loses the geometry that # does not fit, and the log says so. # mesh_fallback_mb = 128 +# Threads that prepare those compute passes for the mesh programs a place uses, the first time each is drawn (until +# then its draws are left out). auto (default): one for each 4 logical processors, 1 to 4. +# mesh_fallback_workers = auto # Windows, 1 (experimental): the game's heap is committed a 64 KiB run at a time where it is first used, instead of # all of it when the game sizes it. Windows counts committed memory against RAM plus the page file whether it is used # or not, and in Castle Town after Hyrule Field about 3.4 of the heap's 6.8 GiB had never been used. 0: committed up diff --git a/runtime/cfg.c b/runtime/cfg.c index 089e2425..6ad029a8 100644 --- a/runtime/cfg.c +++ b/runtime/cfg.c @@ -36,7 +36,7 @@ static int save_ok = 1; // the last cfg_save_to wrote (o static const char *const known[] = { "CORES", "TM_DISPATCH", "NVN2_CS_TABLE", "SKIP_SHADERS", "MOVIE_DEBAND", "MOVIE_FPS", "MOVIE_SKIP", "MOVIE_OVERLAY", "MOVIES", "NPLN", "NVN2_PROGRAMS", "NVN_TRAP", "OCARINA_REALTIME", "ONESHOT_BUDGET", "ONESHOT_WAIT_MS", "OCARINA_STEP", "PHYSICS_STEPS", "PICKUP_COUNT", "STEP_STEADY", "VI_HZ", "VI_VRR", "TIME_DUMP", "SHADER_DUMP", "SHOT_AFTER", "SHOT_DIR", "SHOT_EVERY", // Switch 2 port: movie skip (romfs.c), NVN2 proc traps (nvn_procs.c) "AA", "AA_LOG", "AB", "AMIIBO_DIR", "ANISO", "APP_POOL", "ASPECT", "ASPECT_PATCH", "ASSUME_FOCUS", "ASTC", "ASTC_BC", "ASTC_TIME", "ASYNC_PREEMPT", "AUDIO", "AUDIO_CHANNELS", "AUDIO_DEVICE", "AUDIO_GAME_PRIO", "AUDIO_OFF", "AUDIO_PREFILL_MS", "AUDIO_STATS", - "BARRIER_GAME", "MESH_AHEAD", "MESH_FALLBACK", "MESH_FALLBACK_MB", "PROBE_MESHFB_DIR", + "BARRIER_GAME", "MESH_AHEAD", "MESH_FALLBACK", "MESH_FALLBACK_MB", "MESH_FALLBACK_WORKERS", "PROBE_MESHFB_DIR", "BACKGROUND_WINDOW", "BIND_A", "BIND_B", "BIND_DOWN", "BIND_L", "BIND_LCLICK", "BIND_LEFT", "BIND_LSTICK_DOWN", "BIND_LSTICK_LEFT", "BIND_LSTICK_RIGHT", "BIND_LSTICK_UP", "BIND_MINUS", "BIND_PLUS", "BIND_R", "BIND_RCLICK", "BIND_RIGHT", "BIND_RSTICK_DOWN", "BIND_RSTICK_LEFT", "BIND_RSTICK_RIGHT", "BIND_RSTICK_UP", @@ -140,7 +140,7 @@ static const struct { const char *key, *section; } sections[] = { {"VK_DYN_VERTEX", "performance"}, {"VK_DYN_A2C", "performance"}, {"VK_SUBGROUP_PIN", "performance"}, {"VK_RECONVERGE", "performance"}, {"VK_NAN_PRESERVE", "performance"}, {"VK_NAN_BITS", "performance"}, {"TRACE", "diagnostics"}, {"FLIGHT", "diagnostics"}, {"PASS_CRUMBS", "diagnostics"}, {"FLIGHT_SPIKE_MS", "diagnostics"}, - {"WB_FENCE_LIMIT", "diagnostics"}, {"WB_MERGE", "diagnostics"}, {"WB_EXACT", "diagnostics"}, {"MEM_CENSUS", "diagnostics"}, {"WB_NO_REUP", "diagnostics"}, {"WB_PROBE", "diagnostics"}, {"WB_SPLIT", "performance"}, {"SHADOW_BULK_WRITE", "performance"}, {"WB_LAZY", "performance"}, {"HEAP_LAZY", "performance"}, {"BARRIER_GAME", "performance"}, {"MESH_AHEAD", "performance"}, {"MESH_FALLBACK", "performance"}, {"MESH_FALLBACK_MB", "performance"}, {"WB_LAZY_TOUCH", "diagnostics"}, {"WB_LAZY_CHUNK", "performance"}, {"FLUSH_UPLOAD", "diagnostics"}, {"GPA_TWIN_MAX_KB", "diagnostics"}, + {"WB_FENCE_LIMIT", "diagnostics"}, {"WB_MERGE", "diagnostics"}, {"WB_EXACT", "diagnostics"}, {"MEM_CENSUS", "diagnostics"}, {"WB_NO_REUP", "diagnostics"}, {"WB_PROBE", "diagnostics"}, {"WB_SPLIT", "performance"}, {"SHADOW_BULK_WRITE", "performance"}, {"WB_LAZY", "performance"}, {"HEAP_LAZY", "performance"}, {"BARRIER_GAME", "performance"}, {"MESH_AHEAD", "performance"}, {"MESH_FALLBACK", "performance"}, {"MESH_FALLBACK_MB", "performance"}, {"MESH_FALLBACK_WORKERS", "performance"}, {"WB_LAZY_TOUCH", "diagnostics"}, {"WB_LAZY_CHUNK", "performance"}, {"FLUSH_UPLOAD", "diagnostics"}, {"GPA_TWIN_MAX_KB", "diagnostics"}, {"WB_MERGE_MAX", "diagnostics"}, {"MV_SURVEY", "diagnostics"}, {"TEXUP_LOG_MS", "diagnostics"}, {"WAIT_WATCH_S", "diagnostics"}, {"VMA_BLOCK_MB", "diagnostics"}, {"ASTC_BC_PROBE", "diagnostics"}, {"DEVICE_LOST_AT", "diagnostics"}, @@ -267,6 +267,7 @@ static const CfgMeta metas[] = { // vk_device.c: task and mesh programs through compute passes (vk_exec_draw.c "mesh fallback"); auto: on a device // without mesh shaders. The megabytes are the buffers the passes write (vk_mesh_fb.c). {"MESH_FALLBACK", "auto", "auto|0|1", RESTART}, {"MESH_FALLBACK_MB", "128", "16..4096", RESTART}, + {"MESH_FALLBACK_WORKERS", "auto", "auto|1|2|3|4", RESTART}, // vk_exec_draw.c mfb_worker_count {"PIPE_AHEAD", "1", "0|1", RESTART}, // vk_exec.c vk_exec_ahead, vk_pipeline.c vk_pipeline_hint {"GPL_FEEDBACK", "1", "0|1", RESTART}, // vk_pipeline.c gpl_feedback {"PIPE_FAST_WORKERS", "1", "0|1", RESTART}, // vk_pipeline.c lib_worker, fast_take diff --git a/runtime/vk_exec_draw.c b/runtime/vk_exec_draw.c index 0d323712..a3c1f01c 100644 --- a/runtime/vk_exec_draw.c +++ b/runtime/vk_exec_draw.c @@ -5036,9 +5036,10 @@ static MfbVs mfb_vs_tab[MFB_TAB]; static struct { PlatMutex lock; PlatCond wake; - PlatThread *th; + int threads, busy; // workers started, workers making a program now MfbCs *head, *tail; uint64_t made, failed, pending_skips; + uint64_t first_ms, drained; // the first program's queueing; the queue has emptied once (logged) } MFB = {.lock = PLAT_MUTEX_INIT}; static uint32_t mfb_binding_base(const GfxShader *s) { // one past the highest binding the module declares @@ -5090,6 +5091,16 @@ static void mfb_make(MfbCs *e) { __atomic_store_n(&e->state, ok ? 1 : -1, __ATOMIC_RELEASE); } +// The programs queued at their first draws are made by NX2NAVI_MESH_FALLBACK_WORKERS threads (auto: one for each 4 +// logical processors, 1 to 4). A place's mesh programs all arrive in its first frames and each one's draws are left +// out until its compute pipelines are built (up to seconds each on some drivers), so with one thread the foliage of a +// first visit filled in one program at a time. The first time the queue empties the log says how long it took. +static int mfb_worker_count(void) { + const char *v = cfg_value("NX2NAVI_MESH_FALLBACK_WORKERS"); + int n = strcmp(v, "auto") ? atoi(v) : (int)plat_cpu_usable() / 4; + return n < 1 ? 1 : n > 4 ? 4 : n; +} + static void mfb_worker(void *arg) { (void)arg; plat_thread_priority(PLAT_PRIO_BELOW_NORMAL); // as the pipeline workers' optimised builds: the game's threads go first @@ -5098,8 +5109,15 @@ static void mfb_worker(void *arg) { while (!MFB.head) plat_cond_wait(&MFB.wake, &MFB.lock); MfbCs *e = MFB.head; if (!(MFB.head = e->next)) MFB.tail = NULL; + MFB.busy++; plat_mutex_unlock(&MFB.lock); mfb_make(e); + plat_mutex_lock(&MFB.lock); + if (!--MFB.busy && !MFB.head && !MFB.drained++) + rt_log("vk: mesh fallback: %llu programs made (%llu refused) on %d threads, the queue empty %llu ms after the " + "first program was queued (NX2NAVI_MESH_FALLBACK_WORKERS)", (unsigned long long)MFB.made, + (unsigned long long)MFB.failed, MFB.threads, (unsigned long long)(plat_now_ms() - MFB.first_ms)); + plat_mutex_unlock(&MFB.lock); } } @@ -5132,7 +5150,15 @@ static MfbCs *mfb_cs_get(VkExec *ex, const GfxShader *ms, const GfxShader *ts) { return e; } plat_mutex_lock(&MFB.lock); - if (!MFB.th) plat_cond_init(&MFB.wake), MFB.th = plat_thread_new(mfb_worker, NULL, "mesh fallback"); + if (!MFB.threads) { + plat_cond_init(&MFB.wake); + MFB.first_ms = plat_now_ms(); + for (int w = mfb_worker_count(); w > 0; w--) { + PlatThread *t = plat_thread_new(mfb_worker, NULL, "mesh fallback"); + if (t) plat_thread_detach(t), MFB.threads++; + } + if (!MFB.threads) rt_fatal("vk: mesh fallback: cannot start a worker thread"); + } if (MFB.tail) MFB.tail->next = e; else MFB.head = e; MFB.tail = e;