[Mesa-dev] [PATCH 02/23] i965/fs: Optimize (gl_FrontFacing ? x : y) where x and y are ±1.0.

Ian Romanick idr at freedesktop.org
Fri Mar 20 13:58:02 PDT 2015


From: Ian Romanick <ian.d.romanick at intel.com>

This is like Matt's earlier patch, but it enables the optimization for
ir_triop_csel.

There are no shader-db changes now, but this prevents a large number of
regressions from a later commit (glsl: Optimize certain if-statements to
ir_triop_csel).

Signed-off-by: Ian Romanick <ian.d.romanick at intel.com>
Cc: Abdiel Janulgue <abdiel.janulgue at intel.com>
---
 src/mesa/drivers/dri/i965/brw_fs.h           |  1 +
 src/mesa/drivers/dri/i965/brw_fs_visitor.cpp | 34 ++++++++++++++++++++++++++++
 2 files changed, 35 insertions(+)

diff --git a/src/mesa/drivers/dri/i965/brw_fs.h b/src/mesa/drivers/dri/i965/brw_fs.h
index 8317831..c63f7db 100644
--- a/src/mesa/drivers/dri/i965/brw_fs.h
+++ b/src/mesa/drivers/dri/i965/brw_fs.h
@@ -313,6 +313,7 @@ public:
    bool try_emit_mad(ir_expression *ir);
    bool try_replace_with_sel();
    bool try_opt_frontfacing_ternary(ir_if *ir);
+   bool try_opt_frontfacing_ternary(ir_expression *ir);
    bool opt_peephole_sel();
    bool opt_peephole_predicated_break();
    bool opt_saturate_propagation();
diff --git a/src/mesa/drivers/dri/i965/brw_fs_visitor.cpp b/src/mesa/drivers/dri/i965/brw_fs_visitor.cpp
index 3388098..2f1d49c 100644
--- a/src/mesa/drivers/dri/i965/brw_fs_visitor.cpp
+++ b/src/mesa/drivers/dri/i965/brw_fs_visitor.cpp
@@ -708,6 +708,9 @@ fs_visitor::visit(ir_expression *ir)
       break;
 
    case ir_triop_csel:
+      if (try_opt_frontfacing_ternary(ir))
+         return;
+
       ir->operands[1]->accept(this);
       op[1] = this->result;
       ir->operands[2]->accept(this);
@@ -2900,6 +2903,37 @@ fs_visitor::try_opt_frontfacing_ternary(ir_if *ir)
    return false;
 }
 
+bool
+fs_visitor::try_opt_frontfacing_ternary(ir_expression *ir)
+{
+   if (ir->operation != ir_triop_csel)
+      return false;
+
+   ir_dereference_variable *deref = ir->operands[0]->as_dereference_variable();
+   if (!deref || strcmp(deref->var->name, "gl_FrontFacing") != 0)
+      return false;
+
+   ir_constant *then_rhs = ir->operands[1]->as_constant();
+   ir_constant *else_rhs = ir->operands[2]->as_constant();
+
+   if (!then_rhs || !else_rhs)
+      return false;
+
+   if (ir->type->base_type != GLSL_TYPE_FLOAT)
+      return false;
+
+   if ((then_rhs->is_one() && else_rhs->is_negative_one()) ||
+       (else_rhs->is_one() && then_rhs->is_negative_one())) {
+      fs_reg dst = vgrf(glsl_type::int_type);
+      this->result = dst;
+
+      emit_frontfacing_to_float_one_neg_one(this, dst, then_rhs, else_rhs);
+      return true;
+   }
+
+   return false;
+}
+
 /**
  * Try to replace IF/MOV/ELSE/MOV/ENDIF with SEL.
  *
-- 
2.1.0



More information about the mesa-dev mailing list