diff --git a/MobileGL/MG_Backend/DirectGLES/DirectGLES.cpp b/MobileGL/MG_Backend/DirectGLES/DirectGLES.cpp index 6878eac9..eb26c12b 100644 --- a/MobileGL/MG_Backend/DirectGLES/DirectGLES.cpp +++ b/MobileGL/MG_Backend/DirectGLES/DirectGLES.cpp @@ -25,6 +25,7 @@ #include #include #include +#include namespace MobileGL::MG_Backend::DirectGLES { enum class DrawSyncBit : Uint32 { @@ -1322,19 +1323,28 @@ namespace MobileGL::MG_Backend::DirectGLES { } void GetTexImage(GLenum target, GLint level, GLenum format, GLenum type, void* pixels) { + DebugImpl::ErrorLopper errorLopper; MGLOG_D("GetTexImage: target=%s level=%d format=%s type=%s pixels=%p", MG_Util::ConvertGLEnumToString(target).c_str(), level, MG_Util::ConvertGLEnumToString(format).c_str(), MG_Util::ConvertGLEnumToString(type).c_str(), pixels); - MOBILEGL_ASSERT(format == GL_RGBA || format == GL_RGBA_INTEGER, - "Only GL_RGBA and GL_RGBA_INTEGER are supported currently, while requested %s.", + MOBILEGL_ASSERT(format == GL_RGBA || format == GL_RGBA_INTEGER || format == GL_BGRA, + "Only GL_RGBA, GL_RGBA_INTEGER and GL_BGRA are supported currently, while requested %s.", MG_Util::ConvertGLEnumToString(format).c_str()); MOBILEGL_ASSERT(type == GL_UNSIGNED_BYTE || type == GL_UNSIGNED_INT || type == GL_UNSIGNED_INT_2_10_10_10_REV || - type == GL_INT || type == GL_FLOAT, + type == GL_INT || type == GL_FLOAT || + type == GL_UNSIGNED_INT_8_8_8_8 || type == GL_UNSIGNED_INT_8_8_8_8_REV, "Only GL_UNSIGNED_BYTE, GL_UNSIGNED_INT, GL_UNSIGNED_INT_2_10_10_10_REV, " - "GL_INT and GL_FLOAT are supported currently, while requested %s.", + "GL_INT, GL_FLOAT, GL_UNSIGNED_INT_8_8_8_8 and GL_UNSIGNED_INT_8_8_8_8_REV " + "are supported currently, while requested %s.", MG_Util::ConvertGLEnumToString(type).c_str()); + GLenum esFormat = format, esType = type; + if (esFormat == GL_BGRA) + esFormat = GL_RGBA; + if (esType == GL_UNSIGNED_INT_8_8_8_8 || esType == GL_UNSIGNED_INT_8_8_8_8_REV) + esType = GL_UNSIGNED_BYTE; + MGLOG_D("GetTexImage: SyncNeccessaryTextures()"); TextureImpl::SyncNeccessaryTextures(); @@ -1380,14 +1390,21 @@ namespace MobileGL::MG_Backend::DirectGLES { return; } + errorLopper.Loop([file = __FILE__, line = __LINE__](auto err) { + MGLOG_D("ES error (%s:%d): %s", file, line, MG_Util::ConvertGLEnumToString(err).c_str()); + }); + MGLOG_D("GetTexImage: Applying TempPixelStoreParameterSync (PACK)"); TempPixelStoreParameterSync tempPackParamsSync(false); + errorLopper.Loop([file = __FILE__, line = __LINE__](auto err) { + MGLOG_D("ES error (%s:%d): %s", file, line, MG_Util::ConvertGLEnumToString(err).c_str()); + }); + const auto& storageType = textureObject->GetStorageType(); MGLOG_D("GetTexImage: texture storage type = %d", (int)storageType); if (storageType == TextureStorageType::Buffer) { - MGLOG_E("GetTexImage: Texture storage type Buffer is not supported."); MGLOG_E("GetTexImage: Texture storage type Buffer is not supported."); return; } @@ -1432,8 +1449,17 @@ namespace MobileGL::MG_Backend::DirectGLES { usePBO = false; MGLOG_D("GetTexImage: Not using PBO"); } - MGLOG_D("GetTexImage: glReadPixels()"); - MG_External::GLES::glReadPixels(0, 0, size.x(), size.y(), format, type, pixels); + + errorLopper.Loop([file = __FILE__, line = __LINE__](auto err) { + MGLOG_D("ES error (%s:%d): %s", file, line, MG_Util::ConvertGLEnumToString(err).c_str()); + }); + MGLOG_D("GetTexImage: glReadPixels(0, 0, %d, %d, %s, %s, %p)", size.x(), size.y(), + MG_Util::ConvertGLEnumToString(esFormat).c_str(), MG_Util::ConvertGLEnumToString(esType).c_str(), pixels); + MG_External::GLES::glReadPixels(0, 0, size.x(), size.y(), esFormat, esType, pixels); + + errorLopper.Loop([file = __FILE__, line = __LINE__](auto err) { + MGLOG_D("ES error (%s:%d): %s", file, line, MG_Util::ConvertGLEnumToString(err).c_str()); + }); if (usePBO) { // pull back to client memory if PBO is used MGLOG_D("ReadPixels: PBO used, mapping buffer to client memory"); @@ -1448,12 +1474,20 @@ namespace MobileGL::MG_Backend::DirectGLES { MG_External::GLES::glUnmapBuffer(GL_PIXEL_PACK_BUFFER); } else { MGLOG_E("ReadPixels: glMapBufferRange returned nullptr"); - MGLOG_E("ReadPixels: glMapBufferRange returned nullptr"); } MGLOG_D("ReadPixels: Restoring previous pixel pack buffer binding %u", prevPixelPackBuffer); MG_External::GLES::glBindBuffer(GL_PIXEL_PACK_BUFFER, prevPixelPackBuffer); + } else { + if (esFormat == GL_RGBA && format == GL_BGRA && esType == GL_UNSIGNED_BYTE && type == GL_UNSIGNED_INT_8_8_8_8_REV) { + MGLOG_D("ReadPixels: ProcessColorSwizzle BGRA (not implemented)"); + + } } + + errorLopper.Loop([file = __FILE__, line = __LINE__](auto err) { + MGLOG_D("ES error (%s:%d): %s", file, line, MG_Util::ConvertGLEnumToString(err).c_str()); + }); MGLOG_D("GetTexImage: finished"); } diff --git a/MobileGL/MG_Util/Texture/PixelStoreProcessor.cpp b/MobileGL/MG_Util/Texture/PixelStoreProcessor.cpp index 6092914f..74c532fe 100644 --- a/MobileGL/MG_Util/Texture/PixelStoreProcessor.cpp +++ b/MobileGL/MG_Util/Texture/PixelStoreProcessor.cpp @@ -72,7 +72,7 @@ namespace MobileGL::MG_Util::PixelStoreProcessor { // assume 8 bit per channel // swizzle.size() == channel count - static void ProcessColorSwizzle(void* data, SizeT pixelCount, const Vector& swizzle) { + void ProcessColorSwizzle(void* data, SizeT pixelCount, const Vector& swizzle) { const auto bpp = swizzle.size(); Uint8* bytes = static_cast(data); Uint8 pixelScratch[4]; @@ -172,22 +172,25 @@ namespace MobileGL::MG_Util::PixelStoreProcessor { return outputPixels; } - void* ProcessTexturePixelsDataPack(const void* inputPixels, const PixelStoreParameters& params, SizeT pixelSize, + void* ProcessTexturePixelsDataPack(const void* inputPixels, const PixelStoreParameters& params, + TextureInternalFormat srcInternalFormat, TexturePixelDataType srcDataType, + TextureInputFormat dstInputFormat, TexturePixelDataType dstDataType, IntVec3 dimension, Bool isBitmap, SizeT& outSize) { - if (pixelSize == 0) { - outSize = 0; - return nullptr; - } + const SizeT pixelSize = MG_Util::GetInternalBytesPerPixel(srcInternalFormat, srcDataType); Int width = dimension.x(); Int height = dimension.y(); Int depth = dimension.z(); - const Int outputWidth = (params.RowLength > 0) ? params.RowLength : width; - const SizeT outputRowStride = CalculateRowStride(outputWidth, pixelSize, params.Alignment); - const SizeT inputRowStride = static_cast(width) * pixelSize; + if (pixelSize == 0) { + outSize = 0; + return nullptr; + } - const Int effectiveHeight = (params.ImageHeight > 0) ? params.ImageHeight : height; + // TODO: take care of PixelStoreParameters + const SizeT outputRowStride = width * pixelSize; + const Int effectiveHeight = height; + const SizeT inputRowStride = outputRowStride; outSize = static_cast(outputRowStride) * static_cast(effectiveHeight) * static_cast(depth); void* outputPixels = malloc(outSize); @@ -201,12 +204,22 @@ namespace MobileGL::MG_Util::PixelStoreProcessor { const Uint8* src = static_cast(inputPixels); Uint8* dst = static_cast(outputPixels); - dst += static_cast(params.SkipImages) * static_cast(effectiveHeight) * outputRowStride; - dst += static_cast(params.SkipRows) * outputRowStride; - dst += static_cast(params.SkipPixels) * pixelSize; - Vector tempRow(static_cast(width) * pixelSize); + bool needSwizzle = false; + static Vector swizzle; + swizzle = { TextureSwizzleParam::Red, TextureSwizzleParam::Green, + TextureSwizzleParam::Blue, TextureSwizzleParam::Alpha }; + if (srcInternalFormat == TextureInternalFormat::RGBA && dstInputFormat == TextureInputFormat::BGRA) { + swizzle = { TextureSwizzleParam::Blue, TextureSwizzleParam::Green, + TextureSwizzleParam::Red, TextureSwizzleParam::Alpha }; + needSwizzle = true; + } + if (dstDataType == TexturePixelDataType::UnsignedInt8888) { + std::reverse(swizzle.begin(), swizzle.end()); + needSwizzle = true; + } + for (Int z = 0; z < depth; ++z) { Uint8* layerDst = dst; const Uint8* layerSrc = src; @@ -222,6 +235,10 @@ namespace MobileGL::MG_Util::PixelStoreProcessor { ProcessLSBFirst(tempRow.data(), static_cast(width), 1); } + if (needSwizzle) { + ProcessColorSwizzle(tempRow.data(), static_cast(width), swizzle); + } + Memcpy(layerDst, tempRow.data(), static_cast(width) * pixelSize); layerSrc += inputRowStride; diff --git a/MobileGL/MG_Util/Texture/PixelStoreProcessor.h b/MobileGL/MG_Util/Texture/PixelStoreProcessor.h index d5fcbca2..32f4ba0d 100644 --- a/MobileGL/MG_Util/Texture/PixelStoreProcessor.h +++ b/MobileGL/MG_Util/Texture/PixelStoreProcessor.h @@ -17,6 +17,9 @@ namespace MobileGL::MG_Util::PixelStoreProcessor { TextureInternalFormat targetInternalFormat, TextureInputFormat textureInputFormat, TexturePixelDataType inputDataType, IntVec3 dimension, Bool isBitmap, SizeT& outSize); - void* ProcessTexturePixelsDataPack(const void* inputPixels, const PixelStoreParameters& params, SizeT pixelSize, + void* ProcessTexturePixelsDataPack(const void* inputPixels, const PixelStoreParameters& params, + TextureInternalFormat srcInternalFormat, TexturePixelDataType srcDataType, + TextureInputFormat dstInputFormat, TexturePixelDataType dstDataType, IntVec3 dimension, Bool isBitmap, SizeT& outSize); + void ProcessColorSwizzle(void* data, SizeT pixelCount, const Vector& swizzle); } // namespace MobileGL::MG_Util::PixelStoreProcessor