Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / gpu / command_buffer / tests / gl_clear_framebuffer_unittest.cc
blob3ce80f9623a812ff17c1f2a7886d58051d35741a
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
7 #endif
9 #include <GLES2/gl2.h>
10 #include <GLES2/gl2ext.h>
11 #include <GLES2/gl2extchromium.h>
13 #include <vector>
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"
23 namespace gpu {
25 // A collection of tests that exercise the glClear workaround.
26 class GLClearFramebufferTest : public testing::TestWithParam<bool> {
27 public:
28 GLClearFramebufferTest() : color_handle_(0u), depth_handle_(0u) {}
30 protected:
31 void SetUp() override {
32 if (GetParam()) {
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);
39 } else {
40 gl_.Initialize(GLManager::Options());
41 DCHECK(!gl_.workarounds().gl_clear_broken);
45 void InitDraw();
46 void SetDrawColor(GLfloat r, GLfloat g, GLfloat b, GLfloat a);
47 void SetDrawDepth(GLfloat depth);
48 void DrawQuad();
50 void TearDown() override {
51 GLTestHelper::CheckGLError("no errors", __LINE__);
52 gl_.Destroy();
55 private:
56 GLManager gl_;
57 GLuint color_handle_;
58 GLuint depth_handle_;
61 void GLClearFramebufferTest::InitDraw() {
62 static const char* v_shader_str =
63 "attribute vec4 a_Position;\n"
64 "uniform float u_depth;\n"
65 "void main()\n"
66 "{\n"
67 " gl_Position = a_Position;\n"
68 " gl_Position.z = u_depth;\n"
69 "}\n";
70 static const char* f_shader_str =
71 "precision mediump float;\n"
72 "uniform vec4 u_draw_color;\n"
73 "void main()\n"
74 "{\n"
75 " gl_FragColor = u_draw_color;\n"
76 "}\n";
78 GLuint program = GLTestHelper::LoadProgram(v_shader_str, f_shader_str);
79 DCHECK(program);
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,
91 GLfloat g,
92 GLfloat b,
93 GLfloat a) {
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);
113 // Verify.
114 const uint8 expected[] = {255, 128, 64, 128};
115 EXPECT_TRUE(
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);
124 // Verify.
125 const uint8 expected[] = {255, 0, 0, 0};
126 EXPECT_TRUE(
127 GLTestHelper::CheckPixels(0, 0, 1, 1, 0 /* tolerance */, expected));
130 // crbug.com/434094
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);
136 // Verify.
137 const uint8 expected[] = {255, 255, 255, 255};
138 EXPECT_TRUE(
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.
147 EXPECT_TRUE(
148 GLTestHelper::CheckPixels(0, 0, 1, 1, 0 /* tolerance */, expected));
150 #endif
152 TEST_P(GLClearFramebufferTest, ClearDepthStencil) {
153 const GLuint kStencilRef = 1 << 2;
154 InitDraw();
155 SetDrawColor(1.0f, 0.0f, 0.0f, 1.0f);
156 DrawQuad();
157 // Verify.
158 const uint8 kRed[] = {255, 0, 0, 255};
159 const uint8 kGreen[] = {0, 255, 0, 255};
160 EXPECT_TRUE(
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);
170 DrawQuad();
171 // Verify - stencil should have failed, so still red.
172 EXPECT_TRUE(
173 GLTestHelper::CheckPixels(0, 0, 1, 1, 0 /* tolerance */, kRed));
175 glStencilFunc(GL_EQUAL, kStencilRef, 0xFFFFFFFF);
176 DrawQuad();
177 // Verify - stencil should have passed, so green.
178 EXPECT_TRUE(
179 GLTestHelper::CheckPixels(0, 0, 1, 1, 0 /* tolerance */, kGreen));
181 glEnable(GL_DEPTH_TEST);
182 glClearDepthf(0.0f);
183 glClear(GL_DEPTH_BUFFER_BIT);
185 SetDrawDepth(0.5f);
186 SetDrawColor(1.0f, 0.0f, 0.0f, 1.0f);
187 DrawQuad();
188 // Verify - depth test should have failed, so still green.
189 EXPECT_TRUE(
190 GLTestHelper::CheckPixels(0, 0, 1, 1, 0 /* tolerance */, kGreen));
192 glClearDepthf(0.9f);
193 glClear(GL_DEPTH_BUFFER_BIT);
194 DrawQuad();
195 // Verify - depth test should have passed, so red.
196 EXPECT_TRUE(
197 GLTestHelper::CheckPixels(0, 0, 1, 1, 0 /* tolerance */, kRed));
200 } // namespace gpu