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>
8 class GlslCompiler: public Msp::RegisteredApplication<GlslCompiler>
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;
17 Msp::GL::SL::Stage::Type stage;
19 std::string out_filename;
22 GlslCompiler(int, char **);
30 GlslCompiler::GlslCompiler(int argc, char **argv):
31 features(GL::SL::Features::latest()),
32 compile_mode(GL::SL::Compiler::PROGRAM),
35 stage(GL::SL::Stage::SHARED),
39 vector<string> spec_values_in;
40 unsigned as_module = 0;
41 string module_type = "glsl";
42 unsigned target_version = 0;
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");
57 features = GL::SL::Features::from_version(GL::Version(target_version/100, target_version%100));
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;
66 throw usage_error("Invalid module type");
69 if(compile_mode==GL::SL::Compiler::SPIRV && out_filename.empty())
70 throw usage_error("-o is required for SPIR-V");
74 if(!stage_str.empty())
75 throw usage_error("-s can't be used with -p");
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;
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)
94 unsigned colon = i->find(':');
95 if(colon==string::npos || colon==0 || colon+1>=i->size())
96 throw usage_error("Invalid specialization value");
98 string value_str = i->substr(colon+1);
100 if(isnumrc(value_str))
101 value = lexical_cast<int>(value_str);
103 value = lexical_cast<bool>(value_str);
104 spec_values[i->substr(0, colon)] = value;
108 int GlslCompiler::main()
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);
119 compiler.compile(compile_mode);
120 string diag = compiler.get_diagnostics();
122 IO::print("Diagnostic messages from compiler:\n%s\n", diag);
124 catch(const GL::SL::invalid_shader_source &exc)
129 IO::print("Compilation resulted in errors:\n%s\n", exc.what());
131 stage = GL::SL::Stage::SHARED;
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));
142 IO::Base *out = &IO::cout;
143 RefPtr<IO::File> out_file;
144 if(!out_filename.empty())
146 out_file = new IO::File(out_filename, IO::M_WRITE);
147 out = out_file.get();
150 if(compile_mode==GL::SL::Compiler::SPIRV)
152 vector<uint32_t> code = compiler.get_combined_spirv();
153 out->write(reinterpret_cast<char *>(&code.front()), code.size()*4);
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));