f78d2a6f8d8cae10552366309a5504f20e54916c
[mesa.git] / src / glsl / 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 <stdio.h>
25 #include <stdlib.h>
26 #include "main/core.h" /* for Elements */
27 #include "glsl_symbol_table.h"
28 #include "glsl_parser_extras.h"
29 #include "glsl_types.h"
30 #include "builtin_types.h"
31 extern "C" {
32 #include "program/hash_table.h"
33 }
34
35 hash_table *glsl_type::array_types = NULL;
36 hash_table *glsl_type::record_types = NULL;
37 hash_table *glsl_type::interface_types = NULL;
38 void *glsl_type::mem_ctx = NULL;
39
40 void
41 glsl_type::init_ralloc_type_ctx(void)
42 {
43 if (glsl_type::mem_ctx == NULL) {
44 glsl_type::mem_ctx = ralloc_autofree_context();
45 assert(glsl_type::mem_ctx != NULL);
46 }
47 }
48
49 glsl_type::glsl_type(GLenum gl_type,
50 glsl_base_type base_type, unsigned vector_elements,
51 unsigned matrix_columns, const char *name) :
52 gl_type(gl_type),
53 base_type(base_type),
54 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
55 sampler_type(0), interface_packing(0),
56 vector_elements(vector_elements), matrix_columns(matrix_columns),
57 length(0)
58 {
59 init_ralloc_type_ctx();
60 this->name = ralloc_strdup(this->mem_ctx, name);
61 /* Neither dimension is zero or both dimensions are zero.
62 */
63 assert((vector_elements == 0) == (matrix_columns == 0));
64 memset(& fields, 0, sizeof(fields));
65 }
66
67 glsl_type::glsl_type(GLenum gl_type,
68 enum glsl_sampler_dim dim, bool shadow, bool array,
69 unsigned type, const char *name) :
70 gl_type(gl_type),
71 base_type(GLSL_TYPE_SAMPLER),
72 sampler_dimensionality(dim), sampler_shadow(shadow),
73 sampler_array(array), sampler_type(type), interface_packing(0),
74 vector_elements(0), matrix_columns(0),
75 length(0)
76 {
77 init_ralloc_type_ctx();
78 this->name = ralloc_strdup(this->mem_ctx, name);
79 memset(& fields, 0, sizeof(fields));
80 }
81
82 glsl_type::glsl_type(const glsl_struct_field *fields, unsigned num_fields,
83 const char *name) :
84 gl_type(0),
85 base_type(GLSL_TYPE_STRUCT),
86 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
87 sampler_type(0), interface_packing(0),
88 vector_elements(0), matrix_columns(0),
89 length(num_fields)
90 {
91 unsigned int i;
92
93 init_ralloc_type_ctx();
94 this->name = ralloc_strdup(this->mem_ctx, name);
95 this->fields.structure = ralloc_array(this->mem_ctx,
96 glsl_struct_field, length);
97 for (i = 0; i < length; i++) {
98 this->fields.structure[i].type = fields[i].type;
99 this->fields.structure[i].name = ralloc_strdup(this->fields.structure,
100 fields[i].name);
101 this->fields.structure[i].row_major = fields[i].row_major;
102 }
103 }
104
105 glsl_type::glsl_type(const glsl_struct_field *fields, unsigned num_fields,
106 enum glsl_interface_packing packing, const char *name) :
107 gl_type(0),
108 base_type(GLSL_TYPE_INTERFACE),
109 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
110 sampler_type(0), interface_packing((unsigned) packing),
111 vector_elements(0), matrix_columns(0),
112 length(num_fields)
113 {
114 unsigned int i;
115
116 init_ralloc_type_ctx();
117 this->name = ralloc_strdup(this->mem_ctx, name);
118 this->fields.structure = ralloc_array(this->mem_ctx,
119 glsl_struct_field, length);
120 for (i = 0; i < length; i++) {
121 this->fields.structure[i].type = fields[i].type;
122 this->fields.structure[i].name = ralloc_strdup(this->fields.structure,
123 fields[i].name);
124 this->fields.structure[i].row_major = fields[i].row_major;
125 }
126 }
127
128 static void
129 add_types_to_symbol_table(glsl_symbol_table *symtab,
130 const struct glsl_type *types,
131 unsigned num_types, bool warn,
132 bool skip_1d)
133 {
134 (void) warn;
135
136 for (unsigned i = 0; i < num_types; i++) {
137 if (skip_1d && types[i].base_type == GLSL_TYPE_SAMPLER
138 && types[i].sampler_dimensionality == GLSL_SAMPLER_DIM_1D)
139 continue;
140
141 symtab->add_type(types[i].name, & types[i]);
142 }
143 }
144
145 bool
146 glsl_type::contains_sampler() const
147 {
148 if (this->is_array()) {
149 return this->fields.array->contains_sampler();
150 } else if (this->is_record()) {
151 for (unsigned int i = 0; i < this->length; i++) {
152 if (this->fields.structure[i].type->contains_sampler())
153 return true;
154 }
155 return false;
156 } else {
157 return this->is_sampler();
158 }
159 }
160
161
162 bool
163 glsl_type::contains_integer() const
164 {
165 if (this->is_array()) {
166 return this->fields.array->contains_integer();
167 } else if (this->is_record()) {
168 for (unsigned int i = 0; i < this->length; i++) {
169 if (this->fields.structure[i].type->contains_integer())
170 return true;
171 }
172 return false;
173 } else {
174 return this->is_integer();
175 }
176 }
177
178
179 gl_texture_index
180 glsl_type::sampler_index() const
181 {
182 const glsl_type *const t = (this->is_array()) ? this->fields.array : this;
183
184 assert(t->is_sampler());
185
186 switch (t->sampler_dimensionality) {
187 case GLSL_SAMPLER_DIM_1D:
188 return (t->sampler_array) ? TEXTURE_1D_ARRAY_INDEX : TEXTURE_1D_INDEX;
189 case GLSL_SAMPLER_DIM_2D:
190 return (t->sampler_array) ? TEXTURE_2D_ARRAY_INDEX : TEXTURE_2D_INDEX;
191 case GLSL_SAMPLER_DIM_3D:
192 return TEXTURE_3D_INDEX;
193 case GLSL_SAMPLER_DIM_CUBE:
194 return (t->sampler_array) ? TEXTURE_CUBE_ARRAY_INDEX : TEXTURE_CUBE_INDEX;
195 case GLSL_SAMPLER_DIM_RECT:
196 return TEXTURE_RECT_INDEX;
197 case GLSL_SAMPLER_DIM_BUF:
198 return TEXTURE_BUFFER_INDEX;
199 case GLSL_SAMPLER_DIM_EXTERNAL:
200 return TEXTURE_EXTERNAL_INDEX;
201 default:
202 assert(!"Should not get here.");
203 return TEXTURE_BUFFER_INDEX;
204 }
205 }
206
207 void
208 glsl_type::generate_100ES_types(glsl_symbol_table *symtab)
209 {
210 bool skip_1d = false;
211 add_types_to_symbol_table(symtab, builtin_core_types,
212 Elements(builtin_core_types),
213 false, skip_1d);
214 add_types_to_symbol_table(symtab, builtin_structure_types,
215 Elements(builtin_structure_types),
216 false, skip_1d);
217 add_types_to_symbol_table(symtab, void_type, 1, false, skip_1d);
218 }
219
220 void
221 glsl_type::generate_300ES_types(glsl_symbol_table *symtab)
222 {
223 /* GLSL 3.00 ES types are the same as GLSL 1.30 types, except that 1D
224 * samplers are skipped, and samplerCubeShadow is added.
225 */
226 bool add_deprecated = false;
227 bool skip_1d = true;
228
229 generate_130_types(symtab, add_deprecated, skip_1d);
230
231 add_types_to_symbol_table(symtab, &_samplerCubeShadow_type, 1, false,
232 skip_1d);
233 }
234
235 void
236 glsl_type::generate_110_types(glsl_symbol_table *symtab, bool add_deprecated,
237 bool skip_1d)
238 {
239 generate_100ES_types(symtab);
240
241 add_types_to_symbol_table(symtab, builtin_110_types,
242 Elements(builtin_110_types),
243 false, skip_1d);
244 add_types_to_symbol_table(symtab, &_sampler3D_type, 1, false, skip_1d);
245 if (add_deprecated) {
246 add_types_to_symbol_table(symtab, builtin_110_deprecated_structure_types,
247 Elements(builtin_110_deprecated_structure_types),
248 false, skip_1d);
249 }
250 }
251
252
253 void
254 glsl_type::generate_120_types(glsl_symbol_table *symtab, bool add_deprecated,
255 bool skip_1d)
256 {
257 generate_110_types(symtab, add_deprecated, skip_1d);
258
259 add_types_to_symbol_table(symtab, builtin_120_types,
260 Elements(builtin_120_types), false, skip_1d);
261 }
262
263
264 void
265 glsl_type::generate_130_types(glsl_symbol_table *symtab, bool add_deprecated,
266 bool skip_1d)
267 {
268 generate_120_types(symtab, add_deprecated, skip_1d);
269
270 add_types_to_symbol_table(symtab, builtin_130_types,
271 Elements(builtin_130_types), false, skip_1d);
272 generate_EXT_texture_array_types(symtab, false);
273 }
274
275
276 void
277 glsl_type::generate_140_types(glsl_symbol_table *symtab)
278 {
279 bool skip_1d = false;
280
281 generate_130_types(symtab, false, skip_1d);
282
283 add_types_to_symbol_table(symtab, builtin_140_types,
284 Elements(builtin_140_types), false, skip_1d);
285
286 add_types_to_symbol_table(symtab, builtin_EXT_texture_buffer_object_types,
287 Elements(builtin_EXT_texture_buffer_object_types),
288 false, skip_1d);
289 }
290
291
292 void
293 glsl_type::generate_ARB_texture_rectangle_types(glsl_symbol_table *symtab,
294 bool warn)
295 {
296 bool skip_1d = false;
297
298 add_types_to_symbol_table(symtab, builtin_ARB_texture_rectangle_types,
299 Elements(builtin_ARB_texture_rectangle_types),
300 warn, skip_1d);
301 }
302
303
304 void
305 glsl_type::generate_EXT_texture_array_types(glsl_symbol_table *symtab,
306 bool warn)
307 {
308 bool skip_1d = false;
309
310 add_types_to_symbol_table(symtab, builtin_EXT_texture_array_types,
311 Elements(builtin_EXT_texture_array_types),
312 warn, skip_1d);
313 }
314
315
316 void
317 glsl_type::generate_OES_texture_3D_types(glsl_symbol_table *symtab, bool warn)
318 {
319 bool skip_1d = false;
320
321 add_types_to_symbol_table(symtab, &_sampler3D_type, 1, warn, skip_1d);
322 }
323
324
325 void
326 glsl_type::generate_OES_EGL_image_external_types(glsl_symbol_table *symtab,
327 bool warn)
328 {
329 bool skip_1d = false;
330
331 add_types_to_symbol_table(symtab, builtin_OES_EGL_image_external_types,
332 Elements(builtin_OES_EGL_image_external_types),
333 warn, skip_1d);
334 }
335
336 void
337 glsl_type::generate_ARB_texture_cube_map_array_types(glsl_symbol_table *symtab,
338 bool warn)
339 {
340 bool skip_1d = false;
341
342 add_types_to_symbol_table(symtab, builtin_ARB_texture_cube_map_array_types,
343 Elements(builtin_ARB_texture_cube_map_array_types),
344 warn, skip_1d);
345 }
346
347 void
348 _mesa_glsl_initialize_types(struct _mesa_glsl_parse_state *state)
349 {
350 if (state->es_shader) {
351 switch (state->language_version) {
352 case 100:
353 assert(state->es_shader);
354 glsl_type::generate_100ES_types(state->symbols);
355 break;
356 case 300:
357 glsl_type::generate_300ES_types(state->symbols);
358 break;
359 default:
360 assert(!"Unexpected language version");
361 break;
362 }
363 } else {
364 bool skip_1d = false;
365 switch (state->language_version) {
366 case 110:
367 glsl_type::generate_110_types(state->symbols, true, skip_1d);
368 break;
369 case 120:
370 glsl_type::generate_120_types(state->symbols, true, skip_1d);
371 break;
372 case 130:
373 glsl_type::generate_130_types(state->symbols, true, skip_1d);
374 break;
375 case 140:
376 case 150:
377 glsl_type::generate_140_types(state->symbols);
378 break;
379 default:
380 assert(!"Unexpected language version");
381 break;
382 }
383 }
384
385 if (state->ARB_texture_rectangle_enable ||
386 state->is_version(140, 0)) {
387 glsl_type::generate_ARB_texture_rectangle_types(state->symbols,
388 state->ARB_texture_rectangle_warn);
389 }
390 if (state->OES_texture_3D_enable
391 && state->is_version(0, 100)) {
392 glsl_type::generate_OES_texture_3D_types(state->symbols,
393 state->OES_texture_3D_warn);
394 }
395
396 if (state->EXT_texture_array_enable
397 && !state->is_version(130, 0)) {
398 // These are already included in 130; don't create twice.
399 glsl_type::generate_EXT_texture_array_types(state->symbols,
400 state->EXT_texture_array_warn);
401 }
402
403 /* We cannot check for language_version == 100 here because we need the
404 * types to support fixed-function program generation. But this is fine
405 * since the extension is never enabled for OpenGL contexts.
406 */
407 if (state->OES_EGL_image_external_enable) {
408 glsl_type::generate_OES_EGL_image_external_types(state->symbols,
409 state->OES_EGL_image_external_warn);
410 }
411
412 if (state->ARB_texture_cube_map_array_enable) {
413 glsl_type::generate_ARB_texture_cube_map_array_types(state->symbols,
414 state->ARB_texture_cube_map_array_warn);
415 }
416 }
417
418
419 const glsl_type *glsl_type::get_base_type() const
420 {
421 switch (base_type) {
422 case GLSL_TYPE_UINT:
423 return uint_type;
424 case GLSL_TYPE_INT:
425 return int_type;
426 case GLSL_TYPE_FLOAT:
427 return float_type;
428 case GLSL_TYPE_BOOL:
429 return bool_type;
430 default:
431 return error_type;
432 }
433 }
434
435
436 const glsl_type *glsl_type::get_scalar_type() const
437 {
438 const glsl_type *type = this;
439
440 /* Handle arrays */
441 while (type->base_type == GLSL_TYPE_ARRAY)
442 type = type->fields.array;
443
444 /* Handle vectors and matrices */
445 switch (type->base_type) {
446 case GLSL_TYPE_UINT:
447 return uint_type;
448 case GLSL_TYPE_INT:
449 return int_type;
450 case GLSL_TYPE_FLOAT:
451 return float_type;
452 default:
453 /* Handle everything else */
454 return type;
455 }
456 }
457
458
459 void
460 _mesa_glsl_release_types(void)
461 {
462 if (glsl_type::array_types != NULL) {
463 hash_table_dtor(glsl_type::array_types);
464 glsl_type::array_types = NULL;
465 }
466
467 if (glsl_type::record_types != NULL) {
468 hash_table_dtor(glsl_type::record_types);
469 glsl_type::record_types = NULL;
470 }
471 }
472
473
474 glsl_type::glsl_type(const glsl_type *array, unsigned length) :
475 base_type(GLSL_TYPE_ARRAY),
476 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
477 sampler_type(0), interface_packing(0),
478 vector_elements(0), matrix_columns(0),
479 name(NULL), length(length)
480 {
481 this->fields.array = array;
482 /* Inherit the gl type of the base. The GL type is used for
483 * uniform/statevar handling in Mesa and the arrayness of the type
484 * is represented by the size rather than the type.
485 */
486 this->gl_type = array->gl_type;
487
488 /* Allow a maximum of 10 characters for the array size. This is enough
489 * for 32-bits of ~0. The extra 3 are for the '[', ']', and terminating
490 * NUL.
491 */
492 const unsigned name_length = strlen(array->name) + 10 + 3;
493 char *const n = (char *) ralloc_size(this->mem_ctx, name_length);
494
495 if (length == 0)
496 snprintf(n, name_length, "%s[]", array->name);
497 else
498 snprintf(n, name_length, "%s[%u]", array->name, length);
499
500 this->name = n;
501 }
502
503
504 const glsl_type *
505 glsl_type::get_instance(unsigned base_type, unsigned rows, unsigned columns)
506 {
507 if (base_type == GLSL_TYPE_VOID)
508 return void_type;
509
510 if ((rows < 1) || (rows > 4) || (columns < 1) || (columns > 4))
511 return error_type;
512
513 /* Treat GLSL vectors as Nx1 matrices.
514 */
515 if (columns == 1) {
516 switch (base_type) {
517 case GLSL_TYPE_UINT:
518 return uint_type + (rows - 1);
519 case GLSL_TYPE_INT:
520 return int_type + (rows - 1);
521 case GLSL_TYPE_FLOAT:
522 return float_type + (rows - 1);
523 case GLSL_TYPE_BOOL:
524 return bool_type + (rows - 1);
525 default:
526 return error_type;
527 }
528 } else {
529 if ((base_type != GLSL_TYPE_FLOAT) || (rows == 1))
530 return error_type;
531
532 /* GLSL matrix types are named mat{COLUMNS}x{ROWS}. Only the following
533 * combinations are valid:
534 *
535 * 1 2 3 4
536 * 1
537 * 2 x x x
538 * 3 x x x
539 * 4 x x x
540 */
541 #define IDX(c,r) (((c-1)*3) + (r-1))
542
543 switch (IDX(columns, rows)) {
544 case IDX(2,2): return mat2_type;
545 case IDX(2,3): return mat2x3_type;
546 case IDX(2,4): return mat2x4_type;
547 case IDX(3,2): return mat3x2_type;
548 case IDX(3,3): return mat3_type;
549 case IDX(3,4): return mat3x4_type;
550 case IDX(4,2): return mat4x2_type;
551 case IDX(4,3): return mat4x3_type;
552 case IDX(4,4): return mat4_type;
553 default: return error_type;
554 }
555 }
556
557 assert(!"Should not get here.");
558 return error_type;
559 }
560
561
562 const glsl_type *
563 glsl_type::get_array_instance(const glsl_type *base, unsigned array_size)
564 {
565
566 if (array_types == NULL) {
567 array_types = hash_table_ctor(64, hash_table_string_hash,
568 hash_table_string_compare);
569 }
570
571 /* Generate a name using the base type pointer in the key. This is
572 * done because the name of the base type may not be unique across
573 * shaders. For example, two shaders may have different record types
574 * named 'foo'.
575 */
576 char key[128];
577 snprintf(key, sizeof(key), "%p[%u]", (void *) base, array_size);
578
579 const glsl_type *t = (glsl_type *) hash_table_find(array_types, key);
580 if (t == NULL) {
581 t = new glsl_type(base, array_size);
582
583 hash_table_insert(array_types, (void *) t, ralloc_strdup(mem_ctx, key));
584 }
585
586 assert(t->base_type == GLSL_TYPE_ARRAY);
587 assert(t->length == array_size);
588 assert(t->fields.array == base);
589
590 return t;
591 }
592
593
594 int
595 glsl_type::record_key_compare(const void *a, const void *b)
596 {
597 const glsl_type *const key1 = (glsl_type *) a;
598 const glsl_type *const key2 = (glsl_type *) b;
599
600 /* Return zero is the types match (there is zero difference) or non-zero
601 * otherwise.
602 */
603 if (strcmp(key1->name, key2->name) != 0)
604 return 1;
605
606 if (key1->length != key2->length)
607 return 1;
608
609 if (key1->interface_packing != key2->interface_packing)
610 return 1;
611
612 for (unsigned i = 0; i < key1->length; i++) {
613 if (key1->fields.structure[i].type != key2->fields.structure[i].type)
614 return 1;
615 if (strcmp(key1->fields.structure[i].name,
616 key2->fields.structure[i].name) != 0)
617 return 1;
618 if (key1->fields.structure[i].row_major
619 != key2->fields.structure[i].row_major)
620 return 1;
621 }
622
623 return 0;
624 }
625
626
627 unsigned
628 glsl_type::record_key_hash(const void *a)
629 {
630 const glsl_type *const key = (glsl_type *) a;
631 char hash_key[128];
632 unsigned size = 0;
633
634 size = snprintf(hash_key, sizeof(hash_key), "%08x", key->length);
635
636 for (unsigned i = 0; i < key->length; i++) {
637 if (size >= sizeof(hash_key))
638 break;
639
640 size += snprintf(& hash_key[size], sizeof(hash_key) - size,
641 "%p", (void *) key->fields.structure[i].type);
642 }
643
644 return hash_table_string_hash(& hash_key);
645 }
646
647
648 const glsl_type *
649 glsl_type::get_record_instance(const glsl_struct_field *fields,
650 unsigned num_fields,
651 const char *name)
652 {
653 const glsl_type key(fields, num_fields, name);
654
655 if (record_types == NULL) {
656 record_types = hash_table_ctor(64, record_key_hash, record_key_compare);
657 }
658
659 const glsl_type *t = (glsl_type *) hash_table_find(record_types, & key);
660 if (t == NULL) {
661 t = new glsl_type(fields, num_fields, name);
662
663 hash_table_insert(record_types, (void *) t, t);
664 }
665
666 assert(t->base_type == GLSL_TYPE_STRUCT);
667 assert(t->length == num_fields);
668 assert(strcmp(t->name, name) == 0);
669
670 return t;
671 }
672
673
674 const glsl_type *
675 glsl_type::get_interface_instance(const glsl_struct_field *fields,
676 unsigned num_fields,
677 enum glsl_interface_packing packing,
678 const char *name)
679 {
680 const glsl_type key(fields, num_fields, packing, name);
681
682 if (interface_types == NULL) {
683 interface_types = hash_table_ctor(64, record_key_hash, record_key_compare);
684 }
685
686 const glsl_type *t = (glsl_type *) hash_table_find(interface_types, & key);
687 if (t == NULL) {
688 t = new glsl_type(fields, num_fields, packing, name);
689
690 hash_table_insert(interface_types, (void *) t, t);
691 }
692
693 assert(t->base_type == GLSL_TYPE_INTERFACE);
694 assert(t->length == num_fields);
695 assert(strcmp(t->name, name) == 0);
696
697 return t;
698 }
699
700
701 const glsl_type *
702 glsl_type::field_type(const char *name) const
703 {
704 if (this->base_type != GLSL_TYPE_STRUCT
705 && this->base_type != GLSL_TYPE_INTERFACE)
706 return error_type;
707
708 for (unsigned i = 0; i < this->length; i++) {
709 if (strcmp(name, this->fields.structure[i].name) == 0)
710 return this->fields.structure[i].type;
711 }
712
713 return error_type;
714 }
715
716
717 int
718 glsl_type::field_index(const char *name) const
719 {
720 if (this->base_type != GLSL_TYPE_STRUCT
721 && this->base_type != GLSL_TYPE_INTERFACE)
722 return -1;
723
724 for (unsigned i = 0; i < this->length; i++) {
725 if (strcmp(name, this->fields.structure[i].name) == 0)
726 return i;
727 }
728
729 return -1;
730 }
731
732
733 unsigned
734 glsl_type::component_slots() const
735 {
736 switch (this->base_type) {
737 case GLSL_TYPE_UINT:
738 case GLSL_TYPE_INT:
739 case GLSL_TYPE_FLOAT:
740 case GLSL_TYPE_BOOL:
741 return this->components();
742
743 case GLSL_TYPE_STRUCT:
744 case GLSL_TYPE_INTERFACE: {
745 unsigned size = 0;
746
747 for (unsigned i = 0; i < this->length; i++)
748 size += this->fields.structure[i].type->component_slots();
749
750 return size;
751 }
752
753 case GLSL_TYPE_ARRAY:
754 return this->length * this->fields.array->component_slots();
755
756 case GLSL_TYPE_SAMPLER:
757 case GLSL_TYPE_VOID:
758 case GLSL_TYPE_ERROR:
759 break;
760 }
761
762 return 0;
763 }
764
765 bool
766 glsl_type::can_implicitly_convert_to(const glsl_type *desired) const
767 {
768 if (this == desired)
769 return true;
770
771 /* There is no conversion among matrix types. */
772 if (this->matrix_columns > 1 || desired->matrix_columns > 1)
773 return false;
774
775 /* int and uint can be converted to float. */
776 return desired->is_float()
777 && this->is_integer()
778 && this->vector_elements == desired->vector_elements;
779 }
780
781 unsigned
782 glsl_type::std140_base_alignment(bool row_major) const
783 {
784 /* (1) If the member is a scalar consuming <N> basic machine units, the
785 * base alignment is <N>.
786 *
787 * (2) If the member is a two- or four-component vector with components
788 * consuming <N> basic machine units, the base alignment is 2<N> or
789 * 4<N>, respectively.
790 *
791 * (3) If the member is a three-component vector with components consuming
792 * <N> basic machine units, the base alignment is 4<N>.
793 */
794 if (this->is_scalar() || this->is_vector()) {
795 switch (this->vector_elements) {
796 case 1:
797 return 4;
798 case 2:
799 return 8;
800 case 3:
801 case 4:
802 return 16;
803 }
804 }
805
806 /* (4) If the member is an array of scalars or vectors, the base alignment
807 * and array stride are set to match the base alignment of a single
808 * array element, according to rules (1), (2), and (3), and rounded up
809 * to the base alignment of a vec4. The array may have padding at the
810 * end; the base offset of the member following the array is rounded up
811 * to the next multiple of the base alignment.
812 *
813 * (6) If the member is an array of <S> column-major matrices with <C>
814 * columns and <R> rows, the matrix is stored identically to a row of
815 * <S>*<C> column vectors with <R> components each, according to rule
816 * (4).
817 *
818 * (8) If the member is an array of <S> row-major matrices with <C> columns
819 * and <R> rows, the matrix is stored identically to a row of <S>*<R>
820 * row vectors with <C> components each, according to rule (4).
821 *
822 * (10) If the member is an array of <S> structures, the <S> elements of
823 * the array are laid out in order, according to rule (9).
824 */
825 if (this->is_array()) {
826 if (this->fields.array->is_scalar() ||
827 this->fields.array->is_vector() ||
828 this->fields.array->is_matrix()) {
829 return MAX2(this->fields.array->std140_base_alignment(row_major), 16);
830 } else {
831 assert(this->fields.array->is_record());
832 return this->fields.array->std140_base_alignment(row_major);
833 }
834 }
835
836 /* (5) If the member is a column-major matrix with <C> columns and
837 * <R> rows, the matrix is stored identically to an array of
838 * <C> column vectors with <R> components each, according to
839 * rule (4).
840 *
841 * (7) If the member is a row-major matrix with <C> columns and <R>
842 * rows, the matrix is stored identically to an array of <R>
843 * row vectors with <C> components each, according to rule (4).
844 */
845 if (this->is_matrix()) {
846 const struct glsl_type *vec_type, *array_type;
847 int c = this->matrix_columns;
848 int r = this->vector_elements;
849
850 if (row_major) {
851 vec_type = get_instance(GLSL_TYPE_FLOAT, c, 1);
852 array_type = glsl_type::get_array_instance(vec_type, r);
853 } else {
854 vec_type = get_instance(GLSL_TYPE_FLOAT, r, 1);
855 array_type = glsl_type::get_array_instance(vec_type, c);
856 }
857
858 return array_type->std140_base_alignment(false);
859 }
860
861 /* (9) If the member is a structure, the base alignment of the
862 * structure is <N>, where <N> is the largest base alignment
863 * value of any of its members, and rounded up to the base
864 * alignment of a vec4. The individual members of this
865 * sub-structure are then assigned offsets by applying this set
866 * of rules recursively, where the base offset of the first
867 * member of the sub-structure is equal to the aligned offset
868 * of the structure. The structure may have padding at the end;
869 * the base offset of the member following the sub-structure is
870 * rounded up to the next multiple of the base alignment of the
871 * structure.
872 */
873 if (this->is_record()) {
874 unsigned base_alignment = 16;
875 for (unsigned i = 0; i < this->length; i++) {
876 const struct glsl_type *field_type = this->fields.structure[i].type;
877 base_alignment = MAX2(base_alignment,
878 field_type->std140_base_alignment(row_major));
879 }
880 return base_alignment;
881 }
882
883 assert(!"not reached");
884 return -1;
885 }
886
887 unsigned
888 glsl_type::std140_size(bool row_major) const
889 {
890 /* (1) If the member is a scalar consuming <N> basic machine units, the
891 * base alignment is <N>.
892 *
893 * (2) If the member is a two- or four-component vector with components
894 * consuming <N> basic machine units, the base alignment is 2<N> or
895 * 4<N>, respectively.
896 *
897 * (3) If the member is a three-component vector with components consuming
898 * <N> basic machine units, the base alignment is 4<N>.
899 */
900 if (this->is_scalar() || this->is_vector()) {
901 return this->vector_elements * 4;
902 }
903
904 /* (5) If the member is a column-major matrix with <C> columns and
905 * <R> rows, the matrix is stored identically to an array of
906 * <C> column vectors with <R> components each, according to
907 * rule (4).
908 *
909 * (6) If the member is an array of <S> column-major matrices with <C>
910 * columns and <R> rows, the matrix is stored identically to a row of
911 * <S>*<C> column vectors with <R> components each, according to rule
912 * (4).
913 *
914 * (7) If the member is a row-major matrix with <C> columns and <R>
915 * rows, the matrix is stored identically to an array of <R>
916 * row vectors with <C> components each, according to rule (4).
917 *
918 * (8) If the member is an array of <S> row-major matrices with <C> columns
919 * and <R> rows, the matrix is stored identically to a row of <S>*<R>
920 * row vectors with <C> components each, according to rule (4).
921 */
922 if (this->is_matrix() || (this->is_array() &&
923 this->fields.array->is_matrix())) {
924 const struct glsl_type *element_type;
925 const struct glsl_type *vec_type;
926 unsigned int array_len;
927
928 if (this->is_array()) {
929 element_type = this->fields.array;
930 array_len = this->length;
931 } else {
932 element_type = this;
933 array_len = 1;
934 }
935
936 if (row_major) {
937 vec_type = get_instance(GLSL_TYPE_FLOAT,
938 element_type->matrix_columns, 1);
939 array_len *= element_type->vector_elements;
940 } else {
941 vec_type = get_instance(GLSL_TYPE_FLOAT,
942 element_type->vector_elements, 1);
943 array_len *= element_type->matrix_columns;
944 }
945 const glsl_type *array_type = glsl_type::get_array_instance(vec_type,
946 array_len);
947
948 return array_type->std140_size(false);
949 }
950
951 /* (4) If the member is an array of scalars or vectors, the base alignment
952 * and array stride are set to match the base alignment of a single
953 * array element, according to rules (1), (2), and (3), and rounded up
954 * to the base alignment of a vec4. The array may have padding at the
955 * end; the base offset of the member following the array is rounded up
956 * to the next multiple of the base alignment.
957 *
958 * (10) If the member is an array of <S> structures, the <S> elements of
959 * the array are laid out in order, according to rule (9).
960 */
961 if (this->is_array()) {
962 if (this->fields.array->is_record()) {
963 return this->length * this->fields.array->std140_size(row_major);
964 } else {
965 unsigned element_base_align =
966 this->fields.array->std140_base_alignment(row_major);
967 return this->length * MAX2(element_base_align, 16);
968 }
969 }
970
971 /* (9) If the member is a structure, the base alignment of the
972 * structure is <N>, where <N> is the largest base alignment
973 * value of any of its members, and rounded up to the base
974 * alignment of a vec4. The individual members of this
975 * sub-structure are then assigned offsets by applying this set
976 * of rules recursively, where the base offset of the first
977 * member of the sub-structure is equal to the aligned offset
978 * of the structure. The structure may have padding at the end;
979 * the base offset of the member following the sub-structure is
980 * rounded up to the next multiple of the base alignment of the
981 * structure.
982 */
983 if (this->is_record()) {
984 unsigned size = 0;
985 for (unsigned i = 0; i < this->length; i++) {
986 const struct glsl_type *field_type = this->fields.structure[i].type;
987 unsigned align = field_type->std140_base_alignment(row_major);
988 size = glsl_align(size, align);
989 size += field_type->std140_size(row_major);
990 }
991 size = glsl_align(size,
992 this->fields.structure[0].type->std140_base_alignment(row_major));
993 return size;
994 }
995
996 assert(!"not reached");
997 return -1;
998 }