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
].memory_read_only
944 != b
->fields
.structure
[i
].memory_read_only
)
946 if (this->fields
.structure
[i
].memory_write_only
947 != b
->fields
.structure
[i
].memory_write_only
)
949 if (this->fields
.structure
[i
].memory_coherent
950 != b
->fields
.structure
[i
].memory_coherent
)
952 if (this->fields
.structure
[i
].memory_volatile
953 != b
->fields
.structure
[i
].memory_volatile
)
955 if (this->fields
.structure
[i
].memory_restrict
956 != b
->fields
.structure
[i
].memory_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
:
1298 case GLSL_TYPE_SUBROUTINE
:
1301 case GLSL_TYPE_FUNCTION
:
1302 case GLSL_TYPE_ATOMIC_UINT
:
1303 case GLSL_TYPE_VOID
:
1304 case GLSL_TYPE_ERROR
:
1312 glsl_type::record_location_offset(unsigned length
) const
1314 unsigned offset
= 0;
1315 const glsl_type
*t
= this->without_array();
1316 if (t
->is_record()) {
1317 assert(length
<= t
->length
);
1319 for (unsigned i
= 0; i
< length
; i
++) {
1320 const glsl_type
*st
= t
->fields
.structure
[i
].type
;
1321 const glsl_type
*wa
= st
->without_array();
1322 if (wa
->is_record()) {
1323 unsigned r_offset
= wa
->record_location_offset(wa
->length
);
1324 offset
+= st
->is_array() ?
1325 st
->arrays_of_arrays_size() * r_offset
: r_offset
;
1326 } else if (st
->is_array() && st
->fields
.array
->is_array()) {
1327 unsigned outer_array_size
= st
->length
;
1328 const glsl_type
*base_type
= st
->fields
.array
;
1330 /* For arrays of arrays the outer arrays take up a uniform
1331 * slot for each element. The innermost array elements share a
1332 * single slot so we ignore the innermost array when calculating
1335 while (base_type
->fields
.array
->is_array()) {
1336 outer_array_size
= outer_array_size
* base_type
->length
;
1337 base_type
= base_type
->fields
.array
;
1339 offset
+= outer_array_size
;
1341 /* We dont worry about arrays here because unless the array
1342 * contains a structure or another array it only takes up a single
1353 glsl_type::uniform_locations() const
1357 switch (this->base_type
) {
1358 case GLSL_TYPE_UINT
:
1360 case GLSL_TYPE_FLOAT
:
1361 case GLSL_TYPE_DOUBLE
:
1362 case GLSL_TYPE_UINT64
:
1363 case GLSL_TYPE_INT64
:
1364 case GLSL_TYPE_BOOL
:
1365 case GLSL_TYPE_SAMPLER
:
1366 case GLSL_TYPE_IMAGE
:
1367 case GLSL_TYPE_SUBROUTINE
:
1370 case GLSL_TYPE_STRUCT
:
1371 case GLSL_TYPE_INTERFACE
:
1372 for (unsigned i
= 0; i
< this->length
; i
++)
1373 size
+= this->fields
.structure
[i
].type
->uniform_locations();
1375 case GLSL_TYPE_ARRAY
:
1376 return this->length
* this->fields
.array
->uniform_locations();
1383 glsl_type::varying_count() const
1387 switch (this->base_type
) {
1388 case GLSL_TYPE_UINT
:
1390 case GLSL_TYPE_FLOAT
:
1391 case GLSL_TYPE_DOUBLE
:
1392 case GLSL_TYPE_BOOL
:
1393 case GLSL_TYPE_UINT64
:
1394 case GLSL_TYPE_INT64
:
1397 case GLSL_TYPE_STRUCT
:
1398 case GLSL_TYPE_INTERFACE
:
1399 for (unsigned i
= 0; i
< this->length
; i
++)
1400 size
+= this->fields
.structure
[i
].type
->varying_count();
1402 case GLSL_TYPE_ARRAY
:
1403 /* Don't count innermost array elements */
1404 if (this->without_array()->is_record() ||
1405 this->without_array()->is_interface() ||
1406 this->fields
.array
->is_array())
1407 return this->length
* this->fields
.array
->varying_count();
1409 return this->fields
.array
->varying_count();
1411 assert(!"unsupported varying type");
1417 glsl_type::can_implicitly_convert_to(const glsl_type
*desired
,
1418 _mesa_glsl_parse_state
*state
) const
1420 if (this == desired
)
1423 /* GLSL 1.10 and ESSL do not allow implicit conversions. If there is no
1424 * state, we're doing intra-stage function linking where these checks have
1425 * already been done.
1427 if (state
&& (state
->es_shader
|| !state
->is_version(120, 0)))
1430 /* There is no conversion among matrix types. */
1431 if (this->matrix_columns
> 1 || desired
->matrix_columns
> 1)
1434 /* Vector size must match. */
1435 if (this->vector_elements
!= desired
->vector_elements
)
1438 /* int and uint can be converted to float. */
1439 if (desired
->is_float() && this->is_integer())
1442 /* With GLSL 4.0, ARB_gpu_shader5, or MESA_shader_integer_functions, int
1443 * can be converted to uint. Note that state may be NULL here, when
1444 * resolving function calls in the linker. By this time, all the
1445 * state-dependent checks have already happened though, so allow anything
1446 * that's allowed in any shader version.
1448 if ((!state
|| state
->is_version(400, 0) || state
->ARB_gpu_shader5_enable
||
1449 state
->MESA_shader_integer_functions_enable
) &&
1450 desired
->base_type
== GLSL_TYPE_UINT
&& this->base_type
== GLSL_TYPE_INT
)
1453 /* No implicit conversions from double. */
1454 if ((!state
|| state
->has_double()) && this->is_double())
1457 /* Conversions from different types to double. */
1458 if ((!state
|| state
->has_double()) && desired
->is_double()) {
1459 if (this->is_float())
1461 if (this->is_integer())
1469 glsl_type::std140_base_alignment(bool row_major
) const
1471 unsigned N
= is_64bit() ? 8 : 4;
1473 /* (1) If the member is a scalar consuming <N> basic machine units, the
1474 * base alignment is <N>.
1476 * (2) If the member is a two- or four-component vector with components
1477 * consuming <N> basic machine units, the base alignment is 2<N> or
1478 * 4<N>, respectively.
1480 * (3) If the member is a three-component vector with components consuming
1481 * <N> basic machine units, the base alignment is 4<N>.
1483 if (this->is_scalar() || this->is_vector()) {
1484 switch (this->vector_elements
) {
1495 /* (4) If the member is an array of scalars or vectors, the base alignment
1496 * and array stride are set to match the base alignment of a single
1497 * array element, according to rules (1), (2), and (3), and rounded up
1498 * to the base alignment of a vec4. The array may have padding at the
1499 * end; the base offset of the member following the array is rounded up
1500 * to the next multiple of the base alignment.
1502 * (6) If the member is an array of <S> column-major matrices with <C>
1503 * columns and <R> rows, the matrix is stored identically to a row of
1504 * <S>*<C> column vectors with <R> components each, according to rule
1507 * (8) If the member is an array of <S> row-major matrices with <C> columns
1508 * and <R> rows, the matrix is stored identically to a row of <S>*<R>
1509 * row vectors with <C> components each, according to rule (4).
1511 * (10) If the member is an array of <S> structures, the <S> elements of
1512 * the array are laid out in order, according to rule (9).
1514 if (this->is_array()) {
1515 if (this->fields
.array
->is_scalar() ||
1516 this->fields
.array
->is_vector() ||
1517 this->fields
.array
->is_matrix()) {
1518 return MAX2(this->fields
.array
->std140_base_alignment(row_major
), 16);
1520 assert(this->fields
.array
->is_record() ||
1521 this->fields
.array
->is_array());
1522 return this->fields
.array
->std140_base_alignment(row_major
);
1526 /* (5) If the member is a column-major matrix with <C> columns and
1527 * <R> rows, the matrix is stored identically to an array of
1528 * <C> column vectors with <R> components each, according to
1531 * (7) If the member is a row-major matrix with <C> columns and <R>
1532 * rows, the matrix is stored identically to an array of <R>
1533 * row vectors with <C> components each, according to rule (4).
1535 if (this->is_matrix()) {
1536 const struct glsl_type
*vec_type
, *array_type
;
1537 int c
= this->matrix_columns
;
1538 int r
= this->vector_elements
;
1541 vec_type
= get_instance(base_type
, c
, 1);
1542 array_type
= glsl_type::get_array_instance(vec_type
, r
);
1544 vec_type
= get_instance(base_type
, r
, 1);
1545 array_type
= glsl_type::get_array_instance(vec_type
, c
);
1548 return array_type
->std140_base_alignment(false);
1551 /* (9) If the member is a structure, the base alignment of the
1552 * structure is <N>, where <N> is the largest base alignment
1553 * value of any of its members, and rounded up to the base
1554 * alignment of a vec4. The individual members of this
1555 * sub-structure are then assigned offsets by applying this set
1556 * of rules recursively, where the base offset of the first
1557 * member of the sub-structure is equal to the aligned offset
1558 * of the structure. The structure may have padding at the end;
1559 * the base offset of the member following the sub-structure is
1560 * rounded up to the next multiple of the base alignment of the
1563 if (this->is_record()) {
1564 unsigned base_alignment
= 16;
1565 for (unsigned i
= 0; i
< this->length
; i
++) {
1566 bool field_row_major
= row_major
;
1567 const enum glsl_matrix_layout matrix_layout
=
1568 glsl_matrix_layout(this->fields
.structure
[i
].matrix_layout
);
1569 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
1570 field_row_major
= true;
1571 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
1572 field_row_major
= false;
1575 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
1576 base_alignment
= MAX2(base_alignment
,
1577 field_type
->std140_base_alignment(field_row_major
));
1579 return base_alignment
;
1582 assert(!"not reached");
1587 glsl_type::std140_size(bool row_major
) const
1589 unsigned N
= is_64bit() ? 8 : 4;
1591 /* (1) If the member is a scalar consuming <N> basic machine units, the
1592 * base alignment is <N>.
1594 * (2) If the member is a two- or four-component vector with components
1595 * consuming <N> basic machine units, the base alignment is 2<N> or
1596 * 4<N>, respectively.
1598 * (3) If the member is a three-component vector with components consuming
1599 * <N> basic machine units, the base alignment is 4<N>.
1601 if (this->is_scalar() || this->is_vector()) {
1602 return this->vector_elements
* N
;
1605 /* (5) If the member is a column-major matrix with <C> columns and
1606 * <R> rows, the matrix is stored identically to an array of
1607 * <C> column vectors with <R> components each, according to
1610 * (6) If the member is an array of <S> column-major matrices with <C>
1611 * columns and <R> rows, the matrix is stored identically to a row of
1612 * <S>*<C> column vectors with <R> components each, according to rule
1615 * (7) If the member is a row-major matrix with <C> columns and <R>
1616 * rows, the matrix is stored identically to an array of <R>
1617 * row vectors with <C> components each, according to rule (4).
1619 * (8) If the member is an array of <S> row-major matrices with <C> columns
1620 * and <R> rows, the matrix is stored identically to a row of <S>*<R>
1621 * row vectors with <C> components each, according to rule (4).
1623 if (this->without_array()->is_matrix()) {
1624 const struct glsl_type
*element_type
;
1625 const struct glsl_type
*vec_type
;
1626 unsigned int array_len
;
1628 if (this->is_array()) {
1629 element_type
= this->without_array();
1630 array_len
= this->arrays_of_arrays_size();
1632 element_type
= this;
1637 vec_type
= get_instance(element_type
->base_type
,
1638 element_type
->matrix_columns
, 1);
1640 array_len
*= element_type
->vector_elements
;
1642 vec_type
= get_instance(element_type
->base_type
,
1643 element_type
->vector_elements
, 1);
1644 array_len
*= element_type
->matrix_columns
;
1646 const glsl_type
*array_type
= glsl_type::get_array_instance(vec_type
,
1649 return array_type
->std140_size(false);
1652 /* (4) If the member is an array of scalars or vectors, the base alignment
1653 * and array stride are set to match the base alignment of a single
1654 * array element, according to rules (1), (2), and (3), and rounded up
1655 * to the base alignment of a vec4. The array may have padding at the
1656 * end; the base offset of the member following the array is rounded up
1657 * to the next multiple of the base alignment.
1659 * (10) If the member is an array of <S> structures, the <S> elements of
1660 * the array are laid out in order, according to rule (9).
1662 if (this->is_array()) {
1663 if (this->without_array()->is_record()) {
1664 return this->arrays_of_arrays_size() *
1665 this->without_array()->std140_size(row_major
);
1667 unsigned element_base_align
=
1668 this->without_array()->std140_base_alignment(row_major
);
1669 return this->arrays_of_arrays_size() * MAX2(element_base_align
, 16);
1673 /* (9) If the member is a structure, the base alignment of the
1674 * structure is <N>, where <N> is the largest base alignment
1675 * value of any of its members, and rounded up to the base
1676 * alignment of a vec4. The individual members of this
1677 * sub-structure are then assigned offsets by applying this set
1678 * of rules recursively, where the base offset of the first
1679 * member of the sub-structure is equal to the aligned offset
1680 * of the structure. The structure may have padding at the end;
1681 * the base offset of the member following the sub-structure is
1682 * rounded up to the next multiple of the base alignment of the
1685 if (this->is_record() || this->is_interface()) {
1687 unsigned max_align
= 0;
1689 for (unsigned i
= 0; i
< this->length
; i
++) {
1690 bool field_row_major
= row_major
;
1691 const enum glsl_matrix_layout matrix_layout
=
1692 glsl_matrix_layout(this->fields
.structure
[i
].matrix_layout
);
1693 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
1694 field_row_major
= true;
1695 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
1696 field_row_major
= false;
1699 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
1700 unsigned align
= field_type
->std140_base_alignment(field_row_major
);
1702 /* Ignore unsized arrays when calculating size */
1703 if (field_type
->is_unsized_array())
1706 size
= glsl_align(size
, align
);
1707 size
+= field_type
->std140_size(field_row_major
);
1709 max_align
= MAX2(align
, max_align
);
1711 if (field_type
->is_record() && (i
+ 1 < this->length
))
1712 size
= glsl_align(size
, 16);
1714 size
= glsl_align(size
, MAX2(max_align
, 16));
1718 assert(!"not reached");
1723 glsl_type::std430_base_alignment(bool row_major
) const
1726 unsigned N
= is_64bit() ? 8 : 4;
1728 /* (1) If the member is a scalar consuming <N> basic machine units, the
1729 * base alignment is <N>.
1731 * (2) If the member is a two- or four-component vector with components
1732 * consuming <N> basic machine units, the base alignment is 2<N> or
1733 * 4<N>, respectively.
1735 * (3) If the member is a three-component vector with components consuming
1736 * <N> basic machine units, the base alignment is 4<N>.
1738 if (this->is_scalar() || this->is_vector()) {
1739 switch (this->vector_elements
) {
1750 /* OpenGL 4.30 spec, section 7.6.2.2 "Standard Uniform Block Layout":
1752 * "When using the std430 storage layout, shader storage blocks will be
1753 * laid out in buffer storage identically to uniform and shader storage
1754 * blocks using the std140 layout, except that the base alignment and
1755 * stride of arrays of scalars and vectors in rule 4 and of structures
1756 * in rule 9 are not rounded up a multiple of the base alignment of a vec4.
1759 /* (1) If the member is a scalar consuming <N> basic machine units, the
1760 * base alignment is <N>.
1762 * (2) If the member is a two- or four-component vector with components
1763 * consuming <N> basic machine units, the base alignment is 2<N> or
1764 * 4<N>, respectively.
1766 * (3) If the member is a three-component vector with components consuming
1767 * <N> basic machine units, the base alignment is 4<N>.
1769 if (this->is_array())
1770 return this->fields
.array
->std430_base_alignment(row_major
);
1772 /* (5) If the member is a column-major matrix with <C> columns and
1773 * <R> rows, the matrix is stored identically to an array of
1774 * <C> column vectors with <R> components each, according to
1777 * (7) If the member is a row-major matrix with <C> columns and <R>
1778 * rows, the matrix is stored identically to an array of <R>
1779 * row vectors with <C> components each, according to rule (4).
1781 if (this->is_matrix()) {
1782 const struct glsl_type
*vec_type
, *array_type
;
1783 int c
= this->matrix_columns
;
1784 int r
= this->vector_elements
;
1787 vec_type
= get_instance(base_type
, c
, 1);
1788 array_type
= glsl_type::get_array_instance(vec_type
, r
);
1790 vec_type
= get_instance(base_type
, r
, 1);
1791 array_type
= glsl_type::get_array_instance(vec_type
, c
);
1794 return array_type
->std430_base_alignment(false);
1797 /* (9) If the member is a structure, the base alignment of the
1798 * structure is <N>, where <N> is the largest base alignment
1799 * value of any of its members, and rounded up to the base
1800 * alignment of a vec4. The individual members of this
1801 * sub-structure are then assigned offsets by applying this set
1802 * of rules recursively, where the base offset of the first
1803 * member of the sub-structure is equal to the aligned offset
1804 * of the structure. The structure may have padding at the end;
1805 * the base offset of the member following the sub-structure is
1806 * rounded up to the next multiple of the base alignment of the
1809 if (this->is_record()) {
1810 unsigned base_alignment
= 0;
1811 for (unsigned i
= 0; i
< this->length
; i
++) {
1812 bool field_row_major
= row_major
;
1813 const enum glsl_matrix_layout matrix_layout
=
1814 glsl_matrix_layout(this->fields
.structure
[i
].matrix_layout
);
1815 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
1816 field_row_major
= true;
1817 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
1818 field_row_major
= false;
1821 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
1822 base_alignment
= MAX2(base_alignment
,
1823 field_type
->std430_base_alignment(field_row_major
));
1825 assert(base_alignment
> 0);
1826 return base_alignment
;
1828 assert(!"not reached");
1833 glsl_type::std430_array_stride(bool row_major
) const
1835 unsigned N
= is_64bit() ? 8 : 4;
1837 /* Notice that the array stride of a vec3 is not 3 * N but 4 * N.
1838 * See OpenGL 4.30 spec, section 7.6.2.2 "Standard Uniform Block Layout"
1840 * (3) If the member is a three-component vector with components consuming
1841 * <N> basic machine units, the base alignment is 4<N>.
1843 if (this->is_vector() && this->vector_elements
== 3)
1846 /* By default use std430_size(row_major) */
1847 return this->std430_size(row_major
);
1851 glsl_type::std430_size(bool row_major
) const
1853 unsigned N
= is_64bit() ? 8 : 4;
1855 /* OpenGL 4.30 spec, section 7.6.2.2 "Standard Uniform Block Layout":
1857 * "When using the std430 storage layout, shader storage blocks will be
1858 * laid out in buffer storage identically to uniform and shader storage
1859 * blocks using the std140 layout, except that the base alignment and
1860 * stride of arrays of scalars and vectors in rule 4 and of structures
1861 * in rule 9 are not rounded up a multiple of the base alignment of a vec4.
1863 if (this->is_scalar() || this->is_vector())
1864 return this->vector_elements
* N
;
1866 if (this->without_array()->is_matrix()) {
1867 const struct glsl_type
*element_type
;
1868 const struct glsl_type
*vec_type
;
1869 unsigned int array_len
;
1871 if (this->is_array()) {
1872 element_type
= this->without_array();
1873 array_len
= this->arrays_of_arrays_size();
1875 element_type
= this;
1880 vec_type
= get_instance(element_type
->base_type
,
1881 element_type
->matrix_columns
, 1);
1883 array_len
*= element_type
->vector_elements
;
1885 vec_type
= get_instance(element_type
->base_type
,
1886 element_type
->vector_elements
, 1);
1887 array_len
*= element_type
->matrix_columns
;
1889 const glsl_type
*array_type
= glsl_type::get_array_instance(vec_type
,
1892 return array_type
->std430_size(false);
1895 if (this->is_array()) {
1896 if (this->without_array()->is_record())
1897 return this->arrays_of_arrays_size() *
1898 this->without_array()->std430_size(row_major
);
1900 return this->arrays_of_arrays_size() *
1901 this->without_array()->std430_base_alignment(row_major
);
1904 if (this->is_record() || this->is_interface()) {
1906 unsigned max_align
= 0;
1908 for (unsigned i
= 0; i
< this->length
; i
++) {
1909 bool field_row_major
= row_major
;
1910 const enum glsl_matrix_layout matrix_layout
=
1911 glsl_matrix_layout(this->fields
.structure
[i
].matrix_layout
);
1912 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
1913 field_row_major
= true;
1914 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
1915 field_row_major
= false;
1918 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
1919 unsigned align
= field_type
->std430_base_alignment(field_row_major
);
1920 size
= glsl_align(size
, align
);
1921 size
+= field_type
->std430_size(field_row_major
);
1923 max_align
= MAX2(align
, max_align
);
1925 size
= glsl_align(size
, max_align
);
1929 assert(!"not reached");
1934 glsl_type::count_attribute_slots(bool is_vertex_input
) const
1936 /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
1938 * "A scalar input counts the same amount against this limit as a vec4,
1939 * so applications may want to consider packing groups of four
1940 * unrelated float inputs together into a vector to better utilize the
1941 * capabilities of the underlying hardware. A matrix input will use up
1942 * multiple locations. The number of locations used will equal the
1943 * number of columns in the matrix."
1945 * The spec does not explicitly say how arrays are counted. However, it
1946 * should be safe to assume the total number of slots consumed by an array
1947 * is the number of entries in the array multiplied by the number of slots
1948 * consumed by a single element of the array.
1950 * The spec says nothing about how structs are counted, because vertex
1951 * attributes are not allowed to be (or contain) structs. However, Mesa
1952 * allows varying structs, the number of varying slots taken up by a
1953 * varying struct is simply equal to the sum of the number of slots taken
1954 * up by each element.
1956 * Doubles are counted different depending on whether they are vertex
1957 * inputs or everything else. Vertex inputs from ARB_vertex_attrib_64bit
1958 * take one location no matter what size they are, otherwise dvec3/4
1959 * take two locations.
1961 switch (this->base_type
) {
1962 case GLSL_TYPE_UINT
:
1964 case GLSL_TYPE_FLOAT
:
1965 case GLSL_TYPE_BOOL
:
1966 case GLSL_TYPE_SAMPLER
:
1967 case GLSL_TYPE_IMAGE
:
1968 return this->matrix_columns
;
1969 case GLSL_TYPE_DOUBLE
:
1970 case GLSL_TYPE_UINT64
:
1971 case GLSL_TYPE_INT64
:
1972 if (this->vector_elements
> 2 && !is_vertex_input
)
1973 return this->matrix_columns
* 2;
1975 return this->matrix_columns
;
1976 case GLSL_TYPE_STRUCT
:
1977 case GLSL_TYPE_INTERFACE
: {
1980 for (unsigned i
= 0; i
< this->length
; i
++)
1981 size
+= this->fields
.structure
[i
].type
->count_attribute_slots(is_vertex_input
);
1986 case GLSL_TYPE_ARRAY
:
1987 return this->length
* this->fields
.array
->count_attribute_slots(is_vertex_input
);
1989 case GLSL_TYPE_FUNCTION
:
1990 case GLSL_TYPE_ATOMIC_UINT
:
1991 case GLSL_TYPE_VOID
:
1992 case GLSL_TYPE_SUBROUTINE
:
1993 case GLSL_TYPE_ERROR
:
1997 assert(!"Unexpected type in count_attribute_slots()");
2003 glsl_type::coordinate_components() const
2007 switch (sampler_dimensionality
) {
2008 case GLSL_SAMPLER_DIM_1D
:
2009 case GLSL_SAMPLER_DIM_BUF
:
2012 case GLSL_SAMPLER_DIM_2D
:
2013 case GLSL_SAMPLER_DIM_RECT
:
2014 case GLSL_SAMPLER_DIM_MS
:
2015 case GLSL_SAMPLER_DIM_EXTERNAL
:
2016 case GLSL_SAMPLER_DIM_SUBPASS
:
2019 case GLSL_SAMPLER_DIM_3D
:
2020 case GLSL_SAMPLER_DIM_CUBE
:
2024 assert(!"Should not get here.");
2029 /* Array textures need an additional component for the array index, except
2030 * for cubemap array images that behave like a 2D array of interleaved
2033 if (sampler_array
&&
2034 !(is_image() && sampler_dimensionality
== GLSL_SAMPLER_DIM_CUBE
))
2041 * Declarations of type flyweights (glsl_type::_foo_type) and
2042 * convenience pointers (glsl_type::foo_type).
2045 #define DECL_TYPE(NAME, ...) \
2046 const glsl_type glsl_type::_##NAME##_type = glsl_type(__VA_ARGS__, #NAME); \
2047 const glsl_type *const glsl_type::NAME##_type = &glsl_type::_##NAME##_type;
2049 #define STRUCT_TYPE(NAME)
2051 #include "compiler/builtin_type_macros.h"