Skip to content
This repository has been archived by the owner on Nov 5, 2020. It is now read-only.

Commit

Permalink
Moved shaders to external glsl files instead of embedding them in code
Browse files Browse the repository at this point in the history
  • Loading branch information
Apoorva committed Aug 21, 2016
1 parent 3808e39 commit 2da0636
Show file tree
Hide file tree
Showing 16 changed files with 384 additions and 389 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ build/linux/*.o
build/linux/*.d build/linux/*.d
build/linux/*.png build/linux/*.png
build/linux/papaya build/linux/papaya
build/linux/shaders/*


## Vim files ## Vim files
*.swp *.swp
Expand Down
8 changes: 5 additions & 3 deletions build/linux/makefile
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -17,14 +17,16 @@ LIBS=-ldl -lGL -lX11 -lXi `pkg-config --cflags --libs gtk+-2.0` -DUSE_GTK
CFLAGS=-I../../src -O0 -Wno-write-strings -Wno-format-security -g CFLAGS=-I../../src -O0 -Wno-write-strings -Wno-format-security -g




papaya: $(OBJS) img papaya: $(OBJS) misc_data
g++ $(OBJS) $(LIBS) $(CFLAGS) -o $@ g++ $(OBJS) $(LIBS) $(CFLAGS) -o $@


$(OBJS): %.o: %.cpp $(OBJS): %.o: %.cpp
g++ -MMD -MF $@.d $< $(LIBS) $(CFLAGS) -o $@ -c g++ -MMD -MF $@.d $< $(LIBS) $(CFLAGS) -o $@ -c


img: ../../img/ui.png misc_data: ../../img/ui.png ../../src/shaders
cp -u $^ . cp -ru $^ .


clean: clean:
rm -f *.d *.o *.png papaya rm -f *.d *.o *.png papaya
rm -rf shaders

54 changes: 39 additions & 15 deletions src/libs/gl_util.h
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -30,14 +30,13 @@ struct Mesh {


namespace gl { namespace gl {
void check_error(char* expr, char* file, int line); void check_error(char* expr, char* file, int line);
void compile_shader(Shader& shader, const char* file, int line, char* read_file(char* file_name);
const char* vert, const char* frag, void compile_shader(Shader& shader, char* vert_file, char* frag_file,
int32 attrib_count, int32 uniform_count, ...); int32 attrib_count, int32 uniform_count, ...);
void set_vertex_attribs(Shader& shader); void set_vertex_attribs(Shader& shader);
void init_quad(Mesh& mesh, Vec2 pos, Vec2 size, uint32 usage); void init_quad(Mesh& mesh, Vec2 pos, Vec2 size, uint32 usage);
void transform_quad(Mesh& mesh, Vec2 pos, Vec2 size); void transform_quad(Mesh& mesh, Vec2 pos, Vec2 size);
void draw_mesh(Mesh& mesh, Shader& shader, bool scissor, void draw_mesh(Mesh& mesh, Shader& shader, bool scissor, int32 uniform_count, ...);
int32 uniform_count, ...);
uint32 allocate_tex(int32 width, int32 height, uint8* data = 0); uint32 allocate_tex(int32 width, int32 height, uint8* data = 0);
} }


Expand All @@ -47,6 +46,9 @@ namespace gl {


#ifdef GL_UTIL_IMPLEMENTATION #ifdef GL_UTIL_IMPLEMENTATION


#include <stdio.h>
#include <stdlib.h>

void gl::check_error(char* expr, char* file, int line) void gl::check_error(char* expr, char* file, int line)
{ {
GLenum error = glGetError(); GLenum error = glGetError();
Expand All @@ -66,13 +68,30 @@ void gl::check_error(char* expr, char* file, int line)
printf(" --- Expression: %s\n", expr); printf(" --- Expression: %s\n", expr);
} }


internal void print_compilation_errors(uint32 handle, const char* type, const char* file, char* gl::read_file(char* file_name)
int32 line) {
char path[512];
snprintf(path, sizeof(path), "shaders/%s", file_name);
FILE* f = fopen(path, "rb");
fseek(f, 0, SEEK_END);
size_t f_size = ftell(f);
fseek(f, 0, SEEK_SET);

char* buf = (char*)malloc(f_size + 1);
fread(buf, f_size, 1, f);
fclose(f);

buf[f_size] = 0;
return buf;
}


internal void print_compilation_errors(uint32 handle, char* glsl_file)
{ {
int32 compilation_status; int32 compilation_status;
GLCHK( glGetShaderiv(handle, GL_COMPILE_STATUS, &compilation_status) ); GLCHK( glGetShaderiv(handle, GL_COMPILE_STATUS, &compilation_status) );
if (compilation_status != GL_TRUE) { if (compilation_status != GL_TRUE) {
printf("Compilation error in %s shader in %s:%d\n", type, file, line); printf("Compilation error in %s\n", glsl_file);


char log[4096]; char log[4096];
int32 out_length; int32 out_length;
Expand All @@ -82,23 +101,28 @@ internal void print_compilation_errors(uint32 handle, const char* type, const ch
} }
} }


void gl::compile_shader(Shader& shader, const char* file, int line, void gl::compile_shader(Shader& shader, char* vert_file, char* frag_file,
const char* vert, const char* frag,
int32 attrib_count, int32 uniform_count, ...) int32 attrib_count, int32 uniform_count, ...)
{ {
shader.handle = GLCHK( glCreateProgram() ); shader.handle = GLCHK( glCreateProgram() );
uint32 vert_handle = GLCHK( glCreateShader(GL_VERTEX_SHADER) ); uint32 vert_handle = GLCHK( glCreateShader(GL_VERTEX_SHADER) );
uint32 frag_handle = GLCHK( glCreateShader(GL_FRAGMENT_SHADER) ); uint32 frag_handle = GLCHK( glCreateShader(GL_FRAGMENT_SHADER) );


GLCHK( glShaderSource (vert_handle, 1, &vert, 0) ); {
GLCHK( glShaderSource (frag_handle, 1, &frag, 0) ); char* vert = read_file(vert_file);
char* frag = read_file(frag_file);
GLCHK( glShaderSource (vert_handle, 1, &vert, 0) );
GLCHK( glShaderSource (frag_handle, 1, &frag, 0) );
free(vert);
free(frag);
}
GLCHK( glCompileShader(vert_handle) ); GLCHK( glCompileShader(vert_handle) );
GLCHK( glCompileShader(frag_handle) ); GLCHK( glCompileShader(frag_handle) );
GLCHK( glAttachShader (shader.handle, vert_handle) ); GLCHK( glAttachShader (shader.handle, vert_handle) );
GLCHK( glAttachShader (shader.handle, frag_handle) ); GLCHK( glAttachShader (shader.handle, frag_handle) );


print_compilation_errors(vert_handle, "vertex" , file, line); print_compilation_errors(vert_handle, vert_file);
print_compilation_errors(frag_handle, "fragment", file, line); print_compilation_errors(frag_handle, frag_file);


GLCHK( glLinkProgram(shader.handle) ); // TODO: Print linking errors GLCHK( glLinkProgram(shader.handle) ); // TODO: Print linking errors


Expand All @@ -111,7 +135,7 @@ void gl::compile_shader(Shader& shader, const char* file, int line,
shader.attribs[i] = GLCHK( glGetAttribLocation(shader.handle, name) ); shader.attribs[i] = GLCHK( glGetAttribLocation(shader.handle, name) );


if (shader.attribs[i] == -1) { if (shader.attribs[i] == -1) {
printf("Attribute %s not found in shader at %s:%d\n", name, file, line); printf("Attribute %s not found in %s\n", name, frag_file);
} }
} }


Expand All @@ -120,7 +144,7 @@ void gl::compile_shader(Shader& shader, const char* file, int line,
shader.uniforms[i] = GLCHK( glGetUniformLocation(shader.handle, name) ); shader.uniforms[i] = GLCHK( glGetUniformLocation(shader.handle, name) );


if (shader.uniforms[i] == -1) { if (shader.uniforms[i] == -1) {
printf("Uniform %s not found in shader at %s:%d\n", name, file, line); printf("Uniform %s not found in shader at %s\n", name, frag_file);
} }
} }
va_end(args); va_end(args);
Expand Down
Loading

0 comments on commit 2da0636

Please sign in to comment.