glsl: Modify strategy for accumulating conditions when lowering if-statements
[mesa.git] / src / glsl / ast_function.cpp
1 /*
2 * Copyright © 2010 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 #include "glsl_symbol_table.h"
25 #include "ast.h"
26 #include "glsl_types.h"
27 #include "ir.h"
28 #include "main/core.h" /* for MIN2 */
29
30 static ir_rvalue *
31 convert_component(ir_rvalue *src, const glsl_type *desired_type);
32
33 bool
34 apply_implicit_conversion(const glsl_type *to, ir_rvalue * &from,
35 struct _mesa_glsl_parse_state *state);
36
37 static unsigned
38 process_parameters(exec_list *instructions, exec_list *actual_parameters,
39 exec_list *parameters,
40 struct _mesa_glsl_parse_state *state)
41 {
42 unsigned count = 0;
43
44 foreach_list (n, parameters) {
45 ast_node *const ast = exec_node_data(ast_node, n, link);
46 ir_rvalue *result = ast->hir(instructions, state);
47
48 ir_constant *const constant = result->constant_expression_value();
49 if (constant != NULL)
50 result = constant;
51
52 actual_parameters->push_tail(result);
53 count++;
54 }
55
56 return count;
57 }
58
59
60 /**
61 * Generate a source prototype for a function signature
62 *
63 * \param return_type Return type of the function. May be \c NULL.
64 * \param name Name of the function.
65 * \param parameters List of \c ir_instruction nodes representing the
66 * parameter list for the function. This may be either a
67 * formal (\c ir_variable) or actual (\c ir_rvalue)
68 * parameter list. Only the type is used.
69 *
70 * \return
71 * A ralloced string representing the prototype of the function.
72 */
73 char *
74 prototype_string(const glsl_type *return_type, const char *name,
75 exec_list *parameters)
76 {
77 char *str = NULL;
78
79 if (return_type != NULL)
80 str = ralloc_asprintf(NULL, "%s ", return_type->name);
81
82 ralloc_asprintf_append(&str, "%s(", name);
83
84 const char *comma = "";
85 foreach_list(node, parameters) {
86 const ir_instruction *const param = (ir_instruction *) node;
87
88 ralloc_asprintf_append(&str, "%s%s", comma, param->type->name);
89 comma = ", ";
90 }
91
92 ralloc_strcat(&str, ")");
93 return str;
94 }
95
96
97 static ir_rvalue *
98 match_function_by_name(exec_list *instructions, const char *name,
99 YYLTYPE *loc, exec_list *actual_parameters,
100 struct _mesa_glsl_parse_state *state)
101 {
102 void *ctx = state;
103 ir_function *f = state->symbols->get_function(name);
104 ir_function_signature *sig;
105
106 sig = f ? f->matching_signature(actual_parameters) : NULL;
107
108 /* FINISHME: This doesn't handle the case where shader X contains a
109 * FINISHME: matching signature but shader X + N contains an _exact_
110 * FINISHME: matching signature.
111 */
112 if (sig == NULL
113 && (f == NULL || state->es_shader || !f->has_user_signature())
114 && state->symbols->get_type(name) == NULL
115 && (state->language_version == 110
116 || state->symbols->get_variable(name) == NULL)) {
117 /* The current shader doesn't contain a matching function or signature.
118 * Before giving up, look for the prototype in the built-in functions.
119 */
120 for (unsigned i = 0; i < state->num_builtins_to_link; i++) {
121 ir_function *builtin;
122 builtin = state->builtins_to_link[i]->symbols->get_function(name);
123 sig = builtin ? builtin->matching_signature(actual_parameters) : NULL;
124 if (sig != NULL) {
125 if (f == NULL) {
126 f = new(ctx) ir_function(name);
127 state->symbols->add_global_function(f);
128 emit_function(state, f);
129 }
130
131 f->add_signature(sig->clone_prototype(f, NULL));
132 break;
133 }
134 }
135 }
136
137 if (sig != NULL) {
138 /* Verify that 'out' and 'inout' actual parameters are lvalues. This
139 * isn't done in ir_function::matching_signature because that function
140 * cannot generate the necessary diagnostics.
141 *
142 * Also, validate that 'const_in' formal parameters (an extension of our
143 * IR) correspond to ir_constant actual parameters.
144 */
145 exec_list_iterator actual_iter = actual_parameters->iterator();
146 exec_list_iterator formal_iter = sig->parameters.iterator();
147
148 while (actual_iter.has_next()) {
149 ir_rvalue *actual = (ir_rvalue *) actual_iter.get();
150 ir_variable *formal = (ir_variable *) formal_iter.get();
151
152 assert(actual != NULL);
153 assert(formal != NULL);
154
155 if (formal->mode == ir_var_const_in && !actual->as_constant()) {
156 _mesa_glsl_error(loc, state,
157 "parameter `%s' must be a constant expression",
158 formal->name);
159 }
160
161 if ((formal->mode == ir_var_out)
162 || (formal->mode == ir_var_inout)) {
163 const char *mode = NULL;
164 switch (formal->mode) {
165 case ir_var_out: mode = "out"; break;
166 case ir_var_inout: mode = "inout"; break;
167 default: assert(false); break;
168 }
169 /* FIXME: 'loc' is incorrect (as of 2011-01-21). It is always
170 * FIXME: 0:0(0).
171 */
172 if (actual->variable_referenced()
173 && actual->variable_referenced()->read_only) {
174 _mesa_glsl_error(loc, state,
175 "function parameter '%s %s' references the "
176 "read-only variable '%s'",
177 mode, formal->name,
178 actual->variable_referenced()->name);
179
180 } else if (!actual->is_lvalue()) {
181 _mesa_glsl_error(loc, state,
182 "function parameter '%s %s' is not an lvalue",
183 mode, formal->name);
184 }
185 }
186
187 if (formal->type->is_numeric() || formal->type->is_boolean()) {
188 ir_rvalue *converted = convert_component(actual, formal->type);
189 actual->replace_with(converted);
190 }
191
192 actual_iter.next();
193 formal_iter.next();
194 }
195
196 /* Always insert the call in the instruction stream, and return a deref
197 * of its return val if it returns a value, since we don't know if
198 * the rvalue is going to be assigned to anything or not.
199 */
200 ir_call *call = new(ctx) ir_call(sig, actual_parameters);
201 if (!sig->return_type->is_void()) {
202 /* If the function call is a constant expression, don't
203 * generate the instructions to call it; just generate an
204 * ir_constant representing the constant value.
205 *
206 * Function calls can only be constant expressions starting
207 * in GLSL 1.20.
208 */
209 if (state->language_version >= 120) {
210 ir_constant *const_val = call->constant_expression_value();
211 if (const_val) {
212 return const_val;
213 }
214 }
215
216 ir_variable *var;
217 ir_dereference_variable *deref;
218
219 var = new(ctx) ir_variable(sig->return_type,
220 ralloc_asprintf(ctx, "%s_retval",
221 sig->function_name()),
222 ir_var_temporary);
223 instructions->push_tail(var);
224
225 deref = new(ctx) ir_dereference_variable(var);
226 ir_assignment *assign = new(ctx) ir_assignment(deref, call, NULL);
227 instructions->push_tail(assign);
228
229 deref = new(ctx) ir_dereference_variable(var);
230 return deref;
231 } else {
232 instructions->push_tail(call);
233 return NULL;
234 }
235 } else {
236 char *str = prototype_string(NULL, name, actual_parameters);
237
238 _mesa_glsl_error(loc, state, "no matching function for call to `%s'",
239 str);
240 ralloc_free(str);
241
242 const char *prefix = "candidates are: ";
243
244 for (int i = -1; i < (int) state->num_builtins_to_link; i++) {
245 glsl_symbol_table *syms = i >= 0 ? state->builtins_to_link[i]->symbols
246 : state->symbols;
247 f = syms->get_function(name);
248 if (f == NULL)
249 continue;
250
251 foreach_list (node, &f->signatures) {
252 ir_function_signature *sig = (ir_function_signature *) node;
253
254 str = prototype_string(sig->return_type, f->name, &sig->parameters);
255 _mesa_glsl_error(loc, state, "%s%s", prefix, str);
256 ralloc_free(str);
257
258 prefix = " ";
259 }
260
261 }
262
263 return ir_call::get_error_instruction(ctx);
264 }
265 }
266
267
268 /**
269 * Perform automatic type conversion of constructor parameters
270 *
271 * This implements the rules in the "Conversion and Scalar Constructors"
272 * section (GLSL 1.10 section 5.4.1), not the "Implicit Conversions" rules.
273 */
274 static ir_rvalue *
275 convert_component(ir_rvalue *src, const glsl_type *desired_type)
276 {
277 void *ctx = ralloc_parent(src);
278 const unsigned a = desired_type->base_type;
279 const unsigned b = src->type->base_type;
280 ir_expression *result = NULL;
281
282 if (src->type->is_error())
283 return src;
284
285 assert(a <= GLSL_TYPE_BOOL);
286 assert(b <= GLSL_TYPE_BOOL);
287
288 if (a == b)
289 return src;
290
291 switch (a) {
292 case GLSL_TYPE_UINT:
293 switch (b) {
294 case GLSL_TYPE_INT:
295 result = new(ctx) ir_expression(ir_unop_i2u, src);
296 break;
297 case GLSL_TYPE_FLOAT:
298 result = new(ctx) ir_expression(ir_unop_i2u,
299 new(ctx) ir_expression(ir_unop_f2i, src));
300 break;
301 case GLSL_TYPE_BOOL:
302 result = new(ctx) ir_expression(ir_unop_i2u,
303 new(ctx) ir_expression(ir_unop_b2i, src));
304 break;
305 }
306 break;
307 case GLSL_TYPE_INT:
308 switch (b) {
309 case GLSL_TYPE_UINT:
310 result = new(ctx) ir_expression(ir_unop_u2i, src);
311 break;
312 case GLSL_TYPE_FLOAT:
313 result = new(ctx) ir_expression(ir_unop_f2i, src);
314 break;
315 case GLSL_TYPE_BOOL:
316 result = new(ctx) ir_expression(ir_unop_b2i, src);
317 break;
318 }
319 break;
320 case GLSL_TYPE_FLOAT:
321 switch (b) {
322 case GLSL_TYPE_UINT:
323 result = new(ctx) ir_expression(ir_unop_u2f, desired_type, src, NULL);
324 break;
325 case GLSL_TYPE_INT:
326 result = new(ctx) ir_expression(ir_unop_i2f, desired_type, src, NULL);
327 break;
328 case GLSL_TYPE_BOOL:
329 result = new(ctx) ir_expression(ir_unop_b2f, desired_type, src, NULL);
330 break;
331 }
332 break;
333 case GLSL_TYPE_BOOL:
334 switch (b) {
335 case GLSL_TYPE_UINT:
336 result = new(ctx) ir_expression(ir_unop_i2b,
337 new(ctx) ir_expression(ir_unop_u2i, src));
338 break;
339 case GLSL_TYPE_INT:
340 result = new(ctx) ir_expression(ir_unop_i2b, desired_type, src, NULL);
341 break;
342 case GLSL_TYPE_FLOAT:
343 result = new(ctx) ir_expression(ir_unop_f2b, desired_type, src, NULL);
344 break;
345 }
346 break;
347 }
348
349 assert(result != NULL);
350 assert(result->type == desired_type);
351
352 /* Try constant folding; it may fold in the conversion we just added. */
353 ir_constant *const constant = result->constant_expression_value();
354 return (constant != NULL) ? (ir_rvalue *) constant : (ir_rvalue *) result;
355 }
356
357 /**
358 * Dereference a specific component from a scalar, vector, or matrix
359 */
360 static ir_rvalue *
361 dereference_component(ir_rvalue *src, unsigned component)
362 {
363 void *ctx = ralloc_parent(src);
364 assert(component < src->type->components());
365
366 /* If the source is a constant, just create a new constant instead of a
367 * dereference of the existing constant.
368 */
369 ir_constant *constant = src->as_constant();
370 if (constant)
371 return new(ctx) ir_constant(constant, component);
372
373 if (src->type->is_scalar()) {
374 return src;
375 } else if (src->type->is_vector()) {
376 return new(ctx) ir_swizzle(src, component, 0, 0, 0, 1);
377 } else {
378 assert(src->type->is_matrix());
379
380 /* Dereference a row of the matrix, then call this function again to get
381 * a specific element from that row.
382 */
383 const int c = component / src->type->column_type()->vector_elements;
384 const int r = component % src->type->column_type()->vector_elements;
385 ir_constant *const col_index = new(ctx) ir_constant(c);
386 ir_dereference *const col = new(ctx) ir_dereference_array(src, col_index);
387
388 col->type = src->type->column_type();
389
390 return dereference_component(col, r);
391 }
392
393 assert(!"Should not get here.");
394 return NULL;
395 }
396
397
398 static ir_rvalue *
399 process_array_constructor(exec_list *instructions,
400 const glsl_type *constructor_type,
401 YYLTYPE *loc, exec_list *parameters,
402 struct _mesa_glsl_parse_state *state)
403 {
404 void *ctx = state;
405 /* Array constructors come in two forms: sized and unsized. Sized array
406 * constructors look like 'vec4[2](a, b)', where 'a' and 'b' are vec4
407 * variables. In this case the number of parameters must exactly match the
408 * specified size of the array.
409 *
410 * Unsized array constructors look like 'vec4[](a, b)', where 'a' and 'b'
411 * are vec4 variables. In this case the size of the array being constructed
412 * is determined by the number of parameters.
413 *
414 * From page 52 (page 58 of the PDF) of the GLSL 1.50 spec:
415 *
416 * "There must be exactly the same number of arguments as the size of
417 * the array being constructed. If no size is present in the
418 * constructor, then the array is explicitly sized to the number of
419 * arguments provided. The arguments are assigned in order, starting at
420 * element 0, to the elements of the constructed array. Each argument
421 * must be the same type as the element type of the array, or be a type
422 * that can be converted to the element type of the array according to
423 * Section 4.1.10 "Implicit Conversions.""
424 */
425 exec_list actual_parameters;
426 const unsigned parameter_count =
427 process_parameters(instructions, &actual_parameters, parameters, state);
428
429 if ((parameter_count == 0)
430 || ((constructor_type->length != 0)
431 && (constructor_type->length != parameter_count))) {
432 const unsigned min_param = (constructor_type->length == 0)
433 ? 1 : constructor_type->length;
434
435 _mesa_glsl_error(loc, state, "array constructor must have %s %u "
436 "parameter%s",
437 (constructor_type->length != 0) ? "at least" : "exactly",
438 min_param, (min_param <= 1) ? "" : "s");
439 return ir_call::get_error_instruction(ctx);
440 }
441
442 if (constructor_type->length == 0) {
443 constructor_type =
444 glsl_type::get_array_instance(constructor_type->element_type(),
445 parameter_count);
446 assert(constructor_type != NULL);
447 assert(constructor_type->length == parameter_count);
448 }
449
450 bool all_parameters_are_constant = true;
451
452 /* Type cast each parameter and, if possible, fold constants. */
453 foreach_list_safe(n, &actual_parameters) {
454 ir_rvalue *ir = (ir_rvalue *) n;
455 ir_rvalue *result = ir;
456
457 /* Apply implicit conversions (not the scalar constructor rules!). See
458 * the spec quote above. */
459 if (constructor_type->element_type()->is_float()) {
460 const glsl_type *desired_type =
461 glsl_type::get_instance(GLSL_TYPE_FLOAT,
462 ir->type->vector_elements,
463 ir->type->matrix_columns);
464 if (result->type->can_implicitly_convert_to(desired_type)) {
465 /* Even though convert_component() implements the constructor
466 * conversion rules (not the implicit conversion rules), its safe
467 * to use it here because we already checked that the implicit
468 * conversion is legal.
469 */
470 result = convert_component(ir, desired_type);
471 }
472 }
473
474 if (result->type != constructor_type->element_type()) {
475 _mesa_glsl_error(loc, state, "type error in array constructor: "
476 "expected: %s, found %s",
477 constructor_type->element_type()->name,
478 result->type->name);
479 }
480
481 /* Attempt to convert the parameter to a constant valued expression.
482 * After doing so, track whether or not all the parameters to the
483 * constructor are trivially constant valued expressions.
484 */
485 ir_rvalue *const constant = result->constant_expression_value();
486
487 if (constant != NULL)
488 result = constant;
489 else
490 all_parameters_are_constant = false;
491
492 ir->replace_with(result);
493 }
494
495 if (all_parameters_are_constant)
496 return new(ctx) ir_constant(constructor_type, &actual_parameters);
497
498 ir_variable *var = new(ctx) ir_variable(constructor_type, "array_ctor",
499 ir_var_temporary);
500 instructions->push_tail(var);
501
502 int i = 0;
503 foreach_list(node, &actual_parameters) {
504 ir_rvalue *rhs = (ir_rvalue *) node;
505 ir_rvalue *lhs = new(ctx) ir_dereference_array(var,
506 new(ctx) ir_constant(i));
507
508 ir_instruction *assignment = new(ctx) ir_assignment(lhs, rhs, NULL);
509 instructions->push_tail(assignment);
510
511 i++;
512 }
513
514 return new(ctx) ir_dereference_variable(var);
515 }
516
517
518 /**
519 * Try to convert a record constructor to a constant expression
520 */
521 static ir_constant *
522 constant_record_constructor(const glsl_type *constructor_type,
523 exec_list *parameters, void *mem_ctx)
524 {
525 foreach_list(node, parameters) {
526 ir_constant *constant = ((ir_instruction *) node)->as_constant();
527 if (constant == NULL)
528 return NULL;
529 node->replace_with(constant);
530 }
531
532 return new(mem_ctx) ir_constant(constructor_type, parameters);
533 }
534
535
536 /**
537 * Determine if a list consists of a single scalar r-value
538 */
539 bool
540 single_scalar_parameter(exec_list *parameters)
541 {
542 const ir_rvalue *const p = (ir_rvalue *) parameters->head;
543 assert(((ir_rvalue *)p)->as_rvalue() != NULL);
544
545 return (p->type->is_scalar() && p->next->is_tail_sentinel());
546 }
547
548
549 /**
550 * Generate inline code for a vector constructor
551 *
552 * The generated constructor code will consist of a temporary variable
553 * declaration of the same type as the constructor. A sequence of assignments
554 * from constructor parameters to the temporary will follow.
555 *
556 * \return
557 * An \c ir_dereference_variable of the temprorary generated in the constructor
558 * body.
559 */
560 ir_rvalue *
561 emit_inline_vector_constructor(const glsl_type *type,
562 exec_list *instructions,
563 exec_list *parameters,
564 void *ctx)
565 {
566 assert(!parameters->is_empty());
567
568 ir_variable *var = new(ctx) ir_variable(type, "vec_ctor", ir_var_temporary);
569 instructions->push_tail(var);
570
571 /* There are two kinds of vector constructors.
572 *
573 * - Construct a vector from a single scalar by replicating that scalar to
574 * all components of the vector.
575 *
576 * - Construct a vector from an arbirary combination of vectors and
577 * scalars. The components of the constructor parameters are assigned
578 * to the vector in order until the vector is full.
579 */
580 const unsigned lhs_components = type->components();
581 if (single_scalar_parameter(parameters)) {
582 ir_rvalue *first_param = (ir_rvalue *)parameters->head;
583 ir_rvalue *rhs = new(ctx) ir_swizzle(first_param, 0, 0, 0, 0,
584 lhs_components);
585 ir_dereference_variable *lhs = new(ctx) ir_dereference_variable(var);
586 const unsigned mask = (1U << lhs_components) - 1;
587
588 assert(rhs->type == lhs->type);
589
590 ir_instruction *inst = new(ctx) ir_assignment(lhs, rhs, NULL, mask);
591 instructions->push_tail(inst);
592 } else {
593 unsigned base_component = 0;
594 unsigned base_lhs_component = 0;
595 ir_constant_data data;
596 unsigned constant_mask = 0, constant_components = 0;
597
598 memset(&data, 0, sizeof(data));
599
600 foreach_list(node, parameters) {
601 ir_rvalue *param = (ir_rvalue *) node;
602 unsigned rhs_components = param->type->components();
603
604 /* Do not try to assign more components to the vector than it has!
605 */
606 if ((rhs_components + base_lhs_component) > lhs_components) {
607 rhs_components = lhs_components - base_lhs_component;
608 }
609
610 const ir_constant *const c = param->as_constant();
611 if (c != NULL) {
612 for (unsigned i = 0; i < rhs_components; i++) {
613 switch (c->type->base_type) {
614 case GLSL_TYPE_UINT:
615 data.u[i + base_component] = c->get_uint_component(i);
616 break;
617 case GLSL_TYPE_INT:
618 data.i[i + base_component] = c->get_int_component(i);
619 break;
620 case GLSL_TYPE_FLOAT:
621 data.f[i + base_component] = c->get_float_component(i);
622 break;
623 case GLSL_TYPE_BOOL:
624 data.b[i + base_component] = c->get_bool_component(i);
625 break;
626 default:
627 assert(!"Should not get here.");
628 break;
629 }
630 }
631
632 /* Mask of fields to be written in the assignment.
633 */
634 constant_mask |= ((1U << rhs_components) - 1) << base_lhs_component;
635 constant_components += rhs_components;
636
637 base_component += rhs_components;
638 }
639 /* Advance the component index by the number of components
640 * that were just assigned.
641 */
642 base_lhs_component += rhs_components;
643 }
644
645 if (constant_mask != 0) {
646 ir_dereference *lhs = new(ctx) ir_dereference_variable(var);
647 const glsl_type *rhs_type = glsl_type::get_instance(var->type->base_type,
648 constant_components,
649 1);
650 ir_rvalue *rhs = new(ctx) ir_constant(rhs_type, &data);
651
652 ir_instruction *inst =
653 new(ctx) ir_assignment(lhs, rhs, NULL, constant_mask);
654 instructions->push_tail(inst);
655 }
656
657 base_component = 0;
658 foreach_list(node, parameters) {
659 ir_rvalue *param = (ir_rvalue *) node;
660 unsigned rhs_components = param->type->components();
661
662 /* Do not try to assign more components to the vector than it has!
663 */
664 if ((rhs_components + base_component) > lhs_components) {
665 rhs_components = lhs_components - base_component;
666 }
667
668 const ir_constant *const c = param->as_constant();
669 if (c == NULL) {
670 /* Mask of fields to be written in the assignment.
671 */
672 const unsigned write_mask = ((1U << rhs_components) - 1)
673 << base_component;
674
675 ir_dereference *lhs = new(ctx) ir_dereference_variable(var);
676
677 /* Generate a swizzle so that LHS and RHS sizes match.
678 */
679 ir_rvalue *rhs =
680 new(ctx) ir_swizzle(param, 0, 1, 2, 3, rhs_components);
681
682 ir_instruction *inst =
683 new(ctx) ir_assignment(lhs, rhs, NULL, write_mask);
684 instructions->push_tail(inst);
685 }
686
687 /* Advance the component index by the number of components that were
688 * just assigned.
689 */
690 base_component += rhs_components;
691 }
692 }
693 return new(ctx) ir_dereference_variable(var);
694 }
695
696
697 /**
698 * Generate assignment of a portion of a vector to a portion of a matrix column
699 *
700 * \param src_base First component of the source to be used in assignment
701 * \param column Column of destination to be assiged
702 * \param row_base First component of the destination column to be assigned
703 * \param count Number of components to be assigned
704 *
705 * \note
706 * \c src_base + \c count must be less than or equal to the number of components
707 * in the source vector.
708 */
709 ir_instruction *
710 assign_to_matrix_column(ir_variable *var, unsigned column, unsigned row_base,
711 ir_rvalue *src, unsigned src_base, unsigned count,
712 void *mem_ctx)
713 {
714 ir_constant *col_idx = new(mem_ctx) ir_constant(column);
715 ir_dereference *column_ref = new(mem_ctx) ir_dereference_array(var, col_idx);
716
717 assert(column_ref->type->components() >= (row_base + count));
718 assert(src->type->components() >= (src_base + count));
719
720 /* Generate a swizzle that extracts the number of components from the source
721 * that are to be assigned to the column of the matrix.
722 */
723 if (count < src->type->vector_elements) {
724 src = new(mem_ctx) ir_swizzle(src,
725 src_base + 0, src_base + 1,
726 src_base + 2, src_base + 3,
727 count);
728 }
729
730 /* Mask of fields to be written in the assignment.
731 */
732 const unsigned write_mask = ((1U << count) - 1) << row_base;
733
734 return new(mem_ctx) ir_assignment(column_ref, src, NULL, write_mask);
735 }
736
737
738 /**
739 * Generate inline code for a matrix constructor
740 *
741 * The generated constructor code will consist of a temporary variable
742 * declaration of the same type as the constructor. A sequence of assignments
743 * from constructor parameters to the temporary will follow.
744 *
745 * \return
746 * An \c ir_dereference_variable of the temprorary generated in the constructor
747 * body.
748 */
749 ir_rvalue *
750 emit_inline_matrix_constructor(const glsl_type *type,
751 exec_list *instructions,
752 exec_list *parameters,
753 void *ctx)
754 {
755 assert(!parameters->is_empty());
756
757 ir_variable *var = new(ctx) ir_variable(type, "mat_ctor", ir_var_temporary);
758 instructions->push_tail(var);
759
760 /* There are three kinds of matrix constructors.
761 *
762 * - Construct a matrix from a single scalar by replicating that scalar to
763 * along the diagonal of the matrix and setting all other components to
764 * zero.
765 *
766 * - Construct a matrix from an arbirary combination of vectors and
767 * scalars. The components of the constructor parameters are assigned
768 * to the matrix in colum-major order until the matrix is full.
769 *
770 * - Construct a matrix from a single matrix. The source matrix is copied
771 * to the upper left portion of the constructed matrix, and the remaining
772 * elements take values from the identity matrix.
773 */
774 ir_rvalue *const first_param = (ir_rvalue *) parameters->head;
775 if (single_scalar_parameter(parameters)) {
776 /* Assign the scalar to the X component of a vec4, and fill the remaining
777 * components with zero.
778 */
779 ir_variable *rhs_var =
780 new(ctx) ir_variable(glsl_type::vec4_type, "mat_ctor_vec",
781 ir_var_temporary);
782 instructions->push_tail(rhs_var);
783
784 ir_constant_data zero;
785 zero.f[0] = 0.0;
786 zero.f[1] = 0.0;
787 zero.f[2] = 0.0;
788 zero.f[3] = 0.0;
789
790 ir_instruction *inst =
791 new(ctx) ir_assignment(new(ctx) ir_dereference_variable(rhs_var),
792 new(ctx) ir_constant(rhs_var->type, &zero),
793 NULL);
794 instructions->push_tail(inst);
795
796 ir_dereference *const rhs_ref = new(ctx) ir_dereference_variable(rhs_var);
797
798 inst = new(ctx) ir_assignment(rhs_ref, first_param, NULL, 0x01);
799 instructions->push_tail(inst);
800
801 /* Assign the temporary vector to each column of the destination matrix
802 * with a swizzle that puts the X component on the diagonal of the
803 * matrix. In some cases this may mean that the X component does not
804 * get assigned into the column at all (i.e., when the matrix has more
805 * columns than rows).
806 */
807 static const unsigned rhs_swiz[4][4] = {
808 { 0, 1, 1, 1 },
809 { 1, 0, 1, 1 },
810 { 1, 1, 0, 1 },
811 { 1, 1, 1, 0 }
812 };
813
814 const unsigned cols_to_init = MIN2(type->matrix_columns,
815 type->vector_elements);
816 for (unsigned i = 0; i < cols_to_init; i++) {
817 ir_constant *const col_idx = new(ctx) ir_constant(i);
818 ir_rvalue *const col_ref = new(ctx) ir_dereference_array(var, col_idx);
819
820 ir_rvalue *const rhs_ref = new(ctx) ir_dereference_variable(rhs_var);
821 ir_rvalue *const rhs = new(ctx) ir_swizzle(rhs_ref, rhs_swiz[i],
822 type->vector_elements);
823
824 inst = new(ctx) ir_assignment(col_ref, rhs, NULL);
825 instructions->push_tail(inst);
826 }
827
828 for (unsigned i = cols_to_init; i < type->matrix_columns; i++) {
829 ir_constant *const col_idx = new(ctx) ir_constant(i);
830 ir_rvalue *const col_ref = new(ctx) ir_dereference_array(var, col_idx);
831
832 ir_rvalue *const rhs_ref = new(ctx) ir_dereference_variable(rhs_var);
833 ir_rvalue *const rhs = new(ctx) ir_swizzle(rhs_ref, 1, 1, 1, 1,
834 type->vector_elements);
835
836 inst = new(ctx) ir_assignment(col_ref, rhs, NULL);
837 instructions->push_tail(inst);
838 }
839 } else if (first_param->type->is_matrix()) {
840 /* From page 50 (56 of the PDF) of the GLSL 1.50 spec:
841 *
842 * "If a matrix is constructed from a matrix, then each component
843 * (column i, row j) in the result that has a corresponding
844 * component (column i, row j) in the argument will be initialized
845 * from there. All other components will be initialized to the
846 * identity matrix. If a matrix argument is given to a matrix
847 * constructor, it is an error to have any other arguments."
848 */
849 assert(first_param->next->is_tail_sentinel());
850 ir_rvalue *const src_matrix = first_param;
851
852 /* If the source matrix is smaller, pre-initialize the relavent parts of
853 * the destination matrix to the identity matrix.
854 */
855 if ((src_matrix->type->matrix_columns < var->type->matrix_columns)
856 || (src_matrix->type->vector_elements < var->type->vector_elements)) {
857
858 /* If the source matrix has fewer rows, every column of the destination
859 * must be initialized. Otherwise only the columns in the destination
860 * that do not exist in the source must be initialized.
861 */
862 unsigned col =
863 (src_matrix->type->vector_elements < var->type->vector_elements)
864 ? 0 : src_matrix->type->matrix_columns;
865
866 const glsl_type *const col_type = var->type->column_type();
867 for (/* empty */; col < var->type->matrix_columns; col++) {
868 ir_constant_data ident;
869
870 ident.f[0] = 0.0;
871 ident.f[1] = 0.0;
872 ident.f[2] = 0.0;
873 ident.f[3] = 0.0;
874
875 ident.f[col] = 1.0;
876
877 ir_rvalue *const rhs = new(ctx) ir_constant(col_type, &ident);
878
879 ir_rvalue *const lhs =
880 new(ctx) ir_dereference_array(var, new(ctx) ir_constant(col));
881
882 ir_instruction *inst = new(ctx) ir_assignment(lhs, rhs, NULL);
883 instructions->push_tail(inst);
884 }
885 }
886
887 /* Assign columns from the source matrix to the destination matrix.
888 *
889 * Since the parameter will be used in the RHS of multiple assignments,
890 * generate a temporary and copy the paramter there.
891 */
892 ir_variable *const rhs_var =
893 new(ctx) ir_variable(first_param->type, "mat_ctor_mat",
894 ir_var_temporary);
895 instructions->push_tail(rhs_var);
896
897 ir_dereference *const rhs_var_ref =
898 new(ctx) ir_dereference_variable(rhs_var);
899 ir_instruction *const inst =
900 new(ctx) ir_assignment(rhs_var_ref, first_param, NULL);
901 instructions->push_tail(inst);
902
903 const unsigned last_row = MIN2(src_matrix->type->vector_elements,
904 var->type->vector_elements);
905 const unsigned last_col = MIN2(src_matrix->type->matrix_columns,
906 var->type->matrix_columns);
907
908 unsigned swiz[4] = { 0, 0, 0, 0 };
909 for (unsigned i = 1; i < last_row; i++)
910 swiz[i] = i;
911
912 const unsigned write_mask = (1U << last_row) - 1;
913
914 for (unsigned i = 0; i < last_col; i++) {
915 ir_dereference *const lhs =
916 new(ctx) ir_dereference_array(var, new(ctx) ir_constant(i));
917 ir_rvalue *const rhs_col =
918 new(ctx) ir_dereference_array(rhs_var, new(ctx) ir_constant(i));
919
920 /* If one matrix has columns that are smaller than the columns of the
921 * other matrix, wrap the column access of the larger with a swizzle
922 * so that the LHS and RHS of the assignment have the same size (and
923 * therefore have the same type).
924 *
925 * It would be perfectly valid to unconditionally generate the
926 * swizzles, this this will typically result in a more compact IR tree.
927 */
928 ir_rvalue *rhs;
929 if (lhs->type->vector_elements != rhs_col->type->vector_elements) {
930 rhs = new(ctx) ir_swizzle(rhs_col, swiz, last_row);
931 } else {
932 rhs = rhs_col;
933 }
934
935 ir_instruction *inst =
936 new(ctx) ir_assignment(lhs, rhs, NULL, write_mask);
937 instructions->push_tail(inst);
938 }
939 } else {
940 const unsigned cols = type->matrix_columns;
941 const unsigned rows = type->vector_elements;
942 unsigned col_idx = 0;
943 unsigned row_idx = 0;
944
945 foreach_list (node, parameters) {
946 ir_rvalue *const rhs = (ir_rvalue *) node;
947 const unsigned components_remaining_this_column = rows - row_idx;
948 unsigned rhs_components = rhs->type->components();
949 unsigned rhs_base = 0;
950
951 /* Since the parameter might be used in the RHS of two assignments,
952 * generate a temporary and copy the paramter there.
953 */
954 ir_variable *rhs_var =
955 new(ctx) ir_variable(rhs->type, "mat_ctor_vec", ir_var_temporary);
956 instructions->push_tail(rhs_var);
957
958 ir_dereference *rhs_var_ref =
959 new(ctx) ir_dereference_variable(rhs_var);
960 ir_instruction *inst = new(ctx) ir_assignment(rhs_var_ref, rhs, NULL);
961 instructions->push_tail(inst);
962
963 /* Assign the current parameter to as many components of the matrix
964 * as it will fill.
965 *
966 * NOTE: A single vector parameter can span two matrix columns. A
967 * single vec4, for example, can completely fill a mat2.
968 */
969 if (rhs_components >= components_remaining_this_column) {
970 const unsigned count = MIN2(rhs_components,
971 components_remaining_this_column);
972
973 rhs_var_ref = new(ctx) ir_dereference_variable(rhs_var);
974
975 ir_instruction *inst = assign_to_matrix_column(var, col_idx,
976 row_idx,
977 rhs_var_ref, 0,
978 count, ctx);
979 instructions->push_tail(inst);
980
981 rhs_base = count;
982
983 col_idx++;
984 row_idx = 0;
985 }
986
987 /* If there is data left in the parameter and components left to be
988 * set in the destination, emit another assignment. It is possible
989 * that the assignment could be of a vec4 to the last element of the
990 * matrix. In this case col_idx==cols, but there is still data
991 * left in the source parameter. Obviously, don't emit an assignment
992 * to data outside the destination matrix.
993 */
994 if ((col_idx < cols) && (rhs_base < rhs_components)) {
995 const unsigned count = rhs_components - rhs_base;
996
997 rhs_var_ref = new(ctx) ir_dereference_variable(rhs_var);
998
999 ir_instruction *inst = assign_to_matrix_column(var, col_idx,
1000 row_idx,
1001 rhs_var_ref,
1002 rhs_base,
1003 count, ctx);
1004 instructions->push_tail(inst);
1005
1006 row_idx += count;
1007 }
1008 }
1009 }
1010
1011 return new(ctx) ir_dereference_variable(var);
1012 }
1013
1014
1015 ir_rvalue *
1016 emit_inline_record_constructor(const glsl_type *type,
1017 exec_list *instructions,
1018 exec_list *parameters,
1019 void *mem_ctx)
1020 {
1021 ir_variable *const var =
1022 new(mem_ctx) ir_variable(type, "record_ctor", ir_var_temporary);
1023 ir_dereference_variable *const d = new(mem_ctx) ir_dereference_variable(var);
1024
1025 instructions->push_tail(var);
1026
1027 exec_node *node = parameters->head;
1028 for (unsigned i = 0; i < type->length; i++) {
1029 assert(!node->is_tail_sentinel());
1030
1031 ir_dereference *const lhs =
1032 new(mem_ctx) ir_dereference_record(d->clone(mem_ctx, NULL),
1033 type->fields.structure[i].name);
1034
1035 ir_rvalue *const rhs = ((ir_instruction *) node)->as_rvalue();
1036 assert(rhs != NULL);
1037
1038 ir_instruction *const assign = new(mem_ctx) ir_assignment(lhs, rhs, NULL);
1039
1040 instructions->push_tail(assign);
1041 node = node->next;
1042 }
1043
1044 return d;
1045 }
1046
1047
1048 ir_rvalue *
1049 ast_function_expression::hir(exec_list *instructions,
1050 struct _mesa_glsl_parse_state *state)
1051 {
1052 void *ctx = state;
1053 /* There are three sorts of function calls.
1054 *
1055 * 1. constructors - The first subexpression is an ast_type_specifier.
1056 * 2. methods - Only the .length() method of array types.
1057 * 3. functions - Calls to regular old functions.
1058 *
1059 * Method calls are actually detected when the ast_field_selection
1060 * expression is handled.
1061 */
1062 if (is_constructor()) {
1063 const ast_type_specifier *type = (ast_type_specifier *) subexpressions[0];
1064 YYLTYPE loc = type->get_location();
1065 const char *name;
1066
1067 const glsl_type *const constructor_type = type->glsl_type(& name, state);
1068
1069 /* constructor_type can be NULL if a variable with the same name as the
1070 * structure has come into scope.
1071 */
1072 if (constructor_type == NULL) {
1073 _mesa_glsl_error(& loc, state, "unknown type `%s' (structure name "
1074 "may be shadowed by a variable with the same name)",
1075 type->type_name);
1076 return ir_call::get_error_instruction(ctx);
1077 }
1078
1079
1080 /* Constructors for samplers are illegal.
1081 */
1082 if (constructor_type->is_sampler()) {
1083 _mesa_glsl_error(& loc, state, "cannot construct sampler type `%s'",
1084 constructor_type->name);
1085 return ir_call::get_error_instruction(ctx);
1086 }
1087
1088 if (constructor_type->is_array()) {
1089 if (state->language_version <= 110) {
1090 _mesa_glsl_error(& loc, state,
1091 "array constructors forbidden in GLSL 1.10");
1092 return ir_call::get_error_instruction(ctx);
1093 }
1094
1095 return process_array_constructor(instructions, constructor_type,
1096 & loc, &this->expressions, state);
1097 }
1098
1099
1100 /* There are two kinds of constructor call. Constructors for built-in
1101 * language types, such as mat4 and vec2, are free form. The only
1102 * requirement is that the parameters must provide enough values of the
1103 * correct scalar type. Constructors for arrays and structures must
1104 * have the exact number of parameters with matching types in the
1105 * correct order. These constructors follow essentially the same type
1106 * matching rules as functions.
1107 */
1108 if (constructor_type->is_record()) {
1109 exec_list actual_parameters;
1110
1111 process_parameters(instructions, &actual_parameters,
1112 &this->expressions, state);
1113
1114 exec_node *node = actual_parameters.head;
1115 for (unsigned i = 0; i < constructor_type->length; i++) {
1116 ir_rvalue *ir = (ir_rvalue *) node;
1117
1118 if (node->is_tail_sentinel()) {
1119 _mesa_glsl_error(&loc, state,
1120 "insufficient parameters to constructor "
1121 "for `%s'",
1122 constructor_type->name);
1123 return ir_call::get_error_instruction(ctx);
1124 }
1125
1126 if (apply_implicit_conversion(constructor_type->fields.structure[i].type,
1127 ir, state)) {
1128 node->replace_with(ir);
1129 } else {
1130 _mesa_glsl_error(&loc, state,
1131 "parameter type mismatch in constructor "
1132 "for `%s.%s' (%s vs %s)",
1133 constructor_type->name,
1134 constructor_type->fields.structure[i].name,
1135 ir->type->name,
1136 constructor_type->fields.structure[i].type->name);
1137 return ir_call::get_error_instruction(ctx);;
1138 }
1139
1140 node = node->next;
1141 }
1142
1143 if (!node->is_tail_sentinel()) {
1144 _mesa_glsl_error(&loc, state, "too many parameters in constructor "
1145 "for `%s'", constructor_type->name);
1146 return ir_call::get_error_instruction(ctx);
1147 }
1148
1149 ir_rvalue *const constant =
1150 constant_record_constructor(constructor_type, &actual_parameters,
1151 state);
1152
1153 return (constant != NULL)
1154 ? constant
1155 : emit_inline_record_constructor(constructor_type, instructions,
1156 &actual_parameters, state);
1157 }
1158
1159 if (!constructor_type->is_numeric() && !constructor_type->is_boolean())
1160 return ir_call::get_error_instruction(ctx);
1161
1162 /* Total number of components of the type being constructed. */
1163 const unsigned type_components = constructor_type->components();
1164
1165 /* Number of components from parameters that have actually been
1166 * consumed. This is used to perform several kinds of error checking.
1167 */
1168 unsigned components_used = 0;
1169
1170 unsigned matrix_parameters = 0;
1171 unsigned nonmatrix_parameters = 0;
1172 exec_list actual_parameters;
1173
1174 foreach_list (n, &this->expressions) {
1175 ast_node *ast = exec_node_data(ast_node, n, link);
1176 ir_rvalue *result = ast->hir(instructions, state)->as_rvalue();
1177
1178 /* From page 50 (page 56 of the PDF) of the GLSL 1.50 spec:
1179 *
1180 * "It is an error to provide extra arguments beyond this
1181 * last used argument."
1182 */
1183 if (components_used >= type_components) {
1184 _mesa_glsl_error(& loc, state, "too many parameters to `%s' "
1185 "constructor",
1186 constructor_type->name);
1187 return ir_call::get_error_instruction(ctx);
1188 }
1189
1190 if (!result->type->is_numeric() && !result->type->is_boolean()) {
1191 _mesa_glsl_error(& loc, state, "cannot construct `%s' from a "
1192 "non-numeric data type",
1193 constructor_type->name);
1194 return ir_call::get_error_instruction(ctx);
1195 }
1196
1197 /* Count the number of matrix and nonmatrix parameters. This
1198 * is used below to enforce some of the constructor rules.
1199 */
1200 if (result->type->is_matrix())
1201 matrix_parameters++;
1202 else
1203 nonmatrix_parameters++;
1204
1205 actual_parameters.push_tail(result);
1206 components_used += result->type->components();
1207 }
1208
1209 /* From page 28 (page 34 of the PDF) of the GLSL 1.10 spec:
1210 *
1211 * "It is an error to construct matrices from other matrices. This
1212 * is reserved for future use."
1213 */
1214 if (state->language_version == 110 && matrix_parameters > 0
1215 && constructor_type->is_matrix()) {
1216 _mesa_glsl_error(& loc, state, "cannot construct `%s' from a "
1217 "matrix in GLSL 1.10",
1218 constructor_type->name);
1219 return ir_call::get_error_instruction(ctx);
1220 }
1221
1222 /* From page 50 (page 56 of the PDF) of the GLSL 1.50 spec:
1223 *
1224 * "If a matrix argument is given to a matrix constructor, it is
1225 * an error to have any other arguments."
1226 */
1227 if ((matrix_parameters > 0)
1228 && ((matrix_parameters + nonmatrix_parameters) > 1)
1229 && constructor_type->is_matrix()) {
1230 _mesa_glsl_error(& loc, state, "for matrix `%s' constructor, "
1231 "matrix must be only parameter",
1232 constructor_type->name);
1233 return ir_call::get_error_instruction(ctx);
1234 }
1235
1236 /* From page 28 (page 34 of the PDF) of the GLSL 1.10 spec:
1237 *
1238 * "In these cases, there must be enough components provided in the
1239 * arguments to provide an initializer for every component in the
1240 * constructed value."
1241 */
1242 if (components_used < type_components && components_used != 1
1243 && matrix_parameters == 0) {
1244 _mesa_glsl_error(& loc, state, "too few components to construct "
1245 "`%s'",
1246 constructor_type->name);
1247 return ir_call::get_error_instruction(ctx);
1248 }
1249
1250 /* Later, we cast each parameter to the same base type as the
1251 * constructor. Since there are no non-floating point matrices, we
1252 * need to break them up into a series of column vectors.
1253 */
1254 if (constructor_type->base_type != GLSL_TYPE_FLOAT) {
1255 foreach_list_safe(n, &actual_parameters) {
1256 ir_rvalue *matrix = (ir_rvalue *) n;
1257
1258 if (!matrix->type->is_matrix())
1259 continue;
1260
1261 /* Create a temporary containing the matrix. */
1262 ir_variable *var = new(ctx) ir_variable(matrix->type, "matrix_tmp",
1263 ir_var_temporary);
1264 instructions->push_tail(var);
1265 instructions->push_tail(new(ctx) ir_assignment(new(ctx)
1266 ir_dereference_variable(var), matrix, NULL));
1267 var->constant_value = matrix->constant_expression_value();
1268
1269 /* Replace the matrix with dereferences of its columns. */
1270 for (int i = 0; i < matrix->type->matrix_columns; i++) {
1271 matrix->insert_before(new (ctx) ir_dereference_array(var,
1272 new(ctx) ir_constant(i)));
1273 }
1274 matrix->remove();
1275 }
1276 }
1277
1278 bool all_parameters_are_constant = true;
1279
1280 /* Type cast each parameter and, if possible, fold constants.*/
1281 foreach_list_safe(n, &actual_parameters) {
1282 ir_rvalue *ir = (ir_rvalue *) n;
1283
1284 const glsl_type *desired_type =
1285 glsl_type::get_instance(constructor_type->base_type,
1286 ir->type->vector_elements,
1287 ir->type->matrix_columns);
1288 ir_rvalue *result = convert_component(ir, desired_type);
1289
1290 /* Attempt to convert the parameter to a constant valued expression.
1291 * After doing so, track whether or not all the parameters to the
1292 * constructor are trivially constant valued expressions.
1293 */
1294 ir_rvalue *const constant = result->constant_expression_value();
1295
1296 if (constant != NULL)
1297 result = constant;
1298 else
1299 all_parameters_are_constant = false;
1300
1301 if (result != ir) {
1302 ir->replace_with(result);
1303 }
1304 }
1305
1306 /* If all of the parameters are trivially constant, create a
1307 * constant representing the complete collection of parameters.
1308 */
1309 if (all_parameters_are_constant) {
1310 return new(ctx) ir_constant(constructor_type, &actual_parameters);
1311 } else if (constructor_type->is_scalar()) {
1312 return dereference_component((ir_rvalue *) actual_parameters.head,
1313 0);
1314 } else if (constructor_type->is_vector()) {
1315 return emit_inline_vector_constructor(constructor_type,
1316 instructions,
1317 &actual_parameters,
1318 ctx);
1319 } else {
1320 assert(constructor_type->is_matrix());
1321 return emit_inline_matrix_constructor(constructor_type,
1322 instructions,
1323 &actual_parameters,
1324 ctx);
1325 }
1326 } else {
1327 const ast_expression *id = subexpressions[0];
1328 YYLTYPE loc = id->get_location();
1329 exec_list actual_parameters;
1330
1331 process_parameters(instructions, &actual_parameters, &this->expressions,
1332 state);
1333
1334 return match_function_by_name(instructions,
1335 id->primary_expression.identifier, & loc,
1336 &actual_parameters, state);
1337 }
1338
1339 return ir_call::get_error_instruction(ctx);
1340 }