From 584cf5e42c4758a3f4d197655d861d31ee23db4e Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Sat, 24 Apr 2021 17:55:14 +0300 Subject: [PATCH] Handle function forward declarations in the SPIR-V generator --- source/glsl/spirv.cpp | 30 ++++++++++++++++++++++++++++-- source/glsl/spirv.h | 1 + 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/source/glsl/spirv.cpp b/source/glsl/spirv.cpp index 7d6edf65..70d5fe78 100644 --- a/source/glsl/spirv.cpp +++ b/source/glsl/spirv.cpp @@ -237,8 +237,28 @@ SpirVGenerator::Id SpirVGenerator::get_id(Node &node) const SpirVGenerator::Id SpirVGenerator::allocate_id(Node &node, Id type_id) { + map::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::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 param_type_ids; diff --git a/source/glsl/spirv.h b/source/glsl/spirv.h index add5a967..26467bcd 100644 --- a/source/glsl/spirv.h +++ b/source/glsl/spirv.h @@ -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); -- 2.45.2