blob: 69cdbce319e07f0940c610782615b0ad0cb43af0 [file] [log] [blame]
Brian Paul9abbeda2009-09-16 19:33:01 -06001/*
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
Brian Paul05bce082009-09-21 11:58:03 -060029#include <stdio.h>
Brian Paul9abbeda2009-09-16 19:33:01 -060030#include "glmain.h"
31#include <GL/glut.h>
32
33
34static int Win;
35static GLfloat Xrot = 0, Yrot = 0, Zrot = 0;
Brian Paul9abbeda2009-09-16 19:33:01 -060036
37
38/** Return time in seconds */
39double
40PerfGetTime(void)
41{
42 return glutGet(GLUT_ELAPSED_TIME) * 0.001;
43}
44
45
46void
47PerfSwapBuffers(void)
48{
49 glutSwapBuffers();
50}
51
52
Brian Paul05bce082009-09-21 11:58:03 -060053/** make simple checkerboard texture object */
54GLuint
55PerfCheckerTexture(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
91static GLuint
92CompileShader(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 */
116GLuint
117PerfShaderProgram(const char *vertShader, const char *fragShader)
118{
119 GLuint prog;
120 GLint stat;
121
122 {
123 const char *version = (const char *) glGetString(GL_VERSION);
Keith Whitwell6e0b56e2009-09-23 19:03:07 +0100124 if ((version[0] != '2' &&
125 version[0] != '3') || version[1] != '.') {
126 fprintf(stderr, "Error: GL version 2.x or better required\n");
Brian Paul05bce082009-09-21 11:58:03 -0600127 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
Keith Whitwella7b26592009-09-21 16:55:12 +0100156int
157PerfReshapeWindow( 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
Brian Paulcfb0f242009-09-22 11:02:04 -0600169GLboolean
170PerfExtensionSupported(const char *ext)
171{
172 return glutExtensionSupported(ext);
173}
174
175
Brian Paul9abbeda2009-09-16 19:33:01 -0600176static void
177Idle(void)
178{
Keith Whitwella7b26592009-09-21 16:55:12 +0100179 PerfNextRound();
Brian Paul9abbeda2009-09-16 19:33:01 -0600180}
181
182
183static void
184Draw(void)
185{
186 PerfDraw();
187 glutSwapBuffers();
188}
189
190
191static void
192Reshape(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
206static void
207Key(unsigned char key, int x, int y)
208{
209 const GLfloat step = 3.0;
210 (void) x;
211 (void) y;
212 switch (key) {
Brian Paul9abbeda2009-09-16 19:33:01 -0600213 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
228static void
229SpecialKey(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
252int
253main(int argc, char *argv[])
254{
255 glutInit(&argc, argv);
256 glutInitWindowSize(WinWidth, WinHeight);
Brian Paul489f8bc2009-09-23 14:04:25 -0600257 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH | GLUT_STENCIL);
Brian Paul9abbeda2009-09-16 19:33:01 -0600258 Win = glutCreateWindow(argv[0]);
259 glewInit();
260 glutReshapeFunc(Reshape);
261 glutKeyboardFunc(Key);
262 glutSpecialFunc(SpecialKey);
263 glutDisplayFunc(Draw);
Keith Whitwella7b26592009-09-21 16:55:12 +0100264 glutIdleFunc(Idle);
Brian Paul9abbeda2009-09-16 19:33:01 -0600265 PerfInit();
266 glutMainLoop();
267 return 0;
268}