[Mesa-dev] [v2 PATCH 10/16] glsl: Optimize clamp(x, 0.0, b), where b < 1.0 as min(saturate(x), b)

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


v2: - Output min(saturate(x),b) instead of saturate(min(x,b)) suggested by Ilia Mirkin
    - 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 | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/src/glsl/opt_algebraic.cpp b/src/glsl/opt_algebraic.cpp
index 2ad561c..bcda7a9 100644
--- a/src/glsl/opt_algebraic.cpp
+++ b/src/glsl/opt_algebraic.cpp
@@ -643,6 +643,16 @@ ir_algebraic_visitor::handle_expression(ir_expression *ir)
             /* Found a min (max(x, 0), 1.0) */
             if (outer_const->is_one() && inner_val_a->is_zero())
                return saturate(inner_val_b);
+
+            unsigned component = 0;
+            for (int c = 0; c < outer_const->type->vector_elements; c++) {
+               if (outer_const->get_float_component(c) < 1.0f)
+                  component++;
+            }
+            /* Found a min (max(x, 0.0) b), where b < 1.0 */
+            if ((component == outer_const->type->vector_elements) &&
+                inner_val_b->is_zero())
+               return expr(ir_binop_min, saturate(inner_val_a), outer_const);
          }
       }
 
-- 
1.9.1



More information about the mesa-dev mailing list