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>
29 #include "c3program.h"
35 c3program_p p = malloc(sizeof(*p));
36 memset(p, 0, sizeof(*p));
37 p->name = str_new(name);
45 // TODO: implement c3program_dispose
52 // TODO: implement c3program_purge
56 c3program_load_shader(
60 const char * filename,
66 if (stat(filename, &st))
68 int fd = open(filename, O_RDONLY);
72 int hlen = header ? strlen(header) : 0;
73 pgm = str_alloc(st.st_size + hlen);
75 strcpy(pgm->str, header);
77 if (read(fd, pgm->str + hlen, st.st_size) != st.st_size)
80 pgm->str[pgm->len] = 0; // zero terminate it
84 .name = str_new(filename),
87 c3shader_array_add(&p->shaders, s);
89 if (flags & C3_PROGRAM_LOAD_UNIFORM) {
90 char * cur = pgm->str;
93 while ((l = strsep(&cur, "\r\n")) != NULL) {
94 while (*l && *l <= ' ')
96 str_p line = str_new(l);
97 if (cur) // fix the endline after strsep
99 if (strncmp(line->str, "uniform", 7))
101 // printf("UNI: %s\n", line->str);
103 char * sep = line->str;
104 char * uniform = strsep(&sep, " \t");
105 char * unitype = strsep(&sep, " \t");
106 char * uniname = strsep(&sep, " \t");
108 * found a parameter, extract it's type & name
110 if (uniform && unitype && uniname) {
111 str_p name = str_new(uniname);
112 for (int pi = 0; pi < p->params.count && uniform; pi++)
113 if (!str_cmp(name, p->params.e[pi].name))
114 uniform = NULL; // already there
116 c3program_param_t pa = {
117 .type = str_new(unitype),
120 c3program_param_array_add(&p->params, pa);
121 printf("%s %s: new parameter '%s' '%s'\n", __func__,
122 p->name->str, unitype, uniname);
129 return p->shaders.count - 1;
136 fprintf(stderr, "%s: %s: %s\n", __func__, filename, strerror(errno));