Ian Romanick | 548fa29 | 2010-03-15 13:04:13 -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 | |
Ian Romanick | 8bde4ce | 2010-03-19 11:57:24 -0700 | [diff] [blame] | 24 | #include "glsl_symbol_table.h" |
Ian Romanick | 548fa29 | 2010-03-15 13:04:13 -0700 | [diff] [blame] | 25 | #include "ast.h" |
| 26 | #include "glsl_types.h" |
| 27 | #include "ir.h" |
| 28 | |
Ian Romanick | 81c7e94 | 2010-06-25 16:10:43 -0700 | [diff] [blame] | 29 | inline unsigned min(unsigned a, unsigned b) |
| 30 | { |
| 31 | return (a < b) ? a : b; |
| 32 | } |
| 33 | |
Kenneth Graunke | 17a307d | 2010-07-14 13:22:07 -0700 | [diff] [blame] | 34 | static ir_rvalue * |
| 35 | convert_component(ir_rvalue *src, const glsl_type *desired_type); |
| 36 | |
Ian Romanick | 68515ee | 2010-03-31 16:28:51 -0700 | [diff] [blame] | 37 | static unsigned |
| 38 | process_parameters(exec_list *instructions, exec_list *actual_parameters, |
Ian Romanick | 304ea90 | 2010-05-10 11:17:53 -0700 | [diff] [blame] | 39 | exec_list *parameters, |
Ian Romanick | 68515ee | 2010-03-31 16:28:51 -0700 | [diff] [blame] | 40 | struct _mesa_glsl_parse_state *state) |
| 41 | { |
Ian Romanick | 68515ee | 2010-03-31 16:28:51 -0700 | [diff] [blame] | 42 | unsigned count = 0; |
| 43 | |
Ian Romanick | 304ea90 | 2010-05-10 11:17:53 -0700 | [diff] [blame] | 44 | foreach_list (n, parameters) { |
| 45 | ast_node *const ast = exec_node_data(ast_node, n, link); |
Ian Romanick | 1a872b1 | 2010-06-09 17:31:02 -0700 | [diff] [blame] | 46 | ir_rvalue *result = ast->hir(instructions, state); |
| 47 | |
| 48 | ir_constant *const constant = result->constant_expression_value(); |
| 49 | if (constant != NULL) |
| 50 | result = constant; |
Ian Romanick | 68515ee | 2010-03-31 16:28:51 -0700 | [diff] [blame] | 51 | |
Ian Romanick | 3521f0b | 2010-05-10 10:47:14 -0700 | [diff] [blame] | 52 | actual_parameters->push_tail(result); |
| 53 | count++; |
Ian Romanick | 68515ee | 2010-03-31 16:28:51 -0700 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | return count; |
| 57 | } |
| 58 | |
| 59 | |
| 60 | static ir_rvalue * |
| 61 | process_call(exec_list *instructions, ir_function *f, |
| 62 | YYLTYPE *loc, exec_list *actual_parameters, |
| 63 | struct _mesa_glsl_parse_state *state) |
| 64 | { |
Kenneth Graunke | 953ff12 | 2010-06-25 13:14:37 -0700 | [diff] [blame] | 65 | void *ctx = state; |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 66 | |
Ian Romanick | 68515ee | 2010-03-31 16:28:51 -0700 | [diff] [blame] | 67 | const ir_function_signature *sig = |
| 68 | f->matching_signature(actual_parameters); |
| 69 | |
| 70 | /* The instructions param will be used when the FINISHMEs below are done */ |
| 71 | (void) instructions; |
| 72 | |
| 73 | if (sig != NULL) { |
Ian Romanick | c35bb00 | 2010-04-02 15:51:02 -0700 | [diff] [blame] | 74 | /* Verify that 'out' and 'inout' actual parameters are lvalues. This |
| 75 | * isn't done in ir_function::matching_signature because that function |
| 76 | * cannot generate the necessary diagnostics. |
| 77 | */ |
| 78 | exec_list_iterator actual_iter = actual_parameters->iterator(); |
| 79 | exec_list_iterator formal_iter = sig->parameters.iterator(); |
| 80 | |
| 81 | while (actual_iter.has_next()) { |
Eric Anholt | f1ddca9 | 2010-04-07 12:35:34 -0700 | [diff] [blame] | 82 | ir_rvalue *actual = (ir_rvalue *) actual_iter.get(); |
| 83 | ir_variable *formal = (ir_variable *) formal_iter.get(); |
Ian Romanick | c35bb00 | 2010-04-02 15:51:02 -0700 | [diff] [blame] | 84 | |
| 85 | assert(actual != NULL); |
| 86 | assert(formal != NULL); |
| 87 | |
| 88 | if ((formal->mode == ir_var_out) |
| 89 | || (formal->mode == ir_var_inout)) { |
| 90 | if (! actual->is_lvalue()) { |
| 91 | /* FINISHME: Log a better diagnostic here. There is no way |
| 92 | * FINISHME: to tell the user which parameter is invalid. |
| 93 | */ |
| 94 | _mesa_glsl_error(loc, state, "`%s' parameter is not lvalue", |
| 95 | (formal->mode == ir_var_out) ? "out" : "inout"); |
| 96 | } |
| 97 | } |
| 98 | |
Kenneth Graunke | 17a307d | 2010-07-14 13:22:07 -0700 | [diff] [blame] | 99 | if (formal->type->is_numeric() || formal->type->is_boolean()) { |
| 100 | ir_rvalue *converted = convert_component(actual, formal->type); |
| 101 | actual->replace_with(converted); |
| 102 | } |
| 103 | |
Ian Romanick | c35bb00 | 2010-04-02 15:51:02 -0700 | [diff] [blame] | 104 | actual_iter.next(); |
| 105 | formal_iter.next(); |
| 106 | } |
| 107 | |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 108 | return new(ctx) ir_call(sig, actual_parameters); |
Ian Romanick | 68515ee | 2010-03-31 16:28:51 -0700 | [diff] [blame] | 109 | } else { |
| 110 | /* FINISHME: Log a better error message here. G++ will show the types |
| 111 | * FINISHME: of the actual parameters and the set of candidate |
| 112 | * FINISHME: functions. A different error should also be logged when |
| 113 | * FINISHME: multiple functions match. |
| 114 | */ |
| 115 | _mesa_glsl_error(loc, state, "no matching function for call to `%s'", |
| 116 | f->name); |
Carl Worth | e01193a | 2010-06-23 18:25:04 -0700 | [diff] [blame] | 117 | return ir_call::get_error_instruction(ctx); |
Ian Romanick | 68515ee | 2010-03-31 16:28:51 -0700 | [diff] [blame] | 118 | } |
| 119 | } |
| 120 | |
| 121 | |
Kenneth Graunke | fb9fb5f | 2010-03-26 00:25:36 -0700 | [diff] [blame] | 122 | static ir_rvalue * |
Ian Romanick | f474961 | 2010-03-15 13:26:02 -0700 | [diff] [blame] | 123 | match_function_by_name(exec_list *instructions, const char *name, |
Ian Romanick | c077131 | 2010-06-09 17:23:26 -0700 | [diff] [blame] | 124 | YYLTYPE *loc, exec_list *actual_parameters, |
Ian Romanick | f474961 | 2010-03-15 13:26:02 -0700 | [diff] [blame] | 125 | struct _mesa_glsl_parse_state *state) |
| 126 | { |
Kenneth Graunke | 953ff12 | 2010-06-25 13:14:37 -0700 | [diff] [blame] | 127 | void *ctx = state; |
Ian Romanick | 8bde4ce | 2010-03-19 11:57:24 -0700 | [diff] [blame] | 128 | ir_function *f = state->symbols->get_function(name); |
Ian Romanick | f474961 | 2010-03-15 13:26:02 -0700 | [diff] [blame] | 129 | |
| 130 | if (f == NULL) { |
| 131 | _mesa_glsl_error(loc, state, "function `%s' undeclared", name); |
Carl Worth | e01193a | 2010-06-23 18:25:04 -0700 | [diff] [blame] | 132 | return ir_call::get_error_instruction(ctx); |
Ian Romanick | f474961 | 2010-03-15 13:26:02 -0700 | [diff] [blame] | 133 | } |
| 134 | |
Ian Romanick | c077131 | 2010-06-09 17:23:26 -0700 | [diff] [blame] | 135 | /* Once we've determined that the function being called might exist, try |
| 136 | * to find an overload of the function that matches the parameters. |
Ian Romanick | f474961 | 2010-03-15 13:26:02 -0700 | [diff] [blame] | 137 | */ |
Ian Romanick | c077131 | 2010-06-09 17:23:26 -0700 | [diff] [blame] | 138 | return process_call(instructions, f, loc, actual_parameters, state); |
Ian Romanick | f474961 | 2010-03-15 13:26:02 -0700 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | |
Ian Romanick | 0b7dcc8 | 2010-03-26 17:38:58 -0700 | [diff] [blame] | 142 | /** |
| 143 | * Perform automatic type conversion of constructor parameters |
| 144 | */ |
| 145 | static ir_rvalue * |
| 146 | convert_component(ir_rvalue *src, const glsl_type *desired_type) |
| 147 | { |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 148 | void *ctx = talloc_parent(src); |
Ian Romanick | 0b7dcc8 | 2010-03-26 17:38:58 -0700 | [diff] [blame] | 149 | const unsigned a = desired_type->base_type; |
| 150 | const unsigned b = src->type->base_type; |
Ian Romanick | 00eb466 | 2010-06-07 15:08:04 -0700 | [diff] [blame] | 151 | ir_expression *result = NULL; |
Ian Romanick | 0b7dcc8 | 2010-03-26 17:38:58 -0700 | [diff] [blame] | 152 | |
| 153 | if (src->type->is_error()) |
| 154 | return src; |
| 155 | |
| 156 | assert(a <= GLSL_TYPE_BOOL); |
| 157 | assert(b <= GLSL_TYPE_BOOL); |
| 158 | |
| 159 | if ((a == b) || (src->type->is_integer() && desired_type->is_integer())) |
| 160 | return src; |
| 161 | |
| 162 | switch (a) { |
| 163 | case GLSL_TYPE_UINT: |
| 164 | case GLSL_TYPE_INT: |
| 165 | if (b == GLSL_TYPE_FLOAT) |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 166 | result = new(ctx) ir_expression(ir_unop_f2i, desired_type, src, NULL); |
Ian Romanick | 0b7dcc8 | 2010-03-26 17:38:58 -0700 | [diff] [blame] | 167 | else { |
| 168 | assert(b == GLSL_TYPE_BOOL); |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 169 | result = new(ctx) ir_expression(ir_unop_b2i, desired_type, src, NULL); |
Ian Romanick | 0b7dcc8 | 2010-03-26 17:38:58 -0700 | [diff] [blame] | 170 | } |
Ian Romanick | 565185c | 2010-06-11 13:49:00 -0700 | [diff] [blame] | 171 | break; |
Ian Romanick | 0b7dcc8 | 2010-03-26 17:38:58 -0700 | [diff] [blame] | 172 | case GLSL_TYPE_FLOAT: |
| 173 | switch (b) { |
| 174 | case GLSL_TYPE_UINT: |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 175 | result = new(ctx) ir_expression(ir_unop_u2f, desired_type, src, NULL); |
Ian Romanick | 00eb466 | 2010-06-07 15:08:04 -0700 | [diff] [blame] | 176 | break; |
Ian Romanick | 0b7dcc8 | 2010-03-26 17:38:58 -0700 | [diff] [blame] | 177 | case GLSL_TYPE_INT: |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 178 | result = new(ctx) ir_expression(ir_unop_i2f, desired_type, src, NULL); |
Ian Romanick | 00eb466 | 2010-06-07 15:08:04 -0700 | [diff] [blame] | 179 | break; |
Ian Romanick | 0b7dcc8 | 2010-03-26 17:38:58 -0700 | [diff] [blame] | 180 | case GLSL_TYPE_BOOL: |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 181 | result = new(ctx) ir_expression(ir_unop_b2f, desired_type, src, NULL); |
Ian Romanick | 00eb466 | 2010-06-07 15:08:04 -0700 | [diff] [blame] | 182 | break; |
Ian Romanick | 0b7dcc8 | 2010-03-26 17:38:58 -0700 | [diff] [blame] | 183 | } |
| 184 | break; |
Ian Romanick | 26b5d33 | 2010-06-25 16:19:45 -0700 | [diff] [blame] | 185 | case GLSL_TYPE_BOOL: |
Ian Romanick | b74b43e | 2010-06-11 16:52:09 -0700 | [diff] [blame] | 186 | switch (b) { |
Ian Romanick | 26b5d33 | 2010-06-25 16:19:45 -0700 | [diff] [blame] | 187 | case GLSL_TYPE_UINT: |
| 188 | case GLSL_TYPE_INT: |
| 189 | result = new(ctx) ir_expression(ir_unop_i2b, desired_type, src, NULL); |
| 190 | break; |
| 191 | case GLSL_TYPE_FLOAT: |
| 192 | result = new(ctx) ir_expression(ir_unop_f2b, desired_type, src, NULL); |
| 193 | break; |
Ian Romanick | b74b43e | 2010-06-11 16:52:09 -0700 | [diff] [blame] | 194 | } |
Ian Romanick | 26b5d33 | 2010-06-25 16:19:45 -0700 | [diff] [blame] | 195 | break; |
Ian Romanick | 0b7dcc8 | 2010-03-26 17:38:58 -0700 | [diff] [blame] | 196 | } |
| 197 | |
Ian Romanick | 00eb466 | 2010-06-07 15:08:04 -0700 | [diff] [blame] | 198 | assert(result != NULL); |
| 199 | |
| 200 | ir_constant *const constant = result->constant_expression_value(); |
| 201 | return (constant != NULL) ? (ir_rvalue *) constant : (ir_rvalue *) result; |
Ian Romanick | 0b7dcc8 | 2010-03-26 17:38:58 -0700 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | |
| 205 | /** |
| 206 | * Dereference a specific component from a scalar, vector, or matrix |
| 207 | */ |
| 208 | static ir_rvalue * |
| 209 | dereference_component(ir_rvalue *src, unsigned component) |
| 210 | { |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 211 | void *ctx = talloc_parent(src); |
Ian Romanick | 0b7dcc8 | 2010-03-26 17:38:58 -0700 | [diff] [blame] | 212 | assert(component < src->type->components()); |
| 213 | |
Ian Romanick | c9cb103 | 2010-06-04 16:20:35 -0700 | [diff] [blame] | 214 | /* If the source is a constant, just create a new constant instead of a |
| 215 | * dereference of the existing constant. |
| 216 | */ |
| 217 | ir_constant *constant = src->as_constant(); |
| 218 | if (constant) |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 219 | return new(ctx) ir_constant(constant, component); |
Ian Romanick | c9cb103 | 2010-06-04 16:20:35 -0700 | [diff] [blame] | 220 | |
Ian Romanick | 0b7dcc8 | 2010-03-26 17:38:58 -0700 | [diff] [blame] | 221 | if (src->type->is_scalar()) { |
| 222 | return src; |
| 223 | } else if (src->type->is_vector()) { |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 224 | return new(ctx) ir_swizzle(src, component, 0, 0, 0, 1); |
Ian Romanick | 0b7dcc8 | 2010-03-26 17:38:58 -0700 | [diff] [blame] | 225 | } else { |
| 226 | assert(src->type->is_matrix()); |
| 227 | |
| 228 | /* Dereference a row of the matrix, then call this function again to get |
| 229 | * a specific element from that row. |
| 230 | */ |
| 231 | const int c = component / src->type->column_type()->vector_elements; |
| 232 | const int r = component % src->type->column_type()->vector_elements; |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 233 | ir_constant *const col_index = new(ctx) ir_constant(c); |
| 234 | ir_dereference *const col = new(ctx) ir_dereference_array(src, col_index); |
Ian Romanick | 0b7dcc8 | 2010-03-26 17:38:58 -0700 | [diff] [blame] | 235 | |
| 236 | col->type = src->type->column_type(); |
| 237 | |
| 238 | return dereference_component(col, r); |
| 239 | } |
| 240 | |
| 241 | assert(!"Should not get here."); |
| 242 | return NULL; |
| 243 | } |
| 244 | |
| 245 | |
Ian Romanick | 00aa173 | 2010-03-31 16:48:48 -0700 | [diff] [blame] | 246 | static ir_rvalue * |
| 247 | process_array_constructor(exec_list *instructions, |
| 248 | const glsl_type *constructor_type, |
Ian Romanick | 304ea90 | 2010-05-10 11:17:53 -0700 | [diff] [blame] | 249 | YYLTYPE *loc, exec_list *parameters, |
Ian Romanick | 00aa173 | 2010-03-31 16:48:48 -0700 | [diff] [blame] | 250 | struct _mesa_glsl_parse_state *state) |
| 251 | { |
Kenneth Graunke | 953ff12 | 2010-06-25 13:14:37 -0700 | [diff] [blame] | 252 | void *ctx = state; |
Ian Romanick | 00aa173 | 2010-03-31 16:48:48 -0700 | [diff] [blame] | 253 | /* Array constructors come in two forms: sized and unsized. Sized array |
| 254 | * constructors look like 'vec4[2](a, b)', where 'a' and 'b' are vec4 |
| 255 | * variables. In this case the number of parameters must exactly match the |
| 256 | * specified size of the array. |
| 257 | * |
| 258 | * Unsized array constructors look like 'vec4[](a, b)', where 'a' and 'b' |
| 259 | * are vec4 variables. In this case the size of the array being constructed |
| 260 | * is determined by the number of parameters. |
| 261 | * |
| 262 | * From page 52 (page 58 of the PDF) of the GLSL 1.50 spec: |
| 263 | * |
| 264 | * "There must be exactly the same number of arguments as the size of |
| 265 | * the array being constructed. If no size is present in the |
| 266 | * constructor, then the array is explicitly sized to the number of |
| 267 | * arguments provided. The arguments are assigned in order, starting at |
| 268 | * element 0, to the elements of the constructed array. Each argument |
| 269 | * must be the same type as the element type of the array, or be a type |
| 270 | * that can be converted to the element type of the array according to |
| 271 | * Section 4.1.10 "Implicit Conversions."" |
| 272 | */ |
| 273 | exec_list actual_parameters; |
| 274 | const unsigned parameter_count = |
| 275 | process_parameters(instructions, &actual_parameters, parameters, state); |
| 276 | |
| 277 | if ((parameter_count == 0) |
| 278 | || ((constructor_type->length != 0) |
| 279 | && (constructor_type->length != parameter_count))) { |
| 280 | const unsigned min_param = (constructor_type->length == 0) |
| 281 | ? 1 : constructor_type->length; |
| 282 | |
| 283 | _mesa_glsl_error(loc, state, "array constructor must have %s %u " |
| 284 | "parameter%s", |
| 285 | (constructor_type->length != 0) ? "at least" : "exactly", |
| 286 | min_param, (min_param <= 1) ? "" : "s"); |
Carl Worth | e01193a | 2010-06-23 18:25:04 -0700 | [diff] [blame] | 287 | return ir_call::get_error_instruction(ctx); |
Ian Romanick | 00aa173 | 2010-03-31 16:48:48 -0700 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | if (constructor_type->length == 0) { |
| 291 | constructor_type = |
Carl Worth | 12c4115 | 2010-06-18 17:52:59 -0700 | [diff] [blame] | 292 | glsl_type::get_array_instance(state, |
| 293 | constructor_type->element_type(), |
Ian Romanick | 00aa173 | 2010-03-31 16:48:48 -0700 | [diff] [blame] | 294 | parameter_count); |
| 295 | assert(constructor_type != NULL); |
| 296 | assert(constructor_type->length == parameter_count); |
| 297 | } |
| 298 | |
| 299 | ir_function *f = state->symbols->get_function(constructor_type->name); |
| 300 | |
| 301 | /* If the constructor for this type of array does not exist, generate the |
Ian Romanick | 82baaf4 | 2010-04-23 13:21:22 -0700 | [diff] [blame] | 302 | * prototype and add it to the symbol table. |
Ian Romanick | 00aa173 | 2010-03-31 16:48:48 -0700 | [diff] [blame] | 303 | */ |
| 304 | if (f == NULL) { |
Ian Romanick | 82baaf4 | 2010-04-23 13:21:22 -0700 | [diff] [blame] | 305 | f = constructor_type->generate_constructor(state->symbols); |
Ian Romanick | 00aa173 | 2010-03-31 16:48:48 -0700 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | ir_rvalue *const r = |
| 309 | process_call(instructions, f, loc, &actual_parameters, state); |
| 310 | |
| 311 | assert(r != NULL); |
| 312 | assert(r->type->is_error() || (r->type == constructor_type)); |
| 313 | |
| 314 | return r; |
| 315 | } |
| 316 | |
| 317 | |
Ian Romanick | ab92d0e | 2010-06-09 17:26:20 -0700 | [diff] [blame] | 318 | /** |
| 319 | * Try to convert a record constructor to a constant expression |
| 320 | */ |
| 321 | static ir_constant * |
| 322 | constant_record_constructor(const glsl_type *constructor_type, |
| 323 | YYLTYPE *loc, exec_list *parameters, |
| 324 | struct _mesa_glsl_parse_state *state) |
| 325 | { |
Kenneth Graunke | 953ff12 | 2010-06-25 13:14:37 -0700 | [diff] [blame] | 326 | void *ctx = state; |
Ian Romanick | ab92d0e | 2010-06-09 17:26:20 -0700 | [diff] [blame] | 327 | bool all_parameters_are_constant = true; |
| 328 | |
| 329 | exec_node *node = parameters->head; |
| 330 | for (unsigned i = 0; i < constructor_type->length; i++) { |
| 331 | ir_instruction *ir = (ir_instruction *) node; |
| 332 | |
| 333 | if (node->is_tail_sentinal()) { |
| 334 | _mesa_glsl_error(loc, state, |
| 335 | "insufficient parameters to constructor for `%s'", |
| 336 | constructor_type->name); |
| 337 | return NULL; |
| 338 | } |
| 339 | |
| 340 | if (ir->type != constructor_type->fields.structure[i].type) { |
| 341 | _mesa_glsl_error(loc, state, |
| 342 | "parameter type mismatch in constructor for `%s' " |
| 343 | " (%s vs %s)", |
| 344 | constructor_type->name, |
| 345 | ir->type->name, |
| 346 | constructor_type->fields.structure[i].type->name); |
| 347 | return NULL; |
| 348 | } |
| 349 | |
| 350 | if (ir->as_constant() == NULL) |
| 351 | all_parameters_are_constant = false; |
| 352 | |
| 353 | node = node->next; |
| 354 | } |
| 355 | |
| 356 | if (!all_parameters_are_constant) |
| 357 | return NULL; |
| 358 | |
Carl Worth | 1660a29 | 2010-06-23 18:11:51 -0700 | [diff] [blame] | 359 | return new(ctx) ir_constant(constructor_type, parameters); |
Ian Romanick | ab92d0e | 2010-06-09 17:26:20 -0700 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | |
Ian Romanick | be1d2bf | 2010-06-11 14:01:44 -0700 | [diff] [blame] | 363 | /** |
| 364 | * Generate data for a constant matrix constructor w/a single scalar parameter |
| 365 | * |
| 366 | * Matrix constructors in GLSL can be passed a single scalar of the |
| 367 | * approriate type. In these cases, the resulting matrix is the identity |
| 368 | * matrix multipled by the specified scalar. This function generates data for |
| 369 | * that matrix. |
| 370 | * |
| 371 | * \param type Type of the desired matrix. |
| 372 | * \param initializer Scalar value used to initialize the matrix diagonal. |
| 373 | * \param data Location to store the resulting matrix. |
| 374 | */ |
| 375 | void |
| 376 | generate_constructor_matrix(const glsl_type *type, ir_constant *initializer, |
| 377 | ir_constant_data *data) |
| 378 | { |
| 379 | switch (type->base_type) { |
| 380 | case GLSL_TYPE_UINT: |
| 381 | case GLSL_TYPE_INT: |
| 382 | for (unsigned i = 0; i < type->components(); i++) |
| 383 | data->u[i] = 0; |
| 384 | |
| 385 | for (unsigned i = 0; i < type->matrix_columns; i++) { |
| 386 | /* The array offset of the ith row and column of the matrix. |
| 387 | */ |
| 388 | const unsigned idx = (i * type->vector_elements) + i; |
| 389 | |
| 390 | data->u[idx] = initializer->value.u[0]; |
| 391 | } |
| 392 | break; |
| 393 | |
| 394 | case GLSL_TYPE_FLOAT: |
| 395 | for (unsigned i = 0; i < type->components(); i++) |
| 396 | data->f[i] = 0; |
| 397 | |
| 398 | for (unsigned i = 0; i < type->matrix_columns; i++) { |
| 399 | /* The array offset of the ith row and column of the matrix. |
| 400 | */ |
| 401 | const unsigned idx = (i * type->vector_elements) + i; |
| 402 | |
| 403 | data->f[idx] = initializer->value.f[0]; |
| 404 | } |
| 405 | |
| 406 | break; |
| 407 | |
| 408 | default: |
| 409 | assert(!"Should not get here."); |
| 410 | break; |
| 411 | } |
| 412 | } |
| 413 | |
| 414 | |
| 415 | /** |
| 416 | * Generate data for a constant vector constructor w/a single scalar parameter |
| 417 | * |
| 418 | * Vector constructors in GLSL can be passed a single scalar of the |
| 419 | * approriate type. In these cases, the resulting vector contains the specified |
| 420 | * value in all components. This function generates data for that vector. |
| 421 | * |
| 422 | * \param type Type of the desired vector. |
| 423 | * \param initializer Scalar value used to initialize the vector. |
| 424 | * \param data Location to store the resulting vector data. |
| 425 | */ |
| 426 | void |
| 427 | generate_constructor_vector(const glsl_type *type, ir_constant *initializer, |
| 428 | ir_constant_data *data) |
| 429 | { |
| 430 | switch (type->base_type) { |
| 431 | case GLSL_TYPE_UINT: |
| 432 | case GLSL_TYPE_INT: |
| 433 | for (unsigned i = 0; i < type->components(); i++) |
| 434 | data->u[i] = initializer->value.u[0]; |
| 435 | |
| 436 | break; |
| 437 | |
| 438 | case GLSL_TYPE_FLOAT: |
| 439 | for (unsigned i = 0; i < type->components(); i++) |
| 440 | data->f[i] = initializer->value.f[0]; |
| 441 | |
| 442 | break; |
| 443 | |
| 444 | case GLSL_TYPE_BOOL: |
| 445 | for (unsigned i = 0; i < type->components(); i++) |
| 446 | data->b[i] = initializer->value.b[0]; |
| 447 | |
| 448 | break; |
| 449 | |
| 450 | default: |
| 451 | assert(!"Should not get here."); |
| 452 | break; |
| 453 | } |
| 454 | } |
| 455 | |
| 456 | |
Ian Romanick | c31dcdf | 2010-06-23 15:19:40 -0700 | [diff] [blame] | 457 | /** |
| 458 | * Determine if a list consists of a single scalar r-value |
| 459 | */ |
| 460 | bool |
| 461 | single_scalar_parameter(exec_list *parameters) |
| 462 | { |
| 463 | const ir_rvalue *const p = (ir_rvalue *) parameters->head; |
| 464 | assert(((ir_rvalue *)p)->as_rvalue() != NULL); |
| 465 | |
| 466 | return (p->type->is_scalar() && p->next->is_tail_sentinal()); |
| 467 | } |
| 468 | |
| 469 | |
| 470 | /** |
| 471 | * Generate inline code for a vector constructor |
| 472 | * |
| 473 | * The generated constructor code will consist of a temporary variable |
| 474 | * declaration of the same type as the constructor. A sequence of assignments |
| 475 | * from constructor parameters to the temporary will follow. |
| 476 | * |
| 477 | * \return |
| 478 | * An \c ir_dereference_variable of the temprorary generated in the constructor |
| 479 | * body. |
| 480 | */ |
| 481 | ir_rvalue * |
| 482 | emit_inline_vector_constructor(const glsl_type *type, |
| 483 | exec_list *instructions, |
| 484 | exec_list *parameters, |
| 485 | void *ctx) |
| 486 | { |
| 487 | assert(!parameters->is_empty()); |
| 488 | |
Ian Romanick | 4b6feb0 | 2010-06-28 13:22:55 -0700 | [diff] [blame] | 489 | ir_variable *var = new(ctx) ir_variable(type, |
| 490 | talloc_strdup(ctx, "vec_ctor")); |
Ian Romanick | c31dcdf | 2010-06-23 15:19:40 -0700 | [diff] [blame] | 491 | instructions->push_tail(var); |
| 492 | |
| 493 | /* There are two kinds of vector constructors. |
| 494 | * |
| 495 | * - Construct a vector from a single scalar by replicating that scalar to |
| 496 | * all components of the vector. |
| 497 | * |
| 498 | * - Construct a vector from an arbirary combination of vectors and |
| 499 | * scalars. The components of the constructor parameters are assigned |
| 500 | * to the vector in order until the vector is full. |
| 501 | */ |
| 502 | const unsigned lhs_components = type->components(); |
| 503 | if (single_scalar_parameter(parameters)) { |
| 504 | ir_rvalue *first_param = (ir_rvalue *)parameters->head; |
| 505 | ir_rvalue *rhs = new(ctx) ir_swizzle(first_param, 0, 0, 0, 0, |
| 506 | lhs_components); |
| 507 | ir_dereference_variable *lhs = new(ctx) ir_dereference_variable(var); |
| 508 | |
| 509 | assert(rhs->type == lhs->type); |
| 510 | |
| 511 | ir_instruction *inst = new(ctx) ir_assignment(lhs, rhs, NULL); |
| 512 | instructions->push_tail(inst); |
| 513 | } else { |
| 514 | unsigned base_component = 0; |
| 515 | foreach_list(node, parameters) { |
| 516 | ir_rvalue *rhs = (ir_rvalue *) node; |
| 517 | unsigned rhs_components = rhs->type->components(); |
| 518 | |
| 519 | /* Do not try to assign more components to the vector than it has! |
| 520 | */ |
| 521 | if ((rhs_components + base_component) > lhs_components) { |
| 522 | rhs_components = lhs_components - base_component; |
| 523 | } |
| 524 | |
| 525 | /* Emit an assignment of the constructor parameter to the next set of |
| 526 | * components in the temporary variable. |
| 527 | */ |
| 528 | unsigned mask[4] = { 0, 0, 0, 0 }; |
| 529 | for (unsigned i = 0; i < rhs_components; i++) { |
| 530 | mask[i] = i + base_component; |
| 531 | } |
| 532 | |
| 533 | |
| 534 | ir_rvalue *lhs_ref = new(ctx) ir_dereference_variable(var); |
| 535 | ir_swizzle *lhs = new(ctx) ir_swizzle(lhs_ref, mask, rhs_components); |
| 536 | |
| 537 | ir_instruction *inst = new(ctx) ir_assignment(lhs, rhs, NULL); |
| 538 | instructions->push_tail(inst); |
| 539 | |
| 540 | /* Advance the component index by the number of components that were |
| 541 | * just assigned. |
| 542 | */ |
| 543 | base_component += rhs_components; |
| 544 | } |
| 545 | } |
| 546 | return new(ctx) ir_dereference_variable(var); |
| 547 | } |
| 548 | |
| 549 | |
Ian Romanick | 81c7e94 | 2010-06-25 16:10:43 -0700 | [diff] [blame] | 550 | /** |
| 551 | * Generate assignment of a portion of a vector to a portion of a matrix column |
| 552 | * |
| 553 | * \param src_base First component of the source to be used in assignment |
| 554 | * \param column Column of destination to be assiged |
| 555 | * \param row_base First component of the destination column to be assigned |
| 556 | * \param count Number of components to be assigned |
| 557 | * |
| 558 | * \note |
| 559 | * \c src_base + \c count must be less than or equal to the number of components |
| 560 | * in the source vector. |
| 561 | */ |
| 562 | ir_instruction * |
| 563 | assign_to_matrix_column(ir_variable *var, unsigned column, unsigned row_base, |
| 564 | ir_rvalue *src, unsigned src_base, unsigned count, |
| 565 | TALLOC_CTX *ctx) |
| 566 | { |
| 567 | const unsigned mask[8] = { 0, 1, 2, 3, 0, 0, 0, 0 }; |
| 568 | |
| 569 | ir_constant *col_idx = new(ctx) ir_constant(column); |
| 570 | ir_rvalue *column_ref = new(ctx) ir_dereference_array(var, col_idx); |
| 571 | |
| 572 | assert(column_ref->type->components() >= (row_base + count)); |
| 573 | ir_rvalue *lhs = new(ctx) ir_swizzle(column_ref, &mask[row_base], count); |
| 574 | |
| 575 | assert(src->type->components() >= (src_base + count)); |
| 576 | ir_rvalue *rhs = new(ctx) ir_swizzle(src, &mask[src_base], count); |
| 577 | |
| 578 | return new(ctx) ir_assignment(lhs, rhs, NULL); |
| 579 | } |
| 580 | |
| 581 | |
| 582 | /** |
| 583 | * Generate inline code for a matrix constructor |
| 584 | * |
| 585 | * The generated constructor code will consist of a temporary variable |
| 586 | * declaration of the same type as the constructor. A sequence of assignments |
| 587 | * from constructor parameters to the temporary will follow. |
| 588 | * |
| 589 | * \return |
| 590 | * An \c ir_dereference_variable of the temprorary generated in the constructor |
| 591 | * body. |
| 592 | */ |
| 593 | ir_rvalue * |
| 594 | emit_inline_matrix_constructor(const glsl_type *type, |
| 595 | exec_list *instructions, |
| 596 | exec_list *parameters, |
| 597 | void *ctx) |
| 598 | { |
| 599 | assert(!parameters->is_empty()); |
| 600 | |
Ian Romanick | 4b6feb0 | 2010-06-28 13:22:55 -0700 | [diff] [blame] | 601 | ir_variable *var = new(ctx) ir_variable(type, |
| 602 | talloc_strdup(ctx, "mat_ctor")); |
Ian Romanick | 81c7e94 | 2010-06-25 16:10:43 -0700 | [diff] [blame] | 603 | instructions->push_tail(var); |
| 604 | |
| 605 | /* There are three kinds of matrix constructors. |
| 606 | * |
| 607 | * - Construct a matrix from a single scalar by replicating that scalar to |
| 608 | * along the diagonal of the matrix and setting all other components to |
| 609 | * zero. |
| 610 | * |
| 611 | * - Construct a matrix from an arbirary combination of vectors and |
| 612 | * scalars. The components of the constructor parameters are assigned |
| 613 | * to the matrix in colum-major order until the matrix is full. |
| 614 | * |
| 615 | * - Construct a matrix from a single matrix. The source matrix is copied |
| 616 | * to the upper left portion of the constructed matrix, and the remaining |
| 617 | * elements take values from the identity matrix. |
| 618 | */ |
| 619 | ir_rvalue *const first_param = (ir_rvalue *) parameters->head; |
| 620 | if (single_scalar_parameter(parameters)) { |
| 621 | /* Assign the scalar to the X component of a vec4, and fill the remaining |
| 622 | * components with zero. |
| 623 | */ |
Ian Romanick | 4b6feb0 | 2010-06-28 13:22:55 -0700 | [diff] [blame] | 624 | ir_variable *rhs_var = |
| 625 | new(ctx) ir_variable(glsl_type::vec4_type, |
| 626 | talloc_strdup(ctx, "mat_ctor_vec")); |
Ian Romanick | 81c7e94 | 2010-06-25 16:10:43 -0700 | [diff] [blame] | 627 | instructions->push_tail(rhs_var); |
| 628 | |
| 629 | ir_constant_data zero; |
| 630 | zero.f[0] = 0.0; |
| 631 | zero.f[1] = 0.0; |
| 632 | zero.f[2] = 0.0; |
| 633 | zero.f[3] = 0.0; |
| 634 | |
| 635 | ir_instruction *inst = |
| 636 | new(ctx) ir_assignment(new(ctx) ir_dereference_variable(rhs_var), |
| 637 | new(ctx) ir_constant(rhs_var->type, &zero), |
| 638 | NULL); |
| 639 | instructions->push_tail(inst); |
| 640 | |
| 641 | ir_rvalue *const rhs_ref = new(ctx) ir_dereference_variable(rhs_var); |
| 642 | ir_rvalue *const x_of_rhs = new(ctx) ir_swizzle(rhs_ref, 0, 0, 0, 0, 1); |
| 643 | |
| 644 | inst = new(ctx) ir_assignment(x_of_rhs, first_param, NULL); |
| 645 | instructions->push_tail(inst); |
| 646 | |
| 647 | /* Assign the temporary vector to each column of the destination matrix |
| 648 | * with a swizzle that puts the X component on the diagonal of the |
| 649 | * matrix. In some cases this may mean that the X component does not |
| 650 | * get assigned into the column at all (i.e., when the matrix has more |
| 651 | * columns than rows). |
| 652 | */ |
| 653 | static const unsigned rhs_swiz[4][4] = { |
| 654 | { 0, 1, 1, 1 }, |
| 655 | { 1, 0, 1, 1 }, |
| 656 | { 1, 1, 0, 1 }, |
| 657 | { 1, 1, 1, 0 } |
| 658 | }; |
| 659 | |
| 660 | const unsigned cols_to_init = min(type->matrix_columns, |
| 661 | type->vector_elements); |
| 662 | for (unsigned i = 0; i < cols_to_init; i++) { |
| 663 | ir_constant *const col_idx = new(ctx) ir_constant(i); |
| 664 | ir_rvalue *const col_ref = new(ctx) ir_dereference_array(var, col_idx); |
| 665 | |
| 666 | ir_rvalue *const rhs_ref = new(ctx) ir_dereference_variable(rhs_var); |
| 667 | ir_rvalue *const rhs = new(ctx) ir_swizzle(rhs_ref, rhs_swiz[i], |
| 668 | type->vector_elements); |
| 669 | |
| 670 | inst = new(ctx) ir_assignment(col_ref, rhs, NULL); |
| 671 | instructions->push_tail(inst); |
| 672 | } |
| 673 | |
| 674 | for (unsigned i = cols_to_init; i < type->matrix_columns; i++) { |
| 675 | ir_constant *const col_idx = new(ctx) ir_constant(i); |
| 676 | ir_rvalue *const col_ref = new(ctx) ir_dereference_array(var, col_idx); |
| 677 | |
| 678 | ir_rvalue *const rhs_ref = new(ctx) ir_dereference_variable(rhs_var); |
| 679 | ir_rvalue *const rhs = new(ctx) ir_swizzle(rhs_ref, 1, 1, 1, 1, |
| 680 | type->vector_elements); |
| 681 | |
| 682 | inst = new(ctx) ir_assignment(col_ref, rhs, NULL); |
| 683 | instructions->push_tail(inst); |
| 684 | } |
| 685 | } else if (first_param->type->is_matrix()) { |
| 686 | /* From page 50 (56 of the PDF) of the GLSL 1.50 spec: |
| 687 | * |
| 688 | * "If a matrix is constructed from a matrix, then each component |
| 689 | * (column i, row j) in the result that has a corresponding |
| 690 | * component (column i, row j) in the argument will be initialized |
| 691 | * from there. All other components will be initialized to the |
| 692 | * identity matrix. If a matrix argument is given to a matrix |
| 693 | * constructor, it is an error to have any other arguments." |
| 694 | */ |
| 695 | assert(first_param->next->is_tail_sentinal()); |
| 696 | ir_rvalue *const src_matrix = first_param; |
| 697 | |
| 698 | /* If the source matrix is smaller, pre-initialize the relavent parts of |
| 699 | * the destination matrix to the identity matrix. |
| 700 | */ |
| 701 | if ((src_matrix->type->matrix_columns < var->type->matrix_columns) |
| 702 | || (src_matrix->type->vector_elements < var->type->vector_elements)) { |
| 703 | |
| 704 | /* If the source matrix has fewer rows, every column of the destination |
| 705 | * must be initialized. Otherwise only the columns in the destination |
| 706 | * that do not exist in the source must be initialized. |
| 707 | */ |
| 708 | unsigned col = |
| 709 | (src_matrix->type->vector_elements < var->type->vector_elements) |
| 710 | ? 0 : src_matrix->type->matrix_columns; |
| 711 | |
| 712 | const glsl_type *const col_type = var->type->column_type(); |
| 713 | for (/* empty */; col < var->type->matrix_columns; col++) { |
| 714 | ir_constant_data ident; |
| 715 | |
| 716 | ident.f[0] = 0.0; |
| 717 | ident.f[1] = 0.0; |
| 718 | ident.f[2] = 0.0; |
| 719 | ident.f[3] = 0.0; |
| 720 | |
| 721 | ident.f[col] = 1.0; |
| 722 | |
| 723 | ir_rvalue *const rhs = new(ctx) ir_constant(col_type, &ident); |
| 724 | |
| 725 | ir_rvalue *const lhs = |
| 726 | new(ctx) ir_dereference_array(var, new(ctx) ir_constant(col)); |
| 727 | |
| 728 | ir_instruction *inst = new(ctx) ir_assignment(lhs, rhs, NULL); |
| 729 | instructions->push_tail(inst); |
| 730 | } |
| 731 | } |
| 732 | |
| 733 | /* Assign columns from the source matrix to the destination matrix. |
| 734 | * |
| 735 | * Since the parameter will be used in the RHS of multiple assignments, |
| 736 | * generate a temporary and copy the paramter there. |
| 737 | */ |
Ian Romanick | 4b6feb0 | 2010-06-28 13:22:55 -0700 | [diff] [blame] | 738 | ir_variable *const rhs_var = |
| 739 | new(ctx) ir_variable(first_param->type, |
| 740 | talloc_strdup(ctx, "mat_ctor_mat")); |
Ian Romanick | 81c7e94 | 2010-06-25 16:10:43 -0700 | [diff] [blame] | 741 | instructions->push_tail(rhs_var); |
| 742 | |
| 743 | ir_dereference *const rhs_var_ref = |
| 744 | new(ctx) ir_dereference_variable(rhs_var); |
| 745 | ir_instruction *const inst = |
| 746 | new(ctx) ir_assignment(rhs_var_ref, first_param, NULL); |
| 747 | instructions->push_tail(inst); |
| 748 | |
| 749 | |
| 750 | const unsigned swiz[4] = { 0, 1, 2, 3 }; |
| 751 | const unsigned last_col = min(src_matrix->type->matrix_columns, |
| 752 | var->type->matrix_columns); |
| 753 | for (unsigned i = 0; i < last_col; i++) { |
| 754 | ir_rvalue *const lhs_col = |
| 755 | new(ctx) ir_dereference_array(var, new(ctx) ir_constant(i)); |
| 756 | ir_rvalue *const rhs_col = |
| 757 | new(ctx) ir_dereference_array(rhs_var, new(ctx) ir_constant(i)); |
| 758 | |
| 759 | /* If one matrix has columns that are smaller than the columns of the |
| 760 | * other matrix, wrap the column access of the larger with a swizzle |
| 761 | * so that the LHS and RHS of the assignment have the same size (and |
| 762 | * therefore have the same type). |
| 763 | * |
| 764 | * It would be perfectly valid to unconditionally generate the |
| 765 | * swizzles, this this will typically result in a more compact IR tree. |
| 766 | */ |
| 767 | ir_rvalue *lhs; |
| 768 | ir_rvalue *rhs; |
| 769 | if (lhs_col->type->vector_elements < rhs_col->type->vector_elements) { |
| 770 | lhs = lhs_col; |
| 771 | |
| 772 | rhs = new(ctx) ir_swizzle(rhs_col, swiz, |
| 773 | lhs_col->type->vector_elements); |
| 774 | } else if (lhs_col->type->vector_elements |
| 775 | > rhs_col->type->vector_elements) { |
| 776 | lhs = new(ctx) ir_swizzle(lhs_col, swiz, |
| 777 | rhs_col->type->vector_elements); |
| 778 | rhs = rhs_col; |
| 779 | } else { |
| 780 | lhs = lhs_col; |
| 781 | rhs = rhs_col; |
| 782 | } |
| 783 | |
| 784 | assert(lhs->type == rhs->type); |
| 785 | |
| 786 | ir_instruction *inst = new(ctx) ir_assignment(lhs, rhs, NULL); |
| 787 | instructions->push_tail(inst); |
| 788 | } |
| 789 | } else { |
| 790 | const unsigned rows = type->matrix_columns; |
| 791 | const unsigned cols = type->vector_elements; |
| 792 | unsigned col_idx = 0; |
| 793 | unsigned row_idx = 0; |
| 794 | |
| 795 | foreach_list (node, parameters) { |
| 796 | ir_rvalue *const rhs = (ir_rvalue *) node; |
| 797 | const unsigned components_remaining_this_column = rows - row_idx; |
| 798 | unsigned rhs_components = rhs->type->components(); |
| 799 | unsigned rhs_base = 0; |
| 800 | |
| 801 | /* Since the parameter might be used in the RHS of two assignments, |
| 802 | * generate a temporary and copy the paramter there. |
| 803 | */ |
Ian Romanick | 4b6feb0 | 2010-06-28 13:22:55 -0700 | [diff] [blame] | 804 | ir_variable *rhs_var = |
| 805 | new(ctx) ir_variable(rhs->type, |
| 806 | talloc_strdup(ctx, "mat_ctor_vec")); |
Ian Romanick | 81c7e94 | 2010-06-25 16:10:43 -0700 | [diff] [blame] | 807 | instructions->push_tail(rhs_var); |
| 808 | |
| 809 | ir_dereference *rhs_var_ref = |
| 810 | new(ctx) ir_dereference_variable(rhs_var); |
| 811 | ir_instruction *inst = new(ctx) ir_assignment(rhs_var_ref, rhs, NULL); |
| 812 | instructions->push_tail(inst); |
| 813 | |
| 814 | /* Assign the current parameter to as many components of the matrix |
| 815 | * as it will fill. |
| 816 | * |
| 817 | * NOTE: A single vector parameter can span two matrix columns. A |
| 818 | * single vec4, for example, can completely fill a mat2. |
| 819 | */ |
| 820 | if (rhs_components >= components_remaining_this_column) { |
| 821 | const unsigned count = min(rhs_components, |
| 822 | components_remaining_this_column); |
| 823 | |
| 824 | rhs_var_ref = new(ctx) ir_dereference_variable(rhs_var); |
| 825 | |
| 826 | ir_instruction *inst = assign_to_matrix_column(var, col_idx, |
| 827 | row_idx, |
| 828 | rhs_var_ref, 0, |
| 829 | count, ctx); |
| 830 | instructions->push_tail(inst); |
| 831 | |
| 832 | rhs_base = count; |
| 833 | |
| 834 | col_idx++; |
| 835 | row_idx = 0; |
| 836 | } |
| 837 | |
| 838 | /* If there is data left in the parameter and components left to be |
| 839 | * set in the destination, emit another assignment. It is possible |
| 840 | * that the assignment could be of a vec4 to the last element of the |
| 841 | * matrix. In this case col_idx==cols, but there is still data |
| 842 | * left in the source parameter. Obviously, don't emit an assignment |
| 843 | * to data outside the destination matrix. |
| 844 | */ |
| 845 | if ((col_idx < cols) && (rhs_base < rhs_components)) { |
| 846 | const unsigned count = rhs_components - rhs_base; |
| 847 | |
| 848 | rhs_var_ref = new(ctx) ir_dereference_variable(rhs_var); |
| 849 | |
| 850 | ir_instruction *inst = assign_to_matrix_column(var, col_idx, |
| 851 | row_idx, |
| 852 | rhs_var_ref, |
| 853 | rhs_base, |
| 854 | count, ctx); |
| 855 | instructions->push_tail(inst); |
| 856 | |
| 857 | row_idx += count; |
| 858 | } |
| 859 | } |
| 860 | } |
| 861 | |
| 862 | return new(ctx) ir_dereference_variable(var); |
| 863 | } |
| 864 | |
| 865 | |
Kenneth Graunke | fb9fb5f | 2010-03-26 00:25:36 -0700 | [diff] [blame] | 866 | ir_rvalue * |
Ian Romanick | 548fa29 | 2010-03-15 13:04:13 -0700 | [diff] [blame] | 867 | ast_function_expression::hir(exec_list *instructions, |
| 868 | struct _mesa_glsl_parse_state *state) |
| 869 | { |
Kenneth Graunke | 953ff12 | 2010-06-25 13:14:37 -0700 | [diff] [blame] | 870 | void *ctx = state; |
Ian Romanick | 548fa29 | 2010-03-15 13:04:13 -0700 | [diff] [blame] | 871 | /* There are three sorts of function calls. |
| 872 | * |
Kenneth Graunke | f58bbd1 | 2010-07-08 18:03:28 -0700 | [diff] [blame] | 873 | * 1. constructors - The first subexpression is an ast_type_specifier. |
Ian Romanick | 548fa29 | 2010-03-15 13:04:13 -0700 | [diff] [blame] | 874 | * 2. methods - Only the .length() method of array types. |
| 875 | * 3. functions - Calls to regular old functions. |
| 876 | * |
Ian Romanick | 548fa29 | 2010-03-15 13:04:13 -0700 | [diff] [blame] | 877 | * Method calls are actually detected when the ast_field_selection |
| 878 | * expression is handled. |
| 879 | */ |
| 880 | if (is_constructor()) { |
Ian Romanick | abef955 | 2010-03-23 15:08:30 -0700 | [diff] [blame] | 881 | const ast_type_specifier *type = (ast_type_specifier *) subexpressions[0]; |
| 882 | YYLTYPE loc = type->get_location(); |
Ian Romanick | 3e0ef5f | 2010-03-31 16:22:56 -0700 | [diff] [blame] | 883 | const char *name; |
Ian Romanick | abef955 | 2010-03-23 15:08:30 -0700 | [diff] [blame] | 884 | |
Ian Romanick | 3e0ef5f | 2010-03-31 16:22:56 -0700 | [diff] [blame] | 885 | const glsl_type *const constructor_type = type->glsl_type(& name, state); |
Ian Romanick | abef955 | 2010-03-23 15:08:30 -0700 | [diff] [blame] | 886 | |
| 887 | |
| 888 | /* Constructors for samplers are illegal. |
| 889 | */ |
| 890 | if (constructor_type->is_sampler()) { |
| 891 | _mesa_glsl_error(& loc, state, "cannot construct sampler type `%s'", |
| 892 | constructor_type->name); |
Carl Worth | e01193a | 2010-06-23 18:25:04 -0700 | [diff] [blame] | 893 | return ir_call::get_error_instruction(ctx); |
Ian Romanick | abef955 | 2010-03-23 15:08:30 -0700 | [diff] [blame] | 894 | } |
| 895 | |
Ian Romanick | b6326ab | 2010-03-31 16:25:21 -0700 | [diff] [blame] | 896 | if (constructor_type->is_array()) { |
| 897 | if (state->language_version <= 110) { |
| 898 | _mesa_glsl_error(& loc, state, |
| 899 | "array constructors forbidden in GLSL 1.10"); |
Carl Worth | e01193a | 2010-06-23 18:25:04 -0700 | [diff] [blame] | 900 | return ir_call::get_error_instruction(ctx); |
Ian Romanick | b6326ab | 2010-03-31 16:25:21 -0700 | [diff] [blame] | 901 | } |
| 902 | |
Ian Romanick | 00aa173 | 2010-03-31 16:48:48 -0700 | [diff] [blame] | 903 | return process_array_constructor(instructions, constructor_type, |
Ian Romanick | 3521f0b | 2010-05-10 10:47:14 -0700 | [diff] [blame] | 904 | & loc, &this->expressions, state); |
Ian Romanick | b6326ab | 2010-03-31 16:25:21 -0700 | [diff] [blame] | 905 | } |
Ian Romanick | abef955 | 2010-03-23 15:08:30 -0700 | [diff] [blame] | 906 | |
| 907 | /* There are two kinds of constructor call. Constructors for built-in |
| 908 | * language types, such as mat4 and vec2, are free form. The only |
| 909 | * requirement is that the parameters must provide enough values of the |
| 910 | * correct scalar type. Constructors for arrays and structures must |
| 911 | * have the exact number of parameters with matching types in the |
| 912 | * correct order. These constructors follow essentially the same type |
| 913 | * matching rules as functions. |
| 914 | */ |
Kenneth Graunke | f58bbd1 | 2010-07-08 18:03:28 -0700 | [diff] [blame] | 915 | if (!constructor_type->is_numeric() && !constructor_type->is_boolean()) |
| 916 | return ir_call::get_error_instruction(ctx); |
Ian Romanick | 0b7dcc8 | 2010-03-26 17:38:58 -0700 | [diff] [blame] | 917 | |
Kenneth Graunke | f58bbd1 | 2010-07-08 18:03:28 -0700 | [diff] [blame] | 918 | /* Total number of components of the type being constructed. */ |
| 919 | const unsigned type_components = constructor_type->components(); |
Ian Romanick | 0b7dcc8 | 2010-03-26 17:38:58 -0700 | [diff] [blame] | 920 | |
Kenneth Graunke | f58bbd1 | 2010-07-08 18:03:28 -0700 | [diff] [blame] | 921 | /* Number of components from parameters that have actually been |
| 922 | * consumed. This is used to perform several kinds of error checking. |
| 923 | */ |
| 924 | unsigned components_used = 0; |
Ian Romanick | 0b7dcc8 | 2010-03-26 17:38:58 -0700 | [diff] [blame] | 925 | |
Kenneth Graunke | f58bbd1 | 2010-07-08 18:03:28 -0700 | [diff] [blame] | 926 | unsigned matrix_parameters = 0; |
| 927 | unsigned nonmatrix_parameters = 0; |
| 928 | exec_list actual_parameters; |
Ian Romanick | 9e08d01 | 2010-06-04 16:36:09 -0700 | [diff] [blame] | 929 | |
Kenneth Graunke | f58bbd1 | 2010-07-08 18:03:28 -0700 | [diff] [blame] | 930 | foreach_list (n, &this->expressions) { |
| 931 | ast_node *ast = exec_node_data(ast_node, n, link); |
| 932 | ir_rvalue *result = ast->hir(instructions, state)->as_rvalue(); |
Ian Romanick | 0b7dcc8 | 2010-03-26 17:38:58 -0700 | [diff] [blame] | 933 | |
| 934 | /* From page 50 (page 56 of the PDF) of the GLSL 1.50 spec: |
| 935 | * |
Kenneth Graunke | f58bbd1 | 2010-07-08 18:03:28 -0700 | [diff] [blame] | 936 | * "It is an error to provide extra arguments beyond this |
| 937 | * last used argument." |
Ian Romanick | 0b7dcc8 | 2010-03-26 17:38:58 -0700 | [diff] [blame] | 938 | */ |
Kenneth Graunke | f58bbd1 | 2010-07-08 18:03:28 -0700 | [diff] [blame] | 939 | if (components_used >= type_components) { |
| 940 | _mesa_glsl_error(& loc, state, "too many parameters to `%s' " |
| 941 | "constructor", |
Ian Romanick | 0b7dcc8 | 2010-03-26 17:38:58 -0700 | [diff] [blame] | 942 | constructor_type->name); |
Carl Worth | e01193a | 2010-06-23 18:25:04 -0700 | [diff] [blame] | 943 | return ir_call::get_error_instruction(ctx); |
Ian Romanick | 0b7dcc8 | 2010-03-26 17:38:58 -0700 | [diff] [blame] | 944 | } |
| 945 | |
Kenneth Graunke | f58bbd1 | 2010-07-08 18:03:28 -0700 | [diff] [blame] | 946 | if (!result->type->is_numeric() && !result->type->is_boolean()) { |
| 947 | _mesa_glsl_error(& loc, state, "cannot construct `%s' from a " |
| 948 | "non-numeric data type", |
Ian Romanick | 0b7dcc8 | 2010-03-26 17:38:58 -0700 | [diff] [blame] | 949 | constructor_type->name); |
Carl Worth | e01193a | 2010-06-23 18:25:04 -0700 | [diff] [blame] | 950 | return ir_call::get_error_instruction(ctx); |
Ian Romanick | 0b7dcc8 | 2010-03-26 17:38:58 -0700 | [diff] [blame] | 951 | } |
| 952 | |
Kenneth Graunke | f58bbd1 | 2010-07-08 18:03:28 -0700 | [diff] [blame] | 953 | /* Count the number of matrix and nonmatrix parameters. This |
| 954 | * is used below to enforce some of the constructor rules. |
Ian Romanick | 699b247 | 2010-06-25 17:36:17 -0700 | [diff] [blame] | 955 | */ |
Kenneth Graunke | f58bbd1 | 2010-07-08 18:03:28 -0700 | [diff] [blame] | 956 | if (result->type->is_matrix()) |
| 957 | matrix_parameters++; |
| 958 | else |
| 959 | nonmatrix_parameters++; |
Ian Romanick | 699b247 | 2010-06-25 17:36:17 -0700 | [diff] [blame] | 960 | |
Kenneth Graunke | f58bbd1 | 2010-07-08 18:03:28 -0700 | [diff] [blame] | 961 | actual_parameters.push_tail(result); |
| 962 | components_used += result->type->components(); |
Ian Romanick | 0b7dcc8 | 2010-03-26 17:38:58 -0700 | [diff] [blame] | 963 | } |
Ian Romanick | abef955 | 2010-03-23 15:08:30 -0700 | [diff] [blame] | 964 | |
Kenneth Graunke | f58bbd1 | 2010-07-08 18:03:28 -0700 | [diff] [blame] | 965 | /* From page 28 (page 34 of the PDF) of the GLSL 1.10 spec: |
| 966 | * |
| 967 | * "It is an error to construct matrices from other matrices. This |
| 968 | * is reserved for future use." |
| 969 | */ |
| 970 | if ((state->language_version <= 110) && (matrix_parameters > 0) |
| 971 | && constructor_type->is_matrix()) { |
| 972 | _mesa_glsl_error(& loc, state, "cannot construct `%s' from a " |
| 973 | "matrix in GLSL 1.10", |
| 974 | constructor_type->name); |
| 975 | return ir_call::get_error_instruction(ctx); |
| 976 | } |
| 977 | |
| 978 | /* From page 50 (page 56 of the PDF) of the GLSL 1.50 spec: |
| 979 | * |
| 980 | * "If a matrix argument is given to a matrix constructor, it is |
| 981 | * an error to have any other arguments." |
| 982 | */ |
| 983 | if ((matrix_parameters > 0) |
| 984 | && ((matrix_parameters + nonmatrix_parameters) > 1) |
| 985 | && constructor_type->is_matrix()) { |
| 986 | _mesa_glsl_error(& loc, state, "for matrix `%s' constructor, " |
| 987 | "matrix must be only parameter", |
| 988 | constructor_type->name); |
| 989 | return ir_call::get_error_instruction(ctx); |
| 990 | } |
| 991 | |
| 992 | /* From page 28 (page 34 of the PDF) of the GLSL 1.10 spec: |
| 993 | * |
| 994 | * "In these cases, there must be enough components provided in the |
| 995 | * arguments to provide an initializer for every component in the |
| 996 | * constructed value." |
| 997 | */ |
| 998 | if ((components_used < type_components) && (components_used != 1)) { |
| 999 | _mesa_glsl_error(& loc, state, "too few components to construct " |
| 1000 | "`%s'", |
| 1001 | constructor_type->name); |
| 1002 | return ir_call::get_error_instruction(ctx); |
| 1003 | } |
| 1004 | |
Kenneth Graunke | 284d821 | 2010-07-08 18:15:32 -0700 | [diff] [blame] | 1005 | /* Later, we cast each parameter to the same base type as the |
| 1006 | * constructor. Since there are no non-floating point matrices, we |
| 1007 | * need to break them up into a series of column vectors. |
| 1008 | */ |
| 1009 | if (constructor_type->base_type != GLSL_TYPE_FLOAT) { |
| 1010 | foreach_list_safe(n, &actual_parameters) { |
| 1011 | ir_rvalue *matrix = (ir_rvalue *) n; |
| 1012 | |
| 1013 | if (!matrix->type->is_matrix()) |
| 1014 | continue; |
| 1015 | |
| 1016 | /* Create a temporary containing the matrix. */ |
| 1017 | ir_variable *var = new(ctx) ir_variable(matrix->type, "matrix_tmp"); |
| 1018 | instructions->push_tail(var); |
| 1019 | instructions->push_tail(new(ctx) ir_assignment(new(ctx) |
| 1020 | ir_dereference_variable(var), matrix, NULL)); |
| 1021 | var->constant_value = matrix->constant_expression_value(); |
| 1022 | |
| 1023 | /* Replace the matrix with dereferences of its columns. */ |
| 1024 | for (int i = 0; i < matrix->type->matrix_columns; i++) { |
| 1025 | matrix->insert_before(new (ctx) ir_dereference_array(var, |
| 1026 | new(ctx) ir_constant(i))); |
| 1027 | } |
| 1028 | matrix->remove(); |
| 1029 | } |
| 1030 | } |
| 1031 | |
| 1032 | bool all_parameters_are_constant = true; |
| 1033 | |
| 1034 | /* Type cast each parameter and, if possible, fold constants.*/ |
| 1035 | foreach_list_safe(n, &actual_parameters) { |
| 1036 | ir_rvalue *ir = (ir_rvalue *) n; |
| 1037 | |
| 1038 | const glsl_type *desired_type = |
| 1039 | glsl_type::get_instance(constructor_type->base_type, |
| 1040 | ir->type->vector_elements, |
| 1041 | ir->type->matrix_columns); |
| 1042 | ir_rvalue *result = convert_component(ir, desired_type); |
| 1043 | |
| 1044 | /* Attempt to convert the parameter to a constant valued expression. |
| 1045 | * After doing so, track whether or not all the parameters to the |
| 1046 | * constructor are trivially constant valued expressions. |
| 1047 | */ |
| 1048 | ir_rvalue *const constant = result->constant_expression_value(); |
| 1049 | |
| 1050 | if (constant != NULL) |
| 1051 | result = constant; |
| 1052 | else |
| 1053 | all_parameters_are_constant = false; |
| 1054 | |
| 1055 | if (result != ir) { |
| 1056 | ir->insert_before(result); |
| 1057 | ir->remove(); |
| 1058 | } |
| 1059 | } |
Kenneth Graunke | f58bbd1 | 2010-07-08 18:03:28 -0700 | [diff] [blame] | 1060 | |
| 1061 | /* If all of the parameters are trivially constant, create a |
| 1062 | * constant representing the complete collection of parameters. |
| 1063 | */ |
| 1064 | if (all_parameters_are_constant) { |
| 1065 | if (components_used >= type_components) |
| 1066 | return new(ctx) ir_constant(constructor_type, |
| 1067 | & actual_parameters); |
| 1068 | |
| 1069 | /* The above case must handle all scalar constructors. |
| 1070 | */ |
| 1071 | assert(constructor_type->is_vector() |
| 1072 | || constructor_type->is_matrix()); |
| 1073 | |
| 1074 | /* Constructors with exactly one component are special for |
| 1075 | * vectors and matrices. For vectors it causes all elements of |
| 1076 | * the vector to be filled with the value. For matrices it |
| 1077 | * causes the matrix to be filled with 0 and the diagonal to be |
| 1078 | * filled with the value. |
| 1079 | */ |
| 1080 | ir_constant_data data; |
| 1081 | ir_constant *const initializer = |
| 1082 | (ir_constant *) actual_parameters.head; |
| 1083 | if (constructor_type->is_matrix()) |
| 1084 | generate_constructor_matrix(constructor_type, initializer, |
| 1085 | &data); |
| 1086 | else |
| 1087 | generate_constructor_vector(constructor_type, initializer, |
| 1088 | &data); |
| 1089 | |
| 1090 | return new(ctx) ir_constant(constructor_type, &data); |
| 1091 | } else if (constructor_type->is_scalar()) { |
| 1092 | return dereference_component((ir_rvalue *) actual_parameters.head, |
| 1093 | 0); |
| 1094 | } else if (constructor_type->is_vector()) { |
| 1095 | return emit_inline_vector_constructor(constructor_type, |
| 1096 | instructions, |
| 1097 | &actual_parameters, |
| 1098 | ctx); |
| 1099 | } else { |
| 1100 | assert(constructor_type->is_matrix()); |
| 1101 | return emit_inline_matrix_constructor(constructor_type, |
| 1102 | instructions, |
| 1103 | &actual_parameters, |
| 1104 | ctx); |
| 1105 | } |
Ian Romanick | 548fa29 | 2010-03-15 13:04:13 -0700 | [diff] [blame] | 1106 | } else { |
| 1107 | const ast_expression *id = subexpressions[0]; |
Ian Romanick | f474961 | 2010-03-15 13:26:02 -0700 | [diff] [blame] | 1108 | YYLTYPE loc = id->get_location(); |
Ian Romanick | c077131 | 2010-06-09 17:23:26 -0700 | [diff] [blame] | 1109 | exec_list actual_parameters; |
| 1110 | |
| 1111 | process_parameters(instructions, &actual_parameters, &this->expressions, |
| 1112 | state); |
Ian Romanick | 548fa29 | 2010-03-15 13:04:13 -0700 | [diff] [blame] | 1113 | |
Ian Romanick | ab92d0e | 2010-06-09 17:26:20 -0700 | [diff] [blame] | 1114 | const glsl_type *const type = |
| 1115 | state->symbols->get_type(id->primary_expression.identifier); |
| 1116 | |
| 1117 | if ((type != NULL) && type->is_record()) { |
| 1118 | ir_constant *constant = |
| 1119 | constant_record_constructor(type, &loc, &actual_parameters, state); |
| 1120 | |
| 1121 | if (constant != NULL) |
| 1122 | return constant; |
| 1123 | } |
| 1124 | |
Ian Romanick | f474961 | 2010-03-15 13:26:02 -0700 | [diff] [blame] | 1125 | return match_function_by_name(instructions, |
| 1126 | id->primary_expression.identifier, & loc, |
Ian Romanick | c077131 | 2010-06-09 17:23:26 -0700 | [diff] [blame] | 1127 | &actual_parameters, state); |
Ian Romanick | 548fa29 | 2010-03-15 13:04:13 -0700 | [diff] [blame] | 1128 | } |
| 1129 | |
Carl Worth | e01193a | 2010-06-23 18:25:04 -0700 | [diff] [blame] | 1130 | return ir_call::get_error_instruction(ctx); |
Ian Romanick | 548fa29 | 2010-03-15 13:04:13 -0700 | [diff] [blame] | 1131 | } |