X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fglsl%2Fgenerate.cpp;h=38eda220cee20533db6936e1b79d0831c01aefff;hb=8173340a7737e32cb25b21b67049102bd1526beb;hp=434dbe7a8486e0d73104c9a2c0e0ad451c67d39a;hpb=bd8816692056230c36504dcccd76c6946dff47b1;p=libs%2Fgl.git diff --git a/source/glsl/generate.cpp b/source/glsl/generate.cpp index 434dbe7a..38eda220 100644 --- a/source/glsl/generate.cpp +++ b/source/glsl/generate.cpp @@ -1,4 +1,6 @@ +#include #include +#include #include "builtin.h" #include "generate.h" @@ -22,20 +24,6 @@ void DeclarationCombiner::visit(Block &block) TraversingVisitor::visit(block); } -void DeclarationCombiner::visit(FunctionDeclaration &func) -{ - vector &decls = functions[func.name]; - if(func.definition) - { - for(vector::iterator i=decls.begin(); i!=decls.end(); ++i) - { - (*i)->definition = func.definition; - (*i)->body.body.clear(); - } - } - decls.push_back(&func); -} - void DeclarationCombiner::visit(VariableDeclaration &var) { VariableDeclaration *&ptr = variables[var.name]; @@ -73,40 +61,73 @@ void DeclarationCombiner::visit(VariableDeclaration &var) } -void BlockResolver::enter(Block &block) +ConstantSpecializer::ConstantSpecializer(): + values(0) +{ } + +void ConstantSpecializer::apply(Stage &stage, const map *v) { - block.parent = current_block; + values = v; + stage.content.visit(*this); } -void BlockResolver::visit(InterfaceBlock &iface) +void ConstantSpecializer::visit(VariableDeclaration &var) { - /* 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; - if(!iface.instance_name.empty()) - current_block->interfaces[iface.instance_name] = &iface; - TraversingVisitor::visit(iface); + bool specializable = false; + if(var.layout) + { + vector &qualifiers = var.layout->qualifiers; + for(vector::iterator i=qualifiers.begin(); i!=qualifiers.end(); ++i) + if(i->name=="constant_id") + { + specializable = true; + if(values) + qualifiers.erase(i); + else if(i->value==-1) + i->value = hash32(var.name)&0x7FFFFFFF; + break; + } + + if(qualifiers.empty()) + var.layout = 0; + } + + if(specializable && values) + { + map::const_iterator i = values->find(var.name); + if(i!=values->end()) + { + RefPtr literal = new Literal; + if(var.type=="bool") + literal->token = (i->second ? "true" : "false"); + else if(var.type=="int") + literal->token = lexical_cast(i->second); + var.init_expression = literal; + } + } +} + + +void BlockHierarchyResolver::enter(Block &block) +{ + block.parent = current_block; } VariableResolver::VariableResolver(): - builtins(0), - members(0), + stage(0), + r_members(0), record_target(false), - assignment_target(0), - self_referencing(false) + r_assignment_target(0), + r_self_referencing(false) { } -void VariableResolver::apply(Stage &stage) +void VariableResolver::apply(Stage &s) { - 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; + stage = &s; + s.types.clear(); + s.interface_blocks.clear(); + s.content.visit(*this); } void VariableResolver::enter(Block &block) @@ -117,66 +138,66 @@ void VariableResolver::enter(Block &block) void VariableResolver::visit(VariableReference &var) { var.declaration = 0; - members = 0; - for(Block *block=current_block; block; block=next_block(*block)) + r_members = 0; + 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; - 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) + { + if(var.declaration->type_declaration) + r_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) { - if(var.declaration->type_declaration) - members = &var.declaration->type_declaration->members.variables; - break; + r_iface_ref = new InterfaceBlockReference; + r_iface_ref->name = var.name; + r_iface_ref->declaration = i->second; + r_members = &i->second->members.variables; } - - map::iterator j = block->interfaces.find(var.name); - if(j!=block->interfaces.end() && j->second->instance_name==var.name) + else { - iface_ref = new InterfaceBlockReference; - iface_ref->name = var.name; - iface_ref->declaration = j->second; - members = &j->second->members.variables; - break; + 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(record_target) { - if(assignment_target) + if(r_assignment_target) { 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 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; + r_members = &i->second->members.variables; break; } } @@ -184,48 +205,48 @@ 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; + r_members = &i->second->type_declaration->members.variables; } else - members = 0; + r_members = 0; } } void VariableResolver::visit(BinaryExpression &binary) { - if(binary.oper=="[") + if(binary.oper->token[0]=='[') { { SetForScope 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; + r_iface_ref = 0; } else { TraversingVisitor::visit(binary); - members = 0; + r_members = 0; } } @@ -233,31 +254,28 @@ void VariableResolver::visit(Assignment &assign) { { SetFlag set(record_target); - assignment_target = 0; + r_assignment_target = 0; assign.left->visit(*this); } - self_referencing = false; + r_self_referencing = false; assign.right->visit(*this); - assign.self_referencing = (self_referencing || assign.oper!="="); - assign.target_declaration = assignment_target; + assign.self_referencing = (r_self_referencing || assign.oper->token[0]!='='); + assign.target_declaration = r_assignment_target; } 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; @@ -268,34 +286,56 @@ void VariableResolver::visit(VariableDeclaration &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; + if(!iface.instance_name.empty()) + stage->interface_blocks[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]; - if(func.definition) + FunctionDeclaration *&stage_decl = stage->functions[func.name]; + vector &decls = declarations[func.name]; + if(func.definition==&func) { + stage_decl = &func; + for(vector::iterator i=decls.begin(); i!=decls.end(); ++i) + { (*i)->definition = func.definition; - decls.clear(); - decls.push_back(&func); + (*i)->body.body.clear(); + } } - else if(!decls.empty() && decls.back()->definition) - func.definition = decls.back()->definition; else - decls.push_back(&func); + { + func.definition = 0; + if(!stage_decl) + stage_decl = &func; + else + func.definition = stage_decl->definition; + } + decls.push_back(&func); TraversingVisitor::visit(func); } @@ -304,7 +344,9 @@ void FunctionResolver::visit(FunctionDeclaration &func) InterfaceGenerator::InterfaceGenerator(): stage(0), function_scope(false), - iface_block(0) + iface_block(0), + copy_block(false), + iface_target_block(0) { } string InterfaceGenerator::get_out_prefix(Stage::Type type) @@ -320,6 +362,7 @@ string InterfaceGenerator::get_out_prefix(Stage::Type type) void InterfaceGenerator::apply(Stage &s) { stage = &s; + iface_target_block = &stage->content; if(stage->previous) in_prefix = get_out_prefix(stage->previous->type); out_prefix = get_out_prefix(stage->type); @@ -357,7 +400,7 @@ bool InterfaceGenerator::generate_interface(VariableDeclaration &var, const stri iface_var->type = var.type; iface_var->type_declaration = var.type_declaration; iface_var->name = name; - if(stage->type==Stage::GEOMETRY) + if(stage->type==Stage::GEOMETRY && !copy_block) iface_var->array = ((var.array && var.interface!="in") || iface=="in"); else iface_var->array = var.array; @@ -365,11 +408,48 @@ bool InterfaceGenerator::generate_interface(VariableDeclaration &var, const stri iface_var->array_size = var.array_size; if(iface=="in") { + iface_var->layout = var.layout; 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; + + iface_target_block->body.insert(iface_insert_point, iface_var); + iface_target_block->variables[name] = iface_var; + + return true; +} + +bool InterfaceGenerator::generate_interface(InterfaceBlock &out_block) +{ + if(stage->interface_blocks.count(out_block.name)) + return false; + + InterfaceBlock *in_block = new InterfaceBlock; + in_block->interface = "in"; + in_block->name = out_block.name; + in_block->instance_name = out_block.instance_name; + if(stage->type==Stage::GEOMETRY) + in_block->array = true; + else + in_block->array = out_block.array; + in_block->linked_block = &out_block; + out_block.linked_block = in_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); + } + + iface_target_block->body.insert(iface_insert_point, in_block); + stage->interface_blocks[in_block->name] = in_block; + if(!in_block->instance_name.empty()) + stage->interface_blocks[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; } @@ -380,7 +460,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; @@ -408,11 +488,37 @@ void InterfaceGenerator::visit(VariableReference &var) { generate_interface(*i->second, "in", i->second->name); var.name = i->second->name; + return; } + + 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) + { + generate_interface(*j->second); + return; + } + + for(j=prev_blocks.begin(); j!=prev_blocks.end(); ++j) + if(j->second->instance_name.empty()) + { + i = j->second->members.variables.find(var.name); + if(i!=j->second->members.variables.end()) + { + generate_interface(*j->second); + return; + } + } } void InterfaceGenerator::visit(VariableDeclaration &var) { + if(copy_block) + { + generate_interface(var, "in", var.name); + return; + } + if(iface_block) { if(iface_block->linked_block) @@ -466,7 +572,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) { @@ -516,9 +622,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 = "]"; MemberAccess *memacc = new MemberAccess; memacc->left = subscript; @@ -538,9 +643,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 @@ -550,111 +654,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