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"
29 #include "util/u_string.h"
32 mtx_t
glsl_type::hash_mutex
= _MTX_INITIALIZER_NP
;
33 hash_table
*glsl_type::explicit_matrix_types
= NULL
;
34 hash_table
*glsl_type::array_types
= NULL
;
35 hash_table
*glsl_type::struct_types
= NULL
;
36 hash_table
*glsl_type::interface_types
= NULL
;
37 hash_table
*glsl_type::function_types
= NULL
;
38 hash_table
*glsl_type::subroutine_types
= NULL
;
40 /* There might be multiple users for types (e.g. application using OpenGL
41 * and Vulkan simultanously or app using multiple Vulkan instances). Counter
42 * is used to make sure we don't release the types if a user is still present.
44 static uint32_t glsl_type_users
= 0;
46 glsl_type::glsl_type(GLenum gl_type
,
47 glsl_base_type base_type
, unsigned vector_elements
,
48 unsigned matrix_columns
, const char *name
,
49 unsigned explicit_stride
, bool row_major
) :
51 base_type(base_type
), sampled_type(GLSL_TYPE_VOID
),
52 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
53 interface_packing(0), interface_row_major(row_major
),
54 vector_elements(vector_elements
), matrix_columns(matrix_columns
),
55 length(0), explicit_stride(explicit_stride
)
57 /* Values of these types must fit in the two bits of
58 * glsl_type::sampled_type.
60 STATIC_ASSERT((unsigned(GLSL_TYPE_UINT
) & 3) == unsigned(GLSL_TYPE_UINT
));
61 STATIC_ASSERT((unsigned(GLSL_TYPE_INT
) & 3) == unsigned(GLSL_TYPE_INT
));
62 STATIC_ASSERT((unsigned(GLSL_TYPE_FLOAT
) & 3) == unsigned(GLSL_TYPE_FLOAT
));
64 ASSERT_BITFIELD_SIZE(glsl_type
, base_type
, GLSL_TYPE_ERROR
);
65 ASSERT_BITFIELD_SIZE(glsl_type
, sampled_type
, GLSL_TYPE_ERROR
);
66 ASSERT_BITFIELD_SIZE(glsl_type
, sampler_dimensionality
,
67 GLSL_SAMPLER_DIM_SUBPASS_MS
);
69 this->mem_ctx
= ralloc_context(NULL
);
70 assert(this->mem_ctx
!= NULL
);
73 this->name
= ralloc_strdup(this->mem_ctx
, name
);
75 /* Neither dimension is zero or both dimensions are zero.
77 assert((vector_elements
== 0) == (matrix_columns
== 0));
78 memset(& fields
, 0, sizeof(fields
));
81 glsl_type::glsl_type(GLenum gl_type
, glsl_base_type base_type
,
82 enum glsl_sampler_dim dim
, bool shadow
, bool array
,
83 glsl_base_type type
, const char *name
) :
85 base_type(base_type
), sampled_type(type
),
86 sampler_dimensionality(dim
), sampler_shadow(shadow
),
87 sampler_array(array
), interface_packing(0),
88 interface_row_major(0),
89 length(0), explicit_stride(0)
91 this->mem_ctx
= ralloc_context(NULL
);
92 assert(this->mem_ctx
!= NULL
);
95 this->name
= ralloc_strdup(this->mem_ctx
, name
);
97 memset(& fields
, 0, sizeof(fields
));
99 matrix_columns
= vector_elements
= 1;
102 glsl_type::glsl_type(const glsl_struct_field
*fields
, unsigned num_fields
,
103 const char *name
, bool packed
) :
105 base_type(GLSL_TYPE_STRUCT
), sampled_type(GLSL_TYPE_VOID
),
106 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
107 interface_packing(0), interface_row_major(0), packed(packed
),
108 vector_elements(0), matrix_columns(0),
109 length(num_fields
), explicit_stride(0)
113 this->mem_ctx
= ralloc_context(NULL
);
114 assert(this->mem_ctx
!= NULL
);
116 assert(name
!= NULL
);
117 this->name
= ralloc_strdup(this->mem_ctx
, name
);
118 /* Zero-fill to prevent spurious Valgrind errors when serializing NIR
119 * due to uninitialized unused bits in bit fields. */
120 this->fields
.structure
= rzalloc_array(this->mem_ctx
,
121 glsl_struct_field
, length
);
123 for (i
= 0; i
< length
; i
++) {
124 this->fields
.structure
[i
] = fields
[i
];
125 this->fields
.structure
[i
].name
= ralloc_strdup(this->fields
.structure
,
130 glsl_type::glsl_type(const glsl_struct_field
*fields
, unsigned num_fields
,
131 enum glsl_interface_packing packing
,
132 bool row_major
, const char *name
) :
134 base_type(GLSL_TYPE_INTERFACE
), sampled_type(GLSL_TYPE_VOID
),
135 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
136 interface_packing((unsigned) packing
),
137 interface_row_major((unsigned) row_major
),
138 vector_elements(0), matrix_columns(0),
139 length(num_fields
), explicit_stride(0)
143 this->mem_ctx
= ralloc_context(NULL
);
144 assert(this->mem_ctx
!= NULL
);
146 assert(name
!= NULL
);
147 this->name
= ralloc_strdup(this->mem_ctx
, name
);
148 this->fields
.structure
= rzalloc_array(this->mem_ctx
,
149 glsl_struct_field
, length
);
150 for (i
= 0; i
< length
; i
++) {
151 this->fields
.structure
[i
] = fields
[i
];
152 this->fields
.structure
[i
].name
= ralloc_strdup(this->fields
.structure
,
157 glsl_type::glsl_type(const glsl_type
*return_type
,
158 const glsl_function_param
*params
, unsigned num_params
) :
160 base_type(GLSL_TYPE_FUNCTION
), sampled_type(GLSL_TYPE_VOID
),
161 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
162 interface_packing(0), interface_row_major(0),
163 vector_elements(0), matrix_columns(0),
164 length(num_params
), explicit_stride(0)
168 this->mem_ctx
= ralloc_context(NULL
);
169 assert(this->mem_ctx
!= NULL
);
171 this->fields
.parameters
= rzalloc_array(this->mem_ctx
,
172 glsl_function_param
, num_params
+ 1);
174 /* We store the return type as the first parameter */
175 this->fields
.parameters
[0].type
= return_type
;
176 this->fields
.parameters
[0].in
= false;
177 this->fields
.parameters
[0].out
= true;
179 /* We store the i'th parameter in slot i+1 */
180 for (i
= 0; i
< length
; i
++) {
181 this->fields
.parameters
[i
+ 1].type
= params
[i
].type
;
182 this->fields
.parameters
[i
+ 1].in
= params
[i
].in
;
183 this->fields
.parameters
[i
+ 1].out
= params
[i
].out
;
187 glsl_type::glsl_type(const char *subroutine_name
) :
189 base_type(GLSL_TYPE_SUBROUTINE
), sampled_type(GLSL_TYPE_VOID
),
190 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
191 interface_packing(0), interface_row_major(0),
192 vector_elements(1), matrix_columns(1),
193 length(0), explicit_stride(0)
195 this->mem_ctx
= ralloc_context(NULL
);
196 assert(this->mem_ctx
!= NULL
);
198 assert(subroutine_name
!= NULL
);
199 this->name
= ralloc_strdup(this->mem_ctx
, subroutine_name
);
202 glsl_type::~glsl_type()
204 ralloc_free(this->mem_ctx
);
208 glsl_type::contains_sampler() const
210 if (this->is_array()) {
211 return this->fields
.array
->contains_sampler();
212 } else if (this->is_struct() || this->is_interface()) {
213 for (unsigned int i
= 0; i
< this->length
; i
++) {
214 if (this->fields
.structure
[i
].type
->contains_sampler())
219 return this->is_sampler();
224 glsl_type::contains_array() const
226 if (this->is_struct() || this->is_interface()) {
227 for (unsigned int i
= 0; i
< this->length
; i
++) {
228 if (this->fields
.structure
[i
].type
->contains_array())
233 return this->is_array();
238 glsl_type::contains_integer() const
240 if (this->is_array()) {
241 return this->fields
.array
->contains_integer();
242 } else if (this->is_struct() || this->is_interface()) {
243 for (unsigned int i
= 0; i
< this->length
; i
++) {
244 if (this->fields
.structure
[i
].type
->contains_integer())
249 return this->is_integer();
254 glsl_type::contains_double() const
256 if (this->is_array()) {
257 return this->fields
.array
->contains_double();
258 } else if (this->is_struct() || this->is_interface()) {
259 for (unsigned int i
= 0; i
< this->length
; i
++) {
260 if (this->fields
.structure
[i
].type
->contains_double())
265 return this->is_double();
270 glsl_type::contains_64bit() const
272 if (this->is_array()) {
273 return this->fields
.array
->contains_64bit();
274 } else if (this->is_struct() || this->is_interface()) {
275 for (unsigned int i
= 0; i
< this->length
; i
++) {
276 if (this->fields
.structure
[i
].type
->contains_64bit())
281 return this->is_64bit();
286 glsl_type::contains_opaque() const {
288 case GLSL_TYPE_SAMPLER
:
289 case GLSL_TYPE_IMAGE
:
290 case GLSL_TYPE_ATOMIC_UINT
:
292 case GLSL_TYPE_ARRAY
:
293 return fields
.array
->contains_opaque();
294 case GLSL_TYPE_STRUCT
:
295 case GLSL_TYPE_INTERFACE
:
296 for (unsigned int i
= 0; i
< length
; i
++) {
297 if (fields
.structure
[i
].type
->contains_opaque())
307 glsl_type::contains_subroutine() const
309 if (this->is_array()) {
310 return this->fields
.array
->contains_subroutine();
311 } else if (this->is_struct() || this->is_interface()) {
312 for (unsigned int i
= 0; i
< this->length
; i
++) {
313 if (this->fields
.structure
[i
].type
->contains_subroutine())
318 return this->is_subroutine();
323 glsl_type::sampler_index() const
325 const glsl_type
*const t
= (this->is_array()) ? this->fields
.array
: this;
327 assert(t
->is_sampler() || t
->is_image());
329 switch (t
->sampler_dimensionality
) {
330 case GLSL_SAMPLER_DIM_1D
:
331 return (t
->sampler_array
) ? TEXTURE_1D_ARRAY_INDEX
: TEXTURE_1D_INDEX
;
332 case GLSL_SAMPLER_DIM_2D
:
333 return (t
->sampler_array
) ? TEXTURE_2D_ARRAY_INDEX
: TEXTURE_2D_INDEX
;
334 case GLSL_SAMPLER_DIM_3D
:
335 return TEXTURE_3D_INDEX
;
336 case GLSL_SAMPLER_DIM_CUBE
:
337 return (t
->sampler_array
) ? TEXTURE_CUBE_ARRAY_INDEX
: TEXTURE_CUBE_INDEX
;
338 case GLSL_SAMPLER_DIM_RECT
:
339 return TEXTURE_RECT_INDEX
;
340 case GLSL_SAMPLER_DIM_BUF
:
341 return TEXTURE_BUFFER_INDEX
;
342 case GLSL_SAMPLER_DIM_EXTERNAL
:
343 return TEXTURE_EXTERNAL_INDEX
;
344 case GLSL_SAMPLER_DIM_MS
:
345 return (t
->sampler_array
) ? TEXTURE_2D_MULTISAMPLE_ARRAY_INDEX
: TEXTURE_2D_MULTISAMPLE_INDEX
;
347 assert(!"Should not get here.");
348 return TEXTURE_BUFFER_INDEX
;
353 glsl_type::contains_image() const
355 if (this->is_array()) {
356 return this->fields
.array
->contains_image();
357 } else if (this->is_struct() || this->is_interface()) {
358 for (unsigned int i
= 0; i
< this->length
; i
++) {
359 if (this->fields
.structure
[i
].type
->contains_image())
364 return this->is_image();
368 const glsl_type
*glsl_type::get_base_type() const
373 case GLSL_TYPE_UINT16
:
374 return uint16_t_type
;
375 case GLSL_TYPE_UINT8
:
379 case GLSL_TYPE_INT16
:
383 case GLSL_TYPE_FLOAT
:
385 case GLSL_TYPE_FLOAT16
:
386 return float16_t_type
;
387 case GLSL_TYPE_DOUBLE
:
391 case GLSL_TYPE_UINT64
:
392 return uint64_t_type
;
393 case GLSL_TYPE_INT64
:
401 const glsl_type
*glsl_type::get_scalar_type() const
403 const glsl_type
*type
= this;
406 while (type
->base_type
== GLSL_TYPE_ARRAY
)
407 type
= type
->fields
.array
;
409 const glsl_type
*scalar_type
= type
->get_base_type();
410 if (scalar_type
== error_type
)
417 const glsl_type
*glsl_type::get_bare_type() const
419 switch (this->base_type
) {
420 case GLSL_TYPE_UINT8
:
422 case GLSL_TYPE_UINT16
:
423 case GLSL_TYPE_INT16
:
424 case GLSL_TYPE_FLOAT16
:
427 case GLSL_TYPE_FLOAT
:
429 case GLSL_TYPE_DOUBLE
:
430 case GLSL_TYPE_UINT64
:
431 case GLSL_TYPE_INT64
:
432 return get_instance(this->base_type
, this->vector_elements
,
433 this->matrix_columns
);
435 case GLSL_TYPE_STRUCT
:
436 case GLSL_TYPE_INTERFACE
: {
437 glsl_struct_field
*bare_fields
= new glsl_struct_field
[this->length
];
438 for (unsigned i
= 0; i
< this->length
; i
++) {
439 bare_fields
[i
].type
= this->fields
.structure
[i
].type
->get_bare_type();
440 bare_fields
[i
].name
= this->fields
.structure
[i
].name
;
442 const glsl_type
*bare_type
=
443 get_struct_instance(bare_fields
, this->length
, this->name
);
444 delete[] bare_fields
;
448 case GLSL_TYPE_ARRAY
:
449 return get_array_instance(this->fields
.array
->get_bare_type(),
452 case GLSL_TYPE_SAMPLER
:
453 case GLSL_TYPE_IMAGE
:
454 case GLSL_TYPE_ATOMIC_UINT
:
456 case GLSL_TYPE_SUBROUTINE
:
457 case GLSL_TYPE_FUNCTION
:
458 case GLSL_TYPE_ERROR
:
462 unreachable("Invalid base type");
467 hash_free_type_function(struct hash_entry
*entry
)
469 glsl_type
*type
= (glsl_type
*) entry
->data
;
471 if (type
->is_array())
472 free((void*)entry
->key
);
478 glsl_type_singleton_init_or_ref()
480 mtx_lock(&glsl_type::hash_mutex
);
482 mtx_unlock(&glsl_type::hash_mutex
);
486 glsl_type_singleton_decref()
488 mtx_lock(&glsl_type::hash_mutex
);
490 assert(glsl_type_users
> 0);
492 /* Do not release glsl_types if they are still used. */
493 if (--glsl_type_users
) {
494 mtx_unlock(&glsl_type::hash_mutex
);
498 if (glsl_type::explicit_matrix_types
!= NULL
) {
499 _mesa_hash_table_destroy(glsl_type::explicit_matrix_types
,
500 hash_free_type_function
);
501 glsl_type::explicit_matrix_types
= NULL
;
504 if (glsl_type::array_types
!= NULL
) {
505 _mesa_hash_table_destroy(glsl_type::array_types
, hash_free_type_function
);
506 glsl_type::array_types
= NULL
;
509 if (glsl_type::struct_types
!= NULL
) {
510 _mesa_hash_table_destroy(glsl_type::struct_types
, hash_free_type_function
);
511 glsl_type::struct_types
= NULL
;
514 if (glsl_type::interface_types
!= NULL
) {
515 _mesa_hash_table_destroy(glsl_type::interface_types
, hash_free_type_function
);
516 glsl_type::interface_types
= NULL
;
519 if (glsl_type::function_types
!= NULL
) {
520 _mesa_hash_table_destroy(glsl_type::function_types
, hash_free_type_function
);
521 glsl_type::function_types
= NULL
;
524 if (glsl_type::subroutine_types
!= NULL
) {
525 _mesa_hash_table_destroy(glsl_type::subroutine_types
, hash_free_type_function
);
526 glsl_type::subroutine_types
= NULL
;
529 mtx_unlock(&glsl_type::hash_mutex
);
533 glsl_type::glsl_type(const glsl_type
*array
, unsigned length
,
534 unsigned explicit_stride
) :
535 base_type(GLSL_TYPE_ARRAY
), sampled_type(GLSL_TYPE_VOID
),
536 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
537 interface_packing(0), interface_row_major(0),
538 vector_elements(0), matrix_columns(0),
539 length(length
), name(NULL
), explicit_stride(explicit_stride
)
541 this->fields
.array
= array
;
542 /* Inherit the gl type of the base. The GL type is used for
543 * uniform/statevar handling in Mesa and the arrayness of the type
544 * is represented by the size rather than the type.
546 this->gl_type
= array
->gl_type
;
548 /* Allow a maximum of 10 characters for the array size. This is enough
549 * for 32-bits of ~0. The extra 3 are for the '[', ']', and terminating
552 const unsigned name_length
= strlen(array
->name
) + 10 + 3;
554 this->mem_ctx
= ralloc_context(NULL
);
555 assert(this->mem_ctx
!= NULL
);
557 char *const n
= (char *) ralloc_size(this->mem_ctx
, name_length
);
560 util_snprintf(n
, name_length
, "%s[]", array
->name
);
562 /* insert outermost dimensions in the correct spot
563 * otherwise the dimension order will be backwards
565 const char *pos
= strchr(array
->name
, '[');
567 int idx
= pos
- array
->name
;
568 util_snprintf(n
, idx
+1, "%s", array
->name
);
569 util_snprintf(n
+ idx
, name_length
- idx
, "[%u]%s",
570 length
, array
->name
+ idx
);
572 util_snprintf(n
, name_length
, "%s[%u]", array
->name
, length
);
580 glsl_type::vec(unsigned components
, const glsl_type
*const ts
[])
582 unsigned n
= components
;
586 else if (components
== 16)
595 #define VECN(components, sname, vname) \
597 glsl_type:: vname (unsigned components) \
599 static const glsl_type *const ts[] = { \
600 sname ## _type, vname ## 2_type, \
601 vname ## 3_type, vname ## 4_type, \
602 vname ## 8_type, vname ## 16_type, \
604 return glsl_type::vec(components, ts); \
607 VECN(components
, float, vec
)
608 VECN(components
, float16_t
, f16vec
)
609 VECN(components
, double, dvec
)
610 VECN(components
, int, ivec
)
611 VECN(components
, uint
, uvec
)
612 VECN(components
, bool, bvec
)
613 VECN(components
, int64_t, i64vec
)
614 VECN(components
, uint64_t, u64vec
)
615 VECN(components
, int16_t, i16vec
)
616 VECN(components
, uint16_t, u16vec
)
617 VECN(components
, int8_t, i8vec
)
618 VECN(components
, uint8_t, u8vec
)
621 glsl_type::get_instance(unsigned base_type
, unsigned rows
, unsigned columns
,
622 unsigned explicit_stride
, bool row_major
)
624 if (base_type
== GLSL_TYPE_VOID
) {
625 assert(explicit_stride
== 0 && !row_major
);
629 /* Matrix and vector types with explicit strides have to be looked up in a
630 * table so they're handled separately.
632 if (explicit_stride
> 0) {
633 const glsl_type
*bare_type
= get_instance(base_type
, rows
, columns
);
635 assert(columns
> 1 || !row_major
);
638 util_snprintf(name
, sizeof(name
), "%sx%uB%s", bare_type
->name
,
639 explicit_stride
, row_major
? "RM" : "");
641 mtx_lock(&glsl_type::hash_mutex
);
643 if (explicit_matrix_types
== NULL
) {
644 explicit_matrix_types
=
645 _mesa_hash_table_create(NULL
, _mesa_key_hash_string
,
646 _mesa_key_string_equal
);
649 const struct hash_entry
*entry
=
650 _mesa_hash_table_search(explicit_matrix_types
, name
);
652 const glsl_type
*t
= new glsl_type(bare_type
->gl_type
,
653 (glsl_base_type
)base_type
,
655 explicit_stride
, row_major
);
657 entry
= _mesa_hash_table_insert(explicit_matrix_types
,
661 assert(((glsl_type
*) entry
->data
)->base_type
== base_type
);
662 assert(((glsl_type
*) entry
->data
)->vector_elements
== rows
);
663 assert(((glsl_type
*) entry
->data
)->matrix_columns
== columns
);
664 assert(((glsl_type
*) entry
->data
)->explicit_stride
== explicit_stride
);
666 mtx_unlock(&glsl_type::hash_mutex
);
668 return (const glsl_type
*) entry
->data
;
673 /* Treat GLSL vectors as Nx1 matrices.
681 case GLSL_TYPE_FLOAT
:
683 case GLSL_TYPE_FLOAT16
:
685 case GLSL_TYPE_DOUBLE
:
689 case GLSL_TYPE_UINT64
:
691 case GLSL_TYPE_INT64
:
693 case GLSL_TYPE_UINT16
:
695 case GLSL_TYPE_INT16
:
697 case GLSL_TYPE_UINT8
:
705 if ((base_type
!= GLSL_TYPE_FLOAT
&&
706 base_type
!= GLSL_TYPE_DOUBLE
&&
707 base_type
!= GLSL_TYPE_FLOAT16
) || (rows
== 1))
710 /* GLSL matrix types are named mat{COLUMNS}x{ROWS}. Only the following
711 * combinations are valid:
719 #define IDX(c,r) (((c-1)*3) + (r-1))
722 case GLSL_TYPE_DOUBLE
: {
723 switch (IDX(columns
, rows
)) {
724 case IDX(2,2): return dmat2_type
;
725 case IDX(2,3): return dmat2x3_type
;
726 case IDX(2,4): return dmat2x4_type
;
727 case IDX(3,2): return dmat3x2_type
;
728 case IDX(3,3): return dmat3_type
;
729 case IDX(3,4): return dmat3x4_type
;
730 case IDX(4,2): return dmat4x2_type
;
731 case IDX(4,3): return dmat4x3_type
;
732 case IDX(4,4): return dmat4_type
;
733 default: return error_type
;
736 case GLSL_TYPE_FLOAT
: {
737 switch (IDX(columns
, rows
)) {
738 case IDX(2,2): return mat2_type
;
739 case IDX(2,3): return mat2x3_type
;
740 case IDX(2,4): return mat2x4_type
;
741 case IDX(3,2): return mat3x2_type
;
742 case IDX(3,3): return mat3_type
;
743 case IDX(3,4): return mat3x4_type
;
744 case IDX(4,2): return mat4x2_type
;
745 case IDX(4,3): return mat4x3_type
;
746 case IDX(4,4): return mat4_type
;
747 default: return error_type
;
750 case GLSL_TYPE_FLOAT16
: {
751 switch (IDX(columns
, rows
)) {
752 case IDX(2,2): return f16mat2_type
;
753 case IDX(2,3): return f16mat2x3_type
;
754 case IDX(2,4): return f16mat2x4_type
;
755 case IDX(3,2): return f16mat3x2_type
;
756 case IDX(3,3): return f16mat3_type
;
757 case IDX(3,4): return f16mat3x4_type
;
758 case IDX(4,2): return f16mat4x2_type
;
759 case IDX(4,3): return f16mat4x3_type
;
760 case IDX(4,4): return f16mat4_type
;
761 default: return error_type
;
764 default: return error_type
;
768 assert(!"Should not get here.");
773 glsl_type::get_sampler_instance(enum glsl_sampler_dim dim
,
779 case GLSL_TYPE_FLOAT
:
781 case GLSL_SAMPLER_DIM_1D
:
783 return (array
? sampler1DArrayShadow_type
: sampler1DShadow_type
);
785 return (array
? sampler1DArray_type
: sampler1D_type
);
786 case GLSL_SAMPLER_DIM_2D
:
788 return (array
? sampler2DArrayShadow_type
: sampler2DShadow_type
);
790 return (array
? sampler2DArray_type
: sampler2D_type
);
791 case GLSL_SAMPLER_DIM_3D
:
795 return sampler3D_type
;
796 case GLSL_SAMPLER_DIM_CUBE
:
798 return (array
? samplerCubeArrayShadow_type
: samplerCubeShadow_type
);
800 return (array
? samplerCubeArray_type
: samplerCube_type
);
801 case GLSL_SAMPLER_DIM_RECT
:
805 return sampler2DRectShadow_type
;
807 return sampler2DRect_type
;
808 case GLSL_SAMPLER_DIM_BUF
:
812 return samplerBuffer_type
;
813 case GLSL_SAMPLER_DIM_MS
:
816 return (array
? sampler2DMSArray_type
: sampler2DMS_type
);
817 case GLSL_SAMPLER_DIM_EXTERNAL
:
821 return samplerExternalOES_type
;
822 case GLSL_SAMPLER_DIM_SUBPASS
:
823 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
830 case GLSL_SAMPLER_DIM_1D
:
831 return (array
? isampler1DArray_type
: isampler1D_type
);
832 case GLSL_SAMPLER_DIM_2D
:
833 return (array
? isampler2DArray_type
: isampler2D_type
);
834 case GLSL_SAMPLER_DIM_3D
:
837 return isampler3D_type
;
838 case GLSL_SAMPLER_DIM_CUBE
:
839 return (array
? isamplerCubeArray_type
: isamplerCube_type
);
840 case GLSL_SAMPLER_DIM_RECT
:
843 return isampler2DRect_type
;
844 case GLSL_SAMPLER_DIM_BUF
:
847 return isamplerBuffer_type
;
848 case GLSL_SAMPLER_DIM_MS
:
849 return (array
? isampler2DMSArray_type
: isampler2DMS_type
);
850 case GLSL_SAMPLER_DIM_EXTERNAL
:
852 case GLSL_SAMPLER_DIM_SUBPASS
:
853 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
860 case GLSL_SAMPLER_DIM_1D
:
861 return (array
? usampler1DArray_type
: usampler1D_type
);
862 case GLSL_SAMPLER_DIM_2D
:
863 return (array
? usampler2DArray_type
: usampler2D_type
);
864 case GLSL_SAMPLER_DIM_3D
:
867 return usampler3D_type
;
868 case GLSL_SAMPLER_DIM_CUBE
:
869 return (array
? usamplerCubeArray_type
: usamplerCube_type
);
870 case GLSL_SAMPLER_DIM_RECT
:
873 return usampler2DRect_type
;
874 case GLSL_SAMPLER_DIM_BUF
:
877 return usamplerBuffer_type
;
878 case GLSL_SAMPLER_DIM_MS
:
879 return (array
? usampler2DMSArray_type
: usampler2DMS_type
);
880 case GLSL_SAMPLER_DIM_EXTERNAL
:
882 case GLSL_SAMPLER_DIM_SUBPASS
:
883 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
890 unreachable("switch statement above should be complete");
894 glsl_type::get_image_instance(enum glsl_sampler_dim dim
,
895 bool array
, glsl_base_type type
)
898 case GLSL_TYPE_FLOAT
:
900 case GLSL_SAMPLER_DIM_1D
:
901 return (array
? image1DArray_type
: image1D_type
);
902 case GLSL_SAMPLER_DIM_2D
:
903 return (array
? image2DArray_type
: image2D_type
);
904 case GLSL_SAMPLER_DIM_3D
:
906 case GLSL_SAMPLER_DIM_CUBE
:
907 return (array
? imageCubeArray_type
: imageCube_type
);
908 case GLSL_SAMPLER_DIM_RECT
:
912 return image2DRect_type
;
913 case GLSL_SAMPLER_DIM_BUF
:
917 return imageBuffer_type
;
918 case GLSL_SAMPLER_DIM_MS
:
919 return (array
? image2DMSArray_type
: image2DMS_type
);
920 case GLSL_SAMPLER_DIM_SUBPASS
:
921 return subpassInput_type
;
922 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
923 return subpassInputMS_type
;
924 case GLSL_SAMPLER_DIM_EXTERNAL
:
929 case GLSL_SAMPLER_DIM_1D
:
930 return (array
? iimage1DArray_type
: iimage1D_type
);
931 case GLSL_SAMPLER_DIM_2D
:
932 return (array
? iimage2DArray_type
: iimage2D_type
);
933 case GLSL_SAMPLER_DIM_3D
:
936 return iimage3D_type
;
937 case GLSL_SAMPLER_DIM_CUBE
:
938 return (array
? iimageCubeArray_type
: iimageCube_type
);
939 case GLSL_SAMPLER_DIM_RECT
:
942 return iimage2DRect_type
;
943 case GLSL_SAMPLER_DIM_BUF
:
946 return iimageBuffer_type
;
947 case GLSL_SAMPLER_DIM_MS
:
948 return (array
? iimage2DMSArray_type
: iimage2DMS_type
);
949 case GLSL_SAMPLER_DIM_SUBPASS
:
950 return isubpassInput_type
;
951 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
952 return isubpassInputMS_type
;
953 case GLSL_SAMPLER_DIM_EXTERNAL
:
958 case GLSL_SAMPLER_DIM_1D
:
959 return (array
? uimage1DArray_type
: uimage1D_type
);
960 case GLSL_SAMPLER_DIM_2D
:
961 return (array
? uimage2DArray_type
: uimage2D_type
);
962 case GLSL_SAMPLER_DIM_3D
:
965 return uimage3D_type
;
966 case GLSL_SAMPLER_DIM_CUBE
:
967 return (array
? uimageCubeArray_type
: uimageCube_type
);
968 case GLSL_SAMPLER_DIM_RECT
:
971 return uimage2DRect_type
;
972 case GLSL_SAMPLER_DIM_BUF
:
975 return uimageBuffer_type
;
976 case GLSL_SAMPLER_DIM_MS
:
977 return (array
? uimage2DMSArray_type
: uimage2DMS_type
);
978 case GLSL_SAMPLER_DIM_SUBPASS
:
979 return usubpassInput_type
;
980 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
981 return usubpassInputMS_type
;
982 case GLSL_SAMPLER_DIM_EXTERNAL
:
989 unreachable("switch statement above should be complete");
993 glsl_type::get_array_instance(const glsl_type
*base
,
995 unsigned explicit_stride
)
997 /* Generate a name using the base type pointer in the key. This is
998 * done because the name of the base type may not be unique across
999 * shaders. For example, two shaders may have different record types
1003 util_snprintf(key
, sizeof(key
), "%p[%u]x%uB", (void *) base
, array_size
,
1006 mtx_lock(&glsl_type::hash_mutex
);
1008 if (array_types
== NULL
) {
1009 array_types
= _mesa_hash_table_create(NULL
, _mesa_key_hash_string
,
1010 _mesa_key_string_equal
);
1013 const struct hash_entry
*entry
= _mesa_hash_table_search(array_types
, key
);
1014 if (entry
== NULL
) {
1015 const glsl_type
*t
= new glsl_type(base
, array_size
, explicit_stride
);
1017 entry
= _mesa_hash_table_insert(array_types
,
1022 assert(((glsl_type
*) entry
->data
)->base_type
== GLSL_TYPE_ARRAY
);
1023 assert(((glsl_type
*) entry
->data
)->length
== array_size
);
1024 assert(((glsl_type
*) entry
->data
)->fields
.array
== base
);
1026 mtx_unlock(&glsl_type::hash_mutex
);
1028 return (glsl_type
*) entry
->data
;
1033 glsl_type::record_compare(const glsl_type
*b
, bool match_name
,
1034 bool match_locations
) const
1036 if (this->length
!= b
->length
)
1039 if (this->interface_packing
!= b
->interface_packing
)
1042 if (this->interface_row_major
!= b
->interface_row_major
)
1045 /* From the GLSL 4.20 specification (Sec 4.2):
1047 * "Structures must have the same name, sequence of type names, and
1048 * type definitions, and field names to be considered the same type."
1050 * GLSL ES behaves the same (Ver 1.00 Sec 4.2.4, Ver 3.00 Sec 4.2.5).
1052 * Section 7.4.1 (Shader Interface Matching) of the OpenGL 4.30 spec says:
1054 * "Variables or block members declared as structures are considered
1055 * to match in type if and only if structure members match in name,
1056 * type, qualification, and declaration order."
1059 if (strcmp(this->name
, b
->name
) != 0)
1062 for (unsigned i
= 0; i
< this->length
; i
++) {
1063 if (this->fields
.structure
[i
].type
!= b
->fields
.structure
[i
].type
)
1065 if (strcmp(this->fields
.structure
[i
].name
,
1066 b
->fields
.structure
[i
].name
) != 0)
1068 if (this->fields
.structure
[i
].matrix_layout
1069 != b
->fields
.structure
[i
].matrix_layout
)
1071 if (match_locations
&& this->fields
.structure
[i
].location
1072 != b
->fields
.structure
[i
].location
)
1074 if (this->fields
.structure
[i
].offset
1075 != b
->fields
.structure
[i
].offset
)
1077 if (this->fields
.structure
[i
].interpolation
1078 != b
->fields
.structure
[i
].interpolation
)
1080 if (this->fields
.structure
[i
].centroid
1081 != b
->fields
.structure
[i
].centroid
)
1083 if (this->fields
.structure
[i
].sample
1084 != b
->fields
.structure
[i
].sample
)
1086 if (this->fields
.structure
[i
].patch
1087 != b
->fields
.structure
[i
].patch
)
1089 if (this->fields
.structure
[i
].memory_read_only
1090 != b
->fields
.structure
[i
].memory_read_only
)
1092 if (this->fields
.structure
[i
].memory_write_only
1093 != b
->fields
.structure
[i
].memory_write_only
)
1095 if (this->fields
.structure
[i
].memory_coherent
1096 != b
->fields
.structure
[i
].memory_coherent
)
1098 if (this->fields
.structure
[i
].memory_volatile
1099 != b
->fields
.structure
[i
].memory_volatile
)
1101 if (this->fields
.structure
[i
].memory_restrict
1102 != b
->fields
.structure
[i
].memory_restrict
)
1104 if (this->fields
.structure
[i
].image_format
1105 != b
->fields
.structure
[i
].image_format
)
1107 if (this->fields
.structure
[i
].precision
1108 != b
->fields
.structure
[i
].precision
)
1110 if (this->fields
.structure
[i
].explicit_xfb_buffer
1111 != b
->fields
.structure
[i
].explicit_xfb_buffer
)
1113 if (this->fields
.structure
[i
].xfb_buffer
1114 != b
->fields
.structure
[i
].xfb_buffer
)
1116 if (this->fields
.structure
[i
].xfb_stride
1117 != b
->fields
.structure
[i
].xfb_stride
)
1126 glsl_type::record_key_compare(const void *a
, const void *b
)
1128 const glsl_type
*const key1
= (glsl_type
*) a
;
1129 const glsl_type
*const key2
= (glsl_type
*) b
;
1131 return strcmp(key1
->name
, key2
->name
) == 0 &&
1132 key1
->record_compare(key2
, true);
1137 * Generate an integer hash value for a glsl_type structure type.
1140 glsl_type::record_key_hash(const void *a
)
1142 const glsl_type
*const key
= (glsl_type
*) a
;
1143 uintptr_t hash
= key
->length
;
1146 for (unsigned i
= 0; i
< key
->length
; i
++) {
1147 /* casting pointer to uintptr_t */
1148 hash
= (hash
* 13 ) + (uintptr_t) key
->fields
.structure
[i
].type
;
1151 if (sizeof(hash
) == 8)
1152 retval
= (hash
& 0xffffffff) ^ ((uint64_t) hash
>> 32);
1161 glsl_type::get_struct_instance(const glsl_struct_field
*fields
,
1162 unsigned num_fields
,
1166 const glsl_type
key(fields
, num_fields
, name
, packed
);
1168 mtx_lock(&glsl_type::hash_mutex
);
1170 if (struct_types
== NULL
) {
1171 struct_types
= _mesa_hash_table_create(NULL
, record_key_hash
,
1172 record_key_compare
);
1175 const struct hash_entry
*entry
= _mesa_hash_table_search(struct_types
,
1177 if (entry
== NULL
) {
1178 const glsl_type
*t
= new glsl_type(fields
, num_fields
, name
, packed
);
1180 entry
= _mesa_hash_table_insert(struct_types
, t
, (void *) t
);
1183 assert(((glsl_type
*) entry
->data
)->base_type
== GLSL_TYPE_STRUCT
);
1184 assert(((glsl_type
*) entry
->data
)->length
== num_fields
);
1185 assert(strcmp(((glsl_type
*) entry
->data
)->name
, name
) == 0);
1186 assert(((glsl_type
*) entry
->data
)->packed
== packed
);
1188 mtx_unlock(&glsl_type::hash_mutex
);
1190 return (glsl_type
*) entry
->data
;
1195 glsl_type::get_interface_instance(const glsl_struct_field
*fields
,
1196 unsigned num_fields
,
1197 enum glsl_interface_packing packing
,
1199 const char *block_name
)
1201 const glsl_type
key(fields
, num_fields
, packing
, row_major
, block_name
);
1203 mtx_lock(&glsl_type::hash_mutex
);
1205 if (interface_types
== NULL
) {
1206 interface_types
= _mesa_hash_table_create(NULL
, record_key_hash
,
1207 record_key_compare
);
1210 const struct hash_entry
*entry
= _mesa_hash_table_search(interface_types
,
1212 if (entry
== NULL
) {
1213 const glsl_type
*t
= new glsl_type(fields
, num_fields
,
1214 packing
, row_major
, block_name
);
1216 entry
= _mesa_hash_table_insert(interface_types
, t
, (void *) t
);
1219 assert(((glsl_type
*) entry
->data
)->base_type
== GLSL_TYPE_INTERFACE
);
1220 assert(((glsl_type
*) entry
->data
)->length
== num_fields
);
1221 assert(strcmp(((glsl_type
*) entry
->data
)->name
, block_name
) == 0);
1223 mtx_unlock(&glsl_type::hash_mutex
);
1225 return (glsl_type
*) entry
->data
;
1229 glsl_type::get_subroutine_instance(const char *subroutine_name
)
1231 const glsl_type
key(subroutine_name
);
1233 mtx_lock(&glsl_type::hash_mutex
);
1235 if (subroutine_types
== NULL
) {
1236 subroutine_types
= _mesa_hash_table_create(NULL
, record_key_hash
,
1237 record_key_compare
);
1240 const struct hash_entry
*entry
= _mesa_hash_table_search(subroutine_types
,
1242 if (entry
== NULL
) {
1243 const glsl_type
*t
= new glsl_type(subroutine_name
);
1245 entry
= _mesa_hash_table_insert(subroutine_types
, t
, (void *) t
);
1248 assert(((glsl_type
*) entry
->data
)->base_type
== GLSL_TYPE_SUBROUTINE
);
1249 assert(strcmp(((glsl_type
*) entry
->data
)->name
, subroutine_name
) == 0);
1251 mtx_unlock(&glsl_type::hash_mutex
);
1253 return (glsl_type
*) entry
->data
;
1258 function_key_compare(const void *a
, const void *b
)
1260 const glsl_type
*const key1
= (glsl_type
*) a
;
1261 const glsl_type
*const key2
= (glsl_type
*) b
;
1263 if (key1
->length
!= key2
->length
)
1266 return memcmp(key1
->fields
.parameters
, key2
->fields
.parameters
,
1267 (key1
->length
+ 1) * sizeof(*key1
->fields
.parameters
)) == 0;
1272 function_key_hash(const void *a
)
1274 const glsl_type
*const key
= (glsl_type
*) a
;
1275 return _mesa_hash_data(key
->fields
.parameters
,
1276 (key
->length
+ 1) * sizeof(*key
->fields
.parameters
));
1280 glsl_type::get_function_instance(const glsl_type
*return_type
,
1281 const glsl_function_param
*params
,
1282 unsigned num_params
)
1284 const glsl_type
key(return_type
, params
, num_params
);
1286 mtx_lock(&glsl_type::hash_mutex
);
1288 if (function_types
== NULL
) {
1289 function_types
= _mesa_hash_table_create(NULL
, function_key_hash
,
1290 function_key_compare
);
1293 struct hash_entry
*entry
= _mesa_hash_table_search(function_types
, &key
);
1294 if (entry
== NULL
) {
1295 const glsl_type
*t
= new glsl_type(return_type
, params
, num_params
);
1297 entry
= _mesa_hash_table_insert(function_types
, t
, (void *) t
);
1300 const glsl_type
*t
= (const glsl_type
*)entry
->data
;
1302 assert(t
->base_type
== GLSL_TYPE_FUNCTION
);
1303 assert(t
->length
== num_params
);
1305 mtx_unlock(&glsl_type::hash_mutex
);
1312 glsl_type::get_mul_type(const glsl_type
*type_a
, const glsl_type
*type_b
)
1314 if (type_a
== type_b
) {
1316 } else if (type_a
->is_matrix() && type_b
->is_matrix()) {
1317 /* Matrix multiply. The columns of A must match the rows of B. Given
1318 * the other previously tested constraints, this means the vector type
1319 * of a row from A must be the same as the vector type of a column from
1322 if (type_a
->row_type() == type_b
->column_type()) {
1323 /* The resulting matrix has the number of columns of matrix B and
1324 * the number of rows of matrix A. We get the row count of A by
1325 * looking at the size of a vector that makes up a column. The
1326 * transpose (size of a row) is done for B.
1328 const glsl_type
*const type
=
1329 get_instance(type_a
->base_type
,
1330 type_a
->column_type()->vector_elements
,
1331 type_b
->row_type()->vector_elements
);
1332 assert(type
!= error_type
);
1336 } else if (type_a
->is_matrix()) {
1337 /* A is a matrix and B is a column vector. Columns of A must match
1338 * rows of B. Given the other previously tested constraints, this
1339 * means the vector type of a row from A must be the same as the
1340 * vector the type of B.
1342 if (type_a
->row_type() == type_b
) {
1343 /* The resulting vector has a number of elements equal to
1344 * the number of rows of matrix A. */
1345 const glsl_type
*const type
=
1346 get_instance(type_a
->base_type
,
1347 type_a
->column_type()->vector_elements
,
1349 assert(type
!= error_type
);
1354 assert(type_b
->is_matrix());
1356 /* A is a row vector and B is a matrix. Columns of A must match rows
1357 * of B. Given the other previously tested constraints, this means
1358 * the type of A must be the same as the vector type of a column from
1361 if (type_a
== type_b
->column_type()) {
1362 /* The resulting vector has a number of elements equal to
1363 * the number of columns of matrix B. */
1364 const glsl_type
*const type
=
1365 get_instance(type_a
->base_type
,
1366 type_b
->row_type()->vector_elements
,
1368 assert(type
!= error_type
);
1379 glsl_type::field_type(const char *name
) const
1381 if (this->base_type
!= GLSL_TYPE_STRUCT
1382 && this->base_type
!= GLSL_TYPE_INTERFACE
)
1385 for (unsigned i
= 0; i
< this->length
; i
++) {
1386 if (strcmp(name
, this->fields
.structure
[i
].name
) == 0)
1387 return this->fields
.structure
[i
].type
;
1395 glsl_type::field_index(const char *name
) const
1397 if (this->base_type
!= GLSL_TYPE_STRUCT
1398 && this->base_type
!= GLSL_TYPE_INTERFACE
)
1401 for (unsigned i
= 0; i
< this->length
; i
++) {
1402 if (strcmp(name
, this->fields
.structure
[i
].name
) == 0)
1411 glsl_type::component_slots() const
1413 switch (this->base_type
) {
1414 case GLSL_TYPE_UINT
:
1416 case GLSL_TYPE_UINT8
:
1417 case GLSL_TYPE_INT8
:
1418 case GLSL_TYPE_UINT16
:
1419 case GLSL_TYPE_INT16
:
1420 case GLSL_TYPE_FLOAT
:
1421 case GLSL_TYPE_FLOAT16
:
1422 case GLSL_TYPE_BOOL
:
1423 return this->components();
1425 case GLSL_TYPE_DOUBLE
:
1426 case GLSL_TYPE_UINT64
:
1427 case GLSL_TYPE_INT64
:
1428 return 2 * this->components();
1430 case GLSL_TYPE_STRUCT
:
1431 case GLSL_TYPE_INTERFACE
: {
1434 for (unsigned i
= 0; i
< this->length
; i
++)
1435 size
+= this->fields
.structure
[i
].type
->component_slots();
1440 case GLSL_TYPE_ARRAY
:
1441 return this->length
* this->fields
.array
->component_slots();
1443 case GLSL_TYPE_SAMPLER
:
1444 case GLSL_TYPE_IMAGE
:
1447 case GLSL_TYPE_SUBROUTINE
:
1450 case GLSL_TYPE_FUNCTION
:
1451 case GLSL_TYPE_ATOMIC_UINT
:
1452 case GLSL_TYPE_VOID
:
1453 case GLSL_TYPE_ERROR
:
1461 glsl_type::struct_location_offset(unsigned length
) const
1463 unsigned offset
= 0;
1464 const glsl_type
*t
= this->without_array();
1465 if (t
->is_struct()) {
1466 assert(length
<= t
->length
);
1468 for (unsigned i
= 0; i
< length
; i
++) {
1469 const glsl_type
*st
= t
->fields
.structure
[i
].type
;
1470 const glsl_type
*wa
= st
->without_array();
1471 if (wa
->is_struct()) {
1472 unsigned r_offset
= wa
->struct_location_offset(wa
->length
);
1473 offset
+= st
->is_array() ?
1474 st
->arrays_of_arrays_size() * r_offset
: r_offset
;
1475 } else if (st
->is_array() && st
->fields
.array
->is_array()) {
1476 unsigned outer_array_size
= st
->length
;
1477 const glsl_type
*base_type
= st
->fields
.array
;
1479 /* For arrays of arrays the outer arrays take up a uniform
1480 * slot for each element. The innermost array elements share a
1481 * single slot so we ignore the innermost array when calculating
1484 while (base_type
->fields
.array
->is_array()) {
1485 outer_array_size
= outer_array_size
* base_type
->length
;
1486 base_type
= base_type
->fields
.array
;
1488 offset
+= outer_array_size
;
1490 /* We dont worry about arrays here because unless the array
1491 * contains a structure or another array it only takes up a single
1502 glsl_type::uniform_locations() const
1506 switch (this->base_type
) {
1507 case GLSL_TYPE_UINT
:
1509 case GLSL_TYPE_FLOAT
:
1510 case GLSL_TYPE_FLOAT16
:
1511 case GLSL_TYPE_DOUBLE
:
1512 case GLSL_TYPE_UINT16
:
1513 case GLSL_TYPE_UINT8
:
1514 case GLSL_TYPE_INT16
:
1515 case GLSL_TYPE_INT8
:
1516 case GLSL_TYPE_UINT64
:
1517 case GLSL_TYPE_INT64
:
1518 case GLSL_TYPE_BOOL
:
1519 case GLSL_TYPE_SAMPLER
:
1520 case GLSL_TYPE_IMAGE
:
1521 case GLSL_TYPE_SUBROUTINE
:
1524 case GLSL_TYPE_STRUCT
:
1525 case GLSL_TYPE_INTERFACE
:
1526 for (unsigned i
= 0; i
< this->length
; i
++)
1527 size
+= this->fields
.structure
[i
].type
->uniform_locations();
1529 case GLSL_TYPE_ARRAY
:
1530 return this->length
* this->fields
.array
->uniform_locations();
1537 glsl_type::varying_count() const
1541 switch (this->base_type
) {
1542 case GLSL_TYPE_UINT
:
1544 case GLSL_TYPE_FLOAT
:
1545 case GLSL_TYPE_FLOAT16
:
1546 case GLSL_TYPE_DOUBLE
:
1547 case GLSL_TYPE_BOOL
:
1548 case GLSL_TYPE_UINT16
:
1549 case GLSL_TYPE_UINT8
:
1550 case GLSL_TYPE_INT16
:
1551 case GLSL_TYPE_INT8
:
1552 case GLSL_TYPE_UINT64
:
1553 case GLSL_TYPE_INT64
:
1556 case GLSL_TYPE_STRUCT
:
1557 case GLSL_TYPE_INTERFACE
:
1558 for (unsigned i
= 0; i
< this->length
; i
++)
1559 size
+= this->fields
.structure
[i
].type
->varying_count();
1561 case GLSL_TYPE_ARRAY
:
1562 /* Don't count innermost array elements */
1563 if (this->without_array()->is_struct() ||
1564 this->without_array()->is_interface() ||
1565 this->fields
.array
->is_array())
1566 return this->length
* this->fields
.array
->varying_count();
1568 return this->fields
.array
->varying_count();
1570 assert(!"unsupported varying type");
1576 glsl_type::can_implicitly_convert_to(const glsl_type
*desired
,
1577 _mesa_glsl_parse_state
*state
) const
1579 if (this == desired
)
1582 /* GLSL 1.10 and ESSL do not allow implicit conversions. If there is no
1583 * state, we're doing intra-stage function linking where these checks have
1584 * already been done.
1586 if (state
&& !state
->has_implicit_conversions())
1589 /* There is no conversion among matrix types. */
1590 if (this->matrix_columns
> 1 || desired
->matrix_columns
> 1)
1593 /* Vector size must match. */
1594 if (this->vector_elements
!= desired
->vector_elements
)
1597 /* int and uint can be converted to float. */
1598 if (desired
->is_float() && this->is_integer())
1601 /* With GLSL 4.0, ARB_gpu_shader5, or MESA_shader_integer_functions, int
1602 * can be converted to uint. Note that state may be NULL here, when
1603 * resolving function calls in the linker. By this time, all the
1604 * state-dependent checks have already happened though, so allow anything
1605 * that's allowed in any shader version.
1607 if ((!state
|| state
->has_implicit_uint_to_int_conversion()) &&
1608 desired
->base_type
== GLSL_TYPE_UINT
&& this->base_type
== GLSL_TYPE_INT
)
1611 /* No implicit conversions from double. */
1612 if ((!state
|| state
->has_double()) && this->is_double())
1615 /* Conversions from different types to double. */
1616 if ((!state
|| state
->has_double()) && desired
->is_double()) {
1617 if (this->is_float())
1619 if (this->is_integer())
1627 glsl_type::std140_base_alignment(bool row_major
) const
1629 unsigned N
= is_64bit() ? 8 : 4;
1631 /* (1) If the member is a scalar consuming <N> basic machine units, the
1632 * base alignment is <N>.
1634 * (2) If the member is a two- or four-component vector with components
1635 * consuming <N> basic machine units, the base alignment is 2<N> or
1636 * 4<N>, respectively.
1638 * (3) If the member is a three-component vector with components consuming
1639 * <N> basic machine units, the base alignment is 4<N>.
1641 if (this->is_scalar() || this->is_vector()) {
1642 switch (this->vector_elements
) {
1653 /* (4) If the member is an array of scalars or vectors, the base alignment
1654 * and array stride are set to match the base alignment of a single
1655 * array element, according to rules (1), (2), and (3), and rounded up
1656 * to the base alignment of a vec4. The array may have padding at the
1657 * end; the base offset of the member following the array is rounded up
1658 * to the next multiple of the base alignment.
1660 * (6) If the member is an array of <S> column-major matrices with <C>
1661 * columns and <R> rows, the matrix is stored identically to a row of
1662 * <S>*<C> column vectors with <R> components each, according to rule
1665 * (8) If the member is an array of <S> row-major matrices with <C> columns
1666 * and <R> rows, the matrix is stored identically to a row of <S>*<R>
1667 * row vectors with <C> components each, according to rule (4).
1669 * (10) If the member is an array of <S> structures, the <S> elements of
1670 * the array are laid out in order, according to rule (9).
1672 if (this->is_array()) {
1673 if (this->fields
.array
->is_scalar() ||
1674 this->fields
.array
->is_vector() ||
1675 this->fields
.array
->is_matrix()) {
1676 return MAX2(this->fields
.array
->std140_base_alignment(row_major
), 16);
1678 assert(this->fields
.array
->is_struct() ||
1679 this->fields
.array
->is_array());
1680 return this->fields
.array
->std140_base_alignment(row_major
);
1684 /* (5) If the member is a column-major matrix with <C> columns and
1685 * <R> rows, the matrix is stored identically to an array of
1686 * <C> column vectors with <R> components each, according to
1689 * (7) If the member is a row-major matrix with <C> columns and <R>
1690 * rows, the matrix is stored identically to an array of <R>
1691 * row vectors with <C> components each, according to rule (4).
1693 if (this->is_matrix()) {
1694 const struct glsl_type
*vec_type
, *array_type
;
1695 int c
= this->matrix_columns
;
1696 int r
= this->vector_elements
;
1699 vec_type
= get_instance(base_type
, c
, 1);
1700 array_type
= glsl_type::get_array_instance(vec_type
, r
);
1702 vec_type
= get_instance(base_type
, r
, 1);
1703 array_type
= glsl_type::get_array_instance(vec_type
, c
);
1706 return array_type
->std140_base_alignment(false);
1709 /* (9) If the member is a structure, the base alignment of the
1710 * structure is <N>, where <N> is the largest base alignment
1711 * value of any of its members, and rounded up to the base
1712 * alignment of a vec4. The individual members of this
1713 * sub-structure are then assigned offsets by applying this set
1714 * of rules recursively, where the base offset of the first
1715 * member of the sub-structure is equal to the aligned offset
1716 * of the structure. The structure may have padding at the end;
1717 * the base offset of the member following the sub-structure is
1718 * rounded up to the next multiple of the base alignment of the
1721 if (this->is_struct()) {
1722 unsigned base_alignment
= 16;
1723 for (unsigned i
= 0; i
< this->length
; i
++) {
1724 bool field_row_major
= row_major
;
1725 const enum glsl_matrix_layout matrix_layout
=
1726 glsl_matrix_layout(this->fields
.structure
[i
].matrix_layout
);
1727 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
1728 field_row_major
= true;
1729 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
1730 field_row_major
= false;
1733 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
1734 base_alignment
= MAX2(base_alignment
,
1735 field_type
->std140_base_alignment(field_row_major
));
1737 return base_alignment
;
1740 assert(!"not reached");
1745 glsl_type::std140_size(bool row_major
) const
1747 unsigned N
= is_64bit() ? 8 : 4;
1749 /* (1) If the member is a scalar consuming <N> basic machine units, the
1750 * base alignment is <N>.
1752 * (2) If the member is a two- or four-component vector with components
1753 * consuming <N> basic machine units, the base alignment is 2<N> or
1754 * 4<N>, respectively.
1756 * (3) If the member is a three-component vector with components consuming
1757 * <N> basic machine units, the base alignment is 4<N>.
1759 if (this->is_scalar() || this->is_vector()) {
1760 assert(this->explicit_stride
== 0);
1761 return this->vector_elements
* N
;
1764 /* (5) If the member is a column-major matrix with <C> columns and
1765 * <R> rows, the matrix is stored identically to an array of
1766 * <C> column vectors with <R> components each, according to
1769 * (6) If the member is an array of <S> column-major matrices with <C>
1770 * columns and <R> rows, the matrix is stored identically to a row of
1771 * <S>*<C> column vectors with <R> components each, according to rule
1774 * (7) If the member is a row-major matrix with <C> columns and <R>
1775 * rows, the matrix is stored identically to an array of <R>
1776 * row vectors with <C> components each, according to rule (4).
1778 * (8) If the member is an array of <S> row-major matrices with <C> columns
1779 * and <R> rows, the matrix is stored identically to a row of <S>*<R>
1780 * row vectors with <C> components each, according to rule (4).
1782 if (this->without_array()->is_matrix()) {
1783 const struct glsl_type
*element_type
;
1784 const struct glsl_type
*vec_type
;
1785 unsigned int array_len
;
1787 if (this->is_array()) {
1788 element_type
= this->without_array();
1789 array_len
= this->arrays_of_arrays_size();
1791 element_type
= this;
1796 vec_type
= get_instance(element_type
->base_type
,
1797 element_type
->matrix_columns
, 1);
1799 array_len
*= element_type
->vector_elements
;
1801 vec_type
= get_instance(element_type
->base_type
,
1802 element_type
->vector_elements
, 1);
1803 array_len
*= element_type
->matrix_columns
;
1805 const glsl_type
*array_type
= glsl_type::get_array_instance(vec_type
,
1808 return array_type
->std140_size(false);
1811 /* (4) If the member is an array of scalars or vectors, the base alignment
1812 * and array stride are set to match the base alignment of a single
1813 * array element, according to rules (1), (2), and (3), and rounded up
1814 * to the base alignment of a vec4. The array may have padding at the
1815 * end; the base offset of the member following the array is rounded up
1816 * to the next multiple of the base alignment.
1818 * (10) If the member is an array of <S> structures, the <S> elements of
1819 * the array are laid out in order, according to rule (9).
1821 if (this->is_array()) {
1823 if (this->without_array()->is_struct()) {
1824 stride
= this->without_array()->std140_size(row_major
);
1826 unsigned element_base_align
=
1827 this->without_array()->std140_base_alignment(row_major
);
1828 stride
= MAX2(element_base_align
, 16);
1831 unsigned size
= this->arrays_of_arrays_size() * stride
;
1832 assert(this->explicit_stride
== 0 ||
1833 size
== this->length
* this->explicit_stride
);
1837 /* (9) If the member is a structure, the base alignment of the
1838 * structure is <N>, where <N> is the largest base alignment
1839 * value of any of its members, and rounded up to the base
1840 * alignment of a vec4. The individual members of this
1841 * sub-structure are then assigned offsets by applying this set
1842 * of rules recursively, where the base offset of the first
1843 * member of the sub-structure is equal to the aligned offset
1844 * of the structure. The structure may have padding at the end;
1845 * the base offset of the member following the sub-structure is
1846 * rounded up to the next multiple of the base alignment of the
1849 if (this->is_struct() || this->is_interface()) {
1851 unsigned max_align
= 0;
1853 for (unsigned i
= 0; i
< this->length
; i
++) {
1854 bool field_row_major
= row_major
;
1855 const enum glsl_matrix_layout matrix_layout
=
1856 glsl_matrix_layout(this->fields
.structure
[i
].matrix_layout
);
1857 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
1858 field_row_major
= true;
1859 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
1860 field_row_major
= false;
1863 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
1864 unsigned align
= field_type
->std140_base_alignment(field_row_major
);
1866 /* Ignore unsized arrays when calculating size */
1867 if (field_type
->is_unsized_array())
1870 size
= glsl_align(size
, align
);
1871 size
+= field_type
->std140_size(field_row_major
);
1873 max_align
= MAX2(align
, max_align
);
1875 if (field_type
->is_struct() && (i
+ 1 < this->length
))
1876 size
= glsl_align(size
, 16);
1878 size
= glsl_align(size
, MAX2(max_align
, 16));
1882 assert(!"not reached");
1887 glsl_type::get_explicit_std140_type(bool row_major
) const
1889 if (this->is_vector() || this->is_scalar()) {
1891 } else if (this->is_matrix()) {
1892 const glsl_type
*vec_type
;
1894 vec_type
= get_instance(this->base_type
, this->matrix_columns
, 1);
1896 vec_type
= get_instance(this->base_type
, this->vector_elements
, 1);
1897 unsigned elem_size
= vec_type
->std140_size(false);
1898 unsigned stride
= glsl_align(elem_size
, 16);
1899 return get_instance(this->base_type
, this->vector_elements
,
1900 this->matrix_columns
, stride
, row_major
);
1901 } else if (this->is_array()) {
1902 unsigned elem_size
= this->fields
.array
->std140_size(row_major
);
1903 const glsl_type
*elem_type
=
1904 this->fields
.array
->get_explicit_std140_type(row_major
);
1905 unsigned stride
= glsl_align(elem_size
, 16);
1906 return get_array_instance(elem_type
, this->length
, stride
);
1907 } else if (this->is_struct() || this->is_interface()) {
1908 glsl_struct_field
*fields
= new glsl_struct_field
[this->length
];
1909 unsigned offset
= 0;
1910 for (unsigned i
= 0; i
< length
; i
++) {
1911 fields
[i
] = this->fields
.structure
[i
];
1913 bool field_row_major
= row_major
;
1914 if (fields
[i
].matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
1915 field_row_major
= false;
1916 } else if (fields
[i
].matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
1917 field_row_major
= true;
1920 fields
[i
].type
->get_explicit_std140_type(field_row_major
);
1922 unsigned fsize
= fields
[i
].type
->std140_size(field_row_major
);
1923 unsigned falign
= fields
[i
].type
->std140_base_alignment(field_row_major
);
1924 /* From the GLSL 460 spec section "Uniform and Shader Storage Block
1925 * Layout Qualifiers":
1927 * "The actual offset of a member is computed as follows: If
1928 * offset was declared, start with that offset, otherwise start
1929 * with the next available offset. If the resulting offset is not
1930 * a multiple of the actual alignment, increase it to the first
1931 * offset that is a multiple of the actual alignment. This results
1932 * in the actual offset the member will have."
1934 if (fields
[i
].offset
>= 0) {
1935 assert((unsigned)fields
[i
].offset
>= offset
);
1936 offset
= fields
[i
].offset
;
1938 offset
= glsl_align(offset
, falign
);
1939 fields
[i
].offset
= offset
;
1943 const glsl_type
*type
;
1944 if (this->is_struct())
1945 type
= get_struct_instance(fields
, this->length
, this->name
);
1947 type
= get_interface_instance(fields
, this->length
,
1948 (enum glsl_interface_packing
)this->interface_packing
,
1949 this->interface_row_major
,
1955 unreachable("Invalid type for UBO or SSBO");
1960 glsl_type::std430_base_alignment(bool row_major
) const
1963 unsigned N
= is_64bit() ? 8 : 4;
1965 /* (1) If the member is a scalar consuming <N> basic machine units, the
1966 * base alignment is <N>.
1968 * (2) If the member is a two- or four-component vector with components
1969 * consuming <N> basic machine units, the base alignment is 2<N> or
1970 * 4<N>, respectively.
1972 * (3) If the member is a three-component vector with components consuming
1973 * <N> basic machine units, the base alignment is 4<N>.
1975 if (this->is_scalar() || this->is_vector()) {
1976 switch (this->vector_elements
) {
1987 /* OpenGL 4.30 spec, section 7.6.2.2 "Standard Uniform Block Layout":
1989 * "When using the std430 storage layout, shader storage blocks will be
1990 * laid out in buffer storage identically to uniform and shader storage
1991 * blocks using the std140 layout, except that the base alignment and
1992 * stride of arrays of scalars and vectors in rule 4 and of structures
1993 * in rule 9 are not rounded up a multiple of the base alignment of a vec4.
1996 /* (1) If the member is a scalar consuming <N> basic machine units, the
1997 * base alignment is <N>.
1999 * (2) If the member is a two- or four-component vector with components
2000 * consuming <N> basic machine units, the base alignment is 2<N> or
2001 * 4<N>, respectively.
2003 * (3) If the member is a three-component vector with components consuming
2004 * <N> basic machine units, the base alignment is 4<N>.
2006 if (this->is_array())
2007 return this->fields
.array
->std430_base_alignment(row_major
);
2009 /* (5) If the member is a column-major matrix with <C> columns and
2010 * <R> rows, the matrix is stored identically to an array of
2011 * <C> column vectors with <R> components each, according to
2014 * (7) If the member is a row-major matrix with <C> columns and <R>
2015 * rows, the matrix is stored identically to an array of <R>
2016 * row vectors with <C> components each, according to rule (4).
2018 if (this->is_matrix()) {
2019 const struct glsl_type
*vec_type
, *array_type
;
2020 int c
= this->matrix_columns
;
2021 int r
= this->vector_elements
;
2024 vec_type
= get_instance(base_type
, c
, 1);
2025 array_type
= glsl_type::get_array_instance(vec_type
, r
);
2027 vec_type
= get_instance(base_type
, r
, 1);
2028 array_type
= glsl_type::get_array_instance(vec_type
, c
);
2031 return array_type
->std430_base_alignment(false);
2034 /* (9) If the member is a structure, the base alignment of the
2035 * structure is <N>, where <N> is the largest base alignment
2036 * value of any of its members, and rounded up to the base
2037 * alignment of a vec4. The individual members of this
2038 * sub-structure are then assigned offsets by applying this set
2039 * of rules recursively, where the base offset of the first
2040 * member of the sub-structure is equal to the aligned offset
2041 * of the structure. The structure may have padding at the end;
2042 * the base offset of the member following the sub-structure is
2043 * rounded up to the next multiple of the base alignment of the
2046 if (this->is_struct()) {
2047 unsigned base_alignment
= 0;
2048 for (unsigned i
= 0; i
< this->length
; i
++) {
2049 bool field_row_major
= row_major
;
2050 const enum glsl_matrix_layout matrix_layout
=
2051 glsl_matrix_layout(this->fields
.structure
[i
].matrix_layout
);
2052 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
2053 field_row_major
= true;
2054 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
2055 field_row_major
= false;
2058 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
2059 base_alignment
= MAX2(base_alignment
,
2060 field_type
->std430_base_alignment(field_row_major
));
2062 assert(base_alignment
> 0);
2063 return base_alignment
;
2065 assert(!"not reached");
2070 glsl_type::std430_array_stride(bool row_major
) const
2072 unsigned N
= is_64bit() ? 8 : 4;
2074 /* Notice that the array stride of a vec3 is not 3 * N but 4 * N.
2075 * See OpenGL 4.30 spec, section 7.6.2.2 "Standard Uniform Block Layout"
2077 * (3) If the member is a three-component vector with components consuming
2078 * <N> basic machine units, the base alignment is 4<N>.
2080 if (this->is_vector() && this->vector_elements
== 3)
2083 /* By default use std430_size(row_major) */
2084 unsigned stride
= this->std430_size(row_major
);
2085 assert(this->explicit_stride
== 0 || this->explicit_stride
== stride
);
2090 glsl_type::std430_size(bool row_major
) const
2092 unsigned N
= is_64bit() ? 8 : 4;
2094 /* OpenGL 4.30 spec, section 7.6.2.2 "Standard Uniform Block Layout":
2096 * "When using the std430 storage layout, shader storage blocks will be
2097 * laid out in buffer storage identically to uniform and shader storage
2098 * blocks using the std140 layout, except that the base alignment and
2099 * stride of arrays of scalars and vectors in rule 4 and of structures
2100 * in rule 9 are not rounded up a multiple of the base alignment of a vec4.
2102 if (this->is_scalar() || this->is_vector()) {
2103 assert(this->explicit_stride
== 0);
2104 return this->vector_elements
* N
;
2107 if (this->without_array()->is_matrix()) {
2108 const struct glsl_type
*element_type
;
2109 const struct glsl_type
*vec_type
;
2110 unsigned int array_len
;
2112 if (this->is_array()) {
2113 element_type
= this->without_array();
2114 array_len
= this->arrays_of_arrays_size();
2116 element_type
= this;
2121 vec_type
= get_instance(element_type
->base_type
,
2122 element_type
->matrix_columns
, 1);
2124 array_len
*= element_type
->vector_elements
;
2126 vec_type
= get_instance(element_type
->base_type
,
2127 element_type
->vector_elements
, 1);
2128 array_len
*= element_type
->matrix_columns
;
2130 const glsl_type
*array_type
= glsl_type::get_array_instance(vec_type
,
2133 return array_type
->std430_size(false);
2136 if (this->is_array()) {
2138 if (this->without_array()->is_struct())
2139 stride
= this->without_array()->std430_size(row_major
);
2141 stride
= this->without_array()->std430_base_alignment(row_major
);
2143 unsigned size
= this->arrays_of_arrays_size() * stride
;
2144 assert(this->explicit_stride
== 0 ||
2145 size
== this->length
* this->explicit_stride
);
2149 if (this->is_struct() || this->is_interface()) {
2151 unsigned max_align
= 0;
2153 for (unsigned i
= 0; i
< this->length
; i
++) {
2154 bool field_row_major
= row_major
;
2155 const enum glsl_matrix_layout matrix_layout
=
2156 glsl_matrix_layout(this->fields
.structure
[i
].matrix_layout
);
2157 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
2158 field_row_major
= true;
2159 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
2160 field_row_major
= false;
2163 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
2164 unsigned align
= field_type
->std430_base_alignment(field_row_major
);
2165 size
= glsl_align(size
, align
);
2166 size
+= field_type
->std430_size(field_row_major
);
2168 max_align
= MAX2(align
, max_align
);
2170 size
= glsl_align(size
, max_align
);
2174 assert(!"not reached");
2179 glsl_type::get_explicit_std430_type(bool row_major
) const
2181 if (this->is_vector() || this->is_scalar()) {
2183 } else if (this->is_matrix()) {
2184 const glsl_type
*vec_type
;
2186 vec_type
= get_instance(this->base_type
, this->matrix_columns
, 1);
2188 vec_type
= get_instance(this->base_type
, this->vector_elements
, 1);
2189 unsigned stride
= vec_type
->std430_array_stride(false);
2190 return get_instance(this->base_type
, this->vector_elements
,
2191 this->matrix_columns
, stride
, row_major
);
2192 } else if (this->is_array()) {
2193 const glsl_type
*elem_type
=
2194 this->fields
.array
->get_explicit_std430_type(row_major
);
2195 unsigned stride
= this->fields
.array
->std430_array_stride(row_major
);
2196 return get_array_instance(elem_type
, this->length
, stride
);
2197 } else if (this->is_struct() || this->is_interface()) {
2198 glsl_struct_field
*fields
= new glsl_struct_field
[this->length
];
2199 unsigned offset
= 0;
2200 for (unsigned i
= 0; i
< length
; i
++) {
2201 fields
[i
] = this->fields
.structure
[i
];
2203 bool field_row_major
= row_major
;
2204 if (fields
[i
].matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
2205 field_row_major
= false;
2206 } else if (fields
[i
].matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
2207 field_row_major
= true;
2210 fields
[i
].type
->get_explicit_std430_type(field_row_major
);
2212 unsigned fsize
= fields
[i
].type
->std430_size(field_row_major
);
2213 unsigned falign
= fields
[i
].type
->std430_base_alignment(field_row_major
);
2214 /* From the GLSL 460 spec section "Uniform and Shader Storage Block
2215 * Layout Qualifiers":
2217 * "The actual offset of a member is computed as follows: If
2218 * offset was declared, start with that offset, otherwise start
2219 * with the next available offset. If the resulting offset is not
2220 * a multiple of the actual alignment, increase it to the first
2221 * offset that is a multiple of the actual alignment. This results
2222 * in the actual offset the member will have."
2224 if (fields
[i
].offset
>= 0) {
2225 assert((unsigned)fields
[i
].offset
>= offset
);
2226 offset
= fields
[i
].offset
;
2228 offset
= glsl_align(offset
, falign
);
2229 fields
[i
].offset
= offset
;
2233 const glsl_type
*type
;
2234 if (this->is_struct())
2235 type
= get_struct_instance(fields
, this->length
, this->name
);
2237 type
= get_interface_instance(fields
, this->length
,
2238 (enum glsl_interface_packing
)this->interface_packing
,
2239 this->interface_row_major
,
2245 unreachable("Invalid type for SSBO");
2250 glsl_type::get_explicit_interface_type(bool supports_std430
) const
2252 enum glsl_interface_packing packing
=
2253 this->get_internal_ifc_packing(supports_std430
);
2254 if (packing
== GLSL_INTERFACE_PACKING_STD140
) {
2255 return this->get_explicit_std140_type(this->interface_row_major
);
2257 assert(packing
== GLSL_INTERFACE_PACKING_STD430
);
2258 return this->get_explicit_std430_type(this->interface_row_major
);
2263 glsl_type::count_attribute_slots(bool is_gl_vertex_input
) const
2265 /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
2267 * "A scalar input counts the same amount against this limit as a vec4,
2268 * so applications may want to consider packing groups of four
2269 * unrelated float inputs together into a vector to better utilize the
2270 * capabilities of the underlying hardware. A matrix input will use up
2271 * multiple locations. The number of locations used will equal the
2272 * number of columns in the matrix."
2274 * The spec does not explicitly say how arrays are counted. However, it
2275 * should be safe to assume the total number of slots consumed by an array
2276 * is the number of entries in the array multiplied by the number of slots
2277 * consumed by a single element of the array.
2279 * The spec says nothing about how structs are counted, because vertex
2280 * attributes are not allowed to be (or contain) structs. However, Mesa
2281 * allows varying structs, the number of varying slots taken up by a
2282 * varying struct is simply equal to the sum of the number of slots taken
2283 * up by each element.
2285 * Doubles are counted different depending on whether they are vertex
2286 * inputs or everything else. Vertex inputs from ARB_vertex_attrib_64bit
2287 * take one location no matter what size they are, otherwise dvec3/4
2288 * take two locations.
2290 switch (this->base_type
) {
2291 case GLSL_TYPE_UINT
:
2293 case GLSL_TYPE_UINT8
:
2294 case GLSL_TYPE_INT8
:
2295 case GLSL_TYPE_UINT16
:
2296 case GLSL_TYPE_INT16
:
2297 case GLSL_TYPE_FLOAT
:
2298 case GLSL_TYPE_FLOAT16
:
2299 case GLSL_TYPE_BOOL
:
2300 case GLSL_TYPE_SAMPLER
:
2301 case GLSL_TYPE_IMAGE
:
2302 return this->matrix_columns
;
2303 case GLSL_TYPE_DOUBLE
:
2304 case GLSL_TYPE_UINT64
:
2305 case GLSL_TYPE_INT64
:
2306 if (this->vector_elements
> 2 && !is_gl_vertex_input
)
2307 return this->matrix_columns
* 2;
2309 return this->matrix_columns
;
2310 case GLSL_TYPE_STRUCT
:
2311 case GLSL_TYPE_INTERFACE
: {
2314 for (unsigned i
= 0; i
< this->length
; i
++) {
2315 const glsl_type
*member_type
= this->fields
.structure
[i
].type
;
2316 size
+= member_type
->count_attribute_slots(is_gl_vertex_input
);
2322 case GLSL_TYPE_ARRAY
: {
2323 const glsl_type
*element
= this->fields
.array
;
2324 return this->length
* element
->count_attribute_slots(is_gl_vertex_input
);
2327 case GLSL_TYPE_SUBROUTINE
:
2330 case GLSL_TYPE_FUNCTION
:
2331 case GLSL_TYPE_ATOMIC_UINT
:
2332 case GLSL_TYPE_VOID
:
2333 case GLSL_TYPE_ERROR
:
2337 assert(!"Unexpected type in count_attribute_slots()");
2343 glsl_type::coordinate_components() const
2347 switch (sampler_dimensionality
) {
2348 case GLSL_SAMPLER_DIM_1D
:
2349 case GLSL_SAMPLER_DIM_BUF
:
2352 case GLSL_SAMPLER_DIM_2D
:
2353 case GLSL_SAMPLER_DIM_RECT
:
2354 case GLSL_SAMPLER_DIM_MS
:
2355 case GLSL_SAMPLER_DIM_EXTERNAL
:
2356 case GLSL_SAMPLER_DIM_SUBPASS
:
2359 case GLSL_SAMPLER_DIM_3D
:
2360 case GLSL_SAMPLER_DIM_CUBE
:
2364 assert(!"Should not get here.");
2369 /* Array textures need an additional component for the array index, except
2370 * for cubemap array images that behave like a 2D array of interleaved
2373 if (sampler_array
&&
2374 !(is_image() && sampler_dimensionality
== GLSL_SAMPLER_DIM_CUBE
))
2381 * Declarations of type flyweights (glsl_type::_foo_type) and
2382 * convenience pointers (glsl_type::foo_type).
2385 #define DECL_TYPE(NAME, ...) \
2386 const glsl_type glsl_type::_##NAME##_type = glsl_type(__VA_ARGS__, #NAME); \
2387 const glsl_type *const glsl_type::NAME##_type = &glsl_type::_##NAME##_type;
2389 #define STRUCT_TYPE(NAME)
2391 #include "compiler/builtin_type_macros.h"
2395 get_struct_type_field_and_pointer_sizes(size_t *s_field_size
,
2396 size_t *s_field_ptrs
)
2398 *s_field_size
= sizeof(glsl_struct_field
);
2400 sizeof(((glsl_struct_field
*)0)->type
) +
2401 sizeof(((glsl_struct_field
*)0)->name
);
2405 encode_type_to_blob(struct blob
*blob
, const glsl_type
*type
)
2410 blob_write_uint32(blob
, 0);
2414 switch (type
->base_type
) {
2415 case GLSL_TYPE_UINT
:
2417 case GLSL_TYPE_FLOAT
:
2418 case GLSL_TYPE_FLOAT16
:
2419 case GLSL_TYPE_DOUBLE
:
2420 case GLSL_TYPE_UINT8
:
2421 case GLSL_TYPE_INT8
:
2422 case GLSL_TYPE_UINT16
:
2423 case GLSL_TYPE_INT16
:
2424 case GLSL_TYPE_UINT64
:
2425 case GLSL_TYPE_INT64
:
2426 case GLSL_TYPE_BOOL
:
2427 encoding
= (type
->base_type
<< 24) |
2428 (type
->interface_row_major
<< 10) |
2429 (type
->vector_elements
<< 4) |
2430 (type
->matrix_columns
);
2431 blob_write_uint32(blob
, encoding
);
2432 blob_write_uint32(blob
, type
->explicit_stride
);
2434 case GLSL_TYPE_SAMPLER
:
2435 encoding
= (type
->base_type
) << 24 |
2436 (type
->sampler_dimensionality
<< 4) |
2437 (type
->sampler_shadow
<< 3) |
2438 (type
->sampler_array
<< 2) |
2439 (type
->sampled_type
);
2441 case GLSL_TYPE_SUBROUTINE
:
2442 encoding
= type
->base_type
<< 24;
2443 blob_write_uint32(blob
, encoding
);
2444 blob_write_string(blob
, type
->name
);
2446 case GLSL_TYPE_IMAGE
:
2447 encoding
= (type
->base_type
) << 24 |
2448 (type
->sampler_dimensionality
<< 3) |
2449 (type
->sampler_array
<< 2) |
2450 (type
->sampled_type
);
2452 case GLSL_TYPE_ATOMIC_UINT
:
2453 encoding
= (type
->base_type
<< 24);
2455 case GLSL_TYPE_ARRAY
:
2456 blob_write_uint32(blob
, (type
->base_type
) << 24);
2457 blob_write_uint32(blob
, type
->length
);
2458 blob_write_uint32(blob
, type
->explicit_stride
);
2459 encode_type_to_blob(blob
, type
->fields
.array
);
2461 case GLSL_TYPE_STRUCT
:
2462 case GLSL_TYPE_INTERFACE
:
2463 blob_write_uint32(blob
, (type
->base_type
) << 24);
2464 blob_write_string(blob
, type
->name
);
2465 blob_write_uint32(blob
, type
->length
);
2467 size_t s_field_size
, s_field_ptrs
;
2468 get_struct_type_field_and_pointer_sizes(&s_field_size
, &s_field_ptrs
);
2470 for (unsigned i
= 0; i
< type
->length
; i
++) {
2471 encode_type_to_blob(blob
, type
->fields
.structure
[i
].type
);
2472 blob_write_string(blob
, type
->fields
.structure
[i
].name
);
2474 /* Write the struct field skipping the pointers */
2475 blob_write_bytes(blob
,
2476 ((char *)&type
->fields
.structure
[i
]) + s_field_ptrs
,
2477 s_field_size
- s_field_ptrs
);
2480 if (type
->is_interface()) {
2481 blob_write_uint32(blob
, type
->interface_packing
);
2482 blob_write_uint32(blob
, type
->interface_row_major
);
2484 blob_write_uint32(blob
, type
->packed
);
2487 case GLSL_TYPE_VOID
:
2488 encoding
= (type
->base_type
<< 24);
2490 case GLSL_TYPE_ERROR
:
2492 assert(!"Cannot encode type!");
2497 blob_write_uint32(blob
, encoding
);
2501 decode_type_from_blob(struct blob_reader
*blob
)
2503 uint32_t u
= blob_read_uint32(blob
);
2509 glsl_base_type base_type
= (glsl_base_type
) (u
>> 24);
2511 switch (base_type
) {
2512 case GLSL_TYPE_UINT
:
2514 case GLSL_TYPE_FLOAT
:
2515 case GLSL_TYPE_FLOAT16
:
2516 case GLSL_TYPE_DOUBLE
:
2517 case GLSL_TYPE_UINT8
:
2518 case GLSL_TYPE_INT8
:
2519 case GLSL_TYPE_UINT16
:
2520 case GLSL_TYPE_INT16
:
2521 case GLSL_TYPE_UINT64
:
2522 case GLSL_TYPE_INT64
:
2523 case GLSL_TYPE_BOOL
: {
2524 unsigned explicit_stride
= blob_read_uint32(blob
);
2525 return glsl_type::get_instance(base_type
, (u
>> 4) & 0x0f, u
& 0x0f,
2526 explicit_stride
, (u
>> 10) & 0x1);
2528 case GLSL_TYPE_SAMPLER
:
2529 return glsl_type::get_sampler_instance((enum glsl_sampler_dim
) ((u
>> 4) & 0x0f),
2532 (glsl_base_type
) ((u
>> 0) & 0x03));
2533 case GLSL_TYPE_SUBROUTINE
:
2534 return glsl_type::get_subroutine_instance(blob_read_string(blob
));
2535 case GLSL_TYPE_IMAGE
:
2536 return glsl_type::get_image_instance((enum glsl_sampler_dim
) ((u
>> 3) & 0x0f),
2538 (glsl_base_type
) ((u
>> 0) & 0x03));
2539 case GLSL_TYPE_ATOMIC_UINT
:
2540 return glsl_type::atomic_uint_type
;
2541 case GLSL_TYPE_ARRAY
: {
2542 unsigned length
= blob_read_uint32(blob
);
2543 unsigned explicit_stride
= blob_read_uint32(blob
);
2544 return glsl_type::get_array_instance(decode_type_from_blob(blob
),
2545 length
, explicit_stride
);
2547 case GLSL_TYPE_STRUCT
:
2548 case GLSL_TYPE_INTERFACE
: {
2549 char *name
= blob_read_string(blob
);
2550 unsigned num_fields
= blob_read_uint32(blob
);
2552 size_t s_field_size
, s_field_ptrs
;
2553 get_struct_type_field_and_pointer_sizes(&s_field_size
, &s_field_ptrs
);
2555 glsl_struct_field
*fields
=
2556 (glsl_struct_field
*) malloc(s_field_size
* num_fields
);
2557 for (unsigned i
= 0; i
< num_fields
; i
++) {
2558 fields
[i
].type
= decode_type_from_blob(blob
);
2559 fields
[i
].name
= blob_read_string(blob
);
2561 blob_copy_bytes(blob
, ((uint8_t *) &fields
[i
]) + s_field_ptrs
,
2562 s_field_size
- s_field_ptrs
);
2566 if (base_type
== GLSL_TYPE_INTERFACE
) {
2567 enum glsl_interface_packing packing
=
2568 (glsl_interface_packing
) blob_read_uint32(blob
);
2569 bool row_major
= blob_read_uint32(blob
);
2570 t
= glsl_type::get_interface_instance(fields
, num_fields
, packing
,
2573 unsigned packed
= blob_read_uint32(blob
);
2574 t
= glsl_type::get_struct_instance(fields
, num_fields
, name
, packed
);
2580 case GLSL_TYPE_VOID
:
2581 return glsl_type::void_type
;
2582 case GLSL_TYPE_ERROR
:
2584 assert(!"Cannot decode type!");
2590 glsl_type::cl_alignment() const
2592 /* vectors unlike arrays are aligned to their size */
2593 if (this->is_scalar() || this->is_vector())
2594 return this->cl_size();
2595 else if (this->is_array())
2596 return this->without_array()->cl_alignment();
2597 else if (this->is_struct()) {
2598 /* Packed Structs are 0x1 aligned despite their size. */
2603 for (unsigned i
= 0; i
< this->length
; ++i
) {
2604 struct glsl_struct_field
&field
= this->fields
.structure
[i
];
2605 res
= MAX2(res
, field
.type
->cl_alignment());
2613 glsl_type::cl_size() const
2615 if (this->is_scalar()) {
2616 return glsl_base_type_get_bit_size(this->base_type
) / 8;
2617 } else if (this->is_vector()) {
2618 unsigned vec_elemns
= this->vector_elements
== 3 ? 4 : this->vector_elements
;
2619 return vec_elemns
* glsl_base_type_get_bit_size(this->base_type
) / 8;
2620 } else if (this->is_array()) {
2621 unsigned size
= this->without_array()->cl_size();
2622 return size
* this->length
;
2623 } else if (this->is_struct()) {
2625 for (unsigned i
= 0; i
< this->length
; ++i
) {
2626 struct glsl_struct_field
&field
= this->fields
.structure
[i
];
2627 /* if a struct is packed, members don't get aligned */
2629 size
= align(size
, field
.type
->cl_alignment());
2630 size
+= field
.type
->cl_size();