Re-land: Don't expose non-conformant multisampling modes on GL

Re-landing with a fallback for failed internal format queries to work
around issue seen on Shield TV. Also fixed wrong handling of integer
RG formats in isRequiredRenderbufferFormat.

Some NVIDIA GL drivers expose non-conformant multisampling modes. The
conformance of multisampling modes can be queried using the extension
NV_internalformat_sample_query. Use it to filter out the
non-conformant modes from the modes that are exposed by ANGLE.

The MAX_SAMPLES value and other similar values stored in caps also
need to be lowered to match the maximum number of samples exposed
for required formats.

There seems to be an NVIDIA driver bug related to querying
STENCIL_INDEX8 multisample format. Work around this by querying
DEPTH24_STENCIL8 instead.

There's also some confusion around whether RGB9_E5 should be
renderable. Once the floating point texture extensions got rolled
into the core GL spec, it was eventually made clear that RGB9_E5
is intended not to be renderable. The extension specs that predate
float textures in the core spec do suggest that it would be
renderable, but in practice drivers that advertise the extension
strings don't reliably implement RGB9_E5 as renderable. Solve this
by disabling it as a renderable format and adding an explanatory
comment.

BUG=chromium:682815
TEST=angle_end2end_tests,
     dEQP-GLES31.functional.state_query.internal_format.renderbuffer.*

Change-Id: I727f03045a1534d6764b571e6d839243705d25b3
Reviewed-on: https://chromium-review.googlesource.com/551957
Reviewed-by: Jamie Madill <jmadill@chromium.org>
Commit-Queue: Jamie Madill <jmadill@chromium.org>
diff --git a/src/libANGLE/formatutils.cpp b/src/libANGLE/formatutils.cpp
index 008f581..53eabe4 100644
--- a/src/libANGLE/formatutils.cpp
+++ b/src/libANGLE/formatutils.cpp
@@ -327,6 +327,110 @@
     }
 }
 
+bool InternalFormat::isRequiredRenderbufferFormat(const Version &version) const
+{
+    // GLES 3.0.5 section 4.4.2.2:
+    // "Implementations are required to support the same internal formats for renderbuffers as the
+    // required formats for textures enumerated in section 3.8.3.1, with the exception of the color
+    // formats labelled "texture-only"."
+    if (!sized || compressed)
+    {
+        return false;
+    }
+
+    // Luma formats.
+    if (isLUMA())
+    {
+        return false;
+    }
+
+    // Depth/stencil formats.
+    if (depthBits > 0 || stencilBits > 0)
+    {
+        // GLES 2.0.25 table 4.5.
+        // GLES 3.0.5 section 3.8.3.1.
+        // GLES 3.1 table 8.14.
+
+        // Required formats in all versions.
+        switch (internalFormat)
+        {
+            case GL_DEPTH_COMPONENT16:
+            case GL_STENCIL_INDEX8:
+                // Note that STENCIL_INDEX8 is not mentioned in GLES 3.0.5 section 3.8.3.1, but it
+                // is in section 4.4.2.2.
+                return true;
+            default:
+                break;
+        }
+        if (version.major < 3)
+        {
+            return false;
+        }
+        // Required formats in GLES 3.0 and up.
+        switch (internalFormat)
+        {
+            case GL_DEPTH_COMPONENT32F:
+            case GL_DEPTH_COMPONENT24:
+            case GL_DEPTH32F_STENCIL8:
+            case GL_DEPTH24_STENCIL8:
+                return true;
+            default:
+                return false;
+        }
+    }
+
+    // RGBA formats.
+    // GLES 2.0.25 table 4.5.
+    // GLES 3.0.5 section 3.8.3.1.
+    // GLES 3.1 table 8.13.
+
+    // Required formats in all versions.
+    switch (internalFormat)
+    {
+        case GL_RGBA4:
+        case GL_RGB5_A1:
+        case GL_RGB565:
+            return true;
+        default:
+            break;
+    }
+    if (version.major < 3)
+    {
+        return false;
+    }
+
+    if (format == GL_BGRA_EXT)
+    {
+        return false;
+    }
+
+    switch (componentType)
+    {
+        case GL_SIGNED_NORMALIZED:
+        case GL_FLOAT:
+            return false;
+        case GL_UNSIGNED_INT:
+        case GL_INT:
+            // Integer RGB formats are not required renderbuffer formats.
+            if (alphaBits == 0 && blueBits != 0)
+            {
+                return false;
+            }
+            // All integer R and RG formats are required.
+            // Integer RGBA formats including RGB10_A2_UI are required.
+            return true;
+        case GL_UNSIGNED_NORMALIZED:
+            if (internalFormat == GL_SRGB8)
+            {
+                return false;
+            }
+            return true;
+        default:
+            UNREACHABLE();
+            return false;
+    }
+}
+
 Format::Format(GLenum internalFormat) : Format(GetSizedInternalFormatInfo(internalFormat))
 {
 }