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