Ian Romanick | 1cf43a4 | 2010-03-30 16:56:50 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 36 | #define NULL 0 |
Eric Anholt | 43ad37a | 2010-05-12 14:42:21 -0700 | [diff] [blame] | 37 | #include <math.h> |
Ian Romanick | 1cf43a4 | 2010-03-30 16:56:50 -0700 | [diff] [blame] | 38 | #include "ir.h" |
| 39 | #include "ir_visitor.h" |
Eric Anholt | a576f9d | 2010-03-31 16:25:12 -1000 | [diff] [blame] | 40 | #include "glsl_types.h" |
Ian Romanick | 1cf43a4 | 2010-03-30 16:56:50 -0700 | [diff] [blame] | 41 | |
| 42 | /** |
| 43 | * Visitor class for evaluating constant expressions |
| 44 | */ |
| 45 | class ir_constant_visitor : public ir_visitor { |
| 46 | public: |
| 47 | ir_constant_visitor() |
| 48 | : value(NULL) |
| 49 | { |
| 50 | /* empty */ |
| 51 | } |
| 52 | |
| 53 | virtual ~ir_constant_visitor() |
| 54 | { |
| 55 | /* empty */ |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * \name Visit methods |
| 60 | * |
| 61 | * As typical for the visitor pattern, there must be one \c visit method for |
| 62 | * each concrete subclass of \c ir_instruction. Virtual base classes within |
| 63 | * the hierarchy should not have \c visit methods. |
| 64 | */ |
| 65 | /*@{*/ |
| 66 | virtual void visit(ir_variable *); |
Ian Romanick | 1cf43a4 | 2010-03-30 16:56:50 -0700 | [diff] [blame] | 67 | virtual void visit(ir_function_signature *); |
| 68 | virtual void visit(ir_function *); |
| 69 | virtual void visit(ir_expression *); |
Kenneth Graunke | 26d74cd | 2010-05-26 17:42:03 -0700 | [diff] [blame] | 70 | virtual void visit(ir_texture *); |
Ian Romanick | 1cf43a4 | 2010-03-30 16:56:50 -0700 | [diff] [blame] | 71 | virtual void visit(ir_swizzle *); |
Ian Romanick | c7b1046 | 2010-05-19 13:20:12 +0200 | [diff] [blame] | 72 | virtual void visit(ir_dereference_variable *); |
| 73 | virtual void visit(ir_dereference_array *); |
| 74 | virtual void visit(ir_dereference_record *); |
Ian Romanick | 1cf43a4 | 2010-03-30 16:56:50 -0700 | [diff] [blame] | 75 | virtual void visit(ir_assignment *); |
| 76 | virtual void visit(ir_constant *); |
| 77 | virtual void visit(ir_call *); |
| 78 | virtual void visit(ir_return *); |
| 79 | virtual void visit(ir_if *); |
Ian Romanick | fad607a | 2010-04-05 16:16:07 -0700 | [diff] [blame] | 80 | virtual void visit(ir_loop *); |
Ian Romanick | f8e31e0 | 2010-04-05 16:28:15 -0700 | [diff] [blame] | 81 | virtual void visit(ir_loop_jump *); |
Ian Romanick | 1cf43a4 | 2010-03-30 16:56:50 -0700 | [diff] [blame] | 82 | /*@}*/ |
| 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 | |
| 95 | ir_constant * |
| 96 | ir_instruction::constant_expression_value() |
| 97 | { |
| 98 | ir_constant_visitor visitor; |
| 99 | |
| 100 | this->accept(& visitor); |
| 101 | return visitor.value; |
| 102 | } |
| 103 | |
| 104 | |
| 105 | void |
| 106 | ir_constant_visitor::visit(ir_variable *ir) |
| 107 | { |
| 108 | (void) ir; |
| 109 | value = NULL; |
| 110 | } |
| 111 | |
| 112 | |
| 113 | void |
Ian Romanick | 1cf43a4 | 2010-03-30 16:56:50 -0700 | [diff] [blame] | 114 | ir_constant_visitor::visit(ir_function_signature *ir) |
| 115 | { |
| 116 | (void) ir; |
| 117 | value = NULL; |
| 118 | } |
| 119 | |
| 120 | |
| 121 | void |
| 122 | ir_constant_visitor::visit(ir_function *ir) |
| 123 | { |
| 124 | (void) ir; |
| 125 | value = NULL; |
| 126 | } |
| 127 | |
Ian Romanick | 1cf43a4 | 2010-03-30 16:56:50 -0700 | [diff] [blame] | 128 | void |
| 129 | ir_constant_visitor::visit(ir_expression *ir) |
| 130 | { |
Ian Romanick | 1cf43a4 | 2010-03-30 16:56:50 -0700 | [diff] [blame] | 131 | value = NULL; |
Eric Anholt | a576f9d | 2010-03-31 16:25:12 -1000 | [diff] [blame] | 132 | ir_constant *op[2]; |
Eric Anholt | d98da97 | 2010-04-01 18:25:11 -1000 | [diff] [blame] | 133 | unsigned int operand, c; |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 134 | ir_constant_data data; |
Eric Anholt | d98da97 | 2010-04-01 18:25:11 -1000 | [diff] [blame] | 135 | const glsl_type *type = NULL; |
Eric Anholt | 160d092 | 2010-04-01 18:07:08 -1000 | [diff] [blame] | 136 | |
Eric Anholt | d98da97 | 2010-04-01 18:25:11 -1000 | [diff] [blame] | 137 | for (operand = 0; operand < ir->get_num_operands(); operand++) { |
| 138 | op[operand] = ir->operands[operand]->constant_expression_value(); |
| 139 | if (!op[operand]) |
Eric Anholt | 160d092 | 2010-04-01 18:07:08 -1000 | [diff] [blame] | 140 | return; |
| 141 | } |
Eric Anholt | a576f9d | 2010-03-31 16:25:12 -1000 | [diff] [blame] | 142 | |
| 143 | switch (ir->operation) { |
Eric Anholt | 528bb85 | 2010-03-31 21:09:02 -1000 | [diff] [blame] | 144 | case ir_unop_logic_not: |
Eric Anholt | d98da97 | 2010-04-01 18:25:11 -1000 | [diff] [blame] | 145 | type = ir->operands[0]->type; |
| 146 | assert(type->base_type == GLSL_TYPE_BOOL); |
| 147 | for (c = 0; c < ir->operands[0]->type->components(); c++) |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 148 | data.b[c] = !op[0]->value.b[c]; |
Eric Anholt | 528bb85 | 2010-03-31 21:09:02 -1000 | [diff] [blame] | 149 | break; |
Eric Anholt | af18641 | 2010-04-06 10:53:57 -0700 | [diff] [blame] | 150 | |
| 151 | case ir_unop_f2i: |
| 152 | assert(op[0]->type->base_type == GLSL_TYPE_FLOAT); |
| 153 | type = ir->type; |
| 154 | for (c = 0; c < ir->operands[0]->type->components(); c++) { |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 155 | data.i[c] = op[0]->value.f[c]; |
Eric Anholt | af18641 | 2010-04-06 10:53:57 -0700 | [diff] [blame] | 156 | } |
| 157 | break; |
| 158 | case ir_unop_i2f: |
| 159 | assert(op[0]->type->base_type == GLSL_TYPE_UINT || |
| 160 | op[0]->type->base_type == GLSL_TYPE_INT); |
| 161 | type = ir->type; |
| 162 | for (c = 0; c < ir->operands[0]->type->components(); c++) { |
| 163 | if (op[0]->type->base_type == GLSL_TYPE_INT) |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 164 | data.f[c] = op[0]->value.i[c]; |
Eric Anholt | af18641 | 2010-04-06 10:53:57 -0700 | [diff] [blame] | 165 | else |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 166 | data.f[c] = op[0]->value.u[c]; |
Eric Anholt | af18641 | 2010-04-06 10:53:57 -0700 | [diff] [blame] | 167 | } |
| 168 | break; |
Ian Romanick | 7dc2b71 | 2010-06-07 15:10:14 -0700 | [diff] [blame] | 169 | case ir_unop_b2f: |
| 170 | assert(op[0]->type->base_type == GLSL_TYPE_BOOL); |
| 171 | type = ir->type; |
| 172 | for (c = 0; c < ir->operands[0]->type->components(); c++) { |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 173 | data.f[c] = op[0]->value.b[c] ? 1.0 : 0.0; |
Ian Romanick | 7dc2b71 | 2010-06-07 15:10:14 -0700 | [diff] [blame] | 174 | } |
| 175 | break; |
| 176 | case ir_unop_f2b: |
| 177 | assert(op[0]->type->base_type == GLSL_TYPE_FLOAT); |
| 178 | type = ir->type; |
| 179 | for (c = 0; c < ir->operands[0]->type->components(); c++) { |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 180 | data.b[c] = bool(op[0]->value.f[c]); |
Ian Romanick | 7dc2b71 | 2010-06-07 15:10:14 -0700 | [diff] [blame] | 181 | } |
| 182 | break; |
Ian Romanick | 39d6dd3 | 2010-06-11 13:46:30 -0700 | [diff] [blame] | 183 | case ir_unop_b2i: |
| 184 | assert(op[0]->type->base_type == GLSL_TYPE_BOOL); |
| 185 | type = ir->type; |
| 186 | for (c = 0; c < ir->operands[0]->type->components(); c++) { |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 187 | data.u[c] = op[0]->value.b[c] ? 1 : 0; |
Ian Romanick | 39d6dd3 | 2010-06-11 13:46:30 -0700 | [diff] [blame] | 188 | } |
| 189 | break; |
| 190 | case ir_unop_i2b: |
| 191 | assert(op[0]->type->is_integer()); |
| 192 | type = ir->type; |
| 193 | for (c = 0; c < ir->operands[0]->type->components(); c++) { |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 194 | data.b[c] = bool(op[0]->value.u[c]); |
Ian Romanick | 39d6dd3 | 2010-06-11 13:46:30 -0700 | [diff] [blame] | 195 | } |
| 196 | break; |
Eric Anholt | af18641 | 2010-04-06 10:53:57 -0700 | [diff] [blame] | 197 | |
Eric Anholt | 43ad37a | 2010-05-12 14:42:21 -0700 | [diff] [blame] | 198 | case ir_unop_neg: |
| 199 | type = ir->type; |
| 200 | for (c = 0; c < ir->operands[0]->type->components(); c++) { |
| 201 | switch (type->base_type) { |
| 202 | case GLSL_TYPE_UINT: |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 203 | data.u[c] = -op[0]->value.u[c]; |
Eric Anholt | 43ad37a | 2010-05-12 14:42:21 -0700 | [diff] [blame] | 204 | break; |
| 205 | case GLSL_TYPE_INT: |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 206 | data.i[c] = -op[0]->value.i[c]; |
Eric Anholt | 43ad37a | 2010-05-12 14:42:21 -0700 | [diff] [blame] | 207 | break; |
| 208 | case GLSL_TYPE_FLOAT: |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 209 | data.f[c] = -op[0]->value.f[c]; |
Eric Anholt | 43ad37a | 2010-05-12 14:42:21 -0700 | [diff] [blame] | 210 | break; |
| 211 | default: |
| 212 | assert(0); |
| 213 | } |
| 214 | } |
| 215 | break; |
| 216 | |
| 217 | case ir_unop_abs: |
| 218 | assert(op[0]->type->base_type == GLSL_TYPE_FLOAT); |
| 219 | type = ir->type; |
| 220 | for (c = 0; c < ir->operands[0]->type->components(); c++) { |
| 221 | switch (type->base_type) { |
| 222 | case GLSL_TYPE_UINT: |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 223 | data.u[c] = op[0]->value.u[c]; |
Eric Anholt | 43ad37a | 2010-05-12 14:42:21 -0700 | [diff] [blame] | 224 | break; |
| 225 | case GLSL_TYPE_INT: |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 226 | data.i[c] = op[0]->value.i[c]; |
| 227 | if (data.i[c] < 0) |
| 228 | data.i[c] = -data.i[c]; |
Eric Anholt | 43ad37a | 2010-05-12 14:42:21 -0700 | [diff] [blame] | 229 | break; |
| 230 | case GLSL_TYPE_FLOAT: |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 231 | data.f[c] = fabs(op[0]->value.f[c]); |
Eric Anholt | 43ad37a | 2010-05-12 14:42:21 -0700 | [diff] [blame] | 232 | break; |
| 233 | default: |
| 234 | assert(0); |
| 235 | } |
| 236 | } |
| 237 | break; |
| 238 | |
| 239 | case ir_unop_rcp: |
| 240 | assert(op[0]->type->base_type == GLSL_TYPE_FLOAT); |
| 241 | type = ir->type; |
| 242 | for (c = 0; c < ir->operands[0]->type->components(); c++) { |
| 243 | switch (type->base_type) { |
| 244 | case GLSL_TYPE_UINT: |
| 245 | if (op[0]->value.u[c] != 0.0) |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 246 | data.u[c] = 1 / op[0]->value.u[c]; |
Eric Anholt | 43ad37a | 2010-05-12 14:42:21 -0700 | [diff] [blame] | 247 | break; |
| 248 | case GLSL_TYPE_INT: |
| 249 | if (op[0]->value.i[c] != 0.0) |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 250 | data.i[c] = 1 / op[0]->value.i[c]; |
Eric Anholt | 43ad37a | 2010-05-12 14:42:21 -0700 | [diff] [blame] | 251 | break; |
| 252 | case GLSL_TYPE_FLOAT: |
| 253 | if (op[0]->value.f[c] != 0.0) |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 254 | data.f[c] = 1.0 / op[0]->value.f[c]; |
Eric Anholt | 43ad37a | 2010-05-12 14:42:21 -0700 | [diff] [blame] | 255 | break; |
| 256 | default: |
| 257 | assert(0); |
| 258 | } |
| 259 | } |
| 260 | break; |
| 261 | |
| 262 | case ir_unop_rsq: |
| 263 | assert(op[0]->type->base_type == GLSL_TYPE_FLOAT); |
| 264 | type = ir->type; |
| 265 | for (c = 0; c < ir->operands[0]->type->components(); c++) { |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 266 | data.f[c] = 1.0 / sqrtf(op[0]->value.f[c]); |
Eric Anholt | 43ad37a | 2010-05-12 14:42:21 -0700 | [diff] [blame] | 267 | } |
| 268 | break; |
| 269 | |
| 270 | case ir_unop_sqrt: |
| 271 | assert(op[0]->type->base_type == GLSL_TYPE_FLOAT); |
| 272 | type = ir->type; |
| 273 | for (c = 0; c < ir->operands[0]->type->components(); c++) { |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 274 | data.f[c] = sqrtf(op[0]->value.f[c]); |
Eric Anholt | 43ad37a | 2010-05-12 14:42:21 -0700 | [diff] [blame] | 275 | } |
| 276 | break; |
| 277 | |
| 278 | case ir_unop_exp: |
| 279 | assert(op[0]->type->base_type == GLSL_TYPE_FLOAT); |
| 280 | type = ir->type; |
| 281 | for (c = 0; c < ir->operands[0]->type->components(); c++) { |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 282 | data.f[c] = expf(op[0]->value.f[c]); |
Eric Anholt | 43ad37a | 2010-05-12 14:42:21 -0700 | [diff] [blame] | 283 | } |
| 284 | break; |
| 285 | |
| 286 | case ir_unop_log: |
| 287 | assert(op[0]->type->base_type == GLSL_TYPE_FLOAT); |
| 288 | type = ir->type; |
| 289 | for (c = 0; c < ir->operands[0]->type->components(); c++) { |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 290 | data.f[c] = logf(op[0]->value.f[c]); |
Eric Anholt | 43ad37a | 2010-05-12 14:42:21 -0700 | [diff] [blame] | 291 | } |
| 292 | break; |
| 293 | |
Kenneth Graunke | d6a32d4 | 2010-06-09 15:22:35 -0700 | [diff] [blame] | 294 | case ir_unop_dFdx: |
| 295 | case ir_unop_dFdy: |
| 296 | assert(op[0]->type->base_type == GLSL_TYPE_FLOAT); |
| 297 | type = ir->type; |
| 298 | for (c = 0; c < ir->operands[0]->type->components(); c++) { |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 299 | data.f[c] = 0.0; |
Kenneth Graunke | d6a32d4 | 2010-06-09 15:22:35 -0700 | [diff] [blame] | 300 | } |
| 301 | break; |
| 302 | |
Eric Anholt | d251b92 | 2010-04-01 18:35:42 -1000 | [diff] [blame] | 303 | case ir_binop_add: |
| 304 | if (ir->operands[0]->type == ir->operands[1]->type) { |
| 305 | type = ir->operands[0]->type; |
| 306 | for (c = 0; c < ir->operands[0]->type->components(); c++) { |
| 307 | switch (ir->operands[0]->type->base_type) { |
| 308 | case GLSL_TYPE_UINT: |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 309 | data.u[c] = op[0]->value.u[c] + op[1]->value.u[c]; |
Eric Anholt | d251b92 | 2010-04-01 18:35:42 -1000 | [diff] [blame] | 310 | break; |
| 311 | case GLSL_TYPE_INT: |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 312 | data.i[c] = op[0]->value.i[c] + op[1]->value.i[c]; |
Eric Anholt | d251b92 | 2010-04-01 18:35:42 -1000 | [diff] [blame] | 313 | break; |
| 314 | case GLSL_TYPE_FLOAT: |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 315 | data.f[c] = op[0]->value.f[c] + op[1]->value.f[c]; |
Eric Anholt | d251b92 | 2010-04-01 18:35:42 -1000 | [diff] [blame] | 316 | break; |
| 317 | default: |
| 318 | assert(0); |
| 319 | } |
| 320 | } |
| 321 | } |
| 322 | break; |
| 323 | case ir_binop_sub: |
| 324 | if (ir->operands[0]->type == ir->operands[1]->type) { |
| 325 | type = ir->operands[0]->type; |
| 326 | for (c = 0; c < ir->operands[0]->type->components(); c++) { |
| 327 | switch (ir->operands[0]->type->base_type) { |
| 328 | case GLSL_TYPE_UINT: |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 329 | data.u[c] = op[0]->value.u[c] - op[1]->value.u[c]; |
Eric Anholt | d251b92 | 2010-04-01 18:35:42 -1000 | [diff] [blame] | 330 | break; |
| 331 | case GLSL_TYPE_INT: |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 332 | data.i[c] = op[0]->value.i[c] - op[1]->value.i[c]; |
Eric Anholt | d251b92 | 2010-04-01 18:35:42 -1000 | [diff] [blame] | 333 | break; |
| 334 | case GLSL_TYPE_FLOAT: |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 335 | data.f[c] = op[0]->value.f[c] - op[1]->value.f[c]; |
Eric Anholt | d251b92 | 2010-04-01 18:35:42 -1000 | [diff] [blame] | 336 | break; |
| 337 | default: |
| 338 | assert(0); |
| 339 | } |
| 340 | } |
| 341 | } |
| 342 | break; |
Eric Anholt | a576f9d | 2010-03-31 16:25:12 -1000 | [diff] [blame] | 343 | case ir_binop_mul: |
Eric Anholt | d98da97 | 2010-04-01 18:25:11 -1000 | [diff] [blame] | 344 | if (ir->operands[0]->type == ir->operands[1]->type && |
| 345 | !ir->operands[0]->type->is_matrix()) { |
| 346 | type = ir->operands[0]->type; |
| 347 | for (c = 0; c < ir->operands[0]->type->components(); c++) { |
| 348 | switch (ir->operands[0]->type->base_type) { |
| 349 | case GLSL_TYPE_UINT: |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 350 | data.u[c] = op[0]->value.u[c] * op[1]->value.u[c]; |
Eric Anholt | d98da97 | 2010-04-01 18:25:11 -1000 | [diff] [blame] | 351 | break; |
| 352 | case GLSL_TYPE_INT: |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 353 | data.i[c] = op[0]->value.i[c] * op[1]->value.i[c]; |
Eric Anholt | d98da97 | 2010-04-01 18:25:11 -1000 | [diff] [blame] | 354 | break; |
| 355 | case GLSL_TYPE_FLOAT: |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 356 | data.f[c] = op[0]->value.f[c] * op[1]->value.f[c]; |
Eric Anholt | d98da97 | 2010-04-01 18:25:11 -1000 | [diff] [blame] | 357 | break; |
| 358 | default: |
| 359 | assert(0); |
| 360 | } |
Eric Anholt | a576f9d | 2010-03-31 16:25:12 -1000 | [diff] [blame] | 361 | } |
| 362 | } |
| 363 | break; |
Eric Anholt | d251b92 | 2010-04-01 18:35:42 -1000 | [diff] [blame] | 364 | case ir_binop_div: |
| 365 | if (ir->operands[0]->type == ir->operands[1]->type) { |
| 366 | type = ir->operands[0]->type; |
| 367 | for (c = 0; c < ir->operands[0]->type->components(); c++) { |
| 368 | switch (ir->operands[0]->type->base_type) { |
| 369 | case GLSL_TYPE_UINT: |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 370 | data.u[c] = op[0]->value.u[c] / op[1]->value.u[c]; |
Eric Anholt | d251b92 | 2010-04-01 18:35:42 -1000 | [diff] [blame] | 371 | break; |
| 372 | case GLSL_TYPE_INT: |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 373 | data.i[c] = op[0]->value.i[c] / op[1]->value.i[c]; |
Eric Anholt | d251b92 | 2010-04-01 18:35:42 -1000 | [diff] [blame] | 374 | break; |
| 375 | case GLSL_TYPE_FLOAT: |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 376 | data.f[c] = op[0]->value.f[c] / op[1]->value.f[c]; |
Eric Anholt | d251b92 | 2010-04-01 18:35:42 -1000 | [diff] [blame] | 377 | break; |
| 378 | default: |
| 379 | assert(0); |
| 380 | } |
| 381 | } |
| 382 | } |
| 383 | break; |
Eric Anholt | a576f9d | 2010-03-31 16:25:12 -1000 | [diff] [blame] | 384 | case ir_binop_logic_and: |
Eric Anholt | d98da97 | 2010-04-01 18:25:11 -1000 | [diff] [blame] | 385 | type = ir->operands[0]->type; |
| 386 | assert(type->base_type == GLSL_TYPE_BOOL); |
| 387 | for (c = 0; c < ir->operands[0]->type->components(); c++) |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 388 | data.b[c] = op[0]->value.b[c] && op[1]->value.b[c]; |
Eric Anholt | a576f9d | 2010-03-31 16:25:12 -1000 | [diff] [blame] | 389 | break; |
Eric Anholt | d251b92 | 2010-04-01 18:35:42 -1000 | [diff] [blame] | 390 | case ir_binop_logic_xor: |
| 391 | type = ir->operands[0]->type; |
| 392 | assert(type->base_type == GLSL_TYPE_BOOL); |
| 393 | for (c = 0; c < ir->operands[0]->type->components(); c++) |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 394 | data.b[c] = op[0]->value.b[c] ^ op[1]->value.b[c]; |
Eric Anholt | d251b92 | 2010-04-01 18:35:42 -1000 | [diff] [blame] | 395 | break; |
Eric Anholt | a576f9d | 2010-03-31 16:25:12 -1000 | [diff] [blame] | 396 | case ir_binop_logic_or: |
Eric Anholt | d98da97 | 2010-04-01 18:25:11 -1000 | [diff] [blame] | 397 | type = ir->operands[0]->type; |
| 398 | assert(type->base_type == GLSL_TYPE_BOOL); |
| 399 | for (c = 0; c < ir->operands[0]->type->components(); c++) |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 400 | data.b[c] = op[0]->value.b[c] || op[1]->value.b[c]; |
Eric Anholt | a576f9d | 2010-03-31 16:25:12 -1000 | [diff] [blame] | 401 | break; |
Eric Anholt | 85171c2 | 2010-04-06 09:55:45 -0700 | [diff] [blame] | 402 | |
| 403 | case ir_binop_less: |
| 404 | type = glsl_type::bool_type; |
| 405 | switch (ir->operands[0]->type->base_type) { |
| 406 | case GLSL_TYPE_UINT: |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 407 | data.b[0] = op[0]->value.u[0] < op[1]->value.u[0]; |
Eric Anholt | 85171c2 | 2010-04-06 09:55:45 -0700 | [diff] [blame] | 408 | break; |
| 409 | case GLSL_TYPE_INT: |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 410 | data.b[0] = op[0]->value.i[0] < op[1]->value.i[0]; |
Eric Anholt | 85171c2 | 2010-04-06 09:55:45 -0700 | [diff] [blame] | 411 | break; |
| 412 | case GLSL_TYPE_FLOAT: |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 413 | data.b[0] = op[0]->value.f[0] < op[1]->value.f[0]; |
Eric Anholt | 85171c2 | 2010-04-06 09:55:45 -0700 | [diff] [blame] | 414 | break; |
| 415 | default: |
| 416 | assert(0); |
| 417 | } |
| 418 | break; |
| 419 | case ir_binop_greater: |
| 420 | type = glsl_type::bool_type; |
| 421 | switch (ir->operands[0]->type->base_type) { |
| 422 | case GLSL_TYPE_UINT: |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 423 | data.b[0] = op[0]->value.u[0] > op[1]->value.u[0]; |
Eric Anholt | 85171c2 | 2010-04-06 09:55:45 -0700 | [diff] [blame] | 424 | break; |
| 425 | case GLSL_TYPE_INT: |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 426 | data.b[0] = op[0]->value.i[0] > op[1]->value.i[0]; |
Eric Anholt | 85171c2 | 2010-04-06 09:55:45 -0700 | [diff] [blame] | 427 | break; |
| 428 | case GLSL_TYPE_FLOAT: |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 429 | data.b[0] = op[0]->value.f[0] > op[1]->value.f[0]; |
Eric Anholt | 85171c2 | 2010-04-06 09:55:45 -0700 | [diff] [blame] | 430 | break; |
| 431 | default: |
| 432 | assert(0); |
| 433 | } |
| 434 | break; |
| 435 | case ir_binop_lequal: |
| 436 | type = glsl_type::bool_type; |
| 437 | switch (ir->operands[0]->type->base_type) { |
| 438 | case GLSL_TYPE_UINT: |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 439 | data.b[0] = op[0]->value.u[0] <= op[1]->value.u[0]; |
Eric Anholt | 85171c2 | 2010-04-06 09:55:45 -0700 | [diff] [blame] | 440 | break; |
| 441 | case GLSL_TYPE_INT: |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 442 | data.b[0] = op[0]->value.i[0] <= op[1]->value.i[0]; |
Eric Anholt | 85171c2 | 2010-04-06 09:55:45 -0700 | [diff] [blame] | 443 | break; |
| 444 | case GLSL_TYPE_FLOAT: |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 445 | data.b[0] = op[0]->value.f[0] <= op[1]->value.f[0]; |
Eric Anholt | 85171c2 | 2010-04-06 09:55:45 -0700 | [diff] [blame] | 446 | break; |
| 447 | default: |
| 448 | assert(0); |
| 449 | } |
| 450 | break; |
| 451 | case ir_binop_gequal: |
| 452 | type = glsl_type::bool_type; |
| 453 | switch (ir->operands[0]->type->base_type) { |
| 454 | case GLSL_TYPE_UINT: |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 455 | data.b[0] = op[0]->value.u[0] >= op[1]->value.u[0]; |
Eric Anholt | 85171c2 | 2010-04-06 09:55:45 -0700 | [diff] [blame] | 456 | break; |
| 457 | case GLSL_TYPE_INT: |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 458 | data.b[0] = op[0]->value.i[0] >= op[1]->value.i[0]; |
Eric Anholt | 85171c2 | 2010-04-06 09:55:45 -0700 | [diff] [blame] | 459 | break; |
| 460 | case GLSL_TYPE_FLOAT: |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 461 | data.b[0] = op[0]->value.f[0] >= op[1]->value.f[0]; |
Eric Anholt | 85171c2 | 2010-04-06 09:55:45 -0700 | [diff] [blame] | 462 | break; |
| 463 | default: |
| 464 | assert(0); |
| 465 | } |
| 466 | break; |
| 467 | |
Eric Anholt | ec1949e | 2010-04-06 10:02:27 -0700 | [diff] [blame] | 468 | case ir_binop_equal: |
Ian Romanick | 083d75a | 2010-06-11 16:20:43 -0700 | [diff] [blame^] | 469 | type = glsl_type::bool_type; |
| 470 | data.b[0] = true; |
| 471 | for (c = 0; c < ir->operands[0]->type->components(); c++) { |
| 472 | switch (ir->operands[0]->type->base_type) { |
| 473 | case GLSL_TYPE_UINT: |
| 474 | data.b[0] = data.b[0] && op[0]->value.u[c] == op[1]->value.u[c]; |
| 475 | break; |
| 476 | case GLSL_TYPE_INT: |
| 477 | data.b[0] = data.b[0] && op[0]->value.i[c] == op[1]->value.i[c]; |
| 478 | break; |
| 479 | case GLSL_TYPE_FLOAT: |
| 480 | data.b[0] = data.b[0] && op[0]->value.f[c] == op[1]->value.f[c]; |
| 481 | break; |
| 482 | case GLSL_TYPE_BOOL: |
| 483 | data.b[0] = data.b[0] && op[0]->value.b[c] == op[1]->value.b[c]; |
| 484 | break; |
| 485 | default: |
| 486 | assert(0); |
Eric Anholt | ec1949e | 2010-04-06 10:02:27 -0700 | [diff] [blame] | 487 | } |
| 488 | } |
| 489 | break; |
| 490 | case ir_binop_nequal: |
Ian Romanick | 083d75a | 2010-06-11 16:20:43 -0700 | [diff] [blame^] | 491 | type = glsl_type::bool_type; |
| 492 | data.b[0] = false; |
| 493 | for (c = 0; c < ir->operands[0]->type->components(); c++) { |
| 494 | switch (ir->operands[0]->type->base_type) { |
| 495 | case GLSL_TYPE_UINT: |
| 496 | data.b[0] = data.b[0] || op[0]->value.u[c] != op[1]->value.u[c]; |
| 497 | break; |
| 498 | case GLSL_TYPE_INT: |
| 499 | data.b[0] = data.b[0] || op[0]->value.i[c] != op[1]->value.i[c]; |
| 500 | break; |
| 501 | case GLSL_TYPE_FLOAT: |
| 502 | data.b[0] = data.b[0] || op[0]->value.f[c] != op[1]->value.f[c]; |
| 503 | break; |
| 504 | case GLSL_TYPE_BOOL: |
| 505 | data.b[0] = data.b[0] || op[0]->value.b[c] != op[1]->value.b[c]; |
| 506 | break; |
| 507 | default: |
| 508 | assert(0); |
Eric Anholt | ec1949e | 2010-04-06 10:02:27 -0700 | [diff] [blame] | 509 | } |
| 510 | } |
| 511 | break; |
| 512 | |
Eric Anholt | a576f9d | 2010-03-31 16:25:12 -1000 | [diff] [blame] | 513 | default: |
| 514 | break; |
| 515 | } |
Eric Anholt | d98da97 | 2010-04-01 18:25:11 -1000 | [diff] [blame] | 516 | |
| 517 | if (type) { |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 518 | this->value = new ir_constant(type, &data); |
Eric Anholt | d98da97 | 2010-04-01 18:25:11 -1000 | [diff] [blame] | 519 | } |
Ian Romanick | 1cf43a4 | 2010-03-30 16:56:50 -0700 | [diff] [blame] | 520 | } |
| 521 | |
| 522 | |
| 523 | void |
Kenneth Graunke | 26d74cd | 2010-05-26 17:42:03 -0700 | [diff] [blame] | 524 | ir_constant_visitor::visit(ir_texture *ir) |
| 525 | { |
| 526 | // FINISHME: Do stuff with texture lookups |
| 527 | (void) ir; |
| 528 | value = NULL; |
| 529 | } |
| 530 | |
| 531 | |
| 532 | void |
Ian Romanick | 1cf43a4 | 2010-03-30 16:56:50 -0700 | [diff] [blame] | 533 | ir_constant_visitor::visit(ir_swizzle *ir) |
| 534 | { |
Ian Romanick | c2ba619 | 2010-06-11 12:30:28 -0700 | [diff] [blame] | 535 | ir_constant *v = ir->val->constant_expression_value(); |
| 536 | |
| 537 | this->value = NULL; |
| 538 | |
| 539 | if (v != NULL) { |
Ian Romanick | 0bb70a3 | 2010-06-11 15:49:49 -0700 | [diff] [blame] | 540 | ir_constant_data data; |
Ian Romanick | c2ba619 | 2010-06-11 12:30:28 -0700 | [diff] [blame] | 541 | |
| 542 | const unsigned swiz_idx[4] = { |
| 543 | ir->mask.x, ir->mask.y, ir->mask.z, ir->mask.w |
| 544 | }; |
| 545 | |
| 546 | for (unsigned i = 0; i < ir->mask.num_components; i++) { |
| 547 | switch (v->type->base_type) { |
| 548 | case GLSL_TYPE_UINT: |
| 549 | case GLSL_TYPE_INT: data.u[i] = v->value.u[swiz_idx[i]]; break; |
| 550 | case GLSL_TYPE_FLOAT: data.f[i] = v->value.f[swiz_idx[i]]; break; |
| 551 | case GLSL_TYPE_BOOL: data.b[i] = v->value.b[swiz_idx[i]]; break; |
| 552 | default: assert(!"Should not get here."); break; |
| 553 | } |
| 554 | } |
| 555 | |
| 556 | this->value = new ir_constant(ir->type, &data); |
| 557 | } |
Ian Romanick | 1cf43a4 | 2010-03-30 16:56:50 -0700 | [diff] [blame] | 558 | } |
| 559 | |
| 560 | |
| 561 | void |
Ian Romanick | c7b1046 | 2010-05-19 13:20:12 +0200 | [diff] [blame] | 562 | ir_constant_visitor::visit(ir_dereference_variable *ir) |
Ian Romanick | 1cf43a4 | 2010-03-30 16:56:50 -0700 | [diff] [blame] | 563 | { |
Ian Romanick | 1cf43a4 | 2010-03-30 16:56:50 -0700 | [diff] [blame] | 564 | value = NULL; |
Eric Anholt | 326c676 | 2010-04-06 10:30:54 -0700 | [diff] [blame] | 565 | |
Ian Romanick | c7b1046 | 2010-05-19 13:20:12 +0200 | [diff] [blame] | 566 | ir_variable *var = ir->variable_referenced(); |
| 567 | if (var && var->constant_value) |
Ian Romanick | d4b33ed | 2010-06-09 17:19:10 -0700 | [diff] [blame] | 568 | value = var->constant_value->clone(); |
Ian Romanick | c7b1046 | 2010-05-19 13:20:12 +0200 | [diff] [blame] | 569 | } |
| 570 | |
| 571 | |
| 572 | void |
| 573 | ir_constant_visitor::visit(ir_dereference_array *ir) |
| 574 | { |
Ian Romanick | 9b92af9 | 2010-06-11 12:20:12 -0700 | [diff] [blame] | 575 | ir_constant *array = ir->array->constant_expression_value(); |
| 576 | ir_constant *idx = ir->array_index->constant_expression_value(); |
| 577 | |
| 578 | this->value = NULL; |
| 579 | |
| 580 | if ((array != NULL) && (idx != NULL)) { |
| 581 | if (array->type->is_matrix()) { |
| 582 | /* Array access of a matrix results in a vector. |
| 583 | */ |
| 584 | const unsigned column = idx->value.u[0]; |
| 585 | |
| 586 | const glsl_type *const column_type = array->type->column_type(); |
| 587 | |
| 588 | /* Offset in the constant matrix to the first element of the column |
| 589 | * to be extracted. |
| 590 | */ |
| 591 | const unsigned mat_idx = column * column_type->vector_elements; |
| 592 | |
Ian Romanick | 0bb70a3 | 2010-06-11 15:49:49 -0700 | [diff] [blame] | 593 | ir_constant_data data; |
Ian Romanick | 9b92af9 | 2010-06-11 12:20:12 -0700 | [diff] [blame] | 594 | |
| 595 | switch (column_type->base_type) { |
| 596 | case GLSL_TYPE_UINT: |
| 597 | case GLSL_TYPE_INT: |
| 598 | for (unsigned i = 0; i < column_type->vector_elements; i++) |
| 599 | data.u[i] = array->value.u[mat_idx + i]; |
| 600 | |
| 601 | break; |
| 602 | |
| 603 | case GLSL_TYPE_FLOAT: |
| 604 | for (unsigned i = 0; i < column_type->vector_elements; i++) |
| 605 | data.f[i] = array->value.f[mat_idx + i]; |
| 606 | |
| 607 | break; |
| 608 | |
| 609 | default: |
| 610 | assert(!"Should not get here."); |
| 611 | break; |
| 612 | } |
| 613 | |
| 614 | this->value = new ir_constant(column_type, &data); |
| 615 | } else if (array->type->is_vector()) { |
| 616 | const unsigned component = idx->value.u[0]; |
| 617 | |
| 618 | this->value = new ir_constant(array, component); |
| 619 | } else { |
| 620 | /* FINISHME: Handle access of constant arrays. */ |
| 621 | } |
| 622 | } |
Ian Romanick | c7b1046 | 2010-05-19 13:20:12 +0200 | [diff] [blame] | 623 | } |
| 624 | |
| 625 | |
| 626 | void |
| 627 | ir_constant_visitor::visit(ir_dereference_record *ir) |
| 628 | { |
Ian Romanick | 253dede | 2010-06-09 17:30:19 -0700 | [diff] [blame] | 629 | ir_constant *v = ir->record->constant_expression_value(); |
| 630 | |
| 631 | this->value = (v != NULL) ? v->get_record_field(ir->field) : NULL; |
Ian Romanick | 1cf43a4 | 2010-03-30 16:56:50 -0700 | [diff] [blame] | 632 | } |
| 633 | |
| 634 | |
| 635 | void |
| 636 | ir_constant_visitor::visit(ir_assignment *ir) |
| 637 | { |
| 638 | (void) ir; |
| 639 | value = NULL; |
| 640 | } |
| 641 | |
| 642 | |
| 643 | void |
| 644 | ir_constant_visitor::visit(ir_constant *ir) |
| 645 | { |
| 646 | value = ir; |
| 647 | } |
| 648 | |
| 649 | |
| 650 | void |
| 651 | ir_constant_visitor::visit(ir_call *ir) |
| 652 | { |
| 653 | (void) ir; |
| 654 | value = NULL; |
| 655 | } |
| 656 | |
| 657 | |
| 658 | void |
| 659 | ir_constant_visitor::visit(ir_return *ir) |
| 660 | { |
| 661 | (void) ir; |
| 662 | value = NULL; |
| 663 | } |
| 664 | |
| 665 | |
| 666 | void |
| 667 | ir_constant_visitor::visit(ir_if *ir) |
| 668 | { |
| 669 | (void) ir; |
| 670 | value = NULL; |
| 671 | } |
Ian Romanick | fad607a | 2010-04-05 16:16:07 -0700 | [diff] [blame] | 672 | |
| 673 | |
| 674 | void |
| 675 | ir_constant_visitor::visit(ir_loop *ir) |
| 676 | { |
| 677 | (void) ir; |
| 678 | value = NULL; |
| 679 | } |
Ian Romanick | f8e31e0 | 2010-04-05 16:28:15 -0700 | [diff] [blame] | 680 | |
| 681 | |
| 682 | void |
| 683 | ir_constant_visitor::visit(ir_loop_jump *ir) |
| 684 | { |
| 685 | (void) ir; |
| 686 | value = NULL; |
| 687 | } |