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
);
537 const glsl_type
*const
538 glsl_type::vec(unsigned components
)
540 if (components
== 0 || components
> 4)
543 static const glsl_type
*const ts
[] = {
544 float_type
, vec2_type
, vec3_type
, vec4_type
546 return ts
[components
- 1];
550 const glsl_type
*const
551 glsl_type::ivec(unsigned components
)
553 if (components
== 0 || components
> 4)
556 static const glsl_type
*const ts
[] = {
557 int_type
, ivec2_type
, ivec3_type
, ivec4_type
559 return ts
[components
- 1];
563 const glsl_type
*const
564 glsl_type::uvec(unsigned components
)
566 if (components
== 0 || components
> 4)
569 static const glsl_type
*const ts
[] = {
570 uint_type
, uvec2_type
, uvec3_type
, uvec4_type
572 return ts
[components
- 1];
576 const glsl_type
*const
577 glsl_type::bvec(unsigned components
)
579 if (components
== 0 || components
> 4)
582 static const glsl_type
*const ts
[] = {
583 bool_type
, bvec2_type
, bvec3_type
, bvec4_type
585 return ts
[components
- 1];
590 glsl_type::get_instance(unsigned base_type
, unsigned rows
, unsigned columns
)
592 if (base_type
== GLSL_TYPE_VOID
)
595 if ((rows
< 1) || (rows
> 4) || (columns
< 1) || (columns
> 4))
598 /* Treat GLSL vectors as Nx1 matrices.
603 return uint_type
+ (rows
- 1);
605 return int_type
+ (rows
- 1);
606 case GLSL_TYPE_FLOAT
:
607 return float_type
+ (rows
- 1);
609 return bool_type
+ (rows
- 1);
614 if ((base_type
!= GLSL_TYPE_FLOAT
) || (rows
== 1))
617 /* GLSL matrix types are named mat{COLUMNS}x{ROWS}. Only the following
618 * combinations are valid:
626 #define IDX(c,r) (((c-1)*3) + (r-1))
628 switch (IDX(columns
, rows
)) {
629 case IDX(2,2): return mat2_type
;
630 case IDX(2,3): return mat2x3_type
;
631 case IDX(2,4): return mat2x4_type
;
632 case IDX(3,2): return mat3x2_type
;
633 case IDX(3,3): return mat3_type
;
634 case IDX(3,4): return mat3x4_type
;
635 case IDX(4,2): return mat4x2_type
;
636 case IDX(4,3): return mat4x3_type
;
637 case IDX(4,4): return mat4_type
;
638 default: return error_type
;
642 assert(!"Should not get here.");
648 glsl_type::get_array_instance(const glsl_type
*base
, unsigned array_size
)
651 if (array_types
== NULL
) {
652 array_types
= hash_table_ctor(64, hash_table_string_hash
,
653 hash_table_string_compare
);
656 /* Generate a name using the base type pointer in the key. This is
657 * done because the name of the base type may not be unique across
658 * shaders. For example, two shaders may have different record types
662 snprintf(key
, sizeof(key
), "%p[%u]", (void *) base
, array_size
);
664 const glsl_type
*t
= (glsl_type
*) hash_table_find(array_types
, key
);
666 t
= new glsl_type(base
, array_size
);
668 hash_table_insert(array_types
, (void *) t
, ralloc_strdup(mem_ctx
, key
));
671 assert(t
->base_type
== GLSL_TYPE_ARRAY
);
672 assert(t
->length
== array_size
);
673 assert(t
->fields
.array
== base
);
680 glsl_type::record_key_compare(const void *a
, const void *b
)
682 const glsl_type
*const key1
= (glsl_type
*) a
;
683 const glsl_type
*const key2
= (glsl_type
*) b
;
685 /* Return zero is the types match (there is zero difference) or non-zero
688 if (strcmp(key1
->name
, key2
->name
) != 0)
691 if (key1
->length
!= key2
->length
)
694 if (key1
->interface_packing
!= key2
->interface_packing
)
697 for (unsigned i
= 0; i
< key1
->length
; i
++) {
698 if (key1
->fields
.structure
[i
].type
!= key2
->fields
.structure
[i
].type
)
700 if (strcmp(key1
->fields
.structure
[i
].name
,
701 key2
->fields
.structure
[i
].name
) != 0)
703 if (key1
->fields
.structure
[i
].row_major
704 != key2
->fields
.structure
[i
].row_major
)
713 glsl_type::record_key_hash(const void *a
)
715 const glsl_type
*const key
= (glsl_type
*) a
;
719 size
= snprintf(hash_key
, sizeof(hash_key
), "%08x", key
->length
);
721 for (unsigned i
= 0; i
< key
->length
; i
++) {
722 if (size
>= sizeof(hash_key
))
725 size
+= snprintf(& hash_key
[size
], sizeof(hash_key
) - size
,
726 "%p", (void *) key
->fields
.structure
[i
].type
);
729 return hash_table_string_hash(& hash_key
);
734 glsl_type::get_record_instance(const glsl_struct_field
*fields
,
738 const glsl_type
key(fields
, num_fields
, name
);
740 if (record_types
== NULL
) {
741 record_types
= hash_table_ctor(64, record_key_hash
, record_key_compare
);
744 const glsl_type
*t
= (glsl_type
*) hash_table_find(record_types
, & key
);
746 t
= new glsl_type(fields
, num_fields
, name
);
748 hash_table_insert(record_types
, (void *) t
, t
);
751 assert(t
->base_type
== GLSL_TYPE_STRUCT
);
752 assert(t
->length
== num_fields
);
753 assert(strcmp(t
->name
, name
) == 0);
760 glsl_type::get_interface_instance(const glsl_struct_field
*fields
,
762 enum glsl_interface_packing packing
,
765 const glsl_type
key(fields
, num_fields
, packing
, name
);
767 if (interface_types
== NULL
) {
768 interface_types
= hash_table_ctor(64, record_key_hash
, record_key_compare
);
771 const glsl_type
*t
= (glsl_type
*) hash_table_find(interface_types
, & key
);
773 t
= new glsl_type(fields
, num_fields
, packing
, name
);
775 hash_table_insert(interface_types
, (void *) t
, t
);
778 assert(t
->base_type
== GLSL_TYPE_INTERFACE
);
779 assert(t
->length
== num_fields
);
780 assert(strcmp(t
->name
, name
) == 0);
787 glsl_type::field_type(const char *name
) const
789 if (this->base_type
!= GLSL_TYPE_STRUCT
790 && this->base_type
!= GLSL_TYPE_INTERFACE
)
793 for (unsigned i
= 0; i
< this->length
; i
++) {
794 if (strcmp(name
, this->fields
.structure
[i
].name
) == 0)
795 return this->fields
.structure
[i
].type
;
803 glsl_type::field_index(const char *name
) const
805 if (this->base_type
!= GLSL_TYPE_STRUCT
806 && this->base_type
!= GLSL_TYPE_INTERFACE
)
809 for (unsigned i
= 0; i
< this->length
; i
++) {
810 if (strcmp(name
, this->fields
.structure
[i
].name
) == 0)
819 glsl_type::component_slots() const
821 switch (this->base_type
) {
824 case GLSL_TYPE_FLOAT
:
826 return this->components();
828 case GLSL_TYPE_STRUCT
:
829 case GLSL_TYPE_INTERFACE
: {
832 for (unsigned i
= 0; i
< this->length
; i
++)
833 size
+= this->fields
.structure
[i
].type
->component_slots();
838 case GLSL_TYPE_ARRAY
:
839 return this->length
* this->fields
.array
->component_slots();
841 case GLSL_TYPE_SAMPLER
:
843 case GLSL_TYPE_ERROR
:
851 glsl_type::can_implicitly_convert_to(const glsl_type
*desired
) const
856 /* There is no conversion among matrix types. */
857 if (this->matrix_columns
> 1 || desired
->matrix_columns
> 1)
860 /* int and uint can be converted to float. */
861 return desired
->is_float()
862 && this->is_integer()
863 && this->vector_elements
== desired
->vector_elements
;
867 glsl_type::std140_base_alignment(bool row_major
) const
869 /* (1) If the member is a scalar consuming <N> basic machine units, the
870 * base alignment is <N>.
872 * (2) If the member is a two- or four-component vector with components
873 * consuming <N> basic machine units, the base alignment is 2<N> or
874 * 4<N>, respectively.
876 * (3) If the member is a three-component vector with components consuming
877 * <N> basic machine units, the base alignment is 4<N>.
879 if (this->is_scalar() || this->is_vector()) {
880 switch (this->vector_elements
) {
891 /* (4) If the member is an array of scalars or vectors, the base alignment
892 * and array stride are set to match the base alignment of a single
893 * array element, according to rules (1), (2), and (3), and rounded up
894 * to the base alignment of a vec4. The array may have padding at the
895 * end; the base offset of the member following the array is rounded up
896 * to the next multiple of the base alignment.
898 * (6) If the member is an array of <S> column-major matrices with <C>
899 * columns and <R> rows, the matrix is stored identically to a row of
900 * <S>*<C> column vectors with <R> components each, according to rule
903 * (8) If the member is an array of <S> row-major matrices with <C> columns
904 * and <R> rows, the matrix is stored identically to a row of <S>*<R>
905 * row vectors with <C> components each, according to rule (4).
907 * (10) If the member is an array of <S> structures, the <S> elements of
908 * the array are laid out in order, according to rule (9).
910 if (this->is_array()) {
911 if (this->fields
.array
->is_scalar() ||
912 this->fields
.array
->is_vector() ||
913 this->fields
.array
->is_matrix()) {
914 return MAX2(this->fields
.array
->std140_base_alignment(row_major
), 16);
916 assert(this->fields
.array
->is_record());
917 return this->fields
.array
->std140_base_alignment(row_major
);
921 /* (5) If the member is a column-major matrix with <C> columns and
922 * <R> rows, the matrix is stored identically to an array of
923 * <C> column vectors with <R> components each, according to
926 * (7) If the member is a row-major matrix with <C> columns and <R>
927 * rows, the matrix is stored identically to an array of <R>
928 * row vectors with <C> components each, according to rule (4).
930 if (this->is_matrix()) {
931 const struct glsl_type
*vec_type
, *array_type
;
932 int c
= this->matrix_columns
;
933 int r
= this->vector_elements
;
936 vec_type
= get_instance(GLSL_TYPE_FLOAT
, c
, 1);
937 array_type
= glsl_type::get_array_instance(vec_type
, r
);
939 vec_type
= get_instance(GLSL_TYPE_FLOAT
, r
, 1);
940 array_type
= glsl_type::get_array_instance(vec_type
, c
);
943 return array_type
->std140_base_alignment(false);
946 /* (9) If the member is a structure, the base alignment of the
947 * structure is <N>, where <N> is the largest base alignment
948 * value of any of its members, and rounded up to the base
949 * alignment of a vec4. The individual members of this
950 * sub-structure are then assigned offsets by applying this set
951 * of rules recursively, where the base offset of the first
952 * member of the sub-structure is equal to the aligned offset
953 * of the structure. The structure may have padding at the end;
954 * the base offset of the member following the sub-structure is
955 * rounded up to the next multiple of the base alignment of the
958 if (this->is_record()) {
959 unsigned base_alignment
= 16;
960 for (unsigned i
= 0; i
< this->length
; i
++) {
961 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
962 base_alignment
= MAX2(base_alignment
,
963 field_type
->std140_base_alignment(row_major
));
965 return base_alignment
;
968 assert(!"not reached");
973 glsl_type::std140_size(bool row_major
) const
975 /* (1) If the member is a scalar consuming <N> basic machine units, the
976 * base alignment is <N>.
978 * (2) If the member is a two- or four-component vector with components
979 * consuming <N> basic machine units, the base alignment is 2<N> or
980 * 4<N>, respectively.
982 * (3) If the member is a three-component vector with components consuming
983 * <N> basic machine units, the base alignment is 4<N>.
985 if (this->is_scalar() || this->is_vector()) {
986 return this->vector_elements
* 4;
989 /* (5) If the member is a column-major matrix with <C> columns and
990 * <R> rows, the matrix is stored identically to an array of
991 * <C> column vectors with <R> components each, according to
994 * (6) If the member is an array of <S> column-major matrices with <C>
995 * columns and <R> rows, the matrix is stored identically to a row of
996 * <S>*<C> column vectors with <R> components each, according to rule
999 * (7) If the member is a row-major matrix with <C> columns and <R>
1000 * rows, the matrix is stored identically to an array of <R>
1001 * row vectors with <C> components each, according to rule (4).
1003 * (8) If the member is an array of <S> row-major matrices with <C> columns
1004 * and <R> rows, the matrix is stored identically to a row of <S>*<R>
1005 * row vectors with <C> components each, according to rule (4).
1007 if (this->is_matrix() || (this->is_array() &&
1008 this->fields
.array
->is_matrix())) {
1009 const struct glsl_type
*element_type
;
1010 const struct glsl_type
*vec_type
;
1011 unsigned int array_len
;
1013 if (this->is_array()) {
1014 element_type
= this->fields
.array
;
1015 array_len
= this->length
;
1017 element_type
= this;
1022 vec_type
= get_instance(GLSL_TYPE_FLOAT
,
1023 element_type
->matrix_columns
, 1);
1024 array_len
*= element_type
->vector_elements
;
1026 vec_type
= get_instance(GLSL_TYPE_FLOAT
,
1027 element_type
->vector_elements
, 1);
1028 array_len
*= element_type
->matrix_columns
;
1030 const glsl_type
*array_type
= glsl_type::get_array_instance(vec_type
,
1033 return array_type
->std140_size(false);
1036 /* (4) If the member is an array of scalars or vectors, the base alignment
1037 * and array stride are set to match the base alignment of a single
1038 * array element, according to rules (1), (2), and (3), and rounded up
1039 * to the base alignment of a vec4. The array may have padding at the
1040 * end; the base offset of the member following the array is rounded up
1041 * to the next multiple of the base alignment.
1043 * (10) If the member is an array of <S> structures, the <S> elements of
1044 * the array are laid out in order, according to rule (9).
1046 if (this->is_array()) {
1047 if (this->fields
.array
->is_record()) {
1048 return this->length
* this->fields
.array
->std140_size(row_major
);
1050 unsigned element_base_align
=
1051 this->fields
.array
->std140_base_alignment(row_major
);
1052 return this->length
* MAX2(element_base_align
, 16);
1056 /* (9) If the member is a structure, the base alignment of the
1057 * structure is <N>, where <N> is the largest base alignment
1058 * value of any of its members, and rounded up to the base
1059 * alignment of a vec4. The individual members of this
1060 * sub-structure are then assigned offsets by applying this set
1061 * of rules recursively, where the base offset of the first
1062 * member of the sub-structure is equal to the aligned offset
1063 * of the structure. The structure may have padding at the end;
1064 * the base offset of the member following the sub-structure is
1065 * rounded up to the next multiple of the base alignment of the
1068 if (this->is_record()) {
1070 for (unsigned i
= 0; i
< this->length
; i
++) {
1071 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
1072 unsigned align
= field_type
->std140_base_alignment(row_major
);
1073 size
= glsl_align(size
, align
);
1074 size
+= field_type
->std140_size(row_major
);
1076 size
= glsl_align(size
,
1077 this->fields
.structure
[0].type
->std140_base_alignment(row_major
));
1081 assert(!"not reached");