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