From 40a458b743950fa35cfa9764e087d5bf936e9b0d Mon Sep 17 00:00:00 2001 From: Swung0x48 Date: Sun, 8 Jun 2025 20:19:06 +0800 Subject: [PATCH] [Fix]: save order of uniform names, and apply it at linking and buffer mapping --- MG/MG_GL/Implementations/EGL/Diligent/EGL_impl.cpp | 4 ++-- MG/MG_GL/Implementations/EGL/Diligent/EGL_impl.h | 6 +++++- MG/MG_GL/Implementations/GL/Drawing/GL_Drawing.cpp | 3 ++- MG/MG_GL/Implementations/GL/Program/GL_Program.cpp | 5 +++-- MG/MG_UTIL/Program/GLSLTool.cpp | 6 +++++- MG/MG_UTIL/Program/GLSLTool.h | 2 +- 6 files changed, 18 insertions(+), 8 deletions(-) diff --git a/MG/MG_GL/Implementations/EGL/Diligent/EGL_impl.cpp b/MG/MG_GL/Implementations/EGL/Diligent/EGL_impl.cpp index d00b6e4a..cc6f1900 100644 --- a/MG/MG_GL/Implementations/EGL/Diligent/EGL_impl.cpp +++ b/MG/MG_GL/Implementations/EGL/Diligent/EGL_impl.cpp @@ -286,7 +286,7 @@ namespace MG_Diligent { void PipelineStateManager::ConfigureResourceLayout( Diligent::GraphicsPipelineStateCreateInfo& PSOCreateInfo, - const GLProgramInfo& programInfo) + GLProgramInfo& programInfo) { MG_Util::Debug::LogD("Begin configuring resource layout for pipeline"); @@ -308,7 +308,7 @@ namespace MG_Diligent { } } - MG_Util::Program::GenerateDefaultUBOForGLSL_Multi(shaderSourcesMap); + MG_Util::Program::GenerateDefaultUBOForGLSL_Multi(shaderSourcesMap, programInfo.uniformBufferNames); for (auto shader : programInfo.AttachedShaders) { GLuint shaderId = 0; diff --git a/MG/MG_GL/Implementations/EGL/Diligent/EGL_impl.h b/MG/MG_GL/Implementations/EGL/Diligent/EGL_impl.h index 1abcf904..94d80752 100644 --- a/MG/MG_GL/Implementations/EGL/Diligent/EGL_impl.h +++ b/MG/MG_GL/Implementations/EGL/Diligent/EGL_impl.h @@ -43,6 +43,10 @@ namespace MG_Diligent { std::vector inputLayout; GLuint DefaultFBO = 0; ProgramObject programObj; + + // Uniform names, in the order of their index in uniform buffer + std::vector uniformBufferNames; + Diligent::IBuffer* pDefaultUBO = nullptr; std::unordered_map uniformStages; @@ -239,7 +243,7 @@ namespace MG_Diligent { void ConfigureResourceLayout( Diligent::GraphicsPipelineStateCreateInfo& PSOCreateInfo, - const GLProgramInfo& programInfo); + GLProgramInfo& programInfo); }; extern PipelineStateManager g_PSOManager; diff --git a/MG/MG_GL/Implementations/GL/Drawing/GL_Drawing.cpp b/MG/MG_GL/Implementations/GL/Drawing/GL_Drawing.cpp index 85a3c704..6b33a4fe 100644 --- a/MG/MG_GL/Implementations/GL/Drawing/GL_Drawing.cpp +++ b/MG/MG_GL/Implementations/GL/Drawing/GL_Drawing.cpp @@ -232,7 +232,8 @@ namespace MG_GL::GL { MG_Util::Debug::LogD("UBO mapped successfully for program %u. Updating uniform values.", program); uint8_t* uboData = static_cast(mapped); - for (auto& [name, uniform] : programObj.uniformValues) { + for (auto& name: programInfo.uniformBufferNames) { + auto& uniform = programObj.uniformValues[name]; if (IsSamplerType(uniform.type)) continue; auto it = programInfo.uniformOffsets.find(name); diff --git a/MG/MG_GL/Implementations/GL/Program/GL_Program.cpp b/MG/MG_GL/Implementations/GL/Program/GL_Program.cpp index f7bd197e..5d938990 100644 --- a/MG/MG_GL/Implementations/GL/Program/GL_Program.cpp +++ b/MG/MG_GL/Implementations/GL/Program/GL_Program.cpp @@ -86,7 +86,8 @@ namespace MG_GL::GL { size_t offset = 0; programInfo.uniformOffsets.clear(); - for (auto& [name, uniform] : programInfo.programObj.uniformValues) { + for (auto& name: programInfo.uniformBufferNames) { + auto& uniform = programInfo.programObj.uniformValues[name]; if (IsSamplerType(uniform.type)) continue; size_t size = GetUniformSize(uniform.type); @@ -255,7 +256,7 @@ namespace MG_GL::GL { shaderSources[shaderId] = shaderObj.source; } - MG_Util::Program::GenerateDefaultUBOForGLSL_Multi(shaderSources); + MG_Util::Program::GenerateDefaultUBOForGLSL_Multi(shaderSources, programInfo.uniformBufferNames); // Compile attached shaders for (GLuint shaderId : programInfo.AttachedShadersID) { diff --git a/MG/MG_UTIL/Program/GLSLTool.cpp b/MG/MG_UTIL/Program/GLSLTool.cpp index f89d2319..d7784c4e 100644 --- a/MG/MG_UTIL/Program/GLSLTool.cpp +++ b/MG/MG_UTIL/Program/GLSLTool.cpp @@ -324,9 +324,11 @@ namespace MG_Util::Program { } void GenerateDefaultUBOForGLSL_Multi( - MG_Global::unordered_map &shaderSources) { + MG_Global::unordered_map &shaderSources, std::vector& outUniformBufferNames) { if (shaderSources.empty()) return; + outUniformBufferNames.clear(); + std::unordered_map> commentMasks; for (auto const& [id, source] : shaderSources) { commentMasks[id] = buildCommentMask(source); @@ -405,6 +407,8 @@ namespace MG_Util::Program { if (allUniformNames.find(varName) == allUniformNames.end()) { mergedUniforms.push_back(declaration); allUniformNames.insert(varName); + // save the uniform names in order + outUniformBufferNames.emplace_back(varName); } size_t removeStart = declStart; diff --git a/MG/MG_UTIL/Program/GLSLTool.h b/MG/MG_UTIL/Program/GLSLTool.h index 4e9156d3..fd9d0313 100644 --- a/MG/MG_UTIL/Program/GLSLTool.h +++ b/MG/MG_UTIL/Program/GLSLTool.h @@ -181,7 +181,7 @@ namespace MG_Util::Program { std::pair GenerateDefaultUBOForGLSL(const std::pair& glslSources); void GenerateDefaultUBOForGLSL_Multi( - MG_Global::unordered_map &shaderSources); + MG_Global::unordered_map &shaderSources, std::vector& outUniformBufferNames); } #endif //MOBILEGL_GLSLTOOL_H