glsl: struct constructors/initializers only allow implicit conversions
[mesa.git] / src / compiler / 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 "compiler/glsl_types.h"
27 #include "ir.h"
28 #include "main/core.h" /* for MIN2 */
29 #include "main/shaderobj.h"
30
31 static ir_rvalue *
32 convert_component(ir_rvalue *src, const glsl_type *desired_type);
33
34 bool
35 apply_implicit_conversion(const glsl_type *to, ir_rvalue * &from,
36 struct _mesa_glsl_parse_state *state);
37
38 static unsigned
39 process_parameters(exec_list *instructions, exec_list *actual_parameters,
40 exec_list *parameters,
41 struct _mesa_glsl_parse_state *state)
42 {
43 unsigned count = 0;
44
45 foreach_list_typed(ast_node, ast, link, parameters) {
46 /* We need to process the parameters first in order to know if we can
47 * raise or not a unitialized warning. Calling set_is_lhs silence the
48 * warning for now. Raising the warning or not will be checked at
49 * verify_parameter_modes.
50 */
51 ast->set_is_lhs(true);
52 ir_rvalue *result = ast->hir(instructions, state);
53
54 ir_constant *const constant = result->constant_expression_value();
55 if (constant != NULL)
56 result = constant;
57
58 actual_parameters->push_tail(result);
59 count++;
60 }
61
62 return count;
63 }
64
65
66 /**
67 * Generate a source prototype for a function signature
68 *
69 * \param return_type Return type of the function. May be \c NULL.
70 * \param name Name of the function.
71 * \param parameters List of \c ir_instruction nodes representing the
72 * parameter list for the function. This may be either a
73 * formal (\c ir_variable) or actual (\c ir_rvalue)
74 * parameter list. Only the type is used.
75 *
76 * \return
77 * A ralloced string representing the prototype of the function.
78 */
79 char *
80 prototype_string(const glsl_type *return_type, const char *name,
81 exec_list *parameters)
82 {
83 char *str = NULL;
84
85 if (return_type != NULL)
86 str = ralloc_asprintf(NULL, "%s ", return_type->name);
87
88 ralloc_asprintf_append(&str, "%s(", name);
89
90 const char *comma = "";
91 foreach_in_list(const ir_variable, param, parameters) {
92 ralloc_asprintf_append(&str, "%s%s", comma, param->type->name);
93 comma = ", ";
94 }
95
96 ralloc_strcat(&str, ")");
97 return str;
98 }
99
100 static bool
101 verify_image_parameter(YYLTYPE *loc, _mesa_glsl_parse_state *state,
102 const ir_variable *formal, const ir_variable *actual)
103 {
104 /**
105 * From the ARB_shader_image_load_store specification:
106 *
107 * "The values of image variables qualified with coherent,
108 * volatile, restrict, readonly, or writeonly may not be passed
109 * to functions whose formal parameters lack such
110 * qualifiers. [...] It is legal to have additional qualifiers
111 * on a formal parameter, but not to have fewer."
112 */
113 if (actual->data.image_coherent && !formal->data.image_coherent) {
114 _mesa_glsl_error(loc, state,
115 "function call parameter `%s' drops "
116 "`coherent' qualifier", formal->name);
117 return false;
118 }
119
120 if (actual->data.image_volatile && !formal->data.image_volatile) {
121 _mesa_glsl_error(loc, state,
122 "function call parameter `%s' drops "
123 "`volatile' qualifier", formal->name);
124 return false;
125 }
126
127 if (actual->data.image_restrict && !formal->data.image_restrict) {
128 _mesa_glsl_error(loc, state,
129 "function call parameter `%s' drops "
130 "`restrict' qualifier", formal->name);
131 return false;
132 }
133
134 if (actual->data.image_read_only && !formal->data.image_read_only) {
135 _mesa_glsl_error(loc, state,
136 "function call parameter `%s' drops "
137 "`readonly' qualifier", formal->name);
138 return false;
139 }
140
141 if (actual->data.image_write_only && !formal->data.image_write_only) {
142 _mesa_glsl_error(loc, state,
143 "function call parameter `%s' drops "
144 "`writeonly' qualifier", formal->name);
145 return false;
146 }
147
148 return true;
149 }
150
151 static bool
152 verify_first_atomic_parameter(YYLTYPE *loc, _mesa_glsl_parse_state *state,
153 ir_variable *var)
154 {
155 if (!var ||
156 (!var->is_in_shader_storage_block() &&
157 var->data.mode != ir_var_shader_shared)) {
158 _mesa_glsl_error(loc, state, "First argument to atomic function "
159 "must be a buffer or shared variable");
160 return false;
161 }
162 return true;
163 }
164
165 static bool
166 is_atomic_function(const char *func_name)
167 {
168 return !strcmp(func_name, "atomicAdd") ||
169 !strcmp(func_name, "atomicMin") ||
170 !strcmp(func_name, "atomicMax") ||
171 !strcmp(func_name, "atomicAnd") ||
172 !strcmp(func_name, "atomicOr") ||
173 !strcmp(func_name, "atomicXor") ||
174 !strcmp(func_name, "atomicExchange") ||
175 !strcmp(func_name, "atomicCompSwap");
176 }
177
178 /**
179 * Verify that 'out' and 'inout' actual parameters are lvalues. Also, verify
180 * that 'const_in' formal parameters (an extension in our IR) correspond to
181 * ir_constant actual parameters.
182 */
183 static bool
184 verify_parameter_modes(_mesa_glsl_parse_state *state,
185 ir_function_signature *sig,
186 exec_list &actual_ir_parameters,
187 exec_list &actual_ast_parameters)
188 {
189 exec_node *actual_ir_node = actual_ir_parameters.get_head_raw();
190 exec_node *actual_ast_node = actual_ast_parameters.get_head_raw();
191
192 foreach_in_list(const ir_variable, formal, &sig->parameters) {
193 /* The lists must be the same length. */
194 assert(!actual_ir_node->is_tail_sentinel());
195 assert(!actual_ast_node->is_tail_sentinel());
196
197 const ir_rvalue *const actual = (ir_rvalue *) actual_ir_node;
198 const ast_expression *const actual_ast =
199 exec_node_data(ast_expression, actual_ast_node, link);
200
201 /* FIXME: 'loc' is incorrect (as of 2011-01-21). It is always
202 * FIXME: 0:0(0).
203 */
204 YYLTYPE loc = actual_ast->get_location();
205
206 /* Verify that 'const_in' parameters are ir_constants. */
207 if (formal->data.mode == ir_var_const_in &&
208 actual->ir_type != ir_type_constant) {
209 _mesa_glsl_error(&loc, state,
210 "parameter `in %s' must be a constant expression",
211 formal->name);
212 return false;
213 }
214
215 /* Verify that shader_in parameters are shader inputs */
216 if (formal->data.must_be_shader_input) {
217 const ir_rvalue *val = actual;
218
219 // GLSL 4.40 allows swizzles, while earlier GLSL versions do not.
220 if (val->ir_type == ir_type_swizzle) {
221 if (!state->is_version(440, 0)) {
222 _mesa_glsl_error(&loc, state,
223 "parameter `%s` must not be swizzled",
224 formal->name);
225 return false;
226 }
227 val = ((ir_swizzle *)val)->val;
228 }
229
230 while (val->ir_type == ir_type_dereference_array) {
231 val = ((ir_dereference_array *)val)->array;
232 }
233
234 if (!val->as_dereference_variable() ||
235 val->variable_referenced()->data.mode != ir_var_shader_in) {
236 _mesa_glsl_error(&loc, state,
237 "parameter `%s` must be a shader input",
238 formal->name);
239 return false;
240 }
241 }
242
243 /* Verify that 'out' and 'inout' actual parameters are lvalues. */
244 if (formal->data.mode == ir_var_function_out
245 || formal->data.mode == ir_var_function_inout) {
246 const char *mode = NULL;
247 switch (formal->data.mode) {
248 case ir_var_function_out: mode = "out"; break;
249 case ir_var_function_inout: mode = "inout"; break;
250 default: assert(false); break;
251 }
252
253 /* This AST-based check catches errors like f(i++). The IR-based
254 * is_lvalue() is insufficient because the actual parameter at the
255 * IR-level is just a temporary value, which is an l-value.
256 */
257 if (actual_ast->non_lvalue_description != NULL) {
258 _mesa_glsl_error(&loc, state,
259 "function parameter '%s %s' references a %s",
260 mode, formal->name,
261 actual_ast->non_lvalue_description);
262 return false;
263 }
264
265 ir_variable *var = actual->variable_referenced();
266
267 if (var && formal->data.mode == ir_var_function_inout) {
268 if ((var->data.mode == ir_var_auto || var->data.mode == ir_var_shader_out) &&
269 !var->data.assigned &&
270 !is_gl_identifier(var->name)) {
271 _mesa_glsl_warning(&loc, state, "`%s' used uninitialized",
272 var->name);
273 }
274 }
275
276 if (var)
277 var->data.assigned = true;
278
279 if (var && var->data.read_only) {
280 _mesa_glsl_error(&loc, state,
281 "function parameter '%s %s' references the "
282 "read-only variable '%s'",
283 mode, formal->name,
284 actual->variable_referenced()->name);
285 return false;
286 } else if (!actual->is_lvalue()) {
287 _mesa_glsl_error(&loc, state,
288 "function parameter '%s %s' is not an lvalue",
289 mode, formal->name);
290 return false;
291 }
292 } else {
293 assert(formal->data.mode == ir_var_function_in ||
294 formal->data.mode == ir_var_const_in);
295 ir_variable *var = actual->variable_referenced();
296 if (var) {
297 if ((var->data.mode == ir_var_auto || var->data.mode == ir_var_shader_out) &&
298 !var->data.assigned &&
299 !is_gl_identifier(var->name)) {
300 _mesa_glsl_warning(&loc, state, "`%s' used uninitialized",
301 var->name);
302 }
303 }
304 }
305
306 if (formal->type->is_image() &&
307 actual->variable_referenced()) {
308 if (!verify_image_parameter(&loc, state, formal,
309 actual->variable_referenced()))
310 return false;
311 }
312
313 actual_ir_node = actual_ir_node->next;
314 actual_ast_node = actual_ast_node->next;
315 }
316
317 /* The first parameter of atomic functions must be a buffer variable */
318 const char *func_name = sig->function_name();
319 bool is_atomic = is_atomic_function(func_name);
320 if (is_atomic) {
321 const ir_rvalue *const actual =
322 (ir_rvalue *) actual_ir_parameters.get_head_raw();
323
324 const ast_expression *const actual_ast =
325 exec_node_data(ast_expression,
326 actual_ast_parameters.get_head_raw(), link);
327 YYLTYPE loc = actual_ast->get_location();
328
329 if (!verify_first_atomic_parameter(&loc, state,
330 actual->variable_referenced())) {
331 return false;
332 }
333 }
334
335 return true;
336 }
337
338 static void
339 fix_parameter(void *mem_ctx, ir_rvalue *actual, const glsl_type *formal_type,
340 exec_list *before_instructions, exec_list *after_instructions,
341 bool parameter_is_inout)
342 {
343 ir_expression *const expr = actual->as_expression();
344
345 /* If the types match exactly and the parameter is not a vector-extract,
346 * nothing needs to be done to fix the parameter.
347 */
348 if (formal_type == actual->type
349 && (expr == NULL || expr->operation != ir_binop_vector_extract))
350 return;
351
352 /* To convert an out parameter, we need to create a temporary variable to
353 * hold the value before conversion, and then perform the conversion after
354 * the function call returns.
355 *
356 * This has the effect of transforming code like this:
357 *
358 * void f(out int x);
359 * float value;
360 * f(value);
361 *
362 * Into IR that's equivalent to this:
363 *
364 * void f(out int x);
365 * float value;
366 * int out_parameter_conversion;
367 * f(out_parameter_conversion);
368 * value = float(out_parameter_conversion);
369 *
370 * If the parameter is an ir_expression of ir_binop_vector_extract,
371 * additional conversion is needed in the post-call re-write.
372 */
373 ir_variable *tmp =
374 new(mem_ctx) ir_variable(formal_type, "inout_tmp", ir_var_temporary);
375
376 before_instructions->push_tail(tmp);
377
378 /* If the parameter is an inout parameter, copy the value of the actual
379 * parameter to the new temporary. Note that no type conversion is allowed
380 * here because inout parameters must match types exactly.
381 */
382 if (parameter_is_inout) {
383 /* Inout parameters should never require conversion, since that would
384 * require an implicit conversion to exist both to and from the formal
385 * parameter type, and there are no bidirectional implicit conversions.
386 */
387 assert (actual->type == formal_type);
388
389 ir_dereference_variable *const deref_tmp_1 =
390 new(mem_ctx) ir_dereference_variable(tmp);
391 ir_assignment *const assignment =
392 new(mem_ctx) ir_assignment(deref_tmp_1, actual);
393 before_instructions->push_tail(assignment);
394 }
395
396 /* Replace the parameter in the call with a dereference of the new
397 * temporary.
398 */
399 ir_dereference_variable *const deref_tmp_2 =
400 new(mem_ctx) ir_dereference_variable(tmp);
401 actual->replace_with(deref_tmp_2);
402
403
404 /* Copy the temporary variable to the actual parameter with optional
405 * type conversion applied.
406 */
407 ir_rvalue *rhs = new(mem_ctx) ir_dereference_variable(tmp);
408 if (actual->type != formal_type)
409 rhs = convert_component(rhs, actual->type);
410
411 ir_rvalue *lhs = actual;
412 if (expr != NULL && expr->operation == ir_binop_vector_extract) {
413 lhs = new(mem_ctx) ir_dereference_array(expr->operands[0]->clone(mem_ctx, NULL),
414 expr->operands[1]->clone(mem_ctx, NULL));
415 }
416
417 ir_assignment *const assignment_2 = new(mem_ctx) ir_assignment(lhs, rhs);
418 after_instructions->push_tail(assignment_2);
419 }
420
421 /**
422 * Generate a function call.
423 *
424 * For non-void functions, this returns a dereference of the temporary variable
425 * which stores the return value for the call. For void functions, this returns
426 * NULL.
427 */
428 static ir_rvalue *
429 generate_call(exec_list *instructions, ir_function_signature *sig,
430 exec_list *actual_parameters,
431 ir_variable *sub_var,
432 ir_rvalue *array_idx,
433 struct _mesa_glsl_parse_state *state)
434 {
435 void *ctx = state;
436 exec_list post_call_conversions;
437
438 /* Perform implicit conversion of arguments. For out parameters, we need
439 * to place them in a temporary variable and do the conversion after the
440 * call takes place. Since we haven't emitted the call yet, we'll place
441 * the post-call conversions in a temporary exec_list, and emit them later.
442 */
443 foreach_two_lists(formal_node, &sig->parameters,
444 actual_node, actual_parameters) {
445 ir_rvalue *actual = (ir_rvalue *) actual_node;
446 ir_variable *formal = (ir_variable *) formal_node;
447
448 if (formal->type->is_numeric() || formal->type->is_boolean()) {
449 switch (formal->data.mode) {
450 case ir_var_const_in:
451 case ir_var_function_in: {
452 ir_rvalue *converted
453 = convert_component(actual, formal->type);
454 actual->replace_with(converted);
455 break;
456 }
457 case ir_var_function_out:
458 case ir_var_function_inout:
459 fix_parameter(ctx, actual, formal->type,
460 instructions, &post_call_conversions,
461 formal->data.mode == ir_var_function_inout);
462 break;
463 default:
464 assert (!"Illegal formal parameter mode");
465 break;
466 }
467 }
468 }
469
470 /* Section 4.3.2 (Const) of the GLSL 1.10.59 spec says:
471 *
472 * "Initializers for const declarations must be formed from literal
473 * values, other const variables (not including function call
474 * paramaters), or expressions of these.
475 *
476 * Constructors may be used in such expressions, but function calls may
477 * not."
478 *
479 * Section 4.3.3 (Constant Expressions) of the GLSL 1.20.8 spec says:
480 *
481 * "A constant expression is one of
482 *
483 * ...
484 *
485 * - a built-in function call whose arguments are all constant
486 * expressions, with the exception of the texture lookup
487 * functions, the noise functions, and ftransform. The built-in
488 * functions dFdx, dFdy, and fwidth must return 0 when evaluated
489 * inside an initializer with an argument that is a constant
490 * expression."
491 *
492 * Section 5.10 (Constant Expressions) of the GLSL ES 1.00.17 spec says:
493 *
494 * "A constant expression is one of
495 *
496 * ...
497 *
498 * - a built-in function call whose arguments are all constant
499 * expressions, with the exception of the texture lookup
500 * functions."
501 *
502 * Section 4.3.3 (Constant Expressions) of the GLSL ES 3.00.4 spec says:
503 *
504 * "A constant expression is one of
505 *
506 * ...
507 *
508 * - a built-in function call whose arguments are all constant
509 * expressions, with the exception of the texture lookup
510 * functions. The built-in functions dFdx, dFdy, and fwidth must
511 * return 0 when evaluated inside an initializer with an argument
512 * that is a constant expression."
513 *
514 * If the function call is a constant expression, don't generate any
515 * instructions; just generate an ir_constant.
516 */
517 if (state->is_version(120, 100)) {
518 ir_constant *value = sig->constant_expression_value(actual_parameters, NULL);
519 if (value != NULL) {
520 return value;
521 }
522 }
523
524 ir_dereference_variable *deref = NULL;
525 if (!sig->return_type->is_void()) {
526 /* Create a new temporary to hold the return value. */
527 char *const name = ir_variable::temporaries_allocate_names
528 ? ralloc_asprintf(ctx, "%s_retval", sig->function_name())
529 : NULL;
530
531 ir_variable *var;
532
533 var = new(ctx) ir_variable(sig->return_type, name, ir_var_temporary);
534 instructions->push_tail(var);
535
536 ralloc_free(name);
537
538 deref = new(ctx) ir_dereference_variable(var);
539 }
540
541 ir_call *call = new(ctx) ir_call(sig, deref, actual_parameters, sub_var, array_idx);
542 instructions->push_tail(call);
543
544 /* Also emit any necessary out-parameter conversions. */
545 instructions->append_list(&post_call_conversions);
546
547 return deref ? deref->clone(ctx, NULL) : NULL;
548 }
549
550 /**
551 * Given a function name and parameter list, find the matching signature.
552 */
553 static ir_function_signature *
554 match_function_by_name(const char *name,
555 exec_list *actual_parameters,
556 struct _mesa_glsl_parse_state *state)
557 {
558 void *ctx = state;
559 ir_function *f = state->symbols->get_function(name);
560 ir_function_signature *local_sig = NULL;
561 ir_function_signature *sig = NULL;
562
563 /* Is the function hidden by a record type constructor? */
564 if (state->symbols->get_type(name))
565 goto done; /* no match */
566
567 /* Is the function hidden by a variable (impossible in 1.10)? */
568 if (!state->symbols->separate_function_namespace
569 && state->symbols->get_variable(name))
570 goto done; /* no match */
571
572 if (f != NULL) {
573 /* In desktop GL, the presence of a user-defined signature hides any
574 * built-in signatures, so we must ignore them. In contrast, in ES2
575 * user-defined signatures add new overloads, so we must consider them.
576 */
577 bool allow_builtins = state->es_shader || !f->has_user_signature();
578
579 /* Look for a match in the local shader. If exact, we're done. */
580 bool is_exact = false;
581 sig = local_sig = f->matching_signature(state, actual_parameters,
582 allow_builtins, &is_exact);
583 if (is_exact)
584 goto done;
585
586 if (!allow_builtins)
587 goto done;
588 }
589
590 /* Local shader has no exact candidates; check the built-ins. */
591 _mesa_glsl_initialize_builtin_functions();
592 sig = _mesa_glsl_find_builtin_function(state, name, actual_parameters);
593
594 done:
595 if (sig != NULL) {
596 /* If the match is from a linked built-in shader, import the prototype. */
597 if (sig != local_sig) {
598 if (f == NULL) {
599 f = new(ctx) ir_function(name);
600 state->symbols->add_global_function(f);
601 emit_function(state, f);
602 }
603 sig = sig->clone_prototype(f, NULL);
604 f->add_signature(sig);
605 }
606 }
607 return sig;
608 }
609
610 static ir_function_signature *
611 match_subroutine_by_name(const char *name,
612 exec_list *actual_parameters,
613 struct _mesa_glsl_parse_state *state,
614 ir_variable **var_r)
615 {
616 void *ctx = state;
617 ir_function_signature *sig = NULL;
618 ir_function *f, *found = NULL;
619 const char *new_name;
620 ir_variable *var;
621 bool is_exact = false;
622
623 new_name = ralloc_asprintf(ctx, "%s_%s", _mesa_shader_stage_to_subroutine_prefix(state->stage), name);
624 var = state->symbols->get_variable(new_name);
625 if (!var)
626 return NULL;
627
628 for (int i = 0; i < state->num_subroutine_types; i++) {
629 f = state->subroutine_types[i];
630 if (strcmp(f->name, var->type->without_array()->name))
631 continue;
632 found = f;
633 break;
634 }
635
636 if (!found)
637 return NULL;
638 *var_r = var;
639 sig = found->matching_signature(state, actual_parameters,
640 false, &is_exact);
641 return sig;
642 }
643
644 static ir_rvalue *
645 generate_array_index(void *mem_ctx, exec_list *instructions,
646 struct _mesa_glsl_parse_state *state, YYLTYPE loc,
647 const ast_expression *array, ast_expression *idx,
648 const char **function_name, exec_list *actual_parameters)
649 {
650 if (array->oper == ast_array_index) {
651 /* This handles arrays of arrays */
652 ir_rvalue *outer_array = generate_array_index(mem_ctx, instructions,
653 state, loc,
654 array->subexpressions[0],
655 array->subexpressions[1],
656 function_name, actual_parameters);
657 ir_rvalue *outer_array_idx = idx->hir(instructions, state);
658
659 YYLTYPE index_loc = idx->get_location();
660 return _mesa_ast_array_index_to_hir(mem_ctx, state, outer_array,
661 outer_array_idx, loc,
662 index_loc);
663 } else {
664 ir_variable *sub_var = NULL;
665 *function_name = array->primary_expression.identifier;
666
667 match_subroutine_by_name(*function_name, actual_parameters,
668 state, &sub_var);
669
670 ir_rvalue *outer_array_idx = idx->hir(instructions, state);
671 return new(mem_ctx) ir_dereference_array(sub_var, outer_array_idx);
672 }
673 }
674
675 static void
676 print_function_prototypes(_mesa_glsl_parse_state *state, YYLTYPE *loc,
677 ir_function *f)
678 {
679 if (f == NULL)
680 return;
681
682 foreach_in_list(ir_function_signature, sig, &f->signatures) {
683 if (sig->is_builtin() && !sig->is_builtin_available(state))
684 continue;
685
686 char *str = prototype_string(sig->return_type, f->name, &sig->parameters);
687 _mesa_glsl_error(loc, state, " %s", str);
688 ralloc_free(str);
689 }
690 }
691
692 /**
693 * Raise a "no matching function" error, listing all possible overloads the
694 * compiler considered so developers can figure out what went wrong.
695 */
696 static void
697 no_matching_function_error(const char *name,
698 YYLTYPE *loc,
699 exec_list *actual_parameters,
700 _mesa_glsl_parse_state *state)
701 {
702 gl_shader *sh = _mesa_glsl_get_builtin_function_shader();
703
704 if (state->symbols->get_function(name) == NULL
705 && (!state->uses_builtin_functions
706 || sh->symbols->get_function(name) == NULL)) {
707 _mesa_glsl_error(loc, state, "no function with name '%s'", name);
708 } else {
709 char *str = prototype_string(NULL, name, actual_parameters);
710 _mesa_glsl_error(loc, state,
711 "no matching function for call to `%s'; candidates are:",
712 str);
713 ralloc_free(str);
714
715 print_function_prototypes(state, loc, state->symbols->get_function(name));
716
717 if (state->uses_builtin_functions) {
718 print_function_prototypes(state, loc, sh->symbols->get_function(name));
719 }
720 }
721 }
722
723 /**
724 * Perform automatic type conversion of constructor parameters
725 *
726 * This implements the rules in the "Conversion and Scalar Constructors"
727 * section (GLSL 1.10 section 5.4.1), not the "Implicit Conversions" rules.
728 */
729 static ir_rvalue *
730 convert_component(ir_rvalue *src, const glsl_type *desired_type)
731 {
732 void *ctx = ralloc_parent(src);
733 const unsigned a = desired_type->base_type;
734 const unsigned b = src->type->base_type;
735 ir_expression *result = NULL;
736
737 if (src->type->is_error())
738 return src;
739
740 assert(a <= GLSL_TYPE_BOOL);
741 assert(b <= GLSL_TYPE_BOOL);
742
743 if (a == b)
744 return src;
745
746 switch (a) {
747 case GLSL_TYPE_UINT:
748 switch (b) {
749 case GLSL_TYPE_INT:
750 result = new(ctx) ir_expression(ir_unop_i2u, src);
751 break;
752 case GLSL_TYPE_FLOAT:
753 result = new(ctx) ir_expression(ir_unop_f2u, src);
754 break;
755 case GLSL_TYPE_BOOL:
756 result = new(ctx) ir_expression(ir_unop_i2u,
757 new(ctx) ir_expression(ir_unop_b2i, src));
758 break;
759 case GLSL_TYPE_DOUBLE:
760 result = new(ctx) ir_expression(ir_unop_d2u, src);
761 break;
762 }
763 break;
764 case GLSL_TYPE_INT:
765 switch (b) {
766 case GLSL_TYPE_UINT:
767 result = new(ctx) ir_expression(ir_unop_u2i, src);
768 break;
769 case GLSL_TYPE_FLOAT:
770 result = new(ctx) ir_expression(ir_unop_f2i, src);
771 break;
772 case GLSL_TYPE_BOOL:
773 result = new(ctx) ir_expression(ir_unop_b2i, src);
774 break;
775 case GLSL_TYPE_DOUBLE:
776 result = new(ctx) ir_expression(ir_unop_d2i, src);
777 break;
778 }
779 break;
780 case GLSL_TYPE_FLOAT:
781 switch (b) {
782 case GLSL_TYPE_UINT:
783 result = new(ctx) ir_expression(ir_unop_u2f, desired_type, src, NULL);
784 break;
785 case GLSL_TYPE_INT:
786 result = new(ctx) ir_expression(ir_unop_i2f, desired_type, src, NULL);
787 break;
788 case GLSL_TYPE_BOOL:
789 result = new(ctx) ir_expression(ir_unop_b2f, desired_type, src, NULL);
790 break;
791 case GLSL_TYPE_DOUBLE:
792 result = new(ctx) ir_expression(ir_unop_d2f, desired_type, src, NULL);
793 break;
794 }
795 break;
796 case GLSL_TYPE_BOOL:
797 switch (b) {
798 case GLSL_TYPE_UINT:
799 result = new(ctx) ir_expression(ir_unop_i2b,
800 new(ctx) ir_expression(ir_unop_u2i, src));
801 break;
802 case GLSL_TYPE_INT:
803 result = new(ctx) ir_expression(ir_unop_i2b, desired_type, src, NULL);
804 break;
805 case GLSL_TYPE_FLOAT:
806 result = new(ctx) ir_expression(ir_unop_f2b, desired_type, src, NULL);
807 break;
808 case GLSL_TYPE_DOUBLE:
809 result = new(ctx) ir_expression(ir_unop_d2b, desired_type, src, NULL);
810 break;
811 }
812 break;
813 case GLSL_TYPE_DOUBLE:
814 switch (b) {
815 case GLSL_TYPE_INT:
816 result = new(ctx) ir_expression(ir_unop_i2d, src);
817 break;
818 case GLSL_TYPE_UINT:
819 result = new(ctx) ir_expression(ir_unop_u2d, src);
820 break;
821 case GLSL_TYPE_BOOL:
822 result = new(ctx) ir_expression(ir_unop_f2d,
823 new(ctx) ir_expression(ir_unop_b2f, src));
824 break;
825 case GLSL_TYPE_FLOAT:
826 result = new(ctx) ir_expression(ir_unop_f2d, desired_type, src, NULL);
827 break;
828 }
829 }
830
831 assert(result != NULL);
832 assert(result->type == desired_type);
833
834 /* Try constant folding; it may fold in the conversion we just added. */
835 ir_constant *const constant = result->constant_expression_value();
836 return (constant != NULL) ? (ir_rvalue *) constant : (ir_rvalue *) result;
837 }
838
839
840 /**
841 * Perform automatic type and constant conversion of constructor parameters
842 *
843 * This implements the rules in the "Implicit Conversions" rules, not the
844 * "Conversion and Scalar Constructors".
845 *
846 * After attempting the implicit conversion, an attempt to convert into a
847 * constant valued expression is also done.
848 *
849 * The \c from \c ir_rvalue is converted "in place".
850 *
851 * \param from Operand that is being converted
852 * \param to Base type the operand will be converted to
853 * \param state GLSL compiler state
854 *
855 * \return
856 * If the attempt to convert into a constant expression succeeds, \c true is
857 * returned. Otherwise \c false is returned.
858 */
859 static bool
860 implicitly_convert_component(ir_rvalue * &from, const glsl_base_type to,
861 struct _mesa_glsl_parse_state *state)
862 {
863 ir_rvalue *result = from;
864
865 if (to != from->type->base_type) {
866 const glsl_type *desired_type =
867 glsl_type::get_instance(to,
868 from->type->vector_elements,
869 from->type->matrix_columns);
870
871 if (from->type->can_implicitly_convert_to(desired_type, state)) {
872 /* Even though convert_component() implements the constructor
873 * conversion rules (not the implicit conversion rules), its safe
874 * to use it here because we already checked that the implicit
875 * conversion is legal.
876 */
877 result = convert_component(from, desired_type);
878 }
879 }
880
881 ir_rvalue *const constant = result->constant_expression_value();
882
883 if (constant != NULL)
884 result = constant;
885
886 if (from != result) {
887 from->replace_with(result);
888 from = result;
889 }
890
891 return constant != NULL;
892 }
893
894
895 /**
896 * Dereference a specific component from a scalar, vector, or matrix
897 */
898 static ir_rvalue *
899 dereference_component(ir_rvalue *src, unsigned component)
900 {
901 void *ctx = ralloc_parent(src);
902 assert(component < src->type->components());
903
904 /* If the source is a constant, just create a new constant instead of a
905 * dereference of the existing constant.
906 */
907 ir_constant *constant = src->as_constant();
908 if (constant)
909 return new(ctx) ir_constant(constant, component);
910
911 if (src->type->is_scalar()) {
912 return src;
913 } else if (src->type->is_vector()) {
914 return new(ctx) ir_swizzle(src, component, 0, 0, 0, 1);
915 } else {
916 assert(src->type->is_matrix());
917
918 /* Dereference a row of the matrix, then call this function again to get
919 * a specific element from that row.
920 */
921 const int c = component / src->type->column_type()->vector_elements;
922 const int r = component % src->type->column_type()->vector_elements;
923 ir_constant *const col_index = new(ctx) ir_constant(c);
924 ir_dereference *const col = new(ctx) ir_dereference_array(src, col_index);
925
926 col->type = src->type->column_type();
927
928 return dereference_component(col, r);
929 }
930
931 assert(!"Should not get here.");
932 return NULL;
933 }
934
935
936 static ir_rvalue *
937 process_vec_mat_constructor(exec_list *instructions,
938 const glsl_type *constructor_type,
939 YYLTYPE *loc, exec_list *parameters,
940 struct _mesa_glsl_parse_state *state)
941 {
942 void *ctx = state;
943
944 /* The ARB_shading_language_420pack spec says:
945 *
946 * "If an initializer is a list of initializers enclosed in curly braces,
947 * the variable being declared must be a vector, a matrix, an array, or a
948 * structure.
949 *
950 * int i = { 1 }; // illegal, i is not an aggregate"
951 */
952 if (constructor_type->vector_elements <= 1) {
953 _mesa_glsl_error(loc, state, "aggregates can only initialize vectors, "
954 "matrices, arrays, and structs");
955 return ir_rvalue::error_value(ctx);
956 }
957
958 exec_list actual_parameters;
959 const unsigned parameter_count =
960 process_parameters(instructions, &actual_parameters, parameters, state);
961
962 if (parameter_count == 0
963 || (constructor_type->is_vector() &&
964 constructor_type->vector_elements != parameter_count)
965 || (constructor_type->is_matrix() &&
966 constructor_type->matrix_columns != parameter_count)) {
967 _mesa_glsl_error(loc, state, "%s constructor must have %u parameters",
968 constructor_type->is_vector() ? "vector" : "matrix",
969 constructor_type->vector_elements);
970 return ir_rvalue::error_value(ctx);
971 }
972
973 bool all_parameters_are_constant = true;
974
975 /* Type cast each parameter and, if possible, fold constants. */
976 foreach_in_list_safe(ir_rvalue, ir, &actual_parameters) {
977 /* Apply implicit conversions (not the scalar constructor rules, see the
978 * spec quote above!) and attempt to convert the parameter to a constant
979 * valued expression. After doing so, track whether or not all the
980 * parameters to the constructor are trivially constant valued
981 * expressions.
982 */
983 all_parameters_are_constant &=
984 implicitly_convert_component(ir, constructor_type->base_type, state);
985
986 if (constructor_type->is_matrix()) {
987 if (ir->type != constructor_type->column_type()) {
988 _mesa_glsl_error(loc, state, "type error in matrix constructor: "
989 "expected: %s, found %s",
990 constructor_type->column_type()->name,
991 ir->type->name);
992 return ir_rvalue::error_value(ctx);
993 }
994 } else if (ir->type != constructor_type->get_scalar_type()) {
995 _mesa_glsl_error(loc, state, "type error in vector constructor: "
996 "expected: %s, found %s",
997 constructor_type->get_scalar_type()->name,
998 ir->type->name);
999 return ir_rvalue::error_value(ctx);
1000 }
1001 }
1002
1003 if (all_parameters_are_constant)
1004 return new(ctx) ir_constant(constructor_type, &actual_parameters);
1005
1006 ir_variable *var = new(ctx) ir_variable(constructor_type, "vec_mat_ctor",
1007 ir_var_temporary);
1008 instructions->push_tail(var);
1009
1010 int i = 0;
1011
1012 foreach_in_list(ir_rvalue, rhs, &actual_parameters) {
1013 ir_instruction *assignment = NULL;
1014
1015 if (var->type->is_matrix()) {
1016 ir_rvalue *lhs = new(ctx) ir_dereference_array(var,
1017 new(ctx) ir_constant(i));
1018 assignment = new(ctx) ir_assignment(lhs, rhs, NULL);
1019 } else {
1020 /* use writemask rather than index for vector */
1021 assert(var->type->is_vector());
1022 assert(i < 4);
1023 ir_dereference *lhs = new(ctx) ir_dereference_variable(var);
1024 assignment = new(ctx) ir_assignment(lhs, rhs, NULL, (unsigned)(1 << i));
1025 }
1026
1027 instructions->push_tail(assignment);
1028
1029 i++;
1030 }
1031
1032 return new(ctx) ir_dereference_variable(var);
1033 }
1034
1035
1036 static ir_rvalue *
1037 process_array_constructor(exec_list *instructions,
1038 const glsl_type *constructor_type,
1039 YYLTYPE *loc, exec_list *parameters,
1040 struct _mesa_glsl_parse_state *state)
1041 {
1042 void *ctx = state;
1043 /* Array constructors come in two forms: sized and unsized. Sized array
1044 * constructors look like 'vec4[2](a, b)', where 'a' and 'b' are vec4
1045 * variables. In this case the number of parameters must exactly match the
1046 * specified size of the array.
1047 *
1048 * Unsized array constructors look like 'vec4[](a, b)', where 'a' and 'b'
1049 * are vec4 variables. In this case the size of the array being constructed
1050 * is determined by the number of parameters.
1051 *
1052 * From page 52 (page 58 of the PDF) of the GLSL 1.50 spec:
1053 *
1054 * "There must be exactly the same number of arguments as the size of
1055 * the array being constructed. If no size is present in the
1056 * constructor, then the array is explicitly sized to the number of
1057 * arguments provided. The arguments are assigned in order, starting at
1058 * element 0, to the elements of the constructed array. Each argument
1059 * must be the same type as the element type of the array, or be a type
1060 * that can be converted to the element type of the array according to
1061 * Section 4.1.10 "Implicit Conversions.""
1062 */
1063 exec_list actual_parameters;
1064 const unsigned parameter_count =
1065 process_parameters(instructions, &actual_parameters, parameters, state);
1066 bool is_unsized_array = constructor_type->is_unsized_array();
1067
1068 if ((parameter_count == 0) ||
1069 (!is_unsized_array && (constructor_type->length != parameter_count))) {
1070 const unsigned min_param = is_unsized_array
1071 ? 1 : constructor_type->length;
1072
1073 _mesa_glsl_error(loc, state, "array constructor must have %s %u "
1074 "parameter%s",
1075 is_unsized_array ? "at least" : "exactly",
1076 min_param, (min_param <= 1) ? "" : "s");
1077 return ir_rvalue::error_value(ctx);
1078 }
1079
1080 if (is_unsized_array) {
1081 constructor_type =
1082 glsl_type::get_array_instance(constructor_type->fields.array,
1083 parameter_count);
1084 assert(constructor_type != NULL);
1085 assert(constructor_type->length == parameter_count);
1086 }
1087
1088 bool all_parameters_are_constant = true;
1089 const glsl_type *element_type = constructor_type->fields.array;
1090
1091 /* Type cast each parameter and, if possible, fold constants. */
1092 foreach_in_list_safe(ir_rvalue, ir, &actual_parameters) {
1093 /* Apply implicit conversions (not the scalar constructor rules, see the
1094 * spec quote above!) and attempt to convert the parameter to a constant
1095 * valued expression. After doing so, track whether or not all the
1096 * parameters to the constructor are trivially constant valued
1097 * expressions.
1098 */
1099 all_parameters_are_constant &=
1100 implicitly_convert_component(ir, element_type->base_type, state);
1101
1102 if (constructor_type->fields.array->is_unsized_array()) {
1103 /* As the inner parameters of the constructor are created without
1104 * knowledge of each other we need to check to make sure unsized
1105 * parameters of unsized constructors all end up with the same size.
1106 *
1107 * e.g we make sure to fail for a constructor like this:
1108 * vec4[][] a = vec4[][](vec4[](vec4(0.0), vec4(1.0)),
1109 * vec4[](vec4(0.0), vec4(1.0), vec4(1.0)),
1110 * vec4[](vec4(0.0), vec4(1.0)));
1111 */
1112 if (element_type->is_unsized_array()) {
1113 /* This is the first parameter so just get the type */
1114 element_type = ir->type;
1115 } else if (element_type != ir->type) {
1116 _mesa_glsl_error(loc, state, "type error in array constructor: "
1117 "expected: %s, found %s",
1118 element_type->name,
1119 ir->type->name);
1120 return ir_rvalue::error_value(ctx);
1121 }
1122 } else if (ir->type != constructor_type->fields.array) {
1123 _mesa_glsl_error(loc, state, "type error in array constructor: "
1124 "expected: %s, found %s",
1125 constructor_type->fields.array->name,
1126 ir->type->name);
1127 return ir_rvalue::error_value(ctx);
1128 } else {
1129 element_type = ir->type;
1130 }
1131 }
1132
1133 if (constructor_type->fields.array->is_unsized_array()) {
1134 constructor_type =
1135 glsl_type::get_array_instance(element_type,
1136 parameter_count);
1137 assert(constructor_type != NULL);
1138 assert(constructor_type->length == parameter_count);
1139 }
1140
1141 if (all_parameters_are_constant)
1142 return new(ctx) ir_constant(constructor_type, &actual_parameters);
1143
1144 ir_variable *var = new(ctx) ir_variable(constructor_type, "array_ctor",
1145 ir_var_temporary);
1146 instructions->push_tail(var);
1147
1148 int i = 0;
1149 foreach_in_list(ir_rvalue, rhs, &actual_parameters) {
1150 ir_rvalue *lhs = new(ctx) ir_dereference_array(var,
1151 new(ctx) ir_constant(i));
1152
1153 ir_instruction *assignment = new(ctx) ir_assignment(lhs, rhs, NULL);
1154 instructions->push_tail(assignment);
1155
1156 i++;
1157 }
1158
1159 return new(ctx) ir_dereference_variable(var);
1160 }
1161
1162
1163 /**
1164 * Determine if a list consists of a single scalar r-value
1165 */
1166 bool
1167 single_scalar_parameter(exec_list *parameters)
1168 {
1169 const ir_rvalue *const p = (ir_rvalue *) parameters->get_head_raw();
1170 assert(((ir_rvalue *)p)->as_rvalue() != NULL);
1171
1172 return (p->type->is_scalar() && p->next->is_tail_sentinel());
1173 }
1174
1175
1176 /**
1177 * Generate inline code for a vector constructor
1178 *
1179 * The generated constructor code will consist of a temporary variable
1180 * declaration of the same type as the constructor. A sequence of assignments
1181 * from constructor parameters to the temporary will follow.
1182 *
1183 * \return
1184 * An \c ir_dereference_variable of the temprorary generated in the constructor
1185 * body.
1186 */
1187 ir_rvalue *
1188 emit_inline_vector_constructor(const glsl_type *type,
1189 exec_list *instructions,
1190 exec_list *parameters,
1191 void *ctx)
1192 {
1193 assert(!parameters->is_empty());
1194
1195 ir_variable *var = new(ctx) ir_variable(type, "vec_ctor", ir_var_temporary);
1196 instructions->push_tail(var);
1197
1198 /* There are three kinds of vector constructors.
1199 *
1200 * - Construct a vector from a single scalar by replicating that scalar to
1201 * all components of the vector.
1202 *
1203 * - Construct a vector from at least a matrix. This case should already
1204 * have been taken care of in ast_function_expression::hir by breaking
1205 * down the matrix into a series of column vectors.
1206 *
1207 * - Construct a vector from an arbirary combination of vectors and
1208 * scalars. The components of the constructor parameters are assigned
1209 * to the vector in order until the vector is full.
1210 */
1211 const unsigned lhs_components = type->components();
1212 if (single_scalar_parameter(parameters)) {
1213 ir_rvalue *first_param = (ir_rvalue *)parameters->get_head_raw();
1214 ir_rvalue *rhs = new(ctx) ir_swizzle(first_param, 0, 0, 0, 0,
1215 lhs_components);
1216 ir_dereference_variable *lhs = new(ctx) ir_dereference_variable(var);
1217 const unsigned mask = (1U << lhs_components) - 1;
1218
1219 assert(rhs->type == lhs->type);
1220
1221 ir_instruction *inst = new(ctx) ir_assignment(lhs, rhs, NULL, mask);
1222 instructions->push_tail(inst);
1223 } else {
1224 unsigned base_component = 0;
1225 unsigned base_lhs_component = 0;
1226 ir_constant_data data;
1227 unsigned constant_mask = 0, constant_components = 0;
1228
1229 memset(&data, 0, sizeof(data));
1230
1231 foreach_in_list(ir_rvalue, param, parameters) {
1232 unsigned rhs_components = param->type->components();
1233
1234 /* Do not try to assign more components to the vector than it has!
1235 */
1236 if ((rhs_components + base_lhs_component) > lhs_components) {
1237 rhs_components = lhs_components - base_lhs_component;
1238 }
1239
1240 const ir_constant *const c = param->as_constant();
1241 if (c != NULL) {
1242 for (unsigned i = 0; i < rhs_components; i++) {
1243 switch (c->type->base_type) {
1244 case GLSL_TYPE_UINT:
1245 data.u[i + base_component] = c->get_uint_component(i);
1246 break;
1247 case GLSL_TYPE_INT:
1248 data.i[i + base_component] = c->get_int_component(i);
1249 break;
1250 case GLSL_TYPE_FLOAT:
1251 data.f[i + base_component] = c->get_float_component(i);
1252 break;
1253 case GLSL_TYPE_DOUBLE:
1254 data.d[i + base_component] = c->get_double_component(i);
1255 break;
1256 case GLSL_TYPE_BOOL:
1257 data.b[i + base_component] = c->get_bool_component(i);
1258 break;
1259 default:
1260 assert(!"Should not get here.");
1261 break;
1262 }
1263 }
1264
1265 /* Mask of fields to be written in the assignment.
1266 */
1267 constant_mask |= ((1U << rhs_components) - 1) << base_lhs_component;
1268 constant_components += rhs_components;
1269
1270 base_component += rhs_components;
1271 }
1272 /* Advance the component index by the number of components
1273 * that were just assigned.
1274 */
1275 base_lhs_component += rhs_components;
1276 }
1277
1278 if (constant_mask != 0) {
1279 ir_dereference *lhs = new(ctx) ir_dereference_variable(var);
1280 const glsl_type *rhs_type = glsl_type::get_instance(var->type->base_type,
1281 constant_components,
1282 1);
1283 ir_rvalue *rhs = new(ctx) ir_constant(rhs_type, &data);
1284
1285 ir_instruction *inst =
1286 new(ctx) ir_assignment(lhs, rhs, NULL, constant_mask);
1287 instructions->push_tail(inst);
1288 }
1289
1290 base_component = 0;
1291 foreach_in_list(ir_rvalue, param, parameters) {
1292 unsigned rhs_components = param->type->components();
1293
1294 /* Do not try to assign more components to the vector than it has!
1295 */
1296 if ((rhs_components + base_component) > lhs_components) {
1297 rhs_components = lhs_components - base_component;
1298 }
1299
1300 /* If we do not have any components left to copy, break out of the
1301 * loop. This can happen when initializing a vec4 with a mat3 as the
1302 * mat3 would have been broken into a series of column vectors.
1303 */
1304 if (rhs_components == 0) {
1305 break;
1306 }
1307
1308 const ir_constant *const c = param->as_constant();
1309 if (c == NULL) {
1310 /* Mask of fields to be written in the assignment.
1311 */
1312 const unsigned write_mask = ((1U << rhs_components) - 1)
1313 << base_component;
1314
1315 ir_dereference *lhs = new(ctx) ir_dereference_variable(var);
1316
1317 /* Generate a swizzle so that LHS and RHS sizes match.
1318 */
1319 ir_rvalue *rhs =
1320 new(ctx) ir_swizzle(param, 0, 1, 2, 3, rhs_components);
1321
1322 ir_instruction *inst =
1323 new(ctx) ir_assignment(lhs, rhs, NULL, write_mask);
1324 instructions->push_tail(inst);
1325 }
1326
1327 /* Advance the component index by the number of components that were
1328 * just assigned.
1329 */
1330 base_component += rhs_components;
1331 }
1332 }
1333 return new(ctx) ir_dereference_variable(var);
1334 }
1335
1336
1337 /**
1338 * Generate assignment of a portion of a vector to a portion of a matrix column
1339 *
1340 * \param src_base First component of the source to be used in assignment
1341 * \param column Column of destination to be assiged
1342 * \param row_base First component of the destination column to be assigned
1343 * \param count Number of components to be assigned
1344 *
1345 * \note
1346 * \c src_base + \c count must be less than or equal to the number of components
1347 * in the source vector.
1348 */
1349 ir_instruction *
1350 assign_to_matrix_column(ir_variable *var, unsigned column, unsigned row_base,
1351 ir_rvalue *src, unsigned src_base, unsigned count,
1352 void *mem_ctx)
1353 {
1354 ir_constant *col_idx = new(mem_ctx) ir_constant(column);
1355 ir_dereference *column_ref = new(mem_ctx) ir_dereference_array(var, col_idx);
1356
1357 assert(column_ref->type->components() >= (row_base + count));
1358 assert(src->type->components() >= (src_base + count));
1359
1360 /* Generate a swizzle that extracts the number of components from the source
1361 * that are to be assigned to the column of the matrix.
1362 */
1363 if (count < src->type->vector_elements) {
1364 src = new(mem_ctx) ir_swizzle(src,
1365 src_base + 0, src_base + 1,
1366 src_base + 2, src_base + 3,
1367 count);
1368 }
1369
1370 /* Mask of fields to be written in the assignment.
1371 */
1372 const unsigned write_mask = ((1U << count) - 1) << row_base;
1373
1374 return new(mem_ctx) ir_assignment(column_ref, src, NULL, write_mask);
1375 }
1376
1377
1378 /**
1379 * Generate inline code for a matrix constructor
1380 *
1381 * The generated constructor code will consist of a temporary variable
1382 * declaration of the same type as the constructor. A sequence of assignments
1383 * from constructor parameters to the temporary will follow.
1384 *
1385 * \return
1386 * An \c ir_dereference_variable of the temprorary generated in the constructor
1387 * body.
1388 */
1389 ir_rvalue *
1390 emit_inline_matrix_constructor(const glsl_type *type,
1391 exec_list *instructions,
1392 exec_list *parameters,
1393 void *ctx)
1394 {
1395 assert(!parameters->is_empty());
1396
1397 ir_variable *var = new(ctx) ir_variable(type, "mat_ctor", ir_var_temporary);
1398 instructions->push_tail(var);
1399
1400 /* There are three kinds of matrix constructors.
1401 *
1402 * - Construct a matrix from a single scalar by replicating that scalar to
1403 * along the diagonal of the matrix and setting all other components to
1404 * zero.
1405 *
1406 * - Construct a matrix from an arbirary combination of vectors and
1407 * scalars. The components of the constructor parameters are assigned
1408 * to the matrix in column-major order until the matrix is full.
1409 *
1410 * - Construct a matrix from a single matrix. The source matrix is copied
1411 * to the upper left portion of the constructed matrix, and the remaining
1412 * elements take values from the identity matrix.
1413 */
1414 ir_rvalue *const first_param = (ir_rvalue *) parameters->get_head_raw();
1415 if (single_scalar_parameter(parameters)) {
1416 /* Assign the scalar to the X component of a vec4, and fill the remaining
1417 * components with zero.
1418 */
1419 glsl_base_type param_base_type = first_param->type->base_type;
1420 assert(param_base_type == GLSL_TYPE_FLOAT ||
1421 param_base_type == GLSL_TYPE_DOUBLE);
1422 ir_variable *rhs_var =
1423 new(ctx) ir_variable(glsl_type::get_instance(param_base_type, 4, 1),
1424 "mat_ctor_vec",
1425 ir_var_temporary);
1426 instructions->push_tail(rhs_var);
1427
1428 ir_constant_data zero;
1429 for (unsigned i = 0; i < 4; i++)
1430 if (param_base_type == GLSL_TYPE_FLOAT)
1431 zero.f[i] = 0.0;
1432 else
1433 zero.d[i] = 0.0;
1434
1435 ir_instruction *inst =
1436 new(ctx) ir_assignment(new(ctx) ir_dereference_variable(rhs_var),
1437 new(ctx) ir_constant(rhs_var->type, &zero),
1438 NULL);
1439 instructions->push_tail(inst);
1440
1441 ir_dereference *const rhs_ref = new(ctx) ir_dereference_variable(rhs_var);
1442
1443 inst = new(ctx) ir_assignment(rhs_ref, first_param, NULL, 0x01);
1444 instructions->push_tail(inst);
1445
1446 /* Assign the temporary vector to each column of the destination matrix
1447 * with a swizzle that puts the X component on the diagonal of the
1448 * matrix. In some cases this may mean that the X component does not
1449 * get assigned into the column at all (i.e., when the matrix has more
1450 * columns than rows).
1451 */
1452 static const unsigned rhs_swiz[4][4] = {
1453 { 0, 1, 1, 1 },
1454 { 1, 0, 1, 1 },
1455 { 1, 1, 0, 1 },
1456 { 1, 1, 1, 0 }
1457 };
1458
1459 const unsigned cols_to_init = MIN2(type->matrix_columns,
1460 type->vector_elements);
1461 for (unsigned i = 0; i < cols_to_init; i++) {
1462 ir_constant *const col_idx = new(ctx) ir_constant(i);
1463 ir_rvalue *const col_ref = new(ctx) ir_dereference_array(var, col_idx);
1464
1465 ir_rvalue *const rhs_ref = new(ctx) ir_dereference_variable(rhs_var);
1466 ir_rvalue *const rhs = new(ctx) ir_swizzle(rhs_ref, rhs_swiz[i],
1467 type->vector_elements);
1468
1469 inst = new(ctx) ir_assignment(col_ref, rhs, NULL);
1470 instructions->push_tail(inst);
1471 }
1472
1473 for (unsigned i = cols_to_init; i < type->matrix_columns; i++) {
1474 ir_constant *const col_idx = new(ctx) ir_constant(i);
1475 ir_rvalue *const col_ref = new(ctx) ir_dereference_array(var, col_idx);
1476
1477 ir_rvalue *const rhs_ref = new(ctx) ir_dereference_variable(rhs_var);
1478 ir_rvalue *const rhs = new(ctx) ir_swizzle(rhs_ref, 1, 1, 1, 1,
1479 type->vector_elements);
1480
1481 inst = new(ctx) ir_assignment(col_ref, rhs, NULL);
1482 instructions->push_tail(inst);
1483 }
1484 } else if (first_param->type->is_matrix()) {
1485 /* From page 50 (56 of the PDF) of the GLSL 1.50 spec:
1486 *
1487 * "If a matrix is constructed from a matrix, then each component
1488 * (column i, row j) in the result that has a corresponding
1489 * component (column i, row j) in the argument will be initialized
1490 * from there. All other components will be initialized to the
1491 * identity matrix. If a matrix argument is given to a matrix
1492 * constructor, it is an error to have any other arguments."
1493 */
1494 assert(first_param->next->is_tail_sentinel());
1495 ir_rvalue *const src_matrix = first_param;
1496
1497 /* If the source matrix is smaller, pre-initialize the relavent parts of
1498 * the destination matrix to the identity matrix.
1499 */
1500 if ((src_matrix->type->matrix_columns < var->type->matrix_columns) ||
1501 (src_matrix->type->vector_elements < var->type->vector_elements)) {
1502
1503 /* If the source matrix has fewer rows, every column of the destination
1504 * must be initialized. Otherwise only the columns in the destination
1505 * that do not exist in the source must be initialized.
1506 */
1507 unsigned col =
1508 (src_matrix->type->vector_elements < var->type->vector_elements)
1509 ? 0 : src_matrix->type->matrix_columns;
1510
1511 const glsl_type *const col_type = var->type->column_type();
1512 for (/* empty */; col < var->type->matrix_columns; col++) {
1513 ir_constant_data ident;
1514
1515 if (!col_type->is_double()) {
1516 ident.f[0] = 0.0f;
1517 ident.f[1] = 0.0f;
1518 ident.f[2] = 0.0f;
1519 ident.f[3] = 0.0f;
1520 ident.f[col] = 1.0f;
1521 } else {
1522 ident.d[0] = 0.0;
1523 ident.d[1] = 0.0;
1524 ident.d[2] = 0.0;
1525 ident.d[3] = 0.0;
1526 ident.d[col] = 1.0;
1527 }
1528
1529 ir_rvalue *const rhs = new(ctx) ir_constant(col_type, &ident);
1530
1531 ir_rvalue *const lhs =
1532 new(ctx) ir_dereference_array(var, new(ctx) ir_constant(col));
1533
1534 ir_instruction *inst = new(ctx) ir_assignment(lhs, rhs, NULL);
1535 instructions->push_tail(inst);
1536 }
1537 }
1538
1539 /* Assign columns from the source matrix to the destination matrix.
1540 *
1541 * Since the parameter will be used in the RHS of multiple assignments,
1542 * generate a temporary and copy the paramter there.
1543 */
1544 ir_variable *const rhs_var =
1545 new(ctx) ir_variable(first_param->type, "mat_ctor_mat",
1546 ir_var_temporary);
1547 instructions->push_tail(rhs_var);
1548
1549 ir_dereference *const rhs_var_ref =
1550 new(ctx) ir_dereference_variable(rhs_var);
1551 ir_instruction *const inst =
1552 new(ctx) ir_assignment(rhs_var_ref, first_param, NULL);
1553 instructions->push_tail(inst);
1554
1555 const unsigned last_row = MIN2(src_matrix->type->vector_elements,
1556 var->type->vector_elements);
1557 const unsigned last_col = MIN2(src_matrix->type->matrix_columns,
1558 var->type->matrix_columns);
1559
1560 unsigned swiz[4] = { 0, 0, 0, 0 };
1561 for (unsigned i = 1; i < last_row; i++)
1562 swiz[i] = i;
1563
1564 const unsigned write_mask = (1U << last_row) - 1;
1565
1566 for (unsigned i = 0; i < last_col; i++) {
1567 ir_dereference *const lhs =
1568 new(ctx) ir_dereference_array(var, new(ctx) ir_constant(i));
1569 ir_rvalue *const rhs_col =
1570 new(ctx) ir_dereference_array(rhs_var, new(ctx) ir_constant(i));
1571
1572 /* If one matrix has columns that are smaller than the columns of the
1573 * other matrix, wrap the column access of the larger with a swizzle
1574 * so that the LHS and RHS of the assignment have the same size (and
1575 * therefore have the same type).
1576 *
1577 * It would be perfectly valid to unconditionally generate the
1578 * swizzles, this this will typically result in a more compact IR tree.
1579 */
1580 ir_rvalue *rhs;
1581 if (lhs->type->vector_elements != rhs_col->type->vector_elements) {
1582 rhs = new(ctx) ir_swizzle(rhs_col, swiz, last_row);
1583 } else {
1584 rhs = rhs_col;
1585 }
1586
1587 ir_instruction *inst =
1588 new(ctx) ir_assignment(lhs, rhs, NULL, write_mask);
1589 instructions->push_tail(inst);
1590 }
1591 } else {
1592 const unsigned cols = type->matrix_columns;
1593 const unsigned rows = type->vector_elements;
1594 unsigned remaining_slots = rows * cols;
1595 unsigned col_idx = 0;
1596 unsigned row_idx = 0;
1597
1598 foreach_in_list(ir_rvalue, rhs, parameters) {
1599 unsigned rhs_components = rhs->type->components();
1600 unsigned rhs_base = 0;
1601
1602 if (remaining_slots == 0)
1603 break;
1604
1605 /* Since the parameter might be used in the RHS of two assignments,
1606 * generate a temporary and copy the paramter there.
1607 */
1608 ir_variable *rhs_var =
1609 new(ctx) ir_variable(rhs->type, "mat_ctor_vec", ir_var_temporary);
1610 instructions->push_tail(rhs_var);
1611
1612 ir_dereference *rhs_var_ref =
1613 new(ctx) ir_dereference_variable(rhs_var);
1614 ir_instruction *inst = new(ctx) ir_assignment(rhs_var_ref, rhs, NULL);
1615 instructions->push_tail(inst);
1616
1617 do {
1618 /* Assign the current parameter to as many components of the matrix
1619 * as it will fill.
1620 *
1621 * NOTE: A single vector parameter can span two matrix columns. A
1622 * single vec4, for example, can completely fill a mat2.
1623 */
1624 unsigned count = MIN2(rows - row_idx,
1625 rhs_components - rhs_base);
1626
1627 rhs_var_ref = new(ctx) ir_dereference_variable(rhs_var);
1628 ir_instruction *inst = assign_to_matrix_column(var, col_idx,
1629 row_idx,
1630 rhs_var_ref,
1631 rhs_base,
1632 count, ctx);
1633 instructions->push_tail(inst);
1634 rhs_base += count;
1635 row_idx += count;
1636 remaining_slots -= count;
1637
1638 /* Sometimes, there is still data left in the parameters and
1639 * components left to be set in the destination but in other
1640 * column.
1641 */
1642 if (row_idx >= rows) {
1643 row_idx = 0;
1644 col_idx++;
1645 }
1646 } while(remaining_slots > 0 && rhs_base < rhs_components);
1647 }
1648 }
1649
1650 return new(ctx) ir_dereference_variable(var);
1651 }
1652
1653
1654 ir_rvalue *
1655 emit_inline_record_constructor(const glsl_type *type,
1656 exec_list *instructions,
1657 exec_list *parameters,
1658 void *mem_ctx)
1659 {
1660 ir_variable *const var =
1661 new(mem_ctx) ir_variable(type, "record_ctor", ir_var_temporary);
1662 ir_dereference_variable *const d = new(mem_ctx) ir_dereference_variable(var);
1663
1664 instructions->push_tail(var);
1665
1666 exec_node *node = parameters->get_head_raw();
1667 for (unsigned i = 0; i < type->length; i++) {
1668 assert(!node->is_tail_sentinel());
1669
1670 ir_dereference *const lhs =
1671 new(mem_ctx) ir_dereference_record(d->clone(mem_ctx, NULL),
1672 type->fields.structure[i].name);
1673
1674 ir_rvalue *const rhs = ((ir_instruction *) node)->as_rvalue();
1675 assert(rhs != NULL);
1676
1677 ir_instruction *const assign = new(mem_ctx) ir_assignment(lhs, rhs, NULL);
1678
1679 instructions->push_tail(assign);
1680 node = node->next;
1681 }
1682
1683 return d;
1684 }
1685
1686
1687 static ir_rvalue *
1688 process_record_constructor(exec_list *instructions,
1689 const glsl_type *constructor_type,
1690 YYLTYPE *loc, exec_list *parameters,
1691 struct _mesa_glsl_parse_state *state)
1692 {
1693 void *ctx = state;
1694 /* From page 32 (page 38 of the PDF) of the GLSL 1.20 spec:
1695 *
1696 * "The arguments to the constructor will be used to set the structure's
1697 * fields, in order, using one argument per field. Each argument must
1698 * be the same type as the field it sets, or be a type that can be
1699 * converted to the field's type according to Section 4.1.10 “Implicit
1700 * Conversions.”"
1701 *
1702 * From page 35 (page 41 of the PDF) of the GLSL 4.20 spec:
1703 *
1704 * "In all cases, the innermost initializer (i.e., not a list of
1705 * initializers enclosed in curly braces) applied to an object must
1706 * have the same type as the object being initialized or be a type that
1707 * can be converted to the object's type according to section 4.1.10
1708 * "Implicit Conversions". In the latter case, an implicit conversion
1709 * will be done on the initializer before the assignment is done."
1710 */
1711 exec_list actual_parameters;
1712
1713 const unsigned parameter_count =
1714 process_parameters(instructions, &actual_parameters, parameters,
1715 state);
1716
1717 if (parameter_count != constructor_type->length) {
1718 _mesa_glsl_error(loc, state,
1719 "%s parameters in constructor for `%s'",
1720 parameter_count > constructor_type->length
1721 ? "too many": "insufficient",
1722 constructor_type->name);
1723 return ir_rvalue::error_value(ctx);
1724 }
1725
1726 bool all_parameters_are_constant = true;
1727
1728 int i = 0;
1729 /* Type cast each parameter and, if possible, fold constants. */
1730 foreach_in_list_safe(ir_rvalue, ir, &actual_parameters) {
1731
1732 const glsl_struct_field *struct_field =
1733 &constructor_type->fields.structure[i];
1734
1735 /* Apply implicit conversions (not the scalar constructor rules, see the
1736 * spec quote above!) and attempt to convert the parameter to a constant
1737 * valued expression. After doing so, track whether or not all the
1738 * parameters to the constructor are trivially constant valued
1739 * expressions.
1740 */
1741 all_parameters_are_constant &=
1742 implicitly_convert_component(ir, struct_field->type->base_type,
1743 state);
1744
1745 if (ir->type != struct_field->type) {
1746 _mesa_glsl_error(loc, state,
1747 "parameter type mismatch in constructor for `%s.%s' "
1748 "(%s vs %s)",
1749 constructor_type->name,
1750 struct_field->name,
1751 ir->type->name,
1752 struct_field->type->name);
1753 return ir_rvalue::error_value(ctx);
1754 }
1755
1756 i++;
1757 }
1758
1759 if (all_parameters_are_constant) {
1760 return new(ctx) ir_constant(constructor_type, &actual_parameters);
1761 } else {
1762 return emit_inline_record_constructor(constructor_type, instructions,
1763 &actual_parameters, state);
1764 }
1765 }
1766
1767 ir_rvalue *
1768 ast_function_expression::handle_method(exec_list *instructions,
1769 struct _mesa_glsl_parse_state *state)
1770 {
1771 const ast_expression *field = subexpressions[0];
1772 ir_rvalue *op;
1773 ir_rvalue *result;
1774 void *ctx = state;
1775 /* Handle "method calls" in GLSL 1.20 - namely, array.length() */
1776 YYLTYPE loc = get_location();
1777 state->check_version(120, 300, &loc, "methods not supported");
1778
1779 const char *method;
1780 method = field->primary_expression.identifier;
1781
1782 /* This would prevent to raise "uninitialized variable" warnings when
1783 * calling array.length.
1784 */
1785 field->subexpressions[0]->set_is_lhs(true);
1786 op = field->subexpressions[0]->hir(instructions, state);
1787 if (strcmp(method, "length") == 0) {
1788 if (!this->expressions.is_empty()) {
1789 _mesa_glsl_error(&loc, state, "length method takes no arguments");
1790 goto fail;
1791 }
1792
1793 if (op->type->is_array()) {
1794 if (op->type->is_unsized_array()) {
1795 if (!state->has_shader_storage_buffer_objects()) {
1796 _mesa_glsl_error(&loc, state, "length called on unsized array"
1797 " only available with "
1798 "ARB_shader_storage_buffer_object");
1799 }
1800 /* Calculate length of an unsized array in run-time */
1801 result = new(ctx) ir_expression(ir_unop_ssbo_unsized_array_length, op);
1802 } else {
1803 result = new(ctx) ir_constant(op->type->array_size());
1804 }
1805 } else if (op->type->is_vector()) {
1806 if (state->has_420pack()) {
1807 /* .length() returns int. */
1808 result = new(ctx) ir_constant((int) op->type->vector_elements);
1809 } else {
1810 _mesa_glsl_error(&loc, state, "length method on matrix only available"
1811 "with ARB_shading_language_420pack");
1812 goto fail;
1813 }
1814 } else if (op->type->is_matrix()) {
1815 if (state->has_420pack()) {
1816 /* .length() returns int. */
1817 result = new(ctx) ir_constant((int) op->type->matrix_columns);
1818 } else {
1819 _mesa_glsl_error(&loc, state, "length method on matrix only available"
1820 "with ARB_shading_language_420pack");
1821 goto fail;
1822 }
1823 } else {
1824 _mesa_glsl_error(&loc, state, "length called on scalar.");
1825 goto fail;
1826 }
1827 } else {
1828 _mesa_glsl_error(&loc, state, "unknown method: `%s'", method);
1829 goto fail;
1830 }
1831 return result;
1832 fail:
1833 return ir_rvalue::error_value(ctx);
1834 }
1835
1836 ir_rvalue *
1837 ast_function_expression::hir(exec_list *instructions,
1838 struct _mesa_glsl_parse_state *state)
1839 {
1840 void *ctx = state;
1841 /* There are three sorts of function calls.
1842 *
1843 * 1. constructors - The first subexpression is an ast_type_specifier.
1844 * 2. methods - Only the .length() method of array types.
1845 * 3. functions - Calls to regular old functions.
1846 *
1847 */
1848 if (is_constructor()) {
1849 const ast_type_specifier *type = (ast_type_specifier *) subexpressions[0];
1850 YYLTYPE loc = type->get_location();
1851 const char *name;
1852
1853 const glsl_type *const constructor_type = type->glsl_type(& name, state);
1854
1855 /* constructor_type can be NULL if a variable with the same name as the
1856 * structure has come into scope.
1857 */
1858 if (constructor_type == NULL) {
1859 _mesa_glsl_error(& loc, state, "unknown type `%s' (structure name "
1860 "may be shadowed by a variable with the same name)",
1861 type->type_name);
1862 return ir_rvalue::error_value(ctx);
1863 }
1864
1865
1866 /* Constructors for opaque types are illegal.
1867 */
1868 if (constructor_type->contains_opaque()) {
1869 _mesa_glsl_error(& loc, state, "cannot construct opaque type `%s'",
1870 constructor_type->name);
1871 return ir_rvalue::error_value(ctx);
1872 }
1873
1874 if (constructor_type->is_subroutine()) {
1875 _mesa_glsl_error(& loc, state, "subroutine name cannot be a constructor `%s'",
1876 constructor_type->name);
1877 return ir_rvalue::error_value(ctx);
1878 }
1879
1880 if (constructor_type->is_array()) {
1881 if (!state->check_version(120, 300, &loc,
1882 "array constructors forbidden")) {
1883 return ir_rvalue::error_value(ctx);
1884 }
1885
1886 return process_array_constructor(instructions, constructor_type,
1887 & loc, &this->expressions, state);
1888 }
1889
1890
1891 /* There are two kinds of constructor calls. Constructors for arrays and
1892 * structures must have the exact number of arguments with matching types
1893 * in the correct order. These constructors follow essentially the same
1894 * type matching rules as functions.
1895 *
1896 * Constructors for built-in language types, such as mat4 and vec2, are
1897 * free form. The only requirements are that the parameters must provide
1898 * enough values of the correct scalar type and that no arguments are
1899 * given past the last used argument.
1900 *
1901 * When using the C-style initializer syntax from GLSL 4.20, constructors
1902 * must have the exact number of arguments with matching types in the
1903 * correct order.
1904 */
1905 if (constructor_type->is_record()) {
1906 return process_record_constructor(instructions, constructor_type,
1907 &loc, &this->expressions,
1908 state);
1909 }
1910
1911 if (!constructor_type->is_numeric() && !constructor_type->is_boolean())
1912 return ir_rvalue::error_value(ctx);
1913
1914 /* Total number of components of the type being constructed. */
1915 const unsigned type_components = constructor_type->components();
1916
1917 /* Number of components from parameters that have actually been
1918 * consumed. This is used to perform several kinds of error checking.
1919 */
1920 unsigned components_used = 0;
1921
1922 unsigned matrix_parameters = 0;
1923 unsigned nonmatrix_parameters = 0;
1924 exec_list actual_parameters;
1925
1926 foreach_list_typed(ast_node, ast, link, &this->expressions) {
1927 ir_rvalue *result = ast->hir(instructions, state);
1928
1929 /* From page 50 (page 56 of the PDF) of the GLSL 1.50 spec:
1930 *
1931 * "It is an error to provide extra arguments beyond this
1932 * last used argument."
1933 */
1934 if (components_used >= type_components) {
1935 _mesa_glsl_error(& loc, state, "too many parameters to `%s' "
1936 "constructor",
1937 constructor_type->name);
1938 return ir_rvalue::error_value(ctx);
1939 }
1940
1941 if (!result->type->is_numeric() && !result->type->is_boolean()) {
1942 _mesa_glsl_error(& loc, state, "cannot construct `%s' from a "
1943 "non-numeric data type",
1944 constructor_type->name);
1945 return ir_rvalue::error_value(ctx);
1946 }
1947
1948 /* Count the number of matrix and nonmatrix parameters. This
1949 * is used below to enforce some of the constructor rules.
1950 */
1951 if (result->type->is_matrix())
1952 matrix_parameters++;
1953 else
1954 nonmatrix_parameters++;
1955
1956 actual_parameters.push_tail(result);
1957 components_used += result->type->components();
1958 }
1959
1960 /* From page 28 (page 34 of the PDF) of the GLSL 1.10 spec:
1961 *
1962 * "It is an error to construct matrices from other matrices. This
1963 * is reserved for future use."
1964 */
1965 if (matrix_parameters > 0
1966 && constructor_type->is_matrix()
1967 && !state->check_version(120, 100, &loc,
1968 "cannot construct `%s' from a matrix",
1969 constructor_type->name)) {
1970 return ir_rvalue::error_value(ctx);
1971 }
1972
1973 /* From page 50 (page 56 of the PDF) of the GLSL 1.50 spec:
1974 *
1975 * "If a matrix argument is given to a matrix constructor, it is
1976 * an error to have any other arguments."
1977 */
1978 if ((matrix_parameters > 0)
1979 && ((matrix_parameters + nonmatrix_parameters) > 1)
1980 && constructor_type->is_matrix()) {
1981 _mesa_glsl_error(& loc, state, "for matrix `%s' constructor, "
1982 "matrix must be only parameter",
1983 constructor_type->name);
1984 return ir_rvalue::error_value(ctx);
1985 }
1986
1987 /* From page 28 (page 34 of the PDF) of the GLSL 1.10 spec:
1988 *
1989 * "In these cases, there must be enough components provided in the
1990 * arguments to provide an initializer for every component in the
1991 * constructed value."
1992 */
1993 if (components_used < type_components && components_used != 1
1994 && matrix_parameters == 0) {
1995 _mesa_glsl_error(& loc, state, "too few components to construct "
1996 "`%s'",
1997 constructor_type->name);
1998 return ir_rvalue::error_value(ctx);
1999 }
2000
2001 /* Matrices can never be consumed as is by any constructor but matrix
2002 * constructors. If the constructor type is not matrix, always break the
2003 * matrix up into a series of column vectors.
2004 */
2005 if (!constructor_type->is_matrix()) {
2006 foreach_in_list_safe(ir_rvalue, matrix, &actual_parameters) {
2007 if (!matrix->type->is_matrix())
2008 continue;
2009
2010 /* Create a temporary containing the matrix. */
2011 ir_variable *var = new(ctx) ir_variable(matrix->type, "matrix_tmp",
2012 ir_var_temporary);
2013 instructions->push_tail(var);
2014 instructions->push_tail(new(ctx) ir_assignment(new(ctx)
2015 ir_dereference_variable(var), matrix, NULL));
2016 var->constant_value = matrix->constant_expression_value();
2017
2018 /* Replace the matrix with dereferences of its columns. */
2019 for (int i = 0; i < matrix->type->matrix_columns; i++) {
2020 matrix->insert_before(new (ctx) ir_dereference_array(var,
2021 new(ctx) ir_constant(i)));
2022 }
2023 matrix->remove();
2024 }
2025 }
2026
2027 bool all_parameters_are_constant = true;
2028
2029 /* Type cast each parameter and, if possible, fold constants.*/
2030 foreach_in_list_safe(ir_rvalue, ir, &actual_parameters) {
2031 const glsl_type *desired_type =
2032 glsl_type::get_instance(constructor_type->base_type,
2033 ir->type->vector_elements,
2034 ir->type->matrix_columns);
2035 ir_rvalue *result = convert_component(ir, desired_type);
2036
2037 /* Attempt to convert the parameter to a constant valued expression.
2038 * After doing so, track whether or not all the parameters to the
2039 * constructor are trivially constant valued expressions.
2040 */
2041 ir_rvalue *const constant = result->constant_expression_value();
2042
2043 if (constant != NULL)
2044 result = constant;
2045 else
2046 all_parameters_are_constant = false;
2047
2048 if (result != ir) {
2049 ir->replace_with(result);
2050 }
2051 }
2052
2053 /* If all of the parameters are trivially constant, create a
2054 * constant representing the complete collection of parameters.
2055 */
2056 if (all_parameters_are_constant) {
2057 return new(ctx) ir_constant(constructor_type, &actual_parameters);
2058 } else if (constructor_type->is_scalar()) {
2059 return dereference_component((ir_rvalue *) actual_parameters.get_head_raw(),
2060 0);
2061 } else if (constructor_type->is_vector()) {
2062 return emit_inline_vector_constructor(constructor_type,
2063 instructions,
2064 &actual_parameters,
2065 ctx);
2066 } else {
2067 assert(constructor_type->is_matrix());
2068 return emit_inline_matrix_constructor(constructor_type,
2069 instructions,
2070 &actual_parameters,
2071 ctx);
2072 }
2073 } else if (subexpressions[0]->oper == ast_field_selection) {
2074 return handle_method(instructions, state);
2075 } else {
2076 const ast_expression *id = subexpressions[0];
2077 const char *func_name;
2078 YYLTYPE loc = get_location();
2079 exec_list actual_parameters;
2080 ir_variable *sub_var = NULL;
2081 ir_rvalue *array_idx = NULL;
2082
2083 process_parameters(instructions, &actual_parameters, &this->expressions,
2084 state);
2085
2086 if (id->oper == ast_array_index) {
2087 array_idx = generate_array_index(ctx, instructions, state, loc,
2088 id->subexpressions[0],
2089 id->subexpressions[1], &func_name,
2090 &actual_parameters);
2091 } else {
2092 func_name = id->primary_expression.identifier;
2093 }
2094
2095 /* an error was emitted earlier */
2096 if (!func_name)
2097 return ir_rvalue::error_value(ctx);
2098
2099 ir_function_signature *sig =
2100 match_function_by_name(func_name, &actual_parameters, state);
2101
2102 ir_rvalue *value = NULL;
2103 if (sig == NULL) {
2104 sig = match_subroutine_by_name(func_name, &actual_parameters, state, &sub_var);
2105 }
2106
2107 if (sig == NULL) {
2108 no_matching_function_error(func_name, &loc, &actual_parameters, state);
2109 value = ir_rvalue::error_value(ctx);
2110 } else if (!verify_parameter_modes(state, sig, actual_parameters, this->expressions)) {
2111 /* an error has already been emitted */
2112 value = ir_rvalue::error_value(ctx);
2113 } else {
2114 value = generate_call(instructions, sig, &actual_parameters, sub_var, array_idx, state);
2115 if (!value) {
2116 ir_variable *const tmp = new(ctx) ir_variable(glsl_type::void_type,
2117 "void_var",
2118 ir_var_temporary);
2119 instructions->push_tail(tmp);
2120 value = new(ctx) ir_dereference_variable(tmp);
2121 }
2122 }
2123
2124 return value;
2125 }
2126
2127 unreachable("not reached");
2128 }
2129
2130 bool
2131 ast_function_expression::has_sequence_subexpression() const
2132 {
2133 foreach_list_typed(const ast_node, ast, link, &this->expressions) {
2134 if (ast->has_sequence_subexpression())
2135 return true;
2136 }
2137
2138 return false;
2139 }
2140
2141 ir_rvalue *
2142 ast_aggregate_initializer::hir(exec_list *instructions,
2143 struct _mesa_glsl_parse_state *state)
2144 {
2145 void *ctx = state;
2146 YYLTYPE loc = this->get_location();
2147
2148 if (!this->constructor_type) {
2149 _mesa_glsl_error(&loc, state, "type of C-style initializer unknown");
2150 return ir_rvalue::error_value(ctx);
2151 }
2152 const glsl_type *const constructor_type = this->constructor_type;
2153
2154 if (!state->has_420pack()) {
2155 _mesa_glsl_error(&loc, state, "C-style initialization requires the "
2156 "GL_ARB_shading_language_420pack extension");
2157 return ir_rvalue::error_value(ctx);
2158 }
2159
2160 if (constructor_type->is_array()) {
2161 return process_array_constructor(instructions, constructor_type, &loc,
2162 &this->expressions, state);
2163 }
2164
2165 if (constructor_type->is_record()) {
2166 return process_record_constructor(instructions, constructor_type, &loc,
2167 &this->expressions, state);
2168 }
2169
2170 return process_vec_mat_constructor(instructions, constructor_type, &loc,
2171 &this->expressions, state);
2172 }