Commit cfa527859288457f413cc78cdf33c5a7e79518b6
authorMichel Pollet <buserror@gmail.com>
Fri, 15 Jun 2012 08:20:33 +0000 (09:20 +0100)
committerMichel Pollet <buserror@gmail.com>
Fri, 15 Jun 2012 08:20:33 +0000 (09:20 +0100)
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 <buserror@gmail.com>
2 files changed:
examples/shared/libc3/src/c3program.c
examples/shared/libc3/src/c3program.h

index 5e2edf41717628857f3ee6a636781004d75e6d52..86e4404e00158da410f936cac7f7aa4f6d45d732 100644 (file)
 
 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);
index 626d12bc7744526cbbe8bd4908151a31b7628093..23d4b4fe8432a6d18ed7e9f4488b117d1adac032 100644 (file)
@@ -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