linker: Track and validate GLSL versions used in shaders
[mesa.git] / src / glsl / main.cpp
1 /*
2 * Copyright © 2008, 2009 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 */
23 #include <cstdlib>
24 #include <cstdio>
25 #include <getopt.h>
26
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <fcntl.h>
30 #include <unistd.h>
31
32 #include "ast.h"
33 #include "glsl_parser_extras.h"
34 #include "glsl_parser.h"
35 #include "ir_optimization.h"
36 #include "ir_print_visitor.h"
37 #include "program.h"
38
39 extern "C" struct gl_shader *
40 _mesa_new_shader(GLcontext *ctx, GLuint name, GLenum type);
41
42 /* Copied from shader_api.c for the stand-alone compiler.
43 */
44 struct gl_shader *
45 _mesa_new_shader(GLcontext *ctx, GLuint name, GLenum type)
46 {
47 struct gl_shader *shader;
48 assert(type == GL_FRAGMENT_SHADER || type == GL_VERTEX_SHADER);
49 shader = talloc_zero(NULL, struct gl_shader);
50 if (shader) {
51 shader->Type = type;
52 shader->Name = name;
53 shader->RefCount = 1;
54 }
55 return shader;
56 }
57
58 /* Returned string will have 'ctx' as its talloc owner. */
59 static char *
60 load_text_file(void *ctx, const char *file_name)
61 {
62 char *text = NULL;
63 struct stat st;
64 ssize_t total_read = 0;
65 int fd = open(file_name, O_RDONLY);
66
67 if (fd < 0) {
68 return NULL;
69 }
70
71 if (fstat(fd, & st) == 0) {
72 text = (char *) talloc_size(ctx, st.st_size + 1);
73 if (text != NULL) {
74 do {
75 ssize_t bytes = read(fd, text + total_read,
76 st.st_size - total_read);
77 if (bytes < 0) {
78 free(text);
79 text = NULL;
80 break;
81 }
82
83 if (bytes == 0) {
84 break;
85 }
86
87 total_read += bytes;
88 } while (total_read < st.st_size);
89
90 text[total_read] = '\0';
91 }
92 }
93
94 close(fd);
95
96 return text;
97 }
98
99
100 void
101 usage_fail(const char *name)
102 {
103 printf("%s <filename.frag|filename.vert>\n", name);
104 exit(EXIT_FAILURE);
105 }
106
107
108 int dump_ast = 0;
109 int dump_hir = 0;
110 int dump_lir = 0;
111 int do_link = 0;
112
113 const struct option compiler_opts[] = {
114 { "dump-ast", 0, &dump_ast, 1 },
115 { "dump-hir", 0, &dump_hir, 1 },
116 { "dump-lir", 0, &dump_lir, 1 },
117 { "link", 0, &do_link, 1 },
118 { NULL, 0, NULL, 0 }
119 };
120
121 static void
122 steal_memory(ir_instruction *ir, void *new_ctx)
123 {
124 talloc_steal(new_ctx, ir);
125 }
126
127 void
128 compile_shader(struct gl_shader *shader)
129 {
130 struct _mesa_glsl_parse_state *state;
131 struct gl_extensions ext;
132
133 state = talloc_zero(talloc_parent(shader), struct _mesa_glsl_parse_state);
134
135 switch (shader->Type) {
136 case GL_VERTEX_SHADER: state->target = vertex_shader; break;
137 case GL_FRAGMENT_SHADER: state->target = fragment_shader; break;
138 case GL_GEOMETRY_SHADER: state->target = geometry_shader; break;
139 }
140
141 state->scanner = NULL;
142 state->translation_unit.make_empty();
143 state->symbols = new(shader) glsl_symbol_table;
144 state->info_log = talloc_strdup(shader, "");
145 state->error = false;
146 state->loop_or_switch_nesting = NULL;
147 state->ARB_texture_rectangle_enable = true;
148
149 memset(&ext, 0, sizeof(ext));
150 state->extensions = &ext;
151 state->Const.MaxDrawBuffers = 2;
152 state->Const.MaxTextureCoords = 4;
153
154 const char *source = shader->Source;
155 state->error = preprocess(state, &source, &state->info_log, &ext);
156
157 if (!state->error) {
158 _mesa_glsl_lexer_ctor(state, source);
159 _mesa_glsl_parse(state);
160 _mesa_glsl_lexer_dtor(state);
161 }
162
163 if (dump_ast) {
164 foreach_list_const(n, &state->translation_unit) {
165 ast_node *ast = exec_node_data(ast_node, n, link);
166 ast->print();
167 }
168 printf("\n\n");
169 }
170
171 shader->ir = new(shader) exec_list;
172 if (!state->error && !state->translation_unit.is_empty())
173 _mesa_ast_to_hir(shader->ir, state);
174
175 validate_ir_tree(shader->ir);
176
177 /* Print out the unoptimized IR. */
178 if (!state->error && dump_hir) {
179 _mesa_print_ir(shader->ir, state);
180 }
181
182 /* Optimization passes */
183 if (!state->error && !shader->ir->is_empty()) {
184 bool progress;
185 do {
186 progress = false;
187
188 progress = do_function_inlining(shader->ir) || progress;
189 progress = do_if_simplification(shader->ir) || progress;
190 progress = do_copy_propagation(shader->ir) || progress;
191 progress = do_dead_code_local(shader->ir) || progress;
192 progress = do_dead_code_unlinked(state, shader->ir) || progress;
193 progress = do_constant_variable_unlinked(shader->ir) || progress;
194 progress = do_constant_folding(shader->ir) || progress;
195 progress = do_vec_index_to_swizzle(shader->ir) || progress;
196 progress = do_vec_index_to_cond_assign(shader->ir) || progress;
197 progress = do_swizzle_swizzle(shader->ir) || progress;
198 } while (progress);
199 }
200
201 validate_ir_tree(shader->ir);
202
203 /* Print out the resulting IR */
204 if (!state->error && dump_lir) {
205 _mesa_print_ir(shader->ir, state);
206 }
207
208 shader->symbols = state->symbols;
209 shader->CompileStatus = !state->error;
210 shader->Version = state->language_version;
211
212 if (shader->InfoLog)
213 talloc_free(shader->InfoLog);
214
215 shader->InfoLog = state->info_log;
216
217 /* Retain any live IR, but trash the rest. */
218 foreach_list(node, shader->ir) {
219 visit_tree((ir_instruction *) node, steal_memory, shader);
220 }
221
222 talloc_free(state);
223
224 return;
225 }
226
227 int
228 main(int argc, char **argv)
229 {
230 int status = EXIT_SUCCESS;
231
232 int c;
233 int idx = 0;
234 while ((c = getopt_long(argc, argv, "", compiler_opts, &idx)) != -1)
235 /* empty */ ;
236
237
238 if (argc <= optind)
239 usage_fail(argv[0]);
240
241 struct gl_shader_program *whole_program;
242
243 whole_program = talloc_zero (NULL, struct gl_shader_program);
244 assert(whole_program != NULL);
245
246 for (/* empty */; argc > optind; optind++) {
247 whole_program->Shaders = (struct gl_shader **)
248 talloc_realloc(whole_program, whole_program->Shaders,
249 struct gl_shader *, whole_program->NumShaders + 1);
250 assert(whole_program->Shaders != NULL);
251
252 struct gl_shader *shader = talloc_zero(whole_program, gl_shader);
253
254 whole_program->Shaders[whole_program->NumShaders] = shader;
255 whole_program->NumShaders++;
256
257 const unsigned len = strlen(argv[optind]);
258 if (len < 6)
259 usage_fail(argv[0]);
260
261 const char *const ext = & argv[optind][len - 5];
262 if (strncmp(".vert", ext, 5) == 0)
263 shader->Type = GL_VERTEX_SHADER;
264 else if (strncmp(".geom", ext, 5) == 0)
265 shader->Type = GL_GEOMETRY_SHADER;
266 else if (strncmp(".frag", ext, 5) == 0)
267 shader->Type = GL_FRAGMENT_SHADER;
268 else
269 usage_fail(argv[0]);
270
271 shader->Source = load_text_file(whole_program, argv[optind]);
272 if (shader->Source == NULL) {
273 printf("File \"%s\" does not exist.\n", argv[optind]);
274 exit(EXIT_FAILURE);
275 }
276
277 compile_shader(shader);
278
279 if (!shader->CompileStatus) {
280 printf("Info log for %s:\n%s\n", argv[optind], shader->InfoLog);
281 status = EXIT_FAILURE;
282 break;
283 }
284 }
285
286 if ((status == EXIT_SUCCESS) && do_link) {
287 link_shaders(whole_program);
288 status = (whole_program->LinkStatus) ? EXIT_SUCCESS : EXIT_FAILURE;
289
290 if (strlen(whole_program->InfoLog) > 0)
291 printf("Info log for linking:\n%s\n", whole_program->InfoLog);
292 }
293
294 for (unsigned i = 0; i < whole_program->_NumLinkedShaders; i++)
295 talloc_free(whole_program->_LinkedShaders[i]);
296
297 talloc_free(whole_program);
298 _mesa_glsl_release_types();
299
300 return status;
301 }