OLD | NEW |
1 // Copyright (c) 2010 The Chromium OS Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <fcntl.h> | 5 #include <fcntl.h> |
6 #include <stdio.h> | 6 #include <stdio.h> |
7 #include <string.h> | 7 #include <string.h> |
8 #include <sys/mman.h> | 8 #include <sys/mman.h> |
9 #include <unistd.h> | 9 #include <unistd.h> |
10 | 10 |
(...skipping 29 matching lines...) Expand all Loading... |
40 | 40 |
41 close(fd); | 41 close(fd); |
42 | 42 |
43 if (mmap_ptr) | 43 if (mmap_ptr) |
44 *length = sb.st_size; | 44 *length = sb.st_size; |
45 | 45 |
46 return mmap_ptr; | 46 return mmap_ptr; |
47 } | 47 } |
48 | 48 |
49 | 49 |
| 50 namespace glbench { |
| 51 |
| 52 GLuint SetupTexture(GLsizei size_log2) { |
| 53 GLsizei size = 1 << size_log2; |
| 54 GLuint name = ~0; |
| 55 glGenTextures(1, &name); |
| 56 glBindTexture(GL_TEXTURE_2D, name); |
| 57 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
| 58 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
| 59 |
| 60 unsigned char *pixels = new unsigned char[size * size * 4]; |
| 61 if (!pixels) |
| 62 return 0; |
| 63 |
| 64 for (GLint level = 0; size > 0; level++, size /= 2) { |
| 65 unsigned char *p = pixels; |
| 66 for (int i = 0; i < size; i++) { |
| 67 for (int j = 0; j < size; j++) { |
| 68 *p++ = level %3 != 0 ? (i ^ j) << level : 0; |
| 69 *p++ = level %3 != 1 ? (i ^ j) << level : 0; |
| 70 *p++ = level %3 != 2 ? (i ^ j) << level : 0; |
| 71 *p++ = 255; |
| 72 } |
| 73 } |
| 74 if (size == 1) { |
| 75 unsigned char *p = pixels; |
| 76 *p++ = 255; |
| 77 *p++ = 255; |
| 78 *p++ = 255; |
| 79 *p++ = 255; |
| 80 } |
| 81 glTexImage2D(GL_TEXTURE_2D, level, GL_RGBA, size, size, 0, |
| 82 GL_RGBA, GL_UNSIGNED_BYTE, pixels); |
| 83 } |
| 84 delete[] pixels; |
| 85 return name; |
| 86 } |
| 87 |
| 88 GLuint SetupVBO(GLenum target, GLsizeiptr size, const GLvoid *data) { |
| 89 GLuint buf = ~0; |
| 90 glGenBuffers(1, &buf); |
| 91 glBindBuffer(target, buf); |
| 92 glBufferData(target, size, data, GL_STATIC_DRAW); |
| 93 CHECK(!glGetError()); |
| 94 return buf; |
| 95 } |
| 96 |
| 97 // Generates a tautological lattice. |
| 98 void CreateLattice(GLfloat **vertices, GLsizeiptr *size, |
| 99 GLfloat size_x, GLfloat size_y, int width, int height) |
| 100 { |
| 101 GLfloat *vptr = *vertices = new GLfloat[2 * (width + 1) * (height + 1)]; |
| 102 for (int j = 0; j <= height; j++) { |
| 103 for (int i = 0; i <= width; i++) { |
| 104 *vptr++ = i * size_x; |
| 105 *vptr++ = j * size_y; |
| 106 } |
| 107 } |
| 108 *size = (vptr - *vertices) * sizeof(GLfloat); |
| 109 } |
| 110 |
| 111 // Generates a mesh of 2*width*height triangles. The ratio of front facing to |
| 112 // back facing triangles is culled_ratio/RAND_MAX. Returns the number of |
| 113 // vertices in the mesh. |
| 114 int CreateMesh(GLuint **indices, GLsizeiptr *size, |
| 115 int width, int height, int culled_ratio) { |
| 116 srand(0); |
| 117 |
| 118 GLuint *iptr = *indices = new GLuint[2 * 3 * (width * height)]; |
| 119 const int swath_height = 4; |
| 120 |
| 121 CHECK(width % swath_height == 0 && height % swath_height == 0); |
| 122 |
| 123 for (int j = 0; j < height; j += swath_height) { |
| 124 for (int i = 0; i < width; i++) { |
| 125 for (int j2 = 0; j2 < swath_height; j2++) { |
| 126 GLuint first = (j + j2) * (width + 1) + i; |
| 127 GLuint second = first + 1; |
| 128 GLuint third = first + (width + 1); |
| 129 GLuint fourth = third + 1; |
| 130 |
| 131 bool flag = rand() < culled_ratio; |
| 132 *iptr++ = first; |
| 133 *iptr++ = flag ? second : third; |
| 134 *iptr++ = flag ? third : second; |
| 135 |
| 136 *iptr++ = fourth; |
| 137 *iptr++ = flag ? third : second; |
| 138 *iptr++ = flag ? second : third; |
| 139 } |
| 140 } |
| 141 } |
| 142 *size = (iptr - *indices) * sizeof(GLuint); |
| 143 |
| 144 return iptr - *indices; |
| 145 } |
| 146 |
50 static void print_info_log(int obj) | 147 static void print_info_log(int obj) |
51 { | 148 { |
52 char info_log[4096]; | 149 char info_log[4096]; |
53 int length; | 150 int length; |
54 glGetError(); | 151 glGetError(); |
55 glGetShaderInfoLog(obj, sizeof(info_log)-1, &length, info_log); | 152 glGetShaderInfoLog(obj, sizeof(info_log)-1, &length, info_log); |
56 if (glGetError() != 0) | 153 if (glGetError() != 0) |
57 glGetProgramInfoLog(obj, sizeof(info_log)-1, &length, info_log); | 154 glGetProgramInfoLog(obj, sizeof(info_log)-1, &length, info_log); |
58 char *p = info_log; | 155 char *p = info_log; |
59 while (p < info_log + length) { | 156 while (p < info_log + length) { |
60 char *newline = strchr(p, '\n'); | 157 char *newline = strchr(p, '\n'); |
61 if (newline) | 158 if (newline) |
62 *newline = '\0'; | 159 *newline = '\0'; |
63 printf("# Log: %s\n", p); | 160 printf("# Log: %s\n", p); |
64 if (!newline) | 161 if (!newline) |
65 break; | 162 break; |
66 p = newline + 1; | 163 p = newline + 1; |
67 } | 164 } |
68 } | 165 } |
69 | 166 |
70 | |
71 GLuint InitShaderProgram(const char *vertex_src, const char *fragment_src) { | 167 GLuint InitShaderProgram(const char *vertex_src, const char *fragment_src) { |
72 GLuint vertex_shader = glCreateShader(GL_VERTEX_SHADER); | 168 GLuint vertex_shader = glCreateShader(GL_VERTEX_SHADER); |
73 GLuint fragment_shader = glCreateShader(GL_FRAGMENT_SHADER); | 169 GLuint fragment_shader = glCreateShader(GL_FRAGMENT_SHADER); |
74 | 170 |
75 glShaderSource(vertex_shader, 1, &vertex_src, NULL); | 171 glShaderSource(vertex_shader, 1, &vertex_src, NULL); |
76 glShaderSource(fragment_shader, 1, &fragment_src, NULL); | 172 glShaderSource(fragment_shader, 1, &fragment_src, NULL); |
77 | 173 |
78 glCompileShader(vertex_shader); | 174 glCompileShader(vertex_shader); |
79 print_info_log(vertex_shader); | 175 print_info_log(vertex_shader); |
80 glCompileShader(fragment_shader); | 176 glCompileShader(fragment_shader); |
81 print_info_log(fragment_shader); | 177 print_info_log(fragment_shader); |
82 | 178 |
83 GLuint program = glCreateProgram(); | 179 GLuint program = glCreateProgram(); |
84 glAttachShader(program, vertex_shader); | 180 glAttachShader(program, vertex_shader); |
85 glAttachShader(program, fragment_shader); | 181 glAttachShader(program, fragment_shader); |
86 glLinkProgram(program); | 182 glLinkProgram(program); |
87 print_info_log(program); | 183 print_info_log(program); |
88 glUseProgram(program); | 184 glUseProgram(program); |
89 | 185 |
90 glDeleteShader(vertex_shader); | 186 glDeleteShader(vertex_shader); |
91 glDeleteShader(fragment_shader); | 187 glDeleteShader(fragment_shader); |
92 | 188 |
93 return program; | 189 return program; |
94 } | 190 } |
| 191 |
| 192 } // namespace glbench |
OLD | NEW |