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 | |
Eric Anholt | 43ad37a | 2010-05-12 14:42:21 -0700 | [diff] [blame] | 36 | #include <math.h> |
Ian Romanick | 1cf43a4 | 2010-03-30 16:56:50 -0700 | [diff] [blame] | 37 | #include "ir.h" |
| 38 | #include "ir_visitor.h" |
Eric Anholt | a576f9d | 2010-03-31 16:25:12 -1000 | [diff] [blame] | 39 | #include "glsl_types.h" |
Ian Romanick | 1cf43a4 | 2010-03-30 16:56:50 -0700 | [diff] [blame] | 40 | |
| 41 | /** |
| 42 | * Visitor class for evaluating constant expressions |
| 43 | */ |
| 44 | class ir_constant_visitor : public ir_visitor { |
| 45 | public: |
| 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 Romanick | 1cf43a4 | 2010-03-30 16:56:50 -0700 | [diff] [blame] | 66 | virtual void visit(ir_function_signature *); |
| 67 | virtual void visit(ir_function *); |
| 68 | virtual void visit(ir_expression *); |
Kenneth Graunke | 26d74cd | 2010-05-26 17:42:03 -0700 | [diff] [blame] | 69 | virtual void visit(ir_texture *); |
Ian Romanick | 1cf43a4 | 2010-03-30 16:56:50 -0700 | [diff] [blame] | 70 | virtual void visit(ir_swizzle *); |
Ian Romanick | c7b1046 | 2010-05-19 13:20:12 +0200 | [diff] [blame] | 71 | virtual void visit(ir_dereference_variable *); |
| 72 | virtual void visit(ir_dereference_array *); |
| 73 | virtual void visit(ir_dereference_record *); |
Ian Romanick | 1cf43a4 | 2010-03-30 16:56:50 -0700 | [diff] [blame] | 74 | virtual void visit(ir_assignment *); |
| 75 | virtual void visit(ir_constant *); |
| 76 | virtual void visit(ir_call *); |
| 77 | virtual void visit(ir_return *); |
Kenneth Graunke | 16efab1 | 2010-06-30 10:47:34 -0700 | [diff] [blame] | 78 | virtual void visit(ir_discard *); |
Ian Romanick | 1cf43a4 | 2010-03-30 16:56:50 -0700 | [diff] [blame] | 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; |
Kenneth Graunke | 6bc432e | 2010-07-02 17:12:23 -0700 | [diff] [blame] | 132 | ir_constant *op[2] = { NULL, NULL }; |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 133 | ir_constant_data data; |
Eric Anholt | 160d092 | 2010-04-01 18:07:08 -1000 | [diff] [blame] | 134 | |
Kenneth Graunke | c63a1db | 2010-07-05 22:33:35 -0700 | [diff] [blame] | 135 | memset(&data, 0, sizeof(data)); |
| 136 | |
Kenneth Graunke | f14e596 | 2010-07-06 16:26:11 -0700 | [diff] [blame] | 137 | for (unsigned operand = 0; operand < ir->get_num_operands(); operand++) { |
Eric Anholt | d98da97 | 2010-04-01 18:25:11 -1000 | [diff] [blame] | 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 | |
Kenneth Graunke | 6fc983b | 2010-07-06 02:39:57 -0700 | [diff] [blame] | 143 | if (op[1] != NULL) |
| 144 | assert(op[0]->type->base_type == op[1]->type->base_type); |
| 145 | |
Kenneth Graunke | e74dcd7 | 2010-07-06 02:48:16 -0700 | [diff] [blame] | 146 | 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 | */ |
| 152 | unsigned c0_inc = op0_scalar ? 1 : 0; |
| 153 | unsigned c1_inc = op1_scalar ? 1 : 0; |
Eric Anholt | 570dc0d | 2010-07-07 09:07:09 -0700 | [diff] [blame^] | 154 | unsigned components; |
| 155 | if (op1_scalar || !op[1]) { |
| 156 | components = op[0]->type->components(); |
| 157 | } else { |
| 158 | components = op[1]->type->components(); |
| 159 | } |
Kenneth Graunke | e74dcd7 | 2010-07-06 02:48:16 -0700 | [diff] [blame] | 160 | |
Eric Anholt | a576f9d | 2010-03-31 16:25:12 -1000 | [diff] [blame] | 161 | switch (ir->operation) { |
Eric Anholt | 528bb85 | 2010-03-31 21:09:02 -1000 | [diff] [blame] | 162 | case ir_unop_logic_not: |
Ian Romanick | f8b88be | 2010-06-11 16:23:52 -0700 | [diff] [blame] | 163 | assert(op[0]->type->base_type == GLSL_TYPE_BOOL); |
Kenneth Graunke | f14e596 | 2010-07-06 16:26:11 -0700 | [diff] [blame] | 164 | for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 165 | data.b[c] = !op[0]->value.b[c]; |
Eric Anholt | 528bb85 | 2010-03-31 21:09:02 -1000 | [diff] [blame] | 166 | break; |
Eric Anholt | af18641 | 2010-04-06 10:53:57 -0700 | [diff] [blame] | 167 | |
| 168 | case ir_unop_f2i: |
| 169 | assert(op[0]->type->base_type == GLSL_TYPE_FLOAT); |
Kenneth Graunke | f14e596 | 2010-07-06 16:26:11 -0700 | [diff] [blame] | 170 | for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) { |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 171 | data.i[c] = op[0]->value.f[c]; |
Eric Anholt | af18641 | 2010-04-06 10:53:57 -0700 | [diff] [blame] | 172 | } |
| 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 Graunke | f14e596 | 2010-07-06 16:26:11 -0700 | [diff] [blame] | 177 | for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) { |
Eric Anholt | af18641 | 2010-04-06 10:53:57 -0700 | [diff] [blame] | 178 | if (op[0]->type->base_type == GLSL_TYPE_INT) |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 179 | data.f[c] = op[0]->value.i[c]; |
Eric Anholt | af18641 | 2010-04-06 10:53:57 -0700 | [diff] [blame] | 180 | else |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 181 | data.f[c] = op[0]->value.u[c]; |
Eric Anholt | af18641 | 2010-04-06 10:53:57 -0700 | [diff] [blame] | 182 | } |
| 183 | break; |
Ian Romanick | 7dc2b71 | 2010-06-07 15:10:14 -0700 | [diff] [blame] | 184 | case ir_unop_b2f: |
| 185 | assert(op[0]->type->base_type == GLSL_TYPE_BOOL); |
Kenneth Graunke | f14e596 | 2010-07-06 16:26:11 -0700 | [diff] [blame] | 186 | for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) { |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 187 | data.f[c] = op[0]->value.b[c] ? 1.0 : 0.0; |
Ian Romanick | 7dc2b71 | 2010-06-07 15:10:14 -0700 | [diff] [blame] | 188 | } |
| 189 | break; |
| 190 | case ir_unop_f2b: |
| 191 | assert(op[0]->type->base_type == GLSL_TYPE_FLOAT); |
Kenneth Graunke | f14e596 | 2010-07-06 16:26:11 -0700 | [diff] [blame] | 192 | for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) { |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 193 | data.b[c] = bool(op[0]->value.f[c]); |
Ian Romanick | 7dc2b71 | 2010-06-07 15:10:14 -0700 | [diff] [blame] | 194 | } |
| 195 | break; |
Ian Romanick | 39d6dd3 | 2010-06-11 13:46:30 -0700 | [diff] [blame] | 196 | case ir_unop_b2i: |
| 197 | assert(op[0]->type->base_type == GLSL_TYPE_BOOL); |
Kenneth Graunke | f14e596 | 2010-07-06 16:26:11 -0700 | [diff] [blame] | 198 | for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) { |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 199 | data.u[c] = op[0]->value.b[c] ? 1 : 0; |
Ian Romanick | 39d6dd3 | 2010-06-11 13:46:30 -0700 | [diff] [blame] | 200 | } |
| 201 | break; |
| 202 | case ir_unop_i2b: |
| 203 | assert(op[0]->type->is_integer()); |
Kenneth Graunke | f14e596 | 2010-07-06 16:26:11 -0700 | [diff] [blame] | 204 | for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) { |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 205 | data.b[c] = bool(op[0]->value.u[c]); |
Ian Romanick | 39d6dd3 | 2010-06-11 13:46:30 -0700 | [diff] [blame] | 206 | } |
| 207 | break; |
Eric Anholt | af18641 | 2010-04-06 10:53:57 -0700 | [diff] [blame] | 208 | |
Eric Anholt | d925c91 | 2010-07-01 10:37:11 -0700 | [diff] [blame] | 209 | case ir_unop_fract: |
Kenneth Graunke | f14e596 | 2010-07-06 16:26:11 -0700 | [diff] [blame] | 210 | for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) { |
Eric Anholt | d925c91 | 2010-07-01 10:37:11 -0700 | [diff] [blame] | 211 | switch (ir->type->base_type) { |
| 212 | case GLSL_TYPE_UINT: |
| 213 | data.u[c] = 0; |
| 214 | break; |
| 215 | case GLSL_TYPE_INT: |
| 216 | data.i[c] = 0; |
| 217 | break; |
| 218 | case GLSL_TYPE_FLOAT: |
| 219 | data.f[c] = op[0]->value.f[c] - floor(op[0]->value.f[c]); |
| 220 | break; |
| 221 | default: |
| 222 | assert(0); |
| 223 | } |
| 224 | } |
| 225 | break; |
| 226 | |
Eric Anholt | 43ad37a | 2010-05-12 14:42:21 -0700 | [diff] [blame] | 227 | case ir_unop_neg: |
Kenneth Graunke | f14e596 | 2010-07-06 16:26:11 -0700 | [diff] [blame] | 228 | for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) { |
Ian Romanick | f8b88be | 2010-06-11 16:23:52 -0700 | [diff] [blame] | 229 | switch (ir->type->base_type) { |
Eric Anholt | 43ad37a | 2010-05-12 14:42:21 -0700 | [diff] [blame] | 230 | case GLSL_TYPE_UINT: |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 231 | data.u[c] = -op[0]->value.u[c]; |
Eric Anholt | 43ad37a | 2010-05-12 14:42:21 -0700 | [diff] [blame] | 232 | break; |
| 233 | case GLSL_TYPE_INT: |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 234 | data.i[c] = -op[0]->value.i[c]; |
Eric Anholt | 43ad37a | 2010-05-12 14:42:21 -0700 | [diff] [blame] | 235 | break; |
| 236 | case GLSL_TYPE_FLOAT: |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 237 | data.f[c] = -op[0]->value.f[c]; |
Eric Anholt | 43ad37a | 2010-05-12 14:42:21 -0700 | [diff] [blame] | 238 | break; |
| 239 | default: |
| 240 | assert(0); |
| 241 | } |
| 242 | } |
| 243 | break; |
| 244 | |
| 245 | case ir_unop_abs: |
| 246 | assert(op[0]->type->base_type == GLSL_TYPE_FLOAT); |
Kenneth Graunke | f14e596 | 2010-07-06 16:26:11 -0700 | [diff] [blame] | 247 | for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) { |
Ian Romanick | f8b88be | 2010-06-11 16:23:52 -0700 | [diff] [blame] | 248 | switch (ir->type->base_type) { |
Eric Anholt | 43ad37a | 2010-05-12 14:42:21 -0700 | [diff] [blame] | 249 | case GLSL_TYPE_UINT: |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 250 | data.u[c] = op[0]->value.u[c]; |
Eric Anholt | 43ad37a | 2010-05-12 14:42:21 -0700 | [diff] [blame] | 251 | break; |
| 252 | case GLSL_TYPE_INT: |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 253 | data.i[c] = op[0]->value.i[c]; |
| 254 | if (data.i[c] < 0) |
| 255 | data.i[c] = -data.i[c]; |
Eric Anholt | 43ad37a | 2010-05-12 14:42:21 -0700 | [diff] [blame] | 256 | break; |
| 257 | case GLSL_TYPE_FLOAT: |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 258 | data.f[c] = fabs(op[0]->value.f[c]); |
Eric Anholt | 43ad37a | 2010-05-12 14:42:21 -0700 | [diff] [blame] | 259 | break; |
| 260 | default: |
| 261 | assert(0); |
| 262 | } |
| 263 | } |
| 264 | break; |
| 265 | |
| 266 | case ir_unop_rcp: |
| 267 | assert(op[0]->type->base_type == GLSL_TYPE_FLOAT); |
Kenneth Graunke | f14e596 | 2010-07-06 16:26:11 -0700 | [diff] [blame] | 268 | for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) { |
Ian Romanick | f8b88be | 2010-06-11 16:23:52 -0700 | [diff] [blame] | 269 | switch (ir->type->base_type) { |
Eric Anholt | 43ad37a | 2010-05-12 14:42:21 -0700 | [diff] [blame] | 270 | case GLSL_TYPE_UINT: |
| 271 | if (op[0]->value.u[c] != 0.0) |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 272 | data.u[c] = 1 / op[0]->value.u[c]; |
Eric Anholt | 43ad37a | 2010-05-12 14:42:21 -0700 | [diff] [blame] | 273 | break; |
| 274 | case GLSL_TYPE_INT: |
| 275 | if (op[0]->value.i[c] != 0.0) |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 276 | data.i[c] = 1 / op[0]->value.i[c]; |
Eric Anholt | 43ad37a | 2010-05-12 14:42:21 -0700 | [diff] [blame] | 277 | break; |
| 278 | case GLSL_TYPE_FLOAT: |
| 279 | if (op[0]->value.f[c] != 0.0) |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 280 | data.f[c] = 1.0 / op[0]->value.f[c]; |
Eric Anholt | 43ad37a | 2010-05-12 14:42:21 -0700 | [diff] [blame] | 281 | break; |
| 282 | default: |
| 283 | assert(0); |
| 284 | } |
| 285 | } |
| 286 | break; |
| 287 | |
| 288 | case ir_unop_rsq: |
| 289 | assert(op[0]->type->base_type == GLSL_TYPE_FLOAT); |
Kenneth Graunke | f14e596 | 2010-07-06 16:26:11 -0700 | [diff] [blame] | 290 | for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) { |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 291 | data.f[c] = 1.0 / sqrtf(op[0]->value.f[c]); |
Eric Anholt | 43ad37a | 2010-05-12 14:42:21 -0700 | [diff] [blame] | 292 | } |
| 293 | break; |
| 294 | |
| 295 | case ir_unop_sqrt: |
| 296 | assert(op[0]->type->base_type == GLSL_TYPE_FLOAT); |
Kenneth Graunke | f14e596 | 2010-07-06 16:26:11 -0700 | [diff] [blame] | 297 | for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) { |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 298 | data.f[c] = sqrtf(op[0]->value.f[c]); |
Eric Anholt | 43ad37a | 2010-05-12 14:42:21 -0700 | [diff] [blame] | 299 | } |
| 300 | break; |
| 301 | |
| 302 | case ir_unop_exp: |
| 303 | assert(op[0]->type->base_type == GLSL_TYPE_FLOAT); |
Kenneth Graunke | f14e596 | 2010-07-06 16:26:11 -0700 | [diff] [blame] | 304 | for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) { |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 305 | data.f[c] = expf(op[0]->value.f[c]); |
Eric Anholt | 43ad37a | 2010-05-12 14:42:21 -0700 | [diff] [blame] | 306 | } |
| 307 | break; |
| 308 | |
| 309 | case ir_unop_log: |
| 310 | assert(op[0]->type->base_type == GLSL_TYPE_FLOAT); |
Kenneth Graunke | f14e596 | 2010-07-06 16:26:11 -0700 | [diff] [blame] | 311 | for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) { |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 312 | data.f[c] = logf(op[0]->value.f[c]); |
Eric Anholt | 43ad37a | 2010-05-12 14:42:21 -0700 | [diff] [blame] | 313 | } |
| 314 | break; |
| 315 | |
Kenneth Graunke | d6a32d4 | 2010-06-09 15:22:35 -0700 | [diff] [blame] | 316 | case ir_unop_dFdx: |
| 317 | case ir_unop_dFdy: |
| 318 | assert(op[0]->type->base_type == GLSL_TYPE_FLOAT); |
Kenneth Graunke | f14e596 | 2010-07-06 16:26:11 -0700 | [diff] [blame] | 319 | for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) { |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 320 | data.f[c] = 0.0; |
Kenneth Graunke | d6a32d4 | 2010-06-09 15:22:35 -0700 | [diff] [blame] | 321 | } |
| 322 | break; |
| 323 | |
Kenneth Graunke | 3f4a0b8 | 2010-07-05 21:15:32 -0700 | [diff] [blame] | 324 | case ir_binop_dot: |
| 325 | assert(op[0]->type->is_vector() && op[1]->type->is_vector()); |
| 326 | data.f[0] = 0; |
Kenneth Graunke | f14e596 | 2010-07-06 16:26:11 -0700 | [diff] [blame] | 327 | for (unsigned c = 0; c < op[0]->type->components(); c++) { |
Kenneth Graunke | 3f4a0b8 | 2010-07-05 21:15:32 -0700 | [diff] [blame] | 328 | switch (ir->operands[0]->type->base_type) { |
| 329 | case GLSL_TYPE_UINT: |
| 330 | data.u[0] += op[0]->value.u[c] * op[1]->value.u[c]; |
| 331 | break; |
| 332 | case GLSL_TYPE_INT: |
| 333 | data.i[0] += op[0]->value.i[c] * op[1]->value.i[c]; |
| 334 | break; |
| 335 | case GLSL_TYPE_FLOAT: |
| 336 | data.f[0] += op[0]->value.f[c] * op[1]->value.f[c]; |
| 337 | break; |
| 338 | default: |
| 339 | assert(0); |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | break; |
Eric Anholt | d251b92 | 2010-04-01 18:35:42 -1000 | [diff] [blame] | 344 | case ir_binop_add: |
Kenneth Graunke | e74dcd7 | 2010-07-06 02:48:16 -0700 | [diff] [blame] | 345 | assert(op[0]->type == op[1]->type || op0_scalar || op1_scalar); |
| 346 | for (unsigned c = 0, c0 = 0, c1 = 0; |
| 347 | c < components; |
| 348 | c0 += c0_inc, c1 += c1_inc, c++) { |
| 349 | |
| 350 | switch (ir->operands[0]->type->base_type) { |
| 351 | case GLSL_TYPE_UINT: |
| 352 | data.u[c] = op[0]->value.u[c0] + op[1]->value.u[c1]; |
| 353 | break; |
| 354 | case GLSL_TYPE_INT: |
| 355 | data.i[c] = op[0]->value.i[c0] + op[1]->value.i[c1]; |
| 356 | break; |
| 357 | case GLSL_TYPE_FLOAT: |
| 358 | data.f[c] = op[0]->value.f[c0] + op[1]->value.f[c1]; |
| 359 | break; |
| 360 | default: |
| 361 | assert(0); |
Eric Anholt | d251b92 | 2010-04-01 18:35:42 -1000 | [diff] [blame] | 362 | } |
Kenneth Graunke | e74dcd7 | 2010-07-06 02:48:16 -0700 | [diff] [blame] | 363 | } |
Ian Romanick | f8b88be | 2010-06-11 16:23:52 -0700 | [diff] [blame] | 364 | |
Eric Anholt | d251b92 | 2010-04-01 18:35:42 -1000 | [diff] [blame] | 365 | break; |
| 366 | case ir_binop_sub: |
Kenneth Graunke | 97b44f0 | 2010-07-06 02:53:29 -0700 | [diff] [blame] | 367 | assert(op[0]->type == op[1]->type || op0_scalar || op1_scalar); |
| 368 | for (unsigned c = 0, c0 = 0, c1 = 0; |
| 369 | c < components; |
| 370 | c0 += c0_inc, c1 += c1_inc, c++) { |
| 371 | |
| 372 | switch (ir->operands[0]->type->base_type) { |
| 373 | case GLSL_TYPE_UINT: |
| 374 | data.u[c] = op[0]->value.u[c0] - op[1]->value.u[c1]; |
| 375 | break; |
| 376 | case GLSL_TYPE_INT: |
| 377 | data.i[c] = op[0]->value.i[c0] - op[1]->value.i[c1]; |
| 378 | break; |
| 379 | case GLSL_TYPE_FLOAT: |
| 380 | data.f[c] = op[0]->value.f[c0] - op[1]->value.f[c1]; |
| 381 | break; |
| 382 | default: |
| 383 | assert(0); |
Eric Anholt | d251b92 | 2010-04-01 18:35:42 -1000 | [diff] [blame] | 384 | } |
Kenneth Graunke | 97b44f0 | 2010-07-06 02:53:29 -0700 | [diff] [blame] | 385 | } |
Ian Romanick | f8b88be | 2010-06-11 16:23:52 -0700 | [diff] [blame] | 386 | |
Eric Anholt | d251b92 | 2010-04-01 18:35:42 -1000 | [diff] [blame] | 387 | break; |
Eric Anholt | a576f9d | 2010-03-31 16:25:12 -1000 | [diff] [blame] | 388 | case ir_binop_mul: |
Kenneth Graunke | cf80a4d | 2010-07-05 23:19:56 -0700 | [diff] [blame] | 389 | /* Check for equal types, or unequal types involving scalars */ |
Kenneth Graunke | 37b3f9d | 2010-07-06 03:01:15 -0700 | [diff] [blame] | 390 | if ((op[0]->type == op[1]->type && !op[0]->type->is_matrix()) |
| 391 | || op0_scalar || op1_scalar) { |
| 392 | for (unsigned c = 0, c0 = 0, c1 = 0; |
| 393 | c < components; |
| 394 | c0 += c0_inc, c1 += c1_inc, c++) { |
| 395 | |
Eric Anholt | d98da97 | 2010-04-01 18:25:11 -1000 | [diff] [blame] | 396 | switch (ir->operands[0]->type->base_type) { |
| 397 | case GLSL_TYPE_UINT: |
Kenneth Graunke | 37b3f9d | 2010-07-06 03:01:15 -0700 | [diff] [blame] | 398 | data.u[c] = op[0]->value.u[c0] * op[1]->value.u[c1]; |
Eric Anholt | d98da97 | 2010-04-01 18:25:11 -1000 | [diff] [blame] | 399 | break; |
| 400 | case GLSL_TYPE_INT: |
Kenneth Graunke | 37b3f9d | 2010-07-06 03:01:15 -0700 | [diff] [blame] | 401 | data.i[c] = op[0]->value.i[c0] * op[1]->value.i[c1]; |
Eric Anholt | d98da97 | 2010-04-01 18:25:11 -1000 | [diff] [blame] | 402 | break; |
| 403 | case GLSL_TYPE_FLOAT: |
Kenneth Graunke | 37b3f9d | 2010-07-06 03:01:15 -0700 | [diff] [blame] | 404 | data.f[c] = op[0]->value.f[c0] * op[1]->value.f[c1]; |
Eric Anholt | d98da97 | 2010-04-01 18:25:11 -1000 | [diff] [blame] | 405 | break; |
| 406 | default: |
| 407 | assert(0); |
| 408 | } |
Eric Anholt | a576f9d | 2010-03-31 16:25:12 -1000 | [diff] [blame] | 409 | } |
Kenneth Graunke | cf80a4d | 2010-07-05 23:19:56 -0700 | [diff] [blame] | 410 | } else { |
| 411 | assert(op[0]->type->is_matrix() || op[1]->type->is_matrix()); |
| 412 | |
| 413 | /* Multiply an N-by-M matrix with an M-by-P matrix. Since either |
| 414 | * matrix can be a GLSL vector, either N or P can be 1. |
| 415 | * |
| 416 | * For vec*mat, the vector is treated as a row vector. This |
| 417 | * means the vector is a 1-row x M-column matrix. |
| 418 | * |
| 419 | * For mat*vec, the vector is treated as a column vector. Since |
| 420 | * matrix_columns is 1 for vectors, this just works. |
| 421 | */ |
| 422 | const unsigned n = op[0]->type->is_vector() |
| 423 | ? 1 : op[0]->type->vector_elements; |
| 424 | const unsigned m = op[1]->type->vector_elements; |
| 425 | const unsigned p = op[1]->type->matrix_columns; |
| 426 | for (unsigned j = 0; j < p; j++) { |
| 427 | for (unsigned i = 0; i < n; i++) { |
| 428 | for (unsigned k = 0; k < m; k++) { |
| 429 | data.f[i+n*j] += op[0]->value.f[i+n*k]*op[1]->value.f[k+m*j]; |
| 430 | } |
| 431 | } |
| 432 | } |
| 433 | } |
Ian Romanick | f8b88be | 2010-06-11 16:23:52 -0700 | [diff] [blame] | 434 | |
Eric Anholt | a576f9d | 2010-03-31 16:25:12 -1000 | [diff] [blame] | 435 | break; |
Eric Anholt | d251b92 | 2010-04-01 18:35:42 -1000 | [diff] [blame] | 436 | case ir_binop_div: |
Kenneth Graunke | dad35eb | 2010-07-06 02:56:36 -0700 | [diff] [blame] | 437 | assert(op[0]->type == op[1]->type || op0_scalar || op1_scalar); |
| 438 | for (unsigned c = 0, c0 = 0, c1 = 0; |
| 439 | c < components; |
| 440 | c0 += c0_inc, c1 += c1_inc, c++) { |
| 441 | |
| 442 | switch (ir->operands[0]->type->base_type) { |
| 443 | case GLSL_TYPE_UINT: |
| 444 | data.u[c] = op[0]->value.u[c0] / op[1]->value.u[c1]; |
| 445 | break; |
| 446 | case GLSL_TYPE_INT: |
| 447 | data.i[c] = op[0]->value.i[c0] / op[1]->value.i[c1]; |
| 448 | break; |
| 449 | case GLSL_TYPE_FLOAT: |
| 450 | data.f[c] = op[0]->value.f[c0] / op[1]->value.f[c1]; |
| 451 | break; |
| 452 | default: |
| 453 | assert(0); |
Eric Anholt | d251b92 | 2010-04-01 18:35:42 -1000 | [diff] [blame] | 454 | } |
Kenneth Graunke | dad35eb | 2010-07-06 02:56:36 -0700 | [diff] [blame] | 455 | } |
Ian Romanick | f8b88be | 2010-06-11 16:23:52 -0700 | [diff] [blame] | 456 | |
Eric Anholt | d251b92 | 2010-04-01 18:35:42 -1000 | [diff] [blame] | 457 | break; |
Eric Anholt | a576f9d | 2010-03-31 16:25:12 -1000 | [diff] [blame] | 458 | case ir_binop_logic_and: |
Ian Romanick | f8b88be | 2010-06-11 16:23:52 -0700 | [diff] [blame] | 459 | assert(op[0]->type->base_type == GLSL_TYPE_BOOL); |
Kenneth Graunke | f14e596 | 2010-07-06 16:26:11 -0700 | [diff] [blame] | 460 | for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 461 | 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] | 462 | break; |
Eric Anholt | d251b92 | 2010-04-01 18:35:42 -1000 | [diff] [blame] | 463 | case ir_binop_logic_xor: |
Ian Romanick | f8b88be | 2010-06-11 16:23:52 -0700 | [diff] [blame] | 464 | assert(op[0]->type->base_type == GLSL_TYPE_BOOL); |
Kenneth Graunke | f14e596 | 2010-07-06 16:26:11 -0700 | [diff] [blame] | 465 | for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 466 | 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] | 467 | break; |
Eric Anholt | a576f9d | 2010-03-31 16:25:12 -1000 | [diff] [blame] | 468 | case ir_binop_logic_or: |
Ian Romanick | f8b88be | 2010-06-11 16:23:52 -0700 | [diff] [blame] | 469 | assert(op[0]->type->base_type == GLSL_TYPE_BOOL); |
Kenneth Graunke | f14e596 | 2010-07-06 16:26:11 -0700 | [diff] [blame] | 470 | for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 471 | 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] | 472 | break; |
Eric Anholt | 85171c2 | 2010-04-06 09:55:45 -0700 | [diff] [blame] | 473 | |
| 474 | case ir_binop_less: |
Eric Anholt | 85171c2 | 2010-04-06 09:55:45 -0700 | [diff] [blame] | 475 | switch (ir->operands[0]->type->base_type) { |
| 476 | case GLSL_TYPE_UINT: |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 477 | 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] | 478 | break; |
| 479 | case GLSL_TYPE_INT: |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 480 | 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] | 481 | break; |
| 482 | case GLSL_TYPE_FLOAT: |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 483 | 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] | 484 | break; |
| 485 | default: |
| 486 | assert(0); |
| 487 | } |
| 488 | break; |
| 489 | case ir_binop_greater: |
Eric Anholt | 85171c2 | 2010-04-06 09:55:45 -0700 | [diff] [blame] | 490 | switch (ir->operands[0]->type->base_type) { |
| 491 | case GLSL_TYPE_UINT: |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 492 | 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] | 493 | break; |
| 494 | case GLSL_TYPE_INT: |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 495 | 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] | 496 | break; |
| 497 | case GLSL_TYPE_FLOAT: |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 498 | 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] | 499 | break; |
| 500 | default: |
| 501 | assert(0); |
| 502 | } |
| 503 | break; |
| 504 | case ir_binop_lequal: |
Eric Anholt | 85171c2 | 2010-04-06 09:55:45 -0700 | [diff] [blame] | 505 | switch (ir->operands[0]->type->base_type) { |
| 506 | case GLSL_TYPE_UINT: |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 507 | 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] | 508 | break; |
| 509 | case GLSL_TYPE_INT: |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 510 | 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] | 511 | break; |
| 512 | case GLSL_TYPE_FLOAT: |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 513 | 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] | 514 | break; |
| 515 | default: |
| 516 | assert(0); |
| 517 | } |
| 518 | break; |
| 519 | case ir_binop_gequal: |
Eric Anholt | 85171c2 | 2010-04-06 09:55:45 -0700 | [diff] [blame] | 520 | switch (ir->operands[0]->type->base_type) { |
| 521 | case GLSL_TYPE_UINT: |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 522 | 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] | 523 | break; |
| 524 | case GLSL_TYPE_INT: |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 525 | 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] | 526 | break; |
| 527 | case GLSL_TYPE_FLOAT: |
Ian Romanick | 4daaab6 | 2010-06-11 16:08:47 -0700 | [diff] [blame] | 528 | 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] | 529 | break; |
| 530 | default: |
| 531 | assert(0); |
| 532 | } |
| 533 | break; |
| 534 | |
Eric Anholt | ec1949e | 2010-04-06 10:02:27 -0700 | [diff] [blame] | 535 | case ir_binop_equal: |
Ian Romanick | 083d75a | 2010-06-11 16:20:43 -0700 | [diff] [blame] | 536 | data.b[0] = true; |
Kenneth Graunke | f14e596 | 2010-07-06 16:26:11 -0700 | [diff] [blame] | 537 | for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) { |
Ian Romanick | 083d75a | 2010-06-11 16:20:43 -0700 | [diff] [blame] | 538 | switch (ir->operands[0]->type->base_type) { |
| 539 | case GLSL_TYPE_UINT: |
| 540 | data.b[0] = data.b[0] && op[0]->value.u[c] == op[1]->value.u[c]; |
| 541 | break; |
| 542 | case GLSL_TYPE_INT: |
| 543 | data.b[0] = data.b[0] && op[0]->value.i[c] == op[1]->value.i[c]; |
| 544 | break; |
| 545 | case GLSL_TYPE_FLOAT: |
| 546 | data.b[0] = data.b[0] && op[0]->value.f[c] == op[1]->value.f[c]; |
| 547 | break; |
| 548 | case GLSL_TYPE_BOOL: |
| 549 | data.b[0] = data.b[0] && op[0]->value.b[c] == op[1]->value.b[c]; |
| 550 | break; |
| 551 | default: |
| 552 | assert(0); |
Eric Anholt | ec1949e | 2010-04-06 10:02:27 -0700 | [diff] [blame] | 553 | } |
| 554 | } |
| 555 | break; |
| 556 | case ir_binop_nequal: |
Ian Romanick | 083d75a | 2010-06-11 16:20:43 -0700 | [diff] [blame] | 557 | data.b[0] = false; |
Kenneth Graunke | f14e596 | 2010-07-06 16:26:11 -0700 | [diff] [blame] | 558 | for (unsigned c = 0; c < ir->operands[0]->type->components(); c++) { |
Ian Romanick | 083d75a | 2010-06-11 16:20:43 -0700 | [diff] [blame] | 559 | switch (ir->operands[0]->type->base_type) { |
| 560 | case GLSL_TYPE_UINT: |
| 561 | data.b[0] = data.b[0] || op[0]->value.u[c] != op[1]->value.u[c]; |
| 562 | break; |
| 563 | case GLSL_TYPE_INT: |
| 564 | data.b[0] = data.b[0] || op[0]->value.i[c] != op[1]->value.i[c]; |
| 565 | break; |
| 566 | case GLSL_TYPE_FLOAT: |
| 567 | data.b[0] = data.b[0] || op[0]->value.f[c] != op[1]->value.f[c]; |
| 568 | break; |
| 569 | case GLSL_TYPE_BOOL: |
| 570 | data.b[0] = data.b[0] || op[0]->value.b[c] != op[1]->value.b[c]; |
| 571 | break; |
| 572 | default: |
| 573 | assert(0); |
Eric Anholt | ec1949e | 2010-04-06 10:02:27 -0700 | [diff] [blame] | 574 | } |
| 575 | } |
| 576 | break; |
| 577 | |
Eric Anholt | a576f9d | 2010-03-31 16:25:12 -1000 | [diff] [blame] | 578 | default: |
Ian Romanick | f8b88be | 2010-06-11 16:23:52 -0700 | [diff] [blame] | 579 | /* FINISHME: Should handle all expression types. */ |
| 580 | return; |
Eric Anholt | a576f9d | 2010-03-31 16:25:12 -1000 | [diff] [blame] | 581 | } |
Eric Anholt | d98da97 | 2010-04-01 18:25:11 -1000 | [diff] [blame] | 582 | |
Eric Anholt | 6b01b50 | 2010-06-24 15:18:39 -0700 | [diff] [blame] | 583 | void *ctx = talloc_parent(ir); |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 584 | this->value = new(ctx) ir_constant(ir->type, &data); |
Ian Romanick | 1cf43a4 | 2010-03-30 16:56:50 -0700 | [diff] [blame] | 585 | } |
| 586 | |
| 587 | |
| 588 | void |
Kenneth Graunke | 26d74cd | 2010-05-26 17:42:03 -0700 | [diff] [blame] | 589 | ir_constant_visitor::visit(ir_texture *ir) |
| 590 | { |
| 591 | // FINISHME: Do stuff with texture lookups |
| 592 | (void) ir; |
| 593 | value = NULL; |
| 594 | } |
| 595 | |
| 596 | |
| 597 | void |
Ian Romanick | 1cf43a4 | 2010-03-30 16:56:50 -0700 | [diff] [blame] | 598 | ir_constant_visitor::visit(ir_swizzle *ir) |
| 599 | { |
Ian Romanick | c2ba619 | 2010-06-11 12:30:28 -0700 | [diff] [blame] | 600 | ir_constant *v = ir->val->constant_expression_value(); |
| 601 | |
| 602 | this->value = NULL; |
| 603 | |
| 604 | if (v != NULL) { |
Ian Romanick | 0bb70a3 | 2010-06-11 15:49:49 -0700 | [diff] [blame] | 605 | ir_constant_data data; |
Ian Romanick | c2ba619 | 2010-06-11 12:30:28 -0700 | [diff] [blame] | 606 | |
| 607 | const unsigned swiz_idx[4] = { |
| 608 | ir->mask.x, ir->mask.y, ir->mask.z, ir->mask.w |
| 609 | }; |
| 610 | |
| 611 | for (unsigned i = 0; i < ir->mask.num_components; i++) { |
| 612 | switch (v->type->base_type) { |
| 613 | case GLSL_TYPE_UINT: |
| 614 | case GLSL_TYPE_INT: data.u[i] = v->value.u[swiz_idx[i]]; break; |
| 615 | case GLSL_TYPE_FLOAT: data.f[i] = v->value.f[swiz_idx[i]]; break; |
| 616 | case GLSL_TYPE_BOOL: data.b[i] = v->value.b[swiz_idx[i]]; break; |
| 617 | default: assert(!"Should not get here."); break; |
| 618 | } |
| 619 | } |
| 620 | |
Eric Anholt | 6b01b50 | 2010-06-24 15:18:39 -0700 | [diff] [blame] | 621 | void *ctx = talloc_parent(ir); |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 622 | this->value = new(ctx) ir_constant(ir->type, &data); |
Ian Romanick | c2ba619 | 2010-06-11 12:30:28 -0700 | [diff] [blame] | 623 | } |
Ian Romanick | 1cf43a4 | 2010-03-30 16:56:50 -0700 | [diff] [blame] | 624 | } |
| 625 | |
| 626 | |
| 627 | void |
Ian Romanick | c7b1046 | 2010-05-19 13:20:12 +0200 | [diff] [blame] | 628 | ir_constant_visitor::visit(ir_dereference_variable *ir) |
Ian Romanick | 1cf43a4 | 2010-03-30 16:56:50 -0700 | [diff] [blame] | 629 | { |
Ian Romanick | 1cf43a4 | 2010-03-30 16:56:50 -0700 | [diff] [blame] | 630 | value = NULL; |
Eric Anholt | 326c676 | 2010-04-06 10:30:54 -0700 | [diff] [blame] | 631 | |
Ian Romanick | c7b1046 | 2010-05-19 13:20:12 +0200 | [diff] [blame] | 632 | ir_variable *var = ir->variable_referenced(); |
| 633 | if (var && var->constant_value) |
Ian Romanick | ca088cc | 2010-07-06 17:41:02 -0700 | [diff] [blame] | 634 | value = var->constant_value->clone(NULL); |
Ian Romanick | c7b1046 | 2010-05-19 13:20:12 +0200 | [diff] [blame] | 635 | } |
| 636 | |
| 637 | |
| 638 | void |
| 639 | ir_constant_visitor::visit(ir_dereference_array *ir) |
| 640 | { |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 641 | void *ctx = talloc_parent(ir); |
Ian Romanick | 9b92af9 | 2010-06-11 12:20:12 -0700 | [diff] [blame] | 642 | ir_constant *array = ir->array->constant_expression_value(); |
| 643 | ir_constant *idx = ir->array_index->constant_expression_value(); |
| 644 | |
| 645 | this->value = NULL; |
| 646 | |
| 647 | if ((array != NULL) && (idx != NULL)) { |
| 648 | if (array->type->is_matrix()) { |
| 649 | /* Array access of a matrix results in a vector. |
| 650 | */ |
| 651 | const unsigned column = idx->value.u[0]; |
| 652 | |
| 653 | const glsl_type *const column_type = array->type->column_type(); |
| 654 | |
| 655 | /* Offset in the constant matrix to the first element of the column |
| 656 | * to be extracted. |
| 657 | */ |
| 658 | const unsigned mat_idx = column * column_type->vector_elements; |
| 659 | |
Ian Romanick | 0bb70a3 | 2010-06-11 15:49:49 -0700 | [diff] [blame] | 660 | ir_constant_data data; |
Ian Romanick | 9b92af9 | 2010-06-11 12:20:12 -0700 | [diff] [blame] | 661 | |
| 662 | switch (column_type->base_type) { |
| 663 | case GLSL_TYPE_UINT: |
| 664 | case GLSL_TYPE_INT: |
| 665 | for (unsigned i = 0; i < column_type->vector_elements; i++) |
| 666 | data.u[i] = array->value.u[mat_idx + i]; |
| 667 | |
| 668 | break; |
| 669 | |
| 670 | case GLSL_TYPE_FLOAT: |
| 671 | for (unsigned i = 0; i < column_type->vector_elements; i++) |
| 672 | data.f[i] = array->value.f[mat_idx + i]; |
| 673 | |
| 674 | break; |
| 675 | |
| 676 | default: |
| 677 | assert(!"Should not get here."); |
| 678 | break; |
| 679 | } |
| 680 | |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 681 | this->value = new(ctx) ir_constant(column_type, &data); |
Ian Romanick | 9b92af9 | 2010-06-11 12:20:12 -0700 | [diff] [blame] | 682 | } else if (array->type->is_vector()) { |
| 683 | const unsigned component = idx->value.u[0]; |
| 684 | |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 685 | this->value = new(ctx) ir_constant(array, component); |
Ian Romanick | 9b92af9 | 2010-06-11 12:20:12 -0700 | [diff] [blame] | 686 | } else { |
| 687 | /* FINISHME: Handle access of constant arrays. */ |
| 688 | } |
| 689 | } |
Ian Romanick | c7b1046 | 2010-05-19 13:20:12 +0200 | [diff] [blame] | 690 | } |
| 691 | |
| 692 | |
| 693 | void |
| 694 | ir_constant_visitor::visit(ir_dereference_record *ir) |
| 695 | { |
Ian Romanick | 253dede | 2010-06-09 17:30:19 -0700 | [diff] [blame] | 696 | ir_constant *v = ir->record->constant_expression_value(); |
| 697 | |
| 698 | this->value = (v != NULL) ? v->get_record_field(ir->field) : NULL; |
Ian Romanick | 1cf43a4 | 2010-03-30 16:56:50 -0700 | [diff] [blame] | 699 | } |
| 700 | |
| 701 | |
| 702 | void |
| 703 | ir_constant_visitor::visit(ir_assignment *ir) |
| 704 | { |
| 705 | (void) ir; |
| 706 | value = NULL; |
| 707 | } |
| 708 | |
| 709 | |
| 710 | void |
| 711 | ir_constant_visitor::visit(ir_constant *ir) |
| 712 | { |
| 713 | value = ir; |
| 714 | } |
| 715 | |
| 716 | |
| 717 | void |
| 718 | ir_constant_visitor::visit(ir_call *ir) |
| 719 | { |
| 720 | (void) ir; |
| 721 | value = NULL; |
| 722 | } |
| 723 | |
| 724 | |
| 725 | void |
| 726 | ir_constant_visitor::visit(ir_return *ir) |
| 727 | { |
| 728 | (void) ir; |
| 729 | value = NULL; |
| 730 | } |
| 731 | |
| 732 | |
| 733 | void |
Kenneth Graunke | 16efab1 | 2010-06-30 10:47:34 -0700 | [diff] [blame] | 734 | ir_constant_visitor::visit(ir_discard *ir) |
| 735 | { |
| 736 | (void) ir; |
| 737 | value = NULL; |
| 738 | } |
| 739 | |
| 740 | |
| 741 | void |
Ian Romanick | 1cf43a4 | 2010-03-30 16:56:50 -0700 | [diff] [blame] | 742 | ir_constant_visitor::visit(ir_if *ir) |
| 743 | { |
| 744 | (void) ir; |
| 745 | value = NULL; |
| 746 | } |
Ian Romanick | fad607a | 2010-04-05 16:16:07 -0700 | [diff] [blame] | 747 | |
| 748 | |
| 749 | void |
| 750 | ir_constant_visitor::visit(ir_loop *ir) |
| 751 | { |
| 752 | (void) ir; |
| 753 | value = NULL; |
| 754 | } |
Ian Romanick | f8e31e0 | 2010-04-05 16:28:15 -0700 | [diff] [blame] | 755 | |
| 756 | |
| 757 | void |
| 758 | ir_constant_visitor::visit(ir_loop_jump *ir) |
| 759 | { |
| 760 | (void) ir; |
| 761 | value = NULL; |
| 762 | } |