xref: /aosp_15_r20/external/angle/src/libANGLE/renderer/gl/renderergl_utils.cpp (revision 8975f5c5ed3d1c378011245431ada316dfb6f244)
1 //
2 // Copyright 2012 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 // renderergl_utils.cpp: Conversion functions and other utility routines
8 // specific to the OpenGL renderer.
9 
10 #include "libANGLE/renderer/gl/renderergl_utils.h"
11 
12 #include <array>
13 #include <limits>
14 
15 #include "common/android_util.h"
16 #include "common/mathutil.h"
17 #include "common/platform.h"
18 #include "common/string_utils.h"
19 #include "gpu_info_util/SystemInfo.h"
20 #include "libANGLE/Buffer.h"
21 #include "libANGLE/Caps.h"
22 #include "libANGLE/Context.h"
23 #include "libANGLE/formatutils.h"
24 #include "libANGLE/queryconversions.h"
25 #include "libANGLE/renderer/driver_utils.h"
26 #include "libANGLE/renderer/gl/ContextGL.h"
27 #include "libANGLE/renderer/gl/FenceNVGL.h"
28 #include "libANGLE/renderer/gl/FunctionsGL.h"
29 #include "libANGLE/renderer/gl/QueryGL.h"
30 #include "libANGLE/renderer/gl/formatutilsgl.h"
31 #include "platform/autogen/FeaturesGL_autogen.h"
32 #include "platform/autogen/FrontendFeatures_autogen.h"
33 
34 #include <EGL/eglext.h>
35 #include <algorithm>
36 #include <sstream>
37 
38 using angle::CheckedNumeric;
39 
40 namespace rx
41 {
42 
43 namespace
44 {
45 
GetString(const FunctionsGL * functions,GLenum name)46 const char *GetString(const FunctionsGL *functions, GLenum name)
47 {
48     const char *cStr = reinterpret_cast<const char *>(functions->getString(name));
49     if (cStr == nullptr)
50     {
51         return "";
52     }
53     return cStr;
54 }
55 
IsMesa(const FunctionsGL * functions,std::array<int,3> * version)56 bool IsMesa(const FunctionsGL *functions, std::array<int, 3> *version)
57 {
58     ASSERT(version);
59 
60     std::string nativeVersionString(GetString(functions, GL_VERSION));
61     size_t pos = nativeVersionString.find("Mesa");
62     if (pos == std::string::npos)
63     {
64         return false;
65     }
66 
67     int *data = version->data();
68     data[0] = data[1] = data[2] = 0;
69     std::sscanf(nativeVersionString.c_str() + pos, "Mesa %d.%d.%d", data, data + 1, data + 2);
70 
71     return true;
72 }
73 
getAdrenoNumber(const FunctionsGL * functions)74 int getAdrenoNumber(const FunctionsGL *functions)
75 {
76     static int number = -1;
77     if (number == -1)
78     {
79         const char *nativeGLRenderer = GetString(functions, GL_RENDERER);
80         if (std::sscanf(nativeGLRenderer, "Adreno (TM) %d", &number) < 1 &&
81             std::sscanf(nativeGLRenderer, "FD%d", &number) < 1)
82         {
83             number = 0;
84         }
85     }
86     return number;
87 }
88 
GetQualcommVersion(const FunctionsGL * functions)89 int GetQualcommVersion(const FunctionsGL *functions)
90 {
91     static int version = -1;
92     if (version == -1)
93     {
94         const std::string nativeVersionString(GetString(functions, GL_VERSION));
95         const size_t pos = nativeVersionString.find("V@");
96         if (pos == std::string::npos ||
97             std::sscanf(nativeVersionString.c_str() + pos, "V@%d", &version) < 1)
98         {
99             version = 0;
100         }
101     }
102     return version;
103 }
104 
getMaliTNumber(const FunctionsGL * functions)105 int getMaliTNumber(const FunctionsGL *functions)
106 {
107     static int number = -1;
108     if (number == -1)
109     {
110         const char *nativeGLRenderer = GetString(functions, GL_RENDERER);
111         if (std::sscanf(nativeGLRenderer, "Mali-T%d", &number) < 1)
112         {
113             number = 0;
114         }
115     }
116     return number;
117 }
118 
getMaliGNumber(const FunctionsGL * functions)119 int getMaliGNumber(const FunctionsGL *functions)
120 {
121     static int number = -1;
122     if (number == -1)
123     {
124         const char *nativeGLRenderer = GetString(functions, GL_RENDERER);
125         if (std::sscanf(nativeGLRenderer, "Mali-G%d", &number) < 1)
126         {
127             number = 0;
128         }
129     }
130     return number;
131 }
132 
IsAdreno3xx(const FunctionsGL * functions)133 bool IsAdreno3xx(const FunctionsGL *functions)
134 {
135     int number = getAdrenoNumber(functions);
136     return number != 0 && number >= 300 && number < 400;
137 }
138 
IsAdreno42xOr3xx(const FunctionsGL * functions)139 bool IsAdreno42xOr3xx(const FunctionsGL *functions)
140 {
141     int number = getAdrenoNumber(functions);
142     return number != 0 && getAdrenoNumber(functions) < 430;
143 }
144 
IsAdreno4xx(const FunctionsGL * functions)145 bool IsAdreno4xx(const FunctionsGL *functions)
146 {
147     int number = getAdrenoNumber(functions);
148     return number != 0 && number >= 400 && number < 500;
149 }
150 
IsAdreno5xxOrOlder(const FunctionsGL * functions)151 bool IsAdreno5xxOrOlder(const FunctionsGL *functions)
152 {
153     int number = getAdrenoNumber(functions);
154     return number != 0 && number < 600;
155 }
156 
IsAdreno5xx(const FunctionsGL * functions)157 bool IsAdreno5xx(const FunctionsGL *functions)
158 {
159     int number = getAdrenoNumber(functions);
160     return number != 0 && number >= 500 && number < 600;
161 }
162 
IsMaliT8xxOrOlder(const FunctionsGL * functions)163 bool IsMaliT8xxOrOlder(const FunctionsGL *functions)
164 {
165     int number = getMaliTNumber(functions);
166     return number != 0 && number < 900;
167 }
168 
IsMaliG31OrOlder(const FunctionsGL * functions)169 bool IsMaliG31OrOlder(const FunctionsGL *functions)
170 {
171     int number = getMaliGNumber(functions);
172     return number != 0 && number <= 31;
173 }
174 
IsMaliG72OrG76OrG51(const FunctionsGL * functions)175 bool IsMaliG72OrG76OrG51(const FunctionsGL *functions)
176 {
177     int number = getMaliGNumber(functions);
178     return number == 72 || number == 76 || number == 51;
179 }
180 
IsMaliValhall(const FunctionsGL * functions)181 bool IsMaliValhall(const FunctionsGL *functions)
182 {
183     int number = getMaliGNumber(functions);
184     return number == 57 || number == 77 || number == 68 || number == 78 || number == 310 ||
185            number == 510 || number == 610 || number == 710 || number == 615 || number == 715;
186 }
187 
IsPixel7OrPixel8(const FunctionsGL * functions)188 bool IsPixel7OrPixel8(const FunctionsGL *functions)
189 {
190     int number = getMaliGNumber(functions);
191     return number == 710 || number == 715;
192 }
193 
IsAndroidEmulator(const FunctionsGL * functions)194 [[maybe_unused]] bool IsAndroidEmulator(const FunctionsGL *functions)
195 {
196     constexpr char androidEmulator[] = "Android Emulator";
197     const char *nativeGLRenderer     = GetString(functions, GL_RENDERER);
198     return angle::BeginsWith(nativeGLRenderer, androidEmulator);
199 }
200 
IsPowerVrRogue(const FunctionsGL * functions)201 bool IsPowerVrRogue(const FunctionsGL *functions)
202 {
203     constexpr char powerVRRogue[] = "PowerVR Rogue";
204     const char *nativeGLRenderer  = GetString(functions, GL_RENDERER);
205     return angle::BeginsWith(nativeGLRenderer, powerVRRogue);
206 }
207 
208 }  // namespace
209 
SwapControlData()210 SwapControlData::SwapControlData()
211     : targetSwapInterval(0), maxSwapInterval(-1), currentSwapInterval(-1)
212 {}
213 
GetVendorID(const FunctionsGL * functions)214 VendorID GetVendorID(const FunctionsGL *functions)
215 {
216     std::string nativeVendorString(GetString(functions, GL_VENDOR));
217     // Concatenate GL_RENDERER to the string being checked because some vendors put their names in
218     // GL_RENDERER
219     nativeVendorString += " ";
220     nativeVendorString += GetString(functions, GL_RENDERER);
221 
222     if (nativeVendorString.find("NVIDIA") != std::string::npos)
223     {
224         return VENDOR_ID_NVIDIA;
225     }
226     else if (nativeVendorString.find("ATI") != std::string::npos ||
227              nativeVendorString.find("AMD") != std::string::npos ||
228              nativeVendorString.find("Radeon") != std::string::npos)
229     {
230         return VENDOR_ID_AMD;
231     }
232     else if (nativeVendorString.find("Qualcomm") != std::string::npos)
233     {
234         return VENDOR_ID_QUALCOMM;
235     }
236     else if (nativeVendorString.find("Intel") != std::string::npos)
237     {
238         return VENDOR_ID_INTEL;
239     }
240     else if (nativeVendorString.find("Imagination") != std::string::npos)
241     {
242         return VENDOR_ID_POWERVR;
243     }
244     else if (nativeVendorString.find("Vivante") != std::string::npos)
245     {
246         return VENDOR_ID_VIVANTE;
247     }
248     else if (nativeVendorString.find("Mali") != std::string::npos)
249     {
250         return VENDOR_ID_ARM;
251     }
252     else
253     {
254         return VENDOR_ID_UNKNOWN;
255     }
256 }
257 
GetDeviceID(const FunctionsGL * functions)258 uint32_t GetDeviceID(const FunctionsGL *functions)
259 {
260     std::string nativeRendererString(GetString(functions, GL_RENDERER));
261     constexpr std::pair<const char *, uint32_t> kKnownDeviceIDs[] = {
262         {"Adreno (TM) 418", ANDROID_DEVICE_ID_NEXUS5X},
263         {"Adreno (TM) 530", ANDROID_DEVICE_ID_PIXEL1XL},
264         {"Adreno (TM) 540", ANDROID_DEVICE_ID_PIXEL2},
265     };
266 
267     for (const auto &knownDeviceID : kKnownDeviceIDs)
268     {
269         if (nativeRendererString.find(knownDeviceID.first) != std::string::npos)
270         {
271             return knownDeviceID.second;
272         }
273     }
274 
275     return 0;
276 }
277 
GetShaderOutputType(const FunctionsGL * functions)278 ShShaderOutput GetShaderOutputType(const FunctionsGL *functions)
279 {
280     ASSERT(functions);
281 
282     if (functions->standard == STANDARD_GL_DESKTOP)
283     {
284         // GLSL outputs
285         if (functions->isAtLeastGL(gl::Version(4, 5)))
286         {
287             return SH_GLSL_450_CORE_OUTPUT;
288         }
289         else if (functions->isAtLeastGL(gl::Version(4, 4)))
290         {
291             return SH_GLSL_440_CORE_OUTPUT;
292         }
293         else if (functions->isAtLeastGL(gl::Version(4, 3)))
294         {
295             return SH_GLSL_430_CORE_OUTPUT;
296         }
297         else if (functions->isAtLeastGL(gl::Version(4, 2)))
298         {
299             return SH_GLSL_420_CORE_OUTPUT;
300         }
301         else if (functions->isAtLeastGL(gl::Version(4, 1)))
302         {
303             return SH_GLSL_410_CORE_OUTPUT;
304         }
305         else if (functions->isAtLeastGL(gl::Version(4, 0)))
306         {
307             return SH_GLSL_400_CORE_OUTPUT;
308         }
309         else if (functions->isAtLeastGL(gl::Version(3, 3)))
310         {
311             return SH_GLSL_330_CORE_OUTPUT;
312         }
313         else if (functions->isAtLeastGL(gl::Version(3, 2)))
314         {
315             return SH_GLSL_150_CORE_OUTPUT;
316         }
317         else if (functions->isAtLeastGL(gl::Version(3, 1)))
318         {
319             return SH_GLSL_140_OUTPUT;
320         }
321         else if (functions->isAtLeastGL(gl::Version(3, 0)))
322         {
323             return SH_GLSL_130_OUTPUT;
324         }
325         else
326         {
327             return SH_GLSL_COMPATIBILITY_OUTPUT;
328         }
329     }
330     else if (functions->standard == STANDARD_GL_ES)
331     {
332         // ESSL outputs
333         return SH_ESSL_OUTPUT;
334     }
335     else
336     {
337         UNREACHABLE();
338         return ShShaderOutput(0);
339     }
340 }
341 
342 namespace nativegl_gl
343 {
344 
MeetsRequirements(const FunctionsGL * functions,const nativegl::SupportRequirement & requirements)345 static bool MeetsRequirements(const FunctionsGL *functions,
346                               const nativegl::SupportRequirement &requirements)
347 {
348     bool hasRequiredExtensions = false;
349     for (const std::vector<std::string> &exts : requirements.requiredExtensions)
350     {
351         bool hasAllExtensionsInSet = true;
352         for (const std::string &extension : exts)
353         {
354             if (!functions->hasExtension(extension))
355             {
356                 hasAllExtensionsInSet = false;
357                 break;
358             }
359         }
360         if (hasAllExtensionsInSet)
361         {
362             hasRequiredExtensions = true;
363             break;
364         }
365     }
366     if (!requirements.requiredExtensions.empty() && !hasRequiredExtensions)
367     {
368         return false;
369     }
370 
371     if (functions->version >= requirements.version)
372     {
373         return true;
374     }
375     else if (!requirements.versionExtensions.empty())
376     {
377         for (const std::string &extension : requirements.versionExtensions)
378         {
379             if (!functions->hasExtension(extension))
380             {
381                 return false;
382             }
383         }
384         return true;
385     }
386     else
387     {
388         return false;
389     }
390 }
391 
CheckSizedInternalFormatTextureRenderability(const FunctionsGL * functions,const angle::FeaturesGL & features,GLenum internalFormat)392 static bool CheckSizedInternalFormatTextureRenderability(const FunctionsGL *functions,
393                                                          const angle::FeaturesGL &features,
394                                                          GLenum internalFormat)
395 {
396     const gl::InternalFormat &formatInfo = gl::GetSizedInternalFormatInfo(internalFormat);
397     ASSERT(formatInfo.sized);
398 
399     // Query the current texture so it can be rebound afterwards
400     GLint oldTextureBinding = 0;
401     functions->getIntegerv(GL_TEXTURE_BINDING_2D, &oldTextureBinding);
402 
403     // Create a small texture with the same format and type that gl::Texture would use
404     GLuint texture = 0;
405     functions->genTextures(1, &texture);
406     functions->bindTexture(GL_TEXTURE_2D, texture);
407 
408     // Nearest filter needed for framebuffer completeness on some drivers.
409     functions->texParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
410 
411     nativegl::TexImageFormat texImageFormat = nativegl::GetTexImageFormat(
412         functions, features, formatInfo.internalFormat, formatInfo.format, formatInfo.type);
413     constexpr GLsizei kTextureSize = 16;
414     functions->texImage2D(GL_TEXTURE_2D, 0, texImageFormat.internalFormat, kTextureSize,
415                           kTextureSize, 0, texImageFormat.format, texImageFormat.type, nullptr);
416 
417     // Query the current framebuffer so it can be rebound afterwards
418     GLint oldFramebufferBinding = 0;
419     functions->getIntegerv(GL_FRAMEBUFFER_BINDING, &oldFramebufferBinding);
420 
421     // Bind the texture to the framebuffer and check renderability
422     GLuint fbo = 0;
423     functions->genFramebuffers(1, &fbo);
424     functions->bindFramebuffer(GL_FRAMEBUFFER, fbo);
425     functions->framebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture,
426                                     0);
427 
428     bool supported = functions->checkFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE;
429 
430     // Delete the framebuffer and restore the previous binding
431     functions->deleteFramebuffers(1, &fbo);
432     functions->bindFramebuffer(GL_FRAMEBUFFER, static_cast<GLuint>(oldFramebufferBinding));
433 
434     // Delete the texture and restore the previous binding
435     functions->deleteTextures(1, &texture);
436     functions->bindTexture(GL_TEXTURE_2D, static_cast<GLuint>(oldTextureBinding));
437 
438     if (!supported)
439     {
440         ANGLE_GL_CLEAR_ERRORS(functions);
441     }
442 
443     ASSERT(functions->getError() == GL_NO_ERROR);
444     return supported;
445 }
446 
CheckInternalFormatRenderbufferRenderability(const FunctionsGL * functions,const angle::FeaturesGL & features,GLenum internalFormat)447 static bool CheckInternalFormatRenderbufferRenderability(const FunctionsGL *functions,
448                                                          const angle::FeaturesGL &features,
449                                                          GLenum internalFormat)
450 {
451     const gl::InternalFormat &formatInfo = gl::GetSizedInternalFormatInfo(internalFormat);
452     ASSERT(formatInfo.sized);
453 
454     // Query the current renderbuffer so it can be rebound afterwards
455     GLint oldRenderbufferBinding = 0;
456     functions->getIntegerv(GL_RENDERBUFFER_BINDING, &oldRenderbufferBinding);
457 
458     // Create a small renderbuffer with the same format and type that gl::Renderbuffer would use
459     GLuint renderbuffer = 0;
460     functions->genRenderbuffers(1, &renderbuffer);
461     functions->bindRenderbuffer(GL_RENDERBUFFER, renderbuffer);
462 
463     nativegl::RenderbufferFormat renderbufferFormat =
464         nativegl::GetRenderbufferFormat(functions, features, formatInfo.internalFormat);
465     constexpr GLsizei kRenderbufferSize = 16;
466     functions->renderbufferStorage(GL_RENDERBUFFER, renderbufferFormat.internalFormat,
467                                    kRenderbufferSize, kRenderbufferSize);
468 
469     // Query the current framebuffer so it can be rebound afterwards
470     GLint oldFramebufferBinding = 0;
471     functions->getIntegerv(GL_FRAMEBUFFER_BINDING, &oldFramebufferBinding);
472 
473     // Bind the texture to the framebuffer and check renderability
474     GLuint fbo = 0;
475     functions->genFramebuffers(1, &fbo);
476     functions->bindFramebuffer(GL_FRAMEBUFFER, fbo);
477     functions->framebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER,
478                                        renderbuffer);
479 
480     bool supported = functions->checkFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE;
481 
482     // Delete the framebuffer and restore the previous binding
483     functions->deleteFramebuffers(1, &fbo);
484     functions->bindFramebuffer(GL_FRAMEBUFFER, static_cast<GLuint>(oldFramebufferBinding));
485 
486     // Delete the renderbuffer and restore the previous binding
487     functions->deleteRenderbuffers(1, &renderbuffer);
488     functions->bindRenderbuffer(GL_RENDERBUFFER, static_cast<GLuint>(oldRenderbufferBinding));
489 
490     if (!supported)
491     {
492         ANGLE_GL_CLEAR_ERRORS(functions);
493     }
494 
495     ASSERT(functions->getError() == GL_NO_ERROR);
496     return supported;
497 }
498 
LimitVersion(gl::Version * curVersion,const gl::Version & maxVersion)499 static void LimitVersion(gl::Version *curVersion, const gl::Version &maxVersion)
500 {
501     if (*curVersion >= maxVersion)
502     {
503         *curVersion = maxVersion;
504     }
505 }
506 
GenerateTextureFormatCaps(const FunctionsGL * functions,const angle::FeaturesGL & features,GLenum internalFormat,gl::Version * maxSupportedESVersion)507 static gl::TextureCaps GenerateTextureFormatCaps(const FunctionsGL *functions,
508                                                  const angle::FeaturesGL &features,
509                                                  GLenum internalFormat,
510                                                  gl::Version *maxSupportedESVersion)
511 {
512     ASSERT(functions->getError() == GL_NO_ERROR);
513 
514     gl::TextureCaps textureCaps;
515 
516     const nativegl::InternalFormat &formatInfo =
517         nativegl::GetInternalFormatInfo(internalFormat, functions->standard);
518     textureCaps.texturable = MeetsRequirements(functions, formatInfo.texture);
519     textureCaps.filterable =
520         textureCaps.texturable && MeetsRequirements(functions, formatInfo.filter);
521     textureCaps.textureAttachment = MeetsRequirements(functions, formatInfo.textureAttachment);
522     textureCaps.renderbuffer      = MeetsRequirements(functions, formatInfo.renderbuffer);
523     textureCaps.blendable         = textureCaps.renderbuffer || textureCaps.textureAttachment;
524 
525     // Do extra renderability validation for some formats.
526     if (internalFormat == GL_R16F || internalFormat == GL_RG16F || internalFormat == GL_RGB16F)
527     {
528         // SupportRequirement can't currently express a condition of the form (version && extension)
529         // || other extensions, so do the (version && extension) part here.
530         if (functions->isAtLeastGLES(gl::Version(3, 0)) &&
531             functions->hasGLESExtension("GL_EXT_color_buffer_half_float"))
532         {
533             textureCaps.textureAttachment = true;
534             textureCaps.renderbuffer      = true;
535         }
536     }
537 
538     // We require GL_RGBA16F is renderable to expose EXT_color_buffer_half_float but we can't know
539     // if the format is supported unless we try to create a framebuffer.
540     // Renderability of signed normalized formats is optional on desktop GL.
541     if (internalFormat == GL_RGBA16F ||
542         (functions->standard == STANDARD_GL_DESKTOP &&
543          (internalFormat == GL_R8_SNORM || internalFormat == GL_R16_SNORM ||
544           internalFormat == GL_RG8_SNORM || internalFormat == GL_RG16_SNORM ||
545           internalFormat == GL_RGBA8_SNORM || internalFormat == GL_RGBA16_SNORM)))
546     {
547         if (textureCaps.textureAttachment)
548         {
549             textureCaps.textureAttachment =
550                 CheckSizedInternalFormatTextureRenderability(functions, features, internalFormat);
551         }
552         if (textureCaps.renderbuffer)
553         {
554             textureCaps.renderbuffer =
555                 CheckInternalFormatRenderbufferRenderability(functions, features, internalFormat);
556         }
557     }
558 
559     // glGetInternalformativ is not available until version 4.2 but may be available through the 3.0
560     // extension GL_ARB_internalformat_query
561     if (textureCaps.renderbuffer && functions->getInternalformativ)
562     {
563         GLenum queryInternalFormat = internalFormat;
564 
565         if (internalFormat == GL_BGRA8_EXT)
566         {
567             // Querying GL_NUM_SAMPLE_COUNTS for GL_BGRA8_EXT generates an INVALID_ENUM on some
568             // drivers.  It seems however that allocating a multisampled renderbuffer of this format
569             // succeeds. To avoid breaking multisampling for this format, query the supported sample
570             // counts for GL_RGBA8 instead.
571             queryInternalFormat = GL_RGBA8;
572         }
573 
574         ANGLE_GL_CLEAR_ERRORS(functions);
575         GLint numSamples = 0;
576         functions->getInternalformativ(GL_RENDERBUFFER, queryInternalFormat, GL_NUM_SAMPLE_COUNTS,
577                                        1, &numSamples);
578         GLenum error = functions->getError();
579         if (error != GL_NO_ERROR)
580         {
581             ERR() << "glGetInternalformativ generated error " << gl::FmtHex(error) << " for format "
582                   << gl::FmtHex(queryInternalFormat) << ". Skipping multisample checks.";
583             numSamples = 0;
584         }
585 
586         if (numSamples > 0)
587         {
588             std::vector<GLint> samples(numSamples);
589             functions->getInternalformativ(GL_RENDERBUFFER, queryInternalFormat, GL_SAMPLES,
590                                            static_cast<GLsizei>(samples.size()), &samples[0]);
591 
592             for (size_t sampleIndex = 0; sampleIndex < samples.size(); sampleIndex++)
593             {
594                 if (features.limitMaxMSAASamplesTo4.enabled && samples[sampleIndex] > 4)
595                 {
596                     continue;
597                 }
598 
599                 // Some NVIDIA drivers expose multisampling modes implemented as a combination of
600                 // multisampling and supersampling. These are non-conformant and should not be
601                 // exposed through ANGLE. Query which formats are conformant from the driver if
602                 // supported.
603                 GLint conformant = GL_TRUE;
604                 if (functions->getInternalformatSampleivNV)
605                 {
606                     ASSERT(functions->getError() == GL_NO_ERROR);
607                     functions->getInternalformatSampleivNV(GL_RENDERBUFFER, queryInternalFormat,
608                                                            samples[sampleIndex], GL_CONFORMANT_NV,
609                                                            1, &conformant);
610                     // getInternalFormatSampleivNV does not work for all formats on NVIDIA Shield TV
611                     // drivers. Assume that formats with large sample counts are non-conformant in
612                     // case the query generates an error.
613                     if (functions->getError() != GL_NO_ERROR)
614                     {
615                         conformant = (samples[sampleIndex] <= 8) ? GL_TRUE : GL_FALSE;
616                     }
617                 }
618                 if (conformant == GL_TRUE)
619                 {
620                     textureCaps.sampleCounts.insert(samples[sampleIndex]);
621                 }
622             }
623         }
624     }
625 
626     // GLES 3.0.5 section 4.4.2.2: "Implementations must support creation of renderbuffers in these
627     // required formats with up to the value of MAX_SAMPLES multisamples, with the exception of
628     // signed and unsigned integer formats."
629     const gl::InternalFormat &glFormatInfo = gl::GetSizedInternalFormatInfo(internalFormat);
630     if (textureCaps.renderbuffer && !glFormatInfo.isInt() &&
631         glFormatInfo.isRequiredRenderbufferFormat(gl::Version(3, 0)) &&
632         textureCaps.getMaxSamples() < 4)
633     {
634         LimitVersion(maxSupportedESVersion, gl::Version(2, 0));
635     }
636 
637     ASSERT(functions->getError() == GL_NO_ERROR);
638     return textureCaps;
639 }
640 
QuerySingleGLInt(const FunctionsGL * functions,GLenum name)641 static GLint QuerySingleGLInt(const FunctionsGL *functions, GLenum name)
642 {
643     GLint result = 0;
644     functions->getIntegerv(name, &result);
645     return result;
646 }
647 
QuerySingleIndexGLInt(const FunctionsGL * functions,GLenum name,GLuint index)648 static GLint QuerySingleIndexGLInt(const FunctionsGL *functions, GLenum name, GLuint index)
649 {
650     GLint result;
651     functions->getIntegeri_v(name, index, &result);
652     return result;
653 }
654 
QueryGLIntRange(const FunctionsGL * functions,GLenum name,size_t index)655 static GLint QueryGLIntRange(const FunctionsGL *functions, GLenum name, size_t index)
656 {
657     GLint result[2] = {};
658     functions->getIntegerv(name, result);
659     return result[index];
660 }
661 
QuerySingleGLInt64(const FunctionsGL * functions,GLenum name)662 static GLint64 QuerySingleGLInt64(const FunctionsGL *functions, GLenum name)
663 {
664     // Fall back to 32-bit int if 64-bit query is not available. This can become relevant for some
665     // caps that are defined as 64-bit values in core spec, but were introduced earlier in
666     // extensions as 32-bit. Triggered in some cases by RenderDoc's emulated OpenGL driver.
667     if (!functions->getInteger64v)
668     {
669         GLint result = 0;
670         functions->getIntegerv(name, &result);
671         return static_cast<GLint64>(result);
672     }
673     else
674     {
675         GLint64 result = 0;
676         functions->getInteger64v(name, &result);
677         return result;
678     }
679 }
680 
QuerySingleGLFloat(const FunctionsGL * functions,GLenum name)681 static GLfloat QuerySingleGLFloat(const FunctionsGL *functions, GLenum name)
682 {
683     GLfloat result = 0.0f;
684     functions->getFloatv(name, &result);
685     return result;
686 }
687 
QueryGLFloatRange(const FunctionsGL * functions,GLenum name,size_t index)688 static GLfloat QueryGLFloatRange(const FunctionsGL *functions, GLenum name, size_t index)
689 {
690     GLfloat result[2] = {};
691     functions->getFloatv(name, result);
692     return result[index];
693 }
694 
QueryTypePrecision(const FunctionsGL * functions,GLenum shaderType,GLenum precisionType)695 static gl::TypePrecision QueryTypePrecision(const FunctionsGL *functions,
696                                             GLenum shaderType,
697                                             GLenum precisionType)
698 {
699     gl::TypePrecision precision;
700     functions->getShaderPrecisionFormat(shaderType, precisionType, precision.range.data(),
701                                         &precision.precision);
702     return precision;
703 }
704 
QueryQueryValue(const FunctionsGL * functions,GLenum target,GLenum name)705 static GLint QueryQueryValue(const FunctionsGL *functions, GLenum target, GLenum name)
706 {
707     GLint result;
708     functions->getQueryiv(target, name, &result);
709     return result;
710 }
711 
CapCombinedLimitToESShaders(GLint * combinedLimit,gl::ShaderMap<GLint> & perShaderLimit)712 void CapCombinedLimitToESShaders(GLint *combinedLimit, gl::ShaderMap<GLint> &perShaderLimit)
713 {
714     GLint combinedESLimit = 0;
715     for (gl::ShaderType shaderType : gl::kAllGraphicsShaderTypes)
716     {
717         combinedESLimit += perShaderLimit[shaderType];
718     }
719 
720     *combinedLimit = std::min(*combinedLimit, combinedESLimit);
721 }
722 
GenerateCaps(const FunctionsGL * functions,const angle::FeaturesGL & features,gl::Caps * caps,gl::TextureCapsMap * textureCapsMap,gl::Extensions * extensions,gl::Limitations * limitations,gl::Version * maxSupportedESVersion,MultiviewImplementationTypeGL * multiviewImplementationType,ShPixelLocalStorageOptions * plsOptions)723 void GenerateCaps(const FunctionsGL *functions,
724                   const angle::FeaturesGL &features,
725                   gl::Caps *caps,
726                   gl::TextureCapsMap *textureCapsMap,
727                   gl::Extensions *extensions,
728                   gl::Limitations *limitations,
729                   gl::Version *maxSupportedESVersion,
730                   MultiviewImplementationTypeGL *multiviewImplementationType,
731                   ShPixelLocalStorageOptions *plsOptions)
732 {
733     // Start by assuming ES3.1 support and work down
734     *maxSupportedESVersion = gl::Version(3, 1);
735 
736     // Texture format support checks
737     const gl::FormatSet &allFormats = gl::GetAllSizedInternalFormats();
738     for (GLenum internalFormat : allFormats)
739     {
740         gl::TextureCaps textureCaps =
741             GenerateTextureFormatCaps(functions, features, internalFormat, maxSupportedESVersion);
742         textureCapsMap->insert(internalFormat, textureCaps);
743     }
744 
745     // Table 6.28, implementation dependent values
746     if (functions->isAtLeastGL(gl::Version(4, 3)) ||
747         functions->hasGLExtension("GL_ARB_ES3_compatibility") ||
748         functions->isAtLeastGLES(gl::Version(3, 0)))
749     {
750         caps->maxElementIndex = QuerySingleGLInt64(functions, GL_MAX_ELEMENT_INDEX);
751 
752         // Work around the null driver limitations.
753         if (caps->maxElementIndex == 0)
754         {
755             caps->maxElementIndex = 0xFFFF;
756         }
757     }
758     else
759     {
760         // Doesn't affect ES3 support, can use a pre-defined limit
761         caps->maxElementIndex = static_cast<GLint64>(std::numeric_limits<unsigned int>::max());
762     }
763 
764     if (features.limitWebglMaxTextureSizeTo4096.enabled)
765     {
766         limitations->webGLTextureSizeLimit = 4096;
767     }
768     else if (features.limitWebglMaxTextureSizeTo8192.enabled)
769     {
770         limitations->webGLTextureSizeLimit = 8192;
771     }
772 
773     GLint max3dArrayTextureSizeLimit = std::numeric_limits<GLint>::max();
774     if (features.limitMax3dArrayTextureSizeTo1024.enabled)
775     {
776         max3dArrayTextureSizeLimit = 1024;
777     }
778 
779     if (functions->isAtLeastGL(gl::Version(1, 2)) || functions->isAtLeastGLES(gl::Version(3, 0)) ||
780         functions->hasGLESExtension("GL_OES_texture_3D"))
781     {
782         caps->max3DTextureSize = std::min(
783             {QuerySingleGLInt(functions, GL_MAX_3D_TEXTURE_SIZE), max3dArrayTextureSizeLimit});
784     }
785     else
786     {
787         // Can't support ES3 without 3D textures
788         LimitVersion(maxSupportedESVersion, gl::Version(2, 0));
789     }
790 
791     caps->max2DTextureSize = QuerySingleGLInt(functions, GL_MAX_TEXTURE_SIZE);  // GL 1.0 / ES 2.0
792     caps->maxCubeMapTextureSize =
793         QuerySingleGLInt(functions, GL_MAX_CUBE_MAP_TEXTURE_SIZE);  // GL 1.3 / ES 2.0
794 
795     if (functions->isAtLeastGL(gl::Version(3, 0)) ||
796         functions->hasGLExtension("GL_EXT_texture_array") ||
797         functions->isAtLeastGLES(gl::Version(3, 0)))
798     {
799         caps->maxArrayTextureLayers = std::min(
800             {QuerySingleGLInt(functions, GL_MAX_ARRAY_TEXTURE_LAYERS), max3dArrayTextureSizeLimit});
801     }
802     else
803     {
804         // Can't support ES3 without array textures
805         LimitVersion(maxSupportedESVersion, gl::Version(2, 0));
806     }
807 
808     if (functions->isAtLeastGL(gl::Version(1, 5)) ||
809         functions->hasGLExtension("GL_EXT_texture_lod_bias") ||
810         functions->isAtLeastGLES(gl::Version(3, 0)))
811     {
812         caps->maxLODBias = QuerySingleGLFloat(functions, GL_MAX_TEXTURE_LOD_BIAS);
813     }
814     else
815     {
816         LimitVersion(maxSupportedESVersion, gl::Version(2, 0));
817     }
818 
819     if (functions->isAtLeastGL(gl::Version(3, 0)) ||
820         functions->hasGLExtension("GL_EXT_framebuffer_object") ||
821         functions->isAtLeastGLES(gl::Version(3, 0)))
822     {
823         caps->maxRenderbufferSize = QuerySingleGLInt(functions, GL_MAX_RENDERBUFFER_SIZE);
824         caps->maxColorAttachments = QuerySingleGLInt(functions, GL_MAX_COLOR_ATTACHMENTS);
825     }
826     else if (functions->isAtLeastGLES(gl::Version(2, 0)))
827     {
828         caps->maxRenderbufferSize = QuerySingleGLInt(functions, GL_MAX_RENDERBUFFER_SIZE);
829         caps->maxColorAttachments = 1;
830     }
831     else
832     {
833         // Can't support ES2 without framebuffers and renderbuffers
834         LimitVersion(maxSupportedESVersion, gl::Version(0, 0));
835     }
836 
837     if (functions->isAtLeastGL(gl::Version(2, 0)) ||
838         functions->hasGLExtension("ARB_draw_buffers") ||
839         functions->isAtLeastGLES(gl::Version(3, 0)) ||
840         functions->hasGLESExtension("GL_EXT_draw_buffers"))
841     {
842         caps->maxDrawBuffers = QuerySingleGLInt(functions, GL_MAX_DRAW_BUFFERS);
843     }
844     else
845     {
846         // Framebuffer is required to have at least one drawbuffer even if the extension is not
847         // supported
848         caps->maxDrawBuffers = 1;
849         LimitVersion(maxSupportedESVersion, gl::Version(2, 0));
850     }
851 
852     caps->maxViewportWidth =
853         QueryGLIntRange(functions, GL_MAX_VIEWPORT_DIMS, 0);  // GL 1.0 / ES 2.0
854     caps->maxViewportHeight =
855         QueryGLIntRange(functions, GL_MAX_VIEWPORT_DIMS, 1);  // GL 1.0 / ES 2.0
856 
857     if (functions->standard == STANDARD_GL_DESKTOP &&
858         (functions->profile & GL_CONTEXT_CORE_PROFILE_BIT) != 0)
859     {
860         // Desktop GL core profile deprecated the GL_ALIASED_POINT_SIZE_RANGE query.  Use
861         // GL_POINT_SIZE_RANGE instead.
862         caps->minAliasedPointSize =
863             std::max(1.0f, QueryGLFloatRange(functions, GL_POINT_SIZE_RANGE, 0));
864         caps->maxAliasedPointSize = QueryGLFloatRange(functions, GL_POINT_SIZE_RANGE, 1);
865     }
866     else
867     {
868         caps->minAliasedPointSize =
869             std::max(1.0f, QueryGLFloatRange(functions, GL_ALIASED_POINT_SIZE_RANGE, 0));
870         caps->maxAliasedPointSize = QueryGLFloatRange(functions, GL_ALIASED_POINT_SIZE_RANGE, 1);
871     }
872 
873     caps->minAliasedLineWidth =
874         QueryGLFloatRange(functions, GL_ALIASED_LINE_WIDTH_RANGE, 0);  // GL 1.2 / ES 2.0
875     caps->maxAliasedLineWidth =
876         QueryGLFloatRange(functions, GL_ALIASED_LINE_WIDTH_RANGE, 1);  // GL 1.2 / ES 2.0
877 
878     // Table 6.29, implementation dependent values (cont.)
879     if (functions->isAtLeastGL(gl::Version(1, 2)) || functions->isAtLeastGLES(gl::Version(3, 0)))
880     {
881         caps->maxElementsIndices  = QuerySingleGLInt(functions, GL_MAX_ELEMENTS_INDICES);
882         caps->maxElementsVertices = QuerySingleGLInt(functions, GL_MAX_ELEMENTS_VERTICES);
883     }
884     else
885     {
886         // Doesn't impact supported version
887     }
888 
889     if (functions->isAtLeastGL(gl::Version(4, 1)) ||
890         functions->hasGLExtension("GL_ARB_get_program_binary") ||
891         functions->isAtLeastGLES(gl::Version(3, 0)) ||
892         functions->hasGLESExtension("GL_OES_get_program_binary"))
893     {
894         // Able to support the GL_PROGRAM_BINARY_ANGLE format as long as another program binary
895         // format is available.
896         GLint numBinaryFormats = QuerySingleGLInt(functions, GL_NUM_PROGRAM_BINARY_FORMATS_OES);
897         if (numBinaryFormats > 0)
898         {
899             caps->programBinaryFormats.push_back(GL_PROGRAM_BINARY_ANGLE);
900         }
901     }
902     else
903     {
904         // Doesn't impact supported version
905     }
906 
907     // glGetShaderPrecisionFormat is not available until desktop GL version 4.1 or
908     // GL_ARB_ES2_compatibility exists
909     if (functions->isAtLeastGL(gl::Version(4, 1)) ||
910         functions->hasGLExtension("GL_ARB_ES2_compatibility") ||
911         functions->isAtLeastGLES(gl::Version(2, 0)))
912     {
913         caps->vertexHighpFloat   = QueryTypePrecision(functions, GL_VERTEX_SHADER, GL_HIGH_FLOAT);
914         caps->vertexMediumpFloat = QueryTypePrecision(functions, GL_VERTEX_SHADER, GL_MEDIUM_FLOAT);
915         caps->vertexLowpFloat    = QueryTypePrecision(functions, GL_VERTEX_SHADER, GL_LOW_FLOAT);
916         caps->fragmentHighpFloat = QueryTypePrecision(functions, GL_FRAGMENT_SHADER, GL_HIGH_FLOAT);
917         caps->fragmentMediumpFloat =
918             QueryTypePrecision(functions, GL_FRAGMENT_SHADER, GL_MEDIUM_FLOAT);
919         caps->fragmentLowpFloat  = QueryTypePrecision(functions, GL_FRAGMENT_SHADER, GL_LOW_FLOAT);
920         caps->vertexHighpInt     = QueryTypePrecision(functions, GL_VERTEX_SHADER, GL_HIGH_INT);
921         caps->vertexMediumpInt   = QueryTypePrecision(functions, GL_VERTEX_SHADER, GL_MEDIUM_INT);
922         caps->vertexLowpInt      = QueryTypePrecision(functions, GL_VERTEX_SHADER, GL_LOW_INT);
923         caps->fragmentHighpInt   = QueryTypePrecision(functions, GL_FRAGMENT_SHADER, GL_HIGH_INT);
924         caps->fragmentMediumpInt = QueryTypePrecision(functions, GL_FRAGMENT_SHADER, GL_MEDIUM_INT);
925         caps->fragmentLowpInt    = QueryTypePrecision(functions, GL_FRAGMENT_SHADER, GL_LOW_INT);
926     }
927     else
928     {
929         // Doesn't impact supported version, set some default values
930         caps->vertexHighpFloat.setIEEEFloat();
931         caps->vertexMediumpFloat.setIEEEFloat();
932         caps->vertexLowpFloat.setIEEEFloat();
933         caps->fragmentHighpFloat.setIEEEFloat();
934         caps->fragmentMediumpFloat.setIEEEFloat();
935         caps->fragmentLowpFloat.setIEEEFloat();
936         caps->vertexHighpInt.setTwosComplementInt(32);
937         caps->vertexMediumpInt.setTwosComplementInt(32);
938         caps->vertexLowpInt.setTwosComplementInt(32);
939         caps->fragmentHighpInt.setTwosComplementInt(32);
940         caps->fragmentMediumpInt.setTwosComplementInt(32);
941         caps->fragmentLowpInt.setTwosComplementInt(32);
942     }
943 
944     if (functions->isAtLeastGL(gl::Version(3, 2)) || functions->hasGLExtension("GL_ARB_sync") ||
945         functions->isAtLeastGLES(gl::Version(3, 0)))
946     {
947         // Work around Linux NVIDIA driver bug where GL_TIMEOUT_IGNORED is returned.
948         caps->maxServerWaitTimeout =
949             std::max<GLint64>(QuerySingleGLInt64(functions, GL_MAX_SERVER_WAIT_TIMEOUT), 0);
950     }
951     else
952     {
953         LimitVersion(maxSupportedESVersion, gl::Version(2, 0));
954     }
955 
956     // Table 6.31, implementation dependent vertex shader limits
957     if (functions->isAtLeastGL(gl::Version(2, 0)) || functions->isAtLeastGLES(gl::Version(2, 0)))
958     {
959         caps->maxVertexAttributes = QuerySingleGLInt(functions, GL_MAX_VERTEX_ATTRIBS);
960         caps->maxShaderUniformComponents[gl::ShaderType::Vertex] =
961             QuerySingleGLInt(functions, GL_MAX_VERTEX_UNIFORM_COMPONENTS);
962         caps->maxShaderTextureImageUnits[gl::ShaderType::Vertex] =
963             QuerySingleGLInt(functions, GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS);
964     }
965     else
966     {
967         // Can't support ES2 version without these caps
968         LimitVersion(maxSupportedESVersion, gl::Version(0, 0));
969     }
970 
971     if (functions->isAtLeastGL(gl::Version(4, 1)) ||
972         functions->hasGLExtension("GL_ARB_ES2_compatibility") ||
973         functions->isAtLeastGLES(gl::Version(2, 0)))
974     {
975         caps->maxVertexUniformVectors = QuerySingleGLInt(functions, GL_MAX_VERTEX_UNIFORM_VECTORS);
976         caps->maxFragmentUniformVectors =
977             QuerySingleGLInt(functions, GL_MAX_FRAGMENT_UNIFORM_VECTORS);
978     }
979     else
980     {
981         // Doesn't limit ES version, GL_MAX_VERTEX_UNIFORM_COMPONENTS / 4 is acceptable.
982         caps->maxVertexUniformVectors =
983             caps->maxShaderUniformComponents[gl::ShaderType::Vertex] / 4;
984         // Doesn't limit ES version, GL_MAX_FRAGMENT_UNIFORM_COMPONENTS / 4 is acceptable.
985         caps->maxFragmentUniformVectors =
986             caps->maxShaderUniformComponents[gl::ShaderType::Fragment] / 4;
987     }
988 
989     if (functions->isAtLeastGL(gl::Version(3, 2)) || functions->isAtLeastGLES(gl::Version(3, 0)))
990     {
991         caps->maxVertexOutputComponents =
992             QuerySingleGLInt(functions, GL_MAX_VERTEX_OUTPUT_COMPONENTS);
993     }
994     else
995     {
996         // There doesn't seem, to be a desktop extension to add this cap, maybe it could be given a
997         // safe limit instead of limiting the supported ES version.
998         LimitVersion(maxSupportedESVersion, gl::Version(2, 0));
999     }
1000 
1001     // Table 6.32, implementation dependent fragment shader limits
1002     if (functions->isAtLeastGL(gl::Version(2, 0)) || functions->isAtLeastGLES(gl::Version(2, 0)))
1003     {
1004         caps->maxShaderUniformComponents[gl::ShaderType::Fragment] =
1005             QuerySingleGLInt(functions, GL_MAX_FRAGMENT_UNIFORM_COMPONENTS);
1006         caps->maxShaderTextureImageUnits[gl::ShaderType::Fragment] =
1007             QuerySingleGLInt(functions, GL_MAX_TEXTURE_IMAGE_UNITS);
1008     }
1009     else
1010     {
1011         // Can't support ES2 version without these caps
1012         LimitVersion(maxSupportedESVersion, gl::Version(0, 0));
1013     }
1014 
1015     if (functions->isAtLeastGL(gl::Version(3, 2)) || functions->isAtLeastGLES(gl::Version(3, 0)))
1016     {
1017         caps->maxFragmentInputComponents =
1018             QuerySingleGLInt(functions, GL_MAX_FRAGMENT_INPUT_COMPONENTS);
1019     }
1020     else
1021     {
1022         // There doesn't seem, to be a desktop extension to add this cap, maybe it could be given a
1023         // safe limit instead of limiting the supported ES version.
1024         LimitVersion(maxSupportedESVersion, gl::Version(2, 0));
1025     }
1026 
1027     if (functions->isAtLeastGL(gl::Version(3, 0)) || functions->isAtLeastGLES(gl::Version(3, 0)))
1028     {
1029         caps->minProgramTexelOffset = QuerySingleGLInt(functions, GL_MIN_PROGRAM_TEXEL_OFFSET);
1030         caps->maxProgramTexelOffset = QuerySingleGLInt(functions, GL_MAX_PROGRAM_TEXEL_OFFSET);
1031     }
1032     else
1033     {
1034         // Can't support ES3 without texel offset, could possibly be emulated in the shader
1035         LimitVersion(maxSupportedESVersion, gl::Version(2, 0));
1036     }
1037 
1038     // Table 6.33, implementation dependent aggregate shader limits
1039     if (functions->isAtLeastGL(gl::Version(3, 1)) ||
1040         functions->hasGLExtension("GL_ARB_uniform_buffer_object") ||
1041         functions->isAtLeastGLES(gl::Version(3, 0)))
1042     {
1043         caps->maxShaderUniformBlocks[gl::ShaderType::Vertex] =
1044             QuerySingleGLInt(functions, GL_MAX_VERTEX_UNIFORM_BLOCKS);
1045         caps->maxShaderUniformBlocks[gl::ShaderType::Fragment] =
1046             QuerySingleGLInt(functions, GL_MAX_FRAGMENT_UNIFORM_BLOCKS);
1047         caps->maxUniformBufferBindings =
1048             QuerySingleGLInt(functions, GL_MAX_UNIFORM_BUFFER_BINDINGS);
1049         caps->maxUniformBlockSize = QuerySingleGLInt64(functions, GL_MAX_UNIFORM_BLOCK_SIZE);
1050         caps->uniformBufferOffsetAlignment =
1051             QuerySingleGLInt(functions, GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT);
1052         caps->maxCombinedUniformBlocks =
1053             QuerySingleGLInt(functions, GL_MAX_COMBINED_UNIFORM_BLOCKS);
1054         caps->maxCombinedShaderUniformComponents[gl::ShaderType::Vertex] =
1055             QuerySingleGLInt64(functions, GL_MAX_COMBINED_VERTEX_UNIFORM_COMPONENTS);
1056         caps->maxCombinedShaderUniformComponents[gl::ShaderType::Fragment] =
1057             QuerySingleGLInt64(functions, GL_MAX_COMBINED_FRAGMENT_UNIFORM_COMPONENTS);
1058 
1059         // Clamp the maxUniformBlockSize to 64KB (majority of devices support up to this size
1060         // currently), some drivers expose an excessively large value.
1061         caps->maxUniformBlockSize = std::min<GLint64>(0x10000, caps->maxUniformBlockSize);
1062     }
1063     else
1064     {
1065         // Can't support ES3 without uniform blocks
1066         LimitVersion(maxSupportedESVersion, gl::Version(2, 0));
1067     }
1068 
1069     if (functions->isAtLeastGL(gl::Version(3, 2)) &&
1070         (functions->profile & GL_CONTEXT_CORE_PROFILE_BIT) != 0)
1071     {
1072         caps->maxVaryingComponents = QuerySingleGLInt(functions, GL_MAX_VERTEX_OUTPUT_COMPONENTS);
1073     }
1074     else if (functions->isAtLeastGL(gl::Version(3, 0)) ||
1075              functions->hasGLExtension("GL_ARB_ES2_compatibility") ||
1076              functions->isAtLeastGLES(gl::Version(2, 0)))
1077     {
1078         caps->maxVaryingComponents = QuerySingleGLInt(functions, GL_MAX_VARYING_COMPONENTS);
1079     }
1080     else if (functions->isAtLeastGL(gl::Version(2, 0)))
1081     {
1082         caps->maxVaryingComponents = QuerySingleGLInt(functions, GL_MAX_VARYING_FLOATS);
1083         LimitVersion(maxSupportedESVersion, gl::Version(2, 0));
1084     }
1085     else
1086     {
1087         LimitVersion(maxSupportedESVersion, gl::Version(0, 0));
1088     }
1089 
1090     if (functions->isAtLeastGL(gl::Version(4, 1)) ||
1091         functions->hasGLExtension("GL_ARB_ES2_compatibility") ||
1092         functions->isAtLeastGLES(gl::Version(2, 0)))
1093     {
1094         caps->maxVaryingVectors = QuerySingleGLInt(functions, GL_MAX_VARYING_VECTORS);
1095     }
1096     else
1097     {
1098         // Doesn't limit ES version, GL_MAX_VARYING_COMPONENTS / 4 is acceptable.
1099         caps->maxVaryingVectors = caps->maxVaryingComponents / 4;
1100     }
1101 
1102     // Determine the max combined texture image units by adding the vertex and fragment limits.  If
1103     // the real cap is queried, it would contain the limits for shader types that are not available
1104     // to ES.
1105     caps->maxCombinedTextureImageUnits =
1106         QuerySingleGLInt(functions, GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS);
1107 
1108     // Table 6.34, implementation dependent transform feedback limits
1109     if (functions->isAtLeastGL(gl::Version(4, 0)) ||
1110         functions->hasGLExtension("GL_ARB_transform_feedback2") ||
1111         functions->isAtLeastGLES(gl::Version(3, 0)))
1112     {
1113         caps->maxTransformFeedbackInterleavedComponents =
1114             QuerySingleGLInt(functions, GL_MAX_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS);
1115         caps->maxTransformFeedbackSeparateAttributes =
1116             QuerySingleGLInt(functions, GL_MAX_TRANSFORM_FEEDBACK_SEPARATE_ATTRIBS);
1117         caps->maxTransformFeedbackSeparateComponents =
1118             QuerySingleGLInt(functions, GL_MAX_TRANSFORM_FEEDBACK_SEPARATE_COMPONENTS);
1119     }
1120     else
1121     {
1122         // Can't support ES3 without transform feedback
1123         LimitVersion(maxSupportedESVersion, gl::Version(2, 0));
1124     }
1125 
1126     GLint sampleCountLimit = std::numeric_limits<GLint>::max();
1127     if (features.limitMaxMSAASamplesTo4.enabled)
1128     {
1129         sampleCountLimit = 4;
1130     }
1131 
1132     // Table 6.35, Framebuffer Dependent Values
1133     if (functions->isAtLeastGL(gl::Version(3, 0)) ||
1134         functions->hasGLExtension("GL_EXT_framebuffer_multisample") ||
1135         functions->isAtLeastGLES(gl::Version(3, 0)) ||
1136         functions->hasGLESExtension("GL_EXT_multisampled_render_to_texture"))
1137     {
1138         caps->maxSamples = std::min(QuerySingleGLInt(functions, GL_MAX_SAMPLES), sampleCountLimit);
1139     }
1140     else
1141     {
1142         LimitVersion(maxSupportedESVersion, gl::Version(2, 0));
1143     }
1144 
1145     // Non-constant sampler array indexing is required for OpenGL ES 2 and OpenGL ES after 3.2.
1146     // However having it available on OpenGL ES 2 is a specification bug, and using this
1147     // indexing in WebGL is undefined. Requiring this feature would break WebGL 1 for some users
1148     // so we don't check for it. (it is present with ESSL 100, ESSL >= 320, GLSL >= 400 and
1149     // GL_ARB_gpu_shader5)
1150 
1151     // Check if sampler objects are supported
1152     if (!functions->isAtLeastGL(gl::Version(3, 3)) &&
1153         !functions->hasGLExtension("GL_ARB_sampler_objects") &&
1154         !functions->isAtLeastGLES(gl::Version(3, 0)))
1155     {
1156         // Can't support ES3 without sampler objects
1157         LimitVersion(maxSupportedESVersion, gl::Version(2, 0));
1158     }
1159 
1160     // Can't support ES3 without texture swizzling
1161     if (!functions->isAtLeastGL(gl::Version(3, 3)) &&
1162         !functions->hasGLExtension("GL_ARB_texture_swizzle") &&
1163         !functions->hasGLExtension("GL_EXT_texture_swizzle") &&
1164         !functions->isAtLeastGLES(gl::Version(3, 0)))
1165     {
1166         LimitVersion(maxSupportedESVersion, gl::Version(2, 0));
1167 
1168         // Texture swizzling is required to work around the luminance texture format not being
1169         // present in the core profile
1170         if (functions->profile & GL_CONTEXT_CORE_PROFILE_BIT)
1171         {
1172             LimitVersion(maxSupportedESVersion, gl::Version(0, 0));
1173         }
1174     }
1175 
1176     // Can't support ES3 without the GLSL packing builtins. We have a workaround for all
1177     // desktop OpenGL versions starting from 3.3 with the bit packing extension.
1178     if (!functions->isAtLeastGL(gl::Version(4, 2)) &&
1179         !(functions->isAtLeastGL(gl::Version(3, 2)) &&
1180           functions->hasGLExtension("GL_ARB_shader_bit_encoding")) &&
1181         !functions->hasGLExtension("GL_ARB_shading_language_packing") &&
1182         !functions->isAtLeastGLES(gl::Version(3, 0)))
1183     {
1184         LimitVersion(maxSupportedESVersion, gl::Version(2, 0));
1185     }
1186 
1187     // ES3 needs to support explicit layout location qualifiers, while it might be possible to
1188     // fake them in our side, we currently don't support that.
1189     if (!functions->isAtLeastGL(gl::Version(3, 3)) &&
1190         !functions->hasGLExtension("GL_ARB_explicit_attrib_location") &&
1191         !functions->isAtLeastGLES(gl::Version(3, 0)))
1192     {
1193         LimitVersion(maxSupportedESVersion, gl::Version(2, 0));
1194     }
1195 
1196     if (!functions->isAtLeastGL(gl::Version(4, 3)) &&
1197         !functions->hasGLExtension("GL_ARB_stencil_texturing") &&
1198         !functions->isAtLeastGLES(gl::Version(3, 1)))
1199     {
1200         LimitVersion(maxSupportedESVersion, gl::Version(3, 0));
1201     }
1202 
1203     if (functions->isAtLeastGL(gl::Version(4, 3)) || functions->isAtLeastGLES(gl::Version(3, 1)) ||
1204         functions->hasGLExtension("GL_ARB_framebuffer_no_attachments"))
1205     {
1206         caps->maxFramebufferWidth  = QuerySingleGLInt(functions, GL_MAX_FRAMEBUFFER_WIDTH);
1207         caps->maxFramebufferHeight = QuerySingleGLInt(functions, GL_MAX_FRAMEBUFFER_HEIGHT);
1208         caps->maxFramebufferSamples =
1209             std::min(QuerySingleGLInt(functions, GL_MAX_FRAMEBUFFER_SAMPLES), sampleCountLimit);
1210     }
1211     else
1212     {
1213         LimitVersion(maxSupportedESVersion, gl::Version(3, 0));
1214     }
1215 
1216     if (functions->isAtLeastGL(gl::Version(3, 2)) || functions->isAtLeastGLES(gl::Version(3, 1)) ||
1217         functions->hasGLExtension("GL_ARB_texture_multisample"))
1218     {
1219         caps->maxSampleMaskWords = QuerySingleGLInt(functions, GL_MAX_SAMPLE_MASK_WORDS);
1220         caps->maxColorTextureSamples =
1221             std::min(QuerySingleGLInt(functions, GL_MAX_COLOR_TEXTURE_SAMPLES), sampleCountLimit);
1222         caps->maxDepthTextureSamples =
1223             std::min(QuerySingleGLInt(functions, GL_MAX_DEPTH_TEXTURE_SAMPLES), sampleCountLimit);
1224         caps->maxIntegerSamples =
1225             std::min(QuerySingleGLInt(functions, GL_MAX_INTEGER_SAMPLES), sampleCountLimit);
1226     }
1227     else
1228     {
1229         LimitVersion(maxSupportedESVersion, gl::Version(3, 0));
1230     }
1231 
1232     if (functions->isAtLeastGL(gl::Version(4, 3)) || functions->isAtLeastGLES(gl::Version(3, 1)) ||
1233         functions->hasGLExtension("GL_ARB_vertex_attrib_binding"))
1234     {
1235         caps->maxVertexAttribRelativeOffset =
1236             QuerySingleGLInt(functions, GL_MAX_VERTEX_ATTRIB_RELATIVE_OFFSET);
1237         caps->maxVertexAttribBindings = QuerySingleGLInt(functions, GL_MAX_VERTEX_ATTRIB_BINDINGS);
1238 
1239         // OpenGL 4.3 has no limit on maximum value of stride.
1240         // [OpenGL 4.3 (Core Profile) - February 14, 2013] Chapter 10.3.1 Page 298
1241         if (features.emulateMaxVertexAttribStride.enabled ||
1242             (functions->standard == STANDARD_GL_DESKTOP && functions->version == gl::Version(4, 3)))
1243         {
1244             caps->maxVertexAttribStride = 2048;
1245         }
1246         else
1247         {
1248             caps->maxVertexAttribStride = QuerySingleGLInt(functions, GL_MAX_VERTEX_ATTRIB_STRIDE);
1249         }
1250     }
1251     else
1252     {
1253         LimitVersion(maxSupportedESVersion, gl::Version(3, 0));
1254         // Set maxVertexAttribBindings anyway, a number of places assume this value is at least as
1255         // much as maxVertexAttributes.
1256         caps->maxVertexAttribBindings = caps->maxVertexAttributes;
1257     }
1258 
1259     if (functions->isAtLeastGL(gl::Version(4, 3)) || functions->isAtLeastGLES(gl::Version(3, 1)) ||
1260         functions->hasGLExtension("GL_ARB_shader_storage_buffer_object"))
1261     {
1262         caps->maxCombinedShaderOutputResources =
1263             QuerySingleGLInt(functions, GL_MAX_COMBINED_SHADER_OUTPUT_RESOURCES);
1264         caps->maxShaderStorageBlocks[gl::ShaderType::Fragment] =
1265             QuerySingleGLInt(functions, GL_MAX_FRAGMENT_SHADER_STORAGE_BLOCKS);
1266         caps->maxShaderStorageBlocks[gl::ShaderType::Vertex] =
1267             QuerySingleGLInt(functions, GL_MAX_VERTEX_SHADER_STORAGE_BLOCKS);
1268         caps->maxShaderStorageBufferBindings =
1269             QuerySingleGLInt(functions, GL_MAX_SHADER_STORAGE_BUFFER_BINDINGS);
1270         caps->maxShaderStorageBlockSize =
1271             QuerySingleGLInt64(functions, GL_MAX_SHADER_STORAGE_BLOCK_SIZE);
1272         caps->maxCombinedShaderStorageBlocks =
1273             QuerySingleGLInt(functions, GL_MAX_COMBINED_SHADER_STORAGE_BLOCKS);
1274         caps->shaderStorageBufferOffsetAlignment =
1275             QuerySingleGLInt(functions, GL_SHADER_STORAGE_BUFFER_OFFSET_ALIGNMENT);
1276     }
1277     else
1278     {
1279         LimitVersion(maxSupportedESVersion, gl::Version(3, 0));
1280     }
1281 
1282     if (nativegl::SupportsCompute(functions))
1283     {
1284         for (GLuint index = 0u; index < 3u; ++index)
1285         {
1286             caps->maxComputeWorkGroupCount[index] =
1287                 QuerySingleIndexGLInt(functions, GL_MAX_COMPUTE_WORK_GROUP_COUNT, index);
1288 
1289             caps->maxComputeWorkGroupSize[index] =
1290                 QuerySingleIndexGLInt(functions, GL_MAX_COMPUTE_WORK_GROUP_SIZE, index);
1291         }
1292         caps->maxComputeWorkGroupInvocations =
1293             QuerySingleGLInt(functions, GL_MAX_COMPUTE_WORK_GROUP_INVOCATIONS);
1294         caps->maxShaderUniformBlocks[gl::ShaderType::Compute] =
1295             QuerySingleGLInt(functions, GL_MAX_COMPUTE_UNIFORM_BLOCKS);
1296         caps->maxShaderTextureImageUnits[gl::ShaderType::Compute] =
1297             QuerySingleGLInt(functions, GL_MAX_COMPUTE_TEXTURE_IMAGE_UNITS);
1298         caps->maxComputeSharedMemorySize =
1299             QuerySingleGLInt(functions, GL_MAX_COMPUTE_SHARED_MEMORY_SIZE);
1300         caps->maxShaderUniformComponents[gl::ShaderType::Compute] =
1301             QuerySingleGLInt(functions, GL_MAX_COMPUTE_UNIFORM_COMPONENTS);
1302         caps->maxShaderAtomicCounterBuffers[gl::ShaderType::Compute] =
1303             QuerySingleGLInt(functions, GL_MAX_COMPUTE_ATOMIC_COUNTER_BUFFERS);
1304         caps->maxShaderAtomicCounters[gl::ShaderType::Compute] =
1305             QuerySingleGLInt(functions, GL_MAX_COMPUTE_ATOMIC_COUNTERS);
1306         caps->maxShaderImageUniforms[gl::ShaderType::Compute] =
1307             QuerySingleGLInt(functions, GL_MAX_COMPUTE_IMAGE_UNIFORMS);
1308         caps->maxCombinedShaderUniformComponents[gl::ShaderType::Compute] =
1309             QuerySingleGLInt(functions, GL_MAX_COMBINED_COMPUTE_UNIFORM_COMPONENTS);
1310         caps->maxShaderStorageBlocks[gl::ShaderType::Compute] =
1311             QuerySingleGLInt(functions, GL_MAX_COMPUTE_SHADER_STORAGE_BLOCKS);
1312     }
1313     else
1314     {
1315         LimitVersion(maxSupportedESVersion, gl::Version(3, 0));
1316     }
1317 
1318     if (functions->isAtLeastGL(gl::Version(4, 3)) || functions->isAtLeastGLES(gl::Version(3, 1)) ||
1319         functions->hasGLExtension("GL_ARB_explicit_uniform_location"))
1320     {
1321         caps->maxUniformLocations = QuerySingleGLInt(functions, GL_MAX_UNIFORM_LOCATIONS);
1322     }
1323     else
1324     {
1325         LimitVersion(maxSupportedESVersion, gl::Version(3, 0));
1326     }
1327 
1328     if (functions->isAtLeastGL(gl::Version(4, 0)) || functions->isAtLeastGLES(gl::Version(3, 1)) ||
1329         functions->hasGLExtension("GL_ARB_texture_gather"))
1330     {
1331         caps->minProgramTextureGatherOffset =
1332             QuerySingleGLInt(functions, GL_MIN_PROGRAM_TEXTURE_GATHER_OFFSET);
1333         caps->maxProgramTextureGatherOffset =
1334             QuerySingleGLInt(functions, GL_MAX_PROGRAM_TEXTURE_GATHER_OFFSET);
1335     }
1336     else
1337     {
1338         LimitVersion(maxSupportedESVersion, gl::Version(3, 0));
1339     }
1340 
1341     if (functions->isAtLeastGL(gl::Version(4, 2)) || functions->isAtLeastGLES(gl::Version(3, 1)) ||
1342         functions->hasGLExtension("GL_ARB_shader_image_load_store"))
1343     {
1344         caps->maxShaderImageUniforms[gl::ShaderType::Vertex] =
1345             QuerySingleGLInt(functions, GL_MAX_VERTEX_IMAGE_UNIFORMS);
1346         caps->maxShaderImageUniforms[gl::ShaderType::Fragment] =
1347             QuerySingleGLInt(functions, GL_MAX_FRAGMENT_IMAGE_UNIFORMS);
1348         caps->maxImageUnits = QuerySingleGLInt(functions, GL_MAX_IMAGE_UNITS);
1349         caps->maxCombinedImageUniforms =
1350             QuerySingleGLInt(functions, GL_MAX_COMBINED_IMAGE_UNIFORMS);
1351     }
1352     else
1353     {
1354         LimitVersion(maxSupportedESVersion, gl::Version(3, 0));
1355     }
1356 
1357     if (functions->isAtLeastGL(gl::Version(4, 2)) || functions->isAtLeastGLES(gl::Version(3, 1)) ||
1358         functions->hasGLExtension("GL_ARB_shader_atomic_counters"))
1359     {
1360         caps->maxShaderAtomicCounterBuffers[gl::ShaderType::Vertex] =
1361             QuerySingleGLInt(functions, GL_MAX_VERTEX_ATOMIC_COUNTER_BUFFERS);
1362         caps->maxShaderAtomicCounters[gl::ShaderType::Vertex] =
1363             QuerySingleGLInt(functions, GL_MAX_VERTEX_ATOMIC_COUNTERS);
1364         caps->maxShaderAtomicCounterBuffers[gl::ShaderType::Fragment] =
1365             QuerySingleGLInt(functions, GL_MAX_FRAGMENT_ATOMIC_COUNTER_BUFFERS);
1366         caps->maxShaderAtomicCounters[gl::ShaderType::Fragment] =
1367             QuerySingleGLInt(functions, GL_MAX_FRAGMENT_ATOMIC_COUNTERS);
1368         caps->maxAtomicCounterBufferBindings =
1369             QuerySingleGLInt(functions, GL_MAX_ATOMIC_COUNTER_BUFFER_BINDINGS);
1370         caps->maxAtomicCounterBufferSize =
1371             QuerySingleGLInt(functions, GL_MAX_ATOMIC_COUNTER_BUFFER_SIZE);
1372         caps->maxCombinedAtomicCounterBuffers =
1373             QuerySingleGLInt(functions, GL_MAX_COMBINED_ATOMIC_COUNTER_BUFFERS);
1374         caps->maxCombinedAtomicCounters =
1375             QuerySingleGLInt(functions, GL_MAX_COMBINED_ATOMIC_COUNTERS);
1376     }
1377     else
1378     {
1379         LimitVersion(maxSupportedESVersion, gl::Version(3, 0));
1380     }
1381 
1382     // TODO(geofflang): The gl-uniform-arrays WebGL conformance test struggles to complete on time
1383     // if the max uniform vectors is too large.  Artificially limit the maximum until the test is
1384     // updated.
1385     caps->maxVertexUniformVectors = std::min(1024, caps->maxVertexUniformVectors);
1386     caps->maxShaderUniformComponents[gl::ShaderType::Vertex] =
1387         std::min(caps->maxVertexUniformVectors * 4,
1388                  caps->maxShaderUniformComponents[gl::ShaderType::Vertex]);
1389     caps->maxFragmentUniformVectors = std::min(1024, caps->maxFragmentUniformVectors);
1390     caps->maxShaderUniformComponents[gl::ShaderType::Fragment] =
1391         std::min(caps->maxFragmentUniformVectors * 4,
1392                  caps->maxShaderUniformComponents[gl::ShaderType::Fragment]);
1393 
1394     // If it is not possible to support reading buffer data back, a shadow copy of the buffers must
1395     // be held. This disallows writing to buffers indirectly through transform feedback, thus
1396     // disallowing ES3.
1397     if (!CanMapBufferForRead(functions))
1398     {
1399         LimitVersion(maxSupportedESVersion, gl::Version(2, 0));
1400     }
1401 
1402     // GL_OES_texture_cube_map_array
1403     if (functions->isAtLeastGL(gl::Version(4, 0)) ||
1404         functions->hasGLESExtension("GL_OES_texture_cube_map_array") ||
1405         functions->hasGLESExtension("GL_EXT_texture_cube_map_array") ||
1406         functions->hasGLExtension("GL_ARB_texture_cube_map_array") ||
1407         functions->isAtLeastGLES(gl::Version(3, 2)))
1408     {
1409         extensions->textureCubeMapArrayOES = true;
1410         extensions->textureCubeMapArrayEXT = true;
1411     }
1412     else
1413     {
1414         // Can't support ES3.2 without cube map array textures
1415         LimitVersion(maxSupportedESVersion, gl::Version(3, 1));
1416     }
1417 
1418     if (!nativegl::SupportsVertexArrayObjects(functions) ||
1419         features.syncAllVertexArraysToDefault.enabled)
1420     {
1421         // ES 3.1 vertex bindings are not emulated on the default vertex array
1422         LimitVersion(maxSupportedESVersion, gl::Version(3, 0));
1423     }
1424 
1425     // Extension support
1426     extensions->setTextureExtensionSupport(*textureCapsMap);
1427 
1428     // Expose this extension only when we support the formats or we're running on top of a native
1429     // ES driver.
1430     extensions->textureCompressionAstcLdrKHR =
1431         extensions->textureCompressionAstcLdrKHR &&
1432         (features.allowAstcFormats.enabled || functions->standard == STANDARD_GL_ES);
1433     extensions->textureCompressionAstcHdrKHR =
1434         extensions->textureCompressionAstcLdrKHR &&
1435         functions->hasExtension("GL_KHR_texture_compression_astc_hdr");
1436     extensions->textureCompressionAstcSliced3dKHR =
1437         (extensions->textureCompressionAstcLdrKHR &&
1438          functions->hasExtension("GL_KHR_texture_compression_astc_sliced_3d")) ||
1439         extensions->textureCompressionAstcHdrKHR;
1440     extensions->elementIndexUintOES = functions->standard == STANDARD_GL_DESKTOP ||
1441                                       functions->isAtLeastGLES(gl::Version(3, 0)) ||
1442                                       functions->hasGLESExtension("GL_OES_element_index_uint");
1443     extensions->getProgramBinaryOES = caps->programBinaryFormats.size() > 0;
1444     extensions->readFormatBgraEXT   = functions->isAtLeastGL(gl::Version(1, 2)) ||
1445                                     functions->hasGLExtension("GL_EXT_bgra") ||
1446                                     functions->hasGLESExtension("GL_EXT_read_format_bgra");
1447     extensions->pixelBufferObjectNV = functions->isAtLeastGL(gl::Version(2, 1)) ||
1448                                       functions->isAtLeastGLES(gl::Version(3, 0)) ||
1449                                       functions->hasGLExtension("GL_ARB_pixel_buffer_object") ||
1450                                       functions->hasGLExtension("GL_EXT_pixel_buffer_object") ||
1451                                       functions->hasGLESExtension("GL_NV_pixel_buffer_object");
1452     extensions->syncARB      = nativegl::SupportsFenceSync(functions);
1453     extensions->mapbufferOES = functions->isAtLeastGL(gl::Version(1, 5)) ||
1454                                functions->isAtLeastGLES(gl::Version(3, 0)) ||
1455                                functions->hasGLESExtension("GL_OES_mapbuffer");
1456     extensions->mapBufferRangeEXT = functions->isAtLeastGL(gl::Version(3, 0)) ||
1457                                     functions->hasGLExtension("GL_ARB_map_buffer_range") ||
1458                                     functions->isAtLeastGLES(gl::Version(3, 0)) ||
1459                                     functions->hasGLESExtension("GL_EXT_map_buffer_range");
1460     extensions->textureNpotOES = functions->standard == STANDARD_GL_DESKTOP ||
1461                                  functions->isAtLeastGLES(gl::Version(3, 0)) ||
1462                                  functions->hasGLESExtension("GL_OES_texture_npot");
1463     // Note that we could emulate EXT_draw_buffers on ES 3.0's core functionality.
1464     extensions->drawBuffersEXT = functions->isAtLeastGL(gl::Version(2, 0)) ||
1465                                  functions->hasGLExtension("ARB_draw_buffers") ||
1466                                  functions->hasGLESExtension("GL_EXT_draw_buffers");
1467     extensions->drawBuffersIndexedEXT =
1468         !features.disableDrawBuffersIndexed.enabled &&
1469         (functions->isAtLeastGL(gl::Version(4, 0)) ||
1470          (functions->hasGLExtension("GL_EXT_draw_buffers2") &&
1471           functions->hasGLExtension("GL_ARB_draw_buffers_blend")) ||
1472          functions->isAtLeastGLES(gl::Version(3, 2)) ||
1473          functions->hasGLESExtension("GL_OES_draw_buffers_indexed") ||
1474          functions->hasGLESExtension("GL_EXT_draw_buffers_indexed"));
1475     extensions->drawBuffersIndexedOES = extensions->drawBuffersIndexedEXT;
1476     extensions->textureStorageEXT     = functions->standard == STANDARD_GL_DESKTOP ||
1477                                     functions->hasGLESExtension("GL_EXT_texture_storage");
1478     extensions->textureFilterAnisotropicEXT =
1479         functions->hasGLExtension("GL_EXT_texture_filter_anisotropic") ||
1480         functions->hasGLESExtension("GL_EXT_texture_filter_anisotropic");
1481     extensions->occlusionQueryBooleanEXT = nativegl::SupportsOcclusionQueries(functions);
1482     caps->maxTextureAnisotropy =
1483         extensions->textureFilterAnisotropicEXT
1484             ? QuerySingleGLFloat(functions, GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT)
1485             : 0.0f;
1486     extensions->fenceNV = FenceNVGL::Supported(functions) || FenceNVSyncGL::Supported(functions);
1487     extensions->blendMinmaxEXT = functions->isAtLeastGL(gl::Version(1, 5)) ||
1488                                  functions->hasGLExtension("GL_EXT_blend_minmax") ||
1489                                  functions->isAtLeastGLES(gl::Version(3, 0)) ||
1490                                  functions->hasGLESExtension("GL_EXT_blend_minmax");
1491     extensions->framebufferBlitNV = functions->isAtLeastGL(gl::Version(3, 0)) ||
1492                                     functions->hasGLExtension("GL_EXT_framebuffer_blit") ||
1493                                     functions->isAtLeastGLES(gl::Version(3, 0)) ||
1494                                     functions->hasGLESExtension("GL_NV_framebuffer_blit");
1495     extensions->framebufferBlitANGLE =
1496         extensions->framebufferBlitNV || functions->hasGLESExtension("GL_ANGLE_framebuffer_blit");
1497     extensions->framebufferMultisampleANGLE =
1498         extensions->framebufferBlitANGLE && caps->maxSamples > 0;
1499     extensions->multisampledRenderToTextureEXT =
1500         !features.disableMultisampledRenderToTexture.enabled &&
1501         (functions->hasGLESExtension("GL_EXT_multisampled_render_to_texture") ||
1502          functions->hasGLESExtension("GL_IMG_multisampled_render_to_texture"));
1503     extensions->multisampledRenderToTexture2EXT =
1504         !features.disableMultisampledRenderToTexture.enabled &&
1505         extensions->multisampledRenderToTextureEXT &&
1506         functions->hasGLESExtension("GL_EXT_multisampled_render_to_texture2");
1507     extensions->standardDerivativesOES = functions->isAtLeastGL(gl::Version(2, 0)) ||
1508                                          functions->hasGLExtension("GL_ARB_fragment_shader") ||
1509                                          functions->hasGLESExtension("GL_OES_standard_derivatives");
1510     extensions->shaderTextureLodEXT = functions->isAtLeastGL(gl::Version(3, 0)) ||
1511                                       functions->hasGLExtension("GL_ARB_shader_texture_lod") ||
1512                                       functions->hasGLESExtension("GL_EXT_shader_texture_lod");
1513     extensions->fragDepthEXT = functions->standard == STANDARD_GL_DESKTOP ||
1514                                functions->hasGLESExtension("GL_EXT_frag_depth");
1515     extensions->conservativeDepthEXT = functions->isAtLeastGL(gl::Version(4, 2)) ||
1516                                        functions->hasGLExtension("GL_ARB_conservative_depth") ||
1517                                        functions->hasGLESExtension("GL_EXT_conservative_depth");
1518     extensions->depthClampEXT = functions->isAtLeastGL(gl::Version(3, 2)) ||
1519                                 functions->hasGLExtension("GL_ARB_depth_clamp") ||
1520                                 functions->hasGLESExtension("GL_EXT_depth_clamp");
1521     extensions->polygonOffsetClampEXT = functions->hasExtension("GL_EXT_polygon_offset_clamp");
1522 
1523     if (functions->standard == STANDARD_GL_DESKTOP)
1524     {
1525         extensions->polygonModeNV = true;
1526     }
1527     else if (functions->hasGLESExtension("GL_NV_polygon_mode"))
1528     {
1529         // Some drivers expose the extension string without supporting its caps.
1530         ANGLE_GL_CLEAR_ERRORS(functions);
1531         functions->isEnabled(GL_POLYGON_OFFSET_LINE_NV);
1532         if (functions->getError() != GL_NO_ERROR)
1533         {
1534             WARN() << "Not enabling GL_NV_polygon_mode because "
1535                       "its native driver support is incomplete.";
1536         }
1537         else
1538         {
1539             extensions->polygonModeNV = true;
1540         }
1541     }
1542     extensions->polygonModeANGLE = extensions->polygonModeNV;
1543 
1544     // This functionality is provided by Shader Model 5 and should be available in GLSL 4.00
1545     // or even in older versions with GL_ARB_sample_shading and GL_ARB_gpu_shader5. However,
1546     // some OpenGL implementations (e.g., macOS) that do not support higher context versions
1547     // do not pass the tests so GLSL 4.20 is required here.
1548     extensions->sampleVariablesOES = functions->isAtLeastGL(gl::Version(4, 2)) ||
1549                                      functions->isAtLeastGLES(gl::Version(3, 2)) ||
1550                                      functions->hasGLESExtension("GL_OES_sample_variables");
1551     // Some drivers do not support sample qualifiers in ESSL 3.00, so ESSL 3.10 is required on ES.
1552     extensions->shaderMultisampleInterpolationOES =
1553         functions->isAtLeastGL(gl::Version(4, 2)) || functions->isAtLeastGLES(gl::Version(3, 2)) ||
1554         (functions->isAtLeastGLES(gl::Version(3, 1)) &&
1555          functions->hasGLESExtension("GL_OES_shader_multisample_interpolation"));
1556     if (extensions->shaderMultisampleInterpolationOES)
1557     {
1558         caps->minInterpolationOffset =
1559             QuerySingleGLFloat(functions, GL_MIN_FRAGMENT_INTERPOLATION_OFFSET_OES);
1560         caps->maxInterpolationOffset =
1561             QuerySingleGLFloat(functions, GL_MAX_FRAGMENT_INTERPOLATION_OFFSET_OES);
1562         caps->subPixelInterpolationOffsetBits =
1563             QuerySingleGLInt(functions, GL_FRAGMENT_INTERPOLATION_OFFSET_BITS_OES);
1564     }
1565 
1566     // Support video texture extension on non Android backends.
1567     // TODO(crbug.com/776222): support Android and Apple devices.
1568     extensions->videoTextureWEBGL = !IsAndroid() && !IsApple();
1569 
1570     if (functions->hasGLExtension("GL_ARB_shader_viewport_layer_array") ||
1571         functions->hasGLExtension("GL_NV_viewport_array2"))
1572     {
1573         extensions->multiviewOVR  = true;
1574         extensions->multiview2OVR = true;
1575         // GL_MAX_ARRAY_TEXTURE_LAYERS is guaranteed to be at least 256.
1576         const int maxLayers = QuerySingleGLInt(functions, GL_MAX_ARRAY_TEXTURE_LAYERS);
1577         // GL_MAX_VIEWPORTS is guaranteed to be at least 16.
1578         const int maxViewports       = QuerySingleGLInt(functions, GL_MAX_VIEWPORTS);
1579         caps->maxViews               = static_cast<GLuint>(std::min(maxLayers, maxViewports));
1580         *multiviewImplementationType = MultiviewImplementationTypeGL::NV_VIEWPORT_ARRAY2;
1581     }
1582 
1583     extensions->fboRenderMipmapOES = functions->isAtLeastGL(gl::Version(3, 0)) ||
1584                                      functions->hasGLExtension("GL_EXT_framebuffer_object") ||
1585                                      functions->isAtLeastGLES(gl::Version(3, 0)) ||
1586                                      functions->hasGLESExtension("GL_OES_fbo_render_mipmap");
1587 
1588     extensions->textureBorderClampEXT =
1589         !features.disableTextureClampToBorder.enabled &&
1590         (functions->standard == STANDARD_GL_DESKTOP ||
1591          functions->hasGLESExtension("GL_EXT_texture_border_clamp") ||
1592          functions->hasGLESExtension("GL_OES_texture_border_clamp") ||
1593          functions->hasGLESExtension("GL_NV_texture_border_clamp"));
1594     extensions->textureBorderClampOES = extensions->textureBorderClampEXT;
1595 
1596     // This functionality is supported on macOS but the extension
1597     // strings are not listed there for historical reasons.
1598     extensions->textureMirrorClampToEdgeEXT =
1599         !features.disableTextureMirrorClampToEdge.enabled &&
1600         (IsMac() || functions->isAtLeastGL(gl::Version(4, 4)) ||
1601          functions->hasGLExtension("GL_ARB_texture_mirror_clamp_to_edge") ||
1602          functions->hasGLExtension("GL_EXT_texture_mirror_clamp") ||
1603          functions->hasGLExtension("GL_ATI_texture_mirror_once") ||
1604          functions->hasGLESExtension("GL_EXT_texture_mirror_clamp_to_edge"));
1605 
1606     extensions->textureShadowLodEXT = functions->hasExtension("GL_EXT_texture_shadow_lod");
1607 
1608     extensions->multiDrawIndirectEXT = true;
1609     extensions->instancedArraysANGLE = functions->isAtLeastGL(gl::Version(3, 1)) ||
1610                                        (functions->hasGLExtension("GL_ARB_instanced_arrays") &&
1611                                         (functions->hasGLExtension("GL_ARB_draw_instanced") ||
1612                                          functions->hasGLExtension("GL_EXT_draw_instanced"))) ||
1613                                        functions->isAtLeastGLES(gl::Version(3, 0)) ||
1614                                        functions->hasGLESExtension("GL_EXT_instanced_arrays");
1615     extensions->instancedArraysEXT = extensions->instancedArraysANGLE;
1616     extensions->unpackSubimageEXT  = functions->standard == STANDARD_GL_DESKTOP ||
1617                                     functions->isAtLeastGLES(gl::Version(3, 0)) ||
1618                                     functions->hasGLESExtension("GL_EXT_unpack_subimage");
1619     // Some drivers do not support this extension in ESSL 3.00, so ESSL 3.10 is required on ES.
1620     extensions->shaderNoperspectiveInterpolationNV =
1621         functions->isAtLeastGL(gl::Version(3, 0)) ||
1622         (functions->isAtLeastGLES(gl::Version(3, 1)) &&
1623          functions->hasGLESExtension("GL_NV_shader_noperspective_interpolation"));
1624     extensions->packSubimageNV = functions->standard == STANDARD_GL_DESKTOP ||
1625                                  functions->isAtLeastGLES(gl::Version(3, 0)) ||
1626                                  functions->hasGLESExtension("GL_NV_pack_subimage");
1627     extensions->vertexArrayObjectOES = functions->isAtLeastGL(gl::Version(3, 0)) ||
1628                                        functions->hasGLExtension("GL_ARB_vertex_array_object") ||
1629                                        functions->isAtLeastGLES(gl::Version(3, 0)) ||
1630                                        functions->hasGLESExtension("GL_OES_vertex_array_object");
1631     extensions->debugMarkerEXT = functions->isAtLeastGL(gl::Version(4, 3)) ||
1632                                  functions->hasGLExtension("GL_KHR_debug") ||
1633                                  functions->hasGLExtension("GL_EXT_debug_marker") ||
1634                                  functions->isAtLeastGLES(gl::Version(3, 2)) ||
1635                                  functions->hasGLESExtension("GL_KHR_debug") ||
1636                                  functions->hasGLESExtension("GL_EXT_debug_marker");
1637     extensions->EGLImageOES         = functions->hasGLESExtension("GL_OES_EGL_image");
1638     extensions->EGLImageExternalOES = functions->hasGLESExtension("GL_OES_EGL_image_external");
1639     extensions->EGLImageExternalWrapModesEXT =
1640         functions->hasExtension("GL_EXT_EGL_image_external_wrap_modes");
1641     extensions->EGLImageExternalEssl3OES =
1642         functions->hasGLESExtension("GL_OES_EGL_image_external_essl3");
1643     extensions->EGLImageArrayEXT = functions->hasGLESExtension("GL_EXT_EGL_image_array");
1644 
1645     extensions->EGLSyncOES = functions->hasGLESExtension("GL_OES_EGL_sync");
1646 
1647     if (!features.disableTimestampQueries.enabled &&
1648         (functions->isAtLeastGL(gl::Version(3, 3)) ||
1649          functions->hasGLExtension("GL_ARB_timer_query") ||
1650          functions->hasGLESExtension("GL_EXT_disjoint_timer_query")))
1651     {
1652         extensions->disjointTimerQueryEXT = true;
1653 
1654         // If we can't query the counter bits, leave them at 0.
1655         if (!features.queryCounterBitsGeneratesErrors.enabled)
1656         {
1657             caps->queryCounterBitsTimeElapsed =
1658                 QueryQueryValue(functions, GL_TIME_ELAPSED, GL_QUERY_COUNTER_BITS);
1659             caps->queryCounterBitsTimestamp =
1660                 QueryQueryValue(functions, GL_TIMESTAMP, GL_QUERY_COUNTER_BITS);
1661         }
1662     }
1663 
1664     // the EXT_multisample_compatibility is written against ES3.1 but can apply
1665     // to earlier versions so therefore we're only checking for the extension string
1666     // and not the specific GLES version.
1667     extensions->multisampleCompatibilityEXT =
1668         functions->isAtLeastGL(gl::Version(1, 3)) ||
1669         functions->hasGLESExtension("GL_EXT_multisample_compatibility");
1670 
1671     extensions->framebufferMixedSamplesCHROMIUM =
1672         functions->hasGLExtension("GL_NV_framebuffer_mixed_samples") ||
1673         functions->hasGLESExtension("GL_NV_framebuffer_mixed_samples");
1674 
1675     extensions->robustnessEXT = functions->isAtLeastGL(gl::Version(4, 5)) ||
1676                                 functions->hasGLExtension("GL_KHR_robustness") ||
1677                                 functions->hasGLExtension("GL_ARB_robustness") ||
1678                                 functions->isAtLeastGLES(gl::Version(3, 2)) ||
1679                                 functions->hasGLESExtension("GL_KHR_robustness") ||
1680                                 functions->hasGLESExtension("GL_EXT_robustness");
1681     extensions->robustnessKHR = functions->isAtLeastGL(gl::Version(4, 5)) ||
1682                                 functions->hasGLExtension("GL_KHR_robustness") ||
1683                                 functions->isAtLeastGLES(gl::Version(3, 2)) ||
1684                                 functions->hasGLESExtension("GL_KHR_robustness");
1685 
1686     extensions->robustBufferAccessBehaviorKHR =
1687         extensions->robustnessEXT &&
1688         (functions->hasGLExtension("GL_ARB_robust_buffer_access_behavior") ||
1689          functions->hasGLESExtension("GL_KHR_robust_buffer_access_behavior"));
1690 
1691     extensions->stencilTexturingANGLE = functions->isAtLeastGL(gl::Version(4, 3)) ||
1692                                         functions->hasGLExtension("GL_ARB_stencil_texturing") ||
1693                                         functions->isAtLeastGLES(gl::Version(3, 1));
1694 
1695     if (features.supportsShaderFramebufferFetchEXT.enabled)
1696     {
1697         // We can support PLS natively, probably in tiled memory.
1698         extensions->shaderPixelLocalStorageANGLE         = true;
1699         extensions->shaderPixelLocalStorageCoherentANGLE = true;
1700         plsOptions->type             = ShPixelLocalStorageType::FramebufferFetch;
1701         plsOptions->fragmentSyncType = ShFragmentSynchronizationType::Automatic;
1702     }
1703     else
1704     {
1705         bool hasFragmentShaderImageLoadStore = false;
1706         if (functions->isAtLeastGL(gl::Version(4, 2)) ||
1707             functions->hasGLExtension("GL_ARB_shader_image_load_store"))
1708         {
1709             // [ANGLE_shader_pixel_local_storage] "New Implementation Dependent State":
1710             // MAX_PIXEL_LOCAL_STORAGE_PLANES_ANGLE must be at least 4.
1711             //
1712             // MAX_FRAGMENT_IMAGE_UNIFORMS is at least 8 on Desktop Core and ARB.
1713             hasFragmentShaderImageLoadStore = true;
1714         }
1715         else if (functions->isAtLeastGLES(gl::Version(3, 1)))
1716         {
1717             // [ANGLE_shader_pixel_local_storage] "New Implementation Dependent State":
1718             // MAX_PIXEL_LOCAL_STORAGE_PLANES_ANGLE must be at least 4.
1719             //
1720             // ES 3.1, Table 20.44: MAX_FRAGMENT_IMAGE_UNIFORMS can be 0.
1721             hasFragmentShaderImageLoadStore =
1722                 caps->maxShaderImageUniforms[gl::ShaderType::Fragment] >= 4;
1723         }
1724         if (hasFragmentShaderImageLoadStore &&
1725             (features.supportsFragmentShaderInterlockNV.enabled ||
1726              features.supportsFragmentShaderOrderingINTEL.enabled ||
1727              features.supportsFragmentShaderInterlockARB.enabled))
1728         {
1729             // If shader image load/store can be coherent, prefer it over
1730             // EXT_shader_framebuffer_fetch_non_coherent.
1731             extensions->shaderPixelLocalStorageANGLE         = true;
1732             extensions->shaderPixelLocalStorageCoherentANGLE = true;
1733             plsOptions->type = ShPixelLocalStorageType::ImageLoadStore;
1734             // Prefer vendor-specific extensions first. The PixelLocalStorageTest.Coherency test
1735             // doesn't always pass on Intel when we use the ARB extension.
1736             if (features.supportsFragmentShaderInterlockNV.enabled)
1737             {
1738                 plsOptions->fragmentSyncType =
1739                     ShFragmentSynchronizationType::FragmentShaderInterlock_NV_GL;
1740             }
1741             else if (features.supportsFragmentShaderOrderingINTEL.enabled)
1742             {
1743                 plsOptions->fragmentSyncType =
1744                     ShFragmentSynchronizationType::FragmentShaderOrdering_INTEL_GL;
1745             }
1746             else
1747             {
1748                 ASSERT(features.supportsFragmentShaderInterlockARB.enabled);
1749                 plsOptions->fragmentSyncType =
1750                     ShFragmentSynchronizationType::FragmentShaderInterlock_ARB_GL;
1751             }
1752             // OpenGL ES only allows read/write access to "r32*" images.
1753             plsOptions->supportsNativeRGBA8ImageFormats =
1754                 functions->standard != StandardGL::STANDARD_GL_ES;
1755         }
1756         else if (features.supportsShaderFramebufferFetchNonCoherentEXT.enabled)
1757         {
1758             extensions->shaderPixelLocalStorageANGLE = true;
1759             plsOptions->type                         = ShPixelLocalStorageType::FramebufferFetch;
1760         }
1761         else if (hasFragmentShaderImageLoadStore)
1762         {
1763             extensions->shaderPixelLocalStorageANGLE = true;
1764             plsOptions->type                         = ShPixelLocalStorageType::ImageLoadStore;
1765             // OpenGL ES only allows read/write access to "r32*" images.
1766             plsOptions->supportsNativeRGBA8ImageFormats =
1767                 functions->standard != StandardGL::STANDARD_GL_ES;
1768         }
1769     }
1770 
1771     // EXT_shader_framebuffer_fetch.
1772     if (features.supportsShaderFramebufferFetchEXT.enabled)
1773     {
1774         extensions->shaderFramebufferFetchEXT = true;
1775     }
1776 
1777     // TODO(http://anglebug.com/42266350): Support ARM_shader_framebuffer_fetch
1778 
1779     // EXT_shader_framebuffer_fetch_non_coherent.
1780     if (features.supportsShaderFramebufferFetchNonCoherentEXT.enabled)
1781     {
1782         extensions->shaderFramebufferFetchNonCoherentEXT = true;
1783     }
1784 
1785     extensions->copyTextureCHROMIUM = true;
1786     extensions->syncQueryCHROMIUM   = SyncQueryGL::IsSupported(functions);
1787 
1788     // Note that OES_texture_storage_multisample_2d_array support could be extended down to GL 3.2
1789     // if we emulated texStorage* API on top of texImage*.
1790     extensions->textureStorageMultisample2dArrayOES =
1791         functions->isAtLeastGL(gl::Version(4, 2)) || functions->isAtLeastGLES(gl::Version(3, 2));
1792 
1793     extensions->multiviewMultisampleANGLE = extensions->textureStorageMultisample2dArrayOES &&
1794                                             (extensions->multiviewOVR || extensions->multiview2OVR);
1795 
1796     extensions->textureMultisampleANGLE = functions->isAtLeastGL(gl::Version(3, 2)) ||
1797                                           functions->hasGLExtension("GL_ARB_texture_multisample") ||
1798                                           functions->isAtLeastGLES(gl::Version(3, 1));
1799 
1800     extensions->textureSRGBDecodeEXT = functions->hasGLExtension("GL_EXT_texture_sRGB_decode") ||
1801                                        functions->hasGLESExtension("GL_EXT_texture_sRGB_decode");
1802 
1803     // ANGLE treats ETC1 as ETC2 for ES 3.0 and higher because it becomes a core format, and they
1804     // are backwards compatible.
1805     extensions->compressedETC1RGB8SubTextureEXT =
1806         extensions->compressedETC2RGB8TextureOES || functions->isAtLeastGLES(gl::Version(3, 0)) ||
1807         functions->hasGLESExtension("GL_EXT_compressed_ETC1_RGB8_sub_texture");
1808 
1809 #if ANGLE_ENABLE_CGL
1810     VendorID vendor = GetVendorID(functions);
1811     if ((IsAMD(vendor) || IsIntel(vendor)) && *maxSupportedESVersion >= gl::Version(3, 0))
1812     {
1813         // Apple Intel/AMD drivers do not correctly use the TEXTURE_SRGB_DECODE property of
1814         // sampler states.  Disable this extension when we would advertise any ES version
1815         // that has samplers.
1816         extensions->textureSRGBDecodeEXT = false;
1817     }
1818 #endif
1819 
1820     extensions->sRGBWriteControlEXT = !features.srgbBlendingBroken.enabled &&
1821                                       (functions->isAtLeastGL(gl::Version(3, 0)) ||
1822                                        functions->hasGLExtension("GL_EXT_framebuffer_sRGB") ||
1823                                        functions->hasGLExtension("GL_ARB_framebuffer_sRGB") ||
1824                                        functions->hasGLESExtension("GL_EXT_sRGB_write_control"));
1825 
1826     if (features.bgraTexImageFormatsBroken.enabled)
1827     {
1828         extensions->textureFormatBGRA8888EXT = false;
1829     }
1830 
1831     // EXT_discard_framebuffer can be implemented as long as glDiscardFramebufferEXT or
1832     // glInvalidateFramebuffer is available
1833     extensions->discardFramebufferEXT = functions->isAtLeastGL(gl::Version(4, 3)) ||
1834                                         functions->hasGLExtension("GL_ARB_invalidate_subdata") ||
1835                                         functions->isAtLeastGLES(gl::Version(3, 0)) ||
1836                                         functions->hasGLESExtension("GL_EXT_discard_framebuffer") ||
1837                                         functions->hasGLESExtension("GL_ARB_invalidate_subdata");
1838 
1839     extensions->translatedShaderSourceANGLE = true;
1840 
1841     if (functions->isAtLeastGL(gl::Version(3, 1)) ||
1842         functions->hasGLExtension("GL_ARB_texture_rectangle"))
1843     {
1844         extensions->textureRectangleANGLE = true;
1845         caps->maxRectangleTextureSize =
1846             QuerySingleGLInt(functions, GL_MAX_RECTANGLE_TEXTURE_SIZE_ANGLE);
1847     }
1848 
1849     // OpenGL 4.3 (and above) and OpenGL ES 3.2 can support all features and constants defined in
1850     // GL_EXT_geometry_shader.
1851     bool hasCoreGSSupport =
1852         functions->isAtLeastGL(gl::Version(4, 3)) || functions->isAtLeastGLES(gl::Version(3, 2));
1853     // OpenGL 4.0 adds the support for instanced geometry shader
1854     // GL_ARB_shader_atomic_counters adds atomic counters to geometry shader
1855     // GL_ARB_shader_storage_buffer_object adds shader storage buffers to geometry shader
1856     // GL_ARB_shader_image_load_store adds images to geometry shader
1857     bool hasInstancedGSSupport = functions->isAtLeastGL(gl::Version(4, 0)) &&
1858                                  functions->hasGLExtension("GL_ARB_shader_atomic_counters") &&
1859                                  functions->hasGLExtension("GL_ARB_shader_storage_buffer_object") &&
1860                                  functions->hasGLExtension("GL_ARB_shader_image_load_store");
1861     if (hasCoreGSSupport || functions->hasGLESExtension("GL_OES_geometry_shader") ||
1862         functions->hasGLESExtension("GL_EXT_geometry_shader") || hasInstancedGSSupport)
1863     {
1864         extensions->geometryShaderEXT = functions->hasGLESExtension("GL_EXT_geometry_shader") ||
1865                                         hasCoreGSSupport || hasInstancedGSSupport;
1866         extensions->geometryShaderOES = functions->hasGLESExtension("GL_OES_geometry_shader") ||
1867                                         hasCoreGSSupport || hasInstancedGSSupport;
1868 
1869         caps->maxFramebufferLayers = QuerySingleGLInt(functions, GL_MAX_FRAMEBUFFER_LAYERS_EXT);
1870 
1871         // GL_PROVOKING_VERTEX isn't a valid return value of GL_LAYER_PROVOKING_VERTEX_EXT in
1872         // GL_EXT_geometry_shader SPEC, however it is legal in desktop OpenGL, which means the value
1873         // follows the one set by glProvokingVertex.
1874         // [OpenGL 4.3] Chapter 11.3.4.6
1875         // The vertex conventions followed for gl_Layer and gl_ViewportIndex may be determined by
1876         // calling GetIntegerv with the symbolic constants LAYER_PROVOKING_VERTEX and
1877         // VIEWPORT_INDEX_PROVOKING_VERTEX, respectively. For either query, if the value returned is
1878         // PROVOKING_VERTEX, then vertex selection follows the convention specified by
1879         // ProvokingVertex.
1880         caps->layerProvokingVertex = QuerySingleGLInt(functions, GL_LAYER_PROVOKING_VERTEX_EXT);
1881         if (caps->layerProvokingVertex == GL_PROVOKING_VERTEX)
1882         {
1883             // We should use GL_LAST_VERTEX_CONVENTION_EXT instead because desktop OpenGL SPEC
1884             // requires the initial value of provoking vertex mode is LAST_VERTEX_CONVENTION.
1885             // [OpenGL 4.3] Chapter 13.4
1886             // The initial value of the provoking vertex mode is LAST_VERTEX_CONVENTION.
1887             caps->layerProvokingVertex = GL_LAST_VERTEX_CONVENTION_EXT;
1888         }
1889 
1890         caps->maxShaderUniformComponents[gl::ShaderType::Geometry] =
1891             QuerySingleGLInt(functions, GL_MAX_GEOMETRY_UNIFORM_COMPONENTS_EXT);
1892         caps->maxShaderUniformBlocks[gl::ShaderType::Geometry] =
1893             QuerySingleGLInt(functions, GL_MAX_GEOMETRY_UNIFORM_BLOCKS_EXT);
1894         caps->maxCombinedShaderUniformComponents[gl::ShaderType::Geometry] =
1895             QuerySingleGLInt(functions, GL_MAX_COMBINED_GEOMETRY_UNIFORM_COMPONENTS_EXT);
1896         caps->maxGeometryInputComponents =
1897             QuerySingleGLInt(functions, GL_MAX_GEOMETRY_INPUT_COMPONENTS_EXT);
1898         caps->maxGeometryOutputComponents =
1899             QuerySingleGLInt(functions, GL_MAX_GEOMETRY_OUTPUT_COMPONENTS_EXT);
1900         caps->maxGeometryOutputVertices =
1901             QuerySingleGLInt(functions, GL_MAX_GEOMETRY_OUTPUT_VERTICES_EXT);
1902         caps->maxGeometryTotalOutputComponents =
1903             QuerySingleGLInt(functions, GL_MAX_GEOMETRY_TOTAL_OUTPUT_COMPONENTS_EXT);
1904         caps->maxGeometryShaderInvocations =
1905             QuerySingleGLInt(functions, GL_MAX_GEOMETRY_SHADER_INVOCATIONS_EXT);
1906         caps->maxShaderTextureImageUnits[gl::ShaderType::Geometry] =
1907             QuerySingleGLInt(functions, GL_MAX_GEOMETRY_TEXTURE_IMAGE_UNITS_EXT);
1908         caps->maxShaderAtomicCounterBuffers[gl::ShaderType::Geometry] =
1909             QuerySingleGLInt(functions, GL_MAX_GEOMETRY_ATOMIC_COUNTER_BUFFERS_EXT);
1910         caps->maxShaderAtomicCounters[gl::ShaderType::Geometry] =
1911             QuerySingleGLInt(functions, GL_MAX_GEOMETRY_ATOMIC_COUNTERS_EXT);
1912         caps->maxShaderImageUniforms[gl::ShaderType::Geometry] =
1913             QuerySingleGLInt(functions, GL_MAX_GEOMETRY_IMAGE_UNIFORMS_EXT);
1914         caps->maxShaderStorageBlocks[gl::ShaderType::Geometry] =
1915             QuerySingleGLInt(functions, GL_MAX_GEOMETRY_SHADER_STORAGE_BLOCKS_EXT);
1916     }
1917 
1918     // The real combined caps contain limits for shader types that are not available to ES, so limit
1919     // the caps to the sum of vertex+fragment+geometry shader caps.
1920     CapCombinedLimitToESShaders(&caps->maxCombinedUniformBlocks, caps->maxShaderUniformBlocks);
1921     CapCombinedLimitToESShaders(&caps->maxCombinedTextureImageUnits,
1922                                 caps->maxShaderTextureImageUnits);
1923     CapCombinedLimitToESShaders(&caps->maxCombinedShaderStorageBlocks,
1924                                 caps->maxShaderStorageBlocks);
1925     CapCombinedLimitToESShaders(&caps->maxCombinedImageUniforms, caps->maxShaderImageUniforms);
1926     CapCombinedLimitToESShaders(&caps->maxCombinedAtomicCounterBuffers,
1927                                 caps->maxShaderAtomicCounterBuffers);
1928     CapCombinedLimitToESShaders(&caps->maxCombinedAtomicCounters, caps->maxShaderAtomicCounters);
1929 
1930     // EXT_blend_func_extended is not implemented on top of ARB_blend_func_extended
1931     // because the latter does not support fragment shader output layout qualifiers.
1932     extensions->blendFuncExtendedEXT = !features.disableBlendFuncExtended.enabled &&
1933                                        (functions->isAtLeastGL(gl::Version(3, 3)) ||
1934                                         functions->hasGLESExtension("GL_EXT_blend_func_extended"));
1935     if (extensions->blendFuncExtendedEXT)
1936     {
1937         // TODO(http://anglebug.com/40644593): Support greater values of
1938         // MAX_DUAL_SOURCE_DRAW_BUFFERS_EXT queried from the driver. See comments in ProgramGL.cpp
1939         // for more information about this limitation.
1940         caps->maxDualSourceDrawBuffers = 1;
1941     }
1942 
1943     // EXT_float_blend
1944     // Assume all desktop driver supports this by default.
1945     extensions->floatBlendEXT = functions->standard == STANDARD_GL_DESKTOP ||
1946                                 functions->hasGLESExtension("GL_EXT_float_blend") ||
1947                                 functions->isAtLeastGLES(gl::Version(3, 2));
1948 
1949     // ANGLE_base_vertex_base_instance
1950     extensions->baseVertexBaseInstanceANGLE =
1951         !features.disableBaseInstanceVertex.enabled &&
1952         (functions->isAtLeastGL(gl::Version(3, 2)) || functions->isAtLeastGLES(gl::Version(3, 2)) ||
1953          functions->hasGLESExtension("GL_OES_draw_elements_base_vertex") ||
1954          functions->hasGLESExtension("GL_EXT_draw_elements_base_vertex"));
1955 
1956     // EXT_base_instance
1957     extensions->baseInstanceEXT =
1958         !features.disableBaseInstanceVertex.enabled &&
1959         (functions->isAtLeastGL(gl::Version(3, 2)) || functions->isAtLeastGLES(gl::Version(3, 2)) ||
1960          functions->hasGLESExtension("GL_OES_draw_elements_base_vertex") ||
1961          functions->hasGLESExtension("GL_EXT_draw_elements_base_vertex") ||
1962          functions->hasGLESExtension("GL_EXT_base_instance"));
1963 
1964     // ANGLE_base_vertex_base_instance_shader_builtin
1965     extensions->baseVertexBaseInstanceShaderBuiltinANGLE = extensions->baseVertexBaseInstanceANGLE;
1966 
1967     // OES_draw_elements_base_vertex
1968     extensions->drawElementsBaseVertexOES =
1969         functions->isAtLeastGL(gl::Version(3, 2)) || functions->isAtLeastGLES(gl::Version(3, 2)) ||
1970         functions->hasGLESExtension("GL_OES_draw_elements_base_vertex");
1971 
1972     // EXT_draw_elements_base_vertex
1973     extensions->drawElementsBaseVertexEXT =
1974         functions->isAtLeastGL(gl::Version(3, 2)) || functions->isAtLeastGLES(gl::Version(3, 2)) ||
1975         functions->hasGLESExtension("GL_EXT_draw_elements_base_vertex");
1976 
1977     // ANGLE_compressed_texture_etc
1978     // Expose this extension only when we support the formats or we're running on top of a native
1979     // ES driver.
1980     extensions->compressedTextureEtcANGLE =
1981         (features.allowETCFormats.enabled || functions->standard == STANDARD_GL_ES) &&
1982         gl::DetermineCompressedTextureETCSupport(*textureCapsMap);
1983 
1984     // When running on top of desktop OpenGL drivers and allow_etc_formats feature is not enabled,
1985     // mark ETC1 as emulated to hide it from WebGL clients.
1986     limitations->emulatedEtc1 =
1987         !features.allowETCFormats.enabled && functions->standard == STANDARD_GL_DESKTOP;
1988 
1989     // To work around broken unsized sRGB textures, sized sRGB textures are used. Disable EXT_sRGB
1990     // if those formats are not available.
1991     if (features.unsizedSRGBReadPixelsDoesntTransform.enabled &&
1992         !functions->isAtLeastGLES(gl::Version(3, 0)))
1993     {
1994         extensions->sRGBEXT = false;
1995     }
1996 
1997     extensions->provokingVertexANGLE = functions->hasGLExtension("GL_ARB_provoking_vertex") ||
1998                                        functions->hasGLExtension("GL_EXT_provoking_vertex") ||
1999                                        functions->isAtLeastGL(gl::Version(3, 2));
2000 
2001     extensions->textureExternalUpdateANGLE = true;
2002     extensions->texture3DOES               = functions->isAtLeastGL(gl::Version(1, 2)) ||
2003                                functions->isAtLeastGLES(gl::Version(3, 0)) ||
2004                                functions->hasGLESExtension("GL_OES_texture_3D");
2005 
2006     extensions->memoryObjectEXT = functions->hasGLExtension("GL_EXT_memory_object") ||
2007                                   functions->hasGLESExtension("GL_EXT_memory_object");
2008     extensions->semaphoreEXT = functions->hasGLExtension("GL_EXT_semaphore") ||
2009                                functions->hasGLESExtension("GL_EXT_semaphore");
2010     extensions->memoryObjectFdEXT = functions->hasGLExtension("GL_EXT_memory_object_fd") ||
2011                                     functions->hasGLESExtension("GL_EXT_memory_object_fd");
2012     extensions->semaphoreFdEXT = !features.disableSemaphoreFd.enabled &&
2013                                  (functions->hasGLExtension("GL_EXT_semaphore_fd") ||
2014                                   functions->hasGLESExtension("GL_EXT_semaphore_fd"));
2015     extensions->gpuShader5EXT = functions->isAtLeastGL(gl::Version(4, 0)) ||
2016                                 functions->isAtLeastGLES(gl::Version(3, 2)) ||
2017                                 functions->hasGLExtension("GL_ARB_gpu_shader5") ||
2018                                 functions->hasGLESExtension("GL_EXT_gpu_shader5") ||
2019                                 functions->hasGLESExtension("GL_OES_gpu_shader5");
2020     extensions->gpuShader5OES     = extensions->gpuShader5EXT;
2021     extensions->shaderIoBlocksOES = functions->isAtLeastGL(gl::Version(3, 2)) ||
2022                                     functions->isAtLeastGLES(gl::Version(3, 2)) ||
2023                                     functions->hasGLESExtension("GL_OES_shader_io_blocks") ||
2024                                     functions->hasGLESExtension("GL_EXT_shader_io_blocks");
2025     extensions->shaderIoBlocksEXT = extensions->shaderIoBlocksOES;
2026 
2027     extensions->shadowSamplersEXT = functions->isAtLeastGL(gl::Version(2, 0)) ||
2028                                     functions->isAtLeastGLES(gl::Version(3, 0)) ||
2029                                     functions->hasGLESExtension("GL_EXT_shadow_samplers");
2030 
2031     if (!features.disableClipControl.enabled)
2032     {
2033         extensions->clipControlEXT = functions->isAtLeastGL(gl::Version(4, 5)) ||
2034                                      functions->hasGLExtension("GL_ARB_clip_control") ||
2035                                      functions->hasGLESExtension("GL_EXT_clip_control");
2036     }
2037 
2038     if (features.disableRenderSnorm.enabled)
2039     {
2040         extensions->renderSnormEXT = false;
2041     }
2042 
2043     constexpr uint32_t kRequiredClipDistances                = 8;
2044     constexpr uint32_t kRequiredCullDistances                = 8;
2045     constexpr uint32_t kRequiredCombinedClipAndCullDistances = 8;
2046 
2047     // GL_APPLE_clip_distance cannot be implemented on top of GL_EXT_clip_cull_distance,
2048     // so require either native support or desktop GL.
2049     extensions->clipDistanceAPPLE = functions->isAtLeastGL(gl::Version(3, 0)) ||
2050                                     functions->hasGLESExtension("GL_APPLE_clip_distance");
2051     if (extensions->clipDistanceAPPLE)
2052     {
2053         caps->maxClipDistances = QuerySingleGLInt(functions, GL_MAX_CLIP_DISTANCES_APPLE);
2054 
2055         if (caps->maxClipDistances < kRequiredClipDistances)
2056         {
2057             WARN() << "Disabling GL_APPLE_clip_distance because only " << caps->maxClipDistances
2058                    << " clip distances are supported by the driver.";
2059             extensions->clipDistanceAPPLE = false;
2060         }
2061     }
2062 
2063     // GL_EXT_clip_cull_distance spec requires shader interface blocks to support
2064     // built-in array redeclarations on OpenGL ES.
2065     extensions->clipCullDistanceEXT =
2066         functions->isAtLeastGL(gl::Version(4, 5)) ||
2067         (functions->isAtLeastGL(gl::Version(3, 0)) &&
2068          functions->hasGLExtension("GL_ARB_cull_distance")) ||
2069         (extensions->shaderIoBlocksEXT && functions->hasGLESExtension("GL_EXT_clip_cull_distance"));
2070     if (extensions->clipCullDistanceEXT)
2071     {
2072         caps->maxClipDistances = QuerySingleGLInt(functions, GL_MAX_CLIP_DISTANCES_EXT);
2073         caps->maxCullDistances = QuerySingleGLInt(functions, GL_MAX_CULL_DISTANCES_EXT);
2074         caps->maxCombinedClipAndCullDistances =
2075             QuerySingleGLInt(functions, GL_MAX_COMBINED_CLIP_AND_CULL_DISTANCES_EXT);
2076 
2077         if (caps->maxClipDistances < kRequiredClipDistances ||
2078             caps->maxCullDistances < kRequiredCullDistances ||
2079             caps->maxCombinedClipAndCullDistances < kRequiredCombinedClipAndCullDistances)
2080         {
2081             WARN() << "Disabling GL_EXT_clip_cull_distance because only " << caps->maxClipDistances
2082                    << " clip distances, " << caps->maxCullDistances << " cull distances and "
2083                    << caps->maxCombinedClipAndCullDistances
2084                    << " combined clip/cull distances are supported by the driver.";
2085             extensions->clipCullDistanceEXT = false;
2086         }
2087     }
2088 
2089     // Same as GL_EXT_clip_cull_distance but with cull distance support being optional.
2090     extensions->clipCullDistanceANGLE =
2091         (functions->isAtLeastGL(gl::Version(3, 0)) || extensions->clipCullDistanceEXT) &&
2092         caps->maxClipDistances >= kRequiredClipDistances;
2093     ASSERT(!extensions->clipCullDistanceANGLE || caps->maxClipDistances > 0);
2094 
2095     // GL_OES_shader_image_atomic
2096     //
2097     // Note that imageAtomicExchange() is allowed to accept float textures (of r32f format) in this
2098     // extension, but that's not supported by ARB_shader_image_load_store which this extension is
2099     // based on, neither in the spec it was merged into it.  This support was only added to desktop
2100     // GLSL in version 4.5
2101     if (functions->isAtLeastGL(gl::Version(4, 5)) || functions->isAtLeastGLES(gl::Version(3, 2)) ||
2102         functions->hasGLESExtension("GL_OES_shader_image_atomic"))
2103     {
2104         extensions->shaderImageAtomicOES = true;
2105     }
2106 
2107     // GL_OES_texture_buffer
2108     if (functions->isAtLeastGL(gl::Version(4, 3)) || functions->isAtLeastGLES(gl::Version(3, 2)) ||
2109         functions->hasGLESExtension("GL_OES_texture_buffer") ||
2110         functions->hasGLESExtension("GL_EXT_texture_buffer") ||
2111         functions->hasGLExtension("GL_ARB_texture_buffer_object"))
2112     {
2113         caps->maxTextureBufferSize = QuerySingleGLInt(functions, GL_MAX_TEXTURE_BUFFER_SIZE);
2114         caps->textureBufferOffsetAlignment =
2115             QuerySingleGLInt(functions, GL_TEXTURE_BUFFER_OFFSET_ALIGNMENT);
2116         extensions->textureBufferOES = true;
2117         extensions->textureBufferEXT = true;
2118     }
2119     else
2120     {
2121         // Can't support ES3.2 without texture buffer objects
2122         LimitVersion(maxSupportedESVersion, gl::Version(3, 1));
2123     }
2124 
2125     extensions->YUVTargetEXT = functions->hasGLESExtension("GL_EXT_YUV_target");
2126 
2127     // GL_MESA_framebuffer_flip_y
2128     extensions->framebufferFlipYMESA = functions->hasGLESExtension("GL_MESA_framebuffer_flip_y") ||
2129                                        functions->hasGLExtension("GL_MESA_framebuffer_flip_y");
2130 
2131     // GL_KHR_parallel_shader_compile
2132     extensions->parallelShaderCompileKHR = !features.disableNativeParallelCompile.enabled &&
2133                                            (functions->maxShaderCompilerThreadsKHR != nullptr ||
2134                                             functions->maxShaderCompilerThreadsARB != nullptr);
2135 
2136     // GL_ANGLE_logic_op
2137     extensions->logicOpANGLE = functions->isAtLeastGL(gl::Version(2, 0));
2138 
2139     // GL_EXT_clear_texture
2140     extensions->clearTextureEXT = functions->isAtLeastGL(gl::Version(4, 4)) ||
2141                                   functions->hasGLESExtension("GL_EXT_clear_texture") ||
2142                                   functions->hasGLExtension("GL_ARB_clear_texture");
2143 
2144     // GL_QCOM_tiled_rendering
2145     extensions->tiledRenderingQCOM = !features.disableTiledRendering.enabled &&
2146                                      functions->hasGLESExtension("GL_QCOM_tiled_rendering");
2147 
2148     extensions->blendEquationAdvancedKHR =
2149         !features.disableBlendEquationAdvanced.enabled &&
2150         (functions->hasGLExtension("GL_NV_blend_equation_advanced") ||
2151          functions->hasGLExtension("GL_KHR_blend_equation_advanced") ||
2152          functions->isAtLeastGLES(gl::Version(3, 2)) ||
2153          functions->hasGLESExtension("GL_KHR_blend_equation_advanced"));
2154     extensions->blendEquationAdvancedCoherentKHR =
2155         !features.disableBlendEquationAdvanced.enabled &&
2156         (functions->hasGLExtension("GL_NV_blend_equation_advanced_coherent") ||
2157          functions->hasGLExtension("GL_KHR_blend_equation_advanced_coherent") ||
2158          functions->isAtLeastGLES(gl::Version(3, 2)) ||
2159          functions->hasGLESExtension("GL_KHR_blend_equation_advanced_coherent"));
2160 
2161     // PVRTC1 textures must be squares on Apple platforms.
2162     if (IsApple())
2163     {
2164         limitations->squarePvrtc1 = true;
2165     }
2166 
2167     // Check if the driver clamps constant blend color
2168     if (IsQualcomm(GetVendorID(functions)))
2169     {
2170         // Backup current state
2171         float oldColor[4];
2172         functions->getFloatv(GL_BLEND_COLOR, oldColor);
2173 
2174         // Probe clamping
2175         float color[4];
2176         functions->blendColor(2.0, 0.0, 0.0, 0.0);
2177         functions->getFloatv(GL_BLEND_COLOR, color);
2178         limitations->noUnclampedBlendColor = color[0] == 1.0;
2179 
2180         // Restore previous state
2181         functions->blendColor(oldColor[0], oldColor[1], oldColor[2], oldColor[3]);
2182     }
2183 }
2184 
GetSystemInfoVendorIDAndDeviceID(const FunctionsGL * functions,angle::SystemInfo * outSystemInfo,angle::VendorID * outVendor,angle::DeviceID * outDevice)2185 bool GetSystemInfoVendorIDAndDeviceID(const FunctionsGL *functions,
2186                                       angle::SystemInfo *outSystemInfo,
2187                                       angle::VendorID *outVendor,
2188                                       angle::DeviceID *outDevice)
2189 {
2190     // Get vendor from GL itself, so on multi-GPU systems the correct GPU is selected.
2191     *outVendor = GetVendorID(functions);
2192     *outDevice = 0;
2193 
2194     // Gather additional information from the system to detect multi-GPU scenarios. Do not collect
2195     // system info on Android as it may use Vulkan which can crash on older devices. All the system
2196     // info we need is available in the version and renderer strings on Android.
2197     bool isGetSystemInfoSuccess = !IsAndroid() && angle::GetSystemInfo(outSystemInfo);
2198 
2199     // Get the device id from system info, corresponding to the vendor of the active GPU.
2200     if (isGetSystemInfoSuccess && !outSystemInfo->gpus.empty())
2201     {
2202         if (*outVendor == VENDOR_ID_UNKNOWN)
2203         {
2204             // If vendor ID is unknown, take the best estimate of the active GPU.  Chances are there
2205             // is only one GPU anyway.
2206             *outVendor = outSystemInfo->gpus[outSystemInfo->activeGPUIndex].vendorId;
2207             *outDevice = outSystemInfo->gpus[outSystemInfo->activeGPUIndex].deviceId;
2208         }
2209         else
2210         {
2211             for (const angle::GPUDeviceInfo &gpu : outSystemInfo->gpus)
2212             {
2213                 if (*outVendor == gpu.vendorId)
2214                 {
2215                     // Note that deviceId may not necessarily have been possible to retrieve.
2216                     *outDevice = gpu.deviceId;
2217                     break;
2218                 }
2219             }
2220         }
2221     }
2222     else
2223     {
2224         // If system info is not available, attempt to deduce the device from GL itself.
2225         *outDevice = GetDeviceID(functions);
2226     }
2227 
2228     return isGetSystemInfoSuccess;
2229 }
2230 
Has9thGenIntelGPU(const angle::SystemInfo & systemInfo)2231 bool Has9thGenIntelGPU(const angle::SystemInfo &systemInfo)
2232 {
2233     for (const angle::GPUDeviceInfo &deviceInfo : systemInfo.gpus)
2234     {
2235         if (IsIntel(deviceInfo.vendorId) && Is9thGenIntel(deviceInfo.deviceId))
2236         {
2237             return true;
2238         }
2239     }
2240 
2241     return false;
2242 }
2243 
InitializeFeatures(const FunctionsGL * functions,angle::FeaturesGL * features)2244 void InitializeFeatures(const FunctionsGL *functions, angle::FeaturesGL *features)
2245 {
2246     angle::VendorID vendor;
2247     angle::DeviceID device;
2248     angle::SystemInfo systemInfo;
2249 
2250     bool isGetSystemInfoSuccess =
2251         GetSystemInfoVendorIDAndDeviceID(functions, &systemInfo, &vendor, &device);
2252 
2253     bool isAMD      = IsAMD(vendor);
2254     bool isApple    = IsAppleGPU(vendor);
2255     bool isIntel    = IsIntel(vendor);
2256     bool isNvidia   = IsNvidia(vendor);
2257     bool isQualcomm = IsQualcomm(vendor);
2258     bool isVMWare   = IsVMWare(vendor);
2259     bool hasAMD     = systemInfo.hasAMDGPU();
2260     bool isMali     = IsARM(vendor);
2261 
2262     std::array<int, 3> mesaVersion = {0, 0, 0};
2263     bool isMesa                    = IsMesa(functions, &mesaVersion);
2264 
2265     int qualcommVersion = -1;
2266     if (!isMesa && isQualcomm)
2267     {
2268         qualcommVersion = GetQualcommVersion(functions);
2269     }
2270 
2271     // Don't use 1-bit alpha formats on desktop GL with AMD drivers.
2272     ANGLE_FEATURE_CONDITION(features, avoid1BitAlphaTextureFormats,
2273                             functions->standard == STANDARD_GL_DESKTOP && isAMD);
2274 
2275     ANGLE_FEATURE_CONDITION(features, RGBA4IsNotSupportedForColorRendering,
2276                             functions->standard == STANDARD_GL_DESKTOP && isIntel);
2277 
2278     // Although "Sandy Bridge", "Ivy Bridge", and "Haswell" may support GL_ARB_ES3_compatibility
2279     // extension, ETC2/EAC formats are emulated there. Newer Intel GPUs support them natively.
2280     ANGLE_FEATURE_CONDITION(
2281         features, allowETCFormats,
2282         isIntel && !IsSandyBridge(device) && !IsIvyBridge(device) && !IsHaswell(device));
2283 
2284     // Mesa always exposes ASTC extension but only Intel Gen9, Gen11, and Gen12 have hardware
2285     // support for it. Newer Intel GPUs (Gen12.5+) do not support ASTC.
2286     ANGLE_FEATURE_CONDITION(features, allowAstcFormats,
2287                             !isMesa || isIntel && (Is9thGenIntel(device) || IsGeminiLake(device) ||
2288                                                    IsCoffeeLake(device) || Is11thGenIntel(device) ||
2289                                                    Is12thGenIntel(device)));
2290 
2291     // Ported from gpu_driver_bug_list.json (#183)
2292     ANGLE_FEATURE_CONDITION(features, emulateAbsIntFunction, IsApple() && isIntel);
2293 
2294     ANGLE_FEATURE_CONDITION(features, addAndTrueToLoopCondition, IsApple() && isIntel);
2295 
2296     // Ported from gpu_driver_bug_list.json (#191)
2297     ANGLE_FEATURE_CONDITION(
2298         features, emulateIsnanFloat,
2299         isIntel && IsApple() && IsSkylake(device) && GetMacOSVersion() < OSVersion(10, 13, 2));
2300 
2301     // https://anglebug.com/42266803
2302     ANGLE_FEATURE_CONDITION(features, clearsWithGapsNeedFlush,
2303                             !isMesa && isQualcomm && qualcommVersion < 490);
2304 
2305     ANGLE_FEATURE_CONDITION(features, doesSRGBClearsOnLinearFramebufferAttachments,
2306                             isIntel || isAMD);
2307 
2308     ANGLE_FEATURE_CONDITION(features, emulateMaxVertexAttribStride,
2309                             IsLinux() && functions->standard == STANDARD_GL_DESKTOP && isAMD);
2310     ANGLE_FEATURE_CONDITION(
2311         features, useUnusedBlocksWithStandardOrSharedLayout,
2312         (IsApple() && functions->standard == STANDARD_GL_DESKTOP) || (IsLinux() && isAMD));
2313 
2314     // Ported from gpu_driver_bug_list.json (#187)
2315     ANGLE_FEATURE_CONDITION(features, doWhileGLSLCausesGPUHang,
2316                             IsApple() && functions->standard == STANDARD_GL_DESKTOP &&
2317                                 GetMacOSVersion() < OSVersion(10, 11, 0));
2318 
2319     // Ported from gpu_driver_bug_list.json (#211)
2320     ANGLE_FEATURE_CONDITION(features, rewriteFloatUnaryMinusOperator,
2321                             IsApple() && isIntel && GetMacOSVersion() < OSVersion(10, 12, 0));
2322 
2323     ANGLE_FEATURE_CONDITION(features, vertexIDDoesNotIncludeBaseVertex, IsApple() && isAMD);
2324 
2325     // Triggers a bug on Marshmallow Adreno (4xx?) driver.
2326     // http://anglebug.com/40096454
2327     ANGLE_FEATURE_CONDITION(features, dontInitializeUninitializedLocals, !isMesa && isQualcomm);
2328 
2329     ANGLE_FEATURE_CONDITION(features, finishDoesNotCauseQueriesToBeAvailable,
2330                             functions->standard == STANDARD_GL_DESKTOP && isNvidia);
2331 
2332     // TODO(cwallez): Disable this workaround for MacOSX versions 10.9 or later.
2333     ANGLE_FEATURE_CONDITION(features, alwaysCallUseProgramAfterLink, true);
2334 
2335     ANGLE_FEATURE_CONDITION(features, unpackOverlappingRowsSeparatelyUnpackBuffer, isNvidia);
2336     ANGLE_FEATURE_CONDITION(features, packOverlappingRowsSeparatelyPackBuffer, isNvidia);
2337 
2338     ANGLE_FEATURE_CONDITION(features, initializeCurrentVertexAttributes, isNvidia);
2339 
2340     ANGLE_FEATURE_CONDITION(features, unpackLastRowSeparatelyForPaddingInclusion,
2341                             IsApple() || isNvidia);
2342     ANGLE_FEATURE_CONDITION(features, packLastRowSeparatelyForPaddingInclusion,
2343                             IsApple() || isNvidia);
2344 
2345     ANGLE_FEATURE_CONDITION(features, removeInvariantAndCentroidForESSL3,
2346                             functions->isAtMostGL(gl::Version(4, 1)) ||
2347                                 (functions->standard == STANDARD_GL_DESKTOP && isAMD));
2348 
2349     // TODO(oetuaho): Make this specific to the affected driver versions. Versions that came after
2350     // 364 are known to be affected, at least up to 375.
2351     ANGLE_FEATURE_CONDITION(features, emulateAtan2Float, isNvidia);
2352 
2353     ANGLE_FEATURE_CONDITION(features, reapplyUBOBindingsAfterUsingBinaryProgram,
2354                             isAMD || IsAndroid());
2355 
2356     // TODO(oetuaho): Make this specific to the affected driver versions. Versions at least up to
2357     // 390 are known to be affected. Versions after that are expected not to be affected.
2358     ANGLE_FEATURE_CONDITION(features, clampFragDepth, isNvidia);
2359 
2360     // TODO(oetuaho): Make this specific to the affected driver versions. Versions since 397.31 are
2361     // not affected.
2362     ANGLE_FEATURE_CONDITION(features, rewriteRepeatedAssignToSwizzled, isNvidia);
2363 
2364     // TODO(jmadill): Narrow workaround range for specific devices.
2365 
2366     ANGLE_FEATURE_CONDITION(features, clampPointSize, IsAndroid() || isNvidia);
2367 
2368     // Ported from gpu_driver_bug_list.json (#246, #258)
2369     ANGLE_FEATURE_CONDITION(features, dontUseLoopsToInitializeVariables,
2370                             (!isMesa && isQualcomm) || (isIntel && IsApple()));
2371 
2372     // Intel macOS condition ported from gpu_driver_bug_list.json (#327)
2373     ANGLE_FEATURE_CONDITION(features, disableBlendFuncExtended,
2374                             IsApple() && isIntel && GetMacOSVersion() < OSVersion(10, 14, 0));
2375 
2376     ANGLE_FEATURE_CONDITION(features, avoidBindFragDataLocation, !isMesa && isQualcomm);
2377 
2378     ANGLE_FEATURE_CONDITION(features, unsizedSRGBReadPixelsDoesntTransform, !isMesa && isQualcomm);
2379 
2380     ANGLE_FEATURE_CONDITION(features, queryCounterBitsGeneratesErrors, IsNexus5X(vendor, device));
2381 
2382     bool isAndroidLessThan14 = IsAndroid() && GetAndroidSDKVersion() < 34;
2383     bool isAndroidAtLeast14  = IsAndroid() && GetAndroidSDKVersion() >= 34;
2384     bool isIntelLinuxLessThanKernelVersion5 =
2385         isIntel && IsLinux() && GetLinuxOSVersion() < OSVersion(5, 0, 0);
2386     ANGLE_FEATURE_CONDITION(features, limitWebglMaxTextureSizeTo4096,
2387                             isAndroidLessThan14 || isIntelLinuxLessThanKernelVersion5);
2388     ANGLE_FEATURE_CONDITION(features, limitWebglMaxTextureSizeTo8192, isAndroidAtLeast14);
2389     // On Apple switchable graphics, GL_MAX_SAMPLES may differ between the GPUs.
2390     // 4 is a lowest common denominator that is always supported.
2391     ANGLE_FEATURE_CONDITION(features, limitMaxMSAASamplesTo4,
2392                             IsAndroid() || (IsApple() && (isIntel || isAMD || isNvidia)));
2393     ANGLE_FEATURE_CONDITION(features, limitMax3dArrayTextureSizeTo1024,
2394                             isIntelLinuxLessThanKernelVersion5);
2395 
2396     ANGLE_FEATURE_CONDITION(features, allowClearForRobustResourceInit, IsApple());
2397 
2398     // The WebGL conformance/uniforms/out-of-bounds-uniform-array-access test has been seen to fail
2399     // on AMD and Android devices.
2400     // This test is also flaky on Linux Nvidia. So we just turn it on everywhere and don't rely on
2401     // driver since security is important.
2402     ANGLE_FEATURE_CONDITION(
2403         features, clampArrayAccess,
2404         IsAndroid() || isAMD || !functions->hasExtension("GL_KHR_robust_buffer_access_behavior"));
2405 
2406     ANGLE_FEATURE_CONDITION(features, resetTexImage2DBaseLevel,
2407                             IsApple() && isIntel && GetMacOSVersion() >= OSVersion(10, 12, 4));
2408 
2409     ANGLE_FEATURE_CONDITION(features, clearToZeroOrOneBroken,
2410                             IsApple() && isIntel && GetMacOSVersion() < OSVersion(10, 12, 6));
2411 
2412     ANGLE_FEATURE_CONDITION(features, adjustSrcDstRegionForBlitFramebuffer,
2413                             IsLinux() || (IsAndroid() && isNvidia) || (IsWindows() && isNvidia) ||
2414                                 (IsApple() && functions->standard == STANDARD_GL_ES));
2415 
2416     ANGLE_FEATURE_CONDITION(features, clipSrcRegionForBlitFramebuffer,
2417                             IsApple() || (IsLinux() && isAMD));
2418 
2419     ANGLE_FEATURE_CONDITION(features, RGBDXT1TexturesSampleZeroAlpha, IsApple());
2420 
2421     ANGLE_FEATURE_CONDITION(features, unfoldShortCircuits, IsApple());
2422 
2423     ANGLE_FEATURE_CONDITION(features, emulatePrimitiveRestartFixedIndex,
2424                             functions->standard == STANDARD_GL_DESKTOP &&
2425                                 functions->isAtLeastGL(gl::Version(3, 1)) &&
2426                                 !functions->isAtLeastGL(gl::Version(4, 3)));
2427     ANGLE_FEATURE_CONDITION(
2428         features, setPrimitiveRestartFixedIndexForDrawArrays,
2429         features->emulatePrimitiveRestartFixedIndex.enabled && IsApple() && isIntel);
2430 
2431     ANGLE_FEATURE_CONDITION(features, removeDynamicIndexingOfSwizzledVector,
2432                             IsApple() || IsAndroid() || IsWindows());
2433 
2434     // Ported from gpu_driver_bug_list.json (#89)
2435     ANGLE_FEATURE_CONDITION(features, regenerateStructNames, IsApple());
2436 
2437     // Ported from gpu_driver_bug_list.json (#184)
2438     ANGLE_FEATURE_CONDITION(features, preAddTexelFetchOffsets, IsApple() && isIntel);
2439 
2440     // Workaround for the widespread OpenGL ES driver implementaion bug
2441     ANGLE_FEATURE_CONDITION(features, readPixelsUsingImplementationColorReadFormatForNorm16,
2442                             !isIntel && functions->standard == STANDARD_GL_ES &&
2443                                 functions->isAtLeastGLES(gl::Version(3, 1)) &&
2444                                 functions->hasGLESExtension("GL_EXT_texture_norm16"));
2445 
2446     // anglebug.com/40644715
2447     ANGLE_FEATURE_CONDITION(features, flushBeforeDeleteTextureIfCopiedTo, IsApple() && isIntel);
2448 
2449     // anglebug.com/40096480
2450     // Seems to affect both Intel and AMD GPUs. Enable workaround for all GPUs on macOS.
2451     ANGLE_FEATURE_CONDITION(features, rewriteRowMajorMatrices,
2452                             // IsApple() && functions->standard == STANDARD_GL_DESKTOP);
2453                             // TODO(anglebug.com/40096480): diagnose crashes with this workaround.
2454                             false);
2455 
2456     ANGLE_FEATURE_CONDITION(features, disableDrawBuffersIndexed, IsWindows() && isAMD);
2457 
2458     ANGLE_FEATURE_CONDITION(
2459         features, disableSemaphoreFd,
2460         IsLinux() && isAMD && isMesa && mesaVersion < (std::array<int, 3>{19, 3, 5}));
2461 
2462     ANGLE_FEATURE_CONDITION(
2463         features, disableTimestampQueries,
2464         (IsLinux() && isVMWare) || (IsAndroid() && isNvidia) ||
2465             (IsAndroid() && GetAndroidSDKVersion() < 27 && IsAdreno5xxOrOlder(functions)) ||
2466             (!isMesa && IsMaliT8xxOrOlder(functions)) || (!isMesa && IsMaliG31OrOlder(functions)));
2467 
2468     ANGLE_FEATURE_CONDITION(features, decodeEncodeSRGBForGenerateMipmap, IsApple());
2469 
2470     // anglebug.com/42263273
2471     // The (redundant) explicit exclusion of Windows AMD is because the workaround fails
2472     // Texture2DRGTest.TextureRGUNormTest on that platform, and the test is skipped. If
2473     // you'd like to enable the workaround on Windows AMD, please fix the test first.
2474     ANGLE_FEATURE_CONDITION(
2475         features, emulateCopyTexImage2DFromRenderbuffers,
2476         IsApple() && functions->standard == STANDARD_GL_ES && !(isAMD && IsWindows()));
2477 
2478     // anglebug.com/40096747
2479     // Replace copyTexImage2D with texImage2D + copyTexSubImage2D to bypass driver bug.
2480     ANGLE_FEATURE_CONDITION(features, emulateCopyTexImage2D, isApple);
2481 
2482     // Don't attempt to use the discrete GPU on NVIDIA-based MacBook Pros, since the
2483     // driver is unstable in this situation.
2484     //
2485     // Note that this feature is only set here in order to advertise this workaround
2486     // externally. GPU switching support must be enabled or disabled early, during display
2487     // initialization, before these features are set up.
2488     bool isDualGPUMacWithNVIDIA = false;
2489     if (IsApple() && functions->standard == STANDARD_GL_DESKTOP)
2490     {
2491         if (isGetSystemInfoSuccess)
2492         {
2493             // The full system information must be queried to see whether it's a dual-GPU
2494             // NVIDIA MacBook Pro since it's likely that the integrated GPU will be active
2495             // when these features are initialized.
2496             isDualGPUMacWithNVIDIA = systemInfo.isMacSwitchable && systemInfo.hasNVIDIAGPU();
2497         }
2498     }
2499     ANGLE_FEATURE_CONDITION(features, disableGPUSwitchingSupport, isDualGPUMacWithNVIDIA);
2500 
2501     // Workaround issue in NVIDIA GL driver on Linux when TSAN is enabled
2502     // http://crbug.com/1094869
2503     bool isTSANBuild = false;
2504 #ifdef THREAD_SANITIZER
2505     isTSANBuild = true;
2506 #endif
2507     ANGLE_FEATURE_CONDITION(features, disableNativeParallelCompile,
2508                             isTSANBuild && IsLinux() && isNvidia);
2509 
2510     // anglebug.com/40096712
2511     ANGLE_FEATURE_CONDITION(features, emulatePackSkipRowsAndPackSkipPixels, IsApple());
2512 
2513     // http://crbug.com/1042393
2514     // XWayland defaults to a 1hz refresh rate when the "surface is not visible", which sometimes
2515     // causes issues in Chrome. To get around this, default to a 30Hz refresh rate if we see bogus
2516     // from the driver.
2517     ANGLE_FEATURE_CONDITION(features, clampMscRate, IsLinux() && IsWayland());
2518 
2519     ANGLE_FEATURE_CONDITION(features, bindTransformFeedbackBufferBeforeBindBufferRange, IsApple());
2520 
2521     // http://crbug.com/1137851
2522     // Speculative fix for above issue, users can enable it via flags.
2523     // http://crbug.com/1187475
2524     // Disable on Mesa 20 / Intel
2525     ANGLE_FEATURE_CONDITION(features, disableSyncControlSupport,
2526                             IsLinux() && isIntel && isMesa && mesaVersion[0] == 20);
2527 
2528     ANGLE_FEATURE_CONDITION(features, keepBufferShadowCopy, !CanMapBufferForRead(functions));
2529 
2530     ANGLE_FEATURE_CONDITION(features, setZeroLevelBeforeGenerateMipmap, IsApple());
2531 
2532     ANGLE_FEATURE_CONDITION(features, promotePackedFormatsTo8BitPerChannel, IsApple() && hasAMD);
2533 
2534     // crbug.com/1171371
2535     // If output variable gl_FragColor is written by fragment shader, it may cause context lost with
2536     // Adreno 42x and 3xx.
2537     ANGLE_FEATURE_CONDITION(features, initFragmentOutputVariables, IsAdreno42xOr3xx(functions));
2538 
2539     // http://crbug.com/1144207
2540     // The Mac bot with Intel Iris GPU seems unaffected by this bug. Exclude the Haswell family for
2541     // now.
2542     ANGLE_FEATURE_CONDITION(features, shiftInstancedArrayDataWithOffset,
2543                             IsApple() && IsIntel(vendor) && !IsHaswell(device));
2544     ANGLE_FEATURE_CONDITION(features, syncAllVertexArraysToDefault,
2545                             !nativegl::SupportsVertexArrayObjects(functions));
2546 
2547     // NVIDIA OpenGL ES emulated profile cannot handle client arrays
2548     ANGLE_FEATURE_CONDITION(features, syncDefaultVertexArraysToDefault,
2549                             nativegl::CanUseDefaultVertexArrayObject(functions) && !isNvidia);
2550 
2551     // http://crbug.com/1181193
2552     // On desktop Linux/AMD when using the amdgpu drivers, the precise kernel and DRM version are
2553     // leaked via GL_RENDERER. We workaround this too improve user security.
2554     ANGLE_FEATURE_CONDITION(features, sanitizeAMDGPURendererString, IsLinux() && hasAMD);
2555 
2556     // http://crbug.com/1187513
2557     // Imagination drivers are buggy with context switching. It needs to unbind fbo before context
2558     // switching to workadround the driver issues.
2559     ANGLE_FEATURE_CONDITION(features, unbindFBOBeforeSwitchingContext, IsPowerVR(vendor));
2560 
2561     // http://crbug.com/1181068 and http://crbug.com/783979
2562     ANGLE_FEATURE_CONDITION(features, flushOnFramebufferChange,
2563                             IsApple() && Has9thGenIntelGPU(systemInfo));
2564 
2565     // Disable GL_EXT_multisampled_render_to_texture on a bunch of different configurations:
2566 
2567     // http://crbug.com/490379
2568     // http://crbug.com/767913
2569     bool isAdreno4xxOnAndroidLessThan51 =
2570         IsAndroid() && IsAdreno4xx(functions) && GetAndroidSDKVersion() < 22;
2571 
2572     // http://crbug.com/612474
2573     bool isAdreno4xxOnAndroid70 =
2574         IsAndroid() && IsAdreno4xx(functions) && GetAndroidSDKVersion() == 24;
2575     bool isAdreno5xxOnAndroidLessThan70 =
2576         IsAndroid() && IsAdreno5xx(functions) && GetAndroidSDKVersion() < 24;
2577 
2578     // http://crbug.com/663811
2579     bool isAdreno5xxOnAndroid71 =
2580         IsAndroid() && IsAdreno5xx(functions) && GetAndroidSDKVersion() == 25;
2581 
2582     // http://crbug.com/594016
2583     bool isLinuxVivante = IsLinux() && IsVivante(device);
2584 
2585     // http://anglebug.com/42266736
2586     bool isWindowsNVIDIA = IsWindows() && IsNvidia(vendor);
2587 
2588     // Temporarily disable on all of Android. http://crbug.com/1417485
2589     ANGLE_FEATURE_CONDITION(features, disableMultisampledRenderToTexture,
2590                             isAdreno4xxOnAndroidLessThan51 || isAdreno4xxOnAndroid70 ||
2591                                 isAdreno5xxOnAndroidLessThan70 || isAdreno5xxOnAndroid71 ||
2592                                 isLinuxVivante || isWindowsNVIDIA);
2593 
2594     // http://crbug.com/1181068
2595     ANGLE_FEATURE_CONDITION(features, uploadTextureDataInChunks, IsApple());
2596 
2597     // https://crbug.com/1060012
2598     ANGLE_FEATURE_CONDITION(features, emulateImmutableCompressedTexture3D, isQualcomm);
2599 
2600     // https://crbug.com/1300575
2601     ANGLE_FEATURE_CONDITION(features, emulateRGB10, functions->standard == STANDARD_GL_DESKTOP);
2602 
2603     // https://anglebug.com/42264072
2604     ANGLE_FEATURE_CONDITION(features, alwaysUnbindFramebufferTexture2D,
2605                             isNvidia && (IsWindows() || IsLinux()));
2606 
2607     // https://anglebug.com/42265877
2608     ANGLE_FEATURE_CONDITION(features, disableTextureClampToBorder, false);
2609 
2610     // https://anglebug.com/42265995
2611     ANGLE_FEATURE_CONDITION(features, passHighpToPackUnormSnormBuiltins, isQualcomm);
2612 
2613     // https://anglebug.com/42266348
2614     ANGLE_FEATURE_CONDITION(features, emulateClipDistanceState, isQualcomm);
2615 
2616     // https://anglebug.com/42266817
2617     ANGLE_FEATURE_CONDITION(features, emulateClipOrigin,
2618                             !isMesa && isQualcomm && qualcommVersion < 490 &&
2619                                 functions->hasGLESExtension("GL_EXT_clip_control"));
2620 
2621     // https://anglebug.com/42266740
2622     ANGLE_FEATURE_CONDITION(features, explicitFragmentLocations, isQualcomm);
2623 
2624     // Desktop GLSL-only fragment synchronization extensions. These are injected internally by the
2625     // compiler to make pixel local storage coherent.
2626     // https://anglebug.com/40096838
2627     ANGLE_FEATURE_CONDITION(features, supportsFragmentShaderInterlockNV,
2628                             functions->isAtLeastGL(gl::Version(4, 3)) &&
2629                                 functions->hasGLExtension("GL_NV_fragment_shader_interlock"));
2630     ANGLE_FEATURE_CONDITION(features, supportsFragmentShaderOrderingINTEL,
2631                             functions->isAtLeastGL(gl::Version(4, 4)) &&
2632                                 functions->hasGLExtension("GL_INTEL_fragment_shader_ordering"));
2633     ANGLE_FEATURE_CONDITION(features, supportsFragmentShaderInterlockARB,
2634                             functions->isAtLeastGL(gl::Version(4, 5)) &&
2635                                 functions->hasGLExtension("GL_ARB_fragment_shader_interlock"));
2636 
2637     // EXT_shader_framebuffer_fetch
2638     ANGLE_FEATURE_CONDITION(features, supportsShaderFramebufferFetchEXT,
2639                             functions->hasGLESExtension("GL_EXT_shader_framebuffer_fetch"));
2640 
2641     // EXT_shader_framebuffer_fetch_non_coherent
2642     ANGLE_FEATURE_CONDITION(
2643         features, supportsShaderFramebufferFetchNonCoherentEXT,
2644         functions->hasGLESExtension("GL_EXT_shader_framebuffer_fetch_non_coherent"));
2645 
2646     // https://crbug.com/1356053
2647     ANGLE_FEATURE_CONDITION(features, bindCompleteFramebufferForTimerQueries, isMali);
2648 
2649     // https://crbug.com/40264674
2650     ANGLE_FEATURE_CONDITION(features, disableClipControl, IsMaliG72OrG76OrG51(functions));
2651 
2652     // https://anglebug.com/42266811
2653     ANGLE_FEATURE_CONDITION(features, resyncDepthRangeOnClipControl, !isMesa && isQualcomm);
2654 
2655     // https://anglebug.com/42266745
2656     ANGLE_FEATURE_CONDITION(features, disableRenderSnorm,
2657                             isMesa && (mesaVersion < (std::array<int, 3>{21, 3, 0}) ||
2658                                        (mesaVersion < (std::array<int, 3>{23, 3, 0}) &&
2659                                         functions->standard == STANDARD_GL_ES)));
2660 
2661     // https://anglebug.com/42266748
2662     ANGLE_FEATURE_CONDITION(features, disableTextureMirrorClampToEdge,
2663                             functions->standard == STANDARD_GL_ES && isMesa &&
2664                                 mesaVersion < (std::array<int, 3>{23, 1, 7}));
2665 
2666     // http://anglebug.com/42266610
2667     ANGLE_FEATURE_CONDITION(features, disableBaseInstanceVertex, IsMaliValhall(functions));
2668 
2669     // Mali: http://crbug.com/40063287
2670     // Nvidia: http://crbug.com/328015191
2671     ANGLE_FEATURE_CONDITION(features, scalarizeVecAndMatConstructorArgs, isMali || isNvidia);
2672 
2673     // http://crbug.com/40066076
2674     ANGLE_FEATURE_CONDITION(features, ensureNonEmptyBufferIsBoundForDraw, IsApple() || IsAndroid());
2675 
2676     // https://anglebug.com/42266857
2677     ANGLE_FEATURE_CONDITION(features, preTransformTextureCubeGradDerivatives, isApple);
2678 
2679     // https://crbug.com/40279678
2680     ANGLE_FEATURE_CONDITION(features, useIntermediateTextureForGenerateMipmap,
2681                             IsPixel7OrPixel8(functions));
2682 
2683     // SRGB blending does not appear to work correctly on the Nexus 5 + other QC devices. Writing to
2684     // an SRGB framebuffer with GL_FRAMEBUFFER_SRGB enabled and then reading back returns the same
2685     // value. Disabling GL_FRAMEBUFFER_SRGB will then convert in the wrong direction.
2686     ANGLE_FEATURE_CONDITION(features, srgbBlendingBroken, IsQualcomm(vendor));
2687 
2688     // BGRA formats do not appear to be accepted by the qualcomm driver despite the extension being
2689     // exposed.
2690     ANGLE_FEATURE_CONDITION(features, bgraTexImageFormatsBroken, IsQualcomm(vendor));
2691 
2692     // https://github.com/flutter/flutter/issues/47164
2693     // https://github.com/flutter/flutter/issues/47804
2694     // Some devices expose the QCOM tiled memory extension string but don't actually provide the
2695     // start and end tiling functions.
2696     bool missingTilingEntryPoints = functions->hasGLESExtension("GL_QCOM_tiled_rendering") &&
2697                                     (!functions->startTilingQCOM || !functions->endTilingQCOM);
2698 
2699     // http://skbug.com/9491: Nexus5 produces rendering artifacts when we use QCOM_tiled_rendering.
2700     ANGLE_FEATURE_CONDITION(features, disableTiledRendering,
2701                             missingTilingEntryPoints || IsAdreno3xx(functions));
2702 
2703     // Intel desktop GL drivers fail many Skia blend tests.
2704     // Block on older Qualcomm and ARM, following Skia's blocklists.
2705     ANGLE_FEATURE_CONDITION(
2706         features, disableBlendEquationAdvanced,
2707         (isIntel && IsWindows()) || IsAdreno4xx(functions) || IsAdreno5xx(functions) || isMali);
2708 }
2709 
InitializeFrontendFeatures(const FunctionsGL * functions,angle::FrontendFeatures * features)2710 void InitializeFrontendFeatures(const FunctionsGL *functions, angle::FrontendFeatures *features)
2711 {
2712     VendorID vendor = GetVendorID(functions);
2713     bool isQualcomm = IsQualcomm(vendor);
2714 
2715     std::array<int, 3> mesaVersion = {0, 0, 0};
2716     bool isMesa                    = IsMesa(functions, &mesaVersion);
2717 
2718     ANGLE_FEATURE_CONDITION(features, disableProgramCachingForTransformFeedback,
2719                             !isMesa && isQualcomm);
2720     // https://crbug.com/480992
2721     // Disable shader program cache to workaround PowerVR Rogue issues.
2722     ANGLE_FEATURE_CONDITION(features, disableProgramBinary, IsPowerVrRogue(functions));
2723 
2724     // The compile and link jobs need a context, and previous experiments showed setting up temp
2725     // contexts in threads for the sake of program link triggers too many driver bugs.  See
2726     // https://chromium-review.googlesource.com/c/angle/angle/+/4774785 for context.
2727     //
2728     // As a result, the compile and link jobs are done in the same thread as the call.  If the
2729     // native driver supports parallel compile/link, it's still done internally by the driver, and
2730     // ANGLE supports delaying post-compile and post-link operations until that is done.
2731     ANGLE_FEATURE_CONDITION(features, compileJobIsThreadSafe, false);
2732     ANGLE_FEATURE_CONDITION(features, linkJobIsThreadSafe, false);
2733 
2734     ANGLE_FEATURE_CONDITION(features, cacheCompiledShader, true);
2735 }
2736 
ReInitializeFeaturesAtGPUSwitch(const FunctionsGL * functions,angle::FeaturesGL * features)2737 void ReInitializeFeaturesAtGPUSwitch(const FunctionsGL *functions, angle::FeaturesGL *features)
2738 {
2739     angle::VendorID vendor;
2740     angle::DeviceID device;
2741     angle::SystemInfo systemInfo;
2742 
2743     GetSystemInfoVendorIDAndDeviceID(functions, &systemInfo, &vendor, &device);
2744 
2745     // http://crbug.com/1144207
2746     // The Mac bot with Intel Iris GPU seems unaffected by this bug. Exclude the Haswell family for
2747     // now.
2748     // We need to reinitialize this feature when switching between buggy and non-buggy GPUs.
2749     ANGLE_FEATURE_CONDITION(features, shiftInstancedArrayDataWithOffset,
2750                             IsApple() && IsIntel(vendor) && !IsHaswell(device));
2751 }
2752 
2753 }  // namespace nativegl_gl
2754 
2755 namespace nativegl
2756 {
2757 
SupportsVertexArrayObjects(const FunctionsGL * functions)2758 bool SupportsVertexArrayObjects(const FunctionsGL *functions)
2759 {
2760     return functions->isAtLeastGLES(gl::Version(3, 0)) ||
2761            functions->hasGLESExtension("GL_OES_vertex_array_object") ||
2762            functions->isAtLeastGL(gl::Version(3, 0)) ||
2763            functions->hasGLExtension("GL_ARB_vertex_array_object");
2764 }
2765 
CanUseDefaultVertexArrayObject(const FunctionsGL * functions)2766 bool CanUseDefaultVertexArrayObject(const FunctionsGL *functions)
2767 {
2768     return (functions->profile & GL_CONTEXT_CORE_PROFILE_BIT) == 0;
2769 }
2770 
CanUseClientSideArrays(const FunctionsGL * functions,GLuint vao)2771 bool CanUseClientSideArrays(const FunctionsGL *functions, GLuint vao)
2772 {
2773     // Can use client arrays on GLES or GL compatability profile only on the default VAO
2774     return CanUseDefaultVertexArrayObject(functions) && vao == 0;
2775 }
2776 
SupportsCompute(const FunctionsGL * functions)2777 bool SupportsCompute(const FunctionsGL *functions)
2778 {
2779     // OpenGL 4.2 is required for GL_ARB_compute_shader, some platform drivers have the extension,
2780     // but their maximum supported GL versions are less than 4.2. Explicitly limit the minimum
2781     // GL version to 4.2.
2782     return (functions->isAtLeastGL(gl::Version(4, 3)) ||
2783             functions->isAtLeastGLES(gl::Version(3, 1)) ||
2784             (functions->isAtLeastGL(gl::Version(4, 2)) &&
2785              functions->hasGLExtension("GL_ARB_compute_shader") &&
2786              functions->hasGLExtension("GL_ARB_shader_storage_buffer_object")));
2787 }
2788 
SupportsFenceSync(const FunctionsGL * functions)2789 bool SupportsFenceSync(const FunctionsGL *functions)
2790 {
2791     return functions->isAtLeastGL(gl::Version(3, 2)) || functions->hasGLExtension("GL_ARB_sync") ||
2792            functions->isAtLeastGLES(gl::Version(3, 0));
2793 }
2794 
SupportsOcclusionQueries(const FunctionsGL * functions)2795 bool SupportsOcclusionQueries(const FunctionsGL *functions)
2796 {
2797     return functions->isAtLeastGL(gl::Version(1, 5)) ||
2798            functions->hasGLExtension("GL_ARB_occlusion_query2") ||
2799            functions->isAtLeastGLES(gl::Version(3, 0)) ||
2800            functions->hasGLESExtension("GL_EXT_occlusion_query_boolean");
2801 }
2802 
SupportsNativeRendering(const FunctionsGL * functions,gl::TextureType type,GLenum internalFormat)2803 bool SupportsNativeRendering(const FunctionsGL *functions,
2804                              gl::TextureType type,
2805                              GLenum internalFormat)
2806 {
2807     // Some desktop drivers allow rendering to formats that are not required by the spec, this is
2808     // exposed through the GL_FRAMEBUFFER_RENDERABLE query.
2809     bool hasInternalFormatQuery = functions->isAtLeastGL(gl::Version(4, 3)) ||
2810                                   functions->hasGLExtension("GL_ARB_internalformat_query2");
2811 
2812     // Some Intel drivers have a bug that returns GL_FULL_SUPPORT when asked if they support
2813     // rendering to compressed texture formats yet return framebuffer incomplete when attempting to
2814     // render to the format.  Skip any native queries for compressed formats.
2815     const gl::InternalFormat &internalFormatInfo = gl::GetSizedInternalFormatInfo(internalFormat);
2816 
2817     if (hasInternalFormatQuery && !internalFormatInfo.compressed)
2818     {
2819         GLint framebufferRenderable = GL_NONE;
2820         functions->getInternalformativ(ToGLenum(type), internalFormat, GL_FRAMEBUFFER_RENDERABLE, 1,
2821                                        &framebufferRenderable);
2822         return framebufferRenderable != GL_NONE;
2823     }
2824     else
2825     {
2826         const nativegl::InternalFormat &nativeInfo =
2827             nativegl::GetInternalFormatInfo(internalFormat, functions->standard);
2828         return nativegl_gl::MeetsRequirements(functions, nativeInfo.textureAttachment);
2829     }
2830 }
2831 
SupportsTexImage(gl::TextureType type)2832 bool SupportsTexImage(gl::TextureType type)
2833 {
2834     switch (type)
2835     {
2836             // Multi-sample texture types only support TexStorage data upload
2837         case gl::TextureType::_2DMultisample:
2838         case gl::TextureType::_2DMultisampleArray:
2839             return false;
2840 
2841         default:
2842             return true;
2843     }
2844 }
2845 
UseTexImage2D(gl::TextureType textureType)2846 bool UseTexImage2D(gl::TextureType textureType)
2847 {
2848     return textureType == gl::TextureType::_2D || textureType == gl::TextureType::CubeMap ||
2849            textureType == gl::TextureType::Rectangle ||
2850            textureType == gl::TextureType::_2DMultisample ||
2851            textureType == gl::TextureType::External || textureType == gl::TextureType::VideoImage;
2852 }
2853 
UseTexImage3D(gl::TextureType textureType)2854 bool UseTexImage3D(gl::TextureType textureType)
2855 {
2856     return textureType == gl::TextureType::_2DArray || textureType == gl::TextureType::_3D ||
2857            textureType == gl::TextureType::_2DMultisampleArray ||
2858            textureType == gl::TextureType::CubeMapArray;
2859 }
2860 
GetTextureBindingQuery(gl::TextureType textureType)2861 GLenum GetTextureBindingQuery(gl::TextureType textureType)
2862 {
2863     switch (textureType)
2864     {
2865         case gl::TextureType::_2D:
2866             return GL_TEXTURE_BINDING_2D;
2867         case gl::TextureType::_2DArray:
2868             return GL_TEXTURE_BINDING_2D_ARRAY;
2869         case gl::TextureType::_2DMultisample:
2870             return GL_TEXTURE_BINDING_2D_MULTISAMPLE;
2871         case gl::TextureType::_2DMultisampleArray:
2872             return GL_TEXTURE_BINDING_2D_MULTISAMPLE_ARRAY;
2873         case gl::TextureType::_3D:
2874             return GL_TEXTURE_BINDING_3D;
2875         case gl::TextureType::External:
2876             return GL_TEXTURE_BINDING_EXTERNAL_OES;
2877         case gl::TextureType::Rectangle:
2878             return GL_TEXTURE_BINDING_RECTANGLE;
2879         case gl::TextureType::CubeMap:
2880             return GL_TEXTURE_BINDING_CUBE_MAP;
2881         case gl::TextureType::CubeMapArray:
2882             return GL_TEXTURE_BINDING_CUBE_MAP_ARRAY_OES;
2883         case gl::TextureType::Buffer:
2884             return GL_TEXTURE_BINDING_BUFFER;
2885         default:
2886             UNREACHABLE();
2887             return 0;
2888     }
2889 }
2890 
GetTextureBindingTarget(gl::TextureType textureType)2891 GLenum GetTextureBindingTarget(gl::TextureType textureType)
2892 {
2893     return ToGLenum(GetNativeTextureType(textureType));
2894 }
2895 
GetTextureBindingTarget(gl::TextureTarget textureTarget)2896 GLenum GetTextureBindingTarget(gl::TextureTarget textureTarget)
2897 {
2898     return ToGLenum(GetNativeTextureTarget(textureTarget));
2899 }
2900 
GetBufferBindingQuery(gl::BufferBinding bufferBinding)2901 GLenum GetBufferBindingQuery(gl::BufferBinding bufferBinding)
2902 {
2903     switch (bufferBinding)
2904     {
2905         case gl::BufferBinding::Array:
2906             return GL_ARRAY_BUFFER_BINDING;
2907         case gl::BufferBinding::AtomicCounter:
2908             return GL_ATOMIC_COUNTER_BUFFER_BINDING;
2909         case gl::BufferBinding::CopyRead:
2910             return GL_COPY_READ_BUFFER_BINDING;
2911         case gl::BufferBinding::CopyWrite:
2912             return GL_COPY_WRITE_BUFFER_BINDING;
2913         case gl::BufferBinding::DispatchIndirect:
2914             return GL_DISPATCH_INDIRECT_BUFFER_BINDING;
2915         case gl::BufferBinding::DrawIndirect:
2916             return GL_DRAW_INDIRECT_BUFFER_BINDING;
2917         case gl::BufferBinding::ElementArray:
2918             return GL_ELEMENT_ARRAY_BUFFER_BINDING;
2919         case gl::BufferBinding::PixelPack:
2920             return GL_PIXEL_PACK_BUFFER_BINDING;
2921         case gl::BufferBinding::PixelUnpack:
2922             return GL_PIXEL_UNPACK_BUFFER_BINDING;
2923         case gl::BufferBinding::ShaderStorage:
2924             return GL_SHADER_STORAGE_BUFFER_BINDING;
2925         case gl::BufferBinding::TransformFeedback:
2926             return GL_TRANSFORM_FEEDBACK_BUFFER_BINDING;
2927         case gl::BufferBinding::Uniform:
2928             return GL_UNIFORM_BUFFER_BINDING;
2929         case gl::BufferBinding::Texture:
2930             return GL_TEXTURE_BUFFER_BINDING;
2931         default:
2932             UNREACHABLE();
2933             return 0;
2934     }
2935 }
2936 
GetBufferBindingString(gl::BufferBinding bufferBinding)2937 std::string GetBufferBindingString(gl::BufferBinding bufferBinding)
2938 {
2939     std::ostringstream os;
2940     os << bufferBinding << "_BINDING";
2941     return os.str();
2942 }
2943 
GetNativeTextureType(gl::TextureType type)2944 gl::TextureType GetNativeTextureType(gl::TextureType type)
2945 {
2946     // VideoImage texture type is a WebGL type. It doesn't have
2947     // directly mapping type in native OpenGL/OpenGLES.
2948     // Actually, it will be translated to different texture type
2949     // (TEXTURE2D, TEXTURE_EXTERNAL_OES and TEXTURE_RECTANGLE)
2950     // based on OS and other conditions.
2951     // This will introduce problem that binding VideoImage may
2952     // unbind native image implicitly. Please make sure state
2953     // manager is aware of this implicit unbind behaviour.
2954     if (type != gl::TextureType::VideoImage)
2955     {
2956         return type;
2957     }
2958 
2959     // TODO(http://anglebug.com/42262534): need to figure out rectangle texture and
2960     // external image when these backend are implemented.
2961     return gl::TextureType::_2D;
2962 }
2963 
GetNativeTextureTarget(gl::TextureTarget target)2964 gl::TextureTarget GetNativeTextureTarget(gl::TextureTarget target)
2965 {
2966     // VideoImage texture type is a WebGL type. It doesn't have
2967     // directly mapping type in native OpenGL/OpenGLES.
2968     // Actually, it will be translated to different texture target
2969     // (TEXTURE2D, TEXTURE_EXTERNAL_OES and TEXTURE_RECTANGLE)
2970     // based on OS and other conditions.
2971     // This will introduce problem that binding VideoImage may
2972     // unbind native image implicitly. Please make sure state
2973     // manager is aware of this implicit unbind behaviour.
2974     if (target != gl::TextureTarget::VideoImage)
2975     {
2976         return target;
2977     }
2978 
2979     // TODO(http://anglebug.com/42262534): need to figure out rectangle texture and
2980     // external image when these backend are implemented.
2981     return gl::TextureTarget::_2D;
2982 }
2983 
2984 }  // namespace nativegl
2985 
GetFunctionsGL(const gl::Context * context)2986 const FunctionsGL *GetFunctionsGL(const gl::Context *context)
2987 {
2988     return GetImplAs<ContextGL>(context)->getFunctions();
2989 }
2990 
GetStateManagerGL(const gl::Context * context)2991 StateManagerGL *GetStateManagerGL(const gl::Context *context)
2992 {
2993     return GetImplAs<ContextGL>(context)->getStateManager();
2994 }
2995 
GetBlitGL(const gl::Context * context)2996 BlitGL *GetBlitGL(const gl::Context *context)
2997 {
2998     return GetImplAs<ContextGL>(context)->getBlitter();
2999 }
3000 
GetMultiviewClearer(const gl::Context * context)3001 ClearMultiviewGL *GetMultiviewClearer(const gl::Context *context)
3002 {
3003     return GetImplAs<ContextGL>(context)->getMultiviewClearer();
3004 }
3005 
GetFeaturesGL(const gl::Context * context)3006 const angle::FeaturesGL &GetFeaturesGL(const gl::Context *context)
3007 {
3008     return GetImplAs<ContextGL>(context)->getFeaturesGL();
3009 }
3010 
ClearErrors(const FunctionsGL * functions,const char * file,const char * function,unsigned int line)3011 void ClearErrors(const FunctionsGL *functions,
3012                  const char *file,
3013                  const char *function,
3014                  unsigned int line)
3015 {
3016     GLenum error = functions->getError();
3017     while (error != GL_NO_ERROR)
3018     {
3019         INFO() << "Preexisting GL error " << gl::FmtHex(error) << " as of " << file << ", "
3020                << function << ":" << line << ". ";
3021 
3022         // Skip GL_CONTEXT_LOST errors, they will be generated continuously and result in an
3023         // infinite loop.
3024         if (error == GL_CONTEXT_LOST)
3025         {
3026             return;
3027         }
3028 
3029         error = functions->getError();
3030     }
3031 }
3032 
ClearErrors(const gl::Context * context,const char * file,const char * function,unsigned int line)3033 void ClearErrors(const gl::Context *context,
3034                  const char *file,
3035                  const char *function,
3036                  unsigned int line)
3037 {
3038     const FunctionsGL *functions = GetFunctionsGL(context);
3039     ClearErrors(functions, file, function, line);
3040 }
3041 
CheckError(const gl::Context * context,const char * call,const char * file,const char * function,unsigned int line)3042 angle::Result CheckError(const gl::Context *context,
3043                          const char *call,
3044                          const char *file,
3045                          const char *function,
3046                          unsigned int line)
3047 {
3048     return HandleError(context, GetFunctionsGL(context)->getError(), call, file, function, line);
3049 }
3050 
HandleError(const gl::Context * context,GLenum error,const char * call,const char * file,const char * function,unsigned int line)3051 angle::Result HandleError(const gl::Context *context,
3052                           GLenum error,
3053                           const char *call,
3054                           const char *file,
3055                           const char *function,
3056                           unsigned int line)
3057 {
3058     const FunctionsGL *functions = GetFunctionsGL(context);
3059     if (ANGLE_UNLIKELY(error != GL_NO_ERROR))
3060     {
3061         ContextGL *contextGL = GetImplAs<ContextGL>(context);
3062         contextGL->handleError(error, "Unexpected driver error.", file, function, line);
3063         ERR() << "GL call " << call << " generated error " << gl::FmtHex(error) << " in " << file
3064               << ", " << function << ":" << line << ". ";
3065 
3066         // Check that only one GL error was generated, ClearErrors should have been called first.
3067         // Skip GL_CONTEXT_LOST errors, they will be generated continuously and result in an
3068         // infinite loop.
3069         GLenum nextError = functions->getError();
3070         while (nextError != GL_NO_ERROR && nextError != GL_CONTEXT_LOST)
3071         {
3072             ERR() << "Additional GL error " << gl::FmtHex(nextError) << " generated.";
3073             nextError = functions->getError();
3074         }
3075 
3076         return angle::Result::Stop;
3077     }
3078 
3079     return angle::Result::Continue;
3080 }
3081 
CanMapBufferForRead(const FunctionsGL * functions)3082 bool CanMapBufferForRead(const FunctionsGL *functions)
3083 {
3084     return (functions->mapBufferRange != nullptr) ||
3085            (functions->mapBuffer != nullptr && functions->standard == STANDARD_GL_DESKTOP);
3086 }
3087 
MapBufferRangeWithFallback(const FunctionsGL * functions,GLenum target,size_t offset,size_t length,GLbitfield access)3088 uint8_t *MapBufferRangeWithFallback(const FunctionsGL *functions,
3089                                     GLenum target,
3090                                     size_t offset,
3091                                     size_t length,
3092                                     GLbitfield access)
3093 {
3094     if (functions->mapBufferRange != nullptr)
3095     {
3096         return static_cast<uint8_t *>(functions->mapBufferRange(target, offset, length, access));
3097     }
3098     else if (functions->mapBuffer != nullptr &&
3099              (functions->standard == STANDARD_GL_DESKTOP || access == GL_MAP_WRITE_BIT))
3100     {
3101         // Only the read and write bits are supported
3102         ASSERT((access & (GL_MAP_READ_BIT | GL_MAP_WRITE_BIT)) != 0);
3103 
3104         GLenum accessEnum = 0;
3105         if (access == (GL_MAP_READ_BIT | GL_MAP_WRITE_BIT))
3106         {
3107             accessEnum = GL_READ_WRITE;
3108         }
3109         else if (access == GL_MAP_READ_BIT)
3110         {
3111             accessEnum = GL_READ_ONLY;
3112         }
3113         else if (access == GL_MAP_WRITE_BIT)
3114         {
3115             accessEnum = GL_WRITE_ONLY;
3116         }
3117         else
3118         {
3119             UNREACHABLE();
3120             return nullptr;
3121         }
3122 
3123         return static_cast<uint8_t *>(functions->mapBuffer(target, accessEnum)) + offset;
3124     }
3125     else
3126     {
3127         // No options available
3128         UNREACHABLE();
3129         return nullptr;
3130     }
3131 }
3132 
ShouldApplyLastRowPaddingWorkaround(ContextGL * contextGL,const gl::Extents & size,const gl::PixelStoreStateBase & state,const gl::Buffer * pixelBuffer,GLenum format,GLenum type,bool is3D,const void * pixels,bool * shouldApplyOut)3133 angle::Result ShouldApplyLastRowPaddingWorkaround(ContextGL *contextGL,
3134                                                   const gl::Extents &size,
3135                                                   const gl::PixelStoreStateBase &state,
3136                                                   const gl::Buffer *pixelBuffer,
3137                                                   GLenum format,
3138                                                   GLenum type,
3139                                                   bool is3D,
3140                                                   const void *pixels,
3141                                                   bool *shouldApplyOut)
3142 {
3143     if (pixelBuffer == nullptr)
3144     {
3145         *shouldApplyOut = false;
3146         return angle::Result::Continue;
3147     }
3148 
3149     // We are using an pack or unpack buffer, compute what the driver thinks is going to be the
3150     // last byte read or written. If it is past the end of the buffer, we will need to use the
3151     // workaround otherwise the driver will generate INVALID_OPERATION and not do the operation.
3152 
3153     const gl::InternalFormat &glFormat = gl::GetInternalFormatInfo(format, type);
3154     GLuint endByte                     = 0;
3155     ANGLE_CHECK_GL_MATH(contextGL,
3156                         glFormat.computePackUnpackEndByte(type, size, state, is3D, &endByte));
3157     GLuint rowPitch = 0;
3158     ANGLE_CHECK_GL_MATH(contextGL, glFormat.computeRowPitch(type, size.width, state.alignment,
3159                                                             state.rowLength, &rowPitch));
3160 
3161     CheckedNumeric<size_t> checkedPixelBytes = glFormat.computePixelBytes(type);
3162     CheckedNumeric<size_t> checkedEndByte =
3163         angle::CheckedNumeric<size_t>(endByte) + reinterpret_cast<intptr_t>(pixels);
3164 
3165     // At this point checkedEndByte is the actual last byte read.
3166     // The driver adds an extra row padding (if any), mimic it.
3167     ANGLE_CHECK_GL_MATH(contextGL, checkedPixelBytes.IsValid());
3168     if (static_cast<size_t>(checkedPixelBytes.ValueOrDie()) * size.width < rowPitch)
3169     {
3170         checkedEndByte += rowPitch - checkedPixelBytes * size.width;
3171     }
3172 
3173     ANGLE_CHECK_GL_MATH(contextGL, checkedEndByte.IsValid());
3174 
3175     *shouldApplyOut = checkedEndByte.ValueOrDie() > static_cast<size_t>(pixelBuffer->getSize());
3176     return angle::Result::Continue;
3177 }
3178 
GenerateContextCreationToTry(EGLint requestedType,bool isMesaGLX)3179 std::vector<ContextCreationTry> GenerateContextCreationToTry(EGLint requestedType, bool isMesaGLX)
3180 {
3181     using Type                         = ContextCreationTry::Type;
3182     constexpr EGLint kPlatformOpenGL   = EGL_PLATFORM_ANGLE_TYPE_OPENGL_ANGLE;
3183     constexpr EGLint kPlatformOpenGLES = EGL_PLATFORM_ANGLE_TYPE_OPENGLES_ANGLE;
3184 
3185     std::vector<ContextCreationTry> contextsToTry;
3186 
3187     if (requestedType == EGL_PLATFORM_ANGLE_TYPE_DEFAULT_ANGLE || requestedType == kPlatformOpenGL)
3188     {
3189         contextsToTry.emplace_back(kPlatformOpenGL, Type::DESKTOP_CORE, gl::Version(4, 5));
3190         contextsToTry.emplace_back(kPlatformOpenGL, Type::DESKTOP_CORE, gl::Version(4, 4));
3191         contextsToTry.emplace_back(kPlatformOpenGL, Type::DESKTOP_CORE, gl::Version(4, 3));
3192         contextsToTry.emplace_back(kPlatformOpenGL, Type::DESKTOP_CORE, gl::Version(4, 2));
3193         contextsToTry.emplace_back(kPlatformOpenGL, Type::DESKTOP_CORE, gl::Version(4, 1));
3194         contextsToTry.emplace_back(kPlatformOpenGL, Type::DESKTOP_CORE, gl::Version(4, 0));
3195         contextsToTry.emplace_back(kPlatformOpenGL, Type::DESKTOP_CORE, gl::Version(3, 3));
3196         contextsToTry.emplace_back(kPlatformOpenGL, Type::DESKTOP_CORE, gl::Version(3, 2));
3197         contextsToTry.emplace_back(kPlatformOpenGL, Type::DESKTOP_LEGACY, gl::Version(3, 3));
3198 
3199         // On Mesa, do not try to create OpenGL context versions between 3.0 and
3200         // 3.2 because of compatibility problems. See crbug.com/659030
3201         if (!isMesaGLX)
3202         {
3203             contextsToTry.emplace_back(kPlatformOpenGL, Type::DESKTOP_LEGACY, gl::Version(3, 2));
3204             contextsToTry.emplace_back(kPlatformOpenGL, Type::DESKTOP_LEGACY, gl::Version(3, 1));
3205             contextsToTry.emplace_back(kPlatformOpenGL, Type::DESKTOP_LEGACY, gl::Version(3, 0));
3206         }
3207 
3208         contextsToTry.emplace_back(kPlatformOpenGL, Type::DESKTOP_LEGACY, gl::Version(2, 1));
3209         contextsToTry.emplace_back(kPlatformOpenGL, Type::DESKTOP_LEGACY, gl::Version(2, 0));
3210         contextsToTry.emplace_back(kPlatformOpenGL, Type::DESKTOP_LEGACY, gl::Version(1, 5));
3211         contextsToTry.emplace_back(kPlatformOpenGL, Type::DESKTOP_LEGACY, gl::Version(1, 4));
3212         contextsToTry.emplace_back(kPlatformOpenGL, Type::DESKTOP_LEGACY, gl::Version(1, 3));
3213         contextsToTry.emplace_back(kPlatformOpenGL, Type::DESKTOP_LEGACY, gl::Version(1, 2));
3214         contextsToTry.emplace_back(kPlatformOpenGL, Type::DESKTOP_LEGACY, gl::Version(1, 1));
3215         contextsToTry.emplace_back(kPlatformOpenGL, Type::DESKTOP_LEGACY, gl::Version(1, 0));
3216     }
3217 
3218     if (requestedType == EGL_PLATFORM_ANGLE_TYPE_DEFAULT_ANGLE ||
3219         requestedType == kPlatformOpenGLES)
3220     {
3221         contextsToTry.emplace_back(kPlatformOpenGLES, Type::ES, gl::Version(3, 2));
3222         contextsToTry.emplace_back(kPlatformOpenGLES, Type::ES, gl::Version(3, 1));
3223         contextsToTry.emplace_back(kPlatformOpenGLES, Type::ES, gl::Version(3, 0));
3224         contextsToTry.emplace_back(kPlatformOpenGLES, Type::ES, gl::Version(2, 0));
3225     }
3226 
3227     return contextsToTry;
3228 }
3229 
GetRendererString(const FunctionsGL * functions)3230 std::string GetRendererString(const FunctionsGL *functions)
3231 {
3232     return GetString(functions, GL_RENDERER);
3233 }
3234 
GetVendorString(const FunctionsGL * functions)3235 std::string GetVendorString(const FunctionsGL *functions)
3236 {
3237     return GetString(functions, GL_VENDOR);
3238 }
3239 
GetVersionString(const FunctionsGL * functions)3240 std::string GetVersionString(const FunctionsGL *functions)
3241 {
3242     return GetString(functions, GL_VERSION);
3243 }
3244 
3245 }  // namespace rx
3246