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"
36 c3program_p p = malloc(sizeof(*p));
37 memset(p, 0, sizeof(*p));
38 p->name = str_new(name);
47 for (int pi = 0; pi < p->params.count; pi++) {
48 c3program_param_p pa = &p->params.e[pi];
51 c3program_param_array_free(&p->params);
61 for (int si = 0; si < p->shaders.count; si++) {
62 c3shader_p s = &p->shaders.e[si];
66 c3shader_array_free(&p->shaders);
70 c3program_locate_param(
74 for (int pi = 0; pi < p->params.count; pi++)
75 if (!strcmp(p->params.e[pi].name->str, name))
76 return &p->params.e[pi];
81 c3program_load_shader(
85 const char * filename,
91 if (stat(filename, &st))
93 int fd = open(filename, O_RDONLY);
97 int hlen = header ? strlen(header) : 0;
98 pgm = str_alloc(st.st_size + hlen);
100 strcpy(pgm->str, header);
102 if (read(fd, pgm->str + hlen, st.st_size) != st.st_size)
105 pgm->str[pgm->len] = 0; // zero terminate it
109 .name = str_new(filename),
112 c3shader_array_add(&p->shaders, s);
114 if (flags & C3_PROGRAM_LOAD_UNIFORM) {
115 char * cur = pgm->str;
118 while ((l = strsep(&cur, "\r\n")) != NULL) {
119 while (*l && *l <= ' ')
121 str_p line = str_new(l);
122 if (cur) // fix the endline after strsep
124 if (strncmp(line->str, "uniform", 7))
126 // printf("UNI: %s\n", line->str);
128 char * sep = line->str;
129 char * uniform = strsep(&sep, " \t");
130 char * unitype = strsep(&sep, " \t");
131 char * uniname = strsep(&sep, " \t");
133 * found a parameter, extract it's type & name
135 if (uniform && unitype && uniname) {
136 // trim semicolons etc
138 while (isalpha(*cl) || *cl == '_')
141 str_p name = str_new(uniname);
142 for (int pi = 0; pi < p->params.count && uniform; pi++)
143 if (!str_cmp(name, p->params.e[pi].name))
144 uniform = NULL; // already there
146 c3program_param_t pa = {
147 .type = str_new(unitype),
151 c3program_param_array_add(&p->params, pa);
153 printf("%s %s: new parameter '%s' '%s'\n", __func__,
154 p->name->str, unitype, uniname);
161 return p->shaders.count - 1;
168 fprintf(stderr, "%s: %s: %s\n", __func__, filename, strerror(errno));