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.