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/Context.cpp b/src/libANGLE/Context.cpp
index ecf85b9..bd9508a 100644
--- a/src/libANGLE/Context.cpp
+++ b/src/libANGLE/Context.cpp
@@ -2727,12 +2727,58 @@
 
         // OpenGL ES does not support multisampling with non-rendererable formats
         // OpenGL ES 3.0 or prior does not support multisampling with integer formats
-        if (!formatInfo.renderSupport ||
+        if (!formatCaps.renderable ||
             (getClientVersion() < ES_3_1 &&
              (formatInfo.componentType == GL_INT || formatInfo.componentType == GL_UNSIGNED_INT)))
         {
             formatCaps.sampleCounts.clear();
         }
+        else
+        {
+            // We may have limited the max samples for some required renderbuffer formats due to
+            // non-conformant formats. In this case MAX_SAMPLES needs to be lowered accordingly.
+            GLuint formatMaxSamples = formatCaps.getMaxSamples();
+
+            // GLES 3.0.5 section 4.4.2.2: "Implementations must support creation of renderbuffers
+            // in these required formats with up to the value of MAX_SAMPLES multisamples, with the
+            // exception of signed and unsigned integer formats."
+            if (formatInfo.componentType != GL_INT && formatInfo.componentType != GL_UNSIGNED_INT &&
+                formatInfo.isRequiredRenderbufferFormat(getClientVersion()))
+            {
+                ASSERT(getClientVersion() < ES_3_0 || formatMaxSamples >= 4);
+                mCaps.maxSamples = std::min(mCaps.maxSamples, formatMaxSamples);
+            }
+
+            // Handle GLES 3.1 MAX_*_SAMPLES values similarly to MAX_SAMPLES.
+            if (getClientVersion() >= ES_3_1)
+            {
+                // GLES 3.1 section 9.2.5: "Implementations must support creation of renderbuffers
+                // in these required formats with up to the value of MAX_SAMPLES multisamples, with
+                // the exception that the signed and unsigned integer formats are required only to
+                // support creation of renderbuffers with up to the value of MAX_INTEGER_SAMPLES
+                // multisamples, which must be at least one."
+                if (formatInfo.componentType == GL_INT ||
+                    formatInfo.componentType == GL_UNSIGNED_INT)
+                {
+                    mCaps.maxIntegerSamples = std::min(mCaps.maxIntegerSamples, formatMaxSamples);
+                }
+
+                // GLES 3.1 section 19.3.1.
+                if (formatCaps.texturable)
+                {
+                    if (formatInfo.depthBits > 0)
+                    {
+                        mCaps.maxDepthTextureSamples =
+                            std::min(mCaps.maxDepthTextureSamples, formatMaxSamples);
+                    }
+                    else if (formatInfo.redBits > 0)
+                    {
+                        mCaps.maxColorTextureSamples =
+                            std::min(mCaps.maxColorTextureSamples, formatMaxSamples);
+                    }
+                }
+            }
+        }
 
         if (formatCaps.texturable && formatInfo.compressed)
         {