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 "glsl_types.h"
28 #include "main/core.h" /* for MIN2 */
29 #include "main/shaderobj.h"
32 convert_component(ir_rvalue
*src
, const glsl_type
*desired_type
);
35 apply_implicit_conversion(const glsl_type
*to
, ir_rvalue
* &from
,
36 struct _mesa_glsl_parse_state
*state
);
39 process_parameters(exec_list
*instructions
, exec_list
*actual_parameters
,
40 exec_list
*parameters
,
41 struct _mesa_glsl_parse_state
*state
)
45 foreach_list_typed(ast_node
, ast
, link
, parameters
) {
46 ir_rvalue
*result
= ast
->hir(instructions
, state
);
48 ir_constant
*const constant
= result
->constant_expression_value();
52 actual_parameters
->push_tail(result
);
61 * Generate a source prototype for a function signature
63 * \param return_type Return type of the function. May be \c NULL.
64 * \param name Name of the function.
65 * \param parameters List of \c ir_instruction nodes representing the
66 * parameter list for the function. This may be either a
67 * formal (\c ir_variable) or actual (\c ir_rvalue)
68 * parameter list. Only the type is used.
71 * A ralloced string representing the prototype of the function.
74 prototype_string(const glsl_type
*return_type
, const char *name
,
75 exec_list
*parameters
)
79 if (return_type
!= NULL
)
80 str
= ralloc_asprintf(NULL
, "%s ", return_type
->name
);
82 ralloc_asprintf_append(&str
, "%s(", name
);
84 const char *comma
= "";
85 foreach_in_list(const ir_variable
, param
, parameters
) {
86 ralloc_asprintf_append(&str
, "%s%s", comma
, param
->type
->name
);
90 ralloc_strcat(&str
, ")");
95 verify_image_parameter(YYLTYPE
*loc
, _mesa_glsl_parse_state
*state
,
96 const ir_variable
*formal
, const ir_variable
*actual
)
99 * From the ARB_shader_image_load_store specification:
101 * "The values of image variables qualified with coherent,
102 * volatile, restrict, readonly, or writeonly may not be passed
103 * to functions whose formal parameters lack such
104 * qualifiers. [...] It is legal to have additional qualifiers
105 * on a formal parameter, but not to have fewer."
107 if (actual
->data
.image_coherent
&& !formal
->data
.image_coherent
) {
108 _mesa_glsl_error(loc
, state
,
109 "function call parameter `%s' drops "
110 "`coherent' qualifier", formal
->name
);
114 if (actual
->data
.image_volatile
&& !formal
->data
.image_volatile
) {
115 _mesa_glsl_error(loc
, state
,
116 "function call parameter `%s' drops "
117 "`volatile' qualifier", formal
->name
);
121 if (actual
->data
.image_restrict
&& !formal
->data
.image_restrict
) {
122 _mesa_glsl_error(loc
, state
,
123 "function call parameter `%s' drops "
124 "`restrict' qualifier", formal
->name
);
128 if (actual
->data
.image_read_only
&& !formal
->data
.image_read_only
) {
129 _mesa_glsl_error(loc
, state
,
130 "function call parameter `%s' drops "
131 "`readonly' qualifier", formal
->name
);
135 if (actual
->data
.image_write_only
&& !formal
->data
.image_write_only
) {
136 _mesa_glsl_error(loc
, state
,
137 "function call parameter `%s' drops "
138 "`writeonly' qualifier", formal
->name
);
146 verify_first_atomic_ssbo_parameter(YYLTYPE
*loc
, _mesa_glsl_parse_state
*state
,
149 if (!var
|| !var
->is_in_shader_storage_block()) {
150 _mesa_glsl_error(loc
, state
, "First argument to atomic function "
151 "must be a buffer variable");
158 is_atomic_ssbo_function(const char *func_name
)
160 return !strcmp(func_name
, "atomicAdd") ||
161 !strcmp(func_name
, "atomicMin") ||
162 !strcmp(func_name
, "atomicMax") ||
163 !strcmp(func_name
, "atomicAnd") ||
164 !strcmp(func_name
, "atomicOr") ||
165 !strcmp(func_name
, "atomicXor") ||
166 !strcmp(func_name
, "atomicExchange") ||
167 !strcmp(func_name
, "atomicCompSwap");
171 * Verify that 'out' and 'inout' actual parameters are lvalues. Also, verify
172 * that 'const_in' formal parameters (an extension in our IR) correspond to
173 * ir_constant actual parameters.
176 verify_parameter_modes(_mesa_glsl_parse_state
*state
,
177 ir_function_signature
*sig
,
178 exec_list
&actual_ir_parameters
,
179 exec_list
&actual_ast_parameters
)
181 exec_node
*actual_ir_node
= actual_ir_parameters
.head
;
182 exec_node
*actual_ast_node
= actual_ast_parameters
.head
;
184 foreach_in_list(const ir_variable
, formal
, &sig
->parameters
) {
185 /* The lists must be the same length. */
186 assert(!actual_ir_node
->is_tail_sentinel());
187 assert(!actual_ast_node
->is_tail_sentinel());
189 const ir_rvalue
*const actual
= (ir_rvalue
*) actual_ir_node
;
190 const ast_expression
*const actual_ast
=
191 exec_node_data(ast_expression
, actual_ast_node
, link
);
193 /* FIXME: 'loc' is incorrect (as of 2011-01-21). It is always
196 YYLTYPE loc
= actual_ast
->get_location();
198 /* Verify that 'const_in' parameters are ir_constants. */
199 if (formal
->data
.mode
== ir_var_const_in
&&
200 actual
->ir_type
!= ir_type_constant
) {
201 _mesa_glsl_error(&loc
, state
,
202 "parameter `in %s' must be a constant expression",
207 /* Verify that shader_in parameters are shader inputs */
208 if (formal
->data
.must_be_shader_input
) {
209 ir_variable
*var
= actual
->variable_referenced();
210 if (var
&& var
->data
.mode
!= ir_var_shader_in
) {
211 _mesa_glsl_error(&loc
, state
,
212 "parameter `%s` must be a shader input",
217 if (actual
->ir_type
== ir_type_swizzle
) {
218 _mesa_glsl_error(&loc
, state
,
219 "parameter `%s` must not be swizzled",
225 /* Verify that 'out' and 'inout' actual parameters are lvalues. */
226 if (formal
->data
.mode
== ir_var_function_out
227 || formal
->data
.mode
== ir_var_function_inout
) {
228 const char *mode
= NULL
;
229 switch (formal
->data
.mode
) {
230 case ir_var_function_out
: mode
= "out"; break;
231 case ir_var_function_inout
: mode
= "inout"; break;
232 default: assert(false); break;
235 /* This AST-based check catches errors like f(i++). The IR-based
236 * is_lvalue() is insufficient because the actual parameter at the
237 * IR-level is just a temporary value, which is an l-value.
239 if (actual_ast
->non_lvalue_description
!= NULL
) {
240 _mesa_glsl_error(&loc
, state
,
241 "function parameter '%s %s' references a %s",
243 actual_ast
->non_lvalue_description
);
247 ir_variable
*var
= actual
->variable_referenced();
249 var
->data
.assigned
= true;
251 if (var
&& var
->data
.read_only
) {
252 _mesa_glsl_error(&loc
, state
,
253 "function parameter '%s %s' references the "
254 "read-only variable '%s'",
256 actual
->variable_referenced()->name
);
258 } else if (!actual
->is_lvalue()) {
259 /* Even though ir_binop_vector_extract is not an l-value, let it
260 * slop through. generate_call will handle it correctly.
262 ir_expression
*const expr
= ((ir_rvalue
*) actual
)->as_expression();
264 || expr
->operation
!= ir_binop_vector_extract
265 || !expr
->operands
[0]->is_lvalue()) {
266 _mesa_glsl_error(&loc
, state
,
267 "function parameter '%s %s' is not an lvalue",
274 if (formal
->type
->is_image() &&
275 actual
->variable_referenced()) {
276 if (!verify_image_parameter(&loc
, state
, formal
,
277 actual
->variable_referenced()))
281 actual_ir_node
= actual_ir_node
->next
;
282 actual_ast_node
= actual_ast_node
->next
;
285 /* The first parameter of atomic functions must be a buffer variable */
286 const char *func_name
= sig
->function_name();
287 bool is_atomic_ssbo
= is_atomic_ssbo_function(func_name
);
288 if (is_atomic_ssbo
) {
289 const ir_rvalue
*const actual
= (ir_rvalue
*) actual_ir_parameters
.head
;
291 const ast_expression
*const actual_ast
=
292 exec_node_data(ast_expression
, actual_ast_parameters
.head
, link
);
293 YYLTYPE loc
= actual_ast
->get_location();
295 if (!verify_first_atomic_ssbo_parameter(&loc
, state
,
296 actual
->variable_referenced())) {
305 fix_parameter(void *mem_ctx
, ir_rvalue
*actual
, const glsl_type
*formal_type
,
306 exec_list
*before_instructions
, exec_list
*after_instructions
,
307 bool parameter_is_inout
)
309 ir_expression
*const expr
= actual
->as_expression();
311 /* If the types match exactly and the parameter is not a vector-extract,
312 * nothing needs to be done to fix the parameter.
314 if (formal_type
== actual
->type
315 && (expr
== NULL
|| expr
->operation
!= ir_binop_vector_extract
))
318 /* To convert an out parameter, we need to create a temporary variable to
319 * hold the value before conversion, and then perform the conversion after
320 * the function call returns.
322 * This has the effect of transforming code like this:
328 * Into IR that's equivalent to this:
332 * int out_parameter_conversion;
333 * f(out_parameter_conversion);
334 * value = float(out_parameter_conversion);
336 * If the parameter is an ir_expression of ir_binop_vector_extract,
337 * additional conversion is needed in the post-call re-write.
340 new(mem_ctx
) ir_variable(formal_type
, "inout_tmp", ir_var_temporary
);
342 before_instructions
->push_tail(tmp
);
344 /* If the parameter is an inout parameter, copy the value of the actual
345 * parameter to the new temporary. Note that no type conversion is allowed
346 * here because inout parameters must match types exactly.
348 if (parameter_is_inout
) {
349 /* Inout parameters should never require conversion, since that would
350 * require an implicit conversion to exist both to and from the formal
351 * parameter type, and there are no bidirectional implicit conversions.
353 assert (actual
->type
== formal_type
);
355 ir_dereference_variable
*const deref_tmp_1
=
356 new(mem_ctx
) ir_dereference_variable(tmp
);
357 ir_assignment
*const assignment
=
358 new(mem_ctx
) ir_assignment(deref_tmp_1
, actual
);
359 before_instructions
->push_tail(assignment
);
362 /* Replace the parameter in the call with a dereference of the new
365 ir_dereference_variable
*const deref_tmp_2
=
366 new(mem_ctx
) ir_dereference_variable(tmp
);
367 actual
->replace_with(deref_tmp_2
);
370 /* Copy the temporary variable to the actual parameter with optional
371 * type conversion applied.
373 ir_rvalue
*rhs
= new(mem_ctx
) ir_dereference_variable(tmp
);
374 if (actual
->type
!= formal_type
)
375 rhs
= convert_component(rhs
, actual
->type
);
377 ir_rvalue
*lhs
= actual
;
378 if (expr
!= NULL
&& expr
->operation
== ir_binop_vector_extract
) {
379 rhs
= new(mem_ctx
) ir_expression(ir_triop_vector_insert
,
380 expr
->operands
[0]->type
,
381 expr
->operands
[0]->clone(mem_ctx
, NULL
),
383 expr
->operands
[1]->clone(mem_ctx
, NULL
));
384 lhs
= expr
->operands
[0]->clone(mem_ctx
, NULL
);
387 ir_assignment
*const assignment_2
= new(mem_ctx
) ir_assignment(lhs
, rhs
);
388 after_instructions
->push_tail(assignment_2
);
392 * Generate a function call.
394 * For non-void functions, this returns a dereference of the temporary variable
395 * which stores the return value for the call. For void functions, this returns
399 generate_call(exec_list
*instructions
, ir_function_signature
*sig
,
400 exec_list
*actual_parameters
,
401 ir_variable
*sub_var
,
402 ir_rvalue
*array_idx
,
403 struct _mesa_glsl_parse_state
*state
)
406 exec_list post_call_conversions
;
408 /* Perform implicit conversion of arguments. For out parameters, we need
409 * to place them in a temporary variable and do the conversion after the
410 * call takes place. Since we haven't emitted the call yet, we'll place
411 * the post-call conversions in a temporary exec_list, and emit them later.
413 foreach_two_lists(formal_node
, &sig
->parameters
,
414 actual_node
, actual_parameters
) {
415 ir_rvalue
*actual
= (ir_rvalue
*) actual_node
;
416 ir_variable
*formal
= (ir_variable
*) formal_node
;
418 if (formal
->type
->is_numeric() || formal
->type
->is_boolean()) {
419 switch (formal
->data
.mode
) {
420 case ir_var_const_in
:
421 case ir_var_function_in
: {
423 = convert_component(actual
, formal
->type
);
424 actual
->replace_with(converted
);
427 case ir_var_function_out
:
428 case ir_var_function_inout
:
429 fix_parameter(ctx
, actual
, formal
->type
,
430 instructions
, &post_call_conversions
,
431 formal
->data
.mode
== ir_var_function_inout
);
434 assert (!"Illegal formal parameter mode");
440 /* Section 4.3.2 (Const) of the GLSL 1.10.59 spec says:
442 * "Initializers for const declarations must be formed from literal
443 * values, other const variables (not including function call
444 * paramaters), or expressions of these.
446 * Constructors may be used in such expressions, but function calls may
449 * Section 4.3.3 (Constant Expressions) of the GLSL 1.20.8 spec says:
451 * "A constant expression is one of
455 * - a built-in function call whose arguments are all constant
456 * expressions, with the exception of the texture lookup
457 * functions, the noise functions, and ftransform. The built-in
458 * functions dFdx, dFdy, and fwidth must return 0 when evaluated
459 * inside an initializer with an argument that is a constant
462 * Section 5.10 (Constant Expressions) of the GLSL ES 1.00.17 spec says:
464 * "A constant expression is one of
468 * - a built-in function call whose arguments are all constant
469 * expressions, with the exception of the texture lookup
472 * Section 4.3.3 (Constant Expressions) of the GLSL ES 3.00.4 spec says:
474 * "A constant expression is one of
478 * - a built-in function call whose arguments are all constant
479 * expressions, with the exception of the texture lookup
480 * functions. The built-in functions dFdx, dFdy, and fwidth must
481 * return 0 when evaluated inside an initializer with an argument
482 * that is a constant expression."
484 * If the function call is a constant expression, don't generate any
485 * instructions; just generate an ir_constant.
487 if (state
->is_version(120, 100)) {
488 ir_constant
*value
= sig
->constant_expression_value(actual_parameters
, NULL
);
494 ir_dereference_variable
*deref
= NULL
;
495 if (!sig
->return_type
->is_void()) {
496 /* Create a new temporary to hold the return value. */
497 char *const name
= ir_variable::temporaries_allocate_names
498 ? ralloc_asprintf(ctx
, "%s_retval", sig
->function_name())
503 var
= new(ctx
) ir_variable(sig
->return_type
, name
, ir_var_temporary
);
504 instructions
->push_tail(var
);
508 deref
= new(ctx
) ir_dereference_variable(var
);
511 ir_call
*call
= new(ctx
) ir_call(sig
, deref
, actual_parameters
, sub_var
, array_idx
);
512 instructions
->push_tail(call
);
514 /* Also emit any necessary out-parameter conversions. */
515 instructions
->append_list(&post_call_conversions
);
517 return deref
? deref
->clone(ctx
, NULL
) : NULL
;
521 * Given a function name and parameter list, find the matching signature.
523 static ir_function_signature
*
524 match_function_by_name(const char *name
,
525 exec_list
*actual_parameters
,
526 struct _mesa_glsl_parse_state
*state
)
529 ir_function
*f
= state
->symbols
->get_function(name
);
530 ir_function_signature
*local_sig
= NULL
;
531 ir_function_signature
*sig
= NULL
;
533 /* Is the function hidden by a record type constructor? */
534 if (state
->symbols
->get_type(name
))
535 goto done
; /* no match */
537 /* Is the function hidden by a variable (impossible in 1.10)? */
538 if (!state
->symbols
->separate_function_namespace
539 && state
->symbols
->get_variable(name
))
540 goto done
; /* no match */
543 /* In desktop GL, the presence of a user-defined signature hides any
544 * built-in signatures, so we must ignore them. In contrast, in ES2
545 * user-defined signatures add new overloads, so we must consider them.
547 bool allow_builtins
= state
->es_shader
|| !f
->has_user_signature();
549 /* Look for a match in the local shader. If exact, we're done. */
550 bool is_exact
= false;
551 sig
= local_sig
= f
->matching_signature(state
, actual_parameters
,
552 allow_builtins
, &is_exact
);
560 /* Local shader has no exact candidates; check the built-ins. */
561 _mesa_glsl_initialize_builtin_functions();
562 sig
= _mesa_glsl_find_builtin_function(state
, name
, actual_parameters
);
566 /* If the match is from a linked built-in shader, import the prototype. */
567 if (sig
!= local_sig
) {
569 f
= new(ctx
) ir_function(name
);
570 state
->symbols
->add_global_function(f
);
571 emit_function(state
, f
);
573 f
->add_signature(sig
->clone_prototype(f
, NULL
));
579 static ir_function_signature
*
580 match_subroutine_by_name(const char *name
,
581 exec_list
*actual_parameters
,
582 struct _mesa_glsl_parse_state
*state
,
586 ir_function_signature
*sig
= NULL
;
587 ir_function
*f
, *found
= NULL
;
588 const char *new_name
;
590 bool is_exact
= false;
592 new_name
= ralloc_asprintf(ctx
, "%s_%s", _mesa_shader_stage_to_subroutine_prefix(state
->stage
), name
);
593 var
= state
->symbols
->get_variable(new_name
);
597 for (int i
= 0; i
< state
->num_subroutine_types
; i
++) {
598 f
= state
->subroutine_types
[i
];
599 if (strcmp(f
->name
, var
->type
->without_array()->name
))
608 sig
= found
->matching_signature(state
, actual_parameters
,
614 print_function_prototypes(_mesa_glsl_parse_state
*state
, YYLTYPE
*loc
,
620 foreach_in_list(ir_function_signature
, sig
, &f
->signatures
) {
621 if (sig
->is_builtin() && !sig
->is_builtin_available(state
))
624 char *str
= prototype_string(sig
->return_type
, f
->name
, &sig
->parameters
);
625 _mesa_glsl_error(loc
, state
, " %s", str
);
631 * Raise a "no matching function" error, listing all possible overloads the
632 * compiler considered so developers can figure out what went wrong.
635 no_matching_function_error(const char *name
,
637 exec_list
*actual_parameters
,
638 _mesa_glsl_parse_state
*state
)
640 gl_shader
*sh
= _mesa_glsl_get_builtin_function_shader();
642 if (state
->symbols
->get_function(name
) == NULL
643 && (!state
->uses_builtin_functions
644 || sh
->symbols
->get_function(name
) == NULL
)) {
645 _mesa_glsl_error(loc
, state
, "no function with name '%s'", name
);
647 char *str
= prototype_string(NULL
, name
, actual_parameters
);
648 _mesa_glsl_error(loc
, state
,
649 "no matching function for call to `%s'; candidates are:",
653 print_function_prototypes(state
, loc
, state
->symbols
->get_function(name
));
655 if (state
->uses_builtin_functions
) {
656 print_function_prototypes(state
, loc
, sh
->symbols
->get_function(name
));
662 * Perform automatic type conversion of constructor parameters
664 * This implements the rules in the "Conversion and Scalar Constructors"
665 * section (GLSL 1.10 section 5.4.1), not the "Implicit Conversions" rules.
668 convert_component(ir_rvalue
*src
, const glsl_type
*desired_type
)
670 void *ctx
= ralloc_parent(src
);
671 const unsigned a
= desired_type
->base_type
;
672 const unsigned b
= src
->type
->base_type
;
673 ir_expression
*result
= NULL
;
675 if (src
->type
->is_error())
678 assert(a
<= GLSL_TYPE_BOOL
);
679 assert(b
<= GLSL_TYPE_BOOL
);
688 result
= new(ctx
) ir_expression(ir_unop_i2u
, src
);
690 case GLSL_TYPE_FLOAT
:
691 result
= new(ctx
) ir_expression(ir_unop_f2u
, src
);
694 result
= new(ctx
) ir_expression(ir_unop_i2u
,
695 new(ctx
) ir_expression(ir_unop_b2i
, src
));
697 case GLSL_TYPE_DOUBLE
:
698 result
= new(ctx
) ir_expression(ir_unop_d2u
, src
);
705 result
= new(ctx
) ir_expression(ir_unop_u2i
, src
);
707 case GLSL_TYPE_FLOAT
:
708 result
= new(ctx
) ir_expression(ir_unop_f2i
, src
);
711 result
= new(ctx
) ir_expression(ir_unop_b2i
, src
);
713 case GLSL_TYPE_DOUBLE
:
714 result
= new(ctx
) ir_expression(ir_unop_d2i
, src
);
718 case GLSL_TYPE_FLOAT
:
721 result
= new(ctx
) ir_expression(ir_unop_u2f
, desired_type
, src
, NULL
);
724 result
= new(ctx
) ir_expression(ir_unop_i2f
, desired_type
, src
, NULL
);
727 result
= new(ctx
) ir_expression(ir_unop_b2f
, desired_type
, src
, NULL
);
729 case GLSL_TYPE_DOUBLE
:
730 result
= new(ctx
) ir_expression(ir_unop_d2f
, desired_type
, src
, NULL
);
737 result
= new(ctx
) ir_expression(ir_unop_i2b
,
738 new(ctx
) ir_expression(ir_unop_u2i
, src
));
741 result
= new(ctx
) ir_expression(ir_unop_i2b
, desired_type
, src
, NULL
);
743 case GLSL_TYPE_FLOAT
:
744 result
= new(ctx
) ir_expression(ir_unop_f2b
, desired_type
, src
, NULL
);
746 case GLSL_TYPE_DOUBLE
:
747 result
= new(ctx
) ir_expression(ir_unop_d2b
, desired_type
, src
, NULL
);
751 case GLSL_TYPE_DOUBLE
:
754 result
= new(ctx
) ir_expression(ir_unop_i2d
, src
);
757 result
= new(ctx
) ir_expression(ir_unop_u2d
, src
);
760 result
= new(ctx
) ir_expression(ir_unop_f2d
,
761 new(ctx
) ir_expression(ir_unop_b2f
, src
));
763 case GLSL_TYPE_FLOAT
:
764 result
= new(ctx
) ir_expression(ir_unop_f2d
, desired_type
, src
, NULL
);
769 assert(result
!= NULL
);
770 assert(result
->type
== desired_type
);
772 /* Try constant folding; it may fold in the conversion we just added. */
773 ir_constant
*const constant
= result
->constant_expression_value();
774 return (constant
!= NULL
) ? (ir_rvalue
*) constant
: (ir_rvalue
*) result
;
778 * Dereference a specific component from a scalar, vector, or matrix
781 dereference_component(ir_rvalue
*src
, unsigned component
)
783 void *ctx
= ralloc_parent(src
);
784 assert(component
< src
->type
->components());
786 /* If the source is a constant, just create a new constant instead of a
787 * dereference of the existing constant.
789 ir_constant
*constant
= src
->as_constant();
791 return new(ctx
) ir_constant(constant
, component
);
793 if (src
->type
->is_scalar()) {
795 } else if (src
->type
->is_vector()) {
796 return new(ctx
) ir_swizzle(src
, component
, 0, 0, 0, 1);
798 assert(src
->type
->is_matrix());
800 /* Dereference a row of the matrix, then call this function again to get
801 * a specific element from that row.
803 const int c
= component
/ src
->type
->column_type()->vector_elements
;
804 const int r
= component
% src
->type
->column_type()->vector_elements
;
805 ir_constant
*const col_index
= new(ctx
) ir_constant(c
);
806 ir_dereference
*const col
= new(ctx
) ir_dereference_array(src
, col_index
);
808 col
->type
= src
->type
->column_type();
810 return dereference_component(col
, r
);
813 assert(!"Should not get here.");
819 process_vec_mat_constructor(exec_list
*instructions
,
820 const glsl_type
*constructor_type
,
821 YYLTYPE
*loc
, exec_list
*parameters
,
822 struct _mesa_glsl_parse_state
*state
)
826 /* The ARB_shading_language_420pack spec says:
828 * "If an initializer is a list of initializers enclosed in curly braces,
829 * the variable being declared must be a vector, a matrix, an array, or a
832 * int i = { 1 }; // illegal, i is not an aggregate"
834 if (constructor_type
->vector_elements
<= 1) {
835 _mesa_glsl_error(loc
, state
, "aggregates can only initialize vectors, "
836 "matrices, arrays, and structs");
837 return ir_rvalue::error_value(ctx
);
840 exec_list actual_parameters
;
841 const unsigned parameter_count
=
842 process_parameters(instructions
, &actual_parameters
, parameters
, state
);
844 if (parameter_count
== 0
845 || (constructor_type
->is_vector() &&
846 constructor_type
->vector_elements
!= parameter_count
)
847 || (constructor_type
->is_matrix() &&
848 constructor_type
->matrix_columns
!= parameter_count
)) {
849 _mesa_glsl_error(loc
, state
, "%s constructor must have %u parameters",
850 constructor_type
->is_vector() ? "vector" : "matrix",
851 constructor_type
->vector_elements
);
852 return ir_rvalue::error_value(ctx
);
855 bool all_parameters_are_constant
= true;
857 /* Type cast each parameter and, if possible, fold constants. */
858 foreach_in_list_safe(ir_rvalue
, ir
, &actual_parameters
) {
859 ir_rvalue
*result
= ir
;
861 /* Apply implicit conversions (not the scalar constructor rules!). See
862 * the spec quote above. */
863 if (constructor_type
->base_type
!= result
->type
->base_type
) {
864 const glsl_type
*desired_type
=
865 glsl_type::get_instance(constructor_type
->base_type
,
866 ir
->type
->vector_elements
,
867 ir
->type
->matrix_columns
);
868 if (result
->type
->can_implicitly_convert_to(desired_type
, state
)) {
869 /* Even though convert_component() implements the constructor
870 * conversion rules (not the implicit conversion rules), its safe
871 * to use it here because we already checked that the implicit
872 * conversion is legal.
874 result
= convert_component(ir
, desired_type
);
878 if (constructor_type
->is_matrix()) {
879 if (result
->type
!= constructor_type
->column_type()) {
880 _mesa_glsl_error(loc
, state
, "type error in matrix constructor: "
881 "expected: %s, found %s",
882 constructor_type
->column_type()->name
,
884 return ir_rvalue::error_value(ctx
);
886 } else if (result
->type
!= constructor_type
->get_scalar_type()) {
887 _mesa_glsl_error(loc
, state
, "type error in vector constructor: "
888 "expected: %s, found %s",
889 constructor_type
->get_scalar_type()->name
,
891 return ir_rvalue::error_value(ctx
);
894 /* Attempt to convert the parameter to a constant valued expression.
895 * After doing so, track whether or not all the parameters to the
896 * constructor are trivially constant valued expressions.
898 ir_rvalue
*const constant
= result
->constant_expression_value();
900 if (constant
!= NULL
)
903 all_parameters_are_constant
= false;
905 ir
->replace_with(result
);
908 if (all_parameters_are_constant
)
909 return new(ctx
) ir_constant(constructor_type
, &actual_parameters
);
911 ir_variable
*var
= new(ctx
) ir_variable(constructor_type
, "vec_mat_ctor",
913 instructions
->push_tail(var
);
917 foreach_in_list(ir_rvalue
, rhs
, &actual_parameters
) {
918 ir_instruction
*assignment
= NULL
;
920 if (var
->type
->is_matrix()) {
921 ir_rvalue
*lhs
= new(ctx
) ir_dereference_array(var
,
922 new(ctx
) ir_constant(i
));
923 assignment
= new(ctx
) ir_assignment(lhs
, rhs
, NULL
);
925 /* use writemask rather than index for vector */
926 assert(var
->type
->is_vector());
928 ir_dereference
*lhs
= new(ctx
) ir_dereference_variable(var
);
929 assignment
= new(ctx
) ir_assignment(lhs
, rhs
, NULL
, (unsigned)(1 << i
));
932 instructions
->push_tail(assignment
);
937 return new(ctx
) ir_dereference_variable(var
);
942 process_array_constructor(exec_list
*instructions
,
943 const glsl_type
*constructor_type
,
944 YYLTYPE
*loc
, exec_list
*parameters
,
945 struct _mesa_glsl_parse_state
*state
)
948 /* Array constructors come in two forms: sized and unsized. Sized array
949 * constructors look like 'vec4[2](a, b)', where 'a' and 'b' are vec4
950 * variables. In this case the number of parameters must exactly match the
951 * specified size of the array.
953 * Unsized array constructors look like 'vec4[](a, b)', where 'a' and 'b'
954 * are vec4 variables. In this case the size of the array being constructed
955 * is determined by the number of parameters.
957 * From page 52 (page 58 of the PDF) of the GLSL 1.50 spec:
959 * "There must be exactly the same number of arguments as the size of
960 * the array being constructed. If no size is present in the
961 * constructor, then the array is explicitly sized to the number of
962 * arguments provided. The arguments are assigned in order, starting at
963 * element 0, to the elements of the constructed array. Each argument
964 * must be the same type as the element type of the array, or be a type
965 * that can be converted to the element type of the array according to
966 * Section 4.1.10 "Implicit Conversions.""
968 exec_list actual_parameters
;
969 const unsigned parameter_count
=
970 process_parameters(instructions
, &actual_parameters
, parameters
, state
);
971 bool is_unsized_array
= constructor_type
->is_unsized_array();
973 if ((parameter_count
== 0) ||
974 (!is_unsized_array
&& (constructor_type
->length
!= parameter_count
))) {
975 const unsigned min_param
= is_unsized_array
976 ? 1 : constructor_type
->length
;
978 _mesa_glsl_error(loc
, state
, "array constructor must have %s %u "
980 is_unsized_array
? "at least" : "exactly",
981 min_param
, (min_param
<= 1) ? "" : "s");
982 return ir_rvalue::error_value(ctx
);
985 if (is_unsized_array
) {
987 glsl_type::get_array_instance(constructor_type
->fields
.array
,
989 assert(constructor_type
!= NULL
);
990 assert(constructor_type
->length
== parameter_count
);
993 bool all_parameters_are_constant
= true;
994 const glsl_type
*element_type
= constructor_type
->fields
.array
;
996 /* Type cast each parameter and, if possible, fold constants. */
997 foreach_in_list_safe(ir_rvalue
, ir
, &actual_parameters
) {
998 ir_rvalue
*result
= ir
;
1000 const glsl_base_type element_base_type
=
1001 constructor_type
->fields
.array
->base_type
;
1003 /* Apply implicit conversions (not the scalar constructor rules!). See
1004 * the spec quote above. */
1005 if (element_base_type
!= result
->type
->base_type
) {
1006 const glsl_type
*desired_type
=
1007 glsl_type::get_instance(element_base_type
,
1008 ir
->type
->vector_elements
,
1009 ir
->type
->matrix_columns
);
1011 if (result
->type
->can_implicitly_convert_to(desired_type
, state
)) {
1012 /* Even though convert_component() implements the constructor
1013 * conversion rules (not the implicit conversion rules), its safe
1014 * to use it here because we already checked that the implicit
1015 * conversion is legal.
1017 result
= convert_component(ir
, desired_type
);
1021 if (constructor_type
->fields
.array
->is_unsized_array()) {
1022 /* As the inner parameters of the constructor are created without
1023 * knowledge of each other we need to check to make sure unsized
1024 * parameters of unsized constructors all end up with the same size.
1026 * e.g we make sure to fail for a constructor like this:
1027 * vec4[][] a = vec4[][](vec4[](vec4(0.0), vec4(1.0)),
1028 * vec4[](vec4(0.0), vec4(1.0), vec4(1.0)),
1029 * vec4[](vec4(0.0), vec4(1.0)));
1031 if (element_type
->is_unsized_array()) {
1032 /* This is the first parameter so just get the type */
1033 element_type
= result
->type
;
1034 } else if (element_type
!= result
->type
) {
1035 _mesa_glsl_error(loc
, state
, "type error in array constructor: "
1036 "expected: %s, found %s",
1038 result
->type
->name
);
1039 return ir_rvalue::error_value(ctx
);
1041 } else if (result
->type
!= constructor_type
->fields
.array
) {
1042 _mesa_glsl_error(loc
, state
, "type error in array constructor: "
1043 "expected: %s, found %s",
1044 constructor_type
->fields
.array
->name
,
1045 result
->type
->name
);
1046 return ir_rvalue::error_value(ctx
);
1048 element_type
= result
->type
;
1051 /* Attempt to convert the parameter to a constant valued expression.
1052 * After doing so, track whether or not all the parameters to the
1053 * constructor are trivially constant valued expressions.
1055 ir_rvalue
*const constant
= result
->constant_expression_value();
1057 if (constant
!= NULL
)
1060 all_parameters_are_constant
= false;
1062 ir
->replace_with(result
);
1065 if (constructor_type
->fields
.array
->is_unsized_array()) {
1067 glsl_type::get_array_instance(element_type
,
1069 assert(constructor_type
!= NULL
);
1070 assert(constructor_type
->length
== parameter_count
);
1073 if (all_parameters_are_constant
)
1074 return new(ctx
) ir_constant(constructor_type
, &actual_parameters
);
1076 ir_variable
*var
= new(ctx
) ir_variable(constructor_type
, "array_ctor",
1078 instructions
->push_tail(var
);
1081 foreach_in_list(ir_rvalue
, rhs
, &actual_parameters
) {
1082 ir_rvalue
*lhs
= new(ctx
) ir_dereference_array(var
,
1083 new(ctx
) ir_constant(i
));
1085 ir_instruction
*assignment
= new(ctx
) ir_assignment(lhs
, rhs
, NULL
);
1086 instructions
->push_tail(assignment
);
1091 return new(ctx
) ir_dereference_variable(var
);
1096 * Try to convert a record constructor to a constant expression
1098 static ir_constant
*
1099 constant_record_constructor(const glsl_type
*constructor_type
,
1100 exec_list
*parameters
, void *mem_ctx
)
1102 foreach_in_list(ir_instruction
, node
, parameters
) {
1103 ir_constant
*constant
= node
->as_constant();
1104 if (constant
== NULL
)
1106 node
->replace_with(constant
);
1109 return new(mem_ctx
) ir_constant(constructor_type
, parameters
);
1114 * Determine if a list consists of a single scalar r-value
1117 single_scalar_parameter(exec_list
*parameters
)
1119 const ir_rvalue
*const p
= (ir_rvalue
*) parameters
->head
;
1120 assert(((ir_rvalue
*)p
)->as_rvalue() != NULL
);
1122 return (p
->type
->is_scalar() && p
->next
->is_tail_sentinel());
1127 * Generate inline code for a vector constructor
1129 * The generated constructor code will consist of a temporary variable
1130 * declaration of the same type as the constructor. A sequence of assignments
1131 * from constructor parameters to the temporary will follow.
1134 * An \c ir_dereference_variable of the temprorary generated in the constructor
1138 emit_inline_vector_constructor(const glsl_type
*type
,
1139 exec_list
*instructions
,
1140 exec_list
*parameters
,
1143 assert(!parameters
->is_empty());
1145 ir_variable
*var
= new(ctx
) ir_variable(type
, "vec_ctor", ir_var_temporary
);
1146 instructions
->push_tail(var
);
1148 /* There are three kinds of vector constructors.
1150 * - Construct a vector from a single scalar by replicating that scalar to
1151 * all components of the vector.
1153 * - Construct a vector from at least a matrix. This case should already
1154 * have been taken care of in ast_function_expression::hir by breaking
1155 * down the matrix into a series of column vectors.
1157 * - Construct a vector from an arbirary combination of vectors and
1158 * scalars. The components of the constructor parameters are assigned
1159 * to the vector in order until the vector is full.
1161 const unsigned lhs_components
= type
->components();
1162 if (single_scalar_parameter(parameters
)) {
1163 ir_rvalue
*first_param
= (ir_rvalue
*)parameters
->head
;
1164 ir_rvalue
*rhs
= new(ctx
) ir_swizzle(first_param
, 0, 0, 0, 0,
1166 ir_dereference_variable
*lhs
= new(ctx
) ir_dereference_variable(var
);
1167 const unsigned mask
= (1U << lhs_components
) - 1;
1169 assert(rhs
->type
== lhs
->type
);
1171 ir_instruction
*inst
= new(ctx
) ir_assignment(lhs
, rhs
, NULL
, mask
);
1172 instructions
->push_tail(inst
);
1174 unsigned base_component
= 0;
1175 unsigned base_lhs_component
= 0;
1176 ir_constant_data data
;
1177 unsigned constant_mask
= 0, constant_components
= 0;
1179 memset(&data
, 0, sizeof(data
));
1181 foreach_in_list(ir_rvalue
, param
, parameters
) {
1182 unsigned rhs_components
= param
->type
->components();
1184 /* Do not try to assign more components to the vector than it has!
1186 if ((rhs_components
+ base_lhs_component
) > lhs_components
) {
1187 rhs_components
= lhs_components
- base_lhs_component
;
1190 const ir_constant
*const c
= param
->as_constant();
1192 for (unsigned i
= 0; i
< rhs_components
; i
++) {
1193 switch (c
->type
->base_type
) {
1194 case GLSL_TYPE_UINT
:
1195 data
.u
[i
+ base_component
] = c
->get_uint_component(i
);
1198 data
.i
[i
+ base_component
] = c
->get_int_component(i
);
1200 case GLSL_TYPE_FLOAT
:
1201 data
.f
[i
+ base_component
] = c
->get_float_component(i
);
1203 case GLSL_TYPE_DOUBLE
:
1204 data
.d
[i
+ base_component
] = c
->get_double_component(i
);
1206 case GLSL_TYPE_BOOL
:
1207 data
.b
[i
+ base_component
] = c
->get_bool_component(i
);
1210 assert(!"Should not get here.");
1215 /* Mask of fields to be written in the assignment.
1217 constant_mask
|= ((1U << rhs_components
) - 1) << base_lhs_component
;
1218 constant_components
+= rhs_components
;
1220 base_component
+= rhs_components
;
1222 /* Advance the component index by the number of components
1223 * that were just assigned.
1225 base_lhs_component
+= rhs_components
;
1228 if (constant_mask
!= 0) {
1229 ir_dereference
*lhs
= new(ctx
) ir_dereference_variable(var
);
1230 const glsl_type
*rhs_type
= glsl_type::get_instance(var
->type
->base_type
,
1231 constant_components
,
1233 ir_rvalue
*rhs
= new(ctx
) ir_constant(rhs_type
, &data
);
1235 ir_instruction
*inst
=
1236 new(ctx
) ir_assignment(lhs
, rhs
, NULL
, constant_mask
);
1237 instructions
->push_tail(inst
);
1241 foreach_in_list(ir_rvalue
, param
, parameters
) {
1242 unsigned rhs_components
= param
->type
->components();
1244 /* Do not try to assign more components to the vector than it has!
1246 if ((rhs_components
+ base_component
) > lhs_components
) {
1247 rhs_components
= lhs_components
- base_component
;
1250 /* If we do not have any components left to copy, break out of the
1251 * loop. This can happen when initializing a vec4 with a mat3 as the
1252 * mat3 would have been broken into a series of column vectors.
1254 if (rhs_components
== 0) {
1258 const ir_constant
*const c
= param
->as_constant();
1260 /* Mask of fields to be written in the assignment.
1262 const unsigned write_mask
= ((1U << rhs_components
) - 1)
1265 ir_dereference
*lhs
= new(ctx
) ir_dereference_variable(var
);
1267 /* Generate a swizzle so that LHS and RHS sizes match.
1270 new(ctx
) ir_swizzle(param
, 0, 1, 2, 3, rhs_components
);
1272 ir_instruction
*inst
=
1273 new(ctx
) ir_assignment(lhs
, rhs
, NULL
, write_mask
);
1274 instructions
->push_tail(inst
);
1277 /* Advance the component index by the number of components that were
1280 base_component
+= rhs_components
;
1283 return new(ctx
) ir_dereference_variable(var
);
1288 * Generate assignment of a portion of a vector to a portion of a matrix column
1290 * \param src_base First component of the source to be used in assignment
1291 * \param column Column of destination to be assiged
1292 * \param row_base First component of the destination column to be assigned
1293 * \param count Number of components to be assigned
1296 * \c src_base + \c count must be less than or equal to the number of components
1297 * in the source vector.
1300 assign_to_matrix_column(ir_variable
*var
, unsigned column
, unsigned row_base
,
1301 ir_rvalue
*src
, unsigned src_base
, unsigned count
,
1304 ir_constant
*col_idx
= new(mem_ctx
) ir_constant(column
);
1305 ir_dereference
*column_ref
= new(mem_ctx
) ir_dereference_array(var
, col_idx
);
1307 assert(column_ref
->type
->components() >= (row_base
+ count
));
1308 assert(src
->type
->components() >= (src_base
+ count
));
1310 /* Generate a swizzle that extracts the number of components from the source
1311 * that are to be assigned to the column of the matrix.
1313 if (count
< src
->type
->vector_elements
) {
1314 src
= new(mem_ctx
) ir_swizzle(src
,
1315 src_base
+ 0, src_base
+ 1,
1316 src_base
+ 2, src_base
+ 3,
1320 /* Mask of fields to be written in the assignment.
1322 const unsigned write_mask
= ((1U << count
) - 1) << row_base
;
1324 return new(mem_ctx
) ir_assignment(column_ref
, src
, NULL
, write_mask
);
1329 * Generate inline code for a matrix constructor
1331 * The generated constructor code will consist of a temporary variable
1332 * declaration of the same type as the constructor. A sequence of assignments
1333 * from constructor parameters to the temporary will follow.
1336 * An \c ir_dereference_variable of the temprorary generated in the constructor
1340 emit_inline_matrix_constructor(const glsl_type
*type
,
1341 exec_list
*instructions
,
1342 exec_list
*parameters
,
1345 assert(!parameters
->is_empty());
1347 ir_variable
*var
= new(ctx
) ir_variable(type
, "mat_ctor", ir_var_temporary
);
1348 instructions
->push_tail(var
);
1350 /* There are three kinds of matrix constructors.
1352 * - Construct a matrix from a single scalar by replicating that scalar to
1353 * along the diagonal of the matrix and setting all other components to
1356 * - Construct a matrix from an arbirary combination of vectors and
1357 * scalars. The components of the constructor parameters are assigned
1358 * to the matrix in column-major order until the matrix is full.
1360 * - Construct a matrix from a single matrix. The source matrix is copied
1361 * to the upper left portion of the constructed matrix, and the remaining
1362 * elements take values from the identity matrix.
1364 ir_rvalue
*const first_param
= (ir_rvalue
*) parameters
->head
;
1365 if (single_scalar_parameter(parameters
)) {
1366 /* Assign the scalar to the X component of a vec4, and fill the remaining
1367 * components with zero.
1369 glsl_base_type param_base_type
= first_param
->type
->base_type
;
1370 assert(param_base_type
== GLSL_TYPE_FLOAT
||
1371 param_base_type
== GLSL_TYPE_DOUBLE
);
1372 ir_variable
*rhs_var
=
1373 new(ctx
) ir_variable(glsl_type::get_instance(param_base_type
, 4, 1),
1376 instructions
->push_tail(rhs_var
);
1378 ir_constant_data zero
;
1379 for (unsigned i
= 0; i
< 4; i
++)
1380 if (param_base_type
== GLSL_TYPE_FLOAT
)
1385 ir_instruction
*inst
=
1386 new(ctx
) ir_assignment(new(ctx
) ir_dereference_variable(rhs_var
),
1387 new(ctx
) ir_constant(rhs_var
->type
, &zero
),
1389 instructions
->push_tail(inst
);
1391 ir_dereference
*const rhs_ref
= new(ctx
) ir_dereference_variable(rhs_var
);
1393 inst
= new(ctx
) ir_assignment(rhs_ref
, first_param
, NULL
, 0x01);
1394 instructions
->push_tail(inst
);
1396 /* Assign the temporary vector to each column of the destination matrix
1397 * with a swizzle that puts the X component on the diagonal of the
1398 * matrix. In some cases this may mean that the X component does not
1399 * get assigned into the column at all (i.e., when the matrix has more
1400 * columns than rows).
1402 static const unsigned rhs_swiz
[4][4] = {
1409 const unsigned cols_to_init
= MIN2(type
->matrix_columns
,
1410 type
->vector_elements
);
1411 for (unsigned i
= 0; i
< cols_to_init
; i
++) {
1412 ir_constant
*const col_idx
= new(ctx
) ir_constant(i
);
1413 ir_rvalue
*const col_ref
= new(ctx
) ir_dereference_array(var
, col_idx
);
1415 ir_rvalue
*const rhs_ref
= new(ctx
) ir_dereference_variable(rhs_var
);
1416 ir_rvalue
*const rhs
= new(ctx
) ir_swizzle(rhs_ref
, rhs_swiz
[i
],
1417 type
->vector_elements
);
1419 inst
= new(ctx
) ir_assignment(col_ref
, rhs
, NULL
);
1420 instructions
->push_tail(inst
);
1423 for (unsigned i
= cols_to_init
; i
< type
->matrix_columns
; i
++) {
1424 ir_constant
*const col_idx
= new(ctx
) ir_constant(i
);
1425 ir_rvalue
*const col_ref
= new(ctx
) ir_dereference_array(var
, col_idx
);
1427 ir_rvalue
*const rhs_ref
= new(ctx
) ir_dereference_variable(rhs_var
);
1428 ir_rvalue
*const rhs
= new(ctx
) ir_swizzle(rhs_ref
, 1, 1, 1, 1,
1429 type
->vector_elements
);
1431 inst
= new(ctx
) ir_assignment(col_ref
, rhs
, NULL
);
1432 instructions
->push_tail(inst
);
1434 } else if (first_param
->type
->is_matrix()) {
1435 /* From page 50 (56 of the PDF) of the GLSL 1.50 spec:
1437 * "If a matrix is constructed from a matrix, then each component
1438 * (column i, row j) in the result that has a corresponding
1439 * component (column i, row j) in the argument will be initialized
1440 * from there. All other components will be initialized to the
1441 * identity matrix. If a matrix argument is given to a matrix
1442 * constructor, it is an error to have any other arguments."
1444 assert(first_param
->next
->is_tail_sentinel());
1445 ir_rvalue
*const src_matrix
= first_param
;
1447 /* If the source matrix is smaller, pre-initialize the relavent parts of
1448 * the destination matrix to the identity matrix.
1450 if ((src_matrix
->type
->matrix_columns
< var
->type
->matrix_columns
)
1451 || (src_matrix
->type
->vector_elements
< var
->type
->vector_elements
)) {
1453 /* If the source matrix has fewer rows, every column of the destination
1454 * must be initialized. Otherwise only the columns in the destination
1455 * that do not exist in the source must be initialized.
1458 (src_matrix
->type
->vector_elements
< var
->type
->vector_elements
)
1459 ? 0 : src_matrix
->type
->matrix_columns
;
1461 const glsl_type
*const col_type
= var
->type
->column_type();
1462 for (/* empty */; col
< var
->type
->matrix_columns
; col
++) {
1463 ir_constant_data ident
;
1472 ir_rvalue
*const rhs
= new(ctx
) ir_constant(col_type
, &ident
);
1474 ir_rvalue
*const lhs
=
1475 new(ctx
) ir_dereference_array(var
, new(ctx
) ir_constant(col
));
1477 ir_instruction
*inst
= new(ctx
) ir_assignment(lhs
, rhs
, NULL
);
1478 instructions
->push_tail(inst
);
1482 /* Assign columns from the source matrix to the destination matrix.
1484 * Since the parameter will be used in the RHS of multiple assignments,
1485 * generate a temporary and copy the paramter there.
1487 ir_variable
*const rhs_var
=
1488 new(ctx
) ir_variable(first_param
->type
, "mat_ctor_mat",
1490 instructions
->push_tail(rhs_var
);
1492 ir_dereference
*const rhs_var_ref
=
1493 new(ctx
) ir_dereference_variable(rhs_var
);
1494 ir_instruction
*const inst
=
1495 new(ctx
) ir_assignment(rhs_var_ref
, first_param
, NULL
);
1496 instructions
->push_tail(inst
);
1498 const unsigned last_row
= MIN2(src_matrix
->type
->vector_elements
,
1499 var
->type
->vector_elements
);
1500 const unsigned last_col
= MIN2(src_matrix
->type
->matrix_columns
,
1501 var
->type
->matrix_columns
);
1503 unsigned swiz
[4] = { 0, 0, 0, 0 };
1504 for (unsigned i
= 1; i
< last_row
; i
++)
1507 const unsigned write_mask
= (1U << last_row
) - 1;
1509 for (unsigned i
= 0; i
< last_col
; i
++) {
1510 ir_dereference
*const lhs
=
1511 new(ctx
) ir_dereference_array(var
, new(ctx
) ir_constant(i
));
1512 ir_rvalue
*const rhs_col
=
1513 new(ctx
) ir_dereference_array(rhs_var
, new(ctx
) ir_constant(i
));
1515 /* If one matrix has columns that are smaller than the columns of the
1516 * other matrix, wrap the column access of the larger with a swizzle
1517 * so that the LHS and RHS of the assignment have the same size (and
1518 * therefore have the same type).
1520 * It would be perfectly valid to unconditionally generate the
1521 * swizzles, this this will typically result in a more compact IR tree.
1524 if (lhs
->type
->vector_elements
!= rhs_col
->type
->vector_elements
) {
1525 rhs
= new(ctx
) ir_swizzle(rhs_col
, swiz
, last_row
);
1530 ir_instruction
*inst
=
1531 new(ctx
) ir_assignment(lhs
, rhs
, NULL
, write_mask
);
1532 instructions
->push_tail(inst
);
1535 const unsigned cols
= type
->matrix_columns
;
1536 const unsigned rows
= type
->vector_elements
;
1537 unsigned remaining_slots
= rows
* cols
;
1538 unsigned col_idx
= 0;
1539 unsigned row_idx
= 0;
1541 foreach_in_list(ir_rvalue
, rhs
, parameters
) {
1542 unsigned rhs_components
= rhs
->type
->components();
1543 unsigned rhs_base
= 0;
1545 if (remaining_slots
== 0)
1548 /* Since the parameter might be used in the RHS of two assignments,
1549 * generate a temporary and copy the paramter there.
1551 ir_variable
*rhs_var
=
1552 new(ctx
) ir_variable(rhs
->type
, "mat_ctor_vec", ir_var_temporary
);
1553 instructions
->push_tail(rhs_var
);
1555 ir_dereference
*rhs_var_ref
=
1556 new(ctx
) ir_dereference_variable(rhs_var
);
1557 ir_instruction
*inst
= new(ctx
) ir_assignment(rhs_var_ref
, rhs
, NULL
);
1558 instructions
->push_tail(inst
);
1561 /* Assign the current parameter to as many components of the matrix
1564 * NOTE: A single vector parameter can span two matrix columns. A
1565 * single vec4, for example, can completely fill a mat2.
1567 unsigned count
= MIN2(rows
- row_idx
,
1568 rhs_components
- rhs_base
);
1570 rhs_var_ref
= new(ctx
) ir_dereference_variable(rhs_var
);
1571 ir_instruction
*inst
= assign_to_matrix_column(var
, col_idx
,
1576 instructions
->push_tail(inst
);
1579 remaining_slots
-= count
;
1581 /* Sometimes, there is still data left in the parameters and
1582 * components left to be set in the destination but in other
1585 if (row_idx
>= rows
) {
1589 } while(remaining_slots
> 0 && rhs_base
< rhs_components
);
1593 return new(ctx
) ir_dereference_variable(var
);
1598 emit_inline_record_constructor(const glsl_type
*type
,
1599 exec_list
*instructions
,
1600 exec_list
*parameters
,
1603 ir_variable
*const var
=
1604 new(mem_ctx
) ir_variable(type
, "record_ctor", ir_var_temporary
);
1605 ir_dereference_variable
*const d
= new(mem_ctx
) ir_dereference_variable(var
);
1607 instructions
->push_tail(var
);
1609 exec_node
*node
= parameters
->head
;
1610 for (unsigned i
= 0; i
< type
->length
; i
++) {
1611 assert(!node
->is_tail_sentinel());
1613 ir_dereference
*const lhs
=
1614 new(mem_ctx
) ir_dereference_record(d
->clone(mem_ctx
, NULL
),
1615 type
->fields
.structure
[i
].name
);
1617 ir_rvalue
*const rhs
= ((ir_instruction
*) node
)->as_rvalue();
1618 assert(rhs
!= NULL
);
1620 ir_instruction
*const assign
= new(mem_ctx
) ir_assignment(lhs
, rhs
, NULL
);
1622 instructions
->push_tail(assign
);
1631 process_record_constructor(exec_list
*instructions
,
1632 const glsl_type
*constructor_type
,
1633 YYLTYPE
*loc
, exec_list
*parameters
,
1634 struct _mesa_glsl_parse_state
*state
)
1637 exec_list actual_parameters
;
1639 process_parameters(instructions
, &actual_parameters
,
1642 exec_node
*node
= actual_parameters
.head
;
1643 for (unsigned i
= 0; i
< constructor_type
->length
; i
++) {
1644 ir_rvalue
*ir
= (ir_rvalue
*) node
;
1646 if (node
->is_tail_sentinel()) {
1647 _mesa_glsl_error(loc
, state
,
1648 "insufficient parameters to constructor for `%s'",
1649 constructor_type
->name
);
1650 return ir_rvalue::error_value(ctx
);
1653 if (apply_implicit_conversion(constructor_type
->fields
.structure
[i
].type
,
1655 node
->replace_with(ir
);
1657 _mesa_glsl_error(loc
, state
,
1658 "parameter type mismatch in constructor for `%s.%s' "
1660 constructor_type
->name
,
1661 constructor_type
->fields
.structure
[i
].name
,
1663 constructor_type
->fields
.structure
[i
].type
->name
);
1664 return ir_rvalue::error_value(ctx
);;
1670 if (!node
->is_tail_sentinel()) {
1671 _mesa_glsl_error(loc
, state
, "too many parameters in constructor "
1672 "for `%s'", constructor_type
->name
);
1673 return ir_rvalue::error_value(ctx
);
1676 ir_rvalue
*const constant
=
1677 constant_record_constructor(constructor_type
, &actual_parameters
,
1680 return (constant
!= NULL
)
1682 : emit_inline_record_constructor(constructor_type
, instructions
,
1683 &actual_parameters
, state
);
1687 ast_function_expression::handle_method(exec_list
*instructions
,
1688 struct _mesa_glsl_parse_state
*state
)
1690 const ast_expression
*field
= subexpressions
[0];
1694 /* Handle "method calls" in GLSL 1.20 - namely, array.length() */
1695 YYLTYPE loc
= get_location();
1696 state
->check_version(120, 300, &loc
, "methods not supported");
1699 method
= field
->primary_expression
.identifier
;
1701 op
= field
->subexpressions
[0]->hir(instructions
, state
);
1702 if (strcmp(method
, "length") == 0) {
1703 if (!this->expressions
.is_empty()) {
1704 _mesa_glsl_error(&loc
, state
, "length method takes no arguments");
1708 if (op
->type
->is_array()) {
1709 if (op
->type
->is_unsized_array()) {
1710 if (!state
->has_shader_storage_buffer_objects()) {
1711 _mesa_glsl_error(&loc
, state
, "length called on unsized array"
1712 " only available with "
1713 "ARB_shader_storage_buffer_object");
1715 /* Calculate length of an unsized array in run-time */
1716 result
= new(ctx
) ir_expression(ir_unop_ssbo_unsized_array_length
, op
);
1718 result
= new(ctx
) ir_constant(op
->type
->array_size());
1720 } else if (op
->type
->is_vector()) {
1721 if (state
->ARB_shading_language_420pack_enable
) {
1722 /* .length() returns int. */
1723 result
= new(ctx
) ir_constant((int) op
->type
->vector_elements
);
1725 _mesa_glsl_error(&loc
, state
, "length method on matrix only available"
1726 "with ARB_shading_language_420pack");
1729 } else if (op
->type
->is_matrix()) {
1730 if (state
->ARB_shading_language_420pack_enable
) {
1731 /* .length() returns int. */
1732 result
= new(ctx
) ir_constant((int) op
->type
->matrix_columns
);
1734 _mesa_glsl_error(&loc
, state
, "length method on matrix only available"
1735 "with ARB_shading_language_420pack");
1739 _mesa_glsl_error(&loc
, state
, "length called on scalar.");
1743 _mesa_glsl_error(&loc
, state
, "unknown method: `%s'", method
);
1748 return ir_rvalue::error_value(ctx
);
1752 ast_function_expression::hir(exec_list
*instructions
,
1753 struct _mesa_glsl_parse_state
*state
)
1756 /* There are three sorts of function calls.
1758 * 1. constructors - The first subexpression is an ast_type_specifier.
1759 * 2. methods - Only the .length() method of array types.
1760 * 3. functions - Calls to regular old functions.
1763 if (is_constructor()) {
1764 const ast_type_specifier
*type
= (ast_type_specifier
*) subexpressions
[0];
1765 YYLTYPE loc
= type
->get_location();
1768 const glsl_type
*const constructor_type
= type
->glsl_type(& name
, state
);
1770 /* constructor_type can be NULL if a variable with the same name as the
1771 * structure has come into scope.
1773 if (constructor_type
== NULL
) {
1774 _mesa_glsl_error(& loc
, state
, "unknown type `%s' (structure name "
1775 "may be shadowed by a variable with the same name)",
1777 return ir_rvalue::error_value(ctx
);
1781 /* Constructors for opaque types are illegal.
1783 if (constructor_type
->contains_opaque()) {
1784 _mesa_glsl_error(& loc
, state
, "cannot construct opaque type `%s'",
1785 constructor_type
->name
);
1786 return ir_rvalue::error_value(ctx
);
1789 if (constructor_type
->is_array()) {
1790 if (!state
->check_version(120, 300, &loc
,
1791 "array constructors forbidden")) {
1792 return ir_rvalue::error_value(ctx
);
1795 return process_array_constructor(instructions
, constructor_type
,
1796 & loc
, &this->expressions
, state
);
1800 /* There are two kinds of constructor calls. Constructors for arrays and
1801 * structures must have the exact number of arguments with matching types
1802 * in the correct order. These constructors follow essentially the same
1803 * type matching rules as functions.
1805 * Constructors for built-in language types, such as mat4 and vec2, are
1806 * free form. The only requirements are that the parameters must provide
1807 * enough values of the correct scalar type and that no arguments are
1808 * given past the last used argument.
1810 * When using the C-style initializer syntax from GLSL 4.20, constructors
1811 * must have the exact number of arguments with matching types in the
1814 if (constructor_type
->is_record()) {
1815 return process_record_constructor(instructions
, constructor_type
,
1816 &loc
, &this->expressions
,
1820 if (!constructor_type
->is_numeric() && !constructor_type
->is_boolean())
1821 return ir_rvalue::error_value(ctx
);
1823 /* Total number of components of the type being constructed. */
1824 const unsigned type_components
= constructor_type
->components();
1826 /* Number of components from parameters that have actually been
1827 * consumed. This is used to perform several kinds of error checking.
1829 unsigned components_used
= 0;
1831 unsigned matrix_parameters
= 0;
1832 unsigned nonmatrix_parameters
= 0;
1833 exec_list actual_parameters
;
1835 foreach_list_typed(ast_node
, ast
, link
, &this->expressions
) {
1836 ir_rvalue
*result
= ast
->hir(instructions
, state
);
1838 /* From page 50 (page 56 of the PDF) of the GLSL 1.50 spec:
1840 * "It is an error to provide extra arguments beyond this
1841 * last used argument."
1843 if (components_used
>= type_components
) {
1844 _mesa_glsl_error(& loc
, state
, "too many parameters to `%s' "
1846 constructor_type
->name
);
1847 return ir_rvalue::error_value(ctx
);
1850 if (!result
->type
->is_numeric() && !result
->type
->is_boolean()) {
1851 _mesa_glsl_error(& loc
, state
, "cannot construct `%s' from a "
1852 "non-numeric data type",
1853 constructor_type
->name
);
1854 return ir_rvalue::error_value(ctx
);
1857 /* Count the number of matrix and nonmatrix parameters. This
1858 * is used below to enforce some of the constructor rules.
1860 if (result
->type
->is_matrix())
1861 matrix_parameters
++;
1863 nonmatrix_parameters
++;
1865 actual_parameters
.push_tail(result
);
1866 components_used
+= result
->type
->components();
1869 /* From page 28 (page 34 of the PDF) of the GLSL 1.10 spec:
1871 * "It is an error to construct matrices from other matrices. This
1872 * is reserved for future use."
1874 if (matrix_parameters
> 0
1875 && constructor_type
->is_matrix()
1876 && !state
->check_version(120, 100, &loc
,
1877 "cannot construct `%s' from a matrix",
1878 constructor_type
->name
)) {
1879 return ir_rvalue::error_value(ctx
);
1882 /* From page 50 (page 56 of the PDF) of the GLSL 1.50 spec:
1884 * "If a matrix argument is given to a matrix constructor, it is
1885 * an error to have any other arguments."
1887 if ((matrix_parameters
> 0)
1888 && ((matrix_parameters
+ nonmatrix_parameters
) > 1)
1889 && constructor_type
->is_matrix()) {
1890 _mesa_glsl_error(& loc
, state
, "for matrix `%s' constructor, "
1891 "matrix must be only parameter",
1892 constructor_type
->name
);
1893 return ir_rvalue::error_value(ctx
);
1896 /* From page 28 (page 34 of the PDF) of the GLSL 1.10 spec:
1898 * "In these cases, there must be enough components provided in the
1899 * arguments to provide an initializer for every component in the
1900 * constructed value."
1902 if (components_used
< type_components
&& components_used
!= 1
1903 && matrix_parameters
== 0) {
1904 _mesa_glsl_error(& loc
, state
, "too few components to construct "
1906 constructor_type
->name
);
1907 return ir_rvalue::error_value(ctx
);
1910 /* Matrices can never be consumed as is by any constructor but matrix
1911 * constructors. If the constructor type is not matrix, always break the
1912 * matrix up into a series of column vectors.
1914 if (!constructor_type
->is_matrix()) {
1915 foreach_in_list_safe(ir_rvalue
, matrix
, &actual_parameters
) {
1916 if (!matrix
->type
->is_matrix())
1919 /* Create a temporary containing the matrix. */
1920 ir_variable
*var
= new(ctx
) ir_variable(matrix
->type
, "matrix_tmp",
1922 instructions
->push_tail(var
);
1923 instructions
->push_tail(new(ctx
) ir_assignment(new(ctx
)
1924 ir_dereference_variable(var
), matrix
, NULL
));
1925 var
->constant_value
= matrix
->constant_expression_value();
1927 /* Replace the matrix with dereferences of its columns. */
1928 for (int i
= 0; i
< matrix
->type
->matrix_columns
; i
++) {
1929 matrix
->insert_before(new (ctx
) ir_dereference_array(var
,
1930 new(ctx
) ir_constant(i
)));
1936 bool all_parameters_are_constant
= true;
1938 /* Type cast each parameter and, if possible, fold constants.*/
1939 foreach_in_list_safe(ir_rvalue
, ir
, &actual_parameters
) {
1940 const glsl_type
*desired_type
=
1941 glsl_type::get_instance(constructor_type
->base_type
,
1942 ir
->type
->vector_elements
,
1943 ir
->type
->matrix_columns
);
1944 ir_rvalue
*result
= convert_component(ir
, desired_type
);
1946 /* Attempt to convert the parameter to a constant valued expression.
1947 * After doing so, track whether or not all the parameters to the
1948 * constructor are trivially constant valued expressions.
1950 ir_rvalue
*const constant
= result
->constant_expression_value();
1952 if (constant
!= NULL
)
1955 all_parameters_are_constant
= false;
1958 ir
->replace_with(result
);
1962 /* If all of the parameters are trivially constant, create a
1963 * constant representing the complete collection of parameters.
1965 if (all_parameters_are_constant
) {
1966 return new(ctx
) ir_constant(constructor_type
, &actual_parameters
);
1967 } else if (constructor_type
->is_scalar()) {
1968 return dereference_component((ir_rvalue
*) actual_parameters
.head
,
1970 } else if (constructor_type
->is_vector()) {
1971 return emit_inline_vector_constructor(constructor_type
,
1976 assert(constructor_type
->is_matrix());
1977 return emit_inline_matrix_constructor(constructor_type
,
1982 } else if (subexpressions
[0]->oper
== ast_field_selection
) {
1983 return handle_method(instructions
, state
);
1985 const ast_expression
*id
= subexpressions
[0];
1986 const char *func_name
;
1987 YYLTYPE loc
= get_location();
1988 exec_list actual_parameters
;
1989 ir_variable
*sub_var
= NULL
;
1990 ir_rvalue
*array_idx
= NULL
;
1992 if (id
->oper
== ast_array_index
) {
1993 func_name
= id
->subexpressions
[0]->primary_expression
.identifier
;
1994 array_idx
= id
->subexpressions
[1]->hir(instructions
, state
);
1996 func_name
= id
->primary_expression
.identifier
;
1999 process_parameters(instructions
, &actual_parameters
, &this->expressions
,
2002 ir_function_signature
*sig
=
2003 match_function_by_name(func_name
, &actual_parameters
, state
);
2005 ir_rvalue
*value
= NULL
;
2007 sig
= match_subroutine_by_name(func_name
, &actual_parameters
, state
, &sub_var
);
2011 no_matching_function_error(func_name
, &loc
, &actual_parameters
, state
);
2012 value
= ir_rvalue::error_value(ctx
);
2013 } else if (!verify_parameter_modes(state
, sig
, actual_parameters
, this->expressions
)) {
2014 /* an error has already been emitted */
2015 value
= ir_rvalue::error_value(ctx
);
2017 value
= generate_call(instructions
, sig
, &actual_parameters
, sub_var
, array_idx
, state
);
2019 ir_variable
*const tmp
= new(ctx
) ir_variable(glsl_type::void_type
,
2022 instructions
->push_tail(tmp
);
2023 value
= new(ctx
) ir_dereference_variable(tmp
);
2030 unreachable("not reached");
2034 ast_function_expression::has_sequence_subexpression() const
2036 foreach_list_typed(const ast_node
, ast
, link
, &this->expressions
) {
2037 if (ast
->has_sequence_subexpression())
2045 ast_aggregate_initializer::hir(exec_list
*instructions
,
2046 struct _mesa_glsl_parse_state
*state
)
2049 YYLTYPE loc
= this->get_location();
2051 if (!this->constructor_type
) {
2052 _mesa_glsl_error(&loc
, state
, "type of C-style initializer unknown");
2053 return ir_rvalue::error_value(ctx
);
2055 const glsl_type
*const constructor_type
= this->constructor_type
;
2057 if (!state
->ARB_shading_language_420pack_enable
) {
2058 _mesa_glsl_error(&loc
, state
, "C-style initialization requires the "
2059 "GL_ARB_shading_language_420pack extension");
2060 return ir_rvalue::error_value(ctx
);
2063 if (constructor_type
->is_array()) {
2064 return process_array_constructor(instructions
, constructor_type
, &loc
,
2065 &this->expressions
, state
);
2068 if (constructor_type
->is_record()) {
2069 return process_record_constructor(instructions
, constructor_type
, &loc
,
2070 &this->expressions
, state
);
2073 return process_vec_mat_constructor(instructions
, constructor_type
, &loc
,
2074 &this->expressions
, state
);