summaryrefslogtreecommitdiff
path: root/src/gl/gl.c
blob: 953cefbce3830e607d42adb6879d48e9121c6884 (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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
#include <stdlib.h>
#include <stdbool.h>
#include "gl/glad/glad.h"
#include <GLFW/glfw3.h>
#include <lua.h>
#include <honeysuckle.h>

/* needs to be here because glad uses macros to define glBufferData */
#ifdef HONEY_TEST_H
#undef glBufferData
#define glBufferData mock_glBufferData_
#endif


int gl_init(lua_State *L);
int glad_init(lua_State *L);
int gl_terminate(lua_State *L);
int gl_get_error(lua_State *L);

/* buffers */
int gl_create_buffer(lua_State *L);
int gl_bind_buffer(lua_State *L);
int gl_buffer_data(lua_State *L);

int gl_vertex_array_create(lua_State *L);
int gl_vertex_array_bind(lua_State *L);
int gl_vertex_attrib_pointer(lua_State *L);
int gl_vertex_array_enable_attrib(lua_State *L);

/* shaders */
int gl_create_shader(lua_State *L);
int gl_shader_set_source(lua_State *L);
int gl_shader_compile(lua_State *L);
int gl_shader_delete(lua_State *L);

int gl_program_create(lua_State *L);
int gl_program_attach_shader(lua_State *L);
int gl_program_link(lua_State *L);
int gl_program_use(lua_State *L);

/* drawing */
int gl_set_viewport(lua_State *L);
int gl_draw_arrays(lua_State *L);
int gl_set_clear_color(lua_State *L);
int gl_clear(lua_State *L);


void setup_gl(lua_State *L, int honey_index)
{
	int error_types = hs_create_table(L,
		hs_str_int("noError", GL_NO_ERROR),
		hs_str_int("invalidEnum", GL_INVALID_ENUM),
		hs_str_int("invalidValue", GL_INVALID_VALUE),
		hs_str_int("invalidOperation", GL_INVALID_OPERATION),
		hs_str_int("invalidFramebufferOperation", GL_INVALID_FRAMEBUFFER_OPERATION),
		hs_str_int("outOfMemory", GL_OUT_OF_MEMORY),
	);

	int buffer_binding_targets = hs_create_table(L,
		hs_str_int("arrayBuffer", GL_ARRAY_BUFFER),
	);

	int buffer_usage_patterns = hs_create_table(L,
		hs_str_int("streamDraw", GL_STREAM_DRAW),
		hs_str_int("staticDraw", GL_STATIC_DRAW),
		hs_str_int("dynamicDraw", GL_DYNAMIC_DRAW),
	);

	int shader_types = hs_create_table(L,
		hs_str_int("vertexShader", GL_VERTEX_SHADER),
		hs_str_int("fragmentShader", GL_FRAGMENT_SHADER),
	);

	int primitive_types = hs_create_table(L,
		hs_str_int("points", GL_POINTS),
		hs_str_int("lines", GL_LINES),
		hs_str_int("triangles", GL_TRIANGLES),
	);

	int buffer_masks = hs_create_table(L,
		hs_str_int("colorBuffer", GL_COLOR_BUFFER_BIT),
		hs_str_int("depthBuffer", GL_DEPTH_BUFFER_BIT),
		hs_str_int("stencilBuffer", GL_STENCIL_BUFFER_BIT),
	);

	hs_create_table(L,
		hs_str_cfunc("init", gl_init),
		hs_str_cfunc("initGlad", glad_init),
		hs_str_cfunc("terminate", gl_terminate),
		hs_str_cfunc("getError", gl_get_error),

		hs_str_tbl("errorType", error_types),

		/* buffer */
		hs_str_cfunc("createBuffer", gl_create_buffer),
		hs_str_cfunc("bindBuffer", gl_bind_buffer),
		hs_str_cfunc("bufferData", gl_buffer_data),

		hs_str_cfunc("createVertexArray", gl_vertex_array_create),
		hs_str_cfunc("bindVertexArray", gl_vertex_array_bind),
		hs_str_cfunc("vertexAttribPointer", gl_vertex_attrib_pointer),
		hs_str_cfunc("vertexArrayEnableAttrib", gl_vertex_array_enable_attrib),

		hs_str_tbl("bufferTarget", buffer_binding_targets),
		hs_str_tbl("bufferUsage", buffer_usage_patterns),

		/* shader */
		hs_str_cfunc("createShader", gl_create_shader),
		hs_str_cfunc("setShaderSource", gl_shader_set_source),
		hs_str_cfunc("compileShader", gl_shader_compile),
		hs_str_cfunc("deleteShader", gl_shader_delete),

		hs_str_cfunc("createProgram", gl_program_create),
		hs_str_cfunc("programAttachShader", gl_program_attach_shader),
		hs_str_cfunc("linkProgram", gl_program_link),
		hs_str_cfunc("useProgram", gl_program_use),

		hs_str_tbl("shaderType", shader_types),

		/* drawing */
		hs_str_cfunc("drawArrays", gl_draw_arrays),
		hs_str_cfunc("setClearColor", gl_set_clear_color),
		hs_str_cfunc("clear", gl_clear),
		hs_str_cfunc("setViewport", gl_set_viewport),

		hs_str_tbl("primitiveType", primitive_types),
		hs_str_tbl("bufferMask", buffer_masks),
	);
	lua_setfield(L, honey_index, "gl");
}


int gl_init(lua_State *L)
{
	if (!glfwInit()) {
		hs_throw_error(L, "failed to initialize GLFW");
	}
	return 0;
}


int glad_init(lua_State *L)
{
	if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
		hs_throw_error(L, "failed to initialize GLAD");
	}
}


