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