From f278e91a7c07d8e790b5038032c3d9160f01a312 Mon Sep 17 00:00:00 2001 From: Ruan Shannon Date: Sun, 22 Nov 2026 16:49:02 -0800 Subject: [PATCH] null_attach = 1 gives empty colour slots a dummy attachment instead of a null view (Intel legacy driver crash, #1343) On an HD 520 with Intel's legacy Windows driver (101.2125, Vulkan 1.3.215) the game faults inside vkCmdBeginRendering (igvk64.dll+0x5cfab2/+0x5cfab9, frames +0x5c42dd and +0x548235) right after the logo, with the recorder on or off and with import or mirror memory. The driver reads an attachment entry of the pass that is null. vk_exec_pass_begin leaves imageView VK_NULL_HANDLE in every colour slot without a target, which Vulkan allows, and the first pass of a boot that has one is the 1600x896 scene G-buffer pass (slots 2 and 4 empty), the render area of the reported crash. With TOTK_NULL_ATTACH=1 such a slot gets a view of a dummy R8_UNORM image of the pass's size, layer and sample count (one per shape, made once, GENERAL, DONT_CARE load and store), and the pipelines name that format for it with the writes masked off (vk_pipeline.c gkey_fill, target_wmask), so views and pipelines agree. Off by default: nothing changes, and pipeline keys stay as they were. Untested on the affected driver; Iris Xe (101.7092) plays the same with it on. --- docs/settings.md | 1 + runtime/cfg.c | 2 +- runtime/vk.h | 4 +++ runtime/vk_exec.c | 80 ++++++++++++++++++++++++++++++++++++++++++- runtime/vk_pipeline.c | 4 +++ 5 files changed, 89 insertions(+), 2 deletions(-) diff --git a/docs/settings.md b/docs/settings.md index 14f8207b..4a61749e 100644 --- a/docs/settings.md +++ b/docs/settings.md @@ -170,6 +170,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 | | `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_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 | | `vk_subgroup` (default `32`) | Subgroup size to pin where the driver allows it. | A value other than 32 changes what the translator is told about the GPU, and shaders are translated again. | Caution | diff --git a/runtime/cfg.c b/runtime/cfg.c index 4345978a..bf780810 100644 --- a/runtime/cfg.c +++ b/runtime/cfg.c @@ -66,7 +66,7 @@ static const char *const known[] = { "SHADER_MEMO", "SHADOW", "SHADOW_DEFER", "SHADOW_RANGE", "SHADOW_RES", "SHADOW_SCALE", "SHADOW_SNAP", "SHARED_FONTS", "SHARPNESS", "SHOTS", "SHOTS_DIR", "SHUTTER_FIX", "SHOTS_FULL", "SPEED_PROBE", "SPIN", "SPIN_HANDOFF_US", "SPLIT_DRAWS", "SPLIT_US", "STICK_DEADZONE", "STICK_SENS", "SWKBD_ANSWER", "SYNC_AHEAD", - "SWKBD_TEST", "TEST_OOM", "TEST_PAD", "TEST_PAD_RUMBLE_MS", "TEST_PREPARE_SHADERS", "TEX_ALIAS", "TEX_BLOCK_MIB", "TEX_HEAP_EXTRA_MB", "TEX_MEMO", "TEX_RELEASED_MIB", "TEX_VRAM_PRESSURE", "TIMER_1MS", "TRACE", "TXD_TRANSPOSE", "UBO_SNAPSHOT", "UB_OVERLAY", "UB_OVERLAY_OUT", "UPSCALER", "VB_USED", "VK_DYN_A2C", "VK_DYN_VERTEX", "VK_NO_FLOAT64", + "SWKBD_TEST", "TEST_OOM", "TEST_PAD", "TEST_PAD_RUMBLE_MS", "TEST_PREPARE_SHADERS", "TEX_ALIAS", "TEX_BLOCK_MIB", "TEX_HEAP_EXTRA_MB", "TEX_MEMO", "TEX_RELEASED_MIB", "TEX_VRAM_PRESSURE", "TIMER_1MS", "TRACE", "TXD_TRANSPOSE", "UBO_SNAPSHOT", "UB_OVERLAY", "UB_OVERLAY_OUT", "UPSCALER", "VB_USED", "VK_DYN_A2C", "VK_DYN_VERTEX", "NULL_ATTACH", "VK_NO_FLOAT64", "VK_PRESENT", "VK_RECORDER", "VK_SSBO_ALIGN", "VK_SUBGROUP", "VK_SUBGROUP_PIN", "VK_VALIDATION", "VMA_BLOCK_MIB", "VOLUME", "VOLUME_MASK_BIAS", "VOLUME_MUSIC", "VOLUME_SFX", "VOLUME_VOICE", "VRAM_HEADROOM", "VRAM_LIMIT_MIB", "VS_OUTS", "WALK_KEY", "WALK_SCALE", "WB_EAGER", "WB_EXACT", "WB_FENCE_LIMIT", "WB_MERGE", "WINDOW_SIZE", "XLATE", "XLATE_ID", "XLATE_KEEP", "XLATE_PROCS", "ZSTD_CHECK", "ZSTD_NATIVE", diff --git a/runtime/vk.h b/runtime/vk.h index 9833a213..ab7f3ad0 100644 --- a/runtime/vk.h +++ b/runtime/vk.h @@ -502,6 +502,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). +#define VK_EXEC_NULL_ATTACH_FMT VK_FORMAT_R8_UNORM +int vk_exec_null_attach(void); 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_exec.c b/runtime/vk_exec.c index c16812cb..830015b4 100644 --- a/runtime/vk_exec.c +++ b/runtime/vk_exec.c @@ -674,6 +674,76 @@ 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. +#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)"); + } + return on; +} + +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++) { + 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}; + if (vmaCreateImage(c->vma, &ci, &ai, &a.img, &a.alloc, NULL) != 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; + 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); + return a.view; +} + void vk_exec_pass_begin(VkExec *ex) { if (ex->in_pass) return; resolve_rt(ex); @@ -699,7 +769,15 @@ void vk_exec_pass_begin(VkExec *ex) { 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 (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); + 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..e7df13de 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_exec_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_exec_null_attach()) // empty slots get the dummy attachment's format (vk_exec.c vk_exec_null_attach) + 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);