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