diff options
author | Tomasz Kramkowski <tk@the-tk.com> | 2018-03-30 15:35:05 +0100 |
---|---|---|
committer | Tomasz Kramkowski <tk@the-tk.com> | 2018-03-30 15:35:05 +0100 |
commit | 83b3fe86b1c13f40a6be4580b4079980030a54a7 (patch) | |
tree | d3ea228b9ac42d7ed34ba8699167bf8ff29931e1 | |
parent | 93c76bf190d843ef77c71682570f28e9fb871a08 (diff) | |
download | faqe-83b3fe86b1c13f40a6be4580b4079980030a54a7.tar.gz faqe-83b3fe86b1c13f40a6be4580b4079980030a54a7.tar.xz faqe-83b3fe86b1c13f40a6be4580b4079980030a54a7.zip |
Simplify gl loading by removing m4 and merging gldefs.h
m4 has been removed, loadgl.c.in is now gl.c, gldefs.h and loadgl.h.in
have now been merged into gl.h. loadgl.m4 has been transformed into
glfunc.h which is being used in the way of a bie index file to
generate information at 3 places.
lgl_load is now gl_load and doesn't return anything, the jongjmp.h
method of error handling was pre-emptive and for now this much simpler
system will suffice. This means that lgl_strerror is no longer neede.
-rw-r--r-- | Makefile | 6 | ||||
-rw-r--r-- | gl.c (renamed from loadgl.c.in) | 42 | ||||
-rw-r--r-- | gl.h (renamed from gldefs.h) | 18 | ||||
-rw-r--r-- | glfunc.h | 44 | ||||
-rw-r--r-- | glprog.c | 2 | ||||
-rw-r--r-- | glprog.h | 2 | ||||
-rw-r--r-- | gltest.c | 8 | ||||
-rw-r--r-- | loadgl.h.in | 28 | ||||
-rw-r--r-- | loadgl.m4 | 91 |
9 files changed, 76 insertions, 165 deletions
@@ -14,7 +14,7 @@ CFLAGS += $(shell $(PKG_CONFIG) --cflags $(LIBS)) -std=c11 -MMD -MP LDFLAGS += -Wl,--as-needed LDLIBS += $(shell $(PKG_CONFIG) --libs $(LIBS)) -lm -OBJ := gltest.o loadgl.o eprintf.o glprog.o +OBJ := gltest.o gl.o eprintf.o glprog.o all: $(PROG) @@ -23,10 +23,6 @@ include $(EPRINTF_PATH)/module.mk $(PROG): $(OBJ) gltest.o: assets.h -loadgl.o gltest.o glprog.o: loadgl.h -loadgl.c loadgl.h: loadgl.%: loadgl.m4 loadgl.%.in - m4 $^ >$@ -CLEAN += loadgl.c loadgl.h deplinks: $(EPRINTF_FILES) @@ -2,49 +2,31 @@ * Copyright (C) 2018 Tomasz Kramkowski <tk@the-tk.com> * SPDX-License-Identifier: MIT */ -#include <setjmp.h> -#include "loadgl.h" +#include "eprintf.h" +#include "gl.h" -#define LGL_FUNC(name, glname) \ - name##_func *name; \ - static const char *name##_gln = #glname; +#define GL_FUNC(glname, rtype, name, ...) name##_func *name; +#include "glfunc.h" +#undef GL_FUNC -FUNCS()dnl - -static void *load_func(const char *name, lgl_loadfunc *load, jmp_buf env) +static void *load_func(const char *name, gl_loadfunc *load) { void *ret; ret = load(name); if (ret == NULL) - longjmp(env, LGL_MISSING); + eprintf("Could not load OpenGL function: %s\n", name); return ret; } -enum lgl_status lgl_load(lgl_loadfunc *load) +void gl_load(gl_loadfunc *load) { - enum lgl_status status; - jmp_buf env; - - status = setjmp(env); - if (status != 0) - return status; - -#define LGL_LOAD(name) name = (name##_func *)load_func(name##_gln, load, env); - LOADS()dnl - - return LGL_OK; -} - -const char *lgl_strerror(enum lgl_status status) -{ - switch (status) { - case LGL_OK: return "Success"; - case LGL_MISSING: return "Missing function"; - } - return "Unknown"; +#define GL_FUNC(glname, rtype, name, ...) \ + name = (name##_func *)load_func(#glname, load); +#include "glfunc.h" +#undef GL_FUNC } const char *gl_strerror(GLenum error) @@ -2,8 +2,8 @@ * Copyright (C) 2018 Tomasz Kramkowski <tk@the-tk.com> * SPDX-License-Identifier: MIT */ -#ifndef GLDEFS_H -#define GLDEFS_H +#ifndef GL_H +#define GL_H #include <stdint.h> #include <stddef.h> @@ -60,4 +60,16 @@ enum { GL_LINK_STATUS = 0x8b82, }; -#endif // GLDEFS_H +typedef void *gl_loadfunc(const char *name); +void gl_load(gl_loadfunc *load); +const char *gl_strerror(GLenum error); + +#define _GL_FUNC(rtype, name, type, ...) \ + typedef rtype type(__VA_ARGS__); \ + extern type *name; +#define GL_FUNC(glname, rtype, name, ...) _GL_FUNC(rtype, name, name##_func, __VA_ARGS__) +#include "glfunc.h" +#undef GL_FUNC +#undef _GL_FUNC + +#endif // GL_H diff --git a/glfunc.h b/glfunc.h new file mode 100644 index 0000000..22a0021 --- /dev/null +++ b/glfunc.h @@ -0,0 +1,44 @@ +// Copyright (C) Tomasz Kramkowski <tk@the-tk.com> +// SPDX-License-Identifier: MIT + +GL_FUNC(glGetError, GLenum, gl_error, void) +GL_FUNC(glViewport, void, gl_viewport, GLint x, GLint y, GLsizei width, GLsizei height) +GL_FUNC(glClearColor, void, gl_clearcolor, GLfloat red, GLfloat green, GLfloat blue, GLfloat alpha) +GL_FUNC(glClear, void, gl_clear, GLbitfield mask) +GL_FUNC(glGenBuffers, void, gl_buf_gen, GLsizei n, GLuint *buffers) +GL_FUNC(glBindBuffer, void, gl_buf_bind, GLenum target, GLuint buffer) +GL_FUNC(glBufferData, void, gl_buf_data, GLenum target, GLsizeiptr size, const void *data, GLenum usage) +GL_FUNC(glCreateShader, GLuint, gl_shdr_create, GLenum type) +GL_FUNC(glDeleteShader, void, gl_shdr_del, GLuint shader) +GL_FUNC(glShaderSource, void, gl_shdr_source, GLuint shader, GLsizei count, const char * const *string, const GLint *length) +GL_FUNC(glCompileShader, void, gl_shdr_compile, GLuint shader) +GL_FUNC(glGetShaderiv, void, gl_shdr_param, GLuint shader, GLenum pname, GLint *params) +GL_FUNC(glGetShaderInfoLog, void, gl_shdr_infolog, GLuint shader, GLsizei size, GLsizei *len, char *data) +GL_FUNC(glCreateProgram, GLuint, gl_prog_create, void) +GL_FUNC(glDeleteProgram, void, gl_prog_del, GLuint program) +GL_FUNC(glAttachShader, void, gl_prog_attachshdr, GLuint program, GLuint shader) +GL_FUNC(glDetachShader, void, gl_prog_detachshdr, GLuint program, GLuint shader) +GL_FUNC(glGetAttachedShaders, void, gl_prog_getshdrs, GLuint program, GLsizei max, GLsizei *count, GLuint *shaders) +GL_FUNC(glLinkProgram, void, gl_prog_link, GLuint program) +GL_FUNC(glUseProgram, void, gl_prog_use, GLuint program) +GL_FUNC(glGetProgramiv, void, gl_prog_param, GLuint prog, GLenum pname, GLint *params) +GL_FUNC(glGetProgramInfoLog, void, gl_prog_infolog, GLuint prog, GLsizei size, GLsizei *len, char *data) +GL_FUNC(glVertexAttribPointer, void, gl_va_define, GLuint index, GLint size, GLenum type, GLboolean normalized, GLsizei stride, const void *data) +GL_FUNC(glEnableVertexAttribArray, void, gl_va_enable, GLuint index) +GL_FUNC(glDisableVertexAttribArray, void, gl_va_disable, GLuint index) +GL_FUNC(glGenVertexArrays, void, gl_va_gen, GLsizei n, GLuint *arrays) +GL_FUNC(glBindVertexArray, void, gl_va_bind, GLuint va) +GL_FUNC(glDrawArrays, void, gl_draw_arrays, GLenum mode, GLint first, GLsizei count) +GL_FUNC(glDrawElements, void, gl_draw_elems, GLenum mode, GLsizei count, GLenum type, const void *indices) +GL_FUNC(glPolygonMode, void, gl_poly_mode, GLenum face, GLenum mode) +GL_FUNC(glGetUniformLocation, GLint, gl_uni_loc, GLuint program, const char *name); +GL_FUNC(glGetUniformfv, void, gl_uni_getf, GLuint program, GLint location, GLfloat *params); +GL_FUNC(glGetUniformiv, void, gl_uni_geti, GLuint program, GLint location, GLint *params); +GL_FUNC(glUniform1i, void, gl_uni_set1i, GLint location, GLint v0) +GL_FUNC(glUniform1f, void, gl_uni_set1f, GLint location, GLfloat v0) +GL_FUNC(glUniform3f, void, gl_uni_set3f, GLint location, GLfloat v0, GLfloat v1, GLfloat v2) +GL_FUNC(glUniform4f, void, gl_uni_set4f, GLint location, GLfloat v0, GLfloat v1, GLfloat v2, GLfloat v3) +GL_FUNC(glUniform3fv, void, gl_uni_set3fv, GLint location, GLsizei count, const GLfloat *value) +GL_FUNC(glUniform4fv, void, gl_uni_set4fv, GLint location, GLsizei count, const GLfloat *value) +GL_FUNC(glUniformMatrix3fv, void, gl_uni_setm3fv, GLint location, GLsizei count, GLboolean transpose, const GLfloat *value) +GL_FUNC(glUniformMatrix4fv, void, gl_uni_setm4fv, GLint location, GLsizei count, GLboolean transpose, const GLfloat *value) @@ -5,8 +5,8 @@ #include <assert.h> #include "eprintf.h" +#include "gl.h" #include "glprog.h" -#include "loadgl.h" enum { LOGSIZE = 1024, @@ -5,7 +5,7 @@ #ifndef GLPROG_H #define GLPROG_H -#include "gldefs.h" +#include "gl.h" struct unidat { const char *name; @@ -7,10 +7,9 @@ #include "assets.h" #include "eprintf.h" -#include "gldefs.h" +#include "gl.h" #include "glprog.h" #include "linmath.h" -#include "loadgl.h" #include "nelem.h" enum { @@ -33,7 +32,6 @@ void take_input(GLFWwindow *win) int main(int argc, char **argv) { GLFWwindow *win; - int ret; GLuint ebo, vbo, vao, prog; GLint ucolor; struct vertex { @@ -64,9 +62,7 @@ int main(int argc, char **argv) eprintf("Failed to create GLFW window"); glfwMakeContextCurrent(win); - ret = lgl_load((lgl_loadfunc *)glfwGetProcAddress); - if (ret != 0) - eprintf("Failed to load GL: %s", lgl_strerror(ret)); + gl_load((gl_loadfunc *)glfwGetProcAddress); glfwSetFramebufferSizeCallback(win, &fb_size_cb); fb_size_cb(win, WIDTH, HEIGHT); diff --git a/loadgl.h.in b/loadgl.h.in deleted file mode 100644 index e741d5e..0000000 --- a/loadgl.h.in +++ /dev/null @@ -1,28 +0,0 @@ -/* - * Copyright (C) 2018 Tomasz Kramkowski <tk@the-tk.com> - * SPDX-License-Identifier: MIT - */ -#ifndef LOADGL_H -#define LOADGL_H - -#include "gldefs.h" - -typedef void *lgl_loadfunc(const char *name); - -enum lgl_status { - LGL_OK, - LGL_MISSING, -}; - -enum lgl_status lgl_load(lgl_loadfunc *load); -const char *lgl_strerror(enum lgl_status status); -const char *gl_strerror(GLenum error); - -#define _LGL_INTERFACE(rett, name, func, ...) \ - typedef rett func(__VA_ARGS__); \ - extern func *name; -#define LGL_INTERFACE(rett, name, ...) _LGL_INTERFACE(rett, name, name##_func, __VA_ARGS__) - -INTERFACES()dnl - -#endif // LOADGL_H diff --git a/loadgl.m4 b/loadgl.m4 deleted file mode 100644 index 7a7bde4..0000000 --- a/loadgl.m4 +++ /dev/null @@ -1,91 +0,0 @@ -divert(-1) -# Copyright (C) Tomasz Kramkowski <tk@the-tk.com> -# SPDX-License - -# Macros - -# LOAD(glname, rtype, name, args) -define(`LOAD', -`divert(1)LGL_INTERFACE(shift($@)); -divert(2)LGL_FUNC($3, $1); -divert(3)LGL_LOAD($3); -divert(-1)') -m4wrap(`m4exit') - -# 1 - LGL_INTERFACE(rtype, name, args); -define(`INTERFACES', `undivert(1)') -# 2 - LGL_FUNC(name, glname); -define(`FUNCS', `undivert(2)') -# 3 - LGL_LOAD(name); -define(`LOADS', `undivert(3)') - -define(`FOR', `ifelse($#, 0, ``$0'', `ifelse(eval($2 <= $3), 1, - `pushdef(`$1', $2)$4`'popdef(`$1')$0(`$1', incr($2), $3, `$4')')')') - -# PAT(pattern, subst) -define(`PAT', `pushdef(`P', $2)$1`'popdef(`P')') -# PARAMS(start, n, pattern) -define(`PARAMS', `ifelse(eval(`$1 < ($2 - 1)'), 1, - `PAT(`$3', $1), $0(incr($1), $2, `$3')', `PAT(`$3', $1)')') -define(`TYPE', `PARAMS(0, $1, `$2 v`'P')') -# $1 $2 $3 $4 $5 $6 $7 -# FLOAT(vec, suf, vectype, glname, rtype, name, args...) -define(`LOADV', `LOAD($4$1$2, $5, $6$1$2, shift(shift(shift(shift(shift(shift($@)))))), TYPE($1, $3))') -define(`LOADP', `LOAD($4$1$2, $5, $6$1$2, shift(shift(shift(shift(shift(shift($@)))))), $3 *value)') -define(`MULTI', `FOR(`n', 1, $1, `$2')') -define(`FOREACH', `ifelse(eval($# > 2), 1, - `pushdef(`$1', `$3')$2`'popdef(`$1')dnl -`'ifelse(eval($# > 3), 1, `$0(`$1', `$2', shift(shift(shift($@))))')')') - -# Functions - -LOAD(glGetError, GLenum, gl_error, void) -LOAD(glViewport, void, gl_viewport, GLint x, GLint y, GLsizei width, GLsizei height) -LOAD(glClearColor, void, gl_clearcolor, GLfloat red, GLfloat green, GLfloat blue, GLfloat alpha) -LOAD(glClear, void, gl_clear, GLbitfield mask) -LOAD(glGenBuffers, void, gl_buf_gen, GLsizei n, GLuint *buffers) -LOAD(glBindBuffer, void, gl_buf_bind, GLenum target, GLuint buffer) -LOAD(glBufferData, void, gl_buf_data, GLenum target, GLsizeiptr size, const void *data, GLenum usage) -LOAD(glCreateShader, GLuint, gl_shdr_create, GLenum type) -LOAD(glDeleteShader, void, gl_shdr_del, GLuint shader) -LOAD(glShaderSource, void, gl_shdr_source, GLuint shader, GLsizei count, const char * const *string, const GLint *length) -LOAD(glCompileShader, void, gl_shdr_compile, GLuint shader) -LOAD(glGetShaderiv, void, gl_shdr_param, GLuint shader, GLenum pname, GLint *params) -LOAD(glGetShaderInfoLog, void, gl_shdr_infolog, GLuint shader, GLsizei size, GLsizei *len, char *data) -LOAD(glCreateProgram, GLuint, gl_prog_create, void) -LOAD(glDeleteProgram, void, gl_prog_del, GLuint program) -LOAD(glAttachShader, void, gl_prog_attachshdr, GLuint program, GLuint shader) -LOAD(glDetachShader, void, gl_prog_detachshdr, GLuint program, GLuint shader) -LOAD(glGetAttachedShaders, void, gl_prog_getshdrs, GLuint program, GLsizei max, GLsizei *count, GLuint *shaders) -LOAD(glLinkProgram, void, gl_prog_link, GLuint program) -LOAD(glUseProgram, void, gl_prog_use, GLuint program) -LOAD(glGetProgramiv, void, gl_prog_param, GLuint prog, GLenum pname, GLint *params) -LOAD(glGetProgramInfoLog, void, gl_prog_infolog, GLuint prog, GLsizei size, GLsizei *len, char *data) -LOAD(glVertexAttribPointer, void, gl_va_define, GLuint index, GLint size, GLenum type, GLboolean normalized, GLsizei stride, const void *data) -LOAD(glEnableVertexAttribArray, void, gl_va_enable, GLuint index) -LOAD(glDisableVertexAttribArray, void, gl_va_disable, GLuint index) -LOAD(glGenVertexArrays, void, gl_va_gen, GLsizei n, GLuint *arrays) -LOAD(glBindVertexArray, void, gl_va_bind, GLuint va) -LOAD(glDrawArrays, void, gl_draw_arrays, GLenum mode, GLint first, GLsizei count) -LOAD(glDrawElements, void, gl_draw_elems, GLenum mode, GLsizei count, GLenum type, const void *indices) -LOAD(glPolygonMode, void, gl_poly_mode, GLenum face, GLenum mode) -LOAD(glGetUniformLocation, GLint, gl_uni_loc, GLuint program, const char *name); -LOAD(glGetUniformfv, void, gl_uni_getf, GLuint program, GLint location, GLfloat *params); -LOAD(glGetUniformiv, void, gl_uni_geti, GLuint program, GLint location, GLint *params); -FOREACH(`type', `FOR(`n', 1, 4, `LOADV(n, type, glUniform, void, gl_uni_set, GLint location)')', - `f, GLfloat', `i, GLint', `ui, GLuint') -FOREACH(`type', `FOR(`n', 1, 4, `LOADP(n, type, glUniform, void, gl_uni_set, GLint location, GLsizei count)')', - `fv, const GLfloat', `iv, const GLint', `uiv, const GLuint') -FOR(`n', 2, 4, `LOAD(glUniformMatrix`'n`'fv, void, gl_uni_setm`'n`'fv, GLint location, GLsizei count, GLboolean transpose, const GLfloat *value)') -LOAD(glUniformMatrix2x3fv, void, gl_uni_setm2x3fv, GLint location, GLsizei count, GLboolean transpose, const GLfloat *value) -LOAD(glUniformMatrix2x4fv, void, gl_uni_setm2x4fv, GLint location, GLsizei count, GLboolean transpose, const GLfloat *value) -LOAD(glUniformMatrix3x2fv, void, gl_uni_setm3x2fv, GLint location, GLsizei count, GLboolean transpose, const GLfloat *value) -LOAD(glUniformMatrix3x4fv, void, gl_uni_setm3x4fv, GLint location, GLsizei count, GLboolean transpose, const GLfloat *value) -LOAD(glUniformMatrix4x2fv, void, gl_uni_setm4x2fv, GLint location, GLsizei count, GLboolean transpose, const GLfloat *value) -LOAD(glUniformMatrix4x3fv, void, gl_uni_setm4x3fv, GLint location, GLsizei count, GLboolean transpose, const GLfloat *value) - -# Undefine - -undefine(`LOAD') - -divert(0)dnl |