Eric Anholt | c22c400 | 2010-03-26 18:20:30 -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 | #include <stdlib.h> |
Eric Anholt | d1e3195 | 2010-03-28 01:55:38 -0700 | [diff] [blame] | 25 | #include <math.h> |
Eric Anholt | c22c400 | 2010-03-26 18:20:30 -0700 | [diff] [blame] | 26 | #include "glsl_symbol_table.h" |
| 27 | #include "glsl_parser_extras.h" |
| 28 | #include "glsl_types.h" |
| 29 | #include "ir.h" |
| 30 | |
| 31 | static void |
Eric Anholt | 2eec73f | 2010-03-27 12:25:20 -0700 | [diff] [blame] | 32 | generate_unop(exec_list *instructions, |
| 33 | ir_variable **declarations, |
| 34 | const glsl_type *type, |
| 35 | enum ir_expression_operation op) |
Eric Anholt | c22c400 | 2010-03-26 18:20:30 -0700 | [diff] [blame] | 36 | { |
Eric Anholt | c22c400 | 2010-03-26 18:20:30 -0700 | [diff] [blame] | 37 | ir_dereference *const arg = new ir_dereference(declarations[0]); |
| 38 | ir_rvalue *result; |
| 39 | |
Eric Anholt | 2eec73f | 2010-03-27 12:25:20 -0700 | [diff] [blame] | 40 | result = new ir_expression(op, type, arg, NULL); |
Eric Anholt | c22c400 | 2010-03-26 18:20:30 -0700 | [diff] [blame] | 41 | |
Eric Anholt | 7e78e07 | 2010-04-07 13:34:15 -0700 | [diff] [blame] | 42 | ir_instruction *inst = new ir_return(result); |
Eric Anholt | c22c400 | 2010-03-26 18:20:30 -0700 | [diff] [blame] | 43 | instructions->push_tail(inst); |
| 44 | } |
| 45 | |
Eric Anholt | 2eec73f | 2010-03-27 12:25:20 -0700 | [diff] [blame] | 46 | static void |
Eric Anholt | bfe380a | 2010-03-27 12:43:13 -0700 | [diff] [blame] | 47 | generate_binop(exec_list *instructions, |
| 48 | ir_variable **declarations, |
| 49 | const glsl_type *type, |
| 50 | enum ir_expression_operation op) |
| 51 | { |
Eric Anholt | bfe380a | 2010-03-27 12:43:13 -0700 | [diff] [blame] | 52 | ir_dereference *const arg1 = new ir_dereference(declarations[0]); |
| 53 | ir_dereference *const arg2 = new ir_dereference(declarations[1]); |
| 54 | ir_rvalue *result; |
| 55 | |
| 56 | result = new ir_expression(op, type, arg1, arg2); |
| 57 | |
Eric Anholt | 7e78e07 | 2010-04-07 13:34:15 -0700 | [diff] [blame] | 58 | ir_instruction *inst = new ir_return(result); |
Eric Anholt | bfe380a | 2010-03-27 12:43:13 -0700 | [diff] [blame] | 59 | instructions->push_tail(inst); |
| 60 | } |
| 61 | |
| 62 | static void |
Eric Anholt | d1e3195 | 2010-03-28 01:55:38 -0700 | [diff] [blame] | 63 | generate_radians(exec_list *instructions, |
| 64 | ir_variable **declarations, |
| 65 | const glsl_type *type) |
| 66 | { |
Eric Anholt | d1e3195 | 2010-03-28 01:55:38 -0700 | [diff] [blame] | 67 | ir_dereference *const arg = new ir_dereference(declarations[0]); |
| 68 | ir_rvalue *result; |
| 69 | |
| 70 | result = new ir_expression(ir_binop_mul, type, |
| 71 | arg, |
| 72 | new ir_constant((float)(M_PI / 180.0))); |
| 73 | |
Eric Anholt | 7e78e07 | 2010-04-07 13:34:15 -0700 | [diff] [blame] | 74 | ir_instruction *inst = new ir_return(result); |
Eric Anholt | d1e3195 | 2010-03-28 01:55:38 -0700 | [diff] [blame] | 75 | instructions->push_tail(inst); |
| 76 | } |
| 77 | |
| 78 | static void |
| 79 | generate_degrees(exec_list *instructions, |
| 80 | ir_variable **declarations, |
| 81 | const glsl_type *type) |
| 82 | { |
Eric Anholt | d1e3195 | 2010-03-28 01:55:38 -0700 | [diff] [blame] | 83 | ir_dereference *const arg = new ir_dereference(declarations[0]); |
| 84 | ir_rvalue *result; |
| 85 | |
| 86 | result = new ir_expression(ir_binop_mul, type, |
| 87 | arg, |
| 88 | new ir_constant((float)(180.0 / M_PI))); |
| 89 | |
Eric Anholt | 7e78e07 | 2010-04-07 13:34:15 -0700 | [diff] [blame] | 90 | ir_instruction *inst = new ir_return(result); |
Eric Anholt | d1e3195 | 2010-03-28 01:55:38 -0700 | [diff] [blame] | 91 | instructions->push_tail(inst); |
| 92 | } |
| 93 | |
| 94 | static void |
Eric Anholt | 2eec73f | 2010-03-27 12:25:20 -0700 | [diff] [blame] | 95 | generate_exp(exec_list *instructions, |
| 96 | ir_variable **declarations, |
| 97 | const glsl_type *type) |
| 98 | { |
| 99 | generate_unop(instructions, declarations, type, ir_unop_exp); |
| 100 | } |
| 101 | |
| 102 | static void |
| 103 | generate_log(exec_list *instructions, |
| 104 | ir_variable **declarations, |
| 105 | const glsl_type *type) |
| 106 | { |
| 107 | generate_unop(instructions, declarations, type, ir_unop_log); |
| 108 | } |
| 109 | |
| 110 | static void |
Eric Anholt | 0166526 | 2010-03-27 13:56:35 -0700 | [diff] [blame] | 111 | generate_exp2(exec_list *instructions, |
| 112 | ir_variable **declarations, |
| 113 | const glsl_type *type) |
| 114 | { |
| 115 | generate_unop(instructions, declarations, type, ir_unop_exp2); |
| 116 | } |
| 117 | |
| 118 | static void |
| 119 | generate_log2(exec_list *instructions, |
| 120 | ir_variable **declarations, |
| 121 | const glsl_type *type) |
| 122 | { |
| 123 | generate_unop(instructions, declarations, type, ir_unop_log2); |
| 124 | } |
| 125 | |
| 126 | static void |
Eric Anholt | 2eec73f | 2010-03-27 12:25:20 -0700 | [diff] [blame] | 127 | generate_rsq(exec_list *instructions, |
| 128 | ir_variable **declarations, |
| 129 | const glsl_type *type) |
| 130 | { |
| 131 | generate_unop(instructions, declarations, type, ir_unop_rsq); |
| 132 | } |
| 133 | |
| 134 | static void |
Eric Anholt | 44d68fd | 2010-03-27 13:01:51 -0700 | [diff] [blame] | 135 | generate_sqrt(exec_list *instructions, |
| 136 | ir_variable **declarations, |
| 137 | const glsl_type *type) |
| 138 | { |
| 139 | generate_unop(instructions, declarations, type, ir_unop_sqrt); |
| 140 | } |
| 141 | |
| 142 | static void |
Eric Anholt | 2eec73f | 2010-03-27 12:25:20 -0700 | [diff] [blame] | 143 | generate_abs(exec_list *instructions, |
| 144 | ir_variable **declarations, |
| 145 | const glsl_type *type) |
| 146 | { |
| 147 | generate_unop(instructions, declarations, type, ir_unop_abs); |
| 148 | } |
| 149 | |
| 150 | static void |
| 151 | generate_ceil(exec_list *instructions, |
| 152 | ir_variable **declarations, |
| 153 | const glsl_type *type) |
| 154 | { |
| 155 | generate_unop(instructions, declarations, type, ir_unop_ceil); |
| 156 | } |
| 157 | |
| 158 | static void |
| 159 | generate_floor(exec_list *instructions, |
| 160 | ir_variable **declarations, |
| 161 | const glsl_type *type) |
| 162 | { |
| 163 | generate_unop(instructions, declarations, type, ir_unop_floor); |
| 164 | } |
| 165 | |
Eric Anholt | bfe380a | 2010-03-27 12:43:13 -0700 | [diff] [blame] | 166 | static void |
| 167 | generate_mod(exec_list *instructions, |
| 168 | ir_variable **declarations, |
| 169 | const glsl_type *type) |
| 170 | { |
| 171 | generate_binop(instructions, declarations, type, ir_binop_mod); |
| 172 | } |
| 173 | |
| 174 | static void |
| 175 | generate_min(exec_list *instructions, |
| 176 | ir_variable **declarations, |
| 177 | const glsl_type *type) |
| 178 | { |
| 179 | generate_binop(instructions, declarations, type, ir_binop_min); |
| 180 | } |
| 181 | |
| 182 | static void |
| 183 | generate_max(exec_list *instructions, |
| 184 | ir_variable **declarations, |
| 185 | const glsl_type *type) |
| 186 | { |
| 187 | generate_binop(instructions, declarations, type, ir_binop_max); |
| 188 | } |
| 189 | |
Eric Anholt | ddd2e83 | 2010-03-27 12:59:42 -0700 | [diff] [blame] | 190 | |
| 191 | static void |
Eric Anholt | 9257592 | 2010-04-08 14:34:38 -0700 | [diff] [blame] | 192 | generate_normalize(exec_list *instructions, |
| 193 | ir_variable **declarations, |
| 194 | const glsl_type *type) |
| 195 | { |
| 196 | ir_dereference *const arg = new ir_dereference(declarations[0]); |
| 197 | ir_rvalue *temp; |
| 198 | ir_rvalue *result; |
| 199 | |
| 200 | temp = new ir_expression(ir_binop_dot, glsl_type::float_type, arg, arg); |
| 201 | temp = new ir_expression(ir_unop_rsq, glsl_type::float_type, temp, NULL); |
| 202 | result = new ir_expression(ir_binop_mul, type, arg, temp); |
| 203 | |
| 204 | ir_instruction *inst = new ir_return(result); |
| 205 | instructions->push_tail(inst); |
| 206 | } |
| 207 | |
| 208 | |
| 209 | static void |
Eric Anholt | ddd2e83 | 2010-03-27 12:59:42 -0700 | [diff] [blame] | 210 | generate_pow(exec_list *instructions, |
| 211 | ir_variable **declarations, |
| 212 | const glsl_type *type) |
| 213 | { |
| 214 | generate_binop(instructions, declarations, type, ir_binop_pow); |
| 215 | } |
| 216 | |
Eric Anholt | c22c400 | 2010-03-26 18:20:30 -0700 | [diff] [blame] | 217 | void |
| 218 | generate_function_instance(ir_function *f, |
| 219 | const char *name, |
| 220 | exec_list *instructions, |
Eric Anholt | bfe380a | 2010-03-27 12:43:13 -0700 | [diff] [blame] | 221 | int n_args, |
Eric Anholt | c22c400 | 2010-03-26 18:20:30 -0700 | [diff] [blame] | 222 | void (*generate)(exec_list *instructions, |
| 223 | ir_variable **declarations, |
| 224 | const glsl_type *type), |
Eric Anholt | 53afc36 | 2010-03-27 13:55:04 -0700 | [diff] [blame] | 225 | const glsl_type *ret_type, |
Eric Anholt | c22c400 | 2010-03-26 18:20:30 -0700 | [diff] [blame] | 226 | const glsl_type *type) |
| 227 | { |
Eric Anholt | 7e78e07 | 2010-04-07 13:34:15 -0700 | [diff] [blame] | 228 | ir_variable *declarations[16]; |
Eric Anholt | c22c400 | 2010-03-26 18:20:30 -0700 | [diff] [blame] | 229 | |
Eric Anholt | 7e78e07 | 2010-04-07 13:34:15 -0700 | [diff] [blame] | 230 | ir_function_signature *const sig = new ir_function_signature(ret_type); |
Ian Romanick | 6a15d5b5 | 2010-03-31 16:37:10 -0700 | [diff] [blame] | 231 | f->add_signature(sig); |
Eric Anholt | c22c400 | 2010-03-26 18:20:30 -0700 | [diff] [blame] | 232 | |
Eric Anholt | 894ea97 | 2010-04-07 13:19:11 -0700 | [diff] [blame] | 233 | ir_label *const label = new ir_label(name, sig); |
Eric Anholt | c22c400 | 2010-03-26 18:20:30 -0700 | [diff] [blame] | 234 | instructions->push_tail(label); |
| 235 | sig->definition = label; |
Eric Anholt | bfe380a | 2010-03-27 12:43:13 -0700 | [diff] [blame] | 236 | static const char *arg_names[] = { |
| 237 | "arg0", |
| 238 | "arg1" |
| 239 | }; |
| 240 | int i; |
Eric Anholt | c22c400 | 2010-03-26 18:20:30 -0700 | [diff] [blame] | 241 | |
Eric Anholt | bfe380a | 2010-03-27 12:43:13 -0700 | [diff] [blame] | 242 | for (i = 0; i < n_args; i++) { |
| 243 | ir_variable *var = new ir_variable(type, arg_names[i]); |
Eric Anholt | c22c400 | 2010-03-26 18:20:30 -0700 | [diff] [blame] | 244 | |
Eric Anholt | bfe380a | 2010-03-27 12:43:13 -0700 | [diff] [blame] | 245 | var->mode = ir_var_in; |
| 246 | sig->parameters.push_tail(var); |
Eric Anholt | c22c400 | 2010-03-26 18:20:30 -0700 | [diff] [blame] | 247 | |
Eric Anholt | bfe380a | 2010-03-27 12:43:13 -0700 | [diff] [blame] | 248 | declarations[i] = var; |
| 249 | } |
Eric Anholt | c22c400 | 2010-03-26 18:20:30 -0700 | [diff] [blame] | 250 | |
Eric Anholt | 894ea97 | 2010-04-07 13:19:11 -0700 | [diff] [blame] | 251 | generate(&sig->body, declarations, type); |
Eric Anholt | c22c400 | 2010-03-26 18:20:30 -0700 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | void |
| 255 | make_gentype_function(glsl_symbol_table *symtab, exec_list *instructions, |
| 256 | const char *name, |
Eric Anholt | bfe380a | 2010-03-27 12:43:13 -0700 | [diff] [blame] | 257 | int n_args, |
Eric Anholt | c22c400 | 2010-03-26 18:20:30 -0700 | [diff] [blame] | 258 | void (*generate)(exec_list *instructions, |
| 259 | ir_variable **declarations, |
| 260 | const glsl_type *type)) |
| 261 | { |
| 262 | ir_function *const f = new ir_function(name); |
Eric Anholt | 53afc36 | 2010-03-27 13:55:04 -0700 | [diff] [blame] | 263 | const glsl_type *float_type = glsl_type::float_type; |
Eric Anholt | c22c400 | 2010-03-26 18:20:30 -0700 | [diff] [blame] | 264 | const glsl_type *vec2_type = glsl_type::get_instance(GLSL_TYPE_FLOAT, 2, 1); |
| 265 | const glsl_type *vec3_type = glsl_type::get_instance(GLSL_TYPE_FLOAT, 3, 1); |
| 266 | const glsl_type *vec4_type = glsl_type::get_instance(GLSL_TYPE_FLOAT, 4, 1); |
| 267 | |
| 268 | bool added = symtab->add_function(name, f); |
| 269 | assert(added); |
| 270 | |
Eric Anholt | bfe380a | 2010-03-27 12:43:13 -0700 | [diff] [blame] | 271 | generate_function_instance(f, name, instructions, n_args, generate, |
Eric Anholt | 53afc36 | 2010-03-27 13:55:04 -0700 | [diff] [blame] | 272 | float_type, float_type); |
Eric Anholt | bfe380a | 2010-03-27 12:43:13 -0700 | [diff] [blame] | 273 | generate_function_instance(f, name, instructions, n_args, generate, |
Eric Anholt | 53afc36 | 2010-03-27 13:55:04 -0700 | [diff] [blame] | 274 | vec2_type, vec2_type); |
Eric Anholt | bfe380a | 2010-03-27 12:43:13 -0700 | [diff] [blame] | 275 | generate_function_instance(f, name, instructions, n_args, generate, |
Eric Anholt | 53afc36 | 2010-03-27 13:55:04 -0700 | [diff] [blame] | 276 | vec3_type, vec3_type); |
Eric Anholt | bfe380a | 2010-03-27 12:43:13 -0700 | [diff] [blame] | 277 | generate_function_instance(f, name, instructions, n_args, generate, |
Eric Anholt | 53afc36 | 2010-03-27 13:55:04 -0700 | [diff] [blame] | 278 | vec4_type, vec4_type); |
| 279 | } |
| 280 | |
| 281 | static void |
| 282 | generate_length(exec_list *instructions, |
| 283 | ir_variable **declarations, |
| 284 | const glsl_type *type) |
| 285 | { |
Eric Anholt | 53afc36 | 2010-03-27 13:55:04 -0700 | [diff] [blame] | 286 | ir_dereference *const arg = new ir_dereference(declarations[0]); |
| 287 | ir_rvalue *result, *temp; |
| 288 | |
| 289 | (void)type; |
| 290 | |
| 291 | /* FINISHME: implement the abs(arg) variant for length(float f) */ |
| 292 | |
| 293 | temp = new ir_expression(ir_binop_dot, glsl_type::float_type, arg, arg); |
| 294 | result = new ir_expression(ir_unop_sqrt, glsl_type::float_type, temp, NULL); |
| 295 | |
Eric Anholt | 7e78e07 | 2010-04-07 13:34:15 -0700 | [diff] [blame] | 296 | ir_instruction *inst = new ir_return(result); |
Eric Anholt | 53afc36 | 2010-03-27 13:55:04 -0700 | [diff] [blame] | 297 | instructions->push_tail(inst); |
| 298 | } |
| 299 | |
| 300 | void |
| 301 | generate_length_functions(glsl_symbol_table *symtab, exec_list *instructions) |
| 302 | { |
| 303 | const char *name = "length"; |
| 304 | ir_function *const f = new ir_function(name); |
| 305 | const glsl_type *float_type = glsl_type::float_type; |
| 306 | const glsl_type *vec2_type = glsl_type::get_instance(GLSL_TYPE_FLOAT, 2, 1); |
| 307 | const glsl_type *vec3_type = glsl_type::get_instance(GLSL_TYPE_FLOAT, 3, 1); |
| 308 | const glsl_type *vec4_type = glsl_type::get_instance(GLSL_TYPE_FLOAT, 4, 1); |
| 309 | |
| 310 | bool added = symtab->add_function(name, f); |
| 311 | assert(added); |
| 312 | |
| 313 | generate_function_instance(f, name, instructions, 1, generate_length, |
| 314 | float_type, float_type); |
| 315 | generate_function_instance(f, name, instructions, 1, generate_length, |
| 316 | float_type, vec2_type); |
| 317 | generate_function_instance(f, name, instructions, 1, generate_length, |
| 318 | float_type, vec3_type); |
| 319 | generate_function_instance(f, name, instructions, 1, generate_length, |
| 320 | float_type, vec4_type); |
Eric Anholt | c22c400 | 2010-03-26 18:20:30 -0700 | [diff] [blame] | 321 | } |
| 322 | |
Eric Anholt | 76a91e1 | 2010-03-27 14:04:43 -0700 | [diff] [blame] | 323 | static void |
| 324 | generate_dot(exec_list *instructions, |
| 325 | ir_variable **declarations, |
| 326 | const glsl_type *type) |
| 327 | { |
Eric Anholt | 6173312 | 2010-04-07 15:18:37 -0700 | [diff] [blame] | 328 | ir_dereference *const arg0 = new ir_dereference(declarations[0]); |
| 329 | ir_dereference *const arg1 = new ir_dereference(declarations[1]); |
Eric Anholt | 76a91e1 | 2010-03-27 14:04:43 -0700 | [diff] [blame] | 330 | ir_rvalue *result; |
| 331 | |
| 332 | (void)type; |
| 333 | |
Eric Anholt | 6173312 | 2010-04-07 15:18:37 -0700 | [diff] [blame] | 334 | result = new ir_expression(ir_binop_dot, glsl_type::float_type, arg0, arg1); |
Eric Anholt | 76a91e1 | 2010-03-27 14:04:43 -0700 | [diff] [blame] | 335 | |
Eric Anholt | 7e78e07 | 2010-04-07 13:34:15 -0700 | [diff] [blame] | 336 | ir_instruction *inst = new ir_return(result); |
Eric Anholt | 76a91e1 | 2010-03-27 14:04:43 -0700 | [diff] [blame] | 337 | instructions->push_tail(inst); |
| 338 | } |
| 339 | |
| 340 | void |
| 341 | generate_dot_functions(glsl_symbol_table *symtab, exec_list *instructions) |
| 342 | { |
| 343 | const char *name = "dot"; |
| 344 | ir_function *const f = new ir_function(name); |
| 345 | const glsl_type *float_type = glsl_type::float_type; |
| 346 | const glsl_type *vec2_type = glsl_type::get_instance(GLSL_TYPE_FLOAT, 2, 1); |
| 347 | const glsl_type *vec3_type = glsl_type::get_instance(GLSL_TYPE_FLOAT, 3, 1); |
| 348 | const glsl_type *vec4_type = glsl_type::get_instance(GLSL_TYPE_FLOAT, 4, 1); |
| 349 | |
| 350 | bool added = symtab->add_function(name, f); |
| 351 | assert(added); |
| 352 | |
Eric Anholt | 6173312 | 2010-04-07 15:18:37 -0700 | [diff] [blame] | 353 | generate_function_instance(f, name, instructions, 2, generate_dot, |
Eric Anholt | 76a91e1 | 2010-03-27 14:04:43 -0700 | [diff] [blame] | 354 | float_type, float_type); |
Eric Anholt | 6173312 | 2010-04-07 15:18:37 -0700 | [diff] [blame] | 355 | generate_function_instance(f, name, instructions, 2, generate_dot, |
Eric Anholt | 76a91e1 | 2010-03-27 14:04:43 -0700 | [diff] [blame] | 356 | float_type, vec2_type); |
Eric Anholt | 6173312 | 2010-04-07 15:18:37 -0700 | [diff] [blame] | 357 | generate_function_instance(f, name, instructions, 2, generate_dot, |
Eric Anholt | 76a91e1 | 2010-03-27 14:04:43 -0700 | [diff] [blame] | 358 | float_type, vec3_type); |
Eric Anholt | 6173312 | 2010-04-07 15:18:37 -0700 | [diff] [blame] | 359 | generate_function_instance(f, name, instructions, 2, generate_dot, |
Eric Anholt | 76a91e1 | 2010-03-27 14:04:43 -0700 | [diff] [blame] | 360 | float_type, vec4_type); |
| 361 | } |
| 362 | |
Eric Anholt | feeb43b | 2010-04-08 15:02:59 -0700 | [diff] [blame^] | 363 | static void |
| 364 | generate_any_bvec2(exec_list *instructions, |
| 365 | ir_variable **declarations, |
| 366 | const glsl_type *type) |
| 367 | { |
| 368 | ir_dereference *const arg0 = new ir_dereference(declarations[0]); |
| 369 | ir_rvalue *result; |
| 370 | |
| 371 | (void)type; |
| 372 | |
| 373 | result = new ir_expression(ir_binop_logic_or, glsl_type::bool_type, |
| 374 | new ir_swizzle(arg0, 0, 0, 0, 0, 1), |
| 375 | new ir_swizzle(arg0, 1, 0, 0, 0, 1)); |
| 376 | |
| 377 | ir_instruction *inst = new ir_return(result); |
| 378 | instructions->push_tail(inst); |
| 379 | } |
| 380 | |
| 381 | static void |
| 382 | generate_any_bvec3(exec_list *instructions, |
| 383 | ir_variable **declarations, |
| 384 | const glsl_type *type) |
| 385 | { |
| 386 | ir_dereference *const arg0 = new ir_dereference(declarations[0]); |
| 387 | ir_rvalue *result; |
| 388 | |
| 389 | (void)type; |
| 390 | |
| 391 | result = new ir_expression(ir_binop_logic_or, glsl_type::bool_type, |
| 392 | new ir_swizzle(arg0, 0, 0, 0, 0, 1), |
| 393 | new ir_swizzle(arg0, 1, 0, 0, 0, 1)); |
| 394 | result = new ir_expression(ir_binop_logic_or, glsl_type::bool_type, |
| 395 | result, |
| 396 | new ir_swizzle(arg0, 2, 0, 0, 0, 1)); |
| 397 | |
| 398 | ir_instruction *inst = new ir_return(result); |
| 399 | instructions->push_tail(inst); |
| 400 | } |
| 401 | |
| 402 | static void |
| 403 | generate_any_bvec4(exec_list *instructions, |
| 404 | ir_variable **declarations, |
| 405 | const glsl_type *type) |
| 406 | { |
| 407 | ir_dereference *const arg0 = new ir_dereference(declarations[0]); |
| 408 | ir_rvalue *result; |
| 409 | |
| 410 | (void)type; |
| 411 | |
| 412 | result = new ir_expression(ir_binop_logic_or, glsl_type::bool_type, |
| 413 | new ir_swizzle(arg0, 0, 0, 0, 0, 1), |
| 414 | new ir_swizzle(arg0, 1, 0, 0, 0, 1)); |
| 415 | result = new ir_expression(ir_binop_logic_or, glsl_type::bool_type, |
| 416 | result, |
| 417 | new ir_swizzle(arg0, 2, 0, 0, 0, 1)); |
| 418 | result = new ir_expression(ir_binop_logic_or, glsl_type::bool_type, |
| 419 | result, |
| 420 | new ir_swizzle(arg0, 3, 0, 0, 0, 1)); |
| 421 | |
| 422 | ir_instruction *inst = new ir_return(result); |
| 423 | instructions->push_tail(inst); |
| 424 | } |
| 425 | |
| 426 | static void |
| 427 | generate_all_bvec2(exec_list *instructions, |
| 428 | ir_variable **declarations, |
| 429 | const glsl_type *type) |
| 430 | { |
| 431 | ir_dereference *const arg0 = new ir_dereference(declarations[0]); |
| 432 | ir_rvalue *result; |
| 433 | |
| 434 | (void)type; |
| 435 | |
| 436 | result = new ir_expression(ir_binop_logic_and, glsl_type::bool_type, |
| 437 | new ir_swizzle(arg0, 0, 0, 0, 0, 1), |
| 438 | new ir_swizzle(arg0, 1, 0, 0, 0, 1)); |
| 439 | |
| 440 | ir_instruction *inst = new ir_return(result); |
| 441 | instructions->push_tail(inst); |
| 442 | } |
| 443 | |
| 444 | static void |
| 445 | generate_all_bvec3(exec_list *instructions, |
| 446 | ir_variable **declarations, |
| 447 | const glsl_type *type) |
| 448 | { |
| 449 | ir_dereference *const arg0 = new ir_dereference(declarations[0]); |
| 450 | ir_rvalue *result; |
| 451 | |
| 452 | (void)type; |
| 453 | |
| 454 | result = new ir_expression(ir_binop_logic_and, glsl_type::bool_type, |
| 455 | new ir_swizzle(arg0, 0, 0, 0, 0, 1), |
| 456 | new ir_swizzle(arg0, 1, 0, 0, 0, 1)); |
| 457 | result = new ir_expression(ir_binop_logic_and, glsl_type::bool_type, |
| 458 | result, |
| 459 | new ir_swizzle(arg0, 2, 0, 0, 0, 1)); |
| 460 | |
| 461 | ir_instruction *inst = new ir_return(result); |
| 462 | instructions->push_tail(inst); |
| 463 | } |
| 464 | |
| 465 | static void |
| 466 | generate_all_bvec4(exec_list *instructions, |
| 467 | ir_variable **declarations, |
| 468 | const glsl_type *type) |
| 469 | { |
| 470 | ir_dereference *const arg0 = new ir_dereference(declarations[0]); |
| 471 | ir_rvalue *result; |
| 472 | |
| 473 | (void)type; |
| 474 | |
| 475 | result = new ir_expression(ir_binop_logic_and, glsl_type::bool_type, |
| 476 | new ir_swizzle(arg0, 0, 0, 0, 0, 1), |
| 477 | new ir_swizzle(arg0, 1, 0, 0, 0, 1)); |
| 478 | result = new ir_expression(ir_binop_logic_and, glsl_type::bool_type, |
| 479 | result, |
| 480 | new ir_swizzle(arg0, 2, 0, 0, 0, 1)); |
| 481 | result = new ir_expression(ir_binop_logic_and, glsl_type::bool_type, |
| 482 | result, |
| 483 | new ir_swizzle(arg0, 3, 0, 0, 0, 1)); |
| 484 | |
| 485 | ir_instruction *inst = new ir_return(result); |
| 486 | instructions->push_tail(inst); |
| 487 | } |
| 488 | |
| 489 | static void |
| 490 | generate_not(exec_list *instructions, |
| 491 | ir_variable **declarations, |
| 492 | const glsl_type *type) |
| 493 | { |
| 494 | ir_dereference *const arg0 = new ir_dereference(declarations[0]); |
| 495 | ir_rvalue *result; |
| 496 | |
| 497 | result = new ir_expression(ir_unop_logic_not, type, arg0, NULL); |
| 498 | |
| 499 | ir_instruction *inst = new ir_return(result); |
| 500 | instructions->push_tail(inst); |
| 501 | } |
| 502 | |
| 503 | void |
| 504 | generate_any_functions(glsl_symbol_table *symtab, exec_list *instructions) |
| 505 | { |
| 506 | const char *name = "any"; |
| 507 | ir_function *const f = new ir_function(name); |
| 508 | const glsl_type *bvec2_type = glsl_type::get_instance(GLSL_TYPE_BOOL, 2, 1); |
| 509 | const glsl_type *bvec3_type = glsl_type::get_instance(GLSL_TYPE_BOOL, 3, 1); |
| 510 | const glsl_type *bvec4_type = glsl_type::get_instance(GLSL_TYPE_BOOL, 4, 1); |
| 511 | |
| 512 | bool added = symtab->add_function(name, f); |
| 513 | assert(added); |
| 514 | |
| 515 | generate_function_instance(f, name, instructions, 1, generate_any_bvec2, |
| 516 | glsl_type::bool_type, bvec2_type); |
| 517 | generate_function_instance(f, name, instructions, 1, generate_any_bvec3, |
| 518 | glsl_type::bool_type, bvec3_type); |
| 519 | generate_function_instance(f, name, instructions, 1, generate_any_bvec4, |
| 520 | glsl_type::bool_type, bvec4_type); |
| 521 | } |
| 522 | |
| 523 | void |
| 524 | generate_all_functions(glsl_symbol_table *symtab, exec_list *instructions) |
| 525 | { |
| 526 | const char *name = "all"; |
| 527 | ir_function *const f = new ir_function(name); |
| 528 | const glsl_type *bvec2_type = glsl_type::get_instance(GLSL_TYPE_BOOL, 2, 1); |
| 529 | const glsl_type *bvec3_type = glsl_type::get_instance(GLSL_TYPE_BOOL, 3, 1); |
| 530 | const glsl_type *bvec4_type = glsl_type::get_instance(GLSL_TYPE_BOOL, 4, 1); |
| 531 | |
| 532 | bool added = symtab->add_function(name, f); |
| 533 | assert(added); |
| 534 | |
| 535 | generate_function_instance(f, name, instructions, 1, generate_all_bvec2, |
| 536 | glsl_type::bool_type, bvec2_type); |
| 537 | generate_function_instance(f, name, instructions, 1, generate_all_bvec3, |
| 538 | glsl_type::bool_type, bvec3_type); |
| 539 | generate_function_instance(f, name, instructions, 1, generate_all_bvec4, |
| 540 | glsl_type::bool_type, bvec4_type); |
| 541 | } |
| 542 | |
| 543 | void |
| 544 | generate_not_functions(glsl_symbol_table *symtab, exec_list *instructions) |
| 545 | { |
| 546 | const char *name = "not"; |
| 547 | ir_function *const f = new ir_function(name); |
| 548 | const glsl_type *bvec2_type = glsl_type::get_instance(GLSL_TYPE_BOOL, 2, 1); |
| 549 | const glsl_type *bvec3_type = glsl_type::get_instance(GLSL_TYPE_BOOL, 3, 1); |
| 550 | const glsl_type *bvec4_type = glsl_type::get_instance(GLSL_TYPE_BOOL, 4, 1); |
| 551 | |
| 552 | bool added = symtab->add_function(name, f); |
| 553 | assert(added); |
| 554 | |
| 555 | generate_function_instance(f, name, instructions, 1, generate_not, |
| 556 | bvec2_type, bvec2_type); |
| 557 | generate_function_instance(f, name, instructions, 1, generate_not, |
| 558 | bvec3_type, bvec3_type); |
| 559 | generate_function_instance(f, name, instructions, 1, generate_not, |
| 560 | bvec4_type, bvec4_type); |
| 561 | } |
| 562 | |
Eric Anholt | c22c400 | 2010-03-26 18:20:30 -0700 | [diff] [blame] | 563 | void |
| 564 | generate_110_functions(glsl_symbol_table *symtab, exec_list *instructions) |
| 565 | { |
Eric Anholt | d1e3195 | 2010-03-28 01:55:38 -0700 | [diff] [blame] | 566 | make_gentype_function(symtab, instructions, "radians", 1, generate_radians); |
| 567 | make_gentype_function(symtab, instructions, "degrees", 1, generate_degrees); |
Eric Anholt | c22c400 | 2010-03-26 18:20:30 -0700 | [diff] [blame] | 568 | /* FINISHME: sin() */ |
| 569 | /* FINISHME: cos() */ |
| 570 | /* FINISHME: tan() */ |
| 571 | /* FINISHME: asin() */ |
| 572 | /* FINISHME: acos() */ |
| 573 | /* FINISHME: atan(y,x) */ |
| 574 | /* FINISHME: atan(y/x) */ |
Eric Anholt | ddd2e83 | 2010-03-27 12:59:42 -0700 | [diff] [blame] | 575 | make_gentype_function(symtab, instructions, "pow", 2, generate_pow); |
Eric Anholt | bfe380a | 2010-03-27 12:43:13 -0700 | [diff] [blame] | 576 | make_gentype_function(symtab, instructions, "exp", 1, generate_exp); |
| 577 | make_gentype_function(symtab, instructions, "log", 1, generate_log); |
Eric Anholt | 0166526 | 2010-03-27 13:56:35 -0700 | [diff] [blame] | 578 | make_gentype_function(symtab, instructions, "exp2", 1, generate_exp2); |
| 579 | make_gentype_function(symtab, instructions, "log2", 1, generate_log2); |
Eric Anholt | 44d68fd | 2010-03-27 13:01:51 -0700 | [diff] [blame] | 580 | make_gentype_function(symtab, instructions, "sqrt", 1, generate_sqrt); |
Eric Anholt | bfe380a | 2010-03-27 12:43:13 -0700 | [diff] [blame] | 581 | make_gentype_function(symtab, instructions, "inversesqrt", 1, generate_rsq); |
| 582 | make_gentype_function(symtab, instructions, "abs", 1, generate_abs); |
Eric Anholt | c22c400 | 2010-03-26 18:20:30 -0700 | [diff] [blame] | 583 | /* FINISHME: sign() */ |
Eric Anholt | bfe380a | 2010-03-27 12:43:13 -0700 | [diff] [blame] | 584 | make_gentype_function(symtab, instructions, "floor", 1, generate_floor); |
| 585 | make_gentype_function(symtab, instructions, "ceil", 1, generate_ceil); |
Eric Anholt | c22c400 | 2010-03-26 18:20:30 -0700 | [diff] [blame] | 586 | /* FINISHME: fract() */ |
| 587 | /* FINISHME: mod(x, float y) */ |
Eric Anholt | bfe380a | 2010-03-27 12:43:13 -0700 | [diff] [blame] | 588 | make_gentype_function(symtab, instructions, "mod", 2, generate_mod); |
| 589 | make_gentype_function(symtab, instructions, "min", 2, generate_min); |
| 590 | /* FINISHME: min(x, float y) */ |
| 591 | make_gentype_function(symtab, instructions, "max", 2, generate_max); |
| 592 | /* FINISHME: max(x, float y) */ |
Eric Anholt | c22c400 | 2010-03-26 18:20:30 -0700 | [diff] [blame] | 593 | /* FINISHME: clamp() */ |
| 594 | /* FINISHME: clamp() */ |
| 595 | /* FINISHME: mix() */ |
| 596 | /* FINISHME: mix() */ |
| 597 | /* FINISHME: step() */ |
| 598 | /* FINISHME: step() */ |
| 599 | /* FINISHME: smoothstep() */ |
| 600 | /* FINISHME: smoothstep() */ |
| 601 | /* FINISHME: floor() */ |
| 602 | /* FINISHME: step() */ |
Eric Anholt | 53afc36 | 2010-03-27 13:55:04 -0700 | [diff] [blame] | 603 | generate_length_functions(symtab, instructions); |
Eric Anholt | c22c400 | 2010-03-26 18:20:30 -0700 | [diff] [blame] | 604 | /* FINISHME: distance() */ |
Eric Anholt | 76a91e1 | 2010-03-27 14:04:43 -0700 | [diff] [blame] | 605 | generate_dot_functions(symtab, instructions); |
Eric Anholt | c22c400 | 2010-03-26 18:20:30 -0700 | [diff] [blame] | 606 | /* FINISHME: cross() */ |
Eric Anholt | 9257592 | 2010-04-08 14:34:38 -0700 | [diff] [blame] | 607 | make_gentype_function(symtab, instructions, "normalize", 1, |
| 608 | generate_normalize); |
Eric Anholt | c22c400 | 2010-03-26 18:20:30 -0700 | [diff] [blame] | 609 | /* FINISHME: normalize() */ |
| 610 | /* FINISHME: ftransform() */ |
| 611 | /* FINISHME: faceforward() */ |
| 612 | /* FINISHME: reflect() */ |
| 613 | /* FINISHME: refract() */ |
| 614 | /* FINISHME: matrixCompMult() */ |
| 615 | /* FINISHME: lessThan() */ |
| 616 | /* FINISHME: lessThanEqual() */ |
| 617 | /* FINISHME: greaterThan() */ |
| 618 | /* FINISHME: greaterThanEqual() */ |
| 619 | /* FINISHME: equal() */ |
| 620 | /* FINISHME: notEqual() */ |
Eric Anholt | feeb43b | 2010-04-08 15:02:59 -0700 | [diff] [blame^] | 621 | generate_any_functions(symtab, instructions); |
| 622 | generate_all_functions(symtab, instructions); |
| 623 | generate_not_functions(symtab, instructions); |
Eric Anholt | c22c400 | 2010-03-26 18:20:30 -0700 | [diff] [blame] | 624 | /* FINISHME: texture*() */ |
| 625 | /* FINISHME: shadow*() */ |
| 626 | /* FINISHME: dFd[xy]() */ |
| 627 | /* FINISHME: fwidth() */ |
| 628 | } |
| 629 | |
| 630 | void |
| 631 | _mesa_glsl_initialize_functions(exec_list *instructions, |
| 632 | struct _mesa_glsl_parse_state *state) |
| 633 | { |
| 634 | generate_110_functions(state->symbols, instructions); |
| 635 | } |