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