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
dot_f(ir_constant * op0,ir_constant * op1)45 dot_f(ir_constant *op0, ir_constant *op1)
46 {
47 assert(glsl_type_is_float(op0->type) && glsl_type_is_float(op1->type));
48
49 float result = 0;
50 for (unsigned c = 0; c < glsl_get_components(op0->type); 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(glsl_type_is_double(op0->type) && glsl_type_is_double(op1->type));
60
61 double result = 0;
62 for (unsigned c = 0; c < glsl_get_components(op0->type); 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 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
bitcast_f2u(float f)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
bitcast_u642d(uint64_t u)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
bitcast_i642d(int64_t i)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
bitcast_d2u64(double d)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
bitcast_d2i64(double d)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
pack_2x16(pack_1x16_func_t pack_1x16,float x,float y)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
pack_4x8(pack_1x8_func_t pack_1x8,float x,float y,float z,float w)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
unpack_2x16(unpack_1x16_func_t unpack_1x16,uint32_t u,float * x,float * y)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
unpack_4x8(unpack_1x8_func_t unpack_1x8,uint32_t u,float * x,float * y,float * z,float * w)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
pack_snorm_1x8(float x)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
pack_snorm_1x16(float x)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
unpack_snorm_1x8(uint8_t u)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
unpack_snorm_1x16(uint16_t u)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
pack_unorm_1x8(float x)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(SATURATE(x) * 255.0f);
344 }
345
346 /**
347 * Evaluate one component packUnorm2x16.
348 */
349 static uint16_t
pack_unorm_1x16(float x)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(SATURATE(x) * 65535.0f);
363 }
364
365 /**
366 * Evaluate one component of unpackUnorm4x8.
367 */
368 static float
unpack_unorm_1x8(uint8_t u)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
unpack_unorm_1x16(uint16_t u)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
pack_half_1x16(float x)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
unpack_half_1x16(uint16_t u)414 unpack_half_1x16(uint16_t u)
415 {
416 return _mesa_half_to_float(u);
417 }
418
419 static int32_t
iadd_saturate(int32_t a,int32_t b)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
iadd64_saturate(int64_t a,int64_t b)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
isub_saturate(int32_t a,int32_t b)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
isub64_saturate(int64_t a,int64_t b)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 static uint64_t
pack_2x32(uint32_t a,uint32_t b)456 pack_2x32(uint32_t a, uint32_t b)
457 {
458 uint64_t v = a;
459 v |= (uint64_t)b << 32;
460 return v;
461 }
462
463 static void
unpack_2x32(uint64_t p,uint32_t * a,uint32_t * b)464 unpack_2x32(uint64_t p, uint32_t *a, uint32_t *b)
465 {
466 *a = p & 0xffffffff;
467 *b = (p >> 32);
468 }
469
470 /**
471 * Get the constant that is ultimately referenced by an r-value, in a constant
472 * expression evaluation context.
473 *
474 * The offset is used when the reference is to a specific column of a matrix.
475 */
476 static bool
constant_referenced(const ir_dereference * deref,struct hash_table * variable_context,ir_constant * & store,int & offset)477 constant_referenced(const ir_dereference *deref,
478 struct hash_table *variable_context,
479 ir_constant *&store, int &offset)
480 {
481 store = NULL;
482 offset = 0;
483
484 if (variable_context == NULL)
485 return false;
486
487 switch (deref->ir_type) {
488 case ir_type_dereference_array: {
489 const ir_dereference_array *const da =
490 (const ir_dereference_array *) deref;
491
492 ir_constant *const index_c =
493 da->array_index->constant_expression_value(variable_context);
494
495 if (!index_c || !glsl_type_is_scalar(index_c->type) ||
496 !glsl_type_is_integer_32(index_c->type))
497 break;
498
499 const int index = index_c->type->base_type == GLSL_TYPE_INT ?
500 index_c->get_int_component(0) :
501 index_c->get_uint_component(0);
502
503 ir_constant *substore;
504 int suboffset;
505
506 const ir_dereference *const deref = da->array->as_dereference();
507 if (!deref)
508 break;
509
510 if (!constant_referenced(deref, variable_context, substore, suboffset))
511 break;
512
513 const glsl_type *const vt = da->array->type;
514 if (glsl_type_is_array(vt)) {
515 store = substore->get_array_element(index);
516 offset = 0;
517 } else if (glsl_type_is_matrix(vt)) {
518 store = substore;
519 offset = index * vt->vector_elements;
520 } else if (glsl_type_is_vector(vt)) {
521 store = substore;
522 offset = suboffset + index;
523 }
524
525 break;
526 }
527
528 case ir_type_dereference_record: {
529 const ir_dereference_record *const dr =
530 (const ir_dereference_record *) deref;
531
532 const ir_dereference *const deref = dr->record->as_dereference();
533 if (!deref)
534 break;
535
536 ir_constant *substore;
537 int suboffset;
538
539 if (!constant_referenced(deref, variable_context, substore, suboffset))
540 break;
541
542 /* Since we're dropping it on the floor...
543 */
544 assert(suboffset == 0);
545
546 store = substore->get_record_field(dr->field_idx);
547 break;
548 }
549
550 case ir_type_dereference_variable: {
551 const ir_dereference_variable *const dv =
552 (const ir_dereference_variable *) deref;
553
554 hash_entry *entry = _mesa_hash_table_search(variable_context, dv->var);
555 if (entry)
556 store = (ir_constant *) entry->data;
557 break;
558 }
559
560 default:
561 assert(!"Should not get here.");
562 break;
563 }
564
565 return store != NULL;
566 }
567
568
569 ir_constant *
constant_expression_value(void *,struct hash_table *)570 ir_rvalue::constant_expression_value(void *, struct hash_table *)
571 {
572 assert(glsl_type_is_error(this->type));
573 return NULL;
574 }
575
576 static uint32_t
bitfield_reverse(uint32_t v)577 bitfield_reverse(uint32_t v)
578 {
579 /* http://graphics.stanford.edu/~seander/bithacks.html#BitReverseObvious */
580 uint32_t r = v; // r will be reversed bits of v; first get LSB of v
581 int s = sizeof(v) * CHAR_BIT - 1; // extra shift needed at end
582
583 for (v >>= 1; v; v >>= 1) {
584 r <<= 1;
585 r |= v & 1;
586 s--;
587 }
588 r <<= s; // shift when v's highest bits are zero
589
590 return r;
591 }
592
593 static int
find_msb_uint(uint32_t v)594 find_msb_uint(uint32_t v)
595 {
596 int count = 0;
597
598 /* If v == 0, then the loop will terminate when count == 32. In that case
599 * 31-count will produce the -1 result required by GLSL findMSB().
600 */
601 while (((v & (1u << 31)) == 0) && count != 32) {
602 count++;
603 v <<= 1;
604 }
605
606 return 31 - count;
607 }
608
609 static int
find_msb_int(int32_t v)610 find_msb_int(int32_t v)
611 {
612 /* If v is signed, findMSB() returns the position of the most significant
613 * zero bit.
614 */
615 return find_msb_uint(v < 0 ? ~v : v);
616 }
617
618 static float
ldexpf_flush_subnormal(float x,int exp)619 ldexpf_flush_subnormal(float x, int exp)
620 {
621 const float result = ldexpf(x, exp);
622
623 /* Flush subnormal values to zero. */
624 return !isnormal(result) ? copysignf(0.0f, x) : result;
625 }
626
627 static double
ldexp_flush_subnormal(double x,int exp)628 ldexp_flush_subnormal(double x, int exp)
629 {
630 const double result = ldexp(x, exp);
631
632 /* Flush subnormal values to zero. */
633 return !isnormal(result) ? copysign(0.0, x) : result;
634 }
635
636 static uint32_t
bitfield_extract_uint(uint32_t value,int offset,int bits)637 bitfield_extract_uint(uint32_t value, int offset, int bits)
638 {
639 if (bits == 0)
640 return 0;
641 else if (offset < 0 || bits < 0)
642 return 0; /* Undefined, per spec. */
643 else if (offset + bits > 32)
644 return 0; /* Undefined, per spec. */
645 else {
646 value <<= 32 - bits - offset;
647 value >>= 32 - bits;
648 return value;
649 }
650 }
651
652 static int32_t
bitfield_extract_int(int32_t value,int offset,int bits)653 bitfield_extract_int(int32_t value, int offset, int bits)
654 {
655 if (bits == 0)
656 return 0;
657 else if (offset < 0 || bits < 0)
658 return 0; /* Undefined, per spec. */
659 else if (offset + bits > 32)
660 return 0; /* Undefined, per spec. */
661 else {
662 value <<= 32 - bits - offset;
663 value >>= 32 - bits;
664 return value;
665 }
666 }
667
668 static uint32_t
bitfield_insert(uint32_t base,uint32_t insert,int offset,int bits)669 bitfield_insert(uint32_t base, uint32_t insert, int offset, int bits)
670 {
671 if (bits == 0)
672 return base;
673 else if (offset < 0 || bits < 0)
674 return 0; /* Undefined, per spec. */
675 else if (offset + bits > 32)
676 return 0; /* Undefined, per spec. */
677 else {
678 unsigned insert_mask = ((1ull << bits) - 1) << offset;
679
680 insert <<= offset;
681 insert &= insert_mask;
682 base &= ~insert_mask;
683
684 return base | insert;
685 }
686 }
687
688 ir_constant *
constant_expression_value(void * mem_ctx,struct hash_table * variable_context)689 ir_expression::constant_expression_value(void *mem_ctx,
690 struct hash_table *variable_context)
691 {
692 assert(mem_ctx);
693
694 if (glsl_type_is_error(this->type))
695 return NULL;
696
697 const glsl_type *return_type = this->type;
698 ir_constant *op[ARRAY_SIZE(this->operands)] = { NULL, };
699 ir_constant_data data;
700
701 memset(&data, 0, sizeof(data));
702
703 for (unsigned operand = 0; operand < this->num_operands; operand++) {
704 op[operand] =
705 this->operands[operand]->constant_expression_value(mem_ctx,
706 variable_context);
707 if (!op[operand])
708 return NULL;
709 }
710
711 for (unsigned operand = 0; operand < this->num_operands; operand++) {
712 switch (op[operand]->type->base_type) {
713 case GLSL_TYPE_FLOAT16: {
714 const struct glsl_type *float_type =
715 glsl_simple_explicit_type(GLSL_TYPE_FLOAT,
716 op[operand]->type->vector_elements,
717 op[operand]->type->matrix_columns,
718 op[operand]->type->explicit_stride,
719 op[operand]->type->interface_row_major,
720 0 /* explicit_alignment */);
721
722 ir_constant_data f;
723 for (unsigned i = 0; i < ARRAY_SIZE(f.f); i++)
724 f.f[i] = _mesa_half_to_float(op[operand]->value.f16[i]);
725
726 op[operand] = new(mem_ctx) ir_constant(float_type, &f);
727 break;
728 }
729 case GLSL_TYPE_INT16: {
730 const struct glsl_type *int_type =
731 glsl_simple_explicit_type(GLSL_TYPE_INT,
732 op[operand]->type->vector_elements,
733 op[operand]->type->matrix_columns,
734 op[operand]->type->explicit_stride,
735 op[operand]->type->interface_row_major,
736 0 /* explicit_alignment */);
737
738 ir_constant_data d;
739 for (unsigned i = 0; i < ARRAY_SIZE(d.i); i++)
740 d.i[i] = op[operand]->value.i16[i];
741
742 op[operand] = new(mem_ctx) ir_constant(int_type, &d);
743 break;
744 }
745 case GLSL_TYPE_UINT16: {
746 const struct glsl_type *uint_type =
747 glsl_simple_explicit_type(GLSL_TYPE_UINT,
748 op[operand]->type->vector_elements,
749 op[operand]->type->matrix_columns,
750 op[operand]->type->explicit_stride,
751 op[operand]->type->interface_row_major,
752 0 /* explicit_alignment */);
753
754 ir_constant_data d;
755 for (unsigned i = 0; i < ARRAY_SIZE(d.u); i++)
756 d.u[i] = op[operand]->value.u16[i];
757
758 op[operand] = new(mem_ctx) ir_constant(uint_type, &d);
759 break;
760 }
761 default:
762 /* nothing to do */
763 break;
764 }
765 }
766
767 switch (return_type->base_type) {
768 case GLSL_TYPE_FLOAT16:
769 return_type = glsl_simple_explicit_type(GLSL_TYPE_FLOAT,
770 return_type->vector_elements,
771 return_type->matrix_columns,
772 return_type->explicit_stride,
773 return_type->interface_row_major,
774 0 /* explicit_alignment */);
775 break;
776 case GLSL_TYPE_INT16:
777 return_type = glsl_simple_explicit_type(GLSL_TYPE_INT,
778 return_type->vector_elements,
779 return_type->matrix_columns,
780 return_type->explicit_stride,
781 return_type->interface_row_major,
782 0 /* explicit_alignment */);
783 break;
784 case GLSL_TYPE_UINT16:
785 return_type = glsl_simple_explicit_type(GLSL_TYPE_UINT,
786 return_type->vector_elements,
787 return_type->matrix_columns,
788 return_type->explicit_stride,
789 return_type->interface_row_major,
790 0 /* explicit_alignment */);
791 break;
792 default:
793 /* nothing to do */
794 break;
795 }
796
797 if (op[1] != NULL)
798 switch (this->operation) {
799 case ir_binop_lshift:
800 case ir_binop_rshift:
801 case ir_binop_ldexp:
802 case ir_binop_interpolate_at_offset:
803 case ir_binop_interpolate_at_sample:
804 case ir_binop_vector_extract:
805 case ir_triop_csel:
806 case ir_triop_bitfield_extract:
807 break;
808
809 default:
810 assert(op[0]->type->base_type == op[1]->type->base_type);
811 break;
812 }
813
814 bool op0_scalar = glsl_type_is_scalar(op[0]->type);
815 bool op1_scalar = op[1] != NULL && glsl_type_is_scalar(op[1]->type);
816
817 /* When iterating over a vector or matrix's components, we want to increase
818 * the loop counter. However, for scalars, we want to stay at 0.
819 */
820 unsigned c0_inc = op0_scalar ? 0 : 1;
821 unsigned c1_inc = op1_scalar ? 0 : 1;
822 unsigned components;
823 if (op1_scalar || !op[1]) {
824 components = glsl_get_components(op[0]->type);
825 } else {
826 components = glsl_get_components(op[1]->type);
827 }
828
829 /* Handle array operations here, rather than below. */
830 if (glsl_type_is_array(op[0]->type)) {
831 assert(op[1] != NULL && glsl_type_is_array(op[1]->type));
832 switch (this->operation) {
833 case ir_binop_all_equal:
834 return new(mem_ctx) ir_constant(op[0]->has_value(op[1]));
835 case ir_binop_any_nequal:
836 return new(mem_ctx) ir_constant(!op[0]->has_value(op[1]));
837 default:
838 break;
839 }
840 return NULL;
841 }
842
843 #include "ir_expression_operation_constant.h"
844
845 switch (type->base_type) {
846 case GLSL_TYPE_FLOAT16: {
847 ir_constant_data f;
848 for (unsigned i = 0; i < ARRAY_SIZE(f.f16); i++)
849 f.f16[i] = _mesa_float_to_half(data.f[i]);
850
851 return new(mem_ctx) ir_constant(this->type, &f);
852 }
853 case GLSL_TYPE_INT16: {
854 ir_constant_data d;
855 for (unsigned i = 0; i < ARRAY_SIZE(d.i16); i++)
856 d.i16[i] = data.i[i];
857
858 return new(mem_ctx) ir_constant(this->type, &d);
859 }
860 case GLSL_TYPE_UINT16: {
861 ir_constant_data d;
862 for (unsigned i = 0; i < ARRAY_SIZE(d.u16); i++)
863 d.u16[i] = data.u[i];
864
865 return new(mem_ctx) ir_constant(this->type, &d);
866 }
867 default:
868 return new(mem_ctx) ir_constant(this->type, &data);
869 }
870 }
871
872
873 ir_constant *
constant_expression_value(void *,struct hash_table *)874 ir_texture::constant_expression_value(void *, struct hash_table *)
875 {
876 /* texture lookups aren't constant expressions */
877 return NULL;
878 }
879
880
881 ir_constant *
constant_expression_value(void * mem_ctx,struct hash_table * variable_context)882 ir_swizzle::constant_expression_value(void *mem_ctx,
883 struct hash_table *variable_context)
884 {
885 assert(mem_ctx);
886
887 ir_constant *v = this->val->constant_expression_value(mem_ctx,
888 variable_context);
889
890 if (v != NULL) {
891 ir_constant_data data = { { 0 } };
892
893 const unsigned swiz_idx[4] = {
894 this->mask.x, this->mask.y, this->mask.z, this->mask.w
895 };
896
897 for (unsigned i = 0; i < this->mask.num_components; i++) {
898 switch (v->type->base_type) {
899 case GLSL_TYPE_UINT16:
900 case GLSL_TYPE_INT16: data.u16[i] = v->value.u16[swiz_idx[i]]; break;
901 case GLSL_TYPE_UINT:
902 case GLSL_TYPE_INT: data.u[i] = v->value.u[swiz_idx[i]]; break;
903 case GLSL_TYPE_FLOAT: data.f[i] = v->value.f[swiz_idx[i]]; break;
904 case GLSL_TYPE_FLOAT16: data.f16[i] = v->value.f16[swiz_idx[i]]; break;
905 case GLSL_TYPE_BOOL: data.b[i] = v->value.b[swiz_idx[i]]; break;
906 case GLSL_TYPE_DOUBLE:data.d[i] = v->value.d[swiz_idx[i]]; break;
907 case GLSL_TYPE_UINT64:data.u64[i] = v->value.u64[swiz_idx[i]]; break;
908 case GLSL_TYPE_INT64: data.i64[i] = v->value.i64[swiz_idx[i]]; break;
909 default: assert(!"Should not get here."); break;
910 }
911 }
912
913 return new(mem_ctx) ir_constant(this->type, &data);
914 }
915 return NULL;
916 }
917
918
919 ir_constant *
constant_expression_value(void * mem_ctx,struct hash_table * variable_context)920 ir_dereference_variable::constant_expression_value(void *mem_ctx,
921 struct hash_table *variable_context)
922 {
923 assert(var);
924 assert(mem_ctx);
925
926 /* Give priority to the context hashtable, if it exists */
927 if (variable_context) {
928 hash_entry *entry = _mesa_hash_table_search(variable_context, var);
929
930 if(entry)
931 return (ir_constant *) entry->data;
932 }
933
934 /* The constant_value of a uniform variable is its initializer,
935 * not the lifetime constant value of the uniform.
936 */
937 if (var->data.mode == ir_var_uniform)
938 return NULL;
939
940 if (!var->constant_value)
941 return NULL;
942
943 return var->constant_value->clone(mem_ctx, NULL);
944 }
945
946
947 ir_constant *
constant_expression_value(void * mem_ctx,struct hash_table * variable_context)948 ir_dereference_array::constant_expression_value(void *mem_ctx,
949 struct hash_table *variable_context)
950 {
951 assert(mem_ctx);
952
953 ir_constant *array = this->array->constant_expression_value(mem_ctx, variable_context);
954 ir_constant *idx = this->array_index->constant_expression_value(mem_ctx, variable_context);
955
956 if ((array != NULL) && (idx != NULL)) {
957 if (glsl_type_is_matrix(array->type)) {
958 /* Array access of a matrix results in a vector.
959 */
960 const unsigned column = idx->value.u[0];
961
962 const glsl_type *const column_type = glsl_get_column_type(array->type);
963
964 /* Section 5.11 (Out-of-Bounds Accesses) of the GLSL 4.60 spec says:
965 *
966 * In the subsections described above for array, vector, matrix and
967 * structure accesses, any out-of-bounds access produced undefined
968 * behavior....Out-of-bounds reads return undefined values, which
969 * include values from other variables of the active program or zero.
970 */
971 if (idx->value.i[0] < 0 || column >= array->type->matrix_columns) {
972 ir_constant_data data = { { 0 } };
973
974 return new(mem_ctx) ir_constant(column_type, &data);
975 }
976
977 /* Offset in the constant matrix to the first element of the column
978 * to be extracted.
979 */
980 const unsigned mat_idx = column * column_type->vector_elements;
981
982 ir_constant_data data = { { 0 } };
983
984 switch (column_type->base_type) {
985 case GLSL_TYPE_FLOAT16:
986 for (unsigned i = 0; i < column_type->vector_elements; i++)
987 data.f16[i] = array->value.f16[mat_idx + i];
988
989 break;
990
991 case GLSL_TYPE_FLOAT:
992 for (unsigned i = 0; i < column_type->vector_elements; i++)
993 data.f[i] = array->value.f[mat_idx + i];
994
995 break;
996
997 case GLSL_TYPE_DOUBLE:
998 for (unsigned i = 0; i < column_type->vector_elements; i++)
999 data.d[i] = array->value.d[mat_idx + i];
1000
1001 break;
1002
1003 default:
1004 unreachable("Matrix types are either float or double.");
1005 }
1006
1007 return new(mem_ctx) ir_constant(column_type, &data);
1008 } else if (glsl_type_is_vector(array->type)) {
1009 const unsigned component = idx->value.u[0];
1010
1011 return new(mem_ctx) ir_constant(array, component);
1012 } else if (glsl_type_is_array(array->type)) {
1013 const unsigned index = idx->value.u[0];
1014 return array->get_array_element(index)->clone(mem_ctx, NULL);
1015 }
1016 }
1017 return NULL;
1018 }
1019
1020
1021 ir_constant *
constant_expression_value(void * mem_ctx,struct hash_table *)1022 ir_dereference_record::constant_expression_value(void *mem_ctx,
1023 struct hash_table *)
1024 {
1025 assert(mem_ctx);
1026
1027 ir_constant *v = this->record->constant_expression_value(mem_ctx);
1028
1029 return (v != NULL) ? v->get_record_field(this->field_idx) : NULL;
1030 }
1031
1032
1033 ir_constant *
constant_expression_value(void *,struct hash_table *)1034 ir_assignment::constant_expression_value(void *, struct hash_table *)
1035 {
1036 /* FINISHME: Handle CEs involving assignment (return RHS) */
1037 return NULL;
1038 }
1039
1040
1041 ir_constant *
constant_expression_value(void *,struct hash_table *)1042 ir_constant::constant_expression_value(void *, struct hash_table *)
1043 {
1044 return this;
1045 }
1046
1047
1048 ir_constant *
constant_expression_value(void * mem_ctx,struct hash_table * variable_context)1049 ir_call::constant_expression_value(void *mem_ctx, struct hash_table *variable_context)
1050 {
1051 assert(mem_ctx);
1052
1053 return this->callee->constant_expression_value(mem_ctx,
1054 &this->actual_parameters,
1055 variable_context);
1056 }
1057
1058
constant_expression_evaluate_expression_list(void * mem_ctx,const struct exec_list & body,struct hash_table * variable_context,ir_constant ** result)1059 bool ir_function_signature::constant_expression_evaluate_expression_list(void *mem_ctx,
1060 const struct exec_list &body,
1061 struct hash_table *variable_context,
1062 ir_constant **result)
1063 {
1064 assert(mem_ctx);
1065
1066 foreach_in_list(ir_instruction, inst, &body) {
1067 switch(inst->ir_type) {
1068
1069 /* (declare () type symbol) */
1070 case ir_type_variable: {
1071 ir_variable *var = inst->as_variable();
1072 _mesa_hash_table_insert(variable_context, var, ir_constant::zero(this, var->type));
1073 break;
1074 }
1075
1076 /* (assign [condition] (write-mask) (ref) (value)) */
1077 case ir_type_assignment: {
1078 ir_assignment *asg = inst->as_assignment();
1079 ir_constant *store = NULL;
1080 int offset = 0;
1081
1082 if (!constant_referenced(asg->lhs, variable_context, store, offset))
1083 return false;
1084
1085 ir_constant *value =
1086 asg->rhs->constant_expression_value(mem_ctx, variable_context);
1087
1088 if (!value)
1089 return false;
1090
1091 store->copy_masked_offset(value, offset, asg->write_mask);
1092 break;
1093 }
1094
1095 /* (return (expression)) */
1096 case ir_type_return:
1097 assert (result);
1098 *result =
1099 inst->as_return()->value->constant_expression_value(mem_ctx,
1100 variable_context);
1101 return *result != NULL;
1102
1103 /* (call name (ref) (params))*/
1104 case ir_type_call: {
1105 ir_call *call = inst->as_call();
1106
1107 /* Just say no to void functions in constant expressions. We
1108 * don't need them at that point.
1109 */
1110
1111 if (!call->return_deref)
1112 return false;
1113
1114 ir_constant *store = NULL;
1115 int offset = 0;
1116
1117 if (!constant_referenced(call->return_deref, variable_context,
1118 store, offset))
1119 return false;
1120
1121 ir_constant *value =
1122 call->constant_expression_value(mem_ctx, variable_context);
1123
1124 if(!value)
1125 return false;
1126
1127 store->copy_offset(value, offset);
1128 break;
1129 }
1130
1131 /* (if condition (then-instructions) (else-instructions)) */
1132 case ir_type_if: {
1133 ir_if *iif = inst->as_if();
1134
1135 ir_constant *cond =
1136 iif->condition->constant_expression_value(mem_ctx,
1137 variable_context);
1138 if (!cond || !glsl_type_is_boolean(cond->type))
1139 return false;
1140
1141 exec_list &branch = cond->get_bool_component(0) ? iif->then_instructions : iif->else_instructions;
1142
1143 *result = NULL;
1144 if (!constant_expression_evaluate_expression_list(mem_ctx, branch,
1145 variable_context,
1146 result))
1147 return false;
1148
1149 /* If there was a return in the branch chosen, drop out now. */
1150 if (*result)
1151 return true;
1152
1153 break;
1154 }
1155
1156 /* Every other expression type, we drop out. */
1157 default:
1158 return false;
1159 }
1160 }
1161
1162 /* Reaching the end of the block is not an error condition */
1163 if (result)
1164 *result = NULL;
1165
1166 return true;
1167 }
1168
1169 ir_constant *
constant_expression_value(void * mem_ctx,exec_list * actual_parameters,struct hash_table * variable_context)1170 ir_function_signature::constant_expression_value(void *mem_ctx,
1171 exec_list *actual_parameters,
1172 struct hash_table *variable_context)
1173 {
1174 assert(mem_ctx);
1175
1176 const glsl_type *type = this->return_type;
1177 if (type == &glsl_type_builtin_void)
1178 return NULL;
1179
1180 /* From the GLSL 1.20 spec, page 23:
1181 * "Function calls to user-defined functions (non-built-in functions)
1182 * cannot be used to form constant expressions."
1183 */
1184 if (!this->is_builtin())
1185 return NULL;
1186
1187 /*
1188 * Of the builtin functions, only the texture lookups and the noise
1189 * ones must not be used in constant expressions. Texture instructions
1190 * include special ir_texture opcodes which can't be constant-folded (see
1191 * ir_texture::constant_expression_value). Noise functions, however, we
1192 * have to special case here.
1193 */
1194 if (strcmp(this->function_name(), "noise1") == 0 ||
1195 strcmp(this->function_name(), "noise2") == 0 ||
1196 strcmp(this->function_name(), "noise3") == 0 ||
1197 strcmp(this->function_name(), "noise4") == 0)
1198 return NULL;
1199
1200 /* Initialize the table of dereferencable names with the function
1201 * parameters. Verify their const-ness on the way.
1202 *
1203 * We expect the correctness of the number of parameters to have
1204 * been checked earlier.
1205 */
1206 hash_table *deref_hash = _mesa_pointer_hash_table_create(NULL);
1207
1208 /* If "origin" is non-NULL, then the function body is there. So we
1209 * have to use the variable objects from the object with the body,
1210 * but the parameter instanciation on the current object.
1211 */
1212 const exec_node *parameter_info = origin ? origin->parameters.get_head_raw() : parameters.get_head_raw();
1213
1214 foreach_in_list(ir_rvalue, n, actual_parameters) {
1215 ir_constant *constant =
1216 n->constant_expression_value(mem_ctx, variable_context);
1217 if (constant == NULL) {
1218 _mesa_hash_table_destroy(deref_hash, NULL);
1219 return NULL;
1220 }
1221
1222
1223 ir_variable *var = (ir_variable *)parameter_info;
1224 _mesa_hash_table_insert(deref_hash, var, constant);
1225
1226 parameter_info = parameter_info->next;
1227 }
1228
1229 ir_constant *result = NULL;
1230
1231 /* Now run the builtin function until something non-constant
1232 * happens or we get the result.
1233 */
1234 if (constant_expression_evaluate_expression_list(mem_ctx, origin ? origin->body : body, deref_hash, &result) &&
1235 result)
1236 result = result->clone(mem_ctx, NULL);
1237
1238 _mesa_hash_table_destroy(deref_hash, NULL);
1239
1240 return result;
1241 }
1242