From d8cd5a235a40f8119d0d8d290e1da6c1dd08b848 Mon Sep 17 00:00:00 2001 From: Swung0x48 Date: Tue, 18 Nov 2025 22:37:24 +0800 Subject: [PATCH] [Chore] (MG_Backend/DirectGLES): trim redundant stuff in BackendFramebufferObject::SyncToBackend --- MobileGL/MG_Backend/DirectGLES/Managers.cpp | 96 +++++++++++++-------- MobileGL/MG_Backend/DirectGLES/Managers.h | 7 +- 2 files changed, 63 insertions(+), 40 deletions(-) diff --git a/MobileGL/MG_Backend/DirectGLES/Managers.cpp b/MobileGL/MG_Backend/DirectGLES/Managers.cpp index e8f72408..691c66b4 100644 --- a/MobileGL/MG_Backend/DirectGLES/Managers.cpp +++ b/MobileGL/MG_Backend/DirectGLES/Managers.cpp @@ -520,66 +520,88 @@ namespace MobileGL::MG_Backend::DirectGLES { if (asTarget == FramebufferTarget::Draw) { // Create mappings for draw buffers int nBuffers = 0; - std::fill(m_frontendBuffers, m_frontendBuffers + MG_State::GLState::FramebufferObject::MAX_DRAW_BUFFERS, + std::fill(m_frontendDrawBuffers, m_frontendDrawBuffers + MG_State::GLState::FramebufferObject::MAX_DRAW_BUFFERS, + FramebufferAttachmentType::None); + std::fill(m_compactedFrontendDrawBuffers, + m_compactedFrontendDrawBuffers + MG_State::GLState::FramebufferObject::MAX_DRAW_BUFFERS, + FramebufferAttachmentType::None); + std::fill(m_backendDrawBuffers, m_backendDrawBuffers + MG_State::GLState::FramebufferObject::MAX_DRAW_BUFFERS, GL_NONE); - std::fill(m_backendBuffers, m_backendBuffers + MG_State::GLState::FramebufferObject::MAX_DRAW_BUFFERS, - GL_NONE); - std::fill(m_compactedFrontendBuffers, - m_compactedFrontendBuffers + MG_State::GLState::FramebufferObject::MAX_DRAW_BUFFERS, GL_NONE); auto& stateDrawBuffers = stateFBOObject->GetDrawBuffers(); for (GLint i = 0; i < MG_State::GLState::FramebufferObject::MAX_DRAW_BUFFERS; ++i) { if (stateDrawBuffers[i] == FramebufferAttachmentType::None) { - m_frontendBuffers[i] = GL_NONE; + m_frontendDrawBuffers[i] = FramebufferAttachmentType::None; continue; } - m_frontendBuffers[i] = MG_Util::ConvertFramebufferAttachmentTypeToGLEnum(stateDrawBuffers[i]); + m_frontendDrawBuffers[i] = stateDrawBuffers[i]; // Create compacted mapping - m_backendBuffers[nBuffers] = GL_COLOR_ATTACHMENT0 + nBuffers; - m_compactedFrontendBuffers[nBuffers] = m_frontendBuffers[i]; + m_backendDrawBuffers[nBuffers] = GL_COLOR_ATTACHMENT0 + nBuffers; + m_compactedFrontendDrawBuffers[nBuffers] = m_frontendDrawBuffers[i]; nBuffers++; } - MG_External::GLES::glDrawBuffers(nBuffers, m_backendBuffers); + MG_External::GLES::glDrawBuffers(nBuffers, m_backendDrawBuffers); // Attach textures for compacted draw buffers - for (int i = 0; i < nBuffers; ++i) { - FramebufferAttachmentType frontendAttachmentType = - MG_Util::ConvertGLEnumToFramebufferAttachmentType(m_compactedFrontendBuffers[i]); - GLenum backendAttachment = m_backendBuffers[i]; - const auto& attachment = attachments[static_cast(frontendAttachmentType)]; - if (!attachment.IsTexture()) continue; - const auto& textureObject = attachment.GetTexture(); - const auto& backendTextureIt = TextureImpl::g_backendTextureObjects.find(textureObject); - if (backendTextureIt == TextureImpl::g_backendTextureObjects.end()) continue; - const auto& backendTextureObject = backendTextureIt->second; - auto glTextureTarget = MG_Util::ConvertTextureTargetToGLEnum(textureObject->GetTarget()); - backendTextureObject->Bind(glTextureTarget); - MG_External::GLES::glFramebufferTexture2D(glFBOTarget, backendAttachment, glTextureTarget, - backendTextureObject->GetBackendTextureId(), - static_cast(attachment.GetTextureLevel())); - } +// for (int i = 0; i < nBuffers; ++i) { +// FramebufferAttachmentType frontendAttachmentType = +// MG_Util::ConvertGLEnumToFramebufferAttachmentType(m_compactedFrontendBuffers[i]); +// GLenum backendAttachment = m_backendBuffers[i]; +// const auto& attachment = attachments[static_cast(frontendAttachmentType)]; +// if (!attachment.IsTexture()) continue; +// const auto& textureObject = attachment.GetTexture(); +// const auto& backendTextureIt = TextureImpl::g_backendTextureObjects.find(textureObject); +// if (backendTextureIt == TextureImpl::g_backendTextureObjects.end()) continue; +// const auto& backendTextureObject = backendTextureIt->second; +// auto glTextureTarget = MG_Util::ConvertTextureTargetToGLEnum(textureObject->GetTarget()); +// backendTextureObject->Bind(glTextureTarget); +// MG_External::GLES::glFramebufferTexture2D(glFBOTarget, backendAttachment, glTextureTarget, +// backendTextureObject->GetBackendTextureId(), +// static_cast(attachment.GetTextureLevel())); +// } stateFBOObject->ClearDrawBuffersDirtyState(); } // Handle read buffer for READ_FRAMEBUFFER else if (asTarget == FramebufferTarget::Read) { - FramebufferAttachmentType readBufferType = stateFBOObject->GetReadBuffer(); - GLenum frontendAtt = MG_Util::ConvertFramebufferAttachmentTypeToGLEnum(readBufferType); + m_frontendReadBuffer = stateFBOObject->GetReadBuffer(); + GLenum frontendAtt = MG_Util::ConvertFramebufferAttachmentTypeToGLEnum(m_frontendReadBuffer); GLenum backendAtt = GL_NONE; // Find corresponding backend attachment in compacted draw buffers - for (SizeT i = 0; i < MG_State::GLState::FramebufferObject::MAX_DRAW_BUFFERS; ++i) { - if (m_compactedFrontendBuffers[i] == frontendAtt) { - backendAtt = m_backendBuffers[i]; - break; +// for (SizeT i = 0; i < MG_State::GLState::FramebufferObject::MAX_DRAW_BUFFERS; ++i) { +// if (m_compactedFrontendBuffers[i] == frontendAtt) { +// backendAtt = m_backendBuffers[i]; +// break; +// } +// } +// if (backendAtt != GL_NONE) { +// MG_External::GLES::glReadBuffer(backendAtt); +// } else { + const auto& readAttachment = attachments[(SizeT)m_frontendReadBuffer]; + GLenum glAttachment = MG_Util::ConvertFramebufferAttachmentTypeToGLEnum(m_frontendReadBuffer); + if (!readAttachment.IsValid() || readAttachment.IsEmpty()) { + return; + } + if (readAttachment.IsTexture()) { + const auto& textureObject = readAttachment.GetTexture(); + const auto& backendTextureIt = TextureImpl::g_backendTextureObjects.find(textureObject); + if (backendTextureIt == TextureImpl::g_backendTextureObjects.end()) { + MGLOG_E("ReadBuffer: No backend texture found for FBO attachment, cannot bind texture."); + return; } + const auto& backendTextureObject = backendTextureIt->second; + auto glTextureTarget = MG_Util::ConvertTextureTargetToGLEnum(textureObject->GetTarget()); + backendTextureObject->Bind(glTextureTarget); + MG_External::GLES::glFramebufferTexture2D(glFBOTarget, glAttachment, glTextureTarget, + backendTextureObject->GetBackendTextureId(), + static_cast(readAttachment.GetTextureLevel())); + } else if (readAttachment.IsRenderbuffer()) { + // TODO: renderbuffer support } - if (backendAtt != GL_NONE) { - MG_External::GLES::glReadBuffer(backendAtt); - } else { - MG_External::GLES::glReadBuffer(frontendAtt); - } + MG_External::GLES::glReadBuffer(glAttachment); +// } } } diff --git a/MobileGL/MG_Backend/DirectGLES/Managers.h b/MobileGL/MG_Backend/DirectGLES/Managers.h index 7330603e..f05994d9 100644 --- a/MobileGL/MG_Backend/DirectGLES/Managers.h +++ b/MobileGL/MG_Backend/DirectGLES/Managers.h @@ -99,19 +99,20 @@ namespace MobileGL::MG_Backend::DirectGLES { Probably useful to re-link shader output according to this. aka. realizing `glBindFragDataLocation` */ - GLenum m_frontendBuffers[MG_State::GLState::FramebufferObject::MAX_DRAW_BUFFERS] = {GL_NONE}; + FramebufferAttachmentType m_frontendDrawBuffers[MG_State::GLState::FramebufferObject::MAX_DRAW_BUFFERS] = {FramebufferAttachmentType::None}; /* this will save buffers in its compacted GL form, not consecutive is not allowed i.e. it could be like [COLOR_ATTACHMENT0, COLOR_ATTACHMENT5, COLOR_ATTACHMENT4] (no GL_NONE among those) */ - GLenum m_compactedFrontendBuffers[MG_State::GLState::FramebufferObject::MAX_DRAW_BUFFERS] = {GL_NONE}; + FramebufferAttachmentType m_compactedFrontendDrawBuffers[MG_State::GLState::FramebufferObject::MAX_DRAW_BUFFERS] = {FramebufferAttachmentType::None}; /* this will save buffers in stricter ES rules reversion, absence or not consecutive are not allowed, according to ES spec i.e. it could be like [COLOR_ATTACHMENT0, COLOR_ATTACHMENT1, NONE, NONE, ...] this array could be provided as data directly to ES `glDrawBuffers` function */ - GLenum m_backendBuffers[MG_State::GLState::FramebufferObject::MAX_DRAW_BUFFERS] = {GL_NONE}; + GLenum m_backendDrawBuffers[MG_State::GLState::FramebufferObject::MAX_DRAW_BUFFERS] = {GL_NONE}; + FramebufferAttachmentType m_frontendReadBuffer = FramebufferAttachmentType::Color0; }; extern UnorderedMap, SharedPtr>