From d8584b9d94d9038f843574c8762779a39d02baca Mon Sep 17 00:00:00 2001 From: BearOso Date: Wed, 22 Feb 2023 16:21:00 -0600 Subject: [PATCH] slang: Fix race condition to initialize glslang. --- vulkan/slang_preset.cpp | 3 ++- vulkan/slang_shader.cpp | 42 +++++++---------------------------------- vulkan/slang_shader.hpp | 1 + 3 files changed, 10 insertions(+), 36 deletions(-) diff --git a/vulkan/slang_preset.cpp b/vulkan/slang_preset.cpp index 6ee21516..bdb63c3e 100644 --- a/vulkan/slang_preset.cpp +++ b/vulkan/slang_preset.cpp @@ -107,10 +107,11 @@ bool SlangPreset::load_preset_file(string filename) } } + SlangShader::initialize_glslang(); std::vector> futures; for (size_t i = 0; i < passes.size(); i++) { - futures.push_back(std::async(std::launch::async, [&, i]() -> bool { + futures.push_back(std::async(std::launch::async, [this, i]() -> bool { return passes[i].load_file(); })); } diff --git a/vulkan/slang_shader.cpp b/vulkan/slang_shader.cpp index da91de52..2e0bb9b8 100644 --- a/vulkan/slang_shader.cpp +++ b/vulkan/slang_shader.cpp @@ -181,10 +181,9 @@ bool SlangShader::load_file(string new_filename) return true; } -static void Initializeglslang() +void SlangShader::initialize_glslang() { static bool ProcessInitialized = false; - if (!ProcessInitialized) { glslang::InitializeProcess(); @@ -194,7 +193,7 @@ static void Initializeglslang() std::vector SlangShader::generate_spirv(std::string shader_string, std::string stage) { - Initializeglslang(); + initialize_glslang(); const EShMessages messages = (EShMessages)(EShMsgDefault | EShMsgVulkanRules | EShMsgSpvRules); string debug; auto forbid_includer = glslang::TShader::ForbidIncluder(); @@ -228,7 +227,6 @@ std::vector SlangShader::generate_spirv(std::string shader_string, std } return spirv; - } /* @@ -237,38 +235,12 @@ std::vector SlangShader::generate_spirv(std::string shader_string, std */ bool SlangShader::generate_spirv() { - Initializeglslang(); - - const EShMessages messages = (EShMessages)(EShMsgDefault | EShMsgVulkanRules | EShMsgSpvRules | EShMsgDebugInfo | EShMsgAST | EShMsgEnhanced); - auto forbid_includer = glslang::TShader::ForbidIncluder(); - - glslang::TShader vertexTShader(EShLangVertex); - glslang::TShader fragmentTShader(EShLangFragment); - - auto compile = [&](glslang::TShader &shader, string &shader_string, std::vector &spirv) -> bool { - const char *source = shader_string.c_str(); - shader.setStrings(&source, 1); - if (!shader.parse(&glslang::DefaultTBuiltInResource, 450, false, messages, forbid_includer)) - return false; - - glslang::TProgram program; - program.addShader(&shader); - if (!program.link(messages)) - return false; - - glslang::GlslangToSpv(*program.getIntermediate(shader.getStage()), spirv); - return true; - }; - - if (!compile(vertexTShader, vertex_shader_string, vertex_shader_spirv)) - { - printf("%s\n%s\n", vertexTShader.getInfoLog(), vertexTShader.getInfoDebugLog()); + vertex_shader_spirv = generate_spirv(vertex_shader_string, "vertex"); + if (vertex_shader_spirv.empty()) return false; - } - if (!compile(fragmentTShader, fragment_shader_string, fragment_shader_spirv)) - { - printf("%s\n%s\n", fragmentTShader.getInfoLog(), fragmentTShader.getInfoDebugLog()); + fragment_shader_spirv = generate_spirv(fragment_shader_string, "fragment"); + if (fragment_shader_spirv.empty()) return false; - } + return true; } \ No newline at end of file diff --git a/vulkan/slang_shader.hpp b/vulkan/slang_shader.hpp index abfa0fc0..8af9f48d 100644 --- a/vulkan/slang_shader.hpp +++ b/vulkan/slang_shader.hpp @@ -72,6 +72,7 @@ struct SlangShader bool load_file(std::string new_filename = ""); void divide_into_stages(const std::vector &lines); bool generate_spirv(); + static void initialize_glslang(); static std::vector generate_spirv(std::string shader_string, std::string stage); std::string filename;