slang: Fix race condition to initialize glslang.

This commit is contained in:
BearOso 2023-02-22 16:21:00 -06:00
parent 1907b9e25b
commit d8584b9d94
3 changed files with 10 additions and 36 deletions

View File

@ -107,10 +107,11 @@ bool SlangPreset::load_preset_file(string filename)
}
}
SlangShader::initialize_glslang();
std::vector<std::future<bool>> 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();
}));
}

View File

@ -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<uint32_t> 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<uint32_t> SlangShader::generate_spirv(std::string shader_string, std
}
return spirv;
}
/*
@ -237,38 +235,12 @@ std::vector<uint32_t> 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<uint32_t> &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;
}

View File

@ -72,6 +72,7 @@ struct SlangShader
bool load_file(std::string new_filename = "");
void divide_into_stages(const std::vector<std::string> &lines);
bool generate_spirv();
static void initialize_glslang();
static std::vector<uint32_t> generate_spirv(std::string shader_string, std::string stage);
std::string filename;