| 1 |
#include "GLTextureFormat.h" |
| 2 |
#include "../../../ServiceLocator.h" |
| 3 |
|
| 4 |
#if defined(DEATH_TARGET_ANDROID) |
| 5 |
# include <android/api-level.h> |
| 6 |
#endif |
| 7 |
|
| 8 |
namespace nCine::RHI::GL |
| 9 |
{ |
| 10 |
void GLTextureFormat::Resolve(PixelFormat format, bool bgr, GLint& internalFormat, GLenum& externalFormat, GLenum& dataType) |
| 11 |
{ |
| 12 |
switch (format) { |
| 13 |
#if defined(RHI_GL_PROFILE_ES2) |
| 14 |
// OpenGL|ES 2.0 core has no sized internal formats and no GL_RED/GL_RG - `internalformat` must equal |
| 15 |
// `format` and be one of ALPHA/LUMINANCE/LUMINANCE_ALPHA/RGB/RGBA (ES 2.0 spec table 3.4). The engine's |
| 16 |
// single/dual-channel data maps to LUMINANCE / LUMINANCE_ALPHA, which sample as (L,L,L,1) / (L,L,L,A) - |
| 17 |
// exactly what the palette pipeline's GL3.3 swizzle produces for the channels its shaders read (.r/.a), |
| 18 |
// so GLTexture::SetSwizzle() can be a no-op on this profile |
| 19 |
case PixelFormat::RGBA8: |
| 20 |
internalFormat = GL_RGBA; externalFormat = GL_RGBA; dataType = GL_UNSIGNED_BYTE; break; |
| 21 |
case PixelFormat::RGB8: |
| 22 |
internalFormat = GL_RGB; externalFormat = GL_RGB; dataType = GL_UNSIGNED_BYTE; break; |
| 23 |
case PixelFormat::RG8: |
| 24 |
internalFormat = GL_LUMINANCE_ALPHA; externalFormat = GL_LUMINANCE_ALPHA; dataType = GL_UNSIGNED_BYTE; break; |
| 25 |
case PixelFormat::R8: |
| 26 |
internalFormat = GL_LUMINANCE; externalFormat = GL_LUMINANCE; dataType = GL_UNSIGNED_BYTE; break; |
| 27 |
// Depth *textures* need OES_depth_texture on ES2 and the engine only ever creates depth renderbuffers |
| 28 |
// (GLRenderTarget), so the depth cases intentionally fall through to the unsupported-format assert |
| 29 |
|
| 30 |
// Packed integer formats (already unsized in ES2 style; internal must equal external) |
| 31 |
case PixelFormat::RGB5A1: |
| 32 |
internalFormat = GL_RGBA; externalFormat = GL_RGBA; dataType = GL_UNSIGNED_SHORT_5_5_5_1; break; |
| 33 |
case PixelFormat::RGBA4: |
| 34 |
internalFormat = GL_RGBA; externalFormat = GL_RGBA; dataType = GL_UNSIGNED_SHORT_4_4_4_4; break; |
| 35 |
case PixelFormat::RGB565: |
| 36 |
internalFormat = GL_RGB; externalFormat = GL_RGB; dataType = GL_UNSIGNED_SHORT_5_6_5; break; |
| 37 |
#else |
| 38 |
// Uncompressed integer formats (external pixel type is always GL_UNSIGNED_BYTE) |
| 39 |
case PixelFormat::RGBA8: |
| 40 |
internalFormat = GL_RGBA8; externalFormat = GL_RGBA; dataType = GL_UNSIGNED_BYTE; break; |
| 41 |
case PixelFormat::RGB8: |
| 42 |
internalFormat = GL_RGB8; externalFormat = GL_RGB; dataType = GL_UNSIGNED_BYTE; break; |
| 43 |
case PixelFormat::RG8: |
| 44 |
# if defined(DEATH_TARGET_EMSCRIPTEN) |
| 45 |
// WebGL 2.0 leaves out GL_TEXTURE_SWIZZLE_* even though the ES 3.0 it is based on has them, so the |
| 46 |
// palette pipeline's (R,G,B,G) swizzle cannot be applied - see GLTexture::SetSwizzle(). The ES2 way |
| 47 |
// out works here too: LUMINANCE_ALPHA samples as (L,L,L,A), which is what the shaders read out of an |
| 48 |
// (index, alpha) pair as .r/.a, so they stay profile-independent. Two consequences follow, both of |
| 49 |
// which the ES2 profile already lives with: it is unsized, so glTexStorage2D() rejects it and |
| 50 |
// GLTexture::TexStorage2D() allocates per level instead, and it is not color-renderable, so a |
| 51 |
// two-channel render target has to be RGBA8 here. |
| 52 |
internalFormat = GL_LUMINANCE_ALPHA; externalFormat = GL_LUMINANCE_ALPHA; dataType = GL_UNSIGNED_BYTE; break; |
| 53 |
# else |
| 54 |
internalFormat = GL_RG8; externalFormat = GL_RG; dataType = GL_UNSIGNED_BYTE; break; |
| 55 |
# endif |
| 56 |
case PixelFormat::R8: |
| 57 |
internalFormat = GL_R8; externalFormat = GL_RED; dataType = GL_UNSIGNED_BYTE; break; |
| 58 |
case PixelFormat::Depth16: |
| 59 |
internalFormat = GL_DEPTH_COMPONENT16; externalFormat = GL_DEPTH_COMPONENT; dataType = GL_UNSIGNED_BYTE; break; |
| 60 |
case PixelFormat::Depth24: |
| 61 |
internalFormat = GL_DEPTH_COMPONENT24; externalFormat = GL_DEPTH_COMPONENT; dataType = GL_UNSIGNED_BYTE; break; |
| 62 |
|
| 63 |
// Packed integer formats |
| 64 |
case PixelFormat::RGB5A1: |
| 65 |
internalFormat = GL_RGB5_A1; externalFormat = GL_RGBA; dataType = GL_UNSIGNED_SHORT_5_5_5_1; break; |
| 66 |
case PixelFormat::RGBA4: |
| 67 |
internalFormat = GL_RGBA4; externalFormat = GL_RGBA; dataType = GL_UNSIGNED_SHORT_4_4_4_4; break; |
| 68 |
case PixelFormat::RGB565: |
| 69 |
internalFormat = GL_RGB565; externalFormat = GL_RGB; dataType = GL_UNSIGNED_SHORT_5_6_5; break; |
| 70 |
#endif |
| 71 |
|
| 72 |
// Floating-point formats (external pixel type is always GL_FLOAT) |
| 73 |
case PixelFormat::RGBA16F: |
| 74 |
internalFormat = GL_RGBA16F; externalFormat = GL_RGBA; dataType = GL_FLOAT; break; |
| 75 |
#if !defined(DEATH_TARGET_VITA) // vitaGL declares GL_RGBA16F but not the 32-bit-float / RGB-float internal formats |
| 76 |
case PixelFormat::RGBA32F: |
| 77 |
internalFormat = GL_RGBA32F; externalFormat = GL_RGBA; dataType = GL_FLOAT; break; |
| 78 |
case PixelFormat::RGB16F: |
| 79 |
internalFormat = GL_RGB16F; externalFormat = GL_RGB; dataType = GL_FLOAT; break; |
| 80 |
case PixelFormat::RGB32F: |
| 81 |
internalFormat = GL_RGB32F; externalFormat = GL_RGB; dataType = GL_FLOAT; break; |
| 82 |
#endif |
| 83 |
case PixelFormat::Depth32F: |
| 84 |
internalFormat = GL_DEPTH_COMPONENT32F; externalFormat = GL_DEPTH_COMPONENT; dataType = GL_FLOAT; break; |
| 85 |
|
| 86 |
// Compressed S3TC / DXT (external pixel type is always GL_UNSIGNED_BYTE) |
| 87 |
case PixelFormat::DXT1RGBA: |
| 88 |
internalFormat = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT; externalFormat = GL_RGBA; dataType = GL_UNSIGNED_BYTE; break; |
| 89 |
case PixelFormat::DXT3: |
| 90 |
internalFormat = GL_COMPRESSED_RGBA_S3TC_DXT3_EXT; externalFormat = GL_RGBA; dataType = GL_UNSIGNED_BYTE; break; |
| 91 |
case PixelFormat::DXT5: |
| 92 |
internalFormat = GL_COMPRESSED_RGBA_S3TC_DXT5_EXT; externalFormat = GL_RGBA; dataType = GL_UNSIGNED_BYTE; break; |
| 93 |
case PixelFormat::DXT1RGB: |
| 94 |
internalFormat = GL_COMPRESSED_RGB_S3TC_DXT1_EXT; externalFormat = GL_RGB; dataType = GL_UNSIGNED_BYTE; break; |
| 95 |
|
| 96 |
#if defined(RHI_GL_PROFILE_ES) |
| 97 |
// Compressed OpenGL ES formats (external pixel type is always GL_UNSIGNED_BYTE) |
| 98 |
case PixelFormat::ATC_RGBA_Explicit: |
| 99 |
internalFormat = GL_ATC_RGBA_EXPLICIT_ALPHA_AMD; externalFormat = GL_RGBA; dataType = GL_UNSIGNED_BYTE; break; |
| 100 |
case PixelFormat::ATC_RGBA_Interpolated: |
| 101 |
internalFormat = GL_ATC_RGBA_INTERPOLATED_ALPHA_AMD; externalFormat = GL_RGBA; dataType = GL_UNSIGNED_BYTE; break; |
| 102 |
case PixelFormat::PVRTC_2BPP_RGBA: |
| 103 |
internalFormat = GL_COMPRESSED_RGBA_PVRTC_2BPPV1_IMG; externalFormat = GL_RGBA; dataType = GL_UNSIGNED_BYTE; break; |
| 104 |
case PixelFormat::PVRTC_4BPP_RGBA: |
| 105 |
internalFormat = GL_COMPRESSED_RGBA_PVRTC_4BPPV1_IMG; externalFormat = GL_RGBA; dataType = GL_UNSIGNED_BYTE; break; |
| 106 |
case PixelFormat::ETC2RGBA8: |
| 107 |
internalFormat = GL_COMPRESSED_RGBA8_ETC2_EAC; externalFormat = GL_RGBA; dataType = GL_UNSIGNED_BYTE; break; |
| 108 |
# if !defined(DEATH_TARGET_VITA) // GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2 is not declared by vitaGL |
| 109 |
case PixelFormat::ETC2RGB8A1: |
| 110 |
internalFormat = GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2; externalFormat = GL_RGBA; dataType = GL_UNSIGNED_BYTE; break; |
| 111 |
# endif |
| 112 |
# if ((!defined(DEATH_TARGET_ANDROID) && defined(RHI_GL_PROFILE_ES)) || (defined(DEATH_TARGET_ANDROID) && __ANDROID_API__ >= 21)) && !defined(DEATH_TARGET_VITA) |
| 113 |
case PixelFormat::ASTC_4x4: |
| 114 |
internalFormat = GL_COMPRESSED_RGBA_ASTC_4x4_KHR; externalFormat = GL_RGBA; dataType = GL_UNSIGNED_BYTE; break; |
| 115 |
case PixelFormat::ASTC_5x4: |
| 116 |
internalFormat = GL_COMPRESSED_RGBA_ASTC_5x4_KHR; externalFormat = GL_RGBA; dataType = GL_UNSIGNED_BYTE; break; |
| 117 |
case PixelFormat::ASTC_5x5: |
| 118 |
internalFormat = GL_COMPRESSED_RGBA_ASTC_5x5_KHR; externalFormat = GL_RGBA; dataType = GL_UNSIGNED_BYTE; break; |
| 119 |
case PixelFormat::ASTC_6x5: |
| 120 |
internalFormat = GL_COMPRESSED_RGBA_ASTC_6x5_KHR; externalFormat = GL_RGBA; dataType = GL_UNSIGNED_BYTE; break; |
| 121 |
case PixelFormat::ASTC_6x6: |
| 122 |
internalFormat = GL_COMPRESSED_RGBA_ASTC_6x6_KHR; externalFormat = GL_RGBA; dataType = GL_UNSIGNED_BYTE; break; |
| 123 |
case PixelFormat::ASTC_8x5: |
| 124 |
internalFormat = GL_COMPRESSED_RGBA_ASTC_8x5_KHR; externalFormat = GL_RGBA; dataType = GL_UNSIGNED_BYTE; break; |
| 125 |
case PixelFormat::ASTC_8x6: |
| 126 |
internalFormat = GL_COMPRESSED_RGBA_ASTC_8x6_KHR; externalFormat = GL_RGBA; dataType = GL_UNSIGNED_BYTE; break; |
| 127 |
case PixelFormat::ASTC_8x8: |
| 128 |
internalFormat = GL_COMPRESSED_RGBA_ASTC_8x8_KHR; externalFormat = GL_RGBA; dataType = GL_UNSIGNED_BYTE; break; |
| 129 |
case PixelFormat::ASTC_10x5: |
| 130 |
internalFormat = GL_COMPRESSED_RGBA_ASTC_10x5_KHR; externalFormat = GL_RGBA; dataType = GL_UNSIGNED_BYTE; break; |
| 131 |
case PixelFormat::ASTC_10x6: |
| 132 |
internalFormat = GL_COMPRESSED_RGBA_ASTC_10x6_KHR; externalFormat = GL_RGBA; dataType = GL_UNSIGNED_BYTE; break; |
| 133 |
case PixelFormat::ASTC_10x8: |
| 134 |
internalFormat = GL_COMPRESSED_RGBA_ASTC_10x8_KHR; externalFormat = GL_RGBA; dataType = GL_UNSIGNED_BYTE; break; |
| 135 |
case PixelFormat::ASTC_10x10: |
| 136 |
internalFormat = GL_COMPRESSED_RGBA_ASTC_10x10_KHR; externalFormat = GL_RGBA; dataType = GL_UNSIGNED_BYTE; break; |
| 137 |
case PixelFormat::ASTC_12x10: |
| 138 |
internalFormat = GL_COMPRESSED_RGBA_ASTC_12x10_KHR; externalFormat = GL_RGBA; dataType = GL_UNSIGNED_BYTE; break; |
| 139 |
case PixelFormat::ASTC_12x12: |
| 140 |
internalFormat = GL_COMPRESSED_RGBA_ASTC_12x12_KHR; externalFormat = GL_RGBA; dataType = GL_UNSIGNED_BYTE; break; |
| 141 |
# endif |
| 142 |
case PixelFormat::ETC1: |
| 143 |
internalFormat = GL_ETC1_RGB8_OES; externalFormat = GL_RGB; dataType = GL_UNSIGNED_BYTE; break; |
| 144 |
# if !defined(DEATH_TARGET_VITA) // GL_COMPRESSED_RGB8_ETC2 is not declared by vitaGL |
| 145 |
case PixelFormat::ETC2RGB8: |
| 146 |
internalFormat = GL_COMPRESSED_RGB8_ETC2; externalFormat = GL_RGB; dataType = GL_UNSIGNED_BYTE; break; |
| 147 |
# endif |
| 148 |
case PixelFormat::ATC_RGB: |
| 149 |
internalFormat = GL_ATC_RGB_AMD; externalFormat = GL_RGB; dataType = GL_UNSIGNED_BYTE; break; |
| 150 |
case PixelFormat::PVRTC_2BPP_RGB: |
| 151 |
internalFormat = GL_COMPRESSED_RGB_PVRTC_2BPPV1_IMG; externalFormat = GL_RGB; dataType = GL_UNSIGNED_BYTE; break; |
| 152 |
case PixelFormat::PVRTC_4BPP_RGB: |
| 153 |
internalFormat = GL_COMPRESSED_RGB_PVRTC_4BPPV1_IMG; externalFormat = GL_RGB; dataType = GL_UNSIGNED_BYTE; break; |
| 154 |
# if !defined(DEATH_TARGET_VITA) // GL_COMPRESSED_RG11_EAC / GL_COMPRESSED_R11_EAC are not declared by vitaGL |
| 155 |
case PixelFormat::EAC_RG11: |
| 156 |
internalFormat = GL_COMPRESSED_RG11_EAC; externalFormat = GL_RG; dataType = GL_UNSIGNED_BYTE; break; |
| 157 |
case PixelFormat::EAC_R11: |
| 158 |
internalFormat = GL_COMPRESSED_R11_EAC; externalFormat = GL_RED; dataType = GL_UNSIGNED_BYTE; break; |
| 159 |
# endif |
| 160 |
#endif |
| 161 |
|
| 162 |
default: |
| 163 |
FATAL_ASSERT_MSG(false, "Unsupported pixel format: {}", std::uint32_t(format)); |
| 164 |
break; |
| 165 |
} |
| 166 |
|
| 167 |
// Convert the external format to the corresponding BGR/BGRA one, matching the former `TextureFormat::bgrFormat()` |
| 168 |
if (bgr) { |
| 169 |
if (externalFormat == GL_RGBA) { |
| 170 |
#if defined(DEATH_TARGET_VITA) |
| 171 |
// vitaGL declares no GL_BGRA_EXT, BGRA source data is left unswizzled (the bgr path is unused on Vita) |
| 172 |
#elif defined(RHI_GL_PROFILE_ES2) |
| 173 |
// ES2's GL_EXT_texture_format_BGRA8888 requires `internalformat` to be GL_BGRA_EXT as well |
| 174 |
// (ES2 core mandates internal == external), unlike ES3 where only the external format changes |
| 175 |
externalFormat = GL_BGRA_EXT; |
| 176 |
internalFormat = GL_BGRA_EXT; |
| 177 |
#elif defined(RHI_GL_PROFILE_CORE) |
| 178 |
externalFormat = GL_BGRA; |
| 179 |
#else |
| 180 |
externalFormat = GL_BGRA_EXT; |
| 181 |
#endif |
| 182 |
} |
| 183 |
#if defined(RHI_GL_PROFILE_CORE) |
| 184 |
else if (externalFormat == GL_RGB) { |
| 185 |
externalFormat = GL_BGR; |
| 186 |
} |
| 187 |
#endif |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
bool GLTextureFormat::IsUnsizedInternalFormat(GLint internalFormat) |
| 192 |
{ |
| 193 |
switch (internalFormat) { |
| 194 |
case GL_ALPHA: |
| 195 |
case GL_LUMINANCE: |
| 196 |
case GL_LUMINANCE_ALPHA: |
| 197 |
case GL_RGB: |
| 198 |
case GL_RGBA: |
| 199 |
return true; |
| 200 |
default: |
| 201 |
return false; |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
void GLTextureFormat::CheckSupport(PixelFormat format) |
| 206 |
{ |
| 207 |
const IRhiCapabilities& caps = theServiceLocator().GetRhiCapabilities(); |
| 208 |
|
| 209 |
switch (format) { |
| 210 |
case PixelFormat::DXT1RGB: |
| 211 |
case PixelFormat::DXT3: |
| 212 |
case PixelFormat::DXT5: { |
| 213 |
const bool hasS3tc = caps.HasExtension(IRhiCapabilities::Extensions::ExtTextureCompressionS3tc); |
| 214 |
FATAL_ASSERT_MSG(hasS3tc, "GL_EXT_texture_compression_s3tc not available"); |
| 215 |
break; |
| 216 |
} |
| 217 |
#if defined(RHI_GL_PROFILE_ES) |
| 218 |
case PixelFormat::ETC1: { |
| 219 |
const bool hasEct1 = caps.HasExtension(IRhiCapabilities::Extensions::OesCompressedEtc1Rgb8Texture); |
| 220 |
FATAL_ASSERT_MSG(hasEct1, "GL_OES_compressed_etc1_rgb8_texture not available"); |
| 221 |
break; |
| 222 |
} |
| 223 |
case PixelFormat::ATC_RGB: |
| 224 |
case PixelFormat::ATC_RGBA_Explicit: |
| 225 |
case PixelFormat::ATC_RGBA_Interpolated: { |
| 226 |
const bool hasAtc = caps.HasExtension(IRhiCapabilities::Extensions::AmdCompressedAtcTexture); |
| 227 |
FATAL_ASSERT_MSG(hasAtc, "GL_AMD_compressed_ATC_texture not available"); |
| 228 |
break; |
| 229 |
} |
| 230 |
case PixelFormat::PVRTC_2BPP_RGB: |
| 231 |
case PixelFormat::PVRTC_2BPP_RGBA: |
| 232 |
case PixelFormat::PVRTC_4BPP_RGB: |
| 233 |
case PixelFormat::PVRTC_4BPP_RGBA: { |
| 234 |
const bool hasPvr = caps.HasExtension(IRhiCapabilities::Extensions::ImgTextureCompressionPvrtc); |
| 235 |
FATAL_ASSERT_MSG(hasPvr, "GL_IMG_texture_compression_pvrtc not available"); |
| 236 |
break; |
| 237 |
} |
| 238 |
# if (!defined(DEATH_TARGET_ANDROID) && defined(RHI_GL_PROFILE_ES)) || (defined(DEATH_TARGET_ANDROID) && __ANDROID_API__ >= 21) |
| 239 |
case PixelFormat::ASTC_4x4: |
| 240 |
case PixelFormat::ASTC_5x4: |
| 241 |
case PixelFormat::ASTC_5x5: |
| 242 |
case PixelFormat::ASTC_6x5: |
| 243 |
case PixelFormat::ASTC_6x6: |
| 244 |
case PixelFormat::ASTC_8x5: |
| 245 |
case PixelFormat::ASTC_8x6: |
| 246 |
case PixelFormat::ASTC_8x8: |
| 247 |
case PixelFormat::ASTC_10x5: |
| 248 |
case PixelFormat::ASTC_10x6: |
| 249 |
case PixelFormat::ASTC_10x8: |
| 250 |
case PixelFormat::ASTC_10x10: |
| 251 |
case PixelFormat::ASTC_12x10: |
| 252 |
case PixelFormat::ASTC_12x12: { |
| 253 |
const bool hasAstc = caps.HasExtension(IRhiCapabilities::Extensions::KhrTextureCompressionAstcLdr); |
| 254 |
FATAL_ASSERT_MSG(hasAstc, "GL_KHR_texture_compression_astc_ldr not available"); |
| 255 |
break; |
| 256 |
} |
| 257 |
# endif |
| 258 |
#endif |
| 259 |
default: |
| 260 |
break; |
| 261 |
} |
| 262 |
} |
| 263 |
} |