diff --git a/runtime/cfg.c b/runtime/cfg.c index f18b45b1..c22e5163 100644 --- a/runtime/cfg.c +++ b/runtime/cfg.c @@ -45,7 +45,7 @@ static const char *const known[] = { "RENDER_SCALE", "REPLAY_ASYNC", "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", "SHARPNESS", "SHOTS", "SHOTS_DIR", "SHOTS_FULL", "SPEED_PROBE", "SPLIT_DRAWS", "SWKBD_ANSWER", + "SHADER_MEMO", "SHARED_FONTS", "SHARPNESS", "SHOTS", "SHOTS_DIR", "SHOTS_FULL", "SPEED_PROBE", "SPLIT_DRAWS", "SPLIT_US", "SWKBD_ANSWER", "SWKBD_TEST", "TEST_PREPARE_SHADERS", "TEX_ALIAS", "TEX_MEMO", "TIMER_1MS", "TRACE", "UPSCALER", "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", @@ -68,7 +68,7 @@ static const struct { const char *key, *section; } sections[] = { {"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"}, {"TEX_MEMO", "performance"}, {"DESC_TEMPLATE", "performance"}, - {"PUSH_SKIP", "performance"}, {"SPLIT_DRAWS", "performance"}, {"VK_VALIDATION", "performance"}, + {"PUSH_SKIP", "performance"}, {"SPLIT_DRAWS", "performance"}, {"SPLIT_US", "performance"}, {"VK_VALIDATION", "performance"}, {"VK_DYN_VERTEX", "performance"}, {"VK_SUBGROUP_PIN", "performance"}, {"TRACE", "diagnostics"}, {"FLIGHT", "diagnostics"}, {"FLIGHT_SPIKE_MS", "diagnostics"}, }; @@ -111,6 +111,7 @@ static const CfgMeta metas[] = { {"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}, + {"SPLIT_US", "1000", "0..20000", RESTART}, // Vulkan {"VK_VALIDATION", "0", "0|1", RESTART}, {"VK_DYN_VERTEX", "", "0|1", RESTART}, {"VK_SUBGROUP_PIN", "1", "0|1", RESTART}, diff --git a/runtime/vk_exec.c b/runtime/vk_exec.c index 90883676..f0dbc146 100644 --- a/runtime/vk_exec.c +++ b/runtime/vk_exec.c @@ -264,7 +264,11 @@ static void resolve_rt(VkExec *ex) { ex->pipe.samples = (uint8_t)ex->pass_samples; } +// The attachment view of a target. A registered texture's view lives as long as its image (vk_exec_target_view); any +// other image's view is made for this command buffer and freed at its fence. static VkImageView view_of(VkExec *ex, const VkExecTarget *t) { + if (t->img && t->img->image == t->image) + return vk_exec_target_view(t->img, t->format, t->view_type, t->aspect, t->level, t->base_layer, t->layers, t->blocks); ViewEnt *v = ex->views; for (int i = 0; i < ex->nviews; i++) if (v[i].image == t->image && v[i].format == t->format && v[i].type == t->view_type && v[i].aspect == t->aspect && @@ -614,20 +618,29 @@ static void set_rt(VkExec *ex, const p_set_rt *r) { // queries are not: a barrier's first scope is everything earlier in submission order, image layouts belong to the image, // and occlusion queries end with their pass. Fences, completion callbacks and reports stay per submission: only the last // part signals the value. nxnative commits every 64 draws between passes; MacNativeGames after 30, then every 40. +// A part also spans at least TOTK_SPLIT_US (1000) of replay time: a split costs about 40 us of the render thread +// (vkQueueSubmit2, a command buffer, the mirror prologue), and a fight frame of 2,000 draws made 15 parts on the 7900X; +// a millisecond of recording is a fraction of a millisecond of GPU work, so the GPU still starts early. static int split_n = -1, split_draws, part_start; +static uint64_t split_ns, part_t0; static uint64_t n_parts, n_subs; static void maybe_split(VkExec *ex) { if (split_n < 0) { split_n = cfg_int("TOTK_SPLIT_DRAWS"); if (split_n < 0) split_n = 0; - rt_log("vk: replay command buffer split every %d draws between passes%s", split_n, split_n ? "" : " (off)"); + int us = cfg_int("TOTK_SPLIT_US"); + split_ns = us > 0 ? (uint64_t)us * 1000 : 0; + rt_log("vk: replay command buffer split every %d draws between passes, parts of at least %d us%s", split_n, + us > 0 ? us : 0, split_n ? "" : " (off)"); } if (!split_n || ex->in_pass || split_draws < split_n) return; + if (split_ns && kern_now_ns() - part_t0 < split_ns) return; // split_draws stays: the next draw asks again split_draws = 0; if (ex->recorded == part_start) return; ex->cb = vk_sync_split(); part_start = ex->recorded; + part_t0 = kern_now_ns(); ex->dirty = VK_EXEC_DIRTY_ALL; vk_exec_draw_new_cb(); n_parts++; @@ -730,6 +743,7 @@ int vk_exec_record(VkCommandBuffer cb, const uint64_t *handles, int n, uint64_t ex->nfbl = 0; // the previous command buffer ended with a full barrier: every image is in GENERAL vk_exec_frame.submits++; split_draws = part_start = 0; + part_t0 = kern_now_ns(); x_here = 1; if (n) vk_mem_mirror_begin(); for (int i = 0; i < n; i++) { diff --git a/runtime/vk_exec.h b/runtime/vk_exec.h index 28426318..a274cfb3 100644 --- a/runtime/vk_exec.h +++ b/runtime/vk_exec.h @@ -101,7 +101,7 @@ typedef struct VkExec { // or the last in-pass barrier const void *fb_fs; // feedback_scan's memo: fragment shader, texture bind serial, pass serial uint32_t fb_serial, fb_pass, fb_sampled; - uint32_t tex_serial, pass_serial; // bumped by every texture bind and pass begin + uint32_t tex_serial, pass_serial; // bumped by every texture or image bind and pass begin } VkExec; // Begins dynamic rendering with the current targets unless a pass is open (a full memory barrier precedes it). Call @@ -166,6 +166,8 @@ void vk_c0_fill(uint8_t bank[VK_C0_BYTES], int stage, const p_bind_buf ssbo[16], void vk_exec_op_draw(VkExec *ex, uint32_t op, const void *payload); // The aspect a sampled view of img reads (vk_exec_draw.c get_view): stencil for a view in STENCIL depth-stencil mode. VkImageAspectFlags vk_exec_view_aspect(const VkTexImage *img, const view_t *v, int hv); +VkImageView vk_exec_target_view(const VkTexImage *img, VkFormat format, VkImageViewType type, uint32_t aspect, + uint32_t level, uint32_t base, uint32_t layers, int blocks); // kept for the image's life // T6c3: OP_COPY_B2B, OP_COPY_B2T, OP_COPY_T2B, OP_COPY_T2T, OP_CLEAR_TEX, OP_SIGNAL_EVENT, OP_REPORT_COUNTER, // OP_RESET_COUNTER, OP_RENDER_ENABLE, OP_RENDER_COND, OP_CLEAR_BUF. void vk_exec_op_xfer(VkExec *ex, uint32_t op, const void *payload); diff --git a/runtime/vk_exec_draw.c b/runtime/vk_exec_draw.c index 87cc1d14..fcc14669 100644 --- a/runtime/vk_exec_draw.c +++ b/runtime/vk_exec_draw.c @@ -530,6 +530,34 @@ static VkImageView get_view(VkExec *ex, const VkTexImage *img, const view_t *v, return view; } +// An attachment view of a registered texture, kept with its sampled views for the image's lifetime: made per command +// buffer, every render target's views were created again each frame (vkCreateImageView under vk_exec_pass_begin, 2% +// of the fight replay on the 7900X). Attachment keys carry a swizzle word no sampled key has (those hold four NVN +// selectors, each below 8). The texture lock guards the tables, as for get_view; vk_exec_pass_begin may already hold it. +VkImageView vk_exec_target_view(const VkTexImage *img, VkFormat format, VkImageViewType type, uint32_t aspect, + uint32_t level, uint32_t base, uint32_t layers, int blocks) { + ViewKey k = {img->image, format, type, aspect, level, 1, base, layers, blocks ? 0xfffffffeu : 0xffffffffu}; + vk_texture_lock(); + if (!view_of.klen) nvn_hm_init(&view_of, sizeof(ViewKey)), nvn_hm_init(&views_by_image, sizeof(VkImage)); + VkImageView view = (VkImageView)nvn_hm_get(&view_of, &k); + if (!view) { + VkImageViewCreateInfo ci = {.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO, .image = img->image, + .viewType = type, .format = format, .subresourceRange = {aspect, level, 1, base, layers}}; + VkImageViewUsageCreateInfo use = {.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_USAGE_CREATE_INFO, + .usage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT}; + if (blocks) ci.pNext = &use; // not the BC image's sampling and transfer usages + if (vkCreateImageView(vk_ctx()->device, &ci, NULL, &view) != VK_SUCCESS) + rt_fatal("vk: render target view (format %d) could not be created", (int)format); + ViewNode *n = malloc(sizeof *n); + if (!n) rt_fatal("vk: out of memory"); + *n = (ViewNode){nvn_hm_get(&views_by_image, &img->image), k, view}; + nvn_hm_put(&views_by_image, &img->image, n); + nvn_hm_put(&view_of, &k, (void *)view); + } + vk_texture_unlock(); + return view; +} + void vk_exec_image_gone(VkImage image) { if (!views_by_image.klen) return; for (ViewNode *n = nvn_hm_del(&views_by_image, &image), *next; n; n = next) { @@ -1736,27 +1764,33 @@ static int pipe_skip_mode(void) { // nothing else, so shader_for would return the same shaders. About 65% of field draws repeat them // (TOTK_BIND_STATS). TOTK_SHADER_MEMO=0 turns it off; =check looks the shaders up on every hit too, and every slot's // texture type in answers_for, and stops on a difference. +// tex_serial (bumped by every texture and image bind) equal to the key's means the handles were not rebound since +// they were compared, so the 1 KiB compare is skipped; a rebind to the same handles still matches by the compare. typedef struct { const prog_t *prog; uint64_t serial; - uint32_t gen; + uint32_t gen, tex_serial; uint64_t tex[2][32], img[2][32]; VkPipeState pipe; } MemoKey; static struct { int on, valid, has_fs; MemoKey k; const GfxShader *vs, *fs; } SM = {-1}; static void memo_key(const VkExec *ex, uint32_t gen, MemoKey *k) { - k->prog = ex->prog, k->serial = ex->prog->serial, k->gen = gen; + k->prog = ex->prog, k->serial = ex->prog->serial, k->gen = gen, k->tex_serial = ex->tex_serial; memcpy(k->tex[0], ex->tex[NVN_STAGE_VS], sizeof k->tex[0]), memcpy(k->tex[1], ex->tex[NVN_STAGE_FS], sizeof k->tex[1]); memcpy(k->img[0], ex->img[NVN_STAGE_VS], sizeof k->img[0]), memcpy(k->img[1], ex->img[NVN_STAGE_FS], sizeof k->img[1]); memcpy(&k->pipe, &ex->pipe, sizeof k->pipe); } -static int memo_match(const VkExec *ex, uint32_t gen, const MemoKey *k) { - return k->prog == ex->prog && k->serial == ex->prog->serial && k->gen == gen && - !memcmp(k->tex[0], ex->tex[NVN_STAGE_VS], sizeof k->tex[0]) && !memcmp(k->tex[1], ex->tex[NVN_STAGE_FS], sizeof k->tex[1]) && - !memcmp(k->img[0], ex->img[NVN_STAGE_VS], sizeof k->img[0]) && !memcmp(k->img[1], ex->img[NVN_STAGE_FS], sizeof k->img[1]) && - !memcmp(&k->pipe, &ex->pipe, sizeof k->pipe); +static int memo_match(const VkExec *ex, uint32_t gen, MemoKey *k) { + if (k->prog != ex->prog || k->serial != ex->prog->serial || k->gen != gen || memcmp(&k->pipe, &ex->pipe, sizeof k->pipe)) + return 0; + if (k->tex_serial == ex->tex_serial) return 1; + if (memcmp(k->tex[0], ex->tex[NVN_STAGE_VS], sizeof k->tex[0]) || memcmp(k->tex[1], ex->tex[NVN_STAGE_FS], sizeof k->tex[1]) || + memcmp(k->img[0], ex->img[NVN_STAGE_VS], sizeof k->img[0]) || memcmp(k->img[1], ex->img[NVN_STAGE_FS], sizeof k->img[1])) + return 0; + k->tex_serial = ex->tex_serial; + return 1; } static int shader_memo_on(void) { @@ -2176,6 +2210,7 @@ void vk_exec_op_draw(VkExec *ex, uint32_t op, const void *payload) { case OP_BIND_IMG: { const p_bind_handles *b = payload; bind_handles(b->stage >= 0 && b->stage < 6 ? ex->img[b->stage] : NULL, 32, b); + ex->tex_serial++; // the shader memo's handle check (feedback_scan reads only textures: a rescan at most) bind_gen++; break; }