Add conversion of bool to float as an IR operation to match int to float.
[mesa.git] / glsl_types.cpp
1 /*
2 * Copyright © 2009 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 <cstdio>
25 #include <stdlib.h>
26 #include "glsl_symbol_table.h"
27 #include "glsl_parser_extras.h"
28 #include "glsl_types.h"
29 #include "builtin_types.h"
30 #include "hash_table.h"
31
32
33 hash_table *glsl_type::array_types = NULL;
34
35 static void
36 add_types_to_symbol_table(glsl_symbol_table *symtab,
37 const struct glsl_type *types,
38 unsigned num_types)
39 {
40 unsigned i;
41
42 for (i = 0; i < num_types; i++) {
43 symtab->add_type(types[i].name, & types[i]);
44 }
45 }
46
47
48 static void
49 generate_110_types(glsl_symbol_table *symtab)
50 {
51 add_types_to_symbol_table(symtab, builtin_core_types,
52 Elements(builtin_core_types));
53 add_types_to_symbol_table(symtab, builtin_structure_types,
54 Elements(builtin_structure_types));
55 add_types_to_symbol_table(symtab, builtin_110_deprecated_structure_types,
56 Elements(builtin_110_deprecated_structure_types));
57 add_types_to_symbol_table(symtab, & void_type, 1);
58 }
59
60
61 static void
62 generate_120_types(glsl_symbol_table *symtab)
63 {
64 generate_110_types(symtab);
65
66 add_types_to_symbol_table(symtab, builtin_120_types,
67 Elements(builtin_120_types));
68 }
69
70
71 static void
72 generate_130_types(glsl_symbol_table *symtab)
73 {
74 generate_120_types(symtab);
75
76 add_types_to_symbol_table(symtab, builtin_130_types,
77 Elements(builtin_130_types));
78 }
79
80
81 void
82 _mesa_glsl_initialize_types(struct _mesa_glsl_parse_state *state)
83 {
84 switch (state->language_version) {
85 case 110:
86 generate_110_types(state->symbols);
87 break;
88 case 120:
89 generate_120_types(state->symbols);
90 break;
91 case 130:
92 generate_130_types(state->symbols);
93 break;
94 default:
95 /* error */
96 break;
97 }
98 }
99
100
101 const glsl_type *glsl_type::get_base_type() const
102 {
103 switch (base_type) {
104 case GLSL_TYPE_UINT:
105 return uint_type;
106 case GLSL_TYPE_INT:
107 return int_type;
108 case GLSL_TYPE_FLOAT:
109 return float_type;
110 case GLSL_TYPE_BOOL:
111 return bool_type;
112 default:
113 return error_type;
114 }
115 }
116
117
118 ir_function *
119 glsl_type::generate_constructor_prototype(glsl_symbol_table *symtab) const
120 {
121 /* FINISHME: Add support for non-array types. */
122 assert(base_type == GLSL_TYPE_ARRAY);
123
124 /* Generate the function name and add it to the symbol table.
125 */
126 ir_function *const f = new ir_function(name);
127
128 bool added = symtab->add_function(name, f);
129 assert(added);
130
131 ir_function_signature *const sig = new ir_function_signature(this);
132 f->add_signature(sig);
133
134 for (unsigned i = 0; i < length; i++) {
135 char *const param_name = (char *) malloc(10);
136
137 snprintf(param_name, 10, "p%08X", i);
138
139 ir_variable *var = new ir_variable(fields.array, param_name);
140
141 var->mode = ir_var_in;
142 sig->parameters.push_tail(var);
143 }
144
145 return f;
146 }
147
148
149 /**
150 * Generate the function intro for a constructor
151 *
152 * \param type Data type to be constructed
153 * \param count Number of parameters to this concrete constructor. Most
154 * types have at least two constructors. One will take a
155 * single scalar parameter and the other will take "N"
156 * scalar parameters.
157 * \param parameters Storage for the list of parameters. These are
158 * typically stored in an \c ir_function_signature.
159 * \param instructions Storage for the preamble and body of the function.
160 * \param declarations Pointers to the variable declarations for the function
161 * parameters. These are used later to avoid having to use
162 * the symbol table.
163 */
164 static ir_label *
165 generate_constructor_intro(const glsl_type *type, unsigned parameter_count,
166 exec_list *parameters, exec_list *instructions,
167 ir_variable **declarations)
168 {
169 /* Names of parameters used in vector and matrix constructors
170 */
171 static const char *const names[] = {
172 "a", "b", "c", "d", "e", "f", "g", "h",
173 "i", "j", "k", "l", "m", "n", "o", "p",
174 };
175
176 assert(parameter_count <= Elements(names));
177
178 const glsl_type *const parameter_type = type->get_base_type();
179
180 ir_label *const label = new ir_label(type->name);
181 instructions->push_tail(label);
182
183 for (unsigned i = 0; i < parameter_count; i++) {
184 ir_variable *var = new ir_variable(parameter_type, names[i]);
185
186 var->mode = ir_var_in;
187 parameters->push_tail(var);
188
189 var = new ir_variable(parameter_type, names[i]);
190
191 var->mode = ir_var_in;
192 instructions->push_tail(var);
193
194 declarations[i] = var;
195 }
196
197 ir_variable *retval = new ir_variable(type, "__retval");
198 instructions->push_tail(retval);
199
200 declarations[16] = retval;
201 return label;
202 }
203
204
205 /**
206 * Generate the body of a vector constructor that takes a single scalar
207 */
208 static void
209 generate_vec_body_from_scalar(exec_list *instructions,
210 ir_variable **declarations)
211 {
212 ir_instruction *inst;
213
214 /* Generate a single assignment of the parameter to __retval.x and return
215 * __retval.xxxx for however many vector components there are.
216 */
217 ir_dereference *const lhs_ref = new ir_dereference(declarations[16]);
218 ir_dereference *const rhs = new ir_dereference(declarations[0]);
219
220 ir_swizzle *lhs = new ir_swizzle(lhs_ref, 0, 0, 0, 0, 1);
221
222 inst = new ir_assignment(lhs, rhs, NULL);
223 instructions->push_tail(inst);
224
225 ir_dereference *const retref = new ir_dereference(declarations[16]);
226
227 ir_swizzle *retval = new ir_swizzle(retref, 0, 0, 0, 0,
228 declarations[16]->type->vector_elements);
229
230 inst = new ir_return(retval);
231 instructions->push_tail(inst);
232 }
233
234
235 /**
236 * Generate the body of a vector constructor that takes multiple scalars
237 */
238 static void
239 generate_vec_body_from_N_scalars(exec_list *instructions,
240 ir_variable **declarations)
241 {
242 ir_instruction *inst;
243 const glsl_type *const vec_type = declarations[16]->type;
244
245
246 /* Generate an assignment of each parameter to a single component of
247 * __retval.x and return __retval.
248 */
249 for (unsigned i = 0; i < vec_type->vector_elements; i++) {
250 ir_dereference *const lhs_ref = new ir_dereference(declarations[16]);
251 ir_dereference *const rhs = new ir_dereference(declarations[i]);
252
253 ir_swizzle *lhs = new ir_swizzle(lhs_ref, 1, 0, 0, 0, 1);
254
255 inst = new ir_assignment(lhs, rhs, NULL);
256 instructions->push_tail(inst);
257 }
258
259 ir_dereference *retval = new ir_dereference(declarations[16]);
260
261 inst = new ir_return(retval);
262 instructions->push_tail(inst);
263 }
264
265
266 /**
267 * Generate the body of a matrix constructor that takes a single scalar
268 */
269 static void
270 generate_mat_body_from_scalar(exec_list *instructions,
271 ir_variable **declarations)
272 {
273 ir_instruction *inst;
274
275 /* Generate an assignment of the parameter to the X component of a
276 * temporary vector. Set the remaining fields of the vector to 0. The
277 * size of the vector is equal to the number of rows of the matrix.
278 *
279 * Set each column of the matrix to a successive "rotation" of the
280 * temporary vector. This fills the matrix with 0s, but writes the single
281 * scalar along the matrix's diagonal.
282 *
283 * For a mat4x3, this is equivalent to:
284 *
285 * vec3 tmp;
286 * mat4x3 __retval;
287 * tmp.x = a;
288 * tmp.y = 0.0;
289 * tmp.z = 0.0;
290 * __retval[0] = tmp.xyy;
291 * __retval[1] = tmp.yxy;
292 * __retval[2] = tmp.yyx;
293 * __retval[3] = tmp.yyy;
294 */
295 const glsl_type *const column_type = declarations[16]->type->column_type();
296 const glsl_type *const row_type = declarations[16]->type->row_type();
297 ir_variable *const column = new ir_variable(column_type, "v");
298
299 instructions->push_tail(column);
300
301 ir_dereference *const lhs_ref = new ir_dereference(column);
302 ir_dereference *const rhs = new ir_dereference(declarations[0]);
303
304 ir_swizzle *lhs = new ir_swizzle(lhs_ref, 0, 0, 0, 0, 1);
305
306 inst = new ir_assignment(lhs, rhs, NULL);
307 instructions->push_tail(inst);
308
309 const float z = 0.0f;
310 ir_constant *const zero = new ir_constant(glsl_type::float_type, &z);
311
312 for (unsigned i = 1; i < column_type->vector_elements; i++) {
313 ir_dereference *const lhs_ref = new ir_dereference(column);
314
315 ir_swizzle *lhs = new ir_swizzle(lhs_ref, i, 0, 0, 0, 1);
316
317 inst = new ir_assignment(lhs, zero, NULL);
318 instructions->push_tail(inst);
319 }
320
321
322 for (unsigned i = 0; i < row_type->vector_elements; i++) {
323 static const unsigned swiz[] = { 1, 1, 1, 0, 1, 1, 1 };
324 ir_dereference *const rhs_ref = new ir_dereference(column);
325
326 /* This will be .xyyy when i=0, .yxyy when i=1, etc.
327 */
328 ir_swizzle *rhs = new ir_swizzle(rhs_ref, swiz[3 - i], swiz[4 - i],
329 swiz[5 - i], swiz[6 - i],
330 column_type->vector_elements);
331
332 ir_constant *const idx = new ir_constant(glsl_type::int_type, &i);
333 ir_dereference *const lhs = new ir_dereference(declarations[16], idx);
334
335 inst = new ir_assignment(lhs, rhs, NULL);
336 instructions->push_tail(inst);
337 }
338
339 ir_dereference *const retval = new ir_dereference(declarations[16]);
340 inst = new ir_return(retval);
341 instructions->push_tail(inst);
342 }
343
344
345 /**
346 * Generate the body of a vector constructor that takes multiple scalars
347 */
348 static void
349 generate_mat_body_from_N_scalars(exec_list *instructions,
350 ir_variable **declarations)
351 {
352 ir_instruction *inst;
353 const glsl_type *const row_type = declarations[16]->type->row_type();
354 const glsl_type *const column_type = declarations[16]->type->column_type();
355
356
357 /* Generate an assignment of each parameter to a single component of
358 * of a particular column of __retval and return __retval.
359 */
360 for (unsigned i = 0; i < column_type->vector_elements; i++) {
361 for (unsigned j = 0; j < row_type->vector_elements; j++) {
362 ir_constant *row_index = new ir_constant(glsl_type::int_type, &i);
363 ir_dereference *const row_access =
364 new ir_dereference(declarations[16], row_index);
365
366 ir_dereference *const component_access_ref =
367 new ir_dereference(row_access);
368
369 ir_swizzle *component_access = new ir_swizzle(component_access_ref,
370 j, 0, 0, 0, 1);
371
372 const unsigned param = (i * row_type->vector_elements) + j;
373 ir_dereference *const rhs = new ir_dereference(declarations[param]);
374
375 inst = new ir_assignment(component_access, rhs, NULL);
376 instructions->push_tail(inst);
377 }
378 }
379
380 ir_dereference *retval = new ir_dereference(declarations[16]);
381
382 inst = new ir_return(retval);
383 instructions->push_tail(inst);
384 }
385
386
387 /**
388 * Generate the constructors for a set of GLSL types
389 *
390 * Constructor implementations are added to \c instructions, and the symbols
391 * are added to \c symtab.
392 */
393 static void
394 generate_constructor(glsl_symbol_table *symtab, const struct glsl_type *types,
395 unsigned num_types, exec_list *instructions)
396 {
397 ir_variable *declarations[17];
398
399 for (unsigned i = 0; i < num_types; i++) {
400 /* Only numeric and boolean vectors and matrices get constructors here.
401 * Structures need to be handled elsewhere. It is expected that scalar
402 * constructors are never actually called, so they are not generated.
403 */
404 if (!types[i].is_numeric() && !types[i].is_boolean())
405 continue;
406
407 if (types[i].is_scalar())
408 continue;
409
410 /* Generate the function name and add it to the symbol table.
411 */
412 ir_function *const f = new ir_function(types[i].name);
413
414 bool added = symtab->add_function(types[i].name, f);
415 assert(added);
416
417
418 /* Each type has several basic constructors. The total number of forms
419 * depends on the derived type.
420 *
421 * Vectors: 1 scalar, N scalars
422 * Matrices: 1 scalar, NxM scalars
423 *
424 * Several possible types of constructors are not included in this list.
425 *
426 * Scalar constructors are not included. The expectation is that the
427 * IR generator won't actually generate these as constructor calls. The
428 * expectation is that it will just generate the necessary type
429 * conversion.
430 *
431 * Matrix contructors from matrices are also not included. The
432 * expectation is that the IR generator will generate a call to the
433 * appropriate from-scalars constructor.
434 */
435 ir_function_signature *const sig = new ir_function_signature(& types[i]);
436 f->add_signature(sig);
437
438 sig->definition =
439 generate_constructor_intro(& types[i], 1, & sig->parameters,
440 instructions, declarations);
441
442 if (types[i].is_vector()) {
443 generate_vec_body_from_scalar(instructions, declarations);
444
445 ir_function_signature *const vec_sig =
446 new ir_function_signature(& types[i]);
447 f->add_signature(vec_sig);
448
449 vec_sig->definition =
450 generate_constructor_intro(& types[i], types[i].vector_elements,
451 & vec_sig->parameters, instructions,
452 declarations);
453 generate_vec_body_from_N_scalars(instructions, declarations);
454 } else {
455 assert(types[i].is_matrix());
456
457 generate_mat_body_from_scalar(instructions, declarations);
458
459 ir_function_signature *const mat_sig =
460 new ir_function_signature(& types[i]);
461 f->add_signature(mat_sig);
462
463 mat_sig->definition =
464 generate_constructor_intro(& types[i],
465 (types[i].vector_elements
466 * types[i].matrix_columns),
467 & mat_sig->parameters, instructions,
468 declarations);
469 generate_mat_body_from_N_scalars(instructions, declarations);
470 }
471 }
472 }
473
474
475 void
476 generate_110_constructors(glsl_symbol_table *symtab, exec_list *instructions)
477 {
478 generate_constructor(symtab, builtin_core_types,
479 Elements(builtin_core_types), instructions);
480 }
481
482
483 void
484 generate_120_constructors(glsl_symbol_table *symtab, exec_list *instructions)
485 {
486 generate_110_constructors(symtab, instructions);
487
488 generate_constructor(symtab, builtin_120_types,
489 Elements(builtin_120_types), instructions);
490 }
491
492
493 void
494 generate_130_constructors(glsl_symbol_table *symtab, exec_list *instructions)
495 {
496 generate_120_constructors(symtab, instructions);
497
498 generate_constructor(symtab, builtin_130_types,
499 Elements(builtin_130_types), instructions);
500 }
501
502
503 void
504 _mesa_glsl_initialize_constructors(exec_list *instructions,
505 struct _mesa_glsl_parse_state *state)
506 {
507 switch (state->language_version) {
508 case 110:
509 generate_110_constructors(state->symbols, instructions);
510 break;
511 case 120:
512 generate_120_constructors(state->symbols, instructions);
513 break;
514 case 130:
515 generate_130_constructors(state->symbols, instructions);
516 break;
517 default:
518 /* error */
519 break;
520 }
521 }
522
523
524 glsl_type::glsl_type(const glsl_type *array, unsigned length) :
525 base_type(GLSL_TYPE_ARRAY),
526 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
527 sampler_type(0),
528 vector_elements(0), matrix_columns(0),
529 name(NULL), length(length)
530 {
531 this->fields.array = array;
532
533 /* Allow a maximum of 10 characters for the array size. This is enough
534 * for 32-bits of ~0. The extra 3 are for the '[', ']', and terminating
535 * NUL.
536 */
537 const unsigned name_length = strlen(array->name) + 10 + 3;
538 char *const n = (char *) malloc(name_length);
539
540 if (length == 0)
541 snprintf(n, name_length, "%s[]", array->name);
542 else
543 snprintf(n, name_length, "%s[%u]", array->name, length);
544
545 this->name = n;
546 }
547
548
549 const glsl_type *
550 glsl_type::get_instance(unsigned base_type, unsigned rows, unsigned columns)
551 {
552 if (base_type == GLSL_TYPE_VOID)
553 return &void_type;
554
555 if ((rows < 1) || (rows > 4) || (columns < 1) || (columns > 4))
556 return error_type;
557
558 /* Treat GLSL vectors as Nx1 matrices.
559 */
560 if (columns == 1) {
561 switch (base_type) {
562 case GLSL_TYPE_UINT:
563 return uint_type + (rows - 1);
564 case GLSL_TYPE_INT:
565 return int_type + (rows - 1);
566 case GLSL_TYPE_FLOAT:
567 return float_type + (rows - 1);
568 case GLSL_TYPE_BOOL:
569 return bool_type + (rows - 1);
570 default:
571 return error_type;
572 }
573 } else {
574 if ((base_type != GLSL_TYPE_FLOAT) || (rows == 1))
575 return error_type;
576
577 /* GLSL matrix types are named mat{COLUMNS}x{ROWS}. Only the following
578 * combinations are valid:
579 *
580 * 1 2 3 4
581 * 1
582 * 2 x x x
583 * 3 x x x
584 * 4 x x x
585 */
586 #define IDX(c,r) (((c-1)*3) + (r-1))
587
588 switch (IDX(columns, rows)) {
589 case IDX(2,2): return mat2_type;
590 case IDX(2,3): return mat2x3_type;
591 case IDX(2,4): return mat2x4_type;
592 case IDX(3,2): return mat3x2_type;
593 case IDX(3,3): return mat3_type;
594 case IDX(3,4): return mat3x4_type;
595 case IDX(4,2): return mat4x2_type;
596 case IDX(4,3): return mat4x3_type;
597 case IDX(4,4): return mat4_type;
598 default: return error_type;
599 }
600 }
601
602 assert(!"Should not get here.");
603 return error_type;
604 }
605
606
607 int
608 glsl_type::array_key_compare(const void *a, const void *b)
609 {
610 const glsl_type *const key1 = (glsl_type *) a;
611 const glsl_type *const key2 = (glsl_type *) b;
612
613 /* Return zero is the types match (there is zero difference) or non-zero
614 * otherwise.
615 */
616 return ((key1->fields.array == key2->fields.array)
617 && (key1->length == key2->length)) ? 0 : 1;
618 }
619
620
621 unsigned
622 glsl_type::array_key_hash(const void *a)
623 {
624 const glsl_type *const key = (glsl_type *) a;
625
626 const struct {
627 const glsl_type *t;
628 unsigned l;
629 char nul;
630 } hash_key = {
631 key->fields.array,
632 key->length,
633 '\0'
634 };
635
636 return hash_table_string_hash(& hash_key);
637 }
638
639
640 const glsl_type *
641 glsl_type::get_array_instance(const glsl_type *base, unsigned array_size)
642 {
643 const glsl_type key(base, array_size);
644
645 if (array_types == NULL) {
646 array_types = hash_table_ctor(64, array_key_hash, array_key_compare);
647 }
648
649 const glsl_type *t = (glsl_type *) hash_table_find(array_types, & key);
650 if (t == NULL) {
651 t = new glsl_type(base, array_size);
652
653 hash_table_insert(array_types, (void *) t, t);
654 }
655
656 assert(t->base_type == GLSL_TYPE_ARRAY);
657 assert(t->length == array_size);
658 assert(t->fields.array == base);
659
660 return t;
661 }