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