summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVito Caputo <vcaputo@pengaru.com>2021-05-26 19:45:55 -0700
committerVito Caputo <vcaputo@pengaru.com>2021-05-26 19:45:55 -0700
commitdc737ded2955dbf4bc1e30d910ae38433399105e (patch)
tree04110aab26eee0814a16bd254d87aa308af60414
parent628816cc854a10e6616993b9491d7af6ade7e6d9 (diff)
shader: refresh locations on shader reloads
This needs to be done after every shader pogram rebuild, even if the uniforms and everything are unchanged.
-rw-r--r--src/shader.c50
-rw-r--r--src/shader.h2
2 files changed, 29 insertions, 23 deletions
diff --git a/src/shader.c b/src/shader.c
index bf9d325..1cadc7f 100644
--- a/src/shader.c
+++ b/src/shader.c
@@ -26,7 +26,8 @@
typedef struct shader_t {
unsigned program, refcnt;
unsigned n_uniforms, n_attributes;
- int *uniforms, *attributes;
+ const char **uniforms, **attributes;
+ int *uniform_locations, *attribute_locations;
const char *vs_path, *fs_path;
struct timespec vs_mtime, fs_mtime;
@@ -80,6 +81,14 @@ unsigned int shader_pair_new_bare(const char *vs_src, const char *fs_src)
return shader;
}
+static void get_locations(shader_t *shader)
+{
+ for (unsigned i = 0; i < shader->n_uniforms; i++)
+ shader->uniform_locations[i] = glGetUniformLocation(shader->program, shader->uniforms[i]);
+
+ for (unsigned i = 0; i < shader->n_attributes; i++)
+ shader->attribute_locations[i] = glGetAttribLocation(shader->program, shader->attributes[i]);
+}
shader_t * shader_pair_new(const char *vs_src, const char *fs_src, unsigned n_uniforms, const char **uniforms, unsigned n_attributes, const char **attributes)
{
@@ -95,16 +104,14 @@ shader_t * shader_pair_new(const char *vs_src, const char *fs_src, unsigned n_un
shader->program = shader_pair_new_bare(vs_src, fs_src);
shader->refcnt++;
+ shader->uniforms = uniforms;
shader->n_uniforms = n_uniforms;
+ shader->attributes = attributes;
shader->n_attributes = n_attributes;
- shader->uniforms = shader->locations;
- shader->attributes = &shader->locations[n_uniforms];
+ shader->uniform_locations = shader->locations;
+ shader->attribute_locations = &shader->locations[n_uniforms];
- for (unsigned i = 0; i < n_uniforms; i++)
- shader->uniforms[i] = glGetUniformLocation(shader->program, uniforms[i]);
-
- for (unsigned i = 0; i < n_attributes; i++)
- shader->attributes[i] = glGetAttribLocation(shader->program, attributes[i]);
+ get_locations(shader);
return shader;
}
@@ -170,6 +177,8 @@ int shader_reload_files(shader_t *shader)
free(vs_src);
free(fs_src);
+ get_locations(shader);
+
return ret;
}
@@ -192,19 +201,16 @@ shader_t * shader_pair_new_files(const char *vs_path, const char *fs_path, unsig
shader->fs_path = strdup(fs_path);
fatal_if(!shader->fs_path, "unable to dup fs_path \"%s\"", fs_path);
- (void) shader_reload_files(shader);
-
- shader->refcnt++;
shader->n_uniforms = n_uniforms;
+ shader->uniforms = uniforms;
shader->n_attributes = n_attributes;
- shader->uniforms = shader->locations;
- shader->attributes = &shader->locations[n_uniforms];
+ shader->attributes = attributes;
+ shader->uniform_locations = shader->locations;
+ shader->attribute_locations = &shader->locations[n_uniforms];
- for (unsigned i = 0; i < n_uniforms; i++)
- shader->uniforms[i] = glGetUniformLocation(shader->program, uniforms[i]);
+ shader->refcnt++;
- for (unsigned i = 0; i < n_attributes; i++)
- shader->attributes[i] = glGetAttribLocation(shader->program, attributes[i]);
+ (void) shader_reload_files(shader);
return shader;
}
@@ -233,21 +239,21 @@ shader_t * shader_free(shader_t *shader)
}
-void shader_use(shader_t *shader, unsigned *res_n_uniforms, int **res_uniforms, unsigned *res_n_attributes, int **res_attributes)
+void shader_use(shader_t *shader, unsigned *res_n_uniforms, int **res_uniform_locations, unsigned *res_n_attributes, int **res_attribute_locations)
{
assert(shader);
if (res_n_uniforms)
*res_n_uniforms = shader->n_uniforms;
- if (res_uniforms)
- *res_uniforms = shader->uniforms;
+ if (res_uniform_locations)
+ *res_uniform_locations = shader->uniform_locations;
if (res_n_attributes)
*res_n_attributes = shader->n_attributes;
- if (res_attributes)
- *res_attributes = shader->attributes;
+ if (res_attribute_locations)
+ *res_attribute_locations = shader->attribute_locations;
glUseProgram(shader->program);
}
diff --git a/src/shader.h b/src/shader.h
index 9799e40..b52189a 100644
--- a/src/shader.h
+++ b/src/shader.h
@@ -24,7 +24,7 @@ shader_t * shader_pair_new(const char *vs_src, const char *fs_src, unsigned n_un
shader_t * shader_pair_new_files(const char *vs_path, const char *fs_path, unsigned n_uniforms, const char **uniforms, unsigned n_attributes, const char **attributes);
void shader_ref(shader_t *shader);
shader_t * shader_free(shader_t *shader);
-void shader_use(shader_t *shader, unsigned *res_n_uniforms, int **res_uniforms, unsigned *res_n_attributes, int **res_attributes);
+void shader_use(shader_t *shader, unsigned *res_n_uniforms, int **res_uniform_locations, unsigned *res_n_attributes, int **res_attribute_locations);
int shader_reload_files(shader_t *shader);
#endif
© 2021 All Rights Reserved