X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fglsl%2Fgenerate.cpp;h=13fb72c53039bfbd7e9fed3779bc718bdc6801d7;hb=bbe2fb7bc1384d7683f1795b5cfa9168df18c580;hp=07181d3583e69fbc1067292bbaddc7855c58a5ac;hpb=fd44325059d59d32d47ef3feb6d41d846f7f36f0;p=libs%2Fgl.git diff --git a/source/glsl/generate.cpp b/source/glsl/generate.cpp index 07181d35..13fb72c5 100644 --- a/source/glsl/generate.cpp +++ b/source/glsl/generate.cpp @@ -1,7 +1,5 @@ #include #include -#include -#include "builtin.h" #include "generate.h" using namespace std; @@ -10,57 +8,6 @@ namespace Msp { namespace GL { namespace SL { -void DeclarationCombiner::apply(Stage &stage) -{ - stage.content.visit(*this); - NodeRemover().apply(stage, nodes_to_remove); -} - -void DeclarationCombiner::visit(Block &block) -{ - if(current_block) - return; - - TraversingVisitor::visit(block); -} - -void DeclarationCombiner::visit(VariableDeclaration &var) -{ - VariableDeclaration *&ptr = variables[var.name]; - if(ptr) - { - ptr->type = var.type; - if(var.init_expression) - ptr->init_expression = var.init_expression; - if(var.layout) - { - if(ptr->layout) - { - for(vector::iterator i=var.layout->qualifiers.begin(); i!=var.layout->qualifiers.end(); ++i) - { - bool found = false; - for(vector::iterator j=ptr->layout->qualifiers.begin(); (!found && j!=ptr->layout->qualifiers.end()); ++j) - if(j->name==i->name) - { - j->has_value = i->value; - j->value = i->value; - found = true; - } - - if(!found) - ptr->layout->qualifiers.push_back(*i); - } - } - else - ptr->layout = var.layout; - } - nodes_to_remove.insert(&var); - } - else - ptr = &var; -} - - ConstantSpecializer::ConstantSpecializer(): values(0) { } @@ -114,326 +61,9 @@ void ConstantSpecializer::visit(VariableDeclaration &var) } -void BlockHierarchyResolver::enter(Block &block) -{ - block.parent = current_block; -} - - -TypeResolver::TypeResolver(): - stage(0) -{ } - -void TypeResolver::apply(Stage &s) -{ - stage = &s; - s.types.clear(); - s.content.visit(*this); -} - -TypeDeclaration *TypeResolver::resolve_type(const string &name) -{ - map::iterator i = stage->types.find(name); - if(i!=stage->types.end()) - { - map::iterator j = alias_map.find(i->second); - return (j!=alias_map.end() ? j->second : i->second); - } - else - return 0; -} - -void TypeResolver::visit(BasicTypeDeclaration &type) -{ - type.base_type = resolve_type(type.base); - - if(type.kind==BasicTypeDeclaration::VECTOR && type.base_type) - if(BasicTypeDeclaration *basic_base = dynamic_cast(type.base_type)) - if(basic_base->kind==BasicTypeDeclaration::VECTOR) - { - type.kind = BasicTypeDeclaration::MATRIX; - type.size |= basic_base->size<<16; - } - - if(type.kind==BasicTypeDeclaration::ALIAS && type.base_type) - alias_map[&type] = type.base_type; - - stage->types.insert(make_pair(type.name, &type)); -} - -void TypeResolver::visit(ImageTypeDeclaration &type) -{ - type.base_type = resolve_type(type.base); - stage->types.insert(make_pair(type.name, &type)); -} - -void TypeResolver::visit(StructDeclaration &strct) -{ - stage->types.insert(make_pair(strct.name, &strct)); - TraversingVisitor::visit(strct); -} - -void TypeResolver::visit(VariableDeclaration &var) -{ - var.type_declaration = resolve_type(var.type); -} - -void TypeResolver::visit(FunctionDeclaration &func) -{ - func.return_type_declaration = resolve_type(func.return_type); - TraversingVisitor::visit(func); -} - - -VariableResolver::VariableResolver(): - stage(0), - r_members(0), - record_target(false), - r_self_referencing(false), - r_assignment_target(0) -{ } - -void VariableResolver::apply(Stage &s) -{ - stage = &s; - s.interface_blocks.clear(); - s.content.visit(*this); -} - -void VariableResolver::enter(Block &block) -{ - block.variables.clear(); -} - -void VariableResolver::visit(VariableReference &var) -{ - var.declaration = 0; - r_members = 0; - /* Look for variable declarations in the block hierarchy first. Interface - blocks are always defined in the top level so we can't accidentally skip - one. */ - for(Block *block=current_block; (!var.declaration && block); block=block->parent) - { - map::iterator i = block->variables.find(var.name); - if(i!=block->variables.end()) - var.declaration = i->second; - } - - if(!var.declaration) - { - const map &blocks = stage->interface_blocks; - map::const_iterator i = blocks.find("_"+var.name); - if(i!=blocks.end()) - { - /* The name refers to an interface block with an instance name rather - than a variable. Prepare a new syntax tree node accordingly. */ - r_iface_ref = new InterfaceBlockReference; - r_iface_ref->source = var.source; - r_iface_ref->line = var.line; - r_iface_ref->name = var.name; - r_iface_ref->declaration = i->second; - r_members = &i->second->members.variables; - } - else - { - // Look for the variable in anonymous interface blocks. - 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; - } - } - } - - if(var.declaration) - if(StructDeclaration *strct = dynamic_cast(var.declaration->type_declaration)) - r_members = &strct->members.variables; - - if(record_target) - { - if(r_assignment_target) - { - /* More than one variable reference found in assignment target. - Unable to determine what the primary target is. */ - record_target = false; - r_assignment_target = 0; - } - else - r_assignment_target = var.declaration; - } - else if(var.declaration && var.declaration==r_assignment_target) - r_self_referencing = true; -} - -void VariableResolver::visit(InterfaceBlockReference &iface) -{ - iface.declaration = 0; - for(Block *block=current_block; block; block=block->parent) - { - map::iterator i = stage->interface_blocks.find("_"+iface.name); - if(i!=stage->interface_blocks.end()) - { - iface.declaration = i->second; - r_members = &i->second->members.variables; - break; - } - } -} - -void VariableResolver::visit(MemberAccess &memacc) -{ - r_members = 0; - r_iface_ref = 0; - memacc.left->visit(*this); - - if(r_iface_ref) - memacc.left = r_iface_ref; - r_iface_ref = 0; - - memacc.declaration = 0; - if(r_members) - { - map::iterator i = r_members->find(memacc.member); - if(i!=r_members->end()) - { - memacc.declaration = i->second; - if(StructDeclaration *strct = dynamic_cast(i->second->type_declaration)) - r_members = &strct->members.variables; - } - else - r_members = 0; - } -} - -void VariableResolver::visit(UnaryExpression &unary) -{ - TraversingVisitor::visit(unary); - r_members = 0; - r_iface_ref = 0; -} - -void VariableResolver::visit(BinaryExpression &binary) -{ - if(binary.oper->token[0]=='[') - { - { - /* The subscript expression is not a part of the primary assignment - target. */ - SetFlag set(record_target, false); - binary.right->visit(*this); - } - r_members = 0; - r_iface_ref = 0; - binary.left->visit(*this); - if(r_iface_ref) - binary.left = r_iface_ref; - } - else - { - TraversingVisitor::visit(binary); - r_members = 0; - } - - r_iface_ref = 0; -} - -void VariableResolver::visit(Assignment &assign) -{ - { - SetFlag set(record_target); - r_assignment_target = 0; - assign.left->visit(*this); - assign.target_declaration = r_assignment_target; - } - - r_self_referencing = false; - assign.right->visit(*this); - assign.self_referencing = (r_self_referencing || assign.oper->token[0]!='='); - - r_members = 0; - r_iface_ref = 0; -} - -void VariableResolver::visit(FunctionCall &call) -{ - TraversingVisitor::visit(call); - r_members = 0; - r_iface_ref = 0; -} - -void VariableResolver::visit(VariableDeclaration &var) -{ - if(!block_interface.empty() && var.interface.empty()) - var.interface = block_interface; - - TraversingVisitor::visit(var); - current_block->variables.insert(make_pair(var.name, &var)); -} - -void VariableResolver::visit(InterfaceBlock &iface) -{ - /* Block names can be reused in different interfaces. Prefix the name with - the first character of the interface to avoid conflicts. */ - stage->interface_blocks.insert(make_pair(iface.interface+iface.name, &iface)); - if(!iface.instance_name.empty()) - stage->interface_blocks.insert(make_pair("_"+iface.instance_name, &iface)); - - SetForScope set_iface(block_interface, iface.interface); - TraversingVisitor::visit(iface); -} - - -void FunctionResolver::apply(Stage &s) -{ - stage = &s; - s.functions.clear(); - s.content.visit(*this); -} - -void FunctionResolver::visit(FunctionCall &call) -{ - map::iterator i = stage->functions.find(call.name); - if(i!=stage->functions.end()) - call.declaration = i->second; - - TraversingVisitor::visit(call); -} - -void FunctionResolver::visit(FunctionDeclaration &func) -{ - FunctionDeclaration *&stage_decl = stage->functions[func.name]; - vector &decls = declarations[func.name]; - if(func.definition==&func) - { - stage_decl = &func; - - // Set all previous declarations to use this definition. - for(vector::iterator i=decls.begin(); i!=decls.end(); ++i) - { - (*i)->definition = func.definition; - (*i)->body.body.clear(); - } - } - else - { - func.definition = 0; - if(!stage_decl) - stage_decl = &func; - else - func.definition = stage_decl->definition; - } - decls.push_back(&func); - - TraversingVisitor::visit(func); -} - - InterfaceGenerator::InterfaceGenerator(): stage(0), function_scope(false), - iface_block(0), copy_block(false), iface_target_block(0) { } @@ -483,6 +113,9 @@ VariableDeclaration *InterfaceGenerator::generate_interface(VariableDeclaration if(stage->content.variables.count(name)) return 0; + if(stage->type==Stage::GEOMETRY && !copy_block && var.interface=="out" && var.array) + return 0; + VariableDeclaration* iface_var = new VariableDeclaration; iface_var->sampling = var.sampling; iface_var->interface = iface; @@ -505,18 +138,21 @@ VariableDeclaration *InterfaceGenerator::generate_interface(VariableDeclaration iface_target_block->body.insert(iface_insert_point, iface_var); iface_target_block->variables.insert(make_pair(name, iface_var)); + if(iface_target_block==&stage->content && iface=="in") + declared_inputs.push_back(iface_var); return iface_var; } InterfaceBlock *InterfaceGenerator::generate_interface(InterfaceBlock &out_block) { - if(stage->interface_blocks.count("in"+out_block.name)) + if(stage->interface_blocks.count("in"+out_block.block_name)) return 0; InterfaceBlock *in_block = new InterfaceBlock; in_block->interface = "in"; - in_block->name = out_block.name; + in_block->block_name = out_block.block_name; + in_block->members = new Block; in_block->instance_name = out_block.instance_name; if(stage->type==Stage::GEOMETRY) in_block->array = true; @@ -527,13 +163,16 @@ InterfaceBlock *InterfaceGenerator::generate_interface(InterfaceBlock &out_block { SetFlag set_copy(copy_block, true); - SetForScope set_target(iface_target_block, &in_block->members); - SetForScope::iterator> set_ins_pt(iface_insert_point, in_block->members.body.end()); - out_block.members.visit(*this); + SetForScope set_target(iface_target_block, in_block->members.get()); + SetForScope::iterator> set_ins_pt(iface_insert_point, in_block->members->body.end()); + if(out_block.struct_declaration) + out_block.struct_declaration->members.visit(*this); + else if(out_block.members) + out_block.members->visit(*this); } iface_target_block->body.insert(iface_insert_point, in_block); - stage->interface_blocks.insert(make_pair("in"+in_block->name, in_block)); + stage->interface_blocks.insert(make_pair("in"+in_block->block_name, in_block)); if(!in_block->instance_name.empty()) stage->interface_blocks.insert(make_pair("_"+in_block->instance_name, in_block)); @@ -576,8 +215,14 @@ void InterfaceGenerator::visit(VariableReference &var) 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; + if(stage->type==Stage::GEOMETRY && i->second->array) + stage->diagnostics.push_back(Diagnostic(Diagnostic::WARN, var.source, var.line, + format("Can't access '%s' through automatic interface because it's an array", var.name))); + else + { + generate_interface(*i->second, "in", i->second->name); + var.name = i->second->name; + } return; } @@ -592,10 +237,11 @@ void InterfaceGenerator::visit(VariableReference &var) } for(j=prev_blocks.begin(); j!=prev_blocks.end(); ++j) - if(j->second->instance_name.empty()) + if(j->second->instance_name.empty() && j->second->struct_declaration) { - i = j->second->members.variables.find(var.name); - if(i!=j->second->members.variables.end()) + const map &iface_vars = j->second->struct_declaration->members.variables; + i = iface_vars.find(var.name); + if(i!=iface_vars.end()) { generate_interface(*j->second); return; @@ -606,28 +252,8 @@ void InterfaceGenerator::visit(VariableReference &var) void InterfaceGenerator::visit(VariableDeclaration &var) { if(copy_block) - { generate_interface(var, "in", var.name); - return; - } - - if(iface_block) - { - if(iface_block->linked_block) - { - // Link all variables to their counterparts in the linked block. - const map &linked_vars = iface_block->linked_block->members.variables; - map::const_iterator i = linked_vars.find(var.name); - if(i!=linked_vars.end()) - { - var.linked_declaration = i->second; - var.linked_declaration->linked_declaration = &var; - } - } - return; - } - - if(var.interface=="out") + else if(var.interface=="out") { /* For output variables in function scope, generate a global interface and replace the local declaration with an assignment. */ @@ -646,11 +272,14 @@ void InterfaceGenerator::visit(VariableDeclaration &var) } } } - else if(var.interface=="in") + else if(var.interface=="in" && current_block==&stage->content) { + if(var.name.compare(0, 3, "gl_")) + declared_inputs.push_back(&var); + /* Try to link input variables in global scope with output variables from previous stage. */ - if(current_block==&stage->content && !var.linked_declaration && stage->previous) + if(!var.linked_declaration && stage->previous) { const map &prev_vars = stage->previous->content.variables; map::const_iterator i = prev_vars.find(var.name); @@ -674,7 +303,7 @@ void InterfaceGenerator::visit(InterfaceBlock &iface) if(!iface.linked_block && stage->previous) { const map &prev_blocks = stage->previous->interface_blocks; - map::const_iterator i = prev_blocks.find("out"+iface.name); + map::const_iterator i = prev_blocks.find("out"+iface.block_name); if(i!=prev_blocks.end()) { iface.linked_block = i->second; @@ -683,7 +312,6 @@ void InterfaceGenerator::visit(InterfaceBlock &iface) } } - SetForScope set_iface(iface_block, &iface); TraversingVisitor::visit(iface); } @@ -696,12 +324,8 @@ void InterfaceGenerator::visit(FunctionDeclaration &func) void InterfaceGenerator::visit(Passthrough &pass) { - vector pass_vars; - - // Pass through all input variables of this stage. - 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); + // Pass through all input variables declared so far. + vector pass_vars = declared_inputs; if(stage->previous) {