gl_chromium_framebuffer_multisample_unittest.cc 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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.
  4. #include <GLES2/gl2.h>
  5. #include <GLES2/gl2ext.h>
  6. #include <GLES2/gl2extchromium.h>
  7. #include <stdint.h>
  8. #include "gpu/command_buffer/tests/gl_manager.h"
  9. #include "gpu/command_buffer/tests/gl_test_utils.h"
  10. #include "testing/gmock/include/gmock/gmock.h"
  11. #include "testing/gtest/include/gtest/gtest.h"
  12. namespace gpu {
  13. class GLChromiumFramebufferMultisampleTest : public testing::Test {
  14. protected:
  15. void SetUp() override { gl_.Initialize(GLManager::Options()); }
  16. void TearDown() override { gl_.Destroy(); }
  17. GLManager gl_;
  18. };
  19. // Test that GL is at least minimally working.
  20. TEST_F(GLChromiumFramebufferMultisampleTest, CachedBindingsTest) {
  21. if (!GLTestHelper::HasExtension("GL_CHROMIUM_framebuffer_multisample")) {
  22. return;
  23. }
  24. GLuint fbo = 0;
  25. glGenFramebuffers(1, &fbo);
  26. glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
  27. glBindFramebuffer(GL_FRAMEBUFFER, 0);
  28. // If the caching is bad the second call to glBindFramebuffer will do nothing.
  29. // which means the draw buffer is bad and will not return
  30. // GL_FRAMEBUFFER_COMPLETE and rendering will generate an error.
  31. EXPECT_EQ(static_cast<GLenum>(GL_FRAMEBUFFER_COMPLETE),
  32. glCheckFramebufferStatus(GL_FRAMEBUFFER));
  33. glClear(GL_COLOR_BUFFER_BIT);
  34. GLTestHelper::CheckGLError("no errors", __LINE__);
  35. }
  36. TEST_F(GLChromiumFramebufferMultisampleTest, DrawAndResolve) {
  37. if (!(GLTestHelper::HasExtension("GL_CHROMIUM_framebuffer_multisample") &&
  38. GLTestHelper::HasExtension("GL_OES_rgb8_rgba8"))) {
  39. return;
  40. }
  41. static const char* v_shader_str =
  42. "attribute vec4 a_Position;\n"
  43. "void main()\n"
  44. "{\n"
  45. " gl_Position = a_Position;\n"
  46. "}\n";
  47. static const char* f_shader_str =
  48. "precision mediump float;\n"
  49. "void main()\n"
  50. "{\n"
  51. " gl_FragColor = vec4(0.0, 1.0, 0.0, 1.0);\n"
  52. "}\n";
  53. GLuint program = GLTestHelper::LoadProgram(v_shader_str, f_shader_str);
  54. glUseProgram(program);
  55. GLuint position_loc = glGetAttribLocation(program, "a_Position");
  56. GLTestHelper::SetupUnitQuad(position_loc);
  57. const GLuint width = 100;
  58. const GLuint height = 100;
  59. // Create a sample buffer.
  60. GLsizei num_samples = 4, max_samples = 0;
  61. glGetIntegerv(GL_MAX_SAMPLES, &max_samples);
  62. num_samples = std::min(num_samples, max_samples);
  63. GLuint sample_fbo, sample_rb;
  64. glGenRenderbuffers(1, &sample_rb);
  65. glBindRenderbuffer(GL_RENDERBUFFER, sample_rb);
  66. glRenderbufferStorageMultisampleCHROMIUM(
  67. GL_RENDERBUFFER, num_samples, GL_RGBA8_OES, width, height);
  68. GLint param = 0;
  69. glGetRenderbufferParameteriv(
  70. GL_RENDERBUFFER, GL_RENDERBUFFER_SAMPLES, &param);
  71. EXPECT_GE(param, num_samples);
  72. glGenFramebuffers(1, &sample_fbo);
  73. glBindFramebuffer(GL_FRAMEBUFFER, sample_fbo);
  74. glFramebufferRenderbuffer(GL_FRAMEBUFFER,
  75. GL_COLOR_ATTACHMENT0,
  76. GL_RENDERBUFFER,
  77. sample_rb);
  78. EXPECT_EQ(static_cast<GLenum>(GL_FRAMEBUFFER_COMPLETE),
  79. glCheckFramebufferStatus(GL_FRAMEBUFFER));
  80. // Create another FBO to resolve the multisample buffer into.
  81. GLuint resolve_fbo, resolve_tex;
  82. glGenTextures(1, &resolve_tex);
  83. glBindTexture(GL_TEXTURE_2D, resolve_tex);
  84. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA,
  85. GL_UNSIGNED_BYTE, nullptr);
  86. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  87. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  88. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  89. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  90. glGenFramebuffers(1, &resolve_fbo);
  91. glBindFramebuffer(GL_FRAMEBUFFER, resolve_fbo);
  92. glFramebufferTexture2D(GL_FRAMEBUFFER,
  93. GL_COLOR_ATTACHMENT0,
  94. GL_TEXTURE_2D,
  95. resolve_tex,
  96. 0);
  97. EXPECT_EQ(static_cast<GLenum>(GL_FRAMEBUFFER_COMPLETE),
  98. glCheckFramebufferStatus(GL_FRAMEBUFFER));
  99. // Draw one triangle (bottom left half).
  100. glViewport(0, 0, width, height);
  101. glBindFramebuffer(GL_FRAMEBUFFER, sample_fbo);
  102. glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
  103. glClear(GL_COLOR_BUFFER_BIT);
  104. glDrawArrays(GL_TRIANGLES, 0, 3);
  105. // Resolve.
  106. glBindFramebuffer(GL_READ_FRAMEBUFFER, sample_fbo);
  107. glBindFramebuffer(GL_DRAW_FRAMEBUFFER, resolve_fbo);
  108. glClearColor(1.0f, 0.0f, 0.0f, 0.0f);
  109. glClear(GL_COLOR_BUFFER_BIT);
  110. glBlitFramebufferCHROMIUM(0,
  111. 0,
  112. width,
  113. height,
  114. 0,
  115. 0,
  116. width,
  117. height,
  118. GL_COLOR_BUFFER_BIT,
  119. GL_NEAREST);
  120. // Verify.
  121. const uint8_t green[] = {0, 255, 0, 255};
  122. const uint8_t black[] = {0, 0, 0, 0};
  123. glBindFramebuffer(GL_READ_FRAMEBUFFER, resolve_fbo);
  124. EXPECT_TRUE(GLTestHelper::CheckPixels(width / 4, (3 * height) / 4, 1, 1, 0,
  125. green, nullptr));
  126. EXPECT_TRUE(GLTestHelper::CheckPixels(width - 1, 0, 1, 1, 0, black, nullptr));
  127. }
  128. } // namespace gpu