From b5b9cc2be8401b94d78e668094051f920383828e Mon Sep 17 00:00:00 2001 From: Valiantsin Dzerakh Date: Mon, 23 Nov 2026 19:01:26 +0300 Subject: [PATCH] null_attach reads its switch once at device creation and gives a pass one dummy that covers it, freed with the device (#1343) With the switch off, the draw path now pays one load of vk_caps.null_attach per gkey_fill and per empty slot in target_wmask instead of a call into vk_exec.c, and vk_exec_pass_begin one flag test per pass. The switch is read in vk_caps_init, before the pipeline prewarm makes the first key. With it on, only empty slots below the pass's last target get a dummy, which is where the pipelines' colorAttachmentCount ends (vk_pipeline.c target_count); a pass without colour targets keeps none, so a depth-only or attachment-free pass never gets a 16384-wide image. All empty slots of a pass share one view, and an image serves every pass it covers with its sample count: 1.4.3 at 1600x896 makes one image of 1.75 MiB instead of three. The images are destroyed in destroy_all before the allocator goes, and the log line gives each one's size. The settings row no longer states that empty slots caused #1343: the dumps do not show it. totk.ini.example gets the player entry. Co-Authored-By: Claude Opus 5.5 --- docs/settings.md | 2 +- runtime/vk.h | 7 +++--- runtime/vk_caps.c | 3 +++ runtime/vk_device.c | 1 + runtime/vk_exec.c | 53 +++++++++++++++++++++++++++---------------- runtime/vk_pipeline.c | 4 ++-- totk.ini.example | 4 ++++ 7 files changed, 49 insertions(+), 25 deletions(-) diff --git a/docs/settings.md b/docs/settings.md index e20f0f9b..83b774b5 100644 --- a/docs/settings.md +++ b/docs/settings.md @@ -172,7 +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 a dummy attachment (an `R8_UNORM` image of the pass's size, writes masked off) instead of a null image view. A workaround to try on Intel's legacy Windows driver for 6th-10th generation graphics (HD 520, UHD 620, driver 101.21xx), which crashed inside `vkCmdBeginRendering` at the first pass with an empty slot (#1343). | A little memory per pass size (1.4 MiB at 1600x896); pipelines with empty slots are built again once. | Caution | +| `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/vk.h b/runtime/vk.h index 51ea8ecf..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,10 +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); -// TOTK_NULL_ATTACH=1: 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 vk_exec_null_attach, vk_pipeline.c gkey_fill). +// 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 -int vk_exec_null_attach(void); +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 830015b4..be372f99 100644 --- a/runtime/vk_exec.c +++ b/runtime/vk_exec.c @@ -674,33 +674,39 @@ 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): 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 at -// the game's first pass with an empty slot, the 1600x896 scene G-buffer pass with slots 2 and 4 empty (issue #1343). -// 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. One image per pass size, layer count and sample count, -// in GENERAL from the start (moved there in the upload batch) and kept to the end; loads and stores are DONT_CARE. +// 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; -int vk_exec_null_attach(void) { - static int on = -1; - if (on < 0) { - on = rt_knob("NULL_ATTACH", "0") == 1; - if (on) rt_log("vk_exec: empty colour slots get a dummy attachment (TOTK_NULL_ATTACH=1)"); +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); } - return on; + 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++) { + 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) { + 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; @@ -719,7 +725,8 @@ static VkImageView null_attach_view(uint32_t w, uint32_t h, uint32_t layers, uin .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}; - if (vmaCreateImage(c->vma, &ci, &ai, &a.img, &a.alloc, NULL) != VK_SUCCESS) { + 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; @@ -739,8 +746,10 @@ static VkImageView null_attach_view(uint32_t w, uint32_t h, uint32_t layers, uin 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", w, h, layers, samples); + 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; } @@ -766,12 +775,18 @@ 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) { - if (vk_exec_null_attach()) { // a dummy for the empty slot, which the pipelines name too (see above) - ca[i].imageView = null_attach_view(ex->pass_w, ex->pass_h, ex->pass_layers, ex->pass_samples); + 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; diff --git a/runtime/vk_pipeline.c b/runtime/vk_pipeline.c index e7df13de..4e53487a 100644 --- a/runtime/vk_pipeline.c +++ b/runtime/vk_pipeline.c @@ -1345,7 +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_exec_null_attach()) m = 0; // an empty slot's dummy attachment (vk_exec.c) takes no writes + 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; } @@ -1381,7 +1381,7 @@ 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_exec_null_attach()) // empty slots get the dummy attachment's format (vk_exec.c vk_exec_null_attach) + 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) { 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