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 struct glsl_symbol_table *symb = _mesa_glsl_get_builtin_function_symbols();
847
848 if (!function_exists(state, state->symbols, name)
849 && (!state->uses_builtin_functions
850 || !function_exists(state, symb, 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, symb->get_function(name));
865 }
866 }
867 }
868
869 /**
870 * Perform automatic type conversion of constructor parameters
871 *
872 * This implements the rules in the "Conversion and Scalar Constructors"
873 * section (GLSL 1.10 section 5.4.1), not the "Implicit Conversions" rules.
874 */
875 static ir_rvalue *
convert_component(ir_rvalue * src,const glsl_type * desired_type)876 convert_component(ir_rvalue *src, const glsl_type *desired_type)
877 {
878 void *ctx = ralloc_parent(src);
879 const unsigned a = desired_type->base_type;
880 const unsigned b = src->type->base_type;
881 ir_expression *result = NULL;
882
883 if (glsl_type_is_error(src->type))
884 return src;
885
886 assert(a <= GLSL_TYPE_IMAGE);
887 assert(b <= GLSL_TYPE_IMAGE);
888
889 if (a == b)
890 return src;
891
892 switch (a) {
893 case GLSL_TYPE_UINT:
894 switch (b) {
895 case GLSL_TYPE_INT:
896 result = new(ctx) ir_expression(ir_unop_i2u, src);
897 break;
898 case GLSL_TYPE_FLOAT16:
899 result = new(ctx) ir_expression(ir_unop_f162u, src);
900 break;
901 case GLSL_TYPE_FLOAT:
902 result = new(ctx) ir_expression(ir_unop_f2u, src);
903 break;
904 case GLSL_TYPE_BOOL:
905 result = new(ctx) ir_expression(ir_unop_i2u,
906 new(ctx) ir_expression(ir_unop_b2i,
907 src));
908 break;
909 case GLSL_TYPE_DOUBLE:
910 result = new(ctx) ir_expression(ir_unop_d2u, src);
911 break;
912 case GLSL_TYPE_UINT64:
913 result = new(ctx) ir_expression(ir_unop_u642u, src);
914 break;
915 case GLSL_TYPE_INT64:
916 result = new(ctx) ir_expression(ir_unop_i642u, src);
917 break;
918 case GLSL_TYPE_SAMPLER:
919 result = new(ctx) ir_expression(ir_unop_unpack_sampler_2x32, src);
920 break;
921 case GLSL_TYPE_IMAGE:
922 result = new(ctx) ir_expression(ir_unop_unpack_image_2x32, src);
923 break;
924 }
925 break;
926 case GLSL_TYPE_INT:
927 switch (b) {
928 case GLSL_TYPE_UINT:
929 result = new(ctx) ir_expression(ir_unop_u2i, src);
930 break;
931 case GLSL_TYPE_FLOAT16:
932 result = new(ctx) ir_expression(ir_unop_f162i, src);
933 break;
934 case GLSL_TYPE_FLOAT:
935 result = new(ctx) ir_expression(ir_unop_f2i, src);
936 break;
937 case GLSL_TYPE_BOOL:
938 result = new(ctx) ir_expression(ir_unop_b2i, src);
939 break;
940 case GLSL_TYPE_DOUBLE:
941 result = new(ctx) ir_expression(ir_unop_d2i, src);
942 break;
943 case GLSL_TYPE_UINT64:
944 result = new(ctx) ir_expression(ir_unop_u642i, src);
945 break;
946 case GLSL_TYPE_INT64:
947 result = new(ctx) ir_expression(ir_unop_i642i, src);
948 break;
949 }
950 break;
951 case GLSL_TYPE_FLOAT16:
952 switch (b) {
953 case GLSL_TYPE_UINT:
954 result = new(ctx) ir_expression(ir_unop_u2f16, desired_type, src, NULL);
955 break;
956 case GLSL_TYPE_INT:
957 result = new(ctx) ir_expression(ir_unop_i2f16, desired_type, src, NULL);
958 break;
959 case GLSL_TYPE_BOOL:
960 result = new(ctx) ir_expression(ir_unop_b2f16, desired_type, src, NULL);
961 break;
962 case GLSL_TYPE_FLOAT:
963 result = new(ctx) ir_expression(ir_unop_f2f16, desired_type, src, NULL);
964 break;
965 case GLSL_TYPE_DOUBLE:
966 result = new(ctx) ir_expression(ir_unop_d2f16, desired_type, src, NULL);
967 break;
968 case GLSL_TYPE_UINT64:
969 result = new(ctx) ir_expression(ir_unop_u642f16, desired_type, src, NULL);
970 break;
971 case GLSL_TYPE_INT64:
972 result = new(ctx) ir_expression(ir_unop_i642f16, desired_type, src, NULL);
973 break;
974 }
975 break;
976 case GLSL_TYPE_FLOAT:
977 switch (b) {
978 case GLSL_TYPE_UINT:
979 result = new(ctx) ir_expression(ir_unop_u2f, desired_type, src, NULL);
980 break;
981 case GLSL_TYPE_INT:
982 result = new(ctx) ir_expression(ir_unop_i2f, desired_type, src, NULL);
983 break;
984 case GLSL_TYPE_BOOL:
985 result = new(ctx) ir_expression(ir_unop_b2f, desired_type, src, NULL);
986 break;
987 case GLSL_TYPE_FLOAT16:
988 result = new(ctx) ir_expression(ir_unop_f162f, desired_type, src, NULL);
989 break;
990 case GLSL_TYPE_DOUBLE:
991 result = new(ctx) ir_expression(ir_unop_d2f, desired_type, src, NULL);
992 break;
993 case GLSL_TYPE_UINT64:
994 result = new(ctx) ir_expression(ir_unop_u642f, desired_type, src, NULL);
995 break;
996 case GLSL_TYPE_INT64:
997 result = new(ctx) ir_expression(ir_unop_i642f, desired_type, src, NULL);
998 break;
999 }
1000 break;
1001 case GLSL_TYPE_BOOL:
1002 switch (b) {
1003 case GLSL_TYPE_UINT:
1004 result = new(ctx) ir_expression(ir_unop_i2b,
1005 new(ctx) ir_expression(ir_unop_u2i,
1006 src));
1007 break;
1008 case GLSL_TYPE_INT:
1009 result = new(ctx) ir_expression(ir_unop_i2b, desired_type, src, NULL);
1010 break;
1011 case GLSL_TYPE_FLOAT16:
1012 result = new(ctx) ir_expression(ir_unop_f162b, desired_type, src, NULL);
1013 break;
1014 case GLSL_TYPE_FLOAT:
1015 result = new(ctx) ir_expression(ir_unop_f2b, desired_type, src, NULL);
1016 break;
1017 case GLSL_TYPE_DOUBLE:
1018 result = new(ctx) ir_expression(ir_unop_d2b, desired_type, src, NULL);
1019 break;
1020 case GLSL_TYPE_UINT64:
1021 result = new(ctx) ir_expression(ir_unop_i642b,
1022 new(ctx) ir_expression(ir_unop_u642i64,
1023 src));
1024 break;
1025 case GLSL_TYPE_INT64:
1026 result = new(ctx) ir_expression(ir_unop_i642b, desired_type, src, NULL);
1027 break;
1028 }
1029 break;
1030 case GLSL_TYPE_DOUBLE:
1031 switch (b) {
1032 case GLSL_TYPE_INT:
1033 result = new(ctx) ir_expression(ir_unop_i2d, src);
1034 break;
1035 case GLSL_TYPE_UINT:
1036 result = new(ctx) ir_expression(ir_unop_u2d, src);
1037 break;
1038 case GLSL_TYPE_BOOL:
1039 result = new(ctx) ir_expression(ir_unop_f2d,
1040 new(ctx) ir_expression(ir_unop_b2f,
1041 src));
1042 break;
1043 case GLSL_TYPE_FLOAT16:
1044 result = new(ctx) ir_expression(ir_unop_f162d, desired_type, src, NULL);
1045 break;
1046 case GLSL_TYPE_FLOAT:
1047 result = new(ctx) ir_expression(ir_unop_f2d, desired_type, src, NULL);
1048 break;
1049 case GLSL_TYPE_UINT64:
1050 result = new(ctx) ir_expression(ir_unop_u642d, desired_type, src, NULL);
1051 break;
1052 case GLSL_TYPE_INT64:
1053 result = new(ctx) ir_expression(ir_unop_i642d, desired_type, src, NULL);
1054 break;
1055 }
1056 break;
1057 case GLSL_TYPE_UINT64:
1058 switch (b) {
1059 case GLSL_TYPE_INT:
1060 result = new(ctx) ir_expression(ir_unop_i2u64, src);
1061 break;
1062 case GLSL_TYPE_UINT:
1063 result = new(ctx) ir_expression(ir_unop_u2u64, src);
1064 break;
1065 case GLSL_TYPE_BOOL:
1066 result = new(ctx) ir_expression(ir_unop_i642u64,
1067 new(ctx) ir_expression(ir_unop_b2i64,
1068 src));
1069 break;
1070 case GLSL_TYPE_FLOAT16:
1071 result = new(ctx) ir_expression(ir_unop_f162u64, src);
1072 break;
1073 case GLSL_TYPE_FLOAT:
1074 result = new(ctx) ir_expression(ir_unop_f2u64, src);
1075 break;
1076 case GLSL_TYPE_DOUBLE:
1077 result = new(ctx) ir_expression(ir_unop_d2u64, src);
1078 break;
1079 case GLSL_TYPE_INT64:
1080 result = new(ctx) ir_expression(ir_unop_i642u64, src);
1081 break;
1082 }
1083 break;
1084 case GLSL_TYPE_INT64:
1085 switch (b) {
1086 case GLSL_TYPE_INT:
1087 result = new(ctx) ir_expression(ir_unop_i2i64, src);
1088 break;
1089 case GLSL_TYPE_UINT:
1090 result = new(ctx) ir_expression(ir_unop_u2i64, src);
1091 break;
1092 case GLSL_TYPE_BOOL:
1093 result = new(ctx) ir_expression(ir_unop_b2i64, src);
1094 break;
1095 case GLSL_TYPE_FLOAT16:
1096 result = new(ctx) ir_expression(ir_unop_f162i64, src);
1097 break;
1098 case GLSL_TYPE_FLOAT:
1099 result = new(ctx) ir_expression(ir_unop_f2i64, src);
1100 break;
1101 case GLSL_TYPE_DOUBLE:
1102 result = new(ctx) ir_expression(ir_unop_d2i64, src);
1103 break;
1104 case GLSL_TYPE_UINT64:
1105 result = new(ctx) ir_expression(ir_unop_u642i64, src);
1106 break;
1107 }
1108 break;
1109 case GLSL_TYPE_SAMPLER:
1110 switch (b) {
1111 case GLSL_TYPE_UINT:
1112 result = new(ctx)
1113 ir_expression(ir_unop_pack_sampler_2x32, desired_type, src);
1114 break;
1115 }
1116 break;
1117 case GLSL_TYPE_IMAGE:
1118 switch (b) {
1119 case GLSL_TYPE_UINT:
1120 result = new(ctx)
1121 ir_expression(ir_unop_pack_image_2x32, desired_type, src);
1122 break;
1123 }
1124 break;
1125 }
1126
1127 assert(result != NULL);
1128 assert(result->type == desired_type);
1129
1130 /* Try constant folding; it may fold in the conversion we just added. */
1131 ir_constant *const constant = result->constant_expression_value(ctx);
1132 return (constant != NULL) ? (ir_rvalue *) constant : (ir_rvalue *) result;
1133 }
1134
1135
1136 /**
1137 * Perform automatic type and constant conversion of constructor parameters
1138 *
1139 * This implements the rules in the "Implicit Conversions" rules, not the
1140 * "Conversion and Scalar Constructors".
1141 *
1142 * After attempting the implicit conversion, an attempt to convert into a
1143 * constant valued expression is also done.
1144 *
1145 * The \c from \c ir_rvalue is converted "in place".
1146 *
1147 * \param from Operand that is being converted
1148 * \param to Base type the operand will be converted to
1149 * \param state GLSL compiler state
1150 *
1151 * \return
1152 * If the attempt to convert into a constant expression succeeds, \c true is
1153 * returned. Otherwise \c false is returned.
1154 */
1155 static bool
implicitly_convert_component(ir_rvalue * & from,const glsl_base_type to,struct _mesa_glsl_parse_state * state)1156 implicitly_convert_component(ir_rvalue * &from, const glsl_base_type to,
1157 struct _mesa_glsl_parse_state *state)
1158 {
1159 void *mem_ctx = state;
1160 ir_rvalue *result = from;
1161
1162 if (to != from->type->base_type) {
1163 const glsl_type *desired_type =
1164 glsl_simple_type(to,
1165 from->type->vector_elements,
1166 from->type->matrix_columns);
1167
1168 if (_mesa_glsl_can_implicitly_convert(from->type, desired_type,
1169 state->has_implicit_conversions(),
1170 state->has_implicit_int_to_uint_conversion())) {
1171 /* Even though convert_component() implements the constructor
1172 * conversion rules (not the implicit conversion rules), its safe
1173 * to use it here because we already checked that the implicit
1174 * conversion is legal.
1175 */
1176 result = convert_component(from, desired_type);
1177 }
1178 }
1179
1180 ir_rvalue *const constant = result->constant_expression_value(mem_ctx);
1181
1182 if (constant != NULL)
1183 result = constant;
1184
1185 if (from != result) {
1186 from->replace_with(result);
1187 from = result;
1188 }
1189
1190 return constant != NULL;
1191 }
1192
1193
1194 /**
1195 * Dereference a specific component from a scalar, vector, or matrix
1196 */
1197 static ir_rvalue *
dereference_component(ir_rvalue * src,unsigned component)1198 dereference_component(ir_rvalue *src, unsigned component)
1199 {
1200 void *ctx = ralloc_parent(src);
1201 assert(component < glsl_get_components(src->type));
1202
1203 /* If the source is a constant, just create a new constant instead of a
1204 * dereference of the existing constant.
1205 */
1206 ir_constant *constant = src->as_constant();
1207 if (constant)
1208 return new(ctx) ir_constant(constant, component);
1209
1210 if (glsl_type_is_scalar(src->type)) {
1211 return src;
1212 } else if (glsl_type_is_vector(src->type)) {
1213 return new(ctx) ir_swizzle(src, component, 0, 0, 0, 1);
1214 } else {
1215 assert(glsl_type_is_matrix(src->type));
1216
1217 /* Dereference a row of the matrix, then call this function again to get
1218 * a specific element from that row.
1219 */
1220 const int c = component / glsl_get_column_type(src->type)->vector_elements;
1221 const int r = component % glsl_get_column_type(src->type)->vector_elements;
1222 ir_constant *const col_index = new(ctx) ir_constant(c);
1223 ir_dereference *const col = new(ctx) ir_dereference_array(src,
1224 col_index);
1225
1226 col->type = glsl_get_column_type(src->type);
1227
1228 return dereference_component(col, r);
1229 }
1230
1231 assert(!"Should not get here.");
1232 return NULL;
1233 }
1234
1235
1236 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)1237 process_vec_mat_constructor(exec_list *instructions,
1238 const glsl_type *constructor_type,
1239 YYLTYPE *loc, exec_list *parameters,
1240 struct _mesa_glsl_parse_state *state)
1241 {
1242 void *ctx = state;
1243
1244 /* The ARB_shading_language_420pack spec says:
1245 *
1246 * "If an initializer is a list of initializers enclosed in curly braces,
1247 * the variable being declared must be a vector, a matrix, an array, or a
1248 * structure.
1249 *
1250 * int i = { 1 }; // illegal, i is not an aggregate"
1251 */
1252 if (constructor_type->vector_elements <= 1) {
1253 _mesa_glsl_error(loc, state, "aggregates can only initialize vectors, "
1254 "matrices, arrays, and structs");
1255 return ir_rvalue::error_value(ctx);
1256 }
1257
1258 exec_list actual_parameters;
1259 const unsigned parameter_count =
1260 process_parameters(instructions, &actual_parameters, parameters, state);
1261
1262 if (parameter_count == 0
1263 || (glsl_type_is_vector(constructor_type) &&
1264 constructor_type->vector_elements != parameter_count)
1265 || (glsl_type_is_matrix(constructor_type) &&
1266 constructor_type->matrix_columns != parameter_count)) {
1267 _mesa_glsl_error(loc, state, "%s constructor must have %u parameters",
1268 glsl_type_is_vector(constructor_type) ? "vector" : "matrix",
1269 constructor_type->vector_elements);
1270 return ir_rvalue::error_value(ctx);
1271 }
1272
1273 bool all_parameters_are_constant = true;
1274
1275 /* Type cast each parameter and, if possible, fold constants. */
1276 foreach_in_list_safe(ir_rvalue, ir, &actual_parameters) {
1277 /* Apply implicit conversions (not the scalar constructor rules, see the
1278 * spec quote above!) and attempt to convert the parameter to a constant
1279 * valued expression. After doing so, track whether or not all the
1280 * parameters to the constructor are trivially constant valued
1281 * expressions.
1282 */
1283 all_parameters_are_constant &=
1284 implicitly_convert_component(ir, constructor_type->base_type, state);
1285
1286 if (glsl_type_is_matrix(constructor_type)) {
1287 if (ir->type != glsl_get_column_type(constructor_type)) {
1288 _mesa_glsl_error(loc, state, "type error in matrix constructor: "
1289 "expected: %s, found %s",
1290 glsl_get_type_name(glsl_get_column_type(constructor_type)),
1291 glsl_get_type_name(ir->type));
1292 return ir_rvalue::error_value(ctx);
1293 }
1294 } else if (ir->type != glsl_get_scalar_type(constructor_type)) {
1295 _mesa_glsl_error(loc, state, "type error in vector constructor: "
1296 "expected: %s, found %s",
1297 glsl_get_type_name(glsl_get_scalar_type(constructor_type)),
1298 glsl_get_type_name(ir->type));
1299 return ir_rvalue::error_value(ctx);
1300 }
1301 }
1302
1303 if (all_parameters_are_constant)
1304 return new(ctx) ir_constant(constructor_type, &actual_parameters);
1305
1306 ir_variable *var = new(ctx) ir_variable(constructor_type, "vec_mat_ctor",
1307 ir_var_temporary);
1308 instructions->push_tail(var);
1309
1310 int i = 0;
1311
1312 foreach_in_list(ir_rvalue, rhs, &actual_parameters) {
1313 ir_instruction *assignment = NULL;
1314
1315 if (glsl_type_is_matrix(var->type)) {
1316 ir_rvalue *lhs =
1317 new(ctx) ir_dereference_array(var, new(ctx) ir_constant(i));
1318 assignment = new(ctx) ir_assignment(lhs, rhs);
1319 } else {
1320 /* use writemask rather than index for vector */
1321 assert(glsl_type_is_vector(var->type));
1322 assert(i < 4);
1323 ir_dereference *lhs = new(ctx) ir_dereference_variable(var);
1324 assignment = new(ctx) ir_assignment(lhs, rhs, 1u << i);
1325 }
1326
1327 instructions->push_tail(assignment);
1328
1329 i++;
1330 }
1331
1332 return new(ctx) ir_dereference_variable(var);
1333 }
1334
1335
1336 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)1337 process_array_constructor(exec_list *instructions,
1338 const glsl_type *constructor_type,
1339 YYLTYPE *loc, exec_list *parameters,
1340 struct _mesa_glsl_parse_state *state)
1341 {
1342 void *ctx = state;
1343 /* Array constructors come in two forms: sized and unsized. Sized array
1344 * constructors look like 'vec4[2](a, b)', where 'a' and 'b' are vec4
1345 * variables. In this case the number of parameters must exactly match the
1346 * specified size of the array.
1347 *
1348 * Unsized array constructors look like 'vec4[](a, b)', where 'a' and 'b'
1349 * are vec4 variables. In this case the size of the array being constructed
1350 * is determined by the number of parameters.
1351 *
1352 * From page 52 (page 58 of the PDF) of the GLSL 1.50 spec:
1353 *
1354 * "There must be exactly the same number of arguments as the size of
1355 * the array being constructed. If no size is present in the
1356 * constructor, then the array is explicitly sized to the number of
1357 * arguments provided. The arguments are assigned in order, starting at
1358 * element 0, to the elements of the constructed array. Each argument
1359 * must be the same type as the element type of the array, or be a type
1360 * that can be converted to the element type of the array according to
1361 * Section 4.1.10 "Implicit Conversions.""
1362 */
1363 exec_list actual_parameters;
1364 const unsigned parameter_count =
1365 process_parameters(instructions, &actual_parameters, parameters, state);
1366 bool is_unsized_array = glsl_type_is_unsized_array(constructor_type);
1367
1368 if ((parameter_count == 0) ||
1369 (!is_unsized_array && (constructor_type->length != parameter_count))) {
1370 const unsigned min_param = is_unsized_array
1371 ? 1 : constructor_type->length;
1372
1373 _mesa_glsl_error(loc, state, "array constructor must have %s %u "
1374 "parameter%s",
1375 is_unsized_array ? "at least" : "exactly",
1376 min_param, (min_param <= 1) ? "" : "s");
1377 return ir_rvalue::error_value(ctx);
1378 }
1379
1380 if (is_unsized_array) {
1381 constructor_type =
1382 glsl_array_type(constructor_type->fields.array,
1383 parameter_count, 0);
1384 assert(constructor_type != NULL);
1385 assert(constructor_type->length == parameter_count);
1386 }
1387
1388 bool all_parameters_are_constant = true;
1389 const glsl_type *element_type = constructor_type->fields.array;
1390
1391 /* Type cast each parameter and, if possible, fold constants. */
1392 foreach_in_list_safe(ir_rvalue, ir, &actual_parameters) {
1393 /* Apply implicit conversions (not the scalar constructor rules, see the
1394 * spec quote above!) and attempt to convert the parameter to a constant
1395 * valued expression. After doing so, track whether or not all the
1396 * parameters to the constructor are trivially constant valued
1397 * expressions.
1398 */
1399 all_parameters_are_constant &=
1400 implicitly_convert_component(ir, element_type->base_type, state);
1401
1402 if (glsl_type_is_unsized_array(constructor_type->fields.array)) {
1403 /* As the inner parameters of the constructor are created without
1404 * knowledge of each other we need to check to make sure unsized
1405 * parameters of unsized constructors all end up with the same size.
1406 *
1407 * e.g we make sure to fail for a constructor like this:
1408 * vec4[][] a = vec4[][](vec4[](vec4(0.0), vec4(1.0)),
1409 * vec4[](vec4(0.0), vec4(1.0), vec4(1.0)),
1410 * vec4[](vec4(0.0), vec4(1.0)));
1411 */
1412 if (glsl_type_is_unsized_array(element_type)) {
1413 /* This is the first parameter so just get the type */
1414 element_type = ir->type;
1415 } else if (element_type != ir->type) {
1416 _mesa_glsl_error(loc, state, "type error in array constructor: "
1417 "expected: %s, found %s",
1418 glsl_get_type_name(element_type),
1419 glsl_get_type_name(ir->type));
1420 return ir_rvalue::error_value(ctx);
1421 }
1422 } else if (ir->type != constructor_type->fields.array) {
1423 _mesa_glsl_error(loc, state, "type error in array constructor: "
1424 "expected: %s, found %s",
1425 glsl_get_type_name(constructor_type->fields.array),
1426 glsl_get_type_name(ir->type));
1427 return ir_rvalue::error_value(ctx);
1428 } else {
1429 element_type = ir->type;
1430 }
1431 }
1432
1433 if (glsl_type_is_unsized_array(constructor_type->fields.array)) {
1434 constructor_type =
1435 glsl_array_type(element_type, parameter_count, 0);
1436 assert(constructor_type != NULL);
1437 assert(constructor_type->length == parameter_count);
1438 }
1439
1440 if (all_parameters_are_constant)
1441 return new(ctx) ir_constant(constructor_type, &actual_parameters);
1442
1443 ir_variable *var = new(ctx) ir_variable(constructor_type, "array_ctor",
1444 ir_var_temporary);
1445 instructions->push_tail(var);
1446
1447 int i = 0;
1448 foreach_in_list(ir_rvalue, rhs, &actual_parameters) {
1449 ir_rvalue *lhs = new(ctx) ir_dereference_array(var,
1450 new(ctx) ir_constant(i));
1451
1452 ir_instruction *assignment = new(ctx) ir_assignment(lhs, rhs);
1453 instructions->push_tail(assignment);
1454
1455 i++;
1456 }
1457
1458 return new(ctx) ir_dereference_variable(var);
1459 }
1460
1461
1462 /**
1463 * Determine if a list consists of a single scalar r-value
1464 */
1465 static bool
single_scalar_parameter(exec_list * parameters)1466 single_scalar_parameter(exec_list *parameters)
1467 {
1468 const ir_rvalue *const p = (ir_rvalue *) parameters->get_head_raw();
1469 assert(((ir_rvalue *)p)->as_rvalue() != NULL);
1470
1471 return (glsl_type_is_scalar(p->type) && p->next->is_tail_sentinel());
1472 }
1473
1474
1475 /**
1476 * Generate inline code for a vector constructor
1477 *
1478 * The generated constructor code will consist of a temporary variable
1479 * declaration of the same type as the constructor. A sequence of assignments
1480 * from constructor parameters to the temporary will follow.
1481 *
1482 * \return
1483 * An \c ir_dereference_variable of the temprorary generated in the constructor
1484 * body.
1485 */
1486 static ir_rvalue *
emit_inline_vector_constructor(const glsl_type * type,exec_list * instructions,exec_list * parameters,void * ctx)1487 emit_inline_vector_constructor(const glsl_type *type,
1488 exec_list *instructions,
1489 exec_list *parameters,
1490 void *ctx)
1491 {
1492 assert(!parameters->is_empty());
1493
1494 ir_variable *var = new(ctx) ir_variable(type, "vec_ctor", ir_var_temporary);
1495 instructions->push_tail(var);
1496
1497 /* There are three kinds of vector constructors.
1498 *
1499 * - Construct a vector from a single scalar by replicating that scalar to
1500 * all components of the vector.
1501 *
1502 * - Construct a vector from at least a matrix. This case should already
1503 * have been taken care of in ast_function_expression::hir by breaking
1504 * down the matrix into a series of column vectors.
1505 *
1506 * - Construct a vector from an arbirary combination of vectors and
1507 * scalars. The components of the constructor parameters are assigned
1508 * to the vector in order until the vector is full.
1509 */
1510 const unsigned lhs_components = glsl_get_components(type);
1511 if (single_scalar_parameter(parameters)) {
1512 ir_rvalue *first_param = (ir_rvalue *)parameters->get_head_raw();
1513 return new(ctx) ir_swizzle(first_param, 0, 0, 0, 0, lhs_components);
1514 } else {
1515 unsigned base_component = 0;
1516 unsigned base_lhs_component = 0;
1517 ir_constant_data data;
1518 unsigned constant_mask = 0, constant_components = 0;
1519
1520 memset(&data, 0, sizeof(data));
1521
1522 foreach_in_list(ir_rvalue, param, parameters) {
1523 unsigned rhs_components = glsl_get_components(param->type);
1524
1525 /* Do not try to assign more components to the vector than it has! */
1526 if ((rhs_components + base_lhs_component) > lhs_components) {
1527 rhs_components = lhs_components - base_lhs_component;
1528 }
1529
1530 const ir_constant *const c = param->as_constant();
1531 if (c != NULL) {
1532 for (unsigned i = 0; i < rhs_components; i++) {
1533 switch (c->type->base_type) {
1534 case GLSL_TYPE_UINT:
1535 data.u[i + base_component] = c->get_uint_component(i);
1536 break;
1537 case GLSL_TYPE_INT:
1538 data.i[i + base_component] = c->get_int_component(i);
1539 break;
1540 case GLSL_TYPE_FLOAT:
1541 data.f[i + base_component] = c->get_float_component(i);
1542 break;
1543 case GLSL_TYPE_DOUBLE:
1544 data.d[i + base_component] = c->get_double_component(i);
1545 break;
1546 case GLSL_TYPE_BOOL:
1547 data.b[i + base_component] = c->get_bool_component(i);
1548 break;
1549 case GLSL_TYPE_UINT64:
1550 data.u64[i + base_component] = c->get_uint64_component(i);
1551 break;
1552 case GLSL_TYPE_INT64:
1553 data.i64[i + base_component] = c->get_int64_component(i);
1554 break;
1555 default:
1556 assert(!"Should not get here.");
1557 break;
1558 }
1559 }
1560
1561 /* Mask of fields to be written in the assignment. */
1562 constant_mask |= ((1U << rhs_components) - 1) << base_lhs_component;
1563 constant_components += rhs_components;
1564
1565 base_component += rhs_components;
1566 }
1567 /* Advance the component index by the number of components
1568 * that were just assigned.
1569 */
1570 base_lhs_component += rhs_components;
1571 }
1572
1573 if (constant_mask != 0) {
1574 ir_dereference *lhs = new(ctx) ir_dereference_variable(var);
1575 const glsl_type *rhs_type =
1576 glsl_simple_type(var->type->base_type, constant_components, 1);
1577 ir_rvalue *rhs = new(ctx) ir_constant(rhs_type, &data);
1578
1579 ir_instruction *inst =
1580 new(ctx) ir_assignment(lhs, rhs, constant_mask);
1581 instructions->push_tail(inst);
1582 }
1583
1584 base_component = 0;
1585 foreach_in_list(ir_rvalue, param, parameters) {
1586 unsigned rhs_components = glsl_get_components(param->type);
1587
1588 /* Do not try to assign more components to the vector than it has! */
1589 if ((rhs_components + base_component) > lhs_components) {
1590 rhs_components = lhs_components - base_component;
1591 }
1592
1593 /* If we do not have any components left to copy, break out of the
1594 * loop. This can happen when initializing a vec4 with a mat3 as the
1595 * mat3 would have been broken into a series of column vectors.
1596 */
1597 if (rhs_components == 0) {
1598 break;
1599 }
1600
1601 const ir_constant *const c = param->as_constant();
1602 if (c == NULL) {
1603 /* Mask of fields to be written in the assignment. */
1604 const unsigned write_mask = ((1U << rhs_components) - 1)
1605 << base_component;
1606
1607 ir_dereference *lhs = new(ctx) ir_dereference_variable(var);
1608
1609 /* Generate a swizzle so that LHS and RHS sizes match. */
1610 ir_rvalue *rhs =
1611 new(ctx) ir_swizzle(param, 0, 1, 2, 3, rhs_components);
1612
1613 ir_instruction *inst =
1614 new(ctx) ir_assignment(lhs, rhs, write_mask);
1615 instructions->push_tail(inst);
1616 }
1617
1618 /* Advance the component index by the number of components that were
1619 * just assigned.
1620 */
1621 base_component += rhs_components;
1622 }
1623 }
1624 return new(ctx) ir_dereference_variable(var);
1625 }
1626
1627
1628 /**
1629 * Generate assignment of a portion of a vector to a portion of a matrix column
1630 *
1631 * \param src_base First component of the source to be used in assignment
1632 * \param column Column of destination to be assiged
1633 * \param row_base First component of the destination column to be assigned
1634 * \param count Number of components to be assigned
1635 *
1636 * \note
1637 * \c src_base + \c count must be less than or equal to the number of
1638 * components in the source vector.
1639 */
1640 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)1641 assign_to_matrix_column(ir_variable *var, unsigned column, unsigned row_base,
1642 ir_rvalue *src, unsigned src_base, unsigned count,
1643 void *mem_ctx)
1644 {
1645 ir_constant *col_idx = new(mem_ctx) ir_constant(column);
1646 ir_dereference *column_ref = new(mem_ctx) ir_dereference_array(var,
1647 col_idx);
1648
1649 assert(glsl_get_components(column_ref->type) >= (row_base + count));
1650 assert(glsl_get_components(src->type) >= (src_base + count));
1651
1652 /* Generate a swizzle that extracts the number of components from the source
1653 * that are to be assigned to the column of the matrix.
1654 */
1655 if (count < src->type->vector_elements) {
1656 src = new(mem_ctx) ir_swizzle(src,
1657 src_base + 0, src_base + 1,
1658 src_base + 2, src_base + 3,
1659 count);
1660 }
1661
1662 /* Mask of fields to be written in the assignment. */
1663 const unsigned write_mask = ((1U << count) - 1) << row_base;
1664
1665 return new(mem_ctx) ir_assignment(column_ref, src, write_mask);
1666 }
1667
1668
1669 /**
1670 * Generate inline code for a matrix constructor
1671 *
1672 * The generated constructor code will consist of a temporary variable
1673 * declaration of the same type as the constructor. A sequence of assignments
1674 * from constructor parameters to the temporary will follow.
1675 *
1676 * \return
1677 * An \c ir_dereference_variable of the temprorary generated in the constructor
1678 * body.
1679 */
1680 static ir_rvalue *
emit_inline_matrix_constructor(const glsl_type * type,exec_list * instructions,exec_list * parameters,void * ctx)1681 emit_inline_matrix_constructor(const glsl_type *type,
1682 exec_list *instructions,
1683 exec_list *parameters,
1684 void *ctx)
1685 {
1686 assert(!parameters->is_empty());
1687
1688 ir_variable *var = new(ctx) ir_variable(type, "mat_ctor", ir_var_temporary);
1689 instructions->push_tail(var);
1690
1691 /* There are three kinds of matrix constructors.
1692 *
1693 * - Construct a matrix from a single scalar by replicating that scalar to
1694 * along the diagonal of the matrix and setting all other components to
1695 * zero.
1696 *
1697 * - Construct a matrix from an arbirary combination of vectors and
1698 * scalars. The components of the constructor parameters are assigned
1699 * to the matrix in column-major order until the matrix is full.
1700 *
1701 * - Construct a matrix from a single matrix. The source matrix is copied
1702 * to the upper left portion of the constructed matrix, and the remaining
1703 * elements take values from the identity matrix.
1704 */
1705 ir_rvalue *const first_param = (ir_rvalue *) parameters->get_head_raw();
1706 if (single_scalar_parameter(parameters)) {
1707 /* Assign the scalar to the X component of a vec4, and fill the remaining
1708 * components with zero.
1709 */
1710 glsl_base_type param_base_type = first_param->type->base_type;
1711 assert(glsl_type_is_float_16_32_64(first_param->type));
1712 ir_variable *rhs_var =
1713 new(ctx) ir_variable(glsl_simple_type(param_base_type, 4, 1),
1714 "mat_ctor_vec",
1715 ir_var_temporary);
1716 instructions->push_tail(rhs_var);
1717
1718 ir_constant_data zero;
1719 for (unsigned i = 0; i < 4; i++)
1720 if (glsl_type_is_float(first_param->type))
1721 zero.f[i] = 0.0;
1722 else
1723 zero.d[i] = 0.0;
1724
1725 ir_instruction *inst =
1726 new(ctx) ir_assignment(new(ctx) ir_dereference_variable(rhs_var),
1727 new(ctx) ir_constant(rhs_var->type, &zero));
1728 instructions->push_tail(inst);
1729
1730 ir_dereference *const rhs_ref =
1731 new(ctx) ir_dereference_variable(rhs_var);
1732
1733 inst = new(ctx) ir_assignment(rhs_ref, first_param, 0x01);
1734 instructions->push_tail(inst);
1735
1736 /* Assign the temporary vector to each column of the destination matrix
1737 * with a swizzle that puts the X component on the diagonal of the
1738 * matrix. In some cases this may mean that the X component does not
1739 * get assigned into the column at all (i.e., when the matrix has more
1740 * columns than rows).
1741 */
1742 static const unsigned rhs_swiz[4][4] = {
1743 { 0, 1, 1, 1 },
1744 { 1, 0, 1, 1 },
1745 { 1, 1, 0, 1 },
1746 { 1, 1, 1, 0 }
1747 };
1748
1749 const unsigned cols_to_init = MIN2(type->matrix_columns,
1750 type->vector_elements);
1751 for (unsigned i = 0; i < cols_to_init; i++) {
1752 ir_constant *const col_idx = new(ctx) ir_constant(i);
1753 ir_rvalue *const col_ref = new(ctx) ir_dereference_array(var,
1754 col_idx);
1755
1756 ir_rvalue *const rhs_ref = new(ctx) ir_dereference_variable(rhs_var);
1757 ir_rvalue *const rhs = new(ctx) ir_swizzle(rhs_ref, rhs_swiz[i],
1758 type->vector_elements);
1759
1760 inst = new(ctx) ir_assignment(col_ref, rhs);
1761 instructions->push_tail(inst);
1762 }
1763
1764 for (unsigned i = cols_to_init; i < type->matrix_columns; i++) {
1765 ir_constant *const col_idx = new(ctx) ir_constant(i);
1766 ir_rvalue *const col_ref = new(ctx) ir_dereference_array(var,
1767 col_idx);
1768
1769 ir_rvalue *const rhs_ref = new(ctx) ir_dereference_variable(rhs_var);
1770 ir_rvalue *const rhs = new(ctx) ir_swizzle(rhs_ref, 1, 1, 1, 1,
1771 type->vector_elements);
1772
1773 inst = new(ctx) ir_assignment(col_ref, rhs);
1774 instructions->push_tail(inst);
1775 }
1776 } else if (glsl_type_is_matrix(first_param->type)) {
1777 /* From page 50 (56 of the PDF) of the GLSL 1.50 spec:
1778 *
1779 * "If a matrix is constructed from a matrix, then each component
1780 * (column i, row j) in the result that has a corresponding
1781 * component (column i, row j) in the argument will be initialized
1782 * from there. All other components will be initialized to the
1783 * identity matrix. If a matrix argument is given to a matrix
1784 * constructor, it is an error to have any other arguments."
1785 */
1786 assert(first_param->next->is_tail_sentinel());
1787 ir_rvalue *const src_matrix = first_param;
1788
1789 /* If the source matrix is smaller, pre-initialize the relavent parts of
1790 * the destination matrix to the identity matrix.
1791 */
1792 if ((src_matrix->type->matrix_columns < var->type->matrix_columns) ||
1793 (src_matrix->type->vector_elements < var->type->vector_elements)) {
1794
1795 /* If the source matrix has fewer rows, every column of the
1796 * destination must be initialized. Otherwise only the columns in
1797 * the destination that do not exist in the source must be
1798 * initialized.
1799 */
1800 unsigned col =
1801 (src_matrix->type->vector_elements < var->type->vector_elements)
1802 ? 0 : src_matrix->type->matrix_columns;
1803
1804 const glsl_type *const col_type = glsl_get_column_type(var->type);
1805 for (/* empty */; col < var->type->matrix_columns; col++) {
1806 ir_constant_data ident;
1807
1808 if (!glsl_type_is_double(col_type)) {
1809 ident.f[0] = 0.0f;
1810 ident.f[1] = 0.0f;
1811 ident.f[2] = 0.0f;
1812 ident.f[3] = 0.0f;
1813 ident.f[col] = 1.0f;
1814 } else {
1815 ident.d[0] = 0.0;
1816 ident.d[1] = 0.0;
1817 ident.d[2] = 0.0;
1818 ident.d[3] = 0.0;
1819 ident.d[col] = 1.0;
1820 }
1821
1822 ir_rvalue *const rhs = new(ctx) ir_constant(col_type, &ident);
1823
1824 ir_rvalue *const lhs =
1825 new(ctx) ir_dereference_array(var, new(ctx) ir_constant(col));
1826
1827 ir_instruction *inst = new(ctx) ir_assignment(lhs, rhs);
1828 instructions->push_tail(inst);
1829 }
1830 }
1831
1832 /* Assign columns from the source matrix to the destination matrix.
1833 *
1834 * Since the parameter will be used in the RHS of multiple assignments,
1835 * generate a temporary and copy the paramter there.
1836 */
1837 ir_variable *const rhs_var =
1838 new(ctx) ir_variable(first_param->type, "mat_ctor_mat",
1839 ir_var_temporary);
1840 instructions->push_tail(rhs_var);
1841
1842 ir_dereference *const rhs_var_ref =
1843 new(ctx) ir_dereference_variable(rhs_var);
1844 ir_instruction *const inst =
1845 new(ctx) ir_assignment(rhs_var_ref, first_param);
1846 instructions->push_tail(inst);
1847
1848 const unsigned last_row = MIN2(src_matrix->type->vector_elements,
1849 var->type->vector_elements);
1850 const unsigned last_col = MIN2(src_matrix->type->matrix_columns,
1851 var->type->matrix_columns);
1852
1853 unsigned swiz[4] = { 0, 0, 0, 0 };
1854 for (unsigned i = 1; i < last_row; i++)
1855 swiz[i] = i;
1856
1857 const unsigned write_mask = (1U << last_row) - 1;
1858
1859 for (unsigned i = 0; i < last_col; i++) {
1860 ir_dereference *const lhs =
1861 new(ctx) ir_dereference_array(var, new(ctx) ir_constant(i));
1862 ir_rvalue *const rhs_col =
1863 new(ctx) ir_dereference_array(rhs_var, new(ctx) ir_constant(i));
1864
1865 /* If one matrix has columns that are smaller than the columns of the
1866 * other matrix, wrap the column access of the larger with a swizzle
1867 * so that the LHS and RHS of the assignment have the same size (and
1868 * therefore have the same type).
1869 *
1870 * It would be perfectly valid to unconditionally generate the
1871 * swizzles, this this will typically result in a more compact IR
1872 * tree.
1873 */
1874 ir_rvalue *rhs;
1875 if (lhs->type->vector_elements != rhs_col->type->vector_elements) {
1876 rhs = new(ctx) ir_swizzle(rhs_col, swiz, last_row);
1877 } else {
1878 rhs = rhs_col;
1879 }
1880
1881 ir_instruction *inst =
1882 new(ctx) ir_assignment(lhs, rhs, write_mask);
1883 instructions->push_tail(inst);
1884 }
1885 } else {
1886 const unsigned cols = type->matrix_columns;
1887 const unsigned rows = type->vector_elements;
1888 unsigned remaining_slots = rows * cols;
1889 unsigned col_idx = 0;
1890 unsigned row_idx = 0;
1891
1892 foreach_in_list(ir_rvalue, rhs, parameters) {
1893 unsigned rhs_components = glsl_get_components(rhs->type);
1894 unsigned rhs_base = 0;
1895
1896 if (remaining_slots == 0)
1897 break;
1898
1899 /* Since the parameter might be used in the RHS of two assignments,
1900 * generate a temporary and copy the paramter there.
1901 */
1902 ir_variable *rhs_var =
1903 new(ctx) ir_variable(rhs->type, "mat_ctor_vec", ir_var_temporary);
1904 instructions->push_tail(rhs_var);
1905
1906 ir_dereference *rhs_var_ref =
1907 new(ctx) ir_dereference_variable(rhs_var);
1908 ir_instruction *inst = new(ctx) ir_assignment(rhs_var_ref, rhs);
1909 instructions->push_tail(inst);
1910
1911 do {
1912 /* Assign the current parameter to as many components of the matrix
1913 * as it will fill.
1914 *
1915 * NOTE: A single vector parameter can span two matrix columns. A
1916 * single vec4, for example, can completely fill a mat2.
1917 */
1918 unsigned count = MIN2(rows - row_idx,
1919 rhs_components - rhs_base);
1920
1921 rhs_var_ref = new(ctx) ir_dereference_variable(rhs_var);
1922 ir_instruction *inst = assign_to_matrix_column(var, col_idx,
1923 row_idx,
1924 rhs_var_ref,
1925 rhs_base,
1926 count, ctx);
1927 instructions->push_tail(inst);
1928 rhs_base += count;
1929 row_idx += count;
1930 remaining_slots -= count;
1931
1932 /* Sometimes, there is still data left in the parameters and
1933 * components left to be set in the destination but in other
1934 * column.
1935 */
1936 if (row_idx >= rows) {
1937 row_idx = 0;
1938 col_idx++;
1939 }
1940 } while(remaining_slots > 0 && rhs_base < rhs_components);
1941 }
1942 }
1943
1944 return new(ctx) ir_dereference_variable(var);
1945 }
1946
1947
1948 static ir_rvalue *
emit_inline_record_constructor(const glsl_type * type,exec_list * instructions,exec_list * parameters,void * mem_ctx)1949 emit_inline_record_constructor(const glsl_type *type,
1950 exec_list *instructions,
1951 exec_list *parameters,
1952 void *mem_ctx)
1953 {
1954 ir_variable *const var =
1955 new(mem_ctx) ir_variable(type, "record_ctor", ir_var_temporary);
1956 ir_dereference_variable *const d =
1957 new(mem_ctx) ir_dereference_variable(var);
1958
1959 instructions->push_tail(var);
1960
1961 exec_node *node = parameters->get_head_raw();
1962 for (unsigned i = 0; i < type->length; i++) {
1963 assert(!node->is_tail_sentinel());
1964
1965 ir_dereference *const lhs =
1966 new(mem_ctx) ir_dereference_record(d->clone(mem_ctx, NULL),
1967 type->fields.structure[i].name);
1968
1969 ir_rvalue *const rhs = ((ir_instruction *) node)->as_rvalue();
1970 assert(rhs != NULL);
1971
1972 ir_instruction *const assign = new(mem_ctx) ir_assignment(lhs, rhs);
1973
1974 instructions->push_tail(assign);
1975 node = node->next;
1976 }
1977
1978 return d;
1979 }
1980
1981
1982 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)1983 process_record_constructor(exec_list *instructions,
1984 const glsl_type *constructor_type,
1985 YYLTYPE *loc, exec_list *parameters,
1986 struct _mesa_glsl_parse_state *state)
1987 {
1988 void *ctx = state;
1989 /* From page 32 (page 38 of the PDF) of the GLSL 1.20 spec:
1990 *
1991 * "The arguments to the constructor will be used to set the structure's
1992 * fields, in order, using one argument per field. Each argument must
1993 * be the same type as the field it sets, or be a type that can be
1994 * converted to the field's type according to Section 4.1.10 “Implicit
1995 * Conversions.”"
1996 *
1997 * From page 35 (page 41 of the PDF) of the GLSL 4.20 spec:
1998 *
1999 * "In all cases, the innermost initializer (i.e., not a list of
2000 * initializers enclosed in curly braces) applied to an object must
2001 * have the same type as the object being initialized or be a type that
2002 * can be converted to the object's type according to section 4.1.10
2003 * "Implicit Conversions". In the latter case, an implicit conversion
2004 * will be done on the initializer before the assignment is done."
2005 */
2006 exec_list actual_parameters;
2007
2008 const unsigned parameter_count =
2009 process_parameters(instructions, &actual_parameters, parameters,
2010 state);
2011
2012 if (parameter_count != constructor_type->length) {
2013 _mesa_glsl_error(loc, state,
2014 "%s parameters in constructor for `%s'",
2015 parameter_count > constructor_type->length
2016 ? "too many": "insufficient",
2017 glsl_get_type_name(constructor_type));
2018 return ir_rvalue::error_value(ctx);
2019 }
2020
2021 bool all_parameters_are_constant = true;
2022
2023 int i = 0;
2024 /* Type cast each parameter and, if possible, fold constants. */
2025 foreach_in_list_safe(ir_rvalue, ir, &actual_parameters) {
2026
2027 const glsl_struct_field *struct_field =
2028 &constructor_type->fields.structure[i];
2029
2030 /* Apply implicit conversions (not the scalar constructor rules, see the
2031 * spec quote above!) and attempt to convert the parameter to a constant
2032 * valued expression. After doing so, track whether or not all the
2033 * parameters to the constructor are trivially constant valued
2034 * expressions.
2035 */
2036 all_parameters_are_constant &=
2037 implicitly_convert_component(ir, struct_field->type->base_type,
2038 state);
2039
2040 if (ir->type != struct_field->type) {
2041 _mesa_glsl_error(loc, state,
2042 "parameter type mismatch in constructor for `%s.%s' "
2043 "(%s vs %s)",
2044 glsl_get_type_name(constructor_type),
2045 struct_field->name,
2046 glsl_get_type_name(ir->type),
2047 glsl_get_type_name(struct_field->type));
2048 return ir_rvalue::error_value(ctx);
2049 }
2050
2051 i++;
2052 }
2053
2054 if (all_parameters_are_constant) {
2055 return new(ctx) ir_constant(constructor_type, &actual_parameters);
2056 } else {
2057 return emit_inline_record_constructor(constructor_type, instructions,
2058 &actual_parameters, state);
2059 }
2060 }
2061
2062 ir_rvalue *
handle_method(exec_list * instructions,struct _mesa_glsl_parse_state * state)2063 ast_function_expression::handle_method(exec_list *instructions,
2064 struct _mesa_glsl_parse_state *state)
2065 {
2066 const ast_expression *field = subexpressions[0];
2067 ir_rvalue *op;
2068 ir_rvalue *result;
2069 void *ctx = state;
2070 /* Handle "method calls" in GLSL 1.20 - namely, array.length() */
2071 YYLTYPE loc = get_location();
2072 state->check_version(120, 300, &loc, "methods not supported");
2073
2074 const char *method;
2075 method = field->primary_expression.identifier;
2076
2077 /* This would prevent to raise "uninitialized variable" warnings when
2078 * calling array.length.
2079 */
2080 field->subexpressions[0]->set_is_lhs(true);
2081 op = field->subexpressions[0]->hir(instructions, state);
2082 if (strcmp(method, "length") == 0) {
2083 if (!this->expressions.is_empty()) {
2084 _mesa_glsl_error(&loc, state, "length method takes no arguments");
2085 goto fail;
2086 }
2087
2088 if (glsl_type_is_array(op->type)) {
2089 if (glsl_type_is_unsized_array(op->type)) {
2090 if (!state->has_shader_storage_buffer_objects()) {
2091 _mesa_glsl_error(&loc, state,
2092 "length called on unsized array"
2093 " only available with"
2094 " ARB_shader_storage_buffer_object");
2095 goto fail;
2096 } else if (op->variable_referenced()->is_in_shader_storage_block()) {
2097 /* Calculate length of an unsized array in run-time */
2098 result = new(ctx)
2099 ir_expression(ir_unop_ssbo_unsized_array_length, op);
2100 } else {
2101 /* When actual size is known at link-time, this will be
2102 * replaced with a constant expression.
2103 */
2104 result = new (ctx)
2105 ir_expression(ir_unop_implicitly_sized_array_length, op);
2106 }
2107 } else {
2108 result = new(ctx) ir_constant(glsl_array_size(op->type));
2109 }
2110 } else if (glsl_type_is_vector(op->type)) {
2111 if (state->has_420pack()) {
2112 /* .length() returns int. */
2113 result = new(ctx) ir_constant((int) op->type->vector_elements);
2114 } else {
2115 _mesa_glsl_error(&loc, state, "length method on matrix only"
2116 " available with ARB_shading_language_420pack");
2117 goto fail;
2118 }
2119 } else if (glsl_type_is_matrix(op->type)) {
2120 if (state->has_420pack()) {
2121 /* .length() returns int. */
2122 result = new(ctx) ir_constant((int) op->type->matrix_columns);
2123 } else {
2124 _mesa_glsl_error(&loc, state, "length method on matrix only"
2125 " available with ARB_shading_language_420pack");
2126 goto fail;
2127 }
2128 } else {
2129 _mesa_glsl_error(&loc, state, "length called on scalar.");
2130 goto fail;
2131 }
2132 } else {
2133 _mesa_glsl_error(&loc, state, "unknown method: `%s'", method);
2134 goto fail;
2135 }
2136 return result;
2137 fail:
2138 return ir_rvalue::error_value(ctx);
2139 }
2140
is_valid_constructor(const glsl_type * type,struct _mesa_glsl_parse_state * state)2141 static inline bool is_valid_constructor(const glsl_type *type,
2142 struct _mesa_glsl_parse_state *state)
2143 {
2144 return glsl_type_is_numeric(type) || glsl_type_is_boolean(type) ||
2145 (state->has_bindless() && (glsl_type_is_sampler(type) || glsl_type_is_image(type)));
2146 }
2147
2148 ir_rvalue *
hir(exec_list * instructions,struct _mesa_glsl_parse_state * state)2149 ast_function_expression::hir(exec_list *instructions,
2150 struct _mesa_glsl_parse_state *state)
2151 {
2152 void *ctx = state;
2153 /* There are three sorts of function calls.
2154 *
2155 * 1. constructors - The first subexpression is an ast_type_specifier.
2156 * 2. methods - Only the .length() method of array types.
2157 * 3. functions - Calls to regular old functions.
2158 *
2159 */
2160 if (is_constructor()) {
2161 const ast_type_specifier *type =
2162 (ast_type_specifier *) subexpressions[0];
2163 YYLTYPE loc = type->get_location();
2164 const char *name;
2165
2166 const glsl_type *const constructor_type = type->glsl_type(& name, state);
2167
2168 /* constructor_type can be NULL if a variable with the same name as the
2169 * structure has come into scope.
2170 */
2171 if (constructor_type == NULL) {
2172 _mesa_glsl_error(& loc, state, "unknown type `%s' (structure name "
2173 "may be shadowed by a variable with the same name)",
2174 type->type_name);
2175 return ir_rvalue::error_value(ctx);
2176 }
2177
2178
2179 /* Constructors for opaque types are illegal.
2180 *
2181 * From section 4.1.7 of the ARB_bindless_texture spec:
2182 *
2183 * "Samplers are represented using 64-bit integer handles, and may be "
2184 * converted to and from 64-bit integers using constructors."
2185 *
2186 * From section 4.1.X of the ARB_bindless_texture spec:
2187 *
2188 * "Images are represented using 64-bit integer handles, and may be
2189 * converted to and from 64-bit integers using constructors."
2190 */
2191 if (glsl_contains_atomic(constructor_type) ||
2192 (!state->has_bindless() && glsl_contains_opaque(constructor_type))) {
2193 _mesa_glsl_error(& loc, state, "cannot construct %s type `%s'",
2194 state->has_bindless() ? "atomic" : "opaque",
2195 glsl_get_type_name(constructor_type));
2196 return ir_rvalue::error_value(ctx);
2197 }
2198
2199 if (glsl_type_is_subroutine(constructor_type)) {
2200 _mesa_glsl_error(& loc, state,
2201 "subroutine name cannot be a constructor `%s'",
2202 glsl_get_type_name(constructor_type));
2203 return ir_rvalue::error_value(ctx);
2204 }
2205
2206 if (glsl_type_is_array(constructor_type)) {
2207 if (!state->check_version(state->allow_glsl_120_subset_in_110 ? 110 : 120,
2208 300, &loc, "array constructors forbidden")) {
2209 return ir_rvalue::error_value(ctx);
2210 }
2211
2212 return process_array_constructor(instructions, constructor_type,
2213 & loc, &this->expressions, state);
2214 }
2215
2216
2217 /* There are two kinds of constructor calls. Constructors for arrays and
2218 * structures must have the exact number of arguments with matching types
2219 * in the correct order. These constructors follow essentially the same
2220 * type matching rules as functions.
2221 *
2222 * Constructors for built-in language types, such as mat4 and vec2, are
2223 * free form. The only requirements are that the parameters must provide
2224 * enough values of the correct scalar type and that no arguments are
2225 * given past the last used argument.
2226 *
2227 * When using the C-style initializer syntax from GLSL 4.20, constructors
2228 * must have the exact number of arguments with matching types in the
2229 * correct order.
2230 */
2231 if (glsl_type_is_struct(constructor_type)) {
2232 return process_record_constructor(instructions, constructor_type,
2233 &loc, &this->expressions,
2234 state);
2235 }
2236
2237 if (!is_valid_constructor(constructor_type, state))
2238 return ir_rvalue::error_value(ctx);
2239
2240 /* Total number of components of the type being constructed. */
2241 const unsigned type_components = glsl_get_components(constructor_type);
2242
2243 /* Number of components from parameters that have actually been
2244 * consumed. This is used to perform several kinds of error checking.
2245 */
2246 unsigned components_used = 0;
2247
2248 unsigned matrix_parameters = 0;
2249 unsigned nonmatrix_parameters = 0;
2250 exec_list actual_parameters;
2251
2252 foreach_list_typed(ast_node, ast, link, &this->expressions) {
2253 ir_rvalue *result = ast->hir(instructions, state);
2254
2255 /* From page 50 (page 56 of the PDF) of the GLSL 1.50 spec:
2256 *
2257 * "It is an error to provide extra arguments beyond this
2258 * last used argument."
2259 */
2260 if (components_used >= type_components) {
2261 _mesa_glsl_error(& loc, state, "too many parameters to `%s' "
2262 "constructor",
2263 glsl_get_type_name(constructor_type));
2264 return ir_rvalue::error_value(ctx);
2265 }
2266
2267 if (!is_valid_constructor(result->type, state)) {
2268 _mesa_glsl_error(& loc, state, "cannot construct `%s' from a "
2269 "non-numeric data type",
2270 glsl_get_type_name(constructor_type));
2271 return ir_rvalue::error_value(ctx);
2272 }
2273
2274 /* Count the number of matrix and nonmatrix parameters. This
2275 * is used below to enforce some of the constructor rules.
2276 */
2277 if (glsl_type_is_matrix(result->type))
2278 matrix_parameters++;
2279 else
2280 nonmatrix_parameters++;
2281
2282 actual_parameters.push_tail(result);
2283 components_used += glsl_get_components(result->type);
2284 }
2285
2286 /* From page 28 (page 34 of the PDF) of the GLSL 1.10 spec:
2287 *
2288 * "It is an error to construct matrices from other matrices. This
2289 * is reserved for future use."
2290 */
2291 if (matrix_parameters > 0
2292 && glsl_type_is_matrix(constructor_type)
2293 && !state->check_version(120, 100, &loc,
2294 "cannot construct `%s' from a matrix",
2295 glsl_get_type_name(constructor_type))) {
2296 return ir_rvalue::error_value(ctx);
2297 }
2298
2299 /* From page 50 (page 56 of the PDF) of the GLSL 1.50 spec:
2300 *
2301 * "If a matrix argument is given to a matrix constructor, it is
2302 * an error to have any other arguments."
2303 */
2304 if ((matrix_parameters > 0)
2305 && ((matrix_parameters + nonmatrix_parameters) > 1)
2306 && glsl_type_is_matrix(constructor_type)) {
2307 _mesa_glsl_error(& loc, state, "for matrix `%s' constructor, "
2308 "matrix must be only parameter",
2309 glsl_get_type_name(constructor_type));
2310 return ir_rvalue::error_value(ctx);
2311 }
2312
2313 /* From page 28 (page 34 of the PDF) of the GLSL 1.10 spec:
2314 *
2315 * "In these cases, there must be enough components provided in the
2316 * arguments to provide an initializer for every component in the
2317 * constructed value."
2318 */
2319 if (components_used < type_components && components_used != 1
2320 && matrix_parameters == 0) {
2321 _mesa_glsl_error(& loc, state, "too few components to construct "
2322 "`%s'",
2323 glsl_get_type_name(constructor_type));
2324 return ir_rvalue::error_value(ctx);
2325 }
2326
2327 /* Matrices can never be consumed as is by any constructor but matrix
2328 * constructors. If the constructor type is not matrix, always break the
2329 * matrix up into a series of column vectors.
2330 */
2331 if (!glsl_type_is_matrix(constructor_type)) {
2332 foreach_in_list_safe(ir_rvalue, matrix, &actual_parameters) {
2333 if (!glsl_type_is_matrix(matrix->type))
2334 continue;
2335
2336 /* Create a temporary containing the matrix. */
2337 ir_variable *var = new(ctx) ir_variable(matrix->type, "matrix_tmp",
2338 ir_var_temporary);
2339 instructions->push_tail(var);
2340 instructions->push_tail(
2341 new(ctx) ir_assignment(new(ctx) ir_dereference_variable(var),
2342 matrix));
2343 var->constant_value = matrix->constant_expression_value(ctx);
2344
2345 /* Replace the matrix with dereferences of its columns. */
2346 for (int i = 0; i < matrix->type->matrix_columns; i++) {
2347 matrix->insert_before(
2348 new (ctx) ir_dereference_array(var,
2349 new(ctx) ir_constant(i)));
2350 }
2351 matrix->remove();
2352 }
2353 }
2354
2355 bool all_parameters_are_constant = true;
2356
2357 /* Type cast each parameter and, if possible, fold constants.*/
2358 foreach_in_list_safe(ir_rvalue, ir, &actual_parameters) {
2359 const glsl_type *desired_type;
2360
2361 /* From section 5.4.1 of the ARB_bindless_texture spec:
2362 *
2363 * "In the following four constructors, the low 32 bits of the sampler
2364 * type correspond to the .x component of the uvec2 and the high 32
2365 * bits correspond to the .y component."
2366 *
2367 * uvec2(any sampler type) // Converts a sampler type to a
2368 * // pair of 32-bit unsigned integers
2369 * any sampler type(uvec2) // Converts a pair of 32-bit unsigned integers to
2370 * // a sampler type
2371 * uvec2(any image type) // Converts an image type to a
2372 * // pair of 32-bit unsigned integers
2373 * any image type(uvec2) // Converts a pair of 32-bit unsigned integers to
2374 * // an image type
2375 */
2376 if (glsl_type_is_sampler(ir->type) || glsl_type_is_image(ir->type)) {
2377 /* Convert a sampler/image type to a pair of 32-bit unsigned
2378 * integers as defined by ARB_bindless_texture.
2379 */
2380 if (constructor_type != &glsl_type_builtin_uvec2) {
2381 _mesa_glsl_error(&loc, state, "sampler and image types can only "
2382 "be converted to a pair of 32-bit unsigned "
2383 "integers");
2384 }
2385 desired_type = &glsl_type_builtin_uvec2;
2386 } else if (glsl_type_is_sampler(constructor_type) ||
2387 glsl_type_is_image(constructor_type)) {
2388 /* Convert a pair of 32-bit unsigned integers to a sampler or image
2389 * type as defined by ARB_bindless_texture.
2390 */
2391 if (ir->type != &glsl_type_builtin_uvec2) {
2392 _mesa_glsl_error(&loc, state, "sampler and image types can only "
2393 "be converted from a pair of 32-bit unsigned "
2394 "integers");
2395 }
2396 desired_type = constructor_type;
2397 } else {
2398 desired_type =
2399 glsl_simple_type(constructor_type->base_type,
2400 ir->type->vector_elements,
2401 ir->type->matrix_columns);
2402 }
2403
2404 ir_rvalue *result = convert_component(ir, desired_type);
2405
2406 /* If the bindless packing constructors are used directly as function
2407 * params to bultin functions the compiler doesn't know what to do
2408 * with them. To avoid this make sure we always copy the results from
2409 * the pack to a temp first.
2410 */
2411 if (result->as_expression() &&
2412 result->as_expression()->operation == ir_unop_pack_sampler_2x32) {
2413 ir_variable *var =
2414 new(ctx) ir_variable(desired_type, "sampler_ctor",
2415 ir_var_temporary);
2416 instructions->push_tail(var);
2417
2418 ir_dereference *lhs = new(ctx) ir_dereference_variable(var);
2419 ir_instruction *assignment = new(ctx) ir_assignment(lhs, result);
2420 instructions->push_tail(assignment);
2421 result = lhs;
2422 }
2423
2424 /* Attempt to convert the parameter to a constant valued expression.
2425 * After doing so, track whether or not all the parameters to the
2426 * constructor are trivially constant valued expressions.
2427 */
2428 ir_rvalue *const constant = result->constant_expression_value(ctx);
2429
2430 if (constant != NULL)
2431 result = constant;
2432 else
2433 all_parameters_are_constant = false;
2434
2435 if (result != ir) {
2436 ir->replace_with(result);
2437 }
2438 }
2439
2440 /* If all of the parameters are trivially constant, create a
2441 * constant representing the complete collection of parameters.
2442 */
2443 if (all_parameters_are_constant) {
2444 return new(ctx) ir_constant(constructor_type, &actual_parameters);
2445 } else if (glsl_type_is_scalar(constructor_type)) {
2446 return dereference_component((ir_rvalue *)
2447 actual_parameters.get_head_raw(),
2448 0);
2449 } else if (glsl_type_is_vector(constructor_type)) {
2450 return emit_inline_vector_constructor(constructor_type,
2451 instructions,
2452 &actual_parameters,
2453 ctx);
2454 } else {
2455 assert(glsl_type_is_matrix(constructor_type));
2456 return emit_inline_matrix_constructor(constructor_type,
2457 instructions,
2458 &actual_parameters,
2459 ctx);
2460 }
2461 } else if (subexpressions[0]->oper == ast_field_selection) {
2462 return handle_method(instructions, state);
2463 } else {
2464 const ast_expression *id = subexpressions[0];
2465 const char *func_name = NULL;
2466 YYLTYPE loc = get_location();
2467 exec_list actual_parameters;
2468 ir_variable *sub_var = NULL;
2469 ir_rvalue *array_idx = NULL;
2470
2471 process_parameters(instructions, &actual_parameters, &this->expressions,
2472 state);
2473
2474 if (id->oper == ast_array_index) {
2475 array_idx = generate_array_index(ctx, instructions, state, loc,
2476 id->subexpressions[0],
2477 id->subexpressions[1], &func_name,
2478 &actual_parameters);
2479 } else if (id->oper == ast_identifier) {
2480 func_name = id->primary_expression.identifier;
2481 } else {
2482 _mesa_glsl_error(&loc, state, "function name is not an identifier");
2483 }
2484
2485 /* an error was emitted earlier */
2486 if (!func_name)
2487 return ir_rvalue::error_value(ctx);
2488
2489 ir_function_signature *sig =
2490 match_function_by_name(func_name, &actual_parameters, state);
2491
2492 ir_rvalue *value = NULL;
2493 if (sig == NULL) {
2494 sig = match_subroutine_by_name(func_name, &actual_parameters,
2495 state, &sub_var);
2496 }
2497
2498 if (sig == NULL) {
2499 no_matching_function_error(func_name, &loc,
2500 &actual_parameters, state);
2501 value = ir_rvalue::error_value(ctx);
2502 } else if (!verify_parameter_modes(state, sig,
2503 actual_parameters,
2504 this->expressions)) {
2505 /* an error has already been emitted */
2506 value = ir_rvalue::error_value(ctx);
2507 } else if (sig->is_builtin() && strcmp(func_name, "ftransform") == 0) {
2508 /* ftransform refers to global variables, and we don't have any code
2509 * for remapping the variable references in the built-in shader.
2510 */
2511 ir_variable *mvp =
2512 state->symbols->get_variable("gl_ModelViewProjectionMatrix");
2513 ir_variable *vtx = state->symbols->get_variable("gl_Vertex");
2514 value = new(ctx) ir_expression(ir_binop_mul, &glsl_type_builtin_vec4,
2515 new(ctx) ir_dereference_variable(mvp),
2516 new(ctx) ir_dereference_variable(vtx));
2517 } else {
2518 bool is_begin_interlock = false;
2519 bool is_end_interlock = false;
2520 if (sig->is_builtin() &&
2521 state->stage == MESA_SHADER_FRAGMENT &&
2522 state->ARB_fragment_shader_interlock_enable) {
2523 is_begin_interlock = strcmp(func_name, "beginInvocationInterlockARB") == 0;
2524 is_end_interlock = strcmp(func_name, "endInvocationInterlockARB") == 0;
2525 }
2526
2527 if (sig->is_builtin() &&
2528 ((state->stage == MESA_SHADER_TESS_CTRL &&
2529 strcmp(func_name, "barrier") == 0) ||
2530 is_begin_interlock || is_end_interlock)) {
2531 if (state->current_function == NULL ||
2532 strcmp(state->current_function->function_name(), "main") != 0) {
2533 _mesa_glsl_error(&loc, state,
2534 "%s() may only be used in main()", func_name);
2535 }
2536
2537 if (state->found_return) {
2538 _mesa_glsl_error(&loc, state,
2539 "%s() may not be used after return", func_name);
2540 }
2541
2542 if (instructions != &state->current_function->body) {
2543 _mesa_glsl_error(&loc, state,
2544 "%s() may not be used in control flow", func_name);
2545 }
2546 }
2547
2548 /* There can be only one begin/end interlock pair in the function. */
2549 if (is_begin_interlock) {
2550 if (state->found_begin_interlock)
2551 _mesa_glsl_error(&loc, state,
2552 "beginInvocationInterlockARB may not be used twice");
2553 state->found_begin_interlock = true;
2554 } else if (is_end_interlock) {
2555 if (!state->found_begin_interlock)
2556 _mesa_glsl_error(&loc, state,
2557 "endInvocationInterlockARB may not be used "
2558 "before beginInvocationInterlockARB");
2559 if (state->found_end_interlock)
2560 _mesa_glsl_error(&loc, state,
2561 "endInvocationInterlockARB may not be used twice");
2562 state->found_end_interlock = true;
2563 }
2564
2565 value = generate_call(instructions, sig, &actual_parameters, sub_var,
2566 array_idx, state);
2567 if (!value) {
2568 ir_variable *const tmp = new(ctx) ir_variable(&glsl_type_builtin_void,
2569 "void_var",
2570 ir_var_temporary);
2571 instructions->push_tail(tmp);
2572 value = new(ctx) ir_dereference_variable(tmp);
2573 }
2574 }
2575
2576 return value;
2577 }
2578
2579 unreachable("not reached");
2580 }
2581
2582 bool
has_sequence_subexpression() const2583 ast_function_expression::has_sequence_subexpression() const
2584 {
2585 foreach_list_typed(const ast_node, ast, link, &this->expressions) {
2586 if (ast->has_sequence_subexpression())
2587 return true;
2588 }
2589
2590 return false;
2591 }
2592
2593 ir_rvalue *
hir(exec_list * instructions,struct _mesa_glsl_parse_state * state)2594 ast_aggregate_initializer::hir(exec_list *instructions,
2595 struct _mesa_glsl_parse_state *state)
2596 {
2597 void *ctx = state;
2598 YYLTYPE loc = this->get_location();
2599
2600 if (!this->constructor_type) {
2601 _mesa_glsl_error(&loc, state, "type of C-style initializer unknown");
2602 return ir_rvalue::error_value(ctx);
2603 }
2604 const glsl_type *const constructor_type = this->constructor_type;
2605
2606 if (!state->has_420pack()) {
2607 _mesa_glsl_error(&loc, state, "C-style initialization requires the "
2608 "GL_ARB_shading_language_420pack extension");
2609 return ir_rvalue::error_value(ctx);
2610 }
2611
2612 if (glsl_type_is_array(constructor_type)) {
2613 return process_array_constructor(instructions, constructor_type, &loc,
2614 &this->expressions, state);
2615 }
2616
2617 if (glsl_type_is_struct(constructor_type)) {
2618 return process_record_constructor(instructions, constructor_type, &loc,
2619 &this->expressions, state);
2620 }
2621
2622 return process_vec_mat_constructor(instructions, constructor_type, &loc,
2623 &this->expressions, state);
2624 }
2625