X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fglsl%2Fgenerate.cpp;h=4e225ae520569dab6188666ba1bc7a2c026a5bb2;hb=c1d3a1d;hp=b7ab9d3f7b9fc956c37752f18e7e5bb36df0e0d3;hpb=cb8ee1f5ea24a5f7b561fc88d4ff5af3ae364038;p=libs%2Fgl.git diff --git a/source/glsl/generate.cpp b/source/glsl/generate.cpp index b7ab9d3f..4e225ae5 100644 --- a/source/glsl/generate.cpp +++ b/source/glsl/generate.cpp @@ -116,31 +116,57 @@ void ConstantSpecializer::visit(VariableDeclaration &var) void BlockHierarchyResolver::enter(Block &block) { + r_any_resolved |= (current_block!=block.parent); block.parent = current_block; } TypeResolver::TypeResolver(): - stage(0) + stage(0), + r_any_resolved(false) { } -void TypeResolver::apply(Stage &s) +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; } -TypeDeclaration *TypeResolver::resolve_type(const string &name) +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); - return (j!=alias_map.end() ? j->second : i->second); + resolved = (j!=alias_map.end() ? j->second : i->second); } - else - return 0; + + if(resolved && array) + resolved = get_or_create_array_type(*resolved); + + r_any_resolved |= (resolved!=type); + type=resolved; } void TypeResolver::visit(Block &block) @@ -155,7 +181,7 @@ void TypeResolver::visit(Block &block) void TypeResolver::visit(BasicTypeDeclaration &type) { - type.base_type = resolve_type(type.base); + 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)) @@ -175,7 +201,7 @@ void TypeResolver::visit(BasicTypeDeclaration &type) void TypeResolver::visit(ImageTypeDeclaration &type) { - type.base_type = resolve_type(type.base); + resolve_type(type.base_type, type.base, false); stage->types.insert(make_pair(type.name, &type)); } @@ -187,31 +213,12 @@ void TypeResolver::visit(StructDeclaration &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; + resolve_type(var.type_declaration, var.type, var.array); } void TypeResolver::visit(FunctionDeclaration &func) { - func.return_type_declaration = resolve_type(func.return_type); + resolve_type(func.return_type_declaration, func.return_type, false); TraversingVisitor::visit(func); } @@ -584,10 +591,8 @@ void ExpressionResolver::visit(UnaryExpression &unary) resolve(unary, basic, unary.expression->lvalue); } -void ExpressionResolver::visit(BinaryExpression &binary) +void ExpressionResolver::visit(BinaryExpression &binary, bool assign) { - TraversingVisitor::visit(binary); - /* Binary operators are only defined for basic types (not for image or structure types). */ BasicTypeDeclaration *basic_left = dynamic_cast(binary.left->type); @@ -619,6 +624,8 @@ void ExpressionResolver::visit(BinaryExpression &binary) 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]; @@ -657,11 +664,17 @@ void ExpressionResolver::visit(BinaryExpression &binary) } else if((oper=='<' || oper=='>') && oper2==oper) { - // Shifts only apply to integers. - if(basic_left->kind!=BasicTypeDeclaration::INT || basic_right->kind!=BasicTypeDeclaration::INT) + // 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=='/') { @@ -708,6 +721,9 @@ void ExpressionResolver::visit(BinaryExpression &binary) else return; + if(assign && type!=basic_left) + return; + bool converted = true; if(compat==LEFT_CONVERTIBLE) convert_to(binary.left, *basic_right); @@ -721,12 +737,35 @@ void ExpressionResolver::visit(BinaryExpression &binary) if(!converted) type = 0; - resolve(binary, type, false); + 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); } @@ -767,11 +806,13 @@ void ExpressionResolver::visit(VariableDeclaration &var) } -void FunctionResolver::apply(Stage &s) +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)