fill_common.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /*
  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. #include "fragment_shaders.cpp"
  17. FILE * fOut = NULL;
  18. void ptSwap();
  19. static char gCurrentTestName[1024];
  20. static uint32_t gWidth = 0;
  21. static uint32_t gHeight = 0;
  22. static void checkGlError(const char* op) {
  23. for (GLint error = glGetError(); error; error
  24. = glGetError()) {
  25. ALOGE("after %s() glError (0x%x)\n", op, error);
  26. }
  27. }
  28. GLuint loadShader(GLenum shaderType, const char* pSource) {
  29. GLuint shader = glCreateShader(shaderType);
  30. if (shader) {
  31. glShaderSource(shader, 1, &pSource, NULL);
  32. glCompileShader(shader);
  33. GLint compiled = 0;
  34. glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled);
  35. if (!compiled) {
  36. GLint infoLen = 0;
  37. glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLen);
  38. if (infoLen) {
  39. char* buf = (char*) malloc(infoLen);
  40. if (buf) {
  41. glGetShaderInfoLog(shader, infoLen, NULL, buf);
  42. ALOGE("Could not compile shader %d:\n%s\n", shaderType, buf);
  43. free(buf);
  44. }
  45. glDeleteShader(shader);
  46. shader = 0;
  47. }
  48. }
  49. }
  50. return shader;
  51. }
  52. enum {
  53. A_POS,
  54. A_COLOR,
  55. A_TEX0,
  56. A_TEX1
  57. };
  58. GLuint createProgram(const char* pVertexSource, const char* pFragmentSource) {
  59. GLuint vertexShader = loadShader(GL_VERTEX_SHADER, pVertexSource);
  60. if (!vertexShader) {
  61. return 0;
  62. }
  63. GLuint pixelShader = loadShader(GL_FRAGMENT_SHADER, pFragmentSource);
  64. if (!pixelShader) {
  65. return 0;
  66. }
  67. GLuint program = glCreateProgram();
  68. if (program) {
  69. glAttachShader(program, vertexShader);
  70. checkGlError("glAttachShader v");
  71. glAttachShader(program, pixelShader);
  72. checkGlError("glAttachShader p");
  73. glBindAttribLocation(program, A_POS, "a_pos");
  74. glBindAttribLocation(program, A_COLOR, "a_color");
  75. glBindAttribLocation(program, A_TEX0, "a_tex0");
  76. glBindAttribLocation(program, A_TEX1, "a_tex1");
  77. glLinkProgram(program);
  78. GLint linkStatus = GL_FALSE;
  79. glGetProgramiv(program, GL_LINK_STATUS, &linkStatus);
  80. if (linkStatus != GL_TRUE) {
  81. GLint bufLength = 0;
  82. glGetProgramiv(program, GL_INFO_LOG_LENGTH, &bufLength);
  83. if (bufLength) {
  84. char* buf = (char*) malloc(bufLength);
  85. if (buf) {
  86. glGetProgramInfoLog(program, bufLength, NULL, buf);
  87. ALOGE("Could not link program:\n%s\n", buf);
  88. free(buf);
  89. }
  90. }
  91. glDeleteProgram(program);
  92. program = 0;
  93. }
  94. }
  95. checkGlError("createProgram");
  96. glUseProgram(program);
  97. return program;
  98. }
  99. uint64_t getTime() {
  100. struct timespec t;
  101. clock_gettime(CLOCK_MONOTONIC, &t);
  102. return t.tv_nsec + ((uint64_t)t.tv_sec * 1000 * 1000 * 1000);
  103. }
  104. uint64_t gTime;
  105. void startTimer() {
  106. gTime = getTime();
  107. }
  108. static void endTimer(int count) {
  109. uint64_t t2 = getTime();
  110. double delta = ((double)(t2 - gTime)) / 1000000000;
  111. double pixels = (gWidth * gHeight) * count;
  112. double mpps = pixels / delta / 1000000;
  113. double dc60 = ((double)count) / delta / 60;
  114. if (fOut) {
  115. fprintf(fOut, "%s, %f, %f\r\n", gCurrentTestName, mpps, dc60);
  116. fflush(fOut);
  117. } else {
  118. printf("%s, %f, %f\n", gCurrentTestName, mpps, dc60);
  119. }
  120. ALOGI("%s, %f, %f\r\n", gCurrentTestName, mpps, dc60);
  121. }
  122. static const char gVertexShader[] =
  123. "attribute vec4 a_pos;\n"
  124. "attribute vec4 a_color;\n"
  125. "attribute vec2 a_tex0;\n"
  126. "attribute vec2 a_tex1;\n"
  127. "varying vec4 v_color;\n"
  128. "varying vec2 v_tex0;\n"
  129. "varying vec2 v_tex1;\n"
  130. "uniform vec2 u_texOff;\n"
  131. "void main() {\n"
  132. " v_color = a_color;\n"
  133. " v_tex0 = a_tex0;\n"
  134. " v_tex1 = a_tex1;\n"
  135. " v_tex0.x += u_texOff.x;\n"
  136. " v_tex1.y += u_texOff.y;\n"
  137. " gl_Position = a_pos;\n"
  138. "}\n";
  139. static void setupVA() {
  140. static const float vtx[] = {
  141. -1.0f,-1.0f,
  142. 1.0f,-1.0f,
  143. -1.0f, 1.0f,
  144. 1.0f, 1.0f };
  145. static const float color[] = {
  146. 1.0f,0.0f,1.0f,1.0f,
  147. 0.0f,0.0f,1.0f,1.0f,
  148. 1.0f,1.0f,0.0f,1.0f,
  149. 1.0f,1.0f,1.0f,1.0f };
  150. static const float tex0[] = {
  151. 0.0f,0.0f,
  152. 1.0f,0.0f,
  153. 0.0f,1.0f,
  154. 1.0f,1.0f };
  155. static const float tex1[] = {
  156. 1.0f,0.0f,
  157. 1.0f,1.0f,
  158. 0.0f,1.0f,
  159. 0.0f,0.0f };
  160. glEnableVertexAttribArray(A_POS);
  161. glEnableVertexAttribArray(A_COLOR);
  162. glEnableVertexAttribArray(A_TEX0);
  163. glEnableVertexAttribArray(A_TEX1);
  164. glVertexAttribPointer(A_POS, 2, GL_FLOAT, false, 8, vtx);
  165. glVertexAttribPointer(A_COLOR, 4, GL_FLOAT, false, 16, color);
  166. glVertexAttribPointer(A_TEX0, 2, GL_FLOAT, false, 8, tex0);
  167. glVertexAttribPointer(A_TEX1, 2, GL_FLOAT, false, 8, tex1);
  168. }
  169. static void randUniform(int pgm, const char *var) {
  170. GLint loc = glGetUniformLocation(pgm, var);
  171. if (loc >= 0) {
  172. float x = ((float)rand()) / RAND_MAX;
  173. float y = ((float)rand()) / RAND_MAX;
  174. float z = ((float)rand()) / RAND_MAX;
  175. float w = ((float)rand()) / RAND_MAX;
  176. glUniform4f(loc, x, y, z, w);
  177. }
  178. }
  179. static void doLoop(bool warmup, int pgm, uint32_t passCount) {
  180. if (warmup) {
  181. glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
  182. glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
  183. ptSwap();
  184. glFinish();
  185. return;
  186. }
  187. startTimer();
  188. glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
  189. for (uint32_t ct=0; ct < passCount; ct++) {
  190. GLint loc = glGetUniformLocation(pgm, "u_texOff");
  191. glUniform2f(loc, ((float)ct) / passCount, ((float)ct) / 2.f / passCount);
  192. randUniform(pgm, "u_color");
  193. randUniform(pgm, "u_0");
  194. randUniform(pgm, "u_1");
  195. randUniform(pgm, "u_2");
  196. randUniform(pgm, "u_3");
  197. glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
  198. }
  199. ptSwap();
  200. glFinish();
  201. endTimer(passCount);
  202. }
  203. static uint32_t rgb(uint32_t r, uint32_t g, uint32_t b)
  204. {
  205. uint32_t ret = 0xff000000;
  206. ret |= r & 0xff;
  207. ret |= (g & 0xff) << 8;
  208. ret |= (b & 0xff) << 16;
  209. return ret;
  210. }
  211. void genTextures() {
  212. uint32_t *m = (uint32_t *)malloc(1024*1024*4);
  213. for (int y=0; y < 1024; y++){
  214. for (int x=0; x < 1024; x++){
  215. m[y*1024 + x] = rgb(x, (((x+y) & 0xff) == 0x7f) * 0xff, y);
  216. }
  217. }
  218. glBindTexture(GL_TEXTURE_2D, 1);
  219. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1024, 1024, 0, GL_RGBA, GL_UNSIGNED_BYTE, m);
  220. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  221. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  222. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  223. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  224. for (int y=0; y < 16; y++){
  225. for (int x=0; x < 16; x++){
  226. m[y*16 + x] = rgb(x << 4, (((x+y) & 0xf) == 0x7) * 0xff, y << 4);
  227. }
  228. }
  229. glBindTexture(GL_TEXTURE_2D, 2);
  230. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 16, 16, 0, GL_RGBA, GL_UNSIGNED_BYTE, m);
  231. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  232. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  233. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  234. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  235. free(m);
  236. }
  237. static void doSingleTest(uint32_t pgmNum, int tex) {
  238. const char *pgmTxt = gFragmentTests[pgmNum]->txt;
  239. int pgm = createProgram(gVertexShader, pgmTxt);
  240. if (!pgm) {
  241. printf("error running test\n");
  242. return;
  243. }
  244. GLint loc = glGetUniformLocation(pgm, "u_tex0");
  245. if (loc >= 0) glUniform1i(loc, 0);
  246. loc = glGetUniformLocation(pgm, "u_tex1");
  247. if (loc >= 0) glUniform1i(loc, 1);
  248. glActiveTexture(GL_TEXTURE0);
  249. glBindTexture(GL_TEXTURE_2D, tex);
  250. glActiveTexture(GL_TEXTURE1);
  251. glBindTexture(GL_TEXTURE_2D, tex);
  252. glActiveTexture(GL_TEXTURE0);
  253. glBlendFunc(GL_ONE, GL_ONE);
  254. glDisable(GL_BLEND);
  255. //sprintf(str2, "%i, %i, %i, %i, %i, 0",
  256. //useVarColor, texCount, modulateFirstTex, extraMath, tex0);
  257. //doLoop(true, pgm, w, h, str2);
  258. //doLoop(false, pgm, w, h, str2);
  259. glEnable(GL_BLEND);
  260. sprintf(gCurrentTestName, "%s, %i, %i, 1", gFragmentTests[pgmNum]->name, pgmNum, tex);
  261. doLoop(true, pgm, 100);
  262. doLoop(false, pgm, 100);
  263. }