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
));
98 if (base_type
== GLSL_TYPE_SAMPLER
) {
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
:
363 const glsl_type
*glsl_type::get_scalar_type() const
365 const glsl_type
*type
= this;
368 while (type
->base_type
== GLSL_TYPE_ARRAY
)
369 type
= type
->fields
.array
;
371 /* Handle vectors and matrices */
372 switch (type
->base_type
) {
377 case GLSL_TYPE_FLOAT
:
379 case GLSL_TYPE_DOUBLE
:
384 /* Handle everything else */
391 _mesa_glsl_release_types(void)
393 /* Should only be called during atexit (either when unloading shared
394 * object, or if process terminates), so no mutex-locking should be
397 if (glsl_type::array_types
!= NULL
) {
398 _mesa_hash_table_destroy(glsl_type::array_types
, NULL
);
399 glsl_type::array_types
= NULL
;
402 if (glsl_type::record_types
!= NULL
) {
403 _mesa_hash_table_destroy(glsl_type::record_types
, NULL
);
404 glsl_type::record_types
= NULL
;
407 if (glsl_type::interface_types
!= NULL
) {
408 _mesa_hash_table_destroy(glsl_type::interface_types
, NULL
);
409 glsl_type::interface_types
= NULL
;
414 glsl_type::glsl_type(const glsl_type
*array
, unsigned length
) :
415 base_type(GLSL_TYPE_ARRAY
),
416 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
417 sampled_type(0), interface_packing(0), interface_row_major(0),
418 vector_elements(0), matrix_columns(0),
419 length(length
), name(NULL
)
421 this->fields
.array
= array
;
422 /* Inherit the gl type of the base. The GL type is used for
423 * uniform/statevar handling in Mesa and the arrayness of the type
424 * is represented by the size rather than the type.
426 this->gl_type
= array
->gl_type
;
428 /* Allow a maximum of 10 characters for the array size. This is enough
429 * for 32-bits of ~0. The extra 3 are for the '[', ']', and terminating
432 const unsigned name_length
= strlen(array
->name
) + 10 + 3;
434 mtx_lock(&glsl_type::mutex
);
435 char *const n
= (char *) ralloc_size(this->mem_ctx
, name_length
);
436 mtx_unlock(&glsl_type::mutex
);
439 snprintf(n
, name_length
, "%s[]", array
->name
);
441 /* insert outermost dimensions in the correct spot
442 * otherwise the dimension order will be backwards
444 const char *pos
= strchr(array
->name
, '[');
446 int idx
= pos
- array
->name
;
447 snprintf(n
, idx
+1, "%s", array
->name
);
448 snprintf(n
+ idx
, name_length
- idx
, "[%u]%s",
449 length
, array
->name
+ idx
);
451 snprintf(n
, name_length
, "%s[%u]", array
->name
, length
);
460 glsl_type::vec(unsigned components
)
462 if (components
== 0 || components
> 4)
465 static const glsl_type
*const ts
[] = {
466 float_type
, vec2_type
, vec3_type
, vec4_type
468 return ts
[components
- 1];
472 glsl_type::dvec(unsigned components
)
474 if (components
== 0 || components
> 4)
477 static const glsl_type
*const ts
[] = {
478 double_type
, dvec2_type
, dvec3_type
, dvec4_type
480 return ts
[components
- 1];
484 glsl_type::ivec(unsigned components
)
486 if (components
== 0 || components
> 4)
489 static const glsl_type
*const ts
[] = {
490 int_type
, ivec2_type
, ivec3_type
, ivec4_type
492 return ts
[components
- 1];
497 glsl_type::uvec(unsigned components
)
499 if (components
== 0 || components
> 4)
502 static const glsl_type
*const ts
[] = {
503 uint_type
, uvec2_type
, uvec3_type
, uvec4_type
505 return ts
[components
- 1];
510 glsl_type::bvec(unsigned components
)
512 if (components
== 0 || components
> 4)
515 static const glsl_type
*const ts
[] = {
516 bool_type
, bvec2_type
, bvec3_type
, bvec4_type
518 return ts
[components
- 1];
523 glsl_type::get_instance(unsigned base_type
, unsigned rows
, unsigned columns
)
525 if (base_type
== GLSL_TYPE_VOID
)
528 if ((rows
< 1) || (rows
> 4) || (columns
< 1) || (columns
> 4))
531 /* Treat GLSL vectors as Nx1 matrices.
539 case GLSL_TYPE_FLOAT
:
541 case GLSL_TYPE_DOUBLE
:
549 if ((base_type
!= GLSL_TYPE_FLOAT
&& base_type
!= GLSL_TYPE_DOUBLE
) || (rows
== 1))
552 /* GLSL matrix types are named mat{COLUMNS}x{ROWS}. Only the following
553 * combinations are valid:
561 #define IDX(c,r) (((c-1)*3) + (r-1))
563 if (base_type
== GLSL_TYPE_DOUBLE
) {
564 switch (IDX(columns
, rows
)) {
565 case IDX(2,2): return dmat2_type
;
566 case IDX(2,3): return dmat2x3_type
;
567 case IDX(2,4): return dmat2x4_type
;
568 case IDX(3,2): return dmat3x2_type
;
569 case IDX(3,3): return dmat3_type
;
570 case IDX(3,4): return dmat3x4_type
;
571 case IDX(4,2): return dmat4x2_type
;
572 case IDX(4,3): return dmat4x3_type
;
573 case IDX(4,4): return dmat4_type
;
574 default: return error_type
;
577 switch (IDX(columns
, rows
)) {
578 case IDX(2,2): return mat2_type
;
579 case IDX(2,3): return mat2x3_type
;
580 case IDX(2,4): return mat2x4_type
;
581 case IDX(3,2): return mat3x2_type
;
582 case IDX(3,3): return mat3_type
;
583 case IDX(3,4): return mat3x4_type
;
584 case IDX(4,2): return mat4x2_type
;
585 case IDX(4,3): return mat4x3_type
;
586 case IDX(4,4): return mat4_type
;
587 default: return error_type
;
592 assert(!"Should not get here.");
597 glsl_type::get_sampler_instance(enum glsl_sampler_dim dim
,
603 case GLSL_TYPE_FLOAT
:
605 case GLSL_SAMPLER_DIM_1D
:
607 return (array
? sampler1DArrayShadow_type
: sampler1DShadow_type
);
609 return (array
? sampler1DArray_type
: sampler1D_type
);
610 case GLSL_SAMPLER_DIM_2D
:
612 return (array
? sampler2DArrayShadow_type
: sampler2DShadow_type
);
614 return (array
? sampler2DArray_type
: sampler2D_type
);
615 case GLSL_SAMPLER_DIM_3D
:
619 return sampler3D_type
;
620 case GLSL_SAMPLER_DIM_CUBE
:
622 return (array
? samplerCubeArrayShadow_type
: samplerCubeShadow_type
);
624 return (array
? samplerCubeArray_type
: samplerCube_type
);
625 case GLSL_SAMPLER_DIM_RECT
:
629 return sampler2DRectShadow_type
;
631 return sampler2DRect_type
;
632 case GLSL_SAMPLER_DIM_BUF
:
636 return samplerBuffer_type
;
637 case GLSL_SAMPLER_DIM_MS
:
640 return (array
? sampler2DMSArray_type
: sampler2DMS_type
);
641 case GLSL_SAMPLER_DIM_EXTERNAL
:
645 return samplerExternalOES_type
;
646 case GLSL_SAMPLER_DIM_SUBPASS
:
653 case GLSL_SAMPLER_DIM_1D
:
654 return (array
? isampler1DArray_type
: isampler1D_type
);
655 case GLSL_SAMPLER_DIM_2D
:
656 return (array
? isampler2DArray_type
: isampler2D_type
);
657 case GLSL_SAMPLER_DIM_3D
:
660 return isampler3D_type
;
661 case GLSL_SAMPLER_DIM_CUBE
:
662 return (array
? isamplerCubeArray_type
: isamplerCube_type
);
663 case GLSL_SAMPLER_DIM_RECT
:
666 return isampler2DRect_type
;
667 case GLSL_SAMPLER_DIM_BUF
:
670 return isamplerBuffer_type
;
671 case GLSL_SAMPLER_DIM_MS
:
672 return (array
? isampler2DMSArray_type
: isampler2DMS_type
);
673 case GLSL_SAMPLER_DIM_EXTERNAL
:
675 case GLSL_SAMPLER_DIM_SUBPASS
:
682 case GLSL_SAMPLER_DIM_1D
:
683 return (array
? usampler1DArray_type
: usampler1D_type
);
684 case GLSL_SAMPLER_DIM_2D
:
685 return (array
? usampler2DArray_type
: usampler2D_type
);
686 case GLSL_SAMPLER_DIM_3D
:
689 return usampler3D_type
;
690 case GLSL_SAMPLER_DIM_CUBE
:
691 return (array
? usamplerCubeArray_type
: usamplerCube_type
);
692 case GLSL_SAMPLER_DIM_RECT
:
695 return usampler2DRect_type
;
696 case GLSL_SAMPLER_DIM_BUF
:
699 return usamplerBuffer_type
;
700 case GLSL_SAMPLER_DIM_MS
:
701 return (array
? usampler2DMSArray_type
: usampler2DMS_type
);
702 case GLSL_SAMPLER_DIM_EXTERNAL
:
704 case GLSL_SAMPLER_DIM_SUBPASS
:
711 unreachable("switch statement above should be complete");
715 glsl_type::get_image_instance(enum glsl_sampler_dim dim
,
716 bool array
, glsl_base_type type
)
718 if (dim
== GLSL_SAMPLER_DIM_SUBPASS
)
719 return subpassInput_type
;
721 case GLSL_TYPE_FLOAT
:
723 case GLSL_SAMPLER_DIM_1D
:
724 return (array
? image1DArray_type
: image1D_type
);
725 case GLSL_SAMPLER_DIM_2D
:
726 return (array
? image2DArray_type
: image2D_type
);
727 case GLSL_SAMPLER_DIM_3D
:
729 case GLSL_SAMPLER_DIM_CUBE
:
730 return (array
? imageCubeArray_type
: imageCube_type
);
731 case GLSL_SAMPLER_DIM_RECT
:
735 return image2DRect_type
;
736 case GLSL_SAMPLER_DIM_BUF
:
740 return imageBuffer_type
;
741 case GLSL_SAMPLER_DIM_MS
:
742 return (array
? image2DMSArray_type
: image2DMS_type
);
743 case GLSL_SAMPLER_DIM_EXTERNAL
:
744 case GLSL_SAMPLER_DIM_SUBPASS
:
749 case GLSL_SAMPLER_DIM_1D
:
750 return (array
? iimage1DArray_type
: iimage1D_type
);
751 case GLSL_SAMPLER_DIM_2D
:
752 return (array
? iimage2DArray_type
: iimage2D_type
);
753 case GLSL_SAMPLER_DIM_3D
:
756 return iimage3D_type
;
757 case GLSL_SAMPLER_DIM_CUBE
:
758 return (array
? iimageCubeArray_type
: iimageCube_type
);
759 case GLSL_SAMPLER_DIM_RECT
:
762 return iimage2DRect_type
;
763 case GLSL_SAMPLER_DIM_BUF
:
766 return iimageBuffer_type
;
767 case GLSL_SAMPLER_DIM_MS
:
768 return (array
? iimage2DMSArray_type
: iimage2DMS_type
);
769 case GLSL_SAMPLER_DIM_EXTERNAL
:
770 case GLSL_SAMPLER_DIM_SUBPASS
:
775 case GLSL_SAMPLER_DIM_1D
:
776 return (array
? uimage1DArray_type
: uimage1D_type
);
777 case GLSL_SAMPLER_DIM_2D
:
778 return (array
? uimage2DArray_type
: uimage2D_type
);
779 case GLSL_SAMPLER_DIM_3D
:
782 return uimage3D_type
;
783 case GLSL_SAMPLER_DIM_CUBE
:
784 return (array
? uimageCubeArray_type
: uimageCube_type
);
785 case GLSL_SAMPLER_DIM_RECT
:
788 return uimage2DRect_type
;
789 case GLSL_SAMPLER_DIM_BUF
:
792 return uimageBuffer_type
;
793 case GLSL_SAMPLER_DIM_MS
:
794 return (array
? uimage2DMSArray_type
: uimage2DMS_type
);
795 case GLSL_SAMPLER_DIM_EXTERNAL
:
796 case GLSL_SAMPLER_DIM_SUBPASS
:
803 unreachable("switch statement above should be complete");
807 glsl_type::get_array_instance(const glsl_type
*base
, unsigned array_size
)
809 /* Generate a name using the base type pointer in the key. This is
810 * done because the name of the base type may not be unique across
811 * shaders. For example, two shaders may have different record types
815 snprintf(key
, sizeof(key
), "%p[%u]", (void *) base
, array_size
);
817 mtx_lock(&glsl_type::mutex
);
819 if (array_types
== NULL
) {
820 array_types
= _mesa_hash_table_create(NULL
, _mesa_key_hash_string
,
821 _mesa_key_string_equal
);
824 const struct hash_entry
*entry
= _mesa_hash_table_search(array_types
, key
);
826 mtx_unlock(&glsl_type::mutex
);
827 const glsl_type
*t
= new glsl_type(base
, array_size
);
828 mtx_lock(&glsl_type::mutex
);
830 entry
= _mesa_hash_table_insert(array_types
,
831 ralloc_strdup(mem_ctx
, key
),
835 assert(((glsl_type
*) entry
->data
)->base_type
== GLSL_TYPE_ARRAY
);
836 assert(((glsl_type
*) entry
->data
)->length
== array_size
);
837 assert(((glsl_type
*) entry
->data
)->fields
.array
== base
);
839 mtx_unlock(&glsl_type::mutex
);
841 return (glsl_type
*) entry
->data
;
846 glsl_type::record_compare(const glsl_type
*b
, bool match_locations
) const
848 if (this->length
!= b
->length
)
851 if (this->interface_packing
!= b
->interface_packing
)
854 if (this->interface_row_major
!= b
->interface_row_major
)
857 /* From the GLSL 4.20 specification (Sec 4.2):
859 * "Structures must have the same name, sequence of type names, and
860 * type definitions, and field names to be considered the same type."
862 * GLSL ES behaves the same (Ver 1.00 Sec 4.2.4, Ver 3.00 Sec 4.2.5).
864 * Note that we cannot force type name check when comparing unnamed
865 * structure types, these have a unique name assigned during parsing.
867 if (!this->is_anonymous() && !b
->is_anonymous())
868 if (strcmp(this->name
, b
->name
) != 0)
871 for (unsigned i
= 0; i
< this->length
; i
++) {
872 if (this->fields
.structure
[i
].type
!= b
->fields
.structure
[i
].type
)
874 if (strcmp(this->fields
.structure
[i
].name
,
875 b
->fields
.structure
[i
].name
) != 0)
877 if (this->fields
.structure
[i
].matrix_layout
878 != b
->fields
.structure
[i
].matrix_layout
)
880 if (match_locations
&& this->fields
.structure
[i
].location
881 != b
->fields
.structure
[i
].location
)
883 if (this->fields
.structure
[i
].offset
884 != b
->fields
.structure
[i
].offset
)
886 if (this->fields
.structure
[i
].interpolation
887 != b
->fields
.structure
[i
].interpolation
)
889 if (this->fields
.structure
[i
].centroid
890 != b
->fields
.structure
[i
].centroid
)
892 if (this->fields
.structure
[i
].sample
893 != b
->fields
.structure
[i
].sample
)
895 if (this->fields
.structure
[i
].patch
896 != b
->fields
.structure
[i
].patch
)
898 if (this->fields
.structure
[i
].image_read_only
899 != b
->fields
.structure
[i
].image_read_only
)
901 if (this->fields
.structure
[i
].image_write_only
902 != b
->fields
.structure
[i
].image_write_only
)
904 if (this->fields
.structure
[i
].image_coherent
905 != b
->fields
.structure
[i
].image_coherent
)
907 if (this->fields
.structure
[i
].image_volatile
908 != b
->fields
.structure
[i
].image_volatile
)
910 if (this->fields
.structure
[i
].image_restrict
911 != b
->fields
.structure
[i
].image_restrict
)
913 if (this->fields
.structure
[i
].precision
914 != b
->fields
.structure
[i
].precision
)
916 if (this->fields
.structure
[i
].explicit_xfb_buffer
917 != b
->fields
.structure
[i
].explicit_xfb_buffer
)
919 if (this->fields
.structure
[i
].xfb_buffer
920 != b
->fields
.structure
[i
].xfb_buffer
)
922 if (this->fields
.structure
[i
].xfb_stride
923 != b
->fields
.structure
[i
].xfb_stride
)
932 glsl_type::record_key_compare(const void *a
, const void *b
)
934 const glsl_type
*const key1
= (glsl_type
*) a
;
935 const glsl_type
*const key2
= (glsl_type
*) b
;
937 return strcmp(key1
->name
, key2
->name
) == 0 && key1
->record_compare(key2
);
942 * Generate an integer hash value for a glsl_type structure type.
945 glsl_type::record_key_hash(const void *a
)
947 const glsl_type
*const key
= (glsl_type
*) a
;
948 uintptr_t hash
= key
->length
;
951 for (unsigned i
= 0; i
< key
->length
; i
++) {
952 /* casting pointer to uintptr_t */
953 hash
= (hash
* 13 ) + (uintptr_t) key
->fields
.structure
[i
].type
;
956 if (sizeof(hash
) == 8)
957 retval
= (hash
& 0xffffffff) ^ ((uint64_t) hash
>> 32);
966 glsl_type::get_record_instance(const glsl_struct_field
*fields
,
970 const glsl_type
key(fields
, num_fields
, name
);
972 mtx_lock(&glsl_type::mutex
);
974 if (record_types
== NULL
) {
975 record_types
= _mesa_hash_table_create(NULL
, record_key_hash
,
979 const struct hash_entry
*entry
= _mesa_hash_table_search(record_types
,
982 mtx_unlock(&glsl_type::mutex
);
983 const glsl_type
*t
= new glsl_type(fields
, num_fields
, name
);
984 mtx_lock(&glsl_type::mutex
);
986 entry
= _mesa_hash_table_insert(record_types
, t
, (void *) t
);
989 assert(((glsl_type
*) entry
->data
)->base_type
== GLSL_TYPE_STRUCT
);
990 assert(((glsl_type
*) entry
->data
)->length
== num_fields
);
991 assert(strcmp(((glsl_type
*) entry
->data
)->name
, name
) == 0);
993 mtx_unlock(&glsl_type::mutex
);
995 return (glsl_type
*) entry
->data
;
1000 glsl_type::get_interface_instance(const glsl_struct_field
*fields
,
1001 unsigned num_fields
,
1002 enum glsl_interface_packing packing
,
1004 const char *block_name
)
1006 const glsl_type
key(fields
, num_fields
, packing
, row_major
, block_name
);
1008 mtx_lock(&glsl_type::mutex
);
1010 if (interface_types
== NULL
) {
1011 interface_types
= _mesa_hash_table_create(NULL
, record_key_hash
,
1012 record_key_compare
);
1015 const struct hash_entry
*entry
= _mesa_hash_table_search(interface_types
,
1017 if (entry
== NULL
) {
1018 mtx_unlock(&glsl_type::mutex
);
1019 const glsl_type
*t
= new glsl_type(fields
, num_fields
,
1020 packing
, row_major
, block_name
);
1021 mtx_lock(&glsl_type::mutex
);
1023 entry
= _mesa_hash_table_insert(interface_types
, t
, (void *) t
);
1026 assert(((glsl_type
*) entry
->data
)->base_type
== GLSL_TYPE_INTERFACE
);
1027 assert(((glsl_type
*) entry
->data
)->length
== num_fields
);
1028 assert(strcmp(((glsl_type
*) entry
->data
)->name
, block_name
) == 0);
1030 mtx_unlock(&glsl_type::mutex
);
1032 return (glsl_type
*) entry
->data
;
1036 glsl_type::get_subroutine_instance(const char *subroutine_name
)
1038 const glsl_type
key(subroutine_name
);
1040 mtx_lock(&glsl_type::mutex
);
1042 if (subroutine_types
== NULL
) {
1043 subroutine_types
= _mesa_hash_table_create(NULL
, record_key_hash
,
1044 record_key_compare
);
1047 const struct hash_entry
*entry
= _mesa_hash_table_search(subroutine_types
,
1049 if (entry
== NULL
) {
1050 mtx_unlock(&glsl_type::mutex
);
1051 const glsl_type
*t
= new glsl_type(subroutine_name
);
1052 mtx_lock(&glsl_type::mutex
);
1054 entry
= _mesa_hash_table_insert(subroutine_types
, t
, (void *) t
);
1057 assert(((glsl_type
*) entry
->data
)->base_type
== GLSL_TYPE_SUBROUTINE
);
1058 assert(strcmp(((glsl_type
*) entry
->data
)->name
, subroutine_name
) == 0);
1060 mtx_unlock(&glsl_type::mutex
);
1062 return (glsl_type
*) entry
->data
;
1067 function_key_compare(const void *a
, const void *b
)
1069 const glsl_type
*const key1
= (glsl_type
*) a
;
1070 const glsl_type
*const key2
= (glsl_type
*) b
;
1072 if (key1
->length
!= key2
->length
)
1075 return memcmp(key1
->fields
.parameters
, key2
->fields
.parameters
,
1076 (key1
->length
+ 1) * sizeof(*key1
->fields
.parameters
)) == 0;
1081 function_key_hash(const void *a
)
1083 const glsl_type
*const key
= (glsl_type
*) a
;
1084 return _mesa_hash_data(key
->fields
.parameters
,
1085 (key
->length
+ 1) * sizeof(*key
->fields
.parameters
));
1089 glsl_type::get_function_instance(const glsl_type
*return_type
,
1090 const glsl_function_param
*params
,
1091 unsigned num_params
)
1093 const glsl_type
key(return_type
, params
, num_params
);
1095 mtx_lock(&glsl_type::mutex
);
1097 if (function_types
== NULL
) {
1098 function_types
= _mesa_hash_table_create(NULL
, function_key_hash
,
1099 function_key_compare
);
1102 struct hash_entry
*entry
= _mesa_hash_table_search(function_types
, &key
);
1103 if (entry
== NULL
) {
1104 mtx_unlock(&glsl_type::mutex
);
1105 const glsl_type
*t
= new glsl_type(return_type
, params
, num_params
);
1106 mtx_lock(&glsl_type::mutex
);
1108 entry
= _mesa_hash_table_insert(function_types
, t
, (void *) t
);
1111 const glsl_type
*t
= (const glsl_type
*)entry
->data
;
1113 assert(t
->base_type
== GLSL_TYPE_FUNCTION
);
1114 assert(t
->length
== num_params
);
1116 mtx_unlock(&glsl_type::mutex
);
1123 glsl_type::get_mul_type(const glsl_type
*type_a
, const glsl_type
*type_b
)
1125 if (type_a
== type_b
) {
1127 } else if (type_a
->is_matrix() && type_b
->is_matrix()) {
1128 /* Matrix multiply. The columns of A must match the rows of B. Given
1129 * the other previously tested constraints, this means the vector type
1130 * of a row from A must be the same as the vector type of a column from
1133 if (type_a
->row_type() == type_b
->column_type()) {
1134 /* The resulting matrix has the number of columns of matrix B and
1135 * the number of rows of matrix A. We get the row count of A by
1136 * looking at the size of a vector that makes up a column. The
1137 * transpose (size of a row) is done for B.
1139 const glsl_type
*const type
=
1140 get_instance(type_a
->base_type
,
1141 type_a
->column_type()->vector_elements
,
1142 type_b
->row_type()->vector_elements
);
1143 assert(type
!= error_type
);
1147 } else if (type_a
->is_matrix()) {
1148 /* A is a matrix and B is a column vector. Columns of A must match
1149 * rows of B. Given the other previously tested constraints, this
1150 * means the vector type of a row from A must be the same as the
1151 * vector the type of B.
1153 if (type_a
->row_type() == type_b
) {
1154 /* The resulting vector has a number of elements equal to
1155 * the number of rows of matrix A. */
1156 const glsl_type
*const type
=
1157 get_instance(type_a
->base_type
,
1158 type_a
->column_type()->vector_elements
,
1160 assert(type
!= error_type
);
1165 assert(type_b
->is_matrix());
1167 /* A is a row vector and B is a matrix. Columns of A must match rows
1168 * of B. Given the other previously tested constraints, this means
1169 * the type of A must be the same as the vector type of a column from
1172 if (type_a
== type_b
->column_type()) {
1173 /* The resulting vector has a number of elements equal to
1174 * the number of columns of matrix B. */
1175 const glsl_type
*const type
=
1176 get_instance(type_a
->base_type
,
1177 type_b
->row_type()->vector_elements
,
1179 assert(type
!= error_type
);
1190 glsl_type::field_type(const char *name
) const
1192 if (this->base_type
!= GLSL_TYPE_STRUCT
1193 && this->base_type
!= GLSL_TYPE_INTERFACE
)
1196 for (unsigned i
= 0; i
< this->length
; i
++) {
1197 if (strcmp(name
, this->fields
.structure
[i
].name
) == 0)
1198 return this->fields
.structure
[i
].type
;
1206 glsl_type::field_index(const char *name
) const
1208 if (this->base_type
!= GLSL_TYPE_STRUCT
1209 && this->base_type
!= GLSL_TYPE_INTERFACE
)
1212 for (unsigned i
= 0; i
< this->length
; i
++) {
1213 if (strcmp(name
, this->fields
.structure
[i
].name
) == 0)
1222 glsl_type::component_slots() const
1224 switch (this->base_type
) {
1225 case GLSL_TYPE_UINT
:
1227 case GLSL_TYPE_FLOAT
:
1228 case GLSL_TYPE_BOOL
:
1229 return this->components();
1231 case GLSL_TYPE_DOUBLE
:
1232 return 2 * this->components();
1234 case GLSL_TYPE_STRUCT
:
1235 case GLSL_TYPE_INTERFACE
: {
1238 for (unsigned i
= 0; i
< this->length
; i
++)
1239 size
+= this->fields
.structure
[i
].type
->component_slots();
1244 case GLSL_TYPE_ARRAY
:
1245 return this->length
* this->fields
.array
->component_slots();
1247 case GLSL_TYPE_IMAGE
:
1249 case GLSL_TYPE_SUBROUTINE
:
1252 case GLSL_TYPE_FUNCTION
:
1253 case GLSL_TYPE_SAMPLER
:
1254 case GLSL_TYPE_ATOMIC_UINT
:
1255 case GLSL_TYPE_VOID
:
1256 case GLSL_TYPE_ERROR
:
1264 glsl_type::record_location_offset(unsigned length
) const
1266 unsigned offset
= 0;
1267 const glsl_type
*t
= this->without_array();
1268 if (t
->is_record()) {
1269 assert(length
<= t
->length
);
1271 for (unsigned i
= 0; i
< length
; i
++) {
1272 const glsl_type
*st
= t
->fields
.structure
[i
].type
;
1273 const glsl_type
*wa
= st
->without_array();
1274 if (wa
->is_record()) {
1275 unsigned r_offset
= wa
->record_location_offset(wa
->length
);
1276 offset
+= st
->is_array() ?
1277 st
->arrays_of_arrays_size() * r_offset
: r_offset
;
1278 } else if (st
->is_array() && st
->fields
.array
->is_array()) {
1279 unsigned outer_array_size
= st
->length
;
1280 const glsl_type
*base_type
= st
->fields
.array
;
1282 /* For arrays of arrays the outer arrays take up a uniform
1283 * slot for each element. The innermost array elements share a
1284 * single slot so we ignore the innermost array when calculating
1287 while (base_type
->fields
.array
->is_array()) {
1288 outer_array_size
= outer_array_size
* base_type
->length
;
1289 base_type
= base_type
->fields
.array
;
1291 offset
+= outer_array_size
;
1293 /* We dont worry about arrays here because unless the array
1294 * contains a structure or another array it only takes up a single
1305 glsl_type::uniform_locations() const
1309 switch (this->base_type
) {
1310 case GLSL_TYPE_UINT
:
1312 case GLSL_TYPE_FLOAT
:
1313 case GLSL_TYPE_DOUBLE
:
1314 case GLSL_TYPE_BOOL
:
1315 case GLSL_TYPE_SAMPLER
:
1316 case GLSL_TYPE_IMAGE
:
1317 case GLSL_TYPE_SUBROUTINE
:
1320 case GLSL_TYPE_STRUCT
:
1321 case GLSL_TYPE_INTERFACE
:
1322 for (unsigned i
= 0; i
< this->length
; i
++)
1323 size
+= this->fields
.structure
[i
].type
->uniform_locations();
1325 case GLSL_TYPE_ARRAY
:
1326 return this->length
* this->fields
.array
->uniform_locations();
1333 glsl_type::varying_count() const
1337 switch (this->base_type
) {
1338 case GLSL_TYPE_UINT
:
1340 case GLSL_TYPE_FLOAT
:
1341 case GLSL_TYPE_DOUBLE
:
1342 case GLSL_TYPE_BOOL
:
1345 case GLSL_TYPE_STRUCT
:
1346 case GLSL_TYPE_INTERFACE
:
1347 for (unsigned i
= 0; i
< this->length
; i
++)
1348 size
+= this->fields
.structure
[i
].type
->varying_count();
1350 case GLSL_TYPE_ARRAY
:
1351 /* Don't count innermost array elements */
1352 if (this->without_array()->is_record() ||
1353 this->without_array()->is_interface() ||
1354 this->fields
.array
->is_array())
1355 return this->length
* this->fields
.array
->varying_count();
1357 return this->fields
.array
->varying_count();
1359 assert(!"unsupported varying type");
1365 glsl_type::can_implicitly_convert_to(const glsl_type
*desired
,
1366 _mesa_glsl_parse_state
*state
) const
1368 if (this == desired
)
1371 /* GLSL 1.10 and ESSL do not allow implicit conversions. If there is no
1372 * state, we're doing intra-stage function linking where these checks have
1373 * already been done.
1375 if (state
&& (state
->es_shader
|| !state
->is_version(120, 0)))
1378 /* There is no conversion among matrix types. */
1379 if (this->matrix_columns
> 1 || desired
->matrix_columns
> 1)
1382 /* Vector size must match. */
1383 if (this->vector_elements
!= desired
->vector_elements
)
1386 /* int and uint can be converted to float. */
1387 if (desired
->is_float() && this->is_integer())
1390 /* With GLSL 4.0, ARB_gpu_shader5, or MESA_shader_integer_functions, int
1391 * can be converted to uint. Note that state may be NULL here, when
1392 * resolving function calls in the linker. By this time, all the
1393 * state-dependent checks have already happened though, so allow anything
1394 * that's allowed in any shader version.
1396 if ((!state
|| state
->is_version(400, 0) || state
->ARB_gpu_shader5_enable
||
1397 state
->MESA_shader_integer_functions_enable
) &&
1398 desired
->base_type
== GLSL_TYPE_UINT
&& this->base_type
== GLSL_TYPE_INT
)
1401 /* No implicit conversions from double. */
1402 if ((!state
|| state
->has_double()) && this->is_double())
1405 /* Conversions from different types to double. */
1406 if ((!state
|| state
->has_double()) && desired
->is_double()) {
1407 if (this->is_float())
1409 if (this->is_integer())
1417 glsl_type::std140_base_alignment(bool row_major
) const
1419 unsigned N
= is_64bit() ? 8 : 4;
1421 /* (1) If the member is a scalar consuming <N> basic machine units, the
1422 * base alignment is <N>.
1424 * (2) If the member is a two- or four-component vector with components
1425 * consuming <N> basic machine units, the base alignment is 2<N> or
1426 * 4<N>, respectively.
1428 * (3) If the member is a three-component vector with components consuming
1429 * <N> basic machine units, the base alignment is 4<N>.
1431 if (this->is_scalar() || this->is_vector()) {
1432 switch (this->vector_elements
) {
1443 /* (4) If the member is an array of scalars or vectors, the base alignment
1444 * and array stride are set to match the base alignment of a single
1445 * array element, according to rules (1), (2), and (3), and rounded up
1446 * to the base alignment of a vec4. The array may have padding at the
1447 * end; the base offset of the member following the array is rounded up
1448 * to the next multiple of the base alignment.
1450 * (6) If the member is an array of <S> column-major matrices with <C>
1451 * columns and <R> rows, the matrix is stored identically to a row of
1452 * <S>*<C> column vectors with <R> components each, according to rule
1455 * (8) If the member is an array of <S> row-major matrices with <C> columns
1456 * and <R> rows, the matrix is stored identically to a row of <S>*<R>
1457 * row vectors with <C> components each, according to rule (4).
1459 * (10) If the member is an array of <S> structures, the <S> elements of
1460 * the array are laid out in order, according to rule (9).
1462 if (this->is_array()) {
1463 if (this->fields
.array
->is_scalar() ||
1464 this->fields
.array
->is_vector() ||
1465 this->fields
.array
->is_matrix()) {
1466 return MAX2(this->fields
.array
->std140_base_alignment(row_major
), 16);
1468 assert(this->fields
.array
->is_record() ||
1469 this->fields
.array
->is_array());
1470 return this->fields
.array
->std140_base_alignment(row_major
);
1474 /* (5) If the member is a column-major matrix with <C> columns and
1475 * <R> rows, the matrix is stored identically to an array of
1476 * <C> column vectors with <R> components each, according to
1479 * (7) If the member is a row-major matrix with <C> columns and <R>
1480 * rows, the matrix is stored identically to an array of <R>
1481 * row vectors with <C> components each, according to rule (4).
1483 if (this->is_matrix()) {
1484 const struct glsl_type
*vec_type
, *array_type
;
1485 int c
= this->matrix_columns
;
1486 int r
= this->vector_elements
;
1489 vec_type
= get_instance(base_type
, c
, 1);
1490 array_type
= glsl_type::get_array_instance(vec_type
, r
);
1492 vec_type
= get_instance(base_type
, r
, 1);
1493 array_type
= glsl_type::get_array_instance(vec_type
, c
);
1496 return array_type
->std140_base_alignment(false);
1499 /* (9) If the member is a structure, the base alignment of the
1500 * structure is <N>, where <N> is the largest base alignment
1501 * value of any of its members, and rounded up to the base
1502 * alignment of a vec4. The individual members of this
1503 * sub-structure are then assigned offsets by applying this set
1504 * of rules recursively, where the base offset of the first
1505 * member of the sub-structure is equal to the aligned offset
1506 * of the structure. The structure may have padding at the end;
1507 * the base offset of the member following the sub-structure is
1508 * rounded up to the next multiple of the base alignment of the
1511 if (this->is_record()) {
1512 unsigned base_alignment
= 16;
1513 for (unsigned i
= 0; i
< this->length
; i
++) {
1514 bool field_row_major
= row_major
;
1515 const enum glsl_matrix_layout matrix_layout
=
1516 glsl_matrix_layout(this->fields
.structure
[i
].matrix_layout
);
1517 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
1518 field_row_major
= true;
1519 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
1520 field_row_major
= false;
1523 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
1524 base_alignment
= MAX2(base_alignment
,
1525 field_type
->std140_base_alignment(field_row_major
));
1527 return base_alignment
;
1530 assert(!"not reached");
1535 glsl_type::std140_size(bool row_major
) const
1537 unsigned N
= is_64bit() ? 8 : 4;
1539 /* (1) If the member is a scalar consuming <N> basic machine units, the
1540 * base alignment is <N>.
1542 * (2) If the member is a two- or four-component vector with components
1543 * consuming <N> basic machine units, the base alignment is 2<N> or
1544 * 4<N>, respectively.
1546 * (3) If the member is a three-component vector with components consuming
1547 * <N> basic machine units, the base alignment is 4<N>.
1549 if (this->is_scalar() || this->is_vector()) {
1550 return this->vector_elements
* N
;
1553 /* (5) If the member is a column-major matrix with <C> columns and
1554 * <R> rows, the matrix is stored identically to an array of
1555 * <C> column vectors with <R> components each, according to
1558 * (6) If the member is an array of <S> column-major matrices with <C>
1559 * columns and <R> rows, the matrix is stored identically to a row of
1560 * <S>*<C> column vectors with <R> components each, according to rule
1563 * (7) If the member is a row-major matrix with <C> columns and <R>
1564 * rows, the matrix is stored identically to an array of <R>
1565 * row vectors with <C> components each, according to rule (4).
1567 * (8) If the member is an array of <S> row-major matrices with <C> columns
1568 * and <R> rows, the matrix is stored identically to a row of <S>*<R>
1569 * row vectors with <C> components each, according to rule (4).
1571 if (this->without_array()->is_matrix()) {
1572 const struct glsl_type
*element_type
;
1573 const struct glsl_type
*vec_type
;
1574 unsigned int array_len
;
1576 if (this->is_array()) {
1577 element_type
= this->without_array();
1578 array_len
= this->arrays_of_arrays_size();
1580 element_type
= this;
1585 vec_type
= get_instance(element_type
->base_type
,
1586 element_type
->matrix_columns
, 1);
1588 array_len
*= element_type
->vector_elements
;
1590 vec_type
= get_instance(element_type
->base_type
,
1591 element_type
->vector_elements
, 1);
1592 array_len
*= element_type
->matrix_columns
;
1594 const glsl_type
*array_type
= glsl_type::get_array_instance(vec_type
,
1597 return array_type
->std140_size(false);
1600 /* (4) If the member is an array of scalars or vectors, the base alignment
1601 * and array stride are set to match the base alignment of a single
1602 * array element, according to rules (1), (2), and (3), and rounded up
1603 * to the base alignment of a vec4. The array may have padding at the
1604 * end; the base offset of the member following the array is rounded up
1605 * to the next multiple of the base alignment.
1607 * (10) If the member is an array of <S> structures, the <S> elements of
1608 * the array are laid out in order, according to rule (9).
1610 if (this->is_array()) {
1611 if (this->without_array()->is_record()) {
1612 return this->arrays_of_arrays_size() *
1613 this->without_array()->std140_size(row_major
);
1615 unsigned element_base_align
=
1616 this->without_array()->std140_base_alignment(row_major
);
1617 return this->arrays_of_arrays_size() * MAX2(element_base_align
, 16);
1621 /* (9) If the member is a structure, the base alignment of the
1622 * structure is <N>, where <N> is the largest base alignment
1623 * value of any of its members, and rounded up to the base
1624 * alignment of a vec4. The individual members of this
1625 * sub-structure are then assigned offsets by applying this set
1626 * of rules recursively, where the base offset of the first
1627 * member of the sub-structure is equal to the aligned offset
1628 * of the structure. The structure may have padding at the end;
1629 * the base offset of the member following the sub-structure is
1630 * rounded up to the next multiple of the base alignment of the
1633 if (this->is_record() || this->is_interface()) {
1635 unsigned max_align
= 0;
1637 for (unsigned i
= 0; i
< this->length
; i
++) {
1638 bool field_row_major
= row_major
;
1639 const enum glsl_matrix_layout matrix_layout
=
1640 glsl_matrix_layout(this->fields
.structure
[i
].matrix_layout
);
1641 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
1642 field_row_major
= true;
1643 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
1644 field_row_major
= false;
1647 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
1648 unsigned align
= field_type
->std140_base_alignment(field_row_major
);
1650 /* Ignore unsized arrays when calculating size */
1651 if (field_type
->is_unsized_array())
1654 size
= glsl_align(size
, align
);
1655 size
+= field_type
->std140_size(field_row_major
);
1657 max_align
= MAX2(align
, max_align
);
1659 if (field_type
->is_record() && (i
+ 1 < this->length
))
1660 size
= glsl_align(size
, 16);
1662 size
= glsl_align(size
, MAX2(max_align
, 16));
1666 assert(!"not reached");
1671 glsl_type::std430_base_alignment(bool row_major
) const
1674 unsigned N
= is_64bit() ? 8 : 4;
1676 /* (1) If the member is a scalar consuming <N> basic machine units, the
1677 * base alignment is <N>.
1679 * (2) If the member is a two- or four-component vector with components
1680 * consuming <N> basic machine units, the base alignment is 2<N> or
1681 * 4<N>, respectively.
1683 * (3) If the member is a three-component vector with components consuming
1684 * <N> basic machine units, the base alignment is 4<N>.
1686 if (this->is_scalar() || this->is_vector()) {
1687 switch (this->vector_elements
) {
1698 /* OpenGL 4.30 spec, section 7.6.2.2 "Standard Uniform Block Layout":
1700 * "When using the std430 storage layout, shader storage blocks will be
1701 * laid out in buffer storage identically to uniform and shader storage
1702 * blocks using the std140 layout, except that the base alignment and
1703 * stride of arrays of scalars and vectors in rule 4 and of structures
1704 * in rule 9 are not rounded up a multiple of the base alignment of a vec4.
1707 /* (1) If the member is a scalar consuming <N> basic machine units, the
1708 * base alignment is <N>.
1710 * (2) If the member is a two- or four-component vector with components
1711 * consuming <N> basic machine units, the base alignment is 2<N> or
1712 * 4<N>, respectively.
1714 * (3) If the member is a three-component vector with components consuming
1715 * <N> basic machine units, the base alignment is 4<N>.
1717 if (this->is_array())
1718 return this->fields
.array
->std430_base_alignment(row_major
);
1720 /* (5) If the member is a column-major matrix with <C> columns and
1721 * <R> rows, the matrix is stored identically to an array of
1722 * <C> column vectors with <R> components each, according to
1725 * (7) If the member is a row-major matrix with <C> columns and <R>
1726 * rows, the matrix is stored identically to an array of <R>
1727 * row vectors with <C> components each, according to rule (4).
1729 if (this->is_matrix()) {
1730 const struct glsl_type
*vec_type
, *array_type
;
1731 int c
= this->matrix_columns
;
1732 int r
= this->vector_elements
;
1735 vec_type
= get_instance(base_type
, c
, 1);
1736 array_type
= glsl_type::get_array_instance(vec_type
, r
);
1738 vec_type
= get_instance(base_type
, r
, 1);
1739 array_type
= glsl_type::get_array_instance(vec_type
, c
);
1742 return array_type
->std430_base_alignment(false);
1745 /* (9) If the member is a structure, the base alignment of the
1746 * structure is <N>, where <N> is the largest base alignment
1747 * value of any of its members, and rounded up to the base
1748 * alignment of a vec4. The individual members of this
1749 * sub-structure are then assigned offsets by applying this set
1750 * of rules recursively, where the base offset of the first
1751 * member of the sub-structure is equal to the aligned offset
1752 * of the structure. The structure may have padding at the end;
1753 * the base offset of the member following the sub-structure is
1754 * rounded up to the next multiple of the base alignment of the
1757 if (this->is_record()) {
1758 unsigned base_alignment
= 0;
1759 for (unsigned i
= 0; i
< this->length
; i
++) {
1760 bool field_row_major
= row_major
;
1761 const enum glsl_matrix_layout matrix_layout
=
1762 glsl_matrix_layout(this->fields
.structure
[i
].matrix_layout
);
1763 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
1764 field_row_major
= true;
1765 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
1766 field_row_major
= false;
1769 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
1770 base_alignment
= MAX2(base_alignment
,
1771 field_type
->std430_base_alignment(field_row_major
));
1773 assert(base_alignment
> 0);
1774 return base_alignment
;
1776 assert(!"not reached");
1781 glsl_type::std430_array_stride(bool row_major
) const
1783 unsigned N
= is_64bit() ? 8 : 4;
1785 /* Notice that the array stride of a vec3 is not 3 * N but 4 * N.
1786 * See OpenGL 4.30 spec, section 7.6.2.2 "Standard Uniform Block Layout"
1788 * (3) If the member is a three-component vector with components consuming
1789 * <N> basic machine units, the base alignment is 4<N>.
1791 if (this->is_vector() && this->vector_elements
== 3)
1794 /* By default use std430_size(row_major) */
1795 return this->std430_size(row_major
);
1799 glsl_type::std430_size(bool row_major
) const
1801 unsigned N
= is_64bit() ? 8 : 4;
1803 /* OpenGL 4.30 spec, section 7.6.2.2 "Standard Uniform Block Layout":
1805 * "When using the std430 storage layout, shader storage blocks will be
1806 * laid out in buffer storage identically to uniform and shader storage
1807 * blocks using the std140 layout, except that the base alignment and
1808 * stride of arrays of scalars and vectors in rule 4 and of structures
1809 * in rule 9 are not rounded up a multiple of the base alignment of a vec4.
1811 if (this->is_scalar() || this->is_vector())
1812 return this->vector_elements
* N
;
1814 if (this->without_array()->is_matrix()) {
1815 const struct glsl_type
*element_type
;
1816 const struct glsl_type
*vec_type
;
1817 unsigned int array_len
;
1819 if (this->is_array()) {
1820 element_type
= this->without_array();
1821 array_len
= this->arrays_of_arrays_size();
1823 element_type
= this;
1828 vec_type
= get_instance(element_type
->base_type
,
1829 element_type
->matrix_columns
, 1);
1831 array_len
*= element_type
->vector_elements
;
1833 vec_type
= get_instance(element_type
->base_type
,
1834 element_type
->vector_elements
, 1);
1835 array_len
*= element_type
->matrix_columns
;
1837 const glsl_type
*array_type
= glsl_type::get_array_instance(vec_type
,
1840 return array_type
->std430_size(false);
1843 if (this->is_array()) {
1844 if (this->without_array()->is_record())
1845 return this->arrays_of_arrays_size() *
1846 this->without_array()->std430_size(row_major
);
1848 return this->arrays_of_arrays_size() *
1849 this->without_array()->std430_base_alignment(row_major
);
1852 if (this->is_record() || this->is_interface()) {
1854 unsigned max_align
= 0;
1856 for (unsigned i
= 0; i
< this->length
; i
++) {
1857 bool field_row_major
= row_major
;
1858 const enum glsl_matrix_layout matrix_layout
=
1859 glsl_matrix_layout(this->fields
.structure
[i
].matrix_layout
);
1860 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
1861 field_row_major
= true;
1862 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
1863 field_row_major
= false;
1866 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
1867 unsigned align
= field_type
->std430_base_alignment(field_row_major
);
1868 size
= glsl_align(size
, align
);
1869 size
+= field_type
->std430_size(field_row_major
);
1871 max_align
= MAX2(align
, max_align
);
1873 size
= glsl_align(size
, max_align
);
1877 assert(!"not reached");
1882 glsl_type::count_attribute_slots(bool is_vertex_input
) const
1884 /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
1886 * "A scalar input counts the same amount against this limit as a vec4,
1887 * so applications may want to consider packing groups of four
1888 * unrelated float inputs together into a vector to better utilize the
1889 * capabilities of the underlying hardware. A matrix input will use up
1890 * multiple locations. The number of locations used will equal the
1891 * number of columns in the matrix."
1893 * The spec does not explicitly say how arrays are counted. However, it
1894 * should be safe to assume the total number of slots consumed by an array
1895 * is the number of entries in the array multiplied by the number of slots
1896 * consumed by a single element of the array.
1898 * The spec says nothing about how structs are counted, because vertex
1899 * attributes are not allowed to be (or contain) structs. However, Mesa
1900 * allows varying structs, the number of varying slots taken up by a
1901 * varying struct is simply equal to the sum of the number of slots taken
1902 * up by each element.
1904 * Doubles are counted different depending on whether they are vertex
1905 * inputs or everything else. Vertex inputs from ARB_vertex_attrib_64bit
1906 * take one location no matter what size they are, otherwise dvec3/4
1907 * take two locations.
1909 switch (this->base_type
) {
1910 case GLSL_TYPE_UINT
:
1912 case GLSL_TYPE_FLOAT
:
1913 case GLSL_TYPE_BOOL
:
1914 return this->matrix_columns
;
1915 case GLSL_TYPE_DOUBLE
:
1916 if (this->vector_elements
> 2 && !is_vertex_input
)
1917 return this->matrix_columns
* 2;
1919 return this->matrix_columns
;
1920 case GLSL_TYPE_STRUCT
:
1921 case GLSL_TYPE_INTERFACE
: {
1924 for (unsigned i
= 0; i
< this->length
; i
++)
1925 size
+= this->fields
.structure
[i
].type
->count_attribute_slots(is_vertex_input
);
1930 case GLSL_TYPE_ARRAY
:
1931 return this->length
* this->fields
.array
->count_attribute_slots(is_vertex_input
);
1933 case GLSL_TYPE_FUNCTION
:
1934 case GLSL_TYPE_SAMPLER
:
1935 case GLSL_TYPE_IMAGE
:
1936 case GLSL_TYPE_ATOMIC_UINT
:
1937 case GLSL_TYPE_VOID
:
1938 case GLSL_TYPE_SUBROUTINE
:
1939 case GLSL_TYPE_ERROR
:
1943 assert(!"Unexpected type in count_attribute_slots()");
1949 glsl_type::coordinate_components() const
1953 switch (sampler_dimensionality
) {
1954 case GLSL_SAMPLER_DIM_1D
:
1955 case GLSL_SAMPLER_DIM_BUF
:
1958 case GLSL_SAMPLER_DIM_2D
:
1959 case GLSL_SAMPLER_DIM_RECT
:
1960 case GLSL_SAMPLER_DIM_MS
:
1961 case GLSL_SAMPLER_DIM_EXTERNAL
:
1962 case GLSL_SAMPLER_DIM_SUBPASS
:
1965 case GLSL_SAMPLER_DIM_3D
:
1966 case GLSL_SAMPLER_DIM_CUBE
:
1970 assert(!"Should not get here.");
1975 /* Array textures need an additional component for the array index, except
1976 * for cubemap array images that behave like a 2D array of interleaved
1979 if (sampler_array
&&
1980 !(base_type
== GLSL_TYPE_IMAGE
&&
1981 sampler_dimensionality
== GLSL_SAMPLER_DIM_CUBE
))
1988 * Declarations of type flyweights (glsl_type::_foo_type) and
1989 * convenience pointers (glsl_type::foo_type).
1992 #define DECL_TYPE(NAME, ...) \
1993 const glsl_type glsl_type::_##NAME##_type = glsl_type(__VA_ARGS__, #NAME); \
1994 const glsl_type *const glsl_type::NAME##_type = &glsl_type::_##NAME##_type;
1996 #define STRUCT_TYPE(NAME)
1998 #include "compiler/builtin_type_macros.h"