progs/perf: Include local headers before installed headers.
[mesa.git] / progs / perf / glmain.c
1 /*
2 * Copyright (C) 2009 VMware, Inc. All Rights Reserved.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included
12 * in all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * VMWARE BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
18 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 */
21
22 /**
23 * OpenGL/GLUT common code for perf programs.
24 * Brian Paul
25 * 15 Sep 2009
26 */
27
28
29 #include <stdio.h>
30 #include "glmain.h"
31 #include <GL/glut.h>
32
33
34 static int Win;
35 static GLfloat Xrot = 0, Yrot = 0, Zrot = 0;
36
37
38 /** Return time in seconds */
39 double
40 PerfGetTime(void)
41 {
42 return glutGet(GLUT_ELAPSED_TIME) * 0.001;
43 }
44
45
46 void
47 PerfSwapBuffers(void)
48 {
49 glutSwapBuffers();
50 }
51
52
53 /** make simple checkerboard texture object */
54 GLuint
55 PerfCheckerTexture(GLsizei width, GLsizei height)
56 {
57 const GLenum filter = GL_NEAREST;
58 GLubyte *img = (GLubyte *) malloc(width * height * 4);
59 GLint i, j, k;
60 GLuint obj;
61
62 k = 0;
63 for (i = 0; i < height; i++) {
64 for (j = 0; j < width; j++) {
65 GLubyte color;
66 if (((i / 8) ^ (j / 8)) & 1) {
67 color = 0xff;
68 }
69 else {
70 color = 0x0;
71 }
72 img[k++] = color;
73 img[k++] = color;
74 img[k++] = color;
75 img[k++] = color;
76 }
77 }
78
79 glGenTextures(1, &obj);
80 glBindTexture(GL_TEXTURE_2D, obj);
81 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filter);
82 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filter);
83 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0,
84 GL_RGBA, GL_UNSIGNED_BYTE, img);
85 free(img);
86
87 return obj;
88 }
89
90
91 static GLuint
92 CompileShader(GLenum type, const char *shader)
93 {
94 GLuint sh;
95 GLint stat;
96
97 sh = glCreateShader(type);
98 glShaderSource(sh, 1, (const GLchar **) &shader, NULL);
99
100 glCompileShader(sh);
101
102 glGetShaderiv(sh, GL_COMPILE_STATUS, &stat);
103 if (!stat) {
104 GLchar log[1000];
105 GLsizei len;
106 glGetShaderInfoLog(sh, 1000, &len, log);
107 fprintf(stderr, "Error: problem compiling shader: %s\n", log);
108 exit(1);
109 }
110
111 return sh;
112 }
113
114
115 /** Make shader program from given vert/frag shader text */
116 GLuint
117 PerfShaderProgram(const char *vertShader, const char *fragShader)
118 {
119 GLuint prog;
120 GLint stat;
121
122 {
123 const char *version = (const char *) glGetString(GL_VERSION);
124 if ((version[0] != '2' &&
125 version[0] != '3') || version[1] != '.') {
126 fprintf(stderr, "Error: GL version 2.x or better required\n");
127 exit(1);
128 }
129 }
130
131 prog = glCreateProgram();
132
133 if (vertShader) {
134 GLuint vs = CompileShader(GL_VERTEX_SHADER, vertShader);
135 glAttachShader(prog, vs);
136 }
137 if (fragShader) {
138 GLuint fs = CompileShader(GL_FRAGMENT_SHADER, fragShader);
139 glAttachShader(prog, fs);
140 }
141
142 glLinkProgram(prog);
143 glGetProgramiv(prog, GL_LINK_STATUS, &stat);
144 if (!stat) {
145 GLchar log[1000];
146 GLsizei len;
147 glGetProgramInfoLog(prog, 1000, &len, log);
148 fprintf(stderr, "Shader link error:\n%s\n", log);
149 exit(1);
150 }
151
152 return prog;
153 }
154
155
156 int
157 PerfReshapeWindow( unsigned w, unsigned h )
158 {
159 if (glutGet(GLUT_SCREEN_WIDTH) < w ||
160 glutGet(GLUT_SCREEN_HEIGHT) < h)
161 return 0;
162
163 glutReshapeWindow( w, h );
164 glutPostRedisplay();
165 return 1;
166 }
167
168
169 GLboolean
170 PerfExtensionSupported(const char *ext)
171 {
172 return glutExtensionSupported(ext);
173 }
174
175
176 static void
177 Idle(void)
178 {
179 PerfNextRound();
180 }
181
182
183 static void
184 Draw(void)
185 {
186 PerfDraw();
187 glutSwapBuffers();
188 }
189
190
191 static void
192 Reshape(int width, int height)
193 {
194 WinWidth = width;
195 WinHeight = height;
196 glViewport(0, 0, width, height);
197 glMatrixMode(GL_PROJECTION);
198 glLoadIdentity();
199 glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 25.0);
200 glMatrixMode(GL_MODELVIEW);
201 glLoadIdentity();
202 glTranslatef(0.0, 0.0, -15.0);
203 }
204
205
206 static void
207 Key(unsigned char key, int x, int y)
208 {
209 const GLfloat step = 3.0;
210 (void) x;
211 (void) y;
212 switch (key) {
213 case 'z':
214 Zrot -= step;
215 break;
216 case 'Z':
217 Zrot += step;
218 break;
219 case 27:
220 glutDestroyWindow(Win);
221 exit(0);
222 break;
223 }
224 glutPostRedisplay();
225 }
226
227
228 static void
229 SpecialKey(int key, int x, int y)
230 {
231 const GLfloat step = 3.0;
232 (void) x;
233 (void) y;
234 switch (key) {
235 case GLUT_KEY_UP:
236 Xrot -= step;
237 break;
238 case GLUT_KEY_DOWN:
239 Xrot += step;
240 break;
241 case GLUT_KEY_LEFT:
242 Yrot -= step;
243 break;
244 case GLUT_KEY_RIGHT:
245 Yrot += step;
246 break;
247 }
248 glutPostRedisplay();
249 }
250
251
252 int
253 main(int argc, char *argv[])
254 {
255 glutInit(&argc, argv);
256 glutInitWindowSize(WinWidth, WinHeight);
257 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH | GLUT_STENCIL);
258 Win = glutCreateWindow(argv[0]);
259 glewInit();
260 glutReshapeFunc(Reshape);
261 glutKeyboardFunc(Key);
262 glutSpecialFunc(SpecialKey);
263 glutDisplayFunc(Draw);
264 glutIdleFunc(Idle);
265 PerfInit();
266 glutMainLoop();
267 return 0;
268 }