Merge branch 'gallium-dynamicstencilref'
[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 unsigned int version;
54 FILE *out;
55 unsigned char *outbytes;
56 unsigned int cboutbytes;
57 unsigned int shader_type;
58
59 if (argc != 4) {
60 usage();
61 return 1;
62 }
63
64 if (!strcmp(argv[1], "fragment")) {
65 shader_type = 1;
66 } else if (!strcmp(argv[1], "vertex")) {
67 shader_type = 2;
68 } else {
69 usage();
70 return 1;
71 }
72
73 in = fopen(argv[2], "rb");
74 if (!in) {
75 printf("Could not open `%s' for read.\n", argv[2]);
76 usage();
77 return 1;
78 }
79
80 fseek(in, 0, SEEK_END);
81 size = ftell(in);
82 fseek(in, 0, SEEK_SET);
83
84 out = fopen(argv[3], "w");
85 if (!out) {
86 fclose(in);
87 printf("Could not open `%s' for write.\n", argv[3]);
88 usage();
89 return 1;
90 }
91
92 inbuf = malloc(size + 1);
93 if (!inbuf) {
94 fprintf(out, "$OOMERROR\n");
95
96 fclose(out);
97 fclose(in);
98 printf("Out of memory.\n");
99 return 0;
100 }
101
102 if (fread(inbuf, 1, size, in) != size) {
103 fprintf(out, "$READERROR\n");
104
105 free(inbuf);
106 fclose(out);
107 fclose(in);
108 printf("Could not read from `%s'.\n", argv[2]);
109 return 0;
110 }
111 inbuf[size] = '\0';
112
113 fclose(in);
114
115 memset(&options, 0, sizeof(options));
116
117 context = sl_pp_context_create(inbuf, &options);
118 if (!context) {
119 fprintf(out, "$CONTEXERROR\n");
120
121 free(inbuf);
122 fclose(out);
123 printf("Could not create parse context.\n");
124 return 0;
125 }
126
127 if (sl_pp_version(context, &version)) {
128 fprintf(out, "$ERROR: `%s'\n", sl_pp_context_error_message(context));
129
130 printf("Error: %s\n", sl_pp_context_error_message(context));
131 sl_pp_context_destroy(context);
132 free(inbuf);
133 fclose(out);
134 return 0;
135 }
136
137 if (sl_pp_context_add_extension(context, "GL_ARB_draw_buffers") ||
138 sl_pp_context_add_extension(context, "GL_ARB_texture_rectangle")) {
139 fprintf(out, "$ERROR: `%s'\n", sl_pp_context_error_message(context));
140
141 printf("Error: %s\n", sl_pp_context_error_message(context));
142 sl_pp_context_destroy(context);
143 free(inbuf);
144 fclose(out);
145 return 0;
146 }
147
148 if (sl_cl_compile(context, shader_type, 1, &outbytes, &cboutbytes, errmsg, sizeof(errmsg)) == 0) {
149 unsigned int i;
150 unsigned int line = 0;
151
152 fprintf(out, "\n/* DO NOT EDIT - THIS FILE IS AUTOMATICALLY GENERATED FROM THE FOLLOWING FILE: */");
153 fprintf(out, "\n/* %s */", argv[2]);
154 fprintf(out, "\n\n");
155
156 for (i = 0; i < cboutbytes; i++) {
157 unsigned int a;
158
159 if (outbytes[i] < 10) {
160 a = 1;
161 } else if (outbytes[i] < 100) {
162 a = 2;
163 } else {
164 a = 3;
165 }
166 if (i < cboutbytes - 1) {
167 a++;
168 }
169 if (line + a >= 100) {
170 fprintf (out, "\n");
171 line = 0;
172 }
173 line += a;
174 fprintf (out, "%u", outbytes[i]);
175 if (i < cboutbytes - 1) {
176 fprintf (out, ",");
177 }
178 }
179 fprintf (out, "\n");
180 free(outbytes);
181 } else {
182 fprintf(out, "$SYNTAXERROR: `%s'\n", errmsg);
183
184 printf("Error: %s\n", errmsg);
185 }
186
187 sl_pp_context_destroy(context);
188 free(inbuf);
189 fclose(out);
190 return 0;
191 }