The paths can be off when building out of tree, so have make put them in for us instead. This requires turning the straight genshader.c file to a template. Signed-off-by: Ran Benita <ran234@gmail.com> Signed-off-by: David Herrmann <dh.herrmann@googlemail.com>
150 lines
4.0 KiB
C
150 lines
4.0 KiB
C
/*
|
|
* kmscon - Generate Shader Files
|
|
*
|
|
* Copyright (c) 2011 David Herrmann <dh.herrmann@googlemail.com>
|
|
* Copyright (c) 2011 University of Tuebingen
|
|
*
|
|
* Permission is hereby granted, free of charge, to any person obtaining
|
|
* a copy of this software and associated documentation files
|
|
* (the "Software"), to deal in the Software without restriction, including
|
|
* without limitation the rights to use, copy, modify, merge, publish,
|
|
* distribute, sublicense, and/or sell copies of the Software, and to
|
|
* permit persons to whom the Software is furnished to do so, subject to
|
|
* the following conditions:
|
|
*
|
|
* The above copyright notice and this permission notice shall be included
|
|
* in all copies or substantial portions of the Software.
|
|
*
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
|
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
*/
|
|
|
|
/*
|
|
* Shader Generator
|
|
* This takes as arguments two shaders and creates a C-source file which
|
|
* contains these shaders as constants.
|
|
*/
|
|
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
|
|
static char *read_file(const char *path, size_t *size)
|
|
{
|
|
FILE *ffile;
|
|
ssize_t len;
|
|
char *buf;
|
|
|
|
ffile = fopen(path, "rb");
|
|
if (!ffile) {
|
|
fprintf(stderr, "genshader: cannot open %s: %m\n", path);
|
|
abort();
|
|
}
|
|
|
|
if (fseek(ffile, 0, SEEK_END) != 0) {
|
|
fprintf(stderr, "genshader: cannot seek %s: %m\n", path);
|
|
abort();
|
|
}
|
|
|
|
len = ftell(ffile);
|
|
if (len < 0) {
|
|
fprintf(stderr, "genshader: cannot tell %s: %m\n", path);
|
|
abort();
|
|
}
|
|
|
|
if (len < 1) {
|
|
fprintf(stderr, "genshader: empty file %s\n", path);
|
|
abort();
|
|
}
|
|
|
|
rewind(ffile);
|
|
|
|
buf = malloc(len + 1);
|
|
if (!buf) {
|
|
fprintf(stderr, "genshader: memory allocation failed\n");
|
|
abort();
|
|
}
|
|
|
|
if (len != fread(buf, 1, len, ffile)) {
|
|
fprintf(stderr, "genshader: cannot read %s: %m\n", path);
|
|
abort();
|
|
}
|
|
|
|
buf[len] = 0;
|
|
*size = len;
|
|
fclose(ffile);
|
|
|
|
return buf;
|
|
}
|
|
|
|
static void write_seq(FILE *out, const char *src, size_t len)
|
|
{
|
|
size_t i;
|
|
|
|
for (i = 0; i < len; ++i) {
|
|
if (src[i] == '\n') {
|
|
fwrite("\\n\"\n\"", 5, 1, out);
|
|
} else if (src[i] == '"') {
|
|
fwrite("\\\"", 2, 1, out);
|
|
} else {
|
|
fwrite(&src[i], 1, 1, out);
|
|
}
|
|
}
|
|
}
|
|
|
|
static void write_file(const char *path, const char *vs, size_t l1,
|
|
const char *fs, size_t l2, const char *tvs, size_t l3,
|
|
const char *tfs, size_t l4)
|
|
{
|
|
FILE *out;
|
|
static const char c1[] = "/* This file is generated by genshader.c */\n"
|
|
"const char *kmscon_vert_def = \"";
|
|
static const char c2[] = "\";\nconst char *kmscon_frag_def = \"";
|
|
static const char c3[] = "\";\nconst char *kmscon_vert_tex = \"";
|
|
static const char c4[] = "\";\nconst char *kmscon_frag_tex = \"";
|
|
static const char c5[] = "\";";
|
|
|
|
out = fopen(path, "wb");
|
|
if (!out) {
|
|
fprintf(stderr, "genshader: cannot open %s: %m\n", path);
|
|
abort();
|
|
}
|
|
|
|
fwrite(c1, sizeof(c1) - 1, 1, out);
|
|
write_seq(out, vs, l1);
|
|
fwrite(c2, sizeof(c2) - 1, 1, out);
|
|
write_seq(out, fs, l2);
|
|
fwrite(c3, sizeof(c3) - 1, 1, out);
|
|
write_seq(out, tvs, l3);
|
|
fwrite(c4, sizeof(c4) - 1, 1, out);
|
|
write_seq(out, tfs, l4);
|
|
fwrite(c5, sizeof(c5) - 1, 1, out);
|
|
|
|
fclose(out);
|
|
}
|
|
|
|
int main(int argc, char *argv[])
|
|
{
|
|
char *def_vert, *def_frag, *tex_vert, *tex_frag;
|
|
size_t vs, fs, tvs, tfs;
|
|
|
|
def_vert = read_file("@abs_srcdir@/output_shader_def.vert", &vs);
|
|
def_frag = read_file("@abs_srcdir@/output_shader_def.frag", &fs);
|
|
tex_vert = read_file("@abs_srcdir@/output_shader_tex.vert", &tvs);
|
|
tex_frag = read_file("@abs_srcdir@/output_shader_tex.frag", &tfs);
|
|
|
|
write_file("@abs_builddir@/output_shaders.c", def_vert, vs,
|
|
def_frag, fs, tex_vert, tvs, tex_frag, tfs);
|
|
|
|
free(tex_vert);
|
|
free(tex_frag);
|
|
free(def_vert);
|
|
free(def_frag);
|
|
|
|
return EXIT_SUCCESS;
|
|
}
|