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