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_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 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
));
99 /* Samplers take no storage whatsoever. */
100 matrix_columns
= vector_elements
= 0;
102 matrix_columns
= vector_elements
= 1;
106 glsl_type::glsl_type(const glsl_struct_field
*fields
, unsigned num_fields
,
109 base_type(GLSL_TYPE_STRUCT
),
110 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
111 sampled_type(0), interface_packing(0), interface_row_major(0),
112 vector_elements(0), matrix_columns(0),
117 mtx_lock(&glsl_type::mutex
);
119 init_ralloc_type_ctx();
120 assert(name
!= NULL
);
121 this->name
= ralloc_strdup(this->mem_ctx
, name
);
122 this->fields
.structure
= ralloc_array(this->mem_ctx
,
123 glsl_struct_field
, length
);
125 for (i
= 0; i
< length
; i
++) {
126 this->fields
.structure
[i
] = fields
[i
];
127 this->fields
.structure
[i
].name
= ralloc_strdup(this->fields
.structure
,
131 mtx_unlock(&glsl_type::mutex
);
134 glsl_type::glsl_type(const glsl_struct_field
*fields
, unsigned num_fields
,
135 enum glsl_interface_packing packing
,
136 bool row_major
, const char *name
) :
138 base_type(GLSL_TYPE_INTERFACE
),
139 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
140 sampled_type(0), interface_packing((unsigned) packing
),
141 interface_row_major((unsigned) row_major
),
142 vector_elements(0), matrix_columns(0),
147 mtx_lock(&glsl_type::mutex
);
149 init_ralloc_type_ctx();
150 assert(name
!= NULL
);
151 this->name
= ralloc_strdup(this->mem_ctx
, name
);
152 this->fields
.structure
= rzalloc_array(this->mem_ctx
,
153 glsl_struct_field
, length
);
154 for (i
= 0; i
< length
; i
++) {
155 this->fields
.structure
[i
] = fields
[i
];
156 this->fields
.structure
[i
].name
= ralloc_strdup(this->fields
.structure
,
160 mtx_unlock(&glsl_type::mutex
);
163 glsl_type::glsl_type(const glsl_type
*return_type
,
164 const glsl_function_param
*params
, unsigned num_params
) :
166 base_type(GLSL_TYPE_FUNCTION
),
167 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
168 sampled_type(0), interface_packing(0), interface_row_major(0),
169 vector_elements(0), matrix_columns(0),
174 mtx_lock(&glsl_type::mutex
);
176 init_ralloc_type_ctx();
178 this->fields
.parameters
= rzalloc_array(this->mem_ctx
,
179 glsl_function_param
, num_params
+ 1);
181 /* We store the return type as the first parameter */
182 this->fields
.parameters
[0].type
= return_type
;
183 this->fields
.parameters
[0].in
= false;
184 this->fields
.parameters
[0].out
= true;
186 /* We store the i'th parameter in slot i+1 */
187 for (i
= 0; i
< length
; i
++) {
188 this->fields
.parameters
[i
+ 1].type
= params
[i
].type
;
189 this->fields
.parameters
[i
+ 1].in
= params
[i
].in
;
190 this->fields
.parameters
[i
+ 1].out
= params
[i
].out
;
193 mtx_unlock(&glsl_type::mutex
);
196 glsl_type::glsl_type(const char *subroutine_name
) :
198 base_type(GLSL_TYPE_SUBROUTINE
),
199 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
200 sampled_type(0), interface_packing(0), interface_row_major(0),
201 vector_elements(1), matrix_columns(1),
204 mtx_lock(&glsl_type::mutex
);
206 init_ralloc_type_ctx();
207 assert(subroutine_name
!= NULL
);
208 this->name
= ralloc_strdup(this->mem_ctx
, subroutine_name
);
209 mtx_unlock(&glsl_type::mutex
);
213 glsl_type::contains_sampler() const
215 if (this->is_array()) {
216 return this->fields
.array
->contains_sampler();
217 } else if (this->is_record() || this->is_interface()) {
218 for (unsigned int i
= 0; i
< this->length
; i
++) {
219 if (this->fields
.structure
[i
].type
->contains_sampler())
224 return this->is_sampler();
230 glsl_type::contains_integer() const
232 if (this->is_array()) {
233 return this->fields
.array
->contains_integer();
234 } else if (this->is_record() || this->is_interface()) {
235 for (unsigned int i
= 0; i
< this->length
; i
++) {
236 if (this->fields
.structure
[i
].type
->contains_integer())
241 return this->is_integer();
246 glsl_type::contains_double() const
248 if (this->is_array()) {
249 return this->fields
.array
->contains_double();
250 } else if (this->is_record() || this->is_interface()) {
251 for (unsigned int i
= 0; i
< this->length
; i
++) {
252 if (this->fields
.structure
[i
].type
->contains_double())
257 return this->is_double();
262 glsl_type::contains_opaque() const {
264 case GLSL_TYPE_SAMPLER
:
265 case GLSL_TYPE_IMAGE
:
266 case GLSL_TYPE_ATOMIC_UINT
:
268 case GLSL_TYPE_ARRAY
:
269 return fields
.array
->contains_opaque();
270 case GLSL_TYPE_STRUCT
:
271 case GLSL_TYPE_INTERFACE
:
272 for (unsigned int i
= 0; i
< length
; i
++) {
273 if (fields
.structure
[i
].type
->contains_opaque())
283 glsl_type::contains_subroutine() const
285 if (this->is_array()) {
286 return this->fields
.array
->contains_subroutine();
287 } else if (this->is_record() || this->is_interface()) {
288 for (unsigned int i
= 0; i
< this->length
; i
++) {
289 if (this->fields
.structure
[i
].type
->contains_subroutine())
294 return this->is_subroutine();
299 glsl_type::sampler_index() const
301 const glsl_type
*const t
= (this->is_array()) ? this->fields
.array
: this;
303 assert(t
->is_sampler());
305 switch (t
->sampler_dimensionality
) {
306 case GLSL_SAMPLER_DIM_1D
:
307 return (t
->sampler_array
) ? TEXTURE_1D_ARRAY_INDEX
: TEXTURE_1D_INDEX
;
308 case GLSL_SAMPLER_DIM_2D
:
309 return (t
->sampler_array
) ? TEXTURE_2D_ARRAY_INDEX
: TEXTURE_2D_INDEX
;
310 case GLSL_SAMPLER_DIM_3D
:
311 return TEXTURE_3D_INDEX
;
312 case GLSL_SAMPLER_DIM_CUBE
:
313 return (t
->sampler_array
) ? TEXTURE_CUBE_ARRAY_INDEX
: TEXTURE_CUBE_INDEX
;
314 case GLSL_SAMPLER_DIM_RECT
:
315 return TEXTURE_RECT_INDEX
;
316 case GLSL_SAMPLER_DIM_BUF
:
317 return TEXTURE_BUFFER_INDEX
;
318 case GLSL_SAMPLER_DIM_EXTERNAL
:
319 return TEXTURE_EXTERNAL_INDEX
;
320 case GLSL_SAMPLER_DIM_MS
:
321 return (t
->sampler_array
) ? TEXTURE_2D_MULTISAMPLE_ARRAY_INDEX
: TEXTURE_2D_MULTISAMPLE_INDEX
;
323 assert(!"Should not get here.");
324 return TEXTURE_BUFFER_INDEX
;
329 glsl_type::contains_image() const
331 if (this->is_array()) {
332 return this->fields
.array
->contains_image();
333 } else if (this->is_record() || this->is_interface()) {
334 for (unsigned int i
= 0; i
< this->length
; i
++) {
335 if (this->fields
.structure
[i
].type
->contains_image())
340 return this->is_image();
344 const glsl_type
*glsl_type::get_base_type() const
351 case GLSL_TYPE_FLOAT
:
353 case GLSL_TYPE_DOUBLE
:
357 case GLSL_TYPE_UINT64
:
358 return uint64_t_type
;
359 case GLSL_TYPE_INT64
:
367 const glsl_type
*glsl_type::get_scalar_type() const
369 const glsl_type
*type
= this;
372 while (type
->base_type
== GLSL_TYPE_ARRAY
)
373 type
= type
->fields
.array
;
375 /* Handle vectors and matrices */
376 switch (type
->base_type
) {
381 case GLSL_TYPE_FLOAT
:
383 case GLSL_TYPE_DOUBLE
:
387 case GLSL_TYPE_UINT64
:
388 return uint64_t_type
;
389 case GLSL_TYPE_INT64
:
392 /* Handle everything else */
399 _mesa_glsl_release_types(void)
401 /* Should only be called during atexit (either when unloading shared
402 * object, or if process terminates), so no mutex-locking should be
405 if (glsl_type::array_types
!= NULL
) {
406 _mesa_hash_table_destroy(glsl_type::array_types
, NULL
);
407 glsl_type::array_types
= NULL
;
410 if (glsl_type::record_types
!= NULL
) {
411 _mesa_hash_table_destroy(glsl_type::record_types
, NULL
);
412 glsl_type::record_types
= NULL
;
415 if (glsl_type::interface_types
!= NULL
) {
416 _mesa_hash_table_destroy(glsl_type::interface_types
, NULL
);
417 glsl_type::interface_types
= NULL
;
422 glsl_type::glsl_type(const glsl_type
*array
, unsigned length
) :
423 base_type(GLSL_TYPE_ARRAY
),
424 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
425 sampled_type(0), interface_packing(0), interface_row_major(0),
426 vector_elements(0), matrix_columns(0),
427 length(length
), name(NULL
)
429 this->fields
.array
= array
;
430 /* Inherit the gl type of the base. The GL type is used for
431 * uniform/statevar handling in Mesa and the arrayness of the type
432 * is represented by the size rather than the type.
434 this->gl_type
= array
->gl_type
;
436 /* Allow a maximum of 10 characters for the array size. This is enough
437 * for 32-bits of ~0. The extra 3 are for the '[', ']', and terminating
440 const unsigned name_length
= strlen(array
->name
) + 10 + 3;
442 mtx_lock(&glsl_type::mutex
);
443 char *const n
= (char *) ralloc_size(this->mem_ctx
, name_length
);
444 mtx_unlock(&glsl_type::mutex
);
447 snprintf(n
, name_length
, "%s[]", array
->name
);
449 /* insert outermost dimensions in the correct spot
450 * otherwise the dimension order will be backwards
452 const char *pos
= strchr(array
->name
, '[');
454 int idx
= pos
- array
->name
;
455 snprintf(n
, idx
+1, "%s", array
->name
);
456 snprintf(n
+ idx
, name_length
- idx
, "[%u]%s",
457 length
, array
->name
+ idx
);
459 snprintf(n
, name_length
, "%s[%u]", array
->name
, length
);
468 glsl_type::vec(unsigned components
)
470 if (components
== 0 || components
> 4)
473 static const glsl_type
*const ts
[] = {
474 float_type
, vec2_type
, vec3_type
, vec4_type
476 return ts
[components
- 1];
480 glsl_type::dvec(unsigned components
)
482 if (components
== 0 || components
> 4)
485 static const glsl_type
*const ts
[] = {
486 double_type
, dvec2_type
, dvec3_type
, dvec4_type
488 return ts
[components
- 1];
492 glsl_type::ivec(unsigned components
)
494 if (components
== 0 || components
> 4)
497 static const glsl_type
*const ts
[] = {
498 int_type
, ivec2_type
, ivec3_type
, ivec4_type
500 return ts
[components
- 1];
505 glsl_type::uvec(unsigned components
)
507 if (components
== 0 || components
> 4)
510 static const glsl_type
*const ts
[] = {
511 uint_type
, uvec2_type
, uvec3_type
, uvec4_type
513 return ts
[components
- 1];
518 glsl_type::bvec(unsigned components
)
520 if (components
== 0 || components
> 4)
523 static const glsl_type
*const ts
[] = {
524 bool_type
, bvec2_type
, bvec3_type
, bvec4_type
526 return ts
[components
- 1];
531 glsl_type::i64vec(unsigned components
)
533 if (components
== 0 || components
> 4)
536 static const glsl_type
*const ts
[] = {
537 int64_t_type
, i64vec2_type
, i64vec3_type
, i64vec4_type
539 return ts
[components
- 1];
544 glsl_type::u64vec(unsigned components
)
546 if (components
== 0 || components
> 4)
549 static const glsl_type
*const ts
[] = {
550 uint64_t_type
, u64vec2_type
, u64vec3_type
, u64vec4_type
552 return ts
[components
- 1];
556 glsl_type::get_instance(unsigned base_type
, unsigned rows
, unsigned columns
)
558 if (base_type
== GLSL_TYPE_VOID
)
561 if ((rows
< 1) || (rows
> 4) || (columns
< 1) || (columns
> 4))
564 /* Treat GLSL vectors as Nx1 matrices.
572 case GLSL_TYPE_FLOAT
:
574 case GLSL_TYPE_DOUBLE
:
578 case GLSL_TYPE_UINT64
:
580 case GLSL_TYPE_INT64
:
586 if ((base_type
!= GLSL_TYPE_FLOAT
&& base_type
!= GLSL_TYPE_DOUBLE
) || (rows
== 1))
589 /* GLSL matrix types are named mat{COLUMNS}x{ROWS}. Only the following
590 * combinations are valid:
598 #define IDX(c,r) (((c-1)*3) + (r-1))
600 if (base_type
== GLSL_TYPE_DOUBLE
) {
601 switch (IDX(columns
, rows
)) {
602 case IDX(2,2): return dmat2_type
;
603 case IDX(2,3): return dmat2x3_type
;
604 case IDX(2,4): return dmat2x4_type
;
605 case IDX(3,2): return dmat3x2_type
;
606 case IDX(3,3): return dmat3_type
;
607 case IDX(3,4): return dmat3x4_type
;
608 case IDX(4,2): return dmat4x2_type
;
609 case IDX(4,3): return dmat4x3_type
;
610 case IDX(4,4): return dmat4_type
;
611 default: return error_type
;
614 switch (IDX(columns
, rows
)) {
615 case IDX(2,2): return mat2_type
;
616 case IDX(2,3): return mat2x3_type
;
617 case IDX(2,4): return mat2x4_type
;
618 case IDX(3,2): return mat3x2_type
;
619 case IDX(3,3): return mat3_type
;
620 case IDX(3,4): return mat3x4_type
;
621 case IDX(4,2): return mat4x2_type
;
622 case IDX(4,3): return mat4x3_type
;
623 case IDX(4,4): return mat4_type
;
624 default: return error_type
;
629 assert(!"Should not get here.");
634 glsl_type::get_sampler_instance(enum glsl_sampler_dim dim
,
640 case GLSL_TYPE_FLOAT
:
642 case GLSL_SAMPLER_DIM_1D
:
644 return (array
? sampler1DArrayShadow_type
: sampler1DShadow_type
);
646 return (array
? sampler1DArray_type
: sampler1D_type
);
647 case GLSL_SAMPLER_DIM_2D
:
649 return (array
? sampler2DArrayShadow_type
: sampler2DShadow_type
);
651 return (array
? sampler2DArray_type
: sampler2D_type
);
652 case GLSL_SAMPLER_DIM_3D
:
656 return sampler3D_type
;
657 case GLSL_SAMPLER_DIM_CUBE
:
659 return (array
? samplerCubeArrayShadow_type
: samplerCubeShadow_type
);
661 return (array
? samplerCubeArray_type
: samplerCube_type
);
662 case GLSL_SAMPLER_DIM_RECT
:
666 return sampler2DRectShadow_type
;
668 return sampler2DRect_type
;
669 case GLSL_SAMPLER_DIM_BUF
:
673 return samplerBuffer_type
;
674 case GLSL_SAMPLER_DIM_MS
:
677 return (array
? sampler2DMSArray_type
: sampler2DMS_type
);
678 case GLSL_SAMPLER_DIM_EXTERNAL
:
682 return samplerExternalOES_type
;
683 case GLSL_SAMPLER_DIM_SUBPASS
:
684 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
691 case GLSL_SAMPLER_DIM_1D
:
692 return (array
? isampler1DArray_type
: isampler1D_type
);
693 case GLSL_SAMPLER_DIM_2D
:
694 return (array
? isampler2DArray_type
: isampler2D_type
);
695 case GLSL_SAMPLER_DIM_3D
:
698 return isampler3D_type
;
699 case GLSL_SAMPLER_DIM_CUBE
:
700 return (array
? isamplerCubeArray_type
: isamplerCube_type
);
701 case GLSL_SAMPLER_DIM_RECT
:
704 return isampler2DRect_type
;
705 case GLSL_SAMPLER_DIM_BUF
:
708 return isamplerBuffer_type
;
709 case GLSL_SAMPLER_DIM_MS
:
710 return (array
? isampler2DMSArray_type
: isampler2DMS_type
);
711 case GLSL_SAMPLER_DIM_EXTERNAL
:
713 case GLSL_SAMPLER_DIM_SUBPASS
:
714 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
721 case GLSL_SAMPLER_DIM_1D
:
722 return (array
? usampler1DArray_type
: usampler1D_type
);
723 case GLSL_SAMPLER_DIM_2D
:
724 return (array
? usampler2DArray_type
: usampler2D_type
);
725 case GLSL_SAMPLER_DIM_3D
:
728 return usampler3D_type
;
729 case GLSL_SAMPLER_DIM_CUBE
:
730 return (array
? usamplerCubeArray_type
: usamplerCube_type
);
731 case GLSL_SAMPLER_DIM_RECT
:
734 return usampler2DRect_type
;
735 case GLSL_SAMPLER_DIM_BUF
:
738 return usamplerBuffer_type
;
739 case GLSL_SAMPLER_DIM_MS
:
740 return (array
? usampler2DMSArray_type
: usampler2DMS_type
);
741 case GLSL_SAMPLER_DIM_EXTERNAL
:
743 case GLSL_SAMPLER_DIM_SUBPASS
:
744 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
751 unreachable("switch statement above should be complete");
755 glsl_type::get_image_instance(enum glsl_sampler_dim dim
,
756 bool array
, glsl_base_type type
)
759 case GLSL_TYPE_FLOAT
:
761 case GLSL_SAMPLER_DIM_1D
:
762 return (array
? image1DArray_type
: image1D_type
);
763 case GLSL_SAMPLER_DIM_2D
:
764 return (array
? image2DArray_type
: image2D_type
);
765 case GLSL_SAMPLER_DIM_3D
:
767 case GLSL_SAMPLER_DIM_CUBE
:
768 return (array
? imageCubeArray_type
: imageCube_type
);
769 case GLSL_SAMPLER_DIM_RECT
:
773 return image2DRect_type
;
774 case GLSL_SAMPLER_DIM_BUF
:
778 return imageBuffer_type
;
779 case GLSL_SAMPLER_DIM_MS
:
780 return (array
? image2DMSArray_type
: image2DMS_type
);
781 case GLSL_SAMPLER_DIM_SUBPASS
:
782 return subpassInput_type
;
783 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
784 return subpassInputMS_type
;
785 case GLSL_SAMPLER_DIM_EXTERNAL
:
790 case GLSL_SAMPLER_DIM_1D
:
791 return (array
? iimage1DArray_type
: iimage1D_type
);
792 case GLSL_SAMPLER_DIM_2D
:
793 return (array
? iimage2DArray_type
: iimage2D_type
);
794 case GLSL_SAMPLER_DIM_3D
:
797 return iimage3D_type
;
798 case GLSL_SAMPLER_DIM_CUBE
:
799 return (array
? iimageCubeArray_type
: iimageCube_type
);
800 case GLSL_SAMPLER_DIM_RECT
:
803 return iimage2DRect_type
;
804 case GLSL_SAMPLER_DIM_BUF
:
807 return iimageBuffer_type
;
808 case GLSL_SAMPLER_DIM_MS
:
809 return (array
? iimage2DMSArray_type
: iimage2DMS_type
);
810 case GLSL_SAMPLER_DIM_SUBPASS
:
811 return isubpassInput_type
;
812 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
813 return isubpassInputMS_type
;
814 case GLSL_SAMPLER_DIM_EXTERNAL
:
819 case GLSL_SAMPLER_DIM_1D
:
820 return (array
? uimage1DArray_type
: uimage1D_type
);
821 case GLSL_SAMPLER_DIM_2D
:
822 return (array
? uimage2DArray_type
: uimage2D_type
);
823 case GLSL_SAMPLER_DIM_3D
:
826 return uimage3D_type
;
827 case GLSL_SAMPLER_DIM_CUBE
:
828 return (array
? uimageCubeArray_type
: uimageCube_type
);
829 case GLSL_SAMPLER_DIM_RECT
:
832 return uimage2DRect_type
;
833 case GLSL_SAMPLER_DIM_BUF
:
836 return uimageBuffer_type
;
837 case GLSL_SAMPLER_DIM_MS
:
838 return (array
? uimage2DMSArray_type
: uimage2DMS_type
);
839 case GLSL_SAMPLER_DIM_SUBPASS
:
840 return usubpassInput_type
;
841 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
842 return usubpassInputMS_type
;
843 case GLSL_SAMPLER_DIM_EXTERNAL
:
850 unreachable("switch statement above should be complete");
854 glsl_type::get_array_instance(const glsl_type
*base
, unsigned array_size
)
856 /* Generate a name using the base type pointer in the key. This is
857 * done because the name of the base type may not be unique across
858 * shaders. For example, two shaders may have different record types
862 snprintf(key
, sizeof(key
), "%p[%u]", (void *) base
, array_size
);
864 mtx_lock(&glsl_type::mutex
);
866 if (array_types
== NULL
) {
867 array_types
= _mesa_hash_table_create(NULL
, _mesa_key_hash_string
,
868 _mesa_key_string_equal
);
871 const struct hash_entry
*entry
= _mesa_hash_table_search(array_types
, key
);
873 mtx_unlock(&glsl_type::mutex
);
874 const glsl_type
*t
= new glsl_type(base
, array_size
);
875 mtx_lock(&glsl_type::mutex
);
877 entry
= _mesa_hash_table_insert(array_types
,
878 ralloc_strdup(mem_ctx
, key
),
882 assert(((glsl_type
*) entry
->data
)->base_type
== GLSL_TYPE_ARRAY
);
883 assert(((glsl_type
*) entry
->data
)->length
== array_size
);
884 assert(((glsl_type
*) entry
->data
)->fields
.array
== base
);
886 mtx_unlock(&glsl_type::mutex
);
888 return (glsl_type
*) entry
->data
;
893 glsl_type::record_compare(const glsl_type
*b
, bool match_locations
) const
895 if (this->length
!= b
->length
)
898 if (this->interface_packing
!= b
->interface_packing
)
901 if (this->interface_row_major
!= b
->interface_row_major
)
904 /* From the GLSL 4.20 specification (Sec 4.2):
906 * "Structures must have the same name, sequence of type names, and
907 * type definitions, and field names to be considered the same type."
909 * GLSL ES behaves the same (Ver 1.00 Sec 4.2.4, Ver 3.00 Sec 4.2.5).
911 * Note that we cannot force type name check when comparing unnamed
912 * structure types, these have a unique name assigned during parsing.
914 if (!this->is_anonymous() && !b
->is_anonymous())
915 if (strcmp(this->name
, b
->name
) != 0)
918 for (unsigned i
= 0; i
< this->length
; i
++) {
919 if (this->fields
.structure
[i
].type
!= b
->fields
.structure
[i
].type
)
921 if (strcmp(this->fields
.structure
[i
].name
,
922 b
->fields
.structure
[i
].name
) != 0)
924 if (this->fields
.structure
[i
].matrix_layout
925 != b
->fields
.structure
[i
].matrix_layout
)
927 if (match_locations
&& this->fields
.structure
[i
].location
928 != b
->fields
.structure
[i
].location
)
930 if (this->fields
.structure
[i
].offset
931 != b
->fields
.structure
[i
].offset
)
933 if (this->fields
.structure
[i
].interpolation
934 != b
->fields
.structure
[i
].interpolation
)
936 if (this->fields
.structure
[i
].centroid
937 != b
->fields
.structure
[i
].centroid
)
939 if (this->fields
.structure
[i
].sample
940 != b
->fields
.structure
[i
].sample
)
942 if (this->fields
.structure
[i
].patch
943 != b
->fields
.structure
[i
].patch
)
945 if (this->fields
.structure
[i
].image_read_only
946 != b
->fields
.structure
[i
].image_read_only
)
948 if (this->fields
.structure
[i
].image_write_only
949 != b
->fields
.structure
[i
].image_write_only
)
951 if (this->fields
.structure
[i
].image_coherent
952 != b
->fields
.structure
[i
].image_coherent
)
954 if (this->fields
.structure
[i
].image_volatile
955 != b
->fields
.structure
[i
].image_volatile
)
957 if (this->fields
.structure
[i
].image_restrict
958 != b
->fields
.structure
[i
].image_restrict
)
960 if (this->fields
.structure
[i
].precision
961 != b
->fields
.structure
[i
].precision
)
963 if (this->fields
.structure
[i
].explicit_xfb_buffer
964 != b
->fields
.structure
[i
].explicit_xfb_buffer
)
966 if (this->fields
.structure
[i
].xfb_buffer
967 != b
->fields
.structure
[i
].xfb_buffer
)
969 if (this->fields
.structure
[i
].xfb_stride
970 != b
->fields
.structure
[i
].xfb_stride
)
979 glsl_type::record_key_compare(const void *a
, const void *b
)
981 const glsl_type
*const key1
= (glsl_type
*) a
;
982 const glsl_type
*const key2
= (glsl_type
*) b
;
984 return strcmp(key1
->name
, key2
->name
) == 0 && key1
->record_compare(key2
);
989 * Generate an integer hash value for a glsl_type structure type.
992 glsl_type::record_key_hash(const void *a
)
994 const glsl_type
*const key
= (glsl_type
*) a
;
995 uintptr_t hash
= key
->length
;
998 for (unsigned i
= 0; i
< key
->length
; i
++) {
999 /* casting pointer to uintptr_t */
1000 hash
= (hash
* 13 ) + (uintptr_t) key
->fields
.structure
[i
].type
;
1003 if (sizeof(hash
) == 8)
1004 retval
= (hash
& 0xffffffff) ^ ((uint64_t) hash
>> 32);
1013 glsl_type::get_record_instance(const glsl_struct_field
*fields
,
1014 unsigned num_fields
,
1017 const glsl_type
key(fields
, num_fields
, name
);
1019 mtx_lock(&glsl_type::mutex
);
1021 if (record_types
== NULL
) {
1022 record_types
= _mesa_hash_table_create(NULL
, record_key_hash
,
1023 record_key_compare
);
1026 const struct hash_entry
*entry
= _mesa_hash_table_search(record_types
,
1028 if (entry
== NULL
) {
1029 mtx_unlock(&glsl_type::mutex
);
1030 const glsl_type
*t
= new glsl_type(fields
, num_fields
, name
);
1031 mtx_lock(&glsl_type::mutex
);
1033 entry
= _mesa_hash_table_insert(record_types
, t
, (void *) t
);
1036 assert(((glsl_type
*) entry
->data
)->base_type
== GLSL_TYPE_STRUCT
);
1037 assert(((glsl_type
*) entry
->data
)->length
== num_fields
);
1038 assert(strcmp(((glsl_type
*) entry
->data
)->name
, name
) == 0);
1040 mtx_unlock(&glsl_type::mutex
);
1042 return (glsl_type
*) entry
->data
;
1047 glsl_type::get_interface_instance(const glsl_struct_field
*fields
,
1048 unsigned num_fields
,
1049 enum glsl_interface_packing packing
,
1051 const char *block_name
)
1053 const glsl_type
key(fields
, num_fields
, packing
, row_major
, block_name
);
1055 mtx_lock(&glsl_type::mutex
);
1057 if (interface_types
== NULL
) {
1058 interface_types
= _mesa_hash_table_create(NULL
, record_key_hash
,
1059 record_key_compare
);
1062 const struct hash_entry
*entry
= _mesa_hash_table_search(interface_types
,
1064 if (entry
== NULL
) {
1065 mtx_unlock(&glsl_type::mutex
);
1066 const glsl_type
*t
= new glsl_type(fields
, num_fields
,
1067 packing
, row_major
, block_name
);
1068 mtx_lock(&glsl_type::mutex
);
1070 entry
= _mesa_hash_table_insert(interface_types
, t
, (void *) t
);
1073 assert(((glsl_type
*) entry
->data
)->base_type
== GLSL_TYPE_INTERFACE
);
1074 assert(((glsl_type
*) entry
->data
)->length
== num_fields
);
1075 assert(strcmp(((glsl_type
*) entry
->data
)->name
, block_name
) == 0);
1077 mtx_unlock(&glsl_type::mutex
);
1079 return (glsl_type
*) entry
->data
;
1083 glsl_type::get_subroutine_instance(const char *subroutine_name
)
1085 const glsl_type
key(subroutine_name
);
1087 mtx_lock(&glsl_type::mutex
);
1089 if (subroutine_types
== NULL
) {
1090 subroutine_types
= _mesa_hash_table_create(NULL
, record_key_hash
,
1091 record_key_compare
);
1094 const struct hash_entry
*entry
= _mesa_hash_table_search(subroutine_types
,
1096 if (entry
== NULL
) {
1097 mtx_unlock(&glsl_type::mutex
);
1098 const glsl_type
*t
= new glsl_type(subroutine_name
);
1099 mtx_lock(&glsl_type::mutex
);
1101 entry
= _mesa_hash_table_insert(subroutine_types
, t
, (void *) t
);
1104 assert(((glsl_type
*) entry
->data
)->base_type
== GLSL_TYPE_SUBROUTINE
);
1105 assert(strcmp(((glsl_type
*) entry
->data
)->name
, subroutine_name
) == 0);
1107 mtx_unlock(&glsl_type::mutex
);
1109 return (glsl_type
*) entry
->data
;
1114 function_key_compare(const void *a
, const void *b
)
1116 const glsl_type
*const key1
= (glsl_type
*) a
;
1117 const glsl_type
*const key2
= (glsl_type
*) b
;
1119 if (key1
->length
!= key2
->length
)
1122 return memcmp(key1
->fields
.parameters
, key2
->fields
.parameters
,
1123 (key1
->length
+ 1) * sizeof(*key1
->fields
.parameters
)) == 0;
1128 function_key_hash(const void *a
)
1130 const glsl_type
*const key
= (glsl_type
*) a
;
1131 return _mesa_hash_data(key
->fields
.parameters
,
1132 (key
->length
+ 1) * sizeof(*key
->fields
.parameters
));
1136 glsl_type::get_function_instance(const glsl_type
*return_type
,
1137 const glsl_function_param
*params
,
1138 unsigned num_params
)
1140 const glsl_type
key(return_type
, params
, num_params
);
1142 mtx_lock(&glsl_type::mutex
);
1144 if (function_types
== NULL
) {
1145 function_types
= _mesa_hash_table_create(NULL
, function_key_hash
,
1146 function_key_compare
);
1149 struct hash_entry
*entry
= _mesa_hash_table_search(function_types
, &key
);
1150 if (entry
== NULL
) {
1151 mtx_unlock(&glsl_type::mutex
);
1152 const glsl_type
*t
= new glsl_type(return_type
, params
, num_params
);
1153 mtx_lock(&glsl_type::mutex
);
1155 entry
= _mesa_hash_table_insert(function_types
, t
, (void *) t
);
1158 const glsl_type
*t
= (const glsl_type
*)entry
->data
;
1160 assert(t
->base_type
== GLSL_TYPE_FUNCTION
);
1161 assert(t
->length
== num_params
);
1163 mtx_unlock(&glsl_type::mutex
);
1170 glsl_type::get_mul_type(const glsl_type
*type_a
, const glsl_type
*type_b
)
1172 if (type_a
== type_b
) {
1174 } else if (type_a
->is_matrix() && type_b
->is_matrix()) {
1175 /* Matrix multiply. The columns of A must match the rows of B. Given
1176 * the other previously tested constraints, this means the vector type
1177 * of a row from A must be the same as the vector type of a column from
1180 if (type_a
->row_type() == type_b
->column_type()) {
1181 /* The resulting matrix has the number of columns of matrix B and
1182 * the number of rows of matrix A. We get the row count of A by
1183 * looking at the size of a vector that makes up a column. The
1184 * transpose (size of a row) is done for B.
1186 const glsl_type
*const type
=
1187 get_instance(type_a
->base_type
,
1188 type_a
->column_type()->vector_elements
,
1189 type_b
->row_type()->vector_elements
);
1190 assert(type
!= error_type
);
1194 } else if (type_a
->is_matrix()) {
1195 /* A is a matrix and B is a column vector. Columns of A must match
1196 * rows of B. Given the other previously tested constraints, this
1197 * means the vector type of a row from A must be the same as the
1198 * vector the type of B.
1200 if (type_a
->row_type() == type_b
) {
1201 /* The resulting vector has a number of elements equal to
1202 * the number of rows of matrix A. */
1203 const glsl_type
*const type
=
1204 get_instance(type_a
->base_type
,
1205 type_a
->column_type()->vector_elements
,
1207 assert(type
!= error_type
);
1212 assert(type_b
->is_matrix());
1214 /* A is a row vector and B is a matrix. Columns of A must match rows
1215 * of B. Given the other previously tested constraints, this means
1216 * the type of A must be the same as the vector type of a column from
1219 if (type_a
== type_b
->column_type()) {
1220 /* The resulting vector has a number of elements equal to
1221 * the number of columns of matrix B. */
1222 const glsl_type
*const type
=
1223 get_instance(type_a
->base_type
,
1224 type_b
->row_type()->vector_elements
,
1226 assert(type
!= error_type
);
1237 glsl_type::field_type(const char *name
) const
1239 if (this->base_type
!= GLSL_TYPE_STRUCT
1240 && this->base_type
!= GLSL_TYPE_INTERFACE
)
1243 for (unsigned i
= 0; i
< this->length
; i
++) {
1244 if (strcmp(name
, this->fields
.structure
[i
].name
) == 0)
1245 return this->fields
.structure
[i
].type
;
1253 glsl_type::field_index(const char *name
) const
1255 if (this->base_type
!= GLSL_TYPE_STRUCT
1256 && this->base_type
!= GLSL_TYPE_INTERFACE
)
1259 for (unsigned i
= 0; i
< this->length
; i
++) {
1260 if (strcmp(name
, this->fields
.structure
[i
].name
) == 0)
1269 glsl_type::component_slots() const
1271 switch (this->base_type
) {
1272 case GLSL_TYPE_UINT
:
1274 case GLSL_TYPE_FLOAT
:
1275 case GLSL_TYPE_BOOL
:
1276 return this->components();
1278 case GLSL_TYPE_DOUBLE
:
1279 case GLSL_TYPE_UINT64
:
1280 case GLSL_TYPE_INT64
:
1281 return 2 * this->components();
1283 case GLSL_TYPE_STRUCT
:
1284 case GLSL_TYPE_INTERFACE
: {
1287 for (unsigned i
= 0; i
< this->length
; i
++)
1288 size
+= this->fields
.structure
[i
].type
->component_slots();
1293 case GLSL_TYPE_ARRAY
:
1294 return this->length
* this->fields
.array
->component_slots();
1296 case GLSL_TYPE_IMAGE
:
1298 case GLSL_TYPE_SUBROUTINE
:
1301 case GLSL_TYPE_FUNCTION
:
1302 case GLSL_TYPE_SAMPLER
:
1303 case GLSL_TYPE_ATOMIC_UINT
:
1304 case GLSL_TYPE_VOID
:
1305 case GLSL_TYPE_ERROR
:
1313 glsl_type::record_location_offset(unsigned length
) const
1315 unsigned offset
= 0;
1316 const glsl_type
*t
= this->without_array();
1317 if (t
->is_record()) {
1318 assert(length
<= t
->length
);
1320 for (unsigned i
= 0; i
< length
; i
++) {
1321 const glsl_type
*st
= t
->fields
.structure
[i
].type
;
1322 const glsl_type
*wa
= st
->without_array();
1323 if (wa
->is_record()) {
1324 unsigned r_offset
= wa
->record_location_offset(wa
->length
);
1325 offset
+= st
->is_array() ?
1326 st
->arrays_of_arrays_size() * r_offset
: r_offset
;
1327 } else if (st
->is_array() && st
->fields
.array
->is_array()) {
1328 unsigned outer_array_size
= st
->length
;
1329 const glsl_type
*base_type
= st
->fields
.array
;
1331 /* For arrays of arrays the outer arrays take up a uniform
1332 * slot for each element. The innermost array elements share a
1333 * single slot so we ignore the innermost array when calculating
1336 while (base_type
->fields
.array
->is_array()) {
1337 outer_array_size
= outer_array_size
* base_type
->length
;
1338 base_type
= base_type
->fields
.array
;
1340 offset
+= outer_array_size
;
1342 /* We dont worry about arrays here because unless the array
1343 * contains a structure or another array it only takes up a single
1354 glsl_type::uniform_locations() const
1358 switch (this->base_type
) {
1359 case GLSL_TYPE_UINT
:
1361 case GLSL_TYPE_FLOAT
:
1362 case GLSL_TYPE_DOUBLE
:
1363 case GLSL_TYPE_UINT64
:
1364 case GLSL_TYPE_INT64
:
1365 case GLSL_TYPE_BOOL
:
1366 case GLSL_TYPE_SAMPLER
:
1367 case GLSL_TYPE_IMAGE
:
1368 case GLSL_TYPE_SUBROUTINE
:
1371 case GLSL_TYPE_STRUCT
:
1372 case GLSL_TYPE_INTERFACE
:
1373 for (unsigned i
= 0; i
< this->length
; i
++)
1374 size
+= this->fields
.structure
[i
].type
->uniform_locations();
1376 case GLSL_TYPE_ARRAY
:
1377 return this->length
* this->fields
.array
->uniform_locations();
1384 glsl_type::varying_count() const
1388 switch (this->base_type
) {
1389 case GLSL_TYPE_UINT
:
1391 case GLSL_TYPE_FLOAT
:
1392 case GLSL_TYPE_DOUBLE
:
1393 case GLSL_TYPE_BOOL
:
1394 case GLSL_TYPE_UINT64
:
1395 case GLSL_TYPE_INT64
:
1398 case GLSL_TYPE_STRUCT
:
1399 case GLSL_TYPE_INTERFACE
:
1400 for (unsigned i
= 0; i
< this->length
; i
++)
1401 size
+= this->fields
.structure
[i
].type
->varying_count();
1403 case GLSL_TYPE_ARRAY
:
1404 /* Don't count innermost array elements */
1405 if (this->without_array()->is_record() ||
1406 this->without_array()->is_interface() ||
1407 this->fields
.array
->is_array())
1408 return this->length
* this->fields
.array
->varying_count();
1410 return this->fields
.array
->varying_count();
1412 assert(!"unsupported varying type");
1418 glsl_type::can_implicitly_convert_to(const glsl_type
*desired
,
1419 _mesa_glsl_parse_state
*state
) const
1421 if (this == desired
)
1424 /* GLSL 1.10 and ESSL do not allow implicit conversions. If there is no
1425 * state, we're doing intra-stage function linking where these checks have
1426 * already been done.
1428 if (state
&& (state
->es_shader
|| !state
->is_version(120, 0)))
1431 /* There is no conversion among matrix types. */
1432 if (this->matrix_columns
> 1 || desired
->matrix_columns
> 1)
1435 /* Vector size must match. */
1436 if (this->vector_elements
!= desired
->vector_elements
)
1439 /* int and uint can be converted to float. */
1440 if (desired
->is_float() && this->is_integer())
1443 /* With GLSL 4.0, ARB_gpu_shader5, or MESA_shader_integer_functions, int
1444 * can be converted to uint. Note that state may be NULL here, when
1445 * resolving function calls in the linker. By this time, all the
1446 * state-dependent checks have already happened though, so allow anything
1447 * that's allowed in any shader version.
1449 if ((!state
|| state
->is_version(400, 0) || state
->ARB_gpu_shader5_enable
||
1450 state
->MESA_shader_integer_functions_enable
) &&
1451 desired
->base_type
== GLSL_TYPE_UINT
&& this->base_type
== GLSL_TYPE_INT
)
1454 /* No implicit conversions from double. */
1455 if ((!state
|| state
->has_double()) && this->is_double())
1458 /* Conversions from different types to double. */
1459 if ((!state
|| state
->has_double()) && desired
->is_double()) {
1460 if (this->is_float())
1462 if (this->is_integer())
1470 glsl_type::std140_base_alignment(bool row_major
) const
1472 unsigned N
= is_64bit() ? 8 : 4;
1474 /* (1) If the member is a scalar consuming <N> basic machine units, the
1475 * base alignment is <N>.
1477 * (2) If the member is a two- or four-component vector with components
1478 * consuming <N> basic machine units, the base alignment is 2<N> or
1479 * 4<N>, respectively.
1481 * (3) If the member is a three-component vector with components consuming
1482 * <N> basic machine units, the base alignment is 4<N>.
1484 if (this->is_scalar() || this->is_vector()) {
1485 switch (this->vector_elements
) {
1496 /* (4) If the member is an array of scalars or vectors, the base alignment
1497 * and array stride are set to match the base alignment of a single
1498 * array element, according to rules (1), (2), and (3), and rounded up
1499 * to the base alignment of a vec4. The array may have padding at the
1500 * end; the base offset of the member following the array is rounded up
1501 * to the next multiple of the base alignment.
1503 * (6) If the member is an array of <S> column-major matrices with <C>
1504 * columns and <R> rows, the matrix is stored identically to a row of
1505 * <S>*<C> column vectors with <R> components each, according to rule
1508 * (8) If the member is an array of <S> row-major matrices with <C> columns
1509 * and <R> rows, the matrix is stored identically to a row of <S>*<R>
1510 * row vectors with <C> components each, according to rule (4).
1512 * (10) If the member is an array of <S> structures, the <S> elements of
1513 * the array are laid out in order, according to rule (9).
1515 if (this->is_array()) {
1516 if (this->fields
.array
->is_scalar() ||
1517 this->fields
.array
->is_vector() ||
1518 this->fields
.array
->is_matrix()) {
1519 return MAX2(this->fields
.array
->std140_base_alignment(row_major
), 16);
1521 assert(this->fields
.array
->is_record() ||
1522 this->fields
.array
->is_array());
1523 return this->fields
.array
->std140_base_alignment(row_major
);
1527 /* (5) If the member is a column-major matrix with <C> columns and
1528 * <R> rows, the matrix is stored identically to an array of
1529 * <C> column vectors with <R> components each, according to
1532 * (7) If the member is a row-major matrix with <C> columns and <R>
1533 * rows, the matrix is stored identically to an array of <R>
1534 * row vectors with <C> components each, according to rule (4).
1536 if (this->is_matrix()) {
1537 const struct glsl_type
*vec_type
, *array_type
;
1538 int c
= this->matrix_columns
;
1539 int r
= this->vector_elements
;
1542 vec_type
= get_instance(base_type
, c
, 1);
1543 array_type
= glsl_type::get_array_instance(vec_type
, r
);
1545 vec_type
= get_instance(base_type
, r
, 1);
1546 array_type
= glsl_type::get_array_instance(vec_type
, c
);
1549 return array_type
->std140_base_alignment(false);
1552 /* (9) If the member is a structure, the base alignment of the
1553 * structure is <N>, where <N> is the largest base alignment
1554 * value of any of its members, and rounded up to the base
1555 * alignment of a vec4. The individual members of this
1556 * sub-structure are then assigned offsets by applying this set
1557 * of rules recursively, where the base offset of the first
1558 * member of the sub-structure is equal to the aligned offset
1559 * of the structure. The structure may have padding at the end;
1560 * the base offset of the member following the sub-structure is
1561 * rounded up to the next multiple of the base alignment of the
1564 if (this->is_record()) {
1565 unsigned base_alignment
= 16;
1566 for (unsigned i
= 0; i
< this->length
; i
++) {
1567 bool field_row_major
= row_major
;
1568 const enum glsl_matrix_layout matrix_layout
=
1569 glsl_matrix_layout(this->fields
.structure
[i
].matrix_layout
);
1570 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
1571 field_row_major
= true;
1572 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
1573 field_row_major
= false;
1576 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
1577 base_alignment
= MAX2(base_alignment
,
1578 field_type
->std140_base_alignment(field_row_major
));
1580 return base_alignment
;
1583 assert(!"not reached");
1588 glsl_type::std140_size(bool row_major
) const
1590 unsigned N
= is_64bit() ? 8 : 4;
1592 /* (1) If the member is a scalar consuming <N> basic machine units, the
1593 * base alignment is <N>.
1595 * (2) If the member is a two- or four-component vector with components
1596 * consuming <N> basic machine units, the base alignment is 2<N> or
1597 * 4<N>, respectively.
1599 * (3) If the member is a three-component vector with components consuming
1600 * <N> basic machine units, the base alignment is 4<N>.
1602 if (this->is_scalar() || this->is_vector()) {
1603 return this->vector_elements
* N
;
1606 /* (5) If the member is a column-major matrix with <C> columns and
1607 * <R> rows, the matrix is stored identically to an array of
1608 * <C> column vectors with <R> components each, according to
1611 * (6) If the member is an array of <S> column-major matrices with <C>
1612 * columns and <R> rows, the matrix is stored identically to a row of
1613 * <S>*<C> column vectors with <R> components each, according to rule
1616 * (7) If the member is a row-major matrix with <C> columns and <R>
1617 * rows, the matrix is stored identically to an array of <R>
1618 * row vectors with <C> components each, according to rule (4).
1620 * (8) If the member is an array of <S> row-major matrices with <C> columns
1621 * and <R> rows, the matrix is stored identically to a row of <S>*<R>
1622 * row vectors with <C> components each, according to rule (4).
1624 if (this->without_array()->is_matrix()) {
1625 const struct glsl_type
*element_type
;
1626 const struct glsl_type
*vec_type
;
1627 unsigned int array_len
;
1629 if (this->is_array()) {
1630 element_type
= this->without_array();
1631 array_len
= this->arrays_of_arrays_size();
1633 element_type
= this;
1638 vec_type
= get_instance(element_type
->base_type
,
1639 element_type
->matrix_columns
, 1);
1641 array_len
*= element_type
->vector_elements
;
1643 vec_type
= get_instance(element_type
->base_type
,
1644 element_type
->vector_elements
, 1);
1645 array_len
*= element_type
->matrix_columns
;
1647 const glsl_type
*array_type
= glsl_type::get_array_instance(vec_type
,
1650 return array_type
->std140_size(false);
1653 /* (4) If the member is an array of scalars or vectors, the base alignment
1654 * and array stride are set to match the base alignment of a single
1655 * array element, according to rules (1), (2), and (3), and rounded up
1656 * to the base alignment of a vec4. The array may have padding at the
1657 * end; the base offset of the member following the array is rounded up
1658 * to the next multiple of the base alignment.
1660 * (10) If the member is an array of <S> structures, the <S> elements of
1661 * the array are laid out in order, according to rule (9).
1663 if (this->is_array()) {
1664 if (this->without_array()->is_record()) {
1665 return this->arrays_of_arrays_size() *
1666 this->without_array()->std140_size(row_major
);
1668 unsigned element_base_align
=
1669 this->without_array()->std140_base_alignment(row_major
);
1670 return this->arrays_of_arrays_size() * MAX2(element_base_align
, 16);
1674 /* (9) If the member is a structure, the base alignment of the
1675 * structure is <N>, where <N> is the largest base alignment
1676 * value of any of its members, and rounded up to the base
1677 * alignment of a vec4. The individual members of this
1678 * sub-structure are then assigned offsets by applying this set
1679 * of rules recursively, where the base offset of the first
1680 * member of the sub-structure is equal to the aligned offset
1681 * of the structure. The structure may have padding at the end;
1682 * the base offset of the member following the sub-structure is
1683 * rounded up to the next multiple of the base alignment of the
1686 if (this->is_record() || this->is_interface()) {
1688 unsigned max_align
= 0;
1690 for (unsigned i
= 0; i
< this->length
; i
++) {
1691 bool field_row_major
= row_major
;
1692 const enum glsl_matrix_layout matrix_layout
=
1693 glsl_matrix_layout(this->fields
.structure
[i
].matrix_layout
);
1694 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
1695 field_row_major
= true;
1696 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
1697 field_row_major
= false;
1700 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
1701 unsigned align
= field_type
->std140_base_alignment(field_row_major
);
1703 /* Ignore unsized arrays when calculating size */
1704 if (field_type
->is_unsized_array())
1707 size
= glsl_align(size
, align
);
1708 size
+= field_type
->std140_size(field_row_major
);
1710 max_align
= MAX2(align
, max_align
);
1712 if (field_type
->is_record() && (i
+ 1 < this->length
))
1713 size
= glsl_align(size
, 16);
1715 size
= glsl_align(size
, MAX2(max_align
, 16));
1719 assert(!"not reached");
1724 glsl_type::std430_base_alignment(bool row_major
) const
1727 unsigned N
= is_64bit() ? 8 : 4;
1729 /* (1) If the member is a scalar consuming <N> basic machine units, the
1730 * base alignment is <N>.
1732 * (2) If the member is a two- or four-component vector with components
1733 * consuming <N> basic machine units, the base alignment is 2<N> or
1734 * 4<N>, respectively.
1736 * (3) If the member is a three-component vector with components consuming
1737 * <N> basic machine units, the base alignment is 4<N>.
1739 if (this->is_scalar() || this->is_vector()) {
1740 switch (this->vector_elements
) {
1751 /* OpenGL 4.30 spec, section 7.6.2.2 "Standard Uniform Block Layout":
1753 * "When using the std430 storage layout, shader storage blocks will be
1754 * laid out in buffer storage identically to uniform and shader storage
1755 * blocks using the std140 layout, except that the base alignment and
1756 * stride of arrays of scalars and vectors in rule 4 and of structures
1757 * in rule 9 are not rounded up a multiple of the base alignment of a vec4.
1760 /* (1) If the member is a scalar consuming <N> basic machine units, the
1761 * base alignment is <N>.
1763 * (2) If the member is a two- or four-component vector with components
1764 * consuming <N> basic machine units, the base alignment is 2<N> or
1765 * 4<N>, respectively.
1767 * (3) If the member is a three-component vector with components consuming
1768 * <N> basic machine units, the base alignment is 4<N>.
1770 if (this->is_array())
1771 return this->fields
.array
->std430_base_alignment(row_major
);
1773 /* (5) If the member is a column-major matrix with <C> columns and
1774 * <R> rows, the matrix is stored identically to an array of
1775 * <C> column vectors with <R> components each, according to
1778 * (7) If the member is a row-major matrix with <C> columns and <R>
1779 * rows, the matrix is stored identically to an array of <R>
1780 * row vectors with <C> components each, according to rule (4).
1782 if (this->is_matrix()) {
1783 const struct glsl_type
*vec_type
, *array_type
;
1784 int c
= this->matrix_columns
;
1785 int r
= this->vector_elements
;
1788 vec_type
= get_instance(base_type
, c
, 1);
1789 array_type
= glsl_type::get_array_instance(vec_type
, r
);
1791 vec_type
= get_instance(base_type
, r
, 1);
1792 array_type
= glsl_type::get_array_instance(vec_type
, c
);
1795 return array_type
->std430_base_alignment(false);
1798 /* (9) If the member is a structure, the base alignment of the
1799 * structure is <N>, where <N> is the largest base alignment
1800 * value of any of its members, and rounded up to the base
1801 * alignment of a vec4. The individual members of this
1802 * sub-structure are then assigned offsets by applying this set
1803 * of rules recursively, where the base offset of the first
1804 * member of the sub-structure is equal to the aligned offset
1805 * of the structure. The structure may have padding at the end;
1806 * the base offset of the member following the sub-structure is
1807 * rounded up to the next multiple of the base alignment of the
1810 if (this->is_record()) {
1811 unsigned base_alignment
= 0;
1812 for (unsigned i
= 0; i
< this->length
; i
++) {
1813 bool field_row_major
= row_major
;
1814 const enum glsl_matrix_layout matrix_layout
=
1815 glsl_matrix_layout(this->fields
.structure
[i
].matrix_layout
);
1816 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
1817 field_row_major
= true;
1818 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
1819 field_row_major
= false;
1822 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
1823 base_alignment
= MAX2(base_alignment
,
1824 field_type
->std430_base_alignment(field_row_major
));
1826 assert(base_alignment
> 0);
1827 return base_alignment
;
1829 assert(!"not reached");
1834 glsl_type::std430_array_stride(bool row_major
) const
1836 unsigned N
= is_64bit() ? 8 : 4;
1838 /* Notice that the array stride of a vec3 is not 3 * N but 4 * N.
1839 * See OpenGL 4.30 spec, section 7.6.2.2 "Standard Uniform Block Layout"
1841 * (3) If the member is a three-component vector with components consuming
1842 * <N> basic machine units, the base alignment is 4<N>.
1844 if (this->is_vector() && this->vector_elements
== 3)
1847 /* By default use std430_size(row_major) */
1848 return this->std430_size(row_major
);
1852 glsl_type::std430_size(bool row_major
) const
1854 unsigned N
= is_64bit() ? 8 : 4;
1856 /* OpenGL 4.30 spec, section 7.6.2.2 "Standard Uniform Block Layout":
1858 * "When using the std430 storage layout, shader storage blocks will be
1859 * laid out in buffer storage identically to uniform and shader storage
1860 * blocks using the std140 layout, except that the base alignment and
1861 * stride of arrays of scalars and vectors in rule 4 and of structures
1862 * in rule 9 are not rounded up a multiple of the base alignment of a vec4.
1864 if (this->is_scalar() || this->is_vector())
1865 return this->vector_elements
* N
;
1867 if (this->without_array()->is_matrix()) {
1868 const struct glsl_type
*element_type
;
1869 const struct glsl_type
*vec_type
;
1870 unsigned int array_len
;
1872 if (this->is_array()) {
1873 element_type
= this->without_array();
1874 array_len
= this->arrays_of_arrays_size();
1876 element_type
= this;
1881 vec_type
= get_instance(element_type
->base_type
,
1882 element_type
->matrix_columns
, 1);
1884 array_len
*= element_type
->vector_elements
;
1886 vec_type
= get_instance(element_type
->base_type
,
1887 element_type
->vector_elements
, 1);
1888 array_len
*= element_type
->matrix_columns
;
1890 const glsl_type
*array_type
= glsl_type::get_array_instance(vec_type
,
1893 return array_type
->std430_size(false);
1896 if (this->is_array()) {
1897 if (this->without_array()->is_record())
1898 return this->arrays_of_arrays_size() *
1899 this->without_array()->std430_size(row_major
);
1901 return this->arrays_of_arrays_size() *
1902 this->without_array()->std430_base_alignment(row_major
);
1905 if (this->is_record() || this->is_interface()) {
1907 unsigned max_align
= 0;
1909 for (unsigned i
= 0; i
< this->length
; i
++) {
1910 bool field_row_major
= row_major
;
1911 const enum glsl_matrix_layout matrix_layout
=
1912 glsl_matrix_layout(this->fields
.structure
[i
].matrix_layout
);
1913 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
1914 field_row_major
= true;
1915 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
1916 field_row_major
= false;
1919 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
1920 unsigned align
= field_type
->std430_base_alignment(field_row_major
);
1921 size
= glsl_align(size
, align
);
1922 size
+= field_type
->std430_size(field_row_major
);
1924 max_align
= MAX2(align
, max_align
);
1926 size
= glsl_align(size
, max_align
);
1930 assert(!"not reached");
1935 glsl_type::count_attribute_slots(bool is_vertex_input
) const
1937 /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
1939 * "A scalar input counts the same amount against this limit as a vec4,
1940 * so applications may want to consider packing groups of four
1941 * unrelated float inputs together into a vector to better utilize the
1942 * capabilities of the underlying hardware. A matrix input will use up
1943 * multiple locations. The number of locations used will equal the
1944 * number of columns in the matrix."
1946 * The spec does not explicitly say how arrays are counted. However, it
1947 * should be safe to assume the total number of slots consumed by an array
1948 * is the number of entries in the array multiplied by the number of slots
1949 * consumed by a single element of the array.
1951 * The spec says nothing about how structs are counted, because vertex
1952 * attributes are not allowed to be (or contain) structs. However, Mesa
1953 * allows varying structs, the number of varying slots taken up by a
1954 * varying struct is simply equal to the sum of the number of slots taken
1955 * up by each element.
1957 * Doubles are counted different depending on whether they are vertex
1958 * inputs or everything else. Vertex inputs from ARB_vertex_attrib_64bit
1959 * take one location no matter what size they are, otherwise dvec3/4
1960 * take two locations.
1962 switch (this->base_type
) {
1963 case GLSL_TYPE_UINT
:
1965 case GLSL_TYPE_FLOAT
:
1966 case GLSL_TYPE_BOOL
:
1967 return this->matrix_columns
;
1968 case GLSL_TYPE_DOUBLE
:
1969 case GLSL_TYPE_UINT64
:
1970 case GLSL_TYPE_INT64
:
1971 if (this->vector_elements
> 2 && !is_vertex_input
)
1972 return this->matrix_columns
* 2;
1974 return this->matrix_columns
;
1975 case GLSL_TYPE_STRUCT
:
1976 case GLSL_TYPE_INTERFACE
: {
1979 for (unsigned i
= 0; i
< this->length
; i
++)
1980 size
+= this->fields
.structure
[i
].type
->count_attribute_slots(is_vertex_input
);
1985 case GLSL_TYPE_ARRAY
:
1986 return this->length
* this->fields
.array
->count_attribute_slots(is_vertex_input
);
1988 case GLSL_TYPE_FUNCTION
:
1989 case GLSL_TYPE_SAMPLER
:
1990 case GLSL_TYPE_IMAGE
:
1991 case GLSL_TYPE_ATOMIC_UINT
:
1992 case GLSL_TYPE_VOID
:
1993 case GLSL_TYPE_SUBROUTINE
:
1994 case GLSL_TYPE_ERROR
:
1998 assert(!"Unexpected type in count_attribute_slots()");
2004 glsl_type::coordinate_components() const
2008 switch (sampler_dimensionality
) {
2009 case GLSL_SAMPLER_DIM_1D
:
2010 case GLSL_SAMPLER_DIM_BUF
:
2013 case GLSL_SAMPLER_DIM_2D
:
2014 case GLSL_SAMPLER_DIM_RECT
:
2015 case GLSL_SAMPLER_DIM_MS
:
2016 case GLSL_SAMPLER_DIM_EXTERNAL
:
2017 case GLSL_SAMPLER_DIM_SUBPASS
:
2020 case GLSL_SAMPLER_DIM_3D
:
2021 case GLSL_SAMPLER_DIM_CUBE
:
2025 assert(!"Should not get here.");
2030 /* Array textures need an additional component for the array index, except
2031 * for cubemap array images that behave like a 2D array of interleaved
2034 if (sampler_array
&&
2035 !(is_image() && sampler_dimensionality
== GLSL_SAMPLER_DIM_CUBE
))
2042 * Declarations of type flyweights (glsl_type::_foo_type) and
2043 * convenience pointers (glsl_type::foo_type).
2046 #define DECL_TYPE(NAME, ...) \
2047 const glsl_type glsl_type::_##NAME##_type = glsl_type(__VA_ARGS__, #NAME); \
2048 const glsl_type *const glsl_type::NAME##_type = &glsl_type::_##NAME##_type;
2050 #define STRUCT_TYPE(NAME)
2052 #include "compiler/builtin_type_macros.h"