From 067d5175279095acd2b430adcc324b678029c79c Mon Sep 17 00:00:00 2001 From: Swung0x48 Date: Mon, 30 Jun 2025 15:32:59 +0800 Subject: [PATCH] [Fix] (MG_Util/GLSLTool): get rid of unnecessary whitespace removal --- .../Implementations/GL/Program/GL_Program.cpp | 336 +++++++++--------- MG/MG_UTIL/Program/GLSLTool.cpp | 5 - 2 files changed, 168 insertions(+), 173 deletions(-) diff --git a/MG/MG_GL/Implementations/GL/Program/GL_Program.cpp b/MG/MG_GL/Implementations/GL/Program/GL_Program.cpp index abc9a9f7..f50aee9c 100644 --- a/MG/MG_GL/Implementations/GL/Program/GL_Program.cpp +++ b/MG/MG_GL/Implementations/GL/Program/GL_Program.cpp @@ -281,176 +281,176 @@ namespace MG_GL::GL { void LinkProgram(GLuint program) { MG_Util::Debug::LogD("glLinkProgram, program: %u", program); GLenum result = MG_State::FinalizeProgramPipeline(program); - if (result == GL_NO_ERROR) { - auto& programInfo = MG_Diligent::g_ProgramMap[program]; - auto& programObj = MG_State_T::programState->programs_[program]; - - programInfo.id = program; - - for (const auto& shaderId : programObj.attachedShaders) { - programInfo.AttachedShadersID.push_back(shaderId); - } - - MG_Global::unordered_map shaderSources; - MG_Global::unordered_map finalShaderSources; - for (GLuint shaderId : programObj.attachedShaders) { - auto it = MG_State_T::programState->shaders_.find(shaderId); - if (it != MG_State_T::programState->shaders_.end() && !it->second.markedForDeletion) { - shaderSources[it->first] = it->second.source; -// MG_Util::Program::RenameGLSLBuiltinsForVulkan(shaderSources[it->first]); - } - } - - MG_Util::Program::GenerateDefaultUBOForGLSL_Multi(shaderSources, programInfo.uniformBufferNames); - - for (GLuint shaderId : programObj.attachedShaders) { - auto it = MG_State_T::programState->shaders_.find(shaderId); - if (it != MG_State_T::programState->shaders_.end() && !it->second.markedForDeletion) { - std::string shaderSource; - if (it->second.type == GL_VERTEX_SHADER) { - std::string infoLog; - - const char* fragmentShaderSource = nullptr; - for (GLuint otherShaderId : programObj.attachedShaders) { - if (otherShaderId != shaderId && MG_State_T::programState->shaders_[otherShaderId].type == GL_FRAGMENT_SHADER) { - fragmentShaderSource = shaderSources[otherShaderId].c_str(); - break; - } - } - auto spirv = MG_Util::Program::CompileGLSLToSPIRV(it->second.type, - shaderSources[shaderId], - infoLog, true, GL_FRAGMENT_SHADER, fragmentShaderSource); - if (spirv.empty()) { - MG_Util::Debug::LogE("Failed to compile vertex shader %u: %s", shaderId, - infoLog.c_str()); - continue; - } - - shaderSource = MG_Util::Program::BindInputLayoutLocationsForGLSL(spirv, - programObj.attribLocations); - } else if (it->second.type == GL_FRAGMENT_SHADER) { - std::string infoLog; - - const char* vertexShaderSource = nullptr; - for (GLuint otherShaderId : programObj.attachedShaders) { - if (otherShaderId != shaderId && MG_State_T::programState->shaders_[otherShaderId].type == GL_VERTEX_SHADER) { - vertexShaderSource = shaderSources[otherShaderId].c_str(); - break; - } - } - - auto spirv = MG_Util::Program::CompileGLSLToSPIRV(it->second.type, - shaderSources[shaderId], - infoLog, true, - GL_VERTEX_SHADER, vertexShaderSource); - if (spirv.empty()) { - MG_Util::Debug::LogE("Failed to compile fragment shader %u to SPIRV: %s", shaderId, - infoLog.c_str()); - continue; - } - - shaderSource = MG_Util::Program::CompileSPIRVToGLSL(spirv, 450, false, true); - } else { - shaderSource = it->second.source; - } - finalShaderSources[it->first] = shaderSource; - } - } - - - MG_Util::Debug::LogD("Shader sources after UBO generation for program %u:", program); - for (const auto& [shaderId, source] : finalShaderSources) { - MG_Util::Debug::LogD(" Program %u Shader %u:\n%s", program, shaderId, source.c_str()); - } - - size_t uboTotalSize = RecordUniformOffsets(programInfo, finalShaderSources); - CreateDefaultUBO(programInfo, uboTotalSize); - - // Compile attached shaders - for (GLuint shaderId : programInfo.AttachedShadersID) { - auto& shaderObj = MG_State_T::programState->shaders_[shaderId]; - // Compile only if not already compiled in Diligent - if (MG_Diligent::g_ShaderMap.find(shaderId) == MG_Diligent::g_ShaderMap.end() || MG_Diligent::g_ShaderMap[shaderId] == nullptr) { - GLenum shaderType = shaderObj.type; - std::string sourceStr = finalShaderSources[shaderId]; - MG_Util::Debug::LogD("Shader ID: %u, type: %s\nConverted source:\n%s", - shaderId, MG_Util::Debug::GLEnumToString(shaderType), sourceStr.c_str()); - - Diligent::ShaderCreateInfo ShaderCI; - ShaderCI.Source = sourceStr.c_str(); - ShaderCI.EntryPoint = "main"; - - switch (shaderType) { - case GL_VERTEX_SHADER: ShaderCI.Desc.ShaderType = Diligent::SHADER_TYPE_VERTEX; break; - case GL_FRAGMENT_SHADER: ShaderCI.Desc.ShaderType = Diligent::SHADER_TYPE_PIXEL; break; - case GL_GEOMETRY_SHADER: ShaderCI.Desc.ShaderType = Diligent::SHADER_TYPE_GEOMETRY; break; - case GL_TESS_CONTROL_SHADER: ShaderCI.Desc.ShaderType = Diligent::SHADER_TYPE_HULL; break; - case GL_TESS_EVALUATION_SHADER: ShaderCI.Desc.ShaderType = Diligent::SHADER_TYPE_DOMAIN; break; - case GL_COMPUTE_SHADER: ShaderCI.Desc.ShaderType = Diligent::SHADER_TYPE_COMPUTE; break; - default: MG_Util::Debug::LogW("Unsupported shader type for compilation: %u", shaderType); continue; - } - - ShaderCI.Desc.Name = ("Shader_" + std::to_string(shaderId)).c_str(); - ShaderCI.SourceLanguage = Diligent::SHADER_SOURCE_LANGUAGE_GLSL_VERBATIM; - - Diligent::IShader* pShader = nullptr; - Diligent::RefCntAutoPtr pMsg; - MG_Diligent::g_pDevice->CreateShader(ShaderCI, &pShader, &pMsg); - - if (pShader) { - MG_Diligent::g_ShaderMap[shaderId] = pShader; - programInfo.AttachedShaders.push_back(pShader); - shaderObj.compiled = UncertainBool::True; - shaderObj.compileStatus = GL_TRUE; - MG_Util::Debug::LogD("Successfully compiled shader ID: %u for program %u", shaderId, program); - } else { - shaderObj.compiled = UncertainBool::False; - shaderObj.compileStatus = GL_FALSE; - MG_Util::Debug::LogE("Failed to compile shader ID: %u for program %u. \ncompiler info:\n%s", shaderId, program, pMsg->GetConstDataPtr()); - } - } else { - // Shader already compiled, just add to programInfo - programInfo.AttachedShaders.push_back(MG_Diligent::g_ShaderMap[shaderId]); - } - } - - programInfo.inputLayout.clear(); - - auto* pVAO = MG_State_T::vertexArrayState->GetCurrentVAO(); - if (!pVAO) return; - -// for (const auto& [index, attrib] : pVAO->attribs) { - for (uint32_t index = 0; index < MG_Constants::VertexArray::MAX_VERTEX_ATTRIBS; ++index) { - const auto& attrib = pVAO->attribs[index]; - if (attrib.enabled) { - Diligent::LayoutElement elem; - elem.InputIndex = index; - elem.BufferSlot = 0; - elem.NumComponents = attrib.size; - elem.ValueType = ConvertGLTypeToDiligent(attrib.type); - elem.IsNormalized = attrib.normalized; - elem.RelativeOffset = static_cast(reinterpret_cast(attrib.pointer)); - - programInfo.inputLayout.push_back(elem); - } - } - - programInfo.psoDirty = true; - programInfo.psoStateHash = 0; - programInfo.uniformStages.clear(); - programInfo.programObj = MG_State_T::programState->programs_[program]; - - if (programInfo.pResourceBinding) { - programInfo.pResourceBinding->Release(); - programInfo.pResourceBinding = nullptr; - } - - programObj.linked = true; - programObj.linkStatus = GL_TRUE; + if (result != GL_NO_ERROR) { + MG_State::SetError(result); + MG_Util::Debug::LogE("Error linking program: %s", MG_Util::Debug::GLEnumToString(result)); return; } - MG_State::SetError(result); - MG_Util::Debug::LogE("Error linking program: %s", MG_Util::Debug::GLEnumToString(result)); + auto& programInfo = MG_Diligent::g_ProgramMap[program]; + auto& programObj = MG_State_T::programState->programs_[program]; + + programInfo.id = program; + + for (const auto& shaderId : programObj.attachedShaders) { + programInfo.AttachedShadersID.push_back(shaderId); + } + + MG_Global::unordered_map shaderSources; + MG_Global::unordered_map finalShaderSources; + for (GLuint shaderId : programObj.attachedShaders) { + auto it = MG_State_T::programState->shaders_.find(shaderId); + if (it != MG_State_T::programState->shaders_.end() && !it->second.markedForDeletion) { + shaderSources[it->first] = it->second.source; +// MG_Util::Program::RenameGLSLBuiltinsForVulkan(shaderSources[it->first]); + } + } + + MG_Util::Program::GenerateDefaultUBOForGLSL_Multi(shaderSources, programInfo.uniformBufferNames); + + for (GLuint shaderId : programObj.attachedShaders) { + auto it = MG_State_T::programState->shaders_.find(shaderId); + if (it != MG_State_T::programState->shaders_.end() && !it->second.markedForDeletion) { + std::string shaderSource; + if (it->second.type == GL_VERTEX_SHADER) { + std::string infoLog; + + const char* fragmentShaderSource = nullptr; + for (GLuint otherShaderId : programObj.attachedShaders) { + if (otherShaderId != shaderId && MG_State_T::programState->shaders_[otherShaderId].type == GL_FRAGMENT_SHADER) { + fragmentShaderSource = shaderSources[otherShaderId].c_str(); + break; + } + } + auto spirv = MG_Util::Program::CompileGLSLToSPIRV(it->second.type, + shaderSources[shaderId], + infoLog, true, GL_FRAGMENT_SHADER, fragmentShaderSource); + if (spirv.empty()) { + MG_Util::Debug::LogE("Failed to compile vertex shader %u: %s", shaderId, + infoLog.c_str()); + continue; + } + + shaderSource = MG_Util::Program::BindInputLayoutLocationsForGLSL(spirv, + programObj.attribLocations); + } else if (it->second.type == GL_FRAGMENT_SHADER) { + std::string infoLog; + + const char* vertexShaderSource = nullptr; + for (GLuint otherShaderId : programObj.attachedShaders) { + if (otherShaderId != shaderId && MG_State_T::programState->shaders_[otherShaderId].type == GL_VERTEX_SHADER) { + vertexShaderSource = shaderSources[otherShaderId].c_str(); + break; + } + } + + auto spirv = MG_Util::Program::CompileGLSLToSPIRV(it->second.type, + shaderSources[shaderId], + infoLog, true, + GL_VERTEX_SHADER, vertexShaderSource); + if (spirv.empty()) { + MG_Util::Debug::LogE("Failed to compile fragment shader %u to SPIRV: %s", shaderId, + infoLog.c_str()); + continue; + } + + shaderSource = MG_Util::Program::CompileSPIRVToGLSL(spirv, 450, false, true); + } else { + shaderSource = it->second.source; + } + finalShaderSources[it->first] = shaderSource; + } + } + + + MG_Util::Debug::LogD("Shader sources after UBO generation for program %u:", program); + for (const auto& [shaderId, source] : finalShaderSources) { + MG_Util::Debug::LogD(" Program %u Shader %u:\n%s", program, shaderId, source.c_str()); + } + + size_t uboTotalSize = RecordUniformOffsets(programInfo, finalShaderSources); + CreateDefaultUBO(programInfo, uboTotalSize); + + // Compile attached shaders + for (GLuint shaderId : programInfo.AttachedShadersID) { + auto& shaderObj = MG_State_T::programState->shaders_[shaderId]; + // Compile only if not already compiled in Diligent + if (MG_Diligent::g_ShaderMap.find(shaderId) == MG_Diligent::g_ShaderMap.end() || MG_Diligent::g_ShaderMap[shaderId] == nullptr) { + GLenum shaderType = shaderObj.type; + std::string sourceStr = finalShaderSources[shaderId]; + MG_Util::Debug::LogD("Shader ID: %u, type: %s\nConverted source:\n%s", + shaderId, MG_Util::Debug::GLEnumToString(shaderType), sourceStr.c_str()); + + Diligent::ShaderCreateInfo ShaderCI; + ShaderCI.Source = sourceStr.c_str(); + ShaderCI.EntryPoint = "main"; + + switch (shaderType) { + case GL_VERTEX_SHADER: ShaderCI.Desc.ShaderType = Diligent::SHADER_TYPE_VERTEX; break; + case GL_FRAGMENT_SHADER: ShaderCI.Desc.ShaderType = Diligent::SHADER_TYPE_PIXEL; break; + case GL_GEOMETRY_SHADER: ShaderCI.Desc.ShaderType = Diligent::SHADER_TYPE_GEOMETRY; break; + case GL_TESS_CONTROL_SHADER: ShaderCI.Desc.ShaderType = Diligent::SHADER_TYPE_HULL; break; + case GL_TESS_EVALUATION_SHADER: ShaderCI.Desc.ShaderType = Diligent::SHADER_TYPE_DOMAIN; break; + case GL_COMPUTE_SHADER: ShaderCI.Desc.ShaderType = Diligent::SHADER_TYPE_COMPUTE; break; + default: MG_Util::Debug::LogW("Unsupported shader type for compilation: %u", shaderType); continue; + } + + ShaderCI.Desc.Name = ("Shader_" + std::to_string(shaderId)).c_str(); + ShaderCI.SourceLanguage = Diligent::SHADER_SOURCE_LANGUAGE_GLSL_VERBATIM; + + Diligent::IShader* pShader = nullptr; + Diligent::RefCntAutoPtr pMsg; + MG_Diligent::g_pDevice->CreateShader(ShaderCI, &pShader, &pMsg); + + if (pShader) { + MG_Diligent::g_ShaderMap[shaderId] = pShader; + programInfo.AttachedShaders.push_back(pShader); + shaderObj.compiled = UncertainBool::True; + shaderObj.compileStatus = GL_TRUE; + MG_Util::Debug::LogD("Successfully compiled shader ID: %u for program %u", shaderId, program); + } else { + shaderObj.compiled = UncertainBool::False; + shaderObj.compileStatus = GL_FALSE; + MG_Util::Debug::LogE("Failed to compile shader ID: %u for program %u. \ncompiler info:\n%s", shaderId, program, pMsg->GetConstDataPtr()); + } + } else { + // Shader already compiled, just add to programInfo + programInfo.AttachedShaders.push_back(MG_Diligent::g_ShaderMap[shaderId]); + } + } + + programInfo.inputLayout.clear(); + + auto* pVAO = MG_State_T::vertexArrayState->GetCurrentVAO(); + if (!pVAO) return; + +// for (const auto& [index, attrib] : pVAO->attribs) { + for (uint32_t index = 0; index < MG_Constants::VertexArray::MAX_VERTEX_ATTRIBS; ++index) { + const auto& attrib = pVAO->attribs[index]; + if (attrib.enabled) { + Diligent::LayoutElement elem; + elem.InputIndex = index; + elem.BufferSlot = 0; + elem.NumComponents = attrib.size; + elem.ValueType = ConvertGLTypeToDiligent(attrib.type); + elem.IsNormalized = attrib.normalized; + elem.RelativeOffset = static_cast(reinterpret_cast(attrib.pointer)); + + programInfo.inputLayout.push_back(elem); + } + } + + programInfo.psoDirty = true; + programInfo.psoStateHash = 0; + programInfo.uniformStages.clear(); + programInfo.programObj = MG_State_T::programState->programs_[program]; + + if (programInfo.pResourceBinding) { + programInfo.pResourceBinding->Release(); + programInfo.pResourceBinding = nullptr; + } + + programObj.linked = true; + programObj.linkStatus = GL_TRUE; } void UseProgram(GLuint program) { diff --git a/MG/MG_UTIL/Program/GLSLTool.cpp b/MG/MG_UTIL/Program/GLSLTool.cpp index 5505e107..1643400e 100644 --- a/MG/MG_UTIL/Program/GLSLTool.cpp +++ b/MG/MG_UTIL/Program/GLSLTool.cpp @@ -558,11 +558,6 @@ namespace MG_Util::Program { for (auto& span : removalSpans) { source.erase(span.first, span.second); } - source = std::regex_replace( - source, - std::regex(R"((\r\n|\n|\r){2,})"), - "$1" - ); } std::string uboBlock = "\nlayout(std140) uniform MG_DEFAULT_UBO\n{\n";