2 * Copyright © 2010 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
25 * \file ir_constant_expression.cpp
26 * Evaluate and process constant valued expressions
28 * In GLSL, constant valued expressions are used in several places. These
29 * must be processed and evaluated very early in the compilation process.
32 * * Initializers for uniforms
33 * * Initializers for \c const variables
37 #include "main/core.h" /* for MAX2, MIN2, CLAMP */
39 #include "glsl_types.h"
40 #include "program/hash_table.h"
42 #if defined(_MSC_VER) && (_MSC_VER < 1800)
43 static int isnormal(double x
)
45 return _fpclass(x
) == _FPCLASS_NN
|| _fpclass(x
) == _FPCLASS_PN
;
47 #elif defined(__SUNPRO_CC) && !defined(isnormal)
49 static int isnormal(double x
)
51 return fpclass(x
) == FP_NORMAL
;
56 static double copysign(double x
, double y
)
58 return _copysign(x
, y
);
63 dot_f(ir_constant
*op0
, ir_constant
*op1
)
65 assert(op0
->type
->is_float() && op1
->type
->is_float());
68 for (unsigned c
= 0; c
< op0
->type
->components(); c
++)
69 result
+= op0
->value
.f
[c
] * op1
->value
.f
[c
];
75 dot_d(ir_constant
*op0
, ir_constant
*op1
)
77 assert(op0
->type
->is_double() && op1
->type
->is_double());
80 for (unsigned c
= 0; c
< op0
->type
->components(); c
++)
81 result
+= op0
->value
.d
[c
] * op1
->value
.d
[c
];
86 /* This method is the only one supported by gcc. Unions in particular
87 * are iffy, and read-through-converted-pointer is killed by strict
88 * aliasing. OTOH, the compiler sees through the memcpy, so the
89 * resulting asm is reasonable.
92 bitcast_u2f(unsigned int u
)
94 assert(sizeof(float) == sizeof(unsigned int));
96 memcpy(&f
, &u
, sizeof(f
));
103 assert(sizeof(float) == sizeof(unsigned int));
105 memcpy(&u
, &f
, sizeof(f
));
110 * Evaluate one component of a floating-point 4x8 unpacking function.
113 (*pack_1x8_func_t
)(float);
116 * Evaluate one component of a floating-point 2x16 unpacking function.
119 (*pack_1x16_func_t
)(float);
122 * Evaluate one component of a floating-point 4x8 unpacking function.
125 (*unpack_1x8_func_t
)(uint8_t);
128 * Evaluate one component of a floating-point 2x16 unpacking function.
131 (*unpack_1x16_func_t
)(uint16_t);
134 * Evaluate a 2x16 floating-point packing function.
137 pack_2x16(pack_1x16_func_t pack_1x16
,
140 /* From section 8.4 of the GLSL ES 3.00 spec:
144 * The first component of the vector will be written to the least
145 * significant bits of the output; the last component will be written to
146 * the most significant bits.
148 * The specifications for the other packing functions contain similar
152 u
|= ((uint32_t) pack_1x16(x
) << 0);
153 u
|= ((uint32_t) pack_1x16(y
) << 16);
158 * Evaluate a 4x8 floating-point packing function.
161 pack_4x8(pack_1x8_func_t pack_1x8
,
162 float x
, float y
, float z
, float w
)
164 /* From section 8.4 of the GLSL 4.30 spec:
168 * The first component of the vector will be written to the least
169 * significant bits of the output; the last component will be written to
170 * the most significant bits.
172 * The specifications for the other packing functions contain similar
176 u
|= ((uint32_t) pack_1x8(x
) << 0);
177 u
|= ((uint32_t) pack_1x8(y
) << 8);
178 u
|= ((uint32_t) pack_1x8(z
) << 16);
179 u
|= ((uint32_t) pack_1x8(w
) << 24);
184 * Evaluate a 2x16 floating-point unpacking function.
187 unpack_2x16(unpack_1x16_func_t unpack_1x16
,
191 /* From section 8.4 of the GLSL ES 3.00 spec:
195 * The first component of the returned vector will be extracted from
196 * the least significant bits of the input; the last component will be
197 * extracted from the most significant bits.
199 * The specifications for the other unpacking functions contain similar
202 *x
= unpack_1x16((uint16_t) (u
& 0xffff));
203 *y
= unpack_1x16((uint16_t) (u
>> 16));
207 * Evaluate a 4x8 floating-point unpacking function.
210 unpack_4x8(unpack_1x8_func_t unpack_1x8
, uint32_t u
,
211 float *x
, float *y
, float *z
, float *w
)
213 /* From section 8.4 of the GLSL 4.30 spec:
217 * The first component of the returned vector will be extracted from
218 * the least significant bits of the input; the last component will be
219 * extracted from the most significant bits.
221 * The specifications for the other unpacking functions contain similar
224 *x
= unpack_1x8((uint8_t) (u
& 0xff));
225 *y
= unpack_1x8((uint8_t) (u
>> 8));
226 *z
= unpack_1x8((uint8_t) (u
>> 16));
227 *w
= unpack_1x8((uint8_t) (u
>> 24));
231 * Evaluate one component of packSnorm4x8.
234 pack_snorm_1x8(float x
)
236 /* From section 8.4 of the GLSL 4.30 spec:
240 * The conversion for component c of v to fixed point is done as
243 * packSnorm4x8: round(clamp(c, -1, +1) * 127.0)
245 * We must first cast the float to an int, because casting a negative
246 * float to a uint is undefined.
248 return (uint8_t) (int8_t)
249 _mesa_round_to_even(CLAMP(x
, -1.0f
, +1.0f
) * 127.0f
);
253 * Evaluate one component of packSnorm2x16.
256 pack_snorm_1x16(float x
)
258 /* From section 8.4 of the GLSL ES 3.00 spec:
262 * The conversion for component c of v to fixed point is done as
265 * packSnorm2x16: round(clamp(c, -1, +1) * 32767.0)
267 * We must first cast the float to an int, because casting a negative
268 * float to a uint is undefined.
270 return (uint16_t) (int16_t)
271 _mesa_round_to_even(CLAMP(x
, -1.0f
, +1.0f
) * 32767.0f
);
275 * Evaluate one component of unpackSnorm4x8.
278 unpack_snorm_1x8(uint8_t u
)
280 /* From section 8.4 of the GLSL 4.30 spec:
284 * The conversion for unpacked fixed-point value f to floating point is
287 * unpackSnorm4x8: clamp(f / 127.0, -1, +1)
289 return CLAMP((int8_t) u
/ 127.0f
, -1.0f
, +1.0f
);
293 * Evaluate one component of unpackSnorm2x16.
296 unpack_snorm_1x16(uint16_t u
)
298 /* From section 8.4 of the GLSL ES 3.00 spec:
302 * The conversion for unpacked fixed-point value f to floating point is
305 * unpackSnorm2x16: clamp(f / 32767.0, -1, +1)
307 return CLAMP((int16_t) u
/ 32767.0f
, -1.0f
, +1.0f
);
311 * Evaluate one component packUnorm4x8.
314 pack_unorm_1x8(float x
)
316 /* From section 8.4 of the GLSL 4.30 spec:
320 * The conversion for component c of v to fixed point is done as
323 * packUnorm4x8: round(clamp(c, 0, +1) * 255.0)
325 return (uint8_t) _mesa_round_to_even(CLAMP(x
, 0.0f
, 1.0f
) * 255.0f
);
329 * Evaluate one component packUnorm2x16.
332 pack_unorm_1x16(float x
)
334 /* From section 8.4 of the GLSL ES 3.00 spec:
338 * The conversion for component c of v to fixed point is done as
341 * packUnorm2x16: round(clamp(c, 0, +1) * 65535.0)
343 return (uint16_t) _mesa_round_to_even(CLAMP(x
, 0.0f
, 1.0f
) * 65535.0f
);
347 * Evaluate one component of unpackUnorm4x8.
350 unpack_unorm_1x8(uint8_t u
)
352 /* From section 8.4 of the GLSL 4.30 spec:
356 * The conversion for unpacked fixed-point value f to floating point is
359 * unpackUnorm4x8: f / 255.0
361 return (float) u
/ 255.0f
;
365 * Evaluate one component of unpackUnorm2x16.
368 unpack_unorm_1x16(uint16_t u
)
370 /* From section 8.4 of the GLSL ES 3.00 spec:
374 * The conversion for unpacked fixed-point value f to floating point is
377 * unpackUnorm2x16: f / 65535.0
379 return (float) u
/ 65535.0f
;
383 * Evaluate one component of packHalf2x16.
386 pack_half_1x16(float x
)
388 return _mesa_float_to_half(x
);
392 * Evaluate one component of unpackHalf2x16.
395 unpack_half_1x16(uint16_t u
)
397 return _mesa_half_to_float(u
);
401 * Get the constant that is ultimately referenced by an r-value, in a constant
402 * expression evaluation context.
404 * The offset is used when the reference is to a specific column of a matrix.
407 constant_referenced(const ir_dereference
*deref
,
408 struct hash_table
*variable_context
,
409 ir_constant
*&store
, int &offset
)
414 if (variable_context
== NULL
)
417 switch (deref
->ir_type
) {
418 case ir_type_dereference_array
: {
419 const ir_dereference_array
*const da
=
420 (const ir_dereference_array
*) deref
;
422 ir_constant
*const index_c
=
423 da
->array_index
->constant_expression_value(variable_context
);
425 if (!index_c
|| !index_c
->type
->is_scalar() || !index_c
->type
->is_integer())
428 const int index
= index_c
->type
->base_type
== GLSL_TYPE_INT
?
429 index_c
->get_int_component(0) :
430 index_c
->get_uint_component(0);
432 ir_constant
*substore
;
435 const ir_dereference
*const deref
= da
->array
->as_dereference();
439 if (!constant_referenced(deref
, variable_context
, substore
, suboffset
))
442 const glsl_type
*const vt
= da
->array
->type
;
443 if (vt
->is_array()) {
444 store
= substore
->get_array_element(index
);
446 } else if (vt
->is_matrix()) {
448 offset
= index
* vt
->vector_elements
;
449 } else if (vt
->is_vector()) {
451 offset
= suboffset
+ index
;
457 case ir_type_dereference_record
: {
458 const ir_dereference_record
*const dr
=
459 (const ir_dereference_record
*) deref
;
461 const ir_dereference
*const deref
= dr
->record
->as_dereference();
465 ir_constant
*substore
;
468 if (!constant_referenced(deref
, variable_context
, substore
, suboffset
))
471 /* Since we're dropping it on the floor...
473 assert(suboffset
== 0);
475 store
= substore
->get_record_field(dr
->field
);
479 case ir_type_dereference_variable
: {
480 const ir_dereference_variable
*const dv
=
481 (const ir_dereference_variable
*) deref
;
483 store
= (ir_constant
*) hash_table_find(variable_context
, dv
->var
);
488 assert(!"Should not get here.");
492 return store
!= NULL
;
497 ir_rvalue::constant_expression_value(struct hash_table
*)
499 assert(this->type
->is_error());
504 ir_expression::constant_expression_value(struct hash_table
*variable_context
)
506 if (this->type
->is_error())
509 ir_constant
*op
[Elements(this->operands
)] = { NULL
, };
510 ir_constant_data data
;
512 memset(&data
, 0, sizeof(data
));
514 for (unsigned operand
= 0; operand
< this->get_num_operands(); operand
++) {
515 op
[operand
] = this->operands
[operand
]->constant_expression_value(variable_context
);
521 switch (this->operation
) {
522 case ir_binop_lshift
:
523 case ir_binop_rshift
:
525 case ir_binop_interpolate_at_offset
:
526 case ir_binop_interpolate_at_sample
:
527 case ir_binop_vector_extract
:
529 case ir_triop_bitfield_extract
:
533 assert(op
[0]->type
->base_type
== op
[1]->type
->base_type
);
537 bool op0_scalar
= op
[0]->type
->is_scalar();
538 bool op1_scalar
= op
[1] != NULL
&& op
[1]->type
->is_scalar();
540 /* When iterating over a vector or matrix's components, we want to increase
541 * the loop counter. However, for scalars, we want to stay at 0.
543 unsigned c0_inc
= op0_scalar
? 0 : 1;
544 unsigned c1_inc
= op1_scalar
? 0 : 1;
546 if (op1_scalar
|| !op
[1]) {
547 components
= op
[0]->type
->components();
549 components
= op
[1]->type
->components();
552 void *ctx
= ralloc_parent(this);
554 /* Handle array operations here, rather than below. */
555 if (op
[0]->type
->is_array()) {
556 assert(op
[1] != NULL
&& op
[1]->type
->is_array());
557 switch (this->operation
) {
558 case ir_binop_all_equal
:
559 return new(ctx
) ir_constant(op
[0]->has_value(op
[1]));
560 case ir_binop_any_nequal
:
561 return new(ctx
) ir_constant(!op
[0]->has_value(op
[1]));
568 switch (this->operation
) {
569 case ir_unop_bit_not
:
570 switch (op
[0]->type
->base_type
) {
572 for (unsigned c
= 0; c
< components
; c
++)
573 data
.i
[c
] = ~ op
[0]->value
.i
[c
];
576 for (unsigned c
= 0; c
< components
; c
++)
577 data
.u
[c
] = ~ op
[0]->value
.u
[c
];
584 case ir_unop_logic_not
:
585 assert(op
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
586 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++)
587 data
.b
[c
] = !op
[0]->value
.b
[c
];
591 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
592 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
593 data
.i
[c
] = (int) op
[0]->value
.f
[c
];
597 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
598 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
599 data
.i
[c
] = (unsigned) op
[0]->value
.f
[c
];
603 assert(op
[0]->type
->base_type
== GLSL_TYPE_INT
);
604 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
605 data
.f
[c
] = (float) op
[0]->value
.i
[c
];
609 assert(op
[0]->type
->base_type
== GLSL_TYPE_UINT
);
610 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
611 data
.f
[c
] = (float) op
[0]->value
.u
[c
];
615 assert(op
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
616 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
617 data
.f
[c
] = op
[0]->value
.b
[c
] ? 1.0F
: 0.0F
;
621 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
622 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
623 data
.b
[c
] = op
[0]->value
.f
[c
] != 0.0F
? true : false;
627 assert(op
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
628 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
629 data
.u
[c
] = op
[0]->value
.b
[c
] ? 1 : 0;
633 assert(op
[0]->type
->is_integer());
634 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
635 data
.b
[c
] = op
[0]->value
.u
[c
] ? true : false;
639 assert(op
[0]->type
->base_type
== GLSL_TYPE_UINT
);
640 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
641 data
.i
[c
] = op
[0]->value
.u
[c
];
645 assert(op
[0]->type
->base_type
== GLSL_TYPE_INT
);
646 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
647 data
.u
[c
] = op
[0]->value
.i
[c
];
650 case ir_unop_bitcast_i2f
:
651 assert(op
[0]->type
->base_type
== GLSL_TYPE_INT
);
652 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
653 data
.f
[c
] = bitcast_u2f(op
[0]->value
.i
[c
]);
656 case ir_unop_bitcast_f2i
:
657 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
658 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
659 data
.i
[c
] = bitcast_f2u(op
[0]->value
.f
[c
]);
662 case ir_unop_bitcast_u2f
:
663 assert(op
[0]->type
->base_type
== GLSL_TYPE_UINT
);
664 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
665 data
.f
[c
] = bitcast_u2f(op
[0]->value
.u
[c
]);
668 case ir_unop_bitcast_f2u
:
669 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
670 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
671 data
.u
[c
] = bitcast_f2u(op
[0]->value
.f
[c
]);
675 assert(op
[0]->type
->is_boolean());
677 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
678 if (op
[0]->value
.b
[c
])
683 assert(op
[0]->type
->base_type
== GLSL_TYPE_DOUBLE
);
684 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
685 data
.f
[c
] = op
[0]->value
.d
[c
];
689 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
690 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
691 data
.d
[c
] = op
[0]->value
.f
[c
];
695 assert(op
[0]->type
->base_type
== GLSL_TYPE_DOUBLE
);
696 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
697 data
.i
[c
] = op
[0]->value
.d
[c
];
701 assert(op
[0]->type
->base_type
== GLSL_TYPE_INT
);
702 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
703 data
.d
[c
] = op
[0]->value
.i
[c
];
707 assert(op
[0]->type
->base_type
== GLSL_TYPE_DOUBLE
);
708 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
709 data
.u
[c
] = op
[0]->value
.d
[c
];
713 assert(op
[0]->type
->base_type
== GLSL_TYPE_UINT
);
714 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
715 data
.d
[c
] = op
[0]->value
.u
[c
];
719 assert(op
[0]->type
->base_type
== GLSL_TYPE_DOUBLE
);
720 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
721 data
.b
[c
] = op
[0]->value
.d
[c
] != 0.0;
725 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
726 if (op
[0]->type
->base_type
== GLSL_TYPE_DOUBLE
)
727 data
.d
[c
] = trunc(op
[0]->value
.d
[c
]);
729 data
.f
[c
] = truncf(op
[0]->value
.f
[c
]);
733 case ir_unop_round_even
:
734 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
735 if (op
[0]->type
->base_type
== GLSL_TYPE_DOUBLE
)
736 data
.d
[c
] = _mesa_round_to_even(op
[0]->value
.d
[c
]);
738 data
.f
[c
] = _mesa_round_to_even(op
[0]->value
.f
[c
]);
743 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
744 if (op
[0]->type
->base_type
== GLSL_TYPE_DOUBLE
)
745 data
.d
[c
] = ceil(op
[0]->value
.d
[c
]);
747 data
.f
[c
] = ceilf(op
[0]->value
.f
[c
]);
752 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
753 if (op
[0]->type
->base_type
== GLSL_TYPE_DOUBLE
)
754 data
.d
[c
] = floor(op
[0]->value
.d
[c
]);
756 data
.f
[c
] = floorf(op
[0]->value
.f
[c
]);
761 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
762 switch (this->type
->base_type
) {
769 case GLSL_TYPE_FLOAT
:
770 data
.f
[c
] = op
[0]->value
.f
[c
] - floor(op
[0]->value
.f
[c
]);
772 case GLSL_TYPE_DOUBLE
:
773 data
.d
[c
] = op
[0]->value
.d
[c
] - floor(op
[0]->value
.d
[c
]);
782 case ir_unop_sin_reduced
:
783 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
784 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
785 data
.f
[c
] = sinf(op
[0]->value
.f
[c
]);
790 case ir_unop_cos_reduced
:
791 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
792 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
793 data
.f
[c
] = cosf(op
[0]->value
.f
[c
]);
798 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
799 switch (this->type
->base_type
) {
801 data
.u
[c
] = -((int) op
[0]->value
.u
[c
]);
804 data
.i
[c
] = -op
[0]->value
.i
[c
];
806 case GLSL_TYPE_FLOAT
:
807 data
.f
[c
] = -op
[0]->value
.f
[c
];
809 case GLSL_TYPE_DOUBLE
:
810 data
.d
[c
] = -op
[0]->value
.d
[c
];
819 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
820 switch (this->type
->base_type
) {
822 data
.u
[c
] = op
[0]->value
.u
[c
];
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
.u
[c
] = op
[0]->value
.i
[c
] > 0;
848 data
.i
[c
] = (op
[0]->value
.i
[c
] > 0) - (op
[0]->value
.i
[c
] < 0);
850 case GLSL_TYPE_FLOAT
:
851 data
.f
[c
] = float((op
[0]->value
.f
[c
] > 0)-(op
[0]->value
.f
[c
] < 0));
853 case GLSL_TYPE_DOUBLE
:
854 data
.d
[c
] = double((op
[0]->value
.d
[c
] > 0)-(op
[0]->value
.d
[c
] < 0));
863 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
864 switch (this->type
->base_type
) {
866 if (op
[0]->value
.u
[c
] != 0.0)
867 data
.u
[c
] = 1 / op
[0]->value
.u
[c
];
870 if (op
[0]->value
.i
[c
] != 0.0)
871 data
.i
[c
] = 1 / op
[0]->value
.i
[c
];
873 case GLSL_TYPE_FLOAT
:
874 if (op
[0]->value
.f
[c
] != 0.0)
875 data
.f
[c
] = 1.0F
/ op
[0]->value
.f
[c
];
877 case GLSL_TYPE_DOUBLE
:
878 if (op
[0]->value
.d
[c
] != 0.0)
879 data
.d
[c
] = 1.0 / op
[0]->value
.d
[c
];
888 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
889 if (op
[0]->type
->base_type
== GLSL_TYPE_DOUBLE
)
890 data
.d
[c
] = 1.0 / sqrt(op
[0]->value
.d
[c
]);
892 data
.f
[c
] = 1.0F
/ sqrtf(op
[0]->value
.f
[c
]);
897 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
898 if (op
[0]->type
->base_type
== GLSL_TYPE_DOUBLE
)
899 data
.d
[c
] = sqrt(op
[0]->value
.d
[c
]);
901 data
.f
[c
] = sqrtf(op
[0]->value
.f
[c
]);
906 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
907 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
908 data
.f
[c
] = expf(op
[0]->value
.f
[c
]);
913 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
914 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
915 data
.f
[c
] = exp2f(op
[0]->value
.f
[c
]);
920 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
921 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
922 data
.f
[c
] = logf(op
[0]->value
.f
[c
]);
927 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
928 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
929 data
.f
[c
] = log2f(op
[0]->value
.f
[c
]);
934 case ir_unop_dFdx_coarse
:
935 case ir_unop_dFdx_fine
:
937 case ir_unop_dFdy_coarse
:
938 case ir_unop_dFdy_fine
:
939 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
940 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
945 case ir_unop_pack_snorm_2x16
:
946 assert(op
[0]->type
== glsl_type::vec2_type
);
947 data
.u
[0] = pack_2x16(pack_snorm_1x16
,
951 case ir_unop_pack_snorm_4x8
:
952 assert(op
[0]->type
== glsl_type::vec4_type
);
953 data
.u
[0] = pack_4x8(pack_snorm_1x8
,
959 case ir_unop_unpack_snorm_2x16
:
960 assert(op
[0]->type
== glsl_type::uint_type
);
961 unpack_2x16(unpack_snorm_1x16
,
963 &data
.f
[0], &data
.f
[1]);
965 case ir_unop_unpack_snorm_4x8
:
966 assert(op
[0]->type
== glsl_type::uint_type
);
967 unpack_4x8(unpack_snorm_1x8
,
969 &data
.f
[0], &data
.f
[1], &data
.f
[2], &data
.f
[3]);
971 case ir_unop_pack_unorm_2x16
:
972 assert(op
[0]->type
== glsl_type::vec2_type
);
973 data
.u
[0] = pack_2x16(pack_unorm_1x16
,
977 case ir_unop_pack_unorm_4x8
:
978 assert(op
[0]->type
== glsl_type::vec4_type
);
979 data
.u
[0] = pack_4x8(pack_unorm_1x8
,
985 case ir_unop_unpack_unorm_2x16
:
986 assert(op
[0]->type
== glsl_type::uint_type
);
987 unpack_2x16(unpack_unorm_1x16
,
989 &data
.f
[0], &data
.f
[1]);
991 case ir_unop_unpack_unorm_4x8
:
992 assert(op
[0]->type
== glsl_type::uint_type
);
993 unpack_4x8(unpack_unorm_1x8
,
995 &data
.f
[0], &data
.f
[1], &data
.f
[2], &data
.f
[3]);
997 case ir_unop_pack_half_2x16
:
998 assert(op
[0]->type
== glsl_type::vec2_type
);
999 data
.u
[0] = pack_2x16(pack_half_1x16
,
1003 case ir_unop_unpack_half_2x16
:
1004 assert(op
[0]->type
== glsl_type::uint_type
);
1005 unpack_2x16(unpack_half_1x16
,
1007 &data
.f
[0], &data
.f
[1]);
1010 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
1011 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
1012 data
.f
[c
] = powf(op
[0]->value
.f
[c
], op
[1]->value
.f
[c
]);
1017 if (op
[0]->type
->base_type
== GLSL_TYPE_DOUBLE
)
1018 data
.d
[0] = dot_d(op
[0], op
[1]);
1020 data
.f
[0] = dot_f(op
[0], op
[1]);
1024 assert(op
[0]->type
== op
[1]->type
|| op0_scalar
|| op1_scalar
);
1025 for (unsigned c
= 0, c0
= 0, c1
= 0;
1027 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
1029 switch (op
[0]->type
->base_type
) {
1030 case GLSL_TYPE_UINT
:
1031 data
.u
[c
] = MIN2(op
[0]->value
.u
[c0
], op
[1]->value
.u
[c1
]);
1034 data
.i
[c
] = MIN2(op
[0]->value
.i
[c0
], op
[1]->value
.i
[c1
]);
1036 case GLSL_TYPE_FLOAT
:
1037 data
.f
[c
] = MIN2(op
[0]->value
.f
[c0
], op
[1]->value
.f
[c1
]);
1039 case GLSL_TYPE_DOUBLE
:
1040 data
.d
[c
] = MIN2(op
[0]->value
.d
[c0
], op
[1]->value
.d
[c1
]);
1049 assert(op
[0]->type
== op
[1]->type
|| op0_scalar
|| op1_scalar
);
1050 for (unsigned c
= 0, c0
= 0, c1
= 0;
1052 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
1054 switch (op
[0]->type
->base_type
) {
1055 case GLSL_TYPE_UINT
:
1056 data
.u
[c
] = MAX2(op
[0]->value
.u
[c0
], op
[1]->value
.u
[c1
]);
1059 data
.i
[c
] = MAX2(op
[0]->value
.i
[c0
], op
[1]->value
.i
[c1
]);
1061 case GLSL_TYPE_FLOAT
:
1062 data
.f
[c
] = MAX2(op
[0]->value
.f
[c0
], op
[1]->value
.f
[c1
]);
1064 case GLSL_TYPE_DOUBLE
:
1065 data
.d
[c
] = MAX2(op
[0]->value
.d
[c0
], op
[1]->value
.d
[c1
]);
1074 assert(op
[0]->type
== op
[1]->type
|| op0_scalar
|| op1_scalar
);
1075 for (unsigned c
= 0, c0
= 0, c1
= 0;
1077 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
1079 switch (op
[0]->type
->base_type
) {
1080 case GLSL_TYPE_UINT
:
1081 data
.u
[c
] = op
[0]->value
.u
[c0
] + op
[1]->value
.u
[c1
];
1084 data
.i
[c
] = op
[0]->value
.i
[c0
] + op
[1]->value
.i
[c1
];
1086 case GLSL_TYPE_FLOAT
:
1087 data
.f
[c
] = op
[0]->value
.f
[c0
] + op
[1]->value
.f
[c1
];
1089 case GLSL_TYPE_DOUBLE
:
1090 data
.d
[c
] = op
[0]->value
.d
[c0
] + op
[1]->value
.d
[c1
];
1099 assert(op
[0]->type
== op
[1]->type
|| op0_scalar
|| op1_scalar
);
1100 for (unsigned c
= 0, c0
= 0, c1
= 0;
1102 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
1104 switch (op
[0]->type
->base_type
) {
1105 case GLSL_TYPE_UINT
:
1106 data
.u
[c
] = op
[0]->value
.u
[c0
] - op
[1]->value
.u
[c1
];
1109 data
.i
[c
] = op
[0]->value
.i
[c0
] - op
[1]->value
.i
[c1
];
1111 case GLSL_TYPE_FLOAT
:
1112 data
.f
[c
] = op
[0]->value
.f
[c0
] - op
[1]->value
.f
[c1
];
1114 case GLSL_TYPE_DOUBLE
:
1115 data
.d
[c
] = op
[0]->value
.d
[c0
] - op
[1]->value
.d
[c1
];
1124 /* Check for equal types, or unequal types involving scalars */
1125 if ((op
[0]->type
== op
[1]->type
&& !op
[0]->type
->is_matrix())
1126 || op0_scalar
|| op1_scalar
) {
1127 for (unsigned c
= 0, c0
= 0, c1
= 0;
1129 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
1131 switch (op
[0]->type
->base_type
) {
1132 case GLSL_TYPE_UINT
:
1133 data
.u
[c
] = op
[0]->value
.u
[c0
] * op
[1]->value
.u
[c1
];
1136 data
.i
[c
] = op
[0]->value
.i
[c0
] * op
[1]->value
.i
[c1
];
1138 case GLSL_TYPE_FLOAT
:
1139 data
.f
[c
] = op
[0]->value
.f
[c0
] * op
[1]->value
.f
[c1
];
1141 case GLSL_TYPE_DOUBLE
:
1142 data
.d
[c
] = op
[0]->value
.d
[c0
] * op
[1]->value
.d
[c1
];
1149 assert(op
[0]->type
->is_matrix() || op
[1]->type
->is_matrix());
1151 /* Multiply an N-by-M matrix with an M-by-P matrix. Since either
1152 * matrix can be a GLSL vector, either N or P can be 1.
1154 * For vec*mat, the vector is treated as a row vector. This
1155 * means the vector is a 1-row x M-column matrix.
1157 * For mat*vec, the vector is treated as a column vector. Since
1158 * matrix_columns is 1 for vectors, this just works.
1160 const unsigned n
= op
[0]->type
->is_vector()
1161 ? 1 : op
[0]->type
->vector_elements
;
1162 const unsigned m
= op
[1]->type
->vector_elements
;
1163 const unsigned p
= op
[1]->type
->matrix_columns
;
1164 for (unsigned j
= 0; j
< p
; j
++) {
1165 for (unsigned i
= 0; i
< n
; i
++) {
1166 for (unsigned k
= 0; k
< m
; k
++) {
1167 if (op
[0]->type
->base_type
== GLSL_TYPE_DOUBLE
)
1168 data
.d
[i
+n
*j
] += op
[0]->value
.d
[i
+n
*k
]*op
[1]->value
.d
[k
+m
*j
];
1170 data
.f
[i
+n
*j
] += op
[0]->value
.f
[i
+n
*k
]*op
[1]->value
.f
[k
+m
*j
];
1178 /* FINISHME: Emit warning when division-by-zero is detected. */
1179 assert(op
[0]->type
== op
[1]->type
|| op0_scalar
|| op1_scalar
);
1180 for (unsigned c
= 0, c0
= 0, c1
= 0;
1182 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
1184 switch (op
[0]->type
->base_type
) {
1185 case GLSL_TYPE_UINT
:
1186 if (op
[1]->value
.u
[c1
] == 0) {
1189 data
.u
[c
] = op
[0]->value
.u
[c0
] / op
[1]->value
.u
[c1
];
1193 if (op
[1]->value
.i
[c1
] == 0) {
1196 data
.i
[c
] = op
[0]->value
.i
[c0
] / op
[1]->value
.i
[c1
];
1199 case GLSL_TYPE_FLOAT
:
1200 data
.f
[c
] = op
[0]->value
.f
[c0
] / op
[1]->value
.f
[c1
];
1202 case GLSL_TYPE_DOUBLE
:
1203 data
.d
[c
] = op
[0]->value
.d
[c0
] / op
[1]->value
.d
[c1
];
1212 /* FINISHME: Emit warning when division-by-zero is detected. */
1213 assert(op
[0]->type
== op
[1]->type
|| op0_scalar
|| op1_scalar
);
1214 for (unsigned c
= 0, c0
= 0, c1
= 0;
1216 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
1218 switch (op
[0]->type
->base_type
) {
1219 case GLSL_TYPE_UINT
:
1220 if (op
[1]->value
.u
[c1
] == 0) {
1223 data
.u
[c
] = op
[0]->value
.u
[c0
] % op
[1]->value
.u
[c1
];
1227 if (op
[1]->value
.i
[c1
] == 0) {
1230 data
.i
[c
] = op
[0]->value
.i
[c0
] % op
[1]->value
.i
[c1
];
1233 case GLSL_TYPE_FLOAT
:
1234 /* We don't use fmod because it rounds toward zero; GLSL specifies
1237 data
.f
[c
] = op
[0]->value
.f
[c0
] - op
[1]->value
.f
[c1
]
1238 * floorf(op
[0]->value
.f
[c0
] / op
[1]->value
.f
[c1
]);
1240 case GLSL_TYPE_DOUBLE
:
1241 /* We don't use fmod because it rounds toward zero; GLSL specifies
1244 data
.d
[c
] = op
[0]->value
.d
[c0
] - op
[1]->value
.d
[c1
]
1245 * floor(op
[0]->value
.d
[c0
] / op
[1]->value
.d
[c1
]);
1254 case ir_binop_logic_and
:
1255 assert(op
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
1256 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++)
1257 data
.b
[c
] = op
[0]->value
.b
[c
] && op
[1]->value
.b
[c
];
1259 case ir_binop_logic_xor
:
1260 assert(op
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
1261 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++)
1262 data
.b
[c
] = op
[0]->value
.b
[c
] ^ op
[1]->value
.b
[c
];
1264 case ir_binop_logic_or
:
1265 assert(op
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
1266 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++)
1267 data
.b
[c
] = op
[0]->value
.b
[c
] || op
[1]->value
.b
[c
];
1271 assert(op
[0]->type
== op
[1]->type
);
1272 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
1273 switch (op
[0]->type
->base_type
) {
1274 case GLSL_TYPE_UINT
:
1275 data
.b
[c
] = op
[0]->value
.u
[c
] < op
[1]->value
.u
[c
];
1278 data
.b
[c
] = op
[0]->value
.i
[c
] < op
[1]->value
.i
[c
];
1280 case GLSL_TYPE_FLOAT
:
1281 data
.b
[c
] = op
[0]->value
.f
[c
] < op
[1]->value
.f
[c
];
1283 case GLSL_TYPE_DOUBLE
:
1284 data
.b
[c
] = op
[0]->value
.d
[c
] < op
[1]->value
.d
[c
];
1291 case ir_binop_greater
:
1292 assert(op
[0]->type
== op
[1]->type
);
1293 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
1294 switch (op
[0]->type
->base_type
) {
1295 case GLSL_TYPE_UINT
:
1296 data
.b
[c
] = op
[0]->value
.u
[c
] > op
[1]->value
.u
[c
];
1299 data
.b
[c
] = op
[0]->value
.i
[c
] > op
[1]->value
.i
[c
];
1301 case GLSL_TYPE_FLOAT
:
1302 data
.b
[c
] = op
[0]->value
.f
[c
] > op
[1]->value
.f
[c
];
1304 case GLSL_TYPE_DOUBLE
:
1305 data
.b
[c
] = op
[0]->value
.d
[c
] > op
[1]->value
.d
[c
];
1312 case ir_binop_lequal
:
1313 assert(op
[0]->type
== op
[1]->type
);
1314 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
1315 switch (op
[0]->type
->base_type
) {
1316 case GLSL_TYPE_UINT
:
1317 data
.b
[c
] = op
[0]->value
.u
[c
] <= op
[1]->value
.u
[c
];
1320 data
.b
[c
] = op
[0]->value
.i
[c
] <= op
[1]->value
.i
[c
];
1322 case GLSL_TYPE_FLOAT
:
1323 data
.b
[c
] = op
[0]->value
.f
[c
] <= op
[1]->value
.f
[c
];
1325 case GLSL_TYPE_DOUBLE
:
1326 data
.b
[c
] = op
[0]->value
.d
[c
] <= op
[1]->value
.d
[c
];
1333 case ir_binop_gequal
:
1334 assert(op
[0]->type
== op
[1]->type
);
1335 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
1336 switch (op
[0]->type
->base_type
) {
1337 case GLSL_TYPE_UINT
:
1338 data
.b
[c
] = op
[0]->value
.u
[c
] >= op
[1]->value
.u
[c
];
1341 data
.b
[c
] = op
[0]->value
.i
[c
] >= op
[1]->value
.i
[c
];
1343 case GLSL_TYPE_FLOAT
:
1344 data
.b
[c
] = op
[0]->value
.f
[c
] >= op
[1]->value
.f
[c
];
1346 case GLSL_TYPE_DOUBLE
:
1347 data
.b
[c
] = op
[0]->value
.d
[c
] >= op
[1]->value
.d
[c
];
1354 case ir_binop_equal
:
1355 assert(op
[0]->type
== op
[1]->type
);
1356 for (unsigned c
= 0; c
< components
; c
++) {
1357 switch (op
[0]->type
->base_type
) {
1358 case GLSL_TYPE_UINT
:
1359 data
.b
[c
] = op
[0]->value
.u
[c
] == op
[1]->value
.u
[c
];
1362 data
.b
[c
] = op
[0]->value
.i
[c
] == op
[1]->value
.i
[c
];
1364 case GLSL_TYPE_FLOAT
:
1365 data
.b
[c
] = op
[0]->value
.f
[c
] == op
[1]->value
.f
[c
];
1367 case GLSL_TYPE_BOOL
:
1368 data
.b
[c
] = op
[0]->value
.b
[c
] == op
[1]->value
.b
[c
];
1370 case GLSL_TYPE_DOUBLE
:
1371 data
.b
[c
] = op
[0]->value
.d
[c
] == op
[1]->value
.d
[c
];
1378 case ir_binop_nequal
:
1379 assert(op
[0]->type
== op
[1]->type
);
1380 for (unsigned c
= 0; c
< components
; c
++) {
1381 switch (op
[0]->type
->base_type
) {
1382 case GLSL_TYPE_UINT
:
1383 data
.b
[c
] = op
[0]->value
.u
[c
] != op
[1]->value
.u
[c
];
1386 data
.b
[c
] = op
[0]->value
.i
[c
] != op
[1]->value
.i
[c
];
1388 case GLSL_TYPE_FLOAT
:
1389 data
.b
[c
] = op
[0]->value
.f
[c
] != op
[1]->value
.f
[c
];
1391 case GLSL_TYPE_BOOL
:
1392 data
.b
[c
] = op
[0]->value
.b
[c
] != op
[1]->value
.b
[c
];
1394 case GLSL_TYPE_DOUBLE
:
1395 data
.b
[c
] = op
[0]->value
.d
[c
] != op
[1]->value
.d
[c
];
1402 case ir_binop_all_equal
:
1403 data
.b
[0] = op
[0]->has_value(op
[1]);
1405 case ir_binop_any_nequal
:
1406 data
.b
[0] = !op
[0]->has_value(op
[1]);
1409 case ir_binop_lshift
:
1410 for (unsigned c
= 0, c0
= 0, c1
= 0;
1412 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
1414 if (op
[0]->type
->base_type
== GLSL_TYPE_INT
&&
1415 op
[1]->type
->base_type
== GLSL_TYPE_INT
) {
1416 data
.i
[c
] = op
[0]->value
.i
[c0
] << op
[1]->value
.i
[c1
];
1418 } else if (op
[0]->type
->base_type
== GLSL_TYPE_INT
&&
1419 op
[1]->type
->base_type
== GLSL_TYPE_UINT
) {
1420 data
.i
[c
] = op
[0]->value
.i
[c0
] << op
[1]->value
.u
[c1
];
1422 } else if (op
[0]->type
->base_type
== GLSL_TYPE_UINT
&&
1423 op
[1]->type
->base_type
== GLSL_TYPE_INT
) {
1424 data
.u
[c
] = op
[0]->value
.u
[c0
] << op
[1]->value
.i
[c1
];
1426 } else if (op
[0]->type
->base_type
== GLSL_TYPE_UINT
&&
1427 op
[1]->type
->base_type
== GLSL_TYPE_UINT
) {
1428 data
.u
[c
] = op
[0]->value
.u
[c0
] << op
[1]->value
.u
[c1
];
1433 case ir_binop_rshift
:
1434 for (unsigned c
= 0, c0
= 0, c1
= 0;
1436 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
1438 if (op
[0]->type
->base_type
== GLSL_TYPE_INT
&&
1439 op
[1]->type
->base_type
== GLSL_TYPE_INT
) {
1440 data
.i
[c
] = op
[0]->value
.i
[c0
] >> op
[1]->value
.i
[c1
];
1442 } else if (op
[0]->type
->base_type
== GLSL_TYPE_INT
&&
1443 op
[1]->type
->base_type
== GLSL_TYPE_UINT
) {
1444 data
.i
[c
] = op
[0]->value
.i
[c0
] >> op
[1]->value
.u
[c1
];
1446 } else if (op
[0]->type
->base_type
== GLSL_TYPE_UINT
&&
1447 op
[1]->type
->base_type
== GLSL_TYPE_INT
) {
1448 data
.u
[c
] = op
[0]->value
.u
[c0
] >> op
[1]->value
.i
[c1
];
1450 } else if (op
[0]->type
->base_type
== GLSL_TYPE_UINT
&&
1451 op
[1]->type
->base_type
== GLSL_TYPE_UINT
) {
1452 data
.u
[c
] = op
[0]->value
.u
[c0
] >> op
[1]->value
.u
[c1
];
1457 case ir_binop_bit_and
:
1458 for (unsigned c
= 0, c0
= 0, c1
= 0;
1460 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
1462 switch (op
[0]->type
->base_type
) {
1464 data
.i
[c
] = op
[0]->value
.i
[c0
] & op
[1]->value
.i
[c1
];
1466 case GLSL_TYPE_UINT
:
1467 data
.u
[c
] = op
[0]->value
.u
[c0
] & op
[1]->value
.u
[c1
];
1475 case ir_binop_bit_or
:
1476 for (unsigned c
= 0, c0
= 0, c1
= 0;
1478 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
1480 switch (op
[0]->type
->base_type
) {
1482 data
.i
[c
] = op
[0]->value
.i
[c0
] | op
[1]->value
.i
[c1
];
1484 case GLSL_TYPE_UINT
:
1485 data
.u
[c
] = op
[0]->value
.u
[c0
] | op
[1]->value
.u
[c1
];
1493 case ir_binop_vector_extract
: {
1494 const int c
= CLAMP(op
[1]->value
.i
[0], 0,
1495 (int) op
[0]->type
->vector_elements
- 1);
1497 switch (op
[0]->type
->base_type
) {
1498 case GLSL_TYPE_UINT
:
1499 data
.u
[0] = op
[0]->value
.u
[c
];
1502 data
.i
[0] = op
[0]->value
.i
[c
];
1504 case GLSL_TYPE_FLOAT
:
1505 data
.f
[0] = op
[0]->value
.f
[c
];
1507 case GLSL_TYPE_DOUBLE
:
1508 data
.d
[0] = op
[0]->value
.d
[c
];
1510 case GLSL_TYPE_BOOL
:
1511 data
.b
[0] = op
[0]->value
.b
[c
];
1519 case ir_binop_bit_xor
:
1520 for (unsigned c
= 0, c0
= 0, c1
= 0;
1522 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
1524 switch (op
[0]->type
->base_type
) {
1526 data
.i
[c
] = op
[0]->value
.i
[c0
] ^ op
[1]->value
.i
[c1
];
1528 case GLSL_TYPE_UINT
:
1529 data
.u
[c
] = op
[0]->value
.u
[c0
] ^ op
[1]->value
.u
[c1
];
1537 case ir_unop_bitfield_reverse
:
1538 /* http://graphics.stanford.edu/~seander/bithacks.html#BitReverseObvious */
1539 for (unsigned c
= 0; c
< components
; c
++) {
1540 unsigned int v
= op
[0]->value
.u
[c
]; // input bits to be reversed
1541 unsigned int r
= v
; // r will be reversed bits of v; first get LSB of v
1542 int s
= sizeof(v
) * CHAR_BIT
- 1; // extra shift needed at end
1544 for (v
>>= 1; v
; v
>>= 1) {
1549 r
<<= s
; // shift when v's highest bits are zero
1555 case ir_unop_bit_count
:
1556 for (unsigned c
= 0; c
< components
; c
++) {
1558 unsigned v
= op
[0]->value
.u
[c
];
1560 for (; v
; count
++) {
1567 case ir_unop_find_msb
:
1568 for (unsigned c
= 0; c
< components
; c
++) {
1569 int v
= op
[0]->value
.i
[c
];
1571 if (v
== 0 || (op
[0]->type
->base_type
== GLSL_TYPE_INT
&& v
== -1))
1575 int top_bit
= op
[0]->type
->base_type
== GLSL_TYPE_UINT
1576 ? 0 : v
& (1 << 31);
1578 while (((v
& (1 << 31)) == top_bit
) && count
!= 32) {
1583 data
.i
[c
] = 31 - count
;
1588 case ir_unop_find_lsb
:
1589 for (unsigned c
= 0; c
< components
; c
++) {
1590 if (op
[0]->value
.i
[c
] == 0)
1594 unsigned v
= op
[0]->value
.u
[c
];
1596 for (; !(v
& 1); v
>>= 1) {
1604 case ir_unop_saturate
:
1605 for (unsigned c
= 0; c
< components
; c
++) {
1606 data
.f
[c
] = CLAMP(op
[0]->value
.f
[c
], 0.0f
, 1.0f
);
1609 case ir_unop_pack_double_2x32
: {
1610 /* XXX needs to be checked on big-endian */
1612 temp
= (uint64_t)op
[0]->value
.u
[0] | ((uint64_t)op
[0]->value
.u
[1] << 32);
1613 data
.d
[0] = *(double *)&temp
;
1617 case ir_unop_unpack_double_2x32
:
1618 /* XXX needs to be checked on big-endian */
1619 data
.u
[0] = *(uint32_t *)&op
[0]->value
.d
[0];
1620 data
.u
[1] = *((uint32_t *)&op
[0]->value
.d
[0] + 1);
1623 case ir_triop_bitfield_extract
: {
1624 int offset
= op
[1]->value
.i
[0];
1625 int bits
= op
[2]->value
.i
[0];
1627 for (unsigned c
= 0; c
< components
; c
++) {
1630 else if (offset
< 0 || bits
< 0)
1631 data
.u
[c
] = 0; /* Undefined, per spec. */
1632 else if (offset
+ bits
> 32)
1633 data
.u
[c
] = 0; /* Undefined, per spec. */
1635 if (op
[0]->type
->base_type
== GLSL_TYPE_INT
) {
1636 /* int so that the right shift will sign-extend. */
1637 int value
= op
[0]->value
.i
[c
];
1638 value
<<= 32 - bits
- offset
;
1639 value
>>= 32 - bits
;
1642 unsigned value
= op
[0]->value
.u
[c
];
1643 value
<<= 32 - bits
- offset
;
1644 value
>>= 32 - bits
;
1652 case ir_binop_bfm
: {
1653 int bits
= op
[0]->value
.i
[0];
1654 int offset
= op
[1]->value
.i
[0];
1656 for (unsigned c
= 0; c
< components
; c
++) {
1658 data
.u
[c
] = op
[0]->value
.u
[c
];
1659 else if (offset
< 0 || bits
< 0)
1660 data
.u
[c
] = 0; /* Undefined for bitfieldInsert, per spec. */
1661 else if (offset
+ bits
> 32)
1662 data
.u
[c
] = 0; /* Undefined for bitfieldInsert, per spec. */
1664 data
.u
[c
] = ((1 << bits
) - 1) << offset
;
1669 case ir_binop_ldexp
:
1670 for (unsigned c
= 0; c
< components
; c
++) {
1671 if (op
[0]->type
->base_type
== GLSL_TYPE_DOUBLE
) {
1672 data
.d
[c
] = ldexp(op
[0]->value
.d
[c
], op
[1]->value
.i
[c
]);
1673 /* Flush subnormal values to zero. */
1674 if (!isnormal(data
.d
[c
]))
1675 data
.d
[c
] = copysign(0.0, op
[0]->value
.d
[c
]);
1677 data
.f
[c
] = ldexp(op
[0]->value
.f
[c
], op
[1]->value
.i
[c
]);
1678 /* Flush subnormal values to zero. */
1679 if (!isnormal(data
.f
[c
]))
1680 data
.f
[c
] = copysign(0.0f
, op
[0]->value
.f
[c
]);
1686 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
||
1687 op
[0]->type
->base_type
== GLSL_TYPE_DOUBLE
);
1688 assert(op
[1]->type
->base_type
== GLSL_TYPE_FLOAT
||
1689 op
[1]->type
->base_type
== GLSL_TYPE_DOUBLE
);
1690 assert(op
[2]->type
->base_type
== GLSL_TYPE_FLOAT
||
1691 op
[2]->type
->base_type
== GLSL_TYPE_DOUBLE
);
1693 for (unsigned c
= 0; c
< components
; c
++) {
1694 if (op
[0]->type
->base_type
== GLSL_TYPE_DOUBLE
)
1695 data
.d
[c
] = op
[0]->value
.d
[c
] * op
[1]->value
.d
[c
]
1696 + op
[2]->value
.d
[c
];
1698 data
.f
[c
] = op
[0]->value
.f
[c
] * op
[1]->value
.f
[c
]
1699 + op
[2]->value
.f
[c
];
1703 case ir_triop_lrp
: {
1704 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
||
1705 op
[0]->type
->base_type
== GLSL_TYPE_DOUBLE
);
1706 assert(op
[1]->type
->base_type
== GLSL_TYPE_FLOAT
||
1707 op
[1]->type
->base_type
== GLSL_TYPE_DOUBLE
);
1708 assert(op
[2]->type
->base_type
== GLSL_TYPE_FLOAT
||
1709 op
[2]->type
->base_type
== GLSL_TYPE_DOUBLE
);
1711 unsigned c2_inc
= op
[2]->type
->is_scalar() ? 0 : 1;
1712 for (unsigned c
= 0, c2
= 0; c
< components
; c2
+= c2_inc
, c
++) {
1713 if (op
[0]->type
->base_type
== GLSL_TYPE_DOUBLE
)
1714 data
.d
[c
] = op
[0]->value
.d
[c
] * (1.0 - op
[2]->value
.d
[c2
]) +
1715 (op
[1]->value
.d
[c
] * op
[2]->value
.d
[c2
]);
1717 data
.f
[c
] = op
[0]->value
.f
[c
] * (1.0f
- op
[2]->value
.f
[c2
]) +
1718 (op
[1]->value
.f
[c
] * op
[2]->value
.f
[c2
]);
1724 for (unsigned c
= 0; c
< components
; c
++) {
1725 if (op
[1]->type
->base_type
== GLSL_TYPE_DOUBLE
)
1726 data
.d
[c
] = op
[0]->value
.b
[c
] ? op
[1]->value
.d
[c
]
1727 : op
[2]->value
.d
[c
];
1729 data
.u
[c
] = op
[0]->value
.b
[c
] ? op
[1]->value
.u
[c
]
1730 : op
[2]->value
.u
[c
];
1734 case ir_triop_vector_insert
: {
1735 const unsigned idx
= op
[2]->value
.u
[0];
1737 memcpy(&data
, &op
[0]->value
, sizeof(data
));
1739 switch (this->type
->base_type
) {
1741 data
.i
[idx
] = op
[1]->value
.i
[0];
1743 case GLSL_TYPE_UINT
:
1744 data
.u
[idx
] = op
[1]->value
.u
[0];
1746 case GLSL_TYPE_FLOAT
:
1747 data
.f
[idx
] = op
[1]->value
.f
[0];
1749 case GLSL_TYPE_BOOL
:
1750 data
.b
[idx
] = op
[1]->value
.b
[0];
1752 case GLSL_TYPE_DOUBLE
:
1753 data
.d
[idx
] = op
[1]->value
.d
[0];
1756 assert(!"Should not get here.");
1762 case ir_quadop_bitfield_insert
: {
1763 int offset
= op
[2]->value
.i
[0];
1764 int bits
= op
[3]->value
.i
[0];
1766 for (unsigned c
= 0; c
< components
; c
++) {
1768 data
.u
[c
] = op
[0]->value
.u
[c
];
1769 else if (offset
< 0 || bits
< 0)
1770 data
.u
[c
] = 0; /* Undefined, per spec. */
1771 else if (offset
+ bits
> 32)
1772 data
.u
[c
] = 0; /* Undefined, per spec. */
1774 unsigned insert_mask
= ((1 << bits
) - 1) << offset
;
1776 unsigned insert
= op
[1]->value
.u
[c
];
1778 insert
&= insert_mask
;
1780 unsigned base
= op
[0]->value
.u
[c
];
1781 base
&= ~insert_mask
;
1783 data
.u
[c
] = base
| insert
;
1789 case ir_quadop_vector
:
1790 for (unsigned c
= 0; c
< this->type
->vector_elements
; c
++) {
1791 switch (this->type
->base_type
) {
1793 data
.i
[c
] = op
[c
]->value
.i
[0];
1795 case GLSL_TYPE_UINT
:
1796 data
.u
[c
] = op
[c
]->value
.u
[0];
1798 case GLSL_TYPE_FLOAT
:
1799 data
.f
[c
] = op
[c
]->value
.f
[0];
1801 case GLSL_TYPE_DOUBLE
:
1802 data
.d
[c
] = op
[c
]->value
.d
[0];
1811 /* FINISHME: Should handle all expression types. */
1815 return new(ctx
) ir_constant(this->type
, &data
);
1820 ir_texture::constant_expression_value(struct hash_table
*)
1822 /* texture lookups aren't constant expressions */
1828 ir_swizzle::constant_expression_value(struct hash_table
*variable_context
)
1830 ir_constant
*v
= this->val
->constant_expression_value(variable_context
);
1833 ir_constant_data data
= { { 0 } };
1835 const unsigned swiz_idx
[4] = {
1836 this->mask
.x
, this->mask
.y
, this->mask
.z
, this->mask
.w
1839 for (unsigned i
= 0; i
< this->mask
.num_components
; i
++) {
1840 switch (v
->type
->base_type
) {
1841 case GLSL_TYPE_UINT
:
1842 case GLSL_TYPE_INT
: data
.u
[i
] = v
->value
.u
[swiz_idx
[i
]]; break;
1843 case GLSL_TYPE_FLOAT
: data
.f
[i
] = v
->value
.f
[swiz_idx
[i
]]; break;
1844 case GLSL_TYPE_BOOL
: data
.b
[i
] = v
->value
.b
[swiz_idx
[i
]]; break;
1845 case GLSL_TYPE_DOUBLE
:data
.d
[i
] = v
->value
.d
[swiz_idx
[i
]]; break;
1846 default: assert(!"Should not get here."); break;
1850 void *ctx
= ralloc_parent(this);
1851 return new(ctx
) ir_constant(this->type
, &data
);
1858 ir_dereference_variable::constant_expression_value(struct hash_table
*variable_context
)
1860 /* This may occur during compile and var->type is glsl_type::error_type */
1864 /* Give priority to the context hashtable, if it exists */
1865 if (variable_context
) {
1866 ir_constant
*value
= (ir_constant
*)hash_table_find(variable_context
, var
);
1871 /* The constant_value of a uniform variable is its initializer,
1872 * not the lifetime constant value of the uniform.
1874 if (var
->data
.mode
== ir_var_uniform
)
1877 if (!var
->constant_value
)
1880 return var
->constant_value
->clone(ralloc_parent(var
), NULL
);
1885 ir_dereference_array::constant_expression_value(struct hash_table
*variable_context
)
1887 ir_constant
*array
= this->array
->constant_expression_value(variable_context
);
1888 ir_constant
*idx
= this->array_index
->constant_expression_value(variable_context
);
1890 if ((array
!= NULL
) && (idx
!= NULL
)) {
1891 void *ctx
= ralloc_parent(this);
1892 if (array
->type
->is_matrix()) {
1893 /* Array access of a matrix results in a vector.
1895 const unsigned column
= idx
->value
.u
[0];
1897 const glsl_type
*const column_type
= array
->type
->column_type();
1899 /* Offset in the constant matrix to the first element of the column
1902 const unsigned mat_idx
= column
* column_type
->vector_elements
;
1904 ir_constant_data data
= { { 0 } };
1906 switch (column_type
->base_type
) {
1907 case GLSL_TYPE_UINT
:
1909 for (unsigned i
= 0; i
< column_type
->vector_elements
; i
++)
1910 data
.u
[i
] = array
->value
.u
[mat_idx
+ i
];
1914 case GLSL_TYPE_FLOAT
:
1915 for (unsigned i
= 0; i
< column_type
->vector_elements
; i
++)
1916 data
.f
[i
] = array
->value
.f
[mat_idx
+ i
];
1920 case GLSL_TYPE_DOUBLE
:
1921 for (unsigned i
= 0; i
< column_type
->vector_elements
; i
++)
1922 data
.d
[i
] = array
->value
.d
[mat_idx
+ i
];
1927 assert(!"Should not get here.");
1931 return new(ctx
) ir_constant(column_type
, &data
);
1932 } else if (array
->type
->is_vector()) {
1933 const unsigned component
= idx
->value
.u
[0];
1935 return new(ctx
) ir_constant(array
, component
);
1937 const unsigned index
= idx
->value
.u
[0];
1938 return array
->get_array_element(index
)->clone(ctx
, NULL
);
1946 ir_dereference_record::constant_expression_value(struct hash_table
*)
1948 ir_constant
*v
= this->record
->constant_expression_value();
1950 return (v
!= NULL
) ? v
->get_record_field(this->field
) : NULL
;
1955 ir_assignment::constant_expression_value(struct hash_table
*)
1957 /* FINISHME: Handle CEs involving assignment (return RHS) */
1963 ir_constant::constant_expression_value(struct hash_table
*)
1970 ir_call::constant_expression_value(struct hash_table
*variable_context
)
1972 return this->callee
->constant_expression_value(&this->actual_parameters
, variable_context
);
1976 bool ir_function_signature::constant_expression_evaluate_expression_list(const struct exec_list
&body
,
1977 struct hash_table
*variable_context
,
1978 ir_constant
**result
)
1980 foreach_in_list(ir_instruction
, inst
, &body
) {
1981 switch(inst
->ir_type
) {
1983 /* (declare () type symbol) */
1984 case ir_type_variable
: {
1985 ir_variable
*var
= inst
->as_variable();
1986 hash_table_insert(variable_context
, ir_constant::zero(this, var
->type
), var
);
1990 /* (assign [condition] (write-mask) (ref) (value)) */
1991 case ir_type_assignment
: {
1992 ir_assignment
*asg
= inst
->as_assignment();
1993 if (asg
->condition
) {
1994 ir_constant
*cond
= asg
->condition
->constant_expression_value(variable_context
);
1997 if (!cond
->get_bool_component(0))
2001 ir_constant
*store
= NULL
;
2004 if (!constant_referenced(asg
->lhs
, variable_context
, store
, offset
))
2007 ir_constant
*value
= asg
->rhs
->constant_expression_value(variable_context
);
2012 store
->copy_masked_offset(value
, offset
, asg
->write_mask
);
2016 /* (return (expression)) */
2017 case ir_type_return
:
2019 *result
= inst
->as_return()->value
->constant_expression_value(variable_context
);
2020 return *result
!= NULL
;
2022 /* (call name (ref) (params))*/
2023 case ir_type_call
: {
2024 ir_call
*call
= inst
->as_call();
2026 /* Just say no to void functions in constant expressions. We
2027 * don't need them at that point.
2030 if (!call
->return_deref
)
2033 ir_constant
*store
= NULL
;
2036 if (!constant_referenced(call
->return_deref
, variable_context
,
2040 ir_constant
*value
= call
->constant_expression_value(variable_context
);
2045 store
->copy_offset(value
, offset
);
2049 /* (if condition (then-instructions) (else-instructions)) */
2051 ir_if
*iif
= inst
->as_if();
2053 ir_constant
*cond
= iif
->condition
->constant_expression_value(variable_context
);
2054 if (!cond
|| !cond
->type
->is_boolean())
2057 exec_list
&branch
= cond
->get_bool_component(0) ? iif
->then_instructions
: iif
->else_instructions
;
2060 if (!constant_expression_evaluate_expression_list(branch
, variable_context
, result
))
2063 /* If there was a return in the branch chosen, drop out now. */
2070 /* Every other expression type, we drop out. */
2076 /* Reaching the end of the block is not an error condition */
2084 ir_function_signature::constant_expression_value(exec_list
*actual_parameters
, struct hash_table
*variable_context
)
2086 const glsl_type
*type
= this->return_type
;
2087 if (type
== glsl_type::void_type
)
2090 /* From the GLSL 1.20 spec, page 23:
2091 * "Function calls to user-defined functions (non-built-in functions)
2092 * cannot be used to form constant expressions."
2094 if (!this->is_builtin())
2098 * Of the builtin functions, only the texture lookups and the noise
2099 * ones must not be used in constant expressions. They all include
2100 * specific opcodes so they don't need to be special-cased at this
2104 /* Initialize the table of dereferencable names with the function
2105 * parameters. Verify their const-ness on the way.
2107 * We expect the correctness of the number of parameters to have
2108 * been checked earlier.
2110 hash_table
*deref_hash
= hash_table_ctor(8, hash_table_pointer_hash
,
2111 hash_table_pointer_compare
);
2113 /* If "origin" is non-NULL, then the function body is there. So we
2114 * have to use the variable objects from the object with the body,
2115 * but the parameter instanciation on the current object.
2117 const exec_node
*parameter_info
= origin
? origin
->parameters
.head
: parameters
.head
;
2119 foreach_in_list(ir_rvalue
, n
, actual_parameters
) {
2120 ir_constant
*constant
= n
->constant_expression_value(variable_context
);
2121 if (constant
== NULL
) {
2122 hash_table_dtor(deref_hash
);
2127 ir_variable
*var
= (ir_variable
*)parameter_info
;
2128 hash_table_insert(deref_hash
, constant
, var
);
2130 parameter_info
= parameter_info
->next
;
2133 ir_constant
*result
= NULL
;
2135 /* Now run the builtin function until something non-constant
2136 * happens or we get the result.
2138 if (constant_expression_evaluate_expression_list(origin
? origin
->body
: body
, deref_hash
, &result
) && result
)
2139 result
= result
->clone(ralloc_parent(this), NULL
);
2141 hash_table_dtor(deref_hash
);