Skip to content

Commit

Permalink
Vulkan: Properly shut down compilation threads
Browse files Browse the repository at this point in the history
  • Loading branch information
Exzap committed Dec 10, 2023
1 parent bffeb81 commit e7fa8ec
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
23 changes: 20 additions & 3 deletions src/Cafe/HW/Latte/Renderer/Vulkan/RendererShaderVk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ class _ShaderVkThreadPool
}
}

~_ShaderVkThreadPool()
void StopThreads()
{
m_shutdownThread.store(true);
for (uint32 i = 0; i < s_threads.size(); ++i)
Expand All @@ -149,6 +149,11 @@ class _ShaderVkThreadPool
s_threads.clear();
}

~_ShaderVkThreadPool()
{
StopThreads();
}

void CompilerThreadFunc()
{
while (!m_shutdownThread.load(std::memory_order::relaxed))
Expand Down Expand Up @@ -176,6 +181,8 @@ class _ShaderVkThreadPool
}
}

bool HasThreadsRunning() const { return !m_shutdownThread; }

public:
std::vector<std::thread> s_threads;

Expand All @@ -195,15 +202,25 @@ 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()
{
VulkanRenderer::GetInstance()->destroyShader(this);
}

void RendererShaderVk::Init()
{
ShaderVkThreadPool.StartThreads();
}

void RendererShaderVk::Shutdown()
{
ShaderVkThreadPool.StopThreads();
}

sint32 RendererShaderVk::GetUniformLocation(const char* name)
{
cemu_assert_suspicious();
Expand Down Expand Up @@ -457,4 +474,4 @@ void RendererShaderVk::ShaderCacheLoading_Close()
{
delete s_spirvCache;
s_spirvCache = nullptr;
}
}
3 changes: 3 additions & 0 deletions src/Cafe/HW/Latte/Renderer/Vulkan/RendererShaderVk.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ class RendererShaderVk : public RendererShader
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;
Expand Down
5 changes: 5 additions & 0 deletions src/Cafe/HW/Latte/Renderer/Vulkan/VulkanRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -591,13 +591,18 @@ 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()
{
SubmitCommandBuffer();
WaitDeviceIdle();
WaitCommandBufferFinished(GetCurrentCommandBufferId());
// shut down compilation threads
RendererShaderVk::Shutdown();
// shut down pipeline save thread
m_destructionRequested = true;
m_pipeline_cache_semaphore.notify();
Expand Down

0 comments on commit e7fa8ec

Please sign in to comment.