c3program: Include digits in uniform names
[simavr] / examples / shared / libc3 / src / c3program.c
index e7c7998..f045de8 100644 (file)
@@ -3,20 +3,20 @@
 
        Copyright 2008-2012 Michel Pollet <buserror@gmail.com>
 
-       This file is part of simavr.
+       This file is part of libc3.
 
-       simavr is free software: you can redistribute it and/or modify
+       libc3 is free software: you can redistribute it and/or modify
        it under the terms of the GNU General Public License as published by
        the Free Software Foundation, either version 3 of the License, or
        (at your option) any later version.
 
-       simavr is distributed in the hope that it will be useful,
+       libc3 is distributed in the hope that it will be useful,
        but WITHOUT ANY WARRANTY; without even the implied warranty of
        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
        GNU General Public License for more details.
 
        You should have received a copy of the GNU General Public License
-       along with simavr.  If not, see <http://www.gnu.org/licenses/>.
+       along with libc3.  If not, see <http://www.gnu.org/licenses/>.
  */
 
 #include <sys/types.h>
 
 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;
 }
 
@@ -66,6 +82,17 @@ c3program_purge(
        c3shader_array_free(&p->shaders);
 }
 
+c3program_param_p
+c3program_locate_param(
+               c3program_p p,
+               const char * name )
+{
+       for (int pi = 0; pi < p->params.count; pi++)
+               if (!strcmp(p->params.e[pi].name->str, name))
+                       return &p->params.e[pi];
+       return NULL;
+}
+
 int
 c3program_load_shader(
                c3program_p p,
@@ -117,28 +144,33 @@ c3program_load_shader(
                        char * sep = line->str;
                        char * uniform = strsep(&sep, " \t");
                        char * unitype = strsep(&sep, " \t");
-                       char * uniname = strsep(&sep, " \t");
+                       char * uniname = strsep(&sep, " \t=;");
                        /*
                         * found a parameter, extract it's type & name
                         */
                        if (uniform && unitype && uniname) {
                                // trim semicolons etc
                                char *cl = uniname;
-                               while (isalpha(*cl) || *cl == '_')
+                               while (isalpha(*cl) || *cl == '_' || isdigit(*cl))
                                        cl++;
                                *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);
-                                       printf("%s %s: new parameter '%s' '%s'\n", __func__,
+                                       if (p->verbose)
+                                               printf("%s %s: new parameter '%s' '%s'\n", __func__,
                                                        p->name->str, unitype, uniname);
                                } else
                                        str_free(name);