[Feat]: (MG_Impl/Program, MG_State/Program): implement UBO

This commit is contained in:
2025-11-01 23:06:25 +08:00
parent 4706dea9de
commit 278656ac5a
5 changed files with 164 additions and 4 deletions
@@ -199,10 +199,15 @@ namespace MobileGL {
// ---------- UBO ----------
int uboCount = m_program->getNumUniformBlocks();
m_uniformBlockIndexInTProgram.resize(uboCount, -1);
m_uniformBlockBinding.resize(uboCount, 0);
for (int i = 0; i < uboCount; i++) {
auto& ubo = m_program->getUniformBlock(i);
m_uniformBlockNameMaxLength = std::max(m_uniformBlockNameMaxLength, (Int)ubo.name.length());
m_uniformBlockIndex[ubo.name] = ubo.getBinding();
m_uniformBlockIndexInTProgram[ubo.getBinding()] = i;
}
}
void ProgramObject::GenerateBinary() {
@@ -75,6 +75,34 @@ namespace MobileGL {
Int GetActiveUniformBlocksCount() const { return m_program->getNumUniformBlocks(); }
Int GetActiveAttributesMaxLength() const { return m_attribInNameMaxLength; }
Int GetActiveUniformBlocksMaxNameLength() const { return m_uniformBlockNameMaxLength; }
Uint GetUniformBlockIndex(const char* name) const {
auto it = m_uniformBlockIndex.find(name);
if (it != m_uniformBlockIndex.end())
return it->second;
return 0xFFFFFFFFu; // GL_INVALID_INDEX
}
Bool IsActiveUniformBlock(Uint index) const {
if (index >= GetActiveUniformBlocksCount())
return false;
if (m_uniformBlockIndexInTProgram[index] == -1)
return false;
return true;
}
Uint GetUBOSizeAt(Uint index) const {
if (!IsActiveUniformBlock(index))
return 0;
return m_program->getUniformBlock(m_uniformBlockIndexInTProgram[index]).size;
}
const String& GetUniformBlockName(Uint index) const {
auto& ubo = m_program->getUniform(m_uniformBlockIndexInTProgram[index]);
return ubo.name;
}
void SetUniformBlockBinding(Uint index, Uint binding) {
m_uniformBlockBinding[index] = binding;
}
Vector<Vector<unsigned>>& GetGeneratedSpirv() { return m_generatedSpirv; }
@@ -105,6 +133,12 @@ namespace MobileGL {
// ditto. Will be set at glUniform1i
Vector<Int> m_uniformSamplerOrImageUnitIndex;
UnorderedMap<String, Uint> m_uniformBlockIndex;
// Ordered by block index
Vector<Int> m_uniformBlockIndexInTProgram;
Vector<Int> m_uniformBlockBinding;
// Need to be reflected after linking of SPIR-V binary
Vector<Uint> m_uniformOffsets;
Vector<Uint> m_uniformSizesInBytes;