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())