Re-land "Only require that the stencil masks are same as many effective bits set"

It seems reasonable to validate the equality between stencilWriteMask
and stencilBackWriteMask only by comparing the effective bits.
An existing dEQP test[1] and a WebGL test[2] also use the max size of
stencil bits for checking the stencil mask values.

[1] StencilWriteMaskSeparateTestCase (gles3/functional/
es3fIntegerStateQueryTests.cpp)
[2] https://www.khronos.org/registry/webgl/sdk/tests/conformance/state/
gl-get-calls.html (Search for "minimumRequiredStencilMask ")

Re-land with fix for Clang warning.

Change-Id: I4cadaffced04ca55fa6daf7ac6a462368e0cfeec
Reviewed-on: https://chromium-review.googlesource.com/307530
Reviewed-by: Jinyoung Hur <hurims@gmail.com>
Reviewed-by: Geoff Lang <geofflang@chromium.org>
Tested-by: Jamie Madill <jmadill@chromium.org>
diff --git a/src/libANGLE/validationES.cpp b/src/libANGLE/validationES.cpp
index 7ee2183..8a59c06 100644
--- a/src/libANGLE/validationES.cpp
+++ b/src/libANGLE/validationES.cpp
@@ -1500,10 +1500,16 @@
 
     if (context->getLimitations().noSeparateStencilRefsAndMasks)
     {
-        const gl::DepthStencilState &depthStencilState = state.getDepthStencilState();
-        if (depthStencilState.stencilWritemask != depthStencilState.stencilBackWritemask ||
+        const Framebuffer *framebuffer             = context->getState().getDrawFramebuffer();
+        const FramebufferAttachment *stencilBuffer = framebuffer->getStencilbuffer();
+        GLuint stencilBits                         = stencilBuffer ? stencilBuffer->getStencilSize() : 0;
+        GLuint minimumRequiredStencilMask          = (1 << stencilBits) - 1;
+        const DepthStencilState &depthStencilState = state.getDepthStencilState();
+        if ((depthStencilState.stencilWritemask & minimumRequiredStencilMask) !=
+                (depthStencilState.stencilBackWritemask & minimumRequiredStencilMask) ||
             state.getStencilRef() != state.getStencilBackRef() ||
-            depthStencilState.stencilMask != depthStencilState.stencilBackMask)
+            (depthStencilState.stencilMask & minimumRequiredStencilMask) !=
+                (depthStencilState.stencilBackMask & minimumRequiredStencilMask))
         {
             // Note: these separate values are not supported in WebGL, due to D3D's limitations. See
             // Section 6.10 of the WebGL 1.0 spec