Jamie Madill | fa05f60 | 2015-05-07 13:47:11 -0400 | [diff] [blame] | 1 | // |
| 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 Wallez | d3970de | 2015-05-14 11:07:48 -0400 | [diff] [blame] | 7 | #include "test_utils/ANGLETest.h" |
Geoff Lang | 496123f | 2014-02-12 11:33:51 -0500 | [diff] [blame] | 8 | |
Jamie Madill | fa05f60 | 2015-05-07 13:47:11 -0400 | [diff] [blame] | 9 | using namespace angle; |
Austin Kinross | 18b931d | 2014-09-29 12:58:31 -0700 | [diff] [blame] | 10 | |
Geoff Lang | 496123f | 2014-02-12 11:33:51 -0500 | [diff] [blame] | 11 | class BlendMinMaxTest : public ANGLETest |
| 12 | { |
Jamie Madill | fa05f60 | 2015-05-07 13:47:11 -0400 | [diff] [blame] | 13 | protected: |
| 14 | BlendMinMaxTest() |
Geoff Lang | 496123f | 2014-02-12 11:33:51 -0500 | [diff] [blame] | 15 | { |
| 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; |
Sami Väisänen | c4433f4 | 2016-07-12 16:56:43 +0300 | [diff] [blame] | 26 | mFramebuffer = 0; |
| 27 | mColorRenderbuffer = 0; |
Geoff Lang | 496123f | 2014-02-12 11:33:51 -0500 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | struct Color |
| 31 | { |
| 32 | float values[4]; |
| 33 | }; |
| 34 | |
Geoff Lang | f607c60 | 2016-09-21 11:46:48 -0400 | [diff] [blame^] | 35 | static float getExpected(bool blendMin, float curColor, float prevColor) |
Geoff Lang | 496123f | 2014-02-12 11:33:51 -0500 | [diff] [blame] | 36 | { |
Geoff Lang | f607c60 | 2016-09-21 11:46:48 -0400 | [diff] [blame^] | 37 | return blendMin ? std::min(curColor, prevColor) : std::max(curColor, prevColor); |
Geoff Lang | 496123f | 2014-02-12 11:33:51 -0500 | [diff] [blame] | 38 | } |
| 39 | |
Geoff Lang | f607c60 | 2016-09-21 11:46:48 -0400 | [diff] [blame^] | 40 | void runTest(GLenum colorFormat, GLenum type) |
Geoff Lang | 496123f | 2014-02-12 11:33:51 -0500 | [diff] [blame] | 41 | { |
Martin Radev | 1be913c | 2016-07-11 17:59:16 +0300 | [diff] [blame] | 42 | if (getClientMajorVersion() < 3 && !extensionEnabled("GL_EXT_blend_minmax")) |
Geoff Lang | a809165 | 2015-04-27 10:53:55 -0400 | [diff] [blame] | 43 | { |
| 44 | std::cout << "Test skipped because ES3 or GL_EXT_blend_minmax is not available." << std::endl; |
| 45 | return; |
| 46 | } |
| 47 | |
Geoff Lang | ddf4d39 | 2015-09-29 13:00:14 -0400 | [diff] [blame] | 48 | // TODO(geofflang): figure out why this fails |
Jamie Madill | 518b9fa | 2016-03-02 11:26:02 -0500 | [diff] [blame] | 49 | if (IsIntel() && GetParam() == ES2_OPENGL()) |
Geoff Lang | ddf4d39 | 2015-09-29 13:00:14 -0400 | [diff] [blame] | 50 | { |
| 51 | std::cout << "Test skipped on OpenGL Intel due to flakyness." << std::endl; |
| 52 | return; |
| 53 | } |
| 54 | |
Geoff Lang | f34d1db | 2015-05-20 14:10:46 -0400 | [diff] [blame] | 55 | SetUpFramebuffer(colorFormat); |
| 56 | |
Geoff Lang | f607c60 | 2016-09-21 11:46:48 -0400 | [diff] [blame^] | 57 | int minValue = 0; |
| 58 | int maxValue = 1; |
| 59 | if (type == GL_FLOAT) |
| 60 | { |
| 61 | minValue = -1024; |
| 62 | maxValue = 1024; |
| 63 | } |
| 64 | |
| 65 | const size_t colorCount = 128; |
Geoff Lang | 496123f | 2014-02-12 11:33:51 -0500 | [diff] [blame] | 66 | Color colors[colorCount]; |
| 67 | for (size_t i = 0; i < colorCount; i++) |
| 68 | { |
| 69 | for (size_t j = 0; j < 4; j++) |
| 70 | { |
Geoff Lang | f607c60 | 2016-09-21 11:46:48 -0400 | [diff] [blame^] | 71 | colors[i].values[j] = |
| 72 | static_cast<float>(minValue + (rand() % (maxValue - minValue))); |
Geoff Lang | 496123f | 2014-02-12 11:33:51 -0500 | [diff] [blame] | 73 | } |
| 74 | } |
| 75 | |
Geoff Lang | f607c60 | 2016-09-21 11:46:48 -0400 | [diff] [blame^] | 76 | float prevColor[4]; |
Geoff Lang | 496123f | 2014-02-12 11:33:51 -0500 | [diff] [blame] | 77 | for (size_t i = 0; i < colorCount; i++) |
| 78 | { |
| 79 | const Color &color = colors[i]; |
| 80 | glUseProgram(mProgram); |
| 81 | glUniform4f(mColorLocation, color.values[0], color.values[1], color.values[2], color.values[3]); |
| 82 | |
| 83 | bool blendMin = (rand() % 2 == 0); |
| 84 | glBlendEquation(blendMin ? GL_MIN : GL_MAX); |
| 85 | |
| 86 | drawQuad(mProgram, "aPosition", 0.5f); |
| 87 | |
Geoff Lang | f607c60 | 2016-09-21 11:46:48 -0400 | [diff] [blame^] | 88 | float pixel[4]; |
| 89 | if (type == GL_UNSIGNED_BYTE) |
Geoff Lang | 496123f | 2014-02-12 11:33:51 -0500 | [diff] [blame] | 90 | { |
Geoff Lang | f607c60 | 2016-09-21 11:46:48 -0400 | [diff] [blame^] | 91 | GLubyte ubytePixel[4]; |
| 92 | glReadPixels(0, 0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, ubytePixel); |
| 93 | for (size_t componentIdx = 0; componentIdx < ArraySize(pixel); componentIdx++) |
| 94 | { |
| 95 | pixel[componentIdx] = ubytePixel[componentIdx] / 255.0f; |
| 96 | } |
| 97 | } |
| 98 | else if (type == GL_FLOAT) |
| 99 | { |
| 100 | glReadPixels(0, 0, 1, 1, GL_RGBA, GL_FLOAT, pixel); |
| 101 | } |
| 102 | else |
| 103 | { |
| 104 | FAIL() << "Unexpected pixel type"; |
Geoff Lang | 496123f | 2014-02-12 11:33:51 -0500 | [diff] [blame] | 105 | } |
| 106 | |
Geoff Lang | f607c60 | 2016-09-21 11:46:48 -0400 | [diff] [blame^] | 107 | if (i > 0) |
| 108 | { |
| 109 | const float errorRange = 1.0f / 255.0f; |
| 110 | for (size_t componentIdx = 0; componentIdx < ArraySize(pixel); componentIdx++) |
| 111 | { |
| 112 | EXPECT_NEAR( |
| 113 | getExpected(blendMin, color.values[componentIdx], prevColor[componentIdx]), |
| 114 | pixel[componentIdx], errorRange); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | memcpy(prevColor, pixel, sizeof(pixel)); |
Geoff Lang | 496123f | 2014-02-12 11:33:51 -0500 | [diff] [blame] | 119 | } |
| 120 | } |
| 121 | |
| 122 | virtual void SetUp() |
| 123 | { |
| 124 | ANGLETest::SetUp(); |
| 125 | |
| 126 | const std::string testVertexShaderSource = SHADER_SOURCE |
| 127 | ( |
| 128 | attribute highp vec4 aPosition; |
| 129 | |
| 130 | void main(void) |
| 131 | { |
| 132 | gl_Position = aPosition; |
| 133 | } |
| 134 | ); |
| 135 | |
| 136 | const std::string testFragmentShaderSource = SHADER_SOURCE |
| 137 | ( |
| 138 | uniform highp vec4 color; |
| 139 | void main(void) |
| 140 | { |
| 141 | gl_FragColor = color; |
| 142 | } |
| 143 | ); |
| 144 | |
Jamie Madill | 5599c8f | 2014-08-26 13:16:39 -0400 | [diff] [blame] | 145 | mProgram = CompileProgram(testVertexShaderSource, testFragmentShaderSource); |
Geoff Lang | 496123f | 2014-02-12 11:33:51 -0500 | [diff] [blame] | 146 | if (mProgram == 0) |
| 147 | { |
| 148 | FAIL() << "shader compilation failed."; |
| 149 | } |
| 150 | |
| 151 | mColorLocation = glGetUniformLocation(mProgram, "color"); |
| 152 | |
| 153 | glUseProgram(mProgram); |
| 154 | |
| 155 | glClearColor(0, 0, 0, 0); |
| 156 | glClearDepthf(0.0); |
| 157 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
| 158 | |
| 159 | glEnable(GL_BLEND); |
| 160 | glDisable(GL_DEPTH_TEST); |
| 161 | } |
| 162 | |
| 163 | void SetUpFramebuffer(GLenum colorFormat) |
| 164 | { |
| 165 | glGenFramebuffers(1, &mFramebuffer); |
| 166 | glBindFramebuffer(GL_DRAW_FRAMEBUFFER, mFramebuffer); |
| 167 | glBindFramebuffer(GL_READ_FRAMEBUFFER, mFramebuffer); |
| 168 | |
| 169 | glGenRenderbuffers(1, &mColorRenderbuffer); |
| 170 | glBindRenderbuffer(GL_RENDERBUFFER, mColorRenderbuffer); |
| 171 | glRenderbufferStorage(GL_RENDERBUFFER, colorFormat, getWindowWidth(), getWindowHeight()); |
| 172 | glFramebufferRenderbuffer(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, mColorRenderbuffer); |
| 173 | |
Geoff Lang | f607c60 | 2016-09-21 11:46:48 -0400 | [diff] [blame^] | 174 | glClearColor(0.0f, 0.0f, 0.0f, 0.0f); |
| 175 | glClear(GL_COLOR_BUFFER_BIT); |
| 176 | |
Geoff Lang | 496123f | 2014-02-12 11:33:51 -0500 | [diff] [blame] | 177 | ASSERT_GL_NO_ERROR(); |
| 178 | } |
| 179 | |
| 180 | virtual void TearDown() |
| 181 | { |
| 182 | glDeleteProgram(mProgram); |
| 183 | glDeleteFramebuffers(1, &mFramebuffer); |
| 184 | glDeleteRenderbuffers(1, &mColorRenderbuffer); |
| 185 | |
| 186 | ANGLETest::TearDown(); |
| 187 | } |
| 188 | |
| 189 | GLuint mProgram; |
| 190 | GLint mColorLocation; |
| 191 | |
| 192 | GLuint mFramebuffer; |
| 193 | GLuint mColorRenderbuffer; |
| 194 | }; |
| 195 | |
Jamie Madill | fa05f60 | 2015-05-07 13:47:11 -0400 | [diff] [blame] | 196 | TEST_P(BlendMinMaxTest, RGBA8) |
Geoff Lang | 496123f | 2014-02-12 11:33:51 -0500 | [diff] [blame] | 197 | { |
Geoff Lang | f607c60 | 2016-09-21 11:46:48 -0400 | [diff] [blame^] | 198 | runTest(GL_RGBA8, GL_UNSIGNED_BYTE); |
Geoff Lang | 496123f | 2014-02-12 11:33:51 -0500 | [diff] [blame] | 199 | } |
| 200 | |
Geoff Lang | f607c60 | 2016-09-21 11:46:48 -0400 | [diff] [blame^] | 201 | TEST_P(BlendMinMaxTest, RGBA32F) |
Geoff Lang | 496123f | 2014-02-12 11:33:51 -0500 | [diff] [blame] | 202 | { |
Martin Radev | 1be913c | 2016-07-11 17:59:16 +0300 | [diff] [blame] | 203 | if (getClientMajorVersion() < 3 || !extensionEnabled("GL_EXT_color_buffer_float")) |
Geoff Lang | a809165 | 2015-04-27 10:53:55 -0400 | [diff] [blame] | 204 | { |
Geoff Lang | e0cc2a4 | 2016-01-20 10:58:17 -0500 | [diff] [blame] | 205 | std::cout << "Test skipped because ES3 and GL_EXT_color_buffer_float are not available." |
| 206 | << std::endl; |
Geoff Lang | a809165 | 2015-04-27 10:53:55 -0400 | [diff] [blame] | 207 | return; |
| 208 | } |
| 209 | |
Jamie Madill | 1ea9aaa | 2015-10-07 11:13:55 -0400 | [diff] [blame] | 210 | // TODO(jmadill): Figure out why this is broken on Intel |
Jamie Madill | 518b9fa | 2016-03-02 11:26:02 -0500 | [diff] [blame] | 211 | if (IsIntel() && (GetParam() == ES2_D3D11() || GetParam() == ES2_D3D9())) |
Jamie Madill | 1ea9aaa | 2015-10-07 11:13:55 -0400 | [diff] [blame] | 212 | { |
| 213 | std::cout << "Test skipped on Intel OpenGL." << std::endl; |
| 214 | return; |
| 215 | } |
| 216 | |
Austin Kinross | d544cc9 | 2016-01-11 15:26:42 -0800 | [diff] [blame] | 217 | // TODO (bug 1284): Investigate RGBA32f D3D SDK Layers messages on D3D11_FL9_3 |
Jamie Madill | 518b9fa | 2016-03-02 11:26:02 -0500 | [diff] [blame] | 218 | if (IsD3D11_FL93()) |
Austin Kinross | d544cc9 | 2016-01-11 15:26:42 -0800 | [diff] [blame] | 219 | { |
| 220 | std::cout << "Test skipped on Feature Level 9_3." << std::endl; |
| 221 | return; |
| 222 | } |
| 223 | |
Geoff Lang | f607c60 | 2016-09-21 11:46:48 -0400 | [diff] [blame^] | 224 | runTest(GL_RGBA32F, GL_FLOAT); |
Geoff Lang | 496123f | 2014-02-12 11:33:51 -0500 | [diff] [blame] | 225 | } |
| 226 | |
Jamie Madill | fa05f60 | 2015-05-07 13:47:11 -0400 | [diff] [blame] | 227 | TEST_P(BlendMinMaxTest, RGBA16F) |
Geoff Lang | 496123f | 2014-02-12 11:33:51 -0500 | [diff] [blame] | 228 | { |
Martin Radev | 1be913c | 2016-07-11 17:59:16 +0300 | [diff] [blame] | 229 | if (getClientMajorVersion() < 3 && !extensionEnabled("GL_EXT_color_buffer_half_float")) |
Geoff Lang | a809165 | 2015-04-27 10:53:55 -0400 | [diff] [blame] | 230 | { |
Austin Kinross | ff318af | 2015-12-22 15:34:45 -0800 | [diff] [blame] | 231 | std::cout << "Test skipped because ES3 or GL_EXT_color_buffer_half_float is not available." |
| 232 | << std::endl; |
Geoff Lang | a809165 | 2015-04-27 10:53:55 -0400 | [diff] [blame] | 233 | return; |
| 234 | } |
| 235 | |
Jamie Madill | 0962fc3 | 2015-09-09 10:40:28 -0400 | [diff] [blame] | 236 | // TODO(jmadill): figure out why this fails |
Jamie Madill | 518b9fa | 2016-03-02 11:26:02 -0500 | [diff] [blame] | 237 | if (IsIntel() && (GetParam() == ES2_D3D11() || GetParam() == ES2_D3D9())) |
Jamie Madill | 0962fc3 | 2015-09-09 10:40:28 -0400 | [diff] [blame] | 238 | { |
| 239 | std::cout << "Test skipped on Intel due to failures." << std::endl; |
| 240 | return; |
| 241 | } |
| 242 | |
Geoff Lang | f607c60 | 2016-09-21 11:46:48 -0400 | [diff] [blame^] | 243 | runTest(GL_RGBA16F, GL_FLOAT); |
Geoff Lang | 496123f | 2014-02-12 11:33:51 -0500 | [diff] [blame] | 244 | } |
Jamie Madill | fa05f60 | 2015-05-07 13:47:11 -0400 | [diff] [blame] | 245 | |
| 246 | // Use this to select which configurations (e.g. which renderer, which GLES major version) these tests should be run against. |
Geoff Lang | e0cc2a4 | 2016-01-20 10:58:17 -0500 | [diff] [blame] | 247 | ANGLE_INSTANTIATE_TEST(BlendMinMaxTest, |
| 248 | ES2_D3D9(), |
| 249 | ES2_D3D11(), |
Geoff Lang | f607c60 | 2016-09-21 11:46:48 -0400 | [diff] [blame^] | 250 | ES3_D3D11(), |
Geoff Lang | e0cc2a4 | 2016-01-20 10:58:17 -0500 | [diff] [blame] | 251 | ES2_D3D11_FL9_3(), |
| 252 | ES2_OPENGL(), |
Geoff Lang | f607c60 | 2016-09-21 11:46:48 -0400 | [diff] [blame^] | 253 | ES3_OPENGL(), |
| 254 | ES2_OPENGLES(), |
| 255 | ES3_OPENGLES()); |