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