X-Git-Url: http://git.tdb.fi/?p=libs%2Fgl.git;a=blobdiff_plain;f=source%2Fglsl%2Fgenerate.cpp;h=ec51c19cdd9cc90472a0b2471eff915823fae0d7;hp=ab487a44c470bca769af032b9ce2966c7a0df5d2;hb=38712d8ecc57d043a2419ffbaeeb57f7a6586f14;hpb=9229a3e70dace406d897148857ed6567986c3bcd diff --git a/source/glsl/generate.cpp b/source/glsl/generate.cpp index ab487a44..ec51c19c 100644 --- a/source/glsl/generate.cpp +++ b/source/glsl/generate.cpp @@ -1,9 +1,6 @@ -#include +#include #include #include -#include -#include -#include "builtin.h" #include "generate.h" using namespace std; @@ -12,989 +9,41 @@ namespace Msp { namespace GL { namespace SL { -void DeclarationCombiner::apply(Stage &stage) +void ConstantIdAssigner::apply(Module &module, const Features &features) { - 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) -{ } - -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"); - literal->value = static_cast(i->second); - } - else if(var.type=="int") - { - literal->token = lexical_cast(i->second); - literal->value = i->second; - } - var.init_expression = literal; - } - } -} - - -void BlockHierarchyResolver::enter(Block &block) -{ - r_any_resolved |= (current_block!=block.parent); - block.parent = current_block; -} - - -TypeResolver::TypeResolver(): - stage(0), - iface_block(0), - r_any_resolved(false) -{ } - -bool TypeResolver::apply(Stage &s) -{ - stage = &s; - s.types.clear(); - r_any_resolved = false; - s.content.visit(*this); - return r_any_resolved; -} - -TypeDeclaration *TypeResolver::get_or_create_array_type(TypeDeclaration &type) -{ - map::iterator i = array_types.find(&type); - if(i!=array_types.end()) - return i->second; - - 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_types[&type] = array; - return array; -} - -void TypeResolver::resolve_type(TypeDeclaration *&type, const string &name, bool array) -{ - TypeDeclaration *resolved = 0; - map::iterator i = stage->types.find(name); - if(i!=stage->types.end()) - { - map::iterator j = alias_map.find(i->second); - resolved = (j!=alias_map.end() ? j->second : i->second); - } - - if(resolved && array) - resolved = get_or_create_array_type(*resolved); - - r_any_resolved |= (resolved!=type); - type=resolved; -} - -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) -{ - resolve_type(type.base_type, type.base, false); - - 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; - /* A matrix's base type is its column vector type. This will put - the column vector's size, i.e. the matrix's row count, in the high - half of the size. */ - 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) -{ - resolve_type(type.base_type, type.base, false); - 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) -{ - resolve_type(var.type_declaration, var.type, var.array); - if(iface_block && var.interface==iface_block->interface) - var.interface.clear(); -} - -void TypeResolver::visit(InterfaceBlock &iface) -{ - if(iface.members) - { - SetForScope set_iface(iface_block, &iface); - iface.members->visit(*this); - - StructDeclaration *strct = new StructDeclaration; - strct->source = INTERNAL_SOURCE; - strct->name = format("_%s_%s", iface.interface, iface.name); - strct->members.body.splice(strct->members.body.begin(), iface.members->body); - stage->content.body.insert(type_insert_point, strct); - stage->types.insert(make_pair(strct->name, strct)); - - iface.members = 0; - strct->interface_block = &iface; - iface.struct_declaration = strct; - } - - TypeDeclaration *type = iface.struct_declaration; - if(type && iface.array) - type = get_or_create_array_type(*type); - r_any_resolved = (type!=iface.type_declaration); - iface.type_declaration = type; -} - -void TypeResolver::visit(FunctionDeclaration &func) -{ - resolve_type(func.return_type_declaration, func.return_type, false); - TraversingVisitor::visit(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); - return r_any_resolved; -} - -void VariableResolver::enter(Block &block) -{ - block.variables.clear(); -} - -void VariableResolver::visit(RefPtr &expr) -{ - r_replacement_expr = 0; - expr->visit(*this); - if(r_replacement_expr) - { - expr = r_replacement_expr; - /* Don't record assignment target when doing a replacement, because chain - information won't be correct. */ - r_assignment_target.declaration = 0; - r_any_resolved = true; - } - r_replacement_expr = 0; -} - -void VariableResolver::check_assignment_target(Statement *declaration) -{ - if(record_target) - { - if(r_assignment_target.declaration) - { - /* More than one reference found in assignment target. Unable to - determine what the primary target is. */ - record_target = false; - r_assignment_target.declaration = 0; - } - else - r_assignment_target.declaration = declaration; - } - // TODO This check is overly broad and may prevent some optimizations. - else if(declaration && declaration==r_assignment_target.declaration) - r_self_referencing = true; -} - -void VariableResolver::visit(VariableReference &var) -{ - VariableDeclaration *declaration = 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; (!declaration && block); block=block->parent) - { - map::iterator i = block->variables.find(var.name); - if(i!=block->variables.end()) - declaration = i->second; - } - - if(!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. */ - InterfaceBlockReference *iface_ref = new InterfaceBlockReference; - iface_ref->source = var.source; - iface_ref->line = var.line; - iface_ref->name = var.name; - iface_ref->declaration = i->second; - r_replacement_expr = iface_ref; - } - else - { - // Look for the variable in anonymous interface blocks. - for(i=blocks.begin(); (!declaration && i!=blocks.end()); ++i) - if(i->second->instance_name.empty() && i->second->struct_declaration) - { - const map &iface_vars = i->second->struct_declaration->members.variables; - map::const_iterator j = iface_vars.find(var.name); - if(j!=iface_vars.end()) - declaration = j->second; - } - } - } - - r_any_resolved |= (declaration!=var.declaration); - var.declaration = declaration; - - check_assignment_target(var.declaration); -} - -void VariableResolver::visit(InterfaceBlockReference &iface) -{ - map::iterator 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; - - check_assignment_target(iface.declaration); -} - -void VariableResolver::add_to_chain(Assignment::Target::ChainType type, unsigned index) -{ - if(r_assignment_target.chain_len<7) - r_assignment_target.chain[r_assignment_target.chain_len] = type | min(index, 0x3F); - ++r_assignment_target.chain_len; -} - -void VariableResolver::visit(MemberAccess &memacc) -{ - TraversingVisitor::visit(memacc); - - VariableDeclaration *declaration = 0; - if(StructDeclaration *strct = dynamic_cast(memacc.left->type)) - { - map::iterator i = strct->members.variables.find(memacc.member); - if(i!=strct->members.variables.end()) - { - declaration = i->second; - - if(record_target) - { - unsigned index = 0; - for(NodeList::const_iterator j=strct->members.body.begin(); (j!=strct->members.body.end() && j->get()!=i->second); ++j) - ++index; - - add_to_chain(Assignment::Target::MEMBER, index); - } - } - } - else if(BasicTypeDeclaration *basic = dynamic_cast(memacc.left->type)) - { - bool scalar_swizzle = ((basic->kind==BasicTypeDeclaration::INT || basic->kind==BasicTypeDeclaration::FLOAT) && memacc.member.size()==1); - bool vector_swizzle = (basic->kind==BasicTypeDeclaration::VECTOR && memacc.member.size()<=4); - if(scalar_swizzle || vector_swizzle) - { - static const char component_names[] = { 'x', 'r', 's', 'y', 'g', 't', 'z', 'b', 'p', 'w', 'a', 'q' }; - - bool ok = true; - UInt8 components[4] = { }; - for(unsigned i=0; (ok && isource = memacc.source; - swizzle->line = memacc.line; - swizzle->oper = memacc.oper; - swizzle->left = memacc.left; - swizzle->component_group = memacc.member; - swizzle->count = memacc.member.size(); - copy(components, components+memacc.member.size(), swizzle->components); - r_replacement_expr = swizzle; - } - } - } - - r_any_resolved |= (declaration!=memacc.declaration); - memacc.declaration = declaration; -} - -void VariableResolver::visit(Swizzle &swizzle) -{ - TraversingVisitor::visit(swizzle); - - if(record_target) - { - unsigned mask = 0; - for(unsigned i=0; itoken[0]=='[') + for(VariableDeclaration *v: auto_constants) { - { - /* The subscript expression is not a part of the primary assignment - target. */ - SetFlag set(record_target, false); - visit(binary.right); - } - visit(binary.left); + unsigned id = hash32(v->name)%features.constant_id_range; + while(used_ids.count(id)) + id = (id+1)%features.constant_id_range; - if(record_target) - { - unsigned index = 0x3F; - if(Literal *literal_subscript = dynamic_cast(binary.right.get())) - if(literal_subscript->value.check_type()) - index = literal_subscript->value.value(); - add_to_chain(Assignment::Target::ARRAY, index); - } - } - else - TraversingVisitor::visit(binary); -} + auto i = find_member(v->layout->qualifiers, string("constant_id"), &Layout::Qualifier::name); + if(i!=v->layout->qualifiers.end()) + i->value = id; -void VariableResolver::visit(Assignment &assign) -{ - { - SetFlag set(record_target); - r_assignment_target = Assignment::Target(); - visit(assign.left); - r_any_resolved |= (r_assignment_targettoken[0]!='='); -} - -void VariableResolver::visit(VariableDeclaration &var) -{ - 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)); - - TraversingVisitor::visit(iface); } - -ExpressionResolver::ExpressionResolver(): - stage(0), - r_any_resolved(false) -{ } - -bool ExpressionResolver::apply(Stage &s) +void ConstantIdAssigner::visit(VariableDeclaration &var) { - stage = &s; - r_any_resolved = false; - s.content.visit(*this); - 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) - return SAME_TYPE; - else if(can_convert(left, right)) - return LEFT_CONVERTIBLE; - else if(can_convert(right, left)) - return RIGHT_CONVERTIBLE; - else - return NOT_COMPATIBLE; -} - -BasicTypeDeclaration *ExpressionResolver::find_type(BasicTypeDeclaration::Kind kind, unsigned size) -{ - for(vector::const_iterator i=basic_types.begin(); i!=basic_types.end(); ++i) - if((*i)->kind==kind && (*i)->size==size) - return *i; - return 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; -} - -void ExpressionResolver::convert_to(RefPtr &expr, BasicTypeDeclaration &type) -{ - RefPtr call = new FunctionCall; - call->name = type.name; - call->constructor = true; - call->arguments.push_back(0); - call->arguments.back() = expr; - call->type = &type; - expr = call; -} - -bool ExpressionResolver::convert_to_element(RefPtr &expr, BasicTypeDeclaration &elem_type) -{ - if(BasicTypeDeclaration *expr_basic = dynamic_cast(expr->type)) - { - BasicTypeDeclaration *to_type = &elem_type; - if(is_vector_or_matrix(*expr_basic)) - to_type = find_type(elem_type, expr_basic->kind, expr_basic->size); - if(to_type) - { - convert_to(expr, *to_type); - return true; - } - } - - return false; -} - -void ExpressionResolver::resolve(Expression &expr, TypeDeclaration *type, bool lvalue) -{ - r_any_resolved |= (type!=expr.type || lvalue!=expr.lvalue); - expr.type = type; - expr.lvalue = lvalue; -} - -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); - else if(literal.value.check_type()) - resolve(literal, find_type(BasicTypeDeclaration::FLOAT, 32), false); -} - -void ExpressionResolver::visit(ParenthesizedExpression &parexpr) -{ - TraversingVisitor::visit(parexpr); - resolve(parexpr, parexpr.expression->type, parexpr.expression->lvalue); -} - -void ExpressionResolver::visit(VariableReference &var) -{ - if(var.declaration) - resolve(var, var.declaration->type_declaration, true); -} - -void ExpressionResolver::visit(InterfaceBlockReference &iface) -{ - if(iface.declaration) - resolve(iface, iface.declaration->type_declaration, true); -} - -void ExpressionResolver::visit(MemberAccess &memacc) -{ - TraversingVisitor::visit(memacc); - - if(memacc.declaration) - resolve(memacc, memacc.declaration->type_declaration, memacc.left->lvalue); -} - -void ExpressionResolver::visit(Swizzle &swizzle) -{ - TraversingVisitor::visit(swizzle); - - if(BasicTypeDeclaration *left_basic = dynamic_cast(swizzle.left->type)) - { - BasicTypeDeclaration *left_elem = get_element_type(*left_basic); - if(swizzle.count==1) - resolve(swizzle, left_elem, swizzle.left->lvalue); - else if(left_basic->kind==BasicTypeDeclaration::VECTOR && left_elem) - resolve(swizzle, find_type(*left_elem, left_basic->kind, swizzle.count), swizzle.left->lvalue); - } -} - -void ExpressionResolver::visit(UnaryExpression &unary) -{ - TraversingVisitor::visit(unary); - - BasicTypeDeclaration *basic = dynamic_cast(unary.expression->type); - if(!basic) - return; - - char oper = unary.oper->token[0]; - if(oper=='!') - { - if(basic->kind!=BasicTypeDeclaration::BOOL) - return; - } - else if(oper=='~') - { - if(basic->kind!=BasicTypeDeclaration::INT) - return; - } - else if(oper=='+' || oper=='-') - { - BasicTypeDeclaration *elem = get_element_type(*basic); - if(!elem || !is_scalar(*elem)) - return; - } - resolve(unary, basic, unary.expression->lvalue); -} - -void ExpressionResolver::visit(BinaryExpression &binary, bool assign) -{ - /* Binary operators are only defined for basic types (not for image or - structure types). */ - BasicTypeDeclaration *basic_left = dynamic_cast(binary.left->type); - BasicTypeDeclaration *basic_right = dynamic_cast(binary.right->type); - if(!basic_left || !basic_right) - return; - - char oper = binary.oper->token[0]; - if(oper=='[') - { - /* Subscripting operates on vectors, matrices and arrays, and the right - operand must be an integer. */ - if((!is_vector_or_matrix(*basic_left) && basic_left->kind!=BasicTypeDeclaration::ARRAY) || basic_right->kind!=BasicTypeDeclaration::INT) - return; - - resolve(binary, basic_left->base_type, binary.left->lvalue); - return; - } - else if(basic_left->kind==BasicTypeDeclaration::ARRAY || basic_right->kind==BasicTypeDeclaration::ARRAY) - // No other binary operator can be used with arrays. - return; - - BasicTypeDeclaration *elem_left = get_element_type(*basic_left); - BasicTypeDeclaration *elem_right = get_element_type(*basic_right); - if(!elem_left || !elem_right) - return; - - Compatibility compat = get_compatibility(*basic_left, *basic_right); - Compatibility elem_compat = get_compatibility(*elem_left, *elem_right); - if(elem_compat==NOT_COMPATIBLE) - return; - if(assign && (compat==LEFT_CONVERTIBLE || elem_compat==LEFT_CONVERTIBLE)) - return; - - TypeDeclaration *type = 0; - char oper2 = binary.oper->token[1]; - if((oper=='<' && oper2!='<') || (oper=='>' && oper2!='>')) - { - /* Relational operators compare two scalar integer or floating-point - values. */ - if(!is_scalar(*elem_left) || !is_scalar(*elem_right) || compat==NOT_COMPATIBLE) - return; - - type = find_type(BasicTypeDeclaration::BOOL, 1); - } - else if((oper=='=' || oper=='!') && oper2=='=') - { - // Equality comparison can be done on any compatible types. - if(compat==NOT_COMPATIBLE) - return; - - type = find_type(BasicTypeDeclaration::BOOL, 1); - } - else if(oper2=='&' || oper2=='|' || oper2=='^') - { - // Logical operators can only be applied to booleans. - if(basic_left->kind!=BasicTypeDeclaration::BOOL || basic_right->kind!=BasicTypeDeclaration::BOOL) - return; - - type = basic_left; - } - else if((oper=='&' || oper=='|' || oper=='^' || oper=='%') && !oper2) - { - // Bitwise operators and modulo can only be applied to integers. - if(basic_left->kind!=BasicTypeDeclaration::INT || basic_right->kind!=BasicTypeDeclaration::INT) - return; - - type = (compat==LEFT_CONVERTIBLE ? basic_right : basic_left); - } - else if((oper=='<' || oper=='>') && oper2==oper) - { - // Shifts apply to integer scalars and vectors, with some restrictions. - if(elem_left->kind!=BasicTypeDeclaration::INT || elem_right->kind!=BasicTypeDeclaration::INT) - return; - unsigned left_size = (basic_left->kind==BasicTypeDeclaration::INT ? 1 : basic_left->kind==BasicTypeDeclaration::VECTOR ? basic_left->size : 0); - unsigned right_size = (basic_right->kind==BasicTypeDeclaration::INT ? 1 : basic_right->kind==BasicTypeDeclaration::VECTOR ? basic_right->size : 0); - if(!left_size || (left_size==1 && right_size!=1) || (left_size>1 && right_size!=1 && right_size!=left_size)) - return; - - type = basic_left; - // Don't perform conversion even if the operands are of different sizes. - compat = SAME_TYPE; - } - else if(oper=='+' || oper=='-' || oper=='*' || oper=='/') - { - // Arithmetic operators require scalar elements. - if(!is_scalar(*elem_left) || !is_scalar(*elem_right)) - return; - - if(oper=='*' && is_vector_or_matrix(*basic_left) && is_vector_or_matrix(*basic_right) && - (basic_left->kind==BasicTypeDeclaration::MATRIX || basic_right->kind==BasicTypeDeclaration::MATRIX)) - { - /* Multiplication has special rules when at least one operand is a - matrix and the other is a vector or a matrix. */ - unsigned left_columns = basic_left->size&0xFFFF; - unsigned right_rows = basic_right->size; - if(basic_right->kind==BasicTypeDeclaration::MATRIX) - right_rows >>= 16; - if(left_columns!=right_rows) - return; - - BasicTypeDeclaration *elem_result = (elem_compat==LEFT_CONVERTIBLE ? elem_right : elem_left); - - if(basic_left->kind==BasicTypeDeclaration::VECTOR) - type = find_type(*elem_result, BasicTypeDeclaration::VECTOR, basic_right->size&0xFFFF); - else if(basic_right->kind==BasicTypeDeclaration::VECTOR) - type = find_type(*elem_result, BasicTypeDeclaration::VECTOR, basic_left->size>>16); - else - type = find_type(*elem_result, BasicTypeDeclaration::MATRIX, (basic_left->size&0xFFFF0000)|(basic_right->size&0xFFFF)); - } - else if(compat==NOT_COMPATIBLE) - { - // Arithmetic between scalars and matrices or vectors is supported. - if(is_scalar(*basic_left) && is_vector_or_matrix(*basic_right)) - type = (elem_compat==RIGHT_CONVERTIBLE ? find_type(*elem_left, basic_right->kind, basic_right->size) : basic_right); - else if(is_vector_or_matrix(*basic_left) && is_scalar(*basic_right)) - type = (elem_compat==LEFT_CONVERTIBLE ? find_type(*elem_right, basic_left->kind, basic_left->size) : basic_left); - else - return; - } - else if(compat==LEFT_CONVERTIBLE) - type = basic_right; - else - type = basic_left; - } - else - return; - - if(assign && type!=basic_left) - return; - - bool converted = true; - if(compat==LEFT_CONVERTIBLE) - convert_to(binary.left, *basic_right); - else if(compat==RIGHT_CONVERTIBLE) - convert_to(binary.right, *basic_left); - else if(elem_compat==LEFT_CONVERTIBLE) - converted = convert_to_element(binary.left, *elem_right); - else if(elem_compat==RIGHT_CONVERTIBLE) - converted = convert_to_element(binary.right, *elem_left); - - if(!converted) - type = 0; - - resolve(binary, type, assign); -} - -void ExpressionResolver::visit(BinaryExpression &binary) -{ - TraversingVisitor::visit(binary); - visit(binary, false); -} - -void ExpressionResolver::visit(Assignment &assign) -{ - TraversingVisitor::visit(assign); - - if(assign.oper->token[0]!='=') - return visit(assign, true); - else if(assign.left->type!=assign.right->type) - { - BasicTypeDeclaration *basic_left = dynamic_cast(assign.left->type); - BasicTypeDeclaration *basic_right = dynamic_cast(assign.right->type); - if(!basic_left || !basic_right) - return; - - Compatibility compat = get_compatibility(*basic_left, *basic_right); - if(compat==RIGHT_CONVERTIBLE) - convert_to(assign.right, *basic_left); - else if(compat!=SAME_TYPE) - return; - } - - resolve(assign, assign.left->type, true); -} - -void ExpressionResolver::visit(FunctionCall &call) -{ - TraversingVisitor::visit(call); - - TypeDeclaration *type = 0; - if(call.declaration) - type = call.declaration->return_type_declaration; - else if(call.constructor) - { - map::const_iterator i=stage->types.find(call.name); - type = (i!=stage->types.end() ? i->second : 0); - } - resolve(call, type, false); -} - -void ExpressionResolver::visit(BasicTypeDeclaration &type) -{ - basic_types.push_back(&type); -} - -void ExpressionResolver::visit(VariableDeclaration &var) -{ - TraversingVisitor::visit(var); - if(!var.init_expression) - return; - - BasicTypeDeclaration *var_basic = dynamic_cast(var.type_declaration); - BasicTypeDeclaration *init_basic = dynamic_cast(var.init_expression->type); - if(!var_basic || !init_basic) - return; - - Compatibility compat = get_compatibility(*var_basic, *init_basic); - if(compat==RIGHT_CONVERTIBLE) - convert_to(var.init_expression, *var_basic); -} - - -bool FunctionResolver::apply(Stage &s) -{ - stage = &s; - s.functions.clear(); - r_any_resolved = false; - s.content.visit(*this); - return r_any_resolved; -} - -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) - { - if((*i)->type) - append(arg_types, ",", (*i)->type->name); - else - has_signature = false; - } - - FunctionDeclaration *declaration = 0; - if(has_signature) - { - map::iterator i = stage->functions.find(format("%s(%s)", call.name, arg_types)); - declaration = (i!=stage->functions.end() ? i->second : 0); - } - r_any_resolved |= (declaration!=call.declaration); - call.declaration = declaration; - - TraversingVisitor::visit(call); -} - -void FunctionResolver::visit(FunctionDeclaration &func) -{ - if(func.signature.empty()) + if(var.layout) { - string param_types; - for(NodeArray::const_iterator i=func.parameters.begin(); i!=func.parameters.end(); ++i) + auto i = find_member(var.layout->qualifiers, string("constant_id"), &Layout::Qualifier::name); + if(i!=var.layout->qualifiers.end() && i->has_value) { - if((*i)->type_declaration) - append(param_types, ",", (*i)->type_declaration->name); + if(i->value==-1) + auto_constants.push_back(&var); else - return; + used_ids.insert(i->value); } - func.signature = format("(%s)", param_types); - r_any_resolved = true; } - - string key = func.name+func.signature; - FunctionDeclaration *&stage_decl = stage->functions[key]; - vector &decls = declarations[key]; - 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) - { - r_any_resolved |= (func.definition!=(*i)->definition); - (*i)->definition = func.definition; - (*i)->body.body.clear(); - } - } - else - { - FunctionDeclaration *definition = (stage_decl ? stage_decl->definition : 0); - r_any_resolved |= (definition!=func.definition); - func.definition = definition; - - if(!stage_decl) - stage_decl = &func; - } - decls.push_back(&func); - - TraversingVisitor::visit(func); } -InterfaceGenerator::InterfaceGenerator(): - stage(0), - function_scope(false), - copy_block(false), - iface_target_block(0) -{ } - string InterfaceGenerator::get_out_prefix(Stage::Type type) { if(type==Stage::VERTEX) @@ -1019,7 +68,7 @@ void InterfaceGenerator::apply(Stage &s) void InterfaceGenerator::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) { assignment_insert_point = i; if(&block==&stage->content) @@ -1073,12 +122,12 @@ VariableDeclaration *InterfaceGenerator::generate_interface(VariableDeclaration 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) @@ -1099,9 +148,9 @@ InterfaceBlock *InterfaceGenerator::generate_interface(InterfaceBlock &out_block } 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)); + 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); @@ -1137,7 +186,7 @@ void InterfaceGenerator::visit(VariableReference &var) return; const map &prev_vars = stage->previous->content.variables; - map::const_iterator i = prev_vars.find(var.name); + auto i = prev_vars.find(var.name); if(i==prev_vars.end() || i->second->interface!="out") i = prev_vars.find(in_prefix+var.name); if(i!=prev_vars.end() && i->second->interface=="out") @@ -1154,7 +203,7 @@ void InterfaceGenerator::visit(VariableReference &var) } const map &prev_blocks = stage->previous->interface_blocks; - map::const_iterator j = prev_blocks.find("_"+var.name); + auto j = prev_blocks.find(var.name); if(j!=prev_blocks.end() && j->second->interface=="out") { generate_interface(*j->second); @@ -1163,14 +212,14 @@ void InterfaceGenerator::visit(VariableReference &var) return; } - for(j=prev_blocks.begin(); j!=prev_blocks.end(); ++j) - if(j->second->instance_name.empty() && j->second->struct_declaration) + for(const auto &kvp: prev_blocks) + if(kvp.second->instance_name.empty() && kvp.second->struct_declaration) { - const map &iface_vars = j->second->struct_declaration->members.variables; + const map &iface_vars = kvp.second->struct_declaration->members.variables; i = iface_vars.find(var.name); if(i!=iface_vars.end()) { - generate_interface(*j->second); + generate_interface(*kvp.second); return; } } @@ -1201,14 +250,15 @@ void InterfaceGenerator::visit(VariableDeclaration &var) } else if(var.interface=="in" && current_block==&stage->content) { - declared_inputs.push_back(&var); + 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(!var.linked_declaration && stage->previous) { const map &prev_vars = stage->previous->content.variables; - map::const_iterator i = prev_vars.find(var.name); + auto i = prev_vars.find(var.name); if(i!=prev_vars.end() && i->second->interface=="out") { var.linked_declaration = i->second; @@ -1229,7 +279,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); + auto i = prev_blocks.find("out "+iface.block_name); if(i!=prev_blocks.end()) { iface.linked_block = i->second; @@ -1255,16 +305,15 @@ void InterfaceGenerator::visit(Passthrough &pass) if(stage->previous) { - const map &prev_vars = stage->previous->content.variables; - for(map::const_iterator i=prev_vars.begin(); i!=prev_vars.end(); ++i) + for(const auto &kvp: stage->previous->content.variables) { - if(i->second->interface!="out") + if(kvp.second->interface!="out") continue; /* 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(!kvp.second->linked_declaration && generate_interface(*kvp.second, "in", kvp.second->name)) + pass_vars.push_back(kvp.second); } } @@ -1287,13 +336,13 @@ void InterfaceGenerator::visit(Passthrough &pass) insert_assignment("gl_Position", memacc); } - for(vector::const_iterator i=pass_vars.begin(); i!=pass_vars.end(); ++i) + for(VariableDeclaration *v: pass_vars) { - string out_name = change_prefix((*i)->name, out_prefix); - generate_interface(**i, "out", out_name); + string out_name = change_prefix(v->name, out_prefix); + generate_interface(*v, "out", out_name); VariableReference *ref = new VariableReference; - ref->name = (*i)->name; + ref->name = v->name; if(pass.subscript) { BinaryExpression *subscript = new BinaryExpression;