2 * Copyright © 2010 Intel Corporation
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:
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
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.
25 * \file ir_constant_expression.cpp
26 * Evaluate and process constant valued expressions
28 * In GLSL, constant valued expressions are used in several places. These
29 * must be processed and evaluated very early in the compilation process.
32 * * Initializers for uniforms
33 * * Initializers for \c const variables
38 #include "ir_visitor.h"
39 #include "glsl_types.h"
41 #define min(x,y) (x) < (y) ? (x) : (y)
42 #define max(x,y) (x) > (y) ? (x) : (y)
45 ir_expression::constant_expression_value()
47 ir_constant
*op
[2] = { NULL
, NULL
};
48 ir_constant_data data
;
50 memset(&data
, 0, sizeof(data
));
52 for (unsigned operand
= 0; operand
< this->get_num_operands(); operand
++) {
53 op
[operand
] = this->operands
[operand
]->constant_expression_value();
59 assert(op
[0]->type
->base_type
== op
[1]->type
->base_type
);
61 bool op0_scalar
= op
[0]->type
->is_scalar();
62 bool op1_scalar
= op
[1] != NULL
&& op
[1]->type
->is_scalar();
64 /* When iterating over a vector or matrix's components, we want to increase
65 * the loop counter. However, for scalars, we want to stay at 0.
67 unsigned c0_inc
= op0_scalar
? 0 : 1;
68 unsigned c1_inc
= op1_scalar
? 0 : 1;
70 if (op1_scalar
|| !op
[1]) {
71 components
= op
[0]->type
->components();
73 components
= op
[1]->type
->components();
76 switch (this->operation
) {
77 case ir_unop_logic_not
:
78 assert(op
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
79 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++)
80 data
.b
[c
] = !op
[0]->value
.b
[c
];
84 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
85 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
86 data
.i
[c
] = op
[0]->value
.f
[c
];
90 assert(op
[0]->type
->base_type
== GLSL_TYPE_INT
);
91 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
92 data
.f
[c
] = op
[0]->value
.i
[c
];
96 assert(op
[0]->type
->base_type
== GLSL_TYPE_UINT
);
97 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
98 data
.f
[c
] = op
[0]->value
.u
[c
];
102 assert(op
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
103 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
104 data
.f
[c
] = op
[0]->value
.b
[c
] ? 1.0 : 0.0;
108 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
109 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
110 data
.b
[c
] = bool(op
[0]->value
.f
[c
]);
114 assert(op
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
115 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
116 data
.u
[c
] = op
[0]->value
.b
[c
] ? 1 : 0;
120 assert(op
[0]->type
->is_integer());
121 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
122 data
.b
[c
] = bool(op
[0]->value
.u
[c
]);
127 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
128 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
129 data
.f
[c
] = truncf(op
[0]->value
.f
[c
]);
134 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
135 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
136 data
.f
[c
] = ceilf(op
[0]->value
.f
[c
]);
141 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
142 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
143 data
.f
[c
] = floorf(op
[0]->value
.f
[c
]);
148 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
149 switch (this->type
->base_type
) {
156 case GLSL_TYPE_FLOAT
:
157 data
.f
[c
] = op
[0]->value
.f
[c
] - floor(op
[0]->value
.f
[c
]);
166 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
167 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
168 data
.f
[c
] = sinf(op
[0]->value
.f
[c
]);
173 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
174 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
175 data
.f
[c
] = cosf(op
[0]->value
.f
[c
]);
180 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
181 switch (this->type
->base_type
) {
183 data
.u
[c
] = -op
[0]->value
.u
[c
];
186 data
.i
[c
] = -op
[0]->value
.i
[c
];
188 case GLSL_TYPE_FLOAT
:
189 data
.f
[c
] = -op
[0]->value
.f
[c
];
198 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
199 switch (this->type
->base_type
) {
201 data
.u
[c
] = op
[0]->value
.u
[c
];
204 data
.i
[c
] = op
[0]->value
.i
[c
];
206 data
.i
[c
] = -data
.i
[c
];
208 case GLSL_TYPE_FLOAT
:
209 data
.f
[c
] = fabs(op
[0]->value
.f
[c
]);
218 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
219 switch (this->type
->base_type
) {
221 data
.u
[c
] = op
[0]->value
.i
[c
] > 0;
224 data
.i
[c
] = (op
[0]->value
.i
[c
] > 0) - (op
[0]->value
.i
[c
] < 0);
226 case GLSL_TYPE_FLOAT
:
227 data
.f
[c
] = float((op
[0]->value
.f
[c
] > 0)-(op
[0]->value
.f
[c
] < 0));
236 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
237 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
238 switch (this->type
->base_type
) {
240 if (op
[0]->value
.u
[c
] != 0.0)
241 data
.u
[c
] = 1 / op
[0]->value
.u
[c
];
244 if (op
[0]->value
.i
[c
] != 0.0)
245 data
.i
[c
] = 1 / op
[0]->value
.i
[c
];
247 case GLSL_TYPE_FLOAT
:
248 if (op
[0]->value
.f
[c
] != 0.0)
249 data
.f
[c
] = 1.0 / op
[0]->value
.f
[c
];
258 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
259 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
260 data
.f
[c
] = 1.0 / sqrtf(op
[0]->value
.f
[c
]);
265 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
266 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
267 data
.f
[c
] = sqrtf(op
[0]->value
.f
[c
]);
272 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
273 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
274 data
.f
[c
] = expf(op
[0]->value
.f
[c
]);
279 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
280 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
281 data
.f
[c
] = exp2f(op
[0]->value
.f
[c
]);
286 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
287 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
288 data
.f
[c
] = logf(op
[0]->value
.f
[c
]);
293 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
294 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
295 data
.f
[c
] = log2f(op
[0]->value
.f
[c
]);
301 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
302 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
308 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
309 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
310 data
.f
[c
] = powf(op
[0]->value
.f
[c
], op
[1]->value
.f
[c
]);
315 assert(op
[0]->type
->is_vector() && op
[1]->type
->is_vector());
317 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
318 switch (op
[0]->type
->base_type
) {
320 data
.u
[0] += op
[0]->value
.u
[c
] * op
[1]->value
.u
[c
];
323 data
.i
[0] += op
[0]->value
.i
[c
] * op
[1]->value
.i
[c
];
325 case GLSL_TYPE_FLOAT
:
326 data
.f
[0] += op
[0]->value
.f
[c
] * op
[1]->value
.f
[c
];
335 assert(op
[0]->type
== op
[1]->type
|| op0_scalar
|| op1_scalar
);
336 for (unsigned c
= 0, c0
= 0, c1
= 0;
338 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
340 switch (op
[0]->type
->base_type
) {
342 data
.u
[c
] = min(op
[0]->value
.u
[c0
], op
[1]->value
.u
[c1
]);
345 data
.i
[c
] = min(op
[0]->value
.i
[c0
], op
[1]->value
.i
[c1
]);
347 case GLSL_TYPE_FLOAT
:
348 data
.f
[c
] = min(op
[0]->value
.f
[c0
], op
[1]->value
.f
[c1
]);
357 assert(op
[0]->type
== op
[1]->type
|| op0_scalar
|| op1_scalar
);
358 for (unsigned c
= 0, c0
= 0, c1
= 0;
360 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
362 switch (op
[0]->type
->base_type
) {
364 data
.u
[c
] = max(op
[0]->value
.u
[c0
], op
[1]->value
.u
[c1
]);
367 data
.i
[c
] = max(op
[0]->value
.i
[c0
], op
[1]->value
.i
[c1
]);
369 case GLSL_TYPE_FLOAT
:
370 data
.f
[c
] = max(op
[0]->value
.f
[c0
], op
[1]->value
.f
[c1
]);
379 assert(op
[0]->type
== glsl_type::vec3_type
);
380 assert(op
[1]->type
== glsl_type::vec3_type
);
381 data
.f
[0] = (op
[0]->value
.f
[1] * op
[1]->value
.f
[2] -
382 op
[1]->value
.f
[1] * op
[0]->value
.f
[2]);
383 data
.f
[1] = (op
[0]->value
.f
[2] * op
[1]->value
.f
[0] -
384 op
[1]->value
.f
[2] * op
[0]->value
.f
[0]);
385 data
.f
[2] = (op
[0]->value
.f
[0] * op
[1]->value
.f
[1] -
386 op
[1]->value
.f
[0] * op
[0]->value
.f
[1]);
390 assert(op
[0]->type
== op
[1]->type
|| op0_scalar
|| op1_scalar
);
391 for (unsigned c
= 0, c0
= 0, c1
= 0;
393 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
395 switch (op
[0]->type
->base_type
) {
397 data
.u
[c
] = op
[0]->value
.u
[c0
] + op
[1]->value
.u
[c1
];
400 data
.i
[c
] = op
[0]->value
.i
[c0
] + op
[1]->value
.i
[c1
];
402 case GLSL_TYPE_FLOAT
:
403 data
.f
[c
] = op
[0]->value
.f
[c0
] + op
[1]->value
.f
[c1
];
412 assert(op
[0]->type
== op
[1]->type
|| op0_scalar
|| op1_scalar
);
413 for (unsigned c
= 0, c0
= 0, c1
= 0;
415 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
417 switch (op
[0]->type
->base_type
) {
419 data
.u
[c
] = op
[0]->value
.u
[c0
] - op
[1]->value
.u
[c1
];
422 data
.i
[c
] = op
[0]->value
.i
[c0
] - op
[1]->value
.i
[c1
];
424 case GLSL_TYPE_FLOAT
:
425 data
.f
[c
] = op
[0]->value
.f
[c0
] - op
[1]->value
.f
[c1
];
434 /* Check for equal types, or unequal types involving scalars */
435 if ((op
[0]->type
== op
[1]->type
&& !op
[0]->type
->is_matrix())
436 || op0_scalar
|| op1_scalar
) {
437 for (unsigned c
= 0, c0
= 0, c1
= 0;
439 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
441 switch (op
[0]->type
->base_type
) {
443 data
.u
[c
] = op
[0]->value
.u
[c0
] * op
[1]->value
.u
[c1
];
446 data
.i
[c
] = op
[0]->value
.i
[c0
] * op
[1]->value
.i
[c1
];
448 case GLSL_TYPE_FLOAT
:
449 data
.f
[c
] = op
[0]->value
.f
[c0
] * op
[1]->value
.f
[c1
];
456 assert(op
[0]->type
->is_matrix() || op
[1]->type
->is_matrix());
458 /* Multiply an N-by-M matrix with an M-by-P matrix. Since either
459 * matrix can be a GLSL vector, either N or P can be 1.
461 * For vec*mat, the vector is treated as a row vector. This
462 * means the vector is a 1-row x M-column matrix.
464 * For mat*vec, the vector is treated as a column vector. Since
465 * matrix_columns is 1 for vectors, this just works.
467 const unsigned n
= op
[0]->type
->is_vector()
468 ? 1 : op
[0]->type
->vector_elements
;
469 const unsigned m
= op
[1]->type
->vector_elements
;
470 const unsigned p
= op
[1]->type
->matrix_columns
;
471 for (unsigned j
= 0; j
< p
; j
++) {
472 for (unsigned i
= 0; i
< n
; i
++) {
473 for (unsigned k
= 0; k
< m
; k
++) {
474 data
.f
[i
+n
*j
] += op
[0]->value
.f
[i
+n
*k
]*op
[1]->value
.f
[k
+m
*j
];
482 assert(op
[0]->type
== op
[1]->type
|| op0_scalar
|| op1_scalar
);
483 for (unsigned c
= 0, c0
= 0, c1
= 0;
485 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
487 switch (op
[0]->type
->base_type
) {
489 data
.u
[c
] = op
[0]->value
.u
[c0
] / op
[1]->value
.u
[c1
];
492 data
.i
[c
] = op
[0]->value
.i
[c0
] / op
[1]->value
.i
[c1
];
494 case GLSL_TYPE_FLOAT
:
495 data
.f
[c
] = op
[0]->value
.f
[c0
] / op
[1]->value
.f
[c1
];
504 assert(op
[0]->type
== op
[1]->type
|| op0_scalar
|| op1_scalar
);
505 for (unsigned c
= 0, c0
= 0, c1
= 0;
507 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
509 switch (op
[0]->type
->base_type
) {
511 data
.u
[c
] = op
[0]->value
.u
[c0
] % op
[1]->value
.u
[c1
];
514 data
.i
[c
] = op
[0]->value
.i
[c0
] % op
[1]->value
.i
[c1
];
516 case GLSL_TYPE_FLOAT
:
517 /* We don't use fmod because it rounds toward zero; GLSL specifies
520 data
.f
[c
] = (op
[0]->value
.f
[c0
] - op
[1]->value
.f
[c1
])
521 * floorf(op
[0]->value
.f
[c0
] / op
[1]->value
.f
[c1
]);
530 case ir_binop_logic_and
:
531 assert(op
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
532 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++)
533 data
.b
[c
] = op
[0]->value
.b
[c
] && op
[1]->value
.b
[c
];
535 case ir_binop_logic_xor
:
536 assert(op
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
537 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++)
538 data
.b
[c
] = op
[0]->value
.b
[c
] ^ op
[1]->value
.b
[c
];
540 case ir_binop_logic_or
:
541 assert(op
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
542 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++)
543 data
.b
[c
] = op
[0]->value
.b
[c
] || op
[1]->value
.b
[c
];
547 switch (op
[0]->type
->base_type
) {
549 data
.b
[0] = op
[0]->value
.u
[0] < op
[1]->value
.u
[0];
552 data
.b
[0] = op
[0]->value
.i
[0] < op
[1]->value
.i
[0];
554 case GLSL_TYPE_FLOAT
:
555 data
.b
[0] = op
[0]->value
.f
[0] < op
[1]->value
.f
[0];
561 case ir_binop_greater
:
562 switch (op
[0]->type
->base_type
) {
564 data
.b
[0] = op
[0]->value
.u
[0] > op
[1]->value
.u
[0];
567 data
.b
[0] = op
[0]->value
.i
[0] > op
[1]->value
.i
[0];
569 case GLSL_TYPE_FLOAT
:
570 data
.b
[0] = op
[0]->value
.f
[0] > op
[1]->value
.f
[0];
576 case ir_binop_lequal
:
577 switch (op
[0]->type
->base_type
) {
579 data
.b
[0] = op
[0]->value
.u
[0] <= op
[1]->value
.u
[0];
582 data
.b
[0] = op
[0]->value
.i
[0] <= op
[1]->value
.i
[0];
584 case GLSL_TYPE_FLOAT
:
585 data
.b
[0] = op
[0]->value
.f
[0] <= op
[1]->value
.f
[0];
591 case ir_binop_gequal
:
592 switch (op
[0]->type
->base_type
) {
594 data
.b
[0] = op
[0]->value
.u
[0] >= op
[1]->value
.u
[0];
597 data
.b
[0] = op
[0]->value
.i
[0] >= op
[1]->value
.i
[0];
599 case GLSL_TYPE_FLOAT
:
600 data
.b
[0] = op
[0]->value
.f
[0] >= op
[1]->value
.f
[0];
608 data
.b
[0] = op
[0]->has_value(op
[1]);
610 case ir_binop_nequal
:
611 data
.b
[0] = !op
[0]->has_value(op
[1]);
615 /* FINISHME: Should handle all expression types. */
619 void *ctx
= talloc_parent(this);
620 return new(ctx
) ir_constant(this->type
, &data
);
625 ir_texture::constant_expression_value()
627 /* texture lookups aren't constant expressions */
633 ir_swizzle::constant_expression_value()
635 ir_constant
*v
= this->val
->constant_expression_value();
638 ir_constant_data data
;
640 const unsigned swiz_idx
[4] = {
641 this->mask
.x
, this->mask
.y
, this->mask
.z
, this->mask
.w
644 for (unsigned i
= 0; i
< this->mask
.num_components
; i
++) {
645 switch (v
->type
->base_type
) {
647 case GLSL_TYPE_INT
: data
.u
[i
] = v
->value
.u
[swiz_idx
[i
]]; break;
648 case GLSL_TYPE_FLOAT
: data
.f
[i
] = v
->value
.f
[swiz_idx
[i
]]; break;
649 case GLSL_TYPE_BOOL
: data
.b
[i
] = v
->value
.b
[swiz_idx
[i
]]; break;
650 default: assert(!"Should not get here."); break;
654 void *ctx
= talloc_parent(this);
655 return new(ctx
) ir_constant(this->type
, &data
);
662 ir_dereference_variable::constant_expression_value()
664 return var
->constant_value
? var
->constant_value
->clone(NULL
) : NULL
;
669 ir_dereference_array::constant_expression_value()
671 void *ctx
= talloc_parent(this);
672 ir_constant
*array
= this->array
->constant_expression_value();
673 ir_constant
*idx
= this->array_index
->constant_expression_value();
675 if ((array
!= NULL
) && (idx
!= NULL
)) {
676 if (array
->type
->is_matrix()) {
677 /* Array access of a matrix results in a vector.
679 const unsigned column
= idx
->value
.u
[0];
681 const glsl_type
*const column_type
= array
->type
->column_type();
683 /* Offset in the constant matrix to the first element of the column
686 const unsigned mat_idx
= column
* column_type
->vector_elements
;
688 ir_constant_data data
;
690 switch (column_type
->base_type
) {
693 for (unsigned i
= 0; i
< column_type
->vector_elements
; i
++)
694 data
.u
[i
] = array
->value
.u
[mat_idx
+ i
];
698 case GLSL_TYPE_FLOAT
:
699 for (unsigned i
= 0; i
< column_type
->vector_elements
; i
++)
700 data
.f
[i
] = array
->value
.f
[mat_idx
+ i
];
705 assert(!"Should not get here.");
709 return new(ctx
) ir_constant(column_type
, &data
);
710 } else if (array
->type
->is_vector()) {
711 const unsigned component
= idx
->value
.u
[0];
713 return new(ctx
) ir_constant(array
, component
);
715 const unsigned index
= idx
->value
.u
[0];
716 return array
->get_array_element(index
)->clone(NULL
);
724 ir_dereference_record::constant_expression_value()
726 ir_constant
*v
= this->record
->constant_expression_value();
728 return (v
!= NULL
) ? v
->get_record_field(this->field
) : NULL
;
733 ir_assignment::constant_expression_value()
735 /* FINISHME: Handle CEs involving assignment (return RHS) */
741 ir_constant::constant_expression_value()
748 ir_call::constant_expression_value()
750 /* FINISHME: Handle CEs involving builtin function calls. */