blob: 4c1d8535fd78f92f13769f8b2a7144f72e8d4992 [file] [log] [blame]
Keith Whitwell48e6fff2006-11-01 14:26:10 +00001/*
Brian55d4f322007-10-24 13:55:22 -06002 * GL_ARB_pixel_buffer_object test
Keith Whitwell48e6fff2006-11-01 14:26:10 +00003 *
4 * Command line options:
Brian55d4f322007-10-24 13:55:22 -06005 * -w WIDTH -h HEIGHT sets window size
Keith Whitwell48e6fff2006-11-01 14:26:10 +00006 *
Keith Whitwell48e6fff2006-11-01 14:26:10 +00007 */
8
9#define GL_GLEXT_PROTOTYPES
10
11#include <math.h>
12#include <stdio.h>
13#include <stdlib.h>
14#include <string.h>
15#include <GL/glut.h>
16
17#include "readtex.h"
18
19
20#define ANIMATE 10
21#define PBO 11
22#define QUIT 100
23
Brian55d4f322007-10-24 13:55:22 -060024static GLuint DrawPBO;
25
Keith Whitwell48e6fff2006-11-01 14:26:10 +000026static GLboolean Animate = GL_TRUE;
27static GLboolean use_pbo = 1;
28static GLboolean whole_rect = 1;
29
30static GLfloat Drift = 0.0;
31static GLfloat drift_increment = 1/255.0;
32static GLfloat Xrot = 20.0, Yrot = 30.0;
33
34static GLuint Width = 1024;
35static GLuint Height = 512;
36
37
38static void Idle( void )
39{
40 if (Animate) {
41
42 Drift += drift_increment;
43 if (Drift >= 1.0)
44 Drift = 0.0;
45
46 glutPostRedisplay();
47 }
48}
49
Brian55d4f322007-10-24 13:55:22 -060050/*static int max( int a, int b ) { return a > b ? a : b; }*/
Keith Whitwell48e6fff2006-11-01 14:26:10 +000051static int min( int a, int b ) { return a < b ? a : b; }
52
53static void DrawObject()
54{
55 GLint size = Width * Height * 4;
56
57 if (use_pbo) {
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.
62 */
Brian55d4f322007-10-24 13:55:22 -060063 glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_EXT, DrawPBO);
Keith Whitwell48e6fff2006-11-01 14:26:10 +000064 glBufferDataARB(GL_PIXEL_UNPACK_BUFFER_EXT, size, NULL, GL_STREAM_DRAW_ARB);
65
66 {
67 char *image = glMapBufferARB(GL_PIXEL_UNPACK_BUFFER_EXT, GL_WRITE_ONLY_ARB);
68
69 printf("char %d\n", (unsigned char)(Drift * 255));
70
Brian55d4f322007-10-24 13:55:22 -060071 memset(image, (unsigned char)(Drift * 255), size);
Keith Whitwell48e6fff2006-11-01 14:26:10 +000072
73 glUnmapBufferARB(GL_PIXEL_UNPACK_BUFFER_EXT);
74 }
75
76
77 /* BGRA is required for most hardware paths:
78 */
79 glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA, Width, Height, 0,
80 GL_BGRA, GL_UNSIGNED_BYTE, NULL);
81 }
82 else {
83 static char *image = NULL;
84
85 if (image == NULL)
86 image = malloc(size);
87
Brian55d4f322007-10-24 13:55:22 -060088 glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_EXT, 0);
89
90 memset(image, (unsigned char)(Drift * 255), size);
Keith Whitwell48e6fff2006-11-01 14:26:10 +000091
92 /* BGRA should be the fast path for regular uploads as well.
93 */
94 glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA, Width, Height, 0,
95 GL_BGRA, GL_UNSIGNED_BYTE, image);
96 }
97
98 {
99 int x,y,w,h;
100
101 if (whole_rect) {
102 x = y = 0;
103 w = Width;
104 h = Height;
105 }
106 else {
107 x = y = 0;
108 w = min(10, Width);
109 h = min(10, Height);
110 }
111
112 glBegin(GL_QUADS);
113
114 glTexCoord2f( x, y);
115 glVertex2f( x, y );
116
117 glTexCoord2f( x, y + h);
118 glVertex2f( x, y + h);
119
120 glTexCoord2f( x + w + .5, y + h);
121 glVertex2f( x + w, y + h );
122
123 glTexCoord2f( x + w, y + .5);
124 glVertex2f( x + w, y );
125
126 glEnd();
127 }
128}
129
130
131
132static void Display( void )
133{
134 static GLint T0 = 0;
135 static GLint Frames = 0;
136 GLint t;
137
138 glClear( GL_COLOR_BUFFER_BIT );
139
140 glPushMatrix();
141 DrawObject();
142 glPopMatrix();
143
144 glutSwapBuffers();
145
146 Frames++;
147
148 t = glutGet(GLUT_ELAPSED_TIME);
149 if (t - T0 >= 1000) {
150 GLfloat seconds = (t - T0) / 1000.0;
151
152 GLfloat fps = Frames / seconds;
153 printf("%d frames in %6.3f seconds = %6.3f FPS\n", Frames, seconds, fps);
154
155 drift_increment = 2.2 * seconds / Frames;
156 T0 = t;
157 Frames = 0;
158 }
159}
160
161
162static void Reshape( int width, int height )
163{
164 glViewport( 0, 0, width, height );
165 glMatrixMode( GL_PROJECTION );
166 glLoadIdentity();
167/* glFrustum( -1.0, 1.0, -1.0, 1.0, 10.0, 100.0 ); */
168 gluOrtho2D( 0, width, height, 0 );
169 glMatrixMode( GL_MODELVIEW );
170 glLoadIdentity();
171 glTranslatef(0.375, 0.375, 0);
172}
173
174
175static void ModeMenu(int entry)
176{
177 if (entry==ANIMATE) {
178 Animate = !Animate;
179 }
180 else if (entry==PBO) {
181 use_pbo = !use_pbo;
182 }
183 else if (entry==QUIT) {
184 exit(0);
185 }
186
187 glutPostRedisplay();
188}
189
190
191static void Key( unsigned char key, int x, int y )
192{
193 (void) x;
194 (void) y;
195 switch (key) {
196 case 27:
197 exit(0);
198 break;
199 }
200 glutPostRedisplay();
201}
202
203
204static void SpecialKey( int key, int x, int y )
205{
206 float step = 3.0;
207 (void) x;
208 (void) y;
209
210 switch (key) {
211 case GLUT_KEY_UP:
212 Xrot += step;
213 break;
214 case GLUT_KEY_DOWN:
215 Xrot -= step;
216 break;
217 case GLUT_KEY_LEFT:
218 Yrot += step;
219 break;
220 case GLUT_KEY_RIGHT:
221 Yrot -= step;
222 break;
223 }
224 glutPostRedisplay();
225}
226
227
228static void Init( int argc, char *argv[] )
229{
230 const char *exten = (const char *) glGetString(GL_EXTENSIONS);
Brian55d4f322007-10-24 13:55:22 -0600231 GLuint texObj;
Keith Whitwell48e6fff2006-11-01 14:26:10 +0000232 GLint size;
233
234
Brian55d4f322007-10-24 13:55:22 -0600235 if (!strstr(exten, "GL_ARB_pixel_buffer_object")) {
236 printf("Sorry, GL_ARB_pixel_buffer_object not supported by this renderer.\n");
Keith Whitwell48e6fff2006-11-01 14:26:10 +0000237 exit(1);
238 }
239
240 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &size);
241 printf("%d x %d max texture size\n", size, size);
242
243 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
244
245 /* allocate two texture objects */
246 glGenTextures(1, &texObj);
247
248 /* setup the texture objects */
249 glActiveTextureARB(GL_TEXTURE0_ARB);
250 glBindTexture(GL_TEXTURE_RECTANGLE_ARB, texObj);
251
252 glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
253 glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
254
255 glGenBuffersARB(1, &DrawPBO);
256
257 glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_EXT, DrawPBO);
258 glBufferDataARB(GL_PIXEL_UNPACK_BUFFER_EXT,
259 Width * Height * 4, NULL, GL_STREAM_DRAW);
260
261 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
262
263 glEnable(GL_TEXTURE_RECTANGLE_ARB);
264
265 glShadeModel(GL_SMOOTH);
266 glClearColor(0.3, 0.3, 0.4, 1.0);
267
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));
273 }
274}
275
276
277int main( int argc, char *argv[] )
278{
279 GLint i;
280
281 glutInit( &argc, argv );
282
283 for (i = 1; i < argc; i++) {
284 if (strcmp(argv[i], "-w") == 0) {
285 Width = atoi(argv[i+1]);
286 if (Width <= 0) {
287 printf("Error, bad width\n");
288 exit(1);
289 }
290 i++;
291 }
292 else if (strcmp(argv[i], "-h") == 0) {
293 Height = atoi(argv[i+1]);
294 if (Height <= 0) {
295 printf("Error, bad height\n");
296 exit(1);
297 }
298 i++;
299 }
300 }
301
302 glutInitWindowSize( Width, Height );
303 glutInitWindowPosition( 0, 0 );
304 glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
305 glutCreateWindow(argv[0] );
306
307 Init( argc, argv );
308
309 glutReshapeFunc( Reshape );
310 glutKeyboardFunc( Key );
311 glutSpecialFunc( SpecialKey );
312 glutDisplayFunc( Display );
313 glutIdleFunc( Idle );
314
315 glutCreateMenu(ModeMenu);
316 glutAddMenuEntry("Toggle Animation", ANIMATE);
317 glutAddMenuEntry("Toggle PBO", PBO);
318 glutAddMenuEntry("Quit", QUIT);
319 glutAttachMenu(GLUT_RIGHT_BUTTON);
320
321 glutMainLoop();
322 return 0;
323}