From f08bd843fbe63a0bf5bcbc21308f2751d08f00c1 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Wed, 24 Feb 2021 15:51:30 +0200 Subject: [PATCH] Refactor interface management Rather than having separate maps in Stage, pull the variables from the content block's map. --- source/glsl/debug.cpp | 7 ++-- source/glsl/generate.cpp | 71 +++++++++++++++++++++------------------- source/glsl/syntax.cpp | 2 -- source/glsl/syntax.h | 6 ++-- source/glsl/visitor.cpp | 6 ++-- source/glsl/visitor.h | 1 - 6 files changed, 46 insertions(+), 47 deletions(-) diff --git a/source/glsl/debug.cpp b/source/glsl/debug.cpp index 32eb0ec3..b3eabd75 100644 --- a/source/glsl/debug.cpp +++ b/source/glsl/debug.cpp @@ -12,10 +12,6 @@ const std::string &DumpTree::apply(Stage &stage) { formatted = format("Stage %s\n", Stage::get_stage_name(stage.type)); tree.push_back(BRANCH); - for(map::const_iterator i=stage.in_variables.begin(); i!=stage.in_variables.end(); ++i) - append(format("Input: %%%d %s %s", get_label(*i->second), i->second->type, i->first)); - for(map::const_iterator i=stage.out_variables.begin(); i!=stage.out_variables.end(); ++i) - append(format("Output: %%%d %s %s", get_label(*i->second), i->second->type, i->first)); last_branch(); stage.content.visit(*this); return formatted; @@ -88,6 +84,9 @@ void DumpTree::visit(Block &block) for(std::map::const_iterator i=block.variables.begin(); i!=block.variables.end(); ++i) append(format("Variable %%%d %s %s", get_label(*i->second), i->second->type, i->first)); + for(std::set::const_iterator i=block.interfaces.begin(); i!=block.interfaces.end(); ++i) + append(format("Interface %%%d %s %s", get_label(*i->second), i->second->interface, i->second->name)); + bool labeled_body = (!block.types.empty() || !block.variables.empty()); if(labeled_body) { diff --git a/source/glsl/generate.cpp b/source/glsl/generate.cpp index 62c879c8..48de1efc 100644 --- a/source/glsl/generate.cpp +++ b/source/glsl/generate.cpp @@ -80,7 +80,7 @@ void BlockResolver::enter(Block &block) void BlockResolver::visit(InterfaceBlock &iface) { - iface.members.anonymous = true; + current_block->interfaces.insert(&iface); TraversingVisitor::visit(iface); } @@ -114,11 +114,23 @@ void VariableResolver::visit(VariableReference &var) type = 0; for(Block *block=current_block; block; block=next_block(*block)) { - map::iterator j = block->variables.find(var.name); - if(j!=block->variables.end()) + map::iterator i = block->variables.find(var.name); + if(i!=block->variables.end()) + var.declaration = i->second; + else + { + const set &ifaces = block->interfaces; + for(set::const_iterator j=ifaces.begin(); (!var.declaration && j!=ifaces.end()); ++j) + { + i = (*j)->members.variables.find(var.name); + if(i!=(*j)->members.variables.end()) + var.declaration = i->second; + } + } + + if(var.declaration) { - var.declaration = j->second; - type = j->second->type_declaration; + type = var.declaration->type_declaration; break; } } @@ -208,8 +220,6 @@ void VariableResolver::visit(VariableDeclaration &var) TraversingVisitor::visit(var); current_block->variables[var.name] = &var; - if(current_block->anonymous && current_block->parent) - current_block->parent->variables[var.name] = &var; } void VariableResolver::visit(InterfaceBlock &iface) @@ -292,8 +302,7 @@ string InterfaceGenerator::change_prefix(const string &name, const string &prefi bool InterfaceGenerator::generate_interface(VariableDeclaration &var, const string &iface, const string &name) { - const map &stage_vars = (iface=="in" ? stage->in_variables : stage->out_variables); - if(stage_vars.count(name)) + if(stage->content.variables.count(name)) return false; VariableDeclaration* iface_var = new VariableDeclaration; @@ -309,12 +318,12 @@ bool InterfaceGenerator::generate_interface(VariableDeclaration &var, const stri if(iface_var->array) iface_var->array_size = var.array_size; if(iface=="in") - iface_var->linked_declaration = &var; - stage->content.body.insert(iface_insert_point, iface_var); { - SetForScope set_block(current_block, &stage->content); - iface_var->visit(*this); + iface_var->linked_declaration = &var; + var.linked_declaration = iface_var; } + stage->content.body.insert(iface_insert_point, iface_var); + stage->content.variables[name] = iface_var; return true; } @@ -342,14 +351,14 @@ void InterfaceGenerator::visit(VariableReference &var) return; /* Don't pull a variable from previous stage if we just generated an out interface in this stage */ - if(stage->out_variables.count(var.name)) + if(stage->content.variables.count(var.name)) return; - const map &prev_out = stage->previous->out_variables; - map::const_iterator i = prev_out.find(var.name); - if(i==prev_out.end()) - i = prev_out.find(in_prefix+var.name); - if(i!=prev_out.end()) + const map &prev_vars = stage->previous->content.variables; + map::const_iterator i = prev_vars.find(var.name); + if(i==prev_vars.end() || i->second->interface!="out") + 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; @@ -360,9 +369,7 @@ void InterfaceGenerator::visit(VariableDeclaration &var) { if(var.interface=="out") { - if(current_block==&stage->content) - stage->out_variables[var.name] = &var; - else if(generate_interface(var, "out", change_prefix(var.name, string()))) + if(current_block!=&stage->content && generate_interface(var, "out", change_prefix(var.name, string()))) { nodes_to_remove.insert(&var); if(var.init_expression) @@ -376,14 +383,11 @@ void InterfaceGenerator::visit(VariableDeclaration &var) } else if(var.interface=="in") { - stage->in_variables[var.name] = &var; - if(var.linked_declaration) - var.linked_declaration->linked_declaration = &var; - else if(stage->previous) + if(!var.linked_declaration && stage->previous) { - const map &prev_out = stage->previous->out_variables; - map::const_iterator i = prev_out.find(var.name); - if(i!=prev_out.end()) + const map &prev_vars = stage->previous->content.variables; + map::const_iterator i = prev_vars.find(var.name); + if(i!=prev_vars.end() && i->second->interface=="out") { var.linked_declaration = i->second; i->second->linked_declaration = &var; @@ -404,13 +408,14 @@ void InterfaceGenerator::visit(Passthrough &pass) { vector pass_vars; - for(map::const_iterator i=stage->in_variables.begin(); i!=stage->in_variables.end(); ++i) - pass_vars.push_back(i->second); + for(map::const_iterator i=stage->content.variables.begin(); i!=stage->content.variables.end(); ++i) + if(i->second->interface=="in") + pass_vars.push_back(i->second); if(stage->previous) { - const map &prev_out = stage->previous->out_variables; - for(map::const_iterator i=prev_out.begin(); i!=prev_out.end(); ++i) + const map &prev_vars = stage->previous->content.variables; + for(map::const_iterator i=prev_vars.begin(); i!=prev_vars.end(); ++i) { bool linked = false; for(vector::const_iterator j=pass_vars.begin(); (!linked && j!=pass_vars.end()); ++j) diff --git a/source/glsl/syntax.cpp b/source/glsl/syntax.cpp index 0b9ecb54..b20f63ca 100644 --- a/source/glsl/syntax.cpp +++ b/source/glsl/syntax.cpp @@ -74,7 +74,6 @@ Statement::Statement(): Block::Block(): use_braces(false), - anonymous(false), parent(0) { } @@ -82,7 +81,6 @@ Block::Block(const Block &other): Node(other), body(other.body), use_braces(other.use_braces), - anonymous(other.anonymous), parent(0) { } diff --git a/source/glsl/syntax.h b/source/glsl/syntax.h index 70b1bb39..4ff05fa1 100644 --- a/source/glsl/syntax.h +++ b/source/glsl/syntax.h @@ -3,6 +3,7 @@ #include #include +#include #include #include #include @@ -90,6 +91,7 @@ class NodeArray: public NodeContainer > > struct StructDeclaration; struct VariableDeclaration; +struct InterfaceBlock; struct FunctionDeclaration; struct Statement: Node @@ -106,9 +108,9 @@ struct Block: Node { NodeList body; bool use_braces; - bool anonymous; std::map types; std::map variables; + std::set interfaces; Block *parent; Block(); @@ -383,8 +385,6 @@ struct Stage Type type; Stage *previous; Block content; - std::map in_variables; - std::map out_variables; std::map locations; Features required_features; diff --git a/source/glsl/visitor.cpp b/source/glsl/visitor.cpp index 0577b5ab..c27de118 100644 --- a/source/glsl/visitor.cpp +++ b/source/glsl/visitor.cpp @@ -165,10 +165,6 @@ void NodeRemover::visit(VariableDeclaration &var) if(recursive_remove || to_remove->count(&var)) { remove_variable(current_block->variables, var); - if(current_block->anonymous && current_block->parent) - remove_variable(current_block->parent->variables, var); - remove_variable(stage->in_variables, var); - remove_variable(stage->out_variables, var); stage->locations.erase(var.name); if(var.linked_declaration) var.linked_declaration->linked_declaration = 0; @@ -179,6 +175,8 @@ void NodeRemover::visit(VariableDeclaration &var) void NodeRemover::visit(InterfaceBlock &iface) { + if(to_remove->count(&iface)) + current_block->interfaces.erase(&iface); SetFlag set_recursive(recursive_remove, recursive_remove || to_remove->count(&iface)); TraversingVisitor::visit(iface); } diff --git a/source/glsl/visitor.h b/source/glsl/visitor.h index 685bb4ee..2a4ef549 100644 --- a/source/glsl/visitor.h +++ b/source/glsl/visitor.h @@ -86,7 +86,6 @@ class NodeRemover: private TraversingVisitor private: Stage *stage; const std::set *to_remove; - bool anonymous; bool recursive_remove; public: -- 2.45.2