2 * Copyright © 2010 Intel Corporation
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:
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
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.
24 #include "glsl_symbol_table.h"
26 #include "compiler/glsl_types.h"
28 #include "main/core.h" /* for MIN2 */
29 #include "main/shaderobj.h"
30 #include "builtin_functions.h"
33 convert_component(ir_rvalue
*src
, const glsl_type
*desired_type
);
36 process_parameters(exec_list
*instructions
, exec_list
*actual_parameters
,
37 exec_list
*parameters
,
38 struct _mesa_glsl_parse_state
*state
)
40 void *mem_ctx
= state
;
43 foreach_list_typed(ast_node
, ast
, link
, parameters
) {
44 /* We need to process the parameters first in order to know if we can
45 * raise or not a unitialized warning. Calling set_is_lhs silence the
46 * warning for now. Raising the warning or not will be checked at
47 * verify_parameter_modes.
49 ast
->set_is_lhs(true);
50 ir_rvalue
*result
= ast
->hir(instructions
, state
);
52 ir_constant
*const constant
=
53 result
->constant_expression_value(mem_ctx
);
58 actual_parameters
->push_tail(result
);
67 * Generate a source prototype for a function signature
69 * \param return_type Return type of the function. May be \c NULL.
70 * \param name Name of the function.
71 * \param parameters List of \c ir_instruction nodes representing the
72 * parameter list for the function. This may be either a
73 * formal (\c ir_variable) or actual (\c ir_rvalue)
74 * parameter list. Only the type is used.
77 * A ralloced string representing the prototype of the function.
80 prototype_string(const glsl_type
*return_type
, const char *name
,
81 exec_list
*parameters
)
85 if (return_type
!= NULL
)
86 str
= ralloc_asprintf(NULL
, "%s ", return_type
->name
);
88 ralloc_asprintf_append(&str
, "%s(", name
);
90 const char *comma
= "";
91 foreach_in_list(const ir_variable
, param
, parameters
) {
92 ralloc_asprintf_append(&str
, "%s%s", comma
, param
->type
->name
);
96 ralloc_strcat(&str
, ")");
101 verify_image_parameter(YYLTYPE
*loc
, _mesa_glsl_parse_state
*state
,
102 const ir_variable
*formal
, const ir_variable
*actual
)
105 * From the ARB_shader_image_load_store specification:
107 * "The values of image variables qualified with coherent,
108 * volatile, restrict, readonly, or writeonly may not be passed
109 * to functions whose formal parameters lack such
110 * qualifiers. [...] It is legal to have additional qualifiers
111 * on a formal parameter, but not to have fewer."
113 if (actual
->data
.memory_coherent
&& !formal
->data
.memory_coherent
) {
114 _mesa_glsl_error(loc
, state
,
115 "function call parameter `%s' drops "
116 "`coherent' qualifier", formal
->name
);
120 if (actual
->data
.memory_volatile
&& !formal
->data
.memory_volatile
) {
121 _mesa_glsl_error(loc
, state
,
122 "function call parameter `%s' drops "
123 "`volatile' qualifier", formal
->name
);
127 if (actual
->data
.memory_restrict
&& !formal
->data
.memory_restrict
) {
128 _mesa_glsl_error(loc
, state
,
129 "function call parameter `%s' drops "
130 "`restrict' qualifier", formal
->name
);
134 if (actual
->data
.memory_read_only
&& !formal
->data
.memory_read_only
) {
135 _mesa_glsl_error(loc
, state
,
136 "function call parameter `%s' drops "
137 "`readonly' qualifier", formal
->name
);
141 if (actual
->data
.memory_write_only
&& !formal
->data
.memory_write_only
) {
142 _mesa_glsl_error(loc
, state
,
143 "function call parameter `%s' drops "
144 "`writeonly' qualifier", formal
->name
);
152 verify_first_atomic_parameter(YYLTYPE
*loc
, _mesa_glsl_parse_state
*state
,
156 (!var
->is_in_shader_storage_block() &&
157 var
->data
.mode
!= ir_var_shader_shared
)) {
158 _mesa_glsl_error(loc
, state
, "First argument to atomic function "
159 "must be a buffer or shared variable");
166 is_atomic_function(const char *func_name
)
168 return !strcmp(func_name
, "atomicAdd") ||
169 !strcmp(func_name
, "atomicMin") ||
170 !strcmp(func_name
, "atomicMax") ||
171 !strcmp(func_name
, "atomicAnd") ||
172 !strcmp(func_name
, "atomicOr") ||
173 !strcmp(func_name
, "atomicXor") ||
174 !strcmp(func_name
, "atomicExchange") ||
175 !strcmp(func_name
, "atomicCompSwap");
179 * Verify that 'out' and 'inout' actual parameters are lvalues. Also, verify
180 * that 'const_in' formal parameters (an extension in our IR) correspond to
181 * ir_constant actual parameters.
184 verify_parameter_modes(_mesa_glsl_parse_state
*state
,
185 ir_function_signature
*sig
,
186 exec_list
&actual_ir_parameters
,
187 exec_list
&actual_ast_parameters
)
189 exec_node
*actual_ir_node
= actual_ir_parameters
.get_head_raw();
190 exec_node
*actual_ast_node
= actual_ast_parameters
.get_head_raw();
192 foreach_in_list(const ir_variable
, formal
, &sig
->parameters
) {
193 /* The lists must be the same length. */
194 assert(!actual_ir_node
->is_tail_sentinel());
195 assert(!actual_ast_node
->is_tail_sentinel());
197 const ir_rvalue
*const actual
= (ir_rvalue
*) actual_ir_node
;
198 const ast_expression
*const actual_ast
=
199 exec_node_data(ast_expression
, actual_ast_node
, link
);
201 /* FIXME: 'loc' is incorrect (as of 2011-01-21). It is always
204 YYLTYPE loc
= actual_ast
->get_location();
206 /* Verify that 'const_in' parameters are ir_constants. */
207 if (formal
->data
.mode
== ir_var_const_in
&&
208 actual
->ir_type
!= ir_type_constant
) {
209 _mesa_glsl_error(&loc
, state
,
210 "parameter `in %s' must be a constant expression",
215 /* Verify that shader_in parameters are shader inputs */
216 if (formal
->data
.must_be_shader_input
) {
217 const ir_rvalue
*val
= actual
;
219 /* GLSL 4.40 allows swizzles, while earlier GLSL versions do not. */
220 if (val
->ir_type
== ir_type_swizzle
) {
221 if (!state
->is_version(440, 0)) {
222 _mesa_glsl_error(&loc
, state
,
223 "parameter `%s` must not be swizzled",
227 val
= ((ir_swizzle
*)val
)->val
;
231 if (val
->ir_type
== ir_type_dereference_array
) {
232 val
= ((ir_dereference_array
*)val
)->array
;
233 } else if (val
->ir_type
== ir_type_dereference_record
&&
235 val
= ((ir_dereference_record
*)val
)->record
;
240 ir_variable
*var
= NULL
;
241 if (const ir_dereference_variable
*deref_var
= val
->as_dereference_variable())
242 var
= deref_var
->variable_referenced();
244 if (!var
|| var
->data
.mode
!= ir_var_shader_in
) {
245 _mesa_glsl_error(&loc
, state
,
246 "parameter `%s` must be a shader input",
251 var
->data
.must_be_shader_input
= 1;
254 /* Verify that 'out' and 'inout' actual parameters are lvalues. */
255 if (formal
->data
.mode
== ir_var_function_out
256 || formal
->data
.mode
== ir_var_function_inout
) {
257 const char *mode
= NULL
;
258 switch (formal
->data
.mode
) {
259 case ir_var_function_out
: mode
= "out"; break;
260 case ir_var_function_inout
: mode
= "inout"; break;
261 default: assert(false); break;
264 /* This AST-based check catches errors like f(i++). The IR-based
265 * is_lvalue() is insufficient because the actual parameter at the
266 * IR-level is just a temporary value, which is an l-value.
268 if (actual_ast
->non_lvalue_description
!= NULL
) {
269 _mesa_glsl_error(&loc
, state
,
270 "function parameter '%s %s' references a %s",
272 actual_ast
->non_lvalue_description
);
276 ir_variable
*var
= actual
->variable_referenced();
278 if (var
&& formal
->data
.mode
== ir_var_function_inout
) {
279 if ((var
->data
.mode
== ir_var_auto
||
280 var
->data
.mode
== ir_var_shader_out
) &&
281 !var
->data
.assigned
&&
282 !is_gl_identifier(var
->name
)) {
283 _mesa_glsl_warning(&loc
, state
, "`%s' used uninitialized",
289 var
->data
.assigned
= true;
291 if (var
&& var
->data
.read_only
) {
292 _mesa_glsl_error(&loc
, state
,
293 "function parameter '%s %s' references the "
294 "read-only variable '%s'",
296 actual
->variable_referenced()->name
);
298 } else if (!actual
->is_lvalue(state
)) {
299 _mesa_glsl_error(&loc
, state
,
300 "function parameter '%s %s' is not an lvalue",
305 assert(formal
->data
.mode
== ir_var_function_in
||
306 formal
->data
.mode
== ir_var_const_in
);
307 ir_variable
*var
= actual
->variable_referenced();
309 if ((var
->data
.mode
== ir_var_auto
||
310 var
->data
.mode
== ir_var_shader_out
) &&
311 !var
->data
.assigned
&&
312 !is_gl_identifier(var
->name
)) {
313 _mesa_glsl_warning(&loc
, state
, "`%s' used uninitialized",
319 if (formal
->type
->is_image() &&
320 actual
->variable_referenced()) {
321 if (!verify_image_parameter(&loc
, state
, formal
,
322 actual
->variable_referenced()))
326 actual_ir_node
= actual_ir_node
->next
;
327 actual_ast_node
= actual_ast_node
->next
;
330 /* The first parameter of atomic functions must be a buffer variable */
331 const char *func_name
= sig
->function_name();
332 bool is_atomic
= is_atomic_function(func_name
);
334 const ir_rvalue
*const actual
=
335 (ir_rvalue
*) actual_ir_parameters
.get_head_raw();
337 const ast_expression
*const actual_ast
=
338 exec_node_data(ast_expression
,
339 actual_ast_parameters
.get_head_raw(), link
);
340 YYLTYPE loc
= actual_ast
->get_location();
342 if (!verify_first_atomic_parameter(&loc
, state
,
343 actual
->variable_referenced())) {
352 fix_parameter(void *mem_ctx
, ir_rvalue
*actual
, const glsl_type
*formal_type
,
353 exec_list
*before_instructions
, exec_list
*after_instructions
,
354 bool parameter_is_inout
)
356 ir_expression
*const expr
= actual
->as_expression();
358 /* If the types match exactly and the parameter is not a vector-extract,
359 * nothing needs to be done to fix the parameter.
361 if (formal_type
== actual
->type
362 && (expr
== NULL
|| expr
->operation
!= ir_binop_vector_extract
))
365 /* To convert an out parameter, we need to create a temporary variable to
366 * hold the value before conversion, and then perform the conversion after
367 * the function call returns.
369 * This has the effect of transforming code like this:
375 * Into IR that's equivalent to this:
379 * int out_parameter_conversion;
380 * f(out_parameter_conversion);
381 * value = float(out_parameter_conversion);
383 * If the parameter is an ir_expression of ir_binop_vector_extract,
384 * additional conversion is needed in the post-call re-write.
387 new(mem_ctx
) ir_variable(formal_type
, "inout_tmp", ir_var_temporary
);
389 before_instructions
->push_tail(tmp
);
391 /* If the parameter is an inout parameter, copy the value of the actual
392 * parameter to the new temporary. Note that no type conversion is allowed
393 * here because inout parameters must match types exactly.
395 if (parameter_is_inout
) {
396 /* Inout parameters should never require conversion, since that would
397 * require an implicit conversion to exist both to and from the formal
398 * parameter type, and there are no bidirectional implicit conversions.
400 assert (actual
->type
== formal_type
);
402 ir_dereference_variable
*const deref_tmp_1
=
403 new(mem_ctx
) ir_dereference_variable(tmp
);
404 ir_assignment
*const assignment
=
405 new(mem_ctx
) ir_assignment(deref_tmp_1
, actual
);
406 before_instructions
->push_tail(assignment
);
409 /* Replace the parameter in the call with a dereference of the new
412 ir_dereference_variable
*const deref_tmp_2
=
413 new(mem_ctx
) ir_dereference_variable(tmp
);
414 actual
->replace_with(deref_tmp_2
);
417 /* Copy the temporary variable to the actual parameter with optional
418 * type conversion applied.
420 ir_rvalue
*rhs
= new(mem_ctx
) ir_dereference_variable(tmp
);
421 if (actual
->type
!= formal_type
)
422 rhs
= convert_component(rhs
, actual
->type
);
424 ir_rvalue
*lhs
= actual
;
425 if (expr
!= NULL
&& expr
->operation
== ir_binop_vector_extract
) {
426 lhs
= new(mem_ctx
) ir_dereference_array(expr
->operands
[0]->clone(mem_ctx
,
428 expr
->operands
[1]->clone(mem_ctx
,
432 ir_assignment
*const assignment_2
= new(mem_ctx
) ir_assignment(lhs
, rhs
);
433 after_instructions
->push_tail(assignment_2
);
437 * Generate a function call.
439 * For non-void functions, this returns a dereference of the temporary
440 * variable which stores the return value for the call. For void functions,
444 generate_call(exec_list
*instructions
, ir_function_signature
*sig
,
445 exec_list
*actual_parameters
,
446 ir_variable
*sub_var
,
447 ir_rvalue
*array_idx
,
448 struct _mesa_glsl_parse_state
*state
)
451 exec_list post_call_conversions
;
453 /* Perform implicit conversion of arguments. For out parameters, we need
454 * to place them in a temporary variable and do the conversion after the
455 * call takes place. Since we haven't emitted the call yet, we'll place
456 * the post-call conversions in a temporary exec_list, and emit them later.
458 foreach_two_lists(formal_node
, &sig
->parameters
,
459 actual_node
, actual_parameters
) {
460 ir_rvalue
*actual
= (ir_rvalue
*) actual_node
;
461 ir_variable
*formal
= (ir_variable
*) formal_node
;
463 if (formal
->type
->is_numeric() || formal
->type
->is_boolean()) {
464 switch (formal
->data
.mode
) {
465 case ir_var_const_in
:
466 case ir_var_function_in
: {
468 = convert_component(actual
, formal
->type
);
469 actual
->replace_with(converted
);
472 case ir_var_function_out
:
473 case ir_var_function_inout
:
474 fix_parameter(ctx
, actual
, formal
->type
,
475 instructions
, &post_call_conversions
,
476 formal
->data
.mode
== ir_var_function_inout
);
479 assert (!"Illegal formal parameter mode");
485 /* Section 4.3.2 (Const) of the GLSL 1.10.59 spec says:
487 * "Initializers for const declarations must be formed from literal
488 * values, other const variables (not including function call
489 * paramaters), or expressions of these.
491 * Constructors may be used in such expressions, but function calls may
494 * Section 4.3.3 (Constant Expressions) of the GLSL 1.20.8 spec says:
496 * "A constant expression is one of
500 * - a built-in function call whose arguments are all constant
501 * expressions, with the exception of the texture lookup
502 * functions, the noise functions, and ftransform. The built-in
503 * functions dFdx, dFdy, and fwidth must return 0 when evaluated
504 * inside an initializer with an argument that is a constant
507 * Section 5.10 (Constant Expressions) of the GLSL ES 1.00.17 spec says:
509 * "A constant expression is one of
513 * - a built-in function call whose arguments are all constant
514 * expressions, with the exception of the texture lookup
517 * Section 4.3.3 (Constant Expressions) of the GLSL ES 3.00.4 spec says:
519 * "A constant expression is one of
523 * - a built-in function call whose arguments are all constant
524 * expressions, with the exception of the texture lookup
525 * functions. The built-in functions dFdx, dFdy, and fwidth must
526 * return 0 when evaluated inside an initializer with an argument
527 * that is a constant expression."
529 * If the function call is a constant expression, don't generate any
530 * instructions; just generate an ir_constant.
532 if (state
->is_version(120, 100)) {
533 ir_constant
*value
= sig
->constant_expression_value(ctx
,
541 ir_dereference_variable
*deref
= NULL
;
542 if (!sig
->return_type
->is_void()) {
543 /* Create a new temporary to hold the return value. */
544 char *const name
= ir_variable::temporaries_allocate_names
545 ? ralloc_asprintf(ctx
, "%s_retval", sig
->function_name())
550 var
= new(ctx
) ir_variable(sig
->return_type
, name
, ir_var_temporary
);
551 instructions
->push_tail(var
);
555 deref
= new(ctx
) ir_dereference_variable(var
);
558 ir_call
*call
= new(ctx
) ir_call(sig
, deref
,
559 actual_parameters
, sub_var
, array_idx
);
560 instructions
->push_tail(call
);
561 if (sig
->is_builtin()) {
562 /* inline immediately */
563 call
->generate_inline(call
);
567 /* Also emit any necessary out-parameter conversions. */
568 instructions
->append_list(&post_call_conversions
);
570 return deref
? deref
->clone(ctx
, NULL
) : NULL
;
574 * Given a function name and parameter list, find the matching signature.
576 static ir_function_signature
*
577 match_function_by_name(const char *name
,
578 exec_list
*actual_parameters
,
579 struct _mesa_glsl_parse_state
*state
)
581 ir_function
*f
= state
->symbols
->get_function(name
);
582 ir_function_signature
*local_sig
= NULL
;
583 ir_function_signature
*sig
= NULL
;
585 /* Is the function hidden by a record type constructor? */
586 if (state
->symbols
->get_type(name
))
587 return sig
; /* no match */
589 /* Is the function hidden by a variable (impossible in 1.10)? */
590 if (!state
->symbols
->separate_function_namespace
591 && state
->symbols
->get_variable(name
))
592 return sig
; /* no match */
595 /* In desktop GL, the presence of a user-defined signature hides any
596 * built-in signatures, so we must ignore them. In contrast, in ES2
597 * user-defined signatures add new overloads, so we must consider them.
599 bool allow_builtins
= state
->es_shader
|| !f
->has_user_signature();
601 /* Look for a match in the local shader. If exact, we're done. */
602 bool is_exact
= false;
603 sig
= local_sig
= f
->matching_signature(state
, actual_parameters
,
604 allow_builtins
, &is_exact
);
612 /* Local shader has no exact candidates; check the built-ins. */
613 _mesa_glsl_initialize_builtin_functions();
614 sig
= _mesa_glsl_find_builtin_function(state
, name
, actual_parameters
);
618 static ir_function_signature
*
619 match_subroutine_by_name(const char *name
,
620 exec_list
*actual_parameters
,
621 struct _mesa_glsl_parse_state
*state
,
625 ir_function_signature
*sig
= NULL
;
626 ir_function
*f
, *found
= NULL
;
627 const char *new_name
;
629 bool is_exact
= false;
632 ralloc_asprintf(ctx
, "%s_%s",
633 _mesa_shader_stage_to_subroutine_prefix(state
->stage
),
635 var
= state
->symbols
->get_variable(new_name
);
639 for (int i
= 0; i
< state
->num_subroutine_types
; i
++) {
640 f
= state
->subroutine_types
[i
];
641 if (strcmp(f
->name
, var
->type
->without_array()->name
))
650 sig
= found
->matching_signature(state
, actual_parameters
,
656 generate_array_index(void *mem_ctx
, exec_list
*instructions
,
657 struct _mesa_glsl_parse_state
*state
, YYLTYPE loc
,
658 const ast_expression
*array
, ast_expression
*idx
,
659 const char **function_name
, exec_list
*actual_parameters
)
661 if (array
->oper
== ast_array_index
) {
662 /* This handles arrays of arrays */
663 ir_rvalue
*outer_array
= generate_array_index(mem_ctx
, instructions
,
665 array
->subexpressions
[0],
666 array
->subexpressions
[1],
669 ir_rvalue
*outer_array_idx
= idx
->hir(instructions
, state
);
671 YYLTYPE index_loc
= idx
->get_location();
672 return _mesa_ast_array_index_to_hir(mem_ctx
, state
, outer_array
,
673 outer_array_idx
, loc
,
676 ir_variable
*sub_var
= NULL
;
677 *function_name
= array
->primary_expression
.identifier
;
679 match_subroutine_by_name(*function_name
, actual_parameters
,
682 ir_rvalue
*outer_array_idx
= idx
->hir(instructions
, state
);
683 return new(mem_ctx
) ir_dereference_array(sub_var
, outer_array_idx
);
688 print_function_prototypes(_mesa_glsl_parse_state
*state
, YYLTYPE
*loc
,
694 foreach_in_list(ir_function_signature
, sig
, &f
->signatures
) {
695 if (sig
->is_builtin() && !sig
->is_builtin_available(state
))
698 char *str
= prototype_string(sig
->return_type
, f
->name
,
700 _mesa_glsl_error(loc
, state
, " %s", str
);
706 * Raise a "no matching function" error, listing all possible overloads the
707 * compiler considered so developers can figure out what went wrong.
710 no_matching_function_error(const char *name
,
712 exec_list
*actual_parameters
,
713 _mesa_glsl_parse_state
*state
)
715 gl_shader
*sh
= _mesa_glsl_get_builtin_function_shader();
717 if (state
->symbols
->get_function(name
) == NULL
718 && (!state
->uses_builtin_functions
719 || sh
->symbols
->get_function(name
) == NULL
)) {
720 _mesa_glsl_error(loc
, state
, "no function with name '%s'", name
);
722 char *str
= prototype_string(NULL
, name
, actual_parameters
);
723 _mesa_glsl_error(loc
, state
,
724 "no matching function for call to `%s';"
729 print_function_prototypes(state
, loc
,
730 state
->symbols
->get_function(name
));
732 if (state
->uses_builtin_functions
) {
733 print_function_prototypes(state
, loc
,
734 sh
->symbols
->get_function(name
));
740 * Perform automatic type conversion of constructor parameters
742 * This implements the rules in the "Conversion and Scalar Constructors"
743 * section (GLSL 1.10 section 5.4.1), not the "Implicit Conversions" rules.
746 convert_component(ir_rvalue
*src
, const glsl_type
*desired_type
)
748 void *ctx
= ralloc_parent(src
);
749 const unsigned a
= desired_type
->base_type
;
750 const unsigned b
= src
->type
->base_type
;
751 ir_expression
*result
= NULL
;
753 if (src
->type
->is_error())
756 assert(a
<= GLSL_TYPE_IMAGE
);
757 assert(b
<= GLSL_TYPE_IMAGE
);
766 result
= new(ctx
) ir_expression(ir_unop_i2u
, src
);
768 case GLSL_TYPE_FLOAT
:
769 result
= new(ctx
) ir_expression(ir_unop_f2u
, src
);
772 result
= new(ctx
) ir_expression(ir_unop_i2u
,
773 new(ctx
) ir_expression(ir_unop_b2i
,
776 case GLSL_TYPE_DOUBLE
:
777 result
= new(ctx
) ir_expression(ir_unop_d2u
, src
);
779 case GLSL_TYPE_UINT64
:
780 result
= new(ctx
) ir_expression(ir_unop_u642u
, src
);
782 case GLSL_TYPE_INT64
:
783 result
= new(ctx
) ir_expression(ir_unop_i642u
, src
);
785 case GLSL_TYPE_SAMPLER
:
786 result
= new(ctx
) ir_expression(ir_unop_unpack_sampler_2x32
, src
);
788 case GLSL_TYPE_IMAGE
:
789 result
= new(ctx
) ir_expression(ir_unop_unpack_image_2x32
, src
);
796 result
= new(ctx
) ir_expression(ir_unop_u2i
, src
);
798 case GLSL_TYPE_FLOAT
:
799 result
= new(ctx
) ir_expression(ir_unop_f2i
, src
);
802 result
= new(ctx
) ir_expression(ir_unop_b2i
, src
);
804 case GLSL_TYPE_DOUBLE
:
805 result
= new(ctx
) ir_expression(ir_unop_d2i
, src
);
807 case GLSL_TYPE_UINT64
:
808 result
= new(ctx
) ir_expression(ir_unop_u642i
, src
);
810 case GLSL_TYPE_INT64
:
811 result
= new(ctx
) ir_expression(ir_unop_i642i
, src
);
815 case GLSL_TYPE_FLOAT
:
818 result
= new(ctx
) ir_expression(ir_unop_u2f
, desired_type
, src
, NULL
);
821 result
= new(ctx
) ir_expression(ir_unop_i2f
, desired_type
, src
, NULL
);
824 result
= new(ctx
) ir_expression(ir_unop_b2f
, desired_type
, src
, NULL
);
826 case GLSL_TYPE_DOUBLE
:
827 result
= new(ctx
) ir_expression(ir_unop_d2f
, desired_type
, src
, NULL
);
829 case GLSL_TYPE_UINT64
:
830 result
= new(ctx
) ir_expression(ir_unop_u642f
, desired_type
, src
, NULL
);
832 case GLSL_TYPE_INT64
:
833 result
= new(ctx
) ir_expression(ir_unop_i642f
, desired_type
, src
, NULL
);
840 result
= new(ctx
) ir_expression(ir_unop_i2b
,
841 new(ctx
) ir_expression(ir_unop_u2i
,
845 result
= new(ctx
) ir_expression(ir_unop_i2b
, desired_type
, src
, NULL
);
847 case GLSL_TYPE_FLOAT
:
848 result
= new(ctx
) ir_expression(ir_unop_f2b
, desired_type
, src
, NULL
);
850 case GLSL_TYPE_DOUBLE
:
851 result
= new(ctx
) ir_expression(ir_unop_d2b
, desired_type
, src
, NULL
);
853 case GLSL_TYPE_UINT64
:
854 result
= new(ctx
) ir_expression(ir_unop_i642b
,
855 new(ctx
) ir_expression(ir_unop_u642i64
,
858 case GLSL_TYPE_INT64
:
859 result
= new(ctx
) ir_expression(ir_unop_i642b
, desired_type
, src
, NULL
);
863 case GLSL_TYPE_DOUBLE
:
866 result
= new(ctx
) ir_expression(ir_unop_i2d
, src
);
869 result
= new(ctx
) ir_expression(ir_unop_u2d
, src
);
872 result
= new(ctx
) ir_expression(ir_unop_f2d
,
873 new(ctx
) ir_expression(ir_unop_b2f
,
876 case GLSL_TYPE_FLOAT
:
877 result
= new(ctx
) ir_expression(ir_unop_f2d
, desired_type
, src
, NULL
);
879 case GLSL_TYPE_UINT64
:
880 result
= new(ctx
) ir_expression(ir_unop_u642d
, desired_type
, src
, NULL
);
882 case GLSL_TYPE_INT64
:
883 result
= new(ctx
) ir_expression(ir_unop_i642d
, desired_type
, src
, NULL
);
887 case GLSL_TYPE_UINT64
:
890 result
= new(ctx
) ir_expression(ir_unop_i2u64
, src
);
893 result
= new(ctx
) ir_expression(ir_unop_u2u64
, src
);
896 result
= new(ctx
) ir_expression(ir_unop_i642u64
,
897 new(ctx
) ir_expression(ir_unop_b2i64
,
900 case GLSL_TYPE_FLOAT
:
901 result
= new(ctx
) ir_expression(ir_unop_f2u64
, src
);
903 case GLSL_TYPE_DOUBLE
:
904 result
= new(ctx
) ir_expression(ir_unop_d2u64
, src
);
906 case GLSL_TYPE_INT64
:
907 result
= new(ctx
) ir_expression(ir_unop_i642u64
, src
);
911 case GLSL_TYPE_INT64
:
914 result
= new(ctx
) ir_expression(ir_unop_i2i64
, src
);
917 result
= new(ctx
) ir_expression(ir_unop_u2i64
, src
);
920 result
= new(ctx
) ir_expression(ir_unop_b2i64
, src
);
922 case GLSL_TYPE_FLOAT
:
923 result
= new(ctx
) ir_expression(ir_unop_f2i64
, src
);
925 case GLSL_TYPE_DOUBLE
:
926 result
= new(ctx
) ir_expression(ir_unop_d2i64
, src
);
928 case GLSL_TYPE_UINT64
:
929 result
= new(ctx
) ir_expression(ir_unop_u642i64
, src
);
933 case GLSL_TYPE_SAMPLER
:
937 ir_expression(ir_unop_pack_sampler_2x32
, desired_type
, src
);
941 case GLSL_TYPE_IMAGE
:
945 ir_expression(ir_unop_pack_image_2x32
, desired_type
, src
);
951 assert(result
!= NULL
);
952 assert(result
->type
== desired_type
);
954 /* Try constant folding; it may fold in the conversion we just added. */
955 ir_constant
*const constant
= result
->constant_expression_value(ctx
);
956 return (constant
!= NULL
) ? (ir_rvalue
*) constant
: (ir_rvalue
*) result
;
961 * Perform automatic type and constant conversion of constructor parameters
963 * This implements the rules in the "Implicit Conversions" rules, not the
964 * "Conversion and Scalar Constructors".
966 * After attempting the implicit conversion, an attempt to convert into a
967 * constant valued expression is also done.
969 * The \c from \c ir_rvalue is converted "in place".
971 * \param from Operand that is being converted
972 * \param to Base type the operand will be converted to
973 * \param state GLSL compiler state
976 * If the attempt to convert into a constant expression succeeds, \c true is
977 * returned. Otherwise \c false is returned.
980 implicitly_convert_component(ir_rvalue
* &from
, const glsl_base_type to
,
981 struct _mesa_glsl_parse_state
*state
)
983 void *mem_ctx
= state
;
984 ir_rvalue
*result
= from
;
986 if (to
!= from
->type
->base_type
) {
987 const glsl_type
*desired_type
=
988 glsl_type::get_instance(to
,
989 from
->type
->vector_elements
,
990 from
->type
->matrix_columns
);
992 if (from
->type
->can_implicitly_convert_to(desired_type
, state
)) {
993 /* Even though convert_component() implements the constructor
994 * conversion rules (not the implicit conversion rules), its safe
995 * to use it here because we already checked that the implicit
996 * conversion is legal.
998 result
= convert_component(from
, desired_type
);
1002 ir_rvalue
*const constant
= result
->constant_expression_value(mem_ctx
);
1004 if (constant
!= NULL
)
1007 if (from
!= result
) {
1008 from
->replace_with(result
);
1012 return constant
!= NULL
;
1017 * Dereference a specific component from a scalar, vector, or matrix
1020 dereference_component(ir_rvalue
*src
, unsigned component
)
1022 void *ctx
= ralloc_parent(src
);
1023 assert(component
< src
->type
->components());
1025 /* If the source is a constant, just create a new constant instead of a
1026 * dereference of the existing constant.
1028 ir_constant
*constant
= src
->as_constant();
1030 return new(ctx
) ir_constant(constant
, component
);
1032 if (src
->type
->is_scalar()) {
1034 } else if (src
->type
->is_vector()) {
1035 return new(ctx
) ir_swizzle(src
, component
, 0, 0, 0, 1);
1037 assert(src
->type
->is_matrix());
1039 /* Dereference a row of the matrix, then call this function again to get
1040 * a specific element from that row.
1042 const int c
= component
/ src
->type
->column_type()->vector_elements
;
1043 const int r
= component
% src
->type
->column_type()->vector_elements
;
1044 ir_constant
*const col_index
= new(ctx
) ir_constant(c
);
1045 ir_dereference
*const col
= new(ctx
) ir_dereference_array(src
,
1048 col
->type
= src
->type
->column_type();
1050 return dereference_component(col
, r
);
1053 assert(!"Should not get here.");
1059 process_vec_mat_constructor(exec_list
*instructions
,
1060 const glsl_type
*constructor_type
,
1061 YYLTYPE
*loc
, exec_list
*parameters
,
1062 struct _mesa_glsl_parse_state
*state
)
1066 /* The ARB_shading_language_420pack spec says:
1068 * "If an initializer is a list of initializers enclosed in curly braces,
1069 * the variable being declared must be a vector, a matrix, an array, or a
1072 * int i = { 1 }; // illegal, i is not an aggregate"
1074 if (constructor_type
->vector_elements
<= 1) {
1075 _mesa_glsl_error(loc
, state
, "aggregates can only initialize vectors, "
1076 "matrices, arrays, and structs");
1077 return ir_rvalue::error_value(ctx
);
1080 exec_list actual_parameters
;
1081 const unsigned parameter_count
=
1082 process_parameters(instructions
, &actual_parameters
, parameters
, state
);
1084 if (parameter_count
== 0
1085 || (constructor_type
->is_vector() &&
1086 constructor_type
->vector_elements
!= parameter_count
)
1087 || (constructor_type
->is_matrix() &&
1088 constructor_type
->matrix_columns
!= parameter_count
)) {
1089 _mesa_glsl_error(loc
, state
, "%s constructor must have %u parameters",
1090 constructor_type
->is_vector() ? "vector" : "matrix",
1091 constructor_type
->vector_elements
);
1092 return ir_rvalue::error_value(ctx
);
1095 bool all_parameters_are_constant
= true;
1097 /* Type cast each parameter and, if possible, fold constants. */
1098 foreach_in_list_safe(ir_rvalue
, ir
, &actual_parameters
) {
1099 /* Apply implicit conversions (not the scalar constructor rules, see the
1100 * spec quote above!) and attempt to convert the parameter to a constant
1101 * valued expression. After doing so, track whether or not all the
1102 * parameters to the constructor are trivially constant valued
1105 all_parameters_are_constant
&=
1106 implicitly_convert_component(ir
, constructor_type
->base_type
, state
);
1108 if (constructor_type
->is_matrix()) {
1109 if (ir
->type
!= constructor_type
->column_type()) {
1110 _mesa_glsl_error(loc
, state
, "type error in matrix constructor: "
1111 "expected: %s, found %s",
1112 constructor_type
->column_type()->name
,
1114 return ir_rvalue::error_value(ctx
);
1116 } else if (ir
->type
!= constructor_type
->get_scalar_type()) {
1117 _mesa_glsl_error(loc
, state
, "type error in vector constructor: "
1118 "expected: %s, found %s",
1119 constructor_type
->get_scalar_type()->name
,
1121 return ir_rvalue::error_value(ctx
);
1125 if (all_parameters_are_constant
)
1126 return new(ctx
) ir_constant(constructor_type
, &actual_parameters
);
1128 ir_variable
*var
= new(ctx
) ir_variable(constructor_type
, "vec_mat_ctor",
1130 instructions
->push_tail(var
);
1134 foreach_in_list(ir_rvalue
, rhs
, &actual_parameters
) {
1135 ir_instruction
*assignment
= NULL
;
1137 if (var
->type
->is_matrix()) {
1139 new(ctx
) ir_dereference_array(var
, new(ctx
) ir_constant(i
));
1140 assignment
= new(ctx
) ir_assignment(lhs
, rhs
);
1142 /* use writemask rather than index for vector */
1143 assert(var
->type
->is_vector());
1145 ir_dereference
*lhs
= new(ctx
) ir_dereference_variable(var
);
1146 assignment
= new(ctx
) ir_assignment(lhs
, rhs
, NULL
,
1147 (unsigned)(1 << i
));
1150 instructions
->push_tail(assignment
);
1155 return new(ctx
) ir_dereference_variable(var
);
1160 process_array_constructor(exec_list
*instructions
,
1161 const glsl_type
*constructor_type
,
1162 YYLTYPE
*loc
, exec_list
*parameters
,
1163 struct _mesa_glsl_parse_state
*state
)
1166 /* Array constructors come in two forms: sized and unsized. Sized array
1167 * constructors look like 'vec4[2](a, b)', where 'a' and 'b' are vec4
1168 * variables. In this case the number of parameters must exactly match the
1169 * specified size of the array.
1171 * Unsized array constructors look like 'vec4[](a, b)', where 'a' and 'b'
1172 * are vec4 variables. In this case the size of the array being constructed
1173 * is determined by the number of parameters.
1175 * From page 52 (page 58 of the PDF) of the GLSL 1.50 spec:
1177 * "There must be exactly the same number of arguments as the size of
1178 * the array being constructed. If no size is present in the
1179 * constructor, then the array is explicitly sized to the number of
1180 * arguments provided. The arguments are assigned in order, starting at
1181 * element 0, to the elements of the constructed array. Each argument
1182 * must be the same type as the element type of the array, or be a type
1183 * that can be converted to the element type of the array according to
1184 * Section 4.1.10 "Implicit Conversions.""
1186 exec_list actual_parameters
;
1187 const unsigned parameter_count
=
1188 process_parameters(instructions
, &actual_parameters
, parameters
, state
);
1189 bool is_unsized_array
= constructor_type
->is_unsized_array();
1191 if ((parameter_count
== 0) ||
1192 (!is_unsized_array
&& (constructor_type
->length
!= parameter_count
))) {
1193 const unsigned min_param
= is_unsized_array
1194 ? 1 : constructor_type
->length
;
1196 _mesa_glsl_error(loc
, state
, "array constructor must have %s %u "
1198 is_unsized_array
? "at least" : "exactly",
1199 min_param
, (min_param
<= 1) ? "" : "s");
1200 return ir_rvalue::error_value(ctx
);
1203 if (is_unsized_array
) {
1205 glsl_type::get_array_instance(constructor_type
->fields
.array
,
1207 assert(constructor_type
!= NULL
);
1208 assert(constructor_type
->length
== parameter_count
);
1211 bool all_parameters_are_constant
= true;
1212 const glsl_type
*element_type
= constructor_type
->fields
.array
;
1214 /* Type cast each parameter and, if possible, fold constants. */
1215 foreach_in_list_safe(ir_rvalue
, ir
, &actual_parameters
) {
1216 /* Apply implicit conversions (not the scalar constructor rules, see the
1217 * spec quote above!) and attempt to convert the parameter to a constant
1218 * valued expression. After doing so, track whether or not all the
1219 * parameters to the constructor are trivially constant valued
1222 all_parameters_are_constant
&=
1223 implicitly_convert_component(ir
, element_type
->base_type
, state
);
1225 if (constructor_type
->fields
.array
->is_unsized_array()) {
1226 /* As the inner parameters of the constructor are created without
1227 * knowledge of each other we need to check to make sure unsized
1228 * parameters of unsized constructors all end up with the same size.
1230 * e.g we make sure to fail for a constructor like this:
1231 * vec4[][] a = vec4[][](vec4[](vec4(0.0), vec4(1.0)),
1232 * vec4[](vec4(0.0), vec4(1.0), vec4(1.0)),
1233 * vec4[](vec4(0.0), vec4(1.0)));
1235 if (element_type
->is_unsized_array()) {
1236 /* This is the first parameter so just get the type */
1237 element_type
= ir
->type
;
1238 } else if (element_type
!= ir
->type
) {
1239 _mesa_glsl_error(loc
, state
, "type error in array constructor: "
1240 "expected: %s, found %s",
1243 return ir_rvalue::error_value(ctx
);
1245 } else if (ir
->type
!= constructor_type
->fields
.array
) {
1246 _mesa_glsl_error(loc
, state
, "type error in array constructor: "
1247 "expected: %s, found %s",
1248 constructor_type
->fields
.array
->name
,
1250 return ir_rvalue::error_value(ctx
);
1252 element_type
= ir
->type
;
1256 if (constructor_type
->fields
.array
->is_unsized_array()) {
1258 glsl_type::get_array_instance(element_type
,
1260 assert(constructor_type
!= NULL
);
1261 assert(constructor_type
->length
== parameter_count
);
1264 if (all_parameters_are_constant
)
1265 return new(ctx
) ir_constant(constructor_type
, &actual_parameters
);
1267 ir_variable
*var
= new(ctx
) ir_variable(constructor_type
, "array_ctor",
1269 instructions
->push_tail(var
);
1272 foreach_in_list(ir_rvalue
, rhs
, &actual_parameters
) {
1273 ir_rvalue
*lhs
= new(ctx
) ir_dereference_array(var
,
1274 new(ctx
) ir_constant(i
));
1276 ir_instruction
*assignment
= new(ctx
) ir_assignment(lhs
, rhs
);
1277 instructions
->push_tail(assignment
);
1282 return new(ctx
) ir_dereference_variable(var
);
1287 * Determine if a list consists of a single scalar r-value
1290 single_scalar_parameter(exec_list
*parameters
)
1292 const ir_rvalue
*const p
= (ir_rvalue
*) parameters
->get_head_raw();
1293 assert(((ir_rvalue
*)p
)->as_rvalue() != NULL
);
1295 return (p
->type
->is_scalar() && p
->next
->is_tail_sentinel());
1300 * Generate inline code for a vector constructor
1302 * The generated constructor code will consist of a temporary variable
1303 * declaration of the same type as the constructor. A sequence of assignments
1304 * from constructor parameters to the temporary will follow.
1307 * An \c ir_dereference_variable of the temprorary generated in the constructor
1311 emit_inline_vector_constructor(const glsl_type
*type
,
1312 exec_list
*instructions
,
1313 exec_list
*parameters
,
1316 assert(!parameters
->is_empty());
1318 ir_variable
*var
= new(ctx
) ir_variable(type
, "vec_ctor", ir_var_temporary
);
1319 instructions
->push_tail(var
);
1321 /* There are three kinds of vector constructors.
1323 * - Construct a vector from a single scalar by replicating that scalar to
1324 * all components of the vector.
1326 * - Construct a vector from at least a matrix. This case should already
1327 * have been taken care of in ast_function_expression::hir by breaking
1328 * down the matrix into a series of column vectors.
1330 * - Construct a vector from an arbirary combination of vectors and
1331 * scalars. The components of the constructor parameters are assigned
1332 * to the vector in order until the vector is full.
1334 const unsigned lhs_components
= type
->components();
1335 if (single_scalar_parameter(parameters
)) {
1336 ir_rvalue
*first_param
= (ir_rvalue
*)parameters
->get_head_raw();
1337 ir_rvalue
*rhs
= new(ctx
) ir_swizzle(first_param
, 0, 0, 0, 0,
1339 ir_dereference_variable
*lhs
= new(ctx
) ir_dereference_variable(var
);
1340 const unsigned mask
= (1U << lhs_components
) - 1;
1342 assert(rhs
->type
== lhs
->type
);
1344 ir_instruction
*inst
= new(ctx
) ir_assignment(lhs
, rhs
, NULL
, mask
);
1345 instructions
->push_tail(inst
);
1347 unsigned base_component
= 0;
1348 unsigned base_lhs_component
= 0;
1349 ir_constant_data data
;
1350 unsigned constant_mask
= 0, constant_components
= 0;
1352 memset(&data
, 0, sizeof(data
));
1354 foreach_in_list(ir_rvalue
, param
, parameters
) {
1355 unsigned rhs_components
= param
->type
->components();
1357 /* Do not try to assign more components to the vector than it has! */
1358 if ((rhs_components
+ base_lhs_component
) > lhs_components
) {
1359 rhs_components
= lhs_components
- base_lhs_component
;
1362 const ir_constant
*const c
= param
->as_constant();
1364 for (unsigned i
= 0; i
< rhs_components
; i
++) {
1365 switch (c
->type
->base_type
) {
1366 case GLSL_TYPE_UINT
:
1367 data
.u
[i
+ base_component
] = c
->get_uint_component(i
);
1370 data
.i
[i
+ base_component
] = c
->get_int_component(i
);
1372 case GLSL_TYPE_FLOAT
:
1373 data
.f
[i
+ base_component
] = c
->get_float_component(i
);
1375 case GLSL_TYPE_DOUBLE
:
1376 data
.d
[i
+ base_component
] = c
->get_double_component(i
);
1378 case GLSL_TYPE_BOOL
:
1379 data
.b
[i
+ base_component
] = c
->get_bool_component(i
);
1381 case GLSL_TYPE_UINT64
:
1382 data
.u64
[i
+ base_component
] = c
->get_uint64_component(i
);
1384 case GLSL_TYPE_INT64
:
1385 data
.i64
[i
+ base_component
] = c
->get_int64_component(i
);
1388 assert(!"Should not get here.");
1393 /* Mask of fields to be written in the assignment. */
1394 constant_mask
|= ((1U << rhs_components
) - 1) << base_lhs_component
;
1395 constant_components
+= rhs_components
;
1397 base_component
+= rhs_components
;
1399 /* Advance the component index by the number of components
1400 * that were just assigned.
1402 base_lhs_component
+= rhs_components
;
1405 if (constant_mask
!= 0) {
1406 ir_dereference
*lhs
= new(ctx
) ir_dereference_variable(var
);
1407 const glsl_type
*rhs_type
=
1408 glsl_type::get_instance(var
->type
->base_type
,
1409 constant_components
,
1411 ir_rvalue
*rhs
= new(ctx
) ir_constant(rhs_type
, &data
);
1413 ir_instruction
*inst
=
1414 new(ctx
) ir_assignment(lhs
, rhs
, NULL
, constant_mask
);
1415 instructions
->push_tail(inst
);
1419 foreach_in_list(ir_rvalue
, param
, parameters
) {
1420 unsigned rhs_components
= param
->type
->components();
1422 /* Do not try to assign more components to the vector than it has! */
1423 if ((rhs_components
+ base_component
) > lhs_components
) {
1424 rhs_components
= lhs_components
- base_component
;
1427 /* If we do not have any components left to copy, break out of the
1428 * loop. This can happen when initializing a vec4 with a mat3 as the
1429 * mat3 would have been broken into a series of column vectors.
1431 if (rhs_components
== 0) {
1435 const ir_constant
*const c
= param
->as_constant();
1437 /* Mask of fields to be written in the assignment. */
1438 const unsigned write_mask
= ((1U << rhs_components
) - 1)
1441 ir_dereference
*lhs
= new(ctx
) ir_dereference_variable(var
);
1443 /* Generate a swizzle so that LHS and RHS sizes match. */
1445 new(ctx
) ir_swizzle(param
, 0, 1, 2, 3, rhs_components
);
1447 ir_instruction
*inst
=
1448 new(ctx
) ir_assignment(lhs
, rhs
, NULL
, write_mask
);
1449 instructions
->push_tail(inst
);
1452 /* Advance the component index by the number of components that were
1455 base_component
+= rhs_components
;
1458 return new(ctx
) ir_dereference_variable(var
);
1463 * Generate assignment of a portion of a vector to a portion of a matrix column
1465 * \param src_base First component of the source to be used in assignment
1466 * \param column Column of destination to be assiged
1467 * \param row_base First component of the destination column to be assigned
1468 * \param count Number of components to be assigned
1471 * \c src_base + \c count must be less than or equal to the number of
1472 * components in the source vector.
1474 static ir_instruction
*
1475 assign_to_matrix_column(ir_variable
*var
, unsigned column
, unsigned row_base
,
1476 ir_rvalue
*src
, unsigned src_base
, unsigned count
,
1479 ir_constant
*col_idx
= new(mem_ctx
) ir_constant(column
);
1480 ir_dereference
*column_ref
= new(mem_ctx
) ir_dereference_array(var
,
1483 assert(column_ref
->type
->components() >= (row_base
+ count
));
1484 assert(src
->type
->components() >= (src_base
+ count
));
1486 /* Generate a swizzle that extracts the number of components from the source
1487 * that are to be assigned to the column of the matrix.
1489 if (count
< src
->type
->vector_elements
) {
1490 src
= new(mem_ctx
) ir_swizzle(src
,
1491 src_base
+ 0, src_base
+ 1,
1492 src_base
+ 2, src_base
+ 3,
1496 /* Mask of fields to be written in the assignment. */
1497 const unsigned write_mask
= ((1U << count
) - 1) << row_base
;
1499 return new(mem_ctx
) ir_assignment(column_ref
, src
, NULL
, write_mask
);
1504 * Generate inline code for a matrix constructor
1506 * The generated constructor code will consist of a temporary variable
1507 * declaration of the same type as the constructor. A sequence of assignments
1508 * from constructor parameters to the temporary will follow.
1511 * An \c ir_dereference_variable of the temprorary generated in the constructor
1515 emit_inline_matrix_constructor(const glsl_type
*type
,
1516 exec_list
*instructions
,
1517 exec_list
*parameters
,
1520 assert(!parameters
->is_empty());
1522 ir_variable
*var
= new(ctx
) ir_variable(type
, "mat_ctor", ir_var_temporary
);
1523 instructions
->push_tail(var
);
1525 /* There are three kinds of matrix constructors.
1527 * - Construct a matrix from a single scalar by replicating that scalar to
1528 * along the diagonal of the matrix and setting all other components to
1531 * - Construct a matrix from an arbirary combination of vectors and
1532 * scalars. The components of the constructor parameters are assigned
1533 * to the matrix in column-major order until the matrix is full.
1535 * - Construct a matrix from a single matrix. The source matrix is copied
1536 * to the upper left portion of the constructed matrix, and the remaining
1537 * elements take values from the identity matrix.
1539 ir_rvalue
*const first_param
= (ir_rvalue
*) parameters
->get_head_raw();
1540 if (single_scalar_parameter(parameters
)) {
1541 /* Assign the scalar to the X component of a vec4, and fill the remaining
1542 * components with zero.
1544 glsl_base_type param_base_type
= first_param
->type
->base_type
;
1545 assert(first_param
->type
->is_float() || first_param
->type
->is_double());
1546 ir_variable
*rhs_var
=
1547 new(ctx
) ir_variable(glsl_type::get_instance(param_base_type
, 4, 1),
1550 instructions
->push_tail(rhs_var
);
1552 ir_constant_data zero
;
1553 for (unsigned i
= 0; i
< 4; i
++)
1554 if (first_param
->type
->is_float())
1559 ir_instruction
*inst
=
1560 new(ctx
) ir_assignment(new(ctx
) ir_dereference_variable(rhs_var
),
1561 new(ctx
) ir_constant(rhs_var
->type
, &zero
));
1562 instructions
->push_tail(inst
);
1564 ir_dereference
*const rhs_ref
=
1565 new(ctx
) ir_dereference_variable(rhs_var
);
1567 inst
= new(ctx
) ir_assignment(rhs_ref
, first_param
, NULL
, 0x01);
1568 instructions
->push_tail(inst
);
1570 /* Assign the temporary vector to each column of the destination matrix
1571 * with a swizzle that puts the X component on the diagonal of the
1572 * matrix. In some cases this may mean that the X component does not
1573 * get assigned into the column at all (i.e., when the matrix has more
1574 * columns than rows).
1576 static const unsigned rhs_swiz
[4][4] = {
1583 const unsigned cols_to_init
= MIN2(type
->matrix_columns
,
1584 type
->vector_elements
);
1585 for (unsigned i
= 0; i
< cols_to_init
; i
++) {
1586 ir_constant
*const col_idx
= new(ctx
) ir_constant(i
);
1587 ir_rvalue
*const col_ref
= new(ctx
) ir_dereference_array(var
,
1590 ir_rvalue
*const rhs_ref
= new(ctx
) ir_dereference_variable(rhs_var
);
1591 ir_rvalue
*const rhs
= new(ctx
) ir_swizzle(rhs_ref
, rhs_swiz
[i
],
1592 type
->vector_elements
);
1594 inst
= new(ctx
) ir_assignment(col_ref
, rhs
);
1595 instructions
->push_tail(inst
);
1598 for (unsigned i
= cols_to_init
; i
< type
->matrix_columns
; i
++) {
1599 ir_constant
*const col_idx
= new(ctx
) ir_constant(i
);
1600 ir_rvalue
*const col_ref
= new(ctx
) ir_dereference_array(var
,
1603 ir_rvalue
*const rhs_ref
= new(ctx
) ir_dereference_variable(rhs_var
);
1604 ir_rvalue
*const rhs
= new(ctx
) ir_swizzle(rhs_ref
, 1, 1, 1, 1,
1605 type
->vector_elements
);
1607 inst
= new(ctx
) ir_assignment(col_ref
, rhs
);
1608 instructions
->push_tail(inst
);
1610 } else if (first_param
->type
->is_matrix()) {
1611 /* From page 50 (56 of the PDF) of the GLSL 1.50 spec:
1613 * "If a matrix is constructed from a matrix, then each component
1614 * (column i, row j) in the result that has a corresponding
1615 * component (column i, row j) in the argument will be initialized
1616 * from there. All other components will be initialized to the
1617 * identity matrix. If a matrix argument is given to a matrix
1618 * constructor, it is an error to have any other arguments."
1620 assert(first_param
->next
->is_tail_sentinel());
1621 ir_rvalue
*const src_matrix
= first_param
;
1623 /* If the source matrix is smaller, pre-initialize the relavent parts of
1624 * the destination matrix to the identity matrix.
1626 if ((src_matrix
->type
->matrix_columns
< var
->type
->matrix_columns
) ||
1627 (src_matrix
->type
->vector_elements
< var
->type
->vector_elements
)) {
1629 /* If the source matrix has fewer rows, every column of the
1630 * destination must be initialized. Otherwise only the columns in
1631 * the destination that do not exist in the source must be
1635 (src_matrix
->type
->vector_elements
< var
->type
->vector_elements
)
1636 ? 0 : src_matrix
->type
->matrix_columns
;
1638 const glsl_type
*const col_type
= var
->type
->column_type();
1639 for (/* empty */; col
< var
->type
->matrix_columns
; col
++) {
1640 ir_constant_data ident
;
1642 if (!col_type
->is_double()) {
1647 ident
.f
[col
] = 1.0f
;
1656 ir_rvalue
*const rhs
= new(ctx
) ir_constant(col_type
, &ident
);
1658 ir_rvalue
*const lhs
=
1659 new(ctx
) ir_dereference_array(var
, new(ctx
) ir_constant(col
));
1661 ir_instruction
*inst
= new(ctx
) ir_assignment(lhs
, rhs
);
1662 instructions
->push_tail(inst
);
1666 /* Assign columns from the source matrix to the destination matrix.
1668 * Since the parameter will be used in the RHS of multiple assignments,
1669 * generate a temporary and copy the paramter there.
1671 ir_variable
*const rhs_var
=
1672 new(ctx
) ir_variable(first_param
->type
, "mat_ctor_mat",
1674 instructions
->push_tail(rhs_var
);
1676 ir_dereference
*const rhs_var_ref
=
1677 new(ctx
) ir_dereference_variable(rhs_var
);
1678 ir_instruction
*const inst
=
1679 new(ctx
) ir_assignment(rhs_var_ref
, first_param
);
1680 instructions
->push_tail(inst
);
1682 const unsigned last_row
= MIN2(src_matrix
->type
->vector_elements
,
1683 var
->type
->vector_elements
);
1684 const unsigned last_col
= MIN2(src_matrix
->type
->matrix_columns
,
1685 var
->type
->matrix_columns
);
1687 unsigned swiz
[4] = { 0, 0, 0, 0 };
1688 for (unsigned i
= 1; i
< last_row
; i
++)
1691 const unsigned write_mask
= (1U << last_row
) - 1;
1693 for (unsigned i
= 0; i
< last_col
; i
++) {
1694 ir_dereference
*const lhs
=
1695 new(ctx
) ir_dereference_array(var
, new(ctx
) ir_constant(i
));
1696 ir_rvalue
*const rhs_col
=
1697 new(ctx
) ir_dereference_array(rhs_var
, new(ctx
) ir_constant(i
));
1699 /* If one matrix has columns that are smaller than the columns of the
1700 * other matrix, wrap the column access of the larger with a swizzle
1701 * so that the LHS and RHS of the assignment have the same size (and
1702 * therefore have the same type).
1704 * It would be perfectly valid to unconditionally generate the
1705 * swizzles, this this will typically result in a more compact IR
1709 if (lhs
->type
->vector_elements
!= rhs_col
->type
->vector_elements
) {
1710 rhs
= new(ctx
) ir_swizzle(rhs_col
, swiz
, last_row
);
1715 ir_instruction
*inst
=
1716 new(ctx
) ir_assignment(lhs
, rhs
, NULL
, write_mask
);
1717 instructions
->push_tail(inst
);
1720 const unsigned cols
= type
->matrix_columns
;
1721 const unsigned rows
= type
->vector_elements
;
1722 unsigned remaining_slots
= rows
* cols
;
1723 unsigned col_idx
= 0;
1724 unsigned row_idx
= 0;
1726 foreach_in_list(ir_rvalue
, rhs
, parameters
) {
1727 unsigned rhs_components
= rhs
->type
->components();
1728 unsigned rhs_base
= 0;
1730 if (remaining_slots
== 0)
1733 /* Since the parameter might be used in the RHS of two assignments,
1734 * generate a temporary and copy the paramter there.
1736 ir_variable
*rhs_var
=
1737 new(ctx
) ir_variable(rhs
->type
, "mat_ctor_vec", ir_var_temporary
);
1738 instructions
->push_tail(rhs_var
);
1740 ir_dereference
*rhs_var_ref
=
1741 new(ctx
) ir_dereference_variable(rhs_var
);
1742 ir_instruction
*inst
= new(ctx
) ir_assignment(rhs_var_ref
, rhs
);
1743 instructions
->push_tail(inst
);
1746 /* Assign the current parameter to as many components of the matrix
1749 * NOTE: A single vector parameter can span two matrix columns. A
1750 * single vec4, for example, can completely fill a mat2.
1752 unsigned count
= MIN2(rows
- row_idx
,
1753 rhs_components
- rhs_base
);
1755 rhs_var_ref
= new(ctx
) ir_dereference_variable(rhs_var
);
1756 ir_instruction
*inst
= assign_to_matrix_column(var
, col_idx
,
1761 instructions
->push_tail(inst
);
1764 remaining_slots
-= count
;
1766 /* Sometimes, there is still data left in the parameters and
1767 * components left to be set in the destination but in other
1770 if (row_idx
>= rows
) {
1774 } while(remaining_slots
> 0 && rhs_base
< rhs_components
);
1778 return new(ctx
) ir_dereference_variable(var
);
1783 emit_inline_record_constructor(const glsl_type
*type
,
1784 exec_list
*instructions
,
1785 exec_list
*parameters
,
1788 ir_variable
*const var
=
1789 new(mem_ctx
) ir_variable(type
, "record_ctor", ir_var_temporary
);
1790 ir_dereference_variable
*const d
=
1791 new(mem_ctx
) ir_dereference_variable(var
);
1793 instructions
->push_tail(var
);
1795 exec_node
*node
= parameters
->get_head_raw();
1796 for (unsigned i
= 0; i
< type
->length
; i
++) {
1797 assert(!node
->is_tail_sentinel());
1799 ir_dereference
*const lhs
=
1800 new(mem_ctx
) ir_dereference_record(d
->clone(mem_ctx
, NULL
),
1801 type
->fields
.structure
[i
].name
);
1803 ir_rvalue
*const rhs
= ((ir_instruction
*) node
)->as_rvalue();
1804 assert(rhs
!= NULL
);
1806 ir_instruction
*const assign
= new(mem_ctx
) ir_assignment(lhs
, rhs
);
1808 instructions
->push_tail(assign
);
1817 process_record_constructor(exec_list
*instructions
,
1818 const glsl_type
*constructor_type
,
1819 YYLTYPE
*loc
, exec_list
*parameters
,
1820 struct _mesa_glsl_parse_state
*state
)
1823 /* From page 32 (page 38 of the PDF) of the GLSL 1.20 spec:
1825 * "The arguments to the constructor will be used to set the structure's
1826 * fields, in order, using one argument per field. Each argument must
1827 * be the same type as the field it sets, or be a type that can be
1828 * converted to the field's type according to Section 4.1.10 “Implicit
1831 * From page 35 (page 41 of the PDF) of the GLSL 4.20 spec:
1833 * "In all cases, the innermost initializer (i.e., not a list of
1834 * initializers enclosed in curly braces) applied to an object must
1835 * have the same type as the object being initialized or be a type that
1836 * can be converted to the object's type according to section 4.1.10
1837 * "Implicit Conversions". In the latter case, an implicit conversion
1838 * will be done on the initializer before the assignment is done."
1840 exec_list actual_parameters
;
1842 const unsigned parameter_count
=
1843 process_parameters(instructions
, &actual_parameters
, parameters
,
1846 if (parameter_count
!= constructor_type
->length
) {
1847 _mesa_glsl_error(loc
, state
,
1848 "%s parameters in constructor for `%s'",
1849 parameter_count
> constructor_type
->length
1850 ? "too many": "insufficient",
1851 constructor_type
->name
);
1852 return ir_rvalue::error_value(ctx
);
1855 bool all_parameters_are_constant
= true;
1858 /* Type cast each parameter and, if possible, fold constants. */
1859 foreach_in_list_safe(ir_rvalue
, ir
, &actual_parameters
) {
1861 const glsl_struct_field
*struct_field
=
1862 &constructor_type
->fields
.structure
[i
];
1864 /* Apply implicit conversions (not the scalar constructor rules, see the
1865 * spec quote above!) and attempt to convert the parameter to a constant
1866 * valued expression. After doing so, track whether or not all the
1867 * parameters to the constructor are trivially constant valued
1870 all_parameters_are_constant
&=
1871 implicitly_convert_component(ir
, struct_field
->type
->base_type
,
1874 if (ir
->type
!= struct_field
->type
) {
1875 _mesa_glsl_error(loc
, state
,
1876 "parameter type mismatch in constructor for `%s.%s' "
1878 constructor_type
->name
,
1881 struct_field
->type
->name
);
1882 return ir_rvalue::error_value(ctx
);
1888 if (all_parameters_are_constant
) {
1889 return new(ctx
) ir_constant(constructor_type
, &actual_parameters
);
1891 return emit_inline_record_constructor(constructor_type
, instructions
,
1892 &actual_parameters
, state
);
1897 ast_function_expression::handle_method(exec_list
*instructions
,
1898 struct _mesa_glsl_parse_state
*state
)
1900 const ast_expression
*field
= subexpressions
[0];
1904 /* Handle "method calls" in GLSL 1.20 - namely, array.length() */
1905 YYLTYPE loc
= get_location();
1906 state
->check_version(120, 300, &loc
, "methods not supported");
1909 method
= field
->primary_expression
.identifier
;
1911 /* This would prevent to raise "uninitialized variable" warnings when
1912 * calling array.length.
1914 field
->subexpressions
[0]->set_is_lhs(true);
1915 op
= field
->subexpressions
[0]->hir(instructions
, state
);
1916 if (strcmp(method
, "length") == 0) {
1917 if (!this->expressions
.is_empty()) {
1918 _mesa_glsl_error(&loc
, state
, "length method takes no arguments");
1922 if (op
->type
->is_array()) {
1923 if (op
->type
->is_unsized_array()) {
1924 if (!state
->has_shader_storage_buffer_objects()) {
1925 _mesa_glsl_error(&loc
, state
,
1926 "length called on unsized array"
1927 " only available with"
1928 " ARB_shader_storage_buffer_object");
1930 /* Calculate length of an unsized array in run-time */
1931 result
= new(ctx
) ir_expression(ir_unop_ssbo_unsized_array_length
,
1934 result
= new(ctx
) ir_constant(op
->type
->array_size());
1936 } else if (op
->type
->is_vector()) {
1937 if (state
->has_420pack()) {
1938 /* .length() returns int. */
1939 result
= new(ctx
) ir_constant((int) op
->type
->vector_elements
);
1941 _mesa_glsl_error(&loc
, state
, "length method on matrix only"
1942 " available with ARB_shading_language_420pack");
1945 } else if (op
->type
->is_matrix()) {
1946 if (state
->has_420pack()) {
1947 /* .length() returns int. */
1948 result
= new(ctx
) ir_constant((int) op
->type
->matrix_columns
);
1950 _mesa_glsl_error(&loc
, state
, "length method on matrix only"
1951 " available with ARB_shading_language_420pack");
1955 _mesa_glsl_error(&loc
, state
, "length called on scalar.");
1959 _mesa_glsl_error(&loc
, state
, "unknown method: `%s'", method
);
1964 return ir_rvalue::error_value(ctx
);
1967 static inline bool is_valid_constructor(const glsl_type
*type
,
1968 struct _mesa_glsl_parse_state
*state
)
1970 return type
->is_numeric() || type
->is_boolean() ||
1971 (state
->has_bindless() && (type
->is_sampler() || type
->is_image()));
1975 ast_function_expression::hir(exec_list
*instructions
,
1976 struct _mesa_glsl_parse_state
*state
)
1979 /* There are three sorts of function calls.
1981 * 1. constructors - The first subexpression is an ast_type_specifier.
1982 * 2. methods - Only the .length() method of array types.
1983 * 3. functions - Calls to regular old functions.
1986 if (is_constructor()) {
1987 const ast_type_specifier
*type
=
1988 (ast_type_specifier
*) subexpressions
[0];
1989 YYLTYPE loc
= type
->get_location();
1992 const glsl_type
*const constructor_type
= type
->glsl_type(& name
, state
);
1994 /* constructor_type can be NULL if a variable with the same name as the
1995 * structure has come into scope.
1997 if (constructor_type
== NULL
) {
1998 _mesa_glsl_error(& loc
, state
, "unknown type `%s' (structure name "
1999 "may be shadowed by a variable with the same name)",
2001 return ir_rvalue::error_value(ctx
);
2005 /* Constructors for opaque types are illegal.
2007 * From section 4.1.7 of the ARB_bindless_texture spec:
2009 * "Samplers are represented using 64-bit integer handles, and may be "
2010 * converted to and from 64-bit integers using constructors."
2012 * From section 4.1.X of the ARB_bindless_texture spec:
2014 * "Images are represented using 64-bit integer handles, and may be
2015 * converted to and from 64-bit integers using constructors."
2017 if (constructor_type
->contains_atomic() ||
2018 (!state
->has_bindless() && constructor_type
->contains_opaque())) {
2019 _mesa_glsl_error(& loc
, state
, "cannot construct %s type `%s'",
2020 state
->has_bindless() ? "atomic" : "opaque",
2021 constructor_type
->name
);
2022 return ir_rvalue::error_value(ctx
);
2025 if (constructor_type
->is_subroutine()) {
2026 _mesa_glsl_error(& loc
, state
,
2027 "subroutine name cannot be a constructor `%s'",
2028 constructor_type
->name
);
2029 return ir_rvalue::error_value(ctx
);
2032 if (constructor_type
->is_array()) {
2033 if (!state
->check_version(120, 300, &loc
,
2034 "array constructors forbidden")) {
2035 return ir_rvalue::error_value(ctx
);
2038 return process_array_constructor(instructions
, constructor_type
,
2039 & loc
, &this->expressions
, state
);
2043 /* There are two kinds of constructor calls. Constructors for arrays and
2044 * structures must have the exact number of arguments with matching types
2045 * in the correct order. These constructors follow essentially the same
2046 * type matching rules as functions.
2048 * Constructors for built-in language types, such as mat4 and vec2, are
2049 * free form. The only requirements are that the parameters must provide
2050 * enough values of the correct scalar type and that no arguments are
2051 * given past the last used argument.
2053 * When using the C-style initializer syntax from GLSL 4.20, constructors
2054 * must have the exact number of arguments with matching types in the
2057 if (constructor_type
->is_record()) {
2058 return process_record_constructor(instructions
, constructor_type
,
2059 &loc
, &this->expressions
,
2063 if (!is_valid_constructor(constructor_type
, state
))
2064 return ir_rvalue::error_value(ctx
);
2066 /* Total number of components of the type being constructed. */
2067 const unsigned type_components
= constructor_type
->components();
2069 /* Number of components from parameters that have actually been
2070 * consumed. This is used to perform several kinds of error checking.
2072 unsigned components_used
= 0;
2074 unsigned matrix_parameters
= 0;
2075 unsigned nonmatrix_parameters
= 0;
2076 exec_list actual_parameters
;
2078 foreach_list_typed(ast_node
, ast
, link
, &this->expressions
) {
2079 ir_rvalue
*result
= ast
->hir(instructions
, state
);
2081 /* From page 50 (page 56 of the PDF) of the GLSL 1.50 spec:
2083 * "It is an error to provide extra arguments beyond this
2084 * last used argument."
2086 if (components_used
>= type_components
) {
2087 _mesa_glsl_error(& loc
, state
, "too many parameters to `%s' "
2089 constructor_type
->name
);
2090 return ir_rvalue::error_value(ctx
);
2093 if (!is_valid_constructor(result
->type
, state
)) {
2094 _mesa_glsl_error(& loc
, state
, "cannot construct `%s' from a "
2095 "non-numeric data type",
2096 constructor_type
->name
);
2097 return ir_rvalue::error_value(ctx
);
2100 /* Count the number of matrix and nonmatrix parameters. This
2101 * is used below to enforce some of the constructor rules.
2103 if (result
->type
->is_matrix())
2104 matrix_parameters
++;
2106 nonmatrix_parameters
++;
2108 actual_parameters
.push_tail(result
);
2109 components_used
+= result
->type
->components();
2112 /* From page 28 (page 34 of the PDF) of the GLSL 1.10 spec:
2114 * "It is an error to construct matrices from other matrices. This
2115 * is reserved for future use."
2117 if (matrix_parameters
> 0
2118 && constructor_type
->is_matrix()
2119 && !state
->check_version(120, 100, &loc
,
2120 "cannot construct `%s' from a matrix",
2121 constructor_type
->name
)) {
2122 return ir_rvalue::error_value(ctx
);
2125 /* From page 50 (page 56 of the PDF) of the GLSL 1.50 spec:
2127 * "If a matrix argument is given to a matrix constructor, it is
2128 * an error to have any other arguments."
2130 if ((matrix_parameters
> 0)
2131 && ((matrix_parameters
+ nonmatrix_parameters
) > 1)
2132 && constructor_type
->is_matrix()) {
2133 _mesa_glsl_error(& loc
, state
, "for matrix `%s' constructor, "
2134 "matrix must be only parameter",
2135 constructor_type
->name
);
2136 return ir_rvalue::error_value(ctx
);
2139 /* From page 28 (page 34 of the PDF) of the GLSL 1.10 spec:
2141 * "In these cases, there must be enough components provided in the
2142 * arguments to provide an initializer for every component in the
2143 * constructed value."
2145 if (components_used
< type_components
&& components_used
!= 1
2146 && matrix_parameters
== 0) {
2147 _mesa_glsl_error(& loc
, state
, "too few components to construct "
2149 constructor_type
->name
);
2150 return ir_rvalue::error_value(ctx
);
2153 /* Matrices can never be consumed as is by any constructor but matrix
2154 * constructors. If the constructor type is not matrix, always break the
2155 * matrix up into a series of column vectors.
2157 if (!constructor_type
->is_matrix()) {
2158 foreach_in_list_safe(ir_rvalue
, matrix
, &actual_parameters
) {
2159 if (!matrix
->type
->is_matrix())
2162 /* Create a temporary containing the matrix. */
2163 ir_variable
*var
= new(ctx
) ir_variable(matrix
->type
, "matrix_tmp",
2165 instructions
->push_tail(var
);
2166 instructions
->push_tail(
2167 new(ctx
) ir_assignment(new(ctx
) ir_dereference_variable(var
),
2169 var
->constant_value
= matrix
->constant_expression_value(ctx
);
2171 /* Replace the matrix with dereferences of its columns. */
2172 for (int i
= 0; i
< matrix
->type
->matrix_columns
; i
++) {
2173 matrix
->insert_before(
2174 new (ctx
) ir_dereference_array(var
,
2175 new(ctx
) ir_constant(i
)));
2181 bool all_parameters_are_constant
= true;
2183 /* Type cast each parameter and, if possible, fold constants.*/
2184 foreach_in_list_safe(ir_rvalue
, ir
, &actual_parameters
) {
2185 const glsl_type
*desired_type
;
2187 /* From section 5.4.1 of the ARB_bindless_texture spec:
2189 * "In the following four constructors, the low 32 bits of the sampler
2190 * type correspond to the .x component of the uvec2 and the high 32
2191 * bits correspond to the .y component."
2193 * uvec2(any sampler type) // Converts a sampler type to a
2194 * // pair of 32-bit unsigned integers
2195 * any sampler type(uvec2) // Converts a pair of 32-bit unsigned integers to
2197 * uvec2(any image type) // Converts an image type to a
2198 * // pair of 32-bit unsigned integers
2199 * any image type(uvec2) // Converts a pair of 32-bit unsigned integers to
2202 if (ir
->type
->is_sampler() || ir
->type
->is_image()) {
2203 /* Convert a sampler/image type to a pair of 32-bit unsigned
2204 * integers as defined by ARB_bindless_texture.
2206 if (constructor_type
!= glsl_type::uvec2_type
) {
2207 _mesa_glsl_error(&loc
, state
, "sampler and image types can only "
2208 "be converted to a pair of 32-bit unsigned "
2211 desired_type
= glsl_type::uvec2_type
;
2212 } else if (constructor_type
->is_sampler() ||
2213 constructor_type
->is_image()) {
2214 /* Convert a pair of 32-bit unsigned integers to a sampler or image
2215 * type as defined by ARB_bindless_texture.
2217 if (ir
->type
!= glsl_type::uvec2_type
) {
2218 _mesa_glsl_error(&loc
, state
, "sampler and image types can only "
2219 "be converted from a pair of 32-bit unsigned "
2222 desired_type
= constructor_type
;
2225 glsl_type::get_instance(constructor_type
->base_type
,
2226 ir
->type
->vector_elements
,
2227 ir
->type
->matrix_columns
);
2230 ir_rvalue
*result
= convert_component(ir
, desired_type
);
2232 /* Attempt to convert the parameter to a constant valued expression.
2233 * After doing so, track whether or not all the parameters to the
2234 * constructor are trivially constant valued expressions.
2236 ir_rvalue
*const constant
= result
->constant_expression_value(ctx
);
2238 if (constant
!= NULL
)
2241 all_parameters_are_constant
= false;
2244 ir
->replace_with(result
);
2248 /* If all of the parameters are trivially constant, create a
2249 * constant representing the complete collection of parameters.
2251 if (all_parameters_are_constant
) {
2252 return new(ctx
) ir_constant(constructor_type
, &actual_parameters
);
2253 } else if (constructor_type
->is_scalar()) {
2254 return dereference_component((ir_rvalue
*)
2255 actual_parameters
.get_head_raw(),
2257 } else if (constructor_type
->is_vector()) {
2258 return emit_inline_vector_constructor(constructor_type
,
2263 assert(constructor_type
->is_matrix());
2264 return emit_inline_matrix_constructor(constructor_type
,
2269 } else if (subexpressions
[0]->oper
== ast_field_selection
) {
2270 return handle_method(instructions
, state
);
2272 const ast_expression
*id
= subexpressions
[0];
2273 const char *func_name
= NULL
;
2274 YYLTYPE loc
= get_location();
2275 exec_list actual_parameters
;
2276 ir_variable
*sub_var
= NULL
;
2277 ir_rvalue
*array_idx
= NULL
;
2279 process_parameters(instructions
, &actual_parameters
, &this->expressions
,
2282 if (id
->oper
== ast_array_index
) {
2283 array_idx
= generate_array_index(ctx
, instructions
, state
, loc
,
2284 id
->subexpressions
[0],
2285 id
->subexpressions
[1], &func_name
,
2286 &actual_parameters
);
2287 } else if (id
->oper
== ast_identifier
) {
2288 func_name
= id
->primary_expression
.identifier
;
2290 _mesa_glsl_error(&loc
, state
, "function name is not an identifier");
2293 /* an error was emitted earlier */
2295 return ir_rvalue::error_value(ctx
);
2297 ir_function_signature
*sig
=
2298 match_function_by_name(func_name
, &actual_parameters
, state
);
2300 ir_rvalue
*value
= NULL
;
2302 sig
= match_subroutine_by_name(func_name
, &actual_parameters
,
2307 no_matching_function_error(func_name
, &loc
,
2308 &actual_parameters
, state
);
2309 value
= ir_rvalue::error_value(ctx
);
2310 } else if (!verify_parameter_modes(state
, sig
,
2312 this->expressions
)) {
2313 /* an error has already been emitted */
2314 value
= ir_rvalue::error_value(ctx
);
2315 } else if (sig
->is_builtin() && strcmp(func_name
, "ftransform") == 0) {
2316 /* ftransform refers to global variables, and we don't have any code
2317 * for remapping the variable references in the built-in shader.
2320 state
->symbols
->get_variable("gl_ModelViewProjectionMatrix");
2321 ir_variable
*vtx
= state
->symbols
->get_variable("gl_Vertex");
2322 value
= new(ctx
) ir_expression(ir_binop_mul
, glsl_type::vec4_type
,
2323 new(ctx
) ir_dereference_variable(mvp
),
2324 new(ctx
) ir_dereference_variable(vtx
));
2326 if (state
->stage
== MESA_SHADER_TESS_CTRL
&&
2327 sig
->is_builtin() && strcmp(func_name
, "barrier") == 0) {
2328 if (state
->current_function
== NULL
||
2329 strcmp(state
->current_function
->function_name(), "main") != 0) {
2330 _mesa_glsl_error(&loc
, state
,
2331 "barrier() may only be used in main()");
2334 if (state
->found_return
) {
2335 _mesa_glsl_error(&loc
, state
,
2336 "barrier() may not be used after return");
2339 if (instructions
!= &state
->current_function
->body
) {
2340 _mesa_glsl_error(&loc
, state
,
2341 "barrier() may not be used in control flow");
2345 value
= generate_call(instructions
, sig
, &actual_parameters
, sub_var
,
2348 ir_variable
*const tmp
= new(ctx
) ir_variable(glsl_type::void_type
,
2351 instructions
->push_tail(tmp
);
2352 value
= new(ctx
) ir_dereference_variable(tmp
);
2359 unreachable("not reached");
2363 ast_function_expression::has_sequence_subexpression() const
2365 foreach_list_typed(const ast_node
, ast
, link
, &this->expressions
) {
2366 if (ast
->has_sequence_subexpression())
2374 ast_aggregate_initializer::hir(exec_list
*instructions
,
2375 struct _mesa_glsl_parse_state
*state
)
2378 YYLTYPE loc
= this->get_location();
2380 if (!this->constructor_type
) {
2381 _mesa_glsl_error(&loc
, state
, "type of C-style initializer unknown");
2382 return ir_rvalue::error_value(ctx
);
2384 const glsl_type
*const constructor_type
= this->constructor_type
;
2386 if (!state
->has_420pack()) {
2387 _mesa_glsl_error(&loc
, state
, "C-style initialization requires the "
2388 "GL_ARB_shading_language_420pack extension");
2389 return ir_rvalue::error_value(ctx
);
2392 if (constructor_type
->is_array()) {
2393 return process_array_constructor(instructions
, constructor_type
, &loc
,
2394 &this->expressions
, state
);
2397 if (constructor_type
->is_record()) {
2398 return process_record_constructor(instructions
, constructor_type
, &loc
,
2399 &this->expressions
, state
);
2402 return process_vec_mat_constructor(instructions
, constructor_type
, &loc
,
2403 &this->expressions
, state
);