[Improvement] (MG_Backend/DirectGLES): Make most stuff const auto&

This commit is contained in:
BZLZHH
2025-10-26 09:56:56 +08:00
parent d90e35d630
commit b8d8188239
2 changed files with 36 additions and 26 deletions
+22 -12
View File
@@ -79,7 +79,7 @@ namespace MobileGL::MG_Backend::DirectGLES {
for (int index = 0; index < MG_State::GLState::TextureState::MAX_TEXTURE_IMAGE_UNITS; ++index) { for (int index = 0; index < MG_State::GLState::TextureState::MAX_TEXTURE_IMAGE_UNITS; ++index) {
auto& unit = MG_State::pGLContext->GetTextureUnitObject(index); auto& unit = MG_State::pGLContext->GetTextureUnitObject(index);
for (auto& bindingSlot : unit.GetAllBindingSlots()) { for (const auto& bindingSlot : unit.GetAllBindingSlots()) {
const auto& textureObject = bindingSlot.GetBoundObject(); const auto& textureObject = bindingSlot.GetBoundObject();
if (textureObject) { if (textureObject) {
texturesToSync.push_back(textureObject); texturesToSync.push_back(textureObject);
@@ -90,7 +90,7 @@ namespace MobileGL::MG_Backend::DirectGLES {
const auto& currentFBO = const auto& currentFBO =
MG_State::pGLContext->GetFramebufferBindingSlot(FramebufferTarget::Draw).GetBoundObject(); MG_State::pGLContext->GetFramebufferBindingSlot(FramebufferTarget::Draw).GetBoundObject();
if (currentFBO) { if (currentFBO) {
for (auto& attachment : currentFBO->GetAllAttachments()) { for (const auto& attachment : currentFBO->GetAllAttachments()) {
if (!attachment.IsTexture()) continue; if (!attachment.IsTexture()) continue;
const auto& textureObject = attachment.GetTexture(); const auto& textureObject = attachment.GetTexture();
if (textureObject) { if (textureObject) {
@@ -155,7 +155,7 @@ namespace MobileGL::MG_Backend::DirectGLES {
MG_External::GLES::glDisable(GL_BLEND); 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; BlendFactor srcRGB, dstRGB, srcAlpha, dstAlpha;
@@ -218,7 +218,7 @@ namespace MobileGL::MG_Backend::DirectGLES {
MG_External::GLES::glUseProgram(0); MG_External::GLES::glUseProgram(0);
return; return;
} }
auto backendProgramIt = g_backendProgramObjects.find(currentProgram); const auto& backendProgramIt = g_backendProgramObjects.find(currentProgram);
SharedPtr<BackendProgramObjectImpl> backendProgram; SharedPtr<BackendProgramObjectImpl> backendProgram;
if (backendProgramIt == g_backendProgramObjects.end()) { if (backendProgramIt == g_backendProgramObjects.end()) {
backendProgram = MakeShared<BackendProgramObjectImpl>(); backendProgram = MakeShared<BackendProgramObjectImpl>();
@@ -230,7 +230,6 @@ namespace MobileGL::MG_Backend::DirectGLES {
backendProgram->SyncToBackend(currentProgram); backendProgram->SyncToBackend(currentProgram);
} }
} }
backendProgram->Use();
} }
} // namespace PrgramImpl } // namespace PrgramImpl
@@ -241,9 +240,10 @@ namespace MobileGL::MG_Backend::DirectGLES {
FramebufferImpl::SyncCurrentFBO(); FramebufferImpl::SyncCurrentFBO();
PrgramImpl::SyncCurrentProgram(); 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) { 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()) { if (backendFBOIt != FramebufferImpl::g_backendFramebufferObjects.end()) {
backendFBOIt->second->Bind(); backendFBOIt->second->Bind();
} }
@@ -251,9 +251,9 @@ namespace MobileGL::MG_Backend::DirectGLES {
MG_External::GLES::glBindFramebuffer(GL_FRAMEBUFFER, 0); MG_External::GLES::glBindFramebuffer(GL_FRAMEBUFFER, 0);
} }
auto currentVAO = MG_State::pGLContext->GetBoundVertexArray(); const auto& currentVAO = MG_State::pGLContext->GetBoundVertexArray();
if (currentVAO) { if (currentVAO) {
auto backendVAOIt = VertexArrayImpl::g_backendVertexArrayObjects.find(currentVAO); const auto& backendVAOIt = VertexArrayImpl::g_backendVertexArrayObjects.find(currentVAO);
if (backendVAOIt != VertexArrayImpl::g_backendVertexArrayObjects.end()) { if (backendVAOIt != VertexArrayImpl::g_backendVertexArrayObjects.end()) {
backendVAOIt->second->Bind(); backendVAOIt->second->Bind();
} }
@@ -269,11 +269,11 @@ namespace MobileGL::MG_Backend::DirectGLES {
MG_External::GLES::glActiveTexture(GL_TEXTURE0 + unit); MG_External::GLES::glActiveTexture(GL_TEXTURE0 + unit);
for (auto& bindingSlot : textureUnit.GetAllBindingSlots()) { for (const auto& bindingSlot : textureUnit.GetAllBindingSlots()) {
auto textureObject = bindingSlot.GetBoundObject(); const auto& textureObject = bindingSlot.GetBoundObject();
if (!textureObject) continue; 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; if (backendTextureIt == TextureImpl::g_backendTextureObjects.end()) continue;
GLenum target = MG_Util::ConvertTextureTargetToGLEnum(textureObject->GetTarget()); GLenum target = MG_Util::ConvertTextureTargetToGLEnum(textureObject->GetTarget());
@@ -283,6 +283,16 @@ namespace MobileGL::MG_Backend::DirectGLES {
Int originalActiveUnit = MG_State::pGLContext->GetActiveTextureUnit(); Int originalActiveUnit = MG_State::pGLContext->GetActiveTextureUnit();
MG_External::GLES::glActiveTexture(GL_TEXTURE0 + originalActiveUnit); 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) { void DrawElements(GLenum mode, GLsizei count, GLenum type, const void* indices) {
+14 -14
View File
@@ -86,7 +86,7 @@ namespace MobileGL::MG_Backend::DirectGLES {
MGLOG_D("Syncing buffer sub-data (glBufferSubData) for object with ID : %u", m_backendBufferId); MGLOG_D("Syncing buffer sub-data (glBufferSubData) for object with ID : %u", m_backendBufferId);
const void* data = stateBufferObject->GetDataReadOnly()->data(); const void* data = stateBufferObject->GetDataReadOnly()->data();
auto range = stateBufferObject->GetDirtyRange(); const auto& range = stateBufferObject->GetDirtyRange();
// dirty range: [range.start, range.end) // dirty range: [range.start, range.end)
MG_External::GLES::glBindBuffer(TempBufferTarget, m_backendBufferId); 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("Syncing buffer map (glMapBuffer) for object with ID : %u", m_backendBufferId);
MGLOG_D("Mapping buffer with ID: %u", m_backendBufferId); MGLOG_D("Mapping buffer with ID: %u", m_backendBufferId);
MG_External::GLES::glBindBuffer(TempBufferTarget, m_backendBufferId); MG_External::GLES::glBindBuffer(TempBufferTarget, m_backendBufferId);
auto range = stateBufferObject->GetDirtyRange(); const auto& range = stateBufferObject->GetDirtyRange();
void* mappedData = MG_External::GLES::glMapBufferRange( void* mappedData = MG_External::GLES::glMapBufferRange(
TempBufferTarget, range.start, range.end - range.start, TempBufferTarget, range.start, range.end - range.start,
(invalidate ? GL_MAP_INVALIDATE_BUFFER_BIT : 0) | GL_MAP_WRITE_BIT | GL_MAP_UNSYNCHRONIZED_BIT); (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()) { for (const auto& attribIndex : stateVAOObject->GetDirtyAttributeIndices()) {
const auto& attrib = stateVAOObject->GetAttribute(attribIndex); const auto& attrib = stateVAOObject->GetAttribute(attribIndex);
auto bufferObject = attrib.Buffer; const auto& bufferObject = attrib.Buffer;
if (!bufferObject) { if (!bufferObject) {
MGLOG_W("Attribute has no bound buffer, skipping."); MGLOG_W("Attribute has no bound buffer, skipping.");
continue; continue;
} }
auto backendBufferIt = BufferImpl::g_backendBufferObjects.find(bufferObject); const auto& backendBufferIt = BufferImpl::g_backendBufferObjects.find(bufferObject);
if (backendBufferIt == BufferImpl::g_backendBufferObjects.end()) { if (backendBufferIt == BufferImpl::g_backendBufferObjects.end()) {
MGLOG_E("No backend buffer found for attribute's buffer, cannot bind attribute."); MGLOG_E("No backend buffer found for attribute's buffer, cannot bind attribute.");
continue; continue;
} }
auto backendBufferObject = backendBufferIt->second; const auto& backendBufferObject = backendBufferIt->second;
backendBufferObject->Bind(GL_ARRAY_BUFFER); backendBufferObject->Bind(GL_ARRAY_BUFFER);
MG_External::GLES::glEnableVertexAttribArray(attribIndex); MG_External::GLES::glEnableVertexAttribArray(attribIndex);
@@ -180,11 +180,11 @@ namespace MobileGL::MG_Backend::DirectGLES {
// TODO: divisor // TODO: divisor
} }
auto indexBufferBinding = stateVAOObject->GetIndexBufferBindingSlot().GetBoundObject(); const auto& indexBufferBinding = stateVAOObject->GetIndexBufferBindingSlot().GetBoundObject();
if (indexBufferBinding) { if (indexBufferBinding) {
auto backendBufferIt = BufferImpl::g_backendBufferObjects.find(indexBufferBinding); const auto& backendBufferIt = BufferImpl::g_backendBufferObjects.find(indexBufferBinding);
if (backendBufferIt != BufferImpl::g_backendBufferObjects.end()) { if (backendBufferIt != BufferImpl::g_backendBufferObjects.end()) {
auto backendBufferObject = backendBufferIt->second; const auto& backendBufferObject = backendBufferIt->second;
backendBufferObject->Bind(GL_ELEMENT_ARRAY_BUFFER); backendBufferObject->Bind(GL_ELEMENT_ARRAY_BUFFER);
} else { } else {
MGLOG_E("No backend buffer found for index buffer binding, cannot bind index buffer."); 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 { // Update sampler parameters; TODO: always use sampler objects in backend
auto samplerObject = stateTextureObject->GetSamplerObject(); const auto& samplerObject = stateTextureObject->GetSamplerObject();
if (samplerObject) { if (samplerObject) {
MG_External::GLES::glTexParameteri( MG_External::GLES::glTexParameteri(
target, GL_TEXTURE_MIN_FILTER, target, GL_TEXTURE_MIN_FILTER,
@@ -359,13 +359,13 @@ namespace MobileGL::MG_Backend::DirectGLES {
} }
if (attachment.IsTexture()) { if (attachment.IsTexture()) {
auto textureObject = attachment.GetTexture(); const auto& textureObject = attachment.GetTexture();
auto backendTextureIt = TextureImpl::g_backendTextureObjects.find(textureObject); const auto& backendTextureIt = TextureImpl::g_backendTextureObjects.find(textureObject);
if (backendTextureIt == TextureImpl::g_backendTextureObjects.end()) { if (backendTextureIt == TextureImpl::g_backendTextureObjects.end()) {
MGLOG_E("No backend texture found for FBO attachment, cannot bind texture."); MGLOG_E("No backend texture found for FBO attachment, cannot bind texture.");
continue; continue;
} }
auto backendTextureObject = backendTextureIt->second; const auto& backendTextureObject = backendTextureIt->second;
backendTextureObject->Bind(MG_Util::ConvertTextureTargetToGLEnum(textureObject->GetTarget())); backendTextureObject->Bind(MG_Util::ConvertTextureTargetToGLEnum(textureObject->GetTarget()));
MG_External::GLES::glFramebufferTexture2D( MG_External::GLES::glFramebufferTexture2D(
GL_FRAMEBUFFER, MG_Util::ConvertFramebufferAttachmentTypeToGLEnum(attachmentType), GL_FRAMEBUFFER, MG_Util::ConvertFramebufferAttachmentTypeToGLEnum(attachmentType),
@@ -451,8 +451,8 @@ namespace MobileGL::MG_Backend::DirectGLES {
} }
} }
// Attach current shaders // Attach current shaders
for (auto& shader : stateProgramObject->GetAttachedShaders()) { for (const auto& shader : stateProgramObject->GetAttachedShaders()) {
auto it = g_backendShaderObjects.find(shader); const auto& it = g_backendShaderObjects.find(shader);
if (it != g_backendShaderObjects.end() && it->second) { if (it != g_backendShaderObjects.end() && it->second) {
MG_External::GLES::glAttachShader(m_backendProgramId, it->second->GetBackendShaderId()); MG_External::GLES::glAttachShader(m_backendProgramId, it->second->GetBackendShaderId());
} }