X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fglsl%2Fgenerate.cpp;h=c9803acc3b2afd5698b29f66c8d69a110ddfc18c;hb=de87bb70ae10de39a39b2415407a234ab28099cf;hp=13b62aa32296a3d6c93c851827387205c08bfed1;hpb=f6dfd3e78fc8c4c4a14619f3fdece53600ab0db7;p=libs%2Fgl.git diff --git a/source/glsl/generate.cpp b/source/glsl/generate.cpp index 13b62aa3..c9803acc 100644 --- a/source/glsl/generate.cpp +++ b/source/glsl/generate.cpp @@ -120,28 +120,117 @@ void BlockHierarchyResolver::enter(Block &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(Block &block) +{ + for(NodeList::iterator i=block.body.begin(); i!=block.body.end(); ++i) + { + if(!block.parent) + type_insert_point = i; + (*i)->visit(*this); + } +} + +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; + else if(type.kind==BasicTypeDeclaration::ARRAY && type.base_type) + array_types[type.base_type] = &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) +{ + TypeDeclaration *type = resolve_type(var.type); + if(var.array && type) + { + map::iterator i = array_types.find(type); + if(i==array_types.end()) + { + BasicTypeDeclaration *array = new BasicTypeDeclaration; + array->source = BUILTIN_SOURCE; + array->name = type->name+"[]"; + array->kind = BasicTypeDeclaration::ARRAY; + array->base = type->name; + array->base_type = type; + stage->content.body.insert(type_insert_point, array); + array->visit(*this); + type = array; + } + else + type = i->second; + } + var.type_declaration = type; +} + +void TypeResolver::visit(FunctionDeclaration &func) +{ + func.return_type_declaration = resolve_type(func.return_type); + TraversingVisitor::visit(func); +} + + VariableResolver::VariableResolver(): stage(0), - builtins(0), - members(0), + r_members(0), record_target(false), - assignment_target(0), - self_referencing(false) + r_self_referencing(false), + r_assignment_target(0) { } void VariableResolver::apply(Stage &s) { stage = &s; - Stage *builtin_stage = get_builtins(s.type); - builtins = (builtin_stage ? &builtin_stage->content : 0); + s.interface_blocks.clear(); s.content.visit(*this); } -Block *VariableResolver::next_block(Block &block) -{ - return block.parent ? block.parent : &block!=builtins ? builtins : 0; -} - void VariableResolver::enter(Block &block) { block.variables.clear(); @@ -150,32 +239,35 @@ void VariableResolver::enter(Block &block) void VariableResolver::visit(VariableReference &var) { var.declaration = 0; - members = 0; - for(Block *block=current_block; (!var.declaration && block); block=next_block(*block)) + 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) - { - if(var.declaration->type_declaration) - members = &var.declaration->type_declaration->members.variables; - } - else + if(!var.declaration) { const map &blocks = stage->interface_blocks; - map::const_iterator i = blocks.find(var.name); - if(i!=blocks.end() && i->second->instance_name==var.name) + map::const_iterator i = blocks.find("_"+var.name); + if(i!=blocks.end()) { - iface_ref = new InterfaceBlockReference; - iface_ref->name = var.name; - iface_ref->declaration = i->second; - members = &i->second->members.variables; + /* 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()) { @@ -186,30 +278,36 @@ void VariableResolver::visit(VariableReference &var) } } + if(var.declaration) + if(StructDeclaration *strct = dynamic_cast(var.declaration->type_declaration)) + r_members = &strct->members.variables; + if(record_target) { - if(assignment_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; - assignment_target = 0; + r_assignment_target = 0; } else - assignment_target = var.declaration; + r_assignment_target = var.declaration; } - else if(var.declaration && var.declaration==assignment_target) - self_referencing = true; + 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=next_block(*block)) + for(Block *block=current_block; block; block=block->parent) { - map::iterator i = stage->interface_blocks.find(iface.name); + map::iterator i = stage->interface_blocks.find("_"+iface.name); if(i!=stage->interface_blocks.end()) { iface.declaration = i->second; - members = &i->second->members.variables; + r_members = &i->second->members.variables; break; } } @@ -217,120 +315,146 @@ void VariableResolver::visit(InterfaceBlockReference &iface) void VariableResolver::visit(MemberAccess &memacc) { - members = 0; - iface_ref = 0; + r_members = 0; + r_iface_ref = 0; memacc.left->visit(*this); - if(iface_ref) - memacc.left = iface_ref; - iface_ref = 0; + if(r_iface_ref) + memacc.left = r_iface_ref; + r_iface_ref = 0; memacc.declaration = 0; - if(members) + if(r_members) { - map::iterator i = members->find(memacc.member); - if(i!=members->end()) + map::iterator i = r_members->find(memacc.member); + if(i!=r_members->end()) { memacc.declaration = i->second; - if(i->second->type_declaration) - members = &i->second->type_declaration->members.variables; + if(StructDeclaration *strct = dynamic_cast(i->second->type_declaration)) + r_members = &strct->members.variables; } else - members = 0; + 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=="[") + if(binary.oper->token[0]=='[') { { - SetForScope set(record_target, false); + /* The subscript expression is not a part of the primary assignment + target. */ + SetFlag set(record_target, false); binary.right->visit(*this); } - members = 0; - iface_ref = 0; + r_members = 0; + r_iface_ref = 0; binary.left->visit(*this); - if(iface_ref) - binary.left = iface_ref; - iface_ref = 0; + if(r_iface_ref) + binary.left = r_iface_ref; } else { TraversingVisitor::visit(binary); - members = 0; + r_members = 0; } + + r_iface_ref = 0; } void VariableResolver::visit(Assignment &assign) { { SetFlag set(record_target); - assignment_target = 0; + r_assignment_target = 0; assign.left->visit(*this); + assign.target_declaration = r_assignment_target; } - self_referencing = false; + r_self_referencing = false; assign.right->visit(*this); + assign.self_referencing = (r_self_referencing || assign.oper->token[0]!='='); - assign.self_referencing = (self_referencing || assign.oper!="="); - assign.target_declaration = assignment_target; + r_members = 0; + r_iface_ref = 0; } -void VariableResolver::visit(StructDeclaration &strct) +void VariableResolver::visit(FunctionCall &call) { - TraversingVisitor::visit(strct); - stage->types[strct.name] = &strct; + TraversingVisitor::visit(call); + r_members = 0; + r_iface_ref = 0; } void VariableResolver::visit(VariableDeclaration &var) { - 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; TraversingVisitor::visit(var); - current_block->variables[var.name] = &var; + current_block->variables.insert(make_pair(var.name, &var)); } 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. */ - stage->interface_blocks[iface.name] = &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[iface.instance_name] = &iface; + 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 = functions.find(call.name); - if(i!=functions.end()) - call.declaration = i->second.back(); + 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) { - vector &decls = functions[func.name]; + 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 if(!decls.empty() && decls.back()->definition) - func.definition = decls.back()->definition; + else + { + func.definition = 0; + if(!stage_decl) + stage_decl = &func; + else + func.definition = stage_decl->definition; + } decls.push_back(&func); TraversingVisitor::visit(func); @@ -385,17 +509,18 @@ string InterfaceGenerator::change_prefix(const string &name, const string &prefi return prefix+name.substr(offset); } -bool InterfaceGenerator::generate_interface(VariableDeclaration &var, const string &iface, const string &name) +VariableDeclaration *InterfaceGenerator::generate_interface(VariableDeclaration &var, const string &iface, const string &name) { if(stage->content.variables.count(name)) - return false; + return 0; VariableDeclaration* iface_var = new VariableDeclaration; iface_var->sampling = var.sampling; iface_var->interface = iface; iface_var->type = var.type; - iface_var->type_declaration = var.type_declaration; iface_var->name = name; + /* Geometry shader inputs are always arrays. But if we're bringing in an + entire block, the array is on the block and not individual variables. */ if(stage->type==Stage::GEOMETRY && !copy_block) iface_var->array = ((var.array && var.interface!="in") || iface=="in"); else @@ -410,15 +535,15 @@ bool InterfaceGenerator::generate_interface(VariableDeclaration &var, const stri } iface_target_block->body.insert(iface_insert_point, iface_var); - iface_target_block->variables[name] = iface_var; + iface_target_block->variables.insert(make_pair(name, iface_var)); - return true; + return iface_var; } -bool InterfaceGenerator::generate_interface(InterfaceBlock &out_block) +InterfaceBlock *InterfaceGenerator::generate_interface(InterfaceBlock &out_block) { - if(stage->interface_blocks.count(out_block.name)) - return false; + if(stage->interface_blocks.count("in"+out_block.name)) + return 0; InterfaceBlock *in_block = new InterfaceBlock; in_block->interface = "in"; @@ -439,15 +564,15 @@ bool InterfaceGenerator::generate_interface(InterfaceBlock &out_block) } iface_target_block->body.insert(iface_insert_point, in_block); - stage->interface_blocks[in_block->name] = in_block; + stage->interface_blocks.insert(make_pair("in"+in_block->name, in_block)); if(!in_block->instance_name.empty()) - stage->interface_blocks[in_block->instance_name] = in_block; + stage->interface_blocks.insert(make_pair("_"+in_block->instance_name, in_block)); SetFlag set_scope(function_scope, false); SetForScope set_block(current_block, &stage->content); in_block->visit(*this); - return true; + return in_block; } ExpressionStatement &InterfaceGenerator::insert_assignment(const string &left, Expression *right) @@ -456,7 +581,7 @@ ExpressionStatement &InterfaceGenerator::insert_assignment(const string &left, E VariableReference *ref = new VariableReference; ref->name = left; assign->left = ref; - assign->oper = "="; + assign->oper = &Operator::get_operator("=", Operator::BINARY); assign->right = right; ExpressionStatement *stmt = new ExpressionStatement; @@ -471,7 +596,7 @@ void InterfaceGenerator::visit(VariableReference &var) { if(var.declaration || !stage->previous) return; - /* Don't pull a variable from previous stage if we just generated an out + /* Don't pull a variable from previous stage if we just generated an output interface in this stage */ if(stage->content.variables.count(var.name)) return; @@ -488,10 +613,12 @@ void InterfaceGenerator::visit(VariableReference &var) } 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) + map::const_iterator j = prev_blocks.find("_"+var.name); + if(j!=prev_blocks.end() && j->second->interface=="out") { generate_interface(*j->second); + /* Let VariableResolver convert the variable reference into an interface + block reference. */ return; } @@ -519,20 +646,27 @@ void InterfaceGenerator::visit(VariableDeclaration &var) { 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") { - /* For out variables in function scope, generate a global interface and - replace the local declaration with an assignment. */ - if(function_scope && generate_interface(var, "out", var.name)) + /* For output variables in function scope, generate a global interface + and replace the local declaration with an assignment. */ + VariableDeclaration *out_var = 0; + if(function_scope && (out_var=generate_interface(var, "out", var.name))) { + out_var->source = var.source; + out_var->line = var.line; nodes_to_remove.insert(&var); if(var.init_expression) { @@ -545,8 +679,8 @@ void InterfaceGenerator::visit(VariableDeclaration &var) } else if(var.interface=="in") { - /* Try to link in variables in global scope with out variables from - previous stage */ + /* Try to link input variables in global scope with output variables from + previous stage. */ if(current_block==&stage->content && !var.linked_declaration && stage->previous) { const map &prev_vars = stage->previous->content.variables; @@ -566,11 +700,13 @@ void InterfaceGenerator::visit(InterfaceBlock &iface) { if(iface.interface=="in") { + /* Try to link input blocks with output blocks sharing the same block + name from previous stage. */ if(!iface.linked_block && stage->previous) { 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) + map::const_iterator i = prev_blocks.find("out"+iface.name); + if(i!=prev_blocks.end()) { iface.linked_block = i->second; i->second->linked_block = &iface; @@ -593,6 +729,7 @@ 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); @@ -602,25 +739,27 @@ void InterfaceGenerator::visit(Passthrough &pass) 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) - linked = ((*j)->linked_declaration==i->second); + if(i->second->interface!="out") + continue; - if(!linked && generate_interface(*i->second, "in", i->second->name)) + /* Pass through output variables from the previous stage, but only + those which are not already linked to an input here. */ + if(!i->second->linked_declaration && generate_interface(*i->second, "in", i->second->name)) pass_vars.push_back(i->second); } } if(stage->type==Stage::GEOMETRY) { + /* Special case for geometry shader: copy gl_Position from input to + output. */ InterfaceBlockReference *ref = new InterfaceBlockReference; ref->name = "gl_in"; BinaryExpression *subscript = new BinaryExpression; subscript->left = ref; - subscript->oper = "["; + subscript->oper = &Operator::get_operator("[", Operator::BINARY); subscript->right = pass.subscript; - subscript->after = "]"; MemberAccess *memacc = new MemberAccess; memacc->left = subscript; @@ -640,9 +779,8 @@ void InterfaceGenerator::visit(Passthrough &pass) { BinaryExpression *subscript = new BinaryExpression; subscript->left = ref; - subscript->oper = "["; + subscript->oper = &Operator::get_operator("[", Operator::BINARY); subscript->right = pass.subscript; - subscript->after = "]"; insert_assignment(out_name, subscript); } else @@ -652,111 +790,6 @@ void InterfaceGenerator::visit(Passthrough &pass) nodes_to_remove.insert(&pass); } - -DeclarationReorderer::DeclarationReorderer(): - kind(NO_DECLARATION) -{ } - -void DeclarationReorderer::visit(FunctionCall &call) -{ - FunctionDeclaration *def = call.declaration; - if(def) - def = def->definition; - if(def && !ordered_funcs.count(def)) - needed_funcs.insert(def); -} - -void DeclarationReorderer::visit(Block &block) -{ - if(block.parent) - return TraversingVisitor::visit(block); - - NodeList::iterator struct_insert_point = block.body.end(); - NodeList::iterator variable_insert_point = block.body.end(); - NodeList::iterator function_insert_point = block.body.end(); - unsigned unordered_func_count = 0; - bool ordered_any_funcs = false; - - for(NodeList::iterator i=block.body.begin(); i!=block.body.end(); ) - { - kind = NO_DECLARATION; - (*i)->visit(*this); - - bool moved = false; - if(kind==STRUCT && struct_insert_point!=block.body.end()) - { - block.body.insert(struct_insert_point, *i); - moved = true; - } - else if(kind>STRUCT && struct_insert_point==block.body.end()) - struct_insert_point = i; - - if(kind==VARIABLE && variable_insert_point!=block.body.end()) - { - block.body.insert(variable_insert_point, *i); - moved = true; - } - else if(kind>VARIABLE && variable_insert_point==block.body.end()) - variable_insert_point = i; - - if(kind==FUNCTION) - { - if(function_insert_point==block.body.end()) - function_insert_point = i; - - if(needed_funcs.empty()) - { - ordered_funcs.insert(i->get()); - if(i!=function_insert_point) - { - block.body.insert(function_insert_point, *i); - moved = true; - } - else - ++function_insert_point; - ordered_any_funcs = true; - } - else - ++unordered_func_count; - } - - if(moved) - { - if(function_insert_point==i) - ++function_insert_point; - block.body.erase(i++); - } - else - ++i; - - if(i==block.body.end() && unordered_func_count) - { - if(!ordered_any_funcs) - // A subset of the remaining functions forms a recursive loop - /* TODO pick a function and move it up, adding any necessary - declarations */ - break; - - i = function_insert_point; - unordered_func_count = 0; - } - } -} - -void DeclarationReorderer::visit(VariableDeclaration &var) -{ - TraversingVisitor::visit(var); - kind = VARIABLE; -} - -void DeclarationReorderer::visit(FunctionDeclaration &func) -{ - needed_funcs.clear(); - func.body.visit(*this); - needed_funcs.erase(&func); - kind = FUNCTION; -} - } // namespace SL } // namespace GL } // namespace Msp