]> git.tdb.fi Git - libs/gl.git/commitdiff
Transform interface block contents into structs
authorMikko Rasa <tdb@tdb.fi>
Mon, 8 Mar 2021 13:06:01 +0000 (15:06 +0200)
committerMikko Rasa <tdb@tdb.fi>
Mon, 8 Mar 2021 15:28:21 +0000 (17:28 +0200)
This allows handling them in expression trees like everything else.  It
also helps with SPIR-V, which requires interface blocks to have struct
types.

15 files changed:
source/glsl/compatibility.cpp
source/glsl/compiler.cpp
source/glsl/debug.cpp
source/glsl/generate.cpp
source/glsl/generate.h
source/glsl/optimize.cpp
source/glsl/optimize.h
source/glsl/output.cpp
source/glsl/output.h
source/glsl/parser.cpp
source/glsl/syntax.cpp
source/glsl/syntax.h
source/glsl/validate.cpp
source/glsl/validate.h
source/glsl/visitor.cpp

index a5ba8247a53d705c82223d9b8241e5d28dbea14e..3700a9c9a3379653e661ad0f56799eb454c03c20 100644 (file)
@@ -341,15 +341,20 @@ bool LegacyConverter::supports_interface_blocks(const string &iface) const
 
 void LegacyConverter::visit(InterfaceBlock &iface)
 {
-       if(!supports_interface_blocks(iface.interface))
+       if(!supports_interface_blocks(iface.interface) && iface.type_declaration)
        {
                if(!iface.instance_name.empty())
                        unsupported("ARB_uniform_buffer_object required for interface block instances");
-               else
+               else if(iface.struct_declaration)
                {
-                       stage->content.body.splice(uniform_insert_point, iface.members.body);
+                       stage->content.body.splice(uniform_insert_point, iface.struct_declaration->members.body);
                        nodes_to_remove.insert(&iface);
+                       nodes_to_remove.insert(iface.struct_declaration);
                }
+               else
+                       /* If the interface block is an array, it should have an instance
+                       name too, so this should never be reached */
+                       throw logic_error("Unexpected interface block configuration");
        }
 }
 
index ac014a00147bda31eef70d842c644cab3d27929d..ccfbe691195eefd91aa4314281db6afc1bb97c97 100644 (file)
@@ -166,7 +166,8 @@ 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)
-                       append(combined, "\n", format("%s:%d: %s", module->source_map.get_name(j->source), j->line, j->message));
+                       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;
 }
 
@@ -275,7 +276,7 @@ void Compiler::resolve(Stage &stage, unsigned flags)
                if(resolve<BlockHierarchyResolver>(stage, flags, RESOLVE_BLOCKS))
                        ;
                else if(resolve<TypeResolver>(stage, flags, RESOLVE_TYPES))
-                       flags |= RESOLVE_VARIABLES|RESOLVE_EXPRESSIONS;
+                       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))
index ed604fffa94849b49efeb34e722b4399f2a88cda..ee50bf46e2ccb3af903f44249187cc70a5eb9fb4 100644 (file)
@@ -165,7 +165,7 @@ void DumpTree::visit(InterfaceBlockReference &iface)
        string text;
        if(iface.declaration)
                text += format("%%%d ", get_label(*iface.declaration));
-       text += format("%s (iface)", iface.name);
+       text += format("%s (iface) -> %s", iface.name, format_type(iface.type));
        append(text);
 }
 
@@ -346,7 +346,15 @@ void DumpTree::visit(InterfaceBlock &block)
                head += " (builtin)";
        else if(block.linked_block)
                head += " (linked)";
-       annotated_branch(head, block.members);
+       append(head);
+
+       begin_sub();
+       last_branch();
+       if(block.type_declaration)
+               append(format("Type: %%%d %s", get_label(*block.type_declaration), block.type_declaration->name));
+       else if(block.members)
+               block.members->visit(*this);
+       end_sub();
 }
 
 void DumpTree::visit(FunctionDeclaration &func)
index 4fd8c6ab79aeb3453c1501332dc19953bf42e8d5..53676c26784cf7d32709639fea843459e3cf4269 100644 (file)
@@ -123,6 +123,7 @@ void BlockHierarchyResolver::enter(Block &block)
 
 TypeResolver::TypeResolver():
        stage(0),
+       iface_block(0),
        r_any_resolved(false)
 { }
 
