aboutsummaryrefslogtreecommitdiff
path: root/shader.c
blob: df17d5de3011cdefc737396954c681dca308da74 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#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;
}