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