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.
This commit is contained in:
Eric Anholt
2010-07-01 10:09:58 -07:00
parent 9acf618f24
commit 8a1f186cc5
8 changed files with 120 additions and 7 deletions

View 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 \

View 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,
/**

View 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);
}

View 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 */

View File

@@ -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;
}

View File

@@ -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;
}

View 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);

View 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;