[Chore] (MG_Util/Texture): rearrange formats in NormalizePixelFormat (cont.)

This commit is contained in:
2026-01-06 22:48:32 +08:00
parent 4b8e558565
commit a601307d6f
@@ -14,326 +14,157 @@ namespace MobileGL::MG_Util::TextureFormatProcessor {
#ifdef TRACY_ENABLE
ZoneScopedC(TRACY_ZONECOLOR_BACKEND);
#endif
switch (internalFormat) {
case GL_DEPTH_COMPONENT16:
if (outInternalFormat) *outInternalFormat = internalFormat;
if (outType) *outType = GL_UNSIGNED_SHORT;
if (outFormat) *outFormat = GL_DEPTH_COMPONENT;
break;
case GL_DEPTH_COMPONENT24:
if (outInternalFormat) *outInternalFormat = internalFormat;
if (outType) *outType = GL_UNSIGNED_INT;
if (outFormat) *outFormat = GL_DEPTH_COMPONENT;
break;
case GL_DEPTH_COMPONENT32:
if (outInternalFormat) *outInternalFormat = GL_DEPTH_COMPONENT;
if (outType) *outType = GL_UNSIGNED_INT;
if (outFormat) *outFormat = GL_DEPTH_COMPONENT;
break;
case GL_DEPTH_COMPONENT32F:
if (outInternalFormat) *outInternalFormat = internalFormat;
if (outType) *outType = GL_FLOAT;
if (outFormat) *outFormat = GL_DEPTH_COMPONENT;
break;
case GL_DEPTH_COMPONENT:
if (outInternalFormat) *outInternalFormat = internalFormat;
if (outType) *outType = GL_UNSIGNED_INT;
if (outFormat) *outFormat = GL_DEPTH_COMPONENT;
break;
case GL_DEPTH32F_STENCIL8:
case GL_DEPTH_STENCIL:
if (outInternalFormat) *outInternalFormat = internalFormat;
if (outType) *outType = GL_FLOAT_32_UNSIGNED_INT_24_8_REV;
if (outFormat) *outFormat = GL_DEPTH_STENCIL;
break;
case GL_SRGB8:
if (outInternalFormat) *outInternalFormat = internalFormat;
if (outType) *outType = GL_UNSIGNED_BYTE;
if (outFormat) *outFormat = GL_RGB;
break;
case GL_RGBA32F:
case GL_RGB32F:
case GL_RG32F:
case GL_R32F:
if (outInternalFormat) *outInternalFormat = internalFormat;
if (outType) *outType = GL_FLOAT;
if (outFormat) switch (internalFormat) {
case GL_RGBA32F:
if (outFormat) *outFormat = GL_RGBA;
break;
case GL_RGB32F:
if (outFormat) *outFormat = GL_RGB;
break;
case GL_RG32F:
if (outFormat) *outFormat = GL_RG;
break;
case GL_R32F:
if (outFormat) *outFormat = GL_RED;
break;
}
break;
case GL_RGBA32UI:
case GL_RGB32UI:
case GL_RG32UI:
case GL_R32UI:
if (outInternalFormat) *outInternalFormat = internalFormat;
if (outType) *outType = GL_UNSIGNED_INT;
if (outFormat) switch (internalFormat) {
case GL_RGBA32UI:
if (outFormat) *outFormat = GL_RGBA_INTEGER;
break;
case GL_RGB32UI:
if (outFormat) *outFormat = GL_RGB_INTEGER;
break;
case GL_RG32UI:
if (outFormat) *outFormat = GL_RG_INTEGER;
break;
case GL_R32UI:
if (outFormat) *outFormat = GL_RED_INTEGER;
break;
}
break;
case GL_RGBA32I:
case GL_RGB32I:
case GL_RG32I:
case GL_R32I:
if (outInternalFormat) *outInternalFormat = internalFormat;
if (outType) *outType = GL_INT;
if (outFormat) switch (internalFormat) {
case GL_RGBA32I:
if (outFormat) *outFormat = GL_RGBA_INTEGER;
break;
case GL_RGB32I:
if (outFormat) *outFormat = GL_RGB_INTEGER;
break;
case GL_RG32I:
if (outFormat) *outFormat = GL_RG_INTEGER;
break;
case GL_R32I:
if (outFormat) *outFormat = GL_RED_INTEGER;
break;
}
break;
case GL_RGBA16: {
// TODO: check for extension GL_EXT_texture_norm16 for eligibility of (GL_RGBA16, GL_UNSIGNED_SHORT)
// Most Mali does not support this (< Mali-G6xx, some G720?)
if (outInternalFormat) *outInternalFormat = GL_RGBA16;
if (outType) *outType = GL_UNSIGNED_SHORT;
if (outFormat) *outFormat = GL_RGBA;
break;
// internal format
if (outInternalFormat) {
switch (internalFormat) {
case GL_DEPTH_COMPONENT32:
*outInternalFormat = GL_DEPTH_COMPONENT;
break;
default:
*outInternalFormat = internalFormat;
break;
}
case GL_RGBA8:
if (outInternalFormat) *outInternalFormat = internalFormat;
if (outType) *outType = GL_UNSIGNED_BYTE;
if (outFormat) *outFormat = GL_RGBA;
break;
}
case GL_RGBA:
if (outInternalFormat) *outInternalFormat = internalFormat;
if (outType) *outType = GL_UNSIGNED_BYTE;
if (outFormat) *outFormat = GL_RGBA;
break;
// format
if (outFormat) {
switch (internalFormat) {
// Color Unsigned Normalized
case GL_RGBA:
case GL_RGBA16:
case GL_RGBA8:
*outFormat = GL_RGBA;
break;
case GL_RGBA16F:
case GL_R16F:
if (outInternalFormat) *outInternalFormat = internalFormat;
if (outType) *outType = GL_HALF_FLOAT;
if (outFormat) {
if (internalFormat == GL_RGBA16F) {
*outFormat = GL_RGBA;
} else {
*outFormat = GL_RED;
}
}
break;
case GL_RGB:
case GL_RGB16:
case GL_RGB8:
*outFormat = GL_RGB;
break;
case GL_RGB16:
if (outInternalFormat) *outInternalFormat = GL_RGB16F;
if (outType) *outType = GL_HALF_FLOAT;
if (outFormat) *outFormat = GL_RGB;
break;
case GL_RG:
case GL_RG16:
case GL_RG8:
*outFormat = GL_RG;
break;
case GL_RGB16F:
if (outInternalFormat) *outInternalFormat = internalFormat;
if (outType) *outType = GL_HALF_FLOAT;
if (outFormat) *outFormat = GL_RGB;
break;
case GL_RED:
case GL_R16:
case GL_R8:
*outFormat = GL_RED;
break;
case GL_RG16:
if (outInternalFormat) *outInternalFormat = GL_RG16F;
if (outType) *outType = GL_HALF_FLOAT;
if (outFormat) *outFormat = GL_RG;
break;
case GL_R16:
/* TODO: By using R16F as R16, we're losing ~5bit precision along the way,
* deal with this later.
* This R16F as R16 conversion should only be applied on ES,
* see: https://registry.khronos.org/OpenGL/extensions/OES/OES_texture_buffer.txt
* ```
* Issues:
* (6) Should the R16, RG16 and RGBA16 texture formats be supported?
RESOLVED. No. OpenGL ES 3.0 does not support these formats. They were
considered for late addition to OpenGL ES 3.1 in Bug 11366, but didn't
make the cut. In the absence of another extension to add them, they
are not supported here either.
```
*/
if (outInternalFormat) *outInternalFormat = GL_R16;
if (outType) *outType = GL_UNSIGNED_SHORT;
if (outFormat) *outFormat = GL_RED;
break;
case GL_R8:
if (outInternalFormat) *outInternalFormat = internalFormat;
if (outType) *outType = GL_UNSIGNED_BYTE;
if (outFormat) *outFormat = GL_RED;
break;
// Color Signed Normalized
case GL_RGBA_SNORM:
case GL_RGBA16_SNORM:
case GL_RGBA8_SNORM:
*outFormat = GL_RGBA;
break;
case GL_RED:
// For GL_RED, we need to infer based on type or use default
if (outInternalFormat) *outInternalFormat = GL_R8; // Default fallback
if (outType) *outType = GL_UNSIGNED_BYTE; // Default fallback
if (outFormat) *outFormat = GL_RED;
break;
case GL_RGB_SNORM:
case GL_RGB16_SNORM:
case GL_RGB8_SNORM:
*outFormat = GL_RGB;
break;
case GL_R8UI:
if (outInternalFormat) *outInternalFormat = internalFormat;
if (outType) *outType = GL_UNSIGNED_BYTE;
if (outFormat) *outFormat = GL_RED_INTEGER;
break;
case GL_RG_SNORM:
case GL_RG16_SNORM:
case GL_RG8_SNORM:
*outFormat = GL_RG;
break;
case GL_R8I:
if (outInternalFormat) *outInternalFormat = internalFormat;
if (outType) *outType = GL_BYTE;
if (outFormat) *outFormat = GL_RED_INTEGER;
break;
case GL_RED_SNORM:
case GL_R16_SNORM:
case GL_R8_SNORM:
*outFormat = GL_RED;
break;
case GL_R16UI:
if (outInternalFormat) *outInternalFormat = internalFormat;
if (outType) *outType = GL_UNSIGNED_SHORT;
if (outFormat) *outFormat = GL_RED_INTEGER;
break;
// Color Integer
case GL_RGBA32UI:
case GL_RGBA16UI:
case GL_RGBA8UI:
case GL_RGBA32I:
case GL_RGBA16I:
case GL_RGBA8I:
*outFormat = GL_RGBA_INTEGER;
break;
case GL_RGB32UI:
case GL_RGB16UI:
case GL_RGB8UI:
case GL_RGB32I:
case GL_RGB16I:
case GL_RGB8I:
*outFormat = GL_RGB_INTEGER;
break;
case GL_RG32UI:
case GL_RG16UI:
case GL_RG8UI:
case GL_RG32I:
case GL_RG16I:
case GL_RG8I:
*outFormat = GL_RG_INTEGER;
break;
case GL_R32UI:
case GL_R16UI:
case GL_R8UI:
case GL_R32I:
case GL_R16I:
case GL_R8I:
*outFormat = GL_RED_INTEGER;
break;
case GL_R16I:
if (outInternalFormat) *outInternalFormat = internalFormat;
if (outType) *outType = GL_SHORT;
if (outFormat) *outFormat = GL_RED_INTEGER;
break;
// Color Float
case GL_RGBA32F:
case GL_RGBA16F:
*outFormat = GL_RGBA;
break;
case GL_RGB32F:
case GL_RGB16F:
*outFormat = GL_RGB;
break;
case GL_RG32F:
case GL_RG16F:
*outFormat = GL_RG;
break;
case GL_R32F:
case GL_R16F:
*outFormat = GL_RED;
break;
case GL_RG8:
if (outInternalFormat) *outInternalFormat = internalFormat;
if (outType) *outType = GL_UNSIGNED_BYTE;
if (outFormat) *outFormat = GL_RG;
break;
// Color sRGB
case GL_SRGB:
case GL_SRGB8:
*outFormat = GL_RGB;
break;
case GL_RG16F:
if (outInternalFormat) *outInternalFormat = internalFormat;
if (outType) *outType = GL_HALF_FLOAT;
if (outFormat) *outFormat = GL_RG;
break;
// Color sized other
case GL_RGB9_E5:
case GL_R11F_G11F_B10F:
*outFormat = GL_RGB;
break;
case GL_RGB10_A2:
case GL_RGB5_A1:
*outFormat = GL_RGBA;
break;
case GL_RG8UI:
if (outInternalFormat) *outInternalFormat = internalFormat;
if (outType) *outType = GL_UNSIGNED_BYTE;
if (outFormat) *outFormat = GL_RG_INTEGER;
break;
// Depth
case GL_DEPTH_COMPONENT16:
case GL_DEPTH_COMPONENT24:
case GL_DEPTH_COMPONENT32:
case GL_DEPTH_COMPONENT32F:
case GL_DEPTH_COMPONENT:
*outFormat = GL_DEPTH_COMPONENT;
break;
case GL_RG8I:
if (outInternalFormat) *outInternalFormat = internalFormat;
if (outType) *outType = GL_BYTE;
if (outFormat) *outFormat = GL_RG_INTEGER;
break;
// Depth Stencil
case GL_DEPTH32F_STENCIL8:
case GL_DEPTH_STENCIL:
*outFormat = GL_DEPTH_STENCIL;
break;
case GL_RG16UI:
if (outInternalFormat) *outInternalFormat = internalFormat;
if (outType) *outType = GL_UNSIGNED_SHORT;
if (outFormat) *outFormat = GL_RG_INTEGER;
break;
case GL_RG16I:
if (outInternalFormat) *outInternalFormat = internalFormat;
if (outType) *outType = GL_SHORT;
if (outFormat) *outFormat = GL_RG_INTEGER;
break;
case GL_RGB8:
if (outInternalFormat) *outInternalFormat = internalFormat;
if (outType) *outType = GL_UNSIGNED_BYTE;
if (outFormat) *outFormat = GL_RGB;
break;
// Color Snorm
// 16
case GL_RGBA16_SNORM:
if (outFormat) *outFormat = GL_RGBA;
case GL_RGB16_SNORM:
if (outFormat) *outFormat = GL_RGB;
case GL_RG16_SNORM:
if (outFormat) *outFormat = GL_RG;
case GL_R16_SNORM:
if (outFormat) *outFormat = GL_RED;
if (outInternalFormat) *outInternalFormat = internalFormat;
if (outType) *outType = GL_SHORT;
break;
// 8
case GL_RGBA8_SNORM:
if (outFormat) *outFormat = GL_RGBA;
case GL_RGB8_SNORM:
if (outFormat) *outFormat = GL_RGB;
case GL_RG8_SNORM:
if (outFormat) *outFormat = GL_RG;
case GL_R8_SNORM:
if (outFormat) *outFormat = GL_RED;
if (outInternalFormat) *outInternalFormat = internalFormat;
if (outType) *outType = GL_BYTE;
break;
// Color sized formats
case GL_RGB9_E5:
if (outInternalFormat) *outInternalFormat = internalFormat;
if (outType) *outType = GL_UNSIGNED_INT_5_9_9_9_REV;
if (outFormat) *outFormat = GL_RGB;
break;
case GL_R11F_G11F_B10F:
if (outInternalFormat) *outInternalFormat = internalFormat;
if (outType) *outType = GL_UNSIGNED_INT_10F_11F_11F_REV;
if (outFormat) *outFormat = GL_RGB;
break;
case GL_RGB10_A2:
if (outInternalFormat) *outInternalFormat = internalFormat;
if (outType) *outType = GL_UNSIGNED_INT_2_10_10_10_REV;
if (outFormat) *outFormat = GL_RGBA;
break;
case GL_RGB5_A1:
if (outInternalFormat) *outInternalFormat = internalFormat;
if (outType) *outType = GL_UNSIGNED_SHORT_5_5_5_1;
if (outFormat) *outFormat = GL_RGBA;
break;
case GL_COMPRESSED_RED_RGTC1:
case GL_COMPRESSED_RG_RGTC2:
break;
default:
MGLOG_E("NormalizePixelFormat: unhandled internalFormat: %s", MG_Util::ConvertGLEnumToString(internalFormat).c_str());
// Fallback handling for other formats
if (outInternalFormat) *outInternalFormat = internalFormat;
if (outType) *outType = GL_UNSIGNED_BYTE; // More reasonable default
if (outFormat) {
default:
MGLOG_E("NormalizePixelFormat: outFormat: unhandled internalFormat: %s", MG_Util::ConvertGLEnumToString(internalFormat).c_str());
// Fallback handling for other formats
// Try to infer format from internal format name
if (strstr(MG_Util::ConvertGLEnumToString(internalFormat).c_str(), "RGBA") != nullptr) {
*outFormat = GL_RGBA;
@@ -346,8 +177,144 @@ namespace MobileGL::MG_Util::TextureFormatProcessor {
} else {
*outFormat = GL_RGBA; // Ultimate fallback
}
}
break;
break;
}
}
// type
if (outType) {
switch (internalFormat) {
// Color Unsigned Normalized
case GL_RGBA16:
case GL_RGB16:
case GL_RG16:
case GL_R16:
*outType = GL_UNSIGNED_SHORT;
break;
case GL_RGBA8:
case GL_RGB8:
case GL_RG8:
case GL_R8:
*outType = GL_UNSIGNED_BYTE;
break;
// Color Signed Normalized
case GL_RGBA16_SNORM:
case GL_RGB16_SNORM:
case GL_RG16_SNORM:
case GL_R16_SNORM:
*outType = GL_SHORT;
break;
case GL_RGBA8_SNORM:
case GL_RGB8_SNORM:
case GL_RG8_SNORM:
case GL_R8_SNORM:
*outType = GL_BYTE;
break;
// Color Unsigned Integer
case GL_RGBA32UI:
case GL_RGB32UI:
case GL_RG32UI:
case GL_R32UI:
*outType = GL_UNSIGNED_INT;
break;
case GL_RGBA16UI:
case GL_RGB16UI:
case GL_RG16UI:
case GL_R16UI:
*outType = GL_UNSIGNED_SHORT;
break;
case GL_RGBA8UI:
case GL_RGB8UI:
case GL_RG8UI:
case GL_R8UI:
*outType = GL_UNSIGNED_BYTE;
break;
// Color Integer
case GL_RGBA32I:
case GL_RGB32I:
case GL_RG32I:
case GL_R32I:
*outType = GL_INT;
break;
case GL_RGBA16I:
case GL_RGB16I:
case GL_RG16I:
case GL_R16I:
*outType = GL_SHORT;
break;
case GL_RGBA8I:
case GL_RGB8I:
case GL_RG8I:
case GL_R8I:
*outType = GL_BYTE;
break;
// Color Float
case GL_RGBA32F:
case GL_RGB32F:
case GL_RG32F:
case GL_R32F:
*outType = GL_FLOAT;
break;
case GL_RGBA16F:
case GL_RGB16F:
case GL_RG16F:
case GL_R16F:
*outType = GL_HALF_FLOAT;
break;
// Color sRGB
case GL_SRGB8:
*outType = GL_UNSIGNED_BYTE;
break;
// Color sized other
case GL_RGB9_E5:
*outType = GL_UNSIGNED_INT_5_9_9_9_REV;
break;
case GL_R11F_G11F_B10F:
*outType = GL_UNSIGNED_INT_10F_11F_11F_REV;
break;
case GL_RGB10_A2:
*outType = GL_UNSIGNED_INT_2_10_10_10_REV;
break;
case GL_RGB5_A1:
*outType = GL_UNSIGNED_SHORT_5_5_5_1;
break;
// Depth
case GL_DEPTH_COMPONENT16:
*outType = GL_UNSIGNED_SHORT;
break;
case GL_DEPTH_COMPONENT24:
*outType = GL_UNSIGNED_INT;
break;
case GL_DEPTH_COMPONENT32:
*outType = GL_UNSIGNED_INT;
break;
case GL_DEPTH_COMPONENT32F:
*outType = GL_FLOAT;
break;
case GL_DEPTH_COMPONENT:
*outType = GL_UNSIGNED_INT;
break;
// Depth Stencil
case GL_DEPTH32F_STENCIL8:
case GL_DEPTH_STENCIL:
*outType = GL_FLOAT_32_UNSIGNED_INT_24_8_REV;
break;
default:
MGLOG_E("NormalizePixelFormat: outType: unhandled internalFormat: %s", MG_Util::ConvertGLEnumToString(internalFormat).c_str());
// Fallback handling for other formats
*outType = GL_UNSIGNED_BYTE;
break;
}
}
}
} // namespace MobileGL::MG_Util::TextureFormatProcessor