[Feat] (MG_State/Program): refractor UBO handling

This commit is contained in:
2025-11-05 13:51:05 +08:00
parent 57dd0fe480
commit 62798ce1b8
2 changed files with 14 additions and 14 deletions
@@ -203,13 +203,11 @@ namespace MobileGL {
// ---------- UBO ---------- // ---------- UBO ----------
int uboCount = m_program->getNumUniformBlocks(); int uboCount = m_program->getNumUniformBlocks();
m_uniformBlockIndexInTProgram.resize(uboCount, -1); m_uniformBlockBinding.resize(uboCount, -1);
m_uniformBlockBinding.resize(uboCount, 0);
for (int i = 0; i < uboCount; i++) { for (int i = 0; i < uboCount; i++) {
auto& ubo = m_program->getUniformBlock(i); auto& ubo = m_program->getUniformBlock(i);
m_uniformBlockNameMaxLength = std::max(m_uniformBlockNameMaxLength, (Int)ubo.name.length()); m_uniformBlockNameMaxLength = std::max(m_uniformBlockNameMaxLength, (Int)ubo.name.length());
m_uniformBlockIndex[ubo.name] = ubo.getBinding(); m_uniformBlockIndexByName[ubo.name] = i;
m_uniformBlockIndexInTProgram[ubo.getBinding()] = i;
} }
} }
@@ -76,30 +76,29 @@ namespace MobileGL {
Int GetActiveAttributesMaxLength() const { return m_attribInNameMaxLength; } Int GetActiveAttributesMaxLength() const { return m_attribInNameMaxLength; }
Int GetActiveUniformBlocksMaxNameLength() const { return m_uniformBlockNameMaxLength; } Int GetActiveUniformBlocksMaxNameLength() const { return m_uniformBlockNameMaxLength; }
Uint GetUniformBlockIndex(const char* name) const { Uint GetUniformBlockIndex(const char* name) const {
auto it = m_uniformBlockIndex.find(name); auto it = m_uniformBlockIndexByName.find(name);
if (it != m_uniformBlockIndex.end()) if (it != m_uniformBlockIndexByName.end())
return it->second; return it->second;
return 0xFFFFFFFFu; // GL_INVALID_INDEX return 0xFFFFFFFFu; // GL_INVALID_INDEX
} }
Bool IsActiveUniformBlock(Uint index) const { Bool IsActiveUniformBlock(Uint index) const {
if (index >= GetActiveUniformBlocksCount()) if (index >= GetActiveUniformBlocksCount())
return false; return false;
if (m_uniformBlockIndexInTProgram[index] == -1)
return false;
return true; return true;
} }
Uint GetUBOSizeAt(Uint index) const { Uint GetUBOSizeAt(Uint index) const {
if (!IsActiveUniformBlock(index)) if (!IsActiveUniformBlock(index))
return 0; return 0;
return m_program->getUniformBlock(m_uniformBlockIndexInTProgram[index]).size; return m_program->getUniformBlock(index).size;
} }
const String& GetUniformBlockName(Uint index) const { const String& GetUniformBlockName(Uint index) const {
auto& ubo = m_program->getUniform(m_uniformBlockIndexInTProgram[index]); auto& ubo = m_program->getUniform(index);
return ubo.name; return ubo.name;
} }
// Set by glUniformBlockBinding
void SetUniformBlockBinding(Uint index, Uint binding) { void SetUniformBlockBinding(Uint index, Uint binding) {
m_uniformBlockBinding[index] = binding; m_uniformBlockBinding[index] = binding;
} }
@@ -137,10 +136,13 @@ namespace MobileGL {
// ditto. Will be set at glUniform1i // ditto. Will be set at glUniform1i
Vector<Int> m_uniformSamplerOrImageUnitIndex; Vector<Int> m_uniformSamplerOrImageUnitIndex;
UnorderedMap<String, Uint> m_uniformBlockIndex; // Ordered by uniform block index
// index is DIFFERENT from binding!!!
// Ordered by block index //
Vector<Int> m_uniformBlockIndexInTProgram; // Let's define UniformBlockIndex == the order at glslang getUniformBlock()
// aka `i = glGetUniformBlockIndex(prog, "BlockName")` implies:
// `prog->getUniformBlock(i) == "BlockName"`
UnorderedMap<String, Uint> m_uniformBlockIndexByName;
Vector<Int> m_uniformBlockBinding; Vector<Int> m_uniformBlockBinding;
// Need to be reflected after linking of SPIR-V binary // Need to be reflected after linking of SPIR-V binary