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 ")

Change-Id: I68cefbe8a9f69c5a9ebcb30027a05581e3024add
Reviewed-on: https://chromium-review.googlesource.com/302703
Reviewed-by: Corentin Wallez <cwallez@chromium.org>
Tested-by: Jinyoung Hur <hurims@gmail.com>
Commit-Queue: Jinyoung Hur <hurims@gmail.com>
Tryjob-Request: 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