From 0655fcc711bb32df0c132798f8793aa19032f0a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Sat, 29 Aug 2026 00:40:00 +0200 Subject: [PATCH] Vulkan: Guard the render pass cache against the render thread GetRenderPass() looks up and inserts into renderPasses_ from the main thread (EndCurRenderStep, CreateGraphicsPipeline) and from the render thread (PerformBindFramebufferAsRenderTarget), unsynchronized. The render thread really does insert rather than only hit: PreprocessSteps rewrites the load actions to CLEAR when it merges a clear-only pass into a later one, after the main thread already looked up the pre-merge key. DenseHashMap::Insert can Grow(), which reallocates the buckets out from under a concurrent Get(). VKRRenderPass::Get() has the same problem one level down - it creates the passes lazily and is called from both threads on the same object, so two threads hitting an empty slot each create a pass and one gets overwritten and leaked, while the sample-count branch can queue a pass for deletion that the other thread is about to hand to vkCreateGraphicsPipelines. A mutex each. Handing the VKRRenderPass pointer out from under the map lock is fine, entries are only ever erased all at once in DestroyDeviceObjects. Co-Authored-By: Claude Opus 5 Claude-Session: https://claude.ai/code/session_01Vd8ntC2brCUtCrDJMqLbs8 --- Common/GPU/Vulkan/VulkanFramebuffer.cpp | 3 +++ Common/GPU/Vulkan/VulkanFramebuffer.h | 12 ++++++++++++ Common/GPU/Vulkan/VulkanQueueRunner.cpp | 9 +++++++++ Common/GPU/Vulkan/VulkanQueueRunner.h | 3 +++ 4 files changed, 27 insertions(+) diff --git a/Common/GPU/Vulkan/VulkanFramebuffer.cpp b/Common/GPU/Vulkan/VulkanFramebuffer.cpp index d2073367e8..da0d06311c 100644 --- a/Common/GPU/Vulkan/VulkanFramebuffer.cpp +++ b/Common/GPU/Vulkan/VulkanFramebuffer.cpp @@ -559,6 +559,9 @@ VkRenderPass VKRRenderPass::Get(VulkanContext *vulkan, RenderPassType rpType, Vk _dbg_assert_(!((rpType & RenderPassType::MULTISAMPLE) && sampleCount == VK_SAMPLE_COUNT_1_BIT)); + // Called from both the main thread and the render thread, see the note on mutex_. + std::lock_guard lock(mutex_); + if (!pass[(int)rpType] || sampleCounts[(int)rpType] != sampleCount) { if (pass[(int)rpType]) { vulkan->Delete().QueueDeleteRenderPass(pass[(int)rpType]); diff --git a/Common/GPU/Vulkan/VulkanFramebuffer.h b/Common/GPU/Vulkan/VulkanFramebuffer.h index a21cfbc37e..0b686b3929 100644 --- a/Common/GPU/Vulkan/VulkanFramebuffer.h +++ b/Common/GPU/Vulkan/VulkanFramebuffer.h @@ -1,5 +1,7 @@ #pragma once +#include + #include "Common/Common.h" #include "Common/GPU/Vulkan/VulkanContext.h" @@ -144,6 +146,8 @@ public: explicit VKRRenderPass(const RPKey &key) : key_(key) {} VkRenderPass Get(VulkanContext *vulkan, RenderPassType rpType, VkSampleCountFlagBits sampleCount); + + // Only called from VulkanQueueRunner::DestroyDeviceObjects, with the threads stopped - no lock needed. void Destroy(VulkanContext *vulkan) { for (size_t i = 0; i < (size_t)RenderPassType::TYPE_COUNT; i++) { if (pass[i]) { @@ -153,6 +157,14 @@ public: } private: + // Get() creates the passes lazily, and runs on both the main thread (EndCurRenderStep) and the + // render thread (PerformRenderPass), so the arrays below need guarding. Without it, two threads + // reaching the same empty slot each create a pass and one gets overwritten and leaked - and the + // sample count branch can queue a pass for deletion that the other thread is about to use. + // Lock ordering: taken while VKRGraphicsPipeline::mutex_ is held (VulkanQueueRunner), never the + // other way around. + std::mutex mutex_; + // TODO: Might be better off with a hashmap once the render pass type count grows really large.. VkRenderPass pass[(size_t)RenderPassType::TYPE_COUNT]{}; VkSampleCountFlagBits sampleCounts[(size_t)RenderPassType::TYPE_COUNT]{}; diff --git a/Common/GPU/Vulkan/VulkanQueueRunner.cpp b/Common/GPU/Vulkan/VulkanQueueRunner.cpp index 8e1282c51d..1288618c4b 100644 --- a/Common/GPU/Vulkan/VulkanQueueRunner.cpp +++ b/Common/GPU/Vulkan/VulkanQueueRunner.cpp @@ -197,6 +197,13 @@ void VulkanQueueRunner::DestroyBackBuffers() { // Self-dependency: https://github.com/gpuweb/gpuweb/issues/442#issuecomment-547604827 // Also see https://www.khronos.org/registry/vulkan/specs/1.3-extensions/html/vkspec.html#synchronization-pipeline-barriers-subpass-self-dependencies VKRRenderPass *VulkanQueueRunner::GetRenderPass(const RPKey &key) { + // Called from the main thread (EndCurRenderStep, CreateGraphicsPipeline) and from the render thread + // (PerformBindFramebufferAsRenderTarget). The render thread really does insert new keys, not just hit + // existing ones - PreprocessSteps rewrites the load actions to CLEAR when it merges a clear-only pass + // into a later one, after the main thread already looked up the pre-merge key. Insert() can Grow(), + // which reallocates the buckets out from under a concurrent Get(). + std::lock_guard lock(renderPassesMutex_); + VKRRenderPass *foundPass; if (renderPasses_.Get(key, &foundPass)) { return foundPass; @@ -204,6 +211,8 @@ VKRRenderPass *VulkanQueueRunner::GetRenderPass(const RPKey &key) { VKRRenderPass *pass = new VKRRenderPass(key); renderPasses_.Insert(key, pass); + // Safe to hand out the pointer once the lock is dropped - entries are never erased individually, + // only all at once in DestroyDeviceObjects. return pass; } diff --git a/Common/GPU/Vulkan/VulkanQueueRunner.h b/Common/GPU/Vulkan/VulkanQueueRunner.h index 92865099a9..2e256bb3df 100644 --- a/Common/GPU/Vulkan/VulkanQueueRunner.h +++ b/Common/GPU/Vulkan/VulkanQueueRunner.h @@ -253,6 +253,7 @@ public: VKRRenderPass *GetRenderPass(const RPKey &key); bool GetRenderPassKey(VKRRenderPass *passToFind, RPKey *outKey) const { + std::lock_guard lock(renderPassesMutex_); bool found = false; renderPasses_.Iterate([passToFind, &found, outKey](const RPKey &rpkey, const VKRRenderPass *pass) { if (pass == passToFind) { @@ -302,6 +303,8 @@ private: // Renderpasses, all combinations of preserving or clearing or dont-care-ing fb contents. // Each VKRRenderPass contains all compatibility classes (which attachments they have, etc). + // Looked up and inserted into from both the main thread and the render thread - see GetRenderPass. + mutable std::mutex renderPassesMutex_; DenseHashMap renderPasses_; // Readback buffer. Currently we only support synchronous readback, so we only really need one.