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