From b8d818823924e8fb35faa5cd4300e7a4f9772cc9 Mon Sep 17 00:00:00 2001 From: BZLZHH Date: Sun, 26 Oct 2025 09:56:41 +0800 Subject: [PATCH] [Improvement] (MG_Backend/DirectGLES): Make most stuff const auto& --- MobileGL/MG_Backend/DirectGLES/DirectGLES.cpp | 34 ++++++++++++------- MobileGL/MG_Backend/DirectGLES/Managers.cpp | 28 +++++++-------- 2 files changed, 36 insertions(+), 26 deletions(-) diff --git a/MobileGL/MG_Backend/DirectGLES/DirectGLES.cpp b/MobileGL/MG_Backend/DirectGLES/DirectGLES.cpp index 251ba9f1..8d6baafc 100644 --- a/MobileGL/MG_Backend/DirectGLES/DirectGLES.cpp +++ b/MobileGL/MG_Backend/DirectGLES/DirectGLES.cpp @@ -79,7 +79,7 @@ namespace MobileGL::MG_Backend::DirectGLES { for (int index = 0; index < MG_State::GLState::TextureState::MAX_TEXTURE_IMAGE_UNITS; ++index) { auto& unit = MG_State::pGLContext->GetTextureUnitObject(index); - for (auto& bindingSlot : unit.GetAllBindingSlots()) { + for (const auto& bindingSlot : unit.GetAllBindingSlots()) { const auto& textureObject = bindingSlot.GetBoundObject(); if (textureObject) { texturesToSync.push_back(textureObject); @@ -90,7 +90,7 @@ namespace MobileGL::MG_Backend::DirectGLES { const auto& currentFBO = MG_State::pGLContext->GetFramebufferBindingSlot(FramebufferTarget::Draw).GetBoundObject(); if (currentFBO) { - for (auto& attachment : currentFBO->GetAllAttachments()) { + for (const auto& attachment : currentFBO->GetAllAttachments()) { if (!attachment.IsTexture()) continue; const auto& textureObject = attachment.GetTexture(); if (textureObject) { @@ -155,7 +155,7 @@ namespace MobileGL::MG_Backend::DirectGLES { MG_External::GLES::glDisable(GL_BLEND); } - auto ToGLBoolean = [](Bool b) -> GLboolean { return b ? GL_TRUE : GL_FALSE; }; + const auto& ToGLBoolean = [](Bool b) -> GLboolean { return b ? GL_TRUE : GL_FALSE; }; { BlendFactor srcRGB, dstRGB, srcAlpha, dstAlpha; @@ -218,7 +218,7 @@ namespace MobileGL::MG_Backend::DirectGLES { MG_External::GLES::glUseProgram(0); return; } - auto backendProgramIt = g_backendProgramObjects.find(currentProgram); + const auto& backendProgramIt = g_backendProgramObjects.find(currentProgram); SharedPtr backendProgram; if (backendProgramIt == g_backendProgramObjects.end()) { backendProgram = MakeShared(); @@ -230,7 +230,6 @@ namespace MobileGL::MG_Backend::DirectGLES { backendProgram->SyncToBackend(currentProgram); } } - backendProgram->Use(); } } // namespace PrgramImpl @@ -241,9 +240,10 @@ namespace MobileGL::MG_Backend::DirectGLES { FramebufferImpl::SyncCurrentFBO(); PrgramImpl::SyncCurrentProgram(); - auto currentFBO = MG_State::pGLContext->GetFramebufferBindingSlot(FramebufferTarget::Draw).GetBoundObject(); + const auto& currentFBO = + MG_State::pGLContext->GetFramebufferBindingSlot(FramebufferTarget::Draw).GetBoundObject(); if (currentFBO && currentFBO != MG_Impl::GLImpl::FramebufferImpl::pDefaultFramebufferInfo->defaultFBO) { - auto backendFBOIt = FramebufferImpl::g_backendFramebufferObjects.find(currentFBO); + const auto& backendFBOIt = FramebufferImpl::g_backendFramebufferObjects.find(currentFBO); if (backendFBOIt != FramebufferImpl::g_backendFramebufferObjects.end()) { backendFBOIt->second->Bind(); } @@ -251,9 +251,9 @@ namespace MobileGL::MG_Backend::DirectGLES { MG_External::GLES::glBindFramebuffer(GL_FRAMEBUFFER, 0); } - auto currentVAO = MG_State::pGLContext->GetBoundVertexArray(); + const auto& currentVAO = MG_State::pGLContext->GetBoundVertexArray(); if (currentVAO) { - auto backendVAOIt = VertexArrayImpl::g_backendVertexArrayObjects.find(currentVAO); + const auto& backendVAOIt = VertexArrayImpl::g_backendVertexArrayObjects.find(currentVAO); if (backendVAOIt != VertexArrayImpl::g_backendVertexArrayObjects.end()) { backendVAOIt->second->Bind(); } @@ -269,11 +269,11 @@ namespace MobileGL::MG_Backend::DirectGLES { MG_External::GLES::glActiveTexture(GL_TEXTURE0 + unit); - for (auto& bindingSlot : textureUnit.GetAllBindingSlots()) { - auto textureObject = bindingSlot.GetBoundObject(); + for (const auto& bindingSlot : textureUnit.GetAllBindingSlots()) { + const auto& textureObject = bindingSlot.GetBoundObject(); if (!textureObject) continue; - auto backendTextureIt = TextureImpl::g_backendTextureObjects.find(textureObject); + const auto& backendTextureIt = TextureImpl::g_backendTextureObjects.find(textureObject); if (backendTextureIt == TextureImpl::g_backendTextureObjects.end()) continue; GLenum target = MG_Util::ConvertTextureTargetToGLEnum(textureObject->GetTarget()); @@ -283,6 +283,16 @@ namespace MobileGL::MG_Backend::DirectGLES { Int originalActiveUnit = MG_State::pGLContext->GetActiveTextureUnit(); MG_External::GLES::glActiveTexture(GL_TEXTURE0 + originalActiveUnit); + + const auto& currentProgram = MG_State::pGLContext->GetCurrentProgram(); + if (currentProgram) { + const auto& backendProgramIt = PrgramImpl::g_backendProgramObjects.find(currentProgram); + if (backendProgramIt != PrgramImpl::g_backendProgramObjects.end()) { + backendProgramIt->second->Use(); + } else { + MG_External::GLES::glUseProgram(0); + } + } } void DrawElements(GLenum mode, GLsizei count, GLenum type, const void* indices) { diff --git a/MobileGL/MG_Backend/DirectGLES/Managers.cpp b/MobileGL/MG_Backend/DirectGLES/Managers.cpp index 847e57b4..5fa2f15a 100644 --- a/MobileGL/MG_Backend/DirectGLES/Managers.cpp +++ b/MobileGL/MG_Backend/DirectGLES/Managers.cpp @@ -86,7 +86,7 @@ namespace MobileGL::MG_Backend::DirectGLES { MGLOG_D("Syncing buffer sub-data (glBufferSubData) for object with ID : %u", m_backendBufferId); const void* data = stateBufferObject->GetDataReadOnly()->data(); - auto range = stateBufferObject->GetDirtyRange(); + const auto& range = stateBufferObject->GetDirtyRange(); // dirty range: [range.start, range.end) MG_External::GLES::glBindBuffer(TempBufferTarget, m_backendBufferId); @@ -101,7 +101,7 @@ namespace MobileGL::MG_Backend::DirectGLES { MGLOG_D("Syncing buffer map (glMapBuffer) for object with ID : %u", m_backendBufferId); MGLOG_D("Mapping buffer with ID: %u", m_backendBufferId); MG_External::GLES::glBindBuffer(TempBufferTarget, m_backendBufferId); - auto range = stateBufferObject->GetDirtyRange(); + const auto& range = stateBufferObject->GetDirtyRange(); void* mappedData = MG_External::GLES::glMapBufferRange( TempBufferTarget, range.start, range.end - range.start, (invalidate ? GL_MAP_INVALIDATE_BUFFER_BIT : 0) | GL_MAP_WRITE_BIT | GL_MAP_UNSYNCHRONIZED_BIT); @@ -157,18 +157,18 @@ namespace MobileGL::MG_Backend::DirectGLES { for (const auto& attribIndex : stateVAOObject->GetDirtyAttributeIndices()) { const auto& attrib = stateVAOObject->GetAttribute(attribIndex); - auto bufferObject = attrib.Buffer; + const auto& bufferObject = attrib.Buffer; if (!bufferObject) { MGLOG_W("Attribute has no bound buffer, skipping."); continue; } - auto backendBufferIt = BufferImpl::g_backendBufferObjects.find(bufferObject); + const auto& backendBufferIt = BufferImpl::g_backendBufferObjects.find(bufferObject); if (backendBufferIt == BufferImpl::g_backendBufferObjects.end()) { MGLOG_E("No backend buffer found for attribute's buffer, cannot bind attribute."); continue; } - auto backendBufferObject = backendBufferIt->second; + const auto& backendBufferObject = backendBufferIt->second; backendBufferObject->Bind(GL_ARRAY_BUFFER); MG_External::GLES::glEnableVertexAttribArray(attribIndex); @@ -180,11 +180,11 @@ namespace MobileGL::MG_Backend::DirectGLES { // TODO: divisor } - auto indexBufferBinding = stateVAOObject->GetIndexBufferBindingSlot().GetBoundObject(); + const auto& indexBufferBinding = stateVAOObject->GetIndexBufferBindingSlot().GetBoundObject(); if (indexBufferBinding) { - auto backendBufferIt = BufferImpl::g_backendBufferObjects.find(indexBufferBinding); + const auto& backendBufferIt = BufferImpl::g_backendBufferObjects.find(indexBufferBinding); if (backendBufferIt != BufferImpl::g_backendBufferObjects.end()) { - auto backendBufferObject = backendBufferIt->second; + const auto& backendBufferObject = backendBufferIt->second; backendBufferObject->Bind(GL_ELEMENT_ARRAY_BUFFER); } else { MGLOG_E("No backend buffer found for index buffer binding, cannot bind index buffer."); @@ -274,7 +274,7 @@ namespace MobileGL::MG_Backend::DirectGLES { { // Update sampler parameters; TODO: always use sampler objects in backend - auto samplerObject = stateTextureObject->GetSamplerObject(); + const auto& samplerObject = stateTextureObject->GetSamplerObject(); if (samplerObject) { MG_External::GLES::glTexParameteri( target, GL_TEXTURE_MIN_FILTER, @@ -359,13 +359,13 @@ namespace MobileGL::MG_Backend::DirectGLES { } if (attachment.IsTexture()) { - auto textureObject = attachment.GetTexture(); - auto backendTextureIt = TextureImpl::g_backendTextureObjects.find(textureObject); + const auto& textureObject = attachment.GetTexture(); + const auto& backendTextureIt = TextureImpl::g_backendTextureObjects.find(textureObject); if (backendTextureIt == TextureImpl::g_backendTextureObjects.end()) { MGLOG_E("No backend texture found for FBO attachment, cannot bind texture."); continue; } - auto backendTextureObject = backendTextureIt->second; + const auto& backendTextureObject = backendTextureIt->second; backendTextureObject->Bind(MG_Util::ConvertTextureTargetToGLEnum(textureObject->GetTarget())); MG_External::GLES::glFramebufferTexture2D( GL_FRAMEBUFFER, MG_Util::ConvertFramebufferAttachmentTypeToGLEnum(attachmentType), @@ -451,8 +451,8 @@ namespace MobileGL::MG_Backend::DirectGLES { } } // Attach current shaders - for (auto& shader : stateProgramObject->GetAttachedShaders()) { - auto it = g_backendShaderObjects.find(shader); + for (const auto& shader : stateProgramObject->GetAttachedShaders()) { + const auto& it = g_backendShaderObjects.find(shader); if (it != g_backendShaderObjects.end() && it->second) { MG_External::GLES::glAttachShader(m_backendProgramId, it->second->GetBackendShaderId()); }