diff --git a/nx2navi.ini.example b/nx2navi.ini.example index 510b0f99..afcf2616 100644 --- a/nx2navi.ini.example +++ b/nx2navi.ini.example @@ -64,14 +64,15 @@ # Draws whose pipeline is still building: default waits on a reserved worker; draw is the previous scheduling. # pipe_wait = default # Which draws are dropped instead of waiting for their pipeline: strict (only draws into per-frame passes), any (also -# draws into passes whose target the previous frame rendered) or fullscreen (those and full-screen draws). +# draws into passes whose target a recent frame rendered) or fullscreen (those and full-screen draws). # pipe_skip = fullscreen -# How long (ms) a draw into a target the previous frame did not render waits for a pipeline built for the first time, +# How long (ms) a draw into a target no recent frame rendered waits for a pipeline built for the first time, # then is skipped. The default waits, as such a target (a lookup table, a new area's target) can stay empty if # skipped; a lower value caps the stall. # oneshot_wait_ms = 30000 -# Build shader stage libraries in the background ahead of time, and the threads for it (1 to 16; default: a quarter of -# the CPU threads). +# 1: those draws share a wait budget per presented frame instead (100 ms for large passes, 300 ms for small ones); a +# draw left over is drawn the next frame its target is drawn. Shortens the long first frame of a new area. +# oneshot_budget = 0 # Build shader stage libraries in the background ahead of time, and the threads for it (1 to 16; default 2). More # threads mostly wait on the graphics driver's lock: 16 on NVIDIA spent about 8 cores spinning in the driver. # lib_prebuild = 1 diff --git a/runtime/cfg.c b/runtime/cfg.c index 77ca147d..76f5d3c7 100644 --- a/runtime/cfg.c +++ b/runtime/cfg.c @@ -31,7 +31,7 @@ static int save_ok = 1; // the last cfg_save_to wrote (o // Every name the runtime reads (and the patch sets in generated/), without the prefix. A file key outside this list is // logged as unknown. A new variable goes here too, or its ini key logs a harmless "unknown key" line. 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_WAIT_MS", "OCARINA_STEP", "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) + "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", "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) "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", "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", @@ -106,7 +106,7 @@ static const struct { const char *key, *section; } sections[] = { {"RECORDER_CORE", "performance"}, {"RECORDER_PRIO", "performance"}, {"BIND_REUSE", "performance"}, {"DYN_DELTA", "performance"}, {"STG_ARENA", "performance"}, {"SUBMIT_BATCH", "performance"}, {"ASTC_GPU_DESWIZZLE", "performance"}, {"ASTC_SCRATCH_KEEP", "performance"}, {"ASTC_ARENA", "performance"}, {"POOL_CREATE_BUDGET", "performance"}, - {"TIMER_1MS", "performance"}, {"PIPE_WAIT", "performance"}, {"PIPE_SKIP", "performance"}, + {"TIMER_1MS", "performance"}, {"PIPE_WAIT", "performance"}, {"PIPE_SKIP", "performance"}, {"ONESHOT_BUDGET", "performance"}, {"LIB_PREBUILD", "performance"}, {"LIB_PREBUILD_THREADS", "performance"}, {"PIPE_PREBUILD", "performance"}, {"PREWARM_XLATE", "performance"}, {"GPL", "performance"}, {"ASTC", "performance"}, {"MEMORY", "performance"}, {"TEX_ALIAS", "performance"}, {"TEX_VRAM_PRESSURE", "performance"}, {"ASTC_BC", "performance"}, {"LOD_GUARD", "performance"}, {"FIX_C0", "performance"}, {"FIX_ZERO", "performance"}, @@ -202,7 +202,7 @@ static const CfgMeta metas[] = { {"LIB_PREBUILD", "1", "0|1", RESTART}, {"LIB_PREBUILD_THREADS", "", "1..64", RESTART}, {"PIPE_PREBUILD", "1", "0|1", RESTART}, {"PREWARM_XLATE", "", "1..64", RESTART}, {"GPL", "1", "0|1", RESTART}, {"LIB_PREBUILD_BIG_BG", "1", "0|1", RESTART}, {"PIPE_FEEDBACK", "1", "0|1|2", RESTART}, - {"PIPELINE_CACHE_MAX_MB", "512", "0..65536", RESTART}, {"PIPE_BINARIES", "0", "0|1", RESTART}, {"ONESHOT_WAIT_MS", "30000", "0..60000", RESTART}, {"PIPE_BINARIES_MAX_MB", "1024", "0..65536", RESTART}, {"BIG_BUILD_FREE_MB", "8192", "0..262144", RESTART}, + {"PIPELINE_CACHE_MAX_MB", "512", "0..65536", RESTART}, {"PIPE_BINARIES", "0", "0|1", RESTART}, {"ONESHOT_WAIT_MS", "30000", "0..60000", RESTART}, {"ONESHOT_BUDGET", "0", "0|1", RESTART}, {"PIPE_BINARIES_MAX_MB", "1024", "0..65536", RESTART}, {"BIG_BUILD_FREE_MB", "8192", "0..262144", RESTART}, {"BIG_BUILD_MAX_MB", "4096", "0..262144", RESTART}, // textures and memory {"ASTC", "auto", "auto|gpu|native|cpu", RESTART}, {"MEMORY", "auto", "auto|import|mirror|verify", RESTART}, diff --git a/runtime/vk_exec.c b/runtime/vk_exec.c index 3bbe4abb..67ed665b 100644 --- a/runtime/vk_exec.c +++ b/runtime/vk_exec.c @@ -476,40 +476,55 @@ static void free_views(void *arg) { // completion thread, once the submission // ---- target history // The presented frame (gfx_counters.frames + 1; 0: never) each target subresource was last rendered into, and the one -// before. prepare_draw lets a draw into a target the previous frame did not render (a one-shot draw: a volume or LUT -// generated once) wait for its shaders and pipeline instead of skipping it. Per subresource, not per image: a volume -// may be generated a few slices per frame. Direct-mapped: a slot another subresource took reads as not rendered, so a -// collision makes a draw wait rather than skip. +// before. prepare_draw lets a draw into a target no recent frame rendered (a one-shot draw: a volume or LUT generated +// once) wait for its shaders and pipeline instead of skipping it. Per subresource, not per image: a volume may be +// generated a few slices per frame. +// Recent means within TFR_RECENT frames, not the frame before alone: the game's output images are a ring of three, its +// probe and shadow layers are refreshed in turn every second to fourth frame, and the mips of its small effect buffers +// alternate between images. Sets of TFR_WAYS, the oldest entry replaced: hundreds of subresources are rendered a +// frame, and in a direct-mapped table of 1024 a fifth of the scene passes and most of the probe layers read as never +// rendered because another subresource had taken their slot. Each misread made a draw whose pipeline was still +// building wait for it (ONESHOT_WAIT_MS), so a new area was entered one pipeline at a time. +#define TFR_SETS 1024 +#define TFR_WAYS 4 +#define TFR_RECENT 4 typedef struct { VkImage image; uint32_t level, layer; uint64_t last, prev; } TargetFrames; -static TargetFrames tfr[1024]; +static TargetFrames tfr[TFR_SETS * TFR_WAYS]; -static TargetFrames *tfr_slot(const VkExecTarget *t) { +// The target's entry, or with make the entry it takes (the set's oldest, an empty one first); NULL without make +// when it has none. +static TargetFrames *tfr_find(const VkExecTarget *t, int make) { uint64_t h = ((uint64_t)(uintptr_t)t->image ^ (uint64_t)t->level << 56 ^ (uint64_t)t->base_layer << 40) * 0x9E3779B97F4A7C15ull; - return &tfr[h >> 54]; -} - -static int tfr_match(const TargetFrames *e, const VkExecTarget *t) { - return e->image == t->image && e->level == t->level && e->layer == t->base_layer; + TargetFrames *set = &tfr[(h >> 54) * TFR_WAYS], *oldest = set; + for (int i = 0; i < TFR_WAYS; i++) { + if (set[i].image == t->image && set[i].level == t->level && set[i].layer == t->base_layer) return &set[i]; + if (set[i].last < oldest->last) oldest = &set[i]; + } + if (!make) return NULL; + *oldest = (TargetFrames){t->image, t->level, t->base_layer, 0, 0}; + return oldest; } static void tfr_note(const VkExecTarget *t, uint64_t stamp) { - TargetFrames *e = tfr_slot(t); - if (!tfr_match(e, t)) *e = (TargetFrames){t->image, t->level, t->base_layer, 0, 0}; + TargetFrames *e = tfr_find(t, 1); if (e->last != stamp) e->prev = e->last, e->last = stamp; } -static int tfr_recent(const VkExecTarget *t, uint64_t stamp) { - const TargetFrames *e = tfr_slot(t); - return tfr_match(e, t) && (e->last == stamp ? e->prev : e->last) == stamp - 1; +// The frame before the current one that last rendered the target, 0 for none. +static uint64_t tfr_before(const VkExecTarget *t, uint64_t stamp) { + const TargetFrames *e = tfr_find(t, 0); + return !e ? 0 : e->last == stamp ? e->prev : e->last; } -// No frame before the current one rendered the target (a slot another subresource took reads the same). -static int tfr_first(const VkExecTarget *t, uint64_t stamp) { - const TargetFrames *e = tfr_slot(t); - return !tfr_match(e, t) || !(e->last == stamp ? e->prev : e->last); +static int tfr_recent(const VkExecTarget *t, uint64_t stamp) { + uint64_t before = tfr_before(t, stamp); + return before && stamp - before <= TFR_RECENT; } +// No frame before the current one rendered the target. +static int tfr_first(const VkExecTarget *t, uint64_t stamp) { return !tfr_before(t, stamp); } + // With any, a recent depth target alone does not make the pass per-frame when a colour target is rendered for the // first time: the game's item icon capture (an inventory icon) draws into a new colour target with the // per-frame depth buffer, once. Its draws skipped there stay missing from the icon until the item is captured again. @@ -537,11 +552,10 @@ void vk_exec_targets_describe(VkExec *ex, char *buf, size_t cap) { for (int i = 0; i < 9 && n < cap; i++) { const VkExecTarget *t = i < 8 ? &ex->color[i] : &ex->depth; if (!t->image) continue; - const TargetFrames *e = tfr_slot(t); - int m = tfr_match(e, t); + const TargetFrames *e = tfr_find(t, 0); int w = snprintf(buf + n, cap - n, "%s%c%d fmt %d %ux%u L%u seen %llu/%llu", n ? "; " : "", i < 8 ? 'c' : 'd', i < 8 ? i : 0, (int)t->format, t->width, t->height, t->base_layer, - (unsigned long long)(m ? e->last : 0), (unsigned long long)(m ? e->prev : 0)); + (unsigned long long)(e ? e->last : 0), (unsigned long long)(e ? e->prev : 0)); if (w < 0) break; n += (size_t)w; } diff --git a/runtime/vk_exec_draw.c b/runtime/vk_exec_draw.c index a2496962..b975d1ee 100644 --- a/runtime/vk_exec_draw.c +++ b/runtime/vk_exec_draw.c @@ -58,6 +58,32 @@ static uint32_t oneshot_wait_ms(void) { return (uint32_t)ms; } #define ONESHOT_WAIT_MS oneshot_wait_ms() +// NX2NAVI_ONESHOT_BUDGET=1: one-shot draws wait for their pipelines from a budget per presented frame instead of up to +// ONESHOT_WAIT_MS each (oneshot_budget): one budget for passes of ONESHOT_LARGE_PX pixels or more (the scene, its +// half-resolution buffers, shadow maps) and one for smaller passes (LUTs, icon captures, the smallest bloom levels). +// After a scene load every pass is new, so every draw of the first frame counts as one-shot, and the frame waited for +// one cold pipeline after another, 300-1200 ms each: 5-14 s on an RTX 4080 SUPER while the workers sat idle. A draw +// skipped for the budget comes back next frame, when its pass is recent and its pipeline is built on a worker like any +// other; a target drawn only once (a LUT baked at load) stays as it was until the game draws it again. Off by default +// until play shows no target left empty that way. +#define ONESHOT_LARGE_PX 400000u +#define ONESHOT_BUDGET_LARGE_NS 100000000ull +#define ONESHOT_BUDGET_SMALL_NS 300000000ull +static int oneshot_budget_on(void) { + static int on = -1; + if (on < 0) { + on = rt_knob("ONESHOT_BUDGET", "0") == 1; + if (on) rt_log("vk_exec: one-shot draws wait for their pipelines from a per-frame budget (NX2NAVI_ONESHOT_BUDGET=1)"); + } + return on; +} +// The wait budget left to this frame's one-shot draws (prepare_draw_ex), by pass size, reset at each presented frame. +static uint64_t *oneshot_budget(int large) { + static uint64_t frame = ~0ull, left[2]; + uint64_t f = __atomic_load_n(&gfx_counters.frames, __ATOMIC_RELAXED); + if (f != frame) frame = f, left[0] = ONESHOT_BUDGET_SMALL_NS, left[1] = ONESHOT_BUDGET_LARGE_NS; + return &left[large != 0]; +} enum { W_PROGRAM, W_SHADER, W_PIPELINE, W_NO_VS, W_INDEX, W_UB, W_BIND, W_SSBO_ALIGN, W_SSBO_SLOT, W_RESTART, W_INDIRECT, W_MESH, W_COUNT }; @@ -3219,19 +3245,27 @@ static int prepare_draw_ex(VkExec *ex, int prim, int32_t count, int restart, int int pending; // A draw of at most 6 vertices over half the render area or more (a full-screen triangle or quad, a tile) waits for // its pipeline: skipping it can leave a black tile. The waits share 50 ms per presented frame - // (NX2NAVI_PIPE_SKIP=fullscreen skips them instead). A one-shot draw waits without that budget. Everything else is - // skipped while its pipeline is built. + // (NX2NAVI_PIPE_SKIP=fullscreen skips them instead). A one-shot draw waits without that budget, up to + // ONESHOT_WAIT_MS, or from its own per-frame budget with NX2NAVI_ONESHOT_BUDGET=1 (oneshot_budget). Everything else + // is skipped while its pipeline is built. static uint64_t wait_frame; static uint64_t wait_left_ns; uint32_t wait_ms = 0; uint64_t t0 = 0; - if (oneshot > 0) { + uint64_t *budget = NULL; // the per-frame budget a timed wait is charged to + int large = (uint64_t)ex->pass_w * ex->pass_h >= ONESHOT_LARGE_PX; + if (oneshot > 0 && oneshot_budget_on()) { + budget = oneshot_budget(large); + wait_ms = (uint32_t)(*budget / 1000000); + if (wait_ms) t0 = kern_now_ns(); + } else if (oneshot > 0) { wait_ms = ONESHOT_WAIT_MS; } else if (skip < 2 && count <= 6 && (uint64_t)sc.extent.width * sc.extent.height * 2 >= (uint64_t)ex->pass_w * ex->pass_h) { uint64_t f = __atomic_load_n(&gfx_counters.frames, __ATOMIC_RELAXED); if (f != wait_frame) wait_frame = f, wait_left_ns = 50000000; wait_ms = (uint32_t)(wait_left_ns / 1000000); if (wait_ms) t0 = kern_now_ns(); + budget = &wait_left_ns; } // The previous draw's pipeline while its state repeats (PM, NX2NAVI_PIPE_MEMO=0 turns it off): the cache key is made // from the state alone, and a pipeline once found only changes from its fast link to its optimised build, read @@ -3245,16 +3279,24 @@ static int prepare_draw_ex(VkExec *ex, int prim, int32_t count, int restart, int } else { w0 = t0 ? t0 : wait_ms ? kern_now_ns() : 0; // the clock is read only for draws that may wait p = vk_pipeline_graphics_async(&s, &info, &pending, wait_ms); - if (t0) { + if (t0 && budget) { uint64_t spent = kern_now_ns() - t0; - wait_left_ns = spent < wait_left_ns ? wait_left_ns - spent : 0; + *budget = spent < *budget ? *budget - spent : 0; } why = oneshot > 0 ? "one-shot, shader" : wait_ms ? "full-screen" : NULL; if (pending && oneshot < 0 && !vk_exec_targets_recent(ex, skip >= 1)) { perf_count(PERF_CNT_ONESHOT_WAIT); why = "one-shot"; if (!w0) w0 = kern_now_ns(); - p = vk_pipeline_graphics_async(&s, &info, &pending, ONESHOT_WAIT_MS); + if (oneshot_budget_on()) { + budget = oneshot_budget(large); + uint64_t b0 = kern_now_ns(); + p = vk_pipeline_graphics_async(&s, &info, &pending, (uint32_t)(*budget / 1000000)); + uint64_t spent = kern_now_ns() - b0; + *budget = spent < *budget ? *budget - spent : 0; + } else { + p = vk_pipeline_graphics_async(&s, &info, &pending, ONESHOT_WAIT_MS); + } } PM.valid = !pending && p && info; if (PM.valid) PM.s = s, PM.info = info;