]> git.tdb.fi Git - libs/gl.git/blob - tools/glslcompiler.cpp
Disallow specializing when compiling a module
[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         if(!spec_values_in.empty() && as_module)
78                 throw usage_error("Modules can't be specialized");
79         for(vector<string>::const_iterator i=spec_values_in.begin(); i!=spec_values_in.end(); ++i)
80         {
81                 unsigned colon = i->find(':');
82                 if(colon==string::npos || colon==0 || colon+1>=i->size())
83                         throw usage_error("Invalid specialization value");
84
85                 string value_str = i->substr(colon+1);
86                 int value;
87                 if(isnumrc(value_str))
88                         value = lexical_cast<int>(value_str);
89                 else
90                         value = lexical_cast<bool>(value_str);
91                 spec_values[i->substr(0, colon)] = value;
92         }
93 }
94
95 int GlslCompiler::main()
96 {
97         GL::SL::Compiler compiler(features);
98         IO::File file(source_fn);
99         compiler.load_source(file, source_fn);
100         if(compile_mode==GL::SL::Compiler::PROGRAM)
101                 compiler.specialize(spec_values);
102         if(!parse_only)
103         {
104                 try
105                 {
106                         compiler.compile(compile_mode);
107                         string diag = compiler.get_diagnostics();
108                         if(!diag.empty())
109                                 IO::print("Diagnostic messages from compiler:\n%s\n", diag);
110                 }
111                 catch(const GL::SL::invalid_shader_source &exc)
112                 {
113                         if(!dump_ast)
114                                 throw;
115
116                         IO::print("Compilation resulted in errors:\n%s\n", exc.what());
117                         combined = false;
118                         stage = GL::SL::Stage::SHARED;
119                 }
120         }
121
122         if(dump_ast)
123         {
124                 vector<GL::SL::Stage::Type> stages = compiler.get_stages();
125                 for(vector<GL::SL::Stage::Type>::const_iterator i=stages.begin(); i!=stages.end(); ++i)
126                         IO::print("%s\n", compiler.get_stage_debug(*i));
127         }
128
129         IO::Base *out = &IO::cout;
130         RefPtr<IO::File> out_file;
131         if(!out_filename.empty())
132         {
133                 out_file = new IO::File(out_filename, IO::M_WRITE);
134                 out = out_file.get();
135         }
136
137         if(combined)
138                 IO::print(*out, "%s\n", compiler.get_combined_glsl());
139         else if(stage!=GL::SL::Stage::SHARED)
140                 IO::print(*out, "%s\n", compiler.get_stage_glsl(stage));
141
142         return 0;
143 }