glsl: Extract ir_quadop_bitfield_insert implementation to a separate function
[mesa.git] / src / compiler / glsl / ir_constant_expression.cpp
1 /*
2 * Copyright © 2010 Intel Corporation
3 *
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:
10 *
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
13 * Software.
14 *
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.
22 */
23
24 /**
25 * \file ir_constant_expression.cpp
26 * Evaluate and process constant valued expressions
27 *
28 * In GLSL, constant valued expressions are used in several places. These
29 * must be processed and evaluated very early in the compilation process.
30 *
31 * * Sizes of arrays
32 * * Initializers for uniforms
33 * * Initializers for \c const variables
34 */
35
36 #include <math.h>
37 #include "main/core.h" /* for MAX2, MIN2, CLAMP */
38 #include "util/rounding.h" /* for _mesa_roundeven */
39 #include "util/half_float.h"
40 #include "ir.h"
41 #include "compiler/glsl_types.h"
42 #include "program/hash_table.h"
43
44 static float
45 dot_f(ir_constant *op0, ir_constant *op1)
46 {
47 assert(op0->type->is_float() && op1->type->is_float());
48
49 float result = 0;
50 for (unsigned c = 0; c < op0->type->components(); c++)
51 result += op0->value.f[c] * op1->value.f[c];
52
53 return result;
54 }
55
56 static double
57 dot_d(ir_constant *op0, ir_constant *op1)
58 {
59 assert(op0->type->is_double() && op1->type->is_double());
60
61 double result = 0;
62 for (unsigned c = 0; c < op0->type->components(); c++)
63 result += op0->value.d[c] * op1->value.d[c];
64
65 return result;
66 }
67
68 /* This method is the only one supported by gcc. Unions in particular
69 * are iffy, and read-through-converted-pointer is killed by strict
70 * aliasing. OTOH, the compiler sees through the memcpy, so the
71 * resulting asm is reasonable.
72 */
73 static float
74 bitcast_u2f(unsigned int u)
75 {
76 assert(sizeof(float) == sizeof(unsigned int));
77 float f;
78 memcpy(&f, &u, sizeof(f));
79 return f;
80 }
81
82 static unsigned int
83 bitcast_f2u(float f)
84 {
85 assert(sizeof(float) == sizeof(unsigned int));
86 unsigned int u;
87 memcpy(&u, &f, sizeof(f));
88 return u;
89 }
90
91 /**
92 * Evaluate one component of a floating-point 4x8 unpacking function.
93 */
94 typedef uint8_t
95 (*pack_1x8_func_t)(float);
96
97 /**
98 * Evaluate one component of a floating-point 2x16 unpacking function.
99 */
100 typedef uint16_t
101 (*pack_1x16_func_t)(float);
102
103 /**
104 * Evaluate one component of a floating-point 4x8 unpacking function.
105 */
106 typedef float
107 (*unpack_1x8_func_t)(uint8_t);
108
109 /**
110 * Evaluate one component of a floating-point 2x16 unpacking function.
111 */
112 typedef float
113 (*unpack_1x16_func_t)(uint16_t);
114
115 /**
116 * Evaluate a 2x16 floating-point packing function.
117 */
118 static uint32_t
119 pack_2x16(pack_1x16_func_t pack_1x16,
120 float x, float y)
121 {
122 /* From section 8.4 of the GLSL ES 3.00 spec:
123 *
124 * packSnorm2x16
125 * -------------
126 * The first component of the vector will be written to the least
127 * significant bits of the output; the last component will be written to
128 * the most significant bits.
129 *
130 * The specifications for the other packing functions contain similar
131 * language.
132 */
133 uint32_t u = 0;
134 u |= ((uint32_t) pack_1x16(x) << 0);
135 u |= ((uint32_t) pack_1x16(y) << 16);
136 return u;
137 }
138
139 /**
140 * Evaluate a 4x8 floating-point packing function.
141 */
142 static uint32_t
143 pack_4x8(pack_1x8_func_t pack_1x8,
144 float x, float y, float z, float w)
145 {
146 /* From section 8.4 of the GLSL 4.30 spec:
147 *
148 * packSnorm4x8
149 * ------------
150 * The first component of the vector will be written to the least
151 * significant bits of the output; the last component will be written to
152 * the most significant bits.
153 *
154 * The specifications for the other packing functions contain similar
155 * language.
156 */
157 uint32_t u = 0;
158 u |= ((uint32_t) pack_1x8(x) << 0);
159 u |= ((uint32_t) pack_1x8(y) << 8);
160 u |= ((uint32_t) pack_1x8(z) << 16);
161 u |= ((uint32_t) pack_1x8(w) << 24);
162 return u;
163 }
164
165 /**
166 * Evaluate a 2x16 floating-point unpacking function.
167 */
168 static void
169 unpack_2x16(unpack_1x16_func_t unpack_1x16,
170 uint32_t u,
171 float *x, float *y)
172 {
173 /* From section 8.4 of the GLSL ES 3.00 spec:
174 *
175 * unpackSnorm2x16
176 * ---------------
177 * The first component of the returned vector will be extracted from
178 * the least significant bits of the input; the last component will be
179 * extracted from the most significant bits.
180 *
181 * The specifications for the other unpacking functions contain similar
182 * language.
183 */
184 *x = unpack_1x16((uint16_t) (u & 0xffff));
185 *y = unpack_1x16((uint16_t) (u >> 16));
186 }
187
188 /**
189 * Evaluate a 4x8 floating-point unpacking function.
190 */
191 static void
192 unpack_4x8(unpack_1x8_func_t unpack_1x8, uint32_t u,
193 float *x, float *y, float *z, float *w)
194 {
195 /* From section 8.4 of the GLSL 4.30 spec:
196 *
197 * unpackSnorm4x8
198 * --------------
199 * The first component of the returned vector will be extracted from
200 * the least significant bits of the input; the last component will be
201 * extracted from the most significant bits.
202 *
203 * The specifications for the other unpacking functions contain similar
204 * language.
205 */
206 *x = unpack_1x8((uint8_t) (u & 0xff));
207 *y = unpack_1x8((uint8_t) (u >> 8));
208 *z = unpack_1x8((uint8_t) (u >> 16));
209 *w = unpack_1x8((uint8_t) (u >> 24));
210 }
211
212 /**
213 * Evaluate one component of packSnorm4x8.
214 */
215 static uint8_t
216 pack_snorm_1x8(float x)
217 {
218 /* From section 8.4 of the GLSL 4.30 spec:
219 *
220 * packSnorm4x8
221 * ------------
222 * The conversion for component c of v to fixed point is done as
223 * follows:
224 *
225 * packSnorm4x8: round(clamp(c, -1, +1) * 127.0)
226 */
227 return (uint8_t)
228 _mesa_lroundevenf(CLAMP(x, -1.0f, +1.0f) * 127.0f);
229 }
230
231 /**
232 * Evaluate one component of packSnorm2x16.
233 */
234 static uint16_t
235 pack_snorm_1x16(float x)
236 {
237 /* From section 8.4 of the GLSL ES 3.00 spec:
238 *
239 * packSnorm2x16
240 * -------------
241 * The conversion for component c of v to fixed point is done as
242 * follows:
243 *
244 * packSnorm2x16: round(clamp(c, -1, +1) * 32767.0)
245 */
246 return (uint16_t)
247 _mesa_lroundevenf(CLAMP(x, -1.0f, +1.0f) * 32767.0f);
248 }
249
250 /**
251 * Evaluate one component of unpackSnorm4x8.
252 */
253 static float
254 unpack_snorm_1x8(uint8_t u)
255 {
256 /* From section 8.4 of the GLSL 4.30 spec:
257 *
258 * unpackSnorm4x8
259 * --------------
260 * The conversion for unpacked fixed-point value f to floating point is
261 * done as follows:
262 *
263 * unpackSnorm4x8: clamp(f / 127.0, -1, +1)
264 */
265 return CLAMP((int8_t) u / 127.0f, -1.0f, +1.0f);
266 }
267
268 /**
269 * Evaluate one component of unpackSnorm2x16.
270 */
271 static float
272 unpack_snorm_1x16(uint16_t u)
273 {
274 /* From section 8.4 of the GLSL ES 3.00 spec:
275 *
276 * unpackSnorm2x16
277 * ---------------
278 * The conversion for unpacked fixed-point value f to floating point is
279 * done as follows:
280 *
281 * unpackSnorm2x16: clamp(f / 32767.0, -1, +1)
282 */
283 return CLAMP((int16_t) u / 32767.0f, -1.0f, +1.0f);
284 }
285
286 /**
287 * Evaluate one component packUnorm4x8.
288 */
289 static uint8_t
290 pack_unorm_1x8(float x)
291 {
292 /* From section 8.4 of the GLSL 4.30 spec:
293 *
294 * packUnorm4x8
295 * ------------
296 * The conversion for component c of v to fixed point is done as
297 * follows:
298 *
299 * packUnorm4x8: round(clamp(c, 0, +1) * 255.0)
300 */
301 return (uint8_t) (int) _mesa_roundevenf(CLAMP(x, 0.0f, 1.0f) * 255.0f);
302 }
303
304 /**
305 * Evaluate one component packUnorm2x16.
306 */
307 static uint16_t
308 pack_unorm_1x16(float x)
309 {
310 /* From section 8.4 of the GLSL ES 3.00 spec:
311 *
312 * packUnorm2x16
313 * -------------
314 * The conversion for component c of v to fixed point is done as
315 * follows:
316 *
317 * packUnorm2x16: round(clamp(c, 0, +1) * 65535.0)
318 */
319 return (uint16_t) (int)
320 _mesa_roundevenf(CLAMP(x, 0.0f, 1.0f) * 65535.0f);
321 }
322
323 /**
324 * Evaluate one component of unpackUnorm4x8.
325 */
326 static float
327 unpack_unorm_1x8(uint8_t u)
328 {
329 /* From section 8.4 of the GLSL 4.30 spec:
330 *
331 * unpackUnorm4x8
332 * --------------
333 * The conversion for unpacked fixed-point value f to floating point is
334 * done as follows:
335 *
336 * unpackUnorm4x8: f / 255.0
337 */
338 return (float) u / 255.0f;
339 }
340
341 /**
342 * Evaluate one component of unpackUnorm2x16.
343 */
344 static float
345 unpack_unorm_1x16(uint16_t u)
346 {
347 /* From section 8.4 of the GLSL ES 3.00 spec:
348 *
349 * unpackUnorm2x16
350 * ---------------
351 * The conversion for unpacked fixed-point value f to floating point is
352 * done as follows:
353 *
354 * unpackUnorm2x16: f / 65535.0
355 */
356 return (float) u / 65535.0f;
357 }
358
359 /**
360 * Evaluate one component of packHalf2x16.
361 */
362 static uint16_t
363 pack_half_1x16(float x)
364 {
365 return _mesa_float_to_half(x);
366 }
367
368 /**
369 * Evaluate one component of unpackHalf2x16.
370 */
371 static float
372 unpack_half_1x16(uint16_t u)
373 {
374 return _mesa_half_to_float(u);
375 }
376
377 /**
378 * Get the constant that is ultimately referenced by an r-value, in a constant
379 * expression evaluation context.
380 *
381 * The offset is used when the reference is to a specific column of a matrix.
382 */
383 static bool
384 constant_referenced(const ir_dereference *deref,
385 struct hash_table *variable_context,
386 ir_constant *&store, int &offset)
387 {
388 store = NULL;
389 offset = 0;
390
391 if (variable_context == NULL)
392 return false;
393
394 switch (deref->ir_type) {
395 case ir_type_dereference_array: {
396 const ir_dereference_array *const da =
397 (const ir_dereference_array *) deref;
398
399 ir_constant *const index_c =
400 da->array_index->constant_expression_value(variable_context);
401
402 if (!index_c || !index_c->type->is_scalar() || !index_c->type->is_integer())
403 break;
404
405 const int index = index_c->type->base_type == GLSL_TYPE_INT ?
406 index_c->get_int_component(0) :
407 index_c->get_uint_component(0);
408
409 ir_constant *substore;
410 int suboffset;
411
412 const ir_dereference *const deref = da->array->as_dereference();
413 if (!deref)
414 break;
415
416 if (!constant_referenced(deref, variable_context, substore, suboffset))
417 break;
418
419 const glsl_type *const vt = da->array->type;
420 if (vt->is_array()) {
421 store = substore->get_array_element(index);
422 offset = 0;
423 } else if (vt->is_matrix()) {
424 store = substore;
425 offset = index * vt->vector_elements;
426 } else if (vt->is_vector()) {
427 store = substore;
428 offset = suboffset + index;
429 }
430
431 break;
432 }
433
434 case ir_type_dereference_record: {
435 const ir_dereference_record *const dr =
436 (const ir_dereference_record *) deref;
437
438 const ir_dereference *const deref = dr->record->as_dereference();
439 if (!deref)
440 break;
441
442 ir_constant *substore;
443 int suboffset;
444
445 if (!constant_referenced(deref, variable_context, substore, suboffset))
446 break;
447
448 /* Since we're dropping it on the floor...
449 */
450 assert(suboffset == 0);
451
452 store = substore->get_record_field(dr->field);
453 break;
454 }
455
456 case ir_type_dereference_variable: {
457 const ir_dereference_variable *const dv =
458 (const ir_dereference_variable *) deref;
459
460 store = (ir_constant *) hash_table_find(variable_context, dv->var);
461 break;
462 }
463
464 default:
465 assert(!"Should not get here.");
466 break;
467 }
468
469 return store != NULL;
470 }
471
472
473 ir_constant *
474 ir_rvalue::constant_expression_value(struct hash_table *)
475 {
476 assert(this->type->is_error());
477 return NULL;
478 }
479
480 static uint32_t
481 bitfield_reverse(uint32_t v)
482 {
483 /* http://graphics.stanford.edu/~seander/bithacks.html#BitReverseObvious */
484 uint32_t r = v; // r will be reversed bits of v; first get LSB of v
485 int s = sizeof(v) * CHAR_BIT - 1; // extra shift needed at end
486
487 for (v >>= 1; v; v >>= 1) {
488 r <<= 1;
489 r |= v & 1;
490 s--;
491 }
492 r <<= s; // shift when v's highest bits are zero
493
494 return r;
495 }
496
497 static int
498 find_msb_uint(uint32_t v)
499 {
500 int count = 0;
501
502 /* If v == 0, then the loop will terminate when count == 32. In that case
503 * 31-count will produce the -1 result required by GLSL findMSB().
504 */
505 while (((v & (1u << 31)) == 0) && count != 32) {
506 count++;
507 v <<= 1;
508 }
509
510 return 31 - count;
511 }
512
513 static int
514 find_msb_int(int32_t v)
515 {
516 /* If v is signed, findMSB() returns the position of the most significant
517 * zero bit.
518 */
519 return find_msb_uint(v < 0 ? ~v : v);
520 }
521
522 static float
523 ldexpf_flush_subnormal(float x, int exp)
524 {
525 const float result = ldexpf(x, exp);
526
527 /* Flush subnormal values to zero. */
528 return !isnormal(result) ? copysignf(0.0f, x) : result;
529 }
530
531 static double
532 ldexp_flush_subnormal(double x, int exp)
533 {
534 const double result = ldexp(x, exp);
535
536 /* Flush subnormal values to zero. */
537 return !isnormal(result) ? copysign(0.0, x) : result;
538 }
539
540 static uint32_t
541 bitfield_extract_uint(uint32_t value, int offset, int bits)
542 {
543 if (bits == 0)
544 return 0;
545 else if (offset < 0 || bits < 0)
546 return 0; /* Undefined, per spec. */
547 else if (offset + bits > 32)
548 return 0; /* Undefined, per spec. */
549 else {
550 value <<= 32 - bits - offset;
551 value >>= 32 - bits;
552 return value;
553 }
554 }
555
556 static int32_t
557 bitfield_extract_int(int32_t value, int offset, int bits)
558 {
559 if (bits == 0)
560 return 0;
561 else if (offset < 0 || bits < 0)
562 return 0; /* Undefined, per spec. */
563 else if (offset + bits > 32)
564 return 0; /* Undefined, per spec. */
565 else {
566 value <<= 32 - bits - offset;
567 value >>= 32 - bits;
568 return value;
569 }
570 }
571
572 static uint32_t
573 bitfield_insert(uint32_t base, uint32_t insert, int offset, int bits)
574 {
575 if (bits == 0)
576 return base;
577 else if (offset < 0 || bits < 0)
578 return 0; /* Undefined, per spec. */
579 else if (offset + bits > 32)
580 return 0; /* Undefined, per spec. */
581 else {
582 unsigned insert_mask = ((1ull << bits) - 1) << offset;
583
584 insert <<= offset;
585 insert &= insert_mask;
586 base &= ~insert_mask;
587
588 return base | insert;
589 }
590 }
591
592 ir_constant *
593 ir_expression::constant_expression_value(struct hash_table *variable_context)
594 {
595 if (this->type->is_error())
596 return NULL;
597
598 ir_constant *op[ARRAY_SIZE(this->operands)] = { NULL, };
599 ir_constant_data data;
600
601 memset(&data, 0, sizeof(data));
602
603 for (unsigned operand = 0; operand < this->get_num_operands(); operand++) {
604 op[operand] = this->operands[operand]->constant_expression_value(variable_context);
605 if (!op[operand])
606 return NULL;
607 }
608
609 if (op[1] != NULL)
610 switch (this->operation) {
611 case ir_binop_lshift:
612 case ir_binop_rshift:
613 case ir_binop_ldexp:
614 case ir_binop_interpolate_at_offset:
615 case ir_binop_interpolate_at_sample:
616 case ir_binop_vector_extract:
617 case ir_triop_csel:
618 case ir_triop_bitfield_extract:
619 break;
620
621 default:
622 assert(op[0]->type->base_type == op[1]->type->base_type);
623 break;
624 }
625
626 bool op0_scalar = op[0]->type->is_scalar();
627 bool op1_scalar = op[1] != NULL && op[1]->type->is_scalar();
628
629 /* When iterating over a vector or matrix's components, we want to increase
630 * the loop counter. However, for scalars, we want to stay at 0.
631 */
632 unsigned c0_inc = op0_scalar ? 0 : 1;
633 unsigned c1_inc = op1_scalar ? 0 : 1;
634 unsigned components;
635 if (op1_scalar || !op[1]) {
636 components = op[0]->type->components();
637 } else {
638 components = op[1]->type->components();
639 }
640
641 void *ctx = ralloc_parent(this);
642
643 /* Handle array operations here, rather than below. */
644 if (op[0]->type->is_array()) {
645 assert(op[1] != NULL && op[1]->type->is_array());
646 switch (this->operation) {
647 case ir_binop_all_equal:
648 return new(ctx) ir_constant(op[0]->has_value(op[1]));
649 case ir_binop_any_nequal:
650 return new(ctx) ir_constant(!op[0]->has_value(op[1]));
651 default:
652 break;
653 }
654 return NULL;
655 }
656
657 switch (this->operation) {
658 case ir_unop_bit_not:
659 switch (op[0]->type->base_type) {
660 case GLSL_TYPE_INT:
661 for (unsigned c = 0; c < components; c++)
662 data.i[c] = ~ op[0]->value.i[c];
663 break;
664 case GLSL_TYPE_UINT:
665 for (unsigned c = 0; c < components; c++)
666 data.u[c] = ~ op[0]->value.u[c];
667 break;
668 default:
669 assert(0);
670 }
671 break;
672
673 case ir_unop_logic_not:
674 assert(op[0]->type->base_type == GLSL_TYPE_BOOL);
675 for (unsigned c = 0; c < op[0]->type->components(); c++)
676 data.b[c] = !op[0]->value.b[c];
677 break;
678
679 case ir_unop_f2i:
680 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
681 for (unsigned c = 0; c < op[0]->type->components(); c++) {
682 data.i[c] = (int) op[0]->value.f[c];
683 }
684 break;
685 case ir_unop_f2u:
686 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
687 for (unsigned c = 0; c < op[0]->type->components(); c++) {
688 data.u[c] = (unsigned) op[0]->value.f[c];
689 }
690 break;
691 case ir_unop_i2f:
692 assert(op[0]->type->base_type == GLSL_TYPE_INT);
693 for (unsigned c = 0; c < op[0]->type->components(); c++) {
694 data.f[c] = (float) op[0]->value.i[c];
695 }
696 break;
697 case ir_unop_u2f:
698 assert(op[0]->type->base_type == GLSL_TYPE_UINT);
699 for (unsigned c = 0; c < op[0]->type->components(); c++) {
700 data.f[c] = (float) op[0]->value.u[c];
701 }
702 break;
703 case ir_unop_b2f:
704 assert(op[0]->type->base_type == GLSL_TYPE_BOOL);
705 for (unsigned c = 0; c < op[0]->type->components(); c++) {
706 data.f[c] = op[0]->value.b[c] ? 1.0F : 0.0F;
707 }
708 break;
709 case ir_unop_f2b:
710 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
711 for (unsigned c = 0; c < op[0]->type->components(); c++) {
712 data.b[c] = op[0]->value.f[c] != 0.0F ? true : false;
713 }
714 break;
715 case ir_unop_b2i:
716 assert(op[0]->type->base_type == GLSL_TYPE_BOOL);
717 for (unsigned c = 0; c < op[0]->type->components(); c++) {
718 data.i[c] = op[0]->value.b[c] ? 1 : 0;
719 }
720 break;
721 case ir_unop_i2b:
722 assert(op[0]->type->is_integer());
723 for (unsigned c = 0; c < op[0]->type->components(); c++) {
724 data.b[c] = op[0]->value.u[c] ? true : false;
725 }
726 break;
727 case ir_unop_u2i:
728 assert(op[0]->type->base_type == GLSL_TYPE_UINT);
729 for (unsigned c = 0; c < op[0]->type->components(); c++) {
730 data.i[c] = op[0]->value.u[c];
731 }
732 break;
733 case ir_unop_i2u:
734 assert(op[0]->type->base_type == GLSL_TYPE_INT);
735 for (unsigned c = 0; c < op[0]->type->components(); c++) {
736 data.u[c] = op[0]->value.i[c];
737 }
738 break;
739 case ir_unop_bitcast_i2f:
740 assert(op[0]->type->base_type == GLSL_TYPE_INT);
741 for (unsigned c = 0; c < op[0]->type->components(); c++) {
742 data.f[c] = bitcast_u2f(op[0]->value.i[c]);
743 }
744 break;
745 case ir_unop_bitcast_f2i:
746 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
747 for (unsigned c = 0; c < op[0]->type->components(); c++) {
748 data.i[c] = bitcast_f2u(op[0]->value.f[c]);
749 }
750 break;
751 case ir_unop_bitcast_u2f:
752 assert(op[0]->type->base_type == GLSL_TYPE_UINT);
753 for (unsigned c = 0; c < op[0]->type->components(); c++) {
754 data.f[c] = bitcast_u2f(op[0]->value.u[c]);
755 }
756 break;
757 case ir_unop_bitcast_f2u:
758 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
759 for (unsigned c = 0; c < op[0]->type->components(); c++) {
760 data.u[c] = bitcast_f2u(op[0]->value.f[c]);
761 }
762 break;
763 case ir_unop_d2f:
764 assert(op[0]->type->base_type == GLSL_TYPE_DOUBLE);
765 for (unsigned c = 0; c < op[0]->type->components(); c++) {
766 data.f[c] = op[0]->value.d[c];
767 }
768 break;
769 case ir_unop_f2d:
770 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
771 for (unsigned c = 0; c < op[0]->type->components(); c++) {
772 data.d[c] = op[0]->value.f[c];
773 }
774 break;
775 case ir_unop_d2i:
776 assert(op[0]->type->base_type == GLSL_TYPE_DOUBLE);
777 for (unsigned c = 0; c < op[0]->type->components(); c++) {
778 data.i[c] = op[0]->value.d[c];
779 }
780 break;
781 case ir_unop_i2d:
782 assert(op[0]->type->base_type == GLSL_TYPE_INT);
783 for (unsigned c = 0; c < op[0]->type->components(); c++) {
784 data.d[c] = op[0]->value.i[c];
785 }
786 break;
787 case ir_unop_d2u:
788 assert(op[0]->type->base_type == GLSL_TYPE_DOUBLE);
789 for (unsigned c = 0; c < op[0]->type->components(); c++) {
790 data.u[c] = op[0]->value.d[c];
791 }
792 break;
793 case ir_unop_u2d:
794 assert(op[0]->type->base_type == GLSL_TYPE_UINT);
795 for (unsigned c = 0; c < op[0]->type->components(); c++) {
796 data.d[c] = op[0]->value.u[c];
797 }
798 break;
799 case ir_unop_d2b:
800 assert(op[0]->type->base_type == GLSL_TYPE_DOUBLE);
801 for (unsigned c = 0; c < op[0]->type->components(); c++) {
802 data.b[c] = op[0]->value.d[c] != 0.0;
803 }
804 break;
805 case ir_unop_trunc:
806 for (unsigned c = 0; c < op[0]->type->components(); c++) {
807 if (op[0]->type->base_type == GLSL_TYPE_DOUBLE)
808 data.d[c] = trunc(op[0]->value.d[c]);
809 else
810 data.f[c] = truncf(op[0]->value.f[c]);
811 }
812 break;
813
814 case ir_unop_round_even:
815 for (unsigned c = 0; c < op[0]->type->components(); c++) {
816 if (op[0]->type->base_type == GLSL_TYPE_DOUBLE)
817 data.d[c] = _mesa_roundeven(op[0]->value.d[c]);
818 else
819 data.f[c] = _mesa_roundevenf(op[0]->value.f[c]);
820 }
821 break;
822
823 case ir_unop_ceil:
824 for (unsigned c = 0; c < op[0]->type->components(); c++) {
825 if (op[0]->type->base_type == GLSL_TYPE_DOUBLE)
826 data.d[c] = ceil(op[0]->value.d[c]);
827 else
828 data.f[c] = ceilf(op[0]->value.f[c]);
829 }
830 break;
831
832 case ir_unop_floor:
833 for (unsigned c = 0; c < op[0]->type->components(); c++) {
834 if (op[0]->type->base_type == GLSL_TYPE_DOUBLE)
835 data.d[c] = floor(op[0]->value.d[c]);
836 else
837 data.f[c] = floorf(op[0]->value.f[c]);
838 }
839 break;
840
841 case ir_unop_fract:
842 for (unsigned c = 0; c < op[0]->type->components(); c++) {
843 switch (this->type->base_type) {
844 case GLSL_TYPE_FLOAT:
845 data.f[c] = op[0]->value.f[c] - floor(op[0]->value.f[c]);
846 break;
847 case GLSL_TYPE_DOUBLE:
848 data.d[c] = op[0]->value.d[c] - floor(op[0]->value.d[c]);
849 break;
850 default:
851 assert(0);
852 }
853 }
854 break;
855
856 case ir_unop_sin:
857 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
858 for (unsigned c = 0; c < op[0]->type->components(); c++) {
859 data.f[c] = sinf(op[0]->value.f[c]);
860 }
861 break;
862
863 case ir_unop_cos:
864 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
865 for (unsigned c = 0; c < op[0]->type->components(); c++) {
866 data.f[c] = cosf(op[0]->value.f[c]);
867 }
868 break;
869
870 case ir_unop_neg:
871 for (unsigned c = 0; c < op[0]->type->components(); c++) {
872 switch (this->type->base_type) {
873 case GLSL_TYPE_UINT:
874 data.u[c] = -((int) op[0]->value.u[c]);
875 break;
876 case GLSL_TYPE_INT:
877 data.i[c] = -op[0]->value.i[c];
878 break;
879 case GLSL_TYPE_FLOAT:
880 data.f[c] = -op[0]->value.f[c];
881 break;
882 case GLSL_TYPE_DOUBLE:
883 data.d[c] = -op[0]->value.d[c];
884 break;
885 default:
886 assert(0);
887 }
888 }
889 break;
890
891 case ir_unop_abs:
892 for (unsigned c = 0; c < op[0]->type->components(); c++) {
893 switch (this->type->base_type) {
894 case GLSL_TYPE_INT:
895 data.i[c] = op[0]->value.i[c];
896 if (data.i[c] < 0)
897 data.i[c] = -data.i[c];
898 break;
899 case GLSL_TYPE_FLOAT:
900 data.f[c] = fabs(op[0]->value.f[c]);
901 break;
902 case GLSL_TYPE_DOUBLE:
903 data.d[c] = fabs(op[0]->value.d[c]);
904 break;
905 default:
906 assert(0);
907 }
908 }
909 break;
910
911 case ir_unop_sign:
912 for (unsigned c = 0; c < op[0]->type->components(); c++) {
913 switch (this->type->base_type) {
914 case GLSL_TYPE_INT:
915 data.i[c] = (op[0]->value.i[c] > 0) - (op[0]->value.i[c] < 0);
916 break;
917 case GLSL_TYPE_FLOAT:
918 data.f[c] = float((op[0]->value.f[c] > 0)-(op[0]->value.f[c] < 0));
919 break;
920 case GLSL_TYPE_DOUBLE:
921 data.d[c] = double((op[0]->value.d[c] > 0)-(op[0]->value.d[c] < 0));
922 break;
923 default:
924 assert(0);
925 }
926 }
927 break;
928
929 case ir_unop_rcp:
930 for (unsigned c = 0; c < op[0]->type->components(); c++) {
931 switch (this->type->base_type) {
932 case GLSL_TYPE_FLOAT:
933 if (op[0]->value.f[c] != 0.0)
934 data.f[c] = 1.0F / op[0]->value.f[c];
935 break;
936 case GLSL_TYPE_DOUBLE:
937 if (op[0]->value.d[c] != 0.0)
938 data.d[c] = 1.0 / op[0]->value.d[c];
939 break;
940 default:
941 assert(0);
942 }
943 }
944 break;
945
946 case ir_unop_rsq:
947 for (unsigned c = 0; c < op[0]->type->components(); c++) {
948 if (op[0]->type->base_type == GLSL_TYPE_DOUBLE)
949 data.d[c] = 1.0 / sqrt(op[0]->value.d[c]);
950 else
951 data.f[c] = 1.0F / sqrtf(op[0]->value.f[c]);
952 }
953 break;
954
955 case ir_unop_sqrt:
956 for (unsigned c = 0; c < op[0]->type->components(); c++) {
957 if (op[0]->type->base_type == GLSL_TYPE_DOUBLE)
958 data.d[c] = sqrt(op[0]->value.d[c]);
959 else
960 data.f[c] = sqrtf(op[0]->value.f[c]);
961 }
962 break;
963
964 case ir_unop_exp:
965 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
966 for (unsigned c = 0; c < op[0]->type->components(); c++) {
967 data.f[c] = expf(op[0]->value.f[c]);
968 }
969 break;
970
971 case ir_unop_exp2:
972 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
973 for (unsigned c = 0; c < op[0]->type->components(); c++) {
974 data.f[c] = exp2f(op[0]->value.f[c]);
975 }
976 break;
977
978 case ir_unop_log:
979 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
980 for (unsigned c = 0; c < op[0]->type->components(); c++) {
981 data.f[c] = logf(op[0]->value.f[c]);
982 }
983 break;
984
985 case ir_unop_log2:
986 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
987 for (unsigned c = 0; c < op[0]->type->components(); c++) {
988 data.f[c] = log2f(op[0]->value.f[c]);
989 }
990 break;
991
992 case ir_unop_dFdx:
993 case ir_unop_dFdx_coarse:
994 case ir_unop_dFdx_fine:
995 case ir_unop_dFdy:
996 case ir_unop_dFdy_coarse:
997 case ir_unop_dFdy_fine:
998 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
999 for (unsigned c = 0; c < op[0]->type->components(); c++) {
1000 data.f[c] = 0.0;
1001 }
1002 break;
1003
1004 case ir_unop_pack_snorm_2x16:
1005 assert(op[0]->type == glsl_type::vec2_type);
1006 data.u[0] = pack_2x16(pack_snorm_1x16,
1007 op[0]->value.f[0],
1008 op[0]->value.f[1]);
1009 break;
1010 case ir_unop_pack_snorm_4x8:
1011 assert(op[0]->type == glsl_type::vec4_type);
1012 data.u[0] = pack_4x8(pack_snorm_1x8,
1013 op[0]->value.f[0],
1014 op[0]->value.f[1],
1015 op[0]->value.f[2],
1016 op[0]->value.f[3]);
1017 break;
1018 case ir_unop_unpack_snorm_2x16:
1019 assert(op[0]->type == glsl_type::uint_type);
1020 unpack_2x16(unpack_snorm_1x16,
1021 op[0]->value.u[0],
1022 &data.f[0], &data.f[1]);
1023 break;
1024 case ir_unop_unpack_snorm_4x8:
1025 assert(op[0]->type == glsl_type::uint_type);
1026 unpack_4x8(unpack_snorm_1x8,
1027 op[0]->value.u[0],
1028 &data.f[0], &data.f[1], &data.f[2], &data.f[3]);
1029 break;
1030 case ir_unop_pack_unorm_2x16:
1031 assert(op[0]->type == glsl_type::vec2_type);
1032 data.u[0] = pack_2x16(pack_unorm_1x16,
1033 op[0]->value.f[0],
1034 op[0]->value.f[1]);
1035 break;
1036 case ir_unop_pack_unorm_4x8:
1037 assert(op[0]->type == glsl_type::vec4_type);
1038 data.u[0] = pack_4x8(pack_unorm_1x8,
1039 op[0]->value.f[0],
1040 op[0]->value.f[1],
1041 op[0]->value.f[2],
1042 op[0]->value.f[3]);
1043 break;
1044 case ir_unop_unpack_unorm_2x16:
1045 assert(op[0]->type == glsl_type::uint_type);
1046 unpack_2x16(unpack_unorm_1x16,
1047 op[0]->value.u[0],
1048 &data.f[0], &data.f[1]);
1049 break;
1050 case ir_unop_unpack_unorm_4x8:
1051 assert(op[0]->type == glsl_type::uint_type);
1052 unpack_4x8(unpack_unorm_1x8,
1053 op[0]->value.u[0],
1054 &data.f[0], &data.f[1], &data.f[2], &data.f[3]);
1055 break;
1056 case ir_unop_pack_half_2x16:
1057 assert(op[0]->type == glsl_type::vec2_type);
1058 data.u[0] = pack_2x16(pack_half_1x16,
1059 op[0]->value.f[0],
1060 op[0]->value.f[1]);
1061 break;
1062 case ir_unop_unpack_half_2x16:
1063 assert(op[0]->type == glsl_type::uint_type);
1064 unpack_2x16(unpack_half_1x16,
1065 op[0]->value.u[0],
1066 &data.f[0], &data.f[1]);
1067 break;
1068 case ir_binop_pow:
1069 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
1070 for (unsigned c = 0; c < op[0]->type->components(); c++) {
1071 data.f[c] = powf(op[0]->value.f[c], op[1]->value.f[c]);
1072 }
1073 break;
1074
1075 case ir_binop_dot:
1076 if (op[0]->type->base_type == GLSL_TYPE_DOUBLE)
1077 data.d[0] = dot_d(op[0], op[1]);
1078 else
1079 data.f[0] = dot_f(op[0], op[1]);
1080 break;
1081
1082 case ir_binop_min:
1083 assert(op[0]->type == op[1]->type || op0_scalar || op1_scalar);
1084 for (unsigned c = 0, c0 = 0, c1 = 0;
1085 c < components;
1086 c0 += c0_inc, c1 += c1_inc, c++) {
1087
1088 switch (op[0]->type->base_type) {
1089 case GLSL_TYPE_UINT:
1090 data.u[c] = MIN2(op[0]->value.u[c0], op[1]->value.u[c1]);
1091 break;
1092 case GLSL_TYPE_INT:
1093 data.i[c] = MIN2(op[0]->value.i[c0], op[1]->value.i[c1]);
1094 break;
1095 case GLSL_TYPE_FLOAT:
1096 data.f[c] = MIN2(op[0]->value.f[c0], op[1]->value.f[c1]);
1097 break;
1098 case GLSL_TYPE_DOUBLE:
1099 data.d[c] = MIN2(op[0]->value.d[c0], op[1]->value.d[c1]);
1100 break;
1101 default:
1102 assert(0);
1103 }
1104 }
1105
1106 break;
1107 case ir_binop_max:
1108 assert(op[0]->type == op[1]->type || op0_scalar || op1_scalar);
1109 for (unsigned c = 0, c0 = 0, c1 = 0;
1110 c < components;
1111 c0 += c0_inc, c1 += c1_inc, c++) {
1112
1113 switch (op[0]->type->base_type) {
1114 case GLSL_TYPE_UINT:
1115 data.u[c] = MAX2(op[0]->value.u[c0], op[1]->value.u[c1]);
1116 break;
1117 case GLSL_TYPE_INT:
1118 data.i[c] = MAX2(op[0]->value.i[c0], op[1]->value.i[c1]);
1119 break;
1120 case GLSL_TYPE_FLOAT:
1121 data.f[c] = MAX2(op[0]->value.f[c0], op[1]->value.f[c1]);
1122 break;
1123 case GLSL_TYPE_DOUBLE:
1124 data.d[c] = MAX2(op[0]->value.d[c0], op[1]->value.d[c1]);
1125 break;
1126 default:
1127 assert(0);
1128 }
1129 }
1130 break;
1131
1132 case ir_binop_add:
1133 assert(op[0]->type == op[1]->type || op0_scalar || op1_scalar);
1134 for (unsigned c = 0, c0 = 0, c1 = 0;
1135 c < components;
1136 c0 += c0_inc, c1 += c1_inc, c++) {
1137
1138 switch (op[0]->type->base_type) {
1139 case GLSL_TYPE_UINT:
1140 data.u[c] = op[0]->value.u[c0] + op[1]->value.u[c1];
1141 break;
1142 case GLSL_TYPE_INT:
1143 data.i[c] = op[0]->value.i[c0] + op[1]->value.i[c1];
1144 break;
1145 case GLSL_TYPE_FLOAT:
1146 data.f[c] = op[0]->value.f[c0] + op[1]->value.f[c1];
1147 break;
1148 case GLSL_TYPE_DOUBLE:
1149 data.d[c] = op[0]->value.d[c0] + op[1]->value.d[c1];
1150 break;
1151 default:
1152 assert(0);
1153 }
1154 }
1155
1156 break;
1157 case ir_binop_sub:
1158 assert(op[0]->type == op[1]->type || op0_scalar || op1_scalar);
1159 for (unsigned c = 0, c0 = 0, c1 = 0;
1160 c < components;
1161 c0 += c0_inc, c1 += c1_inc, c++) {
1162
1163 switch (op[0]->type->base_type) {
1164 case GLSL_TYPE_UINT:
1165 data.u[c] = op[0]->value.u[c0] - op[1]->value.u[c1];
1166 break;
1167 case GLSL_TYPE_INT:
1168 data.i[c] = op[0]->value.i[c0] - op[1]->value.i[c1];
1169 break;
1170 case GLSL_TYPE_FLOAT:
1171 data.f[c] = op[0]->value.f[c0] - op[1]->value.f[c1];
1172 break;
1173 case GLSL_TYPE_DOUBLE:
1174 data.d[c] = op[0]->value.d[c0] - op[1]->value.d[c1];
1175 break;
1176 default:
1177 assert(0);
1178 }
1179 }
1180
1181 break;
1182 case ir_binop_mul:
1183 /* Check for equal types, or unequal types involving scalars */
1184 if ((op[0]->type == op[1]->type && !op[0]->type->is_matrix())
1185 || op0_scalar || op1_scalar) {
1186 for (unsigned c = 0, c0 = 0, c1 = 0;
1187 c < components;
1188 c0 += c0_inc, c1 += c1_inc, c++) {
1189
1190 switch (op[0]->type->base_type) {
1191 case GLSL_TYPE_UINT:
1192 data.u[c] = op[0]->value.u[c0] * op[1]->value.u[c1];
1193 break;
1194 case GLSL_TYPE_INT:
1195 data.i[c] = op[0]->value.i[c0] * op[1]->value.i[c1];
1196 break;
1197 case GLSL_TYPE_FLOAT:
1198 data.f[c] = op[0]->value.f[c0] * op[1]->value.f[c1];
1199 break;
1200 case GLSL_TYPE_DOUBLE:
1201 data.d[c] = op[0]->value.d[c0] * op[1]->value.d[c1];
1202 break;
1203 default:
1204 assert(0);
1205 }
1206 }
1207 } else {
1208 assert(op[0]->type->is_matrix() || op[1]->type->is_matrix());
1209
1210 /* Multiply an N-by-M matrix with an M-by-P matrix. Since either
1211 * matrix can be a GLSL vector, either N or P can be 1.
1212 *
1213 * For vec*mat, the vector is treated as a row vector. This
1214 * means the vector is a 1-row x M-column matrix.
1215 *
1216 * For mat*vec, the vector is treated as a column vector. Since
1217 * matrix_columns is 1 for vectors, this just works.
1218 */
1219 const unsigned n = op[0]->type->is_vector()
1220 ? 1 : op[0]->type->vector_elements;
1221 const unsigned m = op[1]->type->vector_elements;
1222 const unsigned p = op[1]->type->matrix_columns;
1223 for (unsigned j = 0; j < p; j++) {
1224 for (unsigned i = 0; i < n; i++) {
1225 for (unsigned k = 0; k < m; k++) {
1226 if (op[0]->type->base_type == GLSL_TYPE_DOUBLE)
1227 data.d[i+n*j] += op[0]->value.d[i+n*k]*op[1]->value.d[k+m*j];
1228 else
1229 data.f[i+n*j] += op[0]->value.f[i+n*k]*op[1]->value.f[k+m*j];
1230 }
1231 }
1232 }
1233 }
1234
1235 break;
1236 case ir_binop_div:
1237 /* FINISHME: Emit warning when division-by-zero is detected. */
1238 assert(op[0]->type == op[1]->type || op0_scalar || op1_scalar);
1239 for (unsigned c = 0, c0 = 0, c1 = 0;
1240 c < components;
1241 c0 += c0_inc, c1 += c1_inc, c++) {
1242
1243 switch (op[0]->type->base_type) {
1244 case GLSL_TYPE_UINT:
1245 if (op[1]->value.u[c1] == 0) {
1246 data.u[c] = 0;
1247 } else {
1248 data.u[c] = op[0]->value.u[c0] / op[1]->value.u[c1];
1249 }
1250 break;
1251 case GLSL_TYPE_INT:
1252 if (op[1]->value.i[c1] == 0) {
1253 data.i[c] = 0;
1254 } else {
1255 data.i[c] = op[0]->value.i[c0] / op[1]->value.i[c1];
1256 }
1257 break;
1258 case GLSL_TYPE_FLOAT:
1259 data.f[c] = op[0]->value.f[c0] / op[1]->value.f[c1];
1260 break;
1261 case GLSL_TYPE_DOUBLE:
1262 data.d[c] = op[0]->value.d[c0] / op[1]->value.d[c1];
1263 break;
1264 default:
1265 assert(0);
1266 }
1267 }
1268
1269 break;
1270 case ir_binop_mod:
1271 /* FINISHME: Emit warning when division-by-zero is detected. */
1272 assert(op[0]->type == op[1]->type || op0_scalar || op1_scalar);
1273 for (unsigned c = 0, c0 = 0, c1 = 0;
1274 c < components;
1275 c0 += c0_inc, c1 += c1_inc, c++) {
1276
1277 switch (op[0]->type->base_type) {
1278 case GLSL_TYPE_UINT:
1279 if (op[1]->value.u[c1] == 0) {
1280 data.u[c] = 0;
1281 } else {
1282 data.u[c] = op[0]->value.u[c0] % op[1]->value.u[c1];
1283 }
1284 break;
1285 case GLSL_TYPE_INT:
1286 if (op[1]->value.i[c1] == 0) {
1287 data.i[c] = 0;
1288 } else {
1289 data.i[c] = op[0]->value.i[c0] % op[1]->value.i[c1];
1290 }
1291 break;
1292 case GLSL_TYPE_FLOAT:
1293 /* We don't use fmod because it rounds toward zero; GLSL specifies
1294 * the use of floor.
1295 */
1296 data.f[c] = op[0]->value.f[c0] - op[1]->value.f[c1]
1297 * floorf(op[0]->value.f[c0] / op[1]->value.f[c1]);
1298 break;
1299 case GLSL_TYPE_DOUBLE:
1300 /* We don't use fmod because it rounds toward zero; GLSL specifies
1301 * the use of floor.
1302 */
1303 data.d[c] = op[0]->value.d[c0] - op[1]->value.d[c1]
1304 * floor(op[0]->value.d[c0] / op[1]->value.d[c1]);
1305 break;
1306 default:
1307 assert(0);
1308 }
1309 }
1310
1311 break;
1312
1313 case ir_binop_logic_and:
1314 assert(op[0]->type->base_type == GLSL_TYPE_BOOL);
1315 for (unsigned c = 0; c < op[0]->type->components(); c++)
1316 data.b[c] = op[0]->value.b[c] && op[1]->value.b[c];
1317 break;
1318 case ir_binop_logic_xor:
1319 assert(op[0]->type->base_type == GLSL_TYPE_BOOL);
1320 for (unsigned c = 0; c < op[0]->type->components(); c++)
1321 data.b[c] = op[0]->value.b[c] ^ op[1]->value.b[c];
1322 break;
1323 case ir_binop_logic_or:
1324 assert(op[0]->type->base_type == GLSL_TYPE_BOOL);
1325 for (unsigned c = 0; c < op[0]->type->components(); c++)
1326 data.b[c] = op[0]->value.b[c] || op[1]->value.b[c];
1327 break;
1328
1329 case ir_binop_less:
1330 assert(op[0]->type == op[1]->type);
1331 for (unsigned c = 0; c < op[0]->type->components(); c++) {
1332 switch (op[0]->type->base_type) {
1333 case GLSL_TYPE_UINT:
1334 data.b[c] = op[0]->value.u[c] < op[1]->value.u[c];
1335 break;
1336 case GLSL_TYPE_INT:
1337 data.b[c] = op[0]->value.i[c] < op[1]->value.i[c];
1338 break;
1339 case GLSL_TYPE_FLOAT:
1340 data.b[c] = op[0]->value.f[c] < op[1]->value.f[c];
1341 break;
1342 case GLSL_TYPE_DOUBLE:
1343 data.b[c] = op[0]->value.d[c] < op[1]->value.d[c];
1344 break;
1345 default:
1346 assert(0);
1347 }
1348 }
1349 break;
1350 case ir_binop_greater:
1351 assert(op[0]->type == op[1]->type);
1352 for (unsigned c = 0; c < op[0]->type->components(); c++) {
1353 switch (op[0]->type->base_type) {
1354 case GLSL_TYPE_UINT:
1355 data.b[c] = op[0]->value.u[c] > op[1]->value.u[c];
1356 break;
1357 case GLSL_TYPE_INT:
1358 data.b[c] = op[0]->value.i[c] > op[1]->value.i[c];
1359 break;
1360 case GLSL_TYPE_FLOAT:
1361 data.b[c] = op[0]->value.f[c] > op[1]->value.f[c];
1362 break;
1363 case GLSL_TYPE_DOUBLE:
1364 data.b[c] = op[0]->value.d[c] > op[1]->value.d[c];
1365 break;
1366 default:
1367 assert(0);
1368 }
1369 }
1370 break;
1371 case ir_binop_lequal:
1372 assert(op[0]->type == op[1]->type);
1373 for (unsigned c = 0; c < op[0]->type->components(); c++) {
1374 switch (op[0]->type->base_type) {
1375 case GLSL_TYPE_UINT:
1376 data.b[c] = op[0]->value.u[c] <= op[1]->value.u[c];
1377 break;
1378 case GLSL_TYPE_INT:
1379 data.b[c] = op[0]->value.i[c] <= op[1]->value.i[c];
1380 break;
1381 case GLSL_TYPE_FLOAT:
1382 data.b[c] = op[0]->value.f[c] <= op[1]->value.f[c];
1383 break;
1384 case GLSL_TYPE_DOUBLE:
1385 data.b[c] = op[0]->value.d[c] <= op[1]->value.d[c];
1386 break;
1387 default:
1388 assert(0);
1389 }
1390 }
1391 break;
1392 case ir_binop_gequal:
1393 assert(op[0]->type == op[1]->type);
1394 for (unsigned c = 0; c < op[0]->type->components(); c++) {
1395 switch (op[0]->type->base_type) {
1396 case GLSL_TYPE_UINT:
1397 data.b[c] = op[0]->value.u[c] >= op[1]->value.u[c];
1398 break;
1399 case GLSL_TYPE_INT:
1400 data.b[c] = op[0]->value.i[c] >= op[1]->value.i[c];
1401 break;
1402 case GLSL_TYPE_FLOAT:
1403 data.b[c] = op[0]->value.f[c] >= op[1]->value.f[c];
1404 break;
1405 case GLSL_TYPE_DOUBLE:
1406 data.b[c] = op[0]->value.d[c] >= op[1]->value.d[c];
1407 break;
1408 default:
1409 assert(0);
1410 }
1411 }
1412 break;
1413 case ir_binop_equal:
1414 assert(op[0]->type == op[1]->type);
1415 for (unsigned c = 0; c < components; c++) {
1416 switch (op[0]->type->base_type) {
1417 case GLSL_TYPE_UINT:
1418 data.b[c] = op[0]->value.u[c] == op[1]->value.u[c];
1419 break;
1420 case GLSL_TYPE_INT:
1421 data.b[c] = op[0]->value.i[c] == op[1]->value.i[c];
1422 break;
1423 case GLSL_TYPE_FLOAT:
1424 data.b[c] = op[0]->value.f[c] == op[1]->value.f[c];
1425 break;
1426 case GLSL_TYPE_BOOL:
1427 data.b[c] = op[0]->value.b[c] == op[1]->value.b[c];
1428 break;
1429 case GLSL_TYPE_DOUBLE:
1430 data.b[c] = op[0]->value.d[c] == op[1]->value.d[c];
1431 break;
1432 default:
1433 assert(0);
1434 }
1435 }
1436 break;
1437 case ir_binop_nequal:
1438 assert(op[0]->type == op[1]->type);
1439 for (unsigned c = 0; c < components; c++) {
1440 switch (op[0]->type->base_type) {
1441 case GLSL_TYPE_UINT:
1442 data.b[c] = op[0]->value.u[c] != op[1]->value.u[c];
1443 break;
1444 case GLSL_TYPE_INT:
1445 data.b[c] = op[0]->value.i[c] != op[1]->value.i[c];
1446 break;
1447 case GLSL_TYPE_FLOAT:
1448 data.b[c] = op[0]->value.f[c] != op[1]->value.f[c];
1449 break;
1450 case GLSL_TYPE_BOOL:
1451 data.b[c] = op[0]->value.b[c] != op[1]->value.b[c];
1452 break;
1453 case GLSL_TYPE_DOUBLE:
1454 data.b[c] = op[0]->value.d[c] != op[1]->value.d[c];
1455 break;
1456 default:
1457 assert(0);
1458 }
1459 }
1460 break;
1461 case ir_binop_all_equal:
1462 data.b[0] = op[0]->has_value(op[1]);
1463 break;
1464 case ir_binop_any_nequal:
1465 data.b[0] = !op[0]->has_value(op[1]);
1466 break;
1467
1468 case ir_binop_lshift:
1469 for (unsigned c = 0, c0 = 0, c1 = 0;
1470 c < components;
1471 c0 += c0_inc, c1 += c1_inc, c++) {
1472
1473 if (op[0]->type->base_type == GLSL_TYPE_INT &&
1474 op[1]->type->base_type == GLSL_TYPE_INT) {
1475 data.i[c] = op[0]->value.i[c0] << op[1]->value.i[c1];
1476
1477 } else if (op[0]->type->base_type == GLSL_TYPE_INT &&
1478 op[1]->type->base_type == GLSL_TYPE_UINT) {
1479 data.i[c] = op[0]->value.i[c0] << op[1]->value.u[c1];
1480
1481 } else if (op[0]->type->base_type == GLSL_TYPE_UINT &&
1482 op[1]->type->base_type == GLSL_TYPE_INT) {
1483 data.u[c] = op[0]->value.u[c0] << op[1]->value.i[c1];
1484
1485 } else if (op[0]->type->base_type == GLSL_TYPE_UINT &&
1486 op[1]->type->base_type == GLSL_TYPE_UINT) {
1487 data.u[c] = op[0]->value.u[c0] << op[1]->value.u[c1];
1488 }
1489 }
1490 break;
1491
1492 case ir_binop_rshift:
1493 for (unsigned c = 0, c0 = 0, c1 = 0;
1494 c < components;
1495 c0 += c0_inc, c1 += c1_inc, c++) {
1496
1497 if (op[0]->type->base_type == GLSL_TYPE_INT &&
1498 op[1]->type->base_type == GLSL_TYPE_INT) {
1499 data.i[c] = op[0]->value.i[c0] >> op[1]->value.i[c1];
1500
1501 } else if (op[0]->type->base_type == GLSL_TYPE_INT &&
1502 op[1]->type->base_type == GLSL_TYPE_UINT) {
1503 data.i[c] = op[0]->value.i[c0] >> op[1]->value.u[c1];
1504
1505 } else if (op[0]->type->base_type == GLSL_TYPE_UINT &&
1506 op[1]->type->base_type == GLSL_TYPE_INT) {
1507 data.u[c] = op[0]->value.u[c0] >> op[1]->value.i[c1];
1508
1509 } else if (op[0]->type->base_type == GLSL_TYPE_UINT &&
1510 op[1]->type->base_type == GLSL_TYPE_UINT) {
1511 data.u[c] = op[0]->value.u[c0] >> op[1]->value.u[c1];
1512 }
1513 }
1514 break;
1515
1516 case ir_binop_bit_and:
1517 for (unsigned c = 0, c0 = 0, c1 = 0;
1518 c < components;
1519 c0 += c0_inc, c1 += c1_inc, c++) {
1520
1521 switch (op[0]->type->base_type) {
1522 case GLSL_TYPE_INT:
1523 data.i[c] = op[0]->value.i[c0] & op[1]->value.i[c1];
1524 break;
1525 case GLSL_TYPE_UINT:
1526 data.u[c] = op[0]->value.u[c0] & op[1]->value.u[c1];
1527 break;
1528 default:
1529 assert(0);
1530 }
1531 }
1532 break;
1533
1534 case ir_binop_bit_or:
1535 for (unsigned c = 0, c0 = 0, c1 = 0;
1536 c < components;
1537 c0 += c0_inc, c1 += c1_inc, c++) {
1538
1539 switch (op[0]->type->base_type) {
1540 case GLSL_TYPE_INT:
1541 data.i[c] = op[0]->value.i[c0] | op[1]->value.i[c1];
1542 break;
1543 case GLSL_TYPE_UINT:
1544 data.u[c] = op[0]->value.u[c0] | op[1]->value.u[c1];
1545 break;
1546 default:
1547 assert(0);
1548 }
1549 }
1550 break;
1551
1552 case ir_binop_vector_extract: {
1553 const int c = CLAMP(op[1]->value.i[0], 0,
1554 (int) op[0]->type->vector_elements - 1);
1555
1556 switch (op[0]->type->base_type) {
1557 case GLSL_TYPE_UINT:
1558 data.u[0] = op[0]->value.u[c];
1559 break;
1560 case GLSL_TYPE_INT:
1561 data.i[0] = op[0]->value.i[c];
1562 break;
1563 case GLSL_TYPE_FLOAT:
1564 data.f[0] = op[0]->value.f[c];
1565 break;
1566 case GLSL_TYPE_DOUBLE:
1567 data.d[0] = op[0]->value.d[c];
1568 break;
1569 case GLSL_TYPE_BOOL:
1570 data.b[0] = op[0]->value.b[c];
1571 break;
1572 default:
1573 assert(0);
1574 }
1575 break;
1576 }
1577
1578 case ir_binop_bit_xor:
1579 for (unsigned c = 0, c0 = 0, c1 = 0;
1580 c < components;
1581 c0 += c0_inc, c1 += c1_inc, c++) {
1582
1583 switch (op[0]->type->base_type) {
1584 case GLSL_TYPE_INT:
1585 data.i[c] = op[0]->value.i[c0] ^ op[1]->value.i[c1];
1586 break;
1587 case GLSL_TYPE_UINT:
1588 data.u[c] = op[0]->value.u[c0] ^ op[1]->value.u[c1];
1589 break;
1590 default:
1591 assert(0);
1592 }
1593 }
1594 break;
1595
1596 case ir_unop_bitfield_reverse:
1597 for (unsigned c = 0; c < op[0]->type->components(); c++) {
1598 switch (this->type->base_type) {
1599 case GLSL_TYPE_UINT:
1600 data.u[c] = bitfield_reverse(op[0]->value.u[c]);
1601 break;
1602 case GLSL_TYPE_INT:
1603 data.i[c] = bitfield_reverse(op[0]->value.i[c]);
1604 break;
1605 default:
1606 assert(0);
1607 }
1608 }
1609 break;
1610
1611 case ir_unop_bit_count:
1612 for (unsigned c = 0; c < components; c++)
1613 data.i[c] = _mesa_bitcount(op[0]->value.u[c]);
1614 break;
1615
1616 case ir_unop_find_msb:
1617 for (unsigned c = 0; c < components; c++) {
1618 switch (op[0]->type->base_type) {
1619 case GLSL_TYPE_UINT:
1620 data.i[c] = find_msb_uint(op[0]->value.u[c]);
1621 break;
1622 case GLSL_TYPE_INT:
1623 data.i[c] = find_msb_int(op[0]->value.i[c]);
1624 break;
1625 default:
1626 assert(0);
1627 }
1628 }
1629 break;
1630
1631 case ir_unop_find_lsb:
1632 for (unsigned c = 0; c < components; c++) {
1633 switch (op[0]->type->base_type) {
1634 case GLSL_TYPE_UINT:
1635 data.i[c] = find_msb_uint(op[0]->value.u[c] & -op[0]->value.u[c]);
1636 break;
1637 case GLSL_TYPE_INT:
1638 data.i[c] = find_msb_uint(op[0]->value.i[c] & -op[0]->value.i[c]);
1639 break;
1640 default:
1641 assert(0);
1642 }
1643 }
1644 break;
1645
1646 case ir_unop_saturate:
1647 for (unsigned c = 0; c < components; c++) {
1648 data.f[c] = CLAMP(op[0]->value.f[c], 0.0f, 1.0f);
1649 }
1650 break;
1651 case ir_unop_pack_double_2x32:
1652 /* XXX needs to be checked on big-endian */
1653 memcpy(&data.d[0], &op[0]->value.u[0], sizeof(double));
1654 break;
1655 case ir_unop_unpack_double_2x32:
1656 /* XXX needs to be checked on big-endian */
1657 memcpy(&data.u[0], &op[0]->value.d[0], sizeof(double));
1658 break;
1659
1660 case ir_triop_bitfield_extract:
1661 for (unsigned c = 0; c < components; c++) {
1662 switch (this->type->base_type) {
1663 case GLSL_TYPE_UINT:
1664 data.u[c] = bitfield_extract_uint(op[0]->value.u[c], op[1]->value.i[c], op[2]->value.i[c]);
1665 break;
1666 case GLSL_TYPE_INT:
1667 data.i[c] = bitfield_extract_int(op[0]->value.i[c], op[1]->value.i[c], op[2]->value.i[c]);
1668 break;
1669 default:
1670 assert(0);
1671 }
1672 }
1673 break;
1674
1675 case ir_binop_ldexp:
1676 for (unsigned c = 0; c < op[0]->type->components(); c++) {
1677 switch (this->type->base_type) {
1678 case GLSL_TYPE_FLOAT:
1679 data.f[c] = ldexpf_flush_subnormal(op[0]->value.f[c], op[1]->value.i[c]);
1680 break;
1681 case GLSL_TYPE_DOUBLE:
1682 data.d[c] = ldexp_flush_subnormal(op[0]->value.d[c], op[1]->value.i[c]);
1683 break;
1684 default:
1685 assert(0);
1686 }
1687 }
1688 break;
1689
1690 case ir_triop_fma:
1691 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT ||
1692 op[0]->type->base_type == GLSL_TYPE_DOUBLE);
1693 assert(op[1]->type->base_type == GLSL_TYPE_FLOAT ||
1694 op[1]->type->base_type == GLSL_TYPE_DOUBLE);
1695 assert(op[2]->type->base_type == GLSL_TYPE_FLOAT ||
1696 op[2]->type->base_type == GLSL_TYPE_DOUBLE);
1697
1698 for (unsigned c = 0; c < components; c++) {
1699 if (op[0]->type->base_type == GLSL_TYPE_DOUBLE)
1700 data.d[c] = op[0]->value.d[c] * op[1]->value.d[c]
1701 + op[2]->value.d[c];
1702 else
1703 data.f[c] = op[0]->value.f[c] * op[1]->value.f[c]
1704 + op[2]->value.f[c];
1705 }
1706 break;
1707
1708 case ir_triop_lrp: {
1709 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT ||
1710 op[0]->type->base_type == GLSL_TYPE_DOUBLE);
1711 assert(op[1]->type->base_type == GLSL_TYPE_FLOAT ||
1712 op[1]->type->base_type == GLSL_TYPE_DOUBLE);
1713 assert(op[2]->type->base_type == GLSL_TYPE_FLOAT ||
1714 op[2]->type->base_type == GLSL_TYPE_DOUBLE);
1715
1716 unsigned c2_inc = op[2]->type->is_scalar() ? 0 : 1;
1717 for (unsigned c = 0, c2 = 0; c < components; c2 += c2_inc, c++) {
1718 if (op[0]->type->base_type == GLSL_TYPE_DOUBLE)
1719 data.d[c] = op[0]->value.d[c] * (1.0 - op[2]->value.d[c2]) +
1720 (op[1]->value.d[c] * op[2]->value.d[c2]);
1721 else
1722 data.f[c] = op[0]->value.f[c] * (1.0f - op[2]->value.f[c2]) +
1723 (op[1]->value.f[c] * op[2]->value.f[c2]);
1724 }
1725 break;
1726 }
1727
1728 case ir_triop_csel:
1729 for (unsigned c = 0; c < components; c++) {
1730 if (op[1]->type->base_type == GLSL_TYPE_DOUBLE)
1731 data.d[c] = op[0]->value.b[c] ? op[1]->value.d[c]
1732 : op[2]->value.d[c];
1733 else
1734 data.u[c] = op[0]->value.b[c] ? op[1]->value.u[c]
1735 : op[2]->value.u[c];
1736 }
1737 break;
1738
1739 case ir_triop_vector_insert: {
1740 const unsigned idx = op[2]->value.u[0];
1741
1742 memcpy(&data, &op[0]->value, sizeof(data));
1743
1744 switch (this->type->base_type) {
1745 case GLSL_TYPE_INT:
1746 data.i[idx] = op[1]->value.i[0];
1747 break;
1748 case GLSL_TYPE_UINT:
1749 data.u[idx] = op[1]->value.u[0];
1750 break;
1751 case GLSL_TYPE_FLOAT:
1752 data.f[idx] = op[1]->value.f[0];
1753 break;
1754 case GLSL_TYPE_BOOL:
1755 data.b[idx] = op[1]->value.b[0];
1756 break;
1757 case GLSL_TYPE_DOUBLE:
1758 data.d[idx] = op[1]->value.d[0];
1759 break;
1760 default:
1761 assert(!"Should not get here.");
1762 break;
1763 }
1764 break;
1765 }
1766
1767 case ir_quadop_bitfield_insert:
1768 for (unsigned c = 0; c < components; c++)
1769 data.u[c] = bitfield_insert(op[0]->value.u[c], op[1]->value.u[c], op[2]->value.i[c], op[3]->value.i[c]);
1770 break;
1771
1772 case ir_quadop_vector:
1773 for (unsigned c = 0; c < this->type->vector_elements; c++) {
1774 switch (this->type->base_type) {
1775 case GLSL_TYPE_INT:
1776 data.i[c] = op[c]->value.i[0];
1777 break;
1778 case GLSL_TYPE_UINT:
1779 data.u[c] = op[c]->value.u[0];
1780 break;
1781 case GLSL_TYPE_FLOAT:
1782 data.f[c] = op[c]->value.f[0];
1783 break;
1784 case GLSL_TYPE_DOUBLE:
1785 data.d[c] = op[c]->value.d[0];
1786 break;
1787 case GLSL_TYPE_BOOL:
1788 data.b[c] = op[c]->value.b[0];
1789 break;
1790 default:
1791 assert(0);
1792 }
1793 }
1794 break;
1795
1796 default:
1797 /* FINISHME: Should handle all expression types. */
1798 return NULL;
1799 }
1800
1801 return new(ctx) ir_constant(this->type, &data);
1802 }
1803
1804
1805 ir_constant *
1806 ir_texture::constant_expression_value(struct hash_table *)
1807 {
1808 /* texture lookups aren't constant expressions */
1809 return NULL;
1810 }
1811
1812
1813 ir_constant *
1814 ir_swizzle::constant_expression_value(struct hash_table *variable_context)
1815 {
1816 ir_constant *v = this->val->constant_expression_value(variable_context);
1817
1818 if (v != NULL) {
1819 ir_constant_data data = { { 0 } };
1820
1821 const unsigned swiz_idx[4] = {
1822 this->mask.x, this->mask.y, this->mask.z, this->mask.w
1823 };
1824
1825 for (unsigned i = 0; i < this->mask.num_components; i++) {
1826 switch (v->type->base_type) {
1827 case GLSL_TYPE_UINT:
1828 case GLSL_TYPE_INT: data.u[i] = v->value.u[swiz_idx[i]]; break;
1829 case GLSL_TYPE_FLOAT: data.f[i] = v->value.f[swiz_idx[i]]; break;
1830 case GLSL_TYPE_BOOL: data.b[i] = v->value.b[swiz_idx[i]]; break;
1831 case GLSL_TYPE_DOUBLE:data.d[i] = v->value.d[swiz_idx[i]]; break;
1832 default: assert(!"Should not get here."); break;
1833 }
1834 }
1835
1836 void *ctx = ralloc_parent(this);
1837 return new(ctx) ir_constant(this->type, &data);
1838 }
1839 return NULL;
1840 }
1841
1842
1843 ir_constant *
1844 ir_dereference_variable::constant_expression_value(struct hash_table *variable_context)
1845 {
1846 assert(var);
1847
1848 /* Give priority to the context hashtable, if it exists */
1849 if (variable_context) {
1850 ir_constant *value = (ir_constant *)hash_table_find(variable_context, var);
1851 if(value)
1852 return value;
1853 }
1854
1855 /* The constant_value of a uniform variable is its initializer,
1856 * not the lifetime constant value of the uniform.
1857 */
1858 if (var->data.mode == ir_var_uniform)
1859 return NULL;
1860
1861 if (!var->constant_value)
1862 return NULL;
1863
1864 return var->constant_value->clone(ralloc_parent(var), NULL);
1865 }
1866
1867
1868 ir_constant *
1869 ir_dereference_array::constant_expression_value(struct hash_table *variable_context)
1870 {
1871 ir_constant *array = this->array->constant_expression_value(variable_context);
1872 ir_constant *idx = this->array_index->constant_expression_value(variable_context);
1873
1874 if ((array != NULL) && (idx != NULL)) {
1875 void *ctx = ralloc_parent(this);
1876 if (array->type->is_matrix()) {
1877 /* Array access of a matrix results in a vector.
1878 */
1879 const unsigned column = idx->value.u[0];
1880
1881 const glsl_type *const column_type = array->type->column_type();
1882
1883 /* Offset in the constant matrix to the first element of the column
1884 * to be extracted.
1885 */
1886 const unsigned mat_idx = column * column_type->vector_elements;
1887
1888 ir_constant_data data = { { 0 } };
1889
1890 switch (column_type->base_type) {
1891 case GLSL_TYPE_UINT:
1892 case GLSL_TYPE_INT:
1893 for (unsigned i = 0; i < column_type->vector_elements; i++)
1894 data.u[i] = array->value.u[mat_idx + i];
1895
1896 break;
1897
1898 case GLSL_TYPE_FLOAT:
1899 for (unsigned i = 0; i < column_type->vector_elements; i++)
1900 data.f[i] = array->value.f[mat_idx + i];
1901
1902 break;
1903
1904 case GLSL_TYPE_DOUBLE:
1905 for (unsigned i = 0; i < column_type->vector_elements; i++)
1906 data.d[i] = array->value.d[mat_idx + i];
1907
1908 break;
1909
1910 default:
1911 assert(!"Should not get here.");
1912 break;
1913 }
1914
1915 return new(ctx) ir_constant(column_type, &data);
1916 } else if (array->type->is_vector()) {
1917 const unsigned component = idx->value.u[0];
1918
1919 return new(ctx) ir_constant(array, component);
1920 } else {
1921 const unsigned index = idx->value.u[0];
1922 return array->get_array_element(index)->clone(ctx, NULL);
1923 }
1924 }
1925 return NULL;
1926 }
1927
1928
1929 ir_constant *
1930 ir_dereference_record::constant_expression_value(struct hash_table *)
1931 {
1932 ir_constant *v = this->record->constant_expression_value();
1933
1934 return (v != NULL) ? v->get_record_field(this->field) : NULL;
1935 }
1936
1937
1938 ir_constant *
1939 ir_assignment::constant_expression_value(struct hash_table *)
1940 {
1941 /* FINISHME: Handle CEs involving assignment (return RHS) */
1942 return NULL;
1943 }
1944
1945
1946 ir_constant *
1947 ir_constant::constant_expression_value(struct hash_table *)
1948 {
1949 return this;
1950 }
1951
1952
1953 ir_constant *
1954 ir_call::constant_expression_value(struct hash_table *variable_context)
1955 {
1956 return this->callee->constant_expression_value(&this->actual_parameters, variable_context);
1957 }
1958
1959
1960 bool ir_function_signature::constant_expression_evaluate_expression_list(const struct exec_list &body,
1961 struct hash_table *variable_context,
1962 ir_constant **result)
1963 {
1964 foreach_in_list(ir_instruction, inst, &body) {
1965 switch(inst->ir_type) {
1966
1967 /* (declare () type symbol) */
1968 case ir_type_variable: {
1969 ir_variable *var = inst->as_variable();
1970 hash_table_insert(variable_context, ir_constant::zero(this, var->type), var);
1971 break;
1972 }
1973
1974 /* (assign [condition] (write-mask) (ref) (value)) */
1975 case ir_type_assignment: {
1976 ir_assignment *asg = inst->as_assignment();
1977 if (asg->condition) {
1978 ir_constant *cond = asg->condition->constant_expression_value(variable_context);
1979 if (!cond)
1980 return false;
1981 if (!cond->get_bool_component(0))
1982 break;
1983 }
1984
1985 ir_constant *store = NULL;
1986 int offset = 0;
1987
1988 if (!constant_referenced(asg->lhs, variable_context, store, offset))
1989 return false;
1990
1991 ir_constant *value = asg->rhs->constant_expression_value(variable_context);
1992
1993 if (!value)
1994 return false;
1995
1996 store->copy_masked_offset(value, offset, asg->write_mask);
1997 break;
1998 }
1999
2000 /* (return (expression)) */
2001 case ir_type_return:
2002 assert (result);
2003 *result = inst->as_return()->value->constant_expression_value(variable_context);
2004 return *result != NULL;
2005
2006 /* (call name (ref) (params))*/
2007 case ir_type_call: {
2008 ir_call *call = inst->as_call();
2009
2010 /* Just say no to void functions in constant expressions. We
2011 * don't need them at that point.
2012 */
2013
2014 if (!call->return_deref)
2015 return false;
2016
2017 ir_constant *store = NULL;
2018 int offset = 0;
2019
2020 if (!constant_referenced(call->return_deref, variable_context,
2021 store, offset))
2022 return false;
2023
2024 ir_constant *value = call->constant_expression_value(variable_context);
2025
2026 if(!value)
2027 return false;
2028
2029 store->copy_offset(value, offset);
2030 break;
2031 }
2032
2033 /* (if condition (then-instructions) (else-instructions)) */
2034 case ir_type_if: {
2035 ir_if *iif = inst->as_if();
2036
2037 ir_constant *cond = iif->condition->constant_expression_value(variable_context);
2038 if (!cond || !cond->type->is_boolean())
2039 return false;
2040
2041 exec_list &branch = cond->get_bool_component(0) ? iif->then_instructions : iif->else_instructions;
2042
2043 *result = NULL;
2044 if (!constant_expression_evaluate_expression_list(branch, variable_context, result))
2045 return false;
2046
2047 /* If there was a return in the branch chosen, drop out now. */
2048 if (*result)
2049 return true;
2050
2051 break;
2052 }
2053
2054 /* Every other expression type, we drop out. */
2055 default:
2056 return false;
2057 }
2058 }
2059
2060 /* Reaching the end of the block is not an error condition */
2061 if (result)
2062 *result = NULL;
2063
2064 return true;
2065 }
2066
2067 ir_constant *
2068 ir_function_signature::constant_expression_value(exec_list *actual_parameters, struct hash_table *variable_context)
2069 {
2070 const glsl_type *type = this->return_type;
2071 if (type == glsl_type::void_type)
2072 return NULL;
2073
2074 /* From the GLSL 1.20 spec, page 23:
2075 * "Function calls to user-defined functions (non-built-in functions)
2076 * cannot be used to form constant expressions."
2077 */
2078 if (!this->is_builtin())
2079 return NULL;
2080
2081 /*
2082 * Of the builtin functions, only the texture lookups and the noise
2083 * ones must not be used in constant expressions. They all include
2084 * specific opcodes so they don't need to be special-cased at this
2085 * point.
2086 */
2087
2088 /* Initialize the table of dereferencable names with the function
2089 * parameters. Verify their const-ness on the way.
2090 *
2091 * We expect the correctness of the number of parameters to have
2092 * been checked earlier.
2093 */
2094 hash_table *deref_hash = hash_table_ctor(8, hash_table_pointer_hash,
2095 hash_table_pointer_compare);
2096
2097 /* If "origin" is non-NULL, then the function body is there. So we
2098 * have to use the variable objects from the object with the body,
2099 * but the parameter instanciation on the current object.
2100 */
2101 const exec_node *parameter_info = origin ? origin->parameters.get_head_raw() : parameters.get_head_raw();
2102
2103 foreach_in_list(ir_rvalue, n, actual_parameters) {
2104 ir_constant *constant = n->constant_expression_value(variable_context);
2105 if (constant == NULL) {
2106 hash_table_dtor(deref_hash);
2107 return NULL;
2108 }
2109
2110
2111 ir_variable *var = (ir_variable *)parameter_info;
2112 hash_table_insert(deref_hash, constant, var);
2113
2114 parameter_info = parameter_info->next;
2115 }
2116
2117 ir_constant *result = NULL;
2118
2119 /* Now run the builtin function until something non-constant
2120 * happens or we get the result.
2121 */
2122 if (constant_expression_evaluate_expression_list(origin ? origin->body : body, deref_hash, &result) && result)
2123 result = result->clone(ralloc_parent(this), NULL);
2124
2125 hash_table_dtor(deref_hash);
2126
2127 return result;
2128 }