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
)
132 ir_constant
*op
[2] = { NULL
, NULL
};
133 ir_constant_data data
;
135 memset(&data
, 0, sizeof(data
));
137 for (unsigned operand
= 0; operand
< ir
->get_num_operands(); operand
++) {
138 op
[operand
] = ir
->operands
[operand
]->constant_expression_value();
144 assert(op
[0]->type
->base_type
== op
[1]->type
->base_type
);
146 bool op0_scalar
= op
[0]->type
->is_scalar();
147 bool op1_scalar
= op
[1] != NULL
&& op
[1]->type
->is_scalar();
149 /* When iterating over a vector or matrix's components, we want to increase
150 * the loop counter. However, for scalars, we want to stay at 0.
152 unsigned c0_inc
= op0_scalar
? 1 : 0;
153 unsigned c1_inc
= op1_scalar
? 1 : 0;
154 unsigned components
= op
[op1_scalar
? 0 : 1]->type
->components();
156 switch (ir
->operation
) {
157 case ir_unop_logic_not
:
158 assert(op
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
159 for (unsigned c
= 0; c
< ir
->operands
[0]->type
->components(); c
++)
160 data
.b
[c
] = !op
[0]->value
.b
[c
];
164 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
165 for (unsigned c
= 0; c
< ir
->operands
[0]->type
->components(); c
++) {
166 data
.i
[c
] = op
[0]->value
.f
[c
];
170 assert(op
[0]->type
->base_type
== GLSL_TYPE_UINT
||
171 op
[0]->type
->base_type
== GLSL_TYPE_INT
);
172 for (unsigned c
= 0; c
< ir
->operands
[0]->type
->components(); c
++) {
173 if (op
[0]->type
->base_type
== GLSL_TYPE_INT
)
174 data
.f
[c
] = op
[0]->value
.i
[c
];
176 data
.f
[c
] = op
[0]->value
.u
[c
];
180 assert(op
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
181 for (unsigned c
= 0; c
< ir
->operands
[0]->type
->components(); c
++) {
182 data
.f
[c
] = op
[0]->value
.b
[c
] ? 1.0 : 0.0;
186 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
187 for (unsigned c
= 0; c
< ir
->operands
[0]->type
->components(); c
++) {
188 data
.b
[c
] = bool(op
[0]->value
.f
[c
]);
192 assert(op
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
193 for (unsigned c
= 0; c
< ir
->operands
[0]->type
->components(); c
++) {
194 data
.u
[c
] = op
[0]->value
.b
[c
] ? 1 : 0;
198 assert(op
[0]->type
->is_integer());
199 for (unsigned c
= 0; c
< ir
->operands
[0]->type
->components(); c
++) {
200 data
.b
[c
] = bool(op
[0]->value
.u
[c
]);
205 for (unsigned c
= 0; c
< ir
->operands
[0]->type
->components(); c
++) {
206 switch (ir
->type
->base_type
) {
213 case GLSL_TYPE_FLOAT
:
214 data
.f
[c
] = op
[0]->value
.f
[c
] - floor(op
[0]->value
.f
[c
]);
223 for (unsigned c
= 0; c
< ir
->operands
[0]->type
->components(); c
++) {
224 switch (ir
->type
->base_type
) {
226 data
.u
[c
] = -op
[0]->value
.u
[c
];
229 data
.i
[c
] = -op
[0]->value
.i
[c
];
231 case GLSL_TYPE_FLOAT
:
232 data
.f
[c
] = -op
[0]->value
.f
[c
];
241 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
242 for (unsigned c
= 0; c
< ir
->operands
[0]->type
->components(); c
++) {
243 switch (ir
->type
->base_type
) {
245 data
.u
[c
] = op
[0]->value
.u
[c
];
248 data
.i
[c
] = op
[0]->value
.i
[c
];
250 data
.i
[c
] = -data
.i
[c
];
252 case GLSL_TYPE_FLOAT
:
253 data
.f
[c
] = fabs(op
[0]->value
.f
[c
]);
262 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
263 for (unsigned c
= 0; c
< ir
->operands
[0]->type
->components(); c
++) {
264 switch (ir
->type
->base_type
) {
266 if (op
[0]->value
.u
[c
] != 0.0)
267 data
.u
[c
] = 1 / op
[0]->value
.u
[c
];
270 if (op
[0]->value
.i
[c
] != 0.0)
271 data
.i
[c
] = 1 / op
[0]->value
.i
[c
];
273 case GLSL_TYPE_FLOAT
:
274 if (op
[0]->value
.f
[c
] != 0.0)
275 data
.f
[c
] = 1.0 / op
[0]->value
.f
[c
];
284 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
285 for (unsigned c
= 0; c
< ir
->operands
[0]->type
->components(); c
++) {
286 data
.f
[c
] = 1.0 / sqrtf(op
[0]->value
.f
[c
]);
291 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
292 for (unsigned c
= 0; c
< ir
->operands
[0]->type
->components(); c
++) {
293 data
.f
[c
] = sqrtf(op
[0]->value
.f
[c
]);
298 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
299 for (unsigned c
= 0; c
< ir
->operands
[0]->type
->components(); c
++) {
300 data
.f
[c
] = expf(op
[0]->value
.f
[c
]);
305 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
306 for (unsigned c
= 0; c
< ir
->operands
[0]->type
->components(); c
++) {
307 data
.f
[c
] = logf(op
[0]->value
.f
[c
]);
313 assert(op
[0]->type
->base_type
== GLSL_TYPE_FLOAT
);
314 for (unsigned c
= 0; c
< ir
->operands
[0]->type
->components(); c
++) {
320 assert(op
[0]->type
->is_vector() && op
[1]->type
->is_vector());
322 for (unsigned c
= 0; c
< op
[0]->type
->components(); c
++) {
323 switch (ir
->operands
[0]->type
->base_type
) {
325 data
.u
[0] += op
[0]->value
.u
[c
] * op
[1]->value
.u
[c
];
328 data
.i
[0] += op
[0]->value
.i
[c
] * op
[1]->value
.i
[c
];
330 case GLSL_TYPE_FLOAT
:
331 data
.f
[0] += op
[0]->value
.f
[c
] * op
[1]->value
.f
[c
];
340 assert(op
[0]->type
== op
[1]->type
|| op0_scalar
|| op1_scalar
);
341 for (unsigned c
= 0, c0
= 0, c1
= 0;
343 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
345 switch (ir
->operands
[0]->type
->base_type
) {
347 data
.u
[c
] = op
[0]->value
.u
[c0
] + op
[1]->value
.u
[c1
];
350 data
.i
[c
] = op
[0]->value
.i
[c0
] + op
[1]->value
.i
[c1
];
352 case GLSL_TYPE_FLOAT
:
353 data
.f
[c
] = op
[0]->value
.f
[c0
] + op
[1]->value
.f
[c1
];
362 assert(op
[0]->type
== op
[1]->type
|| op0_scalar
|| op1_scalar
);
363 for (unsigned c
= 0, c0
= 0, c1
= 0;
365 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
367 switch (ir
->operands
[0]->type
->base_type
) {
369 data
.u
[c
] = op
[0]->value
.u
[c0
] - op
[1]->value
.u
[c1
];
372 data
.i
[c
] = op
[0]->value
.i
[c0
] - op
[1]->value
.i
[c1
];
374 case GLSL_TYPE_FLOAT
:
375 data
.f
[c
] = op
[0]->value
.f
[c0
] - op
[1]->value
.f
[c1
];
384 /* Check for equal types, or unequal types involving scalars */
385 if ((op
[0]->type
== op
[1]->type
&& !op
[0]->type
->is_matrix())
386 || op0_scalar
|| op1_scalar
) {
387 for (unsigned c
= 0, c0
= 0, c1
= 0;
389 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
391 switch (ir
->operands
[0]->type
->base_type
) {
393 data
.u
[c
] = op
[0]->value
.u
[c0
] * op
[1]->value
.u
[c1
];
396 data
.i
[c
] = op
[0]->value
.i
[c0
] * op
[1]->value
.i
[c1
];
398 case GLSL_TYPE_FLOAT
:
399 data
.f
[c
] = op
[0]->value
.f
[c0
] * op
[1]->value
.f
[c1
];
406 assert(op
[0]->type
->is_matrix() || op
[1]->type
->is_matrix());
408 /* Multiply an N-by-M matrix with an M-by-P matrix. Since either
409 * matrix can be a GLSL vector, either N or P can be 1.
411 * For vec*mat, the vector is treated as a row vector. This
412 * means the vector is a 1-row x M-column matrix.
414 * For mat*vec, the vector is treated as a column vector. Since
415 * matrix_columns is 1 for vectors, this just works.
417 const unsigned n
= op
[0]->type
->is_vector()
418 ? 1 : op
[0]->type
->vector_elements
;
419 const unsigned m
= op
[1]->type
->vector_elements
;
420 const unsigned p
= op
[1]->type
->matrix_columns
;
421 for (unsigned j
= 0; j
< p
; j
++) {
422 for (unsigned i
= 0; i
< n
; i
++) {
423 for (unsigned k
= 0; k
< m
; k
++) {
424 data
.f
[i
+n
*j
] += op
[0]->value
.f
[i
+n
*k
]*op
[1]->value
.f
[k
+m
*j
];
432 assert(op
[0]->type
== op
[1]->type
|| op0_scalar
|| op1_scalar
);
433 for (unsigned c
= 0, c0
= 0, c1
= 0;
435 c0
+= c0_inc
, c1
+= c1_inc
, c
++) {
437 switch (ir
->operands
[0]->type
->base_type
) {
439 data
.u
[c
] = op
[0]->value
.u
[c0
] / op
[1]->value
.u
[c1
];
442 data
.i
[c
] = op
[0]->value
.i
[c0
] / op
[1]->value
.i
[c1
];
444 case GLSL_TYPE_FLOAT
:
445 data
.f
[c
] = op
[0]->value
.f
[c0
] / op
[1]->value
.f
[c1
];
453 case ir_binop_logic_and
:
454 assert(op
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
455 for (unsigned c
= 0; c
< ir
->operands
[0]->type
->components(); c
++)
456 data
.b
[c
] = op
[0]->value
.b
[c
] && op
[1]->value
.b
[c
];
458 case ir_binop_logic_xor
:
459 assert(op
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
460 for (unsigned c
= 0; c
< ir
->operands
[0]->type
->components(); c
++)
461 data
.b
[c
] = op
[0]->value
.b
[c
] ^ op
[1]->value
.b
[c
];
463 case ir_binop_logic_or
:
464 assert(op
[0]->type
->base_type
== GLSL_TYPE_BOOL
);
465 for (unsigned c
= 0; c
< ir
->operands
[0]->type
->components(); c
++)
466 data
.b
[c
] = op
[0]->value
.b
[c
] || op
[1]->value
.b
[c
];
470 switch (ir
->operands
[0]->type
->base_type
) {
472 data
.b
[0] = op
[0]->value
.u
[0] < op
[1]->value
.u
[0];
475 data
.b
[0] = op
[0]->value
.i
[0] < op
[1]->value
.i
[0];
477 case GLSL_TYPE_FLOAT
:
478 data
.b
[0] = op
[0]->value
.f
[0] < op
[1]->value
.f
[0];
484 case ir_binop_greater
:
485 switch (ir
->operands
[0]->type
->base_type
) {
487 data
.b
[0] = op
[0]->value
.u
[0] > op
[1]->value
.u
[0];
490 data
.b
[0] = op
[0]->value
.i
[0] > op
[1]->value
.i
[0];
492 case GLSL_TYPE_FLOAT
:
493 data
.b
[0] = op
[0]->value
.f
[0] > op
[1]->value
.f
[0];
499 case ir_binop_lequal
:
500 switch (ir
->operands
[0]->type
->base_type
) {
502 data
.b
[0] = op
[0]->value
.u
[0] <= op
[1]->value
.u
[0];
505 data
.b
[0] = op
[0]->value
.i
[0] <= op
[1]->value
.i
[0];
507 case GLSL_TYPE_FLOAT
:
508 data
.b
[0] = op
[0]->value
.f
[0] <= op
[1]->value
.f
[0];
514 case ir_binop_gequal
:
515 switch (ir
->operands
[0]->type
->base_type
) {
517 data
.b
[0] = op
[0]->value
.u
[0] >= op
[1]->value
.u
[0];
520 data
.b
[0] = op
[0]->value
.i
[0] >= op
[1]->value
.i
[0];
522 case GLSL_TYPE_FLOAT
:
523 data
.b
[0] = op
[0]->value
.f
[0] >= op
[1]->value
.f
[0];
532 for (unsigned c
= 0; c
< ir
->operands
[0]->type
->components(); c
++) {
533 switch (ir
->operands
[0]->type
->base_type
) {
535 data
.b
[0] = data
.b
[0] && op
[0]->value
.u
[c
] == op
[1]->value
.u
[c
];
538 data
.b
[0] = data
.b
[0] && op
[0]->value
.i
[c
] == op
[1]->value
.i
[c
];
540 case GLSL_TYPE_FLOAT
:
541 data
.b
[0] = data
.b
[0] && op
[0]->value
.f
[c
] == op
[1]->value
.f
[c
];
544 data
.b
[0] = data
.b
[0] && op
[0]->value
.b
[c
] == op
[1]->value
.b
[c
];
551 case ir_binop_nequal
:
553 for (unsigned c
= 0; c
< ir
->operands
[0]->type
->components(); c
++) {
554 switch (ir
->operands
[0]->type
->base_type
) {
556 data
.b
[0] = data
.b
[0] || op
[0]->value
.u
[c
] != op
[1]->value
.u
[c
];
559 data
.b
[0] = data
.b
[0] || op
[0]->value
.i
[c
] != op
[1]->value
.i
[c
];
561 case GLSL_TYPE_FLOAT
:
562 data
.b
[0] = data
.b
[0] || op
[0]->value
.f
[c
] != op
[1]->value
.f
[c
];
565 data
.b
[0] = data
.b
[0] || op
[0]->value
.b
[c
] != op
[1]->value
.b
[c
];
574 /* FINISHME: Should handle all expression types. */
578 void *ctx
= talloc_parent(ir
);
579 this->value
= new(ctx
) ir_constant(ir
->type
, &data
);
584 ir_constant_visitor::visit(ir_texture
*ir
)
586 // FINISHME: Do stuff with texture lookups
593 ir_constant_visitor::visit(ir_swizzle
*ir
)
595 ir_constant
*v
= ir
->val
->constant_expression_value();
600 ir_constant_data data
;
602 const unsigned swiz_idx
[4] = {
603 ir
->mask
.x
, ir
->mask
.y
, ir
->mask
.z
, ir
->mask
.w
606 for (unsigned i
= 0; i
< ir
->mask
.num_components
; i
++) {
607 switch (v
->type
->base_type
) {
609 case GLSL_TYPE_INT
: data
.u
[i
] = v
->value
.u
[swiz_idx
[i
]]; break;
610 case GLSL_TYPE_FLOAT
: data
.f
[i
] = v
->value
.f
[swiz_idx
[i
]]; break;
611 case GLSL_TYPE_BOOL
: data
.b
[i
] = v
->value
.b
[swiz_idx
[i
]]; break;
612 default: assert(!"Should not get here."); break;
616 void *ctx
= talloc_parent(ir
);
617 this->value
= new(ctx
) ir_constant(ir
->type
, &data
);
623 ir_constant_visitor::visit(ir_dereference_variable
*ir
)
627 ir_variable
*var
= ir
->variable_referenced();
628 if (var
&& var
->constant_value
)
629 value
= var
->constant_value
->clone(NULL
);
634 ir_constant_visitor::visit(ir_dereference_array
*ir
)
636 void *ctx
= talloc_parent(ir
);
637 ir_constant
*array
= ir
->array
->constant_expression_value();
638 ir_constant
*idx
= ir
->array_index
->constant_expression_value();
642 if ((array
!= NULL
) && (idx
!= NULL
)) {
643 if (array
->type
->is_matrix()) {
644 /* Array access of a matrix results in a vector.
646 const unsigned column
= idx
->value
.u
[0];
648 const glsl_type
*const column_type
= array
->type
->column_type();
650 /* Offset in the constant matrix to the first element of the column
653 const unsigned mat_idx
= column
* column_type
->vector_elements
;
655 ir_constant_data data
;
657 switch (column_type
->base_type
) {
660 for (unsigned i
= 0; i
< column_type
->vector_elements
; i
++)
661 data
.u
[i
] = array
->value
.u
[mat_idx
+ i
];
665 case GLSL_TYPE_FLOAT
:
666 for (unsigned i
= 0; i
< column_type
->vector_elements
; i
++)
667 data
.f
[i
] = array
->value
.f
[mat_idx
+ i
];
672 assert(!"Should not get here.");
676 this->value
= new(ctx
) ir_constant(column_type
, &data
);
677 } else if (array
->type
->is_vector()) {
678 const unsigned component
= idx
->value
.u
[0];
680 this->value
= new(ctx
) ir_constant(array
, component
);
682 /* FINISHME: Handle access of constant arrays. */
689 ir_constant_visitor::visit(ir_dereference_record
*ir
)
691 ir_constant
*v
= ir
->record
->constant_expression_value();
693 this->value
= (v
!= NULL
) ? v
->get_record_field(ir
->field
) : NULL
;
698 ir_constant_visitor::visit(ir_assignment
*ir
)
706 ir_constant_visitor::visit(ir_constant
*ir
)
713 ir_constant_visitor::visit(ir_call
*ir
)
721 ir_constant_visitor::visit(ir_return
*ir
)
729 ir_constant_visitor::visit(ir_discard
*ir
)
737 ir_constant_visitor::visit(ir_if
*ir
)
745 ir_constant_visitor::visit(ir_loop
*ir
)
753 ir_constant_visitor::visit(ir_loop_jump
*ir
)