2 * Copyright © 2009 Intel Corporation
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:
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
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.
26 #include "main/core.h" /* for Elements */
27 #include "glsl_symbol_table.h"
28 #include "glsl_parser_extras.h"
29 #include "glsl_types.h"
31 #include "program/hash_table.h"
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
;
40 glsl_type::init_ralloc_type_ctx(void)
42 if (glsl_type::mem_ctx
== NULL
) {
43 glsl_type::mem_ctx
= ralloc_autofree_context();
44 assert(glsl_type::mem_ctx
!= NULL
);
48 glsl_type::glsl_type(GLenum gl_type
,
49 glsl_base_type base_type
, unsigned vector_elements
,
50 unsigned matrix_columns
, const char *name
) :
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
),
58 init_ralloc_type_ctx();
60 this->name
= ralloc_strdup(this->mem_ctx
, name
);
61 /* Neither dimension is zero or both dimensions are zero.
63 assert((vector_elements
== 0) == (matrix_columns
== 0));
64 memset(& fields
, 0, sizeof(fields
));
67 glsl_type::glsl_type(GLenum gl_type
,
68 enum glsl_sampler_dim dim
, bool shadow
, bool array
,
69 unsigned type
, const char *name
) :
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),
77 init_ralloc_type_ctx();
79 this->name
= ralloc_strdup(this->mem_ctx
, name
);
80 memset(& fields
, 0, sizeof(fields
));
83 glsl_type::glsl_type(const glsl_struct_field
*fields
, unsigned num_fields
,
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),
94 init_ralloc_type_ctx();
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
,
103 this->fields
.structure
[i
].location
= fields
[i
].location
;
104 this->fields
.structure
[i
].interpolation
= fields
[i
].interpolation
;
105 this->fields
.structure
[i
].centroid
= fields
[i
].centroid
;
106 this->fields
.structure
[i
].sample
= fields
[i
].sample
;
107 this->fields
.structure
[i
].row_major
= fields
[i
].row_major
;
111 glsl_type::glsl_type(const glsl_struct_field
*fields
, unsigned num_fields
,
112 enum glsl_interface_packing packing
, const char *name
) :
114 base_type(GLSL_TYPE_INTERFACE
),
115 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
116 sampler_type(0), interface_packing((unsigned) packing
),
117 vector_elements(0), matrix_columns(0),
122 init_ralloc_type_ctx();
123 assert(name
!= NULL
);
124 this->name
= ralloc_strdup(this->mem_ctx
, name
);
125 this->fields
.structure
= ralloc_array(this->mem_ctx
,
126 glsl_struct_field
, length
);
127 for (i
= 0; i
< length
; i
++) {
128 this->fields
.structure
[i
].type
= fields
[i
].type
;
129 this->fields
.structure
[i
].name
= ralloc_strdup(this->fields
.structure
,
131 this->fields
.structure
[i
].location
= fields
[i
].location
;
132 this->fields
.structure
[i
].interpolation
= fields
[i
].interpolation
;
133 this->fields
.structure
[i
].centroid
= fields
[i
].centroid
;
134 this->fields
.structure
[i
].sample
= fields
[i
].sample
;
135 this->fields
.structure
[i
].row_major
= fields
[i
].row_major
;
141 glsl_type::contains_sampler() const
143 if (this->is_array()) {
144 return this->fields
.array
->contains_sampler();
145 } else if (this->is_record()) {
146 for (unsigned int i
= 0; i
< this->length
; i
++) {
147 if (this->fields
.structure
[i
].type
->contains_sampler())
152 return this->is_sampler();
158 glsl_type::contains_integer() const
160 if (this->is_array()) {
161 return this->fields
.array
->contains_integer();
162 } else if (this->is_record()) {
163 for (unsigned int i
= 0; i
< this->length
; i
++) {
164 if (this->fields
.structure
[i
].type
->contains_integer())
169 return this->is_integer();
174 glsl_type::contains_opaque() const {
176 case GLSL_TYPE_SAMPLER
:
177 case GLSL_TYPE_ATOMIC_UINT
:
179 case GLSL_TYPE_ARRAY
:
180 return element_type()->contains_opaque();
181 case GLSL_TYPE_STRUCT
:
182 for (unsigned int i
= 0; i
< length
; i
++) {
183 if (fields
.structure
[i
].type
->contains_opaque())
193 glsl_type::sampler_index() const
195 const glsl_type
*const t
= (this->is_array()) ? this->fields
.array
: this;
197 assert(t
->is_sampler());
199 switch (t
->sampler_dimensionality
) {
200 case GLSL_SAMPLER_DIM_1D
:
201 return (t
->sampler_array
) ? TEXTURE_1D_ARRAY_INDEX
: TEXTURE_1D_INDEX
;
202 case GLSL_SAMPLER_DIM_2D
:
203 return (t
->sampler_array
) ? TEXTURE_2D_ARRAY_INDEX
: TEXTURE_2D_INDEX
;
204 case GLSL_SAMPLER_DIM_3D
:
205 return TEXTURE_3D_INDEX
;
206 case GLSL_SAMPLER_DIM_CUBE
:
207 return (t
->sampler_array
) ? TEXTURE_CUBE_ARRAY_INDEX
: TEXTURE_CUBE_INDEX
;
208 case GLSL_SAMPLER_DIM_RECT
:
209 return TEXTURE_RECT_INDEX
;
210 case GLSL_SAMPLER_DIM_BUF
:
211 return TEXTURE_BUFFER_INDEX
;
212 case GLSL_SAMPLER_DIM_EXTERNAL
:
213 return TEXTURE_EXTERNAL_INDEX
;
214 case GLSL_SAMPLER_DIM_MS
:
215 return (t
->sampler_array
) ? TEXTURE_2D_MULTISAMPLE_ARRAY_INDEX
: TEXTURE_2D_MULTISAMPLE_INDEX
;
217 assert(!"Should not get here.");
218 return TEXTURE_BUFFER_INDEX
;
223 const glsl_type
*glsl_type::get_base_type() const
230 case GLSL_TYPE_FLOAT
:
240 const glsl_type
*glsl_type::get_scalar_type() const
242 const glsl_type
*type
= this;
245 while (type
->base_type
== GLSL_TYPE_ARRAY
)
246 type
= type
->fields
.array
;
248 /* Handle vectors and matrices */
249 switch (type
->base_type
) {
254 case GLSL_TYPE_FLOAT
:
259 /* Handle everything else */
266 _mesa_glsl_release_types(void)
268 if (glsl_type::array_types
!= NULL
) {
269 hash_table_dtor(glsl_type::array_types
);
270 glsl_type::array_types
= NULL
;
273 if (glsl_type::record_types
!= NULL
) {
274 hash_table_dtor(glsl_type::record_types
);
275 glsl_type::record_types
= NULL
;
280 glsl_type::glsl_type(const glsl_type
*array
, unsigned length
) :
281 base_type(GLSL_TYPE_ARRAY
),
282 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
283 sampler_type(0), interface_packing(0),
284 vector_elements(0), matrix_columns(0),
285 name(NULL
), length(length
)
287 this->fields
.array
= array
;
288 /* Inherit the gl type of the base. The GL type is used for
289 * uniform/statevar handling in Mesa and the arrayness of the type
290 * is represented by the size rather than the type.
292 this->gl_type
= array
->gl_type
;
294 /* Allow a maximum of 10 characters for the array size. This is enough
295 * for 32-bits of ~0. The extra 3 are for the '[', ']', and terminating
298 const unsigned name_length
= strlen(array
->name
) + 10 + 3;
299 char *const n
= (char *) ralloc_size(this->mem_ctx
, name_length
);
302 snprintf(n
, name_length
, "%s[]", array
->name
);
304 /* insert outermost dimensions in the correct spot
305 * otherwise the dimension order will be backwards
307 const char *pos
= strchr(array
->name
, '[');
309 int idx
= pos
- array
->name
;
310 snprintf(n
, idx
+1, "%s", array
->name
);
311 snprintf(n
+ idx
, name_length
- idx
, "[%u]%s",
312 length
, array
->name
+ idx
);
314 snprintf(n
, name_length
, "%s[%u]", array
->name
, length
);
323 glsl_type::vec(unsigned components
)
325 if (components
== 0 || components
> 4)
328 static const glsl_type
*const ts
[] = {
329 float_type
, vec2_type
, vec3_type
, vec4_type
331 return ts
[components
- 1];
336 glsl_type::ivec(unsigned components
)
338 if (components
== 0 || components
> 4)
341 static const glsl_type
*const ts
[] = {
342 int_type
, ivec2_type
, ivec3_type
, ivec4_type
344 return ts
[components
- 1];
349 glsl_type::uvec(unsigned components
)
351 if (components
== 0 || components
> 4)
354 static const glsl_type
*const ts
[] = {
355 uint_type
, uvec2_type
, uvec3_type
, uvec4_type
357 return ts
[components
- 1];
362 glsl_type::bvec(unsigned components
)
364 if (components
== 0 || components
> 4)
367 static const glsl_type
*const ts
[] = {
368 bool_type
, bvec2_type
, bvec3_type
, bvec4_type
370 return ts
[components
- 1];
375 glsl_type::get_instance(unsigned base_type
, unsigned rows
, unsigned columns
)
377 if (base_type
== GLSL_TYPE_VOID
)
380 if ((rows
< 1) || (rows
> 4) || (columns
< 1) || (columns
> 4))
383 /* Treat GLSL vectors as Nx1 matrices.
391 case GLSL_TYPE_FLOAT
:
399 if ((base_type
!= GLSL_TYPE_FLOAT
) || (rows
== 1))
402 /* GLSL matrix types are named mat{COLUMNS}x{ROWS}. Only the following
403 * combinations are valid:
411 #define IDX(c,r) (((c-1)*3) + (r-1))
413 switch (IDX(columns
, rows
)) {
414 case IDX(2,2): return mat2_type
;
415 case IDX(2,3): return mat2x3_type
;
416 case IDX(2,4): return mat2x4_type
;
417 case IDX(3,2): return mat3x2_type
;
418 case IDX(3,3): return mat3_type
;
419 case IDX(3,4): return mat3x4_type
;
420 case IDX(4,2): return mat4x2_type
;
421 case IDX(4,3): return mat4x3_type
;
422 case IDX(4,4): return mat4_type
;
423 default: return error_type
;
427 assert(!"Should not get here.");
433 glsl_type::get_array_instance(const glsl_type
*base
, unsigned array_size
)
436 if (array_types
== NULL
) {
437 array_types
= hash_table_ctor(64, hash_table_string_hash
,
438 hash_table_string_compare
);
441 /* Generate a name using the base type pointer in the key. This is
442 * done because the name of the base type may not be unique across
443 * shaders. For example, two shaders may have different record types
447 snprintf(key
, sizeof(key
), "%p[%u]", (void *) base
, array_size
);
449 const glsl_type
*t
= (glsl_type
*) hash_table_find(array_types
, key
);
451 t
= new glsl_type(base
, array_size
);
453 hash_table_insert(array_types
, (void *) t
, ralloc_strdup(mem_ctx
, key
));
456 assert(t
->base_type
== GLSL_TYPE_ARRAY
);
457 assert(t
->length
== array_size
);
458 assert(t
->fields
.array
== base
);
465 glsl_type::record_compare(const glsl_type
*b
) const
467 if (this->length
!= b
->length
)
470 if (this->interface_packing
!= b
->interface_packing
)
473 for (unsigned i
= 0; i
< this->length
; i
++) {
474 if (this->fields
.structure
[i
].type
!= b
->fields
.structure
[i
].type
)
476 if (strcmp(this->fields
.structure
[i
].name
,
477 b
->fields
.structure
[i
].name
) != 0)
479 if (this->fields
.structure
[i
].row_major
480 != b
->fields
.structure
[i
].row_major
)
482 if (this->fields
.structure
[i
].location
483 != b
->fields
.structure
[i
].location
)
485 if (this->fields
.structure
[i
].interpolation
486 != b
->fields
.structure
[i
].interpolation
)
488 if (this->fields
.structure
[i
].centroid
489 != b
->fields
.structure
[i
].centroid
)
491 if (this->fields
.structure
[i
].sample
492 != b
->fields
.structure
[i
].sample
)
501 glsl_type::record_key_compare(const void *a
, const void *b
)
503 const glsl_type
*const key1
= (glsl_type
*) a
;
504 const glsl_type
*const key2
= (glsl_type
*) b
;
506 /* Return zero is the types match (there is zero difference) or non-zero
509 if (strcmp(key1
->name
, key2
->name
) != 0)
512 return !key1
->record_compare(key2
);
517 glsl_type::record_key_hash(const void *a
)
519 const glsl_type
*const key
= (glsl_type
*) a
;
523 size
= snprintf(hash_key
, sizeof(hash_key
), "%08x", key
->length
);
525 for (unsigned i
= 0; i
< key
->length
; i
++) {
526 if (size
>= sizeof(hash_key
))
529 size
+= snprintf(& hash_key
[size
], sizeof(hash_key
) - size
,
530 "%p", (void *) key
->fields
.structure
[i
].type
);
533 return hash_table_string_hash(& hash_key
);
538 glsl_type::get_record_instance(const glsl_struct_field
*fields
,
542 const glsl_type
key(fields
, num_fields
, name
);
544 if (record_types
== NULL
) {
545 record_types
= hash_table_ctor(64, record_key_hash
, record_key_compare
);
548 const glsl_type
*t
= (glsl_type
*) hash_table_find(record_types
, & key
);
550 t
= new glsl_type(fields
, num_fields
, name
);
552 hash_table_insert(record_types
, (void *) t
, t
);
555 assert(t
->base_type
== GLSL_TYPE_STRUCT
);
556 assert(t
->length
== num_fields
);
557 assert(strcmp(t
->name
, name
) == 0);
564 glsl_type::get_interface_instance(const glsl_struct_field
*fields
,
566 enum glsl_interface_packing packing
,
567 const char *block_name
)
569 const glsl_type
key(fields
, num_fields
, packing
, block_name
);
571 if (interface_types
== NULL
) {
572 interface_types
= hash_table_ctor(64, record_key_hash
, record_key_compare
);
575 const glsl_type
*t
= (glsl_type
*) hash_table_find(interface_types
, & key
);
577 t
= new glsl_type(fields
, num_fields
, packing
, block_name
);
579 hash_table_insert(interface_types
, (void *) t
, t
);
582 assert(t
->base_type
== GLSL_TYPE_INTERFACE
);
583 assert(t
->length
== num_fields
);
584 assert(strcmp(t
->name
, block_name
) == 0);
591 glsl_type::field_type(const char *name
) const
593 if (this->base_type
!= GLSL_TYPE_STRUCT
594 && this->base_type
!= GLSL_TYPE_INTERFACE
)
597 for (unsigned i
= 0; i
< this->length
; i
++) {
598 if (strcmp(name
, this->fields
.structure
[i
].name
) == 0)
599 return this->fields
.structure
[i
].type
;
607 glsl_type::field_index(const char *name
) const
609 if (this->base_type
!= GLSL_TYPE_STRUCT
610 && this->base_type
!= GLSL_TYPE_INTERFACE
)
613 for (unsigned i
= 0; i
< this->length
; i
++) {
614 if (strcmp(name
, this->fields
.structure
[i
].name
) == 0)
623 glsl_type::component_slots() const
625 switch (this->base_type
) {
628 case GLSL_TYPE_FLOAT
:
630 return this->components();
632 case GLSL_TYPE_STRUCT
:
633 case GLSL_TYPE_INTERFACE
: {
636 for (unsigned i
= 0; i
< this->length
; i
++)
637 size
+= this->fields
.structure
[i
].type
->component_slots();
642 case GLSL_TYPE_ARRAY
:
643 return this->length
* this->fields
.array
->component_slots();
645 case GLSL_TYPE_SAMPLER
:
646 case GLSL_TYPE_ATOMIC_UINT
:
648 case GLSL_TYPE_ERROR
:
656 glsl_type::can_implicitly_convert_to(const glsl_type
*desired
) const
661 /* There is no conversion among matrix types. */
662 if (this->matrix_columns
> 1 || desired
->matrix_columns
> 1)
665 /* int and uint can be converted to float. */
666 return desired
->is_float()
667 && this->is_integer()
668 && this->vector_elements
== desired
->vector_elements
;
672 glsl_type::std140_base_alignment(bool row_major
) const
674 /* (1) If the member is a scalar consuming <N> basic machine units, the
675 * base alignment is <N>.
677 * (2) If the member is a two- or four-component vector with components
678 * consuming <N> basic machine units, the base alignment is 2<N> or
679 * 4<N>, respectively.
681 * (3) If the member is a three-component vector with components consuming
682 * <N> basic machine units, the base alignment is 4<N>.
684 if (this->is_scalar() || this->is_vector()) {
685 switch (this->vector_elements
) {
696 /* (4) If the member is an array of scalars or vectors, the base alignment
697 * and array stride are set to match the base alignment of a single
698 * array element, according to rules (1), (2), and (3), and rounded up
699 * to the base alignment of a vec4. The array may have padding at the
700 * end; the base offset of the member following the array is rounded up
701 * to the next multiple of the base alignment.
703 * (6) If the member is an array of <S> column-major matrices with <C>
704 * columns and <R> rows, the matrix is stored identically to a row of
705 * <S>*<C> column vectors with <R> components each, according to rule
708 * (8) If the member is an array of <S> row-major matrices with <C> columns
709 * and <R> rows, the matrix is stored identically to a row of <S>*<R>
710 * row vectors with <C> components each, according to rule (4).
712 * (10) If the member is an array of <S> structures, the <S> elements of
713 * the array are laid out in order, according to rule (9).
715 if (this->is_array()) {
716 if (this->fields
.array
->is_scalar() ||
717 this->fields
.array
->is_vector() ||
718 this->fields
.array
->is_matrix()) {
719 return MAX2(this->fields
.array
->std140_base_alignment(row_major
), 16);
721 assert(this->fields
.array
->is_record());
722 return this->fields
.array
->std140_base_alignment(row_major
);
726 /* (5) If the member is a column-major matrix with <C> columns and
727 * <R> rows, the matrix is stored identically to an array of
728 * <C> column vectors with <R> components each, according to
731 * (7) If the member is a row-major matrix with <C> columns and <R>
732 * rows, the matrix is stored identically to an array of <R>
733 * row vectors with <C> components each, according to rule (4).
735 if (this->is_matrix()) {
736 const struct glsl_type
*vec_type
, *array_type
;
737 int c
= this->matrix_columns
;
738 int r
= this->vector_elements
;
741 vec_type
= get_instance(GLSL_TYPE_FLOAT
, c
, 1);
742 array_type
= glsl_type::get_array_instance(vec_type
, r
);
744 vec_type
= get_instance(GLSL_TYPE_FLOAT
, r
, 1);
745 array_type
= glsl_type::get_array_instance(vec_type
, c
);
748 return array_type
->std140_base_alignment(false);
751 /* (9) If the member is a structure, the base alignment of the
752 * structure is <N>, where <N> is the largest base alignment
753 * value of any of its members, and rounded up to the base
754 * alignment of a vec4. The individual members of this
755 * sub-structure are then assigned offsets by applying this set
756 * of rules recursively, where the base offset of the first
757 * member of the sub-structure is equal to the aligned offset
758 * of the structure. The structure may have padding at the end;
759 * the base offset of the member following the sub-structure is
760 * rounded up to the next multiple of the base alignment of the
763 if (this->is_record()) {
764 unsigned base_alignment
= 16;
765 for (unsigned i
= 0; i
< this->length
; i
++) {
766 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
767 base_alignment
= MAX2(base_alignment
,
768 field_type
->std140_base_alignment(row_major
));
770 return base_alignment
;
773 assert(!"not reached");
778 glsl_type::std140_size(bool row_major
) const
780 /* (1) If the member is a scalar consuming <N> basic machine units, the
781 * base alignment is <N>.
783 * (2) If the member is a two- or four-component vector with components
784 * consuming <N> basic machine units, the base alignment is 2<N> or
785 * 4<N>, respectively.
787 * (3) If the member is a three-component vector with components consuming
788 * <N> basic machine units, the base alignment is 4<N>.
790 if (this->is_scalar() || this->is_vector()) {
791 return this->vector_elements
* 4;
794 /* (5) If the member is a column-major matrix with <C> columns and
795 * <R> rows, the matrix is stored identically to an array of
796 * <C> column vectors with <R> components each, according to
799 * (6) If the member is an array of <S> column-major matrices with <C>
800 * columns and <R> rows, the matrix is stored identically to a row of
801 * <S>*<C> column vectors with <R> components each, according to rule
804 * (7) If the member is a row-major matrix with <C> columns and <R>
805 * rows, the matrix is stored identically to an array of <R>
806 * row vectors with <C> components each, according to rule (4).
808 * (8) If the member is an array of <S> row-major matrices with <C> columns
809 * and <R> rows, the matrix is stored identically to a row of <S>*<R>
810 * row vectors with <C> components each, according to rule (4).
812 if (this->is_matrix() || (this->is_array() &&
813 this->fields
.array
->is_matrix())) {
814 const struct glsl_type
*element_type
;
815 const struct glsl_type
*vec_type
;
816 unsigned int array_len
;
818 if (this->is_array()) {
819 element_type
= this->fields
.array
;
820 array_len
= this->length
;
827 vec_type
= get_instance(GLSL_TYPE_FLOAT
,
828 element_type
->matrix_columns
, 1);
829 array_len
*= element_type
->vector_elements
;
831 vec_type
= get_instance(GLSL_TYPE_FLOAT
,
832 element_type
->vector_elements
, 1);
833 array_len
*= element_type
->matrix_columns
;
835 const glsl_type
*array_type
= glsl_type::get_array_instance(vec_type
,
838 return array_type
->std140_size(false);
841 /* (4) If the member is an array of scalars or vectors, the base alignment
842 * and array stride are set to match the base alignment of a single
843 * array element, according to rules (1), (2), and (3), and rounded up
844 * to the base alignment of a vec4. The array may have padding at the
845 * end; the base offset of the member following the array is rounded up
846 * to the next multiple of the base alignment.
848 * (10) If the member is an array of <S> structures, the <S> elements of
849 * the array are laid out in order, according to rule (9).
851 if (this->is_array()) {
852 if (this->fields
.array
->is_record()) {
853 return this->length
* this->fields
.array
->std140_size(row_major
);
855 unsigned element_base_align
=
856 this->fields
.array
->std140_base_alignment(row_major
);
857 return this->length
* MAX2(element_base_align
, 16);
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
873 if (this->is_record()) {
875 for (unsigned i
= 0; i
< this->length
; i
++) {
876 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
877 unsigned align
= field_type
->std140_base_alignment(row_major
);
878 size
= glsl_align(size
, align
);
879 size
+= field_type
->std140_size(row_major
);
881 size
= glsl_align(size
,
882 this->fields
.structure
[0].type
->std140_base_alignment(row_major
));
886 assert(!"not reached");
892 glsl_type::count_attribute_slots() const
894 /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
896 * "A scalar input counts the same amount against this limit as a vec4,
897 * so applications may want to consider packing groups of four
898 * unrelated float inputs together into a vector to better utilize the
899 * capabilities of the underlying hardware. A matrix input will use up
900 * multiple locations. The number of locations used will equal the
901 * number of columns in the matrix."
903 * The spec does not explicitly say how arrays are counted. However, it
904 * should be safe to assume the total number of slots consumed by an array
905 * is the number of entries in the array multiplied by the number of slots
906 * consumed by a single element of the array.
908 * The spec says nothing about how structs are counted, because vertex
909 * attributes are not allowed to be (or contain) structs. However, Mesa
910 * allows varying structs, the number of varying slots taken up by a
911 * varying struct is simply equal to the sum of the number of slots taken
912 * up by each element.
914 switch (this->base_type
) {
917 case GLSL_TYPE_FLOAT
:
919 return this->matrix_columns
;
921 case GLSL_TYPE_STRUCT
:
922 case GLSL_TYPE_INTERFACE
: {
925 for (unsigned i
= 0; i
< this->length
; i
++)
926 size
+= this->fields
.structure
[i
].type
->count_attribute_slots();
931 case GLSL_TYPE_ARRAY
:
932 return this->length
* this->fields
.array
->count_attribute_slots();
934 case GLSL_TYPE_SAMPLER
:
935 case GLSL_TYPE_ATOMIC_UINT
:
937 case GLSL_TYPE_ERROR
:
941 assert(!"Unexpected type in count_attribute_slots()");
947 glsl_type::sampler_coordinate_components() const
949 assert(is_sampler());
953 switch (sampler_dimensionality
) {
954 case GLSL_SAMPLER_DIM_1D
:
955 case GLSL_SAMPLER_DIM_BUF
:
958 case GLSL_SAMPLER_DIM_2D
:
959 case GLSL_SAMPLER_DIM_RECT
:
960 case GLSL_SAMPLER_DIM_MS
:
961 case GLSL_SAMPLER_DIM_EXTERNAL
:
964 case GLSL_SAMPLER_DIM_3D
:
965 case GLSL_SAMPLER_DIM_CUBE
:
969 assert(!"Should not get here.");
974 /* Array textures need an additional component for the array index. */