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