Alexey Marinichev | 9f9b873 | 2010-05-20 19:33:44 -0700 | [diff] [blame] | 1 | // Copyright (c) 2010 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 <stdlib.h> |
| 6 | |
| 7 | #include "main.h" |
| 8 | #include "testbase.h" |
| 9 | #include "utils.h" |
| 10 | |
| 11 | |
| 12 | namespace glbench { |
| 13 | |
| 14 | |
| 15 | class TriangleSetupTest : public DrawElementsTestFunc { |
| 16 | public: |
| 17 | TriangleSetupTest() {} |
| 18 | virtual ~TriangleSetupTest() {} |
| 19 | virtual bool Run(); |
Alexey Marinichev | f50ecb1 | 2010-06-14 15:21:41 -0700 | [diff] [blame] | 20 | virtual const char* Name() const { return "triangle_setup"; } |
Alexey Marinichev | 9f9b873 | 2010-05-20 19:33:44 -0700 | [diff] [blame] | 21 | |
| 22 | private: |
| 23 | DISALLOW_COPY_AND_ASSIGN(TriangleSetupTest); |
| 24 | }; |
| 25 | |
Alexey Marinichev | 2de76ab | 2010-07-12 18:17:42 -0700 | [diff] [blame] | 26 | const char* kVertexShader = |
| 27 | "attribute vec4 c;" |
| 28 | "void main() {" |
| 29 | " gl_Position = c;" |
| 30 | "}"; |
| 31 | |
| 32 | const char* kFragmentShader = |
| 33 | "uniform vec4 color;" |
| 34 | "void main() {" |
| 35 | " gl_FragColor = color;" |
| 36 | "}"; |
Alexey Marinichev | 9f9b873 | 2010-05-20 19:33:44 -0700 | [diff] [blame] | 37 | |
| 38 | bool TriangleSetupTest::Run() { |
Ilja Friedel | c04bdd4 | 2013-01-30 18:55:49 -0800 | [diff] [blame] | 39 | glViewport(0, 0, g_width, g_height); |
Alexey Marinichev | 9f9b873 | 2010-05-20 19:33:44 -0700 | [diff] [blame] | 40 | |
Ilja Friedel | c04bdd4 | 2013-01-30 18:55:49 -0800 | [diff] [blame] | 41 | // This specifies a square mesh in the middle of the viewport. |
Alexey Marinichev | 9f9b873 | 2010-05-20 19:33:44 -0700 | [diff] [blame] | 42 | // Larger meshes make this test too slow for devices that do 1 mtri/sec. |
Ilja Friedel | c04bdd4 | 2013-01-30 18:55:49 -0800 | [diff] [blame] | 43 | // Also note that GLES 2.0 uses 16 bit indices. |
| 44 | GLint width = 128; |
| 45 | GLint height = 128; |
Alexey Marinichev | 9f9b873 | 2010-05-20 19:33:44 -0700 | [diff] [blame] | 46 | |
| 47 | GLfloat *vertices = NULL; |
| 48 | GLsizeiptr vertex_buffer_size = 0; |
| 49 | CreateLattice(&vertices, &vertex_buffer_size, 1.f / g_width, 1.f / g_height, |
| 50 | width, height); |
| 51 | GLuint vertex_buffer = SetupVBO(GL_ARRAY_BUFFER, |
| 52 | vertex_buffer_size, vertices); |
Alexey Marinichev | 2de76ab | 2010-07-12 18:17:42 -0700 | [diff] [blame] | 53 | |
| 54 | GLuint program = |
| 55 | InitShaderProgram(kVertexShader, kFragmentShader); |
| 56 | GLint attribute_index = glGetAttribLocation(program, "c"); |
| 57 | glVertexAttribPointer(attribute_index, 2, GL_FLOAT, GL_FALSE, 0, NULL); |
| 58 | glEnableVertexAttribArray(attribute_index); |
| 59 | |
| 60 | GLint color_uniform = glGetUniformLocation(program, "color"); |
Alexey Marinichev | 9f9b873 | 2010-05-20 19:33:44 -0700 | [diff] [blame] | 61 | |
Stuart Abercrombie | c33878b | 2012-07-25 13:43:27 -0700 | [diff] [blame] | 62 | GLushort *indices = NULL; |
Alexey Marinichev | 9f9b873 | 2010-05-20 19:33:44 -0700 | [diff] [blame] | 63 | GLuint index_buffer = 0; |
| 64 | GLsizeiptr index_buffer_size = 0; |
| 65 | |
| 66 | { |
Ilja Friedel | c04bdd4 | 2013-01-30 18:55:49 -0800 | [diff] [blame] | 67 | // Use orange for drawing solid/all culled quads. |
| 68 | const GLfloat orange[4] = {1.0f, 0.5f, 0.0f, 1.0f}; |
| 69 | glUniform4fv(color_uniform, 1, orange); |
Alexey Marinichev | 9f9b873 | 2010-05-20 19:33:44 -0700 | [diff] [blame] | 70 | count_ = CreateMesh(&indices, &index_buffer_size, width, height, 0); |
| 71 | |
| 72 | index_buffer = SetupVBO(GL_ELEMENT_ARRAY_BUFFER, |
| 73 | index_buffer_size, indices); |
Daniel Kurtz | aace01b | 2014-06-17 21:05:24 +0800 | [diff] [blame] | 74 | RunTest(this, "triangle_setup", count_ / 3, g_width, g_height, true); |
Alexey Marinichev | 9f9b873 | 2010-05-20 19:33:44 -0700 | [diff] [blame] | 75 | glEnable(GL_CULL_FACE); |
Daniel Kurtz | aace01b | 2014-06-17 21:05:24 +0800 | [diff] [blame] | 76 | RunTest(this, "triangle_setup_all_culled", count_ / 3, g_width, g_height, true); |
Alexey Marinichev | 9f9b873 | 2010-05-20 19:33:44 -0700 | [diff] [blame] | 77 | glDisable(GL_CULL_FACE); |
| 78 | |
| 79 | glDeleteBuffers(1, &index_buffer); |
| 80 | delete[] indices; |
| 81 | } |
| 82 | |
| 83 | { |
Ilja Friedel | c04bdd4 | 2013-01-30 18:55:49 -0800 | [diff] [blame] | 84 | // Use blue-ish color for drawing quad with many holes. |
| 85 | const GLfloat cyan[4] = {0.0f, 0.5f, 0.5f, 1.0f}; |
Alexey Marinichev | 2de76ab | 2010-07-12 18:17:42 -0700 | [diff] [blame] | 86 | glUniform4fv(color_uniform, 1, cyan); |
Alexey Marinichev | 9f9b873 | 2010-05-20 19:33:44 -0700 | [diff] [blame] | 87 | count_ = CreateMesh(&indices, &index_buffer_size, width, height, |
| 88 | RAND_MAX / 2); |
| 89 | |
| 90 | index_buffer = SetupVBO(GL_ELEMENT_ARRAY_BUFFER, |
| 91 | index_buffer_size, indices); |
| 92 | glEnable(GL_CULL_FACE); |
Daniel Kurtz | aace01b | 2014-06-17 21:05:24 +0800 | [diff] [blame] | 93 | RunTest(this, "triangle_setup_half_culled", count_ / 3, g_width, g_height, true); |
Alexey Marinichev | 9f9b873 | 2010-05-20 19:33:44 -0700 | [diff] [blame] | 94 | |
| 95 | glDeleteBuffers(1, &index_buffer); |
| 96 | delete[] indices; |
| 97 | } |
| 98 | |
Alexey Marinichev | 2de76ab | 2010-07-12 18:17:42 -0700 | [diff] [blame] | 99 | glDeleteProgram(program); |
Alexey Marinichev | 9f9b873 | 2010-05-20 19:33:44 -0700 | [diff] [blame] | 100 | glDeleteBuffers(1, &vertex_buffer); |
| 101 | delete[] vertices; |
| 102 | return true; |
| 103 | } |
| 104 | |
| 105 | |
| 106 | TestBase* GetTriangleSetupTest() { |
| 107 | return new TriangleSetupTest; |
| 108 | } |
| 109 | |
| 110 | |
| 111 | } // namespace glbench |