blob: d09b311f4f4b39ae39b9088c252cbabd0472beeb [file] [log] [blame]
Jamie Madillfa05f602015-05-07 13:47:11 -04001//
2// Copyright 2015 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
Corentin Wallezd3970de2015-05-14 11:07:48 -04007#include "test_utils/ANGLETest.h"
Geoff Lang496123f2014-02-12 11:33:51 -05008
Jamie Madillfa05f602015-05-07 13:47:11 -04009using namespace angle;
Austin Kinross18b931d2014-09-29 12:58:31 -070010
Geoff Lang496123f2014-02-12 11:33:51 -050011class BlendMinMaxTest : public ANGLETest
12{
Jamie Madillfa05f602015-05-07 13:47:11 -040013 protected:
14 BlendMinMaxTest()
Geoff Lang496123f2014-02-12 11:33:51 -050015 {
16 setWindowWidth(128);
17 setWindowHeight(128);
18 setConfigRedBits(8);
19 setConfigGreenBits(8);
20 setConfigBlueBits(8);
21 setConfigAlphaBits(8);
22 setConfigDepthBits(24);
23
24 mProgram = 0;
25 mColorLocation = -1;
26 }
27
28 struct Color
29 {
30 float values[4];
31 };
32
33 static GLubyte getExpected(bool blendMin, float curColor, GLubyte prevColor)
34 {
Minmin Gong794e0002015-04-07 18:31:54 -070035 GLubyte curAsUbyte = static_cast<GLubyte>((curColor * std::numeric_limits<GLubyte>::max()) + 0.5f);
Geoff Lang496123f2014-02-12 11:33:51 -050036 return blendMin ? std::min<GLubyte>(curAsUbyte, prevColor) : std::max<GLubyte>(curAsUbyte, prevColor);
37 }
38
Geoff Langf34d1db2015-05-20 14:10:46 -040039 void runTest(GLenum colorFormat)
Geoff Lang496123f2014-02-12 11:33:51 -050040 {
Geoff Langa8091652015-04-27 10:53:55 -040041 if (getClientVersion() < 3 && !extensionEnabled("GL_EXT_blend_minmax"))
42 {
43 std::cout << "Test skipped because ES3 or GL_EXT_blend_minmax is not available." << std::endl;
44 return;
45 }
46
Geoff Langddf4d392015-09-29 13:00:14 -040047 // TODO(geofflang): figure out why this fails
48 if (isIntel() && GetParam() == ES2_OPENGL())
49 {
50 std::cout << "Test skipped on OpenGL Intel due to flakyness." << std::endl;
51 return;
52 }
53
Geoff Langf34d1db2015-05-20 14:10:46 -040054 SetUpFramebuffer(colorFormat);
55
Geoff Lang496123f2014-02-12 11:33:51 -050056 const size_t colorCount = 1024;
57 Color colors[colorCount];
58 for (size_t i = 0; i < colorCount; i++)
59 {
60 for (size_t j = 0; j < 4; j++)
61 {
62 colors[i].values[j] = (rand() % 255) / 255.0f;
63 }
64 }
65
66 GLubyte prevColor[4];
67 for (size_t i = 0; i < colorCount; i++)
68 {
69 const Color &color = colors[i];
70 glUseProgram(mProgram);
71 glUniform4f(mColorLocation, color.values[0], color.values[1], color.values[2], color.values[3]);
72
73 bool blendMin = (rand() % 2 == 0);
74 glBlendEquation(blendMin ? GL_MIN : GL_MAX);
75
76 drawQuad(mProgram, "aPosition", 0.5f);
77
78 if (i > 0)
79 {
80 EXPECT_PIXEL_EQ(0, 0,
81 getExpected(blendMin, color.values[0], prevColor[0]),
82 getExpected(blendMin, color.values[1], prevColor[1]),
83 getExpected(blendMin, color.values[2], prevColor[2]),
84 getExpected(blendMin, color.values[3], prevColor[3]));
85 }
86
87 glReadPixels(0, 0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, prevColor);
88 }
89 }
90
91 virtual void SetUp()
92 {
93 ANGLETest::SetUp();
94
95 const std::string testVertexShaderSource = SHADER_SOURCE
96 (
97 attribute highp vec4 aPosition;
98
99 void main(void)
100 {
101 gl_Position = aPosition;
102 }
103 );
104
105 const std::string testFragmentShaderSource = SHADER_SOURCE
106 (
107 uniform highp vec4 color;
108 void main(void)
109 {
110 gl_FragColor = color;
111 }
112 );
113
Jamie Madill5599c8f2014-08-26 13:16:39 -0400114 mProgram = CompileProgram(testVertexShaderSource, testFragmentShaderSource);
Geoff Lang496123f2014-02-12 11:33:51 -0500115 if (mProgram == 0)
116 {
117 FAIL() << "shader compilation failed.";
118 }
119
120 mColorLocation = glGetUniformLocation(mProgram, "color");
121
122 glUseProgram(mProgram);
123
124 glClearColor(0, 0, 0, 0);
125 glClearDepthf(0.0);
126 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
127
128 glEnable(GL_BLEND);
129 glDisable(GL_DEPTH_TEST);
130 }
131
132 void SetUpFramebuffer(GLenum colorFormat)
133 {
134 glGenFramebuffers(1, &mFramebuffer);
135 glBindFramebuffer(GL_DRAW_FRAMEBUFFER, mFramebuffer);
136 glBindFramebuffer(GL_READ_FRAMEBUFFER, mFramebuffer);
137
138 glGenRenderbuffers(1, &mColorRenderbuffer);
139 glBindRenderbuffer(GL_RENDERBUFFER, mColorRenderbuffer);
140 glRenderbufferStorage(GL_RENDERBUFFER, colorFormat, getWindowWidth(), getWindowHeight());
141 glFramebufferRenderbuffer(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, mColorRenderbuffer);
142
143 ASSERT_GL_NO_ERROR();
144 }
145
146 virtual void TearDown()
147 {
148 glDeleteProgram(mProgram);
149 glDeleteFramebuffers(1, &mFramebuffer);
150 glDeleteRenderbuffers(1, &mColorRenderbuffer);
151
152 ANGLETest::TearDown();
153 }
154
155 GLuint mProgram;
156 GLint mColorLocation;
157
158 GLuint mFramebuffer;
159 GLuint mColorRenderbuffer;
160};
161
Jamie Madillfa05f602015-05-07 13:47:11 -0400162TEST_P(BlendMinMaxTest, RGBA8)
Geoff Lang496123f2014-02-12 11:33:51 -0500163{
Geoff Langf34d1db2015-05-20 14:10:46 -0400164 runTest(GL_RGBA8);
Geoff Lang496123f2014-02-12 11:33:51 -0500165}
166
Jamie Madillfa05f602015-05-07 13:47:11 -0400167TEST_P(BlendMinMaxTest, RGBA32f)
Geoff Lang496123f2014-02-12 11:33:51 -0500168{
Geoff Langa8091652015-04-27 10:53:55 -0400169 if (getClientVersion() < 3 && !extensionEnabled("GL_OES_texture_float"))
170 {
171 std::cout << "Test skipped because ES3 or GL_OES_texture_float is not available." << std::endl;
172 return;
173 }
174
Geoff Langf34d1db2015-05-20 14:10:46 -0400175 runTest(GL_RGBA32F);
Geoff Lang496123f2014-02-12 11:33:51 -0500176}
177
Jamie Madillfa05f602015-05-07 13:47:11 -0400178TEST_P(BlendMinMaxTest, RGBA16F)
Geoff Lang496123f2014-02-12 11:33:51 -0500179{
Geoff Langa8091652015-04-27 10:53:55 -0400180 if (getClientVersion() < 3 && !extensionEnabled("GL_OES_texture_half_float"))
181 {
182 std::cout << "Test skipped because ES3 or GL_OES_texture_half_float is not available." << std::endl;
183 return;
184 }
185
Jamie Madill0962fc32015-09-09 10:40:28 -0400186 // TODO(jmadill): figure out why this fails
187 if (isIntel() && GetParam() == ES2_D3D11())
188 {
189 std::cout << "Test skipped on Intel due to failures." << std::endl;
190 return;
191 }
192
Geoff Langf34d1db2015-05-20 14:10:46 -0400193 runTest(GL_RGBA16F);
Geoff Lang496123f2014-02-12 11:33:51 -0500194}
Jamie Madillfa05f602015-05-07 13:47:11 -0400195
196// Use this to select which configurations (e.g. which renderer, which GLES major version) these tests should be run against.
197ANGLE_INSTANTIATE_TEST(BlendMinMaxTest, ES2_D3D9(), ES2_D3D11(), ES2_OPENGL());