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::function_types
= NULL
;
36 hash_table
*glsl_type::subroutine_types
= NULL
;
37 void *glsl_type::mem_ctx
= NULL
;
40 glsl_type::init_ralloc_type_ctx(void)
42 if (glsl_type::mem_ctx
== NULL
) {
43 glsl_type::mem_ctx
= ralloc_autofree_context();
44 assert(glsl_type::mem_ctx
!= NULL
);
48 glsl_type::glsl_type(GLenum gl_type
,
49 glsl_base_type base_type
, unsigned vector_elements
,
50 unsigned matrix_columns
, const char *name
) :
53 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
54 sampler_type(0), interface_packing(0),
55 vector_elements(vector_elements
), matrix_columns(matrix_columns
),
58 mtx_lock(&glsl_type::mutex
);
60 init_ralloc_type_ctx();
62 this->name
= ralloc_strdup(this->mem_ctx
, name
);
64 mtx_unlock(&glsl_type::mutex
);
66 /* Neither dimension is zero or both dimensions are zero.
68 assert((vector_elements
== 0) == (matrix_columns
== 0));
69 memset(& fields
, 0, sizeof(fields
));
72 glsl_type::glsl_type(GLenum gl_type
, glsl_base_type base_type
,
73 enum glsl_sampler_dim dim
, bool shadow
, bool array
,
74 unsigned type
, const char *name
) :
77 sampler_dimensionality(dim
), sampler_shadow(shadow
),
78 sampler_array(array
), sampler_type(type
), interface_packing(0),
81 mtx_lock(&glsl_type::mutex
);
83 init_ralloc_type_ctx();
85 this->name
= ralloc_strdup(this->mem_ctx
, name
);
87 mtx_unlock(&glsl_type::mutex
);
89 memset(& fields
, 0, sizeof(fields
));
91 if (base_type
== GLSL_TYPE_SAMPLER
) {
92 /* Samplers take no storage whatsoever. */
93 matrix_columns
= vector_elements
= 0;
95 matrix_columns
= vector_elements
= 1;
99 glsl_type::glsl_type(const glsl_struct_field
*fields
, unsigned num_fields
,
102 base_type(GLSL_TYPE_STRUCT
),
103 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
104 sampler_type(0), interface_packing(0),
105 vector_elements(0), matrix_columns(0),
110 mtx_lock(&glsl_type::mutex
);
112 init_ralloc_type_ctx();
113 assert(name
!= NULL
);
114 this->name
= ralloc_strdup(this->mem_ctx
, name
);
115 this->fields
.structure
= ralloc_array(this->mem_ctx
,
116 glsl_struct_field
, length
);
118 for (i
= 0; i
< length
; i
++) {
119 this->fields
.structure
[i
].type
= fields
[i
].type
;
120 this->fields
.structure
[i
].name
= ralloc_strdup(this->fields
.structure
,
122 this->fields
.structure
[i
].location
= fields
[i
].location
;
123 this->fields
.structure
[i
].interpolation
= fields
[i
].interpolation
;
124 this->fields
.structure
[i
].centroid
= fields
[i
].centroid
;
125 this->fields
.structure
[i
].sample
= fields
[i
].sample
;
126 this->fields
.structure
[i
].matrix_layout
= fields
[i
].matrix_layout
;
127 this->fields
.structure
[i
].patch
= fields
[i
].patch
;
128 this->fields
.structure
[i
].image_read_only
= fields
[i
].image_read_only
;
129 this->fields
.structure
[i
].image_write_only
= fields
[i
].image_write_only
;
130 this->fields
.structure
[i
].image_coherent
= fields
[i
].image_coherent
;
131 this->fields
.structure
[i
].image_volatile
= fields
[i
].image_volatile
;
132 this->fields
.structure
[i
].image_restrict
= fields
[i
].image_restrict
;
135 mtx_unlock(&glsl_type::mutex
);
138 glsl_type::glsl_type(const glsl_struct_field
*fields
, unsigned num_fields
,
139 enum glsl_interface_packing packing
, const char *name
) :
141 base_type(GLSL_TYPE_INTERFACE
),
142 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
143 sampler_type(0), interface_packing((unsigned) packing
),
144 vector_elements(0), matrix_columns(0),
149 mtx_lock(&glsl_type::mutex
);
151 init_ralloc_type_ctx();
152 assert(name
!= NULL
);
153 this->name
= ralloc_strdup(this->mem_ctx
, name
);
154 this->fields
.structure
= ralloc_array(this->mem_ctx
,
155 glsl_struct_field
, length
);
156 for (i
= 0; i
< length
; i
++) {
157 this->fields
.structure
[i
].type
= fields
[i
].type
;
158 this->fields
.structure
[i
].name
= ralloc_strdup(this->fields
.structure
,
160 this->fields
.structure
[i
].location
= fields
[i
].location
;
161 this->fields
.structure
[i
].interpolation
= fields
[i
].interpolation
;
162 this->fields
.structure
[i
].centroid
= fields
[i
].centroid
;
163 this->fields
.structure
[i
].sample
= fields
[i
].sample
;
164 this->fields
.structure
[i
].matrix_layout
= fields
[i
].matrix_layout
;
165 this->fields
.structure
[i
].patch
= fields
[i
].patch
;
168 mtx_unlock(&glsl_type::mutex
);
171 glsl_type::glsl_type(const glsl_type
*return_type
,
172 const glsl_function_param
*params
, unsigned num_params
) :
174 base_type(GLSL_TYPE_FUNCTION
),
175 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
176 sampler_type(0), interface_packing(0),
177 vector_elements(0), matrix_columns(0),
182 mtx_lock(&glsl_type::mutex
);
184 init_ralloc_type_ctx();
186 this->fields
.parameters
= rzalloc_array(this->mem_ctx
,
187 glsl_function_param
, num_params
+ 1);
189 /* We store the return type as the first parameter */
190 this->fields
.parameters
[0].type
= return_type
;
191 this->fields
.parameters
[0].in
= false;
192 this->fields
.parameters
[0].out
= true;
194 /* We store the i'th parameter in slot i+1 */
195 for (i
= 0; i
< length
; i
++) {
196 this->fields
.parameters
[i
+ 1].type
= params
[i
].type
;
197 this->fields
.parameters
[i
+ 1].in
= params
[i
].in
;
198 this->fields
.parameters
[i
+ 1].out
= params
[i
].out
;
201 mtx_unlock(&glsl_type::mutex
);
204 glsl_type::glsl_type(const char *subroutine_name
) :
206 base_type(GLSL_TYPE_SUBROUTINE
),
207 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
208 sampler_type(0), interface_packing(0),
209 vector_elements(1), matrix_columns(1),
212 mtx_lock(&glsl_type::mutex
);
214 init_ralloc_type_ctx();
215 assert(subroutine_name
!= NULL
);
216 this->name
= ralloc_strdup(this->mem_ctx
, subroutine_name
);
217 mtx_unlock(&glsl_type::mutex
);
221 glsl_type::contains_sampler() const
223 if (this->is_array()) {
224 return this->fields
.array
->contains_sampler();
225 } else if (this->is_record()) {
226 for (unsigned int i
= 0; i
< this->length
; i
++) {
227 if (this->fields
.structure
[i
].type
->contains_sampler())
232 return this->is_sampler();
238 glsl_type::contains_integer() const
240 if (this->is_array()) {
241 return this->fields
.array
->contains_integer();
242 } else if (this->is_record()) {
243 for (unsigned int i
= 0; i
< this->length
; i
++) {
244 if (this->fields
.structure
[i
].type
->contains_integer())
249 return this->is_integer();
254 glsl_type::contains_double() const
256 if (this->is_array()) {
257 return this->fields
.array
->contains_double();
258 } else if (this->is_record()) {
259 for (unsigned int i
= 0; i
< this->length
; i
++) {
260 if (this->fields
.structure
[i
].type
->contains_double())
265 return this->is_double();
270 glsl_type::contains_opaque() const {
272 case GLSL_TYPE_SAMPLER
:
273 case GLSL_TYPE_IMAGE
:
274 case GLSL_TYPE_ATOMIC_UINT
:
276 case GLSL_TYPE_ARRAY
:
277 return fields
.array
->contains_opaque();
278 case GLSL_TYPE_STRUCT
:
279 for (unsigned int i
= 0; i
< length
; i
++) {
280 if (fields
.structure
[i
].type
->contains_opaque())
290 glsl_type::contains_subroutine() const
292 if (this->is_array()) {
293 return this->fields
.array
->contains_subroutine();
294 } else if (this->is_record()) {
295 for (unsigned int i
= 0; i
< this->length
; i
++) {
296 if (this->fields
.structure
[i
].type
->contains_subroutine())
301 return this->is_subroutine();
306 glsl_type::sampler_index() const
308 const glsl_type
*const t
= (this->is_array()) ? this->fields
.array
: this;
310 assert(t
->is_sampler());
312 switch (t
->sampler_dimensionality
) {
313 case GLSL_SAMPLER_DIM_1D
:
314 return (t
->sampler_array
) ? TEXTURE_1D_ARRAY_INDEX
: TEXTURE_1D_INDEX
;
315 case GLSL_SAMPLER_DIM_2D
:
316 return (t
->sampler_array
) ? TEXTURE_2D_ARRAY_INDEX
: TEXTURE_2D_INDEX
;
317 case GLSL_SAMPLER_DIM_3D
:
318 return TEXTURE_3D_INDEX
;
319 case GLSL_SAMPLER_DIM_CUBE
:
320 return (t
->sampler_array
) ? TEXTURE_CUBE_ARRAY_INDEX
: TEXTURE_CUBE_INDEX
;
321 case GLSL_SAMPLER_DIM_RECT
:
322 return TEXTURE_RECT_INDEX
;
323 case GLSL_SAMPLER_DIM_BUF
:
324 return TEXTURE_BUFFER_INDEX
;
325 case GLSL_SAMPLER_DIM_EXTERNAL
:
326 return TEXTURE_EXTERNAL_INDEX
;
327 case GLSL_SAMPLER_DIM_MS
:
328 return (t
->sampler_array
) ? TEXTURE_2D_MULTISAMPLE_ARRAY_INDEX
: TEXTURE_2D_MULTISAMPLE_INDEX
;
330 assert(!"Should not get here.");
331 return TEXTURE_BUFFER_INDEX
;
336 glsl_type::contains_image() const
338 if (this->is_array()) {
339 return this->fields
.array
->contains_image();
340 } else if (this->is_record()) {
341 for (unsigned int i
= 0; i
< this->length
; i
++) {
342 if (this->fields
.structure
[i
].type
->contains_image())
347 return this->is_image();
351 const glsl_type
*glsl_type::get_base_type() const
358 case GLSL_TYPE_FLOAT
:
360 case GLSL_TYPE_DOUBLE
:
370 const glsl_type
*glsl_type::get_scalar_type() const
372 const glsl_type
*type
= this;
375 while (type
->base_type
== GLSL_TYPE_ARRAY
)
376 type
= type
->fields
.array
;
378 /* Handle vectors and matrices */
379 switch (type
->base_type
) {
384 case GLSL_TYPE_FLOAT
:
386 case GLSL_TYPE_DOUBLE
:
391 /* Handle everything else */
398 _mesa_glsl_release_types(void)
400 /* Should only be called during atexit (either when unloading shared
401 * object, or if process terminates), so no mutex-locking should be
404 if (glsl_type::array_types
!= NULL
) {
405 _mesa_hash_table_destroy(glsl_type::array_types
, NULL
);
406 glsl_type::array_types
= NULL
;
409 if (glsl_type::record_types
!= NULL
) {
410 _mesa_hash_table_destroy(glsl_type::record_types
, NULL
);
411 glsl_type::record_types
= NULL
;
414 if (glsl_type::interface_types
!= NULL
) {
415 _mesa_hash_table_destroy(glsl_type::interface_types
, NULL
);
416 glsl_type::interface_types
= NULL
;
421 glsl_type::glsl_type(const glsl_type
*array
, unsigned length
) :
422 base_type(GLSL_TYPE_ARRAY
),
423 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
424 sampler_type(0), interface_packing(0),
425 vector_elements(0), matrix_columns(0),
426 length(length
), name(NULL
)
428 this->fields
.array
= array
;
429 /* Inherit the gl type of the base. The GL type is used for
430 * uniform/statevar handling in Mesa and the arrayness of the type
431 * is represented by the size rather than the type.
433 this->gl_type
= array
->gl_type
;
435 /* Allow a maximum of 10 characters for the array size. This is enough
436 * for 32-bits of ~0. The extra 3 are for the '[', ']', and terminating
439 const unsigned name_length
= strlen(array
->name
) + 10 + 3;
441 mtx_lock(&glsl_type::mutex
);
442 char *const n
= (char *) ralloc_size(this->mem_ctx
, name_length
);
443 mtx_unlock(&glsl_type::mutex
);
446 snprintf(n
, name_length
, "%s[]", array
->name
);
448 /* insert outermost dimensions in the correct spot
449 * otherwise the dimension order will be backwards
451 const char *pos
= strchr(array
->name
, '[');
453 int idx
= pos
- array
->name
;
454 snprintf(n
, idx
+1, "%s", array
->name
);
455 snprintf(n
+ idx
, name_length
- idx
, "[%u]%s",
456 length
, array
->name
+ idx
);
458 snprintf(n
, name_length
, "%s[%u]", array
->name
, length
);
467 glsl_type::vec(unsigned components
)
469 if (components
== 0 || components
> 4)
472 static const glsl_type
*const ts
[] = {
473 float_type
, vec2_type
, vec3_type
, vec4_type
475 return ts
[components
- 1];
479 glsl_type::dvec(unsigned components
)
481 if (components
== 0 || components
> 4)
484 static const glsl_type
*const ts
[] = {
485 double_type
, dvec2_type
, dvec3_type
, dvec4_type
487 return ts
[components
- 1];
491 glsl_type::ivec(unsigned components
)
493 if (components
== 0 || components
> 4)
496 static const glsl_type
*const ts
[] = {
497 int_type
, ivec2_type
, ivec3_type
, ivec4_type
499 return ts
[components
- 1];
504 glsl_type::uvec(unsigned components
)
506 if (components
== 0 || components
> 4)
509 static const glsl_type
*const ts
[] = {
510 uint_type
, uvec2_type
, uvec3_type
, uvec4_type
512 return ts
[components
- 1];
517 glsl_type::bvec(unsigned components
)
519 if (components
== 0 || components
> 4)
522 static const glsl_type
*const ts
[] = {
523 bool_type
, bvec2_type
, bvec3_type
, bvec4_type
525 return ts
[components
- 1];
530 glsl_type::get_instance(unsigned base_type
, unsigned rows
, unsigned columns
)
532 if (base_type
== GLSL_TYPE_VOID
)
535 if ((rows
< 1) || (rows
> 4) || (columns
< 1) || (columns
> 4))
538 /* Treat GLSL vectors as Nx1 matrices.
546 case GLSL_TYPE_FLOAT
:
548 case GLSL_TYPE_DOUBLE
:
556 if ((base_type
!= GLSL_TYPE_FLOAT
&& base_type
!= GLSL_TYPE_DOUBLE
) || (rows
== 1))
559 /* GLSL matrix types are named mat{COLUMNS}x{ROWS}. Only the following
560 * combinations are valid:
568 #define IDX(c,r) (((c-1)*3) + (r-1))
570 if (base_type
== GLSL_TYPE_DOUBLE
) {
571 switch (IDX(columns
, rows
)) {
572 case IDX(2,2): return dmat2_type
;
573 case IDX(2,3): return dmat2x3_type
;
574 case IDX(2,4): return dmat2x4_type
;
575 case IDX(3,2): return dmat3x2_type
;
576 case IDX(3,3): return dmat3_type
;
577 case IDX(3,4): return dmat3x4_type
;
578 case IDX(4,2): return dmat4x2_type
;
579 case IDX(4,3): return dmat4x3_type
;
580 case IDX(4,4): return dmat4_type
;
581 default: return error_type
;
584 switch (IDX(columns
, rows
)) {
585 case IDX(2,2): return mat2_type
;
586 case IDX(2,3): return mat2x3_type
;
587 case IDX(2,4): return mat2x4_type
;
588 case IDX(3,2): return mat3x2_type
;
589 case IDX(3,3): return mat3_type
;
590 case IDX(3,4): return mat3x4_type
;
591 case IDX(4,2): return mat4x2_type
;
592 case IDX(4,3): return mat4x3_type
;
593 case IDX(4,4): return mat4_type
;
594 default: return error_type
;
599 assert(!"Should not get here.");
604 glsl_type::get_sampler_instance(enum glsl_sampler_dim dim
,
610 case GLSL_TYPE_FLOAT
:
612 case GLSL_SAMPLER_DIM_1D
:
614 return (array
? sampler1DArrayShadow_type
: sampler1DShadow_type
);
616 return (array
? sampler1DArray_type
: sampler1D_type
);
617 case GLSL_SAMPLER_DIM_2D
:
619 return (array
? sampler2DArrayShadow_type
: sampler2DShadow_type
);
621 return (array
? sampler2DArray_type
: sampler2D_type
);
622 case GLSL_SAMPLER_DIM_3D
:
626 return sampler3D_type
;
627 case GLSL_SAMPLER_DIM_CUBE
:
629 return (array
? samplerCubeArrayShadow_type
: samplerCubeShadow_type
);
631 return (array
? samplerCubeArray_type
: samplerCube_type
);
632 case GLSL_SAMPLER_DIM_RECT
:
636 return sampler2DRectShadow_type
;
638 return sampler2DRect_type
;
639 case GLSL_SAMPLER_DIM_BUF
:
643 return samplerBuffer_type
;
644 case GLSL_SAMPLER_DIM_MS
:
647 return (array
? sampler2DMSArray_type
: sampler2DMS_type
);
648 case GLSL_SAMPLER_DIM_EXTERNAL
:
652 return samplerExternalOES_type
;
658 case GLSL_SAMPLER_DIM_1D
:
659 return (array
? isampler1DArray_type
: isampler1D_type
);
660 case GLSL_SAMPLER_DIM_2D
:
661 return (array
? isampler2DArray_type
: isampler2D_type
);
662 case GLSL_SAMPLER_DIM_3D
:
665 return isampler3D_type
;
666 case GLSL_SAMPLER_DIM_CUBE
:
667 return (array
? isamplerCubeArray_type
: isamplerCube_type
);
668 case GLSL_SAMPLER_DIM_RECT
:
671 return isampler2DRect_type
;
672 case GLSL_SAMPLER_DIM_BUF
:
675 return isamplerBuffer_type
;
676 case GLSL_SAMPLER_DIM_MS
:
677 return (array
? isampler2DMSArray_type
: isampler2DMS_type
);
678 case GLSL_SAMPLER_DIM_EXTERNAL
:
685 case GLSL_SAMPLER_DIM_1D
:
686 return (array
? usampler1DArray_type
: usampler1D_type
);
687 case GLSL_SAMPLER_DIM_2D
:
688 return (array
? usampler2DArray_type
: usampler2D_type
);
689 case GLSL_SAMPLER_DIM_3D
:
692 return usampler3D_type
;
693 case GLSL_SAMPLER_DIM_CUBE
:
694 return (array
? usamplerCubeArray_type
: usamplerCube_type
);
695 case GLSL_SAMPLER_DIM_RECT
:
698 return usampler2DRect_type
;
699 case GLSL_SAMPLER_DIM_BUF
:
702 return usamplerBuffer_type
;
703 case GLSL_SAMPLER_DIM_MS
:
704 return (array
? usampler2DMSArray_type
: usampler2DMS_type
);
705 case GLSL_SAMPLER_DIM_EXTERNAL
:
712 unreachable("switch statement above should be complete");
716 glsl_type::get_array_instance(const glsl_type
*base
, unsigned array_size
)
718 /* Generate a name using the base type pointer in the key. This is
719 * done because the name of the base type may not be unique across
720 * shaders. For example, two shaders may have different record types
724 snprintf(key
, sizeof(key
), "%p[%u]", (void *) base
, array_size
);
726 mtx_lock(&glsl_type::mutex
);
728 if (array_types
== NULL
) {
729 array_types
= _mesa_hash_table_create(NULL
, _mesa_key_hash_string
,
730 _mesa_key_string_equal
);
733 const struct hash_entry
*entry
= _mesa_hash_table_search(array_types
, key
);
735 mtx_unlock(&glsl_type::mutex
);
736 const glsl_type
*t
= new glsl_type(base
, array_size
);
737 mtx_lock(&glsl_type::mutex
);
739 entry
= _mesa_hash_table_insert(array_types
,
740 ralloc_strdup(mem_ctx
, key
),
744 assert(((glsl_type
*) entry
->data
)->base_type
== GLSL_TYPE_ARRAY
);
745 assert(((glsl_type
*) entry
->data
)->length
== array_size
);
746 assert(((glsl_type
*) entry
->data
)->fields
.array
== base
);
748 mtx_unlock(&glsl_type::mutex
);
750 return (glsl_type
*) entry
->data
;
755 glsl_type::record_compare(const glsl_type
*b
) const
757 if (this->length
!= b
->length
)
760 if (this->interface_packing
!= b
->interface_packing
)
763 /* From the GLSL 4.20 specification (Sec 4.2):
765 * "Structures must have the same name, sequence of type names, and
766 * type definitions, and field names to be considered the same type."
768 * GLSL ES behaves the same (Ver 1.00 Sec 4.2.4, Ver 3.00 Sec 4.2.5).
770 * Note that we cannot force type name check when comparing unnamed
771 * structure types, these have a unique name assigned during parsing.
773 if (!this->is_anonymous() && !b
->is_anonymous())
774 if (strcmp(this->name
, b
->name
) != 0)
777 for (unsigned i
= 0; i
< this->length
; i
++) {
778 if (this->fields
.structure
[i
].type
!= b
->fields
.structure
[i
].type
)
780 if (strcmp(this->fields
.structure
[i
].name
,
781 b
->fields
.structure
[i
].name
) != 0)
783 if (this->fields
.structure
[i
].matrix_layout
784 != b
->fields
.structure
[i
].matrix_layout
)
786 if (this->fields
.structure
[i
].location
787 != b
->fields
.structure
[i
].location
)
789 if (this->fields
.structure
[i
].interpolation
790 != b
->fields
.structure
[i
].interpolation
)
792 if (this->fields
.structure
[i
].centroid
793 != b
->fields
.structure
[i
].centroid
)
795 if (this->fields
.structure
[i
].sample
796 != b
->fields
.structure
[i
].sample
)
798 if (this->fields
.structure
[i
].patch
799 != b
->fields
.structure
[i
].patch
)
801 if (this->fields
.structure
[i
].image_read_only
802 != b
->fields
.structure
[i
].image_read_only
)
804 if (this->fields
.structure
[i
].image_write_only
805 != b
->fields
.structure
[i
].image_write_only
)
807 if (this->fields
.structure
[i
].image_coherent
808 != b
->fields
.structure
[i
].image_coherent
)
810 if (this->fields
.structure
[i
].image_volatile
811 != b
->fields
.structure
[i
].image_volatile
)
813 if (this->fields
.structure
[i
].image_restrict
814 != b
->fields
.structure
[i
].image_restrict
)
823 glsl_type::record_key_compare(const void *a
, const void *b
)
825 const glsl_type
*const key1
= (glsl_type
*) a
;
826 const glsl_type
*const key2
= (glsl_type
*) b
;
828 return strcmp(key1
->name
, key2
->name
) == 0 && key1
->record_compare(key2
);
833 * Generate an integer hash value for a glsl_type structure type.
836 glsl_type::record_key_hash(const void *a
)
838 const glsl_type
*const key
= (glsl_type
*) a
;
839 uintptr_t hash
= key
->length
;
842 for (unsigned i
= 0; i
< key
->length
; i
++) {
843 /* casting pointer to uintptr_t */
844 hash
= (hash
* 13 ) + (uintptr_t) key
->fields
.structure
[i
].type
;
847 if (sizeof(hash
) == 8)
848 retval
= (hash
& 0xffffffff) ^ ((uint64_t) hash
>> 32);
857 glsl_type::get_record_instance(const glsl_struct_field
*fields
,
861 const glsl_type
key(fields
, num_fields
, name
);
863 mtx_lock(&glsl_type::mutex
);
865 if (record_types
== NULL
) {
866 record_types
= _mesa_hash_table_create(NULL
, record_key_hash
,
870 const struct hash_entry
*entry
= _mesa_hash_table_search(record_types
,
873 mtx_unlock(&glsl_type::mutex
);
874 const glsl_type
*t
= new glsl_type(fields
, num_fields
, name
);
875 mtx_lock(&glsl_type::mutex
);
877 entry
= _mesa_hash_table_insert(record_types
, t
, (void *) t
);
880 assert(((glsl_type
*) entry
->data
)->base_type
== GLSL_TYPE_STRUCT
);
881 assert(((glsl_type
*) entry
->data
)->length
== num_fields
);
882 assert(strcmp(((glsl_type
*) entry
->data
)->name
, name
) == 0);
884 mtx_unlock(&glsl_type::mutex
);
886 return (glsl_type
*) entry
->data
;
891 glsl_type::get_interface_instance(const glsl_struct_field
*fields
,
893 enum glsl_interface_packing packing
,
894 const char *block_name
)
896 const glsl_type
key(fields
, num_fields
, packing
, block_name
);
898 mtx_lock(&glsl_type::mutex
);
900 if (interface_types
== NULL
) {
901 interface_types
= _mesa_hash_table_create(NULL
, record_key_hash
,
905 const struct hash_entry
*entry
= _mesa_hash_table_search(interface_types
,
908 mtx_unlock(&glsl_type::mutex
);
909 const glsl_type
*t
= new glsl_type(fields
, num_fields
,
910 packing
, block_name
);
911 mtx_lock(&glsl_type::mutex
);
913 entry
= _mesa_hash_table_insert(interface_types
, t
, (void *) t
);
916 assert(((glsl_type
*) entry
->data
)->base_type
== GLSL_TYPE_INTERFACE
);
917 assert(((glsl_type
*) entry
->data
)->length
== num_fields
);
918 assert(strcmp(((glsl_type
*) entry
->data
)->name
, block_name
) == 0);
920 mtx_unlock(&glsl_type::mutex
);
922 return (glsl_type
*) entry
->data
;
926 glsl_type::get_subroutine_instance(const char *subroutine_name
)
928 const glsl_type
key(subroutine_name
);
930 mtx_lock(&glsl_type::mutex
);
932 if (subroutine_types
== NULL
) {
933 subroutine_types
= _mesa_hash_table_create(NULL
, record_key_hash
,
937 const struct hash_entry
*entry
= _mesa_hash_table_search(subroutine_types
,
940 mtx_unlock(&glsl_type::mutex
);
941 const glsl_type
*t
= new glsl_type(subroutine_name
);
942 mtx_lock(&glsl_type::mutex
);
944 entry
= _mesa_hash_table_insert(subroutine_types
, t
, (void *) t
);
947 assert(((glsl_type
*) entry
->data
)->base_type
== GLSL_TYPE_SUBROUTINE
);
948 assert(strcmp(((glsl_type
*) entry
->data
)->name
, subroutine_name
) == 0);
950 mtx_unlock(&glsl_type::mutex
);
952 return (glsl_type
*) entry
->data
;
957 function_key_compare(const void *a
, const void *b
)
959 const glsl_type
*const key1
= (glsl_type
*) a
;
960 const glsl_type
*const key2
= (glsl_type
*) b
;
962 if (key1
->length
!= key2
->length
)
965 return memcmp(key1
->fields
.parameters
, key2
->fields
.parameters
,
966 (key1
->length
+ 1) * sizeof(*key1
->fields
.parameters
));
971 function_key_hash(const void *a
)
973 const glsl_type
*const key
= (glsl_type
*) a
;
977 size
= snprintf(hash_key
, sizeof(hash_key
), "%08x", key
->length
);
979 for (unsigned i
= 0; i
< key
->length
; i
++) {
980 if (size
>= sizeof(hash_key
))
983 size
+= snprintf(& hash_key
[size
], sizeof(hash_key
) - size
,
984 "%p", (void *) key
->fields
.structure
[i
].type
);
987 return _mesa_hash_string(hash_key
);
991 glsl_type::get_function_instance(const glsl_type
*return_type
,
992 const glsl_function_param
*params
,
995 const glsl_type
key(return_type
, params
, num_params
);
997 mtx_lock(&glsl_type::mutex
);
999 if (function_types
== NULL
) {
1000 function_types
= _mesa_hash_table_create(NULL
, function_key_hash
,
1001 function_key_compare
);
1004 struct hash_entry
*entry
= _mesa_hash_table_search(function_types
, &key
);
1005 if (entry
== NULL
) {
1006 mtx_unlock(&glsl_type::mutex
);
1007 const glsl_type
*t
= new glsl_type(return_type
, params
, num_params
);
1008 mtx_lock(&glsl_type::mutex
);
1010 entry
= _mesa_hash_table_insert(function_types
, t
, (void *) t
);
1013 const glsl_type
*t
= (const glsl_type
*)entry
->data
;
1015 assert(t
->base_type
== GLSL_TYPE_FUNCTION
);
1016 assert(t
->length
== num_params
);
1018 mtx_unlock(&glsl_type::mutex
);
1025 glsl_type::get_mul_type(const glsl_type
*type_a
, const glsl_type
*type_b
)
1027 if (type_a
== type_b
) {
1029 } else if (type_a
->is_matrix() && type_b
->is_matrix()) {
1030 /* Matrix multiply. The columns of A must match the rows of B. Given
1031 * the other previously tested constraints, this means the vector type
1032 * of a row from A must be the same as the vector type of a column from
1035 if (type_a
->row_type() == type_b
->column_type()) {
1036 /* The resulting matrix has the number of columns of matrix B and
1037 * the number of rows of matrix A. We get the row count of A by
1038 * looking at the size of a vector that makes up a column. The
1039 * transpose (size of a row) is done for B.
1041 const glsl_type
*const type
=
1042 get_instance(type_a
->base_type
,
1043 type_a
->column_type()->vector_elements
,
1044 type_b
->row_type()->vector_elements
);
1045 assert(type
!= error_type
);
1049 } else if (type_a
->is_matrix()) {
1050 /* A is a matrix and B is a column vector. Columns of A must match
1051 * rows of B. Given the other previously tested constraints, this
1052 * means the vector type of a row from A must be the same as the
1053 * vector the type of B.
1055 if (type_a
->row_type() == type_b
) {
1056 /* The resulting vector has a number of elements equal to
1057 * the number of rows of matrix A. */
1058 const glsl_type
*const type
=
1059 get_instance(type_a
->base_type
,
1060 type_a
->column_type()->vector_elements
,
1062 assert(type
!= error_type
);
1067 assert(type_b
->is_matrix());
1069 /* A is a row vector and B is a matrix. Columns of A must match rows
1070 * of B. Given the other previously tested constraints, this means
1071 * the type of A must be the same as the vector type of a column from
1074 if (type_a
== type_b
->column_type()) {
1075 /* The resulting vector has a number of elements equal to
1076 * the number of columns of matrix B. */
1077 const glsl_type
*const type
=
1078 get_instance(type_a
->base_type
,
1079 type_b
->row_type()->vector_elements
,
1081 assert(type
!= error_type
);
1092 glsl_type::field_type(const char *name
) const
1094 if (this->base_type
!= GLSL_TYPE_STRUCT
1095 && this->base_type
!= GLSL_TYPE_INTERFACE
)
1098 for (unsigned i
= 0; i
< this->length
; i
++) {
1099 if (strcmp(name
, this->fields
.structure
[i
].name
) == 0)
1100 return this->fields
.structure
[i
].type
;
1108 glsl_type::field_index(const char *name
) const
1110 if (this->base_type
!= GLSL_TYPE_STRUCT
1111 && this->base_type
!= GLSL_TYPE_INTERFACE
)
1114 for (unsigned i
= 0; i
< this->length
; i
++) {
1115 if (strcmp(name
, this->fields
.structure
[i
].name
) == 0)
1124 glsl_type::component_slots() const
1126 switch (this->base_type
) {
1127 case GLSL_TYPE_UINT
:
1129 case GLSL_TYPE_FLOAT
:
1130 case GLSL_TYPE_BOOL
:
1131 return this->components();
1133 case GLSL_TYPE_DOUBLE
:
1134 return 2 * this->components();
1136 case GLSL_TYPE_STRUCT
:
1137 case GLSL_TYPE_INTERFACE
: {
1140 for (unsigned i
= 0; i
< this->length
; i
++)
1141 size
+= this->fields
.structure
[i
].type
->component_slots();
1146 case GLSL_TYPE_ARRAY
:
1147 return this->length
* this->fields
.array
->component_slots();
1149 case GLSL_TYPE_IMAGE
:
1151 case GLSL_TYPE_SUBROUTINE
:
1154 case GLSL_TYPE_FUNCTION
:
1155 case GLSL_TYPE_SAMPLER
:
1156 case GLSL_TYPE_ATOMIC_UINT
:
1157 case GLSL_TYPE_VOID
:
1158 case GLSL_TYPE_ERROR
:
1166 glsl_type::record_location_offset(unsigned length
) const
1168 unsigned offset
= 0;
1169 const glsl_type
*t
= this->without_array();
1170 if (t
->is_record()) {
1171 assert(length
<= t
->length
);
1173 for (unsigned i
= 0; i
< length
; i
++) {
1174 const glsl_type
*st
= t
->fields
.structure
[i
].type
;
1175 const glsl_type
*wa
= st
->without_array();
1176 if (wa
->is_record()) {
1177 unsigned r_offset
= wa
->record_location_offset(wa
->length
);
1178 offset
+= st
->is_array() ? st
->length
* r_offset
: r_offset
;
1180 /* We dont worry about arrays here because unless the array
1181 * contains a structure or another array it only takes up a single
1192 glsl_type::uniform_locations() const
1196 switch (this->base_type
) {
1197 case GLSL_TYPE_UINT
:
1199 case GLSL_TYPE_FLOAT
:
1200 case GLSL_TYPE_DOUBLE
:
1201 case GLSL_TYPE_BOOL
:
1202 case GLSL_TYPE_SAMPLER
:
1203 case GLSL_TYPE_IMAGE
:
1204 case GLSL_TYPE_SUBROUTINE
:
1207 case GLSL_TYPE_STRUCT
:
1208 case GLSL_TYPE_INTERFACE
:
1209 for (unsigned i
= 0; i
< this->length
; i
++)
1210 size
+= this->fields
.structure
[i
].type
->uniform_locations();
1212 case GLSL_TYPE_ARRAY
:
1213 return this->length
* this->fields
.array
->uniform_locations();
1220 glsl_type::can_implicitly_convert_to(const glsl_type
*desired
,
1221 _mesa_glsl_parse_state
*state
) const
1223 if (this == desired
)
1226 /* There is no conversion among matrix types. */
1227 if (this->matrix_columns
> 1 || desired
->matrix_columns
> 1)
1230 /* Vector size must match. */
1231 if (this->vector_elements
!= desired
->vector_elements
)
1234 /* int and uint can be converted to float. */
1235 if (desired
->is_float() && this->is_integer())
1238 /* With GLSL 4.0 / ARB_gpu_shader5, int can be converted to uint.
1239 * Note that state may be NULL here, when resolving function calls in the
1240 * linker. By this time, all the state-dependent checks have already
1241 * happened though, so allow anything that's allowed in any shader version. */
1242 if ((!state
|| state
->is_version(400, 0) || state
->ARB_gpu_shader5_enable
) &&
1243 desired
->base_type
== GLSL_TYPE_UINT
&& this->base_type
== GLSL_TYPE_INT
)
1246 /* No implicit conversions from double. */
1247 if ((!state
|| state
->has_double()) && this->is_double())
1250 /* Conversions from different types to double. */
1251 if ((!state
|| state
->has_double()) && desired
->is_double()) {
1252 if (this->is_float())
1254 if (this->is_integer())
1262 glsl_type::std140_base_alignment(bool row_major
) const
1264 unsigned N
= is_double() ? 8 : 4;
1266 /* (1) If the member is a scalar consuming <N> basic machine units, the
1267 * base alignment is <N>.
1269 * (2) If the member is a two- or four-component vector with components
1270 * consuming <N> basic machine units, the base alignment is 2<N> or
1271 * 4<N>, respectively.
1273 * (3) If the member is a three-component vector with components consuming
1274 * <N> basic machine units, the base alignment is 4<N>.
1276 if (this->is_scalar() || this->is_vector()) {
1277 switch (this->vector_elements
) {
1288 /* (4) If the member is an array of scalars or vectors, the base alignment
1289 * and array stride are set to match the base alignment of a single
1290 * array element, according to rules (1), (2), and (3), and rounded up
1291 * to the base alignment of a vec4. The array may have padding at the
1292 * end; the base offset of the member following the array is rounded up
1293 * to the next multiple of the base alignment.
1295 * (6) If the member is an array of <S> column-major matrices with <C>
1296 * columns and <R> rows, the matrix is stored identically to a row of
1297 * <S>*<C> column vectors with <R> components each, according to rule
1300 * (8) If the member is an array of <S> row-major matrices with <C> columns
1301 * and <R> rows, the matrix is stored identically to a row of <S>*<R>
1302 * row vectors with <C> components each, according to rule (4).
1304 * (10) If the member is an array of <S> structures, the <S> elements of
1305 * the array are laid out in order, according to rule (9).
1307 if (this->is_array()) {
1308 if (this->fields
.array
->is_scalar() ||
1309 this->fields
.array
->is_vector() ||
1310 this->fields
.array
->is_matrix()) {
1311 return MAX2(this->fields
.array
->std140_base_alignment(row_major
), 16);
1313 assert(this->fields
.array
->is_record() ||
1314 this->fields
.array
->is_array());
1315 return this->fields
.array
->std140_base_alignment(row_major
);
1319 /* (5) If the member is a column-major matrix with <C> columns and
1320 * <R> rows, the matrix is stored identically to an array of
1321 * <C> column vectors with <R> components each, according to
1324 * (7) If the member is a row-major matrix with <C> columns and <R>
1325 * rows, the matrix is stored identically to an array of <R>
1326 * row vectors with <C> components each, according to rule (4).
1328 if (this->is_matrix()) {
1329 const struct glsl_type
*vec_type
, *array_type
;
1330 int c
= this->matrix_columns
;
1331 int r
= this->vector_elements
;
1334 vec_type
= get_instance(base_type
, c
, 1);
1335 array_type
= glsl_type::get_array_instance(vec_type
, r
);
1337 vec_type
= get_instance(base_type
, r
, 1);
1338 array_type
= glsl_type::get_array_instance(vec_type
, c
);
1341 return array_type
->std140_base_alignment(false);
1344 /* (9) If the member is a structure, the base alignment of the
1345 * structure is <N>, where <N> is the largest base alignment
1346 * value of any of its members, and rounded up to the base
1347 * alignment of a vec4. The individual members of this
1348 * sub-structure are then assigned offsets by applying this set
1349 * of rules recursively, where the base offset of the first
1350 * member of the sub-structure is equal to the aligned offset
1351 * of the structure. The structure may have padding at the end;
1352 * the base offset of the member following the sub-structure is
1353 * rounded up to the next multiple of the base alignment of the
1356 if (this->is_record()) {
1357 unsigned base_alignment
= 16;
1358 for (unsigned i
= 0; i
< this->length
; i
++) {
1359 bool field_row_major
= row_major
;
1360 const enum glsl_matrix_layout matrix_layout
=
1361 glsl_matrix_layout(this->fields
.structure
[i
].matrix_layout
);
1362 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
1363 field_row_major
= true;
1364 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
1365 field_row_major
= false;
1368 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
1369 base_alignment
= MAX2(base_alignment
,
1370 field_type
->std140_base_alignment(field_row_major
));
1372 return base_alignment
;
1375 assert(!"not reached");
1380 glsl_type::std140_size(bool row_major
) const
1382 unsigned N
= is_double() ? 8 : 4;
1384 /* (1) If the member is a scalar consuming <N> basic machine units, the
1385 * base alignment is <N>.
1387 * (2) If the member is a two- or four-component vector with components
1388 * consuming <N> basic machine units, the base alignment is 2<N> or
1389 * 4<N>, respectively.
1391 * (3) If the member is a three-component vector with components consuming
1392 * <N> basic machine units, the base alignment is 4<N>.
1394 if (this->is_scalar() || this->is_vector()) {
1395 return this->vector_elements
* N
;
1398 /* (5) If the member is a column-major matrix with <C> columns and
1399 * <R> rows, the matrix is stored identically to an array of
1400 * <C> column vectors with <R> components each, according to
1403 * (6) If the member is an array of <S> column-major matrices with <C>
1404 * columns and <R> rows, the matrix is stored identically to a row of
1405 * <S>*<C> column vectors with <R> components each, according to rule
1408 * (7) If the member is a row-major matrix with <C> columns and <R>
1409 * rows, the matrix is stored identically to an array of <R>
1410 * row vectors with <C> components each, according to rule (4).
1412 * (8) If the member is an array of <S> row-major matrices with <C> columns
1413 * and <R> rows, the matrix is stored identically to a row of <S>*<R>
1414 * row vectors with <C> components each, according to rule (4).
1416 if (this->without_array()->is_matrix()) {
1417 const struct glsl_type
*element_type
;
1418 const struct glsl_type
*vec_type
;
1419 unsigned int array_len
;
1421 if (this->is_array()) {
1422 element_type
= this->fields
.array
;
1423 array_len
= this->length
;
1425 element_type
= this;
1430 vec_type
= get_instance(element_type
->base_type
,
1431 element_type
->matrix_columns
, 1);
1433 array_len
*= element_type
->vector_elements
;
1435 vec_type
= get_instance(element_type
->base_type
,
1436 element_type
->vector_elements
, 1);
1437 array_len
*= element_type
->matrix_columns
;
1439 const glsl_type
*array_type
= glsl_type::get_array_instance(vec_type
,
1442 return array_type
->std140_size(false);
1445 /* (4) If the member is an array of scalars or vectors, the base alignment
1446 * and array stride are set to match the base alignment of a single
1447 * array element, according to rules (1), (2), and (3), and rounded up
1448 * to the base alignment of a vec4. The array may have padding at the
1449 * end; the base offset of the member following the array is rounded up
1450 * to the next multiple of the base alignment.
1452 * (10) If the member is an array of <S> structures, the <S> elements of
1453 * the array are laid out in order, according to rule (9).
1455 if (this->is_array()) {
1456 if (this->fields
.array
->is_record()) {
1457 return this->length
* this->fields
.array
->std140_size(row_major
);
1459 unsigned element_base_align
=
1460 this->fields
.array
->std140_base_alignment(row_major
);
1461 return this->length
* MAX2(element_base_align
, 16);
1465 /* (9) If the member is a structure, the base alignment of the
1466 * structure is <N>, where <N> is the largest base alignment
1467 * value of any of its members, and rounded up to the base
1468 * alignment of a vec4. The individual members of this
1469 * sub-structure are then assigned offsets by applying this set
1470 * of rules recursively, where the base offset of the first
1471 * member of the sub-structure is equal to the aligned offset
1472 * of the structure. The structure may have padding at the end;
1473 * the base offset of the member following the sub-structure is
1474 * rounded up to the next multiple of the base alignment of the
1477 if (this->is_record() || this->is_interface()) {
1479 unsigned max_align
= 0;
1481 for (unsigned i
= 0; i
< this->length
; i
++) {
1482 bool field_row_major
= row_major
;
1483 const enum glsl_matrix_layout matrix_layout
=
1484 glsl_matrix_layout(this->fields
.structure
[i
].matrix_layout
);
1485 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
1486 field_row_major
= true;
1487 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
1488 field_row_major
= false;
1491 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
1492 unsigned align
= field_type
->std140_base_alignment(field_row_major
);
1494 /* Ignore unsized arrays when calculating size */
1495 if (field_type
->is_unsized_array())
1498 size
= glsl_align(size
, align
);
1499 size
+= field_type
->std140_size(field_row_major
);
1501 max_align
= MAX2(align
, max_align
);
1503 if (field_type
->is_record() && (i
+ 1 < this->length
))
1504 size
= glsl_align(size
, 16);
1506 size
= glsl_align(size
, MAX2(max_align
, 16));
1510 assert(!"not reached");
1515 glsl_type::std430_base_alignment(bool row_major
) const
1518 unsigned N
= is_double() ? 8 : 4;
1520 /* (1) If the member is a scalar consuming <N> basic machine units, the
1521 * base alignment is <N>.
1523 * (2) If the member is a two- or four-component vector with components
1524 * consuming <N> basic machine units, the base alignment is 2<N> or
1525 * 4<N>, respectively.
1527 * (3) If the member is a three-component vector with components consuming
1528 * <N> basic machine units, the base alignment is 4<N>.
1530 if (this->is_scalar() || this->is_vector()) {
1531 switch (this->vector_elements
) {
1542 /* OpenGL 4.30 spec, section 7.6.2.2 "Standard Uniform Block Layout":
1544 * "When using the std430 storage layout, shader storage blocks will be
1545 * laid out in buffer storage identically to uniform and shader storage
1546 * blocks using the std140 layout, except that the base alignment and
1547 * stride of arrays of scalars and vectors in rule 4 and of structures
1548 * in rule 9 are not rounded up a multiple of the base alignment of a vec4.
1551 /* (1) If the member is a scalar consuming <N> basic machine units, the
1552 * base alignment is <N>.
1554 * (2) If the member is a two- or four-component vector with components
1555 * consuming <N> basic machine units, the base alignment is 2<N> or
1556 * 4<N>, respectively.
1558 * (3) If the member is a three-component vector with components consuming
1559 * <N> basic machine units, the base alignment is 4<N>.
1561 if (this->is_array())
1562 return this->fields
.array
->std430_base_alignment(row_major
);
1564 /* (5) If the member is a column-major matrix with <C> columns and
1565 * <R> rows, the matrix is stored identically to an array of
1566 * <C> column vectors with <R> components each, according to
1569 * (7) If the member is a row-major matrix with <C> columns and <R>
1570 * rows, the matrix is stored identically to an array of <R>
1571 * row vectors with <C> components each, according to rule (4).
1573 if (this->is_matrix()) {
1574 const struct glsl_type
*vec_type
, *array_type
;
1575 int c
= this->matrix_columns
;
1576 int r
= this->vector_elements
;
1579 vec_type
= get_instance(base_type
, c
, 1);
1580 array_type
= glsl_type::get_array_instance(vec_type
, r
);
1582 vec_type
= get_instance(base_type
, r
, 1);
1583 array_type
= glsl_type::get_array_instance(vec_type
, c
);
1586 return array_type
->std430_base_alignment(false);
1589 /* (9) If the member is a structure, the base alignment of the
1590 * structure is <N>, where <N> is the largest base alignment
1591 * value of any of its members, and rounded up to the base
1592 * alignment of a vec4. The individual members of this
1593 * sub-structure are then assigned offsets by applying this set
1594 * of rules recursively, where the base offset of the first
1595 * member of the sub-structure is equal to the aligned offset
1596 * of the structure. The structure may have padding at the end;
1597 * the base offset of the member following the sub-structure is
1598 * rounded up to the next multiple of the base alignment of the
1601 if (this->is_record()) {
1602 unsigned base_alignment
= 0;
1603 for (unsigned i
= 0; i
< this->length
; i
++) {
1604 bool field_row_major
= row_major
;
1605 const enum glsl_matrix_layout matrix_layout
=
1606 glsl_matrix_layout(this->fields
.structure
[i
].matrix_layout
);
1607 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
1608 field_row_major
= true;
1609 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
1610 field_row_major
= false;
1613 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
1614 base_alignment
= MAX2(base_alignment
,
1615 field_type
->std430_base_alignment(field_row_major
));
1617 assert(base_alignment
> 0);
1618 return base_alignment
;
1620 assert(!"not reached");
1625 glsl_type::std430_array_stride(bool row_major
) const
1627 unsigned N
= is_double() ? 8 : 4;
1629 /* Notice that the array stride of a vec3 is not 3 * N but 4 * N.
1630 * See OpenGL 4.30 spec, section 7.6.2.2 "Standard Uniform Block Layout"
1632 * (3) If the member is a three-component vector with components consuming
1633 * <N> basic machine units, the base alignment is 4<N>.
1635 if (this->is_vector() && this->vector_elements
== 3)
1638 /* By default use std430_size(row_major) */
1639 return this->std430_size(row_major
);
1643 glsl_type::std430_size(bool row_major
) const
1645 unsigned N
= is_double() ? 8 : 4;
1647 /* OpenGL 4.30 spec, section 7.6.2.2 "Standard Uniform Block Layout":
1649 * "When using the std430 storage layout, shader storage blocks will be
1650 * laid out in buffer storage identically to uniform and shader storage
1651 * blocks using the std140 layout, except that the base alignment and
1652 * stride of arrays of scalars and vectors in rule 4 and of structures
1653 * in rule 9 are not rounded up a multiple of the base alignment of a vec4.
1655 if (this->is_scalar() || this->is_vector())
1656 return this->vector_elements
* N
;
1658 if (this->without_array()->is_matrix()) {
1659 const struct glsl_type
*element_type
;
1660 const struct glsl_type
*vec_type
;
1661 unsigned int array_len
;
1663 if (this->is_array()) {
1664 element_type
= this->fields
.array
;
1665 array_len
= this->length
;
1667 element_type
= this;
1672 vec_type
= get_instance(element_type
->base_type
,
1673 element_type
->matrix_columns
, 1);
1675 array_len
*= element_type
->vector_elements
;
1677 vec_type
= get_instance(element_type
->base_type
,
1678 element_type
->vector_elements
, 1);
1679 array_len
*= element_type
->matrix_columns
;
1681 const glsl_type
*array_type
= glsl_type::get_array_instance(vec_type
,
1684 return array_type
->std430_size(false);
1687 if (this->is_array()) {
1688 if (this->fields
.array
->is_record())
1689 return this->length
* this->fields
.array
->std430_size(row_major
);
1691 return this->length
* this->fields
.array
->std430_base_alignment(row_major
);
1694 if (this->is_record() || this->is_interface()) {
1696 unsigned max_align
= 0;
1698 for (unsigned i
= 0; i
< this->length
; i
++) {
1699 bool field_row_major
= row_major
;
1700 const enum glsl_matrix_layout matrix_layout
=
1701 glsl_matrix_layout(this->fields
.structure
[i
].matrix_layout
);
1702 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
1703 field_row_major
= true;
1704 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
1705 field_row_major
= false;
1708 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
1709 unsigned align
= field_type
->std430_base_alignment(field_row_major
);
1710 size
= glsl_align(size
, align
);
1711 size
+= field_type
->std430_size(field_row_major
);
1713 max_align
= MAX2(align
, max_align
);
1715 size
= glsl_align(size
, max_align
);
1719 assert(!"not reached");
1724 glsl_type::count_attribute_slots() const
1726 /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
1728 * "A scalar input counts the same amount against this limit as a vec4,
1729 * so applications may want to consider packing groups of four
1730 * unrelated float inputs together into a vector to better utilize the
1731 * capabilities of the underlying hardware. A matrix input will use up
1732 * multiple locations. The number of locations used will equal the
1733 * number of columns in the matrix."
1735 * The spec does not explicitly say how arrays are counted. However, it
1736 * should be safe to assume the total number of slots consumed by an array
1737 * is the number of entries in the array multiplied by the number of slots
1738 * consumed by a single element of the array.
1740 * The spec says nothing about how structs are counted, because vertex
1741 * attributes are not allowed to be (or contain) structs. However, Mesa
1742 * allows varying structs, the number of varying slots taken up by a
1743 * varying struct is simply equal to the sum of the number of slots taken
1744 * up by each element.
1746 switch (this->base_type
) {
1747 case GLSL_TYPE_UINT
:
1749 case GLSL_TYPE_FLOAT
:
1750 case GLSL_TYPE_BOOL
:
1751 case GLSL_TYPE_DOUBLE
:
1752 return this->matrix_columns
;
1754 case GLSL_TYPE_STRUCT
:
1755 case GLSL_TYPE_INTERFACE
: {
1758 for (unsigned i
= 0; i
< this->length
; i
++)
1759 size
+= this->fields
.structure
[i
].type
->count_attribute_slots();
1764 case GLSL_TYPE_ARRAY
:
1765 return this->length
* this->fields
.array
->count_attribute_slots();
1767 case GLSL_TYPE_FUNCTION
:
1768 case GLSL_TYPE_SAMPLER
:
1769 case GLSL_TYPE_IMAGE
:
1770 case GLSL_TYPE_ATOMIC_UINT
:
1771 case GLSL_TYPE_VOID
:
1772 case GLSL_TYPE_SUBROUTINE
:
1773 case GLSL_TYPE_ERROR
:
1777 assert(!"Unexpected type in count_attribute_slots()");
1783 glsl_type::coordinate_components() const
1787 switch (sampler_dimensionality
) {
1788 case GLSL_SAMPLER_DIM_1D
:
1789 case GLSL_SAMPLER_DIM_BUF
:
1792 case GLSL_SAMPLER_DIM_2D
:
1793 case GLSL_SAMPLER_DIM_RECT
:
1794 case GLSL_SAMPLER_DIM_MS
:
1795 case GLSL_SAMPLER_DIM_EXTERNAL
:
1798 case GLSL_SAMPLER_DIM_3D
:
1799 case GLSL_SAMPLER_DIM_CUBE
:
1803 assert(!"Should not get here.");
1808 /* Array textures need an additional component for the array index, except
1809 * for cubemap array images that behave like a 2D array of interleaved
1812 if (sampler_array
&&
1813 !(base_type
== GLSL_TYPE_IMAGE
&&
1814 sampler_dimensionality
== GLSL_SAMPLER_DIM_CUBE
))