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