2 * Copyright © 2009 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
25 #include "main/core.h" /* for Elements, MAX2 */
26 #include "glsl_parser_extras.h"
27 #include "glsl_types.h"
28 #include "util/hash_table.h"
31 mtx_t
glsl_type::mutex
= _MTX_INITIALIZER_NP
;
32 hash_table
*glsl_type::array_types
= NULL
;
33 hash_table
*glsl_type::record_types
= NULL
;
34 hash_table
*glsl_type::interface_types
= NULL
;
35 hash_table
*glsl_type::subroutine_types
= NULL
;
36 void *glsl_type::mem_ctx
= NULL
;
39 glsl_type::init_ralloc_type_ctx(void)
41 if (glsl_type::mem_ctx
== NULL
) {
42 glsl_type::mem_ctx
= ralloc_autofree_context();
43 assert(glsl_type::mem_ctx
!= NULL
);
47 glsl_type::glsl_type(GLenum gl_type
,
48 glsl_base_type base_type
, unsigned vector_elements
,
49 unsigned matrix_columns
, const char *name
) :
52 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
53 sampler_type(0), interface_packing(0),
54 vector_elements(vector_elements
), matrix_columns(matrix_columns
),
57 mtx_lock(&glsl_type::mutex
);
59 init_ralloc_type_ctx();
61 this->name
= ralloc_strdup(this->mem_ctx
, name
);
63 mtx_unlock(&glsl_type::mutex
);
65 /* Neither dimension is zero or both dimensions are zero.
67 assert((vector_elements
== 0) == (matrix_columns
== 0));
68 memset(& fields
, 0, sizeof(fields
));
71 glsl_type::glsl_type(GLenum gl_type
, glsl_base_type base_type
,
72 enum glsl_sampler_dim dim
, bool shadow
, bool array
,
73 unsigned type
, const char *name
) :
76 sampler_dimensionality(dim
), sampler_shadow(shadow
),
77 sampler_array(array
), sampler_type(type
), interface_packing(0),
80 mtx_lock(&glsl_type::mutex
);
82 init_ralloc_type_ctx();
84 this->name
= ralloc_strdup(this->mem_ctx
, name
);
86 mtx_unlock(&glsl_type::mutex
);
88 memset(& fields
, 0, sizeof(fields
));
90 if (base_type
== GLSL_TYPE_SAMPLER
) {
91 /* Samplers take no storage whatsoever. */
92 matrix_columns
= vector_elements
= 0;
94 matrix_columns
= vector_elements
= 1;
98 glsl_type::glsl_type(const glsl_struct_field
*fields
, unsigned num_fields
,
101 base_type(GLSL_TYPE_STRUCT
),
102 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
103 sampler_type(0), interface_packing(0),
104 vector_elements(0), matrix_columns(0),
109 mtx_lock(&glsl_type::mutex
);
111 init_ralloc_type_ctx();
112 assert(name
!= NULL
);
113 this->name
= ralloc_strdup(this->mem_ctx
, name
);
114 this->fields
.structure
= ralloc_array(this->mem_ctx
,
115 glsl_struct_field
, length
);
117 for (i
= 0; i
< length
; i
++) {
118 this->fields
.structure
[i
].type
= fields
[i
].type
;
119 this->fields
.structure
[i
].name
= ralloc_strdup(this->fields
.structure
,
121 this->fields
.structure
[i
].location
= fields
[i
].location
;
122 this->fields
.structure
[i
].interpolation
= fields
[i
].interpolation
;
123 this->fields
.structure
[i
].centroid
= fields
[i
].centroid
;
124 this->fields
.structure
[i
].sample
= fields
[i
].sample
;
125 this->fields
.structure
[i
].matrix_layout
= fields
[i
].matrix_layout
;
126 this->fields
.structure
[i
].patch
= fields
[i
].patch
;
127 this->fields
.structure
[i
].image_read_only
= fields
[i
].image_read_only
;
128 this->fields
.structure
[i
].image_write_only
= fields
[i
].image_write_only
;
129 this->fields
.structure
[i
].image_coherent
= fields
[i
].image_coherent
;
130 this->fields
.structure
[i
].image_volatile
= fields
[i
].image_volatile
;
131 this->fields
.structure
[i
].image_restrict
= fields
[i
].image_restrict
;
134 mtx_unlock(&glsl_type::mutex
);
137 glsl_type::glsl_type(const glsl_struct_field
*fields
, unsigned num_fields
,
138 enum glsl_interface_packing packing
, const char *name
) :
140 base_type(GLSL_TYPE_INTERFACE
),
141 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
142 sampler_type(0), interface_packing((unsigned) packing
),
143 vector_elements(0), matrix_columns(0),
148 mtx_lock(&glsl_type::mutex
);
150 init_ralloc_type_ctx();
151 assert(name
!= NULL
);
152 this->name
= ralloc_strdup(this->mem_ctx
, name
);
153 this->fields
.structure
= ralloc_array(this->mem_ctx
,
154 glsl_struct_field
, length
);
155 for (i
= 0; i
< length
; i
++) {
156 this->fields
.structure
[i
].type
= fields
[i
].type
;
157 this->fields
.structure
[i
].name
= ralloc_strdup(this->fields
.structure
,
159 this->fields
.structure
[i
].location
= fields
[i
].location
;
160 this->fields
.structure
[i
].interpolation
= fields
[i
].interpolation
;
161 this->fields
.structure
[i
].centroid
= fields
[i
].centroid
;
162 this->fields
.structure
[i
].sample
= fields
[i
].sample
;
163 this->fields
.structure
[i
].matrix_layout
= fields
[i
].matrix_layout
;
164 this->fields
.structure
[i
].patch
= fields
[i
].patch
;
167 mtx_unlock(&glsl_type::mutex
);
170 glsl_type::glsl_type(const char *subroutine_name
) :
172 base_type(GLSL_TYPE_SUBROUTINE
),
173 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
174 sampler_type(0), interface_packing(0),
175 vector_elements(0), matrix_columns(0),
178 mtx_lock(&glsl_type::mutex
);
180 init_ralloc_type_ctx();
181 assert(subroutine_name
!= NULL
);
182 this->name
= ralloc_strdup(this->mem_ctx
, subroutine_name
);
183 this->vector_elements
= 1;
184 mtx_unlock(&glsl_type::mutex
);
188 glsl_type::contains_sampler() const
190 if (this->is_array()) {
191 return this->fields
.array
->contains_sampler();
192 } else if (this->is_record()) {
193 for (unsigned int i
= 0; i
< this->length
; i
++) {
194 if (this->fields
.structure
[i
].type
->contains_sampler())
199 return this->is_sampler();
205 glsl_type::contains_integer() const
207 if (this->is_array()) {
208 return this->fields
.array
->contains_integer();
209 } else if (this->is_record()) {
210 for (unsigned int i
= 0; i
< this->length
; i
++) {
211 if (this->fields
.structure
[i
].type
->contains_integer())
216 return this->is_integer();
221 glsl_type::contains_double() const
223 if (this->is_array()) {
224 return this->fields
.array
->contains_double();
225 } else if (this->is_record()) {
226 for (unsigned int i
= 0; i
< this->length
; i
++) {
227 if (this->fields
.structure
[i
].type
->contains_double())
232 return this->is_double();
237 glsl_type::contains_opaque() const {
239 case GLSL_TYPE_SAMPLER
:
240 case GLSL_TYPE_IMAGE
:
241 case GLSL_TYPE_ATOMIC_UINT
:
243 case GLSL_TYPE_ARRAY
:
244 return fields
.array
->contains_opaque();
245 case GLSL_TYPE_STRUCT
:
246 for (unsigned int i
= 0; i
< length
; i
++) {
247 if (fields
.structure
[i
].type
->contains_opaque())
257 glsl_type::contains_subroutine() const
259 if (this->is_array()) {
260 return this->fields
.array
->contains_subroutine();
261 } else if (this->is_record()) {
262 for (unsigned int i
= 0; i
< this->length
; i
++) {
263 if (this->fields
.structure
[i
].type
->contains_subroutine())
268 return this->is_subroutine();
273 glsl_type::sampler_index() const
275 const glsl_type
*const t
= (this->is_array()) ? this->fields
.array
: this;
277 assert(t
->is_sampler());
279 switch (t
->sampler_dimensionality
) {
280 case GLSL_SAMPLER_DIM_1D
:
281 return (t
->sampler_array
) ? TEXTURE_1D_ARRAY_INDEX
: TEXTURE_1D_INDEX
;
282 case GLSL_SAMPLER_DIM_2D
:
283 return (t
->sampler_array
) ? TEXTURE_2D_ARRAY_INDEX
: TEXTURE_2D_INDEX
;
284 case GLSL_SAMPLER_DIM_3D
:
285 return TEXTURE_3D_INDEX
;
286 case GLSL_SAMPLER_DIM_CUBE
:
287 return (t
->sampler_array
) ? TEXTURE_CUBE_ARRAY_INDEX
: TEXTURE_CUBE_INDEX
;
288 case GLSL_SAMPLER_DIM_RECT
:
289 return TEXTURE_RECT_INDEX
;
290 case GLSL_SAMPLER_DIM_BUF
:
291 return TEXTURE_BUFFER_INDEX
;
292 case GLSL_SAMPLER_DIM_EXTERNAL
:
293 return TEXTURE_EXTERNAL_INDEX
;
294 case GLSL_SAMPLER_DIM_MS
:
295 return (t
->sampler_array
) ? TEXTURE_2D_MULTISAMPLE_ARRAY_INDEX
: TEXTURE_2D_MULTISAMPLE_INDEX
;
297 assert(!"Should not get here.");
298 return TEXTURE_BUFFER_INDEX
;
303 glsl_type::contains_image() const
305 if (this->is_array()) {
306 return this->fields
.array
->contains_image();
307 } else if (this->is_record()) {
308 for (unsigned int i
= 0; i
< this->length
; i
++) {
309 if (this->fields
.structure
[i
].type
->contains_image())
314 return this->is_image();
318 const glsl_type
*glsl_type::get_base_type() const
325 case GLSL_TYPE_FLOAT
:
327 case GLSL_TYPE_DOUBLE
:
337 const glsl_type
*glsl_type::get_scalar_type() const
339 const glsl_type
*type
= this;
342 while (type
->base_type
== GLSL_TYPE_ARRAY
)
343 type
= type
->fields
.array
;
345 /* Handle vectors and matrices */
346 switch (type
->base_type
) {
351 case GLSL_TYPE_FLOAT
:
353 case GLSL_TYPE_DOUBLE
:
358 /* Handle everything else */
365 _mesa_glsl_release_types(void)
367 /* Should only be called during atexit (either when unloading shared
368 * object, or if process terminates), so no mutex-locking should be
371 if (glsl_type::array_types
!= NULL
) {
372 _mesa_hash_table_destroy(glsl_type::array_types
, NULL
);
373 glsl_type::array_types
= NULL
;
376 if (glsl_type::record_types
!= NULL
) {
377 _mesa_hash_table_destroy(glsl_type::record_types
, NULL
);
378 glsl_type::record_types
= NULL
;
381 if (glsl_type::interface_types
!= NULL
) {
382 _mesa_hash_table_destroy(glsl_type::interface_types
, NULL
);
383 glsl_type::interface_types
= NULL
;
388 glsl_type::glsl_type(const glsl_type
*array
, unsigned length
) :
389 base_type(GLSL_TYPE_ARRAY
),
390 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
391 sampler_type(0), interface_packing(0),
392 vector_elements(0), matrix_columns(0),
393 length(length
), name(NULL
)
395 this->fields
.array
= array
;
396 /* Inherit the gl type of the base. The GL type is used for
397 * uniform/statevar handling in Mesa and the arrayness of the type
398 * is represented by the size rather than the type.
400 this->gl_type
= array
->gl_type
;
402 /* Allow a maximum of 10 characters for the array size. This is enough
403 * for 32-bits of ~0. The extra 3 are for the '[', ']', and terminating
406 const unsigned name_length
= strlen(array
->name
) + 10 + 3;
408 mtx_lock(&glsl_type::mutex
);
409 char *const n
= (char *) ralloc_size(this->mem_ctx
, name_length
);
410 mtx_unlock(&glsl_type::mutex
);
413 snprintf(n
, name_length
, "%s[]", array
->name
);
415 /* insert outermost dimensions in the correct spot
416 * otherwise the dimension order will be backwards
418 const char *pos
= strchr(array
->name
, '[');
420 int idx
= pos
- array
->name
;
421 snprintf(n
, idx
+1, "%s", array
->name
);
422 snprintf(n
+ idx
, name_length
- idx
, "[%u]%s",
423 length
, array
->name
+ idx
);
425 snprintf(n
, name_length
, "%s[%u]", array
->name
, length
);
434 glsl_type::vec(unsigned components
)
436 if (components
== 0 || components
> 4)
439 static const glsl_type
*const ts
[] = {
440 float_type
, vec2_type
, vec3_type
, vec4_type
442 return ts
[components
- 1];
446 glsl_type::dvec(unsigned components
)
448 if (components
== 0 || components
> 4)
451 static const glsl_type
*const ts
[] = {
452 double_type
, dvec2_type
, dvec3_type
, dvec4_type
454 return ts
[components
- 1];
458 glsl_type::ivec(unsigned components
)
460 if (components
== 0 || components
> 4)
463 static const glsl_type
*const ts
[] = {
464 int_type
, ivec2_type
, ivec3_type
, ivec4_type
466 return ts
[components
- 1];
471 glsl_type::uvec(unsigned components
)
473 if (components
== 0 || components
> 4)
476 static const glsl_type
*const ts
[] = {
477 uint_type
, uvec2_type
, uvec3_type
, uvec4_type
479 return ts
[components
- 1];
484 glsl_type::bvec(unsigned components
)
486 if (components
== 0 || components
> 4)
489 static const glsl_type
*const ts
[] = {
490 bool_type
, bvec2_type
, bvec3_type
, bvec4_type
492 return ts
[components
- 1];
497 glsl_type::get_instance(unsigned base_type
, unsigned rows
, unsigned columns
)
499 if (base_type
== GLSL_TYPE_VOID
)
502 if ((rows
< 1) || (rows
> 4) || (columns
< 1) || (columns
> 4))
505 /* Treat GLSL vectors as Nx1 matrices.
513 case GLSL_TYPE_FLOAT
:
515 case GLSL_TYPE_DOUBLE
:
523 if ((base_type
!= GLSL_TYPE_FLOAT
&& base_type
!= GLSL_TYPE_DOUBLE
) || (rows
== 1))
526 /* GLSL matrix types are named mat{COLUMNS}x{ROWS}. Only the following
527 * combinations are valid:
535 #define IDX(c,r) (((c-1)*3) + (r-1))
537 if (base_type
== GLSL_TYPE_DOUBLE
) {
538 switch (IDX(columns
, rows
)) {
539 case IDX(2,2): return dmat2_type
;
540 case IDX(2,3): return dmat2x3_type
;
541 case IDX(2,4): return dmat2x4_type
;
542 case IDX(3,2): return dmat3x2_type
;
543 case IDX(3,3): return dmat3_type
;
544 case IDX(3,4): return dmat3x4_type
;
545 case IDX(4,2): return dmat4x2_type
;
546 case IDX(4,3): return dmat4x3_type
;
547 case IDX(4,4): return dmat4_type
;
548 default: return error_type
;
551 switch (IDX(columns
, rows
)) {
552 case IDX(2,2): return mat2_type
;
553 case IDX(2,3): return mat2x3_type
;
554 case IDX(2,4): return mat2x4_type
;
555 case IDX(3,2): return mat3x2_type
;
556 case IDX(3,3): return mat3_type
;
557 case IDX(3,4): return mat3x4_type
;
558 case IDX(4,2): return mat4x2_type
;
559 case IDX(4,3): return mat4x3_type
;
560 case IDX(4,4): return mat4_type
;
561 default: return error_type
;
566 assert(!"Should not get here.");
571 glsl_type::get_sampler_instance(enum glsl_sampler_dim dim
,
577 case GLSL_TYPE_FLOAT
:
579 case GLSL_SAMPLER_DIM_1D
:
581 return (array
? sampler1DArrayShadow_type
: sampler1DShadow_type
);
583 return (array
? sampler1DArray_type
: sampler1D_type
);
584 case GLSL_SAMPLER_DIM_2D
:
586 return (array
? sampler2DArrayShadow_type
: sampler2DShadow_type
);
588 return (array
? sampler2DArray_type
: sampler2D_type
);
589 case GLSL_SAMPLER_DIM_3D
:
593 return sampler3D_type
;
594 case GLSL_SAMPLER_DIM_CUBE
:
596 return (array
? samplerCubeArrayShadow_type
: samplerCubeShadow_type
);
598 return (array
? samplerCubeArray_type
: samplerCube_type
);
599 case GLSL_SAMPLER_DIM_RECT
:
603 return sampler2DRectShadow_type
;
605 return sampler2DRect_type
;
606 case GLSL_SAMPLER_DIM_BUF
:
610 return samplerBuffer_type
;
611 case GLSL_SAMPLER_DIM_MS
:
614 return (array
? sampler2DMSArray_type
: sampler2DMS_type
);
615 case GLSL_SAMPLER_DIM_EXTERNAL
:
619 return samplerExternalOES_type
;
625 case GLSL_SAMPLER_DIM_1D
:
626 return (array
? isampler1DArray_type
: isampler1D_type
);
627 case GLSL_SAMPLER_DIM_2D
:
628 return (array
? isampler2DArray_type
: isampler2D_type
);
629 case GLSL_SAMPLER_DIM_3D
:
632 return isampler3D_type
;
633 case GLSL_SAMPLER_DIM_CUBE
:
634 return (array
? isamplerCubeArray_type
: isamplerCube_type
);
635 case GLSL_SAMPLER_DIM_RECT
:
638 return isampler2DRect_type
;
639 case GLSL_SAMPLER_DIM_BUF
:
642 return isamplerBuffer_type
;
643 case GLSL_SAMPLER_DIM_MS
:
644 return (array
? isampler2DMSArray_type
: isampler2DMS_type
);
645 case GLSL_SAMPLER_DIM_EXTERNAL
:
652 case GLSL_SAMPLER_DIM_1D
:
653 return (array
? usampler1DArray_type
: usampler1D_type
);
654 case GLSL_SAMPLER_DIM_2D
:
655 return (array
? usampler2DArray_type
: usampler2D_type
);
656 case GLSL_SAMPLER_DIM_3D
:
659 return usampler3D_type
;
660 case GLSL_SAMPLER_DIM_CUBE
:
661 return (array
? usamplerCubeArray_type
: usamplerCube_type
);
662 case GLSL_SAMPLER_DIM_RECT
:
665 return usampler2DRect_type
;
666 case GLSL_SAMPLER_DIM_BUF
:
669 return usamplerBuffer_type
;
670 case GLSL_SAMPLER_DIM_MS
:
671 return (array
? usampler2DMSArray_type
: usampler2DMS_type
);
672 case GLSL_SAMPLER_DIM_EXTERNAL
:
679 unreachable("switch statement above should be complete");
683 glsl_type::get_array_instance(const glsl_type
*base
, unsigned array_size
)
685 /* Generate a name using the base type pointer in the key. This is
686 * done because the name of the base type may not be unique across
687 * shaders. For example, two shaders may have different record types
691 snprintf(key
, sizeof(key
), "%p[%u]", (void *) base
, array_size
);
693 mtx_lock(&glsl_type::mutex
);
695 if (array_types
== NULL
) {
696 array_types
= _mesa_hash_table_create(NULL
, _mesa_key_hash_string
,
697 _mesa_key_string_equal
);
700 const struct hash_entry
*entry
= _mesa_hash_table_search(array_types
, key
);
702 mtx_unlock(&glsl_type::mutex
);
703 const glsl_type
*t
= new glsl_type(base
, array_size
);
704 mtx_lock(&glsl_type::mutex
);
706 entry
= _mesa_hash_table_insert(array_types
,
707 ralloc_strdup(mem_ctx
, key
),
711 assert(((glsl_type
*) entry
->data
)->base_type
== GLSL_TYPE_ARRAY
);
712 assert(((glsl_type
*) entry
->data
)->length
== array_size
);
713 assert(((glsl_type
*) entry
->data
)->fields
.array
== base
);
715 mtx_unlock(&glsl_type::mutex
);
717 return (glsl_type
*) entry
->data
;
722 glsl_type::record_compare(const glsl_type
*b
) const
724 if (this->length
!= b
->length
)
727 if (this->interface_packing
!= b
->interface_packing
)
730 /* From the GLSL 4.20 specification (Sec 4.2):
732 * "Structures must have the same name, sequence of type names, and
733 * type definitions, and field names to be considered the same type."
735 * GLSL ES behaves the same (Ver 1.00 Sec 4.2.4, Ver 3.00 Sec 4.2.5).
737 * Note that we cannot force type name check when comparing unnamed
738 * structure types, these have a unique name assigned during parsing.
740 if (!this->is_anonymous() && !b
->is_anonymous())
741 if (strcmp(this->name
, b
->name
) != 0)
744 for (unsigned i
= 0; i
< this->length
; i
++) {
745 if (this->fields
.structure
[i
].type
!= b
->fields
.structure
[i
].type
)
747 if (strcmp(this->fields
.structure
[i
].name
,
748 b
->fields
.structure
[i
].name
) != 0)
750 if (this->fields
.structure
[i
].matrix_layout
751 != b
->fields
.structure
[i
].matrix_layout
)
753 if (this->fields
.structure
[i
].location
754 != b
->fields
.structure
[i
].location
)
756 if (this->fields
.structure
[i
].interpolation
757 != b
->fields
.structure
[i
].interpolation
)
759 if (this->fields
.structure
[i
].centroid
760 != b
->fields
.structure
[i
].centroid
)
762 if (this->fields
.structure
[i
].sample
763 != b
->fields
.structure
[i
].sample
)
765 if (this->fields
.structure
[i
].patch
766 != b
->fields
.structure
[i
].patch
)
768 if (this->fields
.structure
[i
].image_read_only
769 != b
->fields
.structure
[i
].image_read_only
)
771 if (this->fields
.structure
[i
].image_write_only
772 != b
->fields
.structure
[i
].image_write_only
)
774 if (this->fields
.structure
[i
].image_coherent
775 != b
->fields
.structure
[i
].image_coherent
)
777 if (this->fields
.structure
[i
].image_volatile
778 != b
->fields
.structure
[i
].image_volatile
)
780 if (this->fields
.structure
[i
].image_restrict
781 != b
->fields
.structure
[i
].image_restrict
)
790 glsl_type::record_key_compare(const void *a
, const void *b
)
792 const glsl_type
*const key1
= (glsl_type
*) a
;
793 const glsl_type
*const key2
= (glsl_type
*) b
;
795 return strcmp(key1
->name
, key2
->name
) == 0 && key1
->record_compare(key2
);
800 * Generate an integer hash value for a glsl_type structure type.
803 glsl_type::record_key_hash(const void *a
)
805 const glsl_type
*const key
= (glsl_type
*) a
;
806 uintptr_t hash
= key
->length
;
809 for (unsigned i
= 0; i
< key
->length
; i
++) {
810 /* casting pointer to uintptr_t */
811 hash
= (hash
* 13 ) + (uintptr_t) key
->fields
.structure
[i
].type
;
814 if (sizeof(hash
) == 8)
815 retval
= (hash
& 0xffffffff) ^ ((uint64_t) hash
>> 32);
824 glsl_type::get_record_instance(const glsl_struct_field
*fields
,
828 const glsl_type
key(fields
, num_fields
, name
);
830 mtx_lock(&glsl_type::mutex
);
832 if (record_types
== NULL
) {
833 record_types
= _mesa_hash_table_create(NULL
, record_key_hash
,
837 const struct hash_entry
*entry
= _mesa_hash_table_search(record_types
,
840 mtx_unlock(&glsl_type::mutex
);
841 const glsl_type
*t
= new glsl_type(fields
, num_fields
, name
);
842 mtx_lock(&glsl_type::mutex
);
844 entry
= _mesa_hash_table_insert(record_types
, t
, (void *) t
);
847 assert(((glsl_type
*) entry
->data
)->base_type
== GLSL_TYPE_STRUCT
);
848 assert(((glsl_type
*) entry
->data
)->length
== num_fields
);
849 assert(strcmp(((glsl_type
*) entry
->data
)->name
, name
) == 0);
851 mtx_unlock(&glsl_type::mutex
);
853 return (glsl_type
*) entry
->data
;
858 glsl_type::get_interface_instance(const glsl_struct_field
*fields
,
860 enum glsl_interface_packing packing
,
861 const char *block_name
)
863 const glsl_type
key(fields
, num_fields
, packing
, block_name
);
865 mtx_lock(&glsl_type::mutex
);
867 if (interface_types
== NULL
) {
868 interface_types
= _mesa_hash_table_create(NULL
, record_key_hash
,
872 const struct hash_entry
*entry
= _mesa_hash_table_search(interface_types
,
875 mtx_unlock(&glsl_type::mutex
);
876 const glsl_type
*t
= new glsl_type(fields
, num_fields
,
877 packing
, block_name
);
878 mtx_lock(&glsl_type::mutex
);
880 entry
= _mesa_hash_table_insert(interface_types
, t
, (void *) t
);
883 assert(((glsl_type
*) entry
->data
)->base_type
== GLSL_TYPE_INTERFACE
);
884 assert(((glsl_type
*) entry
->data
)->length
== num_fields
);
885 assert(strcmp(((glsl_type
*) entry
->data
)->name
, block_name
) == 0);
887 mtx_unlock(&glsl_type::mutex
);
889 return (glsl_type
*) entry
->data
;
893 glsl_type::get_subroutine_instance(const char *subroutine_name
)
895 const glsl_type
key(subroutine_name
);
897 mtx_lock(&glsl_type::mutex
);
899 if (subroutine_types
== NULL
) {
900 subroutine_types
= _mesa_hash_table_create(NULL
, record_key_hash
,
904 const struct hash_entry
*entry
= _mesa_hash_table_search(subroutine_types
,
907 mtx_unlock(&glsl_type::mutex
);
908 const glsl_type
*t
= new glsl_type(subroutine_name
);
909 mtx_lock(&glsl_type::mutex
);
911 entry
= _mesa_hash_table_insert(subroutine_types
, t
, (void *) t
);
914 assert(((glsl_type
*) entry
->data
)->base_type
== GLSL_TYPE_SUBROUTINE
);
915 assert(strcmp(((glsl_type
*) entry
->data
)->name
, subroutine_name
) == 0);
917 mtx_unlock(&glsl_type::mutex
);
919 return (glsl_type
*) entry
->data
;
924 glsl_type::get_mul_type(const glsl_type
*type_a
, const glsl_type
*type_b
)
926 if (type_a
== type_b
) {
928 } else if (type_a
->is_matrix() && type_b
->is_matrix()) {
929 /* Matrix multiply. The columns of A must match the rows of B. Given
930 * the other previously tested constraints, this means the vector type
931 * of a row from A must be the same as the vector type of a column from
934 if (type_a
->row_type() == type_b
->column_type()) {
935 /* The resulting matrix has the number of columns of matrix B and
936 * the number of rows of matrix A. We get the row count of A by
937 * looking at the size of a vector that makes up a column. The
938 * transpose (size of a row) is done for B.
940 const glsl_type
*const type
=
941 get_instance(type_a
->base_type
,
942 type_a
->column_type()->vector_elements
,
943 type_b
->row_type()->vector_elements
);
944 assert(type
!= error_type
);
948 } else if (type_a
->is_matrix()) {
949 /* A is a matrix and B is a column vector. Columns of A must match
950 * rows of B. Given the other previously tested constraints, this
951 * means the vector type of a row from A must be the same as the
952 * vector the type of B.
954 if (type_a
->row_type() == type_b
) {
955 /* The resulting vector has a number of elements equal to
956 * the number of rows of matrix A. */
957 const glsl_type
*const type
=
958 get_instance(type_a
->base_type
,
959 type_a
->column_type()->vector_elements
,
961 assert(type
!= error_type
);
966 assert(type_b
->is_matrix());
968 /* A is a row vector and B is a matrix. Columns of A must match rows
969 * of B. Given the other previously tested constraints, this means
970 * the type of A must be the same as the vector type of a column from
973 if (type_a
== type_b
->column_type()) {
974 /* The resulting vector has a number of elements equal to
975 * the number of columns of matrix B. */
976 const glsl_type
*const type
=
977 get_instance(type_a
->base_type
,
978 type_b
->row_type()->vector_elements
,
980 assert(type
!= error_type
);
991 glsl_type::field_type(const char *name
) const
993 if (this->base_type
!= GLSL_TYPE_STRUCT
994 && this->base_type
!= GLSL_TYPE_INTERFACE
)
997 for (unsigned i
= 0; i
< this->length
; i
++) {
998 if (strcmp(name
, this->fields
.structure
[i
].name
) == 0)
999 return this->fields
.structure
[i
].type
;
1007 glsl_type::field_index(const char *name
) const
1009 if (this->base_type
!= GLSL_TYPE_STRUCT
1010 && this->base_type
!= GLSL_TYPE_INTERFACE
)
1013 for (unsigned i
= 0; i
< this->length
; i
++) {
1014 if (strcmp(name
, this->fields
.structure
[i
].name
) == 0)
1023 glsl_type::component_slots() const
1025 switch (this->base_type
) {
1026 case GLSL_TYPE_UINT
:
1028 case GLSL_TYPE_FLOAT
:
1029 case GLSL_TYPE_BOOL
:
1030 return this->components();
1032 case GLSL_TYPE_DOUBLE
:
1033 return 2 * this->components();
1035 case GLSL_TYPE_STRUCT
:
1036 case GLSL_TYPE_INTERFACE
: {
1039 for (unsigned i
= 0; i
< this->length
; i
++)
1040 size
+= this->fields
.structure
[i
].type
->component_slots();
1045 case GLSL_TYPE_ARRAY
:
1046 return this->length
* this->fields
.array
->component_slots();
1048 case GLSL_TYPE_IMAGE
:
1050 case GLSL_TYPE_SUBROUTINE
:
1052 case GLSL_TYPE_SAMPLER
:
1053 case GLSL_TYPE_ATOMIC_UINT
:
1054 case GLSL_TYPE_VOID
:
1055 case GLSL_TYPE_ERROR
:
1063 glsl_type::record_location_offset(unsigned length
) const
1065 unsigned offset
= 0;
1066 const glsl_type
*t
= this->without_array();
1067 if (t
->is_record()) {
1068 assert(length
<= t
->length
);
1070 for (unsigned i
= 0; i
< length
; i
++) {
1071 const glsl_type
*st
= t
->fields
.structure
[i
].type
;
1072 const glsl_type
*wa
= st
->without_array();
1073 if (wa
->is_record()) {
1074 unsigned r_offset
= wa
->record_location_offset(wa
->length
);
1075 offset
+= st
->is_array() ? st
->length
* r_offset
: r_offset
;
1077 /* We dont worry about arrays here because unless the array
1078 * contains a structure or another array it only takes up a single
1089 glsl_type::uniform_locations() const
1093 switch (this->base_type
) {
1094 case GLSL_TYPE_UINT
:
1096 case GLSL_TYPE_FLOAT
:
1097 case GLSL_TYPE_DOUBLE
:
1098 case GLSL_TYPE_BOOL
:
1099 case GLSL_TYPE_SAMPLER
:
1100 case GLSL_TYPE_IMAGE
:
1101 case GLSL_TYPE_SUBROUTINE
:
1104 case GLSL_TYPE_STRUCT
:
1105 case GLSL_TYPE_INTERFACE
:
1106 for (unsigned i
= 0; i
< this->length
; i
++)
1107 size
+= this->fields
.structure
[i
].type
->uniform_locations();
1109 case GLSL_TYPE_ARRAY
:
1110 return this->length
* this->fields
.array
->uniform_locations();
1117 glsl_type::can_implicitly_convert_to(const glsl_type
*desired
,
1118 _mesa_glsl_parse_state
*state
) const
1120 if (this == desired
)
1123 /* There is no conversion among matrix types. */
1124 if (this->matrix_columns
> 1 || desired
->matrix_columns
> 1)
1127 /* Vector size must match. */
1128 if (this->vector_elements
!= desired
->vector_elements
)
1131 /* int and uint can be converted to float. */
1132 if (desired
->is_float() && this->is_integer())
1135 /* With GLSL 4.0 / ARB_gpu_shader5, int can be converted to uint.
1136 * Note that state may be NULL here, when resolving function calls in the
1137 * linker. By this time, all the state-dependent checks have already
1138 * happened though, so allow anything that's allowed in any shader version. */
1139 if ((!state
|| state
->is_version(400, 0) || state
->ARB_gpu_shader5_enable
) &&
1140 desired
->base_type
== GLSL_TYPE_UINT
&& this->base_type
== GLSL_TYPE_INT
)
1143 /* No implicit conversions from double. */
1144 if ((!state
|| state
->has_double()) && this->is_double())
1147 /* Conversions from different types to double. */
1148 if ((!state
|| state
->has_double()) && desired
->is_double()) {
1149 if (this->is_float())
1151 if (this->is_integer())
1159 glsl_type::std140_base_alignment(bool row_major
) const
1161 unsigned N
= is_double() ? 8 : 4;
1163 /* (1) If the member is a scalar consuming <N> basic machine units, the
1164 * base alignment is <N>.
1166 * (2) If the member is a two- or four-component vector with components
1167 * consuming <N> basic machine units, the base alignment is 2<N> or
1168 * 4<N>, respectively.
1170 * (3) If the member is a three-component vector with components consuming
1171 * <N> basic machine units, the base alignment is 4<N>.
1173 if (this->is_scalar() || this->is_vector()) {
1174 switch (this->vector_elements
) {
1185 /* (4) If the member is an array of scalars or vectors, the base alignment
1186 * and array stride are set to match the base alignment of a single
1187 * array element, according to rules (1), (2), and (3), and rounded up
1188 * to the base alignment of a vec4. The array may have padding at the
1189 * end; the base offset of the member following the array is rounded up
1190 * to the next multiple of the base alignment.
1192 * (6) If the member is an array of <S> column-major matrices with <C>
1193 * columns and <R> rows, the matrix is stored identically to a row of
1194 * <S>*<C> column vectors with <R> components each, according to rule
1197 * (8) If the member is an array of <S> row-major matrices with <C> columns
1198 * and <R> rows, the matrix is stored identically to a row of <S>*<R>
1199 * row vectors with <C> components each, according to rule (4).
1201 * (10) If the member is an array of <S> structures, the <S> elements of
1202 * the array are laid out in order, according to rule (9).
1204 if (this->is_array()) {
1205 if (this->fields
.array
->is_scalar() ||
1206 this->fields
.array
->is_vector() ||
1207 this->fields
.array
->is_matrix()) {
1208 return MAX2(this->fields
.array
->std140_base_alignment(row_major
), 16);
1210 assert(this->fields
.array
->is_record() ||
1211 this->fields
.array
->is_array());
1212 return this->fields
.array
->std140_base_alignment(row_major
);
1216 /* (5) If the member is a column-major matrix with <C> columns and
1217 * <R> rows, the matrix is stored identically to an array of
1218 * <C> column vectors with <R> components each, according to
1221 * (7) If the member is a row-major matrix with <C> columns and <R>
1222 * rows, the matrix is stored identically to an array of <R>
1223 * row vectors with <C> components each, according to rule (4).
1225 if (this->is_matrix()) {
1226 const struct glsl_type
*vec_type
, *array_type
;
1227 int c
= this->matrix_columns
;
1228 int r
= this->vector_elements
;
1231 vec_type
= get_instance(base_type
, c
, 1);
1232 array_type
= glsl_type::get_array_instance(vec_type
, r
);
1234 vec_type
= get_instance(base_type
, r
, 1);
1235 array_type
= glsl_type::get_array_instance(vec_type
, c
);
1238 return array_type
->std140_base_alignment(false);
1241 /* (9) If the member is a structure, the base alignment of the
1242 * structure is <N>, where <N> is the largest base alignment
1243 * value of any of its members, and rounded up to the base
1244 * alignment of a vec4. The individual members of this
1245 * sub-structure are then assigned offsets by applying this set
1246 * of rules recursively, where the base offset of the first
1247 * member of the sub-structure is equal to the aligned offset
1248 * of the structure. The structure may have padding at the end;
1249 * the base offset of the member following the sub-structure is
1250 * rounded up to the next multiple of the base alignment of the
1253 if (this->is_record()) {
1254 unsigned base_alignment
= 16;
1255 for (unsigned i
= 0; i
< this->length
; i
++) {
1256 bool field_row_major
= row_major
;
1257 const enum glsl_matrix_layout matrix_layout
=
1258 glsl_matrix_layout(this->fields
.structure
[i
].matrix_layout
);
1259 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
1260 field_row_major
= true;
1261 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
1262 field_row_major
= false;
1265 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
1266 base_alignment
= MAX2(base_alignment
,
1267 field_type
->std140_base_alignment(field_row_major
));
1269 return base_alignment
;
1272 assert(!"not reached");
1277 glsl_type::std140_size(bool row_major
) const
1279 unsigned N
= is_double() ? 8 : 4;
1281 /* (1) If the member is a scalar consuming <N> basic machine units, the
1282 * base alignment is <N>.
1284 * (2) If the member is a two- or four-component vector with components
1285 * consuming <N> basic machine units, the base alignment is 2<N> or
1286 * 4<N>, respectively.
1288 * (3) If the member is a three-component vector with components consuming
1289 * <N> basic machine units, the base alignment is 4<N>.
1291 if (this->is_scalar() || this->is_vector()) {
1292 return this->vector_elements
* N
;
1295 /* (5) If the member is a column-major matrix with <C> columns and
1296 * <R> rows, the matrix is stored identically to an array of
1297 * <C> column vectors with <R> components each, according to
1300 * (6) If the member is an array of <S> column-major matrices with <C>
1301 * columns and <R> rows, the matrix is stored identically to a row of
1302 * <S>*<C> column vectors with <R> components each, according to rule
1305 * (7) If the member is a row-major matrix with <C> columns and <R>
1306 * rows, the matrix is stored identically to an array of <R>
1307 * row vectors with <C> components each, according to rule (4).
1309 * (8) If the member is an array of <S> row-major matrices with <C> columns
1310 * and <R> rows, the matrix is stored identically to a row of <S>*<R>
1311 * row vectors with <C> components each, according to rule (4).
1313 if (this->without_array()->is_matrix()) {
1314 const struct glsl_type
*element_type
;
1315 const struct glsl_type
*vec_type
;
1316 unsigned int array_len
;
1318 if (this->is_array()) {
1319 element_type
= this->fields
.array
;
1320 array_len
= this->length
;
1322 element_type
= this;
1327 vec_type
= get_instance(element_type
->base_type
,
1328 element_type
->matrix_columns
, 1);
1330 array_len
*= element_type
->vector_elements
;
1332 vec_type
= get_instance(element_type
->base_type
,
1333 element_type
->vector_elements
, 1);
1334 array_len
*= element_type
->matrix_columns
;
1336 const glsl_type
*array_type
= glsl_type::get_array_instance(vec_type
,
1339 return array_type
->std140_size(false);
1342 /* (4) If the member is an array of scalars or vectors, the base alignment
1343 * and array stride are set to match the base alignment of a single
1344 * array element, according to rules (1), (2), and (3), and rounded up
1345 * to the base alignment of a vec4. The array may have padding at the
1346 * end; the base offset of the member following the array is rounded up
1347 * to the next multiple of the base alignment.
1349 * (10) If the member is an array of <S> structures, the <S> elements of
1350 * the array are laid out in order, according to rule (9).
1352 if (this->is_array()) {
1353 if (this->fields
.array
->is_record()) {
1354 return this->length
* this->fields
.array
->std140_size(row_major
);
1356 unsigned element_base_align
=
1357 this->fields
.array
->std140_base_alignment(row_major
);
1358 return this->length
* MAX2(element_base_align
, 16);
1362 /* (9) If the member is a structure, the base alignment of the
1363 * structure is <N>, where <N> is the largest base alignment
1364 * value of any of its members, and rounded up to the base
1365 * alignment of a vec4. The individual members of this
1366 * sub-structure are then assigned offsets by applying this set
1367 * of rules recursively, where the base offset of the first
1368 * member of the sub-structure is equal to the aligned offset
1369 * of the structure. The structure may have padding at the end;
1370 * the base offset of the member following the sub-structure is
1371 * rounded up to the next multiple of the base alignment of the
1374 if (this->is_record() || this->is_interface()) {
1376 unsigned max_align
= 0;
1378 for (unsigned i
= 0; i
< this->length
; i
++) {
1379 bool field_row_major
= row_major
;
1380 const enum glsl_matrix_layout matrix_layout
=
1381 glsl_matrix_layout(this->fields
.structure
[i
].matrix_layout
);
1382 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
1383 field_row_major
= true;
1384 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
1385 field_row_major
= false;
1388 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
1389 unsigned align
= field_type
->std140_base_alignment(field_row_major
);
1391 /* Ignore unsized arrays when calculating size */
1392 if (field_type
->is_unsized_array())
1395 size
= glsl_align(size
, align
);
1396 size
+= field_type
->std140_size(field_row_major
);
1398 max_align
= MAX2(align
, max_align
);
1400 if (field_type
->is_record() && (i
+ 1 < this->length
))
1401 size
= glsl_align(size
, 16);
1403 size
= glsl_align(size
, MAX2(max_align
, 16));
1407 assert(!"not reached");
1412 glsl_type::std430_base_alignment(bool row_major
) const
1415 unsigned N
= is_double() ? 8 : 4;
1417 /* (1) If the member is a scalar consuming <N> basic machine units, the
1418 * base alignment is <N>.
1420 * (2) If the member is a two- or four-component vector with components
1421 * consuming <N> basic machine units, the base alignment is 2<N> or
1422 * 4<N>, respectively.
1424 * (3) If the member is a three-component vector with components consuming
1425 * <N> basic machine units, the base alignment is 4<N>.
1427 if (this->is_scalar() || this->is_vector()) {
1428 switch (this->vector_elements
) {
1439 /* OpenGL 4.30 spec, section 7.6.2.2 "Standard Uniform Block Layout":
1441 * "When using the std430 storage layout, shader storage blocks will be
1442 * laid out in buffer storage identically to uniform and shader storage
1443 * blocks using the std140 layout, except that the base alignment and
1444 * stride of arrays of scalars and vectors in rule 4 and of structures
1445 * in rule 9 are not rounded up a multiple of the base alignment of a vec4.
1448 /* (1) If the member is a scalar consuming <N> basic machine units, the
1449 * base alignment is <N>.
1451 * (2) If the member is a two- or four-component vector with components
1452 * consuming <N> basic machine units, the base alignment is 2<N> or
1453 * 4<N>, respectively.
1455 * (3) If the member is a three-component vector with components consuming
1456 * <N> basic machine units, the base alignment is 4<N>.
1458 if (this->is_array())
1459 return this->fields
.array
->std430_base_alignment(row_major
);
1461 /* (5) If the member is a column-major matrix with <C> columns and
1462 * <R> rows, the matrix is stored identically to an array of
1463 * <C> column vectors with <R> components each, according to
1466 * (7) If the member is a row-major matrix with <C> columns and <R>
1467 * rows, the matrix is stored identically to an array of <R>
1468 * row vectors with <C> components each, according to rule (4).
1470 if (this->is_matrix()) {
1471 const struct glsl_type
*vec_type
, *array_type
;
1472 int c
= this->matrix_columns
;
1473 int r
= this->vector_elements
;
1476 vec_type
= get_instance(base_type
, c
, 1);
1477 array_type
= glsl_type::get_array_instance(vec_type
, r
);
1479 vec_type
= get_instance(base_type
, r
, 1);
1480 array_type
= glsl_type::get_array_instance(vec_type
, c
);
1483 return array_type
->std430_base_alignment(false);
1486 /* (9) If the member is a structure, the base alignment of the
1487 * structure is <N>, where <N> is the largest base alignment
1488 * value of any of its members, and rounded up to the base
1489 * alignment of a vec4. The individual members of this
1490 * sub-structure are then assigned offsets by applying this set
1491 * of rules recursively, where the base offset of the first
1492 * member of the sub-structure is equal to the aligned offset
1493 * of the structure. The structure may have padding at the end;
1494 * the base offset of the member following the sub-structure is
1495 * rounded up to the next multiple of the base alignment of the
1498 if (this->is_record()) {
1499 unsigned base_alignment
= 0;
1500 for (unsigned i
= 0; i
< this->length
; i
++) {
1501 bool field_row_major
= row_major
;
1502 const enum glsl_matrix_layout matrix_layout
=
1503 glsl_matrix_layout(this->fields
.structure
[i
].matrix_layout
);
1504 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
1505 field_row_major
= true;
1506 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
1507 field_row_major
= false;
1510 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
1511 base_alignment
= MAX2(base_alignment
,
1512 field_type
->std430_base_alignment(field_row_major
));
1514 assert(base_alignment
> 0);
1515 return base_alignment
;
1517 assert(!"not reached");
1522 glsl_type::std430_array_stride(bool row_major
) const
1524 unsigned N
= is_double() ? 8 : 4;
1526 /* Notice that the array stride of a vec3 is not 3 * N but 4 * N.
1527 * See OpenGL 4.30 spec, section 7.6.2.2 "Standard Uniform Block Layout"
1529 * (3) If the member is a three-component vector with components consuming
1530 * <N> basic machine units, the base alignment is 4<N>.
1532 if (this->is_vector() && this->vector_elements
== 3)
1535 /* By default use std430_size(row_major) */
1536 return this->std430_size(row_major
);
1540 glsl_type::std430_size(bool row_major
) const
1542 unsigned N
= is_double() ? 8 : 4;
1544 /* OpenGL 4.30 spec, section 7.6.2.2 "Standard Uniform Block Layout":
1546 * "When using the std430 storage layout, shader storage blocks will be
1547 * laid out in buffer storage identically to uniform and shader storage
1548 * blocks using the std140 layout, except that the base alignment and
1549 * stride of arrays of scalars and vectors in rule 4 and of structures
1550 * in rule 9 are not rounded up a multiple of the base alignment of a vec4.
1552 if (this->is_scalar() || this->is_vector())
1553 return this->vector_elements
* N
;
1555 if (this->without_array()->is_matrix()) {
1556 const struct glsl_type
*element_type
;
1557 const struct glsl_type
*vec_type
;
1558 unsigned int array_len
;
1560 if (this->is_array()) {
1561 element_type
= this->fields
.array
;
1562 array_len
= this->length
;
1564 element_type
= this;
1569 vec_type
= get_instance(element_type
->base_type
,
1570 element_type
->matrix_columns
, 1);
1572 array_len
*= element_type
->vector_elements
;
1574 vec_type
= get_instance(element_type
->base_type
,
1575 element_type
->vector_elements
, 1);
1576 array_len
*= element_type
->matrix_columns
;
1578 const glsl_type
*array_type
= glsl_type::get_array_instance(vec_type
,
1581 return array_type
->std430_size(false);
1584 if (this->is_array()) {
1585 if (this->fields
.array
->is_record())
1586 return this->length
* this->fields
.array
->std430_size(row_major
);
1588 return this->length
* this->fields
.array
->std430_base_alignment(row_major
);
1591 if (this->is_record() || this->is_interface()) {
1593 unsigned max_align
= 0;
1595 for (unsigned i
= 0; i
< this->length
; i
++) {
1596 bool field_row_major
= row_major
;
1597 const enum glsl_matrix_layout matrix_layout
=
1598 glsl_matrix_layout(this->fields
.structure
[i
].matrix_layout
);
1599 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
1600 field_row_major
= true;
1601 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
1602 field_row_major
= false;
1605 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
1606 unsigned align
= field_type
->std430_base_alignment(field_row_major
);
1607 size
= glsl_align(size
, align
);
1608 size
+= field_type
->std430_size(field_row_major
);
1610 max_align
= MAX2(align
, max_align
);
1612 size
= glsl_align(size
, max_align
);
1616 assert(!"not reached");
1621 glsl_type::count_attribute_slots() const
1623 /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
1625 * "A scalar input counts the same amount against this limit as a vec4,
1626 * so applications may want to consider packing groups of four
1627 * unrelated float inputs together into a vector to better utilize the
1628 * capabilities of the underlying hardware. A matrix input will use up
1629 * multiple locations. The number of locations used will equal the
1630 * number of columns in the matrix."
1632 * The spec does not explicitly say how arrays are counted. However, it
1633 * should be safe to assume the total number of slots consumed by an array
1634 * is the number of entries in the array multiplied by the number of slots
1635 * consumed by a single element of the array.
1637 * The spec says nothing about how structs are counted, because vertex
1638 * attributes are not allowed to be (or contain) structs. However, Mesa
1639 * allows varying structs, the number of varying slots taken up by a
1640 * varying struct is simply equal to the sum of the number of slots taken
1641 * up by each element.
1643 switch (this->base_type
) {
1644 case GLSL_TYPE_UINT
:
1646 case GLSL_TYPE_FLOAT
:
1647 case GLSL_TYPE_BOOL
:
1648 case GLSL_TYPE_DOUBLE
:
1649 return this->matrix_columns
;
1651 case GLSL_TYPE_STRUCT
:
1652 case GLSL_TYPE_INTERFACE
: {
1655 for (unsigned i
= 0; i
< this->length
; i
++)
1656 size
+= this->fields
.structure
[i
].type
->count_attribute_slots();
1661 case GLSL_TYPE_ARRAY
:
1662 return this->length
* this->fields
.array
->count_attribute_slots();
1664 case GLSL_TYPE_SAMPLER
:
1665 case GLSL_TYPE_IMAGE
:
1666 case GLSL_TYPE_ATOMIC_UINT
:
1667 case GLSL_TYPE_VOID
:
1668 case GLSL_TYPE_SUBROUTINE
:
1669 case GLSL_TYPE_ERROR
:
1673 assert(!"Unexpected type in count_attribute_slots()");
1679 glsl_type::coordinate_components() const
1683 switch (sampler_dimensionality
) {
1684 case GLSL_SAMPLER_DIM_1D
:
1685 case GLSL_SAMPLER_DIM_BUF
:
1688 case GLSL_SAMPLER_DIM_2D
:
1689 case GLSL_SAMPLER_DIM_RECT
:
1690 case GLSL_SAMPLER_DIM_MS
:
1691 case GLSL_SAMPLER_DIM_EXTERNAL
:
1694 case GLSL_SAMPLER_DIM_3D
:
1695 case GLSL_SAMPLER_DIM_CUBE
:
1699 assert(!"Should not get here.");
1704 /* Array textures need an additional component for the array index, except
1705 * for cubemap array images that behave like a 2D array of interleaved
1708 if (sampler_array
&&
1709 !(base_type
== GLSL_TYPE_IMAGE
&&
1710 sampler_dimensionality
== GLSL_SAMPLER_DIM_CUBE
))