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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /*
  2. * GL_EXT_pixel_buffer_object test
  3. *
  4. * Brian Paul
  5. * 11 March 2004
  6. */
  7. #define GL_GLEXT_PROTOTYPES
  8. #include <assert.h>
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <math.h>
  12. #include <GL/glut.h>
  13. #include "../util/readtex.c" /* a hack, I know */
  14. #define IMAGE_FILE "../images/girl.rgb"
  15. static int ImgWidth, ImgHeight;
  16. static GLenum ImgFormat;
  17. static GLubyte *Image = NULL;
  18. static int APosX, APosY; /* simple drawpixels */
  19. static int BPosX, BPosY; /* read/draw pixels */
  20. static int CPosX, CPosY; /* copypixels */
  21. static GLboolean DrawFront = GL_FALSE;
  22. static GLboolean ScaleAndBias = GL_FALSE;
  23. static GLboolean Benchmark = GL_FALSE;
  24. static GLuint DrawPBO, TempPBO;
  25. static GLenum ReadFormat = GL_RGBA;
  26. static GLenum ReadType = GL_UNSIGNED_BYTE;
  27. static void
  28. Reset( void )
  29. {
  30. APosX = 5; APosY = 20;
  31. BPosX = APosX + ImgWidth + 5; BPosY = 20;
  32. CPosX = BPosX + ImgWidth + 5; CPosY = 20;
  33. }
  34. static void
  35. PrintString(const char *s)
  36. {
  37. while (*s) {
  38. glutBitmapCharacter(GLUT_BITMAP_8_BY_13, (int) *s);
  39. s++;
  40. }
  41. }
  42. static void
  43. SetupPixelTransfer(GLboolean invert)
  44. {
  45. if (invert) {
  46. glPixelTransferf(GL_RED_SCALE, -1.0);
  47. glPixelTransferf(GL_RED_BIAS, 1.0);
  48. glPixelTransferf(GL_GREEN_SCALE, -1.0);
  49. glPixelTransferf(GL_GREEN_BIAS, 1.0);
  50. glPixelTransferf(GL_BLUE_SCALE, -1.0);
  51. glPixelTransferf(GL_BLUE_BIAS, 1.0);
  52. }
  53. else {
  54. glPixelTransferf(GL_RED_SCALE, 1.0);
  55. glPixelTransferf(GL_RED_BIAS, 0.0);
  56. glPixelTransferf(GL_GREEN_SCALE, 1.0);
  57. glPixelTransferf(GL_GREEN_BIAS, 0.0);
  58. glPixelTransferf(GL_BLUE_SCALE, 1.0);
  59. glPixelTransferf(GL_BLUE_BIAS, 0.0);
  60. }
  61. }
  62. static void
  63. Display( void )
  64. {
  65. glClearColor(.3, .3, .3, 1);
  66. glClear( GL_COLOR_BUFFER_BIT );
  67. /** Unbind UNPACK pixel buffer before calling glBitmap */
  68. glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_EXT, 0);
  69. glRasterPos2i(5, ImgHeight+25);
  70. PrintString("f = toggle front/back s = toggle scale/bias b = benchmark");
  71. glRasterPos2i(5, ImgHeight+40);
  72. PrintString("GL_EXT_pixel_buffer_object test");
  73. /* draw original image */
  74. glRasterPos2i(APosX, 5);
  75. PrintString("Original");
  76. glRasterPos2i(APosX, APosY);
  77. glEnable(GL_DITHER);
  78. SetupPixelTransfer(GL_FALSE);
  79. /*** Draw from the DrawPBO */
  80. glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_EXT, DrawPBO);
  81. glDrawPixels(ImgWidth, ImgHeight, ImgFormat, GL_UNSIGNED_BYTE, 0);
  82. glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_EXT, 0);
  83. /* do readpixels, drawpixels */
  84. glRasterPos2i(BPosX, 5);
  85. PrintString("Read/DrawPixels");
  86. SetupPixelTransfer(ScaleAndBias);
  87. /*** read into the Temp PBO */
  88. glBindBufferARB(GL_PIXEL_PACK_BUFFER_EXT, TempPBO);
  89. if (Benchmark) {
  90. GLint reads = 0;
  91. GLint endTime;
  92. GLint startTime = glutGet(GLUT_ELAPSED_TIME);
  93. GLdouble seconds, pixelsPerSecond;
  94. printf("Benchmarking...\n");
  95. do {
  96. glReadPixels(APosX, APosY, ImgWidth, ImgHeight,
  97. ReadFormat, ReadType, 0);
  98. reads++;
  99. endTime = glutGet(GLUT_ELAPSED_TIME);
  100. } while (endTime - startTime < 4000); /* 4 seconds */
  101. seconds = (double) (endTime - startTime) / 1000.0;
  102. pixelsPerSecond = reads * ImgWidth * ImgHeight / seconds;
  103. printf("Result: %d reads in %f seconds = %f pixels/sec\n",
  104. reads, seconds, pixelsPerSecond);
  105. Benchmark = GL_FALSE;
  106. }
  107. else {
  108. glReadPixels(APosX, APosY, ImgWidth, ImgHeight,
  109. ReadFormat, ReadType, 0);
  110. }
  111. glRasterPos2i(BPosX, BPosY);
  112. glDisable(GL_DITHER);
  113. SetupPixelTransfer(GL_FALSE);
  114. /*** draw from the Temp PBO */
  115. glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_EXT, TempPBO);
  116. glDrawPixels(ImgWidth, ImgHeight, ReadFormat, ReadType, 0);
  117. glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_EXT, 0);
  118. /* do copypixels */
  119. glRasterPos2i(CPosX, 5);
  120. PrintString("CopyPixels");
  121. glRasterPos2i(CPosX, CPosY);
  122. glDisable(GL_DITHER);
  123. SetupPixelTransfer(ScaleAndBias);
  124. glCopyPixels(APosX, APosY, ImgWidth, ImgHeight, GL_COLOR);
  125. if (!DrawFront)
  126. glutSwapBuffers();
  127. else
  128. glFinish();
  129. }
  130. static void
  131. Reshape( int width, int height )
  132. {
  133. glViewport( 0, 0, width, height );
  134. glMatrixMode( GL_PROJECTION );
  135. glLoadIdentity();
  136. glOrtho( 0.0, width, 0.0, height, -1.0, 1.0 );
  137. glMatrixMode( GL_MODELVIEW );
  138. glLoadIdentity();
  139. }
  140. static void
  141. Key( unsigned char key, int x, int y )
  142. {
  143. (void) x;
  144. (void) y;
  145. switch (key) {
  146. case 'b':
  147. Benchmark = GL_TRUE;
  148. break;
  149. case 's':
  150. ScaleAndBias = !ScaleAndBias;
  151. break;
  152. case 'f':
  153. DrawFront = !DrawFront;
  154. if (DrawFront) {
  155. glDrawBuffer(GL_FRONT);
  156. glReadBuffer(GL_FRONT);
  157. }
  158. else {
  159. glDrawBuffer(GL_BACK);
  160. glReadBuffer(GL_BACK);
  161. }
  162. printf("glDrawBuffer(%s)\n", DrawFront ? "GL_FRONT" : "GL_BACK");
  163. break;
  164. case 27:
  165. exit(0);
  166. break;
  167. }
  168. glutPostRedisplay();
  169. }
  170. static void
  171. Init( GLboolean ciMode )
  172. {
  173. printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION));
  174. printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
  175. if (!glutExtensionSupported("GL_EXT_pixel_buffer_object")) {
  176. printf("Sorry, this demo requires GL_EXT_pixel_buffer_object\n");
  177. exit(0);
  178. }
  179. Image = LoadRGBImage( IMAGE_FILE, &ImgWidth, &ImgHeight, &ImgFormat );
  180. if (!Image) {
  181. printf("Couldn't read %s\n", IMAGE_FILE);
  182. exit(0);
  183. }
  184. if (ciMode) {
  185. /* Convert RGB image to grayscale */
  186. GLubyte *indexImage = (GLubyte *) malloc( ImgWidth * ImgHeight );
  187. GLint i;
  188. for (i=0; i<ImgWidth*ImgHeight; i++) {
  189. int gray = Image[i*3] + Image[i*3+1] + Image[i*3+2];
  190. indexImage[i] = gray / 3;
  191. }
  192. free(Image);
  193. Image = indexImage;
  194. ImgFormat = GL_COLOR_INDEX;
  195. for (i=0;i<255;i++) {
  196. float g = i / 255.0;
  197. glutSetColor(i, g, g, g);
  198. }
  199. }
  200. printf("Loaded %d by %d image\n", ImgWidth, ImgHeight );
  201. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  202. glPixelStorei(GL_UNPACK_ROW_LENGTH, ImgWidth);
  203. glPixelStorei(GL_PACK_ALIGNMENT, 1);
  204. glPixelStorei(GL_PACK_ROW_LENGTH, ImgWidth);
  205. Reset();
  206. /* put image into DrawPBO */
  207. glGenBuffersARB(1, &DrawPBO);
  208. glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_EXT, DrawPBO);
  209. glBufferDataARB(GL_PIXEL_UNPACK_BUFFER_EXT,
  210. ImgWidth * ImgHeight * 4, Image, GL_STATIC_DRAW);
  211. /* Setup TempPBO - used for glReadPixels & glDrawPixels */
  212. glGenBuffersARB(1, &TempPBO);
  213. glBindBufferARB(GL_PIXEL_PACK_BUFFER_EXT, TempPBO);
  214. glBufferDataARB(GL_PIXEL_PACK_BUFFER_EXT,
  215. ImgWidth * ImgHeight * 4, NULL, GL_DYNAMIC_COPY);
  216. }
  217. int
  218. main( int argc, char *argv[] )
  219. {
  220. GLboolean ciMode = GL_FALSE;
  221. if (argc > 1 && strcmp(argv[1], "-ci")==0) {
  222. ciMode = GL_TRUE;
  223. }
  224. glutInit( &argc, argv );
  225. glutInitWindowPosition( 0, 0 );
  226. glutInitWindowSize( 750, 250 );
  227. if (ciMode)
  228. glutInitDisplayMode( GLUT_INDEX | GLUT_DOUBLE );
  229. else
  230. glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
  231. glutCreateWindow(argv[0]);
  232. Init(ciMode);
  233. glutReshapeFunc( Reshape );
  234. glutKeyboardFunc( Key );
  235. glutDisplayFunc( Display );
  236. glutMainLoop();
  237. return 0;
  238. }