diff --git a/runtime/cfg.c b/runtime/cfg.c index badaa090..e2a06afd 100644 --- a/runtime/cfg.c +++ b/runtime/cfg.c @@ -59,7 +59,7 @@ static const char *const known[] = { "SHADER_MEMO", "SHADOW_RANGE", "SHADOW_RES", "SHADOW_SCALE", "SHARED_FONTS", "SHARPNESS", "SHOTS", "SHOTS_DIR", "SHOTS_FULL", "SPEED_PROBE", "SPIN", "SPIN_HANDOFF_US", "SPLIT_DRAWS", "SPLIT_US", "STICK_DEADZONE", "STICK_SENS", "SWKBD_ANSWER", - "SWKBD_TEST", "TEST_PAD", "TEST_PREPARE_SHADERS", "TEX_ALIAS", "TEX_HEAP_EXTRA_MB", "TEX_MEMO", "TEX_VRAM_PRESSURE", "TIMER_1MS", "TRACE", "TXD_TRANSPOSE", "UBO_SNAPSHOT", "UB_OVERLAY", "UPSCALER", "VK_DYN_A2C", "VK_DYN_VERTEX", "VK_NO_FLOAT64", + "SWKBD_TEST", "TEST_PAD", "TEST_PREPARE_SHADERS", "TEX_ALIAS", "TEX_HEAP_EXTRA_MB", "TEX_MEMO", "TEX_VRAM_PRESSURE", "TIMER_1MS", "TRACE", "TXD_TRANSPOSE", "UBO_SNAPSHOT", "UB_OVERLAY", "UPSCALER", "VB_USED", "VK_DYN_A2C", "VK_DYN_VERTEX", "VK_NO_FLOAT64", "VK_PRESENT", "VK_RECORDER", "VK_SSBO_ALIGN", "VK_SUBGROUP", "VK_SUBGROUP_PIN", "VK_VALIDATION", "VOLUME", "VRAM_HEADROOM", "VRAM_LIMIT_MIB", "VS_OUTS", "WALK_KEY", "WALK_SCALE", "WB_EXACT", "WB_FENCE_LIMIT", "WB_MERGE", "WINDOW_SIZE", "XLATE", "XLATE_ID", "XLATE_KEEP", "XLATE_PROCS", "ZSTD_CHECK", "ZSTD_NATIVE", @@ -100,7 +100,7 @@ static const struct { const char *key, *section; } sections[] = { {"LIB_PREBUILD", "performance"}, {"LIB_PREBUILD_THREADS", "performance"}, {"PIPE_PREBUILD", "performance"}, {"PREWARM_XLATE", "performance"}, {"GPL", "performance"}, {"ASTC", "performance"}, {"MEMORY", "performance"}, {"TEX_ALIAS", "performance"}, {"TEX_VRAM_PRESSURE", "performance"}, {"ASTC_BC", "performance"}, {"LOD_GUARD", "performance"}, {"FIX_C0", "performance"}, {"FIX_ZERO", "performance"}, - {"BIND_SKIP", "performance"}, {"SHADER_MEMO", "performance"}, {"PIPE_MEMO", "performance"}, {"TEX_MEMO", "performance"}, {"DESC_TEMPLATE", "performance"}, + {"BIND_SKIP", "performance"}, {"SHADER_MEMO", "performance"}, {"PIPE_MEMO", "performance"}, {"TEX_MEMO", "performance"}, {"VB_USED", "performance"}, {"DESC_TEMPLATE", "performance"}, {"PUSH_SKIP", "performance"}, {"SPLIT_DRAWS", "performance"}, {"SPLIT_US", "performance"}, {"VK_VALIDATION", "performance"}, {"VK_DYN_VERTEX", "performance"}, {"VK_DYN_A2C", "performance"}, {"VK_SUBGROUP_PIN", "performance"}, {"TRACE", "diagnostics"}, {"FLIGHT", "diagnostics"}, {"PASS_CRUMBS", "diagnostics"}, {"FLIGHT_SPIKE_MS", "diagnostics"}, @@ -174,6 +174,7 @@ static const CfgMeta metas[] = { // render fixes {"LOD_GUARD", "1", "0|1", RESTART}, {"FIX_C0", "1", "0|1", RESTART}, {"FIX_ZERO", "1", "0|1", RESTART}, {"BIND_SKIP", "1", "0|1", RESTART}, {"SHADER_MEMO", "1", "0|1", RESTART}, {"PIPE_MEMO", "1", "0|1", RESTART}, {"TEX_MEMO", "1", "0|1", RESTART}, + {"VB_USED", "1", "0|1", RESTART}, {"DESC_TEMPLATE", "1", "0|1", RESTART}, {"PUSH_SKIP", "1", "0|1", RESTART}, {"SPLIT_DRAWS", "64", "0..1000", RESTART}, {"SPLIT_US", "1000", "0..20000", RESTART}, diff --git a/runtime/vk.h b/runtime/vk.h index 39cfb9a4..efebf6e6 100644 --- a/runtime/vk.h +++ b/runtime/vk.h @@ -505,6 +505,7 @@ void vk_pipe_state_defaults(VkPipeState *s); // NVN defaults of every state ob enum { VK_PIPE_SUPPORT = GFX_RES_KINDS }; // VkPipeBinding.kind of the support buffer #define VK_PIPE_ZERO_BINDING 16 // vertex binding of shader inputs without an attribute: bind a zeroed // buffer of at least 16 bytes there (stride 0) +#define VK_PIPE_ALL_BINDINGS 0x1ffffu // every vertex binding, streams 0-15 and the zero binding typedef struct { uint16_t binding; // in its set uint8_t stage; // GFX_STAGE_* that declared it (support buffer: the first stage) @@ -557,8 +558,9 @@ VkPipeline vk_pipeline_compute(const GfxShader *cs, const VkPipeInfo **info); // has them dynamic, depth clamp, polygon mode, blend enables, equations, write masks and alpha to coverage. The replay // sets the rest: vkCmdSetViewportWithCount, vkCmdSetScissorWithCount, depth bias, blend constants, stencil compare and // write masks and references; vertex buffers with vkCmdBindVertexBuffers2 (strides NULL when -// vk_pipeline_vertex_input_dynamic(), else from VkPipeState.vstream). -void vk_pipeline_set_dynamic(VkCommandBuffer cb, const VkPipeState *s); +// 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); 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 20417204..dafa05f1 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; + 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 VkDeviceSize vo[17], vsz[17], vstr[17]; int vcls[17]; // their pool class and pool record, for the census @@ -2046,21 +2048,34 @@ static void record_fixed(VkExec *ex) { ex->dirty = 0; } +// TOTK_VB_USED=0: every bound stream is resolved and bound, also those the draw's vertex input does not read. +static int vb_used_on(void) { + static int on = -1; + if (on < 0) on = cfg_bool("TOTK_VB_USED"); + return on; +} + static void bind_vertex_buffers(VkExec *ex) { VkBuffer vb[17]; VkDeviceSize vo[17], vsz[17], vstr[17]; int vcls[17] = {0}; void *vpool[17] = {0}; int dyn = vk_pipeline_vertex_input_dynamic(); + // Only the streams the draw's vertex input reads (B.vmask, from vk_pipeline_set_dynamic): the game keeps about ten + // bound and a draw reads a few. The others get the zero buffer, never a stale one: a buffer bound but not read must + // still be a live handle. + uint32_t used = vb_used_on() && B.st_valid ? B.vmask : VK_PIPE_ALL_BINDINGS; // A stream whose range is the one its bound buffer was resolved from, with no pool or mirror change since // (vk_mem_gen, read before any resolve), resolves to that buffer again: most draws change one stream or none. uint64_t gen = vk_mem_gen(); int same_gen = B.vb_valid && B.vgen == gen; + uint32_t res = 0; for (int b = 0; b < 17; b++) { vb[b] = Z.zbuf, vo[b] = 0, vsz[b] = ZERO_BYTES, vstr[b] = b < 16 ? ex->pipe.vstream[b].stride : 0; - if (b == VK_PIPE_ZERO_BINDING) continue; + if (b == VK_PIPE_ZERO_BINDING || !(used >> b & 1)) continue; + res |= 1u << b; const NVNbufferRange *r = &ex->vbuf[b]; - if (same_gen && B.vin[b].address == r->address && B.vin[b].size == r->size) { + if (same_gen && (B.vres >> b & 1) && B.vin[b].address == r->address && B.vin[b].size == r->size) { vb[b] = B.vb[b], vo[b] = B.vo[b], vsz[b] = B.vsz[b], vcls[b] = B.vcls[b], vpool[b] = B.vpool[b]; continue; } @@ -2072,7 +2087,7 @@ static void bind_vertex_buffers(VkExec *ex) { } memcpy(B.vcls, vcls, sizeof vcls), memcpy(B.vpool, vpool, sizeof vpool); memcpy(B.vin, ex->vbuf, sizeof B.vin); - B.vgen = gen; + B.vgen = gen, B.vres = res; if (B.vb_valid && !memcmp(B.vb, vb, sizeof vb) && !memcmp(B.vo, vo, sizeof vo) && !memcmp(B.vsz, vsz, sizeof vsz) && (dyn || !memcmp(B.vstr, vstr, sizeof vstr))) return; @@ -2274,7 +2289,7 @@ 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)) { - vk_pipeline_set_dynamic(ex->cb, &s); + B.vmask = vk_pipeline_set_dynamic(ex->cb, &s); ex->recorded++; 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; diff --git a/runtime/vk_pipeline.c b/runtime/vk_pipeline.c index 8c288eec..c026e471 100644 --- a/runtime/vk_pipeline.c +++ b/runtime/vk_pipeline.c @@ -3113,8 +3113,8 @@ int vk_pipeline_warm_shaders(void) { // ---- dynamic state -void vk_pipeline_set_dynamic(VkCommandBuffer cb, const VkPipeState *s) { - if (!ensure_init()) return; +uint32_t vk_pipeline_set_dynamic(VkCommandBuffer cb, const VkPipeState *s) { + if (!ensure_init()) return VK_PIPE_ALL_BINDINGS; const poly_st *pl = &s->poly; const ds_st *d = &s->ds; // The front face maps unchanged: TOTK sets the upper-left window origin, and the y-flipped viewport (y_flip) puts @@ -3137,16 +3137,21 @@ void vk_pipeline_set_dynamic(VkCommandBuffer cb, const VkPipeState *s) { vkCmdSetDepthBiasEnable(cb, (pl->offset_en >> mode & 1) != 0); vkCmdSetRasterizerDiscardEnable(cb, s->raster_discard != 0); vkCmdSetPrimitiveRestartEnable(cb, s->prim_restart != 0); - if (P.dyn_vi && s->vs) { + // The vertex bindings the draw reads: those of its vertex input, set here or made into the pipeline from the same + // state by the same vertex_input. + uint32_t streams = VK_PIPE_ALL_BINDINGS; + if (s->vs) { VtxInput v; vertex_input(s, &v); - rec_vertex_input(P.set_vi, cb, v.n_bind, v.b, v.n_attr, v.a); + if (P.dyn_vi) rec_vertex_input(P.set_vi, cb, v.n_bind, v.b, v.n_attr, v.a); + streams = 0; + for (uint32_t i = 0; i < v.n_bind; i++) streams |= 1u << v.b[i].binding; } if (P.dyn_clamp) rec_bool(P.set_clamp, cb, s->depth_clamp != 0); if (P.dyn_poly) rec_polygon_mode(P.set_poly, cb, polygon_mode(pl->mode)); if (P.dyn_a2c) rec_bool((PFN_vkCmdSetDepthClampEnableEXT)P.set_a2c, cb, s->ms.a2c != 0); // the same signature int n = target_count(s); - if (!n) return; + if (!n) return streams; VkBool32 en[8]; VkColorBlendEquationEXT eq[8]; VkColorComponentFlags wm[8]; @@ -3160,6 +3165,7 @@ void vk_pipeline_set_dynamic(VkCommandBuffer cb, const VkPipeState *s) { if (P.dyn_blend_en) rec_blend_enable(P.set_blend_en, cb, 0, (uint32_t)n, en); if (P.dyn_blend_eq) rec_blend_equation(P.set_blend_eq, cb, 0, (uint32_t)n, eq); if (P.dyn_wmask) rec_write_mask(P.set_wmask, cb, 0, (uint32_t)n, wm); + return streams; } // ---- statistics, persistence, teardown diff --git a/totk.ini.example b/totk.ini.example index 4a09a50c..1507a8c5 100644 --- a/totk.ini.example +++ b/totk.ini.example @@ -93,13 +93,14 @@ # fix_zero = 1 # Optimisations, 0 turns each off: skip bindings that did not change, remember shader lookups, reuse the previous # draw's pipeline when its state repeats, remember texture descriptors between draws, update descriptors in bulk, -# skip push constants that did not change. +# skip push constants that did not change, bind only the vertex buffers a draw reads. # bind_skip = 1 # shader_memo = 1 # pipe_memo = 1 # tex_memo = 1 # desc_template = 1 # push_skip = 1 +# vb_used = 1 # Submit GPU work in parts every N draws between passes so the GPU starts earlier (0 to 1000; 0 = off), each part at # least split_us microseconds long (0 to 20000). # split_draws = 64