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