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::hash_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
;
38 glsl_type::glsl_type(GLenum gl_type
,
39 glsl_base_type base_type
, unsigned vector_elements
,
40 unsigned matrix_columns
, const char *name
) :
42 base_type(base_type
), sampled_type(GLSL_TYPE_VOID
),
43 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
44 interface_packing(0), interface_row_major(0),
45 vector_elements(vector_elements
), matrix_columns(matrix_columns
),
48 /* Values of these types must fit in the two bits of
49 * glsl_type::sampled_type.
51 STATIC_ASSERT((unsigned(GLSL_TYPE_UINT
) & 3) == unsigned(GLSL_TYPE_UINT
));
52 STATIC_ASSERT((unsigned(GLSL_TYPE_INT
) & 3) == unsigned(GLSL_TYPE_INT
));
53 STATIC_ASSERT((unsigned(GLSL_TYPE_FLOAT
) & 3) == unsigned(GLSL_TYPE_FLOAT
));
55 ASSERT_BITFIELD_SIZE(glsl_type
, base_type
, GLSL_TYPE_ERROR
);
56 ASSERT_BITFIELD_SIZE(glsl_type
, sampled_type
, GLSL_TYPE_ERROR
);
57 ASSERT_BITFIELD_SIZE(glsl_type
, sampler_dimensionality
,
58 GLSL_SAMPLER_DIM_SUBPASS_MS
);
60 this->mem_ctx
= ralloc_context(NULL
);
61 assert(this->mem_ctx
!= NULL
);
64 this->name
= ralloc_strdup(this->mem_ctx
, name
);
66 /* Neither dimension is zero or both dimensions are zero.
68 assert((vector_elements
== 0) == (matrix_columns
== 0));
69 memset(& fields
, 0, sizeof(fields
));
72 glsl_type::glsl_type(GLenum gl_type
, glsl_base_type base_type
,
73 enum glsl_sampler_dim dim
, bool shadow
, bool array
,
74 glsl_base_type type
, const char *name
) :
76 base_type(base_type
), sampled_type(type
),
77 sampler_dimensionality(dim
), sampler_shadow(shadow
),
78 sampler_array(array
), interface_packing(0),
79 interface_row_major(0), length(0)
81 this->mem_ctx
= ralloc_context(NULL
);
82 assert(this->mem_ctx
!= NULL
);
85 this->name
= ralloc_strdup(this->mem_ctx
, name
);
87 memset(& fields
, 0, sizeof(fields
));
89 matrix_columns
= vector_elements
= 1;
92 glsl_type::glsl_type(const glsl_struct_field
*fields
, unsigned num_fields
,
95 base_type(GLSL_TYPE_STRUCT
), sampled_type(GLSL_TYPE_VOID
),
96 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
97 interface_packing(0), interface_row_major(0),
98 vector_elements(0), matrix_columns(0),
103 this->mem_ctx
= ralloc_context(NULL
);
104 assert(this->mem_ctx
!= NULL
);
106 assert(name
!= NULL
);
107 this->name
= ralloc_strdup(this->mem_ctx
, name
);
108 this->fields
.structure
= ralloc_array(this->mem_ctx
,
109 glsl_struct_field
, length
);
111 for (i
= 0; i
< length
; i
++) {
112 this->fields
.structure
[i
] = fields
[i
];
113 this->fields
.structure
[i
].name
= ralloc_strdup(this->fields
.structure
,
118 glsl_type::glsl_type(const glsl_struct_field
*fields
, unsigned num_fields
,
119 enum glsl_interface_packing packing
,
120 bool row_major
, const char *name
) :
122 base_type(GLSL_TYPE_INTERFACE
), sampled_type(GLSL_TYPE_VOID
),
123 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
124 interface_packing((unsigned) packing
),
125 interface_row_major((unsigned) row_major
),
126 vector_elements(0), matrix_columns(0),
131 this->mem_ctx
= ralloc_context(NULL
);
132 assert(this->mem_ctx
!= NULL
);
134 assert(name
!= NULL
);
135 this->name
= ralloc_strdup(this->mem_ctx
, name
);
136 this->fields
.structure
= rzalloc_array(this->mem_ctx
,
137 glsl_struct_field
, length
);
138 for (i
= 0; i
< length
; i
++) {
139 this->fields
.structure
[i
] = fields
[i
];
140 this->fields
.structure
[i
].name
= ralloc_strdup(this->fields
.structure
,
145 glsl_type::glsl_type(const glsl_type
*return_type
,
146 const glsl_function_param
*params
, unsigned num_params
) :
148 base_type(GLSL_TYPE_FUNCTION
), sampled_type(GLSL_TYPE_VOID
),
149 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
150 interface_packing(0), interface_row_major(0),
151 vector_elements(0), matrix_columns(0),
156 this->mem_ctx
= ralloc_context(NULL
);
157 assert(this->mem_ctx
!= NULL
);
159 this->fields
.parameters
= rzalloc_array(this->mem_ctx
,
160 glsl_function_param
, num_params
+ 1);
162 /* We store the return type as the first parameter */
163 this->fields
.parameters
[0].type
= return_type
;
164 this->fields
.parameters
[0].in
= false;
165 this->fields
.parameters
[0].out
= true;
167 /* We store the i'th parameter in slot i+1 */
168 for (i
= 0; i
< length
; i
++) {
169 this->fields
.parameters
[i
+ 1].type
= params
[i
].type
;
170 this->fields
.parameters
[i
+ 1].in
= params
[i
].in
;
171 this->fields
.parameters
[i
+ 1].out
= params
[i
].out
;
175 glsl_type::glsl_type(const char *subroutine_name
) :
177 base_type(GLSL_TYPE_SUBROUTINE
), sampled_type(GLSL_TYPE_VOID
),
178 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
179 interface_packing(0), interface_row_major(0),
180 vector_elements(1), matrix_columns(1),
183 this->mem_ctx
= ralloc_context(NULL
);
184 assert(this->mem_ctx
!= NULL
);
186 assert(subroutine_name
!= NULL
);
187 this->name
= ralloc_strdup(this->mem_ctx
, subroutine_name
);
190 glsl_type::~glsl_type()
192 ralloc_free(this->mem_ctx
);
196 glsl_type::contains_sampler() const
198 if (this->is_array()) {
199 return this->fields
.array
->contains_sampler();
200 } else if (this->is_record() || this->is_interface()) {
201 for (unsigned int i
= 0; i
< this->length
; i
++) {
202 if (this->fields
.structure
[i
].type
->contains_sampler())
207 return this->is_sampler();
212 glsl_type::contains_array() const
214 if (this->is_record() || this->is_interface()) {
215 for (unsigned int i
= 0; i
< this->length
; i
++) {
216 if (this->fields
.structure
[i
].type
->contains_array())
221 return this->is_array();
226 glsl_type::contains_integer() const
228 if (this->is_array()) {
229 return this->fields
.array
->contains_integer();
230 } else if (this->is_record() || this->is_interface()) {
231 for (unsigned int i
= 0; i
< this->length
; i
++) {
232 if (this->fields
.structure
[i
].type
->contains_integer())
237 return this->is_integer();
242 glsl_type::contains_double() const
244 if (this->is_array()) {
245 return this->fields
.array
->contains_double();
246 } else if (this->is_record() || this->is_interface()) {
247 for (unsigned int i
= 0; i
< this->length
; i
++) {
248 if (this->fields
.structure
[i
].type
->contains_double())
253 return this->is_double();
258 glsl_type::contains_opaque() const {
260 case GLSL_TYPE_SAMPLER
:
261 case GLSL_TYPE_IMAGE
:
262 case GLSL_TYPE_ATOMIC_UINT
:
264 case GLSL_TYPE_ARRAY
:
265 return fields
.array
->contains_opaque();
266 case GLSL_TYPE_STRUCT
:
267 case GLSL_TYPE_INTERFACE
:
268 for (unsigned int i
= 0; i
< length
; i
++) {
269 if (fields
.structure
[i
].type
->contains_opaque())
279 glsl_type::contains_subroutine() const
281 if (this->is_array()) {
282 return this->fields
.array
->contains_subroutine();
283 } else if (this->is_record() || this->is_interface()) {
284 for (unsigned int i
= 0; i
< this->length
; i
++) {
285 if (this->fields
.structure
[i
].type
->contains_subroutine())
290 return this->is_subroutine();
295 glsl_type::sampler_index() const
297 const glsl_type
*const t
= (this->is_array()) ? this->fields
.array
: this;
299 assert(t
->is_sampler() || t
->is_image());
301 switch (t
->sampler_dimensionality
) {
302 case GLSL_SAMPLER_DIM_1D
:
303 return (t
->sampler_array
) ? TEXTURE_1D_ARRAY_INDEX
: TEXTURE_1D_INDEX
;
304 case GLSL_SAMPLER_DIM_2D
:
305 return (t
->sampler_array
) ? TEXTURE_2D_ARRAY_INDEX
: TEXTURE_2D_INDEX
;
306 case GLSL_SAMPLER_DIM_3D
:
307 return TEXTURE_3D_INDEX
;
308 case GLSL_SAMPLER_DIM_CUBE
:
309 return (t
->sampler_array
) ? TEXTURE_CUBE_ARRAY_INDEX
: TEXTURE_CUBE_INDEX
;
310 case GLSL_SAMPLER_DIM_RECT
:
311 return TEXTURE_RECT_INDEX
;
312 case GLSL_SAMPLER_DIM_BUF
:
313 return TEXTURE_BUFFER_INDEX
;
314 case GLSL_SAMPLER_DIM_EXTERNAL
:
315 return TEXTURE_EXTERNAL_INDEX
;
316 case GLSL_SAMPLER_DIM_MS
:
317 return (t
->sampler_array
) ? TEXTURE_2D_MULTISAMPLE_ARRAY_INDEX
: TEXTURE_2D_MULTISAMPLE_INDEX
;
319 assert(!"Should not get here.");
320 return TEXTURE_BUFFER_INDEX
;
325 glsl_type::contains_image() const
327 if (this->is_array()) {
328 return this->fields
.array
->contains_image();
329 } else if (this->is_record() || this->is_interface()) {
330 for (unsigned int i
= 0; i
< this->length
; i
++) {
331 if (this->fields
.structure
[i
].type
->contains_image())
336 return this->is_image();
340 const glsl_type
*glsl_type::get_base_type() const
345 case GLSL_TYPE_UINT16
:
346 return uint16_t_type
;
349 case GLSL_TYPE_INT16
:
351 case GLSL_TYPE_FLOAT
:
353 case GLSL_TYPE_FLOAT16
:
354 return float16_t_type
;
355 case GLSL_TYPE_DOUBLE
:
359 case GLSL_TYPE_UINT64
:
360 return uint64_t_type
;
361 case GLSL_TYPE_INT64
:
369 const glsl_type
*glsl_type::get_scalar_type() const
371 const glsl_type
*type
= this;
374 while (type
->base_type
== GLSL_TYPE_ARRAY
)
375 type
= type
->fields
.array
;
377 /* Handle vectors and matrices */
378 switch (type
->base_type
) {
381 case GLSL_TYPE_UINT16
:
382 return uint16_t_type
;
385 case GLSL_TYPE_INT16
:
387 case GLSL_TYPE_FLOAT
:
389 case GLSL_TYPE_FLOAT16
:
390 return float16_t_type
;
391 case GLSL_TYPE_DOUBLE
:
395 case GLSL_TYPE_UINT64
:
396 return uint64_t_type
;
397 case GLSL_TYPE_INT64
:
400 /* Handle everything else */
407 hash_free_type_function(struct hash_entry
*entry
)
409 glsl_type
*type
= (glsl_type
*) entry
->data
;
411 if (type
->is_array())
412 free((void*)entry
->key
);
418 _mesa_glsl_release_types(void)
420 /* Should only be called during atexit (either when unloading shared
421 * object, or if process terminates), so no mutex-locking should be
424 if (glsl_type::array_types
!= NULL
) {
425 _mesa_hash_table_destroy(glsl_type::array_types
, hash_free_type_function
);
426 glsl_type::array_types
= NULL
;
429 if (glsl_type::record_types
!= NULL
) {
430 _mesa_hash_table_destroy(glsl_type::record_types
, hash_free_type_function
);
431 glsl_type::record_types
= NULL
;
434 if (glsl_type::interface_types
!= NULL
) {
435 _mesa_hash_table_destroy(glsl_type::interface_types
, hash_free_type_function
);
436 glsl_type::interface_types
= NULL
;
439 if (glsl_type::function_types
!= NULL
) {
440 _mesa_hash_table_destroy(glsl_type::function_types
, hash_free_type_function
);
441 glsl_type::function_types
= NULL
;
444 if (glsl_type::subroutine_types
!= NULL
) {
445 _mesa_hash_table_destroy(glsl_type::subroutine_types
, hash_free_type_function
);
446 glsl_type::subroutine_types
= NULL
;
451 glsl_type::glsl_type(const glsl_type
*array
, unsigned length
) :
452 base_type(GLSL_TYPE_ARRAY
), sampled_type(GLSL_TYPE_VOID
),
453 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
454 interface_packing(0), interface_row_major(0),
455 vector_elements(0), matrix_columns(0),
456 length(length
), name(NULL
)
458 this->fields
.array
= array
;
459 /* Inherit the gl type of the base. The GL type is used for
460 * uniform/statevar handling in Mesa and the arrayness of the type
461 * is represented by the size rather than the type.
463 this->gl_type
= array
->gl_type
;
465 /* Allow a maximum of 10 characters for the array size. This is enough
466 * for 32-bits of ~0. The extra 3 are for the '[', ']', and terminating
469 const unsigned name_length
= strlen(array
->name
) + 10 + 3;
471 this->mem_ctx
= ralloc_context(NULL
);
472 assert(this->mem_ctx
!= NULL
);
474 char *const n
= (char *) ralloc_size(this->mem_ctx
, name_length
);
477 snprintf(n
, name_length
, "%s[]", array
->name
);
479 /* insert outermost dimensions in the correct spot
480 * otherwise the dimension order will be backwards
482 const char *pos
= strchr(array
->name
, '[');
484 int idx
= pos
- array
->name
;
485 snprintf(n
, idx
+1, "%s", array
->name
);
486 snprintf(n
+ idx
, name_length
- idx
, "[%u]%s",
487 length
, array
->name
+ idx
);
489 snprintf(n
, name_length
, "%s[%u]", array
->name
, length
);
497 glsl_type::vec(unsigned components
, const glsl_type
*const ts
[])
499 unsigned n
= components
;
503 else if (components
== 16)
512 #define VECN(components, sname, vname) \
514 glsl_type:: vname (unsigned components) \
516 static const glsl_type *const ts[] = { \
517 sname ## _type, vname ## 2_type, \
518 vname ## 3_type, vname ## 4_type, \
519 vname ## 8_type, vname ## 16_type, \
521 return glsl_type::vec(components, ts); \
524 VECN(components
, float, vec
)
525 VECN(components
, float16_t
, f16vec
)
526 VECN(components
, double, dvec
)
527 VECN(components
, int, ivec
)
528 VECN(components
, uint
, uvec
)
529 VECN(components
, bool, bvec
)
530 VECN(components
, int64_t, i64vec
)
531 VECN(components
, uint64_t, u64vec
)
532 VECN(components
, int16_t, i16vec
)
533 VECN(components
, uint16_t, u16vec
)
534 VECN(components
, int8_t, i8vec
)
535 VECN(components
, uint8_t, u8vec
)
538 glsl_type::get_instance(unsigned base_type
, unsigned rows
, unsigned columns
)
540 if (base_type
== GLSL_TYPE_VOID
)
543 /* Treat GLSL vectors as Nx1 matrices.
551 case GLSL_TYPE_FLOAT
:
553 case GLSL_TYPE_FLOAT16
:
555 case GLSL_TYPE_DOUBLE
:
559 case GLSL_TYPE_UINT64
:
561 case GLSL_TYPE_INT64
:
563 case GLSL_TYPE_UINT16
:
565 case GLSL_TYPE_INT16
:
567 case GLSL_TYPE_UINT8
:
575 if ((base_type
!= GLSL_TYPE_FLOAT
&&
576 base_type
!= GLSL_TYPE_DOUBLE
&&
577 base_type
!= GLSL_TYPE_FLOAT16
) || (rows
== 1))
580 /* GLSL matrix types are named mat{COLUMNS}x{ROWS}. Only the following
581 * combinations are valid:
589 #define IDX(c,r) (((c-1)*3) + (r-1))
592 case GLSL_TYPE_DOUBLE
: {
593 switch (IDX(columns
, rows
)) {
594 case IDX(2,2): return dmat2_type
;
595 case IDX(2,3): return dmat2x3_type
;
596 case IDX(2,4): return dmat2x4_type
;
597 case IDX(3,2): return dmat3x2_type
;
598 case IDX(3,3): return dmat3_type
;
599 case IDX(3,4): return dmat3x4_type
;
600 case IDX(4,2): return dmat4x2_type
;
601 case IDX(4,3): return dmat4x3_type
;
602 case IDX(4,4): return dmat4_type
;
603 default: return error_type
;
606 case GLSL_TYPE_FLOAT
: {
607 switch (IDX(columns
, rows
)) {
608 case IDX(2,2): return mat2_type
;
609 case IDX(2,3): return mat2x3_type
;
610 case IDX(2,4): return mat2x4_type
;
611 case IDX(3,2): return mat3x2_type
;
612 case IDX(3,3): return mat3_type
;
613 case IDX(3,4): return mat3x4_type
;
614 case IDX(4,2): return mat4x2_type
;
615 case IDX(4,3): return mat4x3_type
;
616 case IDX(4,4): return mat4_type
;
617 default: return error_type
;
620 case GLSL_TYPE_FLOAT16
: {
621 switch (IDX(columns
, rows
)) {
622 case IDX(2,2): return f16mat2_type
;
623 case IDX(2,3): return f16mat2x3_type
;
624 case IDX(2,4): return f16mat2x4_type
;
625 case IDX(3,2): return f16mat3x2_type
;
626 case IDX(3,3): return f16mat3_type
;
627 case IDX(3,4): return f16mat3x4_type
;
628 case IDX(4,2): return f16mat4x2_type
;
629 case IDX(4,3): return f16mat4x3_type
;
630 case IDX(4,4): return f16mat4_type
;
631 default: return error_type
;
634 default: return error_type
;
638 assert(!"Should not get here.");
643 glsl_type::get_sampler_instance(enum glsl_sampler_dim dim
,
649 case GLSL_TYPE_FLOAT
:
651 case GLSL_SAMPLER_DIM_1D
:
653 return (array
? sampler1DArrayShadow_type
: sampler1DShadow_type
);
655 return (array
? sampler1DArray_type
: sampler1D_type
);
656 case GLSL_SAMPLER_DIM_2D
:
658 return (array
? sampler2DArrayShadow_type
: sampler2DShadow_type
);
660 return (array
? sampler2DArray_type
: sampler2D_type
);
661 case GLSL_SAMPLER_DIM_3D
:
665 return sampler3D_type
;
666 case GLSL_SAMPLER_DIM_CUBE
:
668 return (array
? samplerCubeArrayShadow_type
: samplerCubeShadow_type
);
670 return (array
? samplerCubeArray_type
: samplerCube_type
);
671 case GLSL_SAMPLER_DIM_RECT
:
675 return sampler2DRectShadow_type
;
677 return sampler2DRect_type
;
678 case GLSL_SAMPLER_DIM_BUF
:
682 return samplerBuffer_type
;
683 case GLSL_SAMPLER_DIM_MS
:
686 return (array
? sampler2DMSArray_type
: sampler2DMS_type
);
687 case GLSL_SAMPLER_DIM_EXTERNAL
:
691 return samplerExternalOES_type
;
692 case GLSL_SAMPLER_DIM_SUBPASS
:
693 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
700 case GLSL_SAMPLER_DIM_1D
:
701 return (array
? isampler1DArray_type
: isampler1D_type
);
702 case GLSL_SAMPLER_DIM_2D
:
703 return (array
? isampler2DArray_type
: isampler2D_type
);
704 case GLSL_SAMPLER_DIM_3D
:
707 return isampler3D_type
;
708 case GLSL_SAMPLER_DIM_CUBE
:
709 return (array
? isamplerCubeArray_type
: isamplerCube_type
);
710 case GLSL_SAMPLER_DIM_RECT
:
713 return isampler2DRect_type
;
714 case GLSL_SAMPLER_DIM_BUF
:
717 return isamplerBuffer_type
;
718 case GLSL_SAMPLER_DIM_MS
:
719 return (array
? isampler2DMSArray_type
: isampler2DMS_type
);
720 case GLSL_SAMPLER_DIM_EXTERNAL
:
722 case GLSL_SAMPLER_DIM_SUBPASS
:
723 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
730 case GLSL_SAMPLER_DIM_1D
:
731 return (array
? usampler1DArray_type
: usampler1D_type
);
732 case GLSL_SAMPLER_DIM_2D
:
733 return (array
? usampler2DArray_type
: usampler2D_type
);
734 case GLSL_SAMPLER_DIM_3D
:
737 return usampler3D_type
;
738 case GLSL_SAMPLER_DIM_CUBE
:
739 return (array
? usamplerCubeArray_type
: usamplerCube_type
);
740 case GLSL_SAMPLER_DIM_RECT
:
743 return usampler2DRect_type
;
744 case GLSL_SAMPLER_DIM_BUF
:
747 return usamplerBuffer_type
;
748 case GLSL_SAMPLER_DIM_MS
:
749 return (array
? usampler2DMSArray_type
: usampler2DMS_type
);
750 case GLSL_SAMPLER_DIM_EXTERNAL
:
752 case GLSL_SAMPLER_DIM_SUBPASS
:
753 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
760 unreachable("switch statement above should be complete");
764 glsl_type::get_image_instance(enum glsl_sampler_dim dim
,
765 bool array
, glsl_base_type type
)
768 case GLSL_TYPE_FLOAT
:
770 case GLSL_SAMPLER_DIM_1D
:
771 return (array
? image1DArray_type
: image1D_type
);
772 case GLSL_SAMPLER_DIM_2D
:
773 return (array
? image2DArray_type
: image2D_type
);
774 case GLSL_SAMPLER_DIM_3D
:
776 case GLSL_SAMPLER_DIM_CUBE
:
777 return (array
? imageCubeArray_type
: imageCube_type
);
778 case GLSL_SAMPLER_DIM_RECT
:
782 return image2DRect_type
;
783 case GLSL_SAMPLER_DIM_BUF
:
787 return imageBuffer_type
;
788 case GLSL_SAMPLER_DIM_MS
:
789 return (array
? image2DMSArray_type
: image2DMS_type
);
790 case GLSL_SAMPLER_DIM_SUBPASS
:
791 return subpassInput_type
;
792 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
793 return subpassInputMS_type
;
794 case GLSL_SAMPLER_DIM_EXTERNAL
:
799 case GLSL_SAMPLER_DIM_1D
:
800 return (array
? iimage1DArray_type
: iimage1D_type
);
801 case GLSL_SAMPLER_DIM_2D
:
802 return (array
? iimage2DArray_type
: iimage2D_type
);
803 case GLSL_SAMPLER_DIM_3D
:
806 return iimage3D_type
;
807 case GLSL_SAMPLER_DIM_CUBE
:
808 return (array
? iimageCubeArray_type
: iimageCube_type
);
809 case GLSL_SAMPLER_DIM_RECT
:
812 return iimage2DRect_type
;
813 case GLSL_SAMPLER_DIM_BUF
:
816 return iimageBuffer_type
;
817 case GLSL_SAMPLER_DIM_MS
:
818 return (array
? iimage2DMSArray_type
: iimage2DMS_type
);
819 case GLSL_SAMPLER_DIM_SUBPASS
:
820 return isubpassInput_type
;
821 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
822 return isubpassInputMS_type
;
823 case GLSL_SAMPLER_DIM_EXTERNAL
:
828 case GLSL_SAMPLER_DIM_1D
:
829 return (array
? uimage1DArray_type
: uimage1D_type
);
830 case GLSL_SAMPLER_DIM_2D
:
831 return (array
? uimage2DArray_type
: uimage2D_type
);
832 case GLSL_SAMPLER_DIM_3D
:
835 return uimage3D_type
;
836 case GLSL_SAMPLER_DIM_CUBE
:
837 return (array
? uimageCubeArray_type
: uimageCube_type
);
838 case GLSL_SAMPLER_DIM_RECT
:
841 return uimage2DRect_type
;
842 case GLSL_SAMPLER_DIM_BUF
:
845 return uimageBuffer_type
;
846 case GLSL_SAMPLER_DIM_MS
:
847 return (array
? uimage2DMSArray_type
: uimage2DMS_type
);
848 case GLSL_SAMPLER_DIM_SUBPASS
:
849 return usubpassInput_type
;
850 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
851 return usubpassInputMS_type
;
852 case GLSL_SAMPLER_DIM_EXTERNAL
:
859 unreachable("switch statement above should be complete");
863 glsl_type::get_array_instance(const glsl_type
*base
, unsigned array_size
)
865 /* Generate a name using the base type pointer in the key. This is
866 * done because the name of the base type may not be unique across
867 * shaders. For example, two shaders may have different record types
871 snprintf(key
, sizeof(key
), "%p[%u]", (void *) base
, array_size
);
873 mtx_lock(&glsl_type::hash_mutex
);
875 if (array_types
== NULL
) {
876 array_types
= _mesa_hash_table_create(NULL
, _mesa_key_hash_string
,
877 _mesa_key_string_equal
);
880 const struct hash_entry
*entry
= _mesa_hash_table_search(array_types
, key
);
882 const glsl_type
*t
= new glsl_type(base
, array_size
);
884 entry
= _mesa_hash_table_insert(array_types
,
889 assert(((glsl_type
*) entry
->data
)->base_type
== GLSL_TYPE_ARRAY
);
890 assert(((glsl_type
*) entry
->data
)->length
== array_size
);
891 assert(((glsl_type
*) entry
->data
)->fields
.array
== base
);
893 mtx_unlock(&glsl_type::hash_mutex
);
895 return (glsl_type
*) entry
->data
;
900 glsl_type::record_compare(const glsl_type
*b
, bool match_locations
) const
902 if (this->length
!= b
->length
)
905 if (this->interface_packing
!= b
->interface_packing
)
908 if (this->interface_row_major
!= b
->interface_row_major
)
911 /* From the GLSL 4.20 specification (Sec 4.2):
913 * "Structures must have the same name, sequence of type names, and
914 * type definitions, and field names to be considered the same type."
916 * GLSL ES behaves the same (Ver 1.00 Sec 4.2.4, Ver 3.00 Sec 4.2.5).
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
].memory_read_only
949 != b
->fields
.structure
[i
].memory_read_only
)
951 if (this->fields
.structure
[i
].memory_write_only
952 != b
->fields
.structure
[i
].memory_write_only
)
954 if (this->fields
.structure
[i
].memory_coherent
955 != b
->fields
.structure
[i
].memory_coherent
)
957 if (this->fields
.structure
[i
].memory_volatile
958 != b
->fields
.structure
[i
].memory_volatile
)
960 if (this->fields
.structure
[i
].memory_restrict
961 != b
->fields
.structure
[i
].memory_restrict
)
963 if (this->fields
.structure
[i
].image_format
964 != b
->fields
.structure
[i
].image_format
)
966 if (this->fields
.structure
[i
].precision
967 != b
->fields
.structure
[i
].precision
)
969 if (this->fields
.structure
[i
].explicit_xfb_buffer
970 != b
->fields
.structure
[i
].explicit_xfb_buffer
)
972 if (this->fields
.structure
[i
].xfb_buffer
973 != b
->fields
.structure
[i
].xfb_buffer
)
975 if (this->fields
.structure
[i
].xfb_stride
976 != b
->fields
.structure
[i
].xfb_stride
)
985 glsl_type::record_key_compare(const void *a
, const void *b
)
987 const glsl_type
*const key1
= (glsl_type
*) a
;
988 const glsl_type
*const key2
= (glsl_type
*) b
;
990 return strcmp(key1
->name
, key2
->name
) == 0 && key1
->record_compare(key2
);
995 * Generate an integer hash value for a glsl_type structure type.
998 glsl_type::record_key_hash(const void *a
)
1000 const glsl_type
*const key
= (glsl_type
*) a
;
1001 uintptr_t hash
= key
->length
;
1004 for (unsigned i
= 0; i
< key
->length
; i
++) {
1005 /* casting pointer to uintptr_t */
1006 hash
= (hash
* 13 ) + (uintptr_t) key
->fields
.structure
[i
].type
;
1009 if (sizeof(hash
) == 8)
1010 retval
= (hash
& 0xffffffff) ^ ((uint64_t) hash
>> 32);
1019 glsl_type::get_record_instance(const glsl_struct_field
*fields
,
1020 unsigned num_fields
,
1023 const glsl_type
key(fields
, num_fields
, name
);
1025 mtx_lock(&glsl_type::hash_mutex
);
1027 if (record_types
== NULL
) {
1028 record_types
= _mesa_hash_table_create(NULL
, record_key_hash
,
1029 record_key_compare
);
1032 const struct hash_entry
*entry
= _mesa_hash_table_search(record_types
,
1034 if (entry
== NULL
) {
1035 const glsl_type
*t
= new glsl_type(fields
, num_fields
, name
);
1037 entry
= _mesa_hash_table_insert(record_types
, t
, (void *) t
);
1040 assert(((glsl_type
*) entry
->data
)->base_type
== GLSL_TYPE_STRUCT
);
1041 assert(((glsl_type
*) entry
->data
)->length
== num_fields
);
1042 assert(strcmp(((glsl_type
*) entry
->data
)->name
, name
) == 0);
1044 mtx_unlock(&glsl_type::hash_mutex
);
1046 return (glsl_type
*) entry
->data
;
1051 glsl_type::get_interface_instance(const glsl_struct_field
*fields
,
1052 unsigned num_fields
,
1053 enum glsl_interface_packing packing
,
1055 const char *block_name
)
1057 const glsl_type
key(fields
, num_fields
, packing
, row_major
, block_name
);
1059 mtx_lock(&glsl_type::hash_mutex
);
1061 if (interface_types
== NULL
) {
1062 interface_types
= _mesa_hash_table_create(NULL
, record_key_hash
,
1063 record_key_compare
);
1066 const struct hash_entry
*entry
= _mesa_hash_table_search(interface_types
,
1068 if (entry
== NULL
) {
1069 const glsl_type
*t
= new glsl_type(fields
, num_fields
,
1070 packing
, row_major
, block_name
);
1072 entry
= _mesa_hash_table_insert(interface_types
, t
, (void *) t
);
1075 assert(((glsl_type
*) entry
->data
)->base_type
== GLSL_TYPE_INTERFACE
);
1076 assert(((glsl_type
*) entry
->data
)->length
== num_fields
);
1077 assert(strcmp(((glsl_type
*) entry
->data
)->name
, block_name
) == 0);
1079 mtx_unlock(&glsl_type::hash_mutex
);
1081 return (glsl_type
*) entry
->data
;
1085 glsl_type::get_subroutine_instance(const char *subroutine_name
)
1087 const glsl_type
key(subroutine_name
);
1089 mtx_lock(&glsl_type::hash_mutex
);
1091 if (subroutine_types
== NULL
) {
1092 subroutine_types
= _mesa_hash_table_create(NULL
, record_key_hash
,
1093 record_key_compare
);
1096 const struct hash_entry
*entry
= _mesa_hash_table_search(subroutine_types
,
1098 if (entry
== NULL
) {
1099 const glsl_type
*t
= new glsl_type(subroutine_name
);
1101 entry
= _mesa_hash_table_insert(subroutine_types
, t
, (void *) t
);
1104 assert(((glsl_type
*) entry
->data
)->base_type
== GLSL_TYPE_SUBROUTINE
);
1105 assert(strcmp(((glsl_type
*) entry
->data
)->name
, subroutine_name
) == 0);
1107 mtx_unlock(&glsl_type::hash_mutex
);
1109 return (glsl_type
*) entry
->data
;
1114 function_key_compare(const void *a
, const void *b
)
1116 const glsl_type
*const key1
= (glsl_type
*) a
;
1117 const glsl_type
*const key2
= (glsl_type
*) b
;
1119 if (key1
->length
!= key2
->length
)
1122 return memcmp(key1
->fields
.parameters
, key2
->fields
.parameters
,
1123 (key1
->length
+ 1) * sizeof(*key1
->fields
.parameters
)) == 0;
1128 function_key_hash(const void *a
)
1130 const glsl_type
*const key
= (glsl_type
*) a
;
1131 return _mesa_hash_data(key
->fields
.parameters
,
1132 (key
->length
+ 1) * sizeof(*key
->fields
.parameters
));
1136 glsl_type::get_function_instance(const glsl_type
*return_type
,
1137 const glsl_function_param
*params
,
1138 unsigned num_params
)
1140 const glsl_type
key(return_type
, params
, num_params
);
1142 mtx_lock(&glsl_type::hash_mutex
);
1144 if (function_types
== NULL
) {
1145 function_types
= _mesa_hash_table_create(NULL
, function_key_hash
,
1146 function_key_compare
);
1149 struct hash_entry
*entry
= _mesa_hash_table_search(function_types
, &key
);
1150 if (entry
== NULL
) {
1151 const glsl_type
*t
= new glsl_type(return_type
, params
, num_params
);
1153 entry
= _mesa_hash_table_insert(function_types
, t
, (void *) t
);
1156 const glsl_type
*t
= (const glsl_type
*)entry
->data
;
1158 assert(t
->base_type
== GLSL_TYPE_FUNCTION
);
1159 assert(t
->length
== num_params
);
1161 mtx_unlock(&glsl_type::hash_mutex
);
1168 glsl_type::get_mul_type(const glsl_type
*type_a
, const glsl_type
*type_b
)
1170 if (type_a
== type_b
) {
1172 } else if (type_a
->is_matrix() && type_b
->is_matrix()) {
1173 /* Matrix multiply. The columns of A must match the rows of B. Given
1174 * the other previously tested constraints, this means the vector type
1175 * of a row from A must be the same as the vector type of a column from
1178 if (type_a
->row_type() == type_b
->column_type()) {
1179 /* The resulting matrix has the number of columns of matrix B and
1180 * the number of rows of matrix A. We get the row count of A by
1181 * looking at the size of a vector that makes up a column. The
1182 * transpose (size of a row) is done for B.
1184 const glsl_type
*const type
=
1185 get_instance(type_a
->base_type
,
1186 type_a
->column_type()->vector_elements
,
1187 type_b
->row_type()->vector_elements
);
1188 assert(type
!= error_type
);
1192 } else if (type_a
->is_matrix()) {
1193 /* A is a matrix and B is a column vector. Columns of A must match
1194 * rows of B. Given the other previously tested constraints, this
1195 * means the vector type of a row from A must be the same as the
1196 * vector the type of B.
1198 if (type_a
->row_type() == type_b
) {
1199 /* The resulting vector has a number of elements equal to
1200 * the number of rows of matrix A. */
1201 const glsl_type
*const type
=
1202 get_instance(type_a
->base_type
,
1203 type_a
->column_type()->vector_elements
,
1205 assert(type
!= error_type
);
1210 assert(type_b
->is_matrix());
1212 /* A is a row vector and B is a matrix. Columns of A must match rows
1213 * of B. Given the other previously tested constraints, this means
1214 * the type of A must be the same as the vector type of a column from
1217 if (type_a
== type_b
->column_type()) {
1218 /* The resulting vector has a number of elements equal to
1219 * the number of columns of matrix B. */
1220 const glsl_type
*const type
=
1221 get_instance(type_a
->base_type
,
1222 type_b
->row_type()->vector_elements
,
1224 assert(type
!= error_type
);
1235 glsl_type::field_type(const char *name
) const
1237 if (this->base_type
!= GLSL_TYPE_STRUCT
1238 && this->base_type
!= GLSL_TYPE_INTERFACE
)
1241 for (unsigned i
= 0; i
< this->length
; i
++) {
1242 if (strcmp(name
, this->fields
.structure
[i
].name
) == 0)
1243 return this->fields
.structure
[i
].type
;
1251 glsl_type::field_index(const char *name
) const
1253 if (this->base_type
!= GLSL_TYPE_STRUCT
1254 && this->base_type
!= GLSL_TYPE_INTERFACE
)
1257 for (unsigned i
= 0; i
< this->length
; i
++) {
1258 if (strcmp(name
, this->fields
.structure
[i
].name
) == 0)
1267 glsl_type::component_slots() const
1269 switch (this->base_type
) {
1270 case GLSL_TYPE_UINT
:
1272 case GLSL_TYPE_UINT8
:
1273 case GLSL_TYPE_INT8
:
1274 case GLSL_TYPE_UINT16
:
1275 case GLSL_TYPE_INT16
:
1276 case GLSL_TYPE_FLOAT
:
1277 case GLSL_TYPE_FLOAT16
:
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_SAMPLER
:
1300 case GLSL_TYPE_IMAGE
:
1303 case GLSL_TYPE_SUBROUTINE
:
1306 case GLSL_TYPE_FUNCTION
:
1307 case GLSL_TYPE_ATOMIC_UINT
:
1308 case GLSL_TYPE_VOID
:
1309 case GLSL_TYPE_ERROR
:
1317 glsl_type::record_location_offset(unsigned length
) const
1319 unsigned offset
= 0;
1320 const glsl_type
*t
= this->without_array();
1321 if (t
->is_record()) {
1322 assert(length
<= t
->length
);
1324 for (unsigned i
= 0; i
< length
; i
++) {
1325 const glsl_type
*st
= t
->fields
.structure
[i
].type
;
1326 const glsl_type
*wa
= st
->without_array();
1327 if (wa
->is_record()) {
1328 unsigned r_offset
= wa
->record_location_offset(wa
->length
);
1329 offset
+= st
->is_array() ?
1330 st
->arrays_of_arrays_size() * r_offset
: r_offset
;
1331 } else if (st
->is_array() && st
->fields
.array
->is_array()) {
1332 unsigned outer_array_size
= st
->length
;
1333 const glsl_type
*base_type
= st
->fields
.array
;
1335 /* For arrays of arrays the outer arrays take up a uniform
1336 * slot for each element. The innermost array elements share a
1337 * single slot so we ignore the innermost array when calculating
1340 while (base_type
->fields
.array
->is_array()) {
1341 outer_array_size
= outer_array_size
* base_type
->length
;
1342 base_type
= base_type
->fields
.array
;
1344 offset
+= outer_array_size
;
1346 /* We dont worry about arrays here because unless the array
1347 * contains a structure or another array it only takes up a single
1358 glsl_type::uniform_locations() const
1362 switch (this->base_type
) {
1363 case GLSL_TYPE_UINT
:
1365 case GLSL_TYPE_FLOAT
:
1366 case GLSL_TYPE_FLOAT16
:
1367 case GLSL_TYPE_DOUBLE
:
1368 case GLSL_TYPE_UINT16
:
1369 case GLSL_TYPE_INT16
:
1370 case GLSL_TYPE_UINT64
:
1371 case GLSL_TYPE_INT64
:
1372 case GLSL_TYPE_BOOL
:
1373 case GLSL_TYPE_SAMPLER
:
1374 case GLSL_TYPE_IMAGE
:
1375 case GLSL_TYPE_SUBROUTINE
:
1378 case GLSL_TYPE_STRUCT
:
1379 case GLSL_TYPE_INTERFACE
:
1380 for (unsigned i
= 0; i
< this->length
; i
++)
1381 size
+= this->fields
.structure
[i
].type
->uniform_locations();
1383 case GLSL_TYPE_ARRAY
:
1384 return this->length
* this->fields
.array
->uniform_locations();
1391 glsl_type::varying_count() const
1395 switch (this->base_type
) {
1396 case GLSL_TYPE_UINT
:
1398 case GLSL_TYPE_FLOAT
:
1399 case GLSL_TYPE_FLOAT16
:
1400 case GLSL_TYPE_DOUBLE
:
1401 case GLSL_TYPE_BOOL
:
1402 case GLSL_TYPE_UINT16
:
1403 case GLSL_TYPE_INT16
:
1404 case GLSL_TYPE_UINT64
:
1405 case GLSL_TYPE_INT64
:
1408 case GLSL_TYPE_STRUCT
:
1409 case GLSL_TYPE_INTERFACE
:
1410 for (unsigned i
= 0; i
< this->length
; i
++)
1411 size
+= this->fields
.structure
[i
].type
->varying_count();
1413 case GLSL_TYPE_ARRAY
:
1414 /* Don't count innermost array elements */
1415 if (this->without_array()->is_record() ||
1416 this->without_array()->is_interface() ||
1417 this->fields
.array
->is_array())
1418 return this->length
* this->fields
.array
->varying_count();
1420 return this->fields
.array
->varying_count();
1422 assert(!"unsupported varying type");
1428 glsl_type::can_implicitly_convert_to(const glsl_type
*desired
,
1429 _mesa_glsl_parse_state
*state
) const
1431 if (this == desired
)
1434 /* GLSL 1.10 and ESSL do not allow implicit conversions. If there is no
1435 * state, we're doing intra-stage function linking where these checks have
1436 * already been done.
1438 if (state
&& (state
->es_shader
|| !state
->is_version(120, 0)))
1441 /* There is no conversion among matrix types. */
1442 if (this->matrix_columns
> 1 || desired
->matrix_columns
> 1)
1445 /* Vector size must match. */
1446 if (this->vector_elements
!= desired
->vector_elements
)
1449 /* int and uint can be converted to float. */
1450 if (desired
->is_float() && this->is_integer())
1453 /* With GLSL 4.0, ARB_gpu_shader5, or MESA_shader_integer_functions, int
1454 * can be converted to uint. Note that state may be NULL here, when
1455 * resolving function calls in the linker. By this time, all the
1456 * state-dependent checks have already happened though, so allow anything
1457 * that's allowed in any shader version.
1459 if ((!state
|| state
->is_version(400, 0) || state
->ARB_gpu_shader5_enable
||
1460 state
->MESA_shader_integer_functions_enable
) &&
1461 desired
->base_type
== GLSL_TYPE_UINT
&& this->base_type
== GLSL_TYPE_INT
)
1464 /* No implicit conversions from double. */
1465 if ((!state
|| state
->has_double()) && this->is_double())
1468 /* Conversions from different types to double. */
1469 if ((!state
|| state
->has_double()) && desired
->is_double()) {
1470 if (this->is_float())
1472 if (this->is_integer())
1480 glsl_type::std140_base_alignment(bool row_major
) const
1482 unsigned N
= is_64bit() ? 8 : 4;
1484 /* (1) If the member is a scalar consuming <N> basic machine units, the
1485 * base alignment is <N>.
1487 * (2) If the member is a two- or four-component vector with components
1488 * consuming <N> basic machine units, the base alignment is 2<N> or
1489 * 4<N>, respectively.
1491 * (3) If the member is a three-component vector with components consuming
1492 * <N> basic machine units, the base alignment is 4<N>.
1494 if (this->is_scalar() || this->is_vector()) {
1495 switch (this->vector_elements
) {
1506 /* (4) If the member is an array of scalars or vectors, the base alignment
1507 * and array stride are set to match the base alignment of a single
1508 * array element, according to rules (1), (2), and (3), and rounded up
1509 * to the base alignment of a vec4. The array may have padding at the
1510 * end; the base offset of the member following the array is rounded up
1511 * to the next multiple of the base alignment.
1513 * (6) If the member is an array of <S> column-major matrices with <C>
1514 * columns and <R> rows, the matrix is stored identically to a row of
1515 * <S>*<C> column vectors with <R> components each, according to rule
1518 * (8) If the member is an array of <S> row-major matrices with <C> columns
1519 * and <R> rows, the matrix is stored identically to a row of <S>*<R>
1520 * row vectors with <C> components each, according to rule (4).
1522 * (10) If the member is an array of <S> structures, the <S> elements of
1523 * the array are laid out in order, according to rule (9).
1525 if (this->is_array()) {
1526 if (this->fields
.array
->is_scalar() ||
1527 this->fields
.array
->is_vector() ||
1528 this->fields
.array
->is_matrix()) {
1529 return MAX2(this->fields
.array
->std140_base_alignment(row_major
), 16);
1531 assert(this->fields
.array
->is_record() ||
1532 this->fields
.array
->is_array());
1533 return this->fields
.array
->std140_base_alignment(row_major
);
1537 /* (5) If the member is a column-major matrix with <C> columns and
1538 * <R> rows, the matrix is stored identically to an array of
1539 * <C> column vectors with <R> components each, according to
1542 * (7) If the member is a row-major matrix with <C> columns and <R>
1543 * rows, the matrix is stored identically to an array of <R>
1544 * row vectors with <C> components each, according to rule (4).
1546 if (this->is_matrix()) {
1547 const struct glsl_type
*vec_type
, *array_type
;
1548 int c
= this->matrix_columns
;
1549 int r
= this->vector_elements
;
1552 vec_type
= get_instance(base_type
, c
, 1);
1553 array_type
= glsl_type::get_array_instance(vec_type
, r
);
1555 vec_type
= get_instance(base_type
, r
, 1);
1556 array_type
= glsl_type::get_array_instance(vec_type
, c
);
1559 return array_type
->std140_base_alignment(false);
1562 /* (9) If the member is a structure, the base alignment of the
1563 * structure is <N>, where <N> is the largest base alignment
1564 * value of any of its members, and rounded up to the base
1565 * alignment of a vec4. The individual members of this
1566 * sub-structure are then assigned offsets by applying this set
1567 * of rules recursively, where the base offset of the first
1568 * member of the sub-structure is equal to the aligned offset
1569 * of the structure. The structure may have padding at the end;
1570 * the base offset of the member following the sub-structure is
1571 * rounded up to the next multiple of the base alignment of the
1574 if (this->is_record()) {
1575 unsigned base_alignment
= 16;
1576 for (unsigned i
= 0; i
< this->length
; i
++) {
1577 bool field_row_major
= row_major
;
1578 const enum glsl_matrix_layout matrix_layout
=
1579 glsl_matrix_layout(this->fields
.structure
[i
].matrix_layout
);
1580 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
1581 field_row_major
= true;
1582 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
1583 field_row_major
= false;
1586 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
1587 base_alignment
= MAX2(base_alignment
,
1588 field_type
->std140_base_alignment(field_row_major
));
1590 return base_alignment
;
1593 assert(!"not reached");
1598 glsl_type::std140_size(bool row_major
) const
1600 unsigned N
= is_64bit() ? 8 : 4;
1602 /* (1) If the member is a scalar consuming <N> basic machine units, the
1603 * base alignment is <N>.
1605 * (2) If the member is a two- or four-component vector with components
1606 * consuming <N> basic machine units, the base alignment is 2<N> or
1607 * 4<N>, respectively.
1609 * (3) If the member is a three-component vector with components consuming
1610 * <N> basic machine units, the base alignment is 4<N>.
1612 if (this->is_scalar() || this->is_vector()) {
1613 return this->vector_elements
* N
;
1616 /* (5) If the member is a column-major matrix with <C> columns and
1617 * <R> rows, the matrix is stored identically to an array of
1618 * <C> column vectors with <R> components each, according to
1621 * (6) If the member is an array of <S> column-major matrices with <C>
1622 * columns and <R> rows, the matrix is stored identically to a row of
1623 * <S>*<C> column vectors with <R> components each, according to rule
1626 * (7) If the member is a row-major matrix with <C> columns and <R>
1627 * rows, the matrix is stored identically to an array of <R>
1628 * row vectors with <C> components each, according to rule (4).
1630 * (8) If the member is an array of <S> row-major matrices with <C> columns
1631 * and <R> rows, the matrix is stored identically to a row of <S>*<R>
1632 * row vectors with <C> components each, according to rule (4).
1634 if (this->without_array()->is_matrix()) {
1635 const struct glsl_type
*element_type
;
1636 const struct glsl_type
*vec_type
;
1637 unsigned int array_len
;
1639 if (this->is_array()) {
1640 element_type
= this->without_array();
1641 array_len
= this->arrays_of_arrays_size();
1643 element_type
= this;
1648 vec_type
= get_instance(element_type
->base_type
,
1649 element_type
->matrix_columns
, 1);
1651 array_len
*= element_type
->vector_elements
;
1653 vec_type
= get_instance(element_type
->base_type
,
1654 element_type
->vector_elements
, 1);
1655 array_len
*= element_type
->matrix_columns
;
1657 const glsl_type
*array_type
= glsl_type::get_array_instance(vec_type
,
1660 return array_type
->std140_size(false);
1663 /* (4) If the member is an array of scalars or vectors, the base alignment
1664 * and array stride are set to match the base alignment of a single
1665 * array element, according to rules (1), (2), and (3), and rounded up
1666 * to the base alignment of a vec4. The array may have padding at the
1667 * end; the base offset of the member following the array is rounded up
1668 * to the next multiple of the base alignment.
1670 * (10) If the member is an array of <S> structures, the <S> elements of
1671 * the array are laid out in order, according to rule (9).
1673 if (this->is_array()) {
1674 if (this->without_array()->is_record()) {
1675 return this->arrays_of_arrays_size() *
1676 this->without_array()->std140_size(row_major
);
1678 unsigned element_base_align
=
1679 this->without_array()->std140_base_alignment(row_major
);
1680 return this->arrays_of_arrays_size() * MAX2(element_base_align
, 16);
1684 /* (9) If the member is a structure, the base alignment of the
1685 * structure is <N>, where <N> is the largest base alignment
1686 * value of any of its members, and rounded up to the base
1687 * alignment of a vec4. The individual members of this
1688 * sub-structure are then assigned offsets by applying this set
1689 * of rules recursively, where the base offset of the first
1690 * member of the sub-structure is equal to the aligned offset
1691 * of the structure. The structure may have padding at the end;
1692 * the base offset of the member following the sub-structure is
1693 * rounded up to the next multiple of the base alignment of the
1696 if (this->is_record() || this->is_interface()) {
1698 unsigned max_align
= 0;
1700 for (unsigned i
= 0; i
< this->length
; i
++) {
1701 bool field_row_major
= row_major
;
1702 const enum glsl_matrix_layout matrix_layout
=
1703 glsl_matrix_layout(this->fields
.structure
[i
].matrix_layout
);
1704 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
1705 field_row_major
= true;
1706 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
1707 field_row_major
= false;
1710 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
1711 unsigned align
= field_type
->std140_base_alignment(field_row_major
);
1713 /* Ignore unsized arrays when calculating size */
1714 if (field_type
->is_unsized_array())
1717 size
= glsl_align(size
, align
);
1718 size
+= field_type
->std140_size(field_row_major
);
1720 max_align
= MAX2(align
, max_align
);
1722 if (field_type
->is_record() && (i
+ 1 < this->length
))
1723 size
= glsl_align(size
, 16);
1725 size
= glsl_align(size
, MAX2(max_align
, 16));
1729 assert(!"not reached");
1734 glsl_type::std430_base_alignment(bool row_major
) const
1737 unsigned N
= is_64bit() ? 8 : 4;
1739 /* (1) If the member is a scalar consuming <N> basic machine units, the
1740 * base alignment is <N>.
1742 * (2) If the member is a two- or four-component vector with components
1743 * consuming <N> basic machine units, the base alignment is 2<N> or
1744 * 4<N>, respectively.
1746 * (3) If the member is a three-component vector with components consuming
1747 * <N> basic machine units, the base alignment is 4<N>.
1749 if (this->is_scalar() || this->is_vector()) {
1750 switch (this->vector_elements
) {
1761 /* OpenGL 4.30 spec, section 7.6.2.2 "Standard Uniform Block Layout":
1763 * "When using the std430 storage layout, shader storage blocks will be
1764 * laid out in buffer storage identically to uniform and shader storage
1765 * blocks using the std140 layout, except that the base alignment and
1766 * stride of arrays of scalars and vectors in rule 4 and of structures
1767 * in rule 9 are not rounded up a multiple of the base alignment of a vec4.
1770 /* (1) If the member is a scalar consuming <N> basic machine units, the
1771 * base alignment is <N>.
1773 * (2) If the member is a two- or four-component vector with components
1774 * consuming <N> basic machine units, the base alignment is 2<N> or
1775 * 4<N>, respectively.
1777 * (3) If the member is a three-component vector with components consuming
1778 * <N> basic machine units, the base alignment is 4<N>.
1780 if (this->is_array())
1781 return this->fields
.array
->std430_base_alignment(row_major
);
1783 /* (5) If the member is a column-major matrix with <C> columns and
1784 * <R> rows, the matrix is stored identically to an array of
1785 * <C> column vectors with <R> components each, according to
1788 * (7) If the member is a row-major matrix with <C> columns and <R>
1789 * rows, the matrix is stored identically to an array of <R>
1790 * row vectors with <C> components each, according to rule (4).
1792 if (this->is_matrix()) {
1793 const struct glsl_type
*vec_type
, *array_type
;
1794 int c
= this->matrix_columns
;
1795 int r
= this->vector_elements
;
1798 vec_type
= get_instance(base_type
, c
, 1);
1799 array_type
= glsl_type::get_array_instance(vec_type
, r
);
1801 vec_type
= get_instance(base_type
, r
, 1);
1802 array_type
= glsl_type::get_array_instance(vec_type
, c
);
1805 return array_type
->std430_base_alignment(false);
1808 /* (9) If the member is a structure, the base alignment of the
1809 * structure is <N>, where <N> is the largest base alignment
1810 * value of any of its members, and rounded up to the base
1811 * alignment of a vec4. The individual members of this
1812 * sub-structure are then assigned offsets by applying this set
1813 * of rules recursively, where the base offset of the first
1814 * member of the sub-structure is equal to the aligned offset
1815 * of the structure. The structure may have padding at the end;
1816 * the base offset of the member following the sub-structure is
1817 * rounded up to the next multiple of the base alignment of the
1820 if (this->is_record()) {
1821 unsigned base_alignment
= 0;
1822 for (unsigned i
= 0; i
< this->length
; i
++) {
1823 bool field_row_major
= row_major
;
1824 const enum glsl_matrix_layout matrix_layout
=
1825 glsl_matrix_layout(this->fields
.structure
[i
].matrix_layout
);
1826 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
1827 field_row_major
= true;
1828 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
1829 field_row_major
= false;
1832 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
1833 base_alignment
= MAX2(base_alignment
,
1834 field_type
->std430_base_alignment(field_row_major
));
1836 assert(base_alignment
> 0);
1837 return base_alignment
;
1839 assert(!"not reached");
1844 glsl_type::std430_array_stride(bool row_major
) const
1846 unsigned N
= is_64bit() ? 8 : 4;
1848 /* Notice that the array stride of a vec3 is not 3 * N but 4 * N.
1849 * See OpenGL 4.30 spec, section 7.6.2.2 "Standard Uniform Block Layout"
1851 * (3) If the member is a three-component vector with components consuming
1852 * <N> basic machine units, the base alignment is 4<N>.
1854 if (this->is_vector() && this->vector_elements
== 3)
1857 /* By default use std430_size(row_major) */
1858 return this->std430_size(row_major
);
1862 glsl_type::std430_size(bool row_major
) const
1864 unsigned N
= is_64bit() ? 8 : 4;
1866 /* OpenGL 4.30 spec, section 7.6.2.2 "Standard Uniform Block Layout":
1868 * "When using the std430 storage layout, shader storage blocks will be
1869 * laid out in buffer storage identically to uniform and shader storage
1870 * blocks using the std140 layout, except that the base alignment and
1871 * stride of arrays of scalars and vectors in rule 4 and of structures
1872 * in rule 9 are not rounded up a multiple of the base alignment of a vec4.
1874 if (this->is_scalar() || this->is_vector())
1875 return this->vector_elements
* N
;
1877 if (this->without_array()->is_matrix()) {
1878 const struct glsl_type
*element_type
;
1879 const struct glsl_type
*vec_type
;
1880 unsigned int array_len
;
1882 if (this->is_array()) {
1883 element_type
= this->without_array();
1884 array_len
= this->arrays_of_arrays_size();
1886 element_type
= this;
1891 vec_type
= get_instance(element_type
->base_type
,
1892 element_type
->matrix_columns
, 1);
1894 array_len
*= element_type
->vector_elements
;
1896 vec_type
= get_instance(element_type
->base_type
,
1897 element_type
->vector_elements
, 1);
1898 array_len
*= element_type
->matrix_columns
;
1900 const glsl_type
*array_type
= glsl_type::get_array_instance(vec_type
,
1903 return array_type
->std430_size(false);
1906 if (this->is_array()) {
1907 if (this->without_array()->is_record())
1908 return this->arrays_of_arrays_size() *
1909 this->without_array()->std430_size(row_major
);
1911 return this->arrays_of_arrays_size() *
1912 this->without_array()->std430_base_alignment(row_major
);
1915 if (this->is_record() || this->is_interface()) {
1917 unsigned max_align
= 0;
1919 for (unsigned i
= 0; i
< this->length
; i
++) {
1920 bool field_row_major
= row_major
;
1921 const enum glsl_matrix_layout matrix_layout
=
1922 glsl_matrix_layout(this->fields
.structure
[i
].matrix_layout
);
1923 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
1924 field_row_major
= true;
1925 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
1926 field_row_major
= false;
1929 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
1930 unsigned align
= field_type
->std430_base_alignment(field_row_major
);
1931 size
= glsl_align(size
, align
);
1932 size
+= field_type
->std430_size(field_row_major
);
1934 max_align
= MAX2(align
, max_align
);
1936 size
= glsl_align(size
, max_align
);
1940 assert(!"not reached");
1945 glsl_type::count_attribute_slots(bool is_vertex_input
) const
1947 /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
1949 * "A scalar input counts the same amount against this limit as a vec4,
1950 * so applications may want to consider packing groups of four
1951 * unrelated float inputs together into a vector to better utilize the
1952 * capabilities of the underlying hardware. A matrix input will use up
1953 * multiple locations. The number of locations used will equal the
1954 * number of columns in the matrix."
1956 * The spec does not explicitly say how arrays are counted. However, it
1957 * should be safe to assume the total number of slots consumed by an array
1958 * is the number of entries in the array multiplied by the number of slots
1959 * consumed by a single element of the array.
1961 * The spec says nothing about how structs are counted, because vertex
1962 * attributes are not allowed to be (or contain) structs. However, Mesa
1963 * allows varying structs, the number of varying slots taken up by a
1964 * varying struct is simply equal to the sum of the number of slots taken
1965 * up by each element.
1967 * Doubles are counted different depending on whether they are vertex
1968 * inputs or everything else. Vertex inputs from ARB_vertex_attrib_64bit
1969 * take one location no matter what size they are, otherwise dvec3/4
1970 * take two locations.
1972 switch (this->base_type
) {
1973 case GLSL_TYPE_UINT
:
1975 case GLSL_TYPE_UINT8
:
1976 case GLSL_TYPE_INT8
:
1977 case GLSL_TYPE_UINT16
:
1978 case GLSL_TYPE_INT16
:
1979 case GLSL_TYPE_FLOAT
:
1980 case GLSL_TYPE_FLOAT16
:
1981 case GLSL_TYPE_BOOL
:
1982 case GLSL_TYPE_SAMPLER
:
1983 case GLSL_TYPE_IMAGE
:
1984 return this->matrix_columns
;
1985 case GLSL_TYPE_DOUBLE
:
1986 case GLSL_TYPE_UINT64
:
1987 case GLSL_TYPE_INT64
:
1988 if (this->vector_elements
> 2 && !is_vertex_input
)
1989 return this->matrix_columns
* 2;
1991 return this->matrix_columns
;
1992 case GLSL_TYPE_STRUCT
:
1993 case GLSL_TYPE_INTERFACE
: {
1996 for (unsigned i
= 0; i
< this->length
; i
++)
1997 size
+= this->fields
.structure
[i
].type
->count_attribute_slots(is_vertex_input
);
2002 case GLSL_TYPE_ARRAY
:
2003 return this->length
* this->fields
.array
->count_attribute_slots(is_vertex_input
);
2005 case GLSL_TYPE_SUBROUTINE
:
2008 case GLSL_TYPE_FUNCTION
:
2009 case GLSL_TYPE_ATOMIC_UINT
:
2010 case GLSL_TYPE_VOID
:
2011 case GLSL_TYPE_ERROR
:
2015 assert(!"Unexpected type in count_attribute_slots()");
2021 glsl_type::coordinate_components() const
2025 switch (sampler_dimensionality
) {
2026 case GLSL_SAMPLER_DIM_1D
:
2027 case GLSL_SAMPLER_DIM_BUF
:
2030 case GLSL_SAMPLER_DIM_2D
:
2031 case GLSL_SAMPLER_DIM_RECT
:
2032 case GLSL_SAMPLER_DIM_MS
:
2033 case GLSL_SAMPLER_DIM_EXTERNAL
:
2034 case GLSL_SAMPLER_DIM_SUBPASS
:
2037 case GLSL_SAMPLER_DIM_3D
:
2038 case GLSL_SAMPLER_DIM_CUBE
:
2042 assert(!"Should not get here.");
2047 /* Array textures need an additional component for the array index, except
2048 * for cubemap array images that behave like a 2D array of interleaved
2051 if (sampler_array
&&
2052 !(is_image() && sampler_dimensionality
== GLSL_SAMPLER_DIM_CUBE
))
2059 * Declarations of type flyweights (glsl_type::_foo_type) and
2060 * convenience pointers (glsl_type::foo_type).
2063 #define DECL_TYPE(NAME, ...) \
2064 const glsl_type glsl_type::_##NAME##_type = glsl_type(__VA_ARGS__, #NAME); \
2065 const glsl_type *const glsl_type::NAME##_type = &glsl_type::_##NAME##_type;
2067 #define STRUCT_TYPE(NAME)
2069 #include "compiler/builtin_type_macros.h"
2073 get_struct_type_field_and_pointer_sizes(size_t *s_field_size
,
2074 size_t *s_field_ptrs
)
2076 *s_field_size
= sizeof(glsl_struct_field
);
2078 sizeof(((glsl_struct_field
*)0)->type
) +
2079 sizeof(((glsl_struct_field
*)0)->name
);
2083 encode_type_to_blob(struct blob
*blob
, const glsl_type
*type
)
2088 blob_write_uint32(blob
, 0);
2092 switch (type
->base_type
) {
2093 case GLSL_TYPE_UINT
:
2095 case GLSL_TYPE_FLOAT
:
2096 case GLSL_TYPE_BOOL
:
2097 case GLSL_TYPE_DOUBLE
:
2098 case GLSL_TYPE_UINT64
:
2099 case GLSL_TYPE_INT64
:
2100 encoding
= (type
->base_type
<< 24) |
2101 (type
->vector_elements
<< 4) |
2102 (type
->matrix_columns
);
2104 case GLSL_TYPE_SAMPLER
:
2105 encoding
= (type
->base_type
) << 24 |
2106 (type
->sampler_dimensionality
<< 4) |
2107 (type
->sampler_shadow
<< 3) |
2108 (type
->sampler_array
<< 2) |
2109 (type
->sampled_type
);
2111 case GLSL_TYPE_SUBROUTINE
:
2112 encoding
= type
->base_type
<< 24;
2113 blob_write_uint32(blob
, encoding
);
2114 blob_write_string(blob
, type
->name
);
2116 case GLSL_TYPE_IMAGE
:
2117 encoding
= (type
->base_type
) << 24 |
2118 (type
->sampler_dimensionality
<< 3) |
2119 (type
->sampler_array
<< 2) |
2120 (type
->sampled_type
);
2122 case GLSL_TYPE_ATOMIC_UINT
:
2123 encoding
= (type
->base_type
<< 24);
2125 case GLSL_TYPE_ARRAY
:
2126 blob_write_uint32(blob
, (type
->base_type
) << 24);
2127 blob_write_uint32(blob
, type
->length
);
2128 encode_type_to_blob(blob
, type
->fields
.array
);
2130 case GLSL_TYPE_STRUCT
:
2131 case GLSL_TYPE_INTERFACE
:
2132 blob_write_uint32(blob
, (type
->base_type
) << 24);
2133 blob_write_string(blob
, type
->name
);
2134 blob_write_uint32(blob
, type
->length
);
2136 size_t s_field_size
, s_field_ptrs
;
2137 get_struct_type_field_and_pointer_sizes(&s_field_size
, &s_field_ptrs
);
2139 for (unsigned i
= 0; i
< type
->length
; i
++) {
2140 encode_type_to_blob(blob
, type
->fields
.structure
[i
].type
);
2141 blob_write_string(blob
, type
->fields
.structure
[i
].name
);
2143 /* Write the struct field skipping the pointers */
2144 blob_write_bytes(blob
,
2145 ((char *)&type
->fields
.structure
[i
]) + s_field_ptrs
,
2146 s_field_size
- s_field_ptrs
);
2149 if (type
->is_interface()) {
2150 blob_write_uint32(blob
, type
->interface_packing
);
2151 blob_write_uint32(blob
, type
->interface_row_major
);
2154 case GLSL_TYPE_VOID
:
2155 encoding
= (type
->base_type
<< 24);
2157 case GLSL_TYPE_ERROR
:
2159 assert(!"Cannot encode type!");
2164 blob_write_uint32(blob
, encoding
);
2168 decode_type_from_blob(struct blob_reader
*blob
)
2170 uint32_t u
= blob_read_uint32(blob
);
2176 glsl_base_type base_type
= (glsl_base_type
) (u
>> 24);
2178 switch (base_type
) {
2179 case GLSL_TYPE_UINT
:
2181 case GLSL_TYPE_FLOAT
:
2182 case GLSL_TYPE_BOOL
:
2183 case GLSL_TYPE_DOUBLE
:
2184 case GLSL_TYPE_UINT64
:
2185 case GLSL_TYPE_INT64
:
2186 return glsl_type::get_instance(base_type
, (u
>> 4) & 0x0f, u
& 0x0f);
2187 case GLSL_TYPE_SAMPLER
:
2188 return glsl_type::get_sampler_instance((enum glsl_sampler_dim
) ((u
>> 4) & 0x07),
2191 (glsl_base_type
) ((u
>> 0) & 0x03));
2192 case GLSL_TYPE_SUBROUTINE
:
2193 return glsl_type::get_subroutine_instance(blob_read_string(blob
));
2194 case GLSL_TYPE_IMAGE
:
2195 return glsl_type::get_image_instance((enum glsl_sampler_dim
) ((u
>> 3) & 0x07),
2197 (glsl_base_type
) ((u
>> 0) & 0x03));
2198 case GLSL_TYPE_ATOMIC_UINT
:
2199 return glsl_type::atomic_uint_type
;
2200 case GLSL_TYPE_ARRAY
: {
2201 unsigned length
= blob_read_uint32(blob
);
2202 return glsl_type::get_array_instance(decode_type_from_blob(blob
),
2205 case GLSL_TYPE_STRUCT
:
2206 case GLSL_TYPE_INTERFACE
: {
2207 char *name
= blob_read_string(blob
);
2208 unsigned num_fields
= blob_read_uint32(blob
);
2210 size_t s_field_size
, s_field_ptrs
;
2211 get_struct_type_field_and_pointer_sizes(&s_field_size
, &s_field_ptrs
);
2213 glsl_struct_field
*fields
=
2214 (glsl_struct_field
*) malloc(s_field_size
* num_fields
);
2215 for (unsigned i
= 0; i
< num_fields
; i
++) {
2216 fields
[i
].type
= decode_type_from_blob(blob
);
2217 fields
[i
].name
= blob_read_string(blob
);
2219 blob_copy_bytes(blob
, ((uint8_t *) &fields
[i
]) + s_field_ptrs
,
2220 s_field_size
- s_field_ptrs
);
2224 if (base_type
== GLSL_TYPE_INTERFACE
) {
2225 enum glsl_interface_packing packing
=
2226 (glsl_interface_packing
) blob_read_uint32(blob
);
2227 bool row_major
= blob_read_uint32(blob
);
2228 t
= glsl_type::get_interface_instance(fields
, num_fields
, packing
,
2231 t
= glsl_type::get_record_instance(fields
, num_fields
, name
);
2237 case GLSL_TYPE_VOID
:
2238 return glsl_type::void_type
;
2239 case GLSL_TYPE_ERROR
:
2241 assert(!"Cannot decode type!");