Merge remote branch 'main/master' into radeon-rewrite
[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
51 #ifndef min
52 static int min( int a, int b ) { return a < b ? a : b; }
53 #endif
54
55 static void DrawObject()
56 {
57 GLint size = Width * Height * 4;
58
59 if (use_pbo) {
60 /* XXX: This is extremely important - semantically makes the buffer
61 * contents undefined, but in practice means that the driver can
62 * release the old copy of the texture and allocate a new one
63 * without waiting for outstanding rendering to complete.
64 */
65 glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_EXT, DrawPBO);
66 glBufferDataARB(GL_PIXEL_UNPACK_BUFFER_EXT, size, NULL, GL_STREAM_DRAW_ARB);
67
68 {
69 char *image = glMapBufferARB(GL_PIXEL_UNPACK_BUFFER_EXT, GL_WRITE_ONLY_ARB);
70
71 printf("char %d\n", (unsigned char)(Drift * 255));
72
73 memset(image, (unsigned char)(Drift * 255), size);
74
75 glUnmapBufferARB(GL_PIXEL_UNPACK_BUFFER_EXT);
76 }
77
78
79 /* BGRA is required for most hardware paths:
80 */
81 glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA, Width, Height, 0,
82 GL_BGRA, GL_UNSIGNED_BYTE, NULL);
83 }
84 else {
85 static char *image = NULL;
86
87 if (image == NULL)
88 image = malloc(size);
89
90 glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_EXT, 0);
91
92 memset(image, (unsigned char)(Drift * 255), size);
93
94 /* BGRA should be the fast path for regular uploads as well.
95 */
96 glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA, Width, Height, 0,
97 GL_BGRA, GL_UNSIGNED_BYTE, image);
98 }
99
100 {
101 int x,y,w,h;
102
103 if (whole_rect) {
104 x = y = 0;
105 w = Width;
106 h = Height;
107 }
108 else {
109 x = y = 0;
110 w = min(10, Width);
111 h = min(10, Height);
112 }
113
114 glBegin(GL_QUADS);
115
116 glTexCoord2f( x, y);
117 glVertex2f( x, y );
118
119 glTexCoord2f( x, y + h);
120 glVertex2f( x, y + h);
121
122 glTexCoord2f( x + w + .5, y + h);
123 glVertex2f( x + w, y + h );
124
125 glTexCoord2f( x + w, y + .5);
126 glVertex2f( x + w, y );
127
128 glEnd();
129 }
130 }
131
132
133
134 static void Display( void )
135 {
136 static GLint T0 = 0;
137 static GLint Frames = 0;
138 GLint t;
139
140 glClear( GL_COLOR_BUFFER_BIT );
141
142 glPushMatrix();
143 DrawObject();
144 glPopMatrix();
145
146 glutSwapBuffers();
147
148 Frames++;
149
150 t = glutGet(GLUT_ELAPSED_TIME);
151 if (t - T0 >= 1000) {
152 GLfloat seconds = (t - T0) / 1000.0;
153
154 GLfloat fps = Frames / seconds;
155 printf("%d frames in %6.3f seconds = %6.3f FPS\n", Frames, seconds, fps);
156 fflush(stdout);
157
158 drift_increment = 2.2 * seconds / Frames;
159 T0 = t;
160 Frames = 0;
161 }
162 }
163
164
165 static void Reshape( int width, int height )
166 {
167 glViewport( 0, 0, width, height );
168 glMatrixMode( GL_PROJECTION );
169 glLoadIdentity();
170 /* glFrustum( -1.0, 1.0, -1.0, 1.0, 10.0, 100.0 ); */
171 gluOrtho2D( 0, width, height, 0 );
172 glMatrixMode( GL_MODELVIEW );
173 glLoadIdentity();
174 glTranslatef(0.375, 0.375, 0);
175 }
176
177
178 static void ModeMenu(int entry)
179 {
180 if (entry==ANIMATE) {
181 Animate = !Animate;
182 }
183 else if (entry==PBO) {
184 use_pbo = !use_pbo;
185 }
186 else if (entry==QUIT) {
187 exit(0);
188 }
189
190 glutPostRedisplay();
191 }
192
193
194 static void Key( unsigned char key, int x, int y )
195 {
196 (void) x;
197 (void) y;
198 switch (key) {
199 case 27:
200 exit(0);
201 break;
202 }
203 glutPostRedisplay();
204 }
205
206
207 static void SpecialKey( int key, int x, int y )
208 {
209 float step = 3.0;
210 (void) x;
211 (void) y;
212
213 switch (key) {
214 case GLUT_KEY_UP:
215 Xrot += step;
216 break;
217 case GLUT_KEY_DOWN:
218 Xrot -= step;
219 break;
220 case GLUT_KEY_LEFT:
221 Yrot += step;
222 break;
223 case GLUT_KEY_RIGHT:
224 Yrot -= step;
225 break;
226 }
227 glutPostRedisplay();
228 }
229
230
231 static void Init( int argc, char *argv[] )
232 {
233 const char *exten = (const char *) glGetString(GL_EXTENSIONS);
234 GLuint texObj;
235 GLint size;
236
237
238 if (!strstr(exten, "GL_ARB_pixel_buffer_object")) {
239 printf("Sorry, GL_ARB_pixel_buffer_object not supported by this renderer.\n");
240 exit(1);
241 }
242
243 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &size);
244 printf("%d x %d max texture size\n", size, size);
245
246 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
247
248 /* allocate two texture objects */
249 glGenTextures(1, &texObj);
250
251 /* setup the texture objects */
252 glActiveTextureARB(GL_TEXTURE0_ARB);
253 glBindTexture(GL_TEXTURE_RECTANGLE_ARB, texObj);
254
255 glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
256 glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
257
258 glGenBuffersARB(1, &DrawPBO);
259
260 glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_EXT, DrawPBO);
261 glBufferDataARB(GL_PIXEL_UNPACK_BUFFER_EXT,
262 Width * Height * 4, NULL, GL_STREAM_DRAW);
263
264 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
265
266 glEnable(GL_TEXTURE_RECTANGLE_ARB);
267
268 glShadeModel(GL_SMOOTH);
269 glClearColor(0.3, 0.3, 0.4, 1.0);
270
271 if (argc > 1 && strcmp(argv[1], "-info")==0) {
272 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
273 printf("GL_VERSION = %s\n", (char *) glGetString(GL_VERSION));
274 printf("GL_VENDOR = %s\n", (char *) glGetString(GL_VENDOR));
275 printf("GL_EXTENSIONS = %s\n", (char *) glGetString(GL_EXTENSIONS));
276 }
277 }
278
279
280 int main( int argc, char *argv[] )
281 {
282 GLint i;
283
284 glutInit( &argc, argv );
285
286 for (i = 1; i < argc; i++) {
287 if (strcmp(argv[i], "-w") == 0) {
288 Width = atoi(argv[i+1]);
289 if (Width <= 0) {
290 printf("Error, bad width\n");
291 exit(1);
292 }
293 i++;
294 }
295 else if (strcmp(argv[i], "-h") == 0) {
296 Height = atoi(argv[i+1]);
297 if (Height <= 0) {
298 printf("Error, bad height\n");
299 exit(1);
300 }
301 i++;
302 }
303 }
304
305 glutInitWindowSize( Width, Height );
306 glutInitWindowPosition( 0, 0 );
307 glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
308 glutCreateWindow(argv[0] );
309 glewInit();
310
311 Init( argc, argv );
312
313 glutReshapeFunc( Reshape );
314 glutKeyboardFunc( Key );
315 glutSpecialFunc( SpecialKey );
316 glutDisplayFunc( Display );
317 glutIdleFunc( Idle );
318
319 glutCreateMenu(ModeMenu);
320 glutAddMenuEntry("Toggle Animation", ANIMATE);
321 glutAddMenuEntry("Toggle PBO", PBO);
322 glutAddMenuEntry("Quit", QUIT);
323 glutAttachMenu(GLUT_RIGHT_BUTTON);
324
325 glutMainLoop();
326 return 0;
327 }