Merge remote branch 'upstream/gallium-0.1' into nouveau-gallium-0.1
[mesa.git] / progs / demos / streaming_rect.c
1 /*
2 * GL_ARB_pixel_buffer_object test
3 *
4 * Command line options:
5 * -w WIDTH -h HEIGHT sets window size
6 *
7 */
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
24 static GLuint DrawPBO;
25
26 static GLboolean Animate = GL_TRUE;
27 static GLboolean use_pbo = 1;
28 static GLboolean whole_rect = 1;
29
30 static GLfloat Drift = 0.0;
31 static GLfloat drift_increment = 1/255.0;
32 static GLfloat Xrot = 20.0, Yrot = 30.0;
33
34 static GLuint Width = 1024;
35 static GLuint Height = 512;
36
37
38 static 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
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; }
52
53 static 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 */
63 glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_EXT, DrawPBO);
64 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
71 memset(image, (unsigned char)(Drift * 255), size);
72
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
88 glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_EXT, 0);
89
90 memset(image, (unsigned char)(Drift * 255), size);
91
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
132 static 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
162 static 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
175 static 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
191 static 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
204 static 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
228 static void Init( int argc, char *argv[] )
229 {
230 const char *exten = (const char *) glGetString(GL_EXTENSIONS);
231 GLuint texObj;
232 GLint size;
233
234
235 if (!strstr(exten, "GL_ARB_pixel_buffer_object")) {
236 printf("Sorry, GL_ARB_pixel_buffer_object not supported by this renderer.\n");
237 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
277 int 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 }