aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEric Anholt <[email protected]>2010-07-01 10:09:58 -0700
committerEric Anholt <[email protected]>2010-07-01 11:07:23 -0700
commit8a1f186cc55979bb9df0a88b48da8d81460c3e7c (patch)
tree7a39126f77b75f94c09f441fdbca6c2be57d34cb
parent9acf618f24428eba72650c0e328e7ed52986728e (diff)
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.
-rw-r--r--src/glsl/Makefile1
-rw-r--r--src/glsl/ir.h9
-rw-r--r--src/glsl/ir_hierarchical_visitor.cpp7
-rw-r--r--src/glsl/ir_hierarchical_visitor.h12
-rw-r--r--src/glsl/ir_hv_accept.cpp5
-rw-r--r--src/glsl/ir_mod_to_fract.cpp89
-rw-r--r--src/glsl/ir_optimization.h1
-rw-r--r--src/mesa/shader/ir_to_mesa.cpp3
8 files changed, 120 insertions, 7 deletions
diff --git a/src/glsl/Makefile b/src/glsl/Makefile
index f4e32b91857..a709bf7121e 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 f47813786b5..c19bd417c33 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 9afb12a4a2b..d475df62fce 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 2c4590d4b10..afa780dc912 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 7b5cc5234c5..e772018a458 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 00000000000..ec1e65092d9
--- /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 147f92176bf..1a8b740566b 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 2f2096ef97b..25267d79b53 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;