[Chore]: (MG_State/Program): use proper enum constant, get rid of junk

This commit is contained in:
2025-11-12 16:00:44 +08:00
parent bfeb827eb7
commit 9bc0ae6b97
3 changed files with 24 additions and 12 deletions
+14 -4
View File
@@ -486,6 +486,11 @@ namespace MobileGL::MG_Backend::DirectGLES {
// Attach current shaders // Attach current shaders
auto& attachedShaders = stateProgramObject->GetAttachedShaders(); auto& attachedShaders = stateProgramObject->GetAttachedShaders();
MGLOG_D("Attaching %zu shaders to program %u", attachedShaders.size(), m_backendProgramId); MGLOG_D("Attaching %zu shaders to program %u", attachedShaders.size(), m_backendProgramId);
for (auto& shader: attachedShaders) {
const auto& src = shader->GetShaderSource();
const auto& stage = MG_Util::ConvertGLEnumToString(MG_Util::ConvertShaderStageToGLEnum(shader->GetShaderStage()));
MGLOG_D("Original src @ %s: \n%s", stage.c_str(), src.c_str());
}
auto& shaderSpirvs = stateProgramObject->GetGeneratedSpirv(); auto& shaderSpirvs = stateProgramObject->GetGeneratedSpirv();
for (int index = 0; index < attachedShaders.size(); ++index) { for (int index = 0; index < attachedShaders.size(); ++index) {
@@ -501,9 +506,14 @@ namespace MobileGL::MG_Backend::DirectGLES {
auto& spirvCode = shaderSpirvs[index]; auto& spirvCode = shaderSpirvs[index];
MG_Util::ShaderTranspiler::SpvcSession spvcSession(spirvCode); MG_Util::ShaderTranspiler::SpvcSession spvcSession(spirvCode);
if (glShaderType == GL_VERTEX_SHADER) { // if (glShaderType == GL_VERTEX_SHADER) {
spvcSession.SetVertexAttribLocation(stateProgramObject->GetAttribLocationMap()); // if (stateProgramObject->GetAttribLocationMap().empty())
} // MGLOG_D("%s: no explicitly set vertex in location", __func__);
// for (auto& [name, loc]: stateProgramObject->GetAttribLocationMap()) {
// MGLOG_D("%s: got explicitly set - layout(location = %d) %s;", __func__, loc, name.c_str());
// }
//// spvcSession.SetVertexAttribLocation(stateProgramObject->GetAttribLocationMap());
// }
spvc_compiler_options options; spvc_compiler_options options;
spvcSession.CreateOptions(&options); spvcSession.CreateOptions(&options);
@@ -533,7 +543,7 @@ namespace MobileGL::MG_Backend::DirectGLES {
source = ForceSupporterOutput(source); source = ForceSupporterOutput(source);
const char* sourceCStr = source.c_str(); const char* sourceCStr = source.c_str();
MGLOG_D("Setting shader source for backend shader ID: %u", backendShaderId); MGLOG_D("Setting shader source for backend shader ID: %u\nsrc:\n%s", backendShaderId, sourceCStr);
MG_External::GLES::glShaderSource(backendShaderId, 1, &sourceCStr, nullptr); MG_External::GLES::glShaderSource(backendShaderId, 1, &sourceCStr, nullptr);
MG_External::GLES::glCompileShader(backendShaderId); MG_External::GLES::glCompileShader(backendShaderId);
@@ -149,7 +149,7 @@ 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 != 4095) { if (location != glslang::TQualifier::layoutLocationEnd) {
m_maxUniformLocation = std::max(m_maxUniformLocation, location); m_maxUniformLocation = std::max(m_maxUniformLocation, location);
} }
m_uniformNameMaxLength = std::max(m_uniformNameMaxLength, (Int)uniform.name.length()); m_uniformNameMaxLength = std::max(m_uniformNameMaxLength, (Int)uniform.name.length());
@@ -171,7 +171,7 @@ namespace MobileGL {
} }
// i-th elements refers to uniform at layout(location = i, ...) // i-th elements refers to uniform at layout(location = i, ...)
m_uniformIndexInTProgram.resize(m_maxUniformLocation + 1, 4095); m_uniformIndexInTProgram.resize(m_maxUniformLocation + 1, glslang::TQualifier::layoutLocationEnd);
m_uniformSamplerOrImageUnitIndex.resize(m_maxUniformLocation + 1, -1); m_uniformSamplerOrImageUnitIndex.resize(m_maxUniformLocation + 1, -1);
Vector<int> unallocatedUniformIndex; Vector<int> unallocatedUniformIndex;
@@ -180,7 +180,7 @@ 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 (m_uniformLocations[uniform.name] == 4095) { if (m_uniformLocations[uniform.name] == glslang::TQualifier::layoutLocationEnd) {
unallocatedUniformIndex.emplace_back(i); unallocatedUniformIndex.emplace_back(i);
MGLOG_D("ProgramObject %u: Reflection - uniform '%s' is unallocated, will assign later", MGLOG_D("ProgramObject %u: Reflection - uniform '%s' is unallocated, will assign later",
m_externalIndex, uniform.name.c_str()); m_externalIndex, uniform.name.c_str());
@@ -195,7 +195,7 @@ namespace MobileGL {
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_uniformIndexInTProgram[locNeedle] != 4095) continue; if (m_uniformIndexInTProgram[locNeedle] != glslang::TQualifier::layoutLocationEnd) continue;
// Found a vacant location at locNeedle // Found a vacant location at locNeedle
m_uniformIndexInTProgram[locNeedle] = index; m_uniformIndexInTProgram[locNeedle] = index;
m_uniformLocations[uniform.name] = locNeedle; m_uniformLocations[uniform.name] = locNeedle;
@@ -207,13 +207,15 @@ namespace MobileGL {
} }
} }
// ------------ attributes (vertex in) ---------------
int inCount = m_program->getNumPipeInputs(); int inCount = m_program->getNumPipeInputs();
MGLOG_D("ProgramObject %u: Reflection - pipe input count (attributes) = %d", m_externalIndex, inCount); MGLOG_D("ProgramObject %u: Reflection - pipe input count (attributes) = %d", m_externalIndex, inCount);
int maxLoc = -1; int maxLoc = -1;
for (int i = 0; i < inCount; ++i) { for (int i = 0; i < inCount; ++i) {
int loc = m_program->getPipeInput(i).layoutLocation(); int loc = m_program->getPipeInput(i).layoutLocation();
if (loc >= 0) maxLoc = std::max(maxLoc, loc); if (loc >= 0 && loc != glslang::TQualifier::layoutLocationEnd)
maxLoc = std::max(maxLoc, loc);
MGLOG_D("ProgramObject %u: Reflection - pipe input[%d] name='%s' layoutLocation=%d glType=%u", MGLOG_D("ProgramObject %u: Reflection - pipe input[%d] name='%s' layoutLocation=%d glType=%u",
m_externalIndex, i, m_program->getPipeInput(i).name.c_str(), loc, m_externalIndex, i, m_program->getPipeInput(i).name.c_str(), loc,
m_program->getPipeInput(i).glDefineType); m_program->getPipeInput(i).glDefineType);
@@ -245,7 +247,7 @@ namespace MobileGL {
if (location >= 0 && location < (int)m_attribs.size()) { if (location >= 0 && location < (int)m_attribs.size()) {
m_attribs[location] = inVar.name; m_attribs[location] = inVar.name;
m_attribTypes[location] = inVar.glDefineType; m_attribTypes[location] = inVar.glDefineType;
MGLOG_D("ProgramObject %u: Reflection - placed attrib '%s' at explicit location %d", MGLOG_D("ProgramObject %u: Reflection - got attrib '%s' at explicit location %d",
m_externalIndex, inVar.name.c_str(), location); m_externalIndex, inVar.name.c_str(), location);
} }
// else if (location >= (int)m_attribs.size()) { // else if (location >= (int)m_attribs.size()) {
@@ -104,7 +104,7 @@ namespace MobileGL {
Uint GetExternalIndex() const { return m_externalIndex; } Uint GetExternalIndex() const { return m_externalIndex; }
const UnorderedMap<String, Uint>& GetAttribLocationMap() const { return m_attribLocation; } // const UnorderedMap<String, Uint>& GetAttribLocationMap() const { return m_attribLocation; }
private: private:
void DoReflection(); void DoReflection();
@@ -124,7 +124,7 @@ namespace MobileGL {
Vector<String> m_attribs; Vector<String> m_attribs;
Vector<GLenum> m_attribTypes; Vector<GLenum> m_attribTypes;
// For SpvcSession::SetVertexAttribLocation() // For SpvcSession::SetVertexAttribLocation()
UnorderedMap<String, Uint> m_attribLocation; // UnorderedMap<String, Uint> m_attribLocation;
// Uniforms // Uniforms
UnorderedMap<String, Uint> m_uniformLocations; UnorderedMap<String, Uint> m_uniformLocations;