Mesa (master): glsl: Optimize clamp(x, b, 1.0), where b > 0.0 as max( saturate(x),b)

Abdiel Janulgue abj at kemper.freedesktop.org
Sun Aug 31 18:04:54 UTC 2014


Module: Mesa
Branch: master
Commit: 0e2ba3ee827f77af0b1f322d95c650f6f7f4da88
URL:    http://cgit.freedesktop.org/mesa/mesa/commit/?id=0e2ba3ee827f77af0b1f322d95c650f6f7f4da88

Author: Abdiel Janulgue <abdiel.janulgue at linux.intel.com>
Date:   Tue Jul  8 14:12:50 2014 +0300

glsl: Optimize clamp(x, b, 1.0), where b > 0.0 as max(saturate(x),b)

v2: - Output max(saturate(x),b) instead of saturate(max(x,b))
    - Make sure we do component-wise comparison for vectors (Ian Romanick)
v3: - Add missing condition where the outer constant value is > 0.0 and
      inner constant is 1.0.
    - Fix comments to show that the optimization is a commutative operation
      (Matt Turner)

Reviewed-by: Ian Romanick <ian.d.romanick at intel.com>
Signed-off-by: Abdiel Janulgue <abdiel.janulgue at linux.intel.com>

---

 src/glsl/opt_algebraic.cpp |   23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)

diff --git a/src/glsl/opt_algebraic.cpp b/src/glsl/opt_algebraic.cpp
index 6dfb681..447618f 100644
--- a/src/glsl/opt_algebraic.cpp
+++ b/src/glsl/opt_algebraic.cpp
@@ -137,6 +137,21 @@ is_less_than_one(ir_constant *ir)
    return (component == ir->type->vector_elements);
 }
 
+static inline bool
+is_greater_than_zero(ir_constant *ir)
+{
+   if (!is_valid_vec_const(ir))
+      return false;
+
+   unsigned component = 0;
+   for (int c = 0; c < ir->type->vector_elements; c++) {
+      if (ir->get_float_component(c) > 0.0f)
+         component++;
+   }
+
+   return (component == ir->type->vector_elements);
+}
+
 static void
 update_type(ir_expression *ir)
 {
@@ -684,6 +699,14 @@ ir_algebraic_visitor::handle_expression(ir_expression *ir)
 
             if (is_less_than_one(inner_val_b->as_constant()) && outer_const->is_zero())
                return expr(ir_binop_min, saturate(inner_val_a), inner_val_b);
+
+            /* Found a {min|max} ({max|min} (x, b), 1.0), where b > 0.0
+             * and its variations
+             */
+            if (outer_const->is_one() && is_greater_than_zero(inner_val_b->as_constant()))
+               return expr(ir_binop_max, saturate(inner_val_a), inner_val_b);
+            if (inner_val_b->as_constant()->is_one() && is_greater_than_zero(outer_const))
+               return expr(ir_binop_max, saturate(inner_val_a), outer_const);
          }
       }
 




More information about the mesa-commit mailing list