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