]> git.tdb.fi Git - libs/gl.git/blobdiff - source/glsl/generate.cpp
Split glsl/generate.cpp in two
[libs/gl.git] / source / glsl / generate.cpp
index ac3a4b75e7f7f9d62aa3925f01be9d6eeb94f8c4..13fb72c53039bfbd7e9fed3779bc718bdc6801d7 100644 (file)
@@ -1,7 +1,5 @@
 #include <msp/core/hash.h>
 #include <msp/core/raii.h>
-#include <msp/strings/lexicalcast.h>
-#include "builtin.h"
 #include "generate.h"
 
 using namespace std;
@@ -10,57 +8,6 @@ namespace Msp {
 namespace GL {
 namespace SL {
 
-void DeclarationCombiner::apply(Stage &stage)
-{
-       stage.content.visit(*this);
-       NodeRemover().apply(stage, nodes_to_remove);
-}
-
-void DeclarationCombiner::visit(Block &block)
-{
-       if(current_block)
-               return;
-
-       TraversingVisitor::visit(block);
-}
-
-void DeclarationCombiner::visit(VariableDeclaration &var)
-{
-       VariableDeclaration *&ptr = variables[var.name];
-       if(ptr)
-       {
-               ptr->type = var.type;
-               if(var.init_expression)
-                       ptr->init_expression = var.init_expression;
-               if(var.layout)
-               {
-                       if(ptr->layout)
-                       {
-                               for(vector<Layout::Qualifier>::iterator i=var.layout->qualifiers.begin(); i!=var.layout->qualifiers.end(); ++i)
-                               {
-                                       bool found = false;
-                                       for(vector<Layout::Qualifier>::iterator j=ptr->layout->qualifiers.begin(); (!found && j!=ptr->layout->qualifiers.end()); ++j)
-                                               if(j->name==i->name)
-                                               {
-                                                       j->has_value = i->value;
-                                                       j->value = i->value;
-                                                       found = true;
-                                               }
-
-                                       if(!found)
-                                               ptr->layout->qualifiers.push_back(*i);
-                               }
-                       }
-                       else
-                               ptr->layout = var.layout;
-               }
-               nodes_to_remove.insert(&var);
-       }
-       else
-               ptr = &var;
-}
-
-
 ConstantSpecializer::ConstantSpecializer():
        values(0)
 { }
@@ -99,269 +46,24 @@ void ConstantSpecializer::visit(VariableDeclaration &var)
                {
                        RefPtr<Literal> literal = new Literal;
                        if(var.type=="bool")
+                       {
                                literal->token = (i->second ? "true" : "false");
+                               literal->value = static_cast<bool>(i->second);
+                       }
                        else if(var.type=="int")
+                       {
                                literal->token = lexical_cast<string>(i->second);
+                               literal->value = i->second;
+                       }
                        var.init_expression = literal;
                }
        }
 }
 
 
-void BlockHierarchyResolver::enter(Block &block)
-{
-       block.parent = current_block;
-}
-
-
-VariableResolver::VariableResolver():
-       stage(0),
-       r_members(0),
-       record_target(false),
-       r_assignment_target(0),
-       r_self_referencing(false)
-{ }
-
-void VariableResolver::apply(Stage &s)
-{
-       stage = &s;
-       s.types.clear();
-       s.interface_blocks.clear();
-       s.content.visit(*this);
-}
-
-void VariableResolver::enter(Block &block)
-{
-       block.variables.clear();
-}
-
-void VariableResolver::visit(VariableReference &var)
-{
-       var.declaration = 0;
-       r_members = 0;
-       for(Block *block=current_block; (!var.declaration && block); block=block->parent)
-       {
-               map<string, VariableDeclaration *>::iterator i = block->variables.find(var.name);
-               if(i!=block->variables.end())
-                       var.declaration = i->second;
-       }
-
-       if(var.declaration)
-       {
-               if(var.declaration->type_declaration)
-                       r_members = &var.declaration->type_declaration->members.variables;
-       }
-       else
-       {
-               const map<string, InterfaceBlock *> &blocks = stage->interface_blocks;
-               map<string, InterfaceBlock *>::const_iterator i = blocks.find(var.name);
-               if(i!=blocks.end() && i->second->instance_name==var.name)
-               {
-                       r_iface_ref = new InterfaceBlockReference;
-                       r_iface_ref->name = var.name;
-                       r_iface_ref->declaration = i->second;
-                       r_members = &i->second->members.variables;
-               }
-               else
-               {
-                       for(i=blocks.begin(); (!var.declaration && i!=blocks.end()); ++i)
-                               if(i->second->instance_name.empty())
-                               {
-                                       map<string, VariableDeclaration *>::iterator j = i->second->members.variables.find(var.name);
-                                       if(j!=i->second->members.variables.end())
-                                               var.declaration = j->second;
-                               }
-               }
-       }
-
-       if(record_target)
-       {
-               if(r_assignment_target)
-               {
-                       record_target = false;
-                       r_assignment_target = 0;
-               }
-               else
-                       r_assignment_target = var.declaration;
-       }
-       else if(var.declaration && var.declaration==r_assignment_target)
-               r_self_referencing = true;
-}
-
-void VariableResolver::visit(InterfaceBlockReference &iface)
-{
-       iface.declaration = 0;
-       for(Block *block=current_block; block; block=block->parent)
-       {
-               map<string, InterfaceBlock *>::iterator i = stage->interface_blocks.find(iface.name);
-               if(i!=stage->interface_blocks.end())
-               {
-                       iface.declaration = i->second;
-                       r_members = &i->second->members.variables;
-                       break;
-               }
-       }
-}
-
-void VariableResolver::visit(MemberAccess &memacc)
-{
-       r_members = 0;
-       r_iface_ref = 0;
-       memacc.left->visit(*this);
-
-       if(r_iface_ref)
-               memacc.left = r_iface_ref;
-       r_iface_ref = 0;
-
-       memacc.declaration = 0;
-       if(r_members)
-       {
-               map<string, VariableDeclaration *>::iterator i = r_members->find(memacc.member);
-               if(i!=r_members->end())
-               {
-                       memacc.declaration = i->second;
-                       if(i->second->type_declaration)
-                               r_members = &i->second->type_declaration->members.variables;
-               }
-               else
-                       r_members = 0;
-       }
-}
-
-void VariableResolver::visit(UnaryExpression &unary)
-{
-       TraversingVisitor::visit(unary);
-       r_members = 0;
-       r_iface_ref = 0;
-}
-
-void VariableResolver::visit(BinaryExpression &binary)
-{
-       if(binary.oper->token[0]=='[')
-       {
-               {
-                       SetForScope<bool> set(record_target, false);
-                       binary.right->visit(*this);
-               }
-               r_members = 0;
-               r_iface_ref = 0;
-               binary.left->visit(*this);
-               if(r_iface_ref)
-                       binary.left = r_iface_ref;
-       }
-       else
-       {
-               TraversingVisitor::visit(binary);
-               r_members = 0;
-       }
-
-       r_iface_ref = 0;
-}
-
-void VariableResolver::visit(Assignment &assign)
-{
-       {
-               SetFlag set(record_target);
-               r_assignment_target = 0;
-               assign.left->visit(*this);
-       }
-
-       r_self_referencing = false;
-       assign.right->visit(*this);
-
-       assign.self_referencing = (r_self_referencing || assign.oper->token[0]!='=');
-       assign.target_declaration = r_assignment_target;
-       r_members = 0;
-       r_iface_ref = 0;
-}
-
-void VariableResolver::visit(FunctionCall &call)
-{
-       TraversingVisitor::visit(call);
-       r_members = 0;
-       r_iface_ref = 0;
-}
-
-void VariableResolver::visit(StructDeclaration &strct)
-{
-       TraversingVisitor::visit(strct);
-       stage->types[strct.name] = &strct;
-}
-
-void VariableResolver::visit(VariableDeclaration &var)
-{
-       map<string, StructDeclaration *>::iterator i = stage->types.find(var.type);
-       if(i!=stage->types.end())
-               var.type_declaration = i->second;
-
-       if(!block_interface.empty() && var.interface.empty())
-               var.interface = block_interface;
-
-       TraversingVisitor::visit(var);
-       current_block->variables[var.name] = &var;
-}
-
-void VariableResolver::visit(InterfaceBlock &iface)
-{
-       /* Block names can't be used for any other identifiers so we can put them
-       in the same map with instance names. */
-       stage->interface_blocks[iface.name] = &iface;
-       if(!iface.instance_name.empty())
-               stage->interface_blocks[iface.instance_name] = &iface;
-
-       SetForScope<string> set_iface(block_interface, iface.interface);
-       TraversingVisitor::visit(iface);
-}
-
-
-void FunctionResolver::apply(Stage &s)
-{
-       stage = &s;
-       s.functions.clear();
-       s.content.visit(*this);
-}
-
-void FunctionResolver::visit(FunctionCall &call)
-{
-       map<string, FunctionDeclaration *>::iterator i = stage->functions.find(call.name);
-       if(i!=stage->functions.end())
-               call.declaration = i->second;
-
-       TraversingVisitor::visit(call);
-}
-
-void FunctionResolver::visit(FunctionDeclaration &func)
-{
-       FunctionDeclaration *&stage_decl = stage->functions[func.name];
-       vector<FunctionDeclaration *> &decls = declarations[func.name];
-       if(func.definition==&func)
-       {
-               stage_decl = &func;
-
-               for(vector<FunctionDeclaration *>::iterator i=decls.begin(); i!=decls.end(); ++i)
-               {
-                       (*i)->definition = func.definition;
-                       (*i)->body.body.clear();
-               }
-       }
-       else
-       {
-               func.definition = 0;
-               if(!stage_decl)
-                       stage_decl = &func;
-               else
-                       func.definition = stage_decl->definition;
-       }
-       decls.push_back(&func);
-
-       TraversingVisitor::visit(func);
-}
-
-
 InterfaceGenerator::InterfaceGenerator():
        stage(0),
        function_scope(false),
-       iface_block(0),
        copy_block(false),
        iface_target_block(0)
 { }
@@ -406,17 +108,21 @@ string InterfaceGenerator::change_prefix(const string &name, const string &prefi
        return prefix+name.substr(offset);
 }
 
-bool InterfaceGenerator::generate_interface(VariableDeclaration &var, const string &iface, const string &name)
+VariableDeclaration *InterfaceGenerator::generate_interface(VariableDeclaration &var, const string &iface, const string &name)
 {
        if(stage->content.variables.count(name))
-               return false;
+               return 0;
+
+       if(stage->type==Stage::GEOMETRY && !copy_block && var.interface=="out" && var.array)
+               return 0;
 
        VariableDeclaration* iface_var = new VariableDeclaration;
        iface_var->sampling = var.sampling;
        iface_var->interface = iface;
        iface_var->type = var.type;
-       iface_var->type_declaration = var.type_declaration;
        iface_var->name = name;
+       /* Geometry shader inputs are always arrays.  But if we're bringing in an
+       entire block, the array is on the block and not individual variables. */
        if(stage->type==Stage::GEOMETRY && !copy_block)
                iface_var->array = ((var.array && var.interface!="in") || iface=="in");
        else
@@ -431,19 +137,22 @@ bool InterfaceGenerator::generate_interface(VariableDeclaration &var, const stri
        }
 
        iface_target_block->body.insert(iface_insert_point, iface_var);
-       iface_target_block->variables[name] = iface_var;
+       iface_target_block->variables.insert(make_pair(name, iface_var));
+       if(iface_target_block==&stage->content && iface=="in")
+               declared_inputs.push_back(iface_var);
 
-       return true;
+       return iface_var;
 }
 
-bool InterfaceGenerator::generate_interface(InterfaceBlock &out_block)
+InterfaceBlock *InterfaceGenerator::generate_interface(InterfaceBlock &out_block)
 {
-       if(stage->interface_blocks.count(out_block.name))
-               return false;
+       if(stage->interface_blocks.count("in"+out_block.block_name))
+               return 0;
 
        InterfaceBlock *in_block = new InterfaceBlock;
        in_block->interface = "in";
-       in_block->name = out_block.name;
+       in_block->block_name = out_block.block_name;
+       in_block->members = new Block;
        in_block->instance_name = out_block.instance_name;
        if(stage->type==Stage::GEOMETRY)
                in_block->array = true;
@@ -454,21 +163,24 @@ bool InterfaceGenerator::generate_interface(InterfaceBlock &out_block)
 
        {
                SetFlag set_copy(copy_block, true);
-               SetForScope<Block *> set_target(iface_target_block, &in_block->members);
-               SetForScope<NodeList<Statement>::iterator> set_ins_pt(iface_insert_point, in_block->members.body.end());
-               out_block.members.visit(*this);
+               SetForScope<Block *> set_target(iface_target_block, in_block->members.get());
+               SetForScope<NodeList<Statement>::iterator> set_ins_pt(iface_insert_point, in_block->members->body.end());
+               if(out_block.struct_declaration)
+                       out_block.struct_declaration->members.visit(*this);
+               else if(out_block.members)
+                       out_block.members->visit(*this);
        }
 
        iface_target_block->body.insert(iface_insert_point, in_block);
-       stage->interface_blocks[in_block->name] = in_block;
+       stage->interface_blocks.insert(make_pair("in"+in_block->block_name, in_block));
        if(!in_block->instance_name.empty())
-               stage->interface_blocks[in_block->instance_name] = in_block;
+               stage->interface_blocks.insert(make_pair("_"+in_block->instance_name, in_block));
 
        SetFlag set_scope(function_scope, false);
        SetForScope<Block *> set_block(current_block, &stage->content);
        in_block->visit(*this);
 
-       return true;
+       return in_block;
 }
 
 ExpressionStatement &InterfaceGenerator::insert_assignment(const string &left, Expression *right)
@@ -492,7 +204,7 @@ void InterfaceGenerator::visit(VariableReference &var)
 {
        if(var.declaration || !stage->previous)
                return;
-       /* Don't pull a variable from previous stage if we just generated an out
+       /* Don't pull a variable from previous stage if we just generated an output
        interface in this stage */
        if(stage->content.variables.count(var.name))
                return;
@@ -503,24 +215,33 @@ void InterfaceGenerator::visit(VariableReference &var)
                i = prev_vars.find(in_prefix+var.name);
        if(i!=prev_vars.end() && i->second->interface=="out")
        {
-               generate_interface(*i->second, "in", i->second->name);
-               var.name = i->second->name;
+               if(stage->type==Stage::GEOMETRY && i->second->array)
+                       stage->diagnostics.push_back(Diagnostic(Diagnostic::WARN, var.source, var.line,
+                               format("Can't access '%s' through automatic interface because it's an array", var.name)));
+               else
+               {
+                       generate_interface(*i->second, "in", i->second->name);
+                       var.name = i->second->name;
+               }
                return;
        }
 
        const map<string, InterfaceBlock *> &prev_blocks = stage->previous->interface_blocks;
-       map<string, InterfaceBlock *>::const_iterator j = prev_blocks.find(var.name);
-       if(j!=prev_blocks.end() && j->second->interface=="out" && j->second->instance_name==var.name)
+       map<string, InterfaceBlock *>::const_iterator j = prev_blocks.find("_"+var.name);
+       if(j!=prev_blocks.end() && j->second->interface=="out")
        {
                generate_interface(*j->second);
+               /* Let VariableResolver convert the variable reference into an interface
+               block reference. */
                return;
        }
 
        for(j=prev_blocks.begin(); j!=prev_blocks.end(); ++j)
-               if(j->second->instance_name.empty())
+               if(j->second->instance_name.empty() && j->second->struct_declaration)
                {
-                       i = j->second->members.variables.find(var.name);
-                       if(i!=j->second->members.variables.end())
+                       const map<string, VariableDeclaration *> &iface_vars = j->second->struct_declaration->members.variables;
+                       i = iface_vars.find(var.name);
+                       if(i!=iface_vars.end())
                        {
                                generate_interface(*j->second);
                                return;
@@ -531,29 +252,16 @@ void InterfaceGenerator::visit(VariableReference &var)
 void InterfaceGenerator::visit(VariableDeclaration &var)
 {
        if(copy_block)
-       {
                generate_interface(var, "in", var.name);
-               return;
-       }
-
-       if(iface_block)
+       else if(var.interface=="out")
        {
-               if(iface_block->linked_block)
-               {
-                       const map<string, VariableDeclaration *> &linked_vars = iface_block->linked_block->members.variables;
-                       map<string, VariableDeclaration *>::const_iterator i = linked_vars.find(var.name);
-                       if(i!=linked_vars.end())
-                               var.linked_declaration = i->second;
-               }
-               return;
-       }
-
-       if(var.interface=="out")
-       {
-               /* For out variables in function scope, generate a global interface and
-               replace the local declaration with an assignment. */
-               if(function_scope && generate_interface(var, "out", var.name))
+               /* For output variables in function scope, generate a global interface
+               and replace the local declaration with an assignment. */
+               VariableDeclaration *out_var = 0;
+               if(function_scope && (out_var=generate_interface(var, "out", var.name)))
                {
+                       out_var->source = var.source;
+                       out_var->line = var.line;
                        nodes_to_remove.insert(&var);
                        if(var.init_expression)
                        {
@@ -564,11 +272,14 @@ void InterfaceGenerator::visit(VariableDeclaration &var)
                        }
                }
        }
-       else if(var.interface=="in")
+       else if(var.interface=="in" && current_block==&stage->content)
        {
-               /* Try to link in variables in global scope with out variables from
-               previous stage */
-               if(current_block==&stage->content && !var.linked_declaration && stage->previous)
+               if(var.name.compare(0, 3, "gl_"))
+                       declared_inputs.push_back(&var);
+
+               /* Try to link input variables in global scope with output variables from
+               previous stage. */
+               if(!var.linked_declaration && stage->previous)
                {
                        const map<string, VariableDeclaration *> &prev_vars = stage->previous->content.variables;
                        map<string, VariableDeclaration *>::const_iterator i = prev_vars.find(var.name);
@@ -587,11 +298,13 @@ void InterfaceGenerator::visit(InterfaceBlock &iface)
 {
        if(iface.interface=="in")
        {
+               /* Try to link input blocks with output blocks sharing the same block
+               name from previous stage. */
                if(!iface.linked_block && stage->previous)
                {
                        const map<string, InterfaceBlock *> &prev_blocks = stage->previous->interface_blocks;
-                       map<string, InterfaceBlock *>::const_iterator i = prev_blocks.find(iface.name);
-                       if(i!=prev_blocks.end() && i->second->interface=="out" && i->second->name==iface.name)
+                       map<string, InterfaceBlock *>::const_iterator i = prev_blocks.find("out"+iface.block_name);
+                       if(i!=prev_blocks.end())
                        {
                                iface.linked_block = i->second;
                                i->second->linked_block = &iface;
@@ -599,7 +312,6 @@ void InterfaceGenerator::visit(InterfaceBlock &iface)
                }
        }
 
-       SetForScope<InterfaceBlock *> set_iface(iface_block, &iface);
        TraversingVisitor::visit(iface);
 }
 
@@ -612,11 +324,8 @@ void InterfaceGenerator::visit(FunctionDeclaration &func)
 
 void InterfaceGenerator::visit(Passthrough &pass)
 {
-       vector<VariableDeclaration *> pass_vars;
-
-       for(map<string, VariableDeclaration *>::const_iterator i=stage->content.variables.begin(); i!=stage->content.variables.end(); ++i)
-               if(i->second->interface=="in")
-                       pass_vars.push_back(i->second);
+       // Pass through all input variables declared so far.
+       vector<VariableDeclaration *> pass_vars = declared_inputs;
 
        if(stage->previous)
        {
@@ -626,6 +335,8 @@ void InterfaceGenerator::visit(Passthrough &pass)
                        if(i->second->interface!="out")
                                continue;
 
+                       /* Pass through output variables from the previous stage, but only
+                       those which are not already linked to an input here. */
                        if(!i->second->linked_declaration && generate_interface(*i->second, "in", i->second->name))
                                pass_vars.push_back(i->second);
                }
@@ -633,6 +344,8 @@ void InterfaceGenerator::visit(Passthrough &pass)
 
        if(stage->type==Stage::GEOMETRY)
        {
+               /* Special case for geometry shader: copy gl_Position from input to
+               output. */
                InterfaceBlockReference *ref = new InterfaceBlockReference;
                ref->name = "gl_in";