1 // Copyright (c) 2012 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.
6 #include <GLES2/gl2ext.h>
7 #include <GLES2/gl2extchromium.h>
9 #include "gpu/command_buffer/tests/gl_manager.h"
10 #include "gpu/command_buffer/tests/gl_test_utils.h"
11 #include "testing/gmock/include/gmock/gmock.h"
12 #include "testing/gtest/include/gtest/gtest.h"
16 class GLChromiumFramebufferMultisampleTest
: public testing::Test
{
18 void SetUp() override
{ gl_
.Initialize(GLManager::Options()); }
20 void TearDown() override
{ gl_
.Destroy(); }
25 // Test that GL is at least minimally working.
26 TEST_F(GLChromiumFramebufferMultisampleTest
, CachedBindingsTest
) {
27 if (!GLTestHelper::HasExtension("GL_CHROMIUM_framebuffer_multisample")) {
32 glGenFramebuffers(1, &fbo
);
33 glBindFramebuffer(GL_DRAW_FRAMEBUFFER
, fbo
);
34 glBindFramebuffer(GL_FRAMEBUFFER
, 0);
36 // If the caching is bad the second call to glBindFramebuffer will do nothing.
37 // which means the draw buffer is bad and will not return
38 // GL_FRAMEBUFFER_COMPLETE and rendering will generate an error.
39 EXPECT_EQ(static_cast<GLenum
>(GL_FRAMEBUFFER_COMPLETE
),
40 glCheckFramebufferStatus(GL_FRAMEBUFFER
));
42 glClear(GL_COLOR_BUFFER_BIT
);
43 GLTestHelper::CheckGLError("no errors", __LINE__
);
46 TEST_F(GLChromiumFramebufferMultisampleTest
, DrawAndResolve
) {
47 if (!GLTestHelper::HasExtension("GL_CHROMIUM_framebuffer_multisample")) {
51 static const char* v_shader_str
=
52 "attribute vec4 a_Position;\n"
55 " gl_Position = a_Position;\n"
57 static const char* f_shader_str
=
58 "precision mediump float;\n"
61 " gl_FragColor = vec4(0.0, 1.0, 0.0, 1.0);\n"
64 GLuint program
= GLTestHelper::LoadProgram(v_shader_str
, f_shader_str
);
65 glUseProgram(program
);
66 GLuint position_loc
= glGetAttribLocation(program
, "a_Position");
68 GLTestHelper::SetupUnitQuad(position_loc
);
70 const GLuint width
= 100;
71 const GLuint height
= 100;
73 // Create a sample buffer.
74 GLsizei num_samples
= 4, max_samples
= 0;
75 glGetIntegerv(GL_MAX_SAMPLES
, &max_samples
);
76 num_samples
= std::min(num_samples
, max_samples
);
78 GLuint sample_fbo
, sample_rb
;
79 glGenRenderbuffers(1, &sample_rb
);
80 glBindRenderbuffer(GL_RENDERBUFFER
, sample_rb
);
81 glRenderbufferStorageMultisampleCHROMIUM(
82 GL_RENDERBUFFER
, num_samples
, GL_RGBA8_OES
, width
, height
);
84 glGetRenderbufferParameteriv(
85 GL_RENDERBUFFER
, GL_RENDERBUFFER_SAMPLES
, ¶m
);
86 EXPECT_GE(param
, num_samples
);
88 glGenFramebuffers(1, &sample_fbo
);
89 glBindFramebuffer(GL_FRAMEBUFFER
, sample_fbo
);
90 glFramebufferRenderbuffer(GL_FRAMEBUFFER
,
94 EXPECT_EQ(static_cast<GLenum
>(GL_FRAMEBUFFER_COMPLETE
),
95 glCheckFramebufferStatus(GL_FRAMEBUFFER
));
97 // Create another FBO to resolve the multisample buffer into.
98 GLuint resolve_fbo
, resolve_tex
;
99 glGenTextures(1, &resolve_tex
);
100 glBindTexture(GL_TEXTURE_2D
, resolve_tex
);
101 glTexImage2D(GL_TEXTURE_2D
,
110 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR
);
111 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
112 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_S
, GL_CLAMP_TO_EDGE
);
113 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_WRAP_T
, GL_CLAMP_TO_EDGE
);
114 glGenFramebuffers(1, &resolve_fbo
);
115 glBindFramebuffer(GL_FRAMEBUFFER
, resolve_fbo
);
116 glFramebufferTexture2D(GL_FRAMEBUFFER
,
117 GL_COLOR_ATTACHMENT0
,
121 EXPECT_EQ(static_cast<GLenum
>(GL_FRAMEBUFFER_COMPLETE
),
122 glCheckFramebufferStatus(GL_FRAMEBUFFER
));
124 // Draw one triangle (bottom left half).
125 glViewport(0, 0, width
, height
);
126 glBindFramebuffer(GL_FRAMEBUFFER
, sample_fbo
);
127 glClearColor(0.0f
, 0.0f
, 0.0f
, 0.0f
);
128 glClear(GL_COLOR_BUFFER_BIT
);
129 glDrawArrays(GL_TRIANGLES
, 0, 3);
132 glBindFramebuffer(GL_READ_FRAMEBUFFER
, sample_fbo
);
133 glBindFramebuffer(GL_DRAW_FRAMEBUFFER
, resolve_fbo
);
134 glClearColor(1.0f
, 0.0f
, 0.0f
, 0.0f
);
135 glClear(GL_COLOR_BUFFER_BIT
);
136 glBlitFramebufferCHROMIUM(0,
148 const uint8 green
[] = {0, 255, 0, 255};
149 const uint8 black
[] = {0, 0, 0, 0};
150 glBindFramebuffer(GL_READ_FRAMEBUFFER
, resolve_fbo
);
152 GLTestHelper::CheckPixels(width
/ 4, (3 * height
) / 4, 1, 1, 0, green
));
153 EXPECT_TRUE(GLTestHelper::CheckPixels(width
- 1, 0, 1, 1, 0, black
));