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