Mesa (glsl2): glsl2: Make cross() be an expression operation.

Eric Anholt anholt at kemper.freedesktop.org
Mon Jul 19 01:13:53 UTC 2010


Module: Mesa
Branch: glsl2
Commit: 9be7f638130f46a9df2bfbcd4a03b36de9e4f3aa
URL:    http://cgit.freedesktop.org/mesa/mesa/commit/?id=9be7f638130f46a9df2bfbcd4a03b36de9e4f3aa

Author: Eric Anholt <eric at anholt.net>
Date:   Tue Jul 13 15:37:57 2010 -0700

glsl2: Make cross() be an expression operation.

ARB_fp, ARB_vp, Mesa IR, and the 965 vertex shader all have
instructions for cross.  Shaves 12 Mesa instructions off of a
66-instruction shader I have.

---

 src/glsl/builtin_function.cpp       |   12 +-----------
 src/glsl/builtins/110/cross         |   12 +-----------
 src/glsl/ir.cpp                     |    2 ++
 src/glsl/ir.h                       |    1 +
 src/glsl/ir_constant_expression.cpp |   11 +++++++++++
 src/mesa/shader/ir_to_mesa.cpp      |    5 +++++
 6 files changed, 21 insertions(+), 22 deletions(-)

diff --git a/src/glsl/builtin_function.cpp b/src/glsl/builtin_function.cpp
index 1cf88ad..b3a2833 100644
--- a/src/glsl/builtin_function.cpp
+++ b/src/glsl/builtin_function.cpp
@@ -486,17 +486,7 @@ static const char *builtins_110_cross = {
    "     (parameters\n"
    "       (declare (in) vec3 arg0)\n"
    "       (declare (in) vec3 arg1))\n"
-   "     ((declare () vec3 t)\n"
-   "      (assign (constant bool (1)) (swiz x (var_ref t))\n"
-   "              (expression float - (expression float * (swiz y (var_ref arg0)) (swiz z (var_ref arg1)))\n"
-   "	                          (expression float * (swiz y (var_ref arg1)) (swiz z (var_ref arg0)))))\n"
-   "      (assign (constant bool (1)) (swiz y (var_ref t))\n"
-   "              (expression float - (expression float * (swiz z (var_ref arg0)) (swiz x (var_ref arg1)))\n"
-   "	                          (expression float * (swiz z (var_ref arg1)) (swiz x (var_ref arg0)))))\n"
-   "      (assign (constant bool (1)) (swiz z (var_ref t))\n"
-   "              (expression float - (expression float * (swiz x (var_ref arg0)) (swiz y (var_ref arg1)))\n"
-   "	                          (expression float * (swiz x (var_ref arg1)) (swiz y (var_ref arg0)))))\n"
-   "      (return (var_ref t))))\n"
+   "     ((return (expression vec3 cross (var_ref arg0) (var_ref arg1)))))\n"
    "))\n"
 };
 
diff --git a/src/glsl/builtins/110/cross b/src/glsl/builtins/110/cross
index deb2f95..24717a2 100644
--- a/src/glsl/builtins/110/cross
+++ b/src/glsl/builtins/110/cross
@@ -3,15 +3,5 @@
      (parameters
        (declare (in) vec3 arg0)
        (declare (in) vec3 arg1))
-     ((declare () vec3 t)
-      (assign (constant bool (1)) (swiz x (var_ref t))
-              (expression float - (expression float * (swiz y (var_ref arg0)) (swiz z (var_ref arg1)))
-	                          (expression float * (swiz y (var_ref arg1)) (swiz z (var_ref arg0)))))
-      (assign (constant bool (1)) (swiz y (var_ref t))
-              (expression float - (expression float * (swiz z (var_ref arg0)) (swiz x (var_ref arg1)))
-	                          (expression float * (swiz z (var_ref arg1)) (swiz x (var_ref arg0)))))
-      (assign (constant bool (1)) (swiz z (var_ref t))
-              (expression float - (expression float * (swiz x (var_ref arg0)) (swiz y (var_ref arg1)))
-	                          (expression float * (swiz x (var_ref arg1)) (swiz y (var_ref arg0)))))
-      (return (var_ref t))))
+     ((return (expression vec3 cross (var_ref arg0) (var_ref arg1)))))
 ))
diff --git a/src/glsl/ir.cpp b/src/glsl/ir.cpp
index 6d89913..fcf5dec 100644
--- a/src/glsl/ir.cpp
+++ b/src/glsl/ir.cpp
@@ -104,6 +104,7 @@ ir_expression::get_num_operands(ir_expression_operation op)
       2, /* ir_binop_logic_or */
 
       2, /* ir_binop_dot */
+      2, /* ir_binop_cross */
       2, /* ir_binop_min */
       2, /* ir_binop_max */
 
@@ -163,6 +164,7 @@ static const char *const operator_strs[] = {
    "^^",
    "||",
    "dot",
+   "cross",
    "min",
    "max",
    "pow",
diff --git a/src/glsl/ir.h b/src/glsl/ir.h
index 790173e..9d7af2d 100644
--- a/src/glsl/ir.h
+++ b/src/glsl/ir.h
@@ -609,6 +609,7 @@ enum ir_expression_operation {
    ir_binop_logic_or,
 
    ir_binop_dot,
+   ir_binop_cross,
    ir_binop_min,
    ir_binop_max,
 
diff --git a/src/glsl/ir_constant_expression.cpp b/src/glsl/ir_constant_expression.cpp
index c0fe470..ca83497 100644
--- a/src/glsl/ir_constant_expression.cpp
+++ b/src/glsl/ir_constant_expression.cpp
@@ -459,8 +459,19 @@ ir_constant_visitor::visit(ir_expression *ir)
 	    assert(0);
 	 }
       }
+      break;
 
+   case ir_binop_cross:
+      assert(op[0]->type == glsl_type::vec3_type);
+      assert(op[1]->type == glsl_type::vec3_type);
+      data.f[0] = (op[0]->value.f[1] * op[1]->value.f[2] -
+		   op[1]->value.f[1] * op[0]->value.f[2]);
+      data.f[1] = (op[0]->value.f[2] * op[1]->value.f[0] -
+		   op[1]->value.f[2] * op[0]->value.f[0]);
+      data.f[2] = (op[0]->value.f[0] * op[1]->value.f[1] -
+		   op[1]->value.f[0] * op[0]->value.f[1]);
       break;
+
    case ir_binop_add:
       assert(op[0]->type == op[1]->type || op0_scalar || op1_scalar);
       for (unsigned c = 0, c0 = 0, c1 = 0;
diff --git a/src/mesa/shader/ir_to_mesa.cpp b/src/mesa/shader/ir_to_mesa.cpp
index 6ecc6d3..f99a1fc 100644
--- a/src/mesa/shader/ir_to_mesa.cpp
+++ b/src/mesa/shader/ir_to_mesa.cpp
@@ -781,6 +781,11 @@ ir_to_mesa_visitor::visit(ir_expression *ir)
 			     op[0], op[1]);
       }
       break;
+
+   case ir_binop_cross:
+      ir_to_mesa_emit_op2(ir, OPCODE_XPD, result_dst, op[0], op[1]);
+      break;
+
    case ir_unop_sqrt:
       ir_to_mesa_emit_scalar_op1(ir, OPCODE_RSQ, result_dst, op[0]);
       ir_to_mesa_emit_scalar_op1(ir, OPCODE_RCP, result_dst, result_src);




More information about the mesa-commit mailing list