Mesa (glsl2): glsl2: Add a pass to convert mod(a, b) to b * fract(a/b).

Eric Anholt anholt at kemper.freedesktop.org
Thu Jul 1 18:27:50 UTC 2010


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

Author: Eric Anholt <eric at anholt.net>
Date:   Thu Jul  1 10:09:58 2010 -0700

glsl2: Add a pass to convert mod(a, b) to b * fract(a/b).

This is used by the Mesa IR backend to implement mod, fixing glsl-fs-mod.

---

 src/glsl/Makefile                    |    1 +
 src/glsl/ir.h                        |    9 ++++
 src/glsl/ir_hierarchical_visitor.cpp |    7 +--
 src/glsl/ir_hierarchical_visitor.h   |   12 +++++
 src/glsl/ir_hv_accept.cpp            |    5 ++-
 src/glsl/ir_mod_to_fract.cpp         |   89 ++++++++++++++++++++++++++++++++++
 src/glsl/ir_optimization.h           |    1 +
 src/mesa/shader/ir_to_mesa.cpp       |    3 +
 8 files changed, 120 insertions(+), 7 deletions(-)

diff --git a/src/glsl/Makefile b/src/glsl/Makefile
index f4e32b9..a709bf7 100644
--- a/src/glsl/Makefile
+++ b/src/glsl/Makefile
@@ -40,6 +40,7 @@ CXX_SOURCES = \
 	ir_hierarchical_visitor.cpp \
 	ir_hv_accept.cpp \
 	ir_if_simplification.cpp \
+	ir_mod_to_fract.cpp \
 	ir_print_visitor.cpp \
 	ir_reader.cpp \
 	ir_swizzle_swizzle.cpp \
