1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef GL_GLEXT_PROTOTYPES
6 #define GL_GLEXT_PROTOTYPES
10 #include <GLES2/gl2ext.h>
11 #include <GLES2/gl2extchromium.h>
15 #include "base/command_line.h"
16 #include "base/strings/string_number_conversions.h"
17 #include "gpu/command_buffer/tests/gl_manager.h"
18 #include "gpu/command_buffer/tests/gl_test_utils.h"
19 #include "gpu/config/gpu_switches.h"
20 #include "testing/gmock/include/gmock/gmock.h"
21 #include "testing/gtest/include/gtest/gtest.h"
25 // A collection of tests that exercise the glClear workaround.
26 class GLClearFramebufferTest
: public testing::TestWithParam
<bool> {
28 GLClearFramebufferTest() : color_handle_(0u), depth_handle_(0u) {}
31 void SetUp() override
{
33 // Force the glClear() workaround so we can test it here.
34 base::CommandLine
command_line(base::CommandLine::NO_PROGRAM
);
35 command_line
.AppendSwitchASCII(switches::kGpuDriverBugWorkarounds
,
36 base::IntToString(gpu::GL_CLEAR_BROKEN
));
37 gl_
.InitializeWithCommandLine(GLManager::Options(), &command_line
);
38 DCHECK(gl_
.workarounds().gl_clear_broken
);
40 gl_
.Initialize(GLManager::Options());
41 DCHECK(!gl_
.workarounds().gl_clear_broken
);
46 void SetDrawColor(GLfloat r
, GLfloat g
, GLfloat b
, GLfloat a
);
47 void SetDrawDepth(GLfloat depth
);
50 void TearDown() override
{
51 GLTestHelper::CheckGLError("no errors", __LINE__
);
61 void GLClearFramebufferTest::InitDraw() {
62 static const char* v_shader_str
=
63 "attribute vec4 a_Position;\n"
64 "uniform float u_depth;\n"
67 " gl_Position = a_Position;\n"
68 " gl_Position.z = u_depth;\n"
70 static const char* f_shader_str
=
71 "precision mediump float;\n"
72 "uniform vec4 u_draw_color;\n"
75 " gl_FragColor = u_draw_color;\n"
78 GLuint program
= GLTestHelper::LoadProgram(v_shader_str
, f_shader_str
);
80 glUseProgram(program
);
81 GLuint position_loc
= glGetAttribLocation(program
, "a_Position");
83 GLTestHelper::SetupUnitQuad(position_loc
);
84 color_handle_
= glGetUniformLocation(program
, "u_draw_color");
85 DCHECK(color_handle_
!= static_cast<GLuint
>(-1));
86 depth_handle_
= glGetUniformLocation(program
, "u_depth");
87 DCHECK(depth_handle_
!= static_cast<GLuint
>(-1));
90 void GLClearFramebufferTest::SetDrawColor(GLfloat r
,
94 glUniform4f(color_handle_
, r
, g
, b
, a
);
97 void GLClearFramebufferTest::SetDrawDepth(GLfloat depth
) {
98 glUniform1f(depth_handle_
, depth
);
101 void GLClearFramebufferTest::DrawQuad() {
102 glDrawArrays(GL_TRIANGLES
, 0, 6);
105 INSTANTIATE_TEST_CASE_P(GLClearFramebufferTestWithParam
,
106 GLClearFramebufferTest
,
107 ::testing::Values(true, false));
109 TEST_P(GLClearFramebufferTest
, ClearColor
) {
110 glClearColor(1.0f
, 0.5f
, 0.25f
, 0.5f
);
111 glClear(GL_COLOR_BUFFER_BIT
);
114 const uint8 expected
[] = {255, 128, 64, 128};
116 GLTestHelper::CheckPixels(0, 0, 1, 1, 1 /* tolerance */, expected
));
119 TEST_P(GLClearFramebufferTest
, ClearColorWithMask
) {
120 glColorMask(GL_TRUE
, GL_FALSE
, GL_FALSE
, GL_FALSE
);
121 glClearColor(1.0f
, 1.0f
, 1.0f
, 1.0f
);
122 glClear(GL_COLOR_BUFFER_BIT
);
125 const uint8 expected
[] = {255, 0, 0, 0};
127 GLTestHelper::CheckPixels(0, 0, 1, 1, 0 /* tolerance */, expected
));
131 #if !defined(OS_MACOSX)
132 TEST_P(GLClearFramebufferTest
, ClearColorWithScissor
) {
133 glClearColor(1.0f
, 1.0f
, 1.0f
, 1.0f
);
134 glClear(GL_COLOR_BUFFER_BIT
);
137 const uint8 expected
[] = {255, 255, 255, 255};
139 GLTestHelper::CheckPixels(0, 0, 1, 1, 0 /* tolerance */, expected
));
141 glScissor(0, 0, 0, 0);
142 glEnable(GL_SCISSOR_TEST
);
143 glClearColor(0, 0, 0, 0);
144 glClear(GL_COLOR_BUFFER_BIT
);
146 // Verify - no changes.
148 GLTestHelper::CheckPixels(0, 0, 1, 1, 0 /* tolerance */, expected
));
152 TEST_P(GLClearFramebufferTest
, ClearDepthStencil
) {
153 const GLuint kStencilRef
= 1 << 2;
155 SetDrawColor(1.0f
, 0.0f
, 0.0f
, 1.0f
);
158 const uint8 kRed
[] = {255, 0, 0, 255};
159 const uint8 kGreen
[] = {0, 255, 0, 255};
161 GLTestHelper::CheckPixels(0, 0, 1, 1, 0 /* tolerance */, kRed
));
163 glClearStencil(kStencilRef
);
164 glClear(GL_STENCIL_BUFFER_BIT
);
165 glEnable(GL_STENCIL_TEST
);
166 glStencilOp(GL_KEEP
, GL_KEEP
, GL_KEEP
);
167 glStencilFunc(GL_NOTEQUAL
, kStencilRef
, 0xFFFFFFFF);
169 SetDrawColor(0.0f
, 1.0f
, 0.0f
, 1.0f
);
171 // Verify - stencil should have failed, so still red.
173 GLTestHelper::CheckPixels(0, 0, 1, 1, 0 /* tolerance */, kRed
));
175 glStencilFunc(GL_EQUAL
, kStencilRef
, 0xFFFFFFFF);
177 // Verify - stencil should have passed, so green.
179 GLTestHelper::CheckPixels(0, 0, 1, 1, 0 /* tolerance */, kGreen
));
181 glEnable(GL_DEPTH_TEST
);
183 glClear(GL_DEPTH_BUFFER_BIT
);
186 SetDrawColor(1.0f
, 0.0f
, 0.0f
, 1.0f
);
188 // Verify - depth test should have failed, so still green.
190 GLTestHelper::CheckPixels(0, 0, 1, 1, 0 /* tolerance */, kGreen
));
193 glClear(GL_DEPTH_BUFFER_BIT
);
195 // Verify - depth test should have passed, so red.
197 GLTestHelper::CheckPixels(0, 0, 1, 1, 0 /* tolerance */, kRed
));