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.

streaming_rect.c 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /*
  2. * GL_ARB_pixel_buffer_object test
  3. *
  4. * Command line options:
  5. * -w WIDTH -h HEIGHT sets window size
  6. *
  7. */
  8. #define GL_GLEXT_PROTOTYPES
  9. #include <math.h>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <GL/glut.h>
  14. #include "readtex.h"
  15. #define ANIMATE 10
  16. #define PBO 11
  17. #define QUIT 100
  18. static GLuint DrawPBO;
  19. static GLboolean Animate = GL_TRUE;
  20. static GLboolean use_pbo = 1;
  21. static GLboolean whole_rect = 1;
  22. static GLfloat Drift = 0.0;
  23. static GLfloat drift_increment = 1/255.0;
  24. static GLfloat Xrot = 20.0, Yrot = 30.0;
  25. static GLuint Width = 1024;
  26. static GLuint Height = 512;
  27. static void Idle( void )
  28. {
  29. if (Animate) {
  30. Drift += drift_increment;
  31. if (Drift >= 1.0)
  32. Drift = 0.0;
  33. glutPostRedisplay();
  34. }
  35. }
  36. /*static int max( int a, int b ) { return a > b ? a : b; }*/
  37. static int min( int a, int b ) { return a < b ? a : b; }
  38. static void DrawObject()
  39. {
  40. GLint size = Width * Height * 4;
  41. if (use_pbo) {
  42. /* XXX: This is extremely important - semantically makes the buffer
  43. * contents undefined, but in practice means that the driver can
  44. * release the old copy of the texture and allocate a new one
  45. * without waiting for outstanding rendering to complete.
  46. */
  47. glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_EXT, DrawPBO);
  48. glBufferDataARB(GL_PIXEL_UNPACK_BUFFER_EXT, size, NULL, GL_STREAM_DRAW_ARB);
  49. {
  50. char *image = glMapBufferARB(GL_PIXEL_UNPACK_BUFFER_EXT, GL_WRITE_ONLY_ARB);
  51. printf("char %d\n", (unsigned char)(Drift * 255));
  52. memset(image, (unsigned char)(Drift * 255), size);
  53. glUnmapBufferARB(GL_PIXEL_UNPACK_BUFFER_EXT);
  54. }
  55. /* BGRA is required for most hardware paths:
  56. */
  57. glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA, Width, Height, 0,
  58. GL_BGRA, GL_UNSIGNED_BYTE, NULL);
  59. }
  60. else {
  61. static char *image = NULL;
  62. if (image == NULL)
  63. image = malloc(size);
  64. glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_EXT, 0);
  65. memset(image, (unsigned char)(Drift * 255), size);
  66. /* BGRA should be the fast path for regular uploads as well.
  67. */
  68. glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA, Width, Height, 0,
  69. GL_BGRA, GL_UNSIGNED_BYTE, image);
  70. }
  71. {
  72. int x,y,w,h;
  73. if (whole_rect) {
  74. x = y = 0;
  75. w = Width;
  76. h = Height;
  77. }
  78. else {
  79. x = y = 0;
  80. w = min(10, Width);
  81. h = min(10, Height);
  82. }
  83. glBegin(GL_QUADS);
  84. glTexCoord2f( x, y);
  85. glVertex2f( x, y );
  86. glTexCoord2f( x, y + h);
  87. glVertex2f( x, y + h);
  88. glTexCoord2f( x + w + .5, y + h);
  89. glVertex2f( x + w, y + h );
  90. glTexCoord2f( x + w, y + .5);
  91. glVertex2f( x + w, y );
  92. glEnd();
  93. }
  94. }
  95. static void Display( void )
  96. {
  97. static GLint T0 = 0;
  98. static GLint Frames = 0;
  99. GLint t;
  100. glClear( GL_COLOR_BUFFER_BIT );
  101. glPushMatrix();
  102. DrawObject();
  103. glPopMatrix();
  104. glutSwapBuffers();
  105. Frames++;
  106. t = glutGet(GLUT_ELAPSED_TIME);
  107. if (t - T0 >= 1000) {
  108. GLfloat seconds = (t - T0) / 1000.0;
  109. GLfloat fps = Frames / seconds;
  110. printf("%d frames in %6.3f seconds = %6.3f FPS\n", Frames, seconds, fps);
  111. drift_increment = 2.2 * seconds / Frames;
  112. T0 = t;
  113. Frames = 0;
  114. }
  115. }
  116. static void Reshape( int width, int height )
  117. {
  118. glViewport( 0, 0, width, height );
  119. glMatrixMode( GL_PROJECTION );
  120. glLoadIdentity();
  121. /* glFrustum( -1.0, 1.0, -1.0, 1.0, 10.0, 100.0 ); */
  122. gluOrtho2D( 0, width, height, 0 );
  123. glMatrixMode( GL_MODELVIEW );
  124. glLoadIdentity();
  125. glTranslatef(0.375, 0.375, 0);
  126. }
  127. static void ModeMenu(int entry)
  128. {
  129. if (entry==ANIMATE) {
  130. Animate = !Animate;
  131. }
  132. else if (entry==PBO) {
  133. use_pbo = !use_pbo;
  134. }
  135. else if (entry==QUIT) {
  136. exit(0);
  137. }
  138. glutPostRedisplay();
  139. }
  140. static void Key( unsigned char key, int x, int y )
  141. {
  142. (void) x;
  143. (void) y;
  144. switch (key) {
  145. case 27:
  146. exit(0);
  147. break;
  148. }
  149. glutPostRedisplay();
  150. }
  151. static void SpecialKey( int key, int x, int y )
  152. {
  153. float step = 3.0;
  154. (void) x;
  155. (void) y;
  156. switch (key) {
  157. case GLUT_KEY_UP:
  158. Xrot += step;
  159. break;
  160. case GLUT_KEY_DOWN:
  161. Xrot -= step;
  162. break;
  163. case GLUT_KEY_LEFT:
  164. Yrot += step;
  165. break;
  166. case GLUT_KEY_RIGHT:
  167. Yrot -= step;
  168. break;
  169. }
  170. glutPostRedisplay();
  171. }
  172. static void Init( int argc, char *argv[] )
  173. {
  174. const char *exten = (const char *) glGetString(GL_EXTENSIONS);
  175. GLuint texObj;
  176. GLint size;
  177. if (!strstr(exten, "GL_ARB_pixel_buffer_object")) {
  178. printf("Sorry, GL_ARB_pixel_buffer_object not supported by this renderer.\n");
  179. exit(1);
  180. }
  181. glGetIntegerv(GL_MAX_TEXTURE_SIZE, &size);
  182. printf("%d x %d max texture size\n", size, size);
  183. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  184. /* allocate two texture objects */
  185. glGenTextures(1, &texObj);
  186. /* setup the texture objects */
  187. glActiveTextureARB(GL_TEXTURE0_ARB);
  188. glBindTexture(GL_TEXTURE_RECTANGLE_ARB, texObj);
  189. glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  190. glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  191. glGenBuffersARB(1, &DrawPBO);
  192. glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_EXT, DrawPBO);
  193. glBufferDataARB(GL_PIXEL_UNPACK_BUFFER_EXT,
  194. Width * Height * 4, NULL, GL_STREAM_DRAW);
  195. glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
  196. glEnable(GL_TEXTURE_RECTANGLE_ARB);
  197. glShadeModel(GL_SMOOTH);
  198. glClearColor(0.3, 0.3, 0.4, 1.0);
  199. if (argc > 1 && strcmp(argv[1], "-info")==0) {
  200. printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
  201. printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION));
  202. printf("GL_VENDOR = %s\n", (char *) glGetString(GL_VENDOR));
  203. printf("GL_EXTENSIONS = %s\n", (char *) glGetString(GL_EXTENSIONS));
  204. }
  205. }
  206. int main( int argc, char *argv[] )
  207. {
  208. GLint i;
  209. glutInit( &argc, argv );
  210. for (i = 1; i < argc; i++) {
  211. if (strcmp(argv[i], "-w") == 0) {
  212. Width = atoi(argv[i+1]);
  213. if (Width <= 0) {
  214. printf("Error, bad width\n");
  215. exit(1);
  216. }
  217. i++;
  218. }
  219. else if (strcmp(argv[i], "-h") == 0) {
  220. Height = atoi(argv[i+1]);
  221. if (Height <= 0) {
  222. printf("Error, bad height\n");
  223. exit(1);
  224. }
  225. i++;
  226. }
  227. }
  228. glutInitWindowSize( Width, Height );
  229. glutInitWindowPosition( 0, 0 );
  230. glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
  231. glutCreateWindow(argv[0] );
  232. Init( argc, argv );
  233. glutReshapeFunc( Reshape );
  234. glutKeyboardFunc( Key );
  235. glutSpecialFunc( SpecialKey );
  236. glutDisplayFunc( Display );
  237. glutIdleFunc( Idle );
  238. glutCreateMenu(ModeMenu);
  239. glutAddMenuEntry("Toggle Animation", ANIMATE);
  240. glutAddMenuEntry("Toggle PBO", PBO);
  241. glutAddMenuEntry("Quit", QUIT);
  242. glutAttachMenu(GLUT_RIGHT_BUTTON);
  243. glutMainLoop();
  244. return 0;
  245. }