X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fglsl%2Foptimize.cpp;h=d21512f6c82e4e6bdef3ccbdf38f70cd6e018e66;hb=4737d137d0a1c7fed868c4adc7a3d7e00ba7681c;hp=63dfcb5c2823758dc054f92cb4d1960650e05d44;hpb=188b3b2bf686aada8772475aabf899a0420cbd0f;p=libs%2Fgl.git diff --git a/source/glsl/optimize.cpp b/source/glsl/optimize.cpp index 63dfcb5c..d21512f6 100644 --- a/source/glsl/optimize.cpp +++ b/source/glsl/optimize.cpp @@ -1,5 +1,6 @@ #include #include +#include #include "optimize.h" #include "reflect.h" @@ -591,6 +592,29 @@ T ConstantFolder::evaluate_arithmetic(char oper, T left, T right) } } +template +T ConstantFolder::evaluate_int_special_op(char oper, T left, T right) +{ + switch(oper) + { + case '%': return left%right; + case '<': return left<': return left>>right; + default: return T(); + } +} + +template +void ConstantFolder::convert_to_result(const Variant &value) +{ + if(value.check_type()) + set_result(static_cast(value.value())); + else if(value.check_type()) + set_result(static_cast(value.value())); + else if(value.check_type()) + set_result(static_cast(value.value())); +} + void ConstantFolder::set_result(const Variant &value, bool literal) { r_constant_value = value; @@ -617,7 +641,11 @@ void ConstantFolder::visit(RefPtr &expr) else if(r_constant_value.check_type()) literal->token = lexical_cast(r_constant_value.value()); else if(r_constant_value.check_type()) + { literal->token = lexical_cast(r_constant_value.value()); + if(isnumrc(literal->token)) + literal->token += ".0"; + } else { r_constant = false; @@ -740,15 +768,8 @@ void ConstantFolder::visit(BinaryExpression &binary) } else if(oper=='%' || ((oper=='<' || oper=='>') && oper2==oper)) { - if(!left_value.check_type()) - return; - - if(oper=='%') - set_result(left_value.value()%r_constant_value.value()); - else if(oper=='<') - set_result(left_value.value()<()); - else if(oper=='>') - set_result(left_value.value()>>r_constant_value.value()); + if(left_value.check_type()) + set_result(evaluate_int_special_op(oper, left_value.value(), r_constant_value.value())); } } @@ -766,6 +787,28 @@ void ConstantFolder::visit(TernaryExpression &ternary) void ConstantFolder::visit(FunctionCall &call) { + if(call.constructor && call.type && call.arguments.size()==1) + { + const BasicTypeDeclaration *basic = dynamic_cast(call.type); + if(basic) + { + call.arguments[0]->visit(*this); + bool can_fold = r_constant; + r_constant = false; + if(!can_fold) + return; + + if(basic->kind==BasicTypeDeclaration::BOOL) + convert_to_result(r_constant_value); + else if(basic->kind==BasicTypeDeclaration::INT && basic->size==32) + convert_to_result(r_constant_value); + else if(basic->kind==BasicTypeDeclaration::FLOAT && basic->size==32) + convert_to_result(r_constant_value); + + return; + } + } + TraversingVisitor::visit(call); r_constant = false; }