2 * Copyright © 2008, 2009 Intel Corporation
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:
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
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.
27 #include <sys/types.h>
33 #include "glsl_parser_extras.h"
34 #include "glsl_parser.h"
35 #include "ir_optimization.h"
36 #include "ir_print_visitor.h"
39 extern "C" struct gl_shader *
40 _mesa_new_shader(GLcontext *ctx, GLuint name, GLenum type);
42 /* Copied from shader_api.c for the stand-alone compiler.
45 _mesa_new_shader(GLcontext *ctx, GLuint name, GLenum type)
47 struct gl_shader *shader;
48 assert(type == GL_FRAGMENT_SHADER || type == GL_VERTEX_SHADER);
49 shader = talloc_zero(NULL, struct gl_shader);
58 /* Returned string will have 'ctx' as its talloc owner. */
60 load_text_file(void *ctx, const char *file_name)
64 ssize_t total_read = 0;
65 int fd = open(file_name, O_RDONLY);
71 if (fstat(fd, & st) == 0) {
72 text = (char *) talloc_size(ctx, st.st_size + 1);
75 ssize_t bytes = read(fd, text + total_read,
76 st.st_size - total_read);
88 } while (total_read < st.st_size);
90 text[total_read] = '\0';
101 usage_fail(const char *name)
103 printf("%s <filename.frag|filename.vert>\n", name);
113 const struct option compiler_opts[] = {
114 { "dump-ast", 0, &dump_ast, 1 },
115 { "dump-hir", 0, &dump_hir, 1 },
116 { "dump-lir", 0, &dump_lir, 1 },
117 { "link", 0, &do_link, 1 },
122 compile_shader(struct gl_shader *shader)
124 struct _mesa_glsl_parse_state *state =
125 new(shader) _mesa_glsl_parse_state(NULL, shader->Type, shader);
127 const char *source = shader->Source;
128 state->error = preprocess(state, &source, &state->info_log,
132 _mesa_glsl_lexer_ctor(state, source);
133 _mesa_glsl_parse(state);
134 _mesa_glsl_lexer_dtor(state);
138 foreach_list_const(n, &state->translation_unit) {
139 ast_node *ast = exec_node_data(ast_node, n, link);
145 shader->ir = new(shader) exec_list;
146 if (!state->error && !state->translation_unit.is_empty())
147 _mesa_ast_to_hir(shader->ir, state);
149 /* Print out the unoptimized IR. */
150 if (!state->error && dump_hir) {
151 validate_ir_tree(shader->ir);
152 _mesa_print_ir(shader->ir, state);
155 /* Optimization passes */
156 if (!state->error && !shader->ir->is_empty()) {
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_algebraic(shader->ir) || progress;
169 progress = do_vec_index_to_swizzle(shader->ir) || progress;
170 progress = do_vec_index_to_cond_assign(shader->ir) || progress;
171 progress = do_swizzle_swizzle(shader->ir) || progress;
174 validate_ir_tree(shader->ir);
178 /* Print out the resulting IR */
179 if (!state->error && dump_lir) {
180 _mesa_print_ir(shader->ir, state);
183 shader->symbols = state->symbols;
184 shader->CompileStatus = !state->error;
185 shader->Version = state->language_version;
186 memcpy(shader->builtins_to_link, state->builtins_to_link,
187 sizeof(shader->builtins_to_link[0]) * state->num_builtins_to_link);
188 shader->num_builtins_to_link = state->num_builtins_to_link;
191 talloc_free(shader->InfoLog);
193 shader->InfoLog = state->info_log;
195 /* Retain any live IR, but trash the rest. */
196 reparent_ir(shader->ir, shader);
204 main(int argc, char **argv)
206 int status = EXIT_SUCCESS;
210 while ((c = getopt_long(argc, argv, "", compiler_opts, &idx)) != -1)
217 struct gl_shader_program *whole_program;
219 whole_program = talloc_zero (NULL, struct gl_shader_program);
220 assert(whole_program != NULL);
222 for (/* empty */; argc > optind; optind++) {
223 whole_program->Shaders = (struct gl_shader **)
224 talloc_realloc(whole_program, whole_program->Shaders,
225 struct gl_shader *, whole_program->NumShaders + 1);
226 assert(whole_program->Shaders != NULL);
228 struct gl_shader *shader = talloc_zero(whole_program, gl_shader);
230 whole_program->Shaders[whole_program->NumShaders] = shader;
231 whole_program->NumShaders++;
233 const unsigned len = strlen(argv[optind]);
237 const char *const ext = & argv[optind][len - 5];
238 if (strncmp(".vert", ext, 5) == 0)
239 shader->Type = GL_VERTEX_SHADER;
240 else if (strncmp(".geom", ext, 5) == 0)
241 shader->Type = GL_GEOMETRY_SHADER;
242 else if (strncmp(".frag", ext, 5) == 0)
243 shader->Type = GL_FRAGMENT_SHADER;
247 shader->Source = load_text_file(whole_program, argv[optind]);
248 if (shader->Source == NULL) {
249 printf("File \"%s\" does not exist.\n", argv[optind]);
253 compile_shader(shader);
255 if (!shader->CompileStatus) {
256 printf("Info log for %s:\n%s\n", argv[optind], shader->InfoLog);
257 status = EXIT_FAILURE;
262 if ((status == EXIT_SUCCESS) && do_link) {
263 link_shaders(whole_program);
264 status = (whole_program->LinkStatus) ? EXIT_SUCCESS : EXIT_FAILURE;
266 if (strlen(whole_program->InfoLog) > 0)
267 printf("Info log for linking:\n%s\n", whole_program->InfoLog);
270 for (unsigned i = 0; i < whole_program->_NumLinkedShaders; i++)
271 talloc_free(whole_program->_LinkedShaders[i]);
273 talloc_free(whole_program);
274 _mesa_glsl_release_types();
275 _mesa_glsl_release_functions();