summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAbdiel Janulgue <[email protected]>2014-06-19 22:15:14 -0700
committerAbdiel Janulgue <[email protected]>2014-08-31 21:04:08 +0300
commit8f890b119eaff88a7fad64abbf183cbcc22edc7a (patch)
tree58204934be18dc747a22ee575c270cea0d1f52fb
parentcbd0d643a30901dabc5b3cc84121bafe16f8fc6f (diff)
glsl: Optimize clamp(x, 0, 1) as saturate(x)
v2: - Check that the base type is float (Ian Romanick) v3: - Make sure comments reflect that we are doing a commutative operation - Add missing condition where the inner constant is 1.0 and outer constant is 0.0 - Make indexing of operands easier to read (Matt Turner) Reviewed-by: Matt Turner <[email protected]> Reviewed-by: Ian Romanick <[email protected]> Signed-off-by: Abdiel Janulgue <[email protected]>
-rw-r--r--src/glsl/opt_algebraic.cpp36
1 files changed, 36 insertions, 0 deletions
diff --git a/src/glsl/opt_algebraic.cpp b/src/glsl/opt_algebraic.cpp
index ac7514acfd9..4b052933654 100644
--- a/src/glsl/opt_algebraic.cpp
+++ b/src/glsl/opt_algebraic.cpp
@@ -614,6 +614,42 @@ ir_algebraic_visitor::handle_expression(ir_expression *ir)
break;
+ case ir_binop_min:
+ case ir_binop_max:
+ if (ir->type->base_type != GLSL_TYPE_FLOAT)
+ break;
+
+ /* Replace min(max) operations and its commutative combinations with
+ * a saturate operation
+ */
+ for (int op = 0; op < 2; op++) {
+ ir_expression *minmax = op_expr[op];
+ ir_constant *outer_const = op_const[1 - op];
+ ir_expression_operation op_cond = (ir->operation == ir_binop_max) ?
+ ir_binop_min : ir_binop_max;
+
+ if (!minmax || !outer_const || (minmax->operation != op_cond))
+ continue;
+
+ /* Found a min(max) combination. Now try to see if its operands
+ * meet our conditions that we can do just a single saturate operation
+ */
+ for (int minmax_op = 0; minmax_op < 2; minmax_op++) {
+ ir_rvalue *inner_val_a = minmax->operands[minmax_op];
+ ir_rvalue *inner_val_b = minmax->operands[1 - minmax_op];
+
+ if (!inner_val_a || !inner_val_b)
+ continue;
+
+ /* Found a {min|max} ({max|min} (x, 0.0), 1.0) operation and its variations */
+ if ((outer_const->is_one() && inner_val_a->is_zero()) ||
+ (inner_val_a->is_one() && outer_const->is_zero()))
+ return saturate(inner_val_b);
+ }
+ }
+
+ break;
+
case ir_unop_rcp:
if (op_expr[0] && op_expr[0]->operation == ir_unop_rcp)
return op_expr[0]->operands[0];