From d7b1610cedbd8139bdecd41556094832b1dd1e6f Mon Sep 17 00:00:00 2001 From: Keith Whitwell Date: Tue, 2 Feb 2010 20:01:30 +0000 Subject: progs/fpglsl: fp-tris for glsl --- progs/fpglsl/.gitignore | 1 + progs/fpglsl/Makefile | 52 ++++++ progs/fpglsl/SConscript | 13 ++ progs/fpglsl/fp-tri.c | 415 ++++++++++++++++++++++++++++++++++++++++++++++ progs/fpglsl/mov-imm.glsl | 3 + progs/fpglsl/mov.glsl | 3 + 6 files changed, 487 insertions(+) create mode 100644 progs/fpglsl/.gitignore create mode 100644 progs/fpglsl/Makefile create mode 100644 progs/fpglsl/SConscript create mode 100644 progs/fpglsl/fp-tri.c create mode 100644 progs/fpglsl/mov-imm.glsl create mode 100644 progs/fpglsl/mov.glsl (limited to 'progs/fpglsl') diff --git a/progs/fpglsl/.gitignore b/progs/fpglsl/.gitignore new file mode 100644 index 0000000000..9fe73ab067 --- /dev/null +++ b/progs/fpglsl/.gitignore @@ -0,0 +1 @@ +fp-tri diff --git a/progs/fpglsl/Makefile b/progs/fpglsl/Makefile new file mode 100644 index 0000000000..3bf14b4b70 --- /dev/null +++ b/progs/fpglsl/Makefile @@ -0,0 +1,52 @@ +# progs/tests/Makefile + + +# These programs aren't intended to be included with the normal distro. +# They're not too interesting but they're good for testing. + +TOP = ../.. +include $(TOP)/configs/current + +LIBS = -L$(TOP)/$(LIB_DIR) -l$(GLUT_LIB) -l$(GLEW_LIB) -l$(GLU_LIB) -l$(GL_LIB) $(APP_LIB_DEPS) + +SOURCES = \ + fp-tri.c + + + +PROGS = $(SOURCES:%.c=%) + +INCLUDES = -I. -I$(TOP)/include -I../samples + + +##### RULES ##### + +.SUFFIXES: +.SUFFIXES: .c + +.c: + $(CC) $(INCLUDES) $(CFLAGS) $< $(LIBS) -o $@ + +.c.o: + $(CC) -c $(INCLUDES) $(CFLAGS) $(DEFINES) $< -o $@ + +.S.o: + $(CC) -c $(INCLUDES) $(CFLAGS) $(DEFINES) $< -o $@ + + +##### TARGETS ##### + +default: $(PROGS) + +clean: + rm -f $(PROGS) + rm -f *.o + rm -f getproclist.h + + + + + +# Emacs tags +tags: + etags `find . -name \*.[ch]` `find ../include` diff --git a/progs/fpglsl/SConscript b/progs/fpglsl/SConscript new file mode 100644 index 0000000000..e31fa32023 --- /dev/null +++ b/progs/fpglsl/SConscript @@ -0,0 +1,13 @@ +Import('env') + +if not env['GLUT']: + Return() + +env = env.Clone() + +env.Prepend(LIBS = ['$GLUT_LIB']) + +env.Program( + target = 'fp-tri', + source = ['fp-tri.c'], + ) diff --git a/progs/fpglsl/fp-tri.c b/progs/fpglsl/fp-tri.c new file mode 100644 index 0000000000..c9b08fbbad --- /dev/null +++ b/progs/fpglsl/fp-tri.c @@ -0,0 +1,415 @@ + +#include +#include +#include + +#ifndef WIN32 +#include +#include +#endif + +#include +#include + +#include "readtex.c" + + +#define TEXTURE_FILE "../images/bw.rgb" + +unsigned show_fps = 0; +unsigned int frame_cnt = 0; +void alarmhandler(int); +static const char *filename = NULL; + +static GLuint fragShader; +static GLuint vertShader; +static GLuint program; + + +static void usage(char *name) +{ + fprintf(stderr, "usage: %s [ options ] shader_filename\n", name); +#ifndef WIN32 + fprintf(stderr, "\n" ); + fprintf(stderr, "options:\n"); + fprintf(stderr, " -fps show frames per second\n"); +#endif +} + +#ifndef WIN32 +void alarmhandler (int sig) +{ + if (sig == SIGALRM) { + printf("%d frames in 5.0 seconds = %.3f FPS\n", frame_cnt, + frame_cnt / 5.0); + + frame_cnt = 0; + } + signal(SIGALRM, alarmhandler); + alarm(5); +} +#endif + + + + +static void load_and_compile_shader(GLuint shader, const char *text) +{ + GLint stat; + + glShaderSource(shader, 1, (const GLchar **) &text, NULL); + + glCompileShader(shader); + + glGetShaderiv(shader, GL_COMPILE_STATUS, &stat); + if (!stat) { + GLchar log[1000]; + GLsizei len; + glGetShaderInfoLog(shader, 1000, &len, log); + fprintf(stderr, "fp-tri: problem compiling shader:\n%s\n", log); + exit(1); + } +} + +static void read_shader(GLuint shader, const char *filename) +{ + const int max = 100*1000; + int n; + char *buffer = (char*) malloc(max); + FILE *f = fopen(filename, "r"); + if (!f) { + fprintf(stderr, "fp-tri: Unable to open shader file %s\n", filename); + exit(1); + } + + n = fread(buffer, 1, max, f); + printf("fp-tri: read %d bytes from shader file %s\n", n, filename); + if (n > 0) { + buffer[n] = 0; + load_and_compile_shader(shader, buffer); + } + + fclose(f); + free(buffer); +} + +static void check_link(GLuint prog) +{ + GLint stat; + glGetProgramiv(prog, GL_LINK_STATUS, &stat); + if (!stat) { + GLchar log[1000]; + GLsizei len; + glGetProgramInfoLog(prog, 1000, &len, log); + fprintf(stderr, "Linker error:\n%s\n", log); + } +} + +static void setup_uniforms() +{ + { + GLint loc1f = glGetUniformLocationARB(program, "Offset1f"); + GLint loc2f = glGetUniformLocationARB(program, "Offset2f"); + GLint loc4f = glGetUniformLocationARB(program, "Offset4f"); + GLfloat vecKer[] = + { 1.0, 0.0, 0.0, 1.0, + 0.0, 1.0, 0.0, 1.0, + 1.0, 0.0, 0.0, 1.0, + 0.0, 0.0, 0.0, 1.0 + }; + if (loc1f >= 0) + glUniform1fv(loc1f, 16, vecKer); + + if (loc2f >= 0) + glUniform2fv(loc2f, 8, vecKer); + + if (loc4f >= 0) + glUniform4fv(loc4f, 4, vecKer); + + } + + { + GLint loc1f = glGetUniformLocationARB(program, "KernelValue1f"); + GLint loc2f = glGetUniformLocationARB(program, "KernelValue2f"); + GLint loc4f = glGetUniformLocationARB(program, "KernelValue4f"); + GLfloat vecKer[] = + { 1.0, 0.0, 0.0, 0.25, + 0.0, 1.0, 0.0, 0.25, + 0.0, 0.0, 1.0, 0.25, + 0.0, 0.0, 0.0, 0.25, + 0.5, 0.0, 0.0, 0.35, + 0.0, 0.5, 0.0, 0.35, + 0.0, 0.0, 0.5, 0.35, + 0.0, 0.0, 0.0, 0.35 + }; + if (loc1f >= 0) + glUniform1fv(loc1f, 16, vecKer); + + if (loc2f >= 0) + glUniform2fv(loc2f, 8, vecKer); + + if (loc4f >= 0) + glUniform4fv(loc4f, 4, vecKer); + } +} + +static void prepare_shaders() +{ + static const char *fragShaderText = + "void main() {\n" + " gl_FragColor = gl_Color;\n" + "}\n"; + static const char *vertShaderText = + "void main() {\n" + " gl_FrontColor = gl_Color;\n" + " gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n" + "}\n"; + fragShader = glCreateShader(GL_FRAGMENT_SHADER); + if (filename) + read_shader(fragShader, filename); + else + load_and_compile_shader(fragShader, fragShaderText); + + + vertShader = glCreateShader(GL_VERTEX_SHADER); + load_and_compile_shader(vertShader, vertShaderText); + + program = glCreateProgram(); + glAttachShader(program, fragShader); + glAttachShader(program, vertShader); + glLinkProgram(program); + check_link(program); + glUseProgram(program); + + setup_uniforms(); +} + +#define LEVELS 8 +#define SIZE (1<