blob: b32e2ad3dbc6c3cb9b91e7595a2fca34f3c38dd9 [file] [log] [blame]
Ian Romanick61d4aa02010-06-14 14:46:09 -07001/*
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>
Ian Romanick7babbdb2010-06-15 12:47:07 -070025#include <getopt.h>
Ian Romanick61d4aa02010-06-14 14:46:09 -070026
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"
Ian Romanick8ce55db2010-06-17 12:01:18 -070037#include "program.h"
Ian Romanick61d4aa02010-06-14 14:46:09 -070038
Carl Wortha9696e72010-06-18 17:37:02 -070039/* Returned string will have 'ctx' as its talloc owner. */
Ian Romanick61d4aa02010-06-14 14:46:09 -070040static char *
Carl Wortha9696e72010-06-18 17:37:02 -070041load_text_file(void *ctx, const char *file_name, size_t *size)
Ian Romanick61d4aa02010-06-14 14:46:09 -070042{
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) {
Carl Wortha9696e72010-06-18 17:37:02 -070054 text = (char *) talloc_size(ctx, st.st_size + 1);
Ian Romanick61d4aa02010-06-14 14:46:09 -070055 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
Ian Romanick2b368952010-06-15 12:00:37 -070083void
84usage_fail(const char *name)
85{
86 printf("%s <filename.frag|filename.vert>\n", name);
87 exit(EXIT_FAILURE);
88}
89
90
Ian Romanick7babbdb2010-06-15 12:47:07 -070091int dump_ast = 0;
Ian Romanick81e17472010-06-15 12:51:38 -070092int dump_lir = 0;
Ian Romanickc648a122010-06-17 19:51:48 -070093int do_link = 0;
Ian Romanick7babbdb2010-06-15 12:47:07 -070094
95const struct option compiler_opts[] = {
96 { "dump-ast", 0, &dump_ast, 1 },
Ian Romanick81e17472010-06-15 12:51:38 -070097 { "dump-lir", 0, &dump_lir, 1 },
Ian Romanickc648a122010-06-17 19:51:48 -070098 { "link", 0, &do_link, 1 },
Ian Romanick7babbdb2010-06-15 12:47:07 -070099 { NULL, 0, NULL, 0 }
100};
101
Ian Romanick8ce55db2010-06-17 12:01:18 -0700102void
Kenneth Graunke29e60872010-06-17 15:28:34 -0700103compile_shader(struct glsl_shader *shader)
Ian Romanick61d4aa02010-06-14 14:46:09 -0700104{
Carl Worth2d2561e2010-06-23 15:43:38 -0700105 struct _mesa_glsl_parse_state *state;
Ian Romanick61d4aa02010-06-14 14:46:09 -0700106
Carl Worth2d2561e2010-06-23 15:43:38 -0700107 state = talloc_zero(talloc_parent(shader), struct _mesa_glsl_parse_state);
108
Kenneth Graunke29e60872010-06-17 15:28:34 -0700109 switch (shader->Type) {
Carl Worth2d2561e2010-06-23 15:43:38 -0700110 case GL_VERTEX_SHADER: state->target = vertex_shader; break;
111 case GL_FRAGMENT_SHADER: state->target = fragment_shader; break;
112 case GL_GEOMETRY_SHADER: state->target = geometry_shader; break;
Ian Romanick8ce55db2010-06-17 12:01:18 -0700113 }
114
Carl Worth2d2561e2010-06-23 15:43:38 -0700115 state->scanner = NULL;
116 state->translation_unit.make_empty();
Carl Worthf961e442010-06-23 15:47:04 -0700117 state->symbols = new(shader) glsl_symbol_table;
Carl Worth2d2561e2010-06-23 15:43:38 -0700118 state->info_log = talloc_strdup(shader, "");
119 state->error = false;
120 state->temp_index = 0;
121 state->loop_or_switch_nesting = NULL;
122 state->ARB_texture_rectangle_enable = true;
Ian Romanick61d4aa02010-06-14 14:46:09 -0700123
Kenneth Graunke04ba86a2010-06-16 12:18:00 -0700124 /* Create a new context for the preprocessor output. Ultimately, this
125 * should probably be the parser context, but there isn't one yet.
126 */
127 const char *source = shader->Source;
Carl Worth2d2561e2010-06-23 15:43:38 -0700128 state->error = preprocess(shader, &source, &state->info_log);
Kenneth Graunke04ba86a2010-06-16 12:18:00 -0700129
Carl Worth2d2561e2010-06-23 15:43:38 -0700130 if (!state->error) {
131 _mesa_glsl_lexer_ctor(state, source);
132 _mesa_glsl_parse(state);
133 _mesa_glsl_lexer_dtor(state);
Kenneth Graunke04ba86a2010-06-16 12:18:00 -0700134 }
Ian Romanick61d4aa02010-06-14 14:46:09 -0700135
Ian Romanick7babbdb2010-06-15 12:47:07 -0700136 if (dump_ast) {
Carl Worth2d2561e2010-06-23 15:43:38 -0700137 foreach_list_const(n, &state->translation_unit) {
Ian Romanick7babbdb2010-06-15 12:47:07 -0700138 ast_node *ast = exec_node_data(ast_node, n, link);
139 ast->print();
140 }
141 printf("\n\n");
Ian Romanick61d4aa02010-06-14 14:46:09 -0700142 }
143
Kenneth Graunke29e60872010-06-17 15:28:34 -0700144 shader->ir.make_empty();
Carl Worth2d2561e2010-06-23 15:43:38 -0700145 if (!state->error && !state->translation_unit.is_empty())
146 _mesa_ast_to_hir(&shader->ir, state);
Ian Romanick54992c32010-06-14 14:47:26 -0700147
Eric Anholt53cdb7e2010-06-22 12:07:21 -0700148 validate_ir_tree(&shader->ir);
149
Ian Romanick61d4aa02010-06-14 14:46:09 -0700150 /* Optimization passes */
Carl Worth2d2561e2010-06-23 15:43:38 -0700151 if (!state->error && !shader->ir.is_empty()) {
Ian Romanick61d4aa02010-06-14 14:46:09 -0700152 bool progress;
153 do {
154 progress = false;
155
Kenneth Graunke29e60872010-06-17 15:28:34 -0700156 progress = do_function_inlining(&shader->ir) || progress;
157 progress = do_if_simplification(&shader->ir) || progress;
158 progress = do_copy_propagation(&shader->ir) || progress;
159 progress = do_dead_code_local(&shader->ir) || progress;
Eric Anholtbda27422010-06-25 13:38:38 -0700160 progress = do_dead_code_unlinked(state, &shader->ir) || progress;
Kenneth Graunke29e60872010-06-17 15:28:34 -0700161 progress = do_constant_variable_unlinked(&shader->ir) || progress;
162 progress = do_constant_folding(&shader->ir) || progress;
163 progress = do_vec_index_to_swizzle(&shader->ir) || progress;
164 progress = do_swizzle_swizzle(&shader->ir) || progress;
Ian Romanick61d4aa02010-06-14 14:46:09 -0700165 } while (progress);
166 }
167
Eric Anholt53cdb7e2010-06-22 12:07:21 -0700168 validate_ir_tree(&shader->ir);
169
Ian Romanick61d4aa02010-06-14 14:46:09 -0700170 /* Print out the resulting IR */
Carl Worth2d2561e2010-06-23 15:43:38 -0700171 if (!state->error && dump_lir) {
172 _mesa_print_ir(&shader->ir, state);
Ian Romanick61d4aa02010-06-14 14:46:09 -0700173 }
174
Carl Worth2d2561e2010-06-23 15:43:38 -0700175 shader->symbols = state->symbols;
176 shader->CompileStatus = !state->error;
Ian Romanickca97bd32010-06-19 01:39:14 -0700177
178 if (shader->InfoLog)
179 talloc_free(shader->InfoLog);
180
Carl Worth2d2561e2010-06-23 15:43:38 -0700181 shader->InfoLog = state->info_log;
182
183 talloc_free(state);
Ian Romanickca97bd32010-06-19 01:39:14 -0700184
Ian Romanick8ce55db2010-06-17 12:01:18 -0700185 return;
186}
Ian Romanick61d4aa02010-06-14 14:46:09 -0700187
Ian Romanick8ce55db2010-06-17 12:01:18 -0700188int
189main(int argc, char **argv)
190{
Ian Romanick6fd9fb22010-06-17 12:22:16 -0700191 int status = EXIT_SUCCESS;
Ian Romanick8ce55db2010-06-17 12:01:18 -0700192
193 int c;
194 int idx = 0;
195 while ((c = getopt_long(argc, argv, "", compiler_opts, &idx)) != -1)
196 /* empty */ ;
197
198
199 if (argc <= optind)
200 usage_fail(argv[0]);
201
Carl Worthbe83eb82010-06-23 13:34:05 -0700202 struct glsl_program *whole_program;
203
204 whole_program = talloc_zero (NULL, struct glsl_program);
205 assert(whole_program != NULL);
Ian Romanick8ce55db2010-06-17 12:01:18 -0700206
Ian Romanick6fd9fb22010-06-17 12:22:16 -0700207 for (/* empty */; argc > optind; optind++) {
Carl Worthbe83eb82010-06-23 13:34:05 -0700208 whole_program->Shaders = (struct glsl_shader **)
Carl Worth26bbfb72010-06-23 19:09:56 -0700209 talloc_realloc(whole_program, whole_program->Shaders,
210 struct glsl_shader *, whole_program->NumShaders + 1);
Carl Worthbe83eb82010-06-23 13:34:05 -0700211 assert(whole_program->Shaders != NULL);
Ian Romanick8ce55db2010-06-17 12:01:18 -0700212
Carl Worthbe83eb82010-06-23 13:34:05 -0700213 struct glsl_shader *shader = talloc_zero(whole_program, glsl_shader);
Ian Romanick8ce55db2010-06-17 12:01:18 -0700214
Carl Worthbe83eb82010-06-23 13:34:05 -0700215 whole_program->Shaders[whole_program->NumShaders] = shader;
216 whole_program->NumShaders++;
Ian Romanick8ce55db2010-06-17 12:01:18 -0700217
Ian Romanick6fd9fb22010-06-17 12:22:16 -0700218 const unsigned len = strlen(argv[optind]);
219 if (len < 6)
220 usage_fail(argv[0]);
Ian Romanick8ce55db2010-06-17 12:01:18 -0700221
Ian Romanick6fd9fb22010-06-17 12:22:16 -0700222 const char *const ext = & argv[optind][len - 5];
223 if (strncmp(".vert", ext, 5) == 0)
Kenneth Graunke29e60872010-06-17 15:28:34 -0700224 shader->Type = GL_VERTEX_SHADER;
Ian Romanick6fd9fb22010-06-17 12:22:16 -0700225 else if (strncmp(".geom", ext, 5) == 0)
Kenneth Graunke29e60872010-06-17 15:28:34 -0700226 shader->Type = GL_GEOMETRY_SHADER;
Ian Romanick6fd9fb22010-06-17 12:22:16 -0700227 else if (strncmp(".frag", ext, 5) == 0)
Kenneth Graunke29e60872010-06-17 15:28:34 -0700228 shader->Type = GL_FRAGMENT_SHADER;
Ian Romanick6fd9fb22010-06-17 12:22:16 -0700229 else
230 usage_fail(argv[0]);
231
Carl Wortha9696e72010-06-18 17:37:02 -0700232 shader->Source = load_text_file(whole_program,
233 argv[optind], &shader->SourceLen);
Kenneth Graunke2848c4c2010-06-16 12:10:55 -0700234 if (shader->Source == NULL) {
235 printf("File \"%s\" does not exist.\n", argv[optind]);
236 exit(EXIT_FAILURE);
237 }
Ian Romanick6fd9fb22010-06-17 12:22:16 -0700238
Kenneth Graunke29e60872010-06-17 15:28:34 -0700239 compile_shader(shader);
Ian Romanick6fd9fb22010-06-17 12:22:16 -0700240
Kenneth Graunke29e60872010-06-17 15:28:34 -0700241 if (!shader->CompileStatus) {
Kenneth Graunkef3eb42d2010-06-19 11:31:01 -0700242 printf("Info log for %s:\n%s\n", argv[optind], shader->InfoLog);
Ian Romanick6fd9fb22010-06-17 12:22:16 -0700243 status = EXIT_FAILURE;
244 break;
245 }
246 }
247
Ian Romanickc648a122010-06-17 19:51:48 -0700248 if ((status == EXIT_SUCCESS) && do_link) {
Carl Worthbe83eb82010-06-23 13:34:05 -0700249 link_shaders(whole_program);
250 status = (whole_program->LinkStatus) ? EXIT_SUCCESS : EXIT_FAILURE;
Ian Romanick832dfa52010-06-17 15:04:20 -0700251 }
252
Carl Worthbe83eb82010-06-23 13:34:05 -0700253 talloc_free(whole_program);
254
Ian Romanick6fd9fb22010-06-17 12:22:16 -0700255 return status;
Ian Romanick61d4aa02010-06-14 14:46:09 -0700256}