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/macros.h"
26 #include "compiler/glsl/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_context(NULL
);
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 sampled_type(0), interface_packing(0), interface_row_major(0),
55 vector_elements(vector_elements
), matrix_columns(matrix_columns
),
58 /* Values of these types must fit in the two bits of
59 * glsl_type::sampled_type.
61 STATIC_ASSERT((unsigned(GLSL_TYPE_UINT
) & 3) == unsigned(GLSL_TYPE_UINT
));
62 STATIC_ASSERT((unsigned(GLSL_TYPE_INT
) & 3) == unsigned(GLSL_TYPE_INT
));
63 STATIC_ASSERT((unsigned(GLSL_TYPE_FLOAT
) & 3) == unsigned(GLSL_TYPE_FLOAT
));
65 mtx_lock(&glsl_type::mutex
);
67 init_ralloc_type_ctx();
69 this->name
= ralloc_strdup(this->mem_ctx
, name
);
71 mtx_unlock(&glsl_type::mutex
);
73 /* Neither dimension is zero or both dimensions are zero.
75 assert((vector_elements
== 0) == (matrix_columns
== 0));
76 memset(& fields
, 0, sizeof(fields
));
79 glsl_type::glsl_type(GLenum gl_type
, glsl_base_type base_type
,
80 enum glsl_sampler_dim dim
, bool shadow
, bool array
,
81 unsigned type
, const char *name
) :
84 sampler_dimensionality(dim
), sampler_shadow(shadow
),
85 sampler_array(array
), sampled_type(type
), interface_packing(0),
86 interface_row_major(0), length(0)
88 mtx_lock(&glsl_type::mutex
);
90 init_ralloc_type_ctx();
92 this->name
= ralloc_strdup(this->mem_ctx
, name
);
94 mtx_unlock(&glsl_type::mutex
);
96 memset(& fields
, 0, sizeof(fields
));
98 matrix_columns
= vector_elements
= 1;
101 glsl_type::glsl_type(const glsl_struct_field
*fields
, unsigned num_fields
,
104 base_type(GLSL_TYPE_STRUCT
),
105 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
106 sampled_type(0), interface_packing(0), interface_row_major(0),
107 vector_elements(0), matrix_columns(0),
112 mtx_lock(&glsl_type::mutex
);
114 init_ralloc_type_ctx();
115 assert(name
!= NULL
);
116 this->name
= ralloc_strdup(this->mem_ctx
, name
);
117 this->fields
.structure
= ralloc_array(this->mem_ctx
,
118 glsl_struct_field
, length
);
120 for (i
= 0; i
< length
; i
++) {
121 this->fields
.structure
[i
] = fields
[i
];
122 this->fields
.structure
[i
].name
= ralloc_strdup(this->fields
.structure
,
126 mtx_unlock(&glsl_type::mutex
);
129 glsl_type::glsl_type(const glsl_struct_field
*fields
, unsigned num_fields
,
130 enum glsl_interface_packing packing
,
131 bool row_major
, const char *name
) :
133 base_type(GLSL_TYPE_INTERFACE
),
134 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
135 sampled_type(0), interface_packing((unsigned) packing
),
136 interface_row_major((unsigned) row_major
),
137 vector_elements(0), matrix_columns(0),
142 mtx_lock(&glsl_type::mutex
);
144 init_ralloc_type_ctx();
145 assert(name
!= NULL
);
146 this->name
= ralloc_strdup(this->mem_ctx
, name
);
147 this->fields
.structure
= rzalloc_array(this->mem_ctx
,
148 glsl_struct_field
, length
);
149 for (i
= 0; i
< length
; i
++) {
150 this->fields
.structure
[i
] = fields
[i
];
151 this->fields
.structure
[i
].name
= ralloc_strdup(this->fields
.structure
,
155 mtx_unlock(&glsl_type::mutex
);
158 glsl_type::glsl_type(const glsl_type
*return_type
,
159 const glsl_function_param
*params
, unsigned num_params
) :
161 base_type(GLSL_TYPE_FUNCTION
),
162 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
163 sampled_type(0), interface_packing(0), interface_row_major(0),
164 vector_elements(0), matrix_columns(0),
169 mtx_lock(&glsl_type::mutex
);
171 init_ralloc_type_ctx();
173 this->fields
.parameters
= rzalloc_array(this->mem_ctx
,
174 glsl_function_param
, num_params
+ 1);
176 /* We store the return type as the first parameter */
177 this->fields
.parameters
[0].type
= return_type
;
178 this->fields
.parameters
[0].in
= false;
179 this->fields
.parameters
[0].out
= true;
181 /* We store the i'th parameter in slot i+1 */
182 for (i
= 0; i
< length
; i
++) {
183 this->fields
.parameters
[i
+ 1].type
= params
[i
].type
;
184 this->fields
.parameters
[i
+ 1].in
= params
[i
].in
;
185 this->fields
.parameters
[i
+ 1].out
= params
[i
].out
;
188 mtx_unlock(&glsl_type::mutex
);
191 glsl_type::glsl_type(const char *subroutine_name
) :
193 base_type(GLSL_TYPE_SUBROUTINE
),
194 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
195 sampled_type(0), interface_packing(0), interface_row_major(0),
196 vector_elements(1), matrix_columns(1),
199 mtx_lock(&glsl_type::mutex
);
201 init_ralloc_type_ctx();
202 assert(subroutine_name
!= NULL
);
203 this->name
= ralloc_strdup(this->mem_ctx
, subroutine_name
);
204 mtx_unlock(&glsl_type::mutex
);
208 glsl_type::contains_sampler() const
210 if (this->is_array()) {
211 return this->fields
.array
->contains_sampler();
212 } else if (this->is_record() || this->is_interface()) {
213 for (unsigned int i
= 0; i
< this->length
; i
++) {
214 if (this->fields
.structure
[i
].type
->contains_sampler())
219 return this->is_sampler();
225 glsl_type::contains_integer() const
227 if (this->is_array()) {
228 return this->fields
.array
->contains_integer();
229 } else if (this->is_record() || this->is_interface()) {
230 for (unsigned int i
= 0; i
< this->length
; i
++) {
231 if (this->fields
.structure
[i
].type
->contains_integer())
236 return this->is_integer();
241 glsl_type::contains_double() const
243 if (this->is_array()) {
244 return this->fields
.array
->contains_double();
245 } else if (this->is_record() || this->is_interface()) {
246 for (unsigned int i
= 0; i
< this->length
; i
++) {
247 if (this->fields
.structure
[i
].type
->contains_double())
252 return this->is_double();
257 glsl_type::contains_opaque() const {
259 case GLSL_TYPE_SAMPLER
:
260 case GLSL_TYPE_IMAGE
:
261 case GLSL_TYPE_ATOMIC_UINT
:
263 case GLSL_TYPE_ARRAY
:
264 return fields
.array
->contains_opaque();
265 case GLSL_TYPE_STRUCT
:
266 case GLSL_TYPE_INTERFACE
:
267 for (unsigned int i
= 0; i
< length
; i
++) {
268 if (fields
.structure
[i
].type
->contains_opaque())
278 glsl_type::contains_subroutine() const
280 if (this->is_array()) {
281 return this->fields
.array
->contains_subroutine();
282 } else if (this->is_record() || this->is_interface()) {
283 for (unsigned int i
= 0; i
< this->length
; i
++) {
284 if (this->fields
.structure
[i
].type
->contains_subroutine())
289 return this->is_subroutine();
294 glsl_type::sampler_index() const
296 const glsl_type
*const t
= (this->is_array()) ? this->fields
.array
: this;
298 assert(t
->is_sampler() || t
->is_image());
300 switch (t
->sampler_dimensionality
) {
301 case GLSL_SAMPLER_DIM_1D
:
302 return (t
->sampler_array
) ? TEXTURE_1D_ARRAY_INDEX
: TEXTURE_1D_INDEX
;
303 case GLSL_SAMPLER_DIM_2D
:
304 return (t
->sampler_array
) ? TEXTURE_2D_ARRAY_INDEX
: TEXTURE_2D_INDEX
;
305 case GLSL_SAMPLER_DIM_3D
:
306 return TEXTURE_3D_INDEX
;
307 case GLSL_SAMPLER_DIM_CUBE
:
308 return (t
->sampler_array
) ? TEXTURE_CUBE_ARRAY_INDEX
: TEXTURE_CUBE_INDEX
;
309 case GLSL_SAMPLER_DIM_RECT
:
310 return TEXTURE_RECT_INDEX
;
311 case GLSL_SAMPLER_DIM_BUF
:
312 return TEXTURE_BUFFER_INDEX
;
313 case GLSL_SAMPLER_DIM_EXTERNAL
:
314 return TEXTURE_EXTERNAL_INDEX
;
315 case GLSL_SAMPLER_DIM_MS
:
316 return (t
->sampler_array
) ? TEXTURE_2D_MULTISAMPLE_ARRAY_INDEX
: TEXTURE_2D_MULTISAMPLE_INDEX
;
318 assert(!"Should not get here.");
319 return TEXTURE_BUFFER_INDEX
;
324 glsl_type::contains_image() const
326 if (this->is_array()) {
327 return this->fields
.array
->contains_image();
328 } else if (this->is_record() || this->is_interface()) {
329 for (unsigned int i
= 0; i
< this->length
; i
++) {
330 if (this->fields
.structure
[i
].type
->contains_image())
335 return this->is_image();
339 const glsl_type
*glsl_type::get_base_type() const
346 case GLSL_TYPE_FLOAT
:
348 case GLSL_TYPE_DOUBLE
:
352 case GLSL_TYPE_UINT64
:
353 return uint64_t_type
;
354 case GLSL_TYPE_INT64
:
362 const glsl_type
*glsl_type::get_scalar_type() const
364 const glsl_type
*type
= this;
367 while (type
->base_type
== GLSL_TYPE_ARRAY
)
368 type
= type
->fields
.array
;
370 /* Handle vectors and matrices */
371 switch (type
->base_type
) {
376 case GLSL_TYPE_FLOAT
:
378 case GLSL_TYPE_DOUBLE
:
382 case GLSL_TYPE_UINT64
:
383 return uint64_t_type
;
384 case GLSL_TYPE_INT64
:
387 /* Handle everything else */
394 _mesa_glsl_release_types(void)
396 /* Should only be called during atexit (either when unloading shared
397 * object, or if process terminates), so no mutex-locking should be
400 if (glsl_type::array_types
!= NULL
) {
401 _mesa_hash_table_destroy(glsl_type::array_types
, NULL
);
402 glsl_type::array_types
= NULL
;
405 if (glsl_type::record_types
!= NULL
) {
406 _mesa_hash_table_destroy(glsl_type::record_types
, NULL
);
407 glsl_type::record_types
= NULL
;
410 if (glsl_type::interface_types
!= NULL
) {
411 _mesa_hash_table_destroy(glsl_type::interface_types
, NULL
);
412 glsl_type::interface_types
= NULL
;
415 if (glsl_type::function_types
!= NULL
) {
416 _mesa_hash_table_destroy(glsl_type::function_types
, NULL
);
417 glsl_type::function_types
= NULL
;
420 if (glsl_type::subroutine_types
!= NULL
) {
421 _mesa_hash_table_destroy(glsl_type::subroutine_types
, NULL
);
422 glsl_type::subroutine_types
= NULL
;
425 ralloc_free(glsl_type::mem_ctx
);
426 glsl_type::mem_ctx
= NULL
;
430 glsl_type::glsl_type(const glsl_type
*array
, unsigned length
) :
431 base_type(GLSL_TYPE_ARRAY
),
432 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
433 sampled_type(0), interface_packing(0), interface_row_major(0),
434 vector_elements(0), matrix_columns(0),
435 length(length
), name(NULL
)
437 this->fields
.array
= array
;
438 /* Inherit the gl type of the base. The GL type is used for
439 * uniform/statevar handling in Mesa and the arrayness of the type
440 * is represented by the size rather than the type.
442 this->gl_type
= array
->gl_type
;
444 /* Allow a maximum of 10 characters for the array size. This is enough
445 * for 32-bits of ~0. The extra 3 are for the '[', ']', and terminating
448 const unsigned name_length
= strlen(array
->name
) + 10 + 3;
450 mtx_lock(&glsl_type::mutex
);
451 char *const n
= (char *) ralloc_size(this->mem_ctx
, name_length
);
452 mtx_unlock(&glsl_type::mutex
);
455 snprintf(n
, name_length
, "%s[]", array
->name
);
457 /* insert outermost dimensions in the correct spot
458 * otherwise the dimension order will be backwards
460 const char *pos
= strchr(array
->name
, '[');
462 int idx
= pos
- array
->name
;
463 snprintf(n
, idx
+1, "%s", array
->name
);
464 snprintf(n
+ idx
, name_length
- idx
, "[%u]%s",
465 length
, array
->name
+ idx
);
467 snprintf(n
, name_length
, "%s[%u]", array
->name
, length
);
476 glsl_type::vec(unsigned components
)
478 if (components
== 0 || components
> 4)
481 static const glsl_type
*const ts
[] = {
482 float_type
, vec2_type
, vec3_type
, vec4_type
484 return ts
[components
- 1];
488 glsl_type::dvec(unsigned components
)
490 if (components
== 0 || components
> 4)
493 static const glsl_type
*const ts
[] = {
494 double_type
, dvec2_type
, dvec3_type
, dvec4_type
496 return ts
[components
- 1];
500 glsl_type::ivec(unsigned components
)
502 if (components
== 0 || components
> 4)
505 static const glsl_type
*const ts
[] = {
506 int_type
, ivec2_type
, ivec3_type
, ivec4_type
508 return ts
[components
- 1];
513 glsl_type::uvec(unsigned components
)
515 if (components
== 0 || components
> 4)
518 static const glsl_type
*const ts
[] = {
519 uint_type
, uvec2_type
, uvec3_type
, uvec4_type
521 return ts
[components
- 1];
526 glsl_type::bvec(unsigned components
)
528 if (components
== 0 || components
> 4)
531 static const glsl_type
*const ts
[] = {
532 bool_type
, bvec2_type
, bvec3_type
, bvec4_type
534 return ts
[components
- 1];
539 glsl_type::i64vec(unsigned components
)
541 if (components
== 0 || components
> 4)
544 static const glsl_type
*const ts
[] = {
545 int64_t_type
, i64vec2_type
, i64vec3_type
, i64vec4_type
547 return ts
[components
- 1];
552 glsl_type::u64vec(unsigned components
)
554 if (components
== 0 || components
> 4)
557 static const glsl_type
*const ts
[] = {
558 uint64_t_type
, u64vec2_type
, u64vec3_type
, u64vec4_type
560 return ts
[components
- 1];
564 glsl_type::get_instance(unsigned base_type
, unsigned rows
, unsigned columns
)
566 if (base_type
== GLSL_TYPE_VOID
)
569 if ((rows
< 1) || (rows
> 4) || (columns
< 1) || (columns
> 4))
572 /* Treat GLSL vectors as Nx1 matrices.
580 case GLSL_TYPE_FLOAT
:
582 case GLSL_TYPE_DOUBLE
:
586 case GLSL_TYPE_UINT64
:
588 case GLSL_TYPE_INT64
:
594 if ((base_type
!= GLSL_TYPE_FLOAT
&& base_type
!= GLSL_TYPE_DOUBLE
) || (rows
== 1))
597 /* GLSL matrix types are named mat{COLUMNS}x{ROWS}. Only the following
598 * combinations are valid:
606 #define IDX(c,r) (((c-1)*3) + (r-1))
608 if (base_type
== GLSL_TYPE_DOUBLE
) {
609 switch (IDX(columns
, rows
)) {
610 case IDX(2,2): return dmat2_type
;
611 case IDX(2,3): return dmat2x3_type
;
612 case IDX(2,4): return dmat2x4_type
;
613 case IDX(3,2): return dmat3x2_type
;
614 case IDX(3,3): return dmat3_type
;
615 case IDX(3,4): return dmat3x4_type
;
616 case IDX(4,2): return dmat4x2_type
;
617 case IDX(4,3): return dmat4x3_type
;
618 case IDX(4,4): return dmat4_type
;
619 default: return error_type
;
622 switch (IDX(columns
, rows
)) {
623 case IDX(2,2): return mat2_type
;
624 case IDX(2,3): return mat2x3_type
;
625 case IDX(2,4): return mat2x4_type
;
626 case IDX(3,2): return mat3x2_type
;
627 case IDX(3,3): return mat3_type
;
628 case IDX(3,4): return mat3x4_type
;
629 case IDX(4,2): return mat4x2_type
;
630 case IDX(4,3): return mat4x3_type
;
631 case IDX(4,4): return mat4_type
;
632 default: return error_type
;
637 assert(!"Should not get here.");
642 glsl_type::get_sampler_instance(enum glsl_sampler_dim dim
,
648 case GLSL_TYPE_FLOAT
:
650 case GLSL_SAMPLER_DIM_1D
:
652 return (array
? sampler1DArrayShadow_type
: sampler1DShadow_type
);
654 return (array
? sampler1DArray_type
: sampler1D_type
);
655 case GLSL_SAMPLER_DIM_2D
:
657 return (array
? sampler2DArrayShadow_type
: sampler2DShadow_type
);
659 return (array
? sampler2DArray_type
: sampler2D_type
);
660 case GLSL_SAMPLER_DIM_3D
:
664 return sampler3D_type
;
665 case GLSL_SAMPLER_DIM_CUBE
:
667 return (array
? samplerCubeArrayShadow_type
: samplerCubeShadow_type
);
669 return (array
? samplerCubeArray_type
: samplerCube_type
);
670 case GLSL_SAMPLER_DIM_RECT
:
674 return sampler2DRectShadow_type
;
676 return sampler2DRect_type
;
677 case GLSL_SAMPLER_DIM_BUF
:
681 return samplerBuffer_type
;
682 case GLSL_SAMPLER_DIM_MS
:
685 return (array
? sampler2DMSArray_type
: sampler2DMS_type
);
686 case GLSL_SAMPLER_DIM_EXTERNAL
:
690 return samplerExternalOES_type
;
691 case GLSL_SAMPLER_DIM_SUBPASS
:
692 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
699 case GLSL_SAMPLER_DIM_1D
:
700 return (array
? isampler1DArray_type
: isampler1D_type
);
701 case GLSL_SAMPLER_DIM_2D
:
702 return (array
? isampler2DArray_type
: isampler2D_type
);
703 case GLSL_SAMPLER_DIM_3D
:
706 return isampler3D_type
;
707 case GLSL_SAMPLER_DIM_CUBE
:
708 return (array
? isamplerCubeArray_type
: isamplerCube_type
);
709 case GLSL_SAMPLER_DIM_RECT
:
712 return isampler2DRect_type
;
713 case GLSL_SAMPLER_DIM_BUF
:
716 return isamplerBuffer_type
;
717 case GLSL_SAMPLER_DIM_MS
:
718 return (array
? isampler2DMSArray_type
: isampler2DMS_type
);
719 case GLSL_SAMPLER_DIM_EXTERNAL
:
721 case GLSL_SAMPLER_DIM_SUBPASS
:
722 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
729 case GLSL_SAMPLER_DIM_1D
:
730 return (array
? usampler1DArray_type
: usampler1D_type
);
731 case GLSL_SAMPLER_DIM_2D
:
732 return (array
? usampler2DArray_type
: usampler2D_type
);
733 case GLSL_SAMPLER_DIM_3D
:
736 return usampler3D_type
;
737 case GLSL_SAMPLER_DIM_CUBE
:
738 return (array
? usamplerCubeArray_type
: usamplerCube_type
);
739 case GLSL_SAMPLER_DIM_RECT
:
742 return usampler2DRect_type
;
743 case GLSL_SAMPLER_DIM_BUF
:
746 return usamplerBuffer_type
;
747 case GLSL_SAMPLER_DIM_MS
:
748 return (array
? usampler2DMSArray_type
: usampler2DMS_type
);
749 case GLSL_SAMPLER_DIM_EXTERNAL
:
751 case GLSL_SAMPLER_DIM_SUBPASS
:
752 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
759 unreachable("switch statement above should be complete");
763 glsl_type::get_image_instance(enum glsl_sampler_dim dim
,
764 bool array
, glsl_base_type type
)
767 case GLSL_TYPE_FLOAT
:
769 case GLSL_SAMPLER_DIM_1D
:
770 return (array
? image1DArray_type
: image1D_type
);
771 case GLSL_SAMPLER_DIM_2D
:
772 return (array
? image2DArray_type
: image2D_type
);
773 case GLSL_SAMPLER_DIM_3D
:
775 case GLSL_SAMPLER_DIM_CUBE
:
776 return (array
? imageCubeArray_type
: imageCube_type
);
777 case GLSL_SAMPLER_DIM_RECT
:
781 return image2DRect_type
;
782 case GLSL_SAMPLER_DIM_BUF
:
786 return imageBuffer_type
;
787 case GLSL_SAMPLER_DIM_MS
:
788 return (array
? image2DMSArray_type
: image2DMS_type
);
789 case GLSL_SAMPLER_DIM_SUBPASS
:
790 return subpassInput_type
;
791 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
792 return subpassInputMS_type
;
793 case GLSL_SAMPLER_DIM_EXTERNAL
:
798 case GLSL_SAMPLER_DIM_1D
:
799 return (array
? iimage1DArray_type
: iimage1D_type
);
800 case GLSL_SAMPLER_DIM_2D
:
801 return (array
? iimage2DArray_type
: iimage2D_type
);
802 case GLSL_SAMPLER_DIM_3D
:
805 return iimage3D_type
;
806 case GLSL_SAMPLER_DIM_CUBE
:
807 return (array
? iimageCubeArray_type
: iimageCube_type
);
808 case GLSL_SAMPLER_DIM_RECT
:
811 return iimage2DRect_type
;
812 case GLSL_SAMPLER_DIM_BUF
:
815 return iimageBuffer_type
;
816 case GLSL_SAMPLER_DIM_MS
:
817 return (array
? iimage2DMSArray_type
: iimage2DMS_type
);
818 case GLSL_SAMPLER_DIM_SUBPASS
:
819 return isubpassInput_type
;
820 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
821 return isubpassInputMS_type
;
822 case GLSL_SAMPLER_DIM_EXTERNAL
:
827 case GLSL_SAMPLER_DIM_1D
:
828 return (array
? uimage1DArray_type
: uimage1D_type
);
829 case GLSL_SAMPLER_DIM_2D
:
830 return (array
? uimage2DArray_type
: uimage2D_type
);
831 case GLSL_SAMPLER_DIM_3D
:
834 return uimage3D_type
;
835 case GLSL_SAMPLER_DIM_CUBE
:
836 return (array
? uimageCubeArray_type
: uimageCube_type
);
837 case GLSL_SAMPLER_DIM_RECT
:
840 return uimage2DRect_type
;
841 case GLSL_SAMPLER_DIM_BUF
:
844 return uimageBuffer_type
;
845 case GLSL_SAMPLER_DIM_MS
:
846 return (array
? uimage2DMSArray_type
: uimage2DMS_type
);
847 case GLSL_SAMPLER_DIM_SUBPASS
:
848 return usubpassInput_type
;
849 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
850 return usubpassInputMS_type
;
851 case GLSL_SAMPLER_DIM_EXTERNAL
:
858 unreachable("switch statement above should be complete");
862 glsl_type::get_array_instance(const glsl_type
*base
, unsigned array_size
)
864 /* Generate a name using the base type pointer in the key. This is
865 * done because the name of the base type may not be unique across
866 * shaders. For example, two shaders may have different record types
870 snprintf(key
, sizeof(key
), "%p[%u]", (void *) base
, array_size
);
872 mtx_lock(&glsl_type::mutex
);
874 if (array_types
== NULL
) {
875 array_types
= _mesa_hash_table_create(NULL
, _mesa_key_hash_string
,
876 _mesa_key_string_equal
);
879 const struct hash_entry
*entry
= _mesa_hash_table_search(array_types
, key
);
881 mtx_unlock(&glsl_type::mutex
);
882 const glsl_type
*t
= new glsl_type(base
, array_size
);
883 mtx_lock(&glsl_type::mutex
);
885 entry
= _mesa_hash_table_insert(array_types
,
886 ralloc_strdup(mem_ctx
, key
),
890 assert(((glsl_type
*) entry
->data
)->base_type
== GLSL_TYPE_ARRAY
);
891 assert(((glsl_type
*) entry
->data
)->length
== array_size
);
892 assert(((glsl_type
*) entry
->data
)->fields
.array
== base
);
894 mtx_unlock(&glsl_type::mutex
);
896 return (glsl_type
*) entry
->data
;
901 glsl_type::record_compare(const glsl_type
*b
, bool match_locations
) const
903 if (this->length
!= b
->length
)
906 if (this->interface_packing
!= b
->interface_packing
)
909 if (this->interface_row_major
!= b
->interface_row_major
)
912 /* From the GLSL 4.20 specification (Sec 4.2):
914 * "Structures must have the same name, sequence of type names, and
915 * type definitions, and field names to be considered the same type."
917 * GLSL ES behaves the same (Ver 1.00 Sec 4.2.4, Ver 3.00 Sec 4.2.5).
919 * Note that we cannot force type name check when comparing unnamed
920 * structure types, these have a unique name assigned during parsing.
922 if (!this->is_anonymous() && !b
->is_anonymous())
923 if (strcmp(this->name
, b
->name
) != 0)
926 for (unsigned i
= 0; i
< this->length
; i
++) {
927 if (this->fields
.structure
[i
].type
!= b
->fields
.structure
[i
].type
)
929 if (strcmp(this->fields
.structure
[i
].name
,
930 b
->fields
.structure
[i
].name
) != 0)
932 if (this->fields
.structure
[i
].matrix_layout
933 != b
->fields
.structure
[i
].matrix_layout
)
935 if (match_locations
&& this->fields
.structure
[i
].location
936 != b
->fields
.structure
[i
].location
)
938 if (this->fields
.structure
[i
].offset
939 != b
->fields
.structure
[i
].offset
)
941 if (this->fields
.structure
[i
].interpolation
942 != b
->fields
.structure
[i
].interpolation
)
944 if (this->fields
.structure
[i
].centroid
945 != b
->fields
.structure
[i
].centroid
)
947 if (this->fields
.structure
[i
].sample
948 != b
->fields
.structure
[i
].sample
)
950 if (this->fields
.structure
[i
].patch
951 != b
->fields
.structure
[i
].patch
)
953 if (this->fields
.structure
[i
].memory_read_only
954 != b
->fields
.structure
[i
].memory_read_only
)
956 if (this->fields
.structure
[i
].memory_write_only
957 != b
->fields
.structure
[i
].memory_write_only
)
959 if (this->fields
.structure
[i
].memory_coherent
960 != b
->fields
.structure
[i
].memory_coherent
)
962 if (this->fields
.structure
[i
].memory_volatile
963 != b
->fields
.structure
[i
].memory_volatile
)
965 if (this->fields
.structure
[i
].memory_restrict
966 != b
->fields
.structure
[i
].memory_restrict
)
968 if (this->fields
.structure
[i
].image_format
969 != b
->fields
.structure
[i
].image_format
)
971 if (this->fields
.structure
[i
].precision
972 != b
->fields
.structure
[i
].precision
)
974 if (this->fields
.structure
[i
].explicit_xfb_buffer
975 != b
->fields
.structure
[i
].explicit_xfb_buffer
)
977 if (this->fields
.structure
[i
].xfb_buffer
978 != b
->fields
.structure
[i
].xfb_buffer
)
980 if (this->fields
.structure
[i
].xfb_stride
981 != b
->fields
.structure
[i
].xfb_stride
)
990 glsl_type::record_key_compare(const void *a
, const void *b
)
992 const glsl_type
*const key1
= (glsl_type
*) a
;
993 const glsl_type
*const key2
= (glsl_type
*) b
;
995 return strcmp(key1
->name
, key2
->name
) == 0 && key1
->record_compare(key2
);
1000 * Generate an integer hash value for a glsl_type structure type.
1003 glsl_type::record_key_hash(const void *a
)
1005 const glsl_type
*const key
= (glsl_type
*) a
;
1006 uintptr_t hash
= key
->length
;
1009 for (unsigned i
= 0; i
< key
->length
; i
++) {
1010 /* casting pointer to uintptr_t */
1011 hash
= (hash
* 13 ) + (uintptr_t) key
->fields
.structure
[i
].type
;
1014 if (sizeof(hash
) == 8)
1015 retval
= (hash
& 0xffffffff) ^ ((uint64_t) hash
>> 32);
1024 glsl_type::get_record_instance(const glsl_struct_field
*fields
,
1025 unsigned num_fields
,
1028 const glsl_type
key(fields
, num_fields
, name
);
1030 mtx_lock(&glsl_type::mutex
);
1032 if (record_types
== NULL
) {
1033 record_types
= _mesa_hash_table_create(NULL
, record_key_hash
,
1034 record_key_compare
);
1037 const struct hash_entry
*entry
= _mesa_hash_table_search(record_types
,
1039 if (entry
== NULL
) {
1040 mtx_unlock(&glsl_type::mutex
);
1041 const glsl_type
*t
= new glsl_type(fields
, num_fields
, name
);
1042 mtx_lock(&glsl_type::mutex
);
1044 entry
= _mesa_hash_table_insert(record_types
, t
, (void *) t
);
1047 assert(((glsl_type
*) entry
->data
)->base_type
== GLSL_TYPE_STRUCT
);
1048 assert(((glsl_type
*) entry
->data
)->length
== num_fields
);
1049 assert(strcmp(((glsl_type
*) entry
->data
)->name
, name
) == 0);
1051 mtx_unlock(&glsl_type::mutex
);
1053 return (glsl_type
*) entry
->data
;
1058 glsl_type::get_interface_instance(const glsl_struct_field
*fields
,
1059 unsigned num_fields
,
1060 enum glsl_interface_packing packing
,
1062 const char *block_name
)
1064 const glsl_type
key(fields
, num_fields
, packing
, row_major
, block_name
);
1066 mtx_lock(&glsl_type::mutex
);
1068 if (interface_types
== NULL
) {
1069 interface_types
= _mesa_hash_table_create(NULL
, record_key_hash
,
1070 record_key_compare
);
1073 const struct hash_entry
*entry
= _mesa_hash_table_search(interface_types
,
1075 if (entry
== NULL
) {
1076 mtx_unlock(&glsl_type::mutex
);
1077 const glsl_type
*t
= new glsl_type(fields
, num_fields
,
1078 packing
, row_major
, block_name
);
1079 mtx_lock(&glsl_type::mutex
);
1081 entry
= _mesa_hash_table_insert(interface_types
, t
, (void *) t
);
1084 assert(((glsl_type
*) entry
->data
)->base_type
== GLSL_TYPE_INTERFACE
);
1085 assert(((glsl_type
*) entry
->data
)->length
== num_fields
);
1086 assert(strcmp(((glsl_type
*) entry
->data
)->name
, block_name
) == 0);
1088 mtx_unlock(&glsl_type::mutex
);
1090 return (glsl_type
*) entry
->data
;
1094 glsl_type::get_subroutine_instance(const char *subroutine_name
)
1096 const glsl_type
key(subroutine_name
);
1098 mtx_lock(&glsl_type::mutex
);
1100 if (subroutine_types
== NULL
) {
1101 subroutine_types
= _mesa_hash_table_create(NULL
, record_key_hash
,
1102 record_key_compare
);
1105 const struct hash_entry
*entry
= _mesa_hash_table_search(subroutine_types
,
1107 if (entry
== NULL
) {
1108 mtx_unlock(&glsl_type::mutex
);
1109 const glsl_type
*t
= new glsl_type(subroutine_name
);
1110 mtx_lock(&glsl_type::mutex
);
1112 entry
= _mesa_hash_table_insert(subroutine_types
, t
, (void *) t
);
1115 assert(((glsl_type
*) entry
->data
)->base_type
== GLSL_TYPE_SUBROUTINE
);
1116 assert(strcmp(((glsl_type
*) entry
->data
)->name
, subroutine_name
) == 0);
1118 mtx_unlock(&glsl_type::mutex
);
1120 return (glsl_type
*) entry
->data
;
1125 function_key_compare(const void *a
, const void *b
)
1127 const glsl_type
*const key1
= (glsl_type
*) a
;
1128 const glsl_type
*const key2
= (glsl_type
*) b
;
1130 if (key1
->length
!= key2
->length
)
1133 return memcmp(key1
->fields
.parameters
, key2
->fields
.parameters
,
1134 (key1
->length
+ 1) * sizeof(*key1
->fields
.parameters
)) == 0;
1139 function_key_hash(const void *a
)
1141 const glsl_type
*const key
= (glsl_type
*) a
;
1142 return _mesa_hash_data(key
->fields
.parameters
,
1143 (key
->length
+ 1) * sizeof(*key
->fields
.parameters
));
1147 glsl_type::get_function_instance(const glsl_type
*return_type
,
1148 const glsl_function_param
*params
,
1149 unsigned num_params
)
1151 const glsl_type
key(return_type
, params
, num_params
);
1153 mtx_lock(&glsl_type::mutex
);
1155 if (function_types
== NULL
) {
1156 function_types
= _mesa_hash_table_create(NULL
, function_key_hash
,
1157 function_key_compare
);
1160 struct hash_entry
*entry
= _mesa_hash_table_search(function_types
, &key
);
1161 if (entry
== NULL
) {
1162 mtx_unlock(&glsl_type::mutex
);
1163 const glsl_type
*t
= new glsl_type(return_type
, params
, num_params
);
1164 mtx_lock(&glsl_type::mutex
);
1166 entry
= _mesa_hash_table_insert(function_types
, t
, (void *) t
);
1169 const glsl_type
*t
= (const glsl_type
*)entry
->data
;
1171 assert(t
->base_type
== GLSL_TYPE_FUNCTION
);
1172 assert(t
->length
== num_params
);
1174 mtx_unlock(&glsl_type::mutex
);
1181 glsl_type::get_mul_type(const glsl_type
*type_a
, const glsl_type
*type_b
)
1183 if (type_a
== type_b
) {
1185 } else if (type_a
->is_matrix() && type_b
->is_matrix()) {
1186 /* Matrix multiply. The columns of A must match the rows of B. Given
1187 * the other previously tested constraints, this means the vector type
1188 * of a row from A must be the same as the vector type of a column from
1191 if (type_a
->row_type() == type_b
->column_type()) {
1192 /* The resulting matrix has the number of columns of matrix B and
1193 * the number of rows of matrix A. We get the row count of A by
1194 * looking at the size of a vector that makes up a column. The
1195 * transpose (size of a row) is done for B.
1197 const glsl_type
*const type
=
1198 get_instance(type_a
->base_type
,
1199 type_a
->column_type()->vector_elements
,
1200 type_b
->row_type()->vector_elements
);
1201 assert(type
!= error_type
);
1205 } else if (type_a
->is_matrix()) {
1206 /* A is a matrix and B is a column vector. Columns of A must match
1207 * rows of B. Given the other previously tested constraints, this
1208 * means the vector type of a row from A must be the same as the
1209 * vector the type of B.
1211 if (type_a
->row_type() == type_b
) {
1212 /* The resulting vector has a number of elements equal to
1213 * the number of rows of matrix A. */
1214 const glsl_type
*const type
=
1215 get_instance(type_a
->base_type
,
1216 type_a
->column_type()->vector_elements
,
1218 assert(type
!= error_type
);
1223 assert(type_b
->is_matrix());
1225 /* A is a row vector and B is a matrix. Columns of A must match rows
1226 * of B. Given the other previously tested constraints, this means
1227 * the type of A must be the same as the vector type of a column from
1230 if (type_a
== type_b
->column_type()) {
1231 /* The resulting vector has a number of elements equal to
1232 * the number of columns of matrix B. */
1233 const glsl_type
*const type
=
1234 get_instance(type_a
->base_type
,
1235 type_b
->row_type()->vector_elements
,
1237 assert(type
!= error_type
);
1248 glsl_type::field_type(const char *name
) const
1250 if (this->base_type
!= GLSL_TYPE_STRUCT
1251 && this->base_type
!= GLSL_TYPE_INTERFACE
)
1254 for (unsigned i
= 0; i
< this->length
; i
++) {
1255 if (strcmp(name
, this->fields
.structure
[i
].name
) == 0)
1256 return this->fields
.structure
[i
].type
;
1264 glsl_type::field_index(const char *name
) const
1266 if (this->base_type
!= GLSL_TYPE_STRUCT
1267 && this->base_type
!= GLSL_TYPE_INTERFACE
)
1270 for (unsigned i
= 0; i
< this->length
; i
++) {
1271 if (strcmp(name
, this->fields
.structure
[i
].name
) == 0)
1280 glsl_type::component_slots() const
1282 switch (this->base_type
) {
1283 case GLSL_TYPE_UINT
:
1285 case GLSL_TYPE_FLOAT
:
1286 case GLSL_TYPE_BOOL
:
1287 return this->components();
1289 case GLSL_TYPE_DOUBLE
:
1290 case GLSL_TYPE_UINT64
:
1291 case GLSL_TYPE_INT64
:
1292 return 2 * this->components();
1294 case GLSL_TYPE_STRUCT
:
1295 case GLSL_TYPE_INTERFACE
: {
1298 for (unsigned i
= 0; i
< this->length
; i
++)
1299 size
+= this->fields
.structure
[i
].type
->component_slots();
1304 case GLSL_TYPE_ARRAY
:
1305 return this->length
* this->fields
.array
->component_slots();
1307 case GLSL_TYPE_SAMPLER
:
1308 case GLSL_TYPE_IMAGE
:
1311 case GLSL_TYPE_SUBROUTINE
:
1314 case GLSL_TYPE_FUNCTION
:
1315 case GLSL_TYPE_ATOMIC_UINT
:
1316 case GLSL_TYPE_VOID
:
1317 case GLSL_TYPE_ERROR
:
1325 glsl_type::record_location_offset(unsigned length
) const
1327 unsigned offset
= 0;
1328 const glsl_type
*t
= this->without_array();
1329 if (t
->is_record()) {
1330 assert(length
<= t
->length
);
1332 for (unsigned i
= 0; i
< length
; i
++) {
1333 const glsl_type
*st
= t
->fields
.structure
[i
].type
;
1334 const glsl_type
*wa
= st
->without_array();
1335 if (wa
->is_record()) {
1336 unsigned r_offset
= wa
->record_location_offset(wa
->length
);
1337 offset
+= st
->is_array() ?
1338 st
->arrays_of_arrays_size() * r_offset
: r_offset
;
1339 } else if (st
->is_array() && st
->fields
.array
->is_array()) {
1340 unsigned outer_array_size
= st
->length
;
1341 const glsl_type
*base_type
= st
->fields
.array
;
1343 /* For arrays of arrays the outer arrays take up a uniform
1344 * slot for each element. The innermost array elements share a
1345 * single slot so we ignore the innermost array when calculating
1348 while (base_type
->fields
.array
->is_array()) {
1349 outer_array_size
= outer_array_size
* base_type
->length
;
1350 base_type
= base_type
->fields
.array
;
1352 offset
+= outer_array_size
;
1354 /* We dont worry about arrays here because unless the array
1355 * contains a structure or another array it only takes up a single
1366 glsl_type::uniform_locations() const
1370 switch (this->base_type
) {
1371 case GLSL_TYPE_UINT
:
1373 case GLSL_TYPE_FLOAT
:
1374 case GLSL_TYPE_DOUBLE
:
1375 case GLSL_TYPE_UINT64
:
1376 case GLSL_TYPE_INT64
:
1377 case GLSL_TYPE_BOOL
:
1378 case GLSL_TYPE_SAMPLER
:
1379 case GLSL_TYPE_IMAGE
:
1380 case GLSL_TYPE_SUBROUTINE
:
1383 case GLSL_TYPE_STRUCT
:
1384 case GLSL_TYPE_INTERFACE
:
1385 for (unsigned i
= 0; i
< this->length
; i
++)
1386 size
+= this->fields
.structure
[i
].type
->uniform_locations();
1388 case GLSL_TYPE_ARRAY
:
1389 return this->length
* this->fields
.array
->uniform_locations();
1396 glsl_type::varying_count() const
1400 switch (this->base_type
) {
1401 case GLSL_TYPE_UINT
:
1403 case GLSL_TYPE_FLOAT
:
1404 case GLSL_TYPE_DOUBLE
:
1405 case GLSL_TYPE_BOOL
:
1406 case GLSL_TYPE_UINT64
:
1407 case GLSL_TYPE_INT64
:
1410 case GLSL_TYPE_STRUCT
:
1411 case GLSL_TYPE_INTERFACE
:
1412 for (unsigned i
= 0; i
< this->length
; i
++)
1413 size
+= this->fields
.structure
[i
].type
->varying_count();
1415 case GLSL_TYPE_ARRAY
:
1416 /* Don't count innermost array elements */
1417 if (this->without_array()->is_record() ||
1418 this->without_array()->is_interface() ||
1419 this->fields
.array
->is_array())
1420 return this->length
* this->fields
.array
->varying_count();
1422 return this->fields
.array
->varying_count();
1424 assert(!"unsupported varying type");
1430 glsl_type::can_implicitly_convert_to(const glsl_type
*desired
,
1431 _mesa_glsl_parse_state
*state
) const
1433 if (this == desired
)
1436 /* GLSL 1.10 and ESSL do not allow implicit conversions. If there is no
1437 * state, we're doing intra-stage function linking where these checks have
1438 * already been done.
1440 if (state
&& (state
->es_shader
|| !state
->is_version(120, 0)))
1443 /* There is no conversion among matrix types. */
1444 if (this->matrix_columns
> 1 || desired
->matrix_columns
> 1)
1447 /* Vector size must match. */
1448 if (this->vector_elements
!= desired
->vector_elements
)
1451 /* int and uint can be converted to float. */
1452 if (desired
->is_float() && this->is_integer())
1455 /* With GLSL 4.0, ARB_gpu_shader5, or MESA_shader_integer_functions, int
1456 * can be converted to uint. Note that state may be NULL here, when
1457 * resolving function calls in the linker. By this time, all the
1458 * state-dependent checks have already happened though, so allow anything
1459 * that's allowed in any shader version.
1461 if ((!state
|| state
->is_version(400, 0) || state
->ARB_gpu_shader5_enable
||
1462 state
->MESA_shader_integer_functions_enable
) &&
1463 desired
->base_type
== GLSL_TYPE_UINT
&& this->base_type
== GLSL_TYPE_INT
)
1466 /* No implicit conversions from double. */
1467 if ((!state
|| state
->has_double()) && this->is_double())
1470 /* Conversions from different types to double. */
1471 if ((!state
|| state
->has_double()) && desired
->is_double()) {
1472 if (this->is_float())
1474 if (this->is_integer())
1482 glsl_type::std140_base_alignment(bool row_major
) const
1484 unsigned N
= is_64bit() ? 8 : 4;
1486 /* (1) If the member is a scalar consuming <N> basic machine units, the
1487 * base alignment is <N>.
1489 * (2) If the member is a two- or four-component vector with components
1490 * consuming <N> basic machine units, the base alignment is 2<N> or
1491 * 4<N>, respectively.
1493 * (3) If the member is a three-component vector with components consuming
1494 * <N> basic machine units, the base alignment is 4<N>.
1496 if (this->is_scalar() || this->is_vector()) {
1497 switch (this->vector_elements
) {
1508 /* (4) If the member is an array of scalars or vectors, the base alignment
1509 * and array stride are set to match the base alignment of a single
1510 * array element, according to rules (1), (2), and (3), and rounded up
1511 * to the base alignment of a vec4. The array may have padding at the
1512 * end; the base offset of the member following the array is rounded up
1513 * to the next multiple of the base alignment.
1515 * (6) If the member is an array of <S> column-major matrices with <C>
1516 * columns and <R> rows, the matrix is stored identically to a row of
1517 * <S>*<C> column vectors with <R> components each, according to rule
1520 * (8) If the member is an array of <S> row-major matrices with <C> columns
1521 * and <R> rows, the matrix is stored identically to a row of <S>*<R>
1522 * row vectors with <C> components each, according to rule (4).
1524 * (10) If the member is an array of <S> structures, the <S> elements of
1525 * the array are laid out in order, according to rule (9).
1527 if (this->is_array()) {
1528 if (this->fields
.array
->is_scalar() ||
1529 this->fields
.array
->is_vector() ||
1530 this->fields
.array
->is_matrix()) {
1531 return MAX2(this->fields
.array
->std140_base_alignment(row_major
), 16);
1533 assert(this->fields
.array
->is_record() ||
1534 this->fields
.array
->is_array());
1535 return this->fields
.array
->std140_base_alignment(row_major
);
1539 /* (5) If the member is a column-major matrix with <C> columns and
1540 * <R> rows, the matrix is stored identically to an array of
1541 * <C> column vectors with <R> components each, according to
1544 * (7) If the member is a row-major matrix with <C> columns and <R>
1545 * rows, the matrix is stored identically to an array of <R>
1546 * row vectors with <C> components each, according to rule (4).
1548 if (this->is_matrix()) {
1549 const struct glsl_type
*vec_type
, *array_type
;
1550 int c
= this->matrix_columns
;
1551 int r
= this->vector_elements
;
1554 vec_type
= get_instance(base_type
, c
, 1);
1555 array_type
= glsl_type::get_array_instance(vec_type
, r
);
1557 vec_type
= get_instance(base_type
, r
, 1);
1558 array_type
= glsl_type::get_array_instance(vec_type
, c
);
1561 return array_type
->std140_base_alignment(false);
1564 /* (9) If the member is a structure, the base alignment of the
1565 * structure is <N>, where <N> is the largest base alignment
1566 * value of any of its members, and rounded up to the base
1567 * alignment of a vec4. The individual members of this
1568 * sub-structure are then assigned offsets by applying this set
1569 * of rules recursively, where the base offset of the first
1570 * member of the sub-structure is equal to the aligned offset
1571 * of the structure. The structure may have padding at the end;
1572 * the base offset of the member following the sub-structure is
1573 * rounded up to the next multiple of the base alignment of the
1576 if (this->is_record()) {
1577 unsigned base_alignment
= 16;
1578 for (unsigned i
= 0; i
< this->length
; i
++) {
1579 bool field_row_major
= row_major
;
1580 const enum glsl_matrix_layout matrix_layout
=
1581 glsl_matrix_layout(this->fields
.structure
[i
].matrix_layout
);
1582 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
1583 field_row_major
= true;
1584 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
1585 field_row_major
= false;
1588 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
1589 base_alignment
= MAX2(base_alignment
,
1590 field_type
->std140_base_alignment(field_row_major
));
1592 return base_alignment
;
1595 assert(!"not reached");
1600 glsl_type::std140_size(bool row_major
) const
1602 unsigned N
= is_64bit() ? 8 : 4;
1604 /* (1) If the member is a scalar consuming <N> basic machine units, the
1605 * base alignment is <N>.
1607 * (2) If the member is a two- or four-component vector with components
1608 * consuming <N> basic machine units, the base alignment is 2<N> or
1609 * 4<N>, respectively.
1611 * (3) If the member is a three-component vector with components consuming
1612 * <N> basic machine units, the base alignment is 4<N>.
1614 if (this->is_scalar() || this->is_vector()) {
1615 return this->vector_elements
* N
;
1618 /* (5) If the member is a column-major matrix with <C> columns and
1619 * <R> rows, the matrix is stored identically to an array of
1620 * <C> column vectors with <R> components each, according to
1623 * (6) If the member is an array of <S> column-major matrices with <C>
1624 * columns and <R> rows, the matrix is stored identically to a row of
1625 * <S>*<C> column vectors with <R> components each, according to rule
1628 * (7) If the member is a row-major matrix with <C> columns and <R>
1629 * rows, the matrix is stored identically to an array of <R>
1630 * row vectors with <C> components each, according to rule (4).
1632 * (8) If the member is an array of <S> row-major matrices with <C> columns
1633 * and <R> rows, the matrix is stored identically to a row of <S>*<R>
1634 * row vectors with <C> components each, according to rule (4).
1636 if (this->without_array()->is_matrix()) {
1637 const struct glsl_type
*element_type
;
1638 const struct glsl_type
*vec_type
;
1639 unsigned int array_len
;
1641 if (this->is_array()) {
1642 element_type
= this->without_array();
1643 array_len
= this->arrays_of_arrays_size();
1645 element_type
= this;
1650 vec_type
= get_instance(element_type
->base_type
,
1651 element_type
->matrix_columns
, 1);
1653 array_len
*= element_type
->vector_elements
;
1655 vec_type
= get_instance(element_type
->base_type
,
1656 element_type
->vector_elements
, 1);
1657 array_len
*= element_type
->matrix_columns
;
1659 const glsl_type
*array_type
= glsl_type::get_array_instance(vec_type
,
1662 return array_type
->std140_size(false);
1665 /* (4) If the member is an array of scalars or vectors, the base alignment
1666 * and array stride are set to match the base alignment of a single
1667 * array element, according to rules (1), (2), and (3), and rounded up
1668 * to the base alignment of a vec4. The array may have padding at the
1669 * end; the base offset of the member following the array is rounded up
1670 * to the next multiple of the base alignment.
1672 * (10) If the member is an array of <S> structures, the <S> elements of
1673 * the array are laid out in order, according to rule (9).
1675 if (this->is_array()) {
1676 if (this->without_array()->is_record()) {
1677 return this->arrays_of_arrays_size() *
1678 this->without_array()->std140_size(row_major
);
1680 unsigned element_base_align
=
1681 this->without_array()->std140_base_alignment(row_major
);
1682 return this->arrays_of_arrays_size() * MAX2(element_base_align
, 16);
1686 /* (9) If the member is a structure, the base alignment of the
1687 * structure is <N>, where <N> is the largest base alignment
1688 * value of any of its members, and rounded up to the base
1689 * alignment of a vec4. The individual members of this
1690 * sub-structure are then assigned offsets by applying this set
1691 * of rules recursively, where the base offset of the first
1692 * member of the sub-structure is equal to the aligned offset
1693 * of the structure. The structure may have padding at the end;
1694 * the base offset of the member following the sub-structure is
1695 * rounded up to the next multiple of the base alignment of the
1698 if (this->is_record() || this->is_interface()) {
1700 unsigned max_align
= 0;
1702 for (unsigned i
= 0; i
< this->length
; i
++) {
1703 bool field_row_major
= row_major
;
1704 const enum glsl_matrix_layout matrix_layout
=
1705 glsl_matrix_layout(this->fields
.structure
[i
].matrix_layout
);
1706 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
1707 field_row_major
= true;
1708 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
1709 field_row_major
= false;
1712 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
1713 unsigned align
= field_type
->std140_base_alignment(field_row_major
);
1715 /* Ignore unsized arrays when calculating size */
1716 if (field_type
->is_unsized_array())
1719 size
= glsl_align(size
, align
);
1720 size
+= field_type
->std140_size(field_row_major
);
1722 max_align
= MAX2(align
, max_align
);
1724 if (field_type
->is_record() && (i
+ 1 < this->length
))
1725 size
= glsl_align(size
, 16);
1727 size
= glsl_align(size
, MAX2(max_align
, 16));
1731 assert(!"not reached");
1736 glsl_type::std430_base_alignment(bool row_major
) const
1739 unsigned N
= is_64bit() ? 8 : 4;
1741 /* (1) If the member is a scalar consuming <N> basic machine units, the
1742 * base alignment is <N>.
1744 * (2) If the member is a two- or four-component vector with components
1745 * consuming <N> basic machine units, the base alignment is 2<N> or
1746 * 4<N>, respectively.
1748 * (3) If the member is a three-component vector with components consuming
1749 * <N> basic machine units, the base alignment is 4<N>.
1751 if (this->is_scalar() || this->is_vector()) {
1752 switch (this->vector_elements
) {
1763 /* OpenGL 4.30 spec, section 7.6.2.2 "Standard Uniform Block Layout":
1765 * "When using the std430 storage layout, shader storage blocks will be
1766 * laid out in buffer storage identically to uniform and shader storage
1767 * blocks using the std140 layout, except that the base alignment and
1768 * stride of arrays of scalars and vectors in rule 4 and of structures
1769 * in rule 9 are not rounded up a multiple of the base alignment of a vec4.
1772 /* (1) If the member is a scalar consuming <N> basic machine units, the
1773 * base alignment is <N>.
1775 * (2) If the member is a two- or four-component vector with components
1776 * consuming <N> basic machine units, the base alignment is 2<N> or
1777 * 4<N>, respectively.
1779 * (3) If the member is a three-component vector with components consuming
1780 * <N> basic machine units, the base alignment is 4<N>.
1782 if (this->is_array())
1783 return this->fields
.array
->std430_base_alignment(row_major
);
1785 /* (5) If the member is a column-major matrix with <C> columns and
1786 * <R> rows, the matrix is stored identically to an array of
1787 * <C> column vectors with <R> components each, according to
1790 * (7) If the member is a row-major matrix with <C> columns and <R>
1791 * rows, the matrix is stored identically to an array of <R>
1792 * row vectors with <C> components each, according to rule (4).
1794 if (this->is_matrix()) {
1795 const struct glsl_type
*vec_type
, *array_type
;
1796 int c
= this->matrix_columns
;
1797 int r
= this->vector_elements
;
1800 vec_type
= get_instance(base_type
, c
, 1);
1801 array_type
= glsl_type::get_array_instance(vec_type
, r
);
1803 vec_type
= get_instance(base_type
, r
, 1);
1804 array_type
= glsl_type::get_array_instance(vec_type
, c
);
1807 return array_type
->std430_base_alignment(false);
1810 /* (9) If the member is a structure, the base alignment of the
1811 * structure is <N>, where <N> is the largest base alignment
1812 * value of any of its members, and rounded up to the base
1813 * alignment of a vec4. The individual members of this
1814 * sub-structure are then assigned offsets by applying this set
1815 * of rules recursively, where the base offset of the first
1816 * member of the sub-structure is equal to the aligned offset
1817 * of the structure. The structure may have padding at the end;
1818 * the base offset of the member following the sub-structure is
1819 * rounded up to the next multiple of the base alignment of the
1822 if (this->is_record()) {
1823 unsigned base_alignment
= 0;
1824 for (unsigned i
= 0; i
< this->length
; i
++) {
1825 bool field_row_major
= row_major
;
1826 const enum glsl_matrix_layout matrix_layout
=
1827 glsl_matrix_layout(this->fields
.structure
[i
].matrix_layout
);
1828 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
1829 field_row_major
= true;
1830 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
1831 field_row_major
= false;
1834 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
1835 base_alignment
= MAX2(base_alignment
,
1836 field_type
->std430_base_alignment(field_row_major
));
1838 assert(base_alignment
> 0);
1839 return base_alignment
;
1841 assert(!"not reached");
1846 glsl_type::std430_array_stride(bool row_major
) const
1848 unsigned N
= is_64bit() ? 8 : 4;
1850 /* Notice that the array stride of a vec3 is not 3 * N but 4 * N.
1851 * See OpenGL 4.30 spec, section 7.6.2.2 "Standard Uniform Block Layout"
1853 * (3) If the member is a three-component vector with components consuming
1854 * <N> basic machine units, the base alignment is 4<N>.
1856 if (this->is_vector() && this->vector_elements
== 3)
1859 /* By default use std430_size(row_major) */
1860 return this->std430_size(row_major
);
1864 glsl_type::std430_size(bool row_major
) const
1866 unsigned N
= is_64bit() ? 8 : 4;
1868 /* OpenGL 4.30 spec, section 7.6.2.2 "Standard Uniform Block Layout":
1870 * "When using the std430 storage layout, shader storage blocks will be
1871 * laid out in buffer storage identically to uniform and shader storage
1872 * blocks using the std140 layout, except that the base alignment and
1873 * stride of arrays of scalars and vectors in rule 4 and of structures
1874 * in rule 9 are not rounded up a multiple of the base alignment of a vec4.
1876 if (this->is_scalar() || this->is_vector())
1877 return this->vector_elements
* N
;
1879 if (this->without_array()->is_matrix()) {
1880 const struct glsl_type
*element_type
;
1881 const struct glsl_type
*vec_type
;
1882 unsigned int array_len
;
1884 if (this->is_array()) {
1885 element_type
= this->without_array();
1886 array_len
= this->arrays_of_arrays_size();
1888 element_type
= this;
1893 vec_type
= get_instance(element_type
->base_type
,
1894 element_type
->matrix_columns
, 1);
1896 array_len
*= element_type
->vector_elements
;
1898 vec_type
= get_instance(element_type
->base_type
,
1899 element_type
->vector_elements
, 1);
1900 array_len
*= element_type
->matrix_columns
;
1902 const glsl_type
*array_type
= glsl_type::get_array_instance(vec_type
,
1905 return array_type
->std430_size(false);
1908 if (this->is_array()) {
1909 if (this->without_array()->is_record())
1910 return this->arrays_of_arrays_size() *
1911 this->without_array()->std430_size(row_major
);
1913 return this->arrays_of_arrays_size() *
1914 this->without_array()->std430_base_alignment(row_major
);
1917 if (this->is_record() || this->is_interface()) {
1919 unsigned max_align
= 0;
1921 for (unsigned i
= 0; i
< this->length
; i
++) {
1922 bool field_row_major
= row_major
;
1923 const enum glsl_matrix_layout matrix_layout
=
1924 glsl_matrix_layout(this->fields
.structure
[i
].matrix_layout
);
1925 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
1926 field_row_major
= true;
1927 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
1928 field_row_major
= false;
1931 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
1932 unsigned align
= field_type
->std430_base_alignment(field_row_major
);
1933 size
= glsl_align(size
, align
);
1934 size
+= field_type
->std430_size(field_row_major
);
1936 max_align
= MAX2(align
, max_align
);
1938 size
= glsl_align(size
, max_align
);
1942 assert(!"not reached");
1947 glsl_type::count_attribute_slots(bool is_vertex_input
) const
1949 /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
1951 * "A scalar input counts the same amount against this limit as a vec4,
1952 * so applications may want to consider packing groups of four
1953 * unrelated float inputs together into a vector to better utilize the
1954 * capabilities of the underlying hardware. A matrix input will use up
1955 * multiple locations. The number of locations used will equal the
1956 * number of columns in the matrix."
1958 * The spec does not explicitly say how arrays are counted. However, it
1959 * should be safe to assume the total number of slots consumed by an array
1960 * is the number of entries in the array multiplied by the number of slots
1961 * consumed by a single element of the array.
1963 * The spec says nothing about how structs are counted, because vertex
1964 * attributes are not allowed to be (or contain) structs. However, Mesa
1965 * allows varying structs, the number of varying slots taken up by a
1966 * varying struct is simply equal to the sum of the number of slots taken
1967 * up by each element.
1969 * Doubles are counted different depending on whether they are vertex
1970 * inputs or everything else. Vertex inputs from ARB_vertex_attrib_64bit
1971 * take one location no matter what size they are, otherwise dvec3/4
1972 * take two locations.
1974 switch (this->base_type
) {
1975 case GLSL_TYPE_UINT
:
1977 case GLSL_TYPE_FLOAT
:
1978 case GLSL_TYPE_BOOL
:
1979 case GLSL_TYPE_SAMPLER
:
1980 case GLSL_TYPE_IMAGE
:
1981 return this->matrix_columns
;
1982 case GLSL_TYPE_DOUBLE
:
1983 case GLSL_TYPE_UINT64
:
1984 case GLSL_TYPE_INT64
:
1985 if (this->vector_elements
> 2 && !is_vertex_input
)
1986 return this->matrix_columns
* 2;
1988 return this->matrix_columns
;
1989 case GLSL_TYPE_STRUCT
:
1990 case GLSL_TYPE_INTERFACE
: {
1993 for (unsigned i
= 0; i
< this->length
; i
++)
1994 size
+= this->fields
.structure
[i
].type
->count_attribute_slots(is_vertex_input
);
1999 case GLSL_TYPE_ARRAY
:
2000 return this->length
* this->fields
.array
->count_attribute_slots(is_vertex_input
);
2002 case GLSL_TYPE_FUNCTION
:
2003 case GLSL_TYPE_ATOMIC_UINT
:
2004 case GLSL_TYPE_VOID
:
2005 case GLSL_TYPE_SUBROUTINE
:
2006 case GLSL_TYPE_ERROR
:
2010 assert(!"Unexpected type in count_attribute_slots()");
2016 glsl_type::coordinate_components() const
2020 switch (sampler_dimensionality
) {
2021 case GLSL_SAMPLER_DIM_1D
:
2022 case GLSL_SAMPLER_DIM_BUF
:
2025 case GLSL_SAMPLER_DIM_2D
:
2026 case GLSL_SAMPLER_DIM_RECT
:
2027 case GLSL_SAMPLER_DIM_MS
:
2028 case GLSL_SAMPLER_DIM_EXTERNAL
:
2029 case GLSL_SAMPLER_DIM_SUBPASS
:
2032 case GLSL_SAMPLER_DIM_3D
:
2033 case GLSL_SAMPLER_DIM_CUBE
:
2037 assert(!"Should not get here.");
2042 /* Array textures need an additional component for the array index, except
2043 * for cubemap array images that behave like a 2D array of interleaved
2046 if (sampler_array
&&
2047 !(is_image() && sampler_dimensionality
== GLSL_SAMPLER_DIM_CUBE
))
2054 * Declarations of type flyweights (glsl_type::_foo_type) and
2055 * convenience pointers (glsl_type::foo_type).
2058 #define DECL_TYPE(NAME, ...) \
2059 const glsl_type glsl_type::_##NAME##_type = glsl_type(__VA_ARGS__, #NAME); \
2060 const glsl_type *const glsl_type::NAME##_type = &glsl_type::_##NAME##_type;
2062 #define STRUCT_TYPE(NAME)
2064 #include "compiler/builtin_type_macros.h"