occlusion_query_unittest.cc 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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.
  4. #include <GLES2/gl2.h>
  5. #include <GLES2/gl2ext.h>
  6. #include "build/build_config.h"
  7. #include "gpu/command_buffer/tests/gl_manager.h"
  8. #include "gpu/command_buffer/tests/gl_test_utils.h"
  9. #include "testing/gmock/include/gmock/gmock.h"
  10. #include "testing/gtest/include/gtest/gtest.h"
  11. namespace gpu {
  12. class OcclusionQueryTest : public testing::Test {
  13. protected:
  14. void SetUp() override {
  15. GLManager::Options options;
  16. options.size = gfx::Size(512, 512);
  17. gl_.Initialize(options);
  18. }
  19. void TearDown() override { gl_.Destroy(); }
  20. void DrawRect(float x, float z, float scale, float* color);
  21. GLManager gl_;
  22. GLint position_loc_;
  23. GLint matrix_loc_;
  24. GLint color_loc_;
  25. };
  26. static void SetMatrix(float x, float z, float scale, float* matrix) {
  27. matrix[0] = scale;
  28. matrix[1] = 0.0f;
  29. matrix[2] = 0.0f;
  30. matrix[3] = 0.0f;
  31. matrix[4] = 0.0f;
  32. matrix[5] = scale;
  33. matrix[6] = 0.0f;
  34. matrix[7] = 0.0f;
  35. matrix[8] = 0.0f;
  36. matrix[9] = 0.0f;
  37. matrix[10] = scale;
  38. matrix[11] = 0.0f;
  39. matrix[12] = x;
  40. matrix[13] = 0.0f;
  41. matrix[14] = z;
  42. matrix[15] = 1.0f;
  43. }
  44. void OcclusionQueryTest::DrawRect(float x, float z, float scale, float* color) {
  45. GLfloat matrix[16];
  46. SetMatrix(x, z, scale, matrix);
  47. // Set up the model matrix
  48. glUniformMatrix4fv(matrix_loc_, 1, GL_FALSE, matrix);
  49. glUniform4fv(color_loc_, 1, color);
  50. glDrawArrays(GL_TRIANGLES, 0, 6);
  51. }
  52. TEST_F(OcclusionQueryTest, Occlusion) {
  53. #if BUILDFLAG(IS_MAC)
  54. EXPECT_TRUE(GLTestHelper::HasExtension("GL_EXT_occlusion_query_boolean"))
  55. << "GL_EXT_occlusion_query_boolean is required on OSX";
  56. #endif
  57. if (!GLTestHelper::HasExtension("GL_EXT_occlusion_query_boolean")) {
  58. return;
  59. }
  60. static const char* v_shader_str =
  61. "uniform mat4 worldMatrix;\n"
  62. "attribute vec3 g_Position;\n"
  63. "void main()\n"
  64. "{\n"
  65. " gl_Position = worldMatrix *\n"
  66. " vec4(g_Position.x, g_Position.y, g_Position.z, 1.0);\n"
  67. "}\n";
  68. static const char* f_shader_str =
  69. "precision mediump float;"
  70. "uniform vec4 color;\n"
  71. "void main()\n"
  72. "{\n"
  73. " gl_FragColor = color;\n"
  74. "}\n";
  75. GLuint program = GLTestHelper::LoadProgram(v_shader_str, f_shader_str);
  76. position_loc_ = glGetAttribLocation(program, "g_Position");
  77. matrix_loc_ = glGetUniformLocation(program, "worldMatrix");
  78. color_loc_ = glGetUniformLocation(program, "color");
  79. GLTestHelper::SetupUnitQuad(position_loc_);
  80. GLuint query = 0;
  81. glGenQueriesEXT(1, &query);
  82. glEnable(GL_DEPTH_TEST);
  83. glClearColor(0.0f, 0.1f, 0.2f, 1.0f);
  84. // Use the program object
  85. glUseProgram(program);
  86. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  87. static float red[] = { 1.0f, 0.0f, 0.0f, 1.0f };
  88. DrawRect(0, 0.0f, 0.50f, red);
  89. glBeginQueryEXT(GL_ANY_SAMPLES_PASSED_EXT, query);
  90. static float blue[] = { 0.0f, 0.0f, 1.0f, 1.0f };
  91. DrawRect(-0.125f, 0.1f, 0.25f, blue);
  92. glEndQueryEXT(GL_ANY_SAMPLES_PASSED_EXT);
  93. glFinish();
  94. GLuint query_status = 0;
  95. GLuint result = 0;
  96. glGetQueryObjectuivEXT(query, GL_QUERY_RESULT_AVAILABLE_EXT, &result);
  97. EXPECT_TRUE(result);
  98. glGetQueryObjectuivEXT(query, GL_QUERY_RESULT_EXT, &query_status);
  99. EXPECT_FALSE(query_status);
  100. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  101. DrawRect(1, 0.0f, 0.50f, red);
  102. glBeginQueryEXT(GL_ANY_SAMPLES_PASSED_EXT, query);
  103. DrawRect(-0.125f, 0.1f, 0.25f, blue);
  104. glEndQueryEXT(GL_ANY_SAMPLES_PASSED_EXT);
  105. glFinish();
  106. query_status = 0;
  107. result = 0;
  108. glGetQueryObjectuivEXT(query, GL_QUERY_RESULT_AVAILABLE_EXT, &result);
  109. EXPECT_TRUE(result);
  110. glGetQueryObjectuivEXT(query, GL_QUERY_RESULT_EXT, &query_status);
  111. EXPECT_TRUE(query_status);
  112. GLTestHelper::CheckGLError("no errors", __LINE__);
  113. }
  114. } // namespace gpu