From 752e15bcc7236f1fdefa3b3436a6b80842b29d26 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 7 Nov 2026 00:56:53 +0000 Subject: [PATCH] Record only the dynamic states that changed since the last pipeline state in the command buffer Each change of pipeline state in prepare_draw re-recorded the whole dynamic state through vk_pipeline_set_dynamic: about twenty commands, the vertex input packet rebuilt, and the blend enable, equation and write mask of every attachment, however few fields differed. Every replay pipeline declares the same dynamic states, which a pipeline bind leaves as they were, so vk_pipeline_set_dynamic_delta compares the values with what the command buffer already holds and records only the commands that differ; the vertex input is rebuilt only when the vertex shader, attributes or streams change. The record is per command buffer (B.dyn_valid, cleared with the rest of B on a new one or a split). TOTK_DYN_DELTA=0 records the full set as before; ab alternates; TOTK_TRACE=perf counts calls, commands and calls that changed nothing. 1.4.3, RTX 4070 Ti, 2560x1440 DLAA, standing still on Great Sky Island, TOTK_AB=10 (tools/ab_summary.py): 13.75 ms a frame with the full sets, 12.59 ms with the deltas (-8.5%, 72.7 to 79.4 fps); replay 7.77 to 6.67 ms. An A/A run with the same method moved the frame by 0.08 ms. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01BspNcezsiV8zT8wcnXrrME --- docs/settings.md | 1 + runtime/cfg.c | 2 +- runtime/vk.h | 6 +++ runtime/vk_exec_draw.c | 30 +++++++++-- runtime/vk_pipeline.c | 112 +++++++++++++++++++++++++++++++++++++++++ 5 files changed, 147 insertions(+), 4 deletions(-) diff --git a/docs/settings.md b/docs/settings.md index eb7cee24..d71092d9 100644 --- a/docs/settings.md +++ b/docs/settings.md @@ -120,6 +120,7 @@ Many rows below are developer tools that only act together with another setting | Setting | What it does | Effect of changing it | Safety | | --- | --- | --- | --- | | `block_views_3d` (default `0`) | `1` gives 3D block-compressed images block views too. | On some AMD cards a GPU write past the end of the image's memory loses the Vulkan device, and the game stops. | Risky | +| `dyn_delta` (default `1`) | Records only the pipeline dynamic states (cull mode, depth and stencil tests, vertex input, blend enables, equations and write masks, ...) whose values changed since the last draw in the command buffer, also across pipeline changes; `0` records the full set at every change of pipeline state, as before; `ab` alternates with the `ab` blocks. | Fewer commands per pipeline change on the render and recorder threads; a driver that loses dynamic state across pipeline binds would draw with stale state. `TOTK_TRACE=perf` prints the calls and commands per submission. | Caution | | `feedback` | `0` samples a target that an earlier draw of the same pass wrote with no barrier; `restart` ends and restarts the pass behind a full barrier; otherwise the feedback-loop layout is used when the GPU supports it. | `0` brings back the old behaviour that the barrier fixed, so the picture can break in passes that read a target they draw into; `restart` costs extra passes. | Risky | | `flush_span` (default `0`) | `1` copies one span from the lowest to the highest flushed address of a pool, as an older version did. | Copies far more data to the GPU in crowded towns: an estimated 30-50 ms of GPU time per large copy over PCIe 3.0 x8. | Caution | | `freeze_idle` (default `60`) | Number of submissions a 2 MiB region stays unwritten before it is made read-only; `0` turns the freeze off. | A larger value keeps more regions writable and costs more write tracking; a write to a frozen region costs about 60 microseconds. | Caution | diff --git a/runtime/cfg.c b/runtime/cfg.c index e2a06afd..5c9f3dbd 100644 --- a/runtime/cfg.c +++ b/runtime/cfg.c @@ -41,7 +41,7 @@ static const char *const known[] = { "BIND_ZL", "BIND_ZR", "BLOCK_VIEWS_3D", "CLOTH_REALTIME", "COLOR_BRIGHTNESS", "COLOR_CONTRAST", "COLOR_GAMMA", "COLOR_PRESET", "COLOR_SATURATION", "CONTACT_DEBUG", "CONTACT_SHADOWS", "CORE_DIAG", "CRIT", "CRIT_OUT", "DESC_TEMPLATE", "DEVICE_PROFILE", "DISCORD_APP_ID", "DISCORD_IMAGE", "DISCORD_RPC", "DLAA", "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", "EXEFS", "FEEDBACK", "FIX_C0", "FIX_ZERO", "FLIGHT", "FLIGHT_AT", "FLIGHT_DIR", "FLIGHT_FRAMES", + "DUMP_LOG", "DYN_DELTA", "EXEFS", "FEEDBACK", "FIX_C0", "FIX_ZERO", "FLIGHT", "FLIGHT_AT", "FLIGHT_DIR", "FLIGHT_FRAMES", "FLIGHT_SPIKE_MS", "FLIGHT_THUMBS", "FLUSH_SPAN", "FOV", "FOV_PATCH", "FPS60", "FPS_LIMIT", "FRAME_QUEUE", "FREEZE_IDLE", "FREE_CORES", "FULLSCREEN", "FXAA", "GPL", "GPL_DRAW_BUILD", "GPU", "GPU_INDEX", "GPU_NAME", "HDR", "HDR_GAME_NITS", "HDR_HUE", diff --git a/runtime/vk.h b/runtime/vk.h index efebf6e6..abe40f57 100644 --- a/runtime/vk.h +++ b/runtime/vk.h @@ -561,6 +561,12 @@ VkPipeline vk_pipeline_compute(const GfxShader *cs, const VkPipeInfo **info); // vk_pipeline_vertex_input_dynamic(), else from VkPipeState.vstream). Returns the vertex bindings (bit per binding) the // state's vertex input reads, whether set here or made into the pipeline. uint32_t vk_pipeline_set_dynamic(VkCommandBuffer cb, const VkPipeState *s); +// The replay's variant (one thread, TOTK_DYN_DELTA): valid 1 says every graphics pipeline bound in cb since the last call +// was one of vk_pipeline_graphics*'s, which all declare the same dynamic states, and nothing else set them, so cb still +// holds what that call recorded (Vulkan keeps dynamic state across such binds for the life of the command buffer); +// then only the commands whose values differ are recorded. valid 0 (a new command buffer, or one whose state another +// recorder changed) records everything. *cmds: the commands recorded (valid 0: 1). Returns as vk_pipeline_set_dynamic. +uint32_t vk_pipeline_set_dynamic_delta(VkCommandBuffer cb, const VkPipeState *s, int valid, uint32_t *cmds); int vk_pipeline_vertex_input_dynamic(void); // 1: vertex input is VK_EXT_vertex_input_dynamic_state // Translator answers from the bound state: attr_type from the vertex attribute formats (vs), frag_out_type from the // colour target formats (fs). Other fields are left as they are; either pointer may be NULL. diff --git a/runtime/vk_exec_draw.c b/runtime/vk_exec_draw.c index dafa05f1..4452cf30 100644 --- a/runtime/vk_exec_draw.c +++ b/runtime/vk_exec_draw.c @@ -99,6 +99,8 @@ static struct { VkPipeline pipe[2]; // bound per bind point: 0 graphics, 1 compute VkPipeState st; // the state vk_pipeline_set_dynamic last recorded int st_valid; + int dyn_valid; // vk_pipeline_set_dynamic_delta's record of this command buffer's dynamic state + // holds (TOTK_DYN_DELTA); pipeline binds keep it, the memset clears it uint32_t vmask; // the vertex bindings st's vertex input reads (valid with st_valid) uint32_t vres; // the streams whose vb entry was resolved from vin (others: the zero buffer) VkBuffer vb[17]; // vertex buffers last bound @@ -139,6 +141,10 @@ static struct { uint64_t draws, bind[2], desc[2], push, push_skip, set_new, set_reuse, ac_miss, same_prog, same_all, bind_skip; } BS = {-1}; +// TOTK_DYN_DELTA (prepare_draw): the mode and, for the perf line, the set_dynamic calls, the commands they recorded +// (a full call counts 1) and the calls that recorded none. +static struct { int mode; uint64_t calls, cmds, empty; } DD = {-1}; + // The inputs of the last graphics bind_resources (bind_sig) and what its build counted for the traffic census, which a // skipped build counts again. RS_BYTES bounds the bound-slot bytes a pipeline reads (RsList): per binding at most one // c0 storage table, otherwise one bound range or handle per descriptor. @@ -2289,8 +2295,20 @@ static int prepare_draw(VkExec *ex, int prim, int32_t count, int restart) { if (!p || !info) return warn_once(W_PIPELINE, "a graphics pipeline could not be created: draws with it are skipped"), 0; bind_pipeline(ex, VK_PIPELINE_BIND_POINT_GRAPHICS, p); if (!B.st_valid || memcmp(&B.st, &s, sizeof s)) { - B.vmask = vk_pipeline_set_dynamic(ex->cb, &s); - ex->recorded++; + // TOTK_DYN_DELTA: only the dynamic states whose values changed since the last call in this command buffer. A + // pipeline bind clears st_valid but not dyn_valid: every pipeline here declares the same dynamic states, which + // the bind leaves as they were (the viewport, scissor and stencil values of record_fixed rely on that too). + if (DD.mode < 0) DD.mode = rt_knob("DYN_DELTA", "1"); + uint32_t cmds = 1; + if (RT_KNOB_ON(DD.mode)) { + B.vmask = vk_pipeline_set_dynamic_delta(ex->cb, &s, B.dyn_valid, &cmds); + B.dyn_valid = 1; + } else { + B.vmask = vk_pipeline_set_dynamic(ex->cb, &s); + B.dyn_valid = 0; // the delta's record is stale from here on + } + DD.calls++, DD.cmds += cmds, DD.empty += !cmds; + ex->recorded += cmds != 0; B.st = s, B.st_valid = 1; if (ex->mark && vk_exec_frame.draws == ex->mark_draws0) ex->mark->first[0] = vs, ex->mark->first[1] = fs; } @@ -3360,7 +3378,13 @@ void vk_exec_ub_flush(VkExec *ex) { // TOTK_TRACE=perf, every 600 submissions: per submission, the updates and how many stayed in their pass. void vk_exec_ub_perf(void) { - static unsigned subs; + static unsigned subs, dsubs; + if (DD.mode >= 0 && ++dsubs % 600 == 0 && DD.calls) { + rt_log("perf: dynamic state per submission: %.1f calls, %.1f commands (a full set counts 1), %.1f calls with " + "nothing changed%s", DD.calls / 600.0, DD.cmds / 600.0, DD.empty / 600.0, + DD.mode == 2 ? " (TOTK_DYN_DELTA=ab)" : DD.mode ? "" : " (TOTK_DYN_DELTA=0)"); + DD.calls = DD.cmds = DD.empty = 0; + } if (OV.mode <= 0 || ++subs % 600) return; const double k = 1 / 600.0; rt_log("perf: uniform updates per submission %.1f: %.1f overlaid in a pass (copied in %.2f batches), %.1f copied " diff --git a/runtime/vk_pipeline.c b/runtime/vk_pipeline.c index c026e471..dedf7a37 100644 --- a/runtime/vk_pipeline.c +++ b/runtime/vk_pipeline.c @@ -3168,6 +3168,118 @@ uint32_t vk_pipeline_set_dynamic(VkCommandBuffer cb, const VkPipeState *s) { return streams; } +// The values vk_pipeline_set_dynamic records for a state, as the commands take them. +typedef struct { + VkCullModeFlags cull; + VkFrontFace front; + VkPrimitiveTopology topo; + VkBool32 dt, dw, st, bias, discard, restart, clamp, a2c; + VkCompareOp df, scmp[2]; + VkStencilOp sop[2][3]; // fail, pass, depth fail; front, back + VkPolygonMode poly; + int n; // colour attachments en, eq and wm hold + VkBool32 en[8]; + VkColorBlendEquationEXT eq[8]; + VkColorComponentFlags wm[8]; +} DynVals; + +static void dyn_vals(const VkPipeState *s, DynVals *o) { + const poly_st *pl = &s->poly; + const ds_st *d = &s->ds; + memset(o, 0, sizeof *o); + o->cull = (VkCullModeFlags)(pl->cull & 3); + o->front = pl->front == NVN_FRONT_FACE_CW ? VK_FRONT_FACE_CLOCKWISE : VK_FRONT_FACE_COUNTER_CLOCKWISE; + o->topo = vk_pipeline_topology(s->prim); + o->dt = d->dt, o->dw = d->dw, o->st = d->st; + o->df = compare_op(d->df); + o->sop[0][0] = stencil_op(d->fail_f), o->sop[0][1] = stencil_op(d->zpass_f), o->sop[0][2] = stencil_op(d->zfail_f); + o->sop[1][0] = stencil_op(d->fail_b), o->sop[1][1] = stencil_op(d->zpass_b), o->sop[1][2] = stencil_op(d->zfail_b); + o->scmp[0] = compare_op(d->sf_f), o->scmp[1] = compare_op(d->sf_b); + unsigned mode = pl->mode <= NVN_POLYGON_MODE_FILL ? pl->mode : NVN_POLYGON_MODE_FILL; + o->bias = (pl->offset_en >> mode & 1) != 0; + o->discard = s->raster_discard != 0, o->restart = s->prim_restart != 0; + o->clamp = s->depth_clamp != 0, o->a2c = s->ms.a2c != 0; + o->poly = polygon_mode(pl->mode); + o->n = target_count(s); + for (int i = 0; i < o->n; i++) { + const blend_st *b = &s->blend[i]; + o->en[i] = (s->color.blend_en >> i & 1) && blendable(target_format(s->color_fmt[i], NULL, NULL)); + o->eq[i] = (VkColorBlendEquationEXT){blend_factor(b->src_rgb), blend_factor(b->dst_rgb), blend_op(b->eq_rgb), + blend_factor(b->src_a), blend_factor(b->dst_a), blend_op(b->eq_a)}; + o->wm[i] = target_wmask(s, i); + } +} + +// What the replay's command buffer holds, as vk_pipeline_set_dynamic_delta last recorded it (the replay thread only): +// the values, the inputs of the vertex input and the vertex bindings it reads. Invariant while the caller passes +// valid 1: cb holds DC.v for every dynamic state of P.dyn, colour attachments [0, DC.v.n) included. +static struct { + DynVals v; + const GfxShader *vs; + uint32_t vattr[16]; + vs_st vstream[16]; + uint32_t streams; +} DC; + +uint32_t vk_pipeline_set_dynamic_delta(VkCommandBuffer cb, const VkPipeState *s, int valid, uint32_t *cmds) { + *cmds = 0; + if (!ensure_init()) return VK_PIPE_ALL_BINDINGS; + if (!valid) { // a new command buffer, or one whose dynamic state someone else set: everything, as before + uint32_t streams = vk_pipeline_set_dynamic(cb, s); + dyn_vals(s, &DC.v); + DC.vs = s->vs, DC.streams = streams; + memcpy(DC.vattr, s->vattr, sizeof DC.vattr), memcpy(DC.vstream, s->vstream, sizeof DC.vstream); + *cmds = 1; + return streams; + } + DynVals v; + dyn_vals(s, &v); + const DynVals *o = &DC.v; + uint32_t k = 0; + if (v.cull != o->cull) vkCmdSetCullMode(cb, v.cull), k++; + if (v.front != o->front) vkCmdSetFrontFace(cb, v.front), k++; + if (v.topo != o->topo) vkCmdSetPrimitiveTopology(cb, v.topo), k++; + if (v.dt != o->dt) vkCmdSetDepthTestEnable(cb, v.dt), k++; + if (v.dw != o->dw) vkCmdSetDepthWriteEnable(cb, v.dw), k++; + if (v.df != o->df) vkCmdSetDepthCompareOp(cb, v.df), k++; + if (v.st != o->st) vkCmdSetStencilTestEnable(cb, v.st), k++; + for (int f = 0; f < 2; f++) + if (memcmp(v.sop[f], o->sop[f], sizeof v.sop[f]) || v.scmp[f] != o->scmp[f]) + vkCmdSetStencilOp(cb, f ? VK_STENCIL_FACE_BACK_BIT : VK_STENCIL_FACE_FRONT_BIT, v.sop[f][0], v.sop[f][1], + v.sop[f][2], v.scmp[f]), + k++; + if (v.bias != o->bias) vkCmdSetDepthBiasEnable(cb, v.bias), k++; + if (v.discard != o->discard) vkCmdSetRasterizerDiscardEnable(cb, v.discard), k++; + if (v.restart != o->restart) vkCmdSetPrimitiveRestartEnable(cb, v.restart), k++; + // The vertex input is made from the vertex shader's inputs and the attribute and stream states alone + // (vertex_input): a draw that keeps all three keeps it, and the bindings it reads. + if (s->vs != DC.vs || memcmp(s->vattr, DC.vattr, sizeof DC.vattr) || memcmp(s->vstream, DC.vstream, sizeof DC.vstream)) { + uint32_t streams = VK_PIPE_ALL_BINDINGS; + if (s->vs) { + VtxInput vi; + vertex_input(s, &vi); + if (P.dyn_vi) rec_vertex_input(P.set_vi, cb, vi.n_bind, vi.b, vi.n_attr, vi.a), k++; + streams = 0; + for (uint32_t i = 0; i < vi.n_bind; i++) streams |= 1u << vi.b[i].binding; + } + DC.vs = s->vs, DC.streams = streams; + memcpy(DC.vattr, s->vattr, sizeof DC.vattr), memcpy(DC.vstream, s->vstream, sizeof DC.vstream); + } + if (P.dyn_clamp && v.clamp != o->clamp) rec_bool(P.set_clamp, cb, v.clamp), k++; + if (P.dyn_poly && v.poly != o->poly) rec_polygon_mode(P.set_poly, cb, v.poly), k++; + if (P.dyn_a2c && v.a2c != o->a2c) rec_bool((PFN_vkCmdSetDepthClampEnableEXT)P.set_a2c, cb, v.a2c), k++; + // Attachments past o->n may hold nothing yet in this command buffer: a state with more targets sets all of them. + // One with fewer leaves the ones past its count as they were, which no draw of it reads. + uint32_t n = (uint32_t)v.n; + int all = v.n > o->n; + if (n && P.dyn_blend_en && (all || memcmp(v.en, o->en, n * sizeof *v.en))) rec_blend_enable(P.set_blend_en, cb, 0, n, v.en), k++; + if (n && P.dyn_blend_eq && (all || memcmp(v.eq, o->eq, n * sizeof *v.eq))) rec_blend_equation(P.set_blend_eq, cb, 0, n, v.eq), k++; + if (n && P.dyn_wmask && (all || memcmp(v.wm, o->wm, n * sizeof *v.wm))) rec_write_mask(P.set_wmask, cb, 0, n, v.wm), k++; + DC.v = v; // the invariant now covers [0, v.n) only + *cmds = k; + return DC.streams; +} + // ---- statistics, persistence, teardown void vk_pipeline_stats(VkPipeStats *s) {