blob: b5148c879817925e47e922efcff22622dc49bf24 [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 Langd1913172013-10-18 16:15:10 -04008
Jamie Madillfa05f602015-05-07 13:47:11 -04009using namespace angle;
Jamie Madill94203b32014-10-02 10:44:16 -040010
Jamie Madill94203b32014-10-02 10:44:16 -040011class ClearTestBase : public ANGLETest
Geoff Langd1913172013-10-18 16:15:10 -040012{
Jamie Madill94203b32014-10-02 10:44:16 -040013 protected:
Jamie Madillfa05f602015-05-07 13:47:11 -040014 ClearTestBase()
Geoff Langd1913172013-10-18 16:15:10 -040015 {
16 setWindowWidth(128);
17 setWindowHeight(128);
Geoff Langefc551f2013-10-31 10:20:28 -040018 setConfigRedBits(8);
19 setConfigGreenBits(8);
20 setConfigBlueBits(8);
21 setConfigAlphaBits(8);
22 setConfigDepthBits(24);
Geoff Langd1913172013-10-18 16:15:10 -040023 }
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 Madill5599c8f2014-08-26 13:16:39 -040050 mProgram = CompileProgram(vertexShaderSource, fragmentShaderSource);
Geoff Langd1913172013-10-18 16:15:10 -040051 if (mProgram == 0)
52 {
53 FAIL() << "shader compilation failed.";
54 }
Jamie Madilla09403c2014-07-21 10:03:36 -040055
56 glGenFramebuffers(1, &mFBO);
57
58 ASSERT_GL_NO_ERROR();
Geoff Langd1913172013-10-18 16:15:10 -040059 }
60
61 virtual void TearDown()
62 {
63 glDeleteProgram(mProgram);
Jamie Madilla09403c2014-07-21 10:03:36 -040064 glDeleteFramebuffers(1, &mFBO);
Geoff Langd1913172013-10-18 16:15:10 -040065
66 ANGLETest::TearDown();
67 }
68
69 GLuint mProgram;
Jamie Madilla09403c2014-07-21 10:03:36 -040070 GLuint mFBO;
Geoff Langd1913172013-10-18 16:15:10 -040071};
72
Jamie Madillfa05f602015-05-07 13:47:11 -040073class ClearTest : public ClearTestBase {};
74class ClearTestES3 : public ClearTestBase {};
Jamie Madill94203b32014-10-02 10:44:16 -040075
Geoff Langafd7f0a2015-09-09 15:33:31 -040076// Test clearing the default framebuffer
77TEST_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
85TEST_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 Madillfa05f602015-05-07 13:47:11 -0400103TEST_P(ClearTest, ClearIssue)
Geoff Langd1913172013-10-18 16:15:10 -0400104{
Geoff Lang463cdea2015-04-28 13:22:31 -0400105 // 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 Langd1913172013-10-18 16:15:10 -0400112 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 Madilla09403c2014-07-21 10:03:36 -0400121 glBindFramebuffer(GL_FRAMEBUFFER, mFBO);
Geoff Langd1913172013-10-18 16:15:10 -0400122
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 Madilla09403c2014-07-21 10:03:36 -0400147
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 Madillfa05f602015-05-07 13:47:11 -0400151TEST_P(ClearTestES3, MaskedClearBufferBug)
Jamie Madilla09403c2014-07-21 10:03:36 -0400152{
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
Jamie Madillcb34ea52015-12-11 16:01:18 -0500180 glReadBuffer(GL_COLOR_ATTACHMENT1);
181 ASSERT_GL_NO_ERROR();
Jamie Madill9abdc2d2014-11-05 16:13:22 -0500182
Jamie Madill9abdc2d2014-11-05 16:13:22 -0500183 EXPECT_PIXEL_NEAR(0, 0, 0, 127, 255, 255, 1);
Jamie Madillc4833262014-09-18 16:18:26 -0400184
185 glDeleteTextures(2, textures);
186}
187
Jamie Madillfa05f602015-05-07 13:47:11 -0400188TEST_P(ClearTestES3, BadFBOSerialBug)
Jamie Madillc4833262014-09-18 16:18:26 -0400189{
190 // First make a simple framebuffer, and clear it to green
191 glBindFramebuffer(GL_FRAMEBUFFER, mFBO);
192
193 GLuint textures[2];
194 glGenTextures(2, &textures[0]);
195
196 glBindTexture(GL_TEXTURE_2D, textures[0]);
197 glTexStorage2D(GL_TEXTURE_2D, 1, GL_RGBA8, getWindowWidth(), getWindowHeight());
198 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, textures[0], 0);
199
200 GLenum drawBuffers[] = { GL_COLOR_ATTACHMENT0 };
201 glDrawBuffers(1, drawBuffers);
202
203 float clearValues1[] = { 0.0f, 1.0f, 0.0f, 1.0f };
204 glClearBufferfv(GL_COLOR, 0, clearValues1);
205
206 ASSERT_GL_NO_ERROR();
207 EXPECT_PIXEL_EQ(0, 0, 0, 255, 0, 255);
208
209 // Next make a second framebuffer, and draw it to red
210 // (Triggers bad applied render target serial)
211 GLuint fbo2;
212 glGenFramebuffers(1, &fbo2);
213 ASSERT_GL_NO_ERROR();
214
215 glBindFramebuffer(GL_FRAMEBUFFER, fbo2);
216
217 glBindTexture(GL_TEXTURE_2D, textures[1]);
218 glTexStorage2D(GL_TEXTURE_2D, 1, GL_RGBA8, getWindowWidth(), getWindowHeight());
219 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, textures[1], 0);
220
221 glDrawBuffers(1, drawBuffers);
222
223 drawQuad(mProgram, "position", 0.5f);
224
225 ASSERT_GL_NO_ERROR();
226 EXPECT_PIXEL_EQ(0, 0, 255, 0, 0, 255);
227
228 // Check that the first framebuffer is still green.
229 glBindFramebuffer(GL_FRAMEBUFFER, mFBO);
230 EXPECT_PIXEL_EQ(0, 0, 0, 255, 0, 255);
231
232 glDeleteTextures(2, textures);
233 glDeleteFramebuffers(1, &fbo2);
Jamie Madilla09403c2014-07-21 10:03:36 -0400234}
Jamie Madillfa05f602015-05-07 13:47:11 -0400235
Geoff Langafd7f0a2015-09-09 15:33:31 -0400236// Test that SRGB framebuffers clear to the linearized clear color
237TEST_P(ClearTestES3, SRGBClear)
238{
Jamie Madill1ea9aaa2015-10-07 11:13:55 -0400239 // TODO(jmadill): figure out why this fails
240 if (isIntel() && GetParam() == ES3_OPENGL())
241 {
242 std::cout << "Test skipped on Intel due to failures." << std::endl;
243 return;
244 }
245
Geoff Langafd7f0a2015-09-09 15:33:31 -0400246 // First make a simple framebuffer, and clear it
247 glBindFramebuffer(GL_FRAMEBUFFER, mFBO);
248
249 GLuint texture;
250 glGenTextures(1, &texture);
251
252 glBindTexture(GL_TEXTURE_2D, texture);
253 glTexStorage2D(GL_TEXTURE_2D, 1, GL_SRGB8_ALPHA8, getWindowWidth(), getWindowHeight());
254 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture, 0);
255
256 glClearColor(0.5f, 0.5f, 0.5f, 0.5f);
257 glClear(GL_COLOR_BUFFER_BIT);
258
259 EXPECT_PIXEL_NEAR(0, 0, 188, 188, 188, 128, 1.0);
260}
261
262// Test that framebuffers with mixed SRGB/Linear attachments clear to the correct color for each
263// attachment
264TEST_P(ClearTestES3, MixedSRGBClear)
265{
Corentin Wallez7b43c9b2015-09-30 10:47:30 -0700266 // TODO(cwallez) figure out why it is broken on Intel on Mac
Olli Etuaho36b73902015-10-05 11:58:50 +0300267#if defined(ANGLE_PLATFORM_APPLE)
Corentin Wallez7b43c9b2015-09-30 10:47:30 -0700268 if (isIntel() && getPlatformRenderer() == EGL_PLATFORM_ANGLE_TYPE_OPENGL_ANGLE)
269 {
270 std::cout << "Test skipped on Intel on Mac." << std::endl;
271 return;
272 }
273#endif
274
Jamie Madill1ea9aaa2015-10-07 11:13:55 -0400275 // TODO(jmadill): figure out why this fails
276 if (isIntel() && GetParam() == ES3_OPENGL())
277 {
278 std::cout << "Test skipped on Intel due to failures." << std::endl;
279 return;
280 }
281
Geoff Langafd7f0a2015-09-09 15:33:31 -0400282 glBindFramebuffer(GL_FRAMEBUFFER, mFBO);
283
284 GLuint textures[2];
285 glGenTextures(2, &textures[0]);
286
287 glBindTexture(GL_TEXTURE_2D, textures[0]);
288 glTexStorage2D(GL_TEXTURE_2D, 1, GL_SRGB8_ALPHA8, getWindowWidth(), getWindowHeight());
289 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, textures[0], 0);
290
291 glBindTexture(GL_TEXTURE_2D, textures[1]);
292 glTexStorage2D(GL_TEXTURE_2D, 1, GL_RGBA8, getWindowWidth(), getWindowHeight());
293 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D, textures[1], 0);
294
295 GLenum drawBuffers[] = {GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1};
296 glDrawBuffers(2, drawBuffers);
297
298 // Clear both textures
299 glClearColor(0.5f, 0.5f, 0.5f, 0.5f);
300 glClear(GL_COLOR_BUFFER_BIT);
301
302 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
303 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D, 0, 0);
304
305 // Check value of texture0
306 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, textures[0], 0);
307 EXPECT_PIXEL_NEAR(0, 0, 188, 188, 188, 128, 1.0);
308
309 // Check value of texture1
310 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, textures[1], 0);
311 EXPECT_PIXEL_NEAR(0, 0, 128, 128, 128, 128, 1.0);
312}
313
Jamie Madillfa05f602015-05-07 13:47:11 -0400314// Use this to select which configurations (e.g. which renderer, which GLES major version) these tests should be run against.
Geoff Lange0cc2a42016-01-20 10:58:17 -0500315ANGLE_INSTANTIATE_TEST(ClearTest,
316 ES2_D3D9(),
317 ES2_D3D11(),
318 ES3_D3D11(),
319 ES2_OPENGL(),
320 ES3_OPENGL(),
321 ES2_OPENGLES(),
322 ES3_OPENGLES());
323ANGLE_INSTANTIATE_TEST(ClearTestES3, ES3_D3D11(), ES3_OPENGL(), ES3_OPENGLES());