From: Mikko Rasa Date: Thu, 25 Feb 2021 23:03:36 +0000 (+0200) Subject: Store types and interface blocks in Stage X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=7daccf2582084283a9e5064995f674eef835996e;p=libs%2Fgl.git Store types and interface blocks in Stage They're only allowed at top level so it seems pointless for every block to have the maps. --- diff --git a/source/glsl/debug.cpp b/source/glsl/debug.cpp index 62cf7a94..c29a60ce 100644 --- a/source/glsl/debug.cpp +++ b/source/glsl/debug.cpp @@ -13,6 +13,20 @@ const std::string &DumpTree::apply(Stage &stage) formatted = format("Stage: %s\n", Stage::get_stage_name(stage.type)); tree.push_back(BRANCH); append(format("Version: %d.%02d", stage.required_features.glsl_version.major, stage.required_features.glsl_version.minor)); + + for(std::map::const_iterator i=stage.types.begin(); i!=stage.types.end(); ++i) + append(format("Type: %%%d %s", get_label(*i->second), i->first)); + + set seen_interfaces; + for(std::map::const_iterator i=stage.interface_blocks.begin(); i!=stage.interface_blocks.end(); ++i) + if(seen_interfaces.insert(i->second).second) + { + string text = format("Interface block: %%%d %s %s", get_label(*i->second), i->second->interface, i->second->name); + if(!i->second->instance_name.empty()) + text += format(" %s", i->second->instance_name); + append(text); + } + last_branch(); stage.content.visit(*this); return formatted; @@ -79,23 +93,10 @@ void DumpTree::visit(Block &block) append(format("Block %s", (block.use_braces ? "{}" : "(inline)"))); begin_sub(); - for(std::map::const_iterator i=block.types.begin(); i!=block.types.end(); ++i) - append(format("Type: %%%d %s", get_label(*i->second), i->first)); - 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)); - set seen_interfaces; - for(std::map::const_iterator i=block.interfaces.begin(); i!=block.interfaces.end(); ++i) - if(seen_interfaces.insert(i->second).second) - { - string text = format("Interface: %%%d %s %s", get_label(*i->second), i->second->interface, i->second->name); - if(!i->second->instance_name.empty()) - text += format(" %s", i->second->instance_name); - append(text); - } - - bool labeled_body = (!block.types.empty() || !block.variables.empty()); + bool labeled_body = !block.variables.empty(); if(labeled_body) { last_branch(); diff --git a/source/glsl/generate.cpp b/source/glsl/generate.cpp index 751043d9..c073d870 100644 --- a/source/glsl/generate.cpp +++ b/source/glsl/generate.cpp @@ -80,6 +80,7 @@ void BlockResolver::enter(Block &block) VariableResolver::VariableResolver(): + stage(0), builtins(0), members(0), record_target(false), @@ -87,11 +88,12 @@ VariableResolver::VariableResolver(): self_referencing(false) { } -void VariableResolver::apply(Stage &stage) +void VariableResolver::apply(Stage &s) { - Stage *builtin_stage = get_builtins(stage.type); + stage = &s; + Stage *builtin_stage = get_builtins(s.type); builtins = (builtin_stage ? &builtin_stage->content : 0); - stage.content.visit(*this); + s.content.visit(*this); } Block *VariableResolver::next_block(Block &block) @@ -108,38 +110,38 @@ void VariableResolver::visit(VariableReference &var) { var.declaration = 0; members = 0; - for(Block *block=current_block; block; block=next_block(*block)) + for(Block *block=current_block; (!var.declaration && block); block=next_block(*block)) { map::iterator i = block->variables.find(var.name); if(i!=block->variables.end()) var.declaration = i->second; - else - { - const map &ifaces = block->interfaces; - for(map::const_iterator j=ifaces.begin(); (!var.declaration && j!=ifaces.end()); ++j) - if(j->second->instance_name.empty()) - { - i = j->second->members.variables.find(var.name); - if(i!=j->second->members.variables.end()) - var.declaration = i->second; - } - } - - if(var.declaration) - { - if(var.declaration->type_declaration) - members = &var.declaration->type_declaration->members.variables; - break; - } + } - map::iterator j = block->interfaces.find(var.name); - if(j!=block->interfaces.end() && j->second->instance_name==var.name) + if(var.declaration) + { + if(var.declaration->type_declaration) + members = &var.declaration->type_declaration->members.variables; + } + else + { + const map &blocks = stage->interface_blocks; + map::const_iterator i = blocks.find(var.name); + if(i!=blocks.end() && i->second->instance_name==var.name) { iface_ref = new InterfaceBlockReference; iface_ref->name = var.name; - iface_ref->declaration = j->second; - members = &j->second->members.variables; - break; + iface_ref->declaration = i->second; + members = &i->second->members.variables; + } + else + { + for(i=blocks.begin(); (!var.declaration && i!=blocks.end()); ++i) + if(i->second->instance_name.empty()) + { + map::iterator j = i->second->members.variables.find(var.name); + if(j!=i->second->members.variables.end()) + var.declaration = j->second; + } } } @@ -162,11 +164,11 @@ void VariableResolver::visit(InterfaceBlockReference &iface) iface.declaration = 0; for(Block *block=current_block; block; block=next_block(*block)) { - map::iterator j = block->interfaces.find(iface.name); - if(j!=block->interfaces.end()) + map::iterator i = stage->interface_blocks.find(iface.name); + if(i!=stage->interface_blocks.end()) { - iface.declaration = j->second; - members = &j->second->members.variables; + iface.declaration = i->second; + members = &i->second->members.variables; break; } } @@ -237,17 +239,14 @@ void VariableResolver::visit(Assignment &assign) void VariableResolver::visit(StructDeclaration &strct) { TraversingVisitor::visit(strct); - current_block->types[strct.name] = &strct; + stage->types[strct.name] = &strct; } void VariableResolver::visit(VariableDeclaration &var) { - for(Block *block=current_block; block; block=next_block(*block)) - { - map::iterator j = block->types.find(var.type); - if(j!=block->types.end()) - var.type_declaration = j->second; - } + map::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; @@ -260,9 +259,9 @@ 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. */ - current_block->interfaces[iface.name] = &iface; + stage->interface_blocks[iface.name] = &iface; if(!iface.instance_name.empty()) - current_block->interfaces[iface.instance_name] = &iface; + stage->interface_blocks[iface.instance_name] = &iface; SetForScope set_iface(block_interface, iface.interface); TraversingVisitor::visit(iface); @@ -376,7 +375,7 @@ bool InterfaceGenerator::generate_interface(VariableDeclaration &var, const stri bool InterfaceGenerator::generate_interface(InterfaceBlock &out_block) { - if(stage->content.interfaces.count(out_block.name)) + if(stage->interface_blocks.count(out_block.name)) return false; InterfaceBlock *in_block = new InterfaceBlock; @@ -398,9 +397,9 @@ bool InterfaceGenerator::generate_interface(InterfaceBlock &out_block) } iface_target_block->body.insert(iface_insert_point, in_block); - iface_target_block->interfaces[in_block->name] = in_block; + stage->interface_blocks[in_block->name] = in_block; if(!in_block->instance_name.empty()) - stage->content.interfaces[in_block->instance_name] = in_block; + stage->interface_blocks[in_block->instance_name] = in_block; SetFlag set_scope(function_scope, false); SetForScope set_block(current_block, &stage->content); @@ -446,7 +445,7 @@ void InterfaceGenerator::visit(VariableReference &var) return; } - const map &prev_blocks = stage->previous->content.interfaces; + const map &prev_blocks = stage->previous->interface_blocks; map::const_iterator j = prev_blocks.find(var.name); if(j!=prev_blocks.end() && j->second->interface=="out" && j->second->instance_name==var.name) { @@ -527,7 +526,7 @@ void InterfaceGenerator::visit(InterfaceBlock &iface) { if(!iface.linked_block && stage->previous) { - const map &prev_blocks = stage->previous->content.interfaces; + const map &prev_blocks = stage->previous->interface_blocks; map::const_iterator i = prev_blocks.find(iface.name); if(i!=prev_blocks.end() && i->second->interface=="out" && i->second->name==iface.name) { diff --git a/source/glsl/generate.h b/source/glsl/generate.h index d0cbc08f..f1f10b89 100644 --- a/source/glsl/generate.h +++ b/source/glsl/generate.h @@ -39,6 +39,7 @@ private: class VariableResolver: private TraversingVisitor { private: + Stage *stage; Block *builtins; std::map *members; RefPtr iface_ref; diff --git a/source/glsl/syntax.h b/source/glsl/syntax.h index 7729611c..49699f2e 100644 --- a/source/glsl/syntax.h +++ b/source/glsl/syntax.h @@ -108,9 +108,7 @@ struct Block: Node { NodeList body; bool use_braces; - std::map types; std::map variables; - std::map interfaces; Block *parent; Block(); @@ -401,6 +399,8 @@ struct Stage Type type; Stage *previous; Block content; + std::map types; + std::map interface_blocks; std::map locations; Features required_features; diff --git a/source/glsl/visitor.cpp b/source/glsl/visitor.cpp index 051d92f6..fd25bf34 100644 --- a/source/glsl/visitor.cpp +++ b/source/glsl/visitor.cpp @@ -158,7 +158,7 @@ void NodeRemover::visit(Block &block) void NodeRemover::visit(StructDeclaration &strct) { if(to_remove->count(&strct)) - remove_from_map(current_block->types, strct.name, strct); + remove_from_map(stage->types, strct.name, strct); } void NodeRemover::visit(VariableDeclaration &var) @@ -178,9 +178,9 @@ void NodeRemover::visit(InterfaceBlock &iface) { if(to_remove->count(&iface)) { - remove_from_map(current_block->interfaces, iface.name, iface); + remove_from_map(stage->interface_blocks, iface.name, iface); if(!iface.instance_name.empty()) - remove_from_map(current_block->interfaces, iface.instance_name, iface); + remove_from_map(stage->interface_blocks, iface.instance_name, iface); } SetFlag set_recursive(recursive_remove, recursive_remove || to_remove->count(&iface)); TraversingVisitor::visit(iface);