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() ? st
->length
* r_offset
: r_offset
;
1076 /* We dont worry about arrays here because unless the array
1077 * contains a structure or another array it only takes up a single
1088 glsl_type::uniform_locations() const
1092 switch (this->base_type
) {
1093 case GLSL_TYPE_UINT
:
1095 case GLSL_TYPE_FLOAT
:
1096 case GLSL_TYPE_DOUBLE
:
1097 case GLSL_TYPE_BOOL
:
1098 case GLSL_TYPE_SAMPLER
:
1099 case GLSL_TYPE_IMAGE
:
1100 case GLSL_TYPE_SUBROUTINE
:
1103 case GLSL_TYPE_STRUCT
:
1104 case GLSL_TYPE_INTERFACE
:
1105 for (unsigned i
= 0; i
< this->length
; i
++)
1106 size
+= this->fields
.structure
[i
].type
->uniform_locations();
1108 case GLSL_TYPE_ARRAY
:
1109 return this->length
* this->fields
.array
->uniform_locations();
1116 glsl_type::can_implicitly_convert_to(const glsl_type
*desired
,
1117 _mesa_glsl_parse_state
*state
) const
1119 if (this == desired
)
1122 /* There is no conversion among matrix types. */
1123 if (this->matrix_columns
> 1 || desired
->matrix_columns
> 1)
1126 /* Vector size must match. */
1127 if (this->vector_elements
!= desired
->vector_elements
)
1130 /* int and uint can be converted to float. */
1131 if (desired
->is_float() && this->is_integer())
1134 /* With GLSL 4.0 / ARB_gpu_shader5, int can be converted to uint.
1135 * Note that state may be NULL here, when resolving function calls in the
1136 * linker. By this time, all the state-dependent checks have already
1137 * happened though, so allow anything that's allowed in any shader version. */
1138 if ((!state
|| state
->is_version(400, 0) || state
->ARB_gpu_shader5_enable
) &&
1139 desired
->base_type
== GLSL_TYPE_UINT
&& this->base_type
== GLSL_TYPE_INT
)
1142 /* No implicit conversions from double. */
1143 if ((!state
|| state
->has_double()) && this->is_double())
1146 /* Conversions from different types to double. */
1147 if ((!state
|| state
->has_double()) && desired
->is_double()) {
1148 if (this->is_float())
1150 if (this->is_integer())
1158 glsl_type::std140_base_alignment(bool row_major
) const
1160 unsigned N
= is_double() ? 8 : 4;
1162 /* (1) If the member is a scalar consuming <N> basic machine units, the
1163 * base alignment is <N>.
1165 * (2) If the member is a two- or four-component vector with components
1166 * consuming <N> basic machine units, the base alignment is 2<N> or
1167 * 4<N>, respectively.
1169 * (3) If the member is a three-component vector with components consuming
1170 * <N> basic machine units, the base alignment is 4<N>.
1172 if (this->is_scalar() || this->is_vector()) {
1173 switch (this->vector_elements
) {
1184 /* (4) If the member is an array of scalars or vectors, the base alignment
1185 * and array stride are set to match the base alignment of a single
1186 * array element, according to rules (1), (2), and (3), and rounded up
1187 * to the base alignment of a vec4. The array may have padding at the
1188 * end; the base offset of the member following the array is rounded up
1189 * to the next multiple of the base alignment.
1191 * (6) If the member is an array of <S> column-major matrices with <C>
1192 * columns and <R> rows, the matrix is stored identically to a row of
1193 * <S>*<C> column vectors with <R> components each, according to rule
1196 * (8) If the member is an array of <S> row-major matrices with <C> columns
1197 * and <R> rows, the matrix is stored identically to a row of <S>*<R>
1198 * row vectors with <C> components each, according to rule (4).
1200 * (10) If the member is an array of <S> structures, the <S> elements of
1201 * the array are laid out in order, according to rule (9).
1203 if (this->is_array()) {
1204 if (this->fields
.array
->is_scalar() ||
1205 this->fields
.array
->is_vector() ||
1206 this->fields
.array
->is_matrix()) {
1207 return MAX2(this->fields
.array
->std140_base_alignment(row_major
), 16);
1209 assert(this->fields
.array
->is_record() ||
1210 this->fields
.array
->is_array());
1211 return this->fields
.array
->std140_base_alignment(row_major
);
1215 /* (5) If the member is a column-major matrix with <C> columns and
1216 * <R> rows, the matrix is stored identically to an array of
1217 * <C> column vectors with <R> components each, according to
1220 * (7) If the member is a row-major matrix with <C> columns and <R>
1221 * rows, the matrix is stored identically to an array of <R>
1222 * row vectors with <C> components each, according to rule (4).
1224 if (this->is_matrix()) {
1225 const struct glsl_type
*vec_type
, *array_type
;
1226 int c
= this->matrix_columns
;
1227 int r
= this->vector_elements
;
1230 vec_type
= get_instance(base_type
, c
, 1);
1231 array_type
= glsl_type::get_array_instance(vec_type
, r
);
1233 vec_type
= get_instance(base_type
, r
, 1);
1234 array_type
= glsl_type::get_array_instance(vec_type
, c
);
1237 return array_type
->std140_base_alignment(false);
1240 /* (9) If the member is a structure, the base alignment of the
1241 * structure is <N>, where <N> is the largest base alignment
1242 * value of any of its members, and rounded up to the base
1243 * alignment of a vec4. The individual members of this
1244 * sub-structure are then assigned offsets by applying this set
1245 * of rules recursively, where the base offset of the first
1246 * member of the sub-structure is equal to the aligned offset
1247 * of the structure. The structure may have padding at the end;
1248 * the base offset of the member following the sub-structure is
1249 * rounded up to the next multiple of the base alignment of the
1252 if (this->is_record()) {
1253 unsigned base_alignment
= 16;
1254 for (unsigned i
= 0; i
< this->length
; i
++) {
1255 bool field_row_major
= row_major
;
1256 const enum glsl_matrix_layout matrix_layout
=
1257 glsl_matrix_layout(this->fields
.structure
[i
].matrix_layout
);
1258 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
1259 field_row_major
= true;
1260 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
1261 field_row_major
= false;
1264 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
1265 base_alignment
= MAX2(base_alignment
,
1266 field_type
->std140_base_alignment(field_row_major
));
1268 return base_alignment
;
1271 assert(!"not reached");
1276 glsl_type::std140_size(bool row_major
) const
1278 unsigned N
= is_double() ? 8 : 4;
1280 /* (1) If the member is a scalar consuming <N> basic machine units, the
1281 * base alignment is <N>.
1283 * (2) If the member is a two- or four-component vector with components
1284 * consuming <N> basic machine units, the base alignment is 2<N> or
1285 * 4<N>, respectively.
1287 * (3) If the member is a three-component vector with components consuming
1288 * <N> basic machine units, the base alignment is 4<N>.
1290 if (this->is_scalar() || this->is_vector()) {
1291 return this->vector_elements
* N
;
1294 /* (5) If the member is a column-major matrix with <C> columns and
1295 * <R> rows, the matrix is stored identically to an array of
1296 * <C> column vectors with <R> components each, according to
1299 * (6) If the member is an array of <S> column-major matrices with <C>
1300 * columns and <R> rows, the matrix is stored identically to a row of
1301 * <S>*<C> column vectors with <R> components each, according to rule
1304 * (7) If the member is a row-major matrix with <C> columns and <R>
1305 * rows, the matrix is stored identically to an array of <R>
1306 * row vectors with <C> components each, according to rule (4).
1308 * (8) If the member is an array of <S> row-major matrices with <C> columns
1309 * and <R> rows, the matrix is stored identically to a row of <S>*<R>
1310 * row vectors with <C> components each, according to rule (4).
1312 if (this->without_array()->is_matrix()) {
1313 const struct glsl_type
*element_type
;
1314 const struct glsl_type
*vec_type
;
1315 unsigned int array_len
;
1317 if (this->is_array()) {
1318 element_type
= this->fields
.array
;
1319 array_len
= this->length
;
1321 element_type
= this;
1326 vec_type
= get_instance(element_type
->base_type
,
1327 element_type
->matrix_columns
, 1);
1329 array_len
*= element_type
->vector_elements
;
1331 vec_type
= get_instance(element_type
->base_type
,
1332 element_type
->vector_elements
, 1);
1333 array_len
*= element_type
->matrix_columns
;
1335 const glsl_type
*array_type
= glsl_type::get_array_instance(vec_type
,
1338 return array_type
->std140_size(false);
1341 /* (4) If the member is an array of scalars or vectors, the base alignment
1342 * and array stride are set to match the base alignment of a single
1343 * array element, according to rules (1), (2), and (3), and rounded up
1344 * to the base alignment of a vec4. The array may have padding at the
1345 * end; the base offset of the member following the array is rounded up
1346 * to the next multiple of the base alignment.
1348 * (10) If the member is an array of <S> structures, the <S> elements of
1349 * the array are laid out in order, according to rule (9).
1351 if (this->is_array()) {
1352 if (this->fields
.array
->is_record()) {
1353 return this->length
* this->fields
.array
->std140_size(row_major
);
1355 unsigned element_base_align
=
1356 this->fields
.array
->std140_base_alignment(row_major
);
1357 return this->length
* MAX2(element_base_align
, 16);
1361 /* (9) If the member is a structure, the base alignment of the
1362 * structure is <N>, where <N> is the largest base alignment
1363 * value of any of its members, and rounded up to the base
1364 * alignment of a vec4. The individual members of this
1365 * sub-structure are then assigned offsets by applying this set
1366 * of rules recursively, where the base offset of the first
1367 * member of the sub-structure is equal to the aligned offset
1368 * of the structure. The structure may have padding at the end;
1369 * the base offset of the member following the sub-structure is
1370 * rounded up to the next multiple of the base alignment of the
1373 if (this->is_record() || this->is_interface()) {
1375 unsigned max_align
= 0;
1377 for (unsigned i
= 0; i
< this->length
; i
++) {
1378 bool field_row_major
= row_major
;
1379 const enum glsl_matrix_layout matrix_layout
=
1380 glsl_matrix_layout(this->fields
.structure
[i
].matrix_layout
);
1381 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
1382 field_row_major
= true;
1383 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
1384 field_row_major
= false;
1387 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
1388 unsigned align
= field_type
->std140_base_alignment(field_row_major
);
1390 /* Ignore unsized arrays when calculating size */
1391 if (field_type
->is_unsized_array())
1394 size
= glsl_align(size
, align
);
1395 size
+= field_type
->std140_size(field_row_major
);
1397 max_align
= MAX2(align
, max_align
);
1399 if (field_type
->is_record() && (i
+ 1 < this->length
))
1400 size
= glsl_align(size
, 16);
1402 size
= glsl_align(size
, MAX2(max_align
, 16));
1406 assert(!"not reached");
1411 glsl_type::std430_base_alignment(bool row_major
) const
1414 unsigned N
= is_double() ? 8 : 4;
1416 /* (1) If the member is a scalar consuming <N> basic machine units, the
1417 * base alignment is <N>.
1419 * (2) If the member is a two- or four-component vector with components
1420 * consuming <N> basic machine units, the base alignment is 2<N> or
1421 * 4<N>, respectively.
1423 * (3) If the member is a three-component vector with components consuming
1424 * <N> basic machine units, the base alignment is 4<N>.
1426 if (this->is_scalar() || this->is_vector()) {
1427 switch (this->vector_elements
) {
1438 /* OpenGL 4.30 spec, section 7.6.2.2 "Standard Uniform Block Layout":
1440 * "When using the std430 storage layout, shader storage blocks will be
1441 * laid out in buffer storage identically to uniform and shader storage
1442 * blocks using the std140 layout, except that the base alignment and
1443 * stride of arrays of scalars and vectors in rule 4 and of structures
1444 * in rule 9 are not rounded up a multiple of the base alignment of a vec4.
1447 /* (1) If the member is a scalar consuming <N> basic machine units, the
1448 * base alignment is <N>.
1450 * (2) If the member is a two- or four-component vector with components
1451 * consuming <N> basic machine units, the base alignment is 2<N> or
1452 * 4<N>, respectively.
1454 * (3) If the member is a three-component vector with components consuming
1455 * <N> basic machine units, the base alignment is 4<N>.
1457 if (this->is_array())
1458 return this->fields
.array
->std430_base_alignment(row_major
);
1460 /* (5) If the member is a column-major matrix with <C> columns and
1461 * <R> rows, the matrix is stored identically to an array of
1462 * <C> column vectors with <R> components each, according to
1465 * (7) If the member is a row-major matrix with <C> columns and <R>
1466 * rows, the matrix is stored identically to an array of <R>
1467 * row vectors with <C> components each, according to rule (4).
1469 if (this->is_matrix()) {
1470 const struct glsl_type
*vec_type
, *array_type
;
1471 int c
= this->matrix_columns
;
1472 int r
= this->vector_elements
;
1475 vec_type
= get_instance(base_type
, c
, 1);
1476 array_type
= glsl_type::get_array_instance(vec_type
, r
);
1478 vec_type
= get_instance(base_type
, r
, 1);
1479 array_type
= glsl_type::get_array_instance(vec_type
, c
);
1482 return array_type
->std430_base_alignment(false);
1485 /* (9) If the member is a structure, the base alignment of the
1486 * structure is <N>, where <N> is the largest base alignment
1487 * value of any of its members, and rounded up to the base
1488 * alignment of a vec4. The individual members of this
1489 * sub-structure are then assigned offsets by applying this set
1490 * of rules recursively, where the base offset of the first
1491 * member of the sub-structure is equal to the aligned offset
1492 * of the structure. The structure may have padding at the end;
1493 * the base offset of the member following the sub-structure is
1494 * rounded up to the next multiple of the base alignment of the
1497 if (this->is_record()) {
1498 unsigned base_alignment
= 0;
1499 for (unsigned i
= 0; i
< this->length
; i
++) {
1500 bool field_row_major
= row_major
;
1501 const enum glsl_matrix_layout matrix_layout
=
1502 glsl_matrix_layout(this->fields
.structure
[i
].matrix_layout
);
1503 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
1504 field_row_major
= true;
1505 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
1506 field_row_major
= false;
1509 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
1510 base_alignment
= MAX2(base_alignment
,
1511 field_type
->std430_base_alignment(field_row_major
));
1513 assert(base_alignment
> 0);
1514 return base_alignment
;
1516 assert(!"not reached");
1521 glsl_type::std430_array_stride(bool row_major
) const
1523 unsigned N
= is_double() ? 8 : 4;
1525 /* Notice that the array stride of a vec3 is not 3 * N but 4 * N.
1526 * See OpenGL 4.30 spec, section 7.6.2.2 "Standard Uniform Block Layout"
1528 * (3) If the member is a three-component vector with components consuming
1529 * <N> basic machine units, the base alignment is 4<N>.
1531 if (this->is_vector() && this->vector_elements
== 3)
1534 /* By default use std430_size(row_major) */
1535 return this->std430_size(row_major
);
1539 glsl_type::std430_size(bool row_major
) const
1541 unsigned N
= is_double() ? 8 : 4;
1543 /* OpenGL 4.30 spec, section 7.6.2.2 "Standard Uniform Block Layout":
1545 * "When using the std430 storage layout, shader storage blocks will be
1546 * laid out in buffer storage identically to uniform and shader storage
1547 * blocks using the std140 layout, except that the base alignment and
1548 * stride of arrays of scalars and vectors in rule 4 and of structures
1549 * in rule 9 are not rounded up a multiple of the base alignment of a vec4.
1551 if (this->is_scalar() || this->is_vector())
1552 return this->vector_elements
* N
;
1554 if (this->without_array()->is_matrix()) {
1555 const struct glsl_type
*element_type
;
1556 const struct glsl_type
*vec_type
;
1557 unsigned int array_len
;
1559 if (this->is_array()) {
1560 element_type
= this->fields
.array
;
1561 array_len
= this->length
;
1563 element_type
= this;
1568 vec_type
= get_instance(element_type
->base_type
,
1569 element_type
->matrix_columns
, 1);
1571 array_len
*= element_type
->vector_elements
;
1573 vec_type
= get_instance(element_type
->base_type
,
1574 element_type
->vector_elements
, 1);
1575 array_len
*= element_type
->matrix_columns
;
1577 const glsl_type
*array_type
= glsl_type::get_array_instance(vec_type
,
1580 return array_type
->std430_size(false);
1583 if (this->is_array()) {
1584 if (this->fields
.array
->is_record())
1585 return this->length
* this->fields
.array
->std430_size(row_major
);
1587 return this->length
* this->fields
.array
->std430_base_alignment(row_major
);
1590 if (this->is_record() || this->is_interface()) {
1592 unsigned max_align
= 0;
1594 for (unsigned i
= 0; i
< this->length
; i
++) {
1595 bool field_row_major
= row_major
;
1596 const enum glsl_matrix_layout matrix_layout
=
1597 glsl_matrix_layout(this->fields
.structure
[i
].matrix_layout
);
1598 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
1599 field_row_major
= true;
1600 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
1601 field_row_major
= false;
1604 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
1605 unsigned align
= field_type
->std430_base_alignment(field_row_major
);
1606 size
= glsl_align(size
, align
);
1607 size
+= field_type
->std430_size(field_row_major
);
1609 max_align
= MAX2(align
, max_align
);
1611 size
= glsl_align(size
, max_align
);
1615 assert(!"not reached");
1620 glsl_type::count_attribute_slots() const
1622 /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
1624 * "A scalar input counts the same amount against this limit as a vec4,
1625 * so applications may want to consider packing groups of four
1626 * unrelated float inputs together into a vector to better utilize the
1627 * capabilities of the underlying hardware. A matrix input will use up
1628 * multiple locations. The number of locations used will equal the
1629 * number of columns in the matrix."
1631 * The spec does not explicitly say how arrays are counted. However, it
1632 * should be safe to assume the total number of slots consumed by an array
1633 * is the number of entries in the array multiplied by the number of slots
1634 * consumed by a single element of the array.
1636 * The spec says nothing about how structs are counted, because vertex
1637 * attributes are not allowed to be (or contain) structs. However, Mesa
1638 * allows varying structs, the number of varying slots taken up by a
1639 * varying struct is simply equal to the sum of the number of slots taken
1640 * up by each element.
1642 switch (this->base_type
) {
1643 case GLSL_TYPE_UINT
:
1645 case GLSL_TYPE_FLOAT
:
1646 case GLSL_TYPE_BOOL
:
1647 case GLSL_TYPE_DOUBLE
:
1648 return this->matrix_columns
;
1650 case GLSL_TYPE_STRUCT
:
1651 case GLSL_TYPE_INTERFACE
: {
1654 for (unsigned i
= 0; i
< this->length
; i
++)
1655 size
+= this->fields
.structure
[i
].type
->count_attribute_slots();
1660 case GLSL_TYPE_ARRAY
:
1661 return this->length
* this->fields
.array
->count_attribute_slots();
1663 case GLSL_TYPE_SAMPLER
:
1664 case GLSL_TYPE_IMAGE
:
1665 case GLSL_TYPE_ATOMIC_UINT
:
1666 case GLSL_TYPE_VOID
:
1667 case GLSL_TYPE_SUBROUTINE
:
1668 case GLSL_TYPE_ERROR
:
1672 assert(!"Unexpected type in count_attribute_slots()");
1678 glsl_type::coordinate_components() const
1682 switch (sampler_dimensionality
) {
1683 case GLSL_SAMPLER_DIM_1D
:
1684 case GLSL_SAMPLER_DIM_BUF
:
1687 case GLSL_SAMPLER_DIM_2D
:
1688 case GLSL_SAMPLER_DIM_RECT
:
1689 case GLSL_SAMPLER_DIM_MS
:
1690 case GLSL_SAMPLER_DIM_EXTERNAL
:
1693 case GLSL_SAMPLER_DIM_3D
:
1694 case GLSL_SAMPLER_DIM_CUBE
:
1698 assert(!"Should not get here.");
1703 /* Array textures need an additional component for the array index, except
1704 * for cubemap array images that behave like a 2D array of interleaved
1707 if (sampler_array
&&
1708 !(base_type
== GLSL_TYPE_IMAGE
&&
1709 sampler_dimensionality
== GLSL_SAMPLER_DIM_CUBE
))