blob: 855f27f175698c4b53a4df734da18ba6a03fb775 [file] [log] [blame]
Ian Romanick548fa292010-03-15 13:04:13 -07001/*
2 * Copyright © 2010 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 */
23
Ian Romanick8bde4ce2010-03-19 11:57:24 -070024#include "glsl_symbol_table.h"
Ian Romanick548fa292010-03-15 13:04:13 -070025#include "ast.h"
26#include "glsl_types.h"
27#include "ir.h"
28
Ian Romanick81c7e942010-06-25 16:10:43 -070029inline unsigned min(unsigned a, unsigned b)
30{
31 return (a < b) ? a : b;
32}
33
Kenneth Graunke17a307d2010-07-14 13:22:07 -070034static ir_rvalue *
35convert_component(ir_rvalue *src, const glsl_type *desired_type);
36
Ian Romanick68515ee2010-03-31 16:28:51 -070037static unsigned
38process_parameters(exec_list *instructions, exec_list *actual_parameters,
Ian Romanick304ea902010-05-10 11:17:53 -070039 exec_list *parameters,
Ian Romanick68515ee2010-03-31 16:28:51 -070040 struct _mesa_glsl_parse_state *state)
41{
Ian Romanick68515ee2010-03-31 16:28:51 -070042 unsigned count = 0;
43
Ian Romanick304ea902010-05-10 11:17:53 -070044 foreach_list (n, parameters) {
45 ast_node *const ast = exec_node_data(ast_node, n, link);
Ian Romanick1a872b12010-06-09 17:31:02 -070046 ir_rvalue *result = ast->hir(instructions, state);
47
48 ir_constant *const constant = result->constant_expression_value();
49 if (constant != NULL)
50 result = constant;
Ian Romanick68515ee2010-03-31 16:28:51 -070051
Ian Romanick3521f0b2010-05-10 10:47:14 -070052 actual_parameters->push_tail(result);
53 count++;
Ian Romanick68515ee2010-03-31 16:28:51 -070054 }
55
56 return count;
57}
58
59
60static ir_rvalue *
61process_call(exec_list *instructions, ir_function *f,
62 YYLTYPE *loc, exec_list *actual_parameters,
63 struct _mesa_glsl_parse_state *state)
64{
Kenneth Graunke953ff122010-06-25 13:14:37 -070065 void *ctx = state;
Carl Worth1660a292010-06-23 18:11:51 -070066
Eric Anholt1f472452010-07-18 17:45:16 -070067 ir_function_signature *sig = f->matching_signature(actual_parameters);
Ian Romanick68515ee2010-03-31 16:28:51 -070068
69 /* The instructions param will be used when the FINISHMEs below are done */
70 (void) instructions;
71
72 if (sig != NULL) {
Ian Romanickc35bb002010-04-02 15:51:02 -070073 /* Verify that 'out' and 'inout' actual parameters are lvalues. This
74 * isn't done in ir_function::matching_signature because that function
75 * cannot generate the necessary diagnostics.
76 */
77 exec_list_iterator actual_iter = actual_parameters->iterator();
78 exec_list_iterator formal_iter = sig->parameters.iterator();
79
80 while (actual_iter.has_next()) {
Eric Anholtf1ddca92010-04-07 12:35:34 -070081 ir_rvalue *actual = (ir_rvalue *) actual_iter.get();
82 ir_variable *formal = (ir_variable *) formal_iter.get();
Ian Romanickc35bb002010-04-02 15:51:02 -070083
84 assert(actual != NULL);
85 assert(formal != NULL);
86
87 if ((formal->mode == ir_var_out)
88 || (formal->mode == ir_var_inout)) {
89 if (! actual->is_lvalue()) {
90 /* FINISHME: Log a better diagnostic here. There is no way
91 * FINISHME: to tell the user which parameter is invalid.
92 */
93 _mesa_glsl_error(loc, state, "`%s' parameter is not lvalue",
94 (formal->mode == ir_var_out) ? "out" : "inout");
95 }
96 }
97
Kenneth Graunke17a307d2010-07-14 13:22:07 -070098 if (formal->type->is_numeric() || formal->type->is_boolean()) {
99 ir_rvalue *converted = convert_component(actual, formal->type);
100 actual->replace_with(converted);
101 }
102
Ian Romanickc35bb002010-04-02 15:51:02 -0700103 actual_iter.next();
104 formal_iter.next();
105 }
106
Eric Anholt02d37112010-07-20 15:50:48 -0700107 /* Always insert the call in the instruction stream, and return a deref
108 * of its return val if it returns a value, since we don't know if
109 * the rvalue is going to be assigned to anything or not.
110 */
111 ir_call *call = new(ctx) ir_call(sig, actual_parameters);
112 if (!sig->return_type->is_void()) {
113 ir_variable *var;
114 ir_dereference_variable *deref;
115
116 var = new(ctx) ir_variable(sig->return_type,
117 talloc_asprintf(ctx, "%s_retval",
Ian Romanick7e2aa912010-07-19 17:12:42 -0700118 sig->function_name()),
119 ir_var_temporary);
Eric Anholt02d37112010-07-20 15:50:48 -0700120 instructions->push_tail(var);
121
122 deref = new(ctx) ir_dereference_variable(var);
123 ir_assignment *assign = new(ctx) ir_assignment(deref, call, NULL);
124 instructions->push_tail(assign);
125
126 deref = new(ctx) ir_dereference_variable(var);
127 return deref;
128 } else {
129 instructions->push_tail(call);
130 return NULL;
131 }
Ian Romanick68515ee2010-03-31 16:28:51 -0700132 } else {
133 /* FINISHME: Log a better error message here. G++ will show the types
134 * FINISHME: of the actual parameters and the set of candidate
135 * FINISHME: functions. A different error should also be logged when
136 * FINISHME: multiple functions match.
137 */
138 _mesa_glsl_error(loc, state, "no matching function for call to `%s'",
139 f->name);
Carl Worthe01193a2010-06-23 18:25:04 -0700140 return ir_call::get_error_instruction(ctx);
Ian Romanick68515ee2010-03-31 16:28:51 -0700141 }
142}
143
144
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -0700145static ir_rvalue *
Ian Romanickf4749612010-03-15 13:26:02 -0700146match_function_by_name(exec_list *instructions, const char *name,
Ian Romanickc0771312010-06-09 17:23:26 -0700147 YYLTYPE *loc, exec_list *actual_parameters,
Ian Romanickf4749612010-03-15 13:26:02 -0700148 struct _mesa_glsl_parse_state *state)
149{
Kenneth Graunke953ff122010-06-25 13:14:37 -0700150 void *ctx = state;
Ian Romanick8bde4ce2010-03-19 11:57:24 -0700151 ir_function *f = state->symbols->get_function(name);
Ian Romanickf4749612010-03-15 13:26:02 -0700152
153 if (f == NULL) {
154 _mesa_glsl_error(loc, state, "function `%s' undeclared", name);
Carl Worthe01193a2010-06-23 18:25:04 -0700155 return ir_call::get_error_instruction(ctx);
Ian Romanickf4749612010-03-15 13:26:02 -0700156 }
157
Ian Romanickc0771312010-06-09 17:23:26 -0700158 /* Once we've determined that the function being called might exist, try
159 * to find an overload of the function that matches the parameters.
Ian Romanickf4749612010-03-15 13:26:02 -0700160 */
Ian Romanickc0771312010-06-09 17:23:26 -0700161 return process_call(instructions, f, loc, actual_parameters, state);
Ian Romanickf4749612010-03-15 13:26:02 -0700162}
163
164
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700165/**
166 * Perform automatic type conversion of constructor parameters
Kenneth Graunke0048c7a2010-07-19 23:45:23 -0700167 *
168 * This implements the rules in the "Conversion and Scalar Constructors"
169 * section (GLSL 1.10 section 5.4.1), not the "Implicit Conversions" rules.
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700170 */
171static ir_rvalue *
172convert_component(ir_rvalue *src, const glsl_type *desired_type)
173{
Carl Worth1660a292010-06-23 18:11:51 -0700174 void *ctx = talloc_parent(src);
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700175 const unsigned a = desired_type->base_type;
176 const unsigned b = src->type->base_type;
Ian Romanick00eb4662010-06-07 15:08:04 -0700177 ir_expression *result = NULL;
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700178
179 if (src->type->is_error())
180 return src;
181
182 assert(a <= GLSL_TYPE_BOOL);
183 assert(b <= GLSL_TYPE_BOOL);
184
185 if ((a == b) || (src->type->is_integer() && desired_type->is_integer()))
186 return src;
187
188 switch (a) {
189 case GLSL_TYPE_UINT:
190 case GLSL_TYPE_INT:
191 if (b == GLSL_TYPE_FLOAT)
Carl Worth1660a292010-06-23 18:11:51 -0700192 result = new(ctx) ir_expression(ir_unop_f2i, desired_type, src, NULL);
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700193 else {
194 assert(b == GLSL_TYPE_BOOL);
Carl Worth1660a292010-06-23 18:11:51 -0700195 result = new(ctx) ir_expression(ir_unop_b2i, desired_type, src, NULL);
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700196 }
Ian Romanick565185c2010-06-11 13:49:00 -0700197 break;
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700198 case GLSL_TYPE_FLOAT:
199 switch (b) {
200 case GLSL_TYPE_UINT:
Carl Worth1660a292010-06-23 18:11:51 -0700201 result = new(ctx) ir_expression(ir_unop_u2f, desired_type, src, NULL);
Ian Romanick00eb4662010-06-07 15:08:04 -0700202 break;
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700203 case GLSL_TYPE_INT:
Carl Worth1660a292010-06-23 18:11:51 -0700204 result = new(ctx) ir_expression(ir_unop_i2f, desired_type, src, NULL);
Ian Romanick00eb4662010-06-07 15:08:04 -0700205 break;
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700206 case GLSL_TYPE_BOOL:
Carl Worth1660a292010-06-23 18:11:51 -0700207 result = new(ctx) ir_expression(ir_unop_b2f, desired_type, src, NULL);
Ian Romanick00eb4662010-06-07 15:08:04 -0700208 break;
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700209 }
210 break;
Ian Romanick26b5d332010-06-25 16:19:45 -0700211 case GLSL_TYPE_BOOL:
Ian Romanickb74b43e2010-06-11 16:52:09 -0700212 switch (b) {
Ian Romanick26b5d332010-06-25 16:19:45 -0700213 case GLSL_TYPE_UINT:
214 case GLSL_TYPE_INT:
215 result = new(ctx) ir_expression(ir_unop_i2b, desired_type, src, NULL);
216 break;
217 case GLSL_TYPE_FLOAT:
218 result = new(ctx) ir_expression(ir_unop_f2b, desired_type, src, NULL);
219 break;
Ian Romanickb74b43e2010-06-11 16:52:09 -0700220 }
Ian Romanick26b5d332010-06-25 16:19:45 -0700221 break;
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700222 }
223
Ian Romanick00eb4662010-06-07 15:08:04 -0700224 assert(result != NULL);
225
Kenneth Graunke0048c7a2010-07-19 23:45:23 -0700226 /* Try constant folding; it may fold in the conversion we just added. */
Ian Romanick00eb4662010-06-07 15:08:04 -0700227 ir_constant *const constant = result->constant_expression_value();
228 return (constant != NULL) ? (ir_rvalue *) constant : (ir_rvalue *) result;
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700229}
230
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700231/**
232 * Dereference a specific component from a scalar, vector, or matrix
233 */
234static ir_rvalue *
235dereference_component(ir_rvalue *src, unsigned component)
236{
Carl Worth1660a292010-06-23 18:11:51 -0700237 void *ctx = talloc_parent(src);
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700238 assert(component < src->type->components());
239
Ian Romanickc9cb1032010-06-04 16:20:35 -0700240 /* If the source is a constant, just create a new constant instead of a
241 * dereference of the existing constant.
242 */
243 ir_constant *constant = src->as_constant();
244 if (constant)
Carl Worth1660a292010-06-23 18:11:51 -0700245 return new(ctx) ir_constant(constant, component);
Ian Romanickc9cb1032010-06-04 16:20:35 -0700246
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700247 if (src->type->is_scalar()) {
248 return src;
249 } else if (src->type->is_vector()) {
Carl Worth1660a292010-06-23 18:11:51 -0700250 return new(ctx) ir_swizzle(src, component, 0, 0, 0, 1);
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700251 } else {
252 assert(src->type->is_matrix());
253
254 /* Dereference a row of the matrix, then call this function again to get
255 * a specific element from that row.
256 */
257 const int c = component / src->type->column_type()->vector_elements;
258 const int r = component % src->type->column_type()->vector_elements;
Carl Worth1660a292010-06-23 18:11:51 -0700259 ir_constant *const col_index = new(ctx) ir_constant(c);
260 ir_dereference *const col = new(ctx) ir_dereference_array(src, col_index);
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700261
262 col->type = src->type->column_type();
263
264 return dereference_component(col, r);
265 }
266
267 assert(!"Should not get here.");
268 return NULL;
269}
270
271
Ian Romanick00aa1732010-03-31 16:48:48 -0700272static ir_rvalue *
273process_array_constructor(exec_list *instructions,
274 const glsl_type *constructor_type,
Ian Romanick304ea902010-05-10 11:17:53 -0700275 YYLTYPE *loc, exec_list *parameters,
Ian Romanick00aa1732010-03-31 16:48:48 -0700276 struct _mesa_glsl_parse_state *state)
277{
Kenneth Graunke953ff122010-06-25 13:14:37 -0700278 void *ctx = state;
Ian Romanick00aa1732010-03-31 16:48:48 -0700279 /* Array constructors come in two forms: sized and unsized. Sized array
280 * constructors look like 'vec4[2](a, b)', where 'a' and 'b' are vec4
281 * variables. In this case the number of parameters must exactly match the
282 * specified size of the array.
283 *
284 * Unsized array constructors look like 'vec4[](a, b)', where 'a' and 'b'
285 * are vec4 variables. In this case the size of the array being constructed
286 * is determined by the number of parameters.
287 *
288 * From page 52 (page 58 of the PDF) of the GLSL 1.50 spec:
289 *
290 * "There must be exactly the same number of arguments as the size of
291 * the array being constructed. If no size is present in the
292 * constructor, then the array is explicitly sized to the number of
293 * arguments provided. The arguments are assigned in order, starting at
294 * element 0, to the elements of the constructed array. Each argument
295 * must be the same type as the element type of the array, or be a type
296 * that can be converted to the element type of the array according to
297 * Section 4.1.10 "Implicit Conversions.""
298 */
299 exec_list actual_parameters;
300 const unsigned parameter_count =
301 process_parameters(instructions, &actual_parameters, parameters, state);
302
303 if ((parameter_count == 0)
304 || ((constructor_type->length != 0)
305 && (constructor_type->length != parameter_count))) {
306 const unsigned min_param = (constructor_type->length == 0)
307 ? 1 : constructor_type->length;
308
309 _mesa_glsl_error(loc, state, "array constructor must have %s %u "
310 "parameter%s",
311 (constructor_type->length != 0) ? "at least" : "exactly",
312 min_param, (min_param <= 1) ? "" : "s");
Carl Worthe01193a2010-06-23 18:25:04 -0700313 return ir_call::get_error_instruction(ctx);
Ian Romanick00aa1732010-03-31 16:48:48 -0700314 }
315
316 if (constructor_type->length == 0) {
317 constructor_type =
Ian Romanickf38d15b2010-07-20 15:33:40 -0700318 glsl_type::get_array_instance(constructor_type->element_type(),
Ian Romanick00aa1732010-03-31 16:48:48 -0700319 parameter_count);
320 assert(constructor_type != NULL);
321 assert(constructor_type->length == parameter_count);
322 }
323
Kenneth Graunke13a19742010-07-19 23:49:58 -0700324 bool all_parameters_are_constant = true;
Ian Romanick00aa1732010-03-31 16:48:48 -0700325
Kenneth Graunke13a19742010-07-19 23:49:58 -0700326 /* Type cast each parameter and, if possible, fold constants. */
327 foreach_list_safe(n, &actual_parameters) {
328 ir_rvalue *ir = (ir_rvalue *) n;
329 ir_rvalue *result = ir;
330
331 /* Apply implicit conversions (not the scalar constructor rules!) */
332 if (constructor_type->element_type()->is_float()) {
333 const glsl_type *desired_type =
334 glsl_type::get_instance(GLSL_TYPE_FLOAT,
335 ir->type->vector_elements,
336 ir->type->matrix_columns);
337 result = convert_component(ir, desired_type);
338 }
339
340 if (result->type != constructor_type->element_type()) {
341 _mesa_glsl_error(loc, state, "type error in array constructor: "
342 "expected: %s, found %s",
343 constructor_type->element_type()->name,
344 result->type->name);
345 }
346
347 /* Attempt to convert the parameter to a constant valued expression.
348 * After doing so, track whether or not all the parameters to the
349 * constructor are trivially constant valued expressions.
350 */
351 ir_rvalue *const constant = result->constant_expression_value();
352
353 if (constant != NULL)
354 result = constant;
355 else
356 all_parameters_are_constant = false;
357
358 ir->replace_with(result);
Ian Romanick00aa1732010-03-31 16:48:48 -0700359 }
360
Kenneth Graunke74e18022010-07-20 01:06:33 -0700361 if (all_parameters_are_constant)
362 return new(ctx) ir_constant(constructor_type, &actual_parameters);
Ian Romanick00aa1732010-03-31 16:48:48 -0700363
Kenneth Graunke13a19742010-07-19 23:49:58 -0700364 ir_variable *var = new(ctx) ir_variable(constructor_type, "array_ctor",
365 ir_var_temporary);
366 instructions->push_tail(var);
Ian Romanick00aa1732010-03-31 16:48:48 -0700367
Kenneth Graunke13a19742010-07-19 23:49:58 -0700368 int i = 0;
369 foreach_list(node, &actual_parameters) {
370 ir_rvalue *rhs = (ir_rvalue *) node;
371 ir_rvalue *lhs = new(ctx) ir_dereference_array(var,
372 new(ctx) ir_constant(i));
373
374 ir_instruction *assignment = new(ctx) ir_assignment(lhs, rhs, NULL);
375 instructions->push_tail(assignment);
376
377 i++;
378 }
379
380 return new(ctx) ir_dereference_variable(var);
Ian Romanick00aa1732010-03-31 16:48:48 -0700381}
382
383
Ian Romanickab92d0e2010-06-09 17:26:20 -0700384/**
385 * Try to convert a record constructor to a constant expression
386 */
387static ir_constant *
388constant_record_constructor(const glsl_type *constructor_type,
389 YYLTYPE *loc, exec_list *parameters,
390 struct _mesa_glsl_parse_state *state)
391{
Kenneth Graunke953ff122010-06-25 13:14:37 -0700392 void *ctx = state;
Ian Romanickab92d0e2010-06-09 17:26:20 -0700393 bool all_parameters_are_constant = true;
394
395 exec_node *node = parameters->head;
396 for (unsigned i = 0; i < constructor_type->length; i++) {
397 ir_instruction *ir = (ir_instruction *) node;
398
399 if (node->is_tail_sentinal()) {
400 _mesa_glsl_error(loc, state,
401 "insufficient parameters to constructor for `%s'",
402 constructor_type->name);
403 return NULL;
404 }
405
406 if (ir->type != constructor_type->fields.structure[i].type) {
407 _mesa_glsl_error(loc, state,
408 "parameter type mismatch in constructor for `%s' "
409 " (%s vs %s)",
410 constructor_type->name,
411 ir->type->name,
412 constructor_type->fields.structure[i].type->name);
413 return NULL;
414 }
415
416 if (ir->as_constant() == NULL)
417 all_parameters_are_constant = false;
418
419 node = node->next;
420 }
421
422 if (!all_parameters_are_constant)
423 return NULL;
424
Carl Worth1660a292010-06-23 18:11:51 -0700425 return new(ctx) ir_constant(constructor_type, parameters);
Ian Romanickab92d0e2010-06-09 17:26:20 -0700426}
427
428
Ian Romanickbe1d2bf2010-06-11 14:01:44 -0700429/**
430 * Generate data for a constant matrix constructor w/a single scalar parameter
431 *
432 * Matrix constructors in GLSL can be passed a single scalar of the
433 * approriate type. In these cases, the resulting matrix is the identity
434 * matrix multipled by the specified scalar. This function generates data for
435 * that matrix.
436 *
437 * \param type Type of the desired matrix.
438 * \param initializer Scalar value used to initialize the matrix diagonal.
439 * \param data Location to store the resulting matrix.
440 */
441void
442generate_constructor_matrix(const glsl_type *type, ir_constant *initializer,
443 ir_constant_data *data)
444{
445 switch (type->base_type) {
446 case GLSL_TYPE_UINT:
447 case GLSL_TYPE_INT:
448 for (unsigned i = 0; i < type->components(); i++)
449 data->u[i] = 0;
450
451 for (unsigned i = 0; i < type->matrix_columns; i++) {
452 /* The array offset of the ith row and column of the matrix.
453 */
454 const unsigned idx = (i * type->vector_elements) + i;
455
456 data->u[idx] = 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] = 0;
463
464 for (unsigned i = 0; i < type->matrix_columns; i++) {
465 /* The array offset of the ith row and column of the matrix.
466 */
467 const unsigned idx = (i * type->vector_elements) + i;
468
469 data->f[idx] = initializer->value.f[0];
470 }
471
472 break;
473
474 default:
475 assert(!"Should not get here.");
476 break;
477 }
478}
479
480
481/**
482 * Generate data for a constant vector constructor w/a single scalar parameter
483 *
484 * Vector constructors in GLSL can be passed a single scalar of the
485 * approriate type. In these cases, the resulting vector contains the specified
486 * value in all components. This function generates data for that vector.
487 *
488 * \param type Type of the desired vector.
489 * \param initializer Scalar value used to initialize the vector.
490 * \param data Location to store the resulting vector data.
491 */
492void
493generate_constructor_vector(const glsl_type *type, ir_constant *initializer,
494 ir_constant_data *data)
495{
496 switch (type->base_type) {
497 case GLSL_TYPE_UINT:
498 case GLSL_TYPE_INT:
499 for (unsigned i = 0; i < type->components(); i++)
500 data->u[i] = initializer->value.u[0];
501
502 break;
503
504 case GLSL_TYPE_FLOAT:
505 for (unsigned i = 0; i < type->components(); i++)
506 data->f[i] = initializer->value.f[0];
507
508 break;
509
510 case GLSL_TYPE_BOOL:
511 for (unsigned i = 0; i < type->components(); i++)
512 data->b[i] = initializer->value.b[0];
513
514 break;
515
516 default:
517 assert(!"Should not get here.");
518 break;
519 }
520}
521
522
Ian Romanickc31dcdf2010-06-23 15:19:40 -0700523/**
524 * Determine if a list consists of a single scalar r-value
525 */
526bool
527single_scalar_parameter(exec_list *parameters)
528{
529 const ir_rvalue *const p = (ir_rvalue *) parameters->head;
530 assert(((ir_rvalue *)p)->as_rvalue() != NULL);
531
532 return (p->type->is_scalar() && p->next->is_tail_sentinal());
533}
534
535
536/**
537 * Generate inline code for a vector constructor
538 *
539 * The generated constructor code will consist of a temporary variable
540 * declaration of the same type as the constructor. A sequence of assignments
541 * from constructor parameters to the temporary will follow.
542 *
543 * \return
544 * An \c ir_dereference_variable of the temprorary generated in the constructor
545 * body.
546 */
547ir_rvalue *
548emit_inline_vector_constructor(const glsl_type *type,
549 exec_list *instructions,
550 exec_list *parameters,
551 void *ctx)
552{
553 assert(!parameters->is_empty());
554
Ian Romanick4b6feb02010-06-28 13:22:55 -0700555 ir_variable *var = new(ctx) ir_variable(type,
Ian Romanick7e2aa912010-07-19 17:12:42 -0700556 talloc_strdup(ctx, "vec_ctor"),
557 ir_var_temporary);
Ian Romanickc31dcdf2010-06-23 15:19:40 -0700558 instructions->push_tail(var);
559
560 /* There are two kinds of vector constructors.
561 *
562 * - Construct a vector from a single scalar by replicating that scalar to
563 * all components of the vector.
564 *
565 * - Construct a vector from an arbirary combination of vectors and
566 * scalars. The components of the constructor parameters are assigned
567 * to the vector in order until the vector is full.
568 */
569 const unsigned lhs_components = type->components();
570 if (single_scalar_parameter(parameters)) {
571 ir_rvalue *first_param = (ir_rvalue *)parameters->head;
572 ir_rvalue *rhs = new(ctx) ir_swizzle(first_param, 0, 0, 0, 0,
573 lhs_components);
574 ir_dereference_variable *lhs = new(ctx) ir_dereference_variable(var);
575
576 assert(rhs->type == lhs->type);
577
578 ir_instruction *inst = new(ctx) ir_assignment(lhs, rhs, NULL);
579 instructions->push_tail(inst);
580 } else {
581 unsigned base_component = 0;
582 foreach_list(node, parameters) {
583 ir_rvalue *rhs = (ir_rvalue *) node;
584 unsigned rhs_components = rhs->type->components();
585
586 /* Do not try to assign more components to the vector than it has!
587 */
588 if ((rhs_components + base_component) > lhs_components) {
589 rhs_components = lhs_components - base_component;
590 }
591
592 /* Emit an assignment of the constructor parameter to the next set of
593 * components in the temporary variable.
594 */
595 unsigned mask[4] = { 0, 0, 0, 0 };
596 for (unsigned i = 0; i < rhs_components; i++) {
597 mask[i] = i + base_component;
598 }
599
600
601 ir_rvalue *lhs_ref = new(ctx) ir_dereference_variable(var);
602 ir_swizzle *lhs = new(ctx) ir_swizzle(lhs_ref, mask, rhs_components);
603
604 ir_instruction *inst = new(ctx) ir_assignment(lhs, rhs, NULL);
605 instructions->push_tail(inst);
606
607 /* Advance the component index by the number of components that were
608 * just assigned.
609 */
610 base_component += rhs_components;
611 }
612 }
613 return new(ctx) ir_dereference_variable(var);
614}
615
616
Ian Romanick81c7e942010-06-25 16:10:43 -0700617/**
618 * Generate assignment of a portion of a vector to a portion of a matrix column
619 *
620 * \param src_base First component of the source to be used in assignment
621 * \param column Column of destination to be assiged
622 * \param row_base First component of the destination column to be assigned
623 * \param count Number of components to be assigned
624 *
625 * \note
626 * \c src_base + \c count must be less than or equal to the number of components
627 * in the source vector.
628 */
629ir_instruction *
630assign_to_matrix_column(ir_variable *var, unsigned column, unsigned row_base,
631 ir_rvalue *src, unsigned src_base, unsigned count,
632 TALLOC_CTX *ctx)
633{
634 const unsigned mask[8] = { 0, 1, 2, 3, 0, 0, 0, 0 };
635
636 ir_constant *col_idx = new(ctx) ir_constant(column);
637 ir_rvalue *column_ref = new(ctx) ir_dereference_array(var, col_idx);
638
639 assert(column_ref->type->components() >= (row_base + count));
640 ir_rvalue *lhs = new(ctx) ir_swizzle(column_ref, &mask[row_base], count);
641
642 assert(src->type->components() >= (src_base + count));
643 ir_rvalue *rhs = new(ctx) ir_swizzle(src, &mask[src_base], count);
644
645 return new(ctx) ir_assignment(lhs, rhs, NULL);
646}
647
648
649/**
650 * Generate inline code for a matrix constructor
651 *
652 * The generated constructor code will consist of a temporary variable
653 * declaration of the same type as the constructor. A sequence of assignments
654 * from constructor parameters to the temporary will follow.
655 *
656 * \return
657 * An \c ir_dereference_variable of the temprorary generated in the constructor
658 * body.
659 */
660ir_rvalue *
661emit_inline_matrix_constructor(const glsl_type *type,
662 exec_list *instructions,
663 exec_list *parameters,
664 void *ctx)
665{
666 assert(!parameters->is_empty());
667
Ian Romanick4b6feb02010-06-28 13:22:55 -0700668 ir_variable *var = new(ctx) ir_variable(type,
Ian Romanick7e2aa912010-07-19 17:12:42 -0700669 talloc_strdup(ctx, "mat_ctor"),
670 ir_var_temporary);
Ian Romanick81c7e942010-06-25 16:10:43 -0700671 instructions->push_tail(var);
672
673 /* There are three kinds of matrix constructors.
674 *
675 * - Construct a matrix from a single scalar by replicating that scalar to
676 * along the diagonal of the matrix and setting all other components to
677 * zero.
678 *
679 * - Construct a matrix from an arbirary combination of vectors and
680 * scalars. The components of the constructor parameters are assigned
681 * to the matrix in colum-major order until the matrix is full.
682 *
683 * - Construct a matrix from a single matrix. The source matrix is copied
684 * to the upper left portion of the constructed matrix, and the remaining
685 * elements take values from the identity matrix.
686 */
687 ir_rvalue *const first_param = (ir_rvalue *) parameters->head;
688 if (single_scalar_parameter(parameters)) {
689 /* Assign the scalar to the X component of a vec4, and fill the remaining
690 * components with zero.
691 */
Ian Romanick4b6feb02010-06-28 13:22:55 -0700692 ir_variable *rhs_var =
693 new(ctx) ir_variable(glsl_type::vec4_type,
Ian Romanick7e2aa912010-07-19 17:12:42 -0700694 talloc_strdup(ctx, "mat_ctor_vec"),
695 ir_var_temporary);
Ian Romanick81c7e942010-06-25 16:10:43 -0700696 instructions->push_tail(rhs_var);
697
698 ir_constant_data zero;
699 zero.f[0] = 0.0;
700 zero.f[1] = 0.0;
701 zero.f[2] = 0.0;
702 zero.f[3] = 0.0;
703
704 ir_instruction *inst =
705 new(ctx) ir_assignment(new(ctx) ir_dereference_variable(rhs_var),
706 new(ctx) ir_constant(rhs_var->type, &zero),
707 NULL);
708 instructions->push_tail(inst);
709
710 ir_rvalue *const rhs_ref = new(ctx) ir_dereference_variable(rhs_var);
711 ir_rvalue *const x_of_rhs = new(ctx) ir_swizzle(rhs_ref, 0, 0, 0, 0, 1);
712
713 inst = new(ctx) ir_assignment(x_of_rhs, first_param, NULL);
714 instructions->push_tail(inst);
715
716 /* Assign the temporary vector to each column of the destination matrix
717 * with a swizzle that puts the X component on the diagonal of the
718 * matrix. In some cases this may mean that the X component does not
719 * get assigned into the column at all (i.e., when the matrix has more
720 * columns than rows).
721 */
722 static const unsigned rhs_swiz[4][4] = {
723 { 0, 1, 1, 1 },
724 { 1, 0, 1, 1 },
725 { 1, 1, 0, 1 },
726 { 1, 1, 1, 0 }
727 };
728
729 const unsigned cols_to_init = min(type->matrix_columns,
730 type->vector_elements);
731 for (unsigned i = 0; i < cols_to_init; i++) {
732 ir_constant *const col_idx = new(ctx) ir_constant(i);
733 ir_rvalue *const col_ref = new(ctx) ir_dereference_array(var, col_idx);
734
735 ir_rvalue *const rhs_ref = new(ctx) ir_dereference_variable(rhs_var);
736 ir_rvalue *const rhs = new(ctx) ir_swizzle(rhs_ref, rhs_swiz[i],
737 type->vector_elements);
738
739 inst = new(ctx) ir_assignment(col_ref, rhs, NULL);
740 instructions->push_tail(inst);
741 }
742
743 for (unsigned i = cols_to_init; i < type->matrix_columns; i++) {
744 ir_constant *const col_idx = new(ctx) ir_constant(i);
745 ir_rvalue *const col_ref = new(ctx) ir_dereference_array(var, col_idx);
746
747 ir_rvalue *const rhs_ref = new(ctx) ir_dereference_variable(rhs_var);
748 ir_rvalue *const rhs = new(ctx) ir_swizzle(rhs_ref, 1, 1, 1, 1,
749 type->vector_elements);
750
751 inst = new(ctx) ir_assignment(col_ref, rhs, NULL);
752 instructions->push_tail(inst);
753 }
754 } else if (first_param->type->is_matrix()) {
755 /* From page 50 (56 of the PDF) of the GLSL 1.50 spec:
756 *
757 * "If a matrix is constructed from a matrix, then each component
758 * (column i, row j) in the result that has a corresponding
759 * component (column i, row j) in the argument will be initialized
760 * from there. All other components will be initialized to the
761 * identity matrix. If a matrix argument is given to a matrix
762 * constructor, it is an error to have any other arguments."
763 */
764 assert(first_param->next->is_tail_sentinal());
765 ir_rvalue *const src_matrix = first_param;
766
767 /* If the source matrix is smaller, pre-initialize the relavent parts of
768 * the destination matrix to the identity matrix.
769 */
770 if ((src_matrix->type->matrix_columns < var->type->matrix_columns)
771 || (src_matrix->type->vector_elements < var->type->vector_elements)) {
772
773 /* If the source matrix has fewer rows, every column of the destination
774 * must be initialized. Otherwise only the columns in the destination
775 * that do not exist in the source must be initialized.
776 */
777 unsigned col =
778 (src_matrix->type->vector_elements < var->type->vector_elements)
779 ? 0 : src_matrix->type->matrix_columns;
780
781 const glsl_type *const col_type = var->type->column_type();
782 for (/* empty */; col < var->type->matrix_columns; col++) {
783 ir_constant_data ident;
784
785 ident.f[0] = 0.0;
786 ident.f[1] = 0.0;
787 ident.f[2] = 0.0;
788 ident.f[3] = 0.0;
789
790 ident.f[col] = 1.0;
791
792 ir_rvalue *const rhs = new(ctx) ir_constant(col_type, &ident);
793
794 ir_rvalue *const lhs =
795 new(ctx) ir_dereference_array(var, new(ctx) ir_constant(col));
796
797 ir_instruction *inst = new(ctx) ir_assignment(lhs, rhs, NULL);
798 instructions->push_tail(inst);
799 }
800 }
801
802 /* Assign columns from the source matrix to the destination matrix.
803 *
804 * Since the parameter will be used in the RHS of multiple assignments,
805 * generate a temporary and copy the paramter there.
806 */
Ian Romanick4b6feb02010-06-28 13:22:55 -0700807 ir_variable *const rhs_var =
808 new(ctx) ir_variable(first_param->type,
Ian Romanick7e2aa912010-07-19 17:12:42 -0700809 talloc_strdup(ctx, "mat_ctor_mat"),
810 ir_var_temporary);
Ian Romanick81c7e942010-06-25 16:10:43 -0700811 instructions->push_tail(rhs_var);
812
813 ir_dereference *const rhs_var_ref =
814 new(ctx) ir_dereference_variable(rhs_var);
815 ir_instruction *const inst =
816 new(ctx) ir_assignment(rhs_var_ref, first_param, NULL);
817 instructions->push_tail(inst);
818
819
820 const unsigned swiz[4] = { 0, 1, 2, 3 };
821 const unsigned last_col = min(src_matrix->type->matrix_columns,
822 var->type->matrix_columns);
823 for (unsigned i = 0; i < last_col; i++) {
824 ir_rvalue *const lhs_col =
825 new(ctx) ir_dereference_array(var, new(ctx) ir_constant(i));
826 ir_rvalue *const rhs_col =
827 new(ctx) ir_dereference_array(rhs_var, new(ctx) ir_constant(i));
828
829 /* If one matrix has columns that are smaller than the columns of the
830 * other matrix, wrap the column access of the larger with a swizzle
831 * so that the LHS and RHS of the assignment have the same size (and
832 * therefore have the same type).
833 *
834 * It would be perfectly valid to unconditionally generate the
835 * swizzles, this this will typically result in a more compact IR tree.
836 */
837 ir_rvalue *lhs;
838 ir_rvalue *rhs;
839 if (lhs_col->type->vector_elements < rhs_col->type->vector_elements) {
840 lhs = lhs_col;
841
842 rhs = new(ctx) ir_swizzle(rhs_col, swiz,
843 lhs_col->type->vector_elements);
844 } else if (lhs_col->type->vector_elements
845 > rhs_col->type->vector_elements) {
846 lhs = new(ctx) ir_swizzle(lhs_col, swiz,
847 rhs_col->type->vector_elements);
848 rhs = rhs_col;
849 } else {
850 lhs = lhs_col;
851 rhs = rhs_col;
852 }
853
854 assert(lhs->type == rhs->type);
855
856 ir_instruction *inst = new(ctx) ir_assignment(lhs, rhs, NULL);
857 instructions->push_tail(inst);
858 }
859 } else {
860 const unsigned rows = type->matrix_columns;
861 const unsigned cols = type->vector_elements;
862 unsigned col_idx = 0;
863 unsigned row_idx = 0;
864
865 foreach_list (node, parameters) {
866 ir_rvalue *const rhs = (ir_rvalue *) node;
867 const unsigned components_remaining_this_column = rows - row_idx;
868 unsigned rhs_components = rhs->type->components();
869 unsigned rhs_base = 0;
870
871 /* Since the parameter might be used in the RHS of two assignments,
872 * generate a temporary and copy the paramter there.
873 */
Ian Romanick4b6feb02010-06-28 13:22:55 -0700874 ir_variable *rhs_var =
875 new(ctx) ir_variable(rhs->type,
Ian Romanick7e2aa912010-07-19 17:12:42 -0700876 talloc_strdup(ctx, "mat_ctor_vec"),
877 ir_var_temporary);
Ian Romanick81c7e942010-06-25 16:10:43 -0700878 instructions->push_tail(rhs_var);
879
880 ir_dereference *rhs_var_ref =
881 new(ctx) ir_dereference_variable(rhs_var);
882 ir_instruction *inst = new(ctx) ir_assignment(rhs_var_ref, rhs, NULL);
883 instructions->push_tail(inst);
884
885 /* Assign the current parameter to as many components of the matrix
886 * as it will fill.
887 *
888 * NOTE: A single vector parameter can span two matrix columns. A
889 * single vec4, for example, can completely fill a mat2.
890 */
891 if (rhs_components >= components_remaining_this_column) {
892 const unsigned count = min(rhs_components,
893 components_remaining_this_column);
894
895 rhs_var_ref = new(ctx) ir_dereference_variable(rhs_var);
896
897 ir_instruction *inst = assign_to_matrix_column(var, col_idx,
898 row_idx,
899 rhs_var_ref, 0,
900 count, ctx);
901 instructions->push_tail(inst);
902
903 rhs_base = count;
904
905 col_idx++;
906 row_idx = 0;
907 }
908
909 /* If there is data left in the parameter and components left to be
910 * set in the destination, emit another assignment. It is possible
911 * that the assignment could be of a vec4 to the last element of the
912 * matrix. In this case col_idx==cols, but there is still data
913 * left in the source parameter. Obviously, don't emit an assignment
914 * to data outside the destination matrix.
915 */
916 if ((col_idx < cols) && (rhs_base < rhs_components)) {
917 const unsigned count = rhs_components - rhs_base;
918
919 rhs_var_ref = new(ctx) ir_dereference_variable(rhs_var);
920
921 ir_instruction *inst = assign_to_matrix_column(var, col_idx,
922 row_idx,
923 rhs_var_ref,
924 rhs_base,
925 count, ctx);
926 instructions->push_tail(inst);
927
928 row_idx += count;
929 }
930 }
931 }
932
933 return new(ctx) ir_dereference_variable(var);
934}
935
936
Kenneth Graunkefb9fb5f2010-03-26 00:25:36 -0700937ir_rvalue *
Ian Romanick548fa292010-03-15 13:04:13 -0700938ast_function_expression::hir(exec_list *instructions,
939 struct _mesa_glsl_parse_state *state)
940{
Kenneth Graunke953ff122010-06-25 13:14:37 -0700941 void *ctx = state;
Ian Romanick548fa292010-03-15 13:04:13 -0700942 /* There are three sorts of function calls.
943 *
Kenneth Graunkef58bbd12010-07-08 18:03:28 -0700944 * 1. constructors - The first subexpression is an ast_type_specifier.
Ian Romanick548fa292010-03-15 13:04:13 -0700945 * 2. methods - Only the .length() method of array types.
946 * 3. functions - Calls to regular old functions.
947 *
Ian Romanick548fa292010-03-15 13:04:13 -0700948 * Method calls are actually detected when the ast_field_selection
949 * expression is handled.
950 */
951 if (is_constructor()) {
Ian Romanickabef9552010-03-23 15:08:30 -0700952 const ast_type_specifier *type = (ast_type_specifier *) subexpressions[0];
953 YYLTYPE loc = type->get_location();
Ian Romanick3e0ef5f2010-03-31 16:22:56 -0700954 const char *name;
Ian Romanickabef9552010-03-23 15:08:30 -0700955
Ian Romanick3e0ef5f2010-03-31 16:22:56 -0700956 const glsl_type *const constructor_type = type->glsl_type(& name, state);
Ian Romanickabef9552010-03-23 15:08:30 -0700957
958
959 /* Constructors for samplers are illegal.
960 */
961 if (constructor_type->is_sampler()) {
962 _mesa_glsl_error(& loc, state, "cannot construct sampler type `%s'",
963 constructor_type->name);
Carl Worthe01193a2010-06-23 18:25:04 -0700964 return ir_call::get_error_instruction(ctx);
Ian Romanickabef9552010-03-23 15:08:30 -0700965 }
966
Ian Romanickb6326ab2010-03-31 16:25:21 -0700967 if (constructor_type->is_array()) {
968 if (state->language_version <= 110) {
969 _mesa_glsl_error(& loc, state,
970 "array constructors forbidden in GLSL 1.10");
Carl Worthe01193a2010-06-23 18:25:04 -0700971 return ir_call::get_error_instruction(ctx);
Ian Romanickb6326ab2010-03-31 16:25:21 -0700972 }
973
Ian Romanick00aa1732010-03-31 16:48:48 -0700974 return process_array_constructor(instructions, constructor_type,
Ian Romanick3521f0b2010-05-10 10:47:14 -0700975 & loc, &this->expressions, state);
Ian Romanickb6326ab2010-03-31 16:25:21 -0700976 }
Ian Romanickabef9552010-03-23 15:08:30 -0700977
978 /* There are two kinds of constructor call. Constructors for built-in
979 * language types, such as mat4 and vec2, are free form. The only
980 * requirement is that the parameters must provide enough values of the
981 * correct scalar type. Constructors for arrays and structures must
982 * have the exact number of parameters with matching types in the
983 * correct order. These constructors follow essentially the same type
984 * matching rules as functions.
985 */
Kenneth Graunkef58bbd12010-07-08 18:03:28 -0700986 if (!constructor_type->is_numeric() && !constructor_type->is_boolean())
987 return ir_call::get_error_instruction(ctx);
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700988
Kenneth Graunkef58bbd12010-07-08 18:03:28 -0700989 /* Total number of components of the type being constructed. */
990 const unsigned type_components = constructor_type->components();
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700991
Kenneth Graunkef58bbd12010-07-08 18:03:28 -0700992 /* Number of components from parameters that have actually been
993 * consumed. This is used to perform several kinds of error checking.
994 */
995 unsigned components_used = 0;
Ian Romanick0b7dcc82010-03-26 17:38:58 -0700996
Kenneth Graunkef58bbd12010-07-08 18:03:28 -0700997 unsigned matrix_parameters = 0;
998 unsigned nonmatrix_parameters = 0;
999 exec_list actual_parameters;
Ian Romanick9e08d012010-06-04 16:36:09 -07001000
Kenneth Graunkef58bbd12010-07-08 18:03:28 -07001001 foreach_list (n, &this->expressions) {
1002 ast_node *ast = exec_node_data(ast_node, n, link);
1003 ir_rvalue *result = ast->hir(instructions, state)->as_rvalue();
Ian Romanick0b7dcc82010-03-26 17:38:58 -07001004
1005 /* From page 50 (page 56 of the PDF) of the GLSL 1.50 spec:
1006 *
Kenneth Graunkef58bbd12010-07-08 18:03:28 -07001007 * "It is an error to provide extra arguments beyond this
1008 * last used argument."
Ian Romanick0b7dcc82010-03-26 17:38:58 -07001009 */
Kenneth Graunkef58bbd12010-07-08 18:03:28 -07001010 if (components_used >= type_components) {
1011 _mesa_glsl_error(& loc, state, "too many parameters to `%s' "
1012 "constructor",
Ian Romanick0b7dcc82010-03-26 17:38:58 -07001013 constructor_type->name);
Carl Worthe01193a2010-06-23 18:25:04 -07001014 return ir_call::get_error_instruction(ctx);
Ian Romanick0b7dcc82010-03-26 17:38:58 -07001015 }
1016
Kenneth Graunkef58bbd12010-07-08 18:03:28 -07001017 if (!result->type->is_numeric() && !result->type->is_boolean()) {
1018 _mesa_glsl_error(& loc, state, "cannot construct `%s' from a "
1019 "non-numeric data type",
Ian Romanick0b7dcc82010-03-26 17:38:58 -07001020 constructor_type->name);
Carl Worthe01193a2010-06-23 18:25:04 -07001021 return ir_call::get_error_instruction(ctx);
Ian Romanick0b7dcc82010-03-26 17:38:58 -07001022 }
1023
Kenneth Graunkef58bbd12010-07-08 18:03:28 -07001024 /* Count the number of matrix and nonmatrix parameters. This
1025 * is used below to enforce some of the constructor rules.
Ian Romanick699b2472010-06-25 17:36:17 -07001026 */
Kenneth Graunkef58bbd12010-07-08 18:03:28 -07001027 if (result->type->is_matrix())
1028 matrix_parameters++;
1029 else
1030 nonmatrix_parameters++;
Ian Romanick699b2472010-06-25 17:36:17 -07001031
Kenneth Graunkef58bbd12010-07-08 18:03:28 -07001032 actual_parameters.push_tail(result);
1033 components_used += result->type->components();
Ian Romanick0b7dcc82010-03-26 17:38:58 -07001034 }
Ian Romanickabef9552010-03-23 15:08:30 -07001035
Kenneth Graunkef58bbd12010-07-08 18:03:28 -07001036 /* From page 28 (page 34 of the PDF) of the GLSL 1.10 spec:
1037 *
1038 * "It is an error to construct matrices from other matrices. This
1039 * is reserved for future use."
1040 */
1041 if ((state->language_version <= 110) && (matrix_parameters > 0)
1042 && constructor_type->is_matrix()) {
1043 _mesa_glsl_error(& loc, state, "cannot construct `%s' from a "
1044 "matrix in GLSL 1.10",
1045 constructor_type->name);
1046 return ir_call::get_error_instruction(ctx);
1047 }
1048
1049 /* From page 50 (page 56 of the PDF) of the GLSL 1.50 spec:
1050 *
1051 * "If a matrix argument is given to a matrix constructor, it is
1052 * an error to have any other arguments."
1053 */
1054 if ((matrix_parameters > 0)
1055 && ((matrix_parameters + nonmatrix_parameters) > 1)
1056 && constructor_type->is_matrix()) {
1057 _mesa_glsl_error(& loc, state, "for matrix `%s' constructor, "
1058 "matrix must be only parameter",
1059 constructor_type->name);
1060 return ir_call::get_error_instruction(ctx);
1061 }
1062
1063 /* From page 28 (page 34 of the PDF) of the GLSL 1.10 spec:
1064 *
1065 * "In these cases, there must be enough components provided in the
1066 * arguments to provide an initializer for every component in the
1067 * constructed value."
1068 */
1069 if ((components_used < type_components) && (components_used != 1)) {
1070 _mesa_glsl_error(& loc, state, "too few components to construct "
1071 "`%s'",
1072 constructor_type->name);
1073 return ir_call::get_error_instruction(ctx);
1074 }
1075
Kenneth Graunke284d8212010-07-08 18:15:32 -07001076 /* Later, we cast each parameter to the same base type as the
1077 * constructor. Since there are no non-floating point matrices, we
1078 * need to break them up into a series of column vectors.
1079 */
1080 if (constructor_type->base_type != GLSL_TYPE_FLOAT) {
1081 foreach_list_safe(n, &actual_parameters) {
1082 ir_rvalue *matrix = (ir_rvalue *) n;
1083
1084 if (!matrix->type->is_matrix())
1085 continue;
1086
1087 /* Create a temporary containing the matrix. */
Ian Romanick7e2aa912010-07-19 17:12:42 -07001088 ir_variable *var = new(ctx) ir_variable(matrix->type, "matrix_tmp",
1089 ir_var_temporary);
Kenneth Graunke284d8212010-07-08 18:15:32 -07001090 instructions->push_tail(var);
1091 instructions->push_tail(new(ctx) ir_assignment(new(ctx)
1092 ir_dereference_variable(var), matrix, NULL));
1093 var->constant_value = matrix->constant_expression_value();
1094
1095 /* Replace the matrix with dereferences of its columns. */
1096 for (int i = 0; i < matrix->type->matrix_columns; i++) {
1097 matrix->insert_before(new (ctx) ir_dereference_array(var,
1098 new(ctx) ir_constant(i)));
1099 }
1100 matrix->remove();
1101 }
1102 }
1103
1104 bool all_parameters_are_constant = true;
1105
1106 /* Type cast each parameter and, if possible, fold constants.*/
1107 foreach_list_safe(n, &actual_parameters) {
1108 ir_rvalue *ir = (ir_rvalue *) n;
1109
1110 const glsl_type *desired_type =
1111 glsl_type::get_instance(constructor_type->base_type,
1112 ir->type->vector_elements,
1113 ir->type->matrix_columns);
1114 ir_rvalue *result = convert_component(ir, desired_type);
1115
1116 /* Attempt to convert the parameter to a constant valued expression.
1117 * After doing so, track whether or not all the parameters to the
1118 * constructor are trivially constant valued expressions.
1119 */
1120 ir_rvalue *const constant = result->constant_expression_value();
1121
1122 if (constant != NULL)
1123 result = constant;
1124 else
1125 all_parameters_are_constant = false;
1126
1127 if (result != ir) {
Kenneth Graunkec7a18da2010-07-19 21:44:03 -07001128 ir->replace_with(result);
Kenneth Graunke284d8212010-07-08 18:15:32 -07001129 }
1130 }
Kenneth Graunkef58bbd12010-07-08 18:03:28 -07001131
1132 /* If all of the parameters are trivially constant, create a
1133 * constant representing the complete collection of parameters.
1134 */
1135 if (all_parameters_are_constant) {
1136 if (components_used >= type_components)
1137 return new(ctx) ir_constant(constructor_type,
1138 & actual_parameters);
1139
1140 /* The above case must handle all scalar constructors.
1141 */
1142 assert(constructor_type->is_vector()
1143 || constructor_type->is_matrix());
1144
1145 /* Constructors with exactly one component are special for
1146 * vectors and matrices. For vectors it causes all elements of
1147 * the vector to be filled with the value. For matrices it
1148 * causes the matrix to be filled with 0 and the diagonal to be
1149 * filled with the value.
1150 */
1151 ir_constant_data data;
1152 ir_constant *const initializer =
1153 (ir_constant *) actual_parameters.head;
1154 if (constructor_type->is_matrix())
1155 generate_constructor_matrix(constructor_type, initializer,
1156 &data);
1157 else
1158 generate_constructor_vector(constructor_type, initializer,
1159 &data);
1160
1161 return new(ctx) ir_constant(constructor_type, &data);
1162 } else if (constructor_type->is_scalar()) {
1163 return dereference_component((ir_rvalue *) actual_parameters.head,
1164 0);
1165 } else if (constructor_type->is_vector()) {
1166 return emit_inline_vector_constructor(constructor_type,
1167 instructions,
1168 &actual_parameters,
1169 ctx);
1170 } else {
1171 assert(constructor_type->is_matrix());
1172 return emit_inline_matrix_constructor(constructor_type,
1173 instructions,
1174 &actual_parameters,
1175 ctx);
1176 }
Ian Romanick548fa292010-03-15 13:04:13 -07001177 } else {
1178 const ast_expression *id = subexpressions[0];
Ian Romanickf4749612010-03-15 13:26:02 -07001179 YYLTYPE loc = id->get_location();
Ian Romanickc0771312010-06-09 17:23:26 -07001180 exec_list actual_parameters;
1181
1182 process_parameters(instructions, &actual_parameters, &this->expressions,
1183 state);
Ian Romanick548fa292010-03-15 13:04:13 -07001184
Ian Romanickab92d0e2010-06-09 17:26:20 -07001185 const glsl_type *const type =
1186 state->symbols->get_type(id->primary_expression.identifier);
1187
1188 if ((type != NULL) && type->is_record()) {
1189 ir_constant *constant =
1190 constant_record_constructor(type, &loc, &actual_parameters, state);
1191
1192 if (constant != NULL)
1193 return constant;
1194 }
1195
Ian Romanickf4749612010-03-15 13:26:02 -07001196 return match_function_by_name(instructions,
1197 id->primary_expression.identifier, & loc,
Ian Romanickc0771312010-06-09 17:23:26 -07001198 &actual_parameters, state);
Ian Romanick548fa292010-03-15 13:04:13 -07001199 }
1200
Carl Worthe01193a2010-06-23 18:25:04 -07001201 return ir_call::get_error_instruction(ctx);
Ian Romanick548fa292010-03-15 13:04:13 -07001202}