diff --git a/runtime/cfg.c b/runtime/cfg.c index 6e413232..c6836ff6 100644 --- a/runtime/cfg.c +++ b/runtime/cfg.c @@ -26,7 +26,7 @@ static const char *const known[] = { "APP_POOL", "ASSUME_FOCUS", "AUDIO", "AUDIO_OFF", "AUDIO_PREFILL_MS", "BACKGROUND_WINDOW", "BIND_A", "BIND_B", "BIND_DOWN", "BIND_L", "BIND_LCLICK", "BIND_LEFT", "BIND_MINUS", "BIND_PLUS", "BIND_R", "BIND_RCLICK", "BIND_RIGHT", "BIND_SKIP", "BIND_STATS", "BIND_UP", "BIND_X", "BIND_Y", "BIND_ZL", "BIND_ZR", - "CLOTH_REALTIME", "DUMP", "DUMP_BUF", "DUMP_DIR", "DUMP_LOG", "EXEFS", "FEEDBACK", "FIX_C0", "FIX_ZERO", "FLIGHT", + "CLOTH_REALTIME", "DESC_TEMPLATE", "DUMP", "DUMP_BUF", "DUMP_DIR", "DUMP_LOG", "EXEFS", "FEEDBACK", "FIX_C0", "FIX_ZERO", "FLIGHT", "FLIGHT_AT", "FLIGHT_DIR", "FLIGHT_FRAMES", "FLIGHT_SPIKE_MS", "FLIGHT_THUMBS", "FPS60", "FREE_CORES", "FREEZE_IDLE", "GPL", "GPL_DRAW_BUILD", "GPU", "GPU_INDEX", "GPU_NAME", "HIGHFPS_FIX", "INPUT", "INPUT_RECORD", "INPUT_SCRIPT", "LANG", "LIB_PREBUILD", diff --git a/runtime/vk.h b/runtime/vk.h index 9033c69c..692eb319 100644 --- a/runtime/vk.h +++ b/runtime/vk.h @@ -347,7 +347,13 @@ typedef struct { VkDescriptorSetLayout set_layout1; int push1; uint32_t n_sets; + // Descriptor update template per set (vk_pipeline.c make_tpl): reads one VkDescSlot per descriptor, in binding order, + // from the start of the set's descriptors. VK_NULL_HANDLE: the set is written with VkWriteDescriptorSet structs. + VkDescriptorUpdateTemplate tpl[2]; } VkPipeInfo; +// One descriptor of the packed array a descriptor update template reads (VkPipeInfo.tpl), 24 bytes for every type; the +// bytes a type leaves unused are zero, so equal descriptors compare equal byte for byte. +typedef union { VkDescriptorImageInfo image; VkDescriptorBufferInfo buffer; VkBufferView view; uint64_t w[3]; } VkDescSlot; // Both return the cached pipeline (created on first use; failures are cached and logged once). info: may be NULL. // Valid until vk_pipeline_shutdown; thread-safe. VkPipeline vk_pipeline_graphics(const VkPipeState *s, const VkPipeInfo **info); @@ -381,6 +387,10 @@ VkPrimitiveTopology vk_pipeline_topology(int nvn_prim); // quads -> triangle l // rewrites those draws; polygon -> fan VkFormat vk_vertex_format(int nvn_format, int *type); // attribute format (supported fallback), GFX_TYPE_* in *type PFN_vkCmdPushDescriptorSetKHR vk_pipeline_push_fn(void); // after the first vk_pipeline_* call +// Descriptor update templates (after the first vk_pipeline_* call): NULL when templates are off (TOTK_DESC_TEMPLATE=0, +// a device below Vulkan 1.1) or, for the push variant, vkCmdPushDescriptorSetWithTemplateKHR is missing. +PFN_vkCmdPushDescriptorSetWithTemplateKHR vk_pipeline_push_tpl_fn(void); +PFN_vkUpdateDescriptorSetWithTemplate vk_pipeline_update_tpl_fn(void); // Probe only: 1 selects stage-independent layouts for pipelines looked up from now on, 0 the shared set 0; returns // the previous mode. 1 needs vk_caps.gpl (the independent-sets flag comes with the extension). int vk_pipeline_set_indep(int on); diff --git a/runtime/vk_exec_draw.c b/runtime/vk_exec_draw.c index c7d86276..a751ea01 100644 --- a/runtime/vk_exec_draw.c +++ b/runtime/vk_exec_draw.c @@ -121,12 +121,12 @@ static struct { int res_valid; // the graphics sets bound hold what the inputs in RS give (bind_resources) } B; -// Contents of B.ds_last: the writes' bindings, types, counts and descriptor infos. A draw whose allocated set would -// hold the same reuses the set instead of allocating and writing one (Task 2.2's per-draw cost). psig: the same for -// the last push per set number; a push equal to it under the same layout is not recorded again (the descriptors stay -// bound across pipeline binds of that layout). -#define DS_SIG_BYTES (MAX_DESC * 24 + MAX_BINDINGS * 12) -static struct { uint32_t n[2], pn[2]; uint8_t sig[2][DS_SIG_BYTES], psig[2][DS_SIG_BYTES], tmp[DS_SIG_BYTES]; } DS; +// Contents of B.ds_last: the set's packed descriptors (VkDescSlot), which with its layout determine every write. A draw +// whose allocated set would hold the same reuses the set instead of allocating and writing one (Task 2.2's per-draw +// cost). psig: the same for the last push per set number; a push equal to it under the same layout is not recorded +// again (the descriptors stay bound across pipeline binds of that layout). +#define DS_SIG_BYTES (MAX_DESC * sizeof(VkDescSlot)) +static struct { uint32_t n[2], pn[2]; uint8_t sig[2][DS_SIG_BYTES], psig[2][DS_SIG_BYTES]; } DS; // TOTK_BIND_STATS counters (bind_stats). static struct { @@ -151,7 +151,7 @@ static struct { RsCount cnt[2 * VK_MCLS_N], pcnt[2 * VK_MCLS_N]; int ncnt, pncnt, cnt_over; int cmds; // check mode: pushes, binds and set writes the build recorded - uint32_t ckn; // check mode: ds_sig of the last build's writes + uint32_t ckn; // check mode: the last build's packed descriptors (bytes in ck) uint8_t ck[DS_SIG_BYTES]; } RS = {-1}; @@ -1039,30 +1039,6 @@ static void repeat_stats(const VkExec *ex, const VkPipeInfo *info) { last = cur; } -// The signature of a set's writes into out (DS layout): bindings, types, counts and descriptor infos. Returns its size. -static uint32_t ds_sig(const VkWriteDescriptorSet *w, uint32_t nw, uint8_t *out) { - uint32_t n = 0; - for (uint32_t k = 0; k < nw; k++) { - const VkWriteDescriptorSet *x = &w[k]; - uint32_t hdr[3] = {x->dstBinding, (uint32_t)x->descriptorType, x->descriptorCount}; - memcpy(out + n, hdr, sizeof hdr), n += sizeof hdr; - if (x->pImageInfo) { // field by field: the struct's tail padding is not written - for (uint32_t d = 0; d < x->descriptorCount; d++) { - const VkDescriptorImageInfo *ii1 = &x->pImageInfo[d]; - memcpy(out + n, &ii1->sampler, 8), memcpy(out + n + 8, &ii1->imageView, 8); - memcpy(out + n + 16, &ii1->imageLayout, 4), n += 20; - } - } else if (x->pBufferInfo) { - memcpy(out + n, x->pBufferInfo, sizeof *x->pBufferInfo * x->descriptorCount); - n += (uint32_t)(sizeof *x->pBufferInfo * x->descriptorCount); - } else { - memcpy(out + n, x->pTexelBufferView, sizeof *x->pTexelBufferView * x->descriptorCount); - n += (uint32_t)(sizeof *x->pTexelBufferView * x->descriptorCount); - } - } - return n; -} - static int push_skip_on(void) { // TOTK_PUSH_SKIP=0: every graphics draw pushes its set again static int on = -1; if (on < 0) { @@ -1196,14 +1172,28 @@ static int bind_sig(const VkExec *ex, const VkPipeInfo *info) { return !diff && !bd; } +// Writes allocated set s from its packed descriptors d: through the layout's update template, or with the write +// structs w[i0..i1) when it has none. +static void write_set(const VkPipeInfo *info, uint32_t s, VkDescriptorSet set, const VkDescSlot *d, VkWriteDescriptorSet *w, + uint32_t i0, uint32_t i1) { + if (i1 == i0) return; + if (info->tpl[s]) { + vk_pipeline_update_tpl_fn()(vk_ctx()->device, set, info->tpl[s], d); + return; + } + for (uint32_t k = i0; k < i1; k++) w[k].dstSet = set; + vkUpdateDescriptorSets(vk_ctx()->device, i1 - i0, w + i0, 0, NULL); +} + // Writes the pipeline's sets from the bound state: set 0, and set 1 with independent layouts. Each is pushed into the // command buffer, or allocated, written and bound; a graphics set whose contents equal the last one allocated for that // set number in this command buffer is reused, and bound again only when another layout or a push came between. A // graphics push equal to the last one at its set number, with no other layout or bind between, is not recorded. +// The descriptors go into one packed array d, a VkDescSlot per descriptor in binding order, which a set's update +// template reads directly (VkPipeInfo.tpl); write structs are filled only when a set has no template. static void bind_resources(VkExec *ex, const VkPipeInfo *info) { static VkWriteDescriptorSet w[MAX_BINDINGS]; - static VkDescriptorBufferInfo bi[MAX_DESC]; - static VkDescriptorImageInfo ii[MAX_DESC]; + static VkDescSlot d[MAX_DESC]; static VkBufferView bv[MAX_DESC]; if (info->n_bindings > MAX_BINDINGS) rt_fatal("vk: %u descriptor bindings exceed the replay's %d", info->n_bindings, MAX_BINDINGS); bind_stats(info); @@ -1221,15 +1211,24 @@ static void bind_resources(VkExec *ex, const VkPipeInfo *info) { if (skip == 2) memcpy(RS.pcnt, RS.cnt, sizeof RS.cnt), RS.pncnt = RS.ncnt; RS.rec = 1, RS.ncnt = 0, RS.cnt_over = 0, RS.volatile_ = 0, RS.cmds = 0; } + uint32_t n_sets = info->n_sets ? info->n_sets : 1; + uint32_t lo[2] = {0, 0}, hi[2] = {0, 0}, d0[2] = {0, 0}; // bindings [lo, hi) and first descriptor per set + int need_w = 0; // a set without a template: fill the write structs + for (uint32_t s = 0, i0 = 0; s < n_sets; s++) { + uint32_t i1 = i0; + while (i1 < info->n_bindings && (n_sets == 1 || info->bindings[i1].set == s)) i1++; + lo[s] = i0, hi[s] = i1, i0 = i1; + if (i1 > lo[s] && !info->tpl[s]) need_w = 1; + } uint32_t nd = 0; for (uint32_t i = 0; i < info->n_bindings; i++) { const VkPipeBinding *b = &info->bindings[i]; const GfxShaderRes *r = b->res; uint32_t cnt = b->count ? b->count : 1; if (nd + cnt > MAX_DESC) rt_fatal("vk: %u descriptors exceed the replay's %d", nd + cnt, MAX_DESC); - w[i] = (VkWriteDescriptorSet){.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET, - .dstBinding = b->binding, .descriptorCount = cnt, .descriptorType = b->type}; - int st = nvn_stage_of(b->stage); + if (n_sets == 2 && i == lo[1]) d0[1] = nd; + VkDescSlot *o = &d[nd]; + int st = nvn_stage_of(b->stage), kind = 0; // kind: 0 buffer, 1 image, 2 texel buffer view if (b->kind == VK_PIPE_SUPPORT) { float in[3]; support_in(ex, in); @@ -1245,37 +1244,45 @@ static void bind_resources(VkExec *ex, const VkPipeInfo *info) { B.sup_buf = sb, B.sup_off = so, B.sup_valid = 1; } } - bi[nd] = (VkDescriptorBufferInfo){B.sup_buf, B.sup_off, SUPPORT_BYTES}; - w[i].pBufferInfo = &bi[nd]; + o->buffer = (VkDescriptorBufferInfo){B.sup_buf, B.sup_off, SUPPORT_BYTES}; } else if (b->kind == GFX_RES_UBO) { - for (uint32_t k = 0; k < cnt; k++) bi[nd + k] = ubo_info(ex, st, r->slot + (int)k); - w[i].pBufferInfo = &bi[nd]; + for (uint32_t k = 0; k < cnt; k++) o[k].buffer = ubo_info(ex, st, r->slot + (int)k); } else if (b->kind == GFX_RES_SSBO) { - for (uint32_t k = 0; k < cnt; k++) bi[nd + k] = ssbo_info(ex, st, r, r->flags & 1); - w[i].pBufferInfo = &bi[nd]; + for (uint32_t k = 0; k < cnt; k++) o[k].buffer = ssbo_info(ex, st, r, r->flags & 1); } else if (b->kind == GFX_RES_TEX || b->kind == GFX_RES_IMG) { int img = b->kind == GFX_RES_IMG; if (b->type == VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER || b->type == VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER) { - for (uint32_t k = 0; k < cnt; k++) bv[nd + k] = Z.zview; // buffer textures are not replayed yet - w[i].pTexelBufferView = &bv[nd]; + kind = 2; + for (uint32_t k = 0; k < cnt; k++) { // buffer textures are not replayed yet + o[k].w[0] = 0, o[k].w[1] = 0, o[k].w[2] = 0; + o[k].view = Z.zview; + if (need_w) bv[nd + k] = Z.zview; + } } else { + kind = 1; vk_texture_lock(); // tex_info looks up with the texture lock held for (uint32_t k = 0; k < cnt; k++) { int slot = r->slot + (int)k; uint64_t h = 0; if (img) h = slot >= 0 && slot < 32 ? ex->img[st][slot] : 0; else h = slot >= 0 && slot < 128 ? ex->tex[st][slot] : 0; - tex_info(ex, r, h, img, r->separate, &ii[nd + k]); + tex_info(ex, r, h, img, r->separate, &o[k].image); + o[k].w[2] = (uint32_t)o[k].image.imageLayout; // zero the tail padding } vk_texture_unlock(); - w[i].pImageInfo = &ii[nd]; } } else { rt_fatal("vk: binding %u has an unknown kind %d", (unsigned)b->binding, (int)b->kind); } + if (need_w) { + w[i] = (VkWriteDescriptorSet){.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET, + .dstBinding = b->binding, .descriptorCount = cnt, .descriptorType = b->type}; + if (kind == 0) w[i].pBufferInfo = &o->buffer; // a VkDescSlot is as large as either info struct + else if (kind == 1) w[i].pImageInfo = &o->image; + else w[i].pTexelBufferView = &bv[nd]; + } nd += cnt; } - uint32_t n_sets = info->n_sets ? info->n_sets : 1; // Under the validation layer, a new graphics layout binds set 1 before set 0 is pushed or bound. The layer (SDK // 1.4.363, PipelineLayoutCompatDef::operator==) takes any two INDEPENDENT_SETS layouts as compatible, so // vkCmdPushDescriptorSetKHR keeps the push set it made for an earlier layout and drops writes to bindings that @@ -1288,14 +1295,10 @@ static void bind_resources(VkExec *ex, const VkPipeInfo *info) { B.ds_pushed[0] = B.ds_pushed[1] = 0; set1_first = n_sets == 2 && vk_ctx()->validation; } - uint32_t lo[2] = {0, 0}, hi[2] = {0, 0}; - for (uint32_t s = 0, i0 = 0; s < n_sets; s++) { - uint32_t i1 = i0; - while (i1 < info->n_bindings && (n_sets == 1 || info->bindings[i1].set == s)) i1++; - lo[s] = i0, hi[s] = i1, i0 = i1; - } for (uint32_t k = 0; k < n_sets; k++) { uint32_t s = set1_first ? 1 - k : k, i0 = lo[s], i1 = hi[s]; + const VkDescSlot *ds = &d[d0[s]]; + uint32_t dbytes = (uint32_t)(((s + 1 < n_sets ? d0[s + 1] : nd) - d0[s]) * sizeof *d); if (s && i1 == i0) { // no fragment stage: set 1 is empty and unused if (set1_first) { // bound only to drop the layer's push set (comment above) VkDescriptorSet set = alloc_set(ex, info->set_layout1); @@ -1306,33 +1309,31 @@ static void bind_resources(VkExec *ex, const VkPipeInfo *info) { continue; } if (s ? info->push1 : info->push) { - uint32_t n = gfx && push_skip_on() ? ds_sig(w + i0, i1 - i0, DS.tmp) : 0; - if (n && B.ds_pushed[s] && DS.pn[s] == n && !memcmp(DS.psig[s], DS.tmp, n)) { + uint32_t n = gfx && push_skip_on() ? dbytes : 0; + if (n && B.ds_pushed[s] && DS.pn[s] == n && !memcmp(DS.psig[s], ds, n)) { BS.push_skip++; continue; } - vk_pipeline_push_fn()(ex->cb, info->bind_point, info->layout, s, i1 - i0, w + i0); + if (info->tpl[s]) vk_pipeline_push_tpl_fn()(ex->cb, info->tpl[s], info->layout, s, ds); + else vk_pipeline_push_fn()(ex->cb, info->bind_point, info->layout, s, i1 - i0, w + i0); BS.push++; RS.cmds++; if (gfx) { B.ds_bound[s] = VK_NULL_HANDLE, B.ds_pushed[s] = n != 0; - if (n) DS.pn[s] = n, memcpy(DS.psig[s], DS.tmp, n); + if (n) DS.pn[s] = n, memcpy(DS.psig[s], ds, n); } } else if (!gfx || s > 1) { VkDescriptorSet set = alloc_set(ex, s ? info->set_layout1 : info->set_layout); - for (uint32_t k = i0; k < i1; k++) w[k].dstSet = set; - if (i1 > i0) vkUpdateDescriptorSets(vk_ctx()->device, i1 - i0, w + i0, 0, NULL); + write_set(info, s, set, ds, w, i0, i1); vkCmdBindDescriptorSets(ex->cb, info->bind_point, info->layout, s, 1, &set, 0, NULL); } else { VkDescriptorSetLayout sl = s ? info->set_layout1 : info->set_layout; - uint32_t n = ds_sig(w + i0, i1 - i0, DS.tmp); VkDescriptorSet set = B.ds_last[s]; - if (!set || B.ds_last_layout[s] != sl || DS.n[s] != n || memcmp(DS.sig[s], DS.tmp, n)) { + if (!set || B.ds_last_layout[s] != sl || DS.n[s] != dbytes || memcmp(DS.sig[s], ds, dbytes)) { set = alloc_set(ex, sl); - for (uint32_t k = i0; k < i1; k++) w[k].dstSet = set; - if (i1 > i0) vkUpdateDescriptorSets(vk_ctx()->device, i1 - i0, w + i0, 0, NULL); - B.ds_last[s] = set, B.ds_last_layout[s] = sl, DS.n[s] = n; - memcpy(DS.sig[s], DS.tmp, n); + write_set(info, s, set, ds, w, i0, i1); + B.ds_last[s] = set, B.ds_last_layout[s] = sl, DS.n[s] = dbytes; + memcpy(DS.sig[s], ds, dbytes); BS.set_new++; RS.cmds++; } else { @@ -1352,9 +1353,9 @@ static void bind_resources(VkExec *ex, const VkPipeInfo *info) { int valid = !RS.volatile_ && !RS.cnt_over && RS.hn; for (uint32_t s = 0; s < n_sets; s++) if (hi[s] > lo[s] && (s ? info->push1 : info->push) && !B.ds_pushed[s]) valid = 0; - if (skip == 2) { - uint32_t n = ds_sig(w, info->n_bindings, DS.tmp); - int wr = n != RS.ckn || memcmp(RS.ck, DS.tmp, n); + if (skip == 2) { // with the pipeline info equal (bind_sig), the packed descriptors determine every write + uint32_t n = (uint32_t)(nd * sizeof *d); + int wr = n != RS.ckn || memcmp(RS.ck, d, n); int cn = RS.ncnt != RS.pncnt || memcmp(RS.cnt, RS.pcnt, sizeof RS.cnt[0] * (size_t)RS.ncnt); if (same && was_valid && (wr || cn || RS.cmds)) rt_fatal("vk_exec: descriptor build skip check: a draw of program %llu with the inputs of the previous one " @@ -1362,7 +1363,7 @@ static void bind_resources(VkExec *ex, const VkPipeInfo *info) { RS.cmds ? "descriptor commands" : wr ? "other writes" : "other census counts"); if (same && was_valid) BS.bind_skip++; // the draws a skip would have spared, compared instead RS.ckn = n; - memcpy(RS.ck, DS.tmp, n); + memcpy(RS.ck, d, n); } B.res_valid = valid; } diff --git a/runtime/vk_pipeline.c b/runtime/vk_pipeline.c index 50b299b6..7ea6c2d5 100644 --- a/runtime/vk_pipeline.c +++ b/runtime/vk_pipeline.c @@ -88,6 +88,11 @@ static struct { VkDynamicState dyn[32]; uint32_t n_dyn; PFN_vkCmdPushDescriptorSetKHR push; + int tpl; // descriptor update templates on (make_tpl) + PFN_vkCreateDescriptorUpdateTemplate tpl_create; + PFN_vkDestroyDescriptorUpdateTemplate tpl_destroy; + PFN_vkUpdateDescriptorSetWithTemplate tpl_update; + PFN_vkCmdPushDescriptorSetWithTemplateKHR tpl_push; PFN_vkCmdSetVertexInputEXT set_vi; PFN_vkCmdSetDepthClampEnableEXT set_clamp; PFN_vkCmdSetPolygonModeEXT set_poly; @@ -110,6 +115,7 @@ typedef struct Layout { int push1; // set 1 is a push-descriptor set uint32_t n0; // independent layouts: b[0..n0) are set 0, the rest set 1 VkPipelineLayout half[2]; // GPL libraries: {set 0, null}, {null, set 1} (made on first use) + VkDescriptorUpdateTemplate tpl[2]; // per set (make_tpl); VK_NULL_HANDLE: written with write structs VkDescriptorSetLayoutBinding b[]; } Layout; @@ -242,6 +248,20 @@ static int ensure_init(void) { P.f_dual_src = f2.features.dualSrcBlend; #define PROC(field, name) (P.field = (void *)vkGetDeviceProcAddr(c->device, name)) PROC(push, "vkCmdPushDescriptorSetKHR"); + // Descriptor update templates (core 1.1; the push variant comes with VK_KHR_push_descriptor on 1.1): the driver + // walks a layout prepared when the layout is made instead of parsing write structs every draw (DXVK does so for the + // same reason). TOTK_DESC_TEMPLATE=0 keeps the write structs. + const char *dt = cfg_get("TOTK_DESC_TEMPLATE"); + if (dt && !strcmp(dt, "0")) { + rt_log("vk_pipeline: descriptor update templates off (TOTK_DESC_TEMPLATE=0)"); + } else if (c->props.apiVersion >= VK_API_VERSION_1_1 && PROC(tpl_create, "vkCreateDescriptorUpdateTemplate") && + PROC(tpl_destroy, "vkDestroyDescriptorUpdateTemplate") && PROC(tpl_update, "vkUpdateDescriptorSetWithTemplate")) { + P.tpl = 1; + if (!PROC(tpl_push, "vkCmdPushDescriptorSetWithTemplateKHR")) + rt_log("vk_pipeline: vkCmdPushDescriptorSetWithTemplateKHR missing: push sets are written with write structs"); + } else { + rt_log("vk_pipeline: descriptor update templates unavailable: sets are written with write structs"); + } // TOTK_VK_DYN_VERTEX=0/1 forces dynamic vertex input off or on. Default off on Intel's frozen Windows driver for its // older integrated GPUs (UHD 630, Ice Lake: Vulkan 1.3.215 and older), which draws nothing with it (LAS Recomp // 61fe44ff, its issues #8 and #357). @@ -588,6 +608,8 @@ static void vertex_input(const VkPipeState *s, VtxInput *v) { int vk_pipeline_vertex_input_dynamic(void) { return ensure_init() && P.dyn_vi; } PFN_vkCmdPushDescriptorSetKHR vk_pipeline_push_fn(void) { return ensure_init() ? P.push : NULL; } +PFN_vkCmdPushDescriptorSetWithTemplateKHR vk_pipeline_push_tpl_fn(void) { return ensure_init() && P.tpl ? P.tpl_push : NULL; } +PFN_vkUpdateDescriptorSetWithTemplate vk_pipeline_update_tpl_fn(void) { return ensure_init() && P.tpl ? P.tpl_update : NULL; } int vk_pipeline_set_indep(int on) { if (!ensure_init()) return 0; @@ -954,6 +976,37 @@ static int layout_covers(const VkPipeState *s, const Layout *l, uint32_t fs_off, return m < 0; } +// The update template of a set whose bindings are b[0..n): one entry per binding, in order, over the packed array the +// replay fills from the set's first descriptor, a VkDescSlot per descriptor. The bind point follows the stages (compute +// layouts hold the compute stage only). VK_NULL_HANDLE when templates are off, for an empty set, for a push set +// without vkCmdPushDescriptorSetWithTemplateKHR or when creation fails: the replay then writes the set with write +// structs. +static VkDescriptorUpdateTemplate make_tpl(const VkDescriptorSetLayoutBinding *b, uint32_t n, VkDescriptorSetLayout sl, + int push, VkPipelineLayout pl, uint32_t set) { + if (!P.tpl || !n || (push && !P.tpl_push)) return VK_NULL_HANDLE; + VkDescriptorUpdateTemplateEntry e[MAX_BINDINGS]; + size_t off = 0; + VkShaderStageFlags stages = 0; + for (uint32_t i = 0; i < n; i++) { + e[i] = (VkDescriptorUpdateTemplateEntry){b[i].binding, 0, b[i].descriptorCount, b[i].descriptorType, off, sizeof(VkDescSlot)}; + off += sizeof(VkDescSlot) * b[i].descriptorCount; + stages |= b[i].stageFlags; + } + VkDescriptorUpdateTemplateCreateInfo ci = { + .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_UPDATE_TEMPLATE_CREATE_INFO, .descriptorUpdateEntryCount = n, + .pDescriptorUpdateEntries = e, + .templateType = push ? VK_DESCRIPTOR_UPDATE_TEMPLATE_TYPE_PUSH_DESCRIPTORS_KHR : VK_DESCRIPTOR_UPDATE_TEMPLATE_TYPE_DESCRIPTOR_SET, + .descriptorSetLayout = sl, + .pipelineBindPoint = stages & VK_SHADER_STAGE_COMPUTE_BIT ? VK_PIPELINE_BIND_POINT_COMPUTE : VK_PIPELINE_BIND_POINT_GRAPHICS, + .pipelineLayout = pl, .set = set}; + VkDescriptorUpdateTemplate t; + if (P.tpl_create(P.device, &ci, NULL, &t) != VK_SUCCESS) { + LOG_ONCE("vk_pipeline: descriptor update template creation failed: that set is written with write structs"); + return VK_NULL_HANDLE; + } + return t; +} + // Finds or creates the layout of a binding list (sorted here); NULL on failure. Caller holds P.lock. // Sorts a binding list by binding and hashes it into *h; the descriptor total, or -1 when a binding repeats. static int64_t bindings_sort(Bindings *B, uint64_t *h) { @@ -998,6 +1051,7 @@ static Layout *layout_get(Bindings *B) { free(l); return NULL; } + l->tpl[0] = make_tpl(l->b, l->n, l->set, l->push, l->layout, 0); l->next = *head; *head = l; P.stats.layouts++; @@ -1055,6 +1109,8 @@ static Layout *layout_pair(Bindings *B, Bindings *F) { free(l); return NULL; } + l->tpl[0] = make_tpl(l->b, n0, l->set, l->push, l->layout, 0); + l->tpl[1] = make_tpl(l->b + n0, n - n0, l->set1, l->push1, l->layout, 1); l->next = *head; *head = l; P.stats.layouts++; @@ -1127,6 +1183,7 @@ static Pipe *pipe_add(const void *key, size_t size, uint64_t h, VkPipeline pipel p->info.set_layout1 = l ? l->set1 : VK_NULL_HANDLE; p->info.push1 = l ? l->push1 : 0; p->info.n_sets = l && l->set1 ? 2 : 1; + if (l) p->info.tpl[0] = l->tpl[0], p->info.tpl[1] = l->tpl[1]; p->lay = l; Pipe **head = &pipes[h % PIPE_BUCKETS]; p->next = *head; @@ -2823,6 +2880,8 @@ void vk_pipeline_shutdown(void) { for (int i = 0; i < LAYOUT_BUCKETS; i++) { for (Layout *l = layouts[i], *n; l; l = n) { n = l->next; + for (int k = 0; k < 2; k++) + if (l->tpl[k]) P.tpl_destroy(P.device, l->tpl[k], NULL); vkDestroyPipelineLayout(P.device, l->layout, NULL); for (int h = 0; h < 2; h++) if (l->half[h]) vkDestroyPipelineLayout(P.device, l->half[h], NULL);