2 * Copyright © 2009 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
26 #include "main/core.h" /* for Elements */
27 #include "glsl_symbol_table.h"
28 #include "glsl_parser_extras.h"
29 #include "glsl_types.h"
30 #include "builtin_types.h"
32 #include "program/hash_table.h"
35 hash_table
*glsl_type::array_types
= NULL
;
36 hash_table
*glsl_type::record_types
= NULL
;
37 hash_table
*glsl_type::interface_types
= NULL
;
38 void *glsl_type::mem_ctx
= NULL
;
41 glsl_type::init_ralloc_type_ctx(void)
43 if (glsl_type::mem_ctx
== NULL
) {
44 glsl_type::mem_ctx
= ralloc_autofree_context();
45 assert(glsl_type::mem_ctx
!= NULL
);
49 glsl_type::glsl_type(GLenum gl_type
,
50 glsl_base_type base_type
, unsigned vector_elements
,
51 unsigned matrix_columns
, const char *name
) :
54 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
55 sampler_type(0), interface_packing(0),
56 vector_elements(vector_elements
), matrix_columns(matrix_columns
),
59 init_ralloc_type_ctx();
60 this->name
= ralloc_strdup(this->mem_ctx
, name
);
61 /* Neither dimension is zero or both dimensions are zero.
63 assert((vector_elements
== 0) == (matrix_columns
== 0));
64 memset(& fields
, 0, sizeof(fields
));
67 glsl_type::glsl_type(GLenum gl_type
,
68 enum glsl_sampler_dim dim
, bool shadow
, bool array
,
69 unsigned type
, const char *name
) :
71 base_type(GLSL_TYPE_SAMPLER
),
72 sampler_dimensionality(dim
), sampler_shadow(shadow
),
73 sampler_array(array
), sampler_type(type
), interface_packing(0),
74 vector_elements(0), matrix_columns(0),
77 init_ralloc_type_ctx();
78 this->name
= ralloc_strdup(this->mem_ctx
, name
);
79 memset(& fields
, 0, sizeof(fields
));
82 glsl_type::glsl_type(const glsl_struct_field
*fields
, unsigned num_fields
,
85 base_type(GLSL_TYPE_STRUCT
),
86 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
87 sampler_type(0), interface_packing(0),
88 vector_elements(0), matrix_columns(0),
93 init_ralloc_type_ctx();
94 this->name
= ralloc_strdup(this->mem_ctx
, name
);
95 this->fields
.structure
= ralloc_array(this->mem_ctx
,
96 glsl_struct_field
, length
);
97 for (i
= 0; i
< length
; i
++) {
98 this->fields
.structure
[i
].type
= fields
[i
].type
;
99 this->fields
.structure
[i
].name
= ralloc_strdup(this->fields
.structure
,
101 this->fields
.structure
[i
].row_major
= fields
[i
].row_major
;
105 glsl_type::glsl_type(const glsl_struct_field
*fields
, unsigned num_fields
,
106 enum glsl_interface_packing packing
, const char *name
) :
108 base_type(GLSL_TYPE_INTERFACE
),
109 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
110 sampler_type(0), interface_packing((unsigned) packing
),
111 vector_elements(0), matrix_columns(0),
116 init_ralloc_type_ctx();
117 this->name
= ralloc_strdup(this->mem_ctx
, name
);
118 this->fields
.structure
= ralloc_array(this->mem_ctx
,
119 glsl_struct_field
, length
);
120 for (i
= 0; i
< length
; i
++) {
121 this->fields
.structure
[i
].type
= fields
[i
].type
;
122 this->fields
.structure
[i
].name
= ralloc_strdup(this->fields
.structure
,
124 this->fields
.structure
[i
].row_major
= fields
[i
].row_major
;
129 add_types_to_symbol_table(glsl_symbol_table
*symtab
,
130 const struct glsl_type
*types
,
131 unsigned num_types
, bool warn
,
136 for (unsigned i
= 0; i
< num_types
; i
++) {
137 if (skip_1d
&& types
[i
].base_type
== GLSL_TYPE_SAMPLER
138 && types
[i
].sampler_dimensionality
== GLSL_SAMPLER_DIM_1D
)
141 symtab
->add_type(types
[i
].name
, & types
[i
]);
146 glsl_type::contains_sampler() const
148 if (this->is_array()) {
149 return this->fields
.array
->contains_sampler();
150 } else if (this->is_record()) {
151 for (unsigned int i
= 0; i
< this->length
; i
++) {
152 if (this->fields
.structure
[i
].type
->contains_sampler())
157 return this->is_sampler();
163 glsl_type::contains_integer() const
165 if (this->is_array()) {
166 return this->fields
.array
->contains_integer();
167 } else if (this->is_record()) {
168 for (unsigned int i
= 0; i
< this->length
; i
++) {
169 if (this->fields
.structure
[i
].type
->contains_integer())
174 return this->is_integer();
180 glsl_type::sampler_index() const
182 const glsl_type
*const t
= (this->is_array()) ? this->fields
.array
: this;
184 assert(t
->is_sampler());
186 switch (t
->sampler_dimensionality
) {
187 case GLSL_SAMPLER_DIM_1D
:
188 return (t
->sampler_array
) ? TEXTURE_1D_ARRAY_INDEX
: TEXTURE_1D_INDEX
;
189 case GLSL_SAMPLER_DIM_2D
:
190 return (t
->sampler_array
) ? TEXTURE_2D_ARRAY_INDEX
: TEXTURE_2D_INDEX
;
191 case GLSL_SAMPLER_DIM_3D
:
192 return TEXTURE_3D_INDEX
;
193 case GLSL_SAMPLER_DIM_CUBE
:
194 return (t
->sampler_array
) ? TEXTURE_CUBE_ARRAY_INDEX
: TEXTURE_CUBE_INDEX
;
195 case GLSL_SAMPLER_DIM_RECT
:
196 return TEXTURE_RECT_INDEX
;
197 case GLSL_SAMPLER_DIM_BUF
:
198 return TEXTURE_BUFFER_INDEX
;
199 case GLSL_SAMPLER_DIM_EXTERNAL
:
200 return TEXTURE_EXTERNAL_INDEX
;
202 assert(!"Should not get here.");
203 return TEXTURE_BUFFER_INDEX
;
208 glsl_type::generate_100ES_types(glsl_symbol_table
*symtab
)
210 bool skip_1d
= false;
211 add_types_to_symbol_table(symtab
, builtin_core_types
,
212 Elements(builtin_core_types
),
214 add_types_to_symbol_table(symtab
, builtin_structure_types
,
215 Elements(builtin_structure_types
),
217 add_types_to_symbol_table(symtab
, void_type
, 1, false, skip_1d
);
221 glsl_type::generate_300ES_types(glsl_symbol_table
*symtab
)
223 /* GLSL 3.00 ES types are the same as GLSL 1.30 types, except that 1D
224 * samplers are skipped, and samplerCubeShadow is added.
226 bool add_deprecated
= false;
229 generate_130_types(symtab
, add_deprecated
, skip_1d
);
231 add_types_to_symbol_table(symtab
, &_samplerCubeShadow_type
, 1, false,
236 glsl_type::generate_110_types(glsl_symbol_table
*symtab
, bool add_deprecated
,
239 generate_100ES_types(symtab
);
241 add_types_to_symbol_table(symtab
, builtin_110_types
,
242 Elements(builtin_110_types
),
244 add_types_to_symbol_table(symtab
, &_sampler3D_type
, 1, false, skip_1d
);
245 if (add_deprecated
) {
246 add_types_to_symbol_table(symtab
, builtin_110_deprecated_structure_types
,
247 Elements(builtin_110_deprecated_structure_types
),
254 glsl_type::generate_120_types(glsl_symbol_table
*symtab
, bool add_deprecated
,
257 generate_110_types(symtab
, add_deprecated
, skip_1d
);
259 add_types_to_symbol_table(symtab
, builtin_120_types
,
260 Elements(builtin_120_types
), false, skip_1d
);
265 glsl_type::generate_130_types(glsl_symbol_table
*symtab
, bool add_deprecated
,
268 generate_120_types(symtab
, add_deprecated
, skip_1d
);
270 add_types_to_symbol_table(symtab
, builtin_130_types
,
271 Elements(builtin_130_types
), false, skip_1d
);
272 generate_EXT_texture_array_types(symtab
, false);
277 glsl_type::generate_140_types(glsl_symbol_table
*symtab
)
279 bool skip_1d
= false;
281 generate_130_types(symtab
, false, skip_1d
);
283 add_types_to_symbol_table(symtab
, builtin_140_types
,
284 Elements(builtin_140_types
), false, skip_1d
);
286 add_types_to_symbol_table(symtab
, builtin_EXT_texture_buffer_object_types
,
287 Elements(builtin_EXT_texture_buffer_object_types
),
293 glsl_type::generate_ARB_texture_rectangle_types(glsl_symbol_table
*symtab
,
296 bool skip_1d
= false;
298 add_types_to_symbol_table(symtab
, builtin_ARB_texture_rectangle_types
,
299 Elements(builtin_ARB_texture_rectangle_types
),
305 glsl_type::generate_EXT_texture_array_types(glsl_symbol_table
*symtab
,
308 bool skip_1d
= false;
310 add_types_to_symbol_table(symtab
, builtin_EXT_texture_array_types
,
311 Elements(builtin_EXT_texture_array_types
),
317 glsl_type::generate_OES_texture_3D_types(glsl_symbol_table
*symtab
, bool warn
)
319 bool skip_1d
= false;
321 add_types_to_symbol_table(symtab
, &_sampler3D_type
, 1, warn
, skip_1d
);
326 glsl_type::generate_OES_EGL_image_external_types(glsl_symbol_table
*symtab
,
329 bool skip_1d
= false;
331 add_types_to_symbol_table(symtab
, builtin_OES_EGL_image_external_types
,
332 Elements(builtin_OES_EGL_image_external_types
),
337 glsl_type::generate_ARB_texture_cube_map_array_types(glsl_symbol_table
*symtab
,
340 bool skip_1d
= false;
342 add_types_to_symbol_table(symtab
, builtin_ARB_texture_cube_map_array_types
,
343 Elements(builtin_ARB_texture_cube_map_array_types
),
348 _mesa_glsl_initialize_types(struct _mesa_glsl_parse_state
*state
)
350 if (state
->es_shader
) {
351 switch (state
->language_version
) {
353 assert(state
->es_shader
);
354 glsl_type::generate_100ES_types(state
->symbols
);
357 glsl_type::generate_300ES_types(state
->symbols
);
360 assert(!"Unexpected language version");
364 bool skip_1d
= false;
365 switch (state
->language_version
) {
367 glsl_type::generate_110_types(state
->symbols
, true, skip_1d
);
370 glsl_type::generate_120_types(state
->symbols
, true, skip_1d
);
373 glsl_type::generate_130_types(state
->symbols
, true, skip_1d
);
376 glsl_type::generate_140_types(state
->symbols
);
379 assert(!"Unexpected language version");
384 if (state
->ARB_texture_rectangle_enable
||
385 state
->is_version(140, 0)) {
386 glsl_type::generate_ARB_texture_rectangle_types(state
->symbols
,
387 state
->ARB_texture_rectangle_warn
);
389 if (state
->OES_texture_3D_enable
390 && state
->is_version(0, 100)) {
391 glsl_type::generate_OES_texture_3D_types(state
->symbols
,
392 state
->OES_texture_3D_warn
);
395 if (state
->EXT_texture_array_enable
396 && !state
->is_version(130, 0)) {
397 // These are already included in 130; don't create twice.
398 glsl_type::generate_EXT_texture_array_types(state
->symbols
,
399 state
->EXT_texture_array_warn
);
402 /* We cannot check for language_version == 100 here because we need the
403 * types to support fixed-function program generation. But this is fine
404 * since the extension is never enabled for OpenGL contexts.
406 if (state
->OES_EGL_image_external_enable
) {
407 glsl_type::generate_OES_EGL_image_external_types(state
->symbols
,
408 state
->OES_EGL_image_external_warn
);
411 if (state
->ARB_texture_cube_map_array_enable
) {
412 glsl_type::generate_ARB_texture_cube_map_array_types(state
->symbols
,
413 state
->ARB_texture_cube_map_array_warn
);
418 const glsl_type
*glsl_type::get_base_type() const
425 case GLSL_TYPE_FLOAT
:
435 const glsl_type
*glsl_type::get_scalar_type() const
437 const glsl_type
*type
= this;
440 while (type
->base_type
== GLSL_TYPE_ARRAY
)
441 type
= type
->fields
.array
;
443 /* Handle vectors and matrices */
444 switch (type
->base_type
) {
449 case GLSL_TYPE_FLOAT
:
452 /* Handle everything else */
459 _mesa_glsl_release_types(void)
461 if (glsl_type::array_types
!= NULL
) {
462 hash_table_dtor(glsl_type::array_types
);
463 glsl_type::array_types
= NULL
;
466 if (glsl_type::record_types
!= NULL
) {
467 hash_table_dtor(glsl_type::record_types
);
468 glsl_type::record_types
= NULL
;
473 glsl_type::glsl_type(const glsl_type
*array
, unsigned length
) :
474 base_type(GLSL_TYPE_ARRAY
),
475 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
476 sampler_type(0), interface_packing(0),
477 vector_elements(0), matrix_columns(0),
478 name(NULL
), length(length
)
480 this->fields
.array
= array
;
481 /* Inherit the gl type of the base. The GL type is used for
482 * uniform/statevar handling in Mesa and the arrayness of the type
483 * is represented by the size rather than the type.
485 this->gl_type
= array
->gl_type
;
487 /* Allow a maximum of 10 characters for the array size. This is enough
488 * for 32-bits of ~0. The extra 3 are for the '[', ']', and terminating
491 const unsigned name_length
= strlen(array
->name
) + 10 + 3;
492 char *const n
= (char *) ralloc_size(this->mem_ctx
, name_length
);
495 snprintf(n
, name_length
, "%s[]", array
->name
);
497 snprintf(n
, name_length
, "%s[%u]", array
->name
, length
);
504 glsl_type::get_instance(unsigned base_type
, unsigned rows
, unsigned columns
)
506 if (base_type
== GLSL_TYPE_VOID
)
509 if ((rows
< 1) || (rows
> 4) || (columns
< 1) || (columns
> 4))
512 /* Treat GLSL vectors as Nx1 matrices.
517 return uint_type
+ (rows
- 1);
519 return int_type
+ (rows
- 1);
520 case GLSL_TYPE_FLOAT
:
521 return float_type
+ (rows
- 1);
523 return bool_type
+ (rows
- 1);
528 if ((base_type
!= GLSL_TYPE_FLOAT
) || (rows
== 1))
531 /* GLSL matrix types are named mat{COLUMNS}x{ROWS}. Only the following
532 * combinations are valid:
540 #define IDX(c,r) (((c-1)*3) + (r-1))
542 switch (IDX(columns
, rows
)) {
543 case IDX(2,2): return mat2_type
;
544 case IDX(2,3): return mat2x3_type
;
545 case IDX(2,4): return mat2x4_type
;
546 case IDX(3,2): return mat3x2_type
;
547 case IDX(3,3): return mat3_type
;
548 case IDX(3,4): return mat3x4_type
;
549 case IDX(4,2): return mat4x2_type
;
550 case IDX(4,3): return mat4x3_type
;
551 case IDX(4,4): return mat4_type
;
552 default: return error_type
;
556 assert(!"Should not get here.");
562 glsl_type::get_array_instance(const glsl_type
*base
, unsigned array_size
)
565 if (array_types
== NULL
) {
566 array_types
= hash_table_ctor(64, hash_table_string_hash
,
567 hash_table_string_compare
);
570 /* Generate a name using the base type pointer in the key. This is
571 * done because the name of the base type may not be unique across
572 * shaders. For example, two shaders may have different record types
576 snprintf(key
, sizeof(key
), "%p[%u]", (void *) base
, array_size
);
578 const glsl_type
*t
= (glsl_type
*) hash_table_find(array_types
, key
);
580 t
= new glsl_type(base
, array_size
);
582 hash_table_insert(array_types
, (void *) t
, ralloc_strdup(mem_ctx
, key
));
585 assert(t
->base_type
== GLSL_TYPE_ARRAY
);
586 assert(t
->length
== array_size
);
587 assert(t
->fields
.array
== base
);
594 glsl_type::record_key_compare(const void *a
, const void *b
)
596 const glsl_type
*const key1
= (glsl_type
*) a
;
597 const glsl_type
*const key2
= (glsl_type
*) b
;
599 /* Return zero is the types match (there is zero difference) or non-zero
602 if (strcmp(key1
->name
, key2
->name
) != 0)
605 if (key1
->length
!= key2
->length
)
608 if (key1
->interface_packing
!= key2
->interface_packing
)
611 for (unsigned i
= 0; i
< key1
->length
; i
++) {
612 if (key1
->fields
.structure
[i
].type
!= key2
->fields
.structure
[i
].type
)
614 if (strcmp(key1
->fields
.structure
[i
].name
,
615 key2
->fields
.structure
[i
].name
) != 0)
617 if (key1
->fields
.structure
[i
].row_major
618 != key2
->fields
.structure
[i
].row_major
)
627 glsl_type::record_key_hash(const void *a
)
629 const glsl_type
*const key
= (glsl_type
*) a
;
633 size
= snprintf(hash_key
, sizeof(hash_key
), "%08x", key
->length
);
635 for (unsigned i
= 0; i
< key
->length
; i
++) {
636 if (size
>= sizeof(hash_key
))
639 size
+= snprintf(& hash_key
[size
], sizeof(hash_key
) - size
,
640 "%p", (void *) key
->fields
.structure
[i
].type
);
643 return hash_table_string_hash(& hash_key
);
648 glsl_type::get_record_instance(const glsl_struct_field
*fields
,
652 const glsl_type
key(fields
, num_fields
, name
);
654 if (record_types
== NULL
) {
655 record_types
= hash_table_ctor(64, record_key_hash
, record_key_compare
);
658 const glsl_type
*t
= (glsl_type
*) hash_table_find(record_types
, & key
);
660 t
= new glsl_type(fields
, num_fields
, name
);
662 hash_table_insert(record_types
, (void *) t
, t
);
665 assert(t
->base_type
== GLSL_TYPE_STRUCT
);
666 assert(t
->length
== num_fields
);
667 assert(strcmp(t
->name
, name
) == 0);
674 glsl_type::get_interface_instance(const glsl_struct_field
*fields
,
676 enum glsl_interface_packing packing
,
679 const glsl_type
key(fields
, num_fields
, packing
, name
);
681 if (interface_types
== NULL
) {
682 interface_types
= hash_table_ctor(64, record_key_hash
, record_key_compare
);
685 const glsl_type
*t
= (glsl_type
*) hash_table_find(interface_types
, & key
);
687 t
= new glsl_type(fields
, num_fields
, packing
, name
);
689 hash_table_insert(interface_types
, (void *) t
, t
);
692 assert(t
->base_type
== GLSL_TYPE_INTERFACE
);
693 assert(t
->length
== num_fields
);
694 assert(strcmp(t
->name
, name
) == 0);
701 glsl_type::field_type(const char *name
) const
703 if (this->base_type
!= GLSL_TYPE_STRUCT
704 && this->base_type
!= GLSL_TYPE_INTERFACE
)
707 for (unsigned i
= 0; i
< this->length
; i
++) {
708 if (strcmp(name
, this->fields
.structure
[i
].name
) == 0)
709 return this->fields
.structure
[i
].type
;
717 glsl_type::field_index(const char *name
) const
719 if (this->base_type
!= GLSL_TYPE_STRUCT
720 && this->base_type
!= GLSL_TYPE_INTERFACE
)
723 for (unsigned i
= 0; i
< this->length
; i
++) {
724 if (strcmp(name
, this->fields
.structure
[i
].name
) == 0)
733 glsl_type::component_slots() const
735 switch (this->base_type
) {
738 case GLSL_TYPE_FLOAT
:
740 return this->components();
742 case GLSL_TYPE_STRUCT
:
743 case GLSL_TYPE_INTERFACE
: {
746 for (unsigned i
= 0; i
< this->length
; i
++)
747 size
+= this->fields
.structure
[i
].type
->component_slots();
752 case GLSL_TYPE_ARRAY
:
753 return this->length
* this->fields
.array
->component_slots();
755 case GLSL_TYPE_SAMPLER
:
757 case GLSL_TYPE_ERROR
:
765 glsl_type::can_implicitly_convert_to(const glsl_type
*desired
) const
770 /* There is no conversion among matrix types. */
771 if (this->matrix_columns
> 1 || desired
->matrix_columns
> 1)
774 /* int and uint can be converted to float. */
775 return desired
->is_float()
776 && this->is_integer()
777 && this->vector_elements
== desired
->vector_elements
;
781 glsl_type::std140_base_alignment(bool row_major
) const
783 /* (1) If the member is a scalar consuming <N> basic machine units, the
784 * base alignment is <N>.
786 * (2) If the member is a two- or four-component vector with components
787 * consuming <N> basic machine units, the base alignment is 2<N> or
788 * 4<N>, respectively.
790 * (3) If the member is a three-component vector with components consuming
791 * <N> basic machine units, the base alignment is 4<N>.
793 if (this->is_scalar() || this->is_vector()) {
794 switch (this->vector_elements
) {
805 /* (4) If the member is an array of scalars or vectors, the base alignment
806 * and array stride are set to match the base alignment of a single
807 * array element, according to rules (1), (2), and (3), and rounded up
808 * to the base alignment of a vec4. The array may have padding at the
809 * end; the base offset of the member following the array is rounded up
810 * to the next multiple of the base alignment.
812 * (6) If the member is an array of <S> column-major matrices with <C>
813 * columns and <R> rows, the matrix is stored identically to a row of
814 * <S>*<C> column vectors with <R> components each, according to rule
817 * (8) If the member is an array of <S> row-major matrices with <C> columns
818 * and <R> rows, the matrix is stored identically to a row of <S>*<R>
819 * row vectors with <C> components each, according to rule (4).
821 * (10) If the member is an array of <S> structures, the <S> elements of
822 * the array are laid out in order, according to rule (9).
824 if (this->is_array()) {
825 if (this->fields
.array
->is_scalar() ||
826 this->fields
.array
->is_vector() ||
827 this->fields
.array
->is_matrix()) {
828 return MAX2(this->fields
.array
->std140_base_alignment(row_major
), 16);
830 assert(this->fields
.array
->is_record());
831 return this->fields
.array
->std140_base_alignment(row_major
);
835 /* (5) If the member is a column-major matrix with <C> columns and
836 * <R> rows, the matrix is stored identically to an array of
837 * <C> column vectors with <R> components each, according to
840 * (7) If the member is a row-major matrix with <C> columns and <R>
841 * rows, the matrix is stored identically to an array of <R>
842 * row vectors with <C> components each, according to rule (4).
844 if (this->is_matrix()) {
845 const struct glsl_type
*vec_type
, *array_type
;
846 int c
= this->matrix_columns
;
847 int r
= this->vector_elements
;
850 vec_type
= get_instance(GLSL_TYPE_FLOAT
, c
, 1);
851 array_type
= glsl_type::get_array_instance(vec_type
, r
);
853 vec_type
= get_instance(GLSL_TYPE_FLOAT
, r
, 1);
854 array_type
= glsl_type::get_array_instance(vec_type
, c
);
857 return array_type
->std140_base_alignment(false);
860 /* (9) If the member is a structure, the base alignment of the
861 * structure is <N>, where <N> is the largest base alignment
862 * value of any of its members, and rounded up to the base
863 * alignment of a vec4. The individual members of this
864 * sub-structure are then assigned offsets by applying this set
865 * of rules recursively, where the base offset of the first
866 * member of the sub-structure is equal to the aligned offset
867 * of the structure. The structure may have padding at the end;
868 * the base offset of the member following the sub-structure is
869 * rounded up to the next multiple of the base alignment of the
872 if (this->is_record()) {
873 unsigned base_alignment
= 16;
874 for (unsigned i
= 0; i
< this->length
; i
++) {
875 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
876 base_alignment
= MAX2(base_alignment
,
877 field_type
->std140_base_alignment(row_major
));
879 return base_alignment
;
882 assert(!"not reached");
887 glsl_type::std140_size(bool row_major
) const
889 /* (1) If the member is a scalar consuming <N> basic machine units, the
890 * base alignment is <N>.
892 * (2) If the member is a two- or four-component vector with components
893 * consuming <N> basic machine units, the base alignment is 2<N> or
894 * 4<N>, respectively.
896 * (3) If the member is a three-component vector with components consuming
897 * <N> basic machine units, the base alignment is 4<N>.
899 if (this->is_scalar() || this->is_vector()) {
900 return this->vector_elements
* 4;
903 /* (5) If the member is a column-major matrix with <C> columns and
904 * <R> rows, the matrix is stored identically to an array of
905 * <C> column vectors with <R> components each, according to
908 * (6) If the member is an array of <S> column-major matrices with <C>
909 * columns and <R> rows, the matrix is stored identically to a row of
910 * <S>*<C> column vectors with <R> components each, according to rule
913 * (7) If the member is a row-major matrix with <C> columns and <R>
914 * rows, the matrix is stored identically to an array of <R>
915 * row vectors with <C> components each, according to rule (4).
917 * (8) If the member is an array of <S> row-major matrices with <C> columns
918 * and <R> rows, the matrix is stored identically to a row of <S>*<R>
919 * row vectors with <C> components each, according to rule (4).
921 if (this->is_matrix() || (this->is_array() &&
922 this->fields
.array
->is_matrix())) {
923 const struct glsl_type
*element_type
;
924 const struct glsl_type
*vec_type
;
925 unsigned int array_len
;
927 if (this->is_array()) {
928 element_type
= this->fields
.array
;
929 array_len
= this->length
;
936 vec_type
= get_instance(GLSL_TYPE_FLOAT
,
937 element_type
->matrix_columns
, 1);
938 array_len
*= element_type
->vector_elements
;
940 vec_type
= get_instance(GLSL_TYPE_FLOAT
,
941 element_type
->vector_elements
, 1);
942 array_len
*= element_type
->matrix_columns
;
944 const glsl_type
*array_type
= glsl_type::get_array_instance(vec_type
,
947 return array_type
->std140_size(false);
950 /* (4) If the member is an array of scalars or vectors, the base alignment
951 * and array stride are set to match the base alignment of a single
952 * array element, according to rules (1), (2), and (3), and rounded up
953 * to the base alignment of a vec4. The array may have padding at the
954 * end; the base offset of the member following the array is rounded up
955 * to the next multiple of the base alignment.
957 * (10) If the member is an array of <S> structures, the <S> elements of
958 * the array are laid out in order, according to rule (9).
960 if (this->is_array()) {
961 if (this->fields
.array
->is_record()) {
962 return this->length
* this->fields
.array
->std140_size(row_major
);
964 unsigned element_base_align
=
965 this->fields
.array
->std140_base_alignment(row_major
);
966 return this->length
* MAX2(element_base_align
, 16);
970 /* (9) If the member is a structure, the base alignment of the
971 * structure is <N>, where <N> is the largest base alignment
972 * value of any of its members, and rounded up to the base
973 * alignment of a vec4. The individual members of this
974 * sub-structure are then assigned offsets by applying this set
975 * of rules recursively, where the base offset of the first
976 * member of the sub-structure is equal to the aligned offset
977 * of the structure. The structure may have padding at the end;
978 * the base offset of the member following the sub-structure is
979 * rounded up to the next multiple of the base alignment of the
982 if (this->is_record()) {
984 for (unsigned i
= 0; i
< this->length
; i
++) {
985 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
986 unsigned align
= field_type
->std140_base_alignment(row_major
);
987 size
= glsl_align(size
, align
);
988 size
+= field_type
->std140_size(row_major
);
990 size
= glsl_align(size
,
991 this->fields
.structure
[0].type
->std140_base_alignment(row_major
));
995 assert(!"not reached");