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