glsl: Populate built-in types correctly for GLSL 3.00 ES.
[mesa.git] / src / glsl / lower_vec_index_to_cond_assign.cpp
1 /*
2 * Copyright © 2010 Intel Corporation
3 *
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:
10 *
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
13 * Software.
14 *
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.
22 */
23
24 /**
25 * \file lower_vec_index_to_cond_assign.cpp
26 *
27 * Turns indexing into vector types to a series of conditional moves
28 * of each channel's swizzle into a temporary.
29 *
30 * Most GPUs don't have a native way to do this operation, and this
31 * works around that. For drivers using both this pass and
32 * ir_vec_index_to_swizzle, there's a risk that this pass will happen
33 * before sufficient constant folding to find that the array index is
34 * constant. However, we hope that other optimization passes,
35 * particularly constant folding of assignment conditions and copy
36 * propagation, will result in the same code in the end.
37 */
38
39 #include "ir.h"
40 #include "ir_visitor.h"
41 #include "ir_optimization.h"
42 #include "glsl_types.h"
43
44 /**
45 * Visitor class for replacing expressions with ir_constant values.
46 */
47
48 class ir_vec_index_to_cond_assign_visitor : public ir_hierarchical_visitor {
49 public:
50 ir_vec_index_to_cond_assign_visitor()
51 {
52 progress = false;
53 }
54
55 ir_rvalue *convert_vec_index_to_cond_assign(ir_rvalue *val);
56
57 virtual ir_visitor_status visit_enter(ir_expression *);
58 virtual ir_visitor_status visit_enter(ir_swizzle *);
59 virtual ir_visitor_status visit_leave(ir_assignment *);
60 virtual ir_visitor_status visit_enter(ir_return *);
61 virtual ir_visitor_status visit_enter(ir_call *);
62 virtual ir_visitor_status visit_enter(ir_if *);
63
64 bool progress;
65 };
66
67 ir_rvalue *
68 ir_vec_index_to_cond_assign_visitor::convert_vec_index_to_cond_assign(ir_rvalue *ir)
69 {
70 ir_dereference_array *orig_deref = ir->as_dereference_array();
71 ir_assignment *assign, *value_assign;
72 ir_variable *index, *var, *value;
73 ir_dereference *deref, *deref_value;
74 unsigned i;
75
76 if (!orig_deref)
77 return ir;
78
79 if (orig_deref->array->type->is_matrix() ||
80 orig_deref->array->type->is_array())
81 return ir;
82
83 void *mem_ctx = ralloc_parent(ir);
84
85 assert(orig_deref->array_index->type->base_type == GLSL_TYPE_INT);
86
87 exec_list list;
88
89 /* Store the index to a temporary to avoid reusing its tree. */
90 index = new(base_ir) ir_variable(glsl_type::int_type,
91 "vec_index_tmp_i",
92 ir_var_temporary);
93 list.push_tail(index);
94 deref = new(base_ir) ir_dereference_variable(index);
95 assign = new(base_ir) ir_assignment(deref, orig_deref->array_index, NULL);
96 list.push_tail(assign);
97
98 /* Store the value inside a temp, thus avoiding matrixes duplication */
99 value = new(base_ir) ir_variable(orig_deref->array->type, "vec_value_tmp",
100 ir_var_temporary);
101 list.push_tail(value);
102 deref_value = new(base_ir) ir_dereference_variable(value);
103 value_assign = new(base_ir) ir_assignment(deref_value, orig_deref->array);
104 list.push_tail(value_assign);
105
106 /* Temporary where we store whichever value we swizzle out. */
107 var = new(base_ir) ir_variable(ir->type, "vec_index_tmp_v",
108 ir_var_temporary);
109 list.push_tail(var);
110
111 /* Generate a single comparison condition "mask" for all of the components
112 * in the vector.
113 */
114 ir_rvalue *const cond_deref =
115 compare_index_block(&list, index, 0,
116 orig_deref->array->type->vector_elements,
117 mem_ctx);
118
119 /* Generate a conditional move of each vector element to the temp. */
120 for (i = 0; i < orig_deref->array->type->vector_elements; i++) {
121 ir_rvalue *condition_swizzle =
122 new(base_ir) ir_swizzle(cond_deref->clone(ir, NULL), i, 0, 0, 0, 1);
123
124 /* Just clone the rest of the deref chain when trying to get at the
125 * underlying variable.
126 */
127 ir_rvalue *swizzle =
128 new(base_ir) ir_swizzle(deref_value->clone(mem_ctx, NULL),
129 i, 0, 0, 0, 1);
130
131 deref = new(base_ir) ir_dereference_variable(var);
132 assign = new(base_ir) ir_assignment(deref, swizzle, condition_swizzle);
133 list.push_tail(assign);
134 }
135
136 /* Put all of the new instructions in the IR stream before the old
137 * instruction.
138 */
139 base_ir->insert_before(&list);
140
141 this->progress = true;
142 return new(base_ir) ir_dereference_variable(var);
143 }
144
145 ir_visitor_status
146 ir_vec_index_to_cond_assign_visitor::visit_enter(ir_expression *ir)
147 {
148 unsigned int i;
149
150 for (i = 0; i < ir->get_num_operands(); i++) {
151 ir->operands[i] = convert_vec_index_to_cond_assign(ir->operands[i]);
152 }
153
154 return visit_continue;
155 }
156
157 ir_visitor_status
158 ir_vec_index_to_cond_assign_visitor::visit_enter(ir_swizzle *ir)
159 {
160 /* Can't be hit from normal GLSL, since you can't swizzle a scalar (which
161 * the result of indexing a vector is. But maybe at some point we'll end up
162 * using swizzling of scalars for vector construction.
163 */
164 ir->val = convert_vec_index_to_cond_assign(ir->val);
165
166 return visit_continue;
167 }
168
169 ir_visitor_status
170 ir_vec_index_to_cond_assign_visitor::visit_leave(ir_assignment *ir)
171 {
172 ir_variable *index, *var;
173 ir_dereference_variable *deref;
174 ir_assignment *assign;
175 unsigned i;
176
177 ir->rhs = convert_vec_index_to_cond_assign(ir->rhs);
178 if (ir->condition)
179 ir->condition = convert_vec_index_to_cond_assign(ir->condition);
180
181 /* Last, handle the LHS */
182 ir_dereference_array *orig_deref = ir->lhs->as_dereference_array();
183
184 if (!orig_deref ||
185 orig_deref->array->type->is_matrix() ||
186 orig_deref->array->type->is_array())
187 return visit_continue;
188
189 void *mem_ctx = ralloc_parent(ir);
190
191 assert(orig_deref->array_index->type->base_type == GLSL_TYPE_INT);
192
193 exec_list list;
194
195 /* Store the index to a temporary to avoid reusing its tree. */
196 index = new(ir) ir_variable(glsl_type::int_type, "vec_index_tmp_i",
197 ir_var_temporary);
198 list.push_tail(index);
199 deref = new(ir) ir_dereference_variable(index);
200 assign = new(ir) ir_assignment(deref, orig_deref->array_index, NULL);
201 list.push_tail(assign);
202
203 /* Store the RHS to a temporary to avoid reusing its tree. */
204 var = new(ir) ir_variable(ir->rhs->type, "vec_index_tmp_v",
205 ir_var_temporary);
206 list.push_tail(var);
207 deref = new(ir) ir_dereference_variable(var);
208 assign = new(ir) ir_assignment(deref, ir->rhs, NULL);
209 list.push_tail(assign);
210
211 /* Generate a single comparison condition "mask" for all of the components
212 * in the vector.
213 */
214 ir_rvalue *const cond_deref =
215 compare_index_block(&list, index, 0,
216 orig_deref->array->type->vector_elements,
217 mem_ctx);
218
219 /* Generate a conditional move of each vector element to the temp. */
220 for (i = 0; i < orig_deref->array->type->vector_elements; i++) {
221 ir_rvalue *condition_swizzle =
222 new(ir) ir_swizzle(cond_deref->clone(ir, NULL), i, 0, 0, 0, 1);
223
224
225 /* Just clone the rest of the deref chain when trying to get at the
226 * underlying variable.
227 */
228 ir_rvalue *swizzle =
229 new(ir) ir_swizzle(orig_deref->array->clone(mem_ctx, NULL),
230 i, 0, 0, 0, 1);
231
232 deref = new(ir) ir_dereference_variable(var);
233 assign = new(ir) ir_assignment(swizzle, deref, condition_swizzle);
234 list.push_tail(assign);
235 }
236
237 /* If the original assignment has a condition, respect that original
238 * condition! This is acomplished by wrapping the new conditional
239 * assignments in an if-statement that uses the original condition.
240 */
241 if (ir->condition != NULL) {
242 /* No need to clone the condition because the IR that it hangs on is
243 * going to be removed from the instruction sequence.
244 */
245 ir_if *if_stmt = new(mem_ctx) ir_if(ir->condition);
246
247 list.move_nodes_to(&if_stmt->then_instructions);
248 ir->insert_before(if_stmt);
249 } else {
250 ir->insert_before(&list);
251 }
252
253 ir->remove();
254
255 this->progress = true;
256
257 return visit_continue;
258 }
259
260 ir_visitor_status
261 ir_vec_index_to_cond_assign_visitor::visit_enter(ir_call *ir)
262 {
263 foreach_iter(exec_list_iterator, iter, *ir) {
264 ir_rvalue *param = (ir_rvalue *)iter.get();
265 ir_rvalue *new_param = convert_vec_index_to_cond_assign(param);
266
267 if (new_param != param) {
268 param->replace_with(new_param);
269 }
270 }
271
272 return visit_continue;
273 }
274
275 ir_visitor_status
276 ir_vec_index_to_cond_assign_visitor::visit_enter(ir_return *ir)
277 {
278 if (ir->value) {
279 ir->value = convert_vec_index_to_cond_assign(ir->value);
280 }
281
282 return visit_continue;
283 }
284
285 ir_visitor_status
286 ir_vec_index_to_cond_assign_visitor::visit_enter(ir_if *ir)
287 {
288 ir->condition = convert_vec_index_to_cond_assign(ir->condition);
289
290 return visit_continue;
291 }
292
293 bool
294 do_vec_index_to_cond_assign(exec_list *instructions)
295 {
296 ir_vec_index_to_cond_assign_visitor v;
297
298 visit_list_elements(&v, instructions);
299
300 return v.progress;
301 }