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 for (unsigned i
= 0; i
< this->length
; i
++) {
494 if (this->fields
.structure
[i
].type
!= b
->fields
.structure
[i
].type
)
496 if (strcmp(this->fields
.structure
[i
].name
,
497 b
->fields
.structure
[i
].name
) != 0)
499 if (this->fields
.structure
[i
].matrix_layout
500 != b
->fields
.structure
[i
].matrix_layout
)
502 if (this->fields
.structure
[i
].location
503 != b
->fields
.structure
[i
].location
)
505 if (this->fields
.structure
[i
].interpolation
506 != b
->fields
.structure
[i
].interpolation
)
508 if (this->fields
.structure
[i
].centroid
509 != b
->fields
.structure
[i
].centroid
)
511 if (this->fields
.structure
[i
].sample
512 != b
->fields
.structure
[i
].sample
)
521 glsl_type::record_key_compare(const void *a
, const void *b
)
523 const glsl_type
*const key1
= (glsl_type
*) a
;
524 const glsl_type
*const key2
= (glsl_type
*) b
;
526 /* Return zero is the types match (there is zero difference) or non-zero
529 if (strcmp(key1
->name
, key2
->name
) != 0)
532 return !key1
->record_compare(key2
);
537 glsl_type::record_key_hash(const void *a
)
539 const glsl_type
*const key
= (glsl_type
*) a
;
543 size
= snprintf(hash_key
, sizeof(hash_key
), "%08x", key
->length
);
545 for (unsigned i
= 0; i
< key
->length
; i
++) {
546 if (size
>= sizeof(hash_key
))
549 size
+= snprintf(& hash_key
[size
], sizeof(hash_key
) - size
,
550 "%p", (void *) key
->fields
.structure
[i
].type
);
553 return hash_table_string_hash(& hash_key
);
558 glsl_type::get_record_instance(const glsl_struct_field
*fields
,
562 const glsl_type
key(fields
, num_fields
, name
);
564 if (record_types
== NULL
) {
565 record_types
= hash_table_ctor(64, record_key_hash
, record_key_compare
);
568 const glsl_type
*t
= (glsl_type
*) hash_table_find(record_types
, & key
);
570 t
= new glsl_type(fields
, num_fields
, name
);
572 hash_table_insert(record_types
, (void *) t
, t
);
575 assert(t
->base_type
== GLSL_TYPE_STRUCT
);
576 assert(t
->length
== num_fields
);
577 assert(strcmp(t
->name
, name
) == 0);
584 glsl_type::get_interface_instance(const glsl_struct_field
*fields
,
586 enum glsl_interface_packing packing
,
587 const char *block_name
)
589 const glsl_type
key(fields
, num_fields
, packing
, block_name
);
591 if (interface_types
== NULL
) {
592 interface_types
= hash_table_ctor(64, record_key_hash
, record_key_compare
);
595 const glsl_type
*t
= (glsl_type
*) hash_table_find(interface_types
, & key
);
597 t
= new glsl_type(fields
, num_fields
, packing
, block_name
);
599 hash_table_insert(interface_types
, (void *) t
, t
);
602 assert(t
->base_type
== GLSL_TYPE_INTERFACE
);
603 assert(t
->length
== num_fields
);
604 assert(strcmp(t
->name
, block_name
) == 0);
611 glsl_type::field_type(const char *name
) const
613 if (this->base_type
!= GLSL_TYPE_STRUCT
614 && this->base_type
!= GLSL_TYPE_INTERFACE
)
617 for (unsigned i
= 0; i
< this->length
; i
++) {
618 if (strcmp(name
, this->fields
.structure
[i
].name
) == 0)
619 return this->fields
.structure
[i
].type
;
627 glsl_type::field_index(const char *name
) const
629 if (this->base_type
!= GLSL_TYPE_STRUCT
630 && this->base_type
!= GLSL_TYPE_INTERFACE
)
633 for (unsigned i
= 0; i
< this->length
; i
++) {
634 if (strcmp(name
, this->fields
.structure
[i
].name
) == 0)
643 glsl_type::component_slots() const
645 switch (this->base_type
) {
648 case GLSL_TYPE_FLOAT
:
650 return this->components();
652 case GLSL_TYPE_STRUCT
:
653 case GLSL_TYPE_INTERFACE
: {
656 for (unsigned i
= 0; i
< this->length
; i
++)
657 size
+= this->fields
.structure
[i
].type
->component_slots();
662 case GLSL_TYPE_ARRAY
:
663 return this->length
* this->fields
.array
->component_slots();
665 case GLSL_TYPE_IMAGE
:
668 case GLSL_TYPE_SAMPLER
:
669 case GLSL_TYPE_ATOMIC_UINT
:
671 case GLSL_TYPE_ERROR
:
679 glsl_type::uniform_locations() const
681 if (this->is_matrix())
686 switch (this->base_type
) {
687 case GLSL_TYPE_STRUCT
:
688 case GLSL_TYPE_INTERFACE
:
689 for (unsigned i
= 0; i
< this->length
; i
++)
690 size
+= this->fields
.structure
[i
].type
->uniform_locations();
692 case GLSL_TYPE_ARRAY
:
693 return this->length
* this->fields
.array
->uniform_locations();
698 /* The location count for many types match with component_slots() result,
699 * all expections should be handled above.
701 return component_slots();
705 glsl_type::can_implicitly_convert_to(const glsl_type
*desired
,
706 _mesa_glsl_parse_state
*state
) const
711 /* There is no conversion among matrix types. */
712 if (this->matrix_columns
> 1 || desired
->matrix_columns
> 1)
715 /* Vector size must match. */
716 if (this->vector_elements
!= desired
->vector_elements
)
719 /* int and uint can be converted to float. */
720 if (desired
->is_float() && this->is_integer())
723 /* With GLSL 4.0 / ARB_gpu_shader5, int can be converted to uint.
724 * Note that state may be NULL here, when resolving function calls in the
725 * linker. By this time, all the state-dependent checks have already
726 * happened though, so allow anything that's allowed in any shader version. */
727 if ((!state
|| state
->is_version(400, 0) || state
->ARB_gpu_shader5_enable
) &&
728 desired
->base_type
== GLSL_TYPE_UINT
&& this->base_type
== GLSL_TYPE_INT
)
735 glsl_type::std140_base_alignment(bool row_major
) const
737 /* (1) If the member is a scalar consuming <N> basic machine units, the
738 * base alignment is <N>.
740 * (2) If the member is a two- or four-component vector with components
741 * consuming <N> basic machine units, the base alignment is 2<N> or
742 * 4<N>, respectively.
744 * (3) If the member is a three-component vector with components consuming
745 * <N> basic machine units, the base alignment is 4<N>.
747 if (this->is_scalar() || this->is_vector()) {
748 switch (this->vector_elements
) {
759 /* (4) If the member is an array of scalars or vectors, the base alignment
760 * and array stride are set to match the base alignment of a single
761 * array element, according to rules (1), (2), and (3), and rounded up
762 * to the base alignment of a vec4. The array may have padding at the
763 * end; the base offset of the member following the array is rounded up
764 * to the next multiple of the base alignment.
766 * (6) If the member is an array of <S> column-major matrices with <C>
767 * columns and <R> rows, the matrix is stored identically to a row of
768 * <S>*<C> column vectors with <R> components each, according to rule
771 * (8) If the member is an array of <S> row-major matrices with <C> columns
772 * and <R> rows, the matrix is stored identically to a row of <S>*<R>
773 * row vectors with <C> components each, according to rule (4).
775 * (10) If the member is an array of <S> structures, the <S> elements of
776 * the array are laid out in order, according to rule (9).
778 if (this->is_array()) {
779 if (this->fields
.array
->is_scalar() ||
780 this->fields
.array
->is_vector() ||
781 this->fields
.array
->is_matrix()) {
782 return MAX2(this->fields
.array
->std140_base_alignment(row_major
), 16);
784 assert(this->fields
.array
->is_record());
785 return this->fields
.array
->std140_base_alignment(row_major
);
789 /* (5) If the member is a column-major matrix with <C> columns and
790 * <R> rows, the matrix is stored identically to an array of
791 * <C> column vectors with <R> components each, according to
794 * (7) If the member is a row-major matrix with <C> columns and <R>
795 * rows, the matrix is stored identically to an array of <R>
796 * row vectors with <C> components each, according to rule (4).
798 if (this->is_matrix()) {
799 const struct glsl_type
*vec_type
, *array_type
;
800 int c
= this->matrix_columns
;
801 int r
= this->vector_elements
;
804 vec_type
= get_instance(GLSL_TYPE_FLOAT
, c
, 1);
805 array_type
= glsl_type::get_array_instance(vec_type
, r
);
807 vec_type
= get_instance(GLSL_TYPE_FLOAT
, r
, 1);
808 array_type
= glsl_type::get_array_instance(vec_type
, c
);
811 return array_type
->std140_base_alignment(false);
814 /* (9) If the member is a structure, the base alignment of the
815 * structure is <N>, where <N> is the largest base alignment
816 * value of any of its members, and rounded up to the base
817 * alignment of a vec4. The individual members of this
818 * sub-structure are then assigned offsets by applying this set
819 * of rules recursively, where the base offset of the first
820 * member of the sub-structure is equal to the aligned offset
821 * of the structure. The structure may have padding at the end;
822 * the base offset of the member following the sub-structure is
823 * rounded up to the next multiple of the base alignment of the
826 if (this->is_record()) {
827 unsigned base_alignment
= 16;
828 for (unsigned i
= 0; i
< this->length
; i
++) {
829 bool field_row_major
= row_major
;
830 const enum glsl_matrix_layout matrix_layout
=
831 glsl_matrix_layout(this->fields
.structure
[i
].matrix_layout
);
832 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
833 field_row_major
= true;
834 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
835 field_row_major
= false;
838 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
839 base_alignment
= MAX2(base_alignment
,
840 field_type
->std140_base_alignment(field_row_major
));
842 return base_alignment
;
845 assert(!"not reached");
850 glsl_type::std140_size(bool row_major
) const
852 /* (1) If the member is a scalar consuming <N> basic machine units, the
853 * base alignment is <N>.
855 * (2) If the member is a two- or four-component vector with components
856 * consuming <N> basic machine units, the base alignment is 2<N> or
857 * 4<N>, respectively.
859 * (3) If the member is a three-component vector with components consuming
860 * <N> basic machine units, the base alignment is 4<N>.
862 if (this->is_scalar() || this->is_vector()) {
863 return this->vector_elements
* 4;
866 /* (5) If the member is a column-major matrix with <C> columns and
867 * <R> rows, the matrix is stored identically to an array of
868 * <C> column vectors with <R> components each, according to
871 * (6) If the member is an array of <S> column-major matrices with <C>
872 * columns and <R> rows, the matrix is stored identically to a row of
873 * <S>*<C> column vectors with <R> components each, according to rule
876 * (7) If the member is a row-major matrix with <C> columns and <R>
877 * rows, the matrix is stored identically to an array of <R>
878 * row vectors with <C> components each, according to rule (4).
880 * (8) If the member is an array of <S> row-major matrices with <C> columns
881 * and <R> rows, the matrix is stored identically to a row of <S>*<R>
882 * row vectors with <C> components each, according to rule (4).
884 if (this->without_array()->is_matrix()) {
885 const struct glsl_type
*element_type
;
886 const struct glsl_type
*vec_type
;
887 unsigned int array_len
;
889 if (this->is_array()) {
890 element_type
= this->fields
.array
;
891 array_len
= this->length
;
898 vec_type
= get_instance(GLSL_TYPE_FLOAT
,
899 element_type
->matrix_columns
, 1);
900 array_len
*= element_type
->vector_elements
;
902 vec_type
= get_instance(GLSL_TYPE_FLOAT
,
903 element_type
->vector_elements
, 1);
904 array_len
*= element_type
->matrix_columns
;
906 const glsl_type
*array_type
= glsl_type::get_array_instance(vec_type
,
909 return array_type
->std140_size(false);
912 /* (4) If the member is an array of scalars or vectors, the base alignment
913 * and array stride are set to match the base alignment of a single
914 * array element, according to rules (1), (2), and (3), and rounded up
915 * to the base alignment of a vec4. The array may have padding at the
916 * end; the base offset of the member following the array is rounded up
917 * to the next multiple of the base alignment.
919 * (10) If the member is an array of <S> structures, the <S> elements of
920 * the array are laid out in order, according to rule (9).
922 if (this->is_array()) {
923 if (this->fields
.array
->is_record()) {
924 return this->length
* this->fields
.array
->std140_size(row_major
);
926 unsigned element_base_align
=
927 this->fields
.array
->std140_base_alignment(row_major
);
928 return this->length
* MAX2(element_base_align
, 16);
932 /* (9) If the member is a structure, the base alignment of the
933 * structure is <N>, where <N> is the largest base alignment
934 * value of any of its members, and rounded up to the base
935 * alignment of a vec4. The individual members of this
936 * sub-structure are then assigned offsets by applying this set
937 * of rules recursively, where the base offset of the first
938 * member of the sub-structure is equal to the aligned offset
939 * of the structure. The structure may have padding at the end;
940 * the base offset of the member following the sub-structure is
941 * rounded up to the next multiple of the base alignment of the
944 if (this->is_record()) {
946 unsigned max_align
= 0;
948 for (unsigned i
= 0; i
< this->length
; i
++) {
949 bool field_row_major
= row_major
;
950 const enum glsl_matrix_layout matrix_layout
=
951 glsl_matrix_layout(this->fields
.structure
[i
].matrix_layout
);
952 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
953 field_row_major
= true;
954 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
955 field_row_major
= false;
958 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
959 unsigned align
= field_type
->std140_base_alignment(field_row_major
);
960 size
= glsl_align(size
, align
);
961 size
+= field_type
->std140_size(field_row_major
);
963 max_align
= MAX2(align
, max_align
);
965 if (field_type
->is_record() && (i
+ 1 < this->length
))
966 size
= glsl_align(size
, 16);
968 size
= glsl_align(size
, max_align
);
972 assert(!"not reached");
978 glsl_type::count_attribute_slots() const
980 /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
982 * "A scalar input counts the same amount against this limit as a vec4,
983 * so applications may want to consider packing groups of four
984 * unrelated float inputs together into a vector to better utilize the
985 * capabilities of the underlying hardware. A matrix input will use up
986 * multiple locations. The number of locations used will equal the
987 * number of columns in the matrix."
989 * The spec does not explicitly say how arrays are counted. However, it
990 * should be safe to assume the total number of slots consumed by an array
991 * is the number of entries in the array multiplied by the number of slots
992 * consumed by a single element of the array.
994 * The spec says nothing about how structs are counted, because vertex
995 * attributes are not allowed to be (or contain) structs. However, Mesa
996 * allows varying structs, the number of varying slots taken up by a
997 * varying struct is simply equal to the sum of the number of slots taken
998 * up by each element.
1000 switch (this->base_type
) {
1001 case GLSL_TYPE_UINT
:
1003 case GLSL_TYPE_FLOAT
:
1004 case GLSL_TYPE_BOOL
:
1005 return this->matrix_columns
;
1007 case GLSL_TYPE_STRUCT
:
1008 case GLSL_TYPE_INTERFACE
: {
1011 for (unsigned i
= 0; i
< this->length
; i
++)
1012 size
+= this->fields
.structure
[i
].type
->count_attribute_slots();
1017 case GLSL_TYPE_ARRAY
:
1018 return this->length
* this->fields
.array
->count_attribute_slots();
1020 case GLSL_TYPE_SAMPLER
:
1021 case GLSL_TYPE_IMAGE
:
1022 case GLSL_TYPE_ATOMIC_UINT
:
1023 case GLSL_TYPE_VOID
:
1024 case GLSL_TYPE_ERROR
:
1028 assert(!"Unexpected type in count_attribute_slots()");
1034 glsl_type::coordinate_components() const
1038 switch (sampler_dimensionality
) {
1039 case GLSL_SAMPLER_DIM_1D
:
1040 case GLSL_SAMPLER_DIM_BUF
:
1043 case GLSL_SAMPLER_DIM_2D
:
1044 case GLSL_SAMPLER_DIM_RECT
:
1045 case GLSL_SAMPLER_DIM_MS
:
1046 case GLSL_SAMPLER_DIM_EXTERNAL
:
1049 case GLSL_SAMPLER_DIM_3D
:
1050 case GLSL_SAMPLER_DIM_CUBE
:
1054 assert(!"Should not get here.");
1059 /* Array textures need an additional component for the array index. */