@@ -214,6 +215,34 @@ void TypeResolver::visit(StructDeclaration &strct)
 void TypeResolver::visit(VariableDeclaration &var)
 {
        resolve_type(var.type_declaration, var.type, var.array);
+       if(iface_block && var.interface==iface_block->interface)
+               var.interface.clear();
+}
+
+void TypeResolver::visit(InterfaceBlock &iface)
+{
+       if(iface.members)
+       {
+               SetForScope<InterfaceBlock *> set_iface(iface_block, &iface);
+               iface.members->visit(*this);
+
+               StructDeclaration *strct = new StructDeclaration;
+               strct->source = INTERNAL_SOURCE;
+               strct->name = format("_%s_%s", iface.interface, iface.name);
+               strct->members.body.splice(strct->members.body.begin(), iface.members->body);
+               stage->content.body.insert(type_insert_point, strct);
+               stage->types.insert(make_pair(strct->name, strct));
+
+               iface.members = 0;
+               strct->interface_block = &iface;
+               iface.struct_declaration = strct;
+       }
+
+       TypeDeclaration *type = iface.struct_declaration;
+       if(type && iface.array)
+               type = get_or_create_array_type(*type);
+       r_any_resolved = (type!=iface.type_declaration);
+       iface.type_declaration = type;
 }
 
 void TypeResolver::visit(FunctionDeclaration &func)
@@ -250,7 +279,10 @@ void VariableResolver::visit_and_replace(RefPtr<Expression> &expr)
        r_replacement_expr = 0;
        expr->visit(*this);
        if(r_replacement_expr)
+       {
                expr = r_replacement_expr;
+               r_any_resolved = true;
+       }
        r_replacement_expr = 0;
 }
 
@@ -287,10 +319,11 @@ void VariableResolver::visit(VariableReference &var)
                {
                        // Look for the variable in anonymous interface blocks.
                        for(i=blocks.begin(); (!declaration && i!=blocks.end()); ++i)
-                               if(i->second->instance_name.empty())
+                               if(i->second->instance_name.empty() && i->second->struct_declaration)
                                {
-                                       map<string, VariableDeclaration *>::iterator j = i->second->members.variables.find(var.name);
-                                       if(j!=i->second->members.variables.end())
+                                       const map<string, VariableDeclaration *> &iface_vars = i->second->struct_declaration->members.variables;
+                                       map<string, VariableDeclaration *>::const_iterator j = iface_vars.find(var.name);
+                                       if(j!=iface_vars.end())
                                                declaration = j->second;
                                }
                }
@@ -327,20 +360,11 @@ void VariableResolver::visit(MemberAccess &memacc)
 {
        visit_and_replace(memacc.left);
 
-       map<string, VariableDeclaration *> *members = 0;
-       if(StructDeclaration *strct = dynamic_cast<StructDeclaration *>(memacc.left->type))
-               members = &strct->members.variables;
-       else if(InterfaceBlockReference *iface_ref = dynamic_cast<InterfaceBlockReference *>(memacc.left.get()))
-       {
-               if(iface_ref->declaration)
-                       members = &iface_ref->declaration->members.variables;
-       }
-
        VariableDeclaration *declaration = 0;
-       if(members)
+       if(StructDeclaration *strct = dynamic_cast<StructDeclaration *>(memacc.left->type))
        {
-               map<string, VariableDeclaration *>::iterator i = members->find(memacc.member);
-               if(i!=members->end())
+               map<string, VariableDeclaration *>::iterator i = strct->members.variables.find(memacc.member);
+               if(i!=strct->members.variables.end())
                        declaration = i->second;
        }
 
@@ -395,9 +419,6 @@ void VariableResolver::visit(FunctionCall &call)
 
 void VariableResolver::visit(VariableDeclaration &var)
 {
-       if(!block_interface.empty() && var.interface.empty())
-               var.interface = block_interface;
-
        if(var.layout)
                var.layout->visit(*this);
        if(var.array_size)
@@ -415,7 +436,6 @@ void VariableResolver::visit(InterfaceBlock &iface)
        if(!iface.instance_name.empty())
                stage->interface_blocks.insert(make_pair("_"+iface.instance_name, &iface));
 
-       SetForScope<string> set_iface(block_interface, iface.interface);
        TraversingVisitor::visit(iface);
 }
 
@@ -557,7 +577,8 @@ void ExpressionResolver::visit(VariableReference &var)
 
 void ExpressionResolver::visit(InterfaceBlockReference &iface)
 {
-       resolve(iface, 0, true);
+       if(iface.declaration)
+               resolve(iface, iface.declaration->type_declaration, true);
 }
 
 void ExpressionResolver::visit(MemberAccess &memacc)
@@ -861,7 +882,6 @@ void FunctionResolver::visit(FunctionDeclaration &func)
 InterfaceGenerator::InterfaceGenerator():
        stage(0),
        function_scope(false),
-       iface_block(0),
        copy_block(false),
        iface_target_block(0)
 { }
@@ -945,6 +965,7 @@ InterfaceBlock *InterfaceGenerator::generate_interface(InterfaceBlock &out_block
        InterfaceBlock *in_block = new InterfaceBlock;
        in_block->interface = "in";
        in_block->name = out_block.name;
+       in_block->members = new Block;
        in_block->instance_name = out_block.instance_name;
        if(stage->type==Stage::GEOMETRY)
                in_block->array = true;
@@ -955,9 +976,12 @@ InterfaceBlock *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);
@@ -1020,10 +1044,11 @@ void InterfaceGenerator::visit(VariableReference &var)
        }
 
        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;
