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