summaryrefslogtreecommitdiff
path: root/src/genshader.c
blob: 25c7bf7f6522073e815d6c08fe631b21190ecdf6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
 * 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)
{
	FILE *out;
	static const char c1[] = "/* This file is generated by genshader.c */\n"
	"const char *kmscon_vert_shader = \"";
	static const char c2[] = "\";\nconst char *kmscon_frag_shader = \"";
	static const char c3[] = "\";";

	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);

	fclose(out);
}

int main(int argc, char *argv[])
{
	char *vert, *frag;
	size_t vs, fs;

	if (argc < 4) {
		fprintf(stderr, "genshader: missing parameters\n");
		return EXIT_FAILURE;
	}

	vert = read_file(argv[2], &vs);
	frag = read_file(argv[3], &fs);

	write_file(argv[1], vert, vs, frag, fs);
	free(vert);
	free(frag);

	return EXIT_SUCCESS;
}