Clone of mesa.
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

pbo.c 7.3KB

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