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