blob: 7e276e1c257783aff0ef73e18680119de970dcc1 [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
Kenneth Graunke79fed372010-07-09 11:53:56 -070041#define min(x,y) (x) < (y) ? (x) : (y)
42#define max(x,y) (x) > (y) ? (x) : (y)
43
Ian Romanick1cf43a42010-03-30 16:56:50 -070044/**
45 * Visitor class for evaluating constant expressions
46 */
47class ir_constant_visitor : public ir_visitor {
48public:
49 ir_constant_visitor()
50 : value(NULL)
51 {
52 /* empty */
53 }
54
55 virtual ~ir_constant_visitor()
56 {
57 /* empty */
58 }
59
60 /**
61 * \name Visit methods
62 *
63 * As typical for the visitor pattern, there must be one \c visit method for
64 * each concrete subclass of \c ir_instruction. Virtual base classes within
65 * the hierarchy should not have \c visit methods.
66 */
67 /*@{*/
68 virtual void visit(ir_variable *);
Ian Romanick1cf43a42010-03-30 16:56:50 -070069 virtual void visit(ir_function_signature *);
70 virtual void visit(ir_function *);
71 virtual void visit(ir_expression *);
Kenneth Graunke26d74cd2010-05-26 17:42:03 -070072 virtual void visit(ir_texture *);
Ian Romanick1cf43a42010-03-30 16:56:50 -070073 virtual void visit(ir_swizzle *);
Ian Romanickc7b10462010-05-19 13:20:12 +020074 virtual void visit(ir_dereference_variable *);
75 virtual void visit(ir_dereference_array *);
76 virtual void visit(ir_dereference_record *);
Ian Romanick1cf43a42010-03-30 16:56:50 -070077 virtual void visit(ir_assignment *);
78 virtual void visit(ir_constant *);
79 virtual void visit(ir_call *);
80 virtual void visit(ir_return *);
Kenneth Graunke16efab12010-06-30 10:47:34 -070081 virtual void visit(ir_discard *);
Ian Romanick1cf43a42010-03-30 16:56:50 -070082 virtual void visit(ir_if *);
Ian Romanickfad607a2010-04-05 16:16:07 -070083 virtual void visit(ir_loop *);
Ian Romanickf8e31e02010-04-05 16:28:15 -070084 virtual void visit(ir_loop_jump *);
Ian Romanick1cf43a42010-03-30 16:56:50 -070085 /*@}*/
86
87 /**
88 * Value of the constant expression.
89 *
90 * \note
91 * This field will be \c NULL if the expression is not constant valued.
92 */
93 /* FINIHSME: This cannot hold values for constant arrays or structures. */
94 ir_constant *value;
95};
96
97
98ir_constant *
99ir_instruction::constant_expression_value()
100{
101 ir_constant_visitor visitor;
102
103 this->accept(& visitor);
104 return visitor.value;
105}
106
107
108void
109ir_constant_visitor::visit(ir_variable *ir)
110{
111 (void) ir;
112 value = NULL;
113}
114
115
116void
Ian Romanick1cf43a42010-03-30 16:56:50 -0700117ir_constant_visitor::visit(ir_function_signature *ir)
118{
119 (void) ir;
120 value = NULL;
121}
122
123
124void
125ir_constant_visitor::visit(ir_function *ir)
126{
127 (void) ir;
128 value = NULL;
129}
130
Ian Romanick1cf43a42010-03-30 16:56:50 -0700131void
132ir_constant_visitor::visit(ir_expression *ir)
133{
Ian Romanick1cf43a42010-03-30 16:56:50 -0700134 value = NULL;
Kenneth Graunke6bc432e2010-07-02 17:12:23 -0700135 ir_constant *op[2] = { NULL, NULL };
Ian Romanick4daaab62010-06-11 16:08:47 -0700136 ir_constant_data data;
Eric Anholt160d0922010-04-01 18:07:08 -1000137
Kenneth Graunkec63a1db2010-07-05 22:33:35 -0700138 memset(&data, 0, sizeof(data));
139
Kenneth Graunkef14e5962010-07-06 16:26:11 -0700140 for (unsigned operand = 0; operand < ir->get_num_operands(); operand++) {
Eric Anholtd98da972010-04-01 18:25:11 -1000141 op[operand] = ir->operands[operand]->constant_expression_value();
142 if (!op[operand])
Eric Anholt160d0922010-04-01 18:07:08 -1000143 return;
144 }
Eric Anholta576f9d2010-03-31 16:25:12 -1000145
Kenneth Graunke6fc983b2010-07-06 02:39:57 -0700146 if (op[1] != NULL)
147 assert(op[0]->type->base_type == op[1]->type->base_type);
148
Kenneth Graunkee74dcd72010-07-06 02:48:16 -0700149 bool op0_scalar = op[0]->type->is_scalar();
150 bool op1_scalar = op[1] != NULL && op[1]->type->is_scalar();
151
152 /* When iterating over a vector or matrix's components, we want to increase
153 * the loop counter. However, for scalars, we want to stay at 0.
154 */
Kenneth Graunkeacf88f22010-07-07 12:08:23 -0700155 unsigned c0_inc = op0_scalar ? 0 : 1;
156 unsigned c1_inc = op1_scalar ? 0 : 1;
Eric Anholt570dc0d2010-07-07 09:07:09 -0700157 unsigned components;
158 if (op1_scalar || !op[1]) {
159 components = op[0]->type->components();
160 } else {
161 components = op[1]->type->components();
162 }
Kenneth Graunkee74dcd72010-07-06 02:48:16 -0700163
Eric Anholta576f9d2010-03-31 16:25:12 -1000164 switch (ir->operation) {
Eric Anholt528bb852010-03-31 21:09:02 -1000165 case ir_unop_logic_not:
Ian Romanickf8b88be2010-06-11 16:23:52 -0700166 assert(op[0]->type->base_type == GLSL_TYPE_BOOL);
Kenneth Graunkef14e5962010-07-06 16:26:11 -0700167 for (unsigned c = 0; c < ir->operands[0]->type->components(); c++)
Ian Romanick4daaab62010-06-11 16:08:47 -0700168 data.b[c] = !op[0]->value.b[c];
Eric Anholt528bb852010-03-31 21:09:02 -1000169 break;
Eric Anholtaf186412010-04-06 10:53:57 -0700170
171 case ir_unop_f2i:
172 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
Kenneth Graunkef14e5962010-07-06 16:26:11 -0700173 for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) {
Ian Romanick4daaab62010-06-11 16:08:47 -0700174 data.i[c] = op[0]->value.f[c];
Eric Anholtaf186412010-04-06 10:53:57 -0700175 }
176 break;
177 case ir_unop_i2f:
178 assert(op[0]->type->base_type == GLSL_TYPE_UINT ||
179 op[0]->type->base_type == GLSL_TYPE_INT);
Kenneth Graunkef14e5962010-07-06 16:26:11 -0700180 for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) {
Eric Anholtaf186412010-04-06 10:53:57 -0700181 if (op[0]->type->base_type == GLSL_TYPE_INT)
Ian Romanick4daaab62010-06-11 16:08:47 -0700182 data.f[c] = op[0]->value.i[c];
Eric Anholtaf186412010-04-06 10:53:57 -0700183 else
Ian Romanick4daaab62010-06-11 16:08:47 -0700184 data.f[c] = op[0]->value.u[c];
Eric Anholtaf186412010-04-06 10:53:57 -0700185 }
186 break;
Ian Romanick7dc2b712010-06-07 15:10:14 -0700187 case ir_unop_b2f:
188 assert(op[0]->type->base_type == GLSL_TYPE_BOOL);
Kenneth Graunkef14e5962010-07-06 16:26:11 -0700189 for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) {
Ian Romanick4daaab62010-06-11 16:08:47 -0700190 data.f[c] = op[0]->value.b[c] ? 1.0 : 0.0;
Ian Romanick7dc2b712010-06-07 15:10:14 -0700191 }
192 break;
193 case ir_unop_f2b:
194 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
Kenneth Graunkef14e5962010-07-06 16:26:11 -0700195 for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) {
Ian Romanick4daaab62010-06-11 16:08:47 -0700196 data.b[c] = bool(op[0]->value.f[c]);
Ian Romanick7dc2b712010-06-07 15:10:14 -0700197 }
198 break;
Ian Romanick39d6dd32010-06-11 13:46:30 -0700199 case ir_unop_b2i:
200 assert(op[0]->type->base_type == GLSL_TYPE_BOOL);
Kenneth Graunkef14e5962010-07-06 16:26:11 -0700201 for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) {
Ian Romanick4daaab62010-06-11 16:08:47 -0700202 data.u[c] = op[0]->value.b[c] ? 1 : 0;
Ian Romanick39d6dd32010-06-11 13:46:30 -0700203 }
204 break;
205 case ir_unop_i2b:
206 assert(op[0]->type->is_integer());
Kenneth Graunkef14e5962010-07-06 16:26:11 -0700207 for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) {
Ian Romanick4daaab62010-06-11 16:08:47 -0700208 data.b[c] = bool(op[0]->value.u[c]);
Ian Romanick39d6dd32010-06-11 13:46:30 -0700209 }
210 break;
Eric Anholtaf186412010-04-06 10:53:57 -0700211
Kenneth Graunke323d9092010-07-08 23:21:36 -0700212 case ir_unop_trunc:
213 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
214 for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) {
215 data.f[c] = truncf(op[0]->value.f[c]);
216 }
217 break;
218
Kenneth Graunkec1ee30a2010-07-08 23:22:36 -0700219 case ir_unop_ceil:
220 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
221 for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) {
222 data.f[c] = ceilf(op[0]->value.f[c]);
223 }
224 break;
225
Kenneth Graunke07472042010-07-08 23:23:23 -0700226 case ir_unop_floor:
227 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
228 for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) {
229 data.f[c] = floorf(op[0]->value.f[c]);
230 }
231 break;
232
Eric Anholtd925c912010-07-01 10:37:11 -0700233 case ir_unop_fract:
Kenneth Graunkef14e5962010-07-06 16:26:11 -0700234 for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) {
Eric Anholtd925c912010-07-01 10:37:11 -0700235 switch (ir->type->base_type) {
236 case GLSL_TYPE_UINT:
237 data.u[c] = 0;
238 break;
239 case GLSL_TYPE_INT:
240 data.i[c] = 0;
241 break;
242 case GLSL_TYPE_FLOAT:
243 data.f[c] = op[0]->value.f[c] - floor(op[0]->value.f[c]);
244 break;
245 default:
246 assert(0);
247 }
248 }
249 break;
250
Kenneth Graunke908afd12010-07-08 23:28:50 -0700251 case ir_unop_sin:
252 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
253 for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) {
254 data.f[c] = sinf(op[0]->value.f[c]);
255 }
256 break;
257
Kenneth Graunke3fab3762010-07-08 23:29:37 -0700258 case ir_unop_cos:
259 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
260 for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) {
261 data.f[c] = cosf(op[0]->value.f[c]);
262 }
263 break;
264
Eric Anholt43ad37a2010-05-12 14:42:21 -0700265 case ir_unop_neg:
Kenneth Graunkef14e5962010-07-06 16:26:11 -0700266 for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) {
Ian Romanickf8b88be2010-06-11 16:23:52 -0700267 switch (ir->type->base_type) {
Eric Anholt43ad37a2010-05-12 14:42:21 -0700268 case GLSL_TYPE_UINT:
Ian Romanick4daaab62010-06-11 16:08:47 -0700269 data.u[c] = -op[0]->value.u[c];
Eric Anholt43ad37a2010-05-12 14:42:21 -0700270 break;
271 case GLSL_TYPE_INT:
Ian Romanick4daaab62010-06-11 16:08:47 -0700272 data.i[c] = -op[0]->value.i[c];
Eric Anholt43ad37a2010-05-12 14:42:21 -0700273 break;
274 case GLSL_TYPE_FLOAT:
Ian Romanick4daaab62010-06-11 16:08:47 -0700275 data.f[c] = -op[0]->value.f[c];
Eric Anholt43ad37a2010-05-12 14:42:21 -0700276 break;
277 default:
278 assert(0);
279 }
280 }
281 break;
282
283 case ir_unop_abs:
Kenneth Graunkef14e5962010-07-06 16:26:11 -0700284 for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) {
Ian Romanickf8b88be2010-06-11 16:23:52 -0700285 switch (ir->type->base_type) {
Eric Anholt43ad37a2010-05-12 14:42:21 -0700286 case GLSL_TYPE_UINT:
Ian Romanick4daaab62010-06-11 16:08:47 -0700287 data.u[c] = op[0]->value.u[c];
Eric Anholt43ad37a2010-05-12 14:42:21 -0700288 break;
289 case GLSL_TYPE_INT:
Ian Romanick4daaab62010-06-11 16:08:47 -0700290 data.i[c] = op[0]->value.i[c];
291 if (data.i[c] < 0)
292 data.i[c] = -data.i[c];
Eric Anholt43ad37a2010-05-12 14:42:21 -0700293 break;
294 case GLSL_TYPE_FLOAT:
Ian Romanick4daaab62010-06-11 16:08:47 -0700295 data.f[c] = fabs(op[0]->value.f[c]);
Eric Anholt43ad37a2010-05-12 14:42:21 -0700296 break;
297 default:
298 assert(0);
299 }
300 }
301 break;
302
Kenneth Graunke14b7b262010-07-08 23:11:14 -0700303 case ir_unop_sign:
304 for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) {
305 switch (ir->type->base_type) {
306 case GLSL_TYPE_UINT:
307 data.u[c] = op[0]->value.i[c] > 0;
308 break;
309 case GLSL_TYPE_INT:
310 data.i[c] = (op[0]->value.i[c] > 0) - (op[0]->value.i[c] < 0);
311 break;
312 case GLSL_TYPE_FLOAT:
313 data.f[c] = float((op[0]->value.f[c] > 0)-(op[0]->value.f[c] < 0));
314 break;
315 default:
316 assert(0);
317 }
318 }
319 break;
320
Eric Anholt43ad37a2010-05-12 14:42:21 -0700321 case ir_unop_rcp:
322 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
Kenneth Graunkef14e5962010-07-06 16:26:11 -0700323 for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) {
Ian Romanickf8b88be2010-06-11 16:23:52 -0700324 switch (ir->type->base_type) {
Eric Anholt43ad37a2010-05-12 14:42:21 -0700325 case GLSL_TYPE_UINT:
326 if (op[0]->value.u[c] != 0.0)
Ian Romanick4daaab62010-06-11 16:08:47 -0700327 data.u[c] = 1 / op[0]->value.u[c];
Eric Anholt43ad37a2010-05-12 14:42:21 -0700328 break;
329 case GLSL_TYPE_INT:
330 if (op[0]->value.i[c] != 0.0)
Ian Romanick4daaab62010-06-11 16:08:47 -0700331 data.i[c] = 1 / op[0]->value.i[c];
Eric Anholt43ad37a2010-05-12 14:42:21 -0700332 break;
333 case GLSL_TYPE_FLOAT:
334 if (op[0]->value.f[c] != 0.0)
Ian Romanick4daaab62010-06-11 16:08:47 -0700335 data.f[c] = 1.0 / op[0]->value.f[c];
Eric Anholt43ad37a2010-05-12 14:42:21 -0700336 break;
337 default:
338 assert(0);
339 }
340 }
341 break;
342
343 case ir_unop_rsq:
344 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
Kenneth Graunkef14e5962010-07-06 16:26:11 -0700345 for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) {
Ian Romanick4daaab62010-06-11 16:08:47 -0700346 data.f[c] = 1.0 / sqrtf(op[0]->value.f[c]);
Eric Anholt43ad37a2010-05-12 14:42:21 -0700347 }
348 break;
349
350 case ir_unop_sqrt:
351 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
Kenneth Graunkef14e5962010-07-06 16:26:11 -0700352 for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) {
Ian Romanick4daaab62010-06-11 16:08:47 -0700353 data.f[c] = sqrtf(op[0]->value.f[c]);
Eric Anholt43ad37a2010-05-12 14:42:21 -0700354 }
355 break;
356
357 case ir_unop_exp:
358 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
Kenneth Graunkef14e5962010-07-06 16:26:11 -0700359 for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) {
Ian Romanick4daaab62010-06-11 16:08:47 -0700360 data.f[c] = expf(op[0]->value.f[c]);
Eric Anholt43ad37a2010-05-12 14:42:21 -0700361 }
362 break;
363
Kenneth Graunkeaca01ed2010-07-08 23:14:32 -0700364 case ir_unop_exp2:
365 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
366 for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) {
367 data.f[c] = exp2f(op[0]->value.f[c]);
368 }
369 break;
370
Eric Anholt43ad37a2010-05-12 14:42:21 -0700371 case ir_unop_log:
372 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
Kenneth Graunkef14e5962010-07-06 16:26:11 -0700373 for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) {
Ian Romanick4daaab62010-06-11 16:08:47 -0700374 data.f[c] = logf(op[0]->value.f[c]);
Eric Anholt43ad37a2010-05-12 14:42:21 -0700375 }
376 break;
377
Kenneth Graunkecb639292010-07-08 23:18:09 -0700378 case ir_unop_log2:
379 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
380 for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) {
381 data.f[c] = log2f(op[0]->value.f[c]);
382 }
383 break;
384
Kenneth Graunked6a32d42010-06-09 15:22:35 -0700385 case ir_unop_dFdx:
386 case ir_unop_dFdy:
387 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
Kenneth Graunkef14e5962010-07-06 16:26:11 -0700388 for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) {
Ian Romanick4daaab62010-06-11 16:08:47 -0700389 data.f[c] = 0.0;
Kenneth Graunked6a32d42010-06-09 15:22:35 -0700390 }
391 break;
392
Kenneth Graunke891a0642010-07-08 23:35:09 -0700393 case ir_binop_pow:
394 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
395 for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) {
396 data.f[c] = powf(op[0]->value.f[c], op[1]->value.f[c]);
397 }
398 break;
399
Kenneth Graunke3f4a0b82010-07-05 21:15:32 -0700400 case ir_binop_dot:
401 assert(op[0]->type->is_vector() && op[1]->type->is_vector());
402 data.f[0] = 0;
Kenneth Graunkef14e5962010-07-06 16:26:11 -0700403 for (unsigned c = 0; c < op[0]->type->components(); c++) {
Kenneth Graunke3f4a0b82010-07-05 21:15:32 -0700404 switch (ir->operands[0]->type->base_type) {
405 case GLSL_TYPE_UINT:
406 data.u[0] += op[0]->value.u[c] * op[1]->value.u[c];
407 break;
408 case GLSL_TYPE_INT:
409 data.i[0] += op[0]->value.i[c] * op[1]->value.i[c];
410 break;
411 case GLSL_TYPE_FLOAT:
412 data.f[0] += op[0]->value.f[c] * op[1]->value.f[c];
413 break;
414 default:
415 assert(0);
416 }
417 }
418
419 break;
Kenneth Graunke79fed372010-07-09 11:53:56 -0700420 case ir_binop_min:
421 assert(op[0]->type == op[1]->type || op0_scalar || op1_scalar);
422 for (unsigned c = 0, c0 = 0, c1 = 0;
423 c < components;
424 c0 += c0_inc, c1 += c1_inc, c++) {
425
426 switch (ir->operands[0]->type->base_type) {
427 case GLSL_TYPE_UINT:
428 data.u[c] = min(op[0]->value.u[c0], op[1]->value.u[c1]);
429 break;
430 case GLSL_TYPE_INT:
431 data.i[c] = min(op[0]->value.i[c0], op[1]->value.i[c1]);
432 break;
433 case GLSL_TYPE_FLOAT:
434 data.f[c] = min(op[0]->value.f[c0], op[1]->value.f[c1]);
435 break;
436 default:
437 assert(0);
438 }
439 }
440
441 break;
442 case ir_binop_max:
443 assert(op[0]->type == op[1]->type || op0_scalar || op1_scalar);
444 for (unsigned c = 0, c0 = 0, c1 = 0;
445 c < components;
446 c0 += c0_inc, c1 += c1_inc, c++) {
447
448 switch (ir->operands[0]->type->base_type) {
449 case GLSL_TYPE_UINT:
450 data.u[c] = max(op[0]->value.u[c0], op[1]->value.u[c1]);
451 break;
452 case GLSL_TYPE_INT:
453 data.i[c] = max(op[0]->value.i[c0], op[1]->value.i[c1]);
454 break;
455 case GLSL_TYPE_FLOAT:
456 data.f[c] = max(op[0]->value.f[c0], op[1]->value.f[c1]);
457 break;
458 default:
459 assert(0);
460 }
461 }
462
463 break;
Eric Anholtd251b922010-04-01 18:35:42 -1000464 case ir_binop_add:
Kenneth Graunkee74dcd72010-07-06 02:48:16 -0700465 assert(op[0]->type == op[1]->type || op0_scalar || op1_scalar);
466 for (unsigned c = 0, c0 = 0, c1 = 0;
467 c < components;
468 c0 += c0_inc, c1 += c1_inc, c++) {
469
470 switch (ir->operands[0]->type->base_type) {
471 case GLSL_TYPE_UINT:
472 data.u[c] = op[0]->value.u[c0] + op[1]->value.u[c1];
473 break;
474 case GLSL_TYPE_INT:
475 data.i[c] = op[0]->value.i[c0] + op[1]->value.i[c1];
476 break;
477 case GLSL_TYPE_FLOAT:
478 data.f[c] = op[0]->value.f[c0] + op[1]->value.f[c1];
479 break;
480 default:
481 assert(0);
Eric Anholtd251b922010-04-01 18:35:42 -1000482 }
Kenneth Graunkee74dcd72010-07-06 02:48:16 -0700483 }
Ian Romanickf8b88be2010-06-11 16:23:52 -0700484
Eric Anholtd251b922010-04-01 18:35:42 -1000485 break;
486 case ir_binop_sub:
Kenneth Graunke97b44f02010-07-06 02:53:29 -0700487 assert(op[0]->type == op[1]->type || op0_scalar || op1_scalar);
488 for (unsigned c = 0, c0 = 0, c1 = 0;
489 c < components;
490 c0 += c0_inc, c1 += c1_inc, c++) {
491
492 switch (ir->operands[0]->type->base_type) {
493 case GLSL_TYPE_UINT:
494 data.u[c] = op[0]->value.u[c0] - op[1]->value.u[c1];
495 break;
496 case GLSL_TYPE_INT:
497 data.i[c] = op[0]->value.i[c0] - op[1]->value.i[c1];
498 break;
499 case GLSL_TYPE_FLOAT:
500 data.f[c] = op[0]->value.f[c0] - op[1]->value.f[c1];
501 break;
502 default:
503 assert(0);
Eric Anholtd251b922010-04-01 18:35:42 -1000504 }
Kenneth Graunke97b44f02010-07-06 02:53:29 -0700505 }
Ian Romanickf8b88be2010-06-11 16:23:52 -0700506
Eric Anholtd251b922010-04-01 18:35:42 -1000507 break;
Eric Anholta576f9d2010-03-31 16:25:12 -1000508 case ir_binop_mul:
Kenneth Graunkecf80a4d2010-07-05 23:19:56 -0700509 /* Check for equal types, or unequal types involving scalars */
Kenneth Graunke37b3f9d2010-07-06 03:01:15 -0700510 if ((op[0]->type == op[1]->type && !op[0]->type->is_matrix())
511 || op0_scalar || op1_scalar) {
512 for (unsigned c = 0, c0 = 0, c1 = 0;
513 c < components;
514 c0 += c0_inc, c1 += c1_inc, c++) {
515
Eric Anholtd98da972010-04-01 18:25:11 -1000516 switch (ir->operands[0]->type->base_type) {
517 case GLSL_TYPE_UINT:
Kenneth Graunke37b3f9d2010-07-06 03:01:15 -0700518 data.u[c] = op[0]->value.u[c0] * op[1]->value.u[c1];
Eric Anholtd98da972010-04-01 18:25:11 -1000519 break;
520 case GLSL_TYPE_INT:
Kenneth Graunke37b3f9d2010-07-06 03:01:15 -0700521 data.i[c] = op[0]->value.i[c0] * op[1]->value.i[c1];
Eric Anholtd98da972010-04-01 18:25:11 -1000522 break;
523 case GLSL_TYPE_FLOAT:
Kenneth Graunke37b3f9d2010-07-06 03:01:15 -0700524 data.f[c] = op[0]->value.f[c0] * op[1]->value.f[c1];
Eric Anholtd98da972010-04-01 18:25:11 -1000525 break;
526 default:
527 assert(0);
528 }
Eric Anholta576f9d2010-03-31 16:25:12 -1000529 }
Kenneth Graunkecf80a4d2010-07-05 23:19:56 -0700530 } else {
531 assert(op[0]->type->is_matrix() || op[1]->type->is_matrix());
532
533 /* Multiply an N-by-M matrix with an M-by-P matrix. Since either
534 * matrix can be a GLSL vector, either N or P can be 1.
535 *
536 * For vec*mat, the vector is treated as a row vector. This
537 * means the vector is a 1-row x M-column matrix.
538 *
539 * For mat*vec, the vector is treated as a column vector. Since
540 * matrix_columns is 1 for vectors, this just works.
541 */
542 const unsigned n = op[0]->type->is_vector()
543 ? 1 : op[0]->type->vector_elements;
544 const unsigned m = op[1]->type->vector_elements;
545 const unsigned p = op[1]->type->matrix_columns;
546 for (unsigned j = 0; j < p; j++) {
547 for (unsigned i = 0; i < n; i++) {
548 for (unsigned k = 0; k < m; k++) {
549 data.f[i+n*j] += op[0]->value.f[i+n*k]*op[1]->value.f[k+m*j];
550 }
551 }
552 }
553 }
Ian Romanickf8b88be2010-06-11 16:23:52 -0700554
Eric Anholta576f9d2010-03-31 16:25:12 -1000555 break;
Eric Anholtd251b922010-04-01 18:35:42 -1000556 case ir_binop_div:
Kenneth Graunkedad35eb2010-07-06 02:56:36 -0700557 assert(op[0]->type == op[1]->type || op0_scalar || op1_scalar);
558 for (unsigned c = 0, c0 = 0, c1 = 0;
559 c < components;
560 c0 += c0_inc, c1 += c1_inc, c++) {
561
562 switch (ir->operands[0]->type->base_type) {
563 case GLSL_TYPE_UINT:
564 data.u[c] = op[0]->value.u[c0] / op[1]->value.u[c1];
565 break;
566 case GLSL_TYPE_INT:
567 data.i[c] = op[0]->value.i[c0] / op[1]->value.i[c1];
568 break;
569 case GLSL_TYPE_FLOAT:
570 data.f[c] = op[0]->value.f[c0] / op[1]->value.f[c1];
571 break;
572 default:
573 assert(0);
Eric Anholtd251b922010-04-01 18:35:42 -1000574 }
Kenneth Graunkedad35eb2010-07-06 02:56:36 -0700575 }
Ian Romanickf8b88be2010-06-11 16:23:52 -0700576
Eric Anholtd251b922010-04-01 18:35:42 -1000577 break;
Eric Anholta576f9d2010-03-31 16:25:12 -1000578 case ir_binop_logic_and:
Ian Romanickf8b88be2010-06-11 16:23:52 -0700579 assert(op[0]->type->base_type == GLSL_TYPE_BOOL);
Kenneth Graunkef14e5962010-07-06 16:26:11 -0700580 for (unsigned c = 0; c < ir->operands[0]->type->components(); c++)
Ian Romanick4daaab62010-06-11 16:08:47 -0700581 data.b[c] = op[0]->value.b[c] && op[1]->value.b[c];
Eric Anholta576f9d2010-03-31 16:25:12 -1000582 break;
Eric Anholtd251b922010-04-01 18:35:42 -1000583 case ir_binop_logic_xor:
Ian Romanickf8b88be2010-06-11 16:23:52 -0700584 assert(op[0]->type->base_type == GLSL_TYPE_BOOL);
Kenneth Graunkef14e5962010-07-06 16:26:11 -0700585 for (unsigned c = 0; c < ir->operands[0]->type->components(); c++)
Ian Romanick4daaab62010-06-11 16:08:47 -0700586 data.b[c] = op[0]->value.b[c] ^ op[1]->value.b[c];
Eric Anholtd251b922010-04-01 18:35:42 -1000587 break;
Eric Anholta576f9d2010-03-31 16:25:12 -1000588 case ir_binop_logic_or:
Ian Romanickf8b88be2010-06-11 16:23:52 -0700589 assert(op[0]->type->base_type == GLSL_TYPE_BOOL);
Kenneth Graunkef14e5962010-07-06 16:26:11 -0700590 for (unsigned c = 0; c < ir->operands[0]->type->components(); c++)
Ian Romanick4daaab62010-06-11 16:08:47 -0700591 data.b[c] = op[0]->value.b[c] || op[1]->value.b[c];
Eric Anholta576f9d2010-03-31 16:25:12 -1000592 break;
Eric Anholt85171c22010-04-06 09:55:45 -0700593
594 case ir_binop_less:
Eric Anholt85171c22010-04-06 09:55:45 -0700595 switch (ir->operands[0]->type->base_type) {
596 case GLSL_TYPE_UINT:
Ian Romanick4daaab62010-06-11 16:08:47 -0700597 data.b[0] = op[0]->value.u[0] < op[1]->value.u[0];
Eric Anholt85171c22010-04-06 09:55:45 -0700598 break;
599 case GLSL_TYPE_INT:
Ian Romanick4daaab62010-06-11 16:08:47 -0700600 data.b[0] = op[0]->value.i[0] < op[1]->value.i[0];
Eric Anholt85171c22010-04-06 09:55:45 -0700601 break;
602 case GLSL_TYPE_FLOAT:
Ian Romanick4daaab62010-06-11 16:08:47 -0700603 data.b[0] = op[0]->value.f[0] < op[1]->value.f[0];
Eric Anholt85171c22010-04-06 09:55:45 -0700604 break;
605 default:
606 assert(0);
607 }
608 break;
609 case ir_binop_greater:
Eric Anholt85171c22010-04-06 09:55:45 -0700610 switch (ir->operands[0]->type->base_type) {
611 case GLSL_TYPE_UINT:
Ian Romanick4daaab62010-06-11 16:08:47 -0700612 data.b[0] = op[0]->value.u[0] > op[1]->value.u[0];
Eric Anholt85171c22010-04-06 09:55:45 -0700613 break;
614 case GLSL_TYPE_INT:
Ian Romanick4daaab62010-06-11 16:08:47 -0700615 data.b[0] = op[0]->value.i[0] > op[1]->value.i[0];
Eric Anholt85171c22010-04-06 09:55:45 -0700616 break;
617 case GLSL_TYPE_FLOAT:
Ian Romanick4daaab62010-06-11 16:08:47 -0700618 data.b[0] = op[0]->value.f[0] > op[1]->value.f[0];
Eric Anholt85171c22010-04-06 09:55:45 -0700619 break;
620 default:
621 assert(0);
622 }
623 break;
624 case ir_binop_lequal:
Eric Anholt85171c22010-04-06 09:55:45 -0700625 switch (ir->operands[0]->type->base_type) {
626 case GLSL_TYPE_UINT:
Ian Romanick4daaab62010-06-11 16:08:47 -0700627 data.b[0] = op[0]->value.u[0] <= op[1]->value.u[0];
Eric Anholt85171c22010-04-06 09:55:45 -0700628 break;
629 case GLSL_TYPE_INT:
Ian Romanick4daaab62010-06-11 16:08:47 -0700630 data.b[0] = op[0]->value.i[0] <= op[1]->value.i[0];
Eric Anholt85171c22010-04-06 09:55:45 -0700631 break;
632 case GLSL_TYPE_FLOAT:
Ian Romanick4daaab62010-06-11 16:08:47 -0700633 data.b[0] = op[0]->value.f[0] <= op[1]->value.f[0];
Eric Anholt85171c22010-04-06 09:55:45 -0700634 break;
635 default:
636 assert(0);
637 }
638 break;
639 case ir_binop_gequal:
Eric Anholt85171c22010-04-06 09:55:45 -0700640 switch (ir->operands[0]->type->base_type) {
641 case GLSL_TYPE_UINT:
Ian Romanick4daaab62010-06-11 16:08:47 -0700642 data.b[0] = op[0]->value.u[0] >= op[1]->value.u[0];
Eric Anholt85171c22010-04-06 09:55:45 -0700643 break;
644 case GLSL_TYPE_INT:
Ian Romanick4daaab62010-06-11 16:08:47 -0700645 data.b[0] = op[0]->value.i[0] >= op[1]->value.i[0];
Eric Anholt85171c22010-04-06 09:55:45 -0700646 break;
647 case GLSL_TYPE_FLOAT:
Ian Romanick4daaab62010-06-11 16:08:47 -0700648 data.b[0] = op[0]->value.f[0] >= op[1]->value.f[0];
Eric Anholt85171c22010-04-06 09:55:45 -0700649 break;
650 default:
651 assert(0);
652 }
653 break;
654
Eric Anholtec1949e2010-04-06 10:02:27 -0700655 case ir_binop_equal:
Ian Romanick083d75a2010-06-11 16:20:43 -0700656 data.b[0] = true;
Kenneth Graunkef14e5962010-07-06 16:26:11 -0700657 for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) {
Ian Romanick083d75a2010-06-11 16:20:43 -0700658 switch (ir->operands[0]->type->base_type) {
659 case GLSL_TYPE_UINT:
660 data.b[0] = data.b[0] && op[0]->value.u[c] == op[1]->value.u[c];
661 break;
662 case GLSL_TYPE_INT:
663 data.b[0] = data.b[0] && op[0]->value.i[c] == op[1]->value.i[c];
664 break;
665 case GLSL_TYPE_FLOAT:
666 data.b[0] = data.b[0] && op[0]->value.f[c] == op[1]->value.f[c];
667 break;
668 case GLSL_TYPE_BOOL:
669 data.b[0] = data.b[0] && op[0]->value.b[c] == op[1]->value.b[c];
670 break;
671 default:
672 assert(0);
Eric Anholtec1949e2010-04-06 10:02:27 -0700673 }
674 }
675 break;
676 case ir_binop_nequal:
Ian Romanick083d75a2010-06-11 16:20:43 -0700677 data.b[0] = false;
Kenneth Graunkef14e5962010-07-06 16:26:11 -0700678 for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) {
Ian Romanick083d75a2010-06-11 16:20:43 -0700679 switch (ir->operands[0]->type->base_type) {
680 case GLSL_TYPE_UINT:
681 data.b[0] = data.b[0] || op[0]->value.u[c] != op[1]->value.u[c];
682 break;
683 case GLSL_TYPE_INT:
684 data.b[0] = data.b[0] || op[0]->value.i[c] != op[1]->value.i[c];
685 break;
686 case GLSL_TYPE_FLOAT:
687 data.b[0] = data.b[0] || op[0]->value.f[c] != op[1]->value.f[c];
688 break;
689 case GLSL_TYPE_BOOL:
690 data.b[0] = data.b[0] || op[0]->value.b[c] != op[1]->value.b[c];
691 break;
692 default:
693 assert(0);
Eric Anholtec1949e2010-04-06 10:02:27 -0700694 }
695 }
696 break;
697
Eric Anholta576f9d2010-03-31 16:25:12 -1000698 default:
Ian Romanickf8b88be2010-06-11 16:23:52 -0700699 /* FINISHME: Should handle all expression types. */
700 return;
Eric Anholta576f9d2010-03-31 16:25:12 -1000701 }
Eric Anholtd98da972010-04-01 18:25:11 -1000702
Eric Anholt6b01b502010-06-24 15:18:39 -0700703 void *ctx = talloc_parent(ir);
Carl Worth1660a292010-06-23 18:11:51 -0700704 this->value = new(ctx) ir_constant(ir->type, &data);
Ian Romanick1cf43a42010-03-30 16:56:50 -0700705}
706
707
708void
Kenneth Graunke26d74cd2010-05-26 17:42:03 -0700709ir_constant_visitor::visit(ir_texture *ir)
710{
711 // FINISHME: Do stuff with texture lookups
712 (void) ir;
713 value = NULL;
714}
715
716
717void
Ian Romanick1cf43a42010-03-30 16:56:50 -0700718ir_constant_visitor::visit(ir_swizzle *ir)
719{
Ian Romanickc2ba6192010-06-11 12:30:28 -0700720 ir_constant *v = ir->val->constant_expression_value();
721
722 this->value = NULL;
723
724 if (v != NULL) {
Ian Romanick0bb70a32010-06-11 15:49:49 -0700725 ir_constant_data data;
Ian Romanickc2ba6192010-06-11 12:30:28 -0700726
727 const unsigned swiz_idx[4] = {
728 ir->mask.x, ir->mask.y, ir->mask.z, ir->mask.w
729 };
730
731 for (unsigned i = 0; i < ir->mask.num_components; i++) {
732 switch (v->type->base_type) {
733 case GLSL_TYPE_UINT:
734 case GLSL_TYPE_INT: data.u[i] = v->value.u[swiz_idx[i]]; break;
735 case GLSL_TYPE_FLOAT: data.f[i] = v->value.f[swiz_idx[i]]; break;
736 case GLSL_TYPE_BOOL: data.b[i] = v->value.b[swiz_idx[i]]; break;
737 default: assert(!"Should not get here."); break;
738 }
739 }
740
Eric Anholt6b01b502010-06-24 15:18:39 -0700741 void *ctx = talloc_parent(ir);
Carl Worth1660a292010-06-23 18:11:51 -0700742 this->value = new(ctx) ir_constant(ir->type, &data);
Ian Romanickc2ba6192010-06-11 12:30:28 -0700743 }
Ian Romanick1cf43a42010-03-30 16:56:50 -0700744}
745
746
747void
Ian Romanickc7b10462010-05-19 13:20:12 +0200748ir_constant_visitor::visit(ir_dereference_variable *ir)
Ian Romanick1cf43a42010-03-30 16:56:50 -0700749{
Ian Romanick1cf43a42010-03-30 16:56:50 -0700750 value = NULL;
Eric Anholt326c6762010-04-06 10:30:54 -0700751
Ian Romanickc7b10462010-05-19 13:20:12 +0200752 ir_variable *var = ir->variable_referenced();
753 if (var && var->constant_value)
Ian Romanickca088cc2010-07-06 17:41:02 -0700754 value = var->constant_value->clone(NULL);
Ian Romanickc7b10462010-05-19 13:20:12 +0200755}
756
757
758void
759ir_constant_visitor::visit(ir_dereference_array *ir)
760{
Carl Worth1660a292010-06-23 18:11:51 -0700761 void *ctx = talloc_parent(ir);
Ian Romanick9b92af92010-06-11 12:20:12 -0700762 ir_constant *array = ir->array->constant_expression_value();
763 ir_constant *idx = ir->array_index->constant_expression_value();
764
765 this->value = NULL;
766
767 if ((array != NULL) && (idx != NULL)) {
768 if (array->type->is_matrix()) {
769 /* Array access of a matrix results in a vector.
770 */
771 const unsigned column = idx->value.u[0];
772
773 const glsl_type *const column_type = array->type->column_type();
774
775 /* Offset in the constant matrix to the first element of the column
776 * to be extracted.
777 */
778 const unsigned mat_idx = column * column_type->vector_elements;
779
Ian Romanick0bb70a32010-06-11 15:49:49 -0700780 ir_constant_data data;
Ian Romanick9b92af92010-06-11 12:20:12 -0700781
782 switch (column_type->base_type) {
783 case GLSL_TYPE_UINT:
784 case GLSL_TYPE_INT:
785 for (unsigned i = 0; i < column_type->vector_elements; i++)
786 data.u[i] = array->value.u[mat_idx + i];
787
788 break;
789
790 case GLSL_TYPE_FLOAT:
791 for (unsigned i = 0; i < column_type->vector_elements; i++)
792 data.f[i] = array->value.f[mat_idx + i];
793
794 break;
795
796 default:
797 assert(!"Should not get here.");
798 break;
799 }
800
Carl Worth1660a292010-06-23 18:11:51 -0700801 this->value = new(ctx) ir_constant(column_type, &data);
Ian Romanick9b92af92010-06-11 12:20:12 -0700802 } else if (array->type->is_vector()) {
803 const unsigned component = idx->value.u[0];
804
Carl Worth1660a292010-06-23 18:11:51 -0700805 this->value = new(ctx) ir_constant(array, component);
Ian Romanick9b92af92010-06-11 12:20:12 -0700806 } else {
807 /* FINISHME: Handle access of constant arrays. */
808 }
809 }
Ian Romanickc7b10462010-05-19 13:20:12 +0200810}
811
812
813void
814ir_constant_visitor::visit(ir_dereference_record *ir)
815{
Ian Romanick253dede2010-06-09 17:30:19 -0700816 ir_constant *v = ir->record->constant_expression_value();
817
818 this->value = (v != NULL) ? v->get_record_field(ir->field) : NULL;
Ian Romanick1cf43a42010-03-30 16:56:50 -0700819}
820
821
822void
823ir_constant_visitor::visit(ir_assignment *ir)
824{
825 (void) ir;
826 value = NULL;
827}
828
829
830void
831ir_constant_visitor::visit(ir_constant *ir)
832{
833 value = ir;
834}
835
836
837void
838ir_constant_visitor::visit(ir_call *ir)
839{
840 (void) ir;
841 value = NULL;
842}
843
844
845void
846ir_constant_visitor::visit(ir_return *ir)
847{
848 (void) ir;
849 value = NULL;
850}
851
852
853void
Kenneth Graunke16efab12010-06-30 10:47:34 -0700854ir_constant_visitor::visit(ir_discard *ir)
855{
856 (void) ir;
857 value = NULL;
858}
859
860
861void
Ian Romanick1cf43a42010-03-30 16:56:50 -0700862ir_constant_visitor::visit(ir_if *ir)
863{
864 (void) ir;
865 value = NULL;
866}
Ian Romanickfad607a2010-04-05 16:16:07 -0700867
868
869void
870ir_constant_visitor::visit(ir_loop *ir)
871{
872 (void) ir;
873 value = NULL;
874}
Ian Romanickf8e31e02010-04-05 16:28:15 -0700875
876
877void
878ir_constant_visitor::visit(ir_loop_jump *ir)
879{
880 (void) ir;
881 value = NULL;
882}