[Fix]: save order of uniform names, and apply it at linking and buffer mapping

This commit is contained in:
2025-06-08 20:19:06 +08:00
parent c183203264
commit 40a458b743
6 changed files with 18 additions and 8 deletions
@@ -286,7 +286,7 @@ namespace MG_Diligent {
void PipelineStateManager::ConfigureResourceLayout( void PipelineStateManager::ConfigureResourceLayout(
Diligent::GraphicsPipelineStateCreateInfo& PSOCreateInfo, Diligent::GraphicsPipelineStateCreateInfo& PSOCreateInfo,
const GLProgramInfo& programInfo) GLProgramInfo& programInfo)
{ {
MG_Util::Debug::LogD("Begin configuring resource layout for pipeline"); 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) { for (auto shader : programInfo.AttachedShaders) {
GLuint shaderId = 0; GLuint shaderId = 0;
@@ -43,6 +43,10 @@ namespace MG_Diligent {
std::vector<Diligent::LayoutElement> inputLayout; std::vector<Diligent::LayoutElement> inputLayout;
GLuint DefaultFBO = 0; GLuint DefaultFBO = 0;
ProgramObject programObj; ProgramObject programObj;
// Uniform names, in the order of their index in uniform buffer
std::vector<std::string> uniformBufferNames;
Diligent::IBuffer* pDefaultUBO = nullptr; Diligent::IBuffer* pDefaultUBO = nullptr;
std::unordered_map<std::string, Diligent::SHADER_TYPE> uniformStages; std::unordered_map<std::string, Diligent::SHADER_TYPE> uniformStages;
@@ -239,7 +243,7 @@ namespace MG_Diligent {
void ConfigureResourceLayout( void ConfigureResourceLayout(
Diligent::GraphicsPipelineStateCreateInfo& PSOCreateInfo, Diligent::GraphicsPipelineStateCreateInfo& PSOCreateInfo,
const GLProgramInfo& programInfo); GLProgramInfo& programInfo);
}; };
extern PipelineStateManager g_PSOManager; extern PipelineStateManager g_PSOManager;
@@ -232,7 +232,8 @@ namespace MG_GL::GL {
MG_Util::Debug::LogD("UBO mapped successfully for program %u. Updating uniform values.", program); MG_Util::Debug::LogD("UBO mapped successfully for program %u. Updating uniform values.", program);
uint8_t* uboData = static_cast<uint8_t*>(mapped); uint8_t* uboData = static_cast<uint8_t*>(mapped);
for (auto& [name, uniform] : programObj.uniformValues) { for (auto& name: programInfo.uniformBufferNames) {
auto& uniform = programObj.uniformValues[name];
if (IsSamplerType(uniform.type)) continue; if (IsSamplerType(uniform.type)) continue;
auto it = programInfo.uniformOffsets.find(name); auto it = programInfo.uniformOffsets.find(name);
@@ -86,7 +86,8 @@ namespace MG_GL::GL {
size_t offset = 0; size_t offset = 0;
programInfo.uniformOffsets.clear(); 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; if (IsSamplerType(uniform.type)) continue;
size_t size = GetUniformSize(uniform.type); size_t size = GetUniformSize(uniform.type);
@@ -255,7 +256,7 @@ namespace MG_GL::GL {
shaderSources[shaderId] = shaderObj.source; shaderSources[shaderId] = shaderObj.source;
} }
MG_Util::Program::GenerateDefaultUBOForGLSL_Multi(shaderSources); MG_Util::Program::GenerateDefaultUBOForGLSL_Multi(shaderSources, programInfo.uniformBufferNames);
// Compile attached shaders // Compile attached shaders
for (GLuint shaderId : programInfo.AttachedShadersID) { for (GLuint shaderId : programInfo.AttachedShadersID) {
+5 -1
View File
@@ -324,9 +324,11 @@ namespace MG_Util::Program {
} }
void GenerateDefaultUBOForGLSL_Multi( void GenerateDefaultUBOForGLSL_Multi(
MG_Global::unordered_map<GLuint, std::string> &shaderSources) { MG_Global::unordered_map<GLuint, std::string> &shaderSources, std::vector<std::string>& outUniformBufferNames) {
if (shaderSources.empty()) return; if (shaderSources.empty()) return;
outUniformBufferNames.clear();
std::unordered_map<GLuint, std::vector<bool>> commentMasks; std::unordered_map<GLuint, std::vector<bool>> commentMasks;
for (auto const& [id, source] : shaderSources) { for (auto const& [id, source] : shaderSources) {
commentMasks[id] = buildCommentMask(source); commentMasks[id] = buildCommentMask(source);
@@ -405,6 +407,8 @@ namespace MG_Util::Program {
if (allUniformNames.find(varName) == allUniformNames.end()) { if (allUniformNames.find(varName) == allUniformNames.end()) {
mergedUniforms.push_back(declaration); mergedUniforms.push_back(declaration);
allUniformNames.insert(varName); allUniformNames.insert(varName);
// save the uniform names in order
outUniformBufferNames.emplace_back(varName);
} }
size_t removeStart = declStart; size_t removeStart = declStart;
+1 -1
View File
@@ -181,7 +181,7 @@ namespace MG_Util::Program {
std::pair<std::string, std::string> GenerateDefaultUBOForGLSL(const std::pair<std::string, std::pair<std::string, std::string> GenerateDefaultUBOForGLSL(const std::pair<std::string,
std::string>& glslSources); std::string>& glslSources);
void GenerateDefaultUBOForGLSL_Multi( void GenerateDefaultUBOForGLSL_Multi(
MG_Global::unordered_map<GLuint, std::string> &shaderSources); MG_Global::unordered_map<GLuint, std::string> &shaderSources, std::vector<std::string>& outUniformBufferNames);
} }
#endif //MOBILEGL_GLSLTOOL_H #endif //MOBILEGL_GLSLTOOL_H