1 //
2 // Copyright 2016 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 // ColorMaskTest.cpp: Test GLES functionality related to color masks, particularly an AMD D3D9
8 // driver bug.
9 
10 #include "test_utils/ANGLETest.h"
11 
12 namespace angle
13 {
14 class ColorMaskTest : public ANGLETest
15 {
16   protected:
ColorMaskTest()17     ColorMaskTest() : mProgram(0)
18     {
19         setWindowWidth(128);
20         setWindowHeight(128);
21         setConfigRedBits(8);
22         setConfigGreenBits(8);
23         setConfigBlueBits(8);
24         setConfigAlphaBits(8);
25         setConfigDepthBits(24);
26     }
27 
SetUp()28     void SetUp() override
29     {
30         ANGLETest::SetUp();
31 
32         const std::string vsSource =
33             "precision highp float;\n"
34             "attribute vec4 position;\n"
35             "\n"
36             "void main()\n"
37             "{\n"
38             "    gl_Position = position;\n"
39             "}\n";
40 
41         const std::string fsSource =
42             "precision highp float;\n"
43             "uniform vec4 color;\n"
44             "\n"
45             "void main()\n"
46             "{\n"
47             "    gl_FragColor = color;\n"
48             "}\n";
49 
50         mProgram = CompileProgram(vsSource, fsSource);
51         ASSERT_NE(0u, mProgram) << "shader compilation failed.";
52 
53         mColorUniform = glGetUniformLocation(mProgram, "color");
54     }
55 
TearDown()56     void TearDown() override
57     {
58         glDeleteProgram(mProgram);
59 
60         ANGLETest::TearDown();
61     }
62 
63     GLuint mProgram     = 0;
64     GLint mColorUniform = -1;
65 };
66 
67 // Some ATI cards have a bug where a draw with a zero color write mask can cause later draws to have
68 // incorrect results. Test to make sure this bug is not exposed.
TEST_P(ColorMaskTest,AMDZeroColorMaskBug)69 TEST_P(ColorMaskTest, AMDZeroColorMaskBug)
70 {
71     int x = getWindowWidth() / 2;
72     int y = getWindowHeight() / 2;
73 
74     // Clear to blue
75     glClearColor(0.0f, 0.0f, 1.0f, 1.0f);
76     glClear(GL_COLOR_BUFFER_BIT);
77     EXPECT_PIXEL_EQ(x, y, 0, 0, 255, 255);
78 
79     // Draw a quad with all colors masked and blending disabled, should remain blue
80     glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
81     glDisable(GL_BLEND);
82     glUseProgram(mProgram);
83     glUniform4f(mColorUniform, 1.0f, 0.0f, 0.0f, 0.0f);
84     EXPECT_GL_NO_ERROR();
85     drawQuad(mProgram, "position", 0.5f);
86     EXPECT_PIXEL_EQ(x, y, 0, 0, 255, 255);
87 
88     // Re-enable the color mask, should be red (with blend disabled, the red should overwrite
89     // everything)
90     glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
91     glUseProgram(mProgram);
92     glUniform4f(mColorUniform, 1.0f, 0.0f, 0.0f, 0.0f);
93     EXPECT_GL_NO_ERROR();
94     drawQuad(mProgram, "position", 0.5f);
95     EXPECT_PIXEL_EQ(x, y, 255, 0, 0, 0);
96 }
97 
98 // Use this to select which configurations (e.g. which renderer, which GLES major version) these
99 // tests should be run against. D3D11 Feature Level 9_3 uses different D3D formats for vertex
100 // attribs compared to Feature Levels 10_0+, so we should test them separately.
101 ANGLE_INSTANTIATE_TEST(ColorMaskTest,
102                        ES2_D3D9(),
103                        ES2_D3D11(),
104                        ES2_D3D11_FL9_3(),
105                        ES2_OPENGL(),
106                        ES3_OPENGL(),
107                        ES2_OPENGLES(),
108                        ES3_OPENGLES());
109 
110 }  // namespace angle
111