diff --git a/docs/settings.md b/docs/settings.md index 047b63fd..83b774b5 100644 --- a/docs/settings.md +++ b/docs/settings.md @@ -172,6 +172,7 @@ Many rows below are developer tools that only act together with another setting | `ub_overlay` (default `1`) | `0` copies each uniform update inside a pass into its buffer at once, ending the pass with a full barrier; `ab` alternates with the `ab` blocks. | With `0` scenes with many updated blocks (the terrain, the sky islands) take longer on the GPU; `TOTK_TRACE=perf` prints how many updates stayed in their pass. | Caution | | `ub_overlay_out` (default `1`) | `0` copies each uniform update between passes into its buffer at once, behind a full barrier of its own; `ab` alternates with the `ab` blocks. Needs `ub_overlay`. | With `0` a run of updates before a pass costs a full GPU barrier each; `TOTK_TRACE=perf` prints how many updates between passes were overlaid. | Caution | | `ubo_snapshot` (default `1`) | `0` binds live memory for uniform blocks; `ab` alternates with the `ab` blocks. | Without snapshots late draws read another object's matrix, so objects can be drawn in the wrong place. | Risky | +| `null_attach` (default `0`) | `1` gives the colour slots of a render pass that hold no target, below its last target, a dummy attachment (an `R8_UNORM` image covering the pass, writes masked off, loads and stores `DONT_CARE`) instead of a null image view; the pipelines name that format for those slots. A workaround to try on Intel's legacy Windows driver for 6th-10th generation graphics (HD 520, UHD 620, driver 101.21xx), which crashes inside `vkCmdBeginRendering` after the logo (#1343); untested there, and the crash dumps do not show that the empty slots are the cause. Also in `totk.ini.example`. | One or two dummy images per scene resolution (1.4 MiB at 1600x896, at most 24, freed when the device goes); pipelines with empty slots are built again once. | Caution | | `vk_no_float64` (default `0`) | `1` clears the 64-bit float feature, so the translator lowers FP64 to FP32. | Shaders change, so the shader cache entries for it are made again; precision of FP64 shaders drops. | Caution | | `vk_no_host_import` (default `0`) | `1` leaves `VK_EXT_external_memory_host` off even when the GPU has it, so the game takes the path of a GPU without it: every CPU-visible pool gets shadow storage, as under Wine. For testing that path. | Shadow storage costs CPU copies before each submission and more memory than imports. | Caution | | `vk_ssbo_align` (default unset) | Power of two that storage buffer offsets are treated as needing, to test the unaligned path. | The translator and the draw code round storage offsets down to it; shaders are translated again. | Caution | diff --git a/runtime/cfg.c b/runtime/cfg.c index 32505b22..e6722ae8 100644 --- a/runtime/cfg.c +++ b/runtime/cfg.c @@ -56,7 +56,7 @@ static const char *const known[] = { "LIB_PREBUILD_KEEP", "LIB_PREBUILD_THREADS", "LIGHTNING_FIX", "LOCALE", "LOD_GUARD", "LOD_STEADY", "LSTICK_DEADZONE", "MAP_CLICK", "MEMORY", "MEMX", "MENU_LANG", "MENU_TEST", "MENU_FONT_FONTCONFIG", "MEM_RESERVE_MIB", "MENU_CURSOR", "MENU_CURSOR_LOG", "MENU_TEST_DIR", "MENU_TEST_SET", "MINIMAL_LAYOUT", "MIRROR_GAP", "MODS_DIR", "MODS_DISABLED", "MODS_NO_CODE", "MODS_ORDER", "MODS_REBUILD", "MODS_WITHOUT_CODE", "MOTION", "MOUSE_AIM_SCALE", "MOUSE_MODE", "MOUSE_DIRECT_PATCH", "MOUSE_SENS", "MOVIE_ASPECT", "MOVIE_HOST", "MOVIE_PACE", "MVOBJ_ALL", "MVOBJ_BANKS", "MVOBJ_EQUAL", "MVOBJ_EXACT", "MVOBJ_EXEC", "MVOBJ_NOC4", "MVOBJ_ONLY", "MVOBJ_TIMES", "MVPREV_CHECK", "MVPREV_GROW", "MVPREV_SKIP", "MVPREV_TRUST", "MV_CHECK", - "MV_SURVEY", + "MV_SURVEY", "NULL_ATTACH", "ONE_PER_CORE", "OPERATION_MODE", "OPTREF_GUARD", "OUTPUT_RES", "PAD_LAYOUT", "PASS_CRUMBS", "PATCH_OUTPUT", "PATCH_SCENE", "PCORE", "PCSS", "PCSS_K", "PERF_CENSUS", "PERF_OVERLAY", "PERF_SLOW_MS", "PIPELINE_CACHE", "PIPE_MEMO", "PIPE_PREBUILD", "PIPE_SKIP", "PIPE_SYNC", "PIPE_WAIT", "PLUGIN_TICK_OFFSET", "PLUGIN_TRACE", "POINTER_IMAGE", "PREWARM_XLATE", "PROBE_MOVIE", "PROBE_MOVIE_BENCH", "PROBE_MOVIE_SHOT", "PROFILE", "PROFILE_CHAINS", "PROFILE_DIR", "PROFILE_FRAMES", "PROFILE_MAIN", "PROFILE_RENDER", "PROMPTS", "PUSH_SKIP", "PUSH_STAGE", "QUICKLOAD_KEY", "QUICKSAVE", "QUICKSAVE_KEY", "RDOC", "RDOC_DIR", "RDOC_DLL", "REALTIME", "REC_ASYNC", "REC_END", "REC_SPIN_US", "REEXEC", "REFLEX", diff --git a/runtime/vk.h b/runtime/vk.h index 44244202..690e87f6 100644 --- a/runtime/vk.h +++ b/runtime/vk.h @@ -66,6 +66,7 @@ typedef struct { int mesa; // a Mesa driver the game runs on: RADV, ANV or NVK int intel_igpu_win; // Intel's Windows driver on an integrated GPU (UHD, Iris Xe, Core Ultra graphics) int demote; // shaderDemoteToHelperInvocation enabled: modules may use OpDemoteToHelperInvocation + int null_attach; // TOTK_NULL_ATTACH=1: empty colour slots get a dummy attachment (vk_exec.c null_attach_view) } VkCaps; extern VkCaps vk_caps; void vk_caps_driver(VkPhysicalDevice p); // first, before vkCreateDevice: vk_caps.amd, amd_sc, mesa, intel_igpu_win @@ -507,6 +508,10 @@ typedef struct { // into it, when the last draw into it was that copy and the swapchain is HDR10. void vk_hdr_present_source(const VkTexImage *win, VkImage *image, VkFormat *format); void vk_hdr_image_gone(VkImage image); +// vk_caps.null_attach: empty colour slots of a pass get a dummy VK_EXEC_NULL_ATTACH_FMT attachment, and the pipelines +// name that format for them with writes masked off (vk_exec.c null_attach_view, vk_pipeline.c gkey_fill). +#define VK_EXEC_NULL_ATTACH_FMT VK_FORMAT_R8_UNORM +void vk_exec_null_attach_release(void); // vk_device.c, before the device goes: frees the dummy attachments const VkTexImage *vk_texture_get(const tex_t *t); // created and made current; NULL for BUFFER or unmapped formats const VkTexImage *vk_texture_revive(const tex_t *t, uint32_t pos); // the entry vk_texture_get would return if it exists, made live at op pos // A texture slot; a slot whose texture was finalized follows the live texture at the same storage (hle_nvn.c). diff --git a/runtime/vk_caps.c b/runtime/vk_caps.c index bc99712e..2cf17362 100644 --- a/runtime/vk_caps.c +++ b/runtime/vk_caps.c @@ -123,6 +123,9 @@ void vk_caps_init(VkCtx *c) { uint32_t zero_locals = env_u32("TOTK_ZERO_LOCALS", (uint32_t)vk_caps.mesa); gfx_shader_set_host(vk_caps.float64, vk_caps.sbuf_align, told, gather, zero_locals != 0); if (gather) rt_log("vk caps: gather coordinates biased for %u sub-texel bits (TOTK_GATHER_BIAS)", gather); + // Read here, before any pipeline key is made: the pipelines' colour formats depend on it (vk_pipeline.c gkey_fill) + vk_caps.null_attach = rt_knob("NULL_ATTACH", "0") == 1; + if (vk_caps.null_attach) rt_log("vk caps: empty colour slots get a dummy attachment (TOTK_NULL_ATTACH=1)"); char dv[32]; driver_version(dv, sizeof dv, c->props.vendorID, c->props.driverVersion); diff --git a/runtime/vk_device.c b/runtime/vk_device.c index 5d54d8bf..9d458925 100644 --- a/runtime/vk_device.c +++ b/runtime/vk_device.c @@ -526,6 +526,7 @@ static void destroy_all(void) { if (g_vk.device) vk_astc_release(); if (g_vk.device) vk_deswz_release(); if (g_vk.device) vk_crumbs_release(); + if (g_vk.device) vk_exec_null_attach_release(); if (g_vk.timeline) vkDestroySemaphore(g_vk.device, g_vk.timeline, NULL); vk_headroom_stop(); if (g_vk.vma) vmaDestroyAllocator(g_vk.vma); diff --git a/runtime/vk_exec.c b/runtime/vk_exec.c index c16812cb..be372f99 100644 --- a/runtime/vk_exec.c +++ b/runtime/vk_exec.c @@ -674,6 +674,85 @@ static void barrier_to(VkExec *ex, const VkImage *want, const VkImageAspectFlags void vk_exec_barrier(VkExec *ex) { barrier_to(ex, NULL, NULL, 0); } +// TOTK_NULL_ATTACH=1 (null_attach, default 0; vk_caps.null_attach): colour slots of a pass that hold no target get a +// view of a dummy image instead of VK_NULL_HANDLE. Leaving a slot's imageView null is valid Vulkan, but Intel's legacy +// Windows driver for 6th-10th generation graphics (HD 520, UHD 620; driver 101.21xx, Vulkan 1.3.215) faulted inside +// vkCmdBeginRendering reading an attachment entry of the pass, at the first scene passes after the logo (issue #1343); +// whether the empty slots are the cause is untested on that driver. The pipelines then name NULL_ATTACH_FMT for those +// slots, with their writes masked off (vk_pipeline.c gkey_fill, target_wmask), so the views match the pipelines' +// formats. Only slots below the last one with a target get a dummy: the pipelines' colorAttachmentCount ends there +// (vk_pipeline.c target_count), and a pass without colour targets keeps none. All empty slots of a pass share one +// view; an image serves every pass it covers (size, layers) with its sample count, so TotK makes one or two per scene +// resolution. Each is moved to GENERAL in the upload batch and kept until the device goes (vk_exec_null_attach_release); +// loads and stores are DONT_CARE, so the slots cost no bandwidth. +#define NULL_ATTACH_MAX 24 +typedef struct { uint32_t w, h, layers, samples; VkImage img; VmaAllocation alloc; VkImageView view; } NullAttach; +static NullAttach null_att[NULL_ATTACH_MAX]; +static int nnull_att; +static PlatMutex null_att_lock = PLAT_MUTEX_INIT; + +void vk_exec_null_attach_release(void) { + VkCtx *c = vk_ctx(); + plat_mutex_lock(&null_att_lock); + for (int i = 0; c && i < nnull_att; i++) { + vkDestroyImageView(c->device, null_att[i].view, NULL); + vmaDestroyImage(c->vma, null_att[i].img, null_att[i].alloc); + } + nnull_att = 0; + plat_mutex_unlock(&null_att_lock); +} + +static VkImageView null_attach_view(uint32_t w, uint32_t h, uint32_t layers, uint32_t samples) { + plat_mutex_lock(&null_att_lock); + for (int i = 0; i < nnull_att; i++) { // a view may be larger than the render area (VkRenderingInfo needs it to cover it) + const NullAttach *a = &null_att[i]; + if (a->w >= w && a->h >= h && a->layers >= layers && a->samples == samples) { + VkImageView v = a->view; + plat_mutex_unlock(&null_att_lock); + return v; + } + } + static int warned; + if (nnull_att == NULL_ATTACH_MAX) { + if (!warned++) rt_log("vk_exec: more than %d dummy attachment shapes: further empty slots stay empty", NULL_ATTACH_MAX); + plat_mutex_unlock(&null_att_lock); + return VK_NULL_HANDLE; + } + VkCtx *c = vk_ctx(); + NullAttach a = {w, h, layers, samples, VK_NULL_HANDLE, NULL, VK_NULL_HANDLE}; + VkImageCreateInfo ci = {.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO, .imageType = VK_IMAGE_TYPE_2D, + .format = VK_EXEC_NULL_ATTACH_FMT, .extent = {w, h, 1}, .mipLevels = 1, .arrayLayers = layers, + .samples = (VkSampleCountFlagBits)samples, .tiling = VK_IMAGE_TILING_OPTIMAL, + .usage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT, .initialLayout = VK_IMAGE_LAYOUT_UNDEFINED}; + VmaAllocationCreateInfo ai = {.usage = VMA_MEMORY_USAGE_AUTO_PREFER_DEVICE}; + VmaAllocationInfo info = {0}; + if (vmaCreateImage(c->vma, &ci, &ai, &a.img, &a.alloc, &info) != VK_SUCCESS) { + if (!warned++) rt_log("vk_exec: a %ux%u x %u dummy attachment could not be created: the slot stays empty", w, h, layers); + plat_mutex_unlock(&null_att_lock); + return VK_NULL_HANDLE; + } + VkImageViewCreateInfo vi = {.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO, .image = a.img, + .viewType = layers > 1 ? VK_IMAGE_VIEW_TYPE_2D_ARRAY : VK_IMAGE_VIEW_TYPE_2D, + .format = VK_EXEC_NULL_ATTACH_FMT, + .subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, layers}}; + if (vkCreateImageView(c->device, &vi, NULL, &a.view) != VK_SUCCESS) { + vmaDestroyImage(c->vma, a.img, a.alloc); + if (!warned++) rt_log("vk_exec: a dummy attachment view could not be created: the slot stays empty"); + plat_mutex_unlock(&null_att_lock); + return VK_NULL_HANDLE; + } + // In the upload batch, which goes to the queue before the part that records this pass (as the BC stand-ins) + VkCommandBuffer cb = vk_upload_begin(); + image_barrier(cb, a.img, VK_IMAGE_LAYOUT_UNDEFINED, layers); + vk_upload_end(); + null_att[nnull_att++] = a; + int n = nnull_att; + plat_mutex_unlock(&null_att_lock); + rt_log("vk_exec: dummy attachment %ux%u x %u, %u samples, for empty colour slots: %.2f MiB (%d of %d)", w, h, layers, + samples, info.size / 1048576.0, n, NULL_ATTACH_MAX); + return a.view; +} + void vk_exec_pass_begin(VkExec *ex) { if (ex->in_pass) return; resolve_rt(ex); @@ -696,10 +775,24 @@ void vk_exec_pass_begin(VkExec *ex) { ex->fb_dirty = 0; ex->pass_serial++; VkRenderingAttachmentInfo ca[8], da = {0}, sa = {0}; + uint32_t dummies = 0; // null_attach: empty slots below this get a dummy (the pipelines' colorAttachmentCount) + VkImageView dummy = VK_NULL_HANDLE; + if (vk_caps.null_attach) + for (uint32_t i = 0; i < ex->pass_ncolor; i++) + if (ex->color[i].image) dummies = i + 1; for (uint32_t i = 0; i < ex->pass_ncolor; i++) { const VkExecTarget *t = &ex->color[i]; ca[i] = (VkRenderingAttachmentInfo){.sType = VK_STRUCTURE_TYPE_RENDERING_ATTACHMENT_INFO}; - if (!t->image) continue; + if (!t->image) { + if (i < dummies) { // a dummy for the empty slot, which the pipelines name too (null_attach_view) + if (!dummy) dummy = null_attach_view(ex->pass_w, ex->pass_h, ex->pass_layers, ex->pass_samples); + ca[i].imageView = dummy; + ca[i].imageLayout = VK_IMAGE_LAYOUT_GENERAL; + ca[i].loadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE; + ca[i].storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE; + } + continue; + } ca[i].imageView = view_of(ex, t); ca[i].imageLayout = fbl >> i & 1 ? VK_IMAGE_LAYOUT_ATTACHMENT_FEEDBACK_LOOP_OPTIMAL_EXT : VK_IMAGE_LAYOUT_GENERAL; ca[i].loadOp = VK_ATTACHMENT_LOAD_OP_LOAD; diff --git a/runtime/vk_pipeline.c b/runtime/vk_pipeline.c index b144140c..4e53487a 100644 --- a/runtime/vk_pipeline.c +++ b/runtime/vk_pipeline.c @@ -1345,6 +1345,7 @@ static uint32_t target_wmask(const VkPipeState *s, int i) { uint32_t m = s->chmask.mask >> (4 * i) & 0xf; if (s->fs && !(gfx_shader_outputs(s->fs, NULL) >> i & 1)) m = 0; if (i == 1 && s->fs && P.f_dual_src && uses_src1(s)) m = 0; // output 1 is the second blend source (spirv_dual_source) + if (!s->color_fmt[i] && vk_caps.null_attach) m = 0; // an empty slot's dummy attachment (vk_exec.c) takes no writes return m; } @@ -1380,6 +1381,9 @@ static void gkey_fill(const VkPipeState *s, GKey *k) { o[3] = (uint8_t)blend_factor(b->src_a), o[4] = (uint8_t)blend_factor(b->dst_a), o[5] = (uint8_t)blend_op(b->eq_a); } } + if (vk_caps.null_attach) // empty slots get the dummy attachment's format (vk_exec.c null_attach_view) + for (int i = 0; i < k->n_color; i++) + if (k->color[i] == VK_FORMAT_UNDEFINED) k->color[i] = VK_EXEC_NULL_ATTACH_FMT; if (!P.dyn_vi) { VtxInput v; vertex_input(s, &v); diff --git a/totk.ini.example b/totk.ini.example index 9aa2252f..3756bd4b 100644 --- a/totk.ini.example +++ b/totk.ini.example @@ -184,6 +184,10 @@ # vk_dyn_a2c = 1 # Pin the shader subgroup size to 32 when the GPU allows it. # vk_subgroup_pin = 1 +# 1 gives empty colour slots of a render pass a dummy image instead of none. Default: 0. A workaround to try if the +# game crashes right after the Nintendo logo on an Intel HD or UHD graphics chip (6th to 10th generation, such as +# HD 520 or UHD 620) with Intel's older driver (101.21xx); it costs a few MB of video memory. +# null_attach = 0 [display] # Device profile: auto (the Steam Deck when this is one), desktop or deck. On a Steam Deck the keys this file does not