2 * GL_ARB_pixel_buffer_object test
4 * Command line options:
5 * -w WIDTH -h HEIGHT sets window size
23 static GLuint DrawPBO
;
25 static GLboolean Animate
= GL_TRUE
;
26 static GLboolean use_pbo
= 1;
27 static GLboolean whole_rect
= 1;
29 static GLfloat Drift
= 0.0;
30 static GLfloat drift_increment
= 1/255.0;
31 static GLfloat Xrot
= 20.0, Yrot
= 30.0;
33 static GLuint Width
= 1024;
34 static GLuint Height
= 512;
37 static void Idle( void )
41 Drift
+= drift_increment
;
49 /*static int max( int a, int b ) { return a > b ? a : b; }*/
52 static int min( int a
, int b
) { return a
< b
? a
: b
; }
55 static void DrawObject()
57 GLint size
= Width
* Height
* 4;
60 /* XXX: This is extremely important - semantically makes the buffer
61 * contents undefined, but in practice means that the driver can
62 * release the old copy of the texture and allocate a new one
63 * without waiting for outstanding rendering to complete.
65 glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_EXT
, DrawPBO
);
66 glBufferDataARB(GL_PIXEL_UNPACK_BUFFER_EXT
, size
, NULL
, GL_STREAM_DRAW_ARB
);
69 char *image
= glMapBufferARB(GL_PIXEL_UNPACK_BUFFER_EXT
, GL_WRITE_ONLY_ARB
);
71 printf("char %d\n", (unsigned char)(Drift
* 255));
73 memset(image
, (unsigned char)(Drift
* 255), size
);
75 glUnmapBufferARB(GL_PIXEL_UNPACK_BUFFER_EXT
);
79 /* BGRA is required for most hardware paths:
81 glTexImage2D(GL_TEXTURE_RECTANGLE_ARB
, 0, GL_RGBA
, Width
, Height
, 0,
82 GL_BGRA
, GL_UNSIGNED_BYTE
, NULL
);
85 static char *image
= NULL
;
90 glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_EXT
, 0);
92 memset(image
, (unsigned char)(Drift
* 255), size
);
94 /* BGRA should be the fast path for regular uploads as well.
96 glTexImage2D(GL_TEXTURE_RECTANGLE_ARB
, 0, GL_RGBA
, Width
, Height
, 0,
97 GL_BGRA
, GL_UNSIGNED_BYTE
, image
);
119 glTexCoord2f( x
, y
+ h
);
120 glVertex2f( x
, y
+ h
);
122 glTexCoord2f( x
+ w
+ .5, y
+ h
);
123 glVertex2f( x
+ w
, y
+ h
);
125 glTexCoord2f( x
+ w
, y
+ .5);
126 glVertex2f( x
+ w
, y
);
134 static void Display( void )
137 static GLint Frames
= 0;
140 glClear( GL_COLOR_BUFFER_BIT
);
150 t
= glutGet(GLUT_ELAPSED_TIME
);
151 if (t
- T0
>= 1000) {
152 GLfloat seconds
= (t
- T0
) / 1000.0;
154 GLfloat fps
= Frames
/ seconds
;
155 printf("%d frames in %6.3f seconds = %6.3f FPS\n", Frames
, seconds
, fps
);
158 drift_increment
= 2.2 * seconds
/ Frames
;
165 static void Reshape( int width
, int height
)
167 glViewport( 0, 0, width
, height
);
168 glMatrixMode( GL_PROJECTION
);
170 /* glFrustum( -1.0, 1.0, -1.0, 1.0, 10.0, 100.0 ); */
171 gluOrtho2D( 0, width
, height
, 0 );
172 glMatrixMode( GL_MODELVIEW
);
174 glTranslatef(0.375, 0.375, 0);
178 static void ModeMenu(int entry
)
180 if (entry
==ANIMATE
) {
183 else if (entry
==PBO
) {
186 else if (entry
==QUIT
) {
194 static void Key( unsigned char key
, int x
, int y
)
207 static void SpecialKey( int key
, int x
, int y
)
231 static void Init( int argc
, char *argv
[] )
233 const char *exten
= (const char *) glGetString(GL_EXTENSIONS
);
238 if (!strstr(exten
, "GL_ARB_pixel_buffer_object")) {
239 printf("Sorry, GL_ARB_pixel_buffer_object not supported by this renderer.\n");
243 glGetIntegerv(GL_MAX_TEXTURE_SIZE
, &size
);
244 printf("%d x %d max texture size\n", size
, size
);
246 glPixelStorei(GL_UNPACK_ALIGNMENT
, 1);
248 /* allocate two texture objects */
249 glGenTextures(1, &texObj
);
251 /* setup the texture objects */
252 glActiveTextureARB(GL_TEXTURE0_ARB
);
253 glBindTexture(GL_TEXTURE_RECTANGLE_ARB
, texObj
);
255 glTexParameteri(GL_TEXTURE_RECTANGLE_ARB
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
256 glTexParameteri(GL_TEXTURE_RECTANGLE_ARB
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
258 glGenBuffersARB(1, &DrawPBO
);
260 glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_EXT
, DrawPBO
);
261 glBufferDataARB(GL_PIXEL_UNPACK_BUFFER_EXT
,
262 Width
* Height
* 4, NULL
, GL_STREAM_DRAW
);
264 glTexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
266 glEnable(GL_TEXTURE_RECTANGLE_ARB
);
268 glShadeModel(GL_SMOOTH
);
269 glClearColor(0.3, 0.3, 0.4, 1.0);
271 if (argc
> 1 && strcmp(argv
[1], "-info")==0) {
272 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER
));
273 printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION
));
274 printf("GL_VENDOR = %s\n", (char *) glGetString(GL_VENDOR
));
275 printf("GL_EXTENSIONS = %s\n", (char *) glGetString(GL_EXTENSIONS
));
280 int main( int argc
, char *argv
[] )
284 glutInit( &argc
, argv
);
286 for (i
= 1; i
< argc
; i
++) {
287 if (strcmp(argv
[i
], "-w") == 0) {
288 Width
= atoi(argv
[i
+1]);
290 printf("Error, bad width\n");
295 else if (strcmp(argv
[i
], "-h") == 0) {
296 Height
= atoi(argv
[i
+1]);
298 printf("Error, bad height\n");
305 glutInitWindowSize( Width
, Height
);
306 glutInitWindowPosition( 0, 0 );
307 glutInitDisplayMode( GLUT_RGB
| GLUT_DOUBLE
);
308 glutCreateWindow(argv
[0] );
313 glutReshapeFunc( Reshape
);
314 glutKeyboardFunc( Key
);
315 glutSpecialFunc( SpecialKey
);
316 glutDisplayFunc( Display
);
317 glutIdleFunc( Idle
);
319 glutCreateMenu(ModeMenu
);
320 glutAddMenuEntry("Toggle Animation", ANIMATE
);
321 glutAddMenuEntry("Toggle PBO", PBO
);
322 glutAddMenuEntry("Quit", QUIT
);
323 glutAttachMenu(GLUT_RIGHT_BUTTON
);