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