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
));
99 /* Samplers take no storage whatsoever. */
100 matrix_columns
= vector_elements
= 0;
102 matrix_columns
= vector_elements
= 1;
106 glsl_type::glsl_type(const glsl_struct_field
*fields
, unsigned num_fields
,
109 base_type(GLSL_TYPE_STRUCT
),
110 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
111 sampled_type(0), interface_packing(0), interface_row_major(0),
112 vector_elements(0), matrix_columns(0),
117 mtx_lock(&glsl_type::mutex
);
119 init_ralloc_type_ctx();
120 assert(name
!= NULL
);
121 this->name
= ralloc_strdup(this->mem_ctx
, name
);
122 this->fields
.structure
= ralloc_array(this->mem_ctx
,
123 glsl_struct_field
, length
);
125 for (i
= 0; i
< length
; i
++) {
126 this->fields
.structure
[i
] = fields
[i
];
127 this->fields
.structure
[i
].name
= ralloc_strdup(this->fields
.structure
,
131 mtx_unlock(&glsl_type::mutex
);
134 glsl_type::glsl_type(const glsl_struct_field
*fields
, unsigned num_fields
,
135 enum glsl_interface_packing packing
,
136 bool row_major
, const char *name
) :
138 base_type(GLSL_TYPE_INTERFACE
),
139 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
140 sampled_type(0), interface_packing((unsigned) packing
),
141 interface_row_major((unsigned) row_major
),
142 vector_elements(0), matrix_columns(0),
147 mtx_lock(&glsl_type::mutex
);
149 init_ralloc_type_ctx();
150 assert(name
!= NULL
);
151 this->name
= ralloc_strdup(this->mem_ctx
, name
);
152 this->fields
.structure
= rzalloc_array(this->mem_ctx
,
153 glsl_struct_field
, length
);
154 for (i
= 0; i
< length
; i
++) {
155 this->fields
.structure
[i
] = fields
[i
];
156 this->fields
.structure
[i
].name
= ralloc_strdup(this->fields
.structure
,
160 mtx_unlock(&glsl_type::mutex
);
163 glsl_type::glsl_type(const glsl_type
*return_type
,
164 const glsl_function_param
*params
, unsigned num_params
) :
166 base_type(GLSL_TYPE_FUNCTION
),
167 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
168 sampled_type(0), interface_packing(0), interface_row_major(0),
169 vector_elements(0), matrix_columns(0),
174 mtx_lock(&glsl_type::mutex
);
176 init_ralloc_type_ctx();
178 this->fields
.parameters
= rzalloc_array(this->mem_ctx
,
179 glsl_function_param
, num_params
+ 1);
181 /* We store the return type as the first parameter */
182 this->fields
.parameters
[0].type
= return_type
;
183 this->fields
.parameters
[0].in
= false;
184 this->fields
.parameters
[0].out
= true;
186 /* We store the i'th parameter in slot i+1 */
187 for (i
= 0; i
< length
; i
++) {
188 this->fields
.parameters
[i
+ 1].type
= params
[i
].type
;
189 this->fields
.parameters
[i
+ 1].in
= params
[i
].in
;
190 this->fields
.parameters
[i
+ 1].out
= params
[i
].out
;
193 mtx_unlock(&glsl_type::mutex
);
196 glsl_type::glsl_type(const char *subroutine_name
) :
198 base_type(GLSL_TYPE_SUBROUTINE
),
199 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
200 sampled_type(0), interface_packing(0), interface_row_major(0),
201 vector_elements(1), matrix_columns(1),
204 mtx_lock(&glsl_type::mutex
);
206 init_ralloc_type_ctx();
207 assert(subroutine_name
!= NULL
);
208 this->name
= ralloc_strdup(this->mem_ctx
, subroutine_name
);
209 mtx_unlock(&glsl_type::mutex
);
213 glsl_type::contains_sampler() const
215 if (this->is_array()) {
216 return this->fields
.array
->contains_sampler();
217 } else if (this->is_record() || this->is_interface()) {
218 for (unsigned int i
= 0; i
< this->length
; i
++) {
219 if (this->fields
.structure
[i
].type
->contains_sampler())
224 return this->is_sampler();
230 glsl_type::contains_integer() const
232 if (this->is_array()) {
233 return this->fields
.array
->contains_integer();
234 } else if (this->is_record() || this->is_interface()) {
235 for (unsigned int i
= 0; i
< this->length
; i
++) {
236 if (this->fields
.structure
[i
].type
->contains_integer())
241 return this->is_integer();
246 glsl_type::contains_double() const
248 if (this->is_array()) {
249 return this->fields
.array
->contains_double();
250 } else if (this->is_record() || this->is_interface()) {
251 for (unsigned int i
= 0; i
< this->length
; i
++) {
252 if (this->fields
.structure
[i
].type
->contains_double())
257 return this->is_double();
262 glsl_type::contains_opaque() const {
264 case GLSL_TYPE_SAMPLER
:
265 case GLSL_TYPE_IMAGE
:
266 case GLSL_TYPE_ATOMIC_UINT
:
268 case GLSL_TYPE_ARRAY
:
269 return fields
.array
->contains_opaque();
270 case GLSL_TYPE_STRUCT
:
271 case GLSL_TYPE_INTERFACE
:
272 for (unsigned int i
= 0; i
< length
; i
++) {
273 if (fields
.structure
[i
].type
->contains_opaque())
283 glsl_type::contains_subroutine() const
285 if (this->is_array()) {
286 return this->fields
.array
->contains_subroutine();
287 } else if (this->is_record() || this->is_interface()) {
288 for (unsigned int i
= 0; i
< this->length
; i
++) {
289 if (this->fields
.structure
[i
].type
->contains_subroutine())
294 return this->is_subroutine();
299 glsl_type::sampler_index() const
301 const glsl_type
*const t
= (this->is_array()) ? this->fields
.array
: this;
303 assert(t
->is_sampler());
305 switch (t
->sampler_dimensionality
) {
306 case GLSL_SAMPLER_DIM_1D
:
307 return (t
->sampler_array
) ? TEXTURE_1D_ARRAY_INDEX
: TEXTURE_1D_INDEX
;
308 case GLSL_SAMPLER_DIM_2D
:
309 return (t
->sampler_array
) ? TEXTURE_2D_ARRAY_INDEX
: TEXTURE_2D_INDEX
;
310 case GLSL_SAMPLER_DIM_3D
:
311 return TEXTURE_3D_INDEX
;
312 case GLSL_SAMPLER_DIM_CUBE
:
313 return (t
->sampler_array
) ? TEXTURE_CUBE_ARRAY_INDEX
: TEXTURE_CUBE_INDEX
;
314 case GLSL_SAMPLER_DIM_RECT
:
315 return TEXTURE_RECT_INDEX
;
316 case GLSL_SAMPLER_DIM_BUF
:
317 return TEXTURE_BUFFER_INDEX
;
318 case GLSL_SAMPLER_DIM_EXTERNAL
:
319 return TEXTURE_EXTERNAL_INDEX
;
320 case GLSL_SAMPLER_DIM_MS
:
321 return (t
->sampler_array
) ? TEXTURE_2D_MULTISAMPLE_ARRAY_INDEX
: TEXTURE_2D_MULTISAMPLE_INDEX
;
323 assert(!"Should not get here.");
324 return TEXTURE_BUFFER_INDEX
;
329 glsl_type::contains_image() const
331 if (this->is_array()) {
332 return this->fields
.array
->contains_image();
333 } else if (this->is_record() || this->is_interface()) {
334 for (unsigned int i
= 0; i
< this->length
; i
++) {
335 if (this->fields
.structure
[i
].type
->contains_image())
340 return this->is_image();
344 const glsl_type
*glsl_type::get_base_type() const
351 case GLSL_TYPE_FLOAT
:
353 case GLSL_TYPE_DOUBLE
:
357 case GLSL_TYPE_UINT64
:
358 return uint64_t_type
;
359 case GLSL_TYPE_INT64
:
367 const glsl_type
*glsl_type::get_scalar_type() const
369 const glsl_type
*type
= this;
372 while (type
->base_type
== GLSL_TYPE_ARRAY
)
373 type
= type
->fields
.array
;
375 /* Handle vectors and matrices */
376 switch (type
->base_type
) {
381 case GLSL_TYPE_FLOAT
:
383 case GLSL_TYPE_DOUBLE
:
387 case GLSL_TYPE_UINT64
:
388 return uint64_t_type
;
389 case GLSL_TYPE_INT64
:
392 /* Handle everything else */
399 _mesa_glsl_release_types(void)
401 /* Should only be called during atexit (either when unloading shared
402 * object, or if process terminates), so no mutex-locking should be
405 if (glsl_type::array_types
!= NULL
) {
406 _mesa_hash_table_destroy(glsl_type::array_types
, NULL
);
407 glsl_type::array_types
= NULL
;
410 if (glsl_type::record_types
!= NULL
) {
411 _mesa_hash_table_destroy(glsl_type::record_types
, NULL
);
412 glsl_type::record_types
= NULL
;
415 if (glsl_type::interface_types
!= NULL
) {
416 _mesa_hash_table_destroy(glsl_type::interface_types
, NULL
);
417 glsl_type::interface_types
= NULL
;
420 ralloc_free(glsl_type::mem_ctx
);
421 glsl_type::mem_ctx
= NULL
;
425 glsl_type::glsl_type(const glsl_type
*array
, unsigned length
) :
426 base_type(GLSL_TYPE_ARRAY
),
427 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
428 sampled_type(0), interface_packing(0), interface_row_major(0),
429 vector_elements(0), matrix_columns(0),
430 length(length
), name(NULL
)
432 this->fields
.array
= array
;
433 /* Inherit the gl type of the base. The GL type is used for
434 * uniform/statevar handling in Mesa and the arrayness of the type
435 * is represented by the size rather than the type.
437 this->gl_type
= array
->gl_type
;
439 /* Allow a maximum of 10 characters for the array size. This is enough
440 * for 32-bits of ~0. The extra 3 are for the '[', ']', and terminating
443 const unsigned name_length
= strlen(array
->name
) + 10 + 3;
445 mtx_lock(&glsl_type::mutex
);
446 char *const n
= (char *) ralloc_size(this->mem_ctx
, name_length
);
447 mtx_unlock(&glsl_type::mutex
);
450 snprintf(n
, name_length
, "%s[]", array
->name
);
452 /* insert outermost dimensions in the correct spot
453 * otherwise the dimension order will be backwards
455 const char *pos
= strchr(array
->name
, '[');
457 int idx
= pos
- array
->name
;
458 snprintf(n
, idx
+1, "%s", array
->name
);
459 snprintf(n
+ idx
, name_length
- idx
, "[%u]%s",
460 length
, array
->name
+ idx
);
462 snprintf(n
, name_length
, "%s[%u]", array
->name
, length
);
471 glsl_type::vec(unsigned components
)
473 if (components
== 0 || components
> 4)
476 static const glsl_type
*const ts
[] = {
477 float_type
, vec2_type
, vec3_type
, vec4_type
479 return ts
[components
- 1];
483 glsl_type::dvec(unsigned components
)
485 if (components
== 0 || components
> 4)
488 static const glsl_type
*const ts
[] = {
489 double_type
, dvec2_type
, dvec3_type
, dvec4_type
491 return ts
[components
- 1];
495 glsl_type::ivec(unsigned components
)
497 if (components
== 0 || components
> 4)
500 static const glsl_type
*const ts
[] = {
501 int_type
, ivec2_type
, ivec3_type
, ivec4_type
503 return ts
[components
- 1];
508 glsl_type::uvec(unsigned components
)
510 if (components
== 0 || components
> 4)
513 static const glsl_type
*const ts
[] = {
514 uint_type
, uvec2_type
, uvec3_type
, uvec4_type
516 return ts
[components
- 1];
521 glsl_type::bvec(unsigned components
)
523 if (components
== 0 || components
> 4)
526 static const glsl_type
*const ts
[] = {
527 bool_type
, bvec2_type
, bvec3_type
, bvec4_type
529 return ts
[components
- 1];
534 glsl_type::i64vec(unsigned components
)
536 if (components
== 0 || components
> 4)
539 static const glsl_type
*const ts
[] = {
540 int64_t_type
, i64vec2_type
, i64vec3_type
, i64vec4_type
542 return ts
[components
- 1];
547 glsl_type::u64vec(unsigned components
)
549 if (components
== 0 || components
> 4)
552 static const glsl_type
*const ts
[] = {
553 uint64_t_type
, u64vec2_type
, u64vec3_type
, u64vec4_type
555 return ts
[components
- 1];
559 glsl_type::get_instance(unsigned base_type
, unsigned rows
, unsigned columns
)
561 if (base_type
== GLSL_TYPE_VOID
)
564 if ((rows
< 1) || (rows
> 4) || (columns
< 1) || (columns
> 4))
567 /* Treat GLSL vectors as Nx1 matrices.
575 case GLSL_TYPE_FLOAT
:
577 case GLSL_TYPE_DOUBLE
:
581 case GLSL_TYPE_UINT64
:
583 case GLSL_TYPE_INT64
:
589 if ((base_type
!= GLSL_TYPE_FLOAT
&& base_type
!= GLSL_TYPE_DOUBLE
) || (rows
== 1))
592 /* GLSL matrix types are named mat{COLUMNS}x{ROWS}. Only the following
593 * combinations are valid:
601 #define IDX(c,r) (((c-1)*3) + (r-1))
603 if (base_type
== GLSL_TYPE_DOUBLE
) {
604 switch (IDX(columns
, rows
)) {
605 case IDX(2,2): return dmat2_type
;
606 case IDX(2,3): return dmat2x3_type
;
607 case IDX(2,4): return dmat2x4_type
;
608 case IDX(3,2): return dmat3x2_type
;
609 case IDX(3,3): return dmat3_type
;
610 case IDX(3,4): return dmat3x4_type
;
611 case IDX(4,2): return dmat4x2_type
;
612 case IDX(4,3): return dmat4x3_type
;
613 case IDX(4,4): return dmat4_type
;
614 default: return error_type
;
617 switch (IDX(columns
, rows
)) {
618 case IDX(2,2): return mat2_type
;
619 case IDX(2,3): return mat2x3_type
;
620 case IDX(2,4): return mat2x4_type
;
621 case IDX(3,2): return mat3x2_type
;
622 case IDX(3,3): return mat3_type
;
623 case IDX(3,4): return mat3x4_type
;
624 case IDX(4,2): return mat4x2_type
;
625 case IDX(4,3): return mat4x3_type
;
626 case IDX(4,4): return mat4_type
;
627 default: return error_type
;
632 assert(!"Should not get here.");
637 glsl_type::get_sampler_instance(enum glsl_sampler_dim dim
,
643 case GLSL_TYPE_FLOAT
:
645 case GLSL_SAMPLER_DIM_1D
:
647 return (array
? sampler1DArrayShadow_type
: sampler1DShadow_type
);
649 return (array
? sampler1DArray_type
: sampler1D_type
);
650 case GLSL_SAMPLER_DIM_2D
:
652 return (array
? sampler2DArrayShadow_type
: sampler2DShadow_type
);
654 return (array
? sampler2DArray_type
: sampler2D_type
);
655 case GLSL_SAMPLER_DIM_3D
:
659 return sampler3D_type
;
660 case GLSL_SAMPLER_DIM_CUBE
:
662 return (array
? samplerCubeArrayShadow_type
: samplerCubeShadow_type
);
664 return (array
? samplerCubeArray_type
: samplerCube_type
);
665 case GLSL_SAMPLER_DIM_RECT
:
669 return sampler2DRectShadow_type
;
671 return sampler2DRect_type
;
672 case GLSL_SAMPLER_DIM_BUF
:
676 return samplerBuffer_type
;
677 case GLSL_SAMPLER_DIM_MS
:
680 return (array
? sampler2DMSArray_type
: sampler2DMS_type
);
681 case GLSL_SAMPLER_DIM_EXTERNAL
:
685 return samplerExternalOES_type
;
686 case GLSL_SAMPLER_DIM_SUBPASS
:
687 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
694 case GLSL_SAMPLER_DIM_1D
:
695 return (array
? isampler1DArray_type
: isampler1D_type
);
696 case GLSL_SAMPLER_DIM_2D
:
697 return (array
? isampler2DArray_type
: isampler2D_type
);
698 case GLSL_SAMPLER_DIM_3D
:
701 return isampler3D_type
;
702 case GLSL_SAMPLER_DIM_CUBE
:
703 return (array
? isamplerCubeArray_type
: isamplerCube_type
);
704 case GLSL_SAMPLER_DIM_RECT
:
707 return isampler2DRect_type
;
708 case GLSL_SAMPLER_DIM_BUF
:
711 return isamplerBuffer_type
;
712 case GLSL_SAMPLER_DIM_MS
:
713 return (array
? isampler2DMSArray_type
: isampler2DMS_type
);
714 case GLSL_SAMPLER_DIM_EXTERNAL
:
716 case GLSL_SAMPLER_DIM_SUBPASS
:
717 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
724 case GLSL_SAMPLER_DIM_1D
:
725 return (array
? usampler1DArray_type
: usampler1D_type
);
726 case GLSL_SAMPLER_DIM_2D
:
727 return (array
? usampler2DArray_type
: usampler2D_type
);
728 case GLSL_SAMPLER_DIM_3D
:
731 return usampler3D_type
;
732 case GLSL_SAMPLER_DIM_CUBE
:
733 return (array
? usamplerCubeArray_type
: usamplerCube_type
);
734 case GLSL_SAMPLER_DIM_RECT
:
737 return usampler2DRect_type
;
738 case GLSL_SAMPLER_DIM_BUF
:
741 return usamplerBuffer_type
;
742 case GLSL_SAMPLER_DIM_MS
:
743 return (array
? usampler2DMSArray_type
: usampler2DMS_type
);
744 case GLSL_SAMPLER_DIM_EXTERNAL
:
746 case GLSL_SAMPLER_DIM_SUBPASS
:
747 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
754 unreachable("switch statement above should be complete");
758 glsl_type::get_image_instance(enum glsl_sampler_dim dim
,
759 bool array
, glsl_base_type type
)
762 case GLSL_TYPE_FLOAT
:
764 case GLSL_SAMPLER_DIM_1D
:
765 return (array
? image1DArray_type
: image1D_type
);
766 case GLSL_SAMPLER_DIM_2D
:
767 return (array
? image2DArray_type
: image2D_type
);
768 case GLSL_SAMPLER_DIM_3D
:
770 case GLSL_SAMPLER_DIM_CUBE
:
771 return (array
? imageCubeArray_type
: imageCube_type
);
772 case GLSL_SAMPLER_DIM_RECT
:
776 return image2DRect_type
;
777 case GLSL_SAMPLER_DIM_BUF
:
781 return imageBuffer_type
;
782 case GLSL_SAMPLER_DIM_MS
:
783 return (array
? image2DMSArray_type
: image2DMS_type
);
784 case GLSL_SAMPLER_DIM_SUBPASS
:
785 return subpassInput_type
;
786 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
787 return subpassInputMS_type
;
788 case GLSL_SAMPLER_DIM_EXTERNAL
:
793 case GLSL_SAMPLER_DIM_1D
:
794 return (array
? iimage1DArray_type
: iimage1D_type
);
795 case GLSL_SAMPLER_DIM_2D
:
796 return (array
? iimage2DArray_type
: iimage2D_type
);
797 case GLSL_SAMPLER_DIM_3D
:
800 return iimage3D_type
;
801 case GLSL_SAMPLER_DIM_CUBE
:
802 return (array
? iimageCubeArray_type
: iimageCube_type
);
803 case GLSL_SAMPLER_DIM_RECT
:
806 return iimage2DRect_type
;
807 case GLSL_SAMPLER_DIM_BUF
:
810 return iimageBuffer_type
;
811 case GLSL_SAMPLER_DIM_MS
:
812 return (array
? iimage2DMSArray_type
: iimage2DMS_type
);
813 case GLSL_SAMPLER_DIM_SUBPASS
:
814 return isubpassInput_type
;
815 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
816 return isubpassInputMS_type
;
817 case GLSL_SAMPLER_DIM_EXTERNAL
:
822 case GLSL_SAMPLER_DIM_1D
:
823 return (array
? uimage1DArray_type
: uimage1D_type
);
824 case GLSL_SAMPLER_DIM_2D
:
825 return (array
? uimage2DArray_type
: uimage2D_type
);
826 case GLSL_SAMPLER_DIM_3D
:
829 return uimage3D_type
;
830 case GLSL_SAMPLER_DIM_CUBE
:
831 return (array
? uimageCubeArray_type
: uimageCube_type
);
832 case GLSL_SAMPLER_DIM_RECT
:
835 return uimage2DRect_type
;
836 case GLSL_SAMPLER_DIM_BUF
:
839 return uimageBuffer_type
;
840 case GLSL_SAMPLER_DIM_MS
:
841 return (array
? uimage2DMSArray_type
: uimage2DMS_type
);
842 case GLSL_SAMPLER_DIM_SUBPASS
:
843 return usubpassInput_type
;
844 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
845 return usubpassInputMS_type
;
846 case GLSL_SAMPLER_DIM_EXTERNAL
:
853 unreachable("switch statement above should be complete");
857 glsl_type::get_array_instance(const glsl_type
*base
, unsigned array_size
)
859 /* Generate a name using the base type pointer in the key. This is
860 * done because the name of the base type may not be unique across
861 * shaders. For example, two shaders may have different record types
865 snprintf(key
, sizeof(key
), "%p[%u]", (void *) base
, array_size
);
867 mtx_lock(&glsl_type::mutex
);
869 if (array_types
== NULL
) {
870 array_types
= _mesa_hash_table_create(NULL
, _mesa_key_hash_string
,
871 _mesa_key_string_equal
);
874 const struct hash_entry
*entry
= _mesa_hash_table_search(array_types
, key
);
876 mtx_unlock(&glsl_type::mutex
);
877 const glsl_type
*t
= new glsl_type(base
, array_size
);
878 mtx_lock(&glsl_type::mutex
);
880 entry
= _mesa_hash_table_insert(array_types
,
881 ralloc_strdup(mem_ctx
, key
),
885 assert(((glsl_type
*) entry
->data
)->base_type
== GLSL_TYPE_ARRAY
);
886 assert(((glsl_type
*) entry
->data
)->length
== array_size
);
887 assert(((glsl_type
*) entry
->data
)->fields
.array
== base
);
889 mtx_unlock(&glsl_type::mutex
);
891 return (glsl_type
*) entry
->data
;
896 glsl_type::record_compare(const glsl_type
*b
, bool match_locations
) const
898 if (this->length
!= b
->length
)
901 if (this->interface_packing
!= b
->interface_packing
)
904 if (this->interface_row_major
!= b
->interface_row_major
)
907 /* From the GLSL 4.20 specification (Sec 4.2):
909 * "Structures must have the same name, sequence of type names, and
910 * type definitions, and field names to be considered the same type."
912 * GLSL ES behaves the same (Ver 1.00 Sec 4.2.4, Ver 3.00 Sec 4.2.5).
914 * Note that we cannot force type name check when comparing unnamed
915 * structure types, these have a unique name assigned during parsing.
917 if (!this->is_anonymous() && !b
->is_anonymous())
918 if (strcmp(this->name
, b
->name
) != 0)
921 for (unsigned i
= 0; i
< this->length
; i
++) {
922 if (this->fields
.structure
[i
].type
!= b
->fields
.structure
[i
].type
)
924 if (strcmp(this->fields
.structure
[i
].name
,
925 b
->fields
.structure
[i
].name
) != 0)
927 if (this->fields
.structure
[i
].matrix_layout
928 != b
->fields
.structure
[i
].matrix_layout
)
930 if (match_locations
&& this->fields
.structure
[i
].location
931 != b
->fields
.structure
[i
].location
)
933 if (this->fields
.structure
[i
].offset
934 != b
->fields
.structure
[i
].offset
)
936 if (this->fields
.structure
[i
].interpolation
937 != b
->fields
.structure
[i
].interpolation
)
939 if (this->fields
.structure
[i
].centroid
940 != b
->fields
.structure
[i
].centroid
)
942 if (this->fields
.structure
[i
].sample
943 != b
->fields
.structure
[i
].sample
)
945 if (this->fields
.structure
[i
].patch
946 != b
->fields
.structure
[i
].patch
)
948 if (this->fields
.structure
[i
].image_read_only
949 != b
->fields
.structure
[i
].image_read_only
)
951 if (this->fields
.structure
[i
].image_write_only
952 != b
->fields
.structure
[i
].image_write_only
)
954 if (this->fields
.structure
[i
].image_coherent
955 != b
->fields
.structure
[i
].image_coherent
)
957 if (this->fields
.structure
[i
].image_volatile
958 != b
->fields
.structure
[i
].image_volatile
)
960 if (this->fields
.structure
[i
].image_restrict
961 != b
->fields
.structure
[i
].image_restrict
)
963 if (this->fields
.structure
[i
].precision
964 != b
->fields
.structure
[i
].precision
)
966 if (this->fields
.structure
[i
].explicit_xfb_buffer
967 != b
->fields
.structure
[i
].explicit_xfb_buffer
)
969 if (this->fields
.structure
[i
].xfb_buffer
970 != b
->fields
.structure
[i
].xfb_buffer
)
972 if (this->fields
.structure
[i
].xfb_stride
973 != b
->fields
.structure
[i
].xfb_stride
)
982 glsl_type::record_key_compare(const void *a
, const void *b
)
984 const glsl_type
*const key1
= (glsl_type
*) a
;
985 const glsl_type
*const key2
= (glsl_type
*) b
;
987 return strcmp(key1
->name
, key2
->name
) == 0 && key1
->record_compare(key2
);
992 * Generate an integer hash value for a glsl_type structure type.
995 glsl_type::record_key_hash(const void *a
)
997 const glsl_type
*const key
= (glsl_type
*) a
;
998 uintptr_t hash
= key
->length
;
1001 for (unsigned i
= 0; i
< key
->length
; i
++) {
1002 /* casting pointer to uintptr_t */
1003 hash
= (hash
* 13 ) + (uintptr_t) key
->fields
.structure
[i
].type
;
1006 if (sizeof(hash
) == 8)
1007 retval
= (hash
& 0xffffffff) ^ ((uint64_t) hash
>> 32);
1016 glsl_type::get_record_instance(const glsl_struct_field
*fields
,
1017 unsigned num_fields
,
1020 const glsl_type
key(fields
, num_fields
, name
);
1022 mtx_lock(&glsl_type::mutex
);
1024 if (record_types
== NULL
) {
1025 record_types
= _mesa_hash_table_create(NULL
, record_key_hash
,
1026 record_key_compare
);
1029 const struct hash_entry
*entry
= _mesa_hash_table_search(record_types
,
1031 if (entry
== NULL
) {
1032 mtx_unlock(&glsl_type::mutex
);
1033 const glsl_type
*t
= new glsl_type(fields
, num_fields
, name
);
1034 mtx_lock(&glsl_type::mutex
);
1036 entry
= _mesa_hash_table_insert(record_types
, t
, (void *) t
);
1039 assert(((glsl_type
*) entry
->data
)->base_type
== GLSL_TYPE_STRUCT
);
1040 assert(((glsl_type
*) entry
->data
)->length
== num_fields
);
1041 assert(strcmp(((glsl_type
*) entry
->data
)->name
, name
) == 0);
1043 mtx_unlock(&glsl_type::mutex
);
1045 return (glsl_type
*) entry
->data
;
1050 glsl_type::get_interface_instance(const glsl_struct_field
*fields
,
1051 unsigned num_fields
,
1052 enum glsl_interface_packing packing
,
1054 const char *block_name
)
1056 const glsl_type
key(fields
, num_fields
, packing
, row_major
, block_name
);
1058 mtx_lock(&glsl_type::mutex
);
1060 if (interface_types
== NULL
) {
1061 interface_types
= _mesa_hash_table_create(NULL
, record_key_hash
,
1062 record_key_compare
);
1065 const struct hash_entry
*entry
= _mesa_hash_table_search(interface_types
,
1067 if (entry
== NULL
) {
1068 mtx_unlock(&glsl_type::mutex
);
1069 const glsl_type
*t
= new glsl_type(fields
, num_fields
,
1070 packing
, row_major
, block_name
);
1071 mtx_lock(&glsl_type::mutex
);
1073 entry
= _mesa_hash_table_insert(interface_types
, t
, (void *) t
);
1076 assert(((glsl_type
*) entry
->data
)->base_type
== GLSL_TYPE_INTERFACE
);
1077 assert(((glsl_type
*) entry
->data
)->length
== num_fields
);
1078 assert(strcmp(((glsl_type
*) entry
->data
)->name
, block_name
) == 0);
1080 mtx_unlock(&glsl_type::mutex
);
1082 return (glsl_type
*) entry
->data
;
1086 glsl_type::get_subroutine_instance(const char *subroutine_name
)
1088 const glsl_type
key(subroutine_name
);
1090 mtx_lock(&glsl_type::mutex
);
1092 if (subroutine_types
== NULL
) {
1093 subroutine_types
= _mesa_hash_table_create(NULL
, record_key_hash
,
1094 record_key_compare
);
1097 const struct hash_entry
*entry
= _mesa_hash_table_search(subroutine_types
,
1099 if (entry
== NULL
) {
1100 mtx_unlock(&glsl_type::mutex
);
1101 const glsl_type
*t
= new glsl_type(subroutine_name
);
1102 mtx_lock(&glsl_type::mutex
);
1104 entry
= _mesa_hash_table_insert(subroutine_types
, t
, (void *) t
);
1107 assert(((glsl_type
*) entry
->data
)->base_type
== GLSL_TYPE_SUBROUTINE
);
1108 assert(strcmp(((glsl_type
*) entry
->data
)->name
, subroutine_name
) == 0);
1110 mtx_unlock(&glsl_type::mutex
);
1112 return (glsl_type
*) entry
->data
;
1117 function_key_compare(const void *a
, const void *b
)
1119 const glsl_type
*const key1
= (glsl_type
*) a
;
1120 const glsl_type
*const key2
= (glsl_type
*) b
;
1122 if (key1
->length
!= key2
->length
)
1125 return memcmp(key1
->fields
.parameters
, key2
->fields
.parameters
,
1126 (key1
->length
+ 1) * sizeof(*key1
->fields
.parameters
)) == 0;
1131 function_key_hash(const void *a
)
1133 const glsl_type
*const key
= (glsl_type
*) a
;
1134 return _mesa_hash_data(key
->fields
.parameters
,
1135 (key
->length
+ 1) * sizeof(*key
->fields
.parameters
));
1139 glsl_type::get_function_instance(const glsl_type
*return_type
,
1140 const glsl_function_param
*params
,
1141 unsigned num_params
)
1143 const glsl_type
key(return_type
, params
, num_params
);
1145 mtx_lock(&glsl_type::mutex
);
1147 if (function_types
== NULL
) {
1148 function_types
= _mesa_hash_table_create(NULL
, function_key_hash
,
1149 function_key_compare
);
1152 struct hash_entry
*entry
= _mesa_hash_table_search(function_types
, &key
);
1153 if (entry
== NULL
) {
1154 mtx_unlock(&glsl_type::mutex
);
1155 const glsl_type
*t
= new glsl_type(return_type
, params
, num_params
);
1156 mtx_lock(&glsl_type::mutex
);
1158 entry
= _mesa_hash_table_insert(function_types
, t
, (void *) t
);
1161 const glsl_type
*t
= (const glsl_type
*)entry
->data
;
1163 assert(t
->base_type
== GLSL_TYPE_FUNCTION
);
1164 assert(t
->length
== num_params
);
1166 mtx_unlock(&glsl_type::mutex
);
1173 glsl_type::get_mul_type(const glsl_type
*type_a
, const glsl_type
*type_b
)
1175 if (type_a
== type_b
) {
1177 } else if (type_a
->is_matrix() && type_b
->is_matrix()) {
1178 /* Matrix multiply. The columns of A must match the rows of B. Given
1179 * the other previously tested constraints, this means the vector type
1180 * of a row from A must be the same as the vector type of a column from
1183 if (type_a
->row_type() == type_b
->column_type()) {
1184 /* The resulting matrix has the number of columns of matrix B and
1185 * the number of rows of matrix A. We get the row count of A by
1186 * looking at the size of a vector that makes up a column. The
1187 * transpose (size of a row) is done for B.
1189 const glsl_type
*const type
=
1190 get_instance(type_a
->base_type
,
1191 type_a
->column_type()->vector_elements
,
1192 type_b
->row_type()->vector_elements
);
1193 assert(type
!= error_type
);
1197 } else if (type_a
->is_matrix()) {
1198 /* A is a matrix and B is a column vector. Columns of A must match
1199 * rows of B. Given the other previously tested constraints, this
1200 * means the vector type of a row from A must be the same as the
1201 * vector the type of B.
1203 if (type_a
->row_type() == type_b
) {
1204 /* The resulting vector has a number of elements equal to
1205 * the number of rows of matrix A. */
1206 const glsl_type
*const type
=
1207 get_instance(type_a
->base_type
,
1208 type_a
->column_type()->vector_elements
,
1210 assert(type
!= error_type
);
1215 assert(type_b
->is_matrix());
1217 /* A is a row vector and B is a matrix. Columns of A must match rows
1218 * of B. Given the other previously tested constraints, this means
1219 * the type of A must be the same as the vector type of a column from
1222 if (type_a
== type_b
->column_type()) {
1223 /* The resulting vector has a number of elements equal to
1224 * the number of columns of matrix B. */
1225 const glsl_type
*const type
=
1226 get_instance(type_a
->base_type
,
1227 type_b
->row_type()->vector_elements
,
1229 assert(type
!= error_type
);
1240 glsl_type::field_type(const char *name
) const
1242 if (this->base_type
!= GLSL_TYPE_STRUCT
1243 && this->base_type
!= GLSL_TYPE_INTERFACE
)
1246 for (unsigned i
= 0; i
< this->length
; i
++) {
1247 if (strcmp(name
, this->fields
.structure
[i
].name
) == 0)
1248 return this->fields
.structure
[i
].type
;
1256 glsl_type::field_index(const char *name
) const
1258 if (this->base_type
!= GLSL_TYPE_STRUCT
1259 && this->base_type
!= GLSL_TYPE_INTERFACE
)
1262 for (unsigned i
= 0; i
< this->length
; i
++) {
1263 if (strcmp(name
, this->fields
.structure
[i
].name
) == 0)
1272 glsl_type::component_slots() const
1274 switch (this->base_type
) {
1275 case GLSL_TYPE_UINT
:
1277 case GLSL_TYPE_FLOAT
:
1278 case GLSL_TYPE_BOOL
:
1279 return this->components();
1281 case GLSL_TYPE_DOUBLE
:
1282 case GLSL_TYPE_UINT64
:
1283 case GLSL_TYPE_INT64
:
1284 return 2 * this->components();
1286 case GLSL_TYPE_STRUCT
:
1287 case GLSL_TYPE_INTERFACE
: {
1290 for (unsigned i
= 0; i
< this->length
; i
++)
1291 size
+= this->fields
.structure
[i
].type
->component_slots();
1296 case GLSL_TYPE_ARRAY
:
1297 return this->length
* this->fields
.array
->component_slots();
1299 case GLSL_TYPE_IMAGE
:
1301 case GLSL_TYPE_SUBROUTINE
:
1304 case GLSL_TYPE_FUNCTION
:
1305 case GLSL_TYPE_SAMPLER
:
1306 case GLSL_TYPE_ATOMIC_UINT
:
1307 case GLSL_TYPE_VOID
:
1308 case GLSL_TYPE_ERROR
:
1316 glsl_type::record_location_offset(unsigned length
) const
1318 unsigned offset
= 0;
1319 const glsl_type
*t
= this->without_array();
1320 if (t
->is_record()) {
1321 assert(length
<= t
->length
);
1323 for (unsigned i
= 0; i
< length
; i
++) {
1324 const glsl_type
*st
= t
->fields
.structure
[i
].type
;
1325 const glsl_type
*wa
= st
->without_array();
1326 if (wa
->is_record()) {
1327 unsigned r_offset
= wa
->record_location_offset(wa
->length
);
1328 offset
+= st
->is_array() ?
1329 st
->arrays_of_arrays_size() * r_offset
: r_offset
;
1330 } else if (st
->is_array() && st
->fields
.array
->is_array()) {
1331 unsigned outer_array_size
= st
->length
;
1332 const glsl_type
*base_type
= st
->fields
.array
;
1334 /* For arrays of arrays the outer arrays take up a uniform
1335 * slot for each element. The innermost array elements share a
1336 * single slot so we ignore the innermost array when calculating
1339 while (base_type
->fields
.array
->is_array()) {
1340 outer_array_size
= outer_array_size
* base_type
->length
;
1341 base_type
= base_type
->fields
.array
;
1343 offset
+= outer_array_size
;
1345 /* We dont worry about arrays here because unless the array
1346 * contains a structure or another array it only takes up a single
1357 glsl_type::uniform_locations() const
1361 switch (this->base_type
) {
1362 case GLSL_TYPE_UINT
:
1364 case GLSL_TYPE_FLOAT
:
1365 case GLSL_TYPE_DOUBLE
:
1366 case GLSL_TYPE_UINT64
:
1367 case GLSL_TYPE_INT64
:
1368 case GLSL_TYPE_BOOL
:
1369 case GLSL_TYPE_SAMPLER
:
1370 case GLSL_TYPE_IMAGE
:
1371 case GLSL_TYPE_SUBROUTINE
:
1374 case GLSL_TYPE_STRUCT
:
1375 case GLSL_TYPE_INTERFACE
:
1376 for (unsigned i
= 0; i
< this->length
; i
++)
1377 size
+= this->fields
.structure
[i
].type
->uniform_locations();
1379 case GLSL_TYPE_ARRAY
:
1380 return this->length
* this->fields
.array
->uniform_locations();
1387 glsl_type::varying_count() const
1391 switch (this->base_type
) {
1392 case GLSL_TYPE_UINT
:
1394 case GLSL_TYPE_FLOAT
:
1395 case GLSL_TYPE_DOUBLE
:
1396 case GLSL_TYPE_BOOL
:
1397 case GLSL_TYPE_UINT64
:
1398 case GLSL_TYPE_INT64
:
1401 case GLSL_TYPE_STRUCT
:
1402 case GLSL_TYPE_INTERFACE
:
1403 for (unsigned i
= 0; i
< this->length
; i
++)
1404 size
+= this->fields
.structure
[i
].type
->varying_count();
1406 case GLSL_TYPE_ARRAY
:
1407 /* Don't count innermost array elements */
1408 if (this->without_array()->is_record() ||
1409 this->without_array()->is_interface() ||
1410 this->fields
.array
->is_array())
1411 return this->length
* this->fields
.array
->varying_count();
1413 return this->fields
.array
->varying_count();
1415 assert(!"unsupported varying type");
1421 glsl_type::can_implicitly_convert_to(const glsl_type
*desired
,
1422 _mesa_glsl_parse_state
*state
) const
1424 if (this == desired
)
1427 /* GLSL 1.10 and ESSL do not allow implicit conversions. If there is no
1428 * state, we're doing intra-stage function linking where these checks have
1429 * already been done.
1431 if (state
&& (state
->es_shader
|| !state
->is_version(120, 0)))
1434 /* There is no conversion among matrix types. */
1435 if (this->matrix_columns
> 1 || desired
->matrix_columns
> 1)
1438 /* Vector size must match. */
1439 if (this->vector_elements
!= desired
->vector_elements
)
1442 /* int and uint can be converted to float. */
1443 if (desired
->is_float() && this->is_integer())
1446 /* With GLSL 4.0, ARB_gpu_shader5, or MESA_shader_integer_functions, int
1447 * can be converted to uint. Note that state may be NULL here, when
1448 * resolving function calls in the linker. By this time, all the
1449 * state-dependent checks have already happened though, so allow anything
1450 * that's allowed in any shader version.
1452 if ((!state
|| state
->is_version(400, 0) || state
->ARB_gpu_shader5_enable
||
1453 state
->MESA_shader_integer_functions_enable
) &&
1454 desired
->base_type
== GLSL_TYPE_UINT
&& this->base_type
== GLSL_TYPE_INT
)
1457 /* No implicit conversions from double. */
1458 if ((!state
|| state
->has_double()) && this->is_double())
1461 /* Conversions from different types to double. */
1462 if ((!state
|| state
->has_double()) && desired
->is_double()) {
1463 if (this->is_float())
1465 if (this->is_integer())
1473 glsl_type::std140_base_alignment(bool row_major
) const
1475 unsigned N
= is_64bit() ? 8 : 4;
1477 /* (1) If the member is a scalar consuming <N> basic machine units, the
1478 * base alignment is <N>.
1480 * (2) If the member is a two- or four-component vector with components
1481 * consuming <N> basic machine units, the base alignment is 2<N> or
1482 * 4<N>, respectively.
1484 * (3) If the member is a three-component vector with components consuming
1485 * <N> basic machine units, the base alignment is 4<N>.
1487 if (this->is_scalar() || this->is_vector()) {
1488 switch (this->vector_elements
) {
1499 /* (4) If the member is an array of scalars or vectors, the base alignment
1500 * and array stride are set to match the base alignment of a single
1501 * array element, according to rules (1), (2), and (3), and rounded up
1502 * to the base alignment of a vec4. The array may have padding at the
1503 * end; the base offset of the member following the array is rounded up
1504 * to the next multiple of the base alignment.
1506 * (6) If the member is an array of <S> column-major matrices with <C>
1507 * columns and <R> rows, the matrix is stored identically to a row of
1508 * <S>*<C> column vectors with <R> components each, according to rule
1511 * (8) If the member is an array of <S> row-major matrices with <C> columns
1512 * and <R> rows, the matrix is stored identically to a row of <S>*<R>
1513 * row vectors with <C> components each, according to rule (4).
1515 * (10) If the member is an array of <S> structures, the <S> elements of
1516 * the array are laid out in order, according to rule (9).
1518 if (this->is_array()) {
1519 if (this->fields
.array
->is_scalar() ||
1520 this->fields
.array
->is_vector() ||
1521 this->fields
.array
->is_matrix()) {
1522 return MAX2(this->fields
.array
->std140_base_alignment(row_major
), 16);
1524 assert(this->fields
.array
->is_record() ||
1525 this->fields
.array
->is_array());
1526 return this->fields
.array
->std140_base_alignment(row_major
);
1530 /* (5) If the member is a column-major matrix with <C> columns and
1531 * <R> rows, the matrix is stored identically to an array of
1532 * <C> column vectors with <R> components each, according to
1535 * (7) If the member is a row-major matrix with <C> columns and <R>
1536 * rows, the matrix is stored identically to an array of <R>
1537 * row vectors with <C> components each, according to rule (4).
1539 if (this->is_matrix()) {
1540 const struct glsl_type
*vec_type
, *array_type
;
1541 int c
= this->matrix_columns
;
1542 int r
= this->vector_elements
;
1545 vec_type
= get_instance(base_type
, c
, 1);
1546 array_type
= glsl_type::get_array_instance(vec_type
, r
);
1548 vec_type
= get_instance(base_type
, r
, 1);
1549 array_type
= glsl_type::get_array_instance(vec_type
, c
);
1552 return array_type
->std140_base_alignment(false);
1555 /* (9) If the member is a structure, the base alignment of the
1556 * structure is <N>, where <N> is the largest base alignment
1557 * value of any of its members, and rounded up to the base
1558 * alignment of a vec4. The individual members of this
1559 * sub-structure are then assigned offsets by applying this set
1560 * of rules recursively, where the base offset of the first
1561 * member of the sub-structure is equal to the aligned offset
1562 * of the structure. The structure may have padding at the end;
1563 * the base offset of the member following the sub-structure is
1564 * rounded up to the next multiple of the base alignment of the
1567 if (this->is_record()) {
1568 unsigned base_alignment
= 16;
1569 for (unsigned i
= 0; i
< this->length
; i
++) {
1570 bool field_row_major
= row_major
;
1571 const enum glsl_matrix_layout matrix_layout
=
1572 glsl_matrix_layout(this->fields
.structure
[i
].matrix_layout
);
1573 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
1574 field_row_major
= true;
1575 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
1576 field_row_major
= false;
1579 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
1580 base_alignment
= MAX2(base_alignment
,
1581 field_type
->std140_base_alignment(field_row_major
));
1583 return base_alignment
;
1586 assert(!"not reached");
1591 glsl_type::std140_size(bool row_major
) const
1593 unsigned N
= is_64bit() ? 8 : 4;
1595 /* (1) If the member is a scalar consuming <N> basic machine units, the
1596 * base alignment is <N>.
1598 * (2) If the member is a two- or four-component vector with components
1599 * consuming <N> basic machine units, the base alignment is 2<N> or
1600 * 4<N>, respectively.
1602 * (3) If the member is a three-component vector with components consuming
1603 * <N> basic machine units, the base alignment is 4<N>.
1605 if (this->is_scalar() || this->is_vector()) {
1606 return this->vector_elements
* N
;
1609 /* (5) If the member is a column-major matrix with <C> columns and
1610 * <R> rows, the matrix is stored identically to an array of
1611 * <C> column vectors with <R> components each, according to
1614 * (6) If the member is an array of <S> column-major matrices with <C>
1615 * columns and <R> rows, the matrix is stored identically to a row of
1616 * <S>*<C> column vectors with <R> components each, according to rule
1619 * (7) If the member is a row-major matrix with <C> columns and <R>
1620 * rows, the matrix is stored identically to an array of <R>
1621 * row vectors with <C> components each, according to rule (4).
1623 * (8) If the member is an array of <S> row-major matrices with <C> columns
1624 * and <R> rows, the matrix is stored identically to a row of <S>*<R>
1625 * row vectors with <C> components each, according to rule (4).
1627 if (this->without_array()->is_matrix()) {
1628 const struct glsl_type
*element_type
;
1629 const struct glsl_type
*vec_type
;
1630 unsigned int array_len
;
1632 if (this->is_array()) {
1633 element_type
= this->without_array();
1634 array_len
= this->arrays_of_arrays_size();
1636 element_type
= this;
1641 vec_type
= get_instance(element_type
->base_type
,
1642 element_type
->matrix_columns
, 1);
1644 array_len
*= element_type
->vector_elements
;
1646 vec_type
= get_instance(element_type
->base_type
,
1647 element_type
->vector_elements
, 1);
1648 array_len
*= element_type
->matrix_columns
;
1650 const glsl_type
*array_type
= glsl_type::get_array_instance(vec_type
,
1653 return array_type
->std140_size(false);
1656 /* (4) If the member is an array of scalars or vectors, the base alignment
1657 * and array stride are set to match the base alignment of a single
1658 * array element, according to rules (1), (2), and (3), and rounded up
1659 * to the base alignment of a vec4. The array may have padding at the
1660 * end; the base offset of the member following the array is rounded up
1661 * to the next multiple of the base alignment.
1663 * (10) If the member is an array of <S> structures, the <S> elements of
1664 * the array are laid out in order, according to rule (9).
1666 if (this->is_array()) {
1667 if (this->without_array()->is_record()) {
1668 return this->arrays_of_arrays_size() *
1669 this->without_array()->std140_size(row_major
);
1671 unsigned element_base_align
=
1672 this->without_array()->std140_base_alignment(row_major
);
1673 return this->arrays_of_arrays_size() * MAX2(element_base_align
, 16);
1677 /* (9) If the member is a structure, the base alignment of the
1678 * structure is <N>, where <N> is the largest base alignment
1679 * value of any of its members, and rounded up to the base
1680 * alignment of a vec4. The individual members of this
1681 * sub-structure are then assigned offsets by applying this set
1682 * of rules recursively, where the base offset of the first
1683 * member of the sub-structure is equal to the aligned offset
1684 * of the structure. The structure may have padding at the end;
1685 * the base offset of the member following the sub-structure is
1686 * rounded up to the next multiple of the base alignment of the
1689 if (this->is_record() || this->is_interface()) {
1691 unsigned max_align
= 0;
1693 for (unsigned i
= 0; i
< this->length
; i
++) {
1694 bool field_row_major
= row_major
;
1695 const enum glsl_matrix_layout matrix_layout
=
1696 glsl_matrix_layout(this->fields
.structure
[i
].matrix_layout
);
1697 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
1698 field_row_major
= true;
1699 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
1700 field_row_major
= false;
1703 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
1704 unsigned align
= field_type
->std140_base_alignment(field_row_major
);
1706 /* Ignore unsized arrays when calculating size */
1707 if (field_type
->is_unsized_array())
1710 size
= glsl_align(size
, align
);
1711 size
+= field_type
->std140_size(field_row_major
);
1713 max_align
= MAX2(align
, max_align
);
1715 if (field_type
->is_record() && (i
+ 1 < this->length
))
1716 size
= glsl_align(size
, 16);
1718 size
= glsl_align(size
, MAX2(max_align
, 16));
1722 assert(!"not reached");
1727 glsl_type::std430_base_alignment(bool row_major
) const
1730 unsigned N
= is_64bit() ? 8 : 4;
1732 /* (1) If the member is a scalar consuming <N> basic machine units, the
1733 * base alignment is <N>.
1735 * (2) If the member is a two- or four-component vector with components
1736 * consuming <N> basic machine units, the base alignment is 2<N> or
1737 * 4<N>, respectively.
1739 * (3) If the member is a three-component vector with components consuming
1740 * <N> basic machine units, the base alignment is 4<N>.
1742 if (this->is_scalar() || this->is_vector()) {
1743 switch (this->vector_elements
) {
1754 /* OpenGL 4.30 spec, section 7.6.2.2 "Standard Uniform Block Layout":
1756 * "When using the std430 storage layout, shader storage blocks will be
1757 * laid out in buffer storage identically to uniform and shader storage
1758 * blocks using the std140 layout, except that the base alignment and
1759 * stride of arrays of scalars and vectors in rule 4 and of structures
1760 * in rule 9 are not rounded up a multiple of the base alignment of a vec4.
1763 /* (1) If the member is a scalar consuming <N> basic machine units, the
1764 * base alignment is <N>.
1766 * (2) If the member is a two- or four-component vector with components
1767 * consuming <N> basic machine units, the base alignment is 2<N> or
1768 * 4<N>, respectively.
1770 * (3) If the member is a three-component vector with components consuming
1771 * <N> basic machine units, the base alignment is 4<N>.
1773 if (this->is_array())
1774 return this->fields
.array
->std430_base_alignment(row_major
);
1776 /* (5) If the member is a column-major matrix with <C> columns and
1777 * <R> rows, the matrix is stored identically to an array of
1778 * <C> column vectors with <R> components each, according to
1781 * (7) If the member is a row-major matrix with <C> columns and <R>
1782 * rows, the matrix is stored identically to an array of <R>
1783 * row vectors with <C> components each, according to rule (4).
1785 if (this->is_matrix()) {
1786 const struct glsl_type
*vec_type
, *array_type
;
1787 int c
= this->matrix_columns
;
1788 int r
= this->vector_elements
;
1791 vec_type
= get_instance(base_type
, c
, 1);
1792 array_type
= glsl_type::get_array_instance(vec_type
, r
);
1794 vec_type
= get_instance(base_type
, r
, 1);
1795 array_type
= glsl_type::get_array_instance(vec_type
, c
);
1798 return array_type
->std430_base_alignment(false);
1801 /* (9) If the member is a structure, the base alignment of the
1802 * structure is <N>, where <N> is the largest base alignment
1803 * value of any of its members, and rounded up to the base
1804 * alignment of a vec4. The individual members of this
1805 * sub-structure are then assigned offsets by applying this set
1806 * of rules recursively, where the base offset of the first
1807 * member of the sub-structure is equal to the aligned offset
1808 * of the structure. The structure may have padding at the end;
1809 * the base offset of the member following the sub-structure is
1810 * rounded up to the next multiple of the base alignment of the
1813 if (this->is_record()) {
1814 unsigned base_alignment
= 0;
1815 for (unsigned i
= 0; i
< this->length
; i
++) {
1816 bool field_row_major
= row_major
;
1817 const enum glsl_matrix_layout matrix_layout
=
1818 glsl_matrix_layout(this->fields
.structure
[i
].matrix_layout
);
1819 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
1820 field_row_major
= true;
1821 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
1822 field_row_major
= false;
1825 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
1826 base_alignment
= MAX2(base_alignment
,
1827 field_type
->std430_base_alignment(field_row_major
));
1829 assert(base_alignment
> 0);
1830 return base_alignment
;
1832 assert(!"not reached");
1837 glsl_type::std430_array_stride(bool row_major
) const
1839 unsigned N
= is_64bit() ? 8 : 4;
1841 /* Notice that the array stride of a vec3 is not 3 * N but 4 * N.
1842 * See OpenGL 4.30 spec, section 7.6.2.2 "Standard Uniform Block Layout"
1844 * (3) If the member is a three-component vector with components consuming
1845 * <N> basic machine units, the base alignment is 4<N>.
1847 if (this->is_vector() && this->vector_elements
== 3)
1850 /* By default use std430_size(row_major) */
1851 return this->std430_size(row_major
);
1855 glsl_type::std430_size(bool row_major
) const
1857 unsigned N
= is_64bit() ? 8 : 4;
1859 /* OpenGL 4.30 spec, section 7.6.2.2 "Standard Uniform Block Layout":
1861 * "When using the std430 storage layout, shader storage blocks will be
1862 * laid out in buffer storage identically to uniform and shader storage
1863 * blocks using the std140 layout, except that the base alignment and
1864 * stride of arrays of scalars and vectors in rule 4 and of structures
1865 * in rule 9 are not rounded up a multiple of the base alignment of a vec4.
1867 if (this->is_scalar() || this->is_vector())
1868 return this->vector_elements
* N
;
1870 if (this->without_array()->is_matrix()) {
1871 const struct glsl_type
*element_type
;
1872 const struct glsl_type
*vec_type
;
1873 unsigned int array_len
;
1875 if (this->is_array()) {
1876 element_type
= this->without_array();
1877 array_len
= this->arrays_of_arrays_size();
1879 element_type
= this;
1884 vec_type
= get_instance(element_type
->base_type
,
1885 element_type
->matrix_columns
, 1);
1887 array_len
*= element_type
->vector_elements
;
1889 vec_type
= get_instance(element_type
->base_type
,
1890 element_type
->vector_elements
, 1);
1891 array_len
*= element_type
->matrix_columns
;
1893 const glsl_type
*array_type
= glsl_type::get_array_instance(vec_type
,
1896 return array_type
->std430_size(false);
1899 if (this->is_array()) {
1900 if (this->without_array()->is_record())
1901 return this->arrays_of_arrays_size() *
1902 this->without_array()->std430_size(row_major
);
1904 return this->arrays_of_arrays_size() *
1905 this->without_array()->std430_base_alignment(row_major
);
1908 if (this->is_record() || this->is_interface()) {
1910 unsigned max_align
= 0;
1912 for (unsigned i
= 0; i
< this->length
; i
++) {
1913 bool field_row_major
= row_major
;
1914 const enum glsl_matrix_layout matrix_layout
=
1915 glsl_matrix_layout(this->fields
.structure
[i
].matrix_layout
);
1916 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
1917 field_row_major
= true;
1918 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
1919 field_row_major
= false;
1922 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
1923 unsigned align
= field_type
->std430_base_alignment(field_row_major
);
1924 size
= glsl_align(size
, align
);
1925 size
+= field_type
->std430_size(field_row_major
);
1927 max_align
= MAX2(align
, max_align
);
1929 size
= glsl_align(size
, max_align
);
1933 assert(!"not reached");
1938 glsl_type::count_attribute_slots(bool is_vertex_input
) const
1940 /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
1942 * "A scalar input counts the same amount against this limit as a vec4,
1943 * so applications may want to consider packing groups of four
1944 * unrelated float inputs together into a vector to better utilize the
1945 * capabilities of the underlying hardware. A matrix input will use up
1946 * multiple locations. The number of locations used will equal the
1947 * number of columns in the matrix."
1949 * The spec does not explicitly say how arrays are counted. However, it
1950 * should be safe to assume the total number of slots consumed by an array
1951 * is the number of entries in the array multiplied by the number of slots
1952 * consumed by a single element of the array.
1954 * The spec says nothing about how structs are counted, because vertex
1955 * attributes are not allowed to be (or contain) structs. However, Mesa
1956 * allows varying structs, the number of varying slots taken up by a
1957 * varying struct is simply equal to the sum of the number of slots taken
1958 * up by each element.
1960 * Doubles are counted different depending on whether they are vertex
1961 * inputs or everything else. Vertex inputs from ARB_vertex_attrib_64bit
1962 * take one location no matter what size they are, otherwise dvec3/4
1963 * take two locations.
1965 switch (this->base_type
) {
1966 case GLSL_TYPE_UINT
:
1968 case GLSL_TYPE_FLOAT
:
1969 case GLSL_TYPE_BOOL
:
1970 return this->matrix_columns
;
1971 case GLSL_TYPE_DOUBLE
:
1972 case GLSL_TYPE_UINT64
:
1973 case GLSL_TYPE_INT64
:
1974 if (this->vector_elements
> 2 && !is_vertex_input
)
1975 return this->matrix_columns
* 2;
1977 return this->matrix_columns
;
1978 case GLSL_TYPE_STRUCT
:
1979 case GLSL_TYPE_INTERFACE
: {
1982 for (unsigned i
= 0; i
< this->length
; i
++)
1983 size
+= this->fields
.structure
[i
].type
->count_attribute_slots(is_vertex_input
);
1988 case GLSL_TYPE_ARRAY
:
1989 return this->length
* this->fields
.array
->count_attribute_slots(is_vertex_input
);
1991 case GLSL_TYPE_FUNCTION
:
1992 case GLSL_TYPE_SAMPLER
:
1993 case GLSL_TYPE_IMAGE
:
1994 case GLSL_TYPE_ATOMIC_UINT
:
1995 case GLSL_TYPE_VOID
:
1996 case GLSL_TYPE_SUBROUTINE
:
1997 case GLSL_TYPE_ERROR
:
2001 assert(!"Unexpected type in count_attribute_slots()");
2007 glsl_type::coordinate_components() const
2011 switch (sampler_dimensionality
) {
2012 case GLSL_SAMPLER_DIM_1D
:
2013 case GLSL_SAMPLER_DIM_BUF
:
2016 case GLSL_SAMPLER_DIM_2D
:
2017 case GLSL_SAMPLER_DIM_RECT
:
2018 case GLSL_SAMPLER_DIM_MS
:
2019 case GLSL_SAMPLER_DIM_EXTERNAL
:
2020 case GLSL_SAMPLER_DIM_SUBPASS
:
2023 case GLSL_SAMPLER_DIM_3D
:
2024 case GLSL_SAMPLER_DIM_CUBE
:
2028 assert(!"Should not get here.");
2033 /* Array textures need an additional component for the array index, except
2034 * for cubemap array images that behave like a 2D array of interleaved
2037 if (sampler_array
&&
2038 !(is_image() && sampler_dimensionality
== GLSL_SAMPLER_DIM_CUBE
))
2045 * Declarations of type flyweights (glsl_type::_foo_type) and
2046 * convenience pointers (glsl_type::foo_type).
2049 #define DECL_TYPE(NAME, ...) \
2050 const glsl_type glsl_type::_##NAME##_type = glsl_type(__VA_ARGS__, #NAME); \
2051 const glsl_type *const glsl_type::NAME##_type = &glsl_type::_##NAME##_type;
2053 #define STRUCT_TYPE(NAME)
2055 #include "compiler/builtin_type_macros.h"