From cfa527859288457f413cc78cdf33c5a7e79518b6 Mon Sep 17 00:00:00 2001 From: Michel Pollet Date: Fri, 15 Jun 2012 09:20:33 +0100 Subject: [PATCH] c3program: can specify uniform order By passing an array of names, these will occupy the uniform array first, before the parsing phase populates the list Signed-off-by: Michel Pollet --- examples/shared/libc3/src/c3program.c | 26 +++++++++++++++++++++++--- examples/shared/libc3/src/c3program.h | 10 +++++++++- 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/examples/shared/libc3/src/c3program.c b/examples/shared/libc3/src/c3program.c index 5e2edf4..86e4404 100644 --- a/examples/shared/libc3/src/c3program.c +++ b/examples/shared/libc3/src/c3program.c @@ -31,11 +31,27 @@ c3program_p c3program_new( - const char * name) + const char * name, + const char ** uniforms ) { c3program_p p = malloc(sizeof(*p)); memset(p, 0, sizeof(*p)); p->name = str_new(name); + + /* Allow specifying uniform names to make sure they are in + * the specified order in the array, this allow direct indexing + * instead of doign string lookup + */ + if (uniforms) { + for (int ui = 0; uniforms[ui]; ui++) { + c3program_param_t pa = { + .name = str_new(uniforms[ui]), + .program = p, + .index = p->params.count, + }; + c3program_param_array_add(&p->params, pa); + } + } return p; } @@ -140,17 +156,21 @@ c3program_load_shader( *cl = 0; str_p name = str_new(uniname); for (int pi = 0; pi < p->params.count && uniform; pi++) - if (!str_cmp(name, p->params.e[pi].name)) + if (!str_cmp(name, p->params.e[pi].name)) { + if (!p->params.e[pi].type) + p->params.e[pi].type = str_new(unitype); uniform = NULL; // already there + } if (uniform) { c3program_param_t pa = { .type = str_new(unitype), .name = name, .program = p, + .index = p->params.count, }; c3program_param_array_add(&p->params, pa); if (p->verbose) - printf("%s %s: new parameter '%s' '%s'\n", __func__, + printf("%s %s: new parameter '%s' '%s'\n", __func__, p->name->str, unitype, uniname); } else str_free(name); diff --git a/examples/shared/libc3/src/c3program.h b/examples/shared/libc3/src/c3program.h index 626d12b..23d4b4f 100644 --- a/examples/shared/libc3/src/c3program.h +++ b/examples/shared/libc3/src/c3program.h @@ -37,6 +37,7 @@ DECLARE_C_ARRAY(c3shader_t, c3shader_array, 4); typedef struct c3program_param_t { struct c3program_t * program; + int index; // index number in paramerer array c3apiobject_t pid; // parameter id str_p type; str_p name; @@ -56,9 +57,16 @@ typedef struct c3program_t { DECLARE_C_ARRAY(c3program_p, c3program_array, 4); //! Allocates a new, empty program +/* if 'uniforms' is non null it is a NULL terminated array of + * uniform names to pre-add to the uniform array in the specified + * order. + * This make sure they are in the specified order in the array, + * to allow direct indexing instead of doing string lookup by name + */ c3program_p c3program_new( - const char * name); + const char * name, + const char ** uniforms /* optional */); //! disposes of a c3program memory void -- 2.39.5