X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fglsl%2Fgenerate.cpp;h=ca4d41e64c7a8c63bf51fd8d446e5ccb4dc8bd5a;hb=5c33b56c3b97ca0381ac216a603c7553f4bea499;hp=3707e4eda2cabc902397f0d9a344950462f24fcc;hpb=488ffa4e7136cb163004befa67f20f553bc82f2e;p=libs%2Fgl.git diff --git a/source/glsl/generate.cpp b/source/glsl/generate.cpp index 3707e4ed..ca4d41e6 100644 --- a/source/glsl/generate.cpp +++ b/source/glsl/generate.cpp @@ -8,22 +8,17 @@ namespace Msp { namespace GL { namespace SL { -DeclarationCombiner::DeclarationCombiner(): - toplevel(true) -{ } - void DeclarationCombiner::apply(Stage &stage) { - visit(stage.content); + stage.content.visit(*this); NodeRemover().apply(stage, nodes_to_remove); } void DeclarationCombiner::visit(Block &block) { - if(!toplevel) + if(current_block) return; - SetForScope set(toplevel, false); TraversingVisitor::visit(block); } @@ -57,8 +52,9 @@ void DeclarationCombiner::visit(VariableDeclaration &var) { bool found = false; for(vector::iterator j=ptr->layout->qualifiers.begin(); (!found && j!=ptr->layout->qualifiers.end()); ++j) - if(j->identifier==i->identifier) + if(j->name==i->name) { + j->has_value = i->value; j->value = i->value; found = true; } @@ -77,8 +73,20 @@ void DeclarationCombiner::visit(VariableDeclaration &var) } +void BlockResolver::visit(Block &block) +{ + block.parent = current_block; + TraversingVisitor::visit(block); +} + +void BlockResolver::visit(InterfaceBlock &iface) +{ + iface.members.anonymous = true; + TraversingVisitor::visit(iface); +} + + VariableResolver::VariableResolver(): - anonymous(false), record_target(false), assignment_target(0), self_referencing(false) @@ -86,34 +94,32 @@ VariableResolver::VariableResolver(): void VariableResolver::apply(Stage &stage) { - Stage *builtins = get_builtins(stage.type); - if(builtins) - blocks.push_back(&builtins->content); - visit(stage.content); - if(builtins) - blocks.pop_back(); + Stage *builtin_stage = get_builtins(stage.type); + builtins = (builtin_stage ? &builtin_stage->content : 0); + stage.content.visit(*this); +} + +Block *VariableResolver::next_block(Block &block) +{ + return block.parent ? block.parent : &block!=builtins ? builtins : 0; } void VariableResolver::visit(Block &block) { - if(!blocks.empty() && blocks.back()==&block) - return TraversingVisitor::visit(block); + if(current_block!=&block) + block.variables.clear(); - blocks.push_back(&block); - block.variables.clear(); TraversingVisitor::visit(block); - blocks.pop_back(); } void VariableResolver::visit(VariableReference &var) { var.declaration = 0; type = 0; - for(vector::iterator i=blocks.end(); i!=blocks.begin(); ) + for(Block *block=current_block; block; block=next_block(*block)) { - --i; - map::iterator j = (*i)->variables.find(var.name); - if(j!=(*i)->variables.end()) + map::iterator j = block->variables.find(var.name); + if(j!=block->variables.end()) { var.declaration = j->second; type = j->second->type_declaration; @@ -189,16 +195,15 @@ void VariableResolver::visit(Assignment &assign) void VariableResolver::visit(StructDeclaration &strct) { TraversingVisitor::visit(strct); - blocks.back()->types[strct.name] = &strct; + current_block->types[strct.name] = &strct; } void VariableResolver::visit(VariableDeclaration &var) { - for(vector::iterator i=blocks.end(); i!=blocks.begin(); ) + for(Block *block=current_block; block; block=next_block(*block)) { - --i; - map::iterator j = (*i)->types.find(var.type); - if(j!=(*i)->types.end()) + map::iterator j = block->types.find(var.type); + if(j!=block->types.end()) var.type_declaration = j->second; } @@ -206,32 +211,29 @@ void VariableResolver::visit(VariableDeclaration &var) var.interface = block_interface; TraversingVisitor::visit(var); - blocks.back()->variables[var.name] = &var; - if(anonymous && blocks.size()>1) - blocks[blocks.size()-2]->variables[var.name] = &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) { - SetFlag set(anonymous); - SetForScope set2(block_interface, iface.interface); + SetForScope set_iface(block_interface, iface.interface); TraversingVisitor::visit(iface); } void VariableResolver::visit(FunctionDeclaration &func) { - blocks.push_back(&func.body); + SetForScope set_block(current_block, &func.body); func.body.variables.clear(); TraversingVisitor::visit(func); - blocks.pop_back(); } void VariableResolver::visit(Iteration &iter) { - blocks.push_back(&iter.body); + SetForScope set_block(current_block, &iter.body); iter.body.variables.clear(); TraversingVisitor::visit(iter); - blocks.pop_back(); } @@ -264,9 +266,7 @@ void FunctionResolver::visit(FunctionDeclaration &func) InterfaceGenerator::InterfaceGenerator(): - stage(0), - scope_level(0), - current_block(0) + stage(0) { } string InterfaceGenerator::get_out_prefix(Stage::Type type) @@ -285,18 +285,17 @@ void InterfaceGenerator::apply(Stage &s) if(stage->previous) in_prefix = get_out_prefix(stage->previous->type); out_prefix = get_out_prefix(stage->type); - visit(s.content); + s.content.visit(*this); NodeRemover().apply(s, nodes_to_remove); } void InterfaceGenerator::visit(Block &block) { - SetForScope set(scope_level, scope_level+1); SetForScope set_block(current_block, &block); for(NodeList::iterator i=block.body.begin(); i!=block.body.end(); ++i) { assignment_insert_point = i; - if(scope_level==1) + if(&block==&stage->content) iface_insert_point = i; (*i)->visit(*this); @@ -331,7 +330,6 @@ bool InterfaceGenerator::generate_interface(VariableDeclaration &var, const stri iface_var->linked_declaration = &var; stage->content.body.insert(iface_insert_point, iface_var); { - SetForScope set_level(scope_level, 1); SetForScope set_block(current_block, &stage->content); iface_var->visit(*this); } @@ -376,7 +374,7 @@ void InterfaceGenerator::visit(VariableDeclaration &var) { if(var.interface=="out") { - if(scope_level==1) + if(current_block==&stage->content) stage->out_variables[var.name] = &var; else if(generate_interface(var, "out", change_prefix(var.name, string()))) { @@ -474,7 +472,6 @@ void InterfaceGenerator::visit(Passthrough &pass) DeclarationReorderer::DeclarationReorderer(): - scope_level(0), kind(NO_DECLARATION) { } @@ -489,8 +486,7 @@ void DeclarationReorderer::visit(FunctionCall &call) void DeclarationReorderer::visit(Block &block) { - SetForScope set(scope_level, scope_level+1); - if(scope_level>1) + if(block.parent) return TraversingVisitor::visit(block); NodeList::iterator struct_insert_point = block.body.end();