blob: 08c19bd0680fa3faa0346c3dc5112ee6b3b22246 [file] [log] [blame]
Alexey Marinichev592b8622010-02-04 15:20:10 -08001// 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
Alexey Marinichev6294a392010-03-02 16:53:36 -08005#include <fcntl.h>
Alexey Marinichev592b8622010-02-04 15:20:10 -08006#include <stdio.h>
7#include <stdlib.h>
Alexey Marinichev19b5a5d2010-02-18 11:37:26 -08008#include <string.h>
Alexey Marinichev6294a392010-03-02 16:53:36 -08009#include <sys/mman.h>
Alexey Marinichev592b8622010-02-04 15:20:10 -080010
Alexey Marinichevaf0b26d2010-02-24 13:03:13 -080011#include "base/logging.h"
12
Alexey Marinichev592b8622010-02-04 15:20:10 -080013#include "main.h"
Alexey Marinichev96e28692010-02-18 16:59:59 -080014#include "shaders.h"
Alexey Marinichev3c199732010-03-11 10:33:35 -080015#include "utils.h"
Alexey Marinichev6294a392010-03-02 16:53:36 -080016#include "yuv2rgb.h"
Alexey Marinichev592b8622010-02-04 15:20:10 -080017
18
Darin Petkovdd314962010-02-18 16:21:29 -080019const int enabled_tests_max = 8;
Alexey Marinichev19b5a5d2010-02-18 11:37:26 -080020const char *enabled_tests[enabled_tests_max+1] = {NULL};
21int seconds_to_run = 0;
22
Alexey Marinicheva991f2b2010-03-11 09:59:51 -080023// Runs Bench on a test function f and prints out results. Bench function
24// returns the slope and bias of a linear model relating the argument passed to
25// f and time it took f to run. Normally the argument of f is assumed to be
26// number of iterations an operation is executed.
27//
28// coefficient is multiplied (if inverse is false) or divided (if inverse is
29// true) by the slope and the result is printed.
30//
31// The test will not run if enabled_tests is nonempty and no string in
32// enabled_tests is contained in name.
33//
34// Examples:
35// coefficient = width * height (measured in pixels), inverse = true
36// returns the throughput in megapixels per second;
37//
38// coefficient = 1, inverse = false
39// returns number of operations per second.
Alexey Marinichev19b5a5d2010-02-18 11:37:26 -080040void RunTest(BenchFunc f, const char *name, float coefficient, bool inverse) {
Alexey Marinichev592b8622010-02-04 15:20:10 -080041 float slope;
42 int64_t bias;
Alexey Marinichev19b5a5d2010-02-18 11:37:26 -080043
44 if (enabled_tests[0]) {
45 bool found = false;
46 for (const char **e = enabled_tests; *e; e++)
47 if (strstr(name, *e)) {
48 found = true;
49 break;
50 }
51 if (!found)
52 return;
53 }
54
Alexey Marinichevc24aa232010-02-24 18:15:54 -080055 GLenum err = glGetError();
56 if (err != 0) {
57 printf("# %s failed, glGetError returned 0x%x.\n", name, err);
58 // float() in python will happily parse Nan.
59 printf("%s: Nan\n", name);
60 } else {
Alexey Marinichev8919b6e2010-03-04 16:21:02 -080061 if (Bench(f, &slope, &bias)) {
Alexey Marinicheve5b107a2010-03-10 17:51:04 -080062 printf("%s: %g\n", name, coefficient * (inverse ? 1.f / slope : slope));
Alexey Marinichev8919b6e2010-03-04 16:21:02 -080063 } else {
64 printf("# %s is too slow, returning zero.\n", name);
65 printf("%s: 0\n", name);
66 }
Alexey Marinichevc24aa232010-02-24 18:15:54 -080067 }
Alexey Marinichev592b8622010-02-04 15:20:10 -080068}
69
70static int arg1 = 0;
71static void *arg2 = NULL;
72
73
74void SwapTestFunc(int iter) {
75 for (int i = 0 ; i < iter; ++i) {
76 SwapBuffers();
77 }
78}
79
80void SwapTest() {
Alexey Marinicheve5b107a2010-03-10 17:51:04 -080081 RunTest(SwapTestFunc, "us_swap_swap", 1.f, false);
Alexey Marinichev592b8622010-02-04 15:20:10 -080082}
83
84
85void ClearTestFunc(int iter) {
86 GLbitfield mask = arg1;
87 glClear(mask);
88 glFlush(); // Kick GPU as soon as possible
89 for (int i = 0 ; i < iter-1; ++i) {
90 glClear(mask);
91 }
92}
93
94
95void ClearTest() {
96 arg1 = GL_COLOR_BUFFER_BIT;
Alexey Marinichev19b5a5d2010-02-18 11:37:26 -080097 RunTest(ClearTestFunc, "mpixels_sec_clear_color", g_width * g_height, true);
Alexey Marinichev592b8622010-02-04 15:20:10 -080098
99 arg1 = GL_DEPTH_BUFFER_BIT;
Alexey Marinichev19b5a5d2010-02-18 11:37:26 -0800100 RunTest(ClearTestFunc, "mpixels_sec_clear_depth", g_width * g_height, true);
Alexey Marinichev592b8622010-02-04 15:20:10 -0800101
102 arg1 = GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT;
Alexey Marinichev19b5a5d2010-02-18 11:37:26 -0800103 RunTest(ClearTestFunc, "mpixels_sec_clear_colordepth",
104 g_width * g_height, true);
Alexey Marinichev592b8622010-02-04 15:20:10 -0800105
106 arg1 = GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT;
Alexey Marinichev19b5a5d2010-02-18 11:37:26 -0800107 RunTest(ClearTestFunc, "mpixels_sec_clear_depthstencil",
108 g_width * g_height, true);
Alexey Marinichev592b8622010-02-04 15:20:10 -0800109
110 arg1 = GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT;
Alexey Marinichev19b5a5d2010-02-18 11:37:26 -0800111 RunTest(ClearTestFunc, "mpixels_sec_clear_colordepthstencil",
112 g_width * g_height, true);
Alexey Marinichev592b8622010-02-04 15:20:10 -0800113}
114
115
116GLuint SetupVBO(GLenum target, GLsizeiptr size, const GLvoid *data) {
117 GLuint buf = ~0;
118 glGenBuffers(1, &buf);
119 glBindBuffer(target, buf);
120 glBufferData(target, size, data, GL_STATIC_DRAW);
121
122 return glGetError() == 0 ? buf : 0;
123}
124
125
Alexey Marinichevd4a0f072010-02-09 17:50:12 -0800126GLuint SetupTexture(GLsizei size_log2) {
127 GLsizei size = 1 << size_log2;
Alexey Marinichev592b8622010-02-04 15:20:10 -0800128 GLuint name = ~0;
129 glGenTextures(1, &name);
130 glBindTexture(GL_TEXTURE_2D, name);
131 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
132 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
133
Alexey Marinichevd4a0f072010-02-09 17:50:12 -0800134 unsigned char *pixels = new unsigned char[size * size * 4];
Alexey Marinichev592b8622010-02-04 15:20:10 -0800135 if (!pixels)
136 return 0;
137
Alexey Marinichevd4a0f072010-02-09 17:50:12 -0800138 for (GLint level = 0; size > 0; level++, size /= 2) {
139 unsigned char *p = pixels;
140 for (int i = 0; i < size; i++) {
141 for (int j = 0; j < size; j++) {
142 *p++ = level %3 != 0 ? (i ^ j) << level : 0;
143 *p++ = level %3 != 1 ? (i ^ j) << level : 0;
144 *p++ = level %3 != 2 ? (i ^ j) << level : 0;
145 *p++ = 255;
146 }
Alexey Marinichev592b8622010-02-04 15:20:10 -0800147 }
Alexey Marinichevd4a0f072010-02-09 17:50:12 -0800148 if (size == 1) {
149 unsigned char *p = pixels;
150 *p++ = 255;
151 *p++ = 255;
152 *p++ = 255;
153 *p++ = 255;
154 }
155 glTexImage2D(GL_TEXTURE_2D, level, GL_RGBA, size, size, 0,
156 GL_RGBA, GL_UNSIGNED_BYTE, pixels);
Alexey Marinichev592b8622010-02-04 15:20:10 -0800157 }
Alexey Marinichev592b8622010-02-04 15:20:10 -0800158 delete[] pixels;
Alexey Marinichev592b8622010-02-04 15:20:10 -0800159 return name;
160}
161
162
Alexey Marinichev96e28692010-02-18 16:59:59 -0800163static void DrawArraysTestFunc(int iter);
Alexey Marinichevd4a0f072010-02-09 17:50:12 -0800164static void FillRateTestNormal(const char *name, float coeff=1.f);
165static void FillRateTestBlendDepth(const char *name);
Alexey Marinichev592b8622010-02-04 15:20:10 -0800166
167void FillRateTest() {
168 glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
169 glDisable(GL_DEPTH_TEST);
170 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
171
Alexey Marinichevd4a0f072010-02-09 17:50:12 -0800172 GLfloat buffer_vertex[8] = {
Alexey Marinichev592b8622010-02-04 15:20:10 -0800173 -1.f, -1.f,
174 1.f, -1.f,
175 -1.f, 1.f,
176 1.f, 1.f,
177 };
Alexey Marinichevd4a0f072010-02-09 17:50:12 -0800178 GLfloat buffer_texture[8] = {
179 0.f, 0.f,
180 1.f, 0.f,
181 0.f, 1.f,
182 1.f, 1.f,
183 };
Alexey Marinichev592b8622010-02-04 15:20:10 -0800184 glEnableClientState(GL_VERTEX_ARRAY);
185
Alexey Marinichevd4a0f072010-02-09 17:50:12 -0800186 GLuint vbo_vertex = SetupVBO(GL_ARRAY_BUFFER,
187 sizeof(buffer_vertex), buffer_vertex);
Alexey Marinichev19b5a5d2010-02-18 11:37:26 -0800188 if (!vbo_vertex)
189 printf("# Not Using VBO!\n");
Alexey Marinichevd4a0f072010-02-09 17:50:12 -0800190 glVertexPointer(2, GL_FLOAT, 0, vbo_vertex ? 0 : buffer_vertex);
191
192 GLuint vbo_texture = SetupVBO(GL_ARRAY_BUFFER,
193 sizeof(buffer_texture), buffer_texture);
194 glTexCoordPointer(2, GL_FLOAT, 0, vbo_texture ? 0 : buffer_texture);
Alexey Marinichev592b8622010-02-04 15:20:10 -0800195
196 glColor4f(1.f, 0.f, 0.f, 1.f);
Alexey Marinichevd4a0f072010-02-09 17:50:12 -0800197 FillRateTestNormal("fill_solid");
198 FillRateTestBlendDepth("fill_solid");
Alexey Marinichev592b8622010-02-04 15:20:10 -0800199
200 glColor4f(1.f, 1.f, 1.f, 1.f);
201 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
202 glEnable(GL_TEXTURE_2D);
203
Alexey Marinichevd4a0f072010-02-09 17:50:12 -0800204 GLuint texture = SetupTexture(9);
205 FillRateTestNormal("fill_tex_nearest");
Alexey Marinichev592b8622010-02-04 15:20:10 -0800206
207 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
208 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Alexey Marinichevd4a0f072010-02-09 17:50:12 -0800209 FillRateTestNormal("fill_tex_bilinear");
Alexey Marinichev592b8622010-02-04 15:20:10 -0800210
Alexey Marinichevd4a0f072010-02-09 17:50:12 -0800211 // lod = 0.5
212 glScalef(0.7071f, 0.7071f, 1.f);
213 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
214 GL_LINEAR_MIPMAP_NEAREST);
215 FillRateTestNormal("fill_tex_trilinear_nearest_05", 0.7071f * 0.7071f);
216
217 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
218 GL_LINEAR_MIPMAP_LINEAR);
219 FillRateTestNormal("fill_tex_trilinear_linear_05", 0.7071f * 0.7071f);
220
221 // lod = 0.4
222 glLoadIdentity();
223 glScalef(0.758f, 0.758f, 1.f);
224 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
225 GL_LINEAR_MIPMAP_LINEAR);
226 FillRateTestNormal("fill_tex_trilinear_linear_04", 0.758f * 0.758f);
227
228 // lod = 0.1
229 glLoadIdentity();
230 glScalef(0.933f, 0.933f, 1.f);
231 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
232 GL_LINEAR_MIPMAP_LINEAR);
233 FillRateTestNormal("fill_tex_trilinear_linear_01", 0.933f * 0.933f);
234
235 glDeleteBuffers(1, &vbo_vertex);
236 glDeleteBuffers(1, &vbo_texture);
Alexey Marinichev592b8622010-02-04 15:20:10 -0800237 glDeleteTextures(1, &texture);
238}
239
240
Alexey Marinichevd4a0f072010-02-09 17:50:12 -0800241static void FillRateTestNormal(const char *name, float coeff) {
Alexey Marinichev19b5a5d2010-02-18 11:37:26 -0800242 const int buffer_len = 64;
243 char buffer[buffer_len];
244 snprintf(buffer, buffer_len, "mpixels_sec_%s", name);
Alexey Marinichev96e28692010-02-18 16:59:59 -0800245 RunTest(DrawArraysTestFunc, buffer, coeff * g_width * g_height, true);
Alexey Marinichevd4a0f072010-02-09 17:50:12 -0800246}
Alexey Marinichev592b8622010-02-04 15:20:10 -0800247
Alexey Marinichevd4a0f072010-02-09 17:50:12 -0800248static void FillRateTestBlendDepth(const char *name) {
Alexey Marinichev19b5a5d2010-02-18 11:37:26 -0800249 const int buffer_len = 64;
250 char buffer[buffer_len];
251
Alexey Marinichev6d014842010-02-10 19:21:39 -0800252 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
253 glEnable(GL_BLEND);
Alexey Marinichev19b5a5d2010-02-18 11:37:26 -0800254 snprintf(buffer, buffer_len, "mpixels_sec_%s_blended", name);
Alexey Marinichev96e28692010-02-18 16:59:59 -0800255 RunTest(DrawArraysTestFunc, buffer, g_width * g_height, true);
Alexey Marinichev6d014842010-02-10 19:21:39 -0800256 glDisable(GL_BLEND);
Alexey Marinichev592b8622010-02-04 15:20:10 -0800257
Alexey Marinichev6d014842010-02-10 19:21:39 -0800258 glEnable(GL_DEPTH_TEST);
259 glDepthFunc(GL_NOTEQUAL);
Alexey Marinichev19b5a5d2010-02-18 11:37:26 -0800260 snprintf(buffer, buffer_len, "mpixels_sec_%s_depth_neq", name);
Alexey Marinichev96e28692010-02-18 16:59:59 -0800261 RunTest(DrawArraysTestFunc, buffer, g_width * g_height, true);
Alexey Marinichev6d014842010-02-10 19:21:39 -0800262 glDepthFunc(GL_NEVER);
Alexey Marinichev19b5a5d2010-02-18 11:37:26 -0800263 snprintf(buffer, buffer_len, "mpixels_sec_%s_depth_never", name);
Alexey Marinichev96e28692010-02-18 16:59:59 -0800264 RunTest(DrawArraysTestFunc, buffer, g_width * g_height, true);
Alexey Marinichev6d014842010-02-10 19:21:39 -0800265 glDisable(GL_DEPTH_TEST);
Alexey Marinichev592b8622010-02-04 15:20:10 -0800266}
267
268
Alexey Marinichev96e28692010-02-18 16:59:59 -0800269static void DrawArraysTestFunc(int iter) {
Alexey Marinichev592b8622010-02-04 15:20:10 -0800270 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
271 glFlush();
272 for (int i = 0; i < iter-1; ++i) {
273 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
274 }
275}
276
277
Alexey Marinichev96e28692010-02-18 16:59:59 -0800278static void DrawElementsTestFunc(int iter) {
Alexey Marinichev592b8622010-02-04 15:20:10 -0800279 glDrawElements(GL_TRIANGLES, arg1, GL_UNSIGNED_INT, arg2);
280 glFlush();
281 for (int i = 0 ; i < iter-1; ++i) {
282 glDrawElements(GL_TRIANGLES, arg1, GL_UNSIGNED_INT, arg2);
283 }
284}
285
Alexey Marinichev6d014842010-02-10 19:21:39 -0800286// Generates a tautological lattice.
Alexey Marinichevbf88d1f2010-02-12 12:49:16 -0800287static void CreateLattice(GLfloat **vertices, GLsizeiptr *size,
288 GLfloat size_x, GLfloat size_y,
Alexey Marinichev6d014842010-02-10 19:21:39 -0800289 int width, int height)
290{
Alexey Marinichevbf88d1f2010-02-12 12:49:16 -0800291 GLfloat *vptr = *vertices = new GLfloat[2 * (width + 1) * (height + 1)];
Alexey Marinichev6d014842010-02-10 19:21:39 -0800292 for (int j = 0; j <= height; j++) {
293 for (int i = 0; i <= width; i++) {
Alexey Marinichevbf88d1f2010-02-12 12:49:16 -0800294 *vptr++ = i * size_x;
295 *vptr++ = j * size_y;
Alexey Marinichev6d014842010-02-10 19:21:39 -0800296 }
297 }
Alexey Marinichevbf88d1f2010-02-12 12:49:16 -0800298 *size = (vptr - *vertices) * sizeof(GLfloat);
Alexey Marinichev6d014842010-02-10 19:21:39 -0800299}
300
Alexey Marinichev592b8622010-02-04 15:20:10 -0800301// Generates a mesh of 2*width*height triangles. The ratio of front facing to
302// back facing triangles is culled_ratio/RAND_MAX. Returns the number of
303// vertices in the mesh.
Alexey Marinichev6d014842010-02-10 19:21:39 -0800304static int CreateMesh(GLuint **indices, GLsizeiptr *size,
Alexey Marinichev592b8622010-02-04 15:20:10 -0800305 int width, int height, int culled_ratio) {
306 srand(0);
307
Alexey Marinichev6d014842010-02-10 19:21:39 -0800308 GLuint *iptr = *indices = new GLuint[2 * 3 * (width * height)];
Alexey Marinichev592b8622010-02-04 15:20:10 -0800309 const int swath_height = 4;
Alexey Marinichevaf0b26d2010-02-24 13:03:13 -0800310
311 CHECK(width % swath_height == 0 && height % swath_height == 0);
312
Alexey Marinichevbf88d1f2010-02-12 12:49:16 -0800313 for (int j = 0; j < height; j += swath_height) {
Alexey Marinichev592b8622010-02-04 15:20:10 -0800314 for (int i = 0; i < width; i++) {
315 for (int j2 = 0; j2 < swath_height; j2++) {
Alexey Marinichev6d014842010-02-10 19:21:39 -0800316 GLuint first = (j + j2) * (width + 1) + i;
Alexey Marinichev592b8622010-02-04 15:20:10 -0800317 GLuint second = first + 1;
Alexey Marinichev6d014842010-02-10 19:21:39 -0800318 GLuint third = first + (width + 1);
Alexey Marinichev592b8622010-02-04 15:20:10 -0800319 GLuint fourth = third + 1;
320
321 bool flag = rand() < culled_ratio;
322 *iptr++ = first;
323 *iptr++ = flag ? second : third;
324 *iptr++ = flag ? third : second;
325
326 *iptr++ = fourth;
327 *iptr++ = flag ? third : second;
328 *iptr++ = flag ? second : third;
329 }
330 }
331 }
Alexey Marinichev6d014842010-02-10 19:21:39 -0800332 *size = (iptr - *indices) * sizeof(GLuint);
Alexey Marinichev592b8622010-02-04 15:20:10 -0800333
Alexey Marinichev6d014842010-02-10 19:21:39 -0800334 return iptr - *indices;
Alexey Marinichev592b8622010-02-04 15:20:10 -0800335}
336
337void TriangleSetupTest() {
338 glViewport(-g_width, -g_height, g_width*2, g_height*2);
Alexey Marinichev592b8622010-02-04 15:20:10 -0800339
340 // Larger meshes make this test too slow for devices that do 1 mtri/sec.
341 GLint width = 64;
342 GLint height = 64;
343
Alexey Marinichevbf88d1f2010-02-12 12:49:16 -0800344 GLfloat *vertices = NULL;
Alexey Marinichev6d014842010-02-10 19:21:39 -0800345 GLsizeiptr vertex_buffer_size = 0;
Alexey Marinichevbf88d1f2010-02-12 12:49:16 -0800346 CreateLattice(&vertices, &vertex_buffer_size, 1.f / g_width, 1.f / g_height,
347 width, height);
Alexey Marinichev6d014842010-02-10 19:21:39 -0800348 GLuint vertex_buffer = SetupVBO(GL_ARRAY_BUFFER,
349 vertex_buffer_size, vertices);
Alexey Marinichevbf88d1f2010-02-12 12:49:16 -0800350 glVertexPointer(2, GL_FLOAT, 0, vertex_buffer != 0 ? 0 : vertices);
Alexey Marinichev96e28692010-02-18 16:59:59 -0800351 glEnableClientState(GL_VERTEX_ARRAY);
Alexey Marinichev6d014842010-02-10 19:21:39 -0800352
353 GLuint *indices = NULL;
354 GLuint index_buffer = 0;
355 GLsizeiptr index_buffer_size = 0;
356
Alexey Marinichev6d014842010-02-10 19:21:39 -0800357 {
358 arg1 = CreateMesh(&indices, &index_buffer_size, width, height, 0);
359
Alexey Marinichev6d014842010-02-10 19:21:39 -0800360 index_buffer = SetupVBO(GL_ELEMENT_ARRAY_BUFFER,
361 index_buffer_size, indices);
362 arg2 = index_buffer ? 0 : indices;
363
Alexey Marinichev96e28692010-02-18 16:59:59 -0800364 RunTest(DrawElementsTestFunc, "mtri_sec_triangle_setup", arg1 / 3, true);
Alexey Marinichev6d014842010-02-10 19:21:39 -0800365 glEnable(GL_CULL_FACE);
Alexey Marinichev96e28692010-02-18 16:59:59 -0800366 RunTest(DrawElementsTestFunc, "mtri_sec_triangle_setup_all_culled",
Alexey Marinichev19b5a5d2010-02-18 11:37:26 -0800367 arg1 / 3, true);
Alexey Marinichev96e28692010-02-18 16:59:59 -0800368 glDisable(GL_CULL_FACE);
Alexey Marinichev6d014842010-02-10 19:21:39 -0800369
Alexey Marinichev6d014842010-02-10 19:21:39 -0800370 glDeleteBuffers(1, &index_buffer);
Alexey Marinichev96e28692010-02-18 16:59:59 -0800371 delete[] indices;
Alexey Marinichev592b8622010-02-04 15:20:10 -0800372 }
373
Alexey Marinichev6d014842010-02-10 19:21:39 -0800374 {
375 glColor4f(0.f, 1.f, 1.f, 1.f);
376 arg1 = CreateMesh(&indices, &index_buffer_size, width, height,
377 RAND_MAX / 2);
Alexey Marinichev592b8622010-02-04 15:20:10 -0800378
Alexey Marinichev6d014842010-02-10 19:21:39 -0800379 index_buffer = SetupVBO(GL_ELEMENT_ARRAY_BUFFER,
380 index_buffer_size, indices);
381 arg2 = index_buffer ? 0 : indices;
Alexey Marinichev592b8622010-02-04 15:20:10 -0800382
Alexey Marinichev96e28692010-02-18 16:59:59 -0800383 glEnable(GL_CULL_FACE);
384 RunTest(DrawElementsTestFunc, "mtri_sec_triangle_setup_half_culled",
Alexey Marinichev19b5a5d2010-02-18 11:37:26 -0800385 arg1 / 3, true);
Alexey Marinichev592b8622010-02-04 15:20:10 -0800386
Alexey Marinichev6d014842010-02-10 19:21:39 -0800387 glDeleteBuffers(1, &index_buffer);
Alexey Marinichev96e28692010-02-18 16:59:59 -0800388 delete[] indices;
Alexey Marinichev6d014842010-02-10 19:21:39 -0800389 }
Alexey Marinichev592b8622010-02-04 15:20:10 -0800390
Alexey Marinichev6d014842010-02-10 19:21:39 -0800391 glDeleteBuffers(1, &vertex_buffer);
Alexey Marinichev592b8622010-02-04 15:20:10 -0800392 delete[] vertices;
Alexey Marinichev592b8622010-02-04 15:20:10 -0800393}
394
395
Alexey Marinichevaf0b26d2010-02-24 13:03:13 -0800396void AttributeFetchShaderTest() {
Alexey Marinichev96e28692010-02-18 16:59:59 -0800397 GLint width = 64;
398 GLint height = 64;
399
400 glViewport(-g_width, -g_height, g_width*2, g_height*2);
401
402 GLfloat *vertices = NULL;
403 GLsizeiptr vertex_buffer_size = 0;
404 CreateLattice(&vertices, &vertex_buffer_size, 1.f / g_width, 1.f / g_height,
405 width, height);
406 GLuint vertex_buffer = SetupVBO(GL_ARRAY_BUFFER,
407 vertex_buffer_size, vertices);
408
409 GLuint *indices = NULL;
410 GLuint index_buffer = 0;
411 GLsizeiptr index_buffer_size = 0;
412
413 // Everything will be back-face culled.
414 arg1 = CreateMesh(&indices, &index_buffer_size, width, height, 0);
415 index_buffer = SetupVBO(GL_ELEMENT_ARRAY_BUFFER,
416 index_buffer_size, indices);
417
418 glEnable(GL_CULL_FACE);
419
420 GLuint vertex_buffers[8];
421 for (GLuint i = 0; i < sizeof(vertex_buffers)/sizeof(vertex_buffers[0]); i++)
422 vertex_buffers[i] = vertex_buffer;
423
424 ShaderProgram program = AttributeFetchShaderProgram(1, vertex_buffers);
425 RunTest(DrawElementsTestFunc,
426 "mvtx_sec_attribute_fetch_shader", arg1, true);
Alexey Marinichev3c199732010-03-11 10:33:35 -0800427 glDeleteProgram(program);
Alexey Marinichev96e28692010-02-18 16:59:59 -0800428
Alexey Marinichev9b818772010-02-23 12:07:48 -0800429 program = AttributeFetchShaderProgram(2, vertex_buffers);
Alexey Marinichev96e28692010-02-18 16:59:59 -0800430 RunTest(DrawElementsTestFunc,
431 "mvtx_sec_attribute_fetch_shader_2_attr", arg1, true);
Alexey Marinichev3c199732010-03-11 10:33:35 -0800432 glDeleteProgram(program);
Alexey Marinichev96e28692010-02-18 16:59:59 -0800433
Alexey Marinichev9b818772010-02-23 12:07:48 -0800434 program = AttributeFetchShaderProgram(4, vertex_buffers);
Alexey Marinichev96e28692010-02-18 16:59:59 -0800435 RunTest(DrawElementsTestFunc,
436 "mvtx_sec_attribute_fetch_shader_4_attr", arg1, true);
Alexey Marinichev3c199732010-03-11 10:33:35 -0800437 glDeleteProgram(program);
Alexey Marinichev96e28692010-02-18 16:59:59 -0800438
Alexey Marinichev9b818772010-02-23 12:07:48 -0800439 program = AttributeFetchShaderProgram(8, vertex_buffers);
Alexey Marinichev96e28692010-02-18 16:59:59 -0800440 RunTest(DrawElementsTestFunc,
441 "mvtx_sec_attribute_fetch_shader_8_attr", arg1, true);
Alexey Marinichev3c199732010-03-11 10:33:35 -0800442 glDeleteProgram(program);
Alexey Marinichev96e28692010-02-18 16:59:59 -0800443
444 glDeleteBuffers(1, &index_buffer);
445 delete[] indices;
446
447 glDeleteBuffers(1, &vertex_buffer);
448 delete[] vertices;
449}
450
Alexey Marinichevaf0b26d2010-02-24 13:03:13 -0800451
Alexey Marinichevc24aa232010-02-24 18:15:54 -0800452void VaryingsAndDdxyShaderTest() {
Alexey Marinichevaf0b26d2010-02-24 13:03:13 -0800453 glViewport(-g_width, -g_height, g_width*2, g_height*2);
454
455 const int c = 4;
456 GLfloat *vertices = NULL;
457 GLsizeiptr vertex_buffer_size = 0;
458 CreateLattice(&vertices, &vertex_buffer_size, 1.f / c, 1.f / c, c, c);
459 GLuint vertex_buffer = SetupVBO(GL_ARRAY_BUFFER,
460 vertex_buffer_size, vertices);
461
462 GLuint *indices = NULL;
463 GLuint index_buffer = 0;
464 GLsizeiptr index_buffer_size = 0;
465
466 arg1 = CreateMesh(&indices, &index_buffer_size, c, c, 0);
467 index_buffer = SetupVBO(GL_ELEMENT_ARRAY_BUFFER,
468 index_buffer_size, indices);
469
470 ShaderProgram program = VaryingsShaderProgram(1, vertex_buffer);
471 RunTest(DrawElementsTestFunc,
472 "mpixels_sec_varyings_shader_1", g_width * g_height, true);
Alexey Marinichev3c199732010-03-11 10:33:35 -0800473 glDeleteProgram(program);
Alexey Marinichevaf0b26d2010-02-24 13:03:13 -0800474
475 program = VaryingsShaderProgram(2, vertex_buffer);
476 RunTest(DrawElementsTestFunc,
477 "mpixels_sec_varyings_shader_2", g_width * g_height, true);
Alexey Marinichev3c199732010-03-11 10:33:35 -0800478 glDeleteProgram(program);
Alexey Marinichevaf0b26d2010-02-24 13:03:13 -0800479
480 program = VaryingsShaderProgram(4, vertex_buffer);
481 RunTest(DrawElementsTestFunc,
482 "mpixels_sec_varyings_shader_4", g_width * g_height, true);
Alexey Marinichev3c199732010-03-11 10:33:35 -0800483 glDeleteProgram(program);
Alexey Marinichevaf0b26d2010-02-24 13:03:13 -0800484
485 program = VaryingsShaderProgram(8, vertex_buffer);
486 RunTest(DrawElementsTestFunc,
487 "mpixels_sec_varyings_shader_8", g_width * g_height, true);
Alexey Marinichev3c199732010-03-11 10:33:35 -0800488 glDeleteProgram(program);
Alexey Marinichevaf0b26d2010-02-24 13:03:13 -0800489
Alexey Marinichevc24aa232010-02-24 18:15:54 -0800490 program = DdxDdyShaderProgram(true, vertex_buffer);
491 RunTest(DrawElementsTestFunc,
492 "mpixels_sec_ddx_shader", g_width * g_height, true);
Alexey Marinichev3c199732010-03-11 10:33:35 -0800493 glDeleteProgram(program);
Alexey Marinichevc24aa232010-02-24 18:15:54 -0800494
495 program = DdxDdyShaderProgram(false, vertex_buffer);
496 RunTest(DrawElementsTestFunc,
497 "mpixels_sec_ddy_shader", g_width * g_height, true);
Alexey Marinichev3c199732010-03-11 10:33:35 -0800498 glDeleteProgram(program);
Alexey Marinichevc24aa232010-02-24 18:15:54 -0800499
Alexey Marinichevaf0b26d2010-02-24 13:03:13 -0800500 glDeleteBuffers(1, &index_buffer);
501 delete[] indices;
502
503 glDeleteBuffers(1, &vertex_buffer);
504 delete[] vertices;
505}
506
Alexey Marinichev8919b6e2010-03-04 16:21:02 -0800507
508void YuvToRgbShaderTestHelper(int type, const char *name) {
Alexey Marinichev6294a392010-03-02 16:53:36 -0800509 size_t size = 0;
Alexey Marinichev6dfea312010-03-03 14:53:12 -0800510 GLuint texture[2];
Alexey Marinichev6294a392010-03-02 16:53:36 -0800511 ShaderProgram program = 0;
512 GLuint vertex_buffer = 0;
513 GLfloat vertices[8] = {
514 0.f, 0.f,
515 1.f, 0.f,
Alexey Marinicheve5b107a2010-03-10 17:51:04 -0800516 0.f, 1.f,
517 1.f, 1.f,
Alexey Marinichev6294a392010-03-02 16:53:36 -0800518 };
Alexey Marinichev6dfea312010-03-03 14:53:12 -0800519 char evenodd[2] = {0, 255};
Alexey Marinicheve5b107a2010-03-10 17:51:04 -0800520 const int pixel_height = YUV2RGB_HEIGHT * 2 / 3;
Alexey Marinichev6294a392010-03-02 16:53:36 -0800521
522 char *pixels = static_cast<char *>(MmapFile(YUV2RGB_NAME, &size));
523 if (!pixels) {
524 printf("# Could not open image file: %s\n", YUV2RGB_NAME);
525 goto done;
526 }
527 if (size != YUV2RGB_SIZE) {
Alexey Marinichev6dfea312010-03-03 14:53:12 -0800528 printf("# Image file of wrong size, got %d, expected %d\n",
529 static_cast<int>(size), YUV2RGB_SIZE);
Alexey Marinichev6294a392010-03-02 16:53:36 -0800530 goto done;
531 }
532
Alexey Marinichev6dfea312010-03-03 14:53:12 -0800533 glGenTextures(2, texture);
534 glActiveTexture(GL_TEXTURE0);
535 glBindTexture(GL_TEXTURE_2D, texture[0]);
Alexey Marinichev6294a392010-03-02 16:53:36 -0800536 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
537 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
538 glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, YUV2RGB_WIDTH, YUV2RGB_HEIGHT,
539 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, pixels);
540
Alexey Marinichev6dfea312010-03-03 14:53:12 -0800541 glActiveTexture(GL_TEXTURE1);
542 glBindTexture(GL_TEXTURE_2D, texture[1]);
543 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
544 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
545 glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, 2, 1,
546 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, evenodd);
547
Alexey Marinicheve5b107a2010-03-10 17:51:04 -0800548 glViewport(-YUV2RGB_WIDTH, -pixel_height, YUV2RGB_WIDTH*2, pixel_height * 2);
Alexey Marinichev6294a392010-03-02 16:53:36 -0800549 vertex_buffer = SetupVBO(GL_ARRAY_BUFFER, sizeof(vertices), vertices);
550
Alexey Marinichev8919b6e2010-03-04 16:21:02 -0800551 program = YuvToRgbShaderProgram(type, vertex_buffer,
Alexey Marinicheve5b107a2010-03-10 17:51:04 -0800552 YUV2RGB_WIDTH, pixel_height);
553
554 if (program) {
555 float coeff = 1.f *
556 (YUV2RGB_WIDTH < g_width ?
557 static_cast<float>(YUV2RGB_WIDTH) / g_width : 1.f) *
558 (pixel_height < g_height ?
559 static_cast<float>(pixel_height) / g_height : 1.f);
560 FillRateTestNormal(name, coeff);
561 } else {
Alexey Marinichev6dfea312010-03-03 14:53:12 -0800562 printf("# Could not set up YUV shader.\n");
Alexey Marinicheve5b107a2010-03-10 17:51:04 -0800563 }
Alexey Marinichev6294a392010-03-02 16:53:36 -0800564
565done:
Alexey Marinichev3c199732010-03-11 10:33:35 -0800566 glDeleteProgram(program);
Alexey Marinichev6dfea312010-03-03 14:53:12 -0800567 glDeleteTextures(2, texture);
568 glDeleteBuffers(1, &vertex_buffer);
Alexey Marinichev6294a392010-03-02 16:53:36 -0800569 munmap(pixels, size);
570}
571
Alexey Marinichev8919b6e2010-03-04 16:21:02 -0800572void YuvToRgbShaderTest1() {
573 YuvToRgbShaderTestHelper(1, "yuv_shader_1");
574}
575
576void YuvToRgbShaderTest2() {
577 YuvToRgbShaderTestHelper(2, "yuv_shader_2");
578}
579
Alexey Marinichev6294a392010-03-02 16:53:36 -0800580
Alexey Marinichev3c199732010-03-11 10:33:35 -0800581// TODO: get resources file from a command line option or something.
Alexey Marinichev19b5a5d2010-02-18 11:37:26 -0800582// TODO: use proper command line parsing library.
583static void ParseArgs(int argc, char *argv[]) {
584 const char **enabled_tests_ptr = enabled_tests;
585 bool test_name_arg = false;
586 bool duration_arg = false;
587 for (int i = 0; i < argc; i++) {
588 if (test_name_arg) {
589 test_name_arg = false;
590 *enabled_tests_ptr++ = argv[i];
591 if (enabled_tests_ptr - enabled_tests >= enabled_tests_max)
592 break;
593 } else if (duration_arg) {
594 duration_arg = false;
595 seconds_to_run = atoi(argv[i]);
596 } else if (strcmp("-t", argv[i]) == 0) {
597 test_name_arg = true;
598 } else if (strcmp("-d", argv[i]) == 0) {
599 duration_arg = true;
600 }
601 }
602 *enabled_tests_ptr++ = NULL;
603}
604
605
Alexey Marinichev592b8622010-02-04 15:20:10 -0800606int main(int argc, char *argv[]) {
Antoine Labourec7eb532010-03-12 11:01:34 -0800607 SetBasePathFromArgv0(argv[0], "src");
Alexey Marinichev19b5a5d2010-02-18 11:37:26 -0800608 ParseArgs(argc, argv);
Alexey Marinichev592b8622010-02-04 15:20:10 -0800609 if (!Init()) {
610 printf("# Failed to initialize.\n");
611 return 1;
612 }
613
614 void (*test[])() = {
615 SwapTest,
616 ClearTest,
617 FillRateTest,
618 TriangleSetupTest,
Alexey Marinichevaf0b26d2010-02-24 13:03:13 -0800619 AttributeFetchShaderTest,
Alexey Marinichevc24aa232010-02-24 18:15:54 -0800620 VaryingsAndDdxyShaderTest,
Alexey Marinichev8919b6e2010-03-04 16:21:02 -0800621 YuvToRgbShaderTest1,
622 YuvToRgbShaderTest2,
Alexey Marinichev592b8622010-02-04 15:20:10 -0800623 };
624
Alexey Marinichev19b5a5d2010-02-18 11:37:26 -0800625 uint64_t done = GetUTime() + 1000000ULL * seconds_to_run;
626 do {
627 for (unsigned int i = 0; i < sizeof(test) / sizeof(*test); i++) {
628 InitContext();
629 test[i]();
Alexey Marinichev9b818772010-02-23 12:07:48 -0800630 GLenum err = glGetError();
631 if (err != 0)
Alexey Marinichev8919b6e2010-03-04 16:21:02 -0800632 printf("# glGetError returned non-zero: 0x%x\n", err);
Alexey Marinichev19b5a5d2010-02-18 11:37:26 -0800633 DestroyContext();
634 }
635 } while (GetUTime() < done);
Alexey Marinichev592b8622010-02-04 15:20:10 -0800636
637 return 0;
638}