blob: 06d7dd8fbe07ec8a0f3c4bc8601f86dbdde67002 [file] [log] [blame]
Ian Romanick1cf43a42010-03-30 16:56:50 -07001/*
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
Eric Anholt43ad37a2010-05-12 14:42:21 -070036#include <math.h>
Ian Romanick1cf43a42010-03-30 16:56:50 -070037#include "ir.h"
38#include "ir_visitor.h"
Eric Anholta576f9d2010-03-31 16:25:12 -100039#include "glsl_types.h"
Ian Romanick1cf43a42010-03-30 16:56:50 -070040
41/**
42 * Visitor class for evaluating constant expressions
43 */
44class ir_constant_visitor : public ir_visitor {
45public:
46 ir_constant_visitor()
47 : value(NULL)
48 {
49 /* empty */
50 }
51
52 virtual ~ir_constant_visitor()
53 {
54 /* empty */
55 }
56
57 /**
58 * \name Visit methods
59 *
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.
63 */
64 /*@{*/
65 virtual void visit(ir_variable *);
Ian Romanick1cf43a42010-03-30 16:56:50 -070066 virtual void visit(ir_function_signature *);
67 virtual void visit(ir_function *);
68 virtual void visit(ir_expression *);
Kenneth Graunke26d74cd2010-05-26 17:42:03 -070069 virtual void visit(ir_texture *);
Ian Romanick1cf43a42010-03-30 16:56:50 -070070 virtual void visit(ir_swizzle *);
Ian Romanickc7b10462010-05-19 13:20:12 +020071 virtual void visit(ir_dereference_variable *);
72 virtual void visit(ir_dereference_array *);
73 virtual void visit(ir_dereference_record *);
Ian Romanick1cf43a42010-03-30 16:56:50 -070074 virtual void visit(ir_assignment *);
75 virtual void visit(ir_constant *);
76 virtual void visit(ir_call *);
77 virtual void visit(ir_return *);
Kenneth Graunke16efab12010-06-30 10:47:34 -070078 virtual void visit(ir_discard *);
Ian Romanick1cf43a42010-03-30 16:56:50 -070079 virtual void visit(ir_if *);
Ian Romanickfad607a2010-04-05 16:16:07 -070080 virtual void visit(ir_loop *);
Ian Romanickf8e31e02010-04-05 16:28:15 -070081 virtual void visit(ir_loop_jump *);
Ian Romanick1cf43a42010-03-30 16:56:50 -070082 /*@}*/
83
84 /**
85 * Value of the constant expression.
86 *
87 * \note
88 * This field will be \c NULL if the expression is not constant valued.
89 */
90 /* FINIHSME: This cannot hold values for constant arrays or structures. */
91 ir_constant *value;
92};
93
94
95ir_constant *
96ir_instruction::constant_expression_value()
97{
98 ir_constant_visitor visitor;
99
100 this->accept(& visitor);
101 return visitor.value;
102}
103
104
105void
106ir_constant_visitor::visit(ir_variable *ir)
107{
108 (void) ir;
109 value = NULL;
110}
111
112
113void
Ian Romanick1cf43a42010-03-30 16:56:50 -0700114ir_constant_visitor::visit(ir_function_signature *ir)
115{
116 (void) ir;
117 value = NULL;
118}
119
120
121void
122ir_constant_visitor::visit(ir_function *ir)
123{
124 (void) ir;
125 value = NULL;
126}
127
Ian Romanick1cf43a42010-03-30 16:56:50 -0700128void
129ir_constant_visitor::visit(ir_expression *ir)
130{
Ian Romanick1cf43a42010-03-30 16:56:50 -0700131 value = NULL;
Kenneth Graunke6bc432e2010-07-02 17:12:23 -0700132 ir_constant *op[2] = { NULL, NULL };
Ian Romanick4daaab62010-06-11 16:08:47 -0700133 ir_constant_data data;
Eric Anholt160d0922010-04-01 18:07:08 -1000134
Kenneth Graunkec63a1db2010-07-05 22:33:35 -0700135 memset(&data, 0, sizeof(data));
136
Kenneth Graunkef14e5962010-07-06 16:26:11 -0700137 for (unsigned operand = 0; operand < ir->get_num_operands(); operand++) {
Eric Anholtd98da972010-04-01 18:25:11 -1000138 op[operand] = ir->operands[operand]->constant_expression_value();
139 if (!op[operand])
Eric Anholt160d0922010-04-01 18:07:08 -1000140 return;
141 }
Eric Anholta576f9d2010-03-31 16:25:12 -1000142
Kenneth Graunke6fc983b2010-07-06 02:39:57 -0700143 if (op[1] != NULL)
144 assert(op[0]->type->base_type == op[1]->type->base_type);
145
Kenneth Graunkee74dcd72010-07-06 02:48:16 -0700146 bool op0_scalar = op[0]->type->is_scalar();
147 bool op1_scalar = op[1] != NULL && op[1]->type->is_scalar();
148
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.
151 */
Kenneth Graunkeacf88f22010-07-07 12:08:23 -0700152 unsigned c0_inc = op0_scalar ? 0 : 1;
153 unsigned c1_inc = op1_scalar ? 0 : 1;
Eric Anholt570dc0d2010-07-07 09:07:09 -0700154 unsigned components;
155 if (op1_scalar || !op[1]) {
156 components = op[0]->type->components();
157 } else {
158 components = op[1]->type->components();
159 }
Kenneth Graunkee74dcd72010-07-06 02:48:16 -0700160
Eric Anholta576f9d2010-03-31 16:25:12 -1000161 switch (ir->operation) {
Eric Anholt528bb852010-03-31 21:09:02 -1000162 case ir_unop_logic_not:
Ian Romanickf8b88be2010-06-11 16:23:52 -0700163 assert(op[0]->type->base_type == GLSL_TYPE_BOOL);
Kenneth Graunkef14e5962010-07-06 16:26:11 -0700164 for (unsigned c = 0; c < ir->operands[0]->type->components(); c++)
Ian Romanick4daaab62010-06-11 16:08:47 -0700165 data.b[c] = !op[0]->value.b[c];
Eric Anholt528bb852010-03-31 21:09:02 -1000166 break;
Eric Anholtaf186412010-04-06 10:53:57 -0700167
168 case ir_unop_f2i:
169 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
Kenneth Graunkef14e5962010-07-06 16:26:11 -0700170 for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) {
Ian Romanick4daaab62010-06-11 16:08:47 -0700171 data.i[c] = op[0]->value.f[c];
Eric Anholtaf186412010-04-06 10:53:57 -0700172 }
173 break;
174 case ir_unop_i2f:
175 assert(op[0]->type->base_type == GLSL_TYPE_UINT ||
176 op[0]->type->base_type == GLSL_TYPE_INT);
Kenneth Graunkef14e5962010-07-06 16:26:11 -0700177 for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) {
Eric Anholtaf186412010-04-06 10:53:57 -0700178 if (op[0]->type->base_type == GLSL_TYPE_INT)
Ian Romanick4daaab62010-06-11 16:08:47 -0700179 data.f[c] = op[0]->value.i[c];
Eric Anholtaf186412010-04-06 10:53:57 -0700180 else
Ian Romanick4daaab62010-06-11 16:08:47 -0700181 data.f[c] = op[0]->value.u[c];
Eric Anholtaf186412010-04-06 10:53:57 -0700182 }
183 break;
Ian Romanick7dc2b712010-06-07 15:10:14 -0700184 case ir_unop_b2f:
185 assert(op[0]->type->base_type == GLSL_TYPE_BOOL);
Kenneth Graunkef14e5962010-07-06 16:26:11 -0700186 for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) {
Ian Romanick4daaab62010-06-11 16:08:47 -0700187 data.f[c] = op[0]->value.b[c] ? 1.0 : 0.0;
Ian Romanick7dc2b712010-06-07 15:10:14 -0700188 }
189 break;
190 case ir_unop_f2b:
191 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
Kenneth Graunkef14e5962010-07-06 16:26:11 -0700192 for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) {
Ian Romanick4daaab62010-06-11 16:08:47 -0700193 data.b[c] = bool(op[0]->value.f[c]);
Ian Romanick7dc2b712010-06-07 15:10:14 -0700194 }
195 break;
Ian Romanick39d6dd32010-06-11 13:46:30 -0700196 case ir_unop_b2i:
197 assert(op[0]->type->base_type == GLSL_TYPE_BOOL);
Kenneth Graunkef14e5962010-07-06 16:26:11 -0700198 for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) {
Ian Romanick4daaab62010-06-11 16:08:47 -0700199 data.u[c] = op[0]->value.b[c] ? 1 : 0;
Ian Romanick39d6dd32010-06-11 13:46:30 -0700200 }
201 break;
202 case ir_unop_i2b:
203 assert(op[0]->type->is_integer());
Kenneth Graunkef14e5962010-07-06 16:26:11 -0700204 for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) {
Ian Romanick4daaab62010-06-11 16:08:47 -0700205 data.b[c] = bool(op[0]->value.u[c]);
Ian Romanick39d6dd32010-06-11 13:46:30 -0700206 }
207 break;
Eric Anholtaf186412010-04-06 10:53:57 -0700208
Eric Anholtd925c912010-07-01 10:37:11 -0700209 case ir_unop_fract:
Kenneth Graunkef14e5962010-07-06 16:26:11 -0700210 for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) {
Eric Anholtd925c912010-07-01 10:37:11 -0700211 switch (ir->type->base_type) {
212 case GLSL_TYPE_UINT:
213 data.u[c] = 0;
214 break;
215 case GLSL_TYPE_INT:
216 data.i[c] = 0;
217 break;
218 case GLSL_TYPE_FLOAT:
219 data.f[c] = op[0]->value.f[c] - floor(op[0]->value.f[c]);
220 break;
221 default:
222 assert(0);
223 }
224 }
225 break;
226
Eric Anholt43ad37a2010-05-12 14:42:21 -0700227 case ir_unop_neg:
Kenneth Graunkef14e5962010-07-06 16:26:11 -0700228 for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) {
Ian Romanickf8b88be2010-06-11 16:23:52 -0700229 switch (ir->type->base_type) {
Eric Anholt43ad37a2010-05-12 14:42:21 -0700230 case GLSL_TYPE_UINT:
Ian Romanick4daaab62010-06-11 16:08:47 -0700231 data.u[c] = -op[0]->value.u[c];
Eric Anholt43ad37a2010-05-12 14:42:21 -0700232 break;
233 case GLSL_TYPE_INT:
Ian Romanick4daaab62010-06-11 16:08:47 -0700234 data.i[c] = -op[0]->value.i[c];
Eric Anholt43ad37a2010-05-12 14:42:21 -0700235 break;
236 case GLSL_TYPE_FLOAT:
Ian Romanick4daaab62010-06-11 16:08:47 -0700237 data.f[c] = -op[0]->value.f[c];
Eric Anholt43ad37a2010-05-12 14:42:21 -0700238 break;
239 default:
240 assert(0);
241 }
242 }
243 break;
244
245 case ir_unop_abs:
Kenneth Graunkef14e5962010-07-06 16:26:11 -0700246 for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) {
Ian Romanickf8b88be2010-06-11 16:23:52 -0700247 switch (ir->type->base_type) {
Eric Anholt43ad37a2010-05-12 14:42:21 -0700248 case GLSL_TYPE_UINT:
Ian Romanick4daaab62010-06-11 16:08:47 -0700249 data.u[c] = op[0]->value.u[c];
Eric Anholt43ad37a2010-05-12 14:42:21 -0700250 break;
251 case GLSL_TYPE_INT:
Ian Romanick4daaab62010-06-11 16:08:47 -0700252 data.i[c] = op[0]->value.i[c];
253 if (data.i[c] < 0)
254 data.i[c] = -data.i[c];
Eric Anholt43ad37a2010-05-12 14:42:21 -0700255 break;
256 case GLSL_TYPE_FLOAT:
Ian Romanick4daaab62010-06-11 16:08:47 -0700257 data.f[c] = fabs(op[0]->value.f[c]);
Eric Anholt43ad37a2010-05-12 14:42:21 -0700258 break;
259 default:
260 assert(0);
261 }
262 }
263 break;
264
265 case ir_unop_rcp:
266 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
Kenneth Graunkef14e5962010-07-06 16:26:11 -0700267 for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) {
Ian Romanickf8b88be2010-06-11 16:23:52 -0700268 switch (ir->type->base_type) {
Eric Anholt43ad37a2010-05-12 14:42:21 -0700269 case GLSL_TYPE_UINT:
270 if (op[0]->value.u[c] != 0.0)
Ian Romanick4daaab62010-06-11 16:08:47 -0700271 data.u[c] = 1 / op[0]->value.u[c];
Eric Anholt43ad37a2010-05-12 14:42:21 -0700272 break;
273 case GLSL_TYPE_INT:
274 if (op[0]->value.i[c] != 0.0)
Ian Romanick4daaab62010-06-11 16:08:47 -0700275 data.i[c] = 1 / op[0]->value.i[c];
Eric Anholt43ad37a2010-05-12 14:42:21 -0700276 break;
277 case GLSL_TYPE_FLOAT:
278 if (op[0]->value.f[c] != 0.0)
Ian Romanick4daaab62010-06-11 16:08:47 -0700279 data.f[c] = 1.0 / op[0]->value.f[c];
Eric Anholt43ad37a2010-05-12 14:42:21 -0700280 break;
281 default:
282 assert(0);
283 }
284 }
285 break;
286
287 case ir_unop_rsq:
288 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
Kenneth Graunkef14e5962010-07-06 16:26:11 -0700289 for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) {
Ian Romanick4daaab62010-06-11 16:08:47 -0700290 data.f[c] = 1.0 / sqrtf(op[0]->value.f[c]);
Eric Anholt43ad37a2010-05-12 14:42:21 -0700291 }
292 break;
293
294 case ir_unop_sqrt:
295 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
Kenneth Graunkef14e5962010-07-06 16:26:11 -0700296 for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) {
Ian Romanick4daaab62010-06-11 16:08:47 -0700297 data.f[c] = sqrtf(op[0]->value.f[c]);
Eric Anholt43ad37a2010-05-12 14:42:21 -0700298 }
299 break;
300
301 case ir_unop_exp:
302 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
Kenneth Graunkef14e5962010-07-06 16:26:11 -0700303 for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) {
Ian Romanick4daaab62010-06-11 16:08:47 -0700304 data.f[c] = expf(op[0]->value.f[c]);
Eric Anholt43ad37a2010-05-12 14:42:21 -0700305 }
306 break;
307
308 case ir_unop_log:
309 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
Kenneth Graunkef14e5962010-07-06 16:26:11 -0700310 for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) {
Ian Romanick4daaab62010-06-11 16:08:47 -0700311 data.f[c] = logf(op[0]->value.f[c]);
Eric Anholt43ad37a2010-05-12 14:42:21 -0700312 }
313 break;
314
Kenneth Graunked6a32d42010-06-09 15:22:35 -0700315 case ir_unop_dFdx:
316 case ir_unop_dFdy:
317 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
Kenneth Graunkef14e5962010-07-06 16:26:11 -0700318 for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) {
Ian Romanick4daaab62010-06-11 16:08:47 -0700319 data.f[c] = 0.0;
Kenneth Graunked6a32d42010-06-09 15:22:35 -0700320 }
321 break;
322
Kenneth Graunke3f4a0b82010-07-05 21:15:32 -0700323 case ir_binop_dot:
324 assert(op[0]->type->is_vector() && op[1]->type->is_vector());
325 data.f[0] = 0;
Kenneth Graunkef14e5962010-07-06 16:26:11 -0700326 for (unsigned c = 0; c < op[0]->type->components(); c++) {
Kenneth Graunke3f4a0b82010-07-05 21:15:32 -0700327 switch (ir->operands[0]->type->base_type) {
328 case GLSL_TYPE_UINT:
329 data.u[0] += op[0]->value.u[c] * op[1]->value.u[c];
330 break;
331 case GLSL_TYPE_INT:
332 data.i[0] += op[0]->value.i[c] * op[1]->value.i[c];
333 break;
334 case GLSL_TYPE_FLOAT:
335 data.f[0] += op[0]->value.f[c] * op[1]->value.f[c];
336 break;
337 default:
338 assert(0);
339 }
340 }
341
342 break;
Eric Anholtd251b922010-04-01 18:35:42 -1000343 case ir_binop_add:
Kenneth Graunkee74dcd72010-07-06 02:48:16 -0700344 assert(op[0]->type == op[1]->type || op0_scalar || op1_scalar);
345 for (unsigned c = 0, c0 = 0, c1 = 0;
346 c < components;
347 c0 += c0_inc, c1 += c1_inc, c++) {
348
349 switch (ir->operands[0]->type->base_type) {
350 case GLSL_TYPE_UINT:
351 data.u[c] = op[0]->value.u[c0] + op[1]->value.u[c1];
352 break;
353 case GLSL_TYPE_INT:
354 data.i[c] = op[0]->value.i[c0] + op[1]->value.i[c1];
355 break;
356 case GLSL_TYPE_FLOAT:
357 data.f[c] = op[0]->value.f[c0] + op[1]->value.f[c1];
358 break;
359 default:
360 assert(0);
Eric Anholtd251b922010-04-01 18:35:42 -1000361 }
Kenneth Graunkee74dcd72010-07-06 02:48:16 -0700362 }
Ian Romanickf8b88be2010-06-11 16:23:52 -0700363
Eric Anholtd251b922010-04-01 18:35:42 -1000364 break;
365 case ir_binop_sub:
Kenneth Graunke97b44f02010-07-06 02:53:29 -0700366 assert(op[0]->type == op[1]->type || op0_scalar || op1_scalar);
367 for (unsigned c = 0, c0 = 0, c1 = 0;
368 c < components;
369 c0 += c0_inc, c1 += c1_inc, c++) {
370
371 switch (ir->operands[0]->type->base_type) {
372 case GLSL_TYPE_UINT:
373 data.u[c] = op[0]->value.u[c0] - op[1]->value.u[c1];
374 break;
375 case GLSL_TYPE_INT:
376 data.i[c] = op[0]->value.i[c0] - op[1]->value.i[c1];
377 break;
378 case GLSL_TYPE_FLOAT:
379 data.f[c] = op[0]->value.f[c0] - op[1]->value.f[c1];
380 break;
381 default:
382 assert(0);
Eric Anholtd251b922010-04-01 18:35:42 -1000383 }
Kenneth Graunke97b44f02010-07-06 02:53:29 -0700384 }
Ian Romanickf8b88be2010-06-11 16:23:52 -0700385
Eric Anholtd251b922010-04-01 18:35:42 -1000386 break;
Eric Anholta576f9d2010-03-31 16:25:12 -1000387 case ir_binop_mul:
Kenneth Graunkecf80a4d2010-07-05 23:19:56 -0700388 /* Check for equal types, or unequal types involving scalars */
Kenneth Graunke37b3f9d2010-07-06 03:01:15 -0700389 if ((op[0]->type == op[1]->type && !op[0]->type->is_matrix())
390 || op0_scalar || op1_scalar) {
391 for (unsigned c = 0, c0 = 0, c1 = 0;
392 c < components;
393 c0 += c0_inc, c1 += c1_inc, c++) {
394
Eric Anholtd98da972010-04-01 18:25:11 -1000395 switch (ir->operands[0]->type->base_type) {
396 case GLSL_TYPE_UINT:
Kenneth Graunke37b3f9d2010-07-06 03:01:15 -0700397 data.u[c] = op[0]->value.u[c0] * op[1]->value.u[c1];
Eric Anholtd98da972010-04-01 18:25:11 -1000398 break;
399 case GLSL_TYPE_INT:
Kenneth Graunke37b3f9d2010-07-06 03:01:15 -0700400 data.i[c] = op[0]->value.i[c0] * op[1]->value.i[c1];
Eric Anholtd98da972010-04-01 18:25:11 -1000401 break;
402 case GLSL_TYPE_FLOAT:
Kenneth Graunke37b3f9d2010-07-06 03:01:15 -0700403 data.f[c] = op[0]->value.f[c0] * op[1]->value.f[c1];
Eric Anholtd98da972010-04-01 18:25:11 -1000404 break;
405 default:
406 assert(0);
407 }
Eric Anholta576f9d2010-03-31 16:25:12 -1000408 }
Kenneth Graunkecf80a4d2010-07-05 23:19:56 -0700409 } else {
410 assert(op[0]->type->is_matrix() || op[1]->type->is_matrix());
411
412 /* Multiply an N-by-M matrix with an M-by-P matrix. Since either
413 * matrix can be a GLSL vector, either N or P can be 1.
414 *
415 * For vec*mat, the vector is treated as a row vector. This
416 * means the vector is a 1-row x M-column matrix.
417 *
418 * For mat*vec, the vector is treated as a column vector. Since
419 * matrix_columns is 1 for vectors, this just works.
420 */
421 const unsigned n = op[0]->type->is_vector()
422 ? 1 : op[0]->type->vector_elements;
423 const unsigned m = op[1]->type->vector_elements;
424 const unsigned p = op[1]->type->matrix_columns;
425 for (unsigned j = 0; j < p; j++) {
426 for (unsigned i = 0; i < n; i++) {
427 for (unsigned k = 0; k < m; k++) {
428 data.f[i+n*j] += op[0]->value.f[i+n*k]*op[1]->value.f[k+m*j];
429 }
430 }
431 }
432 }
Ian Romanickf8b88be2010-06-11 16:23:52 -0700433
Eric Anholta576f9d2010-03-31 16:25:12 -1000434 break;
Eric Anholtd251b922010-04-01 18:35:42 -1000435 case ir_binop_div:
Kenneth Graunkedad35eb2010-07-06 02:56:36 -0700436 assert(op[0]->type == op[1]->type || op0_scalar || op1_scalar);
437 for (unsigned c = 0, c0 = 0, c1 = 0;
438 c < components;
439 c0 += c0_inc, c1 += c1_inc, c++) {
440
441 switch (ir->operands[0]->type->base_type) {
442 case GLSL_TYPE_UINT:
443 data.u[c] = op[0]->value.u[c0] / op[1]->value.u[c1];
444 break;
445 case GLSL_TYPE_INT:
446 data.i[c] = op[0]->value.i[c0] / op[1]->value.i[c1];
447 break;
448 case GLSL_TYPE_FLOAT:
449 data.f[c] = op[0]->value.f[c0] / op[1]->value.f[c1];
450 break;
451 default:
452 assert(0);
Eric Anholtd251b922010-04-01 18:35:42 -1000453 }
Kenneth Graunkedad35eb2010-07-06 02:56:36 -0700454 }
Ian Romanickf8b88be2010-06-11 16:23:52 -0700455
Eric Anholtd251b922010-04-01 18:35:42 -1000456 break;
Eric Anholta576f9d2010-03-31 16:25:12 -1000457 case ir_binop_logic_and:
Ian Romanickf8b88be2010-06-11 16:23:52 -0700458 assert(op[0]->type->base_type == GLSL_TYPE_BOOL);
Kenneth Graunkef14e5962010-07-06 16:26:11 -0700459 for (unsigned c = 0; c < ir->operands[0]->type->components(); c++)
Ian Romanick4daaab62010-06-11 16:08:47 -0700460 data.b[c] = op[0]->value.b[c] && op[1]->value.b[c];
Eric Anholta576f9d2010-03-31 16:25:12 -1000461 break;
Eric Anholtd251b922010-04-01 18:35:42 -1000462 case ir_binop_logic_xor:
Ian Romanickf8b88be2010-06-11 16:23:52 -0700463 assert(op[0]->type->base_type == GLSL_TYPE_BOOL);
Kenneth Graunkef14e5962010-07-06 16:26:11 -0700464 for (unsigned c = 0; c < ir->operands[0]->type->components(); c++)
Ian Romanick4daaab62010-06-11 16:08:47 -0700465 data.b[c] = op[0]->value.b[c] ^ op[1]->value.b[c];
Eric Anholtd251b922010-04-01 18:35:42 -1000466 break;
Eric Anholta576f9d2010-03-31 16:25:12 -1000467 case ir_binop_logic_or:
Ian Romanickf8b88be2010-06-11 16:23:52 -0700468 assert(op[0]->type->base_type == GLSL_TYPE_BOOL);
Kenneth Graunkef14e5962010-07-06 16:26:11 -0700469 for (unsigned c = 0; c < ir->operands[0]->type->components(); c++)
Ian Romanick4daaab62010-06-11 16:08:47 -0700470 data.b[c] = op[0]->value.b[c] || op[1]->value.b[c];
Eric Anholta576f9d2010-03-31 16:25:12 -1000471 break;
Eric Anholt85171c22010-04-06 09:55:45 -0700472
473 case ir_binop_less:
Eric Anholt85171c22010-04-06 09:55:45 -0700474 switch (ir->operands[0]->type->base_type) {
475 case GLSL_TYPE_UINT:
Ian Romanick4daaab62010-06-11 16:08:47 -0700476 data.b[0] = op[0]->value.u[0] < op[1]->value.u[0];
Eric Anholt85171c22010-04-06 09:55:45 -0700477 break;
478 case GLSL_TYPE_INT:
Ian Romanick4daaab62010-06-11 16:08:47 -0700479 data.b[0] = op[0]->value.i[0] < op[1]->value.i[0];
Eric Anholt85171c22010-04-06 09:55:45 -0700480 break;
481 case GLSL_TYPE_FLOAT:
Ian Romanick4daaab62010-06-11 16:08:47 -0700482 data.b[0] = op[0]->value.f[0] < op[1]->value.f[0];
Eric Anholt85171c22010-04-06 09:55:45 -0700483 break;
484 default:
485 assert(0);
486 }
487 break;
488 case ir_binop_greater:
Eric Anholt85171c22010-04-06 09:55:45 -0700489 switch (ir->operands[0]->type->base_type) {
490 case GLSL_TYPE_UINT:
Ian Romanick4daaab62010-06-11 16:08:47 -0700491 data.b[0] = op[0]->value.u[0] > op[1]->value.u[0];
Eric Anholt85171c22010-04-06 09:55:45 -0700492 break;
493 case GLSL_TYPE_INT:
Ian Romanick4daaab62010-06-11 16:08:47 -0700494 data.b[0] = op[0]->value.i[0] > op[1]->value.i[0];
Eric Anholt85171c22010-04-06 09:55:45 -0700495 break;
496 case GLSL_TYPE_FLOAT:
Ian Romanick4daaab62010-06-11 16:08:47 -0700497 data.b[0] = op[0]->value.f[0] > op[1]->value.f[0];
Eric Anholt85171c22010-04-06 09:55:45 -0700498 break;
499 default:
500 assert(0);
501 }
502 break;
503 case ir_binop_lequal:
Eric Anholt85171c22010-04-06 09:55:45 -0700504 switch (ir->operands[0]->type->base_type) {
505 case GLSL_TYPE_UINT:
Ian Romanick4daaab62010-06-11 16:08:47 -0700506 data.b[0] = op[0]->value.u[0] <= op[1]->value.u[0];
Eric Anholt85171c22010-04-06 09:55:45 -0700507 break;
508 case GLSL_TYPE_INT:
Ian Romanick4daaab62010-06-11 16:08:47 -0700509 data.b[0] = op[0]->value.i[0] <= op[1]->value.i[0];
Eric Anholt85171c22010-04-06 09:55:45 -0700510 break;
511 case GLSL_TYPE_FLOAT:
Ian Romanick4daaab62010-06-11 16:08:47 -0700512 data.b[0] = op[0]->value.f[0] <= op[1]->value.f[0];
Eric Anholt85171c22010-04-06 09:55:45 -0700513 break;
514 default:
515 assert(0);
516 }
517 break;
518 case ir_binop_gequal:
Eric Anholt85171c22010-04-06 09:55:45 -0700519 switch (ir->operands[0]->type->base_type) {
520 case GLSL_TYPE_UINT:
Ian Romanick4daaab62010-06-11 16:08:47 -0700521 data.b[0] = op[0]->value.u[0] >= op[1]->value.u[0];
Eric Anholt85171c22010-04-06 09:55:45 -0700522 break;
523 case GLSL_TYPE_INT:
Ian Romanick4daaab62010-06-11 16:08:47 -0700524 data.b[0] = op[0]->value.i[0] >= op[1]->value.i[0];
Eric Anholt85171c22010-04-06 09:55:45 -0700525 break;
526 case GLSL_TYPE_FLOAT:
Ian Romanick4daaab62010-06-11 16:08:47 -0700527 data.b[0] = op[0]->value.f[0] >= op[1]->value.f[0];
Eric Anholt85171c22010-04-06 09:55:45 -0700528 break;
529 default:
530 assert(0);
531 }
532 break;
533
Eric Anholtec1949e2010-04-06 10:02:27 -0700534 case ir_binop_equal:
Ian Romanick083d75a2010-06-11 16:20:43 -0700535 data.b[0] = true;
Kenneth Graunkef14e5962010-07-06 16:26:11 -0700536 for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) {
Ian Romanick083d75a2010-06-11 16:20:43 -0700537 switch (ir->operands[0]->type->base_type) {
538 case GLSL_TYPE_UINT:
539 data.b[0] = data.b[0] && op[0]->value.u[c] == op[1]->value.u[c];
540 break;
541 case GLSL_TYPE_INT:
542 data.b[0] = data.b[0] && op[0]->value.i[c] == op[1]->value.i[c];
543 break;
544 case GLSL_TYPE_FLOAT:
545 data.b[0] = data.b[0] && op[0]->value.f[c] == op[1]->value.f[c];
546 break;
547 case GLSL_TYPE_BOOL:
548 data.b[0] = data.b[0] && op[0]->value.b[c] == op[1]->value.b[c];
549 break;
550 default:
551 assert(0);
Eric Anholtec1949e2010-04-06 10:02:27 -0700552 }
553 }
554 break;
555 case ir_binop_nequal:
Ian Romanick083d75a2010-06-11 16:20:43 -0700556 data.b[0] = false;
Kenneth Graunkef14e5962010-07-06 16:26:11 -0700557 for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) {
Ian Romanick083d75a2010-06-11 16:20:43 -0700558 switch (ir->operands[0]->type->base_type) {
559 case GLSL_TYPE_UINT:
560 data.b[0] = data.b[0] || op[0]->value.u[c] != op[1]->value.u[c];
561 break;
562 case GLSL_TYPE_INT:
563 data.b[0] = data.b[0] || op[0]->value.i[c] != op[1]->value.i[c];
564 break;
565 case GLSL_TYPE_FLOAT:
566 data.b[0] = data.b[0] || op[0]->value.f[c] != op[1]->value.f[c];
567 break;
568 case GLSL_TYPE_BOOL:
569 data.b[0] = data.b[0] || op[0]->value.b[c] != op[1]->value.b[c];
570 break;
571 default:
572 assert(0);
Eric Anholtec1949e2010-04-06 10:02:27 -0700573 }
574 }
575 break;
576
Eric Anholta576f9d2010-03-31 16:25:12 -1000577 default:
Ian Romanickf8b88be2010-06-11 16:23:52 -0700578 /* FINISHME: Should handle all expression types. */
579 return;
Eric Anholta576f9d2010-03-31 16:25:12 -1000580 }
Eric Anholtd98da972010-04-01 18:25:11 -1000581
Eric Anholt6b01b502010-06-24 15:18:39 -0700582 void *ctx = talloc_parent(ir);
Carl Worth1660a292010-06-23 18:11:51 -0700583 this->value = new(ctx) ir_constant(ir->type, &data);
Ian Romanick1cf43a42010-03-30 16:56:50 -0700584}
585
586
587void
Kenneth Graunke26d74cd2010-05-26 17:42:03 -0700588ir_constant_visitor::visit(ir_texture *ir)
589{
590 // FINISHME: Do stuff with texture lookups
591 (void) ir;
592 value = NULL;
593}
594
595
596void
Ian Romanick1cf43a42010-03-30 16:56:50 -0700597ir_constant_visitor::visit(ir_swizzle *ir)
598{
Ian Romanickc2ba6192010-06-11 12:30:28 -0700599 ir_constant *v = ir->val->constant_expression_value();
600
601 this->value = NULL;
602
603 if (v != NULL) {
Ian Romanick0bb70a32010-06-11 15:49:49 -0700604 ir_constant_data data;
Ian Romanickc2ba6192010-06-11 12:30:28 -0700605
606 const unsigned swiz_idx[4] = {
607 ir->mask.x, ir->mask.y, ir->mask.z, ir->mask.w
608 };
609
610 for (unsigned i = 0; i < ir->mask.num_components; i++) {
611 switch (v->type->base_type) {
612 case GLSL_TYPE_UINT:
613 case GLSL_TYPE_INT: data.u[i] = v->value.u[swiz_idx[i]]; break;
614 case GLSL_TYPE_FLOAT: data.f[i] = v->value.f[swiz_idx[i]]; break;
615 case GLSL_TYPE_BOOL: data.b[i] = v->value.b[swiz_idx[i]]; break;
616 default: assert(!"Should not get here."); break;
617 }
618 }
619
Eric Anholt6b01b502010-06-24 15:18:39 -0700620 void *ctx = talloc_parent(ir);
Carl Worth1660a292010-06-23 18:11:51 -0700621 this->value = new(ctx) ir_constant(ir->type, &data);
Ian Romanickc2ba6192010-06-11 12:30:28 -0700622 }
Ian Romanick1cf43a42010-03-30 16:56:50 -0700623}
624
625
626void
Ian Romanickc7b10462010-05-19 13:20:12 +0200627ir_constant_visitor::visit(ir_dereference_variable *ir)
Ian Romanick1cf43a42010-03-30 16:56:50 -0700628{
Ian Romanick1cf43a42010-03-30 16:56:50 -0700629 value = NULL;
Eric Anholt326c6762010-04-06 10:30:54 -0700630
Ian Romanickc7b10462010-05-19 13:20:12 +0200631 ir_variable *var = ir->variable_referenced();
632 if (var && var->constant_value)
Ian Romanickca088cc2010-07-06 17:41:02 -0700633 value = var->constant_value->clone(NULL);
Ian Romanickc7b10462010-05-19 13:20:12 +0200634}
635
636
637void
638ir_constant_visitor::visit(ir_dereference_array *ir)
639{
Carl Worth1660a292010-06-23 18:11:51 -0700640 void *ctx = talloc_parent(ir);
Ian Romanick9b92af92010-06-11 12:20:12 -0700641 ir_constant *array = ir->array->constant_expression_value();
642 ir_constant *idx = ir->array_index->constant_expression_value();
643
644 this->value = NULL;
645
646 if ((array != NULL) && (idx != NULL)) {
647 if (array->type->is_matrix()) {
648 /* Array access of a matrix results in a vector.
649 */
650 const unsigned column = idx->value.u[0];
651
652 const glsl_type *const column_type = array->type->column_type();
653
654 /* Offset in the constant matrix to the first element of the column
655 * to be extracted.
656 */
657 const unsigned mat_idx = column * column_type->vector_elements;
658
Ian Romanick0bb70a32010-06-11 15:49:49 -0700659 ir_constant_data data;
Ian Romanick9b92af92010-06-11 12:20:12 -0700660
661 switch (column_type->base_type) {
662 case GLSL_TYPE_UINT:
663 case GLSL_TYPE_INT:
664 for (unsigned i = 0; i < column_type->vector_elements; i++)
665 data.u[i] = array->value.u[mat_idx + i];
666
667 break;
668
669 case GLSL_TYPE_FLOAT:
670 for (unsigned i = 0; i < column_type->vector_elements; i++)
671 data.f[i] = array->value.f[mat_idx + i];
672
673 break;
674
675 default:
676 assert(!"Should not get here.");
677 break;
678 }
679
Carl Worth1660a292010-06-23 18:11:51 -0700680 this->value = new(ctx) ir_constant(column_type, &data);
Ian Romanick9b92af92010-06-11 12:20:12 -0700681 } else if (array->type->is_vector()) {
682 const unsigned component = idx->value.u[0];
683
Carl Worth1660a292010-06-23 18:11:51 -0700684 this->value = new(ctx) ir_constant(array, component);
Ian Romanick9b92af92010-06-11 12:20:12 -0700685 } else {
686 /* FINISHME: Handle access of constant arrays. */
687 }
688 }
Ian Romanickc7b10462010-05-19 13:20:12 +0200689}
690
691
692void
693ir_constant_visitor::visit(ir_dereference_record *ir)
694{
Ian Romanick253dede2010-06-09 17:30:19 -0700695 ir_constant *v = ir->record->constant_expression_value();
696
697 this->value = (v != NULL) ? v->get_record_field(ir->field) : NULL;
Ian Romanick1cf43a42010-03-30 16:56:50 -0700698}
699
700
701void
702ir_constant_visitor::visit(ir_assignment *ir)
703{
704 (void) ir;
705 value = NULL;
706}
707
708
709void
710ir_constant_visitor::visit(ir_constant *ir)
711{
712 value = ir;
713}
714
715
716void
717ir_constant_visitor::visit(ir_call *ir)
718{
719 (void) ir;
720 value = NULL;
721}
722
723
724void
725ir_constant_visitor::visit(ir_return *ir)
726{
727 (void) ir;
728 value = NULL;
729}
730
731
732void
Kenneth Graunke16efab12010-06-30 10:47:34 -0700733ir_constant_visitor::visit(ir_discard *ir)
734{
735 (void) ir;
736 value = NULL;
737}
738
739
740void
Ian Romanick1cf43a42010-03-30 16:56:50 -0700741ir_constant_visitor::visit(ir_if *ir)
742{
743 (void) ir;
744 value = NULL;
745}
Ian Romanickfad607a2010-04-05 16:16:07 -0700746
747
748void
749ir_constant_visitor::visit(ir_loop *ir)
750{
751 (void) ir;
752 value = NULL;
753}
Ian Romanickf8e31e02010-04-05 16:28:15 -0700754
755
756void
757ir_constant_visitor::visit(ir_loop_jump *ir)
758{
759 (void) ir;
760 value = NULL;
761}