2 * Copyright © 2010 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
25 * \file ir_constant_expression.cpp
26 * Evaluate and process constant valued expressions
28 * In GLSL, constant valued expressions are used in several places. These
29 * must be processed and evaluated very early in the compilation process.
32 * * Initializers for uniforms
33 * * Initializers for \c const variables
37 #include "main/core.h" /* for MAX2, MIN2, CLAMP */
39 #include "ir_visitor.h"
40 #include "glsl_types.h"
41 #include "program/hash_table.h"
43 #if defined(_MSC_VER) && (_MSC_VER < 1800)
44 static int isnormal(double x
)
46 return _fpclass(x
) == _FPCLASS_NN
|| _fpclass(x
) == _FPCLASS_PN
;
48 #elif defined(__SUNPRO_CC)
50 static int isnormal(double x
)
52 return fpclass(x
) == FP_NORMAL
;
57 static double copysign(double x
, double y
)
59 return _copysign(x
, y
);
64 dot(ir_constant
*op0
, ir_constant
*op1
)
66 assert(op0
->type
->is_float() && op1
->type
->is_float());
69 for (unsigned c
= 0; c
< op0
->type
->components(); c
++)
70 result
+= op0
->value
.f
[c
] * op1
->value
.f
[c
];
75 /* This method is the only one supported by gcc. Unions in particular
76 * are iffy, and read-through-converted-pointer is killed by strict
77 * aliasing. OTOH, the compiler sees through the memcpy, so the
78 * resulting asm is reasonable.
81 bitcast_u2f(unsigned int u
)
83 assert(sizeof(float) == sizeof(unsigned int));
85 memcpy(&f
, &u
, sizeof(f
));
92 assert(sizeof(float) == sizeof(unsigned int));
94 memcpy(&u
, &f
, sizeof(f
));
99 * Evaluate one component of a floating-point 4x8 unpacking function.
102 (*pack_1x8_func_t
)(float);
105 * Evaluate one component of a floating-point 2x16 unpacking function.
108 (*pack_1x16_func_t
)(float);
111 * Evaluate one component of a floating-point 4x8 unpacking function.
114 (*unpack_1x8_func_t
)(uint8_t);
117 * Evaluate one component of a floating-point 2x16 unpacking function.
120 (*unpack_1x16_func_t
)(uint16_t);
123 * Evaluate a 2x16 floating-point packing function.
126 pack_2x16(pack_1x16_func_t pack_1x16
,
129 /* From section 8.4 of the GLSL ES 3.00 spec:
133 * The first component of the vector will be written to the least
134 * significant bits of the output; the last component will be written to
135 * the most significant bits.
137 * The specifications for the other packing functions contain similar
141 u
|= ((uint32_t) pack_1x16(x
) << 0);
142 u
|= ((uint32_t) pack_1x16(y
) << 16);
147 * Evaluate a 4x8 floating-point packing function.
150 pack_4x8(pack_1x8_func_t pack_1x8
,
151 float x
, float y
, float z
, float w
)
153 /* From section 8.4 of the GLSL 4.30 spec:
157 * The first component of the vector will be written to the least
158 * significant bits of the output; the last component will be written to
159 * the most significant bits.
161 * The specifications for the other packing functions contain similar
165 u
|= ((uint32_t) pack_1x8(x
) << 0);
166 u
|= ((uint32_t) pack_1x8(y
) << 8);
167 u
|= ((uint32_t) pack_1x8(z
) << 16);
168 u
|= ((uint32_t) pack_1x8(w
) << 24);
173 * Evaluate a 2x16 floating-point unpacking function.
176 unpack_2x16(unpack_1x16_func_t unpack_1x16
,
180 /* From section 8.4 of the GLSL ES 3.00 spec:
184 * The first component of the returned vector will be extracted from
185 * the least significant bits of the input; the last component will be
186 * extracted from the most significant bits.
188 * The specifications for the other unpacking functions contain similar
191 *x
= unpack_1x16((uint16_t) (u
& 0xffff));
192 *y
= unpack_1x16((uint16_t) (u
>> 16));
196 * Evaluate a 4x8 floating-point unpacking function.
199 unpack_4x8(unpack_1x8_func_t unpack_1x8
, uint32_t u
,
200 float *x
, float *y
, float *z
, float *w
)
202 /* From section 8.4 of the GLSL 4.30 spec:
206 * The first component of the returned vector will be extracted from
207 * the least significant bits of the input; the last component will be
208 * extracted from the most significant bits.
210 * The specifications for the other unpacking functions contain similar
213 *x
= unpack_1x8((uint8_t) (u
& 0xff));
214 *y
= unpack_1x8((uint8_t) (u
>> 8));
215 *z
= unpack_1x8((uint8_t) (u
>> 16));
216 *w
= unpack_1x8((uint8_t) (u
>> 24));
220 * Evaluate one component of packSnorm4x8.
223 pack_snorm_1x8(float x
)
225 /* From section 8.4 of the GLSL 4.30 spec:
229 * The conversion for component c of v to fixed point is done as
232 * packSnorm4x8: round(clamp(c, -1, +1) * 127.0)
234 * We must first cast the float to an int, because casting a negative
235 * float to a uint is undefined.
237 return (uint8_t) (int8_t)
238 _mesa_round_to_even(CLAMP(x
, -1.0f
, +1.0f
) * 127.0f
);
242 * Evaluate one component of packSnorm2x16.
245 pack_snorm_1x16(float x
)
247 /* From section 8.4 of the GLSL ES 3.00 spec:
251 * The conversion for component c of v to fixed point is done as
254 * packSnorm2x16: round(clamp(c, -1, +1) * 32767.0)
256 * We must first cast the float to an int, because casting a negative
257 * float to a uint is undefined.
259 return (uint16_t) (int16_t)
260 _mesa_round_to_even(CLAMP(x
, -1.0f
, +1.0f
) * 32767.0f
);
264 * Evaluate one component of unpackSnorm4x8.
267 unpack_snorm_1x8(uint8_t u
)
269 /* From section 8.4 of the GLSL 4.30 spec:
273 * The conversion for unpacked fixed-point value f to floating point is
276 * unpackSnorm4x8: clamp(f / 127.0, -1, +1)
278 return CLAMP((int8_t) u
/ 127.0f
, -1.0f
, +1.0f
);
282 * Evaluate one component of unpackSnorm2x16.
285 unpack_snorm_1x16(uint16_t u
)
287 /* From section 8.4 of the GLSL ES 3.00 spec:
291 * The conversion for unpacked fixed-point value f to floating point is
294 * unpackSnorm2x16: clamp(f / 32767.0, -1, +1)
296 return CLAMP((int16_t) u
/ 32767.0f
, -1.0f
, +1.0f
);
300 * Evaluate one component packUnorm4x8.
303 pack_unorm_1x8(float x
)
305 /* From section 8.4 of the GLSL 4.30 spec:
309 * The conversion for component c of v to fixed point is done as
312 * packUnorm4x8: round(clamp(c, 0, +1) * 255.0)
314 return (uint8_t) _mesa_round_to_even(CLAMP(x
, 0.0f
, 1.0f
) * 255.0f
);
318 * Evaluate one component packUnorm2x16.
321 pack_unorm_1x16(float x
)
323 /* From section 8.4 of the GLSL ES 3.00 spec:
327 * The conversion for component c of v to fixed point is done as
330 * packUnorm2x16: round(clamp(c, 0, +1) * 65535.0)
332 return (uint16_t) _mesa_round_to_even(CLAMP(x
, 0.0f
, 1.0f
) * 65535.0f
);
336 * Evaluate one component of unpackUnorm4x8.
339 unpack_unorm_1x8(uint8_t u
)
341 /* From section 8.4 of the GLSL 4.30 spec:
345 * The conversion for unpacked fixed-point value f to floating point is
348 * unpackUnorm4x8: f / 255.0
350 return (float) u
/ 255.0f
;
354 * Evaluate one component of unpackUnorm2x16.
357 unpack_unorm_1x16(uint16_t u
)
359 /* From section 8.4 of the GLSL ES 3.00 spec:
363 * The conversion for unpacked fixed-point value f to floating point is
366 * unpackUnorm2x16: f / 65535.0
368 return (float) u
/ 65535.0f
;
372 * Evaluate one component of packHalf2x16.
375 pack_half_1x16(float x
)
377 return _mesa_float_to_half(x
);
381 * Evaluate one component of unpackHalf2x16.
384 unpack_half_1x16(uint16_t u
)
386 return _mesa_half_to_float(u
);
390 * \name Functions to get the constant referenced by an r-value
392 * Get the constant that is ultimately referenced by an r-value, in a constant
393 * expression evaluation context.
395 * The offset is used when the reference is to a specific column of a matrix.
399 constant_referenced(const ir_dereference
*deref
,
400 struct hash_table
*variable_context
,
401 ir_constant
*&store
, int &offset
)
406 if (variable_context
== NULL
)
409 switch (deref
->ir_type
) {
410 case ir_type_dereference_array
:
411 ((ir_dereference_array
*) deref
)->constant_referenced(variable_context
,
415 case ir_type_dereference_record
: {
416 const ir_dereference_record
*const dr
=
417 (const ir_dereference_record
*) deref
;
419 const ir_dereference
*const deref
= dr
->record
->as_dereference();
423 ir_constant
*substore
;
426 if (!constant_referenced(deref
, variable_context
, substore
, suboffset
))
429 /* Since we're dropping it on the floor...
431 assert(suboffset
== 0);
433 store
= substore
->get_record_field(dr
->field
);
437 case ir_type_dereference_variable
: {
438 const ir_dereference_variable
*const dv
=
439 (const ir_dereference_variable
*) deref
;
441 store
= (ir_constant
*) hash_table_find(variable_context
, dv
->var
);
446 assert(!"Should not get here.");
450 return store
!= NULL
;
454 ir_dereference_variable::constant_referenced(struct hash_table
*variable_context
,
455 ir_constant
*&store
, int &offset
) const
457 ::constant_referenced(this, variable_context
, store
, offset
);
461 ir_dereference_array::constant_referenced(struct hash_table
*variable_context
,
462 ir_constant
*&store
, int &offset
) const
464 ir_constant
*index_c
= array_index
->constant_expression_value(variable_context
);
466 if (!index_c
|| !index_c
->type
->is_scalar() || !index_c
->type
->is_integer()) {
472 int index
= index_c
->type
->base_type
== GLSL_TYPE_INT
?
473 index_c
->get_int_component(0) :
474 index_c
->get_uint_component(0);
476 ir_constant
*substore
;
478 const ir_dereference
*deref
= array
->as_dereference();
485 if (!::constant_referenced(deref
, variable_context
, substore
, suboffset
))
488 const glsl_type
*vt
= array
->type
;
489 if (vt
->is_array()) {
490 store
= substore
->get_array_element(index
);
494 if (vt
->is_matrix()) {
496 offset
= index
* vt
->vector_elements
;
499 if (vt
->is_vector()) {
501 offset
= suboffset
+ index
;
510 ir_dereference_record::constant_referenced(struct hash_table
*variable_context
,
511 ir_constant
*&store
, int &offset
) const
513 ::constant_referenced(this, variable_context
, store
, offset
);
519 ir_rvalue::constant_expression_value(struct hash_table
*variable_context
)
521 assert(this->type
->is_error());
526 ir_expression::constant_expression_value(struct hash_table
*variable_context
)
528 if (this->type
->is_error())
531 ir_constant
*op
[Elements(this->operands
)] = { NULL
, };
532 ir_constant_data data
;
534 memset(&data
, 0, sizeof(data
));
536 for (unsigned operand
= 0; operand
< this->get_num_operands(); operand
++) {
537 op
[operand
] = this->operands
[operand
]->constant_expression_value(variable_context
);
543 switch (this->operation
) {
544 case ir_binop_lshift
:
545 case ir_binop_rshift
:
547 case ir_binop_vector_extract
:
549 case ir_triop_bitfield_extract
:
553 assert(op
[0]->type
->base_type
== op
[1]->type
->base_type
);
557 bool op0_scalar
= op
[0]->type
->is_scalar();
558 bool op1_scalar
= op
[1] != NULL
&& op
[1]->type
->is_scalar();
560 /* When iterating over a vector or matrix's components, we want to increase
561 * the loop counter. However, for scalars, we want to stay at 0.
563 unsigned c0_inc
= op0_scalar
? 0 : 1;
564 unsigned c1_inc
= op1_scalar
? 0 : 1;
566 if (op1_scalar
|| !op
[1]) {
567 components
= op
[0]->type
->components();
569 components
= op
[1]->type
->components();
572 void *ctx
= ralloc_parent(this);
574 /* Handle array operations here, rather than below. */
575 if (op
[0]->type
->is_array()) {
576 assert(op
[1] != NULL
&& op
[1]->type
->is_array());
577 switch (this->operation
) {
578 case ir_binop_all_equal
:
579 return new(ctx
) ir_constant(op
[0]->has_value(op
[1]));
580 case ir_binop_any_nequal
:
581 return new(ctx
) ir_constant(!op
[0]->has_value(op
[1]));
588 switch (this->operation
) {
589 case ir_unop_bit_not
:
590 switch (op
[0]->type
->base_type
) {
592 for (unsigned c
= 0; c
< components
; c
++)
593 data
.i
[c
] = ~ op
[0]->value
.i
[c
];
596 for (unsigned c
= 0; c
< components
; c
++)
597 data
.u
[c
] = ~ op
[0]->value
.u
[c
];
604 case ir_unop_logic_not
:
605 assert(op
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
606 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++)
607 data
.b
[c
] = !op
[0]->value
.b
[c
];
611 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
612 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
613 data
.i
[c
] = (int) op
[0]->value
.f
[c
];
617 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
618 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
619 data
.i
[c
] = (unsigned) op
[0]->value
.f
[c
];
623 assert(op
[0]->type
->base_type
== GLSL_TYPE_INT
);
624 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
625 data
.f
[c
] = (float) op
[0]->value
.i
[c
];
629 assert(op
[0]->type
->base_type
== GLSL_TYPE_UINT
);
630 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
631 data
.f
[c
] = (float) op
[0]->value
.u
[c
];
635 assert(op
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
636 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
637 data
.f
[c
] = op
[0]->value
.b
[c
] ? 1.0F
: 0.0F
;
641 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
642 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
643 data
.b
[c
] = op
[0]->value
.f
[c
] != 0.0F
? true : false;
647 assert(op
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
648 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
649 data
.u
[c
] = op
[0]->value
.b
[c
] ? 1 : 0;
653 assert(op
[0]->type
->is_integer());
654 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
655 data
.b
[c
] = op
[0]->value
.u
[c
] ? true : false;
659 assert(op
[0]->type
->base_type
== GLSL_TYPE_UINT
);
660 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
661 data
.i
[c
] = op
[0]->value
.u
[c
];
665 assert(op
[0]->type
->base_type
== GLSL_TYPE_INT
);
666 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
667 data
.u
[c
] = op
[0]->value
.i
[c
];
670 case ir_unop_bitcast_i2f
:
671 assert(op
[0]->type
->base_type
== GLSL_TYPE_INT
);
672 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
673 data
.f
[c
] = bitcast_u2f(op
[0]->value
.i
[c
]);
676 case ir_unop_bitcast_f2i
:
677 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
678 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
679 data
.i
[c
] = bitcast_f2u(op
[0]->value
.f
[c
]);
682 case ir_unop_bitcast_u2f
:
683 assert(op
[0]->type
->base_type
== GLSL_TYPE_UINT
);
684 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
685 data
.f
[c
] = bitcast_u2f(op
[0]->value
.u
[c
]);
688 case ir_unop_bitcast_f2u
:
689 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
690 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
691 data
.u
[c
] = bitcast_f2u(op
[0]->value
.f
[c
]);
695 assert(op
[0]->type
->is_boolean());
697 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
698 if (op
[0]->value
.b
[c
])
704 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
705 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
706 data
.f
[c
] = truncf(op
[0]->value
.f
[c
]);
710 case ir_unop_round_even
:
711 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
712 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
713 data
.f
[c
] = _mesa_round_to_even(op
[0]->value
.f
[c
]);
718 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
719 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
720 data
.f
[c
] = ceilf(op
[0]->value
.f
[c
]);
725 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
726 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
727 data
.f
[c
] = floorf(op
[0]->value
.f
[c
]);
732 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
733 switch (this->type
->base_type
) {
740 case GLSL_TYPE_FLOAT
:
741 data
.f
[c
] = op
[0]->value
.f
[c
] - floor(op
[0]->value
.f
[c
]);
750 case ir_unop_sin_reduced
:
751 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
752 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
753 data
.f
[c
] = sinf(op
[0]->value
.f
[c
]);
758 case ir_unop_cos_reduced
:
759 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
760 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
761 data
.f
[c
] = cosf(op
[0]->value
.f
[c
]);
766 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
767 switch (this->type
->base_type
) {
769 data
.u
[c
] = -((int) op
[0]->value
.u
[c
]);
772 data
.i
[c
] = -op
[0]->value
.i
[c
];
774 case GLSL_TYPE_FLOAT
:
775 data
.f
[c
] = -op
[0]->value
.f
[c
];
784 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
785 switch (this->type
->base_type
) {
787 data
.u
[c
] = op
[0]->value
.u
[c
];
790 data
.i
[c
] = op
[0]->value
.i
[c
];
792 data
.i
[c
] = -data
.i
[c
];
794 case GLSL_TYPE_FLOAT
:
795 data
.f
[c
] = fabs(op
[0]->value
.f
[c
]);
804 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
805 switch (this->type
->base_type
) {
807 data
.u
[c
] = op
[0]->value
.i
[c
] > 0;
810 data
.i
[c
] = (op
[0]->value
.i
[c
] > 0) - (op
[0]->value
.i
[c
] < 0);
812 case GLSL_TYPE_FLOAT
:
813 data
.f
[c
] = float((op
[0]->value
.f
[c
] > 0)-(op
[0]->value
.f
[c
] < 0));
822 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
823 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
824 switch (this->type
->base_type
) {
826 if (op
[0]->value
.u
[c
] != 0.0)
827 data
.u
[c
] = 1 / op
[0]->value
.u
[c
];
830 if (op
[0]->value
.i
[c
] != 0.0)
831 data
.i
[c
] = 1 / op
[0]->value
.i
[c
];
833 case GLSL_TYPE_FLOAT
:
834 if (op
[0]->value
.f
[c
] != 0.0)
835 data
.f
[c
] = 1.0F
/ op
[0]->value
.f
[c
];
844 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
845 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
846 data
.f
[c
] = 1.0F
/ sqrtf(op
[0]->value
.f
[c
]);
851 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
852 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
853 data
.f
[c
] = sqrtf(op
[0]->value
.f
[c
]);
858 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
859 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
860 data
.f
[c
] = expf(op
[0]->value
.f
[c
]);
865 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
866 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
867 data
.f
[c
] = exp2f(op
[0]->value
.f
[c
]);
872 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
873 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
874 data
.f
[c
] = logf(op
[0]->value
.f
[c
]);
879 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
880 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
881 data
.f
[c
] = log2f(op
[0]->value
.f
[c
]);
887 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
888 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
893 case ir_unop_pack_snorm_2x16
:
894 assert(op
[0]->type
== glsl_type::vec2_type
);
895 data
.u
[0] = pack_2x16(pack_snorm_1x16
,
899 case ir_unop_pack_snorm_4x8
:
900 assert(op
[0]->type
== glsl_type::vec4_type
);
901 data
.u
[0] = pack_4x8(pack_snorm_1x8
,
907 case ir_unop_unpack_snorm_2x16
:
908 assert(op
[0]->type
== glsl_type::uint_type
);
909 unpack_2x16(unpack_snorm_1x16
,
911 &data
.f
[0], &data
.f
[1]);
913 case ir_unop_unpack_snorm_4x8
:
914 assert(op
[0]->type
== glsl_type::uint_type
);
915 unpack_4x8(unpack_snorm_1x8
,
917 &data
.f
[0], &data
.f
[1], &data
.f
[2], &data
.f
[3]);
919 case ir_unop_pack_unorm_2x16
:
920 assert(op
[0]->type
== glsl_type::vec2_type
);
921 data
.u
[0] = pack_2x16(pack_unorm_1x16
,
925 case ir_unop_pack_unorm_4x8
:
926 assert(op
[0]->type
== glsl_type::vec4_type
);
927 data
.u
[0] = pack_4x8(pack_unorm_1x8
,
933 case ir_unop_unpack_unorm_2x16
:
934 assert(op
[0]->type
== glsl_type::uint_type
);
935 unpack_2x16(unpack_unorm_1x16
,
937 &data
.f
[0], &data
.f
[1]);
939 case ir_unop_unpack_unorm_4x8
:
940 assert(op
[0]->type
== glsl_type::uint_type
);
941 unpack_4x8(unpack_unorm_1x8
,
943 &data
.f
[0], &data
.f
[1], &data
.f
[2], &data
.f
[3]);
945 case ir_unop_pack_half_2x16
:
946 assert(op
[0]->type
== glsl_type::vec2_type
);
947 data
.u
[0] = pack_2x16(pack_half_1x16
,
951 case ir_unop_unpack_half_2x16
:
952 assert(op
[0]->type
== glsl_type::uint_type
);
953 unpack_2x16(unpack_half_1x16
,
955 &data
.f
[0], &data
.f
[1]);
958 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
959 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
960 data
.f
[c
] = powf(op
[0]->value
.f
[c
], op
[1]->value
.f
[c
]);
965 data
.f
[0] = dot(op
[0], op
[1]);
969 assert(op
[0]->type
== op
[1]->type
|| op0_scalar
|| op1_scalar
);
970 for (unsigned c
= 0, c0
= 0, c1
= 0;
972 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
974 switch (op
[0]->type
->base_type
) {
976 data
.u
[c
] = MIN2(op
[0]->value
.u
[c0
], op
[1]->value
.u
[c1
]);
979 data
.i
[c
] = MIN2(op
[0]->value
.i
[c0
], op
[1]->value
.i
[c1
]);
981 case GLSL_TYPE_FLOAT
:
982 data
.f
[c
] = MIN2(op
[0]->value
.f
[c0
], op
[1]->value
.f
[c1
]);
991 assert(op
[0]->type
== op
[1]->type
|| op0_scalar
|| op1_scalar
);
992 for (unsigned c
= 0, c0
= 0, c1
= 0;
994 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
996 switch (op
[0]->type
->base_type
) {
998 data
.u
[c
] = MAX2(op
[0]->value
.u
[c0
], op
[1]->value
.u
[c1
]);
1001 data
.i
[c
] = MAX2(op
[0]->value
.i
[c0
], op
[1]->value
.i
[c1
]);
1003 case GLSL_TYPE_FLOAT
:
1004 data
.f
[c
] = MAX2(op
[0]->value
.f
[c0
], op
[1]->value
.f
[c1
]);
1013 assert(op
[0]->type
== op
[1]->type
|| op0_scalar
|| op1_scalar
);
1014 for (unsigned c
= 0, c0
= 0, c1
= 0;
1016 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
1018 switch (op
[0]->type
->base_type
) {
1019 case GLSL_TYPE_UINT
:
1020 data
.u
[c
] = op
[0]->value
.u
[c0
] + op
[1]->value
.u
[c1
];
1023 data
.i
[c
] = op
[0]->value
.i
[c0
] + op
[1]->value
.i
[c1
];
1025 case GLSL_TYPE_FLOAT
:
1026 data
.f
[c
] = op
[0]->value
.f
[c0
] + op
[1]->value
.f
[c1
];
1035 assert(op
[0]->type
== op
[1]->type
|| op0_scalar
|| op1_scalar
);
1036 for (unsigned c
= 0, c0
= 0, c1
= 0;
1038 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
1040 switch (op
[0]->type
->base_type
) {
1041 case GLSL_TYPE_UINT
:
1042 data
.u
[c
] = op
[0]->value
.u
[c0
] - op
[1]->value
.u
[c1
];
1045 data
.i
[c
] = op
[0]->value
.i
[c0
] - op
[1]->value
.i
[c1
];
1047 case GLSL_TYPE_FLOAT
:
1048 data
.f
[c
] = op
[0]->value
.f
[c0
] - op
[1]->value
.f
[c1
];
1057 /* Check for equal types, or unequal types involving scalars */
1058 if ((op
[0]->type
== op
[1]->type
&& !op
[0]->type
->is_matrix())
1059 || op0_scalar
|| op1_scalar
) {
1060 for (unsigned c
= 0, c0
= 0, c1
= 0;
1062 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
1064 switch (op
[0]->type
->base_type
) {
1065 case GLSL_TYPE_UINT
:
1066 data
.u
[c
] = op
[0]->value
.u
[c0
] * op
[1]->value
.u
[c1
];
1069 data
.i
[c
] = op
[0]->value
.i
[c0
] * op
[1]->value
.i
[c1
];
1071 case GLSL_TYPE_FLOAT
:
1072 data
.f
[c
] = op
[0]->value
.f
[c0
] * op
[1]->value
.f
[c1
];
1079 assert(op
[0]->type
->is_matrix() || op
[1]->type
->is_matrix());
1081 /* Multiply an N-by-M matrix with an M-by-P matrix. Since either
1082 * matrix can be a GLSL vector, either N or P can be 1.
1084 * For vec*mat, the vector is treated as a row vector. This
1085 * means the vector is a 1-row x M-column matrix.
1087 * For mat*vec, the vector is treated as a column vector. Since
1088 * matrix_columns is 1 for vectors, this just works.
1090 const unsigned n
= op
[0]->type
->is_vector()
1091 ? 1 : op
[0]->type
->vector_elements
;
1092 const unsigned m
= op
[1]->type
->vector_elements
;
1093 const unsigned p
= op
[1]->type
->matrix_columns
;
1094 for (unsigned j
= 0; j
< p
; j
++) {
1095 for (unsigned i
= 0; i
< n
; i
++) {
1096 for (unsigned k
= 0; k
< m
; k
++) {
1097 data
.f
[i
+n
*j
] += op
[0]->value
.f
[i
+n
*k
]*op
[1]->value
.f
[k
+m
*j
];
1105 /* FINISHME: Emit warning when division-by-zero is detected. */
1106 assert(op
[0]->type
== op
[1]->type
|| op0_scalar
|| op1_scalar
);
1107 for (unsigned c
= 0, c0
= 0, c1
= 0;
1109 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
1111 switch (op
[0]->type
->base_type
) {
1112 case GLSL_TYPE_UINT
:
1113 if (op
[1]->value
.u
[c1
] == 0) {
1116 data
.u
[c
] = op
[0]->value
.u
[c0
] / op
[1]->value
.u
[c1
];
1120 if (op
[1]->value
.i
[c1
] == 0) {
1123 data
.i
[c
] = op
[0]->value
.i
[c0
] / op
[1]->value
.i
[c1
];
1126 case GLSL_TYPE_FLOAT
:
1127 data
.f
[c
] = op
[0]->value
.f
[c0
] / op
[1]->value
.f
[c1
];
1136 /* FINISHME: Emit warning when division-by-zero is detected. */
1137 assert(op
[0]->type
== op
[1]->type
|| op0_scalar
|| op1_scalar
);
1138 for (unsigned c
= 0, c0
= 0, c1
= 0;
1140 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
1142 switch (op
[0]->type
->base_type
) {
1143 case GLSL_TYPE_UINT
:
1144 if (op
[1]->value
.u
[c1
] == 0) {
1147 data
.u
[c
] = op
[0]->value
.u
[c0
] % op
[1]->value
.u
[c1
];
1151 if (op
[1]->value
.i
[c1
] == 0) {
1154 data
.i
[c
] = op
[0]->value
.i
[c0
] % op
[1]->value
.i
[c1
];
1157 case GLSL_TYPE_FLOAT
:
1158 /* We don't use fmod because it rounds toward zero; GLSL specifies
1161 data
.f
[c
] = op
[0]->value
.f
[c0
] - op
[1]->value
.f
[c1
]
1162 * floorf(op
[0]->value
.f
[c0
] / op
[1]->value
.f
[c1
]);
1171 case ir_binop_logic_and
:
1172 assert(op
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
1173 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++)
1174 data
.b
[c
] = op
[0]->value
.b
[c
] && op
[1]->value
.b
[c
];
1176 case ir_binop_logic_xor
:
1177 assert(op
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
1178 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++)
1179 data
.b
[c
] = op
[0]->value
.b
[c
] ^ op
[1]->value
.b
[c
];
1181 case ir_binop_logic_or
:
1182 assert(op
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
1183 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++)
1184 data
.b
[c
] = op
[0]->value
.b
[c
] || op
[1]->value
.b
[c
];
1188 assert(op
[0]->type
== op
[1]->type
);
1189 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
1190 switch (op
[0]->type
->base_type
) {
1191 case GLSL_TYPE_UINT
:
1192 data
.b
[c
] = op
[0]->value
.u
[c
] < op
[1]->value
.u
[c
];
1195 data
.b
[c
] = op
[0]->value
.i
[c
] < op
[1]->value
.i
[c
];
1197 case GLSL_TYPE_FLOAT
:
1198 data
.b
[c
] = op
[0]->value
.f
[c
] < op
[1]->value
.f
[c
];
1205 case ir_binop_greater
:
1206 assert(op
[0]->type
== op
[1]->type
);
1207 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
1208 switch (op
[0]->type
->base_type
) {
1209 case GLSL_TYPE_UINT
:
1210 data
.b
[c
] = op
[0]->value
.u
[c
] > op
[1]->value
.u
[c
];
1213 data
.b
[c
] = op
[0]->value
.i
[c
] > op
[1]->value
.i
[c
];
1215 case GLSL_TYPE_FLOAT
:
1216 data
.b
[c
] = op
[0]->value
.f
[c
] > op
[1]->value
.f
[c
];
1223 case ir_binop_lequal
:
1224 assert(op
[0]->type
== op
[1]->type
);
1225 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
1226 switch (op
[0]->type
->base_type
) {
1227 case GLSL_TYPE_UINT
:
1228 data
.b
[c
] = op
[0]->value
.u
[c
] <= op
[1]->value
.u
[c
];
1231 data
.b
[c
] = op
[0]->value
.i
[c
] <= op
[1]->value
.i
[c
];
1233 case GLSL_TYPE_FLOAT
:
1234 data
.b
[c
] = op
[0]->value
.f
[c
] <= op
[1]->value
.f
[c
];
1241 case ir_binop_gequal
:
1242 assert(op
[0]->type
== op
[1]->type
);
1243 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
1244 switch (op
[0]->type
->base_type
) {
1245 case GLSL_TYPE_UINT
:
1246 data
.b
[c
] = op
[0]->value
.u
[c
] >= op
[1]->value
.u
[c
];
1249 data
.b
[c
] = op
[0]->value
.i
[c
] >= op
[1]->value
.i
[c
];
1251 case GLSL_TYPE_FLOAT
:
1252 data
.b
[c
] = op
[0]->value
.f
[c
] >= op
[1]->value
.f
[c
];
1259 case ir_binop_equal
:
1260 assert(op
[0]->type
== op
[1]->type
);
1261 for (unsigned c
= 0; c
< components
; c
++) {
1262 switch (op
[0]->type
->base_type
) {
1263 case GLSL_TYPE_UINT
:
1264 data
.b
[c
] = op
[0]->value
.u
[c
] == op
[1]->value
.u
[c
];
1267 data
.b
[c
] = op
[0]->value
.i
[c
] == op
[1]->value
.i
[c
];
1269 case GLSL_TYPE_FLOAT
:
1270 data
.b
[c
] = op
[0]->value
.f
[c
] == op
[1]->value
.f
[c
];
1272 case GLSL_TYPE_BOOL
:
1273 data
.b
[c
] = op
[0]->value
.b
[c
] == op
[1]->value
.b
[c
];
1280 case ir_binop_nequal
:
1281 assert(op
[0]->type
== op
[1]->type
);
1282 for (unsigned c
= 0; c
< components
; c
++) {
1283 switch (op
[0]->type
->base_type
) {
1284 case GLSL_TYPE_UINT
:
1285 data
.b
[c
] = op
[0]->value
.u
[c
] != op
[1]->value
.u
[c
];
1288 data
.b
[c
] = op
[0]->value
.i
[c
] != op
[1]->value
.i
[c
];
1290 case GLSL_TYPE_FLOAT
:
1291 data
.b
[c
] = op
[0]->value
.f
[c
] != op
[1]->value
.f
[c
];
1293 case GLSL_TYPE_BOOL
:
1294 data
.b
[c
] = op
[0]->value
.b
[c
] != op
[1]->value
.b
[c
];
1301 case ir_binop_all_equal
:
1302 data
.b
[0] = op
[0]->has_value(op
[1]);
1304 case ir_binop_any_nequal
:
1305 data
.b
[0] = !op
[0]->has_value(op
[1]);
1308 case ir_binop_lshift
:
1309 for (unsigned c
= 0, c0
= 0, c1
= 0;
1311 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
1313 if (op
[0]->type
->base_type
== GLSL_TYPE_INT
&&
1314 op
[1]->type
->base_type
== GLSL_TYPE_INT
) {
1315 data
.i
[c
] = op
[0]->value
.i
[c0
] << op
[1]->value
.i
[c1
];
1317 } else if (op
[0]->type
->base_type
== GLSL_TYPE_INT
&&
1318 op
[1]->type
->base_type
== GLSL_TYPE_UINT
) {
1319 data
.i
[c
] = op
[0]->value
.i
[c0
] << op
[1]->value
.u
[c1
];
1321 } else if (op
[0]->type
->base_type
== GLSL_TYPE_UINT
&&
1322 op
[1]->type
->base_type
== GLSL_TYPE_INT
) {
1323 data
.u
[c
] = op
[0]->value
.u
[c0
] << op
[1]->value
.i
[c1
];
1325 } else if (op
[0]->type
->base_type
== GLSL_TYPE_UINT
&&
1326 op
[1]->type
->base_type
== GLSL_TYPE_UINT
) {
1327 data
.u
[c
] = op
[0]->value
.u
[c0
] << op
[1]->value
.u
[c1
];
1332 case ir_binop_rshift
:
1333 for (unsigned c
= 0, c0
= 0, c1
= 0;
1335 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
1337 if (op
[0]->type
->base_type
== GLSL_TYPE_INT
&&
1338 op
[1]->type
->base_type
== GLSL_TYPE_INT
) {
1339 data
.i
[c
] = op
[0]->value
.i
[c0
] >> op
[1]->value
.i
[c1
];
1341 } else if (op
[0]->type
->base_type
== GLSL_TYPE_INT
&&
1342 op
[1]->type
->base_type
== GLSL_TYPE_UINT
) {
1343 data
.i
[c
] = op
[0]->value
.i
[c0
] >> op
[1]->value
.u
[c1
];
1345 } else if (op
[0]->type
->base_type
== GLSL_TYPE_UINT
&&
1346 op
[1]->type
->base_type
== GLSL_TYPE_INT
) {
1347 data
.u
[c
] = op
[0]->value
.u
[c0
] >> op
[1]->value
.i
[c1
];
1349 } else if (op
[0]->type
->base_type
== GLSL_TYPE_UINT
&&
1350 op
[1]->type
->base_type
== GLSL_TYPE_UINT
) {
1351 data
.u
[c
] = op
[0]->value
.u
[c0
] >> op
[1]->value
.u
[c1
];
1356 case ir_binop_bit_and
:
1357 for (unsigned c
= 0, c0
= 0, c1
= 0;
1359 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
1361 switch (op
[0]->type
->base_type
) {
1363 data
.i
[c
] = op
[0]->value
.i
[c0
] & op
[1]->value
.i
[c1
];
1365 case GLSL_TYPE_UINT
:
1366 data
.u
[c
] = op
[0]->value
.u
[c0
] & op
[1]->value
.u
[c1
];
1374 case ir_binop_bit_or
:
1375 for (unsigned c
= 0, c0
= 0, c1
= 0;
1377 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
1379 switch (op
[0]->type
->base_type
) {
1381 data
.i
[c
] = op
[0]->value
.i
[c0
] | op
[1]->value
.i
[c1
];
1383 case GLSL_TYPE_UINT
:
1384 data
.u
[c
] = op
[0]->value
.u
[c0
] | op
[1]->value
.u
[c1
];
1392 case ir_binop_vector_extract
: {
1393 const int c
= CLAMP(op
[1]->value
.i
[0], 0,
1394 (int) op
[0]->type
->vector_elements
- 1);
1396 switch (op
[0]->type
->base_type
) {
1397 case GLSL_TYPE_UINT
:
1398 data
.u
[0] = op
[0]->value
.u
[c
];
1401 data
.i
[0] = op
[0]->value
.i
[c
];
1403 case GLSL_TYPE_FLOAT
:
1404 data
.f
[0] = op
[0]->value
.f
[c
];
1406 case GLSL_TYPE_BOOL
:
1407 data
.b
[0] = op
[0]->value
.b
[c
];
1415 case ir_binop_bit_xor
:
1416 for (unsigned c
= 0, c0
= 0, c1
= 0;
1418 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
1420 switch (op
[0]->type
->base_type
) {
1422 data
.i
[c
] = op
[0]->value
.i
[c0
] ^ op
[1]->value
.i
[c1
];
1424 case GLSL_TYPE_UINT
:
1425 data
.u
[c
] = op
[0]->value
.u
[c0
] ^ op
[1]->value
.u
[c1
];
1433 case ir_unop_bitfield_reverse
:
1434 /* http://graphics.stanford.edu/~seander/bithacks.html#BitReverseObvious */
1435 for (unsigned c
= 0; c
< components
; c
++) {
1436 unsigned int v
= op
[0]->value
.u
[c
]; // input bits to be reversed
1437 unsigned int r
= v
; // r will be reversed bits of v; first get LSB of v
1438 int s
= sizeof(v
) * CHAR_BIT
- 1; // extra shift needed at end
1440 for (v
>>= 1; v
; v
>>= 1) {
1445 r
<<= s
; // shift when v's highest bits are zero
1451 case ir_unop_bit_count
:
1452 for (unsigned c
= 0; c
< components
; c
++) {
1454 unsigned v
= op
[0]->value
.u
[c
];
1456 for (; v
; count
++) {
1463 case ir_unop_find_msb
:
1464 for (unsigned c
= 0; c
< components
; c
++) {
1465 int v
= op
[0]->value
.i
[c
];
1467 if (v
== 0 || (op
[0]->type
->base_type
== GLSL_TYPE_INT
&& v
== -1))
1471 int top_bit
= op
[0]->type
->base_type
== GLSL_TYPE_UINT
1472 ? 0 : v
& (1 << 31);
1474 while (((v
& (1 << 31)) == top_bit
) && count
!= 32) {
1479 data
.i
[c
] = 31 - count
;
1484 case ir_unop_find_lsb
:
1485 for (unsigned c
= 0; c
< components
; c
++) {
1486 if (op
[0]->value
.i
[c
] == 0)
1490 unsigned v
= op
[0]->value
.u
[c
];
1492 for (; !(v
& 1); v
>>= 1) {
1500 case ir_triop_bitfield_extract
: {
1501 int offset
= op
[1]->value
.i
[0];
1502 int bits
= op
[2]->value
.i
[0];
1504 for (unsigned c
= 0; c
< components
; c
++) {
1507 else if (offset
< 0 || bits
< 0)
1508 data
.u
[c
] = 0; /* Undefined, per spec. */
1509 else if (offset
+ bits
> 32)
1510 data
.u
[c
] = 0; /* Undefined, per spec. */
1512 if (op
[0]->type
->base_type
== GLSL_TYPE_INT
) {
1513 /* int so that the right shift will sign-extend. */
1514 int value
= op
[0]->value
.i
[c
];
1515 value
<<= 32 - bits
- offset
;
1516 value
>>= 32 - bits
;
1519 unsigned value
= op
[0]->value
.u
[c
];
1520 value
<<= 32 - bits
- offset
;
1521 value
>>= 32 - bits
;
1529 case ir_binop_bfm
: {
1530 int bits
= op
[0]->value
.i
[0];
1531 int offset
= op
[1]->value
.i
[0];
1533 for (unsigned c
= 0; c
< components
; c
++) {
1535 data
.u
[c
] = op
[0]->value
.u
[c
];
1536 else if (offset
< 0 || bits
< 0)
1537 data
.u
[c
] = 0; /* Undefined for bitfieldInsert, per spec. */
1538 else if (offset
+ bits
> 32)
1539 data
.u
[c
] = 0; /* Undefined for bitfieldInsert, per spec. */
1541 data
.u
[c
] = ((1 << bits
) - 1) << offset
;
1546 case ir_binop_ldexp
:
1547 for (unsigned c
= 0; c
< components
; c
++) {
1548 data
.f
[c
] = ldexp(op
[0]->value
.f
[c
], op
[1]->value
.i
[c
]);
1549 /* Flush subnormal values to zero. */
1550 if (!isnormal(data
.f
[c
]))
1551 data
.f
[c
] = copysign(0.0f
, op
[0]->value
.f
[c
]);
1556 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
1557 assert(op
[1]->type
->base_type
== GLSL_TYPE_FLOAT
);
1558 assert(op
[2]->type
->base_type
== GLSL_TYPE_FLOAT
);
1560 for (unsigned c
= 0; c
< components
; c
++) {
1561 data
.f
[c
] = op
[0]->value
.f
[c
] * op
[1]->value
.f
[c
]
1562 + op
[2]->value
.f
[c
];
1566 case ir_triop_lrp
: {
1567 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
1568 assert(op
[1]->type
->base_type
== GLSL_TYPE_FLOAT
);
1569 assert(op
[2]->type
->base_type
== GLSL_TYPE_FLOAT
);
1571 unsigned c2_inc
= op
[2]->type
->is_scalar() ? 0 : 1;
1572 for (unsigned c
= 0, c2
= 0; c
< components
; c2
+= c2_inc
, c
++) {
1573 data
.f
[c
] = op
[0]->value
.f
[c
] * (1.0f
- op
[2]->value
.f
[c2
]) +
1574 (op
[1]->value
.f
[c
] * op
[2]->value
.f
[c2
]);
1580 for (unsigned c
= 0; c
< components
; c
++) {
1581 data
.u
[c
] = op
[0]->value
.b
[c
] ? op
[1]->value
.u
[c
]
1582 : op
[2]->value
.u
[c
];
1586 case ir_triop_vector_insert
: {
1587 const unsigned idx
= op
[2]->value
.u
[0];
1589 memcpy(&data
, &op
[0]->value
, sizeof(data
));
1591 switch (this->type
->base_type
) {
1593 data
.i
[idx
] = op
[1]->value
.i
[0];
1595 case GLSL_TYPE_UINT
:
1596 data
.u
[idx
] = op
[1]->value
.u
[0];
1598 case GLSL_TYPE_FLOAT
:
1599 data
.f
[idx
] = op
[1]->value
.f
[0];
1601 case GLSL_TYPE_BOOL
:
1602 data
.b
[idx
] = op
[1]->value
.b
[0];
1605 assert(!"Should not get here.");
1611 case ir_quadop_bitfield_insert
: {
1612 int offset
= op
[2]->value
.i
[0];
1613 int bits
= op
[3]->value
.i
[0];
1615 for (unsigned c
= 0; c
< components
; c
++) {
1617 data
.u
[c
] = op
[0]->value
.u
[c
];
1618 else if (offset
< 0 || bits
< 0)
1619 data
.u
[c
] = 0; /* Undefined, per spec. */
1620 else if (offset
+ bits
> 32)
1621 data
.u
[c
] = 0; /* Undefined, per spec. */
1623 unsigned insert_mask
= ((1 << bits
) - 1) << offset
;
1625 unsigned insert
= op
[1]->value
.u
[c
];
1627 insert
&= insert_mask
;
1629 unsigned base
= op
[0]->value
.u
[c
];
1630 base
&= ~insert_mask
;
1632 data
.u
[c
] = base
| insert
;
1638 case ir_quadop_vector
:
1639 for (unsigned c
= 0; c
< this->type
->vector_elements
; c
++) {
1640 switch (this->type
->base_type
) {
1642 data
.i
[c
] = op
[c
]->value
.i
[0];
1644 case GLSL_TYPE_UINT
:
1645 data
.u
[c
] = op
[c
]->value
.u
[0];
1647 case GLSL_TYPE_FLOAT
:
1648 data
.f
[c
] = op
[c
]->value
.f
[0];
1657 /* FINISHME: Should handle all expression types. */
1661 return new(ctx
) ir_constant(this->type
, &data
);
1666 ir_texture::constant_expression_value(struct hash_table
*variable_context
)
1668 /* texture lookups aren't constant expressions */
1674 ir_swizzle::constant_expression_value(struct hash_table
*variable_context
)
1676 ir_constant
*v
= this->val
->constant_expression_value(variable_context
);
1679 ir_constant_data data
= { { 0 } };
1681 const unsigned swiz_idx
[4] = {
1682 this->mask
.x
, this->mask
.y
, this->mask
.z
, this->mask
.w
1685 for (unsigned i
= 0; i
< this->mask
.num_components
; i
++) {
1686 switch (v
->type
->base_type
) {
1687 case GLSL_TYPE_UINT
:
1688 case GLSL_TYPE_INT
: data
.u
[i
] = v
->value
.u
[swiz_idx
[i
]]; break;
1689 case GLSL_TYPE_FLOAT
: data
.f
[i
] = v
->value
.f
[swiz_idx
[i
]]; break;
1690 case GLSL_TYPE_BOOL
: data
.b
[i
] = v
->value
.b
[swiz_idx
[i
]]; break;
1691 default: assert(!"Should not get here."); break;
1695 void *ctx
= ralloc_parent(this);
1696 return new(ctx
) ir_constant(this->type
, &data
);
1703 ir_dereference_variable::constant_expression_value(struct hash_table
*variable_context
)
1705 /* This may occur during compile and var->type is glsl_type::error_type */
1709 /* Give priority to the context hashtable, if it exists */
1710 if (variable_context
) {
1711 ir_constant
*value
= (ir_constant
*)hash_table_find(variable_context
, var
);
1716 /* The constant_value of a uniform variable is its initializer,
1717 * not the lifetime constant value of the uniform.
1719 if (var
->data
.mode
== ir_var_uniform
)
1722 if (!var
->constant_value
)
1725 return var
->constant_value
->clone(ralloc_parent(var
), NULL
);
1730 ir_dereference_array::constant_expression_value(struct hash_table
*variable_context
)
1732 ir_constant
*array
= this->array
->constant_expression_value(variable_context
);
1733 ir_constant
*idx
= this->array_index
->constant_expression_value(variable_context
);
1735 if ((array
!= NULL
) && (idx
!= NULL
)) {
1736 void *ctx
= ralloc_parent(this);
1737 if (array
->type
->is_matrix()) {
1738 /* Array access of a matrix results in a vector.
1740 const unsigned column
= idx
->value
.u
[0];
1742 const glsl_type
*const column_type
= array
->type
->column_type();
1744 /* Offset in the constant matrix to the first element of the column
1747 const unsigned mat_idx
= column
* column_type
->vector_elements
;
1749 ir_constant_data data
= { { 0 } };
1751 switch (column_type
->base_type
) {
1752 case GLSL_TYPE_UINT
:
1754 for (unsigned i
= 0; i
< column_type
->vector_elements
; i
++)
1755 data
.u
[i
] = array
->value
.u
[mat_idx
+ i
];
1759 case GLSL_TYPE_FLOAT
:
1760 for (unsigned i
= 0; i
< column_type
->vector_elements
; i
++)
1761 data
.f
[i
] = array
->value
.f
[mat_idx
+ i
];
1766 assert(!"Should not get here.");
1770 return new(ctx
) ir_constant(column_type
, &data
);
1771 } else if (array
->type
->is_vector()) {
1772 const unsigned component
= idx
->value
.u
[0];
1774 return new(ctx
) ir_constant(array
, component
);
1776 const unsigned index
= idx
->value
.u
[0];
1777 return array
->get_array_element(index
)->clone(ctx
, NULL
);
1785 ir_dereference_record::constant_expression_value(struct hash_table
*variable_context
)
1787 ir_constant
*v
= this->record
->constant_expression_value();
1789 return (v
!= NULL
) ? v
->get_record_field(this->field
) : NULL
;
1794 ir_assignment::constant_expression_value(struct hash_table
*variable_context
)
1796 /* FINISHME: Handle CEs involving assignment (return RHS) */
1802 ir_constant::constant_expression_value(struct hash_table
*variable_context
)
1809 ir_call::constant_expression_value(struct hash_table
*variable_context
)
1811 return this->callee
->constant_expression_value(&this->actual_parameters
, variable_context
);
1815 bool ir_function_signature::constant_expression_evaluate_expression_list(const struct exec_list
&body
,
1816 struct hash_table
*variable_context
,
1817 ir_constant
**result
)
1819 foreach_list(n
, &body
) {
1820 ir_instruction
*inst
= (ir_instruction
*)n
;
1821 switch(inst
->ir_type
) {
1823 /* (declare () type symbol) */
1824 case ir_type_variable
: {
1825 ir_variable
*var
= inst
->as_variable();
1826 hash_table_insert(variable_context
, ir_constant::zero(this, var
->type
), var
);
1830 /* (assign [condition] (write-mask) (ref) (value)) */
1831 case ir_type_assignment
: {
1832 ir_assignment
*asg
= inst
->as_assignment();
1833 if (asg
->condition
) {
1834 ir_constant
*cond
= asg
->condition
->constant_expression_value(variable_context
);
1837 if (!cond
->get_bool_component(0))
1841 ir_constant
*store
= NULL
;
1844 if (!constant_referenced(asg
->lhs
, variable_context
, store
, offset
))
1847 ir_constant
*value
= asg
->rhs
->constant_expression_value(variable_context
);
1852 store
->copy_masked_offset(value
, offset
, asg
->write_mask
);
1856 /* (return (expression)) */
1857 case ir_type_return
:
1859 *result
= inst
->as_return()->value
->constant_expression_value(variable_context
);
1860 return *result
!= NULL
;
1862 /* (call name (ref) (params))*/
1863 case ir_type_call
: {
1864 ir_call
*call
= inst
->as_call();
1866 /* Just say no to void functions in constant expressions. We
1867 * don't need them at that point.
1870 if (!call
->return_deref
)
1873 ir_constant
*store
= NULL
;
1876 if (!constant_referenced(call
->return_deref
, variable_context
,
1880 ir_constant
*value
= call
->constant_expression_value(variable_context
);
1885 store
->copy_offset(value
, offset
);
1889 /* (if condition (then-instructions) (else-instructions)) */
1891 ir_if
*iif
= inst
->as_if();
1893 ir_constant
*cond
= iif
->condition
->constant_expression_value(variable_context
);
1894 if (!cond
|| !cond
->type
->is_boolean())
1897 exec_list
&branch
= cond
->get_bool_component(0) ? iif
->then_instructions
: iif
->else_instructions
;
1900 if (!constant_expression_evaluate_expression_list(branch
, variable_context
, result
))
1903 /* If there was a return in the branch chosen, drop out now. */
1910 /* Every other expression type, we drop out. */
1916 /* Reaching the end of the block is not an error condition */
1924 ir_function_signature::constant_expression_value(exec_list
*actual_parameters
, struct hash_table
*variable_context
)
1926 const glsl_type
*type
= this->return_type
;
1927 if (type
== glsl_type::void_type
)
1930 /* From the GLSL 1.20 spec, page 23:
1931 * "Function calls to user-defined functions (non-built-in functions)
1932 * cannot be used to form constant expressions."
1934 if (!this->is_builtin())
1938 * Of the builtin functions, only the texture lookups and the noise
1939 * ones must not be used in constant expressions. They all include
1940 * specific opcodes so they don't need to be special-cased at this
1944 /* Initialize the table of dereferencable names with the function
1945 * parameters. Verify their const-ness on the way.
1947 * We expect the correctness of the number of parameters to have
1948 * been checked earlier.
1950 hash_table
*deref_hash
= hash_table_ctor(8, hash_table_pointer_hash
,
1951 hash_table_pointer_compare
);
1953 /* If "origin" is non-NULL, then the function body is there. So we
1954 * have to use the variable objects from the object with the body,
1955 * but the parameter instanciation on the current object.
1957 const exec_node
*parameter_info
= origin
? origin
->parameters
.head
: parameters
.head
;
1959 foreach_list(n
, actual_parameters
) {
1960 ir_constant
*constant
= ((ir_rvalue
*) n
)->constant_expression_value(variable_context
);
1961 if (constant
== NULL
) {
1962 hash_table_dtor(deref_hash
);
1967 ir_variable
*var
= (ir_variable
*)parameter_info
;
1968 hash_table_insert(deref_hash
, constant
, var
);
1970 parameter_info
= parameter_info
->next
;
1973 ir_constant
*result
= NULL
;
1975 /* Now run the builtin function until something non-constant
1976 * happens or we get the result.
1978 if (constant_expression_evaluate_expression_list(origin
? origin
->body
: body
, deref_hash
, &result
) && result
)
1979 result
= result
->clone(ralloc_parent(this), NULL
);
1981 hash_table_dtor(deref_hash
);