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> |
| 25 | #include "glsl_symbol_table.h" |
| 26 | #include "glsl_parser_extras.h" |
| 27 | #include "glsl_types.h" |
| 28 | #include "ir.h" |
| 29 | |
| 30 | static void |
Eric Anholt | 2eec73f | 2010-03-27 12:25:20 -0700 | [diff] [blame] | 31 | generate_unop(exec_list *instructions, |
| 32 | ir_variable **declarations, |
| 33 | const glsl_type *type, |
| 34 | enum ir_expression_operation op) |
Eric Anholt | c22c400 | 2010-03-26 18:20:30 -0700 | [diff] [blame] | 35 | { |
| 36 | ir_dereference *const retval = new ir_dereference(declarations[16]); |
| 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 | |
| 42 | ir_instruction *inst = new ir_assignment(retval, result, NULL); |
| 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 | { |
| 52 | ir_dereference *const retval = new ir_dereference(declarations[16]); |
| 53 | ir_dereference *const arg1 = new ir_dereference(declarations[0]); |
| 54 | ir_dereference *const arg2 = new ir_dereference(declarations[1]); |
| 55 | ir_rvalue *result; |
| 56 | |
| 57 | result = new ir_expression(op, type, arg1, arg2); |
| 58 | |
| 59 | ir_instruction *inst = new ir_assignment(retval, result, NULL); |
| 60 | instructions->push_tail(inst); |
| 61 | } |
| 62 | |
| 63 | static void |
Eric Anholt | 2eec73f | 2010-03-27 12:25:20 -0700 | [diff] [blame] | 64 | generate_exp(exec_list *instructions, |
| 65 | ir_variable **declarations, |
| 66 | const glsl_type *type) |
| 67 | { |
| 68 | generate_unop(instructions, declarations, type, ir_unop_exp); |
| 69 | } |
| 70 | |
| 71 | static void |
| 72 | generate_log(exec_list *instructions, |
| 73 | ir_variable **declarations, |
| 74 | const glsl_type *type) |
| 75 | { |
| 76 | generate_unop(instructions, declarations, type, ir_unop_log); |
| 77 | } |
| 78 | |
| 79 | static void |
| 80 | generate_rsq(exec_list *instructions, |
| 81 | ir_variable **declarations, |
| 82 | const glsl_type *type) |
| 83 | { |
| 84 | generate_unop(instructions, declarations, type, ir_unop_rsq); |
| 85 | } |
| 86 | |
| 87 | static void |
| 88 | generate_abs(exec_list *instructions, |
| 89 | ir_variable **declarations, |
| 90 | const glsl_type *type) |
| 91 | { |
| 92 | generate_unop(instructions, declarations, type, ir_unop_abs); |
| 93 | } |
| 94 | |
| 95 | static void |
| 96 | generate_ceil(exec_list *instructions, |
| 97 | ir_variable **declarations, |
| 98 | const glsl_type *type) |
| 99 | { |
| 100 | generate_unop(instructions, declarations, type, ir_unop_ceil); |
| 101 | } |
| 102 | |
| 103 | static void |
| 104 | generate_floor(exec_list *instructions, |
| 105 | ir_variable **declarations, |
| 106 | const glsl_type *type) |
| 107 | { |
| 108 | generate_unop(instructions, declarations, type, ir_unop_floor); |
| 109 | } |
| 110 | |
Eric Anholt | bfe380a | 2010-03-27 12:43:13 -0700 | [diff] [blame] | 111 | static void |
| 112 | generate_mod(exec_list *instructions, |
| 113 | ir_variable **declarations, |
| 114 | const glsl_type *type) |
| 115 | { |
| 116 | generate_binop(instructions, declarations, type, ir_binop_mod); |
| 117 | } |
| 118 | |
| 119 | static void |
| 120 | generate_min(exec_list *instructions, |
| 121 | ir_variable **declarations, |
| 122 | const glsl_type *type) |
| 123 | { |
| 124 | generate_binop(instructions, declarations, type, ir_binop_min); |
| 125 | } |
| 126 | |
| 127 | static void |
| 128 | generate_max(exec_list *instructions, |
| 129 | ir_variable **declarations, |
| 130 | const glsl_type *type) |
| 131 | { |
| 132 | generate_binop(instructions, declarations, type, ir_binop_max); |
| 133 | } |
| 134 | |
Eric Anholt | ddd2e83 | 2010-03-27 12:59:42 -0700 | [diff] [blame^] | 135 | |
| 136 | static void |
| 137 | generate_pow(exec_list *instructions, |
| 138 | ir_variable **declarations, |
| 139 | const glsl_type *type) |
| 140 | { |
| 141 | generate_binop(instructions, declarations, type, ir_binop_pow); |
| 142 | } |
| 143 | |
Eric Anholt | c22c400 | 2010-03-26 18:20:30 -0700 | [diff] [blame] | 144 | void |
| 145 | generate_function_instance(ir_function *f, |
| 146 | const char *name, |
| 147 | exec_list *instructions, |
Eric Anholt | bfe380a | 2010-03-27 12:43:13 -0700 | [diff] [blame] | 148 | int n_args, |
Eric Anholt | c22c400 | 2010-03-26 18:20:30 -0700 | [diff] [blame] | 149 | void (*generate)(exec_list *instructions, |
| 150 | ir_variable **declarations, |
| 151 | const glsl_type *type), |
| 152 | const glsl_type *type) |
| 153 | { |
| 154 | ir_variable *declarations[17]; |
| 155 | |
| 156 | ir_function_signature *const sig = new ir_function_signature(type); |
| 157 | f->signatures.push_tail(sig); |
| 158 | |
| 159 | ir_label *const label = new ir_label(name); |
| 160 | instructions->push_tail(label); |
| 161 | sig->definition = label; |
Eric Anholt | bfe380a | 2010-03-27 12:43:13 -0700 | [diff] [blame] | 162 | static const char *arg_names[] = { |
| 163 | "arg0", |
| 164 | "arg1" |
| 165 | }; |
| 166 | int i; |
Eric Anholt | c22c400 | 2010-03-26 18:20:30 -0700 | [diff] [blame] | 167 | |
Eric Anholt | bfe380a | 2010-03-27 12:43:13 -0700 | [diff] [blame] | 168 | for (i = 0; i < n_args; i++) { |
| 169 | ir_variable *var = new ir_variable(type, arg_names[i]); |
Eric Anholt | c22c400 | 2010-03-26 18:20:30 -0700 | [diff] [blame] | 170 | |
Eric Anholt | bfe380a | 2010-03-27 12:43:13 -0700 | [diff] [blame] | 171 | var->mode = ir_var_in; |
| 172 | sig->parameters.push_tail(var); |
Eric Anholt | c22c400 | 2010-03-26 18:20:30 -0700 | [diff] [blame] | 173 | |
Eric Anholt | bfe380a | 2010-03-27 12:43:13 -0700 | [diff] [blame] | 174 | var = new ir_variable(type, arg_names[i]); |
Eric Anholt | c22c400 | 2010-03-26 18:20:30 -0700 | [diff] [blame] | 175 | |
Eric Anholt | bfe380a | 2010-03-27 12:43:13 -0700 | [diff] [blame] | 176 | declarations[i] = var; |
| 177 | } |
Eric Anholt | c22c400 | 2010-03-26 18:20:30 -0700 | [diff] [blame] | 178 | |
| 179 | ir_variable *retval = new ir_variable(type, "__retval"); |
| 180 | instructions->push_tail(retval); |
| 181 | |
| 182 | declarations[16] = retval; |
| 183 | |
| 184 | generate(instructions, declarations, type); |
| 185 | } |
| 186 | |
| 187 | void |
| 188 | make_gentype_function(glsl_symbol_table *symtab, exec_list *instructions, |
| 189 | const char *name, |
Eric Anholt | bfe380a | 2010-03-27 12:43:13 -0700 | [diff] [blame] | 190 | int n_args, |
Eric Anholt | c22c400 | 2010-03-26 18:20:30 -0700 | [diff] [blame] | 191 | void (*generate)(exec_list *instructions, |
| 192 | ir_variable **declarations, |
| 193 | const glsl_type *type)) |
| 194 | { |
| 195 | ir_function *const f = new ir_function(name); |
| 196 | const glsl_type *vec2_type = glsl_type::get_instance(GLSL_TYPE_FLOAT, 2, 1); |
| 197 | const glsl_type *vec3_type = glsl_type::get_instance(GLSL_TYPE_FLOAT, 3, 1); |
| 198 | const glsl_type *vec4_type = glsl_type::get_instance(GLSL_TYPE_FLOAT, 4, 1); |
| 199 | |
| 200 | bool added = symtab->add_function(name, f); |
| 201 | assert(added); |
| 202 | |
Eric Anholt | bfe380a | 2010-03-27 12:43:13 -0700 | [diff] [blame] | 203 | generate_function_instance(f, name, instructions, n_args, generate, |
| 204 | glsl_type::float_type); |
| 205 | generate_function_instance(f, name, instructions, n_args, generate, |
| 206 | vec2_type); |
| 207 | generate_function_instance(f, name, instructions, n_args, generate, |
| 208 | vec3_type); |
| 209 | generate_function_instance(f, name, instructions, n_args, generate, |
| 210 | vec4_type); |
Eric Anholt | c22c400 | 2010-03-26 18:20:30 -0700 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | void |
| 214 | generate_110_functions(glsl_symbol_table *symtab, exec_list *instructions) |
| 215 | { |
| 216 | /* FINISHME: radians() */ |
| 217 | /* FINISHME: degrees() */ |
| 218 | /* FINISHME: sin() */ |
| 219 | /* FINISHME: cos() */ |
| 220 | /* FINISHME: tan() */ |
| 221 | /* FINISHME: asin() */ |
| 222 | /* FINISHME: acos() */ |
| 223 | /* FINISHME: atan(y,x) */ |
| 224 | /* FINISHME: atan(y/x) */ |
Eric Anholt | ddd2e83 | 2010-03-27 12:59:42 -0700 | [diff] [blame^] | 225 | make_gentype_function(symtab, instructions, "pow", 2, generate_pow); |
Eric Anholt | bfe380a | 2010-03-27 12:43:13 -0700 | [diff] [blame] | 226 | make_gentype_function(symtab, instructions, "exp", 1, generate_exp); |
| 227 | make_gentype_function(symtab, instructions, "log", 1, generate_log); |
Eric Anholt | c22c400 | 2010-03-26 18:20:30 -0700 | [diff] [blame] | 228 | /* FINISHME: exp2() */ |
| 229 | /* FINISHME: log2() */ |
| 230 | /* FINISHME: sqrt() */ |
Eric Anholt | bfe380a | 2010-03-27 12:43:13 -0700 | [diff] [blame] | 231 | make_gentype_function(symtab, instructions, "inversesqrt", 1, generate_rsq); |
| 232 | make_gentype_function(symtab, instructions, "abs", 1, generate_abs); |
Eric Anholt | c22c400 | 2010-03-26 18:20:30 -0700 | [diff] [blame] | 233 | /* FINISHME: sign() */ |
Eric Anholt | bfe380a | 2010-03-27 12:43:13 -0700 | [diff] [blame] | 234 | make_gentype_function(symtab, instructions, "floor", 1, generate_floor); |
| 235 | make_gentype_function(symtab, instructions, "ceil", 1, generate_ceil); |
Eric Anholt | c22c400 | 2010-03-26 18:20:30 -0700 | [diff] [blame] | 236 | /* FINISHME: fract() */ |
| 237 | /* FINISHME: mod(x, float y) */ |
Eric Anholt | bfe380a | 2010-03-27 12:43:13 -0700 | [diff] [blame] | 238 | make_gentype_function(symtab, instructions, "mod", 2, generate_mod); |
| 239 | make_gentype_function(symtab, instructions, "min", 2, generate_min); |
| 240 | /* FINISHME: min(x, float y) */ |
| 241 | make_gentype_function(symtab, instructions, "max", 2, generate_max); |
| 242 | /* FINISHME: max(x, float y) */ |
Eric Anholt | c22c400 | 2010-03-26 18:20:30 -0700 | [diff] [blame] | 243 | /* FINISHME: clamp() */ |
| 244 | /* FINISHME: clamp() */ |
| 245 | /* FINISHME: mix() */ |
| 246 | /* FINISHME: mix() */ |
| 247 | /* FINISHME: step() */ |
| 248 | /* FINISHME: step() */ |
| 249 | /* FINISHME: smoothstep() */ |
| 250 | /* FINISHME: smoothstep() */ |
| 251 | /* FINISHME: floor() */ |
| 252 | /* FINISHME: step() */ |
| 253 | /* FINISHME: length() */ |
| 254 | /* FINISHME: distance() */ |
| 255 | /* FINISHME: dot() */ |
| 256 | /* FINISHME: cross() */ |
| 257 | /* FINISHME: normalize() */ |
| 258 | /* FINISHME: ftransform() */ |
| 259 | /* FINISHME: faceforward() */ |
| 260 | /* FINISHME: reflect() */ |
| 261 | /* FINISHME: refract() */ |
| 262 | /* FINISHME: matrixCompMult() */ |
| 263 | /* FINISHME: lessThan() */ |
| 264 | /* FINISHME: lessThanEqual() */ |
| 265 | /* FINISHME: greaterThan() */ |
| 266 | /* FINISHME: greaterThanEqual() */ |
| 267 | /* FINISHME: equal() */ |
| 268 | /* FINISHME: notEqual() */ |
| 269 | /* FINISHME: any() */ |
| 270 | /* FINISHME: all() */ |
| 271 | /* FINISHME: not() */ |
| 272 | /* FINISHME: texture*() */ |
| 273 | /* FINISHME: shadow*() */ |
| 274 | /* FINISHME: dFd[xy]() */ |
| 275 | /* FINISHME: fwidth() */ |
| 276 | } |
| 277 | |
| 278 | void |
| 279 | _mesa_glsl_initialize_functions(exec_list *instructions, |
| 280 | struct _mesa_glsl_parse_state *state) |
| 281 | { |
| 282 | generate_110_functions(state->symbols, instructions); |
| 283 | } |