1 //
2 // Copyright (c) 2013-2014 The ANGLE Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5 //
6 
7 // formatutils.cpp: Queries for GL image formats.
8 
9 #include "libANGLE/formatutils.h"
10 
11 #include "common/mathutil.h"
12 #include "libANGLE/Context.h"
13 #include "libANGLE/Framebuffer.h"
14 
15 using namespace angle;
16 
17 namespace gl
18 {
19 
20 // ES2 requires that format is equal to internal format at all glTex*Image2D entry points and the implementation
21 // can decide the true, sized, internal format. The ES2FormatMap determines the internal format for all valid
22 // format and type combinations.
23 GLenum GetSizedFormatInternal(GLenum format, GLenum type);
24 
25 namespace
26 {
27 using InternalFormatInfoMap =
28     std::unordered_map<GLenum, std::unordered_map<GLenum, InternalFormat>>;
29 
30 }  // anonymous namespace
31 
FormatType()32 FormatType::FormatType() : format(GL_NONE), type(GL_NONE)
33 {
34 }
35 
FormatType(GLenum format_,GLenum type_)36 FormatType::FormatType(GLenum format_, GLenum type_) : format(format_), type(type_)
37 {
38 }
39 
operator <(const FormatType & other) const40 bool FormatType::operator<(const FormatType &other) const
41 {
42     if (format != other.format)
43         return format < other.format;
44     return type < other.type;
45 }
46 
Type()47 Type::Type()
48     : bytes(0),
49       bytesShift(0),
50       specialInterpretation(false)
51 {
52 }
53 
GenTypeInfo(GLuint bytes,bool specialInterpretation)54 static Type GenTypeInfo(GLuint bytes, bool specialInterpretation)
55 {
56     Type info;
57     info.bytes = bytes;
58     GLuint i = 0;
59     while ((1u << i) < bytes)
60     {
61         ++i;
62     }
63     info.bytesShift = i;
64     ASSERT((1u << info.bytesShift) == bytes);
65     info.specialInterpretation = specialInterpretation;
66     return info;
67 }
68 
operator <(const Type & a,const Type & b)69 bool operator<(const Type& a, const Type& b)
70 {
71     return memcmp(&a, &b, sizeof(Type)) < 0;
72 }
73 
74 // Information about internal formats
AlwaysSupported(const Version &,const Extensions &)75 static bool AlwaysSupported(const Version &, const Extensions &)
76 {
77     return true;
78 }
79 
NeverSupported(const Version &,const Extensions &)80 static bool NeverSupported(const Version &, const Extensions &)
81 {
82     return false;
83 }
84 
85 template <GLuint minCoreGLMajorVersion, GLuint minCoreGLMinorVersion>
RequireES(const Version & clientVersion,const Extensions &)86 static bool RequireES(const Version &clientVersion, const Extensions &)
87 {
88     return clientVersion >= Version(minCoreGLMajorVersion, minCoreGLMinorVersion);
89 }
90 
91 // Pointer to a boolean memeber of the Extensions struct
92 typedef bool(Extensions::*ExtensionBool);
93 
94 // Check support for a single extension
95 template <ExtensionBool bool1>
RequireExt(const Version &,const Extensions & extensions)96 static bool RequireExt(const Version &, const Extensions &extensions)
97 {
98     return extensions.*bool1;
99 }
100 
101 // Check for a minimum client version or a single extension
102 template <GLuint minCoreGLMajorVersion, GLuint minCoreGLMinorVersion, ExtensionBool bool1>
RequireESOrExt(const Version & clientVersion,const Extensions & extensions)103 static bool RequireESOrExt(const Version &clientVersion, const Extensions &extensions)
104 {
105     return clientVersion >= Version(minCoreGLMajorVersion, minCoreGLMinorVersion) ||
106            extensions.*bool1;
107 }
108 
109 // Check for a minimum client version or two extensions
110 template <GLuint minCoreGLMajorVersion,
111           GLuint minCoreGLMinorVersion,
112           ExtensionBool bool1,
113           ExtensionBool bool2>
RequireESOrExtAndExt(const Version & clientVersion,const Extensions & extensions)114 static bool RequireESOrExtAndExt(const Version &clientVersion, const Extensions &extensions)
115 {
116     return clientVersion >= Version(minCoreGLMajorVersion, minCoreGLMinorVersion) ||
117            (extensions.*bool1 && extensions.*bool2);
118 }
119 
120 // Check for a minimum client version or at least one of two extensions
121 template <GLuint minCoreGLMajorVersion,
122           GLuint minCoreGLMinorVersion,
123           ExtensionBool bool1,
124           ExtensionBool bool2>
RequireESOrExtOrExt(const Version & clientVersion,const Extensions & extensions)125 static bool RequireESOrExtOrExt(const Version &clientVersion, const Extensions &extensions)
126 {
127     return clientVersion >= Version(minCoreGLMajorVersion, minCoreGLMinorVersion) ||
128            extensions.*bool1 || extensions.*bool2;
129 }
130 
131 // Check support for two extensions
132 template <ExtensionBool bool1, ExtensionBool bool2>
RequireExtAndExt(const Version &,const Extensions & extensions)133 static bool RequireExtAndExt(const Version &, const Extensions &extensions)
134 {
135     return extensions.*bool1 && extensions.*bool2;
136 }
137 
138 // Check support for either of two extensions
139 template <ExtensionBool bool1, ExtensionBool bool2>
RequireExtOrExt(const Version &,const Extensions & extensions)140 static bool RequireExtOrExt(const Version &, const Extensions &extensions)
141 {
142     return extensions.*bool1 || extensions.*bool2;
143 }
144 
145 // Special function for half float formats with three or four channels.
HalfFloatSupport(const Version & clientVersion,const Extensions & extensions)146 static bool HalfFloatSupport(const Version &clientVersion, const Extensions &extensions)
147 {
148     return clientVersion >= Version(3, 0) || extensions.textureHalfFloat;
149 }
150 
HalfFloatRGBRenderableSupport(const Version & clientVersion,const Extensions & extensions)151 static bool HalfFloatRGBRenderableSupport(const Version &clientVersion,
152                                           const Extensions &extensions)
153 {
154     return HalfFloatSupport(clientVersion, extensions) && extensions.colorBufferHalfFloat;
155 }
156 
HalfFloatRGBARenderableSupport(const Version & clientVersion,const Extensions & extensions)157 static bool HalfFloatRGBARenderableSupport(const Version &clientVersion,
158                                            const Extensions &extensions)
159 {
160     return HalfFloatSupport(clientVersion, extensions) &&
161            (extensions.colorBufferHalfFloat || extensions.colorBufferFloat);
162 }
163 
164 // Special function for half float formats with one or two channels.
165 
166 // R16F, RG16F
HalfFloatRGSupport(const Version & clientVersion,const Extensions & extensions)167 static bool HalfFloatRGSupport(const Version &clientVersion, const Extensions &extensions)
168 {
169     return clientVersion >= Version(3, 0) || (extensions.textureHalfFloat && extensions.textureRG);
170 }
171 
172 // R16F, RG16F
HalfFloatRGRenderableSupport(const Version & clientVersion,const Extensions & extensions)173 static bool HalfFloatRGRenderableSupport(const Version &clientVersion, const Extensions &extensions)
174 {
175     // It's unclear if EXT_color_buffer_half_float gives renderability to non-OES half float
176     // textures
177     return HalfFloatRGSupport(clientVersion, extensions) &&
178            (extensions.colorBufferHalfFloat || extensions.colorBufferFloat);
179 }
180 
181 // RED + HALF_FLOAT_OES, RG + HALF_FLOAT_OES
UnsizedHalfFloatOESRGSupport(const Version &,const Extensions & extensions)182 static bool UnsizedHalfFloatOESRGSupport(const Version &, const Extensions &extensions)
183 {
184     return extensions.textureHalfFloat && extensions.textureRG;
185 }
186 
187 // RED + HALF_FLOAT_OES, RG + HALF_FLOAT_OES
UnsizedHalfFloatOESRGRenderableSupport(const Version & clientVersion,const Extensions & extensions)188 static bool UnsizedHalfFloatOESRGRenderableSupport(const Version &clientVersion,
189                                                    const Extensions &extensions)
190 {
191     return UnsizedHalfFloatOESRGSupport(clientVersion, extensions) &&
192            extensions.colorBufferHalfFloat;
193 }
194 
195 // RGB + HALF_FLOAT_OES, RGBA + HALF_FLOAT_OES
UnsizedHalfFloatOESSupport(const Version & clientVersion,const Extensions & extensions)196 static bool UnsizedHalfFloatOESSupport(const Version &clientVersion, const Extensions &extensions)
197 {
198     return extensions.textureHalfFloat;
199 }
200 
201 // RGB + HALF_FLOAT_OES, RGBA + HALF_FLOAT_OES
UnsizedHalfFloatOESRenderableSupport(const Version & clientVersion,const Extensions & extensions)202 static bool UnsizedHalfFloatOESRenderableSupport(const Version &clientVersion,
203                                                  const Extensions &extensions)
204 {
205     return UnsizedHalfFloatOESSupport(clientVersion, extensions) && extensions.colorBufferHalfFloat;
206 }
207 
208 // Special function for float formats with three or four channels.
209 
210 // RGB32F, RGBA32F
FloatSupport(const Version & clientVersion,const Extensions & extensions)211 static bool FloatSupport(const Version &clientVersion, const Extensions &extensions)
212 {
213     return clientVersion >= Version(3, 0) || extensions.textureFloat;
214 }
215 
216 // RGB32F
FloatRGBRenderableSupport(const Version & clientVersion,const Extensions & extensions)217 static bool FloatRGBRenderableSupport(const Version &clientVersion, const Extensions &extensions)
218 {
219     return FloatSupport(clientVersion, extensions) && extensions.colorBufferFloatRGB;
220 }
221 
222 // RGBA32F
FloatRGBARenderableSupport(const Version & clientVersion,const Extensions & extensions)223 static bool FloatRGBARenderableSupport(const Version &clientVersion, const Extensions &extensions)
224 {
225     return FloatSupport(clientVersion, extensions) &&
226            (extensions.colorBufferFloat || extensions.colorBufferFloatRGBA);
227 }
228 
229 // RGB + FLOAT, RGBA + FLOAT
UnsizedFloatSupport(const Version & clientVersion,const Extensions & extensions)230 static bool UnsizedFloatSupport(const Version &clientVersion, const Extensions &extensions)
231 {
232     return extensions.textureFloat;
233 }
234 
235 // RGB + FLOAT
UnsizedFloatRGBRenderableSupport(const Version & clientVersion,const Extensions & extensions)236 static bool UnsizedFloatRGBRenderableSupport(const Version &clientVersion,
237                                              const Extensions &extensions)
238 {
239     return UnsizedFloatSupport(clientVersion, extensions) && extensions.colorBufferFloatRGB;
240 }
241 
242 // RGBA + FLOAT
UnsizedFloatRGBARenderableSupport(const Version & clientVersion,const Extensions & extensions)243 static bool UnsizedFloatRGBARenderableSupport(const Version &clientVersion,
244                                               const Extensions &extensions)
245 {
246     return UnsizedFloatSupport(clientVersion, extensions) &&
247            (extensions.colorBufferFloatRGBA || extensions.colorBufferFloat);
248 }
249 
250 // Special function for float formats with one or two channels.
251 
252 // R32F, RG32F
FloatRGSupport(const Version & clientVersion,const Extensions & extensions)253 static bool FloatRGSupport(const Version &clientVersion, const Extensions &extensions)
254 {
255     return clientVersion >= Version(3, 0) || (extensions.textureFloat && extensions.textureRG);
256 }
257 
258 // R32F, RG32F
FloatRGRenderableSupport(const Version & clientVersion,const Extensions & extensions)259 static bool FloatRGRenderableSupport(const Version &clientVersion, const Extensions &extensions)
260 {
261     return FloatRGSupport(clientVersion, extensions) && extensions.colorBufferFloat;
262 }
263 
264 // RED + FLOAT, RG + FLOAT
UnsizedFloatRGSupport(const Version & clientVersion,const Extensions & extensions)265 static bool UnsizedFloatRGSupport(const Version &clientVersion, const Extensions &extensions)
266 {
267     return extensions.textureFloat && extensions.textureRG;
268 }
269 
270 // RED + FLOAT, RG + FLOAT
UnsizedFloatRGRenderableSupport(const Version & clientVersion,const Extensions & extensions)271 static bool UnsizedFloatRGRenderableSupport(const Version &clientVersion,
272                                             const Extensions &extensions)
273 {
274     return FloatRGSupport(clientVersion, extensions) && extensions.colorBufferFloat;
275 }
276 
InternalFormat()277 InternalFormat::InternalFormat()
278     : internalFormat(GL_NONE),
279       sized(false),
280       sizedInternalFormat(GL_NONE),
281       redBits(0),
282       greenBits(0),
283       blueBits(0),
284       luminanceBits(0),
285       alphaBits(0),
286       sharedBits(0),
287       depthBits(0),
288       stencilBits(0),
289       pixelBytes(0),
290       componentCount(0),
291       compressed(false),
292       compressedBlockWidth(0),
293       compressedBlockHeight(0),
294       format(GL_NONE),
295       type(GL_NONE),
296       componentType(GL_NONE),
297       colorEncoding(GL_NONE),
298       textureSupport(NeverSupported),
299       renderSupport(NeverSupported),
300       filterSupport(NeverSupported)
301 {
302 }
303 
304 InternalFormat::InternalFormat(const InternalFormat &other) = default;
305 
isLUMA() const306 bool InternalFormat::isLUMA() const
307 {
308     return ((redBits + greenBits + blueBits + depthBits + stencilBits) == 0 &&
309             (luminanceBits + alphaBits) > 0);
310 }
311 
getReadPixelsFormat() const312 GLenum InternalFormat::getReadPixelsFormat() const
313 {
314     return format;
315 }
316 
getReadPixelsType(const Version & version) const317 GLenum InternalFormat::getReadPixelsType(const Version &version) const
318 {
319     switch (type)
320     {
321         case GL_HALF_FLOAT:
322         case GL_HALF_FLOAT_OES:
323             if (version < Version(3, 0))
324             {
325                 // The internal format may have a type of GL_HALF_FLOAT but when exposing this type
326                 // as the IMPLEMENTATION_READ_TYPE, only HALF_FLOAT_OES is allowed by
327                 // OES_texture_half_float.  HALF_FLOAT becomes core in ES3 and is acceptable to use
328                 // as an IMPLEMENTATION_READ_TYPE.
329                 return GL_HALF_FLOAT_OES;
330             }
331             else
332             {
333                 return GL_HALF_FLOAT;
334             }
335 
336         default:
337             return type;
338     }
339 }
340 
isRequiredRenderbufferFormat(const Version & version) const341 bool InternalFormat::isRequiredRenderbufferFormat(const Version &version) const
342 {
343     // GLES 3.0.5 section 4.4.2.2:
344     // "Implementations are required to support the same internal formats for renderbuffers as the
345     // required formats for textures enumerated in section 3.8.3.1, with the exception of the color
346     // formats labelled "texture-only"."
347     if (!sized || compressed)
348     {
349         return false;
350     }
351 
352     // Luma formats.
353     if (isLUMA())
354     {
355         return false;
356     }
357 
358     // Depth/stencil formats.
359     if (depthBits > 0 || stencilBits > 0)
360     {
361         // GLES 2.0.25 table 4.5.
362         // GLES 3.0.5 section 3.8.3.1.
363         // GLES 3.1 table 8.14.
364 
365         // Required formats in all versions.
366         switch (internalFormat)
367         {
368             case GL_DEPTH_COMPONENT16:
369             case GL_STENCIL_INDEX8:
370                 // Note that STENCIL_INDEX8 is not mentioned in GLES 3.0.5 section 3.8.3.1, but it
371                 // is in section 4.4.2.2.
372                 return true;
373             default:
374                 break;
375         }
376         if (version.major < 3)
377         {
378             return false;
379         }
380         // Required formats in GLES 3.0 and up.
381         switch (internalFormat)
382         {
383             case GL_DEPTH_COMPONENT32F:
384             case GL_DEPTH_COMPONENT24:
385             case GL_DEPTH32F_STENCIL8:
386             case GL_DEPTH24_STENCIL8:
387                 return true;
388             default:
389                 return false;
390         }
391     }
392 
393     // RGBA formats.
394     // GLES 2.0.25 table 4.5.
395     // GLES 3.0.5 section 3.8.3.1.
396     // GLES 3.1 table 8.13.
397 
398     // Required formats in all versions.
399     switch (internalFormat)
400     {
401         case GL_RGBA4:
402         case GL_RGB5_A1:
403         case GL_RGB565:
404             return true;
405         default:
406             break;
407     }
408     if (version.major < 3)
409     {
410         return false;
411     }
412 
413     if (format == GL_BGRA_EXT)
414     {
415         return false;
416     }
417 
418     switch (componentType)
419     {
420         case GL_SIGNED_NORMALIZED:
421         case GL_FLOAT:
422             return false;
423         case GL_UNSIGNED_INT:
424         case GL_INT:
425             // Integer RGB formats are not required renderbuffer formats.
426             if (alphaBits == 0 && blueBits != 0)
427             {
428                 return false;
429             }
430             // All integer R and RG formats are required.
431             // Integer RGBA formats including RGB10_A2_UI are required.
432             return true;
433         case GL_UNSIGNED_NORMALIZED:
434             if (internalFormat == GL_SRGB8)
435             {
436                 return false;
437             }
438             return true;
439         default:
440             UNREACHABLE();
441             return false;
442     }
443 }
444 
Format(GLenum internalFormat)445 Format::Format(GLenum internalFormat) : Format(GetSizedInternalFormatInfo(internalFormat))
446 {
447 }
448 
Format(const InternalFormat & internalFormat)449 Format::Format(const InternalFormat &internalFormat) : info(&internalFormat)
450 {
451 }
452 
Format(GLenum internalFormat,GLenum type)453 Format::Format(GLenum internalFormat, GLenum type)
454     : info(&GetInternalFormatInfo(internalFormat, type))
455 {
456 }
457 
458 Format::Format(const Format &other) = default;
459 Format &Format::operator=(const Format &other) = default;
460 
valid() const461 bool Format::valid() const
462 {
463     return info->internalFormat != GL_NONE;
464 }
465 
466 // static
SameSized(const Format & a,const Format & b)467 bool Format::SameSized(const Format &a, const Format &b)
468 {
469     return a.info->sizedInternalFormat == b.info->sizedInternalFormat;
470 }
471 
EquivalentBlitInternalFormat(GLenum internalformat)472 static GLenum EquivalentBlitInternalFormat(GLenum internalformat)
473 {
474     // BlitFramebuffer works if the color channels are identically
475     // sized, even if there is a swizzle (for example, blitting from a
476     // multisampled RGBA8 renderbuffer to a BGRA8 texture). This could
477     // be expanded and/or autogenerated if that is found necessary.
478     if (internalformat == GL_BGRA8_EXT)
479         return GL_RGBA8;
480     return internalformat;
481 }
482 
483 // static
EquivalentForBlit(const Format & a,const Format & b)484 bool Format::EquivalentForBlit(const Format &a, const Format &b)
485 {
486     return (EquivalentBlitInternalFormat(a.info->sizedInternalFormat) ==
487             EquivalentBlitInternalFormat(b.info->sizedInternalFormat));
488 }
489 
490 // static
Invalid()491 Format Format::Invalid()
492 {
493     static Format invalid(GL_NONE, GL_NONE);
494     return invalid;
495 }
496 
operator <<(std::ostream & os,const Format & fmt)497 std::ostream &operator<<(std::ostream &os, const Format &fmt)
498 {
499     // TODO(ynovikov): return string representation when available
500     return FmtHexShort(os, fmt.info->sizedInternalFormat);
501 }
502 
operator ==(const InternalFormat & other) const503 bool InternalFormat::operator==(const InternalFormat &other) const
504 {
505     // We assume all internal formats are unique if they have the same internal format and type
506     return internalFormat == other.internalFormat && type == other.type;
507 }
508 
operator !=(const InternalFormat & other) const509 bool InternalFormat::operator!=(const InternalFormat &other) const
510 {
511     return !(*this == other);
512 }
513 
InsertFormatInfo(InternalFormatInfoMap * map,const InternalFormat & formatInfo)514 void InsertFormatInfo(InternalFormatInfoMap *map, const InternalFormat &formatInfo)
515 {
516     ASSERT(!formatInfo.sized || (*map).count(formatInfo.internalFormat) == 0);
517     ASSERT((*map)[formatInfo.internalFormat].count(formatInfo.type) == 0);
518     (*map)[formatInfo.internalFormat][formatInfo.type] = formatInfo;
519 }
520 
AddRGBAFormat(InternalFormatInfoMap * map,GLenum internalFormat,bool sized,GLuint red,GLuint green,GLuint blue,GLuint alpha,GLuint shared,GLenum format,GLenum type,GLenum componentType,bool srgb,InternalFormat::SupportCheckFunction textureSupport,InternalFormat::SupportCheckFunction renderSupport,InternalFormat::SupportCheckFunction filterSupport)521 void AddRGBAFormat(InternalFormatInfoMap *map,
522                    GLenum internalFormat,
523                    bool sized,
524                    GLuint red,
525                    GLuint green,
526                    GLuint blue,
527                    GLuint alpha,
528                    GLuint shared,
529                    GLenum format,
530                    GLenum type,
531                    GLenum componentType,
532                    bool srgb,
533                    InternalFormat::SupportCheckFunction textureSupport,
534                    InternalFormat::SupportCheckFunction renderSupport,
535                    InternalFormat::SupportCheckFunction filterSupport)
536 {
537     InternalFormat formatInfo;
538     formatInfo.internalFormat = internalFormat;
539     formatInfo.sized          = sized;
540     formatInfo.sizedInternalFormat =
541         sized ? internalFormat : GetSizedFormatInternal(internalFormat, type);
542     formatInfo.redBits = red;
543     formatInfo.greenBits = green;
544     formatInfo.blueBits = blue;
545     formatInfo.alphaBits = alpha;
546     formatInfo.sharedBits = shared;
547     formatInfo.pixelBytes = (red + green + blue + alpha + shared) / 8;
548     formatInfo.componentCount =
549         ((red > 0) ? 1 : 0) + ((green > 0) ? 1 : 0) + ((blue > 0) ? 1 : 0) + ((alpha > 0) ? 1 : 0);
550     formatInfo.format = format;
551     formatInfo.type = type;
552     formatInfo.componentType = componentType;
553     formatInfo.colorEncoding = (srgb ? GL_SRGB : GL_LINEAR);
554     formatInfo.textureSupport = textureSupport;
555     formatInfo.renderSupport = renderSupport;
556     formatInfo.filterSupport = filterSupport;
557 
558     InsertFormatInfo(map, formatInfo);
559 }
560 
AddLUMAFormat(InternalFormatInfoMap * map,GLenum internalFormat,bool sized,GLuint luminance,GLuint alpha,GLenum format,GLenum type,GLenum componentType,InternalFormat::SupportCheckFunction textureSupport,InternalFormat::SupportCheckFunction renderSupport,InternalFormat::SupportCheckFunction filterSupport)561 static void AddLUMAFormat(InternalFormatInfoMap *map,
562                           GLenum internalFormat,
563                           bool sized,
564                           GLuint luminance,
565                           GLuint alpha,
566                           GLenum format,
567                           GLenum type,
568                           GLenum componentType,
569                           InternalFormat::SupportCheckFunction textureSupport,
570                           InternalFormat::SupportCheckFunction renderSupport,
571                           InternalFormat::SupportCheckFunction filterSupport)
572 {
573     InternalFormat formatInfo;
574     formatInfo.internalFormat = internalFormat;
575     formatInfo.sized          = sized;
576     formatInfo.sizedInternalFormat =
577         sized ? internalFormat : GetSizedFormatInternal(internalFormat, type);
578     formatInfo.luminanceBits = luminance;
579     formatInfo.alphaBits = alpha;
580     formatInfo.pixelBytes = (luminance + alpha) / 8;
581     formatInfo.componentCount = ((luminance > 0) ? 1 : 0) + ((alpha > 0) ? 1 : 0);
582     formatInfo.format = format;
583     formatInfo.type = type;
584     formatInfo.componentType = componentType;
585     formatInfo.colorEncoding = GL_LINEAR;
586     formatInfo.textureSupport = textureSupport;
587     formatInfo.renderSupport = renderSupport;
588     formatInfo.filterSupport = filterSupport;
589 
590     InsertFormatInfo(map, formatInfo);
591 }
592 
AddDepthStencilFormat(InternalFormatInfoMap * map,GLenum internalFormat,bool sized,GLuint depthBits,GLuint stencilBits,GLuint unusedBits,GLenum format,GLenum type,GLenum componentType,InternalFormat::SupportCheckFunction textureSupport,InternalFormat::SupportCheckFunction renderSupport,InternalFormat::SupportCheckFunction filterSupport)593 void AddDepthStencilFormat(InternalFormatInfoMap *map,
594                            GLenum internalFormat,
595                            bool sized,
596                            GLuint depthBits,
597                            GLuint stencilBits,
598                            GLuint unusedBits,
599                            GLenum format,
600                            GLenum type,
601                            GLenum componentType,
602                            InternalFormat::SupportCheckFunction textureSupport,
603                            InternalFormat::SupportCheckFunction renderSupport,
604                            InternalFormat::SupportCheckFunction filterSupport)
605 {
606     InternalFormat formatInfo;
607     formatInfo.internalFormat = internalFormat;
608     formatInfo.sized          = sized;
609     formatInfo.sizedInternalFormat =
610         sized ? internalFormat : GetSizedFormatInternal(internalFormat, type);
611     formatInfo.depthBits = depthBits;
612     formatInfo.stencilBits = stencilBits;
613     formatInfo.pixelBytes = (depthBits + stencilBits + unusedBits) / 8;
614     formatInfo.componentCount = ((depthBits > 0) ? 1 : 0) + ((stencilBits > 0) ? 1 : 0);
615     formatInfo.format = format;
616     formatInfo.type = type;
617     formatInfo.componentType = componentType;
618     formatInfo.colorEncoding = GL_LINEAR;
619     formatInfo.textureSupport = textureSupport;
620     formatInfo.renderSupport = renderSupport;
621     formatInfo.filterSupport = filterSupport;
622 
623     InsertFormatInfo(map, formatInfo);
624 }
625 
AddCompressedFormat(InternalFormatInfoMap * map,GLenum internalFormat,GLuint compressedBlockWidth,GLuint compressedBlockHeight,GLuint compressedBlockSize,GLuint componentCount,GLenum format,GLenum type,bool srgb,InternalFormat::SupportCheckFunction textureSupport,InternalFormat::SupportCheckFunction renderSupport,InternalFormat::SupportCheckFunction filterSupport)626 void AddCompressedFormat(InternalFormatInfoMap *map,
627                          GLenum internalFormat,
628                          GLuint compressedBlockWidth,
629                          GLuint compressedBlockHeight,
630                          GLuint compressedBlockSize,
631                          GLuint componentCount,
632                          GLenum format,
633                          GLenum type,
634                          bool srgb,
635                          InternalFormat::SupportCheckFunction textureSupport,
636                          InternalFormat::SupportCheckFunction renderSupport,
637                          InternalFormat::SupportCheckFunction filterSupport)
638 {
639     InternalFormat formatInfo;
640     formatInfo.internalFormat        = internalFormat;
641     formatInfo.sized                 = true;
642     formatInfo.sizedInternalFormat   = internalFormat;
643     formatInfo.compressedBlockWidth = compressedBlockWidth;
644     formatInfo.compressedBlockHeight = compressedBlockHeight;
645     formatInfo.pixelBytes = compressedBlockSize / 8;
646     formatInfo.componentCount = componentCount;
647     formatInfo.format = format;
648     formatInfo.type = type;
649     formatInfo.componentType = GL_UNSIGNED_NORMALIZED;
650     formatInfo.colorEncoding = (srgb ? GL_SRGB : GL_LINEAR);
651     formatInfo.compressed = true;
652     formatInfo.textureSupport = textureSupport;
653     formatInfo.renderSupport = renderSupport;
654     formatInfo.filterSupport = filterSupport;
655 
656     InsertFormatInfo(map, formatInfo);
657 }
658 
BuildInternalFormatInfoMap()659 static InternalFormatInfoMap BuildInternalFormatInfoMap()
660 {
661     InternalFormatInfoMap map;
662 
663     // From ES 3.0.1 spec, table 3.12
664     map[GL_NONE][GL_NONE] = InternalFormat();
665 
666     // clang-format off
667 
668     //                 | Internal format    |sized| R | G | B | A |S | Format         | Type                           | Component type        | SRGB | Texture supported                           | Renderable                                  | Filterable    |
669     AddRGBAFormat(&map, GL_R8,               true,  8,  0,  0,  0, 0, GL_RED,          GL_UNSIGNED_BYTE,                GL_UNSIGNED_NORMALIZED, false, RequireESOrExt<3, 0, &Extensions::textureRG>, RequireESOrExt<3, 0, &Extensions::textureRG>, AlwaysSupported);
670     AddRGBAFormat(&map, GL_R8_SNORM,         true,  8,  0,  0,  0, 0, GL_RED,          GL_BYTE,                         GL_SIGNED_NORMALIZED,   false, RequireES<3, 0>,                              NeverSupported,                               AlwaysSupported);
671     AddRGBAFormat(&map, GL_RG8,              true,  8,  8,  0,  0, 0, GL_RG,           GL_UNSIGNED_BYTE,                GL_UNSIGNED_NORMALIZED, false, RequireESOrExt<3, 0, &Extensions::textureRG>, RequireESOrExt<3, 0, &Extensions::textureRG>, AlwaysSupported);
672     AddRGBAFormat(&map, GL_RG8_SNORM,        true,  8,  8,  0,  0, 0, GL_RG,           GL_BYTE,                         GL_SIGNED_NORMALIZED,   false, RequireES<3, 0>,                              NeverSupported,                               AlwaysSupported);
673     AddRGBAFormat(&map, GL_RGB8,             true,  8,  8,  8,  0, 0, GL_RGB,          GL_UNSIGNED_BYTE,                GL_UNSIGNED_NORMALIZED, false, RequireESOrExt<3, 0, &Extensions::rgb8rgba8>, RequireESOrExt<3, 0, &Extensions::rgb8rgba8>, AlwaysSupported);
674     AddRGBAFormat(&map, GL_RGB8_SNORM,       true,  8,  8,  8,  0, 0, GL_RGB,          GL_BYTE,                         GL_SIGNED_NORMALIZED,   false, RequireES<3, 0>,                              NeverSupported,                               AlwaysSupported);
675     AddRGBAFormat(&map, GL_RGB565,           true,  5,  6,  5,  0, 0, GL_RGB,          GL_UNSIGNED_SHORT_5_6_5,         GL_UNSIGNED_NORMALIZED, false, RequireES<2, 0>,                              RequireES<2, 0>,                              AlwaysSupported);
676     AddRGBAFormat(&map, GL_RGBA4,            true,  4,  4,  4,  4, 0, GL_RGBA,         GL_UNSIGNED_SHORT_4_4_4_4,       GL_UNSIGNED_NORMALIZED, false, RequireES<2, 0>,                              RequireES<2, 0>,                              AlwaysSupported);
677     AddRGBAFormat(&map, GL_RGB5_A1,          true,  5,  5,  5,  1, 0, GL_RGBA,         GL_UNSIGNED_SHORT_5_5_5_1,       GL_UNSIGNED_NORMALIZED, false, RequireES<2, 0>,                              RequireES<2, 0>,                              AlwaysSupported);
678     AddRGBAFormat(&map, GL_RGBA8,            true,  8,  8,  8,  8, 0, GL_RGBA,         GL_UNSIGNED_BYTE,                GL_UNSIGNED_NORMALIZED, false, RequireESOrExt<3, 0, &Extensions::rgb8rgba8>, RequireESOrExt<3, 0, &Extensions::rgb8rgba8>, AlwaysSupported);
679     AddRGBAFormat(&map, GL_RGBA8_SNORM,      true,  8,  8,  8,  8, 0, GL_RGBA,         GL_BYTE,                         GL_SIGNED_NORMALIZED,   false, RequireES<3, 0>,                              NeverSupported,                               AlwaysSupported);
680     AddRGBAFormat(&map, GL_RGB10_A2,         true, 10, 10, 10,  2, 0, GL_RGBA,         GL_UNSIGNED_INT_2_10_10_10_REV,  GL_UNSIGNED_NORMALIZED, false, RequireES<3, 0>,                              RequireES<3, 0>,                              AlwaysSupported);
681     AddRGBAFormat(&map, GL_RGB10_A2UI,       true, 10, 10, 10,  2, 0, GL_RGBA_INTEGER, GL_UNSIGNED_INT_2_10_10_10_REV,  GL_UNSIGNED_INT,        false, RequireES<3, 0>,                              RequireES<3, 0>,                              NeverSupported);
682     AddRGBAFormat(&map, GL_SRGB8,            true,  8,  8,  8,  0, 0, GL_RGB,          GL_UNSIGNED_BYTE,                GL_UNSIGNED_NORMALIZED, true,  RequireESOrExt<3, 0, &Extensions::sRGB>,      NeverSupported,                               AlwaysSupported);
683     AddRGBAFormat(&map, GL_SRGB8_ALPHA8,     true,  8,  8,  8,  8, 0, GL_RGBA,         GL_UNSIGNED_BYTE,                GL_UNSIGNED_NORMALIZED, true,  RequireESOrExt<3, 0, &Extensions::sRGB>,      RequireESOrExt<3, 0, &Extensions::sRGB>,      AlwaysSupported);
684     AddRGBAFormat(&map, GL_RGB9_E5,          true,  9,  9,  9,  0, 5, GL_RGB,          GL_UNSIGNED_INT_5_9_9_9_REV,     GL_FLOAT,               false, RequireES<3, 0>,                              NeverSupported,                               AlwaysSupported);
685     AddRGBAFormat(&map, GL_R8I,              true,  8,  0,  0,  0, 0, GL_RED_INTEGER,  GL_BYTE,                         GL_INT,                 false, RequireES<3, 0>,                              RequireES<3, 0>,                              NeverSupported);
686     AddRGBAFormat(&map, GL_R8UI,             true,  8,  0,  0,  0, 0, GL_RED_INTEGER,  GL_UNSIGNED_BYTE,                GL_UNSIGNED_INT,        false, RequireES<3, 0>,                              RequireES<3, 0>,                              NeverSupported);
687     AddRGBAFormat(&map, GL_R16I,             true, 16,  0,  0,  0, 0, GL_RED_INTEGER,  GL_SHORT,                        GL_INT,                 false, RequireES<3, 0>,                              RequireES<3, 0>,                              NeverSupported);
688     AddRGBAFormat(&map, GL_R16UI,            true, 16,  0,  0,  0, 0, GL_RED_INTEGER,  GL_UNSIGNED_SHORT,               GL_UNSIGNED_INT,        false, RequireES<3, 0>,                              RequireES<3, 0>,                              NeverSupported);
689     AddRGBAFormat(&map, GL_R32I,             true, 32,  0,  0,  0, 0, GL_RED_INTEGER,  GL_INT,                          GL_INT,                 false, RequireES<3, 0>,                              RequireES<3, 0>,                              NeverSupported);
690     AddRGBAFormat(&map, GL_R32UI,            true, 32,  0,  0,  0, 0, GL_RED_INTEGER,  GL_UNSIGNED_INT,                 GL_UNSIGNED_INT,        false, RequireES<3, 0>,                              RequireES<3, 0>,                              NeverSupported);
691     AddRGBAFormat(&map, GL_RG8I,             true,  8,  8,  0,  0, 0, GL_RG_INTEGER,   GL_BYTE,                         GL_INT,                 false, RequireES<3, 0>,                              RequireES<3, 0>,                              NeverSupported);
692     AddRGBAFormat(&map, GL_RG8UI,            true,  8,  8,  0,  0, 0, GL_RG_INTEGER,   GL_UNSIGNED_BYTE,                GL_UNSIGNED_INT,        false, RequireES<3, 0>,                              RequireES<3, 0>,                              NeverSupported);
693     AddRGBAFormat(&map, GL_RG16I,            true, 16, 16,  0,  0, 0, GL_RG_INTEGER,   GL_SHORT,                        GL_INT,                 false, RequireES<3, 0>,                              RequireES<3, 0>,                              NeverSupported);
694     AddRGBAFormat(&map, GL_RG16UI,           true, 16, 16,  0,  0, 0, GL_RG_INTEGER,   GL_UNSIGNED_SHORT,               GL_UNSIGNED_INT,        false, RequireES<3, 0>,                              RequireES<3, 0>,                              NeverSupported);
695     AddRGBAFormat(&map, GL_RG32I,            true, 32, 32,  0,  0, 0, GL_RG_INTEGER,   GL_INT,                          GL_INT,                 false, RequireES<3, 0>,                              RequireES<3, 0>,                              NeverSupported);
696     AddRGBAFormat(&map, GL_R11F_G11F_B10F,   true, 11, 11, 10,  0, 0, GL_RGB,          GL_UNSIGNED_INT_10F_11F_11F_REV, GL_FLOAT,               false, RequireES<3, 0>,                              RequireExt<&Extensions::colorBufferFloat>,    AlwaysSupported);
697     AddRGBAFormat(&map, GL_RG32UI,           true, 32, 32,  0,  0, 0, GL_RG_INTEGER,   GL_UNSIGNED_INT,                 GL_UNSIGNED_INT,        false, RequireES<3, 0>,                              RequireES<3, 0>,                              NeverSupported);
698     AddRGBAFormat(&map, GL_RGB8I,            true,  8,  8,  8,  0, 0, GL_RGB_INTEGER,  GL_BYTE,                         GL_INT,                 false, RequireES<3, 0>,                              NeverSupported,                               NeverSupported);
699     AddRGBAFormat(&map, GL_RGB8UI,           true,  8,  8,  8,  0, 0, GL_RGB_INTEGER,  GL_UNSIGNED_BYTE,                GL_UNSIGNED_INT,        false, RequireES<3, 0>,                              NeverSupported,                               NeverSupported);
700     AddRGBAFormat(&map, GL_RGB16I,           true, 16, 16, 16,  0, 0, GL_RGB_INTEGER,  GL_SHORT,                        GL_INT,                 false, RequireES<3, 0>,                              NeverSupported,                               NeverSupported);
701     AddRGBAFormat(&map, GL_RGB16UI,          true, 16, 16, 16,  0, 0, GL_RGB_INTEGER,  GL_UNSIGNED_SHORT,               GL_UNSIGNED_INT,        false, RequireES<3, 0>,                              NeverSupported,                               NeverSupported);
702     AddRGBAFormat(&map, GL_RGB32I,           true, 32, 32, 32,  0, 0, GL_RGB_INTEGER,  GL_INT,                          GL_INT,                 false, RequireES<3, 0>,                              NeverSupported,                               NeverSupported);
703     AddRGBAFormat(&map, GL_RGB32UI,          true, 32, 32, 32,  0, 0, GL_RGB_INTEGER,  GL_UNSIGNED_INT,                 GL_UNSIGNED_INT,        false, RequireES<3, 0>,                              NeverSupported,                               NeverSupported);
704     AddRGBAFormat(&map, GL_RGBA8I,           true,  8,  8,  8,  8, 0, GL_RGBA_INTEGER, GL_BYTE,                         GL_INT,                 false, RequireES<3, 0>,                              RequireES<3, 0>,                              NeverSupported);
705     AddRGBAFormat(&map, GL_RGBA8UI,          true,  8,  8,  8,  8, 0, GL_RGBA_INTEGER, GL_UNSIGNED_BYTE,                GL_UNSIGNED_INT,        false, RequireES<3, 0>,                              RequireES<3, 0>,                              NeverSupported);
706     AddRGBAFormat(&map, GL_RGBA16I,          true, 16, 16, 16, 16, 0, GL_RGBA_INTEGER, GL_SHORT,                        GL_INT,                 false, RequireES<3, 0>,                              RequireES<3, 0>,                              NeverSupported);
707     AddRGBAFormat(&map, GL_RGBA16UI,         true, 16, 16, 16, 16, 0, GL_RGBA_INTEGER, GL_UNSIGNED_SHORT,               GL_UNSIGNED_INT,        false, RequireES<3, 0>,                              RequireES<3, 0>,                              NeverSupported);
708     AddRGBAFormat(&map, GL_RGBA32I,          true, 32, 32, 32, 32, 0, GL_RGBA_INTEGER, GL_INT,                          GL_INT,                 false, RequireES<3, 0>,                              RequireES<3, 0>,                              NeverSupported);
709     AddRGBAFormat(&map, GL_RGBA32UI,         true, 32, 32, 32, 32, 0, GL_RGBA_INTEGER, GL_UNSIGNED_INT,                 GL_UNSIGNED_INT,        false, RequireES<3, 0>,                              RequireES<3, 0>,                              NeverSupported);
710 
711     AddRGBAFormat(&map, GL_BGRA8_EXT,        true,  8,  8,  8,  8, 0, GL_BGRA_EXT,     GL_UNSIGNED_BYTE,                  GL_UNSIGNED_NORMALIZED, false, RequireExt<&Extensions::textureFormatBGRA8888>, RequireExt<&Extensions::textureFormatBGRA8888>, AlwaysSupported);
712     AddRGBAFormat(&map, GL_BGRA4_ANGLEX,     true,  4,  4,  4,  4, 0, GL_BGRA_EXT,     GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT, GL_UNSIGNED_NORMALIZED, false, RequireExt<&Extensions::textureFormatBGRA8888>, RequireExt<&Extensions::textureFormatBGRA8888>, AlwaysSupported);
713     AddRGBAFormat(&map, GL_BGR5_A1_ANGLEX,   true,  5,  5,  5,  1, 0, GL_BGRA_EXT,     GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT, GL_UNSIGNED_NORMALIZED, false, RequireExt<&Extensions::textureFormatBGRA8888>, RequireExt<&Extensions::textureFormatBGRA8888>, AlwaysSupported);
714 
715     // Special format which is not really supported, so always false for all supports.
716     AddRGBAFormat(&map, GL_BGR565_ANGLEX,    true,  5,  6,  5,  1, 0, GL_BGRA_EXT,     GL_UNSIGNED_SHORT_5_6_5,           GL_UNSIGNED_NORMALIZED, false, NeverSupported, NeverSupported, NeverSupported);
717 
718     // Floating point renderability and filtering is provided by OES_texture_float and OES_texture_half_float
719     //                 | Internal format    |sized| D |S | Format             | Type                                   | Comp   | SRGB |  Texture supported | Renderable                    | Filterable                                    |
720     //                 |                    |     |   |  |                    |                                        | type   |      |                    |                               |                                               |
721     AddRGBAFormat(&map, GL_R16F,             true, 16,  0,  0,  0, 0, GL_RED,          GL_HALF_FLOAT,                   GL_FLOAT, false, HalfFloatRGSupport, HalfFloatRGRenderableSupport,   RequireESOrExt<3, 0, &Extensions::textureHalfFloatLinear>);
722     AddRGBAFormat(&map, GL_RG16F,            true, 16, 16,  0,  0, 0, GL_RG,           GL_HALF_FLOAT,                   GL_FLOAT, false, HalfFloatRGSupport, HalfFloatRGRenderableSupport,   RequireESOrExt<3, 0, &Extensions::textureHalfFloatLinear>);
723     AddRGBAFormat(&map, GL_RGB16F,           true, 16, 16, 16,  0, 0, GL_RGB,          GL_HALF_FLOAT,                   GL_FLOAT, false, HalfFloatSupport,   HalfFloatRGBRenderableSupport,  RequireESOrExt<3, 0, &Extensions::textureHalfFloatLinear>);
724     AddRGBAFormat(&map, GL_RGBA16F,          true, 16, 16, 16, 16, 0, GL_RGBA,         GL_HALF_FLOAT,                   GL_FLOAT, false, HalfFloatSupport,   HalfFloatRGBARenderableSupport, RequireESOrExt<3, 0, &Extensions::textureHalfFloatLinear>);
725     AddRGBAFormat(&map, GL_R32F,             true, 32,  0,  0,  0, 0, GL_RED,          GL_FLOAT,                        GL_FLOAT, false, FloatRGSupport,     FloatRGRenderableSupport,       RequireExt<&Extensions::textureFloatLinear>              );
726     AddRGBAFormat(&map, GL_RG32F,            true, 32, 32,  0,  0, 0, GL_RG,           GL_FLOAT,                        GL_FLOAT, false, FloatRGSupport,     FloatRGRenderableSupport,       RequireExt<&Extensions::textureFloatLinear>              );
727     AddRGBAFormat(&map, GL_RGB32F,           true, 32, 32, 32,  0, 0, GL_RGB,          GL_FLOAT,                        GL_FLOAT, false, FloatSupport,       FloatRGBRenderableSupport,      RequireExt<&Extensions::textureFloatLinear>              );
728     AddRGBAFormat(&map, GL_RGBA32F,          true, 32, 32, 32, 32, 0, GL_RGBA,         GL_FLOAT,                        GL_FLOAT, false, FloatSupport,       FloatRGBARenderableSupport,     RequireExt<&Extensions::textureFloatLinear>              );
729 
730     // Depth stencil formats
731     //                         | Internal format         |sized| D |S | X | Format            | Type                             | Component type        | Supported                                       | Renderable                                                                            | Filterable                                  |
732     AddDepthStencilFormat(&map, GL_DEPTH_COMPONENT16,     true, 16, 0,  0, GL_DEPTH_COMPONENT, GL_UNSIGNED_SHORT,                 GL_UNSIGNED_NORMALIZED, RequireES<2, 0>,                                  RequireES<2, 0>,                                                                        RequireESOrExt<3, 0, &Extensions::depthTextures>);
733     AddDepthStencilFormat(&map, GL_DEPTH_COMPONENT24,     true, 24, 0,  0, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT,                   GL_UNSIGNED_NORMALIZED, RequireES<3, 0>,                                  RequireES<3, 0>,                                                                        RequireESOrExt<3, 0, &Extensions::depthTextures>);
734     AddDepthStencilFormat(&map, GL_DEPTH_COMPONENT32F,    true, 32, 0,  0, GL_DEPTH_COMPONENT, GL_FLOAT,                          GL_FLOAT,               RequireES<3, 0>,                                  RequireES<3, 0>,                                                                        RequireESOrExt<3, 0, &Extensions::depthTextures>);
735     AddDepthStencilFormat(&map, GL_DEPTH_COMPONENT32_OES, true, 32, 0,  0, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT,                   GL_UNSIGNED_NORMALIZED, RequireExtOrExt<&Extensions::depthTextures, &Extensions::depth32>, RequireExtOrExt<&Extensions::depthTextures, &Extensions::depth32>,     AlwaysSupported                                 );
736     AddDepthStencilFormat(&map, GL_DEPTH24_STENCIL8,      true, 24, 8,  0, GL_DEPTH_STENCIL,   GL_UNSIGNED_INT_24_8,              GL_UNSIGNED_NORMALIZED, RequireESOrExt<3, 0, &Extensions::depthTextures>, RequireESOrExtOrExt<3, 0, &Extensions::depthTextures, &Extensions::packedDepthStencil>, AlwaysSupported                                 );
737     AddDepthStencilFormat(&map, GL_DEPTH32F_STENCIL8,     true, 32, 8, 24, GL_DEPTH_STENCIL,   GL_FLOAT_32_UNSIGNED_INT_24_8_REV, GL_FLOAT,               RequireES<3, 0>,                                  RequireES<3, 0>,                                                                        AlwaysSupported                                 );
738     // STENCIL_INDEX8 is special-cased, see around the bottom of the list.
739 
740     // Luminance alpha formats
741     //                | Internal format           |sized| L | A | Format            | Type             | Component type        | Supported                                                                   | Renderable    | Filterable    |
742     AddLUMAFormat(&map, GL_ALPHA8_EXT,             true,  0,  8, GL_ALPHA,           GL_UNSIGNED_BYTE,  GL_UNSIGNED_NORMALIZED, RequireExt<&Extensions::textureStorage>,                                      NeverSupported, AlwaysSupported);
743     AddLUMAFormat(&map, GL_LUMINANCE8_EXT,         true,  8,  0, GL_LUMINANCE,       GL_UNSIGNED_BYTE,  GL_UNSIGNED_NORMALIZED, RequireExt<&Extensions::textureStorage>,                                      NeverSupported, AlwaysSupported);
744     AddLUMAFormat(&map, GL_LUMINANCE8_ALPHA8_EXT,  true,  8,  8, GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE,  GL_UNSIGNED_NORMALIZED, RequireExt<&Extensions::textureStorage>,                                      NeverSupported, AlwaysSupported);
745     AddLUMAFormat(&map, GL_ALPHA16F_EXT,           true,  0, 16, GL_ALPHA,           GL_HALF_FLOAT_OES, GL_FLOAT,               RequireExtAndExt<&Extensions::textureStorage, &Extensions::textureHalfFloat>, NeverSupported, RequireESOrExt<3, 0, &Extensions::textureHalfFloatLinear>);
746     AddLUMAFormat(&map, GL_LUMINANCE16F_EXT,       true, 16,  0, GL_LUMINANCE,       GL_HALF_FLOAT_OES, GL_FLOAT,               RequireExtAndExt<&Extensions::textureStorage, &Extensions::textureHalfFloat>, NeverSupported, RequireESOrExt<3, 0, &Extensions::textureHalfFloatLinear>);
747     AddLUMAFormat(&map, GL_LUMINANCE_ALPHA16F_EXT, true, 16, 16, GL_LUMINANCE_ALPHA, GL_HALF_FLOAT_OES, GL_FLOAT,               RequireExtAndExt<&Extensions::textureStorage, &Extensions::textureHalfFloat>, NeverSupported, RequireESOrExt<3, 0, &Extensions::textureHalfFloatLinear>);
748     AddLUMAFormat(&map, GL_ALPHA32F_EXT,           true,  0, 32, GL_ALPHA,           GL_FLOAT,          GL_FLOAT,               RequireExtAndExt<&Extensions::textureStorage, &Extensions::textureFloat>,     NeverSupported, RequireExt<&Extensions::textureFloatLinear>);
749     AddLUMAFormat(&map, GL_LUMINANCE32F_EXT,       true, 32,  0, GL_LUMINANCE,       GL_FLOAT,          GL_FLOAT,               RequireExtAndExt<&Extensions::textureStorage, &Extensions::textureFloat>,     NeverSupported, RequireExt<&Extensions::textureFloatLinear>);
750     AddLUMAFormat(&map, GL_LUMINANCE_ALPHA32F_EXT, true, 32, 32, GL_LUMINANCE_ALPHA, GL_FLOAT,          GL_FLOAT,               RequireExtAndExt<&Extensions::textureStorage, &Extensions::textureFloat>,     NeverSupported, RequireExt<&Extensions::textureFloatLinear>);
751 
752     // Compressed formats, From ES 3.0.1 spec, table 3.16
753     //                       | Internal format                             |W |H | BS |CC| Format | Type            | SRGB | Supported      | Renderable    | Filterable    |
754     AddCompressedFormat(&map, GL_COMPRESSED_R11_EAC,                        4, 4,  64, 1, GL_RED,  GL_UNSIGNED_BYTE, false, RequireES<3, 0>, NeverSupported, AlwaysSupported);
755     AddCompressedFormat(&map, GL_COMPRESSED_SIGNED_R11_EAC,                 4, 4,  64, 1, GL_RED,  GL_UNSIGNED_BYTE, false, RequireES<3, 0>, NeverSupported, AlwaysSupported);
756     AddCompressedFormat(&map, GL_COMPRESSED_RG11_EAC,                       4, 4, 128, 2, GL_RG,   GL_UNSIGNED_BYTE, false, RequireES<3, 0>, NeverSupported, AlwaysSupported);
757     AddCompressedFormat(&map, GL_COMPRESSED_SIGNED_RG11_EAC,                4, 4, 128, 2, GL_RG,   GL_UNSIGNED_BYTE, false, RequireES<3, 0>, NeverSupported, AlwaysSupported);
758     AddCompressedFormat(&map, GL_COMPRESSED_RGB8_ETC2,                      4, 4,  64, 3, GL_RGB,  GL_UNSIGNED_BYTE, false, RequireES<3, 0>, NeverSupported, AlwaysSupported);
759     AddCompressedFormat(&map, GL_COMPRESSED_SRGB8_ETC2,                     4, 4,  64, 3, GL_RGB,  GL_UNSIGNED_BYTE, true,  RequireES<3, 0>, NeverSupported, AlwaysSupported);
760     AddCompressedFormat(&map, GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2,  4, 4,  64, 3, GL_RGB,  GL_UNSIGNED_BYTE, false, RequireES<3, 0>, NeverSupported, AlwaysSupported);
761     AddCompressedFormat(&map, GL_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2, 4, 4,  64, 3, GL_RGB,  GL_UNSIGNED_BYTE, true,  RequireES<3, 0>, NeverSupported, AlwaysSupported);
762     AddCompressedFormat(&map, GL_COMPRESSED_RGBA8_ETC2_EAC,                 4, 4, 128, 4, GL_RGBA, GL_UNSIGNED_BYTE, false, RequireES<3, 0>, NeverSupported, AlwaysSupported);
763     AddCompressedFormat(&map, GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC,          4, 4, 128, 4, GL_RGBA, GL_UNSIGNED_BYTE, true,  RequireES<3, 0>, NeverSupported, AlwaysSupported);
764 
765     // From GL_EXT_texture_compression_dxt1
766     //                       | Internal format                   |W |H | BS |CC| Format | Type            | SRGB | Supported                                         | Renderable    | Filterable    |
767     AddCompressedFormat(&map, GL_COMPRESSED_RGB_S3TC_DXT1_EXT,    4, 4,  64, 3, GL_RGB,  GL_UNSIGNED_BYTE, false, RequireExt<&Extensions::textureCompressionDXT1>,    NeverSupported, AlwaysSupported);
768     AddCompressedFormat(&map, GL_COMPRESSED_RGBA_S3TC_DXT1_EXT,   4, 4,  64, 4, GL_RGBA, GL_UNSIGNED_BYTE, false, RequireExt<&Extensions::textureCompressionDXT1>,    NeverSupported, AlwaysSupported);
769 
770     // From GL_ANGLE_texture_compression_dxt3
771     AddCompressedFormat(&map, GL_COMPRESSED_RGBA_S3TC_DXT3_ANGLE, 4, 4, 128, 4, GL_RGBA, GL_UNSIGNED_BYTE, false, RequireExt<&Extensions::textureCompressionDXT3>,    NeverSupported, AlwaysSupported);
772 
773     // From GL_ANGLE_texture_compression_dxt5
774     AddCompressedFormat(&map, GL_COMPRESSED_RGBA_S3TC_DXT5_ANGLE, 4, 4, 128, 4, GL_RGBA, GL_UNSIGNED_BYTE, false, RequireExt<&Extensions::textureCompressionDXT5>,    NeverSupported, AlwaysSupported);
775 
776     // From GL_OES_compressed_ETC1_RGB8_texture
777     AddCompressedFormat(&map, GL_ETC1_RGB8_OES,                   4, 4,  64, 3, GL_RGB,  GL_UNSIGNED_BYTE, false, RequireExt<&Extensions::compressedETC1RGB8Texture>, NeverSupported, AlwaysSupported);
778 
779     // From GL_EXT_texture_compression_s3tc_srgb
780     //                       | Internal format                       |W |H | BS |CC| Format | Type            | SRGB | Supported                                         | Renderable    | Filterable    |
781     AddCompressedFormat(&map, GL_COMPRESSED_SRGB_S3TC_DXT1_EXT,       4, 4,  64, 3, GL_RGB,  GL_UNSIGNED_BYTE, true, RequireExt<&Extensions::textureCompressionS3TCsRGB>, NeverSupported, AlwaysSupported);
782     AddCompressedFormat(&map, GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT, 4, 4,  64, 4, GL_RGBA, GL_UNSIGNED_BYTE, true, RequireExt<&Extensions::textureCompressionS3TCsRGB>, NeverSupported, AlwaysSupported);
783     AddCompressedFormat(&map, GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT, 4, 4, 128, 4, GL_RGBA, GL_UNSIGNED_BYTE, true, RequireExt<&Extensions::textureCompressionS3TCsRGB>, NeverSupported, AlwaysSupported);
784     AddCompressedFormat(&map, GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT, 4, 4, 128, 4, GL_RGBA, GL_UNSIGNED_BYTE, true, RequireExt<&Extensions::textureCompressionS3TCsRGB>, NeverSupported, AlwaysSupported);
785 
786     // From KHR_texture_compression_astc_hdr
787     //                       | Internal format                          | W | H | BS |CC| Format | Type            | SRGB | Supported                                                                                     | Renderable     | Filterable    |
788     AddCompressedFormat(&map, GL_COMPRESSED_RGBA_ASTC_4x4_KHR,            4,  4, 128, 4, GL_RGBA, GL_UNSIGNED_BYTE, false, RequireExtOrExt<&Extensions::textureCompressionASTCHDR, &Extensions::textureCompressionASTCLDR>, NeverSupported, AlwaysSupported);
789     AddCompressedFormat(&map, GL_COMPRESSED_RGBA_ASTC_5x4_KHR,            5,  4, 128, 4, GL_RGBA, GL_UNSIGNED_BYTE, false, RequireExtOrExt<&Extensions::textureCompressionASTCHDR, &Extensions::textureCompressionASTCLDR>, NeverSupported, AlwaysSupported);
790     AddCompressedFormat(&map, GL_COMPRESSED_RGBA_ASTC_5x5_KHR,            5,  5, 128, 4, GL_RGBA, GL_UNSIGNED_BYTE, false, RequireExtOrExt<&Extensions::textureCompressionASTCHDR, &Extensions::textureCompressionASTCLDR>, NeverSupported, AlwaysSupported);
791     AddCompressedFormat(&map, GL_COMPRESSED_RGBA_ASTC_6x5_KHR,            6,  5, 128, 4, GL_RGBA, GL_UNSIGNED_BYTE, false, RequireExtOrExt<&Extensions::textureCompressionASTCHDR, &Extensions::textureCompressionASTCLDR>, NeverSupported, AlwaysSupported);
792     AddCompressedFormat(&map, GL_COMPRESSED_RGBA_ASTC_6x6_KHR,            6,  6, 128, 4, GL_RGBA, GL_UNSIGNED_BYTE, false, RequireExtOrExt<&Extensions::textureCompressionASTCHDR, &Extensions::textureCompressionASTCLDR>, NeverSupported, AlwaysSupported);
793     AddCompressedFormat(&map, GL_COMPRESSED_RGBA_ASTC_8x5_KHR,            8,  5, 128, 4, GL_RGBA, GL_UNSIGNED_BYTE, false, RequireExtOrExt<&Extensions::textureCompressionASTCHDR, &Extensions::textureCompressionASTCLDR>, NeverSupported, AlwaysSupported);
794     AddCompressedFormat(&map, GL_COMPRESSED_RGBA_ASTC_8x6_KHR,            8,  6, 128, 4, GL_RGBA, GL_UNSIGNED_BYTE, false, RequireExtOrExt<&Extensions::textureCompressionASTCHDR, &Extensions::textureCompressionASTCLDR>, NeverSupported, AlwaysSupported);
795     AddCompressedFormat(&map, GL_COMPRESSED_RGBA_ASTC_8x8_KHR,            8,  8, 128, 4, GL_RGBA, GL_UNSIGNED_BYTE, false, RequireExtOrExt<&Extensions::textureCompressionASTCHDR, &Extensions::textureCompressionASTCLDR>, NeverSupported, AlwaysSupported);
796     AddCompressedFormat(&map, GL_COMPRESSED_RGBA_ASTC_10x5_KHR,          10,  5, 128, 4, GL_RGBA, GL_UNSIGNED_BYTE, false, RequireExtOrExt<&Extensions::textureCompressionASTCHDR, &Extensions::textureCompressionASTCLDR>, NeverSupported, AlwaysSupported);
797     AddCompressedFormat(&map, GL_COMPRESSED_RGBA_ASTC_10x6_KHR,          10,  6, 128, 4, GL_RGBA, GL_UNSIGNED_BYTE, false, RequireExtOrExt<&Extensions::textureCompressionASTCHDR, &Extensions::textureCompressionASTCLDR>, NeverSupported, AlwaysSupported);
798     AddCompressedFormat(&map, GL_COMPRESSED_RGBA_ASTC_10x8_KHR,          10,  8, 128, 4, GL_RGBA, GL_UNSIGNED_BYTE, false, RequireExtOrExt<&Extensions::textureCompressionASTCHDR, &Extensions::textureCompressionASTCLDR>, NeverSupported, AlwaysSupported);
799     AddCompressedFormat(&map, GL_COMPRESSED_RGBA_ASTC_10x10_KHR,         10, 10, 128, 4, GL_RGBA, GL_UNSIGNED_BYTE, false, RequireExtOrExt<&Extensions::textureCompressionASTCHDR, &Extensions::textureCompressionASTCLDR>, NeverSupported, AlwaysSupported);
800     AddCompressedFormat(&map, GL_COMPRESSED_RGBA_ASTC_12x10_KHR,         12, 10, 128, 4, GL_RGBA, GL_UNSIGNED_BYTE, false, RequireExtOrExt<&Extensions::textureCompressionASTCHDR, &Extensions::textureCompressionASTCLDR>, NeverSupported, AlwaysSupported);
801     AddCompressedFormat(&map, GL_COMPRESSED_RGBA_ASTC_12x12_KHR,         12, 12, 128, 4, GL_RGBA, GL_UNSIGNED_BYTE, false, RequireExtOrExt<&Extensions::textureCompressionASTCHDR, &Extensions::textureCompressionASTCLDR>, NeverSupported, AlwaysSupported);
802 
803     AddCompressedFormat(&map, GL_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR,    4,  4, 128, 4, GL_RGBA, GL_UNSIGNED_BYTE, true,  RequireExtOrExt<&Extensions::textureCompressionASTCHDR, &Extensions::textureCompressionASTCLDR>, NeverSupported, AlwaysSupported);
804     AddCompressedFormat(&map, GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR,    5,  4, 128, 4, GL_RGBA, GL_UNSIGNED_BYTE, true,  RequireExtOrExt<&Extensions::textureCompressionASTCHDR, &Extensions::textureCompressionASTCLDR>, NeverSupported, AlwaysSupported);
805     AddCompressedFormat(&map, GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR,    5,  5, 128, 4, GL_RGBA, GL_UNSIGNED_BYTE, true,  RequireExtOrExt<&Extensions::textureCompressionASTCHDR, &Extensions::textureCompressionASTCLDR>, NeverSupported, AlwaysSupported);
806     AddCompressedFormat(&map, GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR,    6,  5, 128, 4, GL_RGBA, GL_UNSIGNED_BYTE, true,  RequireExtOrExt<&Extensions::textureCompressionASTCHDR, &Extensions::textureCompressionASTCLDR>, NeverSupported, AlwaysSupported);
807     AddCompressedFormat(&map, GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR,    6,  6, 128, 4, GL_RGBA, GL_UNSIGNED_BYTE, true,  RequireExtOrExt<&Extensions::textureCompressionASTCHDR, &Extensions::textureCompressionASTCLDR>, NeverSupported, AlwaysSupported);
808     AddCompressedFormat(&map, GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR,    8,  5, 128, 4, GL_RGBA, GL_UNSIGNED_BYTE, true,  RequireExtOrExt<&Extensions::textureCompressionASTCHDR, &Extensions::textureCompressionASTCLDR>, NeverSupported, AlwaysSupported);
809     AddCompressedFormat(&map, GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR,    8,  6, 128, 4, GL_RGBA, GL_UNSIGNED_BYTE, true,  RequireExtOrExt<&Extensions::textureCompressionASTCHDR, &Extensions::textureCompressionASTCLDR>, NeverSupported, AlwaysSupported);
810     AddCompressedFormat(&map, GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR,    8,  8, 128, 4, GL_RGBA, GL_UNSIGNED_BYTE, true,  RequireExtOrExt<&Extensions::textureCompressionASTCHDR, &Extensions::textureCompressionASTCLDR>, NeverSupported, AlwaysSupported);
811     AddCompressedFormat(&map, GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR,  10,  5, 128, 4, GL_RGBA, GL_UNSIGNED_BYTE, true,  RequireExtOrExt<&Extensions::textureCompressionASTCHDR, &Extensions::textureCompressionASTCLDR>, NeverSupported, AlwaysSupported);
812     AddCompressedFormat(&map, GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR,  10,  6, 128, 4, GL_RGBA, GL_UNSIGNED_BYTE, true,  RequireExtOrExt<&Extensions::textureCompressionASTCHDR, &Extensions::textureCompressionASTCLDR>, NeverSupported, AlwaysSupported);
813     AddCompressedFormat(&map, GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR,  10,  8, 128, 4, GL_RGBA, GL_UNSIGNED_BYTE, true,  RequireExtOrExt<&Extensions::textureCompressionASTCHDR, &Extensions::textureCompressionASTCLDR>, NeverSupported, AlwaysSupported);
814     AddCompressedFormat(&map, GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR, 10, 10, 128, 4, GL_RGBA, GL_UNSIGNED_BYTE, true,  RequireExtOrExt<&Extensions::textureCompressionASTCHDR, &Extensions::textureCompressionASTCLDR>, NeverSupported, AlwaysSupported);
815     AddCompressedFormat(&map, GL_COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR, 12, 10, 128, 4, GL_RGBA, GL_UNSIGNED_BYTE, true,  RequireExtOrExt<&Extensions::textureCompressionASTCHDR, &Extensions::textureCompressionASTCLDR>, NeverSupported, AlwaysSupported);
816     AddCompressedFormat(&map, GL_COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR, 12, 12, 128, 4, GL_RGBA, GL_UNSIGNED_BYTE, true,  RequireExtOrExt<&Extensions::textureCompressionASTCHDR, &Extensions::textureCompressionASTCLDR>, NeverSupported, AlwaysSupported);
817 
818     // For STENCIL_INDEX8 we chose a normalized component type for the following reasons:
819     // - Multisampled buffer are disallowed for non-normalized integer component types and we want to support it for STENCIL_INDEX8
820     // - All other stencil formats (all depth-stencil) are either float or normalized
821     // - It affects only validation of internalformat in RenderbufferStorageMultisample.
822     //                         | Internal format  |sized|D |S |X | Format    | Type            | Component type        | Supported      | Renderable     | Filterable   |
823     AddDepthStencilFormat(&map, GL_STENCIL_INDEX8, true, 0, 8, 0, GL_STENCIL, GL_UNSIGNED_BYTE, GL_UNSIGNED_NORMALIZED, RequireES<2, 0>, RequireES<2, 0>, NeverSupported);
824 
825     // From GL_ANGLE_lossy_etc_decode
826     //                       | Internal format                                                |W |H |BS |CC| Format | Type            | SRGB | Supported                                                                                     | Renderable     | Filterable    |
827     AddCompressedFormat(&map, GL_ETC1_RGB8_LOSSY_DECODE_ANGLE,                                 4, 4, 64, 3, GL_RGB,  GL_UNSIGNED_BYTE, false, RequireExt<&Extensions::lossyETCDecode>, NeverSupported, AlwaysSupported);
828     AddCompressedFormat(&map, GL_COMPRESSED_RGB8_LOSSY_DECODE_ETC2_ANGLE,                      4, 4, 64, 3, GL_RGB,  GL_UNSIGNED_BYTE, false, RequireExt<&Extensions::lossyETCDecode>, NeverSupported, AlwaysSupported);
829     AddCompressedFormat(&map, GL_COMPRESSED_SRGB8_LOSSY_DECODE_ETC2_ANGLE,                     4, 4, 64, 3, GL_RGB,  GL_UNSIGNED_BYTE, true,  RequireExt<&Extensions::lossyETCDecode>, NeverSupported, AlwaysSupported);
830     AddCompressedFormat(&map, GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_LOSSY_DECODE_ETC2_ANGLE,  4, 4, 64, 3, GL_RGBA, GL_UNSIGNED_BYTE, false, RequireExt<&Extensions::lossyETCDecode>, NeverSupported, AlwaysSupported);
831     AddCompressedFormat(&map, GL_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_LOSSY_DECODE_ETC2_ANGLE, 4, 4, 64, 3, GL_RGBA, GL_UNSIGNED_BYTE, true,  RequireExt<&Extensions::lossyETCDecode>, NeverSupported, AlwaysSupported);
832 
833     // From GL_EXT_texture_norm16
834     //                 | Internal format    |sized| R | G | B | A |S | Format         | Type                           | Component type        | SRGB | Texture supported                        | Renderable                               | Filterable    |
835     AddRGBAFormat(&map, GL_R16_EXT,          true, 16,  0,  0,  0, 0, GL_RED,          GL_UNSIGNED_SHORT,               GL_UNSIGNED_NORMALIZED, false, RequireExt<&Extensions::textureNorm16>,    RequireExt<&Extensions::textureNorm16>,    AlwaysSupported);
836     AddRGBAFormat(&map, GL_R16_SNORM_EXT,    true, 16,  0,  0,  0, 0, GL_RED,          GL_SHORT,                        GL_SIGNED_NORMALIZED,   false, RequireExt<&Extensions::textureNorm16>,    NeverSupported,                            AlwaysSupported);
837     AddRGBAFormat(&map, GL_RG16_EXT,         true, 16, 16,  0,  0, 0, GL_RG,           GL_UNSIGNED_SHORT,               GL_UNSIGNED_NORMALIZED, false, RequireExt<&Extensions::textureNorm16>,    RequireExt<&Extensions::textureNorm16>,    AlwaysSupported);
838     AddRGBAFormat(&map, GL_RG16_SNORM_EXT,   true, 16, 16,  0,  0, 0, GL_RG,           GL_SHORT,                        GL_SIGNED_NORMALIZED,   false, RequireExt<&Extensions::textureNorm16>,    NeverSupported,                            AlwaysSupported);
839     AddRGBAFormat(&map, GL_RGB16_EXT,        true, 16, 16, 16,  0, 0, GL_RGB,          GL_UNSIGNED_SHORT,               GL_UNSIGNED_NORMALIZED, false, RequireExt<&Extensions::textureNorm16>,    NeverSupported,                            AlwaysSupported);
840     AddRGBAFormat(&map, GL_RGB16_SNORM_EXT,  true, 16, 16, 16,  0, 0, GL_RGB,          GL_SHORT,                        GL_SIGNED_NORMALIZED,   false, RequireExt<&Extensions::textureNorm16>,    NeverSupported,                            AlwaysSupported);
841     AddRGBAFormat(&map, GL_RGBA16_EXT,       true, 16, 16, 16, 16, 0, GL_RGBA,         GL_UNSIGNED_SHORT,               GL_UNSIGNED_NORMALIZED, false, RequireExt<&Extensions::textureNorm16>,    RequireExt<&Extensions::textureNorm16>,    AlwaysSupported);
842     AddRGBAFormat(&map, GL_RGBA16_SNORM_EXT, true, 16, 16, 16, 16, 0, GL_RGBA,         GL_SHORT,                        GL_SIGNED_NORMALIZED,   false, RequireExt<&Extensions::textureNorm16>,    NeverSupported,                            AlwaysSupported);
843 
844     // Unsized formats
845     //                 | Internal format    |sized | R | G | B | A |S | Format         | Type                           | Component type        | SRGB | Texture supported                           | Renderable                                  | Filterable    |
846     AddRGBAFormat(&map, GL_RED,              false,  8,  0,  0,  0, 0, GL_RED,          GL_UNSIGNED_BYTE,                GL_UNSIGNED_NORMALIZED, false, RequireExt<&Extensions::textureRG>,           AlwaysSupported,                              AlwaysSupported);
847     AddRGBAFormat(&map, GL_RED,              false,  8,  0,  0,  0, 0, GL_RED,          GL_BYTE,                         GL_SIGNED_NORMALIZED,   false, NeverSupported,                               NeverSupported,                               NeverSupported );
848     AddRGBAFormat(&map, GL_RG,               false,  8,  8,  0,  0, 0, GL_RG,           GL_UNSIGNED_BYTE,                GL_UNSIGNED_NORMALIZED, false, RequireExt<&Extensions::textureRG>,           AlwaysSupported,                              AlwaysSupported);
849     AddRGBAFormat(&map, GL_RG,               false,  8,  8,  0,  0, 0, GL_RG,           GL_BYTE,                         GL_SIGNED_NORMALIZED,   false, NeverSupported,                               NeverSupported,                               NeverSupported );
850     AddRGBAFormat(&map, GL_RGB,              false,  8,  8,  8,  0, 0, GL_RGB,          GL_UNSIGNED_BYTE,                GL_UNSIGNED_NORMALIZED, false, RequireES<2, 0>,                              AlwaysSupported,                              AlwaysSupported);
851     AddRGBAFormat(&map, GL_RGB,              false,  5,  6,  5,  0, 0, GL_RGB,          GL_UNSIGNED_SHORT_5_6_5,         GL_UNSIGNED_NORMALIZED, false, RequireES<2, 0>,                              RequireES<2, 0>,                              AlwaysSupported);
852     AddRGBAFormat(&map, GL_RGB,              false,  8,  8,  8,  0, 0, GL_RGB,          GL_BYTE,                         GL_SIGNED_NORMALIZED,   false, NeverSupported,                               NeverSupported,                               NeverSupported );
853     AddRGBAFormat(&map, GL_RGBA,             false,  4,  4,  4,  4, 0, GL_RGBA,         GL_UNSIGNED_SHORT_4_4_4_4,       GL_UNSIGNED_NORMALIZED, false, RequireES<2, 0>,                              RequireES<2, 0>,                              AlwaysSupported);
854     AddRGBAFormat(&map, GL_RGBA,             false,  5,  5,  5,  1, 0, GL_RGBA,         GL_UNSIGNED_SHORT_5_5_5_1,       GL_UNSIGNED_NORMALIZED, false, RequireES<2, 0>,                              RequireES<2, 0>,                              AlwaysSupported);
855     AddRGBAFormat(&map, GL_RGBA,             false,  8,  8,  8,  8, 0, GL_RGBA,         GL_UNSIGNED_BYTE,                GL_UNSIGNED_NORMALIZED, false, RequireES<2, 0>,                              RequireES<2, 0>,                              AlwaysSupported);
856     AddRGBAFormat(&map, GL_RGBA,             false, 10, 10, 10,  2, 0, GL_RGBA,         GL_UNSIGNED_INT_2_10_10_10_REV,  GL_UNSIGNED_NORMALIZED, false, RequireES<2, 0>,                              RequireES<2, 0>,                              AlwaysSupported);
857     AddRGBAFormat(&map, GL_RGBA,             false,  8,  8,  8,  8, 0, GL_RGBA,         GL_BYTE,                         GL_SIGNED_NORMALIZED,   false, NeverSupported,                               NeverSupported,                               NeverSupported );
858     AddRGBAFormat(&map, GL_SRGB,             false,  8,  8,  8,  0, 0, GL_RGB,          GL_UNSIGNED_BYTE,                GL_UNSIGNED_NORMALIZED, true,  RequireExt<&Extensions::sRGB>,                NeverSupported,                               AlwaysSupported);
859     AddRGBAFormat(&map, GL_SRGB_ALPHA_EXT,   false,  8,  8,  8,  8, 0, GL_RGBA,         GL_UNSIGNED_BYTE,                GL_UNSIGNED_NORMALIZED, true,  RequireExt<&Extensions::sRGB>,                RequireExt<&Extensions::sRGB>,                AlwaysSupported);
860 
861     AddRGBAFormat(&map, GL_BGRA_EXT,         false,  8,  8,  8,  8, 0, GL_BGRA_EXT,     GL_UNSIGNED_BYTE,                GL_UNSIGNED_NORMALIZED, false, RequireExt<&Extensions::textureFormatBGRA8888>, RequireExt<&Extensions::textureFormatBGRA8888>, AlwaysSupported);
862 
863     // Unsized integer formats
864     //                 |Internal format |sized | R | G | B | A |S | Format         | Type                          | Component type | SRGB | Texture        | Renderable    | Filterable   |
865     AddRGBAFormat(&map, GL_RED_INTEGER,  false,  8,  0,  0,  0, 0, GL_RED_INTEGER,  GL_BYTE,                        GL_INT,          false, RequireES<3, 0>, NeverSupported, NeverSupported);
866     AddRGBAFormat(&map, GL_RED_INTEGER,  false,  8,  0,  0,  0, 0, GL_RED_INTEGER,  GL_UNSIGNED_BYTE,               GL_UNSIGNED_INT, false, RequireES<3, 0>, NeverSupported, NeverSupported);
867     AddRGBAFormat(&map, GL_RED_INTEGER,  false, 16,  0,  0,  0, 0, GL_RED_INTEGER,  GL_SHORT,                       GL_INT,          false, RequireES<3, 0>, NeverSupported, NeverSupported);
868     AddRGBAFormat(&map, GL_RED_INTEGER,  false, 16,  0,  0,  0, 0, GL_RED_INTEGER,  GL_UNSIGNED_SHORT,              GL_UNSIGNED_INT, false, RequireES<3, 0>, NeverSupported, NeverSupported);
869     AddRGBAFormat(&map, GL_RED_INTEGER,  false, 32,  0,  0,  0, 0, GL_RED_INTEGER,  GL_INT,                         GL_INT,          false, RequireES<3, 0>, NeverSupported, NeverSupported);
870     AddRGBAFormat(&map, GL_RED_INTEGER,  false, 32,  0,  0,  0, 0, GL_RED_INTEGER,  GL_UNSIGNED_INT,                GL_UNSIGNED_INT, false, RequireES<3, 0>, NeverSupported, NeverSupported);
871     AddRGBAFormat(&map, GL_RG_INTEGER,   false,  8,  8,  0,  0, 0, GL_RG_INTEGER,   GL_BYTE,                        GL_INT,          false, RequireES<3, 0>, NeverSupported, NeverSupported);
872     AddRGBAFormat(&map, GL_RG_INTEGER,   false,  8,  8,  0,  0, 0, GL_RG_INTEGER,   GL_UNSIGNED_BYTE,               GL_UNSIGNED_INT, false, RequireES<3, 0>, NeverSupported, NeverSupported);
873     AddRGBAFormat(&map, GL_RG_INTEGER,   false, 16, 16,  0,  0, 0, GL_RG_INTEGER,   GL_SHORT,                       GL_INT,          false, RequireES<3, 0>, NeverSupported, NeverSupported);
874     AddRGBAFormat(&map, GL_RG_INTEGER,   false, 16, 16,  0,  0, 0, GL_RG_INTEGER,   GL_UNSIGNED_SHORT,              GL_UNSIGNED_INT, false, RequireES<3, 0>, NeverSupported, NeverSupported);
875     AddRGBAFormat(&map, GL_RG_INTEGER,   false, 32, 32,  0,  0, 0, GL_RG_INTEGER,   GL_INT,                         GL_INT,          false, RequireES<3, 0>, NeverSupported, NeverSupported);
876     AddRGBAFormat(&map, GL_RG_INTEGER,   false, 32, 32,  0,  0, 0, GL_RG_INTEGER,   GL_UNSIGNED_INT,                GL_UNSIGNED_INT, false, RequireES<3, 0>, NeverSupported, NeverSupported);
877     AddRGBAFormat(&map, GL_RGB_INTEGER,  false,  8,  8,  8,  0, 0, GL_RGB_INTEGER,  GL_BYTE,                        GL_INT,          false, RequireES<3, 0>, NeverSupported, NeverSupported);
878     AddRGBAFormat(&map, GL_RGB_INTEGER,  false,  8,  8,  8,  0, 0, GL_RGB_INTEGER,  GL_UNSIGNED_BYTE,               GL_UNSIGNED_INT, false, RequireES<3, 0>, NeverSupported, NeverSupported);
879     AddRGBAFormat(&map, GL_RGB_INTEGER,  false, 16, 16, 16,  0, 0, GL_RGB_INTEGER,  GL_SHORT,                       GL_INT,          false, RequireES<3, 0>, NeverSupported, NeverSupported);
880     AddRGBAFormat(&map, GL_RGB_INTEGER,  false, 16, 16, 16,  0, 0, GL_RGB_INTEGER,  GL_UNSIGNED_SHORT,              GL_UNSIGNED_INT, false, RequireES<3, 0>, NeverSupported, NeverSupported);
881     AddRGBAFormat(&map, GL_RGB_INTEGER,  false, 32, 32, 32,  0, 0, GL_RGB_INTEGER,  GL_INT,                         GL_INT,          false, RequireES<3, 0>, NeverSupported, NeverSupported);
882     AddRGBAFormat(&map, GL_RGB_INTEGER,  false, 32, 32, 32,  0, 0, GL_RGB_INTEGER,  GL_UNSIGNED_INT,                GL_UNSIGNED_INT, false, RequireES<3, 0>, NeverSupported, NeverSupported);
883     AddRGBAFormat(&map, GL_RGBA_INTEGER, false,  8,  8,  8,  8, 0, GL_RGBA_INTEGER, GL_BYTE,                        GL_INT,          false, RequireES<3, 0>, NeverSupported, NeverSupported);
884     AddRGBAFormat(&map, GL_RGBA_INTEGER, false,  8,  8,  8,  8, 0, GL_RGBA_INTEGER, GL_UNSIGNED_BYTE,               GL_UNSIGNED_INT, false, RequireES<3, 0>, NeverSupported, NeverSupported);
885     AddRGBAFormat(&map, GL_RGBA_INTEGER, false, 16, 16, 16, 16, 0, GL_RGBA_INTEGER, GL_SHORT,                       GL_INT,          false, RequireES<3, 0>, NeverSupported, NeverSupported);
886     AddRGBAFormat(&map, GL_RGBA_INTEGER, false, 16, 16, 16, 16, 0, GL_RGBA_INTEGER, GL_UNSIGNED_SHORT,              GL_UNSIGNED_INT, false, RequireES<3, 0>, NeverSupported, NeverSupported);
887     AddRGBAFormat(&map, GL_RGBA_INTEGER, false, 32, 32, 32, 32, 0, GL_RGBA_INTEGER, GL_INT,                         GL_INT,          false, RequireES<3, 0>, NeverSupported, NeverSupported);
888     AddRGBAFormat(&map, GL_RGBA_INTEGER, false, 32, 32, 32, 32, 0, GL_RGBA_INTEGER, GL_UNSIGNED_INT,                GL_UNSIGNED_INT, false, RequireES<3, 0>, NeverSupported, NeverSupported);
889     AddRGBAFormat(&map, GL_RGBA_INTEGER, false, 10, 10, 10,  2, 0, GL_RGBA_INTEGER, GL_UNSIGNED_INT_2_10_10_10_REV, GL_UNSIGNED_INT, false, RequireES<3, 0>, NeverSupported, NeverSupported);
890 
891     // Unsized floating point formats
892     //                 |Internal format |sized | R | G | B | A |S | Format         | Type                           | Comp    | SRGB | Texture supported           | Renderable                            | Filterable                                              |
893     AddRGBAFormat(&map, GL_RED,          false, 16,  0,  0,  0, 0, GL_RED,          GL_HALF_FLOAT,                   GL_FLOAT, false, NeverSupported,               NeverSupported,                         NeverSupported                                           );
894     AddRGBAFormat(&map, GL_RG,           false, 16, 16,  0,  0, 0, GL_RG,           GL_HALF_FLOAT,                   GL_FLOAT, false, NeverSupported,               NeverSupported,                         NeverSupported                                           );
895     AddRGBAFormat(&map, GL_RGB,          false, 16, 16, 16,  0, 0, GL_RGB,          GL_HALF_FLOAT,                   GL_FLOAT, false, NeverSupported,               NeverSupported,                         NeverSupported                                           );
896     AddRGBAFormat(&map, GL_RGBA,         false, 16, 16, 16, 16, 0, GL_RGBA,         GL_HALF_FLOAT,                   GL_FLOAT, false, NeverSupported,               NeverSupported,                         NeverSupported                                           );
897     AddRGBAFormat(&map, GL_RED,          false, 16,  0,  0,  0, 0, GL_RED,          GL_HALF_FLOAT_OES,               GL_FLOAT, false, UnsizedHalfFloatOESRGSupport, UnsizedHalfFloatOESRGRenderableSupport, RequireESOrExt<3, 0, &Extensions::textureHalfFloatLinear>);
898     AddRGBAFormat(&map, GL_RG,           false, 16, 16,  0,  0, 0, GL_RG,           GL_HALF_FLOAT_OES,               GL_FLOAT, false, UnsizedHalfFloatOESRGSupport, UnsizedHalfFloatOESRGRenderableSupport, RequireESOrExt<3, 0, &Extensions::textureHalfFloatLinear>);
899     AddRGBAFormat(&map, GL_RGB,          false, 16, 16, 16,  0, 0, GL_RGB,          GL_HALF_FLOAT_OES,               GL_FLOAT, false, UnsizedHalfFloatOESSupport,   UnsizedHalfFloatOESRenderableSupport,   RequireESOrExt<3, 0, &Extensions::textureHalfFloatLinear>);
900     AddRGBAFormat(&map, GL_RGBA,         false, 16, 16, 16, 16, 0, GL_RGBA,         GL_HALF_FLOAT_OES,               GL_FLOAT, false, UnsizedHalfFloatOESSupport,   UnsizedHalfFloatOESRenderableSupport,   RequireESOrExt<3, 0, &Extensions::textureHalfFloatLinear>);
901     AddRGBAFormat(&map, GL_RED,          false, 32,  0,  0,  0, 0, GL_RED,          GL_FLOAT,                        GL_FLOAT, false, UnsizedFloatRGSupport,        UnsizedFloatRGRenderableSupport,        RequireExt<&Extensions::textureFloatLinear>              );
902     AddRGBAFormat(&map, GL_RG,           false, 32, 32,  0,  0, 0, GL_RG,           GL_FLOAT,                        GL_FLOAT, false, UnsizedFloatRGSupport,        UnsizedFloatRGRenderableSupport,        RequireExt<&Extensions::textureFloatLinear>              );
903     AddRGBAFormat(&map, GL_RGB,          false, 32, 32, 32,  0, 0, GL_RGB,          GL_FLOAT,                        GL_FLOAT, false, UnsizedFloatSupport,          UnsizedFloatRGBRenderableSupport,       RequireExt<&Extensions::textureFloatLinear>              );
904     AddRGBAFormat(&map, GL_RGB,          false,  9,  9,  9,  0, 5, GL_RGB,          GL_UNSIGNED_INT_5_9_9_9_REV,     GL_FLOAT, false, NeverSupported,               NeverSupported,                         NeverSupported                                           );
905     AddRGBAFormat(&map, GL_RGB,          false, 11, 11, 10,  0, 0, GL_RGB,          GL_UNSIGNED_INT_10F_11F_11F_REV, GL_FLOAT, false, NeverSupported,               NeverSupported,                         NeverSupported                                           );
906     AddRGBAFormat(&map, GL_RGBA,         false, 32, 32, 32, 32, 0, GL_RGBA,         GL_FLOAT,                        GL_FLOAT, false, UnsizedFloatSupport,          UnsizedFloatRGBARenderableSupport,      RequireExt<&Extensions::textureFloatLinear>              );
907 
908     // Unsized luminance alpha formats
909     //                | Internal format    |sized | L | A | Format            | Type             | Component type        | Supported                                | Renderable    | Filterable                                    |
910     AddLUMAFormat(&map, GL_ALPHA,           false,  0,  8, GL_ALPHA,           GL_UNSIGNED_BYTE,  GL_UNSIGNED_NORMALIZED, RequireES<2, 0>,                           NeverSupported, AlwaysSupported                                );
911     AddLUMAFormat(&map, GL_LUMINANCE,       false,  8,  0, GL_LUMINANCE,       GL_UNSIGNED_BYTE,  GL_UNSIGNED_NORMALIZED, RequireES<2, 0>,                           NeverSupported, AlwaysSupported                                );
912     AddLUMAFormat(&map, GL_LUMINANCE_ALPHA, false,  8,  8, GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE,  GL_UNSIGNED_NORMALIZED, RequireES<2, 0>,                           NeverSupported, AlwaysSupported                                );
913     AddLUMAFormat(&map, GL_ALPHA,           false,  0, 16, GL_ALPHA,           GL_HALF_FLOAT_OES, GL_FLOAT,               RequireExt<&Extensions::textureHalfFloat>, NeverSupported, RequireExt<&Extensions::textureHalfFloatLinear>);
914     AddLUMAFormat(&map, GL_LUMINANCE,       false, 16,  0, GL_LUMINANCE,       GL_HALF_FLOAT_OES, GL_FLOAT,               RequireExt<&Extensions::textureHalfFloat>, NeverSupported, RequireExt<&Extensions::textureHalfFloatLinear>);
915     AddLUMAFormat(&map, GL_LUMINANCE_ALPHA ,false, 16, 16, GL_LUMINANCE_ALPHA, GL_HALF_FLOAT_OES, GL_FLOAT,               RequireExt<&Extensions::textureHalfFloat>, NeverSupported, RequireExt<&Extensions::textureHalfFloatLinear>);
916     AddLUMAFormat(&map, GL_ALPHA,           false,  0, 32, GL_ALPHA,           GL_FLOAT,          GL_FLOAT,               RequireExt<&Extensions::textureFloat>,     NeverSupported, RequireExt<&Extensions::textureFloatLinear>    );
917     AddLUMAFormat(&map, GL_LUMINANCE,       false, 32,  0, GL_LUMINANCE,       GL_FLOAT,          GL_FLOAT,               RequireExt<&Extensions::textureFloat>,     NeverSupported, RequireExt<&Extensions::textureFloatLinear>    );
918     AddLUMAFormat(&map, GL_LUMINANCE_ALPHA, false, 32, 32, GL_LUMINANCE_ALPHA, GL_FLOAT,          GL_FLOAT,               RequireExt<&Extensions::textureFloat>,     NeverSupported, RequireExt<&Extensions::textureFloatLinear>    );
919 
920     // Unsized depth stencil formats
921     //                         | Internal format        |sized | D |S | X | Format            | Type                             | Component type        | Supported                                            | Renderable                                           | Filterable    |
922     AddDepthStencilFormat(&map, GL_DEPTH_COMPONENT,      false, 16, 0,  0, GL_DEPTH_COMPONENT, GL_UNSIGNED_SHORT,                 GL_UNSIGNED_NORMALIZED, RequireES<2, 0>,                                       RequireES<2, 0>,                                       AlwaysSupported);
923     AddDepthStencilFormat(&map, GL_DEPTH_COMPONENT,      false, 24, 0,  0, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT,                   GL_UNSIGNED_NORMALIZED, RequireES<2, 0>,                                       RequireES<2, 0>,                                       AlwaysSupported);
924     AddDepthStencilFormat(&map, GL_DEPTH_COMPONENT,      false, 32, 0,  0, GL_DEPTH_COMPONENT, GL_FLOAT,                          GL_FLOAT,               RequireES<2, 0>,                                       RequireES<2, 0>,                                       AlwaysSupported);
925     AddDepthStencilFormat(&map, GL_DEPTH_STENCIL,        false, 24, 8,  0, GL_DEPTH_STENCIL,   GL_UNSIGNED_INT_24_8,              GL_UNSIGNED_NORMALIZED, RequireESOrExt<3, 0, &Extensions::packedDepthStencil>, RequireESOrExt<3, 0, &Extensions::packedDepthStencil>, AlwaysSupported);
926     AddDepthStencilFormat(&map, GL_DEPTH_STENCIL,        false, 32, 8, 24, GL_DEPTH_STENCIL,   GL_FLOAT_32_UNSIGNED_INT_24_8_REV, GL_FLOAT,               RequireESOrExt<3, 0, &Extensions::packedDepthStencil>, RequireESOrExt<3, 0, &Extensions::packedDepthStencil>, AlwaysSupported);
927     AddDepthStencilFormat(&map, GL_STENCIL,              false,  0, 8,  0, GL_STENCIL,         GL_UNSIGNED_BYTE,                  GL_UNSIGNED_NORMALIZED, RequireES<2, 0>,                                       RequireES<2, 0>,                                       NeverSupported);
928     // clang-format on
929 
930     return map;
931 }
932 
GetInternalFormatMap()933 static const InternalFormatInfoMap &GetInternalFormatMap()
934 {
935     static const InternalFormatInfoMap formatMap = BuildInternalFormatInfoMap();
936     return formatMap;
937 }
938 
BuildAllSizedInternalFormatSet()939 static FormatSet BuildAllSizedInternalFormatSet()
940 {
941     FormatSet result;
942 
943     for (const auto &internalFormat : GetInternalFormatMap())
944     {
945         for (const auto &type : internalFormat.second)
946         {
947             if (type.second.sized)
948             {
949                 // TODO(jmadill): Fix this hack.
950                 if (internalFormat.first == GL_BGR565_ANGLEX)
951                     continue;
952 
953                 result.insert(internalFormat.first);
954             }
955         }
956     }
957 
958     return result;
959 }
960 
GetTypeInfo(GLenum type)961 const Type &GetTypeInfo(GLenum type)
962 {
963     switch (type)
964     {
965       case GL_UNSIGNED_BYTE:
966       case GL_BYTE:
967         {
968             static const Type info = GenTypeInfo(1, false);
969             return info;
970         }
971       case GL_UNSIGNED_SHORT:
972       case GL_SHORT:
973       case GL_HALF_FLOAT:
974       case GL_HALF_FLOAT_OES:
975         {
976             static const Type info = GenTypeInfo(2, false);
977             return info;
978         }
979       case GL_UNSIGNED_INT:
980       case GL_INT:
981       case GL_FLOAT:
982         {
983             static const Type info = GenTypeInfo(4, false);
984             return info;
985         }
986       case GL_UNSIGNED_SHORT_5_6_5:
987       case GL_UNSIGNED_SHORT_4_4_4_4:
988       case GL_UNSIGNED_SHORT_5_5_5_1:
989       case GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT:
990       case GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT:
991         {
992             static const Type info = GenTypeInfo(2, true);
993             return info;
994         }
995       case GL_UNSIGNED_INT_2_10_10_10_REV:
996       case GL_UNSIGNED_INT_24_8:
997       case GL_UNSIGNED_INT_10F_11F_11F_REV:
998       case GL_UNSIGNED_INT_5_9_9_9_REV:
999         {
1000             ASSERT(GL_UNSIGNED_INT_24_8_OES == GL_UNSIGNED_INT_24_8);
1001             static const Type info = GenTypeInfo(4, true);
1002             return info;
1003         }
1004       case GL_FLOAT_32_UNSIGNED_INT_24_8_REV:
1005         {
1006             static const Type info = GenTypeInfo(8, true);
1007             return info;
1008         }
1009       default:
1010         {
1011             static const Type defaultInfo;
1012             return defaultInfo;
1013         }
1014     }
1015 }
1016 
GetSizedInternalFormatInfo(GLenum internalFormat)1017 const InternalFormat &GetSizedInternalFormatInfo(GLenum internalFormat)
1018 {
1019     static const InternalFormat defaultInternalFormat;
1020     const InternalFormatInfoMap &formatMap = GetInternalFormatMap();
1021     auto iter                              = formatMap.find(internalFormat);
1022 
1023     // Sized internal formats only have one type per entry
1024     if (iter == formatMap.end() || iter->second.size() != 1)
1025     {
1026         return defaultInternalFormat;
1027     }
1028 
1029     const InternalFormat &internalFormatInfo = iter->second.begin()->second;
1030     if (!internalFormatInfo.sized)
1031     {
1032         return defaultInternalFormat;
1033     }
1034 
1035     return internalFormatInfo;
1036 }
1037 
GetInternalFormatInfo(GLenum internalFormat,GLenum type)1038 const InternalFormat &GetInternalFormatInfo(GLenum internalFormat, GLenum type)
1039 {
1040     static const InternalFormat defaultInternalFormat;
1041     const InternalFormatInfoMap &formatMap = GetInternalFormatMap();
1042 
1043     auto internalFormatIter = formatMap.find(internalFormat);
1044     if (internalFormatIter == formatMap.end())
1045     {
1046         return defaultInternalFormat;
1047     }
1048 
1049     // If the internal format is sized, simply return it without the type check.
1050     if (internalFormatIter->second.size() == 1 && internalFormatIter->second.begin()->second.sized)
1051     {
1052         return internalFormatIter->second.begin()->second;
1053     }
1054 
1055     auto typeIter = internalFormatIter->second.find(type);
1056     if (typeIter == internalFormatIter->second.end())
1057     {
1058         return defaultInternalFormat;
1059     }
1060 
1061     return typeIter->second;
1062 }
1063 
computePixelBytes(GLenum formatType) const1064 GLuint InternalFormat::computePixelBytes(GLenum formatType) const
1065 {
1066     const auto &typeInfo = GetTypeInfo(formatType);
1067     GLuint components    = typeInfo.specialInterpretation ? 1u : componentCount;
1068     return components * typeInfo.bytes;
1069 }
1070 
computeRowPitch(GLenum formatType,GLsizei width,GLint alignment,GLint rowLength) const1071 ErrorOrResult<GLuint> InternalFormat::computeRowPitch(GLenum formatType,
1072                                                           GLsizei width,
1073                                                           GLint alignment,
1074                                                           GLint rowLength) const
1075 {
1076     // Compressed images do not use pack/unpack parameters.
1077     if (compressed)
1078     {
1079         ASSERT(rowLength == 0);
1080         return computeCompressedImageSize(Extents(width, 1, 1));
1081     }
1082 
1083     CheckedNumeric<GLuint> checkedWidth(rowLength > 0 ? rowLength : width);
1084     CheckedNumeric<GLuint> checkedRowBytes = checkedWidth * computePixelBytes(formatType);
1085 
1086     ASSERT(alignment > 0 && isPow2(alignment));
1087     CheckedNumeric<GLuint> checkedAlignment(alignment);
1088     auto aligned = rx::roundUp(checkedRowBytes, checkedAlignment);
1089     ANGLE_TRY_CHECKED_MATH(aligned);
1090     return aligned.ValueOrDie();
1091 }
1092 
computeDepthPitch(GLsizei height,GLint imageHeight,GLuint rowPitch) const1093 ErrorOrResult<GLuint> InternalFormat::computeDepthPitch(GLsizei height,
1094                                                         GLint imageHeight,
1095                                                         GLuint rowPitch) const
1096 {
1097     GLuint rows =
1098         (imageHeight > 0 ? static_cast<GLuint>(imageHeight) : static_cast<GLuint>(height));
1099     CheckedNumeric<GLuint> checkedRowPitch(rowPitch);
1100 
1101     auto depthPitch = checkedRowPitch * rows;
1102     ANGLE_TRY_CHECKED_MATH(depthPitch);
1103     return depthPitch.ValueOrDie();
1104 }
1105 
computeDepthPitch(GLenum formatType,GLsizei width,GLsizei height,GLint alignment,GLint rowLength,GLint imageHeight) const1106 ErrorOrResult<GLuint> InternalFormat::computeDepthPitch(GLenum formatType,
1107                                                             GLsizei width,
1108                                                             GLsizei height,
1109                                                             GLint alignment,
1110                                                             GLint rowLength,
1111                                                             GLint imageHeight) const
1112 {
1113     GLuint rowPitch = 0;
1114     ANGLE_TRY_RESULT(computeRowPitch(formatType, width, alignment, rowLength), rowPitch);
1115     return computeDepthPitch(height, imageHeight, rowPitch);
1116 }
1117 
computeCompressedImageSize(const Extents & size) const1118 ErrorOrResult<GLuint> InternalFormat::computeCompressedImageSize(const Extents &size) const
1119 {
1120     CheckedNumeric<GLuint> checkedWidth(size.width);
1121     CheckedNumeric<GLuint> checkedHeight(size.height);
1122     CheckedNumeric<GLuint> checkedDepth(size.depth);
1123     CheckedNumeric<GLuint> checkedBlockWidth(compressedBlockWidth);
1124     CheckedNumeric<GLuint> checkedBlockHeight(compressedBlockHeight);
1125 
1126     ASSERT(compressed);
1127     auto numBlocksWide = (checkedWidth + checkedBlockWidth - 1u) / checkedBlockWidth;
1128     auto numBlocksHigh = (checkedHeight + checkedBlockHeight - 1u) / checkedBlockHeight;
1129     auto bytes         = numBlocksWide * numBlocksHigh * pixelBytes * checkedDepth;
1130     ANGLE_TRY_CHECKED_MATH(bytes);
1131     return bytes.ValueOrDie();
1132 }
1133 
computeSkipBytes(GLuint rowPitch,GLuint depthPitch,const PixelStoreStateBase & state,bool is3D) const1134 ErrorOrResult<GLuint> InternalFormat::computeSkipBytes(GLuint rowPitch,
1135                                                            GLuint depthPitch,
1136                                                            const PixelStoreStateBase &state,
1137                                                            bool is3D) const
1138 {
1139     CheckedNumeric<GLuint> checkedRowPitch(rowPitch);
1140     CheckedNumeric<GLuint> checkedDepthPitch(depthPitch);
1141     CheckedNumeric<GLuint> checkedSkipImages(static_cast<GLuint>(state.skipImages));
1142     CheckedNumeric<GLuint> checkedSkipRows(static_cast<GLuint>(state.skipRows));
1143     CheckedNumeric<GLuint> checkedSkipPixels(static_cast<GLuint>(state.skipPixels));
1144     CheckedNumeric<GLuint> checkedPixelBytes(pixelBytes);
1145     auto checkedSkipImagesBytes = checkedSkipImages * checkedDepthPitch;
1146     if (!is3D)
1147     {
1148         checkedSkipImagesBytes = 0;
1149     }
1150     auto skipBytes = checkedSkipImagesBytes + checkedSkipRows * checkedRowPitch +
1151                      checkedSkipPixels * checkedPixelBytes;
1152     ANGLE_TRY_CHECKED_MATH(skipBytes);
1153     return skipBytes.ValueOrDie();
1154 }
1155 
computePackUnpackEndByte(GLenum formatType,const Extents & size,const PixelStoreStateBase & state,bool is3D) const1156 ErrorOrResult<GLuint> InternalFormat::computePackUnpackEndByte(
1157     GLenum formatType,
1158     const Extents &size,
1159     const PixelStoreStateBase &state,
1160     bool is3D) const
1161 {
1162     GLuint rowPitch = 0;
1163     ANGLE_TRY_RESULT(computeRowPitch(formatType, size.width, state.alignment, state.rowLength),
1164                      rowPitch);
1165 
1166     GLuint depthPitch = 0;
1167     if (is3D)
1168     {
1169         ANGLE_TRY_RESULT(computeDepthPitch(size.height, state.imageHeight, rowPitch), depthPitch);
1170     }
1171 
1172     CheckedNumeric<GLuint> checkedCopyBytes = 0;
1173     if (compressed)
1174     {
1175         ANGLE_TRY_RESULT(computeCompressedImageSize(size), checkedCopyBytes);
1176     }
1177     else if (size.height != 0 && (!is3D || size.depth != 0))
1178     {
1179         CheckedNumeric<GLuint> bytes = computePixelBytes(formatType);
1180         checkedCopyBytes += size.width * bytes;
1181 
1182         CheckedNumeric<GLuint> heightMinusOne = size.height - 1;
1183         checkedCopyBytes += heightMinusOne * rowPitch;
1184 
1185         if (is3D)
1186         {
1187             CheckedNumeric<GLuint> depthMinusOne = size.depth - 1;
1188             checkedCopyBytes += depthMinusOne * depthPitch;
1189         }
1190     }
1191 
1192     CheckedNumeric<GLuint> checkedSkipBytes = 0;
1193     ANGLE_TRY_RESULT(computeSkipBytes(rowPitch, depthPitch, state, is3D), checkedSkipBytes);
1194 
1195     CheckedNumeric<GLuint> endByte = checkedCopyBytes + checkedSkipBytes;
1196 
1197     ANGLE_TRY_CHECKED_MATH(endByte);
1198     return endByte.ValueOrDie();
1199 }
1200 
GetUnsizedFormat(GLenum internalFormat)1201 GLenum GetUnsizedFormat(GLenum internalFormat)
1202 {
1203     auto sizedFormatInfo = GetSizedInternalFormatInfo(internalFormat);
1204     if (sizedFormatInfo.internalFormat != GL_NONE)
1205     {
1206         return sizedFormatInfo.format;
1207     }
1208 
1209     return internalFormat;
1210 }
1211 
GetAllSizedInternalFormats()1212 const FormatSet &GetAllSizedInternalFormats()
1213 {
1214     static FormatSet formatSet = BuildAllSizedInternalFormatSet();
1215     return formatSet;
1216 }
1217 
GetAttributeType(GLenum enumValue)1218 AttributeType GetAttributeType(GLenum enumValue)
1219 {
1220     switch (enumValue)
1221     {
1222         case GL_FLOAT:
1223             return ATTRIBUTE_FLOAT;
1224         case GL_FLOAT_VEC2:
1225             return ATTRIBUTE_VEC2;
1226         case GL_FLOAT_VEC3:
1227             return ATTRIBUTE_VEC3;
1228         case GL_FLOAT_VEC4:
1229             return ATTRIBUTE_VEC4;
1230         case GL_INT:
1231             return ATTRIBUTE_INT;
1232         case GL_INT_VEC2:
1233             return ATTRIBUTE_IVEC2;
1234         case GL_INT_VEC3:
1235             return ATTRIBUTE_IVEC3;
1236         case GL_INT_VEC4:
1237             return ATTRIBUTE_IVEC4;
1238         case GL_UNSIGNED_INT:
1239             return ATTRIBUTE_UINT;
1240         case GL_UNSIGNED_INT_VEC2:
1241             return ATTRIBUTE_UVEC2;
1242         case GL_UNSIGNED_INT_VEC3:
1243             return ATTRIBUTE_UVEC3;
1244         case GL_UNSIGNED_INT_VEC4:
1245             return ATTRIBUTE_UVEC4;
1246         case GL_FLOAT_MAT2:
1247             return ATTRIBUTE_MAT2;
1248         case GL_FLOAT_MAT3:
1249             return ATTRIBUTE_MAT3;
1250         case GL_FLOAT_MAT4:
1251             return ATTRIBUTE_MAT4;
1252         case GL_FLOAT_MAT2x3:
1253             return ATTRIBUTE_MAT2x3;
1254         case GL_FLOAT_MAT2x4:
1255             return ATTRIBUTE_MAT2x4;
1256         case GL_FLOAT_MAT3x2:
1257             return ATTRIBUTE_MAT3x2;
1258         case GL_FLOAT_MAT3x4:
1259             return ATTRIBUTE_MAT3x4;
1260         case GL_FLOAT_MAT4x2:
1261             return ATTRIBUTE_MAT4x2;
1262         case GL_FLOAT_MAT4x3:
1263             return ATTRIBUTE_MAT4x3;
1264         default:
1265             UNREACHABLE();
1266             return ATTRIBUTE_FLOAT;
1267     }
1268 }
1269 
GetVertexFormatType(GLenum type,GLboolean normalized,GLuint components,bool pureInteger)1270 VertexFormatType GetVertexFormatType(GLenum type, GLboolean normalized, GLuint components, bool pureInteger)
1271 {
1272     switch (type)
1273     {
1274         case GL_BYTE:
1275             switch (components)
1276             {
1277                 case 1:
1278                     if (pureInteger)
1279                         return VERTEX_FORMAT_SBYTE1_INT;
1280                     if (normalized)
1281                         return VERTEX_FORMAT_SBYTE1_NORM;
1282                     return VERTEX_FORMAT_SBYTE1;
1283                 case 2:
1284                     if (pureInteger)
1285                         return VERTEX_FORMAT_SBYTE2_INT;
1286                     if (normalized)
1287                         return VERTEX_FORMAT_SBYTE2_NORM;
1288                     return VERTEX_FORMAT_SBYTE2;
1289                 case 3:
1290                     if (pureInteger)
1291                         return VERTEX_FORMAT_SBYTE3_INT;
1292                     if (normalized)
1293                         return VERTEX_FORMAT_SBYTE3_NORM;
1294                     return VERTEX_FORMAT_SBYTE3;
1295                 case 4:
1296                     if (pureInteger)
1297                         return VERTEX_FORMAT_SBYTE4_INT;
1298                     if (normalized)
1299                         return VERTEX_FORMAT_SBYTE4_NORM;
1300                     return VERTEX_FORMAT_SBYTE4;
1301                 default:
1302                     UNREACHABLE();
1303                     break;
1304             }
1305         case GL_UNSIGNED_BYTE:
1306             switch (components)
1307             {
1308                 case 1:
1309                     if (pureInteger)
1310                         return VERTEX_FORMAT_UBYTE1_INT;
1311                     if (normalized)
1312                         return VERTEX_FORMAT_UBYTE1_NORM;
1313                     return VERTEX_FORMAT_UBYTE1;
1314                 case 2:
1315                     if (pureInteger)
1316                         return VERTEX_FORMAT_UBYTE2_INT;
1317                     if (normalized)
1318                         return VERTEX_FORMAT_UBYTE2_NORM;
1319                     return VERTEX_FORMAT_UBYTE2;
1320                 case 3:
1321                     if (pureInteger)
1322                         return VERTEX_FORMAT_UBYTE3_INT;
1323                     if (normalized)
1324                         return VERTEX_FORMAT_UBYTE3_NORM;
1325                     return VERTEX_FORMAT_UBYTE3;
1326                 case 4:
1327                     if (pureInteger)
1328                         return VERTEX_FORMAT_UBYTE4_INT;
1329                     if (normalized)
1330                         return VERTEX_FORMAT_UBYTE4_NORM;
1331                     return VERTEX_FORMAT_UBYTE4;
1332                 default:
1333                     UNREACHABLE();
1334                     break;
1335             }
1336         case GL_SHORT:
1337             switch (components)
1338             {
1339                 case 1:
1340                     if (pureInteger)
1341                         return VERTEX_FORMAT_SSHORT1_INT;
1342                     if (normalized)
1343                         return VERTEX_FORMAT_SSHORT1_NORM;
1344                     return VERTEX_FORMAT_SSHORT1;
1345                 case 2:
1346                     if (pureInteger)
1347                         return VERTEX_FORMAT_SSHORT2_INT;
1348                     if (normalized)
1349                         return VERTEX_FORMAT_SSHORT2_NORM;
1350                     return VERTEX_FORMAT_SSHORT2;
1351                 case 3:
1352                     if (pureInteger)
1353                         return VERTEX_FORMAT_SSHORT3_INT;
1354                     if (normalized)
1355                         return VERTEX_FORMAT_SSHORT3_NORM;
1356                     return VERTEX_FORMAT_SSHORT3;
1357                 case 4:
1358                     if (pureInteger)
1359                         return VERTEX_FORMAT_SSHORT4_INT;
1360                     if (normalized)
1361                         return VERTEX_FORMAT_SSHORT4_NORM;
1362                     return VERTEX_FORMAT_SSHORT4;
1363                 default:
1364                     UNREACHABLE();
1365                     break;
1366             }
1367         case GL_UNSIGNED_SHORT:
1368             switch (components)
1369             {
1370                 case 1:
1371                     if (pureInteger)
1372                         return VERTEX_FORMAT_USHORT1_INT;
1373                     if (normalized)
1374                         return VERTEX_FORMAT_USHORT1_NORM;
1375                     return VERTEX_FORMAT_USHORT1;
1376                 case 2:
1377                     if (pureInteger)
1378                         return VERTEX_FORMAT_USHORT2_INT;
1379                     if (normalized)
1380                         return VERTEX_FORMAT_USHORT2_NORM;
1381                     return VERTEX_FORMAT_USHORT2;
1382                 case 3:
1383                     if (pureInteger)
1384                         return VERTEX_FORMAT_USHORT3_INT;
1385                     if (normalized)
1386                         return VERTEX_FORMAT_USHORT3_NORM;
1387                     return VERTEX_FORMAT_USHORT3;
1388                 case 4:
1389                     if (pureInteger)
1390                         return VERTEX_FORMAT_USHORT4_INT;
1391                     if (normalized)
1392                         return VERTEX_FORMAT_USHORT4_NORM;
1393                     return VERTEX_FORMAT_USHORT4;
1394                 default:
1395                     UNREACHABLE();
1396                     break;
1397             }
1398         case GL_INT:
1399             switch (components)
1400             {
1401                 case 1:
1402                     if (pureInteger)
1403                         return VERTEX_FORMAT_SINT1_INT;
1404                     if (normalized)
1405                         return VERTEX_FORMAT_SINT1_NORM;
1406                     return VERTEX_FORMAT_SINT1;
1407                 case 2:
1408                     if (pureInteger)
1409                         return VERTEX_FORMAT_SINT2_INT;
1410                     if (normalized)
1411                         return VERTEX_FORMAT_SINT2_NORM;
1412                     return VERTEX_FORMAT_SINT2;
1413                 case 3:
1414                     if (pureInteger)
1415                         return VERTEX_FORMAT_SINT3_INT;
1416                     if (normalized)
1417                         return VERTEX_FORMAT_SINT3_NORM;
1418                     return VERTEX_FORMAT_SINT3;
1419                 case 4:
1420                     if (pureInteger)
1421                         return VERTEX_FORMAT_SINT4_INT;
1422                     if (normalized)
1423                         return VERTEX_FORMAT_SINT4_NORM;
1424                     return VERTEX_FORMAT_SINT4;
1425                 default:
1426                     UNREACHABLE();
1427                     break;
1428             }
1429         case GL_UNSIGNED_INT:
1430             switch (components)
1431             {
1432                 case 1:
1433                     if (pureInteger)
1434                         return VERTEX_FORMAT_UINT1_INT;
1435                     if (normalized)
1436                         return VERTEX_FORMAT_UINT1_NORM;
1437                     return VERTEX_FORMAT_UINT1;
1438                 case 2:
1439                     if (pureInteger)
1440                         return VERTEX_FORMAT_UINT2_INT;
1441                     if (normalized)
1442                         return VERTEX_FORMAT_UINT2_NORM;
1443                     return VERTEX_FORMAT_UINT2;
1444                 case 3:
1445                     if (pureInteger)
1446                         return VERTEX_FORMAT_UINT3_INT;
1447                     if (normalized)
1448                         return VERTEX_FORMAT_UINT3_NORM;
1449                     return VERTEX_FORMAT_UINT3;
1450                 case 4:
1451                     if (pureInteger)
1452                         return VERTEX_FORMAT_UINT4_INT;
1453                     if (normalized)
1454                         return VERTEX_FORMAT_UINT4_NORM;
1455                     return VERTEX_FORMAT_UINT4;
1456                 default:
1457                     UNREACHABLE();
1458                     break;
1459             }
1460         case GL_FLOAT:
1461             switch (components)
1462             {
1463                 case 1:
1464                     return VERTEX_FORMAT_FLOAT1;
1465                 case 2:
1466                     return VERTEX_FORMAT_FLOAT2;
1467                 case 3:
1468                     return VERTEX_FORMAT_FLOAT3;
1469                 case 4:
1470                     return VERTEX_FORMAT_FLOAT4;
1471                 default:
1472                     UNREACHABLE();
1473                     break;
1474             }
1475         case GL_HALF_FLOAT:
1476             switch (components)
1477             {
1478                 case 1:
1479                     return VERTEX_FORMAT_HALF1;
1480                 case 2:
1481                     return VERTEX_FORMAT_HALF2;
1482                 case 3:
1483                     return VERTEX_FORMAT_HALF3;
1484                 case 4:
1485                     return VERTEX_FORMAT_HALF4;
1486                 default:
1487                     UNREACHABLE();
1488                     break;
1489             }
1490         case GL_FIXED:
1491             switch (components)
1492             {
1493                 case 1:
1494                     return VERTEX_FORMAT_FIXED1;
1495                 case 2:
1496                     return VERTEX_FORMAT_FIXED2;
1497                 case 3:
1498                     return VERTEX_FORMAT_FIXED3;
1499                 case 4:
1500                     return VERTEX_FORMAT_FIXED4;
1501                 default:
1502                     UNREACHABLE();
1503                     break;
1504             }
1505         case GL_INT_2_10_10_10_REV:
1506             if (pureInteger)
1507                 return VERTEX_FORMAT_SINT210_INT;
1508             if (normalized)
1509                 return VERTEX_FORMAT_SINT210_NORM;
1510             return VERTEX_FORMAT_SINT210;
1511         case GL_UNSIGNED_INT_2_10_10_10_REV:
1512             if (pureInteger)
1513                 return VERTEX_FORMAT_UINT210_INT;
1514             if (normalized)
1515                 return VERTEX_FORMAT_UINT210_NORM;
1516             return VERTEX_FORMAT_UINT210;
1517         default:
1518             UNREACHABLE();
1519             break;
1520     }
1521     return VERTEX_FORMAT_UBYTE1;
1522 }
1523 
GetVertexFormatType(const VertexAttribute & attrib)1524 VertexFormatType GetVertexFormatType(const VertexAttribute &attrib)
1525 {
1526     return GetVertexFormatType(attrib.type, attrib.normalized, attrib.size, attrib.pureInteger);
1527 }
1528 
GetVertexFormatType(const VertexAttribute & attrib,GLenum currentValueType)1529 VertexFormatType GetVertexFormatType(const VertexAttribute &attrib, GLenum currentValueType)
1530 {
1531     if (!attrib.enabled)
1532     {
1533         return GetVertexFormatType(currentValueType, GL_FALSE, 4, (currentValueType != GL_FLOAT));
1534     }
1535     return GetVertexFormatType(attrib);
1536 }
1537 
GetVertexFormatFromType(VertexFormatType vertexFormatType)1538 const VertexFormat &GetVertexFormatFromType(VertexFormatType vertexFormatType)
1539 {
1540     switch (vertexFormatType)
1541     {
1542         case VERTEX_FORMAT_SBYTE1:
1543         {
1544             static const VertexFormat format(GL_BYTE, GL_FALSE, 1, false);
1545             return format;
1546         }
1547         case VERTEX_FORMAT_SBYTE1_NORM:
1548         {
1549             static const VertexFormat format(GL_BYTE, GL_TRUE, 1, false);
1550             return format;
1551         }
1552         case VERTEX_FORMAT_SBYTE2:
1553         {
1554             static const VertexFormat format(GL_BYTE, GL_FALSE, 2, false);
1555             return format;
1556         }
1557         case VERTEX_FORMAT_SBYTE2_NORM:
1558         {
1559             static const VertexFormat format(GL_BYTE, GL_TRUE, 2, false);
1560             return format;
1561         }
1562         case VERTEX_FORMAT_SBYTE3:
1563         {
1564             static const VertexFormat format(GL_BYTE, GL_FALSE, 3, false);
1565             return format;
1566         }
1567         case VERTEX_FORMAT_SBYTE3_NORM:
1568         {
1569             static const VertexFormat format(GL_BYTE, GL_TRUE, 3, false);
1570             return format;
1571         }
1572         case VERTEX_FORMAT_SBYTE4:
1573         {
1574             static const VertexFormat format(GL_BYTE, GL_FALSE, 4, false);
1575             return format;
1576         }
1577         case VERTEX_FORMAT_SBYTE4_NORM:
1578         {
1579             static const VertexFormat format(GL_BYTE, GL_TRUE, 4, false);
1580             return format;
1581         }
1582         case VERTEX_FORMAT_UBYTE1:
1583         {
1584             static const VertexFormat format(GL_UNSIGNED_BYTE, GL_FALSE, 1, false);
1585             return format;
1586         }
1587         case VERTEX_FORMAT_UBYTE1_NORM:
1588         {
1589             static const VertexFormat format(GL_UNSIGNED_BYTE, GL_TRUE, 1, false);
1590             return format;
1591         }
1592         case VERTEX_FORMAT_UBYTE2:
1593         {
1594             static const VertexFormat format(GL_UNSIGNED_BYTE, GL_FALSE, 2, false);
1595             return format;
1596         }
1597         case VERTEX_FORMAT_UBYTE2_NORM:
1598         {
1599             static const VertexFormat format(GL_UNSIGNED_BYTE, GL_TRUE, 2, false);
1600             return format;
1601         }
1602         case VERTEX_FORMAT_UBYTE3:
1603         {
1604             static const VertexFormat format(GL_UNSIGNED_BYTE, GL_FALSE, 3, false);
1605             return format;
1606         }
1607         case VERTEX_FORMAT_UBYTE3_NORM:
1608         {
1609             static const VertexFormat format(GL_UNSIGNED_BYTE, GL_TRUE, 3, false);
1610             return format;
1611         }
1612         case VERTEX_FORMAT_UBYTE4:
1613         {
1614             static const VertexFormat format(GL_UNSIGNED_BYTE, GL_FALSE, 4, false);
1615             return format;
1616         }
1617         case VERTEX_FORMAT_UBYTE4_NORM:
1618         {
1619             static const VertexFormat format(GL_UNSIGNED_BYTE, GL_TRUE, 4, false);
1620             return format;
1621         }
1622         case VERTEX_FORMAT_SSHORT1:
1623         {
1624             static const VertexFormat format(GL_SHORT, GL_FALSE, 1, false);
1625             return format;
1626         }
1627         case VERTEX_FORMAT_SSHORT1_NORM:
1628         {
1629             static const VertexFormat format(GL_SHORT, GL_TRUE, 1, false);
1630             return format;
1631         }
1632         case VERTEX_FORMAT_SSHORT2:
1633         {
1634             static const VertexFormat format(GL_SHORT, GL_FALSE, 2, false);
1635             return format;
1636         }
1637         case VERTEX_FORMAT_SSHORT2_NORM:
1638         {
1639             static const VertexFormat format(GL_SHORT, GL_TRUE, 2, false);
1640             return format;
1641         }
1642         case VERTEX_FORMAT_SSHORT3:
1643         {
1644             static const VertexFormat format(GL_SHORT, GL_FALSE, 3, false);
1645             return format;
1646         }
1647         case VERTEX_FORMAT_SSHORT3_NORM:
1648         {
1649             static const VertexFormat format(GL_SHORT, GL_TRUE, 3, false);
1650             return format;
1651         }
1652         case VERTEX_FORMAT_SSHORT4:
1653         {
1654             static const VertexFormat format(GL_SHORT, GL_FALSE, 4, false);
1655             return format;
1656         }
1657         case VERTEX_FORMAT_SSHORT4_NORM:
1658         {
1659             static const VertexFormat format(GL_SHORT, GL_TRUE, 4, false);
1660             return format;
1661         }
1662         case VERTEX_FORMAT_USHORT1:
1663         {
1664             static const VertexFormat format(GL_UNSIGNED_SHORT, GL_FALSE, 1, false);
1665             return format;
1666         }
1667         case VERTEX_FORMAT_USHORT1_NORM:
1668         {
1669             static const VertexFormat format(GL_UNSIGNED_SHORT, GL_TRUE, 1, false);
1670             return format;
1671         }
1672         case VERTEX_FORMAT_USHORT2:
1673         {
1674             static const VertexFormat format(GL_UNSIGNED_SHORT, GL_FALSE, 2, false);
1675             return format;
1676         }
1677         case VERTEX_FORMAT_USHORT2_NORM:
1678         {
1679             static const VertexFormat format(GL_UNSIGNED_SHORT, GL_TRUE, 2, false);
1680             return format;
1681         }
1682         case VERTEX_FORMAT_USHORT3:
1683         {
1684             static const VertexFormat format(GL_UNSIGNED_SHORT, GL_FALSE, 3, false);
1685             return format;
1686         }
1687         case VERTEX_FORMAT_USHORT3_NORM:
1688         {
1689             static const VertexFormat format(GL_UNSIGNED_SHORT, GL_TRUE, 3, false);
1690             return format;
1691         }
1692         case VERTEX_FORMAT_USHORT4:
1693         {
1694             static const VertexFormat format(GL_UNSIGNED_SHORT, GL_FALSE, 4, false);
1695             return format;
1696         }
1697         case VERTEX_FORMAT_USHORT4_NORM:
1698         {
1699             static const VertexFormat format(GL_UNSIGNED_SHORT, GL_TRUE, 4, false);
1700             return format;
1701         }
1702         case VERTEX_FORMAT_SINT1:
1703         {
1704             static const VertexFormat format(GL_INT, GL_FALSE, 1, false);
1705             return format;
1706         }
1707         case VERTEX_FORMAT_SINT1_NORM:
1708         {
1709             static const VertexFormat format(GL_INT, GL_TRUE, 1, false);
1710             return format;
1711         }
1712         case VERTEX_FORMAT_SINT2:
1713         {
1714             static const VertexFormat format(GL_INT, GL_FALSE, 2, false);
1715             return format;
1716         }
1717         case VERTEX_FORMAT_SINT2_NORM:
1718         {
1719             static const VertexFormat format(GL_INT, GL_TRUE, 2, false);
1720             return format;
1721         }
1722         case VERTEX_FORMAT_SINT3:
1723         {
1724             static const VertexFormat format(GL_INT, GL_FALSE, 3, false);
1725             return format;
1726         }
1727         case VERTEX_FORMAT_SINT3_NORM:
1728         {
1729             static const VertexFormat format(GL_INT, GL_TRUE, 3, false);
1730             return format;
1731         }
1732         case VERTEX_FORMAT_SINT4:
1733         {
1734             static const VertexFormat format(GL_INT, GL_FALSE, 4, false);
1735             return format;
1736         }
1737         case VERTEX_FORMAT_SINT4_NORM:
1738         {
1739             static const VertexFormat format(GL_INT, GL_TRUE, 4, false);
1740             return format;
1741         }
1742         case VERTEX_FORMAT_UINT1:
1743         {
1744             static const VertexFormat format(GL_UNSIGNED_INT, GL_FALSE, 1, false);
1745             return format;
1746         }
1747         case VERTEX_FORMAT_UINT1_NORM:
1748         {
1749             static const VertexFormat format(GL_UNSIGNED_INT, GL_TRUE, 1, false);
1750             return format;
1751         }
1752         case VERTEX_FORMAT_UINT2:
1753         {
1754             static const VertexFormat format(GL_UNSIGNED_INT, GL_FALSE, 2, false);
1755             return format;
1756         }
1757         case VERTEX_FORMAT_UINT2_NORM:
1758         {
1759             static const VertexFormat format(GL_UNSIGNED_INT, GL_TRUE, 2, false);
1760             return format;
1761         }
1762         case VERTEX_FORMAT_UINT3:
1763         {
1764             static const VertexFormat format(GL_UNSIGNED_INT, GL_FALSE, 3, false);
1765             return format;
1766         }
1767         case VERTEX_FORMAT_UINT3_NORM:
1768         {
1769             static const VertexFormat format(GL_UNSIGNED_INT, GL_TRUE, 3, false);
1770             return format;
1771         }
1772         case VERTEX_FORMAT_UINT4:
1773         {
1774             static const VertexFormat format(GL_UNSIGNED_INT, GL_FALSE, 4, false);
1775             return format;
1776         }
1777         case VERTEX_FORMAT_UINT4_NORM:
1778         {
1779             static const VertexFormat format(GL_UNSIGNED_INT, GL_TRUE, 4, false);
1780             return format;
1781         }
1782         case VERTEX_FORMAT_SBYTE1_INT:
1783         {
1784             static const VertexFormat format(GL_BYTE, GL_FALSE, 1, true);
1785             return format;
1786         }
1787         case VERTEX_FORMAT_SBYTE2_INT:
1788         {
1789             static const VertexFormat format(GL_BYTE, GL_FALSE, 2, true);
1790             return format;
1791         }
1792         case VERTEX_FORMAT_SBYTE3_INT:
1793         {
1794             static const VertexFormat format(GL_BYTE, GL_FALSE, 3, true);
1795             return format;
1796         }
1797         case VERTEX_FORMAT_SBYTE4_INT:
1798         {
1799             static const VertexFormat format(GL_BYTE, GL_FALSE, 4, true);
1800             return format;
1801         }
1802         case VERTEX_FORMAT_UBYTE1_INT:
1803         {
1804             static const VertexFormat format(GL_UNSIGNED_BYTE, GL_FALSE, 1, true);
1805             return format;
1806         }
1807         case VERTEX_FORMAT_UBYTE2_INT:
1808         {
1809             static const VertexFormat format(GL_UNSIGNED_BYTE, GL_FALSE, 2, true);
1810             return format;
1811         }
1812         case VERTEX_FORMAT_UBYTE3_INT:
1813         {
1814             static const VertexFormat format(GL_UNSIGNED_BYTE, GL_FALSE, 3, true);
1815             return format;
1816         }
1817         case VERTEX_FORMAT_UBYTE4_INT:
1818         {
1819             static const VertexFormat format(GL_UNSIGNED_BYTE, GL_FALSE, 4, true);
1820             return format;
1821         }
1822         case VERTEX_FORMAT_SSHORT1_INT:
1823         {
1824             static const VertexFormat format(GL_SHORT, GL_FALSE, 1, true);
1825             return format;
1826         }
1827         case VERTEX_FORMAT_SSHORT2_INT:
1828         {
1829             static const VertexFormat format(GL_SHORT, GL_FALSE, 2, true);
1830             return format;
1831         }
1832         case VERTEX_FORMAT_SSHORT3_INT:
1833         {
1834             static const VertexFormat format(GL_SHORT, GL_FALSE, 3, true);
1835             return format;
1836         }
1837         case VERTEX_FORMAT_SSHORT4_INT:
1838         {
1839             static const VertexFormat format(GL_SHORT, GL_FALSE, 4, true);
1840             return format;
1841         }
1842         case VERTEX_FORMAT_USHORT1_INT:
1843         {
1844             static const VertexFormat format(GL_UNSIGNED_SHORT, GL_FALSE, 1, true);
1845             return format;
1846         }
1847         case VERTEX_FORMAT_USHORT2_INT:
1848         {
1849             static const VertexFormat format(GL_UNSIGNED_SHORT, GL_FALSE, 2, true);
1850             return format;
1851         }
1852         case VERTEX_FORMAT_USHORT3_INT:
1853         {
1854             static const VertexFormat format(GL_UNSIGNED_SHORT, GL_FALSE, 3, true);
1855             return format;
1856         }
1857         case VERTEX_FORMAT_USHORT4_INT:
1858         {
1859             static const VertexFormat format(GL_UNSIGNED_SHORT, GL_FALSE, 4, true);
1860             return format;
1861         }
1862         case VERTEX_FORMAT_SINT1_INT:
1863         {
1864             static const VertexFormat format(GL_INT, GL_FALSE, 1, true);
1865             return format;
1866         }
1867         case VERTEX_FORMAT_SINT2_INT:
1868         {
1869             static const VertexFormat format(GL_INT, GL_FALSE, 2, true);
1870             return format;
1871         }
1872         case VERTEX_FORMAT_SINT3_INT:
1873         {
1874             static const VertexFormat format(GL_INT, GL_FALSE, 3, true);
1875             return format;
1876         }
1877         case VERTEX_FORMAT_SINT4_INT:
1878         {
1879             static const VertexFormat format(GL_INT, GL_FALSE, 4, true);
1880             return format;
1881         }
1882         case VERTEX_FORMAT_UINT1_INT:
1883         {
1884             static const VertexFormat format(GL_UNSIGNED_INT, GL_FALSE, 1, true);
1885             return format;
1886         }
1887         case VERTEX_FORMAT_UINT2_INT:
1888         {
1889             static const VertexFormat format(GL_UNSIGNED_INT, GL_FALSE, 2, true);
1890             return format;
1891         }
1892         case VERTEX_FORMAT_UINT3_INT:
1893         {
1894             static const VertexFormat format(GL_UNSIGNED_INT, GL_FALSE, 3, true);
1895             return format;
1896         }
1897         case VERTEX_FORMAT_UINT4_INT:
1898         {
1899             static const VertexFormat format(GL_UNSIGNED_INT, GL_FALSE, 4, true);
1900             return format;
1901         }
1902         case VERTEX_FORMAT_FIXED1:
1903         {
1904             static const VertexFormat format(GL_FIXED, GL_FALSE, 1, false);
1905             return format;
1906         }
1907         case VERTEX_FORMAT_FIXED2:
1908         {
1909             static const VertexFormat format(GL_FIXED, GL_FALSE, 2, false);
1910             return format;
1911         }
1912         case VERTEX_FORMAT_FIXED3:
1913         {
1914             static const VertexFormat format(GL_FIXED, GL_FALSE, 3, false);
1915             return format;
1916         }
1917         case VERTEX_FORMAT_FIXED4:
1918         {
1919             static const VertexFormat format(GL_FIXED, GL_FALSE, 4, false);
1920             return format;
1921         }
1922         case VERTEX_FORMAT_HALF1:
1923         {
1924             static const VertexFormat format(GL_HALF_FLOAT, GL_FALSE, 1, false);
1925             return format;
1926         }
1927         case VERTEX_FORMAT_HALF2:
1928         {
1929             static const VertexFormat format(GL_HALF_FLOAT, GL_FALSE, 2, false);
1930             return format;
1931         }
1932         case VERTEX_FORMAT_HALF3:
1933         {
1934             static const VertexFormat format(GL_HALF_FLOAT, GL_FALSE, 3, false);
1935             return format;
1936         }
1937         case VERTEX_FORMAT_HALF4:
1938         {
1939             static const VertexFormat format(GL_HALF_FLOAT, GL_FALSE, 4, false);
1940             return format;
1941         }
1942         case VERTEX_FORMAT_FLOAT1:
1943         {
1944             static const VertexFormat format(GL_FLOAT, GL_FALSE, 1, false);
1945             return format;
1946         }
1947         case VERTEX_FORMAT_FLOAT2:
1948         {
1949             static const VertexFormat format(GL_FLOAT, GL_FALSE, 2, false);
1950             return format;
1951         }
1952         case VERTEX_FORMAT_FLOAT3:
1953         {
1954             static const VertexFormat format(GL_FLOAT, GL_FALSE, 3, false);
1955             return format;
1956         }
1957         case VERTEX_FORMAT_FLOAT4:
1958         {
1959             static const VertexFormat format(GL_FLOAT, GL_FALSE, 4, false);
1960             return format;
1961         }
1962         case VERTEX_FORMAT_SINT210:
1963         {
1964             static const VertexFormat format(GL_INT_2_10_10_10_REV, GL_FALSE, 4, false);
1965             return format;
1966         }
1967         case VERTEX_FORMAT_UINT210:
1968         {
1969             static const VertexFormat format(GL_UNSIGNED_INT_2_10_10_10_REV, GL_FALSE, 4, false);
1970             return format;
1971         }
1972         case VERTEX_FORMAT_SINT210_NORM:
1973         {
1974             static const VertexFormat format(GL_INT_2_10_10_10_REV, GL_TRUE, 4, false);
1975             return format;
1976         }
1977         case VERTEX_FORMAT_UINT210_NORM:
1978         {
1979             static const VertexFormat format(GL_UNSIGNED_INT_2_10_10_10_REV, GL_TRUE, 4, false);
1980             return format;
1981         }
1982         case VERTEX_FORMAT_SINT210_INT:
1983         {
1984             static const VertexFormat format(GL_INT_2_10_10_10_REV, GL_FALSE, 4, true);
1985             return format;
1986         }
1987         case VERTEX_FORMAT_UINT210_INT:
1988         {
1989             static const VertexFormat format(GL_UNSIGNED_INT_2_10_10_10_REV, GL_FALSE, 4, true);
1990             return format;
1991         }
1992         default:
1993         {
1994             static const VertexFormat format(GL_NONE, GL_FALSE, 0, false);
1995             return format;
1996         }
1997     }
1998 }
1999 
GetVertexFormatTypeSize(VertexFormatType vertexFormatType)2000 size_t GetVertexFormatTypeSize(VertexFormatType vertexFormatType)
2001 {
2002     switch (vertexFormatType)
2003     {
2004         case VERTEX_FORMAT_SBYTE1:
2005         case VERTEX_FORMAT_SBYTE1_NORM:
2006         case VERTEX_FORMAT_UBYTE1:
2007         case VERTEX_FORMAT_UBYTE1_NORM:
2008         case VERTEX_FORMAT_SBYTE1_INT:
2009         case VERTEX_FORMAT_UBYTE1_INT:
2010             return 1;
2011 
2012         case VERTEX_FORMAT_SBYTE2:
2013         case VERTEX_FORMAT_SBYTE2_NORM:
2014         case VERTEX_FORMAT_UBYTE2:
2015         case VERTEX_FORMAT_UBYTE2_NORM:
2016         case VERTEX_FORMAT_SBYTE2_INT:
2017         case VERTEX_FORMAT_UBYTE2_INT:
2018         case VERTEX_FORMAT_SSHORT1:
2019         case VERTEX_FORMAT_SSHORT1_NORM:
2020         case VERTEX_FORMAT_USHORT1:
2021         case VERTEX_FORMAT_USHORT1_NORM:
2022         case VERTEX_FORMAT_SSHORT1_INT:
2023         case VERTEX_FORMAT_USHORT1_INT:
2024         case VERTEX_FORMAT_HALF1:
2025             return 2;
2026 
2027         case VERTEX_FORMAT_SBYTE3:
2028         case VERTEX_FORMAT_SBYTE3_NORM:
2029         case VERTEX_FORMAT_UBYTE3:
2030         case VERTEX_FORMAT_UBYTE3_NORM:
2031         case VERTEX_FORMAT_SBYTE3_INT:
2032         case VERTEX_FORMAT_UBYTE3_INT:
2033             return 3;
2034 
2035         case VERTEX_FORMAT_SBYTE4:
2036         case VERTEX_FORMAT_SBYTE4_NORM:
2037         case VERTEX_FORMAT_UBYTE4:
2038         case VERTEX_FORMAT_UBYTE4_NORM:
2039         case VERTEX_FORMAT_SBYTE4_INT:
2040         case VERTEX_FORMAT_UBYTE4_INT:
2041         case VERTEX_FORMAT_SSHORT2:
2042         case VERTEX_FORMAT_SSHORT2_NORM:
2043         case VERTEX_FORMAT_USHORT2:
2044         case VERTEX_FORMAT_USHORT2_NORM:
2045         case VERTEX_FORMAT_SSHORT2_INT:
2046         case VERTEX_FORMAT_USHORT2_INT:
2047         case VERTEX_FORMAT_SINT1:
2048         case VERTEX_FORMAT_SINT1_NORM:
2049         case VERTEX_FORMAT_UINT1:
2050         case VERTEX_FORMAT_UINT1_NORM:
2051         case VERTEX_FORMAT_SINT1_INT:
2052         case VERTEX_FORMAT_UINT1_INT:
2053         case VERTEX_FORMAT_HALF2:
2054         case VERTEX_FORMAT_FIXED1:
2055         case VERTEX_FORMAT_FLOAT1:
2056         case VERTEX_FORMAT_SINT210:
2057         case VERTEX_FORMAT_UINT210:
2058         case VERTEX_FORMAT_SINT210_NORM:
2059         case VERTEX_FORMAT_UINT210_NORM:
2060         case VERTEX_FORMAT_SINT210_INT:
2061         case VERTEX_FORMAT_UINT210_INT:
2062             return 4;
2063 
2064         case VERTEX_FORMAT_SSHORT3:
2065         case VERTEX_FORMAT_SSHORT3_NORM:
2066         case VERTEX_FORMAT_USHORT3:
2067         case VERTEX_FORMAT_USHORT3_NORM:
2068         case VERTEX_FORMAT_SSHORT3_INT:
2069         case VERTEX_FORMAT_USHORT3_INT:
2070         case VERTEX_FORMAT_HALF3:
2071             return 6;
2072 
2073         case VERTEX_FORMAT_SSHORT4:
2074         case VERTEX_FORMAT_SSHORT4_NORM:
2075         case VERTEX_FORMAT_USHORT4:
2076         case VERTEX_FORMAT_USHORT4_NORM:
2077         case VERTEX_FORMAT_SSHORT4_INT:
2078         case VERTEX_FORMAT_USHORT4_INT:
2079         case VERTEX_FORMAT_SINT2:
2080         case VERTEX_FORMAT_SINT2_NORM:
2081         case VERTEX_FORMAT_UINT2:
2082         case VERTEX_FORMAT_UINT2_NORM:
2083         case VERTEX_FORMAT_SINT2_INT:
2084         case VERTEX_FORMAT_UINT2_INT:
2085         case VERTEX_FORMAT_HALF4:
2086         case VERTEX_FORMAT_FIXED2:
2087         case VERTEX_FORMAT_FLOAT2:
2088             return 8;
2089 
2090         case VERTEX_FORMAT_SINT3:
2091         case VERTEX_FORMAT_SINT3_NORM:
2092         case VERTEX_FORMAT_UINT3:
2093         case VERTEX_FORMAT_UINT3_NORM:
2094         case VERTEX_FORMAT_SINT3_INT:
2095         case VERTEX_FORMAT_UINT3_INT:
2096         case VERTEX_FORMAT_FIXED3:
2097         case VERTEX_FORMAT_FLOAT3:
2098             return 12;
2099 
2100         case VERTEX_FORMAT_SINT4:
2101         case VERTEX_FORMAT_SINT4_NORM:
2102         case VERTEX_FORMAT_UINT4:
2103         case VERTEX_FORMAT_UINT4_NORM:
2104         case VERTEX_FORMAT_SINT4_INT:
2105         case VERTEX_FORMAT_UINT4_INT:
2106         case VERTEX_FORMAT_FIXED4:
2107         case VERTEX_FORMAT_FLOAT4:
2108             return 16;
2109 
2110         case VERTEX_FORMAT_INVALID:
2111         default:
2112             UNREACHABLE();
2113             return 0;
2114     }
2115 }
2116 
ValidES3InternalFormat(GLenum internalFormat)2117 bool ValidES3InternalFormat(GLenum internalFormat)
2118 {
2119     const InternalFormatInfoMap &formatMap = GetInternalFormatMap();
2120     return internalFormat != GL_NONE && formatMap.find(internalFormat) != formatMap.end();
2121 }
2122 
VertexFormat(GLenum typeIn,GLboolean normalizedIn,GLuint componentsIn,bool pureIntegerIn)2123 VertexFormat::VertexFormat(GLenum typeIn, GLboolean normalizedIn, GLuint componentsIn, bool pureIntegerIn)
2124     : type(typeIn),
2125       normalized(normalizedIn),
2126       components(componentsIn),
2127       pureInteger(pureIntegerIn)
2128 {
2129     // float -> !normalized
2130     ASSERT(!(type == GL_FLOAT || type == GL_HALF_FLOAT || type == GL_FIXED) || normalized == GL_FALSE);
2131 }
2132 
2133 }
2134