diff --git a/MobileGL/MG_Backend/DirectGLES/DirectGLES.cpp b/MobileGL/MG_Backend/DirectGLES/DirectGLES.cpp index 2b1cc32f..9381ee4f 100644 --- a/MobileGL/MG_Backend/DirectGLES/DirectGLES.cpp +++ b/MobileGL/MG_Backend/DirectGLES/DirectGLES.cpp @@ -599,15 +599,15 @@ namespace MobileGL::MG_Backend::DirectGLES { auto textureObject = bindingSlot.GetBoundObject(); - const SizeT bytesPerPixel = MG_Util::GetInputBytesPerPixel(mglInternalFormat, texturePixelDataType); - const SizeT totalBytes = width * height * bytesPerPixel; +// const SizeT bytesPerPixel = MG_Util::GetInputBytesPerPixel(mglInternalFormat, texturePixelDataType); +// const SizeT totalBytes = width * height * bytesPerPixel; // MG_State::GLState::MipmapLevelInput mipmap = // MG_State::GLState::MipmapLevelInput({width, height, 1}, level, false, 0, // {nullptr, totalBytes}); textureObject->SetInternalFormat(mglInternalFormat); - textureObject->AllocateStorage(TextureUploadTarget::Texture2D, level, {{width, height, 1}, totalBytes}); + textureObject->AllocateStorage(TextureUploadTarget::Texture2D, level, {{width, height, 1}, 0}); } void CopyTexSubImage2D(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint x, GLint y, diff --git a/MobileGL/MG_Backend/DirectGLES/Managers.cpp b/MobileGL/MG_Backend/DirectGLES/Managers.cpp index 0f62bb2d..f300aa01 100644 --- a/MobileGL/MG_Backend/DirectGLES/Managers.cpp +++ b/MobileGL/MG_Backend/DirectGLES/Managers.cpp @@ -5,6 +5,7 @@ #include "DirectGLES.h" #include #include +#include #include #include #include @@ -282,6 +283,11 @@ namespace MobileGL::MG_Backend::DirectGLES { Bool needsRegeneration = !m_isInitialized || (currentTextureInfo != m_prevTextureInfo); + MGLOG_D("%s: Got texture info: %dx%dx%d, mips %d, format %s", __func__, + baseSize.x(), baseSize.y(), baseSize.z(), + mipmapCount, + MG_Util::ConvertTextureInternalFormatToString(stateTextureObject->GetFormat()).c_str()); + if (needsRegeneration) { MGLOG_D("Texture state changed significantly or not initialized, regenerating texture with ID: %u", m_backendTextureId); @@ -294,6 +300,13 @@ namespace MobileGL::MG_Backend::DirectGLES { // TODO: deal with multiple upload target texture auto levelTexelSize = stateTextureObject->GetMipmapTexelSize(TextureUploadTarget::Texture2D, level); auto levelByteSize = stateTextureObject->GetMipmapByteSize(TextureUploadTarget::Texture2D, level); + bool levelDirty = stateTextureObject->IsStorageDirty(TextureUploadTarget::Texture2D, 0); + auto* pData = (levelDirty && levelByteSize != 0) ? stateTextureObject->MapMipmapData(TextureUploadTarget::Texture2D, level) : nullptr; + MGLOG_D("%s: syncing mip %d: %dx%dx%d, byteSize = %d, pData = %p", __func__, + level, + levelTexelSize.x(), levelTexelSize.y(), levelTexelSize.z(), + levelByteSize, + pData); BufferImpl::BackendBufferBindingProtector pixelUnpackProtector = BufferImpl::BackendBufferBindingProtector(GL_PIXEL_UNPACK_BUFFER); errorLopper.Clear(); @@ -301,7 +314,7 @@ namespace MobileGL::MG_Backend::DirectGLES { MG_External::GLES::glTexImage2D(GL_TEXTURE_2D, static_cast(level), glInternalFormat, static_cast(levelTexelSize.x()), static_cast(levelTexelSize.y()), 0, glFormat, glType, - (levelByteSize != 0) ? stateTextureObject->MapMipmapData(TextureUploadTarget::Texture2D, level) : nullptr); + pData); // errorLopper.Loop([index = stateTextureObject->GetExternalIndex(), &mipmap, level, glInternalFormat, glFormat, glType, file = __FILE__, line = __LINE__, func = __func__](GLenum err) { // MGLOG_D("%s(%s:%d) ES error: %s, texobj %d, mip %d (%dx%d, %s, %s, %s)", func, file, line, MG_Util::ConvertGLEnumToString(err).c_str(), diff --git a/MobileGL/MG_Impl/GLImpl/Texture/GL_Texture.cpp b/MobileGL/MG_Impl/GLImpl/Texture/GL_Texture.cpp index a6d19e2f..f178312c 100644 --- a/MobileGL/MG_Impl/GLImpl/Texture/GL_Texture.cpp +++ b/MobileGL/MG_Impl/GLImpl/Texture/GL_Texture.cpp @@ -382,8 +382,6 @@ namespace MobileGL { const SizeT totalBytes = width * height * bytesPerPixel; textureObject->SetInternalFormat(textureInternalFormat); - // Allocate in TextureObject - textureObject->AllocateStorage(textureUploadingTarget, level, {{width, height, 1}, isProxy ? 0 : totalBytes}); // if isProxy, no more pixel transfer needed below if (isProxy) @@ -401,16 +399,19 @@ namespace MobileGL { reinterpret_cast(pixels); } - void* processedPixels = nullptr; - if (originalPixels) { - processedPixels = MG_Util::PixelStoreProcessor::ProcessTexturePixelsDataUnpack( - originalPixels, MG_State::pGLContext->GetPixelStoreParameters(true), bytesPerPixel, - {width, height, 1}, false, imageSize); - } else { + // Allocate in TextureObject + textureObject->AllocateStorage(textureUploadingTarget, level, {{width, height, 1}, totalBytes}); + + if (!originalPixels) { MGLOG_D("TexImage2D_State: No input pixel and no PBO bound, no pixel transfer"); return; } + void* processedPixels = nullptr; + processedPixels = MG_Util::PixelStoreProcessor::ProcessTexturePixelsDataUnpack( + originalPixels, MG_State::pGLContext->GetPixelStoreParameters(true), bytesPerPixel, + {width, height, 1}, false, imageSize); + if (processedPixels && imageSize > 0) { if (imageSize != totalBytes) { MGLOG_W("TexImage2D_State: Processed pixel data size (%zu) does not match expected size (%zu). " @@ -421,11 +422,8 @@ namespace MobileGL { const SizeT copySize = std::min(imageSize, totalBytes); DataPtr texelInput { processedPixels, copySize }; textureObject->UpdateMipmapSubData(textureUploadingTarget, level, texelInput); - } else if (originalPixels) { - MGLOG_E("TexImage2D_State: Failed to process pixel data, initializing with original data."); - DataPtr texelInput { (void*)originalPixels, totalBytes }; - textureObject->UpdateMipmapSubData(textureUploadingTarget, level, texelInput); } + free(processedPixels); // MG_State::GLState::MipmapLevelInput mipmap = diff --git a/MobileGL/MG_Impl/Init.cpp b/MobileGL/MG_Impl/Init.cpp index b3c27256..1e3821d1 100644 --- a/MobileGL/MG_Impl/Init.cpp +++ b/MobileGL/MG_Impl/Init.cpp @@ -28,7 +28,6 @@ namespace MobileGL { // stencilTex->SetMipmapLevel({{512, 512, 1}, 0, false, 0, {nullptr, 0}}); fbo0->AttachTexture(FramebufferAttachmentType::Color0, colorTex); fbo0->AttachTexture(FramebufferAttachmentType::Depth, depthTex); - fbo0->AttachTexture(FramebufferAttachmentType::Depth, depthTex); fbo0->AttachTexture(FramebufferAttachmentType::Stencil, stencilTex); GLImpl::FramebufferImpl::pDefaultFramebufferInfo = new GLImpl::FramebufferImpl::DefaultFramebufferInfo(fbo0, colorTex, depthTex, stencilTex); diff --git a/MobileGL/MG_State/GLState/TextureState/TextureStorage.h b/MobileGL/MG_State/GLState/TextureState/TextureStorage.h index 62df6e48..13f21b62 100644 --- a/MobileGL/MG_State/GLState/TextureState/TextureStorage.h +++ b/MobileGL/MG_State/GLState/TextureState/TextureStorage.h @@ -29,7 +29,7 @@ namespace MobileGL { targetTexelSizes.resize(level + 1); targetTexelSizes[level] = input.texelSize; auto& dirtyArr = m_isDirty[targetIndex]; - dirtyArr.resize(level + 1, true); + dirtyArr.resize(level + 1, false); auto& data = targetData[level]; data.resize(input.byteSize, 0); @@ -49,7 +49,7 @@ namespace MobileGL { if (input.data && input.size > 0) { const Uint8* src = static_cast(input.data); Memcpy(levelData.data(), src, input.size); - m_isDirty[targetIndex][level] = false; + m_isDirty[targetIndex][level] = true; } }