glsl2: Add a pass to convert exp and log to exp2 and log2.
authorEric Anholt <eric@anholt.net>
Thu, 5 Aug 2010 22:22:05 +0000 (15:22 -0700)
committerEric Anholt <eric@anholt.net>
Thu, 5 Aug 2010 22:34:00 +0000 (15:34 -0700)
Fixes ir_to_mesa handling of unop_log, which used the weird ARB_vp LOG
opcode that doesn't do what we want.  This also lets the multiplication
coefficients in there get constant-folded, possibly.

Fixes:
glsl-fs-log

src/glsl/Makefile
src/glsl/ir.h
src/glsl/ir_explog_to_explog2.cpp [new file with mode: 0644]
src/glsl/ir_optimization.h
src/glsl/ir_validate.cpp
src/glsl/linker.cpp
src/mesa/program/ir_to_mesa.cpp

index 53567508a01496bd38f8d9d07dfa6b54382156b5..752e60a79f6423f3bbfe6ee33c3d23ed10a9f01f 100644 (file)
@@ -41,6 +41,7 @@ CXX_SOURCES = \
        ir_dead_code_local.cpp \
        ir_dead_functions.cpp \
        ir_div_to_mul_rcp.cpp \
+       ir_explog_to_explog2.cpp \
        ir_expression_flattening.cpp \
        ir_function_can_inline.cpp \
        ir_function.cpp \
index ef8339ce1997ab69a827cdd0513a7e550a824dd5..5dc3c6b918606651df32c3ab8ea6bc49aca486fb 100644 (file)
@@ -593,8 +593,8 @@ enum ir_expression_operation {
    ir_unop_rcp,
    ir_unop_rsq,
    ir_unop_sqrt,
-   ir_unop_exp,
-   ir_unop_log,
+   ir_unop_exp,      /**< Log base e on gentype */
+   ir_unop_log,             /**< Natural log on gentype */
    ir_unop_exp2,
    ir_unop_log2,
    ir_unop_f2i,      /**< Float-to-integer conversion. */
diff --git a/src/glsl/ir_explog_to_explog2.cpp b/src/glsl/ir_explog_to_explog2.cpp
new file mode 100644 (file)
index 0000000..4fe1daa
--- /dev/null
@@ -0,0 +1,85 @@
+/*
+ * Copyright © 2010 Intel Corporation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+
+/**
+ * \file ir_explog_to_explog2.cpp
+ *
+ * Many GPUs don't have a base e log or exponent instruction, but they
+ * do have base 2 versions, so this pass converts exp and log to exp2
+ * and log2 operations.
+ */
+
+#include <math.h>
+#include "ir.h"
+#include "glsl_types.h"
+
+class ir_explog_to_explog2_visitor : public ir_hierarchical_visitor {
+public:
+   ir_explog_to_explog2_visitor()
+   {
+      this->progress = false;
+   }
+
+   ir_visitor_status visit_leave(ir_expression *);
+
+   bool progress;
+};
+
+bool
+do_explog_to_explog2(exec_list *instructions)
+{
+   ir_explog_to_explog2_visitor v;
+
+   visit_list_elements(&v, instructions);
+   return v.progress;
+}
+
+ir_visitor_status
+ir_explog_to_explog2_visitor::visit_leave(ir_expression *ir)
+{
+   if (ir->operation == ir_unop_exp) {
+      void *mem_ctx = talloc_parent(ir);
+      ir_constant *log2_e = new(mem_ctx) ir_constant(log2f(M_E));
+
+      ir->operation = ir_unop_exp2;
+      ir->operands[0] = new(mem_ctx) ir_expression(ir_binop_mul,
+                                                  ir->operands[0]->type,
+                                                  ir->operands[0],
+                                                  log2_e);
+      this->progress = true;
+   }
+
+   if (ir->operation == ir_unop_log) {
+      void *mem_ctx = talloc_parent(ir);
+
+      ir->operation = ir_binop_mul;
+      ir->operands[0] = new(mem_ctx) ir_expression(ir_unop_log2,
+                                                  ir->operands[0]->type,
+                                                  ir->operands[0],
+                                                  NULL);
+      ir->operands[1] = new(mem_ctx) ir_constant(1.0f / log2f(M_E));
+      this->progress = true;
+   }
+
+   return visit_continue;
+}
index eac28dc64cb5b6e347a606ad5bad9bd4f231e56b..c6e7beb447fa275cf15ddad2800c3441b1ea37d9 100644 (file)
@@ -38,6 +38,7 @@ bool do_dead_code_local(exec_list *instructions);
 bool do_dead_code_unlinked(exec_list *instructions);
 bool do_dead_functions(exec_list *instructions);
 bool do_div_to_mul_rcp(exec_list *instructions);
+bool do_explog_to_explog2(exec_list *instructions);
 bool do_function_inlining(exec_list *instructions);
 bool do_if_return(exec_list *instructions);
 bool do_if_simplification(exec_list *instructions);
index 701bf21ea61388412789830ee0dec37525e741fe..545fe2799f3faf65055a16eb3cbe23196d841cd3 100644 (file)
@@ -183,10 +183,14 @@ ir_validate::visit_leave(ir_expression *ir)
    case ir_unop_rcp:
    case ir_unop_rsq:
    case ir_unop_sqrt:
+      assert(ir->type == ir->operands[0]->type);
+      break;
+
    case ir_unop_exp:
    case ir_unop_log:
    case ir_unop_exp2:
    case ir_unop_log2:
+      assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT);
       assert(ir->type == ir->operands[0]->type);
       break;
 
index 050116954a14449d81afd0a7f9e7d52ae4a0bed6..9d6de242f5f4257da49629141247eff1761503e8 100644 (file)
@@ -1281,6 +1281,7 @@ link_shaders(struct gl_shader_program *prog)
       do_mat_op_to_vec(ir);
       do_mod_to_fract(ir);
       do_div_to_mul_rcp(ir);
+      do_explog_to_explog2(ir);
 
       do {
         progress = false;
index 299b11d27416d41b1a53eef8b260d8a16dec4665..26fbc4349ab791271b38c60d2f9ee418c7b14401 100644 (file)
@@ -752,15 +752,12 @@ ir_to_mesa_visitor::visit(ir_expression *ir)
       ir_to_mesa_emit_scalar_op1(ir, OPCODE_RCP, result_dst, op[0]);
       break;
 
-   case ir_unop_exp:
-      ir_to_mesa_emit_scalar_op2(ir, OPCODE_POW, result_dst,
-                                src_reg_for_float(M_E), op[0]);
-      break;
    case ir_unop_exp2:
       ir_to_mesa_emit_scalar_op1(ir, OPCODE_EX2, result_dst, op[0]);
       break;
+   case ir_unop_exp:
    case ir_unop_log:
-      ir_to_mesa_emit_scalar_op1(ir, OPCODE_LOG, result_dst, op[0]);
+      assert(!"not reached: should be handled by ir_explog_to_explog2");
       break;
    case ir_unop_log2:
       ir_to_mesa_emit_scalar_op1(ir, OPCODE_LG2, result_dst, op[0]);