blob: 643eb229a77dbbdaf240d98923237f76b1fc302c [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",
118 sig->function_name()));
119 instructions->push_tail(var);
120
121 deref = new(ctx) ir_dereference_variable(var);
122 ir_assignment *assign = new(ctx) ir_assignment(deref, call, NULL);
123 instructions->push_tail(assign);
124
125 deref = new(ctx) ir_dereference_variable(var);
126 return deref;
127 } else {
128 instructions->push_tail(call);
129 return NULL;
130 }
Ian Romanick68515ee2010-03-31 16:28:51 -0700131 } else {
132 /* FINISHME: Log a better error message here. G++ will show the types
133 * FINISHME: of the actual parameters and the set of candidate
134 * FINISHME: functions. A different error should also be logged when
135 * FINISHME: multiple functions match.
136 */
137 _mesa_glsl_error(loc, state, "no matching function for call to `%s'",
138 f->name);
Carl Worthe01193a2010-06-23 18:25:04 -0700139 return ir_call::get_error_instruction(ctx);
Ian Romanick68515ee2010-03-31 16:28:51 -0700140 }
141}
142
143
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -0700144static ir_rvalue *
Ian Romanickf4749612010-03-15 13:26:02 -0700145match_function_by_name(exec_list *instructions, const char *name,
Ian Romanickc0771312010-06-09 17:23:26 -0700146 YYLTYPE *loc, exec_list *actual_parameters,
Ian Romanickf4749612010-03-15 13:26:02 -0700147 struct _mesa_glsl_parse_state *state)
148{
Kenneth Graunke953ff122010-06-25 13:14:37 -0700149 void *ctx = state;
Ian Romanick8bde4ce2010-03-19 11:57:24 -0700150 ir_function *f = state->symbols->get_function(name);
Ian Romanickf4749612010-03-15 13:26:02 -0700151
152 if (f == NULL) {
153 _mesa_glsl_error(loc, state, "function `%s' undeclared", name);
Carl Worthe01193a2010-06-23 18:25:04 -0700154 return ir_call::get_error_instruction(ctx);
Ian Romanickf4749612010-03-15 13:26:02 -0700155 }
156
Ian Romanickc0771312010-06-09 17:23:26 -0700157 /* Once we've determined that the function being called might exist, try
158 * to find an overload of the function that matches the parameters.
Ian Romanickf4749612010-03-15 13:26:02 -0700159 */
Ian Romanickc0771312010-06-09 17:23:26 -0700160 return process_call(instructions, f, loc, actual_parameters, state);
Ian Romanickf4749612010-03-15 13:26:02 -0700161}
162
163
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700164/**
165 * Perform automatic type conversion of constructor parameters
166 */
167static ir_rvalue *
168convert_component(ir_rvalue *src, const glsl_type *desired_type)
169{
Carl Worth1660a292010-06-23 18:11:51 -0700170 void *ctx = talloc_parent(src);
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700171 const unsigned a = desired_type->base_type;
172 const unsigned b = src->type->base_type;
Ian Romanick00eb4662010-06-07 15:08:04 -0700173 ir_expression *result = NULL;
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700174
175 if (src->type->is_error())
176 return src;
177
178 assert(a <= GLSL_TYPE_BOOL);
179 assert(b <= GLSL_TYPE_BOOL);
180
181 if ((a == b) || (src->type->is_integer() && desired_type->is_integer()))
182 return src;
183
184 switch (a) {
185 case GLSL_TYPE_UINT:
186 case GLSL_TYPE_INT:
187 if (b == GLSL_TYPE_FLOAT)
Carl Worth1660a292010-06-23 18:11:51 -0700188 result = new(ctx) ir_expression(ir_unop_f2i, desired_type, src, NULL);
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700189 else {
190 assert(b == GLSL_TYPE_BOOL);
Carl Worth1660a292010-06-23 18:11:51 -0700191 result = new(ctx) ir_expression(ir_unop_b2i, desired_type, src, NULL);
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700192 }
Ian Romanick565185c2010-06-11 13:49:00 -0700193 break;
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700194 case GLSL_TYPE_FLOAT:
195 switch (b) {
196 case GLSL_TYPE_UINT:
Carl Worth1660a292010-06-23 18:11:51 -0700197 result = new(ctx) ir_expression(ir_unop_u2f, desired_type, src, NULL);
Ian Romanick00eb4662010-06-07 15:08:04 -0700198 break;
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700199 case GLSL_TYPE_INT:
Carl Worth1660a292010-06-23 18:11:51 -0700200 result = new(ctx) ir_expression(ir_unop_i2f, desired_type, src, NULL);
Ian Romanick00eb4662010-06-07 15:08:04 -0700201 break;
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700202 case GLSL_TYPE_BOOL:
Carl Worth1660a292010-06-23 18:11:51 -0700203 result = new(ctx) ir_expression(ir_unop_b2f, desired_type, src, NULL);
Ian Romanick00eb4662010-06-07 15:08:04 -0700204 break;
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700205 }
206 break;
Ian Romanick26b5d332010-06-25 16:19:45 -0700207 case GLSL_TYPE_BOOL:
Ian Romanickb74b43e2010-06-11 16:52:09 -0700208 switch (b) {
Ian Romanick26b5d332010-06-25 16:19:45 -0700209 case GLSL_TYPE_UINT:
210 case GLSL_TYPE_INT:
211 result = new(ctx) ir_expression(ir_unop_i2b, desired_type, src, NULL);
212 break;
213 case GLSL_TYPE_FLOAT:
214 result = new(ctx) ir_expression(ir_unop_f2b, desired_type, src, NULL);
215 break;
Ian Romanickb74b43e2010-06-11 16:52:09 -0700216 }
Ian Romanick26b5d332010-06-25 16:19:45 -0700217 break;
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700218 }
219
Ian Romanick00eb4662010-06-07 15:08:04 -0700220 assert(result != NULL);
221
222 ir_constant *const constant = result->constant_expression_value();
223 return (constant != NULL) ? (ir_rvalue *) constant : (ir_rvalue *) result;
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700224}
225
226
227/**
228 * Dereference a specific component from a scalar, vector, or matrix
229 */
230static ir_rvalue *
231dereference_component(ir_rvalue *src, unsigned component)
232{
Carl Worth1660a292010-06-23 18:11:51 -0700233 void *ctx = talloc_parent(src);
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700234 assert(component < src->type->components());
235
Ian Romanickc9cb1032010-06-04 16:20:35 -0700236 /* If the source is a constant, just create a new constant instead of a
237 * dereference of the existing constant.
238 */
239 ir_constant *constant = src->as_constant();
240 if (constant)
Carl Worth1660a292010-06-23 18:11:51 -0700241 return new(ctx) ir_constant(constant, component);
Ian Romanickc9cb1032010-06-04 16:20:35 -0700242
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700243 if (src->type->is_scalar()) {
244 return src;
245 } else if (src->type->is_vector()) {
Carl Worth1660a292010-06-23 18:11:51 -0700246 return new(ctx) ir_swizzle(src, component, 0, 0, 0, 1);
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700247 } else {
248 assert(src->type->is_matrix());
249
250 /* Dereference a row of the matrix, then call this function again to get
251 * a specific element from that row.
252 */
253 const int c = component / src->type->column_type()->vector_elements;
254 const int r = component % src->type->column_type()->vector_elements;
Carl Worth1660a292010-06-23 18:11:51 -0700255 ir_constant *const col_index = new(ctx) ir_constant(c);
256 ir_dereference *const col = new(ctx) ir_dereference_array(src, col_index);
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700257
258 col->type = src->type->column_type();
259
260 return dereference_component(col, r);
261 }
262
263 assert(!"Should not get here.");
264 return NULL;
265}
266
267
Ian Romanick00aa1732010-03-31 16:48:48 -0700268static ir_rvalue *
269process_array_constructor(exec_list *instructions,
270 const glsl_type *constructor_type,
Ian Romanick304ea902010-05-10 11:17:53 -0700271 YYLTYPE *loc, exec_list *parameters,
Ian Romanick00aa1732010-03-31 16:48:48 -0700272 struct _mesa_glsl_parse_state *state)
273{
Kenneth Graunke953ff122010-06-25 13:14:37 -0700274 void *ctx = state;
Ian Romanick00aa1732010-03-31 16:48:48 -0700275 /* Array constructors come in two forms: sized and unsized. Sized array
276 * constructors look like 'vec4[2](a, b)', where 'a' and 'b' are vec4
277 * variables. In this case the number of parameters must exactly match the
278 * specified size of the array.
279 *
280 * Unsized array constructors look like 'vec4[](a, b)', where 'a' and 'b'
281 * are vec4 variables. In this case the size of the array being constructed
282 * is determined by the number of parameters.
283 *
284 * From page 52 (page 58 of the PDF) of the GLSL 1.50 spec:
285 *
286 * "There must be exactly the same number of arguments as the size of
287 * the array being constructed. If no size is present in the
288 * constructor, then the array is explicitly sized to the number of
289 * arguments provided. The arguments are assigned in order, starting at
290 * element 0, to the elements of the constructed array. Each argument
291 * must be the same type as the element type of the array, or be a type
292 * that can be converted to the element type of the array according to
293 * Section 4.1.10 "Implicit Conversions.""
294 */
295 exec_list actual_parameters;
296 const unsigned parameter_count =
297 process_parameters(instructions, &actual_parameters, parameters, state);
298
299 if ((parameter_count == 0)
300 || ((constructor_type->length != 0)
301 && (constructor_type->length != parameter_count))) {
302 const unsigned min_param = (constructor_type->length == 0)
303 ? 1 : constructor_type->length;
304
305 _mesa_glsl_error(loc, state, "array constructor must have %s %u "
306 "parameter%s",
307 (constructor_type->length != 0) ? "at least" : "exactly",
308 min_param, (min_param <= 1) ? "" : "s");
Carl Worthe01193a2010-06-23 18:25:04 -0700309 return ir_call::get_error_instruction(ctx);
Ian Romanick00aa1732010-03-31 16:48:48 -0700310 }
311
312 if (constructor_type->length == 0) {
313 constructor_type =
Carl Worth12c41152010-06-18 17:52:59 -0700314 glsl_type::get_array_instance(state,
315 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,
512 talloc_strdup(ctx, "vec_ctor"));
Ian Romanickc31dcdf2010-06-23 15:19:40 -0700513 instructions->push_tail(var);
514
515 /* There are two kinds of vector constructors.
516 *
517 * - Construct a vector from a single scalar by replicating that scalar to
518 * all components of the vector.
519 *
520 * - Construct a vector from an arbirary combination of vectors and
521 * scalars. The components of the constructor parameters are assigned
522 * to the vector in order until the vector is full.
523 */
524 const unsigned lhs_components = type->components();
525 if (single_scalar_parameter(parameters)) {
526 ir_rvalue *first_param = (ir_rvalue *)parameters->head;
527 ir_rvalue *rhs = new(ctx) ir_swizzle(first_param, 0, 0, 0, 0,
528 lhs_components);
529 ir_dereference_variable *lhs = new(ctx) ir_dereference_variable(var);
530
531 assert(rhs->type == lhs->type);
532
533 ir_instruction *inst = new(ctx) ir_assignment(lhs, rhs, NULL);
534 instructions->push_tail(inst);
535 } else {
536 unsigned base_component = 0;
537 foreach_list(node, parameters) {
538 ir_rvalue *rhs = (ir_rvalue *) node;
539 unsigned rhs_components = rhs->type->components();
540
541 /* Do not try to assign more components to the vector than it has!
542 */
543 if ((rhs_components + base_component) > lhs_components) {
544 rhs_components = lhs_components - base_component;
545 }
546
547 /* Emit an assignment of the constructor parameter to the next set of
548 * components in the temporary variable.
549 */
550 unsigned mask[4] = { 0, 0, 0, 0 };
551 for (unsigned i = 0; i < rhs_components; i++) {
552 mask[i] = i + base_component;
553 }
554
555
556 ir_rvalue *lhs_ref = new(ctx) ir_dereference_variable(var);
557 ir_swizzle *lhs = new(ctx) ir_swizzle(lhs_ref, mask, rhs_components);
558
559 ir_instruction *inst = new(ctx) ir_assignment(lhs, rhs, NULL);
560 instructions->push_tail(inst);
561
562 /* Advance the component index by the number of components that were
563 * just assigned.
564 */
565 base_component += rhs_components;
566 }
567 }
568 return new(ctx) ir_dereference_variable(var);
569}
570
571
Ian Romanick81c7e942010-06-25 16:10:43 -0700572/**
573 * Generate assignment of a portion of a vector to a portion of a matrix column
574 *
575 * \param src_base First component of the source to be used in assignment
576 * \param column Column of destination to be assiged
577 * \param row_base First component of the destination column to be assigned
578 * \param count Number of components to be assigned
579 *
580 * \note
581 * \c src_base + \c count must be less than or equal to the number of components
582 * in the source vector.
583 */
584ir_instruction *
585assign_to_matrix_column(ir_variable *var, unsigned column, unsigned row_base,
586 ir_rvalue *src, unsigned src_base, unsigned count,
587 TALLOC_CTX *ctx)
588{
589 const unsigned mask[8] = { 0, 1, 2, 3, 0, 0, 0, 0 };
590
591 ir_constant *col_idx = new(ctx) ir_constant(column);
592 ir_rvalue *column_ref = new(ctx) ir_dereference_array(var, col_idx);
593
594 assert(column_ref->type->components() >= (row_base + count));
595 ir_rvalue *lhs = new(ctx) ir_swizzle(column_ref, &mask[row_base], count);
596
597 assert(src->type->components() >= (src_base + count));
598 ir_rvalue *rhs = new(ctx) ir_swizzle(src, &mask[src_base], count);
599
600 return new(ctx) ir_assignment(lhs, rhs, NULL);
601}
602
603
604/**
605 * Generate inline code for a matrix constructor
606 *
607 * The generated constructor code will consist of a temporary variable
608 * declaration of the same type as the constructor. A sequence of assignments
609 * from constructor parameters to the temporary will follow.
610 *
611 * \return
612 * An \c ir_dereference_variable of the temprorary generated in the constructor
613 * body.
614 */
615ir_rvalue *
616emit_inline_matrix_constructor(const glsl_type *type,
617 exec_list *instructions,
618 exec_list *parameters,
619 void *ctx)
620{
621 assert(!parameters->is_empty());
622
Ian Romanick4b6feb02010-06-28 13:22:55 -0700623 ir_variable *var = new(ctx) ir_variable(type,
624 talloc_strdup(ctx, "mat_ctor"));
Ian Romanick81c7e942010-06-25 16:10:43 -0700625 instructions->push_tail(var);
626
627 /* There are three kinds of matrix constructors.
628 *
629 * - Construct a matrix from a single scalar by replicating that scalar to
630 * along the diagonal of the matrix and setting all other components to
631 * zero.
632 *
633 * - Construct a matrix from an arbirary combination of vectors and
634 * scalars. The components of the constructor parameters are assigned
635 * to the matrix in colum-major order until the matrix is full.
636 *
637 * - Construct a matrix from a single matrix. The source matrix is copied
638 * to the upper left portion of the constructed matrix, and the remaining
639 * elements take values from the identity matrix.
640 */
641 ir_rvalue *const first_param = (ir_rvalue *) parameters->head;
642 if (single_scalar_parameter(parameters)) {
643 /* Assign the scalar to the X component of a vec4, and fill the remaining
644 * components with zero.
645 */
Ian Romanick4b6feb02010-06-28 13:22:55 -0700646 ir_variable *rhs_var =
647 new(ctx) ir_variable(glsl_type::vec4_type,
648 talloc_strdup(ctx, "mat_ctor_vec"));
Ian Romanick81c7e942010-06-25 16:10:43 -0700649 instructions->push_tail(rhs_var);
650
651 ir_constant_data zero;
652 zero.f[0] = 0.0;
653 zero.f[1] = 0.0;
654 zero.f[2] = 0.0;
655 zero.f[3] = 0.0;
656
657 ir_instruction *inst =
658 new(ctx) ir_assignment(new(ctx) ir_dereference_variable(rhs_var),
659 new(ctx) ir_constant(rhs_var->type, &zero),
660 NULL);
661 instructions->push_tail(inst);
662
663 ir_rvalue *const rhs_ref = new(ctx) ir_dereference_variable(rhs_var);
664 ir_rvalue *const x_of_rhs = new(ctx) ir_swizzle(rhs_ref, 0, 0, 0, 0, 1);
665
666 inst = new(ctx) ir_assignment(x_of_rhs, first_param, NULL);
667 instructions->push_tail(inst);
668
669 /* Assign the temporary vector to each column of the destination matrix
670 * with a swizzle that puts the X component on the diagonal of the
671 * matrix. In some cases this may mean that the X component does not
672 * get assigned into the column at all (i.e., when the matrix has more
673 * columns than rows).
674 */
675 static const unsigned rhs_swiz[4][4] = {
676 { 0, 1, 1, 1 },
677 { 1, 0, 1, 1 },
678 { 1, 1, 0, 1 },
679 { 1, 1, 1, 0 }
680 };
681
682 const unsigned cols_to_init = min(type->matrix_columns,
683 type->vector_elements);
684 for (unsigned i = 0; i < cols_to_init; i++) {
685 ir_constant *const col_idx = new(ctx) ir_constant(i);
686 ir_rvalue *const col_ref = new(ctx) ir_dereference_array(var, col_idx);
687
688 ir_rvalue *const rhs_ref = new(ctx) ir_dereference_variable(rhs_var);
689 ir_rvalue *const rhs = new(ctx) ir_swizzle(rhs_ref, rhs_swiz[i],
690 type->vector_elements);
691
692 inst = new(ctx) ir_assignment(col_ref, rhs, NULL);
693 instructions->push_tail(inst);
694 }
695
696 for (unsigned i = cols_to_init; i < type->matrix_columns; i++) {
697 ir_constant *const col_idx = new(ctx) ir_constant(i);
698 ir_rvalue *const col_ref = new(ctx) ir_dereference_array(var, col_idx);
699
700 ir_rvalue *const rhs_ref = new(ctx) ir_dereference_variable(rhs_var);
701 ir_rvalue *const rhs = new(ctx) ir_swizzle(rhs_ref, 1, 1, 1, 1,
702 type->vector_elements);
703
704 inst = new(ctx) ir_assignment(col_ref, rhs, NULL);
705 instructions->push_tail(inst);
706 }
707 } else if (first_param->type->is_matrix()) {
708 /* From page 50 (56 of the PDF) of the GLSL 1.50 spec:
709 *
710 * "If a matrix is constructed from a matrix, then each component
711 * (column i, row j) in the result that has a corresponding
712 * component (column i, row j) in the argument will be initialized
713 * from there. All other components will be initialized to the
714 * identity matrix. If a matrix argument is given to a matrix
715 * constructor, it is an error to have any other arguments."
716 */
717 assert(first_param->next->is_tail_sentinal());
718 ir_rvalue *const src_matrix = first_param;
719
720 /* If the source matrix is smaller, pre-initialize the relavent parts of
721 * the destination matrix to the identity matrix.
722 */
723 if ((src_matrix->type->matrix_columns < var->type->matrix_columns)
724 || (src_matrix->type->vector_elements < var->type->vector_elements)) {
725
726 /* If the source matrix has fewer rows, every column of the destination
727 * must be initialized. Otherwise only the columns in the destination
728 * that do not exist in the source must be initialized.
729 */
730 unsigned col =
731 (src_matrix->type->vector_elements < var->type->vector_elements)
732 ? 0 : src_matrix->type->matrix_columns;
733
734 const glsl_type *const col_type = var->type->column_type();
735 for (/* empty */; col < var->type->matrix_columns; col++) {
736 ir_constant_data ident;
737
738 ident.f[0] = 0.0;
739 ident.f[1] = 0.0;
740 ident.f[2] = 0.0;
741 ident.f[3] = 0.0;
742
743 ident.f[col] = 1.0;
744
745 ir_rvalue *const rhs = new(ctx) ir_constant(col_type, &ident);
746
747 ir_rvalue *const lhs =
748 new(ctx) ir_dereference_array(var, new(ctx) ir_constant(col));
749
750 ir_instruction *inst = new(ctx) ir_assignment(lhs, rhs, NULL);
751 instructions->push_tail(inst);
752 }
753 }
754
755 /* Assign columns from the source matrix to the destination matrix.
756 *
757 * Since the parameter will be used in the RHS of multiple assignments,
758 * generate a temporary and copy the paramter there.
759 */
Ian Romanick4b6feb02010-06-28 13:22:55 -0700760 ir_variable *const rhs_var =
761 new(ctx) ir_variable(first_param->type,
762 talloc_strdup(ctx, "mat_ctor_mat"));
Ian Romanick81c7e942010-06-25 16:10:43 -0700763 instructions->push_tail(rhs_var);
764
765 ir_dereference *const rhs_var_ref =
766 new(ctx) ir_dereference_variable(rhs_var);
767 ir_instruction *const inst =
768 new(ctx) ir_assignment(rhs_var_ref, first_param, NULL);
769 instructions->push_tail(inst);
770
771
772 const unsigned swiz[4] = { 0, 1, 2, 3 };
773 const unsigned last_col = min(src_matrix->type->matrix_columns,
774 var->type->matrix_columns);
775 for (unsigned i = 0; i < last_col; i++) {
776 ir_rvalue *const lhs_col =
777 new(ctx) ir_dereference_array(var, new(ctx) ir_constant(i));
778 ir_rvalue *const rhs_col =
779 new(ctx) ir_dereference_array(rhs_var, new(ctx) ir_constant(i));
780
781 /* If one matrix has columns that are smaller than the columns of the
782 * other matrix, wrap the column access of the larger with a swizzle
783 * so that the LHS and RHS of the assignment have the same size (and
784 * therefore have the same type).
785 *
786 * It would be perfectly valid to unconditionally generate the
787 * swizzles, this this will typically result in a more compact IR tree.
788 */
789 ir_rvalue *lhs;
790 ir_rvalue *rhs;
791 if (lhs_col->type->vector_elements < rhs_col->type->vector_elements) {
792 lhs = lhs_col;
793
794 rhs = new(ctx) ir_swizzle(rhs_col, swiz,
795 lhs_col->type->vector_elements);
796 } else if (lhs_col->type->vector_elements
797 > rhs_col->type->vector_elements) {
798 lhs = new(ctx) ir_swizzle(lhs_col, swiz,
799 rhs_col->type->vector_elements);
800 rhs = rhs_col;
801 } else {
802 lhs = lhs_col;
803 rhs = rhs_col;
804 }
805
806 assert(lhs->type == rhs->type);
807
808 ir_instruction *inst = new(ctx) ir_assignment(lhs, rhs, NULL);
809 instructions->push_tail(inst);
810 }
811 } else {
812 const unsigned rows = type->matrix_columns;
813 const unsigned cols = type->vector_elements;
814 unsigned col_idx = 0;
815 unsigned row_idx = 0;
816
817 foreach_list (node, parameters) {
818 ir_rvalue *const rhs = (ir_rvalue *) node;
819 const unsigned components_remaining_this_column = rows - row_idx;
820 unsigned rhs_components = rhs->type->components();
821 unsigned rhs_base = 0;
822
823 /* Since the parameter might be used in the RHS of two assignments,
824 * generate a temporary and copy the paramter there.
825 */
Ian Romanick4b6feb02010-06-28 13:22:55 -0700826 ir_variable *rhs_var =
827 new(ctx) ir_variable(rhs->type,
828 talloc_strdup(ctx, "mat_ctor_vec"));
Ian Romanick81c7e942010-06-25 16:10:43 -0700829 instructions->push_tail(rhs_var);
830
831 ir_dereference *rhs_var_ref =
832 new(ctx) ir_dereference_variable(rhs_var);
833 ir_instruction *inst = new(ctx) ir_assignment(rhs_var_ref, rhs, NULL);
834 instructions->push_tail(inst);
835
836 /* Assign the current parameter to as many components of the matrix
837 * as it will fill.
838 *
839 * NOTE: A single vector parameter can span two matrix columns. A
840 * single vec4, for example, can completely fill a mat2.
841 */
842 if (rhs_components >= components_remaining_this_column) {
843 const unsigned count = min(rhs_components,
844 components_remaining_this_column);
845
846 rhs_var_ref = new(ctx) ir_dereference_variable(rhs_var);
847
848 ir_instruction *inst = assign_to_matrix_column(var, col_idx,
849 row_idx,
850 rhs_var_ref, 0,
851 count, ctx);
852 instructions->push_tail(inst);
853
854 rhs_base = count;
855
856 col_idx++;
857 row_idx = 0;
858 }
859
860 /* If there is data left in the parameter and components left to be
861 * set in the destination, emit another assignment. It is possible
862 * that the assignment could be of a vec4 to the last element of the
863 * matrix. In this case col_idx==cols, but there is still data
864 * left in the source parameter. Obviously, don't emit an assignment
865 * to data outside the destination matrix.
866 */
867 if ((col_idx < cols) && (rhs_base < rhs_components)) {
868 const unsigned count = rhs_components - rhs_base;
869
870 rhs_var_ref = new(ctx) ir_dereference_variable(rhs_var);
871
872 ir_instruction *inst = assign_to_matrix_column(var, col_idx,
873 row_idx,
874 rhs_var_ref,
875 rhs_base,
876 count, ctx);
877 instructions->push_tail(inst);
878
879 row_idx += count;
880 }
881 }
882 }
883
884 return new(ctx) ir_dereference_variable(var);
885}
886
887
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -0700888ir_rvalue *
Ian Romanick548fa292010-03-15 13:04:13 -0700889ast_function_expression::hir(exec_list *instructions,
890 struct _mesa_glsl_parse_state *state)
891{
Kenneth Graunke953ff122010-06-25 13:14:37 -0700892 void *ctx = state;
Ian Romanick548fa292010-03-15 13:04:13 -0700893 /* There are three sorts of function calls.
894 *
Kenneth Graunkef58bbd12010-07-08 18:03:28 -0700895 * 1. constructors - The first subexpression is an ast_type_specifier.
Ian Romanick548fa292010-03-15 13:04:13 -0700896 * 2. methods - Only the .length() method of array types.
897 * 3. functions - Calls to regular old functions.
898 *
Ian Romanick548fa292010-03-15 13:04:13 -0700899 * Method calls are actually detected when the ast_field_selection
900 * expression is handled.
901 */
902 if (is_constructor()) {
Ian Romanickabef9552010-03-23 15:08:30 -0700903 const ast_type_specifier *type = (ast_type_specifier *) subexpressions[0];
904 YYLTYPE loc = type->get_location();
Ian Romanick3e0ef5f2010-03-31 16:22:56 -0700905 const char *name;
Ian Romanickabef9552010-03-23 15:08:30 -0700906
Ian Romanick3e0ef5f2010-03-31 16:22:56 -0700907 const glsl_type *const constructor_type = type->glsl_type(& name, state);
Ian Romanickabef9552010-03-23 15:08:30 -0700908
909
910 /* Constructors for samplers are illegal.
911 */
912 if (constructor_type->is_sampler()) {
913 _mesa_glsl_error(& loc, state, "cannot construct sampler type `%s'",
914 constructor_type->name);
Carl Worthe01193a2010-06-23 18:25:04 -0700915 return ir_call::get_error_instruction(ctx);
Ian Romanickabef9552010-03-23 15:08:30 -0700916 }
917
Ian Romanickb6326ab2010-03-31 16:25:21 -0700918 if (constructor_type->is_array()) {
919 if (state->language_version <= 110) {
920 _mesa_glsl_error(& loc, state,
921 "array constructors forbidden in GLSL 1.10");
Carl Worthe01193a2010-06-23 18:25:04 -0700922 return ir_call::get_error_instruction(ctx);
Ian Romanickb6326ab2010-03-31 16:25:21 -0700923 }
924
Ian Romanick00aa1732010-03-31 16:48:48 -0700925 return process_array_constructor(instructions, constructor_type,
Ian Romanick3521f0b2010-05-10 10:47:14 -0700926 & loc, &this->expressions, state);
Ian Romanickb6326ab2010-03-31 16:25:21 -0700927 }
Ian Romanickabef9552010-03-23 15:08:30 -0700928
929 /* There are two kinds of constructor call. Constructors for built-in
930 * language types, such as mat4 and vec2, are free form. The only
931 * requirement is that the parameters must provide enough values of the
932 * correct scalar type. Constructors for arrays and structures must
933 * have the exact number of parameters with matching types in the
934 * correct order. These constructors follow essentially the same type
935 * matching rules as functions.
936 */
Kenneth Graunkef58bbd12010-07-08 18:03:28 -0700937 if (!constructor_type->is_numeric() && !constructor_type->is_boolean())
938 return ir_call::get_error_instruction(ctx);
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700939
Kenneth Graunkef58bbd12010-07-08 18:03:28 -0700940 /* Total number of components of the type being constructed. */
941 const unsigned type_components = constructor_type->components();
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700942
Kenneth Graunkef58bbd12010-07-08 18:03:28 -0700943 /* Number of components from parameters that have actually been
944 * consumed. This is used to perform several kinds of error checking.
945 */
946 unsigned components_used = 0;
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700947
Kenneth Graunkef58bbd12010-07-08 18:03:28 -0700948 unsigned matrix_parameters = 0;
949 unsigned nonmatrix_parameters = 0;
950 exec_list actual_parameters;
Ian Romanick9e08d012010-06-04 16:36:09 -0700951
Kenneth Graunkef58bbd12010-07-08 18:03:28 -0700952 foreach_list (n, &this->expressions) {
953 ast_node *ast = exec_node_data(ast_node, n, link);
954 ir_rvalue *result = ast->hir(instructions, state)->as_rvalue();
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700955
956 /* From page 50 (page 56 of the PDF) of the GLSL 1.50 spec:
957 *
Kenneth Graunkef58bbd12010-07-08 18:03:28 -0700958 * "It is an error to provide extra arguments beyond this
959 * last used argument."
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700960 */
Kenneth Graunkef58bbd12010-07-08 18:03:28 -0700961 if (components_used >= type_components) {
962 _mesa_glsl_error(& loc, state, "too many parameters to `%s' "
963 "constructor",
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700964 constructor_type->name);
Carl Worthe01193a2010-06-23 18:25:04 -0700965 return ir_call::get_error_instruction(ctx);
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700966 }
967
Kenneth Graunkef58bbd12010-07-08 18:03:28 -0700968 if (!result->type->is_numeric() && !result->type->is_boolean()) {
969 _mesa_glsl_error(& loc, state, "cannot construct `%s' from a "
970 "non-numeric data type",
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700971 constructor_type->name);
Carl Worthe01193a2010-06-23 18:25:04 -0700972 return ir_call::get_error_instruction(ctx);
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700973 }
974
Kenneth Graunkef58bbd12010-07-08 18:03:28 -0700975 /* Count the number of matrix and nonmatrix parameters. This
976 * is used below to enforce some of the constructor rules.
Ian Romanick699b2472010-06-25 17:36:17 -0700977 */
Kenneth Graunkef58bbd12010-07-08 18:03:28 -0700978 if (result->type->is_matrix())
979 matrix_parameters++;
980 else
981 nonmatrix_parameters++;
Ian Romanick699b2472010-06-25 17:36:17 -0700982
Kenneth Graunkef58bbd12010-07-08 18:03:28 -0700983 actual_parameters.push_tail(result);
984 components_used += result->type->components();
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700985 }
Ian Romanickabef9552010-03-23 15:08:30 -0700986
Kenneth Graunkef58bbd12010-07-08 18:03:28 -0700987 /* From page 28 (page 34 of the PDF) of the GLSL 1.10 spec:
988 *
989 * "It is an error to construct matrices from other matrices. This
990 * is reserved for future use."
991 */
992 if ((state->language_version <= 110) && (matrix_parameters > 0)
993 && constructor_type->is_matrix()) {
994 _mesa_glsl_error(& loc, state, "cannot construct `%s' from a "
995 "matrix in GLSL 1.10",
996 constructor_type->name);
997 return ir_call::get_error_instruction(ctx);
998 }
999
1000 /* From page 50 (page 56 of the PDF) of the GLSL 1.50 spec:
1001 *
1002 * "If a matrix argument is given to a matrix constructor, it is
1003 * an error to have any other arguments."
1004 */
1005 if ((matrix_parameters > 0)
1006 && ((matrix_parameters + nonmatrix_parameters) > 1)
1007 && constructor_type->is_matrix()) {
1008 _mesa_glsl_error(& loc, state, "for matrix `%s' constructor, "
1009 "matrix must be only parameter",
1010 constructor_type->name);
1011 return ir_call::get_error_instruction(ctx);
1012 }
1013
1014 /* From page 28 (page 34 of the PDF) of the GLSL 1.10 spec:
1015 *
1016 * "In these cases, there must be enough components provided in the
1017 * arguments to provide an initializer for every component in the
1018 * constructed value."
1019 */
1020 if ((components_used < type_components) && (components_used != 1)) {
1021 _mesa_glsl_error(& loc, state, "too few components to construct "
1022 "`%s'",
1023 constructor_type->name);
1024 return ir_call::get_error_instruction(ctx);
1025 }
1026
Kenneth Graunke284d8212010-07-08 18:15:32 -07001027 /* Later, we cast each parameter to the same base type as the
1028 * constructor. Since there are no non-floating point matrices, we
1029 * need to break them up into a series of column vectors.
1030 */
1031 if (constructor_type->base_type != GLSL_TYPE_FLOAT) {
1032 foreach_list_safe(n, &actual_parameters) {
1033 ir_rvalue *matrix = (ir_rvalue *) n;
1034
1035 if (!matrix->type->is_matrix())
1036 continue;
1037
1038 /* Create a temporary containing the matrix. */
1039 ir_variable *var = new(ctx) ir_variable(matrix->type, "matrix_tmp");
1040 instructions->push_tail(var);
1041 instructions->push_tail(new(ctx) ir_assignment(new(ctx)
1042 ir_dereference_variable(var), matrix, NULL));
1043 var->constant_value = matrix->constant_expression_value();
1044
1045 /* Replace the matrix with dereferences of its columns. */
1046 for (int i = 0; i < matrix->type->matrix_columns; i++) {
1047 matrix->insert_before(new (ctx) ir_dereference_array(var,
1048 new(ctx) ir_constant(i)));
1049 }
1050 matrix->remove();
1051 }
1052 }
1053
1054 bool all_parameters_are_constant = true;
1055
1056 /* Type cast each parameter and, if possible, fold constants.*/
1057 foreach_list_safe(n, &actual_parameters) {
1058 ir_rvalue *ir = (ir_rvalue *) n;
1059
1060 const glsl_type *desired_type =
1061 glsl_type::get_instance(constructor_type->base_type,
1062 ir->type->vector_elements,
1063 ir->type->matrix_columns);
1064 ir_rvalue *result = convert_component(ir, desired_type);
1065
1066 /* Attempt to convert the parameter to a constant valued expression.
1067 * After doing so, track whether or not all the parameters to the
1068 * constructor are trivially constant valued expressions.
1069 */
1070 ir_rvalue *const constant = result->constant_expression_value();
1071
1072 if (constant != NULL)
1073 result = constant;
1074 else
1075 all_parameters_are_constant = false;
1076
1077 if (result != ir) {
1078 ir->insert_before(result);
1079 ir->remove();
1080 }
1081 }
Kenneth Graunkef58bbd12010-07-08 18:03:28 -07001082
1083 /* If all of the parameters are trivially constant, create a
1084 * constant representing the complete collection of parameters.
1085 */
1086 if (all_parameters_are_constant) {
1087 if (components_used >= type_components)
1088 return new(ctx) ir_constant(constructor_type,
1089 & actual_parameters);
1090
1091 /* The above case must handle all scalar constructors.
1092 */
1093 assert(constructor_type->is_vector()
1094 || constructor_type->is_matrix());
1095
1096 /* Constructors with exactly one component are special for
1097 * vectors and matrices. For vectors it causes all elements of
1098 * the vector to be filled with the value. For matrices it
1099 * causes the matrix to be filled with 0 and the diagonal to be
1100 * filled with the value.
1101 */
1102 ir_constant_data data;
1103 ir_constant *const initializer =
1104 (ir_constant *) actual_parameters.head;
1105 if (constructor_type->is_matrix())
1106 generate_constructor_matrix(constructor_type, initializer,
1107 &data);
1108 else
1109 generate_constructor_vector(constructor_type, initializer,
1110 &data);
1111
1112 return new(ctx) ir_constant(constructor_type, &data);
1113 } else if (constructor_type->is_scalar()) {
1114 return dereference_component((ir_rvalue *) actual_parameters.head,
1115 0);
1116 } else if (constructor_type->is_vector()) {
1117 return emit_inline_vector_constructor(constructor_type,
1118 instructions,
1119 &actual_parameters,
1120 ctx);
1121 } else {
1122 assert(constructor_type->is_matrix());
1123 return emit_inline_matrix_constructor(constructor_type,
1124 instructions,
1125 &actual_parameters,
1126 ctx);
1127 }
Ian Romanick548fa292010-03-15 13:04:13 -07001128 } else {
1129 const ast_expression *id = subexpressions[0];
Ian Romanickf4749612010-03-15 13:26:02 -07001130 YYLTYPE loc = id->get_location();
Ian Romanickc0771312010-06-09 17:23:26 -07001131 exec_list actual_parameters;
1132
1133 process_parameters(instructions, &actual_parameters, &this->expressions,
1134 state);
Ian Romanick548fa292010-03-15 13:04:13 -07001135
Ian Romanickab92d0e2010-06-09 17:26:20 -07001136 const glsl_type *const type =
1137 state->symbols->get_type(id->primary_expression.identifier);
1138
1139 if ((type != NULL) && type->is_record()) {
1140 ir_constant *constant =
1141 constant_record_constructor(type, &loc, &actual_parameters, state);
1142
1143 if (constant != NULL)
1144 return constant;
1145 }
1146
Ian Romanickf4749612010-03-15 13:26:02 -07001147 return match_function_by_name(instructions,
1148 id->primary_expression.identifier, & loc,
Ian Romanickc0771312010-06-09 17:23:26 -07001149 &actual_parameters, state);
Ian Romanick548fa292010-03-15 13:04:13 -07001150 }
1151
Carl Worthe01193a2010-06-23 18:25:04 -07001152 return ir_call::get_error_instruction(ctx);
Ian Romanick548fa292010-03-15 13:04:13 -07001153}