Skip to content

Commit e7fa8ec

Browse files
committed
Vulkan: Properly shut down compilation threads
1 parent bffeb81 commit e7fa8ec

File tree

3 files changed

+28
-3
lines changed

3 files changed

+28
-3
lines changed

src/Cafe/HW/Latte/Renderer/Vulkan/RendererShaderVk.cpp

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ class _ShaderVkThreadPool
139139
}
140140
}
141141

142-
~_ShaderVkThreadPool()
142+
void StopThreads()
143143
{
144144
m_shutdownThread.store(true);
145145
for (uint32 i = 0; i < s_threads.size(); ++i)
@@ -149,6 +149,11 @@ class _ShaderVkThreadPool
149149
s_threads.clear();
150150
}
151151

152+
~_ShaderVkThreadPool()
153+
{
154+
StopThreads();
155+
}
156+
152157
void CompilerThreadFunc()
153158
{
154159
while (!m_shutdownThread.load(std::memory_order::relaxed))
@@ -176,6 +181,8 @@ class _ShaderVkThreadPool
176181
}
177182
}
178183

184+
bool HasThreadsRunning() const { return !m_shutdownThread; }
185+
179186
public:
180187
std::vector<std::thread> s_threads;
181188

@@ -195,15 +202,25 @@ RendererShaderVk::RendererShaderVk(ShaderType type, uint64 baseHash, uint64 auxH
195202
m_compilationState.setValue(COMPILATION_STATE::QUEUED);
196203
ShaderVkThreadPool.s_compilationQueue.push_back(this);
197204
ShaderVkThreadPool.s_compilationQueueCount.increment();
198-
ShaderVkThreadPool.StartThreads();
199205
ShaderVkThreadPool.s_compilationQueueMutex.unlock();
206+
cemu_assert_debug(ShaderVkThreadPool.HasThreadsRunning()); // make sure .StartThreads() was called
200207
}
201208

202209
RendererShaderVk::~RendererShaderVk()
203210
{
204211
VulkanRenderer::GetInstance()->destroyShader(this);
205212
}
206213

214+
void RendererShaderVk::Init()
215+
{
216+
ShaderVkThreadPool.StartThreads();
217+
}
218+
219+
void RendererShaderVk::Shutdown()
220+
{
221+
ShaderVkThreadPool.StopThreads();
222+
}
223+
207224
sint32 RendererShaderVk::GetUniformLocation(const char* name)
208225
{
209226
cemu_assert_suspicious();
@@ -457,4 +474,4 @@ void RendererShaderVk::ShaderCacheLoading_Close()
457474
{
458475
delete s_spirvCache;
459476
s_spirvCache = nullptr;
460-
}
477+
}

src/Cafe/HW/Latte/Renderer/Vulkan/RendererShaderVk.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ class RendererShaderVk : public RendererShader
2828
RendererShaderVk(ShaderType type, uint64 baseHash, uint64 auxHash, bool isGameShader, bool isGfxPackShader, const std::string& glslCode);
2929
virtual ~RendererShaderVk();
3030

31+
static void Init();
32+
static void Shutdown();
33+
3134
sint32 GetUniformLocation(const char* name) override;
3235
void SetUniform1iv(sint32 location, void* data, sint32 count) override;
3336
void SetUniform2fv(sint32 location, void* data, sint32 count) override;

src/Cafe/HW/Latte/Renderer/Vulkan/VulkanRenderer.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -591,13 +591,18 @@ VulkanRenderer::VulkanRenderer()
591591
{
592592
//cemuLog_log(LogType::Force, "Disable surface copies via buffer (Requires 2GB. Has only {}MB available)", availableSurfaceCopyBufferMem / 1024ull / 1024ull);
593593
}
594+
595+
// start compilation threads
596+
RendererShaderVk::Init();
594597
}
595598

596599
VulkanRenderer::~VulkanRenderer()
597600
{
598601
SubmitCommandBuffer();
599602
WaitDeviceIdle();
600603
WaitCommandBufferFinished(GetCurrentCommandBufferId());
604+
// shut down compilation threads
605+
RendererShaderVk::Shutdown();
601606
// shut down pipeline save thread
602607
m_destructionRequested = true;
603608
m_pipeline_cache_semaphore.notify();

0 commit comments

Comments
 (0)