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"
28 #include "program/hash_table.h"
31 mtx_t
glsl_type::mutex
= _MTX_INITIALIZER_NP
;
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 mtx_lock(&glsl_type::mutex
);
58 init_ralloc_type_ctx();
60 this->name
= ralloc_strdup(this->mem_ctx
, name
);
62 mtx_unlock(&glsl_type::mutex
);
64 /* Neither dimension is zero or both dimensions are zero.
66 assert((vector_elements
== 0) == (matrix_columns
== 0));
67 memset(& fields
, 0, sizeof(fields
));
70 glsl_type::glsl_type(GLenum gl_type
, glsl_base_type base_type
,
71 enum glsl_sampler_dim dim
, bool shadow
, bool array
,
72 unsigned type
, const char *name
) :
75 sampler_dimensionality(dim
), sampler_shadow(shadow
),
76 sampler_array(array
), sampler_type(type
), interface_packing(0),
79 mtx_lock(&glsl_type::mutex
);
81 init_ralloc_type_ctx();
83 this->name
= ralloc_strdup(this->mem_ctx
, name
);
85 mtx_unlock(&glsl_type::mutex
);
87 memset(& fields
, 0, sizeof(fields
));
89 if (base_type
== GLSL_TYPE_SAMPLER
) {
90 /* Samplers take no storage whatsoever. */
91 matrix_columns
= vector_elements
= 0;
93 matrix_columns
= vector_elements
= 1;
97 glsl_type::glsl_type(const glsl_struct_field
*fields
, unsigned num_fields
,
100 base_type(GLSL_TYPE_STRUCT
),
101 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
102 sampler_type(0), interface_packing(0),
103 vector_elements(0), matrix_columns(0),
108 mtx_lock(&glsl_type::mutex
);
110 init_ralloc_type_ctx();
111 assert(name
!= NULL
);
112 this->name
= ralloc_strdup(this->mem_ctx
, name
);
113 this->fields
.structure
= ralloc_array(this->mem_ctx
,
114 glsl_struct_field
, length
);
116 for (i
= 0; i
< length
; i
++) {
117 this->fields
.structure
[i
].type
= fields
[i
].type
;
118 this->fields
.structure
[i
].name
= ralloc_strdup(this->fields
.structure
,
120 this->fields
.structure
[i
].location
= fields
[i
].location
;
121 this->fields
.structure
[i
].interpolation
= fields
[i
].interpolation
;
122 this->fields
.structure
[i
].centroid
= fields
[i
].centroid
;
123 this->fields
.structure
[i
].sample
= fields
[i
].sample
;
124 this->fields
.structure
[i
].matrix_layout
= fields
[i
].matrix_layout
;
127 mtx_unlock(&glsl_type::mutex
);
130 glsl_type::glsl_type(const glsl_struct_field
*fields
, unsigned num_fields
,
131 enum glsl_interface_packing packing
, const char *name
) :
133 base_type(GLSL_TYPE_INTERFACE
),
134 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
135 sampler_type(0), interface_packing((unsigned) packing
),
136 vector_elements(0), matrix_columns(0),
141 mtx_lock(&glsl_type::mutex
);
143 init_ralloc_type_ctx();
144 assert(name
!= NULL
);
145 this->name
= ralloc_strdup(this->mem_ctx
, name
);
146 this->fields
.structure
= ralloc_array(this->mem_ctx
,
147 glsl_struct_field
, length
);
148 for (i
= 0; i
< length
; i
++) {
149 this->fields
.structure
[i
].type
= fields
[i
].type
;
150 this->fields
.structure
[i
].name
= ralloc_strdup(this->fields
.structure
,
152 this->fields
.structure
[i
].location
= fields
[i
].location
;
153 this->fields
.structure
[i
].interpolation
= fields
[i
].interpolation
;
154 this->fields
.structure
[i
].centroid
= fields
[i
].centroid
;
155 this->fields
.structure
[i
].sample
= fields
[i
].sample
;
156 this->fields
.structure
[i
].matrix_layout
= fields
[i
].matrix_layout
;
159 mtx_unlock(&glsl_type::mutex
);
164 glsl_type::contains_sampler() const
166 if (this->is_array()) {
167 return this->fields
.array
->contains_sampler();
168 } else if (this->is_record()) {
169 for (unsigned int i
= 0; i
< this->length
; i
++) {
170 if (this->fields
.structure
[i
].type
->contains_sampler())
175 return this->is_sampler();
181 glsl_type::contains_integer() const
183 if (this->is_array()) {
184 return this->fields
.array
->contains_integer();
185 } else if (this->is_record()) {
186 for (unsigned int i
= 0; i
< this->length
; i
++) {
187 if (this->fields
.structure
[i
].type
->contains_integer())
192 return this->is_integer();
197 glsl_type::contains_opaque() const {
199 case GLSL_TYPE_SAMPLER
:
200 case GLSL_TYPE_IMAGE
:
201 case GLSL_TYPE_ATOMIC_UINT
:
203 case GLSL_TYPE_ARRAY
:
204 return element_type()->contains_opaque();
205 case GLSL_TYPE_STRUCT
:
206 for (unsigned int i
= 0; i
< length
; i
++) {
207 if (fields
.structure
[i
].type
->contains_opaque())
217 glsl_type::sampler_index() const
219 const glsl_type
*const t
= (this->is_array()) ? this->fields
.array
: this;
221 assert(t
->is_sampler());
223 switch (t
->sampler_dimensionality
) {
224 case GLSL_SAMPLER_DIM_1D
:
225 return (t
->sampler_array
) ? TEXTURE_1D_ARRAY_INDEX
: TEXTURE_1D_INDEX
;
226 case GLSL_SAMPLER_DIM_2D
:
227 return (t
->sampler_array
) ? TEXTURE_2D_ARRAY_INDEX
: TEXTURE_2D_INDEX
;
228 case GLSL_SAMPLER_DIM_3D
:
229 return TEXTURE_3D_INDEX
;
230 case GLSL_SAMPLER_DIM_CUBE
:
231 return (t
->sampler_array
) ? TEXTURE_CUBE_ARRAY_INDEX
: TEXTURE_CUBE_INDEX
;
232 case GLSL_SAMPLER_DIM_RECT
:
233 return TEXTURE_RECT_INDEX
;
234 case GLSL_SAMPLER_DIM_BUF
:
235 return TEXTURE_BUFFER_INDEX
;
236 case GLSL_SAMPLER_DIM_EXTERNAL
:
237 return TEXTURE_EXTERNAL_INDEX
;
238 case GLSL_SAMPLER_DIM_MS
:
239 return (t
->sampler_array
) ? TEXTURE_2D_MULTISAMPLE_ARRAY_INDEX
: TEXTURE_2D_MULTISAMPLE_INDEX
;
241 assert(!"Should not get here.");
242 return TEXTURE_BUFFER_INDEX
;
247 glsl_type::contains_image() const
249 if (this->is_array()) {
250 return this->fields
.array
->contains_image();
251 } else if (this->is_record()) {
252 for (unsigned int i
= 0; i
< this->length
; i
++) {
253 if (this->fields
.structure
[i
].type
->contains_image())
258 return this->is_image();
262 const glsl_type
*glsl_type::get_base_type() const
269 case GLSL_TYPE_FLOAT
:
279 const glsl_type
*glsl_type::get_scalar_type() const
281 const glsl_type
*type
= this;
284 while (type
->base_type
== GLSL_TYPE_ARRAY
)
285 type
= type
->fields
.array
;
287 /* Handle vectors and matrices */
288 switch (type
->base_type
) {
293 case GLSL_TYPE_FLOAT
:
298 /* Handle everything else */
305 _mesa_glsl_release_types(void)
307 mtx_lock(&glsl_type::mutex
);
309 if (glsl_type::array_types
!= NULL
) {
310 hash_table_dtor(glsl_type::array_types
);
311 glsl_type::array_types
= NULL
;
314 if (glsl_type::record_types
!= NULL
) {
315 hash_table_dtor(glsl_type::record_types
);
316 glsl_type::record_types
= NULL
;
319 mtx_unlock(&glsl_type::mutex
);
323 glsl_type::glsl_type(const glsl_type
*array
, unsigned length
) :
324 base_type(GLSL_TYPE_ARRAY
),
325 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
326 sampler_type(0), interface_packing(0),
327 vector_elements(0), matrix_columns(0),
328 length(length
), name(NULL
)
330 this->fields
.array
= array
;
331 /* Inherit the gl type of the base. The GL type is used for
332 * uniform/statevar handling in Mesa and the arrayness of the type
333 * is represented by the size rather than the type.
335 this->gl_type
= array
->gl_type
;
337 /* Allow a maximum of 10 characters for the array size. This is enough
338 * for 32-bits of ~0. The extra 3 are for the '[', ']', and terminating
341 const unsigned name_length
= strlen(array
->name
) + 10 + 3;
343 mtx_lock(&glsl_type::mutex
);
344 char *const n
= (char *) ralloc_size(this->mem_ctx
, name_length
);
345 mtx_unlock(&glsl_type::mutex
);
348 snprintf(n
, name_length
, "%s[]", array
->name
);
350 /* insert outermost dimensions in the correct spot
351 * otherwise the dimension order will be backwards
353 const char *pos
= strchr(array
->name
, '[');
355 int idx
= pos
- array
->name
;
356 snprintf(n
, idx
+1, "%s", array
->name
);
357 snprintf(n
+ idx
, name_length
- idx
, "[%u]%s",
358 length
, array
->name
+ idx
);
360 snprintf(n
, name_length
, "%s[%u]", array
->name
, length
);
369 glsl_type::vec(unsigned components
)
371 if (components
== 0 || components
> 4)
374 static const glsl_type
*const ts
[] = {
375 float_type
, vec2_type
, vec3_type
, vec4_type
377 return ts
[components
- 1];
382 glsl_type::ivec(unsigned components
)
384 if (components
== 0 || components
> 4)
387 static const glsl_type
*const ts
[] = {
388 int_type
, ivec2_type
, ivec3_type
, ivec4_type
390 return ts
[components
- 1];
395 glsl_type::uvec(unsigned components
)
397 if (components
== 0 || components
> 4)
400 static const glsl_type
*const ts
[] = {
401 uint_type
, uvec2_type
, uvec3_type
, uvec4_type
403 return ts
[components
- 1];
408 glsl_type::bvec(unsigned components
)
410 if (components
== 0 || components
> 4)
413 static const glsl_type
*const ts
[] = {
414 bool_type
, bvec2_type
, bvec3_type
, bvec4_type
416 return ts
[components
- 1];
421 glsl_type::get_instance(unsigned base_type
, unsigned rows
, unsigned columns
)
423 if (base_type
== GLSL_TYPE_VOID
)
426 if ((rows
< 1) || (rows
> 4) || (columns
< 1) || (columns
> 4))
429 /* Treat GLSL vectors as Nx1 matrices.
437 case GLSL_TYPE_FLOAT
:
445 if ((base_type
!= GLSL_TYPE_FLOAT
) || (rows
== 1))
448 /* GLSL matrix types are named mat{COLUMNS}x{ROWS}. Only the following
449 * combinations are valid:
457 #define IDX(c,r) (((c-1)*3) + (r-1))
459 switch (IDX(columns
, rows
)) {
460 case IDX(2,2): return mat2_type
;
461 case IDX(2,3): return mat2x3_type
;
462 case IDX(2,4): return mat2x4_type
;
463 case IDX(3,2): return mat3x2_type
;
464 case IDX(3,3): return mat3_type
;
465 case IDX(3,4): return mat3x4_type
;
466 case IDX(4,2): return mat4x2_type
;
467 case IDX(4,3): return mat4x3_type
;
468 case IDX(4,4): return mat4_type
;
469 default: return error_type
;
473 assert(!"Should not get here.");
479 glsl_type::get_array_instance(const glsl_type
*base
, unsigned array_size
)
481 /* Generate a name using the base type pointer in the key. This is
482 * done because the name of the base type may not be unique across
483 * shaders. For example, two shaders may have different record types
487 snprintf(key
, sizeof(key
), "%p[%u]", (void *) base
, array_size
);
489 mtx_lock(&glsl_type::mutex
);
491 if (array_types
== NULL
) {
492 array_types
= hash_table_ctor(64, hash_table_string_hash
,
493 hash_table_string_compare
);
496 const glsl_type
*t
= (glsl_type
*) hash_table_find(array_types
, key
);
499 mtx_unlock(&glsl_type::mutex
);
500 t
= new glsl_type(base
, array_size
);
501 mtx_lock(&glsl_type::mutex
);
503 hash_table_insert(array_types
, (void *) t
, ralloc_strdup(mem_ctx
, key
));
506 assert(t
->base_type
== GLSL_TYPE_ARRAY
);
507 assert(t
->length
== array_size
);
508 assert(t
->fields
.array
== base
);
510 mtx_unlock(&glsl_type::mutex
);
517 glsl_type::record_compare(const glsl_type
*b
) const
519 if (this->length
!= b
->length
)
522 if (this->interface_packing
!= b
->interface_packing
)
525 /* From the GLSL 4.20 specification (Sec 4.2):
527 * "Structures must have the same name, sequence of type names, and
528 * type definitions, and field names to be considered the same type."
530 * GLSL ES behaves the same (Ver 1.00 Sec 4.2.4, Ver 3.00 Sec 4.2.5).
532 * Note that we cannot force type name check when comparing unnamed
533 * structure types, these have a unique name assigned during parsing.
535 if (!this->is_anonymous() && !b
->is_anonymous())
536 if (strcmp(this->name
, b
->name
) != 0)
539 for (unsigned i
= 0; i
< this->length
; i
++) {
540 if (this->fields
.structure
[i
].type
!= b
->fields
.structure
[i
].type
)
542 if (strcmp(this->fields
.structure
[i
].name
,
543 b
->fields
.structure
[i
].name
) != 0)
545 if (this->fields
.structure
[i
].matrix_layout
546 != b
->fields
.structure
[i
].matrix_layout
)
548 if (this->fields
.structure
[i
].location
549 != b
->fields
.structure
[i
].location
)
551 if (this->fields
.structure
[i
].interpolation
552 != b
->fields
.structure
[i
].interpolation
)
554 if (this->fields
.structure
[i
].centroid
555 != b
->fields
.structure
[i
].centroid
)
557 if (this->fields
.structure
[i
].sample
558 != b
->fields
.structure
[i
].sample
)
567 glsl_type::record_key_compare(const void *a
, const void *b
)
569 const glsl_type
*const key1
= (glsl_type
*) a
;
570 const glsl_type
*const key2
= (glsl_type
*) b
;
572 /* Return zero is the types match (there is zero difference) or non-zero
575 if (strcmp(key1
->name
, key2
->name
) != 0)
578 return !key1
->record_compare(key2
);
583 glsl_type::record_key_hash(const void *a
)
585 const glsl_type
*const key
= (glsl_type
*) a
;
589 size
= snprintf(hash_key
, sizeof(hash_key
), "%08x", key
->length
);
591 for (unsigned i
= 0; i
< key
->length
; i
++) {
592 if (size
>= sizeof(hash_key
))
595 size
+= snprintf(& hash_key
[size
], sizeof(hash_key
) - size
,
596 "%p", (void *) key
->fields
.structure
[i
].type
);
599 return hash_table_string_hash(& hash_key
);
604 glsl_type::get_record_instance(const glsl_struct_field
*fields
,
608 const glsl_type
key(fields
, num_fields
, name
);
610 mtx_lock(&glsl_type::mutex
);
612 if (record_types
== NULL
) {
613 record_types
= hash_table_ctor(64, record_key_hash
, record_key_compare
);
616 const glsl_type
*t
= (glsl_type
*) hash_table_find(record_types
, & key
);
618 mtx_unlock(&glsl_type::mutex
);
619 t
= new glsl_type(fields
, num_fields
, name
);
620 mtx_lock(&glsl_type::mutex
);
622 hash_table_insert(record_types
, (void *) t
, t
);
625 assert(t
->base_type
== GLSL_TYPE_STRUCT
);
626 assert(t
->length
== num_fields
);
627 assert(strcmp(t
->name
, name
) == 0);
629 mtx_unlock(&glsl_type::mutex
);
636 glsl_type::get_interface_instance(const glsl_struct_field
*fields
,
638 enum glsl_interface_packing packing
,
639 const char *block_name
)
641 const glsl_type
key(fields
, num_fields
, packing
, block_name
);
643 mtx_lock(&glsl_type::mutex
);
645 if (interface_types
== NULL
) {
646 interface_types
= hash_table_ctor(64, record_key_hash
, record_key_compare
);
649 const glsl_type
*t
= (glsl_type
*) hash_table_find(interface_types
, & key
);
651 mtx_unlock(&glsl_type::mutex
);
652 t
= new glsl_type(fields
, num_fields
, packing
, block_name
);
653 mtx_lock(&glsl_type::mutex
);
655 hash_table_insert(interface_types
, (void *) t
, t
);
658 assert(t
->base_type
== GLSL_TYPE_INTERFACE
);
659 assert(t
->length
== num_fields
);
660 assert(strcmp(t
->name
, block_name
) == 0);
662 mtx_unlock(&glsl_type::mutex
);
669 glsl_type::field_type(const char *name
) const
671 if (this->base_type
!= GLSL_TYPE_STRUCT
672 && this->base_type
!= GLSL_TYPE_INTERFACE
)
675 for (unsigned i
= 0; i
< this->length
; i
++) {
676 if (strcmp(name
, this->fields
.structure
[i
].name
) == 0)
677 return this->fields
.structure
[i
].type
;
685 glsl_type::field_index(const char *name
) const
687 if (this->base_type
!= GLSL_TYPE_STRUCT
688 && this->base_type
!= GLSL_TYPE_INTERFACE
)
691 for (unsigned i
= 0; i
< this->length
; i
++) {
692 if (strcmp(name
, this->fields
.structure
[i
].name
) == 0)
701 glsl_type::component_slots() const
703 switch (this->base_type
) {
706 case GLSL_TYPE_FLOAT
:
708 return this->components();
710 case GLSL_TYPE_STRUCT
:
711 case GLSL_TYPE_INTERFACE
: {
714 for (unsigned i
= 0; i
< this->length
; i
++)
715 size
+= this->fields
.structure
[i
].type
->component_slots();
720 case GLSL_TYPE_ARRAY
:
721 return this->length
* this->fields
.array
->component_slots();
723 case GLSL_TYPE_IMAGE
:
726 case GLSL_TYPE_SAMPLER
:
727 case GLSL_TYPE_ATOMIC_UINT
:
729 case GLSL_TYPE_ERROR
:
737 glsl_type::uniform_locations() const
741 switch (this->base_type
) {
744 case GLSL_TYPE_FLOAT
:
746 case GLSL_TYPE_SAMPLER
:
747 case GLSL_TYPE_IMAGE
:
750 case GLSL_TYPE_STRUCT
:
751 case GLSL_TYPE_INTERFACE
:
752 for (unsigned i
= 0; i
< this->length
; i
++)
753 size
+= this->fields
.structure
[i
].type
->uniform_locations();
755 case GLSL_TYPE_ARRAY
:
756 return this->length
* this->fields
.array
->uniform_locations();
763 glsl_type::can_implicitly_convert_to(const glsl_type
*desired
,
764 _mesa_glsl_parse_state
*state
) const
769 /* There is no conversion among matrix types. */
770 if (this->matrix_columns
> 1 || desired
->matrix_columns
> 1)
773 /* Vector size must match. */
774 if (this->vector_elements
!= desired
->vector_elements
)
777 /* int and uint can be converted to float. */
778 if (desired
->is_float() && this->is_integer())
781 /* With GLSL 4.0 / ARB_gpu_shader5, int can be converted to uint.
782 * Note that state may be NULL here, when resolving function calls in the
783 * linker. By this time, all the state-dependent checks have already
784 * happened though, so allow anything that's allowed in any shader version. */
785 if ((!state
|| state
->is_version(400, 0) || state
->ARB_gpu_shader5_enable
) &&
786 desired
->base_type
== GLSL_TYPE_UINT
&& this->base_type
== GLSL_TYPE_INT
)
793 glsl_type::std140_base_alignment(bool row_major
) const
795 /* (1) If the member is a scalar consuming <N> basic machine units, the
796 * base alignment is <N>.
798 * (2) If the member is a two- or four-component vector with components
799 * consuming <N> basic machine units, the base alignment is 2<N> or
800 * 4<N>, respectively.
802 * (3) If the member is a three-component vector with components consuming
803 * <N> basic machine units, the base alignment is 4<N>.
805 if (this->is_scalar() || this->is_vector()) {
806 switch (this->vector_elements
) {
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 * (6) If the member is an array of <S> column-major matrices with <C>
825 * columns and <R> rows, the matrix is stored identically to a row of
826 * <S>*<C> column vectors with <R> components each, according to rule
829 * (8) If the member is an array of <S> row-major matrices with <C> columns
830 * and <R> rows, the matrix is stored identically to a row of <S>*<R>
831 * row vectors with <C> components each, according to rule (4).
833 * (10) If the member is an array of <S> structures, the <S> elements of
834 * the array are laid out in order, according to rule (9).
836 if (this->is_array()) {
837 if (this->fields
.array
->is_scalar() ||
838 this->fields
.array
->is_vector() ||
839 this->fields
.array
->is_matrix()) {
840 return MAX2(this->fields
.array
->std140_base_alignment(row_major
), 16);
842 assert(this->fields
.array
->is_record());
843 return this->fields
.array
->std140_base_alignment(row_major
);
847 /* (5) If the member is a column-major matrix with <C> columns and
848 * <R> rows, the matrix is stored identically to an array of
849 * <C> column vectors with <R> components each, according to
852 * (7) If the member is a row-major matrix with <C> columns and <R>
853 * rows, the matrix is stored identically to an array of <R>
854 * row vectors with <C> components each, according to rule (4).
856 if (this->is_matrix()) {
857 const struct glsl_type
*vec_type
, *array_type
;
858 int c
= this->matrix_columns
;
859 int r
= this->vector_elements
;
862 vec_type
= get_instance(GLSL_TYPE_FLOAT
, c
, 1);
863 array_type
= glsl_type::get_array_instance(vec_type
, r
);
865 vec_type
= get_instance(GLSL_TYPE_FLOAT
, r
, 1);
866 array_type
= glsl_type::get_array_instance(vec_type
, c
);
869 return array_type
->std140_base_alignment(false);
872 /* (9) If the member is a structure, the base alignment of the
873 * structure is <N>, where <N> is the largest base alignment
874 * value of any of its members, and rounded up to the base
875 * alignment of a vec4. The individual members of this
876 * sub-structure are then assigned offsets by applying this set
877 * of rules recursively, where the base offset of the first
878 * member of the sub-structure is equal to the aligned offset
879 * of the structure. The structure may have padding at the end;
880 * the base offset of the member following the sub-structure is
881 * rounded up to the next multiple of the base alignment of the
884 if (this->is_record()) {
885 unsigned base_alignment
= 16;
886 for (unsigned i
= 0; i
< this->length
; i
++) {
887 bool field_row_major
= row_major
;
888 const enum glsl_matrix_layout matrix_layout
=
889 glsl_matrix_layout(this->fields
.structure
[i
].matrix_layout
);
890 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
891 field_row_major
= true;
892 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
893 field_row_major
= false;
896 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
897 base_alignment
= MAX2(base_alignment
,
898 field_type
->std140_base_alignment(field_row_major
));
900 return base_alignment
;
903 assert(!"not reached");
908 glsl_type::std140_size(bool row_major
) const
910 /* (1) If the member is a scalar consuming <N> basic machine units, the
911 * base alignment is <N>.
913 * (2) If the member is a two- or four-component vector with components
914 * consuming <N> basic machine units, the base alignment is 2<N> or
915 * 4<N>, respectively.
917 * (3) If the member is a three-component vector with components consuming
918 * <N> basic machine units, the base alignment is 4<N>.
920 if (this->is_scalar() || this->is_vector()) {
921 return this->vector_elements
* 4;
924 /* (5) If the member is a column-major matrix with <C> columns and
925 * <R> rows, the matrix is stored identically to an array of
926 * <C> column vectors with <R> components each, according to
929 * (6) If the member is an array of <S> column-major matrices with <C>
930 * columns and <R> rows, the matrix is stored identically to a row of
931 * <S>*<C> column vectors with <R> components each, according to rule
934 * (7) If the member is a row-major matrix with <C> columns and <R>
935 * rows, the matrix is stored identically to an array of <R>
936 * row vectors with <C> components each, according to rule (4).
938 * (8) If the member is an array of <S> row-major matrices with <C> columns
939 * and <R> rows, the matrix is stored identically to a row of <S>*<R>
940 * row vectors with <C> components each, according to rule (4).
942 if (this->without_array()->is_matrix()) {
943 const struct glsl_type
*element_type
;
944 const struct glsl_type
*vec_type
;
945 unsigned int array_len
;
947 if (this->is_array()) {
948 element_type
= this->fields
.array
;
949 array_len
= this->length
;
956 vec_type
= get_instance(GLSL_TYPE_FLOAT
,
957 element_type
->matrix_columns
, 1);
958 array_len
*= element_type
->vector_elements
;
960 vec_type
= get_instance(GLSL_TYPE_FLOAT
,
961 element_type
->vector_elements
, 1);
962 array_len
*= element_type
->matrix_columns
;
964 const glsl_type
*array_type
= glsl_type::get_array_instance(vec_type
,
967 return array_type
->std140_size(false);
970 /* (4) If the member is an array of scalars or vectors, the base alignment
971 * and array stride are set to match the base alignment of a single
972 * array element, according to rules (1), (2), and (3), and rounded up
973 * to the base alignment of a vec4. The array may have padding at the
974 * end; the base offset of the member following the array is rounded up
975 * to the next multiple of the base alignment.
977 * (10) If the member is an array of <S> structures, the <S> elements of
978 * the array are laid out in order, according to rule (9).
980 if (this->is_array()) {
981 if (this->fields
.array
->is_record()) {
982 return this->length
* this->fields
.array
->std140_size(row_major
);
984 unsigned element_base_align
=
985 this->fields
.array
->std140_base_alignment(row_major
);
986 return this->length
* MAX2(element_base_align
, 16);
990 /* (9) If the member is a structure, the base alignment of the
991 * structure is <N>, where <N> is the largest base alignment
992 * value of any of its members, and rounded up to the base
993 * alignment of a vec4. The individual members of this
994 * sub-structure are then assigned offsets by applying this set
995 * of rules recursively, where the base offset of the first
996 * member of the sub-structure is equal to the aligned offset
997 * of the structure. The structure may have padding at the end;
998 * the base offset of the member following the sub-structure is
999 * rounded up to the next multiple of the base alignment of the
1002 if (this->is_record()) {
1004 unsigned max_align
= 0;
1006 for (unsigned i
= 0; i
< this->length
; i
++) {
1007 bool field_row_major
= row_major
;
1008 const enum glsl_matrix_layout matrix_layout
=
1009 glsl_matrix_layout(this->fields
.structure
[i
].matrix_layout
);
1010 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
1011 field_row_major
= true;
1012 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
1013 field_row_major
= false;
1016 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
1017 unsigned align
= field_type
->std140_base_alignment(field_row_major
);
1018 size
= glsl_align(size
, align
);
1019 size
+= field_type
->std140_size(field_row_major
);
1021 max_align
= MAX2(align
, max_align
);
1023 if (field_type
->is_record() && (i
+ 1 < this->length
))
1024 size
= glsl_align(size
, 16);
1026 size
= glsl_align(size
, MAX2(max_align
, 16));
1030 assert(!"not reached");
1036 glsl_type::count_attribute_slots() const
1038 /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
1040 * "A scalar input counts the same amount against this limit as a vec4,
1041 * so applications may want to consider packing groups of four
1042 * unrelated float inputs together into a vector to better utilize the
1043 * capabilities of the underlying hardware. A matrix input will use up
1044 * multiple locations. The number of locations used will equal the
1045 * number of columns in the matrix."
1047 * The spec does not explicitly say how arrays are counted. However, it
1048 * should be safe to assume the total number of slots consumed by an array
1049 * is the number of entries in the array multiplied by the number of slots
1050 * consumed by a single element of the array.
1052 * The spec says nothing about how structs are counted, because vertex
1053 * attributes are not allowed to be (or contain) structs. However, Mesa
1054 * allows varying structs, the number of varying slots taken up by a
1055 * varying struct is simply equal to the sum of the number of slots taken
1056 * up by each element.
1058 switch (this->base_type
) {
1059 case GLSL_TYPE_UINT
:
1061 case GLSL_TYPE_FLOAT
:
1062 case GLSL_TYPE_BOOL
:
1063 return this->matrix_columns
;
1065 case GLSL_TYPE_STRUCT
:
1066 case GLSL_TYPE_INTERFACE
: {
1069 for (unsigned i
= 0; i
< this->length
; i
++)
1070 size
+= this->fields
.structure
[i
].type
->count_attribute_slots();
1075 case GLSL_TYPE_ARRAY
:
1076 return this->length
* this->fields
.array
->count_attribute_slots();
1078 case GLSL_TYPE_SAMPLER
:
1079 case GLSL_TYPE_IMAGE
:
1080 case GLSL_TYPE_ATOMIC_UINT
:
1081 case GLSL_TYPE_VOID
:
1082 case GLSL_TYPE_ERROR
:
1086 assert(!"Unexpected type in count_attribute_slots()");
1092 glsl_type::coordinate_components() const
1096 switch (sampler_dimensionality
) {
1097 case GLSL_SAMPLER_DIM_1D
:
1098 case GLSL_SAMPLER_DIM_BUF
:
1101 case GLSL_SAMPLER_DIM_2D
:
1102 case GLSL_SAMPLER_DIM_RECT
:
1103 case GLSL_SAMPLER_DIM_MS
:
1104 case GLSL_SAMPLER_DIM_EXTERNAL
:
1107 case GLSL_SAMPLER_DIM_3D
:
1108 case GLSL_SAMPLER_DIM_CUBE
:
1112 assert(!"Should not get here.");
1117 /* Array textures need an additional component for the array index. */