source: trunk/src/testing/app/bitplay/txspeed.c @ 4

Revision 4, 6.5 KB checked in by ajaworski, 13 years ago (diff)

Added modified SAGE sources

Line 
1/******************************************************************************
2 * SAGE - Scalable Adaptive Graphics Environment
3 *
4 * Copyright (C) 2004 Electronic Visualization Laboratory,
5 * University of Illinois at Chicago
6 *
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions are met:
11 *
12 *  * Redistributions of source code must retain the above copyright
13 *    notice, this list of conditions and the following disclaimer.
14 *  * Redistributions in binary form must reproduce the above
15 *    copyright notice, this list of conditions and the following disclaimer
16 *    in the documentation and/or other materials provided with the distribution.
17 *  * Neither the name of the University of Illinois at Chicago nor
18 *    the names of its contributors may be used to endorse or promote
19 *    products derived from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
25 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 *
33 * Direct questions, comments etc about SAGE to http://www.evl.uic.edu/cavern/forum/
34 *
35 *****************************************************************************/
36
37/*
38 * cc -o txspeed{,.c} -L/usr/X11R6/lib -lglut -lGL -lX11
39 */
40#include <X11/Xlib.h>
41#include <GL/glut.h>
42#include <stdlib.h>
43#include <stdio.h>
44#include <math.h>
45#include <time.h>
46#include <signal.h>
47
48#ifndef MAXTX
49# define MAXTX 128
50#endif
51
52#ifndef TXROOM
53# define TXROOM  64
54#endif
55
56unsigned char txbuf[ TXROOM * MAXTX * MAXTX * 4 ];
57int width = MAXTX;
58int height = MAXTX;
59int internalfmt = GL_RGB;
60int fmt = GL_RGB;
61int type = GL_UNSIGNED_BYTE;
62
63int newtex = 1;
64
65int justone = 0;
66int justload = 0;
67
68#define TXSLOTS  1024
69
70int winx, winy;
71int nxtx, nytx, ntx;
72GLuint txs[TXSLOTS];
73
74void tick( int start );  /* tick(1) to start, tick(0) to continue, tick(-1) to clean up */
75
76void reshape( int xsize, int ysize )
77{
78    int i;
79
80    winx = xsize;  winy = ysize;
81
82    glMatrixMode( GL_PROJECTION );
83    glLoadIdentity();
84    glOrtho( 0, xsize, 0, ysize, -1, 1 );
85    glViewport( 0, 0, xsize, ysize );
86    glMatrixMode( GL_MODELVIEW );
87    glLoadIdentity();
88
89    glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL );
90    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
91    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
92
93    glEnable( GL_TEXTURE_2D );
94
95    /* How many textures do we need to fill the screen? */
96    nxtx = winx / MAXTX;  if(nxtx == 0) nxtx = 1;
97    nytx = winy / MAXTX;  if(nytx == 0) nytx = 1;
98    if(nxtx*nytx > TXSLOTS) nytx = TXSLOTS / nxtx;
99
100    /* ensure that that many texture-objects exist */
101    for(i = 0; i < nxtx*nytx; i++) {
102        if(txs[i] == 0) {
103            glGenTextures( nxtx*nytx-i, &txs[i] );
104            newtex = 1;
105            break;
106        }
107    }
108
109    for(i = 0; i < sizeof(txbuf); i+=4) {
110        int v = i ^ (i/MAXTX) ^ (i/MAXTX/MAXTX);
111        txbuf[i] = v;
112        txbuf[i+1] = (v<<1) | (v>>7);
113        txbuf[i+2] = (v<<3) | (v>>5);
114        txbuf[i+3] = (v<<5) | (v>>3);
115    }
116
117    tick(1);
118    printf("reshape %dx%d  %dx%d\n", xsize, ysize, nxtx, nytx);
119}
120
121int quitnow = 0;
122
123void idler()
124{
125    if(quitnow) {
126        tick(-1);
127        exit(1);
128    }
129    glutPostRedisplay();
130}
131
132void catch(int sig) {
133    quitnow = 1;
134}
135
136int ntimes = 50;
137
138void tick( int start )
139{
140    static struct timeval then;
141    struct timeval now;
142    static int count;
143    double dt;
144
145    if(start>0) {
146        count = 0;
147        return;
148    }
149
150    count++;
151
152    if(count == 2) {
153        gettimeofday( &then, NULL );
154    } else if(count == ntimes+2 || start<0) {
155        gettimeofday( &now, NULL );
156        dt = (now.tv_sec - then.tv_sec) + 1e-6*(now.tv_usec - then.tv_usec);
157        if(count > 2)
158            printf(" %g\n", 1000*dt/(count-2), now.tv_sec, then.tv_sec);
159        else printf("tick done: count %d dt %g\n", count, dt);
160        count = 0;
161    }
162}
163
164void kb( unsigned char key, int x, int y )
165{
166    tick( -1 );
167    exit(0);
168}
169
170void redraw()
171{
172    int i, j, k, start, e;
173    int rowlength = 0;
174
175    glClearColor( .3, newtex, 0, 1 );
176    glClear( GL_COLOR_BUFFER_BIT );
177
178    glEnable( GL_TEXTURE_2D );
179
180    /* Load textures and draw */
181    for(i = k = 0; i < nytx; i++) {
182        for(j = 0; j < nxtx; j++, k++) {
183
184            int x0 = j*MAXTX;
185            int x1 = (j+1)*MAXTX;
186            int y0 = i*MAXTX;
187            int y1 = (i+1)*MAXTX;
188
189            if(x1 > winx) x1 = winx;
190            if(y1 > winy) y1 = winy;
191
192            if(!justone) glBindTexture( GL_TEXTURE_2D, txs[k] );
193            if(rowlength == 0)
194                start = (int) ( (((TXROOM-1)*MAXTX*MAXTX)*4) * drand48() );
195            else
196                start = (int) ((MAXTX*MAXTX*4) * drand48());
197
198            if(newtex) {
199
200                glPixelStorei( GL_UNPACK_ROW_LENGTH, rowlength );
201                /* glEnable( GL_TEXTURE_2D ); */
202                glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL );
203                glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
204                glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
205
206                glTexImage2D( GL_TEXTURE_2D, 0, internalfmt, width, height, 0, fmt, type,
207                    &txbuf[(start / 16) * 16] );
208            } else {
209                glTexSubImage2D( GL_TEXTURE_2D, 0,  0,0,  width,height, fmt, type,
210                    &txbuf[(start / 16) * 16] );
211            }
212
213            if(!justload) {
214            glBegin( GL_QUADS );
215
216            glTexCoord2f( 0, 0 );
217            glVertex3i( x0, y0, 0 );
218
219            glTexCoord2f( 0, 1 );
220            glVertex3i( x0, y1, 0 );
221
222            glTexCoord2f( 1, 1 );
223            glVertex3i( x1, y1, 0 );
224
225            glTexCoord2f( 1, 0 );
226            glVertex3i( x1, y0, 0 );
227            glEnd();
228            }
229
230            e = glGetError();
231            if(e != 0)
232                fprintf(stderr, "E%d ", e);
233        }
234    }
235
236    glutSwapBuffers();
237
238    newtex = 0;
239
240    tick(0);
241
242}
243
244main(int argc, char *argv[])
245{
246    signal(SIGINT, catch);
247    justone = (getenv("JUSTONE") != NULL);
248    justload = (getenv("JUSTLOAD") != NULL);
249
250    glutInit( &argc, argv );
251    glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE );
252
253    glutCreateWindow( "eep" );
254    glutFullScreen();
255
256    glutIdleFunc( idler );
257    glutReshapeFunc( reshape );
258    glutDisplayFunc( redraw );
259    glutKeyboardFunc( kb );
260
261    glutMainLoop();
262}
Note: See TracBrowser for help on using the repository browser.