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
].row_major
= fields
[i
].row_major
;
110 glsl_type::glsl_type(const glsl_struct_field
*fields
, unsigned num_fields
,
111 enum glsl_interface_packing packing
, const char *name
) :
113 base_type(GLSL_TYPE_INTERFACE
),
114 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
115 sampler_type(0), interface_packing((unsigned) packing
),
116 vector_elements(0), matrix_columns(0),
121 init_ralloc_type_ctx();
122 assert(name
!= NULL
);
123 this->name
= ralloc_strdup(this->mem_ctx
, name
);
124 this->fields
.structure
= ralloc_array(this->mem_ctx
,
125 glsl_struct_field
, length
);
126 for (i
= 0; i
< length
; i
++) {
127 this->fields
.structure
[i
].type
= fields
[i
].type
;
128 this->fields
.structure
[i
].name
= ralloc_strdup(this->fields
.structure
,
130 this->fields
.structure
[i
].location
= fields
[i
].location
;
131 this->fields
.structure
[i
].interpolation
= fields
[i
].interpolation
;
132 this->fields
.structure
[i
].centroid
= fields
[i
].centroid
;
133 this->fields
.structure
[i
].row_major
= fields
[i
].row_major
;
139 glsl_type::contains_sampler() const
141 if (this->is_array()) {
142 return this->fields
.array
->contains_sampler();
143 } else if (this->is_record()) {
144 for (unsigned int i
= 0; i
< this->length
; i
++) {
145 if (this->fields
.structure
[i
].type
->contains_sampler())
150 return this->is_sampler();
156 glsl_type::contains_integer() const
158 if (this->is_array()) {
159 return this->fields
.array
->contains_integer();
160 } else if (this->is_record()) {
161 for (unsigned int i
= 0; i
< this->length
; i
++) {
162 if (this->fields
.structure
[i
].type
->contains_integer())
167 return this->is_integer();
172 glsl_type::contains_opaque() const {
174 case GLSL_TYPE_SAMPLER
:
175 case GLSL_TYPE_ATOMIC_UINT
:
177 case GLSL_TYPE_ARRAY
:
178 return element_type()->contains_opaque();
179 case GLSL_TYPE_STRUCT
:
180 for (unsigned int i
= 0; i
< length
; i
++) {
181 if (fields
.structure
[i
].type
->contains_opaque())
191 glsl_type::sampler_index() const
193 const glsl_type
*const t
= (this->is_array()) ? this->fields
.array
: this;
195 assert(t
->is_sampler());
197 switch (t
->sampler_dimensionality
) {
198 case GLSL_SAMPLER_DIM_1D
:
199 return (t
->sampler_array
) ? TEXTURE_1D_ARRAY_INDEX
: TEXTURE_1D_INDEX
;
200 case GLSL_SAMPLER_DIM_2D
:
201 return (t
->sampler_array
) ? TEXTURE_2D_ARRAY_INDEX
: TEXTURE_2D_INDEX
;
202 case GLSL_SAMPLER_DIM_3D
:
203 return TEXTURE_3D_INDEX
;
204 case GLSL_SAMPLER_DIM_CUBE
:
205 return (t
->sampler_array
) ? TEXTURE_CUBE_ARRAY_INDEX
: TEXTURE_CUBE_INDEX
;
206 case GLSL_SAMPLER_DIM_RECT
:
207 return TEXTURE_RECT_INDEX
;
208 case GLSL_SAMPLER_DIM_BUF
:
209 return TEXTURE_BUFFER_INDEX
;
210 case GLSL_SAMPLER_DIM_EXTERNAL
:
211 return TEXTURE_EXTERNAL_INDEX
;
212 case GLSL_SAMPLER_DIM_MS
:
213 return (t
->sampler_array
) ? TEXTURE_2D_MULTISAMPLE_ARRAY_INDEX
: TEXTURE_2D_MULTISAMPLE_INDEX
;
215 assert(!"Should not get here.");
216 return TEXTURE_BUFFER_INDEX
;
221 const glsl_type
*glsl_type::get_base_type() const
228 case GLSL_TYPE_FLOAT
:
238 const glsl_type
*glsl_type::get_scalar_type() const
240 const glsl_type
*type
= this;
243 while (type
->base_type
== GLSL_TYPE_ARRAY
)
244 type
= type
->fields
.array
;
246 /* Handle vectors and matrices */
247 switch (type
->base_type
) {
252 case GLSL_TYPE_FLOAT
:
257 /* Handle everything else */
264 _mesa_glsl_release_types(void)
266 if (glsl_type::array_types
!= NULL
) {
267 hash_table_dtor(glsl_type::array_types
);
268 glsl_type::array_types
= NULL
;
271 if (glsl_type::record_types
!= NULL
) {
272 hash_table_dtor(glsl_type::record_types
);
273 glsl_type::record_types
= NULL
;
278 glsl_type::glsl_type(const glsl_type
*array
, unsigned length
) :
279 base_type(GLSL_TYPE_ARRAY
),
280 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
281 sampler_type(0), interface_packing(0),
282 vector_elements(0), matrix_columns(0),
283 name(NULL
), length(length
)
285 this->fields
.array
= array
;
286 /* Inherit the gl type of the base. The GL type is used for
287 * uniform/statevar handling in Mesa and the arrayness of the type
288 * is represented by the size rather than the type.
290 this->gl_type
= array
->gl_type
;
292 /* Allow a maximum of 10 characters for the array size. This is enough
293 * for 32-bits of ~0. The extra 3 are for the '[', ']', and terminating
296 const unsigned name_length
= strlen(array
->name
) + 10 + 3;
297 char *const n
= (char *) ralloc_size(this->mem_ctx
, name_length
);
300 snprintf(n
, name_length
, "%s[]", array
->name
);
302 snprintf(n
, name_length
, "%s[%u]", array
->name
, length
);
309 glsl_type::vec(unsigned components
)
311 if (components
== 0 || components
> 4)
314 static const glsl_type
*const ts
[] = {
315 float_type
, vec2_type
, vec3_type
, vec4_type
317 return ts
[components
- 1];
322 glsl_type::ivec(unsigned components
)
324 if (components
== 0 || components
> 4)
327 static const glsl_type
*const ts
[] = {
328 int_type
, ivec2_type
, ivec3_type
, ivec4_type
330 return ts
[components
- 1];
335 glsl_type::uvec(unsigned components
)
337 if (components
== 0 || components
> 4)
340 static const glsl_type
*const ts
[] = {
341 uint_type
, uvec2_type
, uvec3_type
, uvec4_type
343 return ts
[components
- 1];
348 glsl_type::bvec(unsigned components
)
350 if (components
== 0 || components
> 4)
353 static const glsl_type
*const ts
[] = {
354 bool_type
, bvec2_type
, bvec3_type
, bvec4_type
356 return ts
[components
- 1];
361 glsl_type::get_instance(unsigned base_type
, unsigned rows
, unsigned columns
)
363 if (base_type
== GLSL_TYPE_VOID
)
366 if ((rows
< 1) || (rows
> 4) || (columns
< 1) || (columns
> 4))
369 /* Treat GLSL vectors as Nx1 matrices.
377 case GLSL_TYPE_FLOAT
:
385 if ((base_type
!= GLSL_TYPE_FLOAT
) || (rows
== 1))
388 /* GLSL matrix types are named mat{COLUMNS}x{ROWS}. Only the following
389 * combinations are valid:
397 #define IDX(c,r) (((c-1)*3) + (r-1))
399 switch (IDX(columns
, rows
)) {
400 case IDX(2,2): return mat2_type
;
401 case IDX(2,3): return mat2x3_type
;
402 case IDX(2,4): return mat2x4_type
;
403 case IDX(3,2): return mat3x2_type
;
404 case IDX(3,3): return mat3_type
;
405 case IDX(3,4): return mat3x4_type
;
406 case IDX(4,2): return mat4x2_type
;
407 case IDX(4,3): return mat4x3_type
;
408 case IDX(4,4): return mat4_type
;
409 default: return error_type
;
413 assert(!"Should not get here.");
419 glsl_type::get_array_instance(const glsl_type
*base
, unsigned array_size
)
422 if (array_types
== NULL
) {
423 array_types
= hash_table_ctor(64, hash_table_string_hash
,
424 hash_table_string_compare
);
427 /* Generate a name using the base type pointer in the key. This is
428 * done because the name of the base type may not be unique across
429 * shaders. For example, two shaders may have different record types
433 snprintf(key
, sizeof(key
), "%p[%u]", (void *) base
, array_size
);
435 const glsl_type
*t
= (glsl_type
*) hash_table_find(array_types
, key
);
437 t
= new glsl_type(base
, array_size
);
439 hash_table_insert(array_types
, (void *) t
, ralloc_strdup(mem_ctx
, key
));
442 assert(t
->base_type
== GLSL_TYPE_ARRAY
);
443 assert(t
->length
== array_size
);
444 assert(t
->fields
.array
== base
);
451 glsl_type::record_key_compare(const void *a
, const void *b
)
453 const glsl_type
*const key1
= (glsl_type
*) a
;
454 const glsl_type
*const key2
= (glsl_type
*) b
;
456 /* Return zero is the types match (there is zero difference) or non-zero
459 if (strcmp(key1
->name
, key2
->name
) != 0)
462 if (key1
->length
!= key2
->length
)
465 if (key1
->interface_packing
!= key2
->interface_packing
)
468 for (unsigned i
= 0; i
< key1
->length
; i
++) {
469 if (key1
->fields
.structure
[i
].type
!= key2
->fields
.structure
[i
].type
)
471 if (strcmp(key1
->fields
.structure
[i
].name
,
472 key2
->fields
.structure
[i
].name
) != 0)
474 if (key1
->fields
.structure
[i
].row_major
475 != key2
->fields
.structure
[i
].row_major
)
477 if (key1
->fields
.structure
[i
].location
478 != key2
->fields
.structure
[i
].location
)
480 if (key1
->fields
.structure
[i
].interpolation
481 != key2
->fields
.structure
[i
].interpolation
)
483 if (key1
->fields
.structure
[i
].centroid
484 != key2
->fields
.structure
[i
].centroid
)
493 glsl_type::record_key_hash(const void *a
)
495 const glsl_type
*const key
= (glsl_type
*) a
;
499 size
= snprintf(hash_key
, sizeof(hash_key
), "%08x", key
->length
);
501 for (unsigned i
= 0; i
< key
->length
; i
++) {
502 if (size
>= sizeof(hash_key
))
505 size
+= snprintf(& hash_key
[size
], sizeof(hash_key
) - size
,
506 "%p", (void *) key
->fields
.structure
[i
].type
);
509 return hash_table_string_hash(& hash_key
);
514 glsl_type::get_record_instance(const glsl_struct_field
*fields
,
518 const glsl_type
key(fields
, num_fields
, name
);
520 if (record_types
== NULL
) {
521 record_types
= hash_table_ctor(64, record_key_hash
, record_key_compare
);
524 const glsl_type
*t
= (glsl_type
*) hash_table_find(record_types
, & key
);
526 t
= new glsl_type(fields
, num_fields
, name
);
528 hash_table_insert(record_types
, (void *) t
, t
);
531 assert(t
->base_type
== GLSL_TYPE_STRUCT
);
532 assert(t
->length
== num_fields
);
533 assert(strcmp(t
->name
, name
) == 0);
540 glsl_type::get_interface_instance(const glsl_struct_field
*fields
,
542 enum glsl_interface_packing packing
,
543 const char *block_name
)
545 const glsl_type
key(fields
, num_fields
, packing
, block_name
);
547 if (interface_types
== NULL
) {
548 interface_types
= hash_table_ctor(64, record_key_hash
, record_key_compare
);
551 const glsl_type
*t
= (glsl_type
*) hash_table_find(interface_types
, & key
);
553 t
= new glsl_type(fields
, num_fields
, packing
, block_name
);
555 hash_table_insert(interface_types
, (void *) t
, t
);
558 assert(t
->base_type
== GLSL_TYPE_INTERFACE
);
559 assert(t
->length
== num_fields
);
560 assert(strcmp(t
->name
, block_name
) == 0);
567 glsl_type::field_type(const char *name
) const
569 if (this->base_type
!= GLSL_TYPE_STRUCT
570 && this->base_type
!= GLSL_TYPE_INTERFACE
)
573 for (unsigned i
= 0; i
< this->length
; i
++) {
574 if (strcmp(name
, this->fields
.structure
[i
].name
) == 0)
575 return this->fields
.structure
[i
].type
;
583 glsl_type::field_index(const char *name
) const
585 if (this->base_type
!= GLSL_TYPE_STRUCT
586 && this->base_type
!= GLSL_TYPE_INTERFACE
)
589 for (unsigned i
= 0; i
< this->length
; i
++) {
590 if (strcmp(name
, this->fields
.structure
[i
].name
) == 0)
599 glsl_type::component_slots() const
601 switch (this->base_type
) {
604 case GLSL_TYPE_FLOAT
:
606 return this->components();
608 case GLSL_TYPE_STRUCT
:
609 case GLSL_TYPE_INTERFACE
: {
612 for (unsigned i
= 0; i
< this->length
; i
++)
613 size
+= this->fields
.structure
[i
].type
->component_slots();
618 case GLSL_TYPE_ARRAY
:
619 return this->length
* this->fields
.array
->component_slots();
621 case GLSL_TYPE_SAMPLER
:
622 case GLSL_TYPE_ATOMIC_UINT
:
624 case GLSL_TYPE_ERROR
:
632 glsl_type::can_implicitly_convert_to(const glsl_type
*desired
) const
637 /* There is no conversion among matrix types. */
638 if (this->matrix_columns
> 1 || desired
->matrix_columns
> 1)
641 /* int and uint can be converted to float. */
642 return desired
->is_float()
643 && this->is_integer()
644 && this->vector_elements
== desired
->vector_elements
;
648 glsl_type::std140_base_alignment(bool row_major
) const
650 /* (1) If the member is a scalar consuming <N> basic machine units, the
651 * base alignment is <N>.
653 * (2) If the member is a two- or four-component vector with components
654 * consuming <N> basic machine units, the base alignment is 2<N> or
655 * 4<N>, respectively.
657 * (3) If the member is a three-component vector with components consuming
658 * <N> basic machine units, the base alignment is 4<N>.
660 if (this->is_scalar() || this->is_vector()) {
661 switch (this->vector_elements
) {
672 /* (4) If the member is an array of scalars or vectors, the base alignment
673 * and array stride are set to match the base alignment of a single
674 * array element, according to rules (1), (2), and (3), and rounded up
675 * to the base alignment of a vec4. The array may have padding at the
676 * end; the base offset of the member following the array is rounded up
677 * to the next multiple of the base alignment.
679 * (6) If the member is an array of <S> column-major matrices with <C>
680 * columns and <R> rows, the matrix is stored identically to a row of
681 * <S>*<C> column vectors with <R> components each, according to rule
684 * (8) If the member is an array of <S> row-major matrices with <C> columns
685 * and <R> rows, the matrix is stored identically to a row of <S>*<R>
686 * row vectors with <C> components each, according to rule (4).
688 * (10) If the member is an array of <S> structures, the <S> elements of
689 * the array are laid out in order, according to rule (9).
691 if (this->is_array()) {
692 if (this->fields
.array
->is_scalar() ||
693 this->fields
.array
->is_vector() ||
694 this->fields
.array
->is_matrix()) {
695 return MAX2(this->fields
.array
->std140_base_alignment(row_major
), 16);
697 assert(this->fields
.array
->is_record());
698 return this->fields
.array
->std140_base_alignment(row_major
);
702 /* (5) If the member is a column-major matrix with <C> columns and
703 * <R> rows, the matrix is stored identically to an array of
704 * <C> column vectors with <R> components each, according to
707 * (7) If the member is a row-major matrix with <C> columns and <R>
708 * rows, the matrix is stored identically to an array of <R>
709 * row vectors with <C> components each, according to rule (4).
711 if (this->is_matrix()) {
712 const struct glsl_type
*vec_type
, *array_type
;
713 int c
= this->matrix_columns
;
714 int r
= this->vector_elements
;
717 vec_type
= get_instance(GLSL_TYPE_FLOAT
, c
, 1);
718 array_type
= glsl_type::get_array_instance(vec_type
, r
);
720 vec_type
= get_instance(GLSL_TYPE_FLOAT
, r
, 1);
721 array_type
= glsl_type::get_array_instance(vec_type
, c
);
724 return array_type
->std140_base_alignment(false);
727 /* (9) If the member is a structure, the base alignment of the
728 * structure is <N>, where <N> is the largest base alignment
729 * value of any of its members, and rounded up to the base
730 * alignment of a vec4. The individual members of this
731 * sub-structure are then assigned offsets by applying this set
732 * of rules recursively, where the base offset of the first
733 * member of the sub-structure is equal to the aligned offset
734 * of the structure. The structure may have padding at the end;
735 * the base offset of the member following the sub-structure is
736 * rounded up to the next multiple of the base alignment of the
739 if (this->is_record()) {
740 unsigned base_alignment
= 16;
741 for (unsigned i
= 0; i
< this->length
; i
++) {
742 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
743 base_alignment
= MAX2(base_alignment
,
744 field_type
->std140_base_alignment(row_major
));
746 return base_alignment
;
749 assert(!"not reached");
754 glsl_type::std140_size(bool row_major
) const
756 /* (1) If the member is a scalar consuming <N> basic machine units, the
757 * base alignment is <N>.
759 * (2) If the member is a two- or four-component vector with components
760 * consuming <N> basic machine units, the base alignment is 2<N> or
761 * 4<N>, respectively.
763 * (3) If the member is a three-component vector with components consuming
764 * <N> basic machine units, the base alignment is 4<N>.
766 if (this->is_scalar() || this->is_vector()) {
767 return this->vector_elements
* 4;
770 /* (5) If the member is a column-major matrix with <C> columns and
771 * <R> rows, the matrix is stored identically to an array of
772 * <C> column vectors with <R> components each, according to
775 * (6) If the member is an array of <S> column-major matrices with <C>
776 * columns and <R> rows, the matrix is stored identically to a row of
777 * <S>*<C> column vectors with <R> components each, according to rule
780 * (7) If the member is a row-major matrix with <C> columns and <R>
781 * rows, the matrix is stored identically to an array of <R>
782 * row vectors with <C> components each, according to rule (4).
784 * (8) If the member is an array of <S> row-major matrices with <C> columns
785 * and <R> rows, the matrix is stored identically to a row of <S>*<R>
786 * row vectors with <C> components each, according to rule (4).
788 if (this->is_matrix() || (this->is_array() &&
789 this->fields
.array
->is_matrix())) {
790 const struct glsl_type
*element_type
;
791 const struct glsl_type
*vec_type
;
792 unsigned int array_len
;
794 if (this->is_array()) {
795 element_type
= this->fields
.array
;
796 array_len
= this->length
;
803 vec_type
= get_instance(GLSL_TYPE_FLOAT
,
804 element_type
->matrix_columns
, 1);
805 array_len
*= element_type
->vector_elements
;
807 vec_type
= get_instance(GLSL_TYPE_FLOAT
,
808 element_type
->vector_elements
, 1);
809 array_len
*= element_type
->matrix_columns
;
811 const glsl_type
*array_type
= glsl_type::get_array_instance(vec_type
,
814 return array_type
->std140_size(false);
817 /* (4) If the member is an array of scalars or vectors, the base alignment
818 * and array stride are set to match the base alignment of a single
819 * array element, according to rules (1), (2), and (3), and rounded up
820 * to the base alignment of a vec4. The array may have padding at the
821 * end; the base offset of the member following the array is rounded up
822 * to the next multiple of the base alignment.
824 * (10) If the member is an array of <S> structures, the <S> elements of
825 * the array are laid out in order, according to rule (9).
827 if (this->is_array()) {
828 if (this->fields
.array
->is_record()) {
829 return this->length
* this->fields
.array
->std140_size(row_major
);
831 unsigned element_base_align
=
832 this->fields
.array
->std140_base_alignment(row_major
);
833 return this->length
* MAX2(element_base_align
, 16);
837 /* (9) If the member is a structure, the base alignment of the
838 * structure is <N>, where <N> is the largest base alignment
839 * value of any of its members, and rounded up to the base
840 * alignment of a vec4. The individual members of this
841 * sub-structure are then assigned offsets by applying this set
842 * of rules recursively, where the base offset of the first
843 * member of the sub-structure is equal to the aligned offset
844 * of the structure. The structure may have padding at the end;
845 * the base offset of the member following the sub-structure is
846 * rounded up to the next multiple of the base alignment of the
849 if (this->is_record()) {
851 for (unsigned i
= 0; i
< this->length
; i
++) {
852 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
853 unsigned align
= field_type
->std140_base_alignment(row_major
);
854 size
= glsl_align(size
, align
);
855 size
+= field_type
->std140_size(row_major
);
857 size
= glsl_align(size
,
858 this->fields
.structure
[0].type
->std140_base_alignment(row_major
));
862 assert(!"not reached");
868 glsl_type::count_attribute_slots() const
870 /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
872 * "A scalar input counts the same amount against this limit as a vec4,
873 * so applications may want to consider packing groups of four
874 * unrelated float inputs together into a vector to better utilize the
875 * capabilities of the underlying hardware. A matrix input will use up
876 * multiple locations. The number of locations used will equal the
877 * number of columns in the matrix."
879 * The spec does not explicitly say how arrays are counted. However, it
880 * should be safe to assume the total number of slots consumed by an array
881 * is the number of entries in the array multiplied by the number of slots
882 * consumed by a single element of the array.
884 * The spec says nothing about how structs are counted, because vertex
885 * attributes are not allowed to be (or contain) structs. However, Mesa
886 * allows varying structs, the number of varying slots taken up by a
887 * varying struct is simply equal to the sum of the number of slots taken
888 * up by each element.
890 switch (this->base_type
) {
893 case GLSL_TYPE_FLOAT
:
895 return this->matrix_columns
;
897 case GLSL_TYPE_STRUCT
:
898 case GLSL_TYPE_INTERFACE
: {
901 for (unsigned i
= 0; i
< this->length
; i
++)
902 size
+= this->fields
.structure
[i
].type
->count_attribute_slots();
907 case GLSL_TYPE_ARRAY
:
908 return this->length
* this->fields
.array
->count_attribute_slots();
910 case GLSL_TYPE_SAMPLER
:
911 case GLSL_TYPE_ATOMIC_UINT
:
913 case GLSL_TYPE_ERROR
:
917 assert(!"Unexpected type in count_attribute_slots()");
923 glsl_type::sampler_coordinate_components() const
925 assert(is_sampler());
929 switch (sampler_dimensionality
) {
930 case GLSL_SAMPLER_DIM_1D
:
931 case GLSL_SAMPLER_DIM_BUF
:
934 case GLSL_SAMPLER_DIM_2D
:
935 case GLSL_SAMPLER_DIM_RECT
:
936 case GLSL_SAMPLER_DIM_MS
:
937 case GLSL_SAMPLER_DIM_EXTERNAL
:
940 case GLSL_SAMPLER_DIM_3D
:
941 case GLSL_SAMPLER_DIM_CUBE
:
945 assert(!"Should not get here.");
950 /* Array textures need an additional component for the array index. */