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