[g3dvl] remove some unneeded Makefiles
[mesa.git] / src / glsl / ast_function.cpp
1 /*
2 * Copyright © 2010 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 */
23
24 #include "glsl_symbol_table.h"
25 #include "ast.h"
26 #include "glsl_types.h"
27 #include "ir.h"
28 #include "main/core.h" /* for MIN2 */
29
30 static ir_rvalue *
31 convert_component(ir_rvalue *src, const glsl_type *desired_type);
32
33 bool
34 apply_implicit_conversion(const glsl_type *to, ir_rvalue * &from,
35 struct _mesa_glsl_parse_state *state);
36
37 static unsigned
38 process_parameters(exec_list *instructions, exec_list *actual_parameters,
39 exec_list *parameters,
40 struct _mesa_glsl_parse_state *state)
41 {
42 unsigned count = 0;
43
44 foreach_list (n, parameters) {
45 ast_node *const ast = exec_node_data(ast_node, n, link);
46 ir_rvalue *result = ast->hir(instructions, state);
47
48 ir_constant *const constant = result->constant_expression_value();
49 if (constant != NULL)
50 result = constant;
51
52 actual_parameters->push_tail(result);
53 count++;
54 }
55
56 return count;
57 }
58
59
60 /**
61 * Generate a source prototype for a function signature
62 *
63 * \param return_type Return type of the function. May be \c NULL.
64 * \param name Name of the function.
65 * \param parameters Parameter list for the function. This may be either a
66 * formal or actual parameter list. Only the type is used.
67 *
68 * \return
69 * A ralloced string representing the prototype of the function.
70 */
71 char *
72 prototype_string(const glsl_type *return_type, const char *name,
73 exec_list *parameters)
74 {
75 char *str = NULL;
76
77 if (return_type != NULL)
78 str = ralloc_asprintf(NULL, "%s ", return_type->name);
79
80 ralloc_asprintf_append(&str, "%s(", name);
81
82 const char *comma = "";
83 foreach_list(node, parameters) {
84 const ir_instruction *const param = (ir_instruction *) node;
85
86 ralloc_asprintf_append(&str, "%s%s", comma, param->type->name);
87 comma = ", ";
88 }
89
90 ralloc_strcat(&str, ")");
91 return str;
92 }
93
94
95 static ir_rvalue *
96 match_function_by_name(exec_list *instructions, const char *name,
97 YYLTYPE *loc, exec_list *actual_parameters,
98 struct _mesa_glsl_parse_state *state)
99 {
100 void *ctx = state;
101 ir_function *f = state->symbols->get_function(name);
102 ir_function_signature *sig;
103
104 sig = f ? f->matching_signature(actual_parameters) : NULL;
105
106 /* FINISHME: This doesn't handle the case where shader X contains a
107 * FINISHME: matching signature but shader X + N contains an _exact_
108 * FINISHME: matching signature.
109 */
110 if (sig == NULL
111 && (f == NULL || state->es_shader || !f->has_user_signature())
112 && state->symbols->get_type(name) == NULL
113 && (state->language_version == 110
114 || state->symbols->get_variable(name) == NULL)) {
115 /* The current shader doesn't contain a matching function or signature.
116 * Before giving up, look for the prototype in the built-in functions.
117 */
118 for (unsigned i = 0; i < state->num_builtins_to_link; i++) {
119 ir_function *builtin;
120 builtin = state->builtins_to_link[i]->symbols->get_function(name);
121 sig = builtin ? builtin->matching_signature(actual_parameters) : NULL;
122 if (sig != NULL) {
123 if (f == NULL) {
124 f = new(ctx) ir_function(name);
125 state->symbols->add_global_function(f);
126 emit_function(state, instructions, f);
127 }
128
129 f->add_signature(sig->clone_prototype(f, NULL));
130 break;
131 }
132 }
133 }
134
135 if (sig != NULL) {
136 /* Verify that 'out' and 'inout' actual parameters are lvalues. This
137 * isn't done in ir_function::matching_signature because that function
138 * cannot generate the necessary diagnostics.
139 *
140 * Also, validate that 'const_in' formal parameters (an extension of our
141 * IR) correspond to ir_constant actual parameters.
142 */
143 exec_list_iterator actual_iter = actual_parameters->iterator();
144 exec_list_iterator formal_iter = sig->parameters.iterator();
145
146 while (actual_iter.has_next()) {
147 ir_rvalue *actual = (ir_rvalue *) actual_iter.get();
148 ir_variable *formal = (ir_variable *) formal_iter.get();
149
150 assert(actual != NULL);
151 assert(formal != NULL);
152
153 if (formal->mode == ir_var_const_in && !actual->as_constant()) {
154 _mesa_glsl_error(loc, state,
155 "parameter `%s' must be a constant expression",
156 formal->name);
157 }
158
159 if ((formal->mode == ir_var_out)
160 || (formal->mode == ir_var_inout)) {
161 const char *mode = NULL;
162 switch (formal->mode) {
163 case ir_var_out: mode = "out"; break;
164 case ir_var_inout: mode = "inout"; break;
165 default: assert(false); break;
166 }
167 /* FIXME: 'loc' is incorrect (as of 2011-01-21). It is always
168 * FIXME: 0:0(0).
169 */
170 if (actual->variable_referenced()
171 && actual->variable_referenced()->read_only) {
172 _mesa_glsl_error(loc, state,
173 "function parameter '%s %s' references the "
174 "read-only variable '%s'",
175 mode, formal->name,
176 actual->variable_referenced()->name);
177
178 } else if (!actual->is_lvalue()) {
179 _mesa_glsl_error(loc, state,
180 "function parameter '%s %s' is not an lvalue",
181 mode, formal->name);
182 }
183 }
184
185 if (formal->type->is_numeric() || formal->type->is_boolean()) {
186 ir_rvalue *converted = convert_component(actual, formal->type);
187 actual->replace_with(converted);
188 }
189
190 actual_iter.next();
191 formal_iter.next();
192 }
193
194 /* Always insert the call in the instruction stream, and return a deref
195 * of its return val if it returns a value, since we don't know if
196 * the rvalue is going to be assigned to anything or not.
197 */
198 ir_call *call = new(ctx) ir_call(sig, actual_parameters);
199 if (!sig->return_type->is_void()) {
200 ir_variable *var;
201 ir_dereference_variable *deref;
202
203 var = new(ctx) ir_variable(sig->return_type,
204 ralloc_asprintf(ctx, "%s_retval",
205 sig->function_name()),
206 ir_var_temporary);
207 instructions->push_tail(var);
208
209 deref = new(ctx) ir_dereference_variable(var);
210 ir_assignment *assign = new(ctx) ir_assignment(deref, call, NULL);
211 instructions->push_tail(assign);
212 if (state->language_version >= 120)
213 var->constant_value = call->constant_expression_value();
214
215 deref = new(ctx) ir_dereference_variable(var);
216 return deref;
217 } else {
218 instructions->push_tail(call);
219 return NULL;
220 }
221 } else {
222 char *str = prototype_string(NULL, name, actual_parameters);
223
224 _mesa_glsl_error(loc, state, "no matching function for call to `%s'",
225 str);
226 ralloc_free(str);
227
228 const char *prefix = "candidates are: ";
229
230 for (int i = -1; i < (int) state->num_builtins_to_link; i++) {
231 glsl_symbol_table *syms = i >= 0 ? state->builtins_to_link[i]->symbols
232 : state->symbols;
233 f = syms->get_function(name);
234 if (f == NULL)
235 continue;
236
237 foreach_list (node, &f->signatures) {
238 ir_function_signature *sig = (ir_function_signature *) node;
239
240 str = prototype_string(sig->return_type, f->name, &sig->parameters);
241 _mesa_glsl_error(loc, state, "%s%s", prefix, str);
242 ralloc_free(str);
243
244 prefix = " ";
245 }
246
247 }
248
249 return ir_call::get_error_instruction(ctx);
250 }
251 }
252
253
254 /**
255 * Perform automatic type conversion of constructor parameters
256 *
257 * This implements the rules in the "Conversion and Scalar Constructors"
258 * section (GLSL 1.10 section 5.4.1), not the "Implicit Conversions" rules.
259 */
260 static ir_rvalue *
261 convert_component(ir_rvalue *src, const glsl_type *desired_type)
262 {
263 void *ctx = ralloc_parent(src);
264 const unsigned a = desired_type->base_type;
265 const unsigned b = src->type->base_type;
266 ir_expression *result = NULL;
267
268 if (src->type->is_error())
269 return src;
270
271 assert(a <= GLSL_TYPE_BOOL);
272 assert(b <= GLSL_TYPE_BOOL);
273
274 if (a == b)
275 return src;
276
277 switch (a) {
278 case GLSL_TYPE_UINT:
279 switch (b) {
280 case GLSL_TYPE_INT:
281 result = new(ctx) ir_expression(ir_unop_i2u, src);
282 break;
283 case GLSL_TYPE_FLOAT:
284 result = new(ctx) ir_expression(ir_unop_i2u,
285 new(ctx) ir_expression(ir_unop_f2i, src));
286 break;
287 case GLSL_TYPE_BOOL:
288 result = new(ctx) ir_expression(ir_unop_i2u,
289 new(ctx) ir_expression(ir_unop_b2i, src));
290 break;
291 }
292 break;
293 case GLSL_TYPE_INT:
294 switch (b) {
295 case GLSL_TYPE_UINT:
296 result = new(ctx) ir_expression(ir_unop_u2i, src);
297 break;
298 case GLSL_TYPE_FLOAT:
299 result = new(ctx) ir_expression(ir_unop_f2i, src);
300 break;
301 case GLSL_TYPE_BOOL:
302 result = new(ctx) ir_expression(ir_unop_b2i, src);
303 break;
304 }
305 break;
306 case GLSL_TYPE_FLOAT:
307 switch (b) {
308 case GLSL_TYPE_UINT:
309 result = new(ctx) ir_expression(ir_unop_u2f, desired_type, src, NULL);
310 break;
311 case GLSL_TYPE_INT:
312 result = new(ctx) ir_expression(ir_unop_i2f, desired_type, src, NULL);
313 break;
314 case GLSL_TYPE_BOOL:
315 result = new(ctx) ir_expression(ir_unop_b2f, desired_type, src, NULL);
316 break;
317 }
318 break;
319 case GLSL_TYPE_BOOL:
320 switch (b) {
321 case GLSL_TYPE_UINT:
322 result = new(ctx) ir_expression(ir_unop_i2b,
323 new(ctx) ir_expression(ir_unop_u2i, src));
324 break;
325 case GLSL_TYPE_INT:
326 result = new(ctx) ir_expression(ir_unop_i2b, desired_type, src, NULL);
327 break;
328 case GLSL_TYPE_FLOAT:
329 result = new(ctx) ir_expression(ir_unop_f2b, desired_type, src, NULL);
330 break;
331 }
332 break;
333 }
334
335 assert(result != NULL);
336 assert(result->type == desired_type);
337
338 /* Try constant folding; it may fold in the conversion we just added. */
339 ir_constant *const constant = result->constant_expression_value();
340 return (constant != NULL) ? (ir_rvalue *) constant : (ir_rvalue *) result;
341 }
342
343 /**
344 * Dereference a specific component from a scalar, vector, or matrix
345 */
346 static ir_rvalue *
347 dereference_component(ir_rvalue *src, unsigned component)
348 {
349 void *ctx = ralloc_parent(src);
350 assert(component < src->type->components());
351
352 /* If the source is a constant, just create a new constant instead of a
353 * dereference of the existing constant.
354 */
355 ir_constant *constant = src->as_constant();
356 if (constant)
357 return new(ctx) ir_constant(constant, component);
358
359 if (src->type->is_scalar()) {
360 return src;
361 } else if (src->type->is_vector()) {
362 return new(ctx) ir_swizzle(src, component, 0, 0, 0, 1);
363 } else {
364 assert(src->type->is_matrix());
365
366 /* Dereference a row of the matrix, then call this function again to get
367 * a specific element from that row.
368 */
369 const int c = component / src->type->column_type()->vector_elements;
370 const int r = component % src->type->column_type()->vector_elements;
371 ir_constant *const col_index = new(ctx) ir_constant(c);
372 ir_dereference *const col = new(ctx) ir_dereference_array(src, col_index);
373
374 col->type = src->type->column_type();
375
376 return dereference_component(col, r);
377 }
378
379 assert(!"Should not get here.");
380 return NULL;
381 }
382
383
384 static ir_rvalue *
385 process_array_constructor(exec_list *instructions,
386 const glsl_type *constructor_type,
387 YYLTYPE *loc, exec_list *parameters,
388 struct _mesa_glsl_parse_state *state)
389 {
390 void *ctx = state;
391 /* Array constructors come in two forms: sized and unsized. Sized array
392 * constructors look like 'vec4[2](a, b)', where 'a' and 'b' are vec4
393 * variables. In this case the number of parameters must exactly match the
394 * specified size of the array.
395 *
396 * Unsized array constructors look like 'vec4[](a, b)', where 'a' and 'b'
397 * are vec4 variables. In this case the size of the array being constructed
398 * is determined by the number of parameters.
399 *
400 * From page 52 (page 58 of the PDF) of the GLSL 1.50 spec:
401 *
402 * "There must be exactly the same number of arguments as the size of
403 * the array being constructed. If no size is present in the
404 * constructor, then the array is explicitly sized to the number of
405 * arguments provided. The arguments are assigned in order, starting at
406 * element 0, to the elements of the constructed array. Each argument
407 * must be the same type as the element type of the array, or be a type
408 * that can be converted to the element type of the array according to
409 * Section 4.1.10 "Implicit Conversions.""
410 */
411 exec_list actual_parameters;
412 const unsigned parameter_count =
413 process_parameters(instructions, &actual_parameters, parameters, state);
414
415 if ((parameter_count == 0)
416 || ((constructor_type->length != 0)
417 && (constructor_type->length != parameter_count))) {
418 const unsigned min_param = (constructor_type->length == 0)
419 ? 1 : constructor_type->length;
420
421 _mesa_glsl_error(loc, state, "array constructor must have %s %u "
422 "parameter%s",
423 (constructor_type->length != 0) ? "at least" : "exactly",
424 min_param, (min_param <= 1) ? "" : "s");
425 return ir_call::get_error_instruction(ctx);
426 }
427
428 if (constructor_type->length == 0) {
429 constructor_type =
430 glsl_type::get_array_instance(constructor_type->element_type(),
431 parameter_count);
432 assert(constructor_type != NULL);
433 assert(constructor_type->length == parameter_count);
434 }
435
436 bool all_parameters_are_constant = true;
437
438 /* Type cast each parameter and, if possible, fold constants. */
439 foreach_list_safe(n, &actual_parameters) {
440 ir_rvalue *ir = (ir_rvalue *) n;
441 ir_rvalue *result = ir;
442
443 /* Apply implicit conversions (not the scalar constructor rules!) */
444 if (constructor_type->element_type()->is_float()) {
445 const glsl_type *desired_type =
446 glsl_type::get_instance(GLSL_TYPE_FLOAT,
447 ir->type->vector_elements,
448 ir->type->matrix_columns);
449 result = convert_component(ir, desired_type);
450 }
451
452 if (result->type != constructor_type->element_type()) {
453 _mesa_glsl_error(loc, state, "type error in array constructor: "
454 "expected: %s, found %s",
455 constructor_type->element_type()->name,
456 result->type->name);
457 }
458
459 /* Attempt to convert the parameter to a constant valued expression.
460 * After doing so, track whether or not all the parameters to the
461 * constructor are trivially constant valued expressions.
462 */
463 ir_rvalue *const constant = result->constant_expression_value();
464
465 if (constant != NULL)
466 result = constant;
467 else
468 all_parameters_are_constant = false;
469
470 ir->replace_with(result);
471 }
472
473 if (all_parameters_are_constant)
474 return new(ctx) ir_constant(constructor_type, &actual_parameters);
475
476 ir_variable *var = new(ctx) ir_variable(constructor_type, "array_ctor",
477 ir_var_temporary);
478 instructions->push_tail(var);
479
480 int i = 0;
481 foreach_list(node, &actual_parameters) {
482 ir_rvalue *rhs = (ir_rvalue *) node;
483 ir_rvalue *lhs = new(ctx) ir_dereference_array(var,
484 new(ctx) ir_constant(i));
485
486 ir_instruction *assignment = new(ctx) ir_assignment(lhs, rhs, NULL);
487 instructions->push_tail(assignment);
488
489 i++;
490 }
491
492 return new(ctx) ir_dereference_variable(var);
493 }
494
495
496 /**
497 * Try to convert a record constructor to a constant expression
498 */
499 static ir_constant *
500 constant_record_constructor(const glsl_type *constructor_type,
501 exec_list *parameters, void *mem_ctx)
502 {
503 foreach_list(node, parameters) {
504 ir_constant *constant = ((ir_instruction *) node)->as_constant();
505 if (constant == NULL)
506 return NULL;
507 node->replace_with(constant);
508 }
509
510 return new(mem_ctx) ir_constant(constructor_type, parameters);
511 }
512
513
514 /**
515 * Determine if a list consists of a single scalar r-value
516 */
517 bool
518 single_scalar_parameter(exec_list *parameters)
519 {
520 const ir_rvalue *const p = (ir_rvalue *) parameters->head;
521 assert(((ir_rvalue *)p)->as_rvalue() != NULL);
522
523 return (p->type->is_scalar() && p->next->is_tail_sentinel());
524 }
525
526
527 /**
528 * Generate inline code for a vector constructor
529 *
530 * The generated constructor code will consist of a temporary variable
531 * declaration of the same type as the constructor. A sequence of assignments
532 * from constructor parameters to the temporary will follow.
533 *
534 * \return
535 * An \c ir_dereference_variable of the temprorary generated in the constructor
536 * body.
537 */
538 ir_rvalue *
539 emit_inline_vector_constructor(const glsl_type *type,
540 exec_list *instructions,
541 exec_list *parameters,
542 void *ctx)
543 {
544 assert(!parameters->is_empty());
545
546 ir_variable *var = new(ctx) ir_variable(type, "vec_ctor", ir_var_temporary);
547 instructions->push_tail(var);
548
549 /* There are two kinds of vector constructors.
550 *
551 * - Construct a vector from a single scalar by replicating that scalar to
552 * all components of the vector.
553 *
554 * - Construct a vector from an arbirary combination of vectors and
555 * scalars. The components of the constructor parameters are assigned
556 * to the vector in order until the vector is full.
557 */
558 const unsigned lhs_components = type->components();
559 if (single_scalar_parameter(parameters)) {
560 ir_rvalue *first_param = (ir_rvalue *)parameters->head;
561 ir_rvalue *rhs = new(ctx) ir_swizzle(first_param, 0, 0, 0, 0,
562 lhs_components);
563 ir_dereference_variable *lhs = new(ctx) ir_dereference_variable(var);
564 const unsigned mask = (1U << lhs_components) - 1;
565
566 assert(rhs->type == lhs->type);
567
568 ir_instruction *inst = new(ctx) ir_assignment(lhs, rhs, NULL, mask);
569 instructions->push_tail(inst);
570 } else {
571 unsigned base_component = 0;
572 unsigned base_lhs_component = 0;
573 ir_constant_data data;
574 unsigned constant_mask = 0, constant_components = 0;
575
576 memset(&data, 0, sizeof(data));
577
578 foreach_list(node, parameters) {
579 ir_rvalue *param = (ir_rvalue *) node;
580 unsigned rhs_components = param->type->components();
581
582 /* Do not try to assign more components to the vector than it has!
583 */
584 if ((rhs_components + base_lhs_component) > lhs_components) {
585 rhs_components = lhs_components - base_lhs_component;
586 }
587
588 const ir_constant *const c = param->as_constant();
589 if (c != NULL) {
590 for (unsigned i = 0; i < rhs_components; i++) {
591 switch (c->type->base_type) {
592 case GLSL_TYPE_UINT:
593 data.u[i + base_component] = c->get_uint_component(i);
594 break;
595 case GLSL_TYPE_INT:
596 data.i[i + base_component] = c->get_int_component(i);
597 break;
598 case GLSL_TYPE_FLOAT:
599 data.f[i + base_component] = c->get_float_component(i);
600 break;
601 case GLSL_TYPE_BOOL:
602 data.b[i + base_component] = c->get_bool_component(i);
603 break;
604 default:
605 assert(!"Should not get here.");
606 break;
607 }
608 }
609
610 /* Mask of fields to be written in the assignment.
611 */
612 constant_mask |= ((1U << rhs_components) - 1) << base_lhs_component;
613 constant_components += rhs_components;
614
615 base_component += rhs_components;
616 }
617 /* Advance the component index by the number of components
618 * that were just assigned.
619 */
620 base_lhs_component += rhs_components;
621 }
622
623 if (constant_mask != 0) {
624 ir_dereference *lhs = new(ctx) ir_dereference_variable(var);
625 const glsl_type *rhs_type = glsl_type::get_instance(var->type->base_type,
626 constant_components,
627 1);
628 ir_rvalue *rhs = new(ctx) ir_constant(rhs_type, &data);
629
630 ir_instruction *inst =
631 new(ctx) ir_assignment(lhs, rhs, NULL, constant_mask);
632 instructions->push_tail(inst);
633 }
634
635 base_component = 0;
636 foreach_list(node, parameters) {
637 ir_rvalue *param = (ir_rvalue *) node;
638 unsigned rhs_components = param->type->components();
639
640 /* Do not try to assign more components to the vector than it has!
641 */
642 if ((rhs_components + base_component) > lhs_components) {
643 rhs_components = lhs_components - base_component;
644 }
645
646 const ir_constant *const c = param->as_constant();
647 if (c == NULL) {
648 /* Mask of fields to be written in the assignment.
649 */
650 const unsigned write_mask = ((1U << rhs_components) - 1)
651 << base_component;
652
653 ir_dereference *lhs = new(ctx) ir_dereference_variable(var);
654
655 /* Generate a swizzle so that LHS and RHS sizes match.
656 */
657 ir_rvalue *rhs =
658 new(ctx) ir_swizzle(param, 0, 1, 2, 3, rhs_components);
659
660 ir_instruction *inst =
661 new(ctx) ir_assignment(lhs, rhs, NULL, write_mask);
662 instructions->push_tail(inst);
663 }
664
665 /* Advance the component index by the number of components that were
666 * just assigned.
667 */
668 base_component += rhs_components;
669 }
670 }
671 return new(ctx) ir_dereference_variable(var);
672 }
673
674
675 /**
676 * Generate assignment of a portion of a vector to a portion of a matrix column
677 *
678 * \param src_base First component of the source to be used in assignment
679 * \param column Column of destination to be assiged
680 * \param row_base First component of the destination column to be assigned
681 * \param count Number of components to be assigned
682 *
683 * \note
684 * \c src_base + \c count must be less than or equal to the number of components
685 * in the source vector.
686 */
687 ir_instruction *
688 assign_to_matrix_column(ir_variable *var, unsigned column, unsigned row_base,
689 ir_rvalue *src, unsigned src_base, unsigned count,
690 void *mem_ctx)
691 {
692 ir_constant *col_idx = new(mem_ctx) ir_constant(column);
693 ir_dereference *column_ref = new(mem_ctx) ir_dereference_array(var, col_idx);
694
695 assert(column_ref->type->components() >= (row_base + count));
696 assert(src->type->components() >= (src_base + count));
697
698 /* Generate a swizzle that extracts the number of components from the source
699 * that are to be assigned to the column of the matrix.
700 */
701 if (count < src->type->vector_elements) {
702 src = new(mem_ctx) ir_swizzle(src,
703 src_base + 0, src_base + 1,
704 src_base + 2, src_base + 3,
705 count);
706 }
707
708 /* Mask of fields to be written in the assignment.
709 */
710 const unsigned write_mask = ((1U << count) - 1) << row_base;
711
712 return new(mem_ctx) ir_assignment(column_ref, src, NULL, write_mask);
713 }
714
715
716 /**
717 * Generate inline code for a matrix constructor
718 *
719 * The generated constructor code will consist of a temporary variable
720 * declaration of the same type as the constructor. A sequence of assignments
721 * from constructor parameters to the temporary will follow.
722 *
723 * \return
724 * An \c ir_dereference_variable of the temprorary generated in the constructor
725 * body.
726 */
727 ir_rvalue *
728 emit_inline_matrix_constructor(const glsl_type *type,
729 exec_list *instructions,
730 exec_list *parameters,
731 void *ctx)
732 {
733 assert(!parameters->is_empty());
734
735 ir_variable *var = new(ctx) ir_variable(type, "mat_ctor", ir_var_temporary);
736 instructions->push_tail(var);
737
738 /* There are three kinds of matrix constructors.
739 *
740 * - Construct a matrix from a single scalar by replicating that scalar to
741 * along the diagonal of the matrix and setting all other components to
742 * zero.
743 *
744 * - Construct a matrix from an arbirary combination of vectors and
745 * scalars. The components of the constructor parameters are assigned
746 * to the matrix in colum-major order until the matrix is full.
747 *
748 * - Construct a matrix from a single matrix. The source matrix is copied
749 * to the upper left portion of the constructed matrix, and the remaining
750 * elements take values from the identity matrix.
751 */
752 ir_rvalue *const first_param = (ir_rvalue *) parameters->head;
753 if (single_scalar_parameter(parameters)) {
754 /* Assign the scalar to the X component of a vec4, and fill the remaining
755 * components with zero.
756 */
757 ir_variable *rhs_var =
758 new(ctx) ir_variable(glsl_type::vec4_type, "mat_ctor_vec",
759 ir_var_temporary);
760 instructions->push_tail(rhs_var);
761
762 ir_constant_data zero;
763 zero.f[0] = 0.0;
764 zero.f[1] = 0.0;
765 zero.f[2] = 0.0;
766 zero.f[3] = 0.0;
767
768 ir_instruction *inst =
769 new(ctx) ir_assignment(new(ctx) ir_dereference_variable(rhs_var),
770 new(ctx) ir_constant(rhs_var->type, &zero),
771 NULL);
772 instructions->push_tail(inst);
773
774 ir_dereference *const rhs_ref = new(ctx) ir_dereference_variable(rhs_var);
775
776 inst = new(ctx) ir_assignment(rhs_ref, first_param, NULL, 0x01);
777 instructions->push_tail(inst);
778
779 /* Assign the temporary vector to each column of the destination matrix
780 * with a swizzle that puts the X component on the diagonal of the
781 * matrix. In some cases this may mean that the X component does not
782 * get assigned into the column at all (i.e., when the matrix has more
783 * columns than rows).
784 */
785 static const unsigned rhs_swiz[4][4] = {
786 { 0, 1, 1, 1 },
787 { 1, 0, 1, 1 },
788 { 1, 1, 0, 1 },
789 { 1, 1, 1, 0 }
790 };
791
792 const unsigned cols_to_init = MIN2(type->matrix_columns,
793 type->vector_elements);
794 for (unsigned i = 0; i < cols_to_init; i++) {
795 ir_constant *const col_idx = new(ctx) ir_constant(i);
796 ir_rvalue *const col_ref = new(ctx) ir_dereference_array(var, col_idx);
797
798 ir_rvalue *const rhs_ref = new(ctx) ir_dereference_variable(rhs_var);
799 ir_rvalue *const rhs = new(ctx) ir_swizzle(rhs_ref, rhs_swiz[i],
800 type->vector_elements);
801
802 inst = new(ctx) ir_assignment(col_ref, rhs, NULL);
803 instructions->push_tail(inst);
804 }
805
806 for (unsigned i = cols_to_init; i < type->matrix_columns; i++) {
807 ir_constant *const col_idx = new(ctx) ir_constant(i);
808 ir_rvalue *const col_ref = new(ctx) ir_dereference_array(var, col_idx);
809
810 ir_rvalue *const rhs_ref = new(ctx) ir_dereference_variable(rhs_var);
811 ir_rvalue *const rhs = new(ctx) ir_swizzle(rhs_ref, 1, 1, 1, 1,
812 type->vector_elements);
813
814 inst = new(ctx) ir_assignment(col_ref, rhs, NULL);
815 instructions->push_tail(inst);
816 }
817 } else if (first_param->type->is_matrix()) {
818 /* From page 50 (56 of the PDF) of the GLSL 1.50 spec:
819 *
820 * "If a matrix is constructed from a matrix, then each component
821 * (column i, row j) in the result that has a corresponding
822 * component (column i, row j) in the argument will be initialized
823 * from there. All other components will be initialized to the
824 * identity matrix. If a matrix argument is given to a matrix
825 * constructor, it is an error to have any other arguments."
826 */
827 assert(first_param->next->is_tail_sentinel());
828 ir_rvalue *const src_matrix = first_param;
829
830 /* If the source matrix is smaller, pre-initialize the relavent parts of
831 * the destination matrix to the identity matrix.
832 */
833 if ((src_matrix->type->matrix_columns < var->type->matrix_columns)
834 || (src_matrix->type->vector_elements < var->type->vector_elements)) {
835
836 /* If the source matrix has fewer rows, every column of the destination
837 * must be initialized. Otherwise only the columns in the destination
838 * that do not exist in the source must be initialized.
839 */
840 unsigned col =
841 (src_matrix->type->vector_elements < var->type->vector_elements)
842 ? 0 : src_matrix->type->matrix_columns;
843
844 const glsl_type *const col_type = var->type->column_type();
845 for (/* empty */; col < var->type->matrix_columns; col++) {
846 ir_constant_data ident;
847
848 ident.f[0] = 0.0;
849 ident.f[1] = 0.0;
850 ident.f[2] = 0.0;
851 ident.f[3] = 0.0;
852
853 ident.f[col] = 1.0;
854
855 ir_rvalue *const rhs = new(ctx) ir_constant(col_type, &ident);
856
857 ir_rvalue *const lhs =
858 new(ctx) ir_dereference_array(var, new(ctx) ir_constant(col));
859
860 ir_instruction *inst = new(ctx) ir_assignment(lhs, rhs, NULL);
861 instructions->push_tail(inst);
862 }
863 }
864
865 /* Assign columns from the source matrix to the destination matrix.
866 *
867 * Since the parameter will be used in the RHS of multiple assignments,
868 * generate a temporary and copy the paramter there.
869 */
870 ir_variable *const rhs_var =
871 new(ctx) ir_variable(first_param->type, "mat_ctor_mat",
872 ir_var_temporary);
873 instructions->push_tail(rhs_var);
874
875 ir_dereference *const rhs_var_ref =
876 new(ctx) ir_dereference_variable(rhs_var);
877 ir_instruction *const inst =
878 new(ctx) ir_assignment(rhs_var_ref, first_param, NULL);
879 instructions->push_tail(inst);
880
881 const unsigned last_row = MIN2(src_matrix->type->vector_elements,
882 var->type->vector_elements);
883 const unsigned last_col = MIN2(src_matrix->type->matrix_columns,
884 var->type->matrix_columns);
885
886 unsigned swiz[4] = { 0, 0, 0, 0 };
887 for (unsigned i = 1; i < last_row; i++)
888 swiz[i] = i;
889
890 const unsigned write_mask = (1U << last_row) - 1;
891
892 for (unsigned i = 0; i < last_col; i++) {
893 ir_dereference *const lhs =
894 new(ctx) ir_dereference_array(var, new(ctx) ir_constant(i));
895 ir_rvalue *const rhs_col =
896 new(ctx) ir_dereference_array(rhs_var, new(ctx) ir_constant(i));
897
898 /* If one matrix has columns that are smaller than the columns of the
899 * other matrix, wrap the column access of the larger with a swizzle
900 * so that the LHS and RHS of the assignment have the same size (and
901 * therefore have the same type).
902 *
903 * It would be perfectly valid to unconditionally generate the
904 * swizzles, this this will typically result in a more compact IR tree.
905 */
906 ir_rvalue *rhs;
907 if (lhs->type->vector_elements != rhs_col->type->vector_elements) {
908 rhs = new(ctx) ir_swizzle(rhs_col, swiz, last_row);
909 } else {
910 rhs = rhs_col;
911 }
912
913 ir_instruction *inst =
914 new(ctx) ir_assignment(lhs, rhs, NULL, write_mask);
915 instructions->push_tail(inst);
916 }
917 } else {
918 const unsigned cols = type->matrix_columns;
919 const unsigned rows = type->vector_elements;
920 unsigned col_idx = 0;
921 unsigned row_idx = 0;
922
923 foreach_list (node, parameters) {
924 ir_rvalue *const rhs = (ir_rvalue *) node;
925 const unsigned components_remaining_this_column = rows - row_idx;
926 unsigned rhs_components = rhs->type->components();
927 unsigned rhs_base = 0;
928
929 /* Since the parameter might be used in the RHS of two assignments,
930 * generate a temporary and copy the paramter there.
931 */
932 ir_variable *rhs_var =
933 new(ctx) ir_variable(rhs->type, "mat_ctor_vec", ir_var_temporary);
934 instructions->push_tail(rhs_var);
935
936 ir_dereference *rhs_var_ref =
937 new(ctx) ir_dereference_variable(rhs_var);
938 ir_instruction *inst = new(ctx) ir_assignment(rhs_var_ref, rhs, NULL);
939 instructions->push_tail(inst);
940
941 /* Assign the current parameter to as many components of the matrix
942 * as it will fill.
943 *
944 * NOTE: A single vector parameter can span two matrix columns. A
945 * single vec4, for example, can completely fill a mat2.
946 */
947 if (rhs_components >= components_remaining_this_column) {
948 const unsigned count = MIN2(rhs_components,
949 components_remaining_this_column);
950
951 rhs_var_ref = new(ctx) ir_dereference_variable(rhs_var);
952
953 ir_instruction *inst = assign_to_matrix_column(var, col_idx,
954 row_idx,
955 rhs_var_ref, 0,
956 count, ctx);
957 instructions->push_tail(inst);
958
959 rhs_base = count;
960
961 col_idx++;
962 row_idx = 0;
963 }
964
965 /* If there is data left in the parameter and components left to be
966 * set in the destination, emit another assignment. It is possible
967 * that the assignment could be of a vec4 to the last element of the
968 * matrix. In this case col_idx==cols, but there is still data
969 * left in the source parameter. Obviously, don't emit an assignment
970 * to data outside the destination matrix.
971 */
972 if ((col_idx < cols) && (rhs_base < rhs_components)) {
973 const unsigned count = rhs_components - rhs_base;
974
975 rhs_var_ref = new(ctx) ir_dereference_variable(rhs_var);
976
977 ir_instruction *inst = assign_to_matrix_column(var, col_idx,
978 row_idx,
979 rhs_var_ref,
980 rhs_base,
981 count, ctx);
982 instructions->push_tail(inst);
983
984 row_idx += count;
985 }
986 }
987 }
988
989 return new(ctx) ir_dereference_variable(var);
990 }
991
992
993 ir_rvalue *
994 emit_inline_record_constructor(const glsl_type *type,
995 exec_list *instructions,
996 exec_list *parameters,
997 void *mem_ctx)
998 {
999 ir_variable *const var =
1000 new(mem_ctx) ir_variable(type, "record_ctor", ir_var_temporary);
1001 ir_dereference_variable *const d = new(mem_ctx) ir_dereference_variable(var);
1002
1003 instructions->push_tail(var);
1004
1005 exec_node *node = parameters->head;
1006 for (unsigned i = 0; i < type->length; i++) {
1007 assert(!node->is_tail_sentinel());
1008
1009 ir_dereference *const lhs =
1010 new(mem_ctx) ir_dereference_record(d->clone(mem_ctx, NULL),
1011 type->fields.structure[i].name);
1012
1013 ir_rvalue *const rhs = ((ir_instruction *) node)->as_rvalue();
1014 assert(rhs != NULL);
1015
1016 ir_instruction *const assign = new(mem_ctx) ir_assignment(lhs, rhs, NULL);
1017
1018 instructions->push_tail(assign);
1019 node = node->next;
1020 }
1021
1022 return d;
1023 }
1024
1025
1026 ir_rvalue *
1027 ast_function_expression::hir(exec_list *instructions,
1028 struct _mesa_glsl_parse_state *state)
1029 {
1030 void *ctx = state;
1031 /* There are three sorts of function calls.
1032 *
1033 * 1. constructors - The first subexpression is an ast_type_specifier.
1034 * 2. methods - Only the .length() method of array types.
1035 * 3. functions - Calls to regular old functions.
1036 *
1037 * Method calls are actually detected when the ast_field_selection
1038 * expression is handled.
1039 */
1040 if (is_constructor()) {
1041 const ast_type_specifier *type = (ast_type_specifier *) subexpressions[0];
1042 YYLTYPE loc = type->get_location();
1043 const char *name;
1044
1045 const glsl_type *const constructor_type = type->glsl_type(& name, state);
1046
1047 /* constructor_type can be NULL if a variable with the same name as the
1048 * structure has come into scope.
1049 */
1050 if (constructor_type == NULL) {
1051 _mesa_glsl_error(& loc, state, "unknown type `%s' (structure name "
1052 "may be shadowed by a variable with the same name)",
1053 type->type_name);
1054 return ir_call::get_error_instruction(ctx);
1055 }
1056
1057
1058 /* Constructors for samplers are illegal.
1059 */
1060 if (constructor_type->is_sampler()) {
1061 _mesa_glsl_error(& loc, state, "cannot construct sampler type `%s'",
1062 constructor_type->name);
1063 return ir_call::get_error_instruction(ctx);
1064 }
1065
1066 if (constructor_type->is_array()) {
1067 if (state->language_version <= 110) {
1068 _mesa_glsl_error(& loc, state,
1069 "array constructors forbidden in GLSL 1.10");
1070 return ir_call::get_error_instruction(ctx);
1071 }
1072
1073 return process_array_constructor(instructions, constructor_type,
1074 & loc, &this->expressions, state);
1075 }
1076
1077
1078 /* There are two kinds of constructor call. Constructors for built-in
1079 * language types, such as mat4 and vec2, are free form. The only
1080 * requirement is that the parameters must provide enough values of the
1081 * correct scalar type. Constructors for arrays and structures must
1082 * have the exact number of parameters with matching types in the
1083 * correct order. These constructors follow essentially the same type
1084 * matching rules as functions.
1085 */
1086 if (constructor_type->is_record()) {
1087 exec_list actual_parameters;
1088
1089 process_parameters(instructions, &actual_parameters,
1090 &this->expressions, state);
1091
1092 exec_node *node = actual_parameters.head;
1093 for (unsigned i = 0; i < constructor_type->length; i++) {
1094 ir_rvalue *ir = (ir_rvalue *) node;
1095
1096 if (node->is_tail_sentinel()) {
1097 _mesa_glsl_error(&loc, state,
1098 "insufficient parameters to constructor "
1099 "for `%s'",
1100 constructor_type->name);
1101 return ir_call::get_error_instruction(ctx);
1102 }
1103
1104 if (apply_implicit_conversion(constructor_type->fields.structure[i].type,
1105 ir, state)) {
1106 node->replace_with(ir);
1107 } else {
1108 _mesa_glsl_error(&loc, state,
1109 "parameter type mismatch in constructor "
1110 "for `%s.%s' (%s vs %s)",
1111 constructor_type->name,
1112 constructor_type->fields.structure[i].name,
1113 ir->type->name,
1114 constructor_type->fields.structure[i].type->name);
1115 return ir_call::get_error_instruction(ctx);;
1116 }
1117
1118 node = node->next;
1119 }
1120
1121 if (!node->is_tail_sentinel()) {
1122 _mesa_glsl_error(&loc, state, "too many parameters in constructor "
1123 "for `%s'", constructor_type->name);
1124 return ir_call::get_error_instruction(ctx);
1125 }
1126
1127 ir_rvalue *const constant =
1128 constant_record_constructor(constructor_type, &actual_parameters,
1129 state);
1130
1131 return (constant != NULL)
1132 ? constant
1133 : emit_inline_record_constructor(constructor_type, instructions,
1134 &actual_parameters, state);
1135 }
1136
1137 if (!constructor_type->is_numeric() && !constructor_type->is_boolean())
1138 return ir_call::get_error_instruction(ctx);
1139
1140 /* Total number of components of the type being constructed. */
1141 const unsigned type_components = constructor_type->components();
1142
1143 /* Number of components from parameters that have actually been
1144 * consumed. This is used to perform several kinds of error checking.
1145 */
1146 unsigned components_used = 0;
1147
1148 unsigned matrix_parameters = 0;
1149 unsigned nonmatrix_parameters = 0;
1150 exec_list actual_parameters;
1151
1152 foreach_list (n, &this->expressions) {
1153 ast_node *ast = exec_node_data(ast_node, n, link);
1154 ir_rvalue *result = ast->hir(instructions, state)->as_rvalue();
1155
1156 /* From page 50 (page 56 of the PDF) of the GLSL 1.50 spec:
1157 *
1158 * "It is an error to provide extra arguments beyond this
1159 * last used argument."
1160 */
1161 if (components_used >= type_components) {
1162 _mesa_glsl_error(& loc, state, "too many parameters to `%s' "
1163 "constructor",
1164 constructor_type->name);
1165 return ir_call::get_error_instruction(ctx);
1166 }
1167
1168 if (!result->type->is_numeric() && !result->type->is_boolean()) {
1169 _mesa_glsl_error(& loc, state, "cannot construct `%s' from a "
1170 "non-numeric data type",
1171 constructor_type->name);
1172 return ir_call::get_error_instruction(ctx);
1173 }
1174
1175 /* Count the number of matrix and nonmatrix parameters. This
1176 * is used below to enforce some of the constructor rules.
1177 */
1178 if (result->type->is_matrix())
1179 matrix_parameters++;
1180 else
1181 nonmatrix_parameters++;
1182
1183 actual_parameters.push_tail(result);
1184 components_used += result->type->components();
1185 }
1186
1187 /* From page 28 (page 34 of the PDF) of the GLSL 1.10 spec:
1188 *
1189 * "It is an error to construct matrices from other matrices. This
1190 * is reserved for future use."
1191 */
1192 if (state->language_version == 110 && matrix_parameters > 0
1193 && constructor_type->is_matrix()) {
1194 _mesa_glsl_error(& loc, state, "cannot construct `%s' from a "
1195 "matrix in GLSL 1.10",
1196 constructor_type->name);
1197 return ir_call::get_error_instruction(ctx);
1198 }
1199
1200 /* From page 50 (page 56 of the PDF) of the GLSL 1.50 spec:
1201 *
1202 * "If a matrix argument is given to a matrix constructor, it is
1203 * an error to have any other arguments."
1204 */
1205 if ((matrix_parameters > 0)
1206 && ((matrix_parameters + nonmatrix_parameters) > 1)
1207 && constructor_type->is_matrix()) {
1208 _mesa_glsl_error(& loc, state, "for matrix `%s' constructor, "
1209 "matrix must be only parameter",
1210 constructor_type->name);
1211 return ir_call::get_error_instruction(ctx);
1212 }
1213
1214 /* From page 28 (page 34 of the PDF) of the GLSL 1.10 spec:
1215 *
1216 * "In these cases, there must be enough components provided in the
1217 * arguments to provide an initializer for every component in the
1218 * constructed value."
1219 */
1220 if (components_used < type_components && components_used != 1
1221 && matrix_parameters == 0) {
1222 _mesa_glsl_error(& loc, state, "too few components to construct "
1223 "`%s'",
1224 constructor_type->name);
1225 return ir_call::get_error_instruction(ctx);
1226 }
1227
1228 /* Later, we cast each parameter to the same base type as the
1229 * constructor. Since there are no non-floating point matrices, we
1230 * need to break them up into a series of column vectors.
1231 */
1232 if (constructor_type->base_type != GLSL_TYPE_FLOAT) {
1233 foreach_list_safe(n, &actual_parameters) {
1234 ir_rvalue *matrix = (ir_rvalue *) n;
1235
1236 if (!matrix->type->is_matrix())
1237 continue;
1238
1239 /* Create a temporary containing the matrix. */
1240 ir_variable *var = new(ctx) ir_variable(matrix->type, "matrix_tmp",
1241 ir_var_temporary);
1242 instructions->push_tail(var);
1243 instructions->push_tail(new(ctx) ir_assignment(new(ctx)
1244 ir_dereference_variable(var), matrix, NULL));
1245 var->constant_value = matrix->constant_expression_value();
1246
1247 /* Replace the matrix with dereferences of its columns. */
1248 for (int i = 0; i < matrix->type->matrix_columns; i++) {
1249 matrix->insert_before(new (ctx) ir_dereference_array(var,
1250 new(ctx) ir_constant(i)));
1251 }
1252 matrix->remove();
1253 }
1254 }
1255
1256 bool all_parameters_are_constant = true;
1257
1258 /* Type cast each parameter and, if possible, fold constants.*/
1259 foreach_list_safe(n, &actual_parameters) {
1260 ir_rvalue *ir = (ir_rvalue *) n;
1261
1262 const glsl_type *desired_type =
1263 glsl_type::get_instance(constructor_type->base_type,
1264 ir->type->vector_elements,
1265 ir->type->matrix_columns);
1266 ir_rvalue *result = convert_component(ir, desired_type);
1267
1268 /* Attempt to convert the parameter to a constant valued expression.
1269 * After doing so, track whether or not all the parameters to the
1270 * constructor are trivially constant valued expressions.
1271 */
1272 ir_rvalue *const constant = result->constant_expression_value();
1273
1274 if (constant != NULL)
1275 result = constant;
1276 else
1277 all_parameters_are_constant = false;
1278
1279 if (result != ir) {
1280 ir->replace_with(result);
1281 }
1282 }
1283
1284 /* If all of the parameters are trivially constant, create a
1285 * constant representing the complete collection of parameters.
1286 */
1287 if (all_parameters_are_constant) {
1288 return new(ctx) ir_constant(constructor_type, &actual_parameters);
1289 } else if (constructor_type->is_scalar()) {
1290 return dereference_component((ir_rvalue *) actual_parameters.head,
1291 0);
1292 } else if (constructor_type->is_vector()) {
1293 return emit_inline_vector_constructor(constructor_type,
1294 instructions,
1295 &actual_parameters,
1296 ctx);
1297 } else {
1298 assert(constructor_type->is_matrix());
1299 return emit_inline_matrix_constructor(constructor_type,
1300 instructions,
1301 &actual_parameters,
1302 ctx);
1303 }
1304 } else {
1305 const ast_expression *id = subexpressions[0];
1306 YYLTYPE loc = id->get_location();
1307 exec_list actual_parameters;
1308
1309 process_parameters(instructions, &actual_parameters, &this->expressions,
1310 state);
1311
1312 return match_function_by_name(instructions,
1313 id->primary_expression.identifier, & loc,
1314 &actual_parameters, state);
1315 }
1316
1317 return ir_call::get_error_instruction(ctx);
1318 }