#include "ir_visitor.h"
#include "ir_optimization.h"
#include "compiler/glsl_types.h"
+#include "ir_builder.h"
+
+using namespace ir_builder;
namespace {
ir_rvalue *orig_index,
const glsl_type *type)
{
- ir_assignment *assign, *value_assign;
- ir_variable *index, *var, *value;
- ir_dereference *deref, *deref_value;
- unsigned i;
-
-
exec_list list;
+ ir_factory body(&list, base_ir);
/* Store the index to a temporary to avoid reusing its tree. */
assert(orig_index->type == glsl_type::int_type ||
orig_index->type == glsl_type::uint_type);
- index = new(base_ir) ir_variable(orig_index->type,
- "vec_index_tmp_i",
- ir_var_temporary);
- list.push_tail(index);
- deref = new(base_ir) ir_dereference_variable(index);
- assign = new(base_ir) ir_assignment(deref, orig_index, NULL);
- list.push_tail(assign);
+ ir_variable *const index =
+ body.make_temp(orig_index->type, "vec_index_tmp_i");
+
+ body.emit(assign(index, orig_index));
/* Store the value inside a temp, thus avoiding matrixes duplication */
- value = new(base_ir) ir_variable(orig_vector->type, "vec_value_tmp",
- ir_var_temporary);
- list.push_tail(value);
- deref_value = new(base_ir) ir_dereference_variable(value);
- value_assign = new(base_ir) ir_assignment(deref_value, orig_vector);
- list.push_tail(value_assign);
+ ir_variable *const value =
+ body.make_temp(orig_vector->type, "vec_value_tmp");
+
+ body.emit(assign(value, orig_vector));
+
/* Temporary where we store whichever value we swizzle out. */
- var = new(base_ir) ir_variable(type, "vec_index_tmp_v",
- ir_var_temporary);
- list.push_tail(var);
+ ir_variable *const var = body.make_temp(type, "vec_index_tmp_v");
/* Generate a single comparison condition "mask" for all of the components
* in the vector.
*/
ir_variable *const cond =
- compare_index_block(&list, index, 0,
- orig_vector->type->vector_elements,
- mem_ctx);
+ compare_index_block(body, index, 0, orig_vector->type->vector_elements);
/* Generate a conditional move of each vector element to the temp. */
- for (i = 0; i < orig_vector->type->vector_elements; i++) {
- ir_rvalue *condition_swizzle =
- new(base_ir) ir_swizzle(new(mem_ctx) ir_dereference_variable(cond),
- i, 0, 0, 0, 1);
-
- /* Just clone the rest of the deref chain when trying to get at the
- * underlying variable.
- */
- ir_rvalue *swizzle =
- new(base_ir) ir_swizzle(deref_value->clone(mem_ctx, NULL),
- i, 0, 0, 0, 1);
-
- deref = new(base_ir) ir_dereference_variable(var);
- assign = new(base_ir) ir_assignment(deref, swizzle, condition_swizzle);
- list.push_tail(assign);
- }
+ for (unsigned i = 0; i < orig_vector->type->vector_elements; i++)
+ body.emit(assign(var, swizzle(value, i, 1), swizzle(cond, i, 1)));
/* Put all of the new instructions in the IR stream before the old
* instruction.
base_ir->insert_before(&list);
this->progress = true;
- return new(base_ir) ir_dereference_variable(var);
+ return deref(var).val;
}
ir_rvalue *
{
ir_expression *const expr = ir->as_expression();
- if (expr == NULL || expr->operation != ir_binop_vector_extract)
+ if (expr == NULL)
+ return ir;
+
+ if (expr->operation == ir_unop_interpolate_at_centroid ||
+ expr->operation == ir_binop_interpolate_at_offset ||
+ expr->operation == ir_binop_interpolate_at_sample) {
+ /* Lower interpolateAtXxx(some_vec[idx], ...) to
+ * interpolateAtXxx(some_vec, ...)[idx] before lowering to conditional
+ * assignments, to maintain the rule that the interpolant is an l-value
+ * referring to a (part of a) shader input.
+ *
+ * This is required when idx is dynamic (otherwise it gets lowered to
+ * a swizzle).
+ */
+ ir_expression *const interpolant = expr->operands[0]->as_expression();
+ if (!interpolant || interpolant->operation != ir_binop_vector_extract)
+ return ir;
+
+ ir_rvalue *vec_input = interpolant->operands[0];
+ ir_expression *const vec_interpolate =
+ new(base_ir) ir_expression(expr->operation, vec_input->type,
+ vec_input, expr->operands[1]);
+
+ return convert_vec_index_to_cond_assign(ralloc_parent(ir),
+ vec_interpolate,
+ interpolant->operands[1],
+ ir->type);
+ }
+
+ if (expr->operation != ir_binop_vector_extract)
return ir;
return convert_vec_index_to_cond_assign(ralloc_parent(ir),