X-Git-Url: http://git.tdb.fi/?p=libs%2Fgl.git;a=blobdiff_plain;f=source%2Fglsl%2Fresolve.cpp;h=406d7780e810181ffc8650e9c0ba8c20466d67aa;hp=bf32326f249defc16224efb11b10ed3a5a6c7244;hb=76cc18518fc8b0b4fa11fda153e7d9b3899ed112;hpb=fdf2d162b21f55ad60bfeec2ad98c58356a083c0 diff --git a/source/glsl/resolve.cpp b/source/glsl/resolve.cpp index bf32326f..406d7780 100644 --- a/source/glsl/resolve.cpp +++ b/source/glsl/resolve.cpp @@ -1,6 +1,7 @@ -#include +#include #include #include +#include "reflect.h" #include "resolve.h" using namespace std; @@ -16,12 +17,6 @@ void BlockHierarchyResolver::enter(Block &block) } -TypeResolver::TypeResolver(): - stage(0), - iface_block(0), - r_any_resolved(false) -{ } - bool TypeResolver::apply(Stage &s) { stage = &s; @@ -33,7 +28,7 @@ bool TypeResolver::apply(Stage &s) TypeDeclaration *TypeResolver::get_or_create_array_type(TypeDeclaration &type) { - map::iterator i = array_types.find(&type); + auto i = array_types.find(&type); if(i!=array_types.end()) return i->second; @@ -51,10 +46,10 @@ TypeDeclaration *TypeResolver::get_or_create_array_type(TypeDeclaration &type) void TypeResolver::resolve_type(TypeDeclaration *&type, const string &name, bool array) { TypeDeclaration *resolved = 0; - map::iterator i = stage->types.find(name); + auto i = stage->types.find(name); if(i!=stage->types.end()) { - map::iterator j = alias_map.find(i->second); + auto j = alias_map.find(i->second); resolved = (j!=alias_map.end() ? j->second : i->second); } @@ -67,7 +62,7 @@ void TypeResolver::resolve_type(TypeDeclaration *&type, const string &name, bool void TypeResolver::visit(Block &block) { - for(NodeList::iterator i=block.body.begin(); i!=block.body.end(); ++i) + for(auto i=block.body.begin(); i!=block.body.end(); ++i) { if(!block.parent) type_insert_point = i; @@ -150,21 +145,14 @@ void TypeResolver::visit(FunctionDeclaration &func) } -VariableResolver::VariableResolver(): - stage(0), - r_any_resolved(false), - record_target(false), - r_self_referencing(false) -{ } - bool VariableResolver::apply(Stage &s) { stage = &s; s.interface_blocks.clear(); r_any_resolved = false; s.content.visit(*this); - for(vector::const_iterator i=redeclared_builtins.begin(); i!=redeclared_builtins.end(); ++i) - (*i)->source = GENERATED_SOURCE; + for(Statement *b: redeclared_builtins) + b->source = GENERATED_SOURCE; NodeRemover().apply(s, nodes_to_remove); return r_any_resolved; } @@ -217,7 +205,7 @@ void VariableResolver::visit(VariableReference &var) one. */ for(Block *block=current_block; (!declaration && block); block=block->parent) { - map::iterator i = block->variables.find(var.name); + auto i = block->variables.find(var.name); if(i!=block->variables.end()) declaration = i->second; } @@ -225,7 +213,7 @@ void VariableResolver::visit(VariableReference &var) if(!declaration) { const map &blocks = stage->interface_blocks; - map::const_iterator i = blocks.find(var.name); + auto i = blocks.find(var.name); if(i==blocks.end()) { // Look for the variable in anonymous interface blocks. @@ -273,7 +261,7 @@ void VariableResolver::visit(VariableReference &var) void VariableResolver::visit(InterfaceBlockReference &iface) { - map::iterator i = stage->interface_blocks.find(iface.name); + auto i = stage->interface_blocks.find(iface.name); InterfaceBlock *declaration = (i!=stage->interface_blocks.end() ? i->second : 0); r_any_resolved |= (declaration!=iface.declaration); iface.declaration = declaration; @@ -289,11 +277,12 @@ void VariableResolver::visit(MemberAccess &memacc) int index = -1; if(StructDeclaration *strct = dynamic_cast(memacc.left->type)) { - map::iterator i = strct->members.variables.find(memacc.member); + auto i = strct->members.variables.find(memacc.member); if(i!=strct->members.variables.end()) { declaration = i->second; - for(NodeList::const_iterator j=strct->members.body.begin(); (j!=strct->members.body.end() && j->get()!=i->second); ++j) + index = 0; + for(auto j=strct->members.body.begin(); (j!=strct->members.body.end() && j->get()!=i->second); ++j) ++index; if(record_target) @@ -309,9 +298,9 @@ void VariableResolver::visit(MemberAccess &memacc) static const char component_names[] = { 'x', 'r', 's', 'y', 'g', 't', 'z', 'b', 'p', 'w', 'a', 'q' }; bool ok = true; - UInt8 components[4] = { }; + uint8_t components[4] = { }; for(unsigned i=0; (ok && i::const_iterator i=from_layout.qualifiers.begin(); i!=from_layout.qualifiers.end(); ++i) + for(const Layout::Qualifier &q: from_layout.qualifiers) { - bool found = false; - for(vector::iterator j=to_layout.qualifiers.begin(); (!found && j!=to_layout.qualifiers.end()); ++j) - if(j->name==i->name) - { - j->has_value = i->value; - j->value = i->value; - found = true; - } + auto i = find_member(to_layout.qualifiers, q.name, &Layout::Qualifier::name); + if(i!=to_layout.qualifiers.end()) + { + i->has_value = q.value; + i->value = q.value; + } + else + to_layout.qualifiers.push_back(q); + } +} - if(!found) - to_layout.qualifiers.push_back(*i); +void VariableResolver::redeclare_builtin(VariableDeclaration &existing, VariableDeclaration &var) +{ + if(var.layout) + { + if(existing.layout) + merge_layouts(*existing.layout, *var.layout); + else + existing.layout = var.layout; } + if(var.array_size) + existing.array_size = var.array_size; + + redeclared_builtins.push_back(&existing); } void VariableResolver::visit(VariableDeclaration &var) { TraversingVisitor::visit(var); - VariableDeclaration *&ptr = current_block->variables[var.name]; - if(!ptr) - ptr = &var; - else if(!current_block->parent && ptr->interface==var.interface && ptr->type==var.type) + + auto i = current_block->variables.find(var.name); + VariableDeclaration *existing = 0; + InterfaceBlock *block = 0; + if(i!=current_block->variables.end()) + existing = i->second; + else if(!current_block->parent) { - if(ptr->source==BUILTIN_SOURCE) - redeclared_builtins.push_back(&var); - else - stage->diagnostics.push_back(Diagnostic(Diagnostic::WARN, var.source, var.line, - format("Redeclaring non-builtin variable '%s' is deprecated", var.name))); + const map &blocks = stage->interface_blocks; + for(auto j=blocks.begin(); j!=blocks.end(); ++j) + if(j->second->instance_name.empty() && j->second->struct_declaration) + { + map &block_vars = j->second->struct_declaration->members.variables; + i = block_vars.find(var.name); + if(i!=block_vars.end()) + { + existing = i->second; + block = j->second; + break; + } + } + } - if(var.init_expression) - ptr->init_expression = var.init_expression; - if(var.layout) + if(!existing) + current_block->variables.insert(make_pair(var.name, &var)); + else if(!current_block->parent && (block ? block->interface : existing->interface)==var.interface && existing->type==var.type && existing->array==var.array) + { + if(existing->source==BUILTIN_SOURCE) { - if(ptr->layout) - merge_layouts(*ptr->layout, *var.layout); - else - ptr->layout = var.layout; - } - nodes_to_remove.insert(&var); + redeclare_builtin(*existing, var); - r_any_resolved = true; + if(block) + { + redeclared_builtins.push_back(block); + for(const auto &kvp: block->struct_declaration->members.variables) + redeclared_builtins.push_back(kvp.second); + } + + nodes_to_remove.insert(&var); + r_any_resolved = true; + } + else if(existing->array && !existing->array_size && !var.layout && !var.init_expression) + { + existing->array_size = var.array_size; + nodes_to_remove.insert(&var); + r_any_resolved = true; + } } } 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(format("%s %s", iface.interface, iface.block_name), &iface)); - if(!iface.instance_name.empty()) - stage->interface_blocks.insert(make_pair(iface.instance_name, &iface)); + string key = format("%s %s", iface.interface, iface.block_name); + auto i = stage->interface_blocks.find(key); + if(i!=stage->interface_blocks.end()) + { + if(i->second->source==BUILTIN_SOURCE && iface.struct_declaration && i->second->struct_declaration) + { + const map &vars = iface.struct_declaration->members.variables; + const map &existing_vars = i->second->struct_declaration->members.variables; + + bool found_all = true; + for(const auto &kvp: vars) + { + auto j = existing_vars.find(kvp.first); + if(j!=existing_vars.end() && j->second->type==kvp.second->type && j->second->array==kvp.second->array) + redeclare_builtin(*j->second, *kvp.second); + else + found_all = false; + } + + if(found_all) + { + redeclared_builtins.push_back(i->second); + nodes_to_remove.insert(&iface); + nodes_to_remove.insert(iface.struct_declaration); + } + } + } + else + { + /* Block names can be reused in different interfaces. Prepend the interface + to the name to avoid conflicts. */ + stage->interface_blocks.insert(make_pair(key, &iface)); + if(!iface.instance_name.empty()) + stage->interface_blocks.insert(make_pair(iface.instance_name, &iface)); + } TraversingVisitor::visit(iface); } -ExpressionResolver::ExpressionResolver(): - stage(0), - r_any_resolved(false) -{ } - bool ExpressionResolver::apply(Stage &s) { stage = &s; @@ -458,43 +508,6 @@ bool ExpressionResolver::apply(Stage &s) return r_any_resolved; } -bool ExpressionResolver::is_scalar(BasicTypeDeclaration &type) -{ - return (type.kind==BasicTypeDeclaration::INT || type.kind==BasicTypeDeclaration::FLOAT); -} - -bool ExpressionResolver::is_vector_or_matrix(BasicTypeDeclaration &type) -{ - return (type.kind==BasicTypeDeclaration::VECTOR || type.kind==BasicTypeDeclaration::MATRIX); -} - -BasicTypeDeclaration *ExpressionResolver::get_element_type(BasicTypeDeclaration &type) -{ - if(is_vector_or_matrix(type) || type.kind==BasicTypeDeclaration::ARRAY) - { - BasicTypeDeclaration *basic_base = dynamic_cast(type.base_type); - return (basic_base ? get_element_type(*basic_base) : 0); - } - else - return &type; -} - -bool ExpressionResolver::can_convert(BasicTypeDeclaration &from, BasicTypeDeclaration &to) -{ - if(from.kind==BasicTypeDeclaration::INT && to.kind==BasicTypeDeclaration::FLOAT) - return from.size<=to.size; - else if(from.kind!=to.kind) - return false; - else if((from.kind==BasicTypeDeclaration::VECTOR || from.kind==BasicTypeDeclaration::MATRIX) && from.size==to.size) - { - BasicTypeDeclaration *from_base = dynamic_cast(from.base_type); - BasicTypeDeclaration *to_base = dynamic_cast(to.base_type); - return (from_base && to_base && can_convert(*from_base, *to_base)); - } - else - return false; -} - ExpressionResolver::Compatibility ExpressionResolver::get_compatibility(BasicTypeDeclaration &left, BasicTypeDeclaration &right) { if(&left==&right) @@ -507,20 +520,18 @@ ExpressionResolver::Compatibility ExpressionResolver::get_compatibility(BasicTyp return NOT_COMPATIBLE; } -BasicTypeDeclaration *ExpressionResolver::find_type(BasicTypeDeclaration::Kind kind, unsigned size) +BasicTypeDeclaration *ExpressionResolver::find_type(BasicTypeDeclaration::Kind kind, unsigned size, bool sign) { - for(vector::const_iterator i=basic_types.begin(); i!=basic_types.end(); ++i) - if((*i)->kind==kind && (*i)->size==size) - return *i; - return 0; + auto i = find_if(basic_types, + [kind, size, sign](const BasicTypeDeclaration *t){ return t->kind==kind && t->size==size && t->sign==sign; }); + return (i!=basic_types.end() ? *i : 0); } BasicTypeDeclaration *ExpressionResolver::find_type(BasicTypeDeclaration &elem_type, BasicTypeDeclaration::Kind kind, unsigned size) { - for(vector::const_iterator i=basic_types.begin(); i!=basic_types.end(); ++i) - if(get_element_type(**i)==&elem_type && (*i)->kind==kind && (*i)->size==size) - return *i; - return 0; + auto i = find_if(basic_types, + [&elem_type, kind, size](BasicTypeDeclaration *t){ return get_element_type(*t)==&elem_type && t->kind==kind && t->size==size; }); + return (i!=basic_types.end() ? *i : 0); } void ExpressionResolver::convert_to(RefPtr &expr, BasicTypeDeclaration &type) @@ -584,7 +595,7 @@ void ExpressionResolver::resolve(Expression &expr, TypeDeclaration *type, bool l void ExpressionResolver::visit(Block &block) { SetForScope set_block(current_block, &block); - for(NodeList::iterator i=block.body.begin(); i!=block.body.end(); ++i) + for(auto i=block.body.begin(); i!=block.body.end(); ++i) { insert_point = i; (*i)->visit(*this); @@ -596,7 +607,9 @@ void ExpressionResolver::visit(Literal &literal) if(literal.value.check_type()) resolve(literal, find_type(BasicTypeDeclaration::BOOL, 1), false); else if(literal.value.check_type()) - resolve(literal, find_type(BasicTypeDeclaration::INT, 32), false); + resolve(literal, find_type(BasicTypeDeclaration::INT, 32, true), false); + else if(literal.value.check_type()) + resolve(literal, find_type(BasicTypeDeclaration::INT, 32, false), false); else if(literal.value.check_type()) resolve(literal, find_type(BasicTypeDeclaration::FLOAT, 32), false); } @@ -744,6 +757,17 @@ void ExpressionResolver::visit(BinaryExpression &binary, bool assign) if(!left_size || (left_size==1 && right_size!=1) || (left_size>1 && right_size!=1 && right_size!=left_size)) return; + /* If the left operand is a vector and right is scalar, convert the right + operand to a vector too. */ + if(left_size>1 && right_size==1) + { + BasicTypeDeclaration *vec_right = find_type(*elem_right, basic_left->kind, basic_left->size); + if(!vec_right) + return; + + convert_to(binary.right, *vec_right); + } + type = basic_left; // Don't perform conversion even if the operands are of different sizes. compat = SAME_TYPE; @@ -879,9 +903,11 @@ void ExpressionResolver::visit_constructor(FunctionCall &call) if(call.arguments.empty()) return; - map::const_iterator i = stage->types.find(call.name); + auto i = stage->types.find(call.name); if(i==stage->types.end()) return; + else if(call.arguments.size()==1 && i->second==call.arguments[0]->type) + ; else if(BasicTypeDeclaration *basic = dynamic_cast(i->second)) { BasicTypeDeclaration *elem = get_element_type(*basic); @@ -892,10 +918,10 @@ void ExpressionResolver::visit_constructor(FunctionCall &call) args.reserve(call.arguments.size()); unsigned arg_component_total = 0; bool has_matrices = false; - for(NodeArray::const_iterator j=call.arguments.begin(); j!=call.arguments.end(); ++j) + for(const RefPtr &a: call.arguments) { ArgumentInfo info; - if(!(info.type=dynamic_cast((*j)->type))) + if(!(info.type=dynamic_cast(a->type))) return; if(is_scalar(*info.type) || info.type->kind==BasicTypeDeclaration::BOOL) info.component_count = 1; @@ -946,6 +972,11 @@ void ExpressionResolver::visit_constructor(FunctionCall &call) { unsigned column_count = basic->size&0xFFFF; unsigned row_count = basic->size>>16; + + vector > columns; + columns.reserve(column_count); + bool changed_columns = false; + if(call.arguments.size()==1) { /* A matrix can be constructed from a single element or another @@ -968,8 +999,6 @@ void ExpressionResolver::visit_constructor(FunctionCall &call) current_block->body.insert(insert_point, temporary); // Create expressions to build each column. - vector > columns; - columns.reserve(column_count); for(unsigned j=0; j ref = new VariableReference; @@ -990,13 +1019,7 @@ void ExpressionResolver::visit_constructor(FunctionCall &call) truncate_vector(columns.back(), row_count); } - call.arguments.resize(column_count); - copy(columns.begin(), columns.end(), call.arguments.begin()); - - /* Let VariableResolver process the new nodes and finish - resolving the constructor on the next pass. */ - r_any_resolved = true; - return; + changed_columns = true; } else return; @@ -1005,9 +1028,6 @@ void ExpressionResolver::visit_constructor(FunctionCall &call) { /* Construct a matrix from individual components in column-major order. Arguments must align at column boundaries. */ - vector > columns; - columns.reserve(column_count); - vector > column_args; column_args.reserve(row_count); unsigned column_component_count = 0; @@ -1041,11 +1061,24 @@ void ExpressionResolver::visit_constructor(FunctionCall &call) else if(column_component_count>row_count) // Argument alignment mismatch. return; + + changed_columns = true; } } } else return; + + if(changed_columns) + { + call.arguments.resize(column_count); + copy(columns.begin(), columns.end(), call.arguments.begin()); + + /* Let VariableResolver process the new nodes and finish + resolving the constructor on the next pass. */ + r_any_resolved = true; + return; + } } else return; @@ -1053,12 +1086,12 @@ void ExpressionResolver::visit_constructor(FunctionCall &call) if(convert_args) { // The argument list may have changed so can't rely on args. - for(NodeArray::iterator j=call.arguments.begin(); j!=call.arguments.end(); ++j) - if(BasicTypeDeclaration *basic_arg = dynamic_cast((*j)->type)) + for(RefPtr &a: call.arguments) + if(BasicTypeDeclaration *basic_arg = dynamic_cast(a->type)) { BasicTypeDeclaration *elem_arg = get_element_type(*basic_arg); if(elem_arg!=elem) - convert_to_element(*j, *elem); + convert_to_element(a, *elem); } } } @@ -1067,16 +1100,17 @@ void ExpressionResolver::visit_constructor(FunctionCall &call) if(call.arguments.size()!=strct->members.body.size()) return; - unsigned k = 0; - for(NodeList::const_iterator j=strct->members.body.begin(); j!=strct->members.body.end(); ++j, ++k) + auto j = call.arguments.begin(); + for(const RefPtr &s: strct->members.body) { - if(VariableDeclaration *var = dynamic_cast(j->get())) + if(VariableDeclaration *var = dynamic_cast(s.get())) { - if(!call.arguments[k]->type || call.arguments[k]->type!=var->type_declaration) + if(!(*j)->type || (*j)->type!=var->type_declaration) return; } else return; + ++j; } } @@ -1114,6 +1148,28 @@ void ExpressionResolver::visit(VariableDeclaration &var) convert_to(var.init_expression, *var_basic); } +void ExpressionResolver::visit(FunctionDeclaration &func) +{ + SetForScope set_func(current_function, &func); + TraversingVisitor::visit(func); +} + +void ExpressionResolver::visit(Return &ret) +{ + TraversingVisitor::visit(ret); + if(!current_function || !ret.expression) + return; + + BasicTypeDeclaration *ret_basic = dynamic_cast(current_function->return_type_declaration); + BasicTypeDeclaration *expr_basic = dynamic_cast(ret.expression->type); + if(!ret_basic || !expr_basic) + return; + + Compatibility compat = get_compatibility(*ret_basic, *expr_basic); + if(compat==RIGHT_CONVERTIBLE) + convert_to(ret.expression, *ret_basic); +} + bool FunctionResolver::apply(Stage &s) { @@ -1124,6 +1180,29 @@ bool FunctionResolver::apply(Stage &s) return r_any_resolved; } +bool FunctionResolver::can_convert_arguments(const FunctionCall &call, const FunctionDeclaration &decl) +{ + if(decl.parameters.size()!=call.arguments.size()) + return false; + + for(unsigned j=0; jtype; + const TypeDeclaration *param_type = decl.parameters[j]->type_declaration; + if(arg_type==param_type) + continue; + + const BasicTypeDeclaration *arg_basic = dynamic_cast(arg_type); + const BasicTypeDeclaration *param_basic = dynamic_cast(param_type); + if(arg_basic && param_basic && can_convert(*arg_basic, *param_basic)) + continue; + + return false; + } + + return true; +} + void FunctionResolver::visit(FunctionCall &call) { FunctionDeclaration *declaration = 0; @@ -1133,7 +1212,7 @@ void FunctionResolver::visit(FunctionCall &call) { string arg_types; bool has_signature = true; - for(NodeArray::const_iterator i=call.arguments.begin(); (has_signature && i!=call.arguments.end()); ++i) + for(auto i=call.arguments.begin(); (has_signature && i!=call.arguments.end()); ++i) { if((*i)->type) append(arg_types, ",", (*i)->type->name); @@ -1143,8 +1222,23 @@ void FunctionResolver::visit(FunctionCall &call) if(has_signature) { - map::iterator i = stage->functions.find(format("%s(%s)", call.name, arg_types)); + auto i = stage->functions.find(format("%s(%s)", call.name, arg_types)); declaration = (i!=stage->functions.end() ? i->second : 0); + + if(!declaration) + { + for(i=stage->functions.lower_bound(call.name+"("); (i!=stage->functions.end() && i->second->name==call.name); ++i) + if(can_convert_arguments(call, *i->second)) + { + if(declaration) + { + declaration = 0; + break; + } + else + declaration = i->second; + } + } } } @@ -1159,10 +1253,10 @@ void FunctionResolver::visit(FunctionDeclaration &func) if(func.signature.empty()) { string param_types; - for(NodeArray::const_iterator i=func.parameters.begin(); i!=func.parameters.end(); ++i) + for(const RefPtr &p: func.parameters) { - if((*i)->type_declaration) - append(param_types, ",", (*i)->type_declaration->name); + if(p->type_declaration) + append(param_types, ",", p->type_declaration->name); else return; } @@ -1187,11 +1281,11 @@ void FunctionResolver::visit(FunctionDeclaration &func) stage_decl = &func; // Set all previous declarations to use this definition. - for(vector::iterator i=decls.begin(); i!=decls.end(); ++i) + for(FunctionDeclaration *f: decls) { - r_any_resolved |= (func.definition!=(*i)->definition); - (*i)->definition = func.definition; - (*i)->body.body.clear(); + r_any_resolved |= (func.definition!=f->definition); + f->definition = func.definition; + f->body.body.clear(); } } else