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 | |
Kenneth Graunke | 32cf482 | 2010-06-19 11:24:33 -0700 | [diff] [blame] | 32 | extern "C" { |
| 33 | #include <talloc.h> |
| 34 | } |
| 35 | |
Ian Romanick | 61d4aa0 | 2010-06-14 14:46:09 -0700 | [diff] [blame] | 36 | #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 Romanick | 8ce55db | 2010-06-17 12:01:18 -0700 | [diff] [blame] | 41 | #include "program.h" |
Ian Romanick | 61d4aa0 | 2010-06-14 14:46:09 -0700 | [diff] [blame] | 42 | |
| 43 | |
| 44 | static char * |
| 45 | load_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 Romanick | 2b36895 | 2010-06-15 12:00:37 -0700 | [diff] [blame] | 87 | void |
| 88 | usage_fail(const char *name) |
| 89 | { |
| 90 | printf("%s <filename.frag|filename.vert>\n", name); |
| 91 | exit(EXIT_FAILURE); |
| 92 | } |
| 93 | |
| 94 | |
Ian Romanick | 7babbdb | 2010-06-15 12:47:07 -0700 | [diff] [blame] | 95 | int dump_ast = 0; |
Ian Romanick | 81e1747 | 2010-06-15 12:51:38 -0700 | [diff] [blame] | 96 | int dump_lir = 0; |
Ian Romanick | c648a12 | 2010-06-17 19:51:48 -0700 | [diff] [blame] | 97 | int do_link = 0; |
Ian Romanick | 7babbdb | 2010-06-15 12:47:07 -0700 | [diff] [blame] | 98 | |
| 99 | const struct option compiler_opts[] = { |
| 100 | { "dump-ast", 0, &dump_ast, 1 }, |
Ian Romanick | 81e1747 | 2010-06-15 12:51:38 -0700 | [diff] [blame] | 101 | { "dump-lir", 0, &dump_lir, 1 }, |
Ian Romanick | c648a12 | 2010-06-17 19:51:48 -0700 | [diff] [blame] | 102 | { "link", 0, &do_link, 1 }, |
Ian Romanick | 7babbdb | 2010-06-15 12:47:07 -0700 | [diff] [blame] | 103 | { NULL, 0, NULL, 0 } |
| 104 | }; |
| 105 | |
Ian Romanick | 8ce55db | 2010-06-17 12:01:18 -0700 | [diff] [blame] | 106 | void |
Kenneth Graunke | 29e6087 | 2010-06-17 15:28:34 -0700 | [diff] [blame] | 107 | compile_shader(struct glsl_shader *shader) |
Ian Romanick | 61d4aa0 | 2010-06-14 14:46:09 -0700 | [diff] [blame] | 108 | { |
| 109 | struct _mesa_glsl_parse_state state; |
Ian Romanick | 61d4aa0 | 2010-06-14 14:46:09 -0700 | [diff] [blame] | 110 | |
Ian Romanick | fc0ef64 | 2010-06-15 12:03:10 -0700 | [diff] [blame] | 111 | memset(& state, 0, sizeof(state)); |
Kenneth Graunke | 29e6087 | 2010-06-17 15:28:34 -0700 | [diff] [blame] | 112 | switch (shader->Type) { |
Ian Romanick | 8ce55db | 2010-06-17 12:01:18 -0700 | [diff] [blame] | 113 | 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 Romanick | 61d4aa0 | 2010-06-14 14:46:09 -0700 | [diff] [blame] | 118 | state.scanner = NULL; |
| 119 | state.translation_unit.make_empty(); |
| 120 | state.symbols = new glsl_symbol_table; |
Ian Romanick | ca97bd3 | 2010-06-19 01:39:14 -0700 | [diff] [blame] | 121 | state.info_log = talloc_strdup(shader, ""); |
Ian Romanick | 61d4aa0 | 2010-06-14 14:46:09 -0700 | [diff] [blame] | 122 | state.error = false; |
| 123 | state.temp_index = 0; |
| 124 | state.loop_or_switch_nesting = NULL; |
| 125 | state.ARB_texture_rectangle_enable = true; |
| 126 | |
Kenneth Graunke | 04ba86a | 2010-06-16 12:18:00 -0700 | [diff] [blame] | 127 | /* 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 Graunke | 74704e8 | 2010-06-21 11:47:55 -0700 | [diff] [blame] | 131 | state.error = preprocess(shader, &source, &state.info_log); |
Kenneth Graunke | 04ba86a | 2010-06-16 12:18:00 -0700 | [diff] [blame] | 132 | |
| 133 | if (!state.error) { |
Kenneth Graunke | 4a2bbda | 2010-06-21 11:43:42 -0700 | [diff] [blame] | 134 | _mesa_glsl_lexer_ctor(& state, source); |
Kenneth Graunke | 04ba86a | 2010-06-16 12:18:00 -0700 | [diff] [blame] | 135 | _mesa_glsl_parse(& state); |
| 136 | _mesa_glsl_lexer_dtor(& state); |
| 137 | } |
Ian Romanick | 61d4aa0 | 2010-06-14 14:46:09 -0700 | [diff] [blame] | 138 | |
Ian Romanick | 7babbdb | 2010-06-15 12:47:07 -0700 | [diff] [blame] | 139 | 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 Romanick | 61d4aa0 | 2010-06-14 14:46:09 -0700 | [diff] [blame] | 145 | } |
| 146 | |
Kenneth Graunke | 29e6087 | 2010-06-17 15:28:34 -0700 | [diff] [blame] | 147 | shader->ir.make_empty(); |
Ian Romanick | 54992c3 | 2010-06-14 14:47:26 -0700 | [diff] [blame] | 148 | if (!state.error && !state.translation_unit.is_empty()) |
Kenneth Graunke | 29e6087 | 2010-06-17 15:28:34 -0700 | [diff] [blame] | 149 | _mesa_ast_to_hir(&shader->ir, &state); |
Ian Romanick | 54992c3 | 2010-06-14 14:47:26 -0700 | [diff] [blame] | 150 | |
Eric Anholt | 53cdb7e | 2010-06-22 12:07:21 -0700 | [diff] [blame] | 151 | validate_ir_tree(&shader->ir); |
| 152 | |
Ian Romanick | 61d4aa0 | 2010-06-14 14:46:09 -0700 | [diff] [blame] | 153 | /* Optimization passes */ |
Kenneth Graunke | 29e6087 | 2010-06-17 15:28:34 -0700 | [diff] [blame] | 154 | if (!state.error && !shader->ir.is_empty()) { |
Ian Romanick | 61d4aa0 | 2010-06-14 14:46:09 -0700 | [diff] [blame] | 155 | bool progress; |
| 156 | do { |
| 157 | progress = false; |
| 158 | |
Kenneth Graunke | 29e6087 | 2010-06-17 15:28:34 -0700 | [diff] [blame] | 159 | 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 Romanick | 61d4aa0 | 2010-06-14 14:46:09 -0700 | [diff] [blame] | 168 | } while (progress); |
| 169 | } |
| 170 | |
Eric Anholt | 53cdb7e | 2010-06-22 12:07:21 -0700 | [diff] [blame] | 171 | validate_ir_tree(&shader->ir); |
| 172 | |
Ian Romanick | 61d4aa0 | 2010-06-14 14:46:09 -0700 | [diff] [blame] | 173 | /* Print out the resulting IR */ |
Ian Romanick | 81e1747 | 2010-06-15 12:51:38 -0700 | [diff] [blame] | 174 | if (!state.error && dump_lir) { |
Kenneth Graunke | 29e6087 | 2010-06-17 15:28:34 -0700 | [diff] [blame] | 175 | _mesa_print_ir(&shader->ir, &state); |
Ian Romanick | 61d4aa0 | 2010-06-14 14:46:09 -0700 | [diff] [blame] | 176 | } |
| 177 | |
Kenneth Graunke | 29e6087 | 2010-06-17 15:28:34 -0700 | [diff] [blame] | 178 | shader->symbols = state.symbols; |
| 179 | shader->CompileStatus = !state.error; |
Ian Romanick | ca97bd3 | 2010-06-19 01:39:14 -0700 | [diff] [blame] | 180 | |
| 181 | if (shader->InfoLog) |
| 182 | talloc_free(shader->InfoLog); |
| 183 | |
| 184 | shader->InfoLog = state.info_log; |
| 185 | |
Ian Romanick | 8ce55db | 2010-06-17 12:01:18 -0700 | [diff] [blame] | 186 | return; |
| 187 | } |
Ian Romanick | 61d4aa0 | 2010-06-14 14:46:09 -0700 | [diff] [blame] | 188 | |
Ian Romanick | 8ce55db | 2010-06-17 12:01:18 -0700 | [diff] [blame] | 189 | int |
| 190 | main(int argc, char **argv) |
| 191 | { |
Ian Romanick | 6fd9fb2 | 2010-06-17 12:22:16 -0700 | [diff] [blame] | 192 | int status = EXIT_SUCCESS; |
Ian Romanick | 8ce55db | 2010-06-17 12:01:18 -0700 | [diff] [blame] | 193 | |
| 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 Worth | be83eb8 | 2010-06-23 13:34:05 -0700 | [diff] [blame^] | 203 | struct glsl_program *whole_program; |
| 204 | |
| 205 | whole_program = talloc_zero (NULL, struct glsl_program); |
| 206 | assert(whole_program != NULL); |
Ian Romanick | 8ce55db | 2010-06-17 12:01:18 -0700 | [diff] [blame] | 207 | |
Ian Romanick | 6fd9fb2 | 2010-06-17 12:22:16 -0700 | [diff] [blame] | 208 | for (/* empty */; argc > optind; optind++) { |
Carl Worth | be83eb8 | 2010-06-23 13:34:05 -0700 | [diff] [blame^] | 209 | 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 Romanick | 8ce55db | 2010-06-17 12:01:18 -0700 | [diff] [blame] | 213 | |
Carl Worth | be83eb8 | 2010-06-23 13:34:05 -0700 | [diff] [blame^] | 214 | struct glsl_shader *shader = talloc_zero(whole_program, glsl_shader); |
Ian Romanick | 8ce55db | 2010-06-17 12:01:18 -0700 | [diff] [blame] | 215 | |
Carl Worth | be83eb8 | 2010-06-23 13:34:05 -0700 | [diff] [blame^] | 216 | whole_program->Shaders[whole_program->NumShaders] = shader; |
| 217 | whole_program->NumShaders++; |
Ian Romanick | 8ce55db | 2010-06-17 12:01:18 -0700 | [diff] [blame] | 218 | |
Ian Romanick | 6fd9fb2 | 2010-06-17 12:22:16 -0700 | [diff] [blame] | 219 | const unsigned len = strlen(argv[optind]); |
| 220 | if (len < 6) |
| 221 | usage_fail(argv[0]); |
Ian Romanick | 8ce55db | 2010-06-17 12:01:18 -0700 | [diff] [blame] | 222 | |
Ian Romanick | 6fd9fb2 | 2010-06-17 12:22:16 -0700 | [diff] [blame] | 223 | const char *const ext = & argv[optind][len - 5]; |
| 224 | if (strncmp(".vert", ext, 5) == 0) |
Kenneth Graunke | 29e6087 | 2010-06-17 15:28:34 -0700 | [diff] [blame] | 225 | shader->Type = GL_VERTEX_SHADER; |
Ian Romanick | 6fd9fb2 | 2010-06-17 12:22:16 -0700 | [diff] [blame] | 226 | else if (strncmp(".geom", ext, 5) == 0) |
Kenneth Graunke | 29e6087 | 2010-06-17 15:28:34 -0700 | [diff] [blame] | 227 | shader->Type = GL_GEOMETRY_SHADER; |
Ian Romanick | 6fd9fb2 | 2010-06-17 12:22:16 -0700 | [diff] [blame] | 228 | else if (strncmp(".frag", ext, 5) == 0) |
Kenneth Graunke | 29e6087 | 2010-06-17 15:28:34 -0700 | [diff] [blame] | 229 | shader->Type = GL_FRAGMENT_SHADER; |
Ian Romanick | 6fd9fb2 | 2010-06-17 12:22:16 -0700 | [diff] [blame] | 230 | else |
| 231 | usage_fail(argv[0]); |
| 232 | |
Kenneth Graunke | 29e6087 | 2010-06-17 15:28:34 -0700 | [diff] [blame] | 233 | shader->Source = load_text_file(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 | } |