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"
42 * Visitor class for evaluating constant expressions
44 class ir_constant_visitor
: public ir_visitor
{
52 virtual ~ir_constant_visitor()
60 * As typical for the visitor pattern, there must be one \c visit method for
61 * each concrete subclass of \c ir_instruction. Virtual base classes within
62 * the hierarchy should not have \c visit methods.
65 virtual void visit(ir_variable
*);
66 virtual void visit(ir_function_signature
*);
67 virtual void visit(ir_function
*);
68 virtual void visit(ir_expression
*);
69 virtual void visit(ir_texture
*);
70 virtual void visit(ir_swizzle
*);
71 virtual void visit(ir_dereference_variable
*);
72 virtual void visit(ir_dereference_array
*);
73 virtual void visit(ir_dereference_record
*);
74 virtual void visit(ir_assignment
*);
75 virtual void visit(ir_constant
*);
76 virtual void visit(ir_call
*);
77 virtual void visit(ir_return
*);
78 virtual void visit(ir_discard
*);
79 virtual void visit(ir_if
*);
80 virtual void visit(ir_loop
*);
81 virtual void visit(ir_loop_jump
*);
85 * Value of the constant expression.
88 * This field will be \c NULL if the expression is not constant valued.
90 /* FINIHSME: This cannot hold values for constant arrays or structures. */
96 ir_instruction::constant_expression_value()
98 ir_constant_visitor visitor
;
100 this->accept(& visitor
);
101 return visitor
.value
;
106 ir_constant_visitor::visit(ir_variable
*ir
)
114 ir_constant_visitor::visit(ir_function_signature
*ir
)
122 ir_constant_visitor::visit(ir_function
*ir
)
129 ir_constant_visitor::visit(ir_expression
*ir
)
133 unsigned int operand
, c
;
134 ir_constant_data data
;
136 memset(&data
, 0, sizeof(data
));
138 for (operand
= 0; operand
< ir
->get_num_operands(); operand
++) {
139 op
[operand
] = ir
->operands
[operand
]->constant_expression_value();
144 switch (ir
->operation
) {
145 case ir_unop_logic_not
:
146 assert(op
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
147 for (c
= 0; c
< ir
->operands
[0]->type
->components(); c
++)
148 data
.b
[c
] = !op
[0]->value
.b
[c
];
152 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
153 for (c
= 0; c
< ir
->operands
[0]->type
->components(); c
++) {
154 data
.i
[c
] = op
[0]->value
.f
[c
];
158 assert(op
[0]->type
->base_type
== GLSL_TYPE_UINT
||
159 op
[0]->type
->base_type
== GLSL_TYPE_INT
);
160 for (c
= 0; c
< ir
->operands
[0]->type
->components(); c
++) {
161 if (op
[0]->type
->base_type
== GLSL_TYPE_INT
)
162 data
.f
[c
] = op
[0]->value
.i
[c
];
164 data
.f
[c
] = op
[0]->value
.u
[c
];
168 assert(op
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
169 for (c
= 0; c
< ir
->operands
[0]->type
->components(); c
++) {
170 data
.f
[c
] = op
[0]->value
.b
[c
] ? 1.0 : 0.0;
174 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
175 for (c
= 0; c
< ir
->operands
[0]->type
->components(); c
++) {
176 data
.b
[c
] = bool(op
[0]->value
.f
[c
]);
180 assert(op
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
181 for (c
= 0; c
< ir
->operands
[0]->type
->components(); c
++) {
182 data
.u
[c
] = op
[0]->value
.b
[c
] ? 1 : 0;
186 assert(op
[0]->type
->is_integer());
187 for (c
= 0; c
< ir
->operands
[0]->type
->components(); c
++) {
188 data
.b
[c
] = bool(op
[0]->value
.u
[c
]);
193 for (c
= 0; c
< ir
->operands
[0]->type
->components(); c
++) {
194 switch (ir
->type
->base_type
) {
201 case GLSL_TYPE_FLOAT
:
202 data
.f
[c
] = op
[0]->value
.f
[c
] - floor(op
[0]->value
.f
[c
]);
211 for (c
= 0; c
< ir
->operands
[0]->type
->components(); c
++) {
212 switch (ir
->type
->base_type
) {
214 data
.u
[c
] = -op
[0]->value
.u
[c
];
217 data
.i
[c
] = -op
[0]->value
.i
[c
];
219 case GLSL_TYPE_FLOAT
:
220 data
.f
[c
] = -op
[0]->value
.f
[c
];
229 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
230 for (c
= 0; c
< ir
->operands
[0]->type
->components(); c
++) {
231 switch (ir
->type
->base_type
) {
233 data
.u
[c
] = op
[0]->value
.u
[c
];
236 data
.i
[c
] = op
[0]->value
.i
[c
];
238 data
.i
[c
] = -data
.i
[c
];
240 case GLSL_TYPE_FLOAT
:
241 data
.f
[c
] = fabs(op
[0]->value
.f
[c
]);
250 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
251 for (c
= 0; c
< ir
->operands
[0]->type
->components(); c
++) {
252 switch (ir
->type
->base_type
) {
254 if (op
[0]->value
.u
[c
] != 0.0)
255 data
.u
[c
] = 1 / op
[0]->value
.u
[c
];
258 if (op
[0]->value
.i
[c
] != 0.0)
259 data
.i
[c
] = 1 / op
[0]->value
.i
[c
];
261 case GLSL_TYPE_FLOAT
:
262 if (op
[0]->value
.f
[c
] != 0.0)
263 data
.f
[c
] = 1.0 / op
[0]->value
.f
[c
];
272 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
273 for (c
= 0; c
< ir
->operands
[0]->type
->components(); c
++) {
274 data
.f
[c
] = 1.0 / sqrtf(op
[0]->value
.f
[c
]);
279 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
280 for (c
= 0; c
< ir
->operands
[0]->type
->components(); c
++) {
281 data
.f
[c
] = sqrtf(op
[0]->value
.f
[c
]);
286 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
287 for (c
= 0; c
< ir
->operands
[0]->type
->components(); c
++) {
288 data
.f
[c
] = expf(op
[0]->value
.f
[c
]);
293 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
294 for (c
= 0; c
< ir
->operands
[0]->type
->components(); c
++) {
295 data
.f
[c
] = logf(op
[0]->value
.f
[c
]);
301 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
302 for (c
= 0; c
< ir
->operands
[0]->type
->components(); c
++) {
308 if (ir
->operands
[0]->type
== ir
->operands
[1]->type
) {
309 for (c
= 0; c
< ir
->operands
[0]->type
->components(); c
++) {
310 switch (ir
->operands
[0]->type
->base_type
) {
312 data
.u
[c
] = op
[0]->value
.u
[c
] + op
[1]->value
.u
[c
];
315 data
.i
[c
] = op
[0]->value
.i
[c
] + op
[1]->value
.i
[c
];
317 case GLSL_TYPE_FLOAT
:
318 data
.f
[c
] = op
[0]->value
.f
[c
] + op
[1]->value
.f
[c
];
325 /* FINISHME: Support operations with non-equal types. */
330 if (ir
->operands
[0]->type
== ir
->operands
[1]->type
) {
331 for (c
= 0; c
< ir
->operands
[0]->type
->components(); c
++) {
332 switch (ir
->operands
[0]->type
->base_type
) {
334 data
.u
[c
] = op
[0]->value
.u
[c
] - op
[1]->value
.u
[c
];
337 data
.i
[c
] = op
[0]->value
.i
[c
] - op
[1]->value
.i
[c
];
339 case GLSL_TYPE_FLOAT
:
340 data
.f
[c
] = op
[0]->value
.f
[c
] - op
[1]->value
.f
[c
];
347 /* FINISHME: Support operations with non-equal types. */
352 if (ir
->operands
[0]->type
== ir
->operands
[1]->type
&&
353 !ir
->operands
[0]->type
->is_matrix()) {
354 for (c
= 0; c
< ir
->operands
[0]->type
->components(); c
++) {
355 switch (ir
->operands
[0]->type
->base_type
) {
357 data
.u
[c
] = op
[0]->value
.u
[c
] * op
[1]->value
.u
[c
];
360 data
.i
[c
] = op
[0]->value
.i
[c
] * op
[1]->value
.i
[c
];
362 case GLSL_TYPE_FLOAT
:
363 data
.f
[c
] = op
[0]->value
.f
[c
] * op
[1]->value
.f
[c
];
370 /* FINISHME: Support operations with non-equal types. */
375 if (ir
->operands
[0]->type
== ir
->operands
[1]->type
) {
376 for (c
= 0; c
< ir
->operands
[0]->type
->components(); c
++) {
377 switch (ir
->operands
[0]->type
->base_type
) {
379 data
.u
[c
] = op
[0]->value
.u
[c
] / op
[1]->value
.u
[c
];
382 data
.i
[c
] = op
[0]->value
.i
[c
] / op
[1]->value
.i
[c
];
384 case GLSL_TYPE_FLOAT
:
385 data
.f
[c
] = op
[0]->value
.f
[c
] / op
[1]->value
.f
[c
];
392 /* FINISHME: Support operations with non-equal types. */
396 case ir_binop_logic_and
:
397 assert(op
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
398 for (c
= 0; c
< ir
->operands
[0]->type
->components(); c
++)
399 data
.b
[c
] = op
[0]->value
.b
[c
] && op
[1]->value
.b
[c
];
401 case ir_binop_logic_xor
:
402 assert(op
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
403 for (c
= 0; c
< ir
->operands
[0]->type
->components(); c
++)
404 data
.b
[c
] = op
[0]->value
.b
[c
] ^ op
[1]->value
.b
[c
];
406 case ir_binop_logic_or
:
407 assert(op
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
408 for (c
= 0; c
< ir
->operands
[0]->type
->components(); c
++)
409 data
.b
[c
] = op
[0]->value
.b
[c
] || op
[1]->value
.b
[c
];
413 switch (ir
->operands
[0]->type
->base_type
) {
415 data
.b
[0] = op
[0]->value
.u
[0] < op
[1]->value
.u
[0];
418 data
.b
[0] = op
[0]->value
.i
[0] < op
[1]->value
.i
[0];
420 case GLSL_TYPE_FLOAT
:
421 data
.b
[0] = op
[0]->value
.f
[0] < op
[1]->value
.f
[0];
427 case ir_binop_greater
:
428 switch (ir
->operands
[0]->type
->base_type
) {
430 data
.b
[0] = op
[0]->value
.u
[0] > op
[1]->value
.u
[0];
433 data
.b
[0] = op
[0]->value
.i
[0] > op
[1]->value
.i
[0];
435 case GLSL_TYPE_FLOAT
:
436 data
.b
[0] = op
[0]->value
.f
[0] > op
[1]->value
.f
[0];
442 case ir_binop_lequal
:
443 switch (ir
->operands
[0]->type
->base_type
) {
445 data
.b
[0] = op
[0]->value
.u
[0] <= op
[1]->value
.u
[0];
448 data
.b
[0] = op
[0]->value
.i
[0] <= op
[1]->value
.i
[0];
450 case GLSL_TYPE_FLOAT
:
451 data
.b
[0] = op
[0]->value
.f
[0] <= op
[1]->value
.f
[0];
457 case ir_binop_gequal
:
458 switch (ir
->operands
[0]->type
->base_type
) {
460 data
.b
[0] = op
[0]->value
.u
[0] >= op
[1]->value
.u
[0];
463 data
.b
[0] = op
[0]->value
.i
[0] >= op
[1]->value
.i
[0];
465 case GLSL_TYPE_FLOAT
:
466 data
.b
[0] = op
[0]->value
.f
[0] >= op
[1]->value
.f
[0];
475 for (c
= 0; c
< ir
->operands
[0]->type
->components(); c
++) {
476 switch (ir
->operands
[0]->type
->base_type
) {
478 data
.b
[0] = data
.b
[0] && op
[0]->value
.u
[c
] == op
[1]->value
.u
[c
];
481 data
.b
[0] = data
.b
[0] && op
[0]->value
.i
[c
] == op
[1]->value
.i
[c
];
483 case GLSL_TYPE_FLOAT
:
484 data
.b
[0] = data
.b
[0] && op
[0]->value
.f
[c
] == op
[1]->value
.f
[c
];
487 data
.b
[0] = data
.b
[0] && op
[0]->value
.b
[c
] == op
[1]->value
.b
[c
];
494 case ir_binop_nequal
:
496 for (c
= 0; c
< ir
->operands
[0]->type
->components(); c
++) {
497 switch (ir
->operands
[0]->type
->base_type
) {
499 data
.b
[0] = data
.b
[0] || op
[0]->value
.u
[c
] != op
[1]->value
.u
[c
];
502 data
.b
[0] = data
.b
[0] || op
[0]->value
.i
[c
] != op
[1]->value
.i
[c
];
504 case GLSL_TYPE_FLOAT
:
505 data
.b
[0] = data
.b
[0] || op
[0]->value
.f
[c
] != op
[1]->value
.f
[c
];
508 data
.b
[0] = data
.b
[0] || op
[0]->value
.b
[c
] != op
[1]->value
.b
[c
];
517 /* FINISHME: Should handle all expression types. */
521 void *ctx
= talloc_parent(ir
);
522 this->value
= new(ctx
) ir_constant(ir
->type
, &data
);
527 ir_constant_visitor::visit(ir_texture
*ir
)
529 // FINISHME: Do stuff with texture lookups
536 ir_constant_visitor::visit(ir_swizzle
*ir
)
538 ir_constant
*v
= ir
->val
->constant_expression_value();
543 ir_constant_data data
;
545 const unsigned swiz_idx
[4] = {
546 ir
->mask
.x
, ir
->mask
.y
, ir
->mask
.z
, ir
->mask
.w
549 for (unsigned i
= 0; i
< ir
->mask
.num_components
; i
++) {
550 switch (v
->type
->base_type
) {
552 case GLSL_TYPE_INT
: data
.u
[i
] = v
->value
.u
[swiz_idx
[i
]]; break;
553 case GLSL_TYPE_FLOAT
: data
.f
[i
] = v
->value
.f
[swiz_idx
[i
]]; break;
554 case GLSL_TYPE_BOOL
: data
.b
[i
] = v
->value
.b
[swiz_idx
[i
]]; break;
555 default: assert(!"Should not get here."); break;
559 void *ctx
= talloc_parent(ir
);
560 this->value
= new(ctx
) ir_constant(ir
->type
, &data
);
566 ir_constant_visitor::visit(ir_dereference_variable
*ir
)
570 ir_variable
*var
= ir
->variable_referenced();
571 if (var
&& var
->constant_value
)
572 value
= (ir_constant
*)var
->constant_value
->clone(NULL
);
577 ir_constant_visitor::visit(ir_dereference_array
*ir
)
579 void *ctx
= talloc_parent(ir
);
580 ir_constant
*array
= ir
->array
->constant_expression_value();
581 ir_constant
*idx
= ir
->array_index
->constant_expression_value();
585 if ((array
!= NULL
) && (idx
!= NULL
)) {
586 if (array
->type
->is_matrix()) {
587 /* Array access of a matrix results in a vector.
589 const unsigned column
= idx
->value
.u
[0];
591 const glsl_type
*const column_type
= array
->type
->column_type();
593 /* Offset in the constant matrix to the first element of the column
596 const unsigned mat_idx
= column
* column_type
->vector_elements
;
598 ir_constant_data data
;
600 switch (column_type
->base_type
) {
603 for (unsigned i
= 0; i
< column_type
->vector_elements
; i
++)
604 data
.u
[i
] = array
->value
.u
[mat_idx
+ i
];
608 case GLSL_TYPE_FLOAT
:
609 for (unsigned i
= 0; i
< column_type
->vector_elements
; i
++)
610 data
.f
[i
] = array
->value
.f
[mat_idx
+ i
];
615 assert(!"Should not get here.");
619 this->value
= new(ctx
) ir_constant(column_type
, &data
);
620 } else if (array
->type
->is_vector()) {
621 const unsigned component
= idx
->value
.u
[0];
623 this->value
= new(ctx
) ir_constant(array
, component
);
625 /* FINISHME: Handle access of constant arrays. */
632 ir_constant_visitor::visit(ir_dereference_record
*ir
)
634 ir_constant
*v
= ir
->record
->constant_expression_value();
636 this->value
= (v
!= NULL
) ? v
->get_record_field(ir
->field
) : NULL
;
641 ir_constant_visitor::visit(ir_assignment
*ir
)
649 ir_constant_visitor::visit(ir_constant
*ir
)
656 ir_constant_visitor::visit(ir_call
*ir
)
664 ir_constant_visitor::visit(ir_return
*ir
)
672 ir_constant_visitor::visit(ir_discard
*ir
)
680 ir_constant_visitor::visit(ir_if
*ir
)
688 ir_constant_visitor::visit(ir_loop
*ir
)
696 ir_constant_visitor::visit(ir_loop_jump
*ir
)