[Feat] (MG_Util/Texture, MG_Backend/DirectGLES): implementing BGRA texture readback (WIP)

This commit is contained in:
2026-02-07 23:10:43 +08:00
parent 40333fa84a
commit b90df42a45
3 changed files with 77 additions and 23 deletions
+42 -8
View File
@@ -25,6 +25,7 @@
#include <MG_Util/Converters/MGToGL/TextureEnumConverter.h>
#include <MG_Util/Converters/MGToStr/TextureEnumConverter.h>
#include <MG_Util/Converters/MGToGL/RenderStateEnumConverter.h>
#include <MG_Util/Texture/PixelStoreProcessor.h>
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");
}
@@ -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<TextureSwizzleParam>& swizzle) {
void ProcessColorSwizzle(void* data, SizeT pixelCount, const Vector<TextureSwizzleParam>& swizzle) {
const auto bpp = swizzle.size();
Uint8* bytes = static_cast<Uint8*>(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<SizeT>(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<SizeT>(outputRowStride) * static_cast<SizeT>(effectiveHeight) * static_cast<SizeT>(depth);
void* outputPixels = malloc(outSize);
@@ -201,12 +204,22 @@ namespace MobileGL::MG_Util::PixelStoreProcessor {
const Uint8* src = static_cast<const Uint8*>(inputPixels);
Uint8* dst = static_cast<Uint8*>(outputPixels);
dst += static_cast<SizeT>(params.SkipImages) * static_cast<SizeT>(effectiveHeight) * outputRowStride;
dst += static_cast<SizeT>(params.SkipRows) * outputRowStride;
dst += static_cast<SizeT>(params.SkipPixels) * pixelSize;
Vector<Uint8> tempRow(static_cast<SizeT>(width) * pixelSize);
bool needSwizzle = false;
static Vector<TextureSwizzleParam> 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<SizeT>(width), 1);
}
if (needSwizzle) {
ProcessColorSwizzle(tempRow.data(), static_cast<SizeT>(width), swizzle);
}
Memcpy(layerDst, tempRow.data(), static_cast<SizeT>(width) * pixelSize);
layerSrc += inputRowStride;
@@ -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<TextureSwizzleParam>& swizzle);
} // namespace MobileGL::MG_Util::PixelStoreProcessor