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::opengl_latest()),
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";
58 unsigned target_version = 0;
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");
74 features = GL::SL::Features::from_opengl_version(GL::Version(target_version/100, target_version%100));
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;
83 throw usage_error("Invalid module type");
86 if(compile_mode==GL::SL::Compiler::SPIRV && out_filename.empty())
87 throw usage_error("-o is required for SPIR-V");
91 if(!stage_str.empty())
92 throw usage_error("-s can't be used with -p");
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;
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)
111 unsigned colon = i->find(':');
112 if(colon==string::npos || colon==0 || colon+1>=i->size())
113 throw usage_error("Invalid specialization value");
115 string value_str = i->substr(colon+1);
117 if(isnumrc(value_str))
118 value = lexical_cast<int>(value_str);
120 value = lexical_cast<bool>(value_str);
121 spec_values[i->substr(0, colon)] = value;
125 int GlslCompiler::main()
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);
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);
143 compiler.compile(compile_mode);
144 string diag = compiler.get_diagnostics();
146 IO::print("Diagnostic messages from compiler:\n%s\n", diag);
148 catch(const GL::SL::invalid_shader_source &exc)
153 IO::print("Compilation resulted in errors:\n%s\n", exc.what());
155 stage = GL::SL::Stage::SHARED;
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));
166 IO::Base *out = &IO::cout;
167 RefPtr<IO::File> out_file;
168 if(!out_filename.empty())
170 out_file = new IO::File(out_filename, IO::M_WRITE);
171 out = out_file.get();
174 if(compile_mode==GL::SL::Compiler::SPIRV)
176 vector<uint32_t> code = compiler.get_combined_spirv();
177 out->write(reinterpret_cast<char *>(&code.front()), code.size()*4);
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));
188 GlslCompiler::Resources::Resources()
193 void GlslCompiler::Resources::add_include_path(const FS::Path &p)
195 source.add_directory(p);