glsl: Bitwise conversion operator support in ir_constant_expression.
[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 /* Using C99 rounding functions for roundToEven() implementation is
44 * difficult, because round(), rint, and nearbyint() are affected by
45 * fesetenv(), which the application may have done for its own
46 * purposes. Mesa's IROUND macro is close to what we want, but it
47 * rounds away from 0 on n + 0.5.
48 */
49 static int
50 round_to_even(float val)
51 {
52 int rounded = IROUND(val);
53
54 if (val - floor(val) == 0.5) {
55 if (rounded % 2 != 0)
56 rounded += val > 0 ? -1 : 1;
57 }
58
59 return rounded;
60 }
61
62 static float
63 dot(ir_constant *op0, ir_constant *op1)
64 {
65 assert(op0->type->is_float() && op1->type->is_float());
66
67 float result = 0;
68 for (unsigned c = 0; c < op0->type->components(); c++)
69 result += op0->value.f[c] * op1->value.f[c];
70
71 return result;
72 }
73
74 /* This method is the only one supported by gcc. Unions in particular
75 * are iffy, and read-through-converted-pointer is killed by strict
76 * aliasing. OTOH, the compiler sees through the memcpy, so the
77 * resulting asm is reasonable.
78 */
79 static float
80 bitcast_u2f(unsigned int u)
81 {
82 assert(sizeof(float) == sizeof(unsigned int));
83 float f;
84 memcpy(&f, &u, sizeof(f));
85 return f;
86 }
87
88 static unsigned int
89 bitcast_f2u(float f)
90 {
91 assert(sizeof(float) == sizeof(unsigned int));
92 unsigned int u;
93 memcpy(&u, &f, sizeof(f));
94 return u;
95 }
96
97 ir_constant *
98 ir_rvalue::constant_expression_value(struct hash_table *variable_context)
99 {
100 assert(this->type->is_error());
101 return NULL;
102 }
103
104 ir_constant *
105 ir_expression::constant_expression_value(struct hash_table *variable_context)
106 {
107 if (this->type->is_error())
108 return NULL;
109
110 ir_constant *op[Elements(this->operands)] = { NULL, };
111 ir_constant_data data;
112
113 memset(&data, 0, sizeof(data));
114
115 for (unsigned operand = 0; operand < this->get_num_operands(); operand++) {
116 op[operand] = this->operands[operand]->constant_expression_value(variable_context);
117 if (!op[operand])
118 return NULL;
119 }
120
121 if (op[1] != NULL)
122 assert(op[0]->type->base_type == op[1]->type->base_type ||
123 this->operation == ir_binop_lshift ||
124 this->operation == ir_binop_rshift);
125
126 bool op0_scalar = op[0]->type->is_scalar();
127 bool op1_scalar = op[1] != NULL && op[1]->type->is_scalar();
128
129 /* When iterating over a vector or matrix's components, we want to increase
130 * the loop counter. However, for scalars, we want to stay at 0.
131 */
132 unsigned c0_inc = op0_scalar ? 0 : 1;
133 unsigned c1_inc = op1_scalar ? 0 : 1;
134 unsigned components;
135 if (op1_scalar || !op[1]) {
136 components = op[0]->type->components();
137 } else {
138 components = op[1]->type->components();
139 }
140
141 void *ctx = ralloc_parent(this);
142
143 /* Handle array operations here, rather than below. */
144 if (op[0]->type->is_array()) {
145 assert(op[1] != NULL && op[1]->type->is_array());
146 switch (this->operation) {
147 case ir_binop_all_equal:
148 return new(ctx) ir_constant(op[0]->has_value(op[1]));
149 case ir_binop_any_nequal:
150 return new(ctx) ir_constant(!op[0]->has_value(op[1]));
151 default:
152 break;
153 }
154 return NULL;
155 }
156
157 switch (this->operation) {
158 case ir_unop_bit_not:
159 switch (op[0]->type->base_type) {
160 case GLSL_TYPE_INT:
161 for (unsigned c = 0; c < components; c++)
162 data.i[c] = ~ op[0]->value.i[c];
163 break;
164 case GLSL_TYPE_UINT:
165 for (unsigned c = 0; c < components; c++)
166 data.u[c] = ~ op[0]->value.u[c];
167 break;
168 default:
169 assert(0);
170 }
171 break;
172
173 case ir_unop_logic_not:
174 assert(op[0]->type->base_type == GLSL_TYPE_BOOL);
175 for (unsigned c = 0; c < op[0]->type->components(); c++)
176 data.b[c] = !op[0]->value.b[c];
177 break;
178
179 case ir_unop_f2i:
180 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
181 for (unsigned c = 0; c < op[0]->type->components(); c++) {
182 data.i[c] = (int) op[0]->value.f[c];
183 }
184 break;
185 case ir_unop_i2f:
186 assert(op[0]->type->base_type == GLSL_TYPE_INT);
187 for (unsigned c = 0; c < op[0]->type->components(); c++) {
188 data.f[c] = (float) op[0]->value.i[c];
189 }
190 break;
191 case ir_unop_u2f:
192 assert(op[0]->type->base_type == GLSL_TYPE_UINT);
193 for (unsigned c = 0; c < op[0]->type->components(); c++) {
194 data.f[c] = (float) op[0]->value.u[c];
195 }
196 break;
197 case ir_unop_b2f:
198 assert(op[0]->type->base_type == GLSL_TYPE_BOOL);
199 for (unsigned c = 0; c < op[0]->type->components(); c++) {
200 data.f[c] = op[0]->value.b[c] ? 1.0F : 0.0F;
201 }
202 break;
203 case ir_unop_f2b:
204 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
205 for (unsigned c = 0; c < op[0]->type->components(); c++) {
206 data.b[c] = op[0]->value.f[c] != 0.0F ? true : false;
207 }
208 break;
209 case ir_unop_b2i:
210 assert(op[0]->type->base_type == GLSL_TYPE_BOOL);
211 for (unsigned c = 0; c < op[0]->type->components(); c++) {
212 data.u[c] = op[0]->value.b[c] ? 1 : 0;
213 }
214 break;
215 case ir_unop_i2b:
216 assert(op[0]->type->is_integer());
217 for (unsigned c = 0; c < op[0]->type->components(); c++) {
218 data.b[c] = op[0]->value.u[c] ? true : false;
219 }
220 break;
221 case ir_unop_u2i:
222 assert(op[0]->type->base_type == GLSL_TYPE_UINT);
223 for (unsigned c = 0; c < op[0]->type->components(); c++) {
224 data.i[c] = op[0]->value.u[c];
225 }
226 break;
227 case ir_unop_i2u:
228 assert(op[0]->type->base_type == GLSL_TYPE_INT);
229 for (unsigned c = 0; c < op[0]->type->components(); c++) {
230 data.u[c] = op[0]->value.i[c];
231 }
232 break;
233 case ir_unop_bitcast_i2f:
234 assert(op[0]->type->base_type == GLSL_TYPE_INT);
235 for (unsigned c = 0; c < op[0]->type->components(); c++) {
236 data.f[c] = bitcast_u2f(op[0]->value.i[c]);
237 }
238 break;
239 case ir_unop_bitcast_f2i:
240 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
241 for (unsigned c = 0; c < op[0]->type->components(); c++) {
242 data.i[c] = bitcast_f2u(op[0]->value.f[c]);
243 }
244 break;
245 case ir_unop_bitcast_u2f:
246 assert(op[0]->type->base_type == GLSL_TYPE_UINT);
247 for (unsigned c = 0; c < op[0]->type->components(); c++) {
248 data.f[c] = bitcast_u2f(op[0]->value.u[c]);
249 }
250 break;
251 case ir_unop_bitcast_f2u:
252 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
253 for (unsigned c = 0; c < op[0]->type->components(); c++) {
254 data.u[c] = bitcast_f2u(op[0]->value.f[c]);
255 }
256 break;
257 case ir_unop_any:
258 assert(op[0]->type->is_boolean());
259 data.b[0] = false;
260 for (unsigned c = 0; c < op[0]->type->components(); c++) {
261 if (op[0]->value.b[c])
262 data.b[0] = true;
263 }
264 break;
265
266 case ir_unop_trunc:
267 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
268 for (unsigned c = 0; c < op[0]->type->components(); c++) {
269 data.f[c] = truncf(op[0]->value.f[c]);
270 }
271 break;
272
273 case ir_unop_round_even:
274 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
275 for (unsigned c = 0; c < op[0]->type->components(); c++) {
276 data.f[c] = round_to_even(op[0]->value.f[c]);
277 }
278 break;
279
280 case ir_unop_ceil:
281 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
282 for (unsigned c = 0; c < op[0]->type->components(); c++) {
283 data.f[c] = ceilf(op[0]->value.f[c]);
284 }
285 break;
286
287 case ir_unop_floor:
288 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
289 for (unsigned c = 0; c < op[0]->type->components(); c++) {
290 data.f[c] = floorf(op[0]->value.f[c]);
291 }
292 break;
293
294 case ir_unop_fract:
295 for (unsigned c = 0; c < op[0]->type->components(); c++) {
296 switch (this->type->base_type) {
297 case GLSL_TYPE_UINT:
298 data.u[c] = 0;
299 break;
300 case GLSL_TYPE_INT:
301 data.i[c] = 0;
302 break;
303 case GLSL_TYPE_FLOAT:
304 data.f[c] = op[0]->value.f[c] - floor(op[0]->value.f[c]);
305 break;
306 default:
307 assert(0);
308 }
309 }
310 break;
311
312 case ir_unop_sin:
313 case ir_unop_sin_reduced:
314 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
315 for (unsigned c = 0; c < op[0]->type->components(); c++) {
316 data.f[c] = sinf(op[0]->value.f[c]);
317 }
318 break;
319
320 case ir_unop_cos:
321 case ir_unop_cos_reduced:
322 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
323 for (unsigned c = 0; c < op[0]->type->components(); c++) {
324 data.f[c] = cosf(op[0]->value.f[c]);
325 }
326 break;
327
328 case ir_unop_neg:
329 for (unsigned c = 0; c < op[0]->type->components(); c++) {
330 switch (this->type->base_type) {
331 case GLSL_TYPE_UINT:
332 data.u[c] = -((int) op[0]->value.u[c]);
333 break;
334 case GLSL_TYPE_INT:
335 data.i[c] = -op[0]->value.i[c];
336 break;
337 case GLSL_TYPE_FLOAT:
338 data.f[c] = -op[0]->value.f[c];
339 break;
340 default:
341 assert(0);
342 }
343 }
344 break;
345
346 case ir_unop_abs:
347 for (unsigned c = 0; c < op[0]->type->components(); c++) {
348 switch (this->type->base_type) {
349 case GLSL_TYPE_UINT:
350 data.u[c] = op[0]->value.u[c];
351 break;
352 case GLSL_TYPE_INT:
353 data.i[c] = op[0]->value.i[c];
354 if (data.i[c] < 0)
355 data.i[c] = -data.i[c];
356 break;
357 case GLSL_TYPE_FLOAT:
358 data.f[c] = fabs(op[0]->value.f[c]);
359 break;
360 default:
361 assert(0);
362 }
363 }
364 break;
365
366 case ir_unop_sign:
367 for (unsigned c = 0; c < op[0]->type->components(); c++) {
368 switch (this->type->base_type) {
369 case GLSL_TYPE_UINT:
370 data.u[c] = op[0]->value.i[c] > 0;
371 break;
372 case GLSL_TYPE_INT:
373 data.i[c] = (op[0]->value.i[c] > 0) - (op[0]->value.i[c] < 0);
374 break;
375 case GLSL_TYPE_FLOAT:
376 data.f[c] = float((op[0]->value.f[c] > 0)-(op[0]->value.f[c] < 0));
377 break;
378 default:
379 assert(0);
380 }
381 }
382 break;
383
384 case ir_unop_rcp:
385 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
386 for (unsigned c = 0; c < op[0]->type->components(); c++) {
387 switch (this->type->base_type) {
388 case GLSL_TYPE_UINT:
389 if (op[0]->value.u[c] != 0.0)
390 data.u[c] = 1 / op[0]->value.u[c];
391 break;
392 case GLSL_TYPE_INT:
393 if (op[0]->value.i[c] != 0.0)
394 data.i[c] = 1 / op[0]->value.i[c];
395 break;
396 case GLSL_TYPE_FLOAT:
397 if (op[0]->value.f[c] != 0.0)
398 data.f[c] = 1.0F / op[0]->value.f[c];
399 break;
400 default:
401 assert(0);
402 }
403 }
404 break;
405
406 case ir_unop_rsq:
407 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
408 for (unsigned c = 0; c < op[0]->type->components(); c++) {
409 data.f[c] = 1.0F / sqrtf(op[0]->value.f[c]);
410 }
411 break;
412
413 case ir_unop_sqrt:
414 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
415 for (unsigned c = 0; c < op[0]->type->components(); c++) {
416 data.f[c] = sqrtf(op[0]->value.f[c]);
417 }
418 break;
419
420 case ir_unop_exp:
421 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
422 for (unsigned c = 0; c < op[0]->type->components(); c++) {
423 data.f[c] = expf(op[0]->value.f[c]);
424 }
425 break;
426
427 case ir_unop_exp2:
428 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
429 for (unsigned c = 0; c < op[0]->type->components(); c++) {
430 data.f[c] = exp2f(op[0]->value.f[c]);
431 }
432 break;
433
434 case ir_unop_log:
435 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
436 for (unsigned c = 0; c < op[0]->type->components(); c++) {
437 data.f[c] = logf(op[0]->value.f[c]);
438 }
439 break;
440
441 case ir_unop_log2:
442 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
443 for (unsigned c = 0; c < op[0]->type->components(); c++) {
444 data.f[c] = log2f(op[0]->value.f[c]);
445 }
446 break;
447
448 case ir_unop_dFdx:
449 case ir_unop_dFdy:
450 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
451 for (unsigned c = 0; c < op[0]->type->components(); c++) {
452 data.f[c] = 0.0;
453 }
454 break;
455
456 case ir_binop_pow:
457 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
458 for (unsigned c = 0; c < op[0]->type->components(); c++) {
459 data.f[c] = powf(op[0]->value.f[c], op[1]->value.f[c]);
460 }
461 break;
462
463 case ir_binop_dot:
464 data.f[0] = dot(op[0], op[1]);
465 break;
466
467 case ir_binop_min:
468 assert(op[0]->type == op[1]->type || op0_scalar || op1_scalar);
469 for (unsigned c = 0, c0 = 0, c1 = 0;
470 c < components;
471 c0 += c0_inc, c1 += c1_inc, c++) {
472
473 switch (op[0]->type->base_type) {
474 case GLSL_TYPE_UINT:
475 data.u[c] = MIN2(op[0]->value.u[c0], op[1]->value.u[c1]);
476 break;
477 case GLSL_TYPE_INT:
478 data.i[c] = MIN2(op[0]->value.i[c0], op[1]->value.i[c1]);
479 break;
480 case GLSL_TYPE_FLOAT:
481 data.f[c] = MIN2(op[0]->value.f[c0], op[1]->value.f[c1]);
482 break;
483 default:
484 assert(0);
485 }
486 }
487
488 break;
489 case ir_binop_max:
490 assert(op[0]->type == op[1]->type || op0_scalar || op1_scalar);
491 for (unsigned c = 0, c0 = 0, c1 = 0;
492 c < components;
493 c0 += c0_inc, c1 += c1_inc, c++) {
494
495 switch (op[0]->type->base_type) {
496 case GLSL_TYPE_UINT:
497 data.u[c] = MAX2(op[0]->value.u[c0], op[1]->value.u[c1]);
498 break;
499 case GLSL_TYPE_INT:
500 data.i[c] = MAX2(op[0]->value.i[c0], op[1]->value.i[c1]);
501 break;
502 case GLSL_TYPE_FLOAT:
503 data.f[c] = MAX2(op[0]->value.f[c0], op[1]->value.f[c1]);
504 break;
505 default:
506 assert(0);
507 }
508 }
509 break;
510
511 case ir_binop_add:
512 assert(op[0]->type == op[1]->type || op0_scalar || op1_scalar);
513 for (unsigned c = 0, c0 = 0, c1 = 0;
514 c < components;
515 c0 += c0_inc, c1 += c1_inc, c++) {
516
517 switch (op[0]->type->base_type) {
518 case GLSL_TYPE_UINT:
519 data.u[c] = op[0]->value.u[c0] + op[1]->value.u[c1];
520 break;
521 case GLSL_TYPE_INT:
522 data.i[c] = op[0]->value.i[c0] + op[1]->value.i[c1];
523 break;
524 case GLSL_TYPE_FLOAT:
525 data.f[c] = op[0]->value.f[c0] + op[1]->value.f[c1];
526 break;
527 default:
528 assert(0);
529 }
530 }
531
532 break;
533 case ir_binop_sub:
534 assert(op[0]->type == op[1]->type || op0_scalar || op1_scalar);
535 for (unsigned c = 0, c0 = 0, c1 = 0;
536 c < components;
537 c0 += c0_inc, c1 += c1_inc, c++) {
538
539 switch (op[0]->type->base_type) {
540 case GLSL_TYPE_UINT:
541 data.u[c] = op[0]->value.u[c0] - op[1]->value.u[c1];
542 break;
543 case GLSL_TYPE_INT:
544 data.i[c] = op[0]->value.i[c0] - op[1]->value.i[c1];
545 break;
546 case GLSL_TYPE_FLOAT:
547 data.f[c] = op[0]->value.f[c0] - op[1]->value.f[c1];
548 break;
549 default:
550 assert(0);
551 }
552 }
553
554 break;
555 case ir_binop_mul:
556 /* Check for equal types, or unequal types involving scalars */
557 if ((op[0]->type == op[1]->type && !op[0]->type->is_matrix())
558 || op0_scalar || op1_scalar) {
559 for (unsigned c = 0, c0 = 0, c1 = 0;
560 c < components;
561 c0 += c0_inc, c1 += c1_inc, c++) {
562
563 switch (op[0]->type->base_type) {
564 case GLSL_TYPE_UINT:
565 data.u[c] = op[0]->value.u[c0] * op[1]->value.u[c1];
566 break;
567 case GLSL_TYPE_INT:
568 data.i[c] = op[0]->value.i[c0] * op[1]->value.i[c1];
569 break;
570 case GLSL_TYPE_FLOAT:
571 data.f[c] = op[0]->value.f[c0] * op[1]->value.f[c1];
572 break;
573 default:
574 assert(0);
575 }
576 }
577 } else {
578 assert(op[0]->type->is_matrix() || op[1]->type->is_matrix());
579
580 /* Multiply an N-by-M matrix with an M-by-P matrix. Since either
581 * matrix can be a GLSL vector, either N or P can be 1.
582 *
583 * For vec*mat, the vector is treated as a row vector. This
584 * means the vector is a 1-row x M-column matrix.
585 *
586 * For mat*vec, the vector is treated as a column vector. Since
587 * matrix_columns is 1 for vectors, this just works.
588 */
589 const unsigned n = op[0]->type->is_vector()
590 ? 1 : op[0]->type->vector_elements;
591 const unsigned m = op[1]->type->vector_elements;
592 const unsigned p = op[1]->type->matrix_columns;
593 for (unsigned j = 0; j < p; j++) {
594 for (unsigned i = 0; i < n; i++) {
595 for (unsigned k = 0; k < m; k++) {
596 data.f[i+n*j] += op[0]->value.f[i+n*k]*op[1]->value.f[k+m*j];
597 }
598 }
599 }
600 }
601
602 break;
603 case ir_binop_div:
604 /* FINISHME: Emit warning when division-by-zero is detected. */
605 assert(op[0]->type == op[1]->type || op0_scalar || op1_scalar);
606 for (unsigned c = 0, c0 = 0, c1 = 0;
607 c < components;
608 c0 += c0_inc, c1 += c1_inc, c++) {
609
610 switch (op[0]->type->base_type) {
611 case GLSL_TYPE_UINT:
612 if (op[1]->value.u[c1] == 0) {
613 data.u[c] = 0;
614 } else {
615 data.u[c] = op[0]->value.u[c0] / op[1]->value.u[c1];
616 }
617 break;
618 case GLSL_TYPE_INT:
619 if (op[1]->value.i[c1] == 0) {
620 data.i[c] = 0;
621 } else {
622 data.i[c] = op[0]->value.i[c0] / op[1]->value.i[c1];
623 }
624 break;
625 case GLSL_TYPE_FLOAT:
626 data.f[c] = op[0]->value.f[c0] / op[1]->value.f[c1];
627 break;
628 default:
629 assert(0);
630 }
631 }
632
633 break;
634 case ir_binop_mod:
635 /* FINISHME: Emit warning when division-by-zero is detected. */
636 assert(op[0]->type == op[1]->type || op0_scalar || op1_scalar);
637 for (unsigned c = 0, c0 = 0, c1 = 0;
638 c < components;
639 c0 += c0_inc, c1 += c1_inc, c++) {
640
641 switch (op[0]->type->base_type) {
642 case GLSL_TYPE_UINT:
643 if (op[1]->value.u[c1] == 0) {
644 data.u[c] = 0;
645 } else {
646 data.u[c] = op[0]->value.u[c0] % op[1]->value.u[c1];
647 }
648 break;
649 case GLSL_TYPE_INT:
650 if (op[1]->value.i[c1] == 0) {
651 data.i[c] = 0;
652 } else {
653 data.i[c] = op[0]->value.i[c0] % op[1]->value.i[c1];
654 }
655 break;
656 case GLSL_TYPE_FLOAT:
657 /* We don't use fmod because it rounds toward zero; GLSL specifies
658 * the use of floor.
659 */
660 data.f[c] = op[0]->value.f[c0] - op[1]->value.f[c1]
661 * floorf(op[0]->value.f[c0] / op[1]->value.f[c1]);
662 break;
663 default:
664 assert(0);
665 }
666 }
667
668 break;
669
670 case ir_binop_logic_and:
671 assert(op[0]->type->base_type == GLSL_TYPE_BOOL);
672 for (unsigned c = 0; c < op[0]->type->components(); c++)
673 data.b[c] = op[0]->value.b[c] && op[1]->value.b[c];
674 break;
675 case ir_binop_logic_xor:
676 assert(op[0]->type->base_type == GLSL_TYPE_BOOL);
677 for (unsigned c = 0; c < op[0]->type->components(); c++)
678 data.b[c] = op[0]->value.b[c] ^ op[1]->value.b[c];
679 break;
680 case ir_binop_logic_or:
681 assert(op[0]->type->base_type == GLSL_TYPE_BOOL);
682 for (unsigned c = 0; c < op[0]->type->components(); c++)
683 data.b[c] = op[0]->value.b[c] || op[1]->value.b[c];
684 break;
685
686 case ir_binop_less:
687 assert(op[0]->type == op[1]->type);
688 for (unsigned c = 0; c < op[0]->type->components(); c++) {
689 switch (op[0]->type->base_type) {
690 case GLSL_TYPE_UINT:
691 data.b[c] = op[0]->value.u[c] < op[1]->value.u[c];
692 break;
693 case GLSL_TYPE_INT:
694 data.b[c] = op[0]->value.i[c] < op[1]->value.i[c];
695 break;
696 case GLSL_TYPE_FLOAT:
697 data.b[c] = op[0]->value.f[c] < op[1]->value.f[c];
698 break;
699 default:
700 assert(0);
701 }
702 }
703 break;
704 case ir_binop_greater:
705 assert(op[0]->type == op[1]->type);
706 for (unsigned c = 0; c < op[0]->type->components(); c++) {
707 switch (op[0]->type->base_type) {
708 case GLSL_TYPE_UINT:
709 data.b[c] = op[0]->value.u[c] > op[1]->value.u[c];
710 break;
711 case GLSL_TYPE_INT:
712 data.b[c] = op[0]->value.i[c] > op[1]->value.i[c];
713 break;
714 case GLSL_TYPE_FLOAT:
715 data.b[c] = op[0]->value.f[c] > op[1]->value.f[c];
716 break;
717 default:
718 assert(0);
719 }
720 }
721 break;
722 case ir_binop_lequal:
723 assert(op[0]->type == op[1]->type);
724 for (unsigned c = 0; c < op[0]->type->components(); c++) {
725 switch (op[0]->type->base_type) {
726 case GLSL_TYPE_UINT:
727 data.b[c] = op[0]->value.u[c] <= op[1]->value.u[c];
728 break;
729 case GLSL_TYPE_INT:
730 data.b[c] = op[0]->value.i[c] <= op[1]->value.i[c];
731 break;
732 case GLSL_TYPE_FLOAT:
733 data.b[c] = op[0]->value.f[c] <= op[1]->value.f[c];
734 break;
735 default:
736 assert(0);
737 }
738 }
739 break;
740 case ir_binop_gequal:
741 assert(op[0]->type == op[1]->type);
742 for (unsigned c = 0; c < op[0]->type->components(); c++) {
743 switch (op[0]->type->base_type) {
744 case GLSL_TYPE_UINT:
745 data.b[c] = op[0]->value.u[c] >= op[1]->value.u[c];
746 break;
747 case GLSL_TYPE_INT:
748 data.b[c] = op[0]->value.i[c] >= op[1]->value.i[c];
749 break;
750 case GLSL_TYPE_FLOAT:
751 data.b[c] = op[0]->value.f[c] >= op[1]->value.f[c];
752 break;
753 default:
754 assert(0);
755 }
756 }
757 break;
758 case ir_binop_equal:
759 assert(op[0]->type == op[1]->type);
760 for (unsigned c = 0; c < components; c++) {
761 switch (op[0]->type->base_type) {
762 case GLSL_TYPE_UINT:
763 data.b[c] = op[0]->value.u[c] == op[1]->value.u[c];
764 break;
765 case GLSL_TYPE_INT:
766 data.b[c] = op[0]->value.i[c] == op[1]->value.i[c];
767 break;
768 case GLSL_TYPE_FLOAT:
769 data.b[c] = op[0]->value.f[c] == op[1]->value.f[c];
770 break;
771 case GLSL_TYPE_BOOL:
772 data.b[c] = op[0]->value.b[c] == op[1]->value.b[c];
773 break;
774 default:
775 assert(0);
776 }
777 }
778 break;
779 case ir_binop_nequal:
780 assert(op[0]->type == op[1]->type);
781 for (unsigned c = 0; c < components; c++) {
782 switch (op[0]->type->base_type) {
783 case GLSL_TYPE_UINT:
784 data.b[c] = op[0]->value.u[c] != op[1]->value.u[c];
785 break;
786 case GLSL_TYPE_INT:
787 data.b[c] = op[0]->value.i[c] != op[1]->value.i[c];
788 break;
789 case GLSL_TYPE_FLOAT:
790 data.b[c] = op[0]->value.f[c] != op[1]->value.f[c];
791 break;
792 case GLSL_TYPE_BOOL:
793 data.b[c] = op[0]->value.b[c] != op[1]->value.b[c];
794 break;
795 default:
796 assert(0);
797 }
798 }
799 break;
800 case ir_binop_all_equal:
801 data.b[0] = op[0]->has_value(op[1]);
802 break;
803 case ir_binop_any_nequal:
804 data.b[0] = !op[0]->has_value(op[1]);
805 break;
806
807 case ir_binop_lshift:
808 for (unsigned c = 0, c0 = 0, c1 = 0;
809 c < components;
810 c0 += c0_inc, c1 += c1_inc, c++) {
811
812 if (op[0]->type->base_type == GLSL_TYPE_INT &&
813 op[1]->type->base_type == GLSL_TYPE_INT) {
814 data.i[c] = op[0]->value.i[c0] << op[1]->value.i[c1];
815
816 } else if (op[0]->type->base_type == GLSL_TYPE_INT &&
817 op[1]->type->base_type == GLSL_TYPE_UINT) {
818 data.i[c] = op[0]->value.i[c0] << op[1]->value.u[c1];
819
820 } else if (op[0]->type->base_type == GLSL_TYPE_UINT &&
821 op[1]->type->base_type == GLSL_TYPE_INT) {
822 data.u[c] = op[0]->value.u[c0] << op[1]->value.i[c1];
823
824 } else if (op[0]->type->base_type == GLSL_TYPE_UINT &&
825 op[1]->type->base_type == GLSL_TYPE_UINT) {
826 data.u[c] = op[0]->value.u[c0] << op[1]->value.u[c1];
827 }
828 }
829 break;
830
831 case ir_binop_rshift:
832 for (unsigned c = 0, c0 = 0, c1 = 0;
833 c < components;
834 c0 += c0_inc, c1 += c1_inc, c++) {
835
836 if (op[0]->type->base_type == GLSL_TYPE_INT &&
837 op[1]->type->base_type == GLSL_TYPE_INT) {
838 data.i[c] = op[0]->value.i[c0] >> op[1]->value.i[c1];
839
840 } else if (op[0]->type->base_type == GLSL_TYPE_INT &&
841 op[1]->type->base_type == GLSL_TYPE_UINT) {
842 data.i[c] = op[0]->value.i[c0] >> op[1]->value.u[c1];
843
844 } else if (op[0]->type->base_type == GLSL_TYPE_UINT &&
845 op[1]->type->base_type == GLSL_TYPE_INT) {
846 data.u[c] = op[0]->value.u[c0] >> op[1]->value.i[c1];
847
848 } else if (op[0]->type->base_type == GLSL_TYPE_UINT &&
849 op[1]->type->base_type == GLSL_TYPE_UINT) {
850 data.u[c] = op[0]->value.u[c0] >> op[1]->value.u[c1];
851 }
852 }
853 break;
854
855 case ir_binop_bit_and:
856 for (unsigned c = 0, c0 = 0, c1 = 0;
857 c < components;
858 c0 += c0_inc, c1 += c1_inc, c++) {
859
860 switch (op[0]->type->base_type) {
861 case GLSL_TYPE_INT:
862 data.i[c] = op[0]->value.i[c0] & op[1]->value.i[c1];
863 break;
864 case GLSL_TYPE_UINT:
865 data.u[c] = op[0]->value.u[c0] & op[1]->value.u[c1];
866 break;
867 default:
868 assert(0);
869 }
870 }
871 break;
872
873 case ir_binop_bit_or:
874 for (unsigned c = 0, c0 = 0, c1 = 0;
875 c < components;
876 c0 += c0_inc, c1 += c1_inc, c++) {
877
878 switch (op[0]->type->base_type) {
879 case GLSL_TYPE_INT:
880 data.i[c] = op[0]->value.i[c0] | op[1]->value.i[c1];
881 break;
882 case GLSL_TYPE_UINT:
883 data.u[c] = op[0]->value.u[c0] | op[1]->value.u[c1];
884 break;
885 default:
886 assert(0);
887 }
888 }
889 break;
890
891 case ir_binop_bit_xor:
892 for (unsigned c = 0, c0 = 0, c1 = 0;
893 c < components;
894 c0 += c0_inc, c1 += c1_inc, c++) {
895
896 switch (op[0]->type->base_type) {
897 case GLSL_TYPE_INT:
898 data.i[c] = op[0]->value.i[c0] ^ op[1]->value.i[c1];
899 break;
900 case GLSL_TYPE_UINT:
901 data.u[c] = op[0]->value.u[c0] ^ op[1]->value.u[c1];
902 break;
903 default:
904 assert(0);
905 }
906 }
907 break;
908
909 case ir_quadop_vector:
910 for (unsigned c = 0; c < this->type->vector_elements; c++) {
911 switch (this->type->base_type) {
912 case GLSL_TYPE_INT:
913 data.i[c] = op[c]->value.i[0];
914 break;
915 case GLSL_TYPE_UINT:
916 data.u[c] = op[c]->value.u[0];
917 break;
918 case GLSL_TYPE_FLOAT:
919 data.f[c] = op[c]->value.f[0];
920 break;
921 default:
922 assert(0);
923 }
924 }
925 break;
926
927 default:
928 /* FINISHME: Should handle all expression types. */
929 return NULL;
930 }
931
932 return new(ctx) ir_constant(this->type, &data);
933 }
934
935
936 ir_constant *
937 ir_texture::constant_expression_value(struct hash_table *variable_context)
938 {
939 /* texture lookups aren't constant expressions */
940 return NULL;
941 }
942
943
944 ir_constant *
945 ir_swizzle::constant_expression_value(struct hash_table *variable_context)
946 {
947 ir_constant *v = this->val->constant_expression_value(variable_context);
948
949 if (v != NULL) {
950 ir_constant_data data = { { 0 } };
951
952 const unsigned swiz_idx[4] = {
953 this->mask.x, this->mask.y, this->mask.z, this->mask.w
954 };
955
956 for (unsigned i = 0; i < this->mask.num_components; i++) {
957 switch (v->type->base_type) {
958 case GLSL_TYPE_UINT:
959 case GLSL_TYPE_INT: data.u[i] = v->value.u[swiz_idx[i]]; break;
960 case GLSL_TYPE_FLOAT: data.f[i] = v->value.f[swiz_idx[i]]; break;
961 case GLSL_TYPE_BOOL: data.b[i] = v->value.b[swiz_idx[i]]; break;
962 default: assert(!"Should not get here."); break;
963 }
964 }
965
966 void *ctx = ralloc_parent(this);
967 return new(ctx) ir_constant(this->type, &data);
968 }
969 return NULL;
970 }
971
972
973 void
974 ir_dereference_variable::constant_referenced(struct hash_table *variable_context,
975 ir_constant *&store, int &offset) const
976 {
977 if (variable_context) {
978 store = (ir_constant *)hash_table_find(variable_context, var);
979 offset = 0;
980 } else {
981 store = NULL;
982 offset = 0;
983 }
984 }
985
986 ir_constant *
987 ir_dereference_variable::constant_expression_value(struct hash_table *variable_context)
988 {
989 /* This may occur during compile and var->type is glsl_type::error_type */
990 if (!var)
991 return NULL;
992
993 /* Give priority to the context hashtable, if it exists */
994 if (variable_context) {
995 ir_constant *value = (ir_constant *)hash_table_find(variable_context, var);
996 if(value)
997 return value;
998 }
999
1000 /* The constant_value of a uniform variable is its initializer,
1001 * not the lifetime constant value of the uniform.
1002 */
1003 if (var->mode == ir_var_uniform)
1004 return NULL;
1005
1006 if (!var->constant_value)
1007 return NULL;
1008
1009 return var->constant_value->clone(ralloc_parent(var), NULL);
1010 }
1011
1012
1013 void
1014 ir_dereference_array::constant_referenced(struct hash_table *variable_context,
1015 ir_constant *&store, int &offset) const
1016 {
1017 ir_constant *index_c = array_index->constant_expression_value(variable_context);
1018
1019 if (!index_c || !index_c->type->is_scalar() || !index_c->type->is_integer()) {
1020 store = 0;
1021 offset = 0;
1022 return;
1023 }
1024
1025 int index = index_c->type->base_type == GLSL_TYPE_INT ?
1026 index_c->get_int_component(0) :
1027 index_c->get_uint_component(0);
1028
1029 ir_constant *substore;
1030 int suboffset;
1031 const ir_dereference *deref = array->as_dereference();
1032 if (!deref) {
1033 store = 0;
1034 offset = 0;
1035 return;
1036 }
1037
1038 deref->constant_referenced(variable_context, substore, suboffset);
1039
1040 if (!substore) {
1041 store = 0;
1042 offset = 0;
1043 return;
1044 }
1045
1046 const glsl_type *vt = substore->type;
1047 if (vt->is_array()) {
1048 store = substore->get_array_element(index);
1049 offset = 0;
1050 return;
1051 }
1052 if (vt->is_matrix()) {
1053 store = substore;
1054 offset = index * vt->vector_elements;
1055 return;
1056 }
1057 if (vt->is_vector()) {
1058 store = substore;
1059 offset = suboffset + index;
1060 return;
1061 }
1062
1063 store = 0;
1064 offset = 0;
1065 }
1066
1067 ir_constant *
1068 ir_dereference_array::constant_expression_value(struct hash_table *variable_context)
1069 {
1070 ir_constant *array = this->array->constant_expression_value(variable_context);
1071 ir_constant *idx = this->array_index->constant_expression_value(variable_context);
1072
1073 if ((array != NULL) && (idx != NULL)) {
1074 void *ctx = ralloc_parent(this);
1075 if (array->type->is_matrix()) {
1076 /* Array access of a matrix results in a vector.
1077 */
1078 const unsigned column = idx->value.u[0];
1079
1080 const glsl_type *const column_type = array->type->column_type();
1081
1082 /* Offset in the constant matrix to the first element of the column
1083 * to be extracted.
1084 */
1085 const unsigned mat_idx = column * column_type->vector_elements;
1086
1087 ir_constant_data data = { { 0 } };
1088
1089 switch (column_type->base_type) {
1090 case GLSL_TYPE_UINT:
1091 case GLSL_TYPE_INT:
1092 for (unsigned i = 0; i < column_type->vector_elements; i++)
1093 data.u[i] = array->value.u[mat_idx + i];
1094
1095 break;
1096
1097 case GLSL_TYPE_FLOAT:
1098 for (unsigned i = 0; i < column_type->vector_elements; i++)
1099 data.f[i] = array->value.f[mat_idx + i];
1100
1101 break;
1102
1103 default:
1104 assert(!"Should not get here.");
1105 break;
1106 }
1107
1108 return new(ctx) ir_constant(column_type, &data);
1109 } else if (array->type->is_vector()) {
1110 const unsigned component = idx->value.u[0];
1111
1112 return new(ctx) ir_constant(array, component);
1113 } else {
1114 const unsigned index = idx->value.u[0];
1115 return array->get_array_element(index)->clone(ctx, NULL);
1116 }
1117 }
1118 return NULL;
1119 }
1120
1121
1122 void
1123 ir_dereference_record::constant_referenced(struct hash_table *variable_context,
1124 ir_constant *&store, int &offset) const
1125 {
1126 ir_constant *substore;
1127 int suboffset;
1128 const ir_dereference *deref = record->as_dereference();
1129 if (!deref) {
1130 store = 0;
1131 offset = 0;
1132 return;
1133 }
1134
1135 deref->constant_referenced(variable_context, substore, suboffset);
1136
1137 if (!substore) {
1138 store = 0;
1139 offset = 0;
1140 return;
1141 }
1142
1143 store = substore->get_record_field(field);
1144 offset = 0;
1145 }
1146
1147 ir_constant *
1148 ir_dereference_record::constant_expression_value(struct hash_table *variable_context)
1149 {
1150 ir_constant *v = this->record->constant_expression_value();
1151
1152 return (v != NULL) ? v->get_record_field(this->field) : NULL;
1153 }
1154
1155
1156 ir_constant *
1157 ir_assignment::constant_expression_value(struct hash_table *variable_context)
1158 {
1159 /* FINISHME: Handle CEs involving assignment (return RHS) */
1160 return NULL;
1161 }
1162
1163
1164 ir_constant *
1165 ir_constant::constant_expression_value(struct hash_table *variable_context)
1166 {
1167 return this;
1168 }
1169
1170
1171 ir_constant *
1172 ir_call::constant_expression_value(struct hash_table *variable_context)
1173 {
1174 return this->callee->constant_expression_value(&this->actual_parameters, variable_context);
1175 }
1176
1177
1178 bool ir_function_signature::constant_expression_evaluate_expression_list(const struct exec_list &body,
1179 struct hash_table *variable_context,
1180 ir_constant **result)
1181 {
1182 foreach_list(n, &body) {
1183 ir_instruction *inst = (ir_instruction *)n;
1184 switch(inst->ir_type) {
1185
1186 /* (declare () type symbol) */
1187 case ir_type_variable: {
1188 ir_variable *var = inst->as_variable();
1189 hash_table_insert(variable_context, ir_constant::zero(this, var->type), var);
1190 break;
1191 }
1192
1193 /* (assign [condition] (write-mask) (ref) (value)) */
1194 case ir_type_assignment: {
1195 ir_assignment *asg = inst->as_assignment();
1196 if (asg->condition) {
1197 ir_constant *cond = asg->condition->constant_expression_value(variable_context);
1198 if (!cond)
1199 return false;
1200 if (!cond->get_bool_component(0))
1201 break;
1202 }
1203
1204 ir_constant *store = NULL;
1205 int offset = 0;
1206 asg->lhs->constant_referenced(variable_context, store, offset);
1207
1208 if (!store)
1209 return false;
1210
1211 ir_constant *value = asg->rhs->constant_expression_value(variable_context);
1212
1213 if (!value)
1214 return false;
1215
1216 store->copy_masked_offset(value, offset, asg->write_mask);
1217 break;
1218 }
1219
1220 /* (return (expression)) */
1221 case ir_type_return:
1222 assert (result);
1223 *result = inst->as_return()->value->constant_expression_value(variable_context);
1224 return *result != NULL;
1225
1226 /* (call name (ref) (params))*/
1227 case ir_type_call: {
1228 ir_call *call = inst->as_call();
1229
1230 /* Just say no to void functions in constant expressions. We
1231 * don't need them at that point.
1232 */
1233
1234 if (!call->return_deref)
1235 return false;
1236
1237 ir_constant *store = NULL;
1238 int offset = 0;
1239 call->return_deref->constant_referenced(variable_context, store, offset);
1240
1241 if (!store)
1242 return false;
1243
1244 ir_constant *value = call->constant_expression_value(variable_context);
1245
1246 if(!value)
1247 return false;
1248
1249 store->copy_offset(value, offset);
1250 break;
1251 }
1252
1253 /* (if condition (then-instructions) (else-instructions)) */
1254 case ir_type_if: {
1255 ir_if *iif = inst->as_if();
1256
1257 ir_constant *cond = iif->condition->constant_expression_value(variable_context);
1258 if (!cond || !cond->type->is_boolean())
1259 return false;
1260
1261 exec_list &branch = cond->get_bool_component(0) ? iif->then_instructions : iif->else_instructions;
1262
1263 *result = NULL;
1264 if (!constant_expression_evaluate_expression_list(branch, variable_context, result))
1265 return false;
1266
1267 /* If there was a return in the branch chosen, drop out now. */
1268 if (*result)
1269 return true;
1270
1271 break;
1272 }
1273
1274 /* Every other expression type, we drop out. */
1275 default:
1276 return false;
1277 }
1278 }
1279
1280 /* Reaching the end of the block is not an error condition */
1281 if (result)
1282 *result = NULL;
1283
1284 return true;
1285 }
1286
1287 ir_constant *
1288 ir_function_signature::constant_expression_value(exec_list *actual_parameters, struct hash_table *variable_context)
1289 {
1290 const glsl_type *type = this->return_type;
1291 if (type == glsl_type::void_type)
1292 return NULL;
1293
1294 /* From the GLSL 1.20 spec, page 23:
1295 * "Function calls to user-defined functions (non-built-in functions)
1296 * cannot be used to form constant expressions."
1297 */
1298 if (!this->is_builtin)
1299 return NULL;
1300
1301 /*
1302 * Of the builtin functions, only the texture lookups and the noise
1303 * ones must not be used in constant expressions. They all include
1304 * specific opcodes so they don't need to be special-cased at this
1305 * point.
1306 */
1307
1308 /* Initialize the table of dereferencable names with the function
1309 * parameters. Verify their const-ness on the way.
1310 *
1311 * We expect the correctness of the number of parameters to have
1312 * been checked earlier.
1313 */
1314 hash_table *deref_hash = hash_table_ctor(8, hash_table_pointer_hash,
1315 hash_table_pointer_compare);
1316
1317 /* If "origin" is non-NULL, then the function body is there. So we
1318 * have to use the variable objects from the object with the body,
1319 * but the parameter instanciation on the current object.
1320 */
1321 const exec_node *parameter_info = origin ? origin->parameters.head : parameters.head;
1322
1323 foreach_list(n, actual_parameters) {
1324 ir_constant *constant = ((ir_rvalue *) n)->constant_expression_value(variable_context);
1325 if (constant == NULL)
1326 return NULL;
1327
1328 ir_variable *var = (ir_variable *)parameter_info;
1329 hash_table_insert(deref_hash, constant, var);
1330
1331 parameter_info = parameter_info->next;
1332 }
1333
1334 ir_constant *result = NULL;
1335
1336 /* Now run the builtin function until something non-constant
1337 * happens or we get the result.
1338 */
1339 if (constant_expression_evaluate_expression_list(origin ? origin->body : body, deref_hash, &result) && result)
1340 result = result->clone(ralloc_parent(this), NULL);
1341
1342 hash_table_dtor(deref_hash);
1343
1344 return result;
1345 }