]> git.tdb.fi Git - libs/gl.git/commitdiff
Handle function forward declarations in the SPIR-V generator
authorMikko Rasa <tdb@tdb.fi>
Sat, 24 Apr 2021 14:55:14 +0000 (17:55 +0300)
committerMikko Rasa <tdb@tdb.fi>
Sat, 24 Apr 2021 14:55:31 +0000 (17:55 +0300)
source/glsl/spirv.cpp
source/glsl/spirv.h

index 7d6edf656a4097607e70b1a68208f3c523e9d212..70d5fe789f125ecd04e0e6bb89d58614f6b39b4c 100644 (file)
@@ -237,8 +237,28 @@ SpirVGenerator::Id SpirVGenerator::get_id(Node &node) const
 
 SpirVGenerator::Id SpirVGenerator::allocate_id(Node &node, Id type_id)
 {
+       map<Node *, Declaration>::iterator i = declared_ids.find(&node);
+       if(i!=declared_ids.end())
+       {
+               if(i->second.type_id)
+                       throw key_error(&node);
+               i->second.type_id = type_id;
+               return i->second.id;
+       }
+
+       Id id = next_id++;
+       declared_ids.insert(make_pair(&node, Declaration(id, type_id)));
+       return id;
+}
+
+SpirVGenerator::Id SpirVGenerator::allocate_forward_id(Node &node)
+{
+       map<Node *, Declaration>::iterator i = declared_ids.find(&node);
+       if(i!=declared_ids.end())
+               return i->second.id;
+
        Id id = next_id++;
-       insert_unique(declared_ids, &node, Declaration(id, type_id));
+       declared_ids.insert(make_pair(&node, Declaration(id, 0)));
        return id;
 }
 
@@ -1706,8 +1726,14 @@ void SpirVGenerator::visit_entry_point(FunctionDeclaration &func, Id func_id)
 
 void SpirVGenerator::visit(FunctionDeclaration &func)
 {
-       if(func.source==BUILTIN_SOURCE || func.definition!=&func)
+       if(func.source==BUILTIN_SOURCE)
                return;
+       else if(func.definition!=&func)
+       {
+               if(func.definition)
+                       allocate_forward_id(*func.definition);
+               return;
+       }
 
        Id return_type_id = get_id(*func.return_type_declaration);
        vector<unsigned> param_type_ids;
index add5a967d3659d525901101cb25e3712601a2f03..26467bcd15a3941858ceb82b6bf3cc1eb0fc4c84 100644 (file)
@@ -113,6 +113,7 @@ private:
        Id import_extension(const std::string &);
        Id get_id(Node &) const;
        Id allocate_id(Node &, Id);
+       Id allocate_forward_id(Node &);
        Id write_constant(Id, Word, bool);
        static ConstantKey get_constant_key(Id, const Variant &value);
        Id get_constant_id(Id, const Variant &value);