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
), packed(0),
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), packed(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
), packed(0),
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), packed(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), packed(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");
465 const glsl_type
*glsl_type::get_float16_type() const
467 assert(this->base_type
== GLSL_TYPE_FLOAT
);
469 return get_instance(GLSL_TYPE_FLOAT16
,
470 this->vector_elements
,
471 this->matrix_columns
,
472 this->explicit_stride
,
473 this->interface_row_major
);
476 const glsl_type
*glsl_type::get_int16_type() const
478 assert(this->base_type
== GLSL_TYPE_INT
);
480 return get_instance(GLSL_TYPE_INT16
,
481 this->vector_elements
,
482 this->matrix_columns
,
483 this->explicit_stride
,
484 this->interface_row_major
);
487 const glsl_type
*glsl_type::get_uint16_type() const
489 assert(this->base_type
== GLSL_TYPE_UINT
);
491 return get_instance(GLSL_TYPE_UINT16
,
492 this->vector_elements
,
493 this->matrix_columns
,
494 this->explicit_stride
,
495 this->interface_row_major
);
499 hash_free_type_function(struct hash_entry
*entry
)
501 glsl_type
*type
= (glsl_type
*) entry
->data
;
503 if (type
->is_array())
504 free((void*)entry
->key
);
510 glsl_type_singleton_init_or_ref()
512 mtx_lock(&glsl_type::hash_mutex
);
514 mtx_unlock(&glsl_type::hash_mutex
);
518 glsl_type_singleton_decref()
520 mtx_lock(&glsl_type::hash_mutex
);
521 assert(glsl_type_users
> 0);
523 /* Do not release glsl_types if they are still used. */
524 if (--glsl_type_users
) {
525 mtx_unlock(&glsl_type::hash_mutex
);
529 if (glsl_type::explicit_matrix_types
!= NULL
) {
530 _mesa_hash_table_destroy(glsl_type::explicit_matrix_types
,
531 hash_free_type_function
);
532 glsl_type::explicit_matrix_types
= NULL
;
535 if (glsl_type::array_types
!= NULL
) {
536 _mesa_hash_table_destroy(glsl_type::array_types
, hash_free_type_function
);
537 glsl_type::array_types
= NULL
;
540 if (glsl_type::struct_types
!= NULL
) {
541 _mesa_hash_table_destroy(glsl_type::struct_types
, hash_free_type_function
);
542 glsl_type::struct_types
= NULL
;
545 if (glsl_type::interface_types
!= NULL
) {
546 _mesa_hash_table_destroy(glsl_type::interface_types
, hash_free_type_function
);
547 glsl_type::interface_types
= NULL
;
550 if (glsl_type::function_types
!= NULL
) {
551 _mesa_hash_table_destroy(glsl_type::function_types
, hash_free_type_function
);
552 glsl_type::function_types
= NULL
;
555 if (glsl_type::subroutine_types
!= NULL
) {
556 _mesa_hash_table_destroy(glsl_type::subroutine_types
, hash_free_type_function
);
557 glsl_type::subroutine_types
= NULL
;
560 mtx_unlock(&glsl_type::hash_mutex
);
564 glsl_type::glsl_type(const glsl_type
*array
, unsigned length
,
565 unsigned explicit_stride
) :
566 base_type(GLSL_TYPE_ARRAY
), sampled_type(GLSL_TYPE_VOID
),
567 sampler_dimensionality(0), sampler_shadow(0), sampler_array(0),
568 interface_packing(0), interface_row_major(0), packed(0),
569 vector_elements(0), matrix_columns(0),
570 length(length
), name(NULL
), explicit_stride(explicit_stride
)
572 this->fields
.array
= array
;
573 /* Inherit the gl type of the base. The GL type is used for
574 * uniform/statevar handling in Mesa and the arrayness of the type
575 * is represented by the size rather than the type.
577 this->gl_type
= array
->gl_type
;
579 /* Allow a maximum of 10 characters for the array size. This is enough
580 * for 32-bits of ~0. The extra 3 are for the '[', ']', and terminating
583 const unsigned name_length
= strlen(array
->name
) + 10 + 3;
585 this->mem_ctx
= ralloc_context(NULL
);
586 assert(this->mem_ctx
!= NULL
);
588 char *const n
= (char *) ralloc_size(this->mem_ctx
, name_length
);
591 snprintf(n
, name_length
, "%s[]", array
->name
);
593 /* insert outermost dimensions in the correct spot
594 * otherwise the dimension order will be backwards
596 const char *pos
= strchr(array
->name
, '[');
598 int idx
= pos
- array
->name
;
599 snprintf(n
, idx
+1, "%s", array
->name
);
600 snprintf(n
+ idx
, name_length
- idx
, "[%u]%s",
601 length
, array
->name
+ idx
);
603 snprintf(n
, name_length
, "%s[%u]", array
->name
, length
);
611 glsl_type::vec(unsigned components
, const glsl_type
*const ts
[])
613 unsigned n
= components
;
617 else if (components
== 16)
626 #define VECN(components, sname, vname) \
628 glsl_type:: vname (unsigned components) \
630 static const glsl_type *const ts[] = { \
631 sname ## _type, vname ## 2_type, \
632 vname ## 3_type, vname ## 4_type, \
633 vname ## 8_type, vname ## 16_type, \
635 return glsl_type::vec(components, ts); \
638 VECN(components
, float, vec
)
639 VECN(components
, float16_t
, f16vec
)
640 VECN(components
, double, dvec
)
641 VECN(components
, int, ivec
)
642 VECN(components
, uint
, uvec
)
643 VECN(components
, bool, bvec
)
644 VECN(components
, int64_t, i64vec
)
645 VECN(components
, uint64_t, u64vec
)
646 VECN(components
, int16_t, i16vec
)
647 VECN(components
, uint16_t, u16vec
)
648 VECN(components
, int8_t, i8vec
)
649 VECN(components
, uint8_t, u8vec
)
652 glsl_type::get_instance(unsigned base_type
, unsigned rows
, unsigned columns
,
653 unsigned explicit_stride
, bool row_major
)
655 if (base_type
== GLSL_TYPE_VOID
) {
656 assert(explicit_stride
== 0 && !row_major
);
660 /* Matrix and vector types with explicit strides have to be looked up in a
661 * table so they're handled separately.
663 if (explicit_stride
> 0) {
664 const glsl_type
*bare_type
= get_instance(base_type
, rows
, columns
);
666 assert(columns
> 1 || !row_major
);
669 snprintf(name
, sizeof(name
), "%sx%uB%s", bare_type
->name
,
670 explicit_stride
, row_major
? "RM" : "");
672 mtx_lock(&glsl_type::hash_mutex
);
673 assert(glsl_type_users
> 0);
675 if (explicit_matrix_types
== NULL
) {
676 explicit_matrix_types
=
677 _mesa_hash_table_create(NULL
, _mesa_hash_string
,
678 _mesa_key_string_equal
);
681 const struct hash_entry
*entry
=
682 _mesa_hash_table_search(explicit_matrix_types
, name
);
684 const glsl_type
*t
= new glsl_type(bare_type
->gl_type
,
685 (glsl_base_type
)base_type
,
687 explicit_stride
, row_major
);
689 entry
= _mesa_hash_table_insert(explicit_matrix_types
,
693 assert(((glsl_type
*) entry
->data
)->base_type
== base_type
);
694 assert(((glsl_type
*) entry
->data
)->vector_elements
== rows
);
695 assert(((glsl_type
*) entry
->data
)->matrix_columns
== columns
);
696 assert(((glsl_type
*) entry
->data
)->explicit_stride
== explicit_stride
);
698 const glsl_type
*t
= (const glsl_type
*) entry
->data
;
700 mtx_unlock(&glsl_type::hash_mutex
);
707 /* Treat GLSL vectors as Nx1 matrices.
715 case GLSL_TYPE_FLOAT
:
717 case GLSL_TYPE_FLOAT16
:
719 case GLSL_TYPE_DOUBLE
:
723 case GLSL_TYPE_UINT64
:
725 case GLSL_TYPE_INT64
:
727 case GLSL_TYPE_UINT16
:
729 case GLSL_TYPE_INT16
:
731 case GLSL_TYPE_UINT8
:
739 if ((base_type
!= GLSL_TYPE_FLOAT
&&
740 base_type
!= GLSL_TYPE_DOUBLE
&&
741 base_type
!= GLSL_TYPE_FLOAT16
) || (rows
== 1))
744 /* GLSL matrix types are named mat{COLUMNS}x{ROWS}. Only the following
745 * combinations are valid:
753 #define IDX(c,r) (((c-1)*3) + (r-1))
756 case GLSL_TYPE_DOUBLE
: {
757 switch (IDX(columns
, rows
)) {
758 case IDX(2,2): return dmat2_type
;
759 case IDX(2,3): return dmat2x3_type
;
760 case IDX(2,4): return dmat2x4_type
;
761 case IDX(3,2): return dmat3x2_type
;
762 case IDX(3,3): return dmat3_type
;
763 case IDX(3,4): return dmat3x4_type
;
764 case IDX(4,2): return dmat4x2_type
;
765 case IDX(4,3): return dmat4x3_type
;
766 case IDX(4,4): return dmat4_type
;
767 default: return error_type
;
770 case GLSL_TYPE_FLOAT
: {
771 switch (IDX(columns
, rows
)) {
772 case IDX(2,2): return mat2_type
;
773 case IDX(2,3): return mat2x3_type
;
774 case IDX(2,4): return mat2x4_type
;
775 case IDX(3,2): return mat3x2_type
;
776 case IDX(3,3): return mat3_type
;
777 case IDX(3,4): return mat3x4_type
;
778 case IDX(4,2): return mat4x2_type
;
779 case IDX(4,3): return mat4x3_type
;
780 case IDX(4,4): return mat4_type
;
781 default: return error_type
;
784 case GLSL_TYPE_FLOAT16
: {
785 switch (IDX(columns
, rows
)) {
786 case IDX(2,2): return f16mat2_type
;
787 case IDX(2,3): return f16mat2x3_type
;
788 case IDX(2,4): return f16mat2x4_type
;
789 case IDX(3,2): return f16mat3x2_type
;
790 case IDX(3,3): return f16mat3_type
;
791 case IDX(3,4): return f16mat3x4_type
;
792 case IDX(4,2): return f16mat4x2_type
;
793 case IDX(4,3): return f16mat4x3_type
;
794 case IDX(4,4): return f16mat4_type
;
795 default: return error_type
;
798 default: return error_type
;
802 assert(!"Should not get here.");
807 glsl_type::get_sampler_instance(enum glsl_sampler_dim dim
,
813 case GLSL_TYPE_FLOAT
:
815 case GLSL_SAMPLER_DIM_1D
:
817 return (array
? sampler1DArrayShadow_type
: sampler1DShadow_type
);
819 return (array
? sampler1DArray_type
: sampler1D_type
);
820 case GLSL_SAMPLER_DIM_2D
:
822 return (array
? sampler2DArrayShadow_type
: sampler2DShadow_type
);
824 return (array
? sampler2DArray_type
: sampler2D_type
);
825 case GLSL_SAMPLER_DIM_3D
:
829 return sampler3D_type
;
830 case GLSL_SAMPLER_DIM_CUBE
:
832 return (array
? samplerCubeArrayShadow_type
: samplerCubeShadow_type
);
834 return (array
? samplerCubeArray_type
: samplerCube_type
);
835 case GLSL_SAMPLER_DIM_RECT
:
839 return sampler2DRectShadow_type
;
841 return sampler2DRect_type
;
842 case GLSL_SAMPLER_DIM_BUF
:
846 return samplerBuffer_type
;
847 case GLSL_SAMPLER_DIM_MS
:
850 return (array
? sampler2DMSArray_type
: sampler2DMS_type
);
851 case GLSL_SAMPLER_DIM_EXTERNAL
:
855 return samplerExternalOES_type
;
856 case GLSL_SAMPLER_DIM_SUBPASS
:
857 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
864 case GLSL_SAMPLER_DIM_1D
:
865 return (array
? isampler1DArray_type
: isampler1D_type
);
866 case GLSL_SAMPLER_DIM_2D
:
867 return (array
? isampler2DArray_type
: isampler2D_type
);
868 case GLSL_SAMPLER_DIM_3D
:
871 return isampler3D_type
;
872 case GLSL_SAMPLER_DIM_CUBE
:
873 return (array
? isamplerCubeArray_type
: isamplerCube_type
);
874 case GLSL_SAMPLER_DIM_RECT
:
877 return isampler2DRect_type
;
878 case GLSL_SAMPLER_DIM_BUF
:
881 return isamplerBuffer_type
;
882 case GLSL_SAMPLER_DIM_MS
:
883 return (array
? isampler2DMSArray_type
: isampler2DMS_type
);
884 case GLSL_SAMPLER_DIM_EXTERNAL
:
886 case GLSL_SAMPLER_DIM_SUBPASS
:
887 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
894 case GLSL_SAMPLER_DIM_1D
:
895 return (array
? usampler1DArray_type
: usampler1D_type
);
896 case GLSL_SAMPLER_DIM_2D
:
897 return (array
? usampler2DArray_type
: usampler2D_type
);
898 case GLSL_SAMPLER_DIM_3D
:
901 return usampler3D_type
;
902 case GLSL_SAMPLER_DIM_CUBE
:
903 return (array
? usamplerCubeArray_type
: usamplerCube_type
);
904 case GLSL_SAMPLER_DIM_RECT
:
907 return usampler2DRect_type
;
908 case GLSL_SAMPLER_DIM_BUF
:
911 return usamplerBuffer_type
;
912 case GLSL_SAMPLER_DIM_MS
:
913 return (array
? usampler2DMSArray_type
: usampler2DMS_type
);
914 case GLSL_SAMPLER_DIM_EXTERNAL
:
916 case GLSL_SAMPLER_DIM_SUBPASS
:
917 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
921 return shadow
? samplerShadow_type
: sampler_type
;
926 unreachable("switch statement above should be complete");
930 glsl_type::get_image_instance(enum glsl_sampler_dim dim
,
931 bool array
, glsl_base_type type
)
934 case GLSL_TYPE_FLOAT
:
936 case GLSL_SAMPLER_DIM_1D
:
937 return (array
? image1DArray_type
: image1D_type
);
938 case GLSL_SAMPLER_DIM_2D
:
939 return (array
? image2DArray_type
: image2D_type
);
940 case GLSL_SAMPLER_DIM_3D
:
942 case GLSL_SAMPLER_DIM_CUBE
:
943 return (array
? imageCubeArray_type
: imageCube_type
);
944 case GLSL_SAMPLER_DIM_RECT
:
948 return image2DRect_type
;
949 case GLSL_SAMPLER_DIM_BUF
:
953 return imageBuffer_type
;
954 case GLSL_SAMPLER_DIM_MS
:
955 return (array
? image2DMSArray_type
: image2DMS_type
);
956 case GLSL_SAMPLER_DIM_SUBPASS
:
957 return subpassInput_type
;
958 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
959 return subpassInputMS_type
;
960 case GLSL_SAMPLER_DIM_EXTERNAL
:
965 case GLSL_SAMPLER_DIM_1D
:
966 return (array
? iimage1DArray_type
: iimage1D_type
);
967 case GLSL_SAMPLER_DIM_2D
:
968 return (array
? iimage2DArray_type
: iimage2D_type
);
969 case GLSL_SAMPLER_DIM_3D
:
972 return iimage3D_type
;
973 case GLSL_SAMPLER_DIM_CUBE
:
974 return (array
? iimageCubeArray_type
: iimageCube_type
);
975 case GLSL_SAMPLER_DIM_RECT
:
978 return iimage2DRect_type
;
979 case GLSL_SAMPLER_DIM_BUF
:
982 return iimageBuffer_type
;
983 case GLSL_SAMPLER_DIM_MS
:
984 return (array
? iimage2DMSArray_type
: iimage2DMS_type
);
985 case GLSL_SAMPLER_DIM_SUBPASS
:
986 return isubpassInput_type
;
987 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
988 return isubpassInputMS_type
;
989 case GLSL_SAMPLER_DIM_EXTERNAL
:
994 case GLSL_SAMPLER_DIM_1D
:
995 return (array
? uimage1DArray_type
: uimage1D_type
);
996 case GLSL_SAMPLER_DIM_2D
:
997 return (array
? uimage2DArray_type
: uimage2D_type
);
998 case GLSL_SAMPLER_DIM_3D
:
1001 return uimage3D_type
;
1002 case GLSL_SAMPLER_DIM_CUBE
:
1003 return (array
? uimageCubeArray_type
: uimageCube_type
);
1004 case GLSL_SAMPLER_DIM_RECT
:
1007 return uimage2DRect_type
;
1008 case GLSL_SAMPLER_DIM_BUF
:
1011 return uimageBuffer_type
;
1012 case GLSL_SAMPLER_DIM_MS
:
1013 return (array
? uimage2DMSArray_type
: uimage2DMS_type
);
1014 case GLSL_SAMPLER_DIM_SUBPASS
:
1015 return usubpassInput_type
;
1016 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
1017 return usubpassInputMS_type
;
1018 case GLSL_SAMPLER_DIM_EXTERNAL
:
1021 case GLSL_TYPE_VOID
:
1023 case GLSL_SAMPLER_DIM_1D
:
1024 return (array
? vimage1DArray_type
: vimage1D_type
);
1025 case GLSL_SAMPLER_DIM_2D
:
1026 return (array
? vimage2DArray_type
: vimage2D_type
);
1027 case GLSL_SAMPLER_DIM_3D
:
1028 return (array
? error_type
: vimage3D_type
);
1029 case GLSL_SAMPLER_DIM_BUF
:
1030 return (array
? error_type
: vbuffer_type
);
1038 unreachable("switch statement above should be complete");
1042 glsl_type::get_array_instance(const glsl_type
*base
,
1043 unsigned array_size
,
1044 unsigned explicit_stride
)
1046 /* Generate a name using the base type pointer in the key. This is
1047 * done because the name of the base type may not be unique across
1048 * shaders. For example, two shaders may have different record types
1052 snprintf(key
, sizeof(key
), "%p[%u]x%uB", (void *) base
, array_size
,
1055 mtx_lock(&glsl_type::hash_mutex
);
1056 assert(glsl_type_users
> 0);
1058 if (array_types
== NULL
) {
1059 array_types
= _mesa_hash_table_create(NULL
, _mesa_hash_string
,
1060 _mesa_key_string_equal
);
1063 const struct hash_entry
*entry
= _mesa_hash_table_search(array_types
, key
);
1064 if (entry
== NULL
) {
1065 const glsl_type
*t
= new glsl_type(base
, array_size
, explicit_stride
);
1067 entry
= _mesa_hash_table_insert(array_types
,
1072 assert(((glsl_type
*) entry
->data
)->base_type
== GLSL_TYPE_ARRAY
);
1073 assert(((glsl_type
*) entry
->data
)->length
== array_size
);
1074 assert(((glsl_type
*) entry
->data
)->fields
.array
== base
);
1076 glsl_type
*t
= (glsl_type
*) entry
->data
;
1078 mtx_unlock(&glsl_type::hash_mutex
);
1084 glsl_type::compare_no_precision(const glsl_type
*b
) const
1089 if (this->is_array()) {
1090 if (!b
->is_array() || this->length
!= b
->length
)
1093 const glsl_type
*b_no_array
= b
->fields
.array
;
1095 return this->fields
.array
->compare_no_precision(b_no_array
);
1098 if (this->is_struct()) {
1099 if (!b
->is_struct())
1101 } else if (this->is_interface()) {
1102 if (!b
->is_interface())
1108 return record_compare(b
,
1109 true, /* match_name */
1110 true, /* match_locations */
1111 false /* match_precision */);
1115 glsl_type::record_compare(const glsl_type
*b
, bool match_name
,
1116 bool match_locations
, bool match_precision
) const
1118 if (this->length
!= b
->length
)
1121 if (this->interface_packing
!= b
->interface_packing
)
1124 if (this->interface_row_major
!= b
->interface_row_major
)
1127 /* From the GLSL 4.20 specification (Sec 4.2):
1129 * "Structures must have the same name, sequence of type names, and
1130 * type definitions, and field names to be considered the same type."
1132 * GLSL ES behaves the same (Ver 1.00 Sec 4.2.4, Ver 3.00 Sec 4.2.5).
1134 * Section 7.4.1 (Shader Interface Matching) of the OpenGL 4.30 spec says:
1136 * "Variables or block members declared as structures are considered
1137 * to match in type if and only if structure members match in name,
1138 * type, qualification, and declaration order."
1141 if (strcmp(this->name
, b
->name
) != 0)
1144 for (unsigned i
= 0; i
< this->length
; i
++) {
1145 if (match_precision
) {
1146 if (this->fields
.structure
[i
].type
!= b
->fields
.structure
[i
].type
)
1149 const glsl_type
*ta
= this->fields
.structure
[i
].type
;
1150 const glsl_type
*tb
= b
->fields
.structure
[i
].type
;
1151 if (!ta
->compare_no_precision(tb
))
1154 if (strcmp(this->fields
.structure
[i
].name
,
1155 b
->fields
.structure
[i
].name
) != 0)
1157 if (this->fields
.structure
[i
].matrix_layout
1158 != b
->fields
.structure
[i
].matrix_layout
)
1160 if (match_locations
&& this->fields
.structure
[i
].location
1161 != b
->fields
.structure
[i
].location
)
1163 if (this->fields
.structure
[i
].offset
1164 != b
->fields
.structure
[i
].offset
)
1166 if (this->fields
.structure
[i
].interpolation
1167 != b
->fields
.structure
[i
].interpolation
)
1169 if (this->fields
.structure
[i
].centroid
1170 != b
->fields
.structure
[i
].centroid
)
1172 if (this->fields
.structure
[i
].sample
1173 != b
->fields
.structure
[i
].sample
)
1175 if (this->fields
.structure
[i
].patch
1176 != b
->fields
.structure
[i
].patch
)
1178 if (this->fields
.structure
[i
].memory_read_only
1179 != b
->fields
.structure
[i
].memory_read_only
)
1181 if (this->fields
.structure
[i
].memory_write_only
1182 != b
->fields
.structure
[i
].memory_write_only
)
1184 if (this->fields
.structure
[i
].memory_coherent
1185 != b
->fields
.structure
[i
].memory_coherent
)
1187 if (this->fields
.structure
[i
].memory_volatile
1188 != b
->fields
.structure
[i
].memory_volatile
)
1190 if (this->fields
.structure
[i
].memory_restrict
1191 != b
->fields
.structure
[i
].memory_restrict
)
1193 if (this->fields
.structure
[i
].image_format
1194 != b
->fields
.structure
[i
].image_format
)
1196 if (match_precision
&&
1197 this->fields
.structure
[i
].precision
1198 != b
->fields
.structure
[i
].precision
)
1200 if (this->fields
.structure
[i
].explicit_xfb_buffer
1201 != b
->fields
.structure
[i
].explicit_xfb_buffer
)
1203 if (this->fields
.structure
[i
].xfb_buffer
1204 != b
->fields
.structure
[i
].xfb_buffer
)
1206 if (this->fields
.structure
[i
].xfb_stride
1207 != b
->fields
.structure
[i
].xfb_stride
)
1216 glsl_type::record_key_compare(const void *a
, const void *b
)
1218 const glsl_type
*const key1
= (glsl_type
*) a
;
1219 const glsl_type
*const key2
= (glsl_type
*) b
;
1221 return strcmp(key1
->name
, key2
->name
) == 0 &&
1222 key1
->record_compare(key2
, true);
1227 * Generate an integer hash value for a glsl_type structure type.
1230 glsl_type::record_key_hash(const void *a
)
1232 const glsl_type
*const key
= (glsl_type
*) a
;
1233 uintptr_t hash
= key
->length
;
1236 for (unsigned i
= 0; i
< key
->length
; i
++) {
1237 /* casting pointer to uintptr_t */
1238 hash
= (hash
* 13 ) + (uintptr_t) key
->fields
.structure
[i
].type
;
1241 if (sizeof(hash
) == 8)
1242 retval
= (hash
& 0xffffffff) ^ ((uint64_t) hash
>> 32);
1251 glsl_type::get_struct_instance(const glsl_struct_field
*fields
,
1252 unsigned num_fields
,
1256 const glsl_type
key(fields
, num_fields
, name
, packed
);
1258 mtx_lock(&glsl_type::hash_mutex
);
1259 assert(glsl_type_users
> 0);
1261 if (struct_types
== NULL
) {
1262 struct_types
= _mesa_hash_table_create(NULL
, record_key_hash
,
1263 record_key_compare
);
1266 const struct hash_entry
*entry
= _mesa_hash_table_search(struct_types
,
1268 if (entry
== NULL
) {
1269 const glsl_type
*t
= new glsl_type(fields
, num_fields
, name
, packed
);
1271 entry
= _mesa_hash_table_insert(struct_types
, t
, (void *) t
);
1274 assert(((glsl_type
*) entry
->data
)->base_type
== GLSL_TYPE_STRUCT
);
1275 assert(((glsl_type
*) entry
->data
)->length
== num_fields
);
1276 assert(strcmp(((glsl_type
*) entry
->data
)->name
, name
) == 0);
1277 assert(((glsl_type
*) entry
->data
)->packed
== packed
);
1279 glsl_type
*t
= (glsl_type
*) entry
->data
;
1281 mtx_unlock(&glsl_type::hash_mutex
);
1288 glsl_type::get_interface_instance(const glsl_struct_field
*fields
,
1289 unsigned num_fields
,
1290 enum glsl_interface_packing packing
,
1292 const char *block_name
)
1294 const glsl_type
key(fields
, num_fields
, packing
, row_major
, block_name
);
1296 mtx_lock(&glsl_type::hash_mutex
);
1297 assert(glsl_type_users
> 0);
1299 if (interface_types
== NULL
) {
1300 interface_types
= _mesa_hash_table_create(NULL
, record_key_hash
,
1301 record_key_compare
);
1304 const struct hash_entry
*entry
= _mesa_hash_table_search(interface_types
,
1306 if (entry
== NULL
) {
1307 const glsl_type
*t
= new glsl_type(fields
, num_fields
,
1308 packing
, row_major
, block_name
);
1310 entry
= _mesa_hash_table_insert(interface_types
, t
, (void *) t
);
1313 assert(((glsl_type
*) entry
->data
)->base_type
== GLSL_TYPE_INTERFACE
);
1314 assert(((glsl_type
*) entry
->data
)->length
== num_fields
);
1315 assert(strcmp(((glsl_type
*) entry
->data
)->name
, block_name
) == 0);
1317 glsl_type
*t
= (glsl_type
*) entry
->data
;
1319 mtx_unlock(&glsl_type::hash_mutex
);
1325 glsl_type::get_subroutine_instance(const char *subroutine_name
)
1327 const glsl_type
key(subroutine_name
);
1329 mtx_lock(&glsl_type::hash_mutex
);
1330 assert(glsl_type_users
> 0);
1332 if (subroutine_types
== NULL
) {
1333 subroutine_types
= _mesa_hash_table_create(NULL
, record_key_hash
,
1334 record_key_compare
);
1337 const struct hash_entry
*entry
= _mesa_hash_table_search(subroutine_types
,
1339 if (entry
== NULL
) {
1340 const glsl_type
*t
= new glsl_type(subroutine_name
);
1342 entry
= _mesa_hash_table_insert(subroutine_types
, t
, (void *) t
);
1345 assert(((glsl_type
*) entry
->data
)->base_type
== GLSL_TYPE_SUBROUTINE
);
1346 assert(strcmp(((glsl_type
*) entry
->data
)->name
, subroutine_name
) == 0);
1348 glsl_type
*t
= (glsl_type
*) entry
->data
;
1350 mtx_unlock(&glsl_type::hash_mutex
);
1357 function_key_compare(const void *a
, const void *b
)
1359 const glsl_type
*const key1
= (glsl_type
*) a
;
1360 const glsl_type
*const key2
= (glsl_type
*) b
;
1362 if (key1
->length
!= key2
->length
)
1365 return memcmp(key1
->fields
.parameters
, key2
->fields
.parameters
,
1366 (key1
->length
+ 1) * sizeof(*key1
->fields
.parameters
)) == 0;
1371 function_key_hash(const void *a
)
1373 const glsl_type
*const key
= (glsl_type
*) a
;
1374 return _mesa_hash_data(key
->fields
.parameters
,
1375 (key
->length
+ 1) * sizeof(*key
->fields
.parameters
));
1379 glsl_type::get_function_instance(const glsl_type
*return_type
,
1380 const glsl_function_param
*params
,
1381 unsigned num_params
)
1383 const glsl_type
key(return_type
, params
, num_params
);
1385 mtx_lock(&glsl_type::hash_mutex
);
1386 assert(glsl_type_users
> 0);
1388 if (function_types
== NULL
) {
1389 function_types
= _mesa_hash_table_create(NULL
, function_key_hash
,
1390 function_key_compare
);
1393 struct hash_entry
*entry
= _mesa_hash_table_search(function_types
, &key
);
1394 if (entry
== NULL
) {
1395 const glsl_type
*t
= new glsl_type(return_type
, params
, num_params
);
1397 entry
= _mesa_hash_table_insert(function_types
, t
, (void *) t
);
1400 const glsl_type
*t
= (const glsl_type
*)entry
->data
;
1402 assert(t
->base_type
== GLSL_TYPE_FUNCTION
);
1403 assert(t
->length
== num_params
);
1405 mtx_unlock(&glsl_type::hash_mutex
);
1412 glsl_type::get_mul_type(const glsl_type
*type_a
, const glsl_type
*type_b
)
1414 if (type_a
->is_matrix() && type_b
->is_matrix()) {
1415 /* Matrix multiply. The columns of A must match the rows of B. Given
1416 * the other previously tested constraints, this means the vector type
1417 * of a row from A must be the same as the vector type of a column from
1420 if (type_a
->row_type() == type_b
->column_type()) {
1421 /* The resulting matrix has the number of columns of matrix B and
1422 * the number of rows of matrix A. We get the row count of A by
1423 * looking at the size of a vector that makes up a column. The
1424 * transpose (size of a row) is done for B.
1426 const glsl_type
*const type
=
1427 get_instance(type_a
->base_type
,
1428 type_a
->column_type()->vector_elements
,
1429 type_b
->row_type()->vector_elements
);
1430 assert(type
!= error_type
);
1434 } else if (type_a
== type_b
) {
1436 } else if (type_a
->is_matrix()) {
1437 /* A is a matrix and B is a column vector. Columns of A must match
1438 * rows of B. Given the other previously tested constraints, this
1439 * means the vector type of a row from A must be the same as the
1440 * vector the type of B.
1442 if (type_a
->row_type() == type_b
) {
1443 /* The resulting vector has a number of elements equal to
1444 * the number of rows of matrix A. */
1445 const glsl_type
*const type
=
1446 get_instance(type_a
->base_type
,
1447 type_a
->column_type()->vector_elements
,
1449 assert(type
!= error_type
);
1454 assert(type_b
->is_matrix());
1456 /* A is a row vector and B is a matrix. Columns of A must match rows
1457 * of B. Given the other previously tested constraints, this means
1458 * the type of A must be the same as the vector type of a column from
1461 if (type_a
== type_b
->column_type()) {
1462 /* The resulting vector has a number of elements equal to
1463 * the number of columns of matrix B. */
1464 const glsl_type
*const type
=
1465 get_instance(type_a
->base_type
,
1466 type_b
->row_type()->vector_elements
,
1468 assert(type
!= error_type
);
1479 glsl_type::field_type(const char *name
) const
1481 if (this->base_type
!= GLSL_TYPE_STRUCT
1482 && this->base_type
!= GLSL_TYPE_INTERFACE
)
1485 for (unsigned i
= 0; i
< this->length
; i
++) {
1486 if (strcmp(name
, this->fields
.structure
[i
].name
) == 0)
1487 return this->fields
.structure
[i
].type
;
1495 glsl_type::field_index(const char *name
) const
1497 if (this->base_type
!= GLSL_TYPE_STRUCT
1498 && this->base_type
!= GLSL_TYPE_INTERFACE
)
1501 for (unsigned i
= 0; i
< this->length
; i
++) {
1502 if (strcmp(name
, this->fields
.structure
[i
].name
) == 0)
1511 glsl_type::component_slots() const
1513 switch (this->base_type
) {
1514 case GLSL_TYPE_UINT
:
1516 case GLSL_TYPE_UINT8
:
1517 case GLSL_TYPE_INT8
:
1518 case GLSL_TYPE_UINT16
:
1519 case GLSL_TYPE_INT16
:
1520 case GLSL_TYPE_FLOAT
:
1521 case GLSL_TYPE_FLOAT16
:
1522 case GLSL_TYPE_BOOL
:
1523 return this->components();
1525 case GLSL_TYPE_DOUBLE
:
1526 case GLSL_TYPE_UINT64
:
1527 case GLSL_TYPE_INT64
:
1528 return 2 * this->components();
1530 case GLSL_TYPE_STRUCT
:
1531 case GLSL_TYPE_INTERFACE
: {
1534 for (unsigned i
= 0; i
< this->length
; i
++)
1535 size
+= this->fields
.structure
[i
].type
->component_slots();
1540 case GLSL_TYPE_ARRAY
:
1541 return this->length
* this->fields
.array
->component_slots();
1543 case GLSL_TYPE_SAMPLER
:
1544 case GLSL_TYPE_IMAGE
:
1547 case GLSL_TYPE_SUBROUTINE
:
1550 case GLSL_TYPE_FUNCTION
:
1551 case GLSL_TYPE_ATOMIC_UINT
:
1552 case GLSL_TYPE_VOID
:
1553 case GLSL_TYPE_ERROR
:
1561 glsl_type::struct_location_offset(unsigned length
) const
1563 unsigned offset
= 0;
1564 const glsl_type
*t
= this->without_array();
1565 if (t
->is_struct()) {
1566 assert(length
<= t
->length
);
1568 for (unsigned i
= 0; i
< length
; i
++) {
1569 const glsl_type
*st
= t
->fields
.structure
[i
].type
;
1570 const glsl_type
*wa
= st
->without_array();
1571 if (wa
->is_struct()) {
1572 unsigned r_offset
= wa
->struct_location_offset(wa
->length
);
1573 offset
+= st
->is_array() ?
1574 st
->arrays_of_arrays_size() * r_offset
: r_offset
;
1575 } else if (st
->is_array() && st
->fields
.array
->is_array()) {
1576 unsigned outer_array_size
= st
->length
;
1577 const glsl_type
*base_type
= st
->fields
.array
;
1579 /* For arrays of arrays the outer arrays take up a uniform
1580 * slot for each element. The innermost array elements share a
1581 * single slot so we ignore the innermost array when calculating
1584 while (base_type
->fields
.array
->is_array()) {
1585 outer_array_size
= outer_array_size
* base_type
->length
;
1586 base_type
= base_type
->fields
.array
;
1588 offset
+= outer_array_size
;
1590 /* We dont worry about arrays here because unless the array
1591 * contains a structure or another array it only takes up a single
1602 glsl_type::uniform_locations() const
1606 switch (this->base_type
) {
1607 case GLSL_TYPE_UINT
:
1609 case GLSL_TYPE_FLOAT
:
1610 case GLSL_TYPE_FLOAT16
:
1611 case GLSL_TYPE_DOUBLE
:
1612 case GLSL_TYPE_UINT16
:
1613 case GLSL_TYPE_UINT8
:
1614 case GLSL_TYPE_INT16
:
1615 case GLSL_TYPE_INT8
:
1616 case GLSL_TYPE_UINT64
:
1617 case GLSL_TYPE_INT64
:
1618 case GLSL_TYPE_BOOL
:
1619 case GLSL_TYPE_SAMPLER
:
1620 case GLSL_TYPE_IMAGE
:
1621 case GLSL_TYPE_SUBROUTINE
:
1624 case GLSL_TYPE_STRUCT
:
1625 case GLSL_TYPE_INTERFACE
:
1626 for (unsigned i
= 0; i
< this->length
; i
++)
1627 size
+= this->fields
.structure
[i
].type
->uniform_locations();
1629 case GLSL_TYPE_ARRAY
:
1630 return this->length
* this->fields
.array
->uniform_locations();
1637 glsl_type::varying_count() const
1641 switch (this->base_type
) {
1642 case GLSL_TYPE_UINT
:
1644 case GLSL_TYPE_FLOAT
:
1645 case GLSL_TYPE_FLOAT16
:
1646 case GLSL_TYPE_DOUBLE
:
1647 case GLSL_TYPE_BOOL
:
1648 case GLSL_TYPE_UINT16
:
1649 case GLSL_TYPE_UINT8
:
1650 case GLSL_TYPE_INT16
:
1651 case GLSL_TYPE_INT8
:
1652 case GLSL_TYPE_UINT64
:
1653 case GLSL_TYPE_INT64
:
1656 case GLSL_TYPE_STRUCT
:
1657 case GLSL_TYPE_INTERFACE
:
1658 for (unsigned i
= 0; i
< this->length
; i
++)
1659 size
+= this->fields
.structure
[i
].type
->varying_count();
1661 case GLSL_TYPE_ARRAY
:
1662 /* Don't count innermost array elements */
1663 if (this->without_array()->is_struct() ||
1664 this->without_array()->is_interface() ||
1665 this->fields
.array
->is_array())
1666 return this->length
* this->fields
.array
->varying_count();
1668 return this->fields
.array
->varying_count();
1670 assert(!"unsupported varying type");
1676 glsl_type::can_implicitly_convert_to(const glsl_type
*desired
,
1677 _mesa_glsl_parse_state
*state
) const
1679 if (this == desired
)
1682 /* GLSL 1.10 and ESSL do not allow implicit conversions. If there is no
1683 * state, we're doing intra-stage function linking where these checks have
1684 * already been done.
1686 if (state
&& !state
->has_implicit_conversions())
1689 /* There is no conversion among matrix types. */
1690 if (this->matrix_columns
> 1 || desired
->matrix_columns
> 1)
1693 /* Vector size must match. */
1694 if (this->vector_elements
!= desired
->vector_elements
)
1697 /* int and uint can be converted to float. */
1698 if (desired
->is_float() && this->is_integer_32())
1701 /* With GLSL 4.0, ARB_gpu_shader5, or MESA_shader_integer_functions, int
1702 * can be converted to uint. Note that state may be NULL here, when
1703 * resolving function calls in the linker. By this time, all the
1704 * state-dependent checks have already happened though, so allow anything
1705 * that's allowed in any shader version.
1707 if ((!state
|| state
->has_implicit_int_to_uint_conversion()) &&
1708 desired
->base_type
== GLSL_TYPE_UINT
&& this->base_type
== GLSL_TYPE_INT
)
1711 /* No implicit conversions from double. */
1712 if ((!state
|| state
->has_double()) && this->is_double())
1715 /* Conversions from different types to double. */
1716 if ((!state
|| state
->has_double()) && desired
->is_double()) {
1717 if (this->is_float())
1719 if (this->is_integer_32())
1727 glsl_type::std140_base_alignment(bool row_major
) const
1729 unsigned N
= is_64bit() ? 8 : 4;
1731 /* (1) If the member is a scalar consuming <N> basic machine units, the
1732 * base alignment is <N>.
1734 * (2) If the member is a two- or four-component vector with components
1735 * consuming <N> basic machine units, the base alignment is 2<N> or
1736 * 4<N>, respectively.
1738 * (3) If the member is a three-component vector with components consuming
1739 * <N> basic machine units, the base alignment is 4<N>.
1741 if (this->is_scalar() || this->is_vector()) {
1742 switch (this->vector_elements
) {
1753 /* (4) If the member is an array of scalars or vectors, the base alignment
1754 * and array stride are set to match the base alignment of a single
1755 * array element, according to rules (1), (2), and (3), and rounded up
1756 * to the base alignment of a vec4. The array may have padding at the
1757 * end; the base offset of the member following the array is rounded up
1758 * to the next multiple of the base alignment.
1760 * (6) If the member is an array of <S> column-major matrices with <C>
1761 * columns and <R> rows, the matrix is stored identically to a row of
1762 * <S>*<C> column vectors with <R> components each, according to rule
1765 * (8) If the member is an array of <S> row-major matrices with <C> columns
1766 * and <R> rows, the matrix is stored identically to a row of <S>*<R>
1767 * row vectors with <C> components each, according to rule (4).
1769 * (10) If the member is an array of <S> structures, the <S> elements of
1770 * the array are laid out in order, according to rule (9).
1772 if (this->is_array()) {
1773 if (this->fields
.array
->is_scalar() ||
1774 this->fields
.array
->is_vector() ||
1775 this->fields
.array
->is_matrix()) {
1776 return MAX2(this->fields
.array
->std140_base_alignment(row_major
), 16);
1778 assert(this->fields
.array
->is_struct() ||
1779 this->fields
.array
->is_array());
1780 return this->fields
.array
->std140_base_alignment(row_major
);
1784 /* (5) If the member is a column-major matrix with <C> columns and
1785 * <R> rows, the matrix is stored identically to an array of
1786 * <C> column vectors with <R> components each, according to
1789 * (7) If the member is a row-major matrix with <C> columns and <R>
1790 * rows, the matrix is stored identically to an array of <R>
1791 * row vectors with <C> components each, according to rule (4).
1793 if (this->is_matrix()) {
1794 const struct glsl_type
*vec_type
, *array_type
;
1795 int c
= this->matrix_columns
;
1796 int r
= this->vector_elements
;
1799 vec_type
= get_instance(base_type
, c
, 1);
1800 array_type
= glsl_type::get_array_instance(vec_type
, r
);
1802 vec_type
= get_instance(base_type
, r
, 1);
1803 array_type
= glsl_type::get_array_instance(vec_type
, c
);
1806 return array_type
->std140_base_alignment(false);
1809 /* (9) If the member is a structure, the base alignment of the
1810 * structure is <N>, where <N> is the largest base alignment
1811 * value of any of its members, and rounded up to the base
1812 * alignment of a vec4. The individual members of this
1813 * sub-structure are then assigned offsets by applying this set
1814 * of rules recursively, where the base offset of the first
1815 * member of the sub-structure is equal to the aligned offset
1816 * of the structure. The structure may have padding at the end;
1817 * the base offset of the member following the sub-structure is
1818 * rounded up to the next multiple of the base alignment of the
1821 if (this->is_struct()) {
1822 unsigned base_alignment
= 16;
1823 for (unsigned i
= 0; i
< this->length
; i
++) {
1824 bool field_row_major
= row_major
;
1825 const enum glsl_matrix_layout matrix_layout
=
1826 glsl_matrix_layout(this->fields
.structure
[i
].matrix_layout
);
1827 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
1828 field_row_major
= true;
1829 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
1830 field_row_major
= false;
1833 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
1834 base_alignment
= MAX2(base_alignment
,
1835 field_type
->std140_base_alignment(field_row_major
));
1837 return base_alignment
;
1840 assert(!"not reached");
1845 glsl_type::std140_size(bool row_major
) const
1847 unsigned N
= is_64bit() ? 8 : 4;
1849 /* (1) If the member is a scalar consuming <N> basic machine units, the
1850 * base alignment is <N>.
1852 * (2) If the member is a two- or four-component vector with components
1853 * consuming <N> basic machine units, the base alignment is 2<N> or
1854 * 4<N>, respectively.
1856 * (3) If the member is a three-component vector with components consuming
1857 * <N> basic machine units, the base alignment is 4<N>.
1859 if (this->is_scalar() || this->is_vector()) {
1860 assert(this->explicit_stride
== 0);
1861 return this->vector_elements
* N
;
1864 /* (5) If the member is a column-major matrix with <C> columns and
1865 * <R> rows, the matrix is stored identically to an array of
1866 * <C> column vectors with <R> components each, according to
1869 * (6) If the member is an array of <S> column-major matrices with <C>
1870 * columns and <R> rows, the matrix is stored identically to a row of
1871 * <S>*<C> column vectors with <R> components each, according to rule
1874 * (7) If the member is a row-major matrix with <C> columns and <R>
1875 * rows, the matrix is stored identically to an array of <R>
1876 * row vectors with <C> components each, according to rule (4).
1878 * (8) If the member is an array of <S> row-major matrices with <C> columns
1879 * and <R> rows, the matrix is stored identically to a row of <S>*<R>
1880 * row vectors with <C> components each, according to rule (4).
1882 if (this->without_array()->is_matrix()) {
1883 const struct glsl_type
*element_type
;
1884 const struct glsl_type
*vec_type
;
1885 unsigned int array_len
;
1887 if (this->is_array()) {
1888 element_type
= this->without_array();
1889 array_len
= this->arrays_of_arrays_size();
1891 element_type
= this;
1896 vec_type
= get_instance(element_type
->base_type
,
1897 element_type
->matrix_columns
, 1);
1899 array_len
*= element_type
->vector_elements
;
1901 vec_type
= get_instance(element_type
->base_type
,
1902 element_type
->vector_elements
, 1);
1903 array_len
*= element_type
->matrix_columns
;
1905 const glsl_type
*array_type
= glsl_type::get_array_instance(vec_type
,
1908 return array_type
->std140_size(false);
1911 /* (4) If the member is an array of scalars or vectors, the base alignment
1912 * and array stride are set to match the base alignment of a single
1913 * array element, according to rules (1), (2), and (3), and rounded up
1914 * to the base alignment of a vec4. The array may have padding at the
1915 * end; the base offset of the member following the array is rounded up
1916 * to the next multiple of the base alignment.
1918 * (10) If the member is an array of <S> structures, the <S> elements of
1919 * the array are laid out in order, according to rule (9).
1921 if (this->is_array()) {
1923 if (this->without_array()->is_struct()) {
1924 stride
= this->without_array()->std140_size(row_major
);
1926 unsigned element_base_align
=
1927 this->without_array()->std140_base_alignment(row_major
);
1928 stride
= MAX2(element_base_align
, 16);
1931 unsigned size
= this->arrays_of_arrays_size() * stride
;
1932 assert(this->explicit_stride
== 0 ||
1933 size
== this->length
* this->explicit_stride
);
1937 /* (9) If the member is a structure, the base alignment of the
1938 * structure is <N>, where <N> is the largest base alignment
1939 * value of any of its members, and rounded up to the base
1940 * alignment of a vec4. The individual members of this
1941 * sub-structure are then assigned offsets by applying this set
1942 * of rules recursively, where the base offset of the first
1943 * member of the sub-structure is equal to the aligned offset
1944 * of the structure. The structure may have padding at the end;
1945 * the base offset of the member following the sub-structure is
1946 * rounded up to the next multiple of the base alignment of the
1949 if (this->is_struct() || this->is_interface()) {
1951 unsigned max_align
= 0;
1953 for (unsigned i
= 0; i
< this->length
; i
++) {
1954 bool field_row_major
= row_major
;
1955 const enum glsl_matrix_layout matrix_layout
=
1956 glsl_matrix_layout(this->fields
.structure
[i
].matrix_layout
);
1957 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
1958 field_row_major
= true;
1959 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
1960 field_row_major
= false;
1963 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
1964 unsigned align
= field_type
->std140_base_alignment(field_row_major
);
1966 /* Ignore unsized arrays when calculating size */
1967 if (field_type
->is_unsized_array())
1970 size
= glsl_align(size
, align
);
1971 size
+= field_type
->std140_size(field_row_major
);
1973 max_align
= MAX2(align
, max_align
);
1975 if (field_type
->is_struct() && (i
+ 1 < this->length
))
1976 size
= glsl_align(size
, 16);
1978 size
= glsl_align(size
, MAX2(max_align
, 16));
1982 assert(!"not reached");
1987 glsl_type::get_explicit_std140_type(bool row_major
) const
1989 if (this->is_vector() || this->is_scalar()) {
1991 } else if (this->is_matrix()) {
1992 const glsl_type
*vec_type
;
1994 vec_type
= get_instance(this->base_type
, this->matrix_columns
, 1);
1996 vec_type
= get_instance(this->base_type
, this->vector_elements
, 1);
1997 unsigned elem_size
= vec_type
->std140_size(false);
1998 unsigned stride
= glsl_align(elem_size
, 16);
1999 return get_instance(this->base_type
, this->vector_elements
,
2000 this->matrix_columns
, stride
, row_major
);
2001 } else if (this->is_array()) {
2002 unsigned elem_size
= this->fields
.array
->std140_size(row_major
);
2003 const glsl_type
*elem_type
=
2004 this->fields
.array
->get_explicit_std140_type(row_major
);
2005 unsigned stride
= glsl_align(elem_size
, 16);
2006 return get_array_instance(elem_type
, this->length
, stride
);
2007 } else if (this->is_struct() || this->is_interface()) {
2008 glsl_struct_field
*fields
= new glsl_struct_field
[this->length
];
2009 unsigned offset
= 0;
2010 for (unsigned i
= 0; i
< length
; i
++) {
2011 fields
[i
] = this->fields
.structure
[i
];
2013 bool field_row_major
= row_major
;
2014 if (fields
[i
].matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
2015 field_row_major
= false;
2016 } else if (fields
[i
].matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
2017 field_row_major
= true;
2020 fields
[i
].type
->get_explicit_std140_type(field_row_major
);
2022 unsigned fsize
= fields
[i
].type
->std140_size(field_row_major
);
2023 unsigned falign
= fields
[i
].type
->std140_base_alignment(field_row_major
);
2024 /* From the GLSL 460 spec section "Uniform and Shader Storage Block
2025 * Layout Qualifiers":
2027 * "The actual offset of a member is computed as follows: If
2028 * offset was declared, start with that offset, otherwise start
2029 * with the next available offset. If the resulting offset is not
2030 * a multiple of the actual alignment, increase it to the first
2031 * offset that is a multiple of the actual alignment. This results
2032 * in the actual offset the member will have."
2034 if (fields
[i
].offset
>= 0) {
2035 assert((unsigned)fields
[i
].offset
>= offset
);
2036 offset
= fields
[i
].offset
;
2038 offset
= glsl_align(offset
, falign
);
2039 fields
[i
].offset
= offset
;
2043 const glsl_type
*type
;
2044 if (this->is_struct())
2045 type
= get_struct_instance(fields
, this->length
, this->name
);
2047 type
= get_interface_instance(fields
, this->length
,
2048 (enum glsl_interface_packing
)this->interface_packing
,
2049 this->interface_row_major
,
2055 unreachable("Invalid type for UBO or SSBO");
2060 glsl_type::std430_base_alignment(bool row_major
) const
2063 unsigned N
= is_64bit() ? 8 : 4;
2065 /* (1) If the member is a scalar consuming <N> basic machine units, the
2066 * base alignment is <N>.
2068 * (2) If the member is a two- or four-component vector with components
2069 * consuming <N> basic machine units, the base alignment is 2<N> or
2070 * 4<N>, respectively.
2072 * (3) If the member is a three-component vector with components consuming
2073 * <N> basic machine units, the base alignment is 4<N>.
2075 if (this->is_scalar() || this->is_vector()) {
2076 switch (this->vector_elements
) {
2087 /* OpenGL 4.30 spec, section 7.6.2.2 "Standard Uniform Block Layout":
2089 * "When using the std430 storage layout, shader storage blocks will be
2090 * laid out in buffer storage identically to uniform and shader storage
2091 * blocks using the std140 layout, except that the base alignment and
2092 * stride of arrays of scalars and vectors in rule 4 and of structures
2093 * in rule 9 are not rounded up a multiple of the base alignment of a vec4.
2096 /* (1) If the member is a scalar consuming <N> basic machine units, the
2097 * base alignment is <N>.
2099 * (2) If the member is a two- or four-component vector with components
2100 * consuming <N> basic machine units, the base alignment is 2<N> or
2101 * 4<N>, respectively.
2103 * (3) If the member is a three-component vector with components consuming
2104 * <N> basic machine units, the base alignment is 4<N>.
2106 if (this->is_array())
2107 return this->fields
.array
->std430_base_alignment(row_major
);
2109 /* (5) If the member is a column-major matrix with <C> columns and
2110 * <R> rows, the matrix is stored identically to an array of
2111 * <C> column vectors with <R> components each, according to
2114 * (7) If the member is a row-major matrix with <C> columns and <R>
2115 * rows, the matrix is stored identically to an array of <R>
2116 * row vectors with <C> components each, according to rule (4).
2118 if (this->is_matrix()) {
2119 const struct glsl_type
*vec_type
, *array_type
;
2120 int c
= this->matrix_columns
;
2121 int r
= this->vector_elements
;
2124 vec_type
= get_instance(base_type
, c
, 1);
2125 array_type
= glsl_type::get_array_instance(vec_type
, r
);
2127 vec_type
= get_instance(base_type
, r
, 1);
2128 array_type
= glsl_type::get_array_instance(vec_type
, c
);
2131 return array_type
->std430_base_alignment(false);
2134 /* (9) If the member is a structure, the base alignment of the
2135 * structure is <N>, where <N> is the largest base alignment
2136 * value of any of its members, and rounded up to the base
2137 * alignment of a vec4. The individual members of this
2138 * sub-structure are then assigned offsets by applying this set
2139 * of rules recursively, where the base offset of the first
2140 * member of the sub-structure is equal to the aligned offset
2141 * of the structure. The structure may have padding at the end;
2142 * the base offset of the member following the sub-structure is
2143 * rounded up to the next multiple of the base alignment of the
2146 if (this->is_struct()) {
2147 unsigned base_alignment
= 0;
2148 for (unsigned i
= 0; i
< this->length
; i
++) {
2149 bool field_row_major
= row_major
;
2150 const enum glsl_matrix_layout matrix_layout
=
2151 glsl_matrix_layout(this->fields
.structure
[i
].matrix_layout
);
2152 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
2153 field_row_major
= true;
2154 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
2155 field_row_major
= false;
2158 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
2159 base_alignment
= MAX2(base_alignment
,
2160 field_type
->std430_base_alignment(field_row_major
));
2162 assert(base_alignment
> 0);
2163 return base_alignment
;
2165 assert(!"not reached");
2170 glsl_type::std430_array_stride(bool row_major
) const
2172 unsigned N
= is_64bit() ? 8 : 4;
2174 /* Notice that the array stride of a vec3 is not 3 * N but 4 * N.
2175 * See OpenGL 4.30 spec, section 7.6.2.2 "Standard Uniform Block Layout"
2177 * (3) If the member is a three-component vector with components consuming
2178 * <N> basic machine units, the base alignment is 4<N>.
2180 if (this->is_vector() && this->vector_elements
== 3)
2183 /* By default use std430_size(row_major) */
2184 unsigned stride
= this->std430_size(row_major
);
2185 assert(this->explicit_stride
== 0 || this->explicit_stride
== stride
);
2189 /* Note that the value returned by this method is only correct if the
2190 * explit offset, and stride values are set, so only with SPIR-V shaders.
2191 * Should not be used with GLSL shaders.
2195 glsl_type::explicit_size(bool align_to_stride
) const
2197 if (this->is_struct() || this->is_interface()) {
2198 if (this->length
> 0) {
2201 for (unsigned i
= 0; i
< this->length
; i
++) {
2202 assert(this->fields
.structure
[i
].offset
>= 0);
2203 unsigned last_byte
= this->fields
.structure
[i
].offset
+
2204 this->fields
.structure
[i
].type
->explicit_size();
2205 size
= MAX2(size
, last_byte
);
2212 } else if (this->is_array()) {
2213 /* From ARB_program_interface_query spec:
2215 * "For the property of BUFFER_DATA_SIZE, then the implementation-dependent
2216 * minimum total buffer object size, in basic machine units, required to
2217 * hold all active variables associated with an active uniform block, shader
2218 * storage block, or atomic counter buffer is written to <params>. If the
2219 * final member of an active shader storage block is array with no declared
2220 * size, the minimum buffer size is computed assuming the array was declared
2221 * as an array with one element."
2224 if (this->is_unsized_array())
2225 return this->explicit_stride
;
2227 assert(this->length
> 0);
2228 unsigned elem_size
= align_to_stride
? this->explicit_stride
: this->fields
.array
->explicit_size();
2229 assert(this->explicit_stride
>= elem_size
);
2231 return this->explicit_stride
* (this->length
- 1) + elem_size
;
2232 } else if (this->is_matrix()) {
2233 const struct glsl_type
*elem_type
;
2236 if (this->interface_row_major
) {
2237 elem_type
= get_instance(this->base_type
,
2238 this->matrix_columns
, 1);
2239 length
= this->vector_elements
;
2241 elem_type
= get_instance(this->base_type
,
2242 this->vector_elements
, 1);
2243 length
= this->matrix_columns
;
2246 unsigned elem_size
= align_to_stride
? this->explicit_stride
: elem_type
->explicit_size();
2248 assert(this->explicit_stride
);
2249 return this->explicit_stride
* (length
- 1) + elem_size
;
2252 unsigned N
= this->bit_size() / 8;
2254 return this->vector_elements
* N
;
2258 glsl_type::std430_size(bool row_major
) const
2260 unsigned N
= is_64bit() ? 8 : 4;
2262 /* OpenGL 4.30 spec, section 7.6.2.2 "Standard Uniform Block Layout":
2264 * "When using the std430 storage layout, shader storage blocks will be
2265 * laid out in buffer storage identically to uniform and shader storage
2266 * blocks using the std140 layout, except that the base alignment and
2267 * stride of arrays of scalars and vectors in rule 4 and of structures
2268 * in rule 9 are not rounded up a multiple of the base alignment of a vec4.
2270 if (this->is_scalar() || this->is_vector()) {
2271 assert(this->explicit_stride
== 0);
2272 return this->vector_elements
* N
;
2275 if (this->without_array()->is_matrix()) {
2276 const struct glsl_type
*element_type
;
2277 const struct glsl_type
*vec_type
;
2278 unsigned int array_len
;
2280 if (this->is_array()) {
2281 element_type
= this->without_array();
2282 array_len
= this->arrays_of_arrays_size();
2284 element_type
= this;
2289 vec_type
= get_instance(element_type
->base_type
,
2290 element_type
->matrix_columns
, 1);
2292 array_len
*= element_type
->vector_elements
;
2294 vec_type
= get_instance(element_type
->base_type
,
2295 element_type
->vector_elements
, 1);
2296 array_len
*= element_type
->matrix_columns
;
2298 const glsl_type
*array_type
= glsl_type::get_array_instance(vec_type
,
2301 return array_type
->std430_size(false);
2304 if (this->is_array()) {
2306 if (this->without_array()->is_struct())
2307 stride
= this->without_array()->std430_size(row_major
);
2309 stride
= this->without_array()->std430_base_alignment(row_major
);
2311 unsigned size
= this->arrays_of_arrays_size() * stride
;
2312 assert(this->explicit_stride
== 0 ||
2313 size
== this->length
* this->explicit_stride
);
2317 if (this->is_struct() || this->is_interface()) {
2319 unsigned max_align
= 0;
2321 for (unsigned i
= 0; i
< this->length
; i
++) {
2322 bool field_row_major
= row_major
;
2323 const enum glsl_matrix_layout matrix_layout
=
2324 glsl_matrix_layout(this->fields
.structure
[i
].matrix_layout
);
2325 if (matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
2326 field_row_major
= true;
2327 } else if (matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
2328 field_row_major
= false;
2331 const struct glsl_type
*field_type
= this->fields
.structure
[i
].type
;
2332 unsigned align
= field_type
->std430_base_alignment(field_row_major
);
2333 size
= glsl_align(size
, align
);
2334 size
+= field_type
->std430_size(field_row_major
);
2336 max_align
= MAX2(align
, max_align
);
2338 size
= glsl_align(size
, max_align
);
2342 assert(!"not reached");
2347 glsl_type::get_explicit_std430_type(bool row_major
) const
2349 if (this->is_vector() || this->is_scalar()) {
2351 } else if (this->is_matrix()) {
2352 const glsl_type
*vec_type
;
2354 vec_type
= get_instance(this->base_type
, this->matrix_columns
, 1);
2356 vec_type
= get_instance(this->base_type
, this->vector_elements
, 1);
2357 unsigned stride
= vec_type
->std430_array_stride(false);
2358 return get_instance(this->base_type
, this->vector_elements
,
2359 this->matrix_columns
, stride
, row_major
);
2360 } else if (this->is_array()) {
2361 const glsl_type
*elem_type
=
2362 this->fields
.array
->get_explicit_std430_type(row_major
);
2363 unsigned stride
= this->fields
.array
->std430_array_stride(row_major
);
2364 return get_array_instance(elem_type
, this->length
, stride
);
2365 } else if (this->is_struct() || this->is_interface()) {
2366 glsl_struct_field
*fields
= new glsl_struct_field
[this->length
];
2367 unsigned offset
= 0;
2368 for (unsigned i
= 0; i
< length
; i
++) {
2369 fields
[i
] = this->fields
.structure
[i
];
2371 bool field_row_major
= row_major
;
2372 if (fields
[i
].matrix_layout
== GLSL_MATRIX_LAYOUT_COLUMN_MAJOR
) {
2373 field_row_major
= false;
2374 } else if (fields
[i
].matrix_layout
== GLSL_MATRIX_LAYOUT_ROW_MAJOR
) {
2375 field_row_major
= true;
2378 fields
[i
].type
->get_explicit_std430_type(field_row_major
);
2380 unsigned fsize
= fields
[i
].type
->std430_size(field_row_major
);
2381 unsigned falign
= fields
[i
].type
->std430_base_alignment(field_row_major
);
2382 /* From the GLSL 460 spec section "Uniform and Shader Storage Block
2383 * Layout Qualifiers":
2385 * "The actual offset of a member is computed as follows: If
2386 * offset was declared, start with that offset, otherwise start
2387 * with the next available offset. If the resulting offset is not
2388 * a multiple of the actual alignment, increase it to the first
2389 * offset that is a multiple of the actual alignment. This results
2390 * in the actual offset the member will have."
2392 if (fields
[i
].offset
>= 0) {
2393 assert((unsigned)fields
[i
].offset
>= offset
);
2394 offset
= fields
[i
].offset
;
2396 offset
= glsl_align(offset
, falign
);
2397 fields
[i
].offset
= offset
;
2401 const glsl_type
*type
;
2402 if (this->is_struct())
2403 type
= get_struct_instance(fields
, this->length
, this->name
);
2405 type
= get_interface_instance(fields
, this->length
,
2406 (enum glsl_interface_packing
)this->interface_packing
,
2407 this->interface_row_major
,
2413 unreachable("Invalid type for SSBO");
2418 glsl_type::get_explicit_interface_type(bool supports_std430
) const
2420 enum glsl_interface_packing packing
=
2421 this->get_internal_ifc_packing(supports_std430
);
2422 if (packing
== GLSL_INTERFACE_PACKING_STD140
) {
2423 return this->get_explicit_std140_type(this->interface_row_major
);
2425 assert(packing
== GLSL_INTERFACE_PACKING_STD430
);
2426 return this->get_explicit_std430_type(this->interface_row_major
);
2430 /* This differs from get_explicit_std430_type() in that it:
2431 * - can size arrays slightly smaller ("stride * (len - 1) + elem_size" instead
2432 * of "stride * len")
2433 * - consumes a glsl_type_size_align_func which allows 8 and 16-bit values to be
2434 * packed more tightly
2435 * - overrides any struct field offsets but get_explicit_std430_type() tries to
2436 * respect any existing ones
2439 glsl_type::get_explicit_type_for_size_align(glsl_type_size_align_func type_info
,
2440 unsigned *size
, unsigned *alignment
) const
2442 if (this->is_scalar() || this->is_vector()) {
2443 type_info(this, size
, alignment
);
2445 } else if (this->is_array()) {
2446 unsigned elem_size
, elem_align
;
2447 const struct glsl_type
*explicit_element
=
2448 this->fields
.array
->get_explicit_type_for_size_align(type_info
, &elem_size
, &elem_align
);
2450 unsigned stride
= align(elem_size
, elem_align
);
2452 *size
= stride
* (this->length
- 1) + elem_size
;
2453 *alignment
= elem_align
;
2454 return glsl_type::get_array_instance(explicit_element
, this->length
, stride
);
2455 } else if (this->is_struct() || this->is_interface()) {
2456 struct glsl_struct_field
*fields
= (struct glsl_struct_field
*)
2457 malloc(sizeof(struct glsl_struct_field
) * this->length
);
2461 for (unsigned i
= 0; i
< this->length
; i
++) {
2462 fields
[i
] = this->fields
.structure
[i
];
2463 assert(fields
[i
].matrix_layout
!= GLSL_MATRIX_LAYOUT_ROW_MAJOR
);
2465 unsigned field_size
, field_align
;
2467 fields
[i
].type
->get_explicit_type_for_size_align(type_info
, &field_size
, &field_align
);
2468 field_align
= this->packed
? 1 : field_align
;
2469 fields
[i
].offset
= align(*size
, field_align
);
2471 *size
= fields
[i
].offset
+ field_size
;
2472 *alignment
= MAX2(*alignment
, field_align
);
2475 const glsl_type
*type
;
2476 if (this->is_struct()) {
2477 type
= get_struct_instance(fields
, this->length
, this->name
,
2480 assert(!this->packed
);
2481 type
= get_interface_instance(fields
, this->length
,
2482 (enum glsl_interface_packing
)this->interface_packing
,
2483 this->interface_row_major
,
2488 } else if (this->is_matrix()) {
2489 unsigned col_size
, col_align
;
2490 type_info(this->column_type(), &col_size
, &col_align
);
2491 unsigned stride
= align(col_size
, col_align
);
2493 *size
= this->matrix_columns
* stride
;
2494 *alignment
= col_align
;
2495 return glsl_type::get_instance(this->base_type
, this->vector_elements
,
2496 this->matrix_columns
, stride
, false);
2498 unreachable("Unhandled type.");
2503 glsl_type::count_vec4_slots(bool is_gl_vertex_input
, bool is_bindless
) const
2505 /* From page 31 (page 37 of the PDF) of the GLSL 1.50 spec:
2507 * "A scalar input counts the same amount against this limit as a vec4,
2508 * so applications may want to consider packing groups of four
2509 * unrelated float inputs together into a vector to better utilize the
2510 * capabilities of the underlying hardware. A matrix input will use up
2511 * multiple locations. The number of locations used will equal the
2512 * number of columns in the matrix."
2514 * The spec does not explicitly say how arrays are counted. However, it
2515 * should be safe to assume the total number of slots consumed by an array
2516 * is the number of entries in the array multiplied by the number of slots
2517 * consumed by a single element of the array.
2519 * The spec says nothing about how structs are counted, because vertex
2520 * attributes are not allowed to be (or contain) structs. However, Mesa
2521 * allows varying structs, the number of varying slots taken up by a
2522 * varying struct is simply equal to the sum of the number of slots taken
2523 * up by each element.
2525 * Doubles are counted different depending on whether they are vertex
2526 * inputs or everything else. Vertex inputs from ARB_vertex_attrib_64bit
2527 * take one location no matter what size they are, otherwise dvec3/4
2528 * take two locations.
2530 switch (this->base_type
) {
2531 case GLSL_TYPE_UINT
:
2533 case GLSL_TYPE_UINT8
:
2534 case GLSL_TYPE_INT8
:
2535 case GLSL_TYPE_UINT16
:
2536 case GLSL_TYPE_INT16
:
2537 case GLSL_TYPE_FLOAT
:
2538 case GLSL_TYPE_FLOAT16
:
2539 case GLSL_TYPE_BOOL
:
2540 return this->matrix_columns
;
2541 case GLSL_TYPE_DOUBLE
:
2542 case GLSL_TYPE_UINT64
:
2543 case GLSL_TYPE_INT64
:
2544 if (this->vector_elements
> 2 && !is_gl_vertex_input
)
2545 return this->matrix_columns
* 2;
2547 return this->matrix_columns
;
2548 case GLSL_TYPE_STRUCT
:
2549 case GLSL_TYPE_INTERFACE
: {
2552 for (unsigned i
= 0; i
< this->length
; i
++) {
2553 const glsl_type
*member_type
= this->fields
.structure
[i
].type
;
2554 size
+= member_type
->count_vec4_slots(is_gl_vertex_input
, is_bindless
);
2560 case GLSL_TYPE_ARRAY
: {
2561 const glsl_type
*element
= this->fields
.array
;
2562 return this->length
* element
->count_vec4_slots(is_gl_vertex_input
,
2566 case GLSL_TYPE_SAMPLER
:
2567 case GLSL_TYPE_IMAGE
:
2573 case GLSL_TYPE_SUBROUTINE
:
2576 case GLSL_TYPE_FUNCTION
:
2577 case GLSL_TYPE_ATOMIC_UINT
:
2578 case GLSL_TYPE_VOID
:
2579 case GLSL_TYPE_ERROR
:
2583 assert(!"Unexpected type in count_attribute_slots()");
2589 glsl_type::count_dword_slots(bool is_bindless
) const
2591 switch (this->base_type
) {
2592 case GLSL_TYPE_UINT
:
2594 case GLSL_TYPE_FLOAT
:
2595 case GLSL_TYPE_BOOL
:
2596 return this->components();
2597 case GLSL_TYPE_UINT16
:
2598 case GLSL_TYPE_INT16
:
2599 case GLSL_TYPE_FLOAT16
:
2600 return DIV_ROUND_UP(this->components(), 2);
2601 case GLSL_TYPE_UINT8
:
2602 case GLSL_TYPE_INT8
:
2603 return DIV_ROUND_UP(this->components(), 4);
2604 case GLSL_TYPE_IMAGE
:
2605 case GLSL_TYPE_SAMPLER
:
2609 case GLSL_TYPE_DOUBLE
:
2610 case GLSL_TYPE_UINT64
:
2611 case GLSL_TYPE_INT64
:
2612 return this->components() * 2;
2613 case GLSL_TYPE_ARRAY
:
2614 return this->fields
.array
->count_dword_slots(is_bindless
) *
2617 case GLSL_TYPE_INTERFACE
:
2618 case GLSL_TYPE_STRUCT
: {
2620 for (unsigned i
= 0; i
< this->length
; i
++) {
2621 size
+= this->fields
.structure
[i
].type
->count_dword_slots(is_bindless
);
2626 case GLSL_TYPE_ATOMIC_UINT
:
2628 case GLSL_TYPE_SUBROUTINE
:
2630 case GLSL_TYPE_VOID
:
2631 case GLSL_TYPE_ERROR
:
2632 case GLSL_TYPE_FUNCTION
:
2634 unreachable("invalid type in st_glsl_type_dword_size()");
2641 glsl_type::coordinate_components() const
2643 enum glsl_sampler_dim dim
= (enum glsl_sampler_dim
)sampler_dimensionality
;
2644 int size
= glsl_get_sampler_dim_coordinate_components(dim
);
2646 /* Array textures need an additional component for the array index, except
2647 * for cubemap array images that behave like a 2D array of interleaved
2650 if (sampler_array
&&
2651 !(is_image() && sampler_dimensionality
== GLSL_SAMPLER_DIM_CUBE
))
2658 * Declarations of type flyweights (glsl_type::_foo_type) and
2659 * convenience pointers (glsl_type::foo_type).
2662 #define DECL_TYPE(NAME, ...) \
2663 const glsl_type glsl_type::_##NAME##_type = glsl_type(__VA_ARGS__, #NAME); \
2664 const glsl_type *const glsl_type::NAME##_type = &glsl_type::_##NAME##_type;
2666 #define STRUCT_TYPE(NAME)
2668 #include "compiler/builtin_type_macros.h"
2674 unsigned base_type
:5;
2675 unsigned interface_row_major
:1;
2676 unsigned vector_elements
:3;
2677 unsigned matrix_columns
:3;
2678 unsigned explicit_stride
:20;
2681 unsigned base_type
:5;
2682 unsigned dimensionality
:4;
2685 unsigned sampled_type
:5;
2689 unsigned base_type
:5;
2691 unsigned explicit_stride
:14;
2694 unsigned base_type
:5;
2695 unsigned interface_packing_or_packed
:2;
2696 unsigned interface_row_major
:1;
2702 encode_glsl_struct_field(blob
*blob
, const glsl_struct_field
*struct_field
)
2704 encode_type_to_blob(blob
, struct_field
->type
);
2705 blob_write_string(blob
, struct_field
->name
);
2706 blob_write_uint32(blob
, struct_field
->location
);
2707 blob_write_uint32(blob
, struct_field
->offset
);
2708 blob_write_uint32(blob
, struct_field
->xfb_buffer
);
2709 blob_write_uint32(blob
, struct_field
->xfb_stride
);
2710 blob_write_uint32(blob
, struct_field
->image_format
);
2711 blob_write_uint32(blob
, struct_field
->flags
);
2715 decode_glsl_struct_field_from_blob(blob_reader
*blob
, glsl_struct_field
*struct_field
)
2717 struct_field
->type
= decode_type_from_blob(blob
);
2718 struct_field
->name
= blob_read_string(blob
);
2719 struct_field
->location
= blob_read_uint32(blob
);
2720 struct_field
->offset
= blob_read_uint32(blob
);
2721 struct_field
->xfb_buffer
= blob_read_uint32(blob
);
2722 struct_field
->xfb_stride
= blob_read_uint32(blob
);
2723 struct_field
->image_format
= (pipe_format
)blob_read_uint32(blob
);
2724 struct_field
->flags
= blob_read_uint32(blob
);
2728 encode_type_to_blob(struct blob
*blob
, const glsl_type
*type
)
2731 blob_write_uint32(blob
, 0);
2735 STATIC_ASSERT(sizeof(union packed_type
) == 4);
2736 union packed_type encoded
;
2738 encoded
.basic
.base_type
= type
->base_type
;
2740 switch (type
->base_type
) {
2741 case GLSL_TYPE_UINT
:
2743 case GLSL_TYPE_FLOAT
:
2744 case GLSL_TYPE_FLOAT16
:
2745 case GLSL_TYPE_DOUBLE
:
2746 case GLSL_TYPE_UINT8
:
2747 case GLSL_TYPE_INT8
:
2748 case GLSL_TYPE_UINT16
:
2749 case GLSL_TYPE_INT16
:
2750 case GLSL_TYPE_UINT64
:
2751 case GLSL_TYPE_INT64
:
2752 case GLSL_TYPE_BOOL
:
2753 encoded
.basic
.interface_row_major
= type
->interface_row_major
;
2754 assert(type
->matrix_columns
< 8);
2755 if (type
->vector_elements
<= 4)
2756 encoded
.basic
.vector_elements
= type
->vector_elements
;
2757 else if (type
->vector_elements
== 8)
2758 encoded
.basic
.vector_elements
= 5;
2759 else if (type
->vector_elements
== 16)
2760 encoded
.basic
.vector_elements
= 6;
2761 encoded
.basic
.matrix_columns
= type
->matrix_columns
;
2762 encoded
.basic
.explicit_stride
= MIN2(type
->explicit_stride
, 0xfffff);
2763 blob_write_uint32(blob
, encoded
.u32
);
2764 /* If we don't have enough bits for explicit_stride, store it
2767 if (encoded
.basic
.explicit_stride
== 0xfffff)
2768 blob_write_uint32(blob
, type
->explicit_stride
);
2770 case GLSL_TYPE_SAMPLER
:
2771 encoded
.sampler
.dimensionality
= type
->sampler_dimensionality
;
2772 encoded
.sampler
.shadow
= type
->sampler_shadow
;
2773 encoded
.sampler
.array
= type
->sampler_array
;
2774 encoded
.sampler
.sampled_type
= type
->sampled_type
;
2776 case GLSL_TYPE_SUBROUTINE
:
2777 blob_write_uint32(blob
, encoded
.u32
);
2778 blob_write_string(blob
, type
->name
);
2780 case GLSL_TYPE_IMAGE
:
2781 encoded
.sampler
.dimensionality
= type
->sampler_dimensionality
;
2782 encoded
.sampler
.array
= type
->sampler_array
;
2783 encoded
.sampler
.sampled_type
= type
->sampled_type
;
2785 case GLSL_TYPE_ATOMIC_UINT
:
2787 case GLSL_TYPE_ARRAY
:
2788 encoded
.array
.length
= MIN2(type
->length
, 0x1fff);
2789 encoded
.array
.explicit_stride
= MIN2(type
->explicit_stride
, 0x3fff);
2790 blob_write_uint32(blob
, encoded
.u32
);
2791 /* If we don't have enough bits for length or explicit_stride, store it
2794 if (encoded
.array
.length
== 0x1fff)
2795 blob_write_uint32(blob
, type
->length
);
2796 if (encoded
.array
.explicit_stride
== 0x3fff)
2797 blob_write_uint32(blob
, type
->explicit_stride
);
2798 encode_type_to_blob(blob
, type
->fields
.array
);
2800 case GLSL_TYPE_STRUCT
:
2801 case GLSL_TYPE_INTERFACE
:
2802 encoded
.strct
.length
= MIN2(type
->length
, 0xffffff);
2803 if (type
->is_interface()) {
2804 encoded
.strct
.interface_packing_or_packed
= type
->interface_packing
;
2805 encoded
.strct
.interface_row_major
= type
->interface_row_major
;
2807 encoded
.strct
.interface_packing_or_packed
= type
->packed
;
2809 blob_write_uint32(blob
, encoded
.u32
);
2810 blob_write_string(blob
, type
->name
);
2812 /* If we don't have enough bits for length, store it separately. */
2813 if (encoded
.strct
.length
== 0xffffff)
2814 blob_write_uint32(blob
, type
->length
);
2816 for (unsigned i
= 0; i
< type
->length
; i
++)
2817 encode_glsl_struct_field(blob
, &type
->fields
.structure
[i
]);
2819 case GLSL_TYPE_VOID
:
2821 case GLSL_TYPE_ERROR
:
2823 assert(!"Cannot encode type!");
2828 blob_write_uint32(blob
, encoded
.u32
);
2832 decode_type_from_blob(struct blob_reader
*blob
)
2834 union packed_type encoded
;
2835 encoded
.u32
= blob_read_uint32(blob
);
2837 if (encoded
.u32
== 0) {
2841 glsl_base_type base_type
= (glsl_base_type
)encoded
.basic
.base_type
;
2843 switch (base_type
) {
2844 case GLSL_TYPE_UINT
:
2846 case GLSL_TYPE_FLOAT
:
2847 case GLSL_TYPE_FLOAT16
:
2848 case GLSL_TYPE_DOUBLE
:
2849 case GLSL_TYPE_UINT8
:
2850 case GLSL_TYPE_INT8
:
2851 case GLSL_TYPE_UINT16
:
2852 case GLSL_TYPE_INT16
:
2853 case GLSL_TYPE_UINT64
:
2854 case GLSL_TYPE_INT64
:
2855 case GLSL_TYPE_BOOL
: {
2856 unsigned explicit_stride
= encoded
.basic
.explicit_stride
;
2857 if (explicit_stride
== 0xfffff)
2858 explicit_stride
= blob_read_uint32(blob
);
2859 uint32_t vector_elements
= encoded
.basic
.vector_elements
;
2860 if (vector_elements
== 5)
2861 vector_elements
= 8;
2862 else if (vector_elements
== 6)
2863 vector_elements
= 16;
2864 return glsl_type::get_instance(base_type
, encoded
.basic
.vector_elements
,
2865 encoded
.basic
.matrix_columns
,
2867 encoded
.basic
.interface_row_major
);
2869 case GLSL_TYPE_SAMPLER
:
2870 return glsl_type::get_sampler_instance((enum glsl_sampler_dim
)encoded
.sampler
.dimensionality
,
2871 encoded
.sampler
.shadow
,
2872 encoded
.sampler
.array
,
2873 (glsl_base_type
) encoded
.sampler
.sampled_type
);
2874 case GLSL_TYPE_SUBROUTINE
:
2875 return glsl_type::get_subroutine_instance(blob_read_string(blob
));
2876 case GLSL_TYPE_IMAGE
:
2877 return glsl_type::get_image_instance((enum glsl_sampler_dim
)encoded
.sampler
.dimensionality
,
2878 encoded
.sampler
.array
,
2879 (glsl_base_type
) encoded
.sampler
.sampled_type
);
2880 case GLSL_TYPE_ATOMIC_UINT
:
2881 return glsl_type::atomic_uint_type
;
2882 case GLSL_TYPE_ARRAY
: {
2883 unsigned length
= encoded
.array
.length
;
2884 if (length
== 0x1fff)
2885 length
= blob_read_uint32(blob
);
2886 unsigned explicit_stride
= encoded
.array
.explicit_stride
;
2887 if (explicit_stride
== 0x3fff)
2888 explicit_stride
= blob_read_uint32(blob
);
2889 return glsl_type::get_array_instance(decode_type_from_blob(blob
),
2890 length
, explicit_stride
);
2892 case GLSL_TYPE_STRUCT
:
2893 case GLSL_TYPE_INTERFACE
: {
2894 char *name
= blob_read_string(blob
);
2895 unsigned num_fields
= encoded
.strct
.length
;
2896 if (num_fields
== 0xffffff)
2897 num_fields
= blob_read_uint32(blob
);
2899 glsl_struct_field
*fields
=
2900 (glsl_struct_field
*) malloc(sizeof(glsl_struct_field
) * num_fields
);
2901 for (unsigned i
= 0; i
< num_fields
; i
++)
2902 decode_glsl_struct_field_from_blob(blob
, &fields
[i
]);
2905 if (base_type
== GLSL_TYPE_INTERFACE
) {
2906 enum glsl_interface_packing packing
=
2907 (glsl_interface_packing
) encoded
.strct
.interface_packing_or_packed
;
2908 bool row_major
= encoded
.strct
.interface_row_major
;
2909 t
= glsl_type::get_interface_instance(fields
, num_fields
, packing
,
2912 unsigned packed
= encoded
.strct
.interface_packing_or_packed
;
2913 t
= glsl_type::get_struct_instance(fields
, num_fields
, name
, packed
);
2919 case GLSL_TYPE_VOID
:
2920 return glsl_type::void_type
;
2921 case GLSL_TYPE_ERROR
:
2923 assert(!"Cannot decode type!");
2929 glsl_type::cl_alignment() const
2931 /* vectors unlike arrays are aligned to their size */
2932 if (this->is_scalar() || this->is_vector())
2933 return this->cl_size();
2934 else if (this->is_array())
2935 return this->without_array()->cl_alignment();
2936 else if (this->is_struct()) {
2937 /* Packed Structs are 0x1 aligned despite their size. */
2942 for (unsigned i
= 0; i
< this->length
; ++i
) {
2943 struct glsl_struct_field
&field
= this->fields
.structure
[i
];
2944 res
= MAX2(res
, field
.type
->cl_alignment());
2952 glsl_type::cl_size() const
2954 if (this->is_scalar()) {
2955 return glsl_base_type_get_bit_size(this->base_type
) / 8;
2956 } else if (this->is_vector()) {
2957 unsigned vec_elemns
= this->vector_elements
== 3 ? 4 : this->vector_elements
;
2958 return vec_elemns
* glsl_base_type_get_bit_size(this->base_type
) / 8;
2959 } else if (this->is_array()) {
2960 unsigned size
= this->without_array()->cl_size();
2961 return size
* this->length
;
2962 } else if (this->is_struct()) {
2964 for (unsigned i
= 0; i
< this->length
; ++i
) {
2965 struct glsl_struct_field
&field
= this->fields
.structure
[i
];
2966 /* if a struct is packed, members don't get aligned */
2968 size
= align(size
, field
.type
->cl_alignment());
2969 size
+= field
.type
->cl_size();
2979 glsl_get_sampler_dim_coordinate_components(enum glsl_sampler_dim dim
)
2982 case GLSL_SAMPLER_DIM_1D
:
2983 case GLSL_SAMPLER_DIM_BUF
:
2985 case GLSL_SAMPLER_DIM_2D
:
2986 case GLSL_SAMPLER_DIM_RECT
:
2987 case GLSL_SAMPLER_DIM_MS
:
2988 case GLSL_SAMPLER_DIM_EXTERNAL
:
2989 case GLSL_SAMPLER_DIM_SUBPASS
:
2990 case GLSL_SAMPLER_DIM_SUBPASS_MS
:
2992 case GLSL_SAMPLER_DIM_3D
:
2993 case GLSL_SAMPLER_DIM_CUBE
:
2996 unreachable("Unknown sampler dim");
3001 glsl_print_type(FILE *f
, const glsl_type
*t
)
3003 if (t
->is_array()) {
3004 fprintf(f
, "(array ");
3005 glsl_print_type(f
, t
->fields
.array
);
3006 fprintf(f
, " %u)", t
->length
);
3007 } else if (t
->is_struct() && !is_gl_identifier(t
->name
)) {
3008 fprintf(f
, "%s@%p", t
->name
, (void *) t
);
3010 fprintf(f
, "%s", t
->name
);