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 */
38 #include "util/rounding.h" /* for _mesa_roundeven */
39 #include "util/half_float.h"
41 #include "compiler/glsl_types.h"
42 #include "program/hash_table.h"
45 dot_f(ir_constant
*op0
, ir_constant
*op1
)
47 assert(op0
->type
->is_float() && op1
->type
->is_float());
50 for (unsigned c
= 0; c
< op0
->type
->components(); c
++)
51 result
+= op0
->value
.f
[c
] * op1
->value
.f
[c
];
57 dot_d(ir_constant
*op0
, ir_constant
*op1
)
59 assert(op0
->type
->is_double() && op1
->type
->is_double());
62 for (unsigned c
= 0; c
< op0
->type
->components(); c
++)
63 result
+= op0
->value
.d
[c
] * op1
->value
.d
[c
];
68 /* This method is the only one supported by gcc. Unions in particular
69 * are iffy, and read-through-converted-pointer is killed by strict
70 * aliasing. OTOH, the compiler sees through the memcpy, so the
71 * resulting asm is reasonable.
74 bitcast_u2f(unsigned int u
)
76 assert(sizeof(float) == sizeof(unsigned int));
78 memcpy(&f
, &u
, sizeof(f
));
85 assert(sizeof(float) == sizeof(unsigned int));
87 memcpy(&u
, &f
, sizeof(f
));
92 * Evaluate one component of a floating-point 4x8 unpacking function.
95 (*pack_1x8_func_t
)(float);
98 * Evaluate one component of a floating-point 2x16 unpacking function.
101 (*pack_1x16_func_t
)(float);
104 * Evaluate one component of a floating-point 4x8 unpacking function.
107 (*unpack_1x8_func_t
)(uint8_t);
110 * Evaluate one component of a floating-point 2x16 unpacking function.
113 (*unpack_1x16_func_t
)(uint16_t);
116 * Evaluate a 2x16 floating-point packing function.
119 pack_2x16(pack_1x16_func_t pack_1x16
,
122 /* From section 8.4 of the GLSL ES 3.00 spec:
126 * The first component of the vector will be written to the least
127 * significant bits of the output; the last component will be written to
128 * the most significant bits.
130 * The specifications for the other packing functions contain similar
134 u
|= ((uint32_t) pack_1x16(x
) << 0);
135 u
|= ((uint32_t) pack_1x16(y
) << 16);
140 * Evaluate a 4x8 floating-point packing function.
143 pack_4x8(pack_1x8_func_t pack_1x8
,
144 float x
, float y
, float z
, float w
)
146 /* From section 8.4 of the GLSL 4.30 spec:
150 * The first component of the vector will be written to the least
151 * significant bits of the output; the last component will be written to
152 * the most significant bits.
154 * The specifications for the other packing functions contain similar
158 u
|= ((uint32_t) pack_1x8(x
) << 0);
159 u
|= ((uint32_t) pack_1x8(y
) << 8);
160 u
|= ((uint32_t) pack_1x8(z
) << 16);
161 u
|= ((uint32_t) pack_1x8(w
) << 24);
166 * Evaluate a 2x16 floating-point unpacking function.
169 unpack_2x16(unpack_1x16_func_t unpack_1x16
,
173 /* From section 8.4 of the GLSL ES 3.00 spec:
177 * The first component of the returned vector will be extracted from
178 * the least significant bits of the input; the last component will be
179 * extracted from the most significant bits.
181 * The specifications for the other unpacking functions contain similar
184 *x
= unpack_1x16((uint16_t) (u
& 0xffff));
185 *y
= unpack_1x16((uint16_t) (u
>> 16));
189 * Evaluate a 4x8 floating-point unpacking function.
192 unpack_4x8(unpack_1x8_func_t unpack_1x8
, uint32_t u
,
193 float *x
, float *y
, float *z
, float *w
)
195 /* From section 8.4 of the GLSL 4.30 spec:
199 * The first component of the returned vector will be extracted from
200 * the least significant bits of the input; the last component will be
201 * extracted from the most significant bits.
203 * The specifications for the other unpacking functions contain similar
206 *x
= unpack_1x8((uint8_t) (u
& 0xff));
207 *y
= unpack_1x8((uint8_t) (u
>> 8));
208 *z
= unpack_1x8((uint8_t) (u
>> 16));
209 *w
= unpack_1x8((uint8_t) (u
>> 24));
213 * Evaluate one component of packSnorm4x8.
216 pack_snorm_1x8(float x
)
218 /* From section 8.4 of the GLSL 4.30 spec:
222 * The conversion for component c of v to fixed point is done as
225 * packSnorm4x8: round(clamp(c, -1, +1) * 127.0)
228 _mesa_lroundevenf(CLAMP(x
, -1.0f
, +1.0f
) * 127.0f
);
232 * Evaluate one component of packSnorm2x16.
235 pack_snorm_1x16(float x
)
237 /* From section 8.4 of the GLSL ES 3.00 spec:
241 * The conversion for component c of v to fixed point is done as
244 * packSnorm2x16: round(clamp(c, -1, +1) * 32767.0)
247 _mesa_lroundevenf(CLAMP(x
, -1.0f
, +1.0f
) * 32767.0f
);
251 * Evaluate one component of unpackSnorm4x8.
254 unpack_snorm_1x8(uint8_t u
)
256 /* From section 8.4 of the GLSL 4.30 spec:
260 * The conversion for unpacked fixed-point value f to floating point is
263 * unpackSnorm4x8: clamp(f / 127.0, -1, +1)
265 return CLAMP((int8_t) u
/ 127.0f
, -1.0f
, +1.0f
);
269 * Evaluate one component of unpackSnorm2x16.
272 unpack_snorm_1x16(uint16_t u
)
274 /* From section 8.4 of the GLSL ES 3.00 spec:
278 * The conversion for unpacked fixed-point value f to floating point is
281 * unpackSnorm2x16: clamp(f / 32767.0, -1, +1)
283 return CLAMP((int16_t) u
/ 32767.0f
, -1.0f
, +1.0f
);
287 * Evaluate one component packUnorm4x8.
290 pack_unorm_1x8(float x
)
292 /* From section 8.4 of the GLSL 4.30 spec:
296 * The conversion for component c of v to fixed point is done as
299 * packUnorm4x8: round(clamp(c, 0, +1) * 255.0)
301 return (uint8_t) (int) _mesa_roundevenf(CLAMP(x
, 0.0f
, 1.0f
) * 255.0f
);
305 * Evaluate one component packUnorm2x16.
308 pack_unorm_1x16(float x
)
310 /* From section 8.4 of the GLSL ES 3.00 spec:
314 * The conversion for component c of v to fixed point is done as
317 * packUnorm2x16: round(clamp(c, 0, +1) * 65535.0)
319 return (uint16_t) (int)
320 _mesa_roundevenf(CLAMP(x
, 0.0f
, 1.0f
) * 65535.0f
);
324 * Evaluate one component of unpackUnorm4x8.
327 unpack_unorm_1x8(uint8_t u
)
329 /* From section 8.4 of the GLSL 4.30 spec:
333 * The conversion for unpacked fixed-point value f to floating point is
336 * unpackUnorm4x8: f / 255.0
338 return (float) u
/ 255.0f
;
342 * Evaluate one component of unpackUnorm2x16.
345 unpack_unorm_1x16(uint16_t u
)
347 /* From section 8.4 of the GLSL ES 3.00 spec:
351 * The conversion for unpacked fixed-point value f to floating point is
354 * unpackUnorm2x16: f / 65535.0
356 return (float) u
/ 65535.0f
;
360 * Evaluate one component of packHalf2x16.
363 pack_half_1x16(float x
)
365 return _mesa_float_to_half(x
);
369 * Evaluate one component of unpackHalf2x16.
372 unpack_half_1x16(uint16_t u
)
374 return _mesa_half_to_float(u
);
378 * Get the constant that is ultimately referenced by an r-value, in a constant
379 * expression evaluation context.
381 * The offset is used when the reference is to a specific column of a matrix.
384 constant_referenced(const ir_dereference
*deref
,
385 struct hash_table
*variable_context
,
386 ir_constant
*&store
, int &offset
)
391 if (variable_context
== NULL
)
394 switch (deref
->ir_type
) {
395 case ir_type_dereference_array
: {
396 const ir_dereference_array
*const da
=
397 (const ir_dereference_array
*) deref
;
399 ir_constant
*const index_c
=
400 da
->array_index
->constant_expression_value(variable_context
);
402 if (!index_c
|| !index_c
->type
->is_scalar() || !index_c
->type
->is_integer())
405 const int index
= index_c
->type
->base_type
== GLSL_TYPE_INT
?
406 index_c
->get_int_component(0) :
407 index_c
->get_uint_component(0);
409 ir_constant
*substore
;
412 const ir_dereference
*const deref
= da
->array
->as_dereference();
416 if (!constant_referenced(deref
, variable_context
, substore
, suboffset
))
419 const glsl_type
*const vt
= da
->array
->type
;
420 if (vt
->is_array()) {
421 store
= substore
->get_array_element(index
);
423 } else if (vt
->is_matrix()) {
425 offset
= index
* vt
->vector_elements
;
426 } else if (vt
->is_vector()) {
428 offset
= suboffset
+ index
;
434 case ir_type_dereference_record
: {
435 const ir_dereference_record
*const dr
=
436 (const ir_dereference_record
*) deref
;
438 const ir_dereference
*const deref
= dr
->record
->as_dereference();
442 ir_constant
*substore
;
445 if (!constant_referenced(deref
, variable_context
, substore
, suboffset
))
448 /* Since we're dropping it on the floor...
450 assert(suboffset
== 0);
452 store
= substore
->get_record_field(dr
->field
);
456 case ir_type_dereference_variable
: {
457 const ir_dereference_variable
*const dv
=
458 (const ir_dereference_variable
*) deref
;
460 store
= (ir_constant
*) hash_table_find(variable_context
, dv
->var
);
465 assert(!"Should not get here.");
469 return store
!= NULL
;
474 ir_rvalue::constant_expression_value(struct hash_table
*)
476 assert(this->type
->is_error());
481 bitfield_reverse(uint32_t v
)
483 /* http://graphics.stanford.edu/~seander/bithacks.html#BitReverseObvious */
484 uint32_t r
= v
; // r will be reversed bits of v; first get LSB of v
485 int s
= sizeof(v
) * CHAR_BIT
- 1; // extra shift needed at end
487 for (v
>>= 1; v
; v
>>= 1) {
492 r
<<= s
; // shift when v's highest bits are zero
498 find_msb_uint(uint32_t v
)
502 /* If v == 0, then the loop will terminate when count == 32. In that case
503 * 31-count will produce the -1 result required by GLSL findMSB().
505 while (((v
& (1u << 31)) == 0) && count
!= 32) {
514 find_msb_int(int32_t v
)
516 /* If v is signed, findMSB() returns the position of the most significant
519 return find_msb_uint(v
< 0 ? ~v
: v
);
523 ldexpf_flush_subnormal(float x
, int exp
)
525 const float result
= ldexpf(x
, exp
);
527 /* Flush subnormal values to zero. */
528 return !isnormal(result
) ? copysignf(0.0f
, x
) : result
;
532 ldexp_flush_subnormal(double x
, int exp
)
534 const double result
= ldexp(x
, exp
);
536 /* Flush subnormal values to zero. */
537 return !isnormal(result
) ? copysign(0.0, x
) : result
;
541 bitfield_extract_uint(uint32_t value
, int offset
, int bits
)
545 else if (offset
< 0 || bits
< 0)
546 return 0; /* Undefined, per spec. */
547 else if (offset
+ bits
> 32)
548 return 0; /* Undefined, per spec. */
550 value
<<= 32 - bits
- offset
;
557 bitfield_extract_int(int32_t value
, int offset
, int bits
)
561 else if (offset
< 0 || bits
< 0)
562 return 0; /* Undefined, per spec. */
563 else if (offset
+ bits
> 32)
564 return 0; /* Undefined, per spec. */
566 value
<<= 32 - bits
- offset
;
573 bitfield_insert(uint32_t base
, uint32_t insert
, int offset
, int bits
)
577 else if (offset
< 0 || bits
< 0)
578 return 0; /* Undefined, per spec. */
579 else if (offset
+ bits
> 32)
580 return 0; /* Undefined, per spec. */
582 unsigned insert_mask
= ((1ull << bits
) - 1) << offset
;
585 insert
&= insert_mask
;
586 base
&= ~insert_mask
;
588 return base
| insert
;
593 ir_expression::constant_expression_value(struct hash_table
*variable_context
)
595 if (this->type
->is_error())
598 ir_constant
*op
[ARRAY_SIZE(this->operands
)] = { NULL
, };
599 ir_constant_data data
;
601 memset(&data
, 0, sizeof(data
));
603 for (unsigned operand
= 0; operand
< this->get_num_operands(); operand
++) {
604 op
[operand
] = this->operands
[operand
]->constant_expression_value(variable_context
);
610 switch (this->operation
) {
611 case ir_binop_lshift
:
612 case ir_binop_rshift
:
614 case ir_binop_interpolate_at_offset
:
615 case ir_binop_interpolate_at_sample
:
616 case ir_binop_vector_extract
:
618 case ir_triop_bitfield_extract
:
622 assert(op
[0]->type
->base_type
== op
[1]->type
->base_type
);
626 bool op0_scalar
= op
[0]->type
->is_scalar();
627 bool op1_scalar
= op
[1] != NULL
&& op
[1]->type
->is_scalar();
629 /* When iterating over a vector or matrix's components, we want to increase
630 * the loop counter. However, for scalars, we want to stay at 0.
632 unsigned c0_inc
= op0_scalar
? 0 : 1;
633 unsigned c1_inc
= op1_scalar
? 0 : 1;
635 if (op1_scalar
|| !op
[1]) {
636 components
= op
[0]->type
->components();
638 components
= op
[1]->type
->components();
641 void *ctx
= ralloc_parent(this);
643 /* Handle array operations here, rather than below. */
644 if (op
[0]->type
->is_array()) {
645 assert(op
[1] != NULL
&& op
[1]->type
->is_array());
646 switch (this->operation
) {
647 case ir_binop_all_equal
:
648 return new(ctx
) ir_constant(op
[0]->has_value(op
[1]));
649 case ir_binop_any_nequal
:
650 return new(ctx
) ir_constant(!op
[0]->has_value(op
[1]));
657 switch (this->operation
) {
658 case ir_unop_bit_not
:
659 switch (op
[0]->type
->base_type
) {
661 for (unsigned c
= 0; c
< components
; c
++)
662 data
.i
[c
] = ~ op
[0]->value
.i
[c
];
665 for (unsigned c
= 0; c
< components
; c
++)
666 data
.u
[c
] = ~ op
[0]->value
.u
[c
];
673 case ir_unop_logic_not
:
674 assert(op
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
675 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++)
676 data
.b
[c
] = !op
[0]->value
.b
[c
];
680 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
681 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
682 data
.i
[c
] = (int) op
[0]->value
.f
[c
];
686 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
687 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
688 data
.u
[c
] = (unsigned) op
[0]->value
.f
[c
];
692 assert(op
[0]->type
->base_type
== GLSL_TYPE_INT
);
693 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
694 data
.f
[c
] = (float) op
[0]->value
.i
[c
];
698 assert(op
[0]->type
->base_type
== GLSL_TYPE_UINT
);
699 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
700 data
.f
[c
] = (float) op
[0]->value
.u
[c
];
704 assert(op
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
705 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
706 data
.f
[c
] = op
[0]->value
.b
[c
] ? 1.0F
: 0.0F
;
710 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
711 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
712 data
.b
[c
] = op
[0]->value
.f
[c
] != 0.0F
? true : false;
716 assert(op
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
717 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
718 data
.i
[c
] = op
[0]->value
.b
[c
] ? 1 : 0;
722 assert(op
[0]->type
->is_integer());
723 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
724 data
.b
[c
] = op
[0]->value
.u
[c
] ? true : false;
728 assert(op
[0]->type
->base_type
== GLSL_TYPE_UINT
);
729 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
730 data
.i
[c
] = op
[0]->value
.u
[c
];
734 assert(op
[0]->type
->base_type
== GLSL_TYPE_INT
);
735 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
736 data
.u
[c
] = op
[0]->value
.i
[c
];
739 case ir_unop_bitcast_i2f
:
740 assert(op
[0]->type
->base_type
== GLSL_TYPE_INT
);
741 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
742 data
.f
[c
] = bitcast_u2f(op
[0]->value
.i
[c
]);
745 case ir_unop_bitcast_f2i
:
746 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
747 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
748 data
.i
[c
] = bitcast_f2u(op
[0]->value
.f
[c
]);
751 case ir_unop_bitcast_u2f
:
752 assert(op
[0]->type
->base_type
== GLSL_TYPE_UINT
);
753 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
754 data
.f
[c
] = bitcast_u2f(op
[0]->value
.u
[c
]);
757 case ir_unop_bitcast_f2u
:
758 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
759 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
760 data
.u
[c
] = bitcast_f2u(op
[0]->value
.f
[c
]);
764 assert(op
[0]->type
->base_type
== GLSL_TYPE_DOUBLE
);
765 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
766 data
.f
[c
] = op
[0]->value
.d
[c
];
770 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
771 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
772 data
.d
[c
] = op
[0]->value
.f
[c
];
776 assert(op
[0]->type
->base_type
== GLSL_TYPE_DOUBLE
);
777 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
778 data
.i
[c
] = op
[0]->value
.d
[c
];
782 assert(op
[0]->type
->base_type
== GLSL_TYPE_INT
);
783 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
784 data
.d
[c
] = op
[0]->value
.i
[c
];
788 assert(op
[0]->type
->base_type
== GLSL_TYPE_DOUBLE
);
789 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
790 data
.u
[c
] = op
[0]->value
.d
[c
];
794 assert(op
[0]->type
->base_type
== GLSL_TYPE_UINT
);
795 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
796 data
.d
[c
] = op
[0]->value
.u
[c
];
800 assert(op
[0]->type
->base_type
== GLSL_TYPE_DOUBLE
);
801 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
802 data
.b
[c
] = op
[0]->value
.d
[c
] != 0.0;
806 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
807 if (op
[0]->type
->base_type
== GLSL_TYPE_DOUBLE
)
808 data
.d
[c
] = trunc(op
[0]->value
.d
[c
]);
810 data
.f
[c
] = truncf(op
[0]->value
.f
[c
]);
814 case ir_unop_round_even
:
815 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
816 if (op
[0]->type
->base_type
== GLSL_TYPE_DOUBLE
)
817 data
.d
[c
] = _mesa_roundeven(op
[0]->value
.d
[c
]);
819 data
.f
[c
] = _mesa_roundevenf(op
[0]->value
.f
[c
]);
824 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
825 if (op
[0]->type
->base_type
== GLSL_TYPE_DOUBLE
)
826 data
.d
[c
] = ceil(op
[0]->value
.d
[c
]);
828 data
.f
[c
] = ceilf(op
[0]->value
.f
[c
]);
833 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
834 if (op
[0]->type
->base_type
== GLSL_TYPE_DOUBLE
)
835 data
.d
[c
] = floor(op
[0]->value
.d
[c
]);
837 data
.f
[c
] = floorf(op
[0]->value
.f
[c
]);
842 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
843 switch (this->type
->base_type
) {
844 case GLSL_TYPE_FLOAT
:
845 data
.f
[c
] = op
[0]->value
.f
[c
] - floor(op
[0]->value
.f
[c
]);
847 case GLSL_TYPE_DOUBLE
:
848 data
.d
[c
] = op
[0]->value
.d
[c
] - floor(op
[0]->value
.d
[c
]);
857 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
858 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
859 data
.f
[c
] = sinf(op
[0]->value
.f
[c
]);
864 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
865 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
866 data
.f
[c
] = cosf(op
[0]->value
.f
[c
]);
871 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
872 switch (this->type
->base_type
) {
874 data
.u
[c
] = -((int) op
[0]->value
.u
[c
]);
877 data
.i
[c
] = -op
[0]->value
.i
[c
];
879 case GLSL_TYPE_FLOAT
:
880 data
.f
[c
] = -op
[0]->value
.f
[c
];
882 case GLSL_TYPE_DOUBLE
:
883 data
.d
[c
] = -op
[0]->value
.d
[c
];
892 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
893 switch (this->type
->base_type
) {
895 data
.i
[c
] = op
[0]->value
.i
[c
];
897 data
.i
[c
] = -data
.i
[c
];
899 case GLSL_TYPE_FLOAT
:
900 data
.f
[c
] = fabs(op
[0]->value
.f
[c
]);
902 case GLSL_TYPE_DOUBLE
:
903 data
.d
[c
] = fabs(op
[0]->value
.d
[c
]);
912 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
913 switch (this->type
->base_type
) {
915 data
.i
[c
] = (op
[0]->value
.i
[c
] > 0) - (op
[0]->value
.i
[c
] < 0);
917 case GLSL_TYPE_FLOAT
:
918 data
.f
[c
] = float((op
[0]->value
.f
[c
] > 0)-(op
[0]->value
.f
[c
] < 0));
920 case GLSL_TYPE_DOUBLE
:
921 data
.d
[c
] = double((op
[0]->value
.d
[c
] > 0)-(op
[0]->value
.d
[c
] < 0));
930 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
931 switch (this->type
->base_type
) {
932 case GLSL_TYPE_FLOAT
:
933 if (op
[0]->value
.f
[c
] != 0.0)
934 data
.f
[c
] = 1.0F
/ op
[0]->value
.f
[c
];
936 case GLSL_TYPE_DOUBLE
:
937 if (op
[0]->value
.d
[c
] != 0.0)
938 data
.d
[c
] = 1.0 / op
[0]->value
.d
[c
];
947 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
948 if (op
[0]->type
->base_type
== GLSL_TYPE_DOUBLE
)
949 data
.d
[c
] = 1.0 / sqrt(op
[0]->value
.d
[c
]);
951 data
.f
[c
] = 1.0F
/ sqrtf(op
[0]->value
.f
[c
]);
956 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
957 if (op
[0]->type
->base_type
== GLSL_TYPE_DOUBLE
)
958 data
.d
[c
] = sqrt(op
[0]->value
.d
[c
]);
960 data
.f
[c
] = sqrtf(op
[0]->value
.f
[c
]);
965 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
966 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
967 data
.f
[c
] = expf(op
[0]->value
.f
[c
]);
972 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
973 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
974 data
.f
[c
] = exp2f(op
[0]->value
.f
[c
]);
979 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
980 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
981 data
.f
[c
] = logf(op
[0]->value
.f
[c
]);
986 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
987 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
988 data
.f
[c
] = log2f(op
[0]->value
.f
[c
]);
993 case ir_unop_dFdx_coarse
:
994 case ir_unop_dFdx_fine
:
996 case ir_unop_dFdy_coarse
:
997 case ir_unop_dFdy_fine
:
998 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
999 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
1004 case ir_unop_pack_snorm_2x16
:
1005 assert(op
[0]->type
== glsl_type::vec2_type
);
1006 data
.u
[0] = pack_2x16(pack_snorm_1x16
,
1010 case ir_unop_pack_snorm_4x8
:
1011 assert(op
[0]->type
== glsl_type::vec4_type
);
1012 data
.u
[0] = pack_4x8(pack_snorm_1x8
,
1018 case ir_unop_unpack_snorm_2x16
:
1019 assert(op
[0]->type
== glsl_type::uint_type
);
1020 unpack_2x16(unpack_snorm_1x16
,
1022 &data
.f
[0], &data
.f
[1]);
1024 case ir_unop_unpack_snorm_4x8
:
1025 assert(op
[0]->type
== glsl_type::uint_type
);
1026 unpack_4x8(unpack_snorm_1x8
,
1028 &data
.f
[0], &data
.f
[1], &data
.f
[2], &data
.f
[3]);
1030 case ir_unop_pack_unorm_2x16
:
1031 assert(op
[0]->type
== glsl_type::vec2_type
);
1032 data
.u
[0] = pack_2x16(pack_unorm_1x16
,
1036 case ir_unop_pack_unorm_4x8
:
1037 assert(op
[0]->type
== glsl_type::vec4_type
);
1038 data
.u
[0] = pack_4x8(pack_unorm_1x8
,
1044 case ir_unop_unpack_unorm_2x16
:
1045 assert(op
[0]->type
== glsl_type::uint_type
);
1046 unpack_2x16(unpack_unorm_1x16
,
1048 &data
.f
[0], &data
.f
[1]);
1050 case ir_unop_unpack_unorm_4x8
:
1051 assert(op
[0]->type
== glsl_type::uint_type
);
1052 unpack_4x8(unpack_unorm_1x8
,
1054 &data
.f
[0], &data
.f
[1], &data
.f
[2], &data
.f
[3]);
1056 case ir_unop_pack_half_2x16
:
1057 assert(op
[0]->type
== glsl_type::vec2_type
);
1058 data
.u
[0] = pack_2x16(pack_half_1x16
,
1062 case ir_unop_unpack_half_2x16
:
1063 assert(op
[0]->type
== glsl_type::uint_type
);
1064 unpack_2x16(unpack_half_1x16
,
1066 &data
.f
[0], &data
.f
[1]);
1069 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
1070 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
1071 data
.f
[c
] = powf(op
[0]->value
.f
[c
], op
[1]->value
.f
[c
]);
1076 if (op
[0]->type
->base_type
== GLSL_TYPE_DOUBLE
)
1077 data
.d
[0] = dot_d(op
[0], op
[1]);
1079 data
.f
[0] = dot_f(op
[0], op
[1]);
1083 assert(op
[0]->type
== op
[1]->type
|| op0_scalar
|| op1_scalar
);
1084 for (unsigned c
= 0, c0
= 0, c1
= 0;
1086 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
1088 switch (op
[0]->type
->base_type
) {
1089 case GLSL_TYPE_UINT
:
1090 data
.u
[c
] = MIN2(op
[0]->value
.u
[c0
], op
[1]->value
.u
[c1
]);
1093 data
.i
[c
] = MIN2(op
[0]->value
.i
[c0
], op
[1]->value
.i
[c1
]);
1095 case GLSL_TYPE_FLOAT
:
1096 data
.f
[c
] = MIN2(op
[0]->value
.f
[c0
], op
[1]->value
.f
[c1
]);
1098 case GLSL_TYPE_DOUBLE
:
1099 data
.d
[c
] = MIN2(op
[0]->value
.d
[c0
], op
[1]->value
.d
[c1
]);
1108 assert(op
[0]->type
== op
[1]->type
|| op0_scalar
|| op1_scalar
);
1109 for (unsigned c
= 0, c0
= 0, c1
= 0;
1111 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
1113 switch (op
[0]->type
->base_type
) {
1114 case GLSL_TYPE_UINT
:
1115 data
.u
[c
] = MAX2(op
[0]->value
.u
[c0
], op
[1]->value
.u
[c1
]);
1118 data
.i
[c
] = MAX2(op
[0]->value
.i
[c0
], op
[1]->value
.i
[c1
]);
1120 case GLSL_TYPE_FLOAT
:
1121 data
.f
[c
] = MAX2(op
[0]->value
.f
[c0
], op
[1]->value
.f
[c1
]);
1123 case GLSL_TYPE_DOUBLE
:
1124 data
.d
[c
] = MAX2(op
[0]->value
.d
[c0
], op
[1]->value
.d
[c1
]);
1133 assert(op
[0]->type
== op
[1]->type
|| op0_scalar
|| op1_scalar
);
1134 for (unsigned c
= 0, c0
= 0, c1
= 0;
1136 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
1138 switch (op
[0]->type
->base_type
) {
1139 case GLSL_TYPE_UINT
:
1140 data
.u
[c
] = op
[0]->value
.u
[c0
] + op
[1]->value
.u
[c1
];
1143 data
.i
[c
] = op
[0]->value
.i
[c0
] + op
[1]->value
.i
[c1
];
1145 case GLSL_TYPE_FLOAT
:
1146 data
.f
[c
] = op
[0]->value
.f
[c0
] + op
[1]->value
.f
[c1
];
1148 case GLSL_TYPE_DOUBLE
:
1149 data
.d
[c
] = op
[0]->value
.d
[c0
] + op
[1]->value
.d
[c1
];
1158 assert(op
[0]->type
== op
[1]->type
|| op0_scalar
|| op1_scalar
);
1159 for (unsigned c
= 0, c0
= 0, c1
= 0;
1161 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
1163 switch (op
[0]->type
->base_type
) {
1164 case GLSL_TYPE_UINT
:
1165 data
.u
[c
] = op
[0]->value
.u
[c0
] - op
[1]->value
.u
[c1
];
1168 data
.i
[c
] = op
[0]->value
.i
[c0
] - op
[1]->value
.i
[c1
];
1170 case GLSL_TYPE_FLOAT
:
1171 data
.f
[c
] = op
[0]->value
.f
[c0
] - op
[1]->value
.f
[c1
];
1173 case GLSL_TYPE_DOUBLE
:
1174 data
.d
[c
] = op
[0]->value
.d
[c0
] - op
[1]->value
.d
[c1
];
1183 /* Check for equal types, or unequal types involving scalars */
1184 if ((op
[0]->type
== op
[1]->type
&& !op
[0]->type
->is_matrix())
1185 || op0_scalar
|| op1_scalar
) {
1186 for (unsigned c
= 0, c0
= 0, c1
= 0;
1188 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
1190 switch (op
[0]->type
->base_type
) {
1191 case GLSL_TYPE_UINT
:
1192 data
.u
[c
] = op
[0]->value
.u
[c0
] * op
[1]->value
.u
[c1
];
1195 data
.i
[c
] = op
[0]->value
.i
[c0
] * op
[1]->value
.i
[c1
];
1197 case GLSL_TYPE_FLOAT
:
1198 data
.f
[c
] = op
[0]->value
.f
[c0
] * op
[1]->value
.f
[c1
];
1200 case GLSL_TYPE_DOUBLE
:
1201 data
.d
[c
] = op
[0]->value
.d
[c0
] * op
[1]->value
.d
[c1
];
1208 assert(op
[0]->type
->is_matrix() || op
[1]->type
->is_matrix());
1210 /* Multiply an N-by-M matrix with an M-by-P matrix. Since either
1211 * matrix can be a GLSL vector, either N or P can be 1.
1213 * For vec*mat, the vector is treated as a row vector. This
1214 * means the vector is a 1-row x M-column matrix.
1216 * For mat*vec, the vector is treated as a column vector. Since
1217 * matrix_columns is 1 for vectors, this just works.
1219 const unsigned n
= op
[0]->type
->is_vector()
1220 ? 1 : op
[0]->type
->vector_elements
;
1221 const unsigned m
= op
[1]->type
->vector_elements
;
1222 const unsigned p
= op
[1]->type
->matrix_columns
;
1223 for (unsigned j
= 0; j
< p
; j
++) {
1224 for (unsigned i
= 0; i
< n
; i
++) {
1225 for (unsigned k
= 0; k
< m
; k
++) {
1226 if (op
[0]->type
->base_type
== GLSL_TYPE_DOUBLE
)
1227 data
.d
[i
+n
*j
] += op
[0]->value
.d
[i
+n
*k
]*op
[1]->value
.d
[k
+m
*j
];
1229 data
.f
[i
+n
*j
] += op
[0]->value
.f
[i
+n
*k
]*op
[1]->value
.f
[k
+m
*j
];
1237 /* FINISHME: Emit warning when division-by-zero is detected. */
1238 assert(op
[0]->type
== op
[1]->type
|| op0_scalar
|| op1_scalar
);
1239 for (unsigned c
= 0, c0
= 0, c1
= 0;
1241 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
1243 switch (op
[0]->type
->base_type
) {
1244 case GLSL_TYPE_UINT
:
1245 if (op
[1]->value
.u
[c1
] == 0) {
1248 data
.u
[c
] = op
[0]->value
.u
[c0
] / op
[1]->value
.u
[c1
];
1252 if (op
[1]->value
.i
[c1
] == 0) {
1255 data
.i
[c
] = op
[0]->value
.i
[c0
] / op
[1]->value
.i
[c1
];
1258 case GLSL_TYPE_FLOAT
:
1259 data
.f
[c
] = op
[0]->value
.f
[c0
] / op
[1]->value
.f
[c1
];
1261 case GLSL_TYPE_DOUBLE
:
1262 data
.d
[c
] = op
[0]->value
.d
[c0
] / op
[1]->value
.d
[c1
];
1271 /* FINISHME: Emit warning when division-by-zero is detected. */
1272 assert(op
[0]->type
== op
[1]->type
|| op0_scalar
|| op1_scalar
);
1273 for (unsigned c
= 0, c0
= 0, c1
= 0;
1275 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
1277 switch (op
[0]->type
->base_type
) {
1278 case GLSL_TYPE_UINT
:
1279 if (op
[1]->value
.u
[c1
] == 0) {
1282 data
.u
[c
] = op
[0]->value
.u
[c0
] % op
[1]->value
.u
[c1
];
1286 if (op
[1]->value
.i
[c1
] == 0) {
1289 data
.i
[c
] = op
[0]->value
.i
[c0
] % op
[1]->value
.i
[c1
];
1292 case GLSL_TYPE_FLOAT
:
1293 /* We don't use fmod because it rounds toward zero; GLSL specifies
1296 data
.f
[c
] = op
[0]->value
.f
[c0
] - op
[1]->value
.f
[c1
]
1297 * floorf(op
[0]->value
.f
[c0
] / op
[1]->value
.f
[c1
]);
1299 case GLSL_TYPE_DOUBLE
:
1300 /* We don't use fmod because it rounds toward zero; GLSL specifies
1303 data
.d
[c
] = op
[0]->value
.d
[c0
] - op
[1]->value
.d
[c1
]
1304 * floor(op
[0]->value
.d
[c0
] / op
[1]->value
.d
[c1
]);
1313 case ir_binop_logic_and
:
1314 assert(op
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
1315 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++)
1316 data
.b
[c
] = op
[0]->value
.b
[c
] && op
[1]->value
.b
[c
];
1318 case ir_binop_logic_xor
:
1319 assert(op
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
1320 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++)
1321 data
.b
[c
] = op
[0]->value
.b
[c
] ^ op
[1]->value
.b
[c
];
1323 case ir_binop_logic_or
:
1324 assert(op
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
1325 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++)
1326 data
.b
[c
] = op
[0]->value
.b
[c
] || op
[1]->value
.b
[c
];
1330 assert(op
[0]->type
== op
[1]->type
);
1331 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
1332 switch (op
[0]->type
->base_type
) {
1333 case GLSL_TYPE_UINT
:
1334 data
.b
[c
] = op
[0]->value
.u
[c
] < op
[1]->value
.u
[c
];
1337 data
.b
[c
] = op
[0]->value
.i
[c
] < op
[1]->value
.i
[c
];
1339 case GLSL_TYPE_FLOAT
:
1340 data
.b
[c
] = op
[0]->value
.f
[c
] < op
[1]->value
.f
[c
];
1342 case GLSL_TYPE_DOUBLE
:
1343 data
.b
[c
] = op
[0]->value
.d
[c
] < op
[1]->value
.d
[c
];
1350 case ir_binop_greater
:
1351 assert(op
[0]->type
== op
[1]->type
);
1352 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
1353 switch (op
[0]->type
->base_type
) {
1354 case GLSL_TYPE_UINT
:
1355 data
.b
[c
] = op
[0]->value
.u
[c
] > op
[1]->value
.u
[c
];
1358 data
.b
[c
] = op
[0]->value
.i
[c
] > op
[1]->value
.i
[c
];
1360 case GLSL_TYPE_FLOAT
:
1361 data
.b
[c
] = op
[0]->value
.f
[c
] > op
[1]->value
.f
[c
];
1363 case GLSL_TYPE_DOUBLE
:
1364 data
.b
[c
] = op
[0]->value
.d
[c
] > op
[1]->value
.d
[c
];
1371 case ir_binop_lequal
:
1372 assert(op
[0]->type
== op
[1]->type
);
1373 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
1374 switch (op
[0]->type
->base_type
) {
1375 case GLSL_TYPE_UINT
:
1376 data
.b
[c
] = op
[0]->value
.u
[c
] <= op
[1]->value
.u
[c
];
1379 data
.b
[c
] = op
[0]->value
.i
[c
] <= op
[1]->value
.i
[c
];
1381 case GLSL_TYPE_FLOAT
:
1382 data
.b
[c
] = op
[0]->value
.f
[c
] <= op
[1]->value
.f
[c
];
1384 case GLSL_TYPE_DOUBLE
:
1385 data
.b
[c
] = op
[0]->value
.d
[c
] <= op
[1]->value
.d
[c
];
1392 case ir_binop_gequal
:
1393 assert(op
[0]->type
== op
[1]->type
);
1394 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
1395 switch (op
[0]->type
->base_type
) {
1396 case GLSL_TYPE_UINT
:
1397 data
.b
[c
] = op
[0]->value
.u
[c
] >= op
[1]->value
.u
[c
];
1400 data
.b
[c
] = op
[0]->value
.i
[c
] >= op
[1]->value
.i
[c
];
1402 case GLSL_TYPE_FLOAT
:
1403 data
.b
[c
] = op
[0]->value
.f
[c
] >= op
[1]->value
.f
[c
];
1405 case GLSL_TYPE_DOUBLE
:
1406 data
.b
[c
] = op
[0]->value
.d
[c
] >= op
[1]->value
.d
[c
];
1413 case ir_binop_equal
:
1414 assert(op
[0]->type
== op
[1]->type
);
1415 for (unsigned c
= 0; c
< components
; c
++) {
1416 switch (op
[0]->type
->base_type
) {
1417 case GLSL_TYPE_UINT
:
1418 data
.b
[c
] = op
[0]->value
.u
[c
] == op
[1]->value
.u
[c
];
1421 data
.b
[c
] = op
[0]->value
.i
[c
] == op
[1]->value
.i
[c
];
1423 case GLSL_TYPE_FLOAT
:
1424 data
.b
[c
] = op
[0]->value
.f
[c
] == op
[1]->value
.f
[c
];
1426 case GLSL_TYPE_BOOL
:
1427 data
.b
[c
] = op
[0]->value
.b
[c
] == op
[1]->value
.b
[c
];
1429 case GLSL_TYPE_DOUBLE
:
1430 data
.b
[c
] = op
[0]->value
.d
[c
] == op
[1]->value
.d
[c
];
1437 case ir_binop_nequal
:
1438 assert(op
[0]->type
== op
[1]->type
);
1439 for (unsigned c
= 0; c
< components
; c
++) {
1440 switch (op
[0]->type
->base_type
) {
1441 case GLSL_TYPE_UINT
:
1442 data
.b
[c
] = op
[0]->value
.u
[c
] != op
[1]->value
.u
[c
];
1445 data
.b
[c
] = op
[0]->value
.i
[c
] != op
[1]->value
.i
[c
];
1447 case GLSL_TYPE_FLOAT
:
1448 data
.b
[c
] = op
[0]->value
.f
[c
] != op
[1]->value
.f
[c
];
1450 case GLSL_TYPE_BOOL
:
1451 data
.b
[c
] = op
[0]->value
.b
[c
] != op
[1]->value
.b
[c
];
1453 case GLSL_TYPE_DOUBLE
:
1454 data
.b
[c
] = op
[0]->value
.d
[c
] != op
[1]->value
.d
[c
];
1461 case ir_binop_all_equal
:
1462 data
.b
[0] = op
[0]->has_value(op
[1]);
1464 case ir_binop_any_nequal
:
1465 data
.b
[0] = !op
[0]->has_value(op
[1]);
1468 case ir_binop_lshift
:
1469 for (unsigned c
= 0, c0
= 0, c1
= 0;
1471 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
1473 if (op
[0]->type
->base_type
== GLSL_TYPE_INT
&&
1474 op
[1]->type
->base_type
== GLSL_TYPE_INT
) {
1475 data
.i
[c
] = op
[0]->value
.i
[c0
] << op
[1]->value
.i
[c1
];
1477 } else if (op
[0]->type
->base_type
== GLSL_TYPE_INT
&&
1478 op
[1]->type
->base_type
== GLSL_TYPE_UINT
) {
1479 data
.i
[c
] = op
[0]->value
.i
[c0
] << op
[1]->value
.u
[c1
];
1481 } else if (op
[0]->type
->base_type
== GLSL_TYPE_UINT
&&
1482 op
[1]->type
->base_type
== GLSL_TYPE_INT
) {
1483 data
.u
[c
] = op
[0]->value
.u
[c0
] << op
[1]->value
.i
[c1
];
1485 } else if (op
[0]->type
->base_type
== GLSL_TYPE_UINT
&&
1486 op
[1]->type
->base_type
== GLSL_TYPE_UINT
) {
1487 data
.u
[c
] = op
[0]->value
.u
[c0
] << op
[1]->value
.u
[c1
];
1492 case ir_binop_rshift
:
1493 for (unsigned c
= 0, c0
= 0, c1
= 0;
1495 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
1497 if (op
[0]->type
->base_type
== GLSL_TYPE_INT
&&
1498 op
[1]->type
->base_type
== GLSL_TYPE_INT
) {
1499 data
.i
[c
] = op
[0]->value
.i
[c0
] >> op
[1]->value
.i
[c1
];
1501 } else if (op
[0]->type
->base_type
== GLSL_TYPE_INT
&&
1502 op
[1]->type
->base_type
== GLSL_TYPE_UINT
) {
1503 data
.i
[c
] = op
[0]->value
.i
[c0
] >> op
[1]->value
.u
[c1
];
1505 } else if (op
[0]->type
->base_type
== GLSL_TYPE_UINT
&&
1506 op
[1]->type
->base_type
== GLSL_TYPE_INT
) {
1507 data
.u
[c
] = op
[0]->value
.u
[c0
] >> op
[1]->value
.i
[c1
];
1509 } else if (op
[0]->type
->base_type
== GLSL_TYPE_UINT
&&
1510 op
[1]->type
->base_type
== GLSL_TYPE_UINT
) {
1511 data
.u
[c
] = op
[0]->value
.u
[c0
] >> op
[1]->value
.u
[c1
];
1516 case ir_binop_bit_and
:
1517 for (unsigned c
= 0, c0
= 0, c1
= 0;
1519 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
1521 switch (op
[0]->type
->base_type
) {
1523 data
.i
[c
] = op
[0]->value
.i
[c0
] & op
[1]->value
.i
[c1
];
1525 case GLSL_TYPE_UINT
:
1526 data
.u
[c
] = op
[0]->value
.u
[c0
] & op
[1]->value
.u
[c1
];
1534 case ir_binop_bit_or
:
1535 for (unsigned c
= 0, c0
= 0, c1
= 0;
1537 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
1539 switch (op
[0]->type
->base_type
) {
1541 data
.i
[c
] = op
[0]->value
.i
[c0
] | op
[1]->value
.i
[c1
];
1543 case GLSL_TYPE_UINT
:
1544 data
.u
[c
] = op
[0]->value
.u
[c0
] | op
[1]->value
.u
[c1
];
1552 case ir_binop_vector_extract
: {
1553 const int c
= CLAMP(op
[1]->value
.i
[0], 0,
1554 (int) op
[0]->type
->vector_elements
- 1);
1556 switch (op
[0]->type
->base_type
) {
1557 case GLSL_TYPE_UINT
:
1558 data
.u
[0] = op
[0]->value
.u
[c
];
1561 data
.i
[0] = op
[0]->value
.i
[c
];
1563 case GLSL_TYPE_FLOAT
:
1564 data
.f
[0] = op
[0]->value
.f
[c
];
1566 case GLSL_TYPE_DOUBLE
:
1567 data
.d
[0] = op
[0]->value
.d
[c
];
1569 case GLSL_TYPE_BOOL
:
1570 data
.b
[0] = op
[0]->value
.b
[c
];
1578 case ir_binop_bit_xor
:
1579 for (unsigned c
= 0, c0
= 0, c1
= 0;
1581 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
1583 switch (op
[0]->type
->base_type
) {
1585 data
.i
[c
] = op
[0]->value
.i
[c0
] ^ op
[1]->value
.i
[c1
];
1587 case GLSL_TYPE_UINT
:
1588 data
.u
[c
] = op
[0]->value
.u
[c0
] ^ op
[1]->value
.u
[c1
];
1596 case ir_unop_bitfield_reverse
:
1597 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
1598 switch (this->type
->base_type
) {
1599 case GLSL_TYPE_UINT
:
1600 data
.u
[c
] = bitfield_reverse(op
[0]->value
.u
[c
]);
1603 data
.i
[c
] = bitfield_reverse(op
[0]->value
.i
[c
]);
1611 case ir_unop_bit_count
:
1612 for (unsigned c
= 0; c
< components
; c
++)
1613 data
.i
[c
] = _mesa_bitcount(op
[0]->value
.u
[c
]);
1616 case ir_unop_find_msb
:
1617 for (unsigned c
= 0; c
< components
; c
++) {
1618 switch (op
[0]->type
->base_type
) {
1619 case GLSL_TYPE_UINT
:
1620 data
.i
[c
] = find_msb_uint(op
[0]->value
.u
[c
]);
1623 data
.i
[c
] = find_msb_int(op
[0]->value
.i
[c
]);
1631 case ir_unop_find_lsb
:
1632 for (unsigned c
= 0; c
< components
; c
++) {
1633 switch (op
[0]->type
->base_type
) {
1634 case GLSL_TYPE_UINT
:
1635 data
.i
[c
] = find_msb_uint(op
[0]->value
.u
[c
] & -op
[0]->value
.u
[c
]);
1638 data
.i
[c
] = find_msb_uint(op
[0]->value
.i
[c
] & -op
[0]->value
.i
[c
]);
1646 case ir_unop_saturate
:
1647 for (unsigned c
= 0; c
< components
; c
++) {
1648 data
.f
[c
] = CLAMP(op
[0]->value
.f
[c
], 0.0f
, 1.0f
);
1651 case ir_unop_pack_double_2x32
:
1652 /* XXX needs to be checked on big-endian */
1653 memcpy(&data
.d
[0], &op
[0]->value
.u
[0], sizeof(double));
1655 case ir_unop_unpack_double_2x32
:
1656 /* XXX needs to be checked on big-endian */
1657 memcpy(&data
.u
[0], &op
[0]->value
.d
[0], sizeof(double));
1660 case ir_triop_bitfield_extract
:
1661 for (unsigned c
= 0; c
< components
; c
++) {
1662 switch (this->type
->base_type
) {
1663 case GLSL_TYPE_UINT
:
1664 data
.u
[c
] = bitfield_extract_uint(op
[0]->value
.u
[c
], op
[1]->value
.i
[c
], op
[2]->value
.i
[c
]);
1667 data
.i
[c
] = bitfield_extract_int(op
[0]->value
.i
[c
], op
[1]->value
.i
[c
], op
[2]->value
.i
[c
]);
1675 case ir_binop_ldexp
:
1676 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
1677 switch (this->type
->base_type
) {
1678 case GLSL_TYPE_FLOAT
:
1679 data
.f
[c
] = ldexpf_flush_subnormal(op
[0]->value
.f
[c
], op
[1]->value
.i
[c
]);
1681 case GLSL_TYPE_DOUBLE
:
1682 data
.d
[c
] = ldexp_flush_subnormal(op
[0]->value
.d
[c
], op
[1]->value
.i
[c
]);
1691 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
||
1692 op
[0]->type
->base_type
== GLSL_TYPE_DOUBLE
);
1693 assert(op
[1]->type
->base_type
== GLSL_TYPE_FLOAT
||
1694 op
[1]->type
->base_type
== GLSL_TYPE_DOUBLE
);
1695 assert(op
[2]->type
->base_type
== GLSL_TYPE_FLOAT
||
1696 op
[2]->type
->base_type
== GLSL_TYPE_DOUBLE
);
1698 for (unsigned c
= 0; c
< components
; c
++) {
1699 if (op
[0]->type
->base_type
== GLSL_TYPE_DOUBLE
)
1700 data
.d
[c
] = op
[0]->value
.d
[c
] * op
[1]->value
.d
[c
]
1701 + op
[2]->value
.d
[c
];
1703 data
.f
[c
] = op
[0]->value
.f
[c
] * op
[1]->value
.f
[c
]
1704 + op
[2]->value
.f
[c
];
1708 case ir_triop_lrp
: {
1709 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
||
1710 op
[0]->type
->base_type
== GLSL_TYPE_DOUBLE
);
1711 assert(op
[1]->type
->base_type
== GLSL_TYPE_FLOAT
||
1712 op
[1]->type
->base_type
== GLSL_TYPE_DOUBLE
);
1713 assert(op
[2]->type
->base_type
== GLSL_TYPE_FLOAT
||
1714 op
[2]->type
->base_type
== GLSL_TYPE_DOUBLE
);
1716 unsigned c2_inc
= op
[2]->type
->is_scalar() ? 0 : 1;
1717 for (unsigned c
= 0, c2
= 0; c
< components
; c2
+= c2_inc
, c
++) {
1718 if (op
[0]->type
->base_type
== GLSL_TYPE_DOUBLE
)
1719 data
.d
[c
] = op
[0]->value
.d
[c
] * (1.0 - op
[2]->value
.d
[c2
]) +
1720 (op
[1]->value
.d
[c
] * op
[2]->value
.d
[c2
]);
1722 data
.f
[c
] = op
[0]->value
.f
[c
] * (1.0f
- op
[2]->value
.f
[c2
]) +
1723 (op
[1]->value
.f
[c
] * op
[2]->value
.f
[c2
]);
1729 for (unsigned c
= 0; c
< components
; c
++) {
1730 if (op
[1]->type
->base_type
== GLSL_TYPE_DOUBLE
)
1731 data
.d
[c
] = op
[0]->value
.b
[c
] ? op
[1]->value
.d
[c
]
1732 : op
[2]->value
.d
[c
];
1734 data
.u
[c
] = op
[0]->value
.b
[c
] ? op
[1]->value
.u
[c
]
1735 : op
[2]->value
.u
[c
];
1739 case ir_triop_vector_insert
: {
1740 const unsigned idx
= op
[2]->value
.u
[0];
1742 memcpy(&data
, &op
[0]->value
, sizeof(data
));
1744 switch (this->type
->base_type
) {
1746 data
.i
[idx
] = op
[1]->value
.i
[0];
1748 case GLSL_TYPE_UINT
:
1749 data
.u
[idx
] = op
[1]->value
.u
[0];
1751 case GLSL_TYPE_FLOAT
:
1752 data
.f
[idx
] = op
[1]->value
.f
[0];
1754 case GLSL_TYPE_BOOL
:
1755 data
.b
[idx
] = op
[1]->value
.b
[0];
1757 case GLSL_TYPE_DOUBLE
:
1758 data
.d
[idx
] = op
[1]->value
.d
[0];
1761 assert(!"Should not get here.");
1767 case ir_quadop_bitfield_insert
:
1768 for (unsigned c
= 0; c
< components
; c
++)
1769 data
.u
[c
] = bitfield_insert(op
[0]->value
.u
[c
], op
[1]->value
.u
[c
], op
[2]->value
.i
[c
], op
[3]->value
.i
[c
]);
1772 case ir_quadop_vector
:
1773 for (unsigned c
= 0; c
< this->type
->vector_elements
; c
++) {
1774 switch (this->type
->base_type
) {
1776 data
.i
[c
] = op
[c
]->value
.i
[0];
1778 case GLSL_TYPE_UINT
:
1779 data
.u
[c
] = op
[c
]->value
.u
[0];
1781 case GLSL_TYPE_FLOAT
:
1782 data
.f
[c
] = op
[c
]->value
.f
[0];
1784 case GLSL_TYPE_DOUBLE
:
1785 data
.d
[c
] = op
[c
]->value
.d
[0];
1787 case GLSL_TYPE_BOOL
:
1788 data
.b
[c
] = op
[c
]->value
.b
[0];
1797 /* FINISHME: Should handle all expression types. */
1801 return new(ctx
) ir_constant(this->type
, &data
);
1806 ir_texture::constant_expression_value(struct hash_table
*)
1808 /* texture lookups aren't constant expressions */
1814 ir_swizzle::constant_expression_value(struct hash_table
*variable_context
)
1816 ir_constant
*v
= this->val
->constant_expression_value(variable_context
);
1819 ir_constant_data data
= { { 0 } };
1821 const unsigned swiz_idx
[4] = {
1822 this->mask
.x
, this->mask
.y
, this->mask
.z
, this->mask
.w
1825 for (unsigned i
= 0; i
< this->mask
.num_components
; i
++) {
1826 switch (v
->type
->base_type
) {
1827 case GLSL_TYPE_UINT
:
1828 case GLSL_TYPE_INT
: data
.u
[i
] = v
->value
.u
[swiz_idx
[i
]]; break;
1829 case GLSL_TYPE_FLOAT
: data
.f
[i
] = v
->value
.f
[swiz_idx
[i
]]; break;
1830 case GLSL_TYPE_BOOL
: data
.b
[i
] = v
->value
.b
[swiz_idx
[i
]]; break;
1831 case GLSL_TYPE_DOUBLE
:data
.d
[i
] = v
->value
.d
[swiz_idx
[i
]]; break;
1832 default: assert(!"Should not get here."); break;
1836 void *ctx
= ralloc_parent(this);
1837 return new(ctx
) ir_constant(this->type
, &data
);
1844 ir_dereference_variable::constant_expression_value(struct hash_table
*variable_context
)
1848 /* Give priority to the context hashtable, if it exists */
1849 if (variable_context
) {
1850 ir_constant
*value
= (ir_constant
*)hash_table_find(variable_context
, var
);
1855 /* The constant_value of a uniform variable is its initializer,
1856 * not the lifetime constant value of the uniform.
1858 if (var
->data
.mode
== ir_var_uniform
)
1861 if (!var
->constant_value
)
1864 return var
->constant_value
->clone(ralloc_parent(var
), NULL
);
1869 ir_dereference_array::constant_expression_value(struct hash_table
*variable_context
)
1871 ir_constant
*array
= this->array
->constant_expression_value(variable_context
);
1872 ir_constant
*idx
= this->array_index
->constant_expression_value(variable_context
);
1874 if ((array
!= NULL
) && (idx
!= NULL
)) {
1875 void *ctx
= ralloc_parent(this);
1876 if (array
->type
->is_matrix()) {
1877 /* Array access of a matrix results in a vector.
1879 const unsigned column
= idx
->value
.u
[0];
1881 const glsl_type
*const column_type
= array
->type
->column_type();
1883 /* Offset in the constant matrix to the first element of the column
1886 const unsigned mat_idx
= column
* column_type
->vector_elements
;
1888 ir_constant_data data
= { { 0 } };
1890 switch (column_type
->base_type
) {
1891 case GLSL_TYPE_UINT
:
1893 for (unsigned i
= 0; i
< column_type
->vector_elements
; i
++)
1894 data
.u
[i
] = array
->value
.u
[mat_idx
+ i
];
1898 case GLSL_TYPE_FLOAT
:
1899 for (unsigned i
= 0; i
< column_type
->vector_elements
; i
++)
1900 data
.f
[i
] = array
->value
.f
[mat_idx
+ i
];
1904 case GLSL_TYPE_DOUBLE
:
1905 for (unsigned i
= 0; i
< column_type
->vector_elements
; i
++)
1906 data
.d
[i
] = array
->value
.d
[mat_idx
+ i
];
1911 assert(!"Should not get here.");
1915 return new(ctx
) ir_constant(column_type
, &data
);
1916 } else if (array
->type
->is_vector()) {
1917 const unsigned component
= idx
->value
.u
[0];
1919 return new(ctx
) ir_constant(array
, component
);
1921 const unsigned index
= idx
->value
.u
[0];
1922 return array
->get_array_element(index
)->clone(ctx
, NULL
);
1930 ir_dereference_record::constant_expression_value(struct hash_table
*)
1932 ir_constant
*v
= this->record
->constant_expression_value();
1934 return (v
!= NULL
) ? v
->get_record_field(this->field
) : NULL
;
1939 ir_assignment::constant_expression_value(struct hash_table
*)
1941 /* FINISHME: Handle CEs involving assignment (return RHS) */
1947 ir_constant::constant_expression_value(struct hash_table
*)
1954 ir_call::constant_expression_value(struct hash_table
*variable_context
)
1956 return this->callee
->constant_expression_value(&this->actual_parameters
, variable_context
);
1960 bool ir_function_signature::constant_expression_evaluate_expression_list(const struct exec_list
&body
,
1961 struct hash_table
*variable_context
,
1962 ir_constant
**result
)
1964 foreach_in_list(ir_instruction
, inst
, &body
) {
1965 switch(inst
->ir_type
) {
1967 /* (declare () type symbol) */
1968 case ir_type_variable
: {
1969 ir_variable
*var
= inst
->as_variable();
1970 hash_table_insert(variable_context
, ir_constant::zero(this, var
->type
), var
);
1974 /* (assign [condition] (write-mask) (ref) (value)) */
1975 case ir_type_assignment
: {
1976 ir_assignment
*asg
= inst
->as_assignment();
1977 if (asg
->condition
) {
1978 ir_constant
*cond
= asg
->condition
->constant_expression_value(variable_context
);
1981 if (!cond
->get_bool_component(0))
1985 ir_constant
*store
= NULL
;
1988 if (!constant_referenced(asg
->lhs
, variable_context
, store
, offset
))
1991 ir_constant
*value
= asg
->rhs
->constant_expression_value(variable_context
);
1996 store
->copy_masked_offset(value
, offset
, asg
->write_mask
);
2000 /* (return (expression)) */
2001 case ir_type_return
:
2003 *result
= inst
->as_return()->value
->constant_expression_value(variable_context
);
2004 return *result
!= NULL
;
2006 /* (call name (ref) (params))*/
2007 case ir_type_call
: {
2008 ir_call
*call
= inst
->as_call();
2010 /* Just say no to void functions in constant expressions. We
2011 * don't need them at that point.
2014 if (!call
->return_deref
)
2017 ir_constant
*store
= NULL
;
2020 if (!constant_referenced(call
->return_deref
, variable_context
,
2024 ir_constant
*value
= call
->constant_expression_value(variable_context
);
2029 store
->copy_offset(value
, offset
);
2033 /* (if condition (then-instructions) (else-instructions)) */
2035 ir_if
*iif
= inst
->as_if();
2037 ir_constant
*cond
= iif
->condition
->constant_expression_value(variable_context
);
2038 if (!cond
|| !cond
->type
->is_boolean())
2041 exec_list
&branch
= cond
->get_bool_component(0) ? iif
->then_instructions
: iif
->else_instructions
;
2044 if (!constant_expression_evaluate_expression_list(branch
, variable_context
, result
))
2047 /* If there was a return in the branch chosen, drop out now. */
2054 /* Every other expression type, we drop out. */
2060 /* Reaching the end of the block is not an error condition */
2068 ir_function_signature::constant_expression_value(exec_list
*actual_parameters
, struct hash_table
*variable_context
)
2070 const glsl_type
*type
= this->return_type
;
2071 if (type
== glsl_type::void_type
)
2074 /* From the GLSL 1.20 spec, page 23:
2075 * "Function calls to user-defined functions (non-built-in functions)
2076 * cannot be used to form constant expressions."
2078 if (!this->is_builtin())
2082 * Of the builtin functions, only the texture lookups and the noise
2083 * ones must not be used in constant expressions. They all include
2084 * specific opcodes so they don't need to be special-cased at this
2088 /* Initialize the table of dereferencable names with the function
2089 * parameters. Verify their const-ness on the way.
2091 * We expect the correctness of the number of parameters to have
2092 * been checked earlier.
2094 hash_table
*deref_hash
= hash_table_ctor(8, hash_table_pointer_hash
,
2095 hash_table_pointer_compare
);
2097 /* If "origin" is non-NULL, then the function body is there. So we
2098 * have to use the variable objects from the object with the body,
2099 * but the parameter instanciation on the current object.
2101 const exec_node
*parameter_info
= origin
? origin
->parameters
.get_head_raw() : parameters
.get_head_raw();
2103 foreach_in_list(ir_rvalue
, n
, actual_parameters
) {
2104 ir_constant
*constant
= n
->constant_expression_value(variable_context
);
2105 if (constant
== NULL
) {
2106 hash_table_dtor(deref_hash
);
2111 ir_variable
*var
= (ir_variable
*)parameter_info
;
2112 hash_table_insert(deref_hash
, constant
, var
);
2114 parameter_info
= parameter_info
->next
;
2117 ir_constant
*result
= NULL
;
2119 /* Now run the builtin function until something non-constant
2120 * happens or we get the result.
2122 if (constant_expression_evaluate_expression_list(origin
? origin
->body
: body
, deref_hash
, &result
) && result
)
2123 result
= result
->clone(ralloc_parent(this), NULL
);
2125 hash_table_dtor(deref_hash
);