X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fglsl%2Fgenerate.cpp;h=4e225ae520569dab6188666ba1bc7a2c026a5bb2;hb=c1d3a1d;hp=f29d65f58bfeeaf4fe837fa2222226473c6d3c66;hpb=f639d088c478fe5d266f9f5779928735b5176976;p=libs%2Fgl.git diff --git a/source/glsl/generate.cpp b/source/glsl/generate.cpp index f29d65f5..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::resolve_type(const string &name) +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); - 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); } @@ -657,11 +664,17 @@ void ExpressionResolver::visit(BinaryExpression &binary, bool assign) } 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=='/') { @@ -793,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)