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