2 * Copyright © 2011 Intel Corporation
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_clip_distance.cpp
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).
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:
41 * gl_ClipDistanceMESA[i>>2][i&3]
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.
48 #include "glsl_symbol_table.h"
49 #include "ir_rvalue_visitor.h"
52 class lower_clip_distance_visitor
: public ir_rvalue_visitor
{
54 lower_clip_distance_visitor()
55 : progress(false), old_clip_distance_var(NULL
),
56 new_clip_distance_var(NULL
)
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_assignment
*);
63 void visit_new_assignment(ir_assignment
*ir
);
64 virtual ir_visitor_status
visit_leave(ir_call
*);
66 virtual void handle_rvalue(ir_rvalue
**rvalue
);
71 * Pointer to the declaration of gl_ClipDistance, if found.
73 ir_variable
*old_clip_distance_var
;
76 * Pointer to the newly-created gl_ClipDistanceMESA variable.
78 ir_variable
*new_clip_distance_var
;
83 * Replace any declaration of gl_ClipDistance as an array of floats with a
84 * declaration of gl_ClipDistanceMESA as an array of vec4's.
87 lower_clip_distance_visitor::visit(ir_variable
*ir
)
89 /* No point in looking for the declaration of gl_ClipDistance if
90 * we've already found it.
92 if (this->old_clip_distance_var
)
93 return visit_continue
;
95 if (ir
->name
&& strcmp(ir
->name
, "gl_ClipDistance") == 0) {
96 this->progress
= true;
97 this->old_clip_distance_var
= ir
;
98 assert (ir
->type
->is_array());
99 assert (ir
->type
->element_type() == glsl_type::float_type
);
100 unsigned new_size
= (ir
->type
->array_size() + 3) / 4;
102 /* Clone the old var so that we inherit all of its properties */
103 this->new_clip_distance_var
= ir
->clone(ralloc_parent(ir
), NULL
);
105 /* And change the properties that we need to change */
106 this->new_clip_distance_var
->name
107 = ralloc_strdup(this->new_clip_distance_var
, "gl_ClipDistanceMESA");
108 this->new_clip_distance_var
->type
109 = glsl_type::get_array_instance(glsl_type::vec4_type
, new_size
);
110 this->new_clip_distance_var
->max_array_access
= ir
->max_array_access
/ 4;
112 ir
->replace_with(this->new_clip_distance_var
);
114 return visit_continue
;
119 * Create the necessary GLSL rvalues to index into gl_ClipDistanceMESA based
120 * on the rvalue previously used to index into gl_ClipDistance.
122 * \param array_index Selects one of the vec4's in gl_ClipDistanceMESA
123 * \param swizzle_index Selects a component within the vec4 selected by
127 lower_clip_distance_visitor::create_indices(ir_rvalue
*old_index
,
128 ir_rvalue
*&array_index
,
129 ir_rvalue
*&swizzle_index
)
131 void *ctx
= ralloc_parent(old_index
);
133 /* Make sure old_index is a signed int so that the bitwise "shift" and
134 * "and" operations below type check properly.
136 if (old_index
->type
!= glsl_type::int_type
) {
137 assert (old_index
->type
== glsl_type::uint_type
);
138 old_index
= new(ctx
) ir_expression(ir_unop_u2i
, old_index
);
141 ir_constant
*old_index_constant
= old_index
->constant_expression_value();
142 if (old_index_constant
) {
143 /* gl_ClipDistance is being accessed via a constant index. Don't bother
144 * creating expressions to calculate the lowered indices. Just create
147 int const_val
= old_index_constant
->get_int_component(0);
148 array_index
= new(ctx
) ir_constant(const_val
/ 4);
149 swizzle_index
= new(ctx
) ir_constant(const_val
% 4);
151 /* Create a variable to hold the value of old_index (so that we
152 * don't compute it twice).
154 ir_variable
*old_index_var
= new(ctx
) ir_variable(
155 glsl_type::int_type
, "clip_distance_index", ir_var_temporary
);
156 this->base_ir
->insert_before(old_index_var
);
157 this->base_ir
->insert_before(new(ctx
) ir_assignment(
158 new(ctx
) ir_dereference_variable(old_index_var
), old_index
));
160 /* Create the expression clip_distance_index / 4. Do this as a bit
161 * shift because that's likely to be more efficient.
163 array_index
= new(ctx
) ir_expression(
164 ir_binop_rshift
, new(ctx
) ir_dereference_variable(old_index_var
),
165 new(ctx
) ir_constant(2));
167 /* Create the expression clip_distance_index % 4. Do this as a bitwise
168 * AND because that's likely to be more efficient.
170 swizzle_index
= new(ctx
) ir_expression(
171 ir_binop_bit_and
, new(ctx
) ir_dereference_variable(old_index_var
),
172 new(ctx
) ir_constant(3));
178 lower_clip_distance_visitor::handle_rvalue(ir_rvalue
**rv
)
180 /* If the gl_ClipDistance var hasn't been declared yet, then
181 * there's no way this deref can refer to it.
183 if (!this->old_clip_distance_var
|| *rv
== NULL
)
186 ir_dereference_array
*const array_deref
= (*rv
)->as_dereference_array();
187 if (array_deref
== NULL
)
190 /* Replace any expression that indexes into the gl_ClipDistance array
191 * with an expression that indexes into one of the vec4's in
192 * gl_ClipDistanceMESA and accesses the appropriate component.
194 ir_dereference_variable
*old_var_ref
=
195 array_deref
->array
->as_dereference_variable();
196 if (old_var_ref
&& old_var_ref
->var
== this->old_clip_distance_var
) {
197 this->progress
= true;
198 ir_rvalue
*array_index
;
199 ir_rvalue
*swizzle_index
;
200 this->create_indices(array_deref
->array_index
, array_index
, swizzle_index
);
201 void *mem_ctx
= ralloc_parent(array_deref
);
202 array_deref
->array
= new(mem_ctx
) ir_dereference_array(
203 this->new_clip_distance_var
, array_index
);
204 array_deref
->array_index
= swizzle_index
;
210 * Replace any assignment having gl_ClipDistance (undereferenced) as its LHS
211 * or RHS with a sequence of assignments, one for each component of the array.
212 * Each of these assignments is lowered to refer to gl_ClipDistanceMESA as
216 lower_clip_distance_visitor::visit_leave(ir_assignment
*ir
)
218 ir_dereference_variable
*lhs_var
= ir
->lhs
->as_dereference_variable();
219 ir_dereference_variable
*rhs_var
= ir
->rhs
->as_dereference_variable();
220 if ((lhs_var
&& lhs_var
->var
== this->old_clip_distance_var
)
221 || (rhs_var
&& rhs_var
->var
== this->old_clip_distance_var
)) {
222 /* LHS or RHS of the assignment is the entire gl_ClipDistance array.
223 * Since we are reshaping gl_ClipDistance from an array of floats to an
224 * array of vec4's, this isn't going to work as a bulk assignment
225 * anymore, so unroll it to element-by-element assignments and lower
228 * Note: to unroll into element-by-element assignments, we need to make
229 * clones of the LHS and RHS. This is only safe if the LHS and RHS are
230 * side-effect free. Fortunately, we know that they are, because the
231 * only kind of rvalue that can have side effects is an ir_call, and
232 * ir_calls only appear (a) as a statement on their own, or (b) as the
233 * RHS of an assignment that stores the result of the call in a
234 * temporary variable.
236 void *ctx
= ralloc_parent(ir
);
237 int array_size
= this->old_clip_distance_var
->type
->array_size();
238 for (int i
= 0; i
< array_size
; ++i
) {
239 ir_dereference_array
*new_lhs
= new(ctx
) ir_dereference_array(
240 ir
->lhs
->clone(ctx
, NULL
), new(ctx
) ir_constant(i
));
241 this->handle_rvalue((ir_rvalue
**) &new_lhs
);
242 ir_dereference_array
*new_rhs
= new(ctx
) ir_dereference_array(
243 ir
->rhs
->clone(ctx
, NULL
), new(ctx
) ir_constant(i
));
244 this->handle_rvalue((ir_rvalue
**) &new_rhs
);
245 this->base_ir
->insert_before(
246 new(ctx
) ir_assignment(new_lhs
, new_rhs
));
250 return visit_continue
;
253 /* Handle the LHS as if it were an r-value. Normally
254 * rvalue_visit(ir_assignment *) only visits the RHS, but we need to lower
255 * expressions in the LHS as well.
257 handle_rvalue((ir_rvalue
**)&ir
->lhs
);
258 return rvalue_visit(ir
);
263 * Set up base_ir properly and call visit_leave() on a newly created
264 * ir_assignment node. This is used in cases where we have to insert an
265 * ir_assignment in a place where we know the hierarchical visitor won't see
269 lower_clip_distance_visitor::visit_new_assignment(ir_assignment
*ir
)
271 ir_instruction
*old_base_ir
= this->base_ir
;
274 this->base_ir
= old_base_ir
;
279 * If gl_ClipDistance appears as an argument in an ir_call expression, replace
280 * it with a temporary variable, and make sure the ir_call is preceded and/or
281 * followed by assignments that copy the contents of the temporary variable to
282 * and/or from gl_ClipDistance. Each of these assignments is then lowered to
283 * refer to gl_ClipDistanceMESA.
286 lower_clip_distance_visitor::visit_leave(ir_call
*ir
)
288 void *ctx
= ralloc_parent(ir
);
290 const exec_node
*formal_param_node
= ir
->callee
->parameters
.head
;
291 const exec_node
*actual_param_node
= ir
->actual_parameters
.head
;
292 while (!actual_param_node
->is_tail_sentinel()) {
293 ir_variable
*formal_param
= (ir_variable
*) formal_param_node
;
294 ir_rvalue
*actual_param
= (ir_rvalue
*) actual_param_node
;
296 /* Advance formal_param_node and actual_param_node now so that we can
297 * safely replace actual_param with another node, if necessary, below.
299 formal_param_node
= formal_param_node
->next
;
300 actual_param_node
= actual_param_node
->next
;
302 ir_dereference_variable
*deref
= actual_param
->as_dereference_variable();
303 if (deref
&& deref
->var
== this->old_clip_distance_var
) {
304 /* User is trying to pass the whole gl_ClipDistance array to a
305 * function call. Since we are reshaping gl_ClipDistance from an
306 * array of floats to an array of vec4's, this isn't going to work
307 * anymore, so use a temporary array instead.
309 ir_variable
*temp_clip_distance
= new(ctx
) ir_variable(
310 actual_param
->type
, "temp_clip_distance", ir_var_temporary
);
311 this->base_ir
->insert_before(temp_clip_distance
);
312 actual_param
->replace_with(
313 new(ctx
) ir_dereference_variable(temp_clip_distance
));
314 if (formal_param
->mode
== ir_var_function_in
315 || formal_param
->mode
== ir_var_function_inout
) {
316 /* Copy from gl_ClipDistance to the temporary before the call.
317 * Since we are going to insert this copy before the current
318 * instruction, we need to visit it afterwards to make sure it
321 ir_assignment
*new_assignment
= new(ctx
) ir_assignment(
322 new(ctx
) ir_dereference_variable(temp_clip_distance
),
323 new(ctx
) ir_dereference_variable(old_clip_distance_var
));
324 this->base_ir
->insert_before(new_assignment
);
325 this->visit_new_assignment(new_assignment
);
327 if (formal_param
->mode
== ir_var_function_out
328 || formal_param
->mode
== ir_var_function_inout
) {
329 /* Copy from the temporary to gl_ClipDistance after the call.
330 * Since visit_list_elements() has already decided which
331 * instruction it's going to visit next, we need to visit
332 * afterwards to make sure it gets lowered.
334 ir_assignment
*new_assignment
= new(ctx
) ir_assignment(
335 new(ctx
) ir_dereference_variable(old_clip_distance_var
),
336 new(ctx
) ir_dereference_variable(temp_clip_distance
));
337 this->base_ir
->insert_after(new_assignment
);
338 this->visit_new_assignment(new_assignment
);
343 return rvalue_visit(ir
);
348 lower_clip_distance(gl_shader
*shader
)
350 lower_clip_distance_visitor v
;
352 visit_list_elements(&v
, shader
->ir
);
354 if (v
.new_clip_distance_var
)
355 shader
->symbols
->add_variable(v
.new_clip_distance_var
);