2 * GL_ARB_pixel_buffer_object test
4 * Command line options:
5 * -w WIDTH -h HEIGHT sets window size
9 #define GL_GLEXT_PROTOTYPES
24 static GLuint DrawPBO
;
26 static GLboolean Animate
= GL_TRUE
;
27 static GLboolean use_pbo
= 1;
28 static GLboolean whole_rect
= 1;
30 static GLfloat Drift
= 0.0;
31 static GLfloat drift_increment
= 1/255.0;
32 static GLfloat Xrot
= 20.0, Yrot
= 30.0;
34 static GLuint Width
= 1024;
35 static GLuint Height
= 512;
38 static void Idle( void )
42 Drift
+= drift_increment
;
50 /*static int max( int a, int b ) { return a > b ? a : b; }*/
51 static int min( int a
, int b
) { return a
< b
? a
: b
; }
53 static void DrawObject()
55 GLint size
= Width
* Height
* 4;
58 /* XXX: This is extremely important - semantically makes the buffer
59 * contents undefined, but in practice means that the driver can
60 * release the old copy of the texture and allocate a new one
61 * without waiting for outstanding rendering to complete.
63 glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_EXT
, DrawPBO
);
64 glBufferDataARB(GL_PIXEL_UNPACK_BUFFER_EXT
, size
, NULL
, GL_STREAM_DRAW_ARB
);
67 char *image
= glMapBufferARB(GL_PIXEL_UNPACK_BUFFER_EXT
, GL_WRITE_ONLY_ARB
);
69 printf("char %d\n", (unsigned char)(Drift
* 255));
71 memset(image
, (unsigned char)(Drift
* 255), size
);
73 glUnmapBufferARB(GL_PIXEL_UNPACK_BUFFER_EXT
);
77 /* BGRA is required for most hardware paths:
79 glTexImage2D(GL_TEXTURE_RECTANGLE_ARB
, 0, GL_RGBA
, Width
, Height
, 0,
80 GL_BGRA
, GL_UNSIGNED_BYTE
, NULL
);
83 static char *image
= NULL
;
88 glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_EXT
, 0);
90 memset(image
, (unsigned char)(Drift
* 255), size
);
92 /* BGRA should be the fast path for regular uploads as well.
94 glTexImage2D(GL_TEXTURE_RECTANGLE_ARB
, 0, GL_RGBA
, Width
, Height
, 0,
95 GL_BGRA
, GL_UNSIGNED_BYTE
, image
);
117 glTexCoord2f( x
, y
+ h
);
118 glVertex2f( x
, y
+ h
);
120 glTexCoord2f( x
+ w
+ .5, y
+ h
);
121 glVertex2f( x
+ w
, y
+ h
);
123 glTexCoord2f( x
+ w
, y
+ .5);
124 glVertex2f( x
+ w
, y
);
132 static void Display( void )
135 static GLint Frames
= 0;
138 glClear( GL_COLOR_BUFFER_BIT
);
148 t
= glutGet(GLUT_ELAPSED_TIME
);
149 if (t
- T0
>= 1000) {
150 GLfloat seconds
= (t
- T0
) / 1000.0;
152 GLfloat fps
= Frames
/ seconds
;
153 printf("%d frames in %6.3f seconds = %6.3f FPS\n", Frames
, seconds
, fps
);
155 drift_increment
= 2.2 * seconds
/ Frames
;
162 static void Reshape( int width
, int height
)
164 glViewport( 0, 0, width
, height
);
165 glMatrixMode( GL_PROJECTION
);
167 /* glFrustum( -1.0, 1.0, -1.0, 1.0, 10.0, 100.0 ); */
168 gluOrtho2D( 0, width
, height
, 0 );
169 glMatrixMode( GL_MODELVIEW
);
171 glTranslatef(0.375, 0.375, 0);
175 static void ModeMenu(int entry
)
177 if (entry
==ANIMATE
) {
180 else if (entry
==PBO
) {
183 else if (entry
==QUIT
) {
191 static void Key( unsigned char key
, int x
, int y
)
204 static void SpecialKey( int key
, int x
, int y
)
228 static void Init( int argc
, char *argv
[] )
230 const char *exten
= (const char *) glGetString(GL_EXTENSIONS
);
235 if (!strstr(exten
, "GL_ARB_pixel_buffer_object")) {
236 printf("Sorry, GL_ARB_pixel_buffer_object not supported by this renderer.\n");
240 glGetIntegerv(GL_MAX_TEXTURE_SIZE
, &size
);
241 printf("%d x %d max texture size\n", size
, size
);
243 glPixelStorei(GL_UNPACK_ALIGNMENT
, 1);
245 /* allocate two texture objects */
246 glGenTextures(1, &texObj
);
248 /* setup the texture objects */
249 glActiveTextureARB(GL_TEXTURE0_ARB
);
250 glBindTexture(GL_TEXTURE_RECTANGLE_ARB
, texObj
);
252 glTexParameteri(GL_TEXTURE_RECTANGLE_ARB
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
253 glTexParameteri(GL_TEXTURE_RECTANGLE_ARB
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
255 glGenBuffersARB(1, &DrawPBO
);
257 glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_EXT
, DrawPBO
);
258 glBufferDataARB(GL_PIXEL_UNPACK_BUFFER_EXT
,
259 Width
* Height
* 4, NULL
, GL_STREAM_DRAW
);
261 glTexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
263 glEnable(GL_TEXTURE_RECTANGLE_ARB
);
265 glShadeModel(GL_SMOOTH
);
266 glClearColor(0.3, 0.3, 0.4, 1.0);
268 if (argc
> 1 && strcmp(argv
[1], "-info")==0) {
269 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER
));
270 printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION
));
271 printf("GL_VENDOR = %s\n", (char *) glGetString(GL_VENDOR
));
272 printf("GL_EXTENSIONS = %s\n", (char *) glGetString(GL_EXTENSIONS
));
277 int main( int argc
, char *argv
[] )
281 glutInit( &argc
, argv
);
283 for (i
= 1; i
< argc
; i
++) {
284 if (strcmp(argv
[i
], "-w") == 0) {
285 Width
= atoi(argv
[i
+1]);
287 printf("Error, bad width\n");
292 else if (strcmp(argv
[i
], "-h") == 0) {
293 Height
= atoi(argv
[i
+1]);
295 printf("Error, bad height\n");
302 glutInitWindowSize( Width
, Height
);
303 glutInitWindowPosition( 0, 0 );
304 glutInitDisplayMode( GLUT_RGB
| GLUT_DOUBLE
);
305 glutCreateWindow(argv
[0] );
309 glutReshapeFunc( Reshape
);
310 glutKeyboardFunc( Key
);
311 glutSpecialFunc( SpecialKey
);
312 glutDisplayFunc( Display
);
313 glutIdleFunc( Idle
);
315 glutCreateMenu(ModeMenu
);
316 glutAddMenuEntry("Toggle Animation", ANIMATE
);
317 glutAddMenuEntry("Toggle PBO", PBO
);
318 glutAddMenuEntry("Quit", QUIT
);
319 glutAttachMenu(GLUT_RIGHT_BUTTON
);