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
;
201 case GLSL_SAMPLER_DIM_MS
:
202 return (t
->sampler_array
) ? TEXTURE_2D_MULTISAMPLE_ARRAY_INDEX
: TEXTURE_2D_MULTISAMPLE_INDEX
;
204 assert(!"Should not get here.");
205 return TEXTURE_BUFFER_INDEX
;
210 glsl_type::generate_100ES_types(glsl_symbol_table
*symtab
)
212 bool skip_1d
= false;
213 add_types_to_symbol_table(symtab
, builtin_core_types
,
214 Elements(builtin_core_types
),
216 add_types_to_symbol_table(symtab
, builtin_structure_types
,
217 Elements(builtin_structure_types
),
219 add_types_to_symbol_table(symtab
, void_type
, 1, false, skip_1d
);
223 glsl_type::generate_300ES_types(glsl_symbol_table
*symtab
)
225 /* GLSL 3.00 ES types are the same as GLSL 1.30 types, except that 1D
226 * samplers are skipped, and samplerCubeShadow is added.
228 bool add_deprecated
= false;
231 generate_130_types(symtab
, add_deprecated
, skip_1d
);
233 add_types_to_symbol_table(symtab
, &_samplerCubeShadow_type
, 1, false,
238 glsl_type::generate_110_types(glsl_symbol_table
*symtab
, bool add_deprecated
,
241 generate_100ES_types(symtab
);
243 add_types_to_symbol_table(symtab
, builtin_110_types
,
244 Elements(builtin_110_types
),
246 add_types_to_symbol_table(symtab
, &_sampler3D_type
, 1, false, skip_1d
);
247 if (add_deprecated
) {
248 add_types_to_symbol_table(symtab
, builtin_110_deprecated_structure_types
,
249 Elements(builtin_110_deprecated_structure_types
),
256 glsl_type::generate_120_types(glsl_symbol_table
*symtab
, bool add_deprecated
,
259 generate_110_types(symtab
, add_deprecated
, skip_1d
);
261 add_types_to_symbol_table(symtab
, builtin_120_types
,
262 Elements(builtin_120_types
), false, skip_1d
);
267 glsl_type::generate_130_types(glsl_symbol_table
*symtab
, bool add_deprecated
,
270 generate_120_types(symtab
, add_deprecated
, skip_1d
);
272 add_types_to_symbol_table(symtab
, builtin_130_types
,
273 Elements(builtin_130_types
), false, skip_1d
);
274 generate_EXT_texture_array_types(symtab
, false);
279 glsl_type::generate_140_types(glsl_symbol_table
*symtab
)
281 bool skip_1d
= false;
283 generate_130_types(symtab
, false, skip_1d
);
285 add_types_to_symbol_table(symtab
, builtin_140_types
,
286 Elements(builtin_140_types
), false, skip_1d
);
288 add_types_to_symbol_table(symtab
, builtin_EXT_texture_buffer_object_types
,
289 Elements(builtin_EXT_texture_buffer_object_types
),
295 glsl_type::generate_150_types(glsl_symbol_table
*symtab
)
297 generate_140_types(symtab
);
298 generate_ARB_texture_multisample_types(symtab
, false);
303 glsl_type::generate_ARB_texture_rectangle_types(glsl_symbol_table
*symtab
,
306 bool skip_1d
= false;
308 add_types_to_symbol_table(symtab
, builtin_ARB_texture_rectangle_types
,
309 Elements(builtin_ARB_texture_rectangle_types
),
315 glsl_type::generate_EXT_texture_array_types(glsl_symbol_table
*symtab
,
318 bool skip_1d
= false;
320 add_types_to_symbol_table(symtab
, builtin_EXT_texture_array_types
,
321 Elements(builtin_EXT_texture_array_types
),
327 glsl_type::generate_OES_texture_3D_types(glsl_symbol_table
*symtab
, bool warn
)
329 bool skip_1d
= false;
331 add_types_to_symbol_table(symtab
, &_sampler3D_type
, 1, warn
, skip_1d
);
336 glsl_type::generate_OES_EGL_image_external_types(glsl_symbol_table
*symtab
,
339 bool skip_1d
= false;
341 add_types_to_symbol_table(symtab
, builtin_OES_EGL_image_external_types
,
342 Elements(builtin_OES_EGL_image_external_types
),
347 glsl_type::generate_ARB_texture_cube_map_array_types(glsl_symbol_table
*symtab
,
350 bool skip_1d
= false;
352 add_types_to_symbol_table(symtab
, builtin_ARB_texture_cube_map_array_types
,
353 Elements(builtin_ARB_texture_cube_map_array_types
),
358 glsl_type::generate_ARB_texture_multisample_types(glsl_symbol_table
*symtab
,
361 bool skip_1d
= false;
362 add_types_to_symbol_table(symtab
, builtin_ARB_texture_multisample_types
,
363 Elements(builtin_ARB_texture_multisample_types
),
368 _mesa_glsl_initialize_types(struct _mesa_glsl_parse_state
*state
)
370 if (state
->es_shader
) {
371 switch (state
->language_version
) {
373 assert(state
->es_shader
);
374 glsl_type::generate_100ES_types(state
->symbols
);
377 glsl_type::generate_300ES_types(state
->symbols
);
380 assert(!"Unexpected language version");
384 bool skip_1d
= false;
385 switch (state
->language_version
) {
387 glsl_type::generate_110_types(state
->symbols
, true, skip_1d
);
390 glsl_type::generate_120_types(state
->symbols
, true, skip_1d
);
393 glsl_type::generate_130_types(state
->symbols
, true, skip_1d
);
396 glsl_type::generate_140_types(state
->symbols
);
399 glsl_type::generate_150_types(state
->symbols
);
402 assert(!"Unexpected language version");
407 if (state
->ARB_texture_rectangle_enable
||
408 state
->is_version(140, 0)) {
409 glsl_type::generate_ARB_texture_rectangle_types(state
->symbols
,
410 state
->ARB_texture_rectangle_warn
);
412 if (state
->OES_texture_3D_enable
413 && state
->is_version(0, 100)) {
414 glsl_type::generate_OES_texture_3D_types(state
->symbols
,
415 state
->OES_texture_3D_warn
);
418 if (state
->EXT_texture_array_enable
419 && !state
->is_version(130, 0)) {
420 // These are already included in 130; don't create twice.
421 glsl_type::generate_EXT_texture_array_types(state
->symbols
,
422 state
->EXT_texture_array_warn
);
425 /* We cannot check for language_version == 100 here because we need the
426 * types to support fixed-function program generation. But this is fine
427 * since the extension is never enabled for OpenGL contexts.
429 if (state
->OES_EGL_image_external_enable
) {
430 glsl_type::generate_OES_EGL_image_external_types(state
->symbols
,
431 state
->OES_EGL_image_external_warn
);
434 if (state
->ARB_texture_cube_map_array_enable
) {
435 glsl_type::generate_ARB_texture_cube_map_array_types(state
->symbols
,
436 state
->ARB_texture_cube_map_array_warn
);
439 if (state
->ARB_texture_multisample_enable
) {
440 glsl_type::generate_ARB_texture_multisample_types(state
->symbols
,
441 state
->ARB_texture_multisample_warn
);
446 const glsl_type
*glsl_type::get_base_type() const
453 case GLSL_TYPE_FLOAT
:
463 const glsl_type
*glsl_type::get_scalar_type() const
465 const glsl_type
*type
= this;
468 while (type
->base_type
== GLSL_TYPE_ARRAY
)
469 type
= type
->fields
.array
;
471 /* Handle vectors and matrices */
472 switch (type
->base_type
) {
477 case GLSL_TYPE_FLOAT
:
482 /* Handle everything else */
489 _mesa_glsl_release_types(void)
491 if (glsl_type::array_types
!= NULL
) {
492 hash_table_dtor(glsl_type::array_types
);
493 glsl_type::array_types
= NULL
;
496 if (glsl_type::record_types
!= NULL
) {
497 hash_table_dtor(glsl_type::record_types
);
498 glsl_type::record_types
= NULL
;
503 glsl_type::glsl_type(const glsl_type
*array
, unsigned length
) :
504 base_type(GLSL_TYPE_ARRAY
),
505 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
506 sampler_type(0), interface_packing(0),
507 vector_elements(0), matrix_columns(0),
508 name(NULL
), length(length
)
510 this->fields
.array
= array
;
511 /* Inherit the gl type of the base. The GL type is used for
512 * uniform/statevar handling in Mesa and the arrayness of the type
513 * is represented by the size rather than the type.
515 this->gl_type
= array
->gl_type
;
517 /* Allow a maximum of 10 characters for the array size. This is enough
518 * for 32-bits of ~0. The extra 3 are for the '[', ']', and terminating
521 const unsigned name_length
= strlen(array
->name
) + 10 + 3;
522 char *const n
= (char *) ralloc_size(this->mem_ctx
, name_length
);
525 snprintf(n
, name_length
, "%s[]", array
->name
);
527 snprintf(n
, name_length
, "%s[%u]", array
->name
, length
);
534 glsl_type::get_instance(unsigned base_type
, unsigned rows
, unsigned columns
)
536 if (base_type
== GLSL_TYPE_VOID
)
539 if ((rows
< 1) || (rows
> 4) || (columns
< 1) || (columns
> 4))
542 /* Treat GLSL vectors as Nx1 matrices.
547 return uint_type
+ (rows
- 1);
549 return int_type
+ (rows
- 1);
550 case GLSL_TYPE_FLOAT
:
551 return float_type
+ (rows
- 1);
553 return bool_type
+ (rows
- 1);
558 if ((base_type
!= GLSL_TYPE_FLOAT
) || (rows
== 1))
561 /* GLSL matrix types are named mat{COLUMNS}x{ROWS}. Only the following
562 * combinations are valid:
570 #define IDX(c,r) (((c-1)*3) + (r-1))
572 switch (IDX(columns
, rows
)) {
573 case IDX(2,2): return mat2_type
;
574 case IDX(2,3): return mat2x3_type
;
575 case IDX(2,4): return mat2x4_type
;
576 case IDX(3,2): return mat3x2_type
;
577 case IDX(3,3): return mat3_type
;
578 case IDX(3,4): return mat3x4_type
;
579 case IDX(4,2): return mat4x2_type
;
580 case IDX(4,3): return mat4x3_type
;
581 case IDX(4,4): return mat4_type
;
582 default: return error_type
;
586 assert(!"Should not get here.");
592 glsl_type::get_array_instance(const glsl_type
*base
, unsigned array_size
)
595 if (array_types
== NULL
) {
596 array_types
= hash_table_ctor(64, hash_table_string_hash
,
597 hash_table_string_compare
);
600 /* Generate a name using the base type pointer in the key. This is
601 * done because the name of the base type may not be unique across
602 * shaders. For example, two shaders may have different record types
606 snprintf(key
, sizeof(key
), "%p[%u]", (void *) base
, array_size
);
608 const glsl_type
*t
= (glsl_type
*) hash_table_find(array_types
, key
);
610 t
= new glsl_type(base
, array_size
);
612 hash_table_insert(array_types
, (void *) t
, ralloc_strdup(mem_ctx
, key
));
615 assert(t
->base_type
== GLSL_TYPE_ARRAY
);
616 assert(t
->length
== array_size
);
617 assert(t
->fields
.array
== base
);
624 glsl_type::record_key_compare(const void *a
, const void *b
)
626 const glsl_type
*const key1
= (glsl_type
*) a
;
627 const glsl_type
*const key2
= (glsl_type
*) b
;
629 /* Return zero is the types match (there is zero difference) or non-zero
632 if (strcmp(key1
->name
, key2
->name
) != 0)
635 if (key1
->length
!= key2
->length
)
638 if (key1
->interface_packing
!= key2
->interface_packing
)
641 for (unsigned i
= 0; i
< key1
->length
; i
++) {
642 if (key1
->fields
.structure
[i
].type
!= key2
->fields
.structure
[i
].type
)
644 if (strcmp(key1
->fields
.structure
[i
].name
,
645 key2
->fields
.structure
[i
].name
) != 0)
647 if (key1
->fields
.structure
[i
].row_major
648 != key2
->fields
.structure
[i
].row_major
)
657 glsl_type::record_key_hash(const void *a
)
659 const glsl_type
*const key
= (glsl_type
*) a
;
663 size
= snprintf(hash_key
, sizeof(hash_key
), "%08x", key
->length
);
665 for (unsigned i
= 0; i
< key
->length
; i
++) {
666 if (size
>= sizeof(hash_key
))
669 size
+= snprintf(& hash_key
[size
], sizeof(hash_key
) - size
,
670 "%p", (void *) key
->fields
.structure
[i
].type
);
673 return hash_table_string_hash(& hash_key
);
678 glsl_type::get_record_instance(const glsl_struct_field
*fields
,
682 const glsl_type
key(fields
, num_fields
, name
);
684 if (record_types
== NULL
) {
685 record_types
= hash_table_ctor(64, record_key_hash
, record_key_compare
);
688 const glsl_type
*t
= (glsl_type
*) hash_table_find(record_types
, & key
);
690 t
= new glsl_type(fields
, num_fields
, name
);
692 hash_table_insert(record_types
, (void *) t
, t
);
695 assert(t
->base_type
== GLSL_TYPE_STRUCT
);
696 assert(t
->length
== num_fields
);
697 assert(strcmp(t
->name
, name
) == 0);
704 glsl_type::get_interface_instance(const glsl_struct_field
*fields
,
706 enum glsl_interface_packing packing
,
709 const glsl_type
key(fields
, num_fields
, packing
, name
);
711 if (interface_types
== NULL
) {
712 interface_types
= hash_table_ctor(64, record_key_hash
, record_key_compare
);
715 const glsl_type
*t
= (glsl_type
*) hash_table_find(interface_types
, & key
);
717 t
= new glsl_type(fields
, num_fields
, packing
, name
);
719 hash_table_insert(interface_types
, (void *) t
, t
);
722 assert(t
->base_type
== GLSL_TYPE_INTERFACE
);
723 assert(t
->length
== num_fields
);
724 assert(strcmp(t
->name
, name
) == 0);
731 glsl_type::field_type(const char *name
) const
733 if (this->base_type
!= GLSL_TYPE_STRUCT
734 && this->base_type
!= GLSL_TYPE_INTERFACE
)
737 for (unsigned i
= 0; i
< this->length
; i
++) {
738 if (strcmp(name
, this->fields
.structure
[i
].name
) == 0)
739 return this->fields
.structure
[i
].type
;
747 glsl_type::field_index(const char *name
) const
749 if (this->base_type
!= GLSL_TYPE_STRUCT
750 && this->base_type
!= GLSL_TYPE_INTERFACE
)
753 for (unsigned i
= 0; i
< this->length
; i
++) {
754 if (strcmp(name
, this->fields
.structure
[i
].name
) == 0)
763 glsl_type::component_slots() const
765 switch (this->base_type
) {
768 case GLSL_TYPE_FLOAT
:
770 return this->components();
772 case GLSL_TYPE_STRUCT
:
773 case GLSL_TYPE_INTERFACE
: {
776 for (unsigned i
= 0; i
< this->length
; i
++)
777 size
+= this->fields
.structure
[i
].type
->component_slots();
782 case GLSL_TYPE_ARRAY
:
783 return this->length
* this->fields
.array
->component_slots();
785 case GLSL_TYPE_SAMPLER
:
787 case GLSL_TYPE_ERROR
:
795 glsl_type::can_implicitly_convert_to(const glsl_type
*desired
) const
800 /* There is no conversion among matrix types. */
801 if (this->matrix_columns
> 1 || desired
->matrix_columns
> 1)
804 /* int and uint can be converted to float. */
805 return desired
->is_float()
806 && this->is_integer()
807 && this->vector_elements
== desired
->vector_elements
;
811 glsl_type::std140_base_alignment(bool row_major
) const
813 /* (1) If the member is a scalar consuming <N> basic machine units, the
814 * base alignment is <N>.
816 * (2) If the member is a two- or four-component vector with components
817 * consuming <N> basic machine units, the base alignment is 2<N> or
818 * 4<N>, respectively.
820 * (3) If the member is a three-component vector with components consuming
821 * <N> basic machine units, the base alignment is 4<N>.
823 if (this->is_scalar() || this->is_vector()) {
824 switch (this->vector_elements
) {
835 /* (4) If the member is an array of scalars or vectors, the base alignment
836 * and array stride are set to match the base alignment of a single
837 * array element, according to rules (1), (2), and (3), and rounded up
838 * to the base alignment of a vec4. The array may have padding at the
839 * end; the base offset of the member following the array is rounded up
840 * to the next multiple of the base alignment.
842 * (6) If the member is an array of <S> column-major matrices with <C>
843 * columns and <R> rows, the matrix is stored identically to a row of
844 * <S>*<C> column vectors with <R> components each, according to rule
847 * (8) If the member is an array of <S> row-major matrices with <C> columns
848 * and <R> rows, the matrix is stored identically to a row of <S>*<R>
849 * row vectors with <C> components each, according to rule (4).
851 * (10) If the member is an array of <S> structures, the <S> elements of
852 * the array are laid out in order, according to rule (9).
854 if (this->is_array()) {
855 if (this->fields
.array
->is_scalar() ||
856 this->fields
.array
->is_vector() ||
857 this->fields
.array
->is_matrix()) {
858 return MAX2(this->fields
.array
->std140_base_alignment(row_major
), 16);
860 assert(this->fields
.array
->is_record());
861 return this->fields
.array
->std140_base_alignment(row_major
);
865 /* (5) If the member is a column-major matrix with <C> columns and
866 * <R> rows, the matrix is stored identically to an array of
867 * <C> column vectors with <R> components each, according to
870 * (7) If the member is a row-major matrix with <C> columns and <R>
871 * rows, the matrix is stored identically to an array of <R>
872 * row vectors with <C> components each, according to rule (4).
874 if (this->is_matrix()) {
875 const struct glsl_type
*vec_type
, *array_type
;
876 int c
= this->matrix_columns
;
877 int r
= this->vector_elements
;
880 vec_type
= get_instance(GLSL_TYPE_FLOAT
, c
, 1);
881 array_type
= glsl_type::get_array_instance(vec_type
, r
);
883 vec_type
= get_instance(GLSL_TYPE_FLOAT
, r
, 1);
884 array_type
= glsl_type::get_array_instance(vec_type
, c
);
887 return array_type
->std140_base_alignment(false);
890 /* (9) If the member is a structure, the base alignment of the
891 * structure is <N>, where <N> is the largest base alignment
892 * value of any of its members, and rounded up to the base
893 * alignment of a vec4. The individual members of this
894 * sub-structure are then assigned offsets by applying this set
895 * of rules recursively, where the base offset of the first
896 * member of the sub-structure is equal to the aligned offset
897 * of the structure. The structure may have padding at the end;
898 * the base offset of the member following the sub-structure is
899 * rounded up to the next multiple of the base alignment of the
902 if (this->is_record()) {
903 unsigned base_alignment
= 16;
904 for (unsigned i
= 0; i
< this->length
; i
++) {
905 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
906 base_alignment
= MAX2(base_alignment
,
907 field_type
->std140_base_alignment(row_major
));
909 return base_alignment
;
912 assert(!"not reached");
917 glsl_type::std140_size(bool row_major
) const
919 /* (1) If the member is a scalar consuming <N> basic machine units, the
920 * base alignment is <N>.
922 * (2) If the member is a two- or four-component vector with components
923 * consuming <N> basic machine units, the base alignment is 2<N> or
924 * 4<N>, respectively.
926 * (3) If the member is a three-component vector with components consuming
927 * <N> basic machine units, the base alignment is 4<N>.
929 if (this->is_scalar() || this->is_vector()) {
930 return this->vector_elements
* 4;
933 /* (5) If the member is a column-major matrix with <C> columns and
934 * <R> rows, the matrix is stored identically to an array of
935 * <C> column vectors with <R> components each, according to
938 * (6) If the member is an array of <S> column-major matrices with <C>
939 * columns and <R> rows, the matrix is stored identically to a row of
940 * <S>*<C> column vectors with <R> components each, according to rule
943 * (7) If the member is a row-major matrix with <C> columns and <R>
944 * rows, the matrix is stored identically to an array of <R>
945 * row vectors with <C> components each, according to rule (4).
947 * (8) If the member is an array of <S> row-major matrices with <C> columns
948 * and <R> rows, the matrix is stored identically to a row of <S>*<R>
949 * row vectors with <C> components each, according to rule (4).
951 if (this->is_matrix() || (this->is_array() &&
952 this->fields
.array
->is_matrix())) {
953 const struct glsl_type
*element_type
;
954 const struct glsl_type
*vec_type
;
955 unsigned int array_len
;
957 if (this->is_array()) {
958 element_type
= this->fields
.array
;
959 array_len
= this->length
;
966 vec_type
= get_instance(GLSL_TYPE_FLOAT
,
967 element_type
->matrix_columns
, 1);
968 array_len
*= element_type
->vector_elements
;
970 vec_type
= get_instance(GLSL_TYPE_FLOAT
,
971 element_type
->vector_elements
, 1);
972 array_len
*= element_type
->matrix_columns
;
974 const glsl_type
*array_type
= glsl_type::get_array_instance(vec_type
,
977 return array_type
->std140_size(false);
980 /* (4) If the member is an array of scalars or vectors, the base alignment
981 * and array stride are set to match the base alignment of a single
982 * array element, according to rules (1), (2), and (3), and rounded up
983 * to the base alignment of a vec4. The array may have padding at the
984 * end; the base offset of the member following the array is rounded up
985 * to the next multiple of the base alignment.
987 * (10) If the member is an array of <S> structures, the <S> elements of
988 * the array are laid out in order, according to rule (9).
990 if (this->is_array()) {
991 if (this->fields
.array
->is_record()) {
992 return this->length
* this->fields
.array
->std140_size(row_major
);
994 unsigned element_base_align
=
995 this->fields
.array
->std140_base_alignment(row_major
);
996 return this->length
* MAX2(element_base_align
, 16);
1000 /* (9) If the member is a structure, the base alignment of the
1001 * structure is <N>, where <N> is the largest base alignment
1002 * value of any of its members, and rounded up to the base
1003 * alignment of a vec4. The individual members of this
1004 * sub-structure are then assigned offsets by applying this set
1005 * of rules recursively, where the base offset of the first
1006 * member of the sub-structure is equal to the aligned offset
1007 * of the structure. The structure may have padding at the end;
1008 * the base offset of the member following the sub-structure is
1009 * rounded up to the next multiple of the base alignment of the
1012 if (this->is_record()) {
1014 for (unsigned i
= 0; i
< this->length
; i
++) {
1015 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
1016 unsigned align
= field_type
->std140_base_alignment(row_major
);
1017 size
= glsl_align(size
, align
);
1018 size
+= field_type
->std140_size(row_major
);
1020 size
= glsl_align(size
,
1021 this->fields
.structure
[0].type
->std140_base_alignment(row_major
));
1025 assert(!"not reached");