tgsi: use x86_fn_arg instead of get_argument() -- it knows about push/pops to the...
[mesa.git] / progs / demos / streaming_rect.c
1
2 /*
3 * GL_ARB_multitexture demo
4 *
5 * Command line options:
6 * -info print GL implementation information
7 *
8 *
9 * Brian Paul November 1998 This program is in the public domain.
10 * Modified on 12 Feb 2002 for > 2 texture units.
11 */
12
13 #define GL_GLEXT_PROTOTYPES
14
15 #include <math.h>
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <string.h>
19 #include <GL/glut.h>
20
21 #include "readtex.h"
22
23
24 #define ANIMATE 10
25 #define PBO 11
26 #define QUIT 100
27
28 static GLboolean Animate = GL_TRUE;
29 static GLboolean use_pbo = 1;
30 static GLboolean whole_rect = 1;
31
32 static GLfloat Drift = 0.0;
33 static GLfloat drift_increment = 1/255.0;
34 static GLfloat Xrot = 20.0, Yrot = 30.0;
35
36 static GLuint Width = 1024;
37 static GLuint Height = 512;
38
39
40 static void Idle( void )
41 {
42 if (Animate) {
43
44 Drift += drift_increment;
45 if (Drift >= 1.0)
46 Drift = 0.0;
47
48 glutPostRedisplay();
49 }
50 }
51
52 static int max( int a, int b ) { return a > b ? a : b; }
53 static int min( int a, int b ) { return a < b ? a : b; }
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 glBufferDataARB(GL_PIXEL_UNPACK_BUFFER_EXT, size, NULL, GL_STREAM_DRAW_ARB);
66
67 {
68 char *image = glMapBufferARB(GL_PIXEL_UNPACK_BUFFER_EXT, GL_WRITE_ONLY_ARB);
69
70 printf("char %d\n", (unsigned char)(Drift * 255));
71
72 memset(image, size, (unsigned char)(Drift * 255));
73
74 glUnmapBufferARB(GL_PIXEL_UNPACK_BUFFER_EXT);
75 }
76
77
78 /* BGRA is required for most hardware paths:
79 */
80 glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA, Width, Height, 0,
81 GL_BGRA, GL_UNSIGNED_BYTE, NULL);
82 }
83 else {
84 static char *image = NULL;
85
86 if (image == NULL)
87 image = malloc(size);
88
89 memset(image, size, (unsigned char)(Drift * 255));
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, DrawPBO;
231 GLint size;
232
233
234 if (!strstr(exten, "GL_ARB_multitexture")) {
235 printf("Sorry, GL_ARB_multitexture 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
306 Init( argc, argv );
307
308 glutReshapeFunc( Reshape );
309 glutKeyboardFunc( Key );
310 glutSpecialFunc( SpecialKey );
311 glutDisplayFunc( Display );
312 glutIdleFunc( Idle );
313
314 glutCreateMenu(ModeMenu);
315 glutAddMenuEntry("Toggle Animation", ANIMATE);
316 glutAddMenuEntry("Toggle PBO", PBO);
317 glutAddMenuEntry("Quit", QUIT);
318 glutAttachMenu(GLUT_RIGHT_BUTTON);
319
320 glutMainLoop();
321 return 0;
322 }