• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
7 #include "test_utils/ANGLETest.h"
8 
9 #include "test_utils/gl_raii.h"
10 #include "util/random_utils.h"
11 #include "util/shader_utils.h"
12 #include "util/test_utils.h"
13 
14 using namespace angle;
15 
16 namespace
17 {
18 class ClearTestBase : public ANGLETest<>
19 {
20   protected:
ClearTestBase()21     ClearTestBase()
22     {
23         setWindowWidth(128);
24         setWindowHeight(128);
25         setConfigRedBits(8);
26         setConfigGreenBits(8);
27         setConfigBlueBits(8);
28         setConfigAlphaBits(8);
29         setConfigDepthBits(24);
30         setConfigStencilBits(8);
31     }
32 
testSetUp()33     void testSetUp() override
34     {
35         mFBOs.resize(2, 0);
36         glGenFramebuffers(2, mFBOs.data());
37 
38         ASSERT_GL_NO_ERROR();
39     }
40 
testTearDown()41     void testTearDown() override
42     {
43         if (!mFBOs.empty())
44         {
45             glDeleteFramebuffers(static_cast<GLsizei>(mFBOs.size()), mFBOs.data());
46         }
47 
48         if (!mTextures.empty())
49         {
50             glDeleteTextures(static_cast<GLsizei>(mTextures.size()), mTextures.data());
51         }
52     }
53 
54     std::vector<GLuint> mFBOs;
55     std::vector<GLuint> mTextures;
56 };
57 
58 class ClearTest : public ClearTestBase
59 {};
60 
61 class ClearTestES3 : public ClearTestBase
62 {
63   protected:
verifyDepth(float depthValue,uint32_t size)64     void verifyDepth(float depthValue, uint32_t size)
65     {
66         // Use a small shader to verify depth.
67         ANGLE_GL_PROGRAM(depthTestProgram, essl1_shaders::vs::Passthrough(),
68                          essl1_shaders::fs::Blue());
69         ANGLE_GL_PROGRAM(depthTestProgramFail, essl1_shaders::vs::Passthrough(),
70                          essl1_shaders::fs::Red());
71 
72         GLboolean hasDepthTest  = GL_FALSE;
73         GLboolean hasDepthWrite = GL_TRUE;
74         GLint prevDepthFunc     = GL_ALWAYS;
75 
76         glGetBooleanv(GL_DEPTH_TEST, &hasDepthTest);
77         glGetBooleanv(GL_DEPTH_WRITEMASK, &hasDepthWrite);
78         glGetIntegerv(GL_DEPTH_FUNC, &prevDepthFunc);
79 
80         if (!hasDepthTest)
81         {
82             glEnable(GL_DEPTH_TEST);
83         }
84         if (hasDepthWrite)
85         {
86             glDepthMask(GL_FALSE);
87         }
88         glDepthFunc(GL_LESS);
89         drawQuad(depthTestProgram, essl1_shaders::PositionAttrib(), depthValue * 2 - 1 - 0.01f);
90         drawQuad(depthTestProgramFail, essl1_shaders::PositionAttrib(), depthValue * 2 - 1 + 0.01f);
91         if (!hasDepthTest)
92         {
93             glDisable(GL_DEPTH_TEST);
94         }
95         if (hasDepthWrite)
96         {
97             glDepthMask(GL_TRUE);
98         }
99         glDepthFunc(prevDepthFunc);
100         ASSERT_GL_NO_ERROR();
101 
102         EXPECT_PIXEL_COLOR_NEAR(0, 0, GLColor::blue, 1);
103         EXPECT_PIXEL_COLOR_NEAR(size - 1, 0, GLColor::blue, 1);
104         EXPECT_PIXEL_COLOR_NEAR(0, size - 1, GLColor::blue, 1);
105         EXPECT_PIXEL_COLOR_NEAR(size - 1, size - 1, GLColor::blue, 1);
106     }
107 
verifyStencil(uint32_t stencilValue,uint32_t size)108     void verifyStencil(uint32_t stencilValue, uint32_t size)
109     {
110         // Use another small shader to verify stencil.
111         ANGLE_GL_PROGRAM(stencilTestProgram, essl1_shaders::vs::Passthrough(),
112                          essl1_shaders::fs::Green());
113         GLboolean hasStencilTest   = GL_FALSE;
114         GLint prevStencilFunc      = GL_ALWAYS;
115         GLint prevStencilValue     = 0xFF;
116         GLint prevStencilRef       = 0xFF;
117         GLint prevStencilFail      = GL_KEEP;
118         GLint prevStencilDepthFail = GL_KEEP;
119         GLint prevStencilDepthPass = GL_KEEP;
120 
121         glGetBooleanv(GL_STENCIL_TEST, &hasStencilTest);
122         glGetIntegerv(GL_STENCIL_FUNC, &prevStencilFunc);
123         glGetIntegerv(GL_STENCIL_VALUE_MASK, &prevStencilValue);
124         glGetIntegerv(GL_STENCIL_REF, &prevStencilRef);
125         glGetIntegerv(GL_STENCIL_FAIL, &prevStencilFail);
126         glGetIntegerv(GL_STENCIL_PASS_DEPTH_FAIL, &prevStencilDepthFail);
127         glGetIntegerv(GL_STENCIL_PASS_DEPTH_PASS, &prevStencilDepthPass);
128 
129         if (!hasStencilTest)
130         {
131             glEnable(GL_STENCIL_TEST);
132         }
133         glStencilFunc(GL_EQUAL, stencilValue, 0xFF);
134         glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
135         drawQuad(stencilTestProgram, essl1_shaders::PositionAttrib(), 0.0f);
136         if (!hasStencilTest)
137         {
138             glDisable(GL_STENCIL_TEST);
139         }
140         glStencilFunc(prevStencilFunc, prevStencilValue, prevStencilRef);
141         glStencilOp(prevStencilFail, prevStencilDepthFail, prevStencilDepthPass);
142         ASSERT_GL_NO_ERROR();
143 
144         EXPECT_PIXEL_COLOR_NEAR(0, 0, GLColor::green, 1);
145         EXPECT_PIXEL_COLOR_NEAR(size - 1, 0, GLColor::green, 1);
146         EXPECT_PIXEL_COLOR_NEAR(0, size - 1, GLColor::green, 1);
147         EXPECT_PIXEL_COLOR_NEAR(size - 1, size - 1, GLColor::green, 1);
148     }
149 };
150 
151 class ClearTestES31 : public ClearTestES3
152 {};
153 
154 class ClearTestRGB : public ANGLETest<>
155 {
156   protected:
ClearTestRGB()157     ClearTestRGB()
158     {
159         setWindowWidth(128);
160         setWindowHeight(128);
161         setConfigRedBits(8);
162         setConfigGreenBits(8);
163         setConfigBlueBits(8);
164     }
165 };
166 
167 class ClearTestRGB_ES3 : public ClearTestRGB
168 {};
169 
170 // Each int parameter can have three values: don't clear, clear, or masked clear.  The bool
171 // parameter controls scissor.
172 using MaskedScissoredClearVariationsTestParams =
173     std::tuple<angle::PlatformParameters, int, int, int, bool>;
174 
ParseMaskedScissoredClearVariationsTestParams(const MaskedScissoredClearVariationsTestParams & params,bool * clearColor,bool * clearDepth,bool * clearStencil,bool * maskColor,bool * maskDepth,bool * maskStencil,bool * scissor)175 void ParseMaskedScissoredClearVariationsTestParams(
176     const MaskedScissoredClearVariationsTestParams &params,
177     bool *clearColor,
178     bool *clearDepth,
179     bool *clearStencil,
180     bool *maskColor,
181     bool *maskDepth,
182     bool *maskStencil,
183     bool *scissor)
184 {
185     int colorClearInfo   = std::get<1>(params);
186     int depthClearInfo   = std::get<2>(params);
187     int stencilClearInfo = std::get<3>(params);
188 
189     *clearColor   = colorClearInfo > 0;
190     *clearDepth   = depthClearInfo > 0;
191     *clearStencil = stencilClearInfo > 0;
192 
193     *maskColor   = colorClearInfo > 1;
194     *maskDepth   = depthClearInfo > 1;
195     *maskStencil = stencilClearInfo > 1;
196 
197     *scissor = std::get<4>(params);
198 }
199 
MaskedScissoredClearVariationsTestPrint(const::testing::TestParamInfo<MaskedScissoredClearVariationsTestParams> & paramsInfo)200 std::string MaskedScissoredClearVariationsTestPrint(
201     const ::testing::TestParamInfo<MaskedScissoredClearVariationsTestParams> &paramsInfo)
202 {
203     const MaskedScissoredClearVariationsTestParams &params = paramsInfo.param;
204     std::ostringstream out;
205 
206     out << std::get<0>(params);
207 
208     bool clearColor, clearDepth, clearStencil;
209     bool maskColor, maskDepth, maskStencil;
210     bool scissor;
211 
212     ParseMaskedScissoredClearVariationsTestParams(params, &clearColor, &clearDepth, &clearStencil,
213                                                   &maskColor, &maskDepth, &maskStencil, &scissor);
214 
215     if (scissor || clearColor || clearDepth || clearStencil || maskColor || maskDepth ||
216         maskStencil)
217     {
218         out << "_";
219     }
220 
221     if (scissor)
222     {
223         out << "_scissored";
224     }
225 
226     if (clearColor || clearDepth || clearStencil)
227     {
228         out << "_clear_";
229         if (clearColor)
230         {
231             out << "c";
232         }
233         if (clearDepth)
234         {
235             out << "d";
236         }
237         if (clearStencil)
238         {
239             out << "s";
240         }
241     }
242 
243     if (maskColor || maskDepth || maskStencil)
244     {
245         out << "_mask_";
246         if (maskColor)
247         {
248             out << "c";
249         }
250         if (maskDepth)
251         {
252             out << "d";
253         }
254         if (maskStencil)
255         {
256             out << "s";
257         }
258     }
259 
260     return out.str();
261 }
262 
263 class MaskedScissoredClearTestBase : public ANGLETest<MaskedScissoredClearVariationsTestParams>
264 {
265   protected:
MaskedScissoredClearTestBase()266     MaskedScissoredClearTestBase()
267     {
268         setWindowWidth(128);
269         setWindowHeight(128);
270         setConfigRedBits(8);
271         setConfigGreenBits(8);
272         setConfigBlueBits(8);
273         setConfigAlphaBits(8);
274         setConfigDepthBits(24);
275         setConfigStencilBits(8);
276     }
277 
278     void maskedScissoredColorDepthStencilClear(
279         const MaskedScissoredClearVariationsTestParams &params);
280 
281     bool mHasDepth   = true;
282     bool mHasStencil = true;
283 };
284 
285 class MaskedScissoredClearTest : public MaskedScissoredClearTestBase
286 {};
287 
288 // Overrides a feature to force emulation of stencil-only and depth-only formats with a packed
289 // depth/stencil format
290 class VulkanClearTest : public MaskedScissoredClearTestBase
291 {
292   protected:
testSetUp()293     void testSetUp() override
294     {
295         glBindTexture(GL_TEXTURE_2D, mColorTexture);
296         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, getWindowWidth(), getWindowHeight(), 0, GL_RGBA,
297                      GL_UNSIGNED_BYTE, nullptr);
298 
299         // Setup Color/Stencil FBO with a stencil format that's emulated with packed depth/stencil.
300         glBindFramebuffer(GL_FRAMEBUFFER, mColorStencilFBO);
301 
302         glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, mColorTexture,
303                                0);
304         glBindRenderbuffer(GL_RENDERBUFFER, mStencilTexture);
305         glRenderbufferStorage(GL_RENDERBUFFER, GL_STENCIL_INDEX8, getWindowWidth(),
306                               getWindowHeight());
307         glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER,
308                                   mStencilTexture);
309 
310         ASSERT_GL_NO_ERROR();
311 
312         // Note: GL_DEPTH_COMPONENT24 is not allowed in GLES2.
313         if (getClientMajorVersion() >= 3)
314         {
315             // Setup Color/Depth FBO with a depth format that's emulated with packed depth/stencil.
316             glBindFramebuffer(GL_FRAMEBUFFER, mColorDepthFBO);
317 
318             glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
319                                    mColorTexture, 0);
320             glBindRenderbuffer(GL_RENDERBUFFER, mDepthTexture);
321             glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT24, getWindowWidth(),
322                                   getWindowHeight());
323             glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER,
324                                       mDepthTexture);
325         }
326 
327         ASSERT_GL_NO_ERROR();
328     }
329 
bindColorStencilFBO()330     void bindColorStencilFBO()
331     {
332         glBindFramebuffer(GL_FRAMEBUFFER, mColorStencilFBO);
333         mHasDepth = false;
334     }
335 
bindColorDepthFBO()336     void bindColorDepthFBO()
337     {
338         glBindFramebuffer(GL_FRAMEBUFFER, mColorDepthFBO);
339         mHasStencil = false;
340     }
341 
342   private:
343     GLFramebuffer mColorStencilFBO;
344     GLFramebuffer mColorDepthFBO;
345     GLTexture mColorTexture;
346     GLRenderbuffer mDepthTexture;
347     GLRenderbuffer mStencilTexture;
348 };
349 
350 // Test clearing the default framebuffer
TEST_P(ClearTest,DefaultFramebuffer)351 TEST_P(ClearTest, DefaultFramebuffer)
352 {
353     glClearColor(0.25f, 0.5f, 0.5f, 0.5f);
354     glClear(GL_COLOR_BUFFER_BIT);
355     EXPECT_PIXEL_NEAR(0, 0, 64, 128, 128, 128, 1.0);
356 }
357 
358 // Test clearing the default framebuffer with scissor and mask
359 // This forces down path that uses draw to do clear
TEST_P(ClearTest,EmptyScissor)360 TEST_P(ClearTest, EmptyScissor)
361 {
362     // These configs have bug that fails this test.
363     // These configs are unmaintained so skipping.
364     ANGLE_SKIP_TEST_IF(IsIntel() && IsD3D9());
365     ANGLE_SKIP_TEST_IF(IsMac());
366     glClearColor(0.25f, 0.5f, 0.5f, 1.0f);
367     glClear(GL_COLOR_BUFFER_BIT);
368     glEnable(GL_SCISSOR_TEST);
369     glScissor(-10, 0, 5, 5);
370     glClearColor(0.5f, 0.25f, 0.75f, 0.5f);
371     glColorMask(GL_TRUE, GL_FALSE, GL_TRUE, GL_TRUE);
372     glClear(GL_COLOR_BUFFER_BIT);
373     glDisable(GL_SCISSOR_TEST);
374     glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
375     EXPECT_PIXEL_NEAR(0, 0, 64, 128, 128, 255, 1.0);
376 }
377 
378 // Test clearing the RGB default framebuffer and verify that the alpha channel is not cleared
TEST_P(ClearTestRGB,DefaultFramebufferRGB)379 TEST_P(ClearTestRGB, DefaultFramebufferRGB)
380 {
381     // Some GPUs don't support RGB format default framebuffer,
382     // so skip if the back buffer has alpha bits.
383     EGLWindow *window          = getEGLWindow();
384     EGLDisplay display         = window->getDisplay();
385     EGLConfig config           = window->getConfig();
386     EGLint backbufferAlphaBits = 0;
387     eglGetConfigAttrib(display, config, EGL_ALPHA_SIZE, &backbufferAlphaBits);
388     ANGLE_SKIP_TEST_IF(backbufferAlphaBits != 0);
389 
390     glClearColor(0.25f, 0.5f, 0.5f, 0.5f);
391     glClear(GL_COLOR_BUFFER_BIT);
392     EXPECT_PIXEL_NEAR(0, 0, 64, 128, 128, 255, 1.0);
393 }
394 
395 // Invalidate the RGB default framebuffer and verify that the alpha channel is not cleared, and
396 // stays set after drawing.
TEST_P(ClearTestRGB_ES3,InvalidateDefaultFramebufferRGB)397 TEST_P(ClearTestRGB_ES3, InvalidateDefaultFramebufferRGB)
398 {
399     ANGLE_GL_PROGRAM(blueProgram, essl1_shaders::vs::Simple(), essl1_shaders::fs::Blue());
400 
401     // Some GPUs don't support RGB format default framebuffer,
402     // so skip if the back buffer has alpha bits.
403     EGLWindow *window          = getEGLWindow();
404     EGLDisplay display         = window->getDisplay();
405     EGLConfig config           = window->getConfig();
406     EGLint backbufferAlphaBits = 0;
407     eglGetConfigAttrib(display, config, EGL_ALPHA_SIZE, &backbufferAlphaBits);
408     ANGLE_SKIP_TEST_IF(backbufferAlphaBits != 0);
409 
410     glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
411     glClear(GL_COLOR_BUFFER_BIT);
412     // Verify that even though Alpha is cleared to 0.0 for this RGB FBO, it should be read back as
413     // 1.0, since the glReadPixels() is issued with GL_RGBA.
414     // OpenGL ES 3.2 spec:
415     // 16.1.3 Obtaining Pixels from the Framebuffer
416     // If G, B, or A values are not present in the internal format, they are taken to be zero,
417     // zero, and one respectively.
418     EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::black);
419 
420     const GLenum discards[] = {GL_COLOR};
421     glInvalidateFramebuffer(GL_FRAMEBUFFER, 1, discards);
422 
423     // Don't explicitly clear, but draw blue (make sure alpha is not cleared)
424     drawQuad(blueProgram, essl1_shaders::PositionAttrib(), 0.5f);
425     EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::blue);
426 }
427 
428 // Draw with a shader that outputs alpha=0.5. Readback and ensure that alpha=1.
TEST_P(ClearTestRGB_ES3,ShaderOutputsAlphaVerifyReadingAlphaIsOne)429 TEST_P(ClearTestRGB_ES3, ShaderOutputsAlphaVerifyReadingAlphaIsOne)
430 {
431     ANGLE_GL_PROGRAM(blueProgram, essl1_shaders::vs::Simple(), essl1_shaders::fs::UniformColor());
432     glUseProgram(blueProgram);
433 
434     // Some GPUs don't support RGB format default framebuffer,
435     // so skip if the back buffer has alpha bits.
436     EGLWindow *window          = getEGLWindow();
437     EGLDisplay display         = window->getDisplay();
438     EGLConfig config           = window->getConfig();
439     EGLint backbufferAlphaBits = 0;
440     eglGetConfigAttrib(display, config, EGL_ALPHA_SIZE, &backbufferAlphaBits);
441     ANGLE_SKIP_TEST_IF(backbufferAlphaBits != 0);
442 
443     glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
444     glClear(GL_COLOR_BUFFER_BIT);
445     EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::black);
446 
447     GLint colorUniformLocation =
448         glGetUniformLocation(blueProgram, angle::essl1_shaders::ColorUniform());
449     ASSERT_NE(colorUniformLocation, -1);
450     glUniform4f(colorUniformLocation, 0.0f, 0.0f, 1.0f, 0.5f);
451     ASSERT_GL_NO_ERROR();
452 
453     // Don't explicitly clear, but draw blue (make sure alpha is not cleared)
454     drawQuad(blueProgram, essl1_shaders::PositionAttrib(), 0.5f);
455     EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::blue);
456 }
457 
458 // Test clearing a RGBA8 Framebuffer
TEST_P(ClearTest,RGBA8Framebuffer)459 TEST_P(ClearTest, RGBA8Framebuffer)
460 {
461     glBindFramebuffer(GL_FRAMEBUFFER, mFBOs[0]);
462 
463     GLTexture texture;
464 
465     glBindTexture(GL_TEXTURE_2D, texture);
466     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, getWindowWidth(), getWindowHeight(), 0, GL_RGBA,
467                  GL_UNSIGNED_BYTE, nullptr);
468     glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture, 0);
469 
470     glClearColor(0.5f, 0.5f, 0.5f, 0.5f);
471     glClear(GL_COLOR_BUFFER_BIT);
472 
473     EXPECT_PIXEL_NEAR(0, 0, 128, 128, 128, 128, 1.0);
474 }
475 
476 // Test to validate that we can go from an RGBA framebuffer attachment, to an RGB one and still
477 // have a correct behavior after.
TEST_P(ClearTest,ChangeFramebufferAttachmentFromRGBAtoRGB)478 TEST_P(ClearTest, ChangeFramebufferAttachmentFromRGBAtoRGB)
479 {
480     // http://anglebug.com/2689
481     ANGLE_SKIP_TEST_IF(IsAndroid() && IsAdreno() && IsOpenGLES());
482 
483     // http://anglebug.com/5165
484     ANGLE_SKIP_TEST_IF(IsMac() && IsDesktopOpenGL() && IsIntel());
485 
486     ANGLE_GL_PROGRAM(program, angle::essl1_shaders::vs::Simple(),
487                      angle::essl1_shaders::fs::UniformColor());
488     setupQuadVertexBuffer(0.5f, 1.0f);
489     glUseProgram(program);
490     GLint positionLocation = glGetAttribLocation(program, angle::essl1_shaders::PositionAttrib());
491     ASSERT_NE(positionLocation, -1);
492     glVertexAttribPointer(positionLocation, 3, GL_FLOAT, GL_FALSE, 0, nullptr);
493     glEnableVertexAttribArray(positionLocation);
494 
495     GLint colorUniformLocation =
496         glGetUniformLocation(program, angle::essl1_shaders::ColorUniform());
497     ASSERT_NE(colorUniformLocation, -1);
498 
499     glUniform4f(colorUniformLocation, 1.0f, 1.0f, 1.0f, 0.5f);
500 
501     glBindFramebuffer(GL_FRAMEBUFFER, mFBOs[0]);
502 
503     GLTexture texture;
504     glBindTexture(GL_TEXTURE_2D, texture);
505     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, getWindowWidth(), getWindowHeight(), 0, GL_RGBA,
506                  GL_UNSIGNED_BYTE, nullptr);
507     glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture, 0);
508 
509     // Initially clear to black.
510     glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
511     glClear(GL_COLOR_BUFFER_BIT);
512 
513     // Clear with masked color.
514     glColorMask(GL_TRUE, GL_FALSE, GL_TRUE, GL_TRUE);
515     glClearColor(0.5f, 0.5f, 0.5f, 0.75f);
516     glClear(GL_COLOR_BUFFER_BIT);
517     ASSERT_GL_NO_ERROR();
518 
519     // So far so good, we have an RGBA framebuffer that we've cleared to 0.5 everywhere.
520     EXPECT_PIXEL_NEAR(0, 0, 128, 0, 128, 192, 1.0);
521 
522     // In the Vulkan backend, RGB textures are emulated with an RGBA texture format
523     // underneath and we keep a special mask to know that we shouldn't touch the alpha
524     // channel when we have that emulated texture. This test exists to validate that
525     // this mask gets updated correctly when the framebuffer attachment changes.
526     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, getWindowWidth(), getWindowHeight(), 0, GL_RGB,
527                  GL_UNSIGNED_BYTE, nullptr);
528     glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture, 0);
529     ASSERT_GL_NO_ERROR();
530 
531     glDrawArrays(GL_TRIANGLES, 0, 6);
532     ASSERT_GL_NO_ERROR();
533 
534     EXPECT_PIXEL_RECT_EQ(0, 0, getWindowWidth(), getWindowHeight(), GLColor::magenta);
535 }
536 
537 // Test clearing a RGB8 Framebuffer with a color mask.
TEST_P(ClearTest,RGB8WithMaskFramebuffer)538 TEST_P(ClearTest, RGB8WithMaskFramebuffer)
539 {
540     glBindFramebuffer(GL_FRAMEBUFFER, mFBOs[0]);
541 
542     GLTexture texture;
543 
544     glBindTexture(GL_TEXTURE_2D, texture);
545     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, getWindowWidth(), getWindowHeight(), 0, GL_RGB,
546                  GL_UNSIGNED_BYTE, nullptr);
547     glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture, 0);
548 
549     glClearColor(0.2f, 0.4f, 0.6f, 0.8f);
550     glClear(GL_COLOR_BUFFER_BIT);
551 
552     // Since there's no alpha, we expect to get 255 back instead of the clear value (204).
553     EXPECT_PIXEL_NEAR(0, 0, 51, 102, 153, 255, 1.0);
554 
555     glColorMask(GL_TRUE, GL_TRUE, GL_FALSE, GL_TRUE);
556     glClearColor(0.1f, 0.3f, 0.5f, 0.7f);
557     glClear(GL_COLOR_BUFFER_BIT);
558 
559     // The blue channel was masked so its value should be unchanged.
560     EXPECT_PIXEL_NEAR(0, 0, 26, 77, 153, 255, 1.0);
561 
562     // Restore default.
563     glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
564 }
565 
TEST_P(ClearTest,ClearIssue)566 TEST_P(ClearTest, ClearIssue)
567 {
568     glEnable(GL_DEPTH_TEST);
569     glDepthFunc(GL_LEQUAL);
570 
571     glClearColor(0.0, 1.0, 0.0, 1.0);
572     glClearDepthf(0.0);
573     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
574 
575     EXPECT_GL_NO_ERROR();
576 
577     glBindFramebuffer(GL_FRAMEBUFFER, mFBOs[0]);
578 
579     GLRenderbuffer rbo;
580     glBindRenderbuffer(GL_RENDERBUFFER, rbo);
581     glRenderbufferStorage(GL_RENDERBUFFER, GL_RGB565, 16, 16);
582 
583     EXPECT_GL_NO_ERROR();
584 
585     glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, rbo);
586 
587     EXPECT_GL_NO_ERROR();
588 
589     glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
590     glClearDepthf(1.0f);
591     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
592 
593     EXPECT_GL_NO_ERROR();
594 
595     glBindFramebuffer(GL_FRAMEBUFFER, 0);
596     glBindBuffer(GL_ARRAY_BUFFER, 0);
597 
598     ANGLE_GL_PROGRAM(blueProgram, essl1_shaders::vs::Simple(), essl1_shaders::fs::Blue());
599     drawQuad(blueProgram, essl1_shaders::PositionAttrib(), 0.5f);
600 
601     EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::green);
602 }
603 
604 // Regression test for a bug where "glClearDepthf"'s argument was not clamped
605 // In GLES 2 they where declared as GLclampf and the behaviour is the same in GLES 3.2
TEST_P(ClearTest,ClearIsClamped)606 TEST_P(ClearTest, ClearIsClamped)
607 {
608     glClearDepthf(5.0f);
609 
610     GLfloat clear_depth;
611     glGetFloatv(GL_DEPTH_CLEAR_VALUE, &clear_depth);
612     EXPECT_EQ(1.0f, clear_depth);
613 }
614 
615 // Regression test for a bug where "glDepthRangef"'s arguments were not clamped
616 // In GLES 2 they where declared as GLclampf and the behaviour is the same in GLES 3.2
TEST_P(ClearTest,DepthRangefIsClamped)617 TEST_P(ClearTest, DepthRangefIsClamped)
618 {
619     glDepthRangef(1.1f, -4.0f);
620 
621     GLfloat depth_range[2];
622     glGetFloatv(GL_DEPTH_RANGE, depth_range);
623     EXPECT_EQ(1.0f, depth_range[0]);
624     EXPECT_EQ(0.0f, depth_range[1]);
625 }
626 
627 // Test scissored clears on Depth16
TEST_P(ClearTest,Depth16Scissored)628 TEST_P(ClearTest, Depth16Scissored)
629 {
630     GLRenderbuffer renderbuffer;
631     glBindRenderbuffer(GL_RENDERBUFFER, renderbuffer);
632     constexpr int kRenderbufferSize = 64;
633     glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, kRenderbufferSize,
634                           kRenderbufferSize);
635 
636     GLFramebuffer fbo;
637     glBindFramebuffer(GL_FRAMEBUFFER, fbo);
638     glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, renderbuffer);
639 
640     glClearDepthf(0.0f);
641     glClear(GL_DEPTH_BUFFER_BIT);
642 
643     glEnable(GL_SCISSOR_TEST);
644     constexpr int kNumSteps = 13;
645     for (int ndx = 1; ndx < kNumSteps; ndx++)
646     {
647         float perc = static_cast<float>(ndx) / static_cast<float>(kNumSteps);
648         glScissor(0, 0, static_cast<int>(kRenderbufferSize * perc),
649                   static_cast<int>(kRenderbufferSize * perc));
650         glClearDepthf(perc);
651         glClear(GL_DEPTH_BUFFER_BIT);
652     }
653 }
654 
655 // Test scissored clears on Stencil8
TEST_P(ClearTest,Stencil8Scissored)656 TEST_P(ClearTest, Stencil8Scissored)
657 {
658     GLRenderbuffer renderbuffer;
659     glBindRenderbuffer(GL_RENDERBUFFER, renderbuffer);
660     constexpr int kRenderbufferSize = 64;
661     glRenderbufferStorage(GL_RENDERBUFFER, GL_STENCIL_INDEX8, kRenderbufferSize, kRenderbufferSize);
662 
663     GLFramebuffer fbo;
664     glBindFramebuffer(GL_FRAMEBUFFER, fbo);
665     glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, renderbuffer);
666 
667     glClearStencil(0);
668     glClear(GL_STENCIL_BUFFER_BIT);
669 
670     glEnable(GL_SCISSOR_TEST);
671     constexpr int kNumSteps = 13;
672     for (int ndx = 1; ndx < kNumSteps; ndx++)
673     {
674         float perc = static_cast<float>(ndx) / static_cast<float>(kNumSteps);
675         glScissor(0, 0, static_cast<int>(kRenderbufferSize * perc),
676                   static_cast<int>(kRenderbufferSize * perc));
677         glClearStencil(static_cast<int>(perc * 255.0f));
678         glClear(GL_STENCIL_BUFFER_BIT);
679     }
680 }
681 
682 // Covers a bug in the Vulkan back-end where starting a new command buffer in
683 // the masked clear would not trigger descriptor sets to be re-bound.
TEST_P(ClearTest,MaskedClearThenDrawWithUniform)684 TEST_P(ClearTest, MaskedClearThenDrawWithUniform)
685 {
686     // Initialize a program with a uniform.
687     ANGLE_GL_PROGRAM(program, essl1_shaders::vs::Simple(), essl1_shaders::fs::UniformColor());
688     glUseProgram(program);
689 
690     GLint uniLoc = glGetUniformLocation(program, essl1_shaders::ColorUniform());
691     ASSERT_NE(-1, uniLoc);
692     glUniform4f(uniLoc, 0.0f, 1.0f, 0.0f, 1.0f);
693 
694     // Initialize position attribute.
695     GLint posLoc = glGetAttribLocation(program, essl1_shaders::PositionAttrib());
696     ASSERT_NE(-1, posLoc);
697     setupQuadVertexBuffer(0.5f, 1.0f);
698     glVertexAttribPointer(posLoc, 3, GL_FLOAT, GL_FALSE, 0, nullptr);
699     glEnableVertexAttribArray(posLoc);
700 
701     // Initialize a simple FBO.
702     constexpr GLsizei kSize = 2;
703     GLTexture clearTexture;
704     glBindTexture(GL_TEXTURE_2D, clearTexture);
705     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, kSize, kSize, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
706 
707     GLFramebuffer fbo;
708     glBindFramebuffer(GL_FRAMEBUFFER, fbo);
709     glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, clearTexture, 0);
710 
711     glViewport(0, 0, kSize, kSize);
712 
713     // Clear and draw to flush out dirty bits.
714     glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
715     glClear(GL_COLOR_BUFFER_BIT);
716 
717     glDrawArrays(GL_TRIANGLES, 0, 6);
718 
719     // Flush to trigger a new serial.
720     glFlush();
721 
722     // Enable color mask and draw again to trigger the bug.
723     glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_FALSE);
724     glClearColor(1.0f, 0.0f, 0.0f, 0.0f);
725     glClear(GL_COLOR_BUFFER_BIT);
726 
727     glDrawArrays(GL_TRIANGLES, 0, 6);
728 
729     ASSERT_GL_NO_ERROR();
730     EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::green);
731 }
732 
733 // Clear with a mask to verify that masked clear is done properly
734 // (can't use inline or RenderOp clear when some color channels are masked)
TEST_P(ClearTestES3,ClearPlusMaskDrawAndClear)735 TEST_P(ClearTestES3, ClearPlusMaskDrawAndClear)
736 {
737     // Initialize a program with a uniform.
738     ANGLE_GL_PROGRAM(program, essl1_shaders::vs::Simple(), essl1_shaders::fs::UniformColor());
739     glUseProgram(program);
740 
741     GLint uniLoc = glGetUniformLocation(program, essl1_shaders::ColorUniform());
742     ASSERT_NE(-1, uniLoc);
743     glUniform4f(uniLoc, 0.0f, 1.0f, 0.0f, 1.0f);
744 
745     // Initialize position attribute.
746     GLint posLoc = glGetAttribLocation(program, essl1_shaders::PositionAttrib());
747     ASSERT_NE(-1, posLoc);
748     setupQuadVertexBuffer(0.5f, 1.0f);
749     glVertexAttribPointer(posLoc, 3, GL_FLOAT, GL_FALSE, 0, nullptr);
750     glEnableVertexAttribArray(posLoc);
751 
752     // Initialize a simple FBO.
753     constexpr GLsizei kSize = 2;
754     GLTexture clearTexture;
755     glBindTexture(GL_TEXTURE_2D, clearTexture);
756     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, kSize, kSize, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
757 
758     GLFramebuffer fbo;
759     glBindFramebuffer(GL_FRAMEBUFFER, fbo);
760     glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, clearTexture, 0);
761 
762     GLRenderbuffer depthStencil;
763     glBindRenderbuffer(GL_RENDERBUFFER, depthStencil);
764     glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, kSize, kSize);
765     glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER,
766                               depthStencil);
767     ASSERT_GL_NO_ERROR();
768 
769     glViewport(0, 0, kSize, kSize);
770 
771     // Clear and draw to flush out dirty bits.
772     glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
773     glClearDepthf(1.0f);
774     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
775 
776     // Draw green rectangle
777     glDrawArrays(GL_TRIANGLES, 0, 6);
778 
779     // Enable color mask and draw again to trigger the bug.
780     glColorMask(GL_TRUE, GL_FALSE, GL_TRUE, GL_TRUE);
781     glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
782     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
783 
784     // Draw purple-ish rectangle, green should be masked off
785     glUniform4f(uniLoc, 1.0f, 0.25f, 1.0f, 1.0f);
786     glDrawArrays(GL_TRIANGLES, 0, 6);
787 
788     ASSERT_GL_NO_ERROR();
789     EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::white);
790 }
791 
792 // Test that clearing all buffers through glClearColor followed by a clear of a specific buffer
793 // clears to the correct values.
TEST_P(ClearTestES3,ClearMultipleAttachmentsFollowedBySpecificOne)794 TEST_P(ClearTestES3, ClearMultipleAttachmentsFollowedBySpecificOne)
795 {
796     constexpr uint32_t kSize            = 16;
797     constexpr uint32_t kAttachmentCount = 4;
798     std::vector<unsigned char> pixelData(kSize * kSize * 4, 255);
799 
800     glBindFramebuffer(GL_FRAMEBUFFER, mFBOs[0]);
801 
802     GLTexture textures[kAttachmentCount];
803     GLenum drawBuffers[kAttachmentCount];
804     GLColor clearValues[kAttachmentCount];
805 
806     for (uint32_t i = 0; i < kAttachmentCount; ++i)
807     {
808         glBindTexture(GL_TEXTURE_2D, textures[i]);
809         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, kSize, kSize, 0, GL_RGBA, GL_UNSIGNED_BYTE,
810                      pixelData.data());
811         glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + i, GL_TEXTURE_2D, textures[i],
812                                0);
813         drawBuffers[i] = GL_COLOR_ATTACHMENT0 + i;
814 
815         clearValues[i].R = static_cast<GLubyte>(1 + i * 20);
816         clearValues[i].G = static_cast<GLubyte>(7 + i * 20);
817         clearValues[i].B = static_cast<GLubyte>(12 + i * 20);
818         clearValues[i].A = static_cast<GLubyte>(16 + i * 20);
819     }
820 
821     glDrawBuffers(kAttachmentCount, drawBuffers);
822 
823     ASSERT_GL_NO_ERROR();
824     EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::white);
825 
826     // Clear all targets.
827     angle::Vector4 clearColor = clearValues[0].toNormalizedVector();
828     glClearColor(clearColor[0], clearColor[1], clearColor[2], clearColor[3]);
829     glClear(GL_COLOR_BUFFER_BIT);
830     ASSERT_GL_NO_ERROR();
831 
832     // Clear odd targets individually.
833     for (uint32_t i = 1; i < kAttachmentCount; i += 2)
834     {
835         clearColor = clearValues[i].toNormalizedVector();
836         glClearBufferfv(GL_COLOR, i, clearColor.data());
837     }
838 
839     // Even attachments should be cleared to color 0, while odd attachments are cleared to their
840     // respective color.
841     for (uint32_t i = 0; i < kAttachmentCount; ++i)
842     {
843         glReadBuffer(GL_COLOR_ATTACHMENT0 + i);
844         ASSERT_GL_NO_ERROR();
845 
846         uint32_t clearIndex   = i % 2 == 0 ? 0 : i;
847         const GLColor &expect = clearValues[clearIndex];
848 
849         EXPECT_PIXEL_COLOR_EQ(0, 0, expect);
850         EXPECT_PIXEL_COLOR_EQ(0, kSize - 1, expect);
851         EXPECT_PIXEL_COLOR_EQ(kSize - 1, 0, expect);
852         EXPECT_PIXEL_COLOR_EQ(kSize - 1, kSize - 1, expect);
853     }
854 }
855 
856 // Test that clearing each render target individually works.  In the Vulkan backend, this should be
857 // done in a single render pass.
TEST_P(ClearTestES3,ClearMultipleAttachmentsIndividually)858 TEST_P(ClearTestES3, ClearMultipleAttachmentsIndividually)
859 {
860     // http://anglebug.com/4855
861     ANGLE_SKIP_TEST_IF(IsWindows() && IsIntel() && IsVulkan());
862 
863     constexpr uint32_t kSize             = 16;
864     constexpr uint32_t kAttachmentCount  = 2;
865     constexpr float kDepthClearValue     = 0.125f;
866     constexpr int32_t kStencilClearValue = 0x67;
867     std::vector<unsigned char> pixelData(kSize * kSize * 4, 255);
868 
869     glBindFramebuffer(GL_FRAMEBUFFER, mFBOs[0]);
870 
871     GLTexture textures[kAttachmentCount];
872     GLRenderbuffer depthStencil;
873     GLenum drawBuffers[kAttachmentCount];
874     GLColor clearValues[kAttachmentCount];
875 
876     for (uint32_t i = 0; i < kAttachmentCount; ++i)
877     {
878         glBindTexture(GL_TEXTURE_2D, textures[i]);
879         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, kSize, kSize, 0, GL_RGBA, GL_UNSIGNED_BYTE,
880                      pixelData.data());
881         glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + i, GL_TEXTURE_2D, textures[i],
882                                0);
883         drawBuffers[i] = GL_COLOR_ATTACHMENT0 + i;
884 
885         clearValues[i].R = static_cast<GLubyte>(1 + i * 20);
886         clearValues[i].G = static_cast<GLubyte>(7 + i * 20);
887         clearValues[i].B = static_cast<GLubyte>(12 + i * 20);
888         clearValues[i].A = static_cast<GLubyte>(16 + i * 20);
889     }
890 
891     glBindRenderbuffer(GL_RENDERBUFFER, depthStencil);
892     glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, kSize, kSize);
893     glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER,
894                               depthStencil);
895 
896     glDrawBuffers(kAttachmentCount, drawBuffers);
897 
898     ASSERT_GL_NO_ERROR();
899     EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::white);
900 
901     for (uint32_t i = 0; i < kAttachmentCount; ++i)
902     {
903         glClearBufferfv(GL_COLOR, i, clearValues[i].toNormalizedVector().data());
904     }
905 
906     glClearBufferfv(GL_DEPTH, 0, &kDepthClearValue);
907     glClearBufferiv(GL_STENCIL, 0, &kStencilClearValue);
908     ASSERT_GL_NO_ERROR();
909 
910     for (uint32_t i = 0; i < kAttachmentCount; ++i)
911     {
912         glReadBuffer(GL_COLOR_ATTACHMENT0 + i);
913         ASSERT_GL_NO_ERROR();
914 
915         const GLColor &expect = clearValues[i];
916 
917         EXPECT_PIXEL_COLOR_EQ(0, 0, expect);
918         EXPECT_PIXEL_COLOR_EQ(0, kSize - 1, expect);
919         EXPECT_PIXEL_COLOR_EQ(kSize - 1, 0, expect);
920         EXPECT_PIXEL_COLOR_EQ(kSize - 1, kSize - 1, expect);
921     }
922 
923     glReadBuffer(GL_COLOR_ATTACHMENT0);
924     for (uint32_t i = 1; i < kAttachmentCount; ++i)
925         drawBuffers[i] = GL_NONE;
926     glDrawBuffers(kAttachmentCount, drawBuffers);
927 
928     verifyDepth(kDepthClearValue, kSize);
929     verifyStencil(kStencilClearValue, kSize);
930 }
931 
932 // Test that clearing multiple attachments in the presence of a color mask, scissor or both
933 // correctly clears all the attachments.
TEST_P(ClearTestES3,MaskedScissoredClearMultipleAttachments)934 TEST_P(ClearTestES3, MaskedScissoredClearMultipleAttachments)
935 {
936     constexpr uint32_t kSize            = 16;
937     constexpr uint32_t kAttachmentCount = 2;
938     std::vector<unsigned char> pixelData(kSize * kSize * 4, 255);
939 
940     glBindFramebuffer(GL_FRAMEBUFFER, mFBOs[0]);
941 
942     GLTexture textures[kAttachmentCount];
943     GLenum drawBuffers[kAttachmentCount];
944 
945     for (uint32_t i = 0; i < kAttachmentCount; ++i)
946     {
947         glBindTexture(GL_TEXTURE_2D, textures[i]);
948         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, kSize, kSize, 0, GL_RGBA, GL_UNSIGNED_BYTE,
949                      pixelData.data());
950         glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + i, GL_TEXTURE_2D, textures[i],
951                                0);
952         drawBuffers[i] = GL_COLOR_ATTACHMENT0 + i;
953     }
954 
955     glDrawBuffers(kAttachmentCount, drawBuffers);
956 
957     ASSERT_GL_NO_ERROR();
958     EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::white);
959 
960     // Masked clear
961     GLColor clearColorMasked(31, 63, 255, 191);
962     angle::Vector4 clearColor = GLColor(31, 63, 127, 191).toNormalizedVector();
963 
964     glColorMask(GL_TRUE, GL_TRUE, GL_FALSE, GL_TRUE);
965     glClearColor(clearColor[0], clearColor[1], clearColor[2], clearColor[3]);
966     glClear(GL_COLOR_BUFFER_BIT);
967     ASSERT_GL_NO_ERROR();
968 
969     // All attachments should be cleared, with the blue channel untouched
970     for (uint32_t i = 0; i < kAttachmentCount; ++i)
971     {
972         glReadBuffer(GL_COLOR_ATTACHMENT0 + i);
973         ASSERT_GL_NO_ERROR();
974 
975         EXPECT_PIXEL_COLOR_EQ(0, 0, clearColorMasked);
976         EXPECT_PIXEL_COLOR_EQ(0, kSize - 1, clearColorMasked);
977         EXPECT_PIXEL_COLOR_EQ(kSize - 1, 0, clearColorMasked);
978         EXPECT_PIXEL_COLOR_EQ(kSize - 1, kSize - 1, clearColorMasked);
979         EXPECT_PIXEL_COLOR_EQ(kSize / 2, kSize / 2, clearColorMasked);
980     }
981 
982     // Masked scissored clear
983     GLColor clearColorMaskedScissored(63, 127, 255, 31);
984     clearColor = GLColor(63, 127, 191, 31).toNormalizedVector();
985 
986     glClearColor(clearColor[0], clearColor[1], clearColor[2], clearColor[3]);
987     glEnable(GL_SCISSOR_TEST);
988     glScissor(kSize / 6, kSize / 6, kSize / 3, kSize / 3);
989     glClear(GL_COLOR_BUFFER_BIT);
990     ASSERT_GL_NO_ERROR();
991 
992     // The corners should keep the previous value while the center is cleared, except its blue
993     // channel.
994     for (uint32_t i = 0; i < kAttachmentCount; ++i)
995     {
996         glReadBuffer(GL_COLOR_ATTACHMENT0 + i);
997         ASSERT_GL_NO_ERROR();
998 
999         EXPECT_PIXEL_COLOR_EQ(0, 0, clearColorMasked);
1000         EXPECT_PIXEL_COLOR_EQ(0, kSize - 1, clearColorMasked);
1001         EXPECT_PIXEL_COLOR_EQ(kSize - 1, 0, clearColorMasked);
1002         EXPECT_PIXEL_COLOR_EQ(kSize - 1, kSize - 1, clearColorMasked);
1003         EXPECT_PIXEL_COLOR_EQ(kSize / 3, 2 * kSize / 3, clearColorMasked);
1004         EXPECT_PIXEL_COLOR_EQ(2 * kSize / 3, kSize / 3, clearColorMasked);
1005         EXPECT_PIXEL_COLOR_EQ(2 * kSize / 3, 2 * kSize / 3, clearColorMasked);
1006 
1007         EXPECT_PIXEL_COLOR_EQ(kSize / 3, kSize / 3, clearColorMaskedScissored);
1008     }
1009 
1010     // Scissored clear
1011     GLColor clearColorScissored(127, 191, 31, 63);
1012     clearColor = GLColor(127, 191, 31, 63).toNormalizedVector();
1013 
1014     glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
1015     glClearColor(clearColor[0], clearColor[1], clearColor[2], clearColor[3]);
1016     glClear(GL_COLOR_BUFFER_BIT);
1017     ASSERT_GL_NO_ERROR();
1018 
1019     // The corners should keep the old value while all channels of the center are cleared.
1020     for (uint32_t i = 0; i < kAttachmentCount; ++i)
1021     {
1022         glReadBuffer(GL_COLOR_ATTACHMENT0 + i);
1023         ASSERT_GL_NO_ERROR();
1024 
1025         EXPECT_PIXEL_COLOR_EQ(0, 0, clearColorMasked);
1026         EXPECT_PIXEL_COLOR_EQ(0, kSize - 1, clearColorMasked);
1027         EXPECT_PIXEL_COLOR_EQ(kSize - 1, 0, clearColorMasked);
1028         EXPECT_PIXEL_COLOR_EQ(kSize - 1, kSize - 1, clearColorMasked);
1029         EXPECT_PIXEL_COLOR_EQ(kSize / 3, 2 * kSize / 3, clearColorMasked);
1030         EXPECT_PIXEL_COLOR_EQ(2 * kSize / 3, kSize / 3, clearColorMasked);
1031         EXPECT_PIXEL_COLOR_EQ(2 * kSize / 3, 2 * kSize / 3, clearColorMasked);
1032 
1033         EXPECT_PIXEL_COLOR_EQ(kSize / 3, kSize / 3, clearColorScissored);
1034     }
1035 }
1036 
1037 // Test clearing multiple attachments in the presence of an indexed color mask.
TEST_P(ClearTestES3,MaskedIndexedClearMultipleAttachments)1038 TEST_P(ClearTestES3, MaskedIndexedClearMultipleAttachments)
1039 {
1040     ANGLE_SKIP_TEST_IF(!IsGLExtensionEnabled("GL_OES_draw_buffers_indexed"));
1041 
1042     constexpr uint32_t kSize            = 16;
1043     constexpr uint32_t kAttachmentCount = 4;
1044     std::vector<unsigned char> pixelData(kSize * kSize * 4, 255);
1045 
1046     glBindFramebuffer(GL_FRAMEBUFFER, mFBOs[0]);
1047 
1048     GLTexture textures[kAttachmentCount];
1049     GLenum drawBuffers[kAttachmentCount];
1050 
1051     for (uint32_t i = 0; i < kAttachmentCount; ++i)
1052     {
1053         glBindTexture(GL_TEXTURE_2D, textures[i]);
1054         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, kSize, kSize, 0, GL_RGBA, GL_UNSIGNED_BYTE,
1055                      pixelData.data());
1056         glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + i, GL_TEXTURE_2D, textures[i],
1057                                0);
1058         drawBuffers[i] = GL_COLOR_ATTACHMENT0 + i;
1059     }
1060 
1061     glDrawBuffers(kAttachmentCount, drawBuffers);
1062 
1063     ASSERT_GL_NO_ERROR();
1064     EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::white);
1065 
1066     // Masked clear
1067     GLColor clearColorMasked(31, 63, 255, 191);
1068     angle::Vector4 clearColor = GLColor(31, 63, 127, 191).toNormalizedVector();
1069 
1070     // Block blue channel for all attachements
1071     glColorMask(GL_TRUE, GL_TRUE, GL_FALSE, GL_TRUE);
1072 
1073     // Unblock blue channel for attachments 0 and 1
1074     glColorMaskiOES(0, GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
1075     glColorMaskiOES(1, GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
1076 
1077     glClearColor(clearColor[0], clearColor[1], clearColor[2], clearColor[3]);
1078     glClear(GL_COLOR_BUFFER_BIT);
1079     ASSERT_GL_NO_ERROR();
1080 
1081     // All attachments should be cleared, with the blue channel untouched for all attachments but 1.
1082     for (uint32_t i = 0; i < kAttachmentCount; ++i)
1083     {
1084         glReadBuffer(GL_COLOR_ATTACHMENT0 + i);
1085         ASSERT_GL_NO_ERROR();
1086 
1087         const GLColor attachmentColor = (i > 1) ? clearColorMasked : clearColor;
1088         EXPECT_PIXEL_COLOR_EQ(0, 0, attachmentColor);
1089         EXPECT_PIXEL_COLOR_EQ(0, kSize - 1, attachmentColor);
1090         EXPECT_PIXEL_COLOR_EQ(kSize - 1, 0, attachmentColor);
1091         EXPECT_PIXEL_COLOR_EQ(kSize - 1, kSize - 1, attachmentColor);
1092         EXPECT_PIXEL_COLOR_EQ(kSize / 2, kSize / 2, attachmentColor);
1093     }
1094 }
1095 
1096 // Test that clearing multiple attachments of different nature (float, int and uint) in the
1097 // presence of a color mask works correctly.  In the Vulkan backend, this exercises clearWithDraw
1098 // and the relevant internal shaders.
TEST_P(ClearTestES3,MaskedClearHeterogeneousAttachments)1099 TEST_P(ClearTestES3, MaskedClearHeterogeneousAttachments)
1100 {
1101     // http://anglebug.com/4855
1102     ANGLE_SKIP_TEST_IF(IsWindows() && IsIntel() && IsVulkan());
1103 
1104     // TODO(anglebug.com/5491)
1105     ANGLE_SKIP_TEST_IF(IsIOS() && IsOpenGLES());
1106 
1107     constexpr uint32_t kSize                              = 16;
1108     constexpr uint32_t kAttachmentCount                   = 3;
1109     constexpr float kDepthClearValue                      = 0.256f;
1110     constexpr int32_t kStencilClearValue                  = 0x1D;
1111     constexpr GLenum kAttachmentFormats[kAttachmentCount] = {
1112         GL_RGBA8,
1113         GL_RGBA8I,
1114         GL_RGBA8UI,
1115     };
1116     constexpr GLenum kDataFormats[kAttachmentCount] = {
1117         GL_RGBA,
1118         GL_RGBA_INTEGER,
1119         GL_RGBA_INTEGER,
1120     };
1121     constexpr GLenum kDataTypes[kAttachmentCount] = {
1122         GL_UNSIGNED_BYTE,
1123         GL_BYTE,
1124         GL_UNSIGNED_BYTE,
1125     };
1126 
1127     std::vector<unsigned char> pixelData(kSize * kSize * 4, 0);
1128 
1129     glBindFramebuffer(GL_FRAMEBUFFER, mFBOs[0]);
1130 
1131     GLTexture textures[kAttachmentCount];
1132     GLRenderbuffer depthStencil;
1133     GLenum drawBuffers[kAttachmentCount];
1134 
1135     for (uint32_t i = 0; i < kAttachmentCount; ++i)
1136     {
1137         glBindTexture(GL_TEXTURE_2D, textures[i]);
1138         glTexImage2D(GL_TEXTURE_2D, 0, kAttachmentFormats[i], kSize, kSize, 0, kDataFormats[i],
1139                      kDataTypes[i], pixelData.data());
1140         glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + i, GL_TEXTURE_2D, textures[i],
1141                                0);
1142         drawBuffers[i] = GL_COLOR_ATTACHMENT0 + i;
1143     }
1144 
1145     glBindRenderbuffer(GL_RENDERBUFFER, depthStencil);
1146     glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, kSize, kSize);
1147     glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER,
1148                               depthStencil);
1149 
1150     glDrawBuffers(kAttachmentCount, drawBuffers);
1151 
1152     ASSERT_GL_NO_ERROR();
1153     EXPECT_PIXEL_EQ(0, 0, 0, 0, 0, 0);
1154 
1155     // Mask out red for all clears
1156     glColorMask(GL_FALSE, GL_TRUE, GL_TRUE, GL_TRUE);
1157 
1158     glClearBufferfv(GL_DEPTH, 0, &kDepthClearValue);
1159     glClearBufferiv(GL_STENCIL, 0, &kStencilClearValue);
1160 
1161     GLColor clearValuef = {25, 50, 75, 100};
1162     glClearBufferfv(GL_COLOR, 0, clearValuef.toNormalizedVector().data());
1163 
1164     int clearValuei[4] = {10, -20, 30, -40};
1165     glClearBufferiv(GL_COLOR, 1, clearValuei);
1166 
1167     uint32_t clearValueui[4] = {50, 60, 70, 80};
1168     glClearBufferuiv(GL_COLOR, 2, clearValueui);
1169 
1170     ASSERT_GL_NO_ERROR();
1171 
1172     {
1173         glReadBuffer(GL_COLOR_ATTACHMENT0);
1174         ASSERT_GL_NO_ERROR();
1175 
1176         GLColor expect = clearValuef;
1177         expect.R       = 0;
1178 
1179         EXPECT_PIXEL_COLOR_EQ(0, 0, expect);
1180         EXPECT_PIXEL_COLOR_EQ(0, kSize - 1, expect);
1181         EXPECT_PIXEL_COLOR_EQ(kSize - 1, 0, expect);
1182         EXPECT_PIXEL_COLOR_EQ(kSize - 1, kSize - 1, expect);
1183     }
1184 
1185     {
1186         glReadBuffer(GL_COLOR_ATTACHMENT1);
1187         ASSERT_GL_NO_ERROR();
1188 
1189         EXPECT_PIXEL_8I(0, 0, 0, clearValuei[1], clearValuei[2], clearValuei[3]);
1190         EXPECT_PIXEL_8I(0, kSize - 1, 0, clearValuei[1], clearValuei[2], clearValuei[3]);
1191         EXPECT_PIXEL_8I(kSize - 1, 0, 0, clearValuei[1], clearValuei[2], clearValuei[3]);
1192         EXPECT_PIXEL_8I(kSize - 1, kSize - 1, 0, clearValuei[1], clearValuei[2], clearValuei[3]);
1193     }
1194 
1195     {
1196         glReadBuffer(GL_COLOR_ATTACHMENT2);
1197         ASSERT_GL_NO_ERROR();
1198 
1199         EXPECT_PIXEL_8UI(0, 0, 0, clearValueui[1], clearValueui[2], clearValueui[3]);
1200         EXPECT_PIXEL_8UI(0, kSize - 1, 0, clearValueui[1], clearValueui[2], clearValueui[3]);
1201         EXPECT_PIXEL_8UI(kSize - 1, 0, 0, clearValueui[1], clearValueui[2], clearValueui[3]);
1202         EXPECT_PIXEL_8UI(kSize - 1, kSize - 1, 0, clearValueui[1], clearValueui[2],
1203                          clearValueui[3]);
1204     }
1205 
1206     glReadBuffer(GL_COLOR_ATTACHMENT0);
1207     for (uint32_t i = 1; i < kAttachmentCount; ++i)
1208         drawBuffers[i] = GL_NONE;
1209     glDrawBuffers(kAttachmentCount, drawBuffers);
1210 
1211     verifyDepth(kDepthClearValue, kSize);
1212     verifyStencil(kStencilClearValue, kSize);
1213 }
1214 
1215 // Test that clearing multiple attachments of different nature (float, int and uint) in the
1216 // presence of a scissor test works correctly.  In the Vulkan backend, this exercises clearWithDraw
1217 // and the relevant internal shaders.
TEST_P(ClearTestES3,ScissoredClearHeterogeneousAttachments)1218 TEST_P(ClearTestES3, ScissoredClearHeterogeneousAttachments)
1219 {
1220     // http://anglebug.com/4855
1221     ANGLE_SKIP_TEST_IF(IsWindows() && IsIntel() && IsVulkan());
1222 
1223     // http://anglebug.com/5116
1224     ANGLE_SKIP_TEST_IF(IsWindows() && (IsOpenGL() || IsD3D11()) && IsAMD());
1225 
1226     // http://anglebug.com/5237
1227     ANGLE_SKIP_TEST_IF(IsWindows7() && IsD3D11() && IsNVIDIA());
1228 
1229     // TODO(anglebug.com/5491)
1230     ANGLE_SKIP_TEST_IF(IsIOS() && IsOpenGLES());
1231 
1232     constexpr uint32_t kSize                              = 16;
1233     constexpr uint32_t kHalfSize                          = kSize / 2;
1234     constexpr uint32_t kAttachmentCount                   = 3;
1235     constexpr float kDepthClearValue                      = 0.256f;
1236     constexpr int32_t kStencilClearValue                  = 0x1D;
1237     constexpr GLenum kAttachmentFormats[kAttachmentCount] = {
1238         GL_RGBA8,
1239         GL_RGBA8I,
1240         GL_RGBA8UI,
1241     };
1242     constexpr GLenum kDataFormats[kAttachmentCount] = {
1243         GL_RGBA,
1244         GL_RGBA_INTEGER,
1245         GL_RGBA_INTEGER,
1246     };
1247     constexpr GLenum kDataTypes[kAttachmentCount] = {
1248         GL_UNSIGNED_BYTE,
1249         GL_BYTE,
1250         GL_UNSIGNED_BYTE,
1251     };
1252 
1253     std::vector<unsigned char> pixelData(kSize * kSize * 4, 0);
1254 
1255     glBindFramebuffer(GL_FRAMEBUFFER, mFBOs[0]);
1256 
1257     GLTexture textures[kAttachmentCount];
1258     GLRenderbuffer depthStencil;
1259     GLenum drawBuffers[kAttachmentCount];
1260 
1261     for (uint32_t i = 0; i < kAttachmentCount; ++i)
1262     {
1263         glBindTexture(GL_TEXTURE_2D, textures[i]);
1264         glTexImage2D(GL_TEXTURE_2D, 0, kAttachmentFormats[i], kSize, kSize, 0, kDataFormats[i],
1265                      kDataTypes[i], pixelData.data());
1266         glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + i, GL_TEXTURE_2D, textures[i],
1267                                0);
1268         drawBuffers[i] = GL_COLOR_ATTACHMENT0 + i;
1269     }
1270 
1271     glBindRenderbuffer(GL_RENDERBUFFER, depthStencil);
1272     glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, kSize, kSize);
1273     glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER,
1274                               depthStencil);
1275 
1276     glDrawBuffers(kAttachmentCount, drawBuffers);
1277 
1278     ASSERT_GL_NO_ERROR();
1279     EXPECT_PIXEL_EQ(0, 0, 0, 0, 0, 0);
1280 
1281     // Enable scissor test
1282     glScissor(0, 0, kHalfSize, kHalfSize);
1283     glEnable(GL_SCISSOR_TEST);
1284 
1285     GLColor clearValuef         = {25, 50, 75, 100};
1286     angle::Vector4 clearValuefv = clearValuef.toNormalizedVector();
1287 
1288     glClearColor(clearValuefv.x(), clearValuefv.y(), clearValuefv.z(), clearValuefv.w());
1289     glClearDepthf(kDepthClearValue);
1290 
1291     // clear stencil.
1292     glClearBufferiv(GL_STENCIL, 0, &kStencilClearValue);
1293 
1294     // clear float color attachment & depth together
1295     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
1296 
1297     // clear integer attachment.
1298     int clearValuei[4] = {10, -20, 30, -40};
1299     glClearBufferiv(GL_COLOR, 1, clearValuei);
1300 
1301     // clear unsigned integer attachment
1302     uint32_t clearValueui[4] = {50, 60, 70, 80};
1303     glClearBufferuiv(GL_COLOR, 2, clearValueui);
1304 
1305     ASSERT_GL_NO_ERROR();
1306 
1307     {
1308         glReadBuffer(GL_COLOR_ATTACHMENT0);
1309         ASSERT_GL_NO_ERROR();
1310 
1311         GLColor expect = clearValuef;
1312 
1313         EXPECT_PIXEL_COLOR_EQ(0, 0, expect);
1314         EXPECT_PIXEL_COLOR_EQ(0, kHalfSize - 1, expect);
1315         EXPECT_PIXEL_COLOR_EQ(kHalfSize - 1, 0, expect);
1316         EXPECT_PIXEL_COLOR_EQ(kHalfSize - 1, kHalfSize - 1, expect);
1317         EXPECT_PIXEL_EQ(kHalfSize + 1, kHalfSize + 1, 0, 0, 0, 0);
1318     }
1319 
1320     {
1321         glReadBuffer(GL_COLOR_ATTACHMENT1);
1322         ASSERT_GL_NO_ERROR();
1323 
1324         EXPECT_PIXEL_8I(0, 0, clearValuei[0], clearValuei[1], clearValuei[2], clearValuei[3]);
1325         EXPECT_PIXEL_8I(0, kHalfSize - 1, clearValuei[0], clearValuei[1], clearValuei[2],
1326                         clearValuei[3]);
1327         EXPECT_PIXEL_8I(kHalfSize - 1, 0, clearValuei[0], clearValuei[1], clearValuei[2],
1328                         clearValuei[3]);
1329         EXPECT_PIXEL_8I(kHalfSize - 1, kHalfSize - 1, clearValuei[0], clearValuei[1],
1330                         clearValuei[2], clearValuei[3]);
1331         EXPECT_PIXEL_8I(kHalfSize + 1, kHalfSize + 1, 0, 0, 0, 0);
1332     }
1333 
1334     {
1335         glReadBuffer(GL_COLOR_ATTACHMENT2);
1336         ASSERT_GL_NO_ERROR();
1337 
1338         EXPECT_PIXEL_8UI(0, 0, clearValueui[0], clearValueui[1], clearValueui[2], clearValueui[3]);
1339         EXPECT_PIXEL_8UI(0, kHalfSize - 1, clearValueui[0], clearValueui[1], clearValueui[2],
1340                          clearValueui[3]);
1341         EXPECT_PIXEL_8UI(kHalfSize - 1, 0, clearValueui[0], clearValueui[1], clearValueui[2],
1342                          clearValueui[3]);
1343         EXPECT_PIXEL_8UI(kHalfSize - 1, kHalfSize - 1, clearValueui[0], clearValueui[1],
1344                          clearValueui[2], clearValueui[3]);
1345         EXPECT_PIXEL_8UI(kHalfSize + 1, kHalfSize + 1, 0, 0, 0, 0);
1346     }
1347 
1348     glReadBuffer(GL_COLOR_ATTACHMENT0);
1349     for (uint32_t i = 1; i < kAttachmentCount; ++i)
1350         drawBuffers[i] = GL_NONE;
1351     glDrawBuffers(kAttachmentCount, drawBuffers);
1352 
1353     verifyDepth(kDepthClearValue, kHalfSize);
1354     verifyStencil(kStencilClearValue, kHalfSize);
1355 }
1356 
1357 // This tests a bug where in a masked clear when calling "ClearBuffer", we would
1358 // mistakenly clear every channel (including the masked-out ones)
TEST_P(ClearTestES3,MaskedClearBufferBug)1359 TEST_P(ClearTestES3, MaskedClearBufferBug)
1360 {
1361     unsigned char pixelData[] = {255, 255, 255, 255};
1362 
1363     glBindFramebuffer(GL_FRAMEBUFFER, mFBOs[0]);
1364 
1365     GLTexture textures[2];
1366 
1367     glBindTexture(GL_TEXTURE_2D, textures[0]);
1368     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixelData);
1369     glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, textures[0], 0);
1370 
1371     glBindTexture(GL_TEXTURE_2D, textures[1]);
1372     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixelData);
1373     glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D, textures[1], 0);
1374 
1375     ASSERT_GL_NO_ERROR();
1376     EXPECT_PIXEL_EQ(0, 0, 255, 255, 255, 255);
1377 
1378     float clearValue[]   = {0, 0.5f, 0.5f, 1.0f};
1379     GLenum drawBuffers[] = {GL_NONE, GL_COLOR_ATTACHMENT1};
1380     glDrawBuffers(2, drawBuffers);
1381     glColorMask(GL_TRUE, GL_TRUE, GL_FALSE, GL_TRUE);
1382     glClearBufferfv(GL_COLOR, 1, clearValue);
1383 
1384     ASSERT_GL_NO_ERROR();
1385     EXPECT_PIXEL_EQ(0, 0, 255, 255, 255, 255);
1386 
1387     glReadBuffer(GL_COLOR_ATTACHMENT1);
1388     ASSERT_GL_NO_ERROR();
1389 
1390     EXPECT_PIXEL_NEAR(0, 0, 0, 127, 255, 255, 1);
1391 }
1392 
TEST_P(ClearTestES3,BadFBOSerialBug)1393 TEST_P(ClearTestES3, BadFBOSerialBug)
1394 {
1395     // First make a simple framebuffer, and clear it to green
1396     glBindFramebuffer(GL_FRAMEBUFFER, mFBOs[0]);
1397 
1398     GLTexture textures[2];
1399 
1400     glBindTexture(GL_TEXTURE_2D, textures[0]);
1401     glTexStorage2D(GL_TEXTURE_2D, 1, GL_RGBA8, getWindowWidth(), getWindowHeight());
1402     glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, textures[0], 0);
1403 
1404     GLenum drawBuffers[] = {GL_COLOR_ATTACHMENT0};
1405     glDrawBuffers(1, drawBuffers);
1406 
1407     float clearValues1[] = {0.0f, 1.0f, 0.0f, 1.0f};
1408     glClearBufferfv(GL_COLOR, 0, clearValues1);
1409 
1410     ASSERT_GL_NO_ERROR();
1411     EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::green);
1412 
1413     // Next make a second framebuffer, and draw it to red
1414     // (Triggers bad applied render target serial)
1415     GLFramebuffer fbo2;
1416     glBindFramebuffer(GL_FRAMEBUFFER, fbo2);
1417     ASSERT_GL_NO_ERROR();
1418 
1419     glBindTexture(GL_TEXTURE_2D, textures[1]);
1420     glTexStorage2D(GL_TEXTURE_2D, 1, GL_RGBA8, getWindowWidth(), getWindowHeight());
1421     glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, textures[1], 0);
1422 
1423     glDrawBuffers(1, drawBuffers);
1424 
1425     ANGLE_GL_PROGRAM(blueProgram, essl1_shaders::vs::Simple(), essl1_shaders::fs::Red());
1426     drawQuad(blueProgram, essl1_shaders::PositionAttrib(), 0.5f);
1427 
1428     ASSERT_GL_NO_ERROR();
1429     EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::red);
1430 
1431     // Check that the first framebuffer is still green.
1432     glBindFramebuffer(GL_FRAMEBUFFER, mFBOs[0]);
1433     EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::green);
1434 }
1435 
1436 // Test that SRGB framebuffers clear to the linearized clear color
TEST_P(ClearTestES3,SRGBClear)1437 TEST_P(ClearTestES3, SRGBClear)
1438 {
1439     // First make a simple framebuffer, and clear it
1440     glBindFramebuffer(GL_FRAMEBUFFER, mFBOs[0]);
1441 
1442     GLTexture texture;
1443 
1444     glBindTexture(GL_TEXTURE_2D, texture);
1445     glTexStorage2D(GL_TEXTURE_2D, 1, GL_SRGB8_ALPHA8, getWindowWidth(), getWindowHeight());
1446     glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture, 0);
1447 
1448     glClearColor(0.5f, 0.5f, 0.5f, 0.5f);
1449     glClear(GL_COLOR_BUFFER_BIT);
1450 
1451     EXPECT_PIXEL_NEAR(0, 0, 188, 188, 188, 128, 1.0);
1452 }
1453 
1454 // Test that framebuffers with mixed SRGB/Linear attachments clear to the correct color for each
1455 // attachment
TEST_P(ClearTestES3,MixedSRGBClear)1456 TEST_P(ClearTestES3, MixedSRGBClear)
1457 {
1458     glBindFramebuffer(GL_FRAMEBUFFER, mFBOs[0]);
1459 
1460     GLTexture textures[2];
1461 
1462     glBindTexture(GL_TEXTURE_2D, textures[0]);
1463     glTexStorage2D(GL_TEXTURE_2D, 1, GL_SRGB8_ALPHA8, getWindowWidth(), getWindowHeight());
1464     glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, textures[0], 0);
1465 
1466     glBindTexture(GL_TEXTURE_2D, textures[1]);
1467     glTexStorage2D(GL_TEXTURE_2D, 1, GL_RGBA8, getWindowWidth(), getWindowHeight());
1468     glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D, textures[1], 0);
1469 
1470     GLenum drawBuffers[] = {GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1};
1471     glDrawBuffers(2, drawBuffers);
1472 
1473     // Clear both textures
1474     glClearColor(0.5f, 0.5f, 0.5f, 0.5f);
1475     glClear(GL_COLOR_BUFFER_BIT);
1476 
1477     glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
1478     glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D, 0, 0);
1479 
1480     // Check value of texture0
1481     glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, textures[0], 0);
1482     EXPECT_PIXEL_NEAR(0, 0, 188, 188, 188, 128, 1.0);
1483 
1484     // Check value of texture1
1485     glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, textures[1], 0);
1486     EXPECT_PIXEL_NEAR(0, 0, 128, 128, 128, 128, 1.0);
1487 }
1488 
1489 // This test covers a D3D11 bug where calling ClearRenderTargetView sometimes wouldn't sync
1490 // before a draw call. The test draws small quads to a larger FBO (the default back buffer).
1491 // Before each blit to the back buffer it clears the quad to a certain color using
1492 // ClearBufferfv to give a solid color. The sync problem goes away if we insert a call to
1493 // flush or finish after ClearBufferfv or each draw.
TEST_P(ClearTestES3,RepeatedClear)1494 TEST_P(ClearTestES3, RepeatedClear)
1495 {
1496     // Fails on 431.02 driver. http://anglebug.com/3748
1497     ANGLE_SKIP_TEST_IF(IsWindows() && IsNVIDIA() && IsVulkan());
1498     ANGLE_SKIP_TEST_IF(IsARM64() && IsWindows() && IsD3D());
1499 
1500     constexpr char kVS[] =
1501         "#version 300 es\n"
1502         "in highp vec2 position;\n"
1503         "out highp vec2 v_coord;\n"
1504         "void main(void)\n"
1505         "{\n"
1506         "    gl_Position = vec4(position, 0, 1);\n"
1507         "    vec2 texCoord = (position * 0.5) + 0.5;\n"
1508         "    v_coord = texCoord;\n"
1509         "}\n";
1510 
1511     constexpr char kFS[] =
1512         "#version 300 es\n"
1513         "in highp vec2 v_coord;\n"
1514         "out highp vec4 color;\n"
1515         "uniform sampler2D tex;\n"
1516         "void main()\n"
1517         "{\n"
1518         "    color = texture(tex, v_coord);\n"
1519         "}\n";
1520 
1521     ANGLE_GL_PROGRAM(program, kVS, kFS);
1522 
1523     mTextures.resize(1, 0);
1524     glGenTextures(1, mTextures.data());
1525 
1526     GLenum format           = GL_RGBA8;
1527     const int numRowsCols   = 3;
1528     const int cellSize      = 32;
1529     const int fboSize       = cellSize;
1530     const int backFBOSize   = cellSize * numRowsCols;
1531     const float fmtValueMin = 0.0f;
1532     const float fmtValueMax = 1.0f;
1533 
1534     glBindTexture(GL_TEXTURE_2D, mTextures[0]);
1535     glTexStorage2D(GL_TEXTURE_2D, 1, format, fboSize, fboSize);
1536     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1537     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1538     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1539     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1540     ASSERT_GL_NO_ERROR();
1541 
1542     glBindFramebuffer(GL_FRAMEBUFFER, mFBOs[0]);
1543     glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, mTextures[0], 0);
1544     ASSERT_GL_NO_ERROR();
1545 
1546     ASSERT_GLENUM_EQ(GL_FRAMEBUFFER_COMPLETE, glCheckFramebufferStatus(GL_FRAMEBUFFER));
1547 
1548     // larger fbo bound -- clear to transparent black
1549     glUseProgram(program);
1550     GLint uniLoc = glGetUniformLocation(program, "tex");
1551     ASSERT_NE(-1, uniLoc);
1552     glUniform1i(uniLoc, 0);
1553     glBindTexture(GL_TEXTURE_2D, mTextures[0]);
1554 
1555     GLint positionLocation = glGetAttribLocation(program, "position");
1556     ASSERT_NE(-1, positionLocation);
1557 
1558     glUseProgram(program);
1559 
1560     for (int cellY = 0; cellY < numRowsCols; cellY++)
1561     {
1562         for (int cellX = 0; cellX < numRowsCols; cellX++)
1563         {
1564             int seed            = cellX + cellY * numRowsCols;
1565             const Vector4 color = RandomVec4(seed, fmtValueMin, fmtValueMax);
1566 
1567             glBindFramebuffer(GL_FRAMEBUFFER, mFBOs[0]);
1568             glClearBufferfv(GL_COLOR, 0, color.data());
1569 
1570             glBindFramebuffer(GL_FRAMEBUFFER, 0);
1571 
1572             // Method 1: Set viewport and draw full-viewport quad
1573             glViewport(cellX * cellSize, cellY * cellSize, cellSize, cellSize);
1574             drawQuad(program, "position", 0.5f);
1575 
1576             // Uncommenting the glFinish call seems to make the test pass.
1577             // glFinish();
1578         }
1579     }
1580 
1581     std::vector<GLColor> pixelData(backFBOSize * backFBOSize);
1582     glReadPixels(0, 0, backFBOSize, backFBOSize, GL_RGBA, GL_UNSIGNED_BYTE, pixelData.data());
1583 
1584     for (int cellY = 0; cellY < numRowsCols; cellY++)
1585     {
1586         for (int cellX = 0; cellX < numRowsCols; cellX++)
1587         {
1588             int seed            = cellX + cellY * numRowsCols;
1589             const Vector4 color = RandomVec4(seed, fmtValueMin, fmtValueMax);
1590             GLColor expectedColor(color);
1591 
1592             int testN = cellX * cellSize + cellY * backFBOSize * cellSize + backFBOSize + 1;
1593             GLColor actualColor = pixelData[testN];
1594             EXPECT_NEAR(expectedColor.R, actualColor.R, 1);
1595             EXPECT_NEAR(expectedColor.G, actualColor.G, 1);
1596             EXPECT_NEAR(expectedColor.B, actualColor.B, 1);
1597             EXPECT_NEAR(expectedColor.A, actualColor.A, 1);
1598         }
1599     }
1600 
1601     ASSERT_GL_NO_ERROR();
1602 }
1603 
1604 // Test that clearing RGB8 attachments work when verified through sampling.
TEST_P(ClearTestES3,ClearRGB8)1605 TEST_P(ClearTestES3, ClearRGB8)
1606 {
1607     GLFramebuffer fb;
1608     glBindFramebuffer(GL_FRAMEBUFFER, fb);
1609 
1610     GLTexture tex;
1611     glBindTexture(GL_TEXTURE_2D, tex);
1612     glTexStorage2D(GL_TEXTURE_2D, 1, GL_RGB8, 1, 1);
1613     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1614     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1615 
1616     glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, tex, 0);
1617     ASSERT_GL_FRAMEBUFFER_COMPLETE(GL_FRAMEBUFFER);
1618 
1619     // Clear the texture through framebuffer.
1620     const GLubyte kClearColor[] = {63, 127, 191, 55};
1621     glClearColor(kClearColor[0] / 255.0f, kClearColor[1] / 255.0f, kClearColor[2] / 255.0f,
1622                  kClearColor[3] / 255.0f);
1623     glClear(GL_COLOR_BUFFER_BIT);
1624     ASSERT_GL_NO_ERROR();
1625 
1626     glBindFramebuffer(GL_FRAMEBUFFER, 0);
1627 
1628     // Sample from it and verify clear is done.
1629     ANGLE_GL_PROGRAM(program, essl3_shaders::vs::Texture2DLod(), essl3_shaders::fs::Texture2DLod());
1630     glUseProgram(program);
1631     GLint textureLocation = glGetUniformLocation(program, essl3_shaders::Texture2DUniform());
1632     ASSERT_NE(-1, textureLocation);
1633     GLint lodLocation = glGetUniformLocation(program, essl3_shaders::LodUniform());
1634     ASSERT_NE(-1, lodLocation);
1635 
1636     glUniform1i(textureLocation, 0);
1637     glUniform1f(lodLocation, 0);
1638 
1639     drawQuad(program, essl3_shaders::PositionAttrib(), 0.5f);
1640 
1641     EXPECT_PIXEL_NEAR(0, 0, kClearColor[0], kClearColor[1], kClearColor[2], 255, 1);
1642     ASSERT_GL_NO_ERROR();
1643 }
1644 
1645 // Test that clearing RGB8 attachments from a 2D texture array does not cause
1646 // VUID-VkImageMemoryBarrier-oldLayout-01197
TEST_P(ClearTestES3,TextureArrayRGB8)1647 TEST_P(ClearTestES3, TextureArrayRGB8)
1648 {
1649     GLFramebuffer fb;
1650     glBindFramebuffer(GL_FRAMEBUFFER, fb);
1651 
1652     GLTexture tex;
1653     glBindTexture(GL_TEXTURE_2D_ARRAY, tex);
1654     glTexStorage3D(GL_TEXTURE_2D_ARRAY, 1, GL_RGB8, 1, 1, 2);
1655     glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1656     glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1657 
1658     glFramebufferTextureLayer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, tex, 0, 0);
1659     glFramebufferTextureLayer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, tex, 0, 1);
1660 
1661     ASSERT_GL_FRAMEBUFFER_COMPLETE(GL_FRAMEBUFFER);
1662 
1663     GLenum bufs[2] = {GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1};
1664     glDrawBuffers(2, &bufs[0]);
1665 
1666     glClearColor(1.0, 0.0, 1.0, 1.0);
1667     glClear(GL_COLOR_BUFFER_BIT);
1668     ASSERT_GL_NO_ERROR();
1669 
1670     glBindFramebuffer(GL_READ_FRAMEBUFFER, fb);
1671 
1672     glReadBuffer(GL_COLOR_ATTACHMENT0);
1673     EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::magenta);
1674 
1675     glReadBuffer(GL_COLOR_ATTACHMENT1);
1676     EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::magenta);
1677 
1678     EXPECT_GL_NO_ERROR();
1679 }
1680 
maskedScissoredColorDepthStencilClear(const MaskedScissoredClearVariationsTestParams & params)1681 void MaskedScissoredClearTestBase::maskedScissoredColorDepthStencilClear(
1682     const MaskedScissoredClearVariationsTestParams &params)
1683 {
1684     // Flaky on Android Nexus 5x and Pixel 2, possible Qualcomm driver bug.
1685     // TODO(jmadill): Re-enable when possible. http://anglebug.com/2548
1686     ANGLE_SKIP_TEST_IF(IsOpenGLES() && IsAndroid());
1687 
1688     const int w      = getWindowWidth();
1689     const int h      = getWindowHeight();
1690     const int wthird = w / 3;
1691     const int hthird = h / 3;
1692 
1693     constexpr float kPreClearDepth     = 0.9f;
1694     constexpr float kClearDepth        = 0.5f;
1695     constexpr uint8_t kPreClearStencil = 0xFF;
1696     constexpr uint8_t kClearStencil    = 0x16;
1697     constexpr uint8_t kStencilMask     = 0x59;
1698     constexpr uint8_t kMaskedClearStencil =
1699         (kPreClearStencil & ~kStencilMask) | (kClearStencil & kStencilMask);
1700 
1701     bool clearColor, clearDepth, clearStencil;
1702     bool maskColor, maskDepth, maskStencil;
1703     bool scissor;
1704 
1705     ParseMaskedScissoredClearVariationsTestParams(params, &clearColor, &clearDepth, &clearStencil,
1706                                                   &maskColor, &maskDepth, &maskStencil, &scissor);
1707 
1708     // Clear to a random color, 0.9 depth and 0x00 stencil
1709     Vector4 color1(0.1f, 0.2f, 0.3f, 0.4f);
1710     GLColor color1RGB(color1);
1711 
1712     glClearColor(color1[0], color1[1], color1[2], color1[3]);
1713     glClearDepthf(kPreClearDepth);
1714     glClearStencil(kPreClearStencil);
1715 
1716     if (!clearColor)
1717     {
1718         // If not asked to clear color, clear it anyway, but individually.  The clear value is
1719         // still used to verify that the depth/stencil clear happened correctly.  This allows
1720         // testing for depth/stencil-only clear implementations.
1721         glClear(GL_COLOR_BUFFER_BIT);
1722     }
1723 
1724     glClear((clearColor ? GL_COLOR_BUFFER_BIT : 0) | (clearDepth ? GL_DEPTH_BUFFER_BIT : 0) |
1725             (clearStencil ? GL_STENCIL_BUFFER_BIT : 0));
1726     ASSERT_GL_NO_ERROR();
1727 
1728     // Verify color was cleared correctly.
1729     EXPECT_PIXEL_COLOR_NEAR(0, 0, color1RGB, 1);
1730 
1731     if (scissor)
1732     {
1733         glEnable(GL_SCISSOR_TEST);
1734         glScissor(wthird / 2, hthird / 2, wthird, hthird);
1735     }
1736 
1737     // Use color and stencil masks to clear to a second color, 0.5 depth and 0x59 stencil.
1738     Vector4 color2(0.2f, 0.4f, 0.6f, 0.8f);
1739     GLColor color2RGB(color2);
1740     glClearColor(color2[0], color2[1], color2[2], color2[3]);
1741     glClearDepthf(kClearDepth);
1742     glClearStencil(kClearStencil);
1743     if (maskColor)
1744     {
1745         glColorMask(GL_TRUE, GL_FALSE, GL_TRUE, GL_FALSE);
1746     }
1747     if (maskDepth)
1748     {
1749         glDepthMask(GL_FALSE);
1750     }
1751     if (maskStencil)
1752     {
1753         glStencilMask(kStencilMask);
1754     }
1755     glClear((clearColor ? GL_COLOR_BUFFER_BIT : 0) | (clearDepth ? GL_DEPTH_BUFFER_BIT : 0) |
1756             (clearStencil ? GL_STENCIL_BUFFER_BIT : 0));
1757     glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
1758     glDepthMask(GL_TRUE);
1759     glStencilMask(0xFF);
1760     glDisable(GL_DEPTH_TEST);
1761     glDisable(GL_STENCIL_TEST);
1762     glDisable(GL_SCISSOR_TEST);
1763     ASSERT_GL_NO_ERROR();
1764 
1765     GLColor color2MaskedRGB(color2RGB[0], color1RGB[1], color2RGB[2], color1RGB[3]);
1766 
1767     // If not clearing color, the original color should be left both in the center and corners.  If
1768     // using a scissor, the corners should be left to the original color, while the center is
1769     // possibly changed.  If using a mask, the center (and corners if not scissored), changes to
1770     // the masked results.
1771     GLColor expectedCenterColorRGB = !clearColor ? color1RGB
1772                                      : maskColor ? color2MaskedRGB
1773                                                  : color2RGB;
1774     GLColor expectedCornerColorRGB = scissor ? color1RGB : expectedCenterColorRGB;
1775 
1776     // Verify second clear color mask worked as expected.
1777     EXPECT_PIXEL_COLOR_NEAR(wthird, hthird, expectedCenterColorRGB, 1);
1778 
1779     EXPECT_PIXEL_COLOR_NEAR(0, 0, expectedCornerColorRGB, 1);
1780     EXPECT_PIXEL_COLOR_NEAR(w - 1, 0, expectedCornerColorRGB, 1);
1781     EXPECT_PIXEL_COLOR_NEAR(0, h - 1, expectedCornerColorRGB, 1);
1782     EXPECT_PIXEL_COLOR_NEAR(w - 1, h - 1, expectedCornerColorRGB, 1);
1783     EXPECT_PIXEL_COLOR_NEAR(wthird, 2 * hthird, expectedCornerColorRGB, 1);
1784     EXPECT_PIXEL_COLOR_NEAR(2 * wthird, hthird, expectedCornerColorRGB, 1);
1785     EXPECT_PIXEL_COLOR_NEAR(2 * wthird, 2 * hthird, expectedCornerColorRGB, 1);
1786 
1787     // If there is depth, but depth is not asked to be cleared, the depth buffer contains garbage,
1788     // so no particular behavior can be expected.
1789     if (clearDepth || !mHasDepth)
1790     {
1791         // We use a small shader to verify depth.
1792         ANGLE_GL_PROGRAM(depthTestProgram, essl1_shaders::vs::Passthrough(),
1793                          essl1_shaders::fs::Blue());
1794         glEnable(GL_DEPTH_TEST);
1795         glDepthFunc(maskDepth ? GL_GREATER : GL_EQUAL);
1796         // - If depth is cleared, but it's masked, kPreClearDepth should be in the depth buffer.
1797         // - If depth is cleared, but it's not masked, kClearDepth should be in the depth buffer.
1798         // - If depth is not cleared, the if above ensures there is no depth buffer at all,
1799         //   which means depth test will always pass.
1800         drawQuad(depthTestProgram, essl1_shaders::PositionAttrib(), maskDepth ? 1.0f : 0.0f);
1801         glDisable(GL_DEPTH_TEST);
1802         ASSERT_GL_NO_ERROR();
1803 
1804         // Either way, we expect blue to be written to the center.
1805         expectedCenterColorRGB = GLColor::blue;
1806         // If there is no depth, depth test always passes so the whole image must be blue.  Same if
1807         // depth write is masked.
1808         expectedCornerColorRGB =
1809             mHasDepth && scissor && !maskDepth ? expectedCornerColorRGB : GLColor::blue;
1810 
1811         EXPECT_PIXEL_COLOR_NEAR(wthird, hthird, expectedCenterColorRGB, 1);
1812 
1813         EXPECT_PIXEL_COLOR_NEAR(0, 0, expectedCornerColorRGB, 1);
1814         EXPECT_PIXEL_COLOR_NEAR(w - 1, 0, expectedCornerColorRGB, 1);
1815         EXPECT_PIXEL_COLOR_NEAR(0, h - 1, expectedCornerColorRGB, 1);
1816         EXPECT_PIXEL_COLOR_NEAR(w - 1, h - 1, expectedCornerColorRGB, 1);
1817         EXPECT_PIXEL_COLOR_NEAR(wthird, 2 * hthird, expectedCornerColorRGB, 1);
1818         EXPECT_PIXEL_COLOR_NEAR(2 * wthird, hthird, expectedCornerColorRGB, 1);
1819         EXPECT_PIXEL_COLOR_NEAR(2 * wthird, 2 * hthird, expectedCornerColorRGB, 1);
1820     }
1821 
1822     // If there is stencil, but it's not asked to be cleared, there is similarly no expectation.
1823     if (clearStencil || !mHasStencil)
1824     {
1825         // And another small shader to verify stencil.
1826         ANGLE_GL_PROGRAM(stencilTestProgram, essl1_shaders::vs::Passthrough(),
1827                          essl1_shaders::fs::Green());
1828         glEnable(GL_STENCIL_TEST);
1829         // - If stencil is cleared, but it's masked, kMaskedClearStencil should be in the stencil
1830         //   buffer.
1831         // - If stencil is cleared, but it's not masked, kClearStencil should be in the stencil
1832         //   buffer.
1833         // - If stencil is not cleared, the if above ensures there is no stencil buffer at all,
1834         //   which means stencil test will always pass.
1835         glStencilFunc(GL_EQUAL, maskStencil ? kMaskedClearStencil : kClearStencil, 0xFF);
1836         drawQuad(stencilTestProgram, essl1_shaders::PositionAttrib(), 0.0f);
1837         glDisable(GL_STENCIL_TEST);
1838         ASSERT_GL_NO_ERROR();
1839 
1840         // Either way, we expect green to be written to the center.
1841         expectedCenterColorRGB = GLColor::green;
1842         // If there is no stencil, stencil test always passes so the whole image must be green.
1843         expectedCornerColorRGB = mHasStencil && scissor ? expectedCornerColorRGB : GLColor::green;
1844 
1845         EXPECT_PIXEL_COLOR_NEAR(wthird, hthird, expectedCenterColorRGB, 1);
1846 
1847         EXPECT_PIXEL_COLOR_NEAR(0, 0, expectedCornerColorRGB, 1);
1848         EXPECT_PIXEL_COLOR_NEAR(w - 1, 0, expectedCornerColorRGB, 1);
1849         EXPECT_PIXEL_COLOR_NEAR(0, h - 1, expectedCornerColorRGB, 1);
1850         EXPECT_PIXEL_COLOR_NEAR(w - 1, h - 1, expectedCornerColorRGB, 1);
1851         EXPECT_PIXEL_COLOR_NEAR(wthird, 2 * hthird, expectedCornerColorRGB, 1);
1852         EXPECT_PIXEL_COLOR_NEAR(2 * wthird, hthird, expectedCornerColorRGB, 1);
1853         EXPECT_PIXEL_COLOR_NEAR(2 * wthird, 2 * hthird, expectedCornerColorRGB, 1);
1854     }
1855 }
1856 
1857 // Tests combinations of color, depth, stencil clears with or without masks or scissor.
TEST_P(MaskedScissoredClearTest,Test)1858 TEST_P(MaskedScissoredClearTest, Test)
1859 {
1860     maskedScissoredColorDepthStencilClear(GetParam());
1861 }
1862 
1863 // Tests combinations of color, depth, stencil clears with or without masks or scissor.
1864 //
1865 // This uses depth/stencil attachments that are single-channel, but are emulated with a format
1866 // that has both channels.
TEST_P(VulkanClearTest,Test)1867 TEST_P(VulkanClearTest, Test)
1868 {
1869     bool clearColor, clearDepth, clearStencil;
1870     bool maskColor, maskDepth, maskStencil;
1871     bool scissor;
1872 
1873     ParseMaskedScissoredClearVariationsTestParams(GetParam(), &clearColor, &clearDepth,
1874                                                   &clearStencil, &maskColor, &maskDepth,
1875                                                   &maskStencil, &scissor);
1876 
1877     // We only care about clearing depth xor stencil.
1878     if (clearDepth == clearStencil)
1879     {
1880         return;
1881     }
1882 
1883     if (clearDepth)
1884     {
1885         // Creating a depth-only renderbuffer is an ES3 feature.
1886         ANGLE_SKIP_TEST_IF(getClientMajorVersion() < 3);
1887         bindColorDepthFBO();
1888     }
1889     else
1890     {
1891         bindColorStencilFBO();
1892     }
1893 
1894     maskedScissoredColorDepthStencilClear(GetParam());
1895 }
1896 
1897 // Tests that clearing a non existing attachment works.
TEST_P(ClearTest,ClearColorThenClearNonExistingDepthStencil)1898 TEST_P(ClearTest, ClearColorThenClearNonExistingDepthStencil)
1899 {
1900     constexpr GLsizei kSize = 16;
1901 
1902     GLTexture color;
1903     glBindTexture(GL_TEXTURE_2D, color);
1904     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, kSize, kSize, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
1905 
1906     GLFramebuffer fbo;
1907     glBindFramebuffer(GL_FRAMEBUFFER, fbo);
1908     glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, color, 0);
1909     ASSERT_GL_NO_ERROR();
1910     ASSERT_GL_FRAMEBUFFER_COMPLETE(GL_FRAMEBUFFER);
1911 
1912     // Clear color.
1913     glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
1914     glClear(GL_COLOR_BUFFER_BIT);
1915 
1916     // Clear depth/stencil.
1917     glClear(GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
1918     ASSERT_GL_NO_ERROR();
1919 
1920     // Read back color.
1921     EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::red);
1922 }
1923 
1924 // Tests that clearing a non existing attachment works.
TEST_P(ClearTestES3,ClearDepthStencilThenClearNonExistingColor)1925 TEST_P(ClearTestES3, ClearDepthStencilThenClearNonExistingColor)
1926 {
1927     constexpr GLsizei kSize = 16;
1928 
1929     GLRenderbuffer depth;
1930     glBindRenderbuffer(GL_RENDERBUFFER, depth);
1931     glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, kSize, kSize);
1932 
1933     GLFramebuffer fbo;
1934     glBindFramebuffer(GL_FRAMEBUFFER, fbo);
1935     glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, depth);
1936     ASSERT_GL_NO_ERROR();
1937     ASSERT_GL_FRAMEBUFFER_COMPLETE(GL_FRAMEBUFFER);
1938 
1939     // Clear depth/stencil.
1940     glClearDepthf(1.0f);
1941     glClearStencil(0xAA);
1942     glClear(GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
1943     ASSERT_GL_NO_ERROR();
1944 
1945     // Clear color.
1946     glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
1947     glClear(GL_COLOR_BUFFER_BIT);
1948     ASSERT_GL_NO_ERROR();
1949 }
1950 
1951 // Test that just clearing a nonexistent drawbuffer of the default framebuffer doesn't cause an
1952 // assert.
TEST_P(ClearTestES3,ClearBuffer1OnDefaultFramebufferNoAssert)1953 TEST_P(ClearTestES3, ClearBuffer1OnDefaultFramebufferNoAssert)
1954 {
1955     std::vector<GLuint> testUint(4);
1956     glClearBufferuiv(GL_COLOR, 1, testUint.data());
1957     std::vector<GLint> testInt(4);
1958     glClearBufferiv(GL_COLOR, 1, testInt.data());
1959     std::vector<GLfloat> testFloat(4);
1960     glClearBufferfv(GL_COLOR, 1, testFloat.data());
1961     EXPECT_GL_NO_ERROR();
1962 }
1963 
1964 // Clears many small concentric rectangles using scissor regions.
TEST_P(ClearTest,InceptionScissorClears)1965 TEST_P(ClearTest, InceptionScissorClears)
1966 {
1967     angle::RNG rng;
1968 
1969     constexpr GLuint kSize = 16;
1970 
1971     // Create a square user FBO so we have more control over the dimensions.
1972     GLFramebuffer fbo;
1973     glBindFramebuffer(GL_FRAMEBUFFER, fbo);
1974 
1975     GLRenderbuffer rbo;
1976     glBindRenderbuffer(GL_RENDERBUFFER, rbo);
1977     glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA8, kSize, kSize);
1978 
1979     glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, rbo);
1980     ASSERT_GL_FRAMEBUFFER_COMPLETE(GL_FRAMEBUFFER);
1981 
1982     glViewport(0, 0, kSize, kSize);
1983 
1984     // Draw small concentric squares using scissor.
1985     std::vector<GLColor> expectedColors;
1986     for (GLuint index = 0; index < (kSize - 1) / 2; index++)
1987     {
1988         // Do the first clear without the scissor.
1989         if (index > 0)
1990         {
1991             glEnable(GL_SCISSOR_TEST);
1992             glScissor(index, index, kSize - (index * 2), kSize - (index * 2));
1993         }
1994 
1995         GLColor color = RandomColor(&rng);
1996         expectedColors.push_back(color);
1997         Vector4 floatColor = color.toNormalizedVector();
1998         glClearColor(floatColor[0], floatColor[1], floatColor[2], floatColor[3]);
1999         glClear(GL_COLOR_BUFFER_BIT);
2000     }
2001 
2002     ASSERT_GL_NO_ERROR();
2003 
2004     std::vector<GLColor> actualColors(expectedColors.size());
2005     glReadPixels(0, kSize / 2, actualColors.size(), 1, GL_RGBA, GL_UNSIGNED_BYTE,
2006                  actualColors.data());
2007 
2008     EXPECT_EQ(expectedColors, actualColors);
2009 }
2010 
2011 // Clears many small concentric rectangles using scissor regions.
TEST_P(ClearTest,DrawThenInceptionScissorClears)2012 TEST_P(ClearTest, DrawThenInceptionScissorClears)
2013 {
2014     angle::RNG rng;
2015 
2016     constexpr GLuint kSize = 16;
2017 
2018     // Create a square user FBO so we have more control over the dimensions.
2019     GLFramebuffer fbo;
2020     glBindFramebuffer(GL_FRAMEBUFFER, fbo);
2021 
2022     GLRenderbuffer rbo;
2023     glBindRenderbuffer(GL_RENDERBUFFER, rbo);
2024     glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA8, kSize, kSize);
2025 
2026     glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, rbo);
2027     ASSERT_GL_FRAMEBUFFER_COMPLETE(GL_FRAMEBUFFER);
2028 
2029     glViewport(0, 0, kSize, kSize);
2030 
2031     ANGLE_GL_PROGRAM(redProgram, essl1_shaders::vs::Simple(), essl1_shaders::fs::Red());
2032     drawQuad(redProgram, essl1_shaders::PositionAttrib(), 0.5f);
2033     EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::red);
2034 
2035     // Draw small concentric squares using scissor.
2036     std::vector<GLColor> expectedColors;
2037     for (GLuint index = 0; index < (kSize - 1) / 2; index++)
2038     {
2039         // Do the first clear without the scissor.
2040         if (index > 0)
2041         {
2042             glEnable(GL_SCISSOR_TEST);
2043             glScissor(index, index, kSize - (index * 2), kSize - (index * 2));
2044         }
2045 
2046         GLColor color = RandomColor(&rng);
2047         expectedColors.push_back(color);
2048         Vector4 floatColor = color.toNormalizedVector();
2049         glClearColor(floatColor[0], floatColor[1], floatColor[2], floatColor[3]);
2050         glClear(GL_COLOR_BUFFER_BIT);
2051     }
2052 
2053     ASSERT_GL_NO_ERROR();
2054 
2055     std::vector<GLColor> actualColors(expectedColors.size());
2056     glReadPixels(0, kSize / 2, actualColors.size(), 1, GL_RGBA, GL_UNSIGNED_BYTE,
2057                  actualColors.data());
2058 
2059     EXPECT_EQ(expectedColors, actualColors);
2060 }
2061 
2062 // Test that clearBuffer with disabled non-zero drawbuffer or disabled read source doesn't cause an
2063 // assert.
TEST_P(ClearTestES3,ClearDisabledNonZeroAttachmentNoAssert)2064 TEST_P(ClearTestES3, ClearDisabledNonZeroAttachmentNoAssert)
2065 {
2066     // http://anglebug.com/4612
2067     ANGLE_SKIP_TEST_IF(IsMac() && IsDesktopOpenGL());
2068 
2069     GLFramebuffer fb;
2070     glBindFramebuffer(GL_FRAMEBUFFER, fb);
2071 
2072     GLRenderbuffer rb;
2073     glBindRenderbuffer(GL_RENDERBUFFER, rb);
2074     glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA8, 16, 16);
2075 
2076     glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_RENDERBUFFER, rb);
2077     glDrawBuffers(0, nullptr);
2078     glReadBuffer(GL_NONE);
2079 
2080     ASSERT_GL_FRAMEBUFFER_COMPLETE(GL_FRAMEBUFFER);
2081 
2082     float clearColorf[4] = {0.5, 0.5, 0.5, 0.5};
2083     glClearBufferfv(GL_COLOR, 1, clearColorf);
2084 
2085     GLuint clearColorui[4] = {255, 255, 255, 255};
2086     glClearBufferuiv(GL_COLOR, 1, clearColorui);
2087 
2088     GLint clearColori[4] = {-127, -127, -127, -127};
2089     glClearBufferiv(GL_COLOR, 1, clearColori);
2090 
2091     EXPECT_GL_NO_ERROR();
2092 }
2093 
2094 // Test that having a framebuffer with maximum number of attachments and clearing color, depth and
2095 // stencil works.
TEST_P(ClearTestES3,ClearMaxAttachments)2096 TEST_P(ClearTestES3, ClearMaxAttachments)
2097 {
2098     // http://anglebug.com/4612
2099     ANGLE_SKIP_TEST_IF(IsMac() && IsDesktopOpenGL());
2100     // http://anglebug.com/5397
2101     ANGLE_SKIP_TEST_IF(IsAMD() && IsD3D11());
2102 
2103     constexpr GLsizei kSize = 16;
2104 
2105     GLint maxDrawBuffers = 0;
2106     glGetIntegerv(GL_MAX_DRAW_BUFFERS, &maxDrawBuffers);
2107     ASSERT_GE(maxDrawBuffers, 4);
2108 
2109     // Setup framebuffer.
2110     GLFramebuffer fb;
2111     glBindFramebuffer(GL_FRAMEBUFFER, fb);
2112 
2113     std::vector<GLRenderbuffer> color(maxDrawBuffers);
2114     std::vector<GLenum> drawBuffers(maxDrawBuffers);
2115 
2116     for (GLint colorIndex = 0; colorIndex < maxDrawBuffers; ++colorIndex)
2117     {
2118         glBindRenderbuffer(GL_RENDERBUFFER, color[colorIndex]);
2119         glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA8, kSize, kSize);
2120         glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + colorIndex,
2121                                   GL_RENDERBUFFER, color[colorIndex]);
2122 
2123         drawBuffers[colorIndex] = GL_COLOR_ATTACHMENT0 + colorIndex;
2124     }
2125 
2126     GLRenderbuffer depthStencil;
2127     glBindRenderbuffer(GL_RENDERBUFFER, depthStencil);
2128     glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, kSize, kSize);
2129     glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER,
2130                               depthStencil);
2131 
2132     EXPECT_GL_NO_ERROR();
2133     ASSERT_GL_FRAMEBUFFER_COMPLETE(GL_FRAMEBUFFER);
2134 
2135     glDrawBuffers(maxDrawBuffers, drawBuffers.data());
2136 
2137     glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
2138     glClearDepthf(1.0f);
2139     glClearStencil(0x55);
2140     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
2141     EXPECT_GL_NO_ERROR();
2142 
2143     // Verify that every color attachment is cleared correctly.
2144     for (GLint colorIndex = 0; colorIndex < maxDrawBuffers; ++colorIndex)
2145     {
2146         glReadBuffer(GL_COLOR_ATTACHMENT0 + colorIndex);
2147 
2148         EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::red);
2149         EXPECT_PIXEL_COLOR_EQ(0, kSize - 1, GLColor::red);
2150         EXPECT_PIXEL_COLOR_EQ(kSize - 1, 0, GLColor::red);
2151         EXPECT_PIXEL_COLOR_EQ(kSize - 1, kSize - 1, GLColor::red);
2152     }
2153 
2154     // Verify that depth and stencil attachments are cleared correctly.
2155     GLFramebuffer fbVerify;
2156     glBindFramebuffer(GL_FRAMEBUFFER, fbVerify);
2157 
2158     glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, color[0]);
2159     glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER,
2160                               depthStencil);
2161 
2162     // If depth is not cleared to 1, rendering would fail.
2163     glEnable(GL_DEPTH_TEST);
2164     glDepthFunc(GL_LESS);
2165 
2166     // If stencil is not cleared to 0x55, rendering would fail.
2167     glEnable(GL_STENCIL_TEST);
2168     glStencilFunc(GL_EQUAL, 0x55, 0xFF);
2169     glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
2170     glStencilMask(0xFF);
2171 
2172     // Draw green.
2173     ANGLE_GL_PROGRAM(drawGreen, essl1_shaders::vs::Simple(), essl1_shaders::fs::Green());
2174     drawQuad(drawGreen, essl1_shaders::PositionAttrib(), 0.95f);
2175 
2176     // Verify that green was drawn.
2177     EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::green);
2178     EXPECT_PIXEL_COLOR_EQ(0, kSize - 1, GLColor::green);
2179     EXPECT_PIXEL_COLOR_EQ(kSize - 1, 0, GLColor::green);
2180     EXPECT_PIXEL_COLOR_EQ(kSize - 1, kSize - 1, GLColor::green);
2181 }
2182 
2183 // Test that having a framebuffer with maximum number of attachments and clearing color, depth and
2184 // stencil after a draw call works.
TEST_P(ClearTestES3,ClearMaxAttachmentsAfterDraw)2185 TEST_P(ClearTestES3, ClearMaxAttachmentsAfterDraw)
2186 {
2187     // http://anglebug.com/4612
2188     ANGLE_SKIP_TEST_IF(IsMac() && IsDesktopOpenGL());
2189 
2190     constexpr GLsizei kSize = 16;
2191 
2192     GLint maxDrawBuffers = 0;
2193     glGetIntegerv(GL_MAX_DRAW_BUFFERS, &maxDrawBuffers);
2194     ASSERT_GE(maxDrawBuffers, 4);
2195 
2196     // Setup framebuffer.
2197     GLFramebuffer fb;
2198     glBindFramebuffer(GL_FRAMEBUFFER, fb);
2199 
2200     std::vector<GLRenderbuffer> color(maxDrawBuffers);
2201     std::vector<GLenum> drawBuffers(maxDrawBuffers);
2202 
2203     for (GLint colorIndex = 0; colorIndex < maxDrawBuffers; ++colorIndex)
2204     {
2205         glBindRenderbuffer(GL_RENDERBUFFER, color[colorIndex]);
2206         glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA8, kSize, kSize);
2207         glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + colorIndex,
2208                                   GL_RENDERBUFFER, color[colorIndex]);
2209 
2210         drawBuffers[colorIndex] = GL_COLOR_ATTACHMENT0 + colorIndex;
2211     }
2212 
2213     GLRenderbuffer depthStencil;
2214     glBindRenderbuffer(GL_RENDERBUFFER, depthStencil);
2215     glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, kSize, kSize);
2216     glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER,
2217                               depthStencil);
2218 
2219     EXPECT_GL_NO_ERROR();
2220     ASSERT_GL_FRAMEBUFFER_COMPLETE(GL_FRAMEBUFFER);
2221 
2222     glDrawBuffers(maxDrawBuffers, drawBuffers.data());
2223 
2224     // Issue a draw call to render blue, depth=0 and stencil 0x3C to the attachments.
2225     glEnable(GL_DEPTH_TEST);
2226     glDepthFunc(GL_ALWAYS);
2227 
2228     glEnable(GL_STENCIL_TEST);
2229     glStencilFunc(GL_ALWAYS, 0x3C, 0xFF);
2230     glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE);
2231     glStencilMask(0xFF);
2232 
2233     // Generate shader for this framebuffer.
2234     std::stringstream strstr;
2235     strstr << "#version 300 es\n"
2236               "precision highp float;\n";
2237     for (GLint colorIndex = 0; colorIndex < maxDrawBuffers; ++colorIndex)
2238     {
2239         strstr << "layout(location = " << colorIndex << ") out vec4 value" << colorIndex << ";\n";
2240     }
2241     strstr << "void main()\n"
2242               "{\n";
2243     for (GLint colorIndex = 0; colorIndex < maxDrawBuffers; ++colorIndex)
2244     {
2245         strstr << "value" << colorIndex << " = vec4(0.0f, 0.0f, 1.0f, 1.0f);\n";
2246     }
2247     strstr << "}\n";
2248 
2249     ANGLE_GL_PROGRAM(drawMRT, essl3_shaders::vs::Simple(), strstr.str().c_str());
2250     drawQuad(drawMRT, essl3_shaders::PositionAttrib(), 0.0f);
2251     ASSERT_GL_NO_ERROR();
2252 
2253     glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
2254     glClearDepthf(1.0f);
2255     glClearStencil(0x55);
2256     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
2257     EXPECT_GL_NO_ERROR();
2258 
2259     // Verify that every color attachment is cleared correctly.
2260     for (GLint colorIndex = 0; colorIndex < maxDrawBuffers; ++colorIndex)
2261     {
2262         glReadBuffer(GL_COLOR_ATTACHMENT0 + colorIndex);
2263 
2264         EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::red) << colorIndex;
2265         EXPECT_PIXEL_COLOR_EQ(0, kSize - 1, GLColor::red) << colorIndex;
2266         EXPECT_PIXEL_COLOR_EQ(kSize - 1, 0, GLColor::red) << colorIndex;
2267         EXPECT_PIXEL_COLOR_EQ(kSize - 1, kSize - 1, GLColor::red) << colorIndex;
2268     }
2269 
2270     // Verify that depth and stencil attachments are cleared correctly.
2271     GLFramebuffer fbVerify;
2272     glBindFramebuffer(GL_FRAMEBUFFER, fbVerify);
2273 
2274     glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, color[0]);
2275     glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER,
2276                               depthStencil);
2277 
2278     // If depth is not cleared to 1, rendering would fail.
2279     glDepthFunc(GL_LESS);
2280 
2281     // If stencil is not cleared to 0x55, rendering would fail.
2282     glStencilFunc(GL_EQUAL, 0x55, 0xFF);
2283     glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
2284 
2285     // Draw green.
2286     ANGLE_GL_PROGRAM(drawGreen, essl1_shaders::vs::Simple(), essl1_shaders::fs::Green());
2287     drawQuad(drawGreen, essl1_shaders::PositionAttrib(), 0.95f);
2288 
2289     // Verify that green was drawn.
2290     EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::green);
2291     EXPECT_PIXEL_COLOR_EQ(0, kSize - 1, GLColor::green);
2292     EXPECT_PIXEL_COLOR_EQ(kSize - 1, 0, GLColor::green);
2293     EXPECT_PIXEL_COLOR_EQ(kSize - 1, kSize - 1, GLColor::green);
2294 }
2295 
2296 // Test that mixed masked clear works after clear.
TEST_P(ClearTestES3,ClearThenMixedMaskedClear)2297 TEST_P(ClearTestES3, ClearThenMixedMaskedClear)
2298 {
2299     constexpr GLsizei kSize = 16;
2300 
2301     // Setup framebuffer.
2302     GLRenderbuffer color;
2303     glBindRenderbuffer(GL_RENDERBUFFER, color);
2304     glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA8, kSize, kSize);
2305 
2306     GLRenderbuffer depthStencil;
2307     glBindRenderbuffer(GL_RENDERBUFFER, depthStencil);
2308     glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, kSize, kSize);
2309 
2310     GLFramebuffer fb;
2311     glBindFramebuffer(GL_FRAMEBUFFER, fb);
2312     glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, color);
2313     glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER,
2314                               depthStencil);
2315     EXPECT_GL_NO_ERROR();
2316     ASSERT_GL_FRAMEBUFFER_COMPLETE(GL_FRAMEBUFFER);
2317 
2318     // Clear color and depth/stencil
2319     glClearColor(0.1f, 1.0f, 0.0f, 0.7f);
2320     glClearDepthf(0.0f);
2321     glClearStencil(0x55);
2322     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
2323 
2324     // Clear again, but with color and stencil masked
2325     glClearColor(1.0f, 0.2f, 0.6f, 1.0f);
2326     glClearDepthf(1.0f);
2327     glClearStencil(0x3C);
2328     glColorMask(GL_TRUE, GL_FALSE, GL_FALSE, GL_TRUE);
2329     glStencilMask(0xF0);
2330     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
2331 
2332     // Issue a draw call to verify color, depth and stencil.
2333 
2334     // If depth is not cleared to 1, rendering would fail.
2335     glEnable(GL_DEPTH_TEST);
2336     glDepthFunc(GL_LESS);
2337 
2338     // If stencil is not cleared to 0x35, rendering would fail.
2339     glEnable(GL_STENCIL_TEST);
2340     glStencilFunc(GL_EQUAL, 0x35, 0xFF);
2341     glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
2342     glStencilMask(0xFF);
2343     glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
2344 
2345     ANGLE_GL_PROGRAM(drawColor, essl1_shaders::vs::Simple(), essl1_shaders::fs::UniformColor());
2346     glUseProgram(drawColor);
2347     GLint colorUniformLocation =
2348         glGetUniformLocation(drawColor, angle::essl1_shaders::ColorUniform());
2349     ASSERT_NE(colorUniformLocation, -1);
2350 
2351     // Blend half-transparent blue into the color buffer.
2352     glUniform4f(colorUniformLocation, 0.0f, 0.0f, 1.0f, 0.5f);
2353     glEnable(GL_BLEND);
2354     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
2355     drawQuad(drawColor, essl1_shaders::PositionAttrib(), 0.95f);
2356     ASSERT_GL_NO_ERROR();
2357 
2358     // Verify that the color buffer is now gray
2359     const GLColor kExpected(127, 127, 127, 191);
2360     EXPECT_PIXEL_COLOR_NEAR(0, 0, kExpected, 1);
2361     EXPECT_PIXEL_COLOR_NEAR(0, kSize - 1, kExpected, 1);
2362     EXPECT_PIXEL_COLOR_NEAR(kSize - 1, 0, kExpected, 1);
2363     EXPECT_PIXEL_COLOR_NEAR(kSize - 1, kSize - 1, kExpected, 1);
2364 }
2365 
2366 // Test that clearing stencil after a draw call works.
TEST_P(ClearTestES3,ClearStencilAfterDraw)2367 TEST_P(ClearTestES3, ClearStencilAfterDraw)
2368 {
2369     // http://anglebug.com/4612
2370     ANGLE_SKIP_TEST_IF(IsMac() && IsDesktopOpenGL());
2371 
2372     constexpr GLsizei kSize = 16;
2373 
2374     GLint maxDrawBuffers = 0;
2375     glGetIntegerv(GL_MAX_DRAW_BUFFERS, &maxDrawBuffers);
2376     ASSERT_GE(maxDrawBuffers, 4);
2377 
2378     // Setup framebuffer.
2379     GLFramebuffer fb;
2380     glBindFramebuffer(GL_FRAMEBUFFER, fb);
2381 
2382     std::vector<GLRenderbuffer> color(maxDrawBuffers);
2383     std::vector<GLenum> drawBuffers(maxDrawBuffers);
2384 
2385     for (GLint colorIndex = 0; colorIndex < maxDrawBuffers; ++colorIndex)
2386     {
2387         glBindRenderbuffer(GL_RENDERBUFFER, color[colorIndex]);
2388         glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA8, kSize, kSize);
2389         glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + colorIndex,
2390                                   GL_RENDERBUFFER, color[colorIndex]);
2391 
2392         drawBuffers[colorIndex] = GL_COLOR_ATTACHMENT0 + colorIndex;
2393     }
2394 
2395     GLRenderbuffer depthStencil;
2396     glBindRenderbuffer(GL_RENDERBUFFER, depthStencil);
2397     glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, kSize, kSize);
2398     glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER,
2399                               depthStencil);
2400 
2401     EXPECT_GL_NO_ERROR();
2402     ASSERT_GL_FRAMEBUFFER_COMPLETE(GL_FRAMEBUFFER);
2403 
2404     glDrawBuffers(maxDrawBuffers, drawBuffers.data());
2405 
2406     // Issue a draw call to render blue and stencil 0x3C to the attachments.
2407     glEnable(GL_STENCIL_TEST);
2408     glStencilFunc(GL_ALWAYS, 0x3C, 0xFF);
2409     glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE);
2410     glStencilMask(0xFF);
2411 
2412     // Generate shader for this framebuffer.
2413     std::stringstream strstr;
2414     strstr << "#version 300 es\n"
2415               "precision highp float;\n";
2416     for (GLint colorIndex = 0; colorIndex < maxDrawBuffers; ++colorIndex)
2417     {
2418         strstr << "layout(location = " << colorIndex << ") out vec4 value" << colorIndex << ";\n";
2419     }
2420     strstr << "void main()\n"
2421               "{\n";
2422     for (GLint colorIndex = 0; colorIndex < maxDrawBuffers; ++colorIndex)
2423     {
2424         strstr << "value" << colorIndex << " = vec4(0.0f, 0.0f, 1.0f, 1.0f);\n";
2425     }
2426     strstr << "}\n";
2427 
2428     ANGLE_GL_PROGRAM(drawMRT, essl3_shaders::vs::Simple(), strstr.str().c_str());
2429     drawQuad(drawMRT, essl3_shaders::PositionAttrib(), 0.0f);
2430     ASSERT_GL_NO_ERROR();
2431 
2432     glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
2433     glClearStencil(0x55);
2434     glClear(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
2435     EXPECT_GL_NO_ERROR();
2436 
2437     // Verify that every color attachment is cleared correctly.
2438     for (GLint colorIndex = 0; colorIndex < maxDrawBuffers; ++colorIndex)
2439     {
2440         glReadBuffer(GL_COLOR_ATTACHMENT0 + colorIndex);
2441 
2442         EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::red);
2443         EXPECT_PIXEL_COLOR_EQ(0, kSize - 1, GLColor::red);
2444         EXPECT_PIXEL_COLOR_EQ(kSize - 1, 0, GLColor::red);
2445         EXPECT_PIXEL_COLOR_EQ(kSize - 1, kSize - 1, GLColor::red);
2446     }
2447 
2448     // Verify that depth and stencil attachments are cleared correctly.
2449     GLFramebuffer fbVerify;
2450     glBindFramebuffer(GL_FRAMEBUFFER, fbVerify);
2451 
2452     glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, color[0]);
2453     glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER,
2454                               depthStencil);
2455 
2456     // If stencil is not cleared to 0x55, rendering would fail.
2457     glStencilFunc(GL_EQUAL, 0x55, 0xFF);
2458     glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
2459 
2460     // Draw green.
2461     ANGLE_GL_PROGRAM(drawGreen, essl1_shaders::vs::Simple(), essl1_shaders::fs::Green());
2462     drawQuad(drawGreen, essl1_shaders::PositionAttrib(), 0.95f);
2463 
2464     // Verify that green was drawn.
2465     EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::green);
2466     EXPECT_PIXEL_COLOR_EQ(0, kSize - 1, GLColor::green);
2467     EXPECT_PIXEL_COLOR_EQ(kSize - 1, 0, GLColor::green);
2468     EXPECT_PIXEL_COLOR_EQ(kSize - 1, kSize - 1, GLColor::green);
2469 }
2470 
2471 // Test that mid-render pass clearing of mixed used and unused color attachments works.
TEST_P(ClearTestES3,MixedRenderPassClearMixedUsedUnusedAttachments)2472 TEST_P(ClearTestES3, MixedRenderPassClearMixedUsedUnusedAttachments)
2473 {
2474     // http://anglebug.com/4612
2475     ANGLE_SKIP_TEST_IF(IsMac() && IsDesktopOpenGL());
2476 
2477     constexpr GLsizei kSize = 16;
2478 
2479     // Setup framebuffer.
2480     GLFramebuffer fb;
2481     glBindFramebuffer(GL_FRAMEBUFFER, fb);
2482 
2483     GLRenderbuffer color[2];
2484 
2485     for (GLint colorIndex = 0; colorIndex < 2; ++colorIndex)
2486     {
2487         glBindRenderbuffer(GL_RENDERBUFFER, color[colorIndex]);
2488         glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA8, kSize, kSize);
2489         glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + colorIndex,
2490                                   GL_RENDERBUFFER, color[colorIndex]);
2491     }
2492     EXPECT_GL_NO_ERROR();
2493     ASSERT_GL_FRAMEBUFFER_COMPLETE(GL_FRAMEBUFFER);
2494 
2495     // Disable color attachment 0.
2496     GLenum drawBuffers[] = {GL_NONE, GL_COLOR_ATTACHMENT1};
2497     glDrawBuffers(2, drawBuffers);
2498 
2499     // Draw into color attachment 1
2500     constexpr char kFS[] = R"(#version 300 es
2501 precision highp float;
2502 layout(location = 0) out vec4 color0;
2503 layout(location = 1) out vec4 color1;
2504 void main()
2505 {
2506     color0 = vec4(0, 0, 1, 1);
2507     color1 = vec4(1, 0, 0, 1);
2508 })";
2509 
2510     ANGLE_GL_PROGRAM(drawMRT, essl3_shaders::vs::Simple(), kFS);
2511     drawQuad(drawMRT, essl3_shaders::PositionAttrib(), 0.0f);
2512     ASSERT_GL_NO_ERROR();
2513 
2514     // Color attachment 0 is now uninitialized, while color attachment 1 is red.
2515     // Re-enable color attachment 0 and clear both attachments to green.
2516     drawBuffers[0] = GL_COLOR_ATTACHMENT0;
2517     glDrawBuffers(2, drawBuffers);
2518 
2519     glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
2520     glClear(GL_COLOR_BUFFER_BIT);
2521     EXPECT_GL_NO_ERROR();
2522 
2523     // Verify that both color attachments are now green.
2524     glReadBuffer(GL_COLOR_ATTACHMENT0);
2525     EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::green);
2526     glReadBuffer(GL_COLOR_ATTACHMENT1);
2527     EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::green);
2528     EXPECT_GL_NO_ERROR();
2529 }
2530 
2531 // Test that draw without state change after masked clear works
TEST_P(ClearTestES3,DrawClearThenDrawWithoutStateChange)2532 TEST_P(ClearTestES3, DrawClearThenDrawWithoutStateChange)
2533 {
2534     swapBuffers();
2535     constexpr GLsizei kSize = 16;
2536 
2537     // Setup framebuffer.
2538     GLRenderbuffer color;
2539     glBindRenderbuffer(GL_RENDERBUFFER, color);
2540     glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA8, kSize, kSize);
2541 
2542     GLFramebuffer fb;
2543     glBindFramebuffer(GL_FRAMEBUFFER, fb);
2544     glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, color);
2545     EXPECT_GL_NO_ERROR();
2546     ASSERT_GL_FRAMEBUFFER_COMPLETE(GL_FRAMEBUFFER);
2547 
2548     // Clear color initially.
2549     glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
2550     glClear(GL_COLOR_BUFFER_BIT);
2551 
2552     // Mask color.
2553     glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_FALSE);
2554 
2555     ANGLE_GL_PROGRAM(drawColor, essl1_shaders::vs::Simple(), essl1_shaders::fs::UniformColor());
2556     glUseProgram(drawColor);
2557     GLint colorUniformLocation =
2558         glGetUniformLocation(drawColor, angle::essl1_shaders::ColorUniform());
2559     ASSERT_NE(colorUniformLocation, -1);
2560 
2561     // Initialize position attribute.
2562     GLint posLoc = glGetAttribLocation(drawColor, essl1_shaders::PositionAttrib());
2563     ASSERT_NE(-1, posLoc);
2564     setupQuadVertexBuffer(0.5f, 1.0f);
2565     glVertexAttribPointer(posLoc, 3, GL_FLOAT, GL_FALSE, 0, nullptr);
2566     glEnableVertexAttribArray(posLoc);
2567 
2568     // Draw red.
2569     glViewport(0, 0, kSize, kSize);
2570     glClearColor(0.0f, 1.0f, 0.0f, 0.0f);
2571     glUniform4f(colorUniformLocation, 1.0f, 0.0f, 0.0f, 0.5f);
2572     glDrawArrays(GL_TRIANGLES, 0, 6);
2573 
2574     // Clear to green without any state change.
2575     glClear(GL_COLOR_BUFFER_BIT);
2576 
2577     // Draw red again without any state change.
2578     glDrawArrays(GL_TRIANGLES, 0, 6);
2579 
2580     // Verify that the color buffer is now red
2581     EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::red);
2582     EXPECT_PIXEL_COLOR_EQ(0, kSize - 1, GLColor::red);
2583     EXPECT_PIXEL_COLOR_EQ(kSize - 1, 0, GLColor::red);
2584     EXPECT_PIXEL_COLOR_EQ(kSize - 1, kSize - 1, GLColor::red);
2585 }
2586 
2587 // Test that clear stencil value is correctly masked to 8 bits.
TEST_P(ClearTest,ClearStencilMask)2588 TEST_P(ClearTest, ClearStencilMask)
2589 {
2590     GLint stencilBits = 0;
2591     glGetIntegerv(GL_STENCIL_BITS, &stencilBits);
2592     EXPECT_EQ(stencilBits, 8);
2593 
2594     ANGLE_GL_PROGRAM(drawColor, essl1_shaders::vs::Simple(), essl1_shaders::fs::Green());
2595     glUseProgram(drawColor);
2596 
2597     glClearColor(0, 0, 0, 1);
2598     glClear(GL_COLOR_BUFFER_BIT);
2599     EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::black);
2600 
2601     // Clear stencil value must be masked to 0x42
2602     glClearStencil(0x142);
2603     glClear(GL_STENCIL_BUFFER_BIT);
2604 
2605     // Check that the stencil test works as expected
2606     glEnable(GL_STENCIL_TEST);
2607 
2608     // Negative case
2609     glStencilFunc(GL_NOTEQUAL, 0x42, 0xFF);
2610     drawQuad(drawColor, essl1_shaders::PositionAttrib(), 0.5f);
2611     EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::black);
2612 
2613     // Positive case
2614     glStencilFunc(GL_EQUAL, 0x42, 0xFF);
2615     drawQuad(drawColor, essl1_shaders::PositionAttrib(), 0.5f);
2616     EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::green);
2617 
2618     ASSERT_GL_NO_ERROR();
2619 }
2620 
2621 // Test that glClearBufferiv correctly masks the clear stencil value.
TEST_P(ClearTestES3,ClearBufferivStencilMask)2622 TEST_P(ClearTestES3, ClearBufferivStencilMask)
2623 {
2624     GLint stencilBits = 0;
2625     glGetIntegerv(GL_STENCIL_BITS, &stencilBits);
2626     EXPECT_EQ(stencilBits, 8);
2627 
2628     ANGLE_GL_PROGRAM(drawColor, essl1_shaders::vs::Simple(), essl1_shaders::fs::Green());
2629     glUseProgram(drawColor);
2630 
2631     glClearColor(0, 0, 0, 1);
2632     glClear(GL_COLOR_BUFFER_BIT);
2633     EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::black);
2634 
2635     // Clear stencil value must be masked to 0x42
2636     const GLint kStencilClearValue = 0x142;
2637     glClearBufferiv(GL_STENCIL, 0, &kStencilClearValue);
2638 
2639     // Check that the stencil test works as expected
2640     glEnable(GL_STENCIL_TEST);
2641 
2642     // Negative case
2643     glStencilFunc(GL_NOTEQUAL, 0x42, 0xFF);
2644     drawQuad(drawColor, essl1_shaders::PositionAttrib(), 0.5f);
2645     EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::black);
2646 
2647     // Positive case
2648     glStencilFunc(GL_EQUAL, 0x42, 0xFF);
2649     drawQuad(drawColor, essl1_shaders::PositionAttrib(), 0.5f);
2650     EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::green);
2651 
2652     ASSERT_GL_NO_ERROR();
2653 }
2654 
2655 // Test that glClearBufferfi correctly masks the clear stencil value.
TEST_P(ClearTestES3,ClearBufferfiStencilMask)2656 TEST_P(ClearTestES3, ClearBufferfiStencilMask)
2657 {
2658     GLint stencilBits = 0;
2659     glGetIntegerv(GL_STENCIL_BITS, &stencilBits);
2660     EXPECT_EQ(stencilBits, 8);
2661 
2662     ANGLE_GL_PROGRAM(drawColor, essl1_shaders::vs::Simple(), essl1_shaders::fs::Green());
2663     glUseProgram(drawColor);
2664 
2665     glClearColor(0, 0, 0, 1);
2666     glClear(GL_COLOR_BUFFER_BIT);
2667     EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::black);
2668 
2669     // Clear stencil value must be masked to 0x42
2670     glClearBufferfi(GL_DEPTH_STENCIL, 0, 0.5f, 0x142);
2671 
2672     // Check that the stencil test works as expected
2673     glEnable(GL_STENCIL_TEST);
2674 
2675     // Negative case
2676     glStencilFunc(GL_NOTEQUAL, 0x42, 0xFF);
2677     drawQuad(drawColor, essl1_shaders::PositionAttrib(), 0.5f);
2678     EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::black);
2679 
2680     // Positive case
2681     glStencilFunc(GL_EQUAL, 0x42, 0xFF);
2682     drawQuad(drawColor, essl1_shaders::PositionAttrib(), 0.5f);
2683     EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::green);
2684 
2685     ASSERT_GL_NO_ERROR();
2686 }
2687 
2688 // Test that glClearBufferfi works when stencil attachment is not present.
TEST_P(ClearTestES3,ClearBufferfiNoStencilAttachment)2689 TEST_P(ClearTestES3, ClearBufferfiNoStencilAttachment)
2690 {
2691     constexpr GLsizei kSize = 16;
2692 
2693     GLRenderbuffer color;
2694     glBindRenderbuffer(GL_RENDERBUFFER, color);
2695     glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA8, kSize, kSize);
2696 
2697     GLRenderbuffer depth;
2698     glBindRenderbuffer(GL_RENDERBUFFER, depth);
2699     glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, kSize, kSize);
2700 
2701     GLFramebuffer fbo;
2702     glBindFramebuffer(GL_FRAMEBUFFER, fbo);
2703     glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, color);
2704     glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depth);
2705     EXPECT_GL_FRAMEBUFFER_COMPLETE(GL_FRAMEBUFFER);
2706     EXPECT_GL_NO_ERROR();
2707 
2708     // Clear depth/stencil with glClearBufferfi.  Note that the stencil attachment doesn't exist.
2709     glClearBufferfi(GL_DEPTH_STENCIL, 0, 0.5f, 0x55);
2710     EXPECT_GL_NO_ERROR();
2711 
2712     // Verify depth is cleared correctly.
2713     verifyDepth(0.5f, kSize);
2714 }
2715 
2716 // Test that scissored clear followed by non-scissored draw works.  Ensures that when scissor size
2717 // is expanded, the clear operation remains limited to the scissor region.  Written to catch
2718 // potential future bugs if loadOp=CLEAR is used in the Vulkan backend for a small render pass and
2719 // then the render area is mistakenly enlarged.
TEST_P(ClearTest,ScissoredClearThenNonScissoredDraw)2720 TEST_P(ClearTest, ScissoredClearThenNonScissoredDraw)
2721 {
2722     constexpr GLsizei kSize = 16;
2723     const std::vector<GLColor> kInitialData(kSize * kSize, GLColor::red);
2724 
2725     // Setup framebuffer.  Initialize color with red.
2726     GLTexture color;
2727     glBindTexture(GL_TEXTURE_2D, color);
2728     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, kSize, kSize, 0, GL_RGBA, GL_UNSIGNED_BYTE,
2729                  kInitialData.data());
2730 
2731     GLFramebuffer fb;
2732     glBindFramebuffer(GL_FRAMEBUFFER, fb);
2733     glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, color, 0);
2734     EXPECT_GL_NO_ERROR();
2735     ASSERT_GL_FRAMEBUFFER_COMPLETE(GL_FRAMEBUFFER);
2736 
2737     // Issue a scissored clear to green.
2738     glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
2739     glScissor(kSize / 2, 0, kSize / 2, kSize);
2740     glEnable(GL_SCISSOR_TEST);
2741     glClear(GL_COLOR_BUFFER_BIT);
2742 
2743     // Expand the scissor and blend blue into the framebuffer.
2744     glScissor(0, 0, kSize, kSize);
2745     glEnable(GL_BLEND);
2746     glBlendFunc(GL_ONE, GL_ONE);
2747 
2748     ANGLE_GL_PROGRAM(drawBlue, essl1_shaders::vs::Simple(), essl1_shaders::fs::Blue());
2749     drawQuad(drawBlue, essl1_shaders::PositionAttrib(), 0.5f);
2750     ASSERT_GL_NO_ERROR();
2751 
2752     // Verify that the left half is magenta, and the right half is cyan.
2753     EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::magenta);
2754     EXPECT_PIXEL_COLOR_EQ(kSize / 2 - 1, 0, GLColor::magenta);
2755     EXPECT_PIXEL_COLOR_EQ(0, kSize - 1, GLColor::magenta);
2756     EXPECT_PIXEL_COLOR_EQ(kSize / 2 - 1, kSize - 1, GLColor::magenta);
2757 
2758     EXPECT_PIXEL_COLOR_EQ(kSize / 2, 0, GLColor::cyan);
2759     EXPECT_PIXEL_COLOR_EQ(kSize / 2, kSize - 1, GLColor::cyan);
2760     EXPECT_PIXEL_COLOR_EQ(kSize - 1, 0, GLColor::cyan);
2761     EXPECT_PIXEL_COLOR_EQ(kSize - 1, kSize - 1, GLColor::cyan);
2762 }
2763 
2764 // Test that clear followed by a scissored masked clear works.
TEST_P(ClearTest,ClearThenScissoredMaskedClear)2765 TEST_P(ClearTest, ClearThenScissoredMaskedClear)
2766 {
2767     constexpr GLsizei kSize = 16;
2768 
2769     // Setup framebuffer
2770     GLTexture color;
2771     glBindTexture(GL_TEXTURE_2D, color);
2772     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, kSize, kSize, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
2773 
2774     GLFramebuffer fb;
2775     glBindFramebuffer(GL_FRAMEBUFFER, fb);
2776     glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, color, 0);
2777     EXPECT_GL_NO_ERROR();
2778     ASSERT_GL_FRAMEBUFFER_COMPLETE(GL_FRAMEBUFFER);
2779 
2780     // Clear to red.
2781     glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
2782     glClear(GL_COLOR_BUFFER_BIT);
2783 
2784     // Mask red and clear to green with a scissor
2785     glColorMask(GL_FALSE, GL_TRUE, GL_TRUE, GL_TRUE);
2786     glScissor(0, 0, kSize / 2, kSize);
2787     glEnable(GL_SCISSOR_TEST);
2788     glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
2789     glClear(GL_COLOR_BUFFER_BIT);
2790 
2791     // Verify that the left half is yellow, and the right half is red.
2792     EXPECT_PIXEL_RECT_EQ(0, 0, kSize / 2, kSize, GLColor::yellow);
2793     EXPECT_PIXEL_RECT_EQ(kSize / 2, 0, kSize / 2, kSize, GLColor::red);
2794 }
2795 
2796 // Test that a scissored stencil clear followed by a full clear works.
TEST_P(ClearTestES3,StencilScissoredClearThenFullClear)2797 TEST_P(ClearTestES3, StencilScissoredClearThenFullClear)
2798 {
2799     constexpr GLsizei kSize = 128;
2800 
2801     GLint stencilBits = 0;
2802     glGetIntegerv(GL_STENCIL_BITS, &stencilBits);
2803     EXPECT_EQ(stencilBits, 8);
2804 
2805     // Clear stencil value must be masked to 0x42
2806     glClearBufferfi(GL_DEPTH_STENCIL, 0, 0.5f, 0x142);
2807 
2808     glClearColor(1, 0, 0, 1);
2809     glClear(GL_COLOR_BUFFER_BIT);
2810     EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::red);
2811 
2812     // Shrink the render area.
2813     glScissor(kSize / 2, 0, kSize / 2, kSize);
2814     glEnable(GL_SCISSOR_TEST);
2815 
2816     // Clear stencil.
2817     glClearBufferfi(GL_DEPTH_STENCIL, 0, 0.5f, 0x64);
2818 
2819     // Grow the render area.
2820     glScissor(0, 0, kSize, kSize);
2821     glEnable(GL_SCISSOR_TEST);
2822 
2823     // Check that the stencil test works as expected
2824     glEnable(GL_STENCIL_TEST);
2825 
2826     // Scissored region is green, outside is red (clear color)
2827     glStencilFunc(GL_EQUAL, 0x64, 0xFF);
2828     ANGLE_GL_PROGRAM(drawGreen, essl3_shaders::vs::Simple(), essl3_shaders::fs::Green());
2829     glUseProgram(drawGreen);
2830     drawQuad(drawGreen, essl3_shaders::PositionAttrib(), 0.5f);
2831     EXPECT_PIXEL_RECT_EQ(0, 0, kSize / 2, kSize, GLColor::red);
2832     EXPECT_PIXEL_RECT_EQ(kSize / 2, 0, kSize / 2, kSize, GLColor::green);
2833 
2834     // Outside scissored region is blue.
2835     glStencilFunc(GL_EQUAL, 0x42, 0xFF);
2836     ANGLE_GL_PROGRAM(drawBlue, essl3_shaders::vs::Simple(), essl3_shaders::fs::Blue());
2837     glUseProgram(drawBlue);
2838     drawQuad(drawBlue, essl3_shaders::PositionAttrib(), 0.5f);
2839     EXPECT_PIXEL_RECT_EQ(0, 0, kSize / 2, kSize, GLColor::blue);
2840     EXPECT_PIXEL_RECT_EQ(kSize / 2, 0, kSize / 2, kSize, GLColor::green);
2841 
2842     ASSERT_GL_NO_ERROR();
2843 }
2844 
2845 // This is a test that must be verified visually.
2846 //
2847 // Tests that clear of the default framebuffer applies to the window.
TEST_P(ClearTest,DISABLED_ClearReachesWindow)2848 TEST_P(ClearTest, DISABLED_ClearReachesWindow)
2849 {
2850     ANGLE_GL_PROGRAM(blueProgram, essl1_shaders::vs::Simple(), essl1_shaders::fs::Blue());
2851 
2852     // Draw blue.
2853     drawQuad(blueProgram, essl1_shaders::PositionAttrib(), 0.5f);
2854     swapBuffers();
2855 
2856     // Use glClear to clear to red.  Regression test for the Vulkan backend where this clear
2857     // remained "deferred" and didn't make it to the window on swap.
2858     glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
2859     glClear(GL_COLOR_BUFFER_BIT);
2860     swapBuffers();
2861 
2862     // Wait for visual verification.
2863     angle::Sleep(2000);
2864 }
2865 
2866 // Tests that masked clear after a no-op framebuffer binding change with an open render pass works.
TEST_P(ClearTest,DrawThenChangeFBOBindingAndBackThenMaskedClear)2867 TEST_P(ClearTest, DrawThenChangeFBOBindingAndBackThenMaskedClear)
2868 {
2869     ANGLE_GL_PROGRAM(blueProgram, essl1_shaders::vs::Simple(), essl1_shaders::fs::Blue());
2870 
2871     // Draw blue.
2872     drawQuad(blueProgram, essl1_shaders::PositionAttrib(), 0.5f);
2873 
2874     // Change framebuffer and back
2875     glBindFramebuffer(GL_FRAMEBUFFER, mFBOs[0]);
2876     glBindFramebuffer(GL_FRAMEBUFFER, 0);
2877 
2878     // Masked clear
2879     glColorMask(1, 0, 0, 1);
2880     glClearColor(1.0f, 0.5f, 0.5f, 1.0f);
2881     glClear(GL_COLOR_BUFFER_BIT);
2882 
2883     EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::magenta);
2884 }
2885 
2886 // Test that clearing slices of a 3D texture and reading them back works.
TEST_P(ClearTestES3,ClearAndReadPixels3DTexture)2887 TEST_P(ClearTestES3, ClearAndReadPixels3DTexture)
2888 {
2889     constexpr uint32_t kWidth  = 128;
2890     constexpr uint32_t kHeight = 128;
2891     constexpr uint32_t kDepth  = 7;
2892 
2893     GLTexture texture;
2894     glBindTexture(GL_TEXTURE_3D, texture);
2895     glTexStorage3D(GL_TEXTURE_3D, 1, GL_RGBA8, kWidth, kHeight, kDepth);
2896     glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAX_LEVEL, 0);
2897 
2898     std::array<GLColor, kDepth> clearColors = {
2899         GLColor::red,  GLColor::green,   GLColor::blue,  GLColor::yellow,
2900         GLColor::cyan, GLColor::magenta, GLColor::white,
2901     };
2902 
2903     GLFramebuffer framebuffer;
2904     glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
2905 
2906     for (uint32_t z = 0; z < kDepth; ++z)
2907     {
2908         glFramebufferTextureLayer(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, texture, 0, z);
2909         glClearBufferfv(GL_COLOR, 0, clearColors[z].toNormalizedVector().data());
2910     }
2911 
2912     for (uint32_t z = 0; z < kDepth; ++z)
2913     {
2914         glFramebufferTextureLayer(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, texture, 0, z);
2915         EXPECT_PIXEL_COLOR_EQ(0, 0, clearColors[z]);
2916     }
2917 }
2918 
2919 // Test that clearing stencil with zero first byte in mask doesn't crash.
TEST_P(ClearTestES3,ClearStencilZeroFirstByteMask)2920 TEST_P(ClearTestES3, ClearStencilZeroFirstByteMask)
2921 {
2922     glStencilMask(0xe7d6a900);
2923     glClear(GL_STENCIL_BUFFER_BIT);
2924 }
2925 
2926 // Test that mid render pass clear after draw sets the render pass size correctly.
TEST_P(ClearTestES3,ScissoredDrawThenFullClear)2927 TEST_P(ClearTestES3, ScissoredDrawThenFullClear)
2928 {
2929     const int w = getWindowWidth();
2930     const int h = getWindowHeight();
2931 
2932     // Use viewport to imply scissor on the draw call
2933     glViewport(w / 4, h / 4, w / 2, h / 2);
2934 
2935     ANGLE_GL_PROGRAM(program, essl1_shaders::vs::Passthrough(), essl1_shaders::fs::Blue());
2936     drawQuad(program, essl1_shaders::PositionAttrib(), 0);
2937 
2938     // Mid-render-pass clear without scissor or viewport change, which covers the whole framebuffer.
2939     glClearColor(1, 1, 0, 1);
2940     glClear(GL_COLOR_BUFFER_BIT);
2941 
2942     EXPECT_PIXEL_RECT_EQ(0, 0, w, h, GLColor::yellow);
2943 }
2944 
2945 // Test that mid render pass clear after masked clear sets the render pass size correctly.
TEST_P(ClearTestES3,MaskedScissoredClearThenFullClear)2946 TEST_P(ClearTestES3, MaskedScissoredClearThenFullClear)
2947 {
2948     const int w = getWindowWidth();
2949     const int h = getWindowHeight();
2950 
2951     // Use viewport to imply a small scissor on (non-existing) draw calls.  This is important to
2952     // make sure render area that's derived from scissor+viewport for draw calls doesn't
2953     // accidentally fix render area derived from scissor for clear calls.
2954     glViewport(w / 2, h / 2, 1, 1);
2955 
2956     glEnable(GL_SCISSOR_TEST);
2957     glScissor(w / 4, h / 4, w / 2, h / 2);
2958     glColorMask(GL_TRUE, GL_FALSE, GL_TRUE, GL_FALSE);
2959     glClearColor(0.13, 0.38, 0.87, 0.65);
2960     glClear(GL_COLOR_BUFFER_BIT);
2961 
2962     // Mid-render-pass clear without scissor, which covers the whole framebuffer.
2963     glDisable(GL_SCISSOR_TEST);
2964     glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
2965     glClearColor(1, 1, 0, 1);
2966     glClear(GL_COLOR_BUFFER_BIT);
2967 
2968     EXPECT_PIXEL_RECT_EQ(0, 0, w, h, GLColor::yellow);
2969 }
2970 
2971 // Test that mid render pass masked clear after masked clear sets the render pass size correctly.
TEST_P(ClearTestES3,MaskedScissoredClearThenFullMaskedClear)2972 TEST_P(ClearTestES3, MaskedScissoredClearThenFullMaskedClear)
2973 {
2974     const int w = getWindowWidth();
2975     const int h = getWindowHeight();
2976 
2977     // Make sure the framebuffer is initialized.
2978     glClearColor(0, 0, 0, 1);
2979     glClear(GL_COLOR_BUFFER_BIT);
2980     EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::black);
2981 
2982     // Use viewport to imply a small scissor on (non-existing) draw calls  This is important to
2983     // make sure render area that's derived from scissor+viewport for draw calls doesn't
2984     // accidentally fix render area derived from scissor for clear calls.
2985     glViewport(w / 2, h / 2, 1, 1);
2986 
2987     glEnable(GL_SCISSOR_TEST);
2988     glScissor(w / 4, h / 4, w / 2, h / 2);
2989     glColorMask(GL_TRUE, GL_FALSE, GL_TRUE, GL_FALSE);
2990     glClearColor(1, 1, 0, 1);
2991     glClear(GL_COLOR_BUFFER_BIT);
2992 
2993     // Mid-render-pass clear without scissor, which covers the whole framebuffer.
2994     glDisable(GL_SCISSOR_TEST);
2995     glColorMask(GL_FALSE, GL_TRUE, GL_FALSE, GL_TRUE);
2996     glClearColor(1, 1, 1, 1);
2997     glClear(GL_COLOR_BUFFER_BIT);
2998 
2999     EXPECT_PIXEL_RECT_EQ(0, 0, w / 4, h, GLColor::green);
3000     EXPECT_PIXEL_RECT_EQ(w / 4, 0, w / 2, h / 4, GLColor::green);
3001     EXPECT_PIXEL_RECT_EQ(w / 4, 3 * h / 4, w / 2, h / 4, GLColor::green);
3002     EXPECT_PIXEL_RECT_EQ(3 * w / 4, 0, w / 4, h, GLColor::green);
3003 
3004     EXPECT_PIXEL_RECT_EQ(w / 4, h / 4, w / 2, h / 2, GLColor::yellow);
3005 }
3006 
3007 // Test that reclearing color to the same value works.
TEST_P(ClearTestES3,RepeatedColorClear)3008 TEST_P(ClearTestES3, RepeatedColorClear)
3009 {
3010     glClearColor(1, 1, 0, 1);
3011     glClear(GL_COLOR_BUFFER_BIT);
3012     EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::yellow);
3013 
3014     glClear(GL_COLOR_BUFFER_BIT);
3015     EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::yellow);
3016 
3017     ASSERT_GL_NO_ERROR();
3018 }
3019 
3020 // Test that reclearing depth to the same value works.
TEST_P(ClearTestES3,RepeatedDepthClear)3021 TEST_P(ClearTestES3, RepeatedDepthClear)
3022 {
3023     glClearDepthf(0.25f);
3024     glClear(GL_DEPTH_BUFFER_BIT);
3025 
3026     verifyDepth(0.25f, 1);
3027 
3028     glClear(GL_DEPTH_BUFFER_BIT);
3029 
3030     verifyDepth(0.25f, 1);
3031 
3032     ASSERT_GL_NO_ERROR();
3033 }
3034 
3035 // Test that reclearing stencil to the same value works.
TEST_P(ClearTestES3,RepeatedStencilClear)3036 TEST_P(ClearTestES3, RepeatedStencilClear)
3037 {
3038     glClearStencil(0xE4);
3039     glClear(GL_STENCIL_BUFFER_BIT);
3040     verifyStencil(0xE4, 1);
3041 
3042     glClear(GL_STENCIL_BUFFER_BIT);
3043     verifyStencil(0xE4, 1);
3044 
3045     ASSERT_GL_NO_ERROR();
3046 }
3047 
3048 // Test that reclearing color to the same value works if color was written to in between with a draw
3049 // call.
TEST_P(ClearTestES3,RepeatedColorClearWithDrawInBetween)3050 TEST_P(ClearTestES3, RepeatedColorClearWithDrawInBetween)
3051 {
3052     glClearColor(1, 0, 0, 1);
3053     glClear(GL_COLOR_BUFFER_BIT);
3054     EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::red);
3055 
3056     glClear(GL_COLOR_BUFFER_BIT);
3057     glEnable(GL_BLEND);
3058     glBlendFunc(GL_ONE, GL_ONE);
3059     ANGLE_GL_PROGRAM(drawGreen, essl1_shaders::vs::Passthrough(), essl1_shaders::fs::Green());
3060     drawQuad(drawGreen, essl1_shaders::PositionAttrib(), 0);
3061     EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::yellow);
3062 
3063     glClear(GL_COLOR_BUFFER_BIT);
3064     EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::red);
3065 
3066     ASSERT_GL_NO_ERROR();
3067 }
3068 
3069 // Test that reclearing depth to the same value works if depth was written to in between with a draw
3070 // call.
TEST_P(ClearTestES3,RepeatedDepthClearWithDrawInBetween)3071 TEST_P(ClearTestES3, RepeatedDepthClearWithDrawInBetween)
3072 {
3073     glClearDepthf(0.25f);
3074     glClear(GL_DEPTH_BUFFER_BIT);
3075 
3076     ANGLE_GL_PROGRAM(drawGreen, essl1_shaders::vs::Passthrough(), essl1_shaders::fs::Green());
3077     glEnable(GL_DEPTH_TEST);
3078     glDepthFunc(GL_ALWAYS);
3079     drawQuad(drawGreen, essl1_shaders::PositionAttrib(), 0.75f);
3080 
3081     glClear(GL_DEPTH_BUFFER_BIT);
3082     glDepthMask(GL_FALSE);
3083     verifyDepth(0.25f, 1);
3084 
3085     ASSERT_GL_NO_ERROR();
3086 }
3087 
3088 // Test that reclearing stencil to the same value works if stencil was written to in between with a
3089 // draw call.
TEST_P(ClearTestES3,RepeatedStencilClearWithDrawInBetween)3090 TEST_P(ClearTestES3, RepeatedStencilClearWithDrawInBetween)
3091 {
3092     glClearStencil(0xE4);
3093     glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE);
3094     glStencilFunc(GL_ALWAYS, 0x3C, 0xFF);
3095     glEnable(GL_STENCIL_TEST);
3096 
3097     glClear(GL_STENCIL_BUFFER_BIT);
3098     ANGLE_GL_PROGRAM(drawGreen, essl1_shaders::vs::Passthrough(), essl1_shaders::fs::Green());
3099     drawQuad(drawGreen, essl1_shaders::PositionAttrib(), 0.75f);
3100 
3101     glClear(GL_STENCIL_BUFFER_BIT);
3102     verifyStencil(0xE4, 1);
3103 
3104     ASSERT_GL_NO_ERROR();
3105 }
3106 
3107 // Test that reclearing color to the same value works if color was written to in between with
3108 // glCopyTexSubImage2D.
TEST_P(ClearTestES3,RepeatedColorClearWithCopyInBetween)3109 TEST_P(ClearTestES3, RepeatedColorClearWithCopyInBetween)
3110 {
3111     GLTexture color;
3112     glBindTexture(GL_TEXTURE_2D, color);
3113     glTexStorage2D(GL_TEXTURE_2D, 1, GL_RGBA8, getWindowWidth(), getWindowHeight());
3114 
3115     GLFramebuffer fbo;
3116     glBindFramebuffer(GL_FRAMEBUFFER, fbo);
3117     glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, color, 0);
3118     ASSERT_GL_FRAMEBUFFER_COMPLETE(GL_FRAMEBUFFER);
3119 
3120     // Clear the texture
3121     glClearColor(1, 0, 0, 1);
3122     glClear(GL_COLOR_BUFFER_BIT);
3123     EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::red);
3124 
3125     // Replace the framebuffer texture
3126     GLTexture color2;
3127     glBindTexture(GL_TEXTURE_2D, color2);
3128     glTexStorage2D(GL_TEXTURE_2D, 1, GL_RGBA8, getWindowWidth(), getWindowHeight());
3129     glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, color2, 0);
3130     ASSERT_GL_FRAMEBUFFER_COMPLETE(GL_FRAMEBUFFER);
3131 
3132     // Clear the new texture and copy it to the old texture
3133     glClearColor(0, 1, 0, 1);
3134     glClear(GL_COLOR_BUFFER_BIT);
3135     glBindTexture(GL_TEXTURE_2D, color);
3136     glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, getWindowWidth(), getWindowHeight());
3137 
3138     // Attach the original texture back to the framebuffer and verify the copy.
3139     glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, color, 0);
3140     ASSERT_GL_FRAMEBUFFER_COMPLETE(GL_FRAMEBUFFER);
3141     EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::green);
3142 
3143     // Clear to the original value and make sure it's applied.
3144     glClearColor(1, 0, 0, 1);
3145     glClear(GL_COLOR_BUFFER_BIT);
3146     EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::red);
3147 
3148     ASSERT_GL_NO_ERROR();
3149 }
3150 
3151 // Test that reclearing color to the same value works if color was written to in between with a
3152 // blit.
TEST_P(ClearTestES3,RepeatedColorClearWithBlitInBetween)3153 TEST_P(ClearTestES3, RepeatedColorClearWithBlitInBetween)
3154 {
3155     GLTexture color;
3156     glBindTexture(GL_TEXTURE_2D, color);
3157     glTexStorage2D(GL_TEXTURE_2D, 1, GL_RGBA8, getWindowWidth(), getWindowHeight());
3158 
3159     GLFramebuffer fbo;
3160     glBindFramebuffer(GL_FRAMEBUFFER, fbo);
3161     glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, color, 0);
3162     ASSERT_GL_FRAMEBUFFER_COMPLETE(GL_FRAMEBUFFER);
3163 
3164     // Clear the texture
3165     glClearColor(1, 0, 0, 1);
3166     glClear(GL_COLOR_BUFFER_BIT);
3167     EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::red);
3168 
3169     // Create another framebuffer as blit src
3170     GLTexture color2;
3171     glBindTexture(GL_TEXTURE_2D, color2);
3172     glTexStorage2D(GL_TEXTURE_2D, 1, GL_RGBA8, getWindowWidth(), getWindowHeight());
3173 
3174     GLFramebuffer fbo2;
3175     glBindFramebuffer(GL_FRAMEBUFFER, fbo2);
3176     glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, color2, 0);
3177     ASSERT_GL_FRAMEBUFFER_COMPLETE(GL_FRAMEBUFFER);
3178 
3179     // Clear the new framebuffer and blit it to the old one
3180     glClearColor(0, 1, 0, 1);
3181     glClear(GL_COLOR_BUFFER_BIT);
3182     glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
3183     glBlitFramebuffer(0, 0, getWindowWidth(), getWindowHeight(), 0, 0, getWindowWidth(),
3184                       getWindowHeight(), GL_COLOR_BUFFER_BIT, GL_NEAREST);
3185 
3186     // Verify the copy is done correctly.
3187     glBindFramebuffer(GL_READ_FRAMEBUFFER, fbo);
3188     EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::green);
3189 
3190     // Clear to the original value and make sure it's applied.
3191     glClearColor(1, 0, 0, 1);
3192     glClear(GL_COLOR_BUFFER_BIT);
3193     EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::red);
3194 
3195     ASSERT_GL_NO_ERROR();
3196 }
3197 
3198 // Test that reclearing depth to the same value works if depth was written to in between with a
3199 // blit.
TEST_P(ClearTestES3,RepeatedDepthClearWithBlitInBetween)3200 TEST_P(ClearTestES3, RepeatedDepthClearWithBlitInBetween)
3201 {
3202     GLTexture color;
3203     glBindTexture(GL_TEXTURE_2D, color);
3204     glTexStorage2D(GL_TEXTURE_2D, 1, GL_RGBA8, getWindowWidth(), getWindowHeight());
3205 
3206     GLRenderbuffer depth;
3207     glBindRenderbuffer(GL_RENDERBUFFER, depth);
3208     glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT24, getWindowWidth(),
3209                           getWindowHeight());
3210 
3211     GLFramebuffer fbo;
3212     glBindFramebuffer(GL_FRAMEBUFFER, fbo);
3213     glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, color, 0);
3214     glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depth);
3215     ASSERT_GL_FRAMEBUFFER_COMPLETE(GL_FRAMEBUFFER);
3216 
3217     // Clear depth
3218     glClearDepthf(0.25f);
3219     glClearColor(1, 0, 0, 1);
3220     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
3221     EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::red);
3222     verifyDepth(0.25f, 1);
3223 
3224     // Create another framebuffer as blit src
3225     GLRenderbuffer depth2;
3226     glBindRenderbuffer(GL_RENDERBUFFER, depth2);
3227     glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT24, getWindowWidth(),
3228                           getWindowHeight());
3229 
3230     GLFramebuffer fbo2;
3231     glBindFramebuffer(GL_FRAMEBUFFER, fbo2);
3232     glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depth2);
3233     ASSERT_GL_FRAMEBUFFER_COMPLETE(GL_FRAMEBUFFER);
3234 
3235     // Clear the new framebuffer and blit it to the old one
3236     glClearDepthf(0.75f);
3237     glClear(GL_DEPTH_BUFFER_BIT);
3238     glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
3239     glBlitFramebuffer(0, 0, getWindowWidth(), getWindowHeight(), 0, 0, getWindowWidth(),
3240                       getWindowHeight(), GL_DEPTH_BUFFER_BIT, GL_NEAREST);
3241 
3242     // Verify the copy is done correctly.
3243     glBindFramebuffer(GL_READ_FRAMEBUFFER, fbo);
3244     verifyDepth(0.75f, 1);
3245 
3246     // Clear to the original value and make sure it's applied.
3247     glClearDepthf(0.25f);
3248     glClear(GL_DEPTH_BUFFER_BIT);
3249     verifyDepth(0.25f, 1);
3250 
3251     ASSERT_GL_NO_ERROR();
3252 }
3253 
3254 // Test that reclearing stencil to the same value works if stencil was written to in between with a
3255 // blit.
TEST_P(ClearTestES3,RepeatedStencilClearWithBlitInBetween)3256 TEST_P(ClearTestES3, RepeatedStencilClearWithBlitInBetween)
3257 {
3258     GLTexture color;
3259     glBindTexture(GL_TEXTURE_2D, color);
3260     glTexStorage2D(GL_TEXTURE_2D, 1, GL_RGBA8, getWindowWidth(), getWindowHeight());
3261 
3262     GLRenderbuffer stencil;
3263     glBindRenderbuffer(GL_RENDERBUFFER, stencil);
3264     glRenderbufferStorage(GL_RENDERBUFFER, GL_STENCIL_INDEX8, getWindowWidth(), getWindowHeight());
3265 
3266     GLFramebuffer fbo;
3267     glBindFramebuffer(GL_FRAMEBUFFER, fbo);
3268     glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, color, 0);
3269     glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, stencil);
3270     ASSERT_GL_FRAMEBUFFER_COMPLETE(GL_FRAMEBUFFER);
3271 
3272     // Clear stencil
3273     glClearStencil(0xE4);
3274     glClearColor(1, 0, 0, 1);
3275     glClear(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
3276     EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::red);
3277     verifyStencil(0xE4, 1);
3278 
3279     // Create another framebuffer as blit src
3280     GLRenderbuffer stencil2;
3281     glBindRenderbuffer(GL_RENDERBUFFER, stencil2);
3282     glRenderbufferStorage(GL_RENDERBUFFER, GL_STENCIL_INDEX8, getWindowWidth(), getWindowHeight());
3283 
3284     GLFramebuffer fbo2;
3285     glBindFramebuffer(GL_FRAMEBUFFER, fbo2);
3286     glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, stencil2);
3287     ASSERT_GL_FRAMEBUFFER_COMPLETE(GL_FRAMEBUFFER);
3288 
3289     // Clear the new framebuffer and blit it to the old one
3290     glClearStencil(0x35);
3291     glClear(GL_STENCIL_BUFFER_BIT);
3292     glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
3293     glBlitFramebuffer(0, 0, getWindowWidth(), getWindowHeight(), 0, 0, getWindowWidth(),
3294                       getWindowHeight(), GL_STENCIL_BUFFER_BIT, GL_NEAREST);
3295 
3296     // Verify the copy is done correctly.
3297     glBindFramebuffer(GL_READ_FRAMEBUFFER, fbo);
3298     verifyStencil(0x35, 1);
3299 
3300     // Clear to the original value and make sure it's applied.
3301     glClearStencil(0xE4);
3302     glClear(GL_STENCIL_BUFFER_BIT);
3303     verifyStencil(0xE4, 1);
3304 
3305     ASSERT_GL_NO_ERROR();
3306 }
3307 
3308 // Test that reclearing color to the same value works if color was written to in between with a
3309 // compute shader.
TEST_P(ClearTestES31,RepeatedColorClearWithDispatchInBetween)3310 TEST_P(ClearTestES31, RepeatedColorClearWithDispatchInBetween)
3311 {
3312     GLTexture color;
3313     glBindTexture(GL_TEXTURE_2D, color);
3314     glTexStorage2D(GL_TEXTURE_2D, 1, GL_RGBA8, getWindowWidth(), getWindowHeight());
3315 
3316     GLFramebuffer fbo;
3317     glBindFramebuffer(GL_FRAMEBUFFER, fbo);
3318     glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, color, 0);
3319     ASSERT_GL_FRAMEBUFFER_COMPLETE(GL_FRAMEBUFFER);
3320 
3321     // Clear the texture
3322     glClearColor(1, 0, 0, 1);
3323     glClear(GL_COLOR_BUFFER_BIT);
3324     EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::red);
3325 
3326     // Write to the texture with a compute shader.
3327     constexpr char kCS[] = R"(#version 310 es
3328 layout(local_size_x=1, local_size_y=1, local_size_z=1) in;
3329 layout(rgba8) uniform highp writeonly image2D imageOut;
3330 void main()
3331 {
3332     imageStore(imageOut, ivec2(gl_GlobalInvocationID.xy), vec4(0, 1, 0, 1));
3333 })";
3334 
3335     ANGLE_GL_COMPUTE_PROGRAM(program, kCS);
3336     glUseProgram(program);
3337     glBindImageTexture(0, color, 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_RGBA8);
3338 
3339     glDispatchCompute(getWindowWidth(), getWindowHeight(), 1);
3340     EXPECT_GL_NO_ERROR();
3341 
3342     glMemoryBarrier(GL_FRAMEBUFFER_BARRIER_BIT);
3343 
3344     // Verify the compute shader overwrites the image correctly.
3345     EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::green);
3346 
3347     // Clear to the original value and make sure it's applied.
3348     glClearColor(1, 0, 0, 1);
3349     glClear(GL_COLOR_BUFFER_BIT);
3350     EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::red);
3351 
3352     ASSERT_GL_NO_ERROR();
3353 }
3354 
3355 // Test that reclearing depth to the same value works if depth is blit after clear, and depth is
3356 // modified in between with a draw call.
TEST_P(ClearTestES3,RepeatedDepthClearWithBlitAfterClearAndDrawInBetween)3357 TEST_P(ClearTestES3, RepeatedDepthClearWithBlitAfterClearAndDrawInBetween)
3358 {
3359     glClearDepthf(0.25f);
3360     glClear(GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
3361 
3362     // Make sure clear is flushed.
3363     GLRenderbuffer depth;
3364     glBindRenderbuffer(GL_RENDERBUFFER, depth);
3365     glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8_OES, getWindowWidth(),
3366                           getWindowHeight());
3367 
3368     GLFramebuffer fbo;
3369     glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
3370     glFramebufferRenderbuffer(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depth);
3371     ASSERT_GL_FRAMEBUFFER_COMPLETE(GL_DRAW_FRAMEBUFFER);
3372 
3373     glBlitFramebuffer(0, 0, getWindowWidth(), getWindowHeight(), 0, 0, getWindowWidth(),
3374                       getWindowHeight(), GL_DEPTH_BUFFER_BIT, GL_NEAREST);
3375 
3376     // Draw to depth, and break the render pass.
3377     glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
3378     ANGLE_GL_PROGRAM(drawGreen, essl1_shaders::vs::Passthrough(), essl1_shaders::fs::Green());
3379     glEnable(GL_DEPTH_TEST);
3380     glDepthFunc(GL_ALWAYS);
3381     drawQuad(drawGreen, essl1_shaders::PositionAttrib(), 0.75f);
3382     EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::green);
3383 
3384     // Clear back to the original value
3385     glClear(GL_DEPTH_BUFFER_BIT);
3386 
3387     // Blit again.
3388     glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
3389     glBlitFramebuffer(0, 0, getWindowWidth(), getWindowHeight(), 0, 0, getWindowWidth(),
3390                       getWindowHeight(), GL_DEPTH_BUFFER_BIT, GL_NEAREST);
3391 
3392     // Make sure the cleared value is in destination, not the modified value.
3393     GLTexture color;
3394     glBindTexture(GL_TEXTURE_2D, color);
3395     glTexStorage2D(GL_TEXTURE_2D, 1, GL_RGBA8, getWindowWidth(), getWindowHeight());
3396 
3397     glBindFramebuffer(GL_READ_FRAMEBUFFER, fbo);
3398     glFramebufferTexture2D(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, color, 0);
3399     ASSERT_GL_FRAMEBUFFER_COMPLETE(GL_READ_FRAMEBUFFER);
3400     verifyDepth(0.25f, 1);
3401 
3402     ASSERT_GL_NO_ERROR();
3403 }
3404 
3405 // Test that reclearing stencil to the same value works if stencil is blit after clear, and stencil
3406 // is modified in between with a draw call.
TEST_P(ClearTestES3,RepeatedStencilClearWithBlitAfterClearAndDrawInBetween)3407 TEST_P(ClearTestES3, RepeatedStencilClearWithBlitAfterClearAndDrawInBetween)
3408 {
3409     glClearStencil(0xE4);
3410     glClear(GL_STENCIL_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
3411 
3412     // Make sure clear is flushed.
3413     GLRenderbuffer stencil;
3414     glBindRenderbuffer(GL_RENDERBUFFER, stencil);
3415     glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8_OES, getWindowWidth(),
3416                           getWindowHeight());
3417 
3418     GLFramebuffer fbo;
3419     glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
3420     glFramebufferRenderbuffer(GL_DRAW_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, stencil);
3421     ASSERT_GL_FRAMEBUFFER_COMPLETE(GL_DRAW_FRAMEBUFFER);
3422 
3423     glBlitFramebuffer(0, 0, getWindowWidth(), getWindowHeight(), 0, 0, getWindowWidth(),
3424                       getWindowHeight(), GL_STENCIL_BUFFER_BIT, GL_NEAREST);
3425 
3426     // Draw to stencil, and break the render pass.
3427     glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
3428     ANGLE_GL_PROGRAM(drawGreen, essl1_shaders::vs::Passthrough(), essl1_shaders::fs::Green());
3429     glEnable(GL_STENCIL_TEST);
3430     glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE);
3431     glStencilFunc(GL_ALWAYS, 0x3C, 0xFF);
3432     drawQuad(drawGreen, essl1_shaders::PositionAttrib(), 0.75f);
3433     EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::green);
3434 
3435     // Clear back to the original value
3436     glClear(GL_STENCIL_BUFFER_BIT);
3437 
3438     // Blit again.
3439     glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
3440     glBlitFramebuffer(0, 0, getWindowWidth(), getWindowHeight(), 0, 0, getWindowWidth(),
3441                       getWindowHeight(), GL_STENCIL_BUFFER_BIT, GL_NEAREST);
3442 
3443     // Make sure the cleared value is in destination, not the modified value.
3444     GLTexture color;
3445     glBindTexture(GL_TEXTURE_2D, color);
3446     glTexStorage2D(GL_TEXTURE_2D, 1, GL_RGBA8, getWindowWidth(), getWindowHeight());
3447 
3448     glBindFramebuffer(GL_READ_FRAMEBUFFER, fbo);
3449     glFramebufferTexture2D(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, color, 0);
3450     ASSERT_GL_FRAMEBUFFER_COMPLETE(GL_READ_FRAMEBUFFER);
3451     verifyStencil(0xE4, 1);
3452 
3453     ASSERT_GL_NO_ERROR();
3454 }
3455 
3456 #ifdef Bool
3457 // X11 craziness.
3458 #    undef Bool
3459 #endif
3460 
3461 ANGLE_INSTANTIATE_TEST_ES2_AND_ES3_AND(
3462     ClearTest,
3463     ES3_VULKAN().enable(Feature::ForceFallbackFormat),
3464     ES3_VULKAN().enable(Feature::PreferDrawClearOverVkCmdClearAttachments));
3465 
3466 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(ClearTestES3);
3467 ANGLE_INSTANTIATE_TEST_ES3_AND(
3468     ClearTestES3,
3469     ES3_VULKAN().enable(Feature::ForceFallbackFormat),
3470     ES3_VULKAN().enable(Feature::PreferDrawClearOverVkCmdClearAttachments));
3471 
3472 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(ClearTestES31);
3473 ANGLE_INSTANTIATE_TEST_ES31_AND(
3474     ClearTestES31,
3475     ES31_VULKAN().enable(Feature::ForceFallbackFormat),
3476     ES31_VULKAN().enable(Feature::PreferDrawClearOverVkCmdClearAttachments));
3477 
3478 ANGLE_INSTANTIATE_TEST_COMBINE_4(MaskedScissoredClearTest,
3479                                  MaskedScissoredClearVariationsTestPrint,
3480                                  testing::Range(0, 3),
3481                                  testing::Range(0, 3),
3482                                  testing::Range(0, 3),
3483                                  testing::Bool(),
3484                                  ANGLE_ALL_TEST_PLATFORMS_ES2,
3485                                  ANGLE_ALL_TEST_PLATFORMS_ES3,
3486                                  ES3_VULKAN()
3487                                      .disable(Feature::SupportsExtendedDynamicState)
3488                                      .disable(Feature::SupportsExtendedDynamicState2),
3489                                  ES3_VULKAN().disable(Feature::SupportsExtendedDynamicState2));
3490 
3491 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(VulkanClearTest);
3492 ANGLE_INSTANTIATE_TEST_COMBINE_4(VulkanClearTest,
3493                                  MaskedScissoredClearVariationsTestPrint,
3494                                  testing::Range(0, 3),
3495                                  testing::Range(0, 3),
3496                                  testing::Range(0, 3),
3497                                  testing::Bool(),
3498                                  ES2_VULKAN().enable(Feature::ForceFallbackFormat),
3499                                  ES2_VULKAN_SWIFTSHADER().enable(Feature::ForceFallbackFormat),
3500                                  ES3_VULKAN().enable(Feature::ForceFallbackFormat),
3501                                  ES3_VULKAN_SWIFTSHADER().enable(Feature::ForceFallbackFormat));
3502 
3503 // Not all ANGLE backends support RGB backbuffers
3504 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(ClearTestRGB);
3505 ANGLE_INSTANTIATE_TEST(ClearTestRGB,
3506                        ES2_D3D11(),
3507                        ES3_D3D11(),
3508                        ES2_VULKAN(),
3509                        ES3_VULKAN(),
3510                        ES2_METAL(),
3511                        ES3_METAL());
3512 
3513 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(ClearTestRGB_ES3);
3514 ANGLE_INSTANTIATE_TEST(ClearTestRGB_ES3, ES3_D3D11(), ES3_VULKAN(), ES3_METAL());
3515 
3516 }  // anonymous namespace
3517