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