530f79d4dffb2b32c5f599c339de6a6d7d201f29
[mesa.git] / src / compiler / glsl / lower_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_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 * LowerCombinedClipCullDistance flag in gl_shader_compiler_options to true.
46 */
47
48 #include "main/macros.h"
49 #include "glsl_symbol_table.h"
50 #include "ir_rvalue_visitor.h"
51 #include "ir.h"
52 #include "program/prog_instruction.h" /* For WRITEMASK_* */
53
54 #define GLSL_CLIP_VAR_NAME "gl_ClipDistanceMESA"
55
56 namespace {
57
58 class lower_distance_visitor : public ir_rvalue_visitor {
59 public:
60 explicit lower_distance_visitor(gl_shader_stage shader_stage,
61 const char *in_name, int total_size,
62 int offset)
63 : progress(false), old_distance_out_var(NULL),
64 old_distance_in_var(NULL), new_distance_out_var(NULL),
65 new_distance_in_var(NULL), shader_stage(shader_stage),
66 in_name(in_name), total_size(total_size), offset(offset)
67 {
68 }
69
70 explicit lower_distance_visitor(gl_shader_stage shader_stage,
71 const char *in_name,
72 const lower_distance_visitor *orig,
73 int offset)
74 : progress(false),
75 old_distance_out_var(NULL),
76 old_distance_in_var(NULL),
77 new_distance_out_var(orig->new_distance_out_var),
78 new_distance_in_var(orig->new_distance_in_var),
79 shader_stage(shader_stage),
80 in_name(in_name),
81 total_size(orig->total_size),
82 offset(offset)
83 {
84 }
85
86 virtual ir_visitor_status visit(ir_variable *);
87 void create_indices(ir_rvalue*, ir_rvalue *&, ir_rvalue *&);
88 bool is_distance_vec8(ir_rvalue *ir);
89 ir_rvalue *lower_distance_vec8(ir_rvalue *ir);
90 virtual ir_visitor_status visit_leave(ir_assignment *);
91 void visit_new_assignment(ir_assignment *ir);
92 virtual ir_visitor_status visit_leave(ir_call *);
93
94 virtual void handle_rvalue(ir_rvalue **rvalue);
95
96 void fix_lhs(ir_assignment *);
97
98 bool progress;
99
100 /**
101 * Pointer to the declaration of gl_ClipDistance, if found.
102 *
103 * Note:
104 *
105 * - the in_var is for geometry and both tessellation shader inputs only.
106 *
107 * - since gl_ClipDistance is available in tessellation control,
108 * tessellation evaluation and geometry shaders as both an input
109 * and an output, it's possible for both old_distance_out_var
110 * and old_distance_in_var to be non-null.
111 */
112 ir_variable *old_distance_out_var;
113 ir_variable *old_distance_in_var;
114
115 /**
116 * Pointer to the newly-created gl_ClipDistanceMESA variable.
117 */
118 ir_variable *new_distance_out_var;
119 ir_variable *new_distance_in_var;
120
121 /**
122 * Type of shader we are compiling (e.g. MESA_SHADER_VERTEX)
123 */
124 const gl_shader_stage shader_stage;
125 const char *in_name;
126 int total_size;
127 int offset;
128 };
129
130 } /* anonymous namespace */
131
132 /**
133 * Replace any declaration of 'in_name' as an array of floats with a
134 * declaration of gl_ClipDistanceMESA as an array of vec4's.
135 */
136 ir_visitor_status
137 lower_distance_visitor::visit(ir_variable *ir)
138 {
139 ir_variable **old_var;
140 ir_variable **new_var;
141
142 if (!ir->name || strcmp(ir->name, in_name) != 0)
143 return visit_continue;
144 assert (ir->type->is_array());
145
146 if (ir->data.mode == ir_var_shader_out) {
147 if (this->old_distance_out_var)
148 return visit_continue;
149 old_var = &old_distance_out_var;
150 new_var = &new_distance_out_var;
151 } else if (ir->data.mode == ir_var_shader_in) {
152 if (this->old_distance_in_var)
153 return visit_continue;
154 old_var = &old_distance_in_var;
155 new_var = &new_distance_in_var;
156 } else {
157 unreachable("not reached");
158 }
159
160 this->progress = true;
161
162 *old_var = ir;
163
164 if (!(*new_var)) {
165 unsigned new_size = (total_size + 3) / 4;
166
167 /* Clone the old var so that we inherit all of its properties */
168 *new_var = ir->clone(ralloc_parent(ir), NULL);
169 (*new_var)->name = ralloc_strdup(*new_var, GLSL_CLIP_VAR_NAME);
170 (*new_var)->data.location = VARYING_SLOT_CLIP_DIST0;
171
172 if (!ir->type->fields.array->is_array()) {
173 /* gl_ClipDistance (used for vertex, tessellation evaluation and
174 * geometry output, and fragment input).
175 */
176 assert((ir->data.mode == ir_var_shader_in &&
177 this->shader_stage == MESA_SHADER_FRAGMENT) ||
178 (ir->data.mode == ir_var_shader_out &&
179 (this->shader_stage == MESA_SHADER_VERTEX ||
180 this->shader_stage == MESA_SHADER_TESS_EVAL ||
181 this->shader_stage == MESA_SHADER_GEOMETRY)));
182
183 assert (ir->type->fields.array == glsl_type::float_type);
184 (*new_var)->data.max_array_access = new_size - 1;
185
186 /* And change the properties that we need to change */
187 (*new_var)->type = glsl_type::get_array_instance(glsl_type::vec4_type,
188 new_size);
189 } else {
190 /* 2D gl_ClipDistance (used for tessellation control, tessellation
191 * evaluation and geometry input, and tessellation control output).
192 */
193 assert((ir->data.mode == ir_var_shader_in &&
194 (this->shader_stage == MESA_SHADER_GEOMETRY ||
195 this->shader_stage == MESA_SHADER_TESS_EVAL)) ||
196 this->shader_stage == MESA_SHADER_TESS_CTRL);
197
198 assert (ir->type->fields.array->fields.array == glsl_type::float_type);
199
200 /* And change the properties that we need to change */
201 (*new_var)->type = glsl_type::get_array_instance(
202 glsl_type::get_array_instance(glsl_type::vec4_type,
203 new_size),
204 ir->type->array_size());
205 }
206 ir->replace_with(*new_var);
207 } else {
208 ir->remove();
209 }
210
211 return visit_continue;
212 }
213
214
215 /**
216 * Create the necessary GLSL rvalues to index into gl_ClipDistanceMESA based
217 * on the rvalue previously used to index into gl_ClipDistance.
218 *
219 * \param array_index Selects one of the vec4's in gl_ClipDistanceMESA
220 * \param swizzle_index Selects a component within the vec4 selected by
221 * array_index.
222 */
223 void
224 lower_distance_visitor::create_indices(ir_rvalue *old_index,
225 ir_rvalue *&array_index,
226 ir_rvalue *&swizzle_index)
227 {
228 void *ctx = ralloc_parent(old_index);
229
230 /* Make sure old_index is a signed int so that the bitwise "shift" and
231 * "and" operations below type check properly.
232 */
233 if (old_index->type != glsl_type::int_type) {
234 assert (old_index->type == glsl_type::uint_type);
235 old_index = new(ctx) ir_expression(ir_unop_u2i, old_index);
236 }
237
238 ir_constant *old_index_constant =
239 old_index->constant_expression_value(ctx);
240 if (old_index_constant) {
241 /* gl_ClipDistance is being accessed via a constant index. Don't bother
242 * creating expressions to calculate the lowered indices. Just create
243 * constants.
244 */
245 int const_val = old_index_constant->get_int_component(0) + offset;
246 array_index = new(ctx) ir_constant(const_val / 4);
247 swizzle_index = new(ctx) ir_constant(const_val % 4);
248 } else {
249 /* Create a variable to hold the value of old_index (so that we
250 * don't compute it twice).
251 */
252 ir_variable *old_index_var = new(ctx) ir_variable(
253 glsl_type::int_type, "distance_index", ir_var_temporary);
254 this->base_ir->insert_before(old_index_var);
255 this->base_ir->insert_before(new(ctx) ir_assignment(
256 new(ctx) ir_dereference_variable(old_index_var), old_index));
257
258 /* Create the expression distance_index / 4. Do this as a bit
259 * shift because that's likely to be more efficient.
260 */
261 array_index = new(ctx) ir_expression(
262 ir_binop_rshift,
263 new(ctx) ir_expression(ir_binop_add,
264 new(ctx) ir_dereference_variable(old_index_var),
265 new(ctx) ir_constant(offset)),
266 new(ctx) ir_constant(2));
267
268 /* Create the expression distance_index % 4. Do this as a bitwise
269 * AND because that's likely to be more efficient.
270 */
271 swizzle_index = new(ctx) ir_expression(
272 ir_binop_bit_and,
273 new(ctx) ir_expression(ir_binop_add,
274 new(ctx) ir_dereference_variable(old_index_var),
275 new(ctx) ir_constant(offset)),
276 new(ctx) ir_constant(3));
277 }
278 }
279
280
281 /**
282 * Determine whether the given rvalue describes an array of 8 floats that
283 * needs to be lowered to an array of 2 vec4's; that is, determine whether it
284 * matches one of the following patterns:
285 *
286 * - gl_ClipDistance (if gl_ClipDistance is 1D)
287 * - gl_ClipDistance[i] (if gl_ClipDistance is 2D)
288 */
289 bool
290 lower_distance_visitor::is_distance_vec8(ir_rvalue *ir)
291 {
292 /* Note that geometry shaders contain gl_ClipDistance both as an input
293 * (which is a 2D array) and an output (which is a 1D array), so it's
294 * possible for both this->old_distance_out_var and
295 * this->old_distance_in_var to be non-NULL in the same shader.
296 */
297
298 if (!ir->type->is_array())
299 return false;
300 if (ir->type->fields.array != glsl_type::float_type)
301 return false;
302
303 if (this->old_distance_out_var) {
304 if (ir->variable_referenced() == this->old_distance_out_var)
305 return true;
306 }
307 if (this->old_distance_in_var) {
308 assert(this->shader_stage == MESA_SHADER_TESS_CTRL ||
309 this->shader_stage == MESA_SHADER_TESS_EVAL ||
310 this->shader_stage == MESA_SHADER_GEOMETRY ||
311 this->shader_stage == MESA_SHADER_FRAGMENT);
312
313 if (ir->variable_referenced() == this->old_distance_in_var)
314 return true;
315 }
316 return false;
317 }
318
319
320 /**
321 * If the given ir satisfies is_distance_vec8(), return new ir
322 * representing its lowered equivalent. That is, map:
323 *
324 * - gl_ClipDistance => gl_ClipDistanceMESA (if gl_ClipDistance is 1D)
325 * - gl_ClipDistance[i] => gl_ClipDistanceMESA[i] (if gl_ClipDistance is 2D)
326 *
327 * Otherwise return NULL.
328 */
329 ir_rvalue *
330 lower_distance_visitor::lower_distance_vec8(ir_rvalue *ir)
331 {
332 if (!ir->type->is_array())
333 return NULL;
334 if (ir->type->fields.array != glsl_type::float_type)
335 return NULL;
336
337 ir_variable **new_var = NULL;
338 if (this->old_distance_out_var) {
339 if (ir->variable_referenced() == this->old_distance_out_var)
340 new_var = &this->new_distance_out_var;
341 }
342 if (this->old_distance_in_var) {
343 if (ir->variable_referenced() == this->old_distance_in_var)
344 new_var = &this->new_distance_in_var;
345 }
346 if (new_var == NULL)
347 return NULL;
348
349 if (ir->as_dereference_variable()) {
350 return new(ralloc_parent(ir)) ir_dereference_variable(*new_var);
351 } else {
352 ir_dereference_array *array_ref = ir->as_dereference_array();
353 assert(array_ref);
354 assert(array_ref->array->as_dereference_variable());
355
356 return new(ralloc_parent(ir))
357 ir_dereference_array(*new_var, array_ref->array_index);
358 }
359 }
360
361
362 void
363 lower_distance_visitor::handle_rvalue(ir_rvalue **rv)
364 {
365 if (*rv == NULL)
366 return;
367
368 ir_dereference_array *const array_deref = (*rv)->as_dereference_array();
369 if (array_deref == NULL)
370 return;
371
372 /* Replace any expression that indexes one of the floats in gl_ClipDistance
373 * with an expression that indexes into one of the vec4's in
374 * gl_ClipDistanceMESA and accesses the appropriate component.
375 */
376 ir_rvalue *lowered_vec8 =
377 this->lower_distance_vec8(array_deref->array);
378 if (lowered_vec8 != NULL) {
379 this->progress = true;
380 ir_rvalue *array_index;
381 ir_rvalue *swizzle_index;
382 this->create_indices(array_deref->array_index, array_index, swizzle_index);
383 void *mem_ctx = ralloc_parent(array_deref);
384
385 ir_dereference_array *const new_array_deref =
386 new(mem_ctx) ir_dereference_array(lowered_vec8, array_index);
387
388 ir_expression *const expr =
389 new(mem_ctx) ir_expression(ir_binop_vector_extract,
390 new_array_deref,
391 swizzle_index);
392
393 *rv = expr;
394 }
395 }
396
397 void
398 lower_distance_visitor::fix_lhs(ir_assignment *ir)
399 {
400 if (ir->lhs->ir_type == ir_type_expression) {
401 void *mem_ctx = ralloc_parent(ir);
402 ir_expression *const expr = (ir_expression *) ir->lhs;
403
404 /* The expression must be of the form:
405 *
406 * (vector_extract gl_ClipDistanceMESA[i], j).
407 */
408 assert(expr->operation == ir_binop_vector_extract);
409 assert(expr->operands[0]->ir_type == ir_type_dereference_array);
410 assert(expr->operands[0]->type == glsl_type::vec4_type);
411
412 ir_dereference *const new_lhs = (ir_dereference *) expr->operands[0];
413 ir->rhs = new(mem_ctx) ir_expression(ir_triop_vector_insert,
414 glsl_type::vec4_type,
415 new_lhs->clone(mem_ctx, NULL),
416 ir->rhs,
417 expr->operands[1]);
418 ir->set_lhs(new_lhs);
419 ir->write_mask = WRITEMASK_XYZW;
420 }
421 }
422
423 /**
424 * Replace any assignment having the 1D gl_ClipDistance (undereferenced) as
425 * its LHS or RHS with a sequence of assignments, one for each component of
426 * the array. Each of these assignments is lowered to refer to
427 * gl_ClipDistanceMESA as appropriate.
428 *
429 * We need to do a similar replacement for 2D gl_ClipDistance, however since
430 * it's an input, the only case we need to address is where a 1D slice of it
431 * is the entire RHS of an assignment, e.g.:
432 *
433 * foo = gl_in[i].gl_ClipDistance
434 */
435 ir_visitor_status
436 lower_distance_visitor::visit_leave(ir_assignment *ir)
437 {
438 /* First invoke the base class visitor. This causes handle_rvalue() to be
439 * called on ir->rhs and ir->condition.
440 */
441 ir_rvalue_visitor::visit_leave(ir);
442
443 if (this->is_distance_vec8(ir->lhs) ||
444 this->is_distance_vec8(ir->rhs)) {
445 /* LHS or RHS of the assignment is the entire 1D gl_ClipDistance array
446 * (or a 1D slice of a 2D gl_ClipDistance input array). Since we are
447 * reshaping gl_ClipDistance from an array of floats to an array of
448 * vec4's, this isn't going to work as a bulk assignment anymore, so
449 * unroll it to element-by-element assignments and lower each of them.
450 *
451 * Note: to unroll into element-by-element assignments, we need to make
452 * clones of the LHS and RHS. This is safe because expressions and
453 * l-values are side-effect free.
454 */
455 void *ctx = ralloc_parent(ir);
456 int array_size = ir->lhs->type->array_size();
457 for (int i = 0; i < array_size; ++i) {
458 ir_dereference_array *new_lhs = new(ctx) ir_dereference_array(
459 ir->lhs->clone(ctx, NULL), new(ctx) ir_constant(i));
460 ir_dereference_array *new_rhs = new(ctx) ir_dereference_array(
461 ir->rhs->clone(ctx, NULL), new(ctx) ir_constant(i));
462 this->handle_rvalue((ir_rvalue **) &new_rhs);
463
464 /* Handle the LHS after creating the new assignment. This must
465 * happen in this order because handle_rvalue may replace the old LHS
466 * with an ir_expression of ir_binop_vector_extract. Since this is
467 * not a valide l-value, this will cause an assertion in the
468 * ir_assignment constructor to fail.
469 *
470 * If this occurs, replace the mangled LHS with a dereference of the
471 * vector, and replace the RHS with an ir_triop_vector_insert.
472 */
473 ir_assignment *const assign = new(ctx) ir_assignment(new_lhs, new_rhs);
474 this->handle_rvalue((ir_rvalue **) &assign->lhs);
475 this->fix_lhs(assign);
476
477 this->base_ir->insert_before(assign);
478 }
479 ir->remove();
480
481 return visit_continue;
482 }
483
484 /* Handle the LHS as if it were an r-value. Normally
485 * rvalue_visit(ir_assignment *) only visits the RHS, but we need to lower
486 * expressions in the LHS as well.
487 *
488 * This may cause the LHS to get replaced with an ir_expression of
489 * ir_binop_vector_extract. If this occurs, replace it with a dereference
490 * of the vector, and replace the RHS with an ir_triop_vector_insert.
491 */
492 handle_rvalue((ir_rvalue **)&ir->lhs);
493 this->fix_lhs(ir);
494
495 return rvalue_visit(ir);
496 }
497
498
499 /**
500 * Set up base_ir properly and call visit_leave() on a newly created
501 * ir_assignment node. This is used in cases where we have to insert an
502 * ir_assignment in a place where we know the hierarchical visitor won't see
503 * it.
504 */
505 void
506 lower_distance_visitor::visit_new_assignment(ir_assignment *ir)
507 {
508 ir_instruction *old_base_ir = this->base_ir;
509 this->base_ir = ir;
510 ir->accept(this);
511 this->base_ir = old_base_ir;
512 }
513
514
515 /**
516 * If a 1D gl_ClipDistance variable appears as an argument in an ir_call
517 * expression, replace it with a temporary variable, and make sure the ir_call
518 * is preceded and/or followed by assignments that copy the contents of the
519 * temporary variable to and/or from gl_ClipDistance. Each of these
520 * assignments is then lowered to refer to gl_ClipDistanceMESA.
521 *
522 * We need to do a similar replacement for 2D gl_ClipDistance, however since
523 * it's an input, the only case we need to address is where a 1D slice of it
524 * is passed as an "in" parameter to an ir_call, e.g.:
525 *
526 * foo(gl_in[i].gl_ClipDistance)
527 */
528 ir_visitor_status
529 lower_distance_visitor::visit_leave(ir_call *ir)
530 {
531 void *ctx = ralloc_parent(ir);
532
533 const exec_node *formal_param_node = ir->callee->parameters.get_head_raw();
534 const exec_node *actual_param_node = ir->actual_parameters.get_head_raw();
535 while (!actual_param_node->is_tail_sentinel()) {
536 ir_variable *formal_param = (ir_variable *) formal_param_node;
537 ir_rvalue *actual_param = (ir_rvalue *) actual_param_node;
538
539 /* Advance formal_param_node and actual_param_node now so that we can
540 * safely replace actual_param with another node, if necessary, below.
541 */
542 formal_param_node = formal_param_node->next;
543 actual_param_node = actual_param_node->next;
544
545 if (this->is_distance_vec8(actual_param)) {
546 /* User is trying to pass the whole 1D gl_ClipDistance array (or a 1D
547 * slice of a 2D gl_ClipDistance array) to a function call. Since we
548 * are reshaping gl_ClipDistance from an array of floats to an array
549 * of vec4's, this isn't going to work anymore, so use a temporary
550 * array instead.
551 */
552 ir_variable *temp_clip_distance = new(ctx) ir_variable(
553 actual_param->type, "temp_clip_distance", ir_var_temporary);
554 this->base_ir->insert_before(temp_clip_distance);
555 actual_param->replace_with(
556 new(ctx) ir_dereference_variable(temp_clip_distance));
557 if (formal_param->data.mode == ir_var_function_in
558 || formal_param->data.mode == ir_var_function_inout) {
559 /* Copy from gl_ClipDistance to the temporary before the call.
560 * Since we are going to insert this copy before the current
561 * instruction, we need to visit it afterwards to make sure it
562 * gets lowered.
563 */
564 ir_assignment *new_assignment = new(ctx) ir_assignment(
565 new(ctx) ir_dereference_variable(temp_clip_distance),
566 actual_param->clone(ctx, NULL));
567 this->base_ir->insert_before(new_assignment);
568 this->visit_new_assignment(new_assignment);
569 }
570 if (formal_param->data.mode == ir_var_function_out
571 || formal_param->data.mode == ir_var_function_inout) {
572 /* Copy from the temporary to gl_ClipDistance after the call.
573 * Since visit_list_elements() has already decided which
574 * instruction it's going to visit next, we need to visit
575 * afterwards to make sure it gets lowered.
576 */
577 ir_assignment *new_assignment = new(ctx) ir_assignment(
578 actual_param->clone(ctx, NULL),
579 new(ctx) ir_dereference_variable(temp_clip_distance));
580 this->base_ir->insert_after(new_assignment);
581 this->visit_new_assignment(new_assignment);
582 }
583 }
584 }
585
586 return rvalue_visit(ir);
587 }
588
589 namespace {
590 class lower_distance_visitor_counter : public ir_rvalue_visitor {
591 public:
592 explicit lower_distance_visitor_counter(void)
593 : in_clip_size(0), in_cull_size(0),
594 out_clip_size(0), out_cull_size(0)
595 {
596 }
597
598 virtual ir_visitor_status visit(ir_variable *);
599 virtual void handle_rvalue(ir_rvalue **rvalue);
600
601 int in_clip_size;
602 int in_cull_size;
603 int out_clip_size;
604 int out_cull_size;
605 };
606
607 }
608 /**
609 * Count gl_ClipDistance and gl_CullDistance sizes.
610 */
611 ir_visitor_status
612 lower_distance_visitor_counter::visit(ir_variable *ir)
613 {
614 int *clip_size, *cull_size;
615
616 if (!ir->name)
617 return visit_continue;
618
619 if (ir->data.mode == ir_var_shader_out) {
620 clip_size = &out_clip_size;
621 cull_size = &out_cull_size;
622 } else if (ir->data.mode == ir_var_shader_in) {
623 clip_size = &in_clip_size;
624 cull_size = &in_cull_size;
625 } else
626 return visit_continue;
627
628 if (ir->type->is_unsized_array())
629 return visit_continue;
630
631 if (*clip_size == 0) {
632 if (!strcmp(ir->name, "gl_ClipDistance")) {
633 if (!ir->type->fields.array->is_array())
634 *clip_size = ir->type->array_size();
635 else
636 *clip_size = ir->type->fields.array->array_size();
637 }
638 }
639
640 if (*cull_size == 0) {
641 if (!strcmp(ir->name, "gl_CullDistance")) {
642 if (!ir->type->fields.array->is_array())
643 *cull_size = ir->type->array_size();
644 else
645 *cull_size = ir->type->fields.array->array_size();
646 }
647 }
648 return visit_continue;
649 }
650
651 void
652 lower_distance_visitor_counter::handle_rvalue(ir_rvalue **)
653 {
654 return;
655 }
656
657 bool
658 lower_clip_cull_distance(struct gl_shader_program *prog,
659 struct gl_linked_shader *shader)
660 {
661 int clip_size, cull_size;
662
663 lower_distance_visitor_counter count;
664 visit_list_elements(&count, shader->ir);
665
666 clip_size = MAX2(count.in_clip_size, count.out_clip_size);
667 cull_size = MAX2(count.in_cull_size, count.out_cull_size);
668
669 if (clip_size == 0 && cull_size == 0)
670 return false;
671
672 lower_distance_visitor v(shader->Stage, "gl_ClipDistance", clip_size + cull_size, 0);
673 visit_list_elements(&v, shader->ir);
674
675 lower_distance_visitor v2(shader->Stage, "gl_CullDistance", &v, clip_size);
676 visit_list_elements(&v2, shader->ir);
677
678 if (v2.new_distance_out_var)
679 shader->symbols->add_variable(v2.new_distance_out_var);
680 if (v2.new_distance_in_var)
681 shader->symbols->add_variable(v2.new_distance_in_var);
682
683 return v2.progress;
684 }