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