blob: f56e6f6142e6f8c79dcc66815473c2248907f115 [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
Kenneth Graunke32cf4822010-06-19 11:24:33 -070032extern "C" {
33#include <talloc.h>
34}
35
Ian Romanick61d4aa02010-06-14 14:46:09 -070036#include "ast.h"
37#include "glsl_parser_extras.h"
38#include "glsl_parser.h"
39#include "ir_optimization.h"
40#include "ir_print_visitor.h"
Ian Romanick8ce55db2010-06-17 12:01:18 -070041#include "program.h"
Ian Romanick61d4aa02010-06-14 14:46:09 -070042
43
44static char *
45load_text_file(const char *file_name, size_t *size)
46{
47 char *text = NULL;
48 struct stat st;
49 ssize_t total_read = 0;
50 int fd = open(file_name, O_RDONLY);
51
52 *size = 0;
53 if (fd < 0) {
54 return NULL;
55 }
56
57 if (fstat(fd, & st) == 0) {
58 text = (char *) malloc(st.st_size + 1);
59 if (text != NULL) {
60 do {
61 ssize_t bytes = read(fd, text + total_read,
62 st.st_size - total_read);
63 if (bytes < 0) {
64 free(text);
65 text = NULL;
66 break;
67 }
68
69 if (bytes == 0) {
70 break;
71 }
72
73 total_read += bytes;
74 } while (total_read < st.st_size);
75
76 text[total_read] = '\0';
77 *size = total_read;
78 }
79 }
80
81 close(fd);
82
83 return text;
84}
85
86
Ian Romanick2b368952010-06-15 12:00:37 -070087void
88usage_fail(const char *name)
89{
90 printf("%s <filename.frag|filename.vert>\n", name);
91 exit(EXIT_FAILURE);
92}
93
94
Ian Romanick7babbdb2010-06-15 12:47:07 -070095int dump_ast = 0;
Ian Romanick81e17472010-06-15 12:51:38 -070096int dump_lir = 0;
Ian Romanickc648a122010-06-17 19:51:48 -070097int do_link = 0;
Ian Romanick7babbdb2010-06-15 12:47:07 -070098
99const struct option compiler_opts[] = {
100 { "dump-ast", 0, &dump_ast, 1 },
Ian Romanick81e17472010-06-15 12:51:38 -0700101 { "dump-lir", 0, &dump_lir, 1 },
Ian Romanickc648a122010-06-17 19:51:48 -0700102 { "link", 0, &do_link, 1 },
Ian Romanick7babbdb2010-06-15 12:47:07 -0700103 { NULL, 0, NULL, 0 }
104};
105
Ian Romanick8ce55db2010-06-17 12:01:18 -0700106void
Kenneth Graunke29e60872010-06-17 15:28:34 -0700107compile_shader(struct glsl_shader *shader)
Ian Romanick61d4aa02010-06-14 14:46:09 -0700108{
Carl Worth2d2561e2010-06-23 15:43:38 -0700109 struct _mesa_glsl_parse_state *state;
Ian Romanick61d4aa02010-06-14 14:46:09 -0700110
Carl Worth2d2561e2010-06-23 15:43:38 -0700111 state = talloc_zero(talloc_parent(shader), struct _mesa_glsl_parse_state);
112
Kenneth Graunke29e60872010-06-17 15:28:34 -0700113 switch (shader->Type) {
Carl Worth2d2561e2010-06-23 15:43:38 -0700114 case GL_VERTEX_SHADER: state->target = vertex_shader; break;
115 case GL_FRAGMENT_SHADER: state->target = fragment_shader; break;
116 case GL_GEOMETRY_SHADER: state->target = geometry_shader; break;
Ian Romanick8ce55db2010-06-17 12:01:18 -0700117 }
118
Carl Worth2d2561e2010-06-23 15:43:38 -0700119 state->scanner = NULL;
120 state->translation_unit.make_empty();
121 state->symbols = new glsl_symbol_table;
122 state->info_log = talloc_strdup(shader, "");
123 state->error = false;
124 state->temp_index = 0;
125 state->loop_or_switch_nesting = NULL;
126 state->ARB_texture_rectangle_enable = true;
Ian Romanick61d4aa02010-06-14 14:46:09 -0700127
Kenneth Graunke04ba86a2010-06-16 12:18:00 -0700128 /* Create a new context for the preprocessor output. Ultimately, this
129 * should probably be the parser context, but there isn't one yet.
130 */
131 const char *source = shader->Source;
Carl Worth2d2561e2010-06-23 15:43:38 -0700132 state->error = preprocess(shader, &source, &state->info_log);
Kenneth Graunke04ba86a2010-06-16 12:18:00 -0700133
Carl Worth2d2561e2010-06-23 15:43:38 -0700134 if (!state->error) {
135 _mesa_glsl_lexer_ctor(state, source);
136 _mesa_glsl_parse(state);
137 _mesa_glsl_lexer_dtor(state);
Kenneth Graunke04ba86a2010-06-16 12:18:00 -0700138 }
Ian Romanick61d4aa02010-06-14 14:46:09 -0700139
Ian Romanick7babbdb2010-06-15 12:47:07 -0700140 if (dump_ast) {
Carl Worth2d2561e2010-06-23 15:43:38 -0700141 foreach_list_const(n, &state->translation_unit) {
Ian Romanick7babbdb2010-06-15 12:47:07 -0700142 ast_node *ast = exec_node_data(ast_node, n, link);
143 ast->print();
144 }
145 printf("\n\n");
Ian Romanick61d4aa02010-06-14 14:46:09 -0700146 }
147
Kenneth Graunke29e60872010-06-17 15:28:34 -0700148 shader->ir.make_empty();
Carl Worth2d2561e2010-06-23 15:43:38 -0700149 if (!state->error && !state->translation_unit.is_empty())
150 _mesa_ast_to_hir(&shader->ir, state);
Ian Romanick54992c32010-06-14 14:47:26 -0700151
Eric Anholt53cdb7e2010-06-22 12:07:21 -0700152 validate_ir_tree(&shader->ir);
153
Ian Romanick61d4aa02010-06-14 14:46:09 -0700154 /* Optimization passes */
Carl Worth2d2561e2010-06-23 15:43:38 -0700155 if (!state->error && !shader->ir.is_empty()) {
Ian Romanick61d4aa02010-06-14 14:46:09 -0700156 bool progress;
157 do {
158 progress = false;
159
Kenneth Graunke29e60872010-06-17 15:28:34 -0700160 progress = do_function_inlining(&shader->ir) || progress;
161 progress = do_if_simplification(&shader->ir) || progress;
162 progress = do_copy_propagation(&shader->ir) || progress;
163 progress = do_dead_code_local(&shader->ir) || progress;
164 progress = do_dead_code_unlinked(&shader->ir) || progress;
165 progress = do_constant_variable_unlinked(&shader->ir) || progress;
166 progress = do_constant_folding(&shader->ir) || progress;
167 progress = do_vec_index_to_swizzle(&shader->ir) || progress;
168 progress = do_swizzle_swizzle(&shader->ir) || progress;
Ian Romanick61d4aa02010-06-14 14:46:09 -0700169 } while (progress);
170 }
171
Eric Anholt53cdb7e2010-06-22 12:07:21 -0700172 validate_ir_tree(&shader->ir);
173
Ian Romanick61d4aa02010-06-14 14:46:09 -0700174 /* Print out the resulting IR */
Carl Worth2d2561e2010-06-23 15:43:38 -0700175 if (!state->error && dump_lir) {
176 _mesa_print_ir(&shader->ir, state);
Ian Romanick61d4aa02010-06-14 14:46:09 -0700177 }
178
Carl Worth2d2561e2010-06-23 15:43:38 -0700179 shader->symbols = state->symbols;
180 shader->CompileStatus = !state->error;
Ian Romanickca97bd32010-06-19 01:39:14 -0700181
182 if (shader->InfoLog)
183 talloc_free(shader->InfoLog);
184
Carl Worth2d2561e2010-06-23 15:43:38 -0700185 shader->InfoLog = state->info_log;
186
187 talloc_free(state);
Ian Romanickca97bd32010-06-19 01:39:14 -0700188
Ian Romanick8ce55db2010-06-17 12:01:18 -0700189 return;
190}
Ian Romanick61d4aa02010-06-14 14:46:09 -0700191
Ian Romanick8ce55db2010-06-17 12:01:18 -0700192int
193main(int argc, char **argv)
194{
Ian Romanick6fd9fb22010-06-17 12:22:16 -0700195 int status = EXIT_SUCCESS;
Ian Romanick8ce55db2010-06-17 12:01:18 -0700196
197 int c;
198 int idx = 0;
199 while ((c = getopt_long(argc, argv, "", compiler_opts, &idx)) != -1)
200 /* empty */ ;
201
202
203 if (argc <= optind)
204 usage_fail(argv[0]);
205
Carl Worthbe83eb82010-06-23 13:34:05 -0700206 struct glsl_program *whole_program;
207
208 whole_program = talloc_zero (NULL, struct glsl_program);
209 assert(whole_program != NULL);
Ian Romanick8ce55db2010-06-17 12:01:18 -0700210
Ian Romanick6fd9fb22010-06-17 12:22:16 -0700211 for (/* empty */; argc > optind; optind++) {
Carl Worthbe83eb82010-06-23 13:34:05 -0700212 whole_program->Shaders = (struct glsl_shader **)
213 realloc(whole_program->Shaders,
214 sizeof(struct glsl_shader *) * (whole_program->NumShaders + 1));
215 assert(whole_program->Shaders != NULL);
Ian Romanick8ce55db2010-06-17 12:01:18 -0700216
Carl Worthbe83eb82010-06-23 13:34:05 -0700217 struct glsl_shader *shader = talloc_zero(whole_program, glsl_shader);
Ian Romanick8ce55db2010-06-17 12:01:18 -0700218
Carl Worthbe83eb82010-06-23 13:34:05 -0700219 whole_program->Shaders[whole_program->NumShaders] = shader;
220 whole_program->NumShaders++;
Ian Romanick8ce55db2010-06-17 12:01:18 -0700221
Ian Romanick6fd9fb22010-06-17 12:22:16 -0700222 const unsigned len = strlen(argv[optind]);
223 if (len < 6)
224 usage_fail(argv[0]);
Ian Romanick8ce55db2010-06-17 12:01:18 -0700225
Ian Romanick6fd9fb22010-06-17 12:22:16 -0700226 const char *const ext = & argv[optind][len - 5];
227 if (strncmp(".vert", ext, 5) == 0)
Kenneth Graunke29e60872010-06-17 15:28:34 -0700228 shader->Type = GL_VERTEX_SHADER;
Ian Romanick6fd9fb22010-06-17 12:22:16 -0700229 else if (strncmp(".geom", ext, 5) == 0)
Kenneth Graunke29e60872010-06-17 15:28:34 -0700230 shader->Type = GL_GEOMETRY_SHADER;
Ian Romanick6fd9fb22010-06-17 12:22:16 -0700231 else if (strncmp(".frag", ext, 5) == 0)
Kenneth Graunke29e60872010-06-17 15:28:34 -0700232 shader->Type = GL_FRAGMENT_SHADER;
Ian Romanick6fd9fb22010-06-17 12:22:16 -0700233 else
234 usage_fail(argv[0]);
235
Kenneth Graunke29e60872010-06-17 15:28:34 -0700236 shader->Source = load_text_file(argv[optind], &shader->SourceLen);
Kenneth Graunke2848c4c2010-06-16 12:10:55 -0700237 if (shader->Source == NULL) {
238 printf("File \"%s\" does not exist.\n", argv[optind]);
239 exit(EXIT_FAILURE);
240 }
Ian Romanick6fd9fb22010-06-17 12:22:16 -0700241
Kenneth Graunke29e60872010-06-17 15:28:34 -0700242 compile_shader(shader);
Ian Romanick6fd9fb22010-06-17 12:22:16 -0700243
Kenneth Graunke29e60872010-06-17 15:28:34 -0700244 if (!shader->CompileStatus) {
Kenneth Graunkef3eb42d2010-06-19 11:31:01 -0700245 printf("Info log for %s:\n%s\n", argv[optind], shader->InfoLog);
Ian Romanick6fd9fb22010-06-17 12:22:16 -0700246 status = EXIT_FAILURE;
247 break;
248 }
249 }
250
Ian Romanickc648a122010-06-17 19:51:48 -0700251 if ((status == EXIT_SUCCESS) && do_link) {
Carl Worthbe83eb82010-06-23 13:34:05 -0700252 link_shaders(whole_program);
253 status = (whole_program->LinkStatus) ? EXIT_SUCCESS : EXIT_FAILURE;
Ian Romanick832dfa52010-06-17 15:04:20 -0700254 }
255
Carl Worthbe83eb82010-06-23 13:34:05 -0700256 talloc_free(whole_program);
257
Ian Romanick6fd9fb22010-06-17 12:22:16 -0700258 return status;
Ian Romanick61d4aa02010-06-14 14:46:09 -0700259}