From 005d30a29cffda3f2069dd5659c7b099dc783b46 Mon Sep 17 00:00:00 2001 From: chrg6 Date: Tue, 10 Nov 2026 11:23:45 -0500 Subject: [PATCH 1/4] Give the Kokiri Forest measurement in the 0.7.1 notes as it was recorded: worst frame 434 to 89 ms, frames over 50 ms 5 to 3. Co-Authored-By: Claude Opus 5.5 --- docs/releases/v0.7.1.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/releases/v0.7.1.md b/docs/releases/v0.7.1.md index 54261403..57b4e48f 100644 --- a/docs/releases/v0.7.1.md +++ b/docs/releases/v0.7.1.md @@ -31,7 +31,7 @@ cards). left the draw out while one thread linked it. Now the rest of the submission is read ahead and the pipelines it will need are queued at once, several threads link them, and pipelines whose shader reads a target of its own pass are linked the fast way like the others. Walking into Kokiri Forest for the first time, the worst frame went from - 434 ms to 44 ms in one measurement each, with no frame over 50 ms. + 434 ms to 89 ms and the frames over 50 ms from 5 to 3, in one measurement each. ## New From bfcefe6fa24bd236fc11c617a4beef7b8cde44f5 Mon Sep 17 00:00:00 2001 From: chrg6 Date: Tue, 10 Nov 2026 12:02:12 -0500 Subject: [PATCH 2/4] A frame no longer stops for the pipelines of an area's first frame: passes the game renders frame after frame stay per-frame through a loading screen, and the waits that remain share 50 ms a frame. A draw whose pipeline is still building is skipped when its pass is rendered every frame and waits otherwise, so that a lookup table or a volume generated once is not left empty. "Every frame" meant "in the last four frames", so after a loading screen or a full-screen menu every pass of the first frame read as new: each draw with a new pipeline waited on the replay thread, up to 30 s each, and a mesh draw, whose pipeline has no fast link, for its whole compile. A player's 0.7.1 log shows it at three area changes: frames of 954, 1015 and 684 ms with 853, 960 and 663 ms of pipeline wait, six mesh draws waiting 52 to 660 ms in passes whose targets had been rendered in thousands of frames. The target history (now runtime/vk_target_frames.h) also counts the frames each target was rendered in, for as long as it is the same guest texture. From eight frames on its pass is per-frame however long ago the last one was, and its draws are skipped and built by the workers like any other (pipe_skip_steady, on by default). A target rendered for the first time still waits, now from one pool of 50 ms a presented frame that the full-screen draws share (pipe_frame_wait_ms; oneshot_budget = 0 gives the old wait back), a mesh draw 8 ms at most, and the wait for a one-shot draw's shader comes from the same pool (gfx_shader_await takes a limit). A full-screen draw with no picture left to hold waits 40 ms more instead of 500. One frame so waits under 100 ms for pipelines in all. Every 600 frames with a miss the log says how many draws found a shader or pipeline missing, how long they held the replay thread in all and in the worst frame, by the kind of wait, and how many were skipped at its end. tests/target_frames_check.py replays the area change of that log against the header. Not yet run in the game. Co-Authored-By: Claude Fable 5.2 --- docs/architecture.md | 7 +- docs/building.md | 6 ++ nx2navi.ini.example | 21 ++-- runtime/cfg.c | 7 +- runtime/gfx.h | 7 +- runtime/gfx_shader.c | 23 +++-- runtime/vk_exec.c | 102 +++++++----------- runtime/vk_exec.h | 5 +- runtime/vk_exec_draw.c | 193 +++++++++++++++++++++++++---------- runtime/vk_target_frames.h | 106 +++++++++++++++++++ tests/target_frames_check.c | 119 +++++++++++++++++++++ tests/target_frames_check.py | 40 ++++++++ 12 files changed, 492 insertions(+), 144 deletions(-) create mode 100644 runtime/vk_target_frames.h create mode 100644 tests/target_frames_check.c create mode 100644 tests/target_frames_check.py diff --git a/docs/architecture.md b/docs/architecture.md index 69b58511..7c3830dc 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -131,7 +131,12 @@ keeps per-core data without locks and relies on that, so the host models it (`ru ring of Vulkan `{x, y, z}` records filled on the GPU. - Pipelines: `VK_EXT_graphics_pipeline_library` where the driver has it: stage libraries compiled ahead, a fast link at a new state, an optimised pipeline replacing it from a background thread. A draw whose pipeline is not ready is - skipped when one of its pass's targets was rendered in the previous frame, and waits otherwise. Three things keep + skipped when its pass is one the game renders frame after frame: one of its targets was rendered in the last few + frames, or in eight frames or more at any time, which keeps the first frame after a loading screen from reading as + all new (`runtime/vk_target_frames.h`). A draw into a target rendered for the first time, and a full-screen draw, + wait instead, from one pool of 50 ms a presented frame (`pipe_frame_wait_ms`; `runtime/vk_exec_draw.c` + `prepare_draw_ex`, `frame_wait`); what is not ready when the pool is spent is skipped and built on a worker, so no + frame waits for pipelines much longer than that. Three things keep that short on a first visit (`runtime/vk_pipeline.c`): a pipeline whose fragment shader reads a target of its own pass is fast-linked like any other, the feedback loop flags being part of the fragment output library alone (`gpl_feedback`); the library workers also link the pipelines of skipped draws, so a burst of new materials is diff --git a/docs/building.md b/docs/building.md index 8237793c..508936f5 100644 --- a/docs/building.md +++ b/docs/building.md @@ -322,6 +322,12 @@ A change to what the library warm logs when the driver's cache of stage librarie `runtime/vk_pipeline.c` `lw_main`) passes `python tests\warm_hint_check.py`, which builds the header alone and checks the added text by GPU vendor and by why the cache was cold. No game files. +A change to which render passes count as drawn frame after frame (`runtime/vk_target_frames.h`: a draw into one is +skipped while its pipeline builds, a draw into any other waits) passes `python tests\target_frames_check.py`, which +builds the header alone and replays an area change after a loading screen from a player's log: the scene, probe and +half-resolution passes are per-frame again at once, a lookup table, a volume's slices and an icon capture still wait, +and an image taken over by another texture starts without the history of the one before. No GPU, no game files. + The lifter (`recomp/`) and the CPU headers (`runtime/cpu.h`, `runtime/simd.h`) are checked against Unicorn. Every lifter change passes these, all run with `.venv`: diff --git a/nx2navi.ini.example b/nx2navi.ini.example index f0b1fc0b..51b33931 100644 --- a/nx2navi.ini.example +++ b/nx2navi.ini.example @@ -90,9 +90,17 @@ # 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 a recent frame rendered; full-screen draws wait, up to 50 ms a frame) or fullscreen +# draws into passes whose target a recent frame rendered; full-screen draws wait, from pipe_frame_wait_ms) or fullscreen # (those and full-screen draws: no wait, but a lighting or fog pass that is dropped shows as a black or unlit frame). # pipe_skip = any +# How many milliseconds one frame may wait, in all, for the shaders and pipelines of its full-screen draws and of +# draws into pictures drawn for the first time (0 to 500). What is not ready by then is left out of that frame and +# built in the background. +# pipe_frame_wait_ms = 50 +# A picture the game has drawn in many frames counts as drawn every frame after a pause too (a loading screen, a +# menu), so the first frame of an area leaves a draw out while its pipeline is built instead of stopping for it. +# 0: as up to 0.7.1, such a frame waits for each new pipeline in turn. +# pipe_skip_steady = 1 # How many milliseconds the draws of one frame may wait, in all, for a missing pipeline to be linked from the shader # libraries in the graphics driver's cache before they are dropped (0 to 100; 0: dropped at once, as before). # pipe_link_ms = 6 @@ -112,13 +120,12 @@ # NVIDIA under Windows, Shader Cache Size Unlimited in the NVIDIA app), start once with force, or delete # shader-warm.txt, to warm again. # lib_warm = 1 -# 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. +# Draws into a picture drawn for the first time (a lookup table, a new area's target) wait for a pipeline built for +# the first time from pipe_frame_wait_ms; one left over is drawn the next frame its picture is drawn. 0: as up to +# 0.7.1, each such draw waits up to oneshot_wait_ms milliseconds, which stops the game for as long as the pipelines +# of a new area take to build. +# oneshot_budget = 1 # oneshot_wait_ms = 30000 -# 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 2605166f..df0d434e 100644 --- a/runtime/cfg.c +++ b/runtime/cfg.c @@ -43,7 +43,7 @@ static const char *const known[] = { "DESC_TEMPLATE", "DEVICE_PROFILE", "DISCORD_APP_ID", "DISCORD_IMAGE", "DISCORD_RPC", "DLAA", "DLAA_CAM_KEEP", "DLSS_IDLE_FREE", "DLAA_JITTER", "DLAA_MV", "DLAA_MASKDEBUG", "DLAA_MVDEBUG", "DLAA_OBJMV", "DLAA_SURVEY", "DLAA_TIMES", "DLSS", "DLSS_CHAR_MASK", "DLSS_PRESET", "DLSS_REACTIVE", "DUMP", "DUMP_BUF", "DUMP_DIR", "DUMP_LOG", "DUMP_RAW", "DUMP_SHADERS", "DUMP_FILE", "DS3", "LOG_CS_BINDS", "LOG_TEX_FLAGS", "LOG_MESH", "MESH", "POOL", "NATIVE_TENSOR", "LOG_RCOND", "LOG_RT_SIZES", "DUMP_WHITE", "DUMP_AFTER", "DUMP_MOVIE", "SUBMIT_FINISH", "ACQUIRE_WAIT", "CRASH_FILTER_GUARD", "CS_FALLBACK", "EXEFS", "FEEDBACK", "FIX_C0", "FIX_ZERO", "FLIGHT", "FLIGHT_AT", "FLIGHT_DIR", "FLIGHT_FRAMES", "FLIGHT_SPIKE_MS", "FLIGHT_THUMBS", "FLIGHT_EVERY", "FLUSH_SPAN", "FLUSH_UPLOAD", "FOG_CULL", "FOLIAGE_SHADOW_CULL", "FOLIAGE_SHADOW_VRS", "FOV", "FOV_PATCH", "FG_HUDLESS_SHOW", "FG_MULT", "FPS60", "FPS_LIMIT", "FRAMEGEN", "FRAMEGEN_MULTIPLIER", "FREEZE_IDLE", "FREE_CORES", - "FULLSCREEN", "FXAA", "GAME_ASPECT", "GAME_RES", "GPA", "GPA_EMPTY", "GPA_TWIN", "GPA_TWIN_MAX_KB", "GPL", "GPL_DRAW_BUILD", "GPL_FEEDBACK", "PIPE_AHEAD", "PIPE_FAST_WORKERS", "PIPE_LINK_DRAW_MS", "PROG_LEAD", "GPU", "GPU_INDEX", "GPU_PASSES", "GPU_NAME", "HDR", "HDR_BLOWOUT", "HDR_CONTRAST", + "FULLSCREEN", "FXAA", "GAME_ASPECT", "GAME_RES", "GPA", "GPA_EMPTY", "GPA_TWIN", "GPA_TWIN_MAX_KB", "GPL", "GPL_DRAW_BUILD", "GPL_FEEDBACK", "PIPE_AHEAD", "PIPE_FAST_WORKERS", "PIPE_FRAME_WAIT_MS", "PIPE_LINK_DRAW_MS", "PIPE_SKIP_STEADY", "PROG_LEAD", "GPU", "GPU_INDEX", "GPU_PASSES", "GPU_NAME", "HDR", "HDR_BLOWOUT", "HDR_CONTRAST", "HDR_EXPOSURE", "HDR_FLARE", "HDR_GAMMA", "HDR_GAME_NITS", "HDR_GRADE", "HDR_HIGHLIGHT_SAT", "HDR_HIGHLIGHTS", "HDR_HUE", "HDR_LIVE", "HDR_PEAK_NITS", "HDR_SATURATION", "HDR_SHADOWS", "HDR_TONEMAP", "HDR_UI_NITS", "HIGHFPS_FIX", "INPUT", "INPUT_RECORD", "INPUT_SCRIPT", "LANG", "LIB_PREBUILD", @@ -114,6 +114,7 @@ static const struct { const char *key, *section; } sections[] = { {"ASTC_GPU_DESWIZZLE", "performance"}, {"ASTC_SCRATCH_KEEP", "performance"}, {"ASTC_ARENA", "performance"}, {"POOL_CREATE_BUDGET", "performance"}, {"POOL_CREATE_MS", "performance"}, {"POOL_LOAD_BACKLOG", "performance"}, {"POOL_LOAD_MS", "performance"}, {"TIMER_1MS", "performance"}, {"PIPE_WAIT", "performance"}, {"PIPE_SKIP", "performance"}, {"ONESHOT_BUDGET", "performance"}, + {"PIPE_FRAME_WAIT_MS", "performance"}, {"PIPE_SKIP_STEADY", "performance"}, {"CACHE_DIE", "performance"}, {"ONE_PER_CORE", "performance"}, {"LIB_PREBUILD", "performance"}, {"LIB_PREBUILD_THREADS", "performance"}, {"LIB_WARM", "performance"}, {"PIPE_LINK_MS", "performance"}, {"PIPE_LINK_DRAW_MS", "performance"}, {"PIPE_AHEAD", "performance"}, {"PIPE_FAST_WORKERS", "performance"}, {"GPL_FEEDBACK", "performance"}, {"FRAME_HOLD_MS", "performance"}, {"PACE_SMOOTH", "performance"}, {"PIPE_PREBUILD", "performance"}, {"COMPUTE_KEEP", "performance"}, @@ -229,6 +230,8 @@ static const CfgMeta metas[] = { {"PIPE_WAIT", "default", "default|draw", RESTART}, {"PIPE_SKIP", "any", "strict|any|fullscreen", RESTART}, {"PIPE_LINK_MS", "6", "0..100", RESTART}, // vk_exec_draw.c link_budget {"PIPE_LINK_DRAW_MS", "3", "1..50", RESTART}, // vk_exec_draw.c link_draw_ms + {"PIPE_FRAME_WAIT_MS", "50", "0..500", RESTART}, // vk_exec_draw.c frame_wait + {"PIPE_SKIP_STEADY", "1", "0|1", RESTART}, // vk_exec.c steady_on {"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 @@ -238,7 +241,7 @@ static const CfgMeta metas[] = { {"LIB_WARM", "1", "0|1|force", RESTART}, // vk_pipeline.c lw_start: the driver's cache of stage libraries, warmed when cold {"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}, {"ONESHOT_BUDGET", "0", "0|1", 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", "1", "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}, // all|: the compute pipelines the boot keeps, those dispatched most recently (vk_pipeline.c, compute-used.bin) {"COMPUTE_KEEP", "512", NULL, RESTART}, diff --git a/runtime/gfx.h b/runtime/gfx.h index 0619209f..168090e5 100644 --- a/runtime/gfx.h +++ b/runtime/gfx.h @@ -48,10 +48,11 @@ const GfxShader *gfx_shader_get(int stage, const uint8_t *control, const uint8_t // NULL returned with *pending = 1 until it is ready. A failed translation stays failed (*pending = 0). const GfxShader *gfx_shader_get_async(int stage, const uint8_t *control, const uint8_t *code, uint64_t code_size, const GfxShaderAnswers *a, int *pending); -// As gfx_shader_get_async, but a shader queued for translation is moved to the front of the queue and waited for. -// NULL when the translation failed. +// As gfx_shader_get_async, but a shader queued for translation is moved to the front of the queue and waited for, +// wait_ms at most (UINT32_MAX: until it is done). NULL when the translation failed, or with *pending = 1 when it was +// still running at the end of the wait: it stays at the front of the queue. const GfxShader *gfx_shader_await(int stage, const uint8_t *control, const uint8_t *code, uint64_t code_size, - const GfxShaderAnswers *a); + const GfxShaderAnswers *a, uint32_t wait_ms, int *pending); uint64_t gfx_shader_async_done(void); // translations finished by the background worker void gfx_shader_counts(uint64_t *keys, uint64_t *translated); // distinct keys in memory, translations done (NX2NAVI_TRACE=perf) void gfx_shader_disk_counts(uint64_t *sync, uint64_t *sync_ns, uint64_t *bg); // disk cache reads on the render diff --git a/runtime/gfx_shader.c b/runtime/gfx_shader.c index c822fc0c..51feb53f 100644 --- a/runtime/gfx_shader.c +++ b/runtime/gfx_shader.c @@ -2211,24 +2211,31 @@ static const GfxShader *get_async_one(Req r, int stage, const uint8_t *control, } const GfxShader *gfx_shader_await(int stage, const uint8_t *control, const uint8_t *code, uint64_t code_size, - const GfxShaderAnswers *a) { - int pending; + const GfxShaderAnswers *a, uint32_t wait_ms, int *pending) { uint8_t ctl2[CONTROL_SIZE]; control = nvn2_control(control, ctl2); - const GfxShader *s = gfx_shader_get_async(stage, control, code, code_size, a, &pending); + const GfxShader *s = gfx_shader_get_async(stage, control, code, code_size, a, pending); Req r; - if (!pending || !req_init(&r, stage, control, code, code_size, NULL, a)) return s; + if (!*pending) return s; + *pending = 0; + if (!req_init(&r, stage, control, code, code_size, NULL, a)) return s; uint64_t p0 = perf_begin(); // the wait counts as the render thread's translation time + uint64_t end = wait_ms == UINT32_MAX ? 0 : plat_now_ms() + wait_ms, now = 0; plat_mutex_lock(&X.lock); to_front(&X.lhead, &X.ltail, r.key); // queued: to the front, a worker takes it next to_front(&X.head, &X.tail, r.key); XWait *w; - while ((w = *wait_slot(r.key)) && !w->failed) plat_cond_wait(&X.done, &X.lock); + while ((w = *wait_slot(r.key)) && !w->failed && (!end || (now = plat_now_ms()) < end)) { + if (end) plat_cond_wait_ms(&X.done, &X.lock, (uint32_t)(end - now)); + else plat_cond_wait(&X.done, &X.lock); + } + int failed = w && w->failed; plat_mutex_unlock(&X.lock); perf_end(PERF_XLATE, p0); - if (w) return NULL; // failed: logged by the worker - s = gfx_shader_get_async(stage, control, code, code_size, a, &pending); - return pending ? NULL : s; + if (failed) return NULL; // logged by the worker + if (w) return *pending = 1, NULL; // the wait ended first + s = gfx_shader_get_async(stage, control, code, code_size, a, pending); + return *pending ? NULL : s; } uint64_t gfx_shader_async_done(void) { return (uint64_t)n_async; } diff --git a/runtime/vk_exec.c b/runtime/vk_exec.c index 6ac73408..2d7e94e4 100644 --- a/runtime/vk_exec.c +++ b/runtime/vk_exec.c @@ -26,6 +26,7 @@ #include "tex.h" #include "vk.h" #include "vk_exec.h" +#include "vk_target_frames.h" #include "vk_dlaa.h" #define WEAK __attribute__((weak)) @@ -477,73 +478,34 @@ static void free_views(void *arg) { // completion thread, once the submission free(l); } -// ---- 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 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[TFR_SETS * TFR_WAYS]; +// ---- target history (vk_target_frames.h: the table, what recent and steady mean, and the pass verdict) +uint64_t vk_exec_steady_passes; // lookups a steady target alone made per-frame (vk_exec_targets_recent) -// 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; - 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]; +static TfrTarget tfr_of(const VkExecTarget *t) { + return (TfrTarget){(uint64_t)(uintptr_t)t->image, t->level, t->base_layer, t->tex ? t->tex->serial : 0}; +} + +static int steady_on(void) { + static int on = -1; + if (on < 0) { + on = cfg_bool("NX2NAVI_PIPE_SKIP_STEADY"); + rt_log("vk: a pass whose targets the game has rendered in %d frames or more counts as per-frame after a " + "pause too, so its draws are skipped, not waited for, while a pipeline builds: %s (NX2NAVI_PIPE_SKIP_STEADY)", + TFR_STEADY, on ? "on" : "off"); } - if (!make) return NULL; - *oldest = (TargetFrames){t->image, t->level, t->base_layer, 0, 0}; - return oldest; + return on; } -static void tfr_note(const VkExecTarget *t, uint64_t stamp) { - TargetFrames *e = tfr_find(t, 1); - if (e->last != stamp) e->prev = e->last, e->last = stamp; -} - -// 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; -} - -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. +// tfr_pass_recent for the current pass; vk_exec_steady_passes counts the lookups that only a steady target made +// per-frame. int vk_exec_targets_recent(VkExec *ex, int any) { resolve_rt(ex); uint64_t stamp = __atomic_load_n(&gfx_counters.frames, __ATOMIC_RELAXED) + 1; - int n = 0, color = 0, depth = 0, color_first = 0; - for (int i = 0; i < 9; i++) { - const VkExecTarget *t = i < 8 ? &ex->color[i] : &ex->depth; - if (!t->image) continue; - n++; - if (tfr_recent(t, stamp)) *(i < 8 ? &color : &depth) = 1; - else if (!any) return 0; - else if (i < 8 && tfr_first(t, stamp)) color_first = 1; - } - return any ? color || (depth && !color_first) : n > 0; + TfrTarget c[8], d = tfr_of(&ex->depth); + for (int i = 0; i < 8; i++) c[i] = tfr_of(&ex->color[i]); + int by_steady = 0, r = tfr_pass_recent(c, 8, &d, stamp, any, steady_on(), &by_steady); + vk_exec_steady_passes += (uint64_t)by_steady; + return r; } // One line naming each target of the current pass: format, extent, layer and the two frames it was last rendered in @@ -555,10 +517,12 @@ 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_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)(e ? e->last : 0), (unsigned long long)(e ? e->prev : 0)); + TfrTarget ft = tfr_of(t); + const TargetFrames *e = tfr_find(&ft, 0); + int w = snprintf(buf + n, cap - n, "%s%c%d fmt %d %ux%u L%u seen %llu/%llu in %u frames", n ? "; " : "", + i < 8 ? 'c' : 'd', i < 8 ? i : 0, (int)t->format, t->width, t->height, t->base_layer, + (unsigned long long)(e ? e->last : 0), (unsigned long long)(e ? e->prev : 0), + e && e->serial == ft.serial ? e->n : 0); if (w < 0) break; n += (size_t)w; } @@ -699,8 +663,14 @@ void vk_exec_pass_end(VkExec *ex) { if (vk_dump_armed) vk_dump_pass_end(ex); uint64_t stamp = __atomic_load_n(&gfx_counters.frames, __ATOMIC_RELAXED) + 1; for (int i = 0; i < 8; i++) - if (ex->color[i].image) vk_texture_gpu_written(ex->color[i].tex), tfr_note(&ex->color[i], stamp); - if (ex->depth.image) vk_texture_gpu_written(ex->depth.tex), tfr_note(&ex->depth, stamp); + if (ex->color[i].image) { + TfrTarget ft = tfr_of(&ex->color[i]); + vk_texture_gpu_written(ex->color[i].tex), tfr_note(&ft, stamp); + } + if (ex->depth.image) { + TfrTarget ft = tfr_of(&ex->depth); + vk_texture_gpu_written(ex->depth.tex), tfr_note(&ft, stamp); + } } VkCommandBuffer vk_exec_inline_begin(int *reopen) { diff --git a/runtime/vk_exec.h b/runtime/vk_exec.h index 6ce192c6..40645d56 100644 --- a/runtime/vk_exec.h +++ b/runtime/vk_exec.h @@ -159,9 +159,10 @@ VkRect2D vk_exec_scissor(const VkExec *ex); void vk_exec_feedback_war(VkExec *ex, uint32_t written); void vk_exec_noatt_dims(const VkExec *ex, uint32_t *w, uint32_t *h); // pass size when no attachment is set // 1: every target of the current pass (any 1: at least one, and not only the depth target while a colour target is -// rendered for the first time) was rendered in the previous presented frame (per-frame targets); 0 otherwise, or when -// the pass has none. +// rendered for the first time) is one the game renders frame after frame: rendered in the last few presented frames, +// or steady (vk_target_frames.h tfr_pass_recent); 0 otherwise, or when the pass has none. int vk_exec_targets_recent(VkExec *ex, int any); +extern uint64_t vk_exec_steady_passes; // its answers of 1 that only a steady target gave (vk_target_frames.h) // Looking ahead (vk_exec.c): called by a draw that found its shader or pipeline missing, before it waits or is skipped. // The rest of the submission being replayed is read once and the stage libraries of the pipelines its later draws need // are queued to the workers (vk_pipeline_hint). vk_exec_ahead_op (vk_exec_draw.c) applies one bind or draw op of that diff --git a/runtime/vk_exec_draw.c b/runtime/vk_exec_draw.c index 4f375ebe..683ec91a 100644 --- a/runtime/vk_exec_draw.c +++ b/runtime/vk_exec_draw.c @@ -43,10 +43,19 @@ #define NVN_STAGE_CS 5 #define NVN_STAGE_TASK 6 // NVN2 #define NVN_STAGE_MESH 7 -// prepare_draw: how long a one-shot draw waits for its pipeline before it is skipped (NX2NAVI_ONESHOT_WAIT_MS, default -// 30000). Such a draw renders into a target the previous frame did not (a volume, a LUT, a target made new at an area -// change); skipped, the target can stay empty for the session, so the default waits. A lower value trades that risk for -// a frame that never stalls longer than the value on a first-time pipeline. +// prepare_draw: how long a one-shot draw waits for its shaders and pipeline before it is skipped. Such a draw renders +// into a target that is neither recent nor steady (vk_target_frames.h: a volume, a lookup table, an icon, a target +// made new at an area change); skipped, a target the game draws once stays as it was. +// NX2NAVI_ONESHOT_BUDGET (default 1): the one-shot draws of a presented frame wait from that frame's wait pool +// (frame_wait, shared with the full-screen draws), a mesh draw MESH_WAIT_MS at most. Until 0.7.1 each waited up to +// NX2NAVI_ONESHOT_WAIT_MS (30 s) on the replay thread, which is what =0 still does: after a scene load every pass was +// new or not recent, so the first frame waited for one pipeline after another, and for the whole compile of each mesh +// pipeline (0.2 to 1.3 s cold): 5-14 s on an RTX 4080 SUPER with no pipeline recorded, and frames of 0.7 to 1 s at +// area changes of a played install (vk_target_frames.h "Steady targets"), while the pipeline workers sat idle. A +// draw skipped at the end of its wait comes back the next frame its target is drawn, when the target is recent and +// the pipeline is built on a worker like any other; a target drawn only once whose pipeline took longer than the pool +// stays as it was until the game draws it again. With the stage libraries in the driver's cache a fast link takes +// 2 to 6 ms, so the pool covers the lookup tables of a load several times over. static uint32_t oneshot_wait_ms(void) { static int64_t ms = -1; if (ms < 0) { @@ -58,32 +67,18 @@ 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 +// A mesh pipeline has no stage libraries and so no fast link: the wait is for its whole build, a few milliseconds +// when the pipeline cache holds it and 200 ms or more when it does not. A longer wait only finds out which. +#define MESH_WAIT_MS 8u 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)"); + on = rt_knob("ONESHOT_BUDGET", "1") == 1; + if (!on) rt_log("vk_exec: a one-shot draw waits up to %u ms for its shaders and pipeline, outside the frame's wait " + "pool (NX2NAVI_ONESHOT_BUDGET=0)", ONESHOT_WAIT_MS); } 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 }; @@ -945,11 +940,12 @@ static void answers_for(VkExec *ex, int st, const uint8_t *ctl, uint32_t mask, G } // *present: the program has this stage. Returns NULL when it has not or the translation failed. pending (graphics -// stages, vk_pipeline_async_on): a shader not translated yet is queued and NULL returned with *pending = 1. await: a -// shader queued earlier is waited for (gfx_shader_await) instead. +// stages, vk_pipeline_async_on): a shader not translated yet is queued and NULL returned with *pending = 1. await_ms: +// a shader queued earlier is waited for instead, that long at most (gfx_shader_await; UINT32_MAX: until it is done), +// and *pending = 1 says the wait ended first. static int sf_mvprev; // shader_for asks for the vertex shader's last-frame variant (per-object motion) -static const GfxShader *shader_for(VkExec *ex, int st, int *present, int *pending, int await) { +static const GfxShader *shader_for(VkExec *ex, int st, int *present, int *pending, uint32_t await_ms) { const uint8_t *ctl, *code; uint32_t len; *present = 0; @@ -977,7 +973,9 @@ static const GfxShader *shader_for(VkExec *ex, int st, int *present, int *pendin } int gs = st == NVN_STAGE_VS ? GFX_STAGE_VS : st == NVN_STAGE_FS ? GFX_STAGE_FS : st == NVN_STAGE_TASK ? GFX_STAGE_TS : st == NVN_STAGE_MESH ? GFX_STAGE_MS : GFX_STAGE_CS; - const GfxShader *s = await ? gfx_shader_await(gs, ctl, code, len, &a) + int late = 0; + if (await_ms && !pending) pending = ⪭ + const GfxShader *s = await_ms ? gfx_shader_await(gs, ctl, code, len, &a, await_ms, pending) : pending && (vk_pipeline_async_on() || a.mvprev) ? gfx_shader_get_async(gs, ctl, code, len, &a, pending) : gfx_shader_get(gs, ctl, code, len, NULL, &a); if (pending && *pending) return NULL; @@ -3341,6 +3339,61 @@ static int pipe_skip_mode(void) { // cold it runs out after a few draws and the rest are skipped as before, so a burst of cold pipelines costs a frame // this much and no more. Measured on an RX 7900 XT, first walk through a village with no pipeline recorded: 2,349 // draws skipped without the budget, with the libraries warm in both runs. +// NX2NAVI_PIPE_FRAME_WAIT_MS (default 50; 0 to 500): the wait pool of one presented frame. Its full-screen draws and +// its one-shot draws (NX2NAVI_ONESHOT_BUDGET) wait for their shaders and pipelines from it, each for what the ones +// before left, and are skipped once it is spent, so the pipelines of one frame hold the replay thread this long in +// all and no longer (beside the fast links' own small budget, link_budget, and FULLSCREEN_LAST_MS). +static uint64_t *frame_wait(void) { + static uint64_t frame = ~0ull, left, per_frame = ~0ull; + if (per_frame == ~0ull) { + long long ms = cfg_int("NX2NAVI_PIPE_FRAME_WAIT_MS"); + per_frame = (uint64_t)(ms < 0 ? 0 : ms > 500 ? 500 : ms) * 1000000ull; + rt_log("vk_exec: the full-screen and one-shot draws of a frame wait up to %llu ms in all for their shaders and " + "pipelines (NX2NAVI_PIPE_FRAME_WAIT_MS), then are skipped while a worker builds them", + (unsigned long long)(per_frame / 1000000)); + } + uint64_t f = __atomic_load_n(&gfx_counters.frames, __ATOMIC_RELAXED); + if (f != frame) frame = f, left = per_frame; + return &left; +} + +// The milliseconds of pool not used up since t0, when the caller began to draw on it; UINT32_MAX without a pool. +static uint32_t pool_ms(const uint64_t *pool, uint64_t t0) { + if (!pool) return UINT32_MAX; + uint64_t spent = kern_now_ns() - t0; + return spent < *pool ? (uint32_t)((*pool - spent) / 1000000) : 0; +} + +static void pool_charge(uint64_t *pool, uint64_t t0) { + if (!pool) return; + uint64_t spent = kern_now_ns() - t0; + *pool = spent < *pool ? *pool - spent : 0; +} + +// What a one-shot draw may wait for its pipeline from pool. +static uint32_t oneshot_ms(const uint64_t *pool, int mesh) { + uint32_t ms = (uint32_t)(*pool / 1000000); + return mesh && ms > MESH_WAIT_MS ? MESH_WAIT_MS : ms; +} + +// ---- wait accounting: what the replay thread spent on draws that found their pipeline or a shader missing +// Counted from the miss to the draw's bind or skip, the look-ahead scan included, by the kind of wait the draw was +// allowed; left: the draws skipped at the end of it. draw_stats_tick logs the sums with the skipped draws, so a +// session's log says how much time the pipelines cost on this thread and which rule spent it. +enum { WK_LINK, WK_FULL, WK_LAST, WK_ONESHOT, WK_ONESHOT_MESH, WK_SHADER, WK_N }; +static const char *const wk_names[WK_N] = {"a fast link", "full-screen", "full-screen with no picture to hold", + "one-shot", "one-shot mesh", "one-shot, for a shader"}; +static struct { uint64_t n[WK_N], ns[WK_N], max_ns[WK_N], left[WK_N], frame_ns, frame_max_ns, frame; } WS; + +static void wait_note(int kind, uint64_t t0, int left) { + uint64_t ns = kern_now_ns() - t0, f = __atomic_load_n(&gfx_counters.frames, __ATOMIC_RELAXED); + WS.n[kind]++, WS.ns[kind] += ns, WS.left[kind] += left != 0; + if (ns > WS.max_ns[kind]) WS.max_ns[kind] = ns; + if (f != WS.frame) WS.frame = f, WS.frame_ns = 0; + WS.frame_ns += ns; + if (WS.frame_ns > WS.frame_max_ns) WS.frame_max_ns = WS.frame_ns; +} + static uint32_t link_draw_ms(void) { static int ms = -1; if (ms < 0) { @@ -3350,9 +3403,12 @@ static uint32_t link_draw_ms(void) { return (uint32_t)ms; } #define LINK_DRAW_MS link_draw_ms() -// How long a full-screen draw waits on for its pipeline once its frame budget is spent and the previous picture -// cannot be held any longer (prepare_draw_ex): the longest optimised build of such a pipeline measured was 194 ms. -#define FULLSCREEN_LAST_MS 500u +// How long a full-screen draw waits on for its pipeline once the frame's wait pool is spent and the previous picture +// cannot be held any longer (prepare_draw_ex). Every such pipeline has a fast link (vk_pipeline.c gpl_feedback), so +// the wait is for stage libraries: a few milliseconds from the driver's cache, and in the one case a player's session +// shows the draw had its pipeline 18 ms into this wait. It was 500 ms while a pipeline with a feedback loop waited +// for its whole optimised build; with the pool before it, a frame now waits under 100 ms for pipelines in all. +#define FULLSCREEN_LAST_MS 40u static uint64_t *link_budget(void) { static uint64_t frame = ~0ull, left, per_frame = ~0ull; if (per_frame == ~0ull) { @@ -3438,6 +3494,25 @@ static void draw_stats_tick(void) { uint64_t f = __atomic_load_n(&gfx_counters.frames, __ATOMIC_RELAXED); if (!DRS.frame) DRS.frame = f; if (f - DRS.frame < DRAW_STATS_FRAMES) return; + uint64_t waits = 0, wait_ns = 0; + for (int k = 0; k < WK_N; k++) waits += WS.n[k], wait_ns += WS.ns[k]; + static uint64_t steady_logged; + if (waits || vk_exec_steady_passes != steady_logged) { + char line[900]; + int o = snprintf(line, sizeof line, "vk_exec: over %llu frames %llu draws found a shader or pipeline missing and " + "held the replay thread %.1f ms in all, %.1f ms in the worst frame; by the wait they were allowed:", + (unsigned long long)(f - DRS.frame), (unsigned long long)waits, wait_ns / 1e6, WS.frame_max_ns / 1e6); + for (int k = 0; k < WK_N && o < (int)sizeof line - 160; k++) + if (WS.n[k]) + o += snprintf(line + o, sizeof line - o, " %s %llu (%.1f ms, longest %.1f ms, %llu skipped at its end);", + wk_names[k], (unsigned long long)WS.n[k], WS.ns[k] / 1e6, WS.max_ns[k] / 1e6, + (unsigned long long)WS.left[k]); + snprintf(line + o, sizeof line - o, " %llu lookups found only steady targets and skipped a draw that would have " + "waited as one-shot", (unsigned long long)(vk_exec_steady_passes - steady_logged)); + rt_log("%s", line); + steady_logged = vk_exec_steady_passes; + memset(&WS, 0, sizeof WS); + } for (int m = 0; m < 2; m++) { uint64_t any = 0; for (int k = 0; k < SK_COUNT; k++) any += DRS.skipped[m][k]; @@ -3582,9 +3657,17 @@ static int prepare_draw_ex(VkExec *ex, int prim, int32_t count, int restart, int if (!(oneshot = !vk_exec_targets_recent(ex, skip >= 1))) // skipped until ready return perf_count(PERF_CNT_SHADER_SKIP), draw_skipped(ex, mesh, SK_SHADER_PENDING); perf_count(PERF_CNT_ONESHOT_WAIT); - if (vs_wait) vs = shader_for(ex, vst, &has_vs, NULL, 1); - if (ts_wait) ts = shader_for(ex, NVN_STAGE_TASK, &has_ts, NULL, 1); - if (fs_wait) fs = shader_for(ex, NVN_STAGE_FS, &has_fs, NULL, 1); + // from the frame's wait pool (NX2NAVI_ONESHOT_BUDGET=0: until each is translated); a shader the pool does not + // reach stays at the front of the translators' queue, and the draw is skipped like a per-frame one + uint64_t *pool = oneshot_budget_on() ? frame_wait() : NULL, x0 = kern_now_ns(); + uint32_t ms; + if (vs_wait && (ms = pool_ms(pool, x0))) vs = shader_for(ex, vst, &has_vs, &vs_wait, ms); + if (ts_wait && (ms = pool_ms(pool, x0))) ts = shader_for(ex, NVN_STAGE_TASK, &has_ts, &ts_wait, ms); + if (fs_wait && (ms = pool_ms(pool, x0))) fs = shader_for(ex, NVN_STAGE_FS, &has_fs, &fs_wait, ms); + pool_charge(pool, x0); + wait_note(WK_SHADER, x0, vs_wait || ts_wait || fs_wait); + if (vs_wait || ts_wait || fs_wait) + return perf_count(PERF_CNT_SHADER_SKIP), draw_skipped(ex, mesh, SK_SHADER_PENDING); } if (!vs || (has_fs && !fs) || (has_ts && !ts)) return draw_skipped(ex, mesh, SK_SHADER_FAILED); if (shader_skipped(vs) || shader_skipped(fs)) return draw_skipped(ex, mesh, SK_POOL_OFF); // NX2NAVI_SKIP_SHADERS @@ -3601,29 +3684,25 @@ static int prepare_draw_ex(VkExec *ex, int prim, int32_t count, int restart, int const VkPipeInfo *info = NULL; 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, 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; + // its pipeline: skipping it can leave a black tile. Those waits and the one-shot draws' come from the frame's wait + // pool (frame_wait; NX2NAVI_PIPE_SKIP=fullscreen skips the full-screen draws instead, and with + // NX2NAVI_ONESHOT_BUDGET=0 a one-shot draw waits up to ONESHOT_WAIT_MS outside the pool). Everything else waits + // for its fast link from the small budget of link_budget and is skipped while its pipeline is built. uint32_t wait_ms = 0; uint64_t t0 = 0; uint64_t *budget = NULL; // the per-frame budget a timed wait is charged to int linking = 0; // the wait is the short one for a fast link (link_budget) - int large = (uint64_t)ex->pass_w * ex->pass_h >= ONESHOT_LARGE_PX; + int fullscreen = 0; // the wait is a full-screen draw's if (oneshot > 0 && oneshot_budget_on()) { - budget = oneshot_budget(large); - wait_ms = (uint32_t)(*budget / 1000000); + budget = frame_wait(); + wait_ms = oneshot_ms(budget, mesh); 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); + budget = frame_wait(), fullscreen = 1; + wait_ms = (uint32_t)(*budget / 1000000); if (wait_ms) t0 = kern_now_ns(); - budget = &wait_left_ns; } else { budget = link_budget(); wait_ms = (uint32_t)(*budget / 1000000); @@ -3636,7 +3715,8 @@ static int prepare_draw_ex(VkExec *ex, int prim, int32_t count, int restart, int // from the info the cache entry keeps. VkPipeline p; const char *why = NULL; - uint64_t w0 = 0; + uint64_t w0 = 0, m0 = 0; // m0: when the lookup missed (wait_note) + int kind = WK_LINK; if (pipe_memo_on() && PM.valid && !memcmp(&PM.s, &s, sizeof s)) { info = PM.info, pending = 0; p = __atomic_load_n(&info->pipeline, __ATOMIC_ACQUIRE); @@ -3646,6 +3726,8 @@ static int prepare_draw_ex(VkExec *ex, int prim, int32_t count, int restart, int // of the later draws' pipelines build on the workers from here on), inside this draw's wait budget. p = vk_pipeline_graphics_async(&s, &info, &pending, 0); if (pending) { + m0 = kern_now_ns(); + kind = oneshot > 0 ? (mesh ? WK_ONESHOT_MESH : WK_ONESHOT) : fullscreen ? WK_FULL : WK_LINK; if (linking) t0 = kern_now_ns(); else w0 = t0 ? t0 : wait_ms ? kern_now_ns() : 0; vk_exec_ahead(ex); @@ -3659,25 +3741,26 @@ static int prepare_draw_ex(VkExec *ex, int prim, int32_t count, int restart, int if (pending && oneshot < 0 && !vk_exec_targets_recent(ex, skip >= 1)) { perf_count(PERF_CNT_ONESHOT_WAIT); why = "one-shot"; + kind = mesh ? WK_ONESHOT_MESH : WK_ONESHOT; if (!w0) w0 = kern_now_ns(); 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; + uint64_t *pool = frame_wait(), b0 = kern_now_ns(); + p = vk_pipeline_graphics_async(&s, &info, &pending, oneshot_ms(pool, mesh)); + pool_charge(pool, b0); } else { p = vk_pipeline_graphics_async(&s, &info, &pending, ONESHOT_WAIT_MS); } } // A full-screen draw still pending after its budget: skipped, it leaves the frame black or unlit. While the // previous picture can stay on screen in its place (vk_frame_hold_left) that is what happens; once that time - // is used up the draw waits on, FULLSCREEN_LAST_MS at most, so that no such frame is shown. - if (pending && budget == &wait_left_ns && !vk_frame_hold_left()) { + // is used up the draw waits on, FULLSCREEN_LAST_MS at most, before such a frame is shown. + if (pending && fullscreen && !vk_frame_hold_left()) { why = "full-screen, nothing left to hold the picture"; + kind = WK_LAST; if (!w0) w0 = kern_now_ns(); p = vk_pipeline_graphics_async(&s, &info, &pending, FULLSCREEN_LAST_MS); } + if (m0) wait_note(kind, m0, pending), draw_stats_tick(); PM.valid = !pending && p && info; if (PM.valid) PM.s = s, PM.info = info; } diff --git a/runtime/vk_target_frames.h b/runtime/vk_target_frames.h new file mode 100644 index 00000000..40aa8249 --- /dev/null +++ b/runtime/vk_target_frames.h @@ -0,0 +1,106 @@ +// Target history: the presented frame (gfx_counters.frames + 1; 0: never) each render target subresource was last +// rendered into, the one before, and in how many frames. vk_exec_draw.c prepare_draw_ex lets a draw into a target the +// game does not render frame after frame (a one-shot draw: a volume or lookup table generated once) wait for its +// shaders and pipeline instead of skipping it. Own header so tests/target_frames_check.c checks the rules without +// Vulkan; vk_exec.c is the one runtime file that includes it (the table is static). +// 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, so a new area was entered one pipeline at a time. +// +// Steady targets (NX2NAVI_PIPE_SKIP_STEADY, default 1; 0 = as before). Recent alone called every pass one-shot in the +// first frame after the game had drawn something else for a while (a loading screen between two areas, a full-screen +// menu): the scene's own targets, the probe layers and the half-resolution buffers had last been rendered hundreds of +// frames before, so each draw with a new pipeline waited on the replay thread, and a mesh draw, whose pipeline has no +// fast link, for its whole compile. One player's session shows it three times at area changes: frames of 954, 1015 +// and 684 ms, six mesh draws waiting 52 to 660 ms each in passes whose targets had been rendered in thousands of +// frames. An entry therefore also counts the frames its subresource was rendered in, for as long as it is the same +// guest texture (its initialization serial: an image taken over by another texture starts again at none). From +// TFR_STEADY frames on the target is one the game draws again and again, and its draws are skipped while a pipeline +// builds, as in any other frame, however long ago the last one was. A target generated once never gets there; one +// generated again at every area load would after TFR_STEADY loads, by when its pipelines are recorded and prebuilt +// at the start. +#pragma once + +#include + +#define TFR_SETS 1024 +#define TFR_WAYS 8 +#define TFR_RECENT 4 +#define TFR_STEADY 8 + +// One render target of a pass: the image handle as a number, the subresource, the guest texture's serial (0 unknown). +typedef struct { uint64_t image; uint32_t level, layer; uint64_t serial; } TfrTarget; +typedef struct { uint64_t image; uint32_t level, layer, n; uint64_t last, prev, serial; } TargetFrames; +static TargetFrames tfr[TFR_SETS * TFR_WAYS]; + +// 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 inline TargetFrames *tfr_find(const TfrTarget *t, int make) { + uint64_t h = (t->image ^ (uint64_t)t->level << 56 ^ (uint64_t)t->layer << 40) * 0x9E3779B97F4A7C15ull; + 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->layer) return &set[i]; + if (set[i].last < oldest->last) oldest = &set[i]; + } + if (!make) return NULL; + *oldest = (TargetFrames){.image = t->image, .level = t->level, .layer = t->layer}; + return oldest; +} + +// The target is rendered in frame stamp. +static inline void tfr_note(const TfrTarget *t, uint64_t stamp) { + TargetFrames *e = tfr_find(t, 1); + if (e->serial != t->serial) e->serial = t->serial, e->n = 0; // another texture has the image now: its own count + if (e->last != stamp) e->prev = e->last, e->last = stamp, e->n += e->n < UINT32_MAX; +} + +// The frame before stamp that last rendered the target, 0 for none. +static inline uint64_t tfr_before(const TfrTarget *t, uint64_t stamp) { + const TargetFrames *e = tfr_find(t, 0); + return !e ? 0 : e->last == stamp ? e->prev : e->last; +} + +static inline int tfr_recent(const TfrTarget *t, uint64_t stamp) { + uint64_t before = tfr_before(t, stamp); + return before && stamp - before <= TFR_RECENT; +} + +// No frame before stamp rendered the target. +static inline int tfr_first(const TfrTarget *t, uint64_t stamp) { return !tfr_before(t, stamp); } + +// TFR_STEADY frames or more before stamp rendered the target as the texture it is now. +static inline int tfr_steady(const TfrTarget *t, uint64_t stamp) { + const TargetFrames *e = tfr_find(t, 0); + return e && e->serial == t->serial && e->n - (e->last == stamp) >= TFR_STEADY; +} + +// Whether the pass with the n_color colour targets c and the depth target d (NULL: none) is one the game renders +// frame after frame, so that a draw into it may be skipped in frame stamp and comes back in the next. +// any 0: every target is recent (and the pass has one). any 1: a colour target is, or the depth target is while no +// colour target is rendered for the first time: the game's item icon capture draws into a new colour target with the +// per-frame depth buffer, once, and its draws skipped there stay missing from the icon until the item is captured +// again. steady 1: a steady target counts as a recent one; *by_steady is set when only that made the answer 1. +static inline int tfr_pass_recent(const TfrTarget *c, int n_color, const TfrTarget *d, uint64_t stamp, int any, + int steady, int *by_steady) { + int n = 0, color[2] = {0, 0}, depth[2] = {0, 0}, color_first = 0, all[2] = {1, 1}; + for (int i = 0; i <= n_color; i++) { + const TfrTarget *t = i < n_color ? &c[i] : d; + if (!t || !t->image) continue; + n++; + int recent = tfr_recent(t, stamp), kept = recent || (steady && tfr_steady(t, stamp)); + if (recent) *(i < n_color ? &color[0] : &depth[0]) = 1; + else all[0] = 0; + if (kept) *(i < n_color ? &color[1] : &depth[1]) = 1; + else all[1] = 0; + if (i < n_color && tfr_first(t, stamp)) color_first = 1; // one never rendered is neither recent nor steady + } + int r[2]; // without the steady rule, and with it + for (int k = 0; k < 2; k++) r[k] = any ? color[k] || (depth[k] && !color_first) : n > 0 && all[k]; + if (by_steady) *by_steady = r[1] && !r[0]; + return r[1]; +} diff --git a/tests/target_frames_check.c b/tests/target_frames_check.c new file mode 100644 index 00000000..0dd5b81e --- /dev/null +++ b/tests/target_frames_check.c @@ -0,0 +1,119 @@ +// runtime/vk_target_frames.h: which passes count as rendered frame after frame, so that a draw into them is skipped +// while its pipeline builds instead of holding the replay thread. Built and run by tests/target_frames_check.py; +// prints each failed check and exits 1 on any. +// The frame numbers of the first block are those of a player's log: an area change after 238 frames of loading +// screen, where every pass read as one-shot and six mesh draws waited 52 to 660 ms each. +#include +#include + +#include "vk_target_frames.h" + +static int fails; +#define CHECK(c, ...) do { if (!(c)) { fails++; printf("FAIL line %d: ", __LINE__); printf(__VA_ARGS__); printf("\n"); } } while (0) + +static TfrTarget T(uint64_t image, uint32_t level, uint32_t layer, uint64_t serial) { + return (TfrTarget){image, level, layer, serial}; +} + +// The verdict with the steady rule and without it; *by: only the steady rule made it per-frame. +static int pass(const TfrTarget *c, int n, const TfrTarget *d, uint64_t stamp, int any, int steady, int *by) { + int b = 0, r = tfr_pass_recent(c, n, d, stamp, any, steady, &b); + if (by) *by = b; + return r; +} + +int main(void) { + int by; + // ---- the area change of the log + TfrTarget scene = T(0x1000, 0, 0, 11), scene_d = T(0x1001, 0, 0, 12); // drawn every frame + TfrTarget probe = T(0x2000, 0, 0, 21), probe_d = T(0x2001, 0, 0, 22); // every second frame + TfrTarget probe_l3 = T(0x2000, 0, 3, 21); // a layer drawn every 15th frame + TfrTarget half = T(0x3000, 0, 0, 31), half_d = T(0x3001, 0, 0, 32); // the half-resolution buffers + for (uint64_t f = 16000; f <= 17925; f++) { + tfr_note(&scene, f), tfr_note(&scene_d, f), tfr_note(&half, f), tfr_note(&half_d, f); + if (f % 2) tfr_note(&probe, f), tfr_note(&probe_d, f); + if (f % 15 == 0) tfr_note(&probe_l3, f); + } + // the frame after: per-frame by being recent, with or without the rule + CHECK(pass(&scene, 1, &scene_d, 17926, 1, 0, NULL) == 1, "a pass drawn the frame before is per-frame"); + CHECK(pass(&scene, 1, &scene_d, 17926, 0, 0, NULL) == 1, "strict: every target recent"); + CHECK(pass(&scene, 1, &scene_d, 17926, 1, 1, &by) == 1 && !by, "recent passes are not counted as steady ones"); + // 238 frames of loading screen later (its own targets are other images) + const uint64_t now = 18163; + TfrTarget load = T(0x9000, 0, 0, 91); + for (uint64_t f = 17926; f < now; f++) tfr_note(&load, f); + CHECK(pass(&scene, 1, &scene_d, now, 1, 0, NULL) == 0, "without the rule the scene pass reads as one-shot after the pause"); + CHECK(pass(&scene, 1, &scene_d, now, 1, 1, &by) == 1 && by, "the scene pass is per-frame after the pause"); + CHECK(pass(&scene, 1, &scene_d, now, 0, 1, &by) == 1 && by, "strict: the scene pass too"); + CHECK(pass(&probe, 1, &probe_d, now, 1, 1, NULL) == 1, "the probe pass (128x128, seen 17925/17923) is per-frame"); + CHECK(pass(&probe_l3, 1, &probe_d, now, 1, 0, NULL) == 0, "a layer drawn every 15th frame is never recent"); + CHECK(pass(&probe_l3, 1, &probe_d, now, 1, 1, NULL) == 1, "a layer drawn every 15th frame, in 128 frames, is steady"); + // the 960x540 pass of the log: one colour target made new, one steady, the depth target drawn earlier this frame + TfrTarget fresh = T(0x4000, 0, 0, 41), cc[2] = {fresh, half}; + tfr_note(&fresh, now), tfr_note(&half, now), tfr_note(&half_d, now); // the pass begins: its targets are noted + CHECK(pass(cc, 2, &half_d, now, 1, 0, NULL) == 0, "without the rule: no recent colour target, a new one beside"); + CHECK(pass(cc, 2, &half_d, now, 1, 1, &by) == 1 && by, "a steady colour target beside a new one: per-frame"); + CHECK(pass(cc, 2, &half_d, now, 0, 1, NULL) == 0, "strict: the new colour target keeps the pass one-shot"); + // the next frame all of it is recent again + tfr_note(&scene, now), tfr_note(&scene_d, now); + CHECK(pass(&scene, 1, &scene_d, now + 1, 1, 0, NULL) == 1, "recent again in the next frame"); + CHECK(pass(&fresh, 1, &half_d, now + 1, 1, 0, NULL) == 1, "the new target is recent in its second frame"); + + // ---- what stays one-shot + TfrTarget lut = T(0x5000, 0, 0, 51); + CHECK(pass(&lut, 1, NULL, now, 1, 1, NULL) == 0, "a target never rendered is one-shot"); + tfr_note(&lut, now); + CHECK(pass(&lut, 1, NULL, now, 1, 1, NULL) == 0, "its first frame, already noted by its pass, is still one-shot"); + tfr_note(&lut, now); // a second pass into it in the same frame + for (int load_no = 1; load_no < TFR_STEADY; load_no++) { // generated again at each area load, far apart + uint64_t f = now + (uint64_t)load_no * 5000; + CHECK(pass(&lut, 1, NULL, f, 1, 1, NULL) == 0, "a table generated at %d area loads is one-shot at the next", load_no); + tfr_note(&lut, f); + } + CHECK(pass(&lut, 1, NULL, now + (uint64_t)TFR_STEADY * 5000, 1, 1, NULL) == 1, + "after %d loads the table counts as steady: its pipelines are recorded by then", TFR_STEADY); + // a volume generated a few slices per frame: each slice is its own target and is rendered once + for (uint32_t z = 0; z < 64; z++) { + TfrTarget slice = T(0x6000, 0, z, 61); + CHECK(pass(&slice, 1, NULL, now + 10 + z / 4, 1, 1, NULL) == 0, "volume slice %u is one-shot", z); + tfr_note(&slice, now + 10 + z / 4); + } + // the item icon capture: a new colour target with the per-frame depth buffer, once + TfrTarget icon = T(0x7000, 0, 0, 71); + uint64_t f2 = now + 1; + tfr_note(&scene_d, f2), tfr_note(&icon, f2); + CHECK(pass(&icon, 1, &scene_d, f2, 1, 1, NULL) == 0, "an icon's first capture is one-shot though its depth buffer is recent"); + CHECK(pass(&icon, 1, &scene_d, f2, 1, 0, NULL) == 0, "and was before the rule"); + // an image taken over by another texture starts again: the per-frame history of its handle is not the new texture's + TfrTarget reused = T(scene.image, 0, 0, 500); + CHECK(tfr_steady(&scene, f2 + 300) == 1, "the scene target is steady"); + CHECK(pass(&reused, 1, NULL, f2 + 300, 1, 1, NULL) == 0, "the same image as another texture is not steady"); + tfr_note(&reused, f2 + 300); + CHECK(tfr_steady(&scene, f2 + 301) == 0, "and the texture that had it lost its count with the image"); + CHECK(tfr_find(&reused, 0)->n == 1, "the new texture's count begins at one"); + // a pass with no target at all + CHECK(pass(NULL, 0, NULL, now, 1, 1, NULL) == 0 && pass(NULL, 0, NULL, now, 0, 1, NULL) == 0, "no targets: not per-frame"); + + // ---- the table keeps the steady targets through an area's first frame + // 400 per-frame targets, a pause, then 600 targets made new are rendered before the old ones come round again + memset(tfr, 0, sizeof tfr); + for (uint64_t f = 1; f <= 100; f++) + for (uint32_t i = 0; i < 400; i++) { + TfrTarget t = T(0x7f3a00000000ull + (uint64_t)(i / 4) * 0x2c0, 0, i % 4, 1000 + i / 4); + tfr_note(&t, f); + } + for (uint32_t i = 0; i < 600; i++) { + TfrTarget t = T(0x7f3b00000000ull + (uint64_t)i * 0x2c0, 0, 0, 5000 + i); + tfr_note(&t, 400); + } + int kept = 0; + for (uint32_t i = 0; i < 400; i++) { + TfrTarget t = T(0x7f3a00000000ull + (uint64_t)(i / 4) * 0x2c0, 0, i % 4, 1000 + i / 4); + kept += tfr_steady(&t, 400); + } + CHECK(kept == 400, "%d of 400 steady targets still known after 600 new ones in one frame", kept); + + if (fails) return printf("%d checks failed\n", fails), 1; + printf("target_frames_check: ok\n"); + return 0; +} diff --git a/tests/target_frames_check.py b/tests/target_frames_check.py new file mode 100644 index 00000000..ec96037a --- /dev/null +++ b/tests/target_frames_check.py @@ -0,0 +1,40 @@ +"""Build and run tests/target_frames_check.c: runtime/vk_target_frames.h, which render passes count as drawn frame +after frame, so that a draw into them is skipped while its pipeline builds instead of waited for. + +No GPU, no game files, any Python. + +Usage: target_frames_check.py +""" +import os +import shutil +import subprocess +import sys +from pathlib import Path + +ROOT = Path(__file__).resolve().parents[1] +MSYS_BIN = Path(r'C:\msys64\ucrt64\bin') + + +def compiler(): + """$CC, else MSYS2 UCRT64's clang, else clang on PATH (as tests/diff/common.py picks it, without its Unicorn).""" + if os.environ.get('CC'): + return os.environ['CC'] + if os.name == 'nt' and (MSYS_BIN / 'clang.exe').exists(): + return str(MSYS_BIN / 'clang.exe') + return shutil.which('clang') or 'clang' + + +def main(): + work = ROOT / 'build' / 'test' / 'target_frames' + work.mkdir(parents=True, exist_ok=True) + exe = work / ('target_frames_check.exe' if os.name == 'nt' else 'target_frames_check') + env = dict(os.environ) + if os.name == 'nt' and MSYS_BIN.exists(): + env['PATH'] = str(MSYS_BIN) + os.pathsep + env.get('PATH', '') + subprocess.check_call([compiler(), '-std=gnu11', '-O1', '-Wall', '-Werror', '-I', str(ROOT / 'runtime'), + str(ROOT / 'tests' / 'target_frames_check.c'), '-o', str(exe)], env=env) + return subprocess.run([str(exe)], env=env).returncode + + +if __name__ == '__main__': + sys.exit(main()) From 9d257edf5ab4ebcab776799e70b3854fde125f80 Mon Sep 17 00:00:00 2001 From: chrg6 Date: Tue, 10 Nov 2026 12:02:24 -0500 Subject: [PATCH 3/4] The log says which missed pipelines were slow and why, and a slow frame that waited for a pipeline is charged to the pipeline. A session's log gave the counts of slow pipelines and slow frames but not what they were. Now: - Each pipeline a draw missed that took 50 ms or longer to become usable gets a line (the first 48 of a run): mesh or vertex, fast-linked or built whole, whether pipeline-keys.bin named it, its shaders' input ids and where their translations came from (the shader cache, a previous translator's, or translated in this session). The summary line counts the same: how many were mesh pipelines, how many the key file named and the prebuild had not reached, how many had a shader translated in this session. - Each shader library that took 25 ms or longer gets a line (the first 32): its shader, the translation's origin, and whether outputs were added for its fragment shader, which is a module the preparation never built. - The count of draw lookups that "found shader libraries missing" says what it counts: a library not made in this process yet, not one the driver's cache lacks. - After the boot prewarm one line says how many of the recorded pipelines' shaders are a previous translator's translations, beside the pipeline cache hits that show whether any pipeline was compiled again. - The frame-time summary's "over 25 ms by cause" took the replay's whole time as one part, so every frame that waited for a pipeline, a translation or a texture upload inside the replay counted as "replay". The replay's part is now its time without those waits. gfx_shader_origin gives a translation's origin. Co-Authored-By: Claude Fable 5.2 --- runtime/gfx.h | 3 ++ runtime/gfx_shader.c | 4 ++ runtime/perf.c | 7 +++- runtime/vk_pipeline.c | 96 +++++++++++++++++++++++++++++++++++-------- 4 files changed, 91 insertions(+), 19 deletions(-) diff --git a/runtime/gfx.h b/runtime/gfx.h index 168090e5..bcff7188 100644 --- a/runtime/gfx.h +++ b/runtime/gfx.h @@ -62,6 +62,9 @@ void gfx_shader_set_host(int float64, uint32_t sbuf_align, const uint32_t subgro int bda); const uint8_t *gfx_shader_key(const GfxShader *s); // the 32-byte cache key (stable while the translator is) const uint8_t *gfx_shader_input_id(const GfxShader *s); // the 32-byte input id (stable across translators) +// Where the translation in use came from: 0 the shader cache, under the current translator; 1 the shader cache, a +// previous translator's (gfx_shader.c "Carry"); 2 translated in this process, so nothing was prepared from it. +int gfx_shader_origin(const GfxShader *s); // By input id from memory or disk; xlate 1 also translates it from its stored inputs. NULL if absent. const GfxShader *gfx_shader_load_input(const uint8_t iid[32], int xlate); // Boot prewarm: out[i] = gfx_shader_load_input(iids[i], 1) on `threads` threads with up to `procs` translators, then diff --git a/runtime/gfx_shader.c b/runtime/gfx_shader.c index 51feb53f..be532ff2 100644 --- a/runtime/gfx_shader.c +++ b/runtime/gfx_shader.c @@ -129,6 +129,7 @@ struct GfxShader { // (4 bits per target, the SPH omap); 0 unknown (older translations) uint8_t carried; // 1 + the previous identity (C.prev) that translated it; 0 the // current translator + uint8_t fresh; // translated in this process (translate_one) }; static PlatMutex cache_lock = PLAT_MUTEX_INIT; // memory cache @@ -1973,6 +1974,7 @@ static const GfxShader *translate_one(const Req *r, int stage, const uint8_t *co } in_store(s->iid, stage, control, code, r, a); plat_inc64(&n_xlated); + s->fresh = 1; return mem_insert(s); } @@ -2244,6 +2246,8 @@ void gfx_shader_counts(uint64_t *keys, uint64_t *translated) { *keys = (uint64_t const uint8_t *gfx_shader_key(const GfxShader *s) { return s->key; } +int gfx_shader_origin(const GfxShader *s) { return s->fresh ? 2 : s->carried ? 1 : 0; } + const uint8_t *gfx_shader_input_id(const GfxShader *s) { return s->iid; } // By input id: memory, then the disk cache under the current translator, then (xlate 1) a translation from the stored diff --git a/runtime/perf.c b/runtime/perf.c index aab9a331..f801ca97 100644 --- a/runtime/perf.c +++ b/runtime/perf.c @@ -133,6 +133,9 @@ typedef struct { // What a frame over 25 ms spent its time on, so a count of slow frames says where to look instead of only that they // happened. A frame is charged to the largest part when that part is at least a quarter of it, and to "unattributed" // otherwise - the part of a slow frame none of the recorded waits explains (the GPU, or a stall outside them). +// The replay's part is its time without the pipeline, translation and texture waits, which all happen inside it: with +// them in, every frame that waited 900 ms for a pipeline was charged to "replay", the larger number, and a log +// could not tell a pipeline stall from a slow replay. enum { FT_C_PIPE, FT_C_XLATE, FT_C_TEX, FT_C_FENCE, FT_C_PACE, FT_C_REPLAY, FT_C_OTHER, FT_C_N }; static const char *const ft_cause_name[FT_C_N] = { "pipeline", "translation", "texture", "fence", "pacing", "replay", "unattributed" }; @@ -380,7 +383,9 @@ static uint64_t ft_frame(uint64_t frame, uint64_t now, uint64_t d) { part[FT_C_TEX] = frame_ns[PERF_TEX]; part[FT_C_FENCE] = frame_ns[PERF_FENCE] + frame_ns[PERF_DRAIN]; part[FT_C_PACE] = frame_ns[PERF_PACE]; - part[FT_C_REPLAY] = (uint64_t)rep > 0 ? (uint64_t)rep : 0; + part[FT_C_REPLAY] = rep > 0 ? (uint64_t)rep : 0; + uint64_t inside = part[FT_C_PIPE] + part[FT_C_XLATE] + part[FT_C_TEX]; // waits on the replay thread + part[FT_C_REPLAY] = part[FT_C_REPLAY] > inside ? part[FT_C_REPLAY] - inside : 0; uint64_t known = 0; for (int c = 0; c < FT_C_OTHER; c++) known += part[c]; part[FT_C_OTHER] = d > known ? d - known : 0; diff --git a/runtime/vk_pipeline.c b/runtime/vk_pipeline.c index 2524c5b5..d4051baf 100644 --- a/runtime/vk_pipeline.c +++ b/runtime/vk_pipeline.c @@ -76,7 +76,7 @@ static struct { int indep; // stage-independent layouts (vk_caps.gpl): vertex set 0, fragment set 1 int push_stage; // GFX_STAGE_* whose set is pushed with independent layouts int gpl; // draws whose pipeline is building get a fast-linked one (vk_caps.gpl) - struct { uint64_t links, fb_links, wlinks, flinks, dlinks, pr_added, lib_slow[4], ready_n[6], ready_nofast, hints, hint_libs, requeued, link_fail, defer, lib_n[4], lib_fail, swaps, slow, pre_used[4]; double pr_added_ms, lib_slow_ms[4], ready_ms, ready_max, link_ms, link_max, lib_ms[4], lib_max[4], dlink_ms, dlink_max; } g; + struct { uint64_t links, fb_links, wlinks, flinks, dlinks, pr_added, lib_slow[4], ready_n[6], ready_nofast, ready_mesh, ready_recorded, ready_fresh, ready_logged, lib_logged, hints, hint_libs, requeued, link_fail, defer, lib_n[4], lib_fail, swaps, slow, pre_used[4]; double pr_added_ms, lib_slow_ms[4], ready_ms, ready_max, link_ms, link_max, lib_ms[4], lib_max[4], dlink_ms, dlink_max; } g; int vs_outs_all; // NX2NAVI_VS_OUTS: 0 union of paired fragment inputs, 1 (all) all 32 output locations int dyn_vi, dyn_clamp, dyn_poly, dyn_blend_en, dyn_blend_eq, dyn_wmask, dyn_a2c; int f_logic_op, f_depth_clamp, f_fill_non_solid, f_dual_src, f_full_subgroups; @@ -1905,6 +1905,12 @@ static VkShaderStageFlags stage_bit(int stage) { static int is_mesh(const VkPipeState *s) { return s->vs && gfx_shader_stage(s->vs) == GFX_STAGE_MS; } +// Where a shader's translation came from (gfx_shader_origin), for the log. +static const char *origin_name(const GfxShader *s) { + static const char *const name[3] = {"from the shader cache", "a previous translator's", "translated in this session"}; + return s ? name[gfx_shader_origin(s)] : "none"; +} + // The shaders of the pipeline this thread builds, for the crash notes (platform.h plat_crash_context): the first 8 // bytes of each cache key, as gfx_shader.c names its cache files. static void crash_note(const char *what, const GfxShader *a, const GfxShader *b, const GfxShader *c) { @@ -2820,8 +2826,21 @@ static VkPipeline lib_build(Lib *b, int kind, const VkPipeState *s, const GKey * plat_mutex_lock(&P.lock); P.g.lib_n[kind]++, P.g.lib_ms[kind] += ms; if (ms > P.g.lib_max[kind]) P.g.lib_max[kind] = ms; - if (kind == LIB_PR && ((const PrKey *)(const void *)b->key)->missing) P.g.pr_added++, P.g.pr_added_ms += ms; + int added = kind == LIB_PR && ((const PrKey *)(const void *)b->key)->missing; + if (added) P.g.pr_added++, P.g.pr_added_ms += ms; if (ms >= 25) P.g.lib_slow[kind]++, P.g.lib_slow_ms[kind] += ms; + // A shader library that took this long was not in the driver's cache; why not, for the first 32 of a run + if (ms >= 25 && (kind == LIB_PR || kind == LIB_FS) && P.g.lib_logged < 32) { + const GfxShader *sh = kind == LIB_PR ? s->vs : s->fs; + const uint8_t *id = sh ? gfx_shader_input_id(sh) : NULL; + P.g.lib_logged++; + rt_log("vk_pipeline: gpl: a %s library took %.0f ms: shader %02x%02x%02x%02x, %s%s", + kind == LIB_PR ? "pre-rasterization" : "fragment shader", ms, id ? id[0] : 0, id ? id[1] : 0, + id ? id[2] : 0, id ? id[3] : 0, origin_name(sh), + added ? "; outputs were added for its fragment shader, a module the preparation did not build" + : sh && gfx_shader_origin(sh) == 2 ? "; nothing was prepared from a shader first translated now" + : "; the driver's cache did not have it, or the build waited behind others in the driver"); + } } if (!pipe && ++P.g.lib_fail <= 8) rt_log("vk_pipeline: gpl: stage library %d not built", kind); b->pipe = pipe, b->building = 0; @@ -2909,18 +2928,6 @@ static void lib_keys(const VkPipeState *s, const GKey *k, ViKey *v, PrKey *pk, F memcpy(ok->color, k->color, sizeof ok->color), memcpy(ok->blend, k->blend, sizeof ok->blend); } -// p, which a draw missed, has its first usable pipeline: how long that took is how long its draws were skipped or -// waited. fast 0: it had no fast link and this is the optimised build. Caller holds P.lock. -static void ready_note(Pipe *p, int fast) { - if (!p->t_miss) return; - double ms = (double)(plat_now_ns() - p->t_miss) / 1e6; - p->t_miss = 0; - P.g.ready_n[ms < 4 ? 0 : ms < 8 ? 1 : ms < 17 ? 2 : ms < 50 ? 3 : ms < 150 ? 4 : 5]++; - P.g.ready_ms += ms; - if (ms > P.g.ready_max) P.g.ready_max = ms; - P.g.ready_nofast += !fast; -} - // NX2NAVI_GPL_FEEDBACK (default 1; 0 = as before): a pipeline whose fragment shader samples an attachment of its own pass // (VkPipeState.feedback: soft particles and water reading the depth buffer, decals, full-screen passes that read the // target they write) gets a fast link like any other. The feedback loop flags belong to the fragment output state, so @@ -3205,6 +3212,44 @@ static void keys_open(void) { static double now_ms(void) { return (double)plat_now_ns() / 1e6; } // Caller holds P.lock. +// p, which a draw missed, has its first usable pipeline: how long that took is how long its draws were skipped or +// waited. fast 0: it had no fast link and this is the optimised build. Caller holds P.lock. +// The summary (save_with) counts what a slow one can be: a mesh pipeline (no stage libraries, so its whole build), one +// the key file names whose prebuild had not come to it, one with a shader translated in this session (nothing of it +// was prepared, so its libraries compile cold). The first READY_LOGS that took READY_SLOW_MS or longer get a line +// each with those facts, so a log says which pipelines left their draws out of the picture and for which reason. +#define READY_SLOW_MS 50.0 +#define READY_LOGS 48 +static void ready_note(Pipe *p, int fast) { + if (!p->t_miss) return; + double ms = (double)(plat_now_ns() - p->t_miss) / 1e6; + p->t_miss = 0; + P.g.ready_n[ms < 4 ? 0 : ms < 8 ? 1 : ms < 17 ? 2 : ms < 50 ? 3 : ms < 150 ? 4 : 5]++; + P.g.ready_ms += ms; + if (ms > P.g.ready_max) P.g.ready_max = ms; + P.g.ready_nofast += !fast; + const Job *j = p->job; + if (!j) return; + int mesh = is_mesh(&j->s); + int fresh = gfx_shader_origin(j->s.vs) == 2 || (j->s.fs && gfx_shader_origin(j->s.fs) == 2) || + (j->s.ts && gfx_shader_origin(j->s.ts) == 2); + P.g.ready_mesh += (uint64_t)mesh, P.g.ready_recorded += j->prebuild, P.g.ready_fresh += (uint64_t)fresh; + if (ms < READY_SLOW_MS || P.g.ready_logged >= READY_LOGS) return; + P.g.ready_logged++; + const uint8_t *vi = gfx_shader_input_id(j->s.vs), *fi = j->s.fs ? gfx_shader_input_id(j->s.fs) : NULL; + char fid[16] = "none"; + if (fi) snprintf(fid, sizeof fid, "%02x%02x%02x%02x", fi[0], fi[1], fi[2], fi[3]); + rt_log("vk_pipeline: a pipeline a draw missed was usable after %.0f ms: a %s pipeline, %s; %s; %s shader " + "%02x%02x%02x%02x (%s), fragment shader %s (%s)%s%s", + ms, mesh ? "mesh" : "vertex", + fast ? "fast-linked, so the time went to its stage libraries (the lines \"a ... library took\" name the slow ones)" + : mesh ? "which has no stage libraries: this is its whole build" + : "which got no fast link: this is its whole build", + j->prebuild ? "pipeline-keys.bin names it and its prebuild had not come to it" : "not in pipeline-keys.bin before", + mesh ? "mesh" : "vertex", vi[0], vi[1], vi[2], vi[3], origin_name(j->s.vs), fid, origin_name(j->s.fs), + j->k.fbl ? "; feedback loop" : "", P.g.ready_logged == READY_LOGS ? " (the last such line of this run)" : ""); +} + static void prebuild_report(void) { rt_log("vk_pipeline: prebuild done: %u built, %u failed, %u asked for by a draw first; %.0f ms after the device was " "ready", K.built, K.failed, K.promoted, now_ms() - K.t0); @@ -3884,6 +3929,9 @@ static void prewarm_run(void) { __atomic_store_n(&pw_phase, 1, __ATOMIC_RELEASE); gfx_shader_prewarm((const uint8_t(*)[32])ids, u, sh, cpus, procs < 1 ? 1 : procs); double t1 = now_ms(); + size_t carried = 0, fresh = 0; // the recorded pipelines' shaders by where their translation came from + for (size_t i = 0; i < u; i++) + if (sh[i]) carried += gfx_shader_origin(sh[i]) == 1, fresh += gfx_shader_origin(sh[i]) == 2; vk_mem_stage_log("the shader prewarm"); if (P.indep) { // vertex output unions first: workers may take a job before the queue is complete @@ -3974,6 +4022,13 @@ static void prewarm_run(void) { free(K.file); K.file = NULL; plat_mutex_unlock(&P.lock); + // After an update with another translator: a record names its shaders by input id, which no translator changes, + // so every record still finds its shaders, and the pipeline cache gives the pipeline back wherever the translation + // in use is the one it was built from. The line below this one says how many it gave back. + if (carried || fresh) + rt_log("vk_pipeline: prewarm: of the %zu shaders the recorded pipelines name, %zu are a previous translator's " + "translations and %zu were translated now; a recorded pipeline is compiled again only where its shaders' " + "translation changed, which the pipeline-cache.bin hits below show", u, carried, fresh); fb_log("after the boot prewarm"); vk_mem_stage_log("the recorded pipelines' prebuild"); vk_pipeline_prewarm_marker_clear(); // the boot's share is done; what the pool builds later runs during play @@ -5895,8 +5950,9 @@ static void save_with(uint32_t wait_ms) { if (P.ready && P.gpl) { rt_log("vk_pipeline: gpl: %llu fast links (%llu by workers, %llu of those by the library workers for skipped draws; " "%llu of pipelines with a feedback loop; avg %.2f ms, " - "max %.2f ms, %llu failed), %llu draws " - "lookups found shader libraries missing (left to a worker), %llu optimised pipelines swapped in (%llu queued " + "max %.2f ms, %llu failed), %llu lookups by a draw found a stage library not made in this process yet and " + "left it to a worker (whether the driver's cache has it shows in the library times below), %llu optimised " + "pipelines swapped in (%llu queued " "low after their fast link), %llu drawing-thread links over 5 ms; %llu waiting draws built their libraries " "(avg %.2f ms, max %.2f ms)", (unsigned long long)P.g.links, (unsigned long long)P.g.wlinks, (unsigned long long)P.g.flinks, @@ -5907,10 +5963,14 @@ static void save_with(uint32_t wait_ms) { (unsigned long long)P.g.dlinks, P.g.dlinks ? P.g.dlink_ms / P.g.dlinks : 0.0, P.g.dlink_max); uint64_t rn = P.g.ready_n[0] + P.g.ready_n[1] + P.g.ready_n[2] + P.g.ready_n[3] + P.g.ready_n[4] + P.g.ready_n[5]; rt_log("vk_pipeline: gpl: pipelines a draw missed were usable after under 4 ms %llu, 4-8 ms %llu, 8-17 ms %llu, " - "17-50 ms %llu, 50-150 ms %llu, longer %llu (avg %.1f ms, longest %.0f ms); %llu of them had no fast link", + "17-50 ms %llu, 50-150 ms %llu, longer %llu (avg %.1f ms, longest %.0f ms); %llu of them had no fast link; " + "%llu were mesh pipelines (no stage libraries), %llu were named in pipeline-keys.bin and not prebuilt yet " + "(the others were new to this install), %llu had a shader translated in this session", (unsigned long long)P.g.ready_n[0], (unsigned long long)P.g.ready_n[1], (unsigned long long)P.g.ready_n[2], (unsigned long long)P.g.ready_n[3], (unsigned long long)P.g.ready_n[4], (unsigned long long)P.g.ready_n[5], - rn ? P.g.ready_ms / (double)rn : 0.0, P.g.ready_max, (unsigned long long)P.g.ready_nofast); + rn ? P.g.ready_ms / (double)rn : 0.0, P.g.ready_max, (unsigned long long)P.g.ready_nofast, + (unsigned long long)P.g.ready_mesh, (unsigned long long)P.g.ready_recorded, + (unsigned long long)P.g.ready_fresh); if (P.g.hints) rt_log("vk_pipeline: gpl: looking ahead named %llu pipeline states not known yet and queued %llu stage libraries " "for them", (unsigned long long)P.g.hints, (unsigned long long)P.g.hint_libs); From e9ec6de6bc70f8ea8c43070be2feade7d1f5bfa4 Mon Sep 17 00:00:00 2001 From: chrg6 Date: Tue, 10 Nov 2026 12:14:29 -0500 Subject: [PATCH 4/4] A render target's frame count follows its storage, not the guest's texture object, and a mesh draw no longer waits for a pipeline that is known to be compiling. Found in the first run of the previous two commits (a first visit of Kokiri Forest from a state that had only seen Hyrule Field): - The count of frames a target was rendered in started again whenever the guest's texture object changed, and the game initializes its render target objects anew every frame at the same storage: the log showed `seen 2129/2128 in 1 frames`, so no target ever became steady and the rule did nothing. The owner of an image is now the storage address the texture cache keys it by. An image handed on to a texture elsewhere still starts at none. - A one-shot mesh draw waited its 8 ms in every frame that met the same pipeline while it compiled for 150 to 1100 ms: 17 waits, 170 ms, every draw skipped at the end, up to four in one frame. A mesh pipeline that has been building for longer than that wait is a compile, and the draws after the first do not wait for it (vk_pipeline_building_ms). - The last wait of a full-screen draw with no picture to hold was 40 ms a draw, and two such draws made 86 ms in one frame. It is 40 ms a frame now. - A one-shot vertex draw skipped at the end of its wait gets its log line however short the wait was (seven were skipped with no line, so their targets could not be named). That run against the same cell of the earlier comparison (same exe path, cache state, route and settings; one run each): the load's worst wait of the draws in one frame 99 ms against 735 ms, no mesh wait over 13 ms against 524 and 206 ms. Two long frames remain in both runs (628 and 862 ms here, 660 and 854 ms there): two compute pipelines built at their first dispatch, about 395 and 397 ms. Co-Authored-By: Claude Fable 5.2 --- runtime/vk.h | 3 +++ runtime/vk_exec.c | 4 ++-- runtime/vk_exec_draw.c | 46 +++++++++++++++++++++++++------------ runtime/vk_pipeline.c | 12 ++++++++++ runtime/vk_target_frames.h | 26 +++++++++++---------- tests/target_frames_check.c | 7 +++--- 6 files changed, 66 insertions(+), 32 deletions(-) diff --git a/runtime/vk.h b/runtime/vk.h index 8a1face5..423ed31a 100644 --- a/runtime/vk.h +++ b/runtime/vk.h @@ -714,6 +714,9 @@ VkPipeline vk_pipeline_graphics(const VkPipeState *s, const VkPipeInfo **info); // wait_ms > 0 waits up to that long for a pending pipeline first. Pipelines built in a run are recorded to // pipeline-keys.bin next to the pipeline cache and prebuilt at the next boot (NX2NAVI_PIPE_PREBUILD=0: not). VkPipeline vk_pipeline_graphics_async(const VkPipeState *s, const VkPipeInfo **info, int *pending, uint32_t wait_ms); +// How long the pipeline of s has been building since a draw first missed it, in milliseconds; 0 when it is unknown, +// usable, or no draw has missed it. +double vk_pipeline_building_ms(const VkPipeState *s); // The stage libraries of a draw the replay has not reached yet, queued to the workers when no pipeline is known for its // state (vk_pipeline.c "stage libraries ahead of their draws"). Returns the libraries queued. int vk_pipeline_hint(const VkPipeState *s); diff --git a/runtime/vk_exec.c b/runtime/vk_exec.c index 2d7e94e4..1d8866d0 100644 --- a/runtime/vk_exec.c +++ b/runtime/vk_exec.c @@ -482,7 +482,7 @@ static void free_views(void *arg) { // completion thread, once the submission uint64_t vk_exec_steady_passes; // lookups a steady target alone made per-frame (vk_exec_targets_recent) static TfrTarget tfr_of(const VkExecTarget *t) { - return (TfrTarget){(uint64_t)(uintptr_t)t->image, t->level, t->base_layer, t->tex ? t->tex->serial : 0}; + return (TfrTarget){(uint64_t)(uintptr_t)t->image, t->level, t->base_layer, t->img ? t->img->addr : 0}; } static int steady_on(void) { @@ -522,7 +522,7 @@ void vk_exec_targets_describe(VkExec *ex, char *buf, size_t cap) { int w = snprintf(buf + n, cap - n, "%s%c%d fmt %d %ux%u L%u seen %llu/%llu in %u frames", n ? "; " : "", i < 8 ? 'c' : 'd', i < 8 ? i : 0, (int)t->format, t->width, t->height, t->base_layer, (unsigned long long)(e ? e->last : 0), (unsigned long long)(e ? e->prev : 0), - e && e->serial == ft.serial ? e->n : 0); + e && e->owner == ft.owner ? e->n : 0); if (w < 0) break; n += (size_t)w; } diff --git a/runtime/vk_exec_draw.c b/runtime/vk_exec_draw.c index 683ec91a..c412607b 100644 --- a/runtime/vk_exec_draw.c +++ b/runtime/vk_exec_draw.c @@ -68,7 +68,10 @@ static uint32_t oneshot_wait_ms(void) { } #define ONESHOT_WAIT_MS oneshot_wait_ms() // A mesh pipeline has no stage libraries and so no fast link: the wait is for its whole build, a few milliseconds -// when the pipeline cache holds it and 200 ms or more when it does not. A longer wait only finds out which. +// when the pipeline cache holds it and 200 ms or more when it does not. A longer wait only finds out which, and one +// that has been building for longer than this is a compile: the draws that meet it in the frames after wait for +// nothing (oneshot_ms). Before that rule a first visit waited 8 to 13 ms for each of 17 mesh draws over nine frames, +// up to four in one frame, and every one of them was skipped at the end. #define MESH_WAIT_MS 8u static int oneshot_budget_on(void) { static int on = -1; @@ -3370,10 +3373,12 @@ static void pool_charge(uint64_t *pool, uint64_t t0) { *pool = spent < *pool ? *pool - spent : 0; } -// What a one-shot draw may wait for its pipeline from pool. -static uint32_t oneshot_ms(const uint64_t *pool, int mesh) { +// What a one-shot draw with the state s may wait for its pipeline from pool. +static uint32_t oneshot_ms(const uint64_t *pool, int mesh, const VkPipeState *s) { uint32_t ms = (uint32_t)(*pool / 1000000); - return mesh && ms > MESH_WAIT_MS ? MESH_WAIT_MS : ms; + if (!mesh || !ms) return ms; + if (vk_pipeline_building_ms(s) > MESH_WAIT_MS) return 0; // a compile (MESH_WAIT_MS) + return ms > MESH_WAIT_MS ? MESH_WAIT_MS : ms; } // ---- wait accounting: what the replay thread spent on draws that found their pipeline or a shader missing @@ -3403,12 +3408,19 @@ static uint32_t link_draw_ms(void) { return (uint32_t)ms; } #define LINK_DRAW_MS link_draw_ms() -// How long a full-screen draw waits on for its pipeline once the frame's wait pool is spent and the previous picture -// cannot be held any longer (prepare_draw_ex). Every such pipeline has a fast link (vk_pipeline.c gpl_feedback), so -// the wait is for stage libraries: a few milliseconds from the driver's cache, and in the one case a player's session -// shows the draw had its pipeline 18 ms into this wait. It was 500 ms while a pipeline with a feedback loop waited -// for its whole optimised build; with the pool before it, a frame now waits under 100 ms for pipelines in all. +// How long the full-screen draws of a frame wait on for their pipelines, in all, once the frame's wait pool is spent +// and the previous picture cannot be held any longer (prepare_draw_ex, last_wait). Every such pipeline has a fast link +// (vk_pipeline.c gpl_feedback), so the wait is for stage libraries: a few milliseconds from the driver's cache, and +// in the one case a player's session shows the draw had its pipeline 18 ms into this wait. It was 500 ms for each +// draw while a pipeline with a feedback loop waited for its whole optimised build; with the pool before it, a frame +// now waits under 100 ms for pipelines in all. #define FULLSCREEN_LAST_MS 40u +static uint64_t *last_wait(void) { + static uint64_t frame = ~0ull, left; + uint64_t f = __atomic_load_n(&gfx_counters.frames, __ATOMIC_RELAXED); + if (f != frame) frame = f, left = FULLSCREEN_LAST_MS * 1000000ull; + return &left; +} static uint64_t *link_budget(void) { static uint64_t frame = ~0ull, left, per_frame = ~0ull; if (per_frame == ~0ull) { @@ -3695,7 +3707,7 @@ static int prepare_draw_ex(VkExec *ex, int prim, int32_t count, int restart, int int fullscreen = 0; // the wait is a full-screen draw's if (oneshot > 0 && oneshot_budget_on()) { budget = frame_wait(); - wait_ms = oneshot_ms(budget, mesh); + wait_ms = oneshot_ms(budget, mesh, &s); if (wait_ms) t0 = kern_now_ns(); } else if (oneshot > 0) { wait_ms = ONESHOT_WAIT_MS; @@ -3745,7 +3757,7 @@ static int prepare_draw_ex(VkExec *ex, int prim, int32_t count, int restart, int if (!w0) w0 = kern_now_ns(); if (oneshot_budget_on()) { uint64_t *pool = frame_wait(), b0 = kern_now_ns(); - p = vk_pipeline_graphics_async(&s, &info, &pending, oneshot_ms(pool, mesh)); + p = vk_pipeline_graphics_async(&s, &info, &pending, oneshot_ms(pool, mesh, &s)); pool_charge(pool, b0); } else { p = vk_pipeline_graphics_async(&s, &info, &pending, ONESHOT_WAIT_MS); @@ -3753,12 +3765,15 @@ static int prepare_draw_ex(VkExec *ex, int prim, int32_t count, int restart, int } // A full-screen draw still pending after its budget: skipped, it leaves the frame black or unlit. While the // previous picture can stay on screen in its place (vk_frame_hold_left) that is what happens; once that time - // is used up the draw waits on, FULLSCREEN_LAST_MS at most, before such a frame is shown. - if (pending && fullscreen && !vk_frame_hold_left()) { + // is used up the draw waits on, from the FULLSCREEN_LAST_MS of its frame, before such a frame is shown. + uint64_t *last = pending && fullscreen && !vk_frame_hold_left() ? last_wait() : NULL; + if (last && *last >= 1000000) { why = "full-screen, nothing left to hold the picture"; kind = WK_LAST; if (!w0) w0 = kern_now_ns(); - p = vk_pipeline_graphics_async(&s, &info, &pending, FULLSCREEN_LAST_MS); + uint64_t b0 = kern_now_ns(); + p = vk_pipeline_graphics_async(&s, &info, &pending, (uint32_t)(*last / 1000000)); + pool_charge(last, b0); } if (m0) wait_note(kind, m0, pending), draw_stats_tick(); PM.valid = !pending && p && info; @@ -3766,7 +3781,8 @@ static int prepare_draw_ex(VkExec *ex, int prim, int32_t count, int restart, int } uint64_t waited = w0 ? kern_now_ns() - w0 : 0; static int wait_logs; - if (why && waited >= 2000000 && wait_logs < 200) { + // also a one-shot draw skipped at the end of its wait, however short: its target may not be drawn again + if (why && (waited >= 2000000 || (pending && kind == WK_ONESHOT)) && wait_logs < 200) { char tg[512]; wait_logs++; vk_exec_targets_describe(ex, tg, sizeof tg); diff --git a/runtime/vk_pipeline.c b/runtime/vk_pipeline.c index d4051baf..327b128a 100644 --- a/runtime/vk_pipeline.c +++ b/runtime/vk_pipeline.c @@ -3819,6 +3819,18 @@ VkPipeline vk_pipeline_graphics_async(const VkPipeState *s, const VkPipeInfo **i return graphics(s, info, A.on, pending, wait_ms); } +double vk_pipeline_building_ms(const VkPipeState *s) { + if (!s->vs || !P.ready) return 0; + GKey k; + gkey_fill(s, &k); + uint64_t h = key_hash(&k, sizeof k); + plat_mutex_lock(&P.lock); + const Pipe *p = pipe_find(&k, sizeof k, h); + double ms = p && p->building && !p->info.pipeline && p->t_miss ? (double)(plat_now_ns() - p->t_miss) / 1e6 : 0; + plat_mutex_unlock(&P.lock); + return ms; +} + // ---- boot prewarm from the key file (comment at "pipeline keys") static int id_cmp(const void *x, const void *y) { return memcmp(x, y, 32); } diff --git a/runtime/vk_target_frames.h b/runtime/vk_target_frames.h index 40aa8249..d2fece2f 100644 --- a/runtime/vk_target_frames.h +++ b/runtime/vk_target_frames.h @@ -18,12 +18,14 @@ // frames before, so each draw with a new pipeline waited on the replay thread, and a mesh draw, whose pipeline has no // fast link, for its whole compile. One player's session shows it three times at area changes: frames of 954, 1015 // and 684 ms, six mesh draws waiting 52 to 660 ms each in passes whose targets had been rendered in thousands of -// frames. An entry therefore also counts the frames its subresource was rendered in, for as long as it is the same -// guest texture (its initialization serial: an image taken over by another texture starts again at none). From -// TFR_STEADY frames on the target is one the game draws again and again, and its draws are skipped while a pipeline -// builds, as in any other frame, however long ago the last one was. A target generated once never gets there; one -// generated again at every area load would after TFR_STEADY loads, by when its pipelines are recorded and prebuilt -// at the start. +// frames. An entry therefore also counts the frames its subresource was rendered in, for as long as the image is the +// texture at the same storage (its owner, the storage GPU address the texture cache keys the image by: an image +// handed on to a texture elsewhere starts again at none). The guest's texture object does not serve as the owner: +// the game initializes its render target objects anew every frame at the same storage, so a count kept per object +// never passed one. From TFR_STEADY frames on the target is one the game draws again and again, and its draws are +// skipped while a pipeline builds, as in any other frame, however long ago the last one was. A target generated once +// never gets there; one generated again at every area load would after TFR_STEADY loads, by when its pipelines are +// recorded and prebuilt at the start. #pragma once #include @@ -33,9 +35,9 @@ #define TFR_RECENT 4 #define TFR_STEADY 8 -// One render target of a pass: the image handle as a number, the subresource, the guest texture's serial (0 unknown). -typedef struct { uint64_t image; uint32_t level, layer; uint64_t serial; } TfrTarget; -typedef struct { uint64_t image; uint32_t level, layer, n; uint64_t last, prev, serial; } TargetFrames; +// One render target of a pass: the image handle as a number, the subresource, the image's owner (0 unknown). +typedef struct { uint64_t image; uint32_t level, layer; uint64_t owner; } TfrTarget; +typedef struct { uint64_t image; uint32_t level, layer, n; uint64_t last, prev, owner; } TargetFrames; static TargetFrames tfr[TFR_SETS * TFR_WAYS]; // The target's entry, or with make the entry it takes (the set's oldest, an empty one first); NULL without make @@ -55,7 +57,7 @@ static inline TargetFrames *tfr_find(const TfrTarget *t, int make) { // The target is rendered in frame stamp. static inline void tfr_note(const TfrTarget *t, uint64_t stamp) { TargetFrames *e = tfr_find(t, 1); - if (e->serial != t->serial) e->serial = t->serial, e->n = 0; // another texture has the image now: its own count + if (e->owner != t->owner) e->owner = t->owner, e->n = 0; // another texture has the image now: its own count if (e->last != stamp) e->prev = e->last, e->last = stamp, e->n += e->n < UINT32_MAX; } @@ -73,10 +75,10 @@ static inline int tfr_recent(const TfrTarget *t, uint64_t stamp) { // No frame before stamp rendered the target. static inline int tfr_first(const TfrTarget *t, uint64_t stamp) { return !tfr_before(t, stamp); } -// TFR_STEADY frames or more before stamp rendered the target as the texture it is now. +// TFR_STEADY frames or more before stamp rendered the target under the owner it has now. static inline int tfr_steady(const TfrTarget *t, uint64_t stamp) { const TargetFrames *e = tfr_find(t, 0); - return e && e->serial == t->serial && e->n - (e->last == stamp) >= TFR_STEADY; + return e && e->owner == t->owner && e->n - (e->last == stamp) >= TFR_STEADY; } // Whether the pass with the n_color colour targets c and the depth target d (NULL: none) is one the game renders diff --git a/tests/target_frames_check.c b/tests/target_frames_check.c index 0dd5b81e..f20ccc6b 100644 --- a/tests/target_frames_check.c +++ b/tests/target_frames_check.c @@ -11,8 +11,8 @@ static int fails; #define CHECK(c, ...) do { if (!(c)) { fails++; printf("FAIL line %d: ", __LINE__); printf(__VA_ARGS__); printf("\n"); } } while (0) -static TfrTarget T(uint64_t image, uint32_t level, uint32_t layer, uint64_t serial) { - return (TfrTarget){image, level, layer, serial}; +static TfrTarget T(uint64_t image, uint32_t level, uint32_t layer, uint64_t owner) { + return (TfrTarget){image, level, layer, owner}; } // The verdict with the steady rule and without it; *by: only the steady rule made it per-frame. @@ -84,7 +84,8 @@ int main(void) { tfr_note(&scene_d, f2), tfr_note(&icon, f2); CHECK(pass(&icon, 1, &scene_d, f2, 1, 1, NULL) == 0, "an icon's first capture is one-shot though its depth buffer is recent"); CHECK(pass(&icon, 1, &scene_d, f2, 1, 0, NULL) == 0, "and was before the rule"); - // an image taken over by another texture starts again: the per-frame history of its handle is not the new texture's + // an image handed on to a texture at other storage starts again: the per-frame history of its handle is not the + // new texture's TfrTarget reused = T(scene.image, 0, 0, 500); CHECK(tfr_steady(&scene, f2 + 300) == 1, "the scene target is steady"); CHECK(pass(&reused, 1, NULL, f2 + 300, 1, 1, NULL) == 0, "the same image as another texture is not steady");