Merge branch 'Feat/Backend-Direct-GLES' into dev

This commit is contained in:
BZLZHH
2026-02-16 18:32:52 +08:00
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/MGToGL/TextureEnumConverter.h>
#include <MG_Util/Converters/MGToStr/TextureEnumConverter.h> #include <MG_Util/Converters/MGToStr/TextureEnumConverter.h>
#include <MG_Util/Converters/MGToGL/RenderStateEnumConverter.h> #include <MG_Util/Converters/MGToGL/RenderStateEnumConverter.h>
#include <MG_Util/Texture/PixelStoreProcessor.h>
namespace MobileGL::MG_Backend::DirectGLES { namespace MobileGL::MG_Backend::DirectGLES {
enum class DrawSyncBit : Uint32 { 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) { 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", 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(target).c_str(), level, MG_Util::ConvertGLEnumToString(format).c_str(),
MG_Util::ConvertGLEnumToString(type).c_str(), pixels); MG_Util::ConvertGLEnumToString(type).c_str(), pixels);
MOBILEGL_ASSERT(format == GL_RGBA || format == GL_RGBA_INTEGER, MOBILEGL_ASSERT(format == GL_RGBA || format == GL_RGBA_INTEGER || format == GL_BGRA,
"Only GL_RGBA and GL_RGBA_INTEGER are supported currently, while requested %s.", "Only GL_RGBA, GL_RGBA_INTEGER and GL_BGRA are supported currently, while requested %s.",
MG_Util::ConvertGLEnumToString(format).c_str()); 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 || 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, " "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()); 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()"); MGLOG_D("GetTexImage: SyncNeccessaryTextures()");
TextureImpl::SyncNeccessaryTextures(); TextureImpl::SyncNeccessaryTextures();
@@ -1380,14 +1390,21 @@ namespace MobileGL::MG_Backend::DirectGLES {
return; 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)"); MGLOG_D("GetTexImage: Applying TempPixelStoreParameterSync (PACK)");
TempPixelStoreParameterSync tempPackParamsSync(false); 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(); const auto& storageType = textureObject->GetStorageType();
MGLOG_D("GetTexImage: texture storage type = %d", (int)storageType); MGLOG_D("GetTexImage: texture storage type = %d", (int)storageType);
if (storageType == TextureStorageType::Buffer) { if (storageType == TextureStorageType::Buffer) {
MGLOG_E("GetTexImage: Texture storage type Buffer is not supported.");
MGLOG_E("GetTexImage: Texture storage type Buffer is not supported."); MGLOG_E("GetTexImage: Texture storage type Buffer is not supported.");
return; return;
} }
@@ -1432,8 +1449,17 @@ namespace MobileGL::MG_Backend::DirectGLES {
usePBO = false; usePBO = false;
MGLOG_D("GetTexImage: Not using PBO"); 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) { if (usePBO) {
// pull back to client memory if PBO is used // pull back to client memory if PBO is used
MGLOG_D("ReadPixels: PBO used, mapping buffer to client memory"); 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); MG_External::GLES::glUnmapBuffer(GL_PIXEL_PACK_BUFFER);
} else { } else {
MGLOG_E("ReadPixels: glMapBufferRange returned nullptr"); MGLOG_E("ReadPixels: glMapBufferRange returned nullptr");
MGLOG_E("ReadPixels: glMapBufferRange returned nullptr");
} }
MGLOG_D("ReadPixels: Restoring previous pixel pack buffer binding %u", prevPixelPackBuffer); MGLOG_D("ReadPixels: Restoring previous pixel pack buffer binding %u", prevPixelPackBuffer);
MG_External::GLES::glBindBuffer(GL_PIXEL_PACK_BUFFER, 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"); MGLOG_D("GetTexImage: finished");
} }
@@ -72,7 +72,7 @@ namespace MobileGL::MG_Util::PixelStoreProcessor {
// assume 8 bit per channel // assume 8 bit per channel
// swizzle.size() == channel count // 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(); const auto bpp = swizzle.size();
Uint8* bytes = static_cast<Uint8*>(data); Uint8* bytes = static_cast<Uint8*>(data);
Uint8 pixelScratch[4]; Uint8 pixelScratch[4];
@@ -172,22 +172,25 @@ namespace MobileGL::MG_Util::PixelStoreProcessor {
return outputPixels; 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) { IntVec3 dimension, Bool isBitmap, SizeT& outSize) {
if (pixelSize == 0) { const SizeT pixelSize = MG_Util::GetInternalBytesPerPixel(srcInternalFormat, srcDataType);
outSize = 0;
return nullptr;
}
Int width = dimension.x(); Int width = dimension.x();
Int height = dimension.y(); Int height = dimension.y();
Int depth = dimension.z(); Int depth = dimension.z();
const Int outputWidth = (params.RowLength > 0) ? params.RowLength : width; if (pixelSize == 0) {
const SizeT outputRowStride = CalculateRowStride(outputWidth, pixelSize, params.Alignment); outSize = 0;
const SizeT inputRowStride = static_cast<SizeT>(width) * pixelSize; 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); outSize = static_cast<SizeT>(outputRowStride) * static_cast<SizeT>(effectiveHeight) * static_cast<SizeT>(depth);
void* outputPixels = malloc(outSize); void* outputPixels = malloc(outSize);
@@ -201,12 +204,22 @@ namespace MobileGL::MG_Util::PixelStoreProcessor {
const Uint8* src = static_cast<const Uint8*>(inputPixels); const Uint8* src = static_cast<const Uint8*>(inputPixels);
Uint8* dst = static_cast<Uint8*>(outputPixels); 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); 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) { for (Int z = 0; z < depth; ++z) {
Uint8* layerDst = dst; Uint8* layerDst = dst;
const Uint8* layerSrc = src; const Uint8* layerSrc = src;
@@ -222,6 +235,10 @@ namespace MobileGL::MG_Util::PixelStoreProcessor {
ProcessLSBFirst(tempRow.data(), static_cast<SizeT>(width), 1); 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); Memcpy(layerDst, tempRow.data(), static_cast<SizeT>(width) * pixelSize);
layerSrc += inputRowStride; layerSrc += inputRowStride;
@@ -17,6 +17,9 @@ namespace MobileGL::MG_Util::PixelStoreProcessor {
TextureInternalFormat targetInternalFormat, TextureInternalFormat targetInternalFormat,
TextureInputFormat textureInputFormat, TexturePixelDataType inputDataType, TextureInputFormat textureInputFormat, TexturePixelDataType inputDataType,
IntVec3 dimension, Bool isBitmap, SizeT& outSize); 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); IntVec3 dimension, Bool isBitmap, SizeT& outSize);
void ProcessColorSwizzle(void* data, SizeT pixelCount, const Vector<TextureSwizzleParam>& swizzle);
} // namespace MobileGL::MG_Util::PixelStoreProcessor } // namespace MobileGL::MG_Util::PixelStoreProcessor