From 6b65fb6c0e2529ca149ed1d2a0615f64b4c00f2f Mon Sep 17 00:00:00 2001 From: Valiantsin Dzerakh Date: Mon, 26 Oct 2026 22:50:22 +0300 Subject: [PATCH] Keep sampled-texture descriptors between draws while the slot, sampler, submission and feedback pass stay the same tex_info resolved the slot, built a view key, probed the view table and looked up the sampler for every texture binding of every draw. A direct-mapped memo keyed by handle, resource type and separate returns the descriptor while vk_texture_slots_gen, vk_sampler_gen, the submission epoch and (with feedback-loop images) the pass are unchanged; storage images are not kept. TOTK_TEX_MEMO=0 turns it off, =check recomputes every hit (a fight run with it and TOTK_SHADER_MEMO=check stopped on nothing). Against main, profiler on, no bind stats: fight replay 13.9 to 11.9 ms per frame, render thread busy 16.3 to 14.6 ms, CPU-bound windows 15 to 9; field replay 9.5 to 8.2 ms. Co-Authored-By: Claude Opus 5.5 --- runtime/cfg.c | 6 +++--- runtime/ui/menu.cpp | 1 + runtime/ui_text.c | 2 ++ runtime/ui_text.h | 1 + runtime/vk_exec_draw.c | 41 +++++++++++++++++++++++++++++++++++++++++ 5 files changed, 48 insertions(+), 3 deletions(-) diff --git a/runtime/cfg.c b/runtime/cfg.c index e15c1f07..b607d728 100644 --- a/runtime/cfg.c +++ b/runtime/cfg.c @@ -45,7 +45,7 @@ static const char *const known[] = { "REPLAY_CHECK", "RESOLVE_CHECK", "ROMFS", "ROMFS_INDEX", "ROMFS_MMAP", "ROMFS_MODS", "ROMFS_PATCHES", "ROMFS_PROOF_DIR", "ROMFS_VERIFY", "RUMBLE", "RUMBLE_CURVE", "RUMBLE_STRENGTH", "RUMBLE_TRIGGERS", "SHADER_CACHE", "SHADER_MEMO", "SHARED_FONTS", "SHOTS", "SHOTS_DIR", "SHOTS_FULL", "SPEED_PROBE", "SPLIT_DRAWS", "SWKBD_ANSWER", - "SWKBD_TEST", "TEST_PREPARE_SHADERS", "TEX_ALIAS", "TIMER_1MS", "TRACE", "VK_DYN_VERTEX", "VK_NO_FLOAT64", + "SWKBD_TEST", "TEST_PREPARE_SHADERS", "TEX_ALIAS", "TEX_MEMO", "TIMER_1MS", "TRACE", "VK_DYN_VERTEX", "VK_NO_FLOAT64", "VK_PRESENT", "VK_SSBO_ALIGN", "VK_SUBGROUP", "VK_SUBGROUP_PIN", "VK_VALIDATION", "VOLUME", "VS_OUTS", "WINDOW_SIZE", "XLATE", "XLATE_ID", "XLATE_PROCS", }; @@ -65,7 +65,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"}, {"LOD_GUARD", "performance"}, {"FIX_C0", "performance"}, {"FIX_ZERO", "performance"}, - {"BIND_SKIP", "performance"}, {"SHADER_MEMO", "performance"}, {"PIPE_MEMO", "performance"}, {"DESC_TEMPLATE", "performance"}, + {"BIND_SKIP", "performance"}, {"SHADER_MEMO", "performance"}, {"PIPE_MEMO", "performance"}, {"TEX_MEMO", "performance"}, {"DESC_TEMPLATE", "performance"}, {"PUSH_SKIP", "performance"}, {"SPLIT_DRAWS", "performance"}, {"VK_VALIDATION", "performance"}, {"VK_DYN_VERTEX", "performance"}, {"VK_SUBGROUP_PIN", "performance"}, {"TRACE", "diagnostics"}, {"FLIGHT", "diagnostics"}, {"FLIGHT_SPIKE_MS", "diagnostics"}, @@ -104,7 +104,7 @@ static const CfgMeta metas[] = { {"TEX_ALIAS", "1", "0|1", RESTART}, // 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}, + {"BIND_SKIP", "1", "0|1", RESTART}, {"SHADER_MEMO", "1", "0|1", RESTART}, {"PIPE_MEMO", "1", "0|1", RESTART}, {"TEX_MEMO", "1", "0|1", RESTART}, {"DESC_TEMPLATE", "1", "0|1", RESTART}, {"PUSH_SKIP", "1", "0|1", RESTART}, {"SPLIT_DRAWS", "64", "0..1000", RESTART}, // Vulkan diff --git a/runtime/ui/menu.cpp b/runtime/ui/menu.cpp index 9771d416..29417698 100644 --- a/runtime/ui/menu.cpp +++ b/runtime/ui/menu.cpp @@ -609,6 +609,7 @@ static void tab_advanced(float scale) { opt_toggle("TOTK_BIND_SKIP", UI_MENU_BIND_SKIP, UI_MENU_BIND_SKIP_DESC); opt_toggle("TOTK_SHADER_MEMO", UI_MENU_SHADER_MEMO, UI_MENU_SHADER_MEMO_DESC); opt_toggle("TOTK_PIPE_MEMO", UI_MENU_PIPE_MEMO, UI_MENU_PIPE_MEMO_DESC); + opt_toggle("TOTK_TEX_MEMO", UI_MENU_TEX_MEMO, UI_MENU_TEX_MEMO_DESC); opt_toggle("TOTK_DESC_TEMPLATE", UI_MENU_DESC_TEMPLATE, UI_MENU_DESC_TEMPLATE_DESC); opt_toggle("TOTK_PUSH_SKIP", UI_MENU_PUSH_SKIP, UI_MENU_PUSH_SKIP_DESC); opt_slider("TOTK_SPLIT_DRAWS", UI_MENU_SPLIT_DRAWS, 0, 1000, UI_MENU_SPLIT_DRAWS_DESC, 8, "%.0f"); diff --git a/runtime/ui_text.c b/runtime/ui_text.c index 084fef38..2347af64 100644 --- a/runtime/ui_text.c +++ b/runtime/ui_text.c @@ -128,6 +128,7 @@ static const char *const text_en[UI_TEXT_COUNT] = { [UI_MENU_BIND_SKIP_DESC] = "Optimisation: skips bindings that did not change.", [UI_MENU_SHADER_MEMO] = "Shader memo", [UI_MENU_SHADER_MEMO_DESC] = "Optimisation: remembers shader lookups.", [UI_MENU_PIPE_MEMO] = "Pipeline memo", [UI_MENU_PIPE_MEMO_DESC] = "Optimisation: reuses the previous draw's pipeline when its state repeats.", + [UI_MENU_TEX_MEMO] = "Texture binding memo", [UI_MENU_TEX_MEMO_DESC] = "Optimisation: remembers texture descriptors between draws.", [UI_MENU_DESC_TEMPLATE] = "Descriptor templates", [UI_MENU_DESC_TEMPLATE_DESC] = "Optimisation: updates descriptors in bulk.", [UI_MENU_PUSH_SKIP] = "Skip repeated push constants", @@ -273,6 +274,7 @@ static const char *const text_ru[UI_TEXT_COUNT] = { [UI_MENU_BIND_SKIP_DESC] = "Оптимизация: пропускает неизменившиеся привязки.", [UI_MENU_SHADER_MEMO] = "Память шейдеров", [UI_MENU_SHADER_MEMO_DESC] = "Оптимизация: запоминает поиск шейдеров.", [UI_MENU_PIPE_MEMO] = "Память конвейера", [UI_MENU_PIPE_MEMO_DESC] = "Оптимизация: повторяет конвейер прошлой отрисовки, если её состояние не изменилось.", + [UI_MENU_TEX_MEMO] = "Память привязок текстур", [UI_MENU_TEX_MEMO_DESC] = "Оптимизация: запоминает дескрипторы текстур между отрисовками.", [UI_MENU_DESC_TEMPLATE] = "Шаблоны дескрипторов", [UI_MENU_DESC_TEMPLATE_DESC] = "Оптимизация: обновляет дескрипторы пачками.", [UI_MENU_PUSH_SKIP] = "Пропуск повторных push-констант", diff --git a/runtime/ui_text.h b/runtime/ui_text.h index 7860bbd7..3a8dc234 100644 --- a/runtime/ui_text.h +++ b/runtime/ui_text.h @@ -85,6 +85,7 @@ typedef enum { UI_MENU_BIND_SKIP, UI_MENU_BIND_SKIP_DESC, UI_MENU_SHADER_MEMO, UI_MENU_SHADER_MEMO_DESC, UI_MENU_PIPE_MEMO, UI_MENU_PIPE_MEMO_DESC, + UI_MENU_TEX_MEMO, UI_MENU_TEX_MEMO_DESC, UI_MENU_DESC_TEMPLATE, UI_MENU_DESC_TEMPLATE_DESC, UI_MENU_PUSH_SKIP, UI_MENU_PUSH_SKIP_DESC, UI_MENU_SPLIT_DRAWS, UI_MENU_SPLIT_DRAWS_DESC, diff --git a/runtime/vk_exec_draw.c b/runtime/vk_exec_draw.c index 9c9f25a1..3c74c7c4 100644 --- a/runtime/vk_exec_draw.c +++ b/runtime/vk_exec_draw.c @@ -999,7 +999,48 @@ static void feedback_written(VkExec *ex, const VkPipeState *s) { if (ex->color[i].image && !(ex->fb_dirty >> i & 1) && vk_pipeline_target_wmask(s, (int)i)) ex->fb_dirty |= 1u << i; } +static void tex_info_full(VkExec *ex, const GfxShaderRes *r, uint64_t h, int storage, int separate, + VkDescriptorImageInfo *out); + +// Sampled-texture descriptors per (handle, resource type, separate), kept while nothing they derive from changed: every +// slot's resolution and texture entry (vk_texture_slots_gen), the samplers (vk_sampler_gen), the submission +// (vk_texture_epoch: the first binding of a slot in a submission checks its storage) and, in a pass with images in the +// feedback layout, the pass. Draws bind the same textures thousands of times a frame; a hit skips the slot lookup, the +// view key with its hash probe and the sampler lookup. Storage images, which note their binding per submission, are +// not kept. Direct-mapped; texture lock held. TOTK_TEX_MEMO=0 turns it off; =check computes every hit again and stops +// on a difference. +#define TD_N 4096 +static struct { uint64_t h; uint32_t gen, epoch, sgen, pass; uint16_t type; uint8_t separate, valid; VkDescriptorImageInfo out; } TD[TD_N]; +static int td_on = -1; + static void tex_info(VkExec *ex, const GfxShaderRes *r, uint64_t h, int storage, int separate, VkDescriptorImageInfo *out) { + if (td_on < 0) { + const char *e = cfg_value("TOTK_TEX_MEMO"); + td_on = !strcmp(e, "check") ? 2 : strcmp(e, "0") != 0; + } + if (!h || storage || !td_on) return tex_info_full(ex, r, h, storage, separate, out); + uint32_t gen = vk_texture_slots_gen(), ep = vk_texture_epoch(), sg = vk_sampler_gen(); // before any lookup + uint32_t pass = ex->nfbl ? ex->pass_serial | 0x80000000u : 0; + typeof(TD[0]) *m = &TD[(h * 0x9E3779B97F4A7C15ull ^ (uint64_t)r->type << 7 ^ (uint64_t)separate) >> 52]; + if (m->valid && m->h == h && m->type == r->type && m->separate == separate && m->gen == gen && m->epoch == ep && + m->sgen == sg && m->pass == pass) { + *out = m->out; + if (td_on == 2) { + VkDescriptorImageInfo full; + tex_info_full(ex, r, h, storage, separate, &full); + if (full.sampler != out->sampler || full.imageView != out->imageView || full.imageLayout != out->imageLayout) + rt_fatal("vk_exec: texture descriptor memo of handle %#llx type %#x differs from a lookup", + (unsigned long long)h, r->type); + } + return; + } + tex_info_full(ex, r, h, storage, separate, out); + *m = (typeof(*m)){.h = h, .gen = gen, .epoch = ep, .sgen = sg, .pass = pass, .type = r->type, + .separate = (uint8_t)separate, .valid = 1, .out = *out}; +} + +static void tex_info_full(VkExec *ex, const GfxShaderRes *r, uint64_t h, int storage, int separate, + VkDescriptorImageInfo *out) { int ms = (r->type & 0x200) != 0; VkImageView view = VK_NULL_HANDLE; VkSampler samp = VK_NULL_HANDLE;