diff --git a/deps/glslang/glslang.cpp b/deps/glslang/glslang.cpp index 12b1d5deb9..0ca6c838d7 100644 --- a/deps/glslang/glslang.cpp +++ b/deps/glslang/glslang.cpp @@ -21,6 +21,7 @@ #include #include #include +#include #include "../../verbosity.h" @@ -31,18 +32,33 @@ struct SlangProcess { public: SlangProcess(); - TBuiltInResource& GetResources() { return Resources; } - ~SlangProcess() { FinalizeProcess(); } private: TBuiltInResource Resources; }; +// We don't use glslang from multiple threads, but to be sure. +// Initializing TLS and freeing it for glslang works around a really bizarre issue +// where the TLS key is suddenly corrupted *somehow*. +static std::mutex glslang_global_lock; +struct SlangProcessHolder +{ + SlangProcessHolder() + { + glslang_global_lock.lock(); + InitializeProcess(); + } + + ~SlangProcessHolder() + { + FinalizeProcess(); + glslang_global_lock.unlock(); + } +}; + SlangProcess::SlangProcess() { - InitializeProcess(); - char DefaultConfig[] = "MaxLights 32\n" "MaxClipPlanes 6\n" @@ -337,6 +353,7 @@ SlangProcess::SlangProcess() bool glslang::compile_spirv(const string &source, Stage stage, std::vector *spirv) { static SlangProcess process; + SlangProcessHolder process_holder; TProgram program; EShLanguage language; diff --git a/deps/glslang/glslang/OGLCompilersDLL/InitializeDll.cpp b/deps/glslang/glslang/OGLCompilersDLL/InitializeDll.cpp index abea9108b1..756da78b4c 100644 --- a/deps/glslang/glslang/OGLCompilersDLL/InitializeDll.cpp +++ b/deps/glslang/glslang/OGLCompilersDLL/InitializeDll.cpp @@ -158,6 +158,7 @@ bool DetachProcess() OS_FreeTLSIndex(ThreadInitializeIndex); ThreadInitializeIndex = OS_INVALID_TLS_INDEX; + DeinitializePoolIndex(); return success; } diff --git a/deps/glslang/glslang/glslang/Include/InitializeGlobals.h b/deps/glslang/glslang/glslang/Include/InitializeGlobals.h index 95d0a40e99..a9d27730b8 100644 --- a/deps/glslang/glslang/glslang/Include/InitializeGlobals.h +++ b/deps/glslang/glslang/glslang/Include/InitializeGlobals.h @@ -38,6 +38,7 @@ namespace glslang { bool InitializePoolIndex(); +bool DeinitializePoolIndex(); } // end namespace glslang diff --git a/deps/glslang/glslang/glslang/MachineIndependent/PoolAlloc.cpp b/deps/glslang/glslang/glslang/MachineIndependent/PoolAlloc.cpp index 84c40f4e79..c27131d56e 100644 --- a/deps/glslang/glslang/glslang/MachineIndependent/PoolAlloc.cpp +++ b/deps/glslang/glslang/glslang/MachineIndependent/PoolAlloc.cpp @@ -65,6 +65,15 @@ bool InitializePoolIndex() return true; } +bool DeinitializePoolIndex() +{ + if (PoolIndex == OS_INVALID_TLS_INDEX) + return false; + OS_FreeTLSIndex(PoolIndex); + PoolIndex = OS_INVALID_TLS_INDEX; + return true; +} + // // Implement the functionality of the TPoolAllocator class, which // is documented in PoolAlloc.h. diff --git a/deps/glslang/glslang/glslang/MachineIndependent/ShaderLang.cpp b/deps/glslang/glslang/glslang/MachineIndependent/ShaderLang.cpp index 7a3061821d..8f9d0f7212 100644 --- a/deps/glslang/glslang/glslang/MachineIndependent/ShaderLang.cpp +++ b/deps/glslang/glslang/glslang/MachineIndependent/ShaderLang.cpp @@ -1312,6 +1312,7 @@ int __fastcall ShFinalize() glslang::HlslScanContext::deleteKeywordMap(); #endif + DetachProcess(); return 1; }