2 * Copyright © 2009 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
25 #include "main/macros.h"
26 #include "compiler/glsl/glsl_parser_extras.h"
27 #include "glsl_types.h"
28 #include "util/hash_table.h"
31 mtx_t
glsl_type::mutex
= _MTX_INITIALIZER_NP
;
32 hash_table
*glsl_type::array_types
= NULL
;
33 hash_table
*glsl_type::record_types
= NULL
;
34 hash_table
*glsl_type::interface_types
= NULL
;
35 hash_table
*glsl_type::function_types
= NULL
;
36 hash_table
*glsl_type::subroutine_types
= NULL
;
37 void *glsl_type::mem_ctx
= NULL
;
40 glsl_type::init_ralloc_type_ctx(void)
42 if (glsl_type::mem_ctx
== NULL
) {
43 glsl_type::mem_ctx
= ralloc_context(NULL
);
44 assert(glsl_type::mem_ctx
!= NULL
);
48 glsl_type::glsl_type(GLenum gl_type
,
49 glsl_base_type base_type
, unsigned vector_elements
,
50 unsigned matrix_columns
, const char *name
) :
53 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
54 sampled_type(0), interface_packing(0), interface_row_major(0),
55 vector_elements(vector_elements
), matrix_columns(matrix_columns
),
58 /* Values of these types must fit in the two bits of
59 * glsl_type::sampled_type.
61 STATIC_ASSERT((unsigned(GLSL_TYPE_UINT
) & 3) == unsigned(GLSL_TYPE_UINT
));
62 STATIC_ASSERT((unsigned(GLSL_TYPE_INT
) & 3) == unsigned(GLSL_TYPE_INT
));
63 STATIC_ASSERT((unsigned(GLSL_TYPE_FLOAT
) & 3) == unsigned(GLSL_TYPE_FLOAT
));
65 mtx_lock(&glsl_type::mutex
);
67 init_ralloc_type_ctx();
69 this->name
= ralloc_strdup(this->mem_ctx
, name
);
71 mtx_unlock(&glsl_type::mutex
);
73 /* Neither dimension is zero or both dimensions are zero.
75 assert((vector_elements
== 0) == (matrix_columns
== 0));
76 memset(& fields
, 0, sizeof(fields
));
79 glsl_type::glsl_type(GLenum gl_type
, glsl_base_type base_type
,
80 enum glsl_sampler_dim dim
, bool shadow
, bool array
,
81 unsigned type
, const char *name
) :
84 sampler_dimensionality(dim
), sampler_shadow(shadow
),
85 sampler_array(array
), sampled_type(type
), interface_packing(0),
86 interface_row_major(0), length(0)
88 mtx_lock(&glsl_type::mutex
);
90 init_ralloc_type_ctx();
92 this->name
= ralloc_strdup(this->mem_ctx
, name
);
94 mtx_unlock(&glsl_type::mutex
);
96 memset(& fields
, 0, sizeof(fields
));
98 matrix_columns
= vector_elements
= 1;
101 glsl_type::glsl_type(const glsl_struct_field
*fields
, unsigned num_fields
,
104 base_type(GLSL_TYPE_STRUCT
),
105 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
106 sampled_type(0), interface_packing(0), interface_row_major(0),
107 vector_elements(0), matrix_columns(0),
112 mtx_lock(&glsl_type::mutex
);
114 init_ralloc_type_ctx();
115 assert(name
!= NULL
);
116 this->name
= ralloc_strdup(this->mem_ctx
, name
);
117 this->fields
.structure
= ralloc_array(this->mem_ctx
,
118 glsl_struct_field
, length
);
120 for (i
= 0; i
< length
; i
++) {
121 this->fields
.structure
[i
] = fields
[i
];
122 this->fields
.structure
[i
].name
= ralloc_strdup(this->fields
.structure
,
126 mtx_unlock(&glsl_type::mutex
);
129 glsl_type::glsl_type(const glsl_struct_field
*fields
, unsigned num_fields
,
130 enum glsl_interface_packing packing
,
131 bool row_major
, const char *name
) :
133 base_type(GLSL_TYPE_INTERFACE
),
134 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
135 sampled_type(0), interface_packing((unsigned) packing
),
136 interface_row_major((unsigned) row_major
),
137 vector_elements(0), matrix_columns(0),
142 mtx_lock(&glsl_type::mutex
);
144 init_ralloc_type_ctx();
145 assert(name
!= NULL
);
146 this->name
= ralloc_strdup(this->mem_ctx
, name
);
147 this->fields
.structure
= rzalloc_array(this->mem_ctx
,
148 glsl_struct_field
, length
);
149 for (i
= 0; i
< length
; i
++) {
150 this->fields
.structure
[i
] = fields
[i
];
151 this->fields
.structure
[i
].name
= ralloc_strdup(this->fields
.structure
,
155 mtx_unlock(&glsl_type::mutex
);
158 glsl_type::glsl_type(const glsl_type
*return_type
,
159 const glsl_function_param
*params
, unsigned num_params
) :
161 base_type(GLSL_TYPE_FUNCTION
),
162 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
163 sampled_type(0), interface_packing(0), interface_row_major(0),
164 vector_elements(0), matrix_columns(0),
169 mtx_lock(&glsl_type::mutex
);
171 init_ralloc_type_ctx();
173 this->fields
.parameters
= rzalloc_array(this->mem_ctx
,
174 glsl_function_param
, num_params
+ 1);
176 /* We store the return type as the first parameter */
177 this->fields
.parameters
[0].type
= return_type
;
178 this->fields
.parameters
[0].in
= false;
179 this->fields
.parameters
[0].out
= true;
181 /* We store the i'th parameter in slot i+1 */
182 for (i
= 0; i
< length
; i
++) {
183 this->fields
.parameters
[i
+ 1].type
= params
[i
].type
;
184 this->fields
.parameters
[i
+ 1].in
= params
[i
].in
;
185 this->fields
.parameters
[i
+ 1].out
= params
[i
].out
;
188 mtx_unlock(&glsl_type::mutex
);
191 glsl_type::glsl_type(const char *subroutine_name
) :
193 base_type(GLSL_TYPE_SUBROUTINE
),
194 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
195 sampled_type(0), interface_packing(0), interface_row_major(0),
196 vector_elements(1), matrix_columns(1),
199 mtx_lock(&glsl_type::mutex
);
201 init_ralloc_type_ctx();
202 assert(subroutine_name
!= NULL
);
203 this->name
= ralloc_strdup(this->mem_ctx
, subroutine_name
);
204 mtx_unlock(&glsl_type::mutex
);
208 glsl_type::contains_sampler() const
210 if (this->is_array()) {
211 return this->fields
.array
->contains_sampler();
212 } else if (this->is_record() || this->is_interface()) {
213 for (unsigned int i
= 0; i
< this->length
; i
++) {
214 if (this->fields
.structure
[i
].type
->contains_sampler())
219 return this->is_sampler();
225 glsl_type::contains_integer() const
227 if (this->is_array()) {
228 return this->fields
.array
->contains_integer();
229 } else if (this->is_record() || this->is_interface()) {
230 for (unsigned int i
= 0; i
< this->length
; i
++) {
231 if (this->fields
.structure
[i
].type
->contains_integer())
236 return this->is_integer();
241 glsl_type::contains_double() const
243 if (this->is_array()) {
244 return this->fields
.array
->contains_double();
245 } else if (this->is_record() || this->is_interface()) {
246 for (unsigned int i
= 0; i
< this->length
; i
++) {
247 if (this->fields
.structure
[i
].type
->contains_double())
252 return this->is_double();
257 glsl_type::contains_opaque() const {
259 case GLSL_TYPE_SAMPLER
:
260 case GLSL_TYPE_IMAGE
:
261 case GLSL_TYPE_ATOMIC_UINT
:
263 case GLSL_TYPE_ARRAY
:
264 return fields
.array
->contains_opaque();
265 case GLSL_TYPE_STRUCT
:
266 case GLSL_TYPE_INTERFACE
:
267 for (unsigned int i
= 0; i
< length
; i
++) {
268 if (fields
.structure
[i
].type
->contains_opaque())
278 glsl_type::contains_subroutine() const
280 if (this->is_array()) {
281 return this->fields
.array
->contains_subroutine();
282 } else if (this->is_record() || this->is_interface()) {
283 for (unsigned int i
= 0; i
< this->length
; i
++) {
284 if (this->fields
.structure
[i
].type
->contains_subroutine())
289 return this->is_subroutine();
294 glsl_type::sampler_index() const
296 const glsl_type
*const t
= (this->is_array()) ? this->fields
.array
: this;
298 assert(t
->is_sampler() || t
->is_image());
300 switch (t
->sampler_dimensionality
) {
301 case GLSL_SAMPLER_DIM_1D
:
302 return (t
->sampler_array
) ? TEXTURE_1D_ARRAY_INDEX
: TEXTURE_1D_INDEX
;
303 case GLSL_SAMPLER_DIM_2D
:
304 return (t
->sampler_array
) ? TEXTURE_2D_ARRAY_INDEX
: TEXTURE_2D_INDEX
;
305 case GLSL_SAMPLER_DIM_3D
:
306 return TEXTURE_3D_INDEX
;
307 case GLSL_SAMPLER_DIM_CUBE
:
308 return (t
->sampler_array
) ? TEXTURE_CUBE_ARRAY_INDEX
: TEXTURE_CUBE_INDEX
;
309 case GLSL_SAMPLER_DIM_RECT
:
310 return TEXTURE_RECT_INDEX
;
311 case GLSL_SAMPLER_DIM_BUF
:
312 return TEXTURE_BUFFER_INDEX
;
313 case GLSL_SAMPLER_DIM_EXTERNAL
:
314 return TEXTURE_EXTERNAL_INDEX
;
315 case GLSL_SAMPLER_DIM_MS
:
316 return (t
->sampler_array
) ? TEXTURE_2D_MULTISAMPLE_ARRAY_INDEX
: TEXTURE_2D_MULTISAMPLE_INDEX
;
318 assert(!"Should not get here.");
319 return TEXTURE_BUFFER_INDEX
;
324 glsl_type::contains_image() const
326 if (this->is_array()) {
327 return this->fields
.array
->contains_image();
328 } else if (this->is_record() || this->is_interface()) {
329 for (unsigned int i
= 0; i
< this->length
; i
++) {
330 if (this->fields
.structure
[i
].type
->contains_image())
335 return this->is_image();
339 const glsl_type
*glsl_type::get_base_type() const
346 case GLSL_TYPE_FLOAT
:
348 case GLSL_TYPE_DOUBLE
:
352 case GLSL_TYPE_UINT64
:
353 return uint64_t_type
;
354 case GLSL_TYPE_INT64
:
362 const glsl_type
*glsl_type::get_scalar_type() const
364 const glsl_type
*type
= this;
367 while (type
->base_type
== GLSL_TYPE_ARRAY
)
368 type
= type
->fields
.array
;
370 /* Handle vectors and matrices */
371 switch (type
->base_type
) {
376 case GLSL_TYPE_FLOAT
:
378 case GLSL_TYPE_DOUBLE
:
382 case GLSL_TYPE_UINT64
:
383 return uint64_t_type
;
384 case GLSL_TYPE_INT64
:
387 /* Handle everything else */
394 _mesa_glsl_release_types(void)
396 /* Should only be called during atexit (either when unloading shared
397 * object, or if process terminates), so no mutex-locking should be
400 if (glsl_type::array_types
!= NULL
) {
401 _mesa_hash_table_destroy(glsl_type::array_types
, NULL
);
402 glsl_type::array_types
= NULL
;
405 if (glsl_type::record_types
!= NULL
) {
406 _mesa_hash_table_destroy(glsl_type::record_types
, NULL
);
407 glsl_type::record_types
= NULL
;
410 if (glsl_type::interface_types
!= NULL
) {
411 _mesa_hash_table_destroy(glsl_type::interface_types
, NULL
);
412 glsl_type::interface_types
= NULL
;
415 ralloc_free(glsl_type::mem_ctx
);
416 glsl_type::mem_ctx
= NULL
;
420 glsl_type::glsl_type(const glsl_type
*array
, unsigned length
) :
421 base_type(GLSL_TYPE_ARRAY
),
422 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
423 sampled_type(0), interface_packing(0), interface_row_major(0),
424 vector_elements(0), matrix_columns(0),
425 length(length
), name(NULL
)
427 this->fields
.array
= array
;
428 /* Inherit the gl type of the base. The GL type is used for
429 * uniform/statevar handling in Mesa and the arrayness of the type
430 * is represented by the size rather than the type.
432 this->gl_type
= array
->gl_type
;
434 /* Allow a maximum of 10 characters for the array size. This is enough
435 * for 32-bits of ~0. The extra 3 are for the '[', ']', and terminating
438 const unsigned name_length
= strlen(array
->name
) + 10 + 3;
440 mtx_lock(&glsl_type::mutex
);
441 char *const n
= (char *) ralloc_size(this->mem_ctx
, name_length
);
442 mtx_unlock(&glsl_type::mutex
);
445 snprintf(n
, name_length
, "%s[]", array
->name
);
447 /* insert outermost dimensions in the correct spot
448 * otherwise the dimension order will be backwards
450 const char *pos
= strchr(array
->name
, '[');
452 int idx
= pos
- array
->name
;
453 snprintf(n
, idx
+1, "%s", array
->name
);
454 snprintf(n
+ idx
, name_length
- idx
, "[%u]%s",
455 length
, array
->name
+ idx
);
457 snprintf(n
, name_length
, "%s[%u]", array
->name
, length
);
466 glsl_type::vec(unsigned components
)
468 if (components
== 0 || components
> 4)
471 static const glsl_type
*const ts
[] = {
472 float_type
, vec2_type
, vec3_type
, vec4_type
474 return ts
[components
- 1];
478 glsl_type::dvec(unsigned components
)
480 if (components
== 0 || components
> 4)
483 static const glsl_type
*const ts
[] = {
484 double_type
, dvec2_type
, dvec3_type
, dvec4_type
486 return ts
[components
- 1];
490 glsl_type::ivec(unsigned components
)
492 if (components
== 0 || components
> 4)
495 static const glsl_type
*const ts
[] = {
496 int_type
, ivec2_type
, ivec3_type
, ivec4_type
498 return ts
[components
- 1];
503 glsl_type::uvec(unsigned components
)
505 if (components
== 0 || components
> 4)
508 static const glsl_type
*const ts
[] = {
509 uint_type
, uvec2_type
, uvec3_type
, uvec4_type
511 return ts
[components
- 1];
516 glsl_type::bvec(unsigned components
)
518 if (components
== 0 || components
> 4)
521 static const glsl_type
*const ts
[] = {
522 bool_type
, bvec2_type
, bvec3_type
, bvec4_type
524 return ts
[components
- 1];
529 glsl_type::i64vec(unsigned components
)
531 if (components
== 0 || components
> 4)
534 static const glsl_type
*const ts
[] = {
535 int64_t_type
, i64vec2_type
, i64vec3_type
, i64vec4_type
537 return ts
[components
- 1];
542 glsl_type::u64vec(unsigned components
)
544 if (components
== 0 || components
> 4)
547 static const glsl_type
*const ts
[] = {
548 uint64_t_type
, u64vec2_type
, u64vec3_type
, u64vec4_type
550 return ts
[components
- 1];
554 glsl_type::get_instance(unsigned base_type
, unsigned rows
, unsigned columns
)
556 if (base_type
== GLSL_TYPE_VOID
)
559 if ((rows
< 1) || (rows
> 4) || (columns
< 1) || (columns
> 4))
562 /* Treat GLSL vectors as Nx1 matrices.
570 case GLSL_TYPE_FLOAT
:
572 case GLSL_TYPE_DOUBLE
:
576 case GLSL_TYPE_UINT64
:
578 case GLSL_TYPE_INT64
:
584 if ((base_type
!= GLSL_TYPE_FLOAT
&& base_type
!= GLSL_TYPE_DOUBLE
) || (rows
== 1))
587 /* GLSL matrix types are named mat{COLUMNS}x{ROWS}. Only the following
588 * combinations are valid:
596 #define IDX(c,r) (((c-1)*3) + (r-1))
598 if (base_type
== GLSL_TYPE_DOUBLE
) {
599 switch (IDX(columns
, rows
)) {
600 case IDX(2,2): return dmat2_type
;
601 case IDX(2,3): return dmat2x3_type
;
602 case IDX(2,4): return dmat2x4_type
;
603 case IDX(3,2): return dmat3x2_type
;
604 case IDX(3,3): return dmat3_type
;
605 case IDX(3,4): return dmat3x4_type
;
606 case IDX(4,2): return dmat4x2_type
;
607 case IDX(4,3): return dmat4x3_type
;
608 case IDX(4,4): return dmat4_type
;
609 default: return error_type
;
612 switch (IDX(columns
, rows
)) {
613 case IDX(2,2): return mat2_type
;
614 case IDX(2,3): return mat2x3_type
;
615 case IDX(2,4): return mat2x4_type
;
616 case IDX(3,2): return mat3x2_type
;
617 case IDX(3,3): return mat3_type
;
618 case IDX(3,4): return mat3x4_type
;
619 case IDX(4,2): return mat4x2_type
;
620 case IDX(4,3): return mat4x3_type
;
621 case IDX(4,4): return mat4_type
;
622 default: return error_type
;
627 assert(!"Should not get here.");
632 glsl_type::get_sampler_instance(enum glsl_sampler_dim dim
,
638 case GLSL_TYPE_FLOAT
:
640 case GLSL_SAMPLER_DIM_1D
:
642 return (array
? sampler1DArrayShadow_type
: sampler1DShadow_type
);
644 return (array
? sampler1DArray_type
: sampler1D_type
);
645 case GLSL_SAMPLER_DIM_2D
:
647 return (array
? sampler2DArrayShadow_type
: sampler2DShadow_type
);
649 return (array
? sampler2DArray_type
: sampler2D_type
);
650 case GLSL_SAMPLER_DIM_3D
:
654 return sampler3D_type
;
655 case GLSL_SAMPLER_DIM_CUBE
:
657 return (array
? samplerCubeArrayShadow_type
: samplerCubeShadow_type
);
659 return (array
? samplerCubeArray_type
: samplerCube_type
);
660 case GLSL_SAMPLER_DIM_RECT
:
664 return sampler2DRectShadow_type
;
666 return sampler2DRect_type
;
667 case GLSL_SAMPLER_DIM_BUF
:
671 return samplerBuffer_type
;
672 case GLSL_SAMPLER_DIM_MS
:
675 return (array
? sampler2DMSArray_type
: sampler2DMS_type
);
676 case GLSL_SAMPLER_DIM_EXTERNAL
:
680 return samplerExternalOES_type
;
681 case GLSL_SAMPLER_DIM_SUBPASS
:
682 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
689 case GLSL_SAMPLER_DIM_1D
:
690 return (array
? isampler1DArray_type
: isampler1D_type
);
691 case GLSL_SAMPLER_DIM_2D
:
692 return (array
? isampler2DArray_type
: isampler2D_type
);
693 case GLSL_SAMPLER_DIM_3D
:
696 return isampler3D_type
;
697 case GLSL_SAMPLER_DIM_CUBE
:
698 return (array
? isamplerCubeArray_type
: isamplerCube_type
);
699 case GLSL_SAMPLER_DIM_RECT
:
702 return isampler2DRect_type
;
703 case GLSL_SAMPLER_DIM_BUF
:
706 return isamplerBuffer_type
;
707 case GLSL_SAMPLER_DIM_MS
:
708 return (array
? isampler2DMSArray_type
: isampler2DMS_type
);
709 case GLSL_SAMPLER_DIM_EXTERNAL
:
711 case GLSL_SAMPLER_DIM_SUBPASS
:
712 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
719 case GLSL_SAMPLER_DIM_1D
:
720 return (array
? usampler1DArray_type
: usampler1D_type
);
721 case GLSL_SAMPLER_DIM_2D
:
722 return (array
? usampler2DArray_type
: usampler2D_type
);
723 case GLSL_SAMPLER_DIM_3D
:
726 return usampler3D_type
;
727 case GLSL_SAMPLER_DIM_CUBE
:
728 return (array
? usamplerCubeArray_type
: usamplerCube_type
);
729 case GLSL_SAMPLER_DIM_RECT
:
732 return usampler2DRect_type
;
733 case GLSL_SAMPLER_DIM_BUF
:
736 return usamplerBuffer_type
;
737 case GLSL_SAMPLER_DIM_MS
:
738 return (array
? usampler2DMSArray_type
: usampler2DMS_type
);
739 case GLSL_SAMPLER_DIM_EXTERNAL
:
741 case GLSL_SAMPLER_DIM_SUBPASS
:
742 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
749 unreachable("switch statement above should be complete");
753 glsl_type::get_image_instance(enum glsl_sampler_dim dim
,
754 bool array
, glsl_base_type type
)
757 case GLSL_TYPE_FLOAT
:
759 case GLSL_SAMPLER_DIM_1D
:
760 return (array
? image1DArray_type
: image1D_type
);
761 case GLSL_SAMPLER_DIM_2D
:
762 return (array
? image2DArray_type
: image2D_type
);
763 case GLSL_SAMPLER_DIM_3D
:
765 case GLSL_SAMPLER_DIM_CUBE
:
766 return (array
? imageCubeArray_type
: imageCube_type
);
767 case GLSL_SAMPLER_DIM_RECT
:
771 return image2DRect_type
;
772 case GLSL_SAMPLER_DIM_BUF
:
776 return imageBuffer_type
;
777 case GLSL_SAMPLER_DIM_MS
:
778 return (array
? image2DMSArray_type
: image2DMS_type
);
779 case GLSL_SAMPLER_DIM_SUBPASS
:
780 return subpassInput_type
;
781 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
782 return subpassInputMS_type
;
783 case GLSL_SAMPLER_DIM_EXTERNAL
:
788 case GLSL_SAMPLER_DIM_1D
:
789 return (array
? iimage1DArray_type
: iimage1D_type
);
790 case GLSL_SAMPLER_DIM_2D
:
791 return (array
? iimage2DArray_type
: iimage2D_type
);
792 case GLSL_SAMPLER_DIM_3D
:
795 return iimage3D_type
;
796 case GLSL_SAMPLER_DIM_CUBE
:
797 return (array
? iimageCubeArray_type
: iimageCube_type
);
798 case GLSL_SAMPLER_DIM_RECT
:
801 return iimage2DRect_type
;
802 case GLSL_SAMPLER_DIM_BUF
:
805 return iimageBuffer_type
;
806 case GLSL_SAMPLER_DIM_MS
:
807 return (array
? iimage2DMSArray_type
: iimage2DMS_type
);
808 case GLSL_SAMPLER_DIM_SUBPASS
:
809 return isubpassInput_type
;
810 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
811 return isubpassInputMS_type
;
812 case GLSL_SAMPLER_DIM_EXTERNAL
:
817 case GLSL_SAMPLER_DIM_1D
:
818 return (array
? uimage1DArray_type
: uimage1D_type
);
819 case GLSL_SAMPLER_DIM_2D
:
820 return (array
? uimage2DArray_type
: uimage2D_type
);
821 case GLSL_SAMPLER_DIM_3D
:
824 return uimage3D_type
;
825 case GLSL_SAMPLER_DIM_CUBE
:
826 return (array
? uimageCubeArray_type
: uimageCube_type
);
827 case GLSL_SAMPLER_DIM_RECT
:
830 return uimage2DRect_type
;
831 case GLSL_SAMPLER_DIM_BUF
:
834 return uimageBuffer_type
;
835 case GLSL_SAMPLER_DIM_MS
:
836 return (array
? uimage2DMSArray_type
: uimage2DMS_type
);
837 case GLSL_SAMPLER_DIM_SUBPASS
:
838 return usubpassInput_type
;
839 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
840 return usubpassInputMS_type
;
841 case GLSL_SAMPLER_DIM_EXTERNAL
:
848 unreachable("switch statement above should be complete");
852 glsl_type::get_array_instance(const glsl_type
*base
, unsigned array_size
)
854 /* Generate a name using the base type pointer in the key. This is
855 * done because the name of the base type may not be unique across
856 * shaders. For example, two shaders may have different record types
860 snprintf(key
, sizeof(key
), "%p[%u]", (void *) base
, array_size
);
862 mtx_lock(&glsl_type::mutex
);
864 if (array_types
== NULL
) {
865 array_types
= _mesa_hash_table_create(NULL
, _mesa_key_hash_string
,
866 _mesa_key_string_equal
);
869 const struct hash_entry
*entry
= _mesa_hash_table_search(array_types
, key
);
871 mtx_unlock(&glsl_type::mutex
);
872 const glsl_type
*t
= new glsl_type(base
, array_size
);
873 mtx_lock(&glsl_type::mutex
);
875 entry
= _mesa_hash_table_insert(array_types
,
876 ralloc_strdup(mem_ctx
, key
),
880 assert(((glsl_type
*) entry
->data
)->base_type
== GLSL_TYPE_ARRAY
);
881 assert(((glsl_type
*) entry
->data
)->length
== array_size
);
882 assert(((glsl_type
*) entry
->data
)->fields
.array
== base
);
884 mtx_unlock(&glsl_type::mutex
);
886 return (glsl_type
*) entry
->data
;
891 glsl_type::record_compare(const glsl_type
*b
, bool match_locations
) const
893 if (this->length
!= b
->length
)
896 if (this->interface_packing
!= b
->interface_packing
)
899 if (this->interface_row_major
!= b
->interface_row_major
)
902 /* From the GLSL 4.20 specification (Sec 4.2):
904 * "Structures must have the same name, sequence of type names, and
905 * type definitions, and field names to be considered the same type."
907 * GLSL ES behaves the same (Ver 1.00 Sec 4.2.4, Ver 3.00 Sec 4.2.5).
909 * Note that we cannot force type name check when comparing unnamed
910 * structure types, these have a unique name assigned during parsing.
912 if (!this->is_anonymous() && !b
->is_anonymous())
913 if (strcmp(this->name
, b
->name
) != 0)
916 for (unsigned i
= 0; i
< this->length
; i
++) {
917 if (this->fields
.structure
[i
].type
!= b
->fields
.structure
[i
].type
)
919 if (strcmp(this->fields
.structure
[i
].name
,
920 b
->fields
.structure
[i
].name
) != 0)
922 if (this->fields
.structure
[i
].matrix_layout
923 != b
->fields
.structure
[i
].matrix_layout
)
925 if (match_locations
&& this->fields
.structure
[i
].location
926 != b
->fields
.structure
[i
].location
)
928 if (this->fields
.structure
[i
].offset
929 != b
->fields
.structure
[i
].offset
)
931 if (this->fields
.structure
[i
].interpolation
932 != b
->fields
.structure
[i
].interpolation
)
934 if (this->fields
.structure
[i
].centroid
935 != b
->fields
.structure
[i
].centroid
)
937 if (this->fields
.structure
[i
].sample
938 != b
->fields
.structure
[i
].sample
)
940 if (this->fields
.structure
[i
].patch
941 != b
->fields
.structure
[i
].patch
)
943 if (this->fields
.structure
[i
].image_read_only
944 != b
->fields
.structure
[i
].image_read_only
)
946 if (this->fields
.structure
[i
].image_write_only
947 != b
->fields
.structure
[i
].image_write_only
)
949 if (this->fields
.structure
[i
].image_coherent
950 != b
->fields
.structure
[i
].image_coherent
)
952 if (this->fields
.structure
[i
].image_volatile
953 != b
->fields
.structure
[i
].image_volatile
)
955 if (this->fields
.structure
[i
].image_restrict
956 != b
->fields
.structure
[i
].image_restrict
)
958 if (this->fields
.structure
[i
].precision
959 != b
->fields
.structure
[i
].precision
)
961 if (this->fields
.structure
[i
].explicit_xfb_buffer
962 != b
->fields
.structure
[i
].explicit_xfb_buffer
)
964 if (this->fields
.structure
[i
].xfb_buffer
965 != b
->fields
.structure
[i
].xfb_buffer
)
967 if (this->fields
.structure
[i
].xfb_stride
968 != b
->fields
.structure
[i
].xfb_stride
)
977 glsl_type::record_key_compare(const void *a
, const void *b
)
979 const glsl_type
*const key1
= (glsl_type
*) a
;
980 const glsl_type
*const key2
= (glsl_type
*) b
;
982 return strcmp(key1
->name
, key2
->name
) == 0 && key1
->record_compare(key2
);
987 * Generate an integer hash value for a glsl_type structure type.
990 glsl_type::record_key_hash(const void *a
)
992 const glsl_type
*const key
= (glsl_type
*) a
;
993 uintptr_t hash
= key
->length
;
996 for (unsigned i
= 0; i
< key
->length
; i
++) {
997 /* casting pointer to uintptr_t */
998 hash
= (hash
* 13 ) + (uintptr_t) key
->fields
.structure
[i
].type
;
1001 if (sizeof(hash
) == 8)
1002 retval
= (hash
& 0xffffffff) ^ ((uint64_t) hash
>> 32);
1011 glsl_type::get_record_instance(const glsl_struct_field
*fields
,
1012 unsigned num_fields
,
1015 const glsl_type
key(fields
, num_fields
, name
);
1017 mtx_lock(&glsl_type::mutex
);
1019 if (record_types
== NULL
) {
1020 record_types
= _mesa_hash_table_create(NULL
, record_key_hash
,
1021 record_key_compare
);
1024 const struct hash_entry
*entry
= _mesa_hash_table_search(record_types
,
1026 if (entry
== NULL
) {
1027 mtx_unlock(&glsl_type::mutex
);
1028 const glsl_type
*t
= new glsl_type(fields
, num_fields
, name
);
1029 mtx_lock(&glsl_type::mutex
);
1031 entry
= _mesa_hash_table_insert(record_types
, t
, (void *) t
);
1034 assert(((glsl_type
*) entry
->data
)->base_type
== GLSL_TYPE_STRUCT
);
1035 assert(((glsl_type
*) entry
->data
)->length
== num_fields
);
1036 assert(strcmp(((glsl_type
*) entry
->data
)->name
, name
) == 0);
1038 mtx_unlock(&glsl_type::mutex
);
1040 return (glsl_type
*) entry
->data
;
1045 glsl_type::get_interface_instance(const glsl_struct_field
*fields
,
1046 unsigned num_fields
,
1047 enum glsl_interface_packing packing
,
1049 const char *block_name
)
1051 const glsl_type
key(fields
, num_fields
, packing
, row_major
, block_name
);
1053 mtx_lock(&glsl_type::mutex
);
1055 if (interface_types
== NULL
) {
1056 interface_types
= _mesa_hash_table_create(NULL
, record_key_hash
,
1057 record_key_compare
);
1060 const struct hash_entry
*entry
= _mesa_hash_table_search(interface_types
,
1062 if (entry
== NULL
) {
1063 mtx_unlock(&glsl_type::mutex
);
1064 const glsl_type
*t
= new glsl_type(fields
, num_fields
,
1065 packing
, row_major
, block_name
);
1066 mtx_lock(&glsl_type::mutex
);
1068 entry
= _mesa_hash_table_insert(interface_types
, t
, (void *) t
);
1071 assert(((glsl_type
*) entry
->data
)->base_type
== GLSL_TYPE_INTERFACE
);
1072 assert(((glsl_type
*) entry
->data
)->length
== num_fields
);
1073 assert(strcmp(((glsl_type
*) entry
->data
)->name
, block_name
) == 0);
1075 mtx_unlock(&glsl_type::mutex
);
1077 return (glsl_type
*) entry
->data
;
1081 glsl_type::get_subroutine_instance(const char *subroutine_name
)
1083 const glsl_type
key(subroutine_name
);
1085 mtx_lock(&glsl_type::mutex
);
1087 if (subroutine_types
== NULL
) {
1088 subroutine_types
= _mesa_hash_table_create(NULL
, record_key_hash
,
1089 record_key_compare
);
1092 const struct hash_entry
*entry
= _mesa_hash_table_search(subroutine_types
,
1094 if (entry
== NULL
) {
1095 mtx_unlock(&glsl_type::mutex
);
1096 const glsl_type
*t
= new glsl_type(subroutine_name
);
1097 mtx_lock(&glsl_type::mutex
);
1099 entry
= _mesa_hash_table_insert(subroutine_types
, t
, (void *) t
);
1102 assert(((glsl_type
*) entry
->data
)->base_type
== GLSL_TYPE_SUBROUTINE
);
1103 assert(strcmp(((glsl_type
*) entry
->data
)->name
, subroutine_name
) == 0);
1105 mtx_unlock(&glsl_type::mutex
);
1107 return (glsl_type
*) entry
->data
;
1112 function_key_compare(const void *a
, const void *b
)
1114 const glsl_type
*const key1
= (glsl_type
*) a
;
1115 const glsl_type
*const key2
= (glsl_type
*) b
;
1117 if (key1
->length
!= key2
->length
)
1120 return memcmp(key1
->fields
.parameters
, key2
->fields
.parameters
,
1121 (key1
->length
+ 1) * sizeof(*key1
->fields
.parameters
)) == 0;
1126 function_key_hash(const void *a
)
1128 const glsl_type
*const key
= (glsl_type
*) a
;
1129 return _mesa_hash_data(key
->fields
.parameters
,
1130 (key
->length
+ 1) * sizeof(*key
->fields
.parameters
));
1134 glsl_type::get_function_instance(const glsl_type
*return_type
,
1135 const glsl_function_param
*params
,
1136 unsigned num_params
)
1138 const glsl_type
key(return_type
, params
, num_params
);
1140 mtx_lock(&glsl_type::mutex
);
1142 if (function_types
== NULL
) {
1143 function_types
= _mesa_hash_table_create(NULL
, function_key_hash
,
1144 function_key_compare
);
1147 struct hash_entry
*entry
= _mesa_hash_table_search(function_types
, &key
);
1148 if (entry
== NULL
) {
1149 mtx_unlock(&glsl_type::mutex
);
1150 const glsl_type
*t
= new glsl_type(return_type
, params
, num_params
);
1151 mtx_lock(&glsl_type::mutex
);
1153 entry
= _mesa_hash_table_insert(function_types
, t
, (void *) t
);
1156 const glsl_type
*t
= (const glsl_type
*)entry
->data
;
1158 assert(t
->base_type
== GLSL_TYPE_FUNCTION
);
1159 assert(t
->length
== num_params
);
1161 mtx_unlock(&glsl_type::mutex
);
1168 glsl_type::get_mul_type(const glsl_type
*type_a
, const glsl_type
*type_b
)
1170 if (type_a
== type_b
) {
1172 } else if (type_a
->is_matrix() && type_b
->is_matrix()) {
1173 /* Matrix multiply. The columns of A must match the rows of B. Given
1174 * the other previously tested constraints, this means the vector type
1175 * of a row from A must be the same as the vector type of a column from
1178 if (type_a
->row_type() == type_b
->column_type()) {
1179 /* The resulting matrix has the number of columns of matrix B and
1180 * the number of rows of matrix A. We get the row count of A by
1181 * looking at the size of a vector that makes up a column. The
1182 * transpose (size of a row) is done for B.
1184 const glsl_type
*const type
=
1185 get_instance(type_a
->base_type
,
1186 type_a
->column_type()->vector_elements
,
1187 type_b
->row_type()->vector_elements
);
1188 assert(type
!= error_type
);
1192 } else if (type_a
->is_matrix()) {
1193 /* A is a matrix and B is a column vector. Columns of A must match
1194 * rows of B. Given the other previously tested constraints, this
1195 * means the vector type of a row from A must be the same as the
1196 * vector the type of B.
1198 if (type_a
->row_type() == type_b
) {
1199 /* The resulting vector has a number of elements equal to
1200 * the number of rows of matrix A. */
1201 const glsl_type
*const type
=
1202 get_instance(type_a
->base_type
,
1203 type_a
->column_type()->vector_elements
,
1205 assert(type
!= error_type
);
1210 assert(type_b
->is_matrix());
1212 /* A is a row vector and B is a matrix. Columns of A must match rows
1213 * of B. Given the other previously tested constraints, this means
1214 * the type of A must be the same as the vector type of a column from
1217 if (type_a
== type_b
->column_type()) {
1218 /* The resulting vector has a number of elements equal to
1219 * the number of columns of matrix B. */
1220 const glsl_type
*const type
=
1221 get_instance(type_a
->base_type
,
1222 type_b
->row_type()->vector_elements
,
1224 assert(type
!= error_type
);
1235 glsl_type::field_type(const char *name
) const
1237 if (this->base_type
!= GLSL_TYPE_STRUCT
1238 && this->base_type
!= GLSL_TYPE_INTERFACE
)
1241 for (unsigned i
= 0; i
< this->length
; i
++) {
1242 if (strcmp(name
, this->fields
.structure
[i
].name
) == 0)
1243 return this->fields
.structure
[i
].type
;
1251 glsl_type::field_index(const char *name
) const
1253 if (this->base_type
!= GLSL_TYPE_STRUCT
1254 && this->base_type
!= GLSL_TYPE_INTERFACE
)
1257 for (unsigned i
= 0; i
< this->length
; i
++) {
1258 if (strcmp(name
, this->fields
.structure
[i
].name
) == 0)
1267 glsl_type::component_slots() const
1269 switch (this->base_type
) {
1270 case GLSL_TYPE_UINT
:
1272 case GLSL_TYPE_FLOAT
:
1273 case GLSL_TYPE_BOOL
:
1274 return this->components();
1276 case GLSL_TYPE_DOUBLE
:
1277 case GLSL_TYPE_UINT64
:
1278 case GLSL_TYPE_INT64
:
1279 return 2 * this->components();
1281 case GLSL_TYPE_STRUCT
:
1282 case GLSL_TYPE_INTERFACE
: {
1285 for (unsigned i
= 0; i
< this->length
; i
++)
1286 size
+= this->fields
.structure
[i
].type
->component_slots();
1291 case GLSL_TYPE_ARRAY
:
1292 return this->length
* this->fields
.array
->component_slots();
1294 case GLSL_TYPE_SAMPLER
:
1295 case GLSL_TYPE_IMAGE
:
1296 case GLSL_TYPE_SUBROUTINE
:
1299 case GLSL_TYPE_FUNCTION
:
1300 case GLSL_TYPE_ATOMIC_UINT
:
1301 case GLSL_TYPE_VOID
:
1302 case GLSL_TYPE_ERROR
:
1310 glsl_type::record_location_offset(unsigned length
) const
1312 unsigned offset
= 0;
1313 const glsl_type
*t
= this->without_array();
1314 if (t
->is_record()) {
1315 assert(length
<= t
->length
);
1317 for (unsigned i
= 0; i
< length
; i
++) {
1318 const glsl_type
*st
= t
->fields
.structure
[i
].type
;
1319 const glsl_type
*wa
= st
->without_array();
1320 if (wa
->is_record()) {
1321 unsigned r_offset
= wa
->record_location_offset(wa
->length
);
1322 offset
+= st
->is_array() ?
1323 st
->arrays_of_arrays_size() * r_offset
: r_offset
;
1324 } else if (st
->is_array() && st
->fields
.array
->is_array()) {
1325 unsigned outer_array_size
= st
->length
;
1326 const glsl_type
*base_type
= st
->fields
.array
;
1328 /* For arrays of arrays the outer arrays take up a uniform
1329 * slot for each element. The innermost array elements share a
1330 * single slot so we ignore the innermost array when calculating
1333 while (base_type
->fields
.array
->is_array()) {
1334 outer_array_size
= outer_array_size
* base_type
->length
;
1335 base_type
= base_type
->fields
.array
;
1337 offset
+= outer_array_size
;
1339 /* We dont worry about arrays here because unless the array
1340 * contains a structure or another array it only takes up a single
1351 glsl_type::uniform_locations() const
1355 switch (this->base_type
) {
1356 case GLSL_TYPE_UINT
:
1358 case GLSL_TYPE_FLOAT
:
1359 case GLSL_TYPE_DOUBLE
:
1360 case GLSL_TYPE_UINT64
:
1361 case GLSL_TYPE_INT64
:
1362 case GLSL_TYPE_BOOL
:
1363 case GLSL_TYPE_SAMPLER
:
1364 case GLSL_TYPE_IMAGE
:
1365 case GLSL_TYPE_SUBROUTINE
:
1368 case GLSL_TYPE_STRUCT
:
1369 case GLSL_TYPE_INTERFACE
:
1370 for (unsigned i
= 0; i
< this->length
; i
++)
1371 size
+= this->fields
.structure
[i
].type
->uniform_locations();
1373 case GLSL_TYPE_ARRAY
:
1374 return this->length
* this->fields
.array
->uniform_locations();
1381 glsl_type::varying_count() const
1385 switch (this->base_type
) {
1386 case GLSL_TYPE_UINT
:
1388 case GLSL_TYPE_FLOAT
:
1389 case GLSL_TYPE_DOUBLE
:
1390 case GLSL_TYPE_BOOL
:
1391 case GLSL_TYPE_UINT64
:
1392 case GLSL_TYPE_INT64
:
1395 case GLSL_TYPE_STRUCT
:
1396 case GLSL_TYPE_INTERFACE
:
1397 for (unsigned i
= 0; i
< this->length
; i
++)
1398 size
+= this->fields
.structure
[i
].type
->varying_count();
1400 case GLSL_TYPE_ARRAY
:
1401 /* Don't count innermost array elements */
1402 if (this->without_array()->is_record() ||
1403 this->without_array()->is_interface() ||
1404 this->fields
.array
->is_array())
1405 return this->length
* this->fields
.array
->varying_count();
1407 return this->fields
.array
->varying_count();
1409 assert(!"unsupported varying type");
1415 glsl_type::can_implicitly_convert_to(const glsl_type
*desired
,
1416 _mesa_glsl_parse_state
*state
) const
1418 if (this == desired
)
1421 /* GLSL 1.10 and ESSL do not allow implicit conversions. If there is no
1422 * state, we're doing intra-stage function linking where these checks have
1423 * already been done.
1425 if (state
&& (state
->es_shader
|| !state
->is_version(120, 0)))
1428 /* There is no conversion among matrix types. */
1429 if (this->matrix_columns
> 1 || desired
->matrix_columns
> 1)
1432 /* Vector size must match. */
1433 if (this->vector_elements
!= desired
->vector_elements
)
1436 /* int and uint can be converted to float. */
1437 if (desired
->is_float() && this->is_integer())
1440 /* With GLSL 4.0, ARB_gpu_shader5, or MESA_shader_integer_functions, int
1441 * can be converted to uint. Note that state may be NULL here, when
1442 * resolving function calls in the linker. By this time, all the
1443 * state-dependent checks have already happened though, so allow anything
1444 * that's allowed in any shader version.
1446 if ((!state
|| state
->is_version(400, 0) || state
->ARB_gpu_shader5_enable
||
1447 state
->MESA_shader_integer_functions_enable
) &&
1448 desired
->base_type
== GLSL_TYPE_UINT
&& this->base_type
== GLSL_TYPE_INT
)
1451 /* No implicit conversions from double. */
1452 if ((!state
|| state
->has_double()) && this->is_double())
1455 /* Conversions from different types to double. */
1456 if ((!state
|| state
->has_double()) && desired
->is_double()) {
1457 if (this->is_float())
1459 if (this->is_integer())
1467 glsl_type::std140_base_alignment(bool row_major
) const
1469 unsigned N
= is_64bit() ? 8 : 4;
1471 /* (1) If the member is a scalar consuming <N> basic machine units, the
1472 * base alignment is <N>.
1474 * (2) If the member is a two- or four-component vector with components
1475 * consuming <N> basic machine units, the base alignment is 2<N> or
1476 * 4<N>, respectively.
1478 * (3) If the member is a three-component vector with components consuming
1479 * <N> basic machine units, the base alignment is 4<N>.
1481 if (this->is_scalar() || this->is_vector()) {
1482 switch (this->vector_elements
) {
1493 /* (4) If the member is an array of scalars or vectors, the base alignment
1494 * and array stride are set to match the base alignment of a single
1495 * array element, according to rules (1), (2), and (3), and rounded up
1496 * to the base alignment of a vec4. The array may have padding at the
1497 * end; the base offset of the member following the array is rounded up
1498 * to the next multiple of the base alignment.
1500 * (6) If the member is an array of <S> column-major matrices with <C>
1501 * columns and <R> rows, the matrix is stored identically to a row of
1502 * <S>*<C> column vectors with <R> components each, according to rule
1505 * (8) If the member is an array of <S> row-major matrices with <C> columns
1506 * and <R> rows, the matrix is stored identically to a row of <S>*<R>
1507 * row vectors with <C> components each, according to rule (4).
1509 * (10) If the member is an array of <S> structures, the <S> elements of
1510 * the array are laid out in order, according to rule (9).
1512 if (this->is_array()) {
1513 if (this->fields
.array
->is_scalar() ||
1514 this->fields
.array
->is_vector() ||
1515 this->fields
.array
->is_matrix()) {
1516 return MAX2(this->fields
.array
->std140_base_alignment(row_major
), 16);
1518 assert(this->fields
.array
->is_record() ||
1519 this->fields
.array
->is_array());
1520 return this->fields
.array
->std140_base_alignment(row_major
);
1524 /* (5) If the member is a column-major matrix with <C> columns and
1525 * <R> rows, the matrix is stored identically to an array of
1526 * <C> column vectors with <R> components each, according to
1529 * (7) If the member is a row-major matrix with <C> columns and <R>
1530 * rows, the matrix is stored identically to an array of <R>
1531 * row vectors with <C> components each, according to rule (4).
1533 if (this->is_matrix()) {
1534 const struct glsl_type
*vec_type
, *array_type
;
1535 int c
= this->matrix_columns
;
1536 int r
= this->vector_elements
;
1539 vec_type
= get_instance(base_type
, c
, 1);
1540 array_type
= glsl_type::get_array_instance(vec_type
, r
);
1542 vec_type
= get_instance(base_type
, r
, 1);
1543 array_type
= glsl_type::get_array_instance(vec_type
, c
);
1546 return array_type
->std140_base_alignment(false);
1549 /* (9) If the member is a structure, the base alignment of the
1550 * structure is <N>, where <N> is the largest base alignment
1551 * value of any of its members, and rounded up to the base
1552 * alignment of a vec4. The individual members of this
1553 * sub-structure are then assigned offsets by applying this set
1554 * of rules recursively, where the base offset of the first
1555 * member of the sub-structure is equal to the aligned offset
1556 * of the structure. The structure may have padding at the end;
1557 * the base offset of the member following the sub-structure is
1558 * rounded up to the next multiple of the base alignment of the
1561 if (this->is_record()) {
1562 unsigned base_alignment
= 16;
1563 for (unsigned i
= 0; i
< this->length
; i
++) {
1564 bool field_row_major
= row_major
;
1565 const enum glsl_matrix_layout matrix_layout
=
1566 glsl_matrix_layout(this->fields
.structure
[i
].matrix_layout
);
1567 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
1568 field_row_major
= true;
1569 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
1570 field_row_major
= false;
1573 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
1574 base_alignment
= MAX2(base_alignment
,
1575 field_type
->std140_base_alignment(field_row_major
));
1577 return base_alignment
;
1580 assert(!"not reached");
1585 glsl_type::std140_size(bool row_major
) const
1587 unsigned N
= is_64bit() ? 8 : 4;
1589 /* (1) If the member is a scalar consuming <N> basic machine units, the
1590 * base alignment is <N>.
1592 * (2) If the member is a two- or four-component vector with components
1593 * consuming <N> basic machine units, the base alignment is 2<N> or
1594 * 4<N>, respectively.
1596 * (3) If the member is a three-component vector with components consuming
1597 * <N> basic machine units, the base alignment is 4<N>.
1599 if (this->is_scalar() || this->is_vector()) {
1600 return this->vector_elements
* N
;
1603 /* (5) If the member is a column-major matrix with <C> columns and
1604 * <R> rows, the matrix is stored identically to an array of
1605 * <C> column vectors with <R> components each, according to
1608 * (6) If the member is an array of <S> column-major matrices with <C>
1609 * columns and <R> rows, the matrix is stored identically to a row of
1610 * <S>*<C> column vectors with <R> components each, according to rule
1613 * (7) If the member is a row-major matrix with <C> columns and <R>
1614 * rows, the matrix is stored identically to an array of <R>
1615 * row vectors with <C> components each, according to rule (4).
1617 * (8) If the member is an array of <S> row-major matrices with <C> columns
1618 * and <R> rows, the matrix is stored identically to a row of <S>*<R>
1619 * row vectors with <C> components each, according to rule (4).
1621 if (this->without_array()->is_matrix()) {
1622 const struct glsl_type
*element_type
;
1623 const struct glsl_type
*vec_type
;
1624 unsigned int array_len
;
1626 if (this->is_array()) {
1627 element_type
= this->without_array();
1628 array_len
= this->arrays_of_arrays_size();
1630 element_type
= this;
1635 vec_type
= get_instance(element_type
->base_type
,
1636 element_type
->matrix_columns
, 1);
1638 array_len
*= element_type
->vector_elements
;
1640 vec_type
= get_instance(element_type
->base_type
,
1641 element_type
->vector_elements
, 1);
1642 array_len
*= element_type
->matrix_columns
;
1644 const glsl_type
*array_type
= glsl_type::get_array_instance(vec_type
,
1647 return array_type
->std140_size(false);
1650 /* (4) If the member is an array of scalars or vectors, the base alignment
1651 * and array stride are set to match the base alignment of a single
1652 * array element, according to rules (1), (2), and (3), and rounded up
1653 * to the base alignment of a vec4. The array may have padding at the
1654 * end; the base offset of the member following the array is rounded up
1655 * to the next multiple of the base alignment.
1657 * (10) If the member is an array of <S> structures, the <S> elements of
1658 * the array are laid out in order, according to rule (9).
1660 if (this->is_array()) {
1661 if (this->without_array()->is_record()) {
1662 return this->arrays_of_arrays_size() *
1663 this->without_array()->std140_size(row_major
);
1665 unsigned element_base_align
=
1666 this->without_array()->std140_base_alignment(row_major
);
1667 return this->arrays_of_arrays_size() * MAX2(element_base_align
, 16);
1671 /* (9) If the member is a structure, the base alignment of the
1672 * structure is <N>, where <N> is the largest base alignment
1673 * value of any of its members, and rounded up to the base
1674 * alignment of a vec4. The individual members of this
1675 * sub-structure are then assigned offsets by applying this set
1676 * of rules recursively, where the base offset of the first
1677 * member of the sub-structure is equal to the aligned offset
1678 * of the structure. The structure may have padding at the end;
1679 * the base offset of the member following the sub-structure is
1680 * rounded up to the next multiple of the base alignment of the
1683 if (this->is_record() || this->is_interface()) {
1685 unsigned max_align
= 0;
1687 for (unsigned i
= 0; i
< this->length
; i
++) {
1688 bool field_row_major
= row_major
;
1689 const enum glsl_matrix_layout matrix_layout
=
1690 glsl_matrix_layout(this->fields
.structure
[i
].matrix_layout
);
1691 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
1692 field_row_major
= true;
1693 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
1694 field_row_major
= false;
1697 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
1698 unsigned align
= field_type
->std140_base_alignment(field_row_major
);
1700 /* Ignore unsized arrays when calculating size */
1701 if (field_type
->is_unsized_array())
1704 size
= glsl_align(size
, align
);
1705 size
+= field_type
->std140_size(field_row_major
);
1707 max_align
= MAX2(align
, max_align
);
1709 if (field_type
->is_record() && (i
+ 1 < this->length
))
1710 size
= glsl_align(size
, 16);
1712 size
= glsl_align(size
, MAX2(max_align
, 16));
1716 assert(!"not reached");
1721 glsl_type::std430_base_alignment(bool row_major
) const
1724 unsigned N
= is_64bit() ? 8 : 4;
1726 /* (1) If the member is a scalar consuming <N> basic machine units, the
1727 * base alignment is <N>.
1729 * (2) If the member is a two- or four-component vector with components
1730 * consuming <N> basic machine units, the base alignment is 2<N> or
1731 * 4<N>, respectively.
1733 * (3) If the member is a three-component vector with components consuming
1734 * <N> basic machine units, the base alignment is 4<N>.
1736 if (this->is_scalar() || this->is_vector()) {
1737 switch (this->vector_elements
) {
1748 /* OpenGL 4.30 spec, section 7.6.2.2 "Standard Uniform Block Layout":
1750 * "When using the std430 storage layout, shader storage blocks will be
1751 * laid out in buffer storage identically to uniform and shader storage
1752 * blocks using the std140 layout, except that the base alignment and
1753 * stride of arrays of scalars and vectors in rule 4 and of structures
1754 * in rule 9 are not rounded up a multiple of the base alignment of a vec4.
1757 /* (1) If the member is a scalar consuming <N> basic machine units, the
1758 * base alignment is <N>.
1760 * (2) If the member is a two- or four-component vector with components
1761 * consuming <N> basic machine units, the base alignment is 2<N> or
1762 * 4<N>, respectively.
1764 * (3) If the member is a three-component vector with components consuming
1765 * <N> basic machine units, the base alignment is 4<N>.
1767 if (this->is_array())
1768 return this->fields
.array
->std430_base_alignment(row_major
);
1770 /* (5) If the member is a column-major matrix with <C> columns and
1771 * <R> rows, the matrix is stored identically to an array of
1772 * <C> column vectors with <R> components each, according to
1775 * (7) If the member is a row-major matrix with <C> columns and <R>
1776 * rows, the matrix is stored identically to an array of <R>
1777 * row vectors with <C> components each, according to rule (4).
1779 if (this->is_matrix()) {
1780 const struct glsl_type
*vec_type
, *array_type
;
1781 int c
= this->matrix_columns
;
1782 int r
= this->vector_elements
;
1785 vec_type
= get_instance(base_type
, c
, 1);
1786 array_type
= glsl_type::get_array_instance(vec_type
, r
);
1788 vec_type
= get_instance(base_type
, r
, 1);
1789 array_type
= glsl_type::get_array_instance(vec_type
, c
);
1792 return array_type
->std430_base_alignment(false);
1795 /* (9) If the member is a structure, the base alignment of the
1796 * structure is <N>, where <N> is the largest base alignment
1797 * value of any of its members, and rounded up to the base
1798 * alignment of a vec4. The individual members of this
1799 * sub-structure are then assigned offsets by applying this set
1800 * of rules recursively, where the base offset of the first
1801 * member of the sub-structure is equal to the aligned offset
1802 * of the structure. The structure may have padding at the end;
1803 * the base offset of the member following the sub-structure is
1804 * rounded up to the next multiple of the base alignment of the
1807 if (this->is_record()) {
1808 unsigned base_alignment
= 0;
1809 for (unsigned i
= 0; i
< this->length
; i
++) {
1810 bool field_row_major
= row_major
;
1811 const enum glsl_matrix_layout matrix_layout
=
1812 glsl_matrix_layout(this->fields
.structure
[i
].matrix_layout
);
1813 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
1814 field_row_major
= true;
1815 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
1816 field_row_major
= false;
1819 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
1820 base_alignment
= MAX2(base_alignment
,
1821 field_type
->std430_base_alignment(field_row_major
));
1823 assert(base_alignment
> 0);
1824 return base_alignment
;
1826 assert(!"not reached");
1831 glsl_type::std430_array_stride(bool row_major
) const
1833 unsigned N
= is_64bit() ? 8 : 4;
1835 /* Notice that the array stride of a vec3 is not 3 * N but 4 * N.
1836 * See OpenGL 4.30 spec, section 7.6.2.2 "Standard Uniform Block Layout"
1838 * (3) If the member is a three-component vector with components consuming
1839 * <N> basic machine units, the base alignment is 4<N>.
1841 if (this->is_vector() && this->vector_elements
== 3)
1844 /* By default use std430_size(row_major) */
1845 return this->std430_size(row_major
);
1849 glsl_type::std430_size(bool row_major
) const
1851 unsigned N
= is_64bit() ? 8 : 4;
1853 /* OpenGL 4.30 spec, section 7.6.2.2 "Standard Uniform Block Layout":
1855 * "When using the std430 storage layout, shader storage blocks will be
1856 * laid out in buffer storage identically to uniform and shader storage
1857 * blocks using the std140 layout, except that the base alignment and
1858 * stride of arrays of scalars and vectors in rule 4 and of structures
1859 * in rule 9 are not rounded up a multiple of the base alignment of a vec4.
1861 if (this->is_scalar() || this->is_vector())
1862 return this->vector_elements
* N
;
1864 if (this->without_array()->is_matrix()) {
1865 const struct glsl_type
*element_type
;
1866 const struct glsl_type
*vec_type
;
1867 unsigned int array_len
;
1869 if (this->is_array()) {
1870 element_type
= this->without_array();
1871 array_len
= this->arrays_of_arrays_size();
1873 element_type
= this;
1878 vec_type
= get_instance(element_type
->base_type
,
1879 element_type
->matrix_columns
, 1);
1881 array_len
*= element_type
->vector_elements
;
1883 vec_type
= get_instance(element_type
->base_type
,
1884 element_type
->vector_elements
, 1);
1885 array_len
*= element_type
->matrix_columns
;
1887 const glsl_type
*array_type
= glsl_type::get_array_instance(vec_type
,
1890 return array_type
->std430_size(false);
1893 if (this->is_array()) {
1894 if (this->without_array()->is_record())
1895 return this->arrays_of_arrays_size() *
1896 this->without_array()->std430_size(row_major
);
1898 return this->arrays_of_arrays_size() *
1899 this->without_array()->std430_base_alignment(row_major
);
1902 if (this->is_record() || this->is_interface()) {
1904 unsigned max_align
= 0;
1906 for (unsigned i
= 0; i
< this->length
; i
++) {
1907 bool field_row_major
= row_major
;
1908 const enum glsl_matrix_layout matrix_layout
=
1909 glsl_matrix_layout(this->fields
.structure
[i
].matrix_layout
);
1910 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
1911 field_row_major
= true;
1912 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
1913 field_row_major
= false;
1916 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
1917 unsigned align
= field_type
->std430_base_alignment(field_row_major
);
1918 size
= glsl_align(size
, align
);
1919 size
+= field_type
->std430_size(field_row_major
);
1921 max_align
= MAX2(align
, max_align
);
1923 size
= glsl_align(size
, max_align
);
1927 assert(!"not reached");
1932 glsl_type::count_attribute_slots(bool is_vertex_input
) const
1934 /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
1936 * "A scalar input counts the same amount against this limit as a vec4,
1937 * so applications may want to consider packing groups of four
1938 * unrelated float inputs together into a vector to better utilize the
1939 * capabilities of the underlying hardware. A matrix input will use up
1940 * multiple locations. The number of locations used will equal the
1941 * number of columns in the matrix."
1943 * The spec does not explicitly say how arrays are counted. However, it
1944 * should be safe to assume the total number of slots consumed by an array
1945 * is the number of entries in the array multiplied by the number of slots
1946 * consumed by a single element of the array.
1948 * The spec says nothing about how structs are counted, because vertex
1949 * attributes are not allowed to be (or contain) structs. However, Mesa
1950 * allows varying structs, the number of varying slots taken up by a
1951 * varying struct is simply equal to the sum of the number of slots taken
1952 * up by each element.
1954 * Doubles are counted different depending on whether they are vertex
1955 * inputs or everything else. Vertex inputs from ARB_vertex_attrib_64bit
1956 * take one location no matter what size they are, otherwise dvec3/4
1957 * take two locations.
1959 switch (this->base_type
) {
1960 case GLSL_TYPE_UINT
:
1962 case GLSL_TYPE_FLOAT
:
1963 case GLSL_TYPE_BOOL
:
1964 return this->matrix_columns
;
1965 case GLSL_TYPE_DOUBLE
:
1966 case GLSL_TYPE_UINT64
:
1967 case GLSL_TYPE_INT64
:
1968 if (this->vector_elements
> 2 && !is_vertex_input
)
1969 return this->matrix_columns
* 2;
1971 return this->matrix_columns
;
1972 case GLSL_TYPE_STRUCT
:
1973 case GLSL_TYPE_INTERFACE
: {
1976 for (unsigned i
= 0; i
< this->length
; i
++)
1977 size
+= this->fields
.structure
[i
].type
->count_attribute_slots(is_vertex_input
);
1982 case GLSL_TYPE_ARRAY
:
1983 return this->length
* this->fields
.array
->count_attribute_slots(is_vertex_input
);
1985 case GLSL_TYPE_FUNCTION
:
1986 case GLSL_TYPE_SAMPLER
:
1987 case GLSL_TYPE_IMAGE
:
1988 case GLSL_TYPE_ATOMIC_UINT
:
1989 case GLSL_TYPE_VOID
:
1990 case GLSL_TYPE_SUBROUTINE
:
1991 case GLSL_TYPE_ERROR
:
1995 assert(!"Unexpected type in count_attribute_slots()");
2001 glsl_type::coordinate_components() const
2005 switch (sampler_dimensionality
) {
2006 case GLSL_SAMPLER_DIM_1D
:
2007 case GLSL_SAMPLER_DIM_BUF
:
2010 case GLSL_SAMPLER_DIM_2D
:
2011 case GLSL_SAMPLER_DIM_RECT
:
2012 case GLSL_SAMPLER_DIM_MS
:
2013 case GLSL_SAMPLER_DIM_EXTERNAL
:
2014 case GLSL_SAMPLER_DIM_SUBPASS
:
2017 case GLSL_SAMPLER_DIM_3D
:
2018 case GLSL_SAMPLER_DIM_CUBE
:
2022 assert(!"Should not get here.");
2027 /* Array textures need an additional component for the array index, except
2028 * for cubemap array images that behave like a 2D array of interleaved
2031 if (sampler_array
&&
2032 !(is_image() && sampler_dimensionality
== GLSL_SAMPLER_DIM_CUBE
))
2039 * Declarations of type flyweights (glsl_type::_foo_type) and
2040 * convenience pointers (glsl_type::foo_type).
2043 #define DECL_TYPE(NAME, ...) \
2044 const glsl_type glsl_type::_##NAME##_type = glsl_type(__VA_ARGS__, #NAME); \
2045 const glsl_type *const glsl_type::NAME##_type = &glsl_type::_##NAME##_type;
2047 #define STRUCT_TYPE(NAME)
2049 #include "compiler/builtin_type_macros.h"