]> git.tdb.fi Git - libs/gl.git/blob - tools/glslcompiler.cpp
Add a target version option to the command-line GLSL compiler
[libs/gl.git] / tools / glslcompiler.cpp
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>
7
8 class GlslCompiler: public Msp::RegisteredApplication<GlslCompiler>
9 {
10 private:
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;
15         bool parse_only;
16         bool combined;
17         Msp::GL::SL::Stage::Type stage;
18         bool dump_ast;
19
20 public:
21         GlslCompiler(int, char **);
22
23         virtual int main();
24 };
25
26 using namespace std;
27 using namespace Msp;
28
29 GlslCompiler::GlslCompiler(int argc, char **argv):
30         features(GL::SL::Features::latest()),
31         compile_mode(GL::SL::Compiler::PROGRAM),
32         parse_only(false),
33         combined(false),
34         stage(GL::SL::Stage::SHARED),
35         dump_ast(false)
36 {
37         string stage_str;
38         vector<string> spec_values_in;
39         bool as_module = false;
40         unsigned target_version = 0;
41
42         GetOpt getopt;
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");
51         getopt(argc, argv);
52
53         if(target_version)
54                 features = GL::SL::Features::from_version(GL::Version(target_version/100, target_version%100));
55
56         if(as_module)
57                 compile_mode = GL::SL::Compiler::MODULE;
58
59         if(parse_only)
60         {
61                 if(!stage_str.empty())
62                         throw usage_error("-s can't be used with -p");
63                 dump_ast = true;
64         }
65
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;
72         else if(!dump_ast)
73                 combined = true;
74
75         for(vector<string>::const_iterator i=spec_values_in.begin(); i!=spec_values_in.end(); ++i)
76         {
77                 unsigned colon = i->find(':');
78                 if(colon==string::npos || colon==0 || colon+1>=i->size())
79                         throw usage_error("Invalid specialization value");
80
81                 string value_str = i->substr(colon+1);
82                 int value;
83                 if(isnumrc(value_str))
84                         value = lexical_cast<int>(value_str);
85                 else
86                         value = lexical_cast<bool>(value_str);
87                 spec_values[i->substr(0, colon)] = value;
88         }
89 }
90
91 int GlslCompiler::main()
92 {
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);
98         if(!parse_only)
99         {
100                 try
101                 {
102                         compiler.compile(compile_mode);
103                         string diag = compiler.get_diagnostics();
104                         if(!diag.empty())
105                                 IO::print("Diagnostic messages from compiler:\n%s\n", diag);
106                 }
107                 catch(const GL::SL::invalid_shader_source &exc)
108                 {
109                         if(!dump_ast)
110                                 throw;
111
112                         IO::print("Compilation resulted in errors:\n%s\n", exc.what());
113                         combined = false;
114                         stage = GL::SL::Stage::SHARED;
115                 }
116         }
117
118         if(dump_ast)
119         {
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));
123         }
124
125         if(combined)
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));
129
130         return 0;
131 }