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 | |
Ian Romanick | 3fb8787 | 2010-07-09 14:09:34 -0700 | [diff] [blame] | 39 | extern "C" struct gl_shader * |
| 40 | _mesa_new_shader(GLcontext *ctx, GLuint name, GLenum type); |
| 41 | |
| 42 | /* Copied from shader_api.c for the stand-alone compiler. |
| 43 | */ |
| 44 | struct gl_shader * |
| 45 | _mesa_new_shader(GLcontext *ctx, GLuint name, GLenum type) |
| 46 | { |
| 47 | struct gl_shader *shader; |
| 48 | assert(type == GL_FRAGMENT_SHADER || type == GL_VERTEX_SHADER); |
| 49 | shader = talloc_zero(NULL, struct gl_shader); |
| 50 | if (shader) { |
| 51 | shader->Type = type; |
| 52 | shader->Name = name; |
| 53 | shader->RefCount = 1; |
| 54 | } |
| 55 | return shader; |
| 56 | } |
| 57 | |
Carl Worth | a9696e7 | 2010-06-18 17:37:02 -0700 | [diff] [blame] | 58 | /* Returned string will have 'ctx' as its talloc owner. */ |
Ian Romanick | 61d4aa0 | 2010-06-14 14:46:09 -0700 | [diff] [blame] | 59 | static char * |
Eric Anholt | 16b68b1 | 2010-06-30 11:05:43 -0700 | [diff] [blame] | 60 | load_text_file(void *ctx, const char *file_name) |
Ian Romanick | 61d4aa0 | 2010-06-14 14:46:09 -0700 | [diff] [blame] | 61 | { |
| 62 | char *text = NULL; |
| 63 | struct stat st; |
| 64 | ssize_t total_read = 0; |
| 65 | int fd = open(file_name, O_RDONLY); |
| 66 | |
Ian Romanick | 61d4aa0 | 2010-06-14 14:46:09 -0700 | [diff] [blame] | 67 | if (fd < 0) { |
| 68 | return NULL; |
| 69 | } |
| 70 | |
| 71 | if (fstat(fd, & st) == 0) { |
Carl Worth | a9696e7 | 2010-06-18 17:37:02 -0700 | [diff] [blame] | 72 | text = (char *) talloc_size(ctx, st.st_size + 1); |
Ian Romanick | 61d4aa0 | 2010-06-14 14:46:09 -0700 | [diff] [blame] | 73 | if (text != NULL) { |
| 74 | do { |
| 75 | ssize_t bytes = read(fd, text + total_read, |
| 76 | st.st_size - total_read); |
| 77 | if (bytes < 0) { |
| 78 | free(text); |
| 79 | text = NULL; |
| 80 | break; |
| 81 | } |
| 82 | |
| 83 | if (bytes == 0) { |
| 84 | break; |
| 85 | } |
| 86 | |
| 87 | total_read += bytes; |
| 88 | } while (total_read < st.st_size); |
| 89 | |
| 90 | text[total_read] = '\0'; |
Ian Romanick | 61d4aa0 | 2010-06-14 14:46:09 -0700 | [diff] [blame] | 91 | } |
| 92 | } |
| 93 | |
| 94 | close(fd); |
| 95 | |
| 96 | return text; |
| 97 | } |
| 98 | |
| 99 | |
Ian Romanick | 2b36895 | 2010-06-15 12:00:37 -0700 | [diff] [blame] | 100 | void |
| 101 | usage_fail(const char *name) |
| 102 | { |
| 103 | printf("%s <filename.frag|filename.vert>\n", name); |
| 104 | exit(EXIT_FAILURE); |
| 105 | } |
| 106 | |
| 107 | |
Ian Romanick | 7babbdb | 2010-06-15 12:47:07 -0700 | [diff] [blame] | 108 | int dump_ast = 0; |
Ian Romanick | 22c23de | 2010-06-25 15:27:47 -0700 | [diff] [blame] | 109 | int dump_hir = 0; |
Ian Romanick | 81e1747 | 2010-06-15 12:51:38 -0700 | [diff] [blame] | 110 | int dump_lir = 0; |
Ian Romanick | c648a12 | 2010-06-17 19:51:48 -0700 | [diff] [blame] | 111 | int do_link = 0; |
Ian Romanick | 7babbdb | 2010-06-15 12:47:07 -0700 | [diff] [blame] | 112 | |
| 113 | const struct option compiler_opts[] = { |
| 114 | { "dump-ast", 0, &dump_ast, 1 }, |
Ian Romanick | 22c23de | 2010-06-25 15:27:47 -0700 | [diff] [blame] | 115 | { "dump-hir", 0, &dump_hir, 1 }, |
Ian Romanick | 81e1747 | 2010-06-15 12:51:38 -0700 | [diff] [blame] | 116 | { "dump-lir", 0, &dump_lir, 1 }, |
Ian Romanick | c648a12 | 2010-06-17 19:51:48 -0700 | [diff] [blame] | 117 | { "link", 0, &do_link, 1 }, |
Ian Romanick | 7babbdb | 2010-06-15 12:47:07 -0700 | [diff] [blame] | 118 | { NULL, 0, NULL, 0 } |
| 119 | }; |
| 120 | |
Ian Romanick | 8ce55db | 2010-06-17 12:01:18 -0700 | [diff] [blame] | 121 | void |
Eric Anholt | 16b68b1 | 2010-06-30 11:05:43 -0700 | [diff] [blame] | 122 | compile_shader(struct gl_shader *shader) |
Ian Romanick | 61d4aa0 | 2010-06-14 14:46:09 -0700 | [diff] [blame] | 123 | { |
Kenneth Graunke | aa9f86a | 2010-07-22 16:20:36 -0700 | [diff] [blame^] | 124 | struct _mesa_glsl_parse_state *state = |
| 125 | new(shader) _mesa_glsl_parse_state(NULL, shader->Type, shader); |
Eric Anholt | f894669 | 2010-07-20 14:03:35 -0700 | [diff] [blame] | 126 | |
Kenneth Graunke | 04ba86a | 2010-06-16 12:18:00 -0700 | [diff] [blame] | 127 | const char *source = shader->Source; |
Kenneth Graunke | aa9f86a | 2010-07-22 16:20:36 -0700 | [diff] [blame^] | 128 | state->error = preprocess(state, &source, &state->info_log, |
| 129 | state->extensions); |
Kenneth Graunke | 04ba86a | 2010-06-16 12:18:00 -0700 | [diff] [blame] | 130 | |
Carl Worth | 2d2561e | 2010-06-23 15:43:38 -0700 | [diff] [blame] | 131 | if (!state->error) { |
| 132 | _mesa_glsl_lexer_ctor(state, source); |
| 133 | _mesa_glsl_parse(state); |
| 134 | _mesa_glsl_lexer_dtor(state); |
Kenneth Graunke | 04ba86a | 2010-06-16 12:18:00 -0700 | [diff] [blame] | 135 | } |
Ian Romanick | 61d4aa0 | 2010-06-14 14:46:09 -0700 | [diff] [blame] | 136 | |
Ian Romanick | 7babbdb | 2010-06-15 12:47:07 -0700 | [diff] [blame] | 137 | if (dump_ast) { |
Carl Worth | 2d2561e | 2010-06-23 15:43:38 -0700 | [diff] [blame] | 138 | foreach_list_const(n, &state->translation_unit) { |
Ian Romanick | 7babbdb | 2010-06-15 12:47:07 -0700 | [diff] [blame] | 139 | ast_node *ast = exec_node_data(ast_node, n, link); |
| 140 | ast->print(); |
| 141 | } |
| 142 | printf("\n\n"); |
Ian Romanick | 61d4aa0 | 2010-06-14 14:46:09 -0700 | [diff] [blame] | 143 | } |
| 144 | |
Eric Anholt | 16b68b1 | 2010-06-30 11:05:43 -0700 | [diff] [blame] | 145 | shader->ir = new(shader) exec_list; |
Carl Worth | 2d2561e | 2010-06-23 15:43:38 -0700 | [diff] [blame] | 146 | if (!state->error && !state->translation_unit.is_empty()) |
Eric Anholt | 16b68b1 | 2010-06-30 11:05:43 -0700 | [diff] [blame] | 147 | _mesa_ast_to_hir(shader->ir, state); |
Ian Romanick | 54992c3 | 2010-06-14 14:47:26 -0700 | [diff] [blame] | 148 | |
Ian Romanick | 22c23de | 2010-06-25 15:27:47 -0700 | [diff] [blame] | 149 | /* Print out the unoptimized IR. */ |
| 150 | if (!state->error && dump_hir) { |
Ian Romanick | 5a2e0b8 | 2010-07-20 11:37:45 -0700 | [diff] [blame] | 151 | validate_ir_tree(shader->ir); |
Eric Anholt | 16b68b1 | 2010-06-30 11:05:43 -0700 | [diff] [blame] | 152 | _mesa_print_ir(shader->ir, state); |
Ian Romanick | 22c23de | 2010-06-25 15:27:47 -0700 | [diff] [blame] | 153 | } |
| 154 | |
Ian Romanick | 61d4aa0 | 2010-06-14 14:46:09 -0700 | [diff] [blame] | 155 | /* Optimization passes */ |
Eric Anholt | 16b68b1 | 2010-06-30 11:05:43 -0700 | [diff] [blame] | 156 | if (!state->error && !shader->ir->is_empty()) { |
Ian Romanick | 61d4aa0 | 2010-06-14 14:46:09 -0700 | [diff] [blame] | 157 | bool progress; |
| 158 | do { |
| 159 | progress = false; |
| 160 | |
Eric Anholt | 16b68b1 | 2010-06-30 11:05:43 -0700 | [diff] [blame] | 161 | progress = do_function_inlining(shader->ir) || progress; |
| 162 | progress = do_if_simplification(shader->ir) || progress; |
| 163 | progress = do_copy_propagation(shader->ir) || progress; |
| 164 | progress = do_dead_code_local(shader->ir) || progress; |
| 165 | progress = do_dead_code_unlinked(state, shader->ir) || progress; |
| 166 | progress = do_constant_variable_unlinked(shader->ir) || progress; |
| 167 | progress = do_constant_folding(shader->ir) || progress; |
| 168 | progress = do_vec_index_to_swizzle(shader->ir) || progress; |
Eric Anholt | a36334b | 2010-07-06 17:53:32 -0700 | [diff] [blame] | 169 | progress = do_vec_index_to_cond_assign(shader->ir) || progress; |
Eric Anholt | 16b68b1 | 2010-06-30 11:05:43 -0700 | [diff] [blame] | 170 | progress = do_swizzle_swizzle(shader->ir) || progress; |
Ian Romanick | 61d4aa0 | 2010-06-14 14:46:09 -0700 | [diff] [blame] | 171 | } while (progress); |
Ian Romanick | 5a2e0b8 | 2010-07-20 11:37:45 -0700 | [diff] [blame] | 172 | |
| 173 | validate_ir_tree(shader->ir); |
Ian Romanick | 61d4aa0 | 2010-06-14 14:46:09 -0700 | [diff] [blame] | 174 | } |
| 175 | |
Eric Anholt | 53cdb7e | 2010-06-22 12:07:21 -0700 | [diff] [blame] | 176 | |
Ian Romanick | 61d4aa0 | 2010-06-14 14:46:09 -0700 | [diff] [blame] | 177 | /* Print out the resulting IR */ |
Carl Worth | 2d2561e | 2010-06-23 15:43:38 -0700 | [diff] [blame] | 178 | if (!state->error && dump_lir) { |
Eric Anholt | 16b68b1 | 2010-06-30 11:05:43 -0700 | [diff] [blame] | 179 | _mesa_print_ir(shader->ir, state); |
Ian Romanick | 61d4aa0 | 2010-06-14 14:46:09 -0700 | [diff] [blame] | 180 | } |
| 181 | |
Carl Worth | 2d2561e | 2010-06-23 15:43:38 -0700 | [diff] [blame] | 182 | shader->symbols = state->symbols; |
| 183 | shader->CompileStatus = !state->error; |
Ian Romanick | 25f51d3 | 2010-07-16 15:51:50 -0700 | [diff] [blame] | 184 | shader->Version = state->language_version; |
Ian Romanick | d5be2ac | 2010-07-20 11:29:46 -0700 | [diff] [blame] | 185 | memcpy(shader->builtins_to_link, state->builtins_to_link, |
| 186 | sizeof(shader->builtins_to_link[0]) * state->num_builtins_to_link); |
| 187 | shader->num_builtins_to_link = state->num_builtins_to_link; |
Ian Romanick | ca97bd3 | 2010-06-19 01:39:14 -0700 | [diff] [blame] | 188 | |
| 189 | if (shader->InfoLog) |
| 190 | talloc_free(shader->InfoLog); |
| 191 | |
Carl Worth | 2d2561e | 2010-06-23 15:43:38 -0700 | [diff] [blame] | 192 | shader->InfoLog = state->info_log; |
| 193 | |
Kenneth Graunke | 116f1d4 | 2010-06-25 14:10:01 -0700 | [diff] [blame] | 194 | /* Retain any live IR, but trash the rest. */ |
Ian Romanick | 60e2d06 | 2010-07-20 11:27:38 -0700 | [diff] [blame] | 195 | reparent_ir(shader->ir, shader); |
Kenneth Graunke | 116f1d4 | 2010-06-25 14:10:01 -0700 | [diff] [blame] | 196 | |
Carl Worth | 2d2561e | 2010-06-23 15:43:38 -0700 | [diff] [blame] | 197 | talloc_free(state); |
Ian Romanick | ca97bd3 | 2010-06-19 01:39:14 -0700 | [diff] [blame] | 198 | |
Ian Romanick | 8ce55db | 2010-06-17 12:01:18 -0700 | [diff] [blame] | 199 | return; |
| 200 | } |
Ian Romanick | 61d4aa0 | 2010-06-14 14:46:09 -0700 | [diff] [blame] | 201 | |
Ian Romanick | 8ce55db | 2010-06-17 12:01:18 -0700 | [diff] [blame] | 202 | int |
| 203 | main(int argc, char **argv) |
| 204 | { |
Ian Romanick | 6fd9fb2 | 2010-06-17 12:22:16 -0700 | [diff] [blame] | 205 | int status = EXIT_SUCCESS; |
Ian Romanick | 8ce55db | 2010-06-17 12:01:18 -0700 | [diff] [blame] | 206 | |
| 207 | int c; |
| 208 | int idx = 0; |
| 209 | while ((c = getopt_long(argc, argv, "", compiler_opts, &idx)) != -1) |
| 210 | /* empty */ ; |
| 211 | |
| 212 | |
| 213 | if (argc <= optind) |
| 214 | usage_fail(argv[0]); |
| 215 | |
Eric Anholt | 849e181 | 2010-06-30 11:49:17 -0700 | [diff] [blame] | 216 | struct gl_shader_program *whole_program; |
Carl Worth | be83eb8 | 2010-06-23 13:34:05 -0700 | [diff] [blame] | 217 | |
Eric Anholt | 849e181 | 2010-06-30 11:49:17 -0700 | [diff] [blame] | 218 | whole_program = talloc_zero (NULL, struct gl_shader_program); |
Carl Worth | be83eb8 | 2010-06-23 13:34:05 -0700 | [diff] [blame] | 219 | assert(whole_program != NULL); |
Ian Romanick | 8ce55db | 2010-06-17 12:01:18 -0700 | [diff] [blame] | 220 | |
Ian Romanick | 6fd9fb2 | 2010-06-17 12:22:16 -0700 | [diff] [blame] | 221 | for (/* empty */; argc > optind; optind++) { |
Eric Anholt | 16b68b1 | 2010-06-30 11:05:43 -0700 | [diff] [blame] | 222 | whole_program->Shaders = (struct gl_shader **) |
Carl Worth | 26bbfb7 | 2010-06-23 19:09:56 -0700 | [diff] [blame] | 223 | talloc_realloc(whole_program, whole_program->Shaders, |
Eric Anholt | 16b68b1 | 2010-06-30 11:05:43 -0700 | [diff] [blame] | 224 | struct gl_shader *, whole_program->NumShaders + 1); |
Carl Worth | be83eb8 | 2010-06-23 13:34:05 -0700 | [diff] [blame] | 225 | assert(whole_program->Shaders != NULL); |
Ian Romanick | 8ce55db | 2010-06-17 12:01:18 -0700 | [diff] [blame] | 226 | |
Eric Anholt | 16b68b1 | 2010-06-30 11:05:43 -0700 | [diff] [blame] | 227 | struct gl_shader *shader = talloc_zero(whole_program, gl_shader); |
Ian Romanick | 8ce55db | 2010-06-17 12:01:18 -0700 | [diff] [blame] | 228 | |
Carl Worth | be83eb8 | 2010-06-23 13:34:05 -0700 | [diff] [blame] | 229 | whole_program->Shaders[whole_program->NumShaders] = shader; |
| 230 | whole_program->NumShaders++; |
Ian Romanick | 8ce55db | 2010-06-17 12:01:18 -0700 | [diff] [blame] | 231 | |
Ian Romanick | 6fd9fb2 | 2010-06-17 12:22:16 -0700 | [diff] [blame] | 232 | const unsigned len = strlen(argv[optind]); |
| 233 | if (len < 6) |
| 234 | usage_fail(argv[0]); |
Ian Romanick | 8ce55db | 2010-06-17 12:01:18 -0700 | [diff] [blame] | 235 | |
Ian Romanick | 6fd9fb2 | 2010-06-17 12:22:16 -0700 | [diff] [blame] | 236 | const char *const ext = & argv[optind][len - 5]; |
| 237 | if (strncmp(".vert", ext, 5) == 0) |
Kenneth Graunke | 29e6087 | 2010-06-17 15:28:34 -0700 | [diff] [blame] | 238 | shader->Type = GL_VERTEX_SHADER; |
Ian Romanick | 6fd9fb2 | 2010-06-17 12:22:16 -0700 | [diff] [blame] | 239 | else if (strncmp(".geom", ext, 5) == 0) |
Kenneth Graunke | 29e6087 | 2010-06-17 15:28:34 -0700 | [diff] [blame] | 240 | shader->Type = GL_GEOMETRY_SHADER; |
Ian Romanick | 6fd9fb2 | 2010-06-17 12:22:16 -0700 | [diff] [blame] | 241 | else if (strncmp(".frag", ext, 5) == 0) |
Kenneth Graunke | 29e6087 | 2010-06-17 15:28:34 -0700 | [diff] [blame] | 242 | shader->Type = GL_FRAGMENT_SHADER; |
Ian Romanick | 6fd9fb2 | 2010-06-17 12:22:16 -0700 | [diff] [blame] | 243 | else |
| 244 | usage_fail(argv[0]); |
| 245 | |
Eric Anholt | 16b68b1 | 2010-06-30 11:05:43 -0700 | [diff] [blame] | 246 | shader->Source = load_text_file(whole_program, argv[optind]); |
Kenneth Graunke | 2848c4c | 2010-06-16 12:10:55 -0700 | [diff] [blame] | 247 | if (shader->Source == NULL) { |
| 248 | printf("File \"%s\" does not exist.\n", argv[optind]); |
| 249 | exit(EXIT_FAILURE); |
| 250 | } |
Ian Romanick | 6fd9fb2 | 2010-06-17 12:22:16 -0700 | [diff] [blame] | 251 | |
Kenneth Graunke | 29e6087 | 2010-06-17 15:28:34 -0700 | [diff] [blame] | 252 | compile_shader(shader); |
Ian Romanick | 6fd9fb2 | 2010-06-17 12:22:16 -0700 | [diff] [blame] | 253 | |
Kenneth Graunke | 29e6087 | 2010-06-17 15:28:34 -0700 | [diff] [blame] | 254 | if (!shader->CompileStatus) { |
Kenneth Graunke | f3eb42d | 2010-06-19 11:31:01 -0700 | [diff] [blame] | 255 | printf("Info log for %s:\n%s\n", argv[optind], shader->InfoLog); |
Ian Romanick | 6fd9fb2 | 2010-06-17 12:22:16 -0700 | [diff] [blame] | 256 | status = EXIT_FAILURE; |
| 257 | break; |
| 258 | } |
| 259 | } |
| 260 | |
Ian Romanick | c648a12 | 2010-06-17 19:51:48 -0700 | [diff] [blame] | 261 | if ((status == EXIT_SUCCESS) && do_link) { |
Carl Worth | be83eb8 | 2010-06-23 13:34:05 -0700 | [diff] [blame] | 262 | link_shaders(whole_program); |
| 263 | status = (whole_program->LinkStatus) ? EXIT_SUCCESS : EXIT_FAILURE; |
Ian Romanick | 4d962e6 | 2010-07-02 14:43:01 -0700 | [diff] [blame] | 264 | |
| 265 | if (strlen(whole_program->InfoLog) > 0) |
| 266 | printf("Info log for linking:\n%s\n", whole_program->InfoLog); |
Ian Romanick | 832dfa5 | 2010-06-17 15:04:20 -0700 | [diff] [blame] | 267 | } |
| 268 | |
Ian Romanick | 3fb8787 | 2010-07-09 14:09:34 -0700 | [diff] [blame] | 269 | for (unsigned i = 0; i < whole_program->_NumLinkedShaders; i++) |
| 270 | talloc_free(whole_program->_LinkedShaders[i]); |
| 271 | |
Carl Worth | be83eb8 | 2010-06-23 13:34:05 -0700 | [diff] [blame] | 272 | talloc_free(whole_program); |
Ian Romanick | efc15f8 | 2010-06-28 13:17:18 -0700 | [diff] [blame] | 273 | _mesa_glsl_release_types(); |
Ian Romanick | d5be2ac | 2010-07-20 11:29:46 -0700 | [diff] [blame] | 274 | _mesa_glsl_release_functions(); |
Carl Worth | be83eb8 | 2010-06-23 13:34:05 -0700 | [diff] [blame] | 275 | |
Ian Romanick | 6fd9fb2 | 2010-06-17 12:22:16 -0700 | [diff] [blame] | 276 | return status; |
Ian Romanick | 61d4aa0 | 2010-06-14 14:46:09 -0700 | [diff] [blame] | 277 | } |