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 <noreply@anthropic.com>
This commit is contained in:
chrg6
2026-11-10 12:02:12 -05:00
parent 005d30a29c
commit bfcefe6fa2
12 changed files with 492 additions and 144 deletions

View File

@@ -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|<count>: the compute pipelines the boot keeps, those dispatched most recently (vk_pipeline.c, compute-used.bin)
{"COMPUTE_KEEP", "512", NULL, RESTART},

View File

@@ -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

View File

@@ -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; }

View File

@@ -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) {

View File

@@ -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

View File

@@ -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 = &late;
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;
}

106
runtime/vk_target_frames.h Normal file
View File

@@ -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 <stdint.h>
#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];
}