2 * Copyright © 2010 Luca Barbieri
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
25 * \file lower_variable_index_to_cond_assign.cpp
27 * Turns non-constant indexing into array types to a series of
28 * conditional moves of each element into a temporary.
30 * Pre-DX10 GPUs often don't have a native way to do this operation,
31 * and this works around that.
35 #include "ir_rvalue_visitor.h"
36 #include "ir_optimization.h"
37 #include "glsl_types.h"
38 #include "main/macros.h"
40 struct assignment_generator
42 ir_instruction
* base_ir
;
45 unsigned int write_mask
;
48 assignment_generator()
52 void generate(unsigned i
, ir_rvalue
* condition
, exec_list
*list
) const
54 /* Just clone the rest of the deref chain when trying to get at the
55 * underlying variable.
57 void *mem_ctx
= talloc_parent(base_ir
);
58 ir_dereference
*element
=
59 new(mem_ctx
) ir_dereference_array(this->array
->clone(mem_ctx
, NULL
),
60 new(mem_ctx
) ir_constant(i
));
61 ir_rvalue
*variable
= new(mem_ctx
) ir_dereference_variable(this->var
);
63 ir_assignment
*assignment
;
65 assignment
= new(mem_ctx
) ir_assignment(element
, variable
, condition
,
68 assignment
= new(mem_ctx
) ir_assignment(variable
, element
, condition
);
71 list
->push_tail(assignment
);
75 struct switch_generator
77 /* make TFunction a template parameter if you need to use other generators */
78 typedef assignment_generator TFunction
;
79 const TFunction
& generator
;
82 unsigned linear_sequence_max_length
;
83 unsigned condition_components
;
87 switch_generator(const TFunction
& generator
, ir_variable
*index
,
88 unsigned linear_sequence_max_length
,
89 unsigned condition_components
)
90 : generator(generator
), index(index
),
91 linear_sequence_max_length(linear_sequence_max_length
),
92 condition_components(condition_components
)
94 this->mem_ctx
= talloc_parent(index
);
97 void linear_sequence(unsigned begin
, unsigned end
, exec_list
*list
)
102 /* If the array access is a read, read the first element of this subregion
103 * unconditionally. The remaining tests will possibly overwrite this
104 * value with one of the other array elements.
106 * This optimization cannot be done for writes because it will cause the
107 * first element of the subregion to be written possibly *in addition* to
108 * one of the other elements.
111 if (!this->generator
.is_write
) {
112 this->generator
.generate(begin
, 0, list
);
118 for (unsigned i
= first
; i
< end
; i
+= 4) {
119 const unsigned comps
= MIN2(condition_components
, end
- i
);
121 ir_rvalue
*broadcast_index
=
122 new(this->mem_ctx
) ir_dereference_variable(index
);
125 const ir_swizzle_mask m
= { 0, 0, 0, 0, comps
, false };
126 broadcast_index
= new(this->mem_ctx
) ir_swizzle(broadcast_index
, m
);
129 /* Compare the desired index value with the next block of four indices.
131 ir_constant_data test_indices_data
;
132 memset(&test_indices_data
, 0, sizeof(test_indices_data
));
133 test_indices_data
.i
[0] = i
;
134 test_indices_data
.i
[1] = i
+ 1;
135 test_indices_data
.i
[2] = i
+ 2;
136 test_indices_data
.i
[3] = i
+ 3;
137 ir_constant
*const test_indices
=
138 new(this->mem_ctx
) ir_constant(broadcast_index
->type
,
141 ir_rvalue
*const condition_val
=
142 new(this->mem_ctx
) ir_expression(ir_binop_equal
,
143 &glsl_type::bool_type
[comps
- 1],
147 ir_variable
*const condition
=
148 new(this->mem_ctx
) ir_variable(condition_val
->type
,
149 "dereference_array_condition",
151 list
->push_tail(condition
);
153 ir_rvalue
*const cond_deref
=
154 new(this->mem_ctx
) ir_dereference_variable(condition
);
155 list
->push_tail(new(this->mem_ctx
) ir_assignment(cond_deref
,
159 ir_rvalue
*const cond_deref
=
160 new(this->mem_ctx
) ir_dereference_variable(condition
);
162 this->generator
.generate(i
, cond_deref
, list
);
164 for (unsigned j
= 0; j
< comps
; j
++) {
165 ir_rvalue
*const cond_deref
=
166 new(this->mem_ctx
) ir_dereference_variable(condition
);
167 ir_rvalue
*const cond_swiz
=
168 new(this->mem_ctx
) ir_swizzle(cond_deref
, j
, 0, 0, 0, 1);
170 this->generator
.generate(i
+ j
, cond_swiz
, list
);
176 void bisect(unsigned begin
, unsigned end
, exec_list
*list
)
178 unsigned middle
= (begin
+ end
) >> 1;
180 assert(index
->type
->is_integer());
182 ir_constant
*const middle_c
= (index
->type
->base_type
== GLSL_TYPE_UINT
)
183 ? new(this->mem_ctx
) ir_constant((unsigned)middle
)
184 : new(this->mem_ctx
) ir_constant((int)middle
);
187 ir_dereference_variable
*deref
=
188 new(this->mem_ctx
) ir_dereference_variable(this->index
);
190 ir_expression
*less
=
191 new(this->mem_ctx
) ir_expression(ir_binop_less
, glsl_type::bool_type
,
194 ir_if
*if_less
= new(this->mem_ctx
) ir_if(less
);
196 generate(begin
, middle
, &if_less
->then_instructions
);
197 generate(middle
, end
, &if_less
->else_instructions
);
199 list
->push_tail(if_less
);
202 void generate(unsigned begin
, unsigned end
, exec_list
*list
)
204 unsigned length
= end
- begin
;
205 if (length
<= this->linear_sequence_max_length
)
206 return linear_sequence(begin
, end
, list
);
208 return bisect(begin
, end
, list
);
213 * Visitor class for replacing expressions with ir_constant values.
216 class variable_index_to_cond_assign_visitor
: public ir_rvalue_visitor
{
218 variable_index_to_cond_assign_visitor(bool lower_input
,
223 this->progress
= false;
224 this->lower_inputs
= lower_input
;
225 this->lower_outputs
= lower_output
;
226 this->lower_temps
= lower_temp
;
227 this->lower_uniforms
= lower_uniform
;
236 bool is_array_or_matrix(const ir_instruction
*ir
) const
238 return (ir
->type
->is_array() || ir
->type
->is_matrix());
241 bool needs_lowering(ir_dereference_array
*deref
) const
243 if (deref
== NULL
|| deref
->array_index
->as_constant()
244 || !is_array_or_matrix(deref
->array
))
247 if (deref
->array
->ir_type
== ir_type_constant
)
248 return this->lower_temps
;
250 const ir_variable
*const var
= deref
->array
->variable_referenced();
253 case ir_var_temporary
:
254 return this->lower_temps
;
256 return this->lower_uniforms
;
258 return (var
->location
== -1) ? this->lower_temps
: this->lower_inputs
;
260 return (var
->location
== -1) ? this->lower_temps
: this->lower_outputs
;
262 return this->lower_temps
;
265 assert(!"Should not get here.");
269 ir_variable
*convert_dereference_array(ir_dereference_array
*orig_deref
,
270 ir_assignment
* orig_assign
)
272 assert(is_array_or_matrix(orig_deref
->array
));
274 const unsigned length
= (orig_deref
->array
->type
->is_array())
275 ? orig_deref
->array
->type
->length
276 : orig_deref
->array
->type
->matrix_columns
;
278 void *const mem_ctx
= talloc_parent(base_ir
);
280 /* Temporary storage for either the result of the dereference of
281 * the array, or the RHS that's being assigned into the
282 * dereference of the array.
287 var
= new(mem_ctx
) ir_variable(orig_assign
->rhs
->type
,
288 "dereference_array_value",
290 base_ir
->insert_before(var
);
292 ir_dereference
*lhs
= new(mem_ctx
) ir_dereference_variable(var
);
293 ir_assignment
*assign
= new(mem_ctx
) ir_assignment(lhs
,
297 base_ir
->insert_before(assign
);
299 var
= new(mem_ctx
) ir_variable(orig_deref
->type
,
300 "dereference_array_value",
302 base_ir
->insert_before(var
);
305 /* Store the index to a temporary to avoid reusing its tree. */
307 new(mem_ctx
) ir_variable(orig_deref
->array_index
->type
,
308 "dereference_array_index", ir_var_temporary
);
309 base_ir
->insert_before(index
);
311 ir_dereference
*lhs
= new(mem_ctx
) ir_dereference_variable(index
);
312 ir_assignment
*assign
=
313 new(mem_ctx
) ir_assignment(lhs
, orig_deref
->array_index
, NULL
);
314 base_ir
->insert_before(assign
);
316 assignment_generator ag
;
317 ag
.array
= orig_deref
->array
;
318 ag
.base_ir
= base_ir
;
322 ag
.write_mask
= orig_assign
->write_mask
;
327 switch_generator
sg(ag
, index
, 4, 4);
330 sg
.generate(0, length
, &list
);
331 base_ir
->insert_before(&list
);
336 virtual void handle_rvalue(ir_rvalue
**pir
)
341 ir_dereference_array
* orig_deref
= (*pir
)->as_dereference_array();
342 if (needs_lowering(orig_deref
)) {
343 ir_variable
* var
= convert_dereference_array(orig_deref
, 0);
345 *pir
= new(talloc_parent(base_ir
)) ir_dereference_variable(var
);
346 this->progress
= true;
351 visit_leave(ir_assignment
*ir
)
353 ir_rvalue_visitor::visit_leave(ir
);
355 ir_dereference_array
*orig_deref
= ir
->lhs
->as_dereference_array();
357 if (needs_lowering(orig_deref
)) {
358 convert_dereference_array(orig_deref
, ir
);
360 this->progress
= true;
363 return visit_continue
;
368 lower_variable_index_to_cond_assign(exec_list
*instructions
,
374 variable_index_to_cond_assign_visitor
v(lower_input
,
379 visit_list_elements(&v
, instructions
);