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