glsl: Fix locations of variables in patch qualified interface blocks.
[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 #include "ir_expression_operation_constant.h"
658
659 return new(ctx) ir_constant(this->type, &data);
660 }
661
662
663 ir_constant *
664 ir_texture::constant_expression_value(struct hash_table *)
665 {
666 /* texture lookups aren't constant expressions */
667 return NULL;
668 }
669
670
671 ir_constant *
672 ir_swizzle::constant_expression_value(struct hash_table *variable_context)
673 {
674 ir_constant *v = this->val->constant_expression_value(variable_context);
675
676 if (v != NULL) {
677 ir_constant_data data = { { 0 } };
678
679 const unsigned swiz_idx[4] = {
680 this->mask.x, this->mask.y, this->mask.z, this->mask.w
681 };
682
683 for (unsigned i = 0; i < this->mask.num_components; i++) {
684 switch (v->type->base_type) {
685 case GLSL_TYPE_UINT:
686 case GLSL_TYPE_INT: data.u[i] = v->value.u[swiz_idx[i]]; break;
687 case GLSL_TYPE_FLOAT: data.f[i] = v->value.f[swiz_idx[i]]; break;
688 case GLSL_TYPE_BOOL: data.b[i] = v->value.b[swiz_idx[i]]; break;
689 case GLSL_TYPE_DOUBLE:data.d[i] = v->value.d[swiz_idx[i]]; break;
690 default: assert(!"Should not get here."); break;
691 }
692 }
693
694 void *ctx = ralloc_parent(this);
695 return new(ctx) ir_constant(this->type, &data);
696 }
697 return NULL;
698 }
699
700
701 ir_constant *
702 ir_dereference_variable::constant_expression_value(struct hash_table *variable_context)
703 {
704 assert(var);
705
706 /* Give priority to the context hashtable, if it exists */
707 if (variable_context) {
708 ir_constant *value = (ir_constant *)hash_table_find(variable_context, var);
709 if(value)
710 return value;
711 }
712
713 /* The constant_value of a uniform variable is its initializer,
714 * not the lifetime constant value of the uniform.
715 */
716 if (var->data.mode == ir_var_uniform)
717 return NULL;
718
719 if (!var->constant_value)
720 return NULL;
721
722 return var->constant_value->clone(ralloc_parent(var), NULL);
723 }
724
725
726 ir_constant *
727 ir_dereference_array::constant_expression_value(struct hash_table *variable_context)
728 {
729 ir_constant *array = this->array->constant_expression_value(variable_context);
730 ir_constant *idx = this->array_index->constant_expression_value(variable_context);
731
732 if ((array != NULL) && (idx != NULL)) {
733 void *ctx = ralloc_parent(this);
734 if (array->type->is_matrix()) {
735 /* Array access of a matrix results in a vector.
736 */
737 const unsigned column = idx->value.u[0];
738
739 const glsl_type *const column_type = array->type->column_type();
740
741 /* Offset in the constant matrix to the first element of the column
742 * to be extracted.
743 */
744 const unsigned mat_idx = column * column_type->vector_elements;
745
746 ir_constant_data data = { { 0 } };
747
748 switch (column_type->base_type) {
749 case GLSL_TYPE_UINT:
750 case GLSL_TYPE_INT:
751 for (unsigned i = 0; i < column_type->vector_elements; i++)
752 data.u[i] = array->value.u[mat_idx + i];
753
754 break;
755
756 case GLSL_TYPE_FLOAT:
757 for (unsigned i = 0; i < column_type->vector_elements; i++)
758 data.f[i] = array->value.f[mat_idx + i];
759
760 break;
761
762 case GLSL_TYPE_DOUBLE:
763 for (unsigned i = 0; i < column_type->vector_elements; i++)
764 data.d[i] = array->value.d[mat_idx + i];
765
766 break;
767
768 default:
769 assert(!"Should not get here.");
770 break;
771 }
772
773 return new(ctx) ir_constant(column_type, &data);
774 } else if (array->type->is_vector()) {
775 const unsigned component = idx->value.u[0];
776
777 return new(ctx) ir_constant(array, component);
778 } else {
779 const unsigned index = idx->value.u[0];
780 return array->get_array_element(index)->clone(ctx, NULL);
781 }
782 }
783 return NULL;
784 }
785
786
787 ir_constant *
788 ir_dereference_record::constant_expression_value(struct hash_table *)
789 {
790 ir_constant *v = this->record->constant_expression_value();
791
792 return (v != NULL) ? v->get_record_field(this->field) : NULL;
793 }
794
795
796 ir_constant *
797 ir_assignment::constant_expression_value(struct hash_table *)
798 {
799 /* FINISHME: Handle CEs involving assignment (return RHS) */
800 return NULL;
801 }
802
803
804 ir_constant *
805 ir_constant::constant_expression_value(struct hash_table *)
806 {
807 return this;
808 }
809
810
811 ir_constant *
812 ir_call::constant_expression_value(struct hash_table *variable_context)
813 {
814 return this->callee->constant_expression_value(&this->actual_parameters, variable_context);
815 }
816
817
818 bool ir_function_signature::constant_expression_evaluate_expression_list(const struct exec_list &body,
819 struct hash_table *variable_context,
820 ir_constant **result)
821 {
822 foreach_in_list(ir_instruction, inst, &body) {
823 switch(inst->ir_type) {
824
825 /* (declare () type symbol) */
826 case ir_type_variable: {
827 ir_variable *var = inst->as_variable();
828 hash_table_insert(variable_context, ir_constant::zero(this, var->type), var);
829 break;
830 }
831
832 /* (assign [condition] (write-mask) (ref) (value)) */
833 case ir_type_assignment: {
834 ir_assignment *asg = inst->as_assignment();
835 if (asg->condition) {
836 ir_constant *cond = asg->condition->constant_expression_value(variable_context);
837 if (!cond)
838 return false;
839 if (!cond->get_bool_component(0))
840 break;
841 }
842
843 ir_constant *store = NULL;
844 int offset = 0;
845
846 if (!constant_referenced(asg->lhs, variable_context, store, offset))
847 return false;
848
849 ir_constant *value = asg->rhs->constant_expression_value(variable_context);
850
851 if (!value)
852 return false;
853
854 store->copy_masked_offset(value, offset, asg->write_mask);
855 break;
856 }
857
858 /* (return (expression)) */
859 case ir_type_return:
860 assert (result);
861 *result = inst->as_return()->value->constant_expression_value(variable_context);
862 return *result != NULL;
863
864 /* (call name (ref) (params))*/
865 case ir_type_call: {
866 ir_call *call = inst->as_call();
867
868 /* Just say no to void functions in constant expressions. We
869 * don't need them at that point.
870 */
871
872 if (!call->return_deref)
873 return false;
874
875 ir_constant *store = NULL;
876 int offset = 0;
877
878 if (!constant_referenced(call->return_deref, variable_context,
879 store, offset))
880 return false;
881
882 ir_constant *value = call->constant_expression_value(variable_context);
883
884 if(!value)
885 return false;
886
887 store->copy_offset(value, offset);
888 break;
889 }
890
891 /* (if condition (then-instructions) (else-instructions)) */
892 case ir_type_if: {
893 ir_if *iif = inst->as_if();
894
895 ir_constant *cond = iif->condition->constant_expression_value(variable_context);
896 if (!cond || !cond->type->is_boolean())
897 return false;
898
899 exec_list &branch = cond->get_bool_component(0) ? iif->then_instructions : iif->else_instructions;
900
901 *result = NULL;
902 if (!constant_expression_evaluate_expression_list(branch, variable_context, result))
903 return false;
904
905 /* If there was a return in the branch chosen, drop out now. */
906 if (*result)
907 return true;
908
909 break;
910 }
911
912 /* Every other expression type, we drop out. */
913 default:
914 return false;
915 }
916 }
917
918 /* Reaching the end of the block is not an error condition */
919 if (result)
920 *result = NULL;
921
922 return true;
923 }
924
925 ir_constant *
926 ir_function_signature::constant_expression_value(exec_list *actual_parameters, struct hash_table *variable_context)
927 {
928 const glsl_type *type = this->return_type;
929 if (type == glsl_type::void_type)
930 return NULL;
931
932 /* From the GLSL 1.20 spec, page 23:
933 * "Function calls to user-defined functions (non-built-in functions)
934 * cannot be used to form constant expressions."
935 */
936 if (!this->is_builtin())
937 return NULL;
938
939 /*
940 * Of the builtin functions, only the texture lookups and the noise
941 * ones must not be used in constant expressions. They all include
942 * specific opcodes so they don't need to be special-cased at this
943 * point.
944 */
945
946 /* Initialize the table of dereferencable names with the function
947 * parameters. Verify their const-ness on the way.
948 *
949 * We expect the correctness of the number of parameters to have
950 * been checked earlier.
951 */
952 hash_table *deref_hash = hash_table_ctor(8, hash_table_pointer_hash,
953 hash_table_pointer_compare);
954
955 /* If "origin" is non-NULL, then the function body is there. So we
956 * have to use the variable objects from the object with the body,
957 * but the parameter instanciation on the current object.
958 */
959 const exec_node *parameter_info = origin ? origin->parameters.get_head_raw() : parameters.get_head_raw();
960
961 foreach_in_list(ir_rvalue, n, actual_parameters) {
962 ir_constant *constant = n->constant_expression_value(variable_context);
963 if (constant == NULL) {
964 hash_table_dtor(deref_hash);
965 return NULL;
966 }
967
968
969 ir_variable *var = (ir_variable *)parameter_info;
970 hash_table_insert(deref_hash, constant, var);
971
972 parameter_info = parameter_info->next;
973 }
974
975 ir_constant *result = NULL;
976
977 /* Now run the builtin function until something non-constant
978 * happens or we get the result.
979 */
980 if (constant_expression_evaluate_expression_list(origin ? origin->body : body, deref_hash, &result) && result)
981 result = result->clone(ralloc_parent(this), NULL);
982
983 hash_table_dtor(deref_hash);
984
985 return result;
986 }