Ian Romanick | 61d4aa0 | 2010-06-14 14:46:09 -0700 | [diff] [blame] | 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> |
Ian Romanick | 7babbdb | 2010-06-15 12:47:07 -0700 | [diff] [blame] | 25 | #include <getopt.h> |
Ian Romanick | 61d4aa0 | 2010-06-14 14:46:09 -0700 | [diff] [blame] | 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" |
Ian Romanick | 8ce55db | 2010-06-17 12:01:18 -0700 | [diff] [blame] | 37 | #include "program.h" |
Ian Romanick | 61d4aa0 | 2010-06-14 14:46:09 -0700 | [diff] [blame] | 38 | |
Carl Worth | a9696e7 | 2010-06-18 17:37:02 -0700 | [diff] [blame] | 39 | /* Returned string will have 'ctx' as its talloc owner. */ |
Ian Romanick | 61d4aa0 | 2010-06-14 14:46:09 -0700 | [diff] [blame] | 40 | static char * |
Carl Worth | a9696e7 | 2010-06-18 17:37:02 -0700 | [diff] [blame] | 41 | load_text_file(void *ctx, const char *file_name, size_t *size) |
Ian Romanick | 61d4aa0 | 2010-06-14 14:46:09 -0700 | [diff] [blame] | 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) { |
Carl Worth | a9696e7 | 2010-06-18 17:37:02 -0700 | [diff] [blame] | 54 | text = (char *) talloc_size(ctx, st.st_size + 1); |
Ian Romanick | 61d4aa0 | 2010-06-14 14:46:09 -0700 | [diff] [blame] | 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 | |
Ian Romanick | 2b36895 | 2010-06-15 12:00:37 -0700 | [diff] [blame] | 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 | |
Ian Romanick | 7babbdb | 2010-06-15 12:47:07 -0700 | [diff] [blame] | 91 | int dump_ast = 0; |
Ian Romanick | 81e1747 | 2010-06-15 12:51:38 -0700 | [diff] [blame] | 92 | int dump_lir = 0; |
Ian Romanick | c648a12 | 2010-06-17 19:51:48 -0700 | [diff] [blame] | 93 | int do_link = 0; |
Ian Romanick | 7babbdb | 2010-06-15 12:47:07 -0700 | [diff] [blame] | 94 | |
| 95 | const struct option compiler_opts[] = { |
| 96 | { "dump-ast", 0, &dump_ast, 1 }, |
Ian Romanick | 81e1747 | 2010-06-15 12:51:38 -0700 | [diff] [blame] | 97 | { "dump-lir", 0, &dump_lir, 1 }, |
Ian Romanick | c648a12 | 2010-06-17 19:51:48 -0700 | [diff] [blame] | 98 | { "link", 0, &do_link, 1 }, |
Ian Romanick | 7babbdb | 2010-06-15 12:47:07 -0700 | [diff] [blame] | 99 | { NULL, 0, NULL, 0 } |
| 100 | }; |
| 101 | |
Ian Romanick | 8ce55db | 2010-06-17 12:01:18 -0700 | [diff] [blame] | 102 | void |
Kenneth Graunke | 29e6087 | 2010-06-17 15:28:34 -0700 | [diff] [blame] | 103 | compile_shader(struct glsl_shader *shader) |
Ian Romanick | 61d4aa0 | 2010-06-14 14:46:09 -0700 | [diff] [blame] | 104 | { |
Carl Worth | 2d2561e | 2010-06-23 15:43:38 -0700 | [diff] [blame] | 105 | struct _mesa_glsl_parse_state *state; |
Ian Romanick | 61d4aa0 | 2010-06-14 14:46:09 -0700 | [diff] [blame] | 106 | |
Carl Worth | 2d2561e | 2010-06-23 15:43:38 -0700 | [diff] [blame] | 107 | state = talloc_zero(talloc_parent(shader), struct _mesa_glsl_parse_state); |
| 108 | |
Kenneth Graunke | 29e6087 | 2010-06-17 15:28:34 -0700 | [diff] [blame] | 109 | switch (shader->Type) { |
Carl Worth | 2d2561e | 2010-06-23 15:43:38 -0700 | [diff] [blame] | 110 | 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 Romanick | 8ce55db | 2010-06-17 12:01:18 -0700 | [diff] [blame] | 113 | } |
| 114 | |
Carl Worth | 2d2561e | 2010-06-23 15:43:38 -0700 | [diff] [blame] | 115 | state->scanner = NULL; |
| 116 | state->translation_unit.make_empty(); |
Carl Worth | f961e44 | 2010-06-23 15:47:04 -0700 | [diff] [blame] | 117 | state->symbols = new(shader) glsl_symbol_table; |
Carl Worth | 2d2561e | 2010-06-23 15:43:38 -0700 | [diff] [blame] | 118 | 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 Romanick | 61d4aa0 | 2010-06-14 14:46:09 -0700 | [diff] [blame] | 123 | |
Kenneth Graunke | 04ba86a | 2010-06-16 12:18:00 -0700 | [diff] [blame] | 124 | /* 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 Worth | 2d2561e | 2010-06-23 15:43:38 -0700 | [diff] [blame] | 128 | state->error = preprocess(shader, &source, &state->info_log); |
Kenneth Graunke | 04ba86a | 2010-06-16 12:18:00 -0700 | [diff] [blame] | 129 | |
Carl Worth | 2d2561e | 2010-06-23 15:43:38 -0700 | [diff] [blame] | 130 | if (!state->error) { |
| 131 | _mesa_glsl_lexer_ctor(state, source); |
| 132 | _mesa_glsl_parse(state); |
| 133 | _mesa_glsl_lexer_dtor(state); |
Kenneth Graunke | 04ba86a | 2010-06-16 12:18:00 -0700 | [diff] [blame] | 134 | } |
Ian Romanick | 61d4aa0 | 2010-06-14 14:46:09 -0700 | [diff] [blame] | 135 | |
Ian Romanick | 7babbdb | 2010-06-15 12:47:07 -0700 | [diff] [blame] | 136 | if (dump_ast) { |
Carl Worth | 2d2561e | 2010-06-23 15:43:38 -0700 | [diff] [blame] | 137 | foreach_list_const(n, &state->translation_unit) { |
Ian Romanick | 7babbdb | 2010-06-15 12:47:07 -0700 | [diff] [blame] | 138 | ast_node *ast = exec_node_data(ast_node, n, link); |
| 139 | ast->print(); |
| 140 | } |
| 141 | printf("\n\n"); |
Ian Romanick | 61d4aa0 | 2010-06-14 14:46:09 -0700 | [diff] [blame] | 142 | } |
| 143 | |
Kenneth Graunke | 29e6087 | 2010-06-17 15:28:34 -0700 | [diff] [blame] | 144 | shader->ir.make_empty(); |
Carl Worth | 2d2561e | 2010-06-23 15:43:38 -0700 | [diff] [blame] | 145 | if (!state->error && !state->translation_unit.is_empty()) |
| 146 | _mesa_ast_to_hir(&shader->ir, state); |
Ian Romanick | 54992c3 | 2010-06-14 14:47:26 -0700 | [diff] [blame] | 147 | |
Eric Anholt | 53cdb7e | 2010-06-22 12:07:21 -0700 | [diff] [blame] | 148 | validate_ir_tree(&shader->ir); |
| 149 | |
Ian Romanick | 61d4aa0 | 2010-06-14 14:46:09 -0700 | [diff] [blame] | 150 | /* Optimization passes */ |
Carl Worth | 2d2561e | 2010-06-23 15:43:38 -0700 | [diff] [blame] | 151 | if (!state->error && !shader->ir.is_empty()) { |
Ian Romanick | 61d4aa0 | 2010-06-14 14:46:09 -0700 | [diff] [blame] | 152 | bool progress; |
| 153 | do { |
| 154 | progress = false; |
| 155 | |
Kenneth Graunke | 29e6087 | 2010-06-17 15:28:34 -0700 | [diff] [blame] | 156 | 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 Anholt | bda2742 | 2010-06-25 13:38:38 -0700 | [diff] [blame^] | 160 | progress = do_dead_code_unlinked(state, &shader->ir) || progress; |
Kenneth Graunke | 29e6087 | 2010-06-17 15:28:34 -0700 | [diff] [blame] | 161 | 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 Romanick | 61d4aa0 | 2010-06-14 14:46:09 -0700 | [diff] [blame] | 165 | } while (progress); |
| 166 | } |
| 167 | |
Eric Anholt | 53cdb7e | 2010-06-22 12:07:21 -0700 | [diff] [blame] | 168 | validate_ir_tree(&shader->ir); |
| 169 | |
Ian Romanick | 61d4aa0 | 2010-06-14 14:46:09 -0700 | [diff] [blame] | 170 | /* Print out the resulting IR */ |
Carl Worth | 2d2561e | 2010-06-23 15:43:38 -0700 | [diff] [blame] | 171 | if (!state->error && dump_lir) { |
| 172 | _mesa_print_ir(&shader->ir, state); |
Ian Romanick | 61d4aa0 | 2010-06-14 14:46:09 -0700 | [diff] [blame] | 173 | } |
| 174 | |
Carl Worth | 2d2561e | 2010-06-23 15:43:38 -0700 | [diff] [blame] | 175 | shader->symbols = state->symbols; |
| 176 | shader->CompileStatus = !state->error; |
Ian Romanick | ca97bd3 | 2010-06-19 01:39:14 -0700 | [diff] [blame] | 177 | |
| 178 | if (shader->InfoLog) |
| 179 | talloc_free(shader->InfoLog); |
| 180 | |
Carl Worth | 2d2561e | 2010-06-23 15:43:38 -0700 | [diff] [blame] | 181 | shader->InfoLog = state->info_log; |
| 182 | |
| 183 | talloc_free(state); |
Ian Romanick | ca97bd3 | 2010-06-19 01:39:14 -0700 | [diff] [blame] | 184 | |
Ian Romanick | 8ce55db | 2010-06-17 12:01:18 -0700 | [diff] [blame] | 185 | return; |
| 186 | } |
Ian Romanick | 61d4aa0 | 2010-06-14 14:46:09 -0700 | [diff] [blame] | 187 | |
Ian Romanick | 8ce55db | 2010-06-17 12:01:18 -0700 | [diff] [blame] | 188 | int |
| 189 | main(int argc, char **argv) |
| 190 | { |
Ian Romanick | 6fd9fb2 | 2010-06-17 12:22:16 -0700 | [diff] [blame] | 191 | int status = EXIT_SUCCESS; |
Ian Romanick | 8ce55db | 2010-06-17 12:01:18 -0700 | [diff] [blame] | 192 | |
| 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 Worth | be83eb8 | 2010-06-23 13:34:05 -0700 | [diff] [blame] | 202 | struct glsl_program *whole_program; |
| 203 | |
| 204 | whole_program = talloc_zero (NULL, struct glsl_program); |
| 205 | assert(whole_program != NULL); |
Ian Romanick | 8ce55db | 2010-06-17 12:01:18 -0700 | [diff] [blame] | 206 | |
Ian Romanick | 6fd9fb2 | 2010-06-17 12:22:16 -0700 | [diff] [blame] | 207 | for (/* empty */; argc > optind; optind++) { |
Carl Worth | be83eb8 | 2010-06-23 13:34:05 -0700 | [diff] [blame] | 208 | whole_program->Shaders = (struct glsl_shader **) |
Carl Worth | 26bbfb7 | 2010-06-23 19:09:56 -0700 | [diff] [blame] | 209 | talloc_realloc(whole_program, whole_program->Shaders, |
| 210 | struct glsl_shader *, whole_program->NumShaders + 1); |
Carl Worth | be83eb8 | 2010-06-23 13:34:05 -0700 | [diff] [blame] | 211 | assert(whole_program->Shaders != NULL); |
Ian Romanick | 8ce55db | 2010-06-17 12:01:18 -0700 | [diff] [blame] | 212 | |
Carl Worth | be83eb8 | 2010-06-23 13:34:05 -0700 | [diff] [blame] | 213 | struct glsl_shader *shader = talloc_zero(whole_program, glsl_shader); |
Ian Romanick | 8ce55db | 2010-06-17 12:01:18 -0700 | [diff] [blame] | 214 | |
Carl Worth | be83eb8 | 2010-06-23 13:34:05 -0700 | [diff] [blame] | 215 | whole_program->Shaders[whole_program->NumShaders] = shader; |
| 216 | whole_program->NumShaders++; |
Ian Romanick | 8ce55db | 2010-06-17 12:01:18 -0700 | [diff] [blame] | 217 | |
Ian Romanick | 6fd9fb2 | 2010-06-17 12:22:16 -0700 | [diff] [blame] | 218 | const unsigned len = strlen(argv[optind]); |
| 219 | if (len < 6) |
| 220 | usage_fail(argv[0]); |
Ian Romanick | 8ce55db | 2010-06-17 12:01:18 -0700 | [diff] [blame] | 221 | |
Ian Romanick | 6fd9fb2 | 2010-06-17 12:22:16 -0700 | [diff] [blame] | 222 | const char *const ext = & argv[optind][len - 5]; |
| 223 | if (strncmp(".vert", ext, 5) == 0) |
Kenneth Graunke | 29e6087 | 2010-06-17 15:28:34 -0700 | [diff] [blame] | 224 | shader->Type = GL_VERTEX_SHADER; |
Ian Romanick | 6fd9fb2 | 2010-06-17 12:22:16 -0700 | [diff] [blame] | 225 | else if (strncmp(".geom", ext, 5) == 0) |
Kenneth Graunke | 29e6087 | 2010-06-17 15:28:34 -0700 | [diff] [blame] | 226 | shader->Type = GL_GEOMETRY_SHADER; |
Ian Romanick | 6fd9fb2 | 2010-06-17 12:22:16 -0700 | [diff] [blame] | 227 | else if (strncmp(".frag", ext, 5) == 0) |
Kenneth Graunke | 29e6087 | 2010-06-17 15:28:34 -0700 | [diff] [blame] | 228 | shader->Type = GL_FRAGMENT_SHADER; |
Ian Romanick | 6fd9fb2 | 2010-06-17 12:22:16 -0700 | [diff] [blame] | 229 | else |
| 230 | usage_fail(argv[0]); |
| 231 | |
Carl Worth | a9696e7 | 2010-06-18 17:37:02 -0700 | [diff] [blame] | 232 | shader->Source = load_text_file(whole_program, |
| 233 | argv[optind], &shader->SourceLen); |
Kenneth Graunke | 2848c4c | 2010-06-16 12:10:55 -0700 | [diff] [blame] | 234 | if (shader->Source == NULL) { |
| 235 | printf("File \"%s\" does not exist.\n", argv[optind]); |
| 236 | exit(EXIT_FAILURE); |
| 237 | } |
Ian Romanick | 6fd9fb2 | 2010-06-17 12:22:16 -0700 | [diff] [blame] | 238 | |
Kenneth Graunke | 29e6087 | 2010-06-17 15:28:34 -0700 | [diff] [blame] | 239 | compile_shader(shader); |
Ian Romanick | 6fd9fb2 | 2010-06-17 12:22:16 -0700 | [diff] [blame] | 240 | |
Kenneth Graunke | 29e6087 | 2010-06-17 15:28:34 -0700 | [diff] [blame] | 241 | if (!shader->CompileStatus) { |
Kenneth Graunke | f3eb42d | 2010-06-19 11:31:01 -0700 | [diff] [blame] | 242 | printf("Info log for %s:\n%s\n", argv[optind], shader->InfoLog); |
Ian Romanick | 6fd9fb2 | 2010-06-17 12:22:16 -0700 | [diff] [blame] | 243 | status = EXIT_FAILURE; |
| 244 | break; |
| 245 | } |
| 246 | } |
| 247 | |
Ian Romanick | c648a12 | 2010-06-17 19:51:48 -0700 | [diff] [blame] | 248 | if ((status == EXIT_SUCCESS) && do_link) { |
Carl Worth | be83eb8 | 2010-06-23 13:34:05 -0700 | [diff] [blame] | 249 | link_shaders(whole_program); |
| 250 | status = (whole_program->LinkStatus) ? EXIT_SUCCESS : EXIT_FAILURE; |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 251 | } |
| 252 | |
Carl Worth | be83eb8 | 2010-06-23 13:34:05 -0700 | [diff] [blame] | 253 | talloc_free(whole_program); |
| 254 | |
Ian Romanick | 6fd9fb2 | 2010-06-17 12:22:16 -0700 | [diff] [blame] | 255 | return status; |
Ian Romanick | 61d4aa0 | 2010-06-14 14:46:09 -0700 | [diff] [blame] | 256 | } |