]> git.tdb.fi Git - libs/gl.git/blobdiff - source/glsl/compiler.cpp
Disallow specializing when compiling a module
[libs/gl.git] / source / glsl / compiler.cpp
index e6c035775a89b0ff1aaa98464755d031a73fed98..b2c4f9ba2cbed88dee8b4e70f00d8a6c4afb5739 100644 (file)
@@ -1,15 +1,18 @@
 #include <msp/core/algorithm.h>
-#include <msp/gl/extensions/ext_gpu_shader4.h>
 #include <msp/strings/format.h>
-#include <msp/strings/regex.h>
 #include <msp/strings/utils.h>
-#include "compatibility.h"
+#include "builtin.h"
 #include "compiler.h"
+#include "debug.h"
 #include "error.h"
+#include "finalize.h"
 #include "generate.h"
+#include "glsl_error.h"
 #include "optimize.h"
 #include "output.h"
-#include "shader.h"
+#include "resolve.h"
+#include "resources.h"
+#include "validate.h"
 
 #undef interface
 
@@ -20,8 +23,17 @@ namespace GL {
 namespace SL {
 
 Compiler::Compiler():
-       resources(0),
-       module(0)
+       features(Features::from_context()),
+       module(0),
+       compiled(false),
+       specialized(false)
+{ }
+
+Compiler::Compiler(const Features &f):
+       features(f),
+       module(0),
+       compiled(false),
+       specialized(false)
 { }
 
 Compiler::~Compiler()
@@ -29,105 +41,189 @@ Compiler::~Compiler()
        delete module;
 }
 
-void Compiler::compile(const string &source, const string &src_name)
+void Compiler::clear()
 {
-       resources = 0;
        delete module;
        module = new Module();
+       imported_names.clear();
+       module->source_map.set_name(0, "<generated>");
+}
+
+void Compiler::set_source(const string &source, const string &src_name)
+{
+       clear();
        Parser parser;
        imported_names.push_back(src_name);
-       append_module(parser.parse(source, src_name, 1));
-       process();
+       append_module(parser.parse(source, src_name, 1), 0);
 }
 
-void Compiler::compile(IO::Base &io, Resources *res, const string &src_name)
+void Compiler::load_source(IO::Base &io, DataFile::Collection *res, const string &src_name)
 {
-       resources = res;
-       delete module;
-       module = new Module();
+       clear();
        Parser parser;
        imported_names.push_back(src_name);
-       append_module(parser.parse(io, src_name, 1));
-       process();
+       append_module(parser.parse(io, src_name, 1), res);
 }
 
-void Compiler::compile(IO::Base &io, const string &src_name)
+void Compiler::load_source(IO::Base &io, const string &src_name)
 {
-       compile(io, 0, src_name);
+       load_source(io, 0, src_name);
 }
 
-void Compiler::add_shaders(Program &program)
+void Compiler::specialize(const map<string, int> &sv)
 {
-       if(!module)
-               throw invalid_operation("Compiler::add_shaders");
+       specialized = true;
+       spec_values = sv;
+}
+
+void Compiler::compile(Mode mode)
+{
+       if(specialized && mode!=PROGRAM)
+               throw invalid_operation("Compiler::compile");
+
+       for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
+               generate(*i);
+       ConstantIdAssigner().apply(*module, features);
+
+       for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
+               validate(*i);
+       GlobalInterfaceValidator().apply(*module);
 
-       try
+       bool valid = true;
+       for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
+               if(!check_errors(*i))
+                       valid = false;
+       if(!valid)
+               throw invalid_shader_source(get_diagnostics());
+
+       if(specialized)
        {
                for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
-               {
-                       if(i->type==Stage::VERTEX)
-                       {
-                               program.attach_shader_owned(new VertexShader(apply<Formatter>(*i)));
-                               for(map<string, unsigned>::iterator j=i->locations.begin(); j!=i->locations.end(); ++j)
-                                       program.bind_attribute(j->second, j->first);
-                       }
-                       else if(i->type==Stage::GEOMETRY)
-                               program.attach_shader_owned(new GeometryShader(apply<Formatter>(*i)));
-                       else if(i->type==Stage::FRAGMENT)
-                       {
-                               program.attach_shader_owned(new FragmentShader(apply<Formatter>(*i)));
-                               if(EXT_gpu_shader4)
-                               {
-                                       for(map<string, unsigned>::iterator j=i->locations.begin(); j!=i->locations.end(); ++j)
-                                               program.bind_fragment_data(j->second, j->first);
-                               }
-                       }
-               }
+                       ConstantSpecializer().apply(*i, spec_values);
        }
-       catch(const compile_error &e)
+       for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); )
        {
-               static const Regex r_message("^(([0-9]+)\\(([0-9]+)\\) :|ERROR: ([0-9]+):([0-9]+):) (.*)$");
-               vector<string> lines = split(e.what(), '\n');
-               string translated;
-               for(vector<string>::const_iterator i=lines.begin(); i!=lines.end(); ++i)
-               {
-                       RegMatch m = r_message.match(*i);
-                       if(m)
-                       {
-                               unsigned index = 0;
-                               unsigned line = 0;
-                               if(m[2])
-                               {
-                                       index = lexical_cast<unsigned>(m[2].str);
-                                       line = lexical_cast<unsigned>(m[3].str);
-                               }
-                               else if(m[4])
-                               {
-                                       index = lexical_cast<unsigned>(m[4].str);
-                                       line = lexical_cast<unsigned>(m[5].str);
-                               }
-                               const char *src = "<unknown>";
-                               if(index==0)
-                                       src = "<generated>";
-                               else if(index-1<imported_names.size())
-                                       src = imported_names[index-1].c_str();
-                               translated += format("%s:%d: %s", src, line, m[6].str);
-                       }
-                       else
-                               translated += *i;
-                       translated += '\n';
-               }
+               OptimizeResult result = optimize(*i);
+               if(result==REDO_PREVIOUS)
+                       i = module->stages.begin();
+               else if(result!=REDO_STAGE)
+                       ++i;
+       }
+
+       LocationAllocator().apply(*module, features);
+       for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
+               finalize(*i, mode);
+
+       compiled = true;
+}
 
