i965: w/a for gather4 green RG32F
[mesa.git] / src / glsl / lower_clip_distance.cpp
1 /*
2 * Copyright © 2011 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_clip_distance.cpp
26 *
27 * This pass accounts for the difference between the way
28 * gl_ClipDistance is declared in standard GLSL (as an array of
29 * floats), and the way it is frequently implemented in hardware (as
30 * a pair of vec4s, with four clip distances packed into each).
31 *
32 * The declaration of gl_ClipDistance is replaced with a declaration
33 * of gl_ClipDistanceMESA, and any references to gl_ClipDistance are
34 * translated to refer to gl_ClipDistanceMESA with the appropriate
35 * swizzling of array indices. For instance:
36 *
37 * gl_ClipDistance[i]
38 *
39 * is translated into:
40 *
41 * gl_ClipDistanceMESA[i>>2][i&3]
42 *
43 * Since some hardware may not internally represent gl_ClipDistance as a pair
44 * of vec4's, this lowering pass is optional. To enable it, set the
45 * LowerClipDistance flag in gl_shader_compiler_options to true.
46 */
47
48 #include "glsl_symbol_table.h"
49 #include "ir_rvalue_visitor.h"
50 #include "ir.h"
51 #include "program/prog_instruction.h" /* For WRITEMASK_* */
52
53 namespace {
54
55 class lower_clip_distance_visitor : public ir_rvalue_visitor {
56 public:
57 lower_clip_distance_visitor()
58 : progress(false), old_clip_distance_var(NULL),
59 new_clip_distance_var(NULL)
60 {
61 }
62
63 virtual ir_visitor_status visit(ir_variable *);
64 void create_indices(ir_rvalue*, ir_rvalue *&, ir_rvalue *&);
65 virtual ir_visitor_status visit_leave(ir_assignment *);
66 void visit_new_assignment(ir_assignment *ir);
67 virtual ir_visitor_status visit_leave(ir_call *);
68
69 virtual void handle_rvalue(ir_rvalue **rvalue);
70
71 void fix_lhs(ir_assignment *);
72
73 bool progress;
74
75 /**
76 * Pointer to the declaration of gl_ClipDistance, if found.
77 */
78 ir_variable *old_clip_distance_var;
79
80 /**
81 * Pointer to the newly-created gl_ClipDistanceMESA variable.
82 */
83 ir_variable *new_clip_distance_var;
84 };
85
86 } /* anonymous namespace */
87
88 /**
89 * Replace any declaration of gl_ClipDistance as an array of floats with a
90 * declaration of gl_ClipDistanceMESA as an array of vec4's.
91 */
92 ir_visitor_status
93 lower_clip_distance_visitor::visit(ir_variable *ir)
94 {
95 /* No point in looking for the declaration of gl_ClipDistance if
96 * we've already found it.
97 */
98 if (this->old_clip_distance_var)
99 return visit_continue;
100
101 if (ir->name && strcmp(ir->name, "gl_ClipDistance") == 0) {
102 this->progress = true;
103 this->old_clip_distance_var = ir;
104 assert (ir->type->is_array());
105 assert (ir->type->element_type() == glsl_type::float_type);
106 unsigned new_size = (ir->type->array_size() + 3) / 4;
107
108 /* Clone the old var so that we inherit all of its properties */
109 this->new_clip_distance_var = ir->clone(ralloc_parent(ir), NULL);
110
111 /* And change the properties that we need to change */
112 this->new_clip_distance_var->name
113 = ralloc_strdup(this->new_clip_distance_var, "gl_ClipDistanceMESA");
114 this->new_clip_distance_var->type
115 = glsl_type::get_array_instance(glsl_type::vec4_type, new_size);
116 this->new_clip_distance_var->max_array_access = ir->max_array_access / 4;
117
118 ir->replace_with(this->new_clip_distance_var);
119 }
120 return visit_continue;
121 }
122
123
124 /**
125 * Create the necessary GLSL rvalues to index into gl_ClipDistanceMESA based
126 * on the rvalue previously used to index into gl_ClipDistance.
127 *
128 * \param array_index Selects one of the vec4's in gl_ClipDistanceMESA
129 * \param swizzle_index Selects a component within the vec4 selected by
130 * array_index.
131 */
132 void
133 lower_clip_distance_visitor::create_indices(ir_rvalue *old_index,
134 ir_rvalue *&array_index,
135 ir_rvalue *&swizzle_index)
136 {
137 void *ctx = ralloc_parent(old_index);
138
139 /* Make sure old_index is a signed int so that the bitwise "shift" and
140 * "and" operations below type check properly.
141 */
142 if (old_index->type != glsl_type::int_type) {
143 assert (old_index->type == glsl_type::uint_type);
144 old_index = new(ctx) ir_expression(ir_unop_u2i, old_index);
145 }
146
147 ir_constant *old_index_constant = old_index->constant_expression_value();
148 if (old_index_constant) {
149 /* gl_ClipDistance is being accessed via a constant index. Don't bother
150 * creating expressions to calculate the lowered indices. Just create
151 * constants.
152 */
153 int const_val = old_index_constant->get_int_component(0);
154 array_index = new(ctx) ir_constant(const_val / 4);
155 swizzle_index = new(ctx) ir_constant(const_val % 4);
156 } else {
157 /* Create a variable to hold the value of old_index (so that we
158 * don't compute it twice).
159 */
160 ir_variable *old_index_var = new(ctx) ir_variable(
161 glsl_type::int_type, "clip_distance_index", ir_var_temporary);
162 this->base_ir->insert_before(old_index_var);
163 this->base_ir->insert_before(new(ctx) ir_assignment(
164 new(ctx) ir_dereference_variable(old_index_var), old_index));
165
166 /* Create the expression clip_distance_index / 4. Do this as a bit
167 * shift because that's likely to be more efficient.
168 */
169 array_index = new(ctx) ir_expression(
170 ir_binop_rshift, new(ctx) ir_dereference_variable(old_index_var),
171 new(ctx) ir_constant(2));
172
173 /* Create the expression clip_distance_index % 4. Do this as a bitwise
174 * AND because that's likely to be more efficient.
175 */
176 swizzle_index = new(ctx) ir_expression(
177 ir_binop_bit_and, new(ctx) ir_dereference_variable(old_index_var),
178 new(ctx) ir_constant(3));
179 }
180 }
181
182
183 void
184 lower_clip_distance_visitor::handle_rvalue(ir_rvalue **rv)
185 {
186 /* If the gl_ClipDistance var hasn't been declared yet, then
187 * there's no way this deref can refer to it.
188 */
189 if (!this->old_clip_distance_var || *rv == NULL)
190 return;
191
192 ir_dereference_array *const array_deref = (*rv)->as_dereference_array();
193 if (array_deref == NULL)
194 return;
195
196 /* Replace any expression that indexes into the gl_ClipDistance array
197 * with an expression that indexes into one of the vec4's in
198 * gl_ClipDistanceMESA and accesses the appropriate component.
199 */
200 ir_dereference_variable *old_var_ref =
201 array_deref->array->as_dereference_variable();
202 if (old_var_ref && old_var_ref->var == this->old_clip_distance_var) {
203 this->progress = true;
204 ir_rvalue *array_index;
205 ir_rvalue *swizzle_index;
206 this->create_indices(array_deref->array_index, array_index, swizzle_index);
207 void *mem_ctx = ralloc_parent(array_deref);
208
209 ir_dereference_array *const ClipDistanceMESA_deref =
210 new(mem_ctx) ir_dereference_array(this->new_clip_distance_var,
211 array_index);
212
213 ir_expression *const expr =
214 new(mem_ctx) ir_expression(ir_binop_vector_extract,
215 ClipDistanceMESA_deref,
216 swizzle_index);
217
218 *rv = expr;
219 }
220 }
221
222 void
223 lower_clip_distance_visitor::fix_lhs(ir_assignment *ir)
224 {
225 if (ir->lhs->ir_type == ir_type_expression) {
226 void *mem_ctx = ralloc_parent(ir);
227 ir_expression *const expr = (ir_expression *) ir->lhs;
228
229 /* The expression must be of the form:
230 *
231 * (vector_extract gl_ClipDistanceMESA[i], j).
232 */
233 assert(expr->operation == ir_binop_vector_extract);
234 assert(expr->operands[0]->ir_type == ir_type_dereference_array);
235 assert(expr->operands[0]->type == glsl_type::vec4_type);
236
237 ir_dereference *const new_lhs = (ir_dereference *) expr->operands[0];
238 ir->rhs = new(mem_ctx) ir_expression(ir_triop_vector_insert,
239 glsl_type::vec4_type,
240 new_lhs->clone(mem_ctx, NULL),
241 ir->rhs,
242 expr->operands[1]);
243 ir->set_lhs(new_lhs);
244 ir->write_mask = WRITEMASK_XYZW;
245 }
246 }
247
248 /**
249 * Replace any assignment having gl_ClipDistance (undereferenced) as its LHS
250 * or RHS with a sequence of assignments, one for each component of the array.
251 * Each of these assignments is lowered to refer to gl_ClipDistanceMESA as
252 * appropriate.
253 */
254 ir_visitor_status
255 lower_clip_distance_visitor::visit_leave(ir_assignment *ir)
256 {
257 ir_dereference_variable *lhs_var = ir->lhs->as_dereference_variable();
258 ir_dereference_variable *rhs_var = ir->rhs->as_dereference_variable();
259 if ((lhs_var && lhs_var->var == this->old_clip_distance_var)
260 || (rhs_var && rhs_var->var == this->old_clip_distance_var)) {
261 /* LHS or RHS of the assignment is the entire gl_ClipDistance array.
262 * Since we are reshaping gl_ClipDistance from an array of floats to an
263 * array of vec4's, this isn't going to work as a bulk assignment
264 * anymore, so unroll it to element-by-element assignments and lower
265 * each of them.
266 *
267 * Note: to unroll into element-by-element assignments, we need to make
268 * clones of the LHS and RHS. This is safe because expressions and
269 * l-values are side-effect free.
270 */
271 void *ctx = ralloc_parent(ir);
272 int array_size = this->old_clip_distance_var->type->array_size();
273 for (int i = 0; i < array_size; ++i) {
274 ir_dereference_array *new_lhs = new(ctx) ir_dereference_array(
275 ir->lhs->clone(ctx, NULL), new(ctx) ir_constant(i));
276 ir_dereference_array *new_rhs = new(ctx) ir_dereference_array(
277 ir->rhs->clone(ctx, NULL), new(ctx) ir_constant(i));
278 this->handle_rvalue((ir_rvalue **) &new_rhs);
279
280 /* Handle the LHS after creating the new assignment. This must
281 * happen in this order because handle_rvalue may replace the old LHS
282 * with an ir_expression of ir_binop_vector_extract. Since this is
283 * not a valide l-value, this will cause an assertion in the
284 * ir_assignment constructor to fail.
285 *
286 * If this occurs, replace the mangled LHS with a dereference of the
287 * vector, and replace the RHS with an ir_triop_vector_insert.
288 */
289 ir_assignment *const assign = new(ctx) ir_assignment(new_lhs, new_rhs);
290 this->handle_rvalue((ir_rvalue **) &assign->lhs);
291 this->fix_lhs(assign);
292
293 this->base_ir->insert_before(assign);
294 }
295 ir->remove();
296
297 return visit_continue;
298 }
299
300 /* Handle the LHS as if it were an r-value. Normally
301 * rvalue_visit(ir_assignment *) only visits the RHS, but we need to lower
302 * expressions in the LHS as well.
303 *
304 * This may cause the LHS to get replaced with an ir_expression of
305 * ir_binop_vector_extract. If this occurs, replace it with a dereference
306 * of the vector, and replace the RHS with an ir_triop_vector_insert.
307 */
308 handle_rvalue((ir_rvalue **)&ir->lhs);
309 this->fix_lhs(ir);
310
311 return rvalue_visit(ir);
312 }
313
314
315 /**
316 * Set up base_ir properly and call visit_leave() on a newly created
317 * ir_assignment node. This is used in cases where we have to insert an
318 * ir_assignment in a place where we know the hierarchical visitor won't see
319 * it.
320 */
321 void
322 lower_clip_distance_visitor::visit_new_assignment(ir_assignment *ir)
323 {
324 ir_instruction *old_base_ir = this->base_ir;
325 this->base_ir = ir;
326 ir->accept(this);
327 this->base_ir = old_base_ir;
328 }
329
330
331 /**
332 * If gl_ClipDistance appears as an argument in an ir_call expression, replace
333 * it with a temporary variable, and make sure the ir_call is preceded and/or
334 * followed by assignments that copy the contents of the temporary variable to
335 * and/or from gl_ClipDistance. Each of these assignments is then lowered to
336 * refer to gl_ClipDistanceMESA.
337 */
338 ir_visitor_status
339 lower_clip_distance_visitor::visit_leave(ir_call *ir)
340 {
341 void *ctx = ralloc_parent(ir);
342
343 const exec_node *formal_param_node = ir->callee->parameters.head;
344 const exec_node *actual_param_node = ir->actual_parameters.head;
345 while (!actual_param_node->is_tail_sentinel()) {
346 ir_variable *formal_param = (ir_variable *) formal_param_node;
347 ir_rvalue *actual_param = (ir_rvalue *) actual_param_node;
348
349 /* Advance formal_param_node and actual_param_node now so that we can
350 * safely replace actual_param with another node, if necessary, below.
351 */
352 formal_param_node = formal_param_node->next;
353 actual_param_node = actual_param_node->next;
354
355 ir_dereference_variable *deref = actual_param->as_dereference_variable();
356 if (deref && deref->var == this->old_clip_distance_var) {
357 /* User is trying to pass the whole gl_ClipDistance array to a
358 * function call. Since we are reshaping gl_ClipDistance from an
359 * array of floats to an array of vec4's, this isn't going to work
360 * anymore, so use a temporary array instead.
361 */
362 ir_variable *temp_clip_distance = new(ctx) ir_variable(
363 actual_param->type, "temp_clip_distance", ir_var_temporary);
364 this->base_ir->insert_before(temp_clip_distance);
365 actual_param->replace_with(
366 new(ctx) ir_dereference_variable(temp_clip_distance));
367 if (formal_param->mode == ir_var_function_in
368 || formal_param->mode == ir_var_function_inout) {
369 /* Copy from gl_ClipDistance to the temporary before the call.
370 * Since we are going to insert this copy before the current
371 * instruction, we need to visit it afterwards to make sure it
372 * gets lowered.
373 */
374 ir_assignment *new_assignment = new(ctx) ir_assignment(
375 new(ctx) ir_dereference_variable(temp_clip_distance),
376 new(ctx) ir_dereference_variable(old_clip_distance_var));
377 this->base_ir->insert_before(new_assignment);
378 this->visit_new_assignment(new_assignment);
379 }
380 if (formal_param->mode == ir_var_function_out
381 || formal_param->mode == ir_var_function_inout) {
382 /* Copy from the temporary to gl_ClipDistance after the call.
383 * Since visit_list_elements() has already decided which
384 * instruction it's going to visit next, we need to visit
385 * afterwards to make sure it gets lowered.
386 */
387 ir_assignment *new_assignment = new(ctx) ir_assignment(
388 new(ctx) ir_dereference_variable(old_clip_distance_var),
389 new(ctx) ir_dereference_variable(temp_clip_distance));
390 this->base_ir->insert_after(new_assignment);
391 this->visit_new_assignment(new_assignment);
392 }
393 }
394 }
395
396 return rvalue_visit(ir);
397 }
398
399
400 bool
401 lower_clip_distance(gl_shader *shader)
402 {
403 lower_clip_distance_visitor v;
404
405 visit_list_elements(&v, shader->ir);
406
407 if (v.new_clip_distance_var)
408 shader->symbols->add_variable(v.new_clip_distance_var);
409
410 return v.progress;
411 }