26d4c62ce36ce99bf2609a2796d59456c1cd60b3
[mesa.git] / src / 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 "glsl_types.h"
27 #include "ir.h"
28 #include "main/core.h" /* for MIN2 */
29 #include "main/shaderobj.h"
30
31 static ir_rvalue *
32 convert_component(ir_rvalue *src, const glsl_type *desired_type);
33
34 bool
35 apply_implicit_conversion(const glsl_type *to, ir_rvalue * &from,
36 struct _mesa_glsl_parse_state *state);
37
38 static unsigned
39 process_parameters(exec_list *instructions, exec_list *actual_parameters,
40 exec_list *parameters,
41 struct _mesa_glsl_parse_state *state)
42 {
43 unsigned count = 0;
44
45 foreach_list_typed(ast_node, ast, link, parameters) {
46 ir_rvalue *result = ast->hir(instructions, state);
47
48 ir_constant *const constant = result->constant_expression_value();
49 if (constant != NULL)
50 result = constant;
51
52 actual_parameters->push_tail(result);
53 count++;
54 }
55
56 return count;
57 }
58
59
60 /**
61 * Generate a source prototype for a function signature
62 *
63 * \param return_type Return type of the function. May be \c NULL.
64 * \param name Name of the function.
65 * \param parameters List of \c ir_instruction nodes representing the
66 * parameter list for the function. This may be either a
67 * formal (\c ir_variable) or actual (\c ir_rvalue)
68 * parameter list. Only the type is used.
69 *
70 * \return
71 * A ralloced string representing the prototype of the function.
72 */
73 char *
74 prototype_string(const glsl_type *return_type, const char *name,
75 exec_list *parameters)
76 {
77 char *str = NULL;
78
79 if (return_type != NULL)
80 str = ralloc_asprintf(NULL, "%s ", return_type->name);
81
82 ralloc_asprintf_append(&str, "%s(", name);
83
84 const char *comma = "";
85 foreach_in_list(const ir_variable, param, parameters) {
86 ralloc_asprintf_append(&str, "%s%s", comma, param->type->name);
87 comma = ", ";
88 }
89
90 ralloc_strcat(&str, ")");
91 return str;
92 }
93
94 static bool
95 verify_image_parameter(YYLTYPE *loc, _mesa_glsl_parse_state *state,
96 const ir_variable *formal, const ir_variable *actual)
97 {
98 /**
99 * From the ARB_shader_image_load_store specification:
100 *
101 * "The values of image variables qualified with coherent,
102 * volatile, restrict, readonly, or writeonly may not be passed
103 * to functions whose formal parameters lack such
104 * qualifiers. [...] It is legal to have additional qualifiers
105 * on a formal parameter, but not to have fewer."
106 */
107 if (actual->data.image_coherent && !formal->data.image_coherent) {
108 _mesa_glsl_error(loc, state,
109 "function call parameter `%s' drops "
110 "`coherent' qualifier", formal->name);
111 return false;
112 }
113
114 if (actual->data.image_volatile && !formal->data.image_volatile) {
115 _mesa_glsl_error(loc, state,
116 "function call parameter `%s' drops "
117 "`volatile' qualifier", formal->name);
118 return false;
119 }
120
121 if (actual->data.image_restrict && !formal->data.image_restrict) {
122 _mesa_glsl_error(loc, state,
123 "function call parameter `%s' drops "
124 "`restrict' qualifier", formal->name);
125 return false;
126 }
127
128 if (actual->data.image_read_only && !formal->data.image_read_only) {
129 _mesa_glsl_error(loc, state,
130 "function call parameter `%s' drops "
131 "`readonly' qualifier", formal->name);
132 return false;
133 }
134
135 if (actual->data.image_write_only && !formal->data.image_write_only) {
136 _mesa_glsl_error(loc, state,
137 "function call parameter `%s' drops "
138 "`writeonly' qualifier", formal->name);
139 return false;
140 }
141
142 return true;
143 }
144
145 static bool
146 verify_first_atomic_ssbo_parameter(YYLTYPE *loc, _mesa_glsl_parse_state *state,
147 ir_variable *var)
148 {
149 if (!var || !var->is_in_shader_storage_block()) {
150 _mesa_glsl_error(loc, state, "First argument to atomic function "
151 "must be a buffer variable");
152 return false;
153 }
154 return true;
155 }
156
157 static bool
158 is_atomic_ssbo_function(const char *func_name)
159 {
160 return !strcmp(func_name, "atomicAdd") ||
161 !strcmp(func_name, "atomicMin") ||
162 !strcmp(func_name, "atomicMax") ||
163 !strcmp(func_name, "atomicAnd") ||
164 !strcmp(func_name, "atomicOr") ||
165 !strcmp(func_name, "atomicXor") ||
166 !strcmp(func_name, "atomicExchange") ||
167 !strcmp(func_name, "atomicCompSwap");
168 }
169
170 /**
171 * Verify that 'out' and 'inout' actual parameters are lvalues. Also, verify
172 * that 'const_in' formal parameters (an extension in our IR) correspond to
173 * ir_constant actual parameters.
174 */
175 static bool
176 verify_parameter_modes(_mesa_glsl_parse_state *state,
177 ir_function_signature *sig,
178 exec_list &actual_ir_parameters,
179 exec_list &actual_ast_parameters)
180 {
181 exec_node *actual_ir_node = actual_ir_parameters.head;
182 exec_node *actual_ast_node = actual_ast_parameters.head;
183
184 foreach_in_list(const ir_variable, formal, &sig->parameters) {
185 /* The lists must be the same length. */
186 assert(!actual_ir_node->is_tail_sentinel());
187 assert(!actual_ast_node->is_tail_sentinel());
188
189 const ir_rvalue *const actual = (ir_rvalue *) actual_ir_node;
190 const ast_expression *const actual_ast =
191 exec_node_data(ast_expression, actual_ast_node, link);
192
193 /* FIXME: 'loc' is incorrect (as of 2011-01-21). It is always
194 * FIXME: 0:0(0).
195 */
196 YYLTYPE loc = actual_ast->get_location();
197
198 /* Verify that 'const_in' parameters are ir_constants. */
199 if (formal->data.mode == ir_var_const_in &&
200 actual->ir_type != ir_type_constant) {
201 _mesa_glsl_error(&loc, state,
202 "parameter `in %s' must be a constant expression",
203 formal->name);
204 return false;
205 }
206
207 /* Verify that shader_in parameters are shader inputs */
208 if (formal->data.must_be_shader_input) {
209 ir_variable *var = actual->variable_referenced();
210 if (var && var->data.mode != ir_var_shader_in) {
211 _mesa_glsl_error(&loc, state,
212 "parameter `%s` must be a shader input",
213 formal->name);
214 return false;
215 }
216
217 if (actual->ir_type == ir_type_swizzle) {
218 _mesa_glsl_error(&loc, state,
219 "parameter `%s` must not be swizzled",
220 formal->name);
221 return false;
222 }
223 }
224
225 /* Verify that 'out' and 'inout' actual parameters are lvalues. */
226 if (formal->data.mode == ir_var_function_out
227 || formal->data.mode == ir_var_function_inout) {
228 const char *mode = NULL;
229 switch (formal->data.mode) {
230 case ir_var_function_out: mode = "out"; break;
231 case ir_var_function_inout: mode = "inout"; break;
232 default: assert(false); break;
233 }
234
235 /* This AST-based check catches errors like f(i++). The IR-based
236 * is_lvalue() is insufficient because the actual parameter at the
237 * IR-level is just a temporary value, which is an l-value.
238 */
239 if (actual_ast->non_lvalue_description != NULL) {
240 _mesa_glsl_error(&loc, state,
241 "function parameter '%s %s' references a %s",
242 mode, formal->name,
243 actual_ast->non_lvalue_description);
244 return false;
245 }
246
247 ir_variable *var = actual->variable_referenced();
248 if (var)
249 var->data.assigned = true;
250
251 if (var && var->data.read_only) {
252 _mesa_glsl_error(&loc, state,
253 "function parameter '%s %s' references the "
254 "read-only variable '%s'",
255 mode, formal->name,
256 actual->variable_referenced()->name);
257 return false;
258 } else if (!actual->is_lvalue()) {
259 /* Even though ir_binop_vector_extract is not an l-value, let it
260 * slop through. generate_call will handle it correctly.
261 */
262 ir_expression *const expr = ((ir_rvalue *) actual)->as_expression();
263 if (expr == NULL
264 || expr->operation != ir_binop_vector_extract
265 || !expr->operands[0]->is_lvalue()) {
266 _mesa_glsl_error(&loc, state,
267 "function parameter '%s %s' is not an lvalue",
268 mode, formal->name);
269 return false;
270 }
271 }
272 }
273
274 if (formal->type->is_image() &&
275 actual->variable_referenced()) {
276 if (!verify_image_parameter(&loc, state, formal,
277 actual->variable_referenced()))
278 return false;
279 }
280
281 actual_ir_node = actual_ir_node->next;
282 actual_ast_node = actual_ast_node->next;
283 }
284
285 /* The first parameter of atomic functions must be a buffer variable */
286 const char *func_name = sig->function_name();
287 bool is_atomic_ssbo = is_atomic_ssbo_function(func_name);
288 if (is_atomic_ssbo) {
289 const ir_rvalue *const actual = (ir_rvalue *) actual_ir_parameters.head;
290
291 const ast_expression *const actual_ast =
292 exec_node_data(ast_expression, actual_ast_parameters.head, link);
293 YYLTYPE loc = actual_ast->get_location();
294
295 if (!verify_first_atomic_ssbo_parameter(&loc, state,
296 actual->variable_referenced())) {
297 return false;
298 }
299 }
300
301 return true;
302 }
303
304 static void
305 fix_parameter(void *mem_ctx, ir_rvalue *actual, const glsl_type *formal_type,
306 exec_list *before_instructions, exec_list *after_instructions,
307 bool parameter_is_inout)
308 {
309 ir_expression *const expr = actual->as_expression();
310
311 /* If the types match exactly and the parameter is not a vector-extract,
312 * nothing needs to be done to fix the parameter.
313 */
314 if (formal_type == actual->type
315 && (expr == NULL || expr->operation != ir_binop_vector_extract))
316 return;
317
318 /* To convert an out parameter, we need to create a temporary variable to
319 * hold the value before conversion, and then perform the conversion after
320 * the function call returns.
321 *
322 * This has the effect of transforming code like this:
323 *
324 * void f(out int x);
325 * float value;
326 * f(value);
327 *
328 * Into IR that's equivalent to this:
329 *
330 * void f(out int x);
331 * float value;
332 * int out_parameter_conversion;
333 * f(out_parameter_conversion);
334 * value = float(out_parameter_conversion);
335 *
336 * If the parameter is an ir_expression of ir_binop_vector_extract,
337 * additional conversion is needed in the post-call re-write.
338 */
339 ir_variable *tmp =
340 new(mem_ctx) ir_variable(formal_type, "inout_tmp", ir_var_temporary);
341
342 before_instructions->push_tail(tmp);
343
344 /* If the parameter is an inout parameter, copy the value of the actual
345 * parameter to the new temporary. Note that no type conversion is allowed
346 * here because inout parameters must match types exactly.
347 */
348 if (parameter_is_inout) {
349 /* Inout parameters should never require conversion, since that would
350 * require an implicit conversion to exist both to and from the formal
351 * parameter type, and there are no bidirectional implicit conversions.
352 */
353 assert (actual->type == formal_type);
354
355 ir_dereference_variable *const deref_tmp_1 =
356 new(mem_ctx) ir_dereference_variable(tmp);
357 ir_assignment *const assignment =
358 new(mem_ctx) ir_assignment(deref_tmp_1, actual);
359 before_instructions->push_tail(assignment);
360 }
361
362 /* Replace the parameter in the call with a dereference of the new
363 * temporary.
364 */
365 ir_dereference_variable *const deref_tmp_2 =
366 new(mem_ctx) ir_dereference_variable(tmp);
367 actual->replace_with(deref_tmp_2);
368
369
370 /* Copy the temporary variable to the actual parameter with optional
371 * type conversion applied.
372 */
373 ir_rvalue *rhs = new(mem_ctx) ir_dereference_variable(tmp);
374 if (actual->type != formal_type)
375 rhs = convert_component(rhs, actual->type);
376
377 ir_rvalue *lhs = actual;
378 if (expr != NULL && expr->operation == ir_binop_vector_extract) {
379 rhs = new(mem_ctx) ir_expression(ir_triop_vector_insert,
380 expr->operands[0]->type,
381 expr->operands[0]->clone(mem_ctx, NULL),
382 rhs,
383 expr->operands[1]->clone(mem_ctx, NULL));
384 lhs = expr->operands[0]->clone(mem_ctx, NULL);
385 }
386
387 ir_assignment *const assignment_2 = new(mem_ctx) ir_assignment(lhs, rhs);
388 after_instructions->push_tail(assignment_2);
389 }
390
391 /**
392 * Generate a function call.
393 *
394 * For non-void functions, this returns a dereference of the temporary variable
395 * which stores the return value for the call. For void functions, this returns
396 * NULL.
397 */
398 static ir_rvalue *
399 generate_call(exec_list *instructions, ir_function_signature *sig,
400 exec_list *actual_parameters,
401 ir_variable *sub_var,
402 ir_rvalue *array_idx,
403 struct _mesa_glsl_parse_state *state)
404 {
405 void *ctx = state;
406 exec_list post_call_conversions;
407
408 /* Perform implicit conversion of arguments. For out parameters, we need
409 * to place them in a temporary variable and do the conversion after the
410 * call takes place. Since we haven't emitted the call yet, we'll place
411 * the post-call conversions in a temporary exec_list, and emit them later.
412 */
413 foreach_two_lists(formal_node, &sig->parameters,
414 actual_node, actual_parameters) {
415 ir_rvalue *actual = (ir_rvalue *) actual_node;
416 ir_variable *formal = (ir_variable *) formal_node;
417
418 if (formal->type->is_numeric() || formal->type->is_boolean()) {
419 switch (formal->data.mode) {
420 case ir_var_const_in:
421 case ir_var_function_in: {
422 ir_rvalue *converted
423 = convert_component(actual, formal->type);
424 actual->replace_with(converted);
425 break;
426 }
427 case ir_var_function_out:
428 case ir_var_function_inout:
429 fix_parameter(ctx, actual, formal->type,
430 instructions, &post_call_conversions,
431 formal->data.mode == ir_var_function_inout);
432 break;
433 default:
434 assert (!"Illegal formal parameter mode");
435 break;
436 }
437 }
438 }
439
440 /* If the function call is a constant expression, don't generate any
441 * instructions; just generate an ir_constant.
442 *
443 * Function calls were first allowed to be constant expressions in GLSL
444 * 1.20 and GLSL ES 3.00.
445 */
446 if (state->is_version(120, 300)) {
447 ir_constant *value = sig->constant_expression_value(actual_parameters, NULL);
448 if (value != NULL) {
449 return value;
450 }
451 }
452
453 ir_dereference_variable *deref = NULL;
454 if (!sig->return_type->is_void()) {
455 /* Create a new temporary to hold the return value. */
456 char *const name = ir_variable::temporaries_allocate_names
457 ? ralloc_asprintf(ctx, "%s_retval", sig->function_name())
458 : NULL;
459
460 ir_variable *var;
461
462 var = new(ctx) ir_variable(sig->return_type, name, ir_var_temporary);
463 instructions->push_tail(var);
464
465 ralloc_free(name);
466
467 deref = new(ctx) ir_dereference_variable(var);
468 }
469
470 ir_call *call = new(ctx) ir_call(sig, deref, actual_parameters, sub_var, array_idx);
471 instructions->push_tail(call);
472
473 /* Also emit any necessary out-parameter conversions. */
474 instructions->append_list(&post_call_conversions);
475
476 return deref ? deref->clone(ctx, NULL) : NULL;
477 }
478
479 /**
480 * Given a function name and parameter list, find the matching signature.
481 */
482 static ir_function_signature *
483 match_function_by_name(const char *name,
484 exec_list *actual_parameters,
485 struct _mesa_glsl_parse_state *state)
486 {
487 void *ctx = state;
488 ir_function *f = state->symbols->get_function(name);
489 ir_function_signature *local_sig = NULL;
490 ir_function_signature *sig = NULL;
491
492 /* Is the function hidden by a record type constructor? */
493 if (state->symbols->get_type(name))
494 goto done; /* no match */
495
496 /* Is the function hidden by a variable (impossible in 1.10)? */
497 if (!state->symbols->separate_function_namespace
498 && state->symbols->get_variable(name))
499 goto done; /* no match */
500
501 if (f != NULL) {
502 /* In desktop GL, the presence of a user-defined signature hides any
503 * built-in signatures, so we must ignore them. In contrast, in ES2
504 * user-defined signatures add new overloads, so we must consider them.
505 */
506 bool allow_builtins = state->es_shader || !f->has_user_signature();
507
508 /* Look for a match in the local shader. If exact, we're done. */
509 bool is_exact = false;
510 sig = local_sig = f->matching_signature(state, actual_parameters,
511 allow_builtins, &is_exact);
512 if (is_exact)
513 goto done;
514
515 if (!allow_builtins)
516 goto done;
517 }
518
519 /* Local shader has no exact candidates; check the built-ins. */
520 _mesa_glsl_initialize_builtin_functions();
521 sig = _mesa_glsl_find_builtin_function(state, name, actual_parameters);
522
523 done:
524 if (sig != NULL) {
525 /* If the match is from a linked built-in shader, import the prototype. */
526 if (sig != local_sig) {
527 if (f == NULL) {
528 f = new(ctx) ir_function(name);
529 state->symbols->add_global_function(f);
530 emit_function(state, f);
531 }
532 f->add_signature(sig->clone_prototype(f, NULL));
533 }
534 }
535 return sig;
536 }
537
538 static ir_function_signature *
539 match_subroutine_by_name(const char *name,
540 exec_list *actual_parameters,
541 struct _mesa_glsl_parse_state *state,
542 ir_variable **var_r)
543 {
544 void *ctx = state;
545 ir_function_signature *sig = NULL;
546 ir_function *f, *found = NULL;
547 const char *new_name;
548 ir_variable *var;
549 bool is_exact = false;
550
551 new_name = ralloc_asprintf(ctx, "%s_%s", _mesa_shader_stage_to_subroutine_prefix(state->stage), name);
552 var = state->symbols->get_variable(new_name);
553 if (!var)
554 return NULL;
555
556 for (int i = 0; i < state->num_subroutine_types; i++) {
557 f = state->subroutine_types[i];
558 if (strcmp(f->name, var->type->without_array()->name))
559 continue;
560 found = f;
561 break;
562 }
563
564 if (!found)
565 return NULL;
566 *var_r = var;
567 sig = found->matching_signature(state, actual_parameters,
568 false, &is_exact);
569 return sig;
570 }
571
572 static void
573 print_function_prototypes(_mesa_glsl_parse_state *state, YYLTYPE *loc,
574 ir_function *f)
575 {
576 if (f == NULL)
577 return;
578
579 foreach_in_list(ir_function_signature, sig, &f->signatures) {
580 if (sig->is_builtin() && !sig->is_builtin_available(state))
581 continue;
582
583 char *str = prototype_string(sig->return_type, f->name, &sig->parameters);
584 _mesa_glsl_error(loc, state, " %s", str);
585 ralloc_free(str);
586 }
587 }
588
589 /**
590 * Raise a "no matching function" error, listing all possible overloads the
591 * compiler considered so developers can figure out what went wrong.
592 */
593 static void
594 no_matching_function_error(const char *name,
595 YYLTYPE *loc,
596 exec_list *actual_parameters,
597 _mesa_glsl_parse_state *state)
598 {
599 gl_shader *sh = _mesa_glsl_get_builtin_function_shader();
600
601 if (state->symbols->get_function(name) == NULL
602 && (!state->uses_builtin_functions
603 || sh->symbols->get_function(name) == NULL)) {
604 _mesa_glsl_error(loc, state, "no function with name '%s'", name);
605 } else {
606 char *str = prototype_string(NULL, name, actual_parameters);
607 _mesa_glsl_error(loc, state,
608 "no matching function for call to `%s'; candidates are:",
609 str);
610 ralloc_free(str);
611
612 print_function_prototypes(state, loc, state->symbols->get_function(name));
613
614 if (state->uses_builtin_functions) {
615 print_function_prototypes(state, loc, sh->symbols->get_function(name));
616 }
617 }
618 }
619
620 /**
621 * Perform automatic type conversion of constructor parameters
622 *
623 * This implements the rules in the "Conversion and Scalar Constructors"
624 * section (GLSL 1.10 section 5.4.1), not the "Implicit Conversions" rules.
625 */
626 static ir_rvalue *
627 convert_component(ir_rvalue *src, const glsl_type *desired_type)
628 {
629 void *ctx = ralloc_parent(src);
630 const unsigned a = desired_type->base_type;
631 const unsigned b = src->type->base_type;
632 ir_expression *result = NULL;
633
634 if (src->type->is_error())
635 return src;
636
637 assert(a <= GLSL_TYPE_BOOL);
638 assert(b <= GLSL_TYPE_BOOL);
639
640 if (a == b)
641 return src;
642
643 switch (a) {
644 case GLSL_TYPE_UINT:
645 switch (b) {
646 case GLSL_TYPE_INT:
647 result = new(ctx) ir_expression(ir_unop_i2u, src);
648 break;
649 case GLSL_TYPE_FLOAT:
650 result = new(ctx) ir_expression(ir_unop_f2u, src);
651 break;
652 case GLSL_TYPE_BOOL:
653 result = new(ctx) ir_expression(ir_unop_i2u,
654 new(ctx) ir_expression(ir_unop_b2i, src));
655 break;
656 case GLSL_TYPE_DOUBLE:
657 result = new(ctx) ir_expression(ir_unop_d2u, src);
658 break;
659 }
660 break;
661 case GLSL_TYPE_INT:
662 switch (b) {
663 case GLSL_TYPE_UINT:
664 result = new(ctx) ir_expression(ir_unop_u2i, src);
665 break;
666 case GLSL_TYPE_FLOAT:
667 result = new(ctx) ir_expression(ir_unop_f2i, src);
668 break;
669 case GLSL_TYPE_BOOL:
670 result = new(ctx) ir_expression(ir_unop_b2i, src);
671 break;
672 case GLSL_TYPE_DOUBLE:
673 result = new(ctx) ir_expression(ir_unop_d2i, src);
674 break;
675 }
676 break;
677 case GLSL_TYPE_FLOAT:
678 switch (b) {
679 case GLSL_TYPE_UINT:
680 result = new(ctx) ir_expression(ir_unop_u2f, desired_type, src, NULL);
681 break;
682 case GLSL_TYPE_INT:
683 result = new(ctx) ir_expression(ir_unop_i2f, desired_type, src, NULL);
684 break;
685 case GLSL_TYPE_BOOL:
686 result = new(ctx) ir_expression(ir_unop_b2f, desired_type, src, NULL);
687 break;
688 case GLSL_TYPE_DOUBLE:
689 result = new(ctx) ir_expression(ir_unop_d2f, desired_type, src, NULL);
690 break;
691 }
692 break;
693 case GLSL_TYPE_BOOL:
694 switch (b) {
695 case GLSL_TYPE_UINT:
696 result = new(ctx) ir_expression(ir_unop_i2b,
697 new(ctx) ir_expression(ir_unop_u2i, src));
698 break;
699 case GLSL_TYPE_INT:
700 result = new(ctx) ir_expression(ir_unop_i2b, desired_type, src, NULL);
701 break;
702 case GLSL_TYPE_FLOAT:
703 result = new(ctx) ir_expression(ir_unop_f2b, desired_type, src, NULL);
704 break;
705 case GLSL_TYPE_DOUBLE:
706 result = new(ctx) ir_expression(ir_unop_d2b, desired_type, src, NULL);
707 break;
708 }
709 break;
710 case GLSL_TYPE_DOUBLE:
711 switch (b) {
712 case GLSL_TYPE_INT:
713 result = new(ctx) ir_expression(ir_unop_i2d, src);
714 break;
715 case GLSL_TYPE_UINT:
716 result = new(ctx) ir_expression(ir_unop_u2d, src);
717 break;
718 case GLSL_TYPE_BOOL:
719 result = new(ctx) ir_expression(ir_unop_f2d,
720 new(ctx) ir_expression(ir_unop_b2f, src));
721 break;
722 case GLSL_TYPE_FLOAT:
723 result = new(ctx) ir_expression(ir_unop_f2d, desired_type, src, NULL);
724 break;
725 }
726 }
727
728 assert(result != NULL);
729 assert(result->type == desired_type);
730
731 /* Try constant folding; it may fold in the conversion we just added. */
732 ir_constant *const constant = result->constant_expression_value();
733 return (constant != NULL) ? (ir_rvalue *) constant : (ir_rvalue *) result;
734 }
735
736 /**
737 * Dereference a specific component from a scalar, vector, or matrix
738 */
739 static ir_rvalue *
740 dereference_component(ir_rvalue *src, unsigned component)
741 {
742 void *ctx = ralloc_parent(src);
743 assert(component < src->type->components());
744
745 /* If the source is a constant, just create a new constant instead of a
746 * dereference of the existing constant.
747 */
748 ir_constant *constant = src->as_constant();
749 if (constant)
750 return new(ctx) ir_constant(constant, component);
751
752 if (src->type->is_scalar()) {
753 return src;
754 } else if (src->type->is_vector()) {
755 return new(ctx) ir_swizzle(src, component, 0, 0, 0, 1);
756 } else {
757 assert(src->type->is_matrix());
758
759 /* Dereference a row of the matrix, then call this function again to get
760 * a specific element from that row.
761 */
762 const int c = component / src->type->column_type()->vector_elements;
763 const int r = component % src->type->column_type()->vector_elements;
764 ir_constant *const col_index = new(ctx) ir_constant(c);
765 ir_dereference *const col = new(ctx) ir_dereference_array(src, col_index);
766
767 col->type = src->type->column_type();
768
769 return dereference_component(col, r);
770 }
771
772 assert(!"Should not get here.");
773 return NULL;
774 }
775
776
777 static ir_rvalue *
778 process_vec_mat_constructor(exec_list *instructions,
779 const glsl_type *constructor_type,
780 YYLTYPE *loc, exec_list *parameters,
781 struct _mesa_glsl_parse_state *state)
782 {
783 void *ctx = state;
784
785 /* The ARB_shading_language_420pack spec says:
786 *
787 * "If an initializer is a list of initializers enclosed in curly braces,
788 * the variable being declared must be a vector, a matrix, an array, or a
789 * structure.
790 *
791 * int i = { 1 }; // illegal, i is not an aggregate"
792 */
793 if (constructor_type->vector_elements <= 1) {
794 _mesa_glsl_error(loc, state, "aggregates can only initialize vectors, "
795 "matrices, arrays, and structs");
796 return ir_rvalue::error_value(ctx);
797 }
798
799 exec_list actual_parameters;
800 const unsigned parameter_count =
801 process_parameters(instructions, &actual_parameters, parameters, state);
802
803 if (parameter_count == 0
804 || (constructor_type->is_vector() &&
805 constructor_type->vector_elements != parameter_count)
806 || (constructor_type->is_matrix() &&
807 constructor_type->matrix_columns != parameter_count)) {
808 _mesa_glsl_error(loc, state, "%s constructor must have %u parameters",
809 constructor_type->is_vector() ? "vector" : "matrix",
810 constructor_type->vector_elements);
811 return ir_rvalue::error_value(ctx);
812 }
813
814 bool all_parameters_are_constant = true;
815
816 /* Type cast each parameter and, if possible, fold constants. */
817 foreach_in_list_safe(ir_rvalue, ir, &actual_parameters) {
818 ir_rvalue *result = ir;
819
820 /* Apply implicit conversions (not the scalar constructor rules!). See
821 * the spec quote above. */
822 if (constructor_type->base_type != result->type->base_type) {
823 const glsl_type *desired_type =
824 glsl_type::get_instance(constructor_type->base_type,
825 ir->type->vector_elements,
826 ir->type->matrix_columns);
827 if (result->type->can_implicitly_convert_to(desired_type, state)) {
828 /* Even though convert_component() implements the constructor
829 * conversion rules (not the implicit conversion rules), its safe
830 * to use it here because we already checked that the implicit
831 * conversion is legal.
832 */
833 result = convert_component(ir, desired_type);
834 }
835 }
836
837 if (constructor_type->is_matrix()) {
838 if (result->type != constructor_type->column_type()) {
839 _mesa_glsl_error(loc, state, "type error in matrix constructor: "
840 "expected: %s, found %s",
841 constructor_type->column_type()->name,
842 result->type->name);
843 return ir_rvalue::error_value(ctx);
844 }
845 } else if (result->type != constructor_type->get_scalar_type()) {
846 _mesa_glsl_error(loc, state, "type error in vector constructor: "
847 "expected: %s, found %s",
848 constructor_type->get_scalar_type()->name,
849 result->type->name);
850 return ir_rvalue::error_value(ctx);
851 }
852
853 /* Attempt to convert the parameter to a constant valued expression.
854 * After doing so, track whether or not all the parameters to the
855 * constructor are trivially constant valued expressions.
856 */
857 ir_rvalue *const constant = result->constant_expression_value();
858
859 if (constant != NULL)
860 result = constant;
861 else
862 all_parameters_are_constant = false;
863
864 ir->replace_with(result);
865 }
866
867 if (all_parameters_are_constant)
868 return new(ctx) ir_constant(constructor_type, &actual_parameters);
869
870 ir_variable *var = new(ctx) ir_variable(constructor_type, "vec_mat_ctor",
871 ir_var_temporary);
872 instructions->push_tail(var);
873
874 int i = 0;
875
876 foreach_in_list(ir_rvalue, rhs, &actual_parameters) {
877 ir_instruction *assignment = NULL;
878
879 if (var->type->is_matrix()) {
880 ir_rvalue *lhs = new(ctx) ir_dereference_array(var,
881 new(ctx) ir_constant(i));
882 assignment = new(ctx) ir_assignment(lhs, rhs, NULL);
883 } else {
884 /* use writemask rather than index for vector */
885 assert(var->type->is_vector());
886 assert(i < 4);
887 ir_dereference *lhs = new(ctx) ir_dereference_variable(var);
888 assignment = new(ctx) ir_assignment(lhs, rhs, NULL, (unsigned)(1 << i));
889 }
890
891 instructions->push_tail(assignment);
892
893 i++;
894 }
895
896 return new(ctx) ir_dereference_variable(var);
897 }
898
899
900 static ir_rvalue *
901 process_array_constructor(exec_list *instructions,
902 const glsl_type *constructor_type,
903 YYLTYPE *loc, exec_list *parameters,
904 struct _mesa_glsl_parse_state *state)
905 {
906 void *ctx = state;
907 /* Array constructors come in two forms: sized and unsized. Sized array
908 * constructors look like 'vec4[2](a, b)', where 'a' and 'b' are vec4
909 * variables. In this case the number of parameters must exactly match the
910 * specified size of the array.
911 *
912 * Unsized array constructors look like 'vec4[](a, b)', where 'a' and 'b'
913 * are vec4 variables. In this case the size of the array being constructed
914 * is determined by the number of parameters.
915 *
916 * From page 52 (page 58 of the PDF) of the GLSL 1.50 spec:
917 *
918 * "There must be exactly the same number of arguments as the size of
919 * the array being constructed. If no size is present in the
920 * constructor, then the array is explicitly sized to the number of
921 * arguments provided. The arguments are assigned in order, starting at
922 * element 0, to the elements of the constructed array. Each argument
923 * must be the same type as the element type of the array, or be a type
924 * that can be converted to the element type of the array according to
925 * Section 4.1.10 "Implicit Conversions.""
926 */
927 exec_list actual_parameters;
928 const unsigned parameter_count =
929 process_parameters(instructions, &actual_parameters, parameters, state);
930 bool is_unsized_array = constructor_type->is_unsized_array();
931
932 if ((parameter_count == 0) ||
933 (!is_unsized_array && (constructor_type->length != parameter_count))) {
934 const unsigned min_param = is_unsized_array
935 ? 1 : constructor_type->length;
936
937 _mesa_glsl_error(loc, state, "array constructor must have %s %u "
938 "parameter%s",
939 is_unsized_array ? "at least" : "exactly",
940 min_param, (min_param <= 1) ? "" : "s");
941 return ir_rvalue::error_value(ctx);
942 }
943
944 if (is_unsized_array) {
945 constructor_type =
946 glsl_type::get_array_instance(constructor_type->fields.array,
947 parameter_count);
948 assert(constructor_type != NULL);
949 assert(constructor_type->length == parameter_count);
950 }
951
952 bool all_parameters_are_constant = true;
953
954 /* Type cast each parameter and, if possible, fold constants. */
955 foreach_in_list_safe(ir_rvalue, ir, &actual_parameters) {
956 ir_rvalue *result = ir;
957
958 const glsl_base_type element_base_type =
959 constructor_type->fields.array->base_type;
960
961 /* Apply implicit conversions (not the scalar constructor rules!). See
962 * the spec quote above. */
963 if (element_base_type != result->type->base_type) {
964 const glsl_type *desired_type =
965 glsl_type::get_instance(element_base_type,
966 ir->type->vector_elements,
967 ir->type->matrix_columns);
968
969 if (result->type->can_implicitly_convert_to(desired_type, state)) {
970 /* Even though convert_component() implements the constructor
971 * conversion rules (not the implicit conversion rules), its safe
972 * to use it here because we already checked that the implicit
973 * conversion is legal.
974 */
975 result = convert_component(ir, desired_type);
976 }
977 }
978
979 if (result->type != constructor_type->fields.array) {
980 _mesa_glsl_error(loc, state, "type error in array constructor: "
981 "expected: %s, found %s",
982 constructor_type->fields.array->name,
983 result->type->name);
984 return ir_rvalue::error_value(ctx);
985 }
986
987 /* Attempt to convert the parameter to a constant valued expression.
988 * After doing so, track whether or not all the parameters to the
989 * constructor are trivially constant valued expressions.
990 */
991 ir_rvalue *const constant = result->constant_expression_value();
992
993 if (constant != NULL)
994 result = constant;
995 else
996 all_parameters_are_constant = false;
997
998 ir->replace_with(result);
999 }
1000
1001 if (all_parameters_are_constant)
1002 return new(ctx) ir_constant(constructor_type, &actual_parameters);
1003
1004 ir_variable *var = new(ctx) ir_variable(constructor_type, "array_ctor",
1005 ir_var_temporary);
1006 instructions->push_tail(var);
1007
1008 int i = 0;
1009 foreach_in_list(ir_rvalue, rhs, &actual_parameters) {
1010 ir_rvalue *lhs = new(ctx) ir_dereference_array(var,
1011 new(ctx) ir_constant(i));
1012
1013 ir_instruction *assignment = new(ctx) ir_assignment(lhs, rhs, NULL);
1014 instructions->push_tail(assignment);
1015
1016 i++;
1017 }
1018
1019 return new(ctx) ir_dereference_variable(var);
1020 }
1021
1022
1023 /**
1024 * Try to convert a record constructor to a constant expression
1025 */
1026 static ir_constant *
1027 constant_record_constructor(const glsl_type *constructor_type,
1028 exec_list *parameters, void *mem_ctx)
1029 {
1030 foreach_in_list(ir_instruction, node, parameters) {
1031 ir_constant *constant = node->as_constant();
1032 if (constant == NULL)
1033 return NULL;
1034 node->replace_with(constant);
1035 }
1036
1037 return new(mem_ctx) ir_constant(constructor_type, parameters);
1038 }
1039
1040
1041 /**
1042 * Determine if a list consists of a single scalar r-value
1043 */
1044 bool
1045 single_scalar_parameter(exec_list *parameters)
1046 {
1047 const ir_rvalue *const p = (ir_rvalue *) parameters->head;
1048 assert(((ir_rvalue *)p)->as_rvalue() != NULL);
1049
1050 return (p->type->is_scalar() && p->next->is_tail_sentinel());
1051 }
1052
1053
1054 /**
1055 * Generate inline code for a vector constructor
1056 *
1057 * The generated constructor code will consist of a temporary variable
1058 * declaration of the same type as the constructor. A sequence of assignments
1059 * from constructor parameters to the temporary will follow.
1060 *
1061 * \return
1062 * An \c ir_dereference_variable of the temprorary generated in the constructor
1063 * body.
1064 */
1065 ir_rvalue *
1066 emit_inline_vector_constructor(const glsl_type *type,
1067 exec_list *instructions,
1068 exec_list *parameters,
1069 void *ctx)
1070 {
1071 assert(!parameters->is_empty());
1072
1073 ir_variable *var = new(ctx) ir_variable(type, "vec_ctor", ir_var_temporary);
1074 instructions->push_tail(var);
1075
1076 /* There are three kinds of vector constructors.
1077 *
1078 * - Construct a vector from a single scalar by replicating that scalar to
1079 * all components of the vector.
1080 *
1081 * - Construct a vector from at least a matrix. This case should already
1082 * have been taken care of in ast_function_expression::hir by breaking
1083 * down the matrix into a series of column vectors.
1084 *
1085 * - Construct a vector from an arbirary combination of vectors and
1086 * scalars. The components of the constructor parameters are assigned
1087 * to the vector in order until the vector is full.
1088 */
1089 const unsigned lhs_components = type->components();
1090 if (single_scalar_parameter(parameters)) {
1091 ir_rvalue *first_param = (ir_rvalue *)parameters->head;
1092 ir_rvalue *rhs = new(ctx) ir_swizzle(first_param, 0, 0, 0, 0,
1093 lhs_components);
1094 ir_dereference_variable *lhs = new(ctx) ir_dereference_variable(var);
1095 const unsigned mask = (1U << lhs_components) - 1;
1096
1097 assert(rhs->type == lhs->type);
1098
1099 ir_instruction *inst = new(ctx) ir_assignment(lhs, rhs, NULL, mask);
1100 instructions->push_tail(inst);
1101 } else {
1102 unsigned base_component = 0;
1103 unsigned base_lhs_component = 0;
1104 ir_constant_data data;
1105 unsigned constant_mask = 0, constant_components = 0;
1106
1107 memset(&data, 0, sizeof(data));
1108
1109 foreach_in_list(ir_rvalue, param, parameters) {
1110 unsigned rhs_components = param->type->components();
1111
1112 /* Do not try to assign more components to the vector than it has!
1113 */
1114 if ((rhs_components + base_lhs_component) > lhs_components) {
1115 rhs_components = lhs_components - base_lhs_component;
1116 }
1117
1118 const ir_constant *const c = param->as_constant();
1119 if (c != NULL) {
1120 for (unsigned i = 0; i < rhs_components; i++) {
1121 switch (c->type->base_type) {
1122 case GLSL_TYPE_UINT:
1123 data.u[i + base_component] = c->get_uint_component(i);
1124 break;
1125 case GLSL_TYPE_INT:
1126 data.i[i + base_component] = c->get_int_component(i);
1127 break;
1128 case GLSL_TYPE_FLOAT:
1129 data.f[i + base_component] = c->get_float_component(i);
1130 break;
1131 case GLSL_TYPE_DOUBLE:
1132 data.d[i + base_component] = c->get_double_component(i);
1133 break;
1134 case GLSL_TYPE_BOOL:
1135 data.b[i + base_component] = c->get_bool_component(i);
1136 break;
1137 default:
1138 assert(!"Should not get here.");
1139 break;
1140 }
1141 }
1142
1143 /* Mask of fields to be written in the assignment.
1144 */
1145 constant_mask |= ((1U << rhs_components) - 1) << base_lhs_component;
1146 constant_components += rhs_components;
1147
1148 base_component += rhs_components;
1149 }
1150 /* Advance the component index by the number of components
1151 * that were just assigned.
1152 */
1153 base_lhs_component += rhs_components;
1154 }
1155
1156 if (constant_mask != 0) {
1157 ir_dereference *lhs = new(ctx) ir_dereference_variable(var);
1158 const glsl_type *rhs_type = glsl_type::get_instance(var->type->base_type,
1159 constant_components,
1160 1);
1161 ir_rvalue *rhs = new(ctx) ir_constant(rhs_type, &data);
1162
1163 ir_instruction *inst =
1164 new(ctx) ir_assignment(lhs, rhs, NULL, constant_mask);
1165 instructions->push_tail(inst);
1166 }
1167
1168 base_component = 0;
1169 foreach_in_list(ir_rvalue, param, parameters) {
1170 unsigned rhs_components = param->type->components();
1171
1172 /* Do not try to assign more components to the vector than it has!
1173 */
1174 if ((rhs_components + base_component) > lhs_components) {
1175 rhs_components = lhs_components - base_component;
1176 }
1177
1178 /* If we do not have any components left to copy, break out of the
1179 * loop. This can happen when initializing a vec4 with a mat3 as the
1180 * mat3 would have been broken into a series of column vectors.
1181 */
1182 if (rhs_components == 0) {
1183 break;
1184 }
1185
1186 const ir_constant *const c = param->as_constant();
1187 if (c == NULL) {
1188 /* Mask of fields to be written in the assignment.
1189 */
1190 const unsigned write_mask = ((1U << rhs_components) - 1)
1191 << base_component;
1192
1193 ir_dereference *lhs = new(ctx) ir_dereference_variable(var);
1194
1195 /* Generate a swizzle so that LHS and RHS sizes match.
1196 */
1197 ir_rvalue *rhs =
1198 new(ctx) ir_swizzle(param, 0, 1, 2, 3, rhs_components);
1199
1200 ir_instruction *inst =
1201 new(ctx) ir_assignment(lhs, rhs, NULL, write_mask);
1202 instructions->push_tail(inst);
1203 }
1204
1205 /* Advance the component index by the number of components that were
1206 * just assigned.
1207 */
1208 base_component += rhs_components;
1209 }
1210 }
1211 return new(ctx) ir_dereference_variable(var);
1212 }
1213
1214
1215 /**
1216 * Generate assignment of a portion of a vector to a portion of a matrix column
1217 *
1218 * \param src_base First component of the source to be used in assignment
1219 * \param column Column of destination to be assiged
1220 * \param row_base First component of the destination column to be assigned
1221 * \param count Number of components to be assigned
1222 *
1223 * \note
1224 * \c src_base + \c count must be less than or equal to the number of components
1225 * in the source vector.
1226 */
1227 ir_instruction *
1228 assign_to_matrix_column(ir_variable *var, unsigned column, unsigned row_base,
1229 ir_rvalue *src, unsigned src_base, unsigned count,
1230 void *mem_ctx)
1231 {
1232 ir_constant *col_idx = new(mem_ctx) ir_constant(column);
1233 ir_dereference *column_ref = new(mem_ctx) ir_dereference_array(var, col_idx);
1234
1235 assert(column_ref->type->components() >= (row_base + count));
1236 assert(src->type->components() >= (src_base + count));
1237
1238 /* Generate a swizzle that extracts the number of components from the source
1239 * that are to be assigned to the column of the matrix.
1240 */
1241 if (count < src->type->vector_elements) {
1242 src = new(mem_ctx) ir_swizzle(src,
1243 src_base + 0, src_base + 1,
1244 src_base + 2, src_base + 3,
1245 count);
1246 }
1247
1248 /* Mask of fields to be written in the assignment.
1249 */
1250 const unsigned write_mask = ((1U << count) - 1) << row_base;
1251
1252 return new(mem_ctx) ir_assignment(column_ref, src, NULL, write_mask);
1253 }
1254
1255
1256 /**
1257 * Generate inline code for a matrix constructor
1258 *
1259 * The generated constructor code will consist of a temporary variable
1260 * declaration of the same type as the constructor. A sequence of assignments
1261 * from constructor parameters to the temporary will follow.
1262 *
1263 * \return
1264 * An \c ir_dereference_variable of the temprorary generated in the constructor
1265 * body.
1266 */
1267 ir_rvalue *
1268 emit_inline_matrix_constructor(const glsl_type *type,
1269 exec_list *instructions,
1270 exec_list *parameters,
1271 void *ctx)
1272 {
1273 assert(!parameters->is_empty());
1274
1275 ir_variable *var = new(ctx) ir_variable(type, "mat_ctor", ir_var_temporary);
1276 instructions->push_tail(var);
1277
1278 /* There are three kinds of matrix constructors.
1279 *
1280 * - Construct a matrix from a single scalar by replicating that scalar to
1281 * along the diagonal of the matrix and setting all other components to
1282 * zero.
1283 *
1284 * - Construct a matrix from an arbirary combination of vectors and
1285 * scalars. The components of the constructor parameters are assigned
1286 * to the matrix in column-major order until the matrix is full.
1287 *
1288 * - Construct a matrix from a single matrix. The source matrix is copied
1289 * to the upper left portion of the constructed matrix, and the remaining
1290 * elements take values from the identity matrix.
1291 */
1292 ir_rvalue *const first_param = (ir_rvalue *) parameters->head;
1293 if (single_scalar_parameter(parameters)) {
1294 /* Assign the scalar to the X component of a vec4, and fill the remaining
1295 * components with zero.
1296 */
1297 glsl_base_type param_base_type = first_param->type->base_type;
1298 assert(param_base_type == GLSL_TYPE_FLOAT ||
1299 param_base_type == GLSL_TYPE_DOUBLE);
1300 ir_variable *rhs_var =
1301 new(ctx) ir_variable(glsl_type::get_instance(param_base_type, 4, 1),
1302 "mat_ctor_vec",
1303 ir_var_temporary);
1304 instructions->push_tail(rhs_var);
1305
1306 ir_constant_data zero;
1307 for (unsigned i = 0; i < 4; i++)
1308 if (param_base_type == GLSL_TYPE_FLOAT)
1309 zero.f[i] = 0.0;
1310 else
1311 zero.d[i] = 0.0;
1312
1313 ir_instruction *inst =
1314 new(ctx) ir_assignment(new(ctx) ir_dereference_variable(rhs_var),
1315 new(ctx) ir_constant(rhs_var->type, &zero),
1316 NULL);
1317 instructions->push_tail(inst);
1318
1319 ir_dereference *const rhs_ref = new(ctx) ir_dereference_variable(rhs_var);
1320
1321 inst = new(ctx) ir_assignment(rhs_ref, first_param, NULL, 0x01);
1322 instructions->push_tail(inst);
1323
1324 /* Assign the temporary vector to each column of the destination matrix
1325 * with a swizzle that puts the X component on the diagonal of the
1326 * matrix. In some cases this may mean that the X component does not
1327 * get assigned into the column at all (i.e., when the matrix has more
1328 * columns than rows).
1329 */
1330 static const unsigned rhs_swiz[4][4] = {
1331 { 0, 1, 1, 1 },
1332 { 1, 0, 1, 1 },
1333 { 1, 1, 0, 1 },
1334 { 1, 1, 1, 0 }
1335 };
1336
1337 const unsigned cols_to_init = MIN2(type->matrix_columns,
1338 type->vector_elements);
1339 for (unsigned i = 0; i < cols_to_init; i++) {
1340 ir_constant *const col_idx = new(ctx) ir_constant(i);
1341 ir_rvalue *const col_ref = new(ctx) ir_dereference_array(var, col_idx);
1342
1343 ir_rvalue *const rhs_ref = new(ctx) ir_dereference_variable(rhs_var);
1344 ir_rvalue *const rhs = new(ctx) ir_swizzle(rhs_ref, rhs_swiz[i],
1345 type->vector_elements);
1346
1347 inst = new(ctx) ir_assignment(col_ref, rhs, NULL);
1348 instructions->push_tail(inst);
1349 }
1350
1351 for (unsigned i = cols_to_init; i < type->matrix_columns; i++) {
1352 ir_constant *const col_idx = new(ctx) ir_constant(i);
1353 ir_rvalue *const col_ref = new(ctx) ir_dereference_array(var, col_idx);
1354
1355 ir_rvalue *const rhs_ref = new(ctx) ir_dereference_variable(rhs_var);
1356 ir_rvalue *const rhs = new(ctx) ir_swizzle(rhs_ref, 1, 1, 1, 1,
1357 type->vector_elements);
1358
1359 inst = new(ctx) ir_assignment(col_ref, rhs, NULL);
1360 instructions->push_tail(inst);
1361 }
1362 } else if (first_param->type->is_matrix()) {
1363 /* From page 50 (56 of the PDF) of the GLSL 1.50 spec:
1364 *
1365 * "If a matrix is constructed from a matrix, then each component
1366 * (column i, row j) in the result that has a corresponding
1367 * component (column i, row j) in the argument will be initialized
1368 * from there. All other components will be initialized to the
1369 * identity matrix. If a matrix argument is given to a matrix
1370 * constructor, it is an error to have any other arguments."
1371 */
1372 assert(first_param->next->is_tail_sentinel());
1373 ir_rvalue *const src_matrix = first_param;
1374
1375 /* If the source matrix is smaller, pre-initialize the relavent parts of
1376 * the destination matrix to the identity matrix.
1377 */
1378 if ((src_matrix->type->matrix_columns < var->type->matrix_columns)
1379 || (src_matrix->type->vector_elements < var->type->vector_elements)) {
1380
1381 /* If the source matrix has fewer rows, every column of the destination
1382 * must be initialized. Otherwise only the columns in the destination
1383 * that do not exist in the source must be initialized.
1384 */
1385 unsigned col =
1386 (src_matrix->type->vector_elements < var->type->vector_elements)
1387 ? 0 : src_matrix->type->matrix_columns;
1388
1389 const glsl_type *const col_type = var->type->column_type();
1390 for (/* empty */; col < var->type->matrix_columns; col++) {
1391 ir_constant_data ident;
1392
1393 ident.f[0] = 0.0;
1394 ident.f[1] = 0.0;
1395 ident.f[2] = 0.0;
1396 ident.f[3] = 0.0;
1397
1398 ident.f[col] = 1.0;
1399
1400 ir_rvalue *const rhs = new(ctx) ir_constant(col_type, &ident);
1401
1402 ir_rvalue *const lhs =
1403 new(ctx) ir_dereference_array(var, new(ctx) ir_constant(col));
1404
1405 ir_instruction *inst = new(ctx) ir_assignment(lhs, rhs, NULL);
1406 instructions->push_tail(inst);
1407 }
1408 }
1409
1410 /* Assign columns from the source matrix to the destination matrix.
1411 *
1412 * Since the parameter will be used in the RHS of multiple assignments,
1413 * generate a temporary and copy the paramter there.
1414 */
1415 ir_variable *const rhs_var =
1416 new(ctx) ir_variable(first_param->type, "mat_ctor_mat",
1417 ir_var_temporary);
1418 instructions->push_tail(rhs_var);
1419
1420 ir_dereference *const rhs_var_ref =
1421 new(ctx) ir_dereference_variable(rhs_var);
1422 ir_instruction *const inst =
1423 new(ctx) ir_assignment(rhs_var_ref, first_param, NULL);
1424 instructions->push_tail(inst);
1425
1426 const unsigned last_row = MIN2(src_matrix->type->vector_elements,
1427 var->type->vector_elements);
1428 const unsigned last_col = MIN2(src_matrix->type->matrix_columns,
1429 var->type->matrix_columns);
1430
1431 unsigned swiz[4] = { 0, 0, 0, 0 };
1432 for (unsigned i = 1; i < last_row; i++)
1433 swiz[i] = i;
1434
1435 const unsigned write_mask = (1U << last_row) - 1;
1436
1437 for (unsigned i = 0; i < last_col; i++) {
1438 ir_dereference *const lhs =
1439 new(ctx) ir_dereference_array(var, new(ctx) ir_constant(i));
1440 ir_rvalue *const rhs_col =
1441 new(ctx) ir_dereference_array(rhs_var, new(ctx) ir_constant(i));
1442
1443 /* If one matrix has columns that are smaller than the columns of the
1444 * other matrix, wrap the column access of the larger with a swizzle
1445 * so that the LHS and RHS of the assignment have the same size (and
1446 * therefore have the same type).
1447 *
1448 * It would be perfectly valid to unconditionally generate the
1449 * swizzles, this this will typically result in a more compact IR tree.
1450 */
1451 ir_rvalue *rhs;
1452 if (lhs->type->vector_elements != rhs_col->type->vector_elements) {
1453 rhs = new(ctx) ir_swizzle(rhs_col, swiz, last_row);
1454 } else {
1455 rhs = rhs_col;
1456 }
1457
1458 ir_instruction *inst =
1459 new(ctx) ir_assignment(lhs, rhs, NULL, write_mask);
1460 instructions->push_tail(inst);
1461 }
1462 } else {
1463 const unsigned cols = type->matrix_columns;
1464 const unsigned rows = type->vector_elements;
1465 unsigned remaining_slots = rows * cols;
1466 unsigned col_idx = 0;
1467 unsigned row_idx = 0;
1468
1469 foreach_in_list(ir_rvalue, rhs, parameters) {
1470 unsigned rhs_components = rhs->type->components();
1471 unsigned rhs_base = 0;
1472
1473 if (remaining_slots == 0)
1474 break;
1475
1476 /* Since the parameter might be used in the RHS of two assignments,
1477 * generate a temporary and copy the paramter there.
1478 */
1479 ir_variable *rhs_var =
1480 new(ctx) ir_variable(rhs->type, "mat_ctor_vec", ir_var_temporary);
1481 instructions->push_tail(rhs_var);
1482
1483 ir_dereference *rhs_var_ref =
1484 new(ctx) ir_dereference_variable(rhs_var);
1485 ir_instruction *inst = new(ctx) ir_assignment(rhs_var_ref, rhs, NULL);
1486 instructions->push_tail(inst);
1487
1488 do {
1489 /* Assign the current parameter to as many components of the matrix
1490 * as it will fill.
1491 *
1492 * NOTE: A single vector parameter can span two matrix columns. A
1493 * single vec4, for example, can completely fill a mat2.
1494 */
1495 unsigned count = MIN2(rows - row_idx,
1496 rhs_components - rhs_base);
1497
1498 rhs_var_ref = new(ctx) ir_dereference_variable(rhs_var);
1499 ir_instruction *inst = assign_to_matrix_column(var, col_idx,
1500 row_idx,
1501 rhs_var_ref,
1502 rhs_base,
1503 count, ctx);
1504 instructions->push_tail(inst);
1505 rhs_base += count;
1506 row_idx += count;
1507 remaining_slots -= count;
1508
1509 /* Sometimes, there is still data left in the parameters and
1510 * components left to be set in the destination but in other
1511 * column.
1512 */
1513 if (row_idx >= rows) {
1514 row_idx = 0;
1515 col_idx++;
1516 }
1517 } while(remaining_slots > 0 && rhs_base < rhs_components);
1518 }
1519 }
1520
1521 return new(ctx) ir_dereference_variable(var);
1522 }
1523
1524
1525 ir_rvalue *
1526 emit_inline_record_constructor(const glsl_type *type,
1527 exec_list *instructions,
1528 exec_list *parameters,
1529 void *mem_ctx)
1530 {
1531 ir_variable *const var =
1532 new(mem_ctx) ir_variable(type, "record_ctor", ir_var_temporary);
1533 ir_dereference_variable *const d = new(mem_ctx) ir_dereference_variable(var);
1534
1535 instructions->push_tail(var);
1536
1537 exec_node *node = parameters->head;
1538 for (unsigned i = 0; i < type->length; i++) {
1539 assert(!node->is_tail_sentinel());
1540
1541 ir_dereference *const lhs =
1542 new(mem_ctx) ir_dereference_record(d->clone(mem_ctx, NULL),
1543 type->fields.structure[i].name);
1544
1545 ir_rvalue *const rhs = ((ir_instruction *) node)->as_rvalue();
1546 assert(rhs != NULL);
1547
1548 ir_instruction *const assign = new(mem_ctx) ir_assignment(lhs, rhs, NULL);
1549
1550 instructions->push_tail(assign);
1551 node = node->next;
1552 }
1553
1554 return d;
1555 }
1556
1557
1558 static ir_rvalue *
1559 process_record_constructor(exec_list *instructions,
1560 const glsl_type *constructor_type,
1561 YYLTYPE *loc, exec_list *parameters,
1562 struct _mesa_glsl_parse_state *state)
1563 {
1564 void *ctx = state;
1565 exec_list actual_parameters;
1566
1567 process_parameters(instructions, &actual_parameters,
1568 parameters, state);
1569
1570 exec_node *node = actual_parameters.head;
1571 for (unsigned i = 0; i < constructor_type->length; i++) {
1572 ir_rvalue *ir = (ir_rvalue *) node;
1573
1574 if (node->is_tail_sentinel()) {
1575 _mesa_glsl_error(loc, state,
1576 "insufficient parameters to constructor for `%s'",
1577 constructor_type->name);
1578 return ir_rvalue::error_value(ctx);
1579 }
1580
1581 if (apply_implicit_conversion(constructor_type->fields.structure[i].type,
1582 ir, state)) {
1583 node->replace_with(ir);
1584 } else {
1585 _mesa_glsl_error(loc, state,
1586 "parameter type mismatch in constructor for `%s.%s' "
1587 "(%s vs %s)",
1588 constructor_type->name,
1589 constructor_type->fields.structure[i].name,
1590 ir->type->name,
1591 constructor_type->fields.structure[i].type->name);
1592 return ir_rvalue::error_value(ctx);;
1593 }
1594
1595 node = node->next;
1596 }
1597
1598 if (!node->is_tail_sentinel()) {
1599 _mesa_glsl_error(loc, state, "too many parameters in constructor "
1600 "for `%s'", constructor_type->name);
1601 return ir_rvalue::error_value(ctx);
1602 }
1603
1604 ir_rvalue *const constant =
1605 constant_record_constructor(constructor_type, &actual_parameters,
1606 state);
1607
1608 return (constant != NULL)
1609 ? constant
1610 : emit_inline_record_constructor(constructor_type, instructions,
1611 &actual_parameters, state);
1612 }
1613
1614 ir_rvalue *
1615 ast_function_expression::handle_method(exec_list *instructions,
1616 struct _mesa_glsl_parse_state *state)
1617 {
1618 const ast_expression *field = subexpressions[0];
1619 ir_rvalue *op;
1620 ir_rvalue *result;
1621 void *ctx = state;
1622 /* Handle "method calls" in GLSL 1.20 - namely, array.length() */
1623 YYLTYPE loc = get_location();
1624 state->check_version(120, 300, &loc, "methods not supported");
1625
1626 const char *method;
1627 method = field->primary_expression.identifier;
1628
1629 op = field->subexpressions[0]->hir(instructions, state);
1630 if (strcmp(method, "length") == 0) {
1631 if (!this->expressions.is_empty()) {
1632 _mesa_glsl_error(&loc, state, "length method takes no arguments");
1633 goto fail;
1634 }
1635
1636 if (op->type->is_array()) {
1637 if (op->type->is_unsized_array()) {
1638 if (!state->has_shader_storage_buffer_objects()) {
1639 _mesa_glsl_error(&loc, state, "length called on unsized array"
1640 " only available with "
1641 "ARB_shader_storage_buffer_object");
1642 }
1643 /* Calculate length of an unsized array in run-time */
1644 result = new(ctx) ir_expression(ir_unop_ssbo_unsized_array_length, op);
1645 } else {
1646 result = new(ctx) ir_constant(op->type->array_size());
1647 }
1648 } else if (op->type->is_vector()) {
1649 if (state->ARB_shading_language_420pack_enable) {
1650 /* .length() returns int. */
1651 result = new(ctx) ir_constant((int) op->type->vector_elements);
1652 } else {
1653 _mesa_glsl_error(&loc, state, "length method on matrix only available"
1654 "with ARB_shading_language_420pack");
1655 goto fail;
1656 }
1657 } else if (op->type->is_matrix()) {
1658 if (state->ARB_shading_language_420pack_enable) {
1659 /* .length() returns int. */
1660 result = new(ctx) ir_constant((int) op->type->matrix_columns);
1661 } else {
1662 _mesa_glsl_error(&loc, state, "length method on matrix only available"
1663 "with ARB_shading_language_420pack");
1664 goto fail;
1665 }
1666 } else {
1667 _mesa_glsl_error(&loc, state, "length called on scalar.");
1668 goto fail;
1669 }
1670 } else {
1671 _mesa_glsl_error(&loc, state, "unknown method: `%s'", method);
1672 goto fail;
1673 }
1674 return result;
1675 fail:
1676 return ir_rvalue::error_value(ctx);
1677 }
1678
1679 ir_rvalue *
1680 ast_function_expression::hir(exec_list *instructions,
1681 struct _mesa_glsl_parse_state *state)
1682 {
1683 void *ctx = state;
1684 /* There are three sorts of function calls.
1685 *
1686 * 1. constructors - The first subexpression is an ast_type_specifier.
1687 * 2. methods - Only the .length() method of array types.
1688 * 3. functions - Calls to regular old functions.
1689 *
1690 */
1691 if (is_constructor()) {
1692 const ast_type_specifier *type = (ast_type_specifier *) subexpressions[0];
1693 YYLTYPE loc = type->get_location();
1694 const char *name;
1695
1696 const glsl_type *const constructor_type = type->glsl_type(& name, state);
1697
1698 /* constructor_type can be NULL if a variable with the same name as the
1699 * structure has come into scope.
1700 */
1701 if (constructor_type == NULL) {
1702 _mesa_glsl_error(& loc, state, "unknown type `%s' (structure name "
1703 "may be shadowed by a variable with the same name)",
1704 type->type_name);
1705 return ir_rvalue::error_value(ctx);
1706 }
1707
1708
1709 /* Constructors for opaque types are illegal.
1710 */
1711 if (constructor_type->contains_opaque()) {
1712 _mesa_glsl_error(& loc, state, "cannot construct opaque type `%s'",
1713 constructor_type->name);
1714 return ir_rvalue::error_value(ctx);
1715 }
1716
1717 if (constructor_type->is_array()) {
1718 if (!state->check_version(120, 300, &loc,
1719 "array constructors forbidden")) {
1720 return ir_rvalue::error_value(ctx);
1721 }
1722
1723 return process_array_constructor(instructions, constructor_type,
1724 & loc, &this->expressions, state);
1725 }
1726
1727
1728 /* There are two kinds of constructor calls. Constructors for arrays and
1729 * structures must have the exact number of arguments with matching types
1730 * in the correct order. These constructors follow essentially the same
1731 * type matching rules as functions.
1732 *
1733 * Constructors for built-in language types, such as mat4 and vec2, are
1734 * free form. The only requirements are that the parameters must provide
1735 * enough values of the correct scalar type and that no arguments are
1736 * given past the last used argument.
1737 *
1738 * When using the C-style initializer syntax from GLSL 4.20, constructors
1739 * must have the exact number of arguments with matching types in the
1740 * correct order.
1741 */
1742 if (constructor_type->is_record()) {
1743 return process_record_constructor(instructions, constructor_type,
1744 &loc, &this->expressions,
1745 state);
1746 }
1747
1748 if (!constructor_type->is_numeric() && !constructor_type->is_boolean())
1749 return ir_rvalue::error_value(ctx);
1750
1751 /* Total number of components of the type being constructed. */
1752 const unsigned type_components = constructor_type->components();
1753
1754 /* Number of components from parameters that have actually been
1755 * consumed. This is used to perform several kinds of error checking.
1756 */
1757 unsigned components_used = 0;
1758
1759 unsigned matrix_parameters = 0;
1760 unsigned nonmatrix_parameters = 0;
1761 exec_list actual_parameters;
1762
1763 foreach_list_typed(ast_node, ast, link, &this->expressions) {
1764 ir_rvalue *result = ast->hir(instructions, state);
1765
1766 /* From page 50 (page 56 of the PDF) of the GLSL 1.50 spec:
1767 *
1768 * "It is an error to provide extra arguments beyond this
1769 * last used argument."
1770 */
1771 if (components_used >= type_components) {
1772 _mesa_glsl_error(& loc, state, "too many parameters to `%s' "
1773 "constructor",
1774 constructor_type->name);
1775 return ir_rvalue::error_value(ctx);
1776 }
1777
1778 if (!result->type->is_numeric() && !result->type->is_boolean()) {
1779 _mesa_glsl_error(& loc, state, "cannot construct `%s' from a "
1780 "non-numeric data type",
1781 constructor_type->name);
1782 return ir_rvalue::error_value(ctx);
1783 }
1784
1785 /* Count the number of matrix and nonmatrix parameters. This
1786 * is used below to enforce some of the constructor rules.
1787 */
1788 if (result->type->is_matrix())
1789 matrix_parameters++;
1790 else
1791 nonmatrix_parameters++;
1792
1793 actual_parameters.push_tail(result);
1794 components_used += result->type->components();
1795 }
1796
1797 /* From page 28 (page 34 of the PDF) of the GLSL 1.10 spec:
1798 *
1799 * "It is an error to construct matrices from other matrices. This
1800 * is reserved for future use."
1801 */
1802 if (matrix_parameters > 0
1803 && constructor_type->is_matrix()
1804 && !state->check_version(120, 100, &loc,
1805 "cannot construct `%s' from a matrix",
1806 constructor_type->name)) {
1807 return ir_rvalue::error_value(ctx);
1808 }
1809
1810 /* From page 50 (page 56 of the PDF) of the GLSL 1.50 spec:
1811 *
1812 * "If a matrix argument is given to a matrix constructor, it is
1813 * an error to have any other arguments."
1814 */
1815 if ((matrix_parameters > 0)
1816 && ((matrix_parameters + nonmatrix_parameters) > 1)
1817 && constructor_type->is_matrix()) {
1818 _mesa_glsl_error(& loc, state, "for matrix `%s' constructor, "
1819 "matrix must be only parameter",
1820 constructor_type->name);
1821 return ir_rvalue::error_value(ctx);
1822 }
1823
1824 /* From page 28 (page 34 of the PDF) of the GLSL 1.10 spec:
1825 *
1826 * "In these cases, there must be enough components provided in the
1827 * arguments to provide an initializer for every component in the
1828 * constructed value."
1829 */
1830 if (components_used < type_components && components_used != 1
1831 && matrix_parameters == 0) {
1832 _mesa_glsl_error(& loc, state, "too few components to construct "
1833 "`%s'",
1834 constructor_type->name);
1835 return ir_rvalue::error_value(ctx);
1836 }
1837
1838 /* Matrices can never be consumed as is by any constructor but matrix
1839 * constructors. If the constructor type is not matrix, always break the
1840 * matrix up into a series of column vectors.
1841 */
1842 if (!constructor_type->is_matrix()) {
1843 foreach_in_list_safe(ir_rvalue, matrix, &actual_parameters) {
1844 if (!matrix->type->is_matrix())
1845 continue;
1846
1847 /* Create a temporary containing the matrix. */
1848 ir_variable *var = new(ctx) ir_variable(matrix->type, "matrix_tmp",
1849 ir_var_temporary);
1850 instructions->push_tail(var);
1851 instructions->push_tail(new(ctx) ir_assignment(new(ctx)
1852 ir_dereference_variable(var), matrix, NULL));
1853 var->constant_value = matrix->constant_expression_value();
1854
1855 /* Replace the matrix with dereferences of its columns. */
1856 for (int i = 0; i < matrix->type->matrix_columns; i++) {
1857 matrix->insert_before(new (ctx) ir_dereference_array(var,
1858 new(ctx) ir_constant(i)));
1859 }
1860 matrix->remove();
1861 }
1862 }
1863
1864 bool all_parameters_are_constant = true;
1865
1866 /* Type cast each parameter and, if possible, fold constants.*/
1867 foreach_in_list_safe(ir_rvalue, ir, &actual_parameters) {
1868 const glsl_type *desired_type =
1869 glsl_type::get_instance(constructor_type->base_type,
1870 ir->type->vector_elements,
1871 ir->type->matrix_columns);
1872 ir_rvalue *result = convert_component(ir, desired_type);
1873
1874 /* Attempt to convert the parameter to a constant valued expression.
1875 * After doing so, track whether or not all the parameters to the
1876 * constructor are trivially constant valued expressions.
1877 */
1878 ir_rvalue *const constant = result->constant_expression_value();
1879
1880 if (constant != NULL)
1881 result = constant;
1882 else
1883 all_parameters_are_constant = false;
1884
1885 if (result != ir) {
1886 ir->replace_with(result);
1887 }
1888 }
1889
1890 /* If all of the parameters are trivially constant, create a
1891 * constant representing the complete collection of parameters.
1892 */
1893 if (all_parameters_are_constant) {
1894 return new(ctx) ir_constant(constructor_type, &actual_parameters);
1895 } else if (constructor_type->is_scalar()) {
1896 return dereference_component((ir_rvalue *) actual_parameters.head,
1897 0);
1898 } else if (constructor_type->is_vector()) {
1899 return emit_inline_vector_constructor(constructor_type,
1900 instructions,
1901 &actual_parameters,
1902 ctx);
1903 } else {
1904 assert(constructor_type->is_matrix());
1905 return emit_inline_matrix_constructor(constructor_type,
1906 instructions,
1907 &actual_parameters,
1908 ctx);
1909 }
1910 } else if (subexpressions[0]->oper == ast_field_selection) {
1911 return handle_method(instructions, state);
1912 } else {
1913 const ast_expression *id = subexpressions[0];
1914 const char *func_name;
1915 YYLTYPE loc = get_location();
1916 exec_list actual_parameters;
1917 ir_variable *sub_var = NULL;
1918 ir_rvalue *array_idx = NULL;
1919
1920 if (id->oper == ast_array_index) {
1921 func_name = id->subexpressions[0]->primary_expression.identifier;
1922 array_idx = id->subexpressions[1]->hir(instructions, state);
1923 } else {
1924 func_name = id->primary_expression.identifier;
1925 }
1926
1927 process_parameters(instructions, &actual_parameters, &this->expressions,
1928 state);
1929
1930 ir_function_signature *sig =
1931 match_function_by_name(func_name, &actual_parameters, state);
1932
1933 ir_rvalue *value = NULL;
1934 if (sig == NULL) {
1935 sig = match_subroutine_by_name(func_name, &actual_parameters, state, &sub_var);
1936 }
1937
1938 if (sig == NULL) {
1939 no_matching_function_error(func_name, &loc, &actual_parameters, state);
1940 value = ir_rvalue::error_value(ctx);
1941 } else if (!verify_parameter_modes(state, sig, actual_parameters, this->expressions)) {
1942 /* an error has already been emitted */
1943 value = ir_rvalue::error_value(ctx);
1944 } else {
1945 value = generate_call(instructions, sig, &actual_parameters, sub_var, array_idx, state);
1946 if (!value) {
1947 ir_variable *const tmp = new(ctx) ir_variable(glsl_type::void_type,
1948 "void_var",
1949 ir_var_temporary);
1950 instructions->push_tail(tmp);
1951 value = new(ctx) ir_dereference_variable(tmp);
1952 }
1953 }
1954
1955 return value;
1956 }
1957
1958 unreachable("not reached");
1959 }
1960
1961 ir_rvalue *
1962 ast_aggregate_initializer::hir(exec_list *instructions,
1963 struct _mesa_glsl_parse_state *state)
1964 {
1965 void *ctx = state;
1966 YYLTYPE loc = this->get_location();
1967
1968 if (!this->constructor_type) {
1969 _mesa_glsl_error(&loc, state, "type of C-style initializer unknown");
1970 return ir_rvalue::error_value(ctx);
1971 }
1972 const glsl_type *const constructor_type = this->constructor_type;
1973
1974 if (!state->ARB_shading_language_420pack_enable) {
1975 _mesa_glsl_error(&loc, state, "C-style initialization requires the "
1976 "GL_ARB_shading_language_420pack extension");
1977 return ir_rvalue::error_value(ctx);
1978 }
1979
1980 if (constructor_type->is_array()) {
1981 return process_array_constructor(instructions, constructor_type, &loc,
1982 &this->expressions, state);
1983 }
1984
1985 if (constructor_type->is_record()) {
1986 return process_record_constructor(instructions, constructor_type, &loc,
1987 &this->expressions, state);
1988 }
1989
1990 return process_vec_mat_constructor(instructions, constructor_type, &loc,
1991 &this->expressions, state);
1992 }