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
,
50 unsigned explicit_alignment
) :
52 base_type(base_type
), sampled_type(GLSL_TYPE_VOID
),
53 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
54 interface_packing(0), interface_row_major(row_major
), packed(0),
55 vector_elements(vector_elements
), matrix_columns(matrix_columns
),
56 length(0), explicit_stride(explicit_stride
),
57 explicit_alignment(explicit_alignment
)
59 /* Values of these types must fit in the two bits of
60 * glsl_type::sampled_type.
62 STATIC_ASSERT((unsigned(GLSL_TYPE_UINT
) & 3) == unsigned(GLSL_TYPE_UINT
));
63 STATIC_ASSERT((unsigned(GLSL_TYPE_INT
) & 3) == unsigned(GLSL_TYPE_INT
));
64 STATIC_ASSERT((unsigned(GLSL_TYPE_FLOAT
) & 3) == unsigned(GLSL_TYPE_FLOAT
));
66 ASSERT_BITFIELD_SIZE(glsl_type
, base_type
, GLSL_TYPE_ERROR
);
67 ASSERT_BITFIELD_SIZE(glsl_type
, sampled_type
, GLSL_TYPE_ERROR
);
68 ASSERT_BITFIELD_SIZE(glsl_type
, sampler_dimensionality
,
69 GLSL_SAMPLER_DIM_SUBPASS_MS
);
71 this->mem_ctx
= ralloc_context(NULL
);
72 assert(this->mem_ctx
!= NULL
);
75 this->name
= ralloc_strdup(this->mem_ctx
, name
);
77 /* Neither dimension is zero or both dimensions are zero.
79 assert((vector_elements
== 0) == (matrix_columns
== 0));
80 assert(util_is_power_of_two_or_zero(explicit_alignment
));
81 memset(& fields
, 0, sizeof(fields
));
84 glsl_type::glsl_type(GLenum gl_type
, glsl_base_type base_type
,
85 enum glsl_sampler_dim dim
, bool shadow
, bool array
,
86 glsl_base_type type
, const char *name
) :
88 base_type(base_type
), sampled_type(type
),
89 sampler_dimensionality(dim
), sampler_shadow(shadow
),
90 sampler_array(array
), interface_packing(0),
91 interface_row_major(0), packed(0),
92 length(0), explicit_stride(0), explicit_alignment(0)
94 this->mem_ctx
= ralloc_context(NULL
);
95 assert(this->mem_ctx
!= NULL
);
98 this->name
= ralloc_strdup(this->mem_ctx
, name
);
100 memset(& fields
, 0, sizeof(fields
));
102 matrix_columns
= vector_elements
= 1;
105 glsl_type::glsl_type(const glsl_struct_field
*fields
, unsigned num_fields
,
106 const char *name
, bool packed
,
107 unsigned explicit_alignment
) :
109 base_type(GLSL_TYPE_STRUCT
), sampled_type(GLSL_TYPE_VOID
),
110 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
111 interface_packing(0), interface_row_major(0), packed(packed
),
112 vector_elements(0), matrix_columns(0),
113 length(num_fields
), explicit_stride(0),
114 explicit_alignment(explicit_alignment
)
118 assert(util_is_power_of_two_or_zero(explicit_alignment
));
120 this->mem_ctx
= ralloc_context(NULL
);
121 assert(this->mem_ctx
!= NULL
);
123 assert(name
!= NULL
);
124 this->name
= ralloc_strdup(this->mem_ctx
, name
);
125 /* Zero-fill to prevent spurious Valgrind errors when serializing NIR
126 * due to uninitialized unused bits in bit fields. */
127 this->fields
.structure
= rzalloc_array(this->mem_ctx
,
128 glsl_struct_field
, length
);
130 for (i
= 0; i
< length
; i
++) {
131 this->fields
.structure
[i
] = fields
[i
];
132 this->fields
.structure
[i
].name
= ralloc_strdup(this->fields
.structure
,
137 glsl_type::glsl_type(const glsl_struct_field
*fields
, unsigned num_fields
,
138 enum glsl_interface_packing packing
,
139 bool row_major
, const char *name
) :
141 base_type(GLSL_TYPE_INTERFACE
), sampled_type(GLSL_TYPE_VOID
),
142 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
143 interface_packing((unsigned) packing
),
144 interface_row_major((unsigned) row_major
), packed(0),
145 vector_elements(0), matrix_columns(0),
146 length(num_fields
), explicit_stride(0), explicit_alignment(0)
150 this->mem_ctx
= ralloc_context(NULL
);
151 assert(this->mem_ctx
!= NULL
);
153 assert(name
!= NULL
);
154 this->name
= ralloc_strdup(this->mem_ctx
, name
);
155 this->fields
.structure
= rzalloc_array(this->mem_ctx
,
156 glsl_struct_field
, length
);
157 for (i
= 0; i
< length
; i
++) {
158 this->fields
.structure
[i
] = fields
[i
];
159 this->fields
.structure
[i
].name
= ralloc_strdup(this->fields
.structure
,
164 glsl_type::glsl_type(const glsl_type
*return_type
,
165 const glsl_function_param
*params
, unsigned num_params
) :
167 base_type(GLSL_TYPE_FUNCTION
), sampled_type(GLSL_TYPE_VOID
),
168 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
169 interface_packing(0), interface_row_major(0), packed(0),
170 vector_elements(0), matrix_columns(0),
171 length(num_params
), explicit_stride(0), explicit_alignment(0)
175 this->mem_ctx
= ralloc_context(NULL
);
176 assert(this->mem_ctx
!= NULL
);
178 this->fields
.parameters
= rzalloc_array(this->mem_ctx
,
179 glsl_function_param
, num_params
+ 1);
181 /* We store the return type as the first parameter */
182 this->fields
.parameters
[0].type
= return_type
;
183 this->fields
.parameters
[0].in
= false;
184 this->fields
.parameters
[0].out
= true;
186 /* We store the i'th parameter in slot i+1 */
187 for (i
= 0; i
< length
; i
++) {
188 this->fields
.parameters
[i
+ 1].type
= params
[i
].type
;
189 this->fields
.parameters
[i
+ 1].in
= params
[i
].in
;
190 this->fields
.parameters
[i
+ 1].out
= params
[i
].out
;
194 glsl_type::glsl_type(const char *subroutine_name
) :
196 base_type(GLSL_TYPE_SUBROUTINE
), sampled_type(GLSL_TYPE_VOID
),
197 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
198 interface_packing(0), interface_row_major(0), packed(0),
199 vector_elements(1), matrix_columns(1),
200 length(0), explicit_stride(0), explicit_alignment(0)
202 this->mem_ctx
= ralloc_context(NULL
);
203 assert(this->mem_ctx
!= NULL
);
205 assert(subroutine_name
!= NULL
);
206 this->name
= ralloc_strdup(this->mem_ctx
, subroutine_name
);
209 glsl_type::~glsl_type()
211 ralloc_free(this->mem_ctx
);
215 glsl_type::contains_sampler() const
217 if (this->is_array()) {
218 return this->fields
.array
->contains_sampler();
219 } else if (this->is_struct() || this->is_interface()) {
220 for (unsigned int i
= 0; i
< this->length
; i
++) {
221 if (this->fields
.structure
[i
].type
->contains_sampler())
226 return this->is_sampler();
231 glsl_type::contains_array() const
233 if (this->is_struct() || this->is_interface()) {
234 for (unsigned int i
= 0; i
< this->length
; i
++) {
235 if (this->fields
.structure
[i
].type
->contains_array())
240 return this->is_array();
245 glsl_type::contains_integer() const
247 if (this->is_array()) {
248 return this->fields
.array
->contains_integer();
249 } else if (this->is_struct() || this->is_interface()) {
250 for (unsigned int i
= 0; i
< this->length
; i
++) {
251 if (this->fields
.structure
[i
].type
->contains_integer())
256 return this->is_integer();
261 glsl_type::contains_double() const
263 if (this->is_array()) {
264 return this->fields
.array
->contains_double();
265 } else if (this->is_struct() || this->is_interface()) {
266 for (unsigned int i
= 0; i
< this->length
; i
++) {
267 if (this->fields
.structure
[i
].type
->contains_double())
272 return this->is_double();
277 glsl_type::contains_64bit() const
279 if (this->is_array()) {
280 return this->fields
.array
->contains_64bit();
281 } else if (this->is_struct() || this->is_interface()) {
282 for (unsigned int i
= 0; i
< this->length
; i
++) {
283 if (this->fields
.structure
[i
].type
->contains_64bit())
288 return this->is_64bit();
293 glsl_type::contains_opaque() const {
295 case GLSL_TYPE_SAMPLER
:
296 case GLSL_TYPE_IMAGE
:
297 case GLSL_TYPE_ATOMIC_UINT
:
299 case GLSL_TYPE_ARRAY
:
300 return fields
.array
->contains_opaque();
301 case GLSL_TYPE_STRUCT
:
302 case GLSL_TYPE_INTERFACE
:
303 for (unsigned int i
= 0; i
< length
; i
++) {
304 if (fields
.structure
[i
].type
->contains_opaque())
314 glsl_type::contains_subroutine() const
316 if (this->is_array()) {
317 return this->fields
.array
->contains_subroutine();
318 } else if (this->is_struct() || this->is_interface()) {
319 for (unsigned int i
= 0; i
< this->length
; i
++) {
320 if (this->fields
.structure
[i
].type
->contains_subroutine())
325 return this->is_subroutine();
330 glsl_type::sampler_index() const
332 const glsl_type
*const t
= (this->is_array()) ? this->fields
.array
: this;
334 assert(t
->is_sampler() || t
->is_image());
336 switch (t
->sampler_dimensionality
) {
337 case GLSL_SAMPLER_DIM_1D
:
338 return (t
->sampler_array
) ? TEXTURE_1D_ARRAY_INDEX
: TEXTURE_1D_INDEX
;
339 case GLSL_SAMPLER_DIM_2D
:
340 return (t
->sampler_array
) ? TEXTURE_2D_ARRAY_INDEX
: TEXTURE_2D_INDEX
;
341 case GLSL_SAMPLER_DIM_3D
:
342 return TEXTURE_3D_INDEX
;
343 case GLSL_SAMPLER_DIM_CUBE
:
344 return (t
->sampler_array
) ? TEXTURE_CUBE_ARRAY_INDEX
: TEXTURE_CUBE_INDEX
;
345 case GLSL_SAMPLER_DIM_RECT
:
346 return TEXTURE_RECT_INDEX
;
347 case GLSL_SAMPLER_DIM_BUF
:
348 return TEXTURE_BUFFER_INDEX
;
349 case GLSL_SAMPLER_DIM_EXTERNAL
:
350 return TEXTURE_EXTERNAL_INDEX
;
351 case GLSL_SAMPLER_DIM_MS
:
352 return (t
->sampler_array
) ? TEXTURE_2D_MULTISAMPLE_ARRAY_INDEX
: TEXTURE_2D_MULTISAMPLE_INDEX
;
354 assert(!"Should not get here.");
355 return TEXTURE_BUFFER_INDEX
;
360 glsl_type::contains_image() const
362 if (this->is_array()) {
363 return this->fields
.array
->contains_image();
364 } else if (this->is_struct() || this->is_interface()) {
365 for (unsigned int i
= 0; i
< this->length
; i
++) {
366 if (this->fields
.structure
[i
].type
->contains_image())
371 return this->is_image();
375 const glsl_type
*glsl_type::get_base_type() const
380 case GLSL_TYPE_UINT16
:
381 return uint16_t_type
;
382 case GLSL_TYPE_UINT8
:
386 case GLSL_TYPE_INT16
:
390 case GLSL_TYPE_FLOAT
:
392 case GLSL_TYPE_FLOAT16
:
393 return float16_t_type
;
394 case GLSL_TYPE_DOUBLE
:
398 case GLSL_TYPE_UINT64
:
399 return uint64_t_type
;
400 case GLSL_TYPE_INT64
:
408 const glsl_type
*glsl_type::get_scalar_type() const
410 const glsl_type
*type
= this;
413 while (type
->base_type
== GLSL_TYPE_ARRAY
)
414 type
= type
->fields
.array
;
416 const glsl_type
*scalar_type
= type
->get_base_type();
417 if (scalar_type
== error_type
)
424 const glsl_type
*glsl_type::get_bare_type() const
426 switch (this->base_type
) {
427 case GLSL_TYPE_UINT8
:
429 case GLSL_TYPE_UINT16
:
430 case GLSL_TYPE_INT16
:
431 case GLSL_TYPE_FLOAT16
:
434 case GLSL_TYPE_FLOAT
:
436 case GLSL_TYPE_DOUBLE
:
437 case GLSL_TYPE_UINT64
:
438 case GLSL_TYPE_INT64
:
439 return get_instance(this->base_type
, this->vector_elements
,
440 this->matrix_columns
);
442 case GLSL_TYPE_STRUCT
:
443 case GLSL_TYPE_INTERFACE
: {
444 glsl_struct_field
*bare_fields
= new glsl_struct_field
[this->length
];
445 for (unsigned i
= 0; i
< this->length
; i
++) {
446 bare_fields
[i
].type
= this->fields
.structure
[i
].type
->get_bare_type();
447 bare_fields
[i
].name
= this->fields
.structure
[i
].name
;
449 const glsl_type
*bare_type
=
450 get_struct_instance(bare_fields
, this->length
, this->name
);
451 delete[] bare_fields
;
455 case GLSL_TYPE_ARRAY
:
456 return get_array_instance(this->fields
.array
->get_bare_type(),
459 case GLSL_TYPE_SAMPLER
:
460 case GLSL_TYPE_IMAGE
:
461 case GLSL_TYPE_ATOMIC_UINT
:
463 case GLSL_TYPE_SUBROUTINE
:
464 case GLSL_TYPE_FUNCTION
:
465 case GLSL_TYPE_ERROR
:
469 unreachable("Invalid base type");
472 const glsl_type
*glsl_type::get_float16_type() const
474 assert(this->base_type
== GLSL_TYPE_FLOAT
);
476 return get_instance(GLSL_TYPE_FLOAT16
,
477 this->vector_elements
,
478 this->matrix_columns
,
479 this->explicit_stride
,
480 this->interface_row_major
);
483 const glsl_type
*glsl_type::get_int16_type() const
485 assert(this->base_type
== GLSL_TYPE_INT
);
487 return get_instance(GLSL_TYPE_INT16
,
488 this->vector_elements
,
489 this->matrix_columns
,
490 this->explicit_stride
,
491 this->interface_row_major
);
494 const glsl_type
*glsl_type::get_uint16_type() const
496 assert(this->base_type
== GLSL_TYPE_UINT
);
498 return get_instance(GLSL_TYPE_UINT16
,
499 this->vector_elements
,
500 this->matrix_columns
,
501 this->explicit_stride
,
502 this->interface_row_major
);
506 hash_free_type_function(struct hash_entry
*entry
)
508 glsl_type
*type
= (glsl_type
*) entry
->data
;
510 if (type
->is_array())
511 free((void*)entry
->key
);
517 glsl_type_singleton_init_or_ref()
519 mtx_lock(&glsl_type::hash_mutex
);
521 mtx_unlock(&glsl_type::hash_mutex
);
525 glsl_type_singleton_decref()
527 mtx_lock(&glsl_type::hash_mutex
);
528 assert(glsl_type_users
> 0);
530 /* Do not release glsl_types if they are still used. */
531 if (--glsl_type_users
) {
532 mtx_unlock(&glsl_type::hash_mutex
);
536 if (glsl_type::explicit_matrix_types
!= NULL
) {
537 _mesa_hash_table_destroy(glsl_type::explicit_matrix_types
,
538 hash_free_type_function
);
539 glsl_type::explicit_matrix_types
= NULL
;
542 if (glsl_type::array_types
!= NULL
) {
543 _mesa_hash_table_destroy(glsl_type::array_types
, hash_free_type_function
);
544 glsl_type::array_types
= NULL
;
547 if (glsl_type::struct_types
!= NULL
) {
548 _mesa_hash_table_destroy(glsl_type::struct_types
, hash_free_type_function
);
549 glsl_type::struct_types
= NULL
;
552 if (glsl_type::interface_types
!= NULL
) {
553 _mesa_hash_table_destroy(glsl_type::interface_types
, hash_free_type_function
);
554 glsl_type::interface_types
= NULL
;
557 if (glsl_type::function_types
!= NULL
) {
558 _mesa_hash_table_destroy(glsl_type::function_types
, hash_free_type_function
);
559 glsl_type::function_types
= NULL
;
562 if (glsl_type::subroutine_types
!= NULL
) {
563 _mesa_hash_table_destroy(glsl_type::subroutine_types
, hash_free_type_function
);
564 glsl_type::subroutine_types
= NULL
;
567 mtx_unlock(&glsl_type::hash_mutex
);
571 glsl_type::glsl_type(const glsl_type
*array
, unsigned length
,
572 unsigned explicit_stride
) :
573 base_type(GLSL_TYPE_ARRAY
), sampled_type(GLSL_TYPE_VOID
),
574 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
575 interface_packing(0), interface_row_major(0), packed(0),
576 vector_elements(0), matrix_columns(0),
577 length(length
), name(NULL
), explicit_stride(explicit_stride
),
578 explicit_alignment(array
->explicit_alignment
)
580 this->fields
.array
= array
;
581 /* Inherit the gl type of the base. The GL type is used for
582 * uniform/statevar handling in Mesa and the arrayness of the type
583 * is represented by the size rather than the type.
585 this->gl_type
= array
->gl_type
;
587 /* Allow a maximum of 10 characters for the array size. This is enough
588 * for 32-bits of ~0. The extra 3 are for the '[', ']', and terminating
591 const unsigned name_length
= strlen(array
->name
) + 10 + 3;
593 this->mem_ctx
= ralloc_context(NULL
);
594 assert(this->mem_ctx
!= NULL
);
596 char *const n
= (char *) ralloc_size(this->mem_ctx
, name_length
);
599 snprintf(n
, name_length
, "%s[]", array
->name
);
601 /* insert outermost dimensions in the correct spot
602 * otherwise the dimension order will be backwards
604 const char *pos
= strchr(array
->name
, '[');
606 int idx
= pos
- array
->name
;
607 snprintf(n
, idx
+1, "%s", array
->name
);
608 snprintf(n
+ idx
, name_length
- idx
, "[%u]%s",
609 length
, array
->name
+ idx
);
611 snprintf(n
, name_length
, "%s[%u]", array
->name
, length
);
619 glsl_type::vec(unsigned components
, const glsl_type
*const ts
[])
621 unsigned n
= components
;
625 else if (components
== 16)
634 #define VECN(components, sname, vname) \
636 glsl_type:: vname (unsigned components) \
638 static const glsl_type *const ts[] = { \
639 sname ## _type, vname ## 2_type, \
640 vname ## 3_type, vname ## 4_type, \
641 vname ## 8_type, vname ## 16_type, \
643 return glsl_type::vec(components, ts); \
646 VECN(components
, float, vec
)
647 VECN(components
, float16_t
, f16vec
)
648 VECN(components
, double, dvec
)
649 VECN(components
, int, ivec
)
650 VECN(components
, uint
, uvec
)
651 VECN(components
, bool, bvec
)
652 VECN(components
, int64_t, i64vec
)
653 VECN(components
, uint64_t, u64vec
)
654 VECN(components
, int16_t, i16vec
)
655 VECN(components
, uint16_t, u16vec
)
656 VECN(components
, int8_t, i8vec
)
657 VECN(components
, uint8_t, u8vec
)
660 glsl_type::get_instance(unsigned base_type
, unsigned rows
, unsigned columns
,
661 unsigned explicit_stride
, bool row_major
,
662 unsigned explicit_alignment
)
664 if (base_type
== GLSL_TYPE_VOID
) {
665 assert(explicit_stride
== 0 && explicit_alignment
== 0 && !row_major
);
669 /* Matrix and vector types with explicit strides or alignment have to be
670 * looked up in a table so they're handled separately.
672 if (explicit_stride
> 0 || explicit_alignment
> 0) {
673 if (explicit_alignment
> 0) {
674 assert(util_is_power_of_two_nonzero(explicit_alignment
));
675 assert(explicit_stride
% explicit_alignment
== 0);
678 const glsl_type
*bare_type
= get_instance(base_type
, rows
, columns
);
680 assert(columns
> 1 || (rows
> 1 && !row_major
));
683 snprintf(name
, sizeof(name
), "%sx%ua%uB%s", bare_type
->name
,
684 explicit_stride
, explicit_alignment
, row_major
? "RM" : "");
686 mtx_lock(&glsl_type::hash_mutex
);
687 assert(glsl_type_users
> 0);
689 if (explicit_matrix_types
== NULL
) {
690 explicit_matrix_types
=
691 _mesa_hash_table_create(NULL
, _mesa_hash_string
,
692 _mesa_key_string_equal
);
695 const struct hash_entry
*entry
=
696 _mesa_hash_table_search(explicit_matrix_types
, name
);
698 const glsl_type
*t
= new glsl_type(bare_type
->gl_type
,
699 (glsl_base_type
)base_type
,
701 explicit_stride
, row_major
,
704 entry
= _mesa_hash_table_insert(explicit_matrix_types
,
708 assert(((glsl_type
*) entry
->data
)->base_type
== base_type
);
709 assert(((glsl_type
*) entry
->data
)->vector_elements
== rows
);
710 assert(((glsl_type
*) entry
->data
)->matrix_columns
== columns
);
711 assert(((glsl_type
*) entry
->data
)->explicit_stride
== explicit_stride
);
712 assert(((glsl_type
*) entry
->data
)->explicit_alignment
== explicit_alignment
);
714 const glsl_type
*t
= (const glsl_type
*) entry
->data
;
716 mtx_unlock(&glsl_type::hash_mutex
);
723 /* Treat GLSL vectors as Nx1 matrices.
731 case GLSL_TYPE_FLOAT
:
733 case GLSL_TYPE_FLOAT16
:
735 case GLSL_TYPE_DOUBLE
:
739 case GLSL_TYPE_UINT64
:
741 case GLSL_TYPE_INT64
:
743 case GLSL_TYPE_UINT16
:
745 case GLSL_TYPE_INT16
:
747 case GLSL_TYPE_UINT8
:
755 if ((base_type
!= GLSL_TYPE_FLOAT
&&
756 base_type
!= GLSL_TYPE_DOUBLE
&&
757 base_type
!= GLSL_TYPE_FLOAT16
) || (rows
== 1))
760 /* GLSL matrix types are named mat{COLUMNS}x{ROWS}. Only the following
761 * combinations are valid:
769 #define IDX(c,r) (((c-1)*3) + (r-1))
772 case GLSL_TYPE_DOUBLE
: {
773 switch (IDX(columns
, rows
)) {
774 case IDX(2,2): return dmat2_type
;
775 case IDX(2,3): return dmat2x3_type
;
776 case IDX(2,4): return dmat2x4_type
;
777 case IDX(3,2): return dmat3x2_type
;
778 case IDX(3,3): return dmat3_type
;
779 case IDX(3,4): return dmat3x4_type
;
780 case IDX(4,2): return dmat4x2_type
;
781 case IDX(4,3): return dmat4x3_type
;
782 case IDX(4,4): return dmat4_type
;
783 default: return error_type
;
786 case GLSL_TYPE_FLOAT
: {
787 switch (IDX(columns
, rows
)) {
788 case IDX(2,2): return mat2_type
;
789 case IDX(2,3): return mat2x3_type
;
790 case IDX(2,4): return mat2x4_type
;
791 case IDX(3,2): return mat3x2_type
;
792 case IDX(3,3): return mat3_type
;
793 case IDX(3,4): return mat3x4_type
;
794 case IDX(4,2): return mat4x2_type
;
795 case IDX(4,3): return mat4x3_type
;
796 case IDX(4,4): return mat4_type
;
797 default: return error_type
;
800 case GLSL_TYPE_FLOAT16
: {
801 switch (IDX(columns
, rows
)) {
802 case IDX(2,2): return f16mat2_type
;
803 case IDX(2,3): return f16mat2x3_type
;
804 case IDX(2,4): return f16mat2x4_type
;
805 case IDX(3,2): return f16mat3x2_type
;
806 case IDX(3,3): return f16mat3_type
;
807 case IDX(3,4): return f16mat3x4_type
;
808 case IDX(4,2): return f16mat4x2_type
;
809 case IDX(4,3): return f16mat4x3_type
;
810 case IDX(4,4): return f16mat4_type
;
811 default: return error_type
;
814 default: return error_type
;
818 assert(!"Should not get here.");
823 glsl_type::get_sampler_instance(enum glsl_sampler_dim dim
,
829 case GLSL_TYPE_FLOAT
:
831 case GLSL_SAMPLER_DIM_1D
:
833 return (array
? sampler1DArrayShadow_type
: sampler1DShadow_type
);
835 return (array
? sampler1DArray_type
: sampler1D_type
);
836 case GLSL_SAMPLER_DIM_2D
:
838 return (array
? sampler2DArrayShadow_type
: sampler2DShadow_type
);
840 return (array
? sampler2DArray_type
: sampler2D_type
);
841 case GLSL_SAMPLER_DIM_3D
:
845 return sampler3D_type
;
846 case GLSL_SAMPLER_DIM_CUBE
:
848 return (array
? samplerCubeArrayShadow_type
: samplerCubeShadow_type
);
850 return (array
? samplerCubeArray_type
: samplerCube_type
);
851 case GLSL_SAMPLER_DIM_RECT
:
855 return sampler2DRectShadow_type
;
857 return sampler2DRect_type
;
858 case GLSL_SAMPLER_DIM_BUF
:
862 return samplerBuffer_type
;
863 case GLSL_SAMPLER_DIM_MS
:
866 return (array
? sampler2DMSArray_type
: sampler2DMS_type
);
867 case GLSL_SAMPLER_DIM_EXTERNAL
:
871 return samplerExternalOES_type
;
872 case GLSL_SAMPLER_DIM_SUBPASS
:
873 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
880 case GLSL_SAMPLER_DIM_1D
:
881 return (array
? isampler1DArray_type
: isampler1D_type
);
882 case GLSL_SAMPLER_DIM_2D
:
883 return (array
? isampler2DArray_type
: isampler2D_type
);
884 case GLSL_SAMPLER_DIM_3D
:
887 return isampler3D_type
;
888 case GLSL_SAMPLER_DIM_CUBE
:
889 return (array
? isamplerCubeArray_type
: isamplerCube_type
);
890 case GLSL_SAMPLER_DIM_RECT
:
893 return isampler2DRect_type
;
894 case GLSL_SAMPLER_DIM_BUF
:
897 return isamplerBuffer_type
;
898 case GLSL_SAMPLER_DIM_MS
:
899 return (array
? isampler2DMSArray_type
: isampler2DMS_type
);
900 case GLSL_SAMPLER_DIM_EXTERNAL
:
902 case GLSL_SAMPLER_DIM_SUBPASS
:
903 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
910 case GLSL_SAMPLER_DIM_1D
:
911 return (array
? usampler1DArray_type
: usampler1D_type
);
912 case GLSL_SAMPLER_DIM_2D
:
913 return (array
? usampler2DArray_type
: usampler2D_type
);
914 case GLSL_SAMPLER_DIM_3D
:
917 return usampler3D_type
;
918 case GLSL_SAMPLER_DIM_CUBE
:
919 return (array
? usamplerCubeArray_type
: usamplerCube_type
);
920 case GLSL_SAMPLER_DIM_RECT
:
923 return usampler2DRect_type
;
924 case GLSL_SAMPLER_DIM_BUF
:
927 return usamplerBuffer_type
;
928 case GLSL_SAMPLER_DIM_MS
:
929 return (array
? usampler2DMSArray_type
: usampler2DMS_type
);
930 case GLSL_SAMPLER_DIM_EXTERNAL
:
932 case GLSL_SAMPLER_DIM_SUBPASS
:
933 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
937 return shadow
? samplerShadow_type
: sampler_type
;
942 unreachable("switch statement above should be complete");
946 glsl_type::get_image_instance(enum glsl_sampler_dim dim
,
947 bool array
, glsl_base_type type
)
950 case GLSL_TYPE_FLOAT
:
952 case GLSL_SAMPLER_DIM_1D
:
953 return (array
? image1DArray_type
: image1D_type
);
954 case GLSL_SAMPLER_DIM_2D
:
955 return (array
? image2DArray_type
: image2D_type
);
956 case GLSL_SAMPLER_DIM_3D
:
958 case GLSL_SAMPLER_DIM_CUBE
:
959 return (array
? imageCubeArray_type
: imageCube_type
);
960 case GLSL_SAMPLER_DIM_RECT
:
964 return image2DRect_type
;
965 case GLSL_SAMPLER_DIM_BUF
:
969 return imageBuffer_type
;
970 case GLSL_SAMPLER_DIM_MS
:
971 return (array
? image2DMSArray_type
: image2DMS_type
);
972 case GLSL_SAMPLER_DIM_SUBPASS
:
973 return subpassInput_type
;
974 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
975 return subpassInputMS_type
;
976 case GLSL_SAMPLER_DIM_EXTERNAL
:
981 case GLSL_SAMPLER_DIM_1D
:
982 return (array
? iimage1DArray_type
: iimage1D_type
);
983 case GLSL_SAMPLER_DIM_2D
:
984 return (array
? iimage2DArray_type
: iimage2D_type
);
985 case GLSL_SAMPLER_DIM_3D
:
988 return iimage3D_type
;
989 case GLSL_SAMPLER_DIM_CUBE
:
990 return (array
? iimageCubeArray_type
: iimageCube_type
);
991 case GLSL_SAMPLER_DIM_RECT
:
994 return iimage2DRect_type
;
995 case GLSL_SAMPLER_DIM_BUF
:
998 return iimageBuffer_type
;
999 case GLSL_SAMPLER_DIM_MS
:
1000 return (array
? iimage2DMSArray_type
: iimage2DMS_type
);
1001 case GLSL_SAMPLER_DIM_SUBPASS
:
1002 return isubpassInput_type
;
1003 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
1004 return isubpassInputMS_type
;
1005 case GLSL_SAMPLER_DIM_EXTERNAL
:
1008 case GLSL_TYPE_UINT
:
1010 case GLSL_SAMPLER_DIM_1D
:
1011 return (array
? uimage1DArray_type
: uimage1D_type
);
1012 case GLSL_SAMPLER_DIM_2D
:
1013 return (array
? uimage2DArray_type
: uimage2D_type
);
1014 case GLSL_SAMPLER_DIM_3D
:
1017 return uimage3D_type
;
1018 case GLSL_SAMPLER_DIM_CUBE
:
1019 return (array
? uimageCubeArray_type
: uimageCube_type
);
1020 case GLSL_SAMPLER_DIM_RECT
:
1023 return uimage2DRect_type
;
1024 case GLSL_SAMPLER_DIM_BUF
:
1027 return uimageBuffer_type
;
1028 case GLSL_SAMPLER_DIM_MS
:
1029 return (array
? uimage2DMSArray_type
: uimage2DMS_type
);
1030 case GLSL_SAMPLER_DIM_SUBPASS
:
1031 return usubpassInput_type
;
1032 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
1033 return usubpassInputMS_type
;
1034 case GLSL_SAMPLER_DIM_EXTERNAL
:
1037 case GLSL_TYPE_VOID
:
1039 case GLSL_SAMPLER_DIM_1D
:
1040 return (array
? vimage1DArray_type
: vimage1D_type
);
1041 case GLSL_SAMPLER_DIM_2D
:
1042 return (array
? vimage2DArray_type
: vimage2D_type
);
1043 case GLSL_SAMPLER_DIM_3D
:
1044 return (array
? error_type
: vimage3D_type
);
1045 case GLSL_SAMPLER_DIM_BUF
:
1046 return (array
? error_type
: vbuffer_type
);
1054 unreachable("switch statement above should be complete");
1058 glsl_type::get_array_instance(const glsl_type
*base
,
1059 unsigned array_size
,
1060 unsigned explicit_stride
)
1062 /* Generate a name using the base type pointer in the key. This is
1063 * done because the name of the base type may not be unique across
1064 * shaders. For example, two shaders may have different record types
1068 snprintf(key
, sizeof(key
), "%p[%u]x%uB", (void *) base
, array_size
,
1071 mtx_lock(&glsl_type::hash_mutex
);
1072 assert(glsl_type_users
> 0);
1074 if (array_types
== NULL
) {
1075 array_types
= _mesa_hash_table_create(NULL
, _mesa_hash_string
,
1076 _mesa_key_string_equal
);
1079 const struct hash_entry
*entry
= _mesa_hash_table_search(array_types
, key
);
1080 if (entry
== NULL
) {
1081 const glsl_type
*t
= new glsl_type(base
, array_size
, explicit_stride
);
1083 entry
= _mesa_hash_table_insert(array_types
,
1088 assert(((glsl_type
*) entry
->data
)->base_type
== GLSL_TYPE_ARRAY
);
1089 assert(((glsl_type
*) entry
->data
)->length
== array_size
);
1090 assert(((glsl_type
*) entry
->data
)->fields
.array
== base
);
1092 glsl_type
*t
= (glsl_type
*) entry
->data
;
1094 mtx_unlock(&glsl_type::hash_mutex
);
1100 glsl_type::compare_no_precision(const glsl_type
*b
) const
1105 if (this->is_array()) {
1106 if (!b
->is_array() || this->length
!= b
->length
)
1109 const glsl_type
*b_no_array
= b
->fields
.array
;
1111 return this->fields
.array
->compare_no_precision(b_no_array
);
1114 if (this->is_struct()) {
1115 if (!b
->is_struct())
1117 } else if (this->is_interface()) {
1118 if (!b
->is_interface())
1124 return record_compare(b
,
1125 true, /* match_name */
1126 true, /* match_locations */
1127 false /* match_precision */);
1131 glsl_type::record_compare(const glsl_type
*b
, bool match_name
,
1132 bool match_locations
, bool match_precision
) const
1134 if (this->length
!= b
->length
)
1137 if (this->interface_packing
!= b
->interface_packing
)
1140 if (this->interface_row_major
!= b
->interface_row_major
)
1143 if (this->explicit_alignment
!= b
->explicit_alignment
)
1146 /* From the GLSL 4.20 specification (Sec 4.2):
1148 * "Structures must have the same name, sequence of type names, and
1149 * type definitions, and field names to be considered the same type."
1151 * GLSL ES behaves the same (Ver 1.00 Sec 4.2.4, Ver 3.00 Sec 4.2.5).
1153 * Section 7.4.1 (Shader Interface Matching) of the OpenGL 4.30 spec says:
1155 * "Variables or block members declared as structures are considered
1156 * to match in type if and only if structure members match in name,
1157 * type, qualification, and declaration order."
1160 if (strcmp(this->name
, b
->name
) != 0)
1163 for (unsigned i
= 0; i
< this->length
; i
++) {
1164 if (match_precision
) {
1165 if (this->fields
.structure
[i
].type
!= b
->fields
.structure
[i
].type
)
1168 const glsl_type
*ta
= this->fields
.structure
[i
].type
;
1169 const glsl_type
*tb
= b
->fields
.structure
[i
].type
;
1170 if (!ta
->compare_no_precision(tb
))
1173 if (strcmp(this->fields
.structure
[i
].name
,
1174 b
->fields
.structure
[i
].name
) != 0)
1176 if (this->fields
.structure
[i
].matrix_layout
1177 != b
->fields
.structure
[i
].matrix_layout
)
1179 if (match_locations
&& this->fields
.structure
[i
].location
1180 != b
->fields
.structure
[i
].location
)
1182 if (this->fields
.structure
[i
].offset
1183 != b
->fields
.structure
[i
].offset
)
1185 if (this->fields
.structure
[i
].interpolation
1186 != b
->fields
.structure
[i
].interpolation
)
1188 if (this->fields
.structure
[i
].centroid
1189 != b
->fields
.structure
[i
].centroid
)
1191 if (this->fields
.structure
[i
].sample
1192 != b
->fields
.structure
[i
].sample
)
1194 if (this->fields
.structure
[i
].patch
1195 != b
->fields
.structure
[i
].patch
)
1197 if (this->fields
.structure
[i
].memory_read_only
1198 != b
->fields
.structure
[i
].memory_read_only
)
1200 if (this->fields
.structure
[i
].memory_write_only
1201 != b
->fields
.structure
[i
].memory_write_only
)
1203 if (this->fields
.structure
[i
].memory_coherent
1204 != b
->fields
.structure
[i
].memory_coherent
)
1206 if (this->fields
.structure
[i
].memory_volatile
1207 != b
->fields
.structure
[i
].memory_volatile
)
1209 if (this->fields
.structure
[i
].memory_restrict
1210 != b
->fields
.structure
[i
].memory_restrict
)
1212 if (this->fields
.structure
[i
].image_format
1213 != b
->fields
.structure
[i
].image_format
)
1215 if (match_precision
&&
1216 this->fields
.structure
[i
].precision
1217 != b
->fields
.structure
[i
].precision
)
1219 if (this->fields
.structure
[i
].explicit_xfb_buffer
1220 != b
->fields
.structure
[i
].explicit_xfb_buffer
)
1222 if (this->fields
.structure
[i
].xfb_buffer
1223 != b
->fields
.structure
[i
].xfb_buffer
)
1225 if (this->fields
.structure
[i
].xfb_stride
1226 != b
->fields
.structure
[i
].xfb_stride
)
1235 glsl_type::record_key_compare(const void *a
, const void *b
)
1237 const glsl_type
*const key1
= (glsl_type
*) a
;
1238 const glsl_type
*const key2
= (glsl_type
*) b
;
1240 return strcmp(key1
->name
, key2
->name
) == 0 &&
1241 key1
->record_compare(key2
, true);
1246 * Generate an integer hash value for a glsl_type structure type.
1249 glsl_type::record_key_hash(const void *a
)
1251 const glsl_type
*const key
= (glsl_type
*) a
;
1252 uintptr_t hash
= key
->length
;
1255 for (unsigned i
= 0; i
< key
->length
; i
++) {
1256 /* casting pointer to uintptr_t */
1257 hash
= (hash
* 13 ) + (uintptr_t) key
->fields
.structure
[i
].type
;
1260 if (sizeof(hash
) == 8)
1261 retval
= (hash
& 0xffffffff) ^ ((uint64_t) hash
>> 32);
1270 glsl_type::get_struct_instance(const glsl_struct_field
*fields
,
1271 unsigned num_fields
,
1273 bool packed
, unsigned explicit_alignment
)
1275 const glsl_type
key(fields
, num_fields
, name
, packed
, explicit_alignment
);
1277 mtx_lock(&glsl_type::hash_mutex
);
1278 assert(glsl_type_users
> 0);
1280 if (struct_types
== NULL
) {
1281 struct_types
= _mesa_hash_table_create(NULL
, record_key_hash
,
1282 record_key_compare
);
1285 const struct hash_entry
*entry
= _mesa_hash_table_search(struct_types
,
1287 if (entry
== NULL
) {
1288 const glsl_type
*t
= new glsl_type(fields
, num_fields
, name
, packed
,
1289 explicit_alignment
);
1291 entry
= _mesa_hash_table_insert(struct_types
, t
, (void *) t
);
1294 assert(((glsl_type
*) entry
->data
)->base_type
== GLSL_TYPE_STRUCT
);
1295 assert(((glsl_type
*) entry
->data
)->length
== num_fields
);
1296 assert(strcmp(((glsl_type
*) entry
->data
)->name
, name
) == 0);
1297 assert(((glsl_type
*) entry
->data
)->packed
== packed
);
1298 assert(((glsl_type
*) entry
->data
)->explicit_alignment
== explicit_alignment
);
1300 glsl_type
*t
= (glsl_type
*) entry
->data
;
1302 mtx_unlock(&glsl_type::hash_mutex
);
1309 glsl_type::get_interface_instance(const glsl_struct_field
*fields
,
1310 unsigned num_fields
,
1311 enum glsl_interface_packing packing
,
1313 const char *block_name
)
1315 const glsl_type
key(fields
, num_fields
, packing
, row_major
, block_name
);
1317 mtx_lock(&glsl_type::hash_mutex
);
1318 assert(glsl_type_users
> 0);
1320 if (interface_types
== NULL
) {
1321 interface_types
= _mesa_hash_table_create(NULL
, record_key_hash
,
1322 record_key_compare
);
1325 const struct hash_entry
*entry
= _mesa_hash_table_search(interface_types
,
1327 if (entry
== NULL
) {
1328 const glsl_type
*t
= new glsl_type(fields
, num_fields
,
1329 packing
, row_major
, block_name
);
1331 entry
= _mesa_hash_table_insert(interface_types
, t
, (void *) t
);
1334 assert(((glsl_type
*) entry
->data
)->base_type
== GLSL_TYPE_INTERFACE
);
1335 assert(((glsl_type
*) entry
->data
)->length
== num_fields
);
1336 assert(strcmp(((glsl_type
*) entry
->data
)->name
, block_name
) == 0);
1338 glsl_type
*t
= (glsl_type
*) entry
->data
;
1340 mtx_unlock(&glsl_type::hash_mutex
);
1346 glsl_type::get_subroutine_instance(const char *subroutine_name
)
1348 const glsl_type
key(subroutine_name
);
1350 mtx_lock(&glsl_type::hash_mutex
);
1351 assert(glsl_type_users
> 0);
1353 if (subroutine_types
== NULL
) {
1354 subroutine_types
= _mesa_hash_table_create(NULL
, record_key_hash
,
1355 record_key_compare
);
1358 const struct hash_entry
*entry
= _mesa_hash_table_search(subroutine_types
,
1360 if (entry
== NULL
) {
1361 const glsl_type
*t
= new glsl_type(subroutine_name
);
1363 entry
= _mesa_hash_table_insert(subroutine_types
, t
, (void *) t
);
1366 assert(((glsl_type
*) entry
->data
)->base_type
== GLSL_TYPE_SUBROUTINE
);
1367 assert(strcmp(((glsl_type
*) entry
->data
)->name
, subroutine_name
) == 0);
1369 glsl_type
*t
= (glsl_type
*) entry
->data
;
1371 mtx_unlock(&glsl_type::hash_mutex
);
1378 function_key_compare(const void *a
, const void *b
)
1380 const glsl_type
*const key1
= (glsl_type
*) a
;
1381 const glsl_type
*const key2
= (glsl_type
*) b
;
1383 if (key1
->length
!= key2
->length
)
1386 return memcmp(key1
->fields
.parameters
, key2
->fields
.parameters
,
1387 (key1
->length
+ 1) * sizeof(*key1
->fields
.parameters
)) == 0;
1392 function_key_hash(const void *a
)
1394 const glsl_type
*const key
= (glsl_type
*) a
;
1395 return _mesa_hash_data(key
->fields
.parameters
,
1396 (key
->length
+ 1) * sizeof(*key
->fields
.parameters
));
1400 glsl_type::get_function_instance(const glsl_type
*return_type
,
1401 const glsl_function_param
*params
,
1402 unsigned num_params
)
1404 const glsl_type
key(return_type
, params
, num_params
);
1406 mtx_lock(&glsl_type::hash_mutex
);
1407 assert(glsl_type_users
> 0);
1409 if (function_types
== NULL
) {
1410 function_types
= _mesa_hash_table_create(NULL
, function_key_hash
,
1411 function_key_compare
);
1414 struct hash_entry
*entry
= _mesa_hash_table_search(function_types
, &key
);
1415 if (entry
== NULL
) {
1416 const glsl_type
*t
= new glsl_type(return_type
, params
, num_params
);
1418 entry
= _mesa_hash_table_insert(function_types
, t
, (void *) t
);
1421 const glsl_type
*t
= (const glsl_type
*)entry
->data
;
1423 assert(t
->base_type
== GLSL_TYPE_FUNCTION
);
1424 assert(t
->length
== num_params
);
1426 mtx_unlock(&glsl_type::hash_mutex
);
1433 glsl_type::get_mul_type(const glsl_type
*type_a
, const glsl_type
*type_b
)
1435 if (type_a
->is_matrix() && type_b
->is_matrix()) {
1436 /* Matrix multiply. The columns of A must match the rows of B. Given
1437 * the other previously tested constraints, this means the vector type
1438 * of a row from A must be the same as the vector type of a column from
1441 if (type_a
->row_type() == type_b
->column_type()) {
1442 /* The resulting matrix has the number of columns of matrix B and
1443 * the number of rows of matrix A. We get the row count of A by
1444 * looking at the size of a vector that makes up a column. The
1445 * transpose (size of a row) is done for B.
1447 const glsl_type
*const type
=
1448 get_instance(type_a
->base_type
,
1449 type_a
->column_type()->vector_elements
,
1450 type_b
->row_type()->vector_elements
);
1451 assert(type
!= error_type
);
1455 } else if (type_a
== type_b
) {
1457 } else if (type_a
->is_matrix()) {
1458 /* A is a matrix and B is a column vector. Columns of A must match
1459 * rows of B. Given the other previously tested constraints, this
1460 * means the vector type of a row from A must be the same as the
1461 * vector the type of B.
1463 if (type_a
->row_type() == type_b
) {
1464 /* The resulting vector has a number of elements equal to
1465 * the number of rows of matrix A. */
1466 const glsl_type
*const type
=
1467 get_instance(type_a
->base_type
,
1468 type_a
->column_type()->vector_elements
,
1470 assert(type
!= error_type
);
1475 assert(type_b
->is_matrix());
1477 /* A is a row vector and B is a matrix. Columns of A must match rows
1478 * of B. Given the other previously tested constraints, this means
1479 * the type of A must be the same as the vector type of a column from
1482 if (type_a
== type_b
->column_type()) {
1483 /* The resulting vector has a number of elements equal to
1484 * the number of columns of matrix B. */
1485 const glsl_type
*const type
=
1486 get_instance(type_a
->base_type
,
1487 type_b
->row_type()->vector_elements
,
1489 assert(type
!= error_type
);
1500 glsl_type::field_type(const char *name
) const
1502 if (this->base_type
!= GLSL_TYPE_STRUCT
1503 && this->base_type
!= GLSL_TYPE_INTERFACE
)
1506 for (unsigned i
= 0; i
< this->length
; i
++) {
1507 if (strcmp(name
, this->fields
.structure
[i
].name
) == 0)
1508 return this->fields
.structure
[i
].type
;
1516 glsl_type::field_index(const char *name
) const
1518 if (this->base_type
!= GLSL_TYPE_STRUCT
1519 && this->base_type
!= GLSL_TYPE_INTERFACE
)
1522 for (unsigned i
= 0; i
< this->length
; i
++) {
1523 if (strcmp(name
, this->fields
.structure
[i
].name
) == 0)
1532 glsl_type::component_slots() const
1534 switch (this->base_type
) {
1535 case GLSL_TYPE_UINT
:
1537 case GLSL_TYPE_UINT8
:
1538 case GLSL_TYPE_INT8
:
1539 case GLSL_TYPE_UINT16
:
1540 case GLSL_TYPE_INT16
:
1541 case GLSL_TYPE_FLOAT
:
1542 case GLSL_TYPE_FLOAT16
:
1543 case GLSL_TYPE_BOOL
:
1544 return this->components();
1546 case GLSL_TYPE_DOUBLE
:
1547 case GLSL_TYPE_UINT64
:
1548 case GLSL_TYPE_INT64
:
1549 return 2 * this->components();
1551 case GLSL_TYPE_STRUCT
:
1552 case GLSL_TYPE_INTERFACE
: {
1555 for (unsigned i
= 0; i
< this->length
; i
++)
1556 size
+= this->fields
.structure
[i
].type
->component_slots();
1561 case GLSL_TYPE_ARRAY
:
1562 return this->length
* this->fields
.array
->component_slots();
1564 case GLSL_TYPE_SAMPLER
:
1565 case GLSL_TYPE_IMAGE
:
1568 case GLSL_TYPE_SUBROUTINE
:
1571 case GLSL_TYPE_FUNCTION
:
1572 case GLSL_TYPE_ATOMIC_UINT
:
1573 case GLSL_TYPE_VOID
:
1574 case GLSL_TYPE_ERROR
:
1582 glsl_type::struct_location_offset(unsigned length
) const
1584 unsigned offset
= 0;
1585 const glsl_type
*t
= this->without_array();
1586 if (t
->is_struct()) {
1587 assert(length
<= t
->length
);
1589 for (unsigned i
= 0; i
< length
; i
++) {
1590 const glsl_type
*st
= t
->fields
.structure
[i
].type
;
1591 const glsl_type
*wa
= st
->without_array();
1592 if (wa
->is_struct()) {
1593 unsigned r_offset
= wa
->struct_location_offset(wa
->length
);
1594 offset
+= st
->is_array() ?
1595 st
->arrays_of_arrays_size() * r_offset
: r_offset
;
1596 } else if (st
->is_array() && st
->fields
.array
->is_array()) {
1597 unsigned outer_array_size
= st
->length
;
1598 const glsl_type
*base_type
= st
->fields
.array
;
1600 /* For arrays of arrays the outer arrays take up a uniform
1601 * slot for each element. The innermost array elements share a
1602 * single slot so we ignore the innermost array when calculating
1605 while (base_type
->fields
.array
->is_array()) {
1606 outer_array_size
= outer_array_size
* base_type
->length
;
1607 base_type
= base_type
->fields
.array
;
1609 offset
+= outer_array_size
;
1611 /* We dont worry about arrays here because unless the array
1612 * contains a structure or another array it only takes up a single
1623 glsl_type::uniform_locations() const
1627 switch (this->base_type
) {
1628 case GLSL_TYPE_UINT
:
1630 case GLSL_TYPE_FLOAT
:
1631 case GLSL_TYPE_FLOAT16
:
1632 case GLSL_TYPE_DOUBLE
:
1633 case GLSL_TYPE_UINT16
:
1634 case GLSL_TYPE_UINT8
:
1635 case GLSL_TYPE_INT16
:
1636 case GLSL_TYPE_INT8
:
1637 case GLSL_TYPE_UINT64
:
1638 case GLSL_TYPE_INT64
:
1639 case GLSL_TYPE_BOOL
:
1640 case GLSL_TYPE_SAMPLER
:
1641 case GLSL_TYPE_IMAGE
:
1642 case GLSL_TYPE_SUBROUTINE
:
1645 case GLSL_TYPE_STRUCT
:
1646 case GLSL_TYPE_INTERFACE
:
1647 for (unsigned i
= 0; i
< this->length
; i
++)
1648 size
+= this->fields
.structure
[i
].type
->uniform_locations();
1650 case GLSL_TYPE_ARRAY
:
1651 return this->length
* this->fields
.array
->uniform_locations();
1658 glsl_type::varying_count() const
1662 switch (this->base_type
) {
1663 case GLSL_TYPE_UINT
:
1665 case GLSL_TYPE_FLOAT
:
1666 case GLSL_TYPE_FLOAT16
:
1667 case GLSL_TYPE_DOUBLE
:
1668 case GLSL_TYPE_BOOL
:
1669 case GLSL_TYPE_UINT16
:
1670 case GLSL_TYPE_UINT8
:
1671 case GLSL_TYPE_INT16
:
1672 case GLSL_TYPE_INT8
:
1673 case GLSL_TYPE_UINT64
:
1674 case GLSL_TYPE_INT64
:
1677 case GLSL_TYPE_STRUCT
:
1678 case GLSL_TYPE_INTERFACE
:
1679 for (unsigned i
= 0; i
< this->length
; i
++)
1680 size
+= this->fields
.structure
[i
].type
->varying_count();
1682 case GLSL_TYPE_ARRAY
:
1683 /* Don't count innermost array elements */
1684 if (this->without_array()->is_struct() ||
1685 this->without_array()->is_interface() ||
1686 this->fields
.array
->is_array())
1687 return this->length
* this->fields
.array
->varying_count();
1689 return this->fields
.array
->varying_count();
1691 assert(!"unsupported varying type");
1697 glsl_type::can_implicitly_convert_to(const glsl_type
*desired
,
1698 _mesa_glsl_parse_state
*state
) const
1700 if (this == desired
)
1703 /* GLSL 1.10 and ESSL do not allow implicit conversions. If there is no
1704 * state, we're doing intra-stage function linking where these checks have
1705 * already been done.
1707 if (state
&& !state
->has_implicit_conversions())
1710 /* There is no conversion among matrix types. */
1711 if (this->matrix_columns
> 1 || desired
->matrix_columns
> 1)
1714 /* Vector size must match. */
1715 if (this->vector_elements
!= desired
->vector_elements
)
1718 /* int and uint can be converted to float. */
1719 if (desired
->is_float() && this->is_integer_32())
1722 /* With GLSL 4.0, ARB_gpu_shader5, or MESA_shader_integer_functions, int
1723 * can be converted to uint. Note that state may be NULL here, when
1724 * resolving function calls in the linker. By this time, all the
1725 * state-dependent checks have already happened though, so allow anything
1726 * that's allowed in any shader version.
1728 if ((!state
|| state
->has_implicit_int_to_uint_conversion()) &&
1729 desired
->base_type
== GLSL_TYPE_UINT
&& this->base_type
== GLSL_TYPE_INT
)
1732 /* No implicit conversions from double. */
1733 if ((!state
|| state
->has_double()) && this->is_double())
1736 /* Conversions from different types to double. */
1737 if ((!state
|| state
->has_double()) && desired
->is_double()) {
1738 if (this->is_float())
1740 if (this->is_integer_32())
1748 glsl_type::std140_base_alignment(bool row_major
) const
1750 unsigned N
= is_64bit() ? 8 : 4;
1752 /* (1) If the member is a scalar consuming <N> basic machine units, the
1753 * base alignment is <N>.
1755 * (2) If the member is a two- or four-component vector with components
1756 * consuming <N> basic machine units, the base alignment is 2<N> or
1757 * 4<N>, respectively.
1759 * (3) If the member is a three-component vector with components consuming
1760 * <N> basic machine units, the base alignment is 4<N>.
1762 if (this->is_scalar() || this->is_vector()) {
1763 switch (this->vector_elements
) {
1774 /* (4) If the member is an array of scalars or vectors, the base alignment
1775 * and array stride are set to match the base alignment of a single
1776 * array element, according to rules (1), (2), and (3), and rounded up
1777 * to the base alignment of a vec4. The array may have padding at the
1778 * end; the base offset of the member following the array is rounded up
1779 * to the next multiple of the base alignment.
1781 * (6) If the member is an array of <S> column-major matrices with <C>
1782 * columns and <R> rows, the matrix is stored identically to a row of
1783 * <S>*<C> column vectors with <R> components each, according to rule
1786 * (8) If the member is an array of <S> row-major matrices with <C> columns
1787 * and <R> rows, the matrix is stored identically to a row of <S>*<R>
1788 * row vectors with <C> components each, according to rule (4).
1790 * (10) If the member is an array of <S> structures, the <S> elements of
1791 * the array are laid out in order, according to rule (9).
1793 if (this->is_array()) {
1794 if (this->fields
.array
->is_scalar() ||
1795 this->fields
.array
->is_vector() ||
1796 this->fields
.array
->is_matrix()) {
1797 return MAX2(this->fields
.array
->std140_base_alignment(row_major
), 16);
1799 assert(this->fields
.array
->is_struct() ||
1800 this->fields
.array
->is_array());
1801 return this->fields
.array
->std140_base_alignment(row_major
);
1805 /* (5) If the member is a column-major matrix with <C> columns and
1806 * <R> rows, the matrix is stored identically to an array of
1807 * <C> column vectors with <R> components each, according to
1810 * (7) If the member is a row-major matrix with <C> columns and <R>
1811 * rows, the matrix is stored identically to an array of <R>
1812 * row vectors with <C> components each, according to rule (4).
1814 if (this->is_matrix()) {
1815 const struct glsl_type
*vec_type
, *array_type
;
1816 int c
= this->matrix_columns
;
1817 int r
= this->vector_elements
;
1820 vec_type
= get_instance(base_type
, c
, 1);
1821 array_type
= glsl_type::get_array_instance(vec_type
, r
);
1823 vec_type
= get_instance(base_type
, r
, 1);
1824 array_type
= glsl_type::get_array_instance(vec_type
, c
);
1827 return array_type
->std140_base_alignment(false);
1830 /* (9) If the member is a structure, the base alignment of the
1831 * structure is <N>, where <N> is the largest base alignment
1832 * value of any of its members, and rounded up to the base
1833 * alignment of a vec4. The individual members of this
1834 * sub-structure are then assigned offsets by applying this set
1835 * of rules recursively, where the base offset of the first
1836 * member of the sub-structure is equal to the aligned offset
1837 * of the structure. The structure may have padding at the end;
1838 * the base offset of the member following the sub-structure is
1839 * rounded up to the next multiple of the base alignment of the
1842 if (this->is_struct()) {
1843 unsigned base_alignment
= 16;
1844 for (unsigned i
= 0; i
< this->length
; i
++) {
1845 bool field_row_major
= row_major
;
1846 const enum glsl_matrix_layout matrix_layout
=
1847 glsl_matrix_layout(this->fields
.structure
[i
].matrix_layout
);
1848 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
1849 field_row_major
= true;
1850 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
1851 field_row_major
= false;
1854 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
1855 base_alignment
= MAX2(base_alignment
,
1856 field_type
->std140_base_alignment(field_row_major
));
1858 return base_alignment
;
1861 assert(!"not reached");
1866 glsl_type::std140_size(bool row_major
) const
1868 unsigned N
= is_64bit() ? 8 : 4;
1870 /* (1) If the member is a scalar consuming <N> basic machine units, the
1871 * base alignment is <N>.
1873 * (2) If the member is a two- or four-component vector with components
1874 * consuming <N> basic machine units, the base alignment is 2<N> or
1875 * 4<N>, respectively.
1877 * (3) If the member is a three-component vector with components consuming
1878 * <N> basic machine units, the base alignment is 4<N>.
1880 if (this->is_scalar() || this->is_vector()) {
1881 assert(this->explicit_stride
== 0);
1882 return this->vector_elements
* N
;
1885 /* (5) If the member is a column-major matrix with <C> columns and
1886 * <R> rows, the matrix is stored identically to an array of
1887 * <C> column vectors with <R> components each, according to
1890 * (6) If the member is an array of <S> column-major matrices with <C>
1891 * columns and <R> rows, the matrix is stored identically to a row of
1892 * <S>*<C> column vectors with <R> components each, according to rule
1895 * (7) If the member is a row-major matrix with <C> columns and <R>
1896 * rows, the matrix is stored identically to an array of <R>
1897 * row vectors with <C> components each, according to rule (4).
1899 * (8) If the member is an array of <S> row-major matrices with <C> columns
1900 * and <R> rows, the matrix is stored identically to a row of <S>*<R>
1901 * row vectors with <C> components each, according to rule (4).
1903 if (this->without_array()->is_matrix()) {
1904 const struct glsl_type
*element_type
;
1905 const struct glsl_type
*vec_type
;
1906 unsigned int array_len
;
1908 if (this->is_array()) {
1909 element_type
= this->without_array();
1910 array_len
= this->arrays_of_arrays_size();
1912 element_type
= this;
1917 vec_type
= get_instance(element_type
->base_type
,
1918 element_type
->matrix_columns
, 1);
1920 array_len
*= element_type
->vector_elements
;
1922 vec_type
= get_instance(element_type
->base_type
,
1923 element_type
->vector_elements
, 1);
1924 array_len
*= element_type
->matrix_columns
;
1926 const glsl_type
*array_type
= glsl_type::get_array_instance(vec_type
,
1929 return array_type
->std140_size(false);
1932 /* (4) If the member is an array of scalars or vectors, the base alignment
1933 * and array stride are set to match the base alignment of a single
1934 * array element, according to rules (1), (2), and (3), and rounded up
1935 * to the base alignment of a vec4. The array may have padding at the
1936 * end; the base offset of the member following the array is rounded up
1937 * to the next multiple of the base alignment.
1939 * (10) If the member is an array of <S> structures, the <S> elements of
1940 * the array are laid out in order, according to rule (9).
1942 if (this->is_array()) {
1944 if (this->without_array()->is_struct()) {
1945 stride
= this->without_array()->std140_size(row_major
);
1947 unsigned element_base_align
=
1948 this->without_array()->std140_base_alignment(row_major
);
1949 stride
= MAX2(element_base_align
, 16);
1952 unsigned size
= this->arrays_of_arrays_size() * stride
;
1953 assert(this->explicit_stride
== 0 ||
1954 size
== this->length
* this->explicit_stride
);
1958 /* (9) If the member is a structure, the base alignment of the
1959 * structure is <N>, where <N> is the largest base alignment
1960 * value of any of its members, and rounded up to the base
1961 * alignment of a vec4. The individual members of this
1962 * sub-structure are then assigned offsets by applying this set
1963 * of rules recursively, where the base offset of the first
1964 * member of the sub-structure is equal to the aligned offset
1965 * of the structure. The structure may have padding at the end;
1966 * the base offset of the member following the sub-structure is
1967 * rounded up to the next multiple of the base alignment of the
1970 if (this->is_struct() || this->is_interface()) {
1972 unsigned max_align
= 0;
1974 for (unsigned i
= 0; i
< this->length
; i
++) {
1975 bool field_row_major
= row_major
;
1976 const enum glsl_matrix_layout matrix_layout
=
1977 glsl_matrix_layout(this->fields
.structure
[i
].matrix_layout
);
1978 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
1979 field_row_major
= true;
1980 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
1981 field_row_major
= false;
1984 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
1985 unsigned align
= field_type
->std140_base_alignment(field_row_major
);
1987 /* Ignore unsized arrays when calculating size */
1988 if (field_type
->is_unsized_array())
1991 size
= glsl_align(size
, align
);
1992 size
+= field_type
->std140_size(field_row_major
);
1994 max_align
= MAX2(align
, max_align
);
1996 if (field_type
->is_struct() && (i
+ 1 < this->length
))
1997 size
= glsl_align(size
, 16);
1999 size
= glsl_align(size
, MAX2(max_align
, 16));
2003 assert(!"not reached");
2008 glsl_type::get_explicit_std140_type(bool row_major
) const
2010 if (this->is_vector() || this->is_scalar()) {
2012 } else if (this->is_matrix()) {
2013 const glsl_type
*vec_type
;
2015 vec_type
= get_instance(this->base_type
, this->matrix_columns
, 1);
2017 vec_type
= get_instance(this->base_type
, this->vector_elements
, 1);
2018 unsigned elem_size
= vec_type
->std140_size(false);
2019 unsigned stride
= glsl_align(elem_size
, 16);
2020 return get_instance(this->base_type
, this->vector_elements
,
2021 this->matrix_columns
, stride
, row_major
);
2022 } else if (this->is_array()) {
2023 unsigned elem_size
= this->fields
.array
->std140_size(row_major
);
2024 const glsl_type
*elem_type
=
2025 this->fields
.array
->get_explicit_std140_type(row_major
);
2026 unsigned stride
= glsl_align(elem_size
, 16);
2027 return get_array_instance(elem_type
, this->length
, stride
);
2028 } else if (this->is_struct() || this->is_interface()) {
2029 glsl_struct_field
*fields
= new glsl_struct_field
[this->length
];
2030 unsigned offset
= 0;
2031 for (unsigned i
= 0; i
< length
; i
++) {
2032 fields
[i
] = this->fields
.structure
[i
];
2034 bool field_row_major
= row_major
;
2035 if (fields
[i
].matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
2036 field_row_major
= false;
2037 } else if (fields
[i
].matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
2038 field_row_major
= true;
2041 fields
[i
].type
->get_explicit_std140_type(field_row_major
);
2043 unsigned fsize
= fields
[i
].type
->std140_size(field_row_major
);
2044 unsigned falign
= fields
[i
].type
->std140_base_alignment(field_row_major
);
2045 /* From the GLSL 460 spec section "Uniform and Shader Storage Block
2046 * Layout Qualifiers":
2048 * "The actual offset of a member is computed as follows: If
2049 * offset was declared, start with that offset, otherwise start
2050 * with the next available offset. If the resulting offset is not
2051 * a multiple of the actual alignment, increase it to the first
2052 * offset that is a multiple of the actual alignment. This results
2053 * in the actual offset the member will have."
2055 if (fields
[i
].offset
>= 0) {
2056 assert((unsigned)fields
[i
].offset
>= offset
);
2057 offset
= fields
[i
].offset
;
2059 offset
= glsl_align(offset
, falign
);
2060 fields
[i
].offset
= offset
;
2064 const glsl_type
*type
;
2065 if (this->is_struct())
2066 type
= get_struct_instance(fields
, this->length
, this->name
);
2068 type
= get_interface_instance(fields
, this->length
,
2069 (enum glsl_interface_packing
)this->interface_packing
,
2070 this->interface_row_major
,
2076 unreachable("Invalid type for UBO or SSBO");
2081 glsl_type::std430_base_alignment(bool row_major
) const
2084 unsigned N
= is_64bit() ? 8 : 4;
2086 /* (1) If the member is a scalar consuming <N> basic machine units, the
2087 * base alignment is <N>.
2089 * (2) If the member is a two- or four-component vector with components
2090 * consuming <N> basic machine units, the base alignment is 2<N> or
2091 * 4<N>, respectively.
2093 * (3) If the member is a three-component vector with components consuming
2094 * <N> basic machine units, the base alignment is 4<N>.
2096 if (this->is_scalar() || this->is_vector()) {
2097 switch (this->vector_elements
) {
2108 /* OpenGL 4.30 spec, section 7.6.2.2 "Standard Uniform Block Layout":
2110 * "When using the std430 storage layout, shader storage blocks will be
2111 * laid out in buffer storage identically to uniform and shader storage
2112 * blocks using the std140 layout, except that the base alignment and
2113 * stride of arrays of scalars and vectors in rule 4 and of structures
2114 * in rule 9 are not rounded up a multiple of the base alignment of a vec4.
2117 /* (1) If the member is a scalar consuming <N> basic machine units, the
2118 * base alignment is <N>.
2120 * (2) If the member is a two- or four-component vector with components
2121 * consuming <N> basic machine units, the base alignment is 2<N> or
2122 * 4<N>, respectively.
2124 * (3) If the member is a three-component vector with components consuming
2125 * <N> basic machine units, the base alignment is 4<N>.
2127 if (this->is_array())
2128 return this->fields
.array
->std430_base_alignment(row_major
);
2130 /* (5) If the member is a column-major matrix with <C> columns and
2131 * <R> rows, the matrix is stored identically to an array of
2132 * <C> column vectors with <R> components each, according to
2135 * (7) If the member is a row-major matrix with <C> columns and <R>
2136 * rows, the matrix is stored identically to an array of <R>
2137 * row vectors with <C> components each, according to rule (4).
2139 if (this->is_matrix()) {
2140 const struct glsl_type
*vec_type
, *array_type
;
2141 int c
= this->matrix_columns
;
2142 int r
= this->vector_elements
;
2145 vec_type
= get_instance(base_type
, c
, 1);
2146 array_type
= glsl_type::get_array_instance(vec_type
, r
);
2148 vec_type
= get_instance(base_type
, r
, 1);
2149 array_type
= glsl_type::get_array_instance(vec_type
, c
);
2152 return array_type
->std430_base_alignment(false);
2155 /* (9) If the member is a structure, the base alignment of the
2156 * structure is <N>, where <N> is the largest base alignment
2157 * value of any of its members, and rounded up to the base
2158 * alignment of a vec4. The individual members of this
2159 * sub-structure are then assigned offsets by applying this set
2160 * of rules recursively, where the base offset of the first
2161 * member of the sub-structure is equal to the aligned offset
2162 * of the structure. The structure may have padding at the end;
2163 * the base offset of the member following the sub-structure is
2164 * rounded up to the next multiple of the base alignment of the
2167 if (this->is_struct()) {
2168 unsigned base_alignment
= 0;
2169 for (unsigned i
= 0; i
< this->length
; i
++) {
2170 bool field_row_major
= row_major
;
2171 const enum glsl_matrix_layout matrix_layout
=
2172 glsl_matrix_layout(this->fields
.structure
[i
].matrix_layout
);
2173 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
2174 field_row_major
= true;
2175 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
2176 field_row_major
= false;
2179 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
2180 base_alignment
= MAX2(base_alignment
,
2181 field_type
->std430_base_alignment(field_row_major
));
2183 assert(base_alignment
> 0);
2184 return base_alignment
;
2186 assert(!"not reached");
2191 glsl_type::std430_array_stride(bool row_major
) const
2193 unsigned N
= is_64bit() ? 8 : 4;
2195 /* Notice that the array stride of a vec3 is not 3 * N but 4 * N.
2196 * See OpenGL 4.30 spec, section 7.6.2.2 "Standard Uniform Block Layout"
2198 * (3) If the member is a three-component vector with components consuming
2199 * <N> basic machine units, the base alignment is 4<N>.
2201 if (this->is_vector() && this->vector_elements
== 3)
2204 /* By default use std430_size(row_major) */
2205 unsigned stride
= this->std430_size(row_major
);
2206 assert(this->explicit_stride
== 0 || this->explicit_stride
== stride
);
2210 /* Note that the value returned by this method is only correct if the
2211 * explit offset, and stride values are set, so only with SPIR-V shaders.
2212 * Should not be used with GLSL shaders.
2216 glsl_type::explicit_size(bool align_to_stride
) const
2218 if (this->is_struct() || this->is_interface()) {
2219 if (this->length
> 0) {
2222 for (unsigned i
= 0; i
< this->length
; i
++) {
2223 assert(this->fields
.structure
[i
].offset
>= 0);
2224 unsigned last_byte
= this->fields
.structure
[i
].offset
+
2225 this->fields
.structure
[i
].type
->explicit_size();
2226 size
= MAX2(size
, last_byte
);
2233 } else if (this->is_array()) {
2234 /* From ARB_program_interface_query spec:
2236 * "For the property of BUFFER_DATA_SIZE, then the implementation-dependent
2237 * minimum total buffer object size, in basic machine units, required to
2238 * hold all active variables associated with an active uniform block, shader
2239 * storage block, or atomic counter buffer is written to <params>. If the
2240 * final member of an active shader storage block is array with no declared
2241 * size, the minimum buffer size is computed assuming the array was declared
2242 * as an array with one element."
2245 if (this->is_unsized_array())
2246 return this->explicit_stride
;
2248 assert(this->length
> 0);
2249 unsigned elem_size
= align_to_stride
? this->explicit_stride
: this->fields
.array
->explicit_size();
2250 assert(this->explicit_stride
>= elem_size
);
2252 return this->explicit_stride
* (this->length
- 1) + elem_size
;
2253 } else if (this->is_matrix()) {
2254 const struct glsl_type
*elem_type
;
2257 if (this->interface_row_major
) {
2258 elem_type
= get_instance(this->base_type
,
2259 this->matrix_columns
, 1);
2260 length
= this->vector_elements
;
2262 elem_type
= get_instance(this->base_type
,
2263 this->vector_elements
, 1);
2264 length
= this->matrix_columns
;
2267 unsigned elem_size
= align_to_stride
? this->explicit_stride
: elem_type
->explicit_size();
2269 assert(this->explicit_stride
);
2270 return this->explicit_stride
* (length
- 1) + elem_size
;
2273 unsigned N
= this->bit_size() / 8;
2275 return this->vector_elements
* N
;
2279 glsl_type::std430_size(bool row_major
) const
2281 unsigned N
= is_64bit() ? 8 : 4;
2283 /* OpenGL 4.30 spec, section 7.6.2.2 "Standard Uniform Block Layout":
2285 * "When using the std430 storage layout, shader storage blocks will be
2286 * laid out in buffer storage identically to uniform and shader storage
2287 * blocks using the std140 layout, except that the base alignment and
2288 * stride of arrays of scalars and vectors in rule 4 and of structures
2289 * in rule 9 are not rounded up a multiple of the base alignment of a vec4.
2291 if (this->is_scalar() || this->is_vector()) {
2292 assert(this->explicit_stride
== 0);
2293 return this->vector_elements
* N
;
2296 if (this->without_array()->is_matrix()) {
2297 const struct glsl_type
*element_type
;
2298 const struct glsl_type
*vec_type
;
2299 unsigned int array_len
;
2301 if (this->is_array()) {
2302 element_type
= this->without_array();
2303 array_len
= this->arrays_of_arrays_size();
2305 element_type
= this;
2310 vec_type
= get_instance(element_type
->base_type
,
2311 element_type
->matrix_columns
, 1);
2313 array_len
*= element_type
->vector_elements
;
2315 vec_type
= get_instance(element_type
->base_type
,
2316 element_type
->vector_elements
, 1);
2317 array_len
*= element_type
->matrix_columns
;
2319 const glsl_type
*array_type
= glsl_type::get_array_instance(vec_type
,
2322 return array_type
->std430_size(false);
2325 if (this->is_array()) {
2327 if (this->without_array()->is_struct())
2328 stride
= this->without_array()->std430_size(row_major
);
2330 stride
= this->without_array()->std430_base_alignment(row_major
);
2332 unsigned size
= this->arrays_of_arrays_size() * stride
;
2333 assert(this->explicit_stride
== 0 ||
2334 size
== this->length
* this->explicit_stride
);
2338 if (this->is_struct() || this->is_interface()) {
2340 unsigned max_align
= 0;
2342 for (unsigned i
= 0; i
< this->length
; i
++) {
2343 bool field_row_major
= row_major
;
2344 const enum glsl_matrix_layout matrix_layout
=
2345 glsl_matrix_layout(this->fields
.structure
[i
].matrix_layout
);
2346 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
2347 field_row_major
= true;
2348 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
2349 field_row_major
= false;
2352 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
2353 unsigned align
= field_type
->std430_base_alignment(field_row_major
);
2354 size
= glsl_align(size
, align
);
2355 size
+= field_type
->std430_size(field_row_major
);
2357 max_align
= MAX2(align
, max_align
);
2359 size
= glsl_align(size
, max_align
);
2363 assert(!"not reached");
2368 glsl_type::get_explicit_std430_type(bool row_major
) const
2370 if (this->is_vector() || this->is_scalar()) {
2372 } else if (this->is_matrix()) {
2373 const glsl_type
*vec_type
;
2375 vec_type
= get_instance(this->base_type
, this->matrix_columns
, 1);
2377 vec_type
= get_instance(this->base_type
, this->vector_elements
, 1);
2378 unsigned stride
= vec_type
->std430_array_stride(false);
2379 return get_instance(this->base_type
, this->vector_elements
,
2380 this->matrix_columns
, stride
, row_major
);
2381 } else if (this->is_array()) {
2382 const glsl_type
*elem_type
=
2383 this->fields
.array
->get_explicit_std430_type(row_major
);
2384 unsigned stride
= this->fields
.array
->std430_array_stride(row_major
);
2385 return get_array_instance(elem_type
, this->length
, stride
);
2386 } else if (this->is_struct() || this->is_interface()) {
2387 glsl_struct_field
*fields
= new glsl_struct_field
[this->length
];
2388 unsigned offset
= 0;
2389 for (unsigned i
= 0; i
< length
; i
++) {
2390 fields
[i
] = this->fields
.structure
[i
];
2392 bool field_row_major
= row_major
;
2393 if (fields
[i
].matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
2394 field_row_major
= false;
2395 } else if (fields
[i
].matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
2396 field_row_major
= true;
2399 fields
[i
].type
->get_explicit_std430_type(field_row_major
);
2401 unsigned fsize
= fields
[i
].type
->std430_size(field_row_major
);
2402 unsigned falign
= fields
[i
].type
->std430_base_alignment(field_row_major
);
2403 /* From the GLSL 460 spec section "Uniform and Shader Storage Block
2404 * Layout Qualifiers":
2406 * "The actual offset of a member is computed as follows: If
2407 * offset was declared, start with that offset, otherwise start
2408 * with the next available offset. If the resulting offset is not
2409 * a multiple of the actual alignment, increase it to the first
2410 * offset that is a multiple of the actual alignment. This results
2411 * in the actual offset the member will have."
2413 if (fields
[i
].offset
>= 0) {
2414 assert((unsigned)fields
[i
].offset
>= offset
);
2415 offset
= fields
[i
].offset
;
2417 offset
= glsl_align(offset
, falign
);
2418 fields
[i
].offset
= offset
;
2422 const glsl_type
*type
;
2423 if (this->is_struct())
2424 type
= get_struct_instance(fields
, this->length
, this->name
);
2426 type
= get_interface_instance(fields
, this->length
,
2427 (enum glsl_interface_packing
)this->interface_packing
,
2428 this->interface_row_major
,
2434 unreachable("Invalid type for SSBO");
2439 glsl_type::get_explicit_interface_type(bool supports_std430
) const
2441 enum glsl_interface_packing packing
=
2442 this->get_internal_ifc_packing(supports_std430
);
2443 if (packing
== GLSL_INTERFACE_PACKING_STD140
) {
2444 return this->get_explicit_std140_type(this->interface_row_major
);
2446 assert(packing
== GLSL_INTERFACE_PACKING_STD430
);
2447 return this->get_explicit_std430_type(this->interface_row_major
);
2452 explicit_type_scalar_byte_size(const glsl_type
*type
)
2454 if (type
->base_type
== GLSL_TYPE_BOOL
)
2457 return glsl_base_type_get_bit_size(type
->base_type
) / 8;
2460 /* This differs from get_explicit_std430_type() in that it:
2461 * - can size arrays slightly smaller ("stride * (len - 1) + elem_size" instead
2462 * of "stride * len")
2463 * - consumes a glsl_type_size_align_func which allows 8 and 16-bit values to be
2464 * packed more tightly
2465 * - overrides any struct field offsets but get_explicit_std430_type() tries to
2466 * respect any existing ones
2469 glsl_type::get_explicit_type_for_size_align(glsl_type_size_align_func type_info
,
2470 unsigned *size
, unsigned *alignment
) const
2472 if (this->is_scalar()) {
2473 type_info(this, size
, alignment
);
2474 assert(*size
== explicit_type_scalar_byte_size(this));
2475 assert(*alignment
== explicit_type_scalar_byte_size(this));
2477 } else if (this->is_vector()) {
2478 type_info(this, size
, alignment
);
2479 assert(*alignment
% explicit_type_scalar_byte_size(this) == 0);
2480 return glsl_type::get_instance(this->base_type
, this->vector_elements
,
2481 1, 0, false, *alignment
);
2482 } else if (this->is_array()) {
2483 unsigned elem_size
, elem_align
;
2484 const struct glsl_type
*explicit_element
=
2485 this->fields
.array
->get_explicit_type_for_size_align(type_info
, &elem_size
, &elem_align
);
2487 unsigned stride
= align(elem_size
, elem_align
);
2489 *size
= stride
* (this->length
- 1) + elem_size
;
2490 *alignment
= elem_align
;
2491 return glsl_type::get_array_instance(explicit_element
, this->length
, stride
);
2492 } else if (this->is_struct() || this->is_interface()) {
2493 struct glsl_struct_field
*fields
= (struct glsl_struct_field
*)
2494 malloc(sizeof(struct glsl_struct_field
) * this->length
);
2498 for (unsigned i
= 0; i
< this->length
; i
++) {
2499 fields
[i
] = this->fields
.structure
[i
];
2500 assert(fields
[i
].matrix_layout
!= GLSL_MATRIX_LAYOUT_ROW_MAJOR
);
2502 unsigned field_size
, field_align
;
2504 fields
[i
].type
->get_explicit_type_for_size_align(type_info
, &field_size
, &field_align
);
2505 field_align
= this->packed
? 1 : field_align
;
2506 fields
[i
].offset
= align(*size
, field_align
);
2508 *size
= fields
[i
].offset
+ field_size
;
2509 *alignment
= MAX2(*alignment
, field_align
);
2512 const glsl_type
*type
;
2513 if (this->is_struct()) {
2514 type
= get_struct_instance(fields
, this->length
, this->name
,
2515 this->packed
, *alignment
);
2517 assert(!this->packed
);
2518 type
= get_interface_instance(fields
, this->length
,
2519 (enum glsl_interface_packing
)this->interface_packing
,
2520 this->interface_row_major
,
2525 } else if (this->is_matrix()) {
2526 unsigned col_size
, col_align
;
2527 type_info(this->column_type(), &col_size
, &col_align
);
2528 unsigned stride
= align(col_size
, col_align
);
2530 *size
= this->matrix_columns
* stride
;
2531 /* Matrix and column alignments match. See glsl_type::column_type() */
2532 *alignment
= col_align
;
2533 return glsl_type::get_instance(this->base_type
, this->vector_elements
,
2534 this->matrix_columns
, stride
, false, *alignment
);
2536 unreachable("Unhandled type.");
2541 glsl_type::count_vec4_slots(bool is_gl_vertex_input
, bool is_bindless
) const
2543 /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
2545 * "A scalar input counts the same amount against this limit as a vec4,
2546 * so applications may want to consider packing groups of four
2547 * unrelated float inputs together into a vector to better utilize the
2548 * capabilities of the underlying hardware. A matrix input will use up
2549 * multiple locations. The number of locations used will equal the
2550 * number of columns in the matrix."
2552 * The spec does not explicitly say how arrays are counted. However, it
2553 * should be safe to assume the total number of slots consumed by an array
2554 * is the number of entries in the array multiplied by the number of slots
2555 * consumed by a single element of the array.
2557 * The spec says nothing about how structs are counted, because vertex
2558 * attributes are not allowed to be (or contain) structs. However, Mesa
2559 * allows varying structs, the number of varying slots taken up by a
2560 * varying struct is simply equal to the sum of the number of slots taken
2561 * up by each element.
2563 * Doubles are counted different depending on whether they are vertex
2564 * inputs or everything else. Vertex inputs from ARB_vertex_attrib_64bit
2565 * take one location no matter what size they are, otherwise dvec3/4
2566 * take two locations.
2568 switch (this->base_type
) {
2569 case GLSL_TYPE_UINT
:
2571 case GLSL_TYPE_UINT8
:
2572 case GLSL_TYPE_INT8
:
2573 case GLSL_TYPE_UINT16
:
2574 case GLSL_TYPE_INT16
:
2575 case GLSL_TYPE_FLOAT
:
2576 case GLSL_TYPE_FLOAT16
:
2577 case GLSL_TYPE_BOOL
:
2578 return this->matrix_columns
;
2579 case GLSL_TYPE_DOUBLE
:
2580 case GLSL_TYPE_UINT64
:
2581 case GLSL_TYPE_INT64
:
2582 if (this->vector_elements
> 2 && !is_gl_vertex_input
)
2583 return this->matrix_columns
* 2;
2585 return this->matrix_columns
;
2586 case GLSL_TYPE_STRUCT
:
2587 case GLSL_TYPE_INTERFACE
: {
2590 for (unsigned i
= 0; i
< this->length
; i
++) {
2591 const glsl_type
*member_type
= this->fields
.structure
[i
].type
;
2592 size
+= member_type
->count_vec4_slots(is_gl_vertex_input
, is_bindless
);
2598 case GLSL_TYPE_ARRAY
: {
2599 const glsl_type
*element
= this->fields
.array
;
2600 return this->length
* element
->count_vec4_slots(is_gl_vertex_input
,
2604 case GLSL_TYPE_SAMPLER
:
2605 case GLSL_TYPE_IMAGE
:
2611 case GLSL_TYPE_SUBROUTINE
:
2614 case GLSL_TYPE_FUNCTION
:
2615 case GLSL_TYPE_ATOMIC_UINT
:
2616 case GLSL_TYPE_VOID
:
2617 case GLSL_TYPE_ERROR
:
2621 assert(!"Unexpected type in count_attribute_slots()");
2627 glsl_type::count_dword_slots(bool is_bindless
) const
2629 switch (this->base_type
) {
2630 case GLSL_TYPE_UINT
:
2632 case GLSL_TYPE_FLOAT
:
2633 case GLSL_TYPE_BOOL
:
2634 return this->components();
2635 case GLSL_TYPE_UINT16
:
2636 case GLSL_TYPE_INT16
:
2637 case GLSL_TYPE_FLOAT16
:
2638 return DIV_ROUND_UP(this->components(), 2);
2639 case GLSL_TYPE_UINT8
:
2640 case GLSL_TYPE_INT8
:
2641 return DIV_ROUND_UP(this->components(), 4);
2642 case GLSL_TYPE_IMAGE
:
2643 case GLSL_TYPE_SAMPLER
:
2647 case GLSL_TYPE_DOUBLE
:
2648 case GLSL_TYPE_UINT64
:
2649 case GLSL_TYPE_INT64
:
2650 return this->components() * 2;
2651 case GLSL_TYPE_ARRAY
:
2652 return this->fields
.array
->count_dword_slots(is_bindless
) *
2655 case GLSL_TYPE_INTERFACE
:
2656 case GLSL_TYPE_STRUCT
: {
2658 for (unsigned i
= 0; i
< this->length
; i
++) {
2659 size
+= this->fields
.structure
[i
].type
->count_dword_slots(is_bindless
);
2664 case GLSL_TYPE_ATOMIC_UINT
:
2666 case GLSL_TYPE_SUBROUTINE
:
2668 case GLSL_TYPE_VOID
:
2669 case GLSL_TYPE_ERROR
:
2670 case GLSL_TYPE_FUNCTION
:
2672 unreachable("invalid type in st_glsl_type_dword_size()");
2679 glsl_type::coordinate_components() const
2681 enum glsl_sampler_dim dim
= (enum glsl_sampler_dim
)sampler_dimensionality
;
2682 int size
= glsl_get_sampler_dim_coordinate_components(dim
);
2684 /* Array textures need an additional component for the array index, except
2685 * for cubemap array images that behave like a 2D array of interleaved
2688 if (sampler_array
&&
2689 !(is_image() && sampler_dimensionality
== GLSL_SAMPLER_DIM_CUBE
))
2696 * Declarations of type flyweights (glsl_type::_foo_type) and
2697 * convenience pointers (glsl_type::foo_type).
2700 #define DECL_TYPE(NAME, ...) \
2701 const glsl_type glsl_type::_##NAME##_type = glsl_type(__VA_ARGS__, #NAME); \
2702 const glsl_type *const glsl_type::NAME##_type = &glsl_type::_##NAME##_type;
2704 #define STRUCT_TYPE(NAME)
2706 #include "compiler/builtin_type_macros.h"
2712 unsigned base_type
:5;
2713 unsigned interface_row_major
:1;
2714 unsigned vector_elements
:3;
2715 unsigned matrix_columns
:3;
2716 unsigned explicit_stride
:16;
2717 unsigned explicit_alignment
:4;
2720 unsigned base_type
:5;
2721 unsigned dimensionality
:4;
2724 unsigned sampled_type
:5;
2728 unsigned base_type
:5;
2730 unsigned explicit_stride
:14;
2733 unsigned base_type
:5;
2734 unsigned interface_packing_or_packed
:2;
2735 unsigned interface_row_major
:1;
2737 unsigned explicit_alignment
:4;
2742 encode_glsl_struct_field(blob
*blob
, const glsl_struct_field
*struct_field
)
2744 encode_type_to_blob(blob
, struct_field
->type
);
2745 blob_write_string(blob
, struct_field
->name
);
2746 blob_write_uint32(blob
, struct_field
->location
);
2747 blob_write_uint32(blob
, struct_field
->offset
);
2748 blob_write_uint32(blob
, struct_field
->xfb_buffer
);
2749 blob_write_uint32(blob
, struct_field
->xfb_stride
);
2750 blob_write_uint32(blob
, struct_field
->image_format
);
2751 blob_write_uint32(blob
, struct_field
->flags
);
2755 decode_glsl_struct_field_from_blob(blob_reader
*blob
, glsl_struct_field
*struct_field
)
2757 struct_field
->type
= decode_type_from_blob(blob
);
2758 struct_field
->name
= blob_read_string(blob
);
2759 struct_field
->location
= blob_read_uint32(blob
);
2760 struct_field
->offset
= blob_read_uint32(blob
);
2761 struct_field
->xfb_buffer
= blob_read_uint32(blob
);
2762 struct_field
->xfb_stride
= blob_read_uint32(blob
);
2763 struct_field
->image_format
= (pipe_format
)blob_read_uint32(blob
);
2764 struct_field
->flags
= blob_read_uint32(blob
);
2768 encode_type_to_blob(struct blob
*blob
, const glsl_type
*type
)
2771 blob_write_uint32(blob
, 0);
2775 STATIC_ASSERT(sizeof(union packed_type
) == 4);
2776 union packed_type encoded
;
2778 encoded
.basic
.base_type
= type
->base_type
;
2780 switch (type
->base_type
) {
2781 case GLSL_TYPE_UINT
:
2783 case GLSL_TYPE_FLOAT
:
2784 case GLSL_TYPE_FLOAT16
:
2785 case GLSL_TYPE_DOUBLE
:
2786 case GLSL_TYPE_UINT8
:
2787 case GLSL_TYPE_INT8
:
2788 case GLSL_TYPE_UINT16
:
2789 case GLSL_TYPE_INT16
:
2790 case GLSL_TYPE_UINT64
:
2791 case GLSL_TYPE_INT64
:
2792 case GLSL_TYPE_BOOL
:
2793 encoded
.basic
.interface_row_major
= type
->interface_row_major
;
2794 assert(type
->matrix_columns
< 8);
2795 if (type
->vector_elements
<= 4)
2796 encoded
.basic
.vector_elements
= type
->vector_elements
;
2797 else if (type
->vector_elements
== 8)
2798 encoded
.basic
.vector_elements
= 5;
2799 else if (type
->vector_elements
== 16)
2800 encoded
.basic
.vector_elements
= 6;
2801 encoded
.basic
.matrix_columns
= type
->matrix_columns
;
2802 encoded
.basic
.explicit_stride
= MIN2(type
->explicit_stride
, 0xffff);
2803 encoded
.basic
.explicit_alignment
=
2804 MIN2(ffs(type
->explicit_alignment
), 0xf);
2805 blob_write_uint32(blob
, encoded
.u32
);
2806 /* If we don't have enough bits for explicit_stride, store it
2809 if (encoded
.basic
.explicit_stride
== 0xffff)
2810 blob_write_uint32(blob
, type
->explicit_stride
);
2811 if (encoded
.basic
.explicit_alignment
== 0xf)
2812 blob_write_uint32(blob
, type
->explicit_alignment
);
2814 case GLSL_TYPE_SAMPLER
:
2815 encoded
.sampler
.dimensionality
= type
->sampler_dimensionality
;
2816 encoded
.sampler
.shadow
= type
->sampler_shadow
;
2817 encoded
.sampler
.array
= type
->sampler_array
;
2818 encoded
.sampler
.sampled_type
= type
->sampled_type
;
2820 case GLSL_TYPE_SUBROUTINE
:
2821 blob_write_uint32(blob
, encoded
.u32
);
2822 blob_write_string(blob
, type
->name
);
2824 case GLSL_TYPE_IMAGE
:
2825 encoded
.sampler
.dimensionality
= type
->sampler_dimensionality
;
2826 encoded
.sampler
.array
= type
->sampler_array
;
2827 encoded
.sampler
.sampled_type
= type
->sampled_type
;
2829 case GLSL_TYPE_ATOMIC_UINT
:
2831 case GLSL_TYPE_ARRAY
:
2832 encoded
.array
.length
= MIN2(type
->length
, 0x1fff);
2833 encoded
.array
.explicit_stride
= MIN2(type
->explicit_stride
, 0x3fff);
2834 blob_write_uint32(blob
, encoded
.u32
);
2835 /* If we don't have enough bits for length or explicit_stride, store it
2838 if (encoded
.array
.length
== 0x1fff)
2839 blob_write_uint32(blob
, type
->length
);
2840 if (encoded
.array
.explicit_stride
== 0x3fff)
2841 blob_write_uint32(blob
, type
->explicit_stride
);
2842 encode_type_to_blob(blob
, type
->fields
.array
);
2844 case GLSL_TYPE_STRUCT
:
2845 case GLSL_TYPE_INTERFACE
:
2846 encoded
.strct
.length
= MIN2(type
->length
, 0xfffff);
2847 encoded
.strct
.explicit_alignment
=
2848 MIN2(ffs(type
->explicit_alignment
), 0xf);
2849 if (type
->is_interface()) {
2850 encoded
.strct
.interface_packing_or_packed
= type
->interface_packing
;
2851 encoded
.strct
.interface_row_major
= type
->interface_row_major
;
2853 encoded
.strct
.interface_packing_or_packed
= type
->packed
;
2855 blob_write_uint32(blob
, encoded
.u32
);
2856 blob_write_string(blob
, type
->name
);
2858 /* If we don't have enough bits for length, store it separately. */
2859 if (encoded
.strct
.length
== 0xfffff)
2860 blob_write_uint32(blob
, type
->length
);
2861 if (encoded
.strct
.length
== 0xf)
2862 blob_write_uint32(blob
, type
->explicit_alignment
);
2864 for (unsigned i
= 0; i
< type
->length
; i
++)
2865 encode_glsl_struct_field(blob
, &type
->fields
.structure
[i
]);
2867 case GLSL_TYPE_VOID
:
2869 case GLSL_TYPE_ERROR
:
2871 assert(!"Cannot encode type!");
2876 blob_write_uint32(blob
, encoded
.u32
);
2880 decode_type_from_blob(struct blob_reader
*blob
)
2882 union packed_type encoded
;
2883 encoded
.u32
= blob_read_uint32(blob
);
2885 if (encoded
.u32
== 0) {
2889 glsl_base_type base_type
= (glsl_base_type
)encoded
.basic
.base_type
;
2891 switch (base_type
) {
2892 case GLSL_TYPE_UINT
:
2894 case GLSL_TYPE_FLOAT
:
2895 case GLSL_TYPE_FLOAT16
:
2896 case GLSL_TYPE_DOUBLE
:
2897 case GLSL_TYPE_UINT8
:
2898 case GLSL_TYPE_INT8
:
2899 case GLSL_TYPE_UINT16
:
2900 case GLSL_TYPE_INT16
:
2901 case GLSL_TYPE_UINT64
:
2902 case GLSL_TYPE_INT64
:
2903 case GLSL_TYPE_BOOL
: {
2904 unsigned explicit_stride
= encoded
.basic
.explicit_stride
;
2905 if (explicit_stride
== 0xffff)
2906 explicit_stride
= blob_read_uint32(blob
);
2907 unsigned explicit_alignment
= encoded
.basic
.explicit_alignment
;
2908 if (explicit_alignment
== 0xf)
2909 explicit_alignment
= blob_read_uint32(blob
);
2910 else if (explicit_alignment
> 0)
2911 explicit_alignment
= 1 << (explicit_alignment
- 1);
2912 uint32_t vector_elements
= encoded
.basic
.vector_elements
;
2913 if (vector_elements
== 5)
2914 vector_elements
= 8;
2915 else if (vector_elements
== 6)
2916 vector_elements
= 16;
2917 return glsl_type::get_instance(base_type
, encoded
.basic
.vector_elements
,
2918 encoded
.basic
.matrix_columns
,
2920 encoded
.basic
.interface_row_major
,
2921 explicit_alignment
);
2923 case GLSL_TYPE_SAMPLER
:
2924 return glsl_type::get_sampler_instance((enum glsl_sampler_dim
)encoded
.sampler
.dimensionality
,
2925 encoded
.sampler
.shadow
,
2926 encoded
.sampler
.array
,
2927 (glsl_base_type
) encoded
.sampler
.sampled_type
);
2928 case GLSL_TYPE_SUBROUTINE
:
2929 return glsl_type::get_subroutine_instance(blob_read_string(blob
));
2930 case GLSL_TYPE_IMAGE
:
2931 return glsl_type::get_image_instance((enum glsl_sampler_dim
)encoded
.sampler
.dimensionality
,
2932 encoded
.sampler
.array
,
2933 (glsl_base_type
) encoded
.sampler
.sampled_type
);
2934 case GLSL_TYPE_ATOMIC_UINT
:
2935 return glsl_type::atomic_uint_type
;
2936 case GLSL_TYPE_ARRAY
: {
2937 unsigned length
= encoded
.array
.length
;
2938 if (length
== 0x1fff)
2939 length
= blob_read_uint32(blob
);
2940 unsigned explicit_stride
= encoded
.array
.explicit_stride
;
2941 if (explicit_stride
== 0x3fff)
2942 explicit_stride
= blob_read_uint32(blob
);
2943 return glsl_type::get_array_instance(decode_type_from_blob(blob
),
2944 length
, explicit_stride
);
2946 case GLSL_TYPE_STRUCT
:
2947 case GLSL_TYPE_INTERFACE
: {
2948 char *name
= blob_read_string(blob
);
2949 unsigned num_fields
= encoded
.strct
.length
;
2950 if (num_fields
== 0xfffff)
2951 num_fields
= blob_read_uint32(blob
);
2952 unsigned explicit_alignment
= encoded
.strct
.explicit_alignment
;
2953 if (explicit_alignment
== 0xf)
2954 explicit_alignment
= blob_read_uint32(blob
);
2955 else if (explicit_alignment
> 0)
2956 explicit_alignment
= 1 << (explicit_alignment
- 1);
2958 glsl_struct_field
*fields
=
2959 (glsl_struct_field
*) malloc(sizeof(glsl_struct_field
) * num_fields
);
2960 for (unsigned i
= 0; i
< num_fields
; i
++)
2961 decode_glsl_struct_field_from_blob(blob
, &fields
[i
]);
2964 if (base_type
== GLSL_TYPE_INTERFACE
) {
2965 assert(explicit_alignment
== 0);
2966 enum glsl_interface_packing packing
=
2967 (glsl_interface_packing
) encoded
.strct
.interface_packing_or_packed
;
2968 bool row_major
= encoded
.strct
.interface_row_major
;
2969 t
= glsl_type::get_interface_instance(fields
, num_fields
, packing
,
2972 unsigned packed
= encoded
.strct
.interface_packing_or_packed
;
2973 t
= glsl_type::get_struct_instance(fields
, num_fields
, name
, packed
,
2974 explicit_alignment
);
2980 case GLSL_TYPE_VOID
:
2981 return glsl_type::void_type
;
2982 case GLSL_TYPE_ERROR
:
2984 assert(!"Cannot decode type!");
2990 glsl_type::cl_alignment() const
2992 /* vectors unlike arrays are aligned to their size */
2993 if (this->is_scalar() || this->is_vector())
2994 return this->cl_size();
2995 else if (this->is_array())
2996 return this->without_array()->cl_alignment();
2997 else if (this->is_struct()) {
2998 /* Packed Structs are 0x1 aligned despite their size. */
3003 for (unsigned i
= 0; i
< this->length
; ++i
) {
3004 struct glsl_struct_field
&field
= this->fields
.structure
[i
];
3005 res
= MAX2(res
, field
.type
->cl_alignment());
3013 glsl_type::cl_size() const
3015 if (this->is_scalar() || this->is_vector()) {
3016 return util_next_power_of_two(this->vector_elements
) *
3017 explicit_type_scalar_byte_size(this);
3018 } else if (this->is_array()) {
3019 unsigned size
= this->without_array()->cl_size();
3020 return size
* this->length
;
3021 } else if (this->is_struct()) {
3023 for (unsigned i
= 0; i
< this->length
; ++i
) {
3024 struct glsl_struct_field
&field
= this->fields
.structure
[i
];
3025 /* if a struct is packed, members don't get aligned */
3027 size
= align(size
, field
.type
->cl_alignment());
3028 size
+= field
.type
->cl_size();
3038 glsl_get_sampler_dim_coordinate_components(enum glsl_sampler_dim dim
)
3041 case GLSL_SAMPLER_DIM_1D
:
3042 case GLSL_SAMPLER_DIM_BUF
:
3044 case GLSL_SAMPLER_DIM_2D
:
3045 case GLSL_SAMPLER_DIM_RECT
:
3046 case GLSL_SAMPLER_DIM_MS
:
3047 case GLSL_SAMPLER_DIM_EXTERNAL
:
3048 case GLSL_SAMPLER_DIM_SUBPASS
:
3049 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
3051 case GLSL_SAMPLER_DIM_3D
:
3052 case GLSL_SAMPLER_DIM_CUBE
:
3055 unreachable("Unknown sampler dim");
3060 glsl_print_type(FILE *f
, const glsl_type
*t
)
3062 if (t
->is_array()) {
3063 fprintf(f
, "(array ");
3064 glsl_print_type(f
, t
->fields
.array
);
3065 fprintf(f
, " %u)", t
->length
);
3066 } else if (t
->is_struct() && !is_gl_identifier(t
->name
)) {
3067 fprintf(f
, "%s@%p", t
->name
, (void *) t
);
3069 fprintf(f
, "%s", t
->name
);