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"
51 #include "program/prog_instruction.h" /* For WRITEMASK_* */
55 class lower_clip_distance_visitor
: public ir_rvalue_visitor
{
57 explicit lower_clip_distance_visitor(gl_shader_stage shader_stage
)
58 : progress(false), old_clip_distance_out_var(NULL
),
59 old_clip_distance_in_var(NULL
), new_clip_distance_out_var(NULL
),
60 new_clip_distance_in_var(NULL
), shader_stage(shader_stage
)
64 virtual ir_visitor_status
visit(ir_variable
*);
65 void create_indices(ir_rvalue
*, ir_rvalue
*&, ir_rvalue
*&);
66 bool is_clip_distance_vec8(ir_rvalue
*ir
);
67 ir_rvalue
*lower_clip_distance_vec8(ir_rvalue
*ir
);
68 virtual ir_visitor_status
visit_leave(ir_assignment
*);
69 void visit_new_assignment(ir_assignment
*ir
);
70 virtual ir_visitor_status
visit_leave(ir_call
*);
72 virtual void handle_rvalue(ir_rvalue
**rvalue
);
74 void fix_lhs(ir_assignment
*);
79 * Pointer to the declaration of gl_ClipDistance, if found.
83 * - the in_var is for geometry and both tessellation shader inputs only.
85 * - since gl_ClipDistance is available in tessellation control,
86 * tessellation evaluation and geometry shaders as both an input
87 * and an output, it's possible for both old_clip_distance_out_var
88 * and old_clip_distance_in_var to be non-null.
90 ir_variable
*old_clip_distance_out_var
;
91 ir_variable
*old_clip_distance_in_var
;
94 * Pointer to the newly-created gl_ClipDistanceMESA variable.
96 ir_variable
*new_clip_distance_out_var
;
97 ir_variable
*new_clip_distance_in_var
;
100 * Type of shader we are compiling (e.g. MESA_SHADER_VERTEX)
102 const gl_shader_stage shader_stage
;
105 } /* anonymous namespace */
108 * Replace any declaration of gl_ClipDistance as an array of floats with a
109 * declaration of gl_ClipDistanceMESA as an array of vec4's.
112 lower_clip_distance_visitor::visit(ir_variable
*ir
)
114 ir_variable
**old_var
;
115 ir_variable
**new_var
;
117 if (!ir
->name
|| strcmp(ir
->name
, "gl_ClipDistance") != 0)
118 return visit_continue
;
119 assert (ir
->type
->is_array());
121 if (ir
->data
.mode
== ir_var_shader_out
) {
122 if (this->old_clip_distance_out_var
)
123 return visit_continue
;
124 old_var
= &old_clip_distance_out_var
;
125 new_var
= &new_clip_distance_out_var
;
126 } else if (ir
->data
.mode
== ir_var_shader_in
) {
127 if (this->old_clip_distance_in_var
)
128 return visit_continue
;
129 old_var
= &old_clip_distance_in_var
;
130 new_var
= &new_clip_distance_in_var
;
132 unreachable("not reached");
135 this->progress
= true;
137 if (!ir
->type
->fields
.array
->is_array()) {
138 /* gl_ClipDistance (used for vertex, tessellation evaluation and
139 * geometry output, and fragment input).
141 assert((ir
->data
.mode
== ir_var_shader_in
&&
142 this->shader_stage
== MESA_SHADER_FRAGMENT
) ||
143 (ir
->data
.mode
== ir_var_shader_out
&&
144 (this->shader_stage
== MESA_SHADER_VERTEX
||
145 this->shader_stage
== MESA_SHADER_TESS_EVAL
||
146 this->shader_stage
== MESA_SHADER_GEOMETRY
)));
149 assert (ir
->type
->fields
.array
== glsl_type::float_type
);
150 unsigned new_size
= (ir
->type
->array_size() + 3) / 4;
152 /* Clone the old var so that we inherit all of its properties */
153 *new_var
= ir
->clone(ralloc_parent(ir
), NULL
);
155 /* And change the properties that we need to change */
156 (*new_var
)->name
= ralloc_strdup(*new_var
, "gl_ClipDistanceMESA");
157 (*new_var
)->type
= glsl_type::get_array_instance(glsl_type::vec4_type
,
159 (*new_var
)->data
.max_array_access
= ir
->data
.max_array_access
/ 4;
161 ir
->replace_with(*new_var
);
163 /* 2D gl_ClipDistance (used for tessellation control, tessellation
164 * evaluation and geometry input, and tessellation control output).
166 assert((ir
->data
.mode
== ir_var_shader_in
&&
167 (this->shader_stage
== MESA_SHADER_GEOMETRY
||
168 this->shader_stage
== MESA_SHADER_TESS_EVAL
)) ||
169 this->shader_stage
== MESA_SHADER_TESS_CTRL
);
172 assert (ir
->type
->fields
.array
->fields
.array
== glsl_type::float_type
);
173 unsigned new_size
= (ir
->type
->fields
.array
->array_size() + 3) / 4;
175 /* Clone the old var so that we inherit all of its properties */
176 *new_var
= ir
->clone(ralloc_parent(ir
), NULL
);
178 /* And change the properties that we need to change */
179 (*new_var
)->name
= ralloc_strdup(*new_var
, "gl_ClipDistanceMESA");
180 (*new_var
)->type
= glsl_type::get_array_instance(
181 glsl_type::get_array_instance(glsl_type::vec4_type
,
183 ir
->type
->array_size());
184 (*new_var
)->data
.max_array_access
= ir
->data
.max_array_access
/ 4;
186 ir
->replace_with(*new_var
);
189 return visit_continue
;
194 * Create the necessary GLSL rvalues to index into gl_ClipDistanceMESA based
195 * on the rvalue previously used to index into gl_ClipDistance.
197 * \param array_index Selects one of the vec4's in gl_ClipDistanceMESA
198 * \param swizzle_index Selects a component within the vec4 selected by
202 lower_clip_distance_visitor::create_indices(ir_rvalue
*old_index
,
203 ir_rvalue
*&array_index
,
204 ir_rvalue
*&swizzle_index
)
206 void *ctx
= ralloc_parent(old_index
);
208 /* Make sure old_index is a signed int so that the bitwise "shift" and
209 * "and" operations below type check properly.
211 if (old_index
->type
!= glsl_type::int_type
) {
212 assert (old_index
->type
== glsl_type::uint_type
);
213 old_index
= new(ctx
) ir_expression(ir_unop_u2i
, old_index
);
216 ir_constant
*old_index_constant
= old_index
->constant_expression_value();
217 if (old_index_constant
) {
218 /* gl_ClipDistance is being accessed via a constant index. Don't bother
219 * creating expressions to calculate the lowered indices. Just create
222 int const_val
= old_index_constant
->get_int_component(0);
223 array_index
= new(ctx
) ir_constant(const_val
/ 4);
224 swizzle_index
= new(ctx
) ir_constant(const_val
% 4);
226 /* Create a variable to hold the value of old_index (so that we
227 * don't compute it twice).
229 ir_variable
*old_index_var
= new(ctx
) ir_variable(
230 glsl_type::int_type
, "clip_distance_index", ir_var_temporary
);
231 this->base_ir
->insert_before(old_index_var
);
232 this->base_ir
->insert_before(new(ctx
) ir_assignment(
233 new(ctx
) ir_dereference_variable(old_index_var
), old_index
));
235 /* Create the expression clip_distance_index / 4. Do this as a bit
236 * shift because that's likely to be more efficient.
238 array_index
= new(ctx
) ir_expression(
239 ir_binop_rshift
, new(ctx
) ir_dereference_variable(old_index_var
),
240 new(ctx
) ir_constant(2));
242 /* Create the expression clip_distance_index % 4. Do this as a bitwise
243 * AND because that's likely to be more efficient.
245 swizzle_index
= new(ctx
) ir_expression(
246 ir_binop_bit_and
, new(ctx
) ir_dereference_variable(old_index_var
),
247 new(ctx
) ir_constant(3));
253 * Determine whether the given rvalue describes an array of 8 floats that
254 * needs to be lowered to an array of 2 vec4's; that is, determine whether it
255 * matches one of the following patterns:
257 * - gl_ClipDistance (if gl_ClipDistance is 1D)
258 * - gl_ClipDistance[i] (if gl_ClipDistance is 2D)
261 lower_clip_distance_visitor::is_clip_distance_vec8(ir_rvalue
*ir
)
263 /* Note that geometry shaders contain gl_ClipDistance both as an input
264 * (which is a 2D array) and an output (which is a 1D array), so it's
265 * possible for both this->old_clip_distance_out_var and
266 * this->old_clip_distance_in_var to be non-NULL in the same shader.
269 if (!ir
->type
->is_array())
271 if (ir
->type
->fields
.array
!= glsl_type::float_type
)
274 if (this->old_clip_distance_out_var
) {
275 if (ir
->variable_referenced() == this->old_clip_distance_out_var
)
278 if (this->old_clip_distance_in_var
) {
279 assert(this->shader_stage
== MESA_SHADER_TESS_CTRL
||
280 this->shader_stage
== MESA_SHADER_TESS_EVAL
||
281 this->shader_stage
== MESA_SHADER_GEOMETRY
||
282 this->shader_stage
== MESA_SHADER_FRAGMENT
);
284 if (ir
->variable_referenced() == this->old_clip_distance_in_var
)
292 * If the given ir satisfies is_clip_distance_vec8(), return new ir
293 * representing its lowered equivalent. That is, map:
295 * - gl_ClipDistance => gl_ClipDistanceMESA (if gl_ClipDistance is 1D)
296 * - gl_ClipDistance[i] => gl_ClipDistanceMESA[i] (if gl_ClipDistance is 2D)
298 * Otherwise return NULL.
301 lower_clip_distance_visitor::lower_clip_distance_vec8(ir_rvalue
*ir
)
303 if (!ir
->type
->is_array())
305 if (ir
->type
->fields
.array
!= glsl_type::float_type
)
308 ir_variable
**new_var
= NULL
;
309 if (this->old_clip_distance_out_var
) {
310 if (ir
->variable_referenced() == this->old_clip_distance_out_var
)
311 new_var
= &this->new_clip_distance_out_var
;
313 if (this->old_clip_distance_in_var
) {
314 if (ir
->variable_referenced() == this->old_clip_distance_in_var
)
315 new_var
= &this->new_clip_distance_in_var
;
320 if (ir
->as_dereference_variable()) {
321 return new(ralloc_parent(ir
)) ir_dereference_variable(*new_var
);
323 ir_dereference_array
*array_ref
= ir
->as_dereference_array();
325 assert(array_ref
->array
->as_dereference_variable());
327 return new(ralloc_parent(ir
))
328 ir_dereference_array(*new_var
, array_ref
->array_index
);
334 lower_clip_distance_visitor::handle_rvalue(ir_rvalue
**rv
)
339 ir_dereference_array
*const array_deref
= (*rv
)->as_dereference_array();
340 if (array_deref
== NULL
)
343 /* Replace any expression that indexes one of the floats in gl_ClipDistance
344 * with an expression that indexes into one of the vec4's in
345 * gl_ClipDistanceMESA and accesses the appropriate component.
347 ir_rvalue
*lowered_vec8
=
348 this->lower_clip_distance_vec8(array_deref
->array
);
349 if (lowered_vec8
!= NULL
) {
350 this->progress
= true;
351 ir_rvalue
*array_index
;
352 ir_rvalue
*swizzle_index
;
353 this->create_indices(array_deref
->array_index
, array_index
, swizzle_index
);
354 void *mem_ctx
= ralloc_parent(array_deref
);
356 ir_dereference_array
*const new_array_deref
=
357 new(mem_ctx
) ir_dereference_array(lowered_vec8
, array_index
);
359 ir_expression
*const expr
=
360 new(mem_ctx
) ir_expression(ir_binop_vector_extract
,
369 lower_clip_distance_visitor::fix_lhs(ir_assignment
*ir
)
371 if (ir
->lhs
->ir_type
== ir_type_expression
) {
372 void *mem_ctx
= ralloc_parent(ir
);
373 ir_expression
*const expr
= (ir_expression
*) ir
->lhs
;
375 /* The expression must be of the form:
377 * (vector_extract gl_ClipDistanceMESA[i], j).
379 assert(expr
->operation
== ir_binop_vector_extract
);
380 assert(expr
->operands
[0]->ir_type
== ir_type_dereference_array
);
381 assert(expr
->operands
[0]->type
== glsl_type::vec4_type
);
383 ir_dereference
*const new_lhs
= (ir_dereference
*) expr
->operands
[0];
384 ir
->rhs
= new(mem_ctx
) ir_expression(ir_triop_vector_insert
,
385 glsl_type::vec4_type
,
386 new_lhs
->clone(mem_ctx
, NULL
),
389 ir
->set_lhs(new_lhs
);
390 ir
->write_mask
= WRITEMASK_XYZW
;
395 * Replace any assignment having the 1D gl_ClipDistance (undereferenced) as
396 * its LHS or RHS with a sequence of assignments, one for each component of
397 * the array. Each of these assignments is lowered to refer to
398 * gl_ClipDistanceMESA as appropriate.
400 * We need to do a similar replacement for 2D gl_ClipDistance, however since
401 * it's an input, the only case we need to address is where a 1D slice of it
402 * is the entire RHS of an assignment, e.g.:
404 * foo = gl_in[i].gl_ClipDistance
407 lower_clip_distance_visitor::visit_leave(ir_assignment
*ir
)
409 /* First invoke the base class visitor. This causes handle_rvalue() to be
410 * called on ir->rhs and ir->condition.
412 ir_rvalue_visitor::visit_leave(ir
);
414 if (this->is_clip_distance_vec8(ir
->lhs
) ||
415 this->is_clip_distance_vec8(ir
->rhs
)) {
416 /* LHS or RHS of the assignment is the entire 1D gl_ClipDistance array
417 * (or a 1D slice of a 2D gl_ClipDistance input array). Since we are
418 * reshaping gl_ClipDistance from an array of floats to an array of
419 * vec4's, this isn't going to work as a bulk assignment anymore, so
420 * unroll it to element-by-element assignments and lower each of them.
422 * Note: to unroll into element-by-element assignments, we need to make
423 * clones of the LHS and RHS. This is safe because expressions and
424 * l-values are side-effect free.
426 void *ctx
= ralloc_parent(ir
);
427 int array_size
= ir
->lhs
->type
->array_size();
428 for (int i
= 0; i
< array_size
; ++i
) {
429 ir_dereference_array
*new_lhs
= new(ctx
) ir_dereference_array(
430 ir
->lhs
->clone(ctx
, NULL
), new(ctx
) ir_constant(i
));
431 ir_dereference_array
*new_rhs
= new(ctx
) ir_dereference_array(
432 ir
->rhs
->clone(ctx
, NULL
), new(ctx
) ir_constant(i
));
433 this->handle_rvalue((ir_rvalue
**) &new_rhs
);
435 /* Handle the LHS after creating the new assignment. This must
436 * happen in this order because handle_rvalue may replace the old LHS
437 * with an ir_expression of ir_binop_vector_extract. Since this is
438 * not a valide l-value, this will cause an assertion in the
439 * ir_assignment constructor to fail.
441 * If this occurs, replace the mangled LHS with a dereference of the
442 * vector, and replace the RHS with an ir_triop_vector_insert.
444 ir_assignment
*const assign
= new(ctx
) ir_assignment(new_lhs
, new_rhs
);
445 this->handle_rvalue((ir_rvalue
**) &assign
->lhs
);
446 this->fix_lhs(assign
);
448 this->base_ir
->insert_before(assign
);
452 return visit_continue
;
455 /* Handle the LHS as if it were an r-value. Normally
456 * rvalue_visit(ir_assignment *) only visits the RHS, but we need to lower
457 * expressions in the LHS as well.
459 * This may cause the LHS to get replaced with an ir_expression of
460 * ir_binop_vector_extract. If this occurs, replace it with a dereference
461 * of the vector, and replace the RHS with an ir_triop_vector_insert.
463 handle_rvalue((ir_rvalue
**)&ir
->lhs
);
466 return rvalue_visit(ir
);
471 * Set up base_ir properly and call visit_leave() on a newly created
472 * ir_assignment node. This is used in cases where we have to insert an
473 * ir_assignment in a place where we know the hierarchical visitor won't see
477 lower_clip_distance_visitor::visit_new_assignment(ir_assignment
*ir
)
479 ir_instruction
*old_base_ir
= this->base_ir
;
482 this->base_ir
= old_base_ir
;
487 * If a 1D gl_ClipDistance variable appears as an argument in an ir_call
488 * expression, replace it with a temporary variable, and make sure the ir_call
489 * is preceded and/or followed by assignments that copy the contents of the
490 * temporary variable to and/or from gl_ClipDistance. Each of these
491 * assignments is then lowered to refer to gl_ClipDistanceMESA.
493 * We need to do a similar replacement for 2D gl_ClipDistance, however since
494 * it's an input, the only case we need to address is where a 1D slice of it
495 * is passed as an "in" parameter to an ir_call, e.g.:
497 * foo(gl_in[i].gl_ClipDistance)
500 lower_clip_distance_visitor::visit_leave(ir_call
*ir
)
502 void *ctx
= ralloc_parent(ir
);
504 const exec_node
*formal_param_node
= ir
->callee
->parameters
.head
;
505 const exec_node
*actual_param_node
= ir
->actual_parameters
.head
;
506 while (!actual_param_node
->is_tail_sentinel()) {
507 ir_variable
*formal_param
= (ir_variable
*) formal_param_node
;
508 ir_rvalue
*actual_param
= (ir_rvalue
*) actual_param_node
;
510 /* Advance formal_param_node and actual_param_node now so that we can
511 * safely replace actual_param with another node, if necessary, below.
513 formal_param_node
= formal_param_node
->next
;
514 actual_param_node
= actual_param_node
->next
;
516 if (this->is_clip_distance_vec8(actual_param
)) {
517 /* User is trying to pass the whole 1D gl_ClipDistance array (or a 1D
518 * slice of a 2D gl_ClipDistance array) to a function call. Since we
519 * are reshaping gl_ClipDistance from an array of floats to an array
520 * of vec4's, this isn't going to work anymore, so use a temporary
523 ir_variable
*temp_clip_distance
= new(ctx
) ir_variable(
524 actual_param
->type
, "temp_clip_distance", ir_var_temporary
);
525 this->base_ir
->insert_before(temp_clip_distance
);
526 actual_param
->replace_with(
527 new(ctx
) ir_dereference_variable(temp_clip_distance
));
528 if (formal_param
->data
.mode
== ir_var_function_in
529 || formal_param
->data
.mode
== ir_var_function_inout
) {
530 /* Copy from gl_ClipDistance to the temporary before the call.
531 * Since we are going to insert this copy before the current
532 * instruction, we need to visit it afterwards to make sure it
535 ir_assignment
*new_assignment
= new(ctx
) ir_assignment(
536 new(ctx
) ir_dereference_variable(temp_clip_distance
),
537 actual_param
->clone(ctx
, NULL
));
538 this->base_ir
->insert_before(new_assignment
);
539 this->visit_new_assignment(new_assignment
);
541 if (formal_param
->data
.mode
== ir_var_function_out
542 || formal_param
->data
.mode
== ir_var_function_inout
) {
543 /* Copy from the temporary to gl_ClipDistance after the call.
544 * Since visit_list_elements() has already decided which
545 * instruction it's going to visit next, we need to visit
546 * afterwards to make sure it gets lowered.
548 ir_assignment
*new_assignment
= new(ctx
) ir_assignment(
549 actual_param
->clone(ctx
, NULL
),
550 new(ctx
) ir_dereference_variable(temp_clip_distance
));
551 this->base_ir
->insert_after(new_assignment
);
552 this->visit_new_assignment(new_assignment
);
557 return rvalue_visit(ir
);
562 lower_clip_distance(gl_shader
*shader
)
564 lower_clip_distance_visitor
v(shader
->Stage
);
566 visit_list_elements(&v
, shader
->ir
);
568 if (v
.new_clip_distance_out_var
)
569 shader
->symbols
->add_variable(v
.new_clip_distance_out_var
);
570 if (v
.new_clip_distance_in_var
)
571 shader
->symbols
->add_variable(v
.new_clip_distance_in_var
);