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