]> git.tdb.fi Git - libs/gl.git/blob - tools/glslcompiler.cpp
Add a standalone GLSL compiler frontend
[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/io/print.h>
5
6 class GlslCompiler: public Msp::RegisteredApplication<GlslCompiler>
7 {
8 private:
9         std::string source_fn;
10         bool parse_only;
11         bool combined;
12         Msp::GL::SL::Stage::Type stage;
13         bool dump_ast;
14
15 public:
16         GlslCompiler(int, char **);
17
18         virtual int main();
19 };
20
21 using namespace std;
22 using namespace Msp;
23
24 GlslCompiler::GlslCompiler(int argc, char **argv):
25         parse_only(false),
26         combined(false),
27         stage(GL::SL::Stage::SHARED),
28         dump_ast(false)
29 {
30         string stage_str;
31
32         GetOpt getopt;
33         getopt.add_option('c', "combined", combined, GetOpt::NO_ARG).set_help("Output combined GLSL");
34         getopt.add_option('a', "dump-ast", dump_ast, GetOpt::NO_ARG).set_help("Dump AST for debugging");
35         getopt.add_option('p', "parse_only", parse_only, GetOpt::NO_ARG).set_help("Only parse the loaded source, don't compile");
36         getopt.add_option('s', "stage", stage_str, GetOpt::REQUIRED_ARG).set_help("Output GLSL for STAGE", "STAGE");
37         getopt.add_argument("source", source_fn, GetOpt::REQUIRED_ARG).set_help("GLSL file to compile");
38         getopt(argc, argv);
39
40         if(stage_str=="vertex")
41                 stage = GL::SL::Stage::VERTEX;
42         else if(stage_str=="geometry")
43                 stage = GL::SL::Stage::GEOMETRY;
44         else if(stage_str=="fragment")
45                 stage = GL::SL::Stage::FRAGMENT;
46         else if(!dump_ast)
47                 combined = true;
48 }
49
50 int GlslCompiler::main()
51 {
52         GL::SL::Compiler compiler(GL::SL::Features::all());
53         IO::File file(source_fn);
54         compiler.load_source(file, source_fn);
55         if(!parse_only)
56                 compiler.compile(GL::SL::Compiler::PROGRAM);
57
58         if(dump_ast)
59         {
60                 vector<GL::SL::Stage::Type> stages = compiler.get_stages();
61                 for(vector<GL::SL::Stage::Type>::const_iterator i=stages.begin(); i!=stages.end(); ++i)
62                         IO::print("%s\n", compiler.get_stage_debug(*i));
63         }
64
65         if(combined)
66                 IO::print("%s\n", compiler.get_combined_glsl());
67         else if(stage!=GL::SL::Stage::SHARED)
68                 IO::print("%s\n", compiler.get_stage_glsl(stage));
69
70         return 0;
71 }