@@ -1034,28 +1059,8 @@ void InterfaceGenerator::visit(VariableReference &var)
 void InterfaceGenerator::visit(VariableDeclaration &var)
 {
        if(copy_block)
-       {
                generate_interface(var, "in", var.name);
-               return;
-       }
-
-       if(iface_block)
-       {
-               if(iface_block->linked_block)
-               {
-                       // Link all variables to their counterparts in the 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;
-                               var.linked_declaration->linked_declaration = &var;
-                       }
-               }
-               return;
-       }
-
-       if(var.interface=="out")
+       else if(var.interface=="out")
        {
                /* For output variables in function scope, generate a global interface
                and replace the local declaration with an assignment. */
@@ -1111,7 +1116,6 @@ void InterfaceGenerator::visit(InterfaceBlock &iface)
                }
        }
 
-       SetForScope<InterfaceBlock *> set_iface(iface_block, &iface);
        TraversingVisitor::visit(iface);
 }
 
index a728e99ced3a8bccd24aaa89de94cbbddee09664..427f4de2360d977819d02ba9373b5beb7dfee7fe 100644 (file)
@@ -68,6 +68,7 @@ private:
        std::map<TypeDeclaration *, TypeDeclaration *> alias_map;
        std::map<TypeDeclaration *, TypeDeclaration *> array_types;
        NodeList<Statement>::iterator type_insert_point;
+       InterfaceBlock *iface_block;
        bool r_any_resolved;
 
 public:
@@ -83,6 +84,7 @@ private:
        virtual void visit(ImageTypeDeclaration &);
        virtual void visit(StructDeclaration &);
        virtual void visit(VariableDeclaration &);
+       virtual void visit(InterfaceBlock &);
        virtual void visit(FunctionDeclaration &);
 };
 
@@ -93,7 +95,6 @@ class VariableResolver: private TraversingVisitor
 private:
        Stage *stage;
        RefPtr<Expression> r_replacement_expr;
-       std::string block_interface;
        bool r_any_resolved;
        bool record_target;
        bool r_self_referencing;
@@ -196,7 +197,6 @@ private:
        std::string in_prefix;
        std::string out_prefix;
        bool function_scope;
-       InterfaceBlock *iface_block;
        bool copy_block;
        Block *iface_target_block;
        NodeList<Statement>::iterator iface_insert_point;
index f938314814ea9cd4211279d5e94ff817c2a92fcc..c1431f2b6e9d9e33ed522cd853bcd326bbb3414a 100644 (file)
@@ -668,6 +668,11 @@ void UnusedTypeRemover::visit(VariableDeclaration &var)
        unused_nodes.erase(var.type_declaration);
 }
 
