4 Copyright 2008-2012 Michel Pollet <buserror@gmail.com>
6 This file is part of simavr.
8 simavr 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 simavr 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 simavr. 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_load_shader(
74 const char * filename,
80 if (stat(filename, &st))
82 int fd = open(filename, O_RDONLY);
86 int hlen = header ? strlen(header) : 0;
87 pgm = str_alloc(st.st_size + hlen);
89 strcpy(pgm->str, header);
91 if (read(fd, pgm->str + hlen, st.st_size) != st.st_size)
94 pgm->str[pgm->len] = 0; // zero terminate it
98 .name = str_new(filename),
101 c3shader_array_add(&p->shaders, s);
103 if (flags & C3_PROGRAM_LOAD_UNIFORM) {
104 char * cur = pgm->str;
107 while ((l = strsep(&cur, "\r\n")) != NULL) {
108 while (*l && *l <= ' ')
110 str_p line = str_new(l);
111 if (cur) // fix the endline after strsep
113 if (strncmp(line->str, "uniform", 7))
115 // printf("UNI: %s\n", line->str);
117 char * sep = line->str;
118 char * uniform = strsep(&sep, " \t");
119 char * unitype = strsep(&sep, " \t");
120 char * uniname = strsep(&sep, " \t");
122 * found a parameter, extract it's type & name
124 if (uniform && unitype && uniname) {
125 // trim semicolons etc
127 while (isalpha(*cl) || *cl == '_')
130 str_p name = str_new(uniname);
131 for (int pi = 0; pi < p->params.count && uniform; pi++)
132 if (!str_cmp(name, p->params.e[pi].name))
133 uniform = NULL; // already there
135 c3program_param_t pa = {
136 .type = str_new(unitype),
140 c3program_param_array_add(&p->params, pa);
141 printf("%s %s: new parameter '%s' '%s'\n", __func__,
142 p->name->str, unitype, uniname);
149 return p->shaders.count - 1;
156 fprintf(stderr, "%s: %s: %s\n", __func__, filename, strerror(errno));