From 39182f3a5e966407f855ed90858b33f6d59cba0a Mon Sep 17 00:00:00 2001 From: Swung0x48 Date: Thu, 8 Jan 2026 14:07:51 +0800 Subject: [PATCH] [Fix] (MG_Backend/DirectGLES): fix bad texture binding in `CopyTexImage2D` --- MobileGL/MG_Backend/DirectGLES/DirectGLES.cpp | 47 ++++++++++++++++++- .../MG_Impl/GLImpl/Texture/GL_Texture.cpp | 3 ++ 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/MobileGL/MG_Backend/DirectGLES/DirectGLES.cpp b/MobileGL/MG_Backend/DirectGLES/DirectGLES.cpp index 916f412a..5f10fa2a 100644 --- a/MobileGL/MG_Backend/DirectGLES/DirectGLES.cpp +++ b/MobileGL/MG_Backend/DirectGLES/DirectGLES.cpp @@ -784,6 +784,46 @@ namespace MobileGL::MG_Backend::DirectGLES { MGLOG_D("ES error (%s:%d): %s", file, line, MG_Util::ConvertGLEnumToString(err).c_str()); }); + { +#ifdef TRACY_ENABLE + ZoneScopedNC("BindCurrentTexturesAtTarget", TRACY_ZONECOLOR_BACKEND); +#endif + auto unit = MG_State::pGLContext->GetActiveTextureUnit(); + auto& textureUnit = MG_State::pGLContext->GetTextureUnitObject(unit); + + MG_External::GLES::glActiveTexture(GL_TEXTURE0 + unit); + auto textureTarget = MG_Util::ConvertGLEnumToTextureTarget(target); + if (textureTarget == TextureTarget::Texture1D || + textureTarget == TextureTarget::TextureRectangle || + textureTarget == TextureTarget::Texture2DMultisampleArray || + textureTarget == TextureTarget::Texture1DArray || + textureTarget == TextureTarget::Texture2DMultisample || + textureTarget == TextureTarget::Texture2DArray) { + MOBILEGL_ASSERT(false, " Texture target %s is not supported, skipping.", + MG_Util::ConvertTextureTargetToString(textureTarget).c_str()); + return; + } + + const auto& bindingSlot = textureUnit.GetBindingSlot(textureTarget); + { + const auto &textureObject = bindingSlot.GetBoundObject(); + if (!textureObject) { + MGLOG_W("%s: Texture target %s does not have texture bound.", __func__, + MG_Util::ConvertTextureTargetToString(textureTarget).c_str()); + } + + const auto &backendTextureIt = TextureImpl::g_backendTextureObjects.find( + textureObject); + if (backendTextureIt == TextureImpl::g_backendTextureObjects.end()) { + MGLOG_W("%s: No backend texture object found for frontend texture object %d.", __func__, + textureObject->GetExternalIndex()); + return; + } + + backendTextureIt->second->Bind(target); + } + } + // GLint realInternalFormat; // MG_External::GLES::glGetTexLevelParameteriv(target, level, GL_TEXTURE_INTERNAL_FORMAT, &realInternalFormat); // errorLopper.Loop([file = __FILE__, line = __LINE__](auto err) { @@ -794,7 +834,12 @@ namespace MobileGL::MG_Backend::DirectGLES { GLenum format = GL_DEPTH_COMPONENT; GLenum type = GL_UNSIGNED_INT; - TextureImpl::GenerateTextureFormatInfo(mglInternalFormat, &internalformat, &type, &format); + TextureImpl::GenerateTextureFormatInfo(mglInternalFormat, &internalformat, &format, &type); + MOBILEGL_ASSERT(format != GL_NONE && type != GL_NONE, "%s: cannot GenerateTextureFormatInfo(%s): out internalformat=%s, format=%s, type=%s", + MG_Util::ConvertTextureInternalFormatToString(mglInternalFormat).c_str(), + MG_Util::ConvertGLEnumToString(internalformat).c_str(), + MG_Util::ConvertGLEnumToString(format).c_str(), + MG_Util::ConvertGLEnumToString(type).c_str()); TexturePixelDataType texturePixelDataType = MG_Util::ConvertGLEnumToTexturePixelDataType(type); bool isDepthFormat = diff --git a/MobileGL/MG_Impl/GLImpl/Texture/GL_Texture.cpp b/MobileGL/MG_Impl/GLImpl/Texture/GL_Texture.cpp index 2fe6833e..7f2ecbce 100644 --- a/MobileGL/MG_Impl/GLImpl/Texture/GL_Texture.cpp +++ b/MobileGL/MG_Impl/GLImpl/Texture/GL_Texture.cpp @@ -679,6 +679,9 @@ namespace MobileGL { const SizeT internalBpp = MG_Util::GetInternalBytesPerPixel(textureInternalFormat, texturePixelDataType); const SizeT internalBytes = width * height * internalBpp; + MGLOG_D("%s: texture object had internal format %s, new format %s", __func__, + MG_Util::ConvertTextureInternalFormatToString(textureObject->GetFormat()).c_str(), + MG_Util::ConvertTextureInternalFormatToString(textureInternalFormat).c_str()); textureObject->SetInternalFormat(textureInternalFormat); // if isProxy, no more pixel transfer needed below