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