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