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