progs/tests: compile with SCons and glew
[mesa.git] / progs / tests / fillrate.c
1 /**
2 * Measure fill rates for basic shading/texturing modes.
3 *
4 * Brian Paul
5 * 1 April 2008
6 */
7
8
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <math.h>
12 #include <GL/glew.h>
13 #include <GL/glut.h>
14 #include "readtex.h"
15
16 #define TEXTURE_1_FILE "../images/tile.rgb"
17 #define TEXTURE_2_FILE "../images/reflect.rgb"
18
19 static int Win;
20 static int Width = 1010, Height = 1010;
21 static GLuint Textures[2];
22
23
24 /**
25 * Draw quad 10 pixels shorter, narrower than window size.
26 */
27 static void
28 DrawQuad(void)
29 {
30 glBegin(GL_POLYGON);
31
32 glColor3f(1.0, 0.5, 0.5);
33 glMultiTexCoord2f(GL_TEXTURE0, 0, 0);
34 glMultiTexCoord2f(GL_TEXTURE1, 0, 0);
35 glVertex2f(5, 5);
36
37 glColor3f(0.5, 1.0, 0.5);
38 glMultiTexCoord2f(GL_TEXTURE0, 1, 0);
39 glMultiTexCoord2f(GL_TEXTURE1, 1, 0);
40 glVertex2f(Width - 5, 5);
41
42 glColor3f(0.5, 0.5, 1.0);
43 glMultiTexCoord2f(GL_TEXTURE0, 1, 1);
44 glMultiTexCoord2f(GL_TEXTURE1, 1, 1);
45 glVertex2f(Width - 5, Height - 5);
46
47 glColor3f(1.0, 0.5, 1.0);
48 glMultiTexCoord2f(GL_TEXTURE0, 0, 1);
49 glMultiTexCoord2f(GL_TEXTURE1, 0, 1);
50 glVertex2f(5, Height - 5);
51
52 glEnd();
53 }
54
55
56 /**
57 * Compute rate for drawing large quad with given shading/texture state.
58 */
59 static void
60 RunTest(GLenum shading, GLuint numTextures, GLenum texFilter)
61 {
62 const GLdouble minPeriod = 2.0;
63 GLdouble t0, t1;
64 GLdouble pixels, rate;
65 GLint i, iters;
66
67 glActiveTexture(GL_TEXTURE0);
68 if (numTextures > 0) {
69 glBindTexture(GL_TEXTURE_2D, Textures[0]);
70 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, texFilter);
71 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, texFilter);
72 glEnable(GL_TEXTURE_2D);
73 }
74 else {
75 glDisable(GL_TEXTURE_2D);
76 }
77
78 glActiveTexture(GL_TEXTURE1);
79 if (numTextures > 1) {
80 glBindTexture(GL_TEXTURE_2D, Textures[1]);
81 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, texFilter);
82 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, texFilter);
83 glEnable(GL_TEXTURE_2D);
84 }
85 else {
86 glDisable(GL_TEXTURE_2D);
87 }
88
89 glShadeModel(shading);
90
91
92 glFinish();
93
94 iters = 1;
95 do {
96 iters *= 4;
97 t0 = glutGet(GLUT_ELAPSED_TIME) * 0.001;
98 for (i = 0; i < iters; i++) {
99 DrawQuad();
100 }
101 glFinish();
102 t1 = glutGet(GLUT_ELAPSED_TIME) * 0.001;
103 } while (t1 - t0 < minPeriod);
104
105 glutSwapBuffers();
106
107 pixels = (double) iters * (Width - 10) * (Height - 10);
108 rate = pixels / (t1 - t0);
109 rate /= 1000000.0; /* megapixels/second */
110
111 printf("%s ", shading == GL_FLAT ? "GL_FLAT" : "GL_SMOOTH");
112 printf("Textures=%u ", numTextures);
113 printf("Filter=%s: ", texFilter == GL_LINEAR ? "GL_LINEAR" : "GL_NEAREST");
114 printf("%g MPixels/sec\n", rate);
115 }
116
117
118 static void
119 Draw(void)
120 {
121 glClear(GL_COLOR_BUFFER_BIT);
122
123 RunTest(GL_FLAT, 0, GL_NEAREST);
124 RunTest(GL_SMOOTH, 0, GL_NEAREST);
125 RunTest(GL_SMOOTH, 1, GL_NEAREST);
126 RunTest(GL_SMOOTH, 1, GL_LINEAR);
127 RunTest(GL_SMOOTH, 2, GL_NEAREST);
128 RunTest(GL_SMOOTH, 2, GL_LINEAR);
129
130 glutSwapBuffers();
131 }
132
133
134 static void
135 Reshape(int width, int height)
136 {
137 glViewport(0, 0, width, height);
138 glMatrixMode(GL_PROJECTION);
139 glLoadIdentity();
140 glOrtho(0, width, 0, height, -1, 1);
141 glMatrixMode(GL_MODELVIEW);
142 glLoadIdentity();
143 Width = width;
144 Height = height;
145 }
146
147
148 static void
149 Key(unsigned char key, int x, int y)
150 {
151 (void) x;
152 (void) y;
153 switch (key) {
154 case 27:
155 glutDestroyWindow(Win);
156 exit(0);
157 break;
158 }
159 glutPostRedisplay();
160 }
161
162
163 static void
164 Init(void)
165 {
166 printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
167
168 glGenTextures(2, Textures);
169
170 glBindTexture(GL_TEXTURE_2D, Textures[0]);
171 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
172
173 if (!LoadRGBMipmaps(TEXTURE_1_FILE, GL_RGB)) {
174 printf("Error: couldn't load texture image\n");
175 exit(1);
176 }
177
178 glBindTexture(GL_TEXTURE_2D, Textures[1]);
179 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
180
181 if (!LoadRGBMipmaps(TEXTURE_2_FILE, GL_RGB)) {
182 printf("Error: couldn't load texture image\n");
183 exit(1);
184 }
185
186 }
187
188
189 int
190 main(int argc, char *argv[])
191 {
192 glutInit(&argc, argv);
193 glutInitWindowPosition(0, 0);
194 glutInitWindowSize(Width, Height);
195 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
196 Win = glutCreateWindow(argv[0]);
197 glewInit();
198 glutReshapeFunc(Reshape);
199 glutKeyboardFunc(Key);
200 glutDisplayFunc(Draw);
201 Init();
202 glutMainLoop();
203 return 0;
204 }