blob: 73af882c530811dcec5b0393f4eb96118e3c9d4e [file] [log] [blame]
Ian Romanick548fa292010-03-15 13:04:13 -07001/*
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 Romanick8bde4ce2010-03-19 11:57:24 -070024#include "glsl_symbol_table.h"
Ian Romanick548fa292010-03-15 13:04:13 -070025#include "ast.h"
26#include "glsl_types.h"
27#include "ir.h"
28
Ian Romanick81c7e942010-06-25 16:10:43 -070029inline unsigned min(unsigned a, unsigned b)
30{
31 return (a < b) ? a : b;
32}
33
Kenneth Graunke17a307d2010-07-14 13:22:07 -070034static ir_rvalue *
35convert_component(ir_rvalue *src, const glsl_type *desired_type);
36
Ian Romanick68515ee2010-03-31 16:28:51 -070037static unsigned
38process_parameters(exec_list *instructions, exec_list *actual_parameters,
Ian Romanick304ea902010-05-10 11:17:53 -070039 exec_list *parameters,
Ian Romanick68515ee2010-03-31 16:28:51 -070040 struct _mesa_glsl_parse_state *state)
41{
Ian Romanick68515ee2010-03-31 16:28:51 -070042 unsigned count = 0;
43
Ian Romanick304ea902010-05-10 11:17:53 -070044 foreach_list (n, parameters) {
45 ast_node *const ast = exec_node_data(ast_node, n, link);
Ian Romanick1a872b12010-06-09 17:31:02 -070046 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 Romanick68515ee2010-03-31 16:28:51 -070051
Ian Romanick3521f0b2010-05-10 10:47:14 -070052 actual_parameters->push_tail(result);
53 count++;
Ian Romanick68515ee2010-03-31 16:28:51 -070054 }
55
56 return count;
57}
58
59
60static ir_rvalue *
61process_call(exec_list *instructions, ir_function *f,
62 YYLTYPE *loc, exec_list *actual_parameters,
63 struct _mesa_glsl_parse_state *state)
64{
Kenneth Graunke953ff122010-06-25 13:14:37 -070065 void *ctx = state;
Carl Worth1660a292010-06-23 18:11:51 -070066
Eric Anholt1f472452010-07-18 17:45:16 -070067 ir_function_signature *sig = f->matching_signature(actual_parameters);
Ian Romanick68515ee2010-03-31 16:28:51 -070068
69 /* The instructions param will be used when the FINISHMEs below are done */
70 (void) instructions;
71
72 if (sig != NULL) {
Ian Romanickc35bb002010-04-02 15:51:02 -070073 /* 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 Anholtf1ddca92010-04-07 12:35:34 -070081 ir_rvalue *actual = (ir_rvalue *) actual_iter.get();
82 ir_variable *formal = (ir_variable *) formal_iter.get();
Ian Romanickc35bb002010-04-02 15:51:02 -070083
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 Graunke17a307d2010-07-14 13:22:07 -070098 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 Romanickc35bb002010-04-02 15:51:02 -0700103 actual_iter.next();
104 formal_iter.next();
105 }
106
Eric Anholt02d37112010-07-20 15:50:48 -0700107 /* Always insert the call in the instruction stream, and return a deref
108 * of its return val if it returns a value, since we don't know if
109 * the rvalue is going to be assigned to anything or not.
110 */
111 ir_call *call = new(ctx) ir_call(sig, actual_parameters);
112 if (!sig->return_type->is_void()) {
113 ir_variable *var;
114 ir_dereference_variable *deref;
115
116 var = new(ctx) ir_variable(sig->return_type,
117 talloc_asprintf(ctx, "%s_retval",
Ian Romanick7e2aa912010-07-19 17:12:42 -0700118 sig->function_name()),
119 ir_var_temporary);
Eric Anholt02d37112010-07-20 15:50:48 -0700120 instructions->push_tail(var);
121
122 deref = new(ctx) ir_dereference_variable(var);
123 ir_assignment *assign = new(ctx) ir_assignment(deref, call, NULL);
124 instructions->push_tail(assign);
125
126 deref = new(ctx) ir_dereference_variable(var);
127 return deref;
128 } else {
129 instructions->push_tail(call);
130 return NULL;
131 }
Ian Romanick68515ee2010-03-31 16:28:51 -0700132 } else {
133 /* FINISHME: Log a better error message here. G++ will show the types
134 * FINISHME: of the actual parameters and the set of candidate
135 * FINISHME: functions. A different error should also be logged when
136 * FINISHME: multiple functions match.
137 */
138 _mesa_glsl_error(loc, state, "no matching function for call to `%s'",
139 f->name);
Carl Worthe01193a2010-06-23 18:25:04 -0700140 return ir_call::get_error_instruction(ctx);
Ian Romanick68515ee2010-03-31 16:28:51 -0700141 }
142}
143
144
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -0700145static ir_rvalue *
Ian Romanickf4749612010-03-15 13:26:02 -0700146match_function_by_name(exec_list *instructions, const char *name,
Ian Romanickc0771312010-06-09 17:23:26 -0700147 YYLTYPE *loc, exec_list *actual_parameters,
Ian Romanickf4749612010-03-15 13:26:02 -0700148 struct _mesa_glsl_parse_state *state)
149{
Kenneth Graunke953ff122010-06-25 13:14:37 -0700150 void *ctx = state;
Ian Romanick8bde4ce2010-03-19 11:57:24 -0700151 ir_function *f = state->symbols->get_function(name);
Ian Romanickf4749612010-03-15 13:26:02 -0700152
153 if (f == NULL) {
154 _mesa_glsl_error(loc, state, "function `%s' undeclared", name);
Carl Worthe01193a2010-06-23 18:25:04 -0700155 return ir_call::get_error_instruction(ctx);
Ian Romanickf4749612010-03-15 13:26:02 -0700156 }
157
Ian Romanickc0771312010-06-09 17:23:26 -0700158 /* Once we've determined that the function being called might exist, try
159 * to find an overload of the function that matches the parameters.
Ian Romanickf4749612010-03-15 13:26:02 -0700160 */
Ian Romanickc0771312010-06-09 17:23:26 -0700161 return process_call(instructions, f, loc, actual_parameters, state);
Ian Romanickf4749612010-03-15 13:26:02 -0700162}
163
164
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700165/**
166 * Perform automatic type conversion of constructor parameters
167 */
168static ir_rvalue *
169convert_component(ir_rvalue *src, const glsl_type *desired_type)
170{
Carl Worth1660a292010-06-23 18:11:51 -0700171 void *ctx = talloc_parent(src);
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700172 const unsigned a = desired_type->base_type;
173 const unsigned b = src->type->base_type;
Ian Romanick00eb4662010-06-07 15:08:04 -0700174 ir_expression *result = NULL;
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700175
176 if (src->type->is_error())
177 return src;
178
179 assert(a <= GLSL_TYPE_BOOL);
180 assert(b <= GLSL_TYPE_BOOL);
181
182 if ((a == b) || (src->type->is_integer() && desired_type->is_integer()))
183 return src;
184
185 switch (a) {
186 case GLSL_TYPE_UINT:
187 case GLSL_TYPE_INT:
188 if (b == GLSL_TYPE_FLOAT)
Carl Worth1660a292010-06-23 18:11:51 -0700189 result = new(ctx) ir_expression(ir_unop_f2i, desired_type, src, NULL);
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700190 else {
191 assert(b == GLSL_TYPE_BOOL);
Carl Worth1660a292010-06-23 18:11:51 -0700192 result = new(ctx) ir_expression(ir_unop_b2i, desired_type, src, NULL);
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700193 }
Ian Romanick565185c2010-06-11 13:49:00 -0700194 break;
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700195 case GLSL_TYPE_FLOAT:
196 switch (b) {
197 case GLSL_TYPE_UINT:
Carl Worth1660a292010-06-23 18:11:51 -0700198 result = new(ctx) ir_expression(ir_unop_u2f, desired_type, src, NULL);
Ian Romanick00eb4662010-06-07 15:08:04 -0700199 break;
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700200 case GLSL_TYPE_INT:
Carl Worth1660a292010-06-23 18:11:51 -0700201 result = new(ctx) ir_expression(ir_unop_i2f, desired_type, src, NULL);
Ian Romanick00eb4662010-06-07 15:08:04 -0700202 break;
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700203 case GLSL_TYPE_BOOL:
Carl Worth1660a292010-06-23 18:11:51 -0700204 result = new(ctx) ir_expression(ir_unop_b2f, desired_type, src, NULL);
Ian Romanick00eb4662010-06-07 15:08:04 -0700205 break;
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700206 }
207 break;
Ian Romanick26b5d332010-06-25 16:19:45 -0700208 case GLSL_TYPE_BOOL:
Ian Romanickb74b43e2010-06-11 16:52:09 -0700209 switch (b) {
Ian Romanick26b5d332010-06-25 16:19:45 -0700210 case GLSL_TYPE_UINT:
211 case GLSL_TYPE_INT:
212 result = new(ctx) ir_expression(ir_unop_i2b, desired_type, src, NULL);
213 break;
214 case GLSL_TYPE_FLOAT:
215 result = new(ctx) ir_expression(ir_unop_f2b, desired_type, src, NULL);
216 break;
Ian Romanickb74b43e2010-06-11 16:52:09 -0700217 }
Ian Romanick26b5d332010-06-25 16:19:45 -0700218 break;
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700219 }
220
Ian Romanick00eb4662010-06-07 15:08:04 -0700221 assert(result != NULL);
222
223 ir_constant *const constant = result->constant_expression_value();
224 return (constant != NULL) ? (ir_rvalue *) constant : (ir_rvalue *) result;
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700225}
226
227
228/**
229 * Dereference a specific component from a scalar, vector, or matrix
230 */
231static ir_rvalue *
232dereference_component(ir_rvalue *src, unsigned component)
233{
Carl Worth1660a292010-06-23 18:11:51 -0700234 void *ctx = talloc_parent(src);
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700235 assert(component < src->type->components());
236
Ian Romanickc9cb1032010-06-04 16:20:35 -0700237 /* If the source is a constant, just create a new constant instead of a
238 * dereference of the existing constant.
239 */
240 ir_constant *constant = src->as_constant();
241 if (constant)
Carl Worth1660a292010-06-23 18:11:51 -0700242 return new(ctx) ir_constant(constant, component);
Ian Romanickc9cb1032010-06-04 16:20:35 -0700243
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700244 if (src->type->is_scalar()) {
245 return src;
246 } else if (src->type->is_vector()) {
Carl Worth1660a292010-06-23 18:11:51 -0700247 return new(ctx) ir_swizzle(src, component, 0, 0, 0, 1);
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700248 } else {
249 assert(src->type->is_matrix());
250
251 /* Dereference a row of the matrix, then call this function again to get
252 * a specific element from that row.
253 */
254 const int c = component / src->type->column_type()->vector_elements;
255 const int r = component % src->type->column_type()->vector_elements;
Carl Worth1660a292010-06-23 18:11:51 -0700256 ir_constant *const col_index = new(ctx) ir_constant(c);
257 ir_dereference *const col = new(ctx) ir_dereference_array(src, col_index);
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700258
259 col->type = src->type->column_type();
260
261 return dereference_component(col, r);
262 }
263
264 assert(!"Should not get here.");
265 return NULL;
266}
267
268
Ian Romanick00aa1732010-03-31 16:48:48 -0700269static ir_rvalue *
270process_array_constructor(exec_list *instructions,
271 const glsl_type *constructor_type,
Ian Romanick304ea902010-05-10 11:17:53 -0700272 YYLTYPE *loc, exec_list *parameters,
Ian Romanick00aa1732010-03-31 16:48:48 -0700273 struct _mesa_glsl_parse_state *state)
274{
Kenneth Graunke953ff122010-06-25 13:14:37 -0700275 void *ctx = state;
Ian Romanick00aa1732010-03-31 16:48:48 -0700276 /* Array constructors come in two forms: sized and unsized. Sized array
277 * constructors look like 'vec4[2](a, b)', where 'a' and 'b' are vec4
278 * variables. In this case the number of parameters must exactly match the
279 * specified size of the array.
280 *
281 * Unsized array constructors look like 'vec4[](a, b)', where 'a' and 'b'
282 * are vec4 variables. In this case the size of the array being constructed
283 * is determined by the number of parameters.
284 *
285 * From page 52 (page 58 of the PDF) of the GLSL 1.50 spec:
286 *
287 * "There must be exactly the same number of arguments as the size of
288 * the array being constructed. If no size is present in the
289 * constructor, then the array is explicitly sized to the number of
290 * arguments provided. The arguments are assigned in order, starting at
291 * element 0, to the elements of the constructed array. Each argument
292 * must be the same type as the element type of the array, or be a type
293 * that can be converted to the element type of the array according to
294 * Section 4.1.10 "Implicit Conversions.""
295 */
296 exec_list actual_parameters;
297 const unsigned parameter_count =
298 process_parameters(instructions, &actual_parameters, parameters, state);
299
300 if ((parameter_count == 0)
301 || ((constructor_type->length != 0)
302 && (constructor_type->length != parameter_count))) {
303 const unsigned min_param = (constructor_type->length == 0)
304 ? 1 : constructor_type->length;
305
306 _mesa_glsl_error(loc, state, "array constructor must have %s %u "
307 "parameter%s",
308 (constructor_type->length != 0) ? "at least" : "exactly",
309 min_param, (min_param <= 1) ? "" : "s");
Carl Worthe01193a2010-06-23 18:25:04 -0700310 return ir_call::get_error_instruction(ctx);
Ian Romanick00aa1732010-03-31 16:48:48 -0700311 }
312
313 if (constructor_type->length == 0) {
314 constructor_type =
Ian Romanickf38d15b2010-07-20 15:33:40 -0700315 glsl_type::get_array_instance(constructor_type->element_type(),
Ian Romanick00aa1732010-03-31 16:48:48 -0700316 parameter_count);
317 assert(constructor_type != NULL);
318 assert(constructor_type->length == parameter_count);
319 }
320
321 ir_function *f = state->symbols->get_function(constructor_type->name);
322
323 /* If the constructor for this type of array does not exist, generate the
Ian Romanick82baaf42010-04-23 13:21:22 -0700324 * prototype and add it to the symbol table.
Ian Romanick00aa1732010-03-31 16:48:48 -0700325 */
326 if (f == NULL) {
Ian Romanick82baaf42010-04-23 13:21:22 -0700327 f = constructor_type->generate_constructor(state->symbols);
Ian Romanick00aa1732010-03-31 16:48:48 -0700328 }
329
330 ir_rvalue *const r =
331 process_call(instructions, f, loc, &actual_parameters, state);
332
333 assert(r != NULL);
334 assert(r->type->is_error() || (r->type == constructor_type));
335
336 return r;
337}
338
339
Ian Romanickab92d0e2010-06-09 17:26:20 -0700340/**
341 * Try to convert a record constructor to a constant expression
342 */
343static ir_constant *
344constant_record_constructor(const glsl_type *constructor_type,
345 YYLTYPE *loc, exec_list *parameters,
346 struct _mesa_glsl_parse_state *state)
347{
Kenneth Graunke953ff122010-06-25 13:14:37 -0700348 void *ctx = state;
Ian Romanickab92d0e2010-06-09 17:26:20 -0700349 bool all_parameters_are_constant = true;
350
351 exec_node *node = parameters->head;
352 for (unsigned i = 0; i < constructor_type->length; i++) {
353 ir_instruction *ir = (ir_instruction *) node;
354
355 if (node->is_tail_sentinal()) {
356 _mesa_glsl_error(loc, state,
357 "insufficient parameters to constructor for `%s'",
358 constructor_type->name);
359 return NULL;
360 }
361
362 if (ir->type != constructor_type->fields.structure[i].type) {
363 _mesa_glsl_error(loc, state,
364 "parameter type mismatch in constructor for `%s' "
365 " (%s vs %s)",
366 constructor_type->name,
367 ir->type->name,
368 constructor_type->fields.structure[i].type->name);
369 return NULL;
370 }
371
372 if (ir->as_constant() == NULL)
373 all_parameters_are_constant = false;
374
375 node = node->next;
376 }
377
378 if (!all_parameters_are_constant)
379 return NULL;
380
Carl Worth1660a292010-06-23 18:11:51 -0700381 return new(ctx) ir_constant(constructor_type, parameters);
Ian Romanickab92d0e2010-06-09 17:26:20 -0700382}
383
384
Ian Romanickbe1d2bf2010-06-11 14:01:44 -0700385/**
386 * Generate data for a constant matrix constructor w/a single scalar parameter
387 *
388 * Matrix constructors in GLSL can be passed a single scalar of the
389 * approriate type. In these cases, the resulting matrix is the identity
390 * matrix multipled by the specified scalar. This function generates data for
391 * that matrix.
392 *
393 * \param type Type of the desired matrix.
394 * \param initializer Scalar value used to initialize the matrix diagonal.
395 * \param data Location to store the resulting matrix.
396 */
397void
398generate_constructor_matrix(const glsl_type *type, ir_constant *initializer,
399 ir_constant_data *data)
400{
401 switch (type->base_type) {
402 case GLSL_TYPE_UINT:
403 case GLSL_TYPE_INT:
404 for (unsigned i = 0; i < type->components(); i++)
405 data->u[i] = 0;
406
407 for (unsigned i = 0; i < type->matrix_columns; i++) {
408 /* The array offset of the ith row and column of the matrix.
409 */
410 const unsigned idx = (i * type->vector_elements) + i;
411
412 data->u[idx] = initializer->value.u[0];
413 }
414 break;
415
416 case GLSL_TYPE_FLOAT:
417 for (unsigned i = 0; i < type->components(); i++)
418 data->f[i] = 0;
419
420 for (unsigned i = 0; i < type->matrix_columns; i++) {
421 /* The array offset of the ith row and column of the matrix.
422 */
423 const unsigned idx = (i * type->vector_elements) + i;
424
425 data->f[idx] = initializer->value.f[0];
426 }
427
428 break;
429
430 default:
431 assert(!"Should not get here.");
432 break;
433 }
434}
435
436
437/**
438 * Generate data for a constant vector constructor w/a single scalar parameter
439 *
440 * Vector constructors in GLSL can be passed a single scalar of the
441 * approriate type. In these cases, the resulting vector contains the specified
442 * value in all components. This function generates data for that vector.
443 *
444 * \param type Type of the desired vector.
445 * \param initializer Scalar value used to initialize the vector.
446 * \param data Location to store the resulting vector data.
447 */
448void
449generate_constructor_vector(const glsl_type *type, ir_constant *initializer,
450 ir_constant_data *data)
451{
452 switch (type->base_type) {
453 case GLSL_TYPE_UINT:
454 case GLSL_TYPE_INT:
455 for (unsigned i = 0; i < type->components(); i++)
456 data->u[i] = initializer->value.u[0];
457
458 break;
459
460 case GLSL_TYPE_FLOAT:
461 for (unsigned i = 0; i < type->components(); i++)
462 data->f[i] = initializer->value.f[0];
463
464 break;
465
466 case GLSL_TYPE_BOOL:
467 for (unsigned i = 0; i < type->components(); i++)
468 data->b[i] = initializer->value.b[0];
469
470 break;
471
472 default:
473 assert(!"Should not get here.");
474 break;
475 }
476}
477
478
Ian Romanickc31dcdf2010-06-23 15:19:40 -0700479/**
480 * Determine if a list consists of a single scalar r-value
481 */
482bool
483single_scalar_parameter(exec_list *parameters)
484{
485 const ir_rvalue *const p = (ir_rvalue *) parameters->head;
486 assert(((ir_rvalue *)p)->as_rvalue() != NULL);
487
488 return (p->type->is_scalar() && p->next->is_tail_sentinal());
489}
490
491
492/**
493 * Generate inline code for a vector constructor
494 *
495 * The generated constructor code will consist of a temporary variable
496 * declaration of the same type as the constructor. A sequence of assignments
497 * from constructor parameters to the temporary will follow.
498 *
499 * \return
500 * An \c ir_dereference_variable of the temprorary generated in the constructor
501 * body.
502 */
503ir_rvalue *
504emit_inline_vector_constructor(const glsl_type *type,
505 exec_list *instructions,
506 exec_list *parameters,
507 void *ctx)
508{
509 assert(!parameters->is_empty());
510
Ian Romanick4b6feb02010-06-28 13:22:55 -0700511 ir_variable *var = new(ctx) ir_variable(type,
Ian Romanick7e2aa912010-07-19 17:12:42 -0700512 talloc_strdup(ctx, "vec_ctor"),
513 ir_var_temporary);
Ian Romanickc31dcdf2010-06-23 15:19:40 -0700514 instructions->push_tail(var);
515
516 /* There are two kinds of vector constructors.
517 *
518 * - Construct a vector from a single scalar by replicating that scalar to
519 * all components of the vector.
520 *
521 * - Construct a vector from an arbirary combination of vectors and
522 * scalars. The components of the constructor parameters are assigned
523 * to the vector in order until the vector is full.
524 */
525 const unsigned lhs_components = type->components();
526 if (single_scalar_parameter(parameters)) {
527 ir_rvalue *first_param = (ir_rvalue *)parameters->head;
528 ir_rvalue *rhs = new(ctx) ir_swizzle(first_param, 0, 0, 0, 0,
529 lhs_components);
530 ir_dereference_variable *lhs = new(ctx) ir_dereference_variable(var);
531
532 assert(rhs->type == lhs->type);
533
534 ir_instruction *inst = new(ctx) ir_assignment(lhs, rhs, NULL);
535 instructions->push_tail(inst);
536 } else {
537 unsigned base_component = 0;
538 foreach_list(node, parameters) {
539 ir_rvalue *rhs = (ir_rvalue *) node;
540 unsigned rhs_components = rhs->type->components();
541
542 /* Do not try to assign more components to the vector than it has!
543 */
544 if ((rhs_components + base_component) > lhs_components) {
545 rhs_components = lhs_components - base_component;
546 }
547
548 /* Emit an assignment of the constructor parameter to the next set of
549 * components in the temporary variable.
550 */
551 unsigned mask[4] = { 0, 0, 0, 0 };
552 for (unsigned i = 0; i < rhs_components; i++) {
553 mask[i] = i + base_component;
554 }
555
556
557 ir_rvalue *lhs_ref = new(ctx) ir_dereference_variable(var);
558 ir_swizzle *lhs = new(ctx) ir_swizzle(lhs_ref, mask, rhs_components);
559
560 ir_instruction *inst = new(ctx) ir_assignment(lhs, rhs, NULL);
561 instructions->push_tail(inst);
562
563 /* Advance the component index by the number of components that were
564 * just assigned.
565 */
566 base_component += rhs_components;
567 }
568 }
569 return new(ctx) ir_dereference_variable(var);
570}
571
572
Ian Romanick81c7e942010-06-25 16:10:43 -0700573/**
574 * Generate assignment of a portion of a vector to a portion of a matrix column
575 *
576 * \param src_base First component of the source to be used in assignment
577 * \param column Column of destination to be assiged
578 * \param row_base First component of the destination column to be assigned
579 * \param count Number of components to be assigned
580 *
581 * \note
582 * \c src_base + \c count must be less than or equal to the number of components
583 * in the source vector.
584 */
585ir_instruction *
586assign_to_matrix_column(ir_variable *var, unsigned column, unsigned row_base,
587 ir_rvalue *src, unsigned src_base, unsigned count,
588 TALLOC_CTX *ctx)
589{
590 const unsigned mask[8] = { 0, 1, 2, 3, 0, 0, 0, 0 };
591
592 ir_constant *col_idx = new(ctx) ir_constant(column);
593 ir_rvalue *column_ref = new(ctx) ir_dereference_array(var, col_idx);
594
595 assert(column_ref->type->components() >= (row_base + count));
596 ir_rvalue *lhs = new(ctx) ir_swizzle(column_ref, &mask[row_base], count);
597
598 assert(src->type->components() >= (src_base + count));
599 ir_rvalue *rhs = new(ctx) ir_swizzle(src, &mask[src_base], count);
600
601 return new(ctx) ir_assignment(lhs, rhs, NULL);
602}
603
604
605/**
606 * Generate inline code for a matrix constructor
607 *
608 * The generated constructor code will consist of a temporary variable
609 * declaration of the same type as the constructor. A sequence of assignments
610 * from constructor parameters to the temporary will follow.
611 *
612 * \return
613 * An \c ir_dereference_variable of the temprorary generated in the constructor
614 * body.
615 */
616ir_rvalue *
617emit_inline_matrix_constructor(const glsl_type *type,
618 exec_list *instructions,
619 exec_list *parameters,
620 void *ctx)
621{
622 assert(!parameters->is_empty());
623
Ian Romanick4b6feb02010-06-28 13:22:55 -0700624 ir_variable *var = new(ctx) ir_variable(type,
Ian Romanick7e2aa912010-07-19 17:12:42 -0700625 talloc_strdup(ctx, "mat_ctor"),
626 ir_var_temporary);
Ian Romanick81c7e942010-06-25 16:10:43 -0700627 instructions->push_tail(var);
628
629 /* There are three kinds of matrix constructors.
630 *
631 * - Construct a matrix from a single scalar by replicating that scalar to
632 * along the diagonal of the matrix and setting all other components to
633 * zero.
634 *
635 * - Construct a matrix from an arbirary combination of vectors and
636 * scalars. The components of the constructor parameters are assigned
637 * to the matrix in colum-major order until the matrix is full.
638 *
639 * - Construct a matrix from a single matrix. The source matrix is copied
640 * to the upper left portion of the constructed matrix, and the remaining
641 * elements take values from the identity matrix.
642 */
643 ir_rvalue *const first_param = (ir_rvalue *) parameters->head;
644 if (single_scalar_parameter(parameters)) {
645 /* Assign the scalar to the X component of a vec4, and fill the remaining
646 * components with zero.
647 */
Ian Romanick4b6feb02010-06-28 13:22:55 -0700648 ir_variable *rhs_var =
649 new(ctx) ir_variable(glsl_type::vec4_type,
Ian Romanick7e2aa912010-07-19 17:12:42 -0700650 talloc_strdup(ctx, "mat_ctor_vec"),
651 ir_var_temporary);
Ian Romanick81c7e942010-06-25 16:10:43 -0700652 instructions->push_tail(rhs_var);
653
654 ir_constant_data zero;
655 zero.f[0] = 0.0;
656 zero.f[1] = 0.0;
657 zero.f[2] = 0.0;
658 zero.f[3] = 0.0;
659
660 ir_instruction *inst =
661 new(ctx) ir_assignment(new(ctx) ir_dereference_variable(rhs_var),
662 new(ctx) ir_constant(rhs_var->type, &zero),
663 NULL);
664 instructions->push_tail(inst);
665
666 ir_rvalue *const rhs_ref = new(ctx) ir_dereference_variable(rhs_var);
667 ir_rvalue *const x_of_rhs = new(ctx) ir_swizzle(rhs_ref, 0, 0, 0, 0, 1);
668
669 inst = new(ctx) ir_assignment(x_of_rhs, first_param, NULL);
670 instructions->push_tail(inst);
671
672 /* Assign the temporary vector to each column of the destination matrix
673 * with a swizzle that puts the X component on the diagonal of the
674 * matrix. In some cases this may mean that the X component does not
675 * get assigned into the column at all (i.e., when the matrix has more
676 * columns than rows).
677 */
678 static const unsigned rhs_swiz[4][4] = {
679 { 0, 1, 1, 1 },
680 { 1, 0, 1, 1 },
681 { 1, 1, 0, 1 },
682 { 1, 1, 1, 0 }
683 };
684
685 const unsigned cols_to_init = min(type->matrix_columns,
686 type->vector_elements);
687 for (unsigned i = 0; i < cols_to_init; i++) {
688 ir_constant *const col_idx = new(ctx) ir_constant(i);
689 ir_rvalue *const col_ref = new(ctx) ir_dereference_array(var, col_idx);
690
691 ir_rvalue *const rhs_ref = new(ctx) ir_dereference_variable(rhs_var);
692 ir_rvalue *const rhs = new(ctx) ir_swizzle(rhs_ref, rhs_swiz[i],
693 type->vector_elements);
694
695 inst = new(ctx) ir_assignment(col_ref, rhs, NULL);
696 instructions->push_tail(inst);
697 }
698
699 for (unsigned i = cols_to_init; i < type->matrix_columns; i++) {
700 ir_constant *const col_idx = new(ctx) ir_constant(i);
701 ir_rvalue *const col_ref = new(ctx) ir_dereference_array(var, col_idx);
702
703 ir_rvalue *const rhs_ref = new(ctx) ir_dereference_variable(rhs_var);
704 ir_rvalue *const rhs = new(ctx) ir_swizzle(rhs_ref, 1, 1, 1, 1,
705 type->vector_elements);
706
707 inst = new(ctx) ir_assignment(col_ref, rhs, NULL);
708 instructions->push_tail(inst);
709 }
710 } else if (first_param->type->is_matrix()) {
711 /* From page 50 (56 of the PDF) of the GLSL 1.50 spec:
712 *
713 * "If a matrix is constructed from a matrix, then each component
714 * (column i, row j) in the result that has a corresponding
715 * component (column i, row j) in the argument will be initialized
716 * from there. All other components will be initialized to the
717 * identity matrix. If a matrix argument is given to a matrix
718 * constructor, it is an error to have any other arguments."
719 */
720 assert(first_param->next->is_tail_sentinal());
721 ir_rvalue *const src_matrix = first_param;
722
723 /* If the source matrix is smaller, pre-initialize the relavent parts of
724 * the destination matrix to the identity matrix.
725 */
726 if ((src_matrix->type->matrix_columns < var->type->matrix_columns)
727 || (src_matrix->type->vector_elements < var->type->vector_elements)) {
728
729 /* If the source matrix has fewer rows, every column of the destination
730 * must be initialized. Otherwise only the columns in the destination
731 * that do not exist in the source must be initialized.
732 */
733 unsigned col =
734 (src_matrix->type->vector_elements < var->type->vector_elements)
735 ? 0 : src_matrix->type->matrix_columns;
736
737 const glsl_type *const col_type = var->type->column_type();
738 for (/* empty */; col < var->type->matrix_columns; col++) {
739 ir_constant_data ident;
740
741 ident.f[0] = 0.0;
742 ident.f[1] = 0.0;
743 ident.f[2] = 0.0;
744 ident.f[3] = 0.0;
745
746 ident.f[col] = 1.0;
747
748 ir_rvalue *const rhs = new(ctx) ir_constant(col_type, &ident);
749
750 ir_rvalue *const lhs =
751 new(ctx) ir_dereference_array(var, new(ctx) ir_constant(col));
752
753 ir_instruction *inst = new(ctx) ir_assignment(lhs, rhs, NULL);
754 instructions->push_tail(inst);
755 }
756 }
757
758 /* Assign columns from the source matrix to the destination matrix.
759 *
760 * Since the parameter will be used in the RHS of multiple assignments,
761 * generate a temporary and copy the paramter there.
762 */
Ian Romanick4b6feb02010-06-28 13:22:55 -0700763 ir_variable *const rhs_var =
764 new(ctx) ir_variable(first_param->type,
Ian Romanick7e2aa912010-07-19 17:12:42 -0700765 talloc_strdup(ctx, "mat_ctor_mat"),
766 ir_var_temporary);
Ian Romanick81c7e942010-06-25 16:10:43 -0700767 instructions->push_tail(rhs_var);
768
769 ir_dereference *const rhs_var_ref =
770 new(ctx) ir_dereference_variable(rhs_var);
771 ir_instruction *const inst =
772 new(ctx) ir_assignment(rhs_var_ref, first_param, NULL);
773 instructions->push_tail(inst);
774
775
776 const unsigned swiz[4] = { 0, 1, 2, 3 };
777 const unsigned last_col = min(src_matrix->type->matrix_columns,
778 var->type->matrix_columns);
779 for (unsigned i = 0; i < last_col; i++) {
780 ir_rvalue *const lhs_col =
781 new(ctx) ir_dereference_array(var, new(ctx) ir_constant(i));
782 ir_rvalue *const rhs_col =
783 new(ctx) ir_dereference_array(rhs_var, new(ctx) ir_constant(i));
784
785 /* If one matrix has columns that are smaller than the columns of the
786 * other matrix, wrap the column access of the larger with a swizzle
787 * so that the LHS and RHS of the assignment have the same size (and
788 * therefore have the same type).
789 *
790 * It would be perfectly valid to unconditionally generate the
791 * swizzles, this this will typically result in a more compact IR tree.
792 */
793 ir_rvalue *lhs;
794 ir_rvalue *rhs;
795 if (lhs_col->type->vector_elements < rhs_col->type->vector_elements) {
796 lhs = lhs_col;
797
798 rhs = new(ctx) ir_swizzle(rhs_col, swiz,
799 lhs_col->type->vector_elements);
800 } else if (lhs_col->type->vector_elements
801 > rhs_col->type->vector_elements) {
802 lhs = new(ctx) ir_swizzle(lhs_col, swiz,
803 rhs_col->type->vector_elements);
804 rhs = rhs_col;
805 } else {
806 lhs = lhs_col;
807 rhs = rhs_col;
808 }
809
810 assert(lhs->type == rhs->type);
811
812 ir_instruction *inst = new(ctx) ir_assignment(lhs, rhs, NULL);
813 instructions->push_tail(inst);
814 }
815 } else {
816 const unsigned rows = type->matrix_columns;
817 const unsigned cols = type->vector_elements;
818 unsigned col_idx = 0;
819 unsigned row_idx = 0;
820
821 foreach_list (node, parameters) {
822 ir_rvalue *const rhs = (ir_rvalue *) node;
823 const unsigned components_remaining_this_column = rows - row_idx;
824 unsigned rhs_components = rhs->type->components();
825 unsigned rhs_base = 0;
826
827 /* Since the parameter might be used in the RHS of two assignments,
828 * generate a temporary and copy the paramter there.
829 */
Ian Romanick4b6feb02010-06-28 13:22:55 -0700830 ir_variable *rhs_var =
831 new(ctx) ir_variable(rhs->type,
Ian Romanick7e2aa912010-07-19 17:12:42 -0700832 talloc_strdup(ctx, "mat_ctor_vec"),
833 ir_var_temporary);
Ian Romanick81c7e942010-06-25 16:10:43 -0700834 instructions->push_tail(rhs_var);
835
836 ir_dereference *rhs_var_ref =
837 new(ctx) ir_dereference_variable(rhs_var);
838 ir_instruction *inst = new(ctx) ir_assignment(rhs_var_ref, rhs, NULL);
839 instructions->push_tail(inst);
840
841 /* Assign the current parameter to as many components of the matrix
842 * as it will fill.
843 *
844 * NOTE: A single vector parameter can span two matrix columns. A
845 * single vec4, for example, can completely fill a mat2.
846 */
847 if (rhs_components >= components_remaining_this_column) {
848 const unsigned count = min(rhs_components,
849 components_remaining_this_column);
850
851 rhs_var_ref = new(ctx) ir_dereference_variable(rhs_var);
852
853 ir_instruction *inst = assign_to_matrix_column(var, col_idx,
854 row_idx,
855 rhs_var_ref, 0,
856 count, ctx);
857 instructions->push_tail(inst);
858
859 rhs_base = count;
860
861 col_idx++;
862 row_idx = 0;
863 }
864
865 /* If there is data left in the parameter and components left to be
866 * set in the destination, emit another assignment. It is possible
867 * that the assignment could be of a vec4 to the last element of the
868 * matrix. In this case col_idx==cols, but there is still data
869 * left in the source parameter. Obviously, don't emit an assignment
870 * to data outside the destination matrix.
871 */
872 if ((col_idx < cols) && (rhs_base < rhs_components)) {
873 const unsigned count = rhs_components - rhs_base;
874
875 rhs_var_ref = new(ctx) ir_dereference_variable(rhs_var);
876
877 ir_instruction *inst = assign_to_matrix_column(var, col_idx,
878 row_idx,
879 rhs_var_ref,
880 rhs_base,
881 count, ctx);
882 instructions->push_tail(inst);
883
884 row_idx += count;
885 }
886 }
887 }
888
889 return new(ctx) ir_dereference_variable(var);
890}
891
892
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -0700893ir_rvalue *
Ian Romanick548fa292010-03-15 13:04:13 -0700894ast_function_expression::hir(exec_list *instructions,
895 struct _mesa_glsl_parse_state *state)
896{
Kenneth Graunke953ff122010-06-25 13:14:37 -0700897 void *ctx = state;
Ian Romanick548fa292010-03-15 13:04:13 -0700898 /* There are three sorts of function calls.
899 *
Kenneth Graunkef58bbd12010-07-08 18:03:28 -0700900 * 1. constructors - The first subexpression is an ast_type_specifier.
Ian Romanick548fa292010-03-15 13:04:13 -0700901 * 2. methods - Only the .length() method of array types.
902 * 3. functions - Calls to regular old functions.
903 *
Ian Romanick548fa292010-03-15 13:04:13 -0700904 * Method calls are actually detected when the ast_field_selection
905 * expression is handled.
906 */
907 if (is_constructor()) {
Ian Romanickabef9552010-03-23 15:08:30 -0700908 const ast_type_specifier *type = (ast_type_specifier *) subexpressions[0];
909 YYLTYPE loc = type->get_location();
Ian Romanick3e0ef5f2010-03-31 16:22:56 -0700910 const char *name;
Ian Romanickabef9552010-03-23 15:08:30 -0700911
Ian Romanick3e0ef5f2010-03-31 16:22:56 -0700912 const glsl_type *const constructor_type = type->glsl_type(& name, state);
Ian Romanickabef9552010-03-23 15:08:30 -0700913
914
915 /* Constructors for samplers are illegal.
916 */
917 if (constructor_type->is_sampler()) {
918 _mesa_glsl_error(& loc, state, "cannot construct sampler type `%s'",
919 constructor_type->name);
Carl Worthe01193a2010-06-23 18:25:04 -0700920 return ir_call::get_error_instruction(ctx);
Ian Romanickabef9552010-03-23 15:08:30 -0700921 }
922
Ian Romanickb6326ab2010-03-31 16:25:21 -0700923 if (constructor_type->is_array()) {
924 if (state->language_version <= 110) {
925 _mesa_glsl_error(& loc, state,
926 "array constructors forbidden in GLSL 1.10");
Carl Worthe01193a2010-06-23 18:25:04 -0700927 return ir_call::get_error_instruction(ctx);
Ian Romanickb6326ab2010-03-31 16:25:21 -0700928 }
929
Ian Romanick00aa1732010-03-31 16:48:48 -0700930 return process_array_constructor(instructions, constructor_type,
Ian Romanick3521f0b2010-05-10 10:47:14 -0700931 & loc, &this->expressions, state);
Ian Romanickb6326ab2010-03-31 16:25:21 -0700932 }
Ian Romanickabef9552010-03-23 15:08:30 -0700933
934 /* There are two kinds of constructor call. Constructors for built-in
935 * language types, such as mat4 and vec2, are free form. The only
936 * requirement is that the parameters must provide enough values of the
937 * correct scalar type. Constructors for arrays and structures must
938 * have the exact number of parameters with matching types in the
939 * correct order. These constructors follow essentially the same type
940 * matching rules as functions.
941 */
Kenneth Graunkef58bbd12010-07-08 18:03:28 -0700942 if (!constructor_type->is_numeric() && !constructor_type->is_boolean())
943 return ir_call::get_error_instruction(ctx);
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700944
Kenneth Graunkef58bbd12010-07-08 18:03:28 -0700945 /* Total number of components of the type being constructed. */
946 const unsigned type_components = constructor_type->components();
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700947
Kenneth Graunkef58bbd12010-07-08 18:03:28 -0700948 /* Number of components from parameters that have actually been
949 * consumed. This is used to perform several kinds of error checking.
950 */
951 unsigned components_used = 0;
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700952
Kenneth Graunkef58bbd12010-07-08 18:03:28 -0700953 unsigned matrix_parameters = 0;
954 unsigned nonmatrix_parameters = 0;
955 exec_list actual_parameters;
Ian Romanick9e08d012010-06-04 16:36:09 -0700956
Kenneth Graunkef58bbd12010-07-08 18:03:28 -0700957 foreach_list (n, &this->expressions) {
958 ast_node *ast = exec_node_data(ast_node, n, link);
959 ir_rvalue *result = ast->hir(instructions, state)->as_rvalue();
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700960
961 /* From page 50 (page 56 of the PDF) of the GLSL 1.50 spec:
962 *
Kenneth Graunkef58bbd12010-07-08 18:03:28 -0700963 * "It is an error to provide extra arguments beyond this
964 * last used argument."
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700965 */
Kenneth Graunkef58bbd12010-07-08 18:03:28 -0700966 if (components_used >= type_components) {
967 _mesa_glsl_error(& loc, state, "too many parameters to `%s' "
968 "constructor",
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700969 constructor_type->name);
Carl Worthe01193a2010-06-23 18:25:04 -0700970 return ir_call::get_error_instruction(ctx);
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700971 }
972
Kenneth Graunkef58bbd12010-07-08 18:03:28 -0700973 if (!result->type->is_numeric() && !result->type->is_boolean()) {
974 _mesa_glsl_error(& loc, state, "cannot construct `%s' from a "
975 "non-numeric data type",
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700976 constructor_type->name);
Carl Worthe01193a2010-06-23 18:25:04 -0700977 return ir_call::get_error_instruction(ctx);
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700978 }
979
Kenneth Graunkef58bbd12010-07-08 18:03:28 -0700980 /* Count the number of matrix and nonmatrix parameters. This
981 * is used below to enforce some of the constructor rules.
Ian Romanick699b2472010-06-25 17:36:17 -0700982 */
Kenneth Graunkef58bbd12010-07-08 18:03:28 -0700983 if (result->type->is_matrix())
984 matrix_parameters++;
985 else
986 nonmatrix_parameters++;
Ian Romanick699b2472010-06-25 17:36:17 -0700987
Kenneth Graunkef58bbd12010-07-08 18:03:28 -0700988 actual_parameters.push_tail(result);
989 components_used += result->type->components();
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700990 }
Ian Romanickabef9552010-03-23 15:08:30 -0700991
Kenneth Graunkef58bbd12010-07-08 18:03:28 -0700992 /* From page 28 (page 34 of the PDF) of the GLSL 1.10 spec:
993 *
994 * "It is an error to construct matrices from other matrices. This
995 * is reserved for future use."
996 */
997 if ((state->language_version <= 110) && (matrix_parameters > 0)
998 && constructor_type->is_matrix()) {
999 _mesa_glsl_error(& loc, state, "cannot construct `%s' from a "
1000 "matrix in GLSL 1.10",
1001 constructor_type->name);
1002 return ir_call::get_error_instruction(ctx);
1003 }
1004
1005 /* From page 50 (page 56 of the PDF) of the GLSL 1.50 spec:
1006 *
1007 * "If a matrix argument is given to a matrix constructor, it is
1008 * an error to have any other arguments."
1009 */
1010 if ((matrix_parameters > 0)
1011 && ((matrix_parameters + nonmatrix_parameters) > 1)
1012 && constructor_type->is_matrix()) {
1013 _mesa_glsl_error(& loc, state, "for matrix `%s' constructor, "
1014 "matrix must be only parameter",
1015 constructor_type->name);
1016 return ir_call::get_error_instruction(ctx);
1017 }
1018
1019 /* From page 28 (page 34 of the PDF) of the GLSL 1.10 spec:
1020 *
1021 * "In these cases, there must be enough components provided in the
1022 * arguments to provide an initializer for every component in the
1023 * constructed value."
1024 */
1025 if ((components_used < type_components) && (components_used != 1)) {
1026 _mesa_glsl_error(& loc, state, "too few components to construct "
1027 "`%s'",
1028 constructor_type->name);
1029 return ir_call::get_error_instruction(ctx);
1030 }
1031
Kenneth Graunke284d8212010-07-08 18:15:32 -07001032 /* Later, we cast each parameter to the same base type as the
1033 * constructor. Since there are no non-floating point matrices, we
1034 * need to break them up into a series of column vectors.
1035 */
1036 if (constructor_type->base_type != GLSL_TYPE_FLOAT) {
1037 foreach_list_safe(n, &actual_parameters) {
1038 ir_rvalue *matrix = (ir_rvalue *) n;
1039
1040 if (!matrix->type->is_matrix())
1041 continue;
1042
1043 /* Create a temporary containing the matrix. */
Ian Romanick7e2aa912010-07-19 17:12:42 -07001044 ir_variable *var = new(ctx) ir_variable(matrix->type, "matrix_tmp",
1045 ir_var_temporary);
Kenneth Graunke284d8212010-07-08 18:15:32 -07001046 instructions->push_tail(var);
1047 instructions->push_tail(new(ctx) ir_assignment(new(ctx)
1048 ir_dereference_variable(var), matrix, NULL));
1049 var->constant_value = matrix->constant_expression_value();
1050
1051 /* Replace the matrix with dereferences of its columns. */
1052 for (int i = 0; i < matrix->type->matrix_columns; i++) {
1053 matrix->insert_before(new (ctx) ir_dereference_array(var,
1054 new(ctx) ir_constant(i)));
1055 }
1056 matrix->remove();
1057 }
1058 }
1059
1060 bool all_parameters_are_constant = true;
1061
1062 /* Type cast each parameter and, if possible, fold constants.*/
1063 foreach_list_safe(n, &actual_parameters) {
1064 ir_rvalue *ir = (ir_rvalue *) n;
1065
1066 const glsl_type *desired_type =
1067 glsl_type::get_instance(constructor_type->base_type,
1068 ir->type->vector_elements,
1069 ir->type->matrix_columns);
1070 ir_rvalue *result = convert_component(ir, desired_type);
1071
1072 /* Attempt to convert the parameter to a constant valued expression.
1073 * After doing so, track whether or not all the parameters to the
1074 * constructor are trivially constant valued expressions.
1075 */
1076 ir_rvalue *const constant = result->constant_expression_value();
1077
1078 if (constant != NULL)
1079 result = constant;
1080 else
1081 all_parameters_are_constant = false;
1082
1083 if (result != ir) {
1084 ir->insert_before(result);
1085 ir->remove();
1086 }
1087 }
Kenneth Graunkef58bbd12010-07-08 18:03:28 -07001088
1089 /* If all of the parameters are trivially constant, create a
1090 * constant representing the complete collection of parameters.
1091 */
1092 if (all_parameters_are_constant) {
1093 if (components_used >= type_components)
1094 return new(ctx) ir_constant(constructor_type,
1095 & actual_parameters);
1096
1097 /* The above case must handle all scalar constructors.
1098 */
1099 assert(constructor_type->is_vector()
1100 || constructor_type->is_matrix());
1101
1102 /* Constructors with exactly one component are special for
1103 * vectors and matrices. For vectors it causes all elements of
1104 * the vector to be filled with the value. For matrices it
1105 * causes the matrix to be filled with 0 and the diagonal to be
1106 * filled with the value.
1107 */
1108 ir_constant_data data;
1109 ir_constant *const initializer =
1110 (ir_constant *) actual_parameters.head;
1111 if (constructor_type->is_matrix())
1112 generate_constructor_matrix(constructor_type, initializer,
1113 &data);
1114 else
1115 generate_constructor_vector(constructor_type, initializer,
1116 &data);
1117
1118 return new(ctx) ir_constant(constructor_type, &data);
1119 } else if (constructor_type->is_scalar()) {
1120 return dereference_component((ir_rvalue *) actual_parameters.head,
1121 0);
1122 } else if (constructor_type->is_vector()) {
1123 return emit_inline_vector_constructor(constructor_type,
1124 instructions,
1125 &actual_parameters,
1126 ctx);
1127 } else {
1128 assert(constructor_type->is_matrix());
1129 return emit_inline_matrix_constructor(constructor_type,
1130 instructions,
1131 &actual_parameters,
1132 ctx);
1133 }
Ian Romanick548fa292010-03-15 13:04:13 -07001134 } else {
1135 const ast_expression *id = subexpressions[0];
Ian Romanickf4749612010-03-15 13:26:02 -07001136 YYLTYPE loc = id->get_location();
Ian Romanickc0771312010-06-09 17:23:26 -07001137 exec_list actual_parameters;
1138
1139 process_parameters(instructions, &actual_parameters, &this->expressions,
1140 state);
Ian Romanick548fa292010-03-15 13:04:13 -07001141
Ian Romanickab92d0e2010-06-09 17:26:20 -07001142 const glsl_type *const type =
1143 state->symbols->get_type(id->primary_expression.identifier);
1144
1145 if ((type != NULL) && type->is_record()) {
1146 ir_constant *constant =
1147 constant_record_constructor(type, &loc, &actual_parameters, state);
1148
1149 if (constant != NULL)
1150 return constant;
1151 }
1152
Ian Romanickf4749612010-03-15 13:26:02 -07001153 return match_function_by_name(instructions,
1154 id->primary_expression.identifier, & loc,
Ian Romanickc0771312010-06-09 17:23:26 -07001155 &actual_parameters, state);
Ian Romanick548fa292010-03-15 13:04:13 -07001156 }
1157
Carl Worthe01193a2010-06-23 18:25:04 -07001158 return ir_call::get_error_instruction(ctx);
Ian Romanick548fa292010-03-15 13:04:13 -07001159}