blob: c77d658928aa9ed078f4a39c94c1f8b73d061aad [file] [log] [blame]
Alexey Marinichev9f9b8732010-05-20 19:33:44 -07001// 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
12namespace glbench {
13
14
15class TriangleSetupTest : public DrawElementsTestFunc {
16 public:
17 TriangleSetupTest() {}
18 virtual ~TriangleSetupTest() {}
19 virtual bool Run();
Alexey Marinichevf50ecb12010-06-14 15:21:41 -070020 virtual const char* Name() const { return "triangle_setup"; }
Alexey Marinichev9f9b8732010-05-20 19:33:44 -070021
22 private:
23 DISALLOW_COPY_AND_ASSIGN(TriangleSetupTest);
24};
25
Alexey Marinichev2de76ab2010-07-12 18:17:42 -070026const char* kVertexShader =
27 "attribute vec4 c;"
28 "void main() {"
29 " gl_Position = c;"
30 "}";
31
32const char* kFragmentShader =
33 "uniform vec4 color;"
34 "void main() {"
35 " gl_FragColor = color;"
36 "}";
Alexey Marinichev9f9b8732010-05-20 19:33:44 -070037
38bool TriangleSetupTest::Run() {
Ilja Friedelc04bdd42013-01-30 18:55:49 -080039 glViewport(0, 0, g_width, g_height);
Alexey Marinichev9f9b8732010-05-20 19:33:44 -070040
Ilja Friedelc04bdd42013-01-30 18:55:49 -080041 // This specifies a square mesh in the middle of the viewport.
Alexey Marinichev9f9b8732010-05-20 19:33:44 -070042 // Larger meshes make this test too slow for devices that do 1 mtri/sec.
Ilja Friedelc04bdd42013-01-30 18:55:49 -080043 // Also note that GLES 2.0 uses 16 bit indices.
44 GLint width = 128;
45 GLint height = 128;
Alexey Marinichev9f9b8732010-05-20 19:33:44 -070046
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 Marinichev2de76ab2010-07-12 18:17:42 -070053
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 Marinichev9f9b8732010-05-20 19:33:44 -070061
Stuart Abercrombiec33878b2012-07-25 13:43:27 -070062 GLushort *indices = NULL;
Alexey Marinichev9f9b8732010-05-20 19:33:44 -070063 GLuint index_buffer = 0;
64 GLsizeiptr index_buffer_size = 0;
65
66 {
Ilja Friedelc04bdd42013-01-30 18:55:49 -080067 // 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 Marinichev9f9b8732010-05-20 19:33:44 -070070 count_ = CreateMesh(&indices, &index_buffer_size, width, height, 0);
71
72 index_buffer = SetupVBO(GL_ELEMENT_ARRAY_BUFFER,
73 index_buffer_size, indices);
Daniel Kurtzaace01b2014-06-17 21:05:24 +080074 RunTest(this, "triangle_setup", count_ / 3, g_width, g_height, true);
Alexey Marinichev9f9b8732010-05-20 19:33:44 -070075 glEnable(GL_CULL_FACE);
Daniel Kurtzaace01b2014-06-17 21:05:24 +080076 RunTest(this, "triangle_setup_all_culled", count_ / 3, g_width, g_height, true);
Alexey Marinichev9f9b8732010-05-20 19:33:44 -070077 glDisable(GL_CULL_FACE);
78
79 glDeleteBuffers(1, &index_buffer);
80 delete[] indices;
81 }
82
83 {
Ilja Friedelc04bdd42013-01-30 18:55:49 -080084 // Use blue-ish color for drawing quad with many holes.
85 const GLfloat cyan[4] = {0.0f, 0.5f, 0.5f, 1.0f};
Alexey Marinichev2de76ab2010-07-12 18:17:42 -070086 glUniform4fv(color_uniform, 1, cyan);
Alexey Marinichev9f9b8732010-05-20 19:33:44 -070087 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 Kurtzaace01b2014-06-17 21:05:24 +080093 RunTest(this, "triangle_setup_half_culled", count_ / 3, g_width, g_height, true);
Alexey Marinichev9f9b8732010-05-20 19:33:44 -070094
95 glDeleteBuffers(1, &index_buffer);
96 delete[] indices;
97 }
98
Alexey Marinichev2de76ab2010-07-12 18:17:42 -070099 glDeleteProgram(program);
Alexey Marinichev9f9b8732010-05-20 19:33:44 -0700100 glDeleteBuffers(1, &vertex_buffer);
101 delete[] vertices;
102 return true;
103}
104
105
106TestBase* GetTriangleSetupTest() {
107 return new TriangleSetupTest;
108}
109
110
111} // namespace glbench