summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Duncan <pabs@pablotron.org>2017-05-11 20:07:43 -0400
committerPaul Duncan <pabs@pablotron.org>2017-05-11 20:07:43 -0400
commit3ad598cad5f26b4d833af11931352de19db10fe9 (patch)
tree70aac4e3a0d454ed13fdd8f1ce548379db861aee
parent642627d6db57c1ae18580477d1c2a24a0817c868 (diff)
downloadcompute-test-3ad598cad5f26b4d833af11931352de19db10fe9.tar.bz2
compute-test-3ad598cad5f26b4d833af11931352de19db10fe9.zip
refactor link_program
-rw-r--r--main.c67
1 files changed, 60 insertions, 7 deletions
diff --git a/main.c b/main.c
index fa61427..e02ab55 100644
--- a/main.c
+++ b/main.c
@@ -13,6 +13,11 @@
// custom SDL_USEREVENT code for timer
#define EVENT_CODE_TIMER 0
+typedef struct {
+ const GLuint type;
+ const char *src;
+} shader_t;
+
static const float
verts[] = {
-0.5, 0.5, 0,
@@ -20,8 +25,8 @@ verts[] = {
0.0, -0.5, 0,
};
-static const char *
-vs_src =
+static const char
+vs_src[] =
"#version 320 es\n"
"\n"
"layout (location = 0) in mediump vec3 pos;\n"
@@ -30,8 +35,8 @@ vs_src =
" gl_Position = vec4(pos.x, pos.y, pos.z, 1);\n"
"}\n";
-static const char *
-fs_src =
+static const char
+fs_src[] =
"#version 320 es\n"
"\n"
"out mediump vec4 color;\n"
@@ -42,6 +47,12 @@ fs_src =
" color = vec4(0.5f + 0.5 * sin(time * 3.141), 0, 0, 1);\n"
"}\n";
+static const shader_t
+RENDER_SHADERS[] = {
+ { GL_VERTEX_SHADER, vs_src },
+ { GL_FRAGMENT_SHADER, fs_src },
+};
+
static GLuint
compile_shader(
const GLenum type,
@@ -69,6 +80,47 @@ compile_shader(
}
static GLuint
+link_program(
+ const size_t num_shaders,
+ const shader_t *shaders
+) {
+ // create program
+ GLuint r = glCreateProgram();
+
+ // compile shaders
+ GLuint ids[128];
+ for (size_t i = 0; i < num_shaders; i++) {
+ // compile shader
+ ids[i] = compile_shader(shaders[i].type, shaders[i].src);
+
+ // attach shader
+ glAttachShader(r, ids[i]);
+ }
+
+ // link program
+ glLinkProgram(r);
+
+ // check link status
+ GLint ok;
+ glGetProgramiv(r, GL_LINK_STATUS, &ok);
+ if (!ok) {
+ GLchar log[1024];
+ glGetProgramInfoLog(r, sizeof(log), NULL, log);
+ fprintf(stderr, "Program Link Error: %s\n", log);
+ exit(EXIT_FAILURE);
+ }
+
+ // delete shaders
+ for (size_t i = 0; i < num_shaders; i++) {
+ glDeleteShader(ids[i]);
+ }
+
+ // return result
+ return r;
+}
+
+#if 0
+static GLuint
link_render_program(
const char * const vs_src,
const char * const fs_src
@@ -97,13 +149,14 @@ link_render_program(
exit(EXIT_FAILURE);
}
- // delete shaders
+ // delete shaders
glDeleteShader(vs);
glDeleteShader(fs);
// return result
return r;
}
+#endif /* 0 */
#if 0
static GLuint
@@ -114,7 +167,7 @@ link_compute_program(
GLuint r = glCreateProgram();
}
-#endif
+#endif /* 0 */
static void
update_viewport(
@@ -262,7 +315,7 @@ int main(int argc, char *argv[]) {
glBindVertexArray(0);
// link program
- GLuint prog = link_render_program(vs_src, fs_src);
+ GLuint prog = link_program(2, RENDER_SHADERS);
GLint u_time = glGetUniformLocation(prog, "time");
// init flags