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
)
42 foreach_list_typed(ast_node
, ast
, link
, parameters
) {
43 /* We need to process the parameters first in order to know if we can
44 * raise or not a unitialized warning. Calling set_is_lhs silence the
45 * warning for now. Raising the warning or not will be checked at
46 * verify_parameter_modes.
48 ast
->set_is_lhs(true);
49 ir_rvalue
*result
= ast
->hir(instructions
, state
);
51 ir_constant
*const constant
= result
->constant_expression_value();
55 actual_parameters
->push_tail(result
);
64 * Generate a source prototype for a function signature
66 * \param return_type Return type of the function. May be \c NULL.
67 * \param name Name of the function.
68 * \param parameters List of \c ir_instruction nodes representing the
69 * parameter list for the function. This may be either a
70 * formal (\c ir_variable) or actual (\c ir_rvalue)
71 * parameter list. Only the type is used.
74 * A ralloced string representing the prototype of the function.
77 prototype_string(const glsl_type
*return_type
, const char *name
,
78 exec_list
*parameters
)
82 if (return_type
!= NULL
)
83 str
= ralloc_asprintf(NULL
, "%s ", return_type
->name
);
85 ralloc_asprintf_append(&str
, "%s(", name
);
87 const char *comma
= "";
88 foreach_in_list(const ir_variable
, param
, parameters
) {
89 ralloc_asprintf_append(&str
, "%s%s", comma
, param
->type
->name
);
93 ralloc_strcat(&str
, ")");
98 verify_image_parameter(YYLTYPE
*loc
, _mesa_glsl_parse_state
*state
,
99 const ir_variable
*formal
, const ir_variable
*actual
)
102 * From the ARB_shader_image_load_store specification:
104 * "The values of image variables qualified with coherent,
105 * volatile, restrict, readonly, or writeonly may not be passed
106 * to functions whose formal parameters lack such
107 * qualifiers. [...] It is legal to have additional qualifiers
108 * on a formal parameter, but not to have fewer."
110 if (actual
->data
.image_coherent
&& !formal
->data
.image_coherent
) {
111 _mesa_glsl_error(loc
, state
,
112 "function call parameter `%s' drops "
113 "`coherent' qualifier", formal
->name
);
117 if (actual
->data
.image_volatile
&& !formal
->data
.image_volatile
) {
118 _mesa_glsl_error(loc
, state
,
119 "function call parameter `%s' drops "
120 "`volatile' qualifier", formal
->name
);
124 if (actual
->data
.image_restrict
&& !formal
->data
.image_restrict
) {
125 _mesa_glsl_error(loc
, state
,
126 "function call parameter `%s' drops "
127 "`restrict' qualifier", formal
->name
);
131 if (actual
->data
.image_read_only
&& !formal
->data
.image_read_only
) {
132 _mesa_glsl_error(loc
, state
,
133 "function call parameter `%s' drops "
134 "`readonly' qualifier", formal
->name
);
138 if (actual
->data
.image_write_only
&& !formal
->data
.image_write_only
) {
139 _mesa_glsl_error(loc
, state
,
140 "function call parameter `%s' drops "
141 "`writeonly' qualifier", formal
->name
);
149 verify_first_atomic_parameter(YYLTYPE
*loc
, _mesa_glsl_parse_state
*state
,
153 (!var
->is_in_shader_storage_block() &&
154 var
->data
.mode
!= ir_var_shader_shared
)) {
155 _mesa_glsl_error(loc
, state
, "First argument to atomic function "
156 "must be a buffer or shared variable");
163 is_atomic_function(const char *func_name
)
165 return !strcmp(func_name
, "atomicAdd") ||
166 !strcmp(func_name
, "atomicMin") ||
167 !strcmp(func_name
, "atomicMax") ||
168 !strcmp(func_name
, "atomicAnd") ||
169 !strcmp(func_name
, "atomicOr") ||
170 !strcmp(func_name
, "atomicXor") ||
171 !strcmp(func_name
, "atomicExchange") ||
172 !strcmp(func_name
, "atomicCompSwap");
176 * Verify that 'out' and 'inout' actual parameters are lvalues. Also, verify
177 * that 'const_in' formal parameters (an extension in our IR) correspond to
178 * ir_constant actual parameters.
181 verify_parameter_modes(_mesa_glsl_parse_state
*state
,
182 ir_function_signature
*sig
,
183 exec_list
&actual_ir_parameters
,
184 exec_list
&actual_ast_parameters
)
186 exec_node
*actual_ir_node
= actual_ir_parameters
.get_head_raw();
187 exec_node
*actual_ast_node
= actual_ast_parameters
.get_head_raw();
189 foreach_in_list(const ir_variable
, formal
, &sig
->parameters
) {
190 /* The lists must be the same length. */
191 assert(!actual_ir_node
->is_tail_sentinel());
192 assert(!actual_ast_node
->is_tail_sentinel());
194 const ir_rvalue
*const actual
= (ir_rvalue
*) actual_ir_node
;
195 const ast_expression
*const actual_ast
=
196 exec_node_data(ast_expression
, actual_ast_node
, link
);
198 /* FIXME: 'loc' is incorrect (as of 2011-01-21). It is always
201 YYLTYPE loc
= actual_ast
->get_location();
203 /* Verify that 'const_in' parameters are ir_constants. */
204 if (formal
->data
.mode
== ir_var_const_in
&&
205 actual
->ir_type
!= ir_type_constant
) {
206 _mesa_glsl_error(&loc
, state
,
207 "parameter `in %s' must be a constant expression",
212 /* Verify that shader_in parameters are shader inputs */
213 if (formal
->data
.must_be_shader_input
) {
214 const ir_rvalue
*val
= actual
;
216 /* GLSL 4.40 allows swizzles, while earlier GLSL versions do not. */
217 if (val
->ir_type
== ir_type_swizzle
) {
218 if (!state
->is_version(440, 0)) {
219 _mesa_glsl_error(&loc
, state
,
220 "parameter `%s` must not be swizzled",
224 val
= ((ir_swizzle
*)val
)->val
;
227 while (val
->ir_type
== ir_type_dereference_array
) {
228 val
= ((ir_dereference_array
*)val
)->array
;
231 if (!val
->as_dereference_variable() ||
232 val
->variable_referenced()->data
.mode
!= ir_var_shader_in
) {
233 _mesa_glsl_error(&loc
, state
,
234 "parameter `%s` must be a shader input",
239 val
->variable_referenced()->data
.must_be_shader_input
= 1;
242 /* Verify that 'out' and 'inout' actual parameters are lvalues. */
243 if (formal
->data
.mode
== ir_var_function_out
244 || formal
->data
.mode
== ir_var_function_inout
) {
245 const char *mode
= NULL
;
246 switch (formal
->data
.mode
) {
247 case ir_var_function_out
: mode
= "out"; break;
248 case ir_var_function_inout
: mode
= "inout"; break;
249 default: assert(false); break;
252 /* This AST-based check catches errors like f(i++). The IR-based
253 * is_lvalue() is insufficient because the actual parameter at the
254 * IR-level is just a temporary value, which is an l-value.
256 if (actual_ast
->non_lvalue_description
!= NULL
) {
257 _mesa_glsl_error(&loc
, state
,
258 "function parameter '%s %s' references a %s",
260 actual_ast
->non_lvalue_description
);
264 ir_variable
*var
= actual
->variable_referenced();
266 if (var
&& formal
->data
.mode
== ir_var_function_inout
) {
267 if ((var
->data
.mode
== ir_var_auto
||
268 var
->data
.mode
== ir_var_shader_out
) &&
269 !var
->data
.assigned
&&
270 !is_gl_identifier(var
->name
)) {
271 _mesa_glsl_warning(&loc
, state
, "`%s' used uninitialized",
277 var
->data
.assigned
= true;
279 if (var
&& var
->data
.read_only
) {
280 _mesa_glsl_error(&loc
, state
,
281 "function parameter '%s %s' references the "
282 "read-only variable '%s'",
284 actual
->variable_referenced()->name
);
286 } else if (!actual
->is_lvalue()) {
287 _mesa_glsl_error(&loc
, state
,
288 "function parameter '%s %s' is not an lvalue",
293 assert(formal
->data
.mode
== ir_var_function_in
||
294 formal
->data
.mode
== ir_var_const_in
);
295 ir_variable
*var
= actual
->variable_referenced();
297 if ((var
->data
.mode
== ir_var_auto
||
298 var
->data
.mode
== ir_var_shader_out
) &&
299 !var
->data
.assigned
&&
300 !is_gl_identifier(var
->name
)) {
301 _mesa_glsl_warning(&loc
, state
, "`%s' used uninitialized",
307 if (formal
->type
->is_image() &&
308 actual
->variable_referenced()) {
309 if (!verify_image_parameter(&loc
, state
, formal
,
310 actual
->variable_referenced()))
314 actual_ir_node
= actual_ir_node
->next
;
315 actual_ast_node
= actual_ast_node
->next
;
318 /* The first parameter of atomic functions must be a buffer variable */
319 const char *func_name
= sig
->function_name();
320 bool is_atomic
= is_atomic_function(func_name
);
322 const ir_rvalue
*const actual
=
323 (ir_rvalue
*) actual_ir_parameters
.get_head_raw();
325 const ast_expression
*const actual_ast
=
326 exec_node_data(ast_expression
,
327 actual_ast_parameters
.get_head_raw(), link
);
328 YYLTYPE loc
= actual_ast
->get_location();
330 if (!verify_first_atomic_parameter(&loc
, state
,
331 actual
->variable_referenced())) {
340 fix_parameter(void *mem_ctx
, ir_rvalue
*actual
, const glsl_type
*formal_type
,
341 exec_list
*before_instructions
, exec_list
*after_instructions
,
342 bool parameter_is_inout
)
344 ir_expression
*const expr
= actual
->as_expression();
346 /* If the types match exactly and the parameter is not a vector-extract,
347 * nothing needs to be done to fix the parameter.
349 if (formal_type
== actual
->type
350 && (expr
== NULL
|| expr
->operation
!= ir_binop_vector_extract
))
353 /* To convert an out parameter, we need to create a temporary variable to
354 * hold the value before conversion, and then perform the conversion after
355 * the function call returns.
357 * This has the effect of transforming code like this:
363 * Into IR that's equivalent to this:
367 * int out_parameter_conversion;
368 * f(out_parameter_conversion);
369 * value = float(out_parameter_conversion);
371 * If the parameter is an ir_expression of ir_binop_vector_extract,
372 * additional conversion is needed in the post-call re-write.
375 new(mem_ctx
) ir_variable(formal_type
, "inout_tmp", ir_var_temporary
);
377 before_instructions
->push_tail(tmp
);
379 /* If the parameter is an inout parameter, copy the value of the actual
380 * parameter to the new temporary. Note that no type conversion is allowed
381 * here because inout parameters must match types exactly.
383 if (parameter_is_inout
) {
384 /* Inout parameters should never require conversion, since that would
385 * require an implicit conversion to exist both to and from the formal
386 * parameter type, and there are no bidirectional implicit conversions.
388 assert (actual
->type
== formal_type
);
390 ir_dereference_variable
*const deref_tmp_1
=
391 new(mem_ctx
) ir_dereference_variable(tmp
);
392 ir_assignment
*const assignment
=
393 new(mem_ctx
) ir_assignment(deref_tmp_1
, actual
);
394 before_instructions
->push_tail(assignment
);
397 /* Replace the parameter in the call with a dereference of the new
400 ir_dereference_variable
*const deref_tmp_2
=
401 new(mem_ctx
) ir_dereference_variable(tmp
);
402 actual
->replace_with(deref_tmp_2
);
405 /* Copy the temporary variable to the actual parameter with optional
406 * type conversion applied.
408 ir_rvalue
*rhs
= new(mem_ctx
) ir_dereference_variable(tmp
);
409 if (actual
->type
!= formal_type
)
410 rhs
= convert_component(rhs
, actual
->type
);
412 ir_rvalue
*lhs
= actual
;
413 if (expr
!= NULL
&& expr
->operation
== ir_binop_vector_extract
) {
414 lhs
= new(mem_ctx
) ir_dereference_array(expr
->operands
[0]->clone(mem_ctx
,
416 expr
->operands
[1]->clone(mem_ctx
,
420 ir_assignment
*const assignment_2
= new(mem_ctx
) ir_assignment(lhs
, rhs
);
421 after_instructions
->push_tail(assignment_2
);
425 * Generate a function call.
427 * For non-void functions, this returns a dereference of the temporary
428 * variable which stores the return value for the call. For void functions,
432 generate_call(exec_list
*instructions
, ir_function_signature
*sig
,
433 exec_list
*actual_parameters
,
434 ir_variable
*sub_var
,
435 ir_rvalue
*array_idx
,
436 struct _mesa_glsl_parse_state
*state
,
437 bool inline_immediately
)
440 exec_list post_call_conversions
;
442 /* Perform implicit conversion of arguments. For out parameters, we need
443 * to place them in a temporary variable and do the conversion after the
444 * call takes place. Since we haven't emitted the call yet, we'll place
445 * the post-call conversions in a temporary exec_list, and emit them later.
447 foreach_two_lists(formal_node
, &sig
->parameters
,
448 actual_node
, actual_parameters
) {
449 ir_rvalue
*actual
= (ir_rvalue
*) actual_node
;
450 ir_variable
*formal
= (ir_variable
*) formal_node
;
452 if (formal
->type
->is_numeric() || formal
->type
->is_boolean()) {
453 switch (formal
->data
.mode
) {
454 case ir_var_const_in
:
455 case ir_var_function_in
: {
457 = convert_component(actual
, formal
->type
);
458 actual
->replace_with(converted
);
461 case ir_var_function_out
:
462 case ir_var_function_inout
:
463 fix_parameter(ctx
, actual
, formal
->type
,
464 instructions
, &post_call_conversions
,
465 formal
->data
.mode
== ir_var_function_inout
);
468 assert (!"Illegal formal parameter mode");
474 /* Section 4.3.2 (Const) of the GLSL 1.10.59 spec says:
476 * "Initializers for const declarations must be formed from literal
477 * values, other const variables (not including function call
478 * paramaters), or expressions of these.
480 * Constructors may be used in such expressions, but function calls may
483 * Section 4.3.3 (Constant Expressions) of the GLSL 1.20.8 spec says:
485 * "A constant expression is one of
489 * - a built-in function call whose arguments are all constant
490 * expressions, with the exception of the texture lookup
491 * functions, the noise functions, and ftransform. The built-in
492 * functions dFdx, dFdy, and fwidth must return 0 when evaluated
493 * inside an initializer with an argument that is a constant
496 * Section 5.10 (Constant Expressions) of the GLSL ES 1.00.17 spec says:
498 * "A constant expression is one of
502 * - a built-in function call whose arguments are all constant
503 * expressions, with the exception of the texture lookup
506 * Section 4.3.3 (Constant Expressions) of the GLSL ES 3.00.4 spec says:
508 * "A constant expression is one of
512 * - a built-in function call whose arguments are all constant
513 * expressions, with the exception of the texture lookup
514 * functions. The built-in functions dFdx, dFdy, and fwidth must
515 * return 0 when evaluated inside an initializer with an argument
516 * that is a constant expression."
518 * If the function call is a constant expression, don't generate any
519 * instructions; just generate an ir_constant.
521 if (state
->is_version(120, 100)) {
522 ir_constant
*value
= sig
->constant_expression_value(actual_parameters
,
529 ir_dereference_variable
*deref
= NULL
;
530 if (!sig
->return_type
->is_void()) {
531 /* Create a new temporary to hold the return value. */
532 char *const name
= ir_variable::temporaries_allocate_names
533 ? ralloc_asprintf(ctx
, "%s_retval", sig
->function_name())
538 var
= new(ctx
) ir_variable(sig
->return_type
, name
, ir_var_temporary
);
539 instructions
->push_tail(var
);
543 deref
= new(ctx
) ir_dereference_variable(var
);
546 ir_call
*call
= new(ctx
) ir_call(sig
, deref
,
547 actual_parameters
, sub_var
, array_idx
);
548 instructions
->push_tail(call
);
549 if (inline_immediately
) {
550 call
->generate_inline(call
);
554 /* Also emit any necessary out-parameter conversions. */
555 instructions
->append_list(&post_call_conversions
);
557 return deref
? deref
->clone(ctx
, NULL
) : NULL
;
561 * Given a function name and parameter list, find the matching signature.
563 static ir_function_signature
*
564 match_function_by_name(const char *name
,
565 exec_list
*actual_parameters
,
566 struct _mesa_glsl_parse_state
*state
)
568 ir_function
*f
= state
->symbols
->get_function(name
);
569 ir_function_signature
*local_sig
= NULL
;
570 ir_function_signature
*sig
= NULL
;
572 /* Is the function hidden by a record type constructor? */
573 if (state
->symbols
->get_type(name
))
574 return sig
; /* no match */
576 /* Is the function hidden by a variable (impossible in 1.10)? */
577 if (!state
->symbols
->separate_function_namespace
578 && state
->symbols
->get_variable(name
))
579 return sig
; /* no match */
582 /* In desktop GL, the presence of a user-defined signature hides any
583 * built-in signatures, so we must ignore them. In contrast, in ES2
584 * user-defined signatures add new overloads, so we must consider them.
586 bool allow_builtins
= state
->es_shader
|| !f
->has_user_signature();
588 /* Look for a match in the local shader. If exact, we're done. */
589 bool is_exact
= false;
590 sig
= local_sig
= f
->matching_signature(state
, actual_parameters
,
591 allow_builtins
, &is_exact
);
599 /* Local shader has no exact candidates; check the built-ins. */
600 _mesa_glsl_initialize_builtin_functions();
601 sig
= _mesa_glsl_find_builtin_function(state
, name
, actual_parameters
);
605 static ir_function_signature
*
606 match_subroutine_by_name(const char *name
,
607 exec_list
*actual_parameters
,
608 struct _mesa_glsl_parse_state
*state
,
612 ir_function_signature
*sig
= NULL
;
613 ir_function
*f
, *found
= NULL
;
614 const char *new_name
;
616 bool is_exact
= false;
619 ralloc_asprintf(ctx
, "%s_%s",
620 _mesa_shader_stage_to_subroutine_prefix(state
->stage
),
622 var
= state
->symbols
->get_variable(new_name
);
626 for (int i
= 0; i
< state
->num_subroutine_types
; i
++) {
627 f
= state
->subroutine_types
[i
];
628 if (strcmp(f
->name
, var
->type
->without_array()->name
))
637 sig
= found
->matching_signature(state
, actual_parameters
,
643 generate_array_index(void *mem_ctx
, exec_list
*instructions
,
644 struct _mesa_glsl_parse_state
*state
, YYLTYPE loc
,
645 const ast_expression
*array
, ast_expression
*idx
,
646 const char **function_name
, exec_list
*actual_parameters
)
648 if (array
->oper
== ast_array_index
) {
649 /* This handles arrays of arrays */
650 ir_rvalue
*outer_array
= generate_array_index(mem_ctx
, instructions
,
652 array
->subexpressions
[0],
653 array
->subexpressions
[1],
656 ir_rvalue
*outer_array_idx
= idx
->hir(instructions
, state
);
658 YYLTYPE index_loc
= idx
->get_location();
659 return _mesa_ast_array_index_to_hir(mem_ctx
, state
, outer_array
,
660 outer_array_idx
, loc
,
663 ir_variable
*sub_var
= NULL
;
664 *function_name
= array
->primary_expression
.identifier
;
666 match_subroutine_by_name(*function_name
, actual_parameters
,
669 ir_rvalue
*outer_array_idx
= idx
->hir(instructions
, state
);
670 return new(mem_ctx
) ir_dereference_array(sub_var
, outer_array_idx
);
675 print_function_prototypes(_mesa_glsl_parse_state
*state
, YYLTYPE
*loc
,
681 foreach_in_list(ir_function_signature
, sig
, &f
->signatures
) {
682 if (sig
->is_builtin() && !sig
->is_builtin_available(state
))
685 char *str
= prototype_string(sig
->return_type
, f
->name
,
687 _mesa_glsl_error(loc
, state
, " %s", str
);
693 * Raise a "no matching function" error, listing all possible overloads the
694 * compiler considered so developers can figure out what went wrong.
697 no_matching_function_error(const char *name
,
699 exec_list
*actual_parameters
,
700 _mesa_glsl_parse_state
*state
)
702 gl_shader
*sh
= _mesa_glsl_get_builtin_function_shader();
704 if (state
->symbols
->get_function(name
) == NULL
705 && (!state
->uses_builtin_functions
706 || sh
->symbols
->get_function(name
) == NULL
)) {
707 _mesa_glsl_error(loc
, state
, "no function with name '%s'", name
);
709 char *str
= prototype_string(NULL
, name
, actual_parameters
);
710 _mesa_glsl_error(loc
, state
,
711 "no matching function for call to `%s';"
716 print_function_prototypes(state
, loc
,
717 state
->symbols
->get_function(name
));
719 if (state
->uses_builtin_functions
) {
720 print_function_prototypes(state
, loc
,
721 sh
->symbols
->get_function(name
));
727 * Perform automatic type conversion of constructor parameters
729 * This implements the rules in the "Conversion and Scalar Constructors"
730 * section (GLSL 1.10 section 5.4.1), not the "Implicit Conversions" rules.
733 convert_component(ir_rvalue
*src
, const glsl_type
*desired_type
)
735 void *ctx
= ralloc_parent(src
);
736 const unsigned a
= desired_type
->base_type
;
737 const unsigned b
= src
->type
->base_type
;
738 ir_expression
*result
= NULL
;
740 if (src
->type
->is_error())
743 assert(a
<= GLSL_TYPE_BOOL
);
744 assert(b
<= GLSL_TYPE_BOOL
);
753 result
= new(ctx
) ir_expression(ir_unop_i2u
, src
);
755 case GLSL_TYPE_FLOAT
:
756 result
= new(ctx
) ir_expression(ir_unop_f2u
, src
);
759 result
= new(ctx
) ir_expression(ir_unop_i2u
,
760 new(ctx
) ir_expression(ir_unop_b2i
,
763 case GLSL_TYPE_DOUBLE
:
764 result
= new(ctx
) ir_expression(ir_unop_d2u
, src
);
766 case GLSL_TYPE_UINT64
:
767 result
= new(ctx
) ir_expression(ir_unop_u642u
, src
);
769 case GLSL_TYPE_INT64
:
770 result
= new(ctx
) ir_expression(ir_unop_i642u
, src
);
777 result
= new(ctx
) ir_expression(ir_unop_u2i
, src
);
779 case GLSL_TYPE_FLOAT
:
780 result
= new(ctx
) ir_expression(ir_unop_f2i
, src
);
783 result
= new(ctx
) ir_expression(ir_unop_b2i
, src
);
785 case GLSL_TYPE_DOUBLE
:
786 result
= new(ctx
) ir_expression(ir_unop_d2i
, src
);
788 case GLSL_TYPE_UINT64
:
789 result
= new(ctx
) ir_expression(ir_unop_u642i
, src
);
791 case GLSL_TYPE_INT64
:
792 result
= new(ctx
) ir_expression(ir_unop_i642i
, src
);
796 case GLSL_TYPE_FLOAT
:
799 result
= new(ctx
) ir_expression(ir_unop_u2f
, desired_type
, src
, NULL
);
802 result
= new(ctx
) ir_expression(ir_unop_i2f
, desired_type
, src
, NULL
);
805 result
= new(ctx
) ir_expression(ir_unop_b2f
, desired_type
, src
, NULL
);
807 case GLSL_TYPE_DOUBLE
:
808 result
= new(ctx
) ir_expression(ir_unop_d2f
, desired_type
, src
, NULL
);
810 case GLSL_TYPE_UINT64
:
811 result
= new(ctx
) ir_expression(ir_unop_u642f
, desired_type
, src
, NULL
);
813 case GLSL_TYPE_INT64
:
814 result
= new(ctx
) ir_expression(ir_unop_i642f
, desired_type
, src
, NULL
);
821 result
= new(ctx
) ir_expression(ir_unop_i2b
,
822 new(ctx
) ir_expression(ir_unop_u2i
,
826 result
= new(ctx
) ir_expression(ir_unop_i2b
, desired_type
, src
, NULL
);
828 case GLSL_TYPE_FLOAT
:
829 result
= new(ctx
) ir_expression(ir_unop_f2b
, desired_type
, src
, NULL
);
831 case GLSL_TYPE_DOUBLE
:
832 result
= new(ctx
) ir_expression(ir_unop_d2b
, desired_type
, src
, NULL
);
834 case GLSL_TYPE_UINT64
:
835 result
= new(ctx
) ir_expression(ir_unop_i642b
,
836 new(ctx
) ir_expression(ir_unop_u642i64
,
839 case GLSL_TYPE_INT64
:
840 result
= new(ctx
) ir_expression(ir_unop_i642b
, desired_type
, src
, NULL
);
844 case GLSL_TYPE_DOUBLE
:
847 result
= new(ctx
) ir_expression(ir_unop_i2d
, src
);
850 result
= new(ctx
) ir_expression(ir_unop_u2d
, src
);
853 result
= new(ctx
) ir_expression(ir_unop_f2d
,
854 new(ctx
) ir_expression(ir_unop_b2f
,
857 case GLSL_TYPE_FLOAT
:
858 result
= new(ctx
) ir_expression(ir_unop_f2d
, desired_type
, src
, NULL
);
860 case GLSL_TYPE_UINT64
:
861 result
= new(ctx
) ir_expression(ir_unop_u642d
, desired_type
, src
, NULL
);
863 case GLSL_TYPE_INT64
:
864 result
= new(ctx
) ir_expression(ir_unop_i642d
, desired_type
, src
, NULL
);
868 case GLSL_TYPE_UINT64
:
871 result
= new(ctx
) ir_expression(ir_unop_i2u64
, src
);
874 result
= new(ctx
) ir_expression(ir_unop_u2u64
, src
);
877 result
= new(ctx
) ir_expression(ir_unop_i642u64
,
878 new(ctx
) ir_expression(ir_unop_b2i64
,
881 case GLSL_TYPE_FLOAT
:
882 result
= new(ctx
) ir_expression(ir_unop_f2u64
, src
);
884 case GLSL_TYPE_DOUBLE
:
885 result
= new(ctx
) ir_expression(ir_unop_d2u64
, src
);
887 case GLSL_TYPE_INT64
:
888 result
= new(ctx
) ir_expression(ir_unop_i642u64
, src
);
892 case GLSL_TYPE_INT64
:
895 result
= new(ctx
) ir_expression(ir_unop_i2i64
, src
);
898 result
= new(ctx
) ir_expression(ir_unop_u2i64
, src
);
901 result
= new(ctx
) ir_expression(ir_unop_b2i64
, src
);
903 case GLSL_TYPE_FLOAT
:
904 result
= new(ctx
) ir_expression(ir_unop_f2i64
, src
);
906 case GLSL_TYPE_DOUBLE
:
907 result
= new(ctx
) ir_expression(ir_unop_d2i64
, src
);
909 case GLSL_TYPE_UINT64
:
910 result
= new(ctx
) ir_expression(ir_unop_u642i64
, src
);
916 assert(result
!= NULL
);
917 assert(result
->type
== desired_type
);
919 /* Try constant folding; it may fold in the conversion we just added. */
920 ir_constant
*const constant
= result
->constant_expression_value();
921 return (constant
!= NULL
) ? (ir_rvalue
*) constant
: (ir_rvalue
*) result
;
926 * Perform automatic type and constant conversion of constructor parameters
928 * This implements the rules in the "Implicit Conversions" rules, not the
929 * "Conversion and Scalar Constructors".
931 * After attempting the implicit conversion, an attempt to convert into a
932 * constant valued expression is also done.
934 * The \c from \c ir_rvalue is converted "in place".
936 * \param from Operand that is being converted
937 * \param to Base type the operand will be converted to
938 * \param state GLSL compiler state
941 * If the attempt to convert into a constant expression succeeds, \c true is
942 * returned. Otherwise \c false is returned.
945 implicitly_convert_component(ir_rvalue
* &from
, const glsl_base_type to
,
946 struct _mesa_glsl_parse_state
*state
)
948 ir_rvalue
*result
= from
;
950 if (to
!= from
->type
->base_type
) {
951 const glsl_type
*desired_type
=
952 glsl_type::get_instance(to
,
953 from
->type
->vector_elements
,
954 from
->type
->matrix_columns
);
956 if (from
->type
->can_implicitly_convert_to(desired_type
, state
)) {
957 /* Even though convert_component() implements the constructor
958 * conversion rules (not the implicit conversion rules), its safe
959 * to use it here because we already checked that the implicit
960 * conversion is legal.
962 result
= convert_component(from
, desired_type
);
966 ir_rvalue
*const constant
= result
->constant_expression_value();
968 if (constant
!= NULL
)
971 if (from
!= result
) {
972 from
->replace_with(result
);
976 return constant
!= NULL
;
981 * Dereference a specific component from a scalar, vector, or matrix
984 dereference_component(ir_rvalue
*src
, unsigned component
)
986 void *ctx
= ralloc_parent(src
);
987 assert(component
< src
->type
->components());
989 /* If the source is a constant, just create a new constant instead of a
990 * dereference of the existing constant.
992 ir_constant
*constant
= src
->as_constant();
994 return new(ctx
) ir_constant(constant
, component
);
996 if (src
->type
->is_scalar()) {
998 } else if (src
->type
->is_vector()) {
999 return new(ctx
) ir_swizzle(src
, component
, 0, 0, 0, 1);
1001 assert(src
->type
->is_matrix());
1003 /* Dereference a row of the matrix, then call this function again to get
1004 * a specific element from that row.
1006 const int c
= component
/ src
->type
->column_type()->vector_elements
;
1007 const int r
= component
% src
->type
->column_type()->vector_elements
;
1008 ir_constant
*const col_index
= new(ctx
) ir_constant(c
);
1009 ir_dereference
*const col
= new(ctx
) ir_dereference_array(src
,
1012 col
->type
= src
->type
->column_type();
1014 return dereference_component(col
, r
);
1017 assert(!"Should not get here.");
1023 process_vec_mat_constructor(exec_list
*instructions
,
1024 const glsl_type
*constructor_type
,
1025 YYLTYPE
*loc
, exec_list
*parameters
,
1026 struct _mesa_glsl_parse_state
*state
)
1030 /* The ARB_shading_language_420pack spec says:
1032 * "If an initializer is a list of initializers enclosed in curly braces,
1033 * the variable being declared must be a vector, a matrix, an array, or a
1036 * int i = { 1 }; // illegal, i is not an aggregate"
1038 if (constructor_type
->vector_elements
<= 1) {
1039 _mesa_glsl_error(loc
, state
, "aggregates can only initialize vectors, "
1040 "matrices, arrays, and structs");
1041 return ir_rvalue::error_value(ctx
);
1044 exec_list actual_parameters
;
1045 const unsigned parameter_count
=
1046 process_parameters(instructions
, &actual_parameters
, parameters
, state
);
1048 if (parameter_count
== 0
1049 || (constructor_type
->is_vector() &&
1050 constructor_type
->vector_elements
!= parameter_count
)
1051 || (constructor_type
->is_matrix() &&
1052 constructor_type
->matrix_columns
!= parameter_count
)) {
1053 _mesa_glsl_error(loc
, state
, "%s constructor must have %u parameters",
1054 constructor_type
->is_vector() ? "vector" : "matrix",
1055 constructor_type
->vector_elements
);
1056 return ir_rvalue::error_value(ctx
);
1059 bool all_parameters_are_constant
= true;
1061 /* Type cast each parameter and, if possible, fold constants. */
1062 foreach_in_list_safe(ir_rvalue
, ir
, &actual_parameters
) {
1063 /* Apply implicit conversions (not the scalar constructor rules, see the
1064 * spec quote above!) and attempt to convert the parameter to a constant
1065 * valued expression. After doing so, track whether or not all the
1066 * parameters to the constructor are trivially constant valued
1069 all_parameters_are_constant
&=
1070 implicitly_convert_component(ir
, constructor_type
->base_type
, state
);
1072 if (constructor_type
->is_matrix()) {
1073 if (ir
->type
!= constructor_type
->column_type()) {
1074 _mesa_glsl_error(loc
, state
, "type error in matrix constructor: "
1075 "expected: %s, found %s",
1076 constructor_type
->column_type()->name
,
1078 return ir_rvalue::error_value(ctx
);
1080 } else if (ir
->type
!= constructor_type
->get_scalar_type()) {
1081 _mesa_glsl_error(loc
, state
, "type error in vector constructor: "
1082 "expected: %s, found %s",
1083 constructor_type
->get_scalar_type()->name
,
1085 return ir_rvalue::error_value(ctx
);
1089 if (all_parameters_are_constant
)
1090 return new(ctx
) ir_constant(constructor_type
, &actual_parameters
);
1092 ir_variable
*var
= new(ctx
) ir_variable(constructor_type
, "vec_mat_ctor",
1094 instructions
->push_tail(var
);
1098 foreach_in_list(ir_rvalue
, rhs
, &actual_parameters
) {
1099 ir_instruction
*assignment
= NULL
;
1101 if (var
->type
->is_matrix()) {
1103 new(ctx
) ir_dereference_array(var
, new(ctx
) ir_constant(i
));
1104 assignment
= new(ctx
) ir_assignment(lhs
, rhs
, NULL
);
1106 /* use writemask rather than index for vector */
1107 assert(var
->type
->is_vector());
1109 ir_dereference
*lhs
= new(ctx
) ir_dereference_variable(var
);
1110 assignment
= new(ctx
) ir_assignment(lhs
, rhs
, NULL
,
1111 (unsigned)(1 << i
));
1114 instructions
->push_tail(assignment
);
1119 return new(ctx
) ir_dereference_variable(var
);
1124 process_array_constructor(exec_list
*instructions
,
1125 const glsl_type
*constructor_type
,
1126 YYLTYPE
*loc
, exec_list
*parameters
,
1127 struct _mesa_glsl_parse_state
*state
)
1130 /* Array constructors come in two forms: sized and unsized. Sized array
1131 * constructors look like 'vec4[2](a, b)', where 'a' and 'b' are vec4
1132 * variables. In this case the number of parameters must exactly match the
1133 * specified size of the array.
1135 * Unsized array constructors look like 'vec4[](a, b)', where 'a' and 'b'
1136 * are vec4 variables. In this case the size of the array being constructed
1137 * is determined by the number of parameters.
1139 * From page 52 (page 58 of the PDF) of the GLSL 1.50 spec:
1141 * "There must be exactly the same number of arguments as the size of
1142 * the array being constructed. If no size is present in the
1143 * constructor, then the array is explicitly sized to the number of
1144 * arguments provided. The arguments are assigned in order, starting at
1145 * element 0, to the elements of the constructed array. Each argument
1146 * must be the same type as the element type of the array, or be a type
1147 * that can be converted to the element type of the array according to
1148 * Section 4.1.10 "Implicit Conversions.""
1150 exec_list actual_parameters
;
1151 const unsigned parameter_count
=
1152 process_parameters(instructions
, &actual_parameters
, parameters
, state
);
1153 bool is_unsized_array
= constructor_type
->is_unsized_array();
1155 if ((parameter_count
== 0) ||
1156 (!is_unsized_array
&& (constructor_type
->length
!= parameter_count
))) {
1157 const unsigned min_param
= is_unsized_array
1158 ? 1 : constructor_type
->length
;
1160 _mesa_glsl_error(loc
, state
, "array constructor must have %s %u "
1162 is_unsized_array
? "at least" : "exactly",
1163 min_param
, (min_param
<= 1) ? "" : "s");
1164 return ir_rvalue::error_value(ctx
);
1167 if (is_unsized_array
) {
1169 glsl_type::get_array_instance(constructor_type
->fields
.array
,
1171 assert(constructor_type
!= NULL
);
1172 assert(constructor_type
->length
== parameter_count
);
1175 bool all_parameters_are_constant
= true;
1176 const glsl_type
*element_type
= constructor_type
->fields
.array
;
1178 /* Type cast each parameter and, if possible, fold constants. */
1179 foreach_in_list_safe(ir_rvalue
, ir
, &actual_parameters
) {
1180 /* Apply implicit conversions (not the scalar constructor rules, see the
1181 * spec quote above!) and attempt to convert the parameter to a constant
1182 * valued expression. After doing so, track whether or not all the
1183 * parameters to the constructor are trivially constant valued
1186 all_parameters_are_constant
&=
1187 implicitly_convert_component(ir
, element_type
->base_type
, state
);
1189 if (constructor_type
->fields
.array
->is_unsized_array()) {
1190 /* As the inner parameters of the constructor are created without
1191 * knowledge of each other we need to check to make sure unsized
1192 * parameters of unsized constructors all end up with the same size.
1194 * e.g we make sure to fail for a constructor like this:
1195 * vec4[][] a = vec4[][](vec4[](vec4(0.0), vec4(1.0)),
1196 * vec4[](vec4(0.0), vec4(1.0), vec4(1.0)),
1197 * vec4[](vec4(0.0), vec4(1.0)));
1199 if (element_type
->is_unsized_array()) {
1200 /* This is the first parameter so just get the type */
1201 element_type
= ir
->type
;
1202 } else if (element_type
!= ir
->type
) {
1203 _mesa_glsl_error(loc
, state
, "type error in array constructor: "
1204 "expected: %s, found %s",
1207 return ir_rvalue::error_value(ctx
);
1209 } else if (ir
->type
!= constructor_type
->fields
.array
) {
1210 _mesa_glsl_error(loc
, state
, "type error in array constructor: "
1211 "expected: %s, found %s",
1212 constructor_type
->fields
.array
->name
,
1214 return ir_rvalue::error_value(ctx
);
1216 element_type
= ir
->type
;
1220 if (constructor_type
->fields
.array
->is_unsized_array()) {
1222 glsl_type::get_array_instance(element_type
,
1224 assert(constructor_type
!= NULL
);
1225 assert(constructor_type
->length
== parameter_count
);
1228 if (all_parameters_are_constant
)
1229 return new(ctx
) ir_constant(constructor_type
, &actual_parameters
);
1231 ir_variable
*var
= new(ctx
) ir_variable(constructor_type
, "array_ctor",
1233 instructions
->push_tail(var
);
1236 foreach_in_list(ir_rvalue
, rhs
, &actual_parameters
) {
1237 ir_rvalue
*lhs
= new(ctx
) ir_dereference_array(var
,
1238 new(ctx
) ir_constant(i
));
1240 ir_instruction
*assignment
= new(ctx
) ir_assignment(lhs
, rhs
, NULL
);
1241 instructions
->push_tail(assignment
);
1246 return new(ctx
) ir_dereference_variable(var
);
1251 * Determine if a list consists of a single scalar r-value
1254 single_scalar_parameter(exec_list
*parameters
)
1256 const ir_rvalue
*const p
= (ir_rvalue
*) parameters
->get_head_raw();
1257 assert(((ir_rvalue
*)p
)->as_rvalue() != NULL
);
1259 return (p
->type
->is_scalar() && p
->next
->is_tail_sentinel());
1264 * Generate inline code for a vector constructor
1266 * The generated constructor code will consist of a temporary variable
1267 * declaration of the same type as the constructor. A sequence of assignments
1268 * from constructor parameters to the temporary will follow.
1271 * An \c ir_dereference_variable of the temprorary generated in the constructor
1275 emit_inline_vector_constructor(const glsl_type
*type
,
1276 exec_list
*instructions
,
1277 exec_list
*parameters
,
1280 assert(!parameters
->is_empty());
1282 ir_variable
*var
= new(ctx
) ir_variable(type
, "vec_ctor", ir_var_temporary
);
1283 instructions
->push_tail(var
);
1285 /* There are three kinds of vector constructors.
1287 * - Construct a vector from a single scalar by replicating that scalar to
1288 * all components of the vector.
1290 * - Construct a vector from at least a matrix. This case should already
1291 * have been taken care of in ast_function_expression::hir by breaking
1292 * down the matrix into a series of column vectors.
1294 * - Construct a vector from an arbirary combination of vectors and
1295 * scalars. The components of the constructor parameters are assigned
1296 * to the vector in order until the vector is full.
1298 const unsigned lhs_components
= type
->components();
1299 if (single_scalar_parameter(parameters
)) {
1300 ir_rvalue
*first_param
= (ir_rvalue
*)parameters
->get_head_raw();
1301 ir_rvalue
*rhs
= new(ctx
) ir_swizzle(first_param
, 0, 0, 0, 0,
1303 ir_dereference_variable
*lhs
= new(ctx
) ir_dereference_variable(var
);
1304 const unsigned mask
= (1U << lhs_components
) - 1;
1306 assert(rhs
->type
== lhs
->type
);
1308 ir_instruction
*inst
= new(ctx
) ir_assignment(lhs
, rhs
, NULL
, mask
);
1309 instructions
->push_tail(inst
);
1311 unsigned base_component
= 0;
1312 unsigned base_lhs_component
= 0;
1313 ir_constant_data data
;
1314 unsigned constant_mask
= 0, constant_components
= 0;
1316 memset(&data
, 0, sizeof(data
));
1318 foreach_in_list(ir_rvalue
, param
, parameters
) {
1319 unsigned rhs_components
= param
->type
->components();
1321 /* Do not try to assign more components to the vector than it has! */
1322 if ((rhs_components
+ base_lhs_component
) > lhs_components
) {
1323 rhs_components
= lhs_components
- base_lhs_component
;
1326 const ir_constant
*const c
= param
->as_constant();
1328 for (unsigned i
= 0; i
< rhs_components
; i
++) {
1329 switch (c
->type
->base_type
) {
1330 case GLSL_TYPE_UINT
:
1331 data
.u
[i
+ base_component
] = c
->get_uint_component(i
);
1334 data
.i
[i
+ base_component
] = c
->get_int_component(i
);
1336 case GLSL_TYPE_FLOAT
:
1337 data
.f
[i
+ base_component
] = c
->get_float_component(i
);
1339 case GLSL_TYPE_DOUBLE
:
1340 data
.d
[i
+ base_component
] = c
->get_double_component(i
);
1342 case GLSL_TYPE_BOOL
:
1343 data
.b
[i
+ base_component
] = c
->get_bool_component(i
);
1345 case GLSL_TYPE_UINT64
:
1346 data
.u64
[i
+ base_component
] = c
->get_uint64_component(i
);
1348 case GLSL_TYPE_INT64
:
1349 data
.i64
[i
+ base_component
] = c
->get_int64_component(i
);
1352 assert(!"Should not get here.");
1357 /* Mask of fields to be written in the assignment. */
1358 constant_mask
|= ((1U << rhs_components
) - 1) << base_lhs_component
;
1359 constant_components
+= rhs_components
;
1361 base_component
+= rhs_components
;
1363 /* Advance the component index by the number of components
1364 * that were just assigned.
1366 base_lhs_component
+= rhs_components
;
1369 if (constant_mask
!= 0) {
1370 ir_dereference
*lhs
= new(ctx
) ir_dereference_variable(var
);
1371 const glsl_type
*rhs_type
=
1372 glsl_type::get_instance(var
->type
->base_type
,
1373 constant_components
,
1375 ir_rvalue
*rhs
= new(ctx
) ir_constant(rhs_type
, &data
);
1377 ir_instruction
*inst
=
1378 new(ctx
) ir_assignment(lhs
, rhs
, NULL
, constant_mask
);
1379 instructions
->push_tail(inst
);
1383 foreach_in_list(ir_rvalue
, param
, parameters
) {
1384 unsigned rhs_components
= param
->type
->components();
1386 /* Do not try to assign more components to the vector than it has! */
1387 if ((rhs_components
+ base_component
) > lhs_components
) {
1388 rhs_components
= lhs_components
- base_component
;
1391 /* If we do not have any components left to copy, break out of the
1392 * loop. This can happen when initializing a vec4 with a mat3 as the
1393 * mat3 would have been broken into a series of column vectors.
1395 if (rhs_components
== 0) {
1399 const ir_constant
*const c
= param
->as_constant();
1401 /* Mask of fields to be written in the assignment. */
1402 const unsigned write_mask
= ((1U << rhs_components
) - 1)
1405 ir_dereference
*lhs
= new(ctx
) ir_dereference_variable(var
);
1407 /* Generate a swizzle so that LHS and RHS sizes match. */
1409 new(ctx
) ir_swizzle(param
, 0, 1, 2, 3, rhs_components
);
1411 ir_instruction
*inst
=
1412 new(ctx
) ir_assignment(lhs
, rhs
, NULL
, write_mask
);
1413 instructions
->push_tail(inst
);
1416 /* Advance the component index by the number of components that were
1419 base_component
+= rhs_components
;
1422 return new(ctx
) ir_dereference_variable(var
);
1427 * Generate assignment of a portion of a vector to a portion of a matrix column
1429 * \param src_base First component of the source to be used in assignment
1430 * \param column Column of destination to be assiged
1431 * \param row_base First component of the destination column to be assigned
1432 * \param count Number of components to be assigned
1435 * \c src_base + \c count must be less than or equal to the number of
1436 * components in the source vector.
1439 assign_to_matrix_column(ir_variable
*var
, unsigned column
, unsigned row_base
,
1440 ir_rvalue
*src
, unsigned src_base
, unsigned count
,
1443 ir_constant
*col_idx
= new(mem_ctx
) ir_constant(column
);
1444 ir_dereference
*column_ref
= new(mem_ctx
) ir_dereference_array(var
,
1447 assert(column_ref
->type
->components() >= (row_base
+ count
));
1448 assert(src
->type
->components() >= (src_base
+ count
));
1450 /* Generate a swizzle that extracts the number of components from the source
1451 * that are to be assigned to the column of the matrix.
1453 if (count
< src
->type
->vector_elements
) {
1454 src
= new(mem_ctx
) ir_swizzle(src
,
1455 src_base
+ 0, src_base
+ 1,
1456 src_base
+ 2, src_base
+ 3,
1460 /* Mask of fields to be written in the assignment. */
1461 const unsigned write_mask
= ((1U << count
) - 1) << row_base
;
1463 return new(mem_ctx
) ir_assignment(column_ref
, src
, NULL
, write_mask
);
1468 * Generate inline code for a matrix constructor
1470 * The generated constructor code will consist of a temporary variable
1471 * declaration of the same type as the constructor. A sequence of assignments
1472 * from constructor parameters to the temporary will follow.
1475 * An \c ir_dereference_variable of the temprorary generated in the constructor
1479 emit_inline_matrix_constructor(const glsl_type
*type
,
1480 exec_list
*instructions
,
1481 exec_list
*parameters
,
1484 assert(!parameters
->is_empty());
1486 ir_variable
*var
= new(ctx
) ir_variable(type
, "mat_ctor", ir_var_temporary
);
1487 instructions
->push_tail(var
);
1489 /* There are three kinds of matrix constructors.
1491 * - Construct a matrix from a single scalar by replicating that scalar to
1492 * along the diagonal of the matrix and setting all other components to
1495 * - Construct a matrix from an arbirary combination of vectors and
1496 * scalars. The components of the constructor parameters are assigned
1497 * to the matrix in column-major order until the matrix is full.
1499 * - Construct a matrix from a single matrix. The source matrix is copied
1500 * to the upper left portion of the constructed matrix, and the remaining
1501 * elements take values from the identity matrix.
1503 ir_rvalue
*const first_param
= (ir_rvalue
*) parameters
->get_head_raw();
1504 if (single_scalar_parameter(parameters
)) {
1505 /* Assign the scalar to the X component of a vec4, and fill the remaining
1506 * components with zero.
1508 glsl_base_type param_base_type
= first_param
->type
->base_type
;
1509 assert(first_param
->type
->is_float() || first_param
->type
->is_double());
1510 ir_variable
*rhs_var
=
1511 new(ctx
) ir_variable(glsl_type::get_instance(param_base_type
, 4, 1),
1514 instructions
->push_tail(rhs_var
);
1516 ir_constant_data zero
;
1517 for (unsigned i
= 0; i
< 4; i
++)
1518 if (first_param
->type
->is_float())
1523 ir_instruction
*inst
=
1524 new(ctx
) ir_assignment(new(ctx
) ir_dereference_variable(rhs_var
),
1525 new(ctx
) ir_constant(rhs_var
->type
, &zero
),
1527 instructions
->push_tail(inst
);
1529 ir_dereference
*const rhs_ref
=
1530 new(ctx
) ir_dereference_variable(rhs_var
);
1532 inst
= new(ctx
) ir_assignment(rhs_ref
, first_param
, NULL
, 0x01);
1533 instructions
->push_tail(inst
);
1535 /* Assign the temporary vector to each column of the destination matrix
1536 * with a swizzle that puts the X component on the diagonal of the
1537 * matrix. In some cases this may mean that the X component does not
1538 * get assigned into the column at all (i.e., when the matrix has more
1539 * columns than rows).
1541 static const unsigned rhs_swiz
[4][4] = {
1548 const unsigned cols_to_init
= MIN2(type
->matrix_columns
,
1549 type
->vector_elements
);
1550 for (unsigned i
= 0; i
< cols_to_init
; i
++) {
1551 ir_constant
*const col_idx
= new(ctx
) ir_constant(i
);
1552 ir_rvalue
*const col_ref
= new(ctx
) ir_dereference_array(var
,
1555 ir_rvalue
*const rhs_ref
= new(ctx
) ir_dereference_variable(rhs_var
);
1556 ir_rvalue
*const rhs
= new(ctx
) ir_swizzle(rhs_ref
, rhs_swiz
[i
],
1557 type
->vector_elements
);
1559 inst
= new(ctx
) ir_assignment(col_ref
, rhs
, NULL
);
1560 instructions
->push_tail(inst
);
1563 for (unsigned i
= cols_to_init
; i
< type
->matrix_columns
; i
++) {
1564 ir_constant
*const col_idx
= new(ctx
) ir_constant(i
);
1565 ir_rvalue
*const col_ref
= new(ctx
) ir_dereference_array(var
,
1568 ir_rvalue
*const rhs_ref
= new(ctx
) ir_dereference_variable(rhs_var
);
1569 ir_rvalue
*const rhs
= new(ctx
) ir_swizzle(rhs_ref
, 1, 1, 1, 1,
1570 type
->vector_elements
);
1572 inst
= new(ctx
) ir_assignment(col_ref
, rhs
, NULL
);
1573 instructions
->push_tail(inst
);
1575 } else if (first_param
->type
->is_matrix()) {
1576 /* From page 50 (56 of the PDF) of the GLSL 1.50 spec:
1578 * "If a matrix is constructed from a matrix, then each component
1579 * (column i, row j) in the result that has a corresponding
1580 * component (column i, row j) in the argument will be initialized
1581 * from there. All other components will be initialized to the
1582 * identity matrix. If a matrix argument is given to a matrix
1583 * constructor, it is an error to have any other arguments."
1585 assert(first_param
->next
->is_tail_sentinel());
1586 ir_rvalue
*const src_matrix
= first_param
;
1588 /* If the source matrix is smaller, pre-initialize the relavent parts of
1589 * the destination matrix to the identity matrix.
1591 if ((src_matrix
->type
->matrix_columns
< var
->type
->matrix_columns
) ||
1592 (src_matrix
->type
->vector_elements
< var
->type
->vector_elements
)) {
1594 /* If the source matrix has fewer rows, every column of the
1595 * destination must be initialized. Otherwise only the columns in
1596 * the destination that do not exist in the source must be
1600 (src_matrix
->type
->vector_elements
< var
->type
->vector_elements
)
1601 ? 0 : src_matrix
->type
->matrix_columns
;
1603 const glsl_type
*const col_type
= var
->type
->column_type();
1604 for (/* empty */; col
< var
->type
->matrix_columns
; col
++) {
1605 ir_constant_data ident
;
1607 if (!col_type
->is_double()) {
1612 ident
.f
[col
] = 1.0f
;
1621 ir_rvalue
*const rhs
= new(ctx
) ir_constant(col_type
, &ident
);
1623 ir_rvalue
*const lhs
=
1624 new(ctx
) ir_dereference_array(var
, new(ctx
) ir_constant(col
));
1626 ir_instruction
*inst
= new(ctx
) ir_assignment(lhs
, rhs
, NULL
);
1627 instructions
->push_tail(inst
);
1631 /* Assign columns from the source matrix to the destination matrix.
1633 * Since the parameter will be used in the RHS of multiple assignments,
1634 * generate a temporary and copy the paramter there.
1636 ir_variable
*const rhs_var
=
1637 new(ctx
) ir_variable(first_param
->type
, "mat_ctor_mat",
1639 instructions
->push_tail(rhs_var
);
1641 ir_dereference
*const rhs_var_ref
=
1642 new(ctx
) ir_dereference_variable(rhs_var
);
1643 ir_instruction
*const inst
=
1644 new(ctx
) ir_assignment(rhs_var_ref
, first_param
, NULL
);
1645 instructions
->push_tail(inst
);
1647 const unsigned last_row
= MIN2(src_matrix
->type
->vector_elements
,
1648 var
->type
->vector_elements
);
1649 const unsigned last_col
= MIN2(src_matrix
->type
->matrix_columns
,
1650 var
->type
->matrix_columns
);
1652 unsigned swiz
[4] = { 0, 0, 0, 0 };
1653 for (unsigned i
= 1; i
< last_row
; i
++)
1656 const unsigned write_mask
= (1U << last_row
) - 1;
1658 for (unsigned i
= 0; i
< last_col
; i
++) {
1659 ir_dereference
*const lhs
=
1660 new(ctx
) ir_dereference_array(var
, new(ctx
) ir_constant(i
));
1661 ir_rvalue
*const rhs_col
=
1662 new(ctx
) ir_dereference_array(rhs_var
, new(ctx
) ir_constant(i
));
1664 /* If one matrix has columns that are smaller than the columns of the
1665 * other matrix, wrap the column access of the larger with a swizzle
1666 * so that the LHS and RHS of the assignment have the same size (and
1667 * therefore have the same type).
1669 * It would be perfectly valid to unconditionally generate the
1670 * swizzles, this this will typically result in a more compact IR
1674 if (lhs
->type
->vector_elements
!= rhs_col
->type
->vector_elements
) {
1675 rhs
= new(ctx
) ir_swizzle(rhs_col
, swiz
, last_row
);
1680 ir_instruction
*inst
=
1681 new(ctx
) ir_assignment(lhs
, rhs
, NULL
, write_mask
);
1682 instructions
->push_tail(inst
);
1685 const unsigned cols
= type
->matrix_columns
;
1686 const unsigned rows
= type
->vector_elements
;
1687 unsigned remaining_slots
= rows
* cols
;
1688 unsigned col_idx
= 0;
1689 unsigned row_idx
= 0;
1691 foreach_in_list(ir_rvalue
, rhs
, parameters
) {
1692 unsigned rhs_components
= rhs
->type
->components();
1693 unsigned rhs_base
= 0;
1695 if (remaining_slots
== 0)
1698 /* Since the parameter might be used in the RHS of two assignments,
1699 * generate a temporary and copy the paramter there.
1701 ir_variable
*rhs_var
=
1702 new(ctx
) ir_variable(rhs
->type
, "mat_ctor_vec", ir_var_temporary
);
1703 instructions
->push_tail(rhs_var
);
1705 ir_dereference
*rhs_var_ref
=
1706 new(ctx
) ir_dereference_variable(rhs_var
);
1707 ir_instruction
*inst
= new(ctx
) ir_assignment(rhs_var_ref
, rhs
, NULL
);
1708 instructions
->push_tail(inst
);
1711 /* Assign the current parameter to as many components of the matrix
1714 * NOTE: A single vector parameter can span two matrix columns. A
1715 * single vec4, for example, can completely fill a mat2.
1717 unsigned count
= MIN2(rows
- row_idx
,
1718 rhs_components
- rhs_base
);
1720 rhs_var_ref
= new(ctx
) ir_dereference_variable(rhs_var
);
1721 ir_instruction
*inst
= assign_to_matrix_column(var
, col_idx
,
1726 instructions
->push_tail(inst
);
1729 remaining_slots
-= count
;
1731 /* Sometimes, there is still data left in the parameters and
1732 * components left to be set in the destination but in other
1735 if (row_idx
>= rows
) {
1739 } while(remaining_slots
> 0 && rhs_base
< rhs_components
);
1743 return new(ctx
) ir_dereference_variable(var
);
1748 emit_inline_record_constructor(const glsl_type
*type
,
1749 exec_list
*instructions
,
1750 exec_list
*parameters
,
1753 ir_variable
*const var
=
1754 new(mem_ctx
) ir_variable(type
, "record_ctor", ir_var_temporary
);
1755 ir_dereference_variable
*const d
=
1756 new(mem_ctx
) ir_dereference_variable(var
);
1758 instructions
->push_tail(var
);
1760 exec_node
*node
= parameters
->get_head_raw();
1761 for (unsigned i
= 0; i
< type
->length
; i
++) {
1762 assert(!node
->is_tail_sentinel());
1764 ir_dereference
*const lhs
=
1765 new(mem_ctx
) ir_dereference_record(d
->clone(mem_ctx
, NULL
),
1766 type
->fields
.structure
[i
].name
);
1768 ir_rvalue
*const rhs
= ((ir_instruction
*) node
)->as_rvalue();
1769 assert(rhs
!= NULL
);
1771 ir_instruction
*const assign
=
1772 new(mem_ctx
) ir_assignment(lhs
, rhs
, NULL
);
1774 instructions
->push_tail(assign
);
1783 process_record_constructor(exec_list
*instructions
,
1784 const glsl_type
*constructor_type
,
1785 YYLTYPE
*loc
, exec_list
*parameters
,
1786 struct _mesa_glsl_parse_state
*state
)
1789 /* From page 32 (page 38 of the PDF) of the GLSL 1.20 spec:
1791 * "The arguments to the constructor will be used to set the structure's
1792 * fields, in order, using one argument per field. Each argument must
1793 * be the same type as the field it sets, or be a type that can be
1794 * converted to the field's type according to Section 4.1.10 “Implicit
1797 * From page 35 (page 41 of the PDF) of the GLSL 4.20 spec:
1799 * "In all cases, the innermost initializer (i.e., not a list of
1800 * initializers enclosed in curly braces) applied to an object must
1801 * have the same type as the object being initialized or be a type that
1802 * can be converted to the object's type according to section 4.1.10
1803 * "Implicit Conversions". In the latter case, an implicit conversion
1804 * will be done on the initializer before the assignment is done."
1806 exec_list actual_parameters
;
1808 const unsigned parameter_count
=
1809 process_parameters(instructions
, &actual_parameters
, parameters
,
1812 if (parameter_count
!= constructor_type
->length
) {
1813 _mesa_glsl_error(loc
, state
,
1814 "%s parameters in constructor for `%s'",
1815 parameter_count
> constructor_type
->length
1816 ? "too many": "insufficient",
1817 constructor_type
->name
);
1818 return ir_rvalue::error_value(ctx
);
1821 bool all_parameters_are_constant
= true;
1824 /* Type cast each parameter and, if possible, fold constants. */
1825 foreach_in_list_safe(ir_rvalue
, ir
, &actual_parameters
) {
1827 const glsl_struct_field
*struct_field
=
1828 &constructor_type
->fields
.structure
[i
];
1830 /* Apply implicit conversions (not the scalar constructor rules, see the
1831 * spec quote above!) and attempt to convert the parameter to a constant
1832 * valued expression. After doing so, track whether or not all the
1833 * parameters to the constructor are trivially constant valued
1836 all_parameters_are_constant
&=
1837 implicitly_convert_component(ir
, struct_field
->type
->base_type
,
1840 if (ir
->type
!= struct_field
->type
) {
1841 _mesa_glsl_error(loc
, state
,
1842 "parameter type mismatch in constructor for `%s.%s' "
1844 constructor_type
->name
,
1847 struct_field
->type
->name
);
1848 return ir_rvalue::error_value(ctx
);
1854 if (all_parameters_are_constant
) {
1855 return new(ctx
) ir_constant(constructor_type
, &actual_parameters
);
1857 return emit_inline_record_constructor(constructor_type
, instructions
,
1858 &actual_parameters
, state
);
1863 ast_function_expression::handle_method(exec_list
*instructions
,
1864 struct _mesa_glsl_parse_state
*state
)
1866 const ast_expression
*field
= subexpressions
[0];
1870 /* Handle "method calls" in GLSL 1.20 - namely, array.length() */
1871 YYLTYPE loc
= get_location();
1872 state
->check_version(120, 300, &loc
, "methods not supported");
1875 method
= field
->primary_expression
.identifier
;
1877 /* This would prevent to raise "uninitialized variable" warnings when
1878 * calling array.length.
1880 field
->subexpressions
[0]->set_is_lhs(true);
1881 op
= field
->subexpressions
[0]->hir(instructions
, state
);
1882 if (strcmp(method
, "length") == 0) {
1883 if (!this->expressions
.is_empty()) {
1884 _mesa_glsl_error(&loc
, state
, "length method takes no arguments");
1888 if (op
->type
->is_array()) {
1889 if (op
->type
->is_unsized_array()) {
1890 if (!state
->has_shader_storage_buffer_objects()) {
1891 _mesa_glsl_error(&loc
, state
,
1892 "length called on unsized array"
1893 " only available with"
1894 " ARB_shader_storage_buffer_object");
1896 /* Calculate length of an unsized array in run-time */
1897 result
= new(ctx
) ir_expression(ir_unop_ssbo_unsized_array_length
,
1900 result
= new(ctx
) ir_constant(op
->type
->array_size());
1902 } else if (op
->type
->is_vector()) {
1903 if (state
->has_420pack()) {
1904 /* .length() returns int. */
1905 result
= new(ctx
) ir_constant((int) op
->type
->vector_elements
);
1907 _mesa_glsl_error(&loc
, state
, "length method on matrix only"
1908 " available with ARB_shading_language_420pack");
1911 } else if (op
->type
->is_matrix()) {
1912 if (state
->has_420pack()) {
1913 /* .length() returns int. */
1914 result
= new(ctx
) ir_constant((int) op
->type
->matrix_columns
);
1916 _mesa_glsl_error(&loc
, state
, "length method on matrix only"
1917 " available with ARB_shading_language_420pack");
1921 _mesa_glsl_error(&loc
, state
, "length called on scalar.");
1925 _mesa_glsl_error(&loc
, state
, "unknown method: `%s'", method
);
1930 return ir_rvalue::error_value(ctx
);
1934 ast_function_expression::hir(exec_list
*instructions
,
1935 struct _mesa_glsl_parse_state
*state
)
1938 /* There are three sorts of function calls.
1940 * 1. constructors - The first subexpression is an ast_type_specifier.
1941 * 2. methods - Only the .length() method of array types.
1942 * 3. functions - Calls to regular old functions.
1945 if (is_constructor()) {
1946 const ast_type_specifier
*type
=
1947 (ast_type_specifier
*) subexpressions
[0];
1948 YYLTYPE loc
= type
->get_location();
1951 const glsl_type
*const constructor_type
= type
->glsl_type(& name
, state
);
1953 /* constructor_type can be NULL if a variable with the same name as the
1954 * structure has come into scope.
1956 if (constructor_type
== NULL
) {
1957 _mesa_glsl_error(& loc
, state
, "unknown type `%s' (structure name "
1958 "may be shadowed by a variable with the same name)",
1960 return ir_rvalue::error_value(ctx
);
1964 /* Constructors for opaque types are illegal.
1966 if (constructor_type
->contains_opaque()) {
1967 _mesa_glsl_error(& loc
, state
, "cannot construct opaque type `%s'",
1968 constructor_type
->name
);
1969 return ir_rvalue::error_value(ctx
);
1972 if (constructor_type
->is_subroutine()) {
1973 _mesa_glsl_error(& loc
, state
,
1974 "subroutine name cannot be a constructor `%s'",
1975 constructor_type
->name
);
1976 return ir_rvalue::error_value(ctx
);
1979 if (constructor_type
->is_array()) {
1980 if (!state
->check_version(120, 300, &loc
,
1981 "array constructors forbidden")) {
1982 return ir_rvalue::error_value(ctx
);
1985 return process_array_constructor(instructions
, constructor_type
,
1986 & loc
, &this->expressions
, state
);
1990 /* There are two kinds of constructor calls. Constructors for arrays and
1991 * structures must have the exact number of arguments with matching types
1992 * in the correct order. These constructors follow essentially the same
1993 * type matching rules as functions.
1995 * Constructors for built-in language types, such as mat4 and vec2, are
1996 * free form. The only requirements are that the parameters must provide
1997 * enough values of the correct scalar type and that no arguments are
1998 * given past the last used argument.
2000 * When using the C-style initializer syntax from GLSL 4.20, constructors
2001 * must have the exact number of arguments with matching types in the
2004 if (constructor_type
->is_record()) {
2005 return process_record_constructor(instructions
, constructor_type
,
2006 &loc
, &this->expressions
,
2010 if (!constructor_type
->is_numeric() && !constructor_type
->is_boolean())
2011 return ir_rvalue::error_value(ctx
);
2013 /* Total number of components of the type being constructed. */
2014 const unsigned type_components
= constructor_type
->components();
2016 /* Number of components from parameters that have actually been
2017 * consumed. This is used to perform several kinds of error checking.
2019 unsigned components_used
= 0;
2021 unsigned matrix_parameters
= 0;
2022 unsigned nonmatrix_parameters
= 0;
2023 exec_list actual_parameters
;
2025 foreach_list_typed(ast_node
, ast
, link
, &this->expressions
) {
2026 ir_rvalue
*result
= ast
->hir(instructions
, state
);
2028 /* From page 50 (page 56 of the PDF) of the GLSL 1.50 spec:
2030 * "It is an error to provide extra arguments beyond this
2031 * last used argument."
2033 if (components_used
>= type_components
) {
2034 _mesa_glsl_error(& loc
, state
, "too many parameters to `%s' "
2036 constructor_type
->name
);
2037 return ir_rvalue::error_value(ctx
);
2040 if (!result
->type
->is_numeric() && !result
->type
->is_boolean()) {
2041 _mesa_glsl_error(& loc
, state
, "cannot construct `%s' from a "
2042 "non-numeric data type",
2043 constructor_type
->name
);
2044 return ir_rvalue::error_value(ctx
);
2047 /* Count the number of matrix and nonmatrix parameters. This
2048 * is used below to enforce some of the constructor rules.
2050 if (result
->type
->is_matrix())
2051 matrix_parameters
++;
2053 nonmatrix_parameters
++;
2055 actual_parameters
.push_tail(result
);
2056 components_used
+= result
->type
->components();
2059 /* From page 28 (page 34 of the PDF) of the GLSL 1.10 spec:
2061 * "It is an error to construct matrices from other matrices. This
2062 * is reserved for future use."
2064 if (matrix_parameters
> 0
2065 && constructor_type
->is_matrix()
2066 && !state
->check_version(120, 100, &loc
,
2067 "cannot construct `%s' from a matrix",
2068 constructor_type
->name
)) {
2069 return ir_rvalue::error_value(ctx
);
2072 /* From page 50 (page 56 of the PDF) of the GLSL 1.50 spec:
2074 * "If a matrix argument is given to a matrix constructor, it is
2075 * an error to have any other arguments."
2077 if ((matrix_parameters
> 0)
2078 && ((matrix_parameters
+ nonmatrix_parameters
) > 1)
2079 && constructor_type
->is_matrix()) {
2080 _mesa_glsl_error(& loc
, state
, "for matrix `%s' constructor, "
2081 "matrix must be only parameter",
2082 constructor_type
->name
);
2083 return ir_rvalue::error_value(ctx
);
2086 /* From page 28 (page 34 of the PDF) of the GLSL 1.10 spec:
2088 * "In these cases, there must be enough components provided in the
2089 * arguments to provide an initializer for every component in the
2090 * constructed value."
2092 if (components_used
< type_components
&& components_used
!= 1
2093 && matrix_parameters
== 0) {
2094 _mesa_glsl_error(& loc
, state
, "too few components to construct "
2096 constructor_type
->name
);
2097 return ir_rvalue::error_value(ctx
);
2100 /* Matrices can never be consumed as is by any constructor but matrix
2101 * constructors. If the constructor type is not matrix, always break the
2102 * matrix up into a series of column vectors.
2104 if (!constructor_type
->is_matrix()) {
2105 foreach_in_list_safe(ir_rvalue
, matrix
, &actual_parameters
) {
2106 if (!matrix
->type
->is_matrix())
2109 /* Create a temporary containing the matrix. */
2110 ir_variable
*var
= new(ctx
) ir_variable(matrix
->type
, "matrix_tmp",
2112 instructions
->push_tail(var
);
2113 instructions
->push_tail(
2114 new(ctx
) ir_assignment(new(ctx
) ir_dereference_variable(var
),
2116 var
->constant_value
= matrix
->constant_expression_value();
2118 /* Replace the matrix with dereferences of its columns. */
2119 for (int i
= 0; i
< matrix
->type
->matrix_columns
; i
++) {
2120 matrix
->insert_before(
2121 new (ctx
) ir_dereference_array(var
,
2122 new(ctx
) ir_constant(i
)));
2128 bool all_parameters_are_constant
= true;
2130 /* Type cast each parameter and, if possible, fold constants.*/
2131 foreach_in_list_safe(ir_rvalue
, ir
, &actual_parameters
) {
2132 const glsl_type
*desired_type
=
2133 glsl_type::get_instance(constructor_type
->base_type
,
2134 ir
->type
->vector_elements
,
2135 ir
->type
->matrix_columns
);
2136 ir_rvalue
*result
= convert_component(ir
, desired_type
);
2138 /* Attempt to convert the parameter to a constant valued expression.
2139 * After doing so, track whether or not all the parameters to the
2140 * constructor are trivially constant valued expressions.
2142 ir_rvalue
*const constant
= result
->constant_expression_value();
2144 if (constant
!= NULL
)
2147 all_parameters_are_constant
= false;
2150 ir
->replace_with(result
);
2154 /* If all of the parameters are trivially constant, create a
2155 * constant representing the complete collection of parameters.
2157 if (all_parameters_are_constant
) {
2158 return new(ctx
) ir_constant(constructor_type
, &actual_parameters
);
2159 } else if (constructor_type
->is_scalar()) {
2160 return dereference_component((ir_rvalue
*)
2161 actual_parameters
.get_head_raw(),
2163 } else if (constructor_type
->is_vector()) {
2164 return emit_inline_vector_constructor(constructor_type
,
2169 assert(constructor_type
->is_matrix());
2170 return emit_inline_matrix_constructor(constructor_type
,
2175 } else if (subexpressions
[0]->oper
== ast_field_selection
) {
2176 return handle_method(instructions
, state
);
2178 const ast_expression
*id
= subexpressions
[0];
2179 const char *func_name
= NULL
;
2180 YYLTYPE loc
= get_location();
2181 exec_list actual_parameters
;
2182 ir_variable
*sub_var
= NULL
;
2183 ir_rvalue
*array_idx
= NULL
;
2185 process_parameters(instructions
, &actual_parameters
, &this->expressions
,
2188 if (id
->oper
== ast_array_index
) {
2189 array_idx
= generate_array_index(ctx
, instructions
, state
, loc
,
2190 id
->subexpressions
[0],
2191 id
->subexpressions
[1], &func_name
,
2192 &actual_parameters
);
2193 } else if (id
->oper
== ast_identifier
) {
2194 func_name
= id
->primary_expression
.identifier
;
2196 _mesa_glsl_error(&loc
, state
, "function name is not an identifier");
2199 /* an error was emitted earlier */
2201 return ir_rvalue::error_value(ctx
);
2203 ir_function_signature
*sig
=
2204 match_function_by_name(func_name
, &actual_parameters
, state
);
2206 ir_rvalue
*value
= NULL
;
2208 sig
= match_subroutine_by_name(func_name
, &actual_parameters
,
2213 no_matching_function_error(func_name
, &loc
,
2214 &actual_parameters
, state
);
2215 value
= ir_rvalue::error_value(ctx
);
2216 } else if (!verify_parameter_modes(state
, sig
,
2218 this->expressions
)) {
2219 /* an error has already been emitted */
2220 value
= ir_rvalue::error_value(ctx
);
2221 } else if (sig
->is_builtin() && strcmp(func_name
, "ftransform") == 0) {
2222 /* ftransform refers to global variables, and we don't have any code
2223 * for remapping the variable references in the built-in shader.
2226 state
->symbols
->get_variable("gl_ModelViewProjectionMatrix");
2227 ir_variable
*vtx
= state
->symbols
->get_variable("gl_Vertex");
2228 value
= new(ctx
) ir_expression(ir_binop_mul
, glsl_type::vec4_type
,
2229 new(ctx
) ir_dereference_variable(mvp
),
2230 new(ctx
) ir_dereference_variable(vtx
));
2232 if (state
->stage
== MESA_SHADER_TESS_CTRL
&&
2233 sig
->is_builtin() && strcmp(func_name
, "barrier") == 0) {
2234 if (state
->current_function
== NULL
||
2235 strcmp(state
->current_function
->function_name(), "main") != 0) {
2236 _mesa_glsl_error(&loc
, state
,
2237 "barrier() may only be used in main()");
2240 if (state
->found_return
) {
2241 _mesa_glsl_error(&loc
, state
,
2242 "barrier() may not be used after return");
2245 if (instructions
!= &state
->current_function
->body
) {
2246 _mesa_glsl_error(&loc
, state
,
2247 "barrier() may not be used in control flow");
2251 value
= generate_call(instructions
, sig
, &actual_parameters
, sub_var
,
2252 array_idx
, state
, sig
->is_builtin());
2254 ir_variable
*const tmp
= new(ctx
) ir_variable(glsl_type::void_type
,
2257 instructions
->push_tail(tmp
);
2258 value
= new(ctx
) ir_dereference_variable(tmp
);
2265 unreachable("not reached");
2269 ast_function_expression::has_sequence_subexpression() const
2271 foreach_list_typed(const ast_node
, ast
, link
, &this->expressions
) {
2272 if (ast
->has_sequence_subexpression())
2280 ast_aggregate_initializer::hir(exec_list
*instructions
,
2281 struct _mesa_glsl_parse_state
*state
)
2284 YYLTYPE loc
= this->get_location();
2286 if (!this->constructor_type
) {
2287 _mesa_glsl_error(&loc
, state
, "type of C-style initializer unknown");
2288 return ir_rvalue::error_value(ctx
);
2290 const glsl_type
*const constructor_type
= this->constructor_type
;
2292 if (!state
->has_420pack()) {
2293 _mesa_glsl_error(&loc
, state
, "C-style initialization requires the "
2294 "GL_ARB_shading_language_420pack extension");
2295 return ir_rvalue::error_value(ctx
);
2298 if (constructor_type
->is_array()) {
2299 return process_array_constructor(instructions
, constructor_type
, &loc
,
2300 &this->expressions
, state
);
2303 if (constructor_type
->is_record()) {
2304 return process_record_constructor(instructions
, constructor_type
, &loc
,
2305 &this->expressions
, state
);
2308 return process_vec_mat_constructor(instructions
, constructor_type
, &loc
,
2309 &this->expressions
, state
);