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:
TriangleSetupTest()17   TriangleSetupTest() {}
~TriangleSetupTest()18   virtual ~TriangleSetupTest() {}
19   virtual bool Run();
Name() const20   virtual const char* Name() const { return "triangle_setup"; }
21 
22  private:
23   DISALLOW_COPY_AND_ASSIGN(TriangleSetupTest);
24 };
25 
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     "}";
37 
Run()38 bool TriangleSetupTest::Run() {
39   glViewport(0, 0, g_width, g_height);
40 
41   // This specifies a square mesh in the middle of the viewport.
42   // Larger meshes make this test too slow for devices that do 1 mtri/sec.
43   // Also note that GLES 2.0 uses 16 bit indices.
44   GLint width = 128;
45   GLint height = 128;
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);
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");
61 
62   GLushort *indices = NULL;
63   GLuint index_buffer = 0;
64   GLsizeiptr index_buffer_size = 0;
65 
66   {
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);
70     count_ = CreateMesh(&indices, &index_buffer_size, width, height, 0);
71 
72     index_buffer = SetupVBO(GL_ELEMENT_ARRAY_BUFFER,
73                             index_buffer_size, indices);
74     RunTest(this, "triangle_setup", count_ / 3, g_width, g_height, true);
75     glEnable(GL_CULL_FACE);
76     RunTest(this, "triangle_setup_all_culled", count_ / 3, g_width, g_height, true);
77     glDisable(GL_CULL_FACE);
78 
79     glDeleteBuffers(1, &index_buffer);
80     delete[] indices;
81   }
82 
83   {
84     // Use blue-ish color for drawing quad with many holes.
85     const GLfloat cyan[4] = {0.0f, 0.5f, 0.5f, 1.0f};
86     glUniform4fv(color_uniform, 1, cyan);
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);
93     RunTest(this, "triangle_setup_half_culled", count_ / 3, g_width, g_height, true);
94 
95     glDeleteBuffers(1, &index_buffer);
96     delete[] indices;
97   }
98 
99   glDeleteProgram(program);
100   glDeleteBuffers(1, &vertex_buffer);
101   delete[] vertices;
102   return true;
103 }
104 
105 
GetTriangleSetupTest()106 TestBase* GetTriangleSetupTest() {
107   return new TriangleSetupTest;
108 }
109 
110 
111 } // namespace glbench
112