]> git.tdb.fi Git - libs/gl.git/blob - tools/glslcompiler.cpp
Add a compile mode flag 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::Compiler::Mode compile_mode;
13         std::map<std::string, int> spec_values;
14         bool parse_only;
15         bool combined;
16         Msp::GL::SL::Stage::Type stage;
17         bool dump_ast;
18
19 public:
20         GlslCompiler(int, char **);
21
22         virtual int main();
23 };
24
25 using namespace std;
26 using namespace Msp;
27
28 GlslCompiler::GlslCompiler(int argc, char **argv):
29         compile_mode(GL::SL::Compiler::PROGRAM),
30         parse_only(false),
31         combined(false),
32         stage(GL::SL::Stage::SHARED),
33         dump_ast(false)
34 {
35         string stage_str;
36         vector<string> spec_values_in;
37         bool as_module = false;
38
39         GetOpt getopt;
40         getopt.add_option('c', "combined", combined, GetOpt::NO_ARG).set_help("Output combined GLSL");
41         getopt.add_option('a', "dump-ast", dump_ast, GetOpt::NO_ARG).set_help("Dump AST for debugging");
42         getopt.add_option('p', "parse_only", parse_only, GetOpt::NO_ARG).set_help("Only parse the loaded source (implies -a)");
43         getopt.add_option('e', "specialize", spec_values_in, GetOpt::REQUIRED_ARG).set_help("Set specialization constant", "NAME:VALUE");
44         getopt.add_option('s', "stage", stage_str, GetOpt::REQUIRED_ARG).set_help("Output GLSL for STAGE", "STAGE");
45         getopt.add_option('m', "module", as_module, GetOpt::NO_ARG).set_help("Compile as unspecialized module");
46         getopt.add_argument("source", source_fn, GetOpt::REQUIRED_ARG).set_help("GLSL file to compile");
47         getopt(argc, argv);
48
49         if(as_module)
50                 compile_mode = GL::SL::Compiler::MODULE;
51
52         if(parse_only)
53         {
54                 if(!stage_str.empty())
55                         throw usage_error("-s can't be used with -p");
56                 dump_ast = true;
57         }
58
59         if(stage_str=="vertex")
60                 stage = GL::SL::Stage::VERTEX;
61         else if(stage_str=="geometry")
62                 stage = GL::SL::Stage::GEOMETRY;
63         else if(stage_str=="fragment")
64                 stage = GL::SL::Stage::FRAGMENT;
65         else if(!dump_ast)
66                 combined = true;
67
68         for(vector<string>::const_iterator i=spec_values_in.begin(); i!=spec_values_in.end(); ++i)
69         {
70                 unsigned colon = i->find(':');
71                 if(colon==string::npos || colon==0 || colon+1>=i->size())
72                         throw usage_error("Invalid specialization value");
73
74                 string value_str = i->substr(colon+1);
75                 int value;
76                 if(isnumrc(value_str))
77                         value = lexical_cast<int>(value_str);
78                 else
79                         value = lexical_cast<bool>(value_str);
80                 spec_values[i->substr(0, colon)] = value;
81         }
82 }
83
84 int GlslCompiler::main()
85 {
86         GL::SL::Compiler compiler(GL::SL::Features::all());
87         IO::File file(source_fn);
88         compiler.load_source(file, source_fn);
89         if(compile_mode==GL::SL::Compiler::PROGRAM)
90                 compiler.specialize(spec_values);
91         if(!parse_only)
92         {
93                 try
94                 {
95                         compiler.compile(compile_mode);
96                         string diag = compiler.get_diagnostics();
97                         if(!diag.empty())
98                                 IO::print("Diagnostic messages from compiler:\n%s\n", diag);
99                 }
100                 catch(const GL::SL::invalid_shader_source &exc)
101                 {
102                         if(!dump_ast)
103                                 throw;
104
105                         IO::print("Compilation resulted in errors:\n%s\n", exc.what());
106                         combined = false;
107                         stage = GL::SL::Stage::SHARED;
108                 }
109         }
110
111         if(dump_ast)
112         {
113                 vector<GL::SL::Stage::Type> stages = compiler.get_stages();
114                 for(vector<GL::SL::Stage::Type>::const_iterator i=stages.begin(); i!=stages.end(); ++i)
115                         IO::print("%s\n", compiler.get_stage_debug(*i));
116         }
117
118         if(combined)
119                 IO::print("%s\n", compiler.get_combined_glsl());
120         else if(stage!=GL::SL::Stage::SHARED)
121                 IO::print("%s\n", compiler.get_stage_glsl(stage));
122
123         return 0;
124 }