2 * Copyright © 2009 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
25 #include "main/macros.h"
26 #include "compiler/glsl/glsl_parser_extras.h"
27 #include "glsl_types.h"
28 #include "util/hash_table.h"
31 mtx_t
glsl_type::mutex
= _MTX_INITIALIZER_NP
;
32 hash_table
*glsl_type::array_types
= NULL
;
33 hash_table
*glsl_type::record_types
= NULL
;
34 hash_table
*glsl_type::interface_types
= NULL
;
35 hash_table
*glsl_type::function_types
= NULL
;
36 hash_table
*glsl_type::subroutine_types
= NULL
;
37 void *glsl_type::mem_ctx
= NULL
;
40 glsl_type::init_ralloc_type_ctx(void)
42 if (glsl_type::mem_ctx
== NULL
) {
43 glsl_type::mem_ctx
= ralloc_autofree_context();
44 assert(glsl_type::mem_ctx
!= NULL
);
48 glsl_type::glsl_type(GLenum gl_type
,
49 glsl_base_type base_type
, unsigned vector_elements
,
50 unsigned matrix_columns
, const char *name
) :
53 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
54 sampled_type(0), interface_packing(0), interface_row_major(0),
55 vector_elements(vector_elements
), matrix_columns(matrix_columns
),
58 /* Values of these types must fit in the two bits of
59 * glsl_type::sampled_type.
61 STATIC_ASSERT((unsigned(GLSL_TYPE_UINT
) & 3) == unsigned(GLSL_TYPE_UINT
));
62 STATIC_ASSERT((unsigned(GLSL_TYPE_INT
) & 3) == unsigned(GLSL_TYPE_INT
));
63 STATIC_ASSERT((unsigned(GLSL_TYPE_FLOAT
) & 3) == unsigned(GLSL_TYPE_FLOAT
));
65 mtx_lock(&glsl_type::mutex
);
67 init_ralloc_type_ctx();
69 this->name
= ralloc_strdup(this->mem_ctx
, name
);
71 mtx_unlock(&glsl_type::mutex
);
73 /* Neither dimension is zero or both dimensions are zero.
75 assert((vector_elements
== 0) == (matrix_columns
== 0));
76 memset(& fields
, 0, sizeof(fields
));
79 glsl_type::glsl_type(GLenum gl_type
, glsl_base_type base_type
,
80 enum glsl_sampler_dim dim
, bool shadow
, bool array
,
81 unsigned type
, const char *name
) :
84 sampler_dimensionality(dim
), sampler_shadow(shadow
),
85 sampler_array(array
), sampled_type(type
), interface_packing(0),
86 interface_row_major(0), length(0)
88 mtx_lock(&glsl_type::mutex
);
90 init_ralloc_type_ctx();
92 this->name
= ralloc_strdup(this->mem_ctx
, name
);
94 mtx_unlock(&glsl_type::mutex
);
96 memset(& fields
, 0, sizeof(fields
));
98 if (base_type
== GLSL_TYPE_SAMPLER
) {
99 /* Samplers take no storage whatsoever. */
100 matrix_columns
= vector_elements
= 0;
102 matrix_columns
= vector_elements
= 1;
106 glsl_type::glsl_type(const glsl_struct_field
*fields
, unsigned num_fields
,
109 base_type(GLSL_TYPE_STRUCT
),
110 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
111 sampled_type(0), interface_packing(0), interface_row_major(0),
112 vector_elements(0), matrix_columns(0),
117 mtx_lock(&glsl_type::mutex
);
119 init_ralloc_type_ctx();
120 assert(name
!= NULL
);
121 this->name
= ralloc_strdup(this->mem_ctx
, name
);
122 this->fields
.structure
= ralloc_array(this->mem_ctx
,
123 glsl_struct_field
, length
);
125 for (i
= 0; i
< length
; i
++) {
126 this->fields
.structure
[i
] = fields
[i
];
127 this->fields
.structure
[i
].name
= ralloc_strdup(this->fields
.structure
,
131 mtx_unlock(&glsl_type::mutex
);
134 glsl_type::glsl_type(const glsl_struct_field
*fields
, unsigned num_fields
,
135 enum glsl_interface_packing packing
,
136 bool row_major
, const char *name
) :
138 base_type(GLSL_TYPE_INTERFACE
),
139 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
140 sampled_type(0), interface_packing((unsigned) packing
),
141 interface_row_major((unsigned) row_major
),
142 vector_elements(0), matrix_columns(0),
147 mtx_lock(&glsl_type::mutex
);
149 init_ralloc_type_ctx();
150 assert(name
!= NULL
);
151 this->name
= ralloc_strdup(this->mem_ctx
, name
);
152 this->fields
.structure
= rzalloc_array(this->mem_ctx
,
153 glsl_struct_field
, length
);
154 for (i
= 0; i
< length
; i
++) {
155 this->fields
.structure
[i
] = fields
[i
];
156 this->fields
.structure
[i
].name
= ralloc_strdup(this->fields
.structure
,
160 mtx_unlock(&glsl_type::mutex
);
163 glsl_type::glsl_type(const glsl_type
*return_type
,
164 const glsl_function_param
*params
, unsigned num_params
) :
166 base_type(GLSL_TYPE_FUNCTION
),
167 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
168 sampled_type(0), interface_packing(0), interface_row_major(0),
169 vector_elements(0), matrix_columns(0),
174 mtx_lock(&glsl_type::mutex
);
176 init_ralloc_type_ctx();
178 this->fields
.parameters
= rzalloc_array(this->mem_ctx
,
179 glsl_function_param
, num_params
+ 1);
181 /* We store the return type as the first parameter */
182 this->fields
.parameters
[0].type
= return_type
;
183 this->fields
.parameters
[0].in
= false;
184 this->fields
.parameters
[0].out
= true;
186 /* We store the i'th parameter in slot i+1 */
187 for (i
= 0; i
< length
; i
++) {
188 this->fields
.parameters
[i
+ 1].type
= params
[i
].type
;
189 this->fields
.parameters
[i
+ 1].in
= params
[i
].in
;
190 this->fields
.parameters
[i
+ 1].out
= params
[i
].out
;
193 mtx_unlock(&glsl_type::mutex
);
196 glsl_type::glsl_type(const char *subroutine_name
) :
198 base_type(GLSL_TYPE_SUBROUTINE
),
199 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
200 sampled_type(0), interface_packing(0), interface_row_major(0),
201 vector_elements(1), matrix_columns(1),
204 mtx_lock(&glsl_type::mutex
);
206 init_ralloc_type_ctx();
207 assert(subroutine_name
!= NULL
);
208 this->name
= ralloc_strdup(this->mem_ctx
, subroutine_name
);
209 mtx_unlock(&glsl_type::mutex
);
213 glsl_type::contains_sampler() const
215 if (this->is_array()) {
216 return this->fields
.array
->contains_sampler();
217 } else if (this->is_record() || this->is_interface()) {
218 for (unsigned int i
= 0; i
< this->length
; i
++) {
219 if (this->fields
.structure
[i
].type
->contains_sampler())
224 return this->is_sampler();
230 glsl_type::contains_integer() const
232 if (this->is_array()) {
233 return this->fields
.array
->contains_integer();
234 } else if (this->is_record() || this->is_interface()) {
235 for (unsigned int i
= 0; i
< this->length
; i
++) {
236 if (this->fields
.structure
[i
].type
->contains_integer())
241 return this->is_integer();
246 glsl_type::contains_double() const
248 if (this->is_array()) {
249 return this->fields
.array
->contains_double();
250 } else if (this->is_record() || this->is_interface()) {
251 for (unsigned int i
= 0; i
< this->length
; i
++) {
252 if (this->fields
.structure
[i
].type
->contains_double())
257 return this->is_double();
262 glsl_type::contains_opaque() const {
264 case GLSL_TYPE_SAMPLER
:
265 case GLSL_TYPE_IMAGE
:
266 case GLSL_TYPE_ATOMIC_UINT
:
268 case GLSL_TYPE_ARRAY
:
269 return fields
.array
->contains_opaque();
270 case GLSL_TYPE_STRUCT
:
271 case GLSL_TYPE_INTERFACE
:
272 for (unsigned int i
= 0; i
< length
; i
++) {
273 if (fields
.structure
[i
].type
->contains_opaque())
283 glsl_type::contains_subroutine() const
285 if (this->is_array()) {
286 return this->fields
.array
->contains_subroutine();
287 } else if (this->is_record() || this->is_interface()) {
288 for (unsigned int i
= 0; i
< this->length
; i
++) {
289 if (this->fields
.structure
[i
].type
->contains_subroutine())
294 return this->is_subroutine();
299 glsl_type::sampler_index() const
301 const glsl_type
*const t
= (this->is_array()) ? this->fields
.array
: this;
303 assert(t
->is_sampler());
305 switch (t
->sampler_dimensionality
) {
306 case GLSL_SAMPLER_DIM_1D
:
307 return (t
->sampler_array
) ? TEXTURE_1D_ARRAY_INDEX
: TEXTURE_1D_INDEX
;
308 case GLSL_SAMPLER_DIM_2D
:
309 return (t
->sampler_array
) ? TEXTURE_2D_ARRAY_INDEX
: TEXTURE_2D_INDEX
;
310 case GLSL_SAMPLER_DIM_3D
:
311 return TEXTURE_3D_INDEX
;
312 case GLSL_SAMPLER_DIM_CUBE
:
313 return (t
->sampler_array
) ? TEXTURE_CUBE_ARRAY_INDEX
: TEXTURE_CUBE_INDEX
;
314 case GLSL_SAMPLER_DIM_RECT
:
315 return TEXTURE_RECT_INDEX
;
316 case GLSL_SAMPLER_DIM_BUF
:
317 return TEXTURE_BUFFER_INDEX
;
318 case GLSL_SAMPLER_DIM_EXTERNAL
:
319 return TEXTURE_EXTERNAL_INDEX
;
320 case GLSL_SAMPLER_DIM_MS
:
321 return (t
->sampler_array
) ? TEXTURE_2D_MULTISAMPLE_ARRAY_INDEX
: TEXTURE_2D_MULTISAMPLE_INDEX
;
323 assert(!"Should not get here.");
324 return TEXTURE_BUFFER_INDEX
;
329 glsl_type::contains_image() const
331 if (this->is_array()) {
332 return this->fields
.array
->contains_image();
333 } else if (this->is_record() || this->is_interface()) {
334 for (unsigned int i
= 0; i
< this->length
; i
++) {
335 if (this->fields
.structure
[i
].type
->contains_image())
340 return this->is_image();
344 const glsl_type
*glsl_type::get_base_type() const
351 case GLSL_TYPE_FLOAT
:
353 case GLSL_TYPE_DOUBLE
:
363 const glsl_type
*glsl_type::get_scalar_type() const
365 const glsl_type
*type
= this;
368 while (type
->base_type
== GLSL_TYPE_ARRAY
)
369 type
= type
->fields
.array
;
371 /* Handle vectors and matrices */
372 switch (type
->base_type
) {
377 case GLSL_TYPE_FLOAT
:
379 case GLSL_TYPE_DOUBLE
:
384 /* Handle everything else */
391 _mesa_glsl_release_types(void)
393 /* Should only be called during atexit (either when unloading shared
394 * object, or if process terminates), so no mutex-locking should be
397 if (glsl_type::array_types
!= NULL
) {
398 _mesa_hash_table_destroy(glsl_type::array_types
, NULL
);
399 glsl_type::array_types
= NULL
;
402 if (glsl_type::record_types
!= NULL
) {
403 _mesa_hash_table_destroy(glsl_type::record_types
, NULL
);
404 glsl_type::record_types
= NULL
;
407 if (glsl_type::interface_types
!= NULL
) {
408 _mesa_hash_table_destroy(glsl_type::interface_types
, NULL
);
409 glsl_type::interface_types
= NULL
;
414 glsl_type::glsl_type(const glsl_type
*array
, unsigned length
) :
415 base_type(GLSL_TYPE_ARRAY
),
416 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
417 sampled_type(0), interface_packing(0), interface_row_major(0),
418 vector_elements(0), matrix_columns(0),
419 length(length
), name(NULL
)
421 this->fields
.array
= array
;
422 /* Inherit the gl type of the base. The GL type is used for
423 * uniform/statevar handling in Mesa and the arrayness of the type
424 * is represented by the size rather than the type.
426 this->gl_type
= array
->gl_type
;
428 /* Allow a maximum of 10 characters for the array size. This is enough
429 * for 32-bits of ~0. The extra 3 are for the '[', ']', and terminating
432 const unsigned name_length
= strlen(array
->name
) + 10 + 3;
434 mtx_lock(&glsl_type::mutex
);
435 char *const n
= (char *) ralloc_size(this->mem_ctx
, name_length
);
436 mtx_unlock(&glsl_type::mutex
);
439 snprintf(n
, name_length
, "%s[]", array
->name
);
441 /* insert outermost dimensions in the correct spot
442 * otherwise the dimension order will be backwards
444 const char *pos
= strchr(array
->name
, '[');
446 int idx
= pos
- array
->name
;
447 snprintf(n
, idx
+1, "%s", array
->name
);
448 snprintf(n
+ idx
, name_length
- idx
, "[%u]%s",
449 length
, array
->name
+ idx
);
451 snprintf(n
, name_length
, "%s[%u]", array
->name
, length
);
460 glsl_type::vec(unsigned components
)
462 if (components
== 0 || components
> 4)
465 static const glsl_type
*const ts
[] = {
466 float_type
, vec2_type
, vec3_type
, vec4_type
468 return ts
[components
- 1];
472 glsl_type::dvec(unsigned components
)
474 if (components
== 0 || components
> 4)
477 static const glsl_type
*const ts
[] = {
478 double_type
, dvec2_type
, dvec3_type
, dvec4_type
480 return ts
[components
- 1];
484 glsl_type::ivec(unsigned components
)
486 if (components
== 0 || components
> 4)
489 static const glsl_type
*const ts
[] = {
490 int_type
, ivec2_type
, ivec3_type
, ivec4_type
492 return ts
[components
- 1];
497 glsl_type::uvec(unsigned components
)
499 if (components
== 0 || components
> 4)
502 static const glsl_type
*const ts
[] = {
503 uint_type
, uvec2_type
, uvec3_type
, uvec4_type
505 return ts
[components
- 1];
510 glsl_type::bvec(unsigned components
)
512 if (components
== 0 || components
> 4)
515 static const glsl_type
*const ts
[] = {
516 bool_type
, bvec2_type
, bvec3_type
, bvec4_type
518 return ts
[components
- 1];
523 glsl_type::get_instance(unsigned base_type
, unsigned rows
, unsigned columns
)
525 if (base_type
== GLSL_TYPE_VOID
)
528 if ((rows
< 1) || (rows
> 4) || (columns
< 1) || (columns
> 4))
531 /* Treat GLSL vectors as Nx1 matrices.
539 case GLSL_TYPE_FLOAT
:
541 case GLSL_TYPE_DOUBLE
:
549 if ((base_type
!= GLSL_TYPE_FLOAT
&& base_type
!= GLSL_TYPE_DOUBLE
) || (rows
== 1))
552 /* GLSL matrix types are named mat{COLUMNS}x{ROWS}. Only the following
553 * combinations are valid:
561 #define IDX(c,r) (((c-1)*3) + (r-1))
563 if (base_type
== GLSL_TYPE_DOUBLE
) {
564 switch (IDX(columns
, rows
)) {
565 case IDX(2,2): return dmat2_type
;
566 case IDX(2,3): return dmat2x3_type
;
567 case IDX(2,4): return dmat2x4_type
;
568 case IDX(3,2): return dmat3x2_type
;
569 case IDX(3,3): return dmat3_type
;
570 case IDX(3,4): return dmat3x4_type
;
571 case IDX(4,2): return dmat4x2_type
;
572 case IDX(4,3): return dmat4x3_type
;
573 case IDX(4,4): return dmat4_type
;
574 default: return error_type
;
577 switch (IDX(columns
, rows
)) {
578 case IDX(2,2): return mat2_type
;
579 case IDX(2,3): return mat2x3_type
;
580 case IDX(2,4): return mat2x4_type
;
581 case IDX(3,2): return mat3x2_type
;
582 case IDX(3,3): return mat3_type
;
583 case IDX(3,4): return mat3x4_type
;
584 case IDX(4,2): return mat4x2_type
;
585 case IDX(4,3): return mat4x3_type
;
586 case IDX(4,4): return mat4_type
;
587 default: return error_type
;
592 assert(!"Should not get here.");
597 glsl_type::get_sampler_instance(enum glsl_sampler_dim dim
,
603 case GLSL_TYPE_FLOAT
:
605 case GLSL_SAMPLER_DIM_1D
:
607 return (array
? sampler1DArrayShadow_type
: sampler1DShadow_type
);
609 return (array
? sampler1DArray_type
: sampler1D_type
);
610 case GLSL_SAMPLER_DIM_2D
:
612 return (array
? sampler2DArrayShadow_type
: sampler2DShadow_type
);
614 return (array
? sampler2DArray_type
: sampler2D_type
);
615 case GLSL_SAMPLER_DIM_3D
:
619 return sampler3D_type
;
620 case GLSL_SAMPLER_DIM_CUBE
:
622 return (array
? samplerCubeArrayShadow_type
: samplerCubeShadow_type
);
624 return (array
? samplerCubeArray_type
: samplerCube_type
);
625 case GLSL_SAMPLER_DIM_RECT
:
629 return sampler2DRectShadow_type
;
631 return sampler2DRect_type
;
632 case GLSL_SAMPLER_DIM_BUF
:
636 return samplerBuffer_type
;
637 case GLSL_SAMPLER_DIM_MS
:
640 return (array
? sampler2DMSArray_type
: sampler2DMS_type
);
641 case GLSL_SAMPLER_DIM_EXTERNAL
:
645 return samplerExternalOES_type
;
646 case GLSL_SAMPLER_DIM_SUBPASS
:
647 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
654 case GLSL_SAMPLER_DIM_1D
:
655 return (array
? isampler1DArray_type
: isampler1D_type
);
656 case GLSL_SAMPLER_DIM_2D
:
657 return (array
? isampler2DArray_type
: isampler2D_type
);
658 case GLSL_SAMPLER_DIM_3D
:
661 return isampler3D_type
;
662 case GLSL_SAMPLER_DIM_CUBE
:
663 return (array
? isamplerCubeArray_type
: isamplerCube_type
);
664 case GLSL_SAMPLER_DIM_RECT
:
667 return isampler2DRect_type
;
668 case GLSL_SAMPLER_DIM_BUF
:
671 return isamplerBuffer_type
;
672 case GLSL_SAMPLER_DIM_MS
:
673 return (array
? isampler2DMSArray_type
: isampler2DMS_type
);
674 case GLSL_SAMPLER_DIM_EXTERNAL
:
676 case GLSL_SAMPLER_DIM_SUBPASS
:
677 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
684 case GLSL_SAMPLER_DIM_1D
:
685 return (array
? usampler1DArray_type
: usampler1D_type
);
686 case GLSL_SAMPLER_DIM_2D
:
687 return (array
? usampler2DArray_type
: usampler2D_type
);
688 case GLSL_SAMPLER_DIM_3D
:
691 return usampler3D_type
;
692 case GLSL_SAMPLER_DIM_CUBE
:
693 return (array
? usamplerCubeArray_type
: usamplerCube_type
);
694 case GLSL_SAMPLER_DIM_RECT
:
697 return usampler2DRect_type
;
698 case GLSL_SAMPLER_DIM_BUF
:
701 return usamplerBuffer_type
;
702 case GLSL_SAMPLER_DIM_MS
:
703 return (array
? usampler2DMSArray_type
: usampler2DMS_type
);
704 case GLSL_SAMPLER_DIM_EXTERNAL
:
706 case GLSL_SAMPLER_DIM_SUBPASS
:
707 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
714 unreachable("switch statement above should be complete");
718 glsl_type::get_image_instance(enum glsl_sampler_dim dim
,
719 bool array
, glsl_base_type type
)
722 case GLSL_TYPE_FLOAT
:
724 case GLSL_SAMPLER_DIM_1D
:
725 return (array
? image1DArray_type
: image1D_type
);
726 case GLSL_SAMPLER_DIM_2D
:
727 return (array
? image2DArray_type
: image2D_type
);
728 case GLSL_SAMPLER_DIM_3D
:
730 case GLSL_SAMPLER_DIM_CUBE
:
731 return (array
? imageCubeArray_type
: imageCube_type
);
732 case GLSL_SAMPLER_DIM_RECT
:
736 return image2DRect_type
;
737 case GLSL_SAMPLER_DIM_BUF
:
741 return imageBuffer_type
;
742 case GLSL_SAMPLER_DIM_MS
:
743 return (array
? image2DMSArray_type
: image2DMS_type
);
744 case GLSL_SAMPLER_DIM_SUBPASS
:
745 return subpassInput_type
;
746 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
747 return subpassInputMS_type
;
748 case GLSL_SAMPLER_DIM_EXTERNAL
:
753 case GLSL_SAMPLER_DIM_1D
:
754 return (array
? iimage1DArray_type
: iimage1D_type
);
755 case GLSL_SAMPLER_DIM_2D
:
756 return (array
? iimage2DArray_type
: iimage2D_type
);
757 case GLSL_SAMPLER_DIM_3D
:
760 return iimage3D_type
;
761 case GLSL_SAMPLER_DIM_CUBE
:
762 return (array
? iimageCubeArray_type
: iimageCube_type
);
763 case GLSL_SAMPLER_DIM_RECT
:
766 return iimage2DRect_type
;
767 case GLSL_SAMPLER_DIM_BUF
:
770 return iimageBuffer_type
;
771 case GLSL_SAMPLER_DIM_MS
:
772 return (array
? iimage2DMSArray_type
: iimage2DMS_type
);
773 case GLSL_SAMPLER_DIM_SUBPASS
:
774 return isubpassInput_type
;
775 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
776 return isubpassInputMS_type
;
777 case GLSL_SAMPLER_DIM_EXTERNAL
:
782 case GLSL_SAMPLER_DIM_1D
:
783 return (array
? uimage1DArray_type
: uimage1D_type
);
784 case GLSL_SAMPLER_DIM_2D
:
785 return (array
? uimage2DArray_type
: uimage2D_type
);
786 case GLSL_SAMPLER_DIM_3D
:
789 return uimage3D_type
;
790 case GLSL_SAMPLER_DIM_CUBE
:
791 return (array
? uimageCubeArray_type
: uimageCube_type
);
792 case GLSL_SAMPLER_DIM_RECT
:
795 return uimage2DRect_type
;
796 case GLSL_SAMPLER_DIM_BUF
:
799 return uimageBuffer_type
;
800 case GLSL_SAMPLER_DIM_MS
:
801 return (array
? uimage2DMSArray_type
: uimage2DMS_type
);
802 case GLSL_SAMPLER_DIM_SUBPASS
:
803 return usubpassInput_type
;
804 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
805 return usubpassInputMS_type
;
806 case GLSL_SAMPLER_DIM_EXTERNAL
:
813 unreachable("switch statement above should be complete");
817 glsl_type::get_array_instance(const glsl_type
*base
, unsigned array_size
)
819 /* Generate a name using the base type pointer in the key. This is
820 * done because the name of the base type may not be unique across
821 * shaders. For example, two shaders may have different record types
825 snprintf(key
, sizeof(key
), "%p[%u]", (void *) base
, array_size
);
827 mtx_lock(&glsl_type::mutex
);
829 if (array_types
== NULL
) {
830 array_types
= _mesa_hash_table_create(NULL
, _mesa_key_hash_string
,
831 _mesa_key_string_equal
);
834 const struct hash_entry
*entry
= _mesa_hash_table_search(array_types
, key
);
836 mtx_unlock(&glsl_type::mutex
);
837 const glsl_type
*t
= new glsl_type(base
, array_size
);
838 mtx_lock(&glsl_type::mutex
);
840 entry
= _mesa_hash_table_insert(array_types
,
841 ralloc_strdup(mem_ctx
, key
),
845 assert(((glsl_type
*) entry
->data
)->base_type
== GLSL_TYPE_ARRAY
);
846 assert(((glsl_type
*) entry
->data
)->length
== array_size
);
847 assert(((glsl_type
*) entry
->data
)->fields
.array
== base
);
849 mtx_unlock(&glsl_type::mutex
);
851 return (glsl_type
*) entry
->data
;
856 glsl_type::record_compare(const glsl_type
*b
, bool match_locations
) const
858 if (this->length
!= b
->length
)
861 if (this->interface_packing
!= b
->interface_packing
)
864 if (this->interface_row_major
!= b
->interface_row_major
)
867 /* From the GLSL 4.20 specification (Sec 4.2):
869 * "Structures must have the same name, sequence of type names, and
870 * type definitions, and field names to be considered the same type."
872 * GLSL ES behaves the same (Ver 1.00 Sec 4.2.4, Ver 3.00 Sec 4.2.5).
874 * Note that we cannot force type name check when comparing unnamed
875 * structure types, these have a unique name assigned during parsing.
877 if (!this->is_anonymous() && !b
->is_anonymous())
878 if (strcmp(this->name
, b
->name
) != 0)
881 for (unsigned i
= 0; i
< this->length
; i
++) {
882 if (this->fields
.structure
[i
].type
!= b
->fields
.structure
[i
].type
)
884 if (strcmp(this->fields
.structure
[i
].name
,
885 b
->fields
.structure
[i
].name
) != 0)
887 if (this->fields
.structure
[i
].matrix_layout
888 != b
->fields
.structure
[i
].matrix_layout
)
890 if (match_locations
&& this->fields
.structure
[i
].location
891 != b
->fields
.structure
[i
].location
)
893 if (this->fields
.structure
[i
].offset
894 != b
->fields
.structure
[i
].offset
)
896 if (this->fields
.structure
[i
].interpolation
897 != b
->fields
.structure
[i
].interpolation
)
899 if (this->fields
.structure
[i
].centroid
900 != b
->fields
.structure
[i
].centroid
)
902 if (this->fields
.structure
[i
].sample
903 != b
->fields
.structure
[i
].sample
)
905 if (this->fields
.structure
[i
].patch
906 != b
->fields
.structure
[i
].patch
)
908 if (this->fields
.structure
[i
].image_read_only
909 != b
->fields
.structure
[i
].image_read_only
)
911 if (this->fields
.structure
[i
].image_write_only
912 != b
->fields
.structure
[i
].image_write_only
)
914 if (this->fields
.structure
[i
].image_coherent
915 != b
->fields
.structure
[i
].image_coherent
)
917 if (this->fields
.structure
[i
].image_volatile
918 != b
->fields
.structure
[i
].image_volatile
)
920 if (this->fields
.structure
[i
].image_restrict
921 != b
->fields
.structure
[i
].image_restrict
)
923 if (this->fields
.structure
[i
].precision
924 != b
->fields
.structure
[i
].precision
)
926 if (this->fields
.structure
[i
].explicit_xfb_buffer
927 != b
->fields
.structure
[i
].explicit_xfb_buffer
)
929 if (this->fields
.structure
[i
].xfb_buffer
930 != b
->fields
.structure
[i
].xfb_buffer
)
932 if (this->fields
.structure
[i
].xfb_stride
933 != b
->fields
.structure
[i
].xfb_stride
)
942 glsl_type::record_key_compare(const void *a
, const void *b
)
944 const glsl_type
*const key1
= (glsl_type
*) a
;
945 const glsl_type
*const key2
= (glsl_type
*) b
;
947 return strcmp(key1
->name
, key2
->name
) == 0 && key1
->record_compare(key2
);
952 * Generate an integer hash value for a glsl_type structure type.
955 glsl_type::record_key_hash(const void *a
)
957 const glsl_type
*const key
= (glsl_type
*) a
;
958 uintptr_t hash
= key
->length
;
961 for (unsigned i
= 0; i
< key
->length
; i
++) {
962 /* casting pointer to uintptr_t */
963 hash
= (hash
* 13 ) + (uintptr_t) key
->fields
.structure
[i
].type
;
966 if (sizeof(hash
) == 8)
967 retval
= (hash
& 0xffffffff) ^ ((uint64_t) hash
>> 32);
976 glsl_type::get_record_instance(const glsl_struct_field
*fields
,
980 const glsl_type
key(fields
, num_fields
, name
);
982 mtx_lock(&glsl_type::mutex
);
984 if (record_types
== NULL
) {
985 record_types
= _mesa_hash_table_create(NULL
, record_key_hash
,
989 const struct hash_entry
*entry
= _mesa_hash_table_search(record_types
,
992 mtx_unlock(&glsl_type::mutex
);
993 const glsl_type
*t
= new glsl_type(fields
, num_fields
, name
);
994 mtx_lock(&glsl_type::mutex
);
996 entry
= _mesa_hash_table_insert(record_types
, t
, (void *) t
);
999 assert(((glsl_type
*) entry
->data
)->base_type
== GLSL_TYPE_STRUCT
);
1000 assert(((glsl_type
*) entry
->data
)->length
== num_fields
);
1001 assert(strcmp(((glsl_type
*) entry
->data
)->name
, name
) == 0);
1003 mtx_unlock(&glsl_type::mutex
);
1005 return (glsl_type
*) entry
->data
;
1010 glsl_type::get_interface_instance(const glsl_struct_field
*fields
,
1011 unsigned num_fields
,
1012 enum glsl_interface_packing packing
,
1014 const char *block_name
)
1016 const glsl_type
key(fields
, num_fields
, packing
, row_major
, block_name
);
1018 mtx_lock(&glsl_type::mutex
);
1020 if (interface_types
== NULL
) {
1021 interface_types
= _mesa_hash_table_create(NULL
, record_key_hash
,
1022 record_key_compare
);
1025 const struct hash_entry
*entry
= _mesa_hash_table_search(interface_types
,
1027 if (entry
== NULL
) {
1028 mtx_unlock(&glsl_type::mutex
);
1029 const glsl_type
*t
= new glsl_type(fields
, num_fields
,
1030 packing
, row_major
, block_name
);
1031 mtx_lock(&glsl_type::mutex
);
1033 entry
= _mesa_hash_table_insert(interface_types
, t
, (void *) t
);
1036 assert(((glsl_type
*) entry
->data
)->base_type
== GLSL_TYPE_INTERFACE
);
1037 assert(((glsl_type
*) entry
->data
)->length
== num_fields
);
1038 assert(strcmp(((glsl_type
*) entry
->data
)->name
, block_name
) == 0);
1040 mtx_unlock(&glsl_type::mutex
);
1042 return (glsl_type
*) entry
->data
;
1046 glsl_type::get_subroutine_instance(const char *subroutine_name
)
1048 const glsl_type
key(subroutine_name
);
1050 mtx_lock(&glsl_type::mutex
);
1052 if (subroutine_types
== NULL
) {
1053 subroutine_types
= _mesa_hash_table_create(NULL
, record_key_hash
,
1054 record_key_compare
);
1057 const struct hash_entry
*entry
= _mesa_hash_table_search(subroutine_types
,
1059 if (entry
== NULL
) {
1060 mtx_unlock(&glsl_type::mutex
);
1061 const glsl_type
*t
= new glsl_type(subroutine_name
);
1062 mtx_lock(&glsl_type::mutex
);
1064 entry
= _mesa_hash_table_insert(subroutine_types
, t
, (void *) t
);
1067 assert(((glsl_type
*) entry
->data
)->base_type
== GLSL_TYPE_SUBROUTINE
);
1068 assert(strcmp(((glsl_type
*) entry
->data
)->name
, subroutine_name
) == 0);
1070 mtx_unlock(&glsl_type::mutex
);
1072 return (glsl_type
*) entry
->data
;
1077 function_key_compare(const void *a
, const void *b
)
1079 const glsl_type
*const key1
= (glsl_type
*) a
;
1080 const glsl_type
*const key2
= (glsl_type
*) b
;
1082 if (key1
->length
!= key2
->length
)
1085 return memcmp(key1
->fields
.parameters
, key2
->fields
.parameters
,
1086 (key1
->length
+ 1) * sizeof(*key1
->fields
.parameters
)) == 0;
1091 function_key_hash(const void *a
)
1093 const glsl_type
*const key
= (glsl_type
*) a
;
1094 return _mesa_hash_data(key
->fields
.parameters
,
1095 (key
->length
+ 1) * sizeof(*key
->fields
.parameters
));
1099 glsl_type::get_function_instance(const glsl_type
*return_type
,
1100 const glsl_function_param
*params
,
1101 unsigned num_params
)
1103 const glsl_type
key(return_type
, params
, num_params
);
1105 mtx_lock(&glsl_type::mutex
);
1107 if (function_types
== NULL
) {
1108 function_types
= _mesa_hash_table_create(NULL
, function_key_hash
,
1109 function_key_compare
);
1112 struct hash_entry
*entry
= _mesa_hash_table_search(function_types
, &key
);
1113 if (entry
== NULL
) {
1114 mtx_unlock(&glsl_type::mutex
);
1115 const glsl_type
*t
= new glsl_type(return_type
, params
, num_params
);
1116 mtx_lock(&glsl_type::mutex
);
1118 entry
= _mesa_hash_table_insert(function_types
, t
, (void *) t
);
1121 const glsl_type
*t
= (const glsl_type
*)entry
->data
;
1123 assert(t
->base_type
== GLSL_TYPE_FUNCTION
);
1124 assert(t
->length
== num_params
);
1126 mtx_unlock(&glsl_type::mutex
);
1133 glsl_type::get_mul_type(const glsl_type
*type_a
, const glsl_type
*type_b
)
1135 if (type_a
== type_b
) {
1137 } else if (type_a
->is_matrix() && type_b
->is_matrix()) {
1138 /* Matrix multiply. The columns of A must match the rows of B. Given
1139 * the other previously tested constraints, this means the vector type
1140 * of a row from A must be the same as the vector type of a column from
1143 if (type_a
->row_type() == type_b
->column_type()) {
1144 /* The resulting matrix has the number of columns of matrix B and
1145 * the number of rows of matrix A. We get the row count of A by
1146 * looking at the size of a vector that makes up a column. The
1147 * transpose (size of a row) is done for B.
1149 const glsl_type
*const type
=
1150 get_instance(type_a
->base_type
,
1151 type_a
->column_type()->vector_elements
,
1152 type_b
->row_type()->vector_elements
);
1153 assert(type
!= error_type
);
1157 } else if (type_a
->is_matrix()) {
1158 /* A is a matrix and B is a column vector. Columns of A must match
1159 * rows of B. Given the other previously tested constraints, this
1160 * means the vector type of a row from A must be the same as the
1161 * vector the type of B.
1163 if (type_a
->row_type() == type_b
) {
1164 /* The resulting vector has a number of elements equal to
1165 * the number of rows of matrix A. */
1166 const glsl_type
*const type
=
1167 get_instance(type_a
->base_type
,
1168 type_a
->column_type()->vector_elements
,
1170 assert(type
!= error_type
);
1175 assert(type_b
->is_matrix());
1177 /* A is a row vector and B is a matrix. Columns of A must match rows
1178 * of B. Given the other previously tested constraints, this means
1179 * the type of A must be the same as the vector type of a column from
1182 if (type_a
== type_b
->column_type()) {
1183 /* The resulting vector has a number of elements equal to
1184 * the number of columns of matrix B. */
1185 const glsl_type
*const type
=
1186 get_instance(type_a
->base_type
,
1187 type_b
->row_type()->vector_elements
,
1189 assert(type
!= error_type
);
1200 glsl_type::field_type(const char *name
) const
1202 if (this->base_type
!= GLSL_TYPE_STRUCT
1203 && this->base_type
!= GLSL_TYPE_INTERFACE
)
1206 for (unsigned i
= 0; i
< this->length
; i
++) {
1207 if (strcmp(name
, this->fields
.structure
[i
].name
) == 0)
1208 return this->fields
.structure
[i
].type
;
1216 glsl_type::field_index(const char *name
) const
1218 if (this->base_type
!= GLSL_TYPE_STRUCT
1219 && this->base_type
!= GLSL_TYPE_INTERFACE
)
1222 for (unsigned i
= 0; i
< this->length
; i
++) {
1223 if (strcmp(name
, this->fields
.structure
[i
].name
) == 0)
1232 glsl_type::component_slots() const
1234 switch (this->base_type
) {
1235 case GLSL_TYPE_UINT
:
1237 case GLSL_TYPE_FLOAT
:
1238 case GLSL_TYPE_BOOL
:
1239 return this->components();
1241 case GLSL_TYPE_DOUBLE
:
1242 return 2 * this->components();
1244 case GLSL_TYPE_STRUCT
:
1245 case GLSL_TYPE_INTERFACE
: {
1248 for (unsigned i
= 0; i
< this->length
; i
++)
1249 size
+= this->fields
.structure
[i
].type
->component_slots();
1254 case GLSL_TYPE_ARRAY
:
1255 return this->length
* this->fields
.array
->component_slots();
1257 case GLSL_TYPE_IMAGE
:
1259 case GLSL_TYPE_SUBROUTINE
:
1262 case GLSL_TYPE_FUNCTION
:
1263 case GLSL_TYPE_SAMPLER
:
1264 case GLSL_TYPE_ATOMIC_UINT
:
1265 case GLSL_TYPE_VOID
:
1266 case GLSL_TYPE_ERROR
:
1274 glsl_type::record_location_offset(unsigned length
) const
1276 unsigned offset
= 0;
1277 const glsl_type
*t
= this->without_array();
1278 if (t
->is_record()) {
1279 assert(length
<= t
->length
);
1281 for (unsigned i
= 0; i
< length
; i
++) {
1282 const glsl_type
*st
= t
->fields
.structure
[i
].type
;
1283 const glsl_type
*wa
= st
->without_array();
1284 if (wa
->is_record()) {
1285 unsigned r_offset
= wa
->record_location_offset(wa
->length
);
1286 offset
+= st
->is_array() ?
1287 st
->arrays_of_arrays_size() * r_offset
: r_offset
;
1288 } else if (st
->is_array() && st
->fields
.array
->is_array()) {
1289 unsigned outer_array_size
= st
->length
;
1290 const glsl_type
*base_type
= st
->fields
.array
;
1292 /* For arrays of arrays the outer arrays take up a uniform
1293 * slot for each element. The innermost array elements share a
1294 * single slot so we ignore the innermost array when calculating
1297 while (base_type
->fields
.array
->is_array()) {
1298 outer_array_size
= outer_array_size
* base_type
->length
;
1299 base_type
= base_type
->fields
.array
;
1301 offset
+= outer_array_size
;
1303 /* We dont worry about arrays here because unless the array
1304 * contains a structure or another array it only takes up a single
1315 glsl_type::uniform_locations() const
1319 switch (this->base_type
) {
1320 case GLSL_TYPE_UINT
:
1322 case GLSL_TYPE_FLOAT
:
1323 case GLSL_TYPE_DOUBLE
:
1324 case GLSL_TYPE_BOOL
:
1325 case GLSL_TYPE_SAMPLER
:
1326 case GLSL_TYPE_IMAGE
:
1327 case GLSL_TYPE_SUBROUTINE
:
1330 case GLSL_TYPE_STRUCT
:
1331 case GLSL_TYPE_INTERFACE
:
1332 for (unsigned i
= 0; i
< this->length
; i
++)
1333 size
+= this->fields
.structure
[i
].type
->uniform_locations();
1335 case GLSL_TYPE_ARRAY
:
1336 return this->length
* this->fields
.array
->uniform_locations();
1343 glsl_type::varying_count() const
1347 switch (this->base_type
) {
1348 case GLSL_TYPE_UINT
:
1350 case GLSL_TYPE_FLOAT
:
1351 case GLSL_TYPE_DOUBLE
:
1352 case GLSL_TYPE_BOOL
:
1355 case GLSL_TYPE_STRUCT
:
1356 case GLSL_TYPE_INTERFACE
:
1357 for (unsigned i
= 0; i
< this->length
; i
++)
1358 size
+= this->fields
.structure
[i
].type
->varying_count();
1360 case GLSL_TYPE_ARRAY
:
1361 /* Don't count innermost array elements */
1362 if (this->without_array()->is_record() ||
1363 this->without_array()->is_interface() ||
1364 this->fields
.array
->is_array())
1365 return this->length
* this->fields
.array
->varying_count();
1367 return this->fields
.array
->varying_count();
1369 assert(!"unsupported varying type");
1375 glsl_type::can_implicitly_convert_to(const glsl_type
*desired
,
1376 _mesa_glsl_parse_state
*state
) const
1378 if (this == desired
)
1381 /* GLSL 1.10 and ESSL do not allow implicit conversions. If there is no
1382 * state, we're doing intra-stage function linking where these checks have
1383 * already been done.
1385 if (state
&& (state
->es_shader
|| !state
->is_version(120, 0)))
1388 /* There is no conversion among matrix types. */
1389 if (this->matrix_columns
> 1 || desired
->matrix_columns
> 1)
1392 /* Vector size must match. */
1393 if (this->vector_elements
!= desired
->vector_elements
)
1396 /* int and uint can be converted to float. */
1397 if (desired
->is_float() && this->is_integer())
1400 /* With GLSL 4.0, ARB_gpu_shader5, or MESA_shader_integer_functions, int
1401 * can be converted to uint. Note that state may be NULL here, when
1402 * resolving function calls in the linker. By this time, all the
1403 * state-dependent checks have already happened though, so allow anything
1404 * that's allowed in any shader version.
1406 if ((!state
|| state
->is_version(400, 0) || state
->ARB_gpu_shader5_enable
||
1407 state
->MESA_shader_integer_functions_enable
) &&
1408 desired
->base_type
== GLSL_TYPE_UINT
&& this->base_type
== GLSL_TYPE_INT
)
1411 /* No implicit conversions from double. */
1412 if ((!state
|| state
->has_double()) && this->is_double())
1415 /* Conversions from different types to double. */
1416 if ((!state
|| state
->has_double()) && desired
->is_double()) {
1417 if (this->is_float())
1419 if (this->is_integer())
1427 glsl_type::std140_base_alignment(bool row_major
) const
1429 unsigned N
= is_64bit() ? 8 : 4;
1431 /* (1) If the member is a scalar consuming <N> basic machine units, the
1432 * base alignment is <N>.
1434 * (2) If the member is a two- or four-component vector with components
1435 * consuming <N> basic machine units, the base alignment is 2<N> or
1436 * 4<N>, respectively.
1438 * (3) If the member is a three-component vector with components consuming
1439 * <N> basic machine units, the base alignment is 4<N>.
1441 if (this->is_scalar() || this->is_vector()) {
1442 switch (this->vector_elements
) {
1453 /* (4) If the member is an array of scalars or vectors, the base alignment
1454 * and array stride are set to match the base alignment of a single
1455 * array element, according to rules (1), (2), and (3), and rounded up
1456 * to the base alignment of a vec4. The array may have padding at the
1457 * end; the base offset of the member following the array is rounded up
1458 * to the next multiple of the base alignment.
1460 * (6) If the member is an array of <S> column-major matrices with <C>
1461 * columns and <R> rows, the matrix is stored identically to a row of
1462 * <S>*<C> column vectors with <R> components each, according to rule
1465 * (8) If the member is an array of <S> row-major matrices with <C> columns
1466 * and <R> rows, the matrix is stored identically to a row of <S>*<R>
1467 * row vectors with <C> components each, according to rule (4).
1469 * (10) If the member is an array of <S> structures, the <S> elements of
1470 * the array are laid out in order, according to rule (9).
1472 if (this->is_array()) {
1473 if (this->fields
.array
->is_scalar() ||
1474 this->fields
.array
->is_vector() ||
1475 this->fields
.array
->is_matrix()) {
1476 return MAX2(this->fields
.array
->std140_base_alignment(row_major
), 16);
1478 assert(this->fields
.array
->is_record() ||
1479 this->fields
.array
->is_array());
1480 return this->fields
.array
->std140_base_alignment(row_major
);
1484 /* (5) If the member is a column-major matrix with <C> columns and
1485 * <R> rows, the matrix is stored identically to an array of
1486 * <C> column vectors with <R> components each, according to
1489 * (7) If the member is a row-major matrix with <C> columns and <R>
1490 * rows, the matrix is stored identically to an array of <R>
1491 * row vectors with <C> components each, according to rule (4).
1493 if (this->is_matrix()) {
1494 const struct glsl_type
*vec_type
, *array_type
;
1495 int c
= this->matrix_columns
;
1496 int r
= this->vector_elements
;
1499 vec_type
= get_instance(base_type
, c
, 1);
1500 array_type
= glsl_type::get_array_instance(vec_type
, r
);
1502 vec_type
= get_instance(base_type
, r
, 1);
1503 array_type
= glsl_type::get_array_instance(vec_type
, c
);
1506 return array_type
->std140_base_alignment(false);
1509 /* (9) If the member is a structure, the base alignment of the
1510 * structure is <N>, where <N> is the largest base alignment
1511 * value of any of its members, and rounded up to the base
1512 * alignment of a vec4. The individual members of this
1513 * sub-structure are then assigned offsets by applying this set
1514 * of rules recursively, where the base offset of the first
1515 * member of the sub-structure is equal to the aligned offset
1516 * of the structure. The structure may have padding at the end;
1517 * the base offset of the member following the sub-structure is
1518 * rounded up to the next multiple of the base alignment of the
1521 if (this->is_record()) {
1522 unsigned base_alignment
= 16;
1523 for (unsigned i
= 0; i
< this->length
; i
++) {
1524 bool field_row_major
= row_major
;
1525 const enum glsl_matrix_layout matrix_layout
=
1526 glsl_matrix_layout(this->fields
.structure
[i
].matrix_layout
);
1527 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
1528 field_row_major
= true;
1529 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
1530 field_row_major
= false;
1533 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
1534 base_alignment
= MAX2(base_alignment
,
1535 field_type
->std140_base_alignment(field_row_major
));
1537 return base_alignment
;
1540 assert(!"not reached");
1545 glsl_type::std140_size(bool row_major
) const
1547 unsigned N
= is_64bit() ? 8 : 4;
1549 /* (1) If the member is a scalar consuming <N> basic machine units, the
1550 * base alignment is <N>.
1552 * (2) If the member is a two- or four-component vector with components
1553 * consuming <N> basic machine units, the base alignment is 2<N> or
1554 * 4<N>, respectively.
1556 * (3) If the member is a three-component vector with components consuming
1557 * <N> basic machine units, the base alignment is 4<N>.
1559 if (this->is_scalar() || this->is_vector()) {
1560 return this->vector_elements
* N
;
1563 /* (5) If the member is a column-major matrix with <C> columns and
1564 * <R> rows, the matrix is stored identically to an array of
1565 * <C> column vectors with <R> components each, according to
1568 * (6) If the member is an array of <S> column-major matrices with <C>
1569 * columns and <R> rows, the matrix is stored identically to a row of
1570 * <S>*<C> column vectors with <R> components each, according to rule
1573 * (7) If the member is a row-major matrix with <C> columns and <R>
1574 * rows, the matrix is stored identically to an array of <R>
1575 * row vectors with <C> components each, according to rule (4).
1577 * (8) If the member is an array of <S> row-major matrices with <C> columns
1578 * and <R> rows, the matrix is stored identically to a row of <S>*<R>
1579 * row vectors with <C> components each, according to rule (4).
1581 if (this->without_array()->is_matrix()) {
1582 const struct glsl_type
*element_type
;
1583 const struct glsl_type
*vec_type
;
1584 unsigned int array_len
;
1586 if (this->is_array()) {
1587 element_type
= this->without_array();
1588 array_len
= this->arrays_of_arrays_size();
1590 element_type
= this;
1595 vec_type
= get_instance(element_type
->base_type
,
1596 element_type
->matrix_columns
, 1);
1598 array_len
*= element_type
->vector_elements
;
1600 vec_type
= get_instance(element_type
->base_type
,
1601 element_type
->vector_elements
, 1);
1602 array_len
*= element_type
->matrix_columns
;
1604 const glsl_type
*array_type
= glsl_type::get_array_instance(vec_type
,
1607 return array_type
->std140_size(false);
1610 /* (4) If the member is an array of scalars or vectors, the base alignment
1611 * and array stride are set to match the base alignment of a single
1612 * array element, according to rules (1), (2), and (3), and rounded up
1613 * to the base alignment of a vec4. The array may have padding at the
1614 * end; the base offset of the member following the array is rounded up
1615 * to the next multiple of the base alignment.
1617 * (10) If the member is an array of <S> structures, the <S> elements of
1618 * the array are laid out in order, according to rule (9).
1620 if (this->is_array()) {
1621 if (this->without_array()->is_record()) {
1622 return this->arrays_of_arrays_size() *
1623 this->without_array()->std140_size(row_major
);
1625 unsigned element_base_align
=
1626 this->without_array()->std140_base_alignment(row_major
);
1627 return this->arrays_of_arrays_size() * MAX2(element_base_align
, 16);
1631 /* (9) If the member is a structure, the base alignment of the
1632 * structure is <N>, where <N> is the largest base alignment
1633 * value of any of its members, and rounded up to the base
1634 * alignment of a vec4. The individual members of this
1635 * sub-structure are then assigned offsets by applying this set
1636 * of rules recursively, where the base offset of the first
1637 * member of the sub-structure is equal to the aligned offset
1638 * of the structure. The structure may have padding at the end;
1639 * the base offset of the member following the sub-structure is
1640 * rounded up to the next multiple of the base alignment of the
1643 if (this->is_record() || this->is_interface()) {
1645 unsigned max_align
= 0;
1647 for (unsigned i
= 0; i
< this->length
; i
++) {
1648 bool field_row_major
= row_major
;
1649 const enum glsl_matrix_layout matrix_layout
=
1650 glsl_matrix_layout(this->fields
.structure
[i
].matrix_layout
);
1651 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
1652 field_row_major
= true;
1653 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
1654 field_row_major
= false;
1657 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
1658 unsigned align
= field_type
->std140_base_alignment(field_row_major
);
1660 /* Ignore unsized arrays when calculating size */
1661 if (field_type
->is_unsized_array())
1664 size
= glsl_align(size
, align
);
1665 size
+= field_type
->std140_size(field_row_major
);
1667 max_align
= MAX2(align
, max_align
);
1669 if (field_type
->is_record() && (i
+ 1 < this->length
))
1670 size
= glsl_align(size
, 16);
1672 size
= glsl_align(size
, MAX2(max_align
, 16));
1676 assert(!"not reached");
1681 glsl_type::std430_base_alignment(bool row_major
) const
1684 unsigned N
= is_64bit() ? 8 : 4;
1686 /* (1) If the member is a scalar consuming <N> basic machine units, the
1687 * base alignment is <N>.
1689 * (2) If the member is a two- or four-component vector with components
1690 * consuming <N> basic machine units, the base alignment is 2<N> or
1691 * 4<N>, respectively.
1693 * (3) If the member is a three-component vector with components consuming
1694 * <N> basic machine units, the base alignment is 4<N>.
1696 if (this->is_scalar() || this->is_vector()) {
1697 switch (this->vector_elements
) {
1708 /* OpenGL 4.30 spec, section 7.6.2.2 "Standard Uniform Block Layout":
1710 * "When using the std430 storage layout, shader storage blocks will be
1711 * laid out in buffer storage identically to uniform and shader storage
1712 * blocks using the std140 layout, except that the base alignment and
1713 * stride of arrays of scalars and vectors in rule 4 and of structures
1714 * in rule 9 are not rounded up a multiple of the base alignment of a vec4.
1717 /* (1) If the member is a scalar consuming <N> basic machine units, the
1718 * base alignment is <N>.
1720 * (2) If the member is a two- or four-component vector with components
1721 * consuming <N> basic machine units, the base alignment is 2<N> or
1722 * 4<N>, respectively.
1724 * (3) If the member is a three-component vector with components consuming
1725 * <N> basic machine units, the base alignment is 4<N>.
1727 if (this->is_array())
1728 return this->fields
.array
->std430_base_alignment(row_major
);
1730 /* (5) If the member is a column-major matrix with <C> columns and
1731 * <R> rows, the matrix is stored identically to an array of
1732 * <C> column vectors with <R> components each, according to
1735 * (7) If the member is a row-major matrix with <C> columns and <R>
1736 * rows, the matrix is stored identically to an array of <R>
1737 * row vectors with <C> components each, according to rule (4).
1739 if (this->is_matrix()) {
1740 const struct glsl_type
*vec_type
, *array_type
;
1741 int c
= this->matrix_columns
;
1742 int r
= this->vector_elements
;
1745 vec_type
= get_instance(base_type
, c
, 1);
1746 array_type
= glsl_type::get_array_instance(vec_type
, r
);
1748 vec_type
= get_instance(base_type
, r
, 1);
1749 array_type
= glsl_type::get_array_instance(vec_type
, c
);
1752 return array_type
->std430_base_alignment(false);
1755 /* (9) If the member is a structure, the base alignment of the
1756 * structure is <N>, where <N> is the largest base alignment
1757 * value of any of its members, and rounded up to the base
1758 * alignment of a vec4. The individual members of this
1759 * sub-structure are then assigned offsets by applying this set
1760 * of rules recursively, where the base offset of the first
1761 * member of the sub-structure is equal to the aligned offset
1762 * of the structure. The structure may have padding at the end;
1763 * the base offset of the member following the sub-structure is
1764 * rounded up to the next multiple of the base alignment of the
1767 if (this->is_record()) {
1768 unsigned base_alignment
= 0;
1769 for (unsigned i
= 0; i
< this->length
; i
++) {
1770 bool field_row_major
= row_major
;
1771 const enum glsl_matrix_layout matrix_layout
=
1772 glsl_matrix_layout(this->fields
.structure
[i
].matrix_layout
);
1773 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
1774 field_row_major
= true;
1775 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
1776 field_row_major
= false;
1779 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
1780 base_alignment
= MAX2(base_alignment
,
1781 field_type
->std430_base_alignment(field_row_major
));
1783 assert(base_alignment
> 0);
1784 return base_alignment
;
1786 assert(!"not reached");
1791 glsl_type::std430_array_stride(bool row_major
) const
1793 unsigned N
= is_64bit() ? 8 : 4;
1795 /* Notice that the array stride of a vec3 is not 3 * N but 4 * N.
1796 * See OpenGL 4.30 spec, section 7.6.2.2 "Standard Uniform Block Layout"
1798 * (3) If the member is a three-component vector with components consuming
1799 * <N> basic machine units, the base alignment is 4<N>.
1801 if (this->is_vector() && this->vector_elements
== 3)
1804 /* By default use std430_size(row_major) */
1805 return this->std430_size(row_major
);
1809 glsl_type::std430_size(bool row_major
) const
1811 unsigned N
= is_64bit() ? 8 : 4;
1813 /* OpenGL 4.30 spec, section 7.6.2.2 "Standard Uniform Block Layout":
1815 * "When using the std430 storage layout, shader storage blocks will be
1816 * laid out in buffer storage identically to uniform and shader storage
1817 * blocks using the std140 layout, except that the base alignment and
1818 * stride of arrays of scalars and vectors in rule 4 and of structures
1819 * in rule 9 are not rounded up a multiple of the base alignment of a vec4.
1821 if (this->is_scalar() || this->is_vector())
1822 return this->vector_elements
* N
;
1824 if (this->without_array()->is_matrix()) {
1825 const struct glsl_type
*element_type
;
1826 const struct glsl_type
*vec_type
;
1827 unsigned int array_len
;
1829 if (this->is_array()) {
1830 element_type
= this->without_array();
1831 array_len
= this->arrays_of_arrays_size();
1833 element_type
= this;
1838 vec_type
= get_instance(element_type
->base_type
,
1839 element_type
->matrix_columns
, 1);
1841 array_len
*= element_type
->vector_elements
;
1843 vec_type
= get_instance(element_type
->base_type
,
1844 element_type
->vector_elements
, 1);
1845 array_len
*= element_type
->matrix_columns
;
1847 const glsl_type
*array_type
= glsl_type::get_array_instance(vec_type
,
1850 return array_type
->std430_size(false);
1853 if (this->is_array()) {
1854 if (this->without_array()->is_record())
1855 return this->arrays_of_arrays_size() *
1856 this->without_array()->std430_size(row_major
);
1858 return this->arrays_of_arrays_size() *
1859 this->without_array()->std430_base_alignment(row_major
);
1862 if (this->is_record() || this->is_interface()) {
1864 unsigned max_align
= 0;
1866 for (unsigned i
= 0; i
< this->length
; i
++) {
1867 bool field_row_major
= row_major
;
1868 const enum glsl_matrix_layout matrix_layout
=
1869 glsl_matrix_layout(this->fields
.structure
[i
].matrix_layout
);
1870 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
1871 field_row_major
= true;
1872 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
1873 field_row_major
= false;
1876 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
1877 unsigned align
= field_type
->std430_base_alignment(field_row_major
);
1878 size
= glsl_align(size
, align
);
1879 size
+= field_type
->std430_size(field_row_major
);
1881 max_align
= MAX2(align
, max_align
);
1883 size
= glsl_align(size
, max_align
);
1887 assert(!"not reached");
1892 glsl_type::count_attribute_slots(bool is_vertex_input
) const
1894 /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
1896 * "A scalar input counts the same amount against this limit as a vec4,
1897 * so applications may want to consider packing groups of four
1898 * unrelated float inputs together into a vector to better utilize the
1899 * capabilities of the underlying hardware. A matrix input will use up
1900 * multiple locations. The number of locations used will equal the
1901 * number of columns in the matrix."
1903 * The spec does not explicitly say how arrays are counted. However, it
1904 * should be safe to assume the total number of slots consumed by an array
1905 * is the number of entries in the array multiplied by the number of slots
1906 * consumed by a single element of the array.
1908 * The spec says nothing about how structs are counted, because vertex
1909 * attributes are not allowed to be (or contain) structs. However, Mesa
1910 * allows varying structs, the number of varying slots taken up by a
1911 * varying struct is simply equal to the sum of the number of slots taken
1912 * up by each element.
1914 * Doubles are counted different depending on whether they are vertex
1915 * inputs or everything else. Vertex inputs from ARB_vertex_attrib_64bit
1916 * take one location no matter what size they are, otherwise dvec3/4
1917 * take two locations.
1919 switch (this->base_type
) {
1920 case GLSL_TYPE_UINT
:
1922 case GLSL_TYPE_FLOAT
:
1923 case GLSL_TYPE_BOOL
:
1924 return this->matrix_columns
;
1925 case GLSL_TYPE_DOUBLE
:
1926 if (this->vector_elements
> 2 && !is_vertex_input
)
1927 return this->matrix_columns
* 2;
1929 return this->matrix_columns
;
1930 case GLSL_TYPE_STRUCT
:
1931 case GLSL_TYPE_INTERFACE
: {
1934 for (unsigned i
= 0; i
< this->length
; i
++)
1935 size
+= this->fields
.structure
[i
].type
->count_attribute_slots(is_vertex_input
);
1940 case GLSL_TYPE_ARRAY
:
1941 return this->length
* this->fields
.array
->count_attribute_slots(is_vertex_input
);
1943 case GLSL_TYPE_FUNCTION
:
1944 case GLSL_TYPE_SAMPLER
:
1945 case GLSL_TYPE_IMAGE
:
1946 case GLSL_TYPE_ATOMIC_UINT
:
1947 case GLSL_TYPE_VOID
:
1948 case GLSL_TYPE_SUBROUTINE
:
1949 case GLSL_TYPE_ERROR
:
1953 assert(!"Unexpected type in count_attribute_slots()");
1959 glsl_type::coordinate_components() const
1963 switch (sampler_dimensionality
) {
1964 case GLSL_SAMPLER_DIM_1D
:
1965 case GLSL_SAMPLER_DIM_BUF
:
1968 case GLSL_SAMPLER_DIM_2D
:
1969 case GLSL_SAMPLER_DIM_RECT
:
1970 case GLSL_SAMPLER_DIM_MS
:
1971 case GLSL_SAMPLER_DIM_EXTERNAL
:
1972 case GLSL_SAMPLER_DIM_SUBPASS
:
1975 case GLSL_SAMPLER_DIM_3D
:
1976 case GLSL_SAMPLER_DIM_CUBE
:
1980 assert(!"Should not get here.");
1985 /* Array textures need an additional component for the array index, except
1986 * for cubemap array images that behave like a 2D array of interleaved
1989 if (sampler_array
&&
1990 !(base_type
== GLSL_TYPE_IMAGE
&&
1991 sampler_dimensionality
== GLSL_SAMPLER_DIM_CUBE
))
1998 * Declarations of type flyweights (glsl_type::_foo_type) and
1999 * convenience pointers (glsl_type::foo_type).
2002 #define DECL_TYPE(NAME, ...) \
2003 const glsl_type glsl_type::_##NAME##_type = glsl_type(__VA_ARGS__, #NAME); \
2004 const glsl_type *const glsl_type::NAME##_type = &glsl_type::_##NAME##_type;
2006 #define STRUCT_TYPE(NAME)
2008 #include "compiler/builtin_type_macros.h"