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.
25 * \file ir_constant_expression.cpp
26 * Evaluate and process constant valued expressions
28 * In GLSL, constant valued expressions are used in several places. These
29 * must be processed and evaluated very early in the compilation process.
32 * * Initializers for uniforms
33 * * Initializers for \c const variables
37 #include "main/core.h" /* for MAX2, MIN2, CLAMP */
39 #include "ir_visitor.h"
40 #include "glsl_types.h"
41 #include "program/hash_table.h"
44 dot(ir_constant
*op0
, ir_constant
*op1
)
46 assert(op0
->type
->is_float() && op1
->type
->is_float());
49 for (unsigned c
= 0; c
< op0
->type
->components(); c
++)
50 result
+= op0
->value
.f
[c
] * op1
->value
.f
[c
];
55 /* This method is the only one supported by gcc. Unions in particular
56 * are iffy, and read-through-converted-pointer is killed by strict
57 * aliasing. OTOH, the compiler sees through the memcpy, so the
58 * resulting asm is reasonable.
61 bitcast_u2f(unsigned int u
)
63 assert(sizeof(float) == sizeof(unsigned int));
65 memcpy(&f
, &u
, sizeof(f
));
72 assert(sizeof(float) == sizeof(unsigned int));
74 memcpy(&u
, &f
, sizeof(f
));
79 * Evaluate one component of a floating-point 4x8 unpacking function.
82 (*pack_1x8_func_t
)(float);
85 * Evaluate one component of a floating-point 2x16 unpacking function.
88 (*pack_1x16_func_t
)(float);
91 * Evaluate one component of a floating-point 4x8 unpacking function.
94 (*unpack_1x8_func_t
)(uint8_t);
97 * Evaluate one component of a floating-point 2x16 unpacking function.
100 (*unpack_1x16_func_t
)(uint16_t);
103 * Evaluate a 2x16 floating-point packing function.
106 pack_2x16(pack_1x16_func_t pack_1x16
,
109 /* From section 8.4 of the GLSL ES 3.00 spec:
113 * The first component of the vector will be written to the least
114 * significant bits of the output; the last component will be written to
115 * the most significant bits.
117 * The specifications for the other packing functions contain similar
121 u
|= ((uint32_t) pack_1x16(x
) << 0);
122 u
|= ((uint32_t) pack_1x16(y
) << 16);
127 * Evaluate a 4x8 floating-point packing function.
130 pack_4x8(pack_1x8_func_t pack_1x8
,
131 float x
, float y
, float z
, float w
)
133 /* From section 8.4 of the GLSL 4.30 spec:
137 * The first component of the vector will be written to the least
138 * significant bits of the output; the last component will be written to
139 * the most significant bits.
141 * The specifications for the other packing functions contain similar
145 u
|= ((uint32_t) pack_1x8(x
) << 0);
146 u
|= ((uint32_t) pack_1x8(y
) << 8);
147 u
|= ((uint32_t) pack_1x8(z
) << 16);
148 u
|= ((uint32_t) pack_1x8(w
) << 24);
153 * Evaluate a 2x16 floating-point unpacking function.
156 unpack_2x16(unpack_1x16_func_t unpack_1x16
,
160 /* From section 8.4 of the GLSL ES 3.00 spec:
164 * The first component of the returned vector will be extracted from
165 * the least significant bits of the input; the last component will be
166 * extracted from the most significant bits.
168 * The specifications for the other unpacking functions contain similar
171 *x
= unpack_1x16((uint16_t) (u
& 0xffff));
172 *y
= unpack_1x16((uint16_t) (u
>> 16));
176 * Evaluate a 4x8 floating-point unpacking function.
179 unpack_4x8(unpack_1x8_func_t unpack_1x8
, uint32_t u
,
180 float *x
, float *y
, float *z
, float *w
)
182 /* From section 8.4 of the GLSL 4.30 spec:
186 * The first component of the returned vector will be extracted from
187 * the least significant bits of the input; the last component will be
188 * extracted from the most significant bits.
190 * The specifications for the other unpacking functions contain similar
193 *x
= unpack_1x8((uint8_t) (u
& 0xff));
194 *y
= unpack_1x8((uint8_t) (u
>> 8));
195 *z
= unpack_1x8((uint8_t) (u
>> 16));
196 *w
= unpack_1x8((uint8_t) (u
>> 24));
200 * Evaluate one component of packSnorm4x8.
203 pack_snorm_1x8(float x
)
205 /* From section 8.4 of the GLSL 4.30 spec:
209 * The conversion for component c of v to fixed point is done as
212 * packSnorm4x8: round(clamp(c, -1, +1) * 127.0)
214 * We must first cast the float to an int, because casting a negative
215 * float to a uint is undefined.
217 return (uint8_t) (int8_t)
218 _mesa_round_to_even(CLAMP(x
, -1.0f
, +1.0f
) * 127.0f
);
222 * Evaluate one component of packSnorm2x16.
225 pack_snorm_1x16(float x
)
227 /* From section 8.4 of the GLSL ES 3.00 spec:
231 * The conversion for component c of v to fixed point is done as
234 * packSnorm2x16: round(clamp(c, -1, +1) * 32767.0)
236 * We must first cast the float to an int, because casting a negative
237 * float to a uint is undefined.
239 return (uint16_t) (int16_t)
240 _mesa_round_to_even(CLAMP(x
, -1.0f
, +1.0f
) * 32767.0f
);
244 * Evaluate one component of unpackSnorm4x8.
247 unpack_snorm_1x8(uint8_t u
)
249 /* From section 8.4 of the GLSL 4.30 spec:
253 * The conversion for unpacked fixed-point value f to floating point is
256 * unpackSnorm4x8: clamp(f / 127.0, -1, +1)
258 return CLAMP((int8_t) u
/ 127.0f
, -1.0f
, +1.0f
);
262 * Evaluate one component of unpackSnorm2x16.
265 unpack_snorm_1x16(uint16_t u
)
267 /* From section 8.4 of the GLSL ES 3.00 spec:
271 * The conversion for unpacked fixed-point value f to floating point is
274 * unpackSnorm2x16: clamp(f / 32767.0, -1, +1)
276 return CLAMP((int16_t) u
/ 32767.0f
, -1.0f
, +1.0f
);
280 * Evaluate one component packUnorm4x8.
283 pack_unorm_1x8(float x
)
285 /* From section 8.4 of the GLSL 4.30 spec:
289 * The conversion for component c of v to fixed point is done as
292 * packUnorm4x8: round(clamp(c, 0, +1) * 255.0)
294 return (uint8_t) _mesa_round_to_even(CLAMP(x
, 0.0f
, 1.0f
) * 255.0f
);
298 * Evaluate one component packUnorm2x16.
301 pack_unorm_1x16(float x
)
303 /* From section 8.4 of the GLSL ES 3.00 spec:
307 * The conversion for component c of v to fixed point is done as
310 * packUnorm2x16: round(clamp(c, 0, +1) * 65535.0)
312 return (uint16_t) _mesa_round_to_even(CLAMP(x
, 0.0f
, 1.0f
) * 65535.0f
);
316 * Evaluate one component of unpackUnorm4x8.
319 unpack_unorm_1x8(uint8_t u
)
321 /* From section 8.4 of the GLSL 4.30 spec:
325 * The conversion for unpacked fixed-point value f to floating point is
328 * unpackUnorm4x8: f / 255.0
330 return (float) u
/ 255.0f
;
334 * Evaluate one component of unpackUnorm2x16.
337 unpack_unorm_1x16(uint16_t u
)
339 /* From section 8.4 of the GLSL ES 3.00 spec:
343 * The conversion for unpacked fixed-point value f to floating point is
346 * unpackUnorm2x16: f / 65535.0
348 return (float) u
/ 65535.0f
;
352 * Evaluate one component of packHalf2x16.
355 pack_half_1x16(float x
)
357 return _mesa_float_to_half(x
);
361 * Evaluate one component of unpackHalf2x16.
364 unpack_half_1x16(uint16_t u
)
366 return _mesa_half_to_float(u
);
370 ir_rvalue::constant_expression_value(struct hash_table
*variable_context
)
372 assert(this->type
->is_error());
377 ir_expression::constant_expression_value(struct hash_table
*variable_context
)
379 if (this->type
->is_error())
382 ir_constant
*op
[Elements(this->operands
)] = { NULL
, };
383 ir_constant_data data
;
385 memset(&data
, 0, sizeof(data
));
387 for (unsigned operand
= 0; operand
< this->get_num_operands(); operand
++) {
388 op
[operand
] = this->operands
[operand
]->constant_expression_value(variable_context
);
394 assert(op
[0]->type
->base_type
== op
[1]->type
->base_type
||
395 this->operation
== ir_binop_lshift
||
396 this->operation
== ir_binop_rshift
);
398 bool op0_scalar
= op
[0]->type
->is_scalar();
399 bool op1_scalar
= op
[1] != NULL
&& op
[1]->type
->is_scalar();
401 /* When iterating over a vector or matrix's components, we want to increase
402 * the loop counter. However, for scalars, we want to stay at 0.
404 unsigned c0_inc
= op0_scalar
? 0 : 1;
405 unsigned c1_inc
= op1_scalar
? 0 : 1;
407 if (op1_scalar
|| !op
[1]) {
408 components
= op
[0]->type
->components();
410 components
= op
[1]->type
->components();
413 void *ctx
= ralloc_parent(this);
415 /* Handle array operations here, rather than below. */
416 if (op
[0]->type
->is_array()) {
417 assert(op
[1] != NULL
&& op
[1]->type
->is_array());
418 switch (this->operation
) {
419 case ir_binop_all_equal
:
420 return new(ctx
) ir_constant(op
[0]->has_value(op
[1]));
421 case ir_binop_any_nequal
:
422 return new(ctx
) ir_constant(!op
[0]->has_value(op
[1]));
429 switch (this->operation
) {
430 case ir_unop_bit_not
:
431 switch (op
[0]->type
->base_type
) {
433 for (unsigned c
= 0; c
< components
; c
++)
434 data
.i
[c
] = ~ op
[0]->value
.i
[c
];
437 for (unsigned c
= 0; c
< components
; c
++)
438 data
.u
[c
] = ~ op
[0]->value
.u
[c
];
445 case ir_unop_logic_not
:
446 assert(op
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
447 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++)
448 data
.b
[c
] = !op
[0]->value
.b
[c
];
452 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
453 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
454 data
.i
[c
] = (int) op
[0]->value
.f
[c
];
458 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
459 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
460 data
.i
[c
] = (unsigned) op
[0]->value
.f
[c
];
464 assert(op
[0]->type
->base_type
== GLSL_TYPE_INT
);
465 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
466 data
.f
[c
] = (float) op
[0]->value
.i
[c
];
470 assert(op
[0]->type
->base_type
== GLSL_TYPE_UINT
);
471 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
472 data
.f
[c
] = (float) op
[0]->value
.u
[c
];
476 assert(op
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
477 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
478 data
.f
[c
] = op
[0]->value
.b
[c
] ? 1.0F
: 0.0F
;
482 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
483 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
484 data
.b
[c
] = op
[0]->value
.f
[c
] != 0.0F
? true : false;
488 assert(op
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
489 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
490 data
.u
[c
] = op
[0]->value
.b
[c
] ? 1 : 0;
494 assert(op
[0]->type
->is_integer());
495 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
496 data
.b
[c
] = op
[0]->value
.u
[c
] ? true : false;
500 assert(op
[0]->type
->base_type
== GLSL_TYPE_UINT
);
501 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
502 data
.i
[c
] = op
[0]->value
.u
[c
];
506 assert(op
[0]->type
->base_type
== GLSL_TYPE_INT
);
507 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
508 data
.u
[c
] = op
[0]->value
.i
[c
];
511 case ir_unop_bitcast_i2f
:
512 assert(op
[0]->type
->base_type
== GLSL_TYPE_INT
);
513 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
514 data
.f
[c
] = bitcast_u2f(op
[0]->value
.i
[c
]);
517 case ir_unop_bitcast_f2i
:
518 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
519 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
520 data
.i
[c
] = bitcast_f2u(op
[0]->value
.f
[c
]);
523 case ir_unop_bitcast_u2f
:
524 assert(op
[0]->type
->base_type
== GLSL_TYPE_UINT
);
525 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
526 data
.f
[c
] = bitcast_u2f(op
[0]->value
.u
[c
]);
529 case ir_unop_bitcast_f2u
:
530 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
531 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
532 data
.u
[c
] = bitcast_f2u(op
[0]->value
.f
[c
]);
536 assert(op
[0]->type
->is_boolean());
538 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
539 if (op
[0]->value
.b
[c
])
545 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
546 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
547 data
.f
[c
] = truncf(op
[0]->value
.f
[c
]);
551 case ir_unop_round_even
:
552 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
553 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
554 data
.f
[c
] = _mesa_round_to_even(op
[0]->value
.f
[c
]);
559 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
560 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
561 data
.f
[c
] = ceilf(op
[0]->value
.f
[c
]);
566 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
567 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
568 data
.f
[c
] = floorf(op
[0]->value
.f
[c
]);
573 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
574 switch (this->type
->base_type
) {
581 case GLSL_TYPE_FLOAT
:
582 data
.f
[c
] = op
[0]->value
.f
[c
] - floor(op
[0]->value
.f
[c
]);
591 case ir_unop_sin_reduced
:
592 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
593 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
594 data
.f
[c
] = sinf(op
[0]->value
.f
[c
]);
599 case ir_unop_cos_reduced
:
600 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
601 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
602 data
.f
[c
] = cosf(op
[0]->value
.f
[c
]);
607 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
608 switch (this->type
->base_type
) {
610 data
.u
[c
] = -((int) op
[0]->value
.u
[c
]);
613 data
.i
[c
] = -op
[0]->value
.i
[c
];
615 case GLSL_TYPE_FLOAT
:
616 data
.f
[c
] = -op
[0]->value
.f
[c
];
625 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
626 switch (this->type
->base_type
) {
628 data
.u
[c
] = op
[0]->value
.u
[c
];
631 data
.i
[c
] = op
[0]->value
.i
[c
];
633 data
.i
[c
] = -data
.i
[c
];
635 case GLSL_TYPE_FLOAT
:
636 data
.f
[c
] = fabs(op
[0]->value
.f
[c
]);
645 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
646 switch (this->type
->base_type
) {
648 data
.u
[c
] = op
[0]->value
.i
[c
] > 0;
651 data
.i
[c
] = (op
[0]->value
.i
[c
] > 0) - (op
[0]->value
.i
[c
] < 0);
653 case GLSL_TYPE_FLOAT
:
654 data
.f
[c
] = float((op
[0]->value
.f
[c
] > 0)-(op
[0]->value
.f
[c
] < 0));
663 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
664 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
665 switch (this->type
->base_type
) {
667 if (op
[0]->value
.u
[c
] != 0.0)
668 data
.u
[c
] = 1 / op
[0]->value
.u
[c
];
671 if (op
[0]->value
.i
[c
] != 0.0)
672 data
.i
[c
] = 1 / op
[0]->value
.i
[c
];
674 case GLSL_TYPE_FLOAT
:
675 if (op
[0]->value
.f
[c
] != 0.0)
676 data
.f
[c
] = 1.0F
/ op
[0]->value
.f
[c
];
685 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
686 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
687 data
.f
[c
] = 1.0F
/ sqrtf(op
[0]->value
.f
[c
]);
692 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
693 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
694 data
.f
[c
] = sqrtf(op
[0]->value
.f
[c
]);
699 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
700 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
701 data
.f
[c
] = expf(op
[0]->value
.f
[c
]);
706 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
707 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
708 data
.f
[c
] = exp2f(op
[0]->value
.f
[c
]);
713 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
714 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
715 data
.f
[c
] = logf(op
[0]->value
.f
[c
]);
720 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
721 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
722 data
.f
[c
] = log2f(op
[0]->value
.f
[c
]);
728 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
729 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
734 case ir_unop_pack_snorm_2x16
:
735 assert(op
[0]->type
== glsl_type::vec2_type
);
736 data
.u
[0] = pack_2x16(pack_snorm_1x16
,
740 case ir_unop_pack_snorm_4x8
:
741 assert(op
[0]->type
== glsl_type::vec4_type
);
742 data
.u
[0] = pack_4x8(pack_snorm_1x8
,
748 case ir_unop_unpack_snorm_2x16
:
749 assert(op
[0]->type
== glsl_type::uint_type
);
750 unpack_2x16(unpack_snorm_1x16
,
752 &data
.f
[0], &data
.f
[1]);
754 case ir_unop_unpack_snorm_4x8
:
755 assert(op
[0]->type
== glsl_type::uint_type
);
756 unpack_4x8(unpack_snorm_1x8
,
758 &data
.f
[0], &data
.f
[1], &data
.f
[2], &data
.f
[3]);
760 case ir_unop_pack_unorm_2x16
:
761 assert(op
[0]->type
== glsl_type::vec2_type
);
762 data
.u
[0] = pack_2x16(pack_unorm_1x16
,
766 case ir_unop_pack_unorm_4x8
:
767 assert(op
[0]->type
== glsl_type::vec4_type
);
768 data
.u
[0] = pack_4x8(pack_unorm_1x8
,
774 case ir_unop_unpack_unorm_2x16
:
775 assert(op
[0]->type
== glsl_type::uint_type
);
776 unpack_2x16(unpack_unorm_1x16
,
778 &data
.f
[0], &data
.f
[1]);
780 case ir_unop_unpack_unorm_4x8
:
781 assert(op
[0]->type
== glsl_type::uint_type
);
782 unpack_4x8(unpack_unorm_1x8
,
784 &data
.f
[0], &data
.f
[1], &data
.f
[2], &data
.f
[3]);
786 case ir_unop_pack_half_2x16
:
787 assert(op
[0]->type
== glsl_type::vec2_type
);
788 data
.u
[0] = pack_2x16(pack_half_1x16
,
792 case ir_unop_unpack_half_2x16
:
793 assert(op
[0]->type
== glsl_type::uint_type
);
794 unpack_2x16(unpack_half_1x16
,
796 &data
.f
[0], &data
.f
[1]);
799 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
800 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
801 data
.f
[c
] = powf(op
[0]->value
.f
[c
], op
[1]->value
.f
[c
]);
806 data
.f
[0] = dot(op
[0], op
[1]);
810 assert(op
[0]->type
== op
[1]->type
|| op0_scalar
|| op1_scalar
);
811 for (unsigned c
= 0, c0
= 0, c1
= 0;
813 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
815 switch (op
[0]->type
->base_type
) {
817 data
.u
[c
] = MIN2(op
[0]->value
.u
[c0
], op
[1]->value
.u
[c1
]);
820 data
.i
[c
] = MIN2(op
[0]->value
.i
[c0
], op
[1]->value
.i
[c1
]);
822 case GLSL_TYPE_FLOAT
:
823 data
.f
[c
] = MIN2(op
[0]->value
.f
[c0
], op
[1]->value
.f
[c1
]);
832 assert(op
[0]->type
== op
[1]->type
|| op0_scalar
|| op1_scalar
);
833 for (unsigned c
= 0, c0
= 0, c1
= 0;
835 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
837 switch (op
[0]->type
->base_type
) {
839 data
.u
[c
] = MAX2(op
[0]->value
.u
[c0
], op
[1]->value
.u
[c1
]);
842 data
.i
[c
] = MAX2(op
[0]->value
.i
[c0
], op
[1]->value
.i
[c1
]);
844 case GLSL_TYPE_FLOAT
:
845 data
.f
[c
] = MAX2(op
[0]->value
.f
[c0
], op
[1]->value
.f
[c1
]);
854 assert(op
[0]->type
== op
[1]->type
|| op0_scalar
|| op1_scalar
);
855 for (unsigned c
= 0, c0
= 0, c1
= 0;
857 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
859 switch (op
[0]->type
->base_type
) {
861 data
.u
[c
] = op
[0]->value
.u
[c0
] + op
[1]->value
.u
[c1
];
864 data
.i
[c
] = op
[0]->value
.i
[c0
] + op
[1]->value
.i
[c1
];
866 case GLSL_TYPE_FLOAT
:
867 data
.f
[c
] = op
[0]->value
.f
[c0
] + op
[1]->value
.f
[c1
];
876 assert(op
[0]->type
== op
[1]->type
|| op0_scalar
|| op1_scalar
);
877 for (unsigned c
= 0, c0
= 0, c1
= 0;
879 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
881 switch (op
[0]->type
->base_type
) {
883 data
.u
[c
] = op
[0]->value
.u
[c0
] - op
[1]->value
.u
[c1
];
886 data
.i
[c
] = op
[0]->value
.i
[c0
] - op
[1]->value
.i
[c1
];
888 case GLSL_TYPE_FLOAT
:
889 data
.f
[c
] = op
[0]->value
.f
[c0
] - op
[1]->value
.f
[c1
];
898 /* Check for equal types, or unequal types involving scalars */
899 if ((op
[0]->type
== op
[1]->type
&& !op
[0]->type
->is_matrix())
900 || op0_scalar
|| op1_scalar
) {
901 for (unsigned c
= 0, c0
= 0, c1
= 0;
903 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
905 switch (op
[0]->type
->base_type
) {
907 data
.u
[c
] = op
[0]->value
.u
[c0
] * op
[1]->value
.u
[c1
];
910 data
.i
[c
] = op
[0]->value
.i
[c0
] * op
[1]->value
.i
[c1
];
912 case GLSL_TYPE_FLOAT
:
913 data
.f
[c
] = op
[0]->value
.f
[c0
] * op
[1]->value
.f
[c1
];
920 assert(op
[0]->type
->is_matrix() || op
[1]->type
->is_matrix());
922 /* Multiply an N-by-M matrix with an M-by-P matrix. Since either
923 * matrix can be a GLSL vector, either N or P can be 1.
925 * For vec*mat, the vector is treated as a row vector. This
926 * means the vector is a 1-row x M-column matrix.
928 * For mat*vec, the vector is treated as a column vector. Since
929 * matrix_columns is 1 for vectors, this just works.
931 const unsigned n
= op
[0]->type
->is_vector()
932 ? 1 : op
[0]->type
->vector_elements
;
933 const unsigned m
= op
[1]->type
->vector_elements
;
934 const unsigned p
= op
[1]->type
->matrix_columns
;
935 for (unsigned j
= 0; j
< p
; j
++) {
936 for (unsigned i
= 0; i
< n
; i
++) {
937 for (unsigned k
= 0; k
< m
; k
++) {
938 data
.f
[i
+n
*j
] += op
[0]->value
.f
[i
+n
*k
]*op
[1]->value
.f
[k
+m
*j
];
946 /* FINISHME: Emit warning when division-by-zero is detected. */
947 assert(op
[0]->type
== op
[1]->type
|| op0_scalar
|| op1_scalar
);
948 for (unsigned c
= 0, c0
= 0, c1
= 0;
950 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
952 switch (op
[0]->type
->base_type
) {
954 if (op
[1]->value
.u
[c1
] == 0) {
957 data
.u
[c
] = op
[0]->value
.u
[c0
] / op
[1]->value
.u
[c1
];
961 if (op
[1]->value
.i
[c1
] == 0) {
964 data
.i
[c
] = op
[0]->value
.i
[c0
] / op
[1]->value
.i
[c1
];
967 case GLSL_TYPE_FLOAT
:
968 data
.f
[c
] = op
[0]->value
.f
[c0
] / op
[1]->value
.f
[c1
];
977 /* FINISHME: Emit warning when division-by-zero is detected. */
978 assert(op
[0]->type
== op
[1]->type
|| op0_scalar
|| op1_scalar
);
979 for (unsigned c
= 0, c0
= 0, c1
= 0;
981 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
983 switch (op
[0]->type
->base_type
) {
985 if (op
[1]->value
.u
[c1
] == 0) {
988 data
.u
[c
] = op
[0]->value
.u
[c0
] % op
[1]->value
.u
[c1
];
992 if (op
[1]->value
.i
[c1
] == 0) {
995 data
.i
[c
] = op
[0]->value
.i
[c0
] % op
[1]->value
.i
[c1
];
998 case GLSL_TYPE_FLOAT
:
999 /* We don't use fmod because it rounds toward zero; GLSL specifies
1002 data
.f
[c
] = op
[0]->value
.f
[c0
] - op
[1]->value
.f
[c1
]
1003 * floorf(op
[0]->value
.f
[c0
] / op
[1]->value
.f
[c1
]);
1012 case ir_binop_logic_and
:
1013 assert(op
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
1014 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++)
1015 data
.b
[c
] = op
[0]->value
.b
[c
] && op
[1]->value
.b
[c
];
1017 case ir_binop_logic_xor
:
1018 assert(op
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
1019 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++)
1020 data
.b
[c
] = op
[0]->value
.b
[c
] ^ op
[1]->value
.b
[c
];
1022 case ir_binop_logic_or
:
1023 assert(op
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
1024 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++)
1025 data
.b
[c
] = op
[0]->value
.b
[c
] || op
[1]->value
.b
[c
];
1029 assert(op
[0]->type
== op
[1]->type
);
1030 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
1031 switch (op
[0]->type
->base_type
) {
1032 case GLSL_TYPE_UINT
:
1033 data
.b
[c
] = op
[0]->value
.u
[c
] < op
[1]->value
.u
[c
];
1036 data
.b
[c
] = op
[0]->value
.i
[c
] < op
[1]->value
.i
[c
];
1038 case GLSL_TYPE_FLOAT
:
1039 data
.b
[c
] = op
[0]->value
.f
[c
] < op
[1]->value
.f
[c
];
1046 case ir_binop_greater
:
1047 assert(op
[0]->type
== op
[1]->type
);
1048 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
1049 switch (op
[0]->type
->base_type
) {
1050 case GLSL_TYPE_UINT
:
1051 data
.b
[c
] = op
[0]->value
.u
[c
] > op
[1]->value
.u
[c
];
1054 data
.b
[c
] = op
[0]->value
.i
[c
] > op
[1]->value
.i
[c
];
1056 case GLSL_TYPE_FLOAT
:
1057 data
.b
[c
] = op
[0]->value
.f
[c
] > op
[1]->value
.f
[c
];
1064 case ir_binop_lequal
:
1065 assert(op
[0]->type
== op
[1]->type
);
1066 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
1067 switch (op
[0]->type
->base_type
) {
1068 case GLSL_TYPE_UINT
:
1069 data
.b
[c
] = op
[0]->value
.u
[c
] <= op
[1]->value
.u
[c
];
1072 data
.b
[c
] = op
[0]->value
.i
[c
] <= op
[1]->value
.i
[c
];
1074 case GLSL_TYPE_FLOAT
:
1075 data
.b
[c
] = op
[0]->value
.f
[c
] <= op
[1]->value
.f
[c
];
1082 case ir_binop_gequal
:
1083 assert(op
[0]->type
== op
[1]->type
);
1084 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
1085 switch (op
[0]->type
->base_type
) {
1086 case GLSL_TYPE_UINT
:
1087 data
.b
[c
] = op
[0]->value
.u
[c
] >= op
[1]->value
.u
[c
];
1090 data
.b
[c
] = op
[0]->value
.i
[c
] >= op
[1]->value
.i
[c
];
1092 case GLSL_TYPE_FLOAT
:
1093 data
.b
[c
] = op
[0]->value
.f
[c
] >= op
[1]->value
.f
[c
];
1100 case ir_binop_equal
:
1101 assert(op
[0]->type
== op
[1]->type
);
1102 for (unsigned c
= 0; c
< components
; c
++) {
1103 switch (op
[0]->type
->base_type
) {
1104 case GLSL_TYPE_UINT
:
1105 data
.b
[c
] = op
[0]->value
.u
[c
] == op
[1]->value
.u
[c
];
1108 data
.b
[c
] = op
[0]->value
.i
[c
] == op
[1]->value
.i
[c
];
1110 case GLSL_TYPE_FLOAT
:
1111 data
.b
[c
] = op
[0]->value
.f
[c
] == op
[1]->value
.f
[c
];
1113 case GLSL_TYPE_BOOL
:
1114 data
.b
[c
] = op
[0]->value
.b
[c
] == op
[1]->value
.b
[c
];
1121 case ir_binop_nequal
:
1122 assert(op
[0]->type
== op
[1]->type
);
1123 for (unsigned c
= 0; c
< components
; c
++) {
1124 switch (op
[0]->type
->base_type
) {
1125 case GLSL_TYPE_UINT
:
1126 data
.b
[c
] = op
[0]->value
.u
[c
] != op
[1]->value
.u
[c
];
1129 data
.b
[c
] = op
[0]->value
.i
[c
] != op
[1]->value
.i
[c
];
1131 case GLSL_TYPE_FLOAT
:
1132 data
.b
[c
] = op
[0]->value
.f
[c
] != op
[1]->value
.f
[c
];
1134 case GLSL_TYPE_BOOL
:
1135 data
.b
[c
] = op
[0]->value
.b
[c
] != op
[1]->value
.b
[c
];
1142 case ir_binop_all_equal
:
1143 data
.b
[0] = op
[0]->has_value(op
[1]);
1145 case ir_binop_any_nequal
:
1146 data
.b
[0] = !op
[0]->has_value(op
[1]);
1149 case ir_binop_lshift
:
1150 for (unsigned c
= 0, c0
= 0, c1
= 0;
1152 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
1154 if (op
[0]->type
->base_type
== GLSL_TYPE_INT
&&
1155 op
[1]->type
->base_type
== GLSL_TYPE_INT
) {
1156 data
.i
[c
] = op
[0]->value
.i
[c0
] << op
[1]->value
.i
[c1
];
1158 } else if (op
[0]->type
->base_type
== GLSL_TYPE_INT
&&
1159 op
[1]->type
->base_type
== GLSL_TYPE_UINT
) {
1160 data
.i
[c
] = op
[0]->value
.i
[c0
] << op
[1]->value
.u
[c1
];
1162 } else if (op
[0]->type
->base_type
== GLSL_TYPE_UINT
&&
1163 op
[1]->type
->base_type
== GLSL_TYPE_INT
) {
1164 data
.u
[c
] = op
[0]->value
.u
[c0
] << op
[1]->value
.i
[c1
];
1166 } else if (op
[0]->type
->base_type
== GLSL_TYPE_UINT
&&
1167 op
[1]->type
->base_type
== GLSL_TYPE_UINT
) {
1168 data
.u
[c
] = op
[0]->value
.u
[c0
] << op
[1]->value
.u
[c1
];
1173 case ir_binop_rshift
:
1174 for (unsigned c
= 0, c0
= 0, c1
= 0;
1176 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
1178 if (op
[0]->type
->base_type
== GLSL_TYPE_INT
&&
1179 op
[1]->type
->base_type
== GLSL_TYPE_INT
) {
1180 data
.i
[c
] = op
[0]->value
.i
[c0
] >> op
[1]->value
.i
[c1
];
1182 } else if (op
[0]->type
->base_type
== GLSL_TYPE_INT
&&
1183 op
[1]->type
->base_type
== GLSL_TYPE_UINT
) {
1184 data
.i
[c
] = op
[0]->value
.i
[c0
] >> op
[1]->value
.u
[c1
];
1186 } else if (op
[0]->type
->base_type
== GLSL_TYPE_UINT
&&
1187 op
[1]->type
->base_type
== GLSL_TYPE_INT
) {
1188 data
.u
[c
] = op
[0]->value
.u
[c0
] >> op
[1]->value
.i
[c1
];
1190 } else if (op
[0]->type
->base_type
== GLSL_TYPE_UINT
&&
1191 op
[1]->type
->base_type
== GLSL_TYPE_UINT
) {
1192 data
.u
[c
] = op
[0]->value
.u
[c0
] >> op
[1]->value
.u
[c1
];
1197 case ir_binop_bit_and
:
1198 for (unsigned c
= 0, c0
= 0, c1
= 0;
1200 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
1202 switch (op
[0]->type
->base_type
) {
1204 data
.i
[c
] = op
[0]->value
.i
[c0
] & op
[1]->value
.i
[c1
];
1206 case GLSL_TYPE_UINT
:
1207 data
.u
[c
] = op
[0]->value
.u
[c0
] & op
[1]->value
.u
[c1
];
1215 case ir_binop_bit_or
:
1216 for (unsigned c
= 0, c0
= 0, c1
= 0;
1218 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
1220 switch (op
[0]->type
->base_type
) {
1222 data
.i
[c
] = op
[0]->value
.i
[c0
] | op
[1]->value
.i
[c1
];
1224 case GLSL_TYPE_UINT
:
1225 data
.u
[c
] = op
[0]->value
.u
[c0
] | op
[1]->value
.u
[c1
];
1233 case ir_binop_bit_xor
:
1234 for (unsigned c
= 0, c0
= 0, c1
= 0;
1236 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
1238 switch (op
[0]->type
->base_type
) {
1240 data
.i
[c
] = op
[0]->value
.i
[c0
] ^ op
[1]->value
.i
[c1
];
1242 case GLSL_TYPE_UINT
:
1243 data
.u
[c
] = op
[0]->value
.u
[c0
] ^ op
[1]->value
.u
[c1
];
1251 case ir_triop_lrp
: {
1252 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
1253 assert(op
[1]->type
->base_type
== GLSL_TYPE_FLOAT
);
1254 assert(op
[2]->type
->base_type
== GLSL_TYPE_FLOAT
);
1256 unsigned c2_inc
= op
[2]->type
->is_scalar() ? 0 : 1;
1257 for (unsigned c
= 0, c2
= 0; c
< components
; c2
+= c2_inc
, c
++) {
1258 data
.f
[c
] = op
[0]->value
.f
[c
] * (1.0f
- op
[2]->value
.f
[c2
]) +
1259 (op
[1]->value
.f
[c
] * op
[2]->value
.f
[c2
]);
1264 case ir_quadop_vector
:
1265 for (unsigned c
= 0; c
< this->type
->vector_elements
; c
++) {
1266 switch (this->type
->base_type
) {
1268 data
.i
[c
] = op
[c
]->value
.i
[0];
1270 case GLSL_TYPE_UINT
:
1271 data
.u
[c
] = op
[c
]->value
.u
[0];
1273 case GLSL_TYPE_FLOAT
:
1274 data
.f
[c
] = op
[c
]->value
.f
[0];
1283 /* FINISHME: Should handle all expression types. */
1287 return new(ctx
) ir_constant(this->type
, &data
);
1292 ir_texture::constant_expression_value(struct hash_table
*variable_context
)
1294 /* texture lookups aren't constant expressions */
1300 ir_swizzle::constant_expression_value(struct hash_table
*variable_context
)
1302 ir_constant
*v
= this->val
->constant_expression_value(variable_context
);
1305 ir_constant_data data
= { { 0 } };
1307 const unsigned swiz_idx
[4] = {
1308 this->mask
.x
, this->mask
.y
, this->mask
.z
, this->mask
.w
1311 for (unsigned i
= 0; i
< this->mask
.num_components
; i
++) {
1312 switch (v
->type
->base_type
) {
1313 case GLSL_TYPE_UINT
:
1314 case GLSL_TYPE_INT
: data
.u
[i
] = v
->value
.u
[swiz_idx
[i
]]; break;
1315 case GLSL_TYPE_FLOAT
: data
.f
[i
] = v
->value
.f
[swiz_idx
[i
]]; break;
1316 case GLSL_TYPE_BOOL
: data
.b
[i
] = v
->value
.b
[swiz_idx
[i
]]; break;
1317 default: assert(!"Should not get here."); break;
1321 void *ctx
= ralloc_parent(this);
1322 return new(ctx
) ir_constant(this->type
, &data
);
1329 ir_dereference_variable::constant_referenced(struct hash_table
*variable_context
,
1330 ir_constant
*&store
, int &offset
) const
1332 if (variable_context
) {
1333 store
= (ir_constant
*)hash_table_find(variable_context
, var
);
1342 ir_dereference_variable::constant_expression_value(struct hash_table
*variable_context
)
1344 /* This may occur during compile and var->type is glsl_type::error_type */
1348 /* Give priority to the context hashtable, if it exists */
1349 if (variable_context
) {
1350 ir_constant
*value
= (ir_constant
*)hash_table_find(variable_context
, var
);
1355 /* The constant_value of a uniform variable is its initializer,
1356 * not the lifetime constant value of the uniform.
1358 if (var
->mode
== ir_var_uniform
)
1361 if (!var
->constant_value
)
1364 return var
->constant_value
->clone(ralloc_parent(var
), NULL
);
1369 ir_dereference_array::constant_referenced(struct hash_table
*variable_context
,
1370 ir_constant
*&store
, int &offset
) const
1372 ir_constant
*index_c
= array_index
->constant_expression_value(variable_context
);
1374 if (!index_c
|| !index_c
->type
->is_scalar() || !index_c
->type
->is_integer()) {
1380 int index
= index_c
->type
->base_type
== GLSL_TYPE_INT
?
1381 index_c
->get_int_component(0) :
1382 index_c
->get_uint_component(0);
1384 ir_constant
*substore
;
1386 const ir_dereference
*deref
= array
->as_dereference();
1393 deref
->constant_referenced(variable_context
, substore
, suboffset
);
1401 const glsl_type
*vt
= substore
->type
;
1402 if (vt
->is_array()) {
1403 store
= substore
->get_array_element(index
);
1407 if (vt
->is_matrix()) {
1409 offset
= index
* vt
->vector_elements
;
1412 if (vt
->is_vector()) {
1414 offset
= suboffset
+ index
;
1423 ir_dereference_array::constant_expression_value(struct hash_table
*variable_context
)
1425 ir_constant
*array
= this->array
->constant_expression_value(variable_context
);
1426 ir_constant
*idx
= this->array_index
->constant_expression_value(variable_context
);
1428 if ((array
!= NULL
) && (idx
!= NULL
)) {
1429 void *ctx
= ralloc_parent(this);
1430 if (array
->type
->is_matrix()) {
1431 /* Array access of a matrix results in a vector.
1433 const unsigned column
= idx
->value
.u
[0];
1435 const glsl_type
*const column_type
= array
->type
->column_type();
1437 /* Offset in the constant matrix to the first element of the column
1440 const unsigned mat_idx
= column
* column_type
->vector_elements
;
1442 ir_constant_data data
= { { 0 } };
1444 switch (column_type
->base_type
) {
1445 case GLSL_TYPE_UINT
:
1447 for (unsigned i
= 0; i
< column_type
->vector_elements
; i
++)
1448 data
.u
[i
] = array
->value
.u
[mat_idx
+ i
];
1452 case GLSL_TYPE_FLOAT
:
1453 for (unsigned i
= 0; i
< column_type
->vector_elements
; i
++)
1454 data
.f
[i
] = array
->value
.f
[mat_idx
+ i
];
1459 assert(!"Should not get here.");
1463 return new(ctx
) ir_constant(column_type
, &data
);
1464 } else if (array
->type
->is_vector()) {
1465 const unsigned component
= idx
->value
.u
[0];
1467 return new(ctx
) ir_constant(array
, component
);
1469 const unsigned index
= idx
->value
.u
[0];
1470 return array
->get_array_element(index
)->clone(ctx
, NULL
);
1478 ir_dereference_record::constant_referenced(struct hash_table
*variable_context
,
1479 ir_constant
*&store
, int &offset
) const
1481 ir_constant
*substore
;
1483 const ir_dereference
*deref
= record
->as_dereference();
1490 deref
->constant_referenced(variable_context
, substore
, suboffset
);
1498 store
= substore
->get_record_field(field
);
1503 ir_dereference_record::constant_expression_value(struct hash_table
*variable_context
)
1505 ir_constant
*v
= this->record
->constant_expression_value();
1507 return (v
!= NULL
) ? v
->get_record_field(this->field
) : NULL
;
1512 ir_assignment::constant_expression_value(struct hash_table
*variable_context
)
1514 /* FINISHME: Handle CEs involving assignment (return RHS) */
1520 ir_constant::constant_expression_value(struct hash_table
*variable_context
)
1527 ir_call::constant_expression_value(struct hash_table
*variable_context
)
1529 return this->callee
->constant_expression_value(&this->actual_parameters
, variable_context
);
1533 bool ir_function_signature::constant_expression_evaluate_expression_list(const struct exec_list
&body
,
1534 struct hash_table
*variable_context
,
1535 ir_constant
**result
)
1537 foreach_list(n
, &body
) {
1538 ir_instruction
*inst
= (ir_instruction
*)n
;
1539 switch(inst
->ir_type
) {
1541 /* (declare () type symbol) */
1542 case ir_type_variable
: {
1543 ir_variable
*var
= inst
->as_variable();
1544 hash_table_insert(variable_context
, ir_constant::zero(this, var
->type
), var
);
1548 /* (assign [condition] (write-mask) (ref) (value)) */
1549 case ir_type_assignment
: {
1550 ir_assignment
*asg
= inst
->as_assignment();
1551 if (asg
->condition
) {
1552 ir_constant
*cond
= asg
->condition
->constant_expression_value(variable_context
);
1555 if (!cond
->get_bool_component(0))
1559 ir_constant
*store
= NULL
;
1561 asg
->lhs
->constant_referenced(variable_context
, store
, offset
);
1566 ir_constant
*value
= asg
->rhs
->constant_expression_value(variable_context
);
1571 store
->copy_masked_offset(value
, offset
, asg
->write_mask
);
1575 /* (return (expression)) */
1576 case ir_type_return
:
1578 *result
= inst
->as_return()->value
->constant_expression_value(variable_context
);
1579 return *result
!= NULL
;
1581 /* (call name (ref) (params))*/
1582 case ir_type_call
: {
1583 ir_call
*call
= inst
->as_call();
1585 /* Just say no to void functions in constant expressions. We
1586 * don't need them at that point.
1589 if (!call
->return_deref
)
1592 ir_constant
*store
= NULL
;
1594 call
->return_deref
->constant_referenced(variable_context
, store
, offset
);
1599 ir_constant
*value
= call
->constant_expression_value(variable_context
);
1604 store
->copy_offset(value
, offset
);
1608 /* (if condition (then-instructions) (else-instructions)) */
1610 ir_if
*iif
= inst
->as_if();
1612 ir_constant
*cond
= iif
->condition
->constant_expression_value(variable_context
);
1613 if (!cond
|| !cond
->type
->is_boolean())
1616 exec_list
&branch
= cond
->get_bool_component(0) ? iif
->then_instructions
: iif
->else_instructions
;
1619 if (!constant_expression_evaluate_expression_list(branch
, variable_context
, result
))
1622 /* If there was a return in the branch chosen, drop out now. */
1629 /* Every other expression type, we drop out. */
1635 /* Reaching the end of the block is not an error condition */
1643 ir_function_signature::constant_expression_value(exec_list
*actual_parameters
, struct hash_table
*variable_context
)
1645 const glsl_type
*type
= this->return_type
;
1646 if (type
== glsl_type::void_type
)
1649 /* From the GLSL 1.20 spec, page 23:
1650 * "Function calls to user-defined functions (non-built-in functions)
1651 * cannot be used to form constant expressions."
1653 if (!this->is_builtin
)
1657 * Of the builtin functions, only the texture lookups and the noise
1658 * ones must not be used in constant expressions. They all include
1659 * specific opcodes so they don't need to be special-cased at this
1663 /* Initialize the table of dereferencable names with the function
1664 * parameters. Verify their const-ness on the way.
1666 * We expect the correctness of the number of parameters to have
1667 * been checked earlier.
1669 hash_table
*deref_hash
= hash_table_ctor(8, hash_table_pointer_hash
,
1670 hash_table_pointer_compare
);
1672 /* If "origin" is non-NULL, then the function body is there. So we
1673 * have to use the variable objects from the object with the body,
1674 * but the parameter instanciation on the current object.
1676 const exec_node
*parameter_info
= origin
? origin
->parameters
.head
: parameters
.head
;
1678 foreach_list(n
, actual_parameters
) {
1679 ir_constant
*constant
= ((ir_rvalue
*) n
)->constant_expression_value(variable_context
);
1680 if (constant
== NULL
) {
1681 hash_table_dtor(deref_hash
);
1686 ir_variable
*var
= (ir_variable
*)parameter_info
;
1687 hash_table_insert(deref_hash
, constant
, var
);
1689 parameter_info
= parameter_info
->next
;
1692 ir_constant
*result
= NULL
;
1694 /* Now run the builtin function until something non-constant
1695 * happens or we get the result.
1697 if (constant_expression_evaluate_expression_list(origin
? origin
->body
: body
, deref_hash
, &result
) && result
)
1698 result
= result
->clone(ralloc_parent(this), NULL
);
1700 hash_table_dtor(deref_hash
);