]> git.tdb.fi Git - libs/gl.git/blob - tools/glslcompiler.cpp
Make the SL::Features factory functions more flexible
[libs/gl.git] / tools / glslcompiler.cpp
1 #include <msp/core/application.h>
2 #include <msp/core/getopt.h>
3 #include <msp/datafile/collection.h>
4 #include <msp/datafile/directorysource.h>
5 #include <msp/fs/dir.h>
6 #include <msp/fs/stat.h>
7 #include <msp/gl/glsl/compiler.h>
8 #include <msp/gl/glsl/glsl_error.h>
9 #include <msp/io/print.h>
10 #include <msp/strings/utils.h>
11
12 class GlslCompiler: public Msp::RegisteredApplication<GlslCompiler>
13 {
14 private:
15         class Resources: public Msp::DataFile::Collection
16         {
17         private:
18                 Msp::DataFile::DirectorySource source;
19
20         public:
21                 Resources();
22
23                 void add_include_path(const Msp::FS::Path &);
24         };
25
26         std::string source_fn;
27         std::vector<std::string> include_paths;
28         Msp::GL::SL::Features features;
29         Msp::GL::SL::Compiler::Mode compile_mode;
30         std::map<std::string, int> spec_values;
31         bool parse_only;
32         bool combined;
33         Msp::GL::SL::Stage::Type stage;
34         bool dump_ast;
35         std::string out_filename;
36
37 public:
38         GlslCompiler(int, char **);
39
40         virtual int main();
41 };
42
43 using namespace std;
44 using namespace Msp;
45
46 GlslCompiler::GlslCompiler(int argc, char **argv):
47         features(GL::SL::Features::latest(GL::OPENGL)),
48         compile_mode(GL::SL::Compiler::PROGRAM),
49         parse_only(false),
50         combined(false),
51         stage(GL::SL::Stage::SHARED),
52         dump_ast(false)
53 {
54         string stage_str;
55         vector<string> spec_values_in;
56         unsigned as_module = 0;
57         string module_type = "glsl";
58         unsigned target_version = 0;
59
60         GetOpt getopt;
61         getopt.add_option('c', "combined", combined, GetOpt::NO_ARG).set_help("Output combined GLSL");
62         getopt.add_option('a', "dump-ast", dump_ast, GetOpt::NO_ARG).set_help("Dump AST for debugging");
63         getopt.add_option('p', "parse_only", parse_only, GetOpt::NO_ARG).set_help("Only parse the loaded source (implies -a)");
64         getopt.add_option('e', "specialize", spec_values_in, GetOpt::REQUIRED_ARG).set_help("Set specialization constant", "NAME:VALUE");
65         getopt.add_option('s', "stage", stage_str, GetOpt::REQUIRED_ARG).set_help("Output GLSL for STAGE", "STAGE");
66         getopt.add_option('m', "module", module_type, GetOpt::OPTIONAL_ARG).bind_seen_count(as_module).set_help("Compile as unspecialized module");
67         getopt.add_option('t', "target-version", target_version, GetOpt::REQUIRED_ARG).set_help("Specify target GLSL version", "VER");
68         getopt.add_option('o', "out-file", out_filename, GetOpt::REQUIRED_ARG).set_help("Write output to file instead of stdout", "FILE");
69         getopt.add_option('I', "include", include_paths, GetOpt::REQUIRED_ARG).set_help("Add a directory to look for imported files", "DIR");
70         getopt.add_argument("source", source_fn, GetOpt::REQUIRED_ARG).set_help("GLSL file to compile");
71         getopt(argc, argv);
72
73         if(target_version)
74                 features = GL::SL::Features::from_api_version(GL::OPENGL, GL::Version(target_version/100, target_version%100));
75
76         if(as_module)
77         {
78                 if(module_type=="glsl" || module_type=="GLSL")
79                         compile_mode = GL::SL::Compiler::MODULE;
80                 else if(module_type=="spirv" || module_type=="spir-v" || module_type=="SPIRV" || module_type=="SPIR-V")
81                         compile_mode = GL::SL::Compiler::SPIRV;
82                 else
83                         throw usage_error("Invalid module type");
84         }
85
86         if(compile_mode==GL::SL::Compiler::SPIRV && out_filename.empty())
87                 throw usage_error("-o is required for SPIR-V");
88
89         if(parse_only)
90         {
91                 if(!stage_str.empty())
92                         throw usage_error("-s can't be used with -p");
93                 dump_ast = true;
94         }
95
96         if(!stage_str.empty() && compile_mode==GL::SL::Compiler::SPIRV)
97                 throw usage_error("-s can't be used with SPIR-V");
98         else if(stage_str=="vertex")
99                 stage = GL::SL::Stage::VERTEX;
100         else if(stage_str=="geometry")
101                 stage = GL::SL::Stage::GEOMETRY;
102         else if(stage_str=="fragment")
103                 stage = GL::SL::Stage::FRAGMENT;
104         else if(!dump_ast)
105                 combined = true;
106
107         if(!spec_values_in.empty() && as_module)
108                 throw usage_error("Modules can't be specialized");
109         for(vector<string>::const_iterator i=spec_values_in.begin(); i!=spec_values_in.end(); ++i)
110         {
111                 unsigned colon = i->find(':');
112                 if(colon==string::npos || colon==0 || colon+1>=i->size())
113                         throw usage_error("Invalid specialization value");
114
115                 string value_str = i->substr(colon+1);
116                 int value;
117                 if(isnumrc(value_str))
118                         value = lexical_cast<int>(value_str);
119                 else
120                         value = lexical_cast<bool>(value_str);
121                 spec_values[i->substr(0, colon)] = value;
122         }
123 }
124
125 int GlslCompiler::main()
126 {
127         Resources resources;
128         for(const string &p: include_paths)
129                 resources.add_include_path(p);
130         FS::Path shaderlib_path = FS::get_sys_data_dir()/"shaderlib";
131         if(FS::exists(shaderlib_path))
132                 resources.add_include_path(shaderlib_path);
133
134         GL::SL::Compiler compiler(features);
135         IO::File file(source_fn);
136         compiler.load_source(file, &resources, source_fn);
137         if(compile_mode==GL::SL::Compiler::PROGRAM)
138                 compiler.specialize(spec_values);
139         if(!parse_only)
140         {
141                 try
142                 {
143                         compiler.compile(compile_mode);
144                         string diag = compiler.get_diagnostics();
145                         if(!diag.empty())
146                                 IO::print("Diagnostic messages from compiler:\n%s\n", diag);
147                 }
148                 catch(const GL::SL::invalid_shader_source &exc)
149                 {
150                         if(!dump_ast)
151                                 throw;
152
153                         IO::print("Compilation resulted in errors:\n%s\n", exc.what());
154                         combined = false;
155                         stage = GL::SL::Stage::SHARED;
156                 }
157         }
158
159         if(dump_ast)
160         {
161                 vector<GL::SL::Stage::Type> stages = compiler.get_stages();
162                 for(vector<GL::SL::Stage::Type>::const_iterator i=stages.begin(); i!=stages.end(); ++i)
163                         IO::print("%s\n", compiler.get_stage_debug(*i));
164         }
165
166         IO::Base *out = &IO::cout;
167         RefPtr<IO::File> out_file;
168         if(!out_filename.empty())
169         {
170                 out_file = new IO::File(out_filename, IO::M_WRITE);
171                 out = out_file.get();
172         }
173
174         if(compile_mode==GL::SL::Compiler::SPIRV)
175         {
176                 vector<uint32_t> code = compiler.get_combined_spirv();
177                 out->write(reinterpret_cast<char *>(&code.front()), code.size()*4);
178         }
179         else if(combined)
180                 IO::print(*out, "%s\n", compiler.get_combined_glsl());
181         else if(stage!=GL::SL::Stage::SHARED)
182                 IO::print(*out, "%s\n", compiler.get_stage_glsl(stage));
183
184         return 0;
185 }
186
187
188 GlslCompiler::Resources::Resources()
189 {
190         add_source(source);
191 }
192
193 void GlslCompiler::Resources::add_include_path(const FS::Path &p)
194 {
195         source.add_directory(p);
196 }