Alexey Marinichev | 592b862 | 2010-02-04 15:20:10 -0800 | [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 | |
Alexey Marinichev | 6294a39 | 2010-03-02 16:53:36 -0800 | [diff] [blame] | 5 | #include <fcntl.h> |
Alexey Marinichev | 592b862 | 2010-02-04 15:20:10 -0800 | [diff] [blame] | 6 | #include <stdio.h> |
| 7 | #include <stdlib.h> |
Alexey Marinichev | 19b5a5d | 2010-02-18 11:37:26 -0800 | [diff] [blame] | 8 | #include <string.h> |
Alexey Marinichev | 6294a39 | 2010-03-02 16:53:36 -0800 | [diff] [blame] | 9 | #include <sys/mman.h> |
Alexey Marinichev | 592b862 | 2010-02-04 15:20:10 -0800 | [diff] [blame] | 10 | |
Alexey Marinichev | af0b26d | 2010-02-24 13:03:13 -0800 | [diff] [blame] | 11 | #include "base/logging.h" |
| 12 | |
Alexey Marinichev | 592b862 | 2010-02-04 15:20:10 -0800 | [diff] [blame] | 13 | #include "main.h" |
Alexey Marinichev | 96e2869 | 2010-02-18 16:59:59 -0800 | [diff] [blame] | 14 | #include "shaders.h" |
Alexey Marinichev | 3c19973 | 2010-03-11 10:33:35 -0800 | [diff] [blame] | 15 | #include "utils.h" |
Alexey Marinichev | 6294a39 | 2010-03-02 16:53:36 -0800 | [diff] [blame] | 16 | #include "yuv2rgb.h" |
Alexey Marinichev | 592b862 | 2010-02-04 15:20:10 -0800 | [diff] [blame] | 17 | |
| 18 | |
Darin Petkov | dd31496 | 2010-02-18 16:21:29 -0800 | [diff] [blame] | 19 | const int enabled_tests_max = 8; |
Alexey Marinichev | 19b5a5d | 2010-02-18 11:37:26 -0800 | [diff] [blame] | 20 | const char *enabled_tests[enabled_tests_max+1] = {NULL}; |
| 21 | int seconds_to_run = 0; |
| 22 | |
Alexey Marinichev | a991f2b | 2010-03-11 09:59:51 -0800 | [diff] [blame] | 23 | // 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 Marinichev | 19b5a5d | 2010-02-18 11:37:26 -0800 | [diff] [blame] | 40 | void RunTest(BenchFunc f, const char *name, float coefficient, bool inverse) { |
Alexey Marinichev | 592b862 | 2010-02-04 15:20:10 -0800 | [diff] [blame] | 41 | float slope; |
| 42 | int64_t bias; |
Alexey Marinichev | 19b5a5d | 2010-02-18 11:37:26 -0800 | [diff] [blame] | 43 | |
| 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 Marinichev | c24aa23 | 2010-02-24 18:15:54 -0800 | [diff] [blame] | 55 | 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 Marinichev | 8919b6e | 2010-03-04 16:21:02 -0800 | [diff] [blame] | 61 | if (Bench(f, &slope, &bias)) { |
Alexey Marinichev | e5b107a | 2010-03-10 17:51:04 -0800 | [diff] [blame] | 62 | printf("%s: %g\n", name, coefficient * (inverse ? 1.f / slope : slope)); |
Alexey Marinichev | 8919b6e | 2010-03-04 16:21:02 -0800 | [diff] [blame] | 63 | } else { |
| 64 | printf("# %s is too slow, returning zero.\n", name); |
| 65 | printf("%s: 0\n", name); |
| 66 | } |
Alexey Marinichev | c24aa23 | 2010-02-24 18:15:54 -0800 | [diff] [blame] | 67 | } |
Alexey Marinichev | 592b862 | 2010-02-04 15:20:10 -0800 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | static int arg1 = 0; |
| 71 | static void *arg2 = NULL; |
| 72 | |
Alexey Marinichev | 592b862 | 2010-02-04 15:20:10 -0800 | [diff] [blame] | 73 | void SwapTestFunc(int iter) { |
| 74 | for (int i = 0 ; i < iter; ++i) { |
| 75 | SwapBuffers(); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | void SwapTest() { |
Alexey Marinichev | e5b107a | 2010-03-10 17:51:04 -0800 | [diff] [blame] | 80 | RunTest(SwapTestFunc, "us_swap_swap", 1.f, false); |
Alexey Marinichev | 592b862 | 2010-02-04 15:20:10 -0800 | [diff] [blame] | 81 | } |
| 82 | |
Alexey Marinichev | 592b862 | 2010-02-04 15:20:10 -0800 | [diff] [blame] | 83 | void ClearTestFunc(int iter) { |
| 84 | GLbitfield mask = arg1; |
| 85 | glClear(mask); |
| 86 | glFlush(); // Kick GPU as soon as possible |
| 87 | for (int i = 0 ; i < iter-1; ++i) { |
| 88 | glClear(mask); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | |
| 93 | void ClearTest() { |
| 94 | arg1 = GL_COLOR_BUFFER_BIT; |
Alexey Marinichev | 19b5a5d | 2010-02-18 11:37:26 -0800 | [diff] [blame] | 95 | RunTest(ClearTestFunc, "mpixels_sec_clear_color", g_width * g_height, true); |
Alexey Marinichev | 592b862 | 2010-02-04 15:20:10 -0800 | [diff] [blame] | 96 | |
| 97 | arg1 = GL_DEPTH_BUFFER_BIT; |
Alexey Marinichev | 19b5a5d | 2010-02-18 11:37:26 -0800 | [diff] [blame] | 98 | RunTest(ClearTestFunc, "mpixels_sec_clear_depth", g_width * g_height, true); |
Alexey Marinichev | 592b862 | 2010-02-04 15:20:10 -0800 | [diff] [blame] | 99 | |
| 100 | arg1 = GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT; |
Alexey Marinichev | 19b5a5d | 2010-02-18 11:37:26 -0800 | [diff] [blame] | 101 | RunTest(ClearTestFunc, "mpixels_sec_clear_colordepth", |
| 102 | g_width * g_height, true); |
Alexey Marinichev | 592b862 | 2010-02-04 15:20:10 -0800 | [diff] [blame] | 103 | |
| 104 | arg1 = GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT; |
Alexey Marinichev | 19b5a5d | 2010-02-18 11:37:26 -0800 | [diff] [blame] | 105 | RunTest(ClearTestFunc, "mpixels_sec_clear_depthstencil", |
| 106 | g_width * g_height, true); |
Alexey Marinichev | 592b862 | 2010-02-04 15:20:10 -0800 | [diff] [blame] | 107 | |
| 108 | arg1 = GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT; |
Alexey Marinichev | 19b5a5d | 2010-02-18 11:37:26 -0800 | [diff] [blame] | 109 | RunTest(ClearTestFunc, "mpixels_sec_clear_colordepthstencil", |
| 110 | g_width * g_height, true); |
Alexey Marinichev | 592b862 | 2010-02-04 15:20:10 -0800 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | |
| 114 | GLuint SetupVBO(GLenum target, GLsizeiptr size, const GLvoid *data) { |
| 115 | GLuint buf = ~0; |
| 116 | glGenBuffers(1, &buf); |
| 117 | glBindBuffer(target, buf); |
| 118 | glBufferData(target, size, data, GL_STATIC_DRAW); |
| 119 | |
| 120 | return glGetError() == 0 ? buf : 0; |
| 121 | } |
| 122 | |
| 123 | |
Alexey Marinichev | d4a0f07 | 2010-02-09 17:50:12 -0800 | [diff] [blame] | 124 | GLuint SetupTexture(GLsizei size_log2) { |
| 125 | GLsizei size = 1 << size_log2; |
Alexey Marinichev | 592b862 | 2010-02-04 15:20:10 -0800 | [diff] [blame] | 126 | GLuint name = ~0; |
| 127 | glGenTextures(1, &name); |
| 128 | glBindTexture(GL_TEXTURE_2D, name); |
| 129 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
| 130 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
| 131 | |
Alexey Marinichev | d4a0f07 | 2010-02-09 17:50:12 -0800 | [diff] [blame] | 132 | unsigned char *pixels = new unsigned char[size * size * 4]; |
Alexey Marinichev | 592b862 | 2010-02-04 15:20:10 -0800 | [diff] [blame] | 133 | if (!pixels) |
| 134 | return 0; |
| 135 | |
Alexey Marinichev | d4a0f07 | 2010-02-09 17:50:12 -0800 | [diff] [blame] | 136 | for (GLint level = 0; size > 0; level++, size /= 2) { |
| 137 | unsigned char *p = pixels; |
| 138 | for (int i = 0; i < size; i++) { |
| 139 | for (int j = 0; j < size; j++) { |
| 140 | *p++ = level %3 != 0 ? (i ^ j) << level : 0; |
| 141 | *p++ = level %3 != 1 ? (i ^ j) << level : 0; |
| 142 | *p++ = level %3 != 2 ? (i ^ j) << level : 0; |
| 143 | *p++ = 255; |
| 144 | } |
Alexey Marinichev | 592b862 | 2010-02-04 15:20:10 -0800 | [diff] [blame] | 145 | } |
Alexey Marinichev | d4a0f07 | 2010-02-09 17:50:12 -0800 | [diff] [blame] | 146 | if (size == 1) { |
| 147 | unsigned char *p = pixels; |
| 148 | *p++ = 255; |
| 149 | *p++ = 255; |
| 150 | *p++ = 255; |
| 151 | *p++ = 255; |
| 152 | } |
| 153 | glTexImage2D(GL_TEXTURE_2D, level, GL_RGBA, size, size, 0, |
| 154 | GL_RGBA, GL_UNSIGNED_BYTE, pixels); |
Alexey Marinichev | 592b862 | 2010-02-04 15:20:10 -0800 | [diff] [blame] | 155 | } |
Alexey Marinichev | 592b862 | 2010-02-04 15:20:10 -0800 | [diff] [blame] | 156 | delete[] pixels; |
Alexey Marinichev | 592b862 | 2010-02-04 15:20:10 -0800 | [diff] [blame] | 157 | return name; |
| 158 | } |
| 159 | |
| 160 | |
Alexey Marinichev | 96e2869 | 2010-02-18 16:59:59 -0800 | [diff] [blame] | 161 | static void DrawArraysTestFunc(int iter); |
Alexey Marinichev | d4a0f07 | 2010-02-09 17:50:12 -0800 | [diff] [blame] | 162 | static void FillRateTestNormal(const char *name, float coeff=1.f); |
| 163 | static void FillRateTestBlendDepth(const char *name); |
Alexey Marinichev | 592b862 | 2010-02-04 15:20:10 -0800 | [diff] [blame] | 164 | |
| 165 | void FillRateTest() { |
| 166 | glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT); |
| 167 | glDisable(GL_DEPTH_TEST); |
| 168 | glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); |
| 169 | |
Alexey Marinichev | d4a0f07 | 2010-02-09 17:50:12 -0800 | [diff] [blame] | 170 | GLfloat buffer_vertex[8] = { |
Alexey Marinichev | 592b862 | 2010-02-04 15:20:10 -0800 | [diff] [blame] | 171 | -1.f, -1.f, |
| 172 | 1.f, -1.f, |
| 173 | -1.f, 1.f, |
| 174 | 1.f, 1.f, |
| 175 | }; |
Alexey Marinichev | d4a0f07 | 2010-02-09 17:50:12 -0800 | [diff] [blame] | 176 | GLfloat buffer_texture[8] = { |
| 177 | 0.f, 0.f, |
| 178 | 1.f, 0.f, |
| 179 | 0.f, 1.f, |
| 180 | 1.f, 1.f, |
| 181 | }; |
Alexey Marinichev | 592b862 | 2010-02-04 15:20:10 -0800 | [diff] [blame] | 182 | glEnableClientState(GL_VERTEX_ARRAY); |
| 183 | |
Alexey Marinichev | d4a0f07 | 2010-02-09 17:50:12 -0800 | [diff] [blame] | 184 | GLuint vbo_vertex = SetupVBO(GL_ARRAY_BUFFER, |
| 185 | sizeof(buffer_vertex), buffer_vertex); |
Alexey Marinichev | 19b5a5d | 2010-02-18 11:37:26 -0800 | [diff] [blame] | 186 | if (!vbo_vertex) |
| 187 | printf("# Not Using VBO!\n"); |
Alexey Marinichev | d4a0f07 | 2010-02-09 17:50:12 -0800 | [diff] [blame] | 188 | glVertexPointer(2, GL_FLOAT, 0, vbo_vertex ? 0 : buffer_vertex); |
| 189 | |
| 190 | GLuint vbo_texture = SetupVBO(GL_ARRAY_BUFFER, |
| 191 | sizeof(buffer_texture), buffer_texture); |
| 192 | glTexCoordPointer(2, GL_FLOAT, 0, vbo_texture ? 0 : buffer_texture); |
Alexey Marinichev | 592b862 | 2010-02-04 15:20:10 -0800 | [diff] [blame] | 193 | |
| 194 | glColor4f(1.f, 0.f, 0.f, 1.f); |
Alexey Marinichev | d4a0f07 | 2010-02-09 17:50:12 -0800 | [diff] [blame] | 195 | FillRateTestNormal("fill_solid"); |
| 196 | FillRateTestBlendDepth("fill_solid"); |
Alexey Marinichev | 592b862 | 2010-02-04 15:20:10 -0800 | [diff] [blame] | 197 | |
| 198 | glColor4f(1.f, 1.f, 1.f, 1.f); |
| 199 | glEnableClientState(GL_TEXTURE_COORD_ARRAY); |
| 200 | glEnable(GL_TEXTURE_2D); |
| 201 | |
Alexey Marinichev | d4a0f07 | 2010-02-09 17:50:12 -0800 | [diff] [blame] | 202 | GLuint texture = SetupTexture(9); |
| 203 | FillRateTestNormal("fill_tex_nearest"); |
Alexey Marinichev | 592b862 | 2010-02-04 15:20:10 -0800 | [diff] [blame] | 204 | |
| 205 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 206 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
Alexey Marinichev | d4a0f07 | 2010-02-09 17:50:12 -0800 | [diff] [blame] | 207 | FillRateTestNormal("fill_tex_bilinear"); |
Alexey Marinichev | 592b862 | 2010-02-04 15:20:10 -0800 | [diff] [blame] | 208 | |
Alexey Marinichev | d4a0f07 | 2010-02-09 17:50:12 -0800 | [diff] [blame] | 209 | // lod = 0.5 |
| 210 | glScalef(0.7071f, 0.7071f, 1.f); |
| 211 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, |
| 212 | GL_LINEAR_MIPMAP_NEAREST); |
| 213 | FillRateTestNormal("fill_tex_trilinear_nearest_05", 0.7071f * 0.7071f); |
| 214 | |
| 215 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, |
| 216 | GL_LINEAR_MIPMAP_LINEAR); |
| 217 | FillRateTestNormal("fill_tex_trilinear_linear_05", 0.7071f * 0.7071f); |
| 218 | |
| 219 | // lod = 0.4 |
| 220 | glLoadIdentity(); |
| 221 | glScalef(0.758f, 0.758f, 1.f); |
| 222 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, |
| 223 | GL_LINEAR_MIPMAP_LINEAR); |
| 224 | FillRateTestNormal("fill_tex_trilinear_linear_04", 0.758f * 0.758f); |
| 225 | |
| 226 | // lod = 0.1 |
| 227 | glLoadIdentity(); |
| 228 | glScalef(0.933f, 0.933f, 1.f); |
| 229 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, |
| 230 | GL_LINEAR_MIPMAP_LINEAR); |
| 231 | FillRateTestNormal("fill_tex_trilinear_linear_01", 0.933f * 0.933f); |
| 232 | |
| 233 | glDeleteBuffers(1, &vbo_vertex); |
| 234 | glDeleteBuffers(1, &vbo_texture); |
Alexey Marinichev | 592b862 | 2010-02-04 15:20:10 -0800 | [diff] [blame] | 235 | glDeleteTextures(1, &texture); |
| 236 | } |
| 237 | |
| 238 | |
Alexey Marinichev | d4a0f07 | 2010-02-09 17:50:12 -0800 | [diff] [blame] | 239 | static void FillRateTestNormal(const char *name, float coeff) { |
Alexey Marinichev | 19b5a5d | 2010-02-18 11:37:26 -0800 | [diff] [blame] | 240 | const int buffer_len = 64; |
| 241 | char buffer[buffer_len]; |
| 242 | snprintf(buffer, buffer_len, "mpixels_sec_%s", name); |
Alexey Marinichev | 96e2869 | 2010-02-18 16:59:59 -0800 | [diff] [blame] | 243 | RunTest(DrawArraysTestFunc, buffer, coeff * g_width * g_height, true); |
Alexey Marinichev | d4a0f07 | 2010-02-09 17:50:12 -0800 | [diff] [blame] | 244 | } |
Alexey Marinichev | 592b862 | 2010-02-04 15:20:10 -0800 | [diff] [blame] | 245 | |
Alexey Marinichev | d4a0f07 | 2010-02-09 17:50:12 -0800 | [diff] [blame] | 246 | static void FillRateTestBlendDepth(const char *name) { |
Alexey Marinichev | 19b5a5d | 2010-02-18 11:37:26 -0800 | [diff] [blame] | 247 | const int buffer_len = 64; |
| 248 | char buffer[buffer_len]; |
| 249 | |
Alexey Marinichev | 6d01484 | 2010-02-10 19:21:39 -0800 | [diff] [blame] | 250 | glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); |
| 251 | glEnable(GL_BLEND); |
Alexey Marinichev | 19b5a5d | 2010-02-18 11:37:26 -0800 | [diff] [blame] | 252 | snprintf(buffer, buffer_len, "mpixels_sec_%s_blended", name); |
Alexey Marinichev | 96e2869 | 2010-02-18 16:59:59 -0800 | [diff] [blame] | 253 | RunTest(DrawArraysTestFunc, buffer, g_width * g_height, true); |
Alexey Marinichev | 6d01484 | 2010-02-10 19:21:39 -0800 | [diff] [blame] | 254 | glDisable(GL_BLEND); |
Alexey Marinichev | 592b862 | 2010-02-04 15:20:10 -0800 | [diff] [blame] | 255 | |
Alexey Marinichev | 6d01484 | 2010-02-10 19:21:39 -0800 | [diff] [blame] | 256 | glEnable(GL_DEPTH_TEST); |
| 257 | glDepthFunc(GL_NOTEQUAL); |
Alexey Marinichev | 19b5a5d | 2010-02-18 11:37:26 -0800 | [diff] [blame] | 258 | snprintf(buffer, buffer_len, "mpixels_sec_%s_depth_neq", name); |
Alexey Marinichev | 96e2869 | 2010-02-18 16:59:59 -0800 | [diff] [blame] | 259 | RunTest(DrawArraysTestFunc, buffer, g_width * g_height, true); |
Alexey Marinichev | 6d01484 | 2010-02-10 19:21:39 -0800 | [diff] [blame] | 260 | glDepthFunc(GL_NEVER); |
Alexey Marinichev | 19b5a5d | 2010-02-18 11:37:26 -0800 | [diff] [blame] | 261 | snprintf(buffer, buffer_len, "mpixels_sec_%s_depth_never", name); |
Alexey Marinichev | 96e2869 | 2010-02-18 16:59:59 -0800 | [diff] [blame] | 262 | RunTest(DrawArraysTestFunc, buffer, g_width * g_height, true); |
Alexey Marinichev | 6d01484 | 2010-02-10 19:21:39 -0800 | [diff] [blame] | 263 | glDisable(GL_DEPTH_TEST); |
Alexey Marinichev | 592b862 | 2010-02-04 15:20:10 -0800 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | |
Alexey Marinichev | 96e2869 | 2010-02-18 16:59:59 -0800 | [diff] [blame] | 267 | static void DrawArraysTestFunc(int iter) { |
Alexey Marinichev | 592b862 | 2010-02-04 15:20:10 -0800 | [diff] [blame] | 268 | glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); |
| 269 | glFlush(); |
| 270 | for (int i = 0; i < iter-1; ++i) { |
| 271 | glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | |
Alexey Marinichev | 96e2869 | 2010-02-18 16:59:59 -0800 | [diff] [blame] | 276 | static void DrawElementsTestFunc(int iter) { |
Alexey Marinichev | 592b862 | 2010-02-04 15:20:10 -0800 | [diff] [blame] | 277 | glDrawElements(GL_TRIANGLES, arg1, GL_UNSIGNED_INT, arg2); |
| 278 | glFlush(); |
| 279 | for (int i = 0 ; i < iter-1; ++i) { |
| 280 | glDrawElements(GL_TRIANGLES, arg1, GL_UNSIGNED_INT, arg2); |
| 281 | } |
| 282 | } |
| 283 | |
Alexey Marinichev | 6d01484 | 2010-02-10 19:21:39 -0800 | [diff] [blame] | 284 | // Generates a tautological lattice. |
Alexey Marinichev | bf88d1f | 2010-02-12 12:49:16 -0800 | [diff] [blame] | 285 | static void CreateLattice(GLfloat **vertices, GLsizeiptr *size, |
| 286 | GLfloat size_x, GLfloat size_y, |
Alexey Marinichev | 6d01484 | 2010-02-10 19:21:39 -0800 | [diff] [blame] | 287 | int width, int height) |
| 288 | { |
Alexey Marinichev | bf88d1f | 2010-02-12 12:49:16 -0800 | [diff] [blame] | 289 | GLfloat *vptr = *vertices = new GLfloat[2 * (width + 1) * (height + 1)]; |
Alexey Marinichev | 6d01484 | 2010-02-10 19:21:39 -0800 | [diff] [blame] | 290 | for (int j = 0; j <= height; j++) { |
| 291 | for (int i = 0; i <= width; i++) { |
Alexey Marinichev | bf88d1f | 2010-02-12 12:49:16 -0800 | [diff] [blame] | 292 | *vptr++ = i * size_x; |
| 293 | *vptr++ = j * size_y; |
Alexey Marinichev | 6d01484 | 2010-02-10 19:21:39 -0800 | [diff] [blame] | 294 | } |
| 295 | } |
Alexey Marinichev | bf88d1f | 2010-02-12 12:49:16 -0800 | [diff] [blame] | 296 | *size = (vptr - *vertices) * sizeof(GLfloat); |
Alexey Marinichev | 6d01484 | 2010-02-10 19:21:39 -0800 | [diff] [blame] | 297 | } |
| 298 | |
Alexey Marinichev | 592b862 | 2010-02-04 15:20:10 -0800 | [diff] [blame] | 299 | // Generates a mesh of 2*width*height triangles. The ratio of front facing to |
| 300 | // back facing triangles is culled_ratio/RAND_MAX. Returns the number of |
| 301 | // vertices in the mesh. |
Alexey Marinichev | 6d01484 | 2010-02-10 19:21:39 -0800 | [diff] [blame] | 302 | static int CreateMesh(GLuint **indices, GLsizeiptr *size, |
Alexey Marinichev | 592b862 | 2010-02-04 15:20:10 -0800 | [diff] [blame] | 303 | int width, int height, int culled_ratio) { |
| 304 | srand(0); |
| 305 | |
Alexey Marinichev | 6d01484 | 2010-02-10 19:21:39 -0800 | [diff] [blame] | 306 | GLuint *iptr = *indices = new GLuint[2 * 3 * (width * height)]; |
Alexey Marinichev | 592b862 | 2010-02-04 15:20:10 -0800 | [diff] [blame] | 307 | const int swath_height = 4; |
Alexey Marinichev | af0b26d | 2010-02-24 13:03:13 -0800 | [diff] [blame] | 308 | |
| 309 | CHECK(width % swath_height == 0 && height % swath_height == 0); |
| 310 | |
Alexey Marinichev | bf88d1f | 2010-02-12 12:49:16 -0800 | [diff] [blame] | 311 | for (int j = 0; j < height; j += swath_height) { |
Alexey Marinichev | 592b862 | 2010-02-04 15:20:10 -0800 | [diff] [blame] | 312 | for (int i = 0; i < width; i++) { |
| 313 | for (int j2 = 0; j2 < swath_height; j2++) { |
Alexey Marinichev | 6d01484 | 2010-02-10 19:21:39 -0800 | [diff] [blame] | 314 | GLuint first = (j + j2) * (width + 1) + i; |
Alexey Marinichev | 592b862 | 2010-02-04 15:20:10 -0800 | [diff] [blame] | 315 | GLuint second = first + 1; |
Alexey Marinichev | 6d01484 | 2010-02-10 19:21:39 -0800 | [diff] [blame] | 316 | GLuint third = first + (width + 1); |
Alexey Marinichev | 592b862 | 2010-02-04 15:20:10 -0800 | [diff] [blame] | 317 | GLuint fourth = third + 1; |
| 318 | |
| 319 | bool flag = rand() < culled_ratio; |
| 320 | *iptr++ = first; |
| 321 | *iptr++ = flag ? second : third; |
| 322 | *iptr++ = flag ? third : second; |
| 323 | |
| 324 | *iptr++ = fourth; |
| 325 | *iptr++ = flag ? third : second; |
| 326 | *iptr++ = flag ? second : third; |
| 327 | } |
| 328 | } |
| 329 | } |
Alexey Marinichev | 6d01484 | 2010-02-10 19:21:39 -0800 | [diff] [blame] | 330 | *size = (iptr - *indices) * sizeof(GLuint); |
Alexey Marinichev | 592b862 | 2010-02-04 15:20:10 -0800 | [diff] [blame] | 331 | |
Alexey Marinichev | 6d01484 | 2010-02-10 19:21:39 -0800 | [diff] [blame] | 332 | return iptr - *indices; |
Alexey Marinichev | 592b862 | 2010-02-04 15:20:10 -0800 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | void TriangleSetupTest() { |
| 336 | glViewport(-g_width, -g_height, g_width*2, g_height*2); |
Alexey Marinichev | 592b862 | 2010-02-04 15:20:10 -0800 | [diff] [blame] | 337 | |
| 338 | // Larger meshes make this test too slow for devices that do 1 mtri/sec. |
| 339 | GLint width = 64; |
| 340 | GLint height = 64; |
| 341 | |
Alexey Marinichev | bf88d1f | 2010-02-12 12:49:16 -0800 | [diff] [blame] | 342 | GLfloat *vertices = NULL; |
Alexey Marinichev | 6d01484 | 2010-02-10 19:21:39 -0800 | [diff] [blame] | 343 | GLsizeiptr vertex_buffer_size = 0; |
Alexey Marinichev | bf88d1f | 2010-02-12 12:49:16 -0800 | [diff] [blame] | 344 | CreateLattice(&vertices, &vertex_buffer_size, 1.f / g_width, 1.f / g_height, |
| 345 | width, height); |
Alexey Marinichev | 6d01484 | 2010-02-10 19:21:39 -0800 | [diff] [blame] | 346 | GLuint vertex_buffer = SetupVBO(GL_ARRAY_BUFFER, |
| 347 | vertex_buffer_size, vertices); |
Alexey Marinichev | bf88d1f | 2010-02-12 12:49:16 -0800 | [diff] [blame] | 348 | glVertexPointer(2, GL_FLOAT, 0, vertex_buffer != 0 ? 0 : vertices); |
Alexey Marinichev | 96e2869 | 2010-02-18 16:59:59 -0800 | [diff] [blame] | 349 | glEnableClientState(GL_VERTEX_ARRAY); |
Alexey Marinichev | 6d01484 | 2010-02-10 19:21:39 -0800 | [diff] [blame] | 350 | |
| 351 | GLuint *indices = NULL; |
| 352 | GLuint index_buffer = 0; |
| 353 | GLsizeiptr index_buffer_size = 0; |
| 354 | |
Alexey Marinichev | 6d01484 | 2010-02-10 19:21:39 -0800 | [diff] [blame] | 355 | { |
| 356 | arg1 = CreateMesh(&indices, &index_buffer_size, width, height, 0); |
| 357 | |
Alexey Marinichev | 6d01484 | 2010-02-10 19:21:39 -0800 | [diff] [blame] | 358 | index_buffer = SetupVBO(GL_ELEMENT_ARRAY_BUFFER, |
| 359 | index_buffer_size, indices); |
| 360 | arg2 = index_buffer ? 0 : indices; |
| 361 | |
Alexey Marinichev | 96e2869 | 2010-02-18 16:59:59 -0800 | [diff] [blame] | 362 | RunTest(DrawElementsTestFunc, "mtri_sec_triangle_setup", arg1 / 3, true); |
Alexey Marinichev | 6d01484 | 2010-02-10 19:21:39 -0800 | [diff] [blame] | 363 | glEnable(GL_CULL_FACE); |
Alexey Marinichev | 96e2869 | 2010-02-18 16:59:59 -0800 | [diff] [blame] | 364 | RunTest(DrawElementsTestFunc, "mtri_sec_triangle_setup_all_culled", |
Alexey Marinichev | 19b5a5d | 2010-02-18 11:37:26 -0800 | [diff] [blame] | 365 | arg1 / 3, true); |
Alexey Marinichev | 96e2869 | 2010-02-18 16:59:59 -0800 | [diff] [blame] | 366 | glDisable(GL_CULL_FACE); |
Alexey Marinichev | 6d01484 | 2010-02-10 19:21:39 -0800 | [diff] [blame] | 367 | |
Alexey Marinichev | 6d01484 | 2010-02-10 19:21:39 -0800 | [diff] [blame] | 368 | glDeleteBuffers(1, &index_buffer); |
Alexey Marinichev | 96e2869 | 2010-02-18 16:59:59 -0800 | [diff] [blame] | 369 | delete[] indices; |
Alexey Marinichev | 592b862 | 2010-02-04 15:20:10 -0800 | [diff] [blame] | 370 | } |
| 371 | |
Alexey Marinichev | 6d01484 | 2010-02-10 19:21:39 -0800 | [diff] [blame] | 372 | { |
| 373 | glColor4f(0.f, 1.f, 1.f, 1.f); |
| 374 | arg1 = CreateMesh(&indices, &index_buffer_size, width, height, |
| 375 | RAND_MAX / 2); |
Alexey Marinichev | 592b862 | 2010-02-04 15:20:10 -0800 | [diff] [blame] | 376 | |
Alexey Marinichev | 6d01484 | 2010-02-10 19:21:39 -0800 | [diff] [blame] | 377 | index_buffer = SetupVBO(GL_ELEMENT_ARRAY_BUFFER, |
| 378 | index_buffer_size, indices); |
| 379 | arg2 = index_buffer ? 0 : indices; |
Alexey Marinichev | 592b862 | 2010-02-04 15:20:10 -0800 | [diff] [blame] | 380 | |
Alexey Marinichev | 96e2869 | 2010-02-18 16:59:59 -0800 | [diff] [blame] | 381 | glEnable(GL_CULL_FACE); |
| 382 | RunTest(DrawElementsTestFunc, "mtri_sec_triangle_setup_half_culled", |
Alexey Marinichev | 19b5a5d | 2010-02-18 11:37:26 -0800 | [diff] [blame] | 383 | arg1 / 3, true); |
Alexey Marinichev | 592b862 | 2010-02-04 15:20:10 -0800 | [diff] [blame] | 384 | |
Alexey Marinichev | 6d01484 | 2010-02-10 19:21:39 -0800 | [diff] [blame] | 385 | glDeleteBuffers(1, &index_buffer); |
Alexey Marinichev | 96e2869 | 2010-02-18 16:59:59 -0800 | [diff] [blame] | 386 | delete[] indices; |
Alexey Marinichev | 6d01484 | 2010-02-10 19:21:39 -0800 | [diff] [blame] | 387 | } |
Alexey Marinichev | 592b862 | 2010-02-04 15:20:10 -0800 | [diff] [blame] | 388 | |
Alexey Marinichev | 6d01484 | 2010-02-10 19:21:39 -0800 | [diff] [blame] | 389 | glDeleteBuffers(1, &vertex_buffer); |
Alexey Marinichev | 592b862 | 2010-02-04 15:20:10 -0800 | [diff] [blame] | 390 | delete[] vertices; |
Alexey Marinichev | 592b862 | 2010-02-04 15:20:10 -0800 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | |
Alexey Marinichev | af0b26d | 2010-02-24 13:03:13 -0800 | [diff] [blame] | 394 | void AttributeFetchShaderTest() { |
Alexey Marinichev | 96e2869 | 2010-02-18 16:59:59 -0800 | [diff] [blame] | 395 | GLint width = 64; |
| 396 | GLint height = 64; |
| 397 | |
| 398 | glViewport(-g_width, -g_height, g_width*2, g_height*2); |
| 399 | |
| 400 | GLfloat *vertices = NULL; |
| 401 | GLsizeiptr vertex_buffer_size = 0; |
| 402 | CreateLattice(&vertices, &vertex_buffer_size, 1.f / g_width, 1.f / g_height, |
| 403 | width, height); |
| 404 | GLuint vertex_buffer = SetupVBO(GL_ARRAY_BUFFER, |
| 405 | vertex_buffer_size, vertices); |
| 406 | |
| 407 | GLuint *indices = NULL; |
| 408 | GLuint index_buffer = 0; |
| 409 | GLsizeiptr index_buffer_size = 0; |
| 410 | |
| 411 | // Everything will be back-face culled. |
| 412 | arg1 = CreateMesh(&indices, &index_buffer_size, width, height, 0); |
| 413 | index_buffer = SetupVBO(GL_ELEMENT_ARRAY_BUFFER, |
| 414 | index_buffer_size, indices); |
| 415 | |
| 416 | glEnable(GL_CULL_FACE); |
| 417 | |
| 418 | GLuint vertex_buffers[8]; |
| 419 | for (GLuint i = 0; i < sizeof(vertex_buffers)/sizeof(vertex_buffers[0]); i++) |
| 420 | vertex_buffers[i] = vertex_buffer; |
| 421 | |
| 422 | ShaderProgram program = AttributeFetchShaderProgram(1, vertex_buffers); |
| 423 | RunTest(DrawElementsTestFunc, |
| 424 | "mvtx_sec_attribute_fetch_shader", arg1, true); |
Alexey Marinichev | 3c19973 | 2010-03-11 10:33:35 -0800 | [diff] [blame] | 425 | glDeleteProgram(program); |
Alexey Marinichev | 96e2869 | 2010-02-18 16:59:59 -0800 | [diff] [blame] | 426 | |
Alexey Marinichev | 9b81877 | 2010-02-23 12:07:48 -0800 | [diff] [blame] | 427 | program = AttributeFetchShaderProgram(2, vertex_buffers); |
Alexey Marinichev | 96e2869 | 2010-02-18 16:59:59 -0800 | [diff] [blame] | 428 | RunTest(DrawElementsTestFunc, |
| 429 | "mvtx_sec_attribute_fetch_shader_2_attr", arg1, true); |
Alexey Marinichev | 3c19973 | 2010-03-11 10:33:35 -0800 | [diff] [blame] | 430 | glDeleteProgram(program); |
Alexey Marinichev | 96e2869 | 2010-02-18 16:59:59 -0800 | [diff] [blame] | 431 | |
Alexey Marinichev | 9b81877 | 2010-02-23 12:07:48 -0800 | [diff] [blame] | 432 | program = AttributeFetchShaderProgram(4, vertex_buffers); |
Alexey Marinichev | 96e2869 | 2010-02-18 16:59:59 -0800 | [diff] [blame] | 433 | RunTest(DrawElementsTestFunc, |
| 434 | "mvtx_sec_attribute_fetch_shader_4_attr", arg1, true); |
Alexey Marinichev | 3c19973 | 2010-03-11 10:33:35 -0800 | [diff] [blame] | 435 | glDeleteProgram(program); |
Alexey Marinichev | 96e2869 | 2010-02-18 16:59:59 -0800 | [diff] [blame] | 436 | |
Alexey Marinichev | 9b81877 | 2010-02-23 12:07:48 -0800 | [diff] [blame] | 437 | program = AttributeFetchShaderProgram(8, vertex_buffers); |
Alexey Marinichev | 96e2869 | 2010-02-18 16:59:59 -0800 | [diff] [blame] | 438 | RunTest(DrawElementsTestFunc, |
| 439 | "mvtx_sec_attribute_fetch_shader_8_attr", arg1, true); |
Alexey Marinichev | 3c19973 | 2010-03-11 10:33:35 -0800 | [diff] [blame] | 440 | glDeleteProgram(program); |
Alexey Marinichev | 96e2869 | 2010-02-18 16:59:59 -0800 | [diff] [blame] | 441 | |
| 442 | glDeleteBuffers(1, &index_buffer); |
| 443 | delete[] indices; |
| 444 | |
| 445 | glDeleteBuffers(1, &vertex_buffer); |
| 446 | delete[] vertices; |
| 447 | } |
| 448 | |
Alexey Marinichev | af0b26d | 2010-02-24 13:03:13 -0800 | [diff] [blame] | 449 | |
Alexey Marinichev | c24aa23 | 2010-02-24 18:15:54 -0800 | [diff] [blame] | 450 | void VaryingsAndDdxyShaderTest() { |
Alexey Marinichev | af0b26d | 2010-02-24 13:03:13 -0800 | [diff] [blame] | 451 | glViewport(-g_width, -g_height, g_width*2, g_height*2); |
| 452 | |
| 453 | const int c = 4; |
| 454 | GLfloat *vertices = NULL; |
| 455 | GLsizeiptr vertex_buffer_size = 0; |
| 456 | CreateLattice(&vertices, &vertex_buffer_size, 1.f / c, 1.f / c, c, c); |
| 457 | GLuint vertex_buffer = SetupVBO(GL_ARRAY_BUFFER, |
| 458 | vertex_buffer_size, vertices); |
| 459 | |
| 460 | GLuint *indices = NULL; |
| 461 | GLuint index_buffer = 0; |
| 462 | GLsizeiptr index_buffer_size = 0; |
| 463 | |
| 464 | arg1 = CreateMesh(&indices, &index_buffer_size, c, c, 0); |
| 465 | index_buffer = SetupVBO(GL_ELEMENT_ARRAY_BUFFER, |
| 466 | index_buffer_size, indices); |
| 467 | |
| 468 | ShaderProgram program = VaryingsShaderProgram(1, vertex_buffer); |
| 469 | RunTest(DrawElementsTestFunc, |
| 470 | "mpixels_sec_varyings_shader_1", g_width * g_height, true); |
Alexey Marinichev | 3c19973 | 2010-03-11 10:33:35 -0800 | [diff] [blame] | 471 | glDeleteProgram(program); |
Alexey Marinichev | af0b26d | 2010-02-24 13:03:13 -0800 | [diff] [blame] | 472 | |
| 473 | program = VaryingsShaderProgram(2, vertex_buffer); |
| 474 | RunTest(DrawElementsTestFunc, |
| 475 | "mpixels_sec_varyings_shader_2", g_width * g_height, true); |
Alexey Marinichev | 3c19973 | 2010-03-11 10:33:35 -0800 | [diff] [blame] | 476 | glDeleteProgram(program); |
Alexey Marinichev | af0b26d | 2010-02-24 13:03:13 -0800 | [diff] [blame] | 477 | |
| 478 | program = VaryingsShaderProgram(4, vertex_buffer); |
| 479 | RunTest(DrawElementsTestFunc, |
| 480 | "mpixels_sec_varyings_shader_4", g_width * g_height, true); |
Alexey Marinichev | 3c19973 | 2010-03-11 10:33:35 -0800 | [diff] [blame] | 481 | glDeleteProgram(program); |
Alexey Marinichev | af0b26d | 2010-02-24 13:03:13 -0800 | [diff] [blame] | 482 | |
| 483 | program = VaryingsShaderProgram(8, vertex_buffer); |
| 484 | RunTest(DrawElementsTestFunc, |
| 485 | "mpixels_sec_varyings_shader_8", g_width * g_height, true); |
Alexey Marinichev | 3c19973 | 2010-03-11 10:33:35 -0800 | [diff] [blame] | 486 | glDeleteProgram(program); |
Alexey Marinichev | af0b26d | 2010-02-24 13:03:13 -0800 | [diff] [blame] | 487 | |
Alexey Marinichev | c24aa23 | 2010-02-24 18:15:54 -0800 | [diff] [blame] | 488 | program = DdxDdyShaderProgram(true, vertex_buffer); |
| 489 | RunTest(DrawElementsTestFunc, |
| 490 | "mpixels_sec_ddx_shader", g_width * g_height, true); |
Alexey Marinichev | 3c19973 | 2010-03-11 10:33:35 -0800 | [diff] [blame] | 491 | glDeleteProgram(program); |
Alexey Marinichev | c24aa23 | 2010-02-24 18:15:54 -0800 | [diff] [blame] | 492 | |
| 493 | program = DdxDdyShaderProgram(false, vertex_buffer); |
| 494 | RunTest(DrawElementsTestFunc, |
| 495 | "mpixels_sec_ddy_shader", g_width * g_height, true); |
Alexey Marinichev | 3c19973 | 2010-03-11 10:33:35 -0800 | [diff] [blame] | 496 | glDeleteProgram(program); |
Alexey Marinichev | c24aa23 | 2010-02-24 18:15:54 -0800 | [diff] [blame] | 497 | |
Alexey Marinichev | af0b26d | 2010-02-24 13:03:13 -0800 | [diff] [blame] | 498 | glDeleteBuffers(1, &index_buffer); |
| 499 | delete[] indices; |
| 500 | |
| 501 | glDeleteBuffers(1, &vertex_buffer); |
| 502 | delete[] vertices; |
| 503 | } |
| 504 | |
Alexey Marinichev | 8919b6e | 2010-03-04 16:21:02 -0800 | [diff] [blame] | 505 | |
| 506 | void YuvToRgbShaderTestHelper(int type, const char *name) { |
Alexey Marinichev | 6294a39 | 2010-03-02 16:53:36 -0800 | [diff] [blame] | 507 | size_t size = 0; |
Alexey Marinichev | 6dfea31 | 2010-03-03 14:53:12 -0800 | [diff] [blame] | 508 | GLuint texture[2]; |
Alexey Marinichev | 6294a39 | 2010-03-02 16:53:36 -0800 | [diff] [blame] | 509 | ShaderProgram program = 0; |
| 510 | GLuint vertex_buffer = 0; |
| 511 | GLfloat vertices[8] = { |
| 512 | 0.f, 0.f, |
| 513 | 1.f, 0.f, |
Alexey Marinichev | e5b107a | 2010-03-10 17:51:04 -0800 | [diff] [blame] | 514 | 0.f, 1.f, |
| 515 | 1.f, 1.f, |
Alexey Marinichev | 6294a39 | 2010-03-02 16:53:36 -0800 | [diff] [blame] | 516 | }; |
Alexey Marinichev | 6dfea31 | 2010-03-03 14:53:12 -0800 | [diff] [blame] | 517 | char evenodd[2] = {0, 255}; |
Alexey Marinichev | e5b107a | 2010-03-10 17:51:04 -0800 | [diff] [blame] | 518 | const int pixel_height = YUV2RGB_HEIGHT * 2 / 3; |
Alexey Marinichev | 6294a39 | 2010-03-02 16:53:36 -0800 | [diff] [blame] | 519 | |
| 520 | char *pixels = static_cast<char *>(MmapFile(YUV2RGB_NAME, &size)); |
| 521 | if (!pixels) { |
| 522 | printf("# Could not open image file: %s\n", YUV2RGB_NAME); |
| 523 | goto done; |
| 524 | } |
| 525 | if (size != YUV2RGB_SIZE) { |
Alexey Marinichev | 6dfea31 | 2010-03-03 14:53:12 -0800 | [diff] [blame] | 526 | printf("# Image file of wrong size, got %d, expected %d\n", |
| 527 | static_cast<int>(size), YUV2RGB_SIZE); |
Alexey Marinichev | 6294a39 | 2010-03-02 16:53:36 -0800 | [diff] [blame] | 528 | goto done; |
| 529 | } |
| 530 | |
Alexey Marinichev | 6dfea31 | 2010-03-03 14:53:12 -0800 | [diff] [blame] | 531 | glGenTextures(2, texture); |
| 532 | glActiveTexture(GL_TEXTURE0); |
| 533 | glBindTexture(GL_TEXTURE_2D, texture[0]); |
Alexey Marinichev | 6294a39 | 2010-03-02 16:53:36 -0800 | [diff] [blame] | 534 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
| 535 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
| 536 | glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, YUV2RGB_WIDTH, YUV2RGB_HEIGHT, |
| 537 | 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, pixels); |
| 538 | |
Alexey Marinichev | 6dfea31 | 2010-03-03 14:53:12 -0800 | [diff] [blame] | 539 | glActiveTexture(GL_TEXTURE1); |
| 540 | glBindTexture(GL_TEXTURE_2D, texture[1]); |
| 541 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
| 542 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
| 543 | glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, 2, 1, |
| 544 | 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, evenodd); |
| 545 | |
Alexey Marinichev | e5b107a | 2010-03-10 17:51:04 -0800 | [diff] [blame] | 546 | glViewport(-YUV2RGB_WIDTH, -pixel_height, YUV2RGB_WIDTH*2, pixel_height * 2); |
Alexey Marinichev | 6294a39 | 2010-03-02 16:53:36 -0800 | [diff] [blame] | 547 | vertex_buffer = SetupVBO(GL_ARRAY_BUFFER, sizeof(vertices), vertices); |
| 548 | |
Alexey Marinichev | 8919b6e | 2010-03-04 16:21:02 -0800 | [diff] [blame] | 549 | program = YuvToRgbShaderProgram(type, vertex_buffer, |
Alexey Marinichev | e5b107a | 2010-03-10 17:51:04 -0800 | [diff] [blame] | 550 | YUV2RGB_WIDTH, pixel_height); |
| 551 | |
| 552 | if (program) { |
| 553 | float coeff = 1.f * |
| 554 | (YUV2RGB_WIDTH < g_width ? |
| 555 | static_cast<float>(YUV2RGB_WIDTH) / g_width : 1.f) * |
| 556 | (pixel_height < g_height ? |
| 557 | static_cast<float>(pixel_height) / g_height : 1.f); |
| 558 | FillRateTestNormal(name, coeff); |
| 559 | } else { |
Alexey Marinichev | 6dfea31 | 2010-03-03 14:53:12 -0800 | [diff] [blame] | 560 | printf("# Could not set up YUV shader.\n"); |
Alexey Marinichev | e5b107a | 2010-03-10 17:51:04 -0800 | [diff] [blame] | 561 | } |
Alexey Marinichev | 6294a39 | 2010-03-02 16:53:36 -0800 | [diff] [blame] | 562 | |
| 563 | done: |
Alexey Marinichev | 3c19973 | 2010-03-11 10:33:35 -0800 | [diff] [blame] | 564 | glDeleteProgram(program); |
Alexey Marinichev | 6dfea31 | 2010-03-03 14:53:12 -0800 | [diff] [blame] | 565 | glDeleteTextures(2, texture); |
| 566 | glDeleteBuffers(1, &vertex_buffer); |
Alexey Marinichev | 6294a39 | 2010-03-02 16:53:36 -0800 | [diff] [blame] | 567 | munmap(pixels, size); |
| 568 | } |
| 569 | |
Alexey Marinichev | 8919b6e | 2010-03-04 16:21:02 -0800 | [diff] [blame] | 570 | void YuvToRgbShaderTest1() { |
| 571 | YuvToRgbShaderTestHelper(1, "yuv_shader_1"); |
| 572 | } |
| 573 | |
| 574 | void YuvToRgbShaderTest2() { |
| 575 | YuvToRgbShaderTestHelper(2, "yuv_shader_2"); |
| 576 | } |
| 577 | |
Matthew Papakipos | 1efe8bb | 2010-03-24 13:57:49 -0700 | [diff] [blame^] | 578 | static GLuint compositing_textures[5]; |
| 579 | static uint32_t texture_base[WINDOW_HEIGHT*WINDOW_WIDTH]; |
| 580 | static uint32_t texture_update[WINDOW_HEIGHT*WINDOW_WIDTH]; |
| 581 | static ShaderProgram compositing_background_program = 0; |
| 582 | static ShaderProgram compositing_foreground_program = 0; |
| 583 | |
| 584 | void InitBaseTexture() { |
| 585 | for (int y = 0; y < WINDOW_HEIGHT; y++) { |
| 586 | for (int x = 0; x < WINDOW_WIDTH; x++) { |
| 587 | // This color is gray, half alpha. |
| 588 | texture_base[y*WINDOW_WIDTH+x] = 0x80808080; |
| 589 | } |
| 590 | } |
| 591 | } |
| 592 | |
| 593 | // UpdateTexture simulates Chrome updating tab contents. |
| 594 | // We cause a bunch of read and write cpu memory bandwidth. |
| 595 | // It's a very rough approximation. |
| 596 | void UpdateTexture() { |
| 597 | memcpy(texture_update, texture_base, sizeof(texture_base)); |
| 598 | } |
| 599 | |
| 600 | void LoadTexture() { |
| 601 | // Use GL_RGBA for compatibility with GLES2.0. |
| 602 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, |
| 603 | WINDOW_WIDTH, WINDOW_HEIGHT, 0, |
| 604 | GL_RGBA, GL_UNSIGNED_BYTE, texture_update); |
| 605 | } |
| 606 | |
| 607 | // Test how fast we can do full-screen compositing of images |
| 608 | // continuously updated from the CPU. |
| 609 | // This tests both GPU compositing performance and also |
| 610 | // CPU -> GPU data transfer speed. |
| 611 | // It is a basic perf test to make sure we have enough performance |
| 612 | // to run a compositing window manager. |
| 613 | void CompositingTestFunc(int iter) { |
| 614 | for (int i = 0 ; i < iter; ++i) { |
| 615 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
| 616 | |
| 617 | // Draw the background |
| 618 | glDisable(GL_BLEND); |
| 619 | glDisable(GL_DEPTH_TEST); |
| 620 | // We have to blend three textures, but we use multi-texture for this |
| 621 | // blending, not fb blend, to avoid the external memory traffic |
| 622 | glActiveTexture(GL_TEXTURE0); |
| 623 | glBindTexture(GL_TEXTURE_2D, compositing_textures[0]); |
| 624 | glActiveTexture(GL_TEXTURE1); |
| 625 | glBindTexture(GL_TEXTURE_2D, compositing_textures[1]); |
| 626 | glActiveTexture(GL_TEXTURE2); |
| 627 | glBindTexture(GL_TEXTURE_2D, compositing_textures[2]); |
| 628 | // Set up the texture coordinate arrays |
| 629 | glClientActiveTexture(GL_TEXTURE0); |
| 630 | glEnableClientState(GL_TEXTURE_COORD_ARRAY); |
| 631 | glClientActiveTexture(GL_TEXTURE1); |
| 632 | glEnableClientState(GL_TEXTURE_COORD_ARRAY); |
| 633 | glClientActiveTexture(GL_TEXTURE2); |
| 634 | glEnableClientState(GL_TEXTURE_COORD_ARRAY); |
| 635 | // Use the right shader |
| 636 | glUseProgram(compositing_background_program); |
| 637 | // Draw the quad |
| 638 | glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); |
| 639 | |
| 640 | // Set up one texture coordinate array |
| 641 | glClientActiveTexture(GL_TEXTURE0); |
| 642 | glEnableClientState(GL_TEXTURE_COORD_ARRAY); |
| 643 | glClientActiveTexture(GL_TEXTURE1); |
| 644 | glDisableClientState(GL_TEXTURE_COORD_ARRAY); |
| 645 | glClientActiveTexture(GL_TEXTURE2); |
| 646 | glDisableClientState(GL_TEXTURE_COORD_ARRAY); |
| 647 | // Use the right shader |
| 648 | glUseProgram(compositing_foreground_program); |
| 649 | |
| 650 | // Compositing is blending, so we shall blend. |
| 651 | glEnable(GL_BLEND); |
| 652 | // Depth test is on for window occlusion |
| 653 | glEnable(GL_DEPTH_TEST); |
| 654 | |
| 655 | // Draw window number one |
| 656 | // This update acts like a chrome webkit sw rendering update. |
| 657 | glActiveTexture(GL_TEXTURE0); |
| 658 | glBindTexture(GL_TEXTURE_2D, compositing_textures[3]); |
| 659 | UpdateTexture(); |
| 660 | // TODO(papakipos): this LoadTexture is likely doing more CPU memory copies |
| 661 | // than we would like. |
| 662 | LoadTexture(); |
| 663 | // TODO(papakipos): add color interpolation here, and modulate |
| 664 | // texture against it. |
| 665 | glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); |
| 666 | |
| 667 | // Draw window number two |
| 668 | // This is a static window, so we don't update it. |
| 669 | glActiveTexture(GL_TEXTURE0); |
| 670 | glBindTexture(GL_TEXTURE_2D, compositing_textures[4]); |
| 671 | // TODO(papakipos): add color interpolation here, and modulate |
| 672 | // texture against it. |
| 673 | glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); |
| 674 | } |
| 675 | } |
| 676 | |
| 677 | void InitializeCompositing() { |
| 678 | InitBaseTexture(); |
| 679 | |
| 680 | glClearColor(0.f, 0.f, 0.f, 0.f); |
| 681 | glDisable(GL_DEPTH_TEST); |
| 682 | glDisable(GL_BLEND); |
| 683 | glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); |
| 684 | glDepthFunc(GL_LEQUAL); |
| 685 | |
| 686 | glGenTextures(5, compositing_textures); |
| 687 | glActiveTexture(GL_TEXTURE0); |
| 688 | for (int i = 0; i < 5; i++) { |
| 689 | glBindTexture(GL_TEXTURE_2D, compositing_textures[i]); |
| 690 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, |
| 691 | GL_LINEAR); |
| 692 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, |
| 693 | GL_LINEAR); |
| 694 | } |
| 695 | |
| 696 | glColor4f(1.f, 1.f, 1.f, 1.f); |
| 697 | |
| 698 | // Set up the vertex arrays for drawing textured quads later on. |
| 699 | glEnableClientState(GL_VERTEX_ARRAY); |
| 700 | GLfloat buffer_vertex[8] = { |
| 701 | -1.f, -1.f, |
| 702 | 1.f, -1.f, |
| 703 | -1.f, 1.f, |
| 704 | 1.f, 1.f, |
| 705 | }; |
| 706 | GLuint vbo_vertex = SetupVBO(GL_ARRAY_BUFFER, |
| 707 | sizeof(buffer_vertex), buffer_vertex); |
| 708 | if (!vbo_vertex) |
| 709 | printf("# Not Using VBO!\n"); |
| 710 | glVertexPointer(2, GL_FLOAT, 0, vbo_vertex ? 0 : buffer_vertex); |
| 711 | |
| 712 | GLfloat buffer_texture[8] = { |
| 713 | 0.f, 0.f, |
| 714 | 1.f, 0.f, |
| 715 | 0.f, 1.f, |
| 716 | 1.f, 1.f, |
| 717 | }; |
| 718 | GLuint vbo_texture = SetupVBO(GL_ARRAY_BUFFER, |
| 719 | sizeof(buffer_texture), buffer_texture); |
| 720 | for (int i = 0; i < 3; i++) { |
| 721 | glClientActiveTexture(GL_TEXTURE0 + i); |
| 722 | glTexCoordPointer(2, GL_FLOAT, 0, vbo_texture ? 0 : buffer_texture); |
| 723 | glEnableClientState(GL_TEXTURE_COORD_ARRAY); |
| 724 | } |
| 725 | |
| 726 | // Set up the static background textures. |
| 727 | UpdateTexture(); |
| 728 | UpdateTexture(); |
| 729 | UpdateTexture(); |
| 730 | // Load these textures into bound texture ids and keep using them |
| 731 | // from there to avoid having to reload this texture every frame |
| 732 | glActiveTexture(GL_TEXTURE0); |
| 733 | glBindTexture(GL_TEXTURE_2D, compositing_textures[0]); |
| 734 | LoadTexture(); |
| 735 | glActiveTexture(GL_TEXTURE1); |
| 736 | glBindTexture(GL_TEXTURE_2D, compositing_textures[1]); |
| 737 | LoadTexture(); |
| 738 | glActiveTexture(GL_TEXTURE2); |
| 739 | glBindTexture(GL_TEXTURE_2D, compositing_textures[2]); |
| 740 | LoadTexture(); |
| 741 | |
| 742 | glActiveTexture(GL_TEXTURE0); |
| 743 | glBindTexture(GL_TEXTURE_2D, compositing_textures[3]); |
| 744 | UpdateTexture(); |
| 745 | LoadTexture(); |
| 746 | |
| 747 | glActiveTexture(GL_TEXTURE0); |
| 748 | glBindTexture(GL_TEXTURE_2D, compositing_textures[4]); |
| 749 | UpdateTexture(); |
| 750 | LoadTexture(); |
| 751 | |
| 752 | // Set up vertex & fragment shaders. |
| 753 | compositing_background_program = |
| 754 | TripleTextureBlendShaderProgram(vbo_vertex, |
| 755 | vbo_texture, vbo_texture, vbo_texture); |
| 756 | compositing_foreground_program = |
| 757 | BasicTextureShaderProgram(vbo_vertex, vbo_texture); |
| 758 | if ((!compositing_background_program) || |
| 759 | (!compositing_foreground_program)) { |
| 760 | printf("# Could not set up compositing shader.\n"); |
| 761 | } |
| 762 | |
| 763 | if (!vbo_vertex) |
| 764 | printf("# Not Using VBO!\n"); |
| 765 | glVertexPointer(2, GL_FLOAT, 0, vbo_vertex ? 0 : buffer_vertex); |
| 766 | } |
| 767 | |
| 768 | void TeardownCompositing() { |
| 769 | glDeleteProgram(compositing_background_program); |
| 770 | glDeleteProgram(compositing_foreground_program); |
| 771 | } |
| 772 | |
| 773 | // Notes on the window manager compositing test: |
| 774 | // Depth |
| 775 | // Depth complexity = 3: background, active window, static window |
| 776 | // Background: may be a tex-blend of three images (2.5d effect) |
| 777 | // The windows -- at most two, fullscreen |
| 778 | // Depth test is on, passing most of the time. |
| 779 | // A lot of texture min-filtering -- not modelled |
| 780 | // One of the two windows is getting live browser frame updates -- not mod |
| 781 | // The live window runs at x/2 and y/2 size -- not modelled |
| 782 | // The two windows are modulated by color interpolation to get gradient |
| 783 | static float screen_scale_factor = (1e6f* |
| 784 | (WINDOW_WIDTH*WINDOW_HEIGHT)/ |
| 785 | (1280.f*768)); |
| 786 | |
| 787 | void WindowManagerCompositingTest() { |
| 788 | InitializeCompositing(); |
| 789 | RunTest(CompositingTestFunc, "1280x768_fps_compositing", |
| 790 | screen_scale_factor, true); |
| 791 | TeardownCompositing(); |
| 792 | } |
| 793 | |
| 794 | void NoFillWindowManagerCompositingTest() { |
| 795 | glScissor(0, 0, 1, 1); |
| 796 | glEnable(GL_SCISSOR_TEST); |
| 797 | InitializeCompositing(); |
| 798 | RunTest(CompositingTestFunc, "1280x768_fps_no_fill_compositing", |
| 799 | screen_scale_factor, true); |
| 800 | TeardownCompositing(); |
| 801 | } |
Alexey Marinichev | 6294a39 | 2010-03-02 16:53:36 -0800 | [diff] [blame] | 802 | |
Alexey Marinichev | 3c19973 | 2010-03-11 10:33:35 -0800 | [diff] [blame] | 803 | // TODO: get resources file from a command line option or something. |
Alexey Marinichev | 19b5a5d | 2010-02-18 11:37:26 -0800 | [diff] [blame] | 804 | // TODO: use proper command line parsing library. |
| 805 | static void ParseArgs(int argc, char *argv[]) { |
| 806 | const char **enabled_tests_ptr = enabled_tests; |
| 807 | bool test_name_arg = false; |
| 808 | bool duration_arg = false; |
| 809 | for (int i = 0; i < argc; i++) { |
| 810 | if (test_name_arg) { |
| 811 | test_name_arg = false; |
| 812 | *enabled_tests_ptr++ = argv[i]; |
| 813 | if (enabled_tests_ptr - enabled_tests >= enabled_tests_max) |
| 814 | break; |
| 815 | } else if (duration_arg) { |
| 816 | duration_arg = false; |
| 817 | seconds_to_run = atoi(argv[i]); |
| 818 | } else if (strcmp("-t", argv[i]) == 0) { |
| 819 | test_name_arg = true; |
| 820 | } else if (strcmp("-d", argv[i]) == 0) { |
| 821 | duration_arg = true; |
| 822 | } |
| 823 | } |
| 824 | *enabled_tests_ptr++ = NULL; |
| 825 | } |
| 826 | |
| 827 | |
Alexey Marinichev | 592b862 | 2010-02-04 15:20:10 -0800 | [diff] [blame] | 828 | int main(int argc, char *argv[]) { |
Antoine Labour | ec7eb53 | 2010-03-12 11:01:34 -0800 | [diff] [blame] | 829 | SetBasePathFromArgv0(argv[0], "src"); |
Alexey Marinichev | 19b5a5d | 2010-02-18 11:37:26 -0800 | [diff] [blame] | 830 | ParseArgs(argc, argv); |
Alexey Marinichev | 592b862 | 2010-02-04 15:20:10 -0800 | [diff] [blame] | 831 | if (!Init()) { |
| 832 | printf("# Failed to initialize.\n"); |
| 833 | return 1; |
| 834 | } |
| 835 | |
| 836 | void (*test[])() = { |
| 837 | SwapTest, |
| 838 | ClearTest, |
| 839 | FillRateTest, |
| 840 | TriangleSetupTest, |
Alexey Marinichev | af0b26d | 2010-02-24 13:03:13 -0800 | [diff] [blame] | 841 | AttributeFetchShaderTest, |
Alexey Marinichev | c24aa23 | 2010-02-24 18:15:54 -0800 | [diff] [blame] | 842 | VaryingsAndDdxyShaderTest, |
Alexey Marinichev | 8919b6e | 2010-03-04 16:21:02 -0800 | [diff] [blame] | 843 | YuvToRgbShaderTest1, |
| 844 | YuvToRgbShaderTest2, |
Matthew Papakipos | 1efe8bb | 2010-03-24 13:57:49 -0700 | [diff] [blame^] | 845 | NoFillWindowManagerCompositingTest, |
| 846 | WindowManagerCompositingTest, |
Alexey Marinichev | 592b862 | 2010-02-04 15:20:10 -0800 | [diff] [blame] | 847 | }; |
| 848 | |
Alexey Marinichev | 19b5a5d | 2010-02-18 11:37:26 -0800 | [diff] [blame] | 849 | uint64_t done = GetUTime() + 1000000ULL * seconds_to_run; |
| 850 | do { |
| 851 | for (unsigned int i = 0; i < sizeof(test) / sizeof(*test); i++) { |
| 852 | InitContext(); |
| 853 | test[i](); |
Alexey Marinichev | 9b81877 | 2010-02-23 12:07:48 -0800 | [diff] [blame] | 854 | GLenum err = glGetError(); |
| 855 | if (err != 0) |
Alexey Marinichev | 8919b6e | 2010-03-04 16:21:02 -0800 | [diff] [blame] | 856 | printf("# glGetError returned non-zero: 0x%x\n", err); |
Alexey Marinichev | 19b5a5d | 2010-02-18 11:37:26 -0800 | [diff] [blame] | 857 | DestroyContext(); |
| 858 | } |
| 859 | } while (GetUTime() < done); |
Alexey Marinichev | 592b862 | 2010-02-04 15:20:10 -0800 | [diff] [blame] | 860 | |
| 861 | return 0; |
| 862 | } |