Clone of mesa.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  1. /*
  2. * This program is under the GNU GPL.
  3. * Use at your own risk.
  4. *
  5. * written by David Bucciarelli (tech.hmw@plus.it)
  6. * Humanware s.r.l.
  7. */
  8. #include <stdlib.h>
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <math.h>
  12. #include <GL/glut.h>
  13. typedef struct
  14. {
  15. char *name;
  16. char *unit;
  17. void (*init) (void);
  18. int (*run) (int, int);
  19. int type;
  20. int numsize;
  21. int size[10];
  22. }
  23. benchmark;
  24. static int frontbuffer = 1;
  25. /***************************************************************************/
  26. static void
  27. init_test01(void)
  28. {
  29. glMatrixMode(GL_PROJECTION);
  30. glLoadIdentity();
  31. gluOrtho2D(-0.5, 639.5, -0.5, 479.5);
  32. glMatrixMode(GL_MODELVIEW);
  33. glShadeModel(GL_FLAT);
  34. glDisable(GL_DEPTH_TEST);
  35. glClearColor(0.0, 0.1, 1.0, 0.0);
  36. glClear(GL_COLOR_BUFFER_BIT);
  37. glColor3f(1.0, 0.0, 0.0);
  38. }
  39. static int
  40. test01(int size, int num)
  41. {
  42. int x, y;
  43. glBegin(GL_POINTS);
  44. for (y = 0; y < num; y++)
  45. for (x = 0; x < 480; x++)
  46. glVertex2i(x, x);
  47. glEnd();
  48. return 480 * num;
  49. }
  50. /***************************************************************************/
  51. static void
  52. init_test02(void)
  53. {
  54. glMatrixMode(GL_PROJECTION);
  55. glLoadIdentity();
  56. gluOrtho2D(-0.5, 639.5, -0.5, 479.5);
  57. glMatrixMode(GL_MODELVIEW);
  58. glShadeModel(GL_SMOOTH);
  59. glDisable(GL_DEPTH_TEST);
  60. glClearColor(0.0, 0.1, 1.0, 0.0);
  61. glClear(GL_COLOR_BUFFER_BIT);
  62. }
  63. static int
  64. test02(int size, int num)
  65. {
  66. int x, y;
  67. glBegin(GL_LINES);
  68. for (y = 0; y < num; y++)
  69. for (x = 0; x < size; x++) {
  70. glColor3f(0.0, 1.0, y / (float) num);
  71. glVertex2i(0, size - 1);
  72. glColor3f(1.0, 0.0, x / (float) size);
  73. glVertex2i(x, x);
  74. }
  75. glEnd();
  76. return num * size;
  77. }
  78. /***************************************************************************/
  79. static void
  80. init_test03(void)
  81. {
  82. glMatrixMode(GL_PROJECTION);
  83. glLoadIdentity();
  84. glOrtho(-0.5, 639.5, -0.5, 479.5, 1.0, -1000.0 * 480.0);
  85. glMatrixMode(GL_MODELVIEW);
  86. glShadeModel(GL_SMOOTH);
  87. glEnable(GL_DEPTH_TEST);
  88. glClearColor(0.0, 0.1, 1.0, 0.0);
  89. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  90. }
  91. static int
  92. test03(int size, int num)
  93. {
  94. int x, y, z;
  95. glBegin(GL_TRIANGLES);
  96. for (y = 0; y < num; y++)
  97. for (x = 0; x < size; x += 5) {
  98. z = num * size - (y * size + x);
  99. glColor3f(0.0, 1.0, 0.0);
  100. glVertex3i(0, x, z);
  101. glColor3f(1.0, 0.0, x / (float) size);
  102. glVertex3i(size - 1 - x, 0, z);
  103. glColor3f(1.0, x / (float) size, 0.0);
  104. glVertex3i(x, size - 1 - x, z);
  105. }
  106. glEnd();
  107. return size * num / 5;
  108. }
  109. /***************************************************************************/
  110. static void
  111. init_test04(void)
  112. {
  113. int x, y;
  114. GLubyte tex[128 * 128 * 3];
  115. GLenum gluerr;
  116. glMatrixMode(GL_PROJECTION);
  117. glLoadIdentity();
  118. glOrtho(-0.5, 639.5, -0.5, 479.5, 1.0, -1000.0 * 480.0);
  119. glMatrixMode(GL_MODELVIEW);
  120. glShadeModel(GL_SMOOTH);
  121. glEnable(GL_DEPTH_TEST);
  122. glEnable(GL_BLEND);
  123. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  124. for (y = 0; y < 128; y++)
  125. for (x = 0; x < 128; x++) {
  126. tex[(x + y * 128) * 3 + 0] = ((x % (128 / 4)) < (128 / 8)) ? 255 : 0;
  127. tex[(x + y * 128) * 3 + 1] = ((y % (128 / 4)) < (128 / 8)) ? 255 : 0;
  128. tex[(x + y * 128) * 3 + 2] = x;
  129. }
  130. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  131. if ((gluerr = gluBuild2DMipmaps(GL_TEXTURE_2D, 3, 128, 128, GL_RGB,
  132. GL_UNSIGNED_BYTE, (GLvoid *) (&tex[0])))) {
  133. fprintf(stderr, "GLULib%s\n", gluErrorString(gluerr));
  134. exit(-1);
  135. }
  136. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  137. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  138. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
  139. GL_LINEAR_MIPMAP_NEAREST);
  140. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  141. glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  142. glEnable(GL_TEXTURE_2D);
  143. glClearColor(0.0, 0.1, 1.0, 0.0);
  144. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  145. }
  146. static int
  147. test04(int size, int num)
  148. {
  149. int x, y, z;
  150. glBegin(GL_TRIANGLES);
  151. for (y = 0; y < num; y++)
  152. for (x = 0; x < size; x += 5) {
  153. z = num * size - (y * size + x);
  154. glTexCoord2f(1.0, 1.0);
  155. glColor3f(1.0, 0.0, 0.0);
  156. glVertex3i(0, x, z);
  157. glTexCoord2f(0.0, 1.0);
  158. glColor3f(0.0, 1.0, 0.0);
  159. glVertex3i(size - 1 - x, 0, z);
  160. glTexCoord2f(1.0, 0.0);
  161. glColor3f(0.0, 0.0, 1.0);
  162. glVertex3i(x, size - 1 - x, z);
  163. }
  164. glEnd();
  165. return num * size / 5;
  166. }
  167. /***************************************************************************/
  168. static void
  169. init_test05(void)
  170. {
  171. int x, y;
  172. GLubyte tex[128 * 128 * 3];
  173. GLenum gluerr;
  174. glMatrixMode(GL_PROJECTION);
  175. glLoadIdentity();
  176. glOrtho(-0.5, 639.5, -0.5, 479.5, -1.0, 1.0);
  177. glMatrixMode(GL_MODELVIEW);
  178. glShadeModel(GL_SMOOTH);
  179. glEnable(GL_DEPTH_TEST);
  180. glEnable(GL_BLEND);
  181. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  182. for (y = 0; y < 128; y++)
  183. for (x = 0; x < 128; x++) {
  184. tex[(x + y * 128) * 3 + 0] = ((x % (128 / 4)) < (128 / 8)) ? 255 : 0;
  185. tex[(x + y * 128) * 3 + 1] = ((y % (128 / 4)) < (128 / 8)) ? 255 : 0;
  186. tex[(x + y * 128) * 3 + 2] = x;
  187. }
  188. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  189. if ((gluerr = gluBuild2DMipmaps(GL_TEXTURE_2D, 3, 128, 128, GL_RGB,
  190. GL_UNSIGNED_BYTE, (GLvoid *) (&tex[0])))) {
  191. fprintf(stderr, "GLULib%s\n", gluErrorString(gluerr));
  192. exit(-1);
  193. }
  194. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  195. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  196. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
  197. GL_LINEAR_MIPMAP_NEAREST);
  198. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  199. glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  200. glEnable(GL_TEXTURE_2D);
  201. glDepthFunc(GL_ALWAYS);
  202. glClearColor(0.0, 0.1, 1.0, 0.0);
  203. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  204. }
  205. static int
  206. test05(int size, int num)
  207. {
  208. int y;
  209. float v0[3], v1[3], v2[3], v3[3];
  210. float cv0[3], cv1[3], cv2[3], cv3[3];
  211. float tv0[3], tv1[3], tv2[3], tv3[3];
  212. v0[0] = 320 - size / 2;
  213. v0[1] = 240 - size / 2;
  214. v0[2] = 0.0;
  215. v1[0] = 320 + size / 2;
  216. v1[1] = 240 - size / 2;
  217. v1[2] = 0.0;
  218. v2[0] = 320 - size / 2;
  219. v2[1] = 240 + size / 2;
  220. v2[2] = 0.0;
  221. v3[0] = 320 + size / 2;
  222. v3[1] = 240 + size / 2;
  223. v3[2] = 0.0;
  224. cv0[0] = 1.0;
  225. cv0[1] = 0.0;
  226. cv0[2] = 0.0;
  227. cv1[0] = 1.0;
  228. cv1[1] = 1.0;
  229. cv1[2] = 0.0;
  230. cv2[0] = 1.0;
  231. cv2[1] = 0.0;
  232. cv2[2] = 1.0;
  233. cv3[0] = 1.0;
  234. cv3[1] = 1.0;
  235. cv3[2] = 1.0;
  236. tv0[0] = 0.0;
  237. tv0[1] = 0.0;
  238. tv0[2] = 0.0;
  239. tv1[0] = 1.0;
  240. tv1[1] = 0.0;
  241. tv1[2] = 0.0;
  242. tv2[0] = 0.0;
  243. tv2[1] = 1.0;
  244. tv2[2] = 0.0;
  245. tv3[0] = 1.0;
  246. tv3[1] = 1.0;
  247. tv3[2] = 0.0;
  248. glBegin(GL_TRIANGLE_STRIP);
  249. for (y = 0; y < num; y++) {
  250. glColor3fv(cv0);
  251. glTexCoord2fv(tv0);
  252. glVertex3fv(v0);
  253. glColor3fv(cv1);
  254. glTexCoord2fv(tv1);
  255. glVertex3fv(v1);
  256. glColor3fv(cv2);
  257. glTexCoord2fv(tv2);
  258. glVertex3fv(v2);
  259. glColor3fv(cv3);
  260. glTexCoord2fv(tv3);
  261. glVertex3fv(v3);
  262. }
  263. glEnd();
  264. return 4 * num - 2;
  265. }
  266. /***************************************************************************/
  267. static void
  268. init_test06(void)
  269. {
  270. glMatrixMode(GL_PROJECTION);
  271. glLoadIdentity();
  272. gluOrtho2D(-0.5, 639.5, -0.5, 479.5);
  273. glMatrixMode(GL_MODELVIEW);
  274. glShadeModel(GL_SMOOTH);
  275. glEnable(GL_DEPTH_TEST);
  276. glClearColor(0.0, 0.1, 1.0, 0.0);
  277. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  278. }
  279. static int
  280. test06(int size, int num)
  281. {
  282. int y;
  283. for (y = 0; y < num; y++) {
  284. glClearColor(y / (float) num, 0.1, 1.0, 0.0);
  285. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  286. }
  287. return num;
  288. }
  289. /***************************************************************************/
  290. #define BMARKS_TIME 5.0
  291. #define NUM_BMARKS 6
  292. /* 554 ~= sqrt(640*480) */
  293. static benchmark bmarks[NUM_BMARKS] = {
  294. {"Simple Points", "Pnts", init_test01, test01, 0, 0,
  295. {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
  296. {"Smooth Lines", "Lins", init_test02, test02, 1, 5,
  297. {480, 250, 100, 50, 25, 0, 0, 0, 0, 0}},
  298. {"ZSmooth Triangles", "Tris", init_test03, test03, 1, 5,
  299. {480, 250, 100, 50, 25, 0, 0, 0, 0, 0}},
  300. {"ZSmooth Tex Blend Triangles", "Tris", init_test04, test04, 1, 5,
  301. {480, 250, 100, 50, 25, 0, 0, 0, 0, 0}},
  302. {"ZSmooth Tex Blend TMesh Triangles", "Tris", init_test05, test05, 2, 8,
  303. {400, 250, 100, 50, 25, 10, 5, 2, 0, 0}},
  304. {"Color/Depth Buffer Clears", "Clrs", init_test06, test06, 3, 0,
  305. {554, 0, 0, 0, 0, 0, 0, 0, 0, 0}}
  306. };
  307. /***************************************************************************/
  308. static void
  309. dotest0param(benchmark * bmark)
  310. {
  311. float stime, etime, dtime, tottime, maxtime, mintime;
  312. int num, numelem, calibnum, j;
  313. glPushAttrib(GL_ALL_ATTRIB_BITS);
  314. bmark->init();
  315. stime = glutGet(GLUT_ELAPSED_TIME);
  316. dtime = 0.0;
  317. calibnum = 0;
  318. while (dtime < 2.0) {
  319. bmark->run(0, 1);
  320. glFinish();
  321. etime = glutGet(GLUT_ELAPSED_TIME);
  322. dtime = (etime - stime) / 1000.0;
  323. calibnum++;
  324. }
  325. glPopAttrib();
  326. fprintf(stderr, "Elapsed time for the calibration test (%d): %f\n",
  327. calibnum, dtime);
  328. num = (int) ((BMARKS_TIME / dtime) * calibnum);
  329. if (num < 1)
  330. num = 1;
  331. fprintf(stderr, "Selected number of benchmark iterations: %d\n", num);
  332. mintime = HUGE_VAL;
  333. maxtime = -HUGE_VAL;
  334. for (tottime = 0.0, j = 0; j < 5; j++) {
  335. glPushAttrib(GL_ALL_ATTRIB_BITS);
  336. bmark->init();
  337. stime = glutGet(GLUT_ELAPSED_TIME);
  338. numelem = bmark->run(0, num);
  339. glFinish();
  340. etime = glutGet(GLUT_ELAPSED_TIME);
  341. glPopAttrib();
  342. dtime = (etime - stime) / 1000.0;
  343. tottime += dtime;
  344. fprintf(stderr, "Elapsed time for run %d: %f\n", j, dtime);
  345. if (dtime < mintime)
  346. mintime = dtime;
  347. if (dtime > maxtime)
  348. maxtime = dtime;
  349. }
  350. tottime -= mintime + maxtime;
  351. fprintf(stdout, "%s\n%f %s/sec", bmark->name, numelem / (tottime / 3.0),
  352. bmark->unit);
  353. if (bmark->type == 3)
  354. fprintf(stdout, ", MPixel Fill/sec: %f\n\n",
  355. (numelem * bmark->size[0] * (float) bmark->size[0]) /
  356. (1000000.0 * tottime / 3.0));
  357. else
  358. fprintf(stdout, "\n\n");
  359. }
  360. /***************************************************************************/
  361. static void
  362. dotest1param(benchmark * bmark)
  363. {
  364. float stime, etime, dtime, tottime, maxtime, mintime;
  365. int num, numelem, calibnum, j, k;
  366. fprintf(stdout, "%s\n", bmark->name);
  367. for (j = 0; j < bmark->numsize; j++) {
  368. fprintf(stderr, "Current size: %d\n", bmark->size[j]);
  369. glPushAttrib(GL_ALL_ATTRIB_BITS);
  370. bmark->init();
  371. stime = glutGet(GLUT_ELAPSED_TIME);
  372. dtime = 0.0;
  373. calibnum = 0;
  374. while (dtime < 2.0) {
  375. bmark->run(bmark->size[j], 1);
  376. glFinish();
  377. etime = glutGet(GLUT_ELAPSED_TIME);
  378. dtime = (etime - stime) / 1000.0;
  379. calibnum++;
  380. }
  381. glPopAttrib();
  382. fprintf(stderr, "Elapsed time for the calibration test (%d): %f\n",
  383. calibnum, dtime);
  384. num = (int) ((BMARKS_TIME / dtime) * calibnum);
  385. if (num < 1)
  386. num = 1;
  387. fprintf(stderr, "Selected number of benchmark iterations: %d\n", num);
  388. mintime = HUGE_VAL;
  389. maxtime = -HUGE_VAL;
  390. for (numelem = 1, tottime = 0.0, k = 0; k < 5; k++) {
  391. glPushAttrib(GL_ALL_ATTRIB_BITS);
  392. bmark->init();
  393. stime = glutGet(GLUT_ELAPSED_TIME);
  394. numelem = bmark->run(bmark->size[j], num);
  395. glFinish();
  396. etime = glutGet(GLUT_ELAPSED_TIME);
  397. glPopAttrib();
  398. dtime = (etime - stime) / 1000.0;
  399. tottime += dtime;
  400. fprintf(stderr, "Elapsed time for run %d: %f\n", k, dtime);
  401. if (dtime < mintime)
  402. mintime = dtime;
  403. if (dtime > maxtime)
  404. maxtime = dtime;
  405. }
  406. tottime -= mintime + maxtime;
  407. fprintf(stdout, "SIZE=%03d => %f %s/sec", bmark->size[j],
  408. numelem / (tottime / 3.0), bmark->unit);
  409. if (bmark->type == 2)
  410. fprintf(stdout, ", MPixel Fill/sec: %f\n",
  411. (numelem * bmark->size[j] * bmark->size[j] / 2) /
  412. (1000000.0 * tottime / 3.0));
  413. else
  414. fprintf(stdout, "\n");
  415. }
  416. fprintf(stdout, "\n\n");
  417. }
  418. /***************************************************************************/
  419. static void
  420. display(void)
  421. {
  422. int i;
  423. if (frontbuffer)
  424. glDrawBuffer(GL_FRONT);
  425. else
  426. glDrawBuffer(GL_BACK);
  427. for (i = 0; i < NUM_BMARKS; i++) {
  428. fprintf(stderr, "Benchmark: %d\n", i);
  429. switch (bmarks[i].type) {
  430. case 0:
  431. case 3:
  432. dotest0param(&bmarks[i]);
  433. break;
  434. case 1:
  435. case 2:
  436. dotest1param(&bmarks[i]);
  437. break;
  438. }
  439. }
  440. exit(0);
  441. }
  442. int
  443. main(int ac, char **av)
  444. {
  445. fprintf(stderr, "GLTest v1.0\nWritten by David Bucciarelli\n");
  446. if (ac == 2)
  447. frontbuffer = 0;
  448. glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
  449. glutInitWindowPosition(0, 0);
  450. glutInitWindowSize(640, 480);
  451. glutCreateWindow("OpenGL/Mesa Performances");
  452. glutDisplayFunc(display);
  453. glutMainLoop();
  454. return 0;
  455. }