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