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(1), matrix_columns(1),
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 mtx_unlock(&glsl_type::mutex
);
187 glsl_type::contains_sampler() const
189 if (this->is_array()) {
190 return this->fields
.array
->contains_sampler();
191 } else if (this->is_record()) {
192 for (unsigned int i
= 0; i
< this->length
; i
++) {
193 if (this->fields
.structure
[i
].type
->contains_sampler())
198 return this->is_sampler();
204 glsl_type::contains_integer() const
206 if (this->is_array()) {
207 return this->fields
.array
->contains_integer();
208 } else if (this->is_record()) {
209 for (unsigned int i
= 0; i
< this->length
; i
++) {
210 if (this->fields
.structure
[i
].type
->contains_integer())
215 return this->is_integer();
220 glsl_type::contains_double() const
222 if (this->is_array()) {
223 return this->fields
.array
->contains_double();
224 } else if (this->is_record()) {
225 for (unsigned int i
= 0; i
< this->length
; i
++) {
226 if (this->fields
.structure
[i
].type
->contains_double())
231 return this->is_double();
236 glsl_type::contains_opaque() const {
238 case GLSL_TYPE_SAMPLER
:
239 case GLSL_TYPE_IMAGE
:
240 case GLSL_TYPE_ATOMIC_UINT
:
242 case GLSL_TYPE_ARRAY
:
243 return fields
.array
->contains_opaque();
244 case GLSL_TYPE_STRUCT
:
245 for (unsigned int i
= 0; i
< length
; i
++) {
246 if (fields
.structure
[i
].type
->contains_opaque())
256 glsl_type::contains_subroutine() const
258 if (this->is_array()) {
259 return this->fields
.array
->contains_subroutine();
260 } else if (this->is_record()) {
261 for (unsigned int i
= 0; i
< this->length
; i
++) {
262 if (this->fields
.structure
[i
].type
->contains_subroutine())
267 return this->is_subroutine();
272 glsl_type::sampler_index() const
274 const glsl_type
*const t
= (this->is_array()) ? this->fields
.array
: this;
276 assert(t
->is_sampler());
278 switch (t
->sampler_dimensionality
) {
279 case GLSL_SAMPLER_DIM_1D
:
280 return (t
->sampler_array
) ? TEXTURE_1D_ARRAY_INDEX
: TEXTURE_1D_INDEX
;
281 case GLSL_SAMPLER_DIM_2D
:
282 return (t
->sampler_array
) ? TEXTURE_2D_ARRAY_INDEX
: TEXTURE_2D_INDEX
;
283 case GLSL_SAMPLER_DIM_3D
:
284 return TEXTURE_3D_INDEX
;
285 case GLSL_SAMPLER_DIM_CUBE
:
286 return (t
->sampler_array
) ? TEXTURE_CUBE_ARRAY_INDEX
: TEXTURE_CUBE_INDEX
;
287 case GLSL_SAMPLER_DIM_RECT
:
288 return TEXTURE_RECT_INDEX
;
289 case GLSL_SAMPLER_DIM_BUF
:
290 return TEXTURE_BUFFER_INDEX
;
291 case GLSL_SAMPLER_DIM_EXTERNAL
:
292 return TEXTURE_EXTERNAL_INDEX
;
293 case GLSL_SAMPLER_DIM_MS
:
294 return (t
->sampler_array
) ? TEXTURE_2D_MULTISAMPLE_ARRAY_INDEX
: TEXTURE_2D_MULTISAMPLE_INDEX
;
296 assert(!"Should not get here.");
297 return TEXTURE_BUFFER_INDEX
;
302 glsl_type::contains_image() const
304 if (this->is_array()) {
305 return this->fields
.array
->contains_image();
306 } else if (this->is_record()) {
307 for (unsigned int i
= 0; i
< this->length
; i
++) {
308 if (this->fields
.structure
[i
].type
->contains_image())
313 return this->is_image();
317 const glsl_type
*glsl_type::get_base_type() const
324 case GLSL_TYPE_FLOAT
:
326 case GLSL_TYPE_DOUBLE
:
336 const glsl_type
*glsl_type::get_scalar_type() const
338 const glsl_type
*type
= this;
341 while (type
->base_type
== GLSL_TYPE_ARRAY
)
342 type
= type
->fields
.array
;
344 /* Handle vectors and matrices */
345 switch (type
->base_type
) {
350 case GLSL_TYPE_FLOAT
:
352 case GLSL_TYPE_DOUBLE
:
357 /* Handle everything else */
364 _mesa_glsl_release_types(void)
366 /* Should only be called during atexit (either when unloading shared
367 * object, or if process terminates), so no mutex-locking should be
370 if (glsl_type::array_types
!= NULL
) {
371 _mesa_hash_table_destroy(glsl_type::array_types
, NULL
);
372 glsl_type::array_types
= NULL
;
375 if (glsl_type::record_types
!= NULL
) {
376 _mesa_hash_table_destroy(glsl_type::record_types
, NULL
);
377 glsl_type::record_types
= NULL
;
380 if (glsl_type::interface_types
!= NULL
) {
381 _mesa_hash_table_destroy(glsl_type::interface_types
, NULL
);
382 glsl_type::interface_types
= NULL
;
387 glsl_type::glsl_type(const glsl_type
*array
, unsigned length
) :
388 base_type(GLSL_TYPE_ARRAY
),
389 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
390 sampler_type(0), interface_packing(0),
391 vector_elements(0), matrix_columns(0),
392 length(length
), name(NULL
)
394 this->fields
.array
= array
;
395 /* Inherit the gl type of the base. The GL type is used for
396 * uniform/statevar handling in Mesa and the arrayness of the type
397 * is represented by the size rather than the type.
399 this->gl_type
= array
->gl_type
;
401 /* Allow a maximum of 10 characters for the array size. This is enough
402 * for 32-bits of ~0. The extra 3 are for the '[', ']', and terminating
405 const unsigned name_length
= strlen(array
->name
) + 10 + 3;
407 mtx_lock(&glsl_type::mutex
);
408 char *const n
= (char *) ralloc_size(this->mem_ctx
, name_length
);
409 mtx_unlock(&glsl_type::mutex
);
412 snprintf(n
, name_length
, "%s[]", array
->name
);
414 /* insert outermost dimensions in the correct spot
415 * otherwise the dimension order will be backwards
417 const char *pos
= strchr(array
->name
, '[');
419 int idx
= pos
- array
->name
;
420 snprintf(n
, idx
+1, "%s", array
->name
);
421 snprintf(n
+ idx
, name_length
- idx
, "[%u]%s",
422 length
, array
->name
+ idx
);
424 snprintf(n
, name_length
, "%s[%u]", array
->name
, length
);
433 glsl_type::vec(unsigned components
)
435 if (components
== 0 || components
> 4)
438 static const glsl_type
*const ts
[] = {
439 float_type
, vec2_type
, vec3_type
, vec4_type
441 return ts
[components
- 1];
445 glsl_type::dvec(unsigned components
)
447 if (components
== 0 || components
> 4)
450 static const glsl_type
*const ts
[] = {
451 double_type
, dvec2_type
, dvec3_type
, dvec4_type
453 return ts
[components
- 1];
457 glsl_type::ivec(unsigned components
)
459 if (components
== 0 || components
> 4)
462 static const glsl_type
*const ts
[] = {
463 int_type
, ivec2_type
, ivec3_type
, ivec4_type
465 return ts
[components
- 1];
470 glsl_type::uvec(unsigned components
)
472 if (components
== 0 || components
> 4)
475 static const glsl_type
*const ts
[] = {
476 uint_type
, uvec2_type
, uvec3_type
, uvec4_type
478 return ts
[components
- 1];
483 glsl_type::bvec(unsigned components
)
485 if (components
== 0 || components
> 4)
488 static const glsl_type
*const ts
[] = {
489 bool_type
, bvec2_type
, bvec3_type
, bvec4_type
491 return ts
[components
- 1];
496 glsl_type::get_instance(unsigned base_type
, unsigned rows
, unsigned columns
)
498 if (base_type
== GLSL_TYPE_VOID
)
501 if ((rows
< 1) || (rows
> 4) || (columns
< 1) || (columns
> 4))
504 /* Treat GLSL vectors as Nx1 matrices.
512 case GLSL_TYPE_FLOAT
:
514 case GLSL_TYPE_DOUBLE
:
522 if ((base_type
!= GLSL_TYPE_FLOAT
&& base_type
!= GLSL_TYPE_DOUBLE
) || (rows
== 1))
525 /* GLSL matrix types are named mat{COLUMNS}x{ROWS}. Only the following
526 * combinations are valid:
534 #define IDX(c,r) (((c-1)*3) + (r-1))
536 if (base_type
== GLSL_TYPE_DOUBLE
) {
537 switch (IDX(columns
, rows
)) {
538 case IDX(2,2): return dmat2_type
;
539 case IDX(2,3): return dmat2x3_type
;
540 case IDX(2,4): return dmat2x4_type
;
541 case IDX(3,2): return dmat3x2_type
;
542 case IDX(3,3): return dmat3_type
;
543 case IDX(3,4): return dmat3x4_type
;
544 case IDX(4,2): return dmat4x2_type
;
545 case IDX(4,3): return dmat4x3_type
;
546 case IDX(4,4): return dmat4_type
;
547 default: return error_type
;
550 switch (IDX(columns
, rows
)) {
551 case IDX(2,2): return mat2_type
;
552 case IDX(2,3): return mat2x3_type
;
553 case IDX(2,4): return mat2x4_type
;
554 case IDX(3,2): return mat3x2_type
;
555 case IDX(3,3): return mat3_type
;
556 case IDX(3,4): return mat3x4_type
;
557 case IDX(4,2): return mat4x2_type
;
558 case IDX(4,3): return mat4x3_type
;
559 case IDX(4,4): return mat4_type
;
560 default: return error_type
;
565 assert(!"Should not get here.");
570 glsl_type::get_sampler_instance(enum glsl_sampler_dim dim
,
576 case GLSL_TYPE_FLOAT
:
578 case GLSL_SAMPLER_DIM_1D
:
580 return (array
? sampler1DArrayShadow_type
: sampler1DShadow_type
);
582 return (array
? sampler1DArray_type
: sampler1D_type
);
583 case GLSL_SAMPLER_DIM_2D
:
585 return (array
? sampler2DArrayShadow_type
: sampler2DShadow_type
);
587 return (array
? sampler2DArray_type
: sampler2D_type
);
588 case GLSL_SAMPLER_DIM_3D
:
592 return sampler3D_type
;
593 case GLSL_SAMPLER_DIM_CUBE
:
595 return (array
? samplerCubeArrayShadow_type
: samplerCubeShadow_type
);
597 return (array
? samplerCubeArray_type
: samplerCube_type
);
598 case GLSL_SAMPLER_DIM_RECT
:
602 return sampler2DRectShadow_type
;
604 return sampler2DRect_type
;
605 case GLSL_SAMPLER_DIM_BUF
:
609 return samplerBuffer_type
;
610 case GLSL_SAMPLER_DIM_MS
:
613 return (array
? sampler2DMSArray_type
: sampler2DMS_type
);
614 case GLSL_SAMPLER_DIM_EXTERNAL
:
618 return samplerExternalOES_type
;
624 case GLSL_SAMPLER_DIM_1D
:
625 return (array
? isampler1DArray_type
: isampler1D_type
);
626 case GLSL_SAMPLER_DIM_2D
:
627 return (array
? isampler2DArray_type
: isampler2D_type
);
628 case GLSL_SAMPLER_DIM_3D
:
631 return isampler3D_type
;
632 case GLSL_SAMPLER_DIM_CUBE
:
633 return (array
? isamplerCubeArray_type
: isamplerCube_type
);
634 case GLSL_SAMPLER_DIM_RECT
:
637 return isampler2DRect_type
;
638 case GLSL_SAMPLER_DIM_BUF
:
641 return isamplerBuffer_type
;
642 case GLSL_SAMPLER_DIM_MS
:
643 return (array
? isampler2DMSArray_type
: isampler2DMS_type
);
644 case GLSL_SAMPLER_DIM_EXTERNAL
:
651 case GLSL_SAMPLER_DIM_1D
:
652 return (array
? usampler1DArray_type
: usampler1D_type
);
653 case GLSL_SAMPLER_DIM_2D
:
654 return (array
? usampler2DArray_type
: usampler2D_type
);
655 case GLSL_SAMPLER_DIM_3D
:
658 return usampler3D_type
;
659 case GLSL_SAMPLER_DIM_CUBE
:
660 return (array
? usamplerCubeArray_type
: usamplerCube_type
);
661 case GLSL_SAMPLER_DIM_RECT
:
664 return usampler2DRect_type
;
665 case GLSL_SAMPLER_DIM_BUF
:
668 return usamplerBuffer_type
;
669 case GLSL_SAMPLER_DIM_MS
:
670 return (array
? usampler2DMSArray_type
: usampler2DMS_type
);
671 case GLSL_SAMPLER_DIM_EXTERNAL
:
678 unreachable("switch statement above should be complete");
682 glsl_type::get_array_instance(const glsl_type
*base
, unsigned array_size
)
684 /* Generate a name using the base type pointer in the key. This is
685 * done because the name of the base type may not be unique across
686 * shaders. For example, two shaders may have different record types
690 snprintf(key
, sizeof(key
), "%p[%u]", (void *) base
, array_size
);
692 mtx_lock(&glsl_type::mutex
);
694 if (array_types
== NULL
) {
695 array_types
= _mesa_hash_table_create(NULL
, _mesa_key_hash_string
,
696 _mesa_key_string_equal
);
699 const struct hash_entry
*entry
= _mesa_hash_table_search(array_types
, key
);
701 mtx_unlock(&glsl_type::mutex
);
702 const glsl_type
*t
= new glsl_type(base
, array_size
);
703 mtx_lock(&glsl_type::mutex
);
705 entry
= _mesa_hash_table_insert(array_types
,
706 ralloc_strdup(mem_ctx
, key
),
710 assert(((glsl_type
*) entry
->data
)->base_type
== GLSL_TYPE_ARRAY
);
711 assert(((glsl_type
*) entry
->data
)->length
== array_size
);
712 assert(((glsl_type
*) entry
->data
)->fields
.array
== base
);
714 mtx_unlock(&glsl_type::mutex
);
716 return (glsl_type
*) entry
->data
;
721 glsl_type::record_compare(const glsl_type
*b
) const
723 if (this->length
!= b
->length
)
726 if (this->interface_packing
!= b
->interface_packing
)
729 /* From the GLSL 4.20 specification (Sec 4.2):
731 * "Structures must have the same name, sequence of type names, and
732 * type definitions, and field names to be considered the same type."
734 * GLSL ES behaves the same (Ver 1.00 Sec 4.2.4, Ver 3.00 Sec 4.2.5).
736 * Note that we cannot force type name check when comparing unnamed
737 * structure types, these have a unique name assigned during parsing.
739 if (!this->is_anonymous() && !b
->is_anonymous())
740 if (strcmp(this->name
, b
->name
) != 0)
743 for (unsigned i
= 0; i
< this->length
; i
++) {
744 if (this->fields
.structure
[i
].type
!= b
->fields
.structure
[i
].type
)
746 if (strcmp(this->fields
.structure
[i
].name
,
747 b
->fields
.structure
[i
].name
) != 0)
749 if (this->fields
.structure
[i
].matrix_layout
750 != b
->fields
.structure
[i
].matrix_layout
)
752 if (this->fields
.structure
[i
].location
753 != b
->fields
.structure
[i
].location
)
755 if (this->fields
.structure
[i
].interpolation
756 != b
->fields
.structure
[i
].interpolation
)
758 if (this->fields
.structure
[i
].centroid
759 != b
->fields
.structure
[i
].centroid
)
761 if (this->fields
.structure
[i
].sample
762 != b
->fields
.structure
[i
].sample
)
764 if (this->fields
.structure
[i
].patch
765 != b
->fields
.structure
[i
].patch
)
767 if (this->fields
.structure
[i
].image_read_only
768 != b
->fields
.structure
[i
].image_read_only
)
770 if (this->fields
.structure
[i
].image_write_only
771 != b
->fields
.structure
[i
].image_write_only
)
773 if (this->fields
.structure
[i
].image_coherent
774 != b
->fields
.structure
[i
].image_coherent
)
776 if (this->fields
.structure
[i
].image_volatile
777 != b
->fields
.structure
[i
].image_volatile
)
779 if (this->fields
.structure
[i
].image_restrict
780 != b
->fields
.structure
[i
].image_restrict
)
789 glsl_type::record_key_compare(const void *a
, const void *b
)
791 const glsl_type
*const key1
= (glsl_type
*) a
;
792 const glsl_type
*const key2
= (glsl_type
*) b
;
794 return strcmp(key1
->name
, key2
->name
) == 0 && key1
->record_compare(key2
);
799 * Generate an integer hash value for a glsl_type structure type.
802 glsl_type::record_key_hash(const void *a
)
804 const glsl_type
*const key
= (glsl_type
*) a
;
805 uintptr_t hash
= key
->length
;
808 for (unsigned i
= 0; i
< key
->length
; i
++) {
809 /* casting pointer to uintptr_t */
810 hash
= (hash
* 13 ) + (uintptr_t) key
->fields
.structure
[i
].type
;
813 if (sizeof(hash
) == 8)
814 retval
= (hash
& 0xffffffff) ^ ((uint64_t) hash
>> 32);
823 glsl_type::get_record_instance(const glsl_struct_field
*fields
,
827 const glsl_type
key(fields
, num_fields
, name
);
829 mtx_lock(&glsl_type::mutex
);
831 if (record_types
== NULL
) {
832 record_types
= _mesa_hash_table_create(NULL
, record_key_hash
,
836 const struct hash_entry
*entry
= _mesa_hash_table_search(record_types
,
839 mtx_unlock(&glsl_type::mutex
);
840 const glsl_type
*t
= new glsl_type(fields
, num_fields
, name
);
841 mtx_lock(&glsl_type::mutex
);
843 entry
= _mesa_hash_table_insert(record_types
, t
, (void *) t
);
846 assert(((glsl_type
*) entry
->data
)->base_type
== GLSL_TYPE_STRUCT
);
847 assert(((glsl_type
*) entry
->data
)->length
== num_fields
);
848 assert(strcmp(((glsl_type
*) entry
->data
)->name
, name
) == 0);
850 mtx_unlock(&glsl_type::mutex
);
852 return (glsl_type
*) entry
->data
;
857 glsl_type::get_interface_instance(const glsl_struct_field
*fields
,
859 enum glsl_interface_packing packing
,
860 const char *block_name
)
862 const glsl_type
key(fields
, num_fields
, packing
, block_name
);
864 mtx_lock(&glsl_type::mutex
);
866 if (interface_types
== NULL
) {
867 interface_types
= _mesa_hash_table_create(NULL
, record_key_hash
,
871 const struct hash_entry
*entry
= _mesa_hash_table_search(interface_types
,
874 mtx_unlock(&glsl_type::mutex
);
875 const glsl_type
*t
= new glsl_type(fields
, num_fields
,
876 packing
, block_name
);
877 mtx_lock(&glsl_type::mutex
);
879 entry
= _mesa_hash_table_insert(interface_types
, t
, (void *) t
);
882 assert(((glsl_type
*) entry
->data
)->base_type
== GLSL_TYPE_INTERFACE
);
883 assert(((glsl_type
*) entry
->data
)->length
== num_fields
);
884 assert(strcmp(((glsl_type
*) entry
->data
)->name
, block_name
) == 0);
886 mtx_unlock(&glsl_type::mutex
);
888 return (glsl_type
*) entry
->data
;
892 glsl_type::get_subroutine_instance(const char *subroutine_name
)
894 const glsl_type
key(subroutine_name
);
896 mtx_lock(&glsl_type::mutex
);
898 if (subroutine_types
== NULL
) {
899 subroutine_types
= _mesa_hash_table_create(NULL
, record_key_hash
,
903 const struct hash_entry
*entry
= _mesa_hash_table_search(subroutine_types
,
906 mtx_unlock(&glsl_type::mutex
);
907 const glsl_type
*t
= new glsl_type(subroutine_name
);
908 mtx_lock(&glsl_type::mutex
);
910 entry
= _mesa_hash_table_insert(subroutine_types
, t
, (void *) t
);
913 assert(((glsl_type
*) entry
->data
)->base_type
== GLSL_TYPE_SUBROUTINE
);
914 assert(strcmp(((glsl_type
*) entry
->data
)->name
, subroutine_name
) == 0);
916 mtx_unlock(&glsl_type::mutex
);
918 return (glsl_type
*) entry
->data
;
923 glsl_type::get_mul_type(const glsl_type
*type_a
, const glsl_type
*type_b
)
925 if (type_a
== type_b
) {
927 } else if (type_a
->is_matrix() && type_b
->is_matrix()) {
928 /* Matrix multiply. The columns of A must match the rows of B. Given
929 * the other previously tested constraints, this means the vector type
930 * of a row from A must be the same as the vector type of a column from
933 if (type_a
->row_type() == type_b
->column_type()) {
934 /* The resulting matrix has the number of columns of matrix B and
935 * the number of rows of matrix A. We get the row count of A by
936 * looking at the size of a vector that makes up a column. The
937 * transpose (size of a row) is done for B.
939 const glsl_type
*const type
=
940 get_instance(type_a
->base_type
,
941 type_a
->column_type()->vector_elements
,
942 type_b
->row_type()->vector_elements
);
943 assert(type
!= error_type
);
947 } else if (type_a
->is_matrix()) {
948 /* A is a matrix and B is a column vector. Columns of A must match
949 * rows of B. Given the other previously tested constraints, this
950 * means the vector type of a row from A must be the same as the
951 * vector the type of B.
953 if (type_a
->row_type() == type_b
) {
954 /* The resulting vector has a number of elements equal to
955 * the number of rows of matrix A. */
956 const glsl_type
*const type
=
957 get_instance(type_a
->base_type
,
958 type_a
->column_type()->vector_elements
,
960 assert(type
!= error_type
);
965 assert(type_b
->is_matrix());
967 /* A is a row vector and B is a matrix. Columns of A must match rows
968 * of B. Given the other previously tested constraints, this means
969 * the type of A must be the same as the vector type of a column from
972 if (type_a
== type_b
->column_type()) {
973 /* The resulting vector has a number of elements equal to
974 * the number of columns of matrix B. */
975 const glsl_type
*const type
=
976 get_instance(type_a
->base_type
,
977 type_b
->row_type()->vector_elements
,
979 assert(type
!= error_type
);
990 glsl_type::field_type(const char *name
) const
992 if (this->base_type
!= GLSL_TYPE_STRUCT
993 && this->base_type
!= GLSL_TYPE_INTERFACE
)
996 for (unsigned i
= 0; i
< this->length
; i
++) {
997 if (strcmp(name
, this->fields
.structure
[i
].name
) == 0)
998 return this->fields
.structure
[i
].type
;
1006 glsl_type::field_index(const char *name
) const
1008 if (this->base_type
!= GLSL_TYPE_STRUCT
1009 && this->base_type
!= GLSL_TYPE_INTERFACE
)
1012 for (unsigned i
= 0; i
< this->length
; i
++) {
1013 if (strcmp(name
, this->fields
.structure
[i
].name
) == 0)
1022 glsl_type::component_slots() const
1024 switch (this->base_type
) {
1025 case GLSL_TYPE_UINT
:
1027 case GLSL_TYPE_FLOAT
:
1028 case GLSL_TYPE_BOOL
:
1029 return this->components();
1031 case GLSL_TYPE_DOUBLE
:
1032 return 2 * this->components();
1034 case GLSL_TYPE_STRUCT
:
1035 case GLSL_TYPE_INTERFACE
: {
1038 for (unsigned i
= 0; i
< this->length
; i
++)
1039 size
+= this->fields
.structure
[i
].type
->component_slots();
1044 case GLSL_TYPE_ARRAY
:
1045 return this->length
* this->fields
.array
->component_slots();
1047 case GLSL_TYPE_IMAGE
:
1049 case GLSL_TYPE_SUBROUTINE
:
1051 case GLSL_TYPE_SAMPLER
:
1052 case GLSL_TYPE_ATOMIC_UINT
:
1053 case GLSL_TYPE_VOID
:
1054 case GLSL_TYPE_ERROR
:
1062 glsl_type::record_location_offset(unsigned length
) const
1064 unsigned offset
= 0;
1065 const glsl_type
*t
= this->without_array();
1066 if (t
->is_record()) {
1067 assert(length
<= t
->length
);
1069 for (unsigned i
= 0; i
< length
; i
++) {
1070 const glsl_type
*st
= t
->fields
.structure
[i
].type
;
1071 const glsl_type
*wa
= st
->without_array();
1072 if (wa
->is_record()) {
1073 unsigned r_offset
= wa
->record_location_offset(wa
->length
);
1074 offset
+= st
->is_array() ?
1075 st
->arrays_of_arrays_size() * r_offset
: r_offset
;
1076 } else if (st
->is_array() && st
->fields
.array
->is_array()) {
1077 unsigned outer_array_size
= st
->length
;
1078 const glsl_type
*base_type
= st
->fields
.array
;
1080 /* For arrays of arrays the outer arrays take up a uniform
1081 * slot for each element. The innermost array elements share a
1082 * single slot so we ignore the innermost array when calculating
1085 while (base_type
->fields
.array
->is_array()) {
1086 outer_array_size
= outer_array_size
* base_type
->length
;
1087 base_type
= base_type
->fields
.array
;
1089 offset
+= outer_array_size
;
1091 /* We dont worry about arrays here because unless the array
1092 * contains a structure or another array it only takes up a single
1103 glsl_type::uniform_locations() const
1107 switch (this->base_type
) {
1108 case GLSL_TYPE_UINT
:
1110 case GLSL_TYPE_FLOAT
:
1111 case GLSL_TYPE_DOUBLE
:
1112 case GLSL_TYPE_BOOL
:
1113 case GLSL_TYPE_SAMPLER
:
1114 case GLSL_TYPE_IMAGE
:
1115 case GLSL_TYPE_SUBROUTINE
:
1118 case GLSL_TYPE_STRUCT
:
1119 case GLSL_TYPE_INTERFACE
:
1120 for (unsigned i
= 0; i
< this->length
; i
++)
1121 size
+= this->fields
.structure
[i
].type
->uniform_locations();
1123 case GLSL_TYPE_ARRAY
:
1124 return this->length
* this->fields
.array
->uniform_locations();
1131 glsl_type::can_implicitly_convert_to(const glsl_type
*desired
,
1132 _mesa_glsl_parse_state
*state
) const
1134 if (this == desired
)
1137 /* There is no conversion among matrix types. */
1138 if (this->matrix_columns
> 1 || desired
->matrix_columns
> 1)
1141 /* Vector size must match. */
1142 if (this->vector_elements
!= desired
->vector_elements
)
1145 /* int and uint can be converted to float. */
1146 if (desired
->is_float() && this->is_integer())
1149 /* With GLSL 4.0 / ARB_gpu_shader5, int can be converted to uint.
1150 * Note that state may be NULL here, when resolving function calls in the
1151 * linker. By this time, all the state-dependent checks have already
1152 * happened though, so allow anything that's allowed in any shader version. */
1153 if ((!state
|| state
->is_version(400, 0) || state
->ARB_gpu_shader5_enable
) &&
1154 desired
->base_type
== GLSL_TYPE_UINT
&& this->base_type
== GLSL_TYPE_INT
)
1157 /* No implicit conversions from double. */
1158 if ((!state
|| state
->has_double()) && this->is_double())
1161 /* Conversions from different types to double. */
1162 if ((!state
|| state
->has_double()) && desired
->is_double()) {
1163 if (this->is_float())
1165 if (this->is_integer())
1173 glsl_type::std140_base_alignment(bool row_major
) const
1175 unsigned N
= is_double() ? 8 : 4;
1177 /* (1) If the member is a scalar consuming <N> basic machine units, the
1178 * base alignment is <N>.
1180 * (2) If the member is a two- or four-component vector with components
1181 * consuming <N> basic machine units, the base alignment is 2<N> or
1182 * 4<N>, respectively.
1184 * (3) If the member is a three-component vector with components consuming
1185 * <N> basic machine units, the base alignment is 4<N>.
1187 if (this->is_scalar() || this->is_vector()) {
1188 switch (this->vector_elements
) {
1199 /* (4) If the member is an array of scalars or vectors, the base alignment
1200 * and array stride are set to match the base alignment of a single
1201 * array element, according to rules (1), (2), and (3), and rounded up
1202 * to the base alignment of a vec4. The array may have padding at the
1203 * end; the base offset of the member following the array is rounded up
1204 * to the next multiple of the base alignment.
1206 * (6) If the member is an array of <S> column-major matrices with <C>
1207 * columns and <R> rows, the matrix is stored identically to a row of
1208 * <S>*<C> column vectors with <R> components each, according to rule
1211 * (8) If the member is an array of <S> row-major matrices with <C> columns
1212 * and <R> rows, the matrix is stored identically to a row of <S>*<R>
1213 * row vectors with <C> components each, according to rule (4).
1215 * (10) If the member is an array of <S> structures, the <S> elements of
1216 * the array are laid out in order, according to rule (9).
1218 if (this->is_array()) {
1219 if (this->fields
.array
->is_scalar() ||
1220 this->fields
.array
->is_vector() ||
1221 this->fields
.array
->is_matrix()) {
1222 return MAX2(this->fields
.array
->std140_base_alignment(row_major
), 16);
1224 assert(this->fields
.array
->is_record() ||
1225 this->fields
.array
->is_array());
1226 return this->fields
.array
->std140_base_alignment(row_major
);
1230 /* (5) If the member is a column-major matrix with <C> columns and
1231 * <R> rows, the matrix is stored identically to an array of
1232 * <C> column vectors with <R> components each, according to
1235 * (7) If the member is a row-major matrix with <C> columns and <R>
1236 * rows, the matrix is stored identically to an array of <R>
1237 * row vectors with <C> components each, according to rule (4).
1239 if (this->is_matrix()) {
1240 const struct glsl_type
*vec_type
, *array_type
;
1241 int c
= this->matrix_columns
;
1242 int r
= this->vector_elements
;
1245 vec_type
= get_instance(base_type
, c
, 1);
1246 array_type
= glsl_type::get_array_instance(vec_type
, r
);
1248 vec_type
= get_instance(base_type
, r
, 1);
1249 array_type
= glsl_type::get_array_instance(vec_type
, c
);
1252 return array_type
->std140_base_alignment(false);
1255 /* (9) If the member is a structure, the base alignment of the
1256 * structure is <N>, where <N> is the largest base alignment
1257 * value of any of its members, and rounded up to the base
1258 * alignment of a vec4. The individual members of this
1259 * sub-structure are then assigned offsets by applying this set
1260 * of rules recursively, where the base offset of the first
1261 * member of the sub-structure is equal to the aligned offset
1262 * of the structure. The structure may have padding at the end;
1263 * the base offset of the member following the sub-structure is
1264 * rounded up to the next multiple of the base alignment of the
1267 if (this->is_record()) {
1268 unsigned base_alignment
= 16;
1269 for (unsigned i
= 0; i
< this->length
; i
++) {
1270 bool field_row_major
= row_major
;
1271 const enum glsl_matrix_layout matrix_layout
=
1272 glsl_matrix_layout(this->fields
.structure
[i
].matrix_layout
);
1273 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
1274 field_row_major
= true;
1275 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
1276 field_row_major
= false;
1279 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
1280 base_alignment
= MAX2(base_alignment
,
1281 field_type
->std140_base_alignment(field_row_major
));
1283 return base_alignment
;
1286 assert(!"not reached");
1291 glsl_type::std140_size(bool row_major
) const
1293 unsigned N
= is_double() ? 8 : 4;
1295 /* (1) If the member is a scalar consuming <N> basic machine units, the
1296 * base alignment is <N>.
1298 * (2) If the member is a two- or four-component vector with components
1299 * consuming <N> basic machine units, the base alignment is 2<N> or
1300 * 4<N>, respectively.
1302 * (3) If the member is a three-component vector with components consuming
1303 * <N> basic machine units, the base alignment is 4<N>.
1305 if (this->is_scalar() || this->is_vector()) {
1306 return this->vector_elements
* N
;
1309 /* (5) If the member is a column-major matrix with <C> columns and
1310 * <R> rows, the matrix is stored identically to an array of
1311 * <C> column vectors with <R> components each, according to
1314 * (6) If the member is an array of <S> column-major matrices with <C>
1315 * columns and <R> rows, the matrix is stored identically to a row of
1316 * <S>*<C> column vectors with <R> components each, according to rule
1319 * (7) If the member is a row-major matrix with <C> columns and <R>
1320 * rows, the matrix is stored identically to an array of <R>
1321 * row vectors with <C> components each, according to rule (4).
1323 * (8) If the member is an array of <S> row-major matrices with <C> columns
1324 * and <R> rows, the matrix is stored identically to a row of <S>*<R>
1325 * row vectors with <C> components each, according to rule (4).
1327 if (this->without_array()->is_matrix()) {
1328 const struct glsl_type
*element_type
;
1329 const struct glsl_type
*vec_type
;
1330 unsigned int array_len
;
1332 if (this->is_array()) {
1333 element_type
= this->without_array();
1334 array_len
= this->arrays_of_arrays_size();
1336 element_type
= this;
1341 vec_type
= get_instance(element_type
->base_type
,
1342 element_type
->matrix_columns
, 1);
1344 array_len
*= element_type
->vector_elements
;
1346 vec_type
= get_instance(element_type
->base_type
,
1347 element_type
->vector_elements
, 1);
1348 array_len
*= element_type
->matrix_columns
;
1350 const glsl_type
*array_type
= glsl_type::get_array_instance(vec_type
,
1353 return array_type
->std140_size(false);
1356 /* (4) If the member is an array of scalars or vectors, the base alignment
1357 * and array stride are set to match the base alignment of a single
1358 * array element, according to rules (1), (2), and (3), and rounded up
1359 * to the base alignment of a vec4. The array may have padding at the
1360 * end; the base offset of the member following the array is rounded up
1361 * to the next multiple of the base alignment.
1363 * (10) If the member is an array of <S> structures, the <S> elements of
1364 * the array are laid out in order, according to rule (9).
1366 if (this->is_array()) {
1367 if (this->without_array()->is_record()) {
1368 return this->arrays_of_arrays_size() *
1369 this->without_array()->std140_size(row_major
);
1371 unsigned element_base_align
=
1372 this->without_array()->std140_base_alignment(row_major
);
1373 return this->arrays_of_arrays_size() * MAX2(element_base_align
, 16);
1377 /* (9) If the member is a structure, the base alignment of the
1378 * structure is <N>, where <N> is the largest base alignment
1379 * value of any of its members, and rounded up to the base
1380 * alignment of a vec4. The individual members of this
1381 * sub-structure are then assigned offsets by applying this set
1382 * of rules recursively, where the base offset of the first
1383 * member of the sub-structure is equal to the aligned offset
1384 * of the structure. The structure may have padding at the end;
1385 * the base offset of the member following the sub-structure is
1386 * rounded up to the next multiple of the base alignment of the
1389 if (this->is_record() || this->is_interface()) {
1391 unsigned max_align
= 0;
1393 for (unsigned i
= 0; i
< this->length
; i
++) {
1394 bool field_row_major
= row_major
;
1395 const enum glsl_matrix_layout matrix_layout
=
1396 glsl_matrix_layout(this->fields
.structure
[i
].matrix_layout
);
1397 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
1398 field_row_major
= true;
1399 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
1400 field_row_major
= false;
1403 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
1404 unsigned align
= field_type
->std140_base_alignment(field_row_major
);
1406 /* Ignore unsized arrays when calculating size */
1407 if (field_type
->is_unsized_array())
1410 size
= glsl_align(size
, align
);
1411 size
+= field_type
->std140_size(field_row_major
);
1413 max_align
= MAX2(align
, max_align
);
1415 if (field_type
->is_record() && (i
+ 1 < this->length
))
1416 size
= glsl_align(size
, 16);
1418 size
= glsl_align(size
, MAX2(max_align
, 16));
1422 assert(!"not reached");
1427 glsl_type::std430_base_alignment(bool row_major
) const
1430 unsigned N
= is_double() ? 8 : 4;
1432 /* (1) If the member is a scalar consuming <N> basic machine units, the
1433 * base alignment is <N>.
1435 * (2) If the member is a two- or four-component vector with components
1436 * consuming <N> basic machine units, the base alignment is 2<N> or
1437 * 4<N>, respectively.
1439 * (3) If the member is a three-component vector with components consuming
1440 * <N> basic machine units, the base alignment is 4<N>.
1442 if (this->is_scalar() || this->is_vector()) {
1443 switch (this->vector_elements
) {
1454 /* OpenGL 4.30 spec, section 7.6.2.2 "Standard Uniform Block Layout":
1456 * "When using the std430 storage layout, shader storage blocks will be
1457 * laid out in buffer storage identically to uniform and shader storage
1458 * blocks using the std140 layout, except that the base alignment and
1459 * stride of arrays of scalars and vectors in rule 4 and of structures
1460 * in rule 9 are not rounded up a multiple of the base alignment of a vec4.
1463 /* (1) If the member is a scalar consuming <N> basic machine units, the
1464 * base alignment is <N>.
1466 * (2) If the member is a two- or four-component vector with components
1467 * consuming <N> basic machine units, the base alignment is 2<N> or
1468 * 4<N>, respectively.
1470 * (3) If the member is a three-component vector with components consuming
1471 * <N> basic machine units, the base alignment is 4<N>.
1473 if (this->is_array())
1474 return this->fields
.array
->std430_base_alignment(row_major
);
1476 /* (5) If the member is a column-major matrix with <C> columns and
1477 * <R> rows, the matrix is stored identically to an array of
1478 * <C> column vectors with <R> components each, according to
1481 * (7) If the member is a row-major matrix with <C> columns and <R>
1482 * rows, the matrix is stored identically to an array of <R>
1483 * row vectors with <C> components each, according to rule (4).
1485 if (this->is_matrix()) {
1486 const struct glsl_type
*vec_type
, *array_type
;
1487 int c
= this->matrix_columns
;
1488 int r
= this->vector_elements
;
1491 vec_type
= get_instance(base_type
, c
, 1);
1492 array_type
= glsl_type::get_array_instance(vec_type
, r
);
1494 vec_type
= get_instance(base_type
, r
, 1);
1495 array_type
= glsl_type::get_array_instance(vec_type
, c
);
1498 return array_type
->std430_base_alignment(false);
1501 /* (9) If the member is a structure, the base alignment of the
1502 * structure is <N>, where <N> is the largest base alignment
1503 * value of any of its members, and rounded up to the base
1504 * alignment of a vec4. The individual members of this
1505 * sub-structure are then assigned offsets by applying this set
1506 * of rules recursively, where the base offset of the first
1507 * member of the sub-structure is equal to the aligned offset
1508 * of the structure. The structure may have padding at the end;
1509 * the base offset of the member following the sub-structure is
1510 * rounded up to the next multiple of the base alignment of the
1513 if (this->is_record()) {
1514 unsigned base_alignment
= 0;
1515 for (unsigned i
= 0; i
< this->length
; i
++) {
1516 bool field_row_major
= row_major
;
1517 const enum glsl_matrix_layout matrix_layout
=
1518 glsl_matrix_layout(this->fields
.structure
[i
].matrix_layout
);
1519 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
1520 field_row_major
= true;
1521 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
1522 field_row_major
= false;
1525 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
1526 base_alignment
= MAX2(base_alignment
,
1527 field_type
->std430_base_alignment(field_row_major
));
1529 assert(base_alignment
> 0);
1530 return base_alignment
;
1532 assert(!"not reached");
1537 glsl_type::std430_array_stride(bool row_major
) const
1539 unsigned N
= is_double() ? 8 : 4;
1541 /* Notice that the array stride of a vec3 is not 3 * N but 4 * N.
1542 * See OpenGL 4.30 spec, section 7.6.2.2 "Standard Uniform Block Layout"
1544 * (3) If the member is a three-component vector with components consuming
1545 * <N> basic machine units, the base alignment is 4<N>.
1547 if (this->is_vector() && this->vector_elements
== 3)
1550 /* By default use std430_size(row_major) */
1551 return this->std430_size(row_major
);
1555 glsl_type::std430_size(bool row_major
) const
1557 unsigned N
= is_double() ? 8 : 4;
1559 /* OpenGL 4.30 spec, section 7.6.2.2 "Standard Uniform Block Layout":
1561 * "When using the std430 storage layout, shader storage blocks will be
1562 * laid out in buffer storage identically to uniform and shader storage
1563 * blocks using the std140 layout, except that the base alignment and
1564 * stride of arrays of scalars and vectors in rule 4 and of structures
1565 * in rule 9 are not rounded up a multiple of the base alignment of a vec4.
1567 if (this->is_scalar() || this->is_vector())
1568 return this->vector_elements
* N
;
1570 if (this->without_array()->is_matrix()) {
1571 const struct glsl_type
*element_type
;
1572 const struct glsl_type
*vec_type
;
1573 unsigned int array_len
;
1575 if (this->is_array()) {
1576 element_type
= this->without_array();
1577 array_len
= this->arrays_of_arrays_size();
1579 element_type
= this;
1584 vec_type
= get_instance(element_type
->base_type
,
1585 element_type
->matrix_columns
, 1);
1587 array_len
*= element_type
->vector_elements
;
1589 vec_type
= get_instance(element_type
->base_type
,
1590 element_type
->vector_elements
, 1);
1591 array_len
*= element_type
->matrix_columns
;
1593 const glsl_type
*array_type
= glsl_type::get_array_instance(vec_type
,
1596 return array_type
->std430_size(false);
1599 if (this->is_array()) {
1600 if (this->without_array()->is_record())
1601 return this->arrays_of_arrays_size() *
1602 this->without_array()->std430_size(row_major
);
1604 return this->arrays_of_arrays_size() *
1605 this->without_array()->std430_base_alignment(row_major
);
1608 if (this->is_record() || this->is_interface()) {
1610 unsigned max_align
= 0;
1612 for (unsigned i
= 0; i
< this->length
; i
++) {
1613 bool field_row_major
= row_major
;
1614 const enum glsl_matrix_layout matrix_layout
=
1615 glsl_matrix_layout(this->fields
.structure
[i
].matrix_layout
);
1616 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
1617 field_row_major
= true;
1618 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
1619 field_row_major
= false;
1622 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
1623 unsigned align
= field_type
->std430_base_alignment(field_row_major
);
1624 size
= glsl_align(size
, align
);
1625 size
+= field_type
->std430_size(field_row_major
);
1627 max_align
= MAX2(align
, max_align
);
1629 size
= glsl_align(size
, max_align
);
1633 assert(!"not reached");
1638 glsl_type::count_attribute_slots() const
1640 /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
1642 * "A scalar input counts the same amount against this limit as a vec4,
1643 * so applications may want to consider packing groups of four
1644 * unrelated float inputs together into a vector to better utilize the
1645 * capabilities of the underlying hardware. A matrix input will use up
1646 * multiple locations. The number of locations used will equal the
1647 * number of columns in the matrix."
1649 * The spec does not explicitly say how arrays are counted. However, it
1650 * should be safe to assume the total number of slots consumed by an array
1651 * is the number of entries in the array multiplied by the number of slots
1652 * consumed by a single element of the array.
1654 * The spec says nothing about how structs are counted, because vertex
1655 * attributes are not allowed to be (or contain) structs. However, Mesa
1656 * allows varying structs, the number of varying slots taken up by a
1657 * varying struct is simply equal to the sum of the number of slots taken
1658 * up by each element.
1660 switch (this->base_type
) {
1661 case GLSL_TYPE_UINT
:
1663 case GLSL_TYPE_FLOAT
:
1664 case GLSL_TYPE_BOOL
:
1665 case GLSL_TYPE_DOUBLE
:
1666 return this->matrix_columns
;
1668 case GLSL_TYPE_STRUCT
:
1669 case GLSL_TYPE_INTERFACE
: {
1672 for (unsigned i
= 0; i
< this->length
; i
++)
1673 size
+= this->fields
.structure
[i
].type
->count_attribute_slots();
1678 case GLSL_TYPE_ARRAY
:
1679 return this->length
* this->fields
.array
->count_attribute_slots();
1681 case GLSL_TYPE_SAMPLER
:
1682 case GLSL_TYPE_IMAGE
:
1683 case GLSL_TYPE_ATOMIC_UINT
:
1684 case GLSL_TYPE_VOID
:
1685 case GLSL_TYPE_SUBROUTINE
:
1686 case GLSL_TYPE_ERROR
:
1690 assert(!"Unexpected type in count_attribute_slots()");
1696 glsl_type::coordinate_components() const
1700 switch (sampler_dimensionality
) {
1701 case GLSL_SAMPLER_DIM_1D
:
1702 case GLSL_SAMPLER_DIM_BUF
:
1705 case GLSL_SAMPLER_DIM_2D
:
1706 case GLSL_SAMPLER_DIM_RECT
:
1707 case GLSL_SAMPLER_DIM_MS
:
1708 case GLSL_SAMPLER_DIM_EXTERNAL
:
1711 case GLSL_SAMPLER_DIM_3D
:
1712 case GLSL_SAMPLER_DIM_CUBE
:
1716 assert(!"Should not get here.");
1721 /* Array textures need an additional component for the array index, except
1722 * for cubemap array images that behave like a 2D array of interleaved
1725 if (sampler_array
&&
1726 !(base_type
== GLSL_TYPE_IMAGE
&&
1727 sampler_dimensionality
== GLSL_SAMPLER_DIM_CUBE
))
1734 * Declarations of type flyweights (glsl_type::_foo_type) and
1735 * convenience pointers (glsl_type::foo_type).
1738 #define DECL_TYPE(NAME, ...) \
1739 const glsl_type glsl_type::_##NAME##_type = glsl_type(__VA_ARGS__, #NAME); \
1740 const glsl_type *const glsl_type::NAME##_type = &glsl_type::_##NAME##_type;
1742 #define STRUCT_TYPE(NAME)
1744 #include "builtin_type_macros.h"