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