From 33980d0bcf540baadd7ee258b6b5372832d13782 Mon Sep 17 00:00:00 2001
From: Chia-I Wu <olvaffe@gmail.com>
Date: Sun, 15 Sep 2013 15:06:41 +0800
Subject: [PATCH] i965: add micro benchmark for sample*

---
 configure.ac                     |   1 +
 src/i965-sample/Makefile.am      |  20 ++++
 src/i965-sample/sample.c         | 207 +++++++++++++++++++++++++++++++++++++++
 src/i965-sample/sample.frag      |   9 ++
 src/i965-sample/sample.vert      |  11 +++
 src/i965-sample/textureGrad.frag |  12 +++
 6 files changed, 260 insertions(+)
 create mode 100644 src/i965-sample/Makefile.am
 create mode 100644 src/i965-sample/sample.c
 create mode 100644 src/i965-sample/sample.frag
 create mode 100644 src/i965-sample/sample.vert
 create mode 100644 src/i965-sample/textureGrad.frag

diff --git a/configure.ac b/configure.ac
index 0c38f4d..bb1e94b 100644
--- a/configure.ac
+++ b/configure.ac
@@ -325,6 +325,7 @@ AC_OUTPUT([
 	src/glsl/Makefile
 	src/gs/Makefile
 	src/data/Makefile
+	src/i965-sample/Makefile
 	src/objviewer/Makefile
 	src/osdemos/Makefile
 	src/perf/Makefile
diff --git a/src/i965-sample/Makefile.am b/src/i965-sample/Makefile.am
new file mode 100644
index 0000000..f899467
--- /dev/null
+++ b/src/i965-sample/Makefile.am
@@ -0,0 +1,20 @@
+AM_CFLAGS = \
+	$(DEMO_CFLAGS) \
+	$(GLUT_CFLAGS) \
+	-I$(top_srcdir)/src/util
+AM_LDFLAGS = \
+	$(DEMO_LIBS) \
+	$(GLUT_LIBS)
+
+if HAVE_GLUT
+bin_PROGRAMS = \
+	sample
+endif
+
+sample_SOURCES = sample.c
+
+sample_LDADD = ../util/libutil.la
+
+EXTRA_DIST = \
+	sample.frag \
+	sample.vert
diff --git a/src/i965-sample/sample.c b/src/i965-sample/sample.c
new file mode 100644
index 0000000..2ab54c9
--- /dev/null
+++ b/src/i965-sample/sample.c
@@ -0,0 +1,207 @@
+#include <stdio.h>
+#include <string.h>
+#include <assert.h>
+#include <math.h>
+#include <sys/time.h>
+#include <GL/glew.h>
+#include "glut_wrap.h"
+#include "readtex.h"
+#include "shaderutil.h"
+
+static GLboolean clear_window = GL_FALSE;
+static GLboolean tilt = GL_FALSE;
+
+static GLuint
+InitShaders(const char *vs_filename, const char *fs_filename)
+{
+   GLuint vs, fs, prog;
+
+   vs = CompileShaderFile(GL_VERTEX_SHADER, vs_filename);
+   fs = CompileShaderFile(GL_FRAGMENT_SHADER, fs_filename);
+   prog = LinkShaders(vs, fs);
+
+   glUseProgram(prog);
+
+   assert(ValidateShaderProgram(prog));
+
+   return prog;
+}
+
+static void
+InitArrays(GLuint prog)
+{
+   static const GLfloat texcoord[4][2] = {
+      { 0.0f, 0.0f },
+      { 1.0f, 0.0f },
+      { 1.0f, 1.0f },
+      { 0.0f, 1.0f }
+   };
+   GLfloat pos[4][2] = {
+      { -0.8f, -0.8f },
+      {  0.8f, -0.8f },
+      {  0.8f,  0.8f },
+      { -0.8f,  0.8f }
+   };
+   GLuint vbo, pos_attr, texcoord_attr;
+
+   if (tilt) {
+      const float c = cos(M_PI / 15.0);
+      const float s = sin(M_PI / 15.0);
+      int i;
+
+      for (i = 0; i < 4; i++) {
+         float x = c * pos[i][0] - s * pos[i][1];
+         float y = s * pos[i][0] + c * pos[i][1];
+         pos[i][0] = x;
+         pos[i][1] = y;
+      }
+   }
+
+   glGenBuffers(1, &vbo);
+   glBindBuffer(GL_ARRAY_BUFFER, vbo);
+   glBufferData(GL_ARRAY_BUFFER, sizeof(pos) + sizeof(texcoord),
+         NULL, GL_STATIC_DRAW);
+
+   glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(pos), pos);
+
+   pos_attr = glGetAttribLocation(prog, "pos_attr");
+   glVertexAttribPointer(pos_attr, 2, GL_FLOAT, GL_FALSE, 0, 0);
+   glEnableVertexAttribArray(pos_attr);
+
+   glBufferSubData(GL_ARRAY_BUFFER, sizeof(pos), sizeof(texcoord), texcoord);
+
+   texcoord_attr = glGetAttribLocation(prog, "texcoord_attr");
+   glVertexAttribPointer(texcoord_attr, 2, GL_FLOAT, GL_FALSE, 0, (void *) sizeof(pos));
+   glEnableVertexAttribArray(texcoord_attr);
+}
+
+static void
+InitTextures(void)
+{
+   const char image[] = DEMOS_DATA_DIR "tile.rgb";
+   GLint width, height;
+   GLenum format;
+   GLubyte *data;
+   GLuint tex;
+
+   data = LoadRGBImage(image, &width, &height, &format);
+   if (!data) {
+      printf("could not read %s\n", image);
+      exit(0);
+   }
+
+   glGenTextures(1, &tex);
+   glBindTexture(GL_TEXTURE_2D, tex);
+
+   glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0,
+         format, GL_UNSIGNED_BYTE, data);
+   free(data);
+
+   //glGenerateMipmap(GL_TEXTURE_2D);
+
+   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
+   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
+}
+
+static void
+InitGL(int argc, char **argv)
+{
+   const char vs_filename[] = "sample.vert";
+   const char *fs_filename = "sample.frag";
+   GLuint prog;
+   int i;
+
+   glewInit();
+
+   if (!GLEW_VERSION_3_0 || !ShadersSupported())
+      exit(1);
+
+   for (i = 1; i < argc; i++) {
+      if (!strcmp(argv[i], "tilt"))
+         tilt = GL_TRUE;
+      else
+         fs_filename = argv[i];
+   }
+
+   printf("GL_RENDERER = %s\n",(const char *) glGetString(GL_RENDERER));
+   printf("TILT = %s\n", (tilt) ? "true" : "false");
+   printf("FS = %s\n", fs_filename);
+
+   prog = InitShaders(vs_filename, fs_filename);
+   InitArrays(prog);
+   InitTextures();
+}
+
+static void
+Reshape(int width, int height)
+{
+   glViewport(0, 0, (GLint) width, (GLint) height);
+}
+
+static void
+Display(void)
+{
+   if (clear_window)
+      glClear(GL_COLOR_BUFFER_BIT);
+
+   glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
+
+   glutSwapBuffers();
+
+   {
+      static int frames;
+      static double last_time;
+      double now;
+      struct timeval tv;
+
+      gettimeofday(&tv, NULL);
+      now = (double) tv.tv_sec + (double) tv.tv_usec / 1000000.0;
+      frames++;
+
+      if (last_time > 0.0) {
+         if (now > last_time + 5.0) {
+            printf("FPS = %.1f\n", frames / (now - last_time));
+            last_time = now;
+            frames = 0;
+         }
+      }
+      else {
+         last_time = now;
+      }
+   }
+}
+
+static void
+Keyboard(unsigned char key, int x, int y)
+{
+   switch (key) {
+   case 27:
+      exit(0);
+      break;
+   }
+}
+
+static void
+Idle(void)
+{
+   glutPostRedisplay();
+}
+
+int
+main(int argc, char **argv)
+{
+   glutInit(&argc, argv);
+   glutInitWindowSize(512, 512);
+   glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
+   glutCreateWindow("i965-sample");
+   glutReshapeFunc(Reshape);
+   glutDisplayFunc(Display);
+   glutKeyboardFunc(Keyboard);
+   glutIdleFunc(Idle);
+
+   InitGL(argc, argv);
+
+   glutMainLoop();
+
+   return 0;
+}
diff --git a/src/i965-sample/sample.frag b/src/i965-sample/sample.frag
new file mode 100644
index 0000000..1a8de6c
--- /dev/null
+++ b/src/i965-sample/sample.frag
@@ -0,0 +1,9 @@
+#version 130
+
+uniform sampler2D tex;
+in vec2 texcoord;
+
+void main()
+{
+   gl_FragColor = texture(tex, texcoord);
+}
diff --git a/src/i965-sample/sample.vert b/src/i965-sample/sample.vert
new file mode 100644
index 0000000..11d6f73
--- /dev/null
+++ b/src/i965-sample/sample.vert
@@ -0,0 +1,11 @@
+#version 130
+
+in vec4 pos_attr;
+in vec2 texcoord_attr;
+out vec2 texcoord;
+
+void main() 
+{
+   gl_Position = pos_attr;
+   texcoord = texcoord_attr;
+}
diff --git a/src/i965-sample/textureGrad.frag b/src/i965-sample/textureGrad.frag
new file mode 100644
index 0000000..dbaab93
--- /dev/null
+++ b/src/i965-sample/textureGrad.frag
@@ -0,0 +1,12 @@
+#version 130
+
+uniform sampler2D tex;
+in vec2 texcoord;
+
+void main()
+{
+   vec2 ddx = dFdx(texcoord);
+   vec2 ddy = dFdy(texcoord);
+
+   gl_FragColor = textureGrad(tex, texcoord, ddx, ddy);
+}
-- 
1.8.3.1

