summaryrefslogtreecommitdiff
path: root/gl.c
blob: 4b41f428aa303f065513f84b5fc9358f2c558fc9 (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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
// various functions for dealing with OpenGL.
// also houses all of the basic rendering functions ted uses.

#include "ted.h"
#include "lib/glcorearb.h"

float gl_window_width, gl_window_height;
int gl_version_major, gl_version_minor;


#if DEBUG
unsigned char *stbi_load(const char *filename, int *x, int *y, int *comp, int req_comp);
#else
#define STB_IMAGE_STATIC
no_warn_start
#include "stb_image.c"
no_warn_end
#endif


#define gl_define_proc(upper, lower) PFNGL##upper##PROC gl##lower;
gl_for_each_proc(gl_define_proc)
#undef gl_define_proc

GlRcSAB *gl_rc_sab_new(GLuint shader, GLuint array, GLuint buffer) {
	GlRcSAB *s = calloc(1, sizeof *s);
	s->ref_count = 1;
	s->shader = shader;
	s->array = array;
	s->buffer = buffer;
	return s;
}

void gl_rc_sab_incref(GlRcSAB *s) {
	if (!s) return;
	++s->ref_count;
}

void gl_rc_sab_decref(GlRcSAB **ps) {
	GlRcSAB *s = *ps;
	if (!s) return;
	if (--s->ref_count == 0) {
		debug_println("Delete program %u", s->shader);
		glDeleteProgram(s->shader);
		glDeleteBuffers(1, &s->buffer);
		glDeleteVertexArrays(1, &s->array);
		free(s);
	}
	*ps = NULL;
}

GlRcTexture *gl_rc_texture_new(GLuint texture) {
	GlRcTexture *t = calloc(1, sizeof *t);
	t->ref_count = 1;
	t->texture = texture;
	return t;
}

void gl_rc_texture_incref(GlRcTexture *t) {
	if (!t) return;
	++t->ref_count;
}

void gl_rc_texture_decref(GlRcTexture **pt) {
	GlRcTexture *t = *pt;
	if (!t) return;
	if (--t->ref_count == 0) {
		glDeleteTextures(1, &t->texture);
		free(t);
	}
	*pt = NULL;
}

void gl_get_procs(void) {
	#define gl_get_proc(upper, lower) gl##lower = (PFNGL##upper##PROC)SDL_GL_GetProcAddress("gl" #lower);
#if __GNUC__ && !__clang__
	#pragma GCC diagnostic push
	#pragma GCC diagnostic ignored "-Wpedantic"
#endif
	gl_for_each_proc(gl_get_proc)
#if __GNUC__ && !__clang__
	#pragma GCC diagnostic pop
#endif
	#undef gl_get_proc
}

static int glsl_version(void) {
	int v = gl_version_major * 100 + gl_version_minor * 10;
	switch (v) {
	case 200: return 110;
	case 210: return 120;
	case 300: return 130;
	case 310: return 140;
	}
	// not going any later than GLSL 150 since
	// then we don't have gl_FragColor
	return 150;
}

// compile a GLSL shader
GLuint gl_compile_shader(char error_buf[256], const char *code, GLenum shader_type) {
	GLuint shader = glCreateShader(shader_type);
	char header[128];
	int glsl = glsl_version();
	strbuf_printf(header, "#version %u\n\
#define IN %s\n\
#define OUT %s\n\
#line 1\n", glsl, glsl >= 130 ? "in" : "varying", glsl >= 130 ? "out" : "varying");
	const char *sources[2] = {
		header,
		code
	};
	glShaderSource(shader, 2, sources, NULL);
	glCompileShader(shader);
	GLint status = 0;
	glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
	if (status == GL_FALSE) {
		char log[1024] = {0};
		glGetShaderInfoLog(shader, sizeof log - 1, NULL, log);
		if (error_buf) {
			str_printf(error_buf, 256, "Error compiling shader: %s", log);
		} else {
			debug_println("Error compiling shader: %s", log);
		}
		return 0;
	}
	return shader;
}

// link together GL shaders
GLuint gl_link_program(char error_buf[256], GLuint *shaders, size_t count) {
	GLuint program = glCreateProgram();
	if (program) {
		for (size_t i = 0; i < count; ++i) {
			if (!shaders[i]) {
				glDeleteProgram(program);
				return 0;
			}
			glAttachShader(program, shaders[i]);
		}
		glLinkProgram(program);
		GLint status = 0;
		glGetProgramiv(program, GL_LINK_STATUS, &status);
		if (status == GL_FALSE) {
			char log[1024] = {0};
			glGetProgramInfoLog(program, sizeof log - 1, NULL, log);
			if (error_buf) {
				str_printf(error_buf, 256, "Error linking shaders: %s", log);
			} else {
				debug_println("Error linking shaders: %s", log);
			}
			glDeleteProgram(program);
			return 0;
		}
	}
	return program;
}

GLuint gl_compile_and_link_shaders(char error_buf[256], const char *vshader_code, const char *fshader_code) {
	GLuint shaders[2];
	shaders[0] = gl_compile_shader(error_buf, vshader_code, GL_VERTEX_SHADER);
	shaders[1] = gl_compile_shader(error_buf, fshader_code, GL_FRAGMENT_SHADER);
	GLuint program = gl_link_program(error_buf, shaders, 2);
	if (shaders[0]) glDeleteShader(shaders[0]);
	if (shaders[1]) glDeleteShader(shaders[1]);
	if (program) {
		debug_print("Successfully linked program %u.\n", program);
	}
	return program;
}

GLuint gl_attrib_location(GLuint program, const char *attrib) {
	GLint loc = glGetAttribLocation(program, attrib);
	if (loc == -1) {
		debug_print("Couldn't find vertex attribute %s.\n", attrib);
		return 0;
	}
	return (GLuint)loc;
}

GLint gl_uniform_location(GLuint program, const char *uniform) {
	GLint loc = glGetUniformLocation(program, uniform);
	if (loc == -1) {
		debug_print("Couldn't find uniform: %s.\n", uniform);
		return -1;
	}
	return loc;
}

typedef struct {
	vec2 pos;
	vec4 color;
} GLSimpleVertex;
typedef struct {
	GLSimpleVertex vert1, vert2, vert3;
} GLSimpleTriangle;

static GLSimpleTriangle *gl_geometry_triangles;
static GLuint gl_geometry_program;
static GLuint gl_geometry_v_pos;
static GLuint gl_geometry_v_color;
static GLint gl_geometry_u_window_size;
static GLuint gl_geometry_vbo, gl_geometry_vao;

void gl_geometry_init(void) {
	const char *vshader_code = "attribute vec2 v_pos;\n\
	attribute vec4 v_color;\n\
	uniform vec2 u_window_size;\n\
	OUT vec4 color;\n\
	void main() {\n\
		vec2 p = v_pos * (2.0 / u_window_size);\n\
		gl_Position = vec4(p.x - 1.0, 1.0 - p.y, 0.0, 1.0);\n\
		color = v_color;\n\
	}\n\
	";
	const char *fshader_code = "IN vec4 color;\n\
	void main() {\n\
		gl_FragColor = color;\n\
	}\n\
	";

	gl_geometry_program = gl_compile_and_link_shaders(NULL, vshader_code, fshader_code);
	gl_geometry_v_pos = gl_attrib_location(gl_geometry_program, "v_pos");
	gl_geometry_v_color = gl_attrib_location(gl_geometry_program, "v_color");
	gl_geometry_u_window_size = gl_uniform_location(gl_geometry_program, "u_window_size");

	glGenBuffers(1, &gl_geometry_vbo);
	if (gl_version_major >= 3)
		glGenVertexArrays(1, &gl_geometry_vao);
}

void gl_geometry_rect(Rect r, u32 color_rgba) {
	vec4 color = rgba_u32_to_vec4(color_rgba);

	vec2 p1 = r.pos;
	vec2 p2 = vec2_add(r.pos, Vec2(0, r.size.y));
	vec2 p3 = vec2_add(r.pos, Vec2(r.size.x, r.size.y));
	vec2 p4 = vec2_add(r.pos, Vec2(r.size.x, 0));

	GLSimpleTriangle triangle = {
		{p1, color},
		{p2, color},
		{p3, color}
	};
	arr_add(gl_geometry_triangles, triangle);
	triangle.vert1.pos = p3;
	triangle.vert2.pos = p4;
	triangle.vert3.pos = p1;
	arr_add(gl_geometry_triangles, triangle);
}

void gl_geometry_rect_border(Rect r, float border_thickness, u32 color) {
	float x1 = r.pos.x, y1 = r.pos.y, x2 = x1 + r.size.x, y2 = y1 + r.size.y;
	
	// make sure rectangle isn't too small
	x2 = maxf(x2, x1 + border_thickness);
	y2 = maxf(y2, y1 + border_thickness);

	gl_geometry_rect(rect4(x1+border_thickness, y1, x2, y1+border_thickness), color);
	gl_geometry_rect(rect4(x1, y2-border_thickness, x2-border_thickness, y2), color);
	gl_geometry_rect(rect4(x1, y1, x1+border_thickness, y2), color);
	gl_geometry_rect(rect4(x2-border_thickness, y1+border_thickness, x2, y2), color);
}

void gl_geometry_draw(void) {
	size_t ntriangles = arr_len(gl_geometry_triangles);
	
	if (ntriangles == 0) return;

	if (gl_version_major >= 3)
		glBindVertexArray(gl_geometry_vao);

	glBindBuffer(GL_ARRAY_BUFFER, gl_geometry_vbo);
	glBufferData(GL_ARRAY_BUFFER, (GLsizeiptr)(ntriangles * sizeof(GLSimpleTriangle)), gl_geometry_triangles, GL_STREAM_DRAW);
	glVertexAttribPointer(gl_geometry_v_pos,   2, GL_FLOAT, 0, sizeof(GLSimpleVertex), (void *)offsetof(GLSimpleVertex, pos));
	glEnableVertexAttribArray(gl_geometry_v_pos);
	glVertexAttribPointer(gl_geometry_v_color, 4, GL_FLOAT, 0, sizeof(GLSimpleVertex), (void *)offsetof(GLSimpleVertex, color));
	glEnableVertexAttribArray(gl_geometry_v_color);

	glUseProgram(gl_geometry_program);
	glUniform2f(gl_geometry_u_window_size, gl_window_width, gl_window_height);
	glDrawArrays(GL_TRIANGLES, 0, (GLsizei)(3 * ntriangles));
	
	arr_clear(gl_geometry_triangles);
}

GLuint gl_load_texture_from_image(const char *path) {
	GLuint texture = 0;
	int w=0, h=0, n=0;
	unsigned char *data = stbi_load(path, &w, &h, &n, 4);
	if (data && w > 0 && h > 0) {
		size_t bytes_per_row = (size_t)w * 4;
		unsigned char *tempbuf = calloc(1, bytes_per_row);
		// flip image vertically
		for (int y = 0; y < h/2; ++y) {
			int y2 = h-1-y;
			unsigned char *row1 = &data[y*w*4];
			unsigned char *row2 = &data[y2*w*4];
			memcpy(tempbuf, row1, bytes_per_row);
			memcpy(row1, row2, bytes_per_row);
			memcpy(row2, tempbuf, bytes_per_row);
		}
		free(tempbuf);
		
		glGenTextures(1, &texture);
		glBindTexture(GL_TEXTURE_2D, texture);
		glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
		free(data);
	}
	return texture;
}