1 #include <msp/core/application.h>
2 #include <msp/core/getopt.h>
3 #include <msp/gl/glsl/compiler.h>
4 #include <msp/gl/glsl/glsl_error.h>
5 #include <msp/io/print.h>
6 #include <msp/strings/utils.h>
8 class GlslCompiler: public Msp::RegisteredApplication<GlslCompiler>
11 std::string source_fn;
12 Msp::GL::SL::Features features;
13 Msp::GL::SL::Compiler::Mode compile_mode;
14 std::map<std::string, int> spec_values;
17 Msp::GL::SL::Stage::Type stage;
21 GlslCompiler(int, char **);
29 GlslCompiler::GlslCompiler(int argc, char **argv):
30 features(GL::SL::Features::latest()),
31 compile_mode(GL::SL::Compiler::PROGRAM),
34 stage(GL::SL::Stage::SHARED),
38 vector<string> spec_values_in;
39 bool as_module = false;
40 unsigned target_version = 0;
43 getopt.add_option('c', "combined", combined, GetOpt::NO_ARG).set_help("Output combined GLSL");
44 getopt.add_option('a', "dump-ast", dump_ast, GetOpt::NO_ARG).set_help("Dump AST for debugging");
45 getopt.add_option('p', "parse_only", parse_only, GetOpt::NO_ARG).set_help("Only parse the loaded source (implies -a)");
46 getopt.add_option('e', "specialize", spec_values_in, GetOpt::REQUIRED_ARG).set_help("Set specialization constant", "NAME:VALUE");
47 getopt.add_option('s', "stage", stage_str, GetOpt::REQUIRED_ARG).set_help("Output GLSL for STAGE", "STAGE");
48 getopt.add_option('m', "module", as_module, GetOpt::NO_ARG).set_help("Compile as unspecialized module");
49 getopt.add_option('t', "target-version", target_version, GetOpt::REQUIRED_ARG).set_help("Specify target GLSL version", "VER");
50 getopt.add_argument("source", source_fn, GetOpt::REQUIRED_ARG).set_help("GLSL file to compile");
54 features = GL::SL::Features::from_version(GL::Version(target_version/100, target_version%100));
57 compile_mode = GL::SL::Compiler::MODULE;
61 if(!stage_str.empty())
62 throw usage_error("-s can't be used with -p");
66 if(stage_str=="vertex")
67 stage = GL::SL::Stage::VERTEX;
68 else if(stage_str=="geometry")
69 stage = GL::SL::Stage::GEOMETRY;
70 else if(stage_str=="fragment")
71 stage = GL::SL::Stage::FRAGMENT;
75 for(vector<string>::const_iterator i=spec_values_in.begin(); i!=spec_values_in.end(); ++i)
77 unsigned colon = i->find(':');
78 if(colon==string::npos || colon==0 || colon+1>=i->size())
79 throw usage_error("Invalid specialization value");
81 string value_str = i->substr(colon+1);
83 if(isnumrc(value_str))
84 value = lexical_cast<int>(value_str);
86 value = lexical_cast<bool>(value_str);
87 spec_values[i->substr(0, colon)] = value;
91 int GlslCompiler::main()
93 GL::SL::Compiler compiler(features);
94 IO::File file(source_fn);
95 compiler.load_source(file, source_fn);
96 if(compile_mode==GL::SL::Compiler::PROGRAM)
97 compiler.specialize(spec_values);
102 compiler.compile(compile_mode);
103 string diag = compiler.get_diagnostics();
105 IO::print("Diagnostic messages from compiler:\n%s\n", diag);
107 catch(const GL::SL::invalid_shader_source &exc)
112 IO::print("Compilation resulted in errors:\n%s\n", exc.what());
114 stage = GL::SL::Stage::SHARED;
120 vector<GL::SL::Stage::Type> stages = compiler.get_stages();
121 for(vector<GL::SL::Stage::Type>::const_iterator i=stages.begin(); i!=stages.end(); ++i)
122 IO::print("%s\n", compiler.get_stage_debug(*i));
126 IO::print("%s\n", compiler.get_combined_glsl());
127 else if(stage!=GL::SL::Stage::SHARED)
128 IO::print("%s\n", compiler.get_stage_glsl(stage));