glsl: Add support for float16 types in the IR tree
[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 "util/rounding.h" /* for _mesa_roundeven */
38 #include "util/half_float.h"
39 #include "ir.h"
40 #include "compiler/glsl_types.h"
41 #include "util/hash_table.h"
42 #include "util/u_math.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 static_assert(sizeof(float) == sizeof(unsigned int),
77 "float and unsigned int size mismatch");
78 float f;
79 memcpy(&f, &u, sizeof(f));
80 return f;
81 }
82
83 static unsigned int
84 bitcast_f2u(float f)
85 {
86 static_assert(sizeof(float) == sizeof(unsigned int),
87 "float and unsigned int size mismatch");
88 unsigned int u;
89 memcpy(&u, &f, sizeof(f));
90 return u;
91 }
92
93 static double
94 bitcast_u642d(uint64_t u)
95 {
96 static_assert(sizeof(double) == sizeof(uint64_t),
97 "double and uint64_t size mismatch");
98 double d;
99 memcpy(&d, &u, sizeof(d));
100 return d;
101 }
102
103 static double
104 bitcast_i642d(int64_t i)
105 {
106 static_assert(sizeof(double) == sizeof(int64_t),
107 "double and int64_t size mismatch");
108 double d;
109 memcpy(&d, &i, sizeof(d));
110 return d;
111 }
112
113 static uint64_t
114 bitcast_d2u64(double d)
115 {
116 static_assert(sizeof(double) == sizeof(uint64_t),
117 "double and uint64_t size mismatch");
118 uint64_t u;
119 memcpy(&u, &d, sizeof(d));
120 return u;
121 }
122
123 static int64_t
124 bitcast_d2i64(double d)
125 {
126 static_assert(sizeof(double) == sizeof(int64_t),
127 "double and int64_t size mismatch");
128 int64_t i;
129 memcpy(&i, &d, sizeof(d));
130 return i;
131 }
132
133 /**
134 * Evaluate one component of a floating-point 4x8 unpacking function.
135 */
136 typedef uint8_t
137 (*pack_1x8_func_t)(float);
138
139 /**
140 * Evaluate one component of a floating-point 2x16 unpacking function.
141 */
142 typedef uint16_t
143 (*pack_1x16_func_t)(float);
144
145 /**
146 * Evaluate one component of a floating-point 4x8 unpacking function.
147 */
148 typedef float
149 (*unpack_1x8_func_t)(uint8_t);
150
151 /**
152 * Evaluate one component of a floating-point 2x16 unpacking function.
153 */
154 typedef float
155 (*unpack_1x16_func_t)(uint16_t);
156
157 /**
158 * Evaluate a 2x16 floating-point packing function.
159 */
160 static uint32_t
161 pack_2x16(pack_1x16_func_t pack_1x16,
162 float x, float y)
163 {
164 /* From section 8.4 of the GLSL ES 3.00 spec:
165 *
166 * packSnorm2x16
167 * -------------
168 * The first component of the vector will be written to the least
169 * significant bits of the output; the last component will be written to
170 * the most significant bits.
171 *
172 * The specifications for the other packing functions contain similar
173 * language.
174 */
175 uint32_t u = 0;
176 u |= ((uint32_t) pack_1x16(x) << 0);
177 u |= ((uint32_t) pack_1x16(y) << 16);
178 return u;
179 }
180
181 /**
182 * Evaluate a 4x8 floating-point packing function.
183 */
184 static uint32_t
185 pack_4x8(pack_1x8_func_t pack_1x8,
186 float x, float y, float z, float w)
187 {
188 /* From section 8.4 of the GLSL 4.30 spec:
189 *
190 * packSnorm4x8
191 * ------------
192 * The first component of the vector will be written to the least
193 * significant bits of the output; the last component will be written to
194 * the most significant bits.
195 *
196 * The specifications for the other packing functions contain similar
197 * language.
198 */
199 uint32_t u = 0;
200 u |= ((uint32_t) pack_1x8(x) << 0);
201 u |= ((uint32_t) pack_1x8(y) << 8);
202 u |= ((uint32_t) pack_1x8(z) << 16);
203 u |= ((uint32_t) pack_1x8(w) << 24);
204 return u;
205 }
206
207 /**
208 * Evaluate a 2x16 floating-point unpacking function.
209 */
210 static void
211 unpack_2x16(unpack_1x16_func_t unpack_1x16,
212 uint32_t u,
213 float *x, float *y)
214 {
215 /* From section 8.4 of the GLSL ES 3.00 spec:
216 *
217 * unpackSnorm2x16
218 * ---------------
219 * The first component of the returned vector will be extracted from
220 * the least significant bits of the input; the last component will be
221 * extracted from the most significant bits.
222 *
223 * The specifications for the other unpacking functions contain similar
224 * language.
225 */
226 *x = unpack_1x16((uint16_t) (u & 0xffff));
227 *y = unpack_1x16((uint16_t) (u >> 16));
228 }
229
230 /**
231 * Evaluate a 4x8 floating-point unpacking function.
232 */
233 static void
234 unpack_4x8(unpack_1x8_func_t unpack_1x8, uint32_t u,
235 float *x, float *y, float *z, float *w)
236 {
237 /* From section 8.4 of the GLSL 4.30 spec:
238 *
239 * unpackSnorm4x8
240 * --------------
241 * The first component of the returned vector will be extracted from
242 * the least significant bits of the input; the last component will be
243 * extracted from the most significant bits.
244 *
245 * The specifications for the other unpacking functions contain similar
246 * language.
247 */
248 *x = unpack_1x8((uint8_t) (u & 0xff));
249 *y = unpack_1x8((uint8_t) (u >> 8));
250 *z = unpack_1x8((uint8_t) (u >> 16));
251 *w = unpack_1x8((uint8_t) (u >> 24));
252 }
253
254 /**
255 * Evaluate one component of packSnorm4x8.
256 */
257 static uint8_t
258 pack_snorm_1x8(float x)
259 {
260 /* From section 8.4 of the GLSL 4.30 spec:
261 *
262 * packSnorm4x8
263 * ------------
264 * The conversion for component c of v to fixed point is done as
265 * follows:
266 *
267 * packSnorm4x8: round(clamp(c, -1, +1) * 127.0)
268 */
269 return (uint8_t)
270 _mesa_lroundevenf(CLAMP(x, -1.0f, +1.0f) * 127.0f);
271 }
272
273 /**
274 * Evaluate one component of packSnorm2x16.
275 */
276 static uint16_t
277 pack_snorm_1x16(float x)
278 {
279 /* From section 8.4 of the GLSL ES 3.00 spec:
280 *
281 * packSnorm2x16
282 * -------------
283 * The conversion for component c of v to fixed point is done as
284 * follows:
285 *
286 * packSnorm2x16: round(clamp(c, -1, +1) * 32767.0)
287 */
288 return (uint16_t)
289 _mesa_lroundevenf(CLAMP(x, -1.0f, +1.0f) * 32767.0f);
290 }
291
292 /**
293 * Evaluate one component of unpackSnorm4x8.
294 */
295 static float
296 unpack_snorm_1x8(uint8_t u)
297 {
298 /* From section 8.4 of the GLSL 4.30 spec:
299 *
300 * unpackSnorm4x8
301 * --------------
302 * The conversion for unpacked fixed-point value f to floating point is
303 * done as follows:
304 *
305 * unpackSnorm4x8: clamp(f / 127.0, -1, +1)
306 */
307 return CLAMP((int8_t) u / 127.0f, -1.0f, +1.0f);
308 }
309
310 /**
311 * Evaluate one component of unpackSnorm2x16.
312 */
313 static float
314 unpack_snorm_1x16(uint16_t u)
315 {
316 /* From section 8.4 of the GLSL ES 3.00 spec:
317 *
318 * unpackSnorm2x16
319 * ---------------
320 * The conversion for unpacked fixed-point value f to floating point is
321 * done as follows:
322 *
323 * unpackSnorm2x16: clamp(f / 32767.0, -1, +1)
324 */
325 return CLAMP((int16_t) u / 32767.0f, -1.0f, +1.0f);
326 }
327
328 /**
329 * Evaluate one component packUnorm4x8.
330 */
331 static uint8_t
332 pack_unorm_1x8(float x)
333 {
334 /* From section 8.4 of the GLSL 4.30 spec:
335 *
336 * packUnorm4x8
337 * ------------
338 * The conversion for component c of v to fixed point is done as
339 * follows:
340 *
341 * packUnorm4x8: round(clamp(c, 0, +1) * 255.0)
342 */
343 return (uint8_t) (int) _mesa_roundevenf(CLAMP(x, 0.0f, 1.0f) * 255.0f);
344 }
345
346 /**
347 * Evaluate one component packUnorm2x16.
348 */
349 static uint16_t
350 pack_unorm_1x16(float x)
351 {
352 /* From section 8.4 of the GLSL ES 3.00 spec:
353 *
354 * packUnorm2x16
355 * -------------
356 * The conversion for component c of v to fixed point is done as
357 * follows:
358 *
359 * packUnorm2x16: round(clamp(c, 0, +1) * 65535.0)
360 */
361 return (uint16_t) (int)
362 _mesa_roundevenf(CLAMP(x, 0.0f, 1.0f) * 65535.0f);
363 }
364
365 /**
366 * Evaluate one component of unpackUnorm4x8.
367 */
368 static float
369 unpack_unorm_1x8(uint8_t u)
370 {
371 /* From section 8.4 of the GLSL 4.30 spec:
372 *
373 * unpackUnorm4x8
374 * --------------
375 * The conversion for unpacked fixed-point value f to floating point is
376 * done as follows:
377 *
378 * unpackUnorm4x8: f / 255.0
379 */
380 return (float) u / 255.0f;
381 }
382
383 /**
384 * Evaluate one component of unpackUnorm2x16.
385 */
386 static float
387 unpack_unorm_1x16(uint16_t u)
388 {
389 /* From section 8.4 of the GLSL ES 3.00 spec:
390 *
391 * unpackUnorm2x16
392 * ---------------
393 * The conversion for unpacked fixed-point value f to floating point is
394 * done as follows:
395 *
396 * unpackUnorm2x16: f / 65535.0
397 */
398 return (float) u / 65535.0f;
399 }
400
401 /**
402 * Evaluate one component of packHalf2x16.
403 */
404 static uint16_t
405 pack_half_1x16(float x)
406 {
407 return _mesa_float_to_half(x);
408 }
409
410 /**
411 * Evaluate one component of unpackHalf2x16.
412 */
413 static float
414 unpack_half_1x16(uint16_t u)
415 {
416 return _mesa_half_to_float(u);
417 }
418
419 static int32_t
420 iadd_saturate(int32_t a, int32_t b)
421 {
422 return CLAMP(int64_t(a) + int64_t(b), INT32_MIN, INT32_MAX);
423 }
424
425 static int64_t
426 iadd64_saturate(int64_t a, int64_t b)
427 {
428 if (a < 0 && b < INT64_MIN - a)
429 return INT64_MIN;
430
431 if (a > 0 && b > INT64_MAX - a)
432 return INT64_MAX;
433
434 return a + b;
435 }
436
437 static int32_t
438 isub_saturate(int32_t a, int32_t b)
439 {
440 return CLAMP(int64_t(a) - int64_t(b), INT32_MIN, INT32_MAX);
441 }
442
443 static int64_t
444 isub64_saturate(int64_t a, int64_t b)
445 {
446 if (b > 0 && a < INT64_MIN + b)
447 return INT64_MIN;
448
449 if (b < 0 && a > INT64_MAX + b)
450 return INT64_MAX;
451
452 return a - b;
453 }
454
455 /**
456 * Get the constant that is ultimately referenced by an r-value, in a constant
457 * expression evaluation context.
458 *
459 * The offset is used when the reference is to a specific column of a matrix.
460 */
461 static bool
462 constant_referenced(const ir_dereference *deref,
463 struct hash_table *variable_context,
464 ir_constant *&store, int &offset)
465 {
466 store = NULL;
467 offset = 0;
468
469 if (variable_context == NULL)
470 return false;
471
472 switch (deref->ir_type) {
473 case ir_type_dereference_array: {
474 const ir_dereference_array *const da =
475 (const ir_dereference_array *) deref;
476
477 ir_constant *const index_c =
478 da->array_index->constant_expression_value(variable_context);
479
480 if (!index_c || !index_c->type->is_scalar() ||
481 !index_c->type->is_integer_32())
482 break;
483
484 const int index = index_c->type->base_type == GLSL_TYPE_INT ?
485 index_c->get_int_component(0) :
486 index_c->get_uint_component(0);
487
488 ir_constant *substore;
489 int suboffset;
490
491 const ir_dereference *const deref = da->array->as_dereference();
492 if (!deref)
493 break;
494
495 if (!constant_referenced(deref, variable_context, substore, suboffset))
496 break;
497
498 const glsl_type *const vt = da->array->type;
499 if (vt->is_array()) {
500 store = substore->get_array_element(index);
501 offset = 0;
502 } else if (vt->is_matrix()) {
503 store = substore;
504 offset = index * vt->vector_elements;
505 } else if (vt->is_vector()) {
506 store = substore;
507 offset = suboffset + index;
508 }
509
510 break;
511 }
512
513 case ir_type_dereference_record: {
514 const ir_dereference_record *const dr =
515 (const ir_dereference_record *) deref;
516
517 const ir_dereference *const deref = dr->record->as_dereference();
518 if (!deref)
519 break;
520
521 ir_constant *substore;
522 int suboffset;
523
524 if (!constant_referenced(deref, variable_context, substore, suboffset))
525 break;
526
527 /* Since we're dropping it on the floor...
528 */
529 assert(suboffset == 0);
530
531 store = substore->get_record_field(dr->field_idx);
532 break;
533 }
534
535 case ir_type_dereference_variable: {
536 const ir_dereference_variable *const dv =
537 (const ir_dereference_variable *) deref;
538
539 hash_entry *entry = _mesa_hash_table_search(variable_context, dv->var);
540 if (entry)
541 store = (ir_constant *) entry->data;
542 break;
543 }
544
545 default:
546 assert(!"Should not get here.");
547 break;
548 }
549
550 return store != NULL;
551 }
552
553
554 ir_constant *
555 ir_rvalue::constant_expression_value(void *, struct hash_table *)
556 {
557 assert(this->type->is_error());
558 return NULL;
559 }
560
561 static uint32_t
562 bitfield_reverse(uint32_t v)
563 {
564 /* http://graphics.stanford.edu/~seander/bithacks.html#BitReverseObvious */
565 uint32_t r = v; // r will be reversed bits of v; first get LSB of v
566 int s = sizeof(v) * CHAR_BIT - 1; // extra shift needed at end
567
568 for (v >>= 1; v; v >>= 1) {
569 r <<= 1;
570 r |= v & 1;
571 s--;
572 }
573 r <<= s; // shift when v's highest bits are zero
574
575 return r;
576 }
577
578 static int
579 find_msb_uint(uint32_t v)
580 {
581 int count = 0;
582
583 /* If v == 0, then the loop will terminate when count == 32. In that case
584 * 31-count will produce the -1 result required by GLSL findMSB().
585 */
586 while (((v & (1u << 31)) == 0) && count != 32) {
587 count++;
588 v <<= 1;
589 }
590
591 return 31 - count;
592 }
593
594 static int
595 find_msb_int(int32_t v)
596 {
597 /* If v is signed, findMSB() returns the position of the most significant
598 * zero bit.
599 */
600 return find_msb_uint(v < 0 ? ~v : v);
601 }
602
603 static float
604 ldexpf_flush_subnormal(float x, int exp)
605 {
606 const float result = ldexpf(x, exp);
607
608 /* Flush subnormal values to zero. */
609 return !isnormal(result) ? copysignf(0.0f, x) : result;
610 }
611
612 static double
613 ldexp_flush_subnormal(double x, int exp)
614 {
615 const double result = ldexp(x, exp);
616
617 /* Flush subnormal values to zero. */
618 return !isnormal(result) ? copysign(0.0, x) : result;
619 }
620
621 static uint32_t
622 bitfield_extract_uint(uint32_t value, int offset, int bits)
623 {
624 if (bits == 0)
625 return 0;
626 else if (offset < 0 || bits < 0)
627 return 0; /* Undefined, per spec. */
628 else if (offset + bits > 32)
629 return 0; /* Undefined, per spec. */
630 else {
631 value <<= 32 - bits - offset;
632 value >>= 32 - bits;
633 return value;
634 }
635 }
636
637 static int32_t
638 bitfield_extract_int(int32_t value, int offset, int bits)
639 {
640 if (bits == 0)
641 return 0;
642 else if (offset < 0 || bits < 0)
643 return 0; /* Undefined, per spec. */
644 else if (offset + bits > 32)
645 return 0; /* Undefined, per spec. */
646 else {
647 value <<= 32 - bits - offset;
648 value >>= 32 - bits;
649 return value;
650 }
651 }
652
653 static uint32_t
654 bitfield_insert(uint32_t base, uint32_t insert, int offset, int bits)
655 {
656 if (bits == 0)
657 return base;
658 else if (offset < 0 || bits < 0)
659 return 0; /* Undefined, per spec. */
660 else if (offset + bits > 32)
661 return 0; /* Undefined, per spec. */
662 else {
663 unsigned insert_mask = ((1ull << bits) - 1) << offset;
664
665 insert <<= offset;
666 insert &= insert_mask;
667 base &= ~insert_mask;
668
669 return base | insert;
670 }
671 }
672
673 ir_constant *
674 ir_expression::constant_expression_value(void *mem_ctx,
675 struct hash_table *variable_context)
676 {
677 assert(mem_ctx);
678
679 if (this->type->is_error())
680 return NULL;
681
682 ir_constant *op[ARRAY_SIZE(this->operands)] = { NULL, };
683 ir_constant_data data;
684
685 memset(&data, 0, sizeof(data));
686
687 for (unsigned operand = 0; operand < this->num_operands; operand++) {
688 op[operand] =
689 this->operands[operand]->constant_expression_value(mem_ctx,
690 variable_context);
691 if (!op[operand])
692 return NULL;
693 }
694
695 if (op[1] != NULL)
696 switch (this->operation) {
697 case ir_binop_lshift:
698 case ir_binop_rshift:
699 case ir_binop_ldexp:
700 case ir_binop_interpolate_at_offset:
701 case ir_binop_interpolate_at_sample:
702 case ir_binop_vector_extract:
703 case ir_triop_csel:
704 case ir_triop_bitfield_extract:
705 break;
706
707 default:
708 assert(op[0]->type->base_type == op[1]->type->base_type);
709 break;
710 }
711
712 bool op0_scalar = op[0]->type->is_scalar();
713 bool op1_scalar = op[1] != NULL && op[1]->type->is_scalar();
714
715 /* When iterating over a vector or matrix's components, we want to increase
716 * the loop counter. However, for scalars, we want to stay at 0.
717 */
718 unsigned c0_inc = op0_scalar ? 0 : 1;
719 unsigned c1_inc = op1_scalar ? 0 : 1;
720 unsigned components;
721 if (op1_scalar || !op[1]) {
722 components = op[0]->type->components();
723 } else {
724 components = op[1]->type->components();
725 }
726
727 /* Handle array operations here, rather than below. */
728 if (op[0]->type->is_array()) {
729 assert(op[1] != NULL && op[1]->type->is_array());
730 switch (this->operation) {
731 case ir_binop_all_equal:
732 return new(mem_ctx) ir_constant(op[0]->has_value(op[1]));
733 case ir_binop_any_nequal:
734 return new(mem_ctx) ir_constant(!op[0]->has_value(op[1]));
735 default:
736 break;
737 }
738 return NULL;
739 }
740
741 #include "ir_expression_operation_constant.h"
742
743 return new(mem_ctx) ir_constant(this->type, &data);
744 }
745
746
747 ir_constant *
748 ir_texture::constant_expression_value(void *, struct hash_table *)
749 {
750 /* texture lookups aren't constant expressions */
751 return NULL;
752 }
753
754
755 ir_constant *
756 ir_swizzle::constant_expression_value(void *mem_ctx,
757 struct hash_table *variable_context)
758 {
759 assert(mem_ctx);
760
761 ir_constant *v = this->val->constant_expression_value(mem_ctx,
762 variable_context);
763
764 if (v != NULL) {
765 ir_constant_data data = { { 0 } };
766
767 const unsigned swiz_idx[4] = {
768 this->mask.x, this->mask.y, this->mask.z, this->mask.w
769 };
770
771 for (unsigned i = 0; i < this->mask.num_components; i++) {
772 switch (v->type->base_type) {
773 case GLSL_TYPE_UINT:
774 case GLSL_TYPE_INT: data.u[i] = v->value.u[swiz_idx[i]]; break;
775 case GLSL_TYPE_FLOAT: data.f[i] = v->value.f[swiz_idx[i]]; break;
776 case GLSL_TYPE_FLOAT16: data.f16[i] = v->value.f16[swiz_idx[i]]; break;
777 case GLSL_TYPE_BOOL: data.b[i] = v->value.b[swiz_idx[i]]; break;
778 case GLSL_TYPE_DOUBLE:data.d[i] = v->value.d[swiz_idx[i]]; break;
779 case GLSL_TYPE_UINT64:data.u64[i] = v->value.u64[swiz_idx[i]]; break;
780 case GLSL_TYPE_INT64: data.i64[i] = v->value.i64[swiz_idx[i]]; break;
781 default: assert(!"Should not get here."); break;
782 }
783 }
784
785 return new(mem_ctx) ir_constant(this->type, &data);
786 }
787 return NULL;
788 }
789
790
791 ir_constant *
792 ir_dereference_variable::constant_expression_value(void *mem_ctx,
793 struct hash_table *variable_context)
794 {
795 assert(var);
796 assert(mem_ctx);
797
798 /* Give priority to the context hashtable, if it exists */
799 if (variable_context) {
800 hash_entry *entry = _mesa_hash_table_search(variable_context, var);
801
802 if(entry)
803 return (ir_constant *) entry->data;
804 }
805
806 /* The constant_value of a uniform variable is its initializer,
807 * not the lifetime constant value of the uniform.
808 */
809 if (var->data.mode == ir_var_uniform)
810 return NULL;
811
812 if (!var->constant_value)
813 return NULL;
814
815 return var->constant_value->clone(mem_ctx, NULL);
816 }
817
818
819 ir_constant *
820 ir_dereference_array::constant_expression_value(void *mem_ctx,
821 struct hash_table *variable_context)
822 {
823 assert(mem_ctx);
824
825 ir_constant *array = this->array->constant_expression_value(mem_ctx, variable_context);
826 ir_constant *idx = this->array_index->constant_expression_value(mem_ctx, variable_context);
827
828 if ((array != NULL) && (idx != NULL)) {
829 if (array->type->is_matrix()) {
830 /* Array access of a matrix results in a vector.
831 */
832 const unsigned column = idx->value.u[0];
833
834 const glsl_type *const column_type = array->type->column_type();
835
836 /* Offset in the constant matrix to the first element of the column
837 * to be extracted.
838 */
839 const unsigned mat_idx = column * column_type->vector_elements;
840
841 ir_constant_data data = { { 0 } };
842
843 switch (column_type->base_type) {
844 case GLSL_TYPE_UINT:
845 case GLSL_TYPE_INT:
846 for (unsigned i = 0; i < column_type->vector_elements; i++)
847 data.u[i] = array->value.u[mat_idx + i];
848
849 break;
850
851 case GLSL_TYPE_FLOAT:
852 for (unsigned i = 0; i < column_type->vector_elements; i++)
853 data.f[i] = array->value.f[mat_idx + i];
854
855 break;
856
857 case GLSL_TYPE_DOUBLE:
858 for (unsigned i = 0; i < column_type->vector_elements; i++)
859 data.d[i] = array->value.d[mat_idx + i];
860
861 break;
862
863 default:
864 assert(!"Should not get here.");
865 break;
866 }
867
868 return new(mem_ctx) ir_constant(column_type, &data);
869 } else if (array->type->is_vector()) {
870 const unsigned component = idx->value.u[0];
871
872 return new(mem_ctx) ir_constant(array, component);
873 } else if (array->type->is_array()) {
874 const unsigned index = idx->value.u[0];
875 return array->get_array_element(index)->clone(mem_ctx, NULL);
876 }
877 }
878 return NULL;
879 }
880
881
882 ir_constant *
883 ir_dereference_record::constant_expression_value(void *mem_ctx,
884 struct hash_table *)
885 {
886 assert(mem_ctx);
887
888 ir_constant *v = this->record->constant_expression_value(mem_ctx);
889
890 return (v != NULL) ? v->get_record_field(this->field_idx) : NULL;
891 }
892
893
894 ir_constant *
895 ir_assignment::constant_expression_value(void *, struct hash_table *)
896 {
897 /* FINISHME: Handle CEs involving assignment (return RHS) */
898 return NULL;
899 }
900
901
902 ir_constant *
903 ir_constant::constant_expression_value(void *, struct hash_table *)
904 {
905 return this;
906 }
907
908
909 ir_constant *
910 ir_call::constant_expression_value(void *mem_ctx, struct hash_table *variable_context)
911 {
912 assert(mem_ctx);
913
914 return this->callee->constant_expression_value(mem_ctx,
915 &this->actual_parameters,
916 variable_context);
917 }
918
919
920 bool ir_function_signature::constant_expression_evaluate_expression_list(void *mem_ctx,
921 const struct exec_list &body,
922 struct hash_table *variable_context,
923 ir_constant **result)
924 {
925 assert(mem_ctx);
926
927 foreach_in_list(ir_instruction, inst, &body) {
928 switch(inst->ir_type) {
929
930 /* (declare () type symbol) */
931 case ir_type_variable: {
932 ir_variable *var = inst->as_variable();
933 _mesa_hash_table_insert(variable_context, var, ir_constant::zero(this, var->type));
934 break;
935 }
936
937 /* (assign [condition] (write-mask) (ref) (value)) */
938 case ir_type_assignment: {
939 ir_assignment *asg = inst->as_assignment();
940 if (asg->condition) {
941 ir_constant *cond =
942 asg->condition->constant_expression_value(mem_ctx,
943 variable_context);
944 if (!cond)
945 return false;
946 if (!cond->get_bool_component(0))
947 break;
948 }
949
950 ir_constant *store = NULL;
951 int offset = 0;
952
953 if (!constant_referenced(asg->lhs, variable_context, store, offset))
954 return false;
955
956 ir_constant *value =
957 asg->rhs->constant_expression_value(mem_ctx, variable_context);
958
959 if (!value)
960 return false;
961
962 store->copy_masked_offset(value, offset, asg->write_mask);
963 break;
964 }
965
966 /* (return (expression)) */
967 case ir_type_return:
968 assert (result);
969 *result =
970 inst->as_return()->value->constant_expression_value(mem_ctx,
971 variable_context);
972 return *result != NULL;
973
974 /* (call name (ref) (params))*/
975 case ir_type_call: {
976 ir_call *call = inst->as_call();
977
978 /* Just say no to void functions in constant expressions. We
979 * don't need them at that point.
980 */
981
982 if (!call->return_deref)
983 return false;
984
985 ir_constant *store = NULL;
986 int offset = 0;
987
988 if (!constant_referenced(call->return_deref, variable_context,
989 store, offset))
990 return false;
991
992 ir_constant *value =
993 call->constant_expression_value(mem_ctx, variable_context);
994
995 if(!value)
996 return false;
997
998 store->copy_offset(value, offset);
999 break;
1000 }
1001
1002 /* (if condition (then-instructions) (else-instructions)) */
1003 case ir_type_if: {
1004 ir_if *iif = inst->as_if();
1005
1006 ir_constant *cond =
1007 iif->condition->constant_expression_value(mem_ctx,
1008 variable_context);
1009 if (!cond || !cond->type->is_boolean())
1010 return false;
1011
1012 exec_list &branch = cond->get_bool_component(0) ? iif->then_instructions : iif->else_instructions;
1013
1014 *result = NULL;
1015 if (!constant_expression_evaluate_expression_list(mem_ctx, branch,
1016 variable_context,
1017 result))
1018 return false;
1019
1020 /* If there was a return in the branch chosen, drop out now. */
1021 if (*result)
1022 return true;
1023
1024 break;
1025 }
1026
1027 /* Every other expression type, we drop out. */
1028 default:
1029 return false;
1030 }
1031 }
1032
1033 /* Reaching the end of the block is not an error condition */
1034 if (result)
1035 *result = NULL;
1036
1037 return true;
1038 }
1039
1040 ir_constant *
1041 ir_function_signature::constant_expression_value(void *mem_ctx,
1042 exec_list *actual_parameters,
1043 struct hash_table *variable_context)
1044 {
1045 assert(mem_ctx);
1046
1047 const glsl_type *type = this->return_type;
1048 if (type == glsl_type::void_type)
1049 return NULL;
1050
1051 /* From the GLSL 1.20 spec, page 23:
1052 * "Function calls to user-defined functions (non-built-in functions)
1053 * cannot be used to form constant expressions."
1054 */
1055 if (!this->is_builtin())
1056 return NULL;
1057
1058 /*
1059 * Of the builtin functions, only the texture lookups and the noise
1060 * ones must not be used in constant expressions. They all include
1061 * specific opcodes so they don't need to be special-cased at this
1062 * point.
1063 */
1064
1065 /* Initialize the table of dereferencable names with the function
1066 * parameters. Verify their const-ness on the way.
1067 *
1068 * We expect the correctness of the number of parameters to have
1069 * been checked earlier.
1070 */
1071 hash_table *deref_hash = _mesa_pointer_hash_table_create(NULL);
1072
1073 /* If "origin" is non-NULL, then the function body is there. So we
1074 * have to use the variable objects from the object with the body,
1075 * but the parameter instanciation on the current object.
1076 */
1077 const exec_node *parameter_info = origin ? origin->parameters.get_head_raw() : parameters.get_head_raw();
1078
1079 foreach_in_list(ir_rvalue, n, actual_parameters) {
1080 ir_constant *constant =
1081 n->constant_expression_value(mem_ctx, variable_context);
1082 if (constant == NULL) {
1083 _mesa_hash_table_destroy(deref_hash, NULL);
1084 return NULL;
1085 }
1086
1087
1088 ir_variable *var = (ir_variable *)parameter_info;
1089 _mesa_hash_table_insert(deref_hash, var, constant);
1090
1091 parameter_info = parameter_info->next;
1092 }
1093
1094 ir_constant *result = NULL;
1095
1096 /* Now run the builtin function until something non-constant
1097 * happens or we get the result.
1098 */
1099 if (constant_expression_evaluate_expression_list(mem_ctx, origin ? origin->body : body, deref_hash, &result) &&
1100 result)
1101 result = result->clone(mem_ctx, NULL);
1102
1103 _mesa_hash_table_destroy(deref_hash, NULL);
1104
1105 return result;
1106 }