glsl2: Add a pass to convert mod(a, b) to b * fract(a/b).
authorEric Anholt <eric@anholt.net>
Thu, 1 Jul 2010 17:09:58 +0000 (10:09 -0700)
committerEric Anholt <eric@anholt.net>
Thu, 1 Jul 2010 18:07:23 +0000 (11:07 -0700)
This is used by the Mesa IR backend to implement mod, fixing glsl-fs-mod.

src/glsl/Makefile
src/glsl/ir.h
src/glsl/ir_hierarchical_visitor.cpp
src/glsl/ir_hierarchical_visitor.h
src/glsl/ir_hv_accept.cpp
src/glsl/ir_mod_to_fract.cpp [new file with mode: 0644]
src/glsl/ir_optimization.h
src/mesa/shader/ir_to_mesa.cpp

index f4e32b918572f60b8cc9e6f722f9e8ba2d047c1f..a709bf7121ea8b2c2d1570d8cbb2deac5656da92 100644 (file)
@@ -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 \
index f47813786b508f913316e6b42bc7802693776c76..c19bd417c331a926bf2302833801042c4909870a 100644 (file)
@@ -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,
 
    /**
index 9afb12a4a2bff6aab9da72c80b8657141d99d5e4..d475df62fce3dddaf35bd8d2809fae5b558a602b 100644 (file)
@@ -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);
 }
 
 
index 2c4590d4b10de2f6f02e2d0a9ad5fbf1dde18ea9..afa780dc91211b19b34ad08fe0a897bba2b031a9 100644 (file)
@@ -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 */
index 7b5cc5234c50b1809f0010fd99b2a0927f170957..e772018a45890e2302f14ea22752e2c48aec263d 100644 (file)
  * 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 (file)
index 0000000..ec1e650
--- /dev/null
@@ -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;
+}
index 147f92176bf5c2a72af34109dd8e0d133eefb9c8..1a8b740566b9ec8b3fe82ccf32f6fc9bdc5ddd96 100644 (file)
@@ -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);
index 2f2096ef97b24f4d61feea2bae1c856beb4b5b16..25267d79b530b8f8dc2b2d69b5fb4897e0cfc5cd 100644 (file)
@@ -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;