Clone of mesa.
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

copypixrate.c 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /*
  2. * Measure glCopyPixels speed
  3. *
  4. * Brian Paul
  5. * 26 Jan 2006
  6. */
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <math.h>
  11. #include <GL/glew.h>
  12. #include <GL/glut.h>
  13. static GLint WinWidth = 1000, WinHeight = 800;
  14. static GLint ImgWidth, ImgHeight;
  15. static GLenum Buffer = GL_FRONT;
  16. static GLenum AlphaTest = GL_FALSE;
  17. static GLboolean UseBlit = GL_FALSE;
  18. static PFNGLBLITFRAMEBUFFEREXTPROC glBlitFramebufferEXT_func = NULL;
  19. /**
  20. * draw teapot in lower-left corner of window
  21. */
  22. static void
  23. DrawTestImage(void)
  24. {
  25. GLfloat ar;
  26. ImgWidth = WinWidth / 3;
  27. ImgHeight = WinHeight / 3;
  28. glViewport(0, 0, ImgWidth, ImgHeight);
  29. glScissor(0, 0, ImgWidth, ImgHeight);
  30. glEnable(GL_SCISSOR_TEST);
  31. glClearColor(0.5, 0.5, 0.5, 0.0);
  32. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  33. ar = (float) WinWidth / WinHeight;
  34. glMatrixMode(GL_PROJECTION);
  35. glLoadIdentity();
  36. glFrustum(-ar, ar, -1.0, 1.0, 5.0, 25.0);
  37. glMatrixMode(GL_MODELVIEW);
  38. glEnable(GL_LIGHTING);
  39. glEnable(GL_LIGHT0);
  40. glEnable(GL_DEPTH_TEST);
  41. glFrontFace(GL_CW);
  42. glPushMatrix();
  43. glRotatef(45, 1, 0, 0);
  44. glutSolidTeapot(2.0);
  45. glPopMatrix();
  46. glFrontFace(GL_CCW);
  47. glDisable(GL_DEPTH_TEST);
  48. glDisable(GL_LIGHTING);
  49. glDisable(GL_SCISSOR_TEST);
  50. glViewport(0, 0, WinWidth, WinHeight);
  51. glFinish();
  52. }
  53. static int
  54. Rand(int max)
  55. {
  56. return ((int) rand()) % max;
  57. }
  58. static void
  59. BlitOne(void)
  60. {
  61. int x, y;
  62. do {
  63. x = Rand(WinWidth);
  64. y = Rand(WinHeight);
  65. } while (x <= ImgWidth && y <= ImgHeight);
  66. #ifdef GL_EXT_framebuffer_blit
  67. if (UseBlit)
  68. {
  69. glBlitFramebufferEXT_func(0, 0, ImgWidth, ImgHeight,
  70. x, y, x + ImgWidth, y + ImgHeight,
  71. GL_COLOR_BUFFER_BIT, GL_LINEAR);
  72. }
  73. else
  74. #endif
  75. {
  76. glWindowPos2iARB(x, y);
  77. glCopyPixels(0, 0, ImgWidth, ImgHeight, GL_COLOR);
  78. }
  79. }
  80. /**
  81. * Measure glCopyPixels rate
  82. */
  83. static void
  84. RunTest(void)
  85. {
  86. double t1, t0 = glutGet(GLUT_ELAPSED_TIME) / 1000.0;
  87. int iters = 0;
  88. float copyRate, mbRate;
  89. int r, g, b, a, bpp;
  90. if (AlphaTest) {
  91. glEnable(GL_ALPHA_TEST);
  92. glAlphaFunc(GL_GREATER, 0.0);
  93. }
  94. glGetIntegerv(GL_RED_BITS, &r);
  95. glGetIntegerv(GL_GREEN_BITS, &g);
  96. glGetIntegerv(GL_BLUE_BITS, &b);
  97. glGetIntegerv(GL_ALPHA_BITS, &a);
  98. bpp = (r + g + b + a) / 8;
  99. do {
  100. BlitOne();
  101. if (Buffer == GL_FRONT)
  102. glFinish(); /* XXX to view progress */
  103. iters++;
  104. t1 = glutGet(GLUT_ELAPSED_TIME) / 1000.0;
  105. } while (t1 - t0 < 5.0);
  106. glDisable(GL_ALPHA_TEST);
  107. copyRate = iters / (t1 - t0);
  108. mbRate = ImgWidth * ImgHeight * bpp * copyRate / (1024 * 1024);
  109. printf("Image size: %d x %d, %d Bpp\n", ImgWidth, ImgHeight, bpp);
  110. printf("%d copies in %.2f = %.2f copies/sec, %.2f MB/s\n",
  111. iters, t1-t0, copyRate, mbRate);
  112. }
  113. static void
  114. Draw(void)
  115. {
  116. glClearColor(0.0, 0.0, 0.0, 0.0);
  117. glClearColor(0.2, 0.2, 0.8, 0);
  118. glReadBuffer(Buffer);
  119. glDrawBuffer(Buffer);
  120. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  121. DrawTestImage();
  122. RunTest();
  123. if (Buffer == GL_FRONT)
  124. glFinish();
  125. else
  126. glutSwapBuffers();
  127. #if 1
  128. printf("exiting\n");
  129. exit(0);
  130. #endif
  131. }
  132. static void
  133. Reshape(int width, int height)
  134. {
  135. glViewport(0, 0, width, height);
  136. glMatrixMode(GL_PROJECTION);
  137. glLoadIdentity();
  138. glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 25.0);
  139. glMatrixMode(GL_MODELVIEW);
  140. glLoadIdentity();
  141. glTranslatef(0.0, 0.0, -15.0);
  142. }
  143. static void
  144. Key(unsigned char key, int x, int y)
  145. {
  146. (void) x;
  147. (void) y;
  148. switch (key) {
  149. case 'b':
  150. BlitOne();
  151. break;
  152. case 27:
  153. exit(0);
  154. break;
  155. }
  156. glutPostRedisplay();
  157. }
  158. static void
  159. SpecialKey(int key, int x, int y)
  160. {
  161. (void) x;
  162. (void) y;
  163. switch (key) {
  164. case GLUT_KEY_UP:
  165. break;
  166. case GLUT_KEY_DOWN:
  167. break;
  168. case GLUT_KEY_LEFT:
  169. break;
  170. case GLUT_KEY_RIGHT:
  171. break;
  172. }
  173. glutPostRedisplay();
  174. }
  175. static void
  176. ParseArgs(int argc, char *argv[])
  177. {
  178. int i;
  179. for (i = 1; i < argc; i++) {
  180. if (strcmp(argv[i], "-back") == 0)
  181. Buffer = GL_BACK;
  182. else if (strcmp(argv[i], "-alpha") == 0)
  183. AlphaTest = GL_TRUE;
  184. else if (strcmp(argv[i], "-blit") == 0)
  185. UseBlit = GL_TRUE;
  186. }
  187. }
  188. static void
  189. Init(void)
  190. {
  191. if (glutExtensionSupported("GL_EXT_framebuffer_blit")) {
  192. glBlitFramebufferEXT_func = (PFNGLBLITFRAMEBUFFEREXTPROC)
  193. glutGetProcAddress("glBlitFramebufferEXT");
  194. }
  195. else if (UseBlit) {
  196. printf("Warning: GL_EXT_framebuffer_blit not supported.\n");
  197. UseBlit = GL_FALSE;
  198. }
  199. }
  200. int
  201. main(int argc, char *argv[])
  202. {
  203. GLint mode = GLUT_RGB | GLUT_ALPHA | GLUT_DOUBLE | GLUT_DEPTH;
  204. glutInit(&argc, argv);
  205. ParseArgs(argc, argv);
  206. if (AlphaTest)
  207. mode |= GLUT_ALPHA;
  208. glutInitWindowPosition(0, 0);
  209. glutInitWindowSize(WinWidth, WinHeight);
  210. glutInitDisplayMode(mode);
  211. glutCreateWindow(argv[0]);
  212. glewInit();
  213. glutReshapeFunc(Reshape);
  214. glutKeyboardFunc(Key);
  215. glutSpecialFunc(SpecialKey);
  216. glutDisplayFunc(Draw);
  217. printf("GL_RENDERER: %s\n", (char *) glGetString(GL_RENDERER));
  218. printf("Draw Buffer: %s\n", (Buffer == GL_BACK) ? "Back" : "Front");
  219. Init();
  220. glutMainLoop();
  221. return 0;
  222. }