blob: 78169d257d040e03f006871922c245570ae55704 [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{
109 struct _mesa_glsl_parse_state state;
Ian Romanick61d4aa02010-06-14 14:46:09 -0700110
Ian Romanickfc0ef642010-06-15 12:03:10 -0700111 memset(& state, 0, sizeof(state));
Kenneth Graunke29e60872010-06-17 15:28:34 -0700112 switch (shader->Type) {
Ian Romanick8ce55db2010-06-17 12:01:18 -0700113 case GL_VERTEX_SHADER: state.target = vertex_shader; break;
114 case GL_FRAGMENT_SHADER: state.target = fragment_shader; break;
115 case GL_GEOMETRY_SHADER: state.target = geometry_shader; break;
116 }
117
Ian Romanick61d4aa02010-06-14 14:46:09 -0700118 state.scanner = NULL;
119 state.translation_unit.make_empty();
120 state.symbols = new glsl_symbol_table;
Ian Romanickca97bd32010-06-19 01:39:14 -0700121 state.info_log = talloc_strdup(shader, "");
Ian Romanick61d4aa02010-06-14 14:46:09 -0700122 state.error = false;
123 state.temp_index = 0;
124 state.loop_or_switch_nesting = NULL;
125 state.ARB_texture_rectangle_enable = true;
126
Kenneth Graunke04ba86a2010-06-16 12:18:00 -0700127 /* Create a new context for the preprocessor output. Ultimately, this
128 * should probably be the parser context, but there isn't one yet.
129 */
130 const char *source = shader->Source;
Kenneth Graunke74704e82010-06-21 11:47:55 -0700131 state.error = preprocess(shader, &source, &state.info_log);
Kenneth Graunke04ba86a2010-06-16 12:18:00 -0700132
133 if (!state.error) {
Kenneth Graunke4a2bbda2010-06-21 11:43:42 -0700134 _mesa_glsl_lexer_ctor(& state, source);
Kenneth Graunke04ba86a2010-06-16 12:18:00 -0700135 _mesa_glsl_parse(& state);
136 _mesa_glsl_lexer_dtor(& state);
137 }
Ian Romanick61d4aa02010-06-14 14:46:09 -0700138
Ian Romanick7babbdb2010-06-15 12:47:07 -0700139 if (dump_ast) {
140 foreach_list_const(n, &state.translation_unit) {
141 ast_node *ast = exec_node_data(ast_node, n, link);
142 ast->print();
143 }
144 printf("\n\n");
Ian Romanick61d4aa02010-06-14 14:46:09 -0700145 }
146
Kenneth Graunke29e60872010-06-17 15:28:34 -0700147 shader->ir.make_empty();
Ian Romanick54992c32010-06-14 14:47:26 -0700148 if (!state.error && !state.translation_unit.is_empty())
Kenneth Graunke29e60872010-06-17 15:28:34 -0700149 _mesa_ast_to_hir(&shader->ir, &state);
Ian Romanick54992c32010-06-14 14:47:26 -0700150
Eric Anholt53cdb7e2010-06-22 12:07:21 -0700151 validate_ir_tree(&shader->ir);
152
Ian Romanick61d4aa02010-06-14 14:46:09 -0700153 /* Optimization passes */
Kenneth Graunke29e60872010-06-17 15:28:34 -0700154 if (!state.error && !shader->ir.is_empty()) {
Ian Romanick61d4aa02010-06-14 14:46:09 -0700155 bool progress;
156 do {
157 progress = false;
158
Kenneth Graunke29e60872010-06-17 15:28:34 -0700159 progress = do_function_inlining(&shader->ir) || progress;
160 progress = do_if_simplification(&shader->ir) || progress;
161 progress = do_copy_propagation(&shader->ir) || progress;
162 progress = do_dead_code_local(&shader->ir) || progress;
163 progress = do_dead_code_unlinked(&shader->ir) || progress;
164 progress = do_constant_variable_unlinked(&shader->ir) || progress;
165 progress = do_constant_folding(&shader->ir) || progress;
166 progress = do_vec_index_to_swizzle(&shader->ir) || progress;
167 progress = do_swizzle_swizzle(&shader->ir) || progress;
Ian Romanick61d4aa02010-06-14 14:46:09 -0700168 } while (progress);
169 }
170
Eric Anholt53cdb7e2010-06-22 12:07:21 -0700171 validate_ir_tree(&shader->ir);
172
Ian Romanick61d4aa02010-06-14 14:46:09 -0700173 /* Print out the resulting IR */
Ian Romanick81e17472010-06-15 12:51:38 -0700174 if (!state.error && dump_lir) {
Kenneth Graunke29e60872010-06-17 15:28:34 -0700175 _mesa_print_ir(&shader->ir, &state);
Ian Romanick61d4aa02010-06-14 14:46:09 -0700176 }
177
Kenneth Graunke29e60872010-06-17 15:28:34 -0700178 shader->symbols = state.symbols;
179 shader->CompileStatus = !state.error;
Ian Romanickca97bd32010-06-19 01:39:14 -0700180
181 if (shader->InfoLog)
182 talloc_free(shader->InfoLog);
183
184 shader->InfoLog = state.info_log;
185
Ian Romanick8ce55db2010-06-17 12:01:18 -0700186 return;
187}
Ian Romanick61d4aa02010-06-14 14:46:09 -0700188
Ian Romanick8ce55db2010-06-17 12:01:18 -0700189int
190main(int argc, char **argv)
191{
Ian Romanick6fd9fb22010-06-17 12:22:16 -0700192 int status = EXIT_SUCCESS;
Ian Romanick8ce55db2010-06-17 12:01:18 -0700193
194 int c;
195 int idx = 0;
196 while ((c = getopt_long(argc, argv, "", compiler_opts, &idx)) != -1)
197 /* empty */ ;
198
199
200 if (argc <= optind)
201 usage_fail(argv[0]);
202
Carl Worthbe83eb82010-06-23 13:34:05 -0700203 struct glsl_program *whole_program;
204
205 whole_program = talloc_zero (NULL, struct glsl_program);
206 assert(whole_program != NULL);
Ian Romanick8ce55db2010-06-17 12:01:18 -0700207
Ian Romanick6fd9fb22010-06-17 12:22:16 -0700208 for (/* empty */; argc > optind; optind++) {
Carl Worthbe83eb82010-06-23 13:34:05 -0700209 whole_program->Shaders = (struct glsl_shader **)
210 realloc(whole_program->Shaders,
211 sizeof(struct glsl_shader *) * (whole_program->NumShaders + 1));
212 assert(whole_program->Shaders != NULL);
Ian Romanick8ce55db2010-06-17 12:01:18 -0700213
Carl Worthbe83eb82010-06-23 13:34:05 -0700214 struct glsl_shader *shader = talloc_zero(whole_program, glsl_shader);
Ian Romanick8ce55db2010-06-17 12:01:18 -0700215
Carl Worthbe83eb82010-06-23 13:34:05 -0700216 whole_program->Shaders[whole_program->NumShaders] = shader;
217 whole_program->NumShaders++;
Ian Romanick8ce55db2010-06-17 12:01:18 -0700218
Ian Romanick6fd9fb22010-06-17 12:22:16 -0700219 const unsigned len = strlen(argv[optind]);
220 if (len < 6)
221 usage_fail(argv[0]);
Ian Romanick8ce55db2010-06-17 12:01:18 -0700222
Ian Romanick6fd9fb22010-06-17 12:22:16 -0700223 const char *const ext = & argv[optind][len - 5];
224 if (strncmp(".vert", ext, 5) == 0)
Kenneth Graunke29e60872010-06-17 15:28:34 -0700225 shader->Type = GL_VERTEX_SHADER;
Ian Romanick6fd9fb22010-06-17 12:22:16 -0700226 else if (strncmp(".geom", ext, 5) == 0)
Kenneth Graunke29e60872010-06-17 15:28:34 -0700227 shader->Type = GL_GEOMETRY_SHADER;
Ian Romanick6fd9fb22010-06-17 12:22:16 -0700228 else if (strncmp(".frag", ext, 5) == 0)
Kenneth Graunke29e60872010-06-17 15:28:34 -0700229 shader->Type = GL_FRAGMENT_SHADER;
Ian Romanick6fd9fb22010-06-17 12:22:16 -0700230 else
231 usage_fail(argv[0]);
232
Kenneth Graunke29e60872010-06-17 15:28:34 -0700233 shader->Source = load_text_file(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}