• 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 "platform/FeaturesVk.h"
10 #include "test_utils/gl_raii.h"
11 #include "util/random_utils.h"
12 #include "util/shader_utils.h"
13 #include "util/test_utils.h"
14 
15 using namespace angle;
16 
17 namespace
18 {
19 class ClearTestBase : public ANGLETest
20 {
21   protected:
ClearTestBase()22     ClearTestBase()
23     {
24         setWindowWidth(128);
25         setWindowHeight(128);
26         setConfigRedBits(8);
27         setConfigGreenBits(8);
28         setConfigBlueBits(8);
29         setConfigAlphaBits(8);
30         setConfigDepthBits(24);
31         setConfigStencilBits(8);
32     }
33 
testSetUp()34     void testSetUp() override
35     {
36         mFBOs.resize(2, 0);
37         glGenFramebuffers(2, mFBOs.data());
38 
39         ASSERT_GL_NO_ERROR();
40     }
41 
testTearDown()42     void testTearDown() override
43     {
44         if (!mFBOs.empty())
45         {
46             glDeleteFramebuffers(static_cast<GLsizei>(mFBOs.size()), mFBOs.data());
47         }
48 
49         if (!mTextures.empty())
50         {
51             glDeleteTextures(static_cast<GLsizei>(mTextures.size()), mTextures.data());
52         }
53     }
54 
55     std::vector<GLuint> mFBOs;
56     std::vector<GLuint> mTextures;
57 };
58 
59 class ClearTest : public ClearTestBase
60 {};
61 
62 class ClearTestES3 : public ClearTestBase
63 {
64   protected:
verifyDepth(float depthValue,uint32_t size)65     void verifyDepth(float depthValue, uint32_t size)
66     {
67         // Use a small shader to verify depth.
68         ANGLE_GL_PROGRAM(depthTestProgram, essl1_shaders::vs::Passthrough(),
69                          essl1_shaders::fs::Blue());
70         ANGLE_GL_PROGRAM(depthTestProgramFail, essl1_shaders::vs::Passthrough(),
71                          essl1_shaders::fs::Red());
72         glEnable(GL_DEPTH_TEST);
73         glDepthFunc(GL_LESS);
74         drawQuad(depthTestProgram, essl1_shaders::PositionAttrib(), depthValue * 2 - 1 - 0.01f);
75         drawQuad(depthTestProgramFail, essl1_shaders::PositionAttrib(), depthValue * 2 - 1 + 0.01f);
76         glDisable(GL_DEPTH_TEST);
77         ASSERT_GL_NO_ERROR();
78 
79         EXPECT_PIXEL_COLOR_NEAR(0, 0, GLColor::blue, 1);
80         EXPECT_PIXEL_COLOR_NEAR(size - 1, 0, GLColor::blue, 1);
81         EXPECT_PIXEL_COLOR_NEAR(0, size - 1, GLColor::blue, 1);
82         EXPECT_PIXEL_COLOR_NEAR(size - 1, size - 1, GLColor::blue, 1);
83     }
84 
verifyStencil(uint32_t stencilValue,uint32_t size)85     void verifyStencil(uint32_t stencilValue, uint32_t size)
86     {
87         // Use another small shader to verify stencil.
88         ANGLE_GL_PROGRAM(stencilTestProgram, essl1_shaders::vs::Passthrough(),
89                          essl1_shaders::fs::Green());
90         glEnable(GL_STENCIL_TEST);
91         glStencilFunc(GL_EQUAL, stencilValue, 0xFF);
92         drawQuad(stencilTestProgram, essl1_shaders::PositionAttrib(), 0.0f);
93         glDisable(GL_STENCIL_TEST);
94         ASSERT_GL_NO_ERROR();
95 
96         EXPECT_PIXEL_COLOR_NEAR(0, 0, GLColor::green, 1);
97         EXPECT_PIXEL_COLOR_NEAR(size - 1, 0, GLColor::green, 1);
98         EXPECT_PIXEL_COLOR_NEAR(0, size - 1, GLColor::green, 1);
99         EXPECT_PIXEL_COLOR_NEAR(size - 1, size - 1, GLColor::green, 1);
100     }
101 };
102 
103 class ClearTestRGB : public ANGLETest
104 {
105   protected:
ClearTestRGB()106     ClearTestRGB()
107     {
108         setWindowWidth(128);
109         setWindowHeight(128);
110         setConfigRedBits(8);
111         setConfigGreenBits(8);
112         setConfigBlueBits(8);
113     }
114 };
115 
116 // Each int parameter can have three values: don't clear, clear, or masked clear.  The bool
117 // parameter controls scissor.
118 using MaskedScissoredClearVariationsTestParams =
119     std::tuple<angle::PlatformParameters, int, int, int, bool>;
120 
ParseMaskedScissoredClearVariationsTestParams(const MaskedScissoredClearVariationsTestParams & params,bool * clearColor,bool * clearDepth,bool * clearStencil,bool * maskColor,bool * maskDepth,bool * maskStencil,bool * scissor)121 void ParseMaskedScissoredClearVariationsTestParams(
122     const MaskedScissoredClearVariationsTestParams &params,
123     bool *clearColor,
124     bool *clearDepth,
125     bool *clearStencil,
126     bool *maskColor,
127     bool *maskDepth,
128     bool *maskStencil,
129     bool *scissor)
130 {
131     int colorClearInfo   = std::get<1>(params);
132     int depthClearInfo   = std::get<2>(params);
133     int stencilClearInfo = std::get<3>(params);
134 
135     *clearColor   = colorClearInfo > 0;
136     *clearDepth   = depthClearInfo > 0;
137     *clearStencil = stencilClearInfo > 0;
138 
139     *maskColor   = colorClearInfo > 1;
140     *maskDepth   = depthClearInfo > 1;
141     *maskStencil = stencilClearInfo > 1;
142 
143     *scissor = std::get<4>(params);
144 }
145 
MaskedScissoredClearVariationsTestPrint(const::testing::TestParamInfo<MaskedScissoredClearVariationsTestParams> & paramsInfo)146 std::string MaskedScissoredClearVariationsTestPrint(
147     const ::testing::TestParamInfo<MaskedScissoredClearVariationsTestParams> &paramsInfo)
148 {
149     const MaskedScissoredClearVariationsTestParams &params = paramsInfo.param;
150     std::ostringstream out;
151 
152     out << std::get<0>(params);
153 
154     bool clearColor, clearDepth, clearStencil;
155     bool maskColor, maskDepth, maskStencil;
156     bool scissor;
157 
158     ParseMaskedScissoredClearVariationsTestParams(params, &clearColor, &clearDepth, &clearStencil,
159                                                   &maskColor, &maskDepth, &maskStencil, &scissor);
160 
161     if (scissor || clearColor || clearDepth || clearStencil || maskColor || maskDepth ||
162         maskStencil)
163     {
164         out << "_";
165     }
166 
167     if (scissor)
168     {
169         out << "_scissored";
170     }
171 
172     if (clearColor || clearDepth || clearStencil)
173     {
174         out << "_clear_";
175         if (clearColor)
176         {
177             out << "c";
178         }
179         if (clearDepth)
180         {
181             out << "d";
182         }
183         if (clearStencil)
184         {
185             out << "s";
186         }
187     }
188 
189     if (maskColor || maskDepth || maskStencil)
190     {
191         out << "_mask_";
192         if (maskColor)
193         {
194             out << "c";
195         }
196         if (maskDepth)
197         {
198             out << "d";
199         }
200         if (maskStencil)
201         {
202             out << "s";
203         }
204     }
205 
206     return out.str();
207 }
208 
209 class MaskedScissoredClearTestBase
210     : public ANGLETestWithParam<MaskedScissoredClearVariationsTestParams>
211 {
212   protected:
MaskedScissoredClearTestBase()213     MaskedScissoredClearTestBase()
214     {
215         setWindowWidth(128);
216         setWindowHeight(128);
217         setConfigRedBits(8);
218         setConfigGreenBits(8);
219         setConfigBlueBits(8);
220         setConfigAlphaBits(8);
221         setConfigDepthBits(24);
222         setConfigStencilBits(8);
223     }
224 
225     void maskedScissoredColorDepthStencilClear(
226         const MaskedScissoredClearVariationsTestParams &params);
227 
228     bool mHasDepth   = true;
229     bool mHasStencil = true;
230 };
231 
232 class MaskedScissoredClearTest : public MaskedScissoredClearTestBase
233 {};
234 
235 class VulkanClearTest : public MaskedScissoredClearTestBase
236 {
237   protected:
testSetUp()238     void testSetUp() override
239     {
240         glBindTexture(GL_TEXTURE_2D, mColorTexture);
241         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, getWindowWidth(), getWindowHeight(), 0, GL_RGBA,
242                      GL_UNSIGNED_BYTE, nullptr);
243 
244         // Setup Color/Stencil FBO with a stencil format that's emulated with packed depth/stencil.
245         glBindFramebuffer(GL_FRAMEBUFFER, mColorStencilFBO);
246 
247         glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, mColorTexture,
248                                0);
249         glBindRenderbuffer(GL_RENDERBUFFER, mStencilTexture);
250         glRenderbufferStorage(GL_RENDERBUFFER, GL_STENCIL_INDEX8, getWindowWidth(),
251                               getWindowHeight());
252         glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER,
253                                   mStencilTexture);
254 
255         ASSERT_GL_NO_ERROR();
256 
257         // Note: GL_DEPTH_COMPONENT24 is not allowed in GLES2.
258         if (getClientMajorVersion() >= 3)
259         {
260             // Setup Color/Depth FBO with a depth format that's emulated with packed depth/stencil.
261             glBindFramebuffer(GL_FRAMEBUFFER, mColorDepthFBO);
262 
263             glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
264                                    mColorTexture, 0);
265             glBindRenderbuffer(GL_RENDERBUFFER, mDepthTexture);
266             glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT24, getWindowWidth(),
267                                   getWindowHeight());
268             glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER,
269                                       mDepthTexture);
270         }
271 
272         ASSERT_GL_NO_ERROR();
273     }
274 
bindColorStencilFBO()275     void bindColorStencilFBO()
276     {
277         glBindFramebuffer(GL_FRAMEBUFFER, mColorStencilFBO);
278         mHasDepth = false;
279     }
280 
bindColorDepthFBO()281     void bindColorDepthFBO()
282     {
283         glBindFramebuffer(GL_FRAMEBUFFER, mColorDepthFBO);
284         mHasStencil = false;
285     }
286 
287     // Override a feature to force emulation of stencil-only and depth-only formats with a packed
288     // depth/stencil format
overrideFeaturesVk(FeaturesVk * featuresVk)289     void overrideFeaturesVk(FeaturesVk *featuresVk) override
290     {
291         featuresVk->overrideFeatures({"force_fallback_format"}, true);
292     }
293 
294   private:
295     GLFramebuffer mColorStencilFBO;
296     GLFramebuffer mColorDepthFBO;
297     GLTexture mColorTexture;
298     GLRenderbuffer mDepthTexture;
299     GLRenderbuffer mStencilTexture;
300 };
301 
302 // Test clearing the default framebuffer
TEST_P(ClearTest,DefaultFramebuffer)303 TEST_P(ClearTest, DefaultFramebuffer)
304 {
305     glClearColor(0.25f, 0.5f, 0.5f, 0.5f);
306     glClear(GL_COLOR_BUFFER_BIT);
307     EXPECT_PIXEL_NEAR(0, 0, 64, 128, 128, 128, 1.0);
308 }
309 
310 // Test clearing the default framebuffer with scissor and mask
311 // This forces down path that uses draw to do clear
TEST_P(ClearTest,EmptyScissor)312 TEST_P(ClearTest, EmptyScissor)
313 {
314     // These configs have bug that fails this test.
315     // These configs are unmaintained so skipping.
316     ANGLE_SKIP_TEST_IF(IsIntel() && IsD3D9());
317     ANGLE_SKIP_TEST_IF(IsOSX());
318     glClearColor(0.25f, 0.5f, 0.5f, 1.0f);
319     glClear(GL_COLOR_BUFFER_BIT);
320     glEnable(GL_SCISSOR_TEST);
321     glScissor(-10, 0, 5, 5);
322     glClearColor(0.5f, 0.25f, 0.75f, 0.5f);
323     glColorMask(GL_TRUE, GL_FALSE, GL_TRUE, GL_TRUE);
324     glClear(GL_COLOR_BUFFER_BIT);
325     glDisable(GL_SCISSOR_TEST);
326     glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
327     EXPECT_PIXEL_NEAR(0, 0, 64, 128, 128, 255, 1.0);
328 }
329 
330 // Test clearing the RGB default framebuffer and verify that the alpha channel is not cleared
TEST_P(ClearTestRGB,DefaultFramebufferRGB)331 TEST_P(ClearTestRGB, DefaultFramebufferRGB)
332 {
333     // Some GPUs don't support RGB format default framebuffer,
334     // so skip if the back buffer has alpha bits.
335     EGLWindow *window          = getEGLWindow();
336     EGLDisplay display         = window->getDisplay();
337     EGLConfig config           = window->getConfig();
338     EGLint backbufferAlphaBits = 0;
339     eglGetConfigAttrib(display, config, EGL_ALPHA_SIZE, &backbufferAlphaBits);
340     ANGLE_SKIP_TEST_IF(backbufferAlphaBits != 0);
341 
342     glClearColor(0.25f, 0.5f, 0.5f, 0.5f);
343     glClear(GL_COLOR_BUFFER_BIT);
344     EXPECT_PIXEL_NEAR(0, 0, 64, 128, 128, 255, 1.0);
345 }
346 
347 // Invalidate the RGB default framebuffer and verify that the alpha channel is not cleared, and
348 // stays set after drawing.
TEST_P(ClearTestRGB,InvalidateDefaultFramebufferRGB)349 TEST_P(ClearTestRGB, InvalidateDefaultFramebufferRGB)
350 {
351     ANGLE_GL_PROGRAM(blueProgram, essl1_shaders::vs::Simple(), essl1_shaders::fs::Blue());
352 
353     // Some GPUs don't support RGB format default framebuffer,
354     // so skip if the back buffer has alpha bits.
355     EGLWindow *window          = getEGLWindow();
356     EGLDisplay display         = window->getDisplay();
357     EGLConfig config           = window->getConfig();
358     EGLint backbufferAlphaBits = 0;
359     eglGetConfigAttrib(display, config, EGL_ALPHA_SIZE, &backbufferAlphaBits);
360     ANGLE_SKIP_TEST_IF(backbufferAlphaBits != 0);
361     // glInvalidateFramebuffer() isn't supported with GLES 2.0
362     ANGLE_SKIP_TEST_IF(getClientMajorVersion() < 3);
363     ANGLE_SKIP_TEST_IF(IsD3D11());
364 
365     glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
366     glClear(GL_COLOR_BUFFER_BIT);
367     // Verify that even though Alpha is cleared to 0.0 for this RGB FBO, it should be read back as
368     // 1.0, since the glReadPixels() is issued with GL_RGBA.
369     // OpenGL ES 3.2 spec:
370     // 16.1.3 Obtaining Pixels from the Framebuffer
371     // If G, B, or A values are not present in the internal format, they are taken to be zero,
372     // zero, and one respectively.
373     EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::black);
374 
375     const GLenum discards[] = {GL_COLOR};
376     glInvalidateFramebuffer(GL_FRAMEBUFFER, 1, discards);
377 
378     // Don't explicitly clear, but draw blue (make sure alpha is not cleared)
379     drawQuad(blueProgram, essl1_shaders::PositionAttrib(), 0.5f);
380     EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::blue);
381 }
382 
383 // Draw with a shader that outputs alpha=0.5. Readback and ensure that alpha=1.
TEST_P(ClearTestRGB,ShaderOutputsAlphaVerifyReadingAlphaIsOne)384 TEST_P(ClearTestRGB, ShaderOutputsAlphaVerifyReadingAlphaIsOne)
385 {
386     ANGLE_GL_PROGRAM(blueProgram, essl1_shaders::vs::Simple(), essl1_shaders::fs::UniformColor());
387     glUseProgram(blueProgram);
388 
389     // Some GPUs don't support RGB format default framebuffer,
390     // so skip if the back buffer has alpha bits.
391     EGLWindow *window          = getEGLWindow();
392     EGLDisplay display         = window->getDisplay();
393     EGLConfig config           = window->getConfig();
394     EGLint backbufferAlphaBits = 0;
395     eglGetConfigAttrib(display, config, EGL_ALPHA_SIZE, &backbufferAlphaBits);
396     ANGLE_SKIP_TEST_IF(backbufferAlphaBits != 0);
397     // glInvalidateFramebuffer() isn't supported with GLES 2.0
398     ANGLE_SKIP_TEST_IF(getClientMajorVersion() < 3);
399     ANGLE_SKIP_TEST_IF(IsD3D11());
400 
401     glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
402     glClear(GL_COLOR_BUFFER_BIT);
403     EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::black);
404 
405     GLint colorUniformLocation =
406         glGetUniformLocation(blueProgram, angle::essl1_shaders::ColorUniform());
407     ASSERT_NE(colorUniformLocation, -1);
408     glUniform4f(colorUniformLocation, 0.0f, 0.0f, 1.0f, 0.5f);
409     ASSERT_GL_NO_ERROR();
410 
411     // Don't explicitly clear, but draw blue (make sure alpha is not cleared)
412     drawQuad(blueProgram, essl1_shaders::PositionAttrib(), 0.5f);
413     EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::blue);
414 }
415 
416 // Test clearing a RGBA8 Framebuffer
TEST_P(ClearTest,RGBA8Framebuffer)417 TEST_P(ClearTest, RGBA8Framebuffer)
418 {
419     glBindFramebuffer(GL_FRAMEBUFFER, mFBOs[0]);
420 
421     GLTexture texture;
422 
423     glBindTexture(GL_TEXTURE_2D, texture);
424     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, getWindowWidth(), getWindowHeight(), 0, GL_RGBA,
425                  GL_UNSIGNED_BYTE, nullptr);
426     glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture, 0);
427 
428     glClearColor(0.5f, 0.5f, 0.5f, 0.5f);
429     glClear(GL_COLOR_BUFFER_BIT);
430 
431     EXPECT_PIXEL_NEAR(0, 0, 128, 128, 128, 128, 1.0);
432 }
433 
434 // Test to validate that we can go from an RGBA framebuffer attachment, to an RGB one and still
435 // have a correct behavior after.
TEST_P(ClearTest,ChangeFramebufferAttachmentFromRGBAtoRGB)436 TEST_P(ClearTest, ChangeFramebufferAttachmentFromRGBAtoRGB)
437 {
438     // http://anglebug.com/2689
439     ANGLE_SKIP_TEST_IF(IsAndroid() && IsAdreno() && IsOpenGLES());
440 
441     // http://anglebug.com/5165
442     ANGLE_SKIP_TEST_IF(IsOSX() && IsDesktopOpenGL() && IsIntel());
443 
444     ANGLE_GL_PROGRAM(program, angle::essl1_shaders::vs::Simple(),
445                      angle::essl1_shaders::fs::UniformColor());
446     setupQuadVertexBuffer(0.5f, 1.0f);
447     glUseProgram(program);
448     GLint positionLocation = glGetAttribLocation(program, angle::essl1_shaders::PositionAttrib());
449     ASSERT_NE(positionLocation, -1);
450     glVertexAttribPointer(positionLocation, 3, GL_FLOAT, GL_FALSE, 0, nullptr);
451     glEnableVertexAttribArray(positionLocation);
452 
453     GLint colorUniformLocation =
454         glGetUniformLocation(program, angle::essl1_shaders::ColorUniform());
455     ASSERT_NE(colorUniformLocation, -1);
456 
457     glUniform4f(colorUniformLocation, 1.0f, 1.0f, 1.0f, 0.5f);
458 
459     glBindFramebuffer(GL_FRAMEBUFFER, mFBOs[0]);
460 
461     GLTexture texture;
462     glBindTexture(GL_TEXTURE_2D, texture);
463     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, getWindowWidth(), getWindowHeight(), 0, GL_RGBA,
464                  GL_UNSIGNED_BYTE, nullptr);
465     glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture, 0);
466 
467     // Initially clear to black.
468     glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
469     glClear(GL_COLOR_BUFFER_BIT);
470 
471     // Clear with masked color.
472     glColorMask(GL_TRUE, GL_FALSE, GL_TRUE, GL_TRUE);
473     glClearColor(0.5f, 0.5f, 0.5f, 0.75f);
474     glClear(GL_COLOR_BUFFER_BIT);
475     ASSERT_GL_NO_ERROR();
476 
477     // So far so good, we have an RGBA framebuffer that we've cleared to 0.5 everywhere.
478     EXPECT_PIXEL_NEAR(0, 0, 128, 0, 128, 192, 1.0);
479 
480     // In the Vulkan backend, RGB textures are emulated with an RGBA texture format
481     // underneath and we keep a special mask to know that we shouldn't touch the alpha
482     // channel when we have that emulated texture. This test exists to validate that
483     // this mask gets updated correctly when the framebuffer attachment changes.
484     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, getWindowWidth(), getWindowHeight(), 0, GL_RGB,
485                  GL_UNSIGNED_BYTE, nullptr);
486     glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture, 0);
487     ASSERT_GL_NO_ERROR();
488 
489     glDrawArrays(GL_TRIANGLES, 0, 6);
490     ASSERT_GL_NO_ERROR();
491 
492     EXPECT_PIXEL_RECT_EQ(0, 0, getWindowWidth(), getWindowHeight(), GLColor::magenta);
493 }
494 
495 // Test clearing a RGB8 Framebuffer with a color mask.
TEST_P(ClearTest,RGB8WithMaskFramebuffer)496 TEST_P(ClearTest, RGB8WithMaskFramebuffer)
497 {
498     glBindFramebuffer(GL_FRAMEBUFFER, mFBOs[0]);
499 
500     GLTexture texture;
501 
502     glBindTexture(GL_TEXTURE_2D, texture);
503     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, getWindowWidth(), getWindowHeight(), 0, GL_RGB,
504                  GL_UNSIGNED_BYTE, nullptr);
505     glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture, 0);
506 
507     glClearColor(0.2f, 0.4f, 0.6f, 0.8f);
508     glClear(GL_COLOR_BUFFER_BIT);
509 
510     // Since there's no alpha, we expect to get 255 back instead of the clear value (204).
511     EXPECT_PIXEL_NEAR(0, 0, 51, 102, 153, 255, 1.0);
512 
513     glColorMask(GL_TRUE, GL_TRUE, GL_FALSE, GL_TRUE);
514     glClearColor(0.1f, 0.3f, 0.5f, 0.7f);
515     glClear(GL_COLOR_BUFFER_BIT);
516 
517     // The blue channel was masked so its value should be unchanged.
518     EXPECT_PIXEL_NEAR(0, 0, 26, 77, 153, 255, 1.0);
519 
520     // Restore default.
521     glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
522 }
523 
TEST_P(ClearTest,ClearIssue)524 TEST_P(ClearTest, ClearIssue)
525 {
526     glEnable(GL_DEPTH_TEST);
527     glDepthFunc(GL_LEQUAL);
528 
529     glClearColor(0.0, 1.0, 0.0, 1.0);
530     glClearDepthf(0.0);
531     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
532 
533     EXPECT_GL_NO_ERROR();
534 
535     glBindFramebuffer(GL_FRAMEBUFFER, mFBOs[0]);
536 
537     GLRenderbuffer rbo;
538     glBindRenderbuffer(GL_RENDERBUFFER, rbo);
539     glRenderbufferStorage(GL_RENDERBUFFER, GL_RGB565, 16, 16);
540 
541     EXPECT_GL_NO_ERROR();
542 
543     glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, rbo);
544 
545     EXPECT_GL_NO_ERROR();
546 
547     glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
548     glClearDepthf(1.0f);
549     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
550 
551     EXPECT_GL_NO_ERROR();
552 
553     glBindFramebuffer(GL_FRAMEBUFFER, 0);
554     glBindBuffer(GL_ARRAY_BUFFER, 0);
555 
556     ANGLE_GL_PROGRAM(blueProgram, essl1_shaders::vs::Simple(), essl1_shaders::fs::Blue());
557     drawQuad(blueProgram, essl1_shaders::PositionAttrib(), 0.5f);
558 
559     EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::green);
560 }
561 
562 // Regression test for a bug where "glClearDepthf"'s argument was not clamped
563 // In GLES 2 they where declared as GLclampf and the behaviour is the same in GLES 3.2
TEST_P(ClearTest,ClearIsClamped)564 TEST_P(ClearTest, ClearIsClamped)
565 {
566     glClearDepthf(5.0f);
567 
568     GLfloat clear_depth;
569     glGetFloatv(GL_DEPTH_CLEAR_VALUE, &clear_depth);
570     EXPECT_EQ(1.0f, clear_depth);
571 }
572 
573 // Regression test for a bug where "glDepthRangef"'s arguments were not clamped
574 // In GLES 2 they where declared as GLclampf and the behaviour is the same in GLES 3.2
TEST_P(ClearTest,DepthRangefIsClamped)575 TEST_P(ClearTest, DepthRangefIsClamped)
576 {
577     glDepthRangef(1.1f, -4.0f);
578 
579     GLfloat depth_range[2];
580     glGetFloatv(GL_DEPTH_RANGE, depth_range);
581     EXPECT_EQ(1.0f, depth_range[0]);
582     EXPECT_EQ(0.0f, depth_range[1]);
583 }
584 
585 // Test scissored clears on Depth16
TEST_P(ClearTest,Depth16Scissored)586 TEST_P(ClearTest, Depth16Scissored)
587 {
588     GLRenderbuffer renderbuffer;
589     glBindRenderbuffer(GL_RENDERBUFFER, renderbuffer);
590     constexpr int kRenderbufferSize = 64;
591     glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, kRenderbufferSize,
592                           kRenderbufferSize);
593 
594     GLFramebuffer fbo;
595     glBindFramebuffer(GL_FRAMEBUFFER, fbo);
596     glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, renderbuffer);
597 
598     glClearDepthf(0.0f);
599     glClear(GL_DEPTH_BUFFER_BIT);
600 
601     glEnable(GL_SCISSOR_TEST);
602     constexpr int kNumSteps = 13;
603     for (int ndx = 1; ndx < kNumSteps; ndx++)
604     {
605         float perc = static_cast<float>(ndx) / static_cast<float>(kNumSteps);
606         glScissor(0, 0, static_cast<int>(kRenderbufferSize * perc),
607                   static_cast<int>(kRenderbufferSize * perc));
608         glClearDepthf(perc);
609         glClear(GL_DEPTH_BUFFER_BIT);
610     }
611 }
612 
613 // Test scissored clears on Stencil8
TEST_P(ClearTest,Stencil8Scissored)614 TEST_P(ClearTest, Stencil8Scissored)
615 {
616     GLRenderbuffer renderbuffer;
617     glBindRenderbuffer(GL_RENDERBUFFER, renderbuffer);
618     constexpr int kRenderbufferSize = 64;
619     glRenderbufferStorage(GL_RENDERBUFFER, GL_STENCIL_INDEX8, kRenderbufferSize, kRenderbufferSize);
620 
621     GLFramebuffer fbo;
622     glBindFramebuffer(GL_FRAMEBUFFER, fbo);
623     glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, renderbuffer);
624 
625     glClearStencil(0);
626     glClear(GL_STENCIL_BUFFER_BIT);
627 
628     glEnable(GL_SCISSOR_TEST);
629     constexpr int kNumSteps = 13;
630     for (int ndx = 1; ndx < kNumSteps; ndx++)
631     {
632         float perc = static_cast<float>(ndx) / static_cast<float>(kNumSteps);
633         glScissor(0, 0, static_cast<int>(kRenderbufferSize * perc),
634                   static_cast<int>(kRenderbufferSize * perc));
635         glClearStencil(static_cast<int>(perc * 255.0f));
636         glClear(GL_STENCIL_BUFFER_BIT);
637     }
638 }
639 
640 // Covers a bug in the Vulkan back-end where starting a new command buffer in
641 // the masked clear would not trigger descriptor sets to be re-bound.
TEST_P(ClearTest,MaskedClearThenDrawWithUniform)642 TEST_P(ClearTest, MaskedClearThenDrawWithUniform)
643 {
644     // Initialize a program with a uniform.
645     ANGLE_GL_PROGRAM(program, essl1_shaders::vs::Simple(), essl1_shaders::fs::UniformColor());
646     glUseProgram(program);
647 
648     GLint uniLoc = glGetUniformLocation(program, essl1_shaders::ColorUniform());
649     ASSERT_NE(-1, uniLoc);
650     glUniform4f(uniLoc, 0.0f, 1.0f, 0.0f, 1.0f);
651 
652     // Initialize position attribute.
653     GLint posLoc = glGetAttribLocation(program, essl1_shaders::PositionAttrib());
654     ASSERT_NE(-1, posLoc);
655     setupQuadVertexBuffer(0.5f, 1.0f);
656     glVertexAttribPointer(posLoc, 3, GL_FLOAT, GL_FALSE, 0, nullptr);
657     glEnableVertexAttribArray(posLoc);
658 
659     // Initialize a simple FBO.
660     constexpr GLsizei kSize = 2;
661     GLTexture clearTexture;
662     glBindTexture(GL_TEXTURE_2D, clearTexture);
663     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, kSize, kSize, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
664 
665     GLFramebuffer fbo;
666     glBindFramebuffer(GL_FRAMEBUFFER, fbo);
667     glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, clearTexture, 0);
668 
669     glViewport(0, 0, kSize, kSize);
670 
671     // Clear and draw to flush out dirty bits.
672     glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
673     glClear(GL_COLOR_BUFFER_BIT);
674 
675     glDrawArrays(GL_TRIANGLES, 0, 6);
676 
677     // Flush to trigger a new serial.
678     glFlush();
679 
680     // Enable color mask and draw again to trigger the bug.
681     glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_FALSE);
682     glClearColor(1.0f, 0.0f, 0.0f, 0.0f);
683     glClear(GL_COLOR_BUFFER_BIT);
684 
685     glDrawArrays(GL_TRIANGLES, 0, 6);
686 
687     ASSERT_GL_NO_ERROR();
688     EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::green);
689 }
690 
691 // Clear with a mask to verify that masked clear is done properly
692 // (can't use inline or RenderOp clear when some color channels are masked)
TEST_P(ClearTestES3,ClearPlusMaskDrawAndClear)693 TEST_P(ClearTestES3, ClearPlusMaskDrawAndClear)
694 {
695     // Initialize a program with a uniform.
696     ANGLE_GL_PROGRAM(program, essl1_shaders::vs::Simple(), essl1_shaders::fs::UniformColor());
697     glUseProgram(program);
698 
699     GLint uniLoc = glGetUniformLocation(program, essl1_shaders::ColorUniform());
700     ASSERT_NE(-1, uniLoc);
701     glUniform4f(uniLoc, 0.0f, 1.0f, 0.0f, 1.0f);
702 
703     // Initialize position attribute.
704     GLint posLoc = glGetAttribLocation(program, essl1_shaders::PositionAttrib());
705     ASSERT_NE(-1, posLoc);
706     setupQuadVertexBuffer(0.5f, 1.0f);
707     glVertexAttribPointer(posLoc, 3, GL_FLOAT, GL_FALSE, 0, nullptr);
708     glEnableVertexAttribArray(posLoc);
709 
710     // Initialize a simple FBO.
711     constexpr GLsizei kSize = 2;
712     GLTexture clearTexture;
713     glBindTexture(GL_TEXTURE_2D, clearTexture);
714     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, kSize, kSize, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
715 
716     GLFramebuffer fbo;
717     glBindFramebuffer(GL_FRAMEBUFFER, fbo);
718     glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, clearTexture, 0);
719 
720     GLRenderbuffer depthStencil;
721     glBindRenderbuffer(GL_RENDERBUFFER, depthStencil);
722     glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, kSize, kSize);
723     glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER,
724                               depthStencil);
725     ASSERT_GL_NO_ERROR();
726 
727     glViewport(0, 0, kSize, kSize);
728 
729     // Clear and draw to flush out dirty bits.
730     glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
731     glClearDepthf(1.0f);
732     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
733 
734     // Draw green rectangle
735     glDrawArrays(GL_TRIANGLES, 0, 6);
736 
737     // Enable color mask and draw again to trigger the bug.
738     glColorMask(GL_TRUE, GL_FALSE, GL_TRUE, GL_TRUE);
739     glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
740     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
741 
742     // Draw purple-ish rectangle, green should be masked off
743     glUniform4f(uniLoc, 1.0f, 0.25f, 1.0f, 1.0f);
744     glDrawArrays(GL_TRIANGLES, 0, 6);
745 
746     ASSERT_GL_NO_ERROR();
747     EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::white);
748 }
749 
750 // Test that clearing all buffers through glClearColor followed by a clear of a specific buffer
751 // clears to the correct values.
TEST_P(ClearTestES3,ClearMultipleAttachmentsFollowedBySpecificOne)752 TEST_P(ClearTestES3, ClearMultipleAttachmentsFollowedBySpecificOne)
753 {
754     // http://anglebug.com/4092
755     ANGLE_SKIP_TEST_IF(isSwiftshader());
756     constexpr uint32_t kSize            = 16;
757     constexpr uint32_t kAttachmentCount = 4;
758     std::vector<unsigned char> pixelData(kSize * kSize * 4, 255);
759 
760     glBindFramebuffer(GL_FRAMEBUFFER, mFBOs[0]);
761 
762     GLTexture textures[kAttachmentCount];
763     GLenum drawBuffers[kAttachmentCount];
764     GLColor clearValues[kAttachmentCount];
765 
766     for (uint32_t i = 0; i < kAttachmentCount; ++i)
767     {
768         glBindTexture(GL_TEXTURE_2D, textures[i]);
769         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, kSize, kSize, 0, GL_RGBA, GL_UNSIGNED_BYTE,
770                      pixelData.data());
771         glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + i, GL_TEXTURE_2D, textures[i],
772                                0);
773         drawBuffers[i] = GL_COLOR_ATTACHMENT0 + i;
774 
775         clearValues[i].R = static_cast<GLubyte>(1 + i * 20);
776         clearValues[i].G = static_cast<GLubyte>(7 + i * 20);
777         clearValues[i].B = static_cast<GLubyte>(12 + i * 20);
778         clearValues[i].A = static_cast<GLubyte>(16 + i * 20);
779     }
780 
781     glDrawBuffers(kAttachmentCount, drawBuffers);
782 
783     ASSERT_GL_NO_ERROR();
784     EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::white);
785 
786     // Clear all targets.
787     angle::Vector4 clearColor = clearValues[0].toNormalizedVector();
788     glClearColor(clearColor[0], clearColor[1], clearColor[2], clearColor[3]);
789     glClear(GL_COLOR_BUFFER_BIT);
790     ASSERT_GL_NO_ERROR();
791 
792     // Clear odd targets individually.
793     for (uint32_t i = 1; i < kAttachmentCount; i += 2)
794     {
795         clearColor = clearValues[i].toNormalizedVector();
796         glClearBufferfv(GL_COLOR, i, clearColor.data());
797     }
798 
799     // Even attachments should be cleared to color 0, while odd attachments are cleared to their
800     // respective color.
801     for (uint32_t i = 0; i < kAttachmentCount; ++i)
802     {
803         glReadBuffer(GL_COLOR_ATTACHMENT0 + i);
804         ASSERT_GL_NO_ERROR();
805 
806         uint32_t clearIndex   = i % 2 == 0 ? 0 : i;
807         const GLColor &expect = clearValues[clearIndex];
808 
809         EXPECT_PIXEL_COLOR_EQ(0, 0, expect);
810         EXPECT_PIXEL_COLOR_EQ(0, kSize - 1, expect);
811         EXPECT_PIXEL_COLOR_EQ(kSize - 1, 0, expect);
812         EXPECT_PIXEL_COLOR_EQ(kSize - 1, kSize - 1, expect);
813     }
814 }
815 
816 // Test that clearing each render target individually works.  In the Vulkan backend, this should be
817 // done in a single render pass.
TEST_P(ClearTestES3,ClearMultipleAttachmentsIndividually)818 TEST_P(ClearTestES3, ClearMultipleAttachmentsIndividually)
819 {
820     // http://anglebug.com/4855
821     ANGLE_SKIP_TEST_IF(IsWindows() && IsIntel() && IsVulkan());
822 
823     constexpr uint32_t kSize             = 16;
824     constexpr uint32_t kAttachmentCount  = 2;
825     constexpr float kDepthClearValue     = 0.125f;
826     constexpr int32_t kStencilClearValue = 0x67;
827     std::vector<unsigned char> pixelData(kSize * kSize * 4, 255);
828 
829     glBindFramebuffer(GL_FRAMEBUFFER, mFBOs[0]);
830 
831     GLTexture textures[kAttachmentCount];
832     GLRenderbuffer depthStencil;
833     GLenum drawBuffers[kAttachmentCount];
834     GLColor clearValues[kAttachmentCount];
835 
836     for (uint32_t i = 0; i < kAttachmentCount; ++i)
837     {
838         glBindTexture(GL_TEXTURE_2D, textures[i]);
839         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, kSize, kSize, 0, GL_RGBA, GL_UNSIGNED_BYTE,
840                      pixelData.data());
841         glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + i, GL_TEXTURE_2D, textures[i],
842                                0);
843         drawBuffers[i] = GL_COLOR_ATTACHMENT0 + i;
844 
845         clearValues[i].R = static_cast<GLubyte>(1 + i * 20);
846         clearValues[i].G = static_cast<GLubyte>(7 + i * 20);
847         clearValues[i].B = static_cast<GLubyte>(12 + i * 20);
848         clearValues[i].A = static_cast<GLubyte>(16 + i * 20);
849     }
850 
851     glBindRenderbuffer(GL_RENDERBUFFER, depthStencil);
852     glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, kSize, kSize);
853     glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER,
854                               depthStencil);
855 
856     glDrawBuffers(kAttachmentCount, drawBuffers);
857 
858     ASSERT_GL_NO_ERROR();
859     EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::white);
860 
861     for (uint32_t i = 0; i < kAttachmentCount; ++i)
862     {
863         glClearBufferfv(GL_COLOR, i, clearValues[i].toNormalizedVector().data());
864     }
865 
866     glClearBufferfv(GL_DEPTH, 0, &kDepthClearValue);
867     glClearBufferiv(GL_STENCIL, 0, &kStencilClearValue);
868     ASSERT_GL_NO_ERROR();
869 
870     for (uint32_t i = 0; i < kAttachmentCount; ++i)
871     {
872         glReadBuffer(GL_COLOR_ATTACHMENT0 + i);
873         ASSERT_GL_NO_ERROR();
874 
875         const GLColor &expect = clearValues[i];
876 
877         EXPECT_PIXEL_COLOR_EQ(0, 0, expect);
878         EXPECT_PIXEL_COLOR_EQ(0, kSize - 1, expect);
879         EXPECT_PIXEL_COLOR_EQ(kSize - 1, 0, expect);
880         EXPECT_PIXEL_COLOR_EQ(kSize - 1, kSize - 1, expect);
881     }
882 
883     glReadBuffer(GL_COLOR_ATTACHMENT0);
884     for (uint32_t i = 1; i < kAttachmentCount; ++i)
885         drawBuffers[i] = GL_NONE;
886     glDrawBuffers(kAttachmentCount, drawBuffers);
887 
888     verifyDepth(kDepthClearValue, kSize);
889     verifyStencil(kStencilClearValue, kSize);
890 }
891 
892 // Test that clearing multiple attachments in the presence of a color mask, scissor or both
893 // correctly clears all the attachments.
TEST_P(ClearTestES3,MaskedScissoredClearMultipleAttachments)894 TEST_P(ClearTestES3, MaskedScissoredClearMultipleAttachments)
895 {
896     constexpr uint32_t kSize            = 16;
897     constexpr uint32_t kAttachmentCount = 2;
898     std::vector<unsigned char> pixelData(kSize * kSize * 4, 255);
899 
900     glBindFramebuffer(GL_FRAMEBUFFER, mFBOs[0]);
901 
902     GLTexture textures[kAttachmentCount];
903     GLenum drawBuffers[kAttachmentCount];
904 
905     for (uint32_t i = 0; i < kAttachmentCount; ++i)
906     {
907         glBindTexture(GL_TEXTURE_2D, textures[i]);
908         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, kSize, kSize, 0, GL_RGBA, GL_UNSIGNED_BYTE,
909                      pixelData.data());
910         glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + i, GL_TEXTURE_2D, textures[i],
911                                0);
912         drawBuffers[i] = GL_COLOR_ATTACHMENT0 + i;
913     }
914 
915     glDrawBuffers(kAttachmentCount, drawBuffers);
916 
917     ASSERT_GL_NO_ERROR();
918     EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::white);
919 
920     // Masked clear
921     GLColor clearColorMasked(31, 63, 255, 191);
922     angle::Vector4 clearColor = GLColor(31, 63, 127, 191).toNormalizedVector();
923 
924     glColorMask(GL_TRUE, GL_TRUE, GL_FALSE, GL_TRUE);
925     glClearColor(clearColor[0], clearColor[1], clearColor[2], clearColor[3]);
926     glClear(GL_COLOR_BUFFER_BIT);
927     ASSERT_GL_NO_ERROR();
928 
929     // All attachments should be cleared, with the blue channel untouched
930     for (uint32_t i = 0; i < kAttachmentCount; ++i)
931     {
932         glReadBuffer(GL_COLOR_ATTACHMENT0 + i);
933         ASSERT_GL_NO_ERROR();
934 
935         EXPECT_PIXEL_COLOR_EQ(0, 0, clearColorMasked);
936         EXPECT_PIXEL_COLOR_EQ(0, kSize - 1, clearColorMasked);
937         EXPECT_PIXEL_COLOR_EQ(kSize - 1, 0, clearColorMasked);
938         EXPECT_PIXEL_COLOR_EQ(kSize - 1, kSize - 1, clearColorMasked);
939         EXPECT_PIXEL_COLOR_EQ(kSize / 2, kSize / 2, clearColorMasked);
940     }
941 
942     // Masked scissored clear
943     GLColor clearColorMaskedScissored(63, 127, 255, 31);
944     clearColor = GLColor(63, 127, 191, 31).toNormalizedVector();
945 
946     glClearColor(clearColor[0], clearColor[1], clearColor[2], clearColor[3]);
947     glEnable(GL_SCISSOR_TEST);
948     glScissor(kSize / 6, kSize / 6, kSize / 3, kSize / 3);
949     glClear(GL_COLOR_BUFFER_BIT);
950     ASSERT_GL_NO_ERROR();
951 
952     // The corners should keep the previous value while the center is cleared, except its blue
953     // channel.
954     for (uint32_t i = 0; i < kAttachmentCount; ++i)
955     {
956         glReadBuffer(GL_COLOR_ATTACHMENT0 + i);
957         ASSERT_GL_NO_ERROR();
958 
959         EXPECT_PIXEL_COLOR_EQ(0, 0, clearColorMasked);
960         EXPECT_PIXEL_COLOR_EQ(0, kSize - 1, clearColorMasked);
961         EXPECT_PIXEL_COLOR_EQ(kSize - 1, 0, clearColorMasked);
962         EXPECT_PIXEL_COLOR_EQ(kSize - 1, kSize - 1, clearColorMasked);
963         EXPECT_PIXEL_COLOR_EQ(kSize / 3, 2 * kSize / 3, clearColorMasked);
964         EXPECT_PIXEL_COLOR_EQ(2 * kSize / 3, kSize / 3, clearColorMasked);
965         EXPECT_PIXEL_COLOR_EQ(2 * kSize / 3, 2 * kSize / 3, clearColorMasked);
966 
967         EXPECT_PIXEL_COLOR_EQ(kSize / 3, kSize / 3, clearColorMaskedScissored);
968     }
969 
970     // Scissored clear
971     GLColor clearColorScissored(127, 191, 31, 63);
972     clearColor = GLColor(127, 191, 31, 63).toNormalizedVector();
973 
974     glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
975     glClearColor(clearColor[0], clearColor[1], clearColor[2], clearColor[3]);
976     glClear(GL_COLOR_BUFFER_BIT);
977     ASSERT_GL_NO_ERROR();
978 
979     // The corners should keep the old value while all channels of the center are cleared.
980     for (uint32_t i = 0; i < kAttachmentCount; ++i)
981     {
982         glReadBuffer(GL_COLOR_ATTACHMENT0 + i);
983         ASSERT_GL_NO_ERROR();
984 
985         EXPECT_PIXEL_COLOR_EQ(0, 0, clearColorMasked);
986         EXPECT_PIXEL_COLOR_EQ(0, kSize - 1, clearColorMasked);
987         EXPECT_PIXEL_COLOR_EQ(kSize - 1, 0, clearColorMasked);
988         EXPECT_PIXEL_COLOR_EQ(kSize - 1, kSize - 1, clearColorMasked);
989         EXPECT_PIXEL_COLOR_EQ(kSize / 3, 2 * kSize / 3, clearColorMasked);
990         EXPECT_PIXEL_COLOR_EQ(2 * kSize / 3, kSize / 3, clearColorMasked);
991         EXPECT_PIXEL_COLOR_EQ(2 * kSize / 3, 2 * kSize / 3, clearColorMasked);
992 
993         EXPECT_PIXEL_COLOR_EQ(kSize / 3, kSize / 3, clearColorScissored);
994     }
995 }
996 
997 // Test clearing multiple attachments in the presence of an indexed color mask.
TEST_P(ClearTestES3,MaskedIndexedClearMultipleAttachments)998 TEST_P(ClearTestES3, MaskedIndexedClearMultipleAttachments)
999 {
1000     ANGLE_SKIP_TEST_IF(!IsGLExtensionEnabled("GL_OES_draw_buffers_indexed"));
1001 
1002     constexpr uint32_t kSize            = 16;
1003     constexpr uint32_t kAttachmentCount = 4;
1004     std::vector<unsigned char> pixelData(kSize * kSize * 4, 255);
1005 
1006     glBindFramebuffer(GL_FRAMEBUFFER, mFBOs[0]);
1007 
1008     GLTexture textures[kAttachmentCount];
1009     GLenum drawBuffers[kAttachmentCount];
1010 
1011     for (uint32_t i = 0; i < kAttachmentCount; ++i)
1012     {
1013         glBindTexture(GL_TEXTURE_2D, textures[i]);
1014         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, kSize, kSize, 0, GL_RGBA, GL_UNSIGNED_BYTE,
1015                      pixelData.data());
1016         glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + i, GL_TEXTURE_2D, textures[i],
1017                                0);
1018         drawBuffers[i] = GL_COLOR_ATTACHMENT0 + i;
1019     }
1020 
1021     glDrawBuffers(kAttachmentCount, drawBuffers);
1022 
1023     ASSERT_GL_NO_ERROR();
1024     EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::white);
1025 
1026     // Masked clear
1027     GLColor clearColorMasked(31, 63, 255, 191);
1028     angle::Vector4 clearColor = GLColor(31, 63, 127, 191).toNormalizedVector();
1029 
1030     // Block blue channel for all attachements
1031     glColorMask(GL_TRUE, GL_TRUE, GL_FALSE, GL_TRUE);
1032 
1033     // Unblock blue channel for attachments 0 and 1
1034     glColorMaskiOES(0, GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
1035     glColorMaskiOES(1, GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
1036 
1037     glClearColor(clearColor[0], clearColor[1], clearColor[2], clearColor[3]);
1038     glClear(GL_COLOR_BUFFER_BIT);
1039     ASSERT_GL_NO_ERROR();
1040 
1041     // All attachments should be cleared, with the blue channel untouched for all attachments but 1.
1042     for (uint32_t i = 0; i < kAttachmentCount; ++i)
1043     {
1044         glReadBuffer(GL_COLOR_ATTACHMENT0 + i);
1045         ASSERT_GL_NO_ERROR();
1046 
1047         const GLColor attachmentColor = (i > 1) ? clearColorMasked : clearColor;
1048         EXPECT_PIXEL_COLOR_EQ(0, 0, attachmentColor);
1049         EXPECT_PIXEL_COLOR_EQ(0, kSize - 1, attachmentColor);
1050         EXPECT_PIXEL_COLOR_EQ(kSize - 1, 0, attachmentColor);
1051         EXPECT_PIXEL_COLOR_EQ(kSize - 1, kSize - 1, attachmentColor);
1052         EXPECT_PIXEL_COLOR_EQ(kSize / 2, kSize / 2, attachmentColor);
1053     }
1054 }
1055 
1056 // Test that clearing multiple attachments of different nature (float, int and uint) in the
1057 // presence of a color mask works correctly.  In the Vulkan backend, this exercises clearWithDraw
1058 // and the relevant internal shaders.
TEST_P(ClearTestES3,MaskedClearHeterogeneousAttachments)1059 TEST_P(ClearTestES3, MaskedClearHeterogeneousAttachments)
1060 {
1061     // http://anglebug.com/4855
1062     ANGLE_SKIP_TEST_IF(IsWindows() && IsIntel() && IsVulkan());
1063 
1064     // TODO(anglebug.com/5491)
1065     ANGLE_SKIP_TEST_IF(IsIOS() && IsOpenGLES());
1066 
1067     constexpr uint32_t kSize                              = 16;
1068     constexpr uint32_t kAttachmentCount                   = 3;
1069     constexpr float kDepthClearValue                      = 0.256f;
1070     constexpr int32_t kStencilClearValue                  = 0x1D;
1071     constexpr GLenum kAttachmentFormats[kAttachmentCount] = {
1072         GL_RGBA8,
1073         GL_RGBA8I,
1074         GL_RGBA8UI,
1075     };
1076     constexpr GLenum kDataFormats[kAttachmentCount] = {
1077         GL_RGBA,
1078         GL_RGBA_INTEGER,
1079         GL_RGBA_INTEGER,
1080     };
1081     constexpr GLenum kDataTypes[kAttachmentCount] = {
1082         GL_UNSIGNED_BYTE,
1083         GL_BYTE,
1084         GL_UNSIGNED_BYTE,
1085     };
1086 
1087     std::vector<unsigned char> pixelData(kSize * kSize * 4, 0);
1088 
1089     glBindFramebuffer(GL_FRAMEBUFFER, mFBOs[0]);
1090 
1091     GLTexture textures[kAttachmentCount];
1092     GLRenderbuffer depthStencil;
1093     GLenum drawBuffers[kAttachmentCount];
1094 
1095     for (uint32_t i = 0; i < kAttachmentCount; ++i)
1096     {
1097         glBindTexture(GL_TEXTURE_2D, textures[i]);
1098         glTexImage2D(GL_TEXTURE_2D, 0, kAttachmentFormats[i], kSize, kSize, 0, kDataFormats[i],
1099                      kDataTypes[i], pixelData.data());
1100         glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + i, GL_TEXTURE_2D, textures[i],
1101                                0);
1102         drawBuffers[i] = GL_COLOR_ATTACHMENT0 + i;
1103     }
1104 
1105     glBindRenderbuffer(GL_RENDERBUFFER, depthStencil);
1106     glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, kSize, kSize);
1107     glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER,
1108                               depthStencil);
1109 
1110     glDrawBuffers(kAttachmentCount, drawBuffers);
1111 
1112     ASSERT_GL_NO_ERROR();
1113     EXPECT_PIXEL_EQ(0, 0, 0, 0, 0, 0);
1114 
1115     // Mask out red for all clears
1116     glColorMask(GL_FALSE, GL_TRUE, GL_TRUE, GL_TRUE);
1117 
1118     glClearBufferfv(GL_DEPTH, 0, &kDepthClearValue);
1119     glClearBufferiv(GL_STENCIL, 0, &kStencilClearValue);
1120 
1121     GLColor clearValuef = {25, 50, 75, 100};
1122     glClearBufferfv(GL_COLOR, 0, clearValuef.toNormalizedVector().data());
1123 
1124     int clearValuei[4] = {10, -20, 30, -40};
1125     glClearBufferiv(GL_COLOR, 1, clearValuei);
1126 
1127     uint32_t clearValueui[4] = {50, 60, 70, 80};
1128     glClearBufferuiv(GL_COLOR, 2, clearValueui);
1129 
1130     ASSERT_GL_NO_ERROR();
1131 
1132     {
1133         glReadBuffer(GL_COLOR_ATTACHMENT0);
1134         ASSERT_GL_NO_ERROR();
1135 
1136         GLColor expect = clearValuef;
1137         expect.R       = 0;
1138 
1139         EXPECT_PIXEL_COLOR_EQ(0, 0, expect);
1140         EXPECT_PIXEL_COLOR_EQ(0, kSize - 1, expect);
1141         EXPECT_PIXEL_COLOR_EQ(kSize - 1, 0, expect);
1142         EXPECT_PIXEL_COLOR_EQ(kSize - 1, kSize - 1, expect);
1143     }
1144 
1145     {
1146         glReadBuffer(GL_COLOR_ATTACHMENT1);
1147         ASSERT_GL_NO_ERROR();
1148 
1149         EXPECT_PIXEL_8I(0, 0, 0, clearValuei[1], clearValuei[2], clearValuei[3]);
1150         EXPECT_PIXEL_8I(0, kSize - 1, 0, clearValuei[1], clearValuei[2], clearValuei[3]);
1151         EXPECT_PIXEL_8I(kSize - 1, 0, 0, clearValuei[1], clearValuei[2], clearValuei[3]);
1152         EXPECT_PIXEL_8I(kSize - 1, kSize - 1, 0, clearValuei[1], clearValuei[2], clearValuei[3]);
1153     }
1154 
1155     {
1156         glReadBuffer(GL_COLOR_ATTACHMENT2);
1157         ASSERT_GL_NO_ERROR();
1158 
1159         EXPECT_PIXEL_8UI(0, 0, 0, clearValueui[1], clearValueui[2], clearValueui[3]);
1160         EXPECT_PIXEL_8UI(0, kSize - 1, 0, clearValueui[1], clearValueui[2], clearValueui[3]);
1161         EXPECT_PIXEL_8UI(kSize - 1, 0, 0, clearValueui[1], clearValueui[2], clearValueui[3]);
1162         EXPECT_PIXEL_8UI(kSize - 1, kSize - 1, 0, clearValueui[1], clearValueui[2],
1163                          clearValueui[3]);
1164     }
1165 
1166     glReadBuffer(GL_COLOR_ATTACHMENT0);
1167     for (uint32_t i = 1; i < kAttachmentCount; ++i)
1168         drawBuffers[i] = GL_NONE;
1169     glDrawBuffers(kAttachmentCount, drawBuffers);
1170 
1171     verifyDepth(kDepthClearValue, kSize);
1172     verifyStencil(kStencilClearValue, kSize);
1173 }
1174 
1175 // Test that clearing multiple attachments of different nature (float, int and uint) in the
1176 // presence of a scissor test works correctly.  In the Vulkan backend, this exercises clearWithDraw
1177 // and the relevant internal shaders.
TEST_P(ClearTestES3,ScissoredClearHeterogeneousAttachments)1178 TEST_P(ClearTestES3, ScissoredClearHeterogeneousAttachments)
1179 {
1180     // http://anglebug.com/4855
1181     ANGLE_SKIP_TEST_IF(IsWindows() && IsIntel() && IsVulkan());
1182 
1183     // http://anglebug.com/5116
1184     ANGLE_SKIP_TEST_IF(IsWindows() && (IsOpenGL() || IsD3D11()) && IsAMD());
1185 
1186     // http://anglebug.com/5237
1187     ANGLE_SKIP_TEST_IF(IsWindows7() && IsD3D11() && IsNVIDIA());
1188 
1189     // TODO(anglebug.com/5491)
1190     ANGLE_SKIP_TEST_IF(IsIOS() && IsOpenGLES());
1191 
1192     constexpr uint32_t kSize                              = 16;
1193     constexpr uint32_t kHalfSize                          = kSize / 2;
1194     constexpr uint32_t kAttachmentCount                   = 3;
1195     constexpr float kDepthClearValue                      = 0.256f;
1196     constexpr int32_t kStencilClearValue                  = 0x1D;
1197     constexpr GLenum kAttachmentFormats[kAttachmentCount] = {
1198         GL_RGBA8,
1199         GL_RGBA8I,
1200         GL_RGBA8UI,
1201     };
1202     constexpr GLenum kDataFormats[kAttachmentCount] = {
1203         GL_RGBA,
1204         GL_RGBA_INTEGER,
1205         GL_RGBA_INTEGER,
1206     };
1207     constexpr GLenum kDataTypes[kAttachmentCount] = {
1208         GL_UNSIGNED_BYTE,
1209         GL_BYTE,
1210         GL_UNSIGNED_BYTE,
1211     };
1212 
1213     std::vector<unsigned char> pixelData(kSize * kSize * 4, 0);
1214 
1215     glBindFramebuffer(GL_FRAMEBUFFER, mFBOs[0]);
1216 
1217     GLTexture textures[kAttachmentCount];
1218     GLRenderbuffer depthStencil;
1219     GLenum drawBuffers[kAttachmentCount];
1220 
1221     for (uint32_t i = 0; i < kAttachmentCount; ++i)
1222     {
1223         glBindTexture(GL_TEXTURE_2D, textures[i]);
1224         glTexImage2D(GL_TEXTURE_2D, 0, kAttachmentFormats[i], kSize, kSize, 0, kDataFormats[i],
1225                      kDataTypes[i], pixelData.data());
1226         glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + i, GL_TEXTURE_2D, textures[i],
1227                                0);
1228         drawBuffers[i] = GL_COLOR_ATTACHMENT0 + i;
1229     }
1230 
1231     glBindRenderbuffer(GL_RENDERBUFFER, depthStencil);
1232     glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, kSize, kSize);
1233     glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER,
1234                               depthStencil);
1235 
1236     glDrawBuffers(kAttachmentCount, drawBuffers);
1237 
1238     ASSERT_GL_NO_ERROR();
1239     EXPECT_PIXEL_EQ(0, 0, 0, 0, 0, 0);
1240 
1241     // Enable scissor test
1242     glScissor(0, 0, kHalfSize, kHalfSize);
1243     glEnable(GL_SCISSOR_TEST);
1244 
1245     GLColor clearValuef         = {25, 50, 75, 100};
1246     angle::Vector4 clearValuefv = clearValuef.toNormalizedVector();
1247 
1248     glClearColor(clearValuefv.x(), clearValuefv.y(), clearValuefv.z(), clearValuefv.w());
1249     glClearDepthf(kDepthClearValue);
1250 
1251     // clear stencil.
1252     glClearBufferiv(GL_STENCIL, 0, &kStencilClearValue);
1253 
1254     // clear float color attachment & depth together
1255     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
1256 
1257     // clear integer attachment.
1258     int clearValuei[4] = {10, -20, 30, -40};
1259     glClearBufferiv(GL_COLOR, 1, clearValuei);
1260 
1261     // clear unsigned integer attachment
1262     uint32_t clearValueui[4] = {50, 60, 70, 80};
1263     glClearBufferuiv(GL_COLOR, 2, clearValueui);
1264 
1265     ASSERT_GL_NO_ERROR();
1266 
1267     {
1268         glReadBuffer(GL_COLOR_ATTACHMENT0);
1269         ASSERT_GL_NO_ERROR();
1270 
1271         GLColor expect = clearValuef;
1272 
1273         EXPECT_PIXEL_COLOR_EQ(0, 0, expect);
1274         EXPECT_PIXEL_COLOR_EQ(0, kHalfSize - 1, expect);
1275         EXPECT_PIXEL_COLOR_EQ(kHalfSize - 1, 0, expect);
1276         EXPECT_PIXEL_COLOR_EQ(kHalfSize - 1, kHalfSize - 1, expect);
1277         EXPECT_PIXEL_EQ(kHalfSize + 1, kHalfSize + 1, 0, 0, 0, 0);
1278     }
1279 
1280     {
1281         glReadBuffer(GL_COLOR_ATTACHMENT1);
1282         ASSERT_GL_NO_ERROR();
1283 
1284         EXPECT_PIXEL_8I(0, 0, clearValuei[0], clearValuei[1], clearValuei[2], clearValuei[3]);
1285         EXPECT_PIXEL_8I(0, kHalfSize - 1, clearValuei[0], clearValuei[1], clearValuei[2],
1286                         clearValuei[3]);
1287         EXPECT_PIXEL_8I(kHalfSize - 1, 0, clearValuei[0], clearValuei[1], clearValuei[2],
1288                         clearValuei[3]);
1289         EXPECT_PIXEL_8I(kHalfSize - 1, kHalfSize - 1, clearValuei[0], clearValuei[1],
1290                         clearValuei[2], clearValuei[3]);
1291         EXPECT_PIXEL_8I(kHalfSize + 1, kHalfSize + 1, 0, 0, 0, 0);
1292     }
1293 
1294     {
1295         glReadBuffer(GL_COLOR_ATTACHMENT2);
1296         ASSERT_GL_NO_ERROR();
1297 
1298         EXPECT_PIXEL_8UI(0, 0, clearValueui[0], clearValueui[1], clearValueui[2], clearValueui[3]);
1299         EXPECT_PIXEL_8UI(0, kHalfSize - 1, clearValueui[0], clearValueui[1], clearValueui[2],
1300                          clearValueui[3]);
1301         EXPECT_PIXEL_8UI(kHalfSize - 1, 0, clearValueui[0], clearValueui[1], clearValueui[2],
1302                          clearValueui[3]);
1303         EXPECT_PIXEL_8UI(kHalfSize - 1, kHalfSize - 1, clearValueui[0], clearValueui[1],
1304                          clearValueui[2], clearValueui[3]);
1305         EXPECT_PIXEL_8UI(kHalfSize + 1, kHalfSize + 1, 0, 0, 0, 0);
1306     }
1307 
1308     glReadBuffer(GL_COLOR_ATTACHMENT0);
1309     for (uint32_t i = 1; i < kAttachmentCount; ++i)
1310         drawBuffers[i] = GL_NONE;
1311     glDrawBuffers(kAttachmentCount, drawBuffers);
1312 
1313     verifyDepth(kDepthClearValue, kHalfSize);
1314     verifyStencil(kStencilClearValue, kHalfSize);
1315 }
1316 
1317 // This tests a bug where in a masked clear when calling "ClearBuffer", we would
1318 // mistakenly clear every channel (including the masked-out ones)
TEST_P(ClearTestES3,MaskedClearBufferBug)1319 TEST_P(ClearTestES3, MaskedClearBufferBug)
1320 {
1321     // TODO(syoussefi): Qualcomm driver crashes in the presence of VK_ATTACHMENT_UNUSED.
1322     // http://anglebug.com/3423
1323     ANGLE_SKIP_TEST_IF(IsVulkan() && IsAndroid());
1324 
1325     unsigned char pixelData[] = {255, 255, 255, 255};
1326 
1327     glBindFramebuffer(GL_FRAMEBUFFER, mFBOs[0]);
1328 
1329     GLTexture textures[2];
1330 
1331     glBindTexture(GL_TEXTURE_2D, textures[0]);
1332     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixelData);
1333     glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, textures[0], 0);
1334 
1335     glBindTexture(GL_TEXTURE_2D, textures[1]);
1336     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixelData);
1337     glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D, textures[1], 0);
1338 
1339     ASSERT_GL_NO_ERROR();
1340     EXPECT_PIXEL_EQ(0, 0, 255, 255, 255, 255);
1341 
1342     float clearValue[]   = {0, 0.5f, 0.5f, 1.0f};
1343     GLenum drawBuffers[] = {GL_NONE, GL_COLOR_ATTACHMENT1};
1344     glDrawBuffers(2, drawBuffers);
1345     glColorMask(GL_TRUE, GL_TRUE, GL_FALSE, GL_TRUE);
1346     glClearBufferfv(GL_COLOR, 1, clearValue);
1347 
1348     ASSERT_GL_NO_ERROR();
1349     EXPECT_PIXEL_EQ(0, 0, 255, 255, 255, 255);
1350 
1351     glReadBuffer(GL_COLOR_ATTACHMENT1);
1352     ASSERT_GL_NO_ERROR();
1353 
1354     EXPECT_PIXEL_NEAR(0, 0, 0, 127, 255, 255, 1);
1355 }
1356 
TEST_P(ClearTestES3,BadFBOSerialBug)1357 TEST_P(ClearTestES3, BadFBOSerialBug)
1358 {
1359     // First make a simple framebuffer, and clear it to green
1360     glBindFramebuffer(GL_FRAMEBUFFER, mFBOs[0]);
1361 
1362     GLTexture textures[2];
1363 
1364     glBindTexture(GL_TEXTURE_2D, textures[0]);
1365     glTexStorage2D(GL_TEXTURE_2D, 1, GL_RGBA8, getWindowWidth(), getWindowHeight());
1366     glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, textures[0], 0);
1367 
1368     GLenum drawBuffers[] = {GL_COLOR_ATTACHMENT0};
1369     glDrawBuffers(1, drawBuffers);
1370 
1371     float clearValues1[] = {0.0f, 1.0f, 0.0f, 1.0f};
1372     glClearBufferfv(GL_COLOR, 0, clearValues1);
1373 
1374     ASSERT_GL_NO_ERROR();
1375     EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::green);
1376 
1377     // Next make a second framebuffer, and draw it to red
1378     // (Triggers bad applied render target serial)
1379     GLFramebuffer fbo2;
1380     glBindFramebuffer(GL_FRAMEBUFFER, fbo2);
1381     ASSERT_GL_NO_ERROR();
1382 
1383     glBindTexture(GL_TEXTURE_2D, textures[1]);
1384     glTexStorage2D(GL_TEXTURE_2D, 1, GL_RGBA8, getWindowWidth(), getWindowHeight());
1385     glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, textures[1], 0);
1386 
1387     glDrawBuffers(1, drawBuffers);
1388 
1389     ANGLE_GL_PROGRAM(blueProgram, essl1_shaders::vs::Simple(), essl1_shaders::fs::Red());
1390     drawQuad(blueProgram, essl1_shaders::PositionAttrib(), 0.5f);
1391 
1392     ASSERT_GL_NO_ERROR();
1393     EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::red);
1394 
1395     // Check that the first framebuffer is still green.
1396     glBindFramebuffer(GL_FRAMEBUFFER, mFBOs[0]);
1397     EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::green);
1398 }
1399 
1400 // Test that SRGB framebuffers clear to the linearized clear color
TEST_P(ClearTestES3,SRGBClear)1401 TEST_P(ClearTestES3, SRGBClear)
1402 {
1403     // First make a simple framebuffer, and clear it
1404     glBindFramebuffer(GL_FRAMEBUFFER, mFBOs[0]);
1405 
1406     GLTexture texture;
1407 
1408     glBindTexture(GL_TEXTURE_2D, texture);
1409     glTexStorage2D(GL_TEXTURE_2D, 1, GL_SRGB8_ALPHA8, getWindowWidth(), getWindowHeight());
1410     glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture, 0);
1411 
1412     glClearColor(0.5f, 0.5f, 0.5f, 0.5f);
1413     glClear(GL_COLOR_BUFFER_BIT);
1414 
1415     EXPECT_PIXEL_NEAR(0, 0, 188, 188, 188, 128, 1.0);
1416 }
1417 
1418 // Test that framebuffers with mixed SRGB/Linear attachments clear to the correct color for each
1419 // attachment
TEST_P(ClearTestES3,MixedSRGBClear)1420 TEST_P(ClearTestES3, MixedSRGBClear)
1421 {
1422     glBindFramebuffer(GL_FRAMEBUFFER, mFBOs[0]);
1423 
1424     GLTexture textures[2];
1425 
1426     glBindTexture(GL_TEXTURE_2D, textures[0]);
1427     glTexStorage2D(GL_TEXTURE_2D, 1, GL_SRGB8_ALPHA8, getWindowWidth(), getWindowHeight());
1428     glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, textures[0], 0);
1429 
1430     glBindTexture(GL_TEXTURE_2D, textures[1]);
1431     glTexStorage2D(GL_TEXTURE_2D, 1, GL_RGBA8, getWindowWidth(), getWindowHeight());
1432     glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D, textures[1], 0);
1433 
1434     GLenum drawBuffers[] = {GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1};
1435     glDrawBuffers(2, drawBuffers);
1436 
1437     // Clear both textures
1438     glClearColor(0.5f, 0.5f, 0.5f, 0.5f);
1439     glClear(GL_COLOR_BUFFER_BIT);
1440 
1441     glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
1442     glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D, 0, 0);
1443 
1444     // Check value of texture0
1445     glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, textures[0], 0);
1446     EXPECT_PIXEL_NEAR(0, 0, 188, 188, 188, 128, 1.0);
1447 
1448     // Check value of texture1
1449     glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, textures[1], 0);
1450     EXPECT_PIXEL_NEAR(0, 0, 128, 128, 128, 128, 1.0);
1451 }
1452 
1453 // This test covers a D3D11 bug where calling ClearRenderTargetView sometimes wouldn't sync
1454 // before a draw call. The test draws small quads to a larger FBO (the default back buffer).
1455 // Before each blit to the back buffer it clears the quad to a certain color using
1456 // ClearBufferfv to give a solid color. The sync problem goes away if we insert a call to
1457 // flush or finish after ClearBufferfv or each draw.
TEST_P(ClearTestES3,RepeatedClear)1458 TEST_P(ClearTestES3, RepeatedClear)
1459 {
1460     // Fails on 431.02 driver. http://anglebug.com/3748
1461     ANGLE_SKIP_TEST_IF(IsWindows() && IsNVIDIA() && IsVulkan());
1462     ANGLE_SKIP_TEST_IF(IsARM64() && IsWindows() && IsD3D());
1463 
1464     constexpr char kVS[] =
1465         "#version 300 es\n"
1466         "in highp vec2 position;\n"
1467         "out highp vec2 v_coord;\n"
1468         "void main(void)\n"
1469         "{\n"
1470         "    gl_Position = vec4(position, 0, 1);\n"
1471         "    vec2 texCoord = (position * 0.5) + 0.5;\n"
1472         "    v_coord = texCoord;\n"
1473         "}\n";
1474 
1475     constexpr char kFS[] =
1476         "#version 300 es\n"
1477         "in highp vec2 v_coord;\n"
1478         "out highp vec4 color;\n"
1479         "uniform sampler2D tex;\n"
1480         "void main()\n"
1481         "{\n"
1482         "    color = texture(tex, v_coord);\n"
1483         "}\n";
1484 
1485     ANGLE_GL_PROGRAM(program, kVS, kFS);
1486 
1487     mTextures.resize(1, 0);
1488     glGenTextures(1, mTextures.data());
1489 
1490     GLenum format           = GL_RGBA8;
1491     const int numRowsCols   = 3;
1492     const int cellSize      = 32;
1493     const int fboSize       = cellSize;
1494     const int backFBOSize   = cellSize * numRowsCols;
1495     const float fmtValueMin = 0.0f;
1496     const float fmtValueMax = 1.0f;
1497 
1498     glBindTexture(GL_TEXTURE_2D, mTextures[0]);
1499     glTexStorage2D(GL_TEXTURE_2D, 1, format, fboSize, fboSize);
1500     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1501     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1502     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1503     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1504     ASSERT_GL_NO_ERROR();
1505 
1506     glBindFramebuffer(GL_FRAMEBUFFER, mFBOs[0]);
1507     glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, mTextures[0], 0);
1508     ASSERT_GL_NO_ERROR();
1509 
1510     ASSERT_GLENUM_EQ(GL_FRAMEBUFFER_COMPLETE, glCheckFramebufferStatus(GL_FRAMEBUFFER));
1511 
1512     // larger fbo bound -- clear to transparent black
1513     glUseProgram(program);
1514     GLint uniLoc = glGetUniformLocation(program, "tex");
1515     ASSERT_NE(-1, uniLoc);
1516     glUniform1i(uniLoc, 0);
1517     glBindTexture(GL_TEXTURE_2D, mTextures[0]);
1518 
1519     GLint positionLocation = glGetAttribLocation(program, "position");
1520     ASSERT_NE(-1, positionLocation);
1521 
1522     glUseProgram(program);
1523 
1524     for (int cellY = 0; cellY < numRowsCols; cellY++)
1525     {
1526         for (int cellX = 0; cellX < numRowsCols; cellX++)
1527         {
1528             int seed            = cellX + cellY * numRowsCols;
1529             const Vector4 color = RandomVec4(seed, fmtValueMin, fmtValueMax);
1530 
1531             glBindFramebuffer(GL_FRAMEBUFFER, mFBOs[0]);
1532             glClearBufferfv(GL_COLOR, 0, color.data());
1533 
1534             glBindFramebuffer(GL_FRAMEBUFFER, 0);
1535 
1536             // Method 1: Set viewport and draw full-viewport quad
1537             glViewport(cellX * cellSize, cellY * cellSize, cellSize, cellSize);
1538             drawQuad(program, "position", 0.5f);
1539 
1540             // Uncommenting the glFinish call seems to make the test pass.
1541             // glFinish();
1542         }
1543     }
1544 
1545     std::vector<GLColor> pixelData(backFBOSize * backFBOSize);
1546     glReadPixels(0, 0, backFBOSize, backFBOSize, GL_RGBA, GL_UNSIGNED_BYTE, pixelData.data());
1547 
1548     for (int cellY = 0; cellY < numRowsCols; cellY++)
1549     {
1550         for (int cellX = 0; cellX < numRowsCols; cellX++)
1551         {
1552             int seed            = cellX + cellY * numRowsCols;
1553             const Vector4 color = RandomVec4(seed, fmtValueMin, fmtValueMax);
1554             GLColor expectedColor(color);
1555 
1556             int testN = cellX * cellSize + cellY * backFBOSize * cellSize + backFBOSize + 1;
1557             GLColor actualColor = pixelData[testN];
1558             EXPECT_NEAR(expectedColor.R, actualColor.R, 1);
1559             EXPECT_NEAR(expectedColor.G, actualColor.G, 1);
1560             EXPECT_NEAR(expectedColor.B, actualColor.B, 1);
1561             EXPECT_NEAR(expectedColor.A, actualColor.A, 1);
1562         }
1563     }
1564 
1565     ASSERT_GL_NO_ERROR();
1566 }
1567 
1568 // Test that clearing RGB8 attachments work when verified through sampling.
TEST_P(ClearTestES3,ClearRGB8)1569 TEST_P(ClearTestES3, ClearRGB8)
1570 {
1571     GLFramebuffer fb;
1572     glBindFramebuffer(GL_FRAMEBUFFER, fb);
1573 
1574     GLTexture tex;
1575     glBindTexture(GL_TEXTURE_2D, tex);
1576     glTexStorage2D(GL_TEXTURE_2D, 1, GL_RGB8, 1, 1);
1577     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1578     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1579 
1580     glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, tex, 0);
1581     ASSERT_GL_FRAMEBUFFER_COMPLETE(GL_FRAMEBUFFER);
1582 
1583     // Clear the texture through framebuffer.
1584     const GLubyte kClearColor[] = {63, 127, 191, 55};
1585     glClearColor(kClearColor[0] / 255.0f, kClearColor[1] / 255.0f, kClearColor[2] / 255.0f,
1586                  kClearColor[3] / 255.0f);
1587     glClear(GL_COLOR_BUFFER_BIT);
1588     ASSERT_GL_NO_ERROR();
1589 
1590     glBindFramebuffer(GL_FRAMEBUFFER, 0);
1591 
1592     // Sample from it and verify clear is done.
1593     ANGLE_GL_PROGRAM(program, essl3_shaders::vs::Texture2DLod(), essl3_shaders::fs::Texture2DLod());
1594     glUseProgram(program);
1595     GLint textureLocation = glGetUniformLocation(program, essl3_shaders::Texture2DUniform());
1596     ASSERT_NE(-1, textureLocation);
1597     GLint lodLocation = glGetUniformLocation(program, essl3_shaders::LodUniform());
1598     ASSERT_NE(-1, lodLocation);
1599 
1600     glUniform1i(textureLocation, 0);
1601     glUniform1f(lodLocation, 0);
1602 
1603     drawQuad(program, essl3_shaders::PositionAttrib(), 0.5f);
1604 
1605     EXPECT_PIXEL_NEAR(0, 0, kClearColor[0], kClearColor[1], kClearColor[2], 255, 1);
1606     ASSERT_GL_NO_ERROR();
1607 }
1608 
1609 // Test that clearing RGB8 attachments from a 2D texture array does not cause
1610 // VUID-VkImageMemoryBarrier-oldLayout-01197
TEST_P(ClearTestES3,TextureArrayRGB8)1611 TEST_P(ClearTestES3, TextureArrayRGB8)
1612 {
1613     GLFramebuffer fb;
1614     glBindFramebuffer(GL_FRAMEBUFFER, fb);
1615 
1616     GLTexture tex;
1617     glBindTexture(GL_TEXTURE_2D_ARRAY, tex);
1618     glTexStorage3D(GL_TEXTURE_2D_ARRAY, 1, GL_RGB8, 1, 1, 2);
1619     glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1620     glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1621 
1622     glFramebufferTextureLayer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, tex, 0, 0);
1623     glFramebufferTextureLayer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, tex, 0, 1);
1624 
1625     ASSERT_GL_FRAMEBUFFER_COMPLETE(GL_FRAMEBUFFER);
1626 
1627     GLenum bufs[2] = {GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1};
1628     glDrawBuffers(2, &bufs[0]);
1629 
1630     glClearColor(1.0, 0.0, 1.0, 1.0);
1631     glClear(GL_COLOR_BUFFER_BIT);
1632     ASSERT_GL_NO_ERROR();
1633 
1634     glBindFramebuffer(GL_READ_FRAMEBUFFER, fb);
1635 
1636     glReadBuffer(GL_COLOR_ATTACHMENT0);
1637     EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::magenta);
1638 
1639     glReadBuffer(GL_COLOR_ATTACHMENT1);
1640     EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::magenta);
1641 
1642     EXPECT_GL_NO_ERROR();
1643 }
1644 
maskedScissoredColorDepthStencilClear(const MaskedScissoredClearVariationsTestParams & params)1645 void MaskedScissoredClearTestBase::maskedScissoredColorDepthStencilClear(
1646     const MaskedScissoredClearVariationsTestParams &params)
1647 {
1648     // Flaky on Android Nexus 5x and Pixel 2, possible Qualcomm driver bug.
1649     // TODO(jmadill): Re-enable when possible. http://anglebug.com/2548
1650     ANGLE_SKIP_TEST_IF(IsOpenGLES() && IsAndroid());
1651 
1652     const int w      = getWindowWidth();
1653     const int h      = getWindowHeight();
1654     const int wthird = w / 3;
1655     const int hthird = h / 3;
1656 
1657     constexpr float kPreClearDepth     = 0.9f;
1658     constexpr float kClearDepth        = 0.5f;
1659     constexpr uint8_t kPreClearStencil = 0xFF;
1660     constexpr uint8_t kClearStencil    = 0x16;
1661     constexpr uint8_t kStencilMask     = 0x59;
1662     constexpr uint8_t kMaskedClearStencil =
1663         (kPreClearStencil & ~kStencilMask) | (kClearStencil & kStencilMask);
1664 
1665     bool clearColor, clearDepth, clearStencil;
1666     bool maskColor, maskDepth, maskStencil;
1667     bool scissor;
1668 
1669     ParseMaskedScissoredClearVariationsTestParams(params, &clearColor, &clearDepth, &clearStencil,
1670                                                   &maskColor, &maskDepth, &maskStencil, &scissor);
1671 
1672     // clearDepth && !maskDepth fails on Intel Ubuntu 19.04 Mesa 19.0.2 GL. http://anglebug.com/3614
1673     ANGLE_SKIP_TEST_IF(IsLinux() && IsIntel() && IsDesktopOpenGL() && clearDepth && !maskDepth);
1674 
1675     // Clear to a random color, 0.9 depth and 0x00 stencil
1676     Vector4 color1(0.1f, 0.2f, 0.3f, 0.4f);
1677     GLColor color1RGB(color1);
1678 
1679     glClearColor(color1[0], color1[1], color1[2], color1[3]);
1680     glClearDepthf(kPreClearDepth);
1681     glClearStencil(kPreClearStencil);
1682 
1683     if (!clearColor)
1684     {
1685         // If not asked to clear color, clear it anyway, but individually.  The clear value is
1686         // still used to verify that the depth/stencil clear happened correctly.  This allows
1687         // testing for depth/stencil-only clear implementations.
1688         glClear(GL_COLOR_BUFFER_BIT);
1689     }
1690 
1691     glClear((clearColor ? GL_COLOR_BUFFER_BIT : 0) | (clearDepth ? GL_DEPTH_BUFFER_BIT : 0) |
1692             (clearStencil ? GL_STENCIL_BUFFER_BIT : 0));
1693     ASSERT_GL_NO_ERROR();
1694 
1695     // Verify color was cleared correctly.
1696     EXPECT_PIXEL_COLOR_NEAR(0, 0, color1RGB, 1);
1697 
1698     if (scissor)
1699     {
1700         glEnable(GL_SCISSOR_TEST);
1701         glScissor(wthird / 2, hthird / 2, wthird, hthird);
1702     }
1703 
1704     // Use color and stencil masks to clear to a second color, 0.5 depth and 0x59 stencil.
1705     Vector4 color2(0.2f, 0.4f, 0.6f, 0.8f);
1706     GLColor color2RGB(color2);
1707     glClearColor(color2[0], color2[1], color2[2], color2[3]);
1708     glClearDepthf(kClearDepth);
1709     glClearStencil(kClearStencil);
1710     if (maskColor)
1711     {
1712         glColorMask(GL_TRUE, GL_FALSE, GL_TRUE, GL_FALSE);
1713     }
1714     if (maskDepth)
1715     {
1716         glDepthMask(GL_FALSE);
1717     }
1718     if (maskStencil)
1719     {
1720         glStencilMask(kStencilMask);
1721     }
1722     glClear((clearColor ? GL_COLOR_BUFFER_BIT : 0) | (clearDepth ? GL_DEPTH_BUFFER_BIT : 0) |
1723             (clearStencil ? GL_STENCIL_BUFFER_BIT : 0));
1724     glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
1725     glDepthMask(GL_TRUE);
1726     glStencilMask(0xFF);
1727     glDisable(GL_DEPTH_TEST);
1728     glDisable(GL_STENCIL_TEST);
1729     glDisable(GL_SCISSOR_TEST);
1730     ASSERT_GL_NO_ERROR();
1731 
1732     GLColor color2MaskedRGB(color2RGB[0], color1RGB[1], color2RGB[2], color1RGB[3]);
1733 
1734     // If not clearing color, the original color should be left both in the center and corners.  If
1735     // using a scissor, the corners should be left to the original color, while the center is
1736     // possibly changed.  If using a mask, the center (and corners if not scissored), changes to
1737     // the masked results.
1738     GLColor expectedCenterColorRGB =
1739         !clearColor ? color1RGB : maskColor ? color2MaskedRGB : color2RGB;
1740     GLColor expectedCornerColorRGB = scissor ? color1RGB : expectedCenterColorRGB;
1741 
1742     // Verify second clear color mask worked as expected.
1743     EXPECT_PIXEL_COLOR_NEAR(wthird, hthird, expectedCenterColorRGB, 1);
1744 
1745     EXPECT_PIXEL_COLOR_NEAR(0, 0, expectedCornerColorRGB, 1);
1746     EXPECT_PIXEL_COLOR_NEAR(w - 1, 0, expectedCornerColorRGB, 1);
1747     EXPECT_PIXEL_COLOR_NEAR(0, h - 1, expectedCornerColorRGB, 1);
1748     EXPECT_PIXEL_COLOR_NEAR(w - 1, h - 1, expectedCornerColorRGB, 1);
1749     EXPECT_PIXEL_COLOR_NEAR(wthird, 2 * hthird, expectedCornerColorRGB, 1);
1750     EXPECT_PIXEL_COLOR_NEAR(2 * wthird, hthird, expectedCornerColorRGB, 1);
1751     EXPECT_PIXEL_COLOR_NEAR(2 * wthird, 2 * hthird, expectedCornerColorRGB, 1);
1752 
1753     // If there is depth, but depth is not asked to be cleared, the depth buffer contains garbage,
1754     // so no particular behavior can be expected.
1755     if (clearDepth || !mHasDepth)
1756     {
1757         // We use a small shader to verify depth.
1758         ANGLE_GL_PROGRAM(depthTestProgram, essl1_shaders::vs::Passthrough(),
1759                          essl1_shaders::fs::Blue());
1760         glEnable(GL_DEPTH_TEST);
1761         glDepthFunc(maskDepth ? GL_GREATER : GL_EQUAL);
1762         // - If depth is cleared, but it's masked, kPreClearDepth should be in the depth buffer.
1763         // - If depth is cleared, but it's not masked, kClearDepth should be in the depth buffer.
1764         // - If depth is not cleared, the if above ensures there is no depth buffer at all,
1765         //   which means depth test will always pass.
1766         drawQuad(depthTestProgram, essl1_shaders::PositionAttrib(), maskDepth ? 1.0f : 0.0f);
1767         glDisable(GL_DEPTH_TEST);
1768         ASSERT_GL_NO_ERROR();
1769 
1770         // Either way, we expect blue to be written to the center.
1771         expectedCenterColorRGB = GLColor::blue;
1772         // If there is no depth, depth test always passes so the whole image must be blue.  Same if
1773         // depth write is masked.
1774         expectedCornerColorRGB =
1775             mHasDepth && scissor && !maskDepth ? expectedCornerColorRGB : GLColor::blue;
1776 
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 
1788     // If there is stencil, but it's not asked to be cleared, there is similarly no expectation.
1789     if (clearStencil || !mHasStencil)
1790     {
1791         // And another small shader to verify stencil.
1792         ANGLE_GL_PROGRAM(stencilTestProgram, essl1_shaders::vs::Passthrough(),
1793                          essl1_shaders::fs::Green());
1794         glEnable(GL_STENCIL_TEST);
1795         // - If stencil is cleared, but it's masked, kMaskedClearStencil should be in the stencil
1796         //   buffer.
1797         // - If stencil is cleared, but it's not masked, kClearStencil should be in the stencil
1798         //   buffer.
1799         // - If stencil is not cleared, the if above ensures there is no stencil buffer at all,
1800         //   which means stencil test will always pass.
1801         glStencilFunc(GL_EQUAL, maskStencil ? kMaskedClearStencil : kClearStencil, 0xFF);
1802         drawQuad(stencilTestProgram, essl1_shaders::PositionAttrib(), 0.0f);
1803         glDisable(GL_STENCIL_TEST);
1804         ASSERT_GL_NO_ERROR();
1805 
1806         // Either way, we expect green to be written to the center.
1807         expectedCenterColorRGB = GLColor::green;
1808         // If there is no stencil, stencil test always passes so the whole image must be green.
1809         expectedCornerColorRGB = mHasStencil && scissor ? expectedCornerColorRGB : GLColor::green;
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 
1823 // Tests combinations of color, depth, stencil clears with or without masks or scissor.
TEST_P(MaskedScissoredClearTest,Test)1824 TEST_P(MaskedScissoredClearTest, Test)
1825 {
1826     maskedScissoredColorDepthStencilClear(GetParam());
1827 }
1828 
1829 // Tests combinations of color, depth, stencil clears with or without masks or scissor.
1830 //
1831 // This uses depth/stencil attachments that are single-channel, but are emulated with a format
1832 // that has both channels.
TEST_P(VulkanClearTest,Test)1833 TEST_P(VulkanClearTest, Test)
1834 {
1835     bool clearColor, clearDepth, clearStencil;
1836     bool maskColor, maskDepth, maskStencil;
1837     bool scissor;
1838 
1839     ParseMaskedScissoredClearVariationsTestParams(GetParam(), &clearColor, &clearDepth,
1840                                                   &clearStencil, &maskColor, &maskDepth,
1841                                                   &maskStencil, &scissor);
1842 
1843     // We only care about clearing depth xor stencil.
1844     if (clearDepth == clearStencil)
1845     {
1846         return;
1847     }
1848 
1849     if (clearDepth)
1850     {
1851         // Creating a depth-only renderbuffer is an ES3 feature.
1852         ANGLE_SKIP_TEST_IF(getClientMajorVersion() < 3);
1853         bindColorDepthFBO();
1854     }
1855     else
1856     {
1857         bindColorStencilFBO();
1858     }
1859 
1860     maskedScissoredColorDepthStencilClear(GetParam());
1861 }
1862 
1863 // Tests that clearing a non existing attachment works.
TEST_P(ClearTest,ClearColorThenClearNonExistingDepthStencil)1864 TEST_P(ClearTest, ClearColorThenClearNonExistingDepthStencil)
1865 {
1866     constexpr GLsizei kSize = 16;
1867 
1868     GLTexture color;
1869     glBindTexture(GL_TEXTURE_2D, color);
1870     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, kSize, kSize, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
1871 
1872     GLFramebuffer fbo;
1873     glBindFramebuffer(GL_FRAMEBUFFER, fbo);
1874     glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, color, 0);
1875     ASSERT_GL_NO_ERROR();
1876     ASSERT_GL_FRAMEBUFFER_COMPLETE(GL_FRAMEBUFFER);
1877 
1878     // Clear color.
1879     glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
1880     glClear(GL_COLOR_BUFFER_BIT);
1881 
1882     // Clear depth/stencil.
1883     glClear(GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
1884     ASSERT_GL_NO_ERROR();
1885 
1886     // Read back color.
1887     EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::red);
1888 }
1889 
1890 // Tests that clearing a non existing attachment works.
TEST_P(ClearTestES3,ClearDepthStencilThenClearNonExistingColor)1891 TEST_P(ClearTestES3, ClearDepthStencilThenClearNonExistingColor)
1892 {
1893     constexpr GLsizei kSize = 16;
1894 
1895     GLRenderbuffer depth;
1896     glBindRenderbuffer(GL_RENDERBUFFER, depth);
1897     glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, kSize, kSize);
1898 
1899     GLFramebuffer fbo;
1900     glBindFramebuffer(GL_FRAMEBUFFER, fbo);
1901     glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, depth);
1902     ASSERT_GL_NO_ERROR();
1903     ASSERT_GL_FRAMEBUFFER_COMPLETE(GL_FRAMEBUFFER);
1904 
1905     // Clear depth/stencil.
1906     glClearDepthf(1.0f);
1907     glClearStencil(0xAA);
1908     glClear(GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
1909     ASSERT_GL_NO_ERROR();
1910 
1911     // Clear color.
1912     glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
1913     glClear(GL_COLOR_BUFFER_BIT);
1914     ASSERT_GL_NO_ERROR();
1915 }
1916 
1917 // Test that just clearing a nonexistent drawbuffer of the default framebuffer doesn't cause an
1918 // assert.
TEST_P(ClearTestES3,ClearBuffer1OnDefaultFramebufferNoAssert)1919 TEST_P(ClearTestES3, ClearBuffer1OnDefaultFramebufferNoAssert)
1920 {
1921     std::vector<GLuint> testUint(4);
1922     glClearBufferuiv(GL_COLOR, 1, testUint.data());
1923     std::vector<GLint> testInt(4);
1924     glClearBufferiv(GL_COLOR, 1, testInt.data());
1925     std::vector<GLfloat> testFloat(4);
1926     glClearBufferfv(GL_COLOR, 1, testFloat.data());
1927     EXPECT_GL_NO_ERROR();
1928 }
1929 
1930 // Clears many small concentric rectangles using scissor regions.
TEST_P(ClearTest,InceptionScissorClears)1931 TEST_P(ClearTest, InceptionScissorClears)
1932 {
1933     angle::RNG rng;
1934 
1935     constexpr GLuint kSize = 16;
1936 
1937     // Create a square user FBO so we have more control over the dimensions.
1938     GLFramebuffer fbo;
1939     glBindFramebuffer(GL_FRAMEBUFFER, fbo);
1940 
1941     GLRenderbuffer rbo;
1942     glBindRenderbuffer(GL_RENDERBUFFER, rbo);
1943     glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA8, kSize, kSize);
1944 
1945     glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, rbo);
1946     ASSERT_GL_FRAMEBUFFER_COMPLETE(GL_FRAMEBUFFER);
1947 
1948     glViewport(0, 0, kSize, kSize);
1949 
1950     // Draw small concentric squares using scissor.
1951     std::vector<GLColor> expectedColors;
1952     for (GLuint index = 0; index < (kSize - 1) / 2; index++)
1953     {
1954         // Do the first clear without the scissor.
1955         if (index > 0)
1956         {
1957             glEnable(GL_SCISSOR_TEST);
1958             glScissor(index, index, kSize - (index * 2), kSize - (index * 2));
1959         }
1960 
1961         GLColor color = RandomColor(&rng);
1962         expectedColors.push_back(color);
1963         Vector4 floatColor = color.toNormalizedVector();
1964         glClearColor(floatColor[0], floatColor[1], floatColor[2], floatColor[3]);
1965         glClear(GL_COLOR_BUFFER_BIT);
1966     }
1967 
1968     ASSERT_GL_NO_ERROR();
1969 
1970     std::vector<GLColor> actualColors(expectedColors.size());
1971     glReadPixels(0, kSize / 2, actualColors.size(), 1, GL_RGBA, GL_UNSIGNED_BYTE,
1972                  actualColors.data());
1973 
1974     EXPECT_EQ(expectedColors, actualColors);
1975 }
1976 
1977 // Test that clearBuffer with disabled non-zero drawbuffer or disabled read source doesn't cause an
1978 // assert.
TEST_P(ClearTestES3,ClearDisabledNonZeroAttachmentNoAssert)1979 TEST_P(ClearTestES3, ClearDisabledNonZeroAttachmentNoAssert)
1980 {
1981     // http://anglebug.com/4612
1982     ANGLE_SKIP_TEST_IF(IsOSX() && IsDesktopOpenGL());
1983 
1984     GLFramebuffer fb;
1985     glBindFramebuffer(GL_FRAMEBUFFER, fb);
1986 
1987     GLRenderbuffer rb;
1988     glBindRenderbuffer(GL_RENDERBUFFER, rb);
1989     glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA8, 16, 16);
1990 
1991     glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_RENDERBUFFER, rb);
1992     glDrawBuffers(0, nullptr);
1993     glReadBuffer(GL_NONE);
1994 
1995     ASSERT_GL_FRAMEBUFFER_COMPLETE(GL_FRAMEBUFFER);
1996 
1997     float clearColorf[4] = {0.5, 0.5, 0.5, 0.5};
1998     glClearBufferfv(GL_COLOR, 1, clearColorf);
1999 
2000     GLuint clearColorui[4] = {255, 255, 255, 255};
2001     glClearBufferuiv(GL_COLOR, 1, clearColorui);
2002 
2003     GLint clearColori[4] = {-127, -127, -127, -127};
2004     glClearBufferiv(GL_COLOR, 1, clearColori);
2005 
2006     EXPECT_GL_NO_ERROR();
2007 }
2008 
2009 // Test that having a framebuffer with maximum number of attachments and clearing color, depth and
2010 // stencil works.
TEST_P(ClearTestES3,ClearMaxAttachments)2011 TEST_P(ClearTestES3, ClearMaxAttachments)
2012 {
2013     // http://anglebug.com/4612
2014     ANGLE_SKIP_TEST_IF(IsOSX() && IsDesktopOpenGL());
2015     // http://anglebug.com/5397
2016     ANGLE_SKIP_TEST_IF(IsAMD() && IsD3D11());
2017 
2018     constexpr GLsizei kSize = 16;
2019 
2020     GLint maxDrawBuffers = 0;
2021     glGetIntegerv(GL_MAX_DRAW_BUFFERS, &maxDrawBuffers);
2022     ASSERT_GE(maxDrawBuffers, 4);
2023 
2024     // Setup framebuffer.
2025     GLFramebuffer fb;
2026     glBindFramebuffer(GL_FRAMEBUFFER, fb);
2027 
2028     std::vector<GLRenderbuffer> color(maxDrawBuffers);
2029     std::vector<GLenum> drawBuffers(maxDrawBuffers);
2030 
2031     for (GLint colorIndex = 0; colorIndex < maxDrawBuffers; ++colorIndex)
2032     {
2033         glBindRenderbuffer(GL_RENDERBUFFER, color[colorIndex]);
2034         glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA8, kSize, kSize);
2035         glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + colorIndex,
2036                                   GL_RENDERBUFFER, color[colorIndex]);
2037 
2038         drawBuffers[colorIndex] = GL_COLOR_ATTACHMENT0 + colorIndex;
2039     }
2040 
2041     GLRenderbuffer depthStencil;
2042     glBindRenderbuffer(GL_RENDERBUFFER, depthStencil);
2043     glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, kSize, kSize);
2044     glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER,
2045                               depthStencil);
2046 
2047     EXPECT_GL_NO_ERROR();
2048     ASSERT_GL_FRAMEBUFFER_COMPLETE(GL_FRAMEBUFFER);
2049 
2050     glDrawBuffers(maxDrawBuffers, drawBuffers.data());
2051 
2052     glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
2053     glClearDepthf(1.0f);
2054     glClearStencil(0x55);
2055     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
2056     EXPECT_GL_NO_ERROR();
2057 
2058     // Verify that every color attachment is cleared correctly.
2059     for (GLint colorIndex = 0; colorIndex < maxDrawBuffers; ++colorIndex)
2060     {
2061         glReadBuffer(GL_COLOR_ATTACHMENT0 + colorIndex);
2062 
2063         EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::red);
2064         EXPECT_PIXEL_COLOR_EQ(0, kSize - 1, GLColor::red);
2065         EXPECT_PIXEL_COLOR_EQ(kSize - 1, 0, GLColor::red);
2066         EXPECT_PIXEL_COLOR_EQ(kSize - 1, kSize - 1, GLColor::red);
2067     }
2068 
2069     // Verify that depth and stencil attachments are cleared correctly.
2070     GLFramebuffer fbVerify;
2071     glBindFramebuffer(GL_FRAMEBUFFER, fbVerify);
2072 
2073     glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, color[0]);
2074     glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER,
2075                               depthStencil);
2076 
2077     // If depth is not cleared to 1, rendering would fail.
2078     glEnable(GL_DEPTH_TEST);
2079     glDepthFunc(GL_LESS);
2080 
2081     // If stencil is not cleared to 0x55, rendering would fail.
2082     glEnable(GL_STENCIL_TEST);
2083     glStencilFunc(GL_EQUAL, 0x55, 0xFF);
2084     glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
2085     glStencilMask(0xFF);
2086 
2087     // Draw green.
2088     ANGLE_GL_PROGRAM(drawGreen, essl1_shaders::vs::Simple(), essl1_shaders::fs::Green());
2089     drawQuad(drawGreen, essl1_shaders::PositionAttrib(), 0.95f);
2090 
2091     // Verify that green was drawn.
2092     EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::green);
2093     EXPECT_PIXEL_COLOR_EQ(0, kSize - 1, GLColor::green);
2094     EXPECT_PIXEL_COLOR_EQ(kSize - 1, 0, GLColor::green);
2095     EXPECT_PIXEL_COLOR_EQ(kSize - 1, kSize - 1, GLColor::green);
2096 }
2097 
2098 // Test that having a framebuffer with maximum number of attachments and clearing color, depth and
2099 // stencil after a draw call works.
TEST_P(ClearTestES3,ClearMaxAttachmentsAfterDraw)2100 TEST_P(ClearTestES3, ClearMaxAttachmentsAfterDraw)
2101 {
2102     // http://anglebug.com/4612
2103     ANGLE_SKIP_TEST_IF(IsOSX() && IsDesktopOpenGL());
2104 
2105     constexpr GLsizei kSize = 16;
2106 
2107     GLint maxDrawBuffers = 0;
2108     glGetIntegerv(GL_MAX_DRAW_BUFFERS, &maxDrawBuffers);
2109     ASSERT_GE(maxDrawBuffers, 4);
2110 
2111     // Setup framebuffer.
2112     GLFramebuffer fb;
2113     glBindFramebuffer(GL_FRAMEBUFFER, fb);
2114 
2115     std::vector<GLRenderbuffer> color(maxDrawBuffers);
2116     std::vector<GLenum> drawBuffers(maxDrawBuffers);
2117 
2118     for (GLint colorIndex = 0; colorIndex < maxDrawBuffers; ++colorIndex)
2119     {
2120         glBindRenderbuffer(GL_RENDERBUFFER, color[colorIndex]);
2121         glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA8, kSize, kSize);
2122         glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + colorIndex,
2123                                   GL_RENDERBUFFER, color[colorIndex]);
2124 
2125         drawBuffers[colorIndex] = GL_COLOR_ATTACHMENT0 + colorIndex;
2126     }
2127 
2128     GLRenderbuffer depthStencil;
2129     glBindRenderbuffer(GL_RENDERBUFFER, depthStencil);
2130     glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, kSize, kSize);
2131     glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER,
2132                               depthStencil);
2133 
2134     EXPECT_GL_NO_ERROR();
2135     ASSERT_GL_FRAMEBUFFER_COMPLETE(GL_FRAMEBUFFER);
2136 
2137     glDrawBuffers(maxDrawBuffers, drawBuffers.data());
2138 
2139     // Issue a draw call to render blue, depth=0 and stencil 0x3C to the attachments.
2140     glEnable(GL_DEPTH_TEST);
2141     glDepthFunc(GL_ALWAYS);
2142 
2143     glEnable(GL_STENCIL_TEST);
2144     glStencilFunc(GL_ALWAYS, 0x3C, 0xFF);
2145     glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE);
2146     glStencilMask(0xFF);
2147 
2148     // Generate shader for this framebuffer.
2149     std::stringstream strstr;
2150     strstr << "#version 300 es\n"
2151               "precision highp float;\n";
2152     for (GLint colorIndex = 0; colorIndex < maxDrawBuffers; ++colorIndex)
2153     {
2154         strstr << "layout(location = " << colorIndex << ") out vec4 value" << colorIndex << ";\n";
2155     }
2156     strstr << "void main()\n"
2157               "{\n";
2158     for (GLint colorIndex = 0; colorIndex < maxDrawBuffers; ++colorIndex)
2159     {
2160         strstr << "value" << colorIndex << " = vec4(0.0f, 0.0f, 1.0f, 1.0f);\n";
2161     }
2162     strstr << "}\n";
2163 
2164     ANGLE_GL_PROGRAM(drawMRT, essl3_shaders::vs::Simple(), strstr.str().c_str());
2165     drawQuad(drawMRT, essl3_shaders::PositionAttrib(), 0.0f);
2166     ASSERT_GL_NO_ERROR();
2167 
2168     glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
2169     glClearDepthf(1.0f);
2170     glClearStencil(0x55);
2171     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
2172     EXPECT_GL_NO_ERROR();
2173 
2174     // Verify that every color attachment is cleared correctly.
2175     for (GLint colorIndex = 0; colorIndex < maxDrawBuffers; ++colorIndex)
2176     {
2177         glReadBuffer(GL_COLOR_ATTACHMENT0 + colorIndex);
2178 
2179         EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::red);
2180         EXPECT_PIXEL_COLOR_EQ(0, kSize - 1, GLColor::red);
2181         EXPECT_PIXEL_COLOR_EQ(kSize - 1, 0, GLColor::red);
2182         EXPECT_PIXEL_COLOR_EQ(kSize - 1, kSize - 1, GLColor::red);
2183     }
2184 
2185     // Verify that depth and stencil attachments are cleared correctly.
2186     GLFramebuffer fbVerify;
2187     glBindFramebuffer(GL_FRAMEBUFFER, fbVerify);
2188 
2189     glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, color[0]);
2190     glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER,
2191                               depthStencil);
2192 
2193     // If depth is not cleared to 1, rendering would fail.
2194     glDepthFunc(GL_LESS);
2195 
2196     // If stencil is not cleared to 0x55, rendering would fail.
2197     glStencilFunc(GL_EQUAL, 0x55, 0xFF);
2198     glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
2199 
2200     // Draw green.
2201     ANGLE_GL_PROGRAM(drawGreen, essl1_shaders::vs::Simple(), essl1_shaders::fs::Green());
2202     drawQuad(drawGreen, essl1_shaders::PositionAttrib(), 0.95f);
2203 
2204     // Verify that green was drawn.
2205     EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::green);
2206     EXPECT_PIXEL_COLOR_EQ(0, kSize - 1, GLColor::green);
2207     EXPECT_PIXEL_COLOR_EQ(kSize - 1, 0, GLColor::green);
2208     EXPECT_PIXEL_COLOR_EQ(kSize - 1, kSize - 1, GLColor::green);
2209 }
2210 
2211 // Test that mixed masked clear works after clear.
TEST_P(ClearTestES3,ClearThenMixedMaskedClear)2212 TEST_P(ClearTestES3, ClearThenMixedMaskedClear)
2213 {
2214     constexpr GLsizei kSize = 16;
2215 
2216     // Setup framebuffer.
2217     GLRenderbuffer color;
2218     glBindRenderbuffer(GL_RENDERBUFFER, color);
2219     glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA8, kSize, kSize);
2220 
2221     GLRenderbuffer depthStencil;
2222     glBindRenderbuffer(GL_RENDERBUFFER, depthStencil);
2223     glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, kSize, kSize);
2224 
2225     GLFramebuffer fb;
2226     glBindFramebuffer(GL_FRAMEBUFFER, fb);
2227     glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, color);
2228     glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER,
2229                               depthStencil);
2230     EXPECT_GL_NO_ERROR();
2231     ASSERT_GL_FRAMEBUFFER_COMPLETE(GL_FRAMEBUFFER);
2232 
2233     // Clear color and depth/stencil
2234     glClearColor(0.1f, 1.0f, 0.0f, 0.7f);
2235     glClearDepthf(0.0f);
2236     glClearStencil(0x55);
2237     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
2238 
2239     // Clear again, but with color and stencil masked
2240     glClearColor(1.0f, 0.2f, 0.6f, 1.0f);
2241     glClearDepthf(1.0f);
2242     glClearStencil(0x3C);
2243     glColorMask(GL_TRUE, GL_FALSE, GL_FALSE, GL_TRUE);
2244     glStencilMask(0xF0);
2245     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
2246 
2247     // Issue a draw call to verify color, depth and stencil.
2248 
2249     // If depth is not cleared to 1, rendering would fail.
2250     glEnable(GL_DEPTH_TEST);
2251     glDepthFunc(GL_LESS);
2252 
2253     // If stencil is not cleared to 0x35, rendering would fail.
2254     glEnable(GL_STENCIL_TEST);
2255     glStencilFunc(GL_EQUAL, 0x35, 0xFF);
2256     glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
2257     glStencilMask(0xFF);
2258     glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
2259 
2260     ANGLE_GL_PROGRAM(drawColor, essl1_shaders::vs::Simple(), essl1_shaders::fs::UniformColor());
2261     glUseProgram(drawColor);
2262     GLint colorUniformLocation =
2263         glGetUniformLocation(drawColor, angle::essl1_shaders::ColorUniform());
2264     ASSERT_NE(colorUniformLocation, -1);
2265 
2266     // Blend half-transparent blue into the color buffer.
2267     glUniform4f(colorUniformLocation, 0.0f, 0.0f, 1.0f, 0.5f);
2268     glEnable(GL_BLEND);
2269     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
2270     drawQuad(drawColor, essl1_shaders::PositionAttrib(), 0.95f);
2271     ASSERT_GL_NO_ERROR();
2272 
2273     // Verify that the color buffer is now gray
2274     const GLColor kExpected(127, 127, 127, 191);
2275     EXPECT_PIXEL_COLOR_NEAR(0, 0, kExpected, 1);
2276     EXPECT_PIXEL_COLOR_NEAR(0, kSize - 1, kExpected, 1);
2277     EXPECT_PIXEL_COLOR_NEAR(kSize - 1, 0, kExpected, 1);
2278     EXPECT_PIXEL_COLOR_NEAR(kSize - 1, kSize - 1, kExpected, 1);
2279 }
2280 
2281 // Test that draw without state change after masked clear works
TEST_P(ClearTestES3,DrawClearThenDrawWithoutStateChange)2282 TEST_P(ClearTestES3, DrawClearThenDrawWithoutStateChange)
2283 {
2284     swapBuffers();
2285     constexpr GLsizei kSize = 16;
2286 
2287     // Setup framebuffer.
2288     GLRenderbuffer color;
2289     glBindRenderbuffer(GL_RENDERBUFFER, color);
2290     glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA8, kSize, kSize);
2291 
2292     GLFramebuffer fb;
2293     glBindFramebuffer(GL_FRAMEBUFFER, fb);
2294     glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, color);
2295     EXPECT_GL_NO_ERROR();
2296     ASSERT_GL_FRAMEBUFFER_COMPLETE(GL_FRAMEBUFFER);
2297 
2298     // Clear color initially.
2299     glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
2300     glClear(GL_COLOR_BUFFER_BIT);
2301 
2302     // Mask color.
2303     glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_FALSE);
2304 
2305     ANGLE_GL_PROGRAM(drawColor, essl1_shaders::vs::Simple(), essl1_shaders::fs::UniformColor());
2306     glUseProgram(drawColor);
2307     GLint colorUniformLocation =
2308         glGetUniformLocation(drawColor, angle::essl1_shaders::ColorUniform());
2309     ASSERT_NE(colorUniformLocation, -1);
2310 
2311     // Initialize position attribute.
2312     GLint posLoc = glGetAttribLocation(drawColor, essl1_shaders::PositionAttrib());
2313     ASSERT_NE(-1, posLoc);
2314     setupQuadVertexBuffer(0.5f, 1.0f);
2315     glVertexAttribPointer(posLoc, 3, GL_FLOAT, GL_FALSE, 0, nullptr);
2316     glEnableVertexAttribArray(posLoc);
2317 
2318     // Draw red.
2319     glViewport(0, 0, kSize, kSize);
2320     glClearColor(0.0f, 1.0f, 0.0f, 0.0f);
2321     glUniform4f(colorUniformLocation, 1.0f, 0.0f, 0.0f, 0.5f);
2322     glDrawArrays(GL_TRIANGLES, 0, 6);
2323 
2324     // Clear to green without any state change.
2325     glClear(GL_COLOR_BUFFER_BIT);
2326 
2327     // Draw red again without any state change.
2328     glDrawArrays(GL_TRIANGLES, 0, 6);
2329 
2330     // Verify that the color buffer is now red
2331     EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::red);
2332     EXPECT_PIXEL_COLOR_EQ(0, kSize - 1, GLColor::red);
2333     EXPECT_PIXEL_COLOR_EQ(kSize - 1, 0, GLColor::red);
2334     EXPECT_PIXEL_COLOR_EQ(kSize - 1, kSize - 1, GLColor::red);
2335 }
2336 
2337 // Test that clear stencil value is correctly masked to 8 bits.
TEST_P(ClearTest,ClearStencilMask)2338 TEST_P(ClearTest, ClearStencilMask)
2339 {
2340     GLint stencilBits = 0;
2341     glGetIntegerv(GL_STENCIL_BITS, &stencilBits);
2342     EXPECT_EQ(stencilBits, 8);
2343 
2344     ANGLE_GL_PROGRAM(drawColor, essl1_shaders::vs::Simple(), essl1_shaders::fs::Green());
2345     glUseProgram(drawColor);
2346 
2347     glClearColor(0, 0, 0, 1);
2348     glClear(GL_COLOR_BUFFER_BIT);
2349     EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::black);
2350 
2351     // Clear stencil value must be masked to 0x42
2352     glClearStencil(0x142);
2353     glClear(GL_STENCIL_BUFFER_BIT);
2354 
2355     // Check that the stencil test works as expected
2356     glEnable(GL_STENCIL_TEST);
2357 
2358     // Negative case
2359     glStencilFunc(GL_NOTEQUAL, 0x42, 0xFF);
2360     drawQuad(drawColor, essl1_shaders::PositionAttrib(), 0.5f);
2361     EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::black);
2362 
2363     // Positive case
2364     glStencilFunc(GL_EQUAL, 0x42, 0xFF);
2365     drawQuad(drawColor, essl1_shaders::PositionAttrib(), 0.5f);
2366     EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::green);
2367 
2368     ASSERT_GL_NO_ERROR();
2369 }
2370 
2371 // Test that glClearBufferiv correctly masks the clear stencil value.
TEST_P(ClearTestES3,ClearBufferivStencilMask)2372 TEST_P(ClearTestES3, ClearBufferivStencilMask)
2373 {
2374     GLint stencilBits = 0;
2375     glGetIntegerv(GL_STENCIL_BITS, &stencilBits);
2376     EXPECT_EQ(stencilBits, 8);
2377 
2378     ANGLE_GL_PROGRAM(drawColor, essl1_shaders::vs::Simple(), essl1_shaders::fs::Green());
2379     glUseProgram(drawColor);
2380 
2381     glClearColor(0, 0, 0, 1);
2382     glClear(GL_COLOR_BUFFER_BIT);
2383     EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::black);
2384 
2385     // Clear stencil value must be masked to 0x42
2386     const GLint kStencilClearValue = 0x142;
2387     glClearBufferiv(GL_STENCIL, 0, &kStencilClearValue);
2388 
2389     // Check that the stencil test works as expected
2390     glEnable(GL_STENCIL_TEST);
2391 
2392     // Negative case
2393     glStencilFunc(GL_NOTEQUAL, 0x42, 0xFF);
2394     drawQuad(drawColor, essl1_shaders::PositionAttrib(), 0.5f);
2395     EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::black);
2396 
2397     // Positive case
2398     glStencilFunc(GL_EQUAL, 0x42, 0xFF);
2399     drawQuad(drawColor, essl1_shaders::PositionAttrib(), 0.5f);
2400     EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::green);
2401 
2402     ASSERT_GL_NO_ERROR();
2403 }
2404 
2405 // Test that glClearBufferfi correctly masks the clear stencil value.
TEST_P(ClearTestES3,ClearBufferfiStencilMask)2406 TEST_P(ClearTestES3, ClearBufferfiStencilMask)
2407 {
2408     GLint stencilBits = 0;
2409     glGetIntegerv(GL_STENCIL_BITS, &stencilBits);
2410     EXPECT_EQ(stencilBits, 8);
2411 
2412     ANGLE_GL_PROGRAM(drawColor, essl1_shaders::vs::Simple(), essl1_shaders::fs::Green());
2413     glUseProgram(drawColor);
2414 
2415     glClearColor(0, 0, 0, 1);
2416     glClear(GL_COLOR_BUFFER_BIT);
2417     EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::black);
2418 
2419     // Clear stencil value must be masked to 0x42
2420     glClearBufferfi(GL_DEPTH_STENCIL, 0, 0.5f, 0x142);
2421 
2422     // Check that the stencil test works as expected
2423     glEnable(GL_STENCIL_TEST);
2424 
2425     // Negative case
2426     glStencilFunc(GL_NOTEQUAL, 0x42, 0xFF);
2427     drawQuad(drawColor, essl1_shaders::PositionAttrib(), 0.5f);
2428     EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::black);
2429 
2430     // Positive case
2431     glStencilFunc(GL_EQUAL, 0x42, 0xFF);
2432     drawQuad(drawColor, essl1_shaders::PositionAttrib(), 0.5f);
2433     EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::green);
2434 
2435     ASSERT_GL_NO_ERROR();
2436 }
2437 
2438 // Test that glClearBufferfi works when stencil attachment is not present.
TEST_P(ClearTestES3,ClearBufferfiNoStencilAttachment)2439 TEST_P(ClearTestES3, ClearBufferfiNoStencilAttachment)
2440 {
2441     constexpr GLsizei kSize = 16;
2442 
2443     GLRenderbuffer color;
2444     glBindRenderbuffer(GL_RENDERBUFFER, color);
2445     glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA8, kSize, kSize);
2446 
2447     GLRenderbuffer depth;
2448     glBindRenderbuffer(GL_RENDERBUFFER, depth);
2449     glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, kSize, kSize);
2450 
2451     GLFramebuffer fbo;
2452     glBindFramebuffer(GL_FRAMEBUFFER, fbo);
2453     glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, color);
2454     glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depth);
2455     EXPECT_GL_FRAMEBUFFER_COMPLETE(GL_FRAMEBUFFER);
2456     EXPECT_GL_NO_ERROR();
2457 
2458     // Clear depth/stencil with glClearBufferfi.  Note that the stencil attachment doesn't exist.
2459     glClearBufferfi(GL_DEPTH_STENCIL, 0, 0.5f, 0x55);
2460     EXPECT_GL_NO_ERROR();
2461 
2462     // Verify depth is cleared correctly.
2463     verifyDepth(0.5f, kSize);
2464 }
2465 
2466 // Test that scissored clear followed by non-scissored draw works.  Ensures that when scissor size
2467 // is expanded, the clear operation remains limited to the scissor region.  Written to catch
2468 // potential future bugs if loadOp=CLEAR is used in the Vulkan backend for a small render pass and
2469 // then the render area is mistakenly enlarged.
TEST_P(ClearTest,ScissoredClearThenNonScissoredDraw)2470 TEST_P(ClearTest, ScissoredClearThenNonScissoredDraw)
2471 {
2472     constexpr GLsizei kSize = 16;
2473     const std::vector<GLColor> kInitialData(kSize * kSize, GLColor::red);
2474 
2475     // Setup framebuffer.  Initialize color with red.
2476     GLTexture color;
2477     glBindTexture(GL_TEXTURE_2D, color);
2478     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, kSize, kSize, 0, GL_RGBA, GL_UNSIGNED_BYTE,
2479                  kInitialData.data());
2480 
2481     GLFramebuffer fb;
2482     glBindFramebuffer(GL_FRAMEBUFFER, fb);
2483     glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, color, 0);
2484     EXPECT_GL_NO_ERROR();
2485     ASSERT_GL_FRAMEBUFFER_COMPLETE(GL_FRAMEBUFFER);
2486 
2487     // Issue a scissored clear to green.
2488     glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
2489     glScissor(kSize / 2, 0, kSize / 2, kSize);
2490     glEnable(GL_SCISSOR_TEST);
2491     glClear(GL_COLOR_BUFFER_BIT);
2492 
2493     // Expand the scissor and blend blue into the framebuffer.
2494     glScissor(0, 0, kSize, kSize);
2495     glEnable(GL_BLEND);
2496     glBlendFunc(GL_ONE, GL_ONE);
2497 
2498     ANGLE_GL_PROGRAM(drawBlue, essl1_shaders::vs::Simple(), essl1_shaders::fs::Blue());
2499     drawQuad(drawBlue, essl1_shaders::PositionAttrib(), 0.5f);
2500     ASSERT_GL_NO_ERROR();
2501 
2502     // Verify that the left half is magenta, and the right half is cyan.
2503     EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::magenta);
2504     EXPECT_PIXEL_COLOR_EQ(kSize / 2 - 1, 0, GLColor::magenta);
2505     EXPECT_PIXEL_COLOR_EQ(0, kSize - 1, GLColor::magenta);
2506     EXPECT_PIXEL_COLOR_EQ(kSize / 2 - 1, kSize - 1, GLColor::magenta);
2507 
2508     EXPECT_PIXEL_COLOR_EQ(kSize / 2, 0, GLColor::cyan);
2509     EXPECT_PIXEL_COLOR_EQ(kSize / 2, kSize - 1, GLColor::cyan);
2510     EXPECT_PIXEL_COLOR_EQ(kSize - 1, 0, GLColor::cyan);
2511     EXPECT_PIXEL_COLOR_EQ(kSize - 1, kSize - 1, GLColor::cyan);
2512 }
2513 
2514 // Test that clear followed by a scissored masked clear works.
TEST_P(ClearTest,ClearThenScissoredMaskedClear)2515 TEST_P(ClearTest, ClearThenScissoredMaskedClear)
2516 {
2517     constexpr GLsizei kSize = 16;
2518 
2519     // Setup framebuffer
2520     GLTexture color;
2521     glBindTexture(GL_TEXTURE_2D, color);
2522     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, kSize, kSize, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
2523 
2524     GLFramebuffer fb;
2525     glBindFramebuffer(GL_FRAMEBUFFER, fb);
2526     glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, color, 0);
2527     EXPECT_GL_NO_ERROR();
2528     ASSERT_GL_FRAMEBUFFER_COMPLETE(GL_FRAMEBUFFER);
2529 
2530     // Clear to red.
2531     glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
2532     glClear(GL_COLOR_BUFFER_BIT);
2533 
2534     // Mask red and clear to green with a scissor
2535     glColorMask(GL_FALSE, GL_TRUE, GL_TRUE, GL_TRUE);
2536     glScissor(0, 0, kSize / 2, kSize);
2537     glEnable(GL_SCISSOR_TEST);
2538     glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
2539     glClear(GL_COLOR_BUFFER_BIT);
2540 
2541     // Verify that the left half is yellow, and the right half is red.
2542     EXPECT_PIXEL_RECT_EQ(0, 0, kSize / 2, kSize, GLColor::yellow);
2543     EXPECT_PIXEL_RECT_EQ(kSize / 2, 0, kSize / 2, kSize, GLColor::red);
2544 }
2545 
2546 // This is a test that must be verified visually.
2547 //
2548 // Tests that clear of the default framebuffer applies to the window.
TEST_P(ClearTest,DISABLED_ClearReachesWindow)2549 TEST_P(ClearTest, DISABLED_ClearReachesWindow)
2550 {
2551     ANGLE_GL_PROGRAM(blueProgram, essl1_shaders::vs::Simple(), essl1_shaders::fs::Blue());
2552 
2553     // Draw blue.
2554     drawQuad(blueProgram, essl1_shaders::PositionAttrib(), 0.5f);
2555     swapBuffers();
2556 
2557     // Use glClear to clear to red.  Regression test for the Vulkan backend where this clear
2558     // remained "deferred" and didn't make it to the window on swap.
2559     glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
2560     glClear(GL_COLOR_BUFFER_BIT);
2561     swapBuffers();
2562 
2563     // Wait for visual verification.
2564     angle::Sleep(2000);
2565 }
2566 
2567 // Test that clearing slices of a 3D texture and reading them back works.
TEST_P(ClearTestES3,ClearAndReadPixels3DTexture)2568 TEST_P(ClearTestES3, ClearAndReadPixels3DTexture)
2569 {
2570     constexpr uint32_t kWidth  = 128;
2571     constexpr uint32_t kHeight = 128;
2572     constexpr uint32_t kDepth  = 7;
2573 
2574     GLTexture texture;
2575     glBindTexture(GL_TEXTURE_3D, texture);
2576     glTexStorage3D(GL_TEXTURE_3D, 1, GL_RGBA8, kWidth, kHeight, kDepth);
2577     glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAX_LEVEL, 0);
2578 
2579     std::array<GLColor, kDepth> clearColors = {
2580         GLColor::red,  GLColor::green,   GLColor::blue,  GLColor::yellow,
2581         GLColor::cyan, GLColor::magenta, GLColor::white,
2582     };
2583 
2584     GLFramebuffer framebuffer;
2585     glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
2586 
2587     for (uint32_t z = 0; z < kDepth; ++z)
2588     {
2589         glFramebufferTextureLayer(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, texture, 0, z);
2590         glClearBufferfv(GL_COLOR, 0, clearColors[z].toNormalizedVector().data());
2591     }
2592 
2593     for (uint32_t z = 0; z < kDepth; ++z)
2594     {
2595         glFramebufferTextureLayer(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, texture, 0, z);
2596         EXPECT_PIXEL_COLOR_EQ(0, 0, clearColors[z]);
2597     }
2598 }
2599 
2600 #ifdef Bool
2601 // X11 craziness.
2602 #    undef Bool
2603 #endif
2604 
2605 ANGLE_INSTANTIATE_TEST_ES2_AND_ES3(ClearTest);
2606 
2607 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(ClearTestES3);
2608 ANGLE_INSTANTIATE_TEST_ES3(ClearTestES3);
2609 
2610 ANGLE_INSTANTIATE_TEST_COMBINE_4(MaskedScissoredClearTest,
2611                                  MaskedScissoredClearVariationsTestPrint,
2612                                  testing::Range(0, 3),
2613                                  testing::Range(0, 3),
2614                                  testing::Range(0, 3),
2615                                  testing::Bool(),
2616                                  ES2_D3D9(),
2617                                  ES2_D3D11(),
2618                                  ES3_D3D11(),
2619                                  ES2_OPENGL(),
2620                                  ES3_OPENGL(),
2621                                  ES2_OPENGLES(),
2622                                  ES3_OPENGLES(),
2623                                  ES2_VULKAN(),
2624                                  ES3_VULKAN(),
2625                                  ES2_METAL(),
2626                                  ES3_METAL());
2627 
2628 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(VulkanClearTest);
2629 ANGLE_INSTANTIATE_TEST_COMBINE_4(VulkanClearTest,
2630                                  MaskedScissoredClearVariationsTestPrint,
2631                                  testing::Range(0, 3),
2632                                  testing::Range(0, 3),
2633                                  testing::Range(0, 3),
2634                                  testing::Bool(),
2635                                  ES2_VULKAN(),
2636                                  ES3_VULKAN());
2637 
2638 // Not all ANGLE backends support RGB backbuffers
2639 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(ClearTestRGB);
2640 ANGLE_INSTANTIATE_TEST(ClearTestRGB,
2641                        ES2_D3D11(),
2642                        ES3_D3D11(),
2643                        ES2_VULKAN(),
2644                        ES3_VULKAN(),
2645                        ES2_METAL(),
2646                        ES3_METAL());
2647 
2648 }  // anonymous namespace
2649