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