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 ir_expression::constant_expression_value(struct hash_table
*variable_context
)
525 if (this->type
->is_error())
528 ir_constant
*op
[ARRAY_SIZE(this->operands
)] = { NULL
, };
529 ir_constant_data data
;
531 memset(&data
, 0, sizeof(data
));
533 for (unsigned operand
= 0; operand
< this->get_num_operands(); operand
++) {
534 op
[operand
] = this->operands
[operand
]->constant_expression_value(variable_context
);
540 switch (this->operation
) {
541 case ir_binop_lshift
:
542 case ir_binop_rshift
:
544 case ir_binop_interpolate_at_offset
:
545 case ir_binop_interpolate_at_sample
:
546 case ir_binop_vector_extract
:
548 case ir_triop_bitfield_extract
:
552 assert(op
[0]->type
->base_type
== op
[1]->type
->base_type
);
556 bool op0_scalar
= op
[0]->type
->is_scalar();
557 bool op1_scalar
= op
[1] != NULL
&& op
[1]->type
->is_scalar();
559 /* When iterating over a vector or matrix's components, we want to increase
560 * the loop counter. However, for scalars, we want to stay at 0.
562 unsigned c0_inc
= op0_scalar
? 0 : 1;
563 unsigned c1_inc
= op1_scalar
? 0 : 1;
565 if (op1_scalar
|| !op
[1]) {
566 components
= op
[0]->type
->components();
568 components
= op
[1]->type
->components();
571 void *ctx
= ralloc_parent(this);
573 /* Handle array operations here, rather than below. */
574 if (op
[0]->type
->is_array()) {
575 assert(op
[1] != NULL
&& op
[1]->type
->is_array());
576 switch (this->operation
) {
577 case ir_binop_all_equal
:
578 return new(ctx
) ir_constant(op
[0]->has_value(op
[1]));
579 case ir_binop_any_nequal
:
580 return new(ctx
) ir_constant(!op
[0]->has_value(op
[1]));
587 switch (this->operation
) {
588 case ir_unop_bit_not
:
589 switch (op
[0]->type
->base_type
) {
591 for (unsigned c
= 0; c
< components
; c
++)
592 data
.i
[c
] = ~ op
[0]->value
.i
[c
];
595 for (unsigned c
= 0; c
< components
; c
++)
596 data
.u
[c
] = ~ op
[0]->value
.u
[c
];
603 case ir_unop_logic_not
:
604 assert(op
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
605 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++)
606 data
.b
[c
] = !op
[0]->value
.b
[c
];
610 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
611 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
612 data
.i
[c
] = (int) op
[0]->value
.f
[c
];
616 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
617 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
618 data
.u
[c
] = (unsigned) op
[0]->value
.f
[c
];
622 assert(op
[0]->type
->base_type
== GLSL_TYPE_INT
);
623 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
624 data
.f
[c
] = (float) op
[0]->value
.i
[c
];
628 assert(op
[0]->type
->base_type
== GLSL_TYPE_UINT
);
629 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
630 data
.f
[c
] = (float) op
[0]->value
.u
[c
];
634 assert(op
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
635 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
636 data
.f
[c
] = op
[0]->value
.b
[c
] ? 1.0F
: 0.0F
;
640 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
641 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
642 data
.b
[c
] = op
[0]->value
.f
[c
] != 0.0F
? true : false;
646 assert(op
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
647 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
648 data
.i
[c
] = op
[0]->value
.b
[c
] ? 1 : 0;
652 assert(op
[0]->type
->is_integer());
653 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
654 data
.b
[c
] = op
[0]->value
.u
[c
] ? true : false;
658 assert(op
[0]->type
->base_type
== GLSL_TYPE_UINT
);
659 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
660 data
.i
[c
] = op
[0]->value
.u
[c
];
664 assert(op
[0]->type
->base_type
== GLSL_TYPE_INT
);
665 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
666 data
.u
[c
] = op
[0]->value
.i
[c
];
669 case ir_unop_bitcast_i2f
:
670 assert(op
[0]->type
->base_type
== GLSL_TYPE_INT
);
671 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
672 data
.f
[c
] = bitcast_u2f(op
[0]->value
.i
[c
]);
675 case ir_unop_bitcast_f2i
:
676 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
677 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
678 data
.i
[c
] = bitcast_f2u(op
[0]->value
.f
[c
]);
681 case ir_unop_bitcast_u2f
:
682 assert(op
[0]->type
->base_type
== GLSL_TYPE_UINT
);
683 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
684 data
.f
[c
] = bitcast_u2f(op
[0]->value
.u
[c
]);
687 case ir_unop_bitcast_f2u
:
688 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
689 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
690 data
.u
[c
] = bitcast_f2u(op
[0]->value
.f
[c
]);
694 assert(op
[0]->type
->base_type
== GLSL_TYPE_DOUBLE
);
695 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
696 data
.f
[c
] = op
[0]->value
.d
[c
];
700 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
701 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
702 data
.d
[c
] = op
[0]->value
.f
[c
];
706 assert(op
[0]->type
->base_type
== GLSL_TYPE_DOUBLE
);
707 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
708 data
.i
[c
] = op
[0]->value
.d
[c
];
712 assert(op
[0]->type
->base_type
== GLSL_TYPE_INT
);
713 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
714 data
.d
[c
] = op
[0]->value
.i
[c
];
718 assert(op
[0]->type
->base_type
== GLSL_TYPE_DOUBLE
);
719 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
720 data
.u
[c
] = op
[0]->value
.d
[c
];
724 assert(op
[0]->type
->base_type
== GLSL_TYPE_UINT
);
725 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
726 data
.d
[c
] = op
[0]->value
.u
[c
];
730 assert(op
[0]->type
->base_type
== GLSL_TYPE_DOUBLE
);
731 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
732 data
.b
[c
] = op
[0]->value
.d
[c
] != 0.0;
736 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
737 if (op
[0]->type
->base_type
== GLSL_TYPE_DOUBLE
)
738 data
.d
[c
] = trunc(op
[0]->value
.d
[c
]);
740 data
.f
[c
] = truncf(op
[0]->value
.f
[c
]);
744 case ir_unop_round_even
:
745 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
746 if (op
[0]->type
->base_type
== GLSL_TYPE_DOUBLE
)
747 data
.d
[c
] = _mesa_roundeven(op
[0]->value
.d
[c
]);
749 data
.f
[c
] = _mesa_roundevenf(op
[0]->value
.f
[c
]);
754 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
755 if (op
[0]->type
->base_type
== GLSL_TYPE_DOUBLE
)
756 data
.d
[c
] = ceil(op
[0]->value
.d
[c
]);
758 data
.f
[c
] = ceilf(op
[0]->value
.f
[c
]);
763 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
764 if (op
[0]->type
->base_type
== GLSL_TYPE_DOUBLE
)
765 data
.d
[c
] = floor(op
[0]->value
.d
[c
]);
767 data
.f
[c
] = floorf(op
[0]->value
.f
[c
]);
772 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
773 switch (this->type
->base_type
) {
774 case GLSL_TYPE_FLOAT
:
775 data
.f
[c
] = op
[0]->value
.f
[c
] - floor(op
[0]->value
.f
[c
]);
777 case GLSL_TYPE_DOUBLE
:
778 data
.d
[c
] = op
[0]->value
.d
[c
] - floor(op
[0]->value
.d
[c
]);
787 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
788 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
789 data
.f
[c
] = sinf(op
[0]->value
.f
[c
]);
794 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
795 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
796 data
.f
[c
] = cosf(op
[0]->value
.f
[c
]);
801 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
802 switch (this->type
->base_type
) {
804 data
.u
[c
] = -((int) op
[0]->value
.u
[c
]);
807 data
.i
[c
] = -op
[0]->value
.i
[c
];
809 case GLSL_TYPE_FLOAT
:
810 data
.f
[c
] = -op
[0]->value
.f
[c
];
812 case GLSL_TYPE_DOUBLE
:
813 data
.d
[c
] = -op
[0]->value
.d
[c
];
822 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
823 switch (this->type
->base_type
) {
825 data
.i
[c
] = op
[0]->value
.i
[c
];
827 data
.i
[c
] = -data
.i
[c
];
829 case GLSL_TYPE_FLOAT
:
830 data
.f
[c
] = fabs(op
[0]->value
.f
[c
]);
832 case GLSL_TYPE_DOUBLE
:
833 data
.d
[c
] = fabs(op
[0]->value
.d
[c
]);
842 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
843 switch (this->type
->base_type
) {
845 data
.i
[c
] = (op
[0]->value
.i
[c
] > 0) - (op
[0]->value
.i
[c
] < 0);
847 case GLSL_TYPE_FLOAT
:
848 data
.f
[c
] = float((op
[0]->value
.f
[c
] > 0)-(op
[0]->value
.f
[c
] < 0));
850 case GLSL_TYPE_DOUBLE
:
851 data
.d
[c
] = double((op
[0]->value
.d
[c
] > 0)-(op
[0]->value
.d
[c
] < 0));
860 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
861 switch (this->type
->base_type
) {
862 case GLSL_TYPE_FLOAT
:
863 if (op
[0]->value
.f
[c
] != 0.0)
864 data
.f
[c
] = 1.0F
/ op
[0]->value
.f
[c
];
866 case GLSL_TYPE_DOUBLE
:
867 if (op
[0]->value
.d
[c
] != 0.0)
868 data
.d
[c
] = 1.0 / op
[0]->value
.d
[c
];
877 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
878 if (op
[0]->type
->base_type
== GLSL_TYPE_DOUBLE
)
879 data
.d
[c
] = 1.0 / sqrt(op
[0]->value
.d
[c
]);
881 data
.f
[c
] = 1.0F
/ sqrtf(op
[0]->value
.f
[c
]);
886 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
887 if (op
[0]->type
->base_type
== GLSL_TYPE_DOUBLE
)
888 data
.d
[c
] = sqrt(op
[0]->value
.d
[c
]);
890 data
.f
[c
] = sqrtf(op
[0]->value
.f
[c
]);
895 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
896 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
897 data
.f
[c
] = expf(op
[0]->value
.f
[c
]);
902 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
903 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
904 data
.f
[c
] = exp2f(op
[0]->value
.f
[c
]);
909 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
910 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
911 data
.f
[c
] = logf(op
[0]->value
.f
[c
]);
916 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
917 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
918 data
.f
[c
] = log2f(op
[0]->value
.f
[c
]);
923 case ir_unop_dFdx_coarse
:
924 case ir_unop_dFdx_fine
:
926 case ir_unop_dFdy_coarse
:
927 case ir_unop_dFdy_fine
:
928 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
929 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
934 case ir_unop_pack_snorm_2x16
:
935 assert(op
[0]->type
== glsl_type::vec2_type
);
936 data
.u
[0] = pack_2x16(pack_snorm_1x16
,
940 case ir_unop_pack_snorm_4x8
:
941 assert(op
[0]->type
== glsl_type::vec4_type
);
942 data
.u
[0] = pack_4x8(pack_snorm_1x8
,
948 case ir_unop_unpack_snorm_2x16
:
949 assert(op
[0]->type
== glsl_type::uint_type
);
950 unpack_2x16(unpack_snorm_1x16
,
952 &data
.f
[0], &data
.f
[1]);
954 case ir_unop_unpack_snorm_4x8
:
955 assert(op
[0]->type
== glsl_type::uint_type
);
956 unpack_4x8(unpack_snorm_1x8
,
958 &data
.f
[0], &data
.f
[1], &data
.f
[2], &data
.f
[3]);
960 case ir_unop_pack_unorm_2x16
:
961 assert(op
[0]->type
== glsl_type::vec2_type
);
962 data
.u
[0] = pack_2x16(pack_unorm_1x16
,
966 case ir_unop_pack_unorm_4x8
:
967 assert(op
[0]->type
== glsl_type::vec4_type
);
968 data
.u
[0] = pack_4x8(pack_unorm_1x8
,
974 case ir_unop_unpack_unorm_2x16
:
975 assert(op
[0]->type
== glsl_type::uint_type
);
976 unpack_2x16(unpack_unorm_1x16
,
978 &data
.f
[0], &data
.f
[1]);
980 case ir_unop_unpack_unorm_4x8
:
981 assert(op
[0]->type
== glsl_type::uint_type
);
982 unpack_4x8(unpack_unorm_1x8
,
984 &data
.f
[0], &data
.f
[1], &data
.f
[2], &data
.f
[3]);
986 case ir_unop_pack_half_2x16
:
987 assert(op
[0]->type
== glsl_type::vec2_type
);
988 data
.u
[0] = pack_2x16(pack_half_1x16
,
992 case ir_unop_unpack_half_2x16
:
993 assert(op
[0]->type
== glsl_type::uint_type
);
994 unpack_2x16(unpack_half_1x16
,
996 &data
.f
[0], &data
.f
[1]);
999 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
1000 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
1001 data
.f
[c
] = powf(op
[0]->value
.f
[c
], op
[1]->value
.f
[c
]);
1006 if (op
[0]->type
->base_type
== GLSL_TYPE_DOUBLE
)
1007 data
.d
[0] = dot_d(op
[0], op
[1]);
1009 data
.f
[0] = dot_f(op
[0], op
[1]);
1013 assert(op
[0]->type
== op
[1]->type
|| op0_scalar
|| op1_scalar
);
1014 for (unsigned c
= 0, c0
= 0, c1
= 0;
1016 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
1018 switch (op
[0]->type
->base_type
) {
1019 case GLSL_TYPE_UINT
:
1020 data
.u
[c
] = MIN2(op
[0]->value
.u
[c0
], op
[1]->value
.u
[c1
]);
1023 data
.i
[c
] = MIN2(op
[0]->value
.i
[c0
], op
[1]->value
.i
[c1
]);
1025 case GLSL_TYPE_FLOAT
:
1026 data
.f
[c
] = MIN2(op
[0]->value
.f
[c0
], op
[1]->value
.f
[c1
]);
1028 case GLSL_TYPE_DOUBLE
:
1029 data
.d
[c
] = MIN2(op
[0]->value
.d
[c0
], op
[1]->value
.d
[c1
]);
1038 assert(op
[0]->type
== op
[1]->type
|| op0_scalar
|| op1_scalar
);
1039 for (unsigned c
= 0, c0
= 0, c1
= 0;
1041 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
1043 switch (op
[0]->type
->base_type
) {
1044 case GLSL_TYPE_UINT
:
1045 data
.u
[c
] = MAX2(op
[0]->value
.u
[c0
], op
[1]->value
.u
[c1
]);
1048 data
.i
[c
] = MAX2(op
[0]->value
.i
[c0
], op
[1]->value
.i
[c1
]);
1050 case GLSL_TYPE_FLOAT
:
1051 data
.f
[c
] = MAX2(op
[0]->value
.f
[c0
], op
[1]->value
.f
[c1
]);
1053 case GLSL_TYPE_DOUBLE
:
1054 data
.d
[c
] = MAX2(op
[0]->value
.d
[c0
], op
[1]->value
.d
[c1
]);
1063 assert(op
[0]->type
== op
[1]->type
|| op0_scalar
|| op1_scalar
);
1064 for (unsigned c
= 0, c0
= 0, c1
= 0;
1066 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
1068 switch (op
[0]->type
->base_type
) {
1069 case GLSL_TYPE_UINT
:
1070 data
.u
[c
] = op
[0]->value
.u
[c0
] + op
[1]->value
.u
[c1
];
1073 data
.i
[c
] = op
[0]->value
.i
[c0
] + op
[1]->value
.i
[c1
];
1075 case GLSL_TYPE_FLOAT
:
1076 data
.f
[c
] = op
[0]->value
.f
[c0
] + op
[1]->value
.f
[c1
];
1078 case GLSL_TYPE_DOUBLE
:
1079 data
.d
[c
] = op
[0]->value
.d
[c0
] + op
[1]->value
.d
[c1
];
1088 assert(op
[0]->type
== op
[1]->type
|| op0_scalar
|| op1_scalar
);
1089 for (unsigned c
= 0, c0
= 0, c1
= 0;
1091 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
1093 switch (op
[0]->type
->base_type
) {
1094 case GLSL_TYPE_UINT
:
1095 data
.u
[c
] = op
[0]->value
.u
[c0
] - op
[1]->value
.u
[c1
];
1098 data
.i
[c
] = op
[0]->value
.i
[c0
] - op
[1]->value
.i
[c1
];
1100 case GLSL_TYPE_FLOAT
:
1101 data
.f
[c
] = op
[0]->value
.f
[c0
] - op
[1]->value
.f
[c1
];
1103 case GLSL_TYPE_DOUBLE
:
1104 data
.d
[c
] = op
[0]->value
.d
[c0
] - op
[1]->value
.d
[c1
];
1113 /* Check for equal types, or unequal types involving scalars */
1114 if ((op
[0]->type
== op
[1]->type
&& !op
[0]->type
->is_matrix())
1115 || op0_scalar
|| op1_scalar
) {
1116 for (unsigned c
= 0, c0
= 0, c1
= 0;
1118 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
1120 switch (op
[0]->type
->base_type
) {
1121 case GLSL_TYPE_UINT
:
1122 data
.u
[c
] = op
[0]->value
.u
[c0
] * op
[1]->value
.u
[c1
];
1125 data
.i
[c
] = op
[0]->value
.i
[c0
] * op
[1]->value
.i
[c1
];
1127 case GLSL_TYPE_FLOAT
:
1128 data
.f
[c
] = op
[0]->value
.f
[c0
] * op
[1]->value
.f
[c1
];
1130 case GLSL_TYPE_DOUBLE
:
1131 data
.d
[c
] = op
[0]->value
.d
[c0
] * op
[1]->value
.d
[c1
];
1138 assert(op
[0]->type
->is_matrix() || op
[1]->type
->is_matrix());
1140 /* Multiply an N-by-M matrix with an M-by-P matrix. Since either
1141 * matrix can be a GLSL vector, either N or P can be 1.
1143 * For vec*mat, the vector is treated as a row vector. This
1144 * means the vector is a 1-row x M-column matrix.
1146 * For mat*vec, the vector is treated as a column vector. Since
1147 * matrix_columns is 1 for vectors, this just works.
1149 const unsigned n
= op
[0]->type
->is_vector()
1150 ? 1 : op
[0]->type
->vector_elements
;
1151 const unsigned m
= op
[1]->type
->vector_elements
;
1152 const unsigned p
= op
[1]->type
->matrix_columns
;
1153 for (unsigned j
= 0; j
< p
; j
++) {
1154 for (unsigned i
= 0; i
< n
; i
++) {
1155 for (unsigned k
= 0; k
< m
; k
++) {
1156 if (op
[0]->type
->base_type
== GLSL_TYPE_DOUBLE
)
1157 data
.d
[i
+n
*j
] += op
[0]->value
.d
[i
+n
*k
]*op
[1]->value
.d
[k
+m
*j
];
1159 data
.f
[i
+n
*j
] += op
[0]->value
.f
[i
+n
*k
]*op
[1]->value
.f
[k
+m
*j
];
1167 /* FINISHME: Emit warning when division-by-zero is detected. */
1168 assert(op
[0]->type
== op
[1]->type
|| op0_scalar
|| op1_scalar
);
1169 for (unsigned c
= 0, c0
= 0, c1
= 0;
1171 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
1173 switch (op
[0]->type
->base_type
) {
1174 case GLSL_TYPE_UINT
:
1175 if (op
[1]->value
.u
[c1
] == 0) {
1178 data
.u
[c
] = op
[0]->value
.u
[c0
] / op
[1]->value
.u
[c1
];
1182 if (op
[1]->value
.i
[c1
] == 0) {
1185 data
.i
[c
] = op
[0]->value
.i
[c0
] / op
[1]->value
.i
[c1
];
1188 case GLSL_TYPE_FLOAT
:
1189 data
.f
[c
] = op
[0]->value
.f
[c0
] / op
[1]->value
.f
[c1
];
1191 case GLSL_TYPE_DOUBLE
:
1192 data
.d
[c
] = op
[0]->value
.d
[c0
] / op
[1]->value
.d
[c1
];
1201 /* FINISHME: Emit warning when division-by-zero is detected. */
1202 assert(op
[0]->type
== op
[1]->type
|| op0_scalar
|| op1_scalar
);
1203 for (unsigned c
= 0, c0
= 0, c1
= 0;
1205 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
1207 switch (op
[0]->type
->base_type
) {
1208 case GLSL_TYPE_UINT
:
1209 if (op
[1]->value
.u
[c1
] == 0) {
1212 data
.u
[c
] = op
[0]->value
.u
[c0
] % op
[1]->value
.u
[c1
];
1216 if (op
[1]->value
.i
[c1
] == 0) {
1219 data
.i
[c
] = op
[0]->value
.i
[c0
] % op
[1]->value
.i
[c1
];
1222 case GLSL_TYPE_FLOAT
:
1223 /* We don't use fmod because it rounds toward zero; GLSL specifies
1226 data
.f
[c
] = op
[0]->value
.f
[c0
] - op
[1]->value
.f
[c1
]
1227 * floorf(op
[0]->value
.f
[c0
] / op
[1]->value
.f
[c1
]);
1229 case GLSL_TYPE_DOUBLE
:
1230 /* We don't use fmod because it rounds toward zero; GLSL specifies
1233 data
.d
[c
] = op
[0]->value
.d
[c0
] - op
[1]->value
.d
[c1
]
1234 * floor(op
[0]->value
.d
[c0
] / op
[1]->value
.d
[c1
]);
1243 case ir_binop_logic_and
:
1244 assert(op
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
1245 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++)
1246 data
.b
[c
] = op
[0]->value
.b
[c
] && op
[1]->value
.b
[c
];
1248 case ir_binop_logic_xor
:
1249 assert(op
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
1250 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++)
1251 data
.b
[c
] = op
[0]->value
.b
[c
] ^ op
[1]->value
.b
[c
];
1253 case ir_binop_logic_or
:
1254 assert(op
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
1255 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++)
1256 data
.b
[c
] = op
[0]->value
.b
[c
] || op
[1]->value
.b
[c
];
1260 assert(op
[0]->type
== op
[1]->type
);
1261 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
1262 switch (op
[0]->type
->base_type
) {
1263 case GLSL_TYPE_UINT
:
1264 data
.b
[c
] = op
[0]->value
.u
[c
] < op
[1]->value
.u
[c
];
1267 data
.b
[c
] = op
[0]->value
.i
[c
] < op
[1]->value
.i
[c
];
1269 case GLSL_TYPE_FLOAT
:
1270 data
.b
[c
] = op
[0]->value
.f
[c
] < op
[1]->value
.f
[c
];
1272 case GLSL_TYPE_DOUBLE
:
1273 data
.b
[c
] = op
[0]->value
.d
[c
] < op
[1]->value
.d
[c
];
1280 case ir_binop_greater
:
1281 assert(op
[0]->type
== op
[1]->type
);
1282 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
1283 switch (op
[0]->type
->base_type
) {
1284 case GLSL_TYPE_UINT
:
1285 data
.b
[c
] = op
[0]->value
.u
[c
] > op
[1]->value
.u
[c
];
1288 data
.b
[c
] = op
[0]->value
.i
[c
] > op
[1]->value
.i
[c
];
1290 case GLSL_TYPE_FLOAT
:
1291 data
.b
[c
] = op
[0]->value
.f
[c
] > op
[1]->value
.f
[c
];
1293 case GLSL_TYPE_DOUBLE
:
1294 data
.b
[c
] = op
[0]->value
.d
[c
] > op
[1]->value
.d
[c
];
1301 case ir_binop_lequal
:
1302 assert(op
[0]->type
== op
[1]->type
);
1303 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
1304 switch (op
[0]->type
->base_type
) {
1305 case GLSL_TYPE_UINT
:
1306 data
.b
[c
] = op
[0]->value
.u
[c
] <= op
[1]->value
.u
[c
];
1309 data
.b
[c
] = op
[0]->value
.i
[c
] <= op
[1]->value
.i
[c
];
1311 case GLSL_TYPE_FLOAT
:
1312 data
.b
[c
] = op
[0]->value
.f
[c
] <= op
[1]->value
.f
[c
];
1314 case GLSL_TYPE_DOUBLE
:
1315 data
.b
[c
] = op
[0]->value
.d
[c
] <= op
[1]->value
.d
[c
];
1322 case ir_binop_gequal
:
1323 assert(op
[0]->type
== op
[1]->type
);
1324 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
1325 switch (op
[0]->type
->base_type
) {
1326 case GLSL_TYPE_UINT
:
1327 data
.b
[c
] = op
[0]->value
.u
[c
] >= op
[1]->value
.u
[c
];
1330 data
.b
[c
] = op
[0]->value
.i
[c
] >= op
[1]->value
.i
[c
];
1332 case GLSL_TYPE_FLOAT
:
1333 data
.b
[c
] = op
[0]->value
.f
[c
] >= op
[1]->value
.f
[c
];
1335 case GLSL_TYPE_DOUBLE
:
1336 data
.b
[c
] = op
[0]->value
.d
[c
] >= op
[1]->value
.d
[c
];
1343 case ir_binop_equal
:
1344 assert(op
[0]->type
== op
[1]->type
);
1345 for (unsigned c
= 0; c
< components
; c
++) {
1346 switch (op
[0]->type
->base_type
) {
1347 case GLSL_TYPE_UINT
:
1348 data
.b
[c
] = op
[0]->value
.u
[c
] == op
[1]->value
.u
[c
];
1351 data
.b
[c
] = op
[0]->value
.i
[c
] == op
[1]->value
.i
[c
];
1353 case GLSL_TYPE_FLOAT
:
1354 data
.b
[c
] = op
[0]->value
.f
[c
] == op
[1]->value
.f
[c
];
1356 case GLSL_TYPE_BOOL
:
1357 data
.b
[c
] = op
[0]->value
.b
[c
] == op
[1]->value
.b
[c
];
1359 case GLSL_TYPE_DOUBLE
:
1360 data
.b
[c
] = op
[0]->value
.d
[c
] == op
[1]->value
.d
[c
];
1367 case ir_binop_nequal
:
1368 assert(op
[0]->type
== op
[1]->type
);
1369 for (unsigned c
= 0; c
< components
; c
++) {
1370 switch (op
[0]->type
->base_type
) {
1371 case GLSL_TYPE_UINT
:
1372 data
.b
[c
] = op
[0]->value
.u
[c
] != op
[1]->value
.u
[c
];
1375 data
.b
[c
] = op
[0]->value
.i
[c
] != op
[1]->value
.i
[c
];
1377 case GLSL_TYPE_FLOAT
:
1378 data
.b
[c
] = op
[0]->value
.f
[c
] != op
[1]->value
.f
[c
];
1380 case GLSL_TYPE_BOOL
:
1381 data
.b
[c
] = op
[0]->value
.b
[c
] != op
[1]->value
.b
[c
];
1383 case GLSL_TYPE_DOUBLE
:
1384 data
.b
[c
] = op
[0]->value
.d
[c
] != op
[1]->value
.d
[c
];
1391 case ir_binop_all_equal
:
1392 data
.b
[0] = op
[0]->has_value(op
[1]);
1394 case ir_binop_any_nequal
:
1395 data
.b
[0] = !op
[0]->has_value(op
[1]);
1398 case ir_binop_lshift
:
1399 for (unsigned c
= 0, c0
= 0, c1
= 0;
1401 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
1403 if (op
[0]->type
->base_type
== GLSL_TYPE_INT
&&
1404 op
[1]->type
->base_type
== GLSL_TYPE_INT
) {
1405 data
.i
[c
] = op
[0]->value
.i
[c0
] << op
[1]->value
.i
[c1
];
1407 } else if (op
[0]->type
->base_type
== GLSL_TYPE_INT
&&
1408 op
[1]->type
->base_type
== GLSL_TYPE_UINT
) {
1409 data
.i
[c
] = op
[0]->value
.i
[c0
] << op
[1]->value
.u
[c1
];
1411 } else if (op
[0]->type
->base_type
== GLSL_TYPE_UINT
&&
1412 op
[1]->type
->base_type
== GLSL_TYPE_INT
) {
1413 data
.u
[c
] = op
[0]->value
.u
[c0
] << op
[1]->value
.i
[c1
];
1415 } else if (op
[0]->type
->base_type
== GLSL_TYPE_UINT
&&
1416 op
[1]->type
->base_type
== GLSL_TYPE_UINT
) {
1417 data
.u
[c
] = op
[0]->value
.u
[c0
] << op
[1]->value
.u
[c1
];
1422 case ir_binop_rshift
:
1423 for (unsigned c
= 0, c0
= 0, c1
= 0;
1425 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
1427 if (op
[0]->type
->base_type
== GLSL_TYPE_INT
&&
1428 op
[1]->type
->base_type
== GLSL_TYPE_INT
) {
1429 data
.i
[c
] = op
[0]->value
.i
[c0
] >> op
[1]->value
.i
[c1
];
1431 } else if (op
[0]->type
->base_type
== GLSL_TYPE_INT
&&
1432 op
[1]->type
->base_type
== GLSL_TYPE_UINT
) {
1433 data
.i
[c
] = op
[0]->value
.i
[c0
] >> op
[1]->value
.u
[c1
];
1435 } else if (op
[0]->type
->base_type
== GLSL_TYPE_UINT
&&
1436 op
[1]->type
->base_type
== GLSL_TYPE_INT
) {
1437 data
.u
[c
] = op
[0]->value
.u
[c0
] >> op
[1]->value
.i
[c1
];
1439 } else if (op
[0]->type
->base_type
== GLSL_TYPE_UINT
&&
1440 op
[1]->type
->base_type
== GLSL_TYPE_UINT
) {
1441 data
.u
[c
] = op
[0]->value
.u
[c0
] >> op
[1]->value
.u
[c1
];
1446 case ir_binop_bit_and
:
1447 for (unsigned c
= 0, c0
= 0, c1
= 0;
1449 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
1451 switch (op
[0]->type
->base_type
) {
1453 data
.i
[c
] = op
[0]->value
.i
[c0
] & op
[1]->value
.i
[c1
];
1455 case GLSL_TYPE_UINT
:
1456 data
.u
[c
] = op
[0]->value
.u
[c0
] & op
[1]->value
.u
[c1
];
1464 case ir_binop_bit_or
:
1465 for (unsigned c
= 0, c0
= 0, c1
= 0;
1467 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
1469 switch (op
[0]->type
->base_type
) {
1471 data
.i
[c
] = op
[0]->value
.i
[c0
] | op
[1]->value
.i
[c1
];
1473 case GLSL_TYPE_UINT
:
1474 data
.u
[c
] = op
[0]->value
.u
[c0
] | op
[1]->value
.u
[c1
];
1482 case ir_binop_vector_extract
: {
1483 const int c
= CLAMP(op
[1]->value
.i
[0], 0,
1484 (int) op
[0]->type
->vector_elements
- 1);
1486 switch (op
[0]->type
->base_type
) {
1487 case GLSL_TYPE_UINT
:
1488 data
.u
[0] = op
[0]->value
.u
[c
];
1491 data
.i
[0] = op
[0]->value
.i
[c
];
1493 case GLSL_TYPE_FLOAT
:
1494 data
.f
[0] = op
[0]->value
.f
[c
];
1496 case GLSL_TYPE_DOUBLE
:
1497 data
.d
[0] = op
[0]->value
.d
[c
];
1499 case GLSL_TYPE_BOOL
:
1500 data
.b
[0] = op
[0]->value
.b
[c
];
1508 case ir_binop_bit_xor
:
1509 for (unsigned c
= 0, c0
= 0, c1
= 0;
1511 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
1513 switch (op
[0]->type
->base_type
) {
1515 data
.i
[c
] = op
[0]->value
.i
[c0
] ^ op
[1]->value
.i
[c1
];
1517 case GLSL_TYPE_UINT
:
1518 data
.u
[c
] = op
[0]->value
.u
[c0
] ^ op
[1]->value
.u
[c1
];
1526 case ir_unop_bitfield_reverse
:
1527 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
1528 switch (this->type
->base_type
) {
1529 case GLSL_TYPE_UINT
:
1530 data
.u
[c
] = bitfield_reverse(op
[0]->value
.u
[c
]);
1533 data
.i
[c
] = bitfield_reverse(op
[0]->value
.i
[c
]);
1541 case ir_unop_bit_count
:
1542 for (unsigned c
= 0; c
< components
; c
++)
1543 data
.i
[c
] = _mesa_bitcount(op
[0]->value
.u
[c
]);
1546 case ir_unop_find_msb
:
1547 for (unsigned c
= 0; c
< components
; c
++) {
1548 switch (op
[0]->type
->base_type
) {
1549 case GLSL_TYPE_UINT
:
1550 data
.i
[c
] = find_msb_uint(op
[0]->value
.u
[c
]);
1553 data
.i
[c
] = find_msb_int(op
[0]->value
.i
[c
]);
1561 case ir_unop_find_lsb
:
1562 for (unsigned c
= 0; c
< components
; c
++) {
1563 if (op
[0]->value
.i
[c
] == 0)
1567 unsigned v
= op
[0]->value
.u
[c
];
1569 for (; !(v
& 1); v
>>= 1) {
1577 case ir_unop_saturate
:
1578 for (unsigned c
= 0; c
< components
; c
++) {
1579 data
.f
[c
] = CLAMP(op
[0]->value
.f
[c
], 0.0f
, 1.0f
);
1582 case ir_unop_pack_double_2x32
:
1583 /* XXX needs to be checked on big-endian */
1584 memcpy(&data
.d
[0], &op
[0]->value
.u
[0], sizeof(double));
1586 case ir_unop_unpack_double_2x32
:
1587 /* XXX needs to be checked on big-endian */
1588 memcpy(&data
.u
[0], &op
[0]->value
.d
[0], sizeof(double));
1591 case ir_triop_bitfield_extract
: {
1592 for (unsigned c
= 0; c
< components
; c
++) {
1593 int offset
= op
[1]->value
.i
[c
];
1594 int bits
= op
[2]->value
.i
[c
];
1598 else if (offset
< 0 || bits
< 0)
1599 data
.u
[c
] = 0; /* Undefined, per spec. */
1600 else if (offset
+ bits
> 32)
1601 data
.u
[c
] = 0; /* Undefined, per spec. */
1603 if (op
[0]->type
->base_type
== GLSL_TYPE_INT
) {
1604 /* int so that the right shift will sign-extend. */
1605 int value
= op
[0]->value
.i
[c
];
1606 value
<<= 32 - bits
- offset
;
1607 value
>>= 32 - bits
;
1610 unsigned value
= op
[0]->value
.u
[c
];
1611 value
<<= 32 - bits
- offset
;
1612 value
>>= 32 - bits
;
1620 case ir_binop_ldexp
:
1621 for (unsigned c
= 0; c
< components
; c
++) {
1622 if (op
[0]->type
->base_type
== GLSL_TYPE_DOUBLE
) {
1623 data
.d
[c
] = ldexp(op
[0]->value
.d
[c
], op
[1]->value
.i
[c
]);
1624 /* Flush subnormal values to zero. */
1625 if (!isnormal(data
.d
[c
]))
1626 data
.d
[c
] = copysign(0.0, op
[0]->value
.d
[c
]);
1628 data
.f
[c
] = ldexpf(op
[0]->value
.f
[c
], op
[1]->value
.i
[c
]);
1629 /* Flush subnormal values to zero. */
1630 if (!isnormal(data
.f
[c
]))
1631 data
.f
[c
] = copysignf(0.0f
, op
[0]->value
.f
[c
]);
1637 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
||
1638 op
[0]->type
->base_type
== GLSL_TYPE_DOUBLE
);
1639 assert(op
[1]->type
->base_type
== GLSL_TYPE_FLOAT
||
1640 op
[1]->type
->base_type
== GLSL_TYPE_DOUBLE
);
1641 assert(op
[2]->type
->base_type
== GLSL_TYPE_FLOAT
||
1642 op
[2]->type
->base_type
== GLSL_TYPE_DOUBLE
);
1644 for (unsigned c
= 0; c
< components
; c
++) {
1645 if (op
[0]->type
->base_type
== GLSL_TYPE_DOUBLE
)
1646 data
.d
[c
] = op
[0]->value
.d
[c
] * op
[1]->value
.d
[c
]
1647 + op
[2]->value
.d
[c
];
1649 data
.f
[c
] = op
[0]->value
.f
[c
] * op
[1]->value
.f
[c
]
1650 + op
[2]->value
.f
[c
];
1654 case ir_triop_lrp
: {
1655 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
||
1656 op
[0]->type
->base_type
== GLSL_TYPE_DOUBLE
);
1657 assert(op
[1]->type
->base_type
== GLSL_TYPE_FLOAT
||
1658 op
[1]->type
->base_type
== GLSL_TYPE_DOUBLE
);
1659 assert(op
[2]->type
->base_type
== GLSL_TYPE_FLOAT
||
1660 op
[2]->type
->base_type
== GLSL_TYPE_DOUBLE
);
1662 unsigned c2_inc
= op
[2]->type
->is_scalar() ? 0 : 1;
1663 for (unsigned c
= 0, c2
= 0; c
< components
; c2
+= c2_inc
, c
++) {
1664 if (op
[0]->type
->base_type
== GLSL_TYPE_DOUBLE
)
1665 data
.d
[c
] = op
[0]->value
.d
[c
] * (1.0 - op
[2]->value
.d
[c2
]) +
1666 (op
[1]->value
.d
[c
] * op
[2]->value
.d
[c2
]);
1668 data
.f
[c
] = op
[0]->value
.f
[c
] * (1.0f
- op
[2]->value
.f
[c2
]) +
1669 (op
[1]->value
.f
[c
] * op
[2]->value
.f
[c2
]);
1675 for (unsigned c
= 0; c
< components
; c
++) {
1676 if (op
[1]->type
->base_type
== GLSL_TYPE_DOUBLE
)
1677 data
.d
[c
] = op
[0]->value
.b
[c
] ? op
[1]->value
.d
[c
]
1678 : op
[2]->value
.d
[c
];
1680 data
.u
[c
] = op
[0]->value
.b
[c
] ? op
[1]->value
.u
[c
]
1681 : op
[2]->value
.u
[c
];
1685 case ir_triop_vector_insert
: {
1686 const unsigned idx
= op
[2]->value
.u
[0];
1688 memcpy(&data
, &op
[0]->value
, sizeof(data
));
1690 switch (this->type
->base_type
) {
1692 data
.i
[idx
] = op
[1]->value
.i
[0];
1694 case GLSL_TYPE_UINT
:
1695 data
.u
[idx
] = op
[1]->value
.u
[0];
1697 case GLSL_TYPE_FLOAT
:
1698 data
.f
[idx
] = op
[1]->value
.f
[0];
1700 case GLSL_TYPE_BOOL
:
1701 data
.b
[idx
] = op
[1]->value
.b
[0];
1703 case GLSL_TYPE_DOUBLE
:
1704 data
.d
[idx
] = op
[1]->value
.d
[0];
1707 assert(!"Should not get here.");
1713 case ir_quadop_bitfield_insert
: {
1714 for (unsigned c
= 0; c
< components
; c
++) {
1715 int offset
= op
[2]->value
.i
[c
];
1716 int bits
= op
[3]->value
.i
[c
];
1719 data
.u
[c
] = op
[0]->value
.u
[c
];
1720 else if (offset
< 0 || bits
< 0)
1721 data
.u
[c
] = 0; /* Undefined, per spec. */
1722 else if (offset
+ bits
> 32)
1723 data
.u
[c
] = 0; /* Undefined, per spec. */
1725 unsigned insert_mask
= ((1ull << bits
) - 1) << offset
;
1727 unsigned insert
= op
[1]->value
.u
[c
];
1729 insert
&= insert_mask
;
1731 unsigned base
= op
[0]->value
.u
[c
];
1732 base
&= ~insert_mask
;
1734 data
.u
[c
] = base
| insert
;
1740 case ir_quadop_vector
:
1741 for (unsigned c
= 0; c
< this->type
->vector_elements
; c
++) {
1742 switch (this->type
->base_type
) {
1744 data
.i
[c
] = op
[c
]->value
.i
[0];
1746 case GLSL_TYPE_UINT
:
1747 data
.u
[c
] = op
[c
]->value
.u
[0];
1749 case GLSL_TYPE_FLOAT
:
1750 data
.f
[c
] = op
[c
]->value
.f
[0];
1752 case GLSL_TYPE_DOUBLE
:
1753 data
.d
[c
] = op
[c
]->value
.d
[0];
1755 case GLSL_TYPE_BOOL
:
1756 data
.b
[c
] = op
[c
]->value
.b
[0];
1765 /* FINISHME: Should handle all expression types. */
1769 return new(ctx
) ir_constant(this->type
, &data
);
1774 ir_texture::constant_expression_value(struct hash_table
*)
1776 /* texture lookups aren't constant expressions */
1782 ir_swizzle::constant_expression_value(struct hash_table
*variable_context
)
1784 ir_constant
*v
= this->val
->constant_expression_value(variable_context
);
1787 ir_constant_data data
= { { 0 } };
1789 const unsigned swiz_idx
[4] = {
1790 this->mask
.x
, this->mask
.y
, this->mask
.z
, this->mask
.w
1793 for (unsigned i
= 0; i
< this->mask
.num_components
; i
++) {
1794 switch (v
->type
->base_type
) {
1795 case GLSL_TYPE_UINT
:
1796 case GLSL_TYPE_INT
: data
.u
[i
] = v
->value
.u
[swiz_idx
[i
]]; break;
1797 case GLSL_TYPE_FLOAT
: data
.f
[i
] = v
->value
.f
[swiz_idx
[i
]]; break;
1798 case GLSL_TYPE_BOOL
: data
.b
[i
] = v
->value
.b
[swiz_idx
[i
]]; break;
1799 case GLSL_TYPE_DOUBLE
:data
.d
[i
] = v
->value
.d
[swiz_idx
[i
]]; break;
1800 default: assert(!"Should not get here."); break;
1804 void *ctx
= ralloc_parent(this);
1805 return new(ctx
) ir_constant(this->type
, &data
);
1812 ir_dereference_variable::constant_expression_value(struct hash_table
*variable_context
)
1816 /* Give priority to the context hashtable, if it exists */
1817 if (variable_context
) {
1818 ir_constant
*value
= (ir_constant
*)hash_table_find(variable_context
, var
);
1823 /* The constant_value of a uniform variable is its initializer,
1824 * not the lifetime constant value of the uniform.
1826 if (var
->data
.mode
== ir_var_uniform
)
1829 if (!var
->constant_value
)
1832 return var
->constant_value
->clone(ralloc_parent(var
), NULL
);
1837 ir_dereference_array::constant_expression_value(struct hash_table
*variable_context
)
1839 ir_constant
*array
= this->array
->constant_expression_value(variable_context
);
1840 ir_constant
*idx
= this->array_index
->constant_expression_value(variable_context
);
1842 if ((array
!= NULL
) && (idx
!= NULL
)) {
1843 void *ctx
= ralloc_parent(this);
1844 if (array
->type
->is_matrix()) {
1845 /* Array access of a matrix results in a vector.
1847 const unsigned column
= idx
->value
.u
[0];
1849 const glsl_type
*const column_type
= array
->type
->column_type();
1851 /* Offset in the constant matrix to the first element of the column
1854 const unsigned mat_idx
= column
* column_type
->vector_elements
;
1856 ir_constant_data data
= { { 0 } };
1858 switch (column_type
->base_type
) {
1859 case GLSL_TYPE_UINT
:
1861 for (unsigned i
= 0; i
< column_type
->vector_elements
; i
++)
1862 data
.u
[i
] = array
->value
.u
[mat_idx
+ i
];
1866 case GLSL_TYPE_FLOAT
:
1867 for (unsigned i
= 0; i
< column_type
->vector_elements
; i
++)
1868 data
.f
[i
] = array
->value
.f
[mat_idx
+ i
];
1872 case GLSL_TYPE_DOUBLE
:
1873 for (unsigned i
= 0; i
< column_type
->vector_elements
; i
++)
1874 data
.d
[i
] = array
->value
.d
[mat_idx
+ i
];
1879 assert(!"Should not get here.");
1883 return new(ctx
) ir_constant(column_type
, &data
);
1884 } else if (array
->type
->is_vector()) {
1885 const unsigned component
= idx
->value
.u
[0];
1887 return new(ctx
) ir_constant(array
, component
);
1889 const unsigned index
= idx
->value
.u
[0];
1890 return array
->get_array_element(index
)->clone(ctx
, NULL
);
1898 ir_dereference_record::constant_expression_value(struct hash_table
*)
1900 ir_constant
*v
= this->record
->constant_expression_value();
1902 return (v
!= NULL
) ? v
->get_record_field(this->field
) : NULL
;
1907 ir_assignment::constant_expression_value(struct hash_table
*)
1909 /* FINISHME: Handle CEs involving assignment (return RHS) */
1915 ir_constant::constant_expression_value(struct hash_table
*)
1922 ir_call::constant_expression_value(struct hash_table
*variable_context
)
1924 return this->callee
->constant_expression_value(&this->actual_parameters
, variable_context
);
1928 bool ir_function_signature::constant_expression_evaluate_expression_list(const struct exec_list
&body
,
1929 struct hash_table
*variable_context
,
1930 ir_constant
**result
)
1932 foreach_in_list(ir_instruction
, inst
, &body
) {
1933 switch(inst
->ir_type
) {
1935 /* (declare () type symbol) */
1936 case ir_type_variable
: {
1937 ir_variable
*var
= inst
->as_variable();
1938 hash_table_insert(variable_context
, ir_constant::zero(this, var
->type
), var
);
1942 /* (assign [condition] (write-mask) (ref) (value)) */
1943 case ir_type_assignment
: {
1944 ir_assignment
*asg
= inst
->as_assignment();
1945 if (asg
->condition
) {
1946 ir_constant
*cond
= asg
->condition
->constant_expression_value(variable_context
);
1949 if (!cond
->get_bool_component(0))
1953 ir_constant
*store
= NULL
;
1956 if (!constant_referenced(asg
->lhs
, variable_context
, store
, offset
))
1959 ir_constant
*value
= asg
->rhs
->constant_expression_value(variable_context
);
1964 store
->copy_masked_offset(value
, offset
, asg
->write_mask
);
1968 /* (return (expression)) */
1969 case ir_type_return
:
1971 *result
= inst
->as_return()->value
->constant_expression_value(variable_context
);
1972 return *result
!= NULL
;
1974 /* (call name (ref) (params))*/
1975 case ir_type_call
: {
1976 ir_call
*call
= inst
->as_call();
1978 /* Just say no to void functions in constant expressions. We
1979 * don't need them at that point.
1982 if (!call
->return_deref
)
1985 ir_constant
*store
= NULL
;
1988 if (!constant_referenced(call
->return_deref
, variable_context
,
1992 ir_constant
*value
= call
->constant_expression_value(variable_context
);
1997 store
->copy_offset(value
, offset
);
2001 /* (if condition (then-instructions) (else-instructions)) */
2003 ir_if
*iif
= inst
->as_if();
2005 ir_constant
*cond
= iif
->condition
->constant_expression_value(variable_context
);
2006 if (!cond
|| !cond
->type
->is_boolean())
2009 exec_list
&branch
= cond
->get_bool_component(0) ? iif
->then_instructions
: iif
->else_instructions
;
2012 if (!constant_expression_evaluate_expression_list(branch
, variable_context
, result
))
2015 /* If there was a return in the branch chosen, drop out now. */
2022 /* Every other expression type, we drop out. */
2028 /* Reaching the end of the block is not an error condition */
2036 ir_function_signature::constant_expression_value(exec_list
*actual_parameters
, struct hash_table
*variable_context
)
2038 const glsl_type
*type
= this->return_type
;
2039 if (type
== glsl_type::void_type
)
2042 /* From the GLSL 1.20 spec, page 23:
2043 * "Function calls to user-defined functions (non-built-in functions)
2044 * cannot be used to form constant expressions."
2046 if (!this->is_builtin())
2050 * Of the builtin functions, only the texture lookups and the noise
2051 * ones must not be used in constant expressions. They all include
2052 * specific opcodes so they don't need to be special-cased at this
2056 /* Initialize the table of dereferencable names with the function
2057 * parameters. Verify their const-ness on the way.
2059 * We expect the correctness of the number of parameters to have
2060 * been checked earlier.
2062 hash_table
*deref_hash
= hash_table_ctor(8, hash_table_pointer_hash
,
2063 hash_table_pointer_compare
);
2065 /* If "origin" is non-NULL, then the function body is there. So we
2066 * have to use the variable objects from the object with the body,
2067 * but the parameter instanciation on the current object.
2069 const exec_node
*parameter_info
= origin
? origin
->parameters
.get_head_raw() : parameters
.get_head_raw();
2071 foreach_in_list(ir_rvalue
, n
, actual_parameters
) {
2072 ir_constant
*constant
= n
->constant_expression_value(variable_context
);
2073 if (constant
== NULL
) {
2074 hash_table_dtor(deref_hash
);
2079 ir_variable
*var
= (ir_variable
*)parameter_info
;
2080 hash_table_insert(deref_hash
, constant
, var
);
2082 parameter_info
= parameter_info
->next
;
2085 ir_constant
*result
= NULL
;
2087 /* Now run the builtin function until something non-constant
2088 * happens or we get the result.
2090 if (constant_expression_evaluate_expression_list(origin
? origin
->body
: body
, deref_hash
, &result
) && result
)
2091 result
= result
->clone(ralloc_parent(this), NULL
);
2093 hash_table_dtor(deref_hash
);