+void UnusedTypeRemover::visit(InterfaceBlock &iface)
+{
+       unused_nodes.erase(iface.type_declaration);
+}
+
 void UnusedTypeRemover::visit(FunctionDeclaration &func)
 {
        unused_nodes.erase(func.return_type_declaration);
@@ -690,9 +695,19 @@ bool UnusedVariableRemover::apply(Stage &stage)
        BlockVariableMap &global_variables = variables.back();
        for(BlockVariableMap::iterator i=global_variables.begin(); i!=global_variables.end(); ++i)
        {
+               string interface = i->first->interface;
+               bool linked = i->first->linked_declaration;
+               map<VariableDeclaration *, Node *>::iterator j = aggregates.find(i->first);
+               if(j!=aggregates.end())
+                       if(InterfaceBlock *iface = dynamic_cast<InterfaceBlock *>(j->second))
+                       {
+                               interface = iface->interface;
+                               linked = iface->linked_block;
+                       }
+
                /* Don't remove output variables which are used by the next stage or the
                graphics API. */
-               if(i->first->interface=="out" && (stage.type==Stage::FRAGMENT || i->first->linked_declaration || !i->first->name.compare(0, 3, "gl_")))
+               if(interface=="out" && (stage.type==Stage::FRAGMENT || linked || !i->first->name.compare(0, 3, "gl_")))
                        continue;
 
                // Mark other unreferenced global variables as unused.
@@ -833,7 +848,7 @@ void UnusedVariableRemover::visit(InterfaceBlock &iface)
 {
        SetForScope<Node *> set(aggregate, &iface);
        unused_nodes.insert(&iface);
-       TraversingVisitor::visit(iface);
+       iface.struct_declaration->members.visit(*this);
 }
 
 void UnusedVariableRemover::visit(FunctionDeclaration &func)
index 652ac252cf32bf5a3015f3e87441826df8b29296..551e2d56976155a0d5b8b1e5c56e848fe95281e1 100644 (file)
@@ -184,6 +184,7 @@ private:
        virtual void visit(ImageTypeDeclaration &);
        virtual void visit(StructDeclaration &);
        virtual void visit(VariableDeclaration &);
+       virtual void visit(InterfaceBlock &);
        virtual void visit(FunctionDeclaration &);
 };
 
index d44dbff99de3f1c0e6b59f244169ff18072f61d9..52371d1bfdd79f9f7de91ff457d10001952f6c62 100644 (file)
@@ -94,7 +94,7 @@ void Formatter::visit(Block &block)
        bool first = true;
        for(NodeList<Statement>::iterator i=block.body.begin(); i!=block.body.end(); ++i)
        {
-               if((*i)->source==BUILTIN_SOURCE)
+               if((*i)->source<=BUILTIN_SOURCE)
                        continue;
                if(!first)
                        append('\n');
@@ -229,7 +229,7 @@ void Formatter::visit(VariableDeclaration &var)
                append(format("%s ", var.interpolation));
        if(!var.sampling.empty())
                append(format("%s ", var.sampling));
-       if(!var.interface.empty() && var.interface!=block_interface)
+       if(!var.interface.empty())
        {
                string interface = var.interface;
                if(mode==Compiler::PROGRAM && stage && stage->required_features.glsl_version<Version(1, 30))
@@ -265,9 +265,9 @@ void Formatter::visit(VariableDeclaration &var)
 
 void Formatter::visit(InterfaceBlock &iface)
 {
-       SetForScope<string> set(block_interface, iface.interface);
        append(format("%s %s\n", iface.interface, iface.name));
-       iface.members.visit(*this);
+       if(iface.struct_declaration)
+               iface.struct_declaration->members.visit(*this);
        if(!iface.instance_name.empty())
        {
                append(' ');
index 69a99d1e5e67979f9dddcd48d5db4c236ce5882e..f720606eadbda1eeef757a39a7bb75bfbd4696d1 100644 (file)
@@ -20,7 +20,6 @@ private:
        unsigned source_line;
        unsigned indent;
        bool parameter_list;
-       std::string block_interface;
 
 public:
        Formatter();
index d2261126a652d109deba3c4193713568dc317616..fd782648083bdb05a1aa700ca65d4ab8f15aa926 100644 (file)
@@ -776,7 +776,8 @@ RefPtr<InterfaceBlock> Parser::parse_interface_block()
                throw parse_error(tokenizer.get_location(), iface->interface, "an interface qualifier");
 
        iface->name = expect_identifier();
-       parse_block(iface->members, true, &Parser::parse_variable_declaration_with_layout);
+       iface->members = new Block;
+       parse_block(*iface->members, true, &Parser::parse_variable_declaration_with_layout);
        if(!check(";"))
        {
                iface->instance_name = expect_identifier();
index d7eec3da4bf680cbf4125d0cb747feccd54af38d..cb77419763645be5987842b9bd7d654eb30e13c5 100644 (file)
@@ -272,11 +272,24 @@ void ImageTypeDeclaration::visit(NodeVisitor &visitor)
 }
 
 
-StructDeclaration::StructDeclaration()
+StructDeclaration::StructDeclaration():
+       interface_block(0)
 {
        members.use_braces = true;
 }
 
+StructDeclaration::StructDeclaration(const StructDeclaration &other):
+       TypeDeclaration(other),
+       members(other.members),
+       interface_block(0)
+{ }
+
+StructDeclaration::~StructDeclaration()
+{
+       if(interface_block && interface_block->struct_declaration==this)
+               interface_block->struct_declaration = 0;
+}
+
 void StructDeclaration::visit(NodeVisitor &visitor)
 {
        visitor.visit(*this);
@@ -321,10 +334,10 @@ void VariableDeclaration::visit(NodeVisitor &visitor)
 
 InterfaceBlock::InterfaceBlock():
        array(false),
+       type_declaration(0),
+       struct_declaration(0),
        linked_block(0)
-{
-       members.use_braces = true;
-}
+{ }
 
 InterfaceBlock::InterfaceBlock(const InterfaceBlock &other):
        Statement(other),
@@ -333,6 +346,8 @@ InterfaceBlock::InterfaceBlock(const InterfaceBlock &other):
        members(other.members),
        instance_name(other.instance_name),
        array(other.array),
+       type_declaration(0),
+       struct_declaration(0),
        linked_block(0)
 { }
 
@@ -340,6 +355,8 @@ InterfaceBlock::~InterfaceBlock()
 {
        if(linked_block && linked_block->linked_block==this)
                linked_block->linked_block = 0;
+       if(struct_declaration && struct_declaration->interface_block==this)
+               struct_declaration->interface_block = 0;
 }
 
 void InterfaceBlock::visit(NodeVisitor &visitor)
index 2c832c149e2467ba346867eb453cc8850b7a11a6..30acca42b957bd42b547b642173032160fcdd0df 100644 (file)
@@ -48,6 +48,7 @@ struct Operator
 
 enum
 {
+       INTERNAL_SOURCE = -2,
        BUILTIN_SOURCE = -1,
        GENERATED_SOURCE = 0
 };
@@ -352,7 +353,11 @@ struct StructDeclaration: TypeDeclaration
 {
        Block members;
 
+       InterfaceBlock *interface_block;
+
        StructDeclaration();
+       StructDeclaration(const StructDeclaration &);
+       ~StructDeclaration();
 
        virtual StructDeclaration *clone() const { return new StructDeclaration(*this); }
        virtual void visit(NodeVisitor &);
@@ -387,10 +392,14 @@ struct InterfaceBlock: Statement
 {
        std::string interface;
        std::string name;
-       Block members;
+       NodePtr<Block> members;
        std::string instance_name;
        bool array;
 
+       /* An interface block's ultimate base type is always a struct.  The
+       immediate type may be either that same struct or an array of it. */
+       TypeDeclaration *type_declaration;
+       StructDeclaration *struct_declaration;
        InterfaceBlock *linked_block;
 
        InterfaceBlock();
index fcccb526e0dc3bc62831e3c949f8bb767b929591..b0c6e3d67acb29cb178bbdeb1e7d2812ea16f5be 100644 (file)
@@ -149,8 +149,13 @@ void DeclarationValidator::visit(InterfaceBlock &iface)
        if(!iface.instance_name.empty())
                check_definition(iface.instance_name, iface);
 
-       SetFlag set_anon(anonymous_block, iface.instance_name.empty());
-       TraversingVisitor::visit(iface);
+       if(iface.instance_name.empty() && iface.struct_declaration)
+       {
+               // Inject anonymous interface block members into the global scope
+               const map<string, VariableDeclaration *> &iface_vars = iface.struct_declaration->members.variables;
+               for(map<string, VariableDeclaration *>::const_iterator j=iface_vars.begin(); j!=iface_vars.end(); ++j)
+                       check_definition(j->first, *j->second);
+       }
 }
 
 void DeclarationValidator::visit(FunctionDeclaration &func)
@@ -207,6 +212,13 @@ void ReferenceValidator::visit(VariableDeclaration &var)
        TraversingVisitor::visit(var);
 }
 
+void ReferenceValidator::visit(InterfaceBlock &iface)
+{
+       if(!iface.struct_declaration)
+               error(iface, format("Interface block '%s %s' lacks a struct declaration", iface.interface, iface.name));
+       TraversingVisitor::visit(iface);
+}
+
 void ReferenceValidator::visit(FunctionDeclaration &func)
 {
        if(!func.return_type_declaration)
index 36b702e01fbb395a58c8c8a2f6c6916a83669429..e5b8ed4ebfa9397bea0220540b6f3719acf6a0b0 100644 (file)
@@ -78,6 +78,7 @@ private:
        virtual void visit(VariableReference &);
        virtual void visit(InterfaceBlockReference &);
        virtual void visit(VariableDeclaration &);
+       virtual void visit(InterfaceBlock &);
        virtual void visit(FunctionDeclaration &);
 };
 
index c740d1188a15cf2f02614f19248ddd2a2c259e75..ebcedb71242cc883bd2ef7b7c58ac6783867a19a 100644 (file)
@@ -76,7 +76,8 @@ void TraversingVisitor::visit(VariableDeclaration &var)
 
 void TraversingVisitor::visit(InterfaceBlock &iface)
 {
-       iface.members.visit(*this);
+       if(iface.members)
+               iface.members->visit(*this);
 }
 
 void TraversingVisitor::visit(FunctionDeclaration &func)