]> git.tdb.fi Git - libs/gl.git/blob - tools/glslcompiler.cpp
Fix incorrect lookup of GLSL interface variables
[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         bool vulkan = false;
59         unsigned target_version = 0;
60
61         GetOpt getopt;
62         getopt.add_option('c', "combined", combined, GetOpt::NO_ARG).set_help("Output combined GLSL");
63         getopt.add_option('a', "dump-ast", dump_ast, GetOpt::NO_ARG).set_help("Dump AST for debugging");
64         getopt.add_option('p', "parse_only", parse_only, GetOpt::NO_ARG).set_help("Only parse the loaded source (implies -a)");
65         getopt.add_option('e', "specialize", spec_values_in, GetOpt::REQUIRED_ARG).set_help("Set specialization constant", "NAME:VALUE");
66         getopt.add_option('s', "stage", stage_str, GetOpt::REQUIRED_ARG).set_help("Output GLSL for STAGE", "STAGE");
67         getopt.add_option('m', "module", module_type, GetOpt::OPTIONAL_ARG).bind_seen_count(as_module).set_help("Compile as unspecialized module");
68         getopt.add_option("vulkan", vulkan, GetOpt::NO_ARG).set_help("Compile for Vulkan target");
69         getopt.add_option('t', "target-version", target_version, GetOpt::REQUIRED_ARG).set_help("Specify target GLSL version", "VER");
70         getopt.add_option('o', "out-file", out_filename, GetOpt::REQUIRED_ARG).set_help("Write output to file instead of stdout", "FILE");
71         getopt.add_option('I', "include", include_paths, GetOpt::REQUIRED_ARG).set_help("Add a directory to look for imported files", "DIR");
72         getopt.add_argument("source", source_fn, GetOpt::REQUIRED_ARG).set_help("GLSL file to compile");
73         getopt(argc, argv);
74
75         if(vulkan)
76         {
77                 features = GL::SL::Features::latest(GL::VULKAN);
78                 as_module = 1;
79                 module_type = "spirv";
80         }
81         else if(target_version)
82                 features = GL::SL::Features::from_api_version(GL::OPENGL, GL::Version(target_version/100, target_version%100));
83
84         if(as_module)
85         {
86                 if(module_type=="glsl" || module_type=="GLSL")
87                         compile_mode = GL::SL::Compiler::MODULE;
88                 else if(module_type=="spirv" || module_type=="spir-v" || module_type=="SPIRV" || module_type=="SPIR-V")
89                         compile_mode = GL::SL::Compiler::SPIRV;
90                 else
91                         throw usage_error("Invalid module type");
92         }
93
94         if(compile_mode==GL::SL::Compiler::SPIRV && out_filename.empty())
95                 throw usage_error("-o is required for SPIR-V");
96
97         if(parse_only)
98         {
99                 if(!stage_str.empty())
100                         throw usage_error("-s can't be used with -p");
101                 dump_ast = true;
102         }
103
104         if(!stage_str.empty() && compile_mode==GL::SL::Compiler::SPIRV)
105                 throw usage_error("-s can't be used with SPIR-V");
106         else if(stage_str=="vertex")
107                 stage = GL::SL::Stage::VERTEX;
108         else if(stage_str=="geometry")
109                 stage = GL::SL::Stage::GEOMETRY;
110         else if(stage_str=="fragment")
111                 stage = GL::SL::Stage::FRAGMENT;
112         else if(!dump_ast)
113                 combined = true;
114
115         if(!spec_values_in.empty() && as_module)
116                 throw usage_error("Modules can't be specialized");
117         for(vector<string>::const_iterator i=spec_values_in.begin(); i!=spec_values_in.end(); ++i)
118         {
119                 unsigned colon = i->find(':');
120                 if(colon==string::npos || colon==0 || colon+1>=i->size())
121                         throw usage_error("Invalid specialization value");
122
123                 string value_str = i->substr(colon+1);
124                 int value;
125                 if(isnumrc(value_str))
126                         value = lexical_cast<int>(value_str);
127                 else
128                         value = lexical_cast<bool>(value_str);
129                 spec_values[i->substr(0, colon)] = value;
130         }
131 }
132
133 int GlslCompiler::main()
134 {
135         Resources resources;
136         for(const string &p: include_paths)
137                 resources.add_include_path(p);
138         FS::Path shaderlib_path = FS::get_sys_data_dir()/"shaderlib";
139         if(FS::exists(shaderlib_path))
140                 resources.add_include_path(shaderlib_path);
141
142         GL::SL::Compiler compiler(features);
143         IO::File file(source_fn);
144         compiler.load_source(file, &resources, source_fn);
145         if(compile_mode==GL::SL::Compiler::PROGRAM)
146                 compiler.specialize(spec_values);
147         if(!parse_only)
148         {
149                 try
150                 {
151                         compiler.compile(compile_mode);
152                         string diag = compiler.get_diagnostics();
153                         if(!diag.empty())
154                                 IO::print("Diagnostic messages from compiler:\n%s\n", diag);
155                 }
156                 catch(const GL::SL::invalid_shader_source &exc)
157                 {
158                         if(!dump_ast)
159                                 throw;
160
161                         IO::print("Compilation resulted in errors:\n%s\n", exc.what());
162                         combined = false;
163                         stage = GL::SL::Stage::SHARED;
164                 }
165         }
166
167         if(dump_ast)
168         {
169                 vector<GL::SL::Stage::Type> stages = compiler.get_stages();
170                 for(vector<GL::SL::Stage::Type>::const_iterator i=stages.begin(); i!=stages.end(); ++i)
171                         IO::print("%s\n", compiler.get_stage_debug(*i, true));
172         }
173
174         IO::Base *out = &IO::cout;
175         RefPtr<IO::File> out_file;
176         if(!out_filename.empty())
177         {
178                 out_file = new IO::File(out_filename, IO::M_WRITE);
179                 out = out_file.get();
180         }
181
182         if(compile_mode==GL::SL::Compiler::SPIRV)
183         {
184                 vector<uint32_t> code = compiler.get_combined_spirv();
185                 out->write(reinterpret_cast<char *>(&code.front()), code.size()*4);
186         }
187         else if(combined)
188                 IO::print(*out, "%s\n", compiler.get_combined_glsl());
189         else if(stage!=GL::SL::Stage::SHARED)
190                 IO::print(*out, "%s\n", compiler.get_stage_glsl(stage));
191
192         return 0;
193 }
194
195
196 GlslCompiler::Resources::Resources()
197 {
198         add_source(source);
199 }
200
201 void GlslCompiler::Resources::add_include_path(const FS::Path &p)
202 {
203         source.add_directory(p);
204 }