glsl: Eliminate ambiguity between function ins/outs and shader ins/outs
[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_hierarchical_visitor.h"
50 #include "ir.h"
51
52 class lower_clip_distance_visitor : public ir_hierarchical_visitor {
53 public:
54 lower_clip_distance_visitor()
55 : progress(false), old_clip_distance_var(NULL),
56 new_clip_distance_var(NULL)
57 {
58 }
59
60 virtual ir_visitor_status visit(ir_variable *);
61 void create_indices(ir_rvalue*, ir_rvalue *&, ir_rvalue *&);
62 virtual ir_visitor_status visit_leave(ir_dereference_array *);
63 virtual ir_visitor_status visit_leave(ir_assignment *);
64 void visit_new_assignment(ir_assignment *ir);
65 virtual ir_visitor_status visit_leave(ir_call *);
66
67 bool progress;
68
69 /**
70 * Pointer to the declaration of gl_ClipDistance, if found.
71 */
72 ir_variable *old_clip_distance_var;
73
74 /**
75 * Pointer to the newly-created gl_ClipDistanceMESA variable.
76 */
77 ir_variable *new_clip_distance_var;
78 };
79
80
81 /**
82 * Replace any declaration of gl_ClipDistance as an array of floats with a
83 * declaration of gl_ClipDistanceMESA as an array of vec4's.
84 */
85 ir_visitor_status
86 lower_clip_distance_visitor::visit(ir_variable *ir)
87 {
88 /* No point in looking for the declaration of gl_ClipDistance if
89 * we've already found it.
90 */
91 if (this->old_clip_distance_var)
92 return visit_continue;
93
94 if (ir->name && strcmp(ir->name, "gl_ClipDistance") == 0) {
95 this->progress = true;
96 this->old_clip_distance_var = ir;
97 assert (ir->type->is_array());
98 assert (ir->type->element_type() == glsl_type::float_type);
99 unsigned new_size = (ir->type->array_size() + 3) / 4;
100
101 /* Clone the old var so that we inherit all of its properties */
102 this->new_clip_distance_var = ir->clone(ralloc_parent(ir), NULL);
103
104 /* And change the properties that we need to change */
105 this->new_clip_distance_var->name
106 = ralloc_strdup(this->new_clip_distance_var, "gl_ClipDistanceMESA");
107 this->new_clip_distance_var->type
108 = glsl_type::get_array_instance(glsl_type::vec4_type, new_size);
109 this->new_clip_distance_var->max_array_access = ir->max_array_access / 4;
110
111 ir->replace_with(this->new_clip_distance_var);
112 }
113 return visit_continue;
114 }
115
116
117 /**
118 * Create the necessary GLSL rvalues to index into gl_ClipDistanceMESA based
119 * on the rvalue previously used to index into gl_ClipDistance.
120 *
121 * \param array_index Selects one of the vec4's in gl_ClipDistanceMESA
122 * \param swizzle_index Selects a component within the vec4 selected by
123 * array_index.
124 */
125 void
126 lower_clip_distance_visitor::create_indices(ir_rvalue *old_index,
127 ir_rvalue *&array_index,
128 ir_rvalue *&swizzle_index)
129 {
130 void *ctx = ralloc_parent(old_index);
131
132 /* Make sure old_index is a signed int so that the bitwise "shift" and
133 * "and" operations below type check properly.
134 */
135 if (old_index->type != glsl_type::int_type) {
136 assert (old_index->type == glsl_type::uint_type);
137 old_index = new(ctx) ir_expression(ir_unop_u2i, old_index);
138 }
139
140 ir_constant *old_index_constant = old_index->constant_expression_value();
141 if (old_index_constant) {
142 /* gl_ClipDistance is being accessed via a constant index. Don't bother
143 * creating expressions to calculate the lowered indices. Just create
144 * constants.
145 */
146 int const_val = old_index_constant->get_int_component(0);
147 array_index = new(ctx) ir_constant(const_val / 4);
148 swizzle_index = new(ctx) ir_constant(const_val % 4);
149 } else {
150 /* Create a variable to hold the value of old_index (so that we
151 * don't compute it twice).
152 */
153 ir_variable *old_index_var = new(ctx) ir_variable(
154 glsl_type::int_type, "clip_distance_index", ir_var_temporary);
155 this->base_ir->insert_before(old_index_var);
156 this->base_ir->insert_before(new(ctx) ir_assignment(
157 new(ctx) ir_dereference_variable(old_index_var), old_index));
158
159 /* Create the expression clip_distance_index / 4. Do this as a bit
160 * shift because that's likely to be more efficient.
161 */
162 array_index = new(ctx) ir_expression(
163 ir_binop_rshift, new(ctx) ir_dereference_variable(old_index_var),
164 new(ctx) ir_constant(2));
165
166 /* Create the expression clip_distance_index % 4. Do this as a bitwise
167 * AND because that's likely to be more efficient.
168 */
169 swizzle_index = new(ctx) ir_expression(
170 ir_binop_bit_and, new(ctx) ir_dereference_variable(old_index_var),
171 new(ctx) ir_constant(3));
172 }
173 }
174
175
176 /**
177 * Replace any expression that indexes into the gl_ClipDistance array with an
178 * expression that indexes into one of the vec4's in gl_ClipDistanceMESA and
179 * accesses the appropriate component.
180 */
181 ir_visitor_status
182 lower_clip_distance_visitor::visit_leave(ir_dereference_array *ir)
183 {
184 /* If the gl_ClipDistance var hasn't been declared yet, then
185 * there's no way this deref can refer to it.
186 */
187 if (!this->old_clip_distance_var)
188 return visit_continue;
189
190 ir_dereference_variable *old_var_ref = ir->array->as_dereference_variable();
191 if (old_var_ref && old_var_ref->var == this->old_clip_distance_var) {
192 this->progress = true;
193 ir_rvalue *array_index;
194 ir_rvalue *swizzle_index;
195 this->create_indices(ir->array_index, array_index, swizzle_index);
196 void *mem_ctx = ralloc_parent(ir);
197 ir->array = new(mem_ctx) ir_dereference_array(
198 this->new_clip_distance_var, array_index);
199 ir->array_index = swizzle_index;
200 }
201
202 return visit_continue;
203 }
204
205
206 /**
207 * Replace any assignment having gl_ClipDistance (undereferenced) as its LHS
208 * or RHS with a sequence of assignments, one for each component of the array.
209 * Each of these assignments is lowered to refer to gl_ClipDistanceMESA as
210 * appropriate.
211 */
212 ir_visitor_status
213 lower_clip_distance_visitor::visit_leave(ir_assignment *ir)
214 {
215 ir_dereference_variable *lhs_var = ir->lhs->as_dereference_variable();
216 ir_dereference_variable *rhs_var = ir->rhs->as_dereference_variable();
217 if ((lhs_var && lhs_var->var == this->old_clip_distance_var)
218 || (rhs_var && rhs_var->var == this->old_clip_distance_var)) {
219 /* LHS or RHS of the assignment is the entire gl_ClipDistance array.
220 * Since we are reshaping gl_ClipDistance from an array of floats to an
221 * array of vec4's, this isn't going to work as a bulk assignment
222 * anymore, so unroll it to element-by-element assignments and lower
223 * each of them.
224 *
225 * Note: to unroll into element-by-element assignments, we need to make
226 * clones of the LHS and RHS. This is only safe if the LHS and RHS are
227 * side-effect free. Fortunately, we know that they are, because the
228 * only kind of rvalue that can have side effects is an ir_call, and
229 * ir_calls only appear (a) as a statement on their own, or (b) as the
230 * RHS of an assignment that stores the result of the call in a
231 * temporary variable.
232 */
233 void *ctx = ralloc_parent(ir);
234 int array_size = this->old_clip_distance_var->type->array_size();
235 for (int i = 0; i < array_size; ++i) {
236 ir_dereference_array *new_lhs = new(ctx) ir_dereference_array(
237 ir->lhs->clone(ctx, NULL), new(ctx) ir_constant(i));
238 new_lhs->accept(this);
239 ir_dereference_array *new_rhs = new(ctx) ir_dereference_array(
240 ir->rhs->clone(ctx, NULL), new(ctx) ir_constant(i));
241 new_rhs->accept(this);
242 this->base_ir->insert_before(
243 new(ctx) ir_assignment(new_lhs, new_rhs));
244 }
245 ir->remove();
246 }
247
248 return visit_continue;
249 }
250
251
252 /**
253 * Set up base_ir properly and call visit_leave() on a newly created
254 * ir_assignment node. This is used in cases where we have to insert an
255 * ir_assignment in a place where we know the hierarchical visitor won't see
256 * it.
257 */
258 void
259 lower_clip_distance_visitor::visit_new_assignment(ir_assignment *ir)
260 {
261 ir_instruction *old_base_ir = this->base_ir;
262 this->base_ir = ir;
263 ir->accept(this);
264 this->base_ir = old_base_ir;
265 }
266
267
268 /**
269 * If gl_ClipDistance appears as an argument in an ir_call expression, replace
270 * it with a temporary variable, and make sure the ir_call is preceded and/or
271 * followed by assignments that copy the contents of the temporary variable to
272 * and/or from gl_ClipDistance. Each of these assignments is then lowered to
273 * refer to gl_ClipDistanceMESA.
274 */
275 ir_visitor_status
276 lower_clip_distance_visitor::visit_leave(ir_call *ir)
277 {
278 void *ctx = ralloc_parent(ir);
279
280 const exec_node *formal_param_node = ir->callee->parameters.head;
281 const exec_node *actual_param_node = ir->actual_parameters.head;
282 while (!actual_param_node->is_tail_sentinel()) {
283 ir_variable *formal_param = (ir_variable *) formal_param_node;
284 ir_rvalue *actual_param = (ir_rvalue *) actual_param_node;
285
286 /* Advance formal_param_node and actual_param_node now so that we can
287 * safely replace actual_param with another node, if necessary, below.
288 */
289 formal_param_node = formal_param_node->next;
290 actual_param_node = actual_param_node->next;
291
292 ir_dereference_variable *deref = actual_param->as_dereference_variable();
293 if (deref && deref->var == this->old_clip_distance_var) {
294 /* User is trying to pass the whole gl_ClipDistance array to a
295 * function call. Since we are reshaping gl_ClipDistance from an
296 * array of floats to an array of vec4's, this isn't going to work
297 * anymore, so use a temporary array instead.
298 */
299 ir_variable *temp_clip_distance = new(ctx) ir_variable(
300 actual_param->type, "temp_clip_distance", ir_var_temporary);
301 this->base_ir->insert_before(temp_clip_distance);
302 actual_param->replace_with(
303 new(ctx) ir_dereference_variable(temp_clip_distance));
304 if (formal_param->mode == ir_var_function_in
305 || formal_param->mode == ir_var_function_inout) {
306 /* Copy from gl_ClipDistance to the temporary before the call.
307 * Since we are going to insert this copy before the current
308 * instruction, we need to visit it afterwards to make sure it
309 * gets lowered.
310 */
311 ir_assignment *new_assignment = new(ctx) ir_assignment(
312 new(ctx) ir_dereference_variable(temp_clip_distance),
313 new(ctx) ir_dereference_variable(old_clip_distance_var));
314 this->base_ir->insert_before(new_assignment);
315 this->visit_new_assignment(new_assignment);
316 }
317 if (formal_param->mode == ir_var_function_out
318 || formal_param->mode == ir_var_function_inout) {
319 /* Copy from the temporary to gl_ClipDistance after the call.
320 * Since visit_list_elements() has already decided which
321 * instruction it's going to visit next, we need to visit
322 * afterwards to make sure it gets lowered.
323 */
324 ir_assignment *new_assignment = new(ctx) ir_assignment(
325 new(ctx) ir_dereference_variable(old_clip_distance_var),
326 new(ctx) ir_dereference_variable(temp_clip_distance));
327 this->base_ir->insert_after(new_assignment);
328 this->visit_new_assignment(new_assignment);
329 }
330 }
331 }
332
333 return visit_continue;
334 }
335
336
337 bool
338 lower_clip_distance(gl_shader *shader)
339 {
340 lower_clip_distance_visitor v;
341
342 visit_list_elements(&v, shader->ir);
343
344 if (v.new_clip_distance_var)
345 shader->symbols->add_variable(v.new_clip_distance_var);
346
347 return v.progress;
348 }