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