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