Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / angle / tests / angle_tests / BlendMinMaxTest.cpp
1 #include "ANGLETest.h"
2
3 // Use this to select which configurations (e.g. which renderer, which GLES major version) these tests should be run against.
4 typedef ::testing::Types<TFT<Gles::Two, Rend::D3D9>, TFT<Gles::Two, Rend::D3D11>> TestFixtureTypes;
5 TYPED_TEST_CASE(BlendMinMaxTest, TestFixtureTypes);
6
7 template<typename T>
8 class BlendMinMaxTest : public ANGLETest
9 {
10 protected:
11     BlendMinMaxTest() : ANGLETest(T::GetGlesMajorVersion(), T::GetRequestedRenderer())
12     {
13         setWindowWidth(128);
14         setWindowHeight(128);
15         setConfigRedBits(8);
16         setConfigGreenBits(8);
17         setConfigBlueBits(8);
18         setConfigAlphaBits(8);
19         setConfigDepthBits(24);
20
21         mProgram = 0;
22         mColorLocation = -1;
23     }
24
25     struct Color
26     {
27         float values[4];
28     };
29
30     static GLubyte getExpected(bool blendMin, float curColor, GLubyte prevColor)
31     {
32         GLubyte curAsUbyte = (curColor * std::numeric_limits<GLubyte>::max()) + 0.5f;
33         return blendMin ? std::min<GLubyte>(curAsUbyte, prevColor) : std::max<GLubyte>(curAsUbyte, prevColor);
34     }
35
36     void runTest()
37     {
38         const size_t colorCount = 1024;
39         Color colors[colorCount];
40         for (size_t i = 0; i < colorCount; i++)
41         {
42             for (size_t j = 0; j < 4; j++)
43             {
44                 colors[i].values[j] = (rand() % 255) / 255.0f;
45             }
46         }
47
48         GLubyte prevColor[4];
49         for (size_t i = 0; i < colorCount; i++)
50         {
51             const Color &color = colors[i];
52             glUseProgram(mProgram);
53             glUniform4f(mColorLocation, color.values[0], color.values[1], color.values[2], color.values[3]);
54
55             bool blendMin = (rand() % 2 == 0);
56             glBlendEquation(blendMin ? GL_MIN : GL_MAX);
57
58             drawQuad(mProgram, "aPosition", 0.5f);
59
60             if (i > 0)
61             {
62                 EXPECT_PIXEL_EQ(0, 0,
63                                 getExpected(blendMin, color.values[0], prevColor[0]),
64                                 getExpected(blendMin, color.values[1], prevColor[1]),
65                                 getExpected(blendMin, color.values[2], prevColor[2]),
66                                 getExpected(blendMin, color.values[3], prevColor[3]));
67             }
68
69             glReadPixels(0, 0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, prevColor);
70         }
71     }
72
73     virtual void SetUp()
74     {
75         ANGLETest::SetUp();
76
77         const std::string testVertexShaderSource = SHADER_SOURCE
78         (
79             attribute highp vec4 aPosition;
80
81             void main(void)
82             {
83                 gl_Position = aPosition;
84             }
85         );
86
87         const std::string testFragmentShaderSource = SHADER_SOURCE
88         (
89             uniform highp vec4 color;
90             void main(void)
91             {
92                 gl_FragColor = color;
93             }
94         );
95
96         mProgram = CompileProgram(testVertexShaderSource, testFragmentShaderSource);
97         if (mProgram == 0)
98         {
99             FAIL() << "shader compilation failed.";
100         }
101
102         mColorLocation = glGetUniformLocation(mProgram, "color");
103
104         glUseProgram(mProgram);
105
106         glClearColor(0, 0, 0, 0);
107         glClearDepthf(0.0);
108         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
109
110         glEnable(GL_BLEND);
111         glDisable(GL_DEPTH_TEST);
112     }
113
114     void SetUpFramebuffer(GLenum colorFormat)
115     {
116         glGenFramebuffers(1, &mFramebuffer);
117         glBindFramebuffer(GL_DRAW_FRAMEBUFFER, mFramebuffer);
118         glBindFramebuffer(GL_READ_FRAMEBUFFER, mFramebuffer);
119
120         glGenRenderbuffers(1, &mColorRenderbuffer);
121         glBindRenderbuffer(GL_RENDERBUFFER, mColorRenderbuffer);
122         glRenderbufferStorage(GL_RENDERBUFFER, colorFormat, getWindowWidth(), getWindowHeight());
123         glFramebufferRenderbuffer(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, mColorRenderbuffer);
124
125         ASSERT_GL_NO_ERROR();
126     }
127
128     virtual void TearDown()
129     {
130         glDeleteProgram(mProgram);
131         glDeleteFramebuffers(1, &mFramebuffer);
132         glDeleteRenderbuffers(1, &mColorRenderbuffer);
133
134         ANGLETest::TearDown();
135     }
136
137     GLuint mProgram;
138     GLint mColorLocation;
139
140     GLuint mFramebuffer;
141     GLuint mColorRenderbuffer;
142 };
143
144 TYPED_TEST(BlendMinMaxTest, RGBA8)
145 {
146     SetUpFramebuffer(GL_RGBA8);
147     runTest();
148 }
149
150 TYPED_TEST(BlendMinMaxTest, RGBA32f)
151 {
152     SetUpFramebuffer(GL_RGBA32F);
153     runTest();
154 }
155
156 TYPED_TEST(BlendMinMaxTest, RGBA16F)
157 {
158     SetUpFramebuffer(GL_RGBA16F);
159     runTest();
160 }