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