Flutter Impeller
formats_gles.cc
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
6 
7 // Compressed texture internal formats. ETC2/EAC are core in OpenGL ES 3.0, but
8 // the S3TC (BC1/BC3), RGTC (BC5), BPTC (BC7), and ASTC enums come from
9 // extension headers that are not present on every platform, so they are
10 // guarded here.
11 #ifndef GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
12 #define GL_COMPRESSED_RGBA_S3TC_DXT1_EXT 0x83F1
13 #endif
14 #ifndef GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
15 #define GL_COMPRESSED_RGBA_S3TC_DXT5_EXT 0x83F3
16 #endif
17 #ifndef GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT
18 #define GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT 0x8C4D
19 #endif
20 #ifndef GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT
21 #define GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT 0x8C4F
22 #endif
23 #ifndef GL_COMPRESSED_RG_RGTC2
24 #define GL_COMPRESSED_RG_RGTC2 0x8DBD
25 #endif
26 #ifndef GL_COMPRESSED_RGBA_BPTC_UNORM
27 #define GL_COMPRESSED_RGBA_BPTC_UNORM 0x8E8C
28 #endif
29 #ifndef GL_COMPRESSED_SRGB_ALPHA_BPTC_UNORM
30 #define GL_COMPRESSED_SRGB_ALPHA_BPTC_UNORM 0x8E8D
31 #endif
32 #ifndef GL_COMPRESSED_RGB8_ETC2
33 #define GL_COMPRESSED_RGB8_ETC2 0x9274
34 #endif
35 #ifndef GL_COMPRESSED_SRGB8_ETC2
36 #define GL_COMPRESSED_SRGB8_ETC2 0x9275
37 #endif
38 #ifndef GL_COMPRESSED_RGBA8_ETC2_EAC
39 #define GL_COMPRESSED_RGBA8_ETC2_EAC 0x9278
40 #endif
41 #ifndef GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC
42 #define GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC 0x9279
43 #endif
44 #ifndef GL_COMPRESSED_RGBA_ASTC_4x4_KHR
45 #define GL_COMPRESSED_RGBA_ASTC_4x4_KHR 0x93B0
46 #endif
47 #ifndef GL_COMPRESSED_RGBA_ASTC_8x8_KHR
48 #define GL_COMPRESSED_RGBA_ASTC_8x8_KHR 0x93B7
49 #endif
50 #ifndef GL_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR
51 #define GL_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR 0x93D0
52 #endif
53 #ifndef GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR
54 #define GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR 0x93D7
55 #endif
56 
57 namespace impeller {
58 
59 std::string DebugToFramebufferError(int status) {
60  switch (status) {
61  case GL_FRAMEBUFFER_UNDEFINED:
62  return "GL_FRAMEBUFFER_UNDEFINED";
63  case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT:
64  return "GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT";
65  case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT:
66  return "GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT";
67  case GL_FRAMEBUFFER_UNSUPPORTED:
68  return "GL_FRAMEBUFFER_UNSUPPORTED";
69  case GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE:
70  return "GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE";
71  default:
72  return "Unknown error code: " + std::to_string(status);
73  }
74 }
75 
76 std::optional<PixelFormatGLES> ToPixelFormatGLES(PixelFormat pixel_format,
77  bool supports_bgra) {
78  PixelFormatGLES format;
79 
80  switch (pixel_format) {
82  format.internal_format = GL_ALPHA;
83  format.external_format = GL_ALPHA;
84  format.type = GL_UNSIGNED_BYTE;
85  break;
87  format.internal_format = GL_RED;
88  format.external_format = GL_RED;
89  format.type = GL_UNSIGNED_BYTE;
90  break;
93  format.internal_format = GL_RGBA;
94  format.external_format = GL_RGBA;
95  format.type = GL_UNSIGNED_BYTE;
96  break;
99  if (supports_bgra) {
100  format.internal_format = GL_BGRA_EXT;
101  format.external_format = GL_BGRA_EXT;
102  } else {
103  format.internal_format = GL_RGBA;
104  format.external_format = GL_RGBA;
105  }
106  format.type = GL_UNSIGNED_BYTE;
107  break;
109  format.internal_format = GL_RGBA32F;
110  format.external_format = GL_RGBA;
111  format.type = GL_FLOAT;
112  break;
114  format.internal_format = GL_R32F;
115  format.external_format = GL_RED;
116  format.type = GL_FLOAT;
117  break;
119  format.internal_format = GL_RGBA16F;
120  format.external_format = GL_RGBA;
121  format.type = GL_HALF_FLOAT;
122  break;
124  // Pure stencil textures are only available in OpenGL 4.4+, which is
125  // ~0% of mobile devices. Instead, we use a depth-stencil texture and
126  // only use the stencil component.
127  //
128  // https://registry.khronos.org/OpenGL-Refpages/gl4/html/glTexImage2D.xhtml
130  format.internal_format = GL_DEPTH_STENCIL;
131  format.external_format = GL_DEPTH_STENCIL;
132  format.type = GL_UNSIGNED_INT_24_8;
133  break;
134  // Block-compressed formats. Only the internal format is meaningful; the
135  // data is uploaded with glCompressedTexImage2D rather than glTexImage2D.
138  format.is_compressed = true;
139  break;
142  format.is_compressed = true;
143  break;
146  format.is_compressed = true;
147  break;
150  format.is_compressed = true;
151  break;
154  format.is_compressed = true;
155  break;
158  format.is_compressed = true;
159  break;
162  format.is_compressed = true;
163  break;
166  format.is_compressed = true;
167  break;
170  format.is_compressed = true;
171  break;
174  format.is_compressed = true;
175  break;
178  format.is_compressed = true;
179  break;
182  format.is_compressed = true;
183  break;
186  format.is_compressed = true;
187  break;
190  format.is_compressed = true;
191  break;
194  format.is_compressed = true;
195  break;
196  // ASTC HDR uses the same internal formats as LDR; the HDR profile is
197  // selected by the GL_KHR_texture_compression_astc_hdr extension.
200  format.is_compressed = true;
201  break;
204  format.is_compressed = true;
205  break;
212  return std::nullopt;
213  }
214  return format;
215 }
216 
217 } // namespace impeller
#define GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR
Definition: formats_gles.cc:54
#define GL_COMPRESSED_SRGB_ALPHA_BPTC_UNORM
Definition: formats_gles.cc:30
#define GL_COMPRESSED_RGB8_ETC2
Definition: formats_gles.cc:33
#define GL_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR
Definition: formats_gles.cc:51
#define GL_COMPRESSED_RG_RGTC2
Definition: formats_gles.cc:24
#define GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT
Definition: formats_gles.cc:21
#define GL_COMPRESSED_RGBA8_ETC2_EAC
Definition: formats_gles.cc:39
#define GL_COMPRESSED_SRGB8_ETC2
Definition: formats_gles.cc:36
#define GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
Definition: formats_gles.cc:15
#define GL_COMPRESSED_RGBA_ASTC_4x4_KHR
Definition: formats_gles.cc:45
#define GL_COMPRESSED_RGBA_BPTC_UNORM
Definition: formats_gles.cc:27
#define GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC
Definition: formats_gles.cc:42
#define GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
Definition: formats_gles.cc:12
#define GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT
Definition: formats_gles.cc:18
#define GL_COMPRESSED_RGBA_ASTC_8x8_KHR
Definition: formats_gles.cc:48
std::optional< PixelFormatGLES > ToPixelFormatGLES(PixelFormat pixel_format, bool supports_bgra)
Definition: formats_gles.cc:76
std::string DebugToFramebufferError(int status)
Definition: formats_gles.cc:59
PixelFormat
The Pixel formats supported by Impeller. The naming convention denotes the usage of the component,...
Definition: formats.h:99