summaryrefslogtreecommitdiff
path: root/src/shader.c
blob: abb62a2f8abeabec884e8135883f8b1f481c4896 (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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#include <GL/glew.h>
#include <stdlib.h>
#include <string.h>

#include "voxel.h"
#include "utils.h"
#include "list.h"
#include "gl.h"

static void print_shader_log(const char *filename, u32 id)
{
	i32 log_len;
	char *log;
	glGetShaderiv(id, GL_INFO_LOG_LENGTH, &log_len);
	log = xalloc(log_len + 1);
	glGetShaderInfoLog(id, log_len, &log_len, log);
	log[log_len] = 0;
	ERROR("failed to compile shader: '%s'\n%s", filename, log);
	free(log);
}

static GL_RESULT compile_shader(u32 *out, const char *filename, const char *code, u32 type)
{
	u32 id;
	i32 status, code_len;

	id = glCreateShader(type);
	code_len = strlen(code);
	glShaderSource(id, 1, &code, &code_len);
	glCompileShader(id);
	glGetShaderiv(id, GL_COMPILE_STATUS, &status);
	if (status == GL_FALSE) {
		print_shader_log(filename, id);
		glDeleteShader(id);
		return GL_ERROR;
	}

	*out = id;
	return GL_OK;
}

static void locate_attributes(Shader *shader)
{
	i32 count, max_len;
	List names, locations;

	list_init_string(&names);
	list_init_i32(&locations);

	glGetProgramiv(shader->program_id, GL_ACTIVE_ATTRIBUTES, &count);
	glGetProgramiv(shader->program_id, GL_ACTIVE_ATTRIBUTE_MAX_LENGTH, &max_len);
	DEBUG("shader %d has %d attributes", shader->program_id, count);

	for (i32 i = 0; i < count; i++) {
		i32 size, location;
		u32 type;
		char *name;

		name = xalloc(max_len + 1);
		glGetActiveAttrib(shader->program_id, i, max_len, NULL, &size, &type, name);

		location = glGetAttribLocation(shader->program_id, name);
		DEBUG("shader %d located attribute %s at location %d", shader->program_id, name,
			  location);

		list_push_string(&names, name);
		list_push_i32(&locations, location);
	}

	shader->attribute_names = names;
	shader->attribute_locations = locations;
}

static void locate_uniforms(Shader *shader)
{
	i32 count, max_len;
	List names, locations;

	list_init_string(&names);
	list_init_i32(&locations);

	glGetProgramiv(shader->program_id, GL_ACTIVE_UNIFORMS, &count);
	glGetProgramiv(shader->program_id, GL_ACTIVE_UNIFORM_MAX_LENGTH, &max_len);
	DEBUG("shader %d has %d uniforms", shader->program_id, count);

	for (i32 i = 0; i < count; i++) {
		i32 size, location;
		u32 type;
		char *name;

		name = xalloc(max_len + 1);
		glGetActiveUniform(shader->program_id, i, max_len, NULL, &size, &type, name);

		location = glGetUniformLocation(shader->program_id, name);
		DEBUG("shader %d located uniform %s at location %d", shader->program_id, name,
			  location);

		list_push_string(&names, name);
		list_push_i32(&locations, location);
	}

	shader->uniform_names = names;
	shader->uniform_locations = locations;
}

static void locate_uniform_blocks(Shader *shader)
{
	i32 count;
	List names, indicies;

	list_init_string(&names);
	list_init_i32(&indicies);

	glGetProgramiv(shader->program_id, GL_ACTIVE_UNIFORM_BLOCKS, &count);
	DEBUG("shader %d has %d uniform blocks", shader->program_id, count);

	for (i32 i = 0; i < count; i++) {
		i32 index, name_len;
		char *name;

		glGetActiveUniformBlockiv(shader->program_id, i, GL_UNIFORM_BLOCK_NAME_LENGTH,
								  &name_len);
		name = xalloc(name_len + 1);
		glGetActiveUniformBlockName(shader->program_id, i, name_len, NULL, name);

		index = glGetUniformBlockIndex(shader->program_id, name);
		DEBUG("shader %d located uniform block %s at index %d", shader->program_id, name,
			  index);

		list_push_string(&names, name);
		list_push_i32(&indicies, index);
	}

	shader->uniform_block_names = names;
	shader->uniform_block_indicies = indicies;
}

GL_RESULT shader_init(Shader *shader, const char *vertex_file, const char *fragment_file)
{
	char *vertex, *fragment;

	// read shader code from file
	vertex = read_file(vertex_file);
	fragment = read_file(fragment_file);
	if (!vertex || !fragment)
		goto failure;

	// compile shaders
	if (compile_shader(&shader->vertex_id, vertex_file, vertex, GL_VERTEX_SHADER))
		goto failure;
	if (compile_shader(&shader->fragment_id, fragment_file, fragment, GL_FRAGMENT_SHADER))
		goto failure;

	shader->program_id = glCreateProgram();
	glAttachShader(shader->program_id, shader->vertex_id);
	glAttachShader(shader->program_id, shader->fragment_id);
	glLinkProgram(shader->program_id);
	glValidateProgram(shader->program_id);

	locate_attributes(shader);
	locate_uniforms(shader);
	locate_uniform_blocks(shader);

	if (vertex)
		free(vertex);
	if (fragment)
		free(fragment);

	return GL_OK;

failure:
	if (vertex)
		free(vertex);
	if (fragment)
		free(fragment);
	free(shader);
	return GL_ERROR;
}

void shader_bind(Shader *shader)
{
	glEnable(GL_DEPTH_TEST);
	glEnable(GL_CULL_FACE);
	glCullFace(GL_BACK);
	glFrontFace(GL_CW);
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glUseProgram(shader->program_id);
}

void shader_unbind(void)
{
	glDisable(GL_CULL_FACE);
	glUseProgram(0);
}

void shader_free(Shader *shader)
{
	glDetachShader(shader->program_id, shader->vertex_id);
	glDetachShader(shader->program_id, shader->fragment_id);
	glDeleteShader(shader->vertex_id);
	glDeleteShader(shader->fragment_id);
	glDeleteProgram(shader->program_id);
	for (u32 i = 0; i < shader->attribute_names.len; i++)
		free(shader->attribute_names.strings[i]);
	list_free(&shader->attribute_names);
	list_free(&shader->attribute_locations);
	for (u32 i = 0; i < shader->uniform_names.len; i++)
		free(shader->uniform_names.strings[i]);
	list_free(&shader->uniform_names);
	list_free(&shader->uniform_locations);
	for (u32 i = 0; i < shader->uniform_block_names.len; i++)
		free(shader->uniform_block_names.strings[i]);
	list_free(&shader->uniform_block_names);
	list_free(&shader->uniform_block_indicies);
}

int shader_attribute_location(Shader *shader, const char *name)
{
	for (u32 i = 0; i < shader->attribute_names.len; i++) {
		const char *attribute = shader->attribute_names.strings[i];
		if (strcmp(attribute, name) == 0)
			return shader->attribute_locations.ints[i];
	}
	WARN("unknown attribute '%s'", name);
	return -1;
}

int shader_uniform_location(Shader *shader, const char *name)
{
	for (u32 i = 0; i < shader->uniform_names.len; i++) {
		const char *uniform = shader->uniform_names.strings[i];
		if (strcmp(uniform, name) == 0)
			return shader->uniform_locations.ints[i];
	}
	WARN("unknown uniform '%s'", name);
	return -1;
}

int shader_uniform_block_index(Shader *shader, const char *name)
{
	for (u32 i = 0; i < shader->uniform_block_names.len; i++) {
		const char *uniform_block = shader->uniform_block_names.strings[i];
		if (strcmp(uniform_block, name) == 0)
			return shader->uniform_block_indicies.ints[i];
	}
	WARN("unknown uniform block '%s'", name);
	return -1;
}

void shader_load_float(Shader *shader, const char *name, float value)
{
	int location = shader_uniform_location(shader, name);
	glUniform1f(location, value);
}

void shader_load_int(Shader *shader, const char *name, int value)
{
	int location = shader_uniform_location(shader, name);
	glUniform1i(location, value);
}

void shader_load_vec3(Shader *shader, const char *name, vec3 value)
{
	int location = shader_uniform_location(shader, name);
	glUniform3f(location, value[0], value[1], value[2]);
}

void shader_load_ivec3(Shader *shader, const char *name, ivec3 value)
{
	int location = shader_uniform_location(shader, name);
	glUniform3i(location, value[0], value[1], value[2]);
}

void shader_load_mat4(Shader *shader, const char *name, mat4 value)
{
	int location = shader_uniform_location(shader, name);
	glUniformMatrix4fv(location, 1, GL_FALSE, (float *)value);
}

void shader_load_ubo(Shader *shader, const char *name, u32 binding)
{
	int index = shader_uniform_block_index(shader, name);
	glUniformBlockBinding(shader->program_id, index, binding);
}