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