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