132001f49Smrg/*
232001f49Smrg * Copyright (C) 2009  VMware, Inc.  All Rights Reserved.
332001f49Smrg *
432001f49Smrg * Permission is hereby granted, free of charge, to any person obtaining a
532001f49Smrg * copy of this software and associated documentation files (the "Software"),
632001f49Smrg * to deal in the Software without restriction, including without limitation
732001f49Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
832001f49Smrg * and/or sell copies of the Software, and to permit persons to whom the
932001f49Smrg * Software is furnished to do so, subject to the following conditions:
1032001f49Smrg *
1132001f49Smrg * The above copyright notice and this permission notice shall be included
1232001f49Smrg * in all copies or substantial portions of the Software.
1332001f49Smrg *
1432001f49Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
1532001f49Smrg * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1632001f49Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
1732001f49Smrg * VMWARE BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
1832001f49Smrg * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
1932001f49Smrg * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2032001f49Smrg */
2132001f49Smrg
2232001f49Smrg/**
2332001f49Smrg * OpenGL/GLUT common code for perf programs.
2432001f49Smrg * Brian Paul
2532001f49Smrg * 15 Sep 2009
2632001f49Smrg */
2732001f49Smrg
2832001f49Smrg
2932001f49Smrg#include <stdio.h>
3032001f49Smrg#include "glmain.h"
3132001f49Smrg#include "glut_wrap.h"
3232001f49Smrg
3332001f49Smrg
3432001f49Smrgstatic int Win;
3532001f49Smrgstatic GLfloat Xrot = 0, Yrot = 0, Zrot = 0;
3632001f49Smrg
3732001f49Smrg
3832001f49Smrg/** Return time in seconds */
3932001f49Smrgdouble
4032001f49SmrgPerfGetTime(void)
4132001f49Smrg{
4232001f49Smrg   return glutGet(GLUT_ELAPSED_TIME) * 0.001;
4332001f49Smrg}
4432001f49Smrg
4532001f49Smrg
4632001f49Smrgvoid
4732001f49SmrgPerfSwapBuffers(void)
4832001f49Smrg{
4932001f49Smrg   glutSwapBuffers();
5032001f49Smrg}
5132001f49Smrg
5232001f49Smrg
5332001f49Smrg/** make simple checkerboard texture object */
5432001f49SmrgGLuint
5532001f49SmrgPerfCheckerTexture(GLsizei width, GLsizei height)
5632001f49Smrg{
5732001f49Smrg   const GLenum filter = GL_NEAREST;
5832001f49Smrg   GLubyte *img = (GLubyte *) malloc(width * height * 4);
5932001f49Smrg   GLint i, j, k;
6032001f49Smrg   GLuint obj;
6132001f49Smrg
6232001f49Smrg   k = 0;
6332001f49Smrg   for (i = 0; i < height; i++) {
6432001f49Smrg      for (j = 0; j < width; j++) {
6532001f49Smrg         GLubyte color;
6632001f49Smrg         if (((i / 8) ^ (j / 8)) & 1) {
6732001f49Smrg            color = 0xff;
6832001f49Smrg         }
6932001f49Smrg         else {
7032001f49Smrg            color = 0x0;
7132001f49Smrg         }
7232001f49Smrg         img[k++] = color;
7332001f49Smrg         img[k++] = color;
7432001f49Smrg         img[k++] = color;
7532001f49Smrg         img[k++] = color;
7632001f49Smrg      }
7732001f49Smrg   }
7832001f49Smrg
7932001f49Smrg   glGenTextures(1, &obj);
8032001f49Smrg   glBindTexture(GL_TEXTURE_2D, obj);
8132001f49Smrg   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filter);
8232001f49Smrg   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filter);
8332001f49Smrg   glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0,
8432001f49Smrg                GL_RGBA, GL_UNSIGNED_BYTE, img);
8532001f49Smrg   free(img);
8632001f49Smrg
8732001f49Smrg   return obj;
8832001f49Smrg}
8932001f49Smrg
9032001f49Smrg
9132001f49Smrgstatic GLuint
9232001f49SmrgCompileShader(GLenum type, const char *shader)
9332001f49Smrg{
9432001f49Smrg   GLuint sh;
9532001f49Smrg   GLint stat;
9632001f49Smrg
9732001f49Smrg   sh = glCreateShader(type);
9832001f49Smrg   glShaderSource(sh, 1, (const GLchar **) &shader, NULL);
9932001f49Smrg
10032001f49Smrg   glCompileShader(sh);
10132001f49Smrg
10232001f49Smrg   glGetShaderiv(sh, GL_COMPILE_STATUS, &stat);
10332001f49Smrg   if (!stat) {
10432001f49Smrg      GLchar log[1000];
10532001f49Smrg      GLsizei len;
10632001f49Smrg      glGetShaderInfoLog(sh, 1000, &len, log);
10732001f49Smrg      fprintf(stderr, "Error: problem compiling shader: %s\n", log);
10832001f49Smrg      exit(1);
10932001f49Smrg   }
11032001f49Smrg
11132001f49Smrg   return sh;
11232001f49Smrg}
11332001f49Smrg
11432001f49Smrg
11532001f49Smrg/** Make shader program from given vert/frag shader text */
11632001f49SmrgGLuint
11732001f49SmrgPerfShaderProgram(const char *vertShader, const char *fragShader)
11832001f49Smrg{
11932001f49Smrg   GLuint prog;
12032001f49Smrg   GLint stat;
12132001f49Smrg
12232001f49Smrg   if (!GLEW_VERSION_2_0) {
12332001f49Smrg      fprintf(stderr, "Error: GL version 2.x or better required\n");
12432001f49Smrg      exit(1);
12532001f49Smrg   }
12632001f49Smrg
12732001f49Smrg   prog = glCreateProgram();
12832001f49Smrg
12932001f49Smrg   if (vertShader) {
13032001f49Smrg      GLuint vs = CompileShader(GL_VERTEX_SHADER, vertShader);
13132001f49Smrg      glAttachShader(prog, vs);
13232001f49Smrg   }
13332001f49Smrg   if (fragShader) {
13432001f49Smrg      GLuint fs = CompileShader(GL_FRAGMENT_SHADER, fragShader);
13532001f49Smrg      glAttachShader(prog, fs);
13632001f49Smrg   }
13732001f49Smrg
13832001f49Smrg   glLinkProgram(prog);
13932001f49Smrg   glGetProgramiv(prog, GL_LINK_STATUS, &stat);
14032001f49Smrg   if (!stat) {
14132001f49Smrg      GLchar log[1000];
14232001f49Smrg      GLsizei len;
14332001f49Smrg      glGetProgramInfoLog(prog, 1000, &len, log);
14432001f49Smrg      fprintf(stderr, "Shader link error:\n%s\n", log);
14532001f49Smrg      exit(1);
14632001f49Smrg   }
14732001f49Smrg
14832001f49Smrg   return prog;
14932001f49Smrg}
15032001f49Smrg
15132001f49Smrg
15232001f49Smrgint
15332001f49SmrgPerfReshapeWindow( unsigned w, unsigned h )
15432001f49Smrg{
15532001f49Smrg   if (glutGet(GLUT_SCREEN_WIDTH) < w ||
15632001f49Smrg       glutGet(GLUT_SCREEN_HEIGHT) < h)
15732001f49Smrg      return 0;
15832001f49Smrg
15932001f49Smrg   glutReshapeWindow( w, h );
16032001f49Smrg   glutPostRedisplay();
16132001f49Smrg   return 1;
16232001f49Smrg}
16332001f49Smrg
16432001f49Smrg
16532001f49SmrgGLboolean
16632001f49SmrgPerfExtensionSupported(const char *ext)
16732001f49Smrg{
16832001f49Smrg   return glutExtensionSupported(ext);
16932001f49Smrg}
17032001f49Smrg
17132001f49Smrg
17232001f49Smrgstatic void
17332001f49SmrgIdle(void)
17432001f49Smrg{
17532001f49Smrg   PerfNextRound();
17632001f49Smrg}
17732001f49Smrg
17832001f49Smrg
17932001f49Smrgstatic void
18032001f49SmrgDraw(void)
18132001f49Smrg{
18232001f49Smrg   PerfDraw();
18332001f49Smrg   glutSwapBuffers();
18432001f49Smrg}
18532001f49Smrg
18632001f49Smrg
18732001f49Smrgstatic void
18832001f49SmrgReshape(int width, int height)
18932001f49Smrg{
19032001f49Smrg   WinWidth = width;
19132001f49Smrg   WinHeight = height;
19232001f49Smrg   glViewport(0, 0, width, height);
19332001f49Smrg   glMatrixMode(GL_PROJECTION);
19432001f49Smrg   glLoadIdentity();
19532001f49Smrg   glFrustum(-1.0, 1.0, -1.0, 1.0, 5.0, 25.0);
19632001f49Smrg   glMatrixMode(GL_MODELVIEW);
19732001f49Smrg   glLoadIdentity();
19832001f49Smrg   glTranslatef(0.0, 0.0, -15.0);
19932001f49Smrg}
20032001f49Smrg
20132001f49Smrg
20232001f49Smrgstatic void
20332001f49SmrgKey(unsigned char key, int x, int y)
20432001f49Smrg{
20532001f49Smrg   const GLfloat step = 3.0;
20632001f49Smrg   (void) x;
20732001f49Smrg   (void) y;
20832001f49Smrg   switch (key) {
20932001f49Smrg   case 'z':
21032001f49Smrg      Zrot -= step;
21132001f49Smrg      break;
21232001f49Smrg   case 'Z':
21332001f49Smrg      Zrot += step;
21432001f49Smrg      break;
21532001f49Smrg   case 27:
21632001f49Smrg      glutDestroyWindow(Win);
21732001f49Smrg      exit(0);
21832001f49Smrg      break;
21932001f49Smrg   }
22032001f49Smrg   glutPostRedisplay();
22132001f49Smrg}
22232001f49Smrg
22332001f49Smrg
22432001f49Smrgstatic void
22532001f49SmrgSpecialKey(int key, int x, int y)
22632001f49Smrg{
22732001f49Smrg   const GLfloat step = 3.0;
22832001f49Smrg   (void) x;
22932001f49Smrg   (void) y;
23032001f49Smrg   switch (key) {
23132001f49Smrg   case GLUT_KEY_UP:
23232001f49Smrg      Xrot -= step;
23332001f49Smrg      break;
23432001f49Smrg   case GLUT_KEY_DOWN:
23532001f49Smrg      Xrot += step;
23632001f49Smrg      break;
23732001f49Smrg   case GLUT_KEY_LEFT:
23832001f49Smrg      Yrot -= step;
23932001f49Smrg      break;
24032001f49Smrg   case GLUT_KEY_RIGHT:
24132001f49Smrg      Yrot += step;
24232001f49Smrg      break;
24332001f49Smrg   }
24432001f49Smrg   glutPostRedisplay();
24532001f49Smrg}
24632001f49Smrg
24732001f49Smrg
24832001f49Smrgint
24932001f49Smrgmain(int argc, char *argv[])
25032001f49Smrg{
25132001f49Smrg   glutInit(&argc, argv);
25232001f49Smrg   glutInitWindowSize(WinWidth, WinHeight);
25332001f49Smrg   glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH | GLUT_STENCIL);
25432001f49Smrg   Win = glutCreateWindow(argv[0]);
25532001f49Smrg   glewInit();
25632001f49Smrg   glutReshapeFunc(Reshape);
25732001f49Smrg   glutKeyboardFunc(Key);
25832001f49Smrg   glutSpecialFunc(SpecialKey);
25932001f49Smrg   glutDisplayFunc(Draw);
26032001f49Smrg   glutIdleFunc(Idle);
26132001f49Smrg   PerfInit();
26232001f49Smrg   glutMainLoop();
26332001f49Smrg   return 0;
26432001f49Smrg}
265