int gl_terminate(lua_State *L)
{
	glfwTerminate();
	return 0;
}


int gl_get_error(lua_State *L)
{
	lua_pushinteger(L, glGetError());
	return 1;
}


int gl_create_buffer(lua_State *L)
{
	int buf;
	glGenBuffers(1, &buf);
	lua_pushinteger(L, buf);
	return 1;
}


int gl_bind_buffer(lua_State *L)
{
	lua_Integer target, buf;
	hs_parse_args(L, hs_int(target), hs_int(buf));
	glBindBuffer(target, buf);
	return 0;
}


int gl_buffer_data(lua_State *L)
{
	lua_Integer target, type, usage;
	int table;
	hs_parse_args(L, hs_int(target), hs_int(type), hs_tbl(table), hs_int(usage));

	if (type != GL_INT && type != GL_FLOAT) {
		hs_throw_error(L, "invalid type");
	}

	/* build raw buffer */
	size_t len = lua_objlen(L, table);
	void *buf;
	if (type == GL_FLOAT) {
		float *fbuf = malloc(len * sizeof(float));
		if (fbuf == NULL)
			hs_throw_error(L, "failed to allocate intermediary fbuffer");
		for (int i=0; i<len; i++) {
			lua_rawgeti(L, table, i+1);
			if (!lua_isnumber(L, -1)) {
				hs_throw_error(L, "all table items must be numbers (failed at index %d)", i);
			}
			fbuf[i] = lua_tonumber(L, -1);
			lua_pop(L, 1);
		}
		len = len * sizeof(float);
		buf = fbuf;
	}
	else {
		int *ibuf = malloc(len * sizeof(int));
		if (ibuf == NULL)
			hs_throw_error(L, "failed to allocate intermediary ibuffer");
		for (int i=0; i<len; i++) {
			lua_rawgeti(L, table, i+1);
			if (!lua_isnumber(L, -1)) {
				hs_throw_error(L, "all table items must be integers (failed at index %d)", i);
			}
			ibuf[i] = lua_tointeger(L, -1);
			lua_pop(L, 1);
		}
		len = len * sizeof(int);
		buf = ibuf;
	}

	/* call */
	glBufferData(target, len, buf, usage);
	free(buf);
	return 0;
}


int gl_create_shader(lua_State *L)
{
	lua_Integer type;
	hs_parse_args(L, hs_int(type));
	lua_Integer shader = glCreateShader(type);
	lua_pushinteger(L, shader);
	return 1;
}


int gl_shader_set_source(lua_State *L)
{
	lua_Integer shader;
	char *code;
	hs_parse_args(L, hs_int(shader), hs_str(code));
	glShaderSource(shader, 1, &code, NULL);
	return 0;
}


int gl_shader_compile(lua_State *L)
{
	lua_Integer shader;
	hs_parse_args(L, hs_int(shader));
	glCompileShader(shader);
	int success; char log[1024];
	glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
	if (!success) {
		glGetShaderInfoLog(shader, 1024, NULL, log);
		hs_throw_error(L, "shader compilation failed: %s", log);
	}
	return 0;
}


int gl_shader_delete(lua_State *L)
{
	lua_Integer shader;
	hs_parse_args(L, hs_int(shader));
	glDeleteShader(shader);
	return 0;
}


int gl_program_create(lua_State *L)
{
	lua_Integer program = glCreateProgram();
	lua_pushinteger(L, program);
	return 1;
}


int gl_program_attach_shader(lua_State *L)
{
	lua_Integer program, shader;
	hs_parse_args(L, hs_int(program), hs_int(shader)),
	glAttachShader(program, shader);
	return 0;
}


int gl_program_link(lua_State *L)
{
	lua_Integer program;
	hs_parse_args(L, hs_int(program));
	glLinkProgram(program);
	int success; char log[1024];
	glGetProgramiv(program, GL_LINK_STATUS, &success);
	if (!success) {
		glGetProgramInfoLog(program, 1024, NULL, log);
		hs_throw_error(L, "shader linking failed: %s", log);
	}
	return 0;
}


int gl_program_use(lua_State *L)
{
	lua_Integer program;
	hs_parse_args(L, hs_int(program));
	glUseProgram(program);
	return 0;
}


int gl_vertex_array_create(lua_State *L)
{
	int array;
	glGenVertexArrays(1, &array);
	lua_pushinteger(L, array);
	return 1;
}


int gl_vertex_array_bind(lua_State *L)
{
	lua_Integer array;
	hs_parse_args(L, hs_int(array));
	glBindVertexArray(array);
	return 0;
}


int gl_vertex_attrib_pointer(lua_State *L)
{
	lua_Integer index, size, stride, offset;
	bool normalized;
	hs_parse_args(L, hs_int(index), hs_int(size), hs_bool(normalized), hs_int(stride), hs_int(offset));
	glVertexAttribPointer(index, size, GL_FLOAT, 
	                      normalized, stride*sizeof(float), 
	                      (void*) (offset*sizeof(float)));
	return 0;
}


int gl_vertex_array_enable_attrib(lua_State *L)
{
	lua_Integer index;
	hs_parse_args(L, hs_int(index));
	glEnableVertexAttribArray(index);
	return 0;
}


int gl_set_clear_color(lua_State *L)
{
	lua_Number r, g, b, a;
	hs_parse_args(L, hs_num(r), hs_num(g), hs_num(b), hs_num(a));
	glClearColor(r, g, b, a);
	return 0;
}


int gl_clear(lua_State *L)
{
	lua_Integer mask;
	hs_parse_args(L, hs_int(mask));
	glClear(mask);
	return 0;
}


int gl_draw_arrays(lua_State *L)
{
	lua_Integer mode, first, count;
	hs_parse_args(L, hs_int(mode), hs_int(first), hs_int(count));
	glDrawArrays(mode, first, count);
	return 0;
}


int gl_set_viewport(lua_State *L)
{
	lua_Integer x, y, w, h;
	hs_parse_args(L, hs_int(x), hs_int(y), hs_int(w), hs_int(h));
	glViewport(x, y, w, h);
	return 0;
}