blob: 09bc5b6bbff0125736767844ef4c3b81972f3abf [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
Kenneth Graunke323d9092010-07-08 23:21:36 -0700209 case ir_unop_trunc:
210 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
211 for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) {
212 data.f[c] = truncf(op[0]->value.f[c]);
213 }
214 break;
215
Eric Anholtd925c912010-07-01 10:37:11 -0700216 case ir_unop_fract:
Kenneth Graunkef14e5962010-07-06 16:26:11 -0700217 for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) {
Eric Anholtd925c912010-07-01 10:37:11 -0700218 switch (ir->type->base_type) {
219 case GLSL_TYPE_UINT:
220 data.u[c] = 0;
221 break;
222 case GLSL_TYPE_INT:
223 data.i[c] = 0;
224 break;
225 case GLSL_TYPE_FLOAT:
226 data.f[c] = op[0]->value.f[c] - floor(op[0]->value.f[c]);
227 break;
228 default:
229 assert(0);
230 }
231 }
232 break;
233
Eric Anholt43ad37a2010-05-12 14:42:21 -0700234 case ir_unop_neg:
Kenneth Graunkef14e5962010-07-06 16:26:11 -0700235 for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) {
Ian Romanickf8b88be2010-06-11 16:23:52 -0700236 switch (ir->type->base_type) {
Eric Anholt43ad37a2010-05-12 14:42:21 -0700237 case GLSL_TYPE_UINT:
Ian Romanick4daaab62010-06-11 16:08:47 -0700238 data.u[c] = -op[0]->value.u[c];
Eric Anholt43ad37a2010-05-12 14:42:21 -0700239 break;
240 case GLSL_TYPE_INT:
Ian Romanick4daaab62010-06-11 16:08:47 -0700241 data.i[c] = -op[0]->value.i[c];
Eric Anholt43ad37a2010-05-12 14:42:21 -0700242 break;
243 case GLSL_TYPE_FLOAT:
Ian Romanick4daaab62010-06-11 16:08:47 -0700244 data.f[c] = -op[0]->value.f[c];
Eric Anholt43ad37a2010-05-12 14:42:21 -0700245 break;
246 default:
247 assert(0);
248 }
249 }
250 break;
251
252 case ir_unop_abs:
Kenneth Graunkef14e5962010-07-06 16:26:11 -0700253 for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) {
Ian Romanickf8b88be2010-06-11 16:23:52 -0700254 switch (ir->type->base_type) {
Eric Anholt43ad37a2010-05-12 14:42:21 -0700255 case GLSL_TYPE_UINT:
Ian Romanick4daaab62010-06-11 16:08:47 -0700256 data.u[c] = op[0]->value.u[c];
Eric Anholt43ad37a2010-05-12 14:42:21 -0700257 break;
258 case GLSL_TYPE_INT:
Ian Romanick4daaab62010-06-11 16:08:47 -0700259 data.i[c] = op[0]->value.i[c];
260 if (data.i[c] < 0)
261 data.i[c] = -data.i[c];
Eric Anholt43ad37a2010-05-12 14:42:21 -0700262 break;
263 case GLSL_TYPE_FLOAT:
Ian Romanick4daaab62010-06-11 16:08:47 -0700264 data.f[c] = fabs(op[0]->value.f[c]);
Eric Anholt43ad37a2010-05-12 14:42:21 -0700265 break;
266 default:
267 assert(0);
268 }
269 }
270 break;
271
Kenneth Graunke14b7b262010-07-08 23:11:14 -0700272 case ir_unop_sign:
273 for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) {
274 switch (ir->type->base_type) {
275 case GLSL_TYPE_UINT:
276 data.u[c] = op[0]->value.i[c] > 0;
277 break;
278 case GLSL_TYPE_INT:
279 data.i[c] = (op[0]->value.i[c] > 0) - (op[0]->value.i[c] < 0);
280 break;
281 case GLSL_TYPE_FLOAT:
282 data.f[c] = float((op[0]->value.f[c] > 0)-(op[0]->value.f[c] < 0));
283 break;
284 default:
285 assert(0);
286 }
287 }
288 break;
289
Eric Anholt43ad37a2010-05-12 14:42:21 -0700290 case ir_unop_rcp:
291 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
Kenneth Graunkef14e5962010-07-06 16:26:11 -0700292 for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) {
Ian Romanickf8b88be2010-06-11 16:23:52 -0700293 switch (ir->type->base_type) {
Eric Anholt43ad37a2010-05-12 14:42:21 -0700294 case GLSL_TYPE_UINT:
295 if (op[0]->value.u[c] != 0.0)
Ian Romanick4daaab62010-06-11 16:08:47 -0700296 data.u[c] = 1 / op[0]->value.u[c];
Eric Anholt43ad37a2010-05-12 14:42:21 -0700297 break;
298 case GLSL_TYPE_INT:
299 if (op[0]->value.i[c] != 0.0)
Ian Romanick4daaab62010-06-11 16:08:47 -0700300 data.i[c] = 1 / op[0]->value.i[c];
Eric Anholt43ad37a2010-05-12 14:42:21 -0700301 break;
302 case GLSL_TYPE_FLOAT:
303 if (op[0]->value.f[c] != 0.0)
Ian Romanick4daaab62010-06-11 16:08:47 -0700304 data.f[c] = 1.0 / op[0]->value.f[c];
Eric Anholt43ad37a2010-05-12 14:42:21 -0700305 break;
306 default:
307 assert(0);
308 }
309 }
310 break;
311
312 case ir_unop_rsq:
313 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
Kenneth Graunkef14e5962010-07-06 16:26:11 -0700314 for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) {
Ian Romanick4daaab62010-06-11 16:08:47 -0700315 data.f[c] = 1.0 / sqrtf(op[0]->value.f[c]);
Eric Anholt43ad37a2010-05-12 14:42:21 -0700316 }
317 break;
318
319 case ir_unop_sqrt:
320 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
Kenneth Graunkef14e5962010-07-06 16:26:11 -0700321 for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) {
Ian Romanick4daaab62010-06-11 16:08:47 -0700322 data.f[c] = sqrtf(op[0]->value.f[c]);
Eric Anholt43ad37a2010-05-12 14:42:21 -0700323 }
324 break;
325
326 case ir_unop_exp:
327 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
Kenneth Graunkef14e5962010-07-06 16:26:11 -0700328 for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) {
Ian Romanick4daaab62010-06-11 16:08:47 -0700329 data.f[c] = expf(op[0]->value.f[c]);
Eric Anholt43ad37a2010-05-12 14:42:21 -0700330 }
331 break;
332
Kenneth Graunkeaca01ed2010-07-08 23:14:32 -0700333 case ir_unop_exp2:
334 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
335 for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) {
336 data.f[c] = exp2f(op[0]->value.f[c]);
337 }
338 break;
339
Eric Anholt43ad37a2010-05-12 14:42:21 -0700340 case ir_unop_log:
341 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
Kenneth Graunkef14e5962010-07-06 16:26:11 -0700342 for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) {
Ian Romanick4daaab62010-06-11 16:08:47 -0700343 data.f[c] = logf(op[0]->value.f[c]);
Eric Anholt43ad37a2010-05-12 14:42:21 -0700344 }
345 break;
346
Kenneth Graunkecb639292010-07-08 23:18:09 -0700347 case ir_unop_log2:
348 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
349 for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) {
350 data.f[c] = log2f(op[0]->value.f[c]);
351 }
352 break;
353
Kenneth Graunked6a32d42010-06-09 15:22:35 -0700354 case ir_unop_dFdx:
355 case ir_unop_dFdy:
356 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
Kenneth Graunkef14e5962010-07-06 16:26:11 -0700357 for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) {
Ian Romanick4daaab62010-06-11 16:08:47 -0700358 data.f[c] = 0.0;
Kenneth Graunked6a32d42010-06-09 15:22:35 -0700359 }
360 break;
361
Kenneth Graunke3f4a0b82010-07-05 21:15:32 -0700362 case ir_binop_dot:
363 assert(op[0]->type->is_vector() && op[1]->type->is_vector());
364 data.f[0] = 0;
Kenneth Graunkef14e5962010-07-06 16:26:11 -0700365 for (unsigned c = 0; c < op[0]->type->components(); c++) {
Kenneth Graunke3f4a0b82010-07-05 21:15:32 -0700366 switch (ir->operands[0]->type->base_type) {
367 case GLSL_TYPE_UINT:
368 data.u[0] += op[0]->value.u[c] * op[1]->value.u[c];
369 break;
370 case GLSL_TYPE_INT:
371 data.i[0] += op[0]->value.i[c] * op[1]->value.i[c];
372 break;
373 case GLSL_TYPE_FLOAT:
374 data.f[0] += op[0]->value.f[c] * op[1]->value.f[c];
375 break;
376 default:
377 assert(0);
378 }
379 }
380
381 break;
Eric Anholtd251b922010-04-01 18:35:42 -1000382 case ir_binop_add:
Kenneth Graunkee74dcd72010-07-06 02:48:16 -0700383 assert(op[0]->type == op[1]->type || op0_scalar || op1_scalar);
384 for (unsigned c = 0, c0 = 0, c1 = 0;
385 c < components;
386 c0 += c0_inc, c1 += c1_inc, c++) {
387
388 switch (ir->operands[0]->type->base_type) {
389 case GLSL_TYPE_UINT:
390 data.u[c] = op[0]->value.u[c0] + op[1]->value.u[c1];
391 break;
392 case GLSL_TYPE_INT:
393 data.i[c] = op[0]->value.i[c0] + op[1]->value.i[c1];
394 break;
395 case GLSL_TYPE_FLOAT:
396 data.f[c] = op[0]->value.f[c0] + op[1]->value.f[c1];
397 break;
398 default:
399 assert(0);
Eric Anholtd251b922010-04-01 18:35:42 -1000400 }
Kenneth Graunkee74dcd72010-07-06 02:48:16 -0700401 }
Ian Romanickf8b88be2010-06-11 16:23:52 -0700402
Eric Anholtd251b922010-04-01 18:35:42 -1000403 break;
404 case ir_binop_sub:
Kenneth Graunke97b44f02010-07-06 02:53:29 -0700405 assert(op[0]->type == op[1]->type || op0_scalar || op1_scalar);
406 for (unsigned c = 0, c0 = 0, c1 = 0;
407 c < components;
408 c0 += c0_inc, c1 += c1_inc, c++) {
409
410 switch (ir->operands[0]->type->base_type) {
411 case GLSL_TYPE_UINT:
412 data.u[c] = op[0]->value.u[c0] - op[1]->value.u[c1];
413 break;
414 case GLSL_TYPE_INT:
415 data.i[c] = op[0]->value.i[c0] - op[1]->value.i[c1];
416 break;
417 case GLSL_TYPE_FLOAT:
418 data.f[c] = op[0]->value.f[c0] - op[1]->value.f[c1];
419 break;
420 default:
421 assert(0);
Eric Anholtd251b922010-04-01 18:35:42 -1000422 }
Kenneth Graunke97b44f02010-07-06 02:53:29 -0700423 }
Ian Romanickf8b88be2010-06-11 16:23:52 -0700424
Eric Anholtd251b922010-04-01 18:35:42 -1000425 break;
Eric Anholta576f9d2010-03-31 16:25:12 -1000426 case ir_binop_mul:
Kenneth Graunkecf80a4d2010-07-05 23:19:56 -0700427 /* Check for equal types, or unequal types involving scalars */
Kenneth Graunke37b3f9d2010-07-06 03:01:15 -0700428 if ((op[0]->type == op[1]->type && !op[0]->type->is_matrix())
429 || op0_scalar || op1_scalar) {
430 for (unsigned c = 0, c0 = 0, c1 = 0;
431 c < components;
432 c0 += c0_inc, c1 += c1_inc, c++) {
433
Eric Anholtd98da972010-04-01 18:25:11 -1000434 switch (ir->operands[0]->type->base_type) {
435 case GLSL_TYPE_UINT:
Kenneth Graunke37b3f9d2010-07-06 03:01:15 -0700436 data.u[c] = op[0]->value.u[c0] * op[1]->value.u[c1];
Eric Anholtd98da972010-04-01 18:25:11 -1000437 break;
438 case GLSL_TYPE_INT:
Kenneth Graunke37b3f9d2010-07-06 03:01:15 -0700439 data.i[c] = op[0]->value.i[c0] * op[1]->value.i[c1];
Eric Anholtd98da972010-04-01 18:25:11 -1000440 break;
441 case GLSL_TYPE_FLOAT:
Kenneth Graunke37b3f9d2010-07-06 03:01:15 -0700442 data.f[c] = op[0]->value.f[c0] * op[1]->value.f[c1];
Eric Anholtd98da972010-04-01 18:25:11 -1000443 break;
444 default:
445 assert(0);
446 }
Eric Anholta576f9d2010-03-31 16:25:12 -1000447 }
Kenneth Graunkecf80a4d2010-07-05 23:19:56 -0700448 } else {
449 assert(op[0]->type->is_matrix() || op[1]->type->is_matrix());
450
451 /* Multiply an N-by-M matrix with an M-by-P matrix. Since either
452 * matrix can be a GLSL vector, either N or P can be 1.
453 *
454 * For vec*mat, the vector is treated as a row vector. This
455 * means the vector is a 1-row x M-column matrix.
456 *
457 * For mat*vec, the vector is treated as a column vector. Since
458 * matrix_columns is 1 for vectors, this just works.
459 */
460 const unsigned n = op[0]->type->is_vector()
461 ? 1 : op[0]->type->vector_elements;
462 const unsigned m = op[1]->type->vector_elements;
463 const unsigned p = op[1]->type->matrix_columns;
464 for (unsigned j = 0; j < p; j++) {
465 for (unsigned i = 0; i < n; i++) {
466 for (unsigned k = 0; k < m; k++) {
467 data.f[i+n*j] += op[0]->value.f[i+n*k]*op[1]->value.f[k+m*j];
468 }
469 }
470 }
471 }
Ian Romanickf8b88be2010-06-11 16:23:52 -0700472
Eric Anholta576f9d2010-03-31 16:25:12 -1000473 break;
Eric Anholtd251b922010-04-01 18:35:42 -1000474 case ir_binop_div:
Kenneth Graunkedad35eb2010-07-06 02:56:36 -0700475 assert(op[0]->type == op[1]->type || op0_scalar || op1_scalar);
476 for (unsigned c = 0, c0 = 0, c1 = 0;
477 c < components;
478 c0 += c0_inc, c1 += c1_inc, c++) {
479
480 switch (ir->operands[0]->type->base_type) {
481 case GLSL_TYPE_UINT:
482 data.u[c] = op[0]->value.u[c0] / op[1]->value.u[c1];
483 break;
484 case GLSL_TYPE_INT:
485 data.i[c] = op[0]->value.i[c0] / op[1]->value.i[c1];
486 break;
487 case GLSL_TYPE_FLOAT:
488 data.f[c] = op[0]->value.f[c0] / op[1]->value.f[c1];
489 break;
490 default:
491 assert(0);
Eric Anholtd251b922010-04-01 18:35:42 -1000492 }
Kenneth Graunkedad35eb2010-07-06 02:56:36 -0700493 }
Ian Romanickf8b88be2010-06-11 16:23:52 -0700494
Eric Anholtd251b922010-04-01 18:35:42 -1000495 break;
Eric Anholta576f9d2010-03-31 16:25:12 -1000496 case ir_binop_logic_and:
Ian Romanickf8b88be2010-06-11 16:23:52 -0700497 assert(op[0]->type->base_type == GLSL_TYPE_BOOL);
Kenneth Graunkef14e5962010-07-06 16:26:11 -0700498 for (unsigned c = 0; c < ir->operands[0]->type->components(); c++)
Ian Romanick4daaab62010-06-11 16:08:47 -0700499 data.b[c] = op[0]->value.b[c] && op[1]->value.b[c];
Eric Anholta576f9d2010-03-31 16:25:12 -1000500 break;
Eric Anholtd251b922010-04-01 18:35:42 -1000501 case ir_binop_logic_xor:
Ian Romanickf8b88be2010-06-11 16:23:52 -0700502 assert(op[0]->type->base_type == GLSL_TYPE_BOOL);
Kenneth Graunkef14e5962010-07-06 16:26:11 -0700503 for (unsigned c = 0; c < ir->operands[0]->type->components(); c++)
Ian Romanick4daaab62010-06-11 16:08:47 -0700504 data.b[c] = op[0]->value.b[c] ^ op[1]->value.b[c];
Eric Anholtd251b922010-04-01 18:35:42 -1000505 break;
Eric Anholta576f9d2010-03-31 16:25:12 -1000506 case ir_binop_logic_or:
Ian Romanickf8b88be2010-06-11 16:23:52 -0700507 assert(op[0]->type->base_type == GLSL_TYPE_BOOL);
Kenneth Graunkef14e5962010-07-06 16:26:11 -0700508 for (unsigned c = 0; c < ir->operands[0]->type->components(); c++)
Ian Romanick4daaab62010-06-11 16:08:47 -0700509 data.b[c] = op[0]->value.b[c] || op[1]->value.b[c];
Eric Anholta576f9d2010-03-31 16:25:12 -1000510 break;
Eric Anholt85171c22010-04-06 09:55:45 -0700511
512 case ir_binop_less:
Eric Anholt85171c22010-04-06 09:55:45 -0700513 switch (ir->operands[0]->type->base_type) {
514 case GLSL_TYPE_UINT:
Ian Romanick4daaab62010-06-11 16:08:47 -0700515 data.b[0] = op[0]->value.u[0] < op[1]->value.u[0];
Eric Anholt85171c22010-04-06 09:55:45 -0700516 break;
517 case GLSL_TYPE_INT:
Ian Romanick4daaab62010-06-11 16:08:47 -0700518 data.b[0] = op[0]->value.i[0] < op[1]->value.i[0];
Eric Anholt85171c22010-04-06 09:55:45 -0700519 break;
520 case GLSL_TYPE_FLOAT:
Ian Romanick4daaab62010-06-11 16:08:47 -0700521 data.b[0] = op[0]->value.f[0] < op[1]->value.f[0];
Eric Anholt85171c22010-04-06 09:55:45 -0700522 break;
523 default:
524 assert(0);
525 }
526 break;
527 case ir_binop_greater:
Eric Anholt85171c22010-04-06 09:55:45 -0700528 switch (ir->operands[0]->type->base_type) {
529 case GLSL_TYPE_UINT:
Ian Romanick4daaab62010-06-11 16:08:47 -0700530 data.b[0] = op[0]->value.u[0] > op[1]->value.u[0];
Eric Anholt85171c22010-04-06 09:55:45 -0700531 break;
532 case GLSL_TYPE_INT:
Ian Romanick4daaab62010-06-11 16:08:47 -0700533 data.b[0] = op[0]->value.i[0] > op[1]->value.i[0];
Eric Anholt85171c22010-04-06 09:55:45 -0700534 break;
535 case GLSL_TYPE_FLOAT:
Ian Romanick4daaab62010-06-11 16:08:47 -0700536 data.b[0] = op[0]->value.f[0] > op[1]->value.f[0];
Eric Anholt85171c22010-04-06 09:55:45 -0700537 break;
538 default:
539 assert(0);
540 }
541 break;
542 case ir_binop_lequal:
Eric Anholt85171c22010-04-06 09:55:45 -0700543 switch (ir->operands[0]->type->base_type) {
544 case GLSL_TYPE_UINT:
Ian Romanick4daaab62010-06-11 16:08:47 -0700545 data.b[0] = op[0]->value.u[0] <= op[1]->value.u[0];
Eric Anholt85171c22010-04-06 09:55:45 -0700546 break;
547 case GLSL_TYPE_INT:
Ian Romanick4daaab62010-06-11 16:08:47 -0700548 data.b[0] = op[0]->value.i[0] <= op[1]->value.i[0];
Eric Anholt85171c22010-04-06 09:55:45 -0700549 break;
550 case GLSL_TYPE_FLOAT:
Ian Romanick4daaab62010-06-11 16:08:47 -0700551 data.b[0] = op[0]->value.f[0] <= op[1]->value.f[0];
Eric Anholt85171c22010-04-06 09:55:45 -0700552 break;
553 default:
554 assert(0);
555 }
556 break;
557 case ir_binop_gequal:
Eric Anholt85171c22010-04-06 09:55:45 -0700558 switch (ir->operands[0]->type->base_type) {
559 case GLSL_TYPE_UINT:
Ian Romanick4daaab62010-06-11 16:08:47 -0700560 data.b[0] = op[0]->value.u[0] >= op[1]->value.u[0];
Eric Anholt85171c22010-04-06 09:55:45 -0700561 break;
562 case GLSL_TYPE_INT:
Ian Romanick4daaab62010-06-11 16:08:47 -0700563 data.b[0] = op[0]->value.i[0] >= op[1]->value.i[0];
Eric Anholt85171c22010-04-06 09:55:45 -0700564 break;
565 case GLSL_TYPE_FLOAT:
Ian Romanick4daaab62010-06-11 16:08:47 -0700566 data.b[0] = op[0]->value.f[0] >= op[1]->value.f[0];
Eric Anholt85171c22010-04-06 09:55:45 -0700567 break;
568 default:
569 assert(0);
570 }
571 break;
572
Eric Anholtec1949e2010-04-06 10:02:27 -0700573 case ir_binop_equal:
Ian Romanick083d75a2010-06-11 16:20:43 -0700574 data.b[0] = true;
Kenneth Graunkef14e5962010-07-06 16:26:11 -0700575 for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) {
Ian Romanick083d75a2010-06-11 16:20:43 -0700576 switch (ir->operands[0]->type->base_type) {
577 case GLSL_TYPE_UINT:
578 data.b[0] = data.b[0] && op[0]->value.u[c] == op[1]->value.u[c];
579 break;
580 case GLSL_TYPE_INT:
581 data.b[0] = data.b[0] && op[0]->value.i[c] == op[1]->value.i[c];
582 break;
583 case GLSL_TYPE_FLOAT:
584 data.b[0] = data.b[0] && op[0]->value.f[c] == op[1]->value.f[c];
585 break;
586 case GLSL_TYPE_BOOL:
587 data.b[0] = data.b[0] && op[0]->value.b[c] == op[1]->value.b[c];
588 break;
589 default:
590 assert(0);
Eric Anholtec1949e2010-04-06 10:02:27 -0700591 }
592 }
593 break;
594 case ir_binop_nequal:
Ian Romanick083d75a2010-06-11 16:20:43 -0700595 data.b[0] = false;
Kenneth Graunkef14e5962010-07-06 16:26:11 -0700596 for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) {
Ian Romanick083d75a2010-06-11 16:20:43 -0700597 switch (ir->operands[0]->type->base_type) {
598 case GLSL_TYPE_UINT:
599 data.b[0] = data.b[0] || op[0]->value.u[c] != op[1]->value.u[c];
600 break;
601 case GLSL_TYPE_INT:
602 data.b[0] = data.b[0] || op[0]->value.i[c] != op[1]->value.i[c];
603 break;
604 case GLSL_TYPE_FLOAT:
605 data.b[0] = data.b[0] || op[0]->value.f[c] != op[1]->value.f[c];
606 break;
607 case GLSL_TYPE_BOOL:
608 data.b[0] = data.b[0] || op[0]->value.b[c] != op[1]->value.b[c];
609 break;
610 default:
611 assert(0);
Eric Anholtec1949e2010-04-06 10:02:27 -0700612 }
613 }
614 break;
615
Eric Anholta576f9d2010-03-31 16:25:12 -1000616 default:
Ian Romanickf8b88be2010-06-11 16:23:52 -0700617 /* FINISHME: Should handle all expression types. */
618 return;
Eric Anholta576f9d2010-03-31 16:25:12 -1000619 }
Eric Anholtd98da972010-04-01 18:25:11 -1000620
Eric Anholt6b01b502010-06-24 15:18:39 -0700621 void *ctx = talloc_parent(ir);
Carl Worth1660a292010-06-23 18:11:51 -0700622 this->value = new(ctx) ir_constant(ir->type, &data);
Ian Romanick1cf43a42010-03-30 16:56:50 -0700623}
624
625
626void
Kenneth Graunke26d74cd2010-05-26 17:42:03 -0700627ir_constant_visitor::visit(ir_texture *ir)
628{
629 // FINISHME: Do stuff with texture lookups
630 (void) ir;
631 value = NULL;
632}
633
634
635void
Ian Romanick1cf43a42010-03-30 16:56:50 -0700636ir_constant_visitor::visit(ir_swizzle *ir)
637{
Ian Romanickc2ba6192010-06-11 12:30:28 -0700638 ir_constant *v = ir->val->constant_expression_value();
639
640 this->value = NULL;
641
642 if (v != NULL) {
Ian Romanick0bb70a32010-06-11 15:49:49 -0700643 ir_constant_data data;
Ian Romanickc2ba6192010-06-11 12:30:28 -0700644
645 const unsigned swiz_idx[4] = {
646 ir->mask.x, ir->mask.y, ir->mask.z, ir->mask.w
647 };
648
649 for (unsigned i = 0; i < ir->mask.num_components; i++) {
650 switch (v->type->base_type) {
651 case GLSL_TYPE_UINT:
652 case GLSL_TYPE_INT: data.u[i] = v->value.u[swiz_idx[i]]; break;
653 case GLSL_TYPE_FLOAT: data.f[i] = v->value.f[swiz_idx[i]]; break;
654 case GLSL_TYPE_BOOL: data.b[i] = v->value.b[swiz_idx[i]]; break;
655 default: assert(!"Should not get here."); break;
656 }
657 }
658
Eric Anholt6b01b502010-06-24 15:18:39 -0700659 void *ctx = talloc_parent(ir);
Carl Worth1660a292010-06-23 18:11:51 -0700660 this->value = new(ctx) ir_constant(ir->type, &data);
Ian Romanickc2ba6192010-06-11 12:30:28 -0700661 }
Ian Romanick1cf43a42010-03-30 16:56:50 -0700662}
663
664
665void
Ian Romanickc7b10462010-05-19 13:20:12 +0200666ir_constant_visitor::visit(ir_dereference_variable *ir)
Ian Romanick1cf43a42010-03-30 16:56:50 -0700667{
Ian Romanick1cf43a42010-03-30 16:56:50 -0700668 value = NULL;
Eric Anholt326c6762010-04-06 10:30:54 -0700669
Ian Romanickc7b10462010-05-19 13:20:12 +0200670 ir_variable *var = ir->variable_referenced();
671 if (var && var->constant_value)
Ian Romanickca088cc2010-07-06 17:41:02 -0700672 value = var->constant_value->clone(NULL);
Ian Romanickc7b10462010-05-19 13:20:12 +0200673}
674
675
676void
677ir_constant_visitor::visit(ir_dereference_array *ir)
678{
Carl Worth1660a292010-06-23 18:11:51 -0700679 void *ctx = talloc_parent(ir);
Ian Romanick9b92af92010-06-11 12:20:12 -0700680 ir_constant *array = ir->array->constant_expression_value();
681 ir_constant *idx = ir->array_index->constant_expression_value();
682
683 this->value = NULL;
684
685 if ((array != NULL) && (idx != NULL)) {
686 if (array->type->is_matrix()) {
687 /* Array access of a matrix results in a vector.
688 */
689 const unsigned column = idx->value.u[0];
690
691 const glsl_type *const column_type = array->type->column_type();
692
693 /* Offset in the constant matrix to the first element of the column
694 * to be extracted.
695 */
696 const unsigned mat_idx = column * column_type->vector_elements;
697
Ian Romanick0bb70a32010-06-11 15:49:49 -0700698 ir_constant_data data;
Ian Romanick9b92af92010-06-11 12:20:12 -0700699
700 switch (column_type->base_type) {
701 case GLSL_TYPE_UINT:
702 case GLSL_TYPE_INT:
703 for (unsigned i = 0; i < column_type->vector_elements; i++)
704 data.u[i] = array->value.u[mat_idx + i];
705
706 break;
707
708 case GLSL_TYPE_FLOAT:
709 for (unsigned i = 0; i < column_type->vector_elements; i++)
710 data.f[i] = array->value.f[mat_idx + i];
711
712 break;
713
714 default:
715 assert(!"Should not get here.");
716 break;
717 }
718
Carl Worth1660a292010-06-23 18:11:51 -0700719 this->value = new(ctx) ir_constant(column_type, &data);
Ian Romanick9b92af92010-06-11 12:20:12 -0700720 } else if (array->type->is_vector()) {
721 const unsigned component = idx->value.u[0];
722
Carl Worth1660a292010-06-23 18:11:51 -0700723 this->value = new(ctx) ir_constant(array, component);
Ian Romanick9b92af92010-06-11 12:20:12 -0700724 } else {
725 /* FINISHME: Handle access of constant arrays. */
726 }
727 }
Ian Romanickc7b10462010-05-19 13:20:12 +0200728}
729
730
731void
732ir_constant_visitor::visit(ir_dereference_record *ir)
733{
Ian Romanick253dede2010-06-09 17:30:19 -0700734 ir_constant *v = ir->record->constant_expression_value();
735
736 this->value = (v != NULL) ? v->get_record_field(ir->field) : NULL;
Ian Romanick1cf43a42010-03-30 16:56:50 -0700737}
738
739
740void
741ir_constant_visitor::visit(ir_assignment *ir)
742{
743 (void) ir;
744 value = NULL;
745}
746
747
748void
749ir_constant_visitor::visit(ir_constant *ir)
750{
751 value = ir;
752}
753
754
755void
756ir_constant_visitor::visit(ir_call *ir)
757{
758 (void) ir;
759 value = NULL;
760}
761
762
763void
764ir_constant_visitor::visit(ir_return *ir)
765{
766 (void) ir;
767 value = NULL;
768}
769
770
771void
Kenneth Graunke16efab12010-06-30 10:47:34 -0700772ir_constant_visitor::visit(ir_discard *ir)
773{
774 (void) ir;
775 value = NULL;
776}
777
778
779void
Ian Romanick1cf43a42010-03-30 16:56:50 -0700780ir_constant_visitor::visit(ir_if *ir)
781{
782 (void) ir;
783 value = NULL;
784}
Ian Romanickfad607a2010-04-05 16:16:07 -0700785
786
787void
788ir_constant_visitor::visit(ir_loop *ir)
789{
790 (void) ir;
791 value = NULL;
792}
Ian Romanickf8e31e02010-04-05 16:28:15 -0700793
794
795void
796ir_constant_visitor::visit(ir_loop_jump *ir)
797{
798 (void) ir;
799 value = NULL;
800}