4 Copyright 2008-2012 Michel Pollet <buserror@gmail.com>
6 This file is part of libc3.
8 libc3 is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 libc3 is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with libc3. If not, see <http://www.gnu.org/licenses/>.
22 #include <sys/types.h>
30 #include "c3program.h"
35 const char ** uniforms )
37 c3program_p p = malloc(sizeof(*p));
38 memset(p, 0, sizeof(*p));
39 p->name = str_new(name);
41 /* Allow specifying uniform names to make sure they are in
42 * the specified order in the array, this allow direct indexing
43 * instead of doign string lookup
46 for (int ui = 0; uniforms[ui]; ui++) {
47 c3program_param_t pa = {
48 .name = str_new(uniforms[ui]),
50 .index = p->params.count,
52 c3program_param_array_add(&p->params, pa);
63 for (int pi = 0; pi < p->params.count; pi++) {
64 c3program_param_p pa = &p->params.e[pi];
67 c3program_param_array_free(&p->params);
77 for (int si = 0; si < p->shaders.count; si++) {
78 c3shader_p s = &p->shaders.e[si];
82 c3shader_array_free(&p->shaders);
86 c3program_locate_param(
90 for (int pi = 0; pi < p->params.count; pi++)
91 if (!strcmp(p->params.e[pi].name->str, name))
92 return &p->params.e[pi];
97 c3program_load_shader(
101 const char * filename,
107 if (stat(filename, &st))
109 int fd = open(filename, O_RDONLY);
113 int hlen = header ? strlen(header) : 0;
114 pgm = str_alloc(st.st_size + hlen);
116 strcpy(pgm->str, header);
118 if (read(fd, pgm->str + hlen, st.st_size) != st.st_size)
121 pgm->str[pgm->len] = 0; // zero terminate it
125 .name = str_new(filename),
128 c3shader_array_add(&p->shaders, s);
130 if (flags & C3_PROGRAM_LOAD_UNIFORM) {
131 char * cur = pgm->str;
134 while ((l = strsep(&cur, "\r\n")) != NULL) {
135 while (*l && *l <= ' ')
137 str_p line = str_new(l);
138 if (cur) // fix the endline after strsep
140 if (strncmp(line->str, "uniform", 7))
142 // printf("UNI: %s\n", line->str);
144 char * sep = line->str;
145 char * uniform = strsep(&sep, " \t");
146 char * unitype = strsep(&sep, " \t");
147 char * uniname = strsep(&sep, " \t");
149 * found a parameter, extract it's type & name
151 if (uniform && unitype && uniname) {
152 // trim semicolons etc
154 while (isalpha(*cl) || *cl == '_')
157 str_p name = str_new(uniname);
158 for (int pi = 0; pi < p->params.count && uniform; pi++)
159 if (!str_cmp(name, p->params.e[pi].name)) {
160 if (!p->params.e[pi].type)
161 p->params.e[pi].type = str_new(unitype);
162 uniform = NULL; // already there
165 c3program_param_t pa = {
166 .type = str_new(unitype),
169 .index = p->params.count,
171 c3program_param_array_add(&p->params, pa);
173 printf("%s %s: new parameter '%s' '%s'\n", __func__,
174 p->name->str, unitype, uniname);
181 return p->shaders.count - 1;
188 fprintf(stderr, "%s: %s: %s\n", __func__, filename, strerror(errno));