aboutsummaryrefslogtreecommitdiff
path: root/shader.c
blob: 321439a870f220594b938678a42c167c483cd930 (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", 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", debug);
}

GLuint load_shader(GLenum type, const char* const src, size_t src_size) {
	GLuint shader = glCreateShader(type);
	glShaderBinary(1, &shader, GL_SHADER_BINARY_FORMAT_SPIR_V_ARB, src, src_size);
	glSpecializeShaderARB(shader, "main", 0, 0, 0);
	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;
}