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