16b2cf84c53452cedb6e935540dbd04307943bd0
[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 /* Returned string will have 'ctx' as its talloc owner. */
40 static char *
41 load_text_file(void *ctx, const char *file_name, size_t *size)
42 {
43 char *text = NULL;
44 struct stat st;
45 ssize_t total_read = 0;
46 int fd = open(file_name, O_RDONLY);
47
48 *size = 0;
49 if (fd < 0) {
50 return NULL;
51 }
52
53 if (fstat(fd, & st) == 0) {
54 text = (char *) talloc_size(ctx, st.st_size + 1);
55 if (text != NULL) {
56 do {
57 ssize_t bytes = read(fd, text + total_read,
58 st.st_size - total_read);
59 if (bytes < 0) {
60 free(text);
61 text = NULL;
62 break;
63 }
64
65 if (bytes == 0) {
66 break;
67 }
68
69 total_read += bytes;
70 } while (total_read < st.st_size);
71
72 text[total_read] = '\0';
73 *size = total_read;
74 }
75 }
76
77 close(fd);
78
79 return text;
80 }
81
82
83 void
84 usage_fail(const char *name)
85 {
86 printf("%s <filename.frag|filename.vert>\n", name);
87 exit(EXIT_FAILURE);
88 }
89
90
91 int dump_ast = 0;
92 int dump_hir = 0;
93 int dump_lir = 0;
94 int do_link = 0;
95
96 const struct option compiler_opts[] = {
97 { "dump-ast", 0, &dump_ast, 1 },
98 { "dump-hir", 0, &dump_hir, 1 },
99 { "dump-lir", 0, &dump_lir, 1 },
100 { "link", 0, &do_link, 1 },
101 { NULL, 0, NULL, 0 }
102 };
103
104 void
105 compile_shader(struct glsl_shader *shader)
106 {
107 struct _mesa_glsl_parse_state *state;
108
109 state = talloc_zero(talloc_parent(shader), struct _mesa_glsl_parse_state);
110
111 switch (shader->Type) {
112 case GL_VERTEX_SHADER: state->target = vertex_shader; break;
113 case GL_FRAGMENT_SHADER: state->target = fragment_shader; break;
114 case GL_GEOMETRY_SHADER: state->target = geometry_shader; break;
115 }
116
117 state->scanner = NULL;
118 state->translation_unit.make_empty();
119 state->symbols = new(shader) glsl_symbol_table;
120 state->info_log = talloc_strdup(shader, "");
121 state->error = false;
122 state->temp_index = 0;
123 state->loop_or_switch_nesting = NULL;
124 state->ARB_texture_rectangle_enable = true;
125
126 /* Create a new context for the preprocessor output. Ultimately, this
127 * should probably be the parser context, but there isn't one yet.
128 */
129 const char *source = shader->Source;
130 state->error = preprocess(shader, &source, &state->info_log);
131
132 if (!state->error) {
133 _mesa_glsl_lexer_ctor(state, source);
134 _mesa_glsl_parse(state);
135 _mesa_glsl_lexer_dtor(state);
136 }
137
138 if (dump_ast) {
139 foreach_list_const(n, &state->translation_unit) {
140 ast_node *ast = exec_node_data(ast_node, n, link);
141 ast->print();
142 }
143 printf("\n\n");
144 }
145
146 shader->ir.make_empty();
147 if (!state->error && !state->translation_unit.is_empty())
148 _mesa_ast_to_hir(&shader->ir, state);
149
150 validate_ir_tree(&shader->ir);
151
152 /* Print out the unoptimized IR. */
153 if (!state->error && dump_hir) {
154 _mesa_print_ir(&shader->ir, state);
155 }
156
157 /* Optimization passes */
158 if (!state->error && !shader->ir.is_empty()) {
159 bool progress;
160 do {
161 progress = false;
162
163 progress = do_function_inlining(&shader->ir) || progress;
164 progress = do_if_simplification(&shader->ir) || progress;
165 progress = do_copy_propagation(&shader->ir) || progress;
166 progress = do_dead_code_local(&shader->ir) || progress;
167 progress = do_dead_code_unlinked(state, &shader->ir) || progress;
168 progress = do_constant_variable_unlinked(&shader->ir) || progress;
169 progress = do_constant_folding(&shader->ir) || progress;
170 progress = do_vec_index_to_swizzle(&shader->ir) || progress;
171 progress = do_swizzle_swizzle(&shader->ir) || progress;
172 } while (progress);
173 }
174
175 validate_ir_tree(&shader->ir);
176
177 /* Print out the resulting IR */
178 if (!state->error && dump_lir) {
179 _mesa_print_ir(&shader->ir, state);
180 }
181
182 shader->symbols = state->symbols;
183 shader->CompileStatus = !state->error;
184
185 if (shader->InfoLog)
186 talloc_free(shader->InfoLog);
187
188 shader->InfoLog = state->info_log;
189
190 talloc_free(state);
191
192 return;
193 }
194
195 int
196 main(int argc, char **argv)
197 {
198 int status = EXIT_SUCCESS;
199
200 int c;
201 int idx = 0;
202 while ((c = getopt_long(argc, argv, "", compiler_opts, &idx)) != -1)
203 /* empty */ ;
204
205
206 if (argc <= optind)
207 usage_fail(argv[0]);
208
209 struct glsl_program *whole_program;
210
211 whole_program = talloc_zero (NULL, struct glsl_program);
212 assert(whole_program != NULL);
213
214 for (/* empty */; argc > optind; optind++) {
215 whole_program->Shaders = (struct glsl_shader **)
216 talloc_realloc(whole_program, whole_program->Shaders,
217 struct glsl_shader *, whole_program->NumShaders + 1);
218 assert(whole_program->Shaders != NULL);
219
220 struct glsl_shader *shader = talloc_zero(whole_program, glsl_shader);
221
222 whole_program->Shaders[whole_program->NumShaders] = shader;
223 whole_program->NumShaders++;
224
225 const unsigned len = strlen(argv[optind]);
226 if (len < 6)
227 usage_fail(argv[0]);
228
229 const char *const ext = & argv[optind][len - 5];
230 if (strncmp(".vert", ext, 5) == 0)
231 shader->Type = GL_VERTEX_SHADER;
232 else if (strncmp(".geom", ext, 5) == 0)
233 shader->Type = GL_GEOMETRY_SHADER;
234 else if (strncmp(".frag", ext, 5) == 0)
235 shader->Type = GL_FRAGMENT_SHADER;
236 else
237 usage_fail(argv[0]);
238
239 shader->Source = load_text_file(whole_program,
240 argv[optind], &shader->SourceLen);
241 if (shader->Source == NULL) {
242 printf("File \"%s\" does not exist.\n", argv[optind]);
243 exit(EXIT_FAILURE);
244 }
245
246 compile_shader(shader);
247
248 if (!shader->CompileStatus) {
249 printf("Info log for %s:\n%s\n", argv[optind], shader->InfoLog);
250 status = EXIT_FAILURE;
251 break;
252 }
253 }
254
255 if ((status == EXIT_SUCCESS) && do_link) {
256 link_shaders(whole_program);
257 status = (whole_program->LinkStatus) ? EXIT_SUCCESS : EXIT_FAILURE;
258 }
259
260 talloc_free(whole_program);
261
262 return status;
263 }