glsl: Refactor variable declaration handling.
[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
49 (void) ctx;
50
51 assert(type == GL_FRAGMENT_SHADER || type == GL_VERTEX_SHADER);
52 shader = talloc_zero(NULL, struct gl_shader);
53 if (shader) {
54 shader->Type = type;
55 shader->Name = name;
56 shader->RefCount = 1;
57 }
58 return shader;
59 }
60
61 /* Returned string will have 'ctx' as its talloc owner. */
62 static char *
63 load_text_file(void *ctx, const char *file_name)
64 {
65 char *text = NULL;
66 struct stat st;
67 ssize_t total_read = 0;
68 int fd = open(file_name, O_RDONLY);
69
70 if (fd < 0) {
71 return NULL;
72 }
73
74 if (fstat(fd, & st) == 0) {
75 text = (char *) talloc_size(ctx, st.st_size + 1);
76 if (text != NULL) {
77 do {
78 ssize_t bytes = read(fd, text + total_read,
79 st.st_size - total_read);
80 if (bytes < 0) {
81 free(text);
82 text = NULL;
83 break;
84 }
85
86 if (bytes == 0) {
87 break;
88 }
89
90 total_read += bytes;
91 } while (total_read < st.st_size);
92
93 text[total_read] = '\0';
94 }
95 }
96
97 close(fd);
98
99 return text;
100 }
101
102
103 void
104 usage_fail(const char *name)
105 {
106 printf("%s <filename.frag|filename.vert>\n", name);
107 exit(EXIT_FAILURE);
108 }
109
110
111 int dump_ast = 0;
112 int dump_hir = 0;
113 int dump_lir = 0;
114 int do_link = 0;
115
116 const struct option compiler_opts[] = {
117 { "dump-ast", 0, &dump_ast, 1 },
118 { "dump-hir", 0, &dump_hir, 1 },
119 { "dump-lir", 0, &dump_lir, 1 },
120 { "link", 0, &do_link, 1 },
121 { NULL, 0, NULL, 0 }
122 };
123
124 void
125 compile_shader(struct gl_shader *shader)
126 {
127 struct _mesa_glsl_parse_state *state =
128 new(shader) _mesa_glsl_parse_state(NULL, shader->Type, shader);
129
130 const char *source = shader->Source;
131 state->error = preprocess(state, &source, &state->info_log,
132 state->extensions);
133
134 if (!state->error) {
135 _mesa_glsl_lexer_ctor(state, source);
136 _mesa_glsl_parse(state);
137 _mesa_glsl_lexer_dtor(state);
138 }
139
140 if (dump_ast) {
141 foreach_list_const(n, &state->translation_unit) {
142 ast_node *ast = exec_node_data(ast_node, n, link);
143 ast->print();
144 }
145 printf("\n\n");
146 }
147
148 shader->ir = new(shader) exec_list;
149 if (!state->error && !state->translation_unit.is_empty())
150 _mesa_ast_to_hir(shader->ir, state);
151
152 /* Print out the unoptimized IR. */
153 if (!state->error && dump_hir) {
154 validate_ir_tree(shader->ir);
155 _mesa_print_ir(shader->ir, state);
156 }
157
158 /* Optimization passes */
159 if (!state->error && !shader->ir->is_empty()) {
160 bool progress;
161 do {
162 progress = false;
163
164 progress = do_function_inlining(shader->ir) || progress;
165 progress = do_if_simplification(shader->ir) || progress;
166 progress = do_copy_propagation(shader->ir) || progress;
167 progress = do_dead_code_local(shader->ir) || progress;
168 progress = do_dead_code_unlinked(shader->ir) || progress;
169 progress = do_tree_grafting(shader->ir) || progress;
170 progress = do_constant_propagation(shader->ir) || progress;
171 progress = do_constant_variable_unlinked(shader->ir) || progress;
172 progress = do_constant_folding(shader->ir) || progress;
173 progress = do_algebraic(shader->ir) || progress;
174 progress = do_vec_index_to_swizzle(shader->ir) || progress;
175 progress = do_vec_index_to_cond_assign(shader->ir) || progress;
176 progress = do_swizzle_swizzle(shader->ir) || progress;
177 } while (progress);
178
179 validate_ir_tree(shader->ir);
180 }
181
182
183 /* Print out the resulting IR */
184 if (!state->error && dump_lir) {
185 _mesa_print_ir(shader->ir, state);
186 }
187
188 shader->symbols = state->symbols;
189 shader->CompileStatus = !state->error;
190 shader->Version = state->language_version;
191 memcpy(shader->builtins_to_link, state->builtins_to_link,
192 sizeof(shader->builtins_to_link[0]) * state->num_builtins_to_link);
193 shader->num_builtins_to_link = state->num_builtins_to_link;
194
195 if (shader->InfoLog)
196 talloc_free(shader->InfoLog);
197
198 shader->InfoLog = state->info_log;
199
200 /* Retain any live IR, but trash the rest. */
201 reparent_ir(shader->ir, shader);
202
203 talloc_free(state);
204
205 return;
206 }
207
208 int
209 main(int argc, char **argv)
210 {
211 int status = EXIT_SUCCESS;
212 GLcontext local_ctx;
213 GLcontext *ctx = &local_ctx;
214
215 ctx->Driver.NewShader = _mesa_new_shader;
216
217 int c;
218 int idx = 0;
219 while ((c = getopt_long(argc, argv, "", compiler_opts, &idx)) != -1)
220 /* empty */ ;
221
222
223 if (argc <= optind)
224 usage_fail(argv[0]);
225
226 struct gl_shader_program *whole_program;
227
228 whole_program = talloc_zero (NULL, struct gl_shader_program);
229 assert(whole_program != NULL);
230
231 for (/* empty */; argc > optind; optind++) {
232 whole_program->Shaders = (struct gl_shader **)
233 talloc_realloc(whole_program, whole_program->Shaders,
234 struct gl_shader *, whole_program->NumShaders + 1);
235 assert(whole_program->Shaders != NULL);
236
237 struct gl_shader *shader = talloc_zero(whole_program, gl_shader);
238
239 whole_program->Shaders[whole_program->NumShaders] = shader;
240 whole_program->NumShaders++;
241
242 const unsigned len = strlen(argv[optind]);
243 if (len < 6)
244 usage_fail(argv[0]);
245
246 const char *const ext = & argv[optind][len - 5];
247 if (strncmp(".vert", ext, 5) == 0)
248 shader->Type = GL_VERTEX_SHADER;
249 else if (strncmp(".geom", ext, 5) == 0)
250 shader->Type = GL_GEOMETRY_SHADER;
251 else if (strncmp(".frag", ext, 5) == 0)
252 shader->Type = GL_FRAGMENT_SHADER;
253 else
254 usage_fail(argv[0]);
255
256 shader->Source = load_text_file(whole_program, argv[optind]);
257 if (shader->Source == NULL) {
258 printf("File \"%s\" does not exist.\n", argv[optind]);
259 exit(EXIT_FAILURE);
260 }
261
262 compile_shader(shader);
263
264 if (!shader->CompileStatus) {
265 printf("Info log for %s:\n%s\n", argv[optind], shader->InfoLog);
266 status = EXIT_FAILURE;
267 break;
268 }
269 }
270
271 if ((status == EXIT_SUCCESS) && do_link) {
272 link_shaders(ctx, whole_program);
273 status = (whole_program->LinkStatus) ? EXIT_SUCCESS : EXIT_FAILURE;
274
275 if (strlen(whole_program->InfoLog) > 0)
276 printf("Info log for linking:\n%s\n", whole_program->InfoLog);
277 }
278
279 for (unsigned i = 0; i < whole_program->_NumLinkedShaders; i++)
280 talloc_free(whole_program->_LinkedShaders[i]);
281
282 talloc_free(whole_program);
283 _mesa_glsl_release_types();
284 _mesa_glsl_release_functions();
285
286 return status;
287 }