From 05bce0898087b135cb18bc4f2f2896da034928dc Mon Sep 17 00:00:00 2001 From: Brian Paul Date: Mon, 21 Sep 2009 11:58:03 -0600 Subject: progs/perf: added helpers for creating simple textures and shader programs --- progs/perf/glmain.c | 103 ++++++++++++++++++++++++++++++++++++++++++++++++++++ progs/perf/glmain.h | 6 +++ 2 files changed, 109 insertions(+) (limited to 'progs/perf') diff --git a/progs/perf/glmain.c b/progs/perf/glmain.c index 62d14259f8..83c7b8a79c 100644 --- a/progs/perf/glmain.c +++ b/progs/perf/glmain.c @@ -26,6 +26,7 @@ */ +#include #include "glmain.h" #include @@ -50,6 +51,108 @@ PerfSwapBuffers(void) } +/** make simple checkerboard texture object */ +GLuint +PerfCheckerTexture(GLsizei width, GLsizei height) +{ + const GLenum filter = GL_NEAREST; + GLubyte *img = (GLubyte *) malloc(width * height * 4); + GLint i, j, k; + GLuint obj; + + k = 0; + for (i = 0; i < height; i++) { + for (j = 0; j < width; j++) { + GLubyte color; + if (((i / 8) ^ (j / 8)) & 1) { + color = 0xff; + } + else { + color = 0x0; + } + img[k++] = color; + img[k++] = color; + img[k++] = color; + img[k++] = color; + } + } + + glGenTextures(1, &obj); + glBindTexture(GL_TEXTURE_2D, obj); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filter); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filter); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, + GL_RGBA, GL_UNSIGNED_BYTE, img); + free(img); + + return obj; +} + + +static GLuint +CompileShader(GLenum type, const char *shader) +{ + GLuint sh; + GLint stat; + + sh = glCreateShader(type); + glShaderSource(sh, 1, (const GLchar **) &shader, NULL); + + glCompileShader(sh); + + glGetShaderiv(sh, GL_COMPILE_STATUS, &stat); + if (!stat) { + GLchar log[1000]; + GLsizei len; + glGetShaderInfoLog(sh, 1000, &len, log); + fprintf(stderr, "Error: problem compiling shader: %s\n", log); + exit(1); + } + + return sh; +} + + +/** Make shader program from given vert/frag shader text */ +GLuint +PerfShaderProgram(const char *vertShader, const char *fragShader) +{ + GLuint prog; + GLint stat; + + { + const char *version = (const char *) glGetString(GL_VERSION); + if (version[0] != '2' || version[1] != '.') { + fprintf(stderr, "Error: GL version 2.x required\n"); + exit(1); + } + } + + prog = glCreateProgram(); + + if (vertShader) { + GLuint vs = CompileShader(GL_VERTEX_SHADER, vertShader); + glAttachShader(prog, vs); + } + if (fragShader) { + GLuint fs = CompileShader(GL_FRAGMENT_SHADER, fragShader); + glAttachShader(prog, fs); + } + + glLinkProgram(prog); + glGetProgramiv(prog, GL_LINK_STATUS, &stat); + if (!stat) { + GLchar log[1000]; + GLsizei len; + glGetProgramInfoLog(prog, 1000, &len, log); + fprintf(stderr, "Shader link error:\n%s\n", log); + exit(1); + } + + return prog; +} + + static void Idle(void) { diff --git a/progs/perf/glmain.h b/progs/perf/glmain.h index fe11d7235e..91f2eb3e74 100644 --- a/progs/perf/glmain.h +++ b/progs/perf/glmain.h @@ -40,6 +40,12 @@ PerfGetTime(void); extern void PerfSwapBuffers(void); +extern GLuint +PerfCheckerTexture(GLsizei width, GLsizei height); + +extern GLuint +PerfShaderProgram(const char *vertShader, const char *fragShader); + /** Test programs must implement these functions **/ -- cgit v1.2.3