X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fglsl%2Fgenerate.cpp;h=13f67d8cade070a4349bb2335080b27d1a2c790c;hb=518f751d385b733adbf43fe4056403740709edec;hp=42226a912396171faf0287b4062cbee19fbe5a40;hpb=b006be7a2eccaeff9647b5b403d7d0a4ff13da3b;p=libs%2Fgl.git diff --git a/source/glsl/generate.cpp b/source/glsl/generate.cpp index 42226a91..13f67d8c 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,7 +61,54 @@ void DeclarationCombiner::visit(VariableDeclaration &var) } -void BlockResolver::enter(Block &block) +ConstantSpecializer::ConstantSpecializer(): + values(0) +{ } + +void ConstantSpecializer::apply(Stage &stage, const map *v) +{ + values = v; + stage.content.visit(*this); +} + +void ConstantSpecializer::visit(VariableDeclaration &var) +{ + 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; } @@ -81,7 +116,6 @@ void BlockResolver::enter(Block &block) VariableResolver::VariableResolver(): stage(0), - builtins(0), members(0), record_target(false), assignment_target(0), @@ -91,16 +125,11 @@ VariableResolver::VariableResolver(): void VariableResolver::apply(Stage &s) { stage = &s; - Stage *builtin_stage = get_builtins(s.type); - builtins = (builtin_stage ? &builtin_stage->content : 0); + s.types.clear(); + 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(); @@ -110,7 +139,7 @@ void VariableResolver::visit(VariableReference &var) { var.declaration = 0; members = 0; - for(Block *block=current_block; (!var.declaration && block); block=next_block(*block)) + for(Block *block=current_block; (!var.declaration && block); block=block->parent) { map::iterator i = block->variables.find(var.name); if(i!=block->variables.end()) @@ -162,7 +191,7 @@ void VariableResolver::visit(VariableReference &var) 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); if(i!=stage->interface_blocks.end()) @@ -268,29 +297,44 @@ void VariableResolver::visit(InterfaceBlock &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); + { + if(!stage_decl) + stage_decl = &func; + + func.definition = stage_decl->definition; + } + decls.push_back(&func); TraversingVisitor::visit(func); } @@ -361,9 +405,9 @@ bool InterfaceGenerator::generate_interface(VariableDeclaration &var, const stri iface_var->array = var.array; if(iface_var->array) iface_var->array_size = var.array_size; - iface_var->layout = var.layout; if(iface=="in") { + iface_var->layout = var.layout; iface_var->linked_declaration = &var; var.linked_declaration = iface_var; } @@ -611,111 +655,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