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; }*/
50 static int min( int a
, int b
) { return a
< b
? a
: b
; }
52 static void DrawObject()
54 GLint size
= Width
* Height
* 4;
57 /* XXX: This is extremely important - semantically makes the buffer
58 * contents undefined, but in practice means that the driver can
59 * release the old copy of the texture and allocate a new one
60 * without waiting for outstanding rendering to complete.
62 glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_EXT
, DrawPBO
);
63 glBufferDataARB(GL_PIXEL_UNPACK_BUFFER_EXT
, size
, NULL
, GL_STREAM_DRAW_ARB
);
66 char *image
= glMapBufferARB(GL_PIXEL_UNPACK_BUFFER_EXT
, GL_WRITE_ONLY_ARB
);
68 printf("char %d\n", (unsigned char)(Drift
* 255));
70 memset(image
, (unsigned char)(Drift
* 255), size
);
72 glUnmapBufferARB(GL_PIXEL_UNPACK_BUFFER_EXT
);
76 /* BGRA is required for most hardware paths:
78 glTexImage2D(GL_TEXTURE_RECTANGLE_ARB
, 0, GL_RGBA
, Width
, Height
, 0,
79 GL_BGRA
, GL_UNSIGNED_BYTE
, NULL
);
82 static char *image
= NULL
;
87 glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_EXT
, 0);
89 memset(image
, (unsigned char)(Drift
* 255), size
);
91 /* BGRA should be the fast path for regular uploads as well.
93 glTexImage2D(GL_TEXTURE_RECTANGLE_ARB
, 0, GL_RGBA
, Width
, Height
, 0,
94 GL_BGRA
, GL_UNSIGNED_BYTE
, image
);
116 glTexCoord2f( x
, y
+ h
);
117 glVertex2f( x
, y
+ h
);
119 glTexCoord2f( x
+ w
+ .5, y
+ h
);
120 glVertex2f( x
+ w
, y
+ h
);
122 glTexCoord2f( x
+ w
, y
+ .5);
123 glVertex2f( x
+ w
, y
);
131 static void Display( void )
134 static GLint Frames
= 0;
137 glClear( GL_COLOR_BUFFER_BIT
);
147 t
= glutGet(GLUT_ELAPSED_TIME
);
148 if (t
- T0
>= 1000) {
149 GLfloat seconds
= (t
- T0
) / 1000.0;
151 GLfloat fps
= Frames
/ seconds
;
152 printf("%d frames in %6.3f seconds = %6.3f FPS\n", Frames
, seconds
, fps
);
154 drift_increment
= 2.2 * seconds
/ Frames
;
161 static void Reshape( int width
, int height
)
163 glViewport( 0, 0, width
, height
);
164 glMatrixMode( GL_PROJECTION
);
166 /* glFrustum( -1.0, 1.0, -1.0, 1.0, 10.0, 100.0 ); */
167 gluOrtho2D( 0, width
, height
, 0 );
168 glMatrixMode( GL_MODELVIEW
);
170 glTranslatef(0.375, 0.375, 0);
174 static void ModeMenu(int entry
)
176 if (entry
==ANIMATE
) {
179 else if (entry
==PBO
) {
182 else if (entry
==QUIT
) {
190 static void Key( unsigned char key
, int x
, int y
)
203 static void SpecialKey( int key
, int x
, int y
)
227 static void Init( int argc
, char *argv
[] )
229 const char *exten
= (const char *) glGetString(GL_EXTENSIONS
);
234 if (!strstr(exten
, "GL_ARB_pixel_buffer_object")) {
235 printf("Sorry, GL_ARB_pixel_buffer_object not supported by this renderer.\n");
239 glGetIntegerv(GL_MAX_TEXTURE_SIZE
, &size
);
240 printf("%d x %d max texture size\n", size
, size
);
242 glPixelStorei(GL_UNPACK_ALIGNMENT
, 1);
244 /* allocate two texture objects */
245 glGenTextures(1, &texObj
);
247 /* setup the texture objects */
248 glActiveTextureARB(GL_TEXTURE0_ARB
);
249 glBindTexture(GL_TEXTURE_RECTANGLE_ARB
, texObj
);
251 glTexParameteri(GL_TEXTURE_RECTANGLE_ARB
, GL_TEXTURE_MIN_FILTER
, GL_NEAREST
);
252 glTexParameteri(GL_TEXTURE_RECTANGLE_ARB
, GL_TEXTURE_MAG_FILTER
, GL_NEAREST
);
254 glGenBuffersARB(1, &DrawPBO
);
256 glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_EXT
, DrawPBO
);
257 glBufferDataARB(GL_PIXEL_UNPACK_BUFFER_EXT
,
258 Width
* Height
* 4, NULL
, GL_STREAM_DRAW
);
260 glTexEnvi(GL_TEXTURE_ENV
, GL_TEXTURE_ENV_MODE
, GL_REPLACE
);
262 glEnable(GL_TEXTURE_RECTANGLE_ARB
);
264 glShadeModel(GL_SMOOTH
);
265 glClearColor(0.3, 0.3, 0.4, 1.0);
267 if (argc
> 1 && strcmp(argv
[1], "-info")==0) {
268 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER
));
269 printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION
));
270 printf("GL_VENDOR = %s\n", (char *) glGetString(GL_VENDOR
));
271 printf("GL_EXTENSIONS = %s\n", (char *) glGetString(GL_EXTENSIONS
));
276 int main( int argc
, char *argv
[] )
280 glutInit( &argc
, argv
);
282 for (i
= 1; i
< argc
; i
++) {
283 if (strcmp(argv
[i
], "-w") == 0) {
284 Width
= atoi(argv
[i
+1]);
286 printf("Error, bad width\n");
291 else if (strcmp(argv
[i
], "-h") == 0) {
292 Height
= atoi(argv
[i
+1]);
294 printf("Error, bad height\n");
301 glutInitWindowSize( Width
, Height
);
302 glutInitWindowPosition( 0, 0 );
303 glutInitDisplayMode( GLUT_RGB
| GLUT_DOUBLE
);
304 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
);