Merge branch 'glsl-pp-rework-2'
[mesa.git] / src / glsl / apps / compile.c
1 /**************************************************************************
2 *
3 * Copyright 2009 VMware, Inc.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <assert.h>
32 #include "../pp/sl_pp_public.h"
33 #include "../cl/sl_cl_parse.h"
34
35
36 static void
37 usage(void)
38 {
39 printf("Usage:\n");
40 printf(" compile fragment|vertex <source> <output>\n");
41 }
42
43 int
44 main(int argc,
45 char *argv[])
46 {
47 FILE *in;
48 long size;
49 char *inbuf;
50 struct sl_pp_purify_options options;
51 char errmsg[100] = "";
52 struct sl_pp_context *context;
53 struct sl_pp_token_info *tokens;
54 unsigned int version;
55 unsigned int tokens_eaten;
56 struct sl_pp_token_info *outtokens;
57 FILE *out;
58 unsigned int i, j;
59 unsigned char *outbytes;
60 unsigned int cboutbytes;
61 unsigned int shader_type;
62
63 if (argc != 4) {
64 usage();
65 return 1;
66 }
67
68 if (!strcmp(argv[1], "fragment")) {
69 shader_type = 1;
70 } else if (!strcmp(argv[1], "vertex")) {
71 shader_type = 2;
72 } else {
73 usage();
74 return 1;
75 }
76
77 in = fopen(argv[2], "rb");
78 if (!in) {
79 printf("Could not open `%s' for read.\n", argv[2]);
80 usage();
81 return 1;
82 }
83
84 fseek(in, 0, SEEK_END);
85 size = ftell(in);
86 fseek(in, 0, SEEK_SET);
87
88 out = fopen(argv[3], "w");
89 if (!out) {
90 fclose(in);
91 printf("Could not open `%s' for write.\n", argv[3]);
92 usage();
93 return 1;
94 }
95
96 inbuf = malloc(size + 1);
97 if (!inbuf) {
98 fprintf(out, "$OOMERROR\n");
99
100 fclose(out);
101 fclose(in);
102 printf("Out of memory.\n");
103 return 0;
104 }
105
106 if (fread(inbuf, 1, size, in) != size) {
107 fprintf(out, "$READERROR\n");
108
109 free(inbuf);
110 fclose(out);
111 fclose(in);
112 printf("Could not read from `%s'.\n", argv[2]);
113 return 0;
114 }
115 inbuf[size] = '\0';
116
117 fclose(in);
118
119 memset(&options, 0, sizeof(options));
120
121 context = sl_pp_context_create();
122 if (!context) {
123 fprintf(out, "$CONTEXERROR\n");
124
125 free(inbuf);
126 fclose(out);
127 printf("Could not create parse context.\n");
128 return 0;
129 }
130
131 if (sl_pp_tokenise(context, inbuf, &options, &tokens)) {
132 fprintf(out, "$ERROR: `%s'\n", sl_pp_context_error_message(context));
133
134 printf("Error: %s.\n", sl_pp_context_error_message(context));
135 sl_pp_context_destroy(context);
136 free(inbuf);
137 fclose(out);
138 return 0;
139 }
140
141 free(inbuf);
142
143 if (sl_pp_version(context, tokens, &version, &tokens_eaten)) {
144 fprintf(out, "$ERROR: `%s'\n", sl_pp_context_error_message(context));
145
146 printf("Error: %s\n", sl_pp_context_error_message(context));
147 sl_pp_context_destroy(context);
148 free(tokens);
149 fclose(out);
150 return 0;
151 }
152
153 if (sl_pp_context_add_extension(context, "ARB_draw_buffers", "GL_ARB_draw_buffers") ||
154 sl_pp_context_add_extension(context, "ARB_texture_rectangle", "GL_ARB_texture_rectangle")) {
155 fprintf(out, "$ERROR: `%s'\n", sl_pp_context_error_message(context));
156
157 printf("Error: %s\n", sl_pp_context_error_message(context));
158 sl_pp_context_destroy(context);
159 free(tokens);
160 fclose(out);
161 return 0;
162 }
163
164 if (sl_pp_process(context, &tokens[tokens_eaten], &outtokens)) {
165 fprintf(out, "$ERROR: `%s'\n", sl_pp_context_error_message(context));
166
167 printf("Error: %s\n", sl_pp_context_error_message(context));
168 sl_pp_context_destroy(context);
169 free(tokens);
170 fclose(out);
171 return 0;
172 }
173
174 free(tokens);
175
176 for (i = j = 0; outtokens[i].token != SL_PP_EOF; i++) {
177 switch (outtokens[i].token) {
178 case SL_PP_NEWLINE:
179 case SL_PP_EXTENSION_REQUIRE:
180 case SL_PP_EXTENSION_ENABLE:
181 case SL_PP_EXTENSION_WARN:
182 case SL_PP_EXTENSION_DISABLE:
183 case SL_PP_LINE:
184 break;
185 default:
186 outtokens[j++] = outtokens[i];
187 }
188 }
189 outtokens[j] = outtokens[i];
190
191 if (sl_cl_compile(context, outtokens, shader_type, 1, &outbytes, &cboutbytes, errmsg, sizeof(errmsg)) == 0) {
192 unsigned int i;
193 unsigned int line = 0;
194
195 fprintf(out, "\n/* DO NOT EDIT - THIS FILE IS AUTOMATICALLY GENERATED FROM THE FOLLOWING FILE: */");
196 fprintf(out, "\n/* %s */", argv[2]);
197 fprintf(out, "\n\n");
198
199 for (i = 0; i < cboutbytes; i++) {
200 unsigned int a;
201
202 if (outbytes[i] < 10) {
203 a = 1;
204 } else if (outbytes[i] < 100) {
205 a = 2;
206 } else {
207 a = 3;
208 }
209 if (i < cboutbytes - 1) {
210 a++;
211 }
212 if (line + a >= 100) {
213 fprintf (out, "\n");
214 line = 0;
215 }
216 line += a;
217 fprintf (out, "%u", outbytes[i]);
218 if (i < cboutbytes - 1) {
219 fprintf (out, ",");
220 }
221 }
222 fprintf (out, "\n");
223 free(outbytes);
224 } else {
225 fprintf(out, "$SYNTAXERROR: `%s'\n", errmsg);
226
227 printf("Error: %s\n", errmsg);
228 }
229
230 sl_pp_context_destroy(context);
231 free(outtokens);
232 fclose(out);
233 return 0;
234 }