blob: 1af48d8d858acd954c25145dbf132493544ee248 [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
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 Madill9abdc2d2014-11-05 16:13:22 -0500183
184 //TODO(jmadill): Robust handling of pixel test error ranges
185 EXPECT_PIXEL_NEAR(0, 0, 0, 127, 255, 255, 1);
Jamie Madillc4833262014-09-18 16:18:26 -0400186
187 glDeleteTextures(2, textures);
188}
189
Jamie Madillfa05f602015-05-07 13:47:11 -0400190TEST_P(ClearTestES3, BadFBOSerialBug)
Jamie Madillc4833262014-09-18 16:18:26 -0400191{
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 Madilla09403c2014-07-21 10:03:36 -0400236}
Jamie Madillfa05f602015-05-07 13:47:11 -0400237
Geoff Langafd7f0a2015-09-09 15:33:31 -0400238// Test that SRGB framebuffers clear to the linearized clear color
239TEST_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
259TEST_P(ClearTestES3, MixedSRGBClear)
260{
Corentin Wallez7b43c9b2015-09-30 10:47:30 -0700261 // TODO(cwallez) figure out why it is broken on Intel on Mac
Olli Etuaho36b73902015-10-05 11:58:50 +0300262#if defined(ANGLE_PLATFORM_APPLE)
Corentin Wallez7b43c9b2015-09-30 10:47:30 -0700263 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 Langafd7f0a2015-09-09 15:33:31 -0400270 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 Madillfa05f602015-05-07 13:47:11 -0400302// Use this to select which configurations (e.g. which renderer, which GLES major version) these tests should be run against.
303ANGLE_INSTANTIATE_TEST(ClearTest, ES2_D3D9(), ES2_D3D11(), ES3_D3D11(), ES2_OPENGL(), ES3_OPENGL());
Geoff Langafd7f0a2015-09-09 15:33:31 -0400304ANGLE_INSTANTIATE_TEST(ClearTestES3, ES3_D3D11(), ES3_OPENGL());