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 "glsl_types.h"
42 #include "program/hash_table.h"
44 #if defined(__SUNPRO_CC) && !defined(isnormal)
46 static int isnormal(double x
)
48 return fpclass(x
) == FP_NORMAL
;
53 dot_f(ir_constant
*op0
, ir_constant
*op1
)
55 assert(op0
->type
->is_float() && op1
->type
->is_float());
58 for (unsigned c
= 0; c
< op0
->type
->components(); c
++)
59 result
+= op0
->value
.f
[c
] * op1
->value
.f
[c
];
65 dot_d(ir_constant
*op0
, ir_constant
*op1
)
67 assert(op0
->type
->is_double() && op1
->type
->is_double());
70 for (unsigned c
= 0; c
< op0
->type
->components(); c
++)
71 result
+= op0
->value
.d
[c
] * op1
->value
.d
[c
];
76 /* This method is the only one supported by gcc. Unions in particular
77 * are iffy, and read-through-converted-pointer is killed by strict
78 * aliasing. OTOH, the compiler sees through the memcpy, so the
79 * resulting asm is reasonable.
82 bitcast_u2f(unsigned int u
)
84 assert(sizeof(float) == sizeof(unsigned int));
86 memcpy(&f
, &u
, sizeof(f
));
93 assert(sizeof(float) == sizeof(unsigned int));
95 memcpy(&u
, &f
, sizeof(f
));
100 * Evaluate one component of a floating-point 4x8 unpacking function.
103 (*pack_1x8_func_t
)(float);
106 * Evaluate one component of a floating-point 2x16 unpacking function.
109 (*pack_1x16_func_t
)(float);
112 * Evaluate one component of a floating-point 4x8 unpacking function.
115 (*unpack_1x8_func_t
)(uint8_t);
118 * Evaluate one component of a floating-point 2x16 unpacking function.
121 (*unpack_1x16_func_t
)(uint16_t);
124 * Evaluate a 2x16 floating-point packing function.
127 pack_2x16(pack_1x16_func_t pack_1x16
,
130 /* From section 8.4 of the GLSL ES 3.00 spec:
134 * The first component of the vector will be written to the least
135 * significant bits of the output; the last component will be written to
136 * the most significant bits.
138 * The specifications for the other packing functions contain similar
142 u
|= ((uint32_t) pack_1x16(x
) << 0);
143 u
|= ((uint32_t) pack_1x16(y
) << 16);
148 * Evaluate a 4x8 floating-point packing function.
151 pack_4x8(pack_1x8_func_t pack_1x8
,
152 float x
, float y
, float z
, float w
)
154 /* From section 8.4 of the GLSL 4.30 spec:
158 * The first component of the vector will be written to the least
159 * significant bits of the output; the last component will be written to
160 * the most significant bits.
162 * The specifications for the other packing functions contain similar
166 u
|= ((uint32_t) pack_1x8(x
) << 0);
167 u
|= ((uint32_t) pack_1x8(y
) << 8);
168 u
|= ((uint32_t) pack_1x8(z
) << 16);
169 u
|= ((uint32_t) pack_1x8(w
) << 24);
174 * Evaluate a 2x16 floating-point unpacking function.
177 unpack_2x16(unpack_1x16_func_t unpack_1x16
,
181 /* From section 8.4 of the GLSL ES 3.00 spec:
185 * The first component of the returned vector will be extracted from
186 * the least significant bits of the input; the last component will be
187 * extracted from the most significant bits.
189 * The specifications for the other unpacking functions contain similar
192 *x
= unpack_1x16((uint16_t) (u
& 0xffff));
193 *y
= unpack_1x16((uint16_t) (u
>> 16));
197 * Evaluate a 4x8 floating-point unpacking function.
200 unpack_4x8(unpack_1x8_func_t unpack_1x8
, uint32_t u
,
201 float *x
, float *y
, float *z
, float *w
)
203 /* From section 8.4 of the GLSL 4.30 spec:
207 * The first component of the returned vector will be extracted from
208 * the least significant bits of the input; the last component will be
209 * extracted from the most significant bits.
211 * The specifications for the other unpacking functions contain similar
214 *x
= unpack_1x8((uint8_t) (u
& 0xff));
215 *y
= unpack_1x8((uint8_t) (u
>> 8));
216 *z
= unpack_1x8((uint8_t) (u
>> 16));
217 *w
= unpack_1x8((uint8_t) (u
>> 24));
221 * Evaluate one component of packSnorm4x8.
224 pack_snorm_1x8(float x
)
226 /* From section 8.4 of the GLSL 4.30 spec:
230 * The conversion for component c of v to fixed point is done as
233 * packSnorm4x8: round(clamp(c, -1, +1) * 127.0)
236 _mesa_lroundevenf(CLAMP(x
, -1.0f
, +1.0f
) * 127.0f
);
240 * Evaluate one component of packSnorm2x16.
243 pack_snorm_1x16(float x
)
245 /* From section 8.4 of the GLSL ES 3.00 spec:
249 * The conversion for component c of v to fixed point is done as
252 * packSnorm2x16: round(clamp(c, -1, +1) * 32767.0)
255 _mesa_lroundevenf(CLAMP(x
, -1.0f
, +1.0f
) * 32767.0f
);
259 * Evaluate one component of unpackSnorm4x8.
262 unpack_snorm_1x8(uint8_t u
)
264 /* From section 8.4 of the GLSL 4.30 spec:
268 * The conversion for unpacked fixed-point value f to floating point is
271 * unpackSnorm4x8: clamp(f / 127.0, -1, +1)
273 return CLAMP((int8_t) u
/ 127.0f
, -1.0f
, +1.0f
);
277 * Evaluate one component of unpackSnorm2x16.
280 unpack_snorm_1x16(uint16_t u
)
282 /* From section 8.4 of the GLSL ES 3.00 spec:
286 * The conversion for unpacked fixed-point value f to floating point is
289 * unpackSnorm2x16: clamp(f / 32767.0, -1, +1)
291 return CLAMP((int16_t) u
/ 32767.0f
, -1.0f
, +1.0f
);
295 * Evaluate one component packUnorm4x8.
298 pack_unorm_1x8(float x
)
300 /* From section 8.4 of the GLSL 4.30 spec:
304 * The conversion for component c of v to fixed point is done as
307 * packUnorm4x8: round(clamp(c, 0, +1) * 255.0)
309 return (uint8_t) (int) _mesa_roundevenf(CLAMP(x
, 0.0f
, 1.0f
) * 255.0f
);
313 * Evaluate one component packUnorm2x16.
316 pack_unorm_1x16(float x
)
318 /* From section 8.4 of the GLSL ES 3.00 spec:
322 * The conversion for component c of v to fixed point is done as
325 * packUnorm2x16: round(clamp(c, 0, +1) * 65535.0)
327 return (uint16_t) (int)
328 _mesa_roundevenf(CLAMP(x
, 0.0f
, 1.0f
) * 65535.0f
);
332 * Evaluate one component of unpackUnorm4x8.
335 unpack_unorm_1x8(uint8_t u
)
337 /* From section 8.4 of the GLSL 4.30 spec:
341 * The conversion for unpacked fixed-point value f to floating point is
344 * unpackUnorm4x8: f / 255.0
346 return (float) u
/ 255.0f
;
350 * Evaluate one component of unpackUnorm2x16.
353 unpack_unorm_1x16(uint16_t u
)
355 /* From section 8.4 of the GLSL ES 3.00 spec:
359 * The conversion for unpacked fixed-point value f to floating point is
362 * unpackUnorm2x16: f / 65535.0
364 return (float) u
/ 65535.0f
;
368 * Evaluate one component of packHalf2x16.
371 pack_half_1x16(float x
)
373 return _mesa_float_to_half(x
);
377 * Evaluate one component of unpackHalf2x16.
380 unpack_half_1x16(uint16_t u
)
382 return _mesa_half_to_float(u
);
386 * Get the constant that is ultimately referenced by an r-value, in a constant
387 * expression evaluation context.
389 * The offset is used when the reference is to a specific column of a matrix.
392 constant_referenced(const ir_dereference
*deref
,
393 struct hash_table
*variable_context
,
394 ir_constant
*&store
, int &offset
)
399 if (variable_context
== NULL
)
402 switch (deref
->ir_type
) {
403 case ir_type_dereference_array
: {
404 const ir_dereference_array
*const da
=
405 (const ir_dereference_array
*) deref
;
407 ir_constant
*const index_c
=
408 da
->array_index
->constant_expression_value(variable_context
);
410 if (!index_c
|| !index_c
->type
->is_scalar() || !index_c
->type
->is_integer())
413 const int index
= index_c
->type
->base_type
== GLSL_TYPE_INT
?
414 index_c
->get_int_component(0) :
415 index_c
->get_uint_component(0);
417 ir_constant
*substore
;
420 const ir_dereference
*const deref
= da
->array
->as_dereference();
424 if (!constant_referenced(deref
, variable_context
, substore
, suboffset
))
427 const glsl_type
*const vt
= da
->array
->type
;
428 if (vt
->is_array()) {
429 store
= substore
->get_array_element(index
);
431 } else if (vt
->is_matrix()) {
433 offset
= index
* vt
->vector_elements
;
434 } else if (vt
->is_vector()) {
436 offset
= suboffset
+ index
;
442 case ir_type_dereference_record
: {
443 const ir_dereference_record
*const dr
=
444 (const ir_dereference_record
*) deref
;
446 const ir_dereference
*const deref
= dr
->record
->as_dereference();
450 ir_constant
*substore
;
453 if (!constant_referenced(deref
, variable_context
, substore
, suboffset
))
456 /* Since we're dropping it on the floor...
458 assert(suboffset
== 0);
460 store
= substore
->get_record_field(dr
->field
);
464 case ir_type_dereference_variable
: {
465 const ir_dereference_variable
*const dv
=
466 (const ir_dereference_variable
*) deref
;
468 store
= (ir_constant
*) hash_table_find(variable_context
, dv
->var
);
473 assert(!"Should not get here.");
477 return store
!= NULL
;
482 ir_rvalue::constant_expression_value(struct hash_table
*)
484 assert(this->type
->is_error());
489 ir_expression::constant_expression_value(struct hash_table
*variable_context
)
491 if (this->type
->is_error())
494 ir_constant
*op
[ARRAY_SIZE(this->operands
)] = { NULL
, };
495 ir_constant_data data
;
497 memset(&data
, 0, sizeof(data
));
499 for (unsigned operand
= 0; operand
< this->get_num_operands(); operand
++) {
500 op
[operand
] = this->operands
[operand
]->constant_expression_value(variable_context
);
506 switch (this->operation
) {
507 case ir_binop_lshift
:
508 case ir_binop_rshift
:
510 case ir_binop_interpolate_at_offset
:
511 case ir_binop_interpolate_at_sample
:
512 case ir_binop_vector_extract
:
514 case ir_triop_bitfield_extract
:
518 assert(op
[0]->type
->base_type
== op
[1]->type
->base_type
);
522 bool op0_scalar
= op
[0]->type
->is_scalar();
523 bool op1_scalar
= op
[1] != NULL
&& op
[1]->type
->is_scalar();
525 /* When iterating over a vector or matrix's components, we want to increase
526 * the loop counter. However, for scalars, we want to stay at 0.
528 unsigned c0_inc
= op0_scalar
? 0 : 1;
529 unsigned c1_inc
= op1_scalar
? 0 : 1;
531 if (op1_scalar
|| !op
[1]) {
532 components
= op
[0]->type
->components();
534 components
= op
[1]->type
->components();
537 void *ctx
= ralloc_parent(this);
539 /* Handle array operations here, rather than below. */
540 if (op
[0]->type
->is_array()) {
541 assert(op
[1] != NULL
&& op
[1]->type
->is_array());
542 switch (this->operation
) {
543 case ir_binop_all_equal
:
544 return new(ctx
) ir_constant(op
[0]->has_value(op
[1]));
545 case ir_binop_any_nequal
:
546 return new(ctx
) ir_constant(!op
[0]->has_value(op
[1]));
553 switch (this->operation
) {
554 case ir_unop_bit_not
:
555 switch (op
[0]->type
->base_type
) {
557 for (unsigned c
= 0; c
< components
; c
++)
558 data
.i
[c
] = ~ op
[0]->value
.i
[c
];
561 for (unsigned c
= 0; c
< components
; c
++)
562 data
.u
[c
] = ~ op
[0]->value
.u
[c
];
569 case ir_unop_logic_not
:
570 assert(op
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
571 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++)
572 data
.b
[c
] = !op
[0]->value
.b
[c
];
576 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
577 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
578 data
.i
[c
] = (int) op
[0]->value
.f
[c
];
582 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
583 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
584 data
.i
[c
] = (unsigned) op
[0]->value
.f
[c
];
588 assert(op
[0]->type
->base_type
== GLSL_TYPE_INT
);
589 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
590 data
.f
[c
] = (float) op
[0]->value
.i
[c
];
594 assert(op
[0]->type
->base_type
== GLSL_TYPE_UINT
);
595 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
596 data
.f
[c
] = (float) op
[0]->value
.u
[c
];
600 assert(op
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
601 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
602 data
.f
[c
] = op
[0]->value
.b
[c
] ? 1.0F
: 0.0F
;
606 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
607 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
608 data
.b
[c
] = op
[0]->value
.f
[c
] != 0.0F
? true : false;
612 assert(op
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
613 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
614 data
.u
[c
] = op
[0]->value
.b
[c
] ? 1 : 0;
618 assert(op
[0]->type
->is_integer());
619 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
620 data
.b
[c
] = op
[0]->value
.u
[c
] ? true : false;
624 assert(op
[0]->type
->base_type
== GLSL_TYPE_UINT
);
625 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
626 data
.i
[c
] = op
[0]->value
.u
[c
];
630 assert(op
[0]->type
->base_type
== GLSL_TYPE_INT
);
631 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
632 data
.u
[c
] = op
[0]->value
.i
[c
];
635 case ir_unop_bitcast_i2f
:
636 assert(op
[0]->type
->base_type
== GLSL_TYPE_INT
);
637 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
638 data
.f
[c
] = bitcast_u2f(op
[0]->value
.i
[c
]);
641 case ir_unop_bitcast_f2i
:
642 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
643 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
644 data
.i
[c
] = bitcast_f2u(op
[0]->value
.f
[c
]);
647 case ir_unop_bitcast_u2f
:
648 assert(op
[0]->type
->base_type
== GLSL_TYPE_UINT
);
649 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
650 data
.f
[c
] = bitcast_u2f(op
[0]->value
.u
[c
]);
653 case ir_unop_bitcast_f2u
:
654 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
655 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
656 data
.u
[c
] = bitcast_f2u(op
[0]->value
.f
[c
]);
660 assert(op
[0]->type
->is_boolean());
662 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
663 if (op
[0]->value
.b
[c
])
668 assert(op
[0]->type
->base_type
== GLSL_TYPE_DOUBLE
);
669 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
670 data
.f
[c
] = op
[0]->value
.d
[c
];
674 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
675 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
676 data
.d
[c
] = op
[0]->value
.f
[c
];
680 assert(op
[0]->type
->base_type
== GLSL_TYPE_DOUBLE
);
681 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
682 data
.i
[c
] = op
[0]->value
.d
[c
];
686 assert(op
[0]->type
->base_type
== GLSL_TYPE_INT
);
687 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
688 data
.d
[c
] = op
[0]->value
.i
[c
];
692 assert(op
[0]->type
->base_type
== GLSL_TYPE_DOUBLE
);
693 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
694 data
.u
[c
] = op
[0]->value
.d
[c
];
698 assert(op
[0]->type
->base_type
== GLSL_TYPE_UINT
);
699 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
700 data
.d
[c
] = op
[0]->value
.u
[c
];
704 assert(op
[0]->type
->base_type
== GLSL_TYPE_DOUBLE
);
705 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
706 data
.b
[c
] = op
[0]->value
.d
[c
] != 0.0;
710 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
711 if (op
[0]->type
->base_type
== GLSL_TYPE_DOUBLE
)
712 data
.d
[c
] = trunc(op
[0]->value
.d
[c
]);
714 data
.f
[c
] = truncf(op
[0]->value
.f
[c
]);
718 case ir_unop_round_even
:
719 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
720 if (op
[0]->type
->base_type
== GLSL_TYPE_DOUBLE
)
721 data
.d
[c
] = _mesa_roundeven(op
[0]->value
.d
[c
]);
723 data
.f
[c
] = _mesa_roundevenf(op
[0]->value
.f
[c
]);
728 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
729 if (op
[0]->type
->base_type
== GLSL_TYPE_DOUBLE
)
730 data
.d
[c
] = ceil(op
[0]->value
.d
[c
]);
732 data
.f
[c
] = ceilf(op
[0]->value
.f
[c
]);
737 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
738 if (op
[0]->type
->base_type
== GLSL_TYPE_DOUBLE
)
739 data
.d
[c
] = floor(op
[0]->value
.d
[c
]);
741 data
.f
[c
] = floorf(op
[0]->value
.f
[c
]);
746 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
747 switch (this->type
->base_type
) {
754 case GLSL_TYPE_FLOAT
:
755 data
.f
[c
] = op
[0]->value
.f
[c
] - floor(op
[0]->value
.f
[c
]);
757 case GLSL_TYPE_DOUBLE
:
758 data
.d
[c
] = op
[0]->value
.d
[c
] - floor(op
[0]->value
.d
[c
]);
767 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
768 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
769 data
.f
[c
] = sinf(op
[0]->value
.f
[c
]);
774 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
775 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
776 data
.f
[c
] = cosf(op
[0]->value
.f
[c
]);
781 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
782 switch (this->type
->base_type
) {
784 data
.u
[c
] = -((int) op
[0]->value
.u
[c
]);
787 data
.i
[c
] = -op
[0]->value
.i
[c
];
789 case GLSL_TYPE_FLOAT
:
790 data
.f
[c
] = -op
[0]->value
.f
[c
];
792 case GLSL_TYPE_DOUBLE
:
793 data
.d
[c
] = -op
[0]->value
.d
[c
];
802 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
803 switch (this->type
->base_type
) {
805 data
.u
[c
] = op
[0]->value
.u
[c
];
808 data
.i
[c
] = op
[0]->value
.i
[c
];
810 data
.i
[c
] = -data
.i
[c
];
812 case GLSL_TYPE_FLOAT
:
813 data
.f
[c
] = fabs(op
[0]->value
.f
[c
]);
815 case GLSL_TYPE_DOUBLE
:
816 data
.d
[c
] = fabs(op
[0]->value
.d
[c
]);
825 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
826 switch (this->type
->base_type
) {
828 data
.u
[c
] = op
[0]->value
.i
[c
] > 0;
831 data
.i
[c
] = (op
[0]->value
.i
[c
] > 0) - (op
[0]->value
.i
[c
] < 0);
833 case GLSL_TYPE_FLOAT
:
834 data
.f
[c
] = float((op
[0]->value
.f
[c
] > 0)-(op
[0]->value
.f
[c
] < 0));
836 case GLSL_TYPE_DOUBLE
:
837 data
.d
[c
] = double((op
[0]->value
.d
[c
] > 0)-(op
[0]->value
.d
[c
] < 0));
846 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
847 switch (this->type
->base_type
) {
849 if (op
[0]->value
.u
[c
] != 0.0)
850 data
.u
[c
] = 1 / op
[0]->value
.u
[c
];
853 if (op
[0]->value
.i
[c
] != 0.0)
854 data
.i
[c
] = 1 / op
[0]->value
.i
[c
];
856 case GLSL_TYPE_FLOAT
:
857 if (op
[0]->value
.f
[c
] != 0.0)
858 data
.f
[c
] = 1.0F
/ op
[0]->value
.f
[c
];
860 case GLSL_TYPE_DOUBLE
:
861 if (op
[0]->value
.d
[c
] != 0.0)
862 data
.d
[c
] = 1.0 / op
[0]->value
.d
[c
];
871 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
872 if (op
[0]->type
->base_type
== GLSL_TYPE_DOUBLE
)
873 data
.d
[c
] = 1.0 / sqrt(op
[0]->value
.d
[c
]);
875 data
.f
[c
] = 1.0F
/ sqrtf(op
[0]->value
.f
[c
]);
880 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
881 if (op
[0]->type
->base_type
== GLSL_TYPE_DOUBLE
)
882 data
.d
[c
] = sqrt(op
[0]->value
.d
[c
]);
884 data
.f
[c
] = sqrtf(op
[0]->value
.f
[c
]);
889 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
890 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
891 data
.f
[c
] = expf(op
[0]->value
.f
[c
]);
896 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
897 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
898 data
.f
[c
] = exp2f(op
[0]->value
.f
[c
]);
903 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
904 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
905 data
.f
[c
] = logf(op
[0]->value
.f
[c
]);
910 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
911 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
912 data
.f
[c
] = log2f(op
[0]->value
.f
[c
]);
917 case ir_unop_dFdx_coarse
:
918 case ir_unop_dFdx_fine
:
920 case ir_unop_dFdy_coarse
:
921 case ir_unop_dFdy_fine
:
922 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
923 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
928 case ir_unop_pack_snorm_2x16
:
929 assert(op
[0]->type
== glsl_type::vec2_type
);
930 data
.u
[0] = pack_2x16(pack_snorm_1x16
,
934 case ir_unop_pack_snorm_4x8
:
935 assert(op
[0]->type
== glsl_type::vec4_type
);
936 data
.u
[0] = pack_4x8(pack_snorm_1x8
,
942 case ir_unop_unpack_snorm_2x16
:
943 assert(op
[0]->type
== glsl_type::uint_type
);
944 unpack_2x16(unpack_snorm_1x16
,
946 &data
.f
[0], &data
.f
[1]);
948 case ir_unop_unpack_snorm_4x8
:
949 assert(op
[0]->type
== glsl_type::uint_type
);
950 unpack_4x8(unpack_snorm_1x8
,
952 &data
.f
[0], &data
.f
[1], &data
.f
[2], &data
.f
[3]);
954 case ir_unop_pack_unorm_2x16
:
955 assert(op
[0]->type
== glsl_type::vec2_type
);
956 data
.u
[0] = pack_2x16(pack_unorm_1x16
,
960 case ir_unop_pack_unorm_4x8
:
961 assert(op
[0]->type
== glsl_type::vec4_type
);
962 data
.u
[0] = pack_4x8(pack_unorm_1x8
,
968 case ir_unop_unpack_unorm_2x16
:
969 assert(op
[0]->type
== glsl_type::uint_type
);
970 unpack_2x16(unpack_unorm_1x16
,
972 &data
.f
[0], &data
.f
[1]);
974 case ir_unop_unpack_unorm_4x8
:
975 assert(op
[0]->type
== glsl_type::uint_type
);
976 unpack_4x8(unpack_unorm_1x8
,
978 &data
.f
[0], &data
.f
[1], &data
.f
[2], &data
.f
[3]);
980 case ir_unop_pack_half_2x16
:
981 assert(op
[0]->type
== glsl_type::vec2_type
);
982 data
.u
[0] = pack_2x16(pack_half_1x16
,
986 case ir_unop_unpack_half_2x16
:
987 assert(op
[0]->type
== glsl_type::uint_type
);
988 unpack_2x16(unpack_half_1x16
,
990 &data
.f
[0], &data
.f
[1]);
993 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
994 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
995 data
.f
[c
] = powf(op
[0]->value
.f
[c
], op
[1]->value
.f
[c
]);
1000 if (op
[0]->type
->base_type
== GLSL_TYPE_DOUBLE
)
1001 data
.d
[0] = dot_d(op
[0], op
[1]);
1003 data
.f
[0] = dot_f(op
[0], op
[1]);
1007 assert(op
[0]->type
== op
[1]->type
|| op0_scalar
|| op1_scalar
);
1008 for (unsigned c
= 0, c0
= 0, c1
= 0;
1010 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
1012 switch (op
[0]->type
->base_type
) {
1013 case GLSL_TYPE_UINT
:
1014 data
.u
[c
] = MIN2(op
[0]->value
.u
[c0
], op
[1]->value
.u
[c1
]);
1017 data
.i
[c
] = MIN2(op
[0]->value
.i
[c0
], op
[1]->value
.i
[c1
]);
1019 case GLSL_TYPE_FLOAT
:
1020 data
.f
[c
] = MIN2(op
[0]->value
.f
[c0
], op
[1]->value
.f
[c1
]);
1022 case GLSL_TYPE_DOUBLE
:
1023 data
.d
[c
] = MIN2(op
[0]->value
.d
[c0
], op
[1]->value
.d
[c1
]);
1032 assert(op
[0]->type
== op
[1]->type
|| op0_scalar
|| op1_scalar
);
1033 for (unsigned c
= 0, c0
= 0, c1
= 0;
1035 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
1037 switch (op
[0]->type
->base_type
) {
1038 case GLSL_TYPE_UINT
:
1039 data
.u
[c
] = MAX2(op
[0]->value
.u
[c0
], op
[1]->value
.u
[c1
]);
1042 data
.i
[c
] = MAX2(op
[0]->value
.i
[c0
], op
[1]->value
.i
[c1
]);
1044 case GLSL_TYPE_FLOAT
:
1045 data
.f
[c
] = MAX2(op
[0]->value
.f
[c0
], op
[1]->value
.f
[c1
]);
1047 case GLSL_TYPE_DOUBLE
:
1048 data
.d
[c
] = MAX2(op
[0]->value
.d
[c0
], op
[1]->value
.d
[c1
]);
1057 assert(op
[0]->type
== op
[1]->type
|| op0_scalar
|| op1_scalar
);
1058 for (unsigned c
= 0, c0
= 0, c1
= 0;
1060 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
1062 switch (op
[0]->type
->base_type
) {
1063 case GLSL_TYPE_UINT
:
1064 data
.u
[c
] = op
[0]->value
.u
[c0
] + op
[1]->value
.u
[c1
];
1067 data
.i
[c
] = op
[0]->value
.i
[c0
] + op
[1]->value
.i
[c1
];
1069 case GLSL_TYPE_FLOAT
:
1070 data
.f
[c
] = op
[0]->value
.f
[c0
] + op
[1]->value
.f
[c1
];
1072 case GLSL_TYPE_DOUBLE
:
1073 data
.d
[c
] = op
[0]->value
.d
[c0
] + op
[1]->value
.d
[c1
];
1082 assert(op
[0]->type
== op
[1]->type
|| op0_scalar
|| op1_scalar
);
1083 for (unsigned c
= 0, c0
= 0, c1
= 0;
1085 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
1087 switch (op
[0]->type
->base_type
) {
1088 case GLSL_TYPE_UINT
:
1089 data
.u
[c
] = op
[0]->value
.u
[c0
] - op
[1]->value
.u
[c1
];
1092 data
.i
[c
] = op
[0]->value
.i
[c0
] - op
[1]->value
.i
[c1
];
1094 case GLSL_TYPE_FLOAT
:
1095 data
.f
[c
] = op
[0]->value
.f
[c0
] - op
[1]->value
.f
[c1
];
1097 case GLSL_TYPE_DOUBLE
:
1098 data
.d
[c
] = op
[0]->value
.d
[c0
] - op
[1]->value
.d
[c1
];
1107 /* Check for equal types, or unequal types involving scalars */
1108 if ((op
[0]->type
== op
[1]->type
&& !op
[0]->type
->is_matrix())
1109 || op0_scalar
|| op1_scalar
) {
1110 for (unsigned c
= 0, c0
= 0, c1
= 0;
1112 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
1114 switch (op
[0]->type
->base_type
) {
1115 case GLSL_TYPE_UINT
:
1116 data
.u
[c
] = op
[0]->value
.u
[c0
] * op
[1]->value
.u
[c1
];
1119 data
.i
[c
] = op
[0]->value
.i
[c0
] * op
[1]->value
.i
[c1
];
1121 case GLSL_TYPE_FLOAT
:
1122 data
.f
[c
] = op
[0]->value
.f
[c0
] * op
[1]->value
.f
[c1
];
1124 case GLSL_TYPE_DOUBLE
:
1125 data
.d
[c
] = op
[0]->value
.d
[c0
] * op
[1]->value
.d
[c1
];
1132 assert(op
[0]->type
->is_matrix() || op
[1]->type
->is_matrix());
1134 /* Multiply an N-by-M matrix with an M-by-P matrix. Since either
1135 * matrix can be a GLSL vector, either N or P can be 1.
1137 * For vec*mat, the vector is treated as a row vector. This
1138 * means the vector is a 1-row x M-column matrix.
1140 * For mat*vec, the vector is treated as a column vector. Since
1141 * matrix_columns is 1 for vectors, this just works.
1143 const unsigned n
= op
[0]->type
->is_vector()
1144 ? 1 : op
[0]->type
->vector_elements
;
1145 const unsigned m
= op
[1]->type
->vector_elements
;
1146 const unsigned p
= op
[1]->type
->matrix_columns
;
1147 for (unsigned j
= 0; j
< p
; j
++) {
1148 for (unsigned i
= 0; i
< n
; i
++) {
1149 for (unsigned k
= 0; k
< m
; k
++) {
1150 if (op
[0]->type
->base_type
== GLSL_TYPE_DOUBLE
)
1151 data
.d
[i
+n
*j
] += op
[0]->value
.d
[i
+n
*k
]*op
[1]->value
.d
[k
+m
*j
];
1153 data
.f
[i
+n
*j
] += op
[0]->value
.f
[i
+n
*k
]*op
[1]->value
.f
[k
+m
*j
];
1161 /* FINISHME: Emit warning when division-by-zero is detected. */
1162 assert(op
[0]->type
== op
[1]->type
|| op0_scalar
|| op1_scalar
);
1163 for (unsigned c
= 0, c0
= 0, c1
= 0;
1165 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
1167 switch (op
[0]->type
->base_type
) {
1168 case GLSL_TYPE_UINT
:
1169 if (op
[1]->value
.u
[c1
] == 0) {
1172 data
.u
[c
] = op
[0]->value
.u
[c0
] / op
[1]->value
.u
[c1
];
1176 if (op
[1]->value
.i
[c1
] == 0) {
1179 data
.i
[c
] = op
[0]->value
.i
[c0
] / op
[1]->value
.i
[c1
];
1182 case GLSL_TYPE_FLOAT
:
1183 data
.f
[c
] = op
[0]->value
.f
[c0
] / op
[1]->value
.f
[c1
];
1185 case GLSL_TYPE_DOUBLE
:
1186 data
.d
[c
] = op
[0]->value
.d
[c0
] / op
[1]->value
.d
[c1
];
1195 /* FINISHME: Emit warning when division-by-zero is detected. */
1196 assert(op
[0]->type
== op
[1]->type
|| op0_scalar
|| op1_scalar
);
1197 for (unsigned c
= 0, c0
= 0, c1
= 0;
1199 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
1201 switch (op
[0]->type
->base_type
) {
1202 case GLSL_TYPE_UINT
:
1203 if (op
[1]->value
.u
[c1
] == 0) {
1206 data
.u
[c
] = op
[0]->value
.u
[c0
] % op
[1]->value
.u
[c1
];
1210 if (op
[1]->value
.i
[c1
] == 0) {
1213 data
.i
[c
] = op
[0]->value
.i
[c0
] % op
[1]->value
.i
[c1
];
1216 case GLSL_TYPE_FLOAT
:
1217 /* We don't use fmod because it rounds toward zero; GLSL specifies
1220 data
.f
[c
] = op
[0]->value
.f
[c0
] - op
[1]->value
.f
[c1
]
1221 * floorf(op
[0]->value
.f
[c0
] / op
[1]->value
.f
[c1
]);
1223 case GLSL_TYPE_DOUBLE
:
1224 /* We don't use fmod because it rounds toward zero; GLSL specifies
1227 data
.d
[c
] = op
[0]->value
.d
[c0
] - op
[1]->value
.d
[c1
]
1228 * floor(op
[0]->value
.d
[c0
] / op
[1]->value
.d
[c1
]);
1237 case ir_binop_logic_and
:
1238 assert(op
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
1239 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++)
1240 data
.b
[c
] = op
[0]->value
.b
[c
] && op
[1]->value
.b
[c
];
1242 case ir_binop_logic_xor
:
1243 assert(op
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
1244 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++)
1245 data
.b
[c
] = op
[0]->value
.b
[c
] ^ op
[1]->value
.b
[c
];
1247 case ir_binop_logic_or
:
1248 assert(op
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
1249 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++)
1250 data
.b
[c
] = op
[0]->value
.b
[c
] || op
[1]->value
.b
[c
];
1254 assert(op
[0]->type
== op
[1]->type
);
1255 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
1256 switch (op
[0]->type
->base_type
) {
1257 case GLSL_TYPE_UINT
:
1258 data
.b
[c
] = op
[0]->value
.u
[c
] < op
[1]->value
.u
[c
];
1261 data
.b
[c
] = op
[0]->value
.i
[c
] < op
[1]->value
.i
[c
];
1263 case GLSL_TYPE_FLOAT
:
1264 data
.b
[c
] = op
[0]->value
.f
[c
] < op
[1]->value
.f
[c
];
1266 case GLSL_TYPE_DOUBLE
:
1267 data
.b
[c
] = op
[0]->value
.d
[c
] < op
[1]->value
.d
[c
];
1274 case ir_binop_greater
:
1275 assert(op
[0]->type
== op
[1]->type
);
1276 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
1277 switch (op
[0]->type
->base_type
) {
1278 case GLSL_TYPE_UINT
:
1279 data
.b
[c
] = op
[0]->value
.u
[c
] > op
[1]->value
.u
[c
];
1282 data
.b
[c
] = op
[0]->value
.i
[c
] > op
[1]->value
.i
[c
];
1284 case GLSL_TYPE_FLOAT
:
1285 data
.b
[c
] = op
[0]->value
.f
[c
] > op
[1]->value
.f
[c
];
1287 case GLSL_TYPE_DOUBLE
:
1288 data
.b
[c
] = op
[0]->value
.d
[c
] > op
[1]->value
.d
[c
];
1295 case ir_binop_lequal
:
1296 assert(op
[0]->type
== op
[1]->type
);
1297 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
1298 switch (op
[0]->type
->base_type
) {
1299 case GLSL_TYPE_UINT
:
1300 data
.b
[c
] = op
[0]->value
.u
[c
] <= op
[1]->value
.u
[c
];
1303 data
.b
[c
] = op
[0]->value
.i
[c
] <= op
[1]->value
.i
[c
];
1305 case GLSL_TYPE_FLOAT
:
1306 data
.b
[c
] = op
[0]->value
.f
[c
] <= op
[1]->value
.f
[c
];
1308 case GLSL_TYPE_DOUBLE
:
1309 data
.b
[c
] = op
[0]->value
.d
[c
] <= op
[1]->value
.d
[c
];
1316 case ir_binop_gequal
:
1317 assert(op
[0]->type
== op
[1]->type
);
1318 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
1319 switch (op
[0]->type
->base_type
) {
1320 case GLSL_TYPE_UINT
:
1321 data
.b
[c
] = op
[0]->value
.u
[c
] >= op
[1]->value
.u
[c
];
1324 data
.b
[c
] = op
[0]->value
.i
[c
] >= op
[1]->value
.i
[c
];
1326 case GLSL_TYPE_FLOAT
:
1327 data
.b
[c
] = op
[0]->value
.f
[c
] >= op
[1]->value
.f
[c
];
1329 case GLSL_TYPE_DOUBLE
:
1330 data
.b
[c
] = op
[0]->value
.d
[c
] >= op
[1]->value
.d
[c
];
1337 case ir_binop_equal
:
1338 assert(op
[0]->type
== op
[1]->type
);
1339 for (unsigned c
= 0; c
< components
; c
++) {
1340 switch (op
[0]->type
->base_type
) {
1341 case GLSL_TYPE_UINT
:
1342 data
.b
[c
] = op
[0]->value
.u
[c
] == op
[1]->value
.u
[c
];
1345 data
.b
[c
] = op
[0]->value
.i
[c
] == op
[1]->value
.i
[c
];
1347 case GLSL_TYPE_FLOAT
:
1348 data
.b
[c
] = op
[0]->value
.f
[c
] == op
[1]->value
.f
[c
];
1350 case GLSL_TYPE_BOOL
:
1351 data
.b
[c
] = op
[0]->value
.b
[c
] == op
[1]->value
.b
[c
];
1353 case GLSL_TYPE_DOUBLE
:
1354 data
.b
[c
] = op
[0]->value
.d
[c
] == op
[1]->value
.d
[c
];
1361 case ir_binop_nequal
:
1362 assert(op
[0]->type
== op
[1]->type
);
1363 for (unsigned c
= 0; c
< components
; c
++) {
1364 switch (op
[0]->type
->base_type
) {
1365 case GLSL_TYPE_UINT
:
1366 data
.b
[c
] = op
[0]->value
.u
[c
] != op
[1]->value
.u
[c
];
1369 data
.b
[c
] = op
[0]->value
.i
[c
] != op
[1]->value
.i
[c
];
1371 case GLSL_TYPE_FLOAT
:
1372 data
.b
[c
] = op
[0]->value
.f
[c
] != op
[1]->value
.f
[c
];
1374 case GLSL_TYPE_BOOL
:
1375 data
.b
[c
] = op
[0]->value
.b
[c
] != op
[1]->value
.b
[c
];
1377 case GLSL_TYPE_DOUBLE
:
1378 data
.b
[c
] = op
[0]->value
.d
[c
] != op
[1]->value
.d
[c
];
1385 case ir_binop_all_equal
:
1386 data
.b
[0] = op
[0]->has_value(op
[1]);
1388 case ir_binop_any_nequal
:
1389 data
.b
[0] = !op
[0]->has_value(op
[1]);
1392 case ir_binop_lshift
:
1393 for (unsigned c
= 0, c0
= 0, c1
= 0;
1395 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
1397 if (op
[0]->type
->base_type
== GLSL_TYPE_INT
&&
1398 op
[1]->type
->base_type
== GLSL_TYPE_INT
) {
1399 data
.i
[c
] = op
[0]->value
.i
[c0
] << op
[1]->value
.i
[c1
];
1401 } else if (op
[0]->type
->base_type
== GLSL_TYPE_INT
&&
1402 op
[1]->type
->base_type
== GLSL_TYPE_UINT
) {
1403 data
.i
[c
] = op
[0]->value
.i
[c0
] << op
[1]->value
.u
[c1
];
1405 } else if (op
[0]->type
->base_type
== GLSL_TYPE_UINT
&&
1406 op
[1]->type
->base_type
== GLSL_TYPE_INT
) {
1407 data
.u
[c
] = op
[0]->value
.u
[c0
] << op
[1]->value
.i
[c1
];
1409 } else if (op
[0]->type
->base_type
== GLSL_TYPE_UINT
&&
1410 op
[1]->type
->base_type
== GLSL_TYPE_UINT
) {
1411 data
.u
[c
] = op
[0]->value
.u
[c0
] << op
[1]->value
.u
[c1
];
1416 case ir_binop_rshift
:
1417 for (unsigned c
= 0, c0
= 0, c1
= 0;
1419 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
1421 if (op
[0]->type
->base_type
== GLSL_TYPE_INT
&&
1422 op
[1]->type
->base_type
== GLSL_TYPE_INT
) {
1423 data
.i
[c
] = op
[0]->value
.i
[c0
] >> op
[1]->value
.i
[c1
];
1425 } else if (op
[0]->type
->base_type
== GLSL_TYPE_INT
&&
1426 op
[1]->type
->base_type
== GLSL_TYPE_UINT
) {
1427 data
.i
[c
] = op
[0]->value
.i
[c0
] >> op
[1]->value
.u
[c1
];
1429 } else if (op
[0]->type
->base_type
== GLSL_TYPE_UINT
&&
1430 op
[1]->type
->base_type
== GLSL_TYPE_INT
) {
1431 data
.u
[c
] = op
[0]->value
.u
[c0
] >> op
[1]->value
.i
[c1
];
1433 } else if (op
[0]->type
->base_type
== GLSL_TYPE_UINT
&&
1434 op
[1]->type
->base_type
== GLSL_TYPE_UINT
) {
1435 data
.u
[c
] = op
[0]->value
.u
[c0
] >> op
[1]->value
.u
[c1
];
1440 case ir_binop_bit_and
:
1441 for (unsigned c
= 0, c0
= 0, c1
= 0;
1443 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
1445 switch (op
[0]->type
->base_type
) {
1447 data
.i
[c
] = op
[0]->value
.i
[c0
] & op
[1]->value
.i
[c1
];
1449 case GLSL_TYPE_UINT
:
1450 data
.u
[c
] = op
[0]->value
.u
[c0
] & op
[1]->value
.u
[c1
];
1458 case ir_binop_bit_or
:
1459 for (unsigned c
= 0, c0
= 0, c1
= 0;
1461 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
1463 switch (op
[0]->type
->base_type
) {
1465 data
.i
[c
] = op
[0]->value
.i
[c0
] | op
[1]->value
.i
[c1
];
1467 case GLSL_TYPE_UINT
:
1468 data
.u
[c
] = op
[0]->value
.u
[c0
] | op
[1]->value
.u
[c1
];
1476 case ir_binop_vector_extract
: {
1477 const int c
= CLAMP(op
[1]->value
.i
[0], 0,
1478 (int) op
[0]->type
->vector_elements
- 1);
1480 switch (op
[0]->type
->base_type
) {
1481 case GLSL_TYPE_UINT
:
1482 data
.u
[0] = op
[0]->value
.u
[c
];
1485 data
.i
[0] = op
[0]->value
.i
[c
];
1487 case GLSL_TYPE_FLOAT
:
1488 data
.f
[0] = op
[0]->value
.f
[c
];
1490 case GLSL_TYPE_DOUBLE
:
1491 data
.d
[0] = op
[0]->value
.d
[c
];
1493 case GLSL_TYPE_BOOL
:
1494 data
.b
[0] = op
[0]->value
.b
[c
];
1502 case ir_binop_bit_xor
:
1503 for (unsigned c
= 0, c0
= 0, c1
= 0;
1505 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
1507 switch (op
[0]->type
->base_type
) {
1509 data
.i
[c
] = op
[0]->value
.i
[c0
] ^ op
[1]->value
.i
[c1
];
1511 case GLSL_TYPE_UINT
:
1512 data
.u
[c
] = op
[0]->value
.u
[c0
] ^ op
[1]->value
.u
[c1
];
1520 case ir_unop_bitfield_reverse
:
1521 /* http://graphics.stanford.edu/~seander/bithacks.html#BitReverseObvious */
1522 for (unsigned c
= 0; c
< components
; c
++) {
1523 unsigned int v
= op
[0]->value
.u
[c
]; // input bits to be reversed
1524 unsigned int r
= v
; // r will be reversed bits of v; first get LSB of v
1525 int s
= sizeof(v
) * CHAR_BIT
- 1; // extra shift needed at end
1527 for (v
>>= 1; v
; v
>>= 1) {
1532 r
<<= s
; // shift when v's highest bits are zero
1538 case ir_unop_bit_count
:
1539 for (unsigned c
= 0; c
< components
; c
++) {
1541 unsigned v
= op
[0]->value
.u
[c
];
1543 for (; v
; count
++) {
1550 case ir_unop_find_msb
:
1551 for (unsigned c
= 0; c
< components
; c
++) {
1552 int v
= op
[0]->value
.i
[c
];
1554 if (v
== 0 || (op
[0]->type
->base_type
== GLSL_TYPE_INT
&& v
== -1))
1558 int top_bit
= op
[0]->type
->base_type
== GLSL_TYPE_UINT
1559 ? 0 : v
& (1 << 31);
1561 while (((v
& (1 << 31)) == top_bit
) && count
!= 32) {
1566 data
.i
[c
] = 31 - count
;
1571 case ir_unop_find_lsb
:
1572 for (unsigned c
= 0; c
< components
; c
++) {
1573 if (op
[0]->value
.i
[c
] == 0)
1577 unsigned v
= op
[0]->value
.u
[c
];
1579 for (; !(v
& 1); v
>>= 1) {
1587 case ir_unop_saturate
:
1588 for (unsigned c
= 0; c
< components
; c
++) {
1589 data
.f
[c
] = CLAMP(op
[0]->value
.f
[c
], 0.0f
, 1.0f
);
1592 case ir_unop_pack_double_2x32
: {
1593 /* XXX needs to be checked on big-endian */
1595 temp
= (uint64_t)op
[0]->value
.u
[0] | ((uint64_t)op
[0]->value
.u
[1] << 32);
1596 data
.d
[0] = *(double *)&temp
;
1600 case ir_unop_unpack_double_2x32
:
1601 /* XXX needs to be checked on big-endian */
1602 data
.u
[0] = *(uint32_t *)&op
[0]->value
.d
[0];
1603 data
.u
[1] = *((uint32_t *)&op
[0]->value
.d
[0] + 1);
1606 case ir_triop_bitfield_extract
: {
1607 int offset
= op
[1]->value
.i
[0];
1608 int bits
= op
[2]->value
.i
[0];
1610 for (unsigned c
= 0; c
< components
; c
++) {
1613 else if (offset
< 0 || bits
< 0)
1614 data
.u
[c
] = 0; /* Undefined, per spec. */
1615 else if (offset
+ bits
> 32)
1616 data
.u
[c
] = 0; /* Undefined, per spec. */
1618 if (op
[0]->type
->base_type
== GLSL_TYPE_INT
) {
1619 /* int so that the right shift will sign-extend. */
1620 int value
= op
[0]->value
.i
[c
];
1621 value
<<= 32 - bits
- offset
;
1622 value
>>= 32 - bits
;
1625 unsigned value
= op
[0]->value
.u
[c
];
1626 value
<<= 32 - bits
- offset
;
1627 value
>>= 32 - bits
;
1635 case ir_binop_bfm
: {
1636 int bits
= op
[0]->value
.i
[0];
1637 int offset
= op
[1]->value
.i
[0];
1639 for (unsigned c
= 0; c
< components
; c
++) {
1641 data
.u
[c
] = op
[0]->value
.u
[c
];
1642 else if (offset
< 0 || bits
< 0)
1643 data
.u
[c
] = 0; /* Undefined for bitfieldInsert, per spec. */
1644 else if (offset
+ bits
> 32)
1645 data
.u
[c
] = 0; /* Undefined for bitfieldInsert, per spec. */
1647 data
.u
[c
] = ((1 << bits
) - 1) << offset
;
1652 case ir_binop_ldexp
:
1653 for (unsigned c
= 0; c
< components
; c
++) {
1654 if (op
[0]->type
->base_type
== GLSL_TYPE_DOUBLE
) {
1655 data
.d
[c
] = ldexp(op
[0]->value
.d
[c
], op
[1]->value
.i
[c
]);
1656 /* Flush subnormal values to zero. */
1657 if (!isnormal(data
.d
[c
]))
1658 data
.d
[c
] = copysign(0.0, op
[0]->value
.d
[c
]);
1660 data
.f
[c
] = ldexpf(op
[0]->value
.f
[c
], op
[1]->value
.i
[c
]);
1661 /* Flush subnormal values to zero. */
1662 if (!isnormal(data
.f
[c
]))
1663 data
.f
[c
] = copysignf(0.0f
, op
[0]->value
.f
[c
]);
1669 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
||
1670 op
[0]->type
->base_type
== GLSL_TYPE_DOUBLE
);
1671 assert(op
[1]->type
->base_type
== GLSL_TYPE_FLOAT
||
1672 op
[1]->type
->base_type
== GLSL_TYPE_DOUBLE
);
1673 assert(op
[2]->type
->base_type
== GLSL_TYPE_FLOAT
||
1674 op
[2]->type
->base_type
== GLSL_TYPE_DOUBLE
);
1676 for (unsigned c
= 0; c
< components
; c
++) {
1677 if (op
[0]->type
->base_type
== GLSL_TYPE_DOUBLE
)
1678 data
.d
[c
] = op
[0]->value
.d
[c
] * op
[1]->value
.d
[c
]
1679 + op
[2]->value
.d
[c
];
1681 data
.f
[c
] = op
[0]->value
.f
[c
] * op
[1]->value
.f
[c
]
1682 + op
[2]->value
.f
[c
];
1686 case ir_triop_lrp
: {
1687 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
||
1688 op
[0]->type
->base_type
== GLSL_TYPE_DOUBLE
);
1689 assert(op
[1]->type
->base_type
== GLSL_TYPE_FLOAT
||
1690 op
[1]->type
->base_type
== GLSL_TYPE_DOUBLE
);
1691 assert(op
[2]->type
->base_type
== GLSL_TYPE_FLOAT
||
1692 op
[2]->type
->base_type
== GLSL_TYPE_DOUBLE
);
1694 unsigned c2_inc
= op
[2]->type
->is_scalar() ? 0 : 1;
1695 for (unsigned c
= 0, c2
= 0; c
< components
; c2
+= c2_inc
, c
++) {
1696 if (op
[0]->type
->base_type
== GLSL_TYPE_DOUBLE
)
1697 data
.d
[c
] = op
[0]->value
.d
[c
] * (1.0 - op
[2]->value
.d
[c2
]) +
1698 (op
[1]->value
.d
[c
] * op
[2]->value
.d
[c2
]);
1700 data
.f
[c
] = op
[0]->value
.f
[c
] * (1.0f
- op
[2]->value
.f
[c2
]) +
1701 (op
[1]->value
.f
[c
] * op
[2]->value
.f
[c2
]);
1707 for (unsigned c
= 0; c
< components
; c
++) {
1708 if (op
[1]->type
->base_type
== GLSL_TYPE_DOUBLE
)
1709 data
.d
[c
] = op
[0]->value
.b
[c
] ? op
[1]->value
.d
[c
]
1710 : op
[2]->value
.d
[c
];
1712 data
.u
[c
] = op
[0]->value
.b
[c
] ? op
[1]->value
.u
[c
]
1713 : op
[2]->value
.u
[c
];
1717 case ir_triop_vector_insert
: {
1718 const unsigned idx
= op
[2]->value
.u
[0];
1720 memcpy(&data
, &op
[0]->value
, sizeof(data
));
1722 switch (this->type
->base_type
) {
1724 data
.i
[idx
] = op
[1]->value
.i
[0];
1726 case GLSL_TYPE_UINT
:
1727 data
.u
[idx
] = op
[1]->value
.u
[0];
1729 case GLSL_TYPE_FLOAT
:
1730 data
.f
[idx
] = op
[1]->value
.f
[0];
1732 case GLSL_TYPE_BOOL
:
1733 data
.b
[idx
] = op
[1]->value
.b
[0];
1735 case GLSL_TYPE_DOUBLE
:
1736 data
.d
[idx
] = op
[1]->value
.d
[0];
1739 assert(!"Should not get here.");
1745 case ir_quadop_bitfield_insert
: {
1746 int offset
= op
[2]->value
.i
[0];
1747 int bits
= op
[3]->value
.i
[0];
1749 for (unsigned c
= 0; c
< components
; c
++) {
1751 data
.u
[c
] = op
[0]->value
.u
[c
];
1752 else if (offset
< 0 || bits
< 0)
1753 data
.u
[c
] = 0; /* Undefined, per spec. */
1754 else if (offset
+ bits
> 32)
1755 data
.u
[c
] = 0; /* Undefined, per spec. */
1757 unsigned insert_mask
= ((1 << bits
) - 1) << offset
;
1759 unsigned insert
= op
[1]->value
.u
[c
];
1761 insert
&= insert_mask
;
1763 unsigned base
= op
[0]->value
.u
[c
];
1764 base
&= ~insert_mask
;
1766 data
.u
[c
] = base
| insert
;
1772 case ir_quadop_vector
:
1773 for (unsigned c
= 0; c
< this->type
->vector_elements
; c
++) {
1774 switch (this->type
->base_type
) {
1776 data
.i
[c
] = op
[c
]->value
.i
[0];
1778 case GLSL_TYPE_UINT
:
1779 data
.u
[c
] = op
[c
]->value
.u
[0];
1781 case GLSL_TYPE_FLOAT
:
1782 data
.f
[c
] = op
[c
]->value
.f
[0];
1784 case GLSL_TYPE_DOUBLE
:
1785 data
.d
[c
] = op
[c
]->value
.d
[0];
1794 /* FINISHME: Should handle all expression types. */
1798 return new(ctx
) ir_constant(this->type
, &data
);
1803 ir_texture::constant_expression_value(struct hash_table
*)
1805 /* texture lookups aren't constant expressions */
1811 ir_swizzle::constant_expression_value(struct hash_table
*variable_context
)
1813 ir_constant
*v
= this->val
->constant_expression_value(variable_context
);
1816 ir_constant_data data
= { { 0 } };
1818 const unsigned swiz_idx
[4] = {
1819 this->mask
.x
, this->mask
.y
, this->mask
.z
, this->mask
.w
1822 for (unsigned i
= 0; i
< this->mask
.num_components
; i
++) {
1823 switch (v
->type
->base_type
) {
1824 case GLSL_TYPE_UINT
:
1825 case GLSL_TYPE_INT
: data
.u
[i
] = v
->value
.u
[swiz_idx
[i
]]; break;
1826 case GLSL_TYPE_FLOAT
: data
.f
[i
] = v
->value
.f
[swiz_idx
[i
]]; break;
1827 case GLSL_TYPE_BOOL
: data
.b
[i
] = v
->value
.b
[swiz_idx
[i
]]; break;
1828 case GLSL_TYPE_DOUBLE
:data
.d
[i
] = v
->value
.d
[swiz_idx
[i
]]; break;
1829 default: assert(!"Should not get here."); break;
1833 void *ctx
= ralloc_parent(this);
1834 return new(ctx
) ir_constant(this->type
, &data
);
1841 ir_dereference_variable::constant_expression_value(struct hash_table
*variable_context
)
1843 /* This may occur during compile and var->type is glsl_type::error_type */
1847 /* Give priority to the context hashtable, if it exists */
1848 if (variable_context
) {
1849 ir_constant
*value
= (ir_constant
*)hash_table_find(variable_context
, var
);
1854 /* The constant_value of a uniform variable is its initializer,
1855 * not the lifetime constant value of the uniform.
1857 if (var
->data
.mode
== ir_var_uniform
)
1860 if (!var
->constant_value
)
1863 return var
->constant_value
->clone(ralloc_parent(var
), NULL
);
1868 ir_dereference_array::constant_expression_value(struct hash_table
*variable_context
)
1870 ir_constant
*array
= this->array
->constant_expression_value(variable_context
);
1871 ir_constant
*idx
= this->array_index
->constant_expression_value(variable_context
);
1873 if ((array
!= NULL
) && (idx
!= NULL
)) {
1874 void *ctx
= ralloc_parent(this);
1875 if (array
->type
->is_matrix()) {
1876 /* Array access of a matrix results in a vector.
1878 const unsigned column
= idx
->value
.u
[0];
1880 const glsl_type
*const column_type
= array
->type
->column_type();
1882 /* Offset in the constant matrix to the first element of the column
1885 const unsigned mat_idx
= column
* column_type
->vector_elements
;
1887 ir_constant_data data
= { { 0 } };
1889 switch (column_type
->base_type
) {
1890 case GLSL_TYPE_UINT
:
1892 for (unsigned i
= 0; i
< column_type
->vector_elements
; i
++)
1893 data
.u
[i
] = array
->value
.u
[mat_idx
+ i
];
1897 case GLSL_TYPE_FLOAT
:
1898 for (unsigned i
= 0; i
< column_type
->vector_elements
; i
++)
1899 data
.f
[i
] = array
->value
.f
[mat_idx
+ i
];
1903 case GLSL_TYPE_DOUBLE
:
1904 for (unsigned i
= 0; i
< column_type
->vector_elements
; i
++)
1905 data
.d
[i
] = array
->value
.d
[mat_idx
+ i
];
1910 assert(!"Should not get here.");
1914 return new(ctx
) ir_constant(column_type
, &data
);
1915 } else if (array
->type
->is_vector()) {
1916 const unsigned component
= idx
->value
.u
[0];
1918 return new(ctx
) ir_constant(array
, component
);
1920 const unsigned index
= idx
->value
.u
[0];
1921 return array
->get_array_element(index
)->clone(ctx
, NULL
);
1929 ir_dereference_record::constant_expression_value(struct hash_table
*)
1931 ir_constant
*v
= this->record
->constant_expression_value();
1933 return (v
!= NULL
) ? v
->get_record_field(this->field
) : NULL
;
1938 ir_assignment::constant_expression_value(struct hash_table
*)
1940 /* FINISHME: Handle CEs involving assignment (return RHS) */
1946 ir_constant::constant_expression_value(struct hash_table
*)
1953 ir_call::constant_expression_value(struct hash_table
*variable_context
)
1955 return this->callee
->constant_expression_value(&this->actual_parameters
, variable_context
);
1959 bool ir_function_signature::constant_expression_evaluate_expression_list(const struct exec_list
&body
,
1960 struct hash_table
*variable_context
,
1961 ir_constant
**result
)
1963 foreach_in_list(ir_instruction
, inst
, &body
) {
1964 switch(inst
->ir_type
) {
1966 /* (declare () type symbol) */
1967 case ir_type_variable
: {
1968 ir_variable
*var
= inst
->as_variable();
1969 hash_table_insert(variable_context
, ir_constant::zero(this, var
->type
), var
);
1973 /* (assign [condition] (write-mask) (ref) (value)) */
1974 case ir_type_assignment
: {
1975 ir_assignment
*asg
= inst
->as_assignment();
1976 if (asg
->condition
) {
1977 ir_constant
*cond
= asg
->condition
->constant_expression_value(variable_context
);
1980 if (!cond
->get_bool_component(0))
1984 ir_constant
*store
= NULL
;
1987 if (!constant_referenced(asg
->lhs
, variable_context
, store
, offset
))
1990 ir_constant
*value
= asg
->rhs
->constant_expression_value(variable_context
);
1995 store
->copy_masked_offset(value
, offset
, asg
->write_mask
);
1999 /* (return (expression)) */
2000 case ir_type_return
:
2002 *result
= inst
->as_return()->value
->constant_expression_value(variable_context
);
2003 return *result
!= NULL
;
2005 /* (call name (ref) (params))*/
2006 case ir_type_call
: {
2007 ir_call
*call
= inst
->as_call();
2009 /* Just say no to void functions in constant expressions. We
2010 * don't need them at that point.
2013 if (!call
->return_deref
)
2016 ir_constant
*store
= NULL
;
2019 if (!constant_referenced(call
->return_deref
, variable_context
,
2023 ir_constant
*value
= call
->constant_expression_value(variable_context
);
2028 store
->copy_offset(value
, offset
);
2032 /* (if condition (then-instructions) (else-instructions)) */
2034 ir_if
*iif
= inst
->as_if();
2036 ir_constant
*cond
= iif
->condition
->constant_expression_value(variable_context
);
2037 if (!cond
|| !cond
->type
->is_boolean())
2040 exec_list
&branch
= cond
->get_bool_component(0) ? iif
->then_instructions
: iif
->else_instructions
;
2043 if (!constant_expression_evaluate_expression_list(branch
, variable_context
, result
))
2046 /* If there was a return in the branch chosen, drop out now. */
2053 /* Every other expression type, we drop out. */
2059 /* Reaching the end of the block is not an error condition */
2067 ir_function_signature::constant_expression_value(exec_list
*actual_parameters
, struct hash_table
*variable_context
)
2069 const glsl_type
*type
= this->return_type
;
2070 if (type
== glsl_type::void_type
)
2073 /* From the GLSL 1.20 spec, page 23:
2074 * "Function calls to user-defined functions (non-built-in functions)
2075 * cannot be used to form constant expressions."
2077 if (!this->is_builtin())
2081 * Of the builtin functions, only the texture lookups and the noise
2082 * ones must not be used in constant expressions. They all include
2083 * specific opcodes so they don't need to be special-cased at this
2087 /* Initialize the table of dereferencable names with the function
2088 * parameters. Verify their const-ness on the way.
2090 * We expect the correctness of the number of parameters to have
2091 * been checked earlier.
2093 hash_table
*deref_hash
= hash_table_ctor(8, hash_table_pointer_hash
,
2094 hash_table_pointer_compare
);
2096 /* If "origin" is non-NULL, then the function body is there. So we
2097 * have to use the variable objects from the object with the body,
2098 * but the parameter instanciation on the current object.
2100 const exec_node
*parameter_info
= origin
? origin
->parameters
.head
: parameters
.head
;
2102 foreach_in_list(ir_rvalue
, n
, actual_parameters
) {
2103 ir_constant
*constant
= n
->constant_expression_value(variable_context
);
2104 if (constant
== NULL
) {
2105 hash_table_dtor(deref_hash
);
2110 ir_variable
*var
= (ir_variable
*)parameter_info
;
2111 hash_table_insert(deref_hash
, constant
, var
);
2113 parameter_info
= parameter_info
->next
;
2116 ir_constant
*result
= NULL
;
2118 /* Now run the builtin function until something non-constant
2119 * happens or we get the result.
2121 if (constant_expression_evaluate_expression_list(origin
? origin
->body
: body
, deref_hash
, &result
) && result
)
2122 result
= result
->clone(ralloc_parent(this), NULL
);
2124 hash_table_dtor(deref_hash
);