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