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>
12 class GlslCompiler: public Msp::RegisteredApplication<GlslCompiler>
15 class Resources: public Msp::DataFile::Collection
18 Msp::DataFile::DirectorySource source;
23 void add_include_path(const Msp::FS::Path &);
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;
33 Msp::GL::SL::Stage::Type stage;
35 std::string out_filename;
38 GlslCompiler(int, char **);
46 GlslCompiler::GlslCompiler(int argc, char **argv):
47 features(GL::SL::Features::latest(GL::OPENGL)),
48 compile_mode(GL::SL::Compiler::PROGRAM),
51 stage(GL::SL::Stage::SHARED),
55 vector<string> spec_values_in;
56 unsigned as_module = 0;
57 string module_type = "glsl";
59 unsigned target_version = 0;
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");
77 features = GL::SL::Features::latest(GL::VULKAN);
79 module_type = "spirv";
81 else if(target_version)
82 features = GL::SL::Features::from_api_version(GL::OPENGL, GL::Version(target_version/100, target_version%100));
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;
91 throw usage_error("Invalid module type");
94 if(compile_mode==GL::SL::Compiler::SPIRV && out_filename.empty())
95 throw usage_error("-o is required for SPIR-V");
99 if(!stage_str.empty())
100 throw usage_error("-s can't be used with -p");
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;
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)
119 unsigned colon = i->find(':');
120 if(colon==string::npos || colon==0 || colon+1>=i->size())
121 throw usage_error("Invalid specialization value");
123 string value_str = i->substr(colon+1);
125 if(isnumrc(value_str))
126 value = lexical_cast<int>(value_str);
128 value = lexical_cast<bool>(value_str);
129 spec_values[i->substr(0, colon)] = value;
133 int GlslCompiler::main()
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);
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);
151 compiler.compile(compile_mode);
152 string diag = compiler.get_diagnostics();
154 IO::print("Diagnostic messages from compiler:\n%s\n", diag);
156 catch(const GL::SL::invalid_shader_source &exc)
161 IO::print("Compilation resulted in errors:\n%s\n", exc.what());
163 stage = GL::SL::Stage::SHARED;
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));
174 IO::Base *out = &IO::cout;
175 RefPtr<IO::File> out_file;
176 if(!out_filename.empty())
178 out_file = new IO::File(out_filename, IO::M_WRITE);
179 out = out_file.get();
182 if(compile_mode==GL::SL::Compiler::SPIRV)
184 vector<uint32_t> code = compiler.get_combined_spirv();
185 out->write(reinterpret_cast<char *>(&code.front()), code.size()*4);
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));
196 GlslCompiler::Resources::Resources()
201 void GlslCompiler::Resources::add_include_path(const FS::Path &p)
203 source.add_directory(p);