progs/tests: compile with SCons and glew
[mesa.git] / progs / tests / copypixrate.c
1 /*
2 * Measure glCopyPixels speed
3 *
4 * Brian Paul
5 * 26 Jan 2006
6 */
7
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>
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 static void
77 BlitOne(void)
78 {
79 int x, y;
80
81 do {
82 x = Rand(WinWidth);
83 y = Rand(WinHeight);
84 } while (x <= ImgWidth && y <= ImgHeight);
85
86 #ifdef GL_EXT_framebuffer_blit
87 if (UseBlit)
88 {
89 glBlitFramebufferEXT_func(0, 0, ImgWidth, ImgHeight,
90 x, y, x + ImgWidth, y + ImgHeight,
91 GL_COLOR_BUFFER_BIT, GL_LINEAR);
92 }
93 else
94 #endif
95 {
96 glWindowPos2iARB(x, y);
97 glCopyPixels(0, 0, ImgWidth, ImgHeight, GL_COLOR);
98 }
99 }
100
101
102 /**
103 * Measure glCopyPixels rate
104 */
105 static void
106 RunTest(void)
107 {
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;
112
113 if (AlphaTest) {
114 glEnable(GL_ALPHA_TEST);
115 glAlphaFunc(GL_GREATER, 0.0);
116 }
117
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;
123
124 do {
125 BlitOne();
126
127 if (Buffer == GL_FRONT)
128 glFinish(); /* XXX to view progress */
129
130 iters++;
131
132 t1 = glutGet(GLUT_ELAPSED_TIME) / 1000.0;
133 } while (t1 - t0 < 5.0);
134
135 glDisable(GL_ALPHA_TEST);
136
137 copyRate = iters / (t1 - t0);
138 mbRate = ImgWidth * ImgHeight * bpp * copyRate / (1024 * 1024);
139
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);
143 }
144
145
146 static void
147 Draw(void)
148 {
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);
154
155 DrawTestImage();
156
157 RunTest();
158
159 if (Buffer == GL_FRONT)
160 glFinish();
161 else
162 glutSwapBuffers();
163
164 #if 1
165 printf("exiting\n");
166 exit(0);
167 #endif
168 }
169
170
171 static void
172 Reshape(int width, int height)
173 {
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);
181 }
182
183
184 static void
185 Key(unsigned char key, int x, int y)
186 {
187 (void) x;
188 (void) y;
189 switch (key) {
190 case 'b':
191 BlitOne();
192 break;
193 case 27:
194 exit(0);
195 break;
196 }
197 glutPostRedisplay();
198 }
199
200
201 static void
202 SpecialKey(int key, int x, int y)
203 {
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;
215 }
216 glutPostRedisplay();
217 }
218
219
220 static void
221 ParseArgs(int argc, char *argv[])
222 {
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;
231 }
232 }
233
234
235 static void
236 Init(void)
237 {
238 if (glutExtensionSupported("GL_EXT_framebuffer_blit")) {
239 glBlitFramebufferEXT_func = (PFNGLBLITFRAMEBUFFEREXTPROC)
240 glutGetProcAddress("glBlitFramebufferEXT");
241 }
242 else if (UseBlit) {
243 printf("Warning: GL_EXT_framebuffer_blit not supported.\n");
244 UseBlit = GL_FALSE;
245 }
246 }
247
248
249 int
250 main(int argc, char *argv[])
251 {
252 GLint mode = GLUT_RGB | GLUT_ALPHA | GLUT_DOUBLE | GLUT_DEPTH;
253 glutInit(&argc, argv);
254
255 ParseArgs(argc, argv);
256 if (AlphaTest)
257 mode |= GLUT_ALPHA;
258
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);
268
269 printf("GL_RENDERER: %s\n", (char *) glGetString(GL_RENDERER));
270 printf("Draw Buffer: %s\n", (Buffer == GL_BACK) ? "Back" : "Front");
271 Init();
272
273 glutMainLoop();
274 return 0;
275 }