]> git.tdb.fi Git - libs/gl.git/blob - tools/glslcompiler.cpp
Make VertexFormat capable of storing type information
[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         unsigned as_module = 0;
41         string module_type = "glsl";
42         unsigned target_version = 0;
43
44         GetOpt getopt;
45         getopt.add_option('c', "combined", combined, GetOpt::NO_ARG).set_help("Output combined GLSL");
46         getopt.add_option('a', "dump-ast", dump_ast, GetOpt::NO_ARG).set_help("Dump AST for debugging");
47         getopt.add_option('p', "parse_only", parse_only, GetOpt::NO_ARG).set_help("Only parse the loaded source (implies -a)");
48         getopt.add_option('e', "specialize", spec_values_in, GetOpt::REQUIRED_ARG).set_help("Set specialization constant", "NAME:VALUE");
49         getopt.add_option('s', "stage", stage_str, GetOpt::REQUIRED_ARG).set_help("Output GLSL for STAGE", "STAGE");
50         getopt.add_option('m', "module", module_type, GetOpt::OPTIONAL_ARG).bind_seen_count(as_module).set_help("Compile as unspecialized module");
51         getopt.add_option('t', "target-version", target_version, GetOpt::REQUIRED_ARG).set_help("Specify target GLSL version", "VER");
52         getopt.add_option('o', "out-file", out_filename, GetOpt::REQUIRED_ARG).set_help("Write output to file instead of stdout", "FILE");
53         getopt.add_argument("source", source_fn, GetOpt::REQUIRED_ARG).set_help("GLSL file to compile");
54         getopt(argc, argv);
55
56         if(target_version)
57                 features = GL::SL::Features::from_version(GL::Version(target_version/100, target_version%100));
58
59         if(as_module)
60         {
61                 if(module_type=="glsl" || module_type=="GLSL")
62                         compile_mode = GL::SL::Compiler::MODULE;
63                 else if(module_type=="spirv" || module_type=="spir-v" || module_type=="SPIRV" || module_type=="SPIR-V")
64                         compile_mode = GL::SL::Compiler::SPIRV;
65                 else
66                         throw usage_error("Invalid module type");
67         }
68
69         if(compile_mode==GL::SL::Compiler::SPIRV && out_filename.empty())
70                 throw usage_error("-o is required for SPIR-V");
71
72         if(parse_only)
73         {
74                 if(!stage_str.empty())
75                         throw usage_error("-s can't be used with -p");
76                 dump_ast = true;
77         }
78
79         if(!stage_str.empty() && compile_mode==GL::SL::Compiler::SPIRV)
80                 throw usage_error("-s can't be used with SPIR-V");
81         else if(stage_str=="vertex")
82                 stage = GL::SL::Stage::VERTEX;
83         else if(stage_str=="geometry")
84                 stage = GL::SL::Stage::GEOMETRY;
85         else if(stage_str=="fragment")
86                 stage = GL::SL::Stage::FRAGMENT;
87         else if(!dump_ast)
88                 combined = true;
89
90         if(!spec_values_in.empty() && as_module)
91                 throw usage_error("Modules can't be specialized");
92         for(vector<string>::const_iterator i=spec_values_in.begin(); i!=spec_values_in.end(); ++i)
93         {
94                 unsigned colon = i->find(':');
95                 if(colon==string::npos || colon==0 || colon+1>=i->size())
96                         throw usage_error("Invalid specialization value");
97
98                 string value_str = i->substr(colon+1);
99                 int value;
100                 if(isnumrc(value_str))
101                         value = lexical_cast<int>(value_str);
102                 else
103                         value = lexical_cast<bool>(value_str);
104                 spec_values[i->substr(0, colon)] = value;
105         }
106 }
107
108 int GlslCompiler::main()
109 {
110         GL::SL::Compiler compiler(features);
111         IO::File file(source_fn);
112         compiler.load_source(file, source_fn);
113         if(compile_mode==GL::SL::Compiler::PROGRAM)
114                 compiler.specialize(spec_values);
115         if(!parse_only)
116         {
117                 try
118                 {
119                         compiler.compile(compile_mode);
120                         string diag = compiler.get_diagnostics();
121                         if(!diag.empty())
122                                 IO::print("Diagnostic messages from compiler:\n%s\n", diag);
123                 }
124                 catch(const GL::SL::invalid_shader_source &exc)
125                 {
126                         if(!dump_ast)
127                                 throw;
128
129                         IO::print("Compilation resulted in errors:\n%s\n", exc.what());
130                         combined = false;
131                         stage = GL::SL::Stage::SHARED;
132                 }
133         }
134
135         if(dump_ast)
136         {
137                 vector<GL::SL::Stage::Type> stages = compiler.get_stages();
138                 for(vector<GL::SL::Stage::Type>::const_iterator i=stages.begin(); i!=stages.end(); ++i)
139                         IO::print("%s\n", compiler.get_stage_debug(*i));
140         }
141
142         IO::Base *out = &IO::cout;
143         RefPtr<IO::File> out_file;
144         if(!out_filename.empty())
145         {
146                 out_file = new IO::File(out_filename, IO::M_WRITE);
147                 out = out_file.get();
148         }
149
150         if(compile_mode==GL::SL::Compiler::SPIRV)
151         {
152                 vector<UInt32> code = compiler.get_combined_spirv();
153                 out->write(reinterpret_cast<char *>(&code.front()), code.size()*4);
154         }
155         else if(combined)
156                 IO::print(*out, "%s\n", compiler.get_combined_glsl());
157         else if(stage!=GL::SL::Stage::SHARED)
158                 IO::print(*out, "%s\n", compiler.get_stage_glsl(stage));
159
160         return 0;
161 }