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
, glsl_base_type base_type
,
68 enum glsl_sampler_dim dim
, bool shadow
, bool array
,
69 unsigned type
, const char *name
) :
72 sampler_dimensionality(dim
), sampler_shadow(shadow
),
73 sampler_array(array
), sampler_type(type
), interface_packing(0),
76 init_ralloc_type_ctx();
78 this->name
= ralloc_strdup(this->mem_ctx
, name
);
79 memset(& fields
, 0, sizeof(fields
));
81 if (base_type
== GLSL_TYPE_SAMPLER
) {
82 /* Samplers take no storage whatsoever. */
83 matrix_columns
= vector_elements
= 0;
85 matrix_columns
= vector_elements
= 1;
89 glsl_type::glsl_type(const glsl_struct_field
*fields
, unsigned num_fields
,
92 base_type(GLSL_TYPE_STRUCT
),
93 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
94 sampler_type(0), interface_packing(0),
95 vector_elements(0), matrix_columns(0),
100 init_ralloc_type_ctx();
101 assert(name
!= NULL
);
102 this->name
= ralloc_strdup(this->mem_ctx
, name
);
103 this->fields
.structure
= ralloc_array(this->mem_ctx
,
104 glsl_struct_field
, length
);
105 for (i
= 0; i
< length
; i
++) {
106 this->fields
.structure
[i
].type
= fields
[i
].type
;
107 this->fields
.structure
[i
].name
= ralloc_strdup(this->fields
.structure
,
109 this->fields
.structure
[i
].location
= fields
[i
].location
;
110 this->fields
.structure
[i
].interpolation
= fields
[i
].interpolation
;
111 this->fields
.structure
[i
].centroid
= fields
[i
].centroid
;
112 this->fields
.structure
[i
].sample
= fields
[i
].sample
;
113 this->fields
.structure
[i
].row_major
= fields
[i
].row_major
;
117 glsl_type::glsl_type(const glsl_struct_field
*fields
, unsigned num_fields
,
118 enum glsl_interface_packing packing
, const char *name
) :
120 base_type(GLSL_TYPE_INTERFACE
),
121 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
122 sampler_type(0), interface_packing((unsigned) packing
),
123 vector_elements(0), matrix_columns(0),
128 init_ralloc_type_ctx();
129 assert(name
!= NULL
);
130 this->name
= ralloc_strdup(this->mem_ctx
, name
);
131 this->fields
.structure
= ralloc_array(this->mem_ctx
,
132 glsl_struct_field
, length
);
133 for (i
= 0; i
< length
; i
++) {
134 this->fields
.structure
[i
].type
= fields
[i
].type
;
135 this->fields
.structure
[i
].name
= ralloc_strdup(this->fields
.structure
,
137 this->fields
.structure
[i
].location
= fields
[i
].location
;
138 this->fields
.structure
[i
].interpolation
= fields
[i
].interpolation
;
139 this->fields
.structure
[i
].centroid
= fields
[i
].centroid
;
140 this->fields
.structure
[i
].sample
= fields
[i
].sample
;
141 this->fields
.structure
[i
].row_major
= fields
[i
].row_major
;
147 glsl_type::contains_sampler() const
149 if (this->is_array()) {
150 return this->fields
.array
->contains_sampler();
151 } else if (this->is_record()) {
152 for (unsigned int i
= 0; i
< this->length
; i
++) {
153 if (this->fields
.structure
[i
].type
->contains_sampler())
158 return this->is_sampler();
164 glsl_type::contains_integer() const
166 if (this->is_array()) {
167 return this->fields
.array
->contains_integer();
168 } else if (this->is_record()) {
169 for (unsigned int i
= 0; i
< this->length
; i
++) {
170 if (this->fields
.structure
[i
].type
->contains_integer())
175 return this->is_integer();
180 glsl_type::contains_opaque() const {
182 case GLSL_TYPE_SAMPLER
:
183 case GLSL_TYPE_IMAGE
:
184 case GLSL_TYPE_ATOMIC_UINT
:
186 case GLSL_TYPE_ARRAY
:
187 return element_type()->contains_opaque();
188 case GLSL_TYPE_STRUCT
:
189 for (unsigned int i
= 0; i
< length
; i
++) {
190 if (fields
.structure
[i
].type
->contains_opaque())
200 glsl_type::sampler_index() const
202 const glsl_type
*const t
= (this->is_array()) ? this->fields
.array
: this;
204 assert(t
->is_sampler());
206 switch (t
->sampler_dimensionality
) {
207 case GLSL_SAMPLER_DIM_1D
:
208 return (t
->sampler_array
) ? TEXTURE_1D_ARRAY_INDEX
: TEXTURE_1D_INDEX
;
209 case GLSL_SAMPLER_DIM_2D
:
210 return (t
->sampler_array
) ? TEXTURE_2D_ARRAY_INDEX
: TEXTURE_2D_INDEX
;
211 case GLSL_SAMPLER_DIM_3D
:
212 return TEXTURE_3D_INDEX
;
213 case GLSL_SAMPLER_DIM_CUBE
:
214 return (t
->sampler_array
) ? TEXTURE_CUBE_ARRAY_INDEX
: TEXTURE_CUBE_INDEX
;
215 case GLSL_SAMPLER_DIM_RECT
:
216 return TEXTURE_RECT_INDEX
;
217 case GLSL_SAMPLER_DIM_BUF
:
218 return TEXTURE_BUFFER_INDEX
;
219 case GLSL_SAMPLER_DIM_EXTERNAL
:
220 return TEXTURE_EXTERNAL_INDEX
;
221 case GLSL_SAMPLER_DIM_MS
:
222 return (t
->sampler_array
) ? TEXTURE_2D_MULTISAMPLE_ARRAY_INDEX
: TEXTURE_2D_MULTISAMPLE_INDEX
;
224 assert(!"Should not get here.");
225 return TEXTURE_BUFFER_INDEX
;
230 glsl_type::contains_image() const
232 if (this->is_array()) {
233 return this->fields
.array
->contains_image();
234 } else if (this->is_record()) {
235 for (unsigned int i
= 0; i
< this->length
; i
++) {
236 if (this->fields
.structure
[i
].type
->contains_image())
241 return this->is_image();
245 const glsl_type
*glsl_type::get_base_type() const
252 case GLSL_TYPE_FLOAT
:
262 const glsl_type
*glsl_type::get_scalar_type() const
264 const glsl_type
*type
= this;
267 while (type
->base_type
== GLSL_TYPE_ARRAY
)
268 type
= type
->fields
.array
;
270 /* Handle vectors and matrices */
271 switch (type
->base_type
) {
276 case GLSL_TYPE_FLOAT
:
281 /* Handle everything else */
288 _mesa_glsl_release_types(void)
290 if (glsl_type::array_types
!= NULL
) {
291 hash_table_dtor(glsl_type::array_types
);
292 glsl_type::array_types
= NULL
;
295 if (glsl_type::record_types
!= NULL
) {
296 hash_table_dtor(glsl_type::record_types
);
297 glsl_type::record_types
= NULL
;
302 glsl_type::glsl_type(const glsl_type
*array
, unsigned length
) :
303 base_type(GLSL_TYPE_ARRAY
),
304 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
305 sampler_type(0), interface_packing(0),
306 vector_elements(0), matrix_columns(0),
307 name(NULL
), length(length
)
309 this->fields
.array
= array
;
310 /* Inherit the gl type of the base. The GL type is used for
311 * uniform/statevar handling in Mesa and the arrayness of the type
312 * is represented by the size rather than the type.
314 this->gl_type
= array
->gl_type
;
316 /* Allow a maximum of 10 characters for the array size. This is enough
317 * for 32-bits of ~0. The extra 3 are for the '[', ']', and terminating
320 const unsigned name_length
= strlen(array
->name
) + 10 + 3;
321 char *const n
= (char *) ralloc_size(this->mem_ctx
, name_length
);
324 snprintf(n
, name_length
, "%s[]", array
->name
);
326 /* insert outermost dimensions in the correct spot
327 * otherwise the dimension order will be backwards
329 const char *pos
= strchr(array
->name
, '[');
331 int idx
= pos
- array
->name
;
332 snprintf(n
, idx
+1, "%s", array
->name
);
333 snprintf(n
+ idx
, name_length
- idx
, "[%u]%s",
334 length
, array
->name
+ idx
);
336 snprintf(n
, name_length
, "%s[%u]", array
->name
, length
);
345 glsl_type::vec(unsigned components
)
347 if (components
== 0 || components
> 4)
350 static const glsl_type
*const ts
[] = {
351 float_type
, vec2_type
, vec3_type
, vec4_type
353 return ts
[components
- 1];
358 glsl_type::ivec(unsigned components
)
360 if (components
== 0 || components
> 4)
363 static const glsl_type
*const ts
[] = {
364 int_type
, ivec2_type
, ivec3_type
, ivec4_type
366 return ts
[components
- 1];
371 glsl_type::uvec(unsigned components
)
373 if (components
== 0 || components
> 4)
376 static const glsl_type
*const ts
[] = {
377 uint_type
, uvec2_type
, uvec3_type
, uvec4_type
379 return ts
[components
- 1];
384 glsl_type::bvec(unsigned components
)
386 if (components
== 0 || components
> 4)
389 static const glsl_type
*const ts
[] = {
390 bool_type
, bvec2_type
, bvec3_type
, bvec4_type
392 return ts
[components
- 1];
397 glsl_type::get_instance(unsigned base_type
, unsigned rows
, unsigned columns
)
399 if (base_type
== GLSL_TYPE_VOID
)
402 if ((rows
< 1) || (rows
> 4) || (columns
< 1) || (columns
> 4))
405 /* Treat GLSL vectors as Nx1 matrices.
413 case GLSL_TYPE_FLOAT
:
421 if ((base_type
!= GLSL_TYPE_FLOAT
) || (rows
== 1))
424 /* GLSL matrix types are named mat{COLUMNS}x{ROWS}. Only the following
425 * combinations are valid:
433 #define IDX(c,r) (((c-1)*3) + (r-1))
435 switch (IDX(columns
, rows
)) {
436 case IDX(2,2): return mat2_type
;
437 case IDX(2,3): return mat2x3_type
;
438 case IDX(2,4): return mat2x4_type
;
439 case IDX(3,2): return mat3x2_type
;
440 case IDX(3,3): return mat3_type
;
441 case IDX(3,4): return mat3x4_type
;
442 case IDX(4,2): return mat4x2_type
;
443 case IDX(4,3): return mat4x3_type
;
444 case IDX(4,4): return mat4_type
;
445 default: return error_type
;
449 assert(!"Should not get here.");
455 glsl_type::get_array_instance(const glsl_type
*base
, unsigned array_size
)
458 if (array_types
== NULL
) {
459 array_types
= hash_table_ctor(64, hash_table_string_hash
,
460 hash_table_string_compare
);
463 /* Generate a name using the base type pointer in the key. This is
464 * done because the name of the base type may not be unique across
465 * shaders. For example, two shaders may have different record types
469 snprintf(key
, sizeof(key
), "%p[%u]", (void *) base
, array_size
);
471 const glsl_type
*t
= (glsl_type
*) hash_table_find(array_types
, key
);
473 t
= new glsl_type(base
, array_size
);
475 hash_table_insert(array_types
, (void *) t
, ralloc_strdup(mem_ctx
, key
));
478 assert(t
->base_type
== GLSL_TYPE_ARRAY
);
479 assert(t
->length
== array_size
);
480 assert(t
->fields
.array
== base
);
487 glsl_type::record_compare(const glsl_type
*b
) const
489 if (this->length
!= b
->length
)
492 if (this->interface_packing
!= b
->interface_packing
)
495 for (unsigned i
= 0; i
< this->length
; i
++) {
496 if (this->fields
.structure
[i
].type
!= b
->fields
.structure
[i
].type
)
498 if (strcmp(this->fields
.structure
[i
].name
,
499 b
->fields
.structure
[i
].name
) != 0)
501 if (this->fields
.structure
[i
].row_major
502 != b
->fields
.structure
[i
].row_major
)
504 if (this->fields
.structure
[i
].location
505 != b
->fields
.structure
[i
].location
)
507 if (this->fields
.structure
[i
].interpolation
508 != b
->fields
.structure
[i
].interpolation
)
510 if (this->fields
.structure
[i
].centroid
511 != b
->fields
.structure
[i
].centroid
)
513 if (this->fields
.structure
[i
].sample
514 != b
->fields
.structure
[i
].sample
)
523 glsl_type::record_key_compare(const void *a
, const void *b
)
525 const glsl_type
*const key1
= (glsl_type
*) a
;
526 const glsl_type
*const key2
= (glsl_type
*) b
;
528 /* Return zero is the types match (there is zero difference) or non-zero
531 if (strcmp(key1
->name
, key2
->name
) != 0)
534 return !key1
->record_compare(key2
);
539 glsl_type::record_key_hash(const void *a
)
541 const glsl_type
*const key
= (glsl_type
*) a
;
545 size
= snprintf(hash_key
, sizeof(hash_key
), "%08x", key
->length
);
547 for (unsigned i
= 0; i
< key
->length
; i
++) {
548 if (size
>= sizeof(hash_key
))
551 size
+= snprintf(& hash_key
[size
], sizeof(hash_key
) - size
,
552 "%p", (void *) key
->fields
.structure
[i
].type
);
555 return hash_table_string_hash(& hash_key
);
560 glsl_type::get_record_instance(const glsl_struct_field
*fields
,
564 const glsl_type
key(fields
, num_fields
, name
);
566 if (record_types
== NULL
) {
567 record_types
= hash_table_ctor(64, record_key_hash
, record_key_compare
);
570 const glsl_type
*t
= (glsl_type
*) hash_table_find(record_types
, & key
);
572 t
= new glsl_type(fields
, num_fields
, name
);
574 hash_table_insert(record_types
, (void *) t
, t
);
577 assert(t
->base_type
== GLSL_TYPE_STRUCT
);
578 assert(t
->length
== num_fields
);
579 assert(strcmp(t
->name
, name
) == 0);
586 glsl_type::get_interface_instance(const glsl_struct_field
*fields
,
588 enum glsl_interface_packing packing
,
589 const char *block_name
)
591 const glsl_type
key(fields
, num_fields
, packing
, block_name
);
593 if (interface_types
== NULL
) {
594 interface_types
= hash_table_ctor(64, record_key_hash
, record_key_compare
);
597 const glsl_type
*t
= (glsl_type
*) hash_table_find(interface_types
, & key
);
599 t
= new glsl_type(fields
, num_fields
, packing
, block_name
);
601 hash_table_insert(interface_types
, (void *) t
, t
);
604 assert(t
->base_type
== GLSL_TYPE_INTERFACE
);
605 assert(t
->length
== num_fields
);
606 assert(strcmp(t
->name
, block_name
) == 0);
613 glsl_type::field_type(const char *name
) const
615 if (this->base_type
!= GLSL_TYPE_STRUCT
616 && this->base_type
!= GLSL_TYPE_INTERFACE
)
619 for (unsigned i
= 0; i
< this->length
; i
++) {
620 if (strcmp(name
, this->fields
.structure
[i
].name
) == 0)
621 return this->fields
.structure
[i
].type
;
629 glsl_type::field_index(const char *name
) const
631 if (this->base_type
!= GLSL_TYPE_STRUCT
632 && this->base_type
!= GLSL_TYPE_INTERFACE
)
635 for (unsigned i
= 0; i
< this->length
; i
++) {
636 if (strcmp(name
, this->fields
.structure
[i
].name
) == 0)
645 glsl_type::component_slots() const
647 switch (this->base_type
) {
650 case GLSL_TYPE_FLOAT
:
652 return this->components();
654 case GLSL_TYPE_STRUCT
:
655 case GLSL_TYPE_INTERFACE
: {
658 for (unsigned i
= 0; i
< this->length
; i
++)
659 size
+= this->fields
.structure
[i
].type
->component_slots();
664 case GLSL_TYPE_ARRAY
:
665 return this->length
* this->fields
.array
->component_slots();
667 case GLSL_TYPE_IMAGE
:
670 case GLSL_TYPE_SAMPLER
:
671 case GLSL_TYPE_ATOMIC_UINT
:
673 case GLSL_TYPE_ERROR
:
681 glsl_type::can_implicitly_convert_to(const glsl_type
*desired
) const
686 /* There is no conversion among matrix types. */
687 if (this->matrix_columns
> 1 || desired
->matrix_columns
> 1)
690 /* int and uint can be converted to float. */
691 return desired
->is_float()
692 && this->is_integer()
693 && this->vector_elements
== desired
->vector_elements
;
697 glsl_type::std140_base_alignment(bool row_major
) const
699 /* (1) If the member is a scalar consuming <N> basic machine units, the
700 * base alignment is <N>.
702 * (2) If the member is a two- or four-component vector with components
703 * consuming <N> basic machine units, the base alignment is 2<N> or
704 * 4<N>, respectively.
706 * (3) If the member is a three-component vector with components consuming
707 * <N> basic machine units, the base alignment is 4<N>.
709 if (this->is_scalar() || this->is_vector()) {
710 switch (this->vector_elements
) {
721 /* (4) If the member is an array of scalars or vectors, the base alignment
722 * and array stride are set to match the base alignment of a single
723 * array element, according to rules (1), (2), and (3), and rounded up
724 * to the base alignment of a vec4. The array may have padding at the
725 * end; the base offset of the member following the array is rounded up
726 * to the next multiple of the base alignment.
728 * (6) If the member is an array of <S> column-major matrices with <C>
729 * columns and <R> rows, the matrix is stored identically to a row of
730 * <S>*<C> column vectors with <R> components each, according to rule
733 * (8) If the member is an array of <S> row-major matrices with <C> columns
734 * and <R> rows, the matrix is stored identically to a row of <S>*<R>
735 * row vectors with <C> components each, according to rule (4).
737 * (10) If the member is an array of <S> structures, the <S> elements of
738 * the array are laid out in order, according to rule (9).
740 if (this->is_array()) {
741 if (this->fields
.array
->is_scalar() ||
742 this->fields
.array
->is_vector() ||
743 this->fields
.array
->is_matrix()) {
744 return MAX2(this->fields
.array
->std140_base_alignment(row_major
), 16);
746 assert(this->fields
.array
->is_record());
747 return this->fields
.array
->std140_base_alignment(row_major
);
751 /* (5) If the member is a column-major matrix with <C> columns and
752 * <R> rows, the matrix is stored identically to an array of
753 * <C> column vectors with <R> components each, according to
756 * (7) If the member is a row-major matrix with <C> columns and <R>
757 * rows, the matrix is stored identically to an array of <R>
758 * row vectors with <C> components each, according to rule (4).
760 if (this->is_matrix()) {
761 const struct glsl_type
*vec_type
, *array_type
;
762 int c
= this->matrix_columns
;
763 int r
= this->vector_elements
;
766 vec_type
= get_instance(GLSL_TYPE_FLOAT
, c
, 1);
767 array_type
= glsl_type::get_array_instance(vec_type
, r
);
769 vec_type
= get_instance(GLSL_TYPE_FLOAT
, r
, 1);
770 array_type
= glsl_type::get_array_instance(vec_type
, c
);
773 return array_type
->std140_base_alignment(false);
776 /* (9) If the member is a structure, the base alignment of the
777 * structure is <N>, where <N> is the largest base alignment
778 * value of any of its members, and rounded up to the base
779 * alignment of a vec4. The individual members of this
780 * sub-structure are then assigned offsets by applying this set
781 * of rules recursively, where the base offset of the first
782 * member of the sub-structure is equal to the aligned offset
783 * of the structure. The structure may have padding at the end;
784 * the base offset of the member following the sub-structure is
785 * rounded up to the next multiple of the base alignment of the
788 if (this->is_record()) {
789 unsigned base_alignment
= 16;
790 for (unsigned i
= 0; i
< this->length
; i
++) {
791 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
792 base_alignment
= MAX2(base_alignment
,
793 field_type
->std140_base_alignment(row_major
));
795 return base_alignment
;
798 assert(!"not reached");
803 glsl_type::std140_size(bool row_major
) const
805 /* (1) If the member is a scalar consuming <N> basic machine units, the
806 * base alignment is <N>.
808 * (2) If the member is a two- or four-component vector with components
809 * consuming <N> basic machine units, the base alignment is 2<N> or
810 * 4<N>, respectively.
812 * (3) If the member is a three-component vector with components consuming
813 * <N> basic machine units, the base alignment is 4<N>.
815 if (this->is_scalar() || this->is_vector()) {
816 return this->vector_elements
* 4;
819 /* (5) If the member is a column-major matrix with <C> columns and
820 * <R> rows, the matrix is stored identically to an array of
821 * <C> column vectors with <R> components each, according to
824 * (6) If the member is an array of <S> column-major matrices with <C>
825 * columns and <R> rows, the matrix is stored identically to a row of
826 * <S>*<C> column vectors with <R> components each, according to rule
829 * (7) If the member is a row-major matrix with <C> columns and <R>
830 * rows, the matrix is stored identically to an array of <R>
831 * row vectors with <C> components each, according to rule (4).
833 * (8) If the member is an array of <S> row-major matrices with <C> columns
834 * and <R> rows, the matrix is stored identically to a row of <S>*<R>
835 * row vectors with <C> components each, according to rule (4).
837 if (this->is_matrix() || (this->is_array() &&
838 this->fields
.array
->is_matrix())) {
839 const struct glsl_type
*element_type
;
840 const struct glsl_type
*vec_type
;
841 unsigned int array_len
;
843 if (this->is_array()) {
844 element_type
= this->fields
.array
;
845 array_len
= this->length
;
852 vec_type
= get_instance(GLSL_TYPE_FLOAT
,
853 element_type
->matrix_columns
, 1);
854 array_len
*= element_type
->vector_elements
;
856 vec_type
= get_instance(GLSL_TYPE_FLOAT
,
857 element_type
->vector_elements
, 1);
858 array_len
*= element_type
->matrix_columns
;
860 const glsl_type
*array_type
= glsl_type::get_array_instance(vec_type
,
863 return array_type
->std140_size(false);
866 /* (4) If the member is an array of scalars or vectors, the base alignment
867 * and array stride are set to match the base alignment of a single
868 * array element, according to rules (1), (2), and (3), and rounded up
869 * to the base alignment of a vec4. The array may have padding at the
870 * end; the base offset of the member following the array is rounded up
871 * to the next multiple of the base alignment.
873 * (10) If the member is an array of <S> structures, the <S> elements of
874 * the array are laid out in order, according to rule (9).
876 if (this->is_array()) {
877 if (this->fields
.array
->is_record()) {
878 return this->length
* this->fields
.array
->std140_size(row_major
);
880 unsigned element_base_align
=
881 this->fields
.array
->std140_base_alignment(row_major
);
882 return this->length
* MAX2(element_base_align
, 16);
886 /* (9) If the member is a structure, the base alignment of the
887 * structure is <N>, where <N> is the largest base alignment
888 * value of any of its members, and rounded up to the base
889 * alignment of a vec4. The individual members of this
890 * sub-structure are then assigned offsets by applying this set
891 * of rules recursively, where the base offset of the first
892 * member of the sub-structure is equal to the aligned offset
893 * of the structure. The structure may have padding at the end;
894 * the base offset of the member following the sub-structure is
895 * rounded up to the next multiple of the base alignment of the
898 if (this->is_record()) {
900 for (unsigned i
= 0; i
< this->length
; i
++) {
901 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
902 unsigned align
= field_type
->std140_base_alignment(row_major
);
903 size
= glsl_align(size
, align
);
904 size
+= field_type
->std140_size(row_major
);
906 size
= glsl_align(size
,
907 this->fields
.structure
[0].type
->std140_base_alignment(row_major
));
911 assert(!"not reached");
917 glsl_type::count_attribute_slots() const
919 /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
921 * "A scalar input counts the same amount against this limit as a vec4,
922 * so applications may want to consider packing groups of four
923 * unrelated float inputs together into a vector to better utilize the
924 * capabilities of the underlying hardware. A matrix input will use up
925 * multiple locations. The number of locations used will equal the
926 * number of columns in the matrix."
928 * The spec does not explicitly say how arrays are counted. However, it
929 * should be safe to assume the total number of slots consumed by an array
930 * is the number of entries in the array multiplied by the number of slots
931 * consumed by a single element of the array.
933 * The spec says nothing about how structs are counted, because vertex
934 * attributes are not allowed to be (or contain) structs. However, Mesa
935 * allows varying structs, the number of varying slots taken up by a
936 * varying struct is simply equal to the sum of the number of slots taken
937 * up by each element.
939 switch (this->base_type
) {
942 case GLSL_TYPE_FLOAT
:
944 return this->matrix_columns
;
946 case GLSL_TYPE_STRUCT
:
947 case GLSL_TYPE_INTERFACE
: {
950 for (unsigned i
= 0; i
< this->length
; i
++)
951 size
+= this->fields
.structure
[i
].type
->count_attribute_slots();
956 case GLSL_TYPE_ARRAY
:
957 return this->length
* this->fields
.array
->count_attribute_slots();
959 case GLSL_TYPE_SAMPLER
:
960 case GLSL_TYPE_IMAGE
:
961 case GLSL_TYPE_ATOMIC_UINT
:
963 case GLSL_TYPE_ERROR
:
967 assert(!"Unexpected type in count_attribute_slots()");
973 glsl_type::coordinate_components() const
977 switch (sampler_dimensionality
) {
978 case GLSL_SAMPLER_DIM_1D
:
979 case GLSL_SAMPLER_DIM_BUF
:
982 case GLSL_SAMPLER_DIM_2D
:
983 case GLSL_SAMPLER_DIM_RECT
:
984 case GLSL_SAMPLER_DIM_MS
:
985 case GLSL_SAMPLER_DIM_EXTERNAL
:
988 case GLSL_SAMPLER_DIM_3D
:
989 case GLSL_SAMPLER_DIM_CUBE
:
993 assert(!"Should not get here.");
998 /* Array textures need an additional component for the array index. */