[Mesa-dev] [PATCH 06/10] glsl: Optimize max(8, sin(x)) == 8 and similar

thomashelland90 at gmail.com thomashelland90 at gmail.com
Mon Jul 14 15:22:35 PDT 2014


From: Thomas Helland <thomashelland90 at gmail.com>

This also only handles floats.
ir_unop_saturate should also be added when the
support for this lands in the ir.

Signed-off-by: Thomas Helland <thomashelland90 at gmail.com>
---
 src/glsl/opt_algebraic.cpp | 38 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 38 insertions(+)

diff --git a/src/glsl/opt_algebraic.cpp b/src/glsl/opt_algebraic.cpp
index 90b6cae..deaa49e 100644
--- a/src/glsl/opt_algebraic.cpp
+++ b/src/glsl/opt_algebraic.cpp
@@ -684,6 +684,44 @@ ir_algebraic_visitor::handle_expression(ir_expression *ir)
       break;
    }
 
+   case ir_binop_max: {
+      ir_rvalue *tempExpr;
+      ir_rvalue *tempConst;
+
+      if (op_expr[0] && op_const[1]) {
+         tempExpr = ir->operands[0];
+         tempConst = ir->operands[1];
+      } else if (op_expr[1] && op_const[0]) {
+         tempExpr = ir->operands[1];
+         tempConst = ir->operands[0];
+      } else {
+         break;
+      }
+
+      if(tempConst->type->is_float()) {
+         float constValue = *(tempConst->constant_expression_value()->value.f);
+         switch(tempExpr->as_expression()->operation) {
+         case ir_unop_cos:
+         case ir_unop_sin:
+         case ir_unop_sign:
+            /* If we are comparing an expression bound between -1 and 1
+             * with a const >= 1 we know that const will be the largest
+             */
+            if(constValue >= 1.0f)
+               return tempConst;
+            /* If we are comparing an expression bound between -1 and 1
+             * with a const <= -1 we know that const will be the smallest
+             */
+            if(constValue <= -1.0f)
+               return tempExpr;
+            break;
+         default:
+            break;
+         }
+      }
+      break;
+   }
+
    case ir_unop_rcp:
       if (op_expr[0] && op_expr[0]->operation == ir_unop_rcp)
 	 return op_expr[0]->operands[0];
-- 
2.0.0



More information about the mesa-dev mailing list