Merge branch 'dev' into Feat/Backend-Direct-GLES

This commit is contained in:
2025-10-27 13:42:31 +08:00
3 changed files with 51 additions and 31 deletions
+11 -4
View File
@@ -455,10 +455,17 @@ namespace MobileGL {
template <GLsizei VecCount, typename T> template <GLsizei VecCount, typename T>
void Uniform_State(MG_State::GLState::ProgramObject& programObject, GLuint location, T* value) { void Uniform_State(MG_State::GLState::ProgramObject& programObject, GLuint location, T* value) {
auto size = programObject.GetUniformSizesInBytes(location); if (!programObject.IsUniformOpaqueAtLocation(location)) {
auto offset = programObject.GetUniformOffset(location); auto size = programObject.GetUniformSizesInBytes(location);
assert(size >= VecCount * sizeof(T)); auto offset = programObject.GetUniformOffset(location);
memcpy((char*)programObject.MapUBO() + offset, value, VecCount * sizeof(T)); assert(size >= VecCount * sizeof(T));
memcpy((char*)programObject.MapUBO() + offset, value, VecCount * sizeof(T));
} else {
auto* ttype = programObject.GetUniformTType(location);
if (ttype->isTexture() || ttype->isImage()) {
programObject.SetUniformSamplerOrImageUnitIndex(location, *value);
}
}
} }
template <GLsizei VecCount, typename T> template <GLsizei VecCount, typename T>
@@ -91,11 +91,8 @@ namespace MobileGL {
// i-th elements refers to uniform at layout(location = i, ...) // i-th elements refers to uniform at layout(location = i, ...)
// Be aware, there could be gaps in between these vectors // Be aware, there could be gaps in between these vectors
// Locations can be not sequential // Locations can be not sequential
m_uniformNames.resize(m_maxUniformLocation + 1); m_uniformIndexInTProgram.resize(m_maxUniformLocation + 1, 4095);
m_uniformTypes.resize(m_maxUniformLocation + 1, GL_ZERO); m_uniformSamplerOrImageUnitIndex.resize(m_maxUniformLocation + 1, 0);
m_uniformIsOpaqueType.resize(m_maxUniformLocation + 1);
m_uniformOffsets.resize(m_maxUniformLocation + 1);
m_uniformArraySizes.resize(m_maxUniformLocation + 1);
Vector<int> unallocatedUniformIndex; Vector<int> unallocatedUniformIndex;
@@ -103,26 +100,21 @@ namespace MobileGL {
for (int i = 0; i < m_activeUniformCount; i++) { for (int i = 0; i < m_activeUniformCount; i++) {
auto& uniform = m_program->getUniform(i); auto& uniform = m_program->getUniform(i);
auto location = uniform.layoutLocation(); auto location = uniform.layoutLocation();
if (location >= m_uniformNames.size()) { if (m_uniformLocations[uniform.name] == 4095) {
unallocatedUniformIndex.emplace_back(i); unallocatedUniformIndex.emplace_back(i);
continue; // will allocate unallocated uniforms later continue; // will allocate unallocated uniforms later
} }
m_uniformNames[location] = uniform.name; m_uniformIndexInTProgram[location] = i;
m_uniformTypes[location] = uniform.glDefineType;
m_uniformIsOpaqueType[location] = uniform.getType()->isOpaque();
m_uniformArraySizes[location] = uniform.size;
} }
SizeT locNeedle = 0; SizeT locNeedle = 0;
for (auto index : unallocatedUniformIndex) { for (auto index: unallocatedUniformIndex) {
auto& uniform = m_program->getUniform(index); auto& uniform = m_program->getUniform(index);
for (; locNeedle <= m_maxUniformLocation; locNeedle++) { for (; locNeedle <= m_maxUniformLocation; locNeedle++) {
if (m_uniformTypes[locNeedle] != GL_ZERO) continue; if (m_uniformIndexInTProgram[locNeedle] != 4095)
continue;
// Found a vacant location at locNeedle // Found a vacant location at locNeedle
m_uniformNames[locNeedle] = uniform.name; m_uniformIndexInTProgram[locNeedle] = index;
m_uniformTypes[locNeedle] = uniform.glDefineType;
m_uniformIsOpaqueType[locNeedle] = uniform.getType()->isOpaque();
m_uniformArraySizes[locNeedle] = uniform.size;
m_uniformLocations[uniform.name] = locNeedle; m_uniformLocations[uniform.name] = locNeedle;
break; break;
} }
@@ -25,17 +25,32 @@ namespace MobileGL {
Uint GetMaxUniformLocation() const { return m_maxUniformLocation; } Uint GetMaxUniformLocation() const { return m_maxUniformLocation; }
Int GetUniformLocation(const String& name) { Int GetUniformLocation(const String& name) {
const auto it = m_uniformLocations.find(name); const auto it = m_uniformLocations.find(name);
if (it == m_uniformLocations.end()) return -1; if (it == m_uniformLocations.end())
return -1;
return (Int)it->second; return (Int)it->second;
} }
GLenum GetUniformType(Uint index) const { return m_uniformTypes[index]; }
Bool IsUniformOpaqueAtLocation(Uint location) const { return m_uniformIsOpaqueType[location]; } GLenum GetUniformType(Uint location) const {
auto& uniform = m_program->getUniform(m_uniformIndexInTProgram[location]);
return uniform.glDefineType;
}
const String& GetUniformName(Uint index) const { return m_uniformNames[index]; } const glslang::TType* GetUniformTType(Uint location) const {
auto& uniform = m_program->getUniform(m_uniformIndexInTProgram[location]);
return uniform.getType();
}
Bool IsUniformOpaqueAtLocation(Uint location) const {
return GetUniformTType(location)->isOpaque();
}
const String& GetUniformName(Uint location) const {
auto& uniform = m_program->getUniform(m_uniformIndexInTProgram[location]);
return uniform.name;
}
Uint GetUniformOffset(Uint location) const { return m_uniformOffsets[location]; } Uint GetUniformOffset(Uint location) const { return m_uniformOffsets[location]; }
Uint GetUniformSizesInBytes(Uint location) const { Uint GetUniformSizesInBytes(Uint location) const {
return MG_Util::GetGLTypeSize(m_uniformTypes[location]); return MG_Util::GetGLTypeSize(GetUniformType(location));
} }
Int GetAttributeLocation(const String& name) { Int GetAttributeLocation(const String& name) {
@@ -47,6 +62,14 @@ namespace MobileGL {
void* MapUBO() { return m_uboScratch.data(); } void* MapUBO() { return m_uboScratch.data(); }
Uint GetUBOSize() const { return static_cast<Uint>(m_uboScratch.size()); } Uint GetUBOSize() const { return static_cast<Uint>(m_uboScratch.size()); }
void SetUniformSamplerOrImageUnitIndex(Uint location, Int unit) {
m_uniformSamplerOrImageUnitIndex[location] = unit;
}
Int SetUniformSamplerOrImageUnitIndex(Uint location) const {
return m_uniformSamplerOrImageUnitIndex[location];
}
Bool GetDeleteStatus() const { return m_deleteStatus; } Bool GetDeleteStatus() const { return m_deleteStatus; }
Bool GetLinkStatus() const { return m_linkStatus; } Bool GetLinkStatus() const { return m_linkStatus; }
Bool GetValidateStatus() const { return m_validateStatus; } Bool GetValidateStatus() const { return m_validateStatus; }
@@ -82,12 +105,10 @@ namespace MobileGL {
UnorderedMap<String, Uint> m_uniformLocations; UnorderedMap<String, Uint> m_uniformLocations;
// Ordered by location, // Ordered by location,
// aka. m_uniformNames[loc] == "name at location `loc`" // aka. m_uniformIndexInTProgram[loc] == "uniform index of TProgram at location `loc`"
Vector<String> m_uniformNames; Vector<Int> m_uniformIndexInTProgram;
// ditto. // ditto. Will be set at glUniform1i
Vector<GLenum> m_uniformTypes; Vector<Int> m_uniformSamplerOrImageUnitIndex;
Vector<Bool> m_uniformIsOpaqueType;
Vector<Int> m_uniformArraySizes;
// Need to be reflected after linking of SPIR-V binary // Need to be reflected after linking of SPIR-V binary
Vector<Uint> m_uniformOffsets; Vector<Uint> m_uniformOffsets;