diff --git a/runtime/vk.h b/runtime/vk.h index 8a1face5..423ed31a 100644 --- a/runtime/vk.h +++ b/runtime/vk.h @@ -714,6 +714,9 @@ VkPipeline vk_pipeline_graphics(const VkPipeState *s, const VkPipeInfo **info); // wait_ms > 0 waits up to that long for a pending pipeline first. Pipelines built in a run are recorded to // pipeline-keys.bin next to the pipeline cache and prebuilt at the next boot (NX2NAVI_PIPE_PREBUILD=0: not). VkPipeline vk_pipeline_graphics_async(const VkPipeState *s, const VkPipeInfo **info, int *pending, uint32_t wait_ms); +// How long the pipeline of s has been building since a draw first missed it, in milliseconds; 0 when it is unknown, +// usable, or no draw has missed it. +double vk_pipeline_building_ms(const VkPipeState *s); // The stage libraries of a draw the replay has not reached yet, queued to the workers when no pipeline is known for its // state (vk_pipeline.c "stage libraries ahead of their draws"). Returns the libraries queued. int vk_pipeline_hint(const VkPipeState *s); diff --git a/runtime/vk_exec.c b/runtime/vk_exec.c index 2d7e94e4..1d8866d0 100644 --- a/runtime/vk_exec.c +++ b/runtime/vk_exec.c @@ -482,7 +482,7 @@ static void free_views(void *arg) { // completion thread, once the submission uint64_t vk_exec_steady_passes; // lookups a steady target alone made per-frame (vk_exec_targets_recent) static TfrTarget tfr_of(const VkExecTarget *t) { - return (TfrTarget){(uint64_t)(uintptr_t)t->image, t->level, t->base_layer, t->tex ? t->tex->serial : 0}; + return (TfrTarget){(uint64_t)(uintptr_t)t->image, t->level, t->base_layer, t->img ? t->img->addr : 0}; } static int steady_on(void) { @@ -522,7 +522,7 @@ void vk_exec_targets_describe(VkExec *ex, char *buf, size_t cap) { int w = snprintf(buf + n, cap - n, "%s%c%d fmt %d %ux%u L%u seen %llu/%llu in %u frames", n ? "; " : "", i < 8 ? 'c' : 'd', i < 8 ? i : 0, (int)t->format, t->width, t->height, t->base_layer, (unsigned long long)(e ? e->last : 0), (unsigned long long)(e ? e->prev : 0), - e && e->serial == ft.serial ? e->n : 0); + e && e->owner == ft.owner ? e->n : 0); if (w < 0) break; n += (size_t)w; } diff --git a/runtime/vk_exec_draw.c b/runtime/vk_exec_draw.c index 683ec91a..c412607b 100644 --- a/runtime/vk_exec_draw.c +++ b/runtime/vk_exec_draw.c @@ -68,7 +68,10 @@ static uint32_t oneshot_wait_ms(void) { } #define ONESHOT_WAIT_MS oneshot_wait_ms() // A mesh pipeline has no stage libraries and so no fast link: the wait is for its whole build, a few milliseconds -// when the pipeline cache holds it and 200 ms or more when it does not. A longer wait only finds out which. +// when the pipeline cache holds it and 200 ms or more when it does not. A longer wait only finds out which, and one +// that has been building for longer than this is a compile: the draws that meet it in the frames after wait for +// nothing (oneshot_ms). Before that rule a first visit waited 8 to 13 ms for each of 17 mesh draws over nine frames, +// up to four in one frame, and every one of them was skipped at the end. #define MESH_WAIT_MS 8u static int oneshot_budget_on(void) { static int on = -1; @@ -3370,10 +3373,12 @@ static void pool_charge(uint64_t *pool, uint64_t t0) { *pool = spent < *pool ? *pool - spent : 0; } -// What a one-shot draw may wait for its pipeline from pool. -static uint32_t oneshot_ms(const uint64_t *pool, int mesh) { +// What a one-shot draw with the state s may wait for its pipeline from pool. +static uint32_t oneshot_ms(const uint64_t *pool, int mesh, const VkPipeState *s) { uint32_t ms = (uint32_t)(*pool / 1000000); - return mesh && ms > MESH_WAIT_MS ? MESH_WAIT_MS : ms; + if (!mesh || !ms) return ms; + if (vk_pipeline_building_ms(s) > MESH_WAIT_MS) return 0; // a compile (MESH_WAIT_MS) + return ms > MESH_WAIT_MS ? MESH_WAIT_MS : ms; } // ---- wait accounting: what the replay thread spent on draws that found their pipeline or a shader missing @@ -3403,12 +3408,19 @@ static uint32_t link_draw_ms(void) { return (uint32_t)ms; } #define LINK_DRAW_MS link_draw_ms() -// How long a full-screen draw waits on for its pipeline once the frame's wait pool is spent and the previous picture -// cannot be held any longer (prepare_draw_ex). Every such pipeline has a fast link (vk_pipeline.c gpl_feedback), so -// the wait is for stage libraries: a few milliseconds from the driver's cache, and in the one case a player's session -// shows the draw had its pipeline 18 ms into this wait. It was 500 ms while a pipeline with a feedback loop waited -// for its whole optimised build; with the pool before it, a frame now waits under 100 ms for pipelines in all. +// How long the full-screen draws of a frame wait on for their pipelines, in all, once the frame's wait pool is spent +// and the previous picture cannot be held any longer (prepare_draw_ex, last_wait). Every such pipeline has a fast link +// (vk_pipeline.c gpl_feedback), so the wait is for stage libraries: a few milliseconds from the driver's cache, and +// in the one case a player's session shows the draw had its pipeline 18 ms into this wait. It was 500 ms for each +// draw while a pipeline with a feedback loop waited for its whole optimised build; with the pool before it, a frame +// now waits under 100 ms for pipelines in all. #define FULLSCREEN_LAST_MS 40u +static uint64_t *last_wait(void) { + static uint64_t frame = ~0ull, left; + uint64_t f = __atomic_load_n(&gfx_counters.frames, __ATOMIC_RELAXED); + if (f != frame) frame = f, left = FULLSCREEN_LAST_MS * 1000000ull; + return &left; +} static uint64_t *link_budget(void) { static uint64_t frame = ~0ull, left, per_frame = ~0ull; if (per_frame == ~0ull) { @@ -3695,7 +3707,7 @@ static int prepare_draw_ex(VkExec *ex, int prim, int32_t count, int restart, int int fullscreen = 0; // the wait is a full-screen draw's if (oneshot > 0 && oneshot_budget_on()) { budget = frame_wait(); - wait_ms = oneshot_ms(budget, mesh); + wait_ms = oneshot_ms(budget, mesh, &s); if (wait_ms) t0 = kern_now_ns(); } else if (oneshot > 0) { wait_ms = ONESHOT_WAIT_MS; @@ -3745,7 +3757,7 @@ static int prepare_draw_ex(VkExec *ex, int prim, int32_t count, int restart, int if (!w0) w0 = kern_now_ns(); if (oneshot_budget_on()) { uint64_t *pool = frame_wait(), b0 = kern_now_ns(); - p = vk_pipeline_graphics_async(&s, &info, &pending, oneshot_ms(pool, mesh)); + p = vk_pipeline_graphics_async(&s, &info, &pending, oneshot_ms(pool, mesh, &s)); pool_charge(pool, b0); } else { p = vk_pipeline_graphics_async(&s, &info, &pending, ONESHOT_WAIT_MS); @@ -3753,12 +3765,15 @@ static int prepare_draw_ex(VkExec *ex, int prim, int32_t count, int restart, int } // A full-screen draw still pending after its budget: skipped, it leaves the frame black or unlit. While the // previous picture can stay on screen in its place (vk_frame_hold_left) that is what happens; once that time - // is used up the draw waits on, FULLSCREEN_LAST_MS at most, before such a frame is shown. - if (pending && fullscreen && !vk_frame_hold_left()) { + // is used up the draw waits on, from the FULLSCREEN_LAST_MS of its frame, before such a frame is shown. + uint64_t *last = pending && fullscreen && !vk_frame_hold_left() ? last_wait() : NULL; + if (last && *last >= 1000000) { why = "full-screen, nothing left to hold the picture"; kind = WK_LAST; if (!w0) w0 = kern_now_ns(); - p = vk_pipeline_graphics_async(&s, &info, &pending, FULLSCREEN_LAST_MS); + uint64_t b0 = kern_now_ns(); + p = vk_pipeline_graphics_async(&s, &info, &pending, (uint32_t)(*last / 1000000)); + pool_charge(last, b0); } if (m0) wait_note(kind, m0, pending), draw_stats_tick(); PM.valid = !pending && p && info; @@ -3766,7 +3781,8 @@ static int prepare_draw_ex(VkExec *ex, int prim, int32_t count, int restart, int } uint64_t waited = w0 ? kern_now_ns() - w0 : 0; static int wait_logs; - if (why && waited >= 2000000 && wait_logs < 200) { + // also a one-shot draw skipped at the end of its wait, however short: its target may not be drawn again + if (why && (waited >= 2000000 || (pending && kind == WK_ONESHOT)) && wait_logs < 200) { char tg[512]; wait_logs++; vk_exec_targets_describe(ex, tg, sizeof tg); diff --git a/runtime/vk_pipeline.c b/runtime/vk_pipeline.c index d4051baf..327b128a 100644 --- a/runtime/vk_pipeline.c +++ b/runtime/vk_pipeline.c @@ -3819,6 +3819,18 @@ VkPipeline vk_pipeline_graphics_async(const VkPipeState *s, const VkPipeInfo **i return graphics(s, info, A.on, pending, wait_ms); } +double vk_pipeline_building_ms(const VkPipeState *s) { + if (!s->vs || !P.ready) return 0; + GKey k; + gkey_fill(s, &k); + uint64_t h = key_hash(&k, sizeof k); + plat_mutex_lock(&P.lock); + const Pipe *p = pipe_find(&k, sizeof k, h); + double ms = p && p->building && !p->info.pipeline && p->t_miss ? (double)(plat_now_ns() - p->t_miss) / 1e6 : 0; + plat_mutex_unlock(&P.lock); + return ms; +} + // ---- boot prewarm from the key file (comment at "pipeline keys") static int id_cmp(const void *x, const void *y) { return memcmp(x, y, 32); } diff --git a/runtime/vk_target_frames.h b/runtime/vk_target_frames.h index 40aa8249..d2fece2f 100644 --- a/runtime/vk_target_frames.h +++ b/runtime/vk_target_frames.h @@ -18,12 +18,14 @@ // frames before, so each draw with a new pipeline waited on the replay thread, and a mesh draw, whose pipeline has no // fast link, for its whole compile. One player's session shows it three times at area changes: frames of 954, 1015 // and 684 ms, six mesh draws waiting 52 to 660 ms each in passes whose targets had been rendered in thousands of -// frames. An entry therefore also counts the frames its subresource was rendered in, for as long as it is the same -// guest texture (its initialization serial: an image taken over by another texture starts again at none). From -// TFR_STEADY frames on the target is one the game draws again and again, and its draws are skipped while a pipeline -// builds, as in any other frame, however long ago the last one was. A target generated once never gets there; one -// generated again at every area load would after TFR_STEADY loads, by when its pipelines are recorded and prebuilt -// at the start. +// frames. An entry therefore also counts the frames its subresource was rendered in, for as long as the image is the +// texture at the same storage (its owner, the storage GPU address the texture cache keys the image by: an image +// handed on to a texture elsewhere starts again at none). The guest's texture object does not serve as the owner: +// the game initializes its render target objects anew every frame at the same storage, so a count kept per object +// never passed one. From TFR_STEADY frames on the target is one the game draws again and again, and its draws are +// skipped while a pipeline builds, as in any other frame, however long ago the last one was. A target generated once +// never gets there; one generated again at every area load would after TFR_STEADY loads, by when its pipelines are +// recorded and prebuilt at the start. #pragma once #include @@ -33,9 +35,9 @@ #define TFR_RECENT 4 #define TFR_STEADY 8 -// One render target of a pass: the image handle as a number, the subresource, the guest texture's serial (0 unknown). -typedef struct { uint64_t image; uint32_t level, layer; uint64_t serial; } TfrTarget; -typedef struct { uint64_t image; uint32_t level, layer, n; uint64_t last, prev, serial; } TargetFrames; +// One render target of a pass: the image handle as a number, the subresource, the image's owner (0 unknown). +typedef struct { uint64_t image; uint32_t level, layer; uint64_t owner; } TfrTarget; +typedef struct { uint64_t image; uint32_t level, layer, n; uint64_t last, prev, owner; } TargetFrames; static TargetFrames tfr[TFR_SETS * TFR_WAYS]; // The target's entry, or with make the entry it takes (the set's oldest, an empty one first); NULL without make @@ -55,7 +57,7 @@ static inline TargetFrames *tfr_find(const TfrTarget *t, int make) { // The target is rendered in frame stamp. static inline void tfr_note(const TfrTarget *t, uint64_t stamp) { TargetFrames *e = tfr_find(t, 1); - if (e->serial != t->serial) e->serial = t->serial, e->n = 0; // another texture has the image now: its own count + if (e->owner != t->owner) e->owner = t->owner, e->n = 0; // another texture has the image now: its own count if (e->last != stamp) e->prev = e->last, e->last = stamp, e->n += e->n < UINT32_MAX; } @@ -73,10 +75,10 @@ static inline int tfr_recent(const TfrTarget *t, uint64_t stamp) { // No frame before stamp rendered the target. static inline int tfr_first(const TfrTarget *t, uint64_t stamp) { return !tfr_before(t, stamp); } -// TFR_STEADY frames or more before stamp rendered the target as the texture it is now. +// TFR_STEADY frames or more before stamp rendered the target under the owner it has now. static inline int tfr_steady(const TfrTarget *t, uint64_t stamp) { const TargetFrames *e = tfr_find(t, 0); - return e && e->serial == t->serial && e->n - (e->last == stamp) >= TFR_STEADY; + return e && e->owner == t->owner && e->n - (e->last == stamp) >= TFR_STEADY; } // Whether the pass with the n_color colour targets c and the depth target d (NULL: none) is one the game renders diff --git a/tests/target_frames_check.c b/tests/target_frames_check.c index 0dd5b81e..f20ccc6b 100644 --- a/tests/target_frames_check.c +++ b/tests/target_frames_check.c @@ -11,8 +11,8 @@ static int fails; #define CHECK(c, ...) do { if (!(c)) { fails++; printf("FAIL line %d: ", __LINE__); printf(__VA_ARGS__); printf("\n"); } } while (0) -static TfrTarget T(uint64_t image, uint32_t level, uint32_t layer, uint64_t serial) { - return (TfrTarget){image, level, layer, serial}; +static TfrTarget T(uint64_t image, uint32_t level, uint32_t layer, uint64_t owner) { + return (TfrTarget){image, level, layer, owner}; } // The verdict with the steady rule and without it; *by: only the steady rule made it per-frame. @@ -84,7 +84,8 @@ int main(void) { tfr_note(&scene_d, f2), tfr_note(&icon, f2); CHECK(pass(&icon, 1, &scene_d, f2, 1, 1, NULL) == 0, "an icon's first capture is one-shot though its depth buffer is recent"); CHECK(pass(&icon, 1, &scene_d, f2, 1, 0, NULL) == 0, "and was before the rule"); - // an image taken over by another texture starts again: the per-frame history of its handle is not the new texture's + // an image handed on to a texture at other storage starts again: the per-frame history of its handle is not the + // new texture's TfrTarget reused = T(scene.image, 0, 0, 500); CHECK(tfr_steady(&scene, f2 + 300) == 1, "the scene target is steady"); CHECK(pass(&reused, 1, NULL, f2 + 300, 1, 1, NULL) == 0, "the same image as another texture is not steady");