]> git.tdb.fi Git - libs/gl.git/blobdiff - tools/glslcompiler.cpp
Disallow specializing when compiling a module
[libs/gl.git] / tools / glslcompiler.cpp
index 5d8362d4289a6f9f1bdd5c78eb2b64c3704a64db..a1eb1a1fa4d87ddd77c71c47acb0fca563c5b309 100644 (file)
@@ -9,12 +9,14 @@ class GlslCompiler: public Msp::RegisteredApplication<GlslCompiler>
 {
 private:
        std::string source_fn;
+       Msp::GL::SL::Features features;
        Msp::GL::SL::Compiler::Mode compile_mode;
        std::map<std::string, int> spec_values;
        bool parse_only;
        bool combined;
        Msp::GL::SL::Stage::Type stage;
        bool dump_ast;
+       std::string out_filename;
 
 public:
        GlslCompiler(int, char **);
@@ -26,6 +28,7 @@ using namespace std;
 using namespace Msp;
 
 GlslCompiler::GlslCompiler(int argc, char **argv):
+       features(GL::SL::Features::latest()),
        compile_mode(GL::SL::Compiler::PROGRAM),
        parse_only(false),
        combined(false),
@@ -35,6 +38,7 @@ GlslCompiler::GlslCompiler(int argc, char **argv):
        string stage_str;
        vector<string> spec_values_in;
        bool as_module = false;
+       unsigned target_version = 0;
 
        GetOpt getopt;
        getopt.add_option('c', "combined", combined, GetOpt::NO_ARG).set_help("Output combined GLSL");
@@ -43,9 +47,14 @@ GlslCompiler::GlslCompiler(int argc, char **argv):
        getopt.add_option('e', "specialize", spec_values_in, GetOpt::REQUIRED_ARG).set_help("Set specialization constant", "NAME:VALUE");
        getopt.add_option('s', "stage", stage_str, GetOpt::REQUIRED_ARG).set_help("Output GLSL for STAGE", "STAGE");
        getopt.add_option('m', "module", as_module, GetOpt::NO_ARG).set_help("Compile as unspecialized module");
+       getopt.add_option('t', "target-version", target_version, GetOpt::REQUIRED_ARG).set_help("Specify target GLSL version", "VER");
+       getopt.add_option('o', "out-file", out_filename, GetOpt::REQUIRED_ARG).set_help("Write output to file instead of stdout", "FILE");
        getopt.add_argument("source", source_fn, GetOpt::REQUIRED_ARG).set_help("GLSL file to compile");
        getopt(argc, argv);
 
+       if(target_version)
+               features = GL::SL::Features::from_version(GL::Version(target_version/100, target_version%100));
+
        if(as_module)
                compile_mode = GL::SL::Compiler::MODULE;
 
@@ -65,6 +74,8 @@ GlslCompiler::GlslCompiler(int argc, char **argv):
        else if(!dump_ast)
                combined = true;
 
+       if(!spec_values_in.empty() && as_module)
+               throw usage_error("Modules can't be specialized");
        for(vector<string>::const_iterator i=spec_values_in.begin(); i!=spec_values_in.end(); ++i)
        {
                unsigned colon = i->find(':');
@@ -83,7 +94,7 @@ GlslCompiler::GlslCompiler(int argc, char **argv):
 
 int GlslCompiler::main()
 {
-       GL::SL::Compiler compiler(GL::SL::Features::all());
+       GL::SL::Compiler compiler(features);
        IO::File file(source_fn);
        compiler.load_source(file, source_fn);
        if(compile_mode==GL::SL::Compiler::PROGRAM)
@@ -115,10 +126,18 @@ int GlslCompiler::main()
                        IO::print("%s\n", compiler.get_stage_debug(*i));
        }
 
+       IO::Base *out = &IO::cout;
+       RefPtr<IO::File> out_file;
+       if(!out_filename.empty())
+       {
+               out_file = new IO::File(out_filename, IO::M_WRITE);
+               out = out_file.get();
+       }
+
        if(combined)
-               IO::print("%s\n", compiler.get_combined_glsl());
+               IO::print(*out, "%s\n", compiler.get_combined_glsl());
        else if(stage!=GL::SL::Stage::SHARED)
-               IO::print("%s\n", compiler.get_stage_glsl(stage));
+               IO::print(*out, "%s\n", compiler.get_stage_glsl(stage));
 
        return 0;
 }