diff --git a/src/glsl/ir.h b/src/glsl/ir.h
index f478137..c19bd41 100644
--- a/src/glsl/ir.h
+++ b/src/glsl/ir.h
@@ -551,6 +551,15 @@ enum ir_expression_operation {
    ir_binop_sub,
    ir_binop_mul,
    ir_binop_div,
+
+   /**
+    * Takes one of two combinations of arguments:
+    *
+    * - mod(vecN, vecN)
+    * - mod(vecN, float)
+    *
+    * Does not take integer types.
+    */
    ir_binop_mod,
 
    /**
diff --git a/src/glsl/ir_hierarchical_visitor.cpp b/src/glsl/ir_hierarchical_visitor.cpp
index 9afb12a..d475df6 100644
--- a/src/glsl/ir_hierarchical_visitor.cpp
+++ b/src/glsl/ir_hierarchical_visitor.cpp
@@ -277,12 +277,7 @@ ir_hierarchical_visitor::visit_leave(ir_if *ir)
 void
 ir_hierarchical_visitor::run(exec_list *instructions)
 {
-   foreach_list(n, instructions) {
-      ir_instruction *ir = (ir_instruction *) n;
-
-      if (ir->accept(this) != visit_continue)
-	 break;
-   }
+   visit_list_elements(this, instructions);
 }
 
 
diff --git a/src/glsl/ir_hierarchical_visitor.h b/src/glsl/ir_hierarchical_visitor.h
index 2c4590d..afa780d 100644
--- a/src/glsl/ir_hierarchical_visitor.h
+++ b/src/glsl/ir_hierarchical_visitor.h
@@ -141,6 +141,16 @@ public:
     */
    void run(struct exec_list *instructions);
 
+   /* Some visitors may need to insert new variable declarations and
+    * assignments for portions of a subtree, which means they need a
+    * pointer to the current instruction in the stream, not just their
+    * node in the tree rooted at that instruction.
+    *
+    * This is implemented by visit_list_elements -- if the visitor is
+    * not called by it, nothing good will happen.
+    */
+   class ir_instruction *base_ir;
+
    /**
     * Callback function that is invoked on entry to each node visited.
     *
@@ -161,4 +171,6 @@ void visit_tree(ir_instruction *ir,
 		void (*callback)(class ir_instruction *ir, void *data),
 		void *data);
 
+ir_visitor_status visit_list_elements(ir_hierarchical_visitor *v, exec_list *l);
+
 #endif /* IR_HIERARCHICAL_VISITOR_H */
diff --git a/src/glsl/ir_hv_accept.cpp b/src/glsl/ir_hv_accept.cpp
index 7b5cc52..e772018 100644
--- a/src/glsl/ir_hv_accept.cpp
+++ b/src/glsl/ir_hv_accept.cpp
@@ -37,20 +37,23 @@
  * from list.  However, if nodes are added to the list after the node being
  * processed, some of the added noded may not be processed.
  */
-static ir_visitor_status
+ir_visitor_status
 visit_list_elements(ir_hierarchical_visitor *v, exec_list *l)
 {
    exec_node *next;
+   ir_instruction *prev_base_ir = v->base_ir;
 
    for (exec_node *n = l->head; n->next != NULL; n = next) {
       next = n->next;
 
       ir_instruction *const ir = (ir_instruction *) n;
+      v->base_ir = ir;
       ir_visitor_status s = ir->accept(v);
 
       if (s != visit_continue)
 	 return s;
    }
+   v->base_ir = prev_base_ir;
 
    return visit_continue;
 }
diff --git a/src/glsl/ir_mod_to_fract.cpp b/src/glsl/ir_mod_to_fract.cpp
new file mode 100644
index 0000000..ec1e650
--- /dev/null
+++ b/src/glsl/ir_mod_to_fract.cpp
@@ -0,0 +1,89 @@
+/*
+ * 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_mod_to_floor.cpp
+ *
+ * Breaks an ir_unop_mod expression down to (op1 * fract(op0 / op1))
+ *
+ * Many GPUs don't have a MOD instruction (945 and 965 included), and
+ * if we have to break it down like this anyway, it gives an
+ * opportunity to do things like constant fold the (1.0 / op1) easily.
+ */
+
+#include "ir.h"
+
+class ir_mod_to_fract_visitor : public ir_hierarchical_visitor {
+public:
+   ir_mod_to_fract_visitor()
+   {
+      this->made_progress = false;
+   }
+
+   ir_visitor_status visit_leave(ir_expression *);
+
+   bool made_progress;
+};
+
+bool
+do_mod_to_fract(exec_list *instructions)
+{
+   ir_mod_to_fract_visitor v;
+
+   visit_list_elements(&v, instructions);
+   return v.made_progress;
+}
+
+ir_visitor_status
+ir_mod_to_fract_visitor::visit_leave(ir_expression *ir)
+{
+   if (ir->operation != ir_binop_mod)
+      return visit_continue;
+
+   ir_variable *temp = new(ir) ir_variable(ir->operands[1]->type, "mod_b");
+   this->base_ir->insert_before(temp);
+
+   ir_assignment *assign;
+   ir_rvalue *expr;
+
+   assign = new(ir) ir_assignment(new(ir) ir_dereference_variable(temp),
+				  ir->operands[1], NULL);
+   this->base_ir->insert_before(assign);
+
+   expr = new(ir) ir_expression(ir_binop_div,
+				ir->operands[0]->type,
+				ir->operands[0],
+				new(ir) ir_dereference_variable(temp));
+
+   expr = new(ir) ir_expression(ir_unop_fract,
+				ir->operands[0]->type,
+				expr,
+				NULL);
+
+   ir->operation = ir_binop_mul;
+   ir->operands[0] = new(ir) ir_dereference_variable(temp);
+   ir->operands[1] = expr;
+   this->made_progress = true;
+
+   return visit_continue;
+}
diff --git a/src/glsl/ir_optimization.h b/src/glsl/ir_optimization.h
index 147f921..1a8b740 100644
--- a/src/glsl/ir_optimization.h
+++ b/src/glsl/ir_optimization.h
@@ -39,5 +39,6 @@ bool do_dead_code_unlinked(struct _mesa_glsl_parse_state *state,
 			   exec_list *instructions);
 bool do_function_inlining(exec_list *instructions);
 bool do_if_simplification(exec_list *instructions);
+bool do_mod_to_fract(exec_list *instructions);
 bool do_swizzle_swizzle(exec_list *instructions);
 bool do_vec_index_to_swizzle(exec_list *instructions);
diff --git a/src/mesa/shader/ir_to_mesa.cpp b/src/mesa/shader/ir_to_mesa.cpp
index 2f2096e..25267d7 100644
--- a/src/mesa/shader/ir_to_mesa.cpp
+++ b/src/mesa/shader/ir_to_mesa.cpp
@@ -1724,6 +1724,9 @@ _mesa_glsl_compile_shader(GLcontext *ctx, struct gl_shader *shader)
    if (!state->error && !state->translation_unit.is_empty())
       _mesa_ast_to_hir(shader->ir, state);
 
+   /* Lowering */
+   do_mod_to_fract(shader->ir);
+
    /* Optimization passes */
    if (!state->error && !shader->ir->is_empty()) {
       bool progress;




More information about the mesa-commit mailing list