[Mesa-dev] [v2 PATCH 11/16] glsl: Optimize clamp(x, b, 1.0), where b > 0.0 as max(saturate(x), b)

Abdiel Janulgue abdiel.janulgue at linux.intel.com
Mon Jul 7 06:57:08 PDT 2014


v2: - Output max(saturate(x),b) instead of saturate(max(x,b))
    - Make sure we do component-wise comparison for vectors (Ian Romanick)

Signed-off-by: Abdiel Janulgue <abdiel.janulgue at linux.intel.com>
---
 src/glsl/opt_algebraic.cpp | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/src/glsl/opt_algebraic.cpp b/src/glsl/opt_algebraic.cpp
index bcda7a9..7314dbb 100644
--- a/src/glsl/opt_algebraic.cpp
+++ b/src/glsl/opt_algebraic.cpp
@@ -653,6 +653,18 @@ ir_algebraic_visitor::handle_expression(ir_expression *ir)
             if ((component == outer_const->type->vector_elements) &&
                 inner_val_b->is_zero())
                return expr(ir_binop_min, saturate(inner_val_a), outer_const);
+
+            component = 0;
+            ir_constant *inner_const = inner_val_b->as_constant();
+            if (!inner_const)
+               continue;
+            for (int c = 0; c < inner_const->type->vector_elements; c++)
+               if (inner_const->get_float_component(c) > 0.0f)
+                  component++;
+            /* Found a min (max(x, b), 1.0), where b > 0.0 */
+            if (outer_const->is_one() &&
+                component == inner_const->type->vector_elements)
+               return expr(ir_binop_max, saturate(inner_val_a), inner_val_b);
          }
       }
 
-- 
1.9.1



More information about the mesa-dev mailing list