From 25b200f94c2c0a64bb9bb5a315a7fcfd8c0b9cf5 Mon Sep 17 00:00:00 2001 From: Valiantsin Dzerakh Date: Fri, 30 Oct 2026 23:50:02 +0300 Subject: [PATCH] Draw 3D BC textures through stand-ins and let the recorder end its parts AMD driver 26.10.1 on RDNA2 cards wrote past the end of a 3D BC4 volume made BLOCK_TEXEL_VIEW_COMPATIBLE and 2D_ARRAY_COMPATIBLE, which lost the device at the logo (issue #5). 3D BC textures no longer get block views: draws into a level go to an uncompressed 2D array stand-in that is copied back at the pass end (TOTK_BLOCK_VIEWS_3D=1 restores the old path). Parts the recorder thread recorded and the submitting thread ended turned the scene green, then white, on the same driver once a submission was split. The recorder now writes the closing breadcrumb and ends the part itself (TOTK_REC_END=0 restores the old split). The recorder stays off on AMD by default. Based on the patch Pelipooper posted in issue #5; stand-ins extended to every mip level. Co-Authored-By: Claude Opus 5.5 --- docs/architecture.md | 2 +- runtime/cfg.c | 4 +- runtime/vk_exec.c | 156 +++++++++++++++++++++++++++++++++++++++++ runtime/vk_exec.h | 2 + runtime/vk_exec_draw.c | 1 + runtime/vk_rec.c | 37 +++++++++- runtime/vk_rec.h | 6 +- runtime/vk_sync.c | 8 +-- runtime/vk_texture.c | 13 +++- 9 files changed, 217 insertions(+), 12 deletions(-) diff --git a/docs/architecture.md b/docs/architecture.md index af1c6605..28a29bf7 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -82,7 +82,7 @@ Every guest thread is a host thread. TotK assumes Horizon's scheduler: three app - Vulkan 1.3 back end in place of MacNativeGames' Metal one. At `QueueSubmitCommands` the recorded NVN commands are replayed into Vulkan command buffers (`vk_exec*.c`). - Memory pools. Apple's unified memory let MacNativeGames use the game's pools zero-copy. The back end imports the pools as host memory (`VK_EXT_external_memory_host`). On a discrete GPU, pools bound as buffers are mirrored in device-local memory and the pages the CPU wrote (write watch, above) are uploaded before a replay; idle pool regions are made read-only so the write-watch queries skip them (`vk_memory.c`). GPU-rendered textures are written back when the CPU reads them: on a pool invalidate, on a CPU copy, and for small targets once the CPU waits for a fence (`vk_texture.c`). - Replay thread. `TOTK_REPLAY_ASYNC=1` moves the replay off the submitting thread onto a queue at most one frame behind the game; reads of GPU results wait for the newest writer instead of draining the queue (`vk_backend.c`). It is off by default because no timing has shown a gain yet. -- Recorder thread. The Vulkan calls the replay makes into its command buffer are made by a thread of their own (`vk_rec.c`, after Savage Wind Recomp and LAS Recomp): each `vkCmd*` copies its arguments into a packet of a ring and the recorder makes the calls in order; calls into other command buffers are made at once, and the submit waits until the ring is empty. In the town on an RX 9070 the replay takes 17% less time on the submitting thread. `TOTK_VK_RECORDER=0` makes every call on the submitting thread. +- Recorder thread. The Vulkan calls the replay makes into its command buffer are made by a thread of their own (`vk_rec.c`, after Savage Wind Recomp and LAS Recomp): each `vkCmd*` copies its arguments into a packet of a ring and the recorder makes the calls in order; calls into other command buffers are made at once; the recorder also ends the command buffer, and the submit waits until it has. In the town on an RX 9070 the replay takes 17% less time on the submitting thread. `TOTK_VK_RECORDER=0` makes every call on the submitting thread. - Uniform snapshot. The game rewrites per-object uniform blocks on other threads soon after it submits the frame that reads them, while the replay still records it. At the start of a submission's replay every uniform range it binds from CPU-written pools is copied, and the draws bind the copy (`vk_usnap.c`, after MacNativeGames' diff --git a/runtime/cfg.c b/runtime/cfg.c index 31eb9213..ed31672a 100644 --- a/runtime/cfg.c +++ b/runtime/cfg.c @@ -35,7 +35,7 @@ static const char *const known[] = { "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", "BIND_SKIP", "BIND_STATS", "BIND_UP", "BIND_X", "BIND_Y", - "BIND_ZL", "BIND_ZR", "CLOTH_REALTIME", "CORE_DIAG", "CRIT", "CRIT_OUT", "DESC_TEMPLATE", "DUMP", "DUMP_BUF", "DUMP_DIR", "DUMP_LOG", + "BIND_ZL", "BIND_ZR", "BLOCK_VIEWS_3D", "CLOTH_REALTIME", "CORE_DIAG", "CRIT", "CRIT_OUT", "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", "FPS_LIMIT", "FREEZE_IDLE", "FREE_CORES", "FULLSCREEN", "GPL", "GPL_DRAW_BUILD", "GPU", "GPU_INDEX", "GPU_NAME", "HIGHFPS_FIX", "INPUT", "INPUT_RECORD", "INPUT_SCRIPT", "LANG", "LIB_PREBUILD", @@ -43,7 +43,7 @@ static const char *const known[] = { "MENU_TEST_DIR", "MENU_TEST_SET", "MINIMAL_LAYOUT", "MIRROR_GAP", "MOTION", "MOUSE_SENS", "MOVIE_HOST", "MOVIE_PACE", "ONE_PER_CORE", "OPERATION_MODE", "OUTPUT_RES", "PAD_LAYOUT", "PATCH_OUTPUT", "PATCH_SCENE", "PERF_OVERLAY", "PERF_SLOW_MS", "PIPELINE_CACHE", "PIPE_MEMO", "PIPE_PREBUILD", "PIPE_SKIP", "PIPE_SYNC", "PIPE_WAIT", "PREWARM_XLATE", "PROBE_MOVIE", "PROBE_MOVIE_SHOT", "PROFILE", "PROFILE_CHAINS", "PROFILE_DIR", "PROFILE_FRAMES", - "PROFILE_MAIN", "PROFILE_RENDER", "PUSH_SKIP", "PUSH_STAGE", "RDOC", "RDOC_DIR", "RDOC_DLL", "REALTIME", "REEXEC", + "PROFILE_MAIN", "PROFILE_RENDER", "PUSH_SKIP", "PUSH_STAGE", "RDOC", "RDOC_DIR", "RDOC_DLL", "REALTIME", "REC_END", "REEXEC", "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", diff --git a/runtime/vk_exec.c b/runtime/vk_exec.c index d82c0574..258b38ed 100644 --- a/runtime/vk_exec.c +++ b/runtime/vk_exec.c @@ -23,6 +23,7 @@ #include "host.h" #include "nvn_api.h" #include "nvn_tex.h" +#include "platform/platform.h" #include "tex.h" #include "vk.h" #include "vk_exec.h" @@ -141,11 +142,163 @@ static int view_format(const tex_t *t, const view_t *v, int blocks, VkFormat *ou return 1; } +// ---- 3D BC render targets +// A 3D BC texture has no block views (vk_texture.c create_tex, TOTK_BLOCK_VIEWS_3D): on RDNA2 cards such an image +// made BLOCK_TEXEL_VIEW_COMPATIBLE and 2D_ARRAY_COMPATIBLE took GPU writes past the end of its memory (issue #5). Draws +// into it through an uncompressed view of its blocks (agl's BC4 encoder fills 32x32x32 and 64x64x64 volumes and their +// levels at the logo) go to a stand-in: a 2D array of the blocks of one level in the view's format, one layer per +// depth plane. It starts with the level's content, so a pass that writes part of a plane keeps the rest, and each pass +// end copies the planes it drew into the level (a copy between a compressed and a size-compatible uncompressed format). +typedef struct { VkImage bc, img; VmaAllocation alloc; VkFormat fmt; uint32_t level, w, h, d; } BcStandIn; +#define BC_STANDINS 64 // one per level drawn: 6 volumes of up to 7 levels at the logo +static BcStandIn bcs[BC_STANDINS]; +static int nbcs; +static PlatMutex bcs_lock = PLAT_MUTEX_INIT; // the replay adds and reads; destroy_tex (any thread) removes + +static void image_barrier(VkCommandBuffer cb, VkImage img, VkImageLayout from, uint32_t layers) { + VkImageMemoryBarrier2 ib = {.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER_2, .srcStageMask = VK_PIPELINE_STAGE_2_NONE, + .dstStageMask = VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT, + .dstAccessMask = VK_ACCESS_2_MEMORY_READ_BIT | VK_ACCESS_2_MEMORY_WRITE_BIT, + .oldLayout = from, .newLayout = VK_IMAGE_LAYOUT_GENERAL, + .srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED, .dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED, + .image = img, .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, layers}}; + VkDependencyInfo di = {.sType = VK_STRUCTURE_TYPE_DEPENDENCY_INFO, .imageMemoryBarrierCount = 1, .pImageMemoryBarriers = &ib}; + vkCmdPipelineBarrier2(cb, &di); +} + +static void memory_barrier(VkCommandBuffer cb, VkPipelineStageFlags2 src, VkAccessFlags2 sa, VkPipelineStageFlags2 dst, + VkAccessFlags2 da) { + VkMemoryBarrier2 mb = {.sType = VK_STRUCTURE_TYPE_MEMORY_BARRIER_2, .srcStageMask = src, .srcAccessMask = sa, + .dstStageMask = dst, .dstAccessMask = da}; + VkDependencyInfo di = {.sType = VK_STRUCTURE_TYPE_DEPENDENCY_INFO, .memoryBarrierCount = 1, .pMemoryBarriers = &mb}; + vkCmdPipelineBarrier2(cb, &di); +} + +// The stand-in of level level of a 3D BC image for draws through view format fmt, made on first use; 0 when none can +// be made (logged). Replay thread. +static int bc_standin(const VkTexImage *img, uint32_t level, VkFormat fmt, BcStandIn *out) { + static int warned; + plat_mutex_lock(&bcs_lock); + int full = nbcs == BC_STANDINS; + for (int i = 0; i < nbcs; i++) + if (bcs[i].bc == img->image && bcs[i].level == level && bcs[i].fmt == fmt) { + *out = bcs[i]; + plat_mutex_unlock(&bcs_lock); + return 1; + } + plat_mutex_unlock(&bcs_lock); + if (full) { + if (!warned++) rt_log("vk: more than %d 3D BC render targets: the others are left out", BC_STANDINS); + return 0; + } + uint32_t lw = level_extent(img->width, level), lh = level_extent(img->height, level); + BcStandIn s = {img->image, VK_NULL_HANDLE, NULL, fmt, level, (lw + 3) / 4, (lh + 3) / 4, + level_extent(img->depth, level)}; + VkImageCreateInfo ci = {.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO, .imageType = VK_IMAGE_TYPE_2D, .format = fmt, + .extent = {s.w, s.h, 1}, .mipLevels = 1, .arrayLayers = s.d, .samples = VK_SAMPLE_COUNT_1_BIT, + .tiling = VK_IMAGE_TILING_OPTIMAL, + .usage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT | + VK_IMAGE_USAGE_TRANSFER_DST_BIT, + .initialLayout = VK_IMAGE_LAYOUT_UNDEFINED}; + VmaAllocationCreateInfo ai = {.usage = VMA_MEMORY_USAGE_AUTO_PREFER_DEVICE}; + if (vmaCreateImage(vk_ctx()->vma, &ci, &ai, &s.img, &s.alloc, NULL) != VK_SUCCESS) { + if (!warned++) rt_log("vk: stand-in for a 3D BC render target could not be created: left out"); + return 0; + } + // In the upload batch, which goes to the queue before this part: GENERAL, then the texture's blocks. + VkCommandBuffer cb = vk_upload_begin(); + image_barrier(cb, s.img, VK_IMAGE_LAYOUT_UNDEFINED, s.d); + memory_barrier(cb, VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT, VK_ACCESS_2_MEMORY_WRITE_BIT, VK_PIPELINE_STAGE_2_COPY_BIT, + VK_ACCESS_2_TRANSFER_READ_BIT | VK_ACCESS_2_TRANSFER_WRITE_BIT); + VkImageCopy rg = {.srcSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, level, 0, 1}, + .dstSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, s.d}, + .extent = {lw, lh, s.d}}; // texels of the compressed source + vkCmdCopyImage(cb, img->image, VK_IMAGE_LAYOUT_GENERAL, s.img, VK_IMAGE_LAYOUT_GENERAL, 1, &rg); + memory_barrier(cb, VK_PIPELINE_STAGE_2_COPY_BIT, VK_ACCESS_2_TRANSFER_WRITE_BIT, VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT, + VK_ACCESS_2_MEMORY_READ_BIT | VK_ACCESS_2_MEMORY_WRITE_BIT); + vk_upload_end(); + plat_mutex_lock(&bcs_lock); + bcs[nbcs++] = s; + plat_mutex_unlock(&bcs_lock); + rt_log("vk: 3D BC render target %ux%ux%u level %u drawn through a %ux%u x %u stand-in (format %d)", img->width, + img->height, img->depth, level, s.w, s.h, s.d, (int)fmt); + *out = s; + return 1; +} + +// vk_exec_draw.c vk_exec_image_gone: the stand-in of a BC image that goes, freed once the GPU is done with it. +void vk_exec_bc_gone(VkImage image) { + plat_mutex_lock(&bcs_lock); + for (int i = 0; i < nbcs; i++) + if (bcs[i].bc == image) { + vk_defer_image(bcs[i].img, bcs[i].alloc); + bcs[i--] = bcs[--nbcs]; + } + plat_mutex_unlock(&bcs_lock); +} + +// At a pass end: the planes the pass drew into a stand-in go to its texture. +static void bc_copy_back(VkExec *ex, const VkExecTarget *t) { + plat_mutex_lock(&bcs_lock); + for (int i = 0; i < nbcs; i++) { + if (bcs[i].img != t->image) continue; + memory_barrier(ex->cb, VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT, VK_ACCESS_2_MEMORY_WRITE_BIT, VK_PIPELINE_STAGE_2_COPY_BIT, + VK_ACCESS_2_TRANSFER_READ_BIT | VK_ACCESS_2_TRANSFER_WRITE_BIT); + VkImageCopy rg = {.srcSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, t->base_layer, t->layers}, + .dstSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, bcs[i].level, 0, 1}, + .dstOffset = {0, 0, (int32_t)t->base_layer}, + .extent = {bcs[i].w, bcs[i].h, t->layers}}; // texels of the uncompressed source + vkCmdCopyImage(ex->cb, t->image, VK_IMAGE_LAYOUT_GENERAL, bcs[i].bc, VK_IMAGE_LAYOUT_GENERAL, 1, &rg); + memory_barrier(ex->cb, VK_PIPELINE_STAGE_2_COPY_BIT, VK_ACCESS_2_TRANSFER_WRITE_BIT, + VK_PIPELINE_STAGE_2_ALL_COMMANDS_BIT, VK_ACCESS_2_MEMORY_READ_BIT | VK_ACCESS_2_MEMORY_WRITE_BIT); + ex->recorded++; + break; + } + plat_mutex_unlock(&bcs_lock); +} + +// A draw into a 3D BC image without block views through an uncompressed view of its blocks: the target is the +// image's stand-in (bc_standin). 1 when o is filled (target or left out), 0 for any other target. +static int bc_target(const tex_t *t, const view_t *v, int has_v, const VkTexImage *img, VkExecTarget *o) { + int bw, bh; + if (img->type != VK_IMAGE_TYPE_3D || img->block_views || !nvn_fmt_bpb(t->d.format, &bw, &bh) || bw != 4 || bh != 4) + return 0; + VkFormat fmt; + BcStandIn s; + uint32_t level = has_v && v->set_levels ? v->base_level : 0; + if (level >= img->levels) { + warn_once(W_LEVEL, "render target level %u of a %u-level texture: left out", level, img->levels); + return 1; + } + if (!has_v || !v->set_format || !view_format(t, v, 1, &fmt) || !bc_standin(img, level, fmt, &s)) { + warn_once(W_BLOCK_VIEW, "render target format %#x (%ux%u) through view format %#x: no block view, left out", + t->d.format, t->d.width, t->d.height, has_v && v->set_format ? v->format : 0); + return 1; + } + uint32_t base = 0, layers = s.d; + if (v->set_layers) { + base = v->min_layer; + layers = v->nlayers ? v->nlayers : (base < s.d ? s.d - base : 0); + } + if (base >= s.d || !layers) return 1; + if (base + layers > s.d) layers = s.d - base; + o->image = s.img; // not img->image: view_of makes this command buffer's view of the stand-in + o->img = img; + o->format = fmt; + o->view_type = layers > 1 ? VK_IMAGE_VIEW_TYPE_2D_ARRAY : VK_IMAGE_VIEW_TYPE_2D; + o->aspect = VK_IMAGE_ASPECT_COLOR_BIT; + o->level = 0, o->base_layer = base, o->layers = layers; + o->width = s.w, o->height = s.h, o->samples = 1; + o->nvn_format = v->format; + return 1; +} + static void resolve_target(const tex_t *t, const view_t *v, int has_v, int depth_slot, VkExecTarget *o) { memset(o, 0, sizeof *o); o->tex = t; if (!t) return; const VkTexImage *img = vk_texture_get(t); + if (img && !depth_slot && bc_target(t, v, has_v, img, o)) return; // a 3D BC image: its stand-in VkImageUsageFlags need = depth_slot ? VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT : VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT; int is3d = img && img->type == VK_IMAGE_TYPE_3D && !depth_slot; // slices drawn through 2D views (2D_ARRAY_COMPATIBLE) if (!img || !(img->usage & need) || (img->type != VK_IMAGE_TYPE_2D && !is3d)) { @@ -469,6 +622,9 @@ void vk_exec_pass_end(VkExec *ex) { vkCmdEndRendering(ex->cb); ex->recorded++; ex->in_pass = 0; + for (int i = 0; i < ex->pass_ncolor && i < 8; i++) // stand-ins of 3D BC targets go to their textures + if (ex->color[i].image && ex->color[i].img && ex->color[i].img->image != ex->color[i].image) + bc_copy_back(ex, &ex->color[i]); if (vk_dump_armed) vk_dump_pass_end(ex); uint64_t stamp = __atomic_load_n(&gfx_counters.frames, __ATOMIC_RELAXED) + 1; for (int i = 0; i < 8; i++) diff --git a/runtime/vk_exec.h b/runtime/vk_exec.h index c81d5c61..6236929e 100644 --- a/runtime/vk_exec.h +++ b/runtime/vk_exec.h @@ -124,6 +124,8 @@ void vk_exec_inline_end(int reopen); // buffers): the replay continues in a new one (vk_sync_split). void vk_exec_draw_new_cb(void); VkBuffer vk_exec_inline_buffer(VkDeviceSize size); +// vk_exec.c: the stand-in a 3D BC render target is drawn through goes with its image (vk_exec_image_gone). +void vk_exec_bc_gone(VkImage image); // Scissor 0 clipped to the render area of the open pass; extent 0: nothing to draw. VkRect2D vk_exec_scissor(const VkExec *ex); void vk_exec_noatt_dims(const VkExec *ex, uint32_t *w, uint32_t *h); // pass size when no attachment is set diff --git a/runtime/vk_exec_draw.c b/runtime/vk_exec_draw.c index 59ff9cbb..436ba0a2 100644 --- a/runtime/vk_exec_draw.c +++ b/runtime/vk_exec_draw.c @@ -564,6 +564,7 @@ VkImageView vk_exec_target_view(const VkTexImage *img, VkFormat format, VkImageV } void vk_exec_image_gone(VkImage image) { + vk_exec_bc_gone(image); if (!views_by_image.klen) return; for (ViewNode *n = nvn_hm_del(&views_by_image, &image), *next; n; n = next) { next = n->next; diff --git a/runtime/vk_rec.c b/runtime/vk_rec.c index 4ebfeb79..6ccd30d3 100644 --- a/runtime/vk_rec.c +++ b/runtime/vk_rec.c @@ -38,7 +38,7 @@ enum { R_SetDepthWriteEnable, R_SetFrontFace, R_SetPrimitiveRestartEnable, R_SetPrimitiveTopology, R_SetRasterizerDiscardEnable, R_SetScissorWithCount, R_SetStencilCompareMask, R_SetStencilOp, R_SetStencilReference, R_SetStencilTestEnable, R_SetStencilWriteMask, R_SetViewportWithCount, R_WriteTimestamp, R_WriteTimestamp2, - R_PushTpl, R_Push, R_VertexInput, R_Bool, R_PolygonMode, R_BlendEnable, R_BlendEquation, R_WriteMask, + R_PushTpl, R_Push, R_VertexInput, R_Bool, R_PolygonMode, R_BlendEnable, R_BlendEquation, R_WriteMask, R_EndCb, }; static uint64_t now_ns(void) { @@ -131,7 +131,8 @@ void rec_begin(VkCommandBuffer cb) { if (mode < 0) { // Default off on AMD GPUs: with the recorder their drivers lost the device within the first frames or at menu // opens, 1 boot in 3 on an RX 9070 XT, and TOTK_VK_RECORDER=0 ended it for three users (issue #5); under RADV - // the kernel saw an illegal opcode at the end of a command buffer (issue #40). The cause is not found. + // the kernel saw an illegal opcode at the end of a command buffer (issue #40). Letting the recorder end its own + // parts (rec_end_cb) fixed the white scene on an RX 6700 XT; the default stays off until more AMD cards run it. const char *set = cfg_get("TOTK_VK_RECORDER"); mode = rt_knob("VK_RECORDER", vk_caps.amd ? "0" : "1"); if (!mode && vk_caps.amd && !(set && *set)) @@ -162,6 +163,32 @@ void rec_end(void) { rec_live = 0; } +// TOTK_REC_END (default 1): the thread that recorded the part also ends it, after the breadcrumb crumb (kind 0: none) +// that vk_sync.c writes at the end of a submission. On an RX 6700 XT (driver 26.10.1, issue #5) parts the recorder +// recorded and the submitting thread ended broke when a submission was split (vk_sync.c vk_sync_split): the GPU hung +// or the scene turned green, then white. The handoff in rec_drain orders the two threads, so Vulkan allows the old +// split; the driver did not cope with it. TOTK_REC_END=0 brings the old split back. +typedef struct { int kind; uint64_t n; } A_EndCb; +int rec_end_cb(VkCommandBuffer cb, int crumb, uint64_t n) { + static int mode = -1; + if (mode < 0) { + mode = rt_knob("REC_END", "1"); + rt_log("vk_rec: a part the recorder records is ended by %s (TOTK_REC_END=%d)", + mode ? "the recorder" : "the submitting thread", mode); + } + if (!rec_live || cb != rec_cb || !mode) { + rec_end(); + if (crumb) vk_crumb(cb, crumb, n); + return 0; + } + A_EndCb *a = put(R_EndCb, sizeof *a); + *a = (A_EndCb){crumb, n}; + pub(); + rec_drain(); + rec_live = 0; + return 1; +} + void rec_stats_log(void) { if (!(rt_trace & RT_TRACE_PERF) || !RS_.parts) return; rt_log("vk_rec: %llu of %llu parts recorded by the recorder: %llu packets (%.1f MiB), %llu drains (%llu for a call " @@ -588,6 +615,12 @@ static void exec_packet(uint32_t op, void *p) { case R_BlendEnable: { A_Targets *a = p; ((PFN_vkCmdSetColorBlendEnableEXT)a->fn)(cb, a->first, a->n, a->v); break; } case R_BlendEquation: { A_Targets *a = p; ((PFN_vkCmdSetColorBlendEquationEXT)a->fn)(cb, a->first, a->n, a->v); break; } case R_WriteMask: { A_Targets *a = p; ((PFN_vkCmdSetColorWriteMaskEXT)a->fn)(cb, a->first, a->n, a->v); break; } + case R_EndCb: { + A_EndCb *a = p; + if (a->kind) vk_crumb(cb, a->kind, a->n); + vkEndCommandBuffer(cb); + break; + } default: rt_fatal("vk_rec: unknown packet %u", op); } } diff --git a/runtime/vk_rec.h b/runtime/vk_rec.h index afbb2937..d4054e1e 100644 --- a/runtime/vk_rec.h +++ b/runtime/vk_rec.h @@ -4,7 +4,8 @@ // ring, in call order, and the recorder makes the real call in that order; calls into any other command buffer (uploads, // the window, probes) are made at once. A call the ring has no packet for (a structure with a pNext chain) waits for // the recorder to empty the ring and is then made at once on the calling thread. rec_end returns once the recorder made -// every call, so ending, submitting and the command pool stay on the submitting thread. +// every call. vk_sync.c ends a part through rec_end_cb: the recorder makes the last calls into a part it recorded, +// vkEndCommandBuffer included; submitting and the command pool stay on the submitting thread. // // Every vkCmd* the runtime calls through vk.h goes through the rc_* wrappers below (the macros at the end); the // extension commands called through function pointers have rec_* wrappers of their own. TOTK_VK_RECORDER=0 makes every @@ -16,6 +17,9 @@ void rec_begin(VkCommandBuffer cb); // the submitting thread, sub_lock held void rec_end(void); // waits for the recorder; no-op when rec_begin did not start it +// The end of part cb: the breadcrumb crumb (0: none, vk_crumbs.c), then vkEndCommandBuffer, both made by the recorder +// when it records cb, and the wait for it (1); otherwise rec_end and the breadcrumb, and the caller ends cb (0). +int rec_end_cb(VkCommandBuffer cb, int crumb, uint64_t n); void rec_drain(void); // waits until the recorder made every call queued so far void rec_stats_log(void); // TOTK_TRACE=perf: packets, bytes, drains and waits since the last call diff --git a/runtime/vk_sync.c b/runtime/vk_sync.c index 671a803e..6fb05a5b 100644 --- a/runtime/vk_sync.c +++ b/runtime/vk_sync.c @@ -293,9 +293,8 @@ static VkCommandBuffer prologue(VkCtx *c, uint64_t fence) { VkCommandBuffer vk_sync_split(void) { VkCtx *c = vk_ctx(); VkCommandBuffer cb = slots[cur_slot].cb; - rec_end(); // every command of the part is in cb ts_end(cb, cur_fence); - vkEndCommandBuffer(cb); + 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); VkCommandBufferSubmitInfo cbi[2] = {{.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_SUBMIT_INFO, .commandBuffer = pro}, @@ -348,11 +347,10 @@ uint64_t vk_sync_submit(const uint64_t *handles, int n) { rec_begin(s->cb); int recorded = vk_exec_record(s->cb, handles, n, fence); s = &slots[cur_slot]; // the last part (vk_sync_split) - rec_end(); - vk_crumb(s->cb, VK_CRUMB_SUBMIT_END, fence); // the recorder is done with the part if (recorded) ts_end(s->cb, fence); else ts_drop(); - vkEndCommandBuffer(s->cb); + // The recorder is done with the part: the end-of-submission crumb, then the end, by the recorder when it records. + if (!rec_end_cb(s->cb, VK_CRUMB_SUBMIT_END, fence)) vkEndCommandBuffer(s->cb); perf_replay(t0); // After the recording: the uploads it asked for (and every earlier one) precede it in queue order. vk_upload_flush(0); diff --git a/runtime/vk_texture.c b/runtime/vk_texture.c index 9631add8..f12e7934 100644 --- a/runtime/vk_texture.c +++ b/runtime/vk_texture.c @@ -328,7 +328,18 @@ static Tex *create_tex(const tex_t *t, const TexKey *k, int defer_init) { .samples = (VkSampleCountFlagBits)samples, .tiling = VK_IMAGE_TILING_OPTIMAL, .usage = usage, .initialLayout = VK_IMAGE_LAYOUT_UNDEFINED}; int bw, bh, block_views = 0; - if (!ds && samples == 1 && f.vk == f.ideal && nvn_fmt_bpb(d->format, &bw, &bh) && bw == 4 && bh == 4) { + // TOTK_BLOCK_VIEWS_3D (default 0): a 3D BC image also gets block views. On RDNA2 cards (driver 26.10.1, issue #5) + // the 32x32x32 BC4 volume the game creates at the logo, made BLOCK_TEXEL_VIEW_COMPATIBLE and 2D_ARRAY_COMPATIBLE, + // took a GPU write just past the end of its memory: device lost at the first submit after it when the image ended + // its memory block. Draws into a 3D BC image go through a stand-in instead (vk_exec.c bc_target). + static int bv3d = -1; + if (bv3d < 0) { + bv3d = rt_knob("BLOCK_VIEWS_3D", "0"); + rt_log("vk: 3D BC textures %s (TOTK_BLOCK_VIEWS_3D=%d)", bv3d ? "get block views (renderable)" : "have no block views", + bv3d); + } + if (!ds && samples == 1 && f.vk == f.ideal && nvn_fmt_bpb(d->format, &bw, &bh) && bw == 4 && bh == 4 && + (type != VK_IMAGE_TYPE_3D || bv3d)) { VkImageCreateInfo bc = ci; // BC formats: renderable through views of their blocks (block_views_ok) bc.usage |= VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT; bc.flags |= VK_IMAGE_CREATE_BLOCK_TEXEL_VIEW_COMPATIBLE_BIT | VK_IMAGE_CREATE_EXTENDED_USAGE_BIT;