2 * Copyright © 2010 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
25 * \file ir_constant_expression.cpp
26 * Evaluate and process constant valued expressions
28 * In GLSL, constant valued expressions are used in several places. These
29 * must be processed and evaluated very early in the compilation process.
32 * * Initializers for uniforms
33 * * Initializers for \c const variables
37 #include "main/core.h" /* for MAX2, MIN2, CLAMP */
39 #include "glsl_types.h"
40 #include "program/hash_table.h"
42 #if defined(_MSC_VER) && (_MSC_VER < 1800)
43 static int isnormal(double x
)
45 return _fpclass(x
) == _FPCLASS_NN
|| _fpclass(x
) == _FPCLASS_PN
;
47 #elif defined(__SUNPRO_CC)
49 static int isnormal(double x
)
51 return fpclass(x
) == FP_NORMAL
;
56 static double copysign(double x
, double y
)
58 return _copysign(x
, y
);
63 dot(ir_constant
*op0
, ir_constant
*op1
)
65 assert(op0
->type
->is_float() && op1
->type
->is_float());
68 for (unsigned c
= 0; c
< op0
->type
->components(); c
++)
69 result
+= op0
->value
.f
[c
] * op1
->value
.f
[c
];
74 /* This method is the only one supported by gcc. Unions in particular
75 * are iffy, and read-through-converted-pointer is killed by strict
76 * aliasing. OTOH, the compiler sees through the memcpy, so the
77 * resulting asm is reasonable.
80 bitcast_u2f(unsigned int u
)
82 assert(sizeof(float) == sizeof(unsigned int));
84 memcpy(&f
, &u
, sizeof(f
));
91 assert(sizeof(float) == sizeof(unsigned int));
93 memcpy(&u
, &f
, sizeof(f
));
98 * Evaluate one component of a floating-point 4x8 unpacking function.
101 (*pack_1x8_func_t
)(float);
104 * Evaluate one component of a floating-point 2x16 unpacking function.
107 (*pack_1x16_func_t
)(float);
110 * Evaluate one component of a floating-point 4x8 unpacking function.
113 (*unpack_1x8_func_t
)(uint8_t);
116 * Evaluate one component of a floating-point 2x16 unpacking function.
119 (*unpack_1x16_func_t
)(uint16_t);
122 * Evaluate a 2x16 floating-point packing function.
125 pack_2x16(pack_1x16_func_t pack_1x16
,
128 /* From section 8.4 of the GLSL ES 3.00 spec:
132 * The first component of the vector will be written to the least
133 * significant bits of the output; the last component will be written to
134 * the most significant bits.
136 * The specifications for the other packing functions contain similar
140 u
|= ((uint32_t) pack_1x16(x
) << 0);
141 u
|= ((uint32_t) pack_1x16(y
) << 16);
146 * Evaluate a 4x8 floating-point packing function.
149 pack_4x8(pack_1x8_func_t pack_1x8
,
150 float x
, float y
, float z
, float w
)
152 /* From section 8.4 of the GLSL 4.30 spec:
156 * The first component of the vector will be written to the least
157 * significant bits of the output; the last component will be written to
158 * the most significant bits.
160 * The specifications for the other packing functions contain similar
164 u
|= ((uint32_t) pack_1x8(x
) << 0);
165 u
|= ((uint32_t) pack_1x8(y
) << 8);
166 u
|= ((uint32_t) pack_1x8(z
) << 16);
167 u
|= ((uint32_t) pack_1x8(w
) << 24);
172 * Evaluate a 2x16 floating-point unpacking function.
175 unpack_2x16(unpack_1x16_func_t unpack_1x16
,
179 /* From section 8.4 of the GLSL ES 3.00 spec:
183 * The first component of the returned vector will be extracted from
184 * the least significant bits of the input; the last component will be
185 * extracted from the most significant bits.
187 * The specifications for the other unpacking functions contain similar
190 *x
= unpack_1x16((uint16_t) (u
& 0xffff));
191 *y
= unpack_1x16((uint16_t) (u
>> 16));
195 * Evaluate a 4x8 floating-point unpacking function.
198 unpack_4x8(unpack_1x8_func_t unpack_1x8
, uint32_t u
,
199 float *x
, float *y
, float *z
, float *w
)
201 /* From section 8.4 of the GLSL 4.30 spec:
205 * The first component of the returned vector will be extracted from
206 * the least significant bits of the input; the last component will be
207 * extracted from the most significant bits.
209 * The specifications for the other unpacking functions contain similar
212 *x
= unpack_1x8((uint8_t) (u
& 0xff));
213 *y
= unpack_1x8((uint8_t) (u
>> 8));
214 *z
= unpack_1x8((uint8_t) (u
>> 16));
215 *w
= unpack_1x8((uint8_t) (u
>> 24));
219 * Evaluate one component of packSnorm4x8.
222 pack_snorm_1x8(float x
)
224 /* From section 8.4 of the GLSL 4.30 spec:
228 * The conversion for component c of v to fixed point is done as
231 * packSnorm4x8: round(clamp(c, -1, +1) * 127.0)
233 * We must first cast the float to an int, because casting a negative
234 * float to a uint is undefined.
236 return (uint8_t) (int8_t)
237 _mesa_round_to_even(CLAMP(x
, -1.0f
, +1.0f
) * 127.0f
);
241 * Evaluate one component of packSnorm2x16.
244 pack_snorm_1x16(float x
)
246 /* From section 8.4 of the GLSL ES 3.00 spec:
250 * The conversion for component c of v to fixed point is done as
253 * packSnorm2x16: round(clamp(c, -1, +1) * 32767.0)
255 * We must first cast the float to an int, because casting a negative
256 * float to a uint is undefined.
258 return (uint16_t) (int16_t)
259 _mesa_round_to_even(CLAMP(x
, -1.0f
, +1.0f
) * 32767.0f
);
263 * Evaluate one component of unpackSnorm4x8.
266 unpack_snorm_1x8(uint8_t u
)
268 /* From section 8.4 of the GLSL 4.30 spec:
272 * The conversion for unpacked fixed-point value f to floating point is
275 * unpackSnorm4x8: clamp(f / 127.0, -1, +1)
277 return CLAMP((int8_t) u
/ 127.0f
, -1.0f
, +1.0f
);
281 * Evaluate one component of unpackSnorm2x16.
284 unpack_snorm_1x16(uint16_t u
)
286 /* From section 8.4 of the GLSL ES 3.00 spec:
290 * The conversion for unpacked fixed-point value f to floating point is
293 * unpackSnorm2x16: clamp(f / 32767.0, -1, +1)
295 return CLAMP((int16_t) u
/ 32767.0f
, -1.0f
, +1.0f
);
299 * Evaluate one component packUnorm4x8.
302 pack_unorm_1x8(float x
)
304 /* From section 8.4 of the GLSL 4.30 spec:
308 * The conversion for component c of v to fixed point is done as
311 * packUnorm4x8: round(clamp(c, 0, +1) * 255.0)
313 return (uint8_t) _mesa_round_to_even(CLAMP(x
, 0.0f
, 1.0f
) * 255.0f
);
317 * Evaluate one component packUnorm2x16.
320 pack_unorm_1x16(float x
)
322 /* From section 8.4 of the GLSL ES 3.00 spec:
326 * The conversion for component c of v to fixed point is done as
329 * packUnorm2x16: round(clamp(c, 0, +1) * 65535.0)
331 return (uint16_t) _mesa_round_to_even(CLAMP(x
, 0.0f
, 1.0f
) * 65535.0f
);
335 * Evaluate one component of unpackUnorm4x8.
338 unpack_unorm_1x8(uint8_t u
)
340 /* From section 8.4 of the GLSL 4.30 spec:
344 * The conversion for unpacked fixed-point value f to floating point is
347 * unpackUnorm4x8: f / 255.0
349 return (float) u
/ 255.0f
;
353 * Evaluate one component of unpackUnorm2x16.
356 unpack_unorm_1x16(uint16_t u
)
358 /* From section 8.4 of the GLSL ES 3.00 spec:
362 * The conversion for unpacked fixed-point value f to floating point is
365 * unpackUnorm2x16: f / 65535.0
367 return (float) u
/ 65535.0f
;
371 * Evaluate one component of packHalf2x16.
374 pack_half_1x16(float x
)
376 return _mesa_float_to_half(x
);
380 * Evaluate one component of unpackHalf2x16.
383 unpack_half_1x16(uint16_t u
)
385 return _mesa_half_to_float(u
);
389 * Get the constant that is ultimately referenced by an r-value, in a constant
390 * expression evaluation context.
392 * The offset is used when the reference is to a specific column of a matrix.
395 constant_referenced(const ir_dereference
*deref
,
396 struct hash_table
*variable_context
,
397 ir_constant
*&store
, int &offset
)
402 if (variable_context
== NULL
)
405 switch (deref
->ir_type
) {
406 case ir_type_dereference_array
: {
407 const ir_dereference_array
*const da
=
408 (const ir_dereference_array
*) deref
;
410 ir_constant
*const index_c
=
411 da
->array_index
->constant_expression_value(variable_context
);
413 if (!index_c
|| !index_c
->type
->is_scalar() || !index_c
->type
->is_integer())
416 const int index
= index_c
->type
->base_type
== GLSL_TYPE_INT
?
417 index_c
->get_int_component(0) :
418 index_c
->get_uint_component(0);
420 ir_constant
*substore
;
423 const ir_dereference
*const deref
= da
->array
->as_dereference();
427 if (!constant_referenced(deref
, variable_context
, substore
, suboffset
))
430 const glsl_type
*const vt
= da
->array
->type
;
431 if (vt
->is_array()) {
432 store
= substore
->get_array_element(index
);
434 } else if (vt
->is_matrix()) {
436 offset
= index
* vt
->vector_elements
;
437 } else if (vt
->is_vector()) {
439 offset
= suboffset
+ index
;
445 case ir_type_dereference_record
: {
446 const ir_dereference_record
*const dr
=
447 (const ir_dereference_record
*) deref
;
449 const ir_dereference
*const deref
= dr
->record
->as_dereference();
453 ir_constant
*substore
;
456 if (!constant_referenced(deref
, variable_context
, substore
, suboffset
))
459 /* Since we're dropping it on the floor...
461 assert(suboffset
== 0);
463 store
= substore
->get_record_field(dr
->field
);
467 case ir_type_dereference_variable
: {
468 const ir_dereference_variable
*const dv
=
469 (const ir_dereference_variable
*) deref
;
471 store
= (ir_constant
*) hash_table_find(variable_context
, dv
->var
);
476 assert(!"Should not get here.");
480 return store
!= NULL
;
485 ir_rvalue::constant_expression_value(struct hash_table
*)
487 assert(this->type
->is_error());
492 ir_expression::constant_expression_value(struct hash_table
*variable_context
)
494 if (this->type
->is_error())
497 ir_constant
*op
[Elements(this->operands
)] = { NULL
, };
498 ir_constant_data data
;
500 memset(&data
, 0, sizeof(data
));
502 for (unsigned operand
= 0; operand
< this->get_num_operands(); operand
++) {
503 op
[operand
] = this->operands
[operand
]->constant_expression_value(variable_context
);
509 switch (this->operation
) {
510 case ir_binop_lshift
:
511 case ir_binop_rshift
:
513 case ir_binop_interpolate_at_offset
:
514 case ir_binop_interpolate_at_sample
:
515 case ir_binop_vector_extract
:
517 case ir_triop_bitfield_extract
:
521 assert(op
[0]->type
->base_type
== op
[1]->type
->base_type
);
525 bool op0_scalar
= op
[0]->type
->is_scalar();
526 bool op1_scalar
= op
[1] != NULL
&& op
[1]->type
->is_scalar();
528 /* When iterating over a vector or matrix's components, we want to increase
529 * the loop counter. However, for scalars, we want to stay at 0.
531 unsigned c0_inc
= op0_scalar
? 0 : 1;
532 unsigned c1_inc
= op1_scalar
? 0 : 1;
534 if (op1_scalar
|| !op
[1]) {
535 components
= op
[0]->type
->components();
537 components
= op
[1]->type
->components();
540 void *ctx
= ralloc_parent(this);
542 /* Handle array operations here, rather than below. */
543 if (op
[0]->type
->is_array()) {
544 assert(op
[1] != NULL
&& op
[1]->type
->is_array());
545 switch (this->operation
) {
546 case ir_binop_all_equal
:
547 return new(ctx
) ir_constant(op
[0]->has_value(op
[1]));
548 case ir_binop_any_nequal
:
549 return new(ctx
) ir_constant(!op
[0]->has_value(op
[1]));
556 switch (this->operation
) {
557 case ir_unop_bit_not
:
558 switch (op
[0]->type
->base_type
) {
560 for (unsigned c
= 0; c
< components
; c
++)
561 data
.i
[c
] = ~ op
[0]->value
.i
[c
];
564 for (unsigned c
= 0; c
< components
; c
++)
565 data
.u
[c
] = ~ op
[0]->value
.u
[c
];
572 case ir_unop_logic_not
:
573 assert(op
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
574 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++)
575 data
.b
[c
] = !op
[0]->value
.b
[c
];
579 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
580 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
581 data
.i
[c
] = (int) op
[0]->value
.f
[c
];
585 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
586 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
587 data
.i
[c
] = (unsigned) op
[0]->value
.f
[c
];
591 assert(op
[0]->type
->base_type
== GLSL_TYPE_INT
);
592 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
593 data
.f
[c
] = (float) op
[0]->value
.i
[c
];
597 assert(op
[0]->type
->base_type
== GLSL_TYPE_UINT
);
598 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
599 data
.f
[c
] = (float) op
[0]->value
.u
[c
];
603 assert(op
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
604 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
605 data
.f
[c
] = op
[0]->value
.b
[c
] ? 1.0F
: 0.0F
;
609 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
610 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
611 data
.b
[c
] = op
[0]->value
.f
[c
] != 0.0F
? true : false;
615 assert(op
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
616 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
617 data
.u
[c
] = op
[0]->value
.b
[c
] ? 1 : 0;
621 assert(op
[0]->type
->is_integer());
622 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
623 data
.b
[c
] = op
[0]->value
.u
[c
] ? true : false;
627 assert(op
[0]->type
->base_type
== GLSL_TYPE_UINT
);
628 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
629 data
.i
[c
] = op
[0]->value
.u
[c
];
633 assert(op
[0]->type
->base_type
== GLSL_TYPE_INT
);
634 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
635 data
.u
[c
] = op
[0]->value
.i
[c
];
638 case ir_unop_bitcast_i2f
:
639 assert(op
[0]->type
->base_type
== GLSL_TYPE_INT
);
640 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
641 data
.f
[c
] = bitcast_u2f(op
[0]->value
.i
[c
]);
644 case ir_unop_bitcast_f2i
:
645 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
646 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
647 data
.i
[c
] = bitcast_f2u(op
[0]->value
.f
[c
]);
650 case ir_unop_bitcast_u2f
:
651 assert(op
[0]->type
->base_type
== GLSL_TYPE_UINT
);
652 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
653 data
.f
[c
] = bitcast_u2f(op
[0]->value
.u
[c
]);
656 case ir_unop_bitcast_f2u
:
657 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
658 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
659 data
.u
[c
] = bitcast_f2u(op
[0]->value
.f
[c
]);
663 assert(op
[0]->type
->is_boolean());
665 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
666 if (op
[0]->value
.b
[c
])
672 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
673 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
674 data
.f
[c
] = truncf(op
[0]->value
.f
[c
]);
678 case ir_unop_round_even
:
679 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
680 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
681 data
.f
[c
] = _mesa_round_to_even(op
[0]->value
.f
[c
]);
686 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
687 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
688 data
.f
[c
] = ceilf(op
[0]->value
.f
[c
]);
693 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
694 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
695 data
.f
[c
] = floorf(op
[0]->value
.f
[c
]);
700 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
701 switch (this->type
->base_type
) {
708 case GLSL_TYPE_FLOAT
:
709 data
.f
[c
] = op
[0]->value
.f
[c
] - floor(op
[0]->value
.f
[c
]);
718 case ir_unop_sin_reduced
:
719 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
720 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
721 data
.f
[c
] = sinf(op
[0]->value
.f
[c
]);
726 case ir_unop_cos_reduced
:
727 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
728 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
729 data
.f
[c
] = cosf(op
[0]->value
.f
[c
]);
734 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
735 switch (this->type
->base_type
) {
737 data
.u
[c
] = -((int) op
[0]->value
.u
[c
]);
740 data
.i
[c
] = -op
[0]->value
.i
[c
];
742 case GLSL_TYPE_FLOAT
:
743 data
.f
[c
] = -op
[0]->value
.f
[c
];
752 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
753 switch (this->type
->base_type
) {
755 data
.u
[c
] = op
[0]->value
.u
[c
];
758 data
.i
[c
] = op
[0]->value
.i
[c
];
760 data
.i
[c
] = -data
.i
[c
];
762 case GLSL_TYPE_FLOAT
:
763 data
.f
[c
] = fabs(op
[0]->value
.f
[c
]);
772 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
773 switch (this->type
->base_type
) {
775 data
.u
[c
] = op
[0]->value
.i
[c
] > 0;
778 data
.i
[c
] = (op
[0]->value
.i
[c
] > 0) - (op
[0]->value
.i
[c
] < 0);
780 case GLSL_TYPE_FLOAT
:
781 data
.f
[c
] = float((op
[0]->value
.f
[c
] > 0)-(op
[0]->value
.f
[c
] < 0));
790 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
791 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
792 switch (this->type
->base_type
) {
794 if (op
[0]->value
.u
[c
] != 0.0)
795 data
.u
[c
] = 1 / op
[0]->value
.u
[c
];
798 if (op
[0]->value
.i
[c
] != 0.0)
799 data
.i
[c
] = 1 / op
[0]->value
.i
[c
];
801 case GLSL_TYPE_FLOAT
:
802 if (op
[0]->value
.f
[c
] != 0.0)
803 data
.f
[c
] = 1.0F
/ op
[0]->value
.f
[c
];
812 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
813 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
814 data
.f
[c
] = 1.0F
/ sqrtf(op
[0]->value
.f
[c
]);
819 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
820 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
821 data
.f
[c
] = sqrtf(op
[0]->value
.f
[c
]);
826 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
827 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
828 data
.f
[c
] = expf(op
[0]->value
.f
[c
]);
833 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
834 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
835 data
.f
[c
] = exp2f(op
[0]->value
.f
[c
]);
840 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
841 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
842 data
.f
[c
] = logf(op
[0]->value
.f
[c
]);
847 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
848 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
849 data
.f
[c
] = log2f(op
[0]->value
.f
[c
]);
854 case ir_unop_dFdx_coarse
:
855 case ir_unop_dFdx_fine
:
857 case ir_unop_dFdy_coarse
:
858 case ir_unop_dFdy_fine
:
859 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
860 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
865 case ir_unop_pack_snorm_2x16
:
866 assert(op
[0]->type
== glsl_type::vec2_type
);
867 data
.u
[0] = pack_2x16(pack_snorm_1x16
,
871 case ir_unop_pack_snorm_4x8
:
872 assert(op
[0]->type
== glsl_type::vec4_type
);
873 data
.u
[0] = pack_4x8(pack_snorm_1x8
,
879 case ir_unop_unpack_snorm_2x16
:
880 assert(op
[0]->type
== glsl_type::uint_type
);
881 unpack_2x16(unpack_snorm_1x16
,
883 &data
.f
[0], &data
.f
[1]);
885 case ir_unop_unpack_snorm_4x8
:
886 assert(op
[0]->type
== glsl_type::uint_type
);
887 unpack_4x8(unpack_snorm_1x8
,
889 &data
.f
[0], &data
.f
[1], &data
.f
[2], &data
.f
[3]);
891 case ir_unop_pack_unorm_2x16
:
892 assert(op
[0]->type
== glsl_type::vec2_type
);
893 data
.u
[0] = pack_2x16(pack_unorm_1x16
,
897 case ir_unop_pack_unorm_4x8
:
898 assert(op
[0]->type
== glsl_type::vec4_type
);
899 data
.u
[0] = pack_4x8(pack_unorm_1x8
,
905 case ir_unop_unpack_unorm_2x16
:
906 assert(op
[0]->type
== glsl_type::uint_type
);
907 unpack_2x16(unpack_unorm_1x16
,
909 &data
.f
[0], &data
.f
[1]);
911 case ir_unop_unpack_unorm_4x8
:
912 assert(op
[0]->type
== glsl_type::uint_type
);
913 unpack_4x8(unpack_unorm_1x8
,
915 &data
.f
[0], &data
.f
[1], &data
.f
[2], &data
.f
[3]);
917 case ir_unop_pack_half_2x16
:
918 assert(op
[0]->type
== glsl_type::vec2_type
);
919 data
.u
[0] = pack_2x16(pack_half_1x16
,
923 case ir_unop_unpack_half_2x16
:
924 assert(op
[0]->type
== glsl_type::uint_type
);
925 unpack_2x16(unpack_half_1x16
,
927 &data
.f
[0], &data
.f
[1]);
930 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
931 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
932 data
.f
[c
] = powf(op
[0]->value
.f
[c
], op
[1]->value
.f
[c
]);
937 data
.f
[0] = dot(op
[0], op
[1]);
941 assert(op
[0]->type
== op
[1]->type
|| op0_scalar
|| op1_scalar
);
942 for (unsigned c
= 0, c0
= 0, c1
= 0;
944 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
946 switch (op
[0]->type
->base_type
) {
948 data
.u
[c
] = MIN2(op
[0]->value
.u
[c0
], op
[1]->value
.u
[c1
]);
951 data
.i
[c
] = MIN2(op
[0]->value
.i
[c0
], op
[1]->value
.i
[c1
]);
953 case GLSL_TYPE_FLOAT
:
954 data
.f
[c
] = MIN2(op
[0]->value
.f
[c0
], op
[1]->value
.f
[c1
]);
963 assert(op
[0]->type
== op
[1]->type
|| op0_scalar
|| op1_scalar
);
964 for (unsigned c
= 0, c0
= 0, c1
= 0;
966 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
968 switch (op
[0]->type
->base_type
) {
970 data
.u
[c
] = MAX2(op
[0]->value
.u
[c0
], op
[1]->value
.u
[c1
]);
973 data
.i
[c
] = MAX2(op
[0]->value
.i
[c0
], op
[1]->value
.i
[c1
]);
975 case GLSL_TYPE_FLOAT
:
976 data
.f
[c
] = MAX2(op
[0]->value
.f
[c0
], op
[1]->value
.f
[c1
]);
985 assert(op
[0]->type
== op
[1]->type
|| op0_scalar
|| op1_scalar
);
986 for (unsigned c
= 0, c0
= 0, c1
= 0;
988 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
990 switch (op
[0]->type
->base_type
) {
992 data
.u
[c
] = op
[0]->value
.u
[c0
] + op
[1]->value
.u
[c1
];
995 data
.i
[c
] = op
[0]->value
.i
[c0
] + op
[1]->value
.i
[c1
];
997 case GLSL_TYPE_FLOAT
:
998 data
.f
[c
] = op
[0]->value
.f
[c0
] + op
[1]->value
.f
[c1
];
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
] = op
[0]->value
.u
[c0
] - op
[1]->value
.u
[c1
];
1017 data
.i
[c
] = op
[0]->value
.i
[c0
] - op
[1]->value
.i
[c1
];
1019 case GLSL_TYPE_FLOAT
:
1020 data
.f
[c
] = op
[0]->value
.f
[c0
] - op
[1]->value
.f
[c1
];
1029 /* Check for equal types, or unequal types involving scalars */
1030 if ((op
[0]->type
== op
[1]->type
&& !op
[0]->type
->is_matrix())
1031 || op0_scalar
|| op1_scalar
) {
1032 for (unsigned c
= 0, c0
= 0, c1
= 0;
1034 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
1036 switch (op
[0]->type
->base_type
) {
1037 case GLSL_TYPE_UINT
:
1038 data
.u
[c
] = op
[0]->value
.u
[c0
] * op
[1]->value
.u
[c1
];
1041 data
.i
[c
] = op
[0]->value
.i
[c0
] * op
[1]->value
.i
[c1
];
1043 case GLSL_TYPE_FLOAT
:
1044 data
.f
[c
] = op
[0]->value
.f
[c0
] * op
[1]->value
.f
[c1
];
1051 assert(op
[0]->type
->is_matrix() || op
[1]->type
->is_matrix());
1053 /* Multiply an N-by-M matrix with an M-by-P matrix. Since either
1054 * matrix can be a GLSL vector, either N or P can be 1.
1056 * For vec*mat, the vector is treated as a row vector. This
1057 * means the vector is a 1-row x M-column matrix.
1059 * For mat*vec, the vector is treated as a column vector. Since
1060 * matrix_columns is 1 for vectors, this just works.
1062 const unsigned n
= op
[0]->type
->is_vector()
1063 ? 1 : op
[0]->type
->vector_elements
;
1064 const unsigned m
= op
[1]->type
->vector_elements
;
1065 const unsigned p
= op
[1]->type
->matrix_columns
;
1066 for (unsigned j
= 0; j
< p
; j
++) {
1067 for (unsigned i
= 0; i
< n
; i
++) {
1068 for (unsigned k
= 0; k
< m
; k
++) {
1069 data
.f
[i
+n
*j
] += op
[0]->value
.f
[i
+n
*k
]*op
[1]->value
.f
[k
+m
*j
];
1077 /* FINISHME: Emit warning when division-by-zero is detected. */
1078 assert(op
[0]->type
== op
[1]->type
|| op0_scalar
|| op1_scalar
);
1079 for (unsigned c
= 0, c0
= 0, c1
= 0;
1081 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
1083 switch (op
[0]->type
->base_type
) {
1084 case GLSL_TYPE_UINT
:
1085 if (op
[1]->value
.u
[c1
] == 0) {
1088 data
.u
[c
] = op
[0]->value
.u
[c0
] / op
[1]->value
.u
[c1
];
1092 if (op
[1]->value
.i
[c1
] == 0) {
1095 data
.i
[c
] = op
[0]->value
.i
[c0
] / op
[1]->value
.i
[c1
];
1098 case GLSL_TYPE_FLOAT
:
1099 data
.f
[c
] = op
[0]->value
.f
[c0
] / op
[1]->value
.f
[c1
];
1108 /* FINISHME: Emit warning when division-by-zero is detected. */
1109 assert(op
[0]->type
== op
[1]->type
|| 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 if (op
[1]->value
.u
[c1
] == 0) {
1119 data
.u
[c
] = op
[0]->value
.u
[c0
] % op
[1]->value
.u
[c1
];
1123 if (op
[1]->value
.i
[c1
] == 0) {
1126 data
.i
[c
] = op
[0]->value
.i
[c0
] % op
[1]->value
.i
[c1
];
1129 case GLSL_TYPE_FLOAT
:
1130 /* We don't use fmod because it rounds toward zero; GLSL specifies
1133 data
.f
[c
] = op
[0]->value
.f
[c0
] - op
[1]->value
.f
[c1
]
1134 * floorf(op
[0]->value
.f
[c0
] / op
[1]->value
.f
[c1
]);
1143 case ir_binop_logic_and
:
1144 assert(op
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
1145 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++)
1146 data
.b
[c
] = op
[0]->value
.b
[c
] && op
[1]->value
.b
[c
];
1148 case ir_binop_logic_xor
:
1149 assert(op
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
1150 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++)
1151 data
.b
[c
] = op
[0]->value
.b
[c
] ^ op
[1]->value
.b
[c
];
1153 case ir_binop_logic_or
:
1154 assert(op
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
1155 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++)
1156 data
.b
[c
] = op
[0]->value
.b
[c
] || op
[1]->value
.b
[c
];
1160 assert(op
[0]->type
== op
[1]->type
);
1161 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
1162 switch (op
[0]->type
->base_type
) {
1163 case GLSL_TYPE_UINT
:
1164 data
.b
[c
] = op
[0]->value
.u
[c
] < op
[1]->value
.u
[c
];
1167 data
.b
[c
] = op
[0]->value
.i
[c
] < op
[1]->value
.i
[c
];
1169 case GLSL_TYPE_FLOAT
:
1170 data
.b
[c
] = op
[0]->value
.f
[c
] < op
[1]->value
.f
[c
];
1177 case ir_binop_greater
:
1178 assert(op
[0]->type
== op
[1]->type
);
1179 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
1180 switch (op
[0]->type
->base_type
) {
1181 case GLSL_TYPE_UINT
:
1182 data
.b
[c
] = op
[0]->value
.u
[c
] > op
[1]->value
.u
[c
];
1185 data
.b
[c
] = op
[0]->value
.i
[c
] > op
[1]->value
.i
[c
];
1187 case GLSL_TYPE_FLOAT
:
1188 data
.b
[c
] = op
[0]->value
.f
[c
] > op
[1]->value
.f
[c
];
1195 case ir_binop_lequal
:
1196 assert(op
[0]->type
== op
[1]->type
);
1197 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
1198 switch (op
[0]->type
->base_type
) {
1199 case GLSL_TYPE_UINT
:
1200 data
.b
[c
] = op
[0]->value
.u
[c
] <= op
[1]->value
.u
[c
];
1203 data
.b
[c
] = op
[0]->value
.i
[c
] <= op
[1]->value
.i
[c
];
1205 case GLSL_TYPE_FLOAT
:
1206 data
.b
[c
] = op
[0]->value
.f
[c
] <= op
[1]->value
.f
[c
];
1213 case ir_binop_gequal
:
1214 assert(op
[0]->type
== op
[1]->type
);
1215 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
1216 switch (op
[0]->type
->base_type
) {
1217 case GLSL_TYPE_UINT
:
1218 data
.b
[c
] = op
[0]->value
.u
[c
] >= op
[1]->value
.u
[c
];
1221 data
.b
[c
] = op
[0]->value
.i
[c
] >= op
[1]->value
.i
[c
];
1223 case GLSL_TYPE_FLOAT
:
1224 data
.b
[c
] = op
[0]->value
.f
[c
] >= op
[1]->value
.f
[c
];
1231 case ir_binop_equal
:
1232 assert(op
[0]->type
== op
[1]->type
);
1233 for (unsigned c
= 0; c
< components
; c
++) {
1234 switch (op
[0]->type
->base_type
) {
1235 case GLSL_TYPE_UINT
:
1236 data
.b
[c
] = op
[0]->value
.u
[c
] == op
[1]->value
.u
[c
];
1239 data
.b
[c
] = op
[0]->value
.i
[c
] == op
[1]->value
.i
[c
];
1241 case GLSL_TYPE_FLOAT
:
1242 data
.b
[c
] = op
[0]->value
.f
[c
] == op
[1]->value
.f
[c
];
1244 case GLSL_TYPE_BOOL
:
1245 data
.b
[c
] = op
[0]->value
.b
[c
] == op
[1]->value
.b
[c
];
1252 case ir_binop_nequal
:
1253 assert(op
[0]->type
== op
[1]->type
);
1254 for (unsigned c
= 0; c
< components
; c
++) {
1255 switch (op
[0]->type
->base_type
) {
1256 case GLSL_TYPE_UINT
:
1257 data
.b
[c
] = op
[0]->value
.u
[c
] != op
[1]->value
.u
[c
];
1260 data
.b
[c
] = op
[0]->value
.i
[c
] != op
[1]->value
.i
[c
];
1262 case GLSL_TYPE_FLOAT
:
1263 data
.b
[c
] = op
[0]->value
.f
[c
] != op
[1]->value
.f
[c
];
1265 case GLSL_TYPE_BOOL
:
1266 data
.b
[c
] = op
[0]->value
.b
[c
] != op
[1]->value
.b
[c
];
1273 case ir_binop_all_equal
:
1274 data
.b
[0] = op
[0]->has_value(op
[1]);
1276 case ir_binop_any_nequal
:
1277 data
.b
[0] = !op
[0]->has_value(op
[1]);
1280 case ir_binop_lshift
:
1281 for (unsigned c
= 0, c0
= 0, c1
= 0;
1283 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
1285 if (op
[0]->type
->base_type
== GLSL_TYPE_INT
&&
1286 op
[1]->type
->base_type
== GLSL_TYPE_INT
) {
1287 data
.i
[c
] = op
[0]->value
.i
[c0
] << op
[1]->value
.i
[c1
];
1289 } else if (op
[0]->type
->base_type
== GLSL_TYPE_INT
&&
1290 op
[1]->type
->base_type
== GLSL_TYPE_UINT
) {
1291 data
.i
[c
] = op
[0]->value
.i
[c0
] << op
[1]->value
.u
[c1
];
1293 } else if (op
[0]->type
->base_type
== GLSL_TYPE_UINT
&&
1294 op
[1]->type
->base_type
== GLSL_TYPE_INT
) {
1295 data
.u
[c
] = op
[0]->value
.u
[c0
] << op
[1]->value
.i
[c1
];
1297 } else if (op
[0]->type
->base_type
== GLSL_TYPE_UINT
&&
1298 op
[1]->type
->base_type
== GLSL_TYPE_UINT
) {
1299 data
.u
[c
] = op
[0]->value
.u
[c0
] << op
[1]->value
.u
[c1
];
1304 case ir_binop_rshift
:
1305 for (unsigned c
= 0, c0
= 0, c1
= 0;
1307 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
1309 if (op
[0]->type
->base_type
== GLSL_TYPE_INT
&&
1310 op
[1]->type
->base_type
== GLSL_TYPE_INT
) {
1311 data
.i
[c
] = op
[0]->value
.i
[c0
] >> op
[1]->value
.i
[c1
];
1313 } else if (op
[0]->type
->base_type
== GLSL_TYPE_INT
&&
1314 op
[1]->type
->base_type
== GLSL_TYPE_UINT
) {
1315 data
.i
[c
] = op
[0]->value
.i
[c0
] >> op
[1]->value
.u
[c1
];
1317 } else if (op
[0]->type
->base_type
== GLSL_TYPE_UINT
&&
1318 op
[1]->type
->base_type
== GLSL_TYPE_INT
) {
1319 data
.u
[c
] = op
[0]->value
.u
[c0
] >> op
[1]->value
.i
[c1
];
1321 } else if (op
[0]->type
->base_type
== GLSL_TYPE_UINT
&&
1322 op
[1]->type
->base_type
== GLSL_TYPE_UINT
) {
1323 data
.u
[c
] = op
[0]->value
.u
[c0
] >> op
[1]->value
.u
[c1
];
1328 case ir_binop_bit_and
:
1329 for (unsigned c
= 0, c0
= 0, c1
= 0;
1331 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
1333 switch (op
[0]->type
->base_type
) {
1335 data
.i
[c
] = op
[0]->value
.i
[c0
] & op
[1]->value
.i
[c1
];
1337 case GLSL_TYPE_UINT
:
1338 data
.u
[c
] = op
[0]->value
.u
[c0
] & op
[1]->value
.u
[c1
];
1346 case ir_binop_bit_or
:
1347 for (unsigned c
= 0, c0
= 0, c1
= 0;
1349 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
1351 switch (op
[0]->type
->base_type
) {
1353 data
.i
[c
] = op
[0]->value
.i
[c0
] | op
[1]->value
.i
[c1
];
1355 case GLSL_TYPE_UINT
:
1356 data
.u
[c
] = op
[0]->value
.u
[c0
] | op
[1]->value
.u
[c1
];
1364 case ir_binop_vector_extract
: {
1365 const int c
= CLAMP(op
[1]->value
.i
[0], 0,
1366 (int) op
[0]->type
->vector_elements
- 1);
1368 switch (op
[0]->type
->base_type
) {
1369 case GLSL_TYPE_UINT
:
1370 data
.u
[0] = op
[0]->value
.u
[c
];
1373 data
.i
[0] = op
[0]->value
.i
[c
];
1375 case GLSL_TYPE_FLOAT
:
1376 data
.f
[0] = op
[0]->value
.f
[c
];
1378 case GLSL_TYPE_BOOL
:
1379 data
.b
[0] = op
[0]->value
.b
[c
];
1387 case ir_binop_bit_xor
:
1388 for (unsigned c
= 0, c0
= 0, c1
= 0;
1390 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
1392 switch (op
[0]->type
->base_type
) {
1394 data
.i
[c
] = op
[0]->value
.i
[c0
] ^ op
[1]->value
.i
[c1
];
1396 case GLSL_TYPE_UINT
:
1397 data
.u
[c
] = op
[0]->value
.u
[c0
] ^ op
[1]->value
.u
[c1
];
1405 case ir_unop_bitfield_reverse
:
1406 /* http://graphics.stanford.edu/~seander/bithacks.html#BitReverseObvious */
1407 for (unsigned c
= 0; c
< components
; c
++) {
1408 unsigned int v
= op
[0]->value
.u
[c
]; // input bits to be reversed
1409 unsigned int r
= v
; // r will be reversed bits of v; first get LSB of v
1410 int s
= sizeof(v
) * CHAR_BIT
- 1; // extra shift needed at end
1412 for (v
>>= 1; v
; v
>>= 1) {
1417 r
<<= s
; // shift when v's highest bits are zero
1423 case ir_unop_bit_count
:
1424 for (unsigned c
= 0; c
< components
; c
++) {
1426 unsigned v
= op
[0]->value
.u
[c
];
1428 for (; v
; count
++) {
1435 case ir_unop_find_msb
:
1436 for (unsigned c
= 0; c
< components
; c
++) {
1437 int v
= op
[0]->value
.i
[c
];
1439 if (v
== 0 || (op
[0]->type
->base_type
== GLSL_TYPE_INT
&& v
== -1))
1443 int top_bit
= op
[0]->type
->base_type
== GLSL_TYPE_UINT
1444 ? 0 : v
& (1 << 31);
1446 while (((v
& (1 << 31)) == top_bit
) && count
!= 32) {
1451 data
.i
[c
] = 31 - count
;
1456 case ir_unop_find_lsb
:
1457 for (unsigned c
= 0; c
< components
; c
++) {
1458 if (op
[0]->value
.i
[c
] == 0)
1462 unsigned v
= op
[0]->value
.u
[c
];
1464 for (; !(v
& 1); v
>>= 1) {
1472 case ir_unop_saturate
:
1473 for (unsigned c
= 0; c
< components
; c
++) {
1474 data
.f
[c
] = CLAMP(op
[0]->value
.f
[c
], 0.0f
, 1.0f
);
1478 case ir_triop_bitfield_extract
: {
1479 int offset
= op
[1]->value
.i
[0];
1480 int bits
= op
[2]->value
.i
[0];
1482 for (unsigned c
= 0; c
< components
; c
++) {
1485 else if (offset
< 0 || bits
< 0)
1486 data
.u
[c
] = 0; /* Undefined, per spec. */
1487 else if (offset
+ bits
> 32)
1488 data
.u
[c
] = 0; /* Undefined, per spec. */
1490 if (op
[0]->type
->base_type
== GLSL_TYPE_INT
) {
1491 /* int so that the right shift will sign-extend. */
1492 int value
= op
[0]->value
.i
[c
];
1493 value
<<= 32 - bits
- offset
;
1494 value
>>= 32 - bits
;
1497 unsigned value
= op
[0]->value
.u
[c
];
1498 value
<<= 32 - bits
- offset
;
1499 value
>>= 32 - bits
;
1507 case ir_binop_bfm
: {
1508 int bits
= op
[0]->value
.i
[0];
1509 int offset
= op
[1]->value
.i
[0];
1511 for (unsigned c
= 0; c
< components
; c
++) {
1513 data
.u
[c
] = op
[0]->value
.u
[c
];
1514 else if (offset
< 0 || bits
< 0)
1515 data
.u
[c
] = 0; /* Undefined for bitfieldInsert, per spec. */
1516 else if (offset
+ bits
> 32)
1517 data
.u
[c
] = 0; /* Undefined for bitfieldInsert, per spec. */
1519 data
.u
[c
] = ((1 << bits
) - 1) << offset
;
1524 case ir_binop_ldexp
:
1525 for (unsigned c
= 0; c
< components
; c
++) {
1526 data
.f
[c
] = ldexp(op
[0]->value
.f
[c
], op
[1]->value
.i
[c
]);
1527 /* Flush subnormal values to zero. */
1528 if (!isnormal(data
.f
[c
]))
1529 data
.f
[c
] = copysign(0.0f
, op
[0]->value
.f
[c
]);
1534 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
1535 assert(op
[1]->type
->base_type
== GLSL_TYPE_FLOAT
);
1536 assert(op
[2]->type
->base_type
== GLSL_TYPE_FLOAT
);
1538 for (unsigned c
= 0; c
< components
; c
++) {
1539 data
.f
[c
] = op
[0]->value
.f
[c
] * op
[1]->value
.f
[c
]
1540 + op
[2]->value
.f
[c
];
1544 case ir_triop_lrp
: {
1545 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
1546 assert(op
[1]->type
->base_type
== GLSL_TYPE_FLOAT
);
1547 assert(op
[2]->type
->base_type
== GLSL_TYPE_FLOAT
);
1549 unsigned c2_inc
= op
[2]->type
->is_scalar() ? 0 : 1;
1550 for (unsigned c
= 0, c2
= 0; c
< components
; c2
+= c2_inc
, c
++) {
1551 data
.f
[c
] = op
[0]->value
.f
[c
] * (1.0f
- op
[2]->value
.f
[c2
]) +
1552 (op
[1]->value
.f
[c
] * op
[2]->value
.f
[c2
]);
1558 for (unsigned c
= 0; c
< components
; c
++) {
1559 data
.u
[c
] = op
[0]->value
.b
[c
] ? op
[1]->value
.u
[c
]
1560 : op
[2]->value
.u
[c
];
1564 case ir_triop_vector_insert
: {
1565 const unsigned idx
= op
[2]->value
.u
[0];
1567 memcpy(&data
, &op
[0]->value
, sizeof(data
));
1569 switch (this->type
->base_type
) {
1571 data
.i
[idx
] = op
[1]->value
.i
[0];
1573 case GLSL_TYPE_UINT
:
1574 data
.u
[idx
] = op
[1]->value
.u
[0];
1576 case GLSL_TYPE_FLOAT
:
1577 data
.f
[idx
] = op
[1]->value
.f
[0];
1579 case GLSL_TYPE_BOOL
:
1580 data
.b
[idx
] = op
[1]->value
.b
[0];
1583 assert(!"Should not get here.");
1589 case ir_quadop_bitfield_insert
: {
1590 int offset
= op
[2]->value
.i
[0];
1591 int bits
= op
[3]->value
.i
[0];
1593 for (unsigned c
= 0; c
< components
; c
++) {
1595 data
.u
[c
] = op
[0]->value
.u
[c
];
1596 else if (offset
< 0 || bits
< 0)
1597 data
.u
[c
] = 0; /* Undefined, per spec. */
1598 else if (offset
+ bits
> 32)
1599 data
.u
[c
] = 0; /* Undefined, per spec. */
1601 unsigned insert_mask
= ((1 << bits
) - 1) << offset
;
1603 unsigned insert
= op
[1]->value
.u
[c
];
1605 insert
&= insert_mask
;
1607 unsigned base
= op
[0]->value
.u
[c
];
1608 base
&= ~insert_mask
;
1610 data
.u
[c
] = base
| insert
;
1616 case ir_quadop_vector
:
1617 for (unsigned c
= 0; c
< this->type
->vector_elements
; c
++) {
1618 switch (this->type
->base_type
) {
1620 data
.i
[c
] = op
[c
]->value
.i
[0];
1622 case GLSL_TYPE_UINT
:
1623 data
.u
[c
] = op
[c
]->value
.u
[0];
1625 case GLSL_TYPE_FLOAT
:
1626 data
.f
[c
] = op
[c
]->value
.f
[0];
1635 /* FINISHME: Should handle all expression types. */
1639 return new(ctx
) ir_constant(this->type
, &data
);
1644 ir_texture::constant_expression_value(struct hash_table
*)
1646 /* texture lookups aren't constant expressions */
1652 ir_swizzle::constant_expression_value(struct hash_table
*variable_context
)
1654 ir_constant
*v
= this->val
->constant_expression_value(variable_context
);
1657 ir_constant_data data
= { { 0 } };
1659 const unsigned swiz_idx
[4] = {
1660 this->mask
.x
, this->mask
.y
, this->mask
.z
, this->mask
.w
1663 for (unsigned i
= 0; i
< this->mask
.num_components
; i
++) {
1664 switch (v
->type
->base_type
) {
1665 case GLSL_TYPE_UINT
:
1666 case GLSL_TYPE_INT
: data
.u
[i
] = v
->value
.u
[swiz_idx
[i
]]; break;
1667 case GLSL_TYPE_FLOAT
: data
.f
[i
] = v
->value
.f
[swiz_idx
[i
]]; break;
1668 case GLSL_TYPE_BOOL
: data
.b
[i
] = v
->value
.b
[swiz_idx
[i
]]; break;
1669 default: assert(!"Should not get here."); break;
1673 void *ctx
= ralloc_parent(this);
1674 return new(ctx
) ir_constant(this->type
, &data
);
1681 ir_dereference_variable::constant_expression_value(struct hash_table
*variable_context
)
1683 /* This may occur during compile and var->type is glsl_type::error_type */
1687 /* Give priority to the context hashtable, if it exists */
1688 if (variable_context
) {
1689 ir_constant
*value
= (ir_constant
*)hash_table_find(variable_context
, var
);
1694 /* The constant_value of a uniform variable is its initializer,
1695 * not the lifetime constant value of the uniform.
1697 if (var
->data
.mode
== ir_var_uniform
)
1700 if (!var
->constant_value
)
1703 return var
->constant_value
->clone(ralloc_parent(var
), NULL
);
1708 ir_dereference_array::constant_expression_value(struct hash_table
*variable_context
)
1710 ir_constant
*array
= this->array
->constant_expression_value(variable_context
);
1711 ir_constant
*idx
= this->array_index
->constant_expression_value(variable_context
);
1713 if ((array
!= NULL
) && (idx
!= NULL
)) {
1714 void *ctx
= ralloc_parent(this);
1715 if (array
->type
->is_matrix()) {
1716 /* Array access of a matrix results in a vector.
1718 const unsigned column
= idx
->value
.u
[0];
1720 const glsl_type
*const column_type
= array
->type
->column_type();
1722 /* Offset in the constant matrix to the first element of the column
1725 const unsigned mat_idx
= column
* column_type
->vector_elements
;
1727 ir_constant_data data
= { { 0 } };
1729 switch (column_type
->base_type
) {
1730 case GLSL_TYPE_UINT
:
1732 for (unsigned i
= 0; i
< column_type
->vector_elements
; i
++)
1733 data
.u
[i
] = array
->value
.u
[mat_idx
+ i
];
1737 case GLSL_TYPE_FLOAT
:
1738 for (unsigned i
= 0; i
< column_type
->vector_elements
; i
++)
1739 data
.f
[i
] = array
->value
.f
[mat_idx
+ i
];
1744 assert(!"Should not get here.");
1748 return new(ctx
) ir_constant(column_type
, &data
);
1749 } else if (array
->type
->is_vector()) {
1750 const unsigned component
= idx
->value
.u
[0];
1752 return new(ctx
) ir_constant(array
, component
);
1754 const unsigned index
= idx
->value
.u
[0];
1755 return array
->get_array_element(index
)->clone(ctx
, NULL
);
1763 ir_dereference_record::constant_expression_value(struct hash_table
*)
1765 ir_constant
*v
= this->record
->constant_expression_value();
1767 return (v
!= NULL
) ? v
->get_record_field(this->field
) : NULL
;
1772 ir_assignment::constant_expression_value(struct hash_table
*)
1774 /* FINISHME: Handle CEs involving assignment (return RHS) */
1780 ir_constant::constant_expression_value(struct hash_table
*)
1787 ir_call::constant_expression_value(struct hash_table
*variable_context
)
1789 return this->callee
->constant_expression_value(&this->actual_parameters
, variable_context
);
1793 bool ir_function_signature::constant_expression_evaluate_expression_list(const struct exec_list
&body
,
1794 struct hash_table
*variable_context
,
1795 ir_constant
**result
)
1797 foreach_in_list(ir_instruction
, inst
, &body
) {
1798 switch(inst
->ir_type
) {
1800 /* (declare () type symbol) */
1801 case ir_type_variable
: {
1802 ir_variable
*var
= inst
->as_variable();
1803 hash_table_insert(variable_context
, ir_constant::zero(this, var
->type
), var
);
1807 /* (assign [condition] (write-mask) (ref) (value)) */
1808 case ir_type_assignment
: {
1809 ir_assignment
*asg
= inst
->as_assignment();
1810 if (asg
->condition
) {
1811 ir_constant
*cond
= asg
->condition
->constant_expression_value(variable_context
);
1814 if (!cond
->get_bool_component(0))
1818 ir_constant
*store
= NULL
;
1821 if (!constant_referenced(asg
->lhs
, variable_context
, store
, offset
))
1824 ir_constant
*value
= asg
->rhs
->constant_expression_value(variable_context
);
1829 store
->copy_masked_offset(value
, offset
, asg
->write_mask
);
1833 /* (return (expression)) */
1834 case ir_type_return
:
1836 *result
= inst
->as_return()->value
->constant_expression_value(variable_context
);
1837 return *result
!= NULL
;
1839 /* (call name (ref) (params))*/
1840 case ir_type_call
: {
1841 ir_call
*call
= inst
->as_call();
1843 /* Just say no to void functions in constant expressions. We
1844 * don't need them at that point.
1847 if (!call
->return_deref
)
1850 ir_constant
*store
= NULL
;
1853 if (!constant_referenced(call
->return_deref
, variable_context
,
1857 ir_constant
*value
= call
->constant_expression_value(variable_context
);
1862 store
->copy_offset(value
, offset
);
1866 /* (if condition (then-instructions) (else-instructions)) */
1868 ir_if
*iif
= inst
->as_if();
1870 ir_constant
*cond
= iif
->condition
->constant_expression_value(variable_context
);
1871 if (!cond
|| !cond
->type
->is_boolean())
1874 exec_list
&branch
= cond
->get_bool_component(0) ? iif
->then_instructions
: iif
->else_instructions
;
1877 if (!constant_expression_evaluate_expression_list(branch
, variable_context
, result
))
1880 /* If there was a return in the branch chosen, drop out now. */
1887 /* Every other expression type, we drop out. */
1893 /* Reaching the end of the block is not an error condition */
1901 ir_function_signature::constant_expression_value(exec_list
*actual_parameters
, struct hash_table
*variable_context
)
1903 const glsl_type
*type
= this->return_type
;
1904 if (type
== glsl_type::void_type
)
1907 /* From the GLSL 1.20 spec, page 23:
1908 * "Function calls to user-defined functions (non-built-in functions)
1909 * cannot be used to form constant expressions."
1911 if (!this->is_builtin())
1915 * Of the builtin functions, only the texture lookups and the noise
1916 * ones must not be used in constant expressions. They all include
1917 * specific opcodes so they don't need to be special-cased at this
1921 /* Initialize the table of dereferencable names with the function
1922 * parameters. Verify their const-ness on the way.
1924 * We expect the correctness of the number of parameters to have
1925 * been checked earlier.
1927 hash_table
*deref_hash
= hash_table_ctor(8, hash_table_pointer_hash
,
1928 hash_table_pointer_compare
);
1930 /* If "origin" is non-NULL, then the function body is there. So we
1931 * have to use the variable objects from the object with the body,
1932 * but the parameter instanciation on the current object.
1934 const exec_node
*parameter_info
= origin
? origin
->parameters
.head
: parameters
.head
;
1936 foreach_in_list(ir_rvalue
, n
, actual_parameters
) {
1937 ir_constant
*constant
= n
->constant_expression_value(variable_context
);
1938 if (constant
== NULL
) {
1939 hash_table_dtor(deref_hash
);
1944 ir_variable
*var
= (ir_variable
*)parameter_info
;
1945 hash_table_insert(deref_hash
, constant
, var
);
1947 parameter_info
= parameter_info
->next
;
1950 ir_constant
*result
= NULL
;
1952 /* Now run the builtin function until something non-constant
1953 * happens or we get the result.
1955 if (constant_expression_evaluate_expression_list(origin
? origin
->body
: body
, deref_hash
, &result
) && result
)
1956 result
= result
->clone(ralloc_parent(this), NULL
);
1958 hash_table_dtor(deref_hash
);