Speed up the replay and fix what the perf pass found, and add a frame capture key for chasing rendering faults.

Performance: draws reuse the bindings that did not change and record only the dynamic states that differ from what
the command buffer holds; small uploads come from a staging arena; finished submissions are held briefly and handed to
the queue in batches; the recorder thread can keep a CPU core of its own; ASTC block-linear textures are deswizzled on
the GPU; dynamic resolution steps the upscaler mode to hold a frame-time target. Each has a setting in nx2navi.ini
(bind_reuse, dyn_delta, stg_arena, submit_batch, recorder_core, recorder_prio, astc_gpu_deswizzle, dyn_res) and the
staging cache now frees what it holds at shutdown.

Capture: NX2NAVI_DUMP=key saves the frame after each press of Insert, with mesh draws listed by task, mesh and fragment
shader, depth test and face culling, and mesh_prims.txt giving each mesh draw's primitive, clipping and fragment counts.
All of it is off unless NX2NAVI_DUMP is set.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
chrg6
2026-11-09 01:35:47 -05:00
parent 86de3320a0
commit 1f7e9c975b
38 changed files with 3105 additions and 1840 deletions

View File

@@ -37,7 +37,9 @@
#include "vk_exec.h"
#include "flight.h"
#include "input.h"
uint64_t vk_exec_cs_storage_addr(const VkExec *ex, const GfxShaderRes *r); // vk_exec_draw.c
uint64_t vk_exec_storage_addr(const VkExec *ex, int st, const GfxShaderRes *r); // vk_exec_draw.c
int vk_exec_raw_9e5(VkImage img, VkFormat f); // vk_exec_draw.c
#include "platform/platform.h"
@@ -558,7 +560,10 @@ static void flush_luts(VkExec *ex, int n) {
nluts = 0;
}
static int nvn_stage(int gfx_stage) { return gfx_stage == GFX_STAGE_VS ? 0 : gfx_stage == GFX_STAGE_FS ? 1 : 5; }
static int nvn_stage(int gfx_stage) { // the NVN stage numbers VkExec's slot arrays use: 6 task, 7 mesh
return gfx_stage == GFX_STAGE_VS ? 0 : gfx_stage == GFX_STAGE_FS ? 1 : gfx_stage == GFX_STAGE_TS ? 6
: gfx_stage == GFX_STAGE_MS ? 7 : 5;
}
static const char *decl_type(uint16_t t, char tmp[24]) {
static const char *base[] = {"?", "1D", "buffer", "2D", "3D", "cube"};
@@ -580,7 +585,7 @@ static uint64_t handle_of(const VkExec *ex, int st, int slot, int img) {
// The textures, images and samplers of info's bindings; 3D textures are queued for a copy.
static void describe_bindings(Str *b, const VkExec *ex, const VkPipeInfo *info) {
static const char *stn[6] = {"vs", "fs", "?", "?", "?", "cs"};
static const char *stn[8] = {"vs", "fs", "?", "?", "?", "cs", "ts", "ms"};
for (uint32_t i = 0; i < info->n_bindings; i++) {
const VkPipeBinding *bd = &info->bindings[i];
const GfxShaderRes *r = bd->res;
@@ -593,8 +598,8 @@ static void describe_bindings(Str *b, const VkExec *ex, const VkPipeInfo *info)
memcpy(&pa, ex->cs_push + r->handle * 4 - 0x10, 8), memcpy(&ps, ex->cs_push + r->handle * 4 - 0x10 + 8, 4);
sp(b, " (push pointer %#llx+%#x)", (unsigned long long)pa, ps);
}
if (st == 5) { // the address and how far its backing span reaches (what a size-0 binding covers)
uint64_t ca = vk_exec_cs_storage_addr(ex, r);
if (st == 5 || st == 6 || st == 7) { // the address and how far its backing span reaches (what a size-0 binding covers)
uint64_t ca = vk_exec_storage_addr(ex, st, r);
VkMemSpan ms;
sp(b, " -> %#llx", (unsigned long long)ca);
if (ca && vk_mem_resolve(ca, 1, &ms)) sp(b, " (span %#llx)", (unsigned long long)ms.avail);
@@ -727,11 +732,111 @@ static uint64_t bindings_hash(const VkExec *ex, const VkPipeInfo *info) {
return h;
}
void vk_dump_draw(VkExec *ex, const VkPipeState *s, VkPipeline p, const VkPipeInfo *info) {
// ---- mesh primitive counts: with VkCtx.has_mesh_queries, each mesh draw of the dumped frame runs inside a
// VK_QUERY_TYPE_MESH_PRIMITIVES_GENERATED_EXT query, and mesh_prims.txt (written by vk_dump_present) lists what each
// draw's mesh shader emitted, with its pass, program and depth state.
// A pipeline statistics query runs alongside: task and mesh shader invocations, primitives the clipper got and gave
// out, and fragment shader invocations (a triangle with a NaN or degenerate vertex is emitted but never clipped).
enum { MQ_MAX = 1024, MQ_STATS = 5 };
static const VkQueryPipelineStatisticFlags mq_stat_bits =
VK_QUERY_PIPELINE_STATISTIC_TASK_SHADER_INVOCATIONS_BIT_EXT | VK_QUERY_PIPELINE_STATISTIC_MESH_SHADER_INVOCATIONS_BIT_EXT |
VK_QUERY_PIPELINE_STATISTIC_CLIPPING_INVOCATIONS_BIT | VK_QUERY_PIPELINE_STATISTIC_CLIPPING_PRIMITIVES_BIT |
VK_QUERY_PIPELINE_STATISTIC_FRAGMENT_SHADER_INVOCATIONS_BIT;
typedef struct { int pass; unsigned long long prog, ts, ms; unsigned dt, dw, df; } MqDraw;
static VkQueryPool mq_pool, mq_stats;
static int mq_failed;
static uint32_t mq_n; // queries begun in the dumped frame
static MqDraw mq_draw[MQ_MAX], mq_next; // mq_next: the mesh draw vk_dump_draw saw last
int vk_dump_mesh_begin(VkExec *ex) {
VkCtx *c = vk_ctx();
if (!c->has_mesh_queries) return -1;
int q = -1;
plat_mutex_lock(&rec_lock);
if (!active() || frame_now() != dump_frame || mq_n >= MQ_MAX || mq_failed) goto out;
if (!mq_pool) {
VkQueryPoolCreateInfo ci = {.sType = VK_STRUCTURE_TYPE_QUERY_POOL_CREATE_INFO,
.queryType = VK_QUERY_TYPE_MESH_PRIMITIVES_GENERATED_EXT, .queryCount = MQ_MAX};
if (vkCreateQueryPool(c->device, &ci, NULL, &mq_pool) != VK_SUCCESS) {
mq_pool = VK_NULL_HANDLE, mq_failed = 1;
rt_log("vk_dump: the mesh primitive query pool could not be created");
goto out;
}
VkQueryPoolCreateInfo si = {.sType = VK_STRUCTURE_TYPE_QUERY_POOL_CREATE_INFO,
.queryType = VK_QUERY_TYPE_PIPELINE_STATISTICS, .queryCount = MQ_MAX,
.pipelineStatistics = mq_stat_bits};
if (vkCreateQueryPool(c->device, &si, NULL, &mq_stats) != VK_SUCCESS) mq_stats = VK_NULL_HANDLE;
}
if (!mq_n) { // the last dump's queries finished at its present
vkResetQueryPool(c->device, mq_pool, 0, MQ_MAX);
if (mq_stats) vkResetQueryPool(c->device, mq_stats, 0, MQ_MAX);
}
q = (int)mq_n++;
mq_draw[q] = mq_next;
mq_draw[q].pass = seq; // the number this pass gets when it ends
vkCmdBeginQuery(ex->cb, mq_pool, (uint32_t)q, 0);
if (mq_stats) vkCmdBeginQuery(ex->cb, mq_stats, (uint32_t)q, 0);
ex->recorded += 2;
out:
plat_mutex_unlock(&rec_lock);
return q;
}
void vk_dump_mesh_end(VkExec *ex, int q) {
if (q < 0) return;
if (mq_stats) vkCmdEndQuery(ex->cb, mq_stats, (uint32_t)q);
vkCmdEndQuery(ex->cb, mq_pool, (uint32_t)q);
ex->recorded += 2;
}
static void mq_write(void) { // after vk_sync_finish: every query of the frame was submitted and ran
if (!mq_n || !mq_pool) return;
uint64_t *r = calloc(2ull * mq_n, sizeof *r), *st = calloc((MQ_STATS + 1ull) * mq_n, sizeof *st);
char path[600];
snprintf(path, sizeof path, "%s/mesh_prims.txt", dir);
FILE *f = r && st ? fopen(path, "w") : NULL;
if (f) {
VkResult vr = vkGetQueryPoolResults(vk_ctx()->device, mq_pool, 0, mq_n, 16ull * mq_n, r, 16,
VK_QUERY_RESULT_64_BIT | VK_QUERY_RESULT_WITH_AVAILABILITY_BIT);
VkResult vs = mq_stats ? vkGetQueryPoolResults(vk_ctx()->device, mq_stats, 0, mq_n, 8ull * (MQ_STATS + 1) * mq_n, st,
8ull * (MQ_STATS + 1),
VK_QUERY_RESULT_64_BIT | VK_QUERY_RESULT_WITH_AVAILABILITY_BIT)
: VK_ERROR_UNKNOWN;
fprintf(f, "# per mesh draw: primitives its mesh shader emitted (mesh primitives generated query; result %d), then "
"pipeline statistics (result %d): task and mesh shader invocations, clipper input and output "
"primitives, fragment shader invocations\n", (int)vr, (int)vs);
for (uint32_t i = 0; i < mq_n; i++) {
const MqDraw *d = &mq_draw[i];
const uint64_t *s = st + (MQ_STATS + 1ull) * i;
fprintf(f, "pass %04d prog %llu ts %016llx ms %016llx depth test %u write %u func %u: ", d->pass, d->prog, d->ts,
d->ms, d->dt, d->dw, d->df);
if (r[2 * i + 1]) fprintf(f, "%llu primitives", (unsigned long long)r[2 * i]);
else fprintf(f, "primitives not available");
// the counters come in the order of their flag bits: clipping invocations 0x20, clipping primitives 0x40,
// fragment invocations 0x80, task invocations 0x800, mesh invocations 0x1000
if (mq_stats && s[MQ_STATS])
fprintf(f, "; task %llu mesh %llu clip in %llu clip out %llu fragments %llu", (unsigned long long)s[3],
(unsigned long long)s[4], (unsigned long long)s[0], (unsigned long long)s[1], (unsigned long long)s[2]);
else fprintf(f, "; statistics not available");
fprintf(f, "\n");
}
fclose(f);
}
free(r), free(st);
mq_n = 0;
}
void vk_dump_draw(VkExec *ex, const VkPipeState *s, VkPipeline p, const VkPipeInfo *info, int mesh) {
plat_mutex_lock(&rec_lock);
if (!active()) goto out;
if (mesh)
mq_next = (MqDraw){0, ex->prog ? ex->prog->serial : 0, shader_hash(s->ts), shader_hash(s->vs), (unsigned)s->ds.dt,
(unsigned)s->ds.dw, (unsigned)s->ds.df};
pass_draws++;
uint64_t th = bindings_hash(ex, info);
if (mesh) // a mesh draw's per-draw parameters often live in its uniform blocks: draws that differ there are told apart
for (int st = 0; st < 8; st++) // every NVN stage: 6 is task, 7 mesh
for (int k = 0; k < 18; k++) th = (th ^ ex->ubo[st][k].addr ^ ex->ubo[st][k].size << 48) * 0x100000001b3ull;
for (int i = 0; i < nents; i++)
if (ents[i].p == p && ents[i].th == th) {
ents[i].draws++;
@@ -742,8 +847,15 @@ void vk_dump_draw(VkExec *ex, const VkPipeState *s, VkPipeline p, const VkPipeIn
goto out;
}
Str b = {0};
sp(&b, "vs %016llx fs %016llx prog %llu prim %d\n", (unsigned long long)shader_hash(s->vs),
(unsigned long long)shader_hash(s->fs), (unsigned long long)(ex->prog ? ex->prog->serial : 0), s->prim);
if (mesh) // depth: test, write and the NVN function (VkCompareOp + 1: LESS 2, EQUAL 3, LEQUAL 4, GREATER 5, GEQUAL 7)
sp(&b, "mesh: ts %016llx ms %016llx fs %016llx prog %llu depth test %u write %u func %u cull %u front %s\n",
(unsigned long long)shader_hash(s->ts), (unsigned long long)shader_hash(s->vs),
(unsigned long long)shader_hash(s->fs), (unsigned long long)(ex->prog ? ex->prog->serial : 0),
(unsigned)s->ds.dt, (unsigned)s->ds.dw, (unsigned)s->ds.df, (unsigned)s->poly.cull,
s->poly.front == NVN_FRONT_FACE_CW ? "CW" : "CCW");
else
sp(&b, "vs %016llx fs %016llx prog %llu prim %d\n", (unsigned long long)shader_hash(s->vs),
(unsigned long long)shader_hash(s->fs), (unsigned long long)(ex->prog ? ex->prog->serial : 0), s->prim);
// target 0's blending as NVN numbers (NVNblendFunc, NVNblendEquation), which decides whether a quad covers or mixes
const blend_st *bl = &s->blend[0];
sp(&b, " blend: enabled targets %#x; target 0 rgb %u*src %u*dst eq %u, alpha %u*src %u*dst eq %u; channel mask %#x\n",
@@ -959,6 +1071,7 @@ int vk_dump_white; // vk_backend.c asks for a thumbnail of every present while
// frame, so a movie's picture can be followed through the game's passes at chosen moments. Each dump has its own
// folder, base_dir/f<present>.
static int movie_k, movie_n, movie_done;
static int key_mode; // NX2NAVI_DUMP=key (vk_dump_init)
static uint64_t movie_at[8];
static int movie_req; // set by the movie's thread, taken by vk_dump_present
@@ -978,6 +1091,11 @@ static void open_files(void) {
snprintf(path, sizeof path, "%s/images.txt", dir);
images_f = fopen(path, "w");
if (!passes_f || !images_f) rt_fatal("vk_dump: cannot write to %s", dir);
// NX2NAVI_DUMP_BUF with dumps named as the run goes (key, white, movie): the log goes with each dump's folder
if (bd_size && !bufs_f) {
snprintf(path, sizeof path, "%s/bufs.txt", dir);
bufs_f = fopen(path, "w");
}
}
void vk_dump_init(void) {
@@ -994,10 +1112,14 @@ void vk_dump_init(void) {
while ((*q == ':' || *q == ',') && movie_n < 8) movie_at[movie_n++] = strtoull(q + 1, &q, 10);
if (movie_k < 1 || !movie_n) movie_k = 0;
}
// NX2NAVI_DUMP=key: no frame is named; each press of Insert (input_dump_key: unbound in the game's layout, and read on
// Windows even while another window has the focus) dumps the frame after it into NX2NAVI_DUMP_DIR/f<frame>, as often
// as it is pressed: for a glitch that comes and goes, caught by the player while it is on screen.
key_mode = e && !strcmp(e, "key");
if (dump_white || dump_after) {
dump_frame = UINT64_MAX;
vk_thumbs_enable(16);
} else if (movie_k) {
} else if (movie_k || key_mode) {
dump_frame = UINT64_MAX;
} else if (!e || !*e || !(dump_frame = strtoull(e, NULL, 10))) return;
const char *lf = cfg_get("NX2NAVI_DUMP_LOG");
@@ -1019,7 +1141,7 @@ void vk_dump_init(void) {
snprintf(base_dir, sizeof base_dir, "%s", d && *d ? d : "dump");
snprintf(dir, sizeof dir, "%s", base_dir);
char path[600];
if (!dump_white && !movie_k) open_files();
if (!dump_white && !movie_k && !key_mode) open_files();
const char *bf = cfg_get("NX2NAVI_DUMP_BUF");
if (bf && *bf) {
char *q;
@@ -1031,7 +1153,8 @@ void vk_dump_init(void) {
if (*q == ':') bd_h = (uint32_t)strtoul(q + 1, &q, 0);
if (!bd_w || !bd_h) bd_stride = 0;
snprintf(path, sizeof path, "%s/bufs.txt", dir);
if (bd_size && !(bufs_f = fopen(path, "w"))) bd_size = 0;
// a dump named as the run goes opens it with its folder (open_files); a named frame's folder exists already
if (bd_size && !dump_white && !movie_k && !key_mode && !(bufs_f = fopen(path, "w"))) bd_size = 0;
}
vk_dump_armed = 1;
rt_log("vk_dump: frame %llu goes to %s", (unsigned long long)dump_frame, dir);
@@ -1080,11 +1203,23 @@ void vk_dump_present(uint64_t frame) {
dump_frame = frame + 1;
rt_log("vk_dump: frame %llu goes to %s", (unsigned long long)dump_frame, dir);
}
if (key_mode && dump_frame == UINT64_MAX) { // NX2NAVI_DUMP=key: a new press of Insert names the next frame
static int held;
int now = input_dump_key();
if (now && !held) {
snprintf(dir, sizeof dir, "%s/f%llu", base_dir, (unsigned long long)(frame + 1));
open_files(); // before the frame is named: its passes write to them
dump_frame = frame + 1;
rt_log("vk_dump: Insert: frame %llu goes to %s", (unsigned long long)dump_frame, dir);
}
held = now;
}
if (frame != dump_frame) return;
plat_mutex_lock(&rec_lock);
finished = 1;
plat_mutex_unlock(&rec_lock);
vk_sync_finish(); // the frame's copies are converted and written
mq_write();
plat_mutex_lock(&img_lock);
fclose(images_f);
images_f = NULL;
@@ -1094,7 +1229,7 @@ void vk_dump_present(uint64_t frame) {
passes_f = NULL;
rt_log("vk_dump: frame %llu: %d passes and dispatches, about %llu MB of PNGs in %s", (unsigned long long)frame, seq,
(unsigned long long)(png_bytes >> 20), dir);
if (dump_white || movie_k) { // ready for another dump (vk_dump_present checks a white one's thumbnail 3 presents on)
if (dump_white || movie_k || key_mode) { // ready for another dump (vk_dump_present checks a white one's thumbnail 3 presents on)
if (dump_white) white_last = frame;
plat_mutex_lock(&rec_lock);
seq = copied = 0, last_targets = 0, pass_skip = NULL, png_bytes = 0;