diff --git a/nx2navi.ini.example b/nx2navi.ini.example index 87c50f13..10748499 100644 --- a/nx2navi.ini.example +++ b/nx2navi.ini.example @@ -705,6 +705,11 @@ # about a third more frames a second in open areas. 0: copied back at the end of every GPU submission, as in 0.7.0. # With shadow storage (Linux, Wine) it stays off. # wb_lazy = 1 +# Barriers between the game's draw passes and compute dispatches. 1: only where the game itself asks for one, or where +# the work changes between drawing and compute; this takes about a tenth off the graphics card's time for a frame. +# 0: a full barrier before every pass and every dispatch. Default: 1 on NVIDIA graphics cards, 0 on all others, where it +# has not been tried: set 1 there to try it, and back to 0 if the picture shows faults. +# barrier_game = 1 # Linux, shadow storage: 1 clears the write watch's protection of a range in one call before the GPU's bytes are copied # back into the game's memory, instead of taking a fault at the first store into each page. # shadow_bulk_write = 0 diff --git a/runtime/cfg.c b/runtime/cfg.c index e4fcaf27..115377e2 100644 --- a/runtime/cfg.c +++ b/runtime/cfg.c @@ -33,6 +33,7 @@ static int save_ok = 1; // the last cfg_save_to wrote (o static const char *const known[] = { "CORES", "TM_DISPATCH", "NVN2_CS_TABLE", "SKIP_SHADERS", "MOVIE_DEBAND", "MOVIE_FPS", "MOVIE_SKIP", "MOVIE_OVERLAY", "MOVIES", "NPLN", "NVN2_PROGRAMS", "NVN_TRAP", "OCARINA_REALTIME", "ONESHOT_BUDGET", "ONESHOT_WAIT_MS", "OCARINA_STEP", "PHYSICS_STEPS", "STEP_STEADY", "VI_HZ", "VI_VRR", "TIME_DUMP", "SHADER_DUMP", "SHOT_AFTER", "SHOT_DIR", "SHOT_EVERY", // Switch 2 port: movie skip (romfs.c), NVN2 proc traps (nvn_procs.c) "AB", "AMIIBO_DIR", "ANISO", "APP_POOL", "ASPECT", "ASPECT_PATCH", "ASSUME_FOCUS", "ASTC", "ASTC_BC", "ASTC_TIME", "ASYNC_PREEMPT", "AUDIO", "AUDIO_CHANNELS", "AUDIO_DEVICE", "AUDIO_GAME_PRIO", "AUDIO_OFF", "AUDIO_PREFILL_MS", "AUDIO_STATS", + "BARRIER_GAME", "BACKGROUND_WINDOW", "BIND_A", "BIND_B", "BIND_DOWN", "BIND_L", "BIND_LCLICK", "BIND_LEFT", "BIND_LSTICK_DOWN", "BIND_LSTICK_LEFT", "BIND_LSTICK_RIGHT", "BIND_LSTICK_UP", "BIND_MINUS", "BIND_PLUS", "BIND_R", "BIND_RCLICK", "BIND_RIGHT", "BIND_RSTICK_DOWN", "BIND_RSTICK_LEFT", "BIND_RSTICK_RIGHT", "BIND_RSTICK_UP", @@ -124,7 +125,7 @@ static const struct { const char *key, *section; } sections[] = { {"VK_DYN_VERTEX", "performance"}, {"VK_DYN_A2C", "performance"}, {"VK_SUBGROUP_PIN", "performance"}, {"VK_RECONVERGE", "performance"}, {"VK_NAN_PRESERVE", "performance"}, {"TRACE", "diagnostics"}, {"FLIGHT", "diagnostics"}, {"PASS_CRUMBS", "diagnostics"}, {"FLIGHT_SPIKE_MS", "diagnostics"}, - {"WB_FENCE_LIMIT", "diagnostics"}, {"WB_MERGE", "diagnostics"}, {"WB_EXACT", "diagnostics"}, {"MEM_CENSUS", "diagnostics"}, {"WB_NO_REUP", "diagnostics"}, {"WB_PROBE", "diagnostics"}, {"WB_SPLIT", "performance"}, {"SHADOW_BULK_WRITE", "performance"}, {"WB_LAZY", "performance"}, {"WB_LAZY_TOUCH", "diagnostics"}, {"FLUSH_UPLOAD", "diagnostics"}, {"GPA_TWIN_MAX_KB", "diagnostics"}, + {"WB_FENCE_LIMIT", "diagnostics"}, {"WB_MERGE", "diagnostics"}, {"WB_EXACT", "diagnostics"}, {"MEM_CENSUS", "diagnostics"}, {"WB_NO_REUP", "diagnostics"}, {"WB_PROBE", "diagnostics"}, {"WB_SPLIT", "performance"}, {"SHADOW_BULK_WRITE", "performance"}, {"WB_LAZY", "performance"}, {"BARRIER_GAME", "performance"}, {"WB_LAZY_TOUCH", "diagnostics"}, {"FLUSH_UPLOAD", "diagnostics"}, {"GPA_TWIN_MAX_KB", "diagnostics"}, {"WB_MERGE_MAX", "diagnostics"}, {"MV_SURVEY", "diagnostics"}, {"TEXUP_LOG_MS", "diagnostics"}, {"VA_REPORT", "diagnostics"}, {"VMA_BLOCK_MB", "diagnostics"}, {"ASTC_BC_PROBE", "diagnostics"}, }; diff --git a/runtime/vk_backend.c b/runtime/vk_backend.c index cbd08768..e3859a65 100644 --- a/runtime/vk_backend.c +++ b/runtime/vk_backend.c @@ -219,6 +219,7 @@ static void present_image(const tex_t *t, const int crop[4], uint64_t at_ns, uin if (!vram_probe) vram_probe = 1, perfmon_set_vram_game(vk_mem_device_used_mb); // the overlay's "game" beside VRAM vk_exec_frame_log(t, frame); dynres_frame(); // before vk_dlaa_frame reads the DLSS mode it may override + vk_exec_barrier_log(frame); vk_dlaa_frame(); vk_sampler_aniso_live(); // NX2NAVI_ANISO changed in the menu: the surface textures' samplers take the new level vk_mvsurvey_frame(); diff --git a/runtime/vk_exec.c b/runtime/vk_exec.c index 6ac73408..441f046b 100644 --- a/runtime/vk_exec.c +++ b/runtime/vk_exec.c @@ -585,8 +585,71 @@ static VkImageMemoryBarrier2 layout_barrier(VkImage img, VkImageAspectFlags aspe // Full barrier. The images in the feedback layout become want[] (pass begin) or none (every other barrier): the others // go back to GENERAL and the new ones leave it, in the same command. +// NX2NAVI_BARRIER_GAME (1; 0: every pass begin and dispatch a full barrier; ab: in the B blocks): the game's own +// barriers order its work, as on the Switch. A pass that +// samples what an earlier one drew, a dispatch that reads what another dispatch wrote, an indirect draw whose arguments +// a dispatch wrote: on the Switch each needs nvnCommandBufferBarrier (ORDER_* and INVALIDATE_* bits) in the game's +// command stream, which the replay keeps as a full barrier (OP_BARRIER). What the GPU orders by itself there is kept: a +// switch between graphics and compute work drains the one before (the 3D and compute classes share the engine). With +// every pass begin and every dispatch a full barrier, 2.3 ms of a 9.7 ms GPU frame in Kokiri Forest was barriers. +// With the switch: +// - a pass begin after an earlier pass and no dispatch since the last full barrier gets an attachment barrier +// (colour and depth/stencil output before the next pass's loads and writes: Vulkan orders attachment access only +// inside one pass instance), after neither nothing; +// - a dispatch after other dispatches only gets none; +// - a pass after a dispatch, and a dispatch after a pass, get a full barrier, the drain of the switch; +// - either gets a full barrier as before after one the replay itself asked for (vk_exec_barrier: its copies, +// clears, fills, uniform updates and stand-in copies call it before their commands, which sets bar_dirty), and +// when feedback-loop targets change layout. +// Copies, clears and the game's barriers keep their full barriers. +// +// The default is 1 on NVIDIA GPUs and 0 on every other. The game placed its barriers for an NVIDIA GPU, and what it +// left out is what such a GPU orders by itself; Vulkan asks for a barrier wherever a pass samples what an earlier pass +// drew or a dispatch reads what an earlier one wrote, whether the game recorded one or not. On an RTX 5090 the picture +// was the same with the switch on and off (34 screenshots across A/B blocks in Kokiri Forest) and the GPU time fell by +// 0.5 to 0.6 ms of 5.4 a frame; no other vendor's driver has run it, so there the setting has to be turned on by hand. +static int barrier_game(void) { + static int mode = -1; + if (mode < 0) { + const VkCtx *c = vk_ctx(); + int nvidia = c && c->props.vendorID == 0x10de; + mode = rt_knob("BARRIER_GAME", nvidia ? "1" : "0"); + if (mode) + rt_log("vk_exec: NX2NAVI_BARRIER_GAME=%s: passes and dispatches are ordered by the game's barriers", + mode == 2 ? "ab" : "1"); + } + return RT_KNOB_ON(mode); +} + +// NX2NAVI_TRACE=perf: barriers per frame by kind (vk_exec_barrier_log) +enum { BS_FULL, BS_GAME, BS_PASS_FULL, BS_PASS_ATT, BS_PASS_NONE, BS_DISP_FULL, BS_DISP_NONE, BS_N }; +static uint64_t bstat[BS_N]; +static const char *const bs_name[BS_N] = {"replay full", "game", "pass full", "pass attachment", "pass none", + "dispatch full", "dispatch none"}; +enum { BR_OTHER, BR_GAME, BR_PASS, BR_DISPATCH }; + +static void barrier_to_why(VkExec *ex, const VkImage *want, const VkImageAspectFlags *wasp, int nwant, int why); static void barrier_to(VkExec *ex, const VkImage *want, const VkImageAspectFlags *wasp, int nwant) { + barrier_to_why(ex, want, wasp, nwant, BR_OTHER); +} + +static void attachment_barrier(VkExec *ex) { + const VkPipelineStageFlags2 st = VK_PIPELINE_STAGE_2_COLOR_ATTACHMENT_OUTPUT_BIT | + VK_PIPELINE_STAGE_2_EARLY_FRAGMENT_TESTS_BIT | VK_PIPELINE_STAGE_2_LATE_FRAGMENT_TESTS_BIT; + VkMemoryBarrier2 mb = {.sType = VK_STRUCTURE_TYPE_MEMORY_BARRIER_2, .srcStageMask = st, + .srcAccessMask = VK_ACCESS_2_COLOR_ATTACHMENT_WRITE_BIT | VK_ACCESS_2_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT, + .dstStageMask = st, + .dstAccessMask = VK_ACCESS_2_COLOR_ATTACHMENT_READ_BIT | VK_ACCESS_2_COLOR_ATTACHMENT_WRITE_BIT | + VK_ACCESS_2_DEPTH_STENCIL_ATTACHMENT_READ_BIT | + VK_ACCESS_2_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT}; + VkDependencyInfo di = {.sType = VK_STRUCTURE_TYPE_DEPENDENCY_INFO, .memoryBarrierCount = 1, .pMemoryBarriers = &mb}; + vkCmdPipelineBarrier2(ex->cb, &di); + ex->recorded++; +} + +static void barrier_to_why(VkExec *ex, const VkImage *want, const VkImageAspectFlags *wasp, int nwant, int why) { vk_exec_pass_end(ex); + if (why == BR_OTHER) ex->bar_dirty = 1; // also when it falls together with the last one: its commands follow VkImageMemoryBarrier2 ib[18]; uint32_t n = 0; for (int i = 0; i < ex->nfbl; i++) @@ -599,6 +662,21 @@ static void barrier_to(VkExec *ex, const VkImage *want, const VkImageAspectFlags for (int i = 0; i < nwant; i++) ex->fbl_img[i] = want[i], ex->fbl_aspect[i] = wasp[i]; ex->nfbl = nwant; if (ex->barrier_at == ex->recorded && !n) return; + if (!n && !ex->bar_dirty && ((why == BR_PASS && !ex->bar_dispatches) || (why == BR_DISPATCH && !ex->bar_passes)) && + barrier_game()) { + if (why == BR_DISPATCH) { + bstat[BS_DISP_NONE]++; + } else if (ex->bar_passes) { + attachment_barrier(ex); + bstat[BS_PASS_ATT]++; + } else { + bstat[BS_PASS_NONE]++; + } + return; + } + bstat[why == BR_GAME ? BS_GAME : why == BR_PASS ? BS_PASS_FULL : why == BR_DISPATCH ? BS_DISP_FULL : BS_FULL]++; + if (why != BR_OTHER) ex->bar_dirty = 0; // after BR_OTHER the replay's own commands follow: the next pass or + ex->bar_passes = ex->bar_dispatches = 0; // dispatch orders after them vk_exec_frame.fb_layouts += n; VkMemoryBarrier2 mb = {.sType = VK_STRUCTURE_TYPE_MEMORY_BARRIER_2, .srcStageMask = VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT, @@ -613,6 +691,25 @@ static void barrier_to(VkExec *ex, const VkImage *want, const VkImageAspectFlags void vk_exec_barrier(VkExec *ex) { barrier_to(ex, NULL, NULL, 0); } +void vk_exec_barrier_dispatch(VkExec *ex) { + barrier_to_why(ex, NULL, NULL, 0, BR_DISPATCH); + ex->bar_dispatches++; +} + +void vk_exec_barrier_log(uint64_t frame) { + static uint64_t last; + if (!(rt_trace & RT_TRACE_PERF) || frame % 600) return; + double f = frame > last ? (double)(frame - last) : 1.0; + last = frame; + char s[320]; + int o = 0; + for (int k = 0; k < BS_N; k++) { + o += snprintf(s + o, sizeof s - o, "%s%s %.1f", k ? ", " : "", bs_name[k], bstat[k] / f); + bstat[k] = 0; + } + rt_log("vk_exec: frame %llu: barriers per frame: %s", (unsigned long long)frame, s); +} + void vk_exec_pass_begin(VkExec *ex) { if (ex->in_pass) return; resolve_rt(ex); @@ -630,7 +727,8 @@ void vk_exec_pass_begin(VkExec *ex) { if (has_image(want, nwant, t->image)) continue; want[nwant] = t->image, wasp[nwant++] = i < 8 ? VK_IMAGE_ASPECT_COLOR_BIT : t->img->aspect; } - barrier_to(ex, want, wasp, nwant); // passes, clears, copies and dispatches see each other's results + barrier_to_why(ex, want, wasp, nwant, BR_PASS); // passes, clears, copies and dispatches see each other's results + ex->bar_passes++; ex->fbl_mask = fbl; ex->fb_dirty = 0; ex->fb_read = 0; @@ -1390,7 +1488,7 @@ static const op_hdr *walk(VkExec *ex, const op_hdr *h, int depth) { case OP_COPY_STRIDE: ex->copy_stride = ((const p_copy_stride *)a)->stride; break; case OP_SET_TEX_POOL: ex->tex_pool = ((const p_set_pool *)a)->pool, ex->tex_serial++; break; case OP_SET_SAMP_POOL: ex->samp_pool = ((const p_set_pool *)a)->pool; break; - case OP_BARRIER: vk_exec_barrier(ex); break; + case OP_BARRIER: barrier_to_why(ex, NULL, NULL, 0, BR_GAME); break; case OP_DRAW: case OP_DRAW_IDX: case OP_DISPATCH: case OP_UPDATE_UB: case OP_DRAW_IDX_IND: case OP_DISPATCH_IND: if (h->op == OP_DRAW || h->op == OP_DRAW_IDX || h->op == OP_DRAW_IDX_IND) vk_exec_frame.draws_seen++, split_draws++; if (h->op == OP_DISPATCH || h->op == OP_DISPATCH_IND) vk_exec_frame.dispatches++, split_draws++; diff --git a/runtime/vk_exec.h b/runtime/vk_exec.h index 6ce192c6..baf35d82 100644 --- a/runtime/vk_exec.h +++ b/runtime/vk_exec.h @@ -103,6 +103,10 @@ typedef struct VkExec { void *views; // image views created for this command buffer (freed at its fence) int nviews, cap_views; int barrier_at; // recorded right after the last full barrier (-1: none yet) + // NX2NAVI_BARRIER_GAME (vk_exec.c barrier_to_why): the last full barrier was one the replay itself needed (a copy, + // clear or fill of its own follows it), so the next pass or dispatch gets a full one too; passes and dispatches + // begun since the last full barrier + int bar_dirty, bar_passes, bar_dispatches; // Feedback loops (vk.h VkCtx.feedback, vk_exec_draw.c feedback_prepare). Learned targets are attached in // ATTACHMENT_FEEDBACK_LOOP_OPTIMAL_EXT; they stay in it across consecutive passes and go back to GENERAL at the next @@ -128,6 +132,11 @@ void vk_exec_pass_end(VkExec *ex); // Full memory barrier (all commands, all memory); ends the open pass first. Copies and dispatches order themselves with // it. Skipped when nothing was recorded since the last one. void vk_exec_barrier(VkExec *ex); +// The barrier before a game dispatch: a full one, or with NX2NAVI_BARRIER_GAME none unless the replay's own commands +// came since the last full barrier (vk_exec.c barrier_to_why). Ends the open pass. +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); // 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 // when this thread is not recording a replay. vk_exec_inline_end begins the pass again when it was open. diff --git a/runtime/vk_exec_draw.c b/runtime/vk_exec_draw.c index 4f375ebe..0c611fd9 100644 --- a/runtime/vk_exec_draw.c +++ b/runtime/vk_exec_draw.c @@ -5182,7 +5182,7 @@ static void op_dispatch(VkExec *ex, const p_dispatch *d) { VkPipeline p = vk_pipeline_compute(cs, &info); if (!p || !info) return warn_once(W_PIPELINE, "a compute pipeline could not be created: dispatches with it are skipped"); if ((info->pool && !pool_on()) || shader_skipped(cs)) return; // NX2NAVI_POOL=0, NX2NAVI_SKIP_SHADERS - vk_exec_barrier(ex); // ends the pass; earlier writes are visible to the shader + vk_exec_barrier_dispatch(ex); // ends the pass; earlier writes are visible to the shader bind_pipeline(ex, VK_PIPELINE_BIND_POINT_COMPUTE, p); c0_dim[0] = (uint32_t)d->x, c0_dim[1] = (uint32_t)d->y, c0_dim[2] = (uint32_t)d->z; twin_dispatch = 1; @@ -5217,7 +5217,7 @@ static void op_dispatch_ind(VkExec *ex, const p_dispatch_ind *d) { VkPipeline p = vk_pipeline_compute(cs, &info); if (!p || !info) return warn_once(W_PIPELINE, "a compute pipeline could not be created: dispatches with it are skipped"); if ((info->pool && !pool_on()) || shader_skipped(cs)) return; // NX2NAVI_POOL=0, NX2NAVI_SKIP_SHADERS - vk_exec_barrier(ex); + vk_exec_barrier_dispatch(ex); VK_MEM_USE(ip); bind_pipeline(ex, VK_PIPELINE_BIND_POINT_COMPUTE, p); c0_dim[0] = c0_dim[1] = c0_dim[2] = 0;