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.
25 #include "main/core.h" /* for Elements, MAX2 */
26 #include "glsl_parser_extras.h"
27 #include "glsl_types.h"
29 #include "program/hash_table.h"
32 hash_table
*glsl_type::array_types
= NULL
;
33 hash_table
*glsl_type::record_types
= NULL
;
34 hash_table
*glsl_type::interface_types
= NULL
;
35 void *glsl_type::mem_ctx
= NULL
;
38 glsl_type::init_ralloc_type_ctx(void)
40 if (glsl_type::mem_ctx
== NULL
) {
41 glsl_type::mem_ctx
= ralloc_autofree_context();
42 assert(glsl_type::mem_ctx
!= NULL
);
46 glsl_type::glsl_type(GLenum gl_type
,
47 glsl_base_type base_type
, unsigned vector_elements
,
48 unsigned matrix_columns
, const char *name
) :
51 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
52 sampler_type(0), interface_packing(0),
53 vector_elements(vector_elements
), matrix_columns(matrix_columns
),
56 init_ralloc_type_ctx();
58 this->name
= ralloc_strdup(this->mem_ctx
, name
);
59 /* Neither dimension is zero or both dimensions are zero.
61 assert((vector_elements
== 0) == (matrix_columns
== 0));
62 memset(& fields
, 0, sizeof(fields
));
65 glsl_type::glsl_type(GLenum gl_type
, glsl_base_type base_type
,
66 enum glsl_sampler_dim dim
, bool shadow
, bool array
,
67 unsigned type
, const char *name
) :
70 sampler_dimensionality(dim
), sampler_shadow(shadow
),
71 sampler_array(array
), sampler_type(type
), interface_packing(0),
74 init_ralloc_type_ctx();
76 this->name
= ralloc_strdup(this->mem_ctx
, name
);
77 memset(& fields
, 0, sizeof(fields
));
79 if (base_type
== GLSL_TYPE_SAMPLER
) {
80 /* Samplers take no storage whatsoever. */
81 matrix_columns
= vector_elements
= 0;
83 matrix_columns
= vector_elements
= 1;
87 glsl_type::glsl_type(const glsl_struct_field
*fields
, unsigned num_fields
,
90 base_type(GLSL_TYPE_STRUCT
),
91 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
92 sampler_type(0), interface_packing(0),
93 vector_elements(0), matrix_columns(0),
98 init_ralloc_type_ctx();
100 this->name
= ralloc_strdup(this->mem_ctx
, name
);
101 this->fields
.structure
= ralloc_array(this->mem_ctx
,
102 glsl_struct_field
, length
);
103 for (i
= 0; i
< length
; i
++) {
104 this->fields
.structure
[i
].type
= fields
[i
].type
;
105 this->fields
.structure
[i
].name
= ralloc_strdup(this->fields
.structure
,
107 this->fields
.structure
[i
].location
= fields
[i
].location
;
108 this->fields
.structure
[i
].interpolation
= fields
[i
].interpolation
;
109 this->fields
.structure
[i
].centroid
= fields
[i
].centroid
;
110 this->fields
.structure
[i
].sample
= fields
[i
].sample
;
111 this->fields
.structure
[i
].matrix_layout
= fields
[i
].matrix_layout
;
115 glsl_type::glsl_type(const glsl_struct_field
*fields
, unsigned num_fields
,
116 enum glsl_interface_packing packing
, const char *name
) :
118 base_type(GLSL_TYPE_INTERFACE
),
119 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
120 sampler_type(0), interface_packing((unsigned) packing
),
121 vector_elements(0), matrix_columns(0),
126 init_ralloc_type_ctx();
127 assert(name
!= NULL
);
128 this->name
= ralloc_strdup(this->mem_ctx
, name
);
129 this->fields
.structure
= ralloc_array(this->mem_ctx
,
130 glsl_struct_field
, length
);
131 for (i
= 0; i
< length
; i
++) {
132 this->fields
.structure
[i
].type
= fields
[i
].type
;
133 this->fields
.structure
[i
].name
= ralloc_strdup(this->fields
.structure
,
135 this->fields
.structure
[i
].location
= fields
[i
].location
;
136 this->fields
.structure
[i
].interpolation
= fields
[i
].interpolation
;
137 this->fields
.structure
[i
].centroid
= fields
[i
].centroid
;
138 this->fields
.structure
[i
].sample
= fields
[i
].sample
;
139 this->fields
.structure
[i
].matrix_layout
= fields
[i
].matrix_layout
;
145 glsl_type::contains_sampler() const
147 if (this->is_array()) {
148 return this->fields
.array
->contains_sampler();
149 } else if (this->is_record()) {
150 for (unsigned int i
= 0; i
< this->length
; i
++) {
151 if (this->fields
.structure
[i
].type
->contains_sampler())
156 return this->is_sampler();
162 glsl_type::contains_integer() const
164 if (this->is_array()) {
165 return this->fields
.array
->contains_integer();
166 } else if (this->is_record()) {
167 for (unsigned int i
= 0; i
< this->length
; i
++) {
168 if (this->fields
.structure
[i
].type
->contains_integer())
173 return this->is_integer();
178 glsl_type::contains_opaque() const {
180 case GLSL_TYPE_SAMPLER
:
181 case GLSL_TYPE_IMAGE
:
182 case GLSL_TYPE_ATOMIC_UINT
:
184 case GLSL_TYPE_ARRAY
:
185 return element_type()->contains_opaque();
186 case GLSL_TYPE_STRUCT
:
187 for (unsigned int i
= 0; i
< length
; i
++) {
188 if (fields
.structure
[i
].type
->contains_opaque())
198 glsl_type::sampler_index() const
200 const glsl_type
*const t
= (this->is_array()) ? this->fields
.array
: this;
202 assert(t
->is_sampler());
204 switch (t
->sampler_dimensionality
) {
205 case GLSL_SAMPLER_DIM_1D
:
206 return (t
->sampler_array
) ? TEXTURE_1D_ARRAY_INDEX
: TEXTURE_1D_INDEX
;
207 case GLSL_SAMPLER_DIM_2D
:
208 return (t
->sampler_array
) ? TEXTURE_2D_ARRAY_INDEX
: TEXTURE_2D_INDEX
;
209 case GLSL_SAMPLER_DIM_3D
:
210 return TEXTURE_3D_INDEX
;
211 case GLSL_SAMPLER_DIM_CUBE
:
212 return (t
->sampler_array
) ? TEXTURE_CUBE_ARRAY_INDEX
: TEXTURE_CUBE_INDEX
;
213 case GLSL_SAMPLER_DIM_RECT
:
214 return TEXTURE_RECT_INDEX
;
215 case GLSL_SAMPLER_DIM_BUF
:
216 return TEXTURE_BUFFER_INDEX
;
217 case GLSL_SAMPLER_DIM_EXTERNAL
:
218 return TEXTURE_EXTERNAL_INDEX
;
219 case GLSL_SAMPLER_DIM_MS
:
220 return (t
->sampler_array
) ? TEXTURE_2D_MULTISAMPLE_ARRAY_INDEX
: TEXTURE_2D_MULTISAMPLE_INDEX
;
222 assert(!"Should not get here.");
223 return TEXTURE_BUFFER_INDEX
;
228 glsl_type::contains_image() const
230 if (this->is_array()) {
231 return this->fields
.array
->contains_image();
232 } else if (this->is_record()) {
233 for (unsigned int i
= 0; i
< this->length
; i
++) {
234 if (this->fields
.structure
[i
].type
->contains_image())
239 return this->is_image();
243 const glsl_type
*glsl_type::get_base_type() const
250 case GLSL_TYPE_FLOAT
:
260 const glsl_type
*glsl_type::get_scalar_type() const
262 const glsl_type
*type
= this;
265 while (type
->base_type
== GLSL_TYPE_ARRAY
)
266 type
= type
->fields
.array
;
268 /* Handle vectors and matrices */
269 switch (type
->base_type
) {
274 case GLSL_TYPE_FLOAT
:
279 /* Handle everything else */
286 _mesa_glsl_release_types(void)
288 if (glsl_type::array_types
!= NULL
) {
289 hash_table_dtor(glsl_type::array_types
);
290 glsl_type::array_types
= NULL
;
293 if (glsl_type::record_types
!= NULL
) {
294 hash_table_dtor(glsl_type::record_types
);
295 glsl_type::record_types
= NULL
;
300 glsl_type::glsl_type(const glsl_type
*array
, unsigned length
) :
301 base_type(GLSL_TYPE_ARRAY
),
302 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
303 sampler_type(0), interface_packing(0),
304 vector_elements(0), matrix_columns(0),
305 name(NULL
), length(length
)
307 this->fields
.array
= array
;
308 /* Inherit the gl type of the base. The GL type is used for
309 * uniform/statevar handling in Mesa and the arrayness of the type
310 * is represented by the size rather than the type.
312 this->gl_type
= array
->gl_type
;
314 /* Allow a maximum of 10 characters for the array size. This is enough
315 * for 32-bits of ~0. The extra 3 are for the '[', ']', and terminating
318 const unsigned name_length
= strlen(array
->name
) + 10 + 3;
319 char *const n
= (char *) ralloc_size(this->mem_ctx
, name_length
);
322 snprintf(n
, name_length
, "%s[]", array
->name
);
324 /* insert outermost dimensions in the correct spot
325 * otherwise the dimension order will be backwards
327 const char *pos
= strchr(array
->name
, '[');
329 int idx
= pos
- array
->name
;
330 snprintf(n
, idx
+1, "%s", array
->name
);
331 snprintf(n
+ idx
, name_length
- idx
, "[%u]%s",
332 length
, array
->name
+ idx
);
334 snprintf(n
, name_length
, "%s[%u]", array
->name
, length
);
343 glsl_type::vec(unsigned components
)
345 if (components
== 0 || components
> 4)
348 static const glsl_type
*const ts
[] = {
349 float_type
, vec2_type
, vec3_type
, vec4_type
351 return ts
[components
- 1];
356 glsl_type::ivec(unsigned components
)
358 if (components
== 0 || components
> 4)
361 static const glsl_type
*const ts
[] = {
362 int_type
, ivec2_type
, ivec3_type
, ivec4_type
364 return ts
[components
- 1];
369 glsl_type::uvec(unsigned components
)
371 if (components
== 0 || components
> 4)
374 static const glsl_type
*const ts
[] = {
375 uint_type
, uvec2_type
, uvec3_type
, uvec4_type
377 return ts
[components
- 1];
382 glsl_type::bvec(unsigned components
)
384 if (components
== 0 || components
> 4)
387 static const glsl_type
*const ts
[] = {
388 bool_type
, bvec2_type
, bvec3_type
, bvec4_type
390 return ts
[components
- 1];
395 glsl_type::get_instance(unsigned base_type
, unsigned rows
, unsigned columns
)
397 if (base_type
== GLSL_TYPE_VOID
)
400 if ((rows
< 1) || (rows
> 4) || (columns
< 1) || (columns
> 4))
403 /* Treat GLSL vectors as Nx1 matrices.
411 case GLSL_TYPE_FLOAT
:
419 if ((base_type
!= GLSL_TYPE_FLOAT
) || (rows
== 1))
422 /* GLSL matrix types are named mat{COLUMNS}x{ROWS}. Only the following
423 * combinations are valid:
431 #define IDX(c,r) (((c-1)*3) + (r-1))
433 switch (IDX(columns
, rows
)) {
434 case IDX(2,2): return mat2_type
;
435 case IDX(2,3): return mat2x3_type
;
436 case IDX(2,4): return mat2x4_type
;
437 case IDX(3,2): return mat3x2_type
;
438 case IDX(3,3): return mat3_type
;
439 case IDX(3,4): return mat3x4_type
;
440 case IDX(4,2): return mat4x2_type
;
441 case IDX(4,3): return mat4x3_type
;
442 case IDX(4,4): return mat4_type
;
443 default: return error_type
;
447 assert(!"Should not get here.");
453 glsl_type::get_array_instance(const glsl_type
*base
, unsigned array_size
)
456 if (array_types
== NULL
) {
457 array_types
= hash_table_ctor(64, hash_table_string_hash
,
458 hash_table_string_compare
);
461 /* Generate a name using the base type pointer in the key. This is
462 * done because the name of the base type may not be unique across
463 * shaders. For example, two shaders may have different record types
467 snprintf(key
, sizeof(key
), "%p[%u]", (void *) base
, array_size
);
469 const glsl_type
*t
= (glsl_type
*) hash_table_find(array_types
, key
);
471 t
= new glsl_type(base
, array_size
);
473 hash_table_insert(array_types
, (void *) t
, ralloc_strdup(mem_ctx
, key
));
476 assert(t
->base_type
== GLSL_TYPE_ARRAY
);
477 assert(t
->length
== array_size
);
478 assert(t
->fields
.array
== base
);
485 glsl_type::record_compare(const glsl_type
*b
) const
487 if (this->length
!= b
->length
)
490 if (this->interface_packing
!= b
->interface_packing
)
493 /* From the GLSL 4.20 specification (Sec 4.2):
495 * "Structures must have the same name, sequence of type names, and
496 * type definitions, and field names to be considered the same type."
498 * GLSL ES behaves the same (Ver 1.00 Sec 4.2.4, Ver 3.00 Sec 4.2.5).
500 * Note that we cannot force type name check when comparing unnamed
501 * structure types, these have a unique name assigned during parsing.
503 if (!this->is_anonymous() && !b
->is_anonymous())
504 if (strcmp(this->name
, b
->name
) != 0)
507 for (unsigned i
= 0; i
< this->length
; i
++) {
508 if (this->fields
.structure
[i
].type
!= b
->fields
.structure
[i
].type
)
510 if (strcmp(this->fields
.structure
[i
].name
,
511 b
->fields
.structure
[i
].name
) != 0)
513 if (this->fields
.structure
[i
].matrix_layout
514 != b
->fields
.structure
[i
].matrix_layout
)
516 if (this->fields
.structure
[i
].location
517 != b
->fields
.structure
[i
].location
)
519 if (this->fields
.structure
[i
].interpolation
520 != b
->fields
.structure
[i
].interpolation
)
522 if (this->fields
.structure
[i
].centroid
523 != b
->fields
.structure
[i
].centroid
)
525 if (this->fields
.structure
[i
].sample
526 != b
->fields
.structure
[i
].sample
)
535 glsl_type::record_key_compare(const void *a
, const void *b
)
537 const glsl_type
*const key1
= (glsl_type
*) a
;
538 const glsl_type
*const key2
= (glsl_type
*) b
;
540 /* Return zero is the types match (there is zero difference) or non-zero
543 if (strcmp(key1
->name
, key2
->name
) != 0)
546 return !key1
->record_compare(key2
);
551 glsl_type::record_key_hash(const void *a
)
553 const glsl_type
*const key
= (glsl_type
*) a
;
557 size
= snprintf(hash_key
, sizeof(hash_key
), "%08x", key
->length
);
559 for (unsigned i
= 0; i
< key
->length
; i
++) {
560 if (size
>= sizeof(hash_key
))
563 size
+= snprintf(& hash_key
[size
], sizeof(hash_key
) - size
,
564 "%p", (void *) key
->fields
.structure
[i
].type
);
567 return hash_table_string_hash(& hash_key
);
572 glsl_type::get_record_instance(const glsl_struct_field
*fields
,
576 const glsl_type
key(fields
, num_fields
, name
);
578 if (record_types
== NULL
) {
579 record_types
= hash_table_ctor(64, record_key_hash
, record_key_compare
);
582 const glsl_type
*t
= (glsl_type
*) hash_table_find(record_types
, & key
);
584 t
= new glsl_type(fields
, num_fields
, name
);
586 hash_table_insert(record_types
, (void *) t
, t
);
589 assert(t
->base_type
== GLSL_TYPE_STRUCT
);
590 assert(t
->length
== num_fields
);
591 assert(strcmp(t
->name
, name
) == 0);
598 glsl_type::get_interface_instance(const glsl_struct_field
*fields
,
600 enum glsl_interface_packing packing
,
601 const char *block_name
)
603 const glsl_type
key(fields
, num_fields
, packing
, block_name
);
605 if (interface_types
== NULL
) {
606 interface_types
= hash_table_ctor(64, record_key_hash
, record_key_compare
);
609 const glsl_type
*t
= (glsl_type
*) hash_table_find(interface_types
, & key
);
611 t
= new glsl_type(fields
, num_fields
, packing
, block_name
);
613 hash_table_insert(interface_types
, (void *) t
, t
);
616 assert(t
->base_type
== GLSL_TYPE_INTERFACE
);
617 assert(t
->length
== num_fields
);
618 assert(strcmp(t
->name
, block_name
) == 0);
625 glsl_type::field_type(const char *name
) const
627 if (this->base_type
!= GLSL_TYPE_STRUCT
628 && this->base_type
!= GLSL_TYPE_INTERFACE
)
631 for (unsigned i
= 0; i
< this->length
; i
++) {
632 if (strcmp(name
, this->fields
.structure
[i
].name
) == 0)
633 return this->fields
.structure
[i
].type
;
641 glsl_type::field_index(const char *name
) const
643 if (this->base_type
!= GLSL_TYPE_STRUCT
644 && this->base_type
!= GLSL_TYPE_INTERFACE
)
647 for (unsigned i
= 0; i
< this->length
; i
++) {
648 if (strcmp(name
, this->fields
.structure
[i
].name
) == 0)
657 glsl_type::component_slots() const
659 switch (this->base_type
) {
662 case GLSL_TYPE_FLOAT
:
664 return this->components();
666 case GLSL_TYPE_STRUCT
:
667 case GLSL_TYPE_INTERFACE
: {
670 for (unsigned i
= 0; i
< this->length
; i
++)
671 size
+= this->fields
.structure
[i
].type
->component_slots();
676 case GLSL_TYPE_ARRAY
:
677 return this->length
* this->fields
.array
->component_slots();
679 case GLSL_TYPE_IMAGE
:
682 case GLSL_TYPE_SAMPLER
:
683 case GLSL_TYPE_ATOMIC_UINT
:
685 case GLSL_TYPE_ERROR
:
693 glsl_type::uniform_locations() const
697 switch (this->base_type
) {
700 case GLSL_TYPE_FLOAT
:
702 case GLSL_TYPE_SAMPLER
:
703 case GLSL_TYPE_IMAGE
:
706 case GLSL_TYPE_STRUCT
:
707 case GLSL_TYPE_INTERFACE
:
708 for (unsigned i
= 0; i
< this->length
; i
++)
709 size
+= this->fields
.structure
[i
].type
->uniform_locations();
711 case GLSL_TYPE_ARRAY
:
712 return this->length
* this->fields
.array
->uniform_locations();
719 glsl_type::can_implicitly_convert_to(const glsl_type
*desired
,
720 _mesa_glsl_parse_state
*state
) const
725 /* There is no conversion among matrix types. */
726 if (this->matrix_columns
> 1 || desired
->matrix_columns
> 1)
729 /* Vector size must match. */
730 if (this->vector_elements
!= desired
->vector_elements
)
733 /* int and uint can be converted to float. */
734 if (desired
->is_float() && this->is_integer())
737 /* With GLSL 4.0 / ARB_gpu_shader5, int can be converted to uint.
738 * Note that state may be NULL here, when resolving function calls in the
739 * linker. By this time, all the state-dependent checks have already
740 * happened though, so allow anything that's allowed in any shader version. */
741 if ((!state
|| state
->is_version(400, 0) || state
->ARB_gpu_shader5_enable
) &&
742 desired
->base_type
== GLSL_TYPE_UINT
&& this->base_type
== GLSL_TYPE_INT
)
749 glsl_type::std140_base_alignment(bool row_major
) const
751 /* (1) If the member is a scalar consuming <N> basic machine units, the
752 * base alignment is <N>.
754 * (2) If the member is a two- or four-component vector with components
755 * consuming <N> basic machine units, the base alignment is 2<N> or
756 * 4<N>, respectively.
758 * (3) If the member is a three-component vector with components consuming
759 * <N> basic machine units, the base alignment is 4<N>.
761 if (this->is_scalar() || this->is_vector()) {
762 switch (this->vector_elements
) {
773 /* (4) If the member is an array of scalars or vectors, the base alignment
774 * and array stride are set to match the base alignment of a single
775 * array element, according to rules (1), (2), and (3), and rounded up
776 * to the base alignment of a vec4. The array may have padding at the
777 * end; the base offset of the member following the array is rounded up
778 * to the next multiple of the base alignment.
780 * (6) If the member is an array of <S> column-major matrices with <C>
781 * columns and <R> rows, the matrix is stored identically to a row of
782 * <S>*<C> column vectors with <R> components each, according to rule
785 * (8) If the member is an array of <S> row-major matrices with <C> columns
786 * and <R> rows, the matrix is stored identically to a row of <S>*<R>
787 * row vectors with <C> components each, according to rule (4).
789 * (10) If the member is an array of <S> structures, the <S> elements of
790 * the array are laid out in order, according to rule (9).
792 if (this->is_array()) {
793 if (this->fields
.array
->is_scalar() ||
794 this->fields
.array
->is_vector() ||
795 this->fields
.array
->is_matrix()) {
796 return MAX2(this->fields
.array
->std140_base_alignment(row_major
), 16);
798 assert(this->fields
.array
->is_record());
799 return this->fields
.array
->std140_base_alignment(row_major
);
803 /* (5) If the member is a column-major matrix with <C> columns and
804 * <R> rows, the matrix is stored identically to an array of
805 * <C> column vectors with <R> components each, according to
808 * (7) If the member is a row-major matrix with <C> columns and <R>
809 * rows, the matrix is stored identically to an array of <R>
810 * row vectors with <C> components each, according to rule (4).
812 if (this->is_matrix()) {
813 const struct glsl_type
*vec_type
, *array_type
;
814 int c
= this->matrix_columns
;
815 int r
= this->vector_elements
;
818 vec_type
= get_instance(GLSL_TYPE_FLOAT
, c
, 1);
819 array_type
= glsl_type::get_array_instance(vec_type
, r
);
821 vec_type
= get_instance(GLSL_TYPE_FLOAT
, r
, 1);
822 array_type
= glsl_type::get_array_instance(vec_type
, c
);
825 return array_type
->std140_base_alignment(false);
828 /* (9) If the member is a structure, the base alignment of the
829 * structure is <N>, where <N> is the largest base alignment
830 * value of any of its members, and rounded up to the base
831 * alignment of a vec4. The individual members of this
832 * sub-structure are then assigned offsets by applying this set
833 * of rules recursively, where the base offset of the first
834 * member of the sub-structure is equal to the aligned offset
835 * of the structure. The structure may have padding at the end;
836 * the base offset of the member following the sub-structure is
837 * rounded up to the next multiple of the base alignment of the
840 if (this->is_record()) {
841 unsigned base_alignment
= 16;
842 for (unsigned i
= 0; i
< this->length
; i
++) {
843 bool field_row_major
= row_major
;
844 const enum glsl_matrix_layout matrix_layout
=
845 glsl_matrix_layout(this->fields
.structure
[i
].matrix_layout
);
846 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
847 field_row_major
= true;
848 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
849 field_row_major
= false;
852 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
853 base_alignment
= MAX2(base_alignment
,
854 field_type
->std140_base_alignment(field_row_major
));
856 return base_alignment
;
859 assert(!"not reached");
864 glsl_type::std140_size(bool row_major
) const
866 /* (1) If the member is a scalar consuming <N> basic machine units, the
867 * base alignment is <N>.
869 * (2) If the member is a two- or four-component vector with components
870 * consuming <N> basic machine units, the base alignment is 2<N> or
871 * 4<N>, respectively.
873 * (3) If the member is a three-component vector with components consuming
874 * <N> basic machine units, the base alignment is 4<N>.
876 if (this->is_scalar() || this->is_vector()) {
877 return this->vector_elements
* 4;
880 /* (5) If the member is a column-major matrix with <C> columns and
881 * <R> rows, the matrix is stored identically to an array of
882 * <C> column vectors with <R> components each, according to
885 * (6) If the member is an array of <S> column-major matrices with <C>
886 * columns and <R> rows, the matrix is stored identically to a row of
887 * <S>*<C> column vectors with <R> components each, according to rule
890 * (7) If the member is a row-major matrix with <C> columns and <R>
891 * rows, the matrix is stored identically to an array of <R>
892 * row vectors with <C> components each, according to rule (4).
894 * (8) If the member is an array of <S> row-major matrices with <C> columns
895 * and <R> rows, the matrix is stored identically to a row of <S>*<R>
896 * row vectors with <C> components each, according to rule (4).
898 if (this->without_array()->is_matrix()) {
899 const struct glsl_type
*element_type
;
900 const struct glsl_type
*vec_type
;
901 unsigned int array_len
;
903 if (this->is_array()) {
904 element_type
= this->fields
.array
;
905 array_len
= this->length
;
912 vec_type
= get_instance(GLSL_TYPE_FLOAT
,
913 element_type
->matrix_columns
, 1);
914 array_len
*= element_type
->vector_elements
;
916 vec_type
= get_instance(GLSL_TYPE_FLOAT
,
917 element_type
->vector_elements
, 1);
918 array_len
*= element_type
->matrix_columns
;
920 const glsl_type
*array_type
= glsl_type::get_array_instance(vec_type
,
923 return array_type
->std140_size(false);
926 /* (4) If the member is an array of scalars or vectors, the base alignment
927 * and array stride are set to match the base alignment of a single
928 * array element, according to rules (1), (2), and (3), and rounded up
929 * to the base alignment of a vec4. The array may have padding at the
930 * end; the base offset of the member following the array is rounded up
931 * to the next multiple of the base alignment.
933 * (10) If the member is an array of <S> structures, the <S> elements of
934 * the array are laid out in order, according to rule (9).
936 if (this->is_array()) {
937 if (this->fields
.array
->is_record()) {
938 return this->length
* this->fields
.array
->std140_size(row_major
);
940 unsigned element_base_align
=
941 this->fields
.array
->std140_base_alignment(row_major
);
942 return this->length
* MAX2(element_base_align
, 16);
946 /* (9) If the member is a structure, the base alignment of the
947 * structure is <N>, where <N> is the largest base alignment
948 * value of any of its members, and rounded up to the base
949 * alignment of a vec4. The individual members of this
950 * sub-structure are then assigned offsets by applying this set
951 * of rules recursively, where the base offset of the first
952 * member of the sub-structure is equal to the aligned offset
953 * of the structure. The structure may have padding at the end;
954 * the base offset of the member following the sub-structure is
955 * rounded up to the next multiple of the base alignment of the
958 if (this->is_record()) {
960 unsigned max_align
= 0;
962 for (unsigned i
= 0; i
< this->length
; i
++) {
963 bool field_row_major
= row_major
;
964 const enum glsl_matrix_layout matrix_layout
=
965 glsl_matrix_layout(this->fields
.structure
[i
].matrix_layout
);
966 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
967 field_row_major
= true;
968 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
969 field_row_major
= false;
972 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
973 unsigned align
= field_type
->std140_base_alignment(field_row_major
);
974 size
= glsl_align(size
, align
);
975 size
+= field_type
->std140_size(field_row_major
);
977 max_align
= MAX2(align
, max_align
);
979 if (field_type
->is_record() && (i
+ 1 < this->length
))
980 size
= glsl_align(size
, 16);
982 size
= glsl_align(size
, MAX2(max_align
, 16));
986 assert(!"not reached");
992 glsl_type::count_attribute_slots() const
994 /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
996 * "A scalar input counts the same amount against this limit as a vec4,
997 * so applications may want to consider packing groups of four
998 * unrelated float inputs together into a vector to better utilize the
999 * capabilities of the underlying hardware. A matrix input will use up
1000 * multiple locations. The number of locations used will equal the
1001 * number of columns in the matrix."
1003 * The spec does not explicitly say how arrays are counted. However, it
1004 * should be safe to assume the total number of slots consumed by an array
1005 * is the number of entries in the array multiplied by the number of slots
1006 * consumed by a single element of the array.
1008 * The spec says nothing about how structs are counted, because vertex
1009 * attributes are not allowed to be (or contain) structs. However, Mesa
1010 * allows varying structs, the number of varying slots taken up by a
1011 * varying struct is simply equal to the sum of the number of slots taken
1012 * up by each element.
1014 switch (this->base_type
) {
1015 case GLSL_TYPE_UINT
:
1017 case GLSL_TYPE_FLOAT
:
1018 case GLSL_TYPE_BOOL
:
1019 return this->matrix_columns
;
1021 case GLSL_TYPE_STRUCT
:
1022 case GLSL_TYPE_INTERFACE
: {
1025 for (unsigned i
= 0; i
< this->length
; i
++)
1026 size
+= this->fields
.structure
[i
].type
->count_attribute_slots();
1031 case GLSL_TYPE_ARRAY
:
1032 return this->length
* this->fields
.array
->count_attribute_slots();
1034 case GLSL_TYPE_SAMPLER
:
1035 case GLSL_TYPE_IMAGE
:
1036 case GLSL_TYPE_ATOMIC_UINT
:
1037 case GLSL_TYPE_VOID
:
1038 case GLSL_TYPE_ERROR
:
1042 assert(!"Unexpected type in count_attribute_slots()");
1048 glsl_type::coordinate_components() const
1052 switch (sampler_dimensionality
) {
1053 case GLSL_SAMPLER_DIM_1D
:
1054 case GLSL_SAMPLER_DIM_BUF
:
1057 case GLSL_SAMPLER_DIM_2D
:
1058 case GLSL_SAMPLER_DIM_RECT
:
1059 case GLSL_SAMPLER_DIM_MS
:
1060 case GLSL_SAMPLER_DIM_EXTERNAL
:
1063 case GLSL_SAMPLER_DIM_3D
:
1064 case GLSL_SAMPLER_DIM_CUBE
:
1068 assert(!"Should not get here.");
1073 /* Array textures need an additional component for the array index. */