From e7fa8ec0c63f37a93fbcb0c7b372d5fb8640091e Mon Sep 17 00:00:00 2001 From: Exzap <13877693+Exzap@users.noreply.github.com> Date: Wed, 6 Dec 2023 02:16:17 +0100 Subject: [PATCH] Vulkan: Properly shut down compilation threads --- .../Renderer/Vulkan/RendererShaderVk.cpp | 23 ++++++++++++++++--- .../Latte/Renderer/Vulkan/RendererShaderVk.h | 3 +++ .../Latte/Renderer/Vulkan/VulkanRenderer.cpp | 5 ++++ 3 files changed, 28 insertions(+), 3 deletions(-) diff --git a/src/Cafe/HW/Latte/Renderer/Vulkan/RendererShaderVk.cpp b/src/Cafe/HW/Latte/Renderer/Vulkan/RendererShaderVk.cpp index e4c87d62..8460c8b5 100644 --- a/src/Cafe/HW/Latte/Renderer/Vulkan/RendererShaderVk.cpp +++ b/src/Cafe/HW/Latte/Renderer/Vulkan/RendererShaderVk.cpp @@ -139,7 +139,7 @@ public: } } - ~_ShaderVkThreadPool() + void StopThreads() { m_shutdownThread.store(true); for (uint32 i = 0; i < s_threads.size(); ++i) @@ -149,6 +149,11 @@ public: s_threads.clear(); } + ~_ShaderVkThreadPool() + { + StopThreads(); + } + void CompilerThreadFunc() { while (!m_shutdownThread.load(std::memory_order::relaxed)) @@ -176,6 +181,8 @@ public: } } + bool HasThreadsRunning() const { return !m_shutdownThread; } + public: std::vector s_threads; @@ -195,8 +202,8 @@ RendererShaderVk::RendererShaderVk(ShaderType type, uint64 baseHash, uint64 auxH m_compilationState.setValue(COMPILATION_STATE::QUEUED); ShaderVkThreadPool.s_compilationQueue.push_back(this); ShaderVkThreadPool.s_compilationQueueCount.increment(); - ShaderVkThreadPool.StartThreads(); ShaderVkThreadPool.s_compilationQueueMutex.unlock(); + cemu_assert_debug(ShaderVkThreadPool.HasThreadsRunning()); // make sure .StartThreads() was called } RendererShaderVk::~RendererShaderVk() @@ -204,6 +211,16 @@ RendererShaderVk::~RendererShaderVk() VulkanRenderer::GetInstance()->destroyShader(this); } +void RendererShaderVk::Init() +{ + ShaderVkThreadPool.StartThreads(); +} + +void RendererShaderVk::Shutdown() +{ + ShaderVkThreadPool.StopThreads(); +} + sint32 RendererShaderVk::GetUniformLocation(const char* name) { cemu_assert_suspicious(); @@ -457,4 +474,4 @@ void RendererShaderVk::ShaderCacheLoading_Close() { delete s_spirvCache; s_spirvCache = nullptr; -} \ No newline at end of file +} diff --git a/src/Cafe/HW/Latte/Renderer/Vulkan/RendererShaderVk.h b/src/Cafe/HW/Latte/Renderer/Vulkan/RendererShaderVk.h index 561145f9..207ea3ea 100644 --- a/src/Cafe/HW/Latte/Renderer/Vulkan/RendererShaderVk.h +++ b/src/Cafe/HW/Latte/Renderer/Vulkan/RendererShaderVk.h @@ -28,6 +28,9 @@ public: RendererShaderVk(ShaderType type, uint64 baseHash, uint64 auxHash, bool isGameShader, bool isGfxPackShader, const std::string& glslCode); virtual ~RendererShaderVk(); + static void Init(); + static void Shutdown(); + sint32 GetUniformLocation(const char* name) override; void SetUniform1iv(sint32 location, void* data, sint32 count) override; void SetUniform2fv(sint32 location, void* data, sint32 count) override; diff --git a/src/Cafe/HW/Latte/Renderer/Vulkan/VulkanRenderer.cpp b/src/Cafe/HW/Latte/Renderer/Vulkan/VulkanRenderer.cpp index 052ca21a..5b4dd739 100644 --- a/src/Cafe/HW/Latte/Renderer/Vulkan/VulkanRenderer.cpp +++ b/src/Cafe/HW/Latte/Renderer/Vulkan/VulkanRenderer.cpp @@ -591,6 +591,9 @@ VulkanRenderer::VulkanRenderer() { //cemuLog_log(LogType::Force, "Disable surface copies via buffer (Requires 2GB. Has only {}MB available)", availableSurfaceCopyBufferMem / 1024ull / 1024ull); } + + // start compilation threads + RendererShaderVk::Init(); } VulkanRenderer::~VulkanRenderer() @@ -598,6 +601,8 @@ VulkanRenderer::~VulkanRenderer() SubmitCommandBuffer(); WaitDeviceIdle(); WaitCommandBufferFinished(GetCurrentCommandBufferId()); + // shut down compilation threads + RendererShaderVk::Shutdown(); // shut down pipeline save thread m_destructionRequested = true; m_pipeline_cache_semaphore.notify();