gallium: change remaining util functions to use cso sampler views
[mesa/mesa-lb.git] / progs / tests / copypixrate.c
blobf63d59f3ceccd4ee7b689eb45310f10dffc71905
1 /*
2 * Measure glCopyPixels speed
4 * Brian Paul
5 * 26 Jan 2006
6 */
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <math.h>
12 #include <GL/glew.h>
13 #include <GL/glut.h>
15 static GLint WinWidth = 1000, WinHeight = 800;
16 static GLint ImgWidth, ImgHeight;
18 static GLenum Buffer = GL_FRONT;
19 static GLenum AlphaTest = GL_FALSE;
20 static GLboolean UseBlit = GL_FALSE;
22 static PFNGLBLITFRAMEBUFFEREXTPROC glBlitFramebufferEXT_func = NULL;
25 /**
26 * draw teapot in lower-left corner of window
28 static void
29 DrawTestImage(void)
31 GLfloat ar;
33 ImgWidth = WinWidth / 3;
34 ImgHeight = WinHeight / 3;
36 glViewport(0, 0, ImgWidth, ImgHeight);
37 glScissor(0, 0, ImgWidth, ImgHeight);
38 glEnable(GL_SCISSOR_TEST);
40 glClearColor(0.5, 0.5, 0.5, 0.0);
41 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
43 ar = (float) WinWidth / WinHeight;
45 glMatrixMode(GL_PROJECTION);
46 glLoadIdentity();
47 glFrustum(-ar, ar, -1.0, 1.0, 5.0, 25.0);
48 glMatrixMode(GL_MODELVIEW);
50 glEnable(GL_LIGHTING);
51 glEnable(GL_LIGHT0);
52 glEnable(GL_DEPTH_TEST);
53 glFrontFace(GL_CW);
54 glPushMatrix();
55 glRotatef(45, 1, 0, 0);
56 glutSolidTeapot(2.0);
57 glPopMatrix();
58 glFrontFace(GL_CCW);
59 glDisable(GL_DEPTH_TEST);
60 glDisable(GL_LIGHTING);
62 glDisable(GL_SCISSOR_TEST);
64 glViewport(0, 0, WinWidth, WinHeight);
65 glFinish();
69 static int
70 Rand(int max)
72 return ((int) rand()) % max;
76 static void
77 BlitOne(void)
79 int x, y;
81 do {
82 x = Rand(WinWidth);
83 y = Rand(WinHeight);
84 } while (x <= ImgWidth && y <= ImgHeight);
86 #ifdef GL_EXT_framebuffer_blit
87 if (UseBlit)
89 glBlitFramebufferEXT_func(0, 0, ImgWidth, ImgHeight,
90 x, y, x + ImgWidth, y + ImgHeight,
91 GL_COLOR_BUFFER_BIT, GL_LINEAR);
93 else
94 #endif
96 glWindowPos2iARB(x, y);
97 glCopyPixels(0, 0, ImgWidth, ImgHeight, GL_COLOR);
103 * Measure glCopyPixels rate
105 static void
106 RunTest(void)
108 double t1, t0 = glutGet(GLUT_ELAPSED_TIME) / 1000.0;
109 int iters = 0;
110 float copyRate, mbRate;
111 int r, g, b, a, bpp;
113 if (AlphaTest) {
114 glEnable(GL_ALPHA_TEST);
115 glAlphaFunc(GL_GREATER, 0.0);
118 glGetIntegerv(GL_RED_BITS, &r);
119 glGetIntegerv(GL_GREEN_BITS, &g);
120 glGetIntegerv(GL_BLUE_BITS, &b);
121 glGetIntegerv(GL_ALPHA_BITS, &a);
122 bpp = (r + g + b + a) / 8;
124 do {
125 BlitOne();
127 if (Buffer == GL_FRONT)
128 glFinish(); /* XXX to view progress */
130 iters++;
132 t1 = glutGet(GLUT_ELAPSED_TIME) / 1000.0;
133 } while (t1 - t0 < 5.0);
135 glDisable(GL_ALPHA_TEST);
137 copyRate = iters / (t1 - t0);
138 mbRate = ImgWidth * ImgHeight * bpp * copyRate / (1024 * 1024);
140 printf("Image size: %d x %d, %d Bpp\n", ImgWidth, ImgHeight, bpp);
141 printf("%d copies in %.2f = %.2f copies/sec, %.2f MB/s\n",
142 iters, t1-t0, copyRate, mbRate);
146 static void
147 Draw(void)
149 glClearColor(0.0, 0.0, 0.0, 0.0);
150 glClearColor(0.2, 0.2, 0.8, 0);
151 glReadBuffer(Buffer);
152 glDrawBuffer(Buffer);
153 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
155 DrawTestImage();
157 RunTest();
159 if (Buffer == GL_FRONT)
160 glFinish();
161 else
162 glutSwapBuffers();
164 #if 1
165 printf("exiting\n");
166 exit(0);
167 #endif
171 static void
172 Reshape(int width, int height)
174 glViewport(0, 0, width, height);
175 glMatrixMode(GL_PROJECTION);
176 glLoadIdentity();
177 glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 25.0);
178 glMatrixMode(GL_MODELVIEW);
179 glLoadIdentity();
180 glTranslatef(0.0, 0.0, -15.0);
184 static void
185 Key(unsigned char key, int x, int y)
187 (void) x;
188 (void) y;
189 switch (key) {
190 case 'b':
191 BlitOne();
192 break;
193 case 27:
194 exit(0);
195 break;
197 glutPostRedisplay();
201 static void
202 SpecialKey(int key, int x, int y)
204 (void) x;
205 (void) y;
206 switch (key) {
207 case GLUT_KEY_UP:
208 break;
209 case GLUT_KEY_DOWN:
210 break;
211 case GLUT_KEY_LEFT:
212 break;
213 case GLUT_KEY_RIGHT:
214 break;
216 glutPostRedisplay();
220 static void
221 ParseArgs(int argc, char *argv[])
223 int i;
224 for (i = 1; i < argc; i++) {
225 if (strcmp(argv[i], "-back") == 0)
226 Buffer = GL_BACK;
227 else if (strcmp(argv[i], "-alpha") == 0)
228 AlphaTest = GL_TRUE;
229 else if (strcmp(argv[i], "-blit") == 0)
230 UseBlit = GL_TRUE;
235 static void
236 Init(void)
238 if (glutExtensionSupported("GL_EXT_framebuffer_blit")) {
239 glBlitFramebufferEXT_func = (PFNGLBLITFRAMEBUFFEREXTPROC)
240 glutGetProcAddress("glBlitFramebufferEXT");
242 else if (UseBlit) {
243 printf("Warning: GL_EXT_framebuffer_blit not supported.\n");
244 UseBlit = GL_FALSE;
250 main(int argc, char *argv[])
252 GLint mode = GLUT_RGB | GLUT_ALPHA | GLUT_DOUBLE | GLUT_DEPTH;
253 glutInit(&argc, argv);
255 ParseArgs(argc, argv);
256 if (AlphaTest)
257 mode |= GLUT_ALPHA;
259 glutInitWindowPosition(0, 0);
260 glutInitWindowSize(WinWidth, WinHeight);
261 glutInitDisplayMode(mode);
262 glutCreateWindow(argv[0]);
263 glewInit();
264 glutReshapeFunc(Reshape);
265 glutKeyboardFunc(Key);
266 glutSpecialFunc(SpecialKey);
267 glutDisplayFunc(Draw);
269 printf("GL_RENDERER: %s\n", (char *) glGetString(GL_RENDERER));
270 printf("Draw Buffer: %s\n", (Buffer == GL_BACK) ? "Back" : "Front");
271 Init();
273 glutMainLoop();
274 return 0;