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