blob: 2a425f7a322184669370a0c1bc21b6ef8d343044 [file] [log] [blame]
Jason Sams744f37f2010-07-20 19:37:58 -07001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Jason Samsd997f712010-09-08 15:22:06 -070017#include "fragment_shaders.cpp"
18
Jason Sams744f37f2010-07-20 19:37:58 -070019FILE * fOut = NULL;
20void ptSwap();
21
Jason Samsd997f712010-09-08 15:22:06 -070022static char gCurrentTestName[1024];
23static uint32_t gWidth = 0;
24static uint32_t gHeight = 0;
25
Jason Sams744f37f2010-07-20 19:37:58 -070026static void checkGlError(const char* op) {
27 for (GLint error = glGetError(); error; error
28 = glGetError()) {
29 LOGE("after %s() glError (0x%x)\n", op, error);
30 }
31}
32
33GLuint loadShader(GLenum shaderType, const char* pSource) {
34 GLuint shader = glCreateShader(shaderType);
35 if (shader) {
36 glShaderSource(shader, 1, &pSource, NULL);
37 glCompileShader(shader);
38 GLint compiled = 0;
39 glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled);
40 if (!compiled) {
41 GLint infoLen = 0;
42 glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLen);
43 if (infoLen) {
44 char* buf = (char*) malloc(infoLen);
45 if (buf) {
46 glGetShaderInfoLog(shader, infoLen, NULL, buf);
47 LOGE("Could not compile shader %d:\n%s\n", shaderType, buf);
48 free(buf);
49 }
50 glDeleteShader(shader);
51 shader = 0;
52 }
53 }
54 }
55 return shader;
56}
57
58enum {
59 A_POS,
60 A_COLOR,
61 A_TEX0,
62 A_TEX1
63};
64
65GLuint createProgram(const char* pVertexSource, const char* pFragmentSource) {
66 GLuint vertexShader = loadShader(GL_VERTEX_SHADER, pVertexSource);
67 if (!vertexShader) {
68 return 0;
69 }
70
71 GLuint pixelShader = loadShader(GL_FRAGMENT_SHADER, pFragmentSource);
72 if (!pixelShader) {
73 return 0;
74 }
75
76 GLuint program = glCreateProgram();
77 if (program) {
78 glAttachShader(program, vertexShader);
79 checkGlError("glAttachShader v");
80 glAttachShader(program, pixelShader);
81 checkGlError("glAttachShader p");
82
83 glBindAttribLocation(program, A_POS, "a_pos");
84 glBindAttribLocation(program, A_COLOR, "a_color");
85 glBindAttribLocation(program, A_TEX0, "a_tex0");
86 glBindAttribLocation(program, A_TEX1, "a_tex1");
87 glLinkProgram(program);
88 GLint linkStatus = GL_FALSE;
89 glGetProgramiv(program, GL_LINK_STATUS, &linkStatus);
90 if (linkStatus != GL_TRUE) {
91 GLint bufLength = 0;
92 glGetProgramiv(program, GL_INFO_LOG_LENGTH, &bufLength);
93 if (bufLength) {
94 char* buf = (char*) malloc(bufLength);
95 if (buf) {
96 glGetProgramInfoLog(program, bufLength, NULL, buf);
97 LOGE("Could not link program:\n%s\n", buf);
98 free(buf);
99 }
100 }
101 glDeleteProgram(program);
102 program = 0;
103 }
104 }
105 checkGlError("createProgram");
106 glUseProgram(program);
107 return program;
108}
109
110uint64_t getTime() {
111 struct timespec t;
112 clock_gettime(CLOCK_MONOTONIC, &t);
113 return t.tv_nsec + ((uint64_t)t.tv_sec * 1000 * 1000 * 1000);
114}
115
116uint64_t gTime;
117void startTimer() {
118 gTime = getTime();
119}
120
Jason Samsd997f712010-09-08 15:22:06 -0700121
122static void endTimer(int count) {
Jason Sams744f37f2010-07-20 19:37:58 -0700123 uint64_t t2 = getTime();
124 double delta = ((double)(t2 - gTime)) / 1000000000;
Jason Samsd997f712010-09-08 15:22:06 -0700125 double pixels = (gWidth * gHeight) * count;
Jason Sams744f37f2010-07-20 19:37:58 -0700126 double mpps = pixels / delta / 1000000;
Jason Samsd997f712010-09-08 15:22:06 -0700127 double dc60 = ((double)count) / delta / 60;
Jason Sams744f37f2010-07-20 19:37:58 -0700128
129 if (fOut) {
Jason Samsd997f712010-09-08 15:22:06 -0700130 fprintf(fOut, "%s, %f, %f\r\n", gCurrentTestName, mpps, dc60);
Jason Sams744f37f2010-07-20 19:37:58 -0700131 fflush(fOut);
132 } else {
Jason Samsd997f712010-09-08 15:22:06 -0700133 printf("%s, %f, %f\n", gCurrentTestName, mpps, dc60);
Jason Sams744f37f2010-07-20 19:37:58 -0700134 }
Steve Block6215d3f2012-01-04 20:05:49 +0000135 ALOGI("%s, %f, %f\r\n", gCurrentTestName, mpps, dc60);
Jason Sams744f37f2010-07-20 19:37:58 -0700136}
137
138
139static const char gVertexShader[] =
140 "attribute vec4 a_pos;\n"
141 "attribute vec4 a_color;\n"
142 "attribute vec2 a_tex0;\n"
143 "attribute vec2 a_tex1;\n"
144 "varying vec4 v_color;\n"
145 "varying vec2 v_tex0;\n"
146 "varying vec2 v_tex1;\n"
Jason Samsd997f712010-09-08 15:22:06 -0700147 "uniform vec2 u_texOff;\n"
Jason Sams744f37f2010-07-20 19:37:58 -0700148
149 "void main() {\n"
150 " v_color = a_color;\n"
151 " v_tex0 = a_tex0;\n"
152 " v_tex1 = a_tex1;\n"
Jason Samsd997f712010-09-08 15:22:06 -0700153 " v_tex0.x += u_texOff.x;\n"
154 " v_tex1.y += u_texOff.y;\n"
Jason Sams744f37f2010-07-20 19:37:58 -0700155 " gl_Position = a_pos;\n"
156 "}\n";
157
Jason Sams744f37f2010-07-20 19:37:58 -0700158static void setupVA() {
159 static const float vtx[] = {
160 -1.0f,-1.0f,
161 1.0f,-1.0f,
162 -1.0f, 1.0f,
163 1.0f, 1.0f };
164 static const float color[] = {
165 1.0f,0.0f,1.0f,1.0f,
166 0.0f,0.0f,1.0f,1.0f,
167 1.0f,1.0f,0.0f,1.0f,
168 1.0f,1.0f,1.0f,1.0f };
169 static const float tex0[] = {
170 0.0f,0.0f,
171 1.0f,0.0f,
Jason Samsd997f712010-09-08 15:22:06 -0700172 0.0f,1.0f,
173 1.0f,1.0f };
Jason Sams744f37f2010-07-20 19:37:58 -0700174 static const float tex1[] = {
175 1.0f,0.0f,
176 1.0f,1.0f,
177 0.0f,1.0f,
178 0.0f,0.0f };
179
180 glEnableVertexAttribArray(A_POS);
181 glEnableVertexAttribArray(A_COLOR);
182 glEnableVertexAttribArray(A_TEX0);
183 glEnableVertexAttribArray(A_TEX1);
184
185 glVertexAttribPointer(A_POS, 2, GL_FLOAT, false, 8, vtx);
186 glVertexAttribPointer(A_COLOR, 4, GL_FLOAT, false, 16, color);
187 glVertexAttribPointer(A_TEX0, 2, GL_FLOAT, false, 8, tex0);
188 glVertexAttribPointer(A_TEX1, 2, GL_FLOAT, false, 8, tex1);
189}
190
191static void randUniform(int pgm, const char *var) {
192 int loc = glGetUniformLocation(pgm, var);
193 if (loc >= 0) {
194 float x = ((float)rand()) / RAND_MAX;
195 float y = ((float)rand()) / RAND_MAX;
196 float z = ((float)rand()) / RAND_MAX;
197 float w = ((float)rand()) / RAND_MAX;
198 glUniform4f(loc, x, y, z, w);
199 }
200}
201
Jason Samsd997f712010-09-08 15:22:06 -0700202static void doLoop(bool warmup, int pgm, uint32_t passCount) {
203 if (warmup) {
Jason Sams744f37f2010-07-20 19:37:58 -0700204 glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
205 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
206 ptSwap();
207 glFinish();
208 return;
209 }
210
211 startTimer();
212 glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
Jason Samsd997f712010-09-08 15:22:06 -0700213 for (uint32_t ct=0; ct < passCount; ct++) {
214 int loc = glGetUniformLocation(pgm, "u_texOff");
215 glUniform2f(loc, ((float)ct) / passCount, ((float)ct) / 2.f / passCount);
216
Jason Sams744f37f2010-07-20 19:37:58 -0700217 randUniform(pgm, "u_color");
218 randUniform(pgm, "u_0");
219 randUniform(pgm, "u_1");
220 randUniform(pgm, "u_2");
221 randUniform(pgm, "u_3");
222 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
223 }
224 ptSwap();
225 glFinish();
Jason Samsd997f712010-09-08 15:22:06 -0700226 endTimer(passCount);
227}
228
229
230static uint32_t rgb(uint32_t r, uint32_t g, uint32_t b)
231{
232 uint32_t ret = 0xff000000;
233 ret |= r & 0xff;
234 ret |= (g & 0xff) << 8;
235 ret |= (b & 0xff) << 16;
236 return ret;
Jason Sams744f37f2010-07-20 19:37:58 -0700237}
238
239void genTextures() {
240 uint32_t *m = (uint32_t *)malloc(1024*1024*4);
241 for (int y=0; y < 1024; y++){
242 for (int x=0; x < 1024; x++){
Jason Samsd997f712010-09-08 15:22:06 -0700243 m[y*1024 + x] = rgb(x, (((x+y) & 0xff) == 0x7f) * 0xff, y);
Jason Sams744f37f2010-07-20 19:37:58 -0700244 }
245 }
246 glBindTexture(GL_TEXTURE_2D, 1);
247 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1024, 1024, 0, GL_RGBA, GL_UNSIGNED_BYTE, m);
248 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
249 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
250 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
251 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
252
253 for (int y=0; y < 16; y++){
254 for (int x=0; x < 16; x++){
Jason Samsd997f712010-09-08 15:22:06 -0700255 m[y*16 + x] = rgb(x << 4, (((x+y) & 0xf) == 0x7) * 0xff, y << 4);
Jason Sams744f37f2010-07-20 19:37:58 -0700256 }
257 }
258 glBindTexture(GL_TEXTURE_2D, 2);
259 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 16, 16, 0, GL_RGBA, GL_UNSIGNED_BYTE, m);
260 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
261 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
262 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
263 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
Jason Samsd997f712010-09-08 15:22:06 -0700264 free(m);
Jason Sams744f37f2010-07-20 19:37:58 -0700265}
266
Jason Samsd997f712010-09-08 15:22:06 -0700267static void doSingleTest(uint32_t pgmNum, int tex) {
268 const char *pgmTxt = gFragmentTests[pgmNum]->txt;
269 int pgm = createProgram(gVertexShader, pgmTxt);
270 if (!pgm) {
271 printf("error running test\n");
272 return;
273 }
274 int loc = glGetUniformLocation(pgm, "u_tex0");
275 if (loc >= 0) glUniform1i(loc, 0);
276 loc = glGetUniformLocation(pgm, "u_tex1");
277 if (loc >= 0) glUniform1i(loc, 1);
278
279
280 glActiveTexture(GL_TEXTURE0);
281 glBindTexture(GL_TEXTURE_2D, tex);
282 glActiveTexture(GL_TEXTURE1);
283 glBindTexture(GL_TEXTURE_2D, tex);
284 glActiveTexture(GL_TEXTURE0);
285
286 glBlendFunc(GL_ONE, GL_ONE);
287 glDisable(GL_BLEND);
288 //sprintf(str2, "%i, %i, %i, %i, %i, 0",
289 //useVarColor, texCount, modulateFirstTex, extraMath, tex0);
290 //doLoop(true, pgm, w, h, str2);
291 //doLoop(false, pgm, w, h, str2);
292
293 glEnable(GL_BLEND);
294 sprintf(gCurrentTestName, "%s, %i, %i, 1", gFragmentTests[pgmNum]->name, pgmNum, tex);
295 doLoop(true, pgm, 100);
296 doLoop(false, pgm, 100);
297}
Jason Sams744f37f2010-07-20 19:37:58 -0700298