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 mtx_t
glsl_type::mutex
= _MTX_INITIALIZER_NP
;
33 hash_table
*glsl_type::array_types
= NULL
;
34 hash_table
*glsl_type::record_types
= NULL
;
35 hash_table
*glsl_type::interface_types
= NULL
;
36 void *glsl_type::mem_ctx
= NULL
;
39 glsl_type::init_ralloc_type_ctx(void)
41 if (glsl_type::mem_ctx
== NULL
) {
42 glsl_type::mem_ctx
= ralloc_autofree_context();
43 assert(glsl_type::mem_ctx
!= NULL
);
47 glsl_type::glsl_type(GLenum gl_type
,
48 glsl_base_type base_type
, unsigned vector_elements
,
49 unsigned matrix_columns
, const char *name
) :
52 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
53 sampler_type(0), interface_packing(0),
54 vector_elements(vector_elements
), matrix_columns(matrix_columns
),
57 mtx_lock(&glsl_type::mutex
);
59 init_ralloc_type_ctx();
61 this->name
= ralloc_strdup(this->mem_ctx
, name
);
63 mtx_unlock(&glsl_type::mutex
);
65 /* Neither dimension is zero or both dimensions are zero.
67 assert((vector_elements
== 0) == (matrix_columns
== 0));
68 memset(& fields
, 0, sizeof(fields
));
71 glsl_type::glsl_type(GLenum gl_type
, glsl_base_type base_type
,
72 enum glsl_sampler_dim dim
, bool shadow
, bool array
,
73 unsigned type
, const char *name
) :
76 sampler_dimensionality(dim
), sampler_shadow(shadow
),
77 sampler_array(array
), sampler_type(type
), interface_packing(0),
80 mtx_lock(&glsl_type::mutex
);
82 init_ralloc_type_ctx();
84 this->name
= ralloc_strdup(this->mem_ctx
, name
);
86 mtx_unlock(&glsl_type::mutex
);
88 memset(& fields
, 0, sizeof(fields
));
90 if (base_type
== GLSL_TYPE_SAMPLER
) {
91 /* Samplers take no storage whatsoever. */
92 matrix_columns
= vector_elements
= 0;
94 matrix_columns
= vector_elements
= 1;
98 glsl_type::glsl_type(const glsl_struct_field
*fields
, unsigned num_fields
,
101 base_type(GLSL_TYPE_STRUCT
),
102 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
103 sampler_type(0), interface_packing(0),
104 vector_elements(0), matrix_columns(0),
109 mtx_lock(&glsl_type::mutex
);
111 init_ralloc_type_ctx();
112 assert(name
!= NULL
);
113 this->name
= ralloc_strdup(this->mem_ctx
, name
);
114 this->fields
.structure
= ralloc_array(this->mem_ctx
,
115 glsl_struct_field
, length
);
117 for (i
= 0; i
< length
; i
++) {
118 this->fields
.structure
[i
].type
= fields
[i
].type
;
119 this->fields
.structure
[i
].name
= ralloc_strdup(this->fields
.structure
,
121 this->fields
.structure
[i
].location
= fields
[i
].location
;
122 this->fields
.structure
[i
].interpolation
= fields
[i
].interpolation
;
123 this->fields
.structure
[i
].centroid
= fields
[i
].centroid
;
124 this->fields
.structure
[i
].sample
= fields
[i
].sample
;
125 this->fields
.structure
[i
].matrix_layout
= fields
[i
].matrix_layout
;
128 mtx_unlock(&glsl_type::mutex
);
131 glsl_type::glsl_type(const glsl_struct_field
*fields
, unsigned num_fields
,
132 enum glsl_interface_packing packing
, const char *name
) :
134 base_type(GLSL_TYPE_INTERFACE
),
135 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
136 sampler_type(0), interface_packing((unsigned) packing
),
137 vector_elements(0), matrix_columns(0),
142 mtx_lock(&glsl_type::mutex
);
144 init_ralloc_type_ctx();
145 assert(name
!= NULL
);
146 this->name
= ralloc_strdup(this->mem_ctx
, name
);
147 this->fields
.structure
= ralloc_array(this->mem_ctx
,
148 glsl_struct_field
, length
);
149 for (i
= 0; i
< length
; i
++) {
150 this->fields
.structure
[i
].type
= fields
[i
].type
;
151 this->fields
.structure
[i
].name
= ralloc_strdup(this->fields
.structure
,
153 this->fields
.structure
[i
].location
= fields
[i
].location
;
154 this->fields
.structure
[i
].interpolation
= fields
[i
].interpolation
;
155 this->fields
.structure
[i
].centroid
= fields
[i
].centroid
;
156 this->fields
.structure
[i
].sample
= fields
[i
].sample
;
157 this->fields
.structure
[i
].matrix_layout
= fields
[i
].matrix_layout
;
160 mtx_unlock(&glsl_type::mutex
);
165 glsl_type::contains_sampler() const
167 if (this->is_array()) {
168 return this->fields
.array
->contains_sampler();
169 } else if (this->is_record()) {
170 for (unsigned int i
= 0; i
< this->length
; i
++) {
171 if (this->fields
.structure
[i
].type
->contains_sampler())
176 return this->is_sampler();
182 glsl_type::contains_integer() const
184 if (this->is_array()) {
185 return this->fields
.array
->contains_integer();
186 } else if (this->is_record()) {
187 for (unsigned int i
= 0; i
< this->length
; i
++) {
188 if (this->fields
.structure
[i
].type
->contains_integer())
193 return this->is_integer();
198 glsl_type::contains_opaque() const {
200 case GLSL_TYPE_SAMPLER
:
201 case GLSL_TYPE_IMAGE
:
202 case GLSL_TYPE_ATOMIC_UINT
:
204 case GLSL_TYPE_ARRAY
:
205 return element_type()->contains_opaque();
206 case GLSL_TYPE_STRUCT
:
207 for (unsigned int i
= 0; i
< length
; i
++) {
208 if (fields
.structure
[i
].type
->contains_opaque())
218 glsl_type::sampler_index() const
220 const glsl_type
*const t
= (this->is_array()) ? this->fields
.array
: this;
222 assert(t
->is_sampler());
224 switch (t
->sampler_dimensionality
) {
225 case GLSL_SAMPLER_DIM_1D
:
226 return (t
->sampler_array
) ? TEXTURE_1D_ARRAY_INDEX
: TEXTURE_1D_INDEX
;
227 case GLSL_SAMPLER_DIM_2D
:
228 return (t
->sampler_array
) ? TEXTURE_2D_ARRAY_INDEX
: TEXTURE_2D_INDEX
;
229 case GLSL_SAMPLER_DIM_3D
:
230 return TEXTURE_3D_INDEX
;
231 case GLSL_SAMPLER_DIM_CUBE
:
232 return (t
->sampler_array
) ? TEXTURE_CUBE_ARRAY_INDEX
: TEXTURE_CUBE_INDEX
;
233 case GLSL_SAMPLER_DIM_RECT
:
234 return TEXTURE_RECT_INDEX
;
235 case GLSL_SAMPLER_DIM_BUF
:
236 return TEXTURE_BUFFER_INDEX
;
237 case GLSL_SAMPLER_DIM_EXTERNAL
:
238 return TEXTURE_EXTERNAL_INDEX
;
239 case GLSL_SAMPLER_DIM_MS
:
240 return (t
->sampler_array
) ? TEXTURE_2D_MULTISAMPLE_ARRAY_INDEX
: TEXTURE_2D_MULTISAMPLE_INDEX
;
242 assert(!"Should not get here.");
243 return TEXTURE_BUFFER_INDEX
;
248 glsl_type::contains_image() const
250 if (this->is_array()) {
251 return this->fields
.array
->contains_image();
252 } else if (this->is_record()) {
253 for (unsigned int i
= 0; i
< this->length
; i
++) {
254 if (this->fields
.structure
[i
].type
->contains_image())
259 return this->is_image();
263 const glsl_type
*glsl_type::get_base_type() const
270 case GLSL_TYPE_FLOAT
:
280 const glsl_type
*glsl_type::get_scalar_type() const
282 const glsl_type
*type
= this;
285 while (type
->base_type
== GLSL_TYPE_ARRAY
)
286 type
= type
->fields
.array
;
288 /* Handle vectors and matrices */
289 switch (type
->base_type
) {
294 case GLSL_TYPE_FLOAT
:
299 /* Handle everything else */
306 _mesa_glsl_release_types(void)
308 mtx_lock(&glsl_type::mutex
);
310 if (glsl_type::array_types
!= NULL
) {
311 hash_table_dtor(glsl_type::array_types
);
312 glsl_type::array_types
= NULL
;
315 if (glsl_type::record_types
!= NULL
) {
316 hash_table_dtor(glsl_type::record_types
);
317 glsl_type::record_types
= NULL
;
320 mtx_unlock(&glsl_type::mutex
);
324 glsl_type::glsl_type(const glsl_type
*array
, unsigned length
) :
325 base_type(GLSL_TYPE_ARRAY
),
326 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
327 sampler_type(0), interface_packing(0),
328 vector_elements(0), matrix_columns(0),
329 name(NULL
), length(length
)
331 this->fields
.array
= array
;
332 /* Inherit the gl type of the base. The GL type is used for
333 * uniform/statevar handling in Mesa and the arrayness of the type
334 * is represented by the size rather than the type.
336 this->gl_type
= array
->gl_type
;
338 /* Allow a maximum of 10 characters for the array size. This is enough
339 * for 32-bits of ~0. The extra 3 are for the '[', ']', and terminating
342 const unsigned name_length
= strlen(array
->name
) + 10 + 3;
344 mtx_lock(&glsl_type::mutex
);
345 char *const n
= (char *) ralloc_size(this->mem_ctx
, name_length
);
346 mtx_unlock(&glsl_type::mutex
);
349 snprintf(n
, name_length
, "%s[]", array
->name
);
351 /* insert outermost dimensions in the correct spot
352 * otherwise the dimension order will be backwards
354 const char *pos
= strchr(array
->name
, '[');
356 int idx
= pos
- array
->name
;
357 snprintf(n
, idx
+1, "%s", array
->name
);
358 snprintf(n
+ idx
, name_length
- idx
, "[%u]%s",
359 length
, array
->name
+ idx
);
361 snprintf(n
, name_length
, "%s[%u]", array
->name
, length
);
370 glsl_type::vec(unsigned components
)
372 if (components
== 0 || components
> 4)
375 static const glsl_type
*const ts
[] = {
376 float_type
, vec2_type
, vec3_type
, vec4_type
378 return ts
[components
- 1];
383 glsl_type::ivec(unsigned components
)
385 if (components
== 0 || components
> 4)
388 static const glsl_type
*const ts
[] = {
389 int_type
, ivec2_type
, ivec3_type
, ivec4_type
391 return ts
[components
- 1];
396 glsl_type::uvec(unsigned components
)
398 if (components
== 0 || components
> 4)
401 static const glsl_type
*const ts
[] = {
402 uint_type
, uvec2_type
, uvec3_type
, uvec4_type
404 return ts
[components
- 1];
409 glsl_type::bvec(unsigned components
)
411 if (components
== 0 || components
> 4)
414 static const glsl_type
*const ts
[] = {
415 bool_type
, bvec2_type
, bvec3_type
, bvec4_type
417 return ts
[components
- 1];
422 glsl_type::get_instance(unsigned base_type
, unsigned rows
, unsigned columns
)
424 if (base_type
== GLSL_TYPE_VOID
)
427 if ((rows
< 1) || (rows
> 4) || (columns
< 1) || (columns
> 4))
430 /* Treat GLSL vectors as Nx1 matrices.
438 case GLSL_TYPE_FLOAT
:
446 if ((base_type
!= GLSL_TYPE_FLOAT
) || (rows
== 1))
449 /* GLSL matrix types are named mat{COLUMNS}x{ROWS}. Only the following
450 * combinations are valid:
458 #define IDX(c,r) (((c-1)*3) + (r-1))
460 switch (IDX(columns
, rows
)) {
461 case IDX(2,2): return mat2_type
;
462 case IDX(2,3): return mat2x3_type
;
463 case IDX(2,4): return mat2x4_type
;
464 case IDX(3,2): return mat3x2_type
;
465 case IDX(3,3): return mat3_type
;
466 case IDX(3,4): return mat3x4_type
;
467 case IDX(4,2): return mat4x2_type
;
468 case IDX(4,3): return mat4x3_type
;
469 case IDX(4,4): return mat4_type
;
470 default: return error_type
;
474 assert(!"Should not get here.");
480 glsl_type::get_array_instance(const glsl_type
*base
, unsigned array_size
)
482 /* Generate a name using the base type pointer in the key. This is
483 * done because the name of the base type may not be unique across
484 * shaders. For example, two shaders may have different record types
488 snprintf(key
, sizeof(key
), "%p[%u]", (void *) base
, array_size
);
490 mtx_lock(&glsl_type::mutex
);
492 if (array_types
== NULL
) {
493 array_types
= hash_table_ctor(64, hash_table_string_hash
,
494 hash_table_string_compare
);
497 const glsl_type
*t
= (glsl_type
*) hash_table_find(array_types
, key
);
500 mtx_unlock(&glsl_type::mutex
);
501 t
= new glsl_type(base
, array_size
);
502 mtx_lock(&glsl_type::mutex
);
504 hash_table_insert(array_types
, (void *) t
, ralloc_strdup(mem_ctx
, key
));
507 assert(t
->base_type
== GLSL_TYPE_ARRAY
);
508 assert(t
->length
== array_size
);
509 assert(t
->fields
.array
== base
);
511 mtx_unlock(&glsl_type::mutex
);
518 glsl_type::record_compare(const glsl_type
*b
) const
520 if (this->length
!= b
->length
)
523 if (this->interface_packing
!= b
->interface_packing
)
526 /* From the GLSL 4.20 specification (Sec 4.2):
528 * "Structures must have the same name, sequence of type names, and
529 * type definitions, and field names to be considered the same type."
531 * GLSL ES behaves the same (Ver 1.00 Sec 4.2.4, Ver 3.00 Sec 4.2.5).
533 * Note that we cannot force type name check when comparing unnamed
534 * structure types, these have a unique name assigned during parsing.
536 if (!this->is_anonymous() && !b
->is_anonymous())
537 if (strcmp(this->name
, b
->name
) != 0)
540 for (unsigned i
= 0; i
< this->length
; i
++) {
541 if (this->fields
.structure
[i
].type
!= b
->fields
.structure
[i
].type
)
543 if (strcmp(this->fields
.structure
[i
].name
,
544 b
->fields
.structure
[i
].name
) != 0)
546 if (this->fields
.structure
[i
].matrix_layout
547 != b
->fields
.structure
[i
].matrix_layout
)
549 if (this->fields
.structure
[i
].location
550 != b
->fields
.structure
[i
].location
)
552 if (this->fields
.structure
[i
].interpolation
553 != b
->fields
.structure
[i
].interpolation
)
555 if (this->fields
.structure
[i
].centroid
556 != b
->fields
.structure
[i
].centroid
)
558 if (this->fields
.structure
[i
].sample
559 != b
->fields
.structure
[i
].sample
)
568 glsl_type::record_key_compare(const void *a
, const void *b
)
570 const glsl_type
*const key1
= (glsl_type
*) a
;
571 const glsl_type
*const key2
= (glsl_type
*) b
;
573 /* Return zero is the types match (there is zero difference) or non-zero
576 if (strcmp(key1
->name
, key2
->name
) != 0)
579 return !key1
->record_compare(key2
);
584 glsl_type::record_key_hash(const void *a
)
586 const glsl_type
*const key
= (glsl_type
*) a
;
590 size
= snprintf(hash_key
, sizeof(hash_key
), "%08x", key
->length
);
592 for (unsigned i
= 0; i
< key
->length
; i
++) {
593 if (size
>= sizeof(hash_key
))
596 size
+= snprintf(& hash_key
[size
], sizeof(hash_key
) - size
,
597 "%p", (void *) key
->fields
.structure
[i
].type
);
600 return hash_table_string_hash(& hash_key
);
605 glsl_type::get_record_instance(const glsl_struct_field
*fields
,
609 const glsl_type
key(fields
, num_fields
, name
);
611 mtx_lock(&glsl_type::mutex
);
613 if (record_types
== NULL
) {
614 record_types
= hash_table_ctor(64, record_key_hash
, record_key_compare
);
617 const glsl_type
*t
= (glsl_type
*) hash_table_find(record_types
, & key
);
619 mtx_unlock(&glsl_type::mutex
);
620 t
= new glsl_type(fields
, num_fields
, name
);
621 mtx_lock(&glsl_type::mutex
);
623 hash_table_insert(record_types
, (void *) t
, t
);
626 assert(t
->base_type
== GLSL_TYPE_STRUCT
);
627 assert(t
->length
== num_fields
);
628 assert(strcmp(t
->name
, name
) == 0);
630 mtx_unlock(&glsl_type::mutex
);
637 glsl_type::get_interface_instance(const glsl_struct_field
*fields
,
639 enum glsl_interface_packing packing
,
640 const char *block_name
)
642 const glsl_type
key(fields
, num_fields
, packing
, block_name
);
644 mtx_lock(&glsl_type::mutex
);
646 if (interface_types
== NULL
) {
647 interface_types
= hash_table_ctor(64, record_key_hash
, record_key_compare
);
650 const glsl_type
*t
= (glsl_type
*) hash_table_find(interface_types
, & key
);
652 mtx_unlock(&glsl_type::mutex
);
653 t
= new glsl_type(fields
, num_fields
, packing
, block_name
);
654 mtx_lock(&glsl_type::mutex
);
656 hash_table_insert(interface_types
, (void *) t
, t
);
659 assert(t
->base_type
== GLSL_TYPE_INTERFACE
);
660 assert(t
->length
== num_fields
);
661 assert(strcmp(t
->name
, block_name
) == 0);
663 mtx_unlock(&glsl_type::mutex
);
670 glsl_type::field_type(const char *name
) const
672 if (this->base_type
!= GLSL_TYPE_STRUCT
673 && this->base_type
!= GLSL_TYPE_INTERFACE
)
676 for (unsigned i
= 0; i
< this->length
; i
++) {
677 if (strcmp(name
, this->fields
.structure
[i
].name
) == 0)
678 return this->fields
.structure
[i
].type
;
686 glsl_type::field_index(const char *name
) const
688 if (this->base_type
!= GLSL_TYPE_STRUCT
689 && this->base_type
!= GLSL_TYPE_INTERFACE
)
692 for (unsigned i
= 0; i
< this->length
; i
++) {
693 if (strcmp(name
, this->fields
.structure
[i
].name
) == 0)
702 glsl_type::component_slots() const
704 switch (this->base_type
) {
707 case GLSL_TYPE_FLOAT
:
709 return this->components();
711 case GLSL_TYPE_STRUCT
:
712 case GLSL_TYPE_INTERFACE
: {
715 for (unsigned i
= 0; i
< this->length
; i
++)
716 size
+= this->fields
.structure
[i
].type
->component_slots();
721 case GLSL_TYPE_ARRAY
:
722 return this->length
* this->fields
.array
->component_slots();
724 case GLSL_TYPE_IMAGE
:
727 case GLSL_TYPE_SAMPLER
:
728 case GLSL_TYPE_ATOMIC_UINT
:
730 case GLSL_TYPE_ERROR
:
738 glsl_type::uniform_locations() const
742 switch (this->base_type
) {
745 case GLSL_TYPE_FLOAT
:
747 case GLSL_TYPE_SAMPLER
:
748 case GLSL_TYPE_IMAGE
:
751 case GLSL_TYPE_STRUCT
:
752 case GLSL_TYPE_INTERFACE
:
753 for (unsigned i
= 0; i
< this->length
; i
++)
754 size
+= this->fields
.structure
[i
].type
->uniform_locations();
756 case GLSL_TYPE_ARRAY
:
757 return this->length
* this->fields
.array
->uniform_locations();
764 glsl_type::can_implicitly_convert_to(const glsl_type
*desired
,
765 _mesa_glsl_parse_state
*state
) const
770 /* There is no conversion among matrix types. */
771 if (this->matrix_columns
> 1 || desired
->matrix_columns
> 1)
774 /* Vector size must match. */
775 if (this->vector_elements
!= desired
->vector_elements
)
778 /* int and uint can be converted to float. */
779 if (desired
->is_float() && this->is_integer())
782 /* With GLSL 4.0 / ARB_gpu_shader5, int can be converted to uint.
783 * Note that state may be NULL here, when resolving function calls in the
784 * linker. By this time, all the state-dependent checks have already
785 * happened though, so allow anything that's allowed in any shader version. */
786 if ((!state
|| state
->is_version(400, 0) || state
->ARB_gpu_shader5_enable
) &&
787 desired
->base_type
== GLSL_TYPE_UINT
&& this->base_type
== GLSL_TYPE_INT
)
794 glsl_type::std140_base_alignment(bool row_major
) const
796 /* (1) If the member is a scalar consuming <N> basic machine units, the
797 * base alignment is <N>.
799 * (2) If the member is a two- or four-component vector with components
800 * consuming <N> basic machine units, the base alignment is 2<N> or
801 * 4<N>, respectively.
803 * (3) If the member is a three-component vector with components consuming
804 * <N> basic machine units, the base alignment is 4<N>.
806 if (this->is_scalar() || this->is_vector()) {
807 switch (this->vector_elements
) {
818 /* (4) If the member is an array of scalars or vectors, the base alignment
819 * and array stride are set to match the base alignment of a single
820 * array element, according to rules (1), (2), and (3), and rounded up
821 * to the base alignment of a vec4. The array may have padding at the
822 * end; the base offset of the member following the array is rounded up
823 * to the next multiple of the base alignment.
825 * (6) If the member is an array of <S> column-major matrices with <C>
826 * columns and <R> rows, the matrix is stored identically to a row of
827 * <S>*<C> column vectors with <R> components each, according to rule
830 * (8) If the member is an array of <S> row-major matrices with <C> columns
831 * and <R> rows, the matrix is stored identically to a row of <S>*<R>
832 * row vectors with <C> components each, according to rule (4).
834 * (10) If the member is an array of <S> structures, the <S> elements of
835 * the array are laid out in order, according to rule (9).
837 if (this->is_array()) {
838 if (this->fields
.array
->is_scalar() ||
839 this->fields
.array
->is_vector() ||
840 this->fields
.array
->is_matrix()) {
841 return MAX2(this->fields
.array
->std140_base_alignment(row_major
), 16);
843 assert(this->fields
.array
->is_record());
844 return this->fields
.array
->std140_base_alignment(row_major
);
848 /* (5) If the member is a column-major matrix with <C> columns and
849 * <R> rows, the matrix is stored identically to an array of
850 * <C> column vectors with <R> components each, according to
853 * (7) If the member is a row-major matrix with <C> columns and <R>
854 * rows, the matrix is stored identically to an array of <R>
855 * row vectors with <C> components each, according to rule (4).
857 if (this->is_matrix()) {
858 const struct glsl_type
*vec_type
, *array_type
;
859 int c
= this->matrix_columns
;
860 int r
= this->vector_elements
;
863 vec_type
= get_instance(GLSL_TYPE_FLOAT
, c
, 1);
864 array_type
= glsl_type::get_array_instance(vec_type
, r
);
866 vec_type
= get_instance(GLSL_TYPE_FLOAT
, r
, 1);
867 array_type
= glsl_type::get_array_instance(vec_type
, c
);
870 return array_type
->std140_base_alignment(false);
873 /* (9) If the member is a structure, the base alignment of the
874 * structure is <N>, where <N> is the largest base alignment
875 * value of any of its members, and rounded up to the base
876 * alignment of a vec4. The individual members of this
877 * sub-structure are then assigned offsets by applying this set
878 * of rules recursively, where the base offset of the first
879 * member of the sub-structure is equal to the aligned offset
880 * of the structure. The structure may have padding at the end;
881 * the base offset of the member following the sub-structure is
882 * rounded up to the next multiple of the base alignment of the
885 if (this->is_record()) {
886 unsigned base_alignment
= 16;
887 for (unsigned i
= 0; i
< this->length
; i
++) {
888 bool field_row_major
= row_major
;
889 const enum glsl_matrix_layout matrix_layout
=
890 glsl_matrix_layout(this->fields
.structure
[i
].matrix_layout
);
891 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
892 field_row_major
= true;
893 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
894 field_row_major
= false;
897 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
898 base_alignment
= MAX2(base_alignment
,
899 field_type
->std140_base_alignment(field_row_major
));
901 return base_alignment
;
904 assert(!"not reached");
909 glsl_type::std140_size(bool row_major
) const
911 /* (1) If the member is a scalar consuming <N> basic machine units, the
912 * base alignment is <N>.
914 * (2) If the member is a two- or four-component vector with components
915 * consuming <N> basic machine units, the base alignment is 2<N> or
916 * 4<N>, respectively.
918 * (3) If the member is a three-component vector with components consuming
919 * <N> basic machine units, the base alignment is 4<N>.
921 if (this->is_scalar() || this->is_vector()) {
922 return this->vector_elements
* 4;
925 /* (5) If the member is a column-major matrix with <C> columns and
926 * <R> rows, the matrix is stored identically to an array of
927 * <C> column vectors with <R> components each, according to
930 * (6) If the member is an array of <S> column-major matrices with <C>
931 * columns and <R> rows, the matrix is stored identically to a row of
932 * <S>*<C> column vectors with <R> components each, according to rule
935 * (7) If the member is a row-major matrix with <C> columns and <R>
936 * rows, the matrix is stored identically to an array of <R>
937 * row vectors with <C> components each, according to rule (4).
939 * (8) If the member is an array of <S> row-major matrices with <C> columns
940 * and <R> rows, the matrix is stored identically to a row of <S>*<R>
941 * row vectors with <C> components each, according to rule (4).
943 if (this->without_array()->is_matrix()) {
944 const struct glsl_type
*element_type
;
945 const struct glsl_type
*vec_type
;
946 unsigned int array_len
;
948 if (this->is_array()) {
949 element_type
= this->fields
.array
;
950 array_len
= this->length
;
957 vec_type
= get_instance(GLSL_TYPE_FLOAT
,
958 element_type
->matrix_columns
, 1);
959 array_len
*= element_type
->vector_elements
;
961 vec_type
= get_instance(GLSL_TYPE_FLOAT
,
962 element_type
->vector_elements
, 1);
963 array_len
*= element_type
->matrix_columns
;
965 const glsl_type
*array_type
= glsl_type::get_array_instance(vec_type
,
968 return array_type
->std140_size(false);
971 /* (4) If the member is an array of scalars or vectors, the base alignment
972 * and array stride are set to match the base alignment of a single
973 * array element, according to rules (1), (2), and (3), and rounded up
974 * to the base alignment of a vec4. The array may have padding at the
975 * end; the base offset of the member following the array is rounded up
976 * to the next multiple of the base alignment.
978 * (10) If the member is an array of <S> structures, the <S> elements of
979 * the array are laid out in order, according to rule (9).
981 if (this->is_array()) {
982 if (this->fields
.array
->is_record()) {
983 return this->length
* this->fields
.array
->std140_size(row_major
);
985 unsigned element_base_align
=
986 this->fields
.array
->std140_base_alignment(row_major
);
987 return this->length
* MAX2(element_base_align
, 16);
991 /* (9) If the member is a structure, the base alignment of the
992 * structure is <N>, where <N> is the largest base alignment
993 * value of any of its members, and rounded up to the base
994 * alignment of a vec4. The individual members of this
995 * sub-structure are then assigned offsets by applying this set
996 * of rules recursively, where the base offset of the first
997 * member of the sub-structure is equal to the aligned offset
998 * of the structure. The structure may have padding at the end;
999 * the base offset of the member following the sub-structure is
1000 * rounded up to the next multiple of the base alignment of the
1003 if (this->is_record()) {
1005 unsigned max_align
= 0;
1007 for (unsigned i
= 0; i
< this->length
; i
++) {
1008 bool field_row_major
= row_major
;
1009 const enum glsl_matrix_layout matrix_layout
=
1010 glsl_matrix_layout(this->fields
.structure
[i
].matrix_layout
);
1011 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
1012 field_row_major
= true;
1013 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
1014 field_row_major
= false;
1017 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
1018 unsigned align
= field_type
->std140_base_alignment(field_row_major
);
1019 size
= glsl_align(size
, align
);
1020 size
+= field_type
->std140_size(field_row_major
);
1022 max_align
= MAX2(align
, max_align
);
1024 if (field_type
->is_record() && (i
+ 1 < this->length
))
1025 size
= glsl_align(size
, 16);
1027 size
= glsl_align(size
, MAX2(max_align
, 16));
1031 assert(!"not reached");
1037 glsl_type::count_attribute_slots() const
1039 /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
1041 * "A scalar input counts the same amount against this limit as a vec4,
1042 * so applications may want to consider packing groups of four
1043 * unrelated float inputs together into a vector to better utilize the
1044 * capabilities of the underlying hardware. A matrix input will use up
1045 * multiple locations. The number of locations used will equal the
1046 * number of columns in the matrix."
1048 * The spec does not explicitly say how arrays are counted. However, it
1049 * should be safe to assume the total number of slots consumed by an array
1050 * is the number of entries in the array multiplied by the number of slots
1051 * consumed by a single element of the array.
1053 * The spec says nothing about how structs are counted, because vertex
1054 * attributes are not allowed to be (or contain) structs. However, Mesa
1055 * allows varying structs, the number of varying slots taken up by a
1056 * varying struct is simply equal to the sum of the number of slots taken
1057 * up by each element.
1059 switch (this->base_type
) {
1060 case GLSL_TYPE_UINT
:
1062 case GLSL_TYPE_FLOAT
:
1063 case GLSL_TYPE_BOOL
:
1064 return this->matrix_columns
;
1066 case GLSL_TYPE_STRUCT
:
1067 case GLSL_TYPE_INTERFACE
: {
1070 for (unsigned i
= 0; i
< this->length
; i
++)
1071 size
+= this->fields
.structure
[i
].type
->count_attribute_slots();
1076 case GLSL_TYPE_ARRAY
:
1077 return this->length
* this->fields
.array
->count_attribute_slots();
1079 case GLSL_TYPE_SAMPLER
:
1080 case GLSL_TYPE_IMAGE
:
1081 case GLSL_TYPE_ATOMIC_UINT
:
1082 case GLSL_TYPE_VOID
:
1083 case GLSL_TYPE_ERROR
:
1087 assert(!"Unexpected type in count_attribute_slots()");
1093 glsl_type::coordinate_components() const
1097 switch (sampler_dimensionality
) {
1098 case GLSL_SAMPLER_DIM_1D
:
1099 case GLSL_SAMPLER_DIM_BUF
:
1102 case GLSL_SAMPLER_DIM_2D
:
1103 case GLSL_SAMPLER_DIM_RECT
:
1104 case GLSL_SAMPLER_DIM_MS
:
1105 case GLSL_SAMPLER_DIM_EXTERNAL
:
1108 case GLSL_SAMPLER_DIM_3D
:
1109 case GLSL_SAMPLER_DIM_CUBE
:
1113 assert(!"Should not get here.");
1118 /* Array textures need an additional component for the array index. */