r500: add missing brackets around depth testing
[mesa.git] / progs / tests / copypixrate.c
1 /*
2 * Measure glCopyPixels speed
3 *
4 * Brian Paul
5 * 26 Jan 2006
6 */
7
8 #define GL_GLEXT_PROTOTYPES
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <math.h>
13 #include <GL/glut.h>
14
15 static GLint WinWidth = 1000, WinHeight = 800;
16 static GLint ImgWidth, ImgHeight;
17
18 static GLenum Buffer = GL_FRONT;
19 static GLenum AlphaTest = GL_FALSE;
20 static GLboolean UseBlit = GL_FALSE;
21
22 static PFNGLBLITFRAMEBUFFEREXTPROC glBlitFramebufferEXT_func = NULL;
23
24
25 /**
26 * draw teapot in lower-left corner of window
27 */
28 static void
29 DrawTestImage(void)
30 {
31 GLfloat ar;
32
33 ImgWidth = WinWidth / 3;
34 ImgHeight = WinHeight / 3;
35
36 glViewport(0, 0, ImgWidth, ImgHeight);
37 glScissor(0, 0, ImgWidth, ImgHeight);
38 glEnable(GL_SCISSOR_TEST);
39
40 glClearColor(0.5, 0.5, 0.5, 0.0);
41 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
42
43 ar = (float) WinWidth / WinHeight;
44
45 glMatrixMode(GL_PROJECTION);
46 glLoadIdentity();
47 glFrustum(-ar, ar, -1.0, 1.0, 5.0, 25.0);
48 glMatrixMode(GL_MODELVIEW);
49
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);
61
62 glDisable(GL_SCISSOR_TEST);
63
64 glViewport(0, 0, WinWidth, WinHeight);
65 glFinish();
66 }
67
68
69 static int
70 Rand(int max)
71 {
72 return ((int) random()) % max;
73 }
74
75
76 /**
77 * Measure glCopyPixels rate
78 */
79 static void
80 RunTest(void)
81 {
82 double t1, t0 = glutGet(GLUT_ELAPSED_TIME) / 1000.0;
83 int iters = 0;
84 float copyRate, mbRate;
85 int r, g, b, a, bpp;
86
87 if (AlphaTest) {
88 glEnable(GL_ALPHA_TEST);
89 glAlphaFunc(GL_GREATER, 0.0);
90 }
91
92 glGetIntegerv(GL_RED_BITS, &r);
93 glGetIntegerv(GL_GREEN_BITS, &g);
94 glGetIntegerv(GL_BLUE_BITS, &b);
95 glGetIntegerv(GL_ALPHA_BITS, &a);
96 bpp = (r + g + b + a) / 8;
97
98 do {
99 int x, y;
100 x = Rand(WinWidth);
101 y = Rand(WinHeight);
102
103 if (x > ImgWidth || y > ImgHeight) {
104 #ifdef GL_EXT_framebuffer_blit
105 if (UseBlit)
106 {
107 glBlitFramebufferEXT_func(0, 0, ImgWidth, ImgHeight,
108 x, y, x + ImgWidth, y + ImgHeight,
109 GL_COLOR_BUFFER_BIT, GL_LINEAR);
110 }
111 else
112 #endif
113 {
114 glWindowPos2iARB(x, y);
115 glCopyPixels(0, 0, ImgWidth, ImgHeight, GL_COLOR);
116 }
117 glFinish(); /* XXX OK? */
118
119 iters++;
120
121 t1 = glutGet(GLUT_ELAPSED_TIME) / 1000.0;
122 }
123 } while (t1 - t0 < 5.0);
124
125 glDisable(GL_ALPHA_TEST);
126
127 copyRate = iters / (t1 - t0);
128 mbRate = ImgWidth * ImgHeight * bpp * copyRate / (1024 * 1024);
129
130 printf("Image size: %d x %d, %d Bpp\n", ImgWidth, ImgHeight, bpp);
131 printf("%d copies in %.2f = %.2f copies/sec, %.2f MB/s\n",
132 iters, t1-t0, copyRate, mbRate);
133 }
134
135
136 static void
137 Draw(void)
138 {
139 glClearColor(0.0, 0.0, 0.0, 0.0);
140 glClearColor(0.2, 0.2, 0.8, 0);
141 glReadBuffer(Buffer);
142 glDrawBuffer(Buffer);
143 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
144
145 DrawTestImage();
146
147 RunTest();
148
149 if (Buffer == GL_FRONT)
150 glFinish();
151 else
152 glutSwapBuffers();
153
154 printf("exiting\n");
155 exit(0);
156 }
157
158
159 static void
160 Reshape(int width, int height)
161 {
162 glViewport(0, 0, width, height);
163 glMatrixMode(GL_PROJECTION);
164 glLoadIdentity();
165 glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 25.0);
166 glMatrixMode(GL_MODELVIEW);
167 glLoadIdentity();
168 glTranslatef(0.0, 0.0, -15.0);
169 }
170
171
172 static void
173 Key(unsigned char key, int x, int y)
174 {
175 (void) x;
176 (void) y;
177 switch (key) {
178 case 27:
179 exit(0);
180 break;
181 }
182 glutPostRedisplay();
183 }
184
185
186 static void
187 SpecialKey(int key, int x, int y)
188 {
189 (void) x;
190 (void) y;
191 switch (key) {
192 case GLUT_KEY_UP:
193 break;
194 case GLUT_KEY_DOWN:
195 break;
196 case GLUT_KEY_LEFT:
197 break;
198 case GLUT_KEY_RIGHT:
199 break;
200 }
201 glutPostRedisplay();
202 }
203
204
205 static void
206 ParseArgs(int argc, char *argv[])
207 {
208 int i;
209 for (i = 1; i < argc; i++) {
210 if (strcmp(argv[i], "-back") == 0)
211 Buffer = GL_BACK;
212 else if (strcmp(argv[i], "-alpha") == 0)
213 AlphaTest = GL_TRUE;
214 else if (strcmp(argv[i], "-blit") == 0)
215 UseBlit = GL_TRUE;
216 }
217 }
218
219
220 static void
221 Init(void)
222 {
223 if (glutExtensionSupported("GL_EXT_framebuffer_blit")) {
224 glBlitFramebufferEXT_func = (PFNGLBLITFRAMEBUFFEREXTPROC)
225 glutGetProcAddress("glBlitFramebufferEXT");
226 }
227 else if (UseBlit) {
228 printf("Warning: GL_EXT_framebuffer_blit not supported.\n");
229 UseBlit = GL_FALSE;
230 }
231 }
232
233
234 int
235 main(int argc, char *argv[])
236 {
237 GLint mode = GLUT_RGB | GLUT_ALPHA | GLUT_DOUBLE | GLUT_DEPTH;
238 glutInit(&argc, argv);
239
240 ParseArgs(argc, argv);
241 if (AlphaTest)
242 mode |= GLUT_ALPHA;
243
244 glutInitWindowPosition(0, 0);
245 glutInitWindowSize(WinWidth, WinHeight);
246 glutInitDisplayMode(mode);
247 glutCreateWindow(argv[0]);
248 glutReshapeFunc(Reshape);
249 glutKeyboardFunc(Key);
250 glutSpecialFunc(SpecialKey);
251 glutDisplayFunc(Draw);
252
253 printf("GL_RENDERER: %s\n", (char *) glGetString(GL_RENDERER));
254 printf("Draw Buffer: %s\n", (Buffer == GL_BACK) ? "Back" : "Front");
255 Init();
256
257 glutMainLoop();
258 return 0;
259 }