c3object: Implements 'hidden' objects
[simavr] / examples / shared / libc3 / src / c3program.c
1 /*
2         c3program.c
3
4         Copyright 2008-2012 Michel Pollet <buserror@gmail.com>
5
6         This file is part of libc3.
7
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.
12
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.
17
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/>.
20  */
21
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <unistd.h>
25 #include <stdio.h>
26 #include <errno.h>
27 #include <ctype.h>
28 #include <fcntl.h>
29
30 #include "c3program.h"
31
32 c3program_p
33 c3program_new(
34                 const char * name)
35 {
36         c3program_p p = malloc(sizeof(*p));
37         memset(p, 0, sizeof(*p));
38         p->name = str_new(name);
39         return p;
40 }
41
42 void
43 c3program_dispose(
44                 c3program_p p)
45 {
46         c3program_purge(p);
47         for (int pi = 0; pi < p->params.count; pi++) {
48                 c3program_param_p pa = &p->params.e[pi];
49                 str_free(pa->name);
50         }
51         c3program_param_array_free(&p->params);
52         str_free(p->name);
53         str_free(p->log);
54         free(p);
55 }
56
57 void
58 c3program_purge(
59                 c3program_p p)
60 {
61         for (int si = 0; si < p->shaders.count; si++) {
62                 c3shader_p s = &p->shaders.e[si];
63                 str_free(s->name);
64                 str_free(s->shader);
65         }
66         c3shader_array_free(&p->shaders);
67 }
68
69 c3program_param_p
70 c3program_locate_param(
71                 c3program_p p,
72                 const char * name )
73 {
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];
77         return NULL;
78 }
79
80 int
81 c3program_load_shader(
82                 c3program_p p,
83                 uint32_t        type,
84                 const char * header,
85                 const char * filename,
86                 uint16_t flags)
87 {
88         struct stat st;
89         str_p pgm = NULL;
90
91         if (stat(filename, &st))
92                 goto error;
93         int fd = open(filename, O_RDONLY);
94         if (fd == -1)
95                 goto error;
96
97         int hlen = header ? strlen(header) : 0;
98         pgm = str_alloc(st.st_size + hlen);
99         if (header)
100                 strcpy(pgm->str, header);
101
102         if (read(fd, pgm->str + hlen, st.st_size) != st.st_size)
103                 goto error;
104         close(fd);
105         pgm->str[pgm->len] = 0; // zero terminate it
106
107         c3shader_t s = {
108                 .type = type,
109                 .name = str_new(filename),
110                 .shader = pgm,
111         };
112         c3shader_array_add(&p->shaders, s);
113
114         if (flags & C3_PROGRAM_LOAD_UNIFORM) {
115                 char * cur = pgm->str;
116                 char * l;
117
118                 while ((l = strsep(&cur, "\r\n")) != NULL) {
119                         while (*l && *l <= ' ')
120                                 l++;
121                         str_p line = str_new(l);
122                         if (cur) // fix the endline after strsep
123                                 *(cur-1) = '\n';
124                         if (strncmp(line->str, "uniform", 7))
125                                 continue;
126                         // printf("UNI: %s\n", line->str);
127
128                         char * sep = line->str;
129                         char * uniform = strsep(&sep, " \t");
130                         char * unitype = strsep(&sep, " \t");
131                         char * uniname = strsep(&sep, " \t");
132                         /*
133                          * found a parameter, extract it's type & name
134                          */
135                         if (uniform && unitype && uniname) {
136                                 // trim semicolons etc
137                                 char *cl = uniname;
138                                 while (isalpha(*cl) || *cl == '_')
139                                         cl++;
140                                 *cl = 0;
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
145                                 if (uniform) {
146                                         c3program_param_t pa = {
147                                                         .type = str_new(unitype),
148                                                         .name = name,
149                                                         .program = p,
150                                         };
151                                         c3program_param_array_add(&p->params, pa);
152                                         if (p->verbose)
153                                         printf("%s %s: new parameter '%s' '%s'\n", __func__,
154                                                         p->name->str, unitype, uniname);
155                                 } else
156                                         str_free(name);
157                         }
158                         str_free(line);
159                 }
160         }
161         return p->shaders.count - 1;
162
163 error:
164         if (fd != -1)
165                 close(fd);
166         if (pgm)
167                 str_free(pgm);
168         fprintf(stderr, "%s: %s: %s\n", __func__, filename, strerror(errno));
169         return -1;
170
171 }