From ef36883ca1733a68162b199518275871c888391e Mon Sep 17 00:00:00 2001 From: jsmall-nvidia Date: Wed, 4 Sep 2019 16:16:17 -0400 Subject: Allow multiple threads to call glslang (#1046) * Allow multiple threads to be able to call into glslang by * Having a single call to glslang::InitializeProcess() per process (in our case dll load) * Explicitly call glslang::InitThread * Fix problem with case in path for linux. * Rename s_processInitializer to g_processInitializer. Move into the function. --- source/slang-glslang/slang-glslang.cpp | 52 ++++++++++++++++++++++++++++++++-- 1 file changed, 49 insertions(+), 3 deletions(-) diff --git a/source/slang-glslang/slang-glslang.cpp b/source/slang-glslang/slang-glslang.cpp index 68bf7b6a3..25b46bafe 100644 --- a/source/slang-glslang/slang-glslang.cpp +++ b/source/slang-glslang/slang-glslang.cpp @@ -12,6 +12,8 @@ #include "SPIRV/doc.h" #include "SPIRV/disassemble.h" +#include "OGLCompilersDLL/InitializeDll.h" + #include "../../slang.h" #if 0 @@ -190,6 +192,42 @@ static int glslang_dissassembleSPIRV(glslang_CompileRequest* request) return 0; } +// We need a per process initialization +class ProcessInitializer +{ +public: + ProcessInitializer() + { + m_isInitialized = false; + } + + bool init() + { + std::lock_guard guard(m_mutex); + if (!m_isInitialized) + { + if (!glslang::InitializeProcess()) + { + return false; + } + m_isInitialized = true; + } + return true; + } + + ~ProcessInitializer() + { + // We *assume* will only be called once dll is detatched and that will be on a single thread + if (m_isInitialized) + { + glslang::FinalizeProcess(); + } + } + + std::mutex m_mutex; + bool m_isInitialized = false; +}; + extern "C" #ifdef _MSC_VER _declspec(dllexport) @@ -198,7 +236,17 @@ __attribute__((__visibility__("default"))) #endif int glslang_compile(glslang_CompileRequest* request) { - glslang::InitializeProcess(); + static ProcessInitializer g_processInitializer; + if (!g_processInitializer.init()) + { + // Failed + return 1; + } + if (!glslang::InitThread()) + { + // Failed + return 1; + } int result = 0; switch(request->action) @@ -216,7 +264,5 @@ int glslang_compile(glslang_CompileRequest* request) break; } - glslang::FinalizeProcess(); - return result; } -- cgit v1.2.3