From 8ce55dbd9207ff4338588dcba155f1a754dc8f19 Mon Sep 17 00:00:00 2001 From: Ian Romanick Date: Thu, 17 Jun 2010 12:01:18 -0700 Subject: [PATCH] Extract compilation to a separate routine Pull all of the code that actually compiles shaders into a separate function. Use a glsl_program to track data about the compiled shader. --- main.cpp | 110 +++++++++++++++++++++++++++++++------------------------ 1 file changed, 63 insertions(+), 47 deletions(-) diff --git a/main.cpp b/main.cpp index 662686373ad..ae564e860dd 100644 --- a/main.cpp +++ b/main.cpp @@ -34,6 +34,7 @@ #include "glsl_parser.h" #include "ir_optimization.h" #include "ir_print_visitor.h" +#include "program.h" static char * @@ -96,42 +97,18 @@ const struct option compiler_opts[] = { { NULL, 0, NULL, 0 } }; -int -main(int argc, char **argv) +void +compile_shader(struct glsl_program *prog) { struct _mesa_glsl_parse_state state; - char *shader; - size_t shader_len; - exec_list instructions; - - int c; - int idx = 0; - while ((c = getopt_long(argc, argv, "", compiler_opts, &idx)) != -1) - /* empty */ ; - - - if (argc <= optind) - usage_fail(argv[0]); - - const unsigned len = strlen(argv[optind]); - if (len < 6) - usage_fail(argv[0]); - - const char *const ext = & argv[optind][len - 5]; - enum _mesa_glsl_parser_targets target; - if (strncmp(".vert", ext, 5) == 0) - target = vertex_shader; - else if (strncmp(".geom", ext, 5) == 0) - target = geometry_shader; - else if (strncmp(".frag", ext, 5) == 0) - target = fragment_shader; - else - usage_fail(argv[0]); - - shader = load_text_file(argv[optind], & shader_len); memset(& state, 0, sizeof(state)); - state.target = target; + switch (prog->Type) { + case GL_VERTEX_SHADER: state.target = vertex_shader; break; + case GL_FRAGMENT_SHADER: state.target = fragment_shader; break; + case GL_GEOMETRY_SHADER: state.target = geometry_shader; break; + } + state.scanner = NULL; state.translation_unit.make_empty(); state.symbols = new glsl_symbol_table; @@ -140,7 +117,7 @@ main(int argc, char **argv) state.loop_or_switch_nesting = NULL; state.ARB_texture_rectangle_enable = true; - _mesa_glsl_lexer_ctor(& state, shader, shader_len); + _mesa_glsl_lexer_ctor(& state, prog->Source, prog->SourceLen); _mesa_glsl_parse(& state); _mesa_glsl_lexer_dtor(& state); @@ -152,33 +129,72 @@ main(int argc, char **argv) printf("\n\n"); } + prog->ir.make_empty(); if (!state.error && !state.translation_unit.is_empty()) - _mesa_ast_to_hir(&instructions, &state); + _mesa_ast_to_hir(&prog->ir, &state); /* Optimization passes */ - if (!state.error && !instructions.is_empty()) { + if (!state.error && !prog->ir.is_empty()) { bool progress; do { progress = false; - progress = do_function_inlining(&instructions) || progress; - progress = do_if_simplification(&instructions) || progress; - progress = do_copy_propagation(&instructions) || progress; - progress = do_dead_code_local(&instructions) || progress; - progress = do_dead_code_unlinked(&instructions) || progress; - progress = do_constant_variable_unlinked(&instructions) || progress; - progress = do_constant_folding(&instructions) || progress; - progress = do_vec_index_to_swizzle(&instructions) || progress; - progress = do_swizzle_swizzle(&instructions) || progress; + progress = do_function_inlining(&prog->ir) || progress; + progress = do_if_simplification(&prog->ir) || progress; + progress = do_copy_propagation(&prog->ir) || progress; + progress = do_dead_code_local(&prog->ir) || progress; + progress = do_dead_code_unlinked(&prog->ir) || progress; + progress = do_constant_variable_unlinked(&prog->ir) || progress; + progress = do_constant_folding(&prog->ir) || progress; + progress = do_vec_index_to_swizzle(&prog->ir) || progress; + progress = do_swizzle_swizzle(&prog->ir) || progress; } while (progress); } /* Print out the resulting IR */ if (!state.error && dump_lir) { - _mesa_print_ir(&instructions, &state); + _mesa_print_ir(&prog->ir, &state); } - delete state.symbols; + prog->symbols = state.symbols; + prog->CompileStatus = !state.error; + return; +} + +int +main(int argc, char **argv) +{ + struct _mesa_glsl_parse_state state; + + int c; + int idx = 0; + while ((c = getopt_long(argc, argv, "", compiler_opts, &idx)) != -1) + /* empty */ ; + + + if (argc <= optind) + usage_fail(argv[0]); + + struct glsl_program *prog = new glsl_program; + memset(prog, 0, sizeof(*prog)); + + const unsigned len = strlen(argv[optind]); + if (len < 6) + usage_fail(argv[0]); + + const char *const ext = & argv[optind][len - 5]; + if (strncmp(".vert", ext, 5) == 0) + prog->Type = GL_VERTEX_SHADER; + else if (strncmp(".geom", ext, 5) == 0) + prog->Type = GL_GEOMETRY_SHADER; + else if (strncmp(".frag", ext, 5) == 0) + prog->Type = GL_FRAGMENT_SHADER; + else + usage_fail(argv[0]); + + prog->Source = load_text_file(argv[optind], &prog->SourceLen); + + compile_shader(prog); - return state.error != 0; + return prog->CompileStatus ? EXIT_SUCCESS : EXIT_FAILURE; } -- 2.30.2