-               throw compile_error(translated);
+string Compiler::get_combined_glsl() const
+{
+       if(!compiled)
+               throw invalid_operation("Compiler::get_combined_glsl");
+
+       string glsl;
+
+       unsigned source_count = module->source_map.get_count();
+       for(unsigned i=1; i<source_count; ++i)
+               glsl += format("#pragma MSP source(%d, \"%s\")\n", i, module->source_map.get_name(i));
+       for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
+       {
+               glsl += format("#pragma MSP stage(%s)\n", Stage::get_stage_name(i->type));
+               glsl += Formatter().apply(*i);
+               glsl += '\n';
        }
+
+       return glsl;
+}
+
+vector<Stage::Type> Compiler::get_stages() const
+{
+       vector<Stage::Type> stage_types;
+       stage_types.reserve(module->stages.size());
+       for(list<Stage>::const_iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
+               stage_types.push_back(i->type);
+       return stage_types;
+}
+
+string Compiler::get_stage_glsl(Stage::Type stage_type) const
+{
+       if(!compiled)
+               throw invalid_operation("Compiler::get_stage_glsl");
+       for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
+               if(i->type==stage_type)
+                       return Formatter().apply(*i);
+       throw key_error(Stage::get_stage_name(stage_type));
+}
+
+const map<string, unsigned> &Compiler::get_vertex_attributes() const
+{
+       if(!compiled)
+               throw invalid_operation("Compiler::get_vertex_attributes");
+       for(list<Stage>::const_iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
+               if(i->type==Stage::VERTEX)
+                       return i->locations;
+       throw invalid_operation("Compiler::get_vertex_attributes");
+}
+
+const map<string, unsigned> &Compiler::get_fragment_outputs() const
+{
+       if(!compiled)
+               throw invalid_operation("Compiler::get_fragment_outputs");
+       for(list<Stage>::const_iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
+               if(i->type==Stage::FRAGMENT)
+                       return i->locations;
+       throw invalid_operation("Compiler::get_fragment_outputs");
+}
+
+const map<string, unsigned> &Compiler::get_texture_bindings() const
+{
+       if(!compiled)
+               throw invalid_operation("Compiler::get_texture_bindings");
+       return module->shared.texture_bindings;
+}
+
+const map<string, unsigned> &Compiler::get_uniform_block_bindings() const
+{
+       if(!compiled)
+               throw invalid_operation("Compiler::get_uniform_block_bindings");
+       return module->shared.uniform_block_bindings;
+}
+
+const SourceMap &Compiler::get_source_map() const
+{
+       return module->source_map;
 }
 
-void Compiler::append_module(Module &mod)
+string Compiler::get_stage_debug(Stage::Type stage_type) const
 {
-       vector<Import *> imports = apply<NodeGatherer<Import> >(mod.shared);
+       for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
+               if(i->type==stage_type)
+                       return DumpTree().apply(*i);
+       throw key_error(Stage::get_stage_name(stage_type));
+}
+
+string Compiler::get_diagnostics() const
+{
+       string combined;
+       for(list<Stage>::const_iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
+               for(vector<Diagnostic>::const_iterator j=i->diagnostics.begin(); j!=i->diagnostics.end(); ++j)
+                       if(j->source!=INTERNAL_SOURCE)
+                               append(combined, "\n", format("%s:%d: %s", module->source_map.get_name(j->source), j->line, j->message));
+       return combined;
+}
+
+void Compiler::append_module(Module &mod, DataFile::Collection *res)
+{
+       module->source_map.merge_from(mod.source_map);
+
+       vector<Import *> imports;
+       for(NodeList<Statement>::const_iterator i=mod.shared.content.body.begin(); i!=mod.shared.content.body.end(); ++i)
+               if(Import *imp = dynamic_cast<Import *>(i->get()))
+                       imports.push_back(imp);
        for(vector<Import *>::iterator i=imports.begin(); i!=imports.end(); ++i)
-               import((*i)->module);
-       apply<NodeRemover>(mod.shared, set<Node *>(imports.begin(), imports.end()));
+               import(res, (*i)->module);
+       NodeRemover().apply(mod.shared, set<Node *>(imports.begin(), imports.end()));
 
        append_stage(mod.shared);
        for(list<Stage>::iterator i=mod.stages.begin(); i!=mod.stages.end(); ++i)
@@ -156,29 +252,13 @@ void Compiler::append_stage(Stage &stage)
                target = &*i;
        }
 
-       if(stage.required_version>target->required_version)
-               target->required_version = stage.required_version;
+       if(stage.required_features.glsl_version>target->required_features.glsl_version)
+               target->required_features.glsl_version = stage.required_features.glsl_version;
        for(NodeList<Statement>::iterator i=stage.content.body.begin(); i!=stage.content.body.end(); ++i)
                target->content.body.push_back(*i);
-       apply<DeclarationCombiner>(*target);
 }
 
-void Compiler::process()
-{
-       for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
-               generate(*i);
-       for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); )
-       {
-               if(optimize(*i))
-                       i = module->stages.begin();
-               else
-                       ++i;
-       }
-       for(list<Stage>::iterator i=module->stages.begin(); i!=module->stages.end(); ++i)
-               finalize(*i);
-}
-
-void Compiler::import(const string &name)
+void Compiler::import(DataFile::Collection *resources, const string &name)
 {
        string fn = name+".glsl";
        if(find(imported_names, fn)!=imported_names.end())
@@ -189,69 +269,140 @@ void Compiler::import(const string &name)
        if(!io)
                throw runtime_error(format("module %s not found", name));
        Parser import_parser;
-       append_module(import_parser.parse(*io, fn, imported_names.size()));
+       append_module(import_parser.parse(*io, fn, module->source_map.get_count()), resources);
 }
 
 void Compiler::generate(Stage &stage)
 {
-       if(module->shared.required_version>stage.required_version)
-               stage.required_version = module->shared.required_version;
+       stage.required_features.gl_api = features.gl_api;
+       if(module->shared.required_features.glsl_version>stage.required_features.glsl_version)
+               stage.required_features.glsl_version = module->shared.required_features.glsl_version;
+
        inject_block(stage.content, module->shared.content);
+       if(const Stage *builtins = get_builtins(stage.type))
+               inject_block(stage.content, builtins->content);
+       if(const Stage *builtins = get_builtins(Stage::SHARED))
+               inject_block(stage.content, builtins->content);
+
+       // Initial resolving pass
+       resolve(stage);
 
-       apply<DeclarationReorderer>(stage);
-       apply<FunctionResolver>(stage);
-       apply<VariableResolver>(stage);
-       apply<InterfaceGenerator>(stage);
-       apply<VariableResolver>(stage);
-       apply<DeclarationReorderer>(stage);
-       apply<FunctionResolver>(stage);
-       apply<LegacyConverter>(stage);
+       /* All variables local to a stage have been resolved.  Resolve non-local
+       variables through interfaces. */
+       InterfaceGenerator().apply(stage);
+       resolve(stage, RESOLVE_BLOCKS|RESOLVE_TYPES|RESOLVE_VARIABLES);
 }
 
-bool Compiler::optimize(Stage &stage)
+template<typename T>
+bool Compiler::resolve(Stage &stage, unsigned &flags, unsigned bit)
 {
-       apply<ConstantConditionEliminator>(stage);
+       if(!(flags&bit))
+               return false;
 
-       set<FunctionDeclaration *> inlineable = apply<InlineableFunctionLocator>(stage);
-       apply<FunctionInliner>(stage, inlineable);
+       flags &= ~bit;
+       return T().apply(stage);
+}
 
-       set<Node *> unused = apply<UnusedVariableLocator>(stage);
-       set<Node *> unused2 = apply<UnusedFunctionLocator>(stage);
-       unused.insert(unused2.begin(), unused2.end());
-       apply<NodeRemover>(stage, unused);
+void Compiler::resolve(Stage &stage, unsigned flags)
+{
+       while(flags)
+       {
+               if(resolve<BlockHierarchyResolver>(stage, flags, RESOLVE_BLOCKS))
+                       ;
+               else if(resolve<TypeResolver>(stage, flags, RESOLVE_TYPES))
+                       flags |= RESOLVE_BLOCKS|RESOLVE_VARIABLES|RESOLVE_EXPRESSIONS;
+               else if(resolve<VariableResolver>(stage, flags, RESOLVE_VARIABLES))
+                       flags |= RESOLVE_EXPRESSIONS;
+               else if(resolve<FunctionResolver>(stage, flags, RESOLVE_FUNCTIONS))
+                       flags |= RESOLVE_EXPRESSIONS;
+               else if(resolve<ExpressionResolver>(stage, flags, RESOLVE_EXPRESSIONS))
+                       flags |= RESOLVE_VARIABLES|RESOLVE_FUNCTIONS;
+       }
+}
 
-       return !unused.empty();
+void Compiler::validate(Stage &stage)
+{
+       DeclarationValidator().apply(stage);
+       IdentifierValidator().apply(stage);
+       ReferenceValidator().apply(stage);
+       ExpressionValidator().apply(stage);
+       FlowControlValidator().apply(stage);
+       StageInterfaceValidator().apply(stage);
 }
 
-void Compiler::finalize(Stage &stage)
+bool Compiler::check_errors(Stage &stage)
 {
-       if(get_gl_api()==OPENGL_ES2)
-               apply<DefaultPrecisionGenerator>(stage);
-       else
-               apply<PrecisionRemover>(stage);
+       stable_sort(stage.diagnostics, &diagnostic_line_order);
+
+       for(vector<Diagnostic>::const_iterator i=stage.diagnostics.begin(); i!=stage.diagnostics.end(); ++i)
+               if(i->severity==Diagnostic::ERR)
+                       return false;
+
+       return true;
 }
 
-void Compiler::inject_block(Block &target, const Block &source)
+bool Compiler::diagnostic_line_order(const Diagnostic &diag1, const Diagnostic &diag2)
 {
-       NodeList<Statement>::iterator insert_point = target.body.begin();
-       for(NodeList<Statement>::const_iterator i=source.body.begin(); i!=source.body.end(); ++i)
-               target.body.insert(insert_point, (*i)->clone());
+       if(diag1.provoking_source!=diag2.provoking_source)
+       {
+               // Sort builtins first and imported modules according to import order.
+               if(diag1.provoking_source<=BUILTIN_SOURCE)
+                       return diag1.provoking_source<diag2.provoking_source;
+               else if(diag2.provoking_source<=BUILTIN_SOURCE)
+                       return false;
+               else
+                       return diag1.provoking_source>diag2.provoking_source;
+       }
+       return diag1.provoking_line<diag2.provoking_line;
 }
 
-template<typename T>
-typename T::ResultType Compiler::apply(Stage &stage)
+Compiler::OptimizeResult Compiler::optimize(Stage &stage)
+{
+       if(ConstantFolder().apply(stage))
+               resolve(stage, RESOLVE_EXPRESSIONS);
+       ConstantConditionEliminator().apply(stage);
+
+       bool any_inlined = false;
+       if(FunctionInliner().apply(stage))
+       {
+               resolve(stage, RESOLVE_TYPES|RESOLVE_VARIABLES|RESOLVE_FUNCTIONS|RESOLVE_EXPRESSIONS);
+               any_inlined = true;
+       }
+       if(ExpressionInliner().apply(stage))
+       {
+               resolve(stage, RESOLVE_VARIABLES|RESOLVE_FUNCTIONS|RESOLVE_EXPRESSIONS);
+               any_inlined = true;
+       }
+
+       /* Removing variables or functions may cause things from the previous stage
+       to become unused. */
+       bool any_removed = UnreachableCodeRemover().apply(stage);
+       any_removed |= UnusedVariableRemover().apply(stage);
+       any_removed |= UnusedFunctionRemover().apply(stage);
+       any_removed |= UnusedTypeRemover().apply(stage);
+
+       return any_removed ? REDO_PREVIOUS : any_inlined ? REDO_STAGE : NEXT_STAGE;
+}
+
+void Compiler::finalize(Stage &stage, Mode mode)
 {
-       T visitor;
-       visitor.apply(stage);
-       return visitor.get_result();
+       if(mode==PROGRAM)
+       {
+               LegacyConverter().apply(stage, features);
+               resolve(stage, RESOLVE_VARIABLES|RESOLVE_FUNCTIONS);
+               PrecisionConverter().apply(stage);
+       }
+
+       // Collect bindings from all stages into the shared stage's maps
+       module->shared.texture_bindings.insert(stage.texture_bindings.begin(), stage.texture_bindings.end());
+       module->shared.uniform_block_bindings.insert(stage.uniform_block_bindings.begin(), stage.uniform_block_bindings.end());
 }
 
-template<typename T, typename A>
-typename T::ResultType Compiler::apply(Stage &stage, const A &arg)
+void Compiler::inject_block(Block &target, const Block &source)
 {
-       T visitor(arg);
-       visitor.apply(stage);
-       return visitor.get_result();
+       NodeList<Statement>::iterator insert_point = target.body.begin();
+       for(NodeList<Statement>::const_iterator i=source.body.begin(); i!=source.body.end(); ++i)
+               target.body.insert(insert_point, (*i)->clone());
 }
 
 } // namespace SL