OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2015 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. |
| 4 |
| 5 #include <GLES2/gl2.h> |
| 6 #include <GLES2/gl2ext.h> |
| 7 #include <GLES2/gl2extchromium.h> |
| 8 |
| 9 #include "base/command_line.h" |
| 10 #include "gpu/command_buffer/service/gpu_switches.h" |
| 11 #include "gpu/command_buffer/tests/gl_manager.h" |
| 12 #include "gpu/command_buffer/tests/gl_test_utils.h" |
| 13 #include "testing/gmock/include/gmock/gmock.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" |
| 15 |
| 16 #define SHADER(Src) #Src |
| 17 |
| 18 namespace gpu { |
| 19 |
| 20 class CHROMIUMFramebufferMixedSamplesTest : public testing::Test { |
| 21 protected: |
| 22 const GLuint kWidth = 100; |
| 23 const GLuint kHeight = 100; |
| 24 |
| 25 void SetUp() override { |
| 26 base::CommandLine command_line(*base::CommandLine::ForCurrentProcess()); |
| 27 command_line.AppendSwitch(switches::kEnableGLPathRendering); |
| 28 gl_.InitializeWithCommandLine(GLManager::Options(), &command_line); |
| 29 } |
| 30 |
| 31 void TearDown() override { gl_.Destroy(); } |
| 32 |
| 33 bool IsApplicable() const { |
| 34 return GLTestHelper::HasExtension( |
| 35 "GL_CHROMIUM_framebuffer_mixed_samples") && |
| 36 GLTestHelper::HasExtension("GL_OES_rgb8_rgba8"); |
| 37 } |
| 38 |
| 39 enum SetupFBOType { |
| 40 MixedSampleFBO, // 1 color sample, N stencil samples. |
| 41 SingleSampleFBO, // 1 color sample, 1 stencil sample. |
| 42 }; |
| 43 |
| 44 void SetupState(SetupFBOType fbo_type) { |
| 45 // clang-format off |
| 46 static const char* kVertexShaderSource = SHADER( |
| 47 attribute mediump vec4 position; |
| 48 void main() { |
| 49 gl_Position = position; |
| 50 }); |
| 51 static const char* kFragmentShaderSource = SHADER( |
| 52 uniform mediump vec4 color; |
| 53 void main() { |
| 54 gl_FragColor = color; |
| 55 }); |
| 56 // clang-format on |
| 57 GLuint program = |
| 58 GLTestHelper::LoadProgram(kVertexShaderSource, kFragmentShaderSource); |
| 59 glUseProgram(program); |
| 60 GLuint position_loc = glGetAttribLocation(program, "position"); |
| 61 color_loc_ = glGetUniformLocation(program, "color"); |
| 62 |
| 63 GLuint vbo = 0; |
| 64 glGenBuffers(1, &vbo); |
| 65 glBindBuffer(GL_ARRAY_BUFFER, vbo); |
| 66 static float vertices[] = { |
| 67 1.0f, 1.0f, -1.0f, 1.0f, -1.0f, -1.0f, -1.0f, 1.0f, -1.0f, |
| 68 -1.0f, 1.0f, -1.0f, -1.0f, -1.0f, 1.0f, -1.0f, 1.0f, 1.0f, |
| 69 }; |
| 70 glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); |
| 71 glEnableVertexAttribArray(position_loc); |
| 72 glVertexAttribPointer(position_loc, 2, GL_FLOAT, GL_FALSE, 0, 0); |
| 73 glBindBuffer(GL_ARRAY_BUFFER, 0); |
| 74 |
| 75 GLuint texture = 0; |
| 76 glActiveTexture(GL_TEXTURE0); |
| 77 glGenTextures(1, &texture); |
| 78 glBindTexture(GL_TEXTURE_2D, texture); |
| 79 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 80 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
| 81 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
| 82 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
| 83 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, kWidth, kHeight, 0, GL_RGBA, |
| 84 GL_UNSIGNED_BYTE, NULL); |
| 85 glBindTexture(GL_TEXTURE_2D, 0); |
| 86 |
| 87 GLuint stencil_rb = 0; |
| 88 glGenRenderbuffers(1, &stencil_rb); |
| 89 glBindRenderbuffer(GL_RENDERBUFFER, stencil_rb); |
| 90 |
| 91 if (fbo_type == MixedSampleFBO) { |
| 92 // Create a sample buffer. |
| 93 GLsizei num_samples = 8, max_samples = 0; |
| 94 glGetIntegerv(GL_MAX_SAMPLES, &max_samples); |
| 95 num_samples = std::min(num_samples, max_samples); |
| 96 glRenderbufferStorageMultisampleCHROMIUM( |
| 97 GL_RENDERBUFFER, num_samples, GL_STENCIL_INDEX8, kWidth, kHeight); |
| 98 GLint param = 0; |
| 99 glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_SAMPLES, |
| 100 ¶m); |
| 101 EXPECT_GT(param, 1); |
| 102 } else { |
| 103 glRenderbufferStorage(GL_RENDERBUFFER, GL_STENCIL_INDEX8, kWidth, |
| 104 kHeight); |
| 105 } |
| 106 glBindRenderbuffer(GL_RENDERBUFFER, 0); |
| 107 |
| 108 GLuint sample_fbo = 0; |
| 109 glGenFramebuffers(1, &sample_fbo); |
| 110 glBindFramebuffer(GL_FRAMEBUFFER, sample_fbo); |
| 111 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, |
| 112 texture, 0); |
| 113 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, |
| 114 GL_RENDERBUFFER, stencil_rb); |
| 115 EXPECT_EQ(static_cast<GLenum>(GL_FRAMEBUFFER_COMPLETE), |
| 116 glCheckFramebufferStatus(GL_FRAMEBUFFER)); |
| 117 |
| 118 glViewport(0, 0, kWidth, kHeight); |
| 119 glClearColor(0.0f, 0.0f, 0.0f, 0.0f); |
| 120 glClearStencil(1); |
| 121 glClear(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); |
| 122 glEnable(GL_STENCIL_TEST); |
| 123 glEnable(GL_BLEND); |
| 124 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); |
| 125 glStencilMask(0xffffffff); |
| 126 glStencilFunc(GL_EQUAL, 1, 0xffffffff); |
| 127 glStencilOp(GL_KEEP, GL_KEEP, GL_ZERO); |
| 128 } |
| 129 |
| 130 GLManager gl_; |
| 131 GLint color_loc_; |
| 132 }; |
| 133 |
| 134 TEST_F(CHROMIUMFramebufferMixedSamplesTest, Simple) { |
| 135 if (!IsApplicable()) { |
| 136 return; |
| 137 } |
| 138 GLint value = -1; |
| 139 glGetIntegerv(GL_COVERAGE_MODULATION_CHROMIUM, &value); |
| 140 EXPECT_EQ(0, value); |
| 141 GLenum kValues[] = {GL_NONE, GL_RGB, GL_RGBA, GL_ALPHA}; |
| 142 for (auto expect : kValues) { |
| 143 glCoverageModulationCHROMIUM(expect); |
| 144 value = -1; |
| 145 glGetIntegerv(GL_COVERAGE_MODULATION_CHROMIUM, &value); |
| 146 EXPECT_EQ(expect, static_cast<GLenum>(value)); |
| 147 EXPECT_EQ(static_cast<GLenum>(GL_NO_ERROR), glGetError()); |
| 148 } |
| 149 |
| 150 glCoverageModulationCHROMIUM(GL_BYTE); |
| 151 EXPECT_EQ(static_cast<GLenum>(GL_INVALID_ENUM), glGetError()); |
| 152 value = -1; |
| 153 glGetIntegerv(GL_COVERAGE_MODULATION_CHROMIUM, &value); |
| 154 EXPECT_EQ(static_cast<GLenum>(GL_ALPHA), static_cast<GLenum>(value)); |
| 155 EXPECT_EQ(static_cast<GLenum>(GL_NO_ERROR), glGetError()); |
| 156 } |
| 157 |
| 158 // The test pattern is as follows: |
| 159 // A green triangle (bottom left, top right, top left). |
| 160 // A blue triangle (top left, bottom right, bottom left). |
| 161 // The triangles will overlap but overlap only contains green pixels, |
| 162 // due to each draw erasing its area from stencil. |
| 163 // The blue triangle will fill only the area (bottom left, center, |
| 164 // bottom right). |
| 165 |
| 166 // The test tests that CoverageModulation call works. |
| 167 // The fractional pixels of both triangles end up being modulated |
| 168 // by the coverage of the fragment. Test that drawing with and without |
| 169 // CoverageModulation causes the result to be different. |
| 170 TEST_F(CHROMIUMFramebufferMixedSamplesTest, CoverageModulation) { |
| 171 if (!IsApplicable()) { |
| 172 return; |
| 173 } |
| 174 static const float kBlue[] = {0.0f, 0.0f, 1.0f, 1.0f}; |
| 175 static const float kGreen[] = {0.0f, 1.0f, 0.0f, 1.0f}; |
| 176 scoped_ptr<uint8[]> results[3]; |
| 177 const GLint kResultSize = kWidth * kHeight * 4; |
| 178 |
| 179 for (int pass = 0; pass < 3; ++pass) { |
| 180 SetupState(MixedSampleFBO); |
| 181 if (pass == 1) { |
| 182 glCoverageModulationCHROMIUM(GL_RGBA); |
| 183 } |
| 184 glUniform4fv(color_loc_, 1, kGreen); |
| 185 glDrawArrays(GL_TRIANGLES, 0, 3); |
| 186 |
| 187 glUniform4fv(color_loc_, 1, kBlue); |
| 188 glDrawArrays(GL_TRIANGLES, 3, 3); |
| 189 if (pass == 1) { |
| 190 glCoverageModulationCHROMIUM(GL_NONE); |
| 191 } |
| 192 |
| 193 results[pass].reset(new uint8[kResultSize]); |
| 194 memset(results[pass].get(), GLTestHelper::kCheckClearValue, kResultSize); |
| 195 glReadPixels(0, 0, kWidth, kHeight, GL_RGBA, GL_UNSIGNED_BYTE, |
| 196 results[pass].get()); |
| 197 } |
| 198 |
| 199 EXPECT_NE(0, memcmp(results[0].get(), results[1].get(), kResultSize)); |
| 200 // Verify that rendering is deterministic, so that the pass above does not |
| 201 // come from non-deterministic rendering. |
| 202 EXPECT_EQ(0, memcmp(results[0].get(), results[2].get(), kResultSize)); |
| 203 } |
| 204 |
| 205 // The test tests that the stencil buffer can be multisampled, even though the |
| 206 // color buffer is single-sampled. Draws the same pattern with single-sample |
| 207 // stencil buffer and with multisample stencil buffer. The images should differ. |
| 208 TEST_F(CHROMIUMFramebufferMixedSamplesTest, MultisampleStencilEffective) { |
| 209 if (!IsApplicable()) { |
| 210 return; |
| 211 } |
| 212 |
| 213 static const float kBlue[] = {0.0f, 0.0f, 1.0f, 1.0f}; |
| 214 static const float kGreen[] = {0.0f, 1.0f, 0.0f, 1.0f}; |
| 215 |
| 216 scoped_ptr<uint8[]> results[3]; |
| 217 const GLint kResultSize = kWidth * kHeight * 4; |
| 218 |
| 219 for (int pass = 0; pass < 3; ++pass) { |
| 220 if (pass == 1) { |
| 221 SetupState(MixedSampleFBO); |
| 222 } else { |
| 223 SetupState(SingleSampleFBO); |
| 224 } |
| 225 glUniform4fv(color_loc_, 1, kGreen); |
| 226 glDrawArrays(GL_TRIANGLES, 0, 3); |
| 227 |
| 228 glUniform4fv(color_loc_, 1, kBlue); |
| 229 glDrawArrays(GL_TRIANGLES, 3, 3); |
| 230 |
| 231 results[pass].reset(new uint8[kResultSize]); |
| 232 memset(results[pass].get(), GLTestHelper::kCheckClearValue, kResultSize); |
| 233 glReadPixels(0, 0, kWidth, kHeight, GL_RGBA, GL_UNSIGNED_BYTE, |
| 234 results[pass].get()); |
| 235 } |
| 236 |
| 237 EXPECT_NE(0, memcmp(results[0].get(), results[1].get(), kResultSize)); |
| 238 // Verify that rendering is deterministic, so that the pass above does not |
| 239 // come from non-deterministic rendering. |
| 240 EXPECT_EQ(0, memcmp(results[0].get(), results[2].get(), kResultSize)); |
| 241 } |
| 242 |
| 243 } // namespace gpu |
OLD | NEW |