From 65ad3a58adcfe21690f379ab894a104b3852e3e9 Mon Sep 17 00:00:00 2001 From: sanine Date: Mon, 22 Aug 2022 10:31:56 -0500 Subject: refactor: move shader functions into separate file --- src/gl/shader.c | 124 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 124 insertions(+) create mode 100644 src/gl/shader.c (limited to 'src/gl/shader.c') diff --git a/src/gl/shader.c b/src/gl/shader.c new file mode 100644 index 0000000..96946fe --- /dev/null +++ b/src/gl/shader.c @@ -0,0 +1,124 @@ +#include "gl/glad/glad.h" +#include +#include +#include + +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); + + +void setup_shader(lua_State *L, int gl_index) +{ + int shader_types = hs_create_table(L, + hs_str_int("vertexShader", GL_VERTEX_SHADER), + hs_str_int("fragmentShader", GL_FRAGMENT_SHADER), + ); + + hs_create_table(L, + hs_str_cfunc("create", gl_create_shader), + hs_str_cfunc("setSource", gl_shader_set_source), + hs_str_cfunc("compile", gl_shader_compile), + hs_str_cfunc("delete", gl_shader_delete), + + hs_str_cfunc("createProgram", gl_program_create), + hs_str_cfunc("attachShader", gl_program_attach_shader), + hs_str_cfunc("link", gl_program_link), + hs_str_cfunc("use", gl_program_use), + + hs_str_tbl("type", shader_types), + ); + + lua_setfield(L, gl_index, "shader"); +} + + +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; +} -- cgit v1.2.1 From b2c0dd12d2a39e74f8e6d63bffec073ef0871ea7 Mon Sep 17 00:00:00 2001 From: sanine Date: Mon, 22 Aug 2022 12:39:51 -0500 Subject: add uniforms --- src/gl/shader.c | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'src/gl/shader.c') diff --git a/src/gl/shader.c b/src/gl/shader.c index 96946fe..fb64bc0 100644 --- a/src/gl/shader.c +++ b/src/gl/shader.c @@ -13,6 +13,9 @@ int gl_program_attach_shader(lua_State *L); int gl_program_link(lua_State *L); int gl_program_use(lua_State *L); +int gl_uniform_get_location(lua_State *L); +int gl_uniform_4f(lua_State *L); + void setup_shader(lua_State *L, int gl_index) { @@ -32,6 +35,9 @@ void setup_shader(lua_State *L, int gl_index) hs_str_cfunc("link", gl_program_link), hs_str_cfunc("use", gl_program_use), + hs_str_cfunc("getUniformLocation", gl_uniform_get_location), + hs_str_cfunc("uniform4f", gl_uniform_4f), + hs_str_tbl("type", shader_types), ); @@ -122,3 +128,24 @@ int gl_program_use(lua_State *L) glUseProgram(program); return 0; } + + +int gl_uniform_get_location(lua_State *L) +{ + lua_Integer program; + char *name; + hs_parse_args(L, hs_int(program), hs_str(name)); + int location = glGetUniformLocation(program, (const GLchar*)name); + lua_pushinteger(L, location); + return 1; +} + + +int gl_uniform_4f(lua_State *L) +{ + lua_Integer location; + lua_Number v0, v1, v2, v3; + hs_parse_args(L, hs_int(location), hs_num(v0), hs_num(v1), hs_num(v2), hs_num(v3)); + glUniform4f(location, v0, v1, v2, v3); + return 0; +} -- cgit v1.2.1 From 1f47b685f35455afcc7441389cdc60018f66d159 Mon Sep 17 00:00:00 2001 From: sanine Date: Mon, 22 Aug 2022 15:55:44 -0500 Subject: add textures --- src/gl/shader.c | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'src/gl/shader.c') diff --git a/src/gl/shader.c b/src/gl/shader.c index fb64bc0..c3b409c 100644 --- a/src/gl/shader.c +++ b/src/gl/shader.c @@ -14,6 +14,7 @@ int gl_program_link(lua_State *L); int gl_program_use(lua_State *L); int gl_uniform_get_location(lua_State *L); +int gl_uniform_1i(lua_State *L); int gl_uniform_4f(lua_State *L); @@ -36,6 +37,7 @@ void setup_shader(lua_State *L, int gl_index) hs_str_cfunc("use", gl_program_use), hs_str_cfunc("getUniformLocation", gl_uniform_get_location), + hs_str_cfunc("uniform1i", gl_uniform_1i), hs_str_cfunc("uniform4f", gl_uniform_4f), hs_str_tbl("type", shader_types), @@ -141,6 +143,15 @@ int gl_uniform_get_location(lua_State *L) } +int gl_uniform_1i(lua_State *L) +{ + lua_Integer location, v0; + hs_parse_args(L, hs_int(location), hs_int(v0)); + glUniform1i(location, v0); + return 0; +} + + int gl_uniform_4f(lua_State *L) { lua_Integer location; -- cgit v1.2.1 From c52e1b30391d730cd6d20f65ec75d61884355c5c Mon Sep 17 00:00:00 2001 From: sanine Date: Mon, 22 Aug 2022 23:58:00 -0500 Subject: refactor: rename opengl functions to match their C counterparts --- src/gl/shader.c | 45 +++++++++++++++++++++++---------------------- 1 file changed, 23 insertions(+), 22 deletions(-) (limited to 'src/gl/shader.c') diff --git a/src/gl/shader.c b/src/gl/shader.c index c3b409c..b4d03e6 100644 --- a/src/gl/shader.c +++ b/src/gl/shader.c @@ -2,6 +2,7 @@ #include #include #include +#include "util/util.h" int gl_create_shader(lua_State *L); int gl_shader_set_source(lua_State *L); @@ -20,30 +21,30 @@ int gl_uniform_4f(lua_State *L); void setup_shader(lua_State *L, int gl_index) { - int shader_types = hs_create_table(L, - hs_str_int("vertexShader", GL_VERTEX_SHADER), - hs_str_int("fragmentShader", GL_FRAGMENT_SHADER), + int tbl = hs_create_table(L, + /* functions */ + hs_str_cfunc("CreateShader", gl_create_shader), + hs_str_cfunc("ShaderSource", 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("AttachShader", gl_program_attach_shader), + hs_str_cfunc("LinkProgram", gl_program_link), + hs_str_cfunc("UseProgram", gl_program_use), + + hs_str_cfunc("GetUniformLocation", gl_uniform_get_location), + hs_str_cfunc("Uniform1i", gl_uniform_1i), + hs_str_cfunc("Uniform4f", gl_uniform_4f), + + /******** enums ********/ + /* shader types */ + hs_str_int("VERTEX_SHADER", GL_VERTEX_SHADER), + hs_str_int("FRAGMENT_SHADER", GL_FRAGMENT_SHADER), ); - hs_create_table(L, - hs_str_cfunc("create", gl_create_shader), - hs_str_cfunc("setSource", gl_shader_set_source), - hs_str_cfunc("compile", gl_shader_compile), - hs_str_cfunc("delete", gl_shader_delete), - - hs_str_cfunc("createProgram", gl_program_create), - hs_str_cfunc("attachShader", gl_program_attach_shader), - hs_str_cfunc("link", gl_program_link), - hs_str_cfunc("use", gl_program_use), - - hs_str_cfunc("getUniformLocation", gl_uniform_get_location), - hs_str_cfunc("uniform1i", gl_uniform_1i), - hs_str_cfunc("uniform4f", gl_uniform_4f), - - hs_str_tbl("type", shader_types), - ); - - lua_setfield(L, gl_index, "shader"); + append_table(L, gl_index, tbl); + lua_pop(L, 1); } -- cgit v1.2.1 From ae4e17fc743ca0344af818ab767db7311ea7829c Mon Sep 17 00:00:00 2001 From: sanine Date: Tue, 23 Aug 2022 13:28:01 -0500 Subject: add basic transforms --- src/gl/shader.c | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) (limited to 'src/gl/shader.c') diff --git a/src/gl/shader.c b/src/gl/shader.c index b4d03e6..3732aff 100644 --- a/src/gl/shader.c +++ b/src/gl/shader.c @@ -18,6 +18,8 @@ int gl_uniform_get_location(lua_State *L); int gl_uniform_1i(lua_State *L); int gl_uniform_4f(lua_State *L); +int gl_uniform_matrix_4fv(lua_State *L); + void setup_shader(lua_State *L, int gl_index) { @@ -37,6 +39,8 @@ void setup_shader(lua_State *L, int gl_index) hs_str_cfunc("Uniform1i", gl_uniform_1i), hs_str_cfunc("Uniform4f", gl_uniform_4f), + hs_str_cfunc("UniformMatrix4fv", gl_uniform_matrix_4fv), + /******** enums ********/ /* shader types */ hs_str_int("VERTEX_SHADER", GL_VERTEX_SHADER), @@ -63,7 +67,7 @@ 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); + glShaderSource(shader, 1, (const GLchar * const*)&code, NULL); return 0; } @@ -161,3 +165,16 @@ int gl_uniform_4f(lua_State *L) glUniform4f(location, v0, v1, v2, v3); return 0; } + + +int gl_uniform_matrix_4fv(lua_State *L) +{ + lua_Integer location; + bool transpose; + void *ptr; + hs_parse_args(L, hs_int(location), hs_bool(transpose), hs_user(ptr)); + + float *value = ptr; + glUniformMatrix4fv(location, 1, transpose, value); + return 0; +} -- cgit v1.2.1