libc3: Added c3program
authorMichel Pollet <buserror@gmail.com>
Sun, 3 Jun 2012 14:09:34 +0000 (15:09 +0100)
committerMichel Pollet <buserror@gmail.com>
Sun, 3 Jun 2012 14:09:34 +0000 (15:09 +0100)
To load shader(s)

Signed-off-by: Michel Pollet <buserror@gmail.com>
examples/shared/libc3/src/c3program.c [new file with mode: 0644]
examples/shared/libc3/src/c3program.h [new file with mode: 0644]

diff --git a/examples/shared/libc3/src/c3program.c b/examples/shared/libc3/src/c3program.c
new file mode 100644 (file)
index 0000000..c91acf1
--- /dev/null
@@ -0,0 +1,139 @@
+/*
+       c3program.c
+
+       Copyright 2008-2012 Michel Pollet <buserror@gmail.com>
+
+       This file is part of simavr.
+
+       simavr is free software: you can redistribute it and/or modify
+       it under the terms of the GNU General Public License as published by
+       the Free Software Foundation, either version 3 of the License, or
+       (at your option) any later version.
+
+       simavr is distributed in the hope that it will be useful,
+       but WITHOUT ANY WARRANTY; without even the implied warranty of
+       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+       GNU General Public License for more details.
+
+       You should have received a copy of the GNU General Public License
+       along with simavr.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
+#include <stdio.h>
+#include <errno.h>
+#include <fcntl.h>
+
+#include "c3program.h"
+
+c3program_p
+c3program_new(
+               const char * name)
+{
+       c3program_p p = malloc(sizeof(*p));
+       memset(p, 0, sizeof(*p));
+       p->name = str_new(name);
+       return p;
+}
+
+void
+c3program_dispose(
+               const char * name)
+{
+       // TODO: implement c3program_dispose
+}
+
+void
+c3program_purge(
+               c3program_p p)
+{
+       // TODO: implement c3program_purge
+}
+
+int
+c3program_load_shader(
+               c3program_p p,
+               uint32_t        type,
+               const char * header,
+               const char * filename,
+               uint16_t flags)
+{
+       struct stat st;
+       str_p pgm = NULL;
+
+       if (stat(filename, &st))
+               goto error;
+       int fd = open(filename, O_RDONLY);
+       if (fd == -1)
+               goto error;
+
+       int hlen = header ? strlen(header) : 0;
+       pgm = str_alloc(st.st_size + hlen);
+       if (header)
+               strcpy(pgm->str, header);
+
+       if (read(fd, pgm->str + hlen, st.st_size) != st.st_size)
+               goto error;
+       close(fd);
+       pgm->str[pgm->len] = 0; // zero terminate it
+
+       c3shader_t s = {
+               .type = type,
+               .name = str_new(filename),
+               .shader = pgm,
+       };
+       c3shader_array_add(&p->shaders, s);
+
+       if (flags & C3_PROGRAM_LOAD_UNIFORM) {
+               char * cur = pgm->str;
+               char * l;
+
+               while ((l = strsep(&cur, "\r\n")) != NULL) {
+                       while (*l && *l <= ' ')
+                               l++;
+                       str_p line = str_new(l);
+                       if (cur) // fix the endline after strsep
+                               *(cur-1) = '\n';
+                       if (strncmp(line->str, "uniform", 7))
+                               continue;
+                       // printf("UNI: %s\n", line->str);
+
+                       char * sep = line->str;
+                       char * uniform = strsep(&sep, " \t");
+                       char * unitype = strsep(&sep, " \t");
+                       char * uniname = strsep(&sep, " \t");
+                       /*
+                        * found a parameter, extract it's type & name
+                        */
+                       if (uniform && unitype && uniname) {
+                               str_p name = str_new(uniname);
+                               for (int pi = 0; pi < p->params.count && uniform; pi++)
+                                       if (!str_cmp(name, p->params.e[pi].name))
+                                               uniform = NULL; // already there
+                               if (uniform) {
+                                       c3program_param_t pa = {
+                                                       .type = str_new(unitype),
+                                                       .name = name,
+                                       };
+                                       c3program_param_array_add(&p->params, pa);
+                                       printf("%s %s: new parameter '%s' '%s'\n", __func__,
+                                                       p->name->str, unitype, uniname);
+                               } else
+                                       str_free(name);
+                       }
+                       str_free(line);
+               }
+       }
+       return p->shaders.count - 1;
+
+error:
+       if (fd != -1)
+               close(fd);
+       if (pgm)
+               str_free(pgm);
+       fprintf(stderr, "%s: %s: %s\n", __func__, filename, strerror(errno));
+       return -1;
+
+}
diff --git a/examples/shared/libc3/src/c3program.h b/examples/shared/libc3/src/c3program.h
new file mode 100644 (file)
index 0000000..c709580
--- /dev/null
@@ -0,0 +1,84 @@
+/*
+       c3program.h
+
+       Copyright 2008-2012 Michel Pollet <buserror@gmail.com>
+
+       This file is part of simavr.
+
+       simavr is free software: you can redistribute it and/or modify
+       it under the terms of the GNU General Public License as published by
+       the Free Software Foundation, either version 3 of the License, or
+       (at your option) any later version.
+
+       simavr is distributed in the hope that it will be useful,
+       but WITHOUT ANY WARRANTY; without even the implied warranty of
+       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+       GNU General Public License for more details.
+
+       You should have received a copy of the GNU General Public License
+       along with simavr.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+
+#ifndef __C3PROGRAM_H___
+#define __C3PROGRAM_H___
+
+#include "c_utils.h"
+
+typedef struct c3shader_t {
+       uint32_t sid;   // shader id
+       uint32_t type;
+       str_p   name;
+       str_p   shader;
+       str_p   log;
+} c3shader_t, *c3shader_p;
+
+DECLARE_C_ARRAY(c3shader_t, c3shader_array, 4);
+
+typedef struct c3program_param_t {
+       int32_t pid;    // parameter id
+       str_p   type;
+       str_p   name;
+} c3program_param_t, *c3program_param_p;
+
+DECLARE_C_ARRAY(c3program_param_t, c3program_param_array, 4);
+
+typedef struct c3program_t {
+       uint32_t pid;   // program id
+       str_p name;
+       c3shader_array_t        shaders;
+       c3program_param_array_t params;
+       str_p   log;
+} c3program_t, *c3program_p;
+
+DECLARE_C_ARRAY(c3program_p, c3program_array, 4);
+
+c3program_p
+c3program_new(
+               const char * name);
+
+void
+c3program_dispose(
+               const char * name);
+
+void
+c3program_purge(
+               c3program_p p);
+
+enum {
+       C3_PROGRAM_LOAD_UNIFORM = (1 << 0),
+};
+
+int
+c3program_load_shader(
+               c3program_p p,
+               uint32_t        type,
+               const char * header,
+               const char * filename,
+               uint16_t flags);
+
+IMPLEMENT_C_ARRAY(c3program_param_array);
+IMPLEMENT_C_ARRAY(c3shader_array);
+IMPLEMENT_C_ARRAY(c3program_array);
+
+#endif /* __C3PROGRAM_H___ */