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();
61 this->name
= ralloc_strdup(this->mem_ctx
, name
);
62 /* Neither dimension is zero or both dimensions are zero.
64 assert((vector_elements
== 0) == (matrix_columns
== 0));
65 memset(& fields
, 0, sizeof(fields
));
68 glsl_type::glsl_type(GLenum gl_type
,
69 enum glsl_sampler_dim dim
, bool shadow
, bool array
,
70 unsigned type
, const char *name
) :
72 base_type(GLSL_TYPE_SAMPLER
),
73 sampler_dimensionality(dim
), sampler_shadow(shadow
),
74 sampler_array(array
), sampler_type(type
), interface_packing(0),
75 vector_elements(0), matrix_columns(0),
78 init_ralloc_type_ctx();
80 this->name
= ralloc_strdup(this->mem_ctx
, name
);
81 memset(& fields
, 0, sizeof(fields
));
84 glsl_type::glsl_type(const glsl_struct_field
*fields
, unsigned num_fields
,
87 base_type(GLSL_TYPE_STRUCT
),
88 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
89 sampler_type(0), interface_packing(0),
90 vector_elements(0), matrix_columns(0),
95 init_ralloc_type_ctx();
97 this->name
= ralloc_strdup(this->mem_ctx
, name
);
98 this->fields
.structure
= ralloc_array(this->mem_ctx
,
99 glsl_struct_field
, length
);
100 for (i
= 0; i
< length
; i
++) {
101 this->fields
.structure
[i
].type
= fields
[i
].type
;
102 this->fields
.structure
[i
].name
= ralloc_strdup(this->fields
.structure
,
104 this->fields
.structure
[i
].row_major
= fields
[i
].row_major
;
108 glsl_type::glsl_type(const glsl_struct_field
*fields
, unsigned num_fields
,
109 enum glsl_interface_packing packing
, const char *name
) :
111 base_type(GLSL_TYPE_INTERFACE
),
112 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
113 sampler_type(0), interface_packing((unsigned) packing
),
114 vector_elements(0), matrix_columns(0),
119 init_ralloc_type_ctx();
120 assert(name
!= NULL
);
121 this->name
= ralloc_strdup(this->mem_ctx
, name
);
122 this->fields
.structure
= ralloc_array(this->mem_ctx
,
123 glsl_struct_field
, length
);
124 for (i
= 0; i
< length
; i
++) {
125 this->fields
.structure
[i
].type
= fields
[i
].type
;
126 this->fields
.structure
[i
].name
= ralloc_strdup(this->fields
.structure
,
128 this->fields
.structure
[i
].row_major
= fields
[i
].row_major
;
133 add_types_to_symbol_table(glsl_symbol_table
*symtab
,
134 const struct glsl_type
*types
,
135 unsigned num_types
, bool warn
,
140 for (unsigned i
= 0; i
< num_types
; i
++) {
141 if (skip_1d
&& types
[i
].base_type
== GLSL_TYPE_SAMPLER
142 && types
[i
].sampler_dimensionality
== GLSL_SAMPLER_DIM_1D
)
145 symtab
->add_type(types
[i
].name
, & types
[i
]);
150 glsl_type::contains_sampler() const
152 if (this->is_array()) {
153 return this->fields
.array
->contains_sampler();
154 } else if (this->is_record()) {
155 for (unsigned int i
= 0; i
< this->length
; i
++) {
156 if (this->fields
.structure
[i
].type
->contains_sampler())
161 return this->is_sampler();
167 glsl_type::contains_integer() const
169 if (this->is_array()) {
170 return this->fields
.array
->contains_integer();
171 } else if (this->is_record()) {
172 for (unsigned int i
= 0; i
< this->length
; i
++) {
173 if (this->fields
.structure
[i
].type
->contains_integer())
178 return this->is_integer();
184 glsl_type::sampler_index() const
186 const glsl_type
*const t
= (this->is_array()) ? this->fields
.array
: this;
188 assert(t
->is_sampler());
190 switch (t
->sampler_dimensionality
) {
191 case GLSL_SAMPLER_DIM_1D
:
192 return (t
->sampler_array
) ? TEXTURE_1D_ARRAY_INDEX
: TEXTURE_1D_INDEX
;
193 case GLSL_SAMPLER_DIM_2D
:
194 return (t
->sampler_array
) ? TEXTURE_2D_ARRAY_INDEX
: TEXTURE_2D_INDEX
;
195 case GLSL_SAMPLER_DIM_3D
:
196 return TEXTURE_3D_INDEX
;
197 case GLSL_SAMPLER_DIM_CUBE
:
198 return (t
->sampler_array
) ? TEXTURE_CUBE_ARRAY_INDEX
: TEXTURE_CUBE_INDEX
;
199 case GLSL_SAMPLER_DIM_RECT
:
200 return TEXTURE_RECT_INDEX
;
201 case GLSL_SAMPLER_DIM_BUF
:
202 return TEXTURE_BUFFER_INDEX
;
203 case GLSL_SAMPLER_DIM_EXTERNAL
:
204 return TEXTURE_EXTERNAL_INDEX
;
205 case GLSL_SAMPLER_DIM_MS
:
206 return (t
->sampler_array
) ? TEXTURE_2D_MULTISAMPLE_ARRAY_INDEX
: TEXTURE_2D_MULTISAMPLE_INDEX
;
208 assert(!"Should not get here.");
209 return TEXTURE_BUFFER_INDEX
;
214 glsl_type::generate_100ES_types(glsl_symbol_table
*symtab
)
216 bool skip_1d
= false;
217 add_types_to_symbol_table(symtab
, builtin_core_types
,
218 Elements(builtin_core_types
),
220 add_types_to_symbol_table(symtab
, builtin_structure_types
,
221 Elements(builtin_structure_types
),
223 add_types_to_symbol_table(symtab
, void_type
, 1, false, skip_1d
);
227 glsl_type::generate_300ES_types(glsl_symbol_table
*symtab
)
229 /* GLSL 3.00 ES types are the same as GLSL 1.30 types, except that 1D
230 * samplers are skipped, and samplerCubeShadow is added.
232 bool add_deprecated
= false;
235 generate_130_types(symtab
, add_deprecated
, skip_1d
);
237 add_types_to_symbol_table(symtab
, &_samplerCubeShadow_type
, 1, false,
242 glsl_type::generate_110_types(glsl_symbol_table
*symtab
, bool add_deprecated
,
245 generate_100ES_types(symtab
);
247 add_types_to_symbol_table(symtab
, builtin_110_types
,
248 Elements(builtin_110_types
),
250 add_types_to_symbol_table(symtab
, &_sampler3D_type
, 1, false, skip_1d
);
251 if (add_deprecated
) {
252 add_types_to_symbol_table(symtab
, builtin_110_deprecated_structure_types
,
253 Elements(builtin_110_deprecated_structure_types
),
260 glsl_type::generate_120_types(glsl_symbol_table
*symtab
, bool add_deprecated
,
263 generate_110_types(symtab
, add_deprecated
, skip_1d
);
265 add_types_to_symbol_table(symtab
, builtin_120_types
,
266 Elements(builtin_120_types
), false, skip_1d
);
271 glsl_type::generate_130_types(glsl_symbol_table
*symtab
, bool add_deprecated
,
274 generate_120_types(symtab
, add_deprecated
, skip_1d
);
276 add_types_to_symbol_table(symtab
, builtin_130_types
,
277 Elements(builtin_130_types
), false, skip_1d
);
278 generate_EXT_texture_array_types(symtab
, false);
283 glsl_type::generate_140_types(glsl_symbol_table
*symtab
)
285 bool skip_1d
= false;
287 generate_130_types(symtab
, false, skip_1d
);
289 add_types_to_symbol_table(symtab
, builtin_140_types
,
290 Elements(builtin_140_types
), false, skip_1d
);
292 add_types_to_symbol_table(symtab
, builtin_EXT_texture_buffer_object_types
,
293 Elements(builtin_EXT_texture_buffer_object_types
),
299 glsl_type::generate_150_types(glsl_symbol_table
*symtab
)
301 generate_140_types(symtab
);
302 generate_ARB_texture_multisample_types(symtab
, false);
307 glsl_type::generate_ARB_texture_rectangle_types(glsl_symbol_table
*symtab
,
310 bool skip_1d
= false;
312 add_types_to_symbol_table(symtab
, builtin_ARB_texture_rectangle_types
,
313 Elements(builtin_ARB_texture_rectangle_types
),
319 glsl_type::generate_EXT_texture_array_types(glsl_symbol_table
*symtab
,
322 bool skip_1d
= false;
324 add_types_to_symbol_table(symtab
, builtin_EXT_texture_array_types
,
325 Elements(builtin_EXT_texture_array_types
),
331 glsl_type::generate_OES_texture_3D_types(glsl_symbol_table
*symtab
, bool warn
)
333 bool skip_1d
= false;
335 add_types_to_symbol_table(symtab
, &_sampler3D_type
, 1, warn
, skip_1d
);
340 glsl_type::generate_OES_EGL_image_external_types(glsl_symbol_table
*symtab
,
343 bool skip_1d
= false;
345 add_types_to_symbol_table(symtab
, builtin_OES_EGL_image_external_types
,
346 Elements(builtin_OES_EGL_image_external_types
),
351 glsl_type::generate_ARB_texture_cube_map_array_types(glsl_symbol_table
*symtab
,
354 bool skip_1d
= false;
356 add_types_to_symbol_table(symtab
, builtin_ARB_texture_cube_map_array_types
,
357 Elements(builtin_ARB_texture_cube_map_array_types
),
362 glsl_type::generate_ARB_texture_multisample_types(glsl_symbol_table
*symtab
,
365 bool skip_1d
= false;
366 add_types_to_symbol_table(symtab
, builtin_ARB_texture_multisample_types
,
367 Elements(builtin_ARB_texture_multisample_types
),
372 _mesa_glsl_initialize_types(struct _mesa_glsl_parse_state
*state
)
374 if (state
->es_shader
) {
375 switch (state
->language_version
) {
377 assert(state
->es_shader
);
378 glsl_type::generate_100ES_types(state
->symbols
);
381 glsl_type::generate_300ES_types(state
->symbols
);
384 assert(!"Unexpected language version");
388 bool skip_1d
= false;
389 switch (state
->language_version
) {
391 glsl_type::generate_110_types(state
->symbols
, true, skip_1d
);
394 glsl_type::generate_120_types(state
->symbols
, true, skip_1d
);
397 glsl_type::generate_130_types(state
->symbols
, true, skip_1d
);
400 glsl_type::generate_140_types(state
->symbols
);
403 glsl_type::generate_150_types(state
->symbols
);
406 assert(!"Unexpected language version");
411 if (state
->ARB_texture_rectangle_enable
||
412 state
->is_version(140, 0)) {
413 glsl_type::generate_ARB_texture_rectangle_types(state
->symbols
,
414 state
->ARB_texture_rectangle_warn
);
416 if (state
->OES_texture_3D_enable
417 && state
->is_version(0, 100)) {
418 glsl_type::generate_OES_texture_3D_types(state
->symbols
,
419 state
->OES_texture_3D_warn
);
422 if (state
->EXT_texture_array_enable
423 && !state
->is_version(130, 0)) {
424 // These are already included in 130; don't create twice.
425 glsl_type::generate_EXT_texture_array_types(state
->symbols
,
426 state
->EXT_texture_array_warn
);
429 /* We cannot check for language_version == 100 here because we need the
430 * types to support fixed-function program generation. But this is fine
431 * since the extension is never enabled for OpenGL contexts.
433 if (state
->OES_EGL_image_external_enable
) {
434 glsl_type::generate_OES_EGL_image_external_types(state
->symbols
,
435 state
->OES_EGL_image_external_warn
);
438 if (state
->ARB_texture_cube_map_array_enable
) {
439 glsl_type::generate_ARB_texture_cube_map_array_types(state
->symbols
,
440 state
->ARB_texture_cube_map_array_warn
);
443 if (state
->ARB_texture_multisample_enable
) {
444 glsl_type::generate_ARB_texture_multisample_types(state
->symbols
,
445 state
->ARB_texture_multisample_warn
);
450 const glsl_type
*glsl_type::get_base_type() const
457 case GLSL_TYPE_FLOAT
:
467 const glsl_type
*glsl_type::get_scalar_type() const
469 const glsl_type
*type
= this;
472 while (type
->base_type
== GLSL_TYPE_ARRAY
)
473 type
= type
->fields
.array
;
475 /* Handle vectors and matrices */
476 switch (type
->base_type
) {
481 case GLSL_TYPE_FLOAT
:
486 /* Handle everything else */
493 _mesa_glsl_release_types(void)
495 if (glsl_type::array_types
!= NULL
) {
496 hash_table_dtor(glsl_type::array_types
);
497 glsl_type::array_types
= NULL
;
500 if (glsl_type::record_types
!= NULL
) {
501 hash_table_dtor(glsl_type::record_types
);
502 glsl_type::record_types
= NULL
;
507 glsl_type::glsl_type(const glsl_type
*array
, unsigned length
) :
508 base_type(GLSL_TYPE_ARRAY
),
509 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
510 sampler_type(0), interface_packing(0),
511 vector_elements(0), matrix_columns(0),
512 name(NULL
), length(length
)
514 this->fields
.array
= array
;
515 /* Inherit the gl type of the base. The GL type is used for
516 * uniform/statevar handling in Mesa and the arrayness of the type
517 * is represented by the size rather than the type.
519 this->gl_type
= array
->gl_type
;
521 /* Allow a maximum of 10 characters for the array size. This is enough
522 * for 32-bits of ~0. The extra 3 are for the '[', ']', and terminating
525 const unsigned name_length
= strlen(array
->name
) + 10 + 3;
526 char *const n
= (char *) ralloc_size(this->mem_ctx
, name_length
);
529 snprintf(n
, name_length
, "%s[]", array
->name
);
531 snprintf(n
, name_length
, "%s[%u]", array
->name
, length
);
538 glsl_type::get_instance(unsigned base_type
, unsigned rows
, unsigned columns
)
540 if (base_type
== GLSL_TYPE_VOID
)
543 if ((rows
< 1) || (rows
> 4) || (columns
< 1) || (columns
> 4))
546 /* Treat GLSL vectors as Nx1 matrices.
551 return uint_type
+ (rows
- 1);
553 return int_type
+ (rows
- 1);
554 case GLSL_TYPE_FLOAT
:
555 return float_type
+ (rows
- 1);
557 return bool_type
+ (rows
- 1);
562 if ((base_type
!= GLSL_TYPE_FLOAT
) || (rows
== 1))
565 /* GLSL matrix types are named mat{COLUMNS}x{ROWS}. Only the following
566 * combinations are valid:
574 #define IDX(c,r) (((c-1)*3) + (r-1))
576 switch (IDX(columns
, rows
)) {
577 case IDX(2,2): return mat2_type
;
578 case IDX(2,3): return mat2x3_type
;
579 case IDX(2,4): return mat2x4_type
;
580 case IDX(3,2): return mat3x2_type
;
581 case IDX(3,3): return mat3_type
;
582 case IDX(3,4): return mat3x4_type
;
583 case IDX(4,2): return mat4x2_type
;
584 case IDX(4,3): return mat4x3_type
;
585 case IDX(4,4): return mat4_type
;
586 default: return error_type
;
590 assert(!"Should not get here.");
596 glsl_type::get_array_instance(const glsl_type
*base
, unsigned array_size
)
599 if (array_types
== NULL
) {
600 array_types
= hash_table_ctor(64, hash_table_string_hash
,
601 hash_table_string_compare
);
604 /* Generate a name using the base type pointer in the key. This is
605 * done because the name of the base type may not be unique across
606 * shaders. For example, two shaders may have different record types
610 snprintf(key
, sizeof(key
), "%p[%u]", (void *) base
, array_size
);
612 const glsl_type
*t
= (glsl_type
*) hash_table_find(array_types
, key
);
614 t
= new glsl_type(base
, array_size
);
616 hash_table_insert(array_types
, (void *) t
, ralloc_strdup(mem_ctx
, key
));
619 assert(t
->base_type
== GLSL_TYPE_ARRAY
);
620 assert(t
->length
== array_size
);
621 assert(t
->fields
.array
== base
);
628 glsl_type::record_key_compare(const void *a
, const void *b
)
630 const glsl_type
*const key1
= (glsl_type
*) a
;
631 const glsl_type
*const key2
= (glsl_type
*) b
;
633 /* Return zero is the types match (there is zero difference) or non-zero
636 if (strcmp(key1
->name
, key2
->name
) != 0)
639 if (key1
->length
!= key2
->length
)
642 if (key1
->interface_packing
!= key2
->interface_packing
)
645 for (unsigned i
= 0; i
< key1
->length
; i
++) {
646 if (key1
->fields
.structure
[i
].type
!= key2
->fields
.structure
[i
].type
)
648 if (strcmp(key1
->fields
.structure
[i
].name
,
649 key2
->fields
.structure
[i
].name
) != 0)
651 if (key1
->fields
.structure
[i
].row_major
652 != key2
->fields
.structure
[i
].row_major
)
661 glsl_type::record_key_hash(const void *a
)
663 const glsl_type
*const key
= (glsl_type
*) a
;
667 size
= snprintf(hash_key
, sizeof(hash_key
), "%08x", key
->length
);
669 for (unsigned i
= 0; i
< key
->length
; i
++) {
670 if (size
>= sizeof(hash_key
))
673 size
+= snprintf(& hash_key
[size
], sizeof(hash_key
) - size
,
674 "%p", (void *) key
->fields
.structure
[i
].type
);
677 return hash_table_string_hash(& hash_key
);
682 glsl_type::get_record_instance(const glsl_struct_field
*fields
,
686 const glsl_type
key(fields
, num_fields
, name
);
688 if (record_types
== NULL
) {
689 record_types
= hash_table_ctor(64, record_key_hash
, record_key_compare
);
692 const glsl_type
*t
= (glsl_type
*) hash_table_find(record_types
, & key
);
694 t
= new glsl_type(fields
, num_fields
, name
);
696 hash_table_insert(record_types
, (void *) t
, t
);
699 assert(t
->base_type
== GLSL_TYPE_STRUCT
);
700 assert(t
->length
== num_fields
);
701 assert(strcmp(t
->name
, name
) == 0);
708 glsl_type::get_interface_instance(const glsl_struct_field
*fields
,
710 enum glsl_interface_packing packing
,
713 const glsl_type
key(fields
, num_fields
, packing
, name
);
715 if (interface_types
== NULL
) {
716 interface_types
= hash_table_ctor(64, record_key_hash
, record_key_compare
);
719 const glsl_type
*t
= (glsl_type
*) hash_table_find(interface_types
, & key
);
721 t
= new glsl_type(fields
, num_fields
, packing
, name
);
723 hash_table_insert(interface_types
, (void *) t
, t
);
726 assert(t
->base_type
== GLSL_TYPE_INTERFACE
);
727 assert(t
->length
== num_fields
);
728 assert(strcmp(t
->name
, name
) == 0);
735 glsl_type::field_type(const char *name
) const
737 if (this->base_type
!= GLSL_TYPE_STRUCT
738 && this->base_type
!= GLSL_TYPE_INTERFACE
)
741 for (unsigned i
= 0; i
< this->length
; i
++) {
742 if (strcmp(name
, this->fields
.structure
[i
].name
) == 0)
743 return this->fields
.structure
[i
].type
;
751 glsl_type::field_index(const char *name
) const
753 if (this->base_type
!= GLSL_TYPE_STRUCT
754 && this->base_type
!= GLSL_TYPE_INTERFACE
)
757 for (unsigned i
= 0; i
< this->length
; i
++) {
758 if (strcmp(name
, this->fields
.structure
[i
].name
) == 0)
767 glsl_type::component_slots() const
769 switch (this->base_type
) {
772 case GLSL_TYPE_FLOAT
:
774 return this->components();
776 case GLSL_TYPE_STRUCT
:
777 case GLSL_TYPE_INTERFACE
: {
780 for (unsigned i
= 0; i
< this->length
; i
++)
781 size
+= this->fields
.structure
[i
].type
->component_slots();
786 case GLSL_TYPE_ARRAY
:
787 return this->length
* this->fields
.array
->component_slots();
789 case GLSL_TYPE_SAMPLER
:
791 case GLSL_TYPE_ERROR
:
799 glsl_type::can_implicitly_convert_to(const glsl_type
*desired
) const
804 /* There is no conversion among matrix types. */
805 if (this->matrix_columns
> 1 || desired
->matrix_columns
> 1)
808 /* int and uint can be converted to float. */
809 return desired
->is_float()
810 && this->is_integer()
811 && this->vector_elements
== desired
->vector_elements
;
815 glsl_type::std140_base_alignment(bool row_major
) const
817 /* (1) If the member is a scalar consuming <N> basic machine units, the
818 * base alignment is <N>.
820 * (2) If the member is a two- or four-component vector with components
821 * consuming <N> basic machine units, the base alignment is 2<N> or
822 * 4<N>, respectively.
824 * (3) If the member is a three-component vector with components consuming
825 * <N> basic machine units, the base alignment is 4<N>.
827 if (this->is_scalar() || this->is_vector()) {
828 switch (this->vector_elements
) {
839 /* (4) If the member is an array of scalars or vectors, the base alignment
840 * and array stride are set to match the base alignment of a single
841 * array element, according to rules (1), (2), and (3), and rounded up
842 * to the base alignment of a vec4. The array may have padding at the
843 * end; the base offset of the member following the array is rounded up
844 * to the next multiple of the base alignment.
846 * (6) If the member is an array of <S> column-major matrices with <C>
847 * columns and <R> rows, the matrix is stored identically to a row of
848 * <S>*<C> column vectors with <R> components each, according to rule
851 * (8) If the member is an array of <S> row-major matrices with <C> columns
852 * and <R> rows, the matrix is stored identically to a row of <S>*<R>
853 * row vectors with <C> components each, according to rule (4).
855 * (10) If the member is an array of <S> structures, the <S> elements of
856 * the array are laid out in order, according to rule (9).
858 if (this->is_array()) {
859 if (this->fields
.array
->is_scalar() ||
860 this->fields
.array
->is_vector() ||
861 this->fields
.array
->is_matrix()) {
862 return MAX2(this->fields
.array
->std140_base_alignment(row_major
), 16);
864 assert(this->fields
.array
->is_record());
865 return this->fields
.array
->std140_base_alignment(row_major
);
869 /* (5) If the member is a column-major matrix with <C> columns and
870 * <R> rows, the matrix is stored identically to an array of
871 * <C> column vectors with <R> components each, according to
874 * (7) If the member is a row-major matrix with <C> columns and <R>
875 * rows, the matrix is stored identically to an array of <R>
876 * row vectors with <C> components each, according to rule (4).
878 if (this->is_matrix()) {
879 const struct glsl_type
*vec_type
, *array_type
;
880 int c
= this->matrix_columns
;
881 int r
= this->vector_elements
;
884 vec_type
= get_instance(GLSL_TYPE_FLOAT
, c
, 1);
885 array_type
= glsl_type::get_array_instance(vec_type
, r
);
887 vec_type
= get_instance(GLSL_TYPE_FLOAT
, r
, 1);
888 array_type
= glsl_type::get_array_instance(vec_type
, c
);
891 return array_type
->std140_base_alignment(false);
894 /* (9) If the member is a structure, the base alignment of the
895 * structure is <N>, where <N> is the largest base alignment
896 * value of any of its members, and rounded up to the base
897 * alignment of a vec4. The individual members of this
898 * sub-structure are then assigned offsets by applying this set
899 * of rules recursively, where the base offset of the first
900 * member of the sub-structure is equal to the aligned offset
901 * of the structure. The structure may have padding at the end;
902 * the base offset of the member following the sub-structure is
903 * rounded up to the next multiple of the base alignment of the
906 if (this->is_record()) {
907 unsigned base_alignment
= 16;
908 for (unsigned i
= 0; i
< this->length
; i
++) {
909 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
910 base_alignment
= MAX2(base_alignment
,
911 field_type
->std140_base_alignment(row_major
));
913 return base_alignment
;
916 assert(!"not reached");
921 glsl_type::std140_size(bool row_major
) const
923 /* (1) If the member is a scalar consuming <N> basic machine units, the
924 * base alignment is <N>.
926 * (2) If the member is a two- or four-component vector with components
927 * consuming <N> basic machine units, the base alignment is 2<N> or
928 * 4<N>, respectively.
930 * (3) If the member is a three-component vector with components consuming
931 * <N> basic machine units, the base alignment is 4<N>.
933 if (this->is_scalar() || this->is_vector()) {
934 return this->vector_elements
* 4;
937 /* (5) If the member is a column-major matrix with <C> columns and
938 * <R> rows, the matrix is stored identically to an array of
939 * <C> column vectors with <R> components each, according to
942 * (6) If the member is an array of <S> column-major matrices with <C>
943 * columns and <R> rows, the matrix is stored identically to a row of
944 * <S>*<C> column vectors with <R> components each, according to rule
947 * (7) If the member is a row-major matrix with <C> columns and <R>
948 * rows, the matrix is stored identically to an array of <R>
949 * row vectors with <C> components each, according to rule (4).
951 * (8) If the member is an array of <S> row-major matrices with <C> columns
952 * and <R> rows, the matrix is stored identically to a row of <S>*<R>
953 * row vectors with <C> components each, according to rule (4).
955 if (this->is_matrix() || (this->is_array() &&
956 this->fields
.array
->is_matrix())) {
957 const struct glsl_type
*element_type
;
958 const struct glsl_type
*vec_type
;
959 unsigned int array_len
;
961 if (this->is_array()) {
962 element_type
= this->fields
.array
;
963 array_len
= this->length
;
970 vec_type
= get_instance(GLSL_TYPE_FLOAT
,
971 element_type
->matrix_columns
, 1);
972 array_len
*= element_type
->vector_elements
;
974 vec_type
= get_instance(GLSL_TYPE_FLOAT
,
975 element_type
->vector_elements
, 1);
976 array_len
*= element_type
->matrix_columns
;
978 const glsl_type
*array_type
= glsl_type::get_array_instance(vec_type
,
981 return array_type
->std140_size(false);
984 /* (4) If the member is an array of scalars or vectors, the base alignment
985 * and array stride are set to match the base alignment of a single
986 * array element, according to rules (1), (2), and (3), and rounded up
987 * to the base alignment of a vec4. The array may have padding at the
988 * end; the base offset of the member following the array is rounded up
989 * to the next multiple of the base alignment.
991 * (10) If the member is an array of <S> structures, the <S> elements of
992 * the array are laid out in order, according to rule (9).
994 if (this->is_array()) {
995 if (this->fields
.array
->is_record()) {
996 return this->length
* this->fields
.array
->std140_size(row_major
);
998 unsigned element_base_align
=
999 this->fields
.array
->std140_base_alignment(row_major
);
1000 return this->length
* MAX2(element_base_align
, 16);
1004 /* (9) If the member is a structure, the base alignment of the
1005 * structure is <N>, where <N> is the largest base alignment
1006 * value of any of its members, and rounded up to the base
1007 * alignment of a vec4. The individual members of this
1008 * sub-structure are then assigned offsets by applying this set
1009 * of rules recursively, where the base offset of the first
1010 * member of the sub-structure is equal to the aligned offset
1011 * of the structure. The structure may have padding at the end;
1012 * the base offset of the member following the sub-structure is
1013 * rounded up to the next multiple of the base alignment of the
1016 if (this->is_record()) {
1018 for (unsigned i
= 0; i
< this->length
; i
++) {
1019 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
1020 unsigned align
= field_type
->std140_base_alignment(row_major
);
1021 size
= glsl_align(size
, align
);
1022 size
+= field_type
->std140_size(row_major
);
1024 size
= glsl_align(size
,
1025 this->fields
.structure
[0].type
->std140_base_alignment(row_major
));
1029 assert(!"not reached");