From 38a3824b8fa14c86a763be0c5071c9cbe3fa8900 Mon Sep 17 00:00:00 2001 From: Tobim6 <102646588+Tobim6@users.noreply.github.com> Date: Thu, 12 Nov 2026 16:22:37 +0100 Subject: [PATCH] Mesh emulation: cut the part at a pass's first emulated draw instead of splitting it, so the texture pool set stays. A split hands the part to the queue and seals its texture pool set; the next part takes another set of the ring and refreshes its entries first. The splits for the mesh stages made 5 parts a submission and 6 set switches a frame at 2.3 ms of replay each (Kokiri Forest on 0.7.1, an RX 580: replay 32-38 ms a frame, the replay thread blocked on fences 26-35% of the time, 79 waits for a set the GPU still used). A cut (vk_sync_cut) ends the command buffer without submitting it: the part and its pre-pass command buffer wait in a chain and go to the queue, in order, with the next part that does, so the set is the same one throughout. Co-Authored-By: Claude Opus 5.5 --- runtime/vk.h | 3 ++ runtime/vk_exec.c | 17 ++++++++--- runtime/vk_exec.h | 2 ++ runtime/vk_exec_draw.c | 3 ++ runtime/vk_sync.c | 67 +++++++++++++++++++++++++++++++++++------- 5 files changed, 77 insertions(+), 15 deletions(-) diff --git a/runtime/vk.h b/runtime/vk.h index 8564149c..a56fe2bf 100644 --- a/runtime/vk.h +++ b/runtime/vk.h @@ -753,6 +753,9 @@ void vk_sync_worker_end(VkCommandBuffer cb); // The part's pre-pass command buffer: submitted right before the part being recorded (after its mirror prologue), begun // with a full barrier at the first call of a part; VK_NULL_HANDLE when it cannot be made. Replay thread, sub_lock held. VkCommandBuffer vk_sync_pre(void); +// Ends the part being recorded without handing it to the queue (it goes with the next part that does, in order) and +// returns the command buffer the replay continues in; VK_NULL_HANDLE when no more parts can wait. Replay thread. +VkCommandBuffer vk_sync_cut(void); extern int vk_sub_in_frame; // replay: the submission being recorded is the frame's n-th (0 the first; vk_backend.c) // Runs fn(arg) on the completion thread once fence is done on the GPU (event writes, counter reports, sync state). // Callbacks run in the order they were added, after those of earlier values; a value already done runs at once there. diff --git a/runtime/vk_exec.c b/runtime/vk_exec.c index 6f7dd7f2..5fcef4c4 100644 --- a/runtime/vk_exec.c +++ b/runtime/vk_exec.c @@ -1111,8 +1111,17 @@ static void set_rt(VkExec *ex, const p_set_rt *r) { // command, it was 4% of the replay in Kokiri Forest. static int split_n = -1, split_idle, split_in_pass, split_draws, split_at, part_start; static uint64_t split_ns, part_t0; -static uint64_t n_parts, n_subs, n_idle_parts, n_pass_splits; +static uint64_t n_parts, n_subs, n_idle_parts, n_pass_splits, n_cuts; + int vk_exec_split_now(VkExec *ex) { + int in_pass = ex->in_pass; + if (in_pass) vk_exec_pass_end(ex); + VkCommandBuffer cb = vk_sync_cut(); // not handed to the queue: the texture pool set stays (vk_exec_pool_split) + if (!cb) return 0; + ex->cb = cb; + ex->dirty = VK_EXEC_DIRTY_ALL; + vk_exec_draw_new_cb(); + n_cuts++; return 1; } @@ -2583,10 +2592,10 @@ int vk_exec_record(VkCommandBuffer cb, const uint64_t *handles, int n, uint64_t if (n) n_subs++; if ((rt_trace & RT_TRACE_PERF) && n_subs >= 240) { rt_log("perf: replay command buffer parts per submission %.2f (%.2f first parts sent while the GPU was idle, %.2f " - "splits inside a pass); event order: held %llu, resumed %llu (%llu without their signal), switches %llu, " + "splits inside a pass, %.2f cuts for emulated mesh stages); event order: held %llu, resumed %llu (%llu without their signal), switches %llu, " "held now %d; waits a CPU signal met only after the submission's copies were taken: %llu", (double)(n_subs + n_parts) / (double)n_subs, (double)n_idle_parts / (double)n_subs, - (double)n_pass_splits / (double)n_subs, (unsigned long long)evo_held, (unsigned long long)evo_resumed, + (double)n_pass_splits / (double)n_subs, (double)n_cuts / (double)n_subs, (unsigned long long)evo_held, (unsigned long long)evo_resumed, (unsigned long long)evo_forced, (unsigned long long)evo_switches, evo_pending, (unsigned long long)vk_exec_event_late_take()); uint64_t xw, xe, xl, xm, le, lc, lo; @@ -2678,7 +2687,7 @@ int vk_exec_record(VkCommandBuffer cb, const uint64_t *handles, int n, uint64_t } RW.groups = RW.group_draws = RW.chunks = RW.serial_draws = RW.wait_ns = 0; evo_held = evo_resumed = evo_forced = evo_switches = 0; - n_subs = n_parts = n_idle_parts = n_pass_splits = 0; + n_subs = n_parts = n_idle_parts = n_pass_splits = n_cuts = 0; } rec_spent_ns += kern_now_ns() - in_ns; if (rec_spent_ns >= REC_CLAIM_NS) rec_claim(); diff --git a/runtime/vk_exec.h b/runtime/vk_exec.h index 837153e0..2b3a0a31 100644 --- a/runtime/vk_exec.h +++ b/runtime/vk_exec.h @@ -142,6 +142,8 @@ void vk_exec_barrier(VkExec *ex); void vk_exec_barrier_dispatch(VkExec *ex); // NX2NAVI_TRACE=perf, every 600 frames: the barriers per frame by kind. void vk_exec_barrier_log(uint64_t frame); +// Ends the open pass and the part without handing it to the queue (vk_sync_cut): the next draw begins the pass again +// at the start of a new command buffer, which the part's pre-pass command buffer precedes. 0 when no more can wait. int vk_exec_split_now(VkExec *ex); // vk_texture.c (NX2NAVI_TEX_ALIAS): commands that must run at the replay's current position. vk_exec_inline_begin returns // the command buffer being recorded after a full barrier (an open pass is ended; *reopen says so), or VK_NULL_HANDLE diff --git a/runtime/vk_exec_draw.c b/runtime/vk_exec_draw.c index 6af2736b..80b75de4 100644 --- a/runtime/vk_exec_draw.c +++ b/runtime/vk_exec_draw.c @@ -5922,6 +5922,9 @@ static int mesh_emu_draw(VkExec *ex, uint32_t count, const VkMemSpan *ind, const VkDescriptorSet set0 = ex->dc->B.ds_bound[0]; // NX2NAVI_MESH_EMU_BATCH (default on): the stages run in the part's pre-pass command buffer (vk_sync_pre), which // the queue runs before the part, and the pass is not split for them. At the first emulated draw of a pass the part + // is cut there (vk_sync_cut: ended, but handed to the queue only with the next part, so the texture pool set stays; + // a split there took a new set, and each took 2.3 ms of the replay to refresh), so that the pass begins the next + // part and only what came before it runs before the stages. Split, a // pass of 20 emulated draws was 20 passes, each emulated draw a pass end and begin with a full barrier, three more // barriers and the targets stored and loaded again: on an RX 580 6 to 7 ms of a frame's GPU time went to the gaps // between passes (gpu_passes, Kokiri Forest, 2026-11-10). diff --git a/runtime/vk_sync.c b/runtime/vk_sync.c index f0cbf4ff..18685580 100644 --- a/runtime/vk_sync.c +++ b/runtime/vk_sync.c @@ -773,6 +773,7 @@ static int npre, pre_pair = -1; static VkCommandBuffer pre_cb; VkCommandBuffer vk_sync_pre(void) { if (pre_cb) return pre_cb; + VkCtx *c = vk_ctx(); if (!pre_pool) { VkCommandPoolCreateInfo pi = {.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO, .flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT, @@ -794,6 +795,9 @@ VkCommandBuffer vk_sync_pre(void) { } pre_slots[i].fence = cur_fence; // the submission's value: its parts are done by then VkCommandBuffer cb = pre_slots[i].cb; + VkCommandBufferBeginInfo bi = {.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO, + .flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT}; + vkBeginCommandBuffer(cb, &bi); pre_pair = ts_open(cb); // everything earlier in queue order (the parts before, the mirror prologue) before what it records VkMemoryBarrier2 mb = {.sType = VK_STRUCTURE_TYPE_MEMORY_BARRIER_2, .srcStageMask = VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT, @@ -803,6 +807,13 @@ VkCommandBuffer vk_sync_pre(void) { vkCmdPipelineBarrier2(cb, &di); pre_cb = cb; return cb; +} + +// Command buffers of the submission that a cut (vk_sync_cut) ended and that go to the queue with the next part, in +// order before it: a cut part's pre-pass command buffer, then the part. +static VkCommandBuffer chain[CHAIN_MAX]; +static int nchain; + // The pre-pass command buffer of the part that goes now, ended; VK_NULL_HANDLE without one. static VkCommandBuffer pre_end(uint64_t fence) { VkCommandBuffer cb = pre_cb; @@ -833,26 +844,59 @@ static VkCommandBuffer prologue(VkCtx *c, uint64_t fence) { return cb; } -// The command buffers of the part that ends now, in queue order, into cbi: the prologue (when there is one), the -// part's earlier pieces (vk_sync_append) and, with `with_last`, its last command buffer. Empties the list. -static uint32_t part_list(VkCommandBufferSubmitInfo *cbi, VkCommandBuffer pro, VkCommandBuffer last, int with_last) { +// What goes to the queue for a part: the mirror prologue, the parts cut before it with their pre-pass command buffers +// and pieces (the chain, emptied), the part's own pre-pass command buffer, the part's earlier pieces (vk_sync_append: +// the replay's and its workers', emptied), the part's last command buffer. Any of them may be missing. +static uint32_t chain_infos(VkCommandBufferSubmitInfo *cbi, VkCommandBuffer pro, VkCommandBuffer pre, VkCommandBuffer cb) { uint32_t n = 0; - VkCommandBufferSubmitInfo one = {.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_SUBMIT_INFO}; - if (pro) one.commandBuffer = pro, cbi[n++] = one; +#define CHAIN_ADD(x) \ + if (x) cbi[n++] = (VkCommandBufferSubmitInfo){.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_SUBMIT_INFO, .commandBuffer = (x)} + CHAIN_ADD(pro); + for (int k = 0; k < nchain; k++) CHAIN_ADD(chain[k]); + CHAIN_ADD(pre); for (int i = 0; i < npart_cbs; i++) { if (!part_cbs[i]) rt_fatal("vk: a replay worker's command buffer is missing from a part"); - one.commandBuffer = part_cbs[i], cbi[n++] = one; + CHAIN_ADD(part_cbs[i]); } - if (with_last) one.commandBuffer = last, cbi[n++] = one; - npart_cbs = 0; + CHAIN_ADD(cb); +#undef CHAIN_ADD + nchain = 0, npart_cbs = 0; return n; } +// A cut: the part ends and the replay goes on in a new command buffer, as at a split, but nothing goes to the queue: +// the part (its pre-pass command buffer, its pieces and itself) waits in the chain for the next part that does. The +// texture pool set stays as it is, as nothing of the submission has been handed to the GPU. VK_NULL_HANDLE when the +// chain is full. +VkCommandBuffer vk_sync_cut(void) { + if (nchain + npart_cbs + 2 > CHAIN_MAX) return VK_NULL_HANDLE; + for (int i = 0; i < npart_cbs; i++) + if (!part_cbs[i]) return VK_NULL_HANDLE; // a replay worker's piece not filled yet: the part goes on + VkCtx *c = vk_ctx(); + VkCommandBuffer cb = slots[cur_slot].cb; + vk_gpu_passes_part(cb, 1); + ts_end(cb, cur_fence); + if (!rec_end_cb(cb, 0, 0)) vkEndCommandBuffer(cb); + VkCommandBuffer pre = pre_end(cur_fence); + if (pre) chain[nchain++] = pre; + for (int i = 0; i < npart_cbs; i++) chain[nchain++] = part_cbs[i]; + npart_cbs = 0; + chain[nchain++] = cb; + CmdSlot *s = take_slot(c, cur_fence); // the cut part keeps its slot until the value is reached + VkCommandBufferBeginInfo bi = {.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO, + .flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT}; + vkBeginCommandBuffer(s->cb, &bi); + ts_begin(s->cb); + rec_begin(s->cb, cur_fence); + vk_gpu_passes_part(s->cb, 0); + return s->cb; +} + // The replay workers (vk_exec.c "replay workers"): the replay's command buffer being recorded ends and joins the // part's list, then n places for the command buffers the workers record, in that order, and a new command buffer of // the same part is begun and returned: the same timeline value, timed and submitted with the rest, so the workers' // commands run between the replay's commands before and after. The places are filled (vk_sync_append_fill) before the -// part is submitted, which part_list checks. vk_sync_append_room: whether the list has room for n more. +// part is submitted, which chain_infos checks. vk_sync_append_room: whether the list has room for n more. int vk_sync_append_room(int n) { return npart_cbs + n + 1 <= PART_CBS; } VkCommandBuffer vk_sync_append_later(int n, int *at) { @@ -914,7 +958,6 @@ VkCommandBuffer vk_sync_worker_begin(int w) { } void vk_sync_worker_end(VkCommandBuffer cb) { vkEndCommandBuffer(cb); } - static VkCommandBuffer split_part(int exec); VkCommandBuffer vk_sync_split(void) { return split_part(0); } // The part that ends here is the last with the submission's own commands: it signals the executed timeline. @@ -928,6 +971,7 @@ static VkCommandBuffer split_part(int exec) { if (!rec_end_cb(cb, 0, 0)) vkEndCommandBuffer(cb); // every command of the part is in cb, ended by its recorder vk_upload_flush(0); // the uploads this part asked for precede it in queue order VkCommandBuffer pro = prologue(c, cur_fence), pre = pre_end(cur_fence); + uint32_t ncb = chain_infos(cbi, pro, pre, cb); // the mirror copies, the cut parts, the pre-pass work, the part VkSubmitInfo2 si = {.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO_2, .commandBufferInfoCount = ncb, .pCommandBufferInfos = cbi}; VkSemaphoreSubmitInfo esig = {.sType = VK_STRUCTURE_TYPE_SEMAPHORE_SUBMIT_INFO, .semaphore = exec_tl, .value = cur_fence, .stageMask = VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT}; @@ -1005,6 +1049,7 @@ static uint64_t submit(const uint64_t *handles, int n, const OwnCommands *own) { s = &slots[cur_slot]; VkCommandBufferSubmitInfo cbi[PART_CBS + 2]; uint32_t ncbi = part_list(cbi, pro, s->cb, recorded); + uint32_t ncb = chain_infos(cbi, pro, pre, sent ? s->cb : VK_NULL_HANDLE); // the timeline, and the executed timeline unless a part of this submission signalled it already (split_part) VkSemaphoreSubmitInfo sig[2] = {{.sType = VK_STRUCTURE_TYPE_SEMAPHORE_SUBMIT_INFO, .semaphore = c->timeline, .value = fence, .stageMask = VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT}, @@ -1016,7 +1061,7 @@ static uint64_t submit(const uint64_t *handles, int n, const OwnCommands *own) { .signalSemaphoreInfoCount = both ? 2u : 1u, .pSignalSemaphoreInfos = sig}; queue_submit(c, &si, fence); // held or handed to the queue (submit batching) vk_mem_lazy_handed(); - s->fence = recorded ? fence : 0; + s->fence = sent ? fence : 0; vk_mem_note_submit(fence); plat_mutex_lock(&st_lock); __atomic_store_n(&issued, fence, __ATOMIC_RELEASE);