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 | d191317 | 2013-10-18 16:15:10 -0400 | [diff] [blame] | 8 | |
Jamie Madill | fa05f60 | 2015-05-07 13:47:11 -0400 | [diff] [blame] | 9 | using namespace angle; |
Jamie Madill | 94203b3 | 2014-10-02 10:44:16 -0400 | [diff] [blame] | 10 | |
Jamie Madill | 94203b3 | 2014-10-02 10:44:16 -0400 | [diff] [blame] | 11 | class ClearTestBase : public ANGLETest |
Geoff Lang | d191317 | 2013-10-18 16:15:10 -0400 | [diff] [blame] | 12 | { |
Jamie Madill | 94203b3 | 2014-10-02 10:44:16 -0400 | [diff] [blame] | 13 | protected: |
Jamie Madill | fa05f60 | 2015-05-07 13:47:11 -0400 | [diff] [blame] | 14 | ClearTestBase() |
Geoff Lang | d191317 | 2013-10-18 16:15:10 -0400 | [diff] [blame] | 15 | { |
| 16 | setWindowWidth(128); |
| 17 | setWindowHeight(128); |
Geoff Lang | efc551f | 2013-10-31 10:20:28 -0400 | [diff] [blame] | 18 | setConfigRedBits(8); |
| 19 | setConfigGreenBits(8); |
| 20 | setConfigBlueBits(8); |
| 21 | setConfigAlphaBits(8); |
| 22 | setConfigDepthBits(24); |
Geoff Lang | d191317 | 2013-10-18 16:15:10 -0400 | [diff] [blame] | 23 | } |
| 24 | |
| 25 | virtual void SetUp() |
| 26 | { |
| 27 | ANGLETest::SetUp(); |
| 28 | |
| 29 | const std::string vertexShaderSource = SHADER_SOURCE |
| 30 | ( |
| 31 | precision highp float; |
| 32 | attribute vec4 position; |
| 33 | |
| 34 | void main() |
| 35 | { |
| 36 | gl_Position = position; |
| 37 | } |
| 38 | ); |
| 39 | |
| 40 | const std::string fragmentShaderSource = SHADER_SOURCE |
| 41 | ( |
| 42 | precision highp float; |
| 43 | |
| 44 | void main() |
| 45 | { |
| 46 | gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0); |
| 47 | } |
| 48 | ); |
| 49 | |
Jamie Madill | 5599c8f | 2014-08-26 13:16:39 -0400 | [diff] [blame] | 50 | mProgram = CompileProgram(vertexShaderSource, fragmentShaderSource); |
Geoff Lang | d191317 | 2013-10-18 16:15:10 -0400 | [diff] [blame] | 51 | if (mProgram == 0) |
| 52 | { |
| 53 | FAIL() << "shader compilation failed."; |
| 54 | } |
Jamie Madill | a09403c | 2014-07-21 10:03:36 -0400 | [diff] [blame] | 55 | |
| 56 | glGenFramebuffers(1, &mFBO); |
| 57 | |
| 58 | ASSERT_GL_NO_ERROR(); |
Geoff Lang | d191317 | 2013-10-18 16:15:10 -0400 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | virtual void TearDown() |
| 62 | { |
| 63 | glDeleteProgram(mProgram); |
Jamie Madill | a09403c | 2014-07-21 10:03:36 -0400 | [diff] [blame] | 64 | glDeleteFramebuffers(1, &mFBO); |
Geoff Lang | d191317 | 2013-10-18 16:15:10 -0400 | [diff] [blame] | 65 | |
| 66 | ANGLETest::TearDown(); |
| 67 | } |
| 68 | |
| 69 | GLuint mProgram; |
Jamie Madill | a09403c | 2014-07-21 10:03:36 -0400 | [diff] [blame] | 70 | GLuint mFBO; |
Geoff Lang | d191317 | 2013-10-18 16:15:10 -0400 | [diff] [blame] | 71 | }; |
| 72 | |
Jamie Madill | fa05f60 | 2015-05-07 13:47:11 -0400 | [diff] [blame] | 73 | class ClearTest : public ClearTestBase {}; |
| 74 | class ClearTestES3 : public ClearTestBase {}; |
Jamie Madill | 94203b3 | 2014-10-02 10:44:16 -0400 | [diff] [blame] | 75 | |
Geoff Lang | afd7f0a | 2015-09-09 15:33:31 -0400 | [diff] [blame] | 76 | // Test clearing the default framebuffer |
| 77 | TEST_P(ClearTest, DefaultFramebuffer) |
| 78 | { |
| 79 | glClearColor(0.25f, 0.5f, 0.5f, 0.5f); |
| 80 | glClear(GL_COLOR_BUFFER_BIT); |
| 81 | EXPECT_PIXEL_NEAR(0, 0, 64, 128, 128, 128, 1.0); |
| 82 | } |
| 83 | |
| 84 | // Test clearing a RGBA8 Framebuffer |
| 85 | TEST_P(ClearTest, RGBA8Framebuffer) |
| 86 | { |
| 87 | glBindFramebuffer(GL_FRAMEBUFFER, mFBO); |
| 88 | |
| 89 | GLuint texture; |
| 90 | glGenTextures(1, &texture); |
| 91 | |
| 92 | glBindTexture(GL_TEXTURE_2D, texture); |
| 93 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, getWindowWidth(), getWindowHeight(), 0, GL_RGBA, |
| 94 | GL_UNSIGNED_BYTE, nullptr); |
| 95 | glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture, 0); |
| 96 | |
| 97 | glClearColor(0.5f, 0.5f, 0.5f, 0.5f); |
| 98 | glClear(GL_COLOR_BUFFER_BIT); |
| 99 | |
| 100 | EXPECT_PIXEL_NEAR(0, 0, 128, 128, 128, 128, 1.0); |
| 101 | } |
| 102 | |
Jamie Madill | fa05f60 | 2015-05-07 13:47:11 -0400 | [diff] [blame] | 103 | TEST_P(ClearTest, ClearIssue) |
Geoff Lang | d191317 | 2013-10-18 16:15:10 -0400 | [diff] [blame] | 104 | { |
Geoff Lang | 463cdea | 2015-04-28 13:22:31 -0400 | [diff] [blame] | 105 | // TODO(geofflang): Figure out why this is broken on Intel OpenGL |
| 106 | if (isIntel() && getPlatformRenderer() == EGL_PLATFORM_ANGLE_TYPE_OPENGL_ANGLE) |
| 107 | { |
| 108 | std::cout << "Test skipped on Intel OpenGL." << std::endl; |
| 109 | return; |
| 110 | } |
| 111 | |
Geoff Lang | d191317 | 2013-10-18 16:15:10 -0400 | [diff] [blame] | 112 | glEnable(GL_DEPTH_TEST); |
| 113 | glDepthFunc(GL_LEQUAL); |
| 114 | |
| 115 | glClearColor(0.0, 1.0, 0.0, 1.0); |
| 116 | glClearDepthf(0.0); |
| 117 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
| 118 | |
| 119 | EXPECT_GL_NO_ERROR(); |
| 120 | |
Jamie Madill | a09403c | 2014-07-21 10:03:36 -0400 | [diff] [blame] | 121 | glBindFramebuffer(GL_FRAMEBUFFER, mFBO); |
Geoff Lang | d191317 | 2013-10-18 16:15:10 -0400 | [diff] [blame] | 122 | |
| 123 | GLuint rbo; |
| 124 | glGenRenderbuffers(1, &rbo); |
| 125 | glBindRenderbuffer(GL_RENDERBUFFER, rbo); |
| 126 | glRenderbufferStorage(GL_RENDERBUFFER, GL_RGB565, 16, 16); |
| 127 | |
| 128 | EXPECT_GL_NO_ERROR(); |
| 129 | |
| 130 | glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, rbo); |
| 131 | |
| 132 | EXPECT_GL_NO_ERROR(); |
| 133 | |
| 134 | glClearColor(1.0f, 0.0f, 0.0f, 1.0f); |
| 135 | glClearDepthf(1.0f); |
| 136 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
| 137 | |
| 138 | EXPECT_GL_NO_ERROR(); |
| 139 | |
| 140 | glBindFramebuffer(GL_FRAMEBUFFER, 0); |
| 141 | glBindBuffer(GL_ARRAY_BUFFER, 0); |
| 142 | |
| 143 | drawQuad(mProgram, "position", 0.5f); |
| 144 | |
| 145 | EXPECT_PIXEL_EQ(0, 0, 0, 255, 0, 255); |
| 146 | } |
Jamie Madill | a09403c | 2014-07-21 10:03:36 -0400 | [diff] [blame] | 147 | |
| 148 | // Requires ES3 |
| 149 | // This tests a bug where in a masked clear when calling "ClearBuffer", we would |
| 150 | // mistakenly clear every channel (including the masked-out ones) |
Jamie Madill | fa05f60 | 2015-05-07 13:47:11 -0400 | [diff] [blame] | 151 | TEST_P(ClearTestES3, MaskedClearBufferBug) |
Jamie Madill | a09403c | 2014-07-21 10:03:36 -0400 | [diff] [blame] | 152 | { |
| 153 | unsigned char pixelData[] = { 255, 255, 255, 255 }; |
| 154 | |
| 155 | glBindFramebuffer(GL_FRAMEBUFFER, mFBO); |
| 156 | |
| 157 | GLuint textures[2]; |
| 158 | glGenTextures(2, &textures[0]); |
| 159 | |
| 160 | glBindTexture(GL_TEXTURE_2D, textures[0]); |
| 161 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixelData); |
| 162 | glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, textures[0], 0); |
| 163 | |
| 164 | glBindTexture(GL_TEXTURE_2D, textures[1]); |
| 165 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixelData); |
| 166 | glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D, textures[1], 0); |
| 167 | |
| 168 | ASSERT_GL_NO_ERROR(); |
| 169 | EXPECT_PIXEL_EQ(0, 0, 255, 255, 255, 255); |
| 170 | |
| 171 | float clearValue[] = { 0, 0.5f, 0.5f, 1.0f }; |
| 172 | GLenum drawBuffers[] = { GL_NONE, GL_COLOR_ATTACHMENT1 }; |
| 173 | glDrawBuffers(2, drawBuffers); |
| 174 | glColorMask(GL_TRUE, GL_TRUE, GL_FALSE, GL_TRUE); |
| 175 | glClearBufferfv(GL_COLOR, 1, clearValue); |
| 176 | |
| 177 | ASSERT_GL_NO_ERROR(); |
| 178 | EXPECT_PIXEL_EQ(0, 0, 255, 255, 255, 255); |
| 179 | |
| 180 | // TODO: glReadBuffer support |
| 181 | glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D, 0, 0); |
| 182 | glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, textures[1], 0); |
Jamie Madill | 9abdc2d | 2014-11-05 16:13:22 -0500 | [diff] [blame] | 183 | |
| 184 | //TODO(jmadill): Robust handling of pixel test error ranges |
| 185 | EXPECT_PIXEL_NEAR(0, 0, 0, 127, 255, 255, 1); |
Jamie Madill | c483326 | 2014-09-18 16:18:26 -0400 | [diff] [blame] | 186 | |
| 187 | glDeleteTextures(2, textures); |
| 188 | } |
| 189 | |
Jamie Madill | fa05f60 | 2015-05-07 13:47:11 -0400 | [diff] [blame] | 190 | TEST_P(ClearTestES3, BadFBOSerialBug) |
Jamie Madill | c483326 | 2014-09-18 16:18:26 -0400 | [diff] [blame] | 191 | { |
| 192 | // First make a simple framebuffer, and clear it to green |
| 193 | glBindFramebuffer(GL_FRAMEBUFFER, mFBO); |
| 194 | |
| 195 | GLuint textures[2]; |
| 196 | glGenTextures(2, &textures[0]); |
| 197 | |
| 198 | glBindTexture(GL_TEXTURE_2D, textures[0]); |
| 199 | glTexStorage2D(GL_TEXTURE_2D, 1, GL_RGBA8, getWindowWidth(), getWindowHeight()); |
| 200 | glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, textures[0], 0); |
| 201 | |
| 202 | GLenum drawBuffers[] = { GL_COLOR_ATTACHMENT0 }; |
| 203 | glDrawBuffers(1, drawBuffers); |
| 204 | |
| 205 | float clearValues1[] = { 0.0f, 1.0f, 0.0f, 1.0f }; |
| 206 | glClearBufferfv(GL_COLOR, 0, clearValues1); |
| 207 | |
| 208 | ASSERT_GL_NO_ERROR(); |
| 209 | EXPECT_PIXEL_EQ(0, 0, 0, 255, 0, 255); |
| 210 | |
| 211 | // Next make a second framebuffer, and draw it to red |
| 212 | // (Triggers bad applied render target serial) |
| 213 | GLuint fbo2; |
| 214 | glGenFramebuffers(1, &fbo2); |
| 215 | ASSERT_GL_NO_ERROR(); |
| 216 | |
| 217 | glBindFramebuffer(GL_FRAMEBUFFER, fbo2); |
| 218 | |
| 219 | glBindTexture(GL_TEXTURE_2D, textures[1]); |
| 220 | glTexStorage2D(GL_TEXTURE_2D, 1, GL_RGBA8, getWindowWidth(), getWindowHeight()); |
| 221 | glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, textures[1], 0); |
| 222 | |
| 223 | glDrawBuffers(1, drawBuffers); |
| 224 | |
| 225 | drawQuad(mProgram, "position", 0.5f); |
| 226 | |
| 227 | ASSERT_GL_NO_ERROR(); |
| 228 | EXPECT_PIXEL_EQ(0, 0, 255, 0, 0, 255); |
| 229 | |
| 230 | // Check that the first framebuffer is still green. |
| 231 | glBindFramebuffer(GL_FRAMEBUFFER, mFBO); |
| 232 | EXPECT_PIXEL_EQ(0, 0, 0, 255, 0, 255); |
| 233 | |
| 234 | glDeleteTextures(2, textures); |
| 235 | glDeleteFramebuffers(1, &fbo2); |
Jamie Madill | a09403c | 2014-07-21 10:03:36 -0400 | [diff] [blame] | 236 | } |
Jamie Madill | fa05f60 | 2015-05-07 13:47:11 -0400 | [diff] [blame] | 237 | |
Geoff Lang | afd7f0a | 2015-09-09 15:33:31 -0400 | [diff] [blame] | 238 | // Test that SRGB framebuffers clear to the linearized clear color |
| 239 | TEST_P(ClearTestES3, SRGBClear) |
| 240 | { |
| 241 | // First make a simple framebuffer, and clear it |
| 242 | glBindFramebuffer(GL_FRAMEBUFFER, mFBO); |
| 243 | |
| 244 | GLuint texture; |
| 245 | glGenTextures(1, &texture); |
| 246 | |
| 247 | glBindTexture(GL_TEXTURE_2D, texture); |
| 248 | glTexStorage2D(GL_TEXTURE_2D, 1, GL_SRGB8_ALPHA8, getWindowWidth(), getWindowHeight()); |
| 249 | glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture, 0); |
| 250 | |
| 251 | glClearColor(0.5f, 0.5f, 0.5f, 0.5f); |
| 252 | glClear(GL_COLOR_BUFFER_BIT); |
| 253 | |
| 254 | EXPECT_PIXEL_NEAR(0, 0, 188, 188, 188, 128, 1.0); |
| 255 | } |
| 256 | |
| 257 | // Test that framebuffers with mixed SRGB/Linear attachments clear to the correct color for each |
| 258 | // attachment |
| 259 | TEST_P(ClearTestES3, MixedSRGBClear) |
| 260 | { |
Corentin Wallez | 7b43c9b | 2015-09-30 10:47:30 -0700 | [diff] [blame] | 261 | // TODO(cwallez) figure out why it is broken on Intel on Mac |
Olli Etuaho | 36b7390 | 2015-10-05 11:58:50 +0300 | [diff] [blame^] | 262 | #if defined(ANGLE_PLATFORM_APPLE) |
Corentin Wallez | 7b43c9b | 2015-09-30 10:47:30 -0700 | [diff] [blame] | 263 | if (isIntel() && getPlatformRenderer() == EGL_PLATFORM_ANGLE_TYPE_OPENGL_ANGLE) |
| 264 | { |
| 265 | std::cout << "Test skipped on Intel on Mac." << std::endl; |
| 266 | return; |
| 267 | } |
| 268 | #endif |
| 269 | |
Geoff Lang | afd7f0a | 2015-09-09 15:33:31 -0400 | [diff] [blame] | 270 | glBindFramebuffer(GL_FRAMEBUFFER, mFBO); |
| 271 | |
| 272 | GLuint textures[2]; |
| 273 | glGenTextures(2, &textures[0]); |
| 274 | |
| 275 | glBindTexture(GL_TEXTURE_2D, textures[0]); |
| 276 | glTexStorage2D(GL_TEXTURE_2D, 1, GL_SRGB8_ALPHA8, getWindowWidth(), getWindowHeight()); |
| 277 | glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, textures[0], 0); |
| 278 | |
| 279 | glBindTexture(GL_TEXTURE_2D, textures[1]); |
| 280 | glTexStorage2D(GL_TEXTURE_2D, 1, GL_RGBA8, getWindowWidth(), getWindowHeight()); |
| 281 | glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D, textures[1], 0); |
| 282 | |
| 283 | GLenum drawBuffers[] = {GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1}; |
| 284 | glDrawBuffers(2, drawBuffers); |
| 285 | |
| 286 | // Clear both textures |
| 287 | glClearColor(0.5f, 0.5f, 0.5f, 0.5f); |
| 288 | glClear(GL_COLOR_BUFFER_BIT); |
| 289 | |
| 290 | glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0); |
| 291 | glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D, 0, 0); |
| 292 | |
| 293 | // Check value of texture0 |
| 294 | glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, textures[0], 0); |
| 295 | EXPECT_PIXEL_NEAR(0, 0, 188, 188, 188, 128, 1.0); |
| 296 | |
| 297 | // Check value of texture1 |
| 298 | glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, textures[1], 0); |
| 299 | EXPECT_PIXEL_NEAR(0, 0, 128, 128, 128, 128, 1.0); |
| 300 | } |
| 301 | |
Jamie Madill | fa05f60 | 2015-05-07 13:47:11 -0400 | [diff] [blame] | 302 | // Use this to select which configurations (e.g. which renderer, which GLES major version) these tests should be run against. |
| 303 | ANGLE_INSTANTIATE_TEST(ClearTest, ES2_D3D9(), ES2_D3D11(), ES3_D3D11(), ES2_OPENGL(), ES3_OPENGL()); |
Geoff Lang | afd7f0a | 2015-09-09 15:33:31 -0400 | [diff] [blame] | 304 | ANGLE_INSTANTIATE_TEST(ClearTestES3, ES3_D3D11(), ES3_OPENGL()); |