1 // Copyright (c) 2013 The Chromium OS 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 <functional> 6 7 #include "arraysize.h" 8 #include "glinterfacetest.h" 9 10 namespace glbench { 11 12 namespace { 13 14 // Basic shader code. 15 const char* kVertexShader = 16 "attribute vec4 c;" 17 "void main() {" 18 " gl_Position = c;" 19 "}"; 20 21 const char* kFragmentShader = 22 "uniform vec4 color;" 23 "void main() {" 24 " gl_FragColor = color;" 25 "}"; 26 27 // Vertex arrays used to draw a diamond. 28 const GLfloat kVertices[] = {1.0, 0.0, 29 0.0, -1.0, 30 -1.0, 0.0, 31 0.0, 1.0}; 32 const GLushort kIndices[] = {0, 1, 2, 33 0, 2, 3}; 34 35 } // namespace 36 37 void GLInterfaceTest::SetupGLRendering() { 38 vertex_buffer_object_ = 39 SetupVBO(GL_ARRAY_BUFFER, sizeof(kVertices), kVertices); 40 41 shader_program_ = InitShaderProgram(kVertexShader, kFragmentShader); 42 attribute_index_ = glGetAttribLocation(shader_program_, "c"); 43 glVertexAttribPointer(attribute_index_, 2, GL_FLOAT, GL_FALSE, 0, NULL); 44 glEnableVertexAttribArray(attribute_index_); 45 46 GLint color_uniform = glGetUniformLocation(shader_program_, "color"); 47 48 const GLfloat white[4] = {1.0f, 1.0f, 1.0f, 1.0f}; 49 glUniform4fv(color_uniform, 1, white); 50 51 num_indices_ = arraysize(kIndices); 52 index_buffer_object_ = 53 SetupVBO(GL_ELEMENT_ARRAY_BUFFER, sizeof(kIndices), kIndices); 54 } 55 56 void GLInterfaceTest::CleanupGLRendering() { 57 glDisableVertexAttribArray(attribute_index_); 58 glDeleteProgram(shader_program_); 59 glDeleteBuffers(1, &index_buffer_object_); 60 glDeleteBuffers(1, &vertex_buffer_object_); 61 } 62 63 bool GLInterfaceTest::Run() { 64 const std::string test_name_base = std::string(Name()) + "_"; 65 66 // Run test without GL commands. 67 render_func_.Reset(); 68 RunTest(this, (test_name_base + "nogl").c_str(), 1.0, g_width, g_height, 69 false); 70 71 // Run main test with simple GL commands. 72 SetupGLRendering(); 73 render_func_.Set(std::bind(&GLInterfaceTest::RenderGLSimple, this)); 74 RunTest(this, (test_name_base + "glsimple").c_str(), 1.0, g_width, g_height, 75 false); 76 CleanupGLRendering(); 77 78 // TODO(sque): Run with complex GL commands. See crosbug.com/36746. 79 return true; 80 } 81 82 void GLInterfaceTest::RenderGLSimple() { 83 glDrawElements(GL_TRIANGLES, num_indices_, GL_UNSIGNED_SHORT, 0); 84 } 85 86 } // namespace glbench 87