#include "shader.h" #include "die.h" void check_shader(GLuint shader) { int success; char debug[LOG_MAX]; glGetShaderiv(shader, GL_COMPILE_STATUS, &success); if (success) return; glGetShaderInfoLog(shader, LOG_MAX, NULL, debug); die("error: shader compilation failed:\n%s\n", debug); } void check_program(GLuint program) { int success; char debug[LOG_MAX]; glGetProgramiv(program, GL_LINK_STATUS, &success); if (success) return; glGetProgramInfoLog(program, LOG_MAX, NULL, debug); die("error: shader linking failed:\n%s\n", debug); } GLuint load_shader(GLenum type, const char* const src, size_t src_size) { GLuint shader = glCreateShader(type); glShaderSource(shader, 1, &src, (const int*) &src_size); glCompileShader(shader); check_shader(shader); return shader; } GLuint frag_shader(const char* const src, size_t src_size) { return load_shader(GL_FRAGMENT_SHADER, src, src_size); } GLuint vert_shader(const char* const src, size_t src_size) { return load_shader(GL_VERTEX_SHADER, src, src_size); } GLuint link_shaders(GLuint vert, GLuint frag) { GLuint shader = glCreateProgram(); glAttachShader(shader, vert); glAttachShader(shader, frag); glLinkProgram(shader); check_program(shader); glDeleteShader(vert); glDeleteShader(frag